arizona-ldo1.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // arizona-ldo1.c -- LDO1 supply for Arizona devices
  4. //
  5. // Copyright 2012 Wolfson Microelectronics PLC.
  6. //
  7. // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/bitops.h>
  12. #include <linux/err.h>
  13. #include <linux/of.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #include <linux/slab.h>
  20. #include <linux/regulator/arizona-ldo1.h>
  21. #include <linux/mfd/arizona/core.h>
  22. #include <linux/mfd/arizona/pdata.h>
  23. #include <linux/mfd/arizona/registers.h>
  24. #include <linux/mfd/madera/core.h>
  25. #include <linux/mfd/madera/pdata.h>
  26. #include <linux/mfd/madera/registers.h>
  27. struct arizona_ldo1 {
  28. struct regulator_dev *regulator;
  29. struct regmap *regmap;
  30. struct regulator_consumer_supply supply;
  31. struct regulator_init_data init_data;
  32. struct gpio_desc *ena_gpiod;
  33. };
  34. static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
  35. unsigned sel)
  36. {
  37. struct regmap *regmap = rdev_get_regmap(rdev);
  38. unsigned int val;
  39. int ret;
  40. if (sel == rdev->desc->n_voltages - 1)
  41. val = ARIZONA_LDO1_HI_PWR;
  42. else
  43. val = 0;
  44. ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
  45. ARIZONA_LDO1_HI_PWR, val);
  46. if (ret != 0)
  47. return ret;
  48. if (val)
  49. return 0;
  50. return regulator_set_voltage_sel_regmap(rdev, sel);
  51. }
  52. static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
  53. {
  54. struct regmap *regmap = rdev_get_regmap(rdev);
  55. unsigned int val;
  56. int ret;
  57. ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
  58. if (ret != 0)
  59. return ret;
  60. if (val & ARIZONA_LDO1_HI_PWR)
  61. return rdev->desc->n_voltages - 1;
  62. return regulator_get_voltage_sel_regmap(rdev);
  63. }
  64. static const struct regulator_ops arizona_ldo1_hc_ops = {
  65. .list_voltage = regulator_list_voltage_linear_range,
  66. .map_voltage = regulator_map_voltage_linear_range,
  67. .get_voltage_sel = arizona_ldo1_hc_get_voltage_sel,
  68. .set_voltage_sel = arizona_ldo1_hc_set_voltage_sel,
  69. .get_bypass = regulator_get_bypass_regmap,
  70. .set_bypass = regulator_set_bypass_regmap,
  71. };
  72. static const struct regulator_linear_range arizona_ldo1_hc_ranges[] = {
  73. REGULATOR_LINEAR_RANGE(900000, 0, 0x6, 50000),
  74. REGULATOR_LINEAR_RANGE(1800000, 0x7, 0x7, 0),
  75. };
  76. static const struct regulator_desc arizona_ldo1_hc = {
  77. .name = "LDO1",
  78. .supply_name = "LDOVDD",
  79. .type = REGULATOR_VOLTAGE,
  80. .ops = &arizona_ldo1_hc_ops,
  81. .vsel_reg = ARIZONA_LDO1_CONTROL_1,
  82. .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
  83. .bypass_reg = ARIZONA_LDO1_CONTROL_1,
  84. .bypass_mask = ARIZONA_LDO1_BYPASS,
  85. .linear_ranges = arizona_ldo1_hc_ranges,
  86. .n_linear_ranges = ARRAY_SIZE(arizona_ldo1_hc_ranges),
  87. .n_voltages = 8,
  88. .enable_time = 1500,
  89. .ramp_delay = 24000,
  90. .owner = THIS_MODULE,
  91. };
  92. static const struct regulator_ops arizona_ldo1_ops = {
  93. .list_voltage = regulator_list_voltage_linear,
  94. .map_voltage = regulator_map_voltage_linear,
  95. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  96. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  97. };
  98. static const struct regulator_desc arizona_ldo1 = {
  99. .name = "LDO1",
  100. .supply_name = "LDOVDD",
  101. .type = REGULATOR_VOLTAGE,
  102. .ops = &arizona_ldo1_ops,
  103. .vsel_reg = ARIZONA_LDO1_CONTROL_1,
  104. .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
  105. .min_uV = 900000,
  106. .uV_step = 25000,
  107. .n_voltages = 13,
  108. .enable_time = 500,
  109. .ramp_delay = 24000,
  110. .owner = THIS_MODULE,
  111. };
  112. static const struct regulator_init_data arizona_ldo1_dvfs = {
  113. .constraints = {
  114. .min_uV = 1200000,
  115. .max_uV = 1800000,
  116. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  117. REGULATOR_CHANGE_VOLTAGE,
  118. },
  119. .num_consumer_supplies = 1,
  120. };
  121. static const struct regulator_init_data arizona_ldo1_default = {
  122. .constraints = {
  123. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  124. },
  125. .num_consumer_supplies = 1,
  126. };
  127. static const struct regulator_init_data arizona_ldo1_wm5110 = {
  128. .constraints = {
  129. .min_uV = 1175000,
  130. .max_uV = 1200000,
  131. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  132. REGULATOR_CHANGE_VOLTAGE,
  133. },
  134. .num_consumer_supplies = 1,
  135. };
  136. static const struct regulator_desc madera_ldo1 = {
  137. .name = "LDO1",
  138. .supply_name = "LDOVDD",
  139. .type = REGULATOR_VOLTAGE,
  140. .ops = &arizona_ldo1_ops,
  141. .vsel_reg = MADERA_LDO1_CONTROL_1,
  142. .vsel_mask = MADERA_LDO1_VSEL_MASK,
  143. .min_uV = 900000,
  144. .uV_step = 25000,
  145. .n_voltages = 13,
  146. .enable_time = 3000,
  147. .owner = THIS_MODULE,
  148. };
  149. static const struct regulator_init_data madera_ldo1_default = {
  150. .constraints = {
  151. .min_uV = 1200000,
  152. .max_uV = 1200000,
  153. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  154. },
  155. .num_consumer_supplies = 1,
  156. };
  157. static int arizona_ldo1_of_get_pdata(struct arizona_ldo1_pdata *pdata,
  158. struct regulator_config *config,
  159. const struct regulator_desc *desc,
  160. bool *external_dcvdd)
  161. {
  162. struct arizona_ldo1 *ldo1 = config->driver_data;
  163. struct device_node *np = config->dev->of_node;
  164. struct device_node *init_node, *dcvdd_node;
  165. struct regulator_init_data *init_data;
  166. init_node = of_get_child_by_name(np, "ldo1");
  167. dcvdd_node = of_parse_phandle(np, "DCVDD-supply", 0);
  168. if (init_node) {
  169. config->of_node = init_node;
  170. init_data = of_get_regulator_init_data(config->dev, init_node,
  171. desc);
  172. if (init_data) {
  173. init_data->consumer_supplies = &ldo1->supply;
  174. init_data->num_consumer_supplies = 1;
  175. if (dcvdd_node && dcvdd_node != init_node)
  176. *external_dcvdd = true;
  177. pdata->init_data = init_data;
  178. }
  179. } else if (dcvdd_node) {
  180. *external_dcvdd = true;
  181. }
  182. of_node_put(dcvdd_node);
  183. return 0;
  184. }
  185. static int arizona_ldo1_common_init(struct platform_device *pdev,
  186. struct arizona_ldo1 *ldo1,
  187. const struct regulator_desc *desc,
  188. struct arizona_ldo1_pdata *pdata,
  189. bool *external_dcvdd)
  190. {
  191. struct device *parent_dev = pdev->dev.parent;
  192. struct regulator_config config = { };
  193. int ret;
  194. *external_dcvdd = false;
  195. ldo1->supply.supply = "DCVDD";
  196. ldo1->init_data.consumer_supplies = &ldo1->supply;
  197. ldo1->supply.dev_name = dev_name(parent_dev);
  198. config.dev = parent_dev;
  199. config.driver_data = ldo1;
  200. config.regmap = ldo1->regmap;
  201. if (IS_ENABLED(CONFIG_OF)) {
  202. if (!dev_get_platdata(parent_dev)) {
  203. ret = arizona_ldo1_of_get_pdata(pdata,
  204. &config, desc,
  205. external_dcvdd);
  206. if (ret < 0)
  207. return ret;
  208. }
  209. }
  210. /* We assume that high output = regulator off
  211. * Don't use devm, since we need to get against the parent device
  212. * so clean up would happen at the wrong time
  213. */
  214. config.ena_gpiod = gpiod_get_optional(parent_dev, "wlf,ldoena",
  215. GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  216. if (IS_ERR(config.ena_gpiod))
  217. return PTR_ERR(config.ena_gpiod);
  218. ldo1->ena_gpiod = config.ena_gpiod;
  219. if (pdata->init_data)
  220. config.init_data = pdata->init_data;
  221. else
  222. config.init_data = &ldo1->init_data;
  223. /*
  224. * LDO1 can only be used to supply DCVDD so if it has no
  225. * consumers then DCVDD is supplied externally.
  226. */
  227. if (config.init_data->num_consumer_supplies == 0)
  228. *external_dcvdd = true;
  229. ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
  230. of_node_put(config.of_node);
  231. if (IS_ERR(ldo1->regulator)) {
  232. ret = PTR_ERR(ldo1->regulator);
  233. dev_err(&pdev->dev, "Failed to register LDO1 supply: %d\n",
  234. ret);
  235. return ret;
  236. }
  237. platform_set_drvdata(pdev, ldo1);
  238. return 0;
  239. }
  240. static int arizona_ldo1_probe(struct platform_device *pdev)
  241. {
  242. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  243. struct arizona_ldo1 *ldo1;
  244. const struct regulator_desc *desc;
  245. bool external_dcvdd;
  246. int ret;
  247. ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
  248. if (!ldo1)
  249. return -ENOMEM;
  250. ldo1->regmap = arizona->regmap;
  251. /*
  252. * Since the chip usually supplies itself we provide some
  253. * default init_data for it. This will be overridden with
  254. * platform data if provided.
  255. */
  256. switch (arizona->type) {
  257. case WM5102:
  258. case WM8997:
  259. case WM8998:
  260. case WM1814:
  261. desc = &arizona_ldo1_hc;
  262. ldo1->init_data = arizona_ldo1_dvfs;
  263. break;
  264. case WM5110:
  265. case WM8280:
  266. desc = &arizona_ldo1;
  267. ldo1->init_data = arizona_ldo1_wm5110;
  268. break;
  269. default:
  270. desc = &arizona_ldo1;
  271. ldo1->init_data = arizona_ldo1_default;
  272. break;
  273. }
  274. ret = arizona_ldo1_common_init(pdev, ldo1, desc,
  275. &arizona->pdata.ldo1,
  276. &external_dcvdd);
  277. if (ret == 0)
  278. arizona->external_dcvdd = external_dcvdd;
  279. return ret;
  280. }
  281. static int arizona_ldo1_remove(struct platform_device *pdev)
  282. {
  283. struct arizona_ldo1 *ldo1 = platform_get_drvdata(pdev);
  284. if (ldo1->ena_gpiod)
  285. gpiod_put(ldo1->ena_gpiod);
  286. return 0;
  287. }
  288. static int madera_ldo1_probe(struct platform_device *pdev)
  289. {
  290. struct madera *madera = dev_get_drvdata(pdev->dev.parent);
  291. struct arizona_ldo1 *ldo1;
  292. bool external_dcvdd;
  293. int ret;
  294. ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
  295. if (!ldo1)
  296. return -ENOMEM;
  297. ldo1->regmap = madera->regmap;
  298. ldo1->init_data = madera_ldo1_default;
  299. ret = arizona_ldo1_common_init(pdev, ldo1, &madera_ldo1,
  300. &madera->pdata.ldo1,
  301. &external_dcvdd);
  302. if (ret)
  303. return ret;
  304. madera->internal_dcvdd = !external_dcvdd;
  305. return 0;
  306. }
  307. static struct platform_driver arizona_ldo1_driver = {
  308. .probe = arizona_ldo1_probe,
  309. .remove = arizona_ldo1_remove,
  310. .driver = {
  311. .name = "arizona-ldo1",
  312. },
  313. };
  314. static struct platform_driver madera_ldo1_driver = {
  315. .probe = madera_ldo1_probe,
  316. .remove = arizona_ldo1_remove,
  317. .driver = {
  318. .name = "madera-ldo1",
  319. },
  320. };
  321. static struct platform_driver * const madera_ldo1_drivers[] = {
  322. &arizona_ldo1_driver,
  323. &madera_ldo1_driver,
  324. };
  325. static int __init arizona_ldo1_init(void)
  326. {
  327. return platform_register_drivers(madera_ldo1_drivers,
  328. ARRAY_SIZE(madera_ldo1_drivers));
  329. }
  330. module_init(arizona_ldo1_init);
  331. static void __exit madera_ldo1_exit(void)
  332. {
  333. platform_unregister_drivers(madera_ldo1_drivers,
  334. ARRAY_SIZE(madera_ldo1_drivers));
  335. }
  336. module_exit(madera_ldo1_exit);
  337. /* Module information */
  338. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  339. MODULE_DESCRIPTION("Arizona LDO1 driver");
  340. MODULE_LICENSE("GPL");
  341. MODULE_ALIAS("platform:arizona-ldo1");
  342. MODULE_ALIAS("platform:madera-ldo1");