arizona-ldo1.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * arizona-ldo1.c -- LDO1 supply for Arizona devices
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/of.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #include <linux/gpio.h>
  25. #include <linux/slab.h>
  26. #include <linux/regulator/arizona-ldo1.h>
  27. #include <linux/mfd/arizona/core.h>
  28. #include <linux/mfd/arizona/pdata.h>
  29. #include <linux/mfd/arizona/registers.h>
  30. struct arizona_ldo1 {
  31. struct regulator_dev *regulator;
  32. struct regmap *regmap;
  33. struct regulator_consumer_supply supply;
  34. struct regulator_init_data init_data;
  35. };
  36. static int arizona_ldo1_hc_list_voltage(struct regulator_dev *rdev,
  37. unsigned int selector)
  38. {
  39. if (selector >= rdev->desc->n_voltages)
  40. return -EINVAL;
  41. if (selector == rdev->desc->n_voltages - 1)
  42. return 1800000;
  43. else
  44. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  45. }
  46. static int arizona_ldo1_hc_map_voltage(struct regulator_dev *rdev,
  47. int min_uV, int max_uV)
  48. {
  49. int sel;
  50. sel = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  51. if (sel >= rdev->desc->n_voltages)
  52. sel = rdev->desc->n_voltages - 1;
  53. return sel;
  54. }
  55. static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
  56. unsigned sel)
  57. {
  58. struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
  59. struct regmap *regmap = ldo->regmap;
  60. unsigned int val;
  61. int ret;
  62. if (sel == rdev->desc->n_voltages - 1)
  63. val = ARIZONA_LDO1_HI_PWR;
  64. else
  65. val = 0;
  66. ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
  67. ARIZONA_LDO1_HI_PWR, val);
  68. if (ret != 0)
  69. return ret;
  70. if (val)
  71. return 0;
  72. val = sel << ARIZONA_LDO1_VSEL_SHIFT;
  73. return regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_1,
  74. ARIZONA_LDO1_VSEL_MASK, val);
  75. }
  76. static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
  77. {
  78. struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
  79. struct regmap *regmap = ldo->regmap;
  80. unsigned int val;
  81. int ret;
  82. ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
  83. if (ret != 0)
  84. return ret;
  85. if (val & ARIZONA_LDO1_HI_PWR)
  86. return rdev->desc->n_voltages - 1;
  87. ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_1, &val);
  88. if (ret != 0)
  89. return ret;
  90. return (val & ARIZONA_LDO1_VSEL_MASK) >> ARIZONA_LDO1_VSEL_SHIFT;
  91. }
  92. static const struct regulator_ops arizona_ldo1_hc_ops = {
  93. .list_voltage = arizona_ldo1_hc_list_voltage,
  94. .map_voltage = arizona_ldo1_hc_map_voltage,
  95. .get_voltage_sel = arizona_ldo1_hc_get_voltage_sel,
  96. .set_voltage_sel = arizona_ldo1_hc_set_voltage_sel,
  97. .get_bypass = regulator_get_bypass_regmap,
  98. .set_bypass = regulator_set_bypass_regmap,
  99. };
  100. static const struct regulator_desc arizona_ldo1_hc = {
  101. .name = "LDO1",
  102. .supply_name = "LDOVDD",
  103. .type = REGULATOR_VOLTAGE,
  104. .ops = &arizona_ldo1_hc_ops,
  105. .bypass_reg = ARIZONA_LDO1_CONTROL_1,
  106. .bypass_mask = ARIZONA_LDO1_BYPASS,
  107. .min_uV = 900000,
  108. .uV_step = 50000,
  109. .n_voltages = 8,
  110. .enable_time = 1500,
  111. .ramp_delay = 24000,
  112. .owner = THIS_MODULE,
  113. };
  114. static const struct regulator_ops arizona_ldo1_ops = {
  115. .list_voltage = regulator_list_voltage_linear,
  116. .map_voltage = regulator_map_voltage_linear,
  117. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  118. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  119. };
  120. static const struct regulator_desc arizona_ldo1 = {
  121. .name = "LDO1",
  122. .supply_name = "LDOVDD",
  123. .type = REGULATOR_VOLTAGE,
  124. .ops = &arizona_ldo1_ops,
  125. .vsel_reg = ARIZONA_LDO1_CONTROL_1,
  126. .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
  127. .min_uV = 900000,
  128. .uV_step = 25000,
  129. .n_voltages = 13,
  130. .enable_time = 500,
  131. .ramp_delay = 24000,
  132. .owner = THIS_MODULE,
  133. };
  134. static const struct regulator_init_data arizona_ldo1_dvfs = {
  135. .constraints = {
  136. .min_uV = 1200000,
  137. .max_uV = 1800000,
  138. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  139. REGULATOR_CHANGE_VOLTAGE,
  140. },
  141. .num_consumer_supplies = 1,
  142. };
  143. static const struct regulator_init_data arizona_ldo1_default = {
  144. .constraints = {
  145. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  146. },
  147. .num_consumer_supplies = 1,
  148. };
  149. static const struct regulator_init_data arizona_ldo1_wm5110 = {
  150. .constraints = {
  151. .min_uV = 1175000,
  152. .max_uV = 1200000,
  153. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  154. REGULATOR_CHANGE_VOLTAGE,
  155. },
  156. .num_consumer_supplies = 1,
  157. };
  158. static int arizona_ldo1_of_get_pdata(struct arizona_ldo1_pdata *pdata,
  159. struct regulator_config *config,
  160. const struct regulator_desc *desc,
  161. bool *external_dcvdd)
  162. {
  163. struct arizona_ldo1 *ldo1 = config->driver_data;
  164. struct device_node *np = config->dev->of_node;
  165. struct device_node *init_node, *dcvdd_node;
  166. struct regulator_init_data *init_data;
  167. pdata->ldoena = of_get_named_gpio(np, "wlf,ldoena", 0);
  168. if (pdata->ldoena < 0) {
  169. dev_warn(config->dev,
  170. "LDOENA GPIO property missing/malformed: %d\n",
  171. pdata->ldoena);
  172. pdata->ldoena = 0;
  173. } else {
  174. config->ena_gpio_initialized = true;
  175. }
  176. init_node = of_get_child_by_name(np, "ldo1");
  177. dcvdd_node = of_parse_phandle(np, "DCVDD-supply", 0);
  178. if (init_node) {
  179. config->of_node = init_node;
  180. init_data = of_get_regulator_init_data(config->dev, init_node,
  181. desc);
  182. if (init_data) {
  183. init_data->consumer_supplies = &ldo1->supply;
  184. init_data->num_consumer_supplies = 1;
  185. if (dcvdd_node && dcvdd_node != init_node)
  186. *external_dcvdd = true;
  187. pdata->init_data = init_data;
  188. }
  189. } else if (dcvdd_node) {
  190. *external_dcvdd = true;
  191. }
  192. of_node_put(dcvdd_node);
  193. return 0;
  194. }
  195. static int arizona_ldo1_common_init(struct platform_device *pdev,
  196. struct arizona_ldo1 *ldo1,
  197. const struct regulator_desc *desc,
  198. struct arizona_ldo1_pdata *pdata,
  199. bool *external_dcvdd)
  200. {
  201. struct device *parent_dev = pdev->dev.parent;
  202. struct regulator_config config = { };
  203. int ret;
  204. *external_dcvdd = false;
  205. ldo1->supply.supply = "DCVDD";
  206. ldo1->init_data.consumer_supplies = &ldo1->supply;
  207. ldo1->supply.dev_name = dev_name(parent_dev);
  208. config.dev = parent_dev;
  209. config.driver_data = ldo1;
  210. config.regmap = ldo1->regmap;
  211. if (IS_ENABLED(CONFIG_OF)) {
  212. if (!dev_get_platdata(parent_dev)) {
  213. ret = arizona_ldo1_of_get_pdata(pdata,
  214. &config, desc,
  215. external_dcvdd);
  216. if (ret < 0)
  217. return ret;
  218. }
  219. }
  220. config.ena_gpio = pdata->ldoena;
  221. if (pdata->init_data)
  222. config.init_data = pdata->init_data;
  223. else
  224. config.init_data = &ldo1->init_data;
  225. /*
  226. * LDO1 can only be used to supply DCVDD so if it has no
  227. * consumers then DCVDD is supplied externally.
  228. */
  229. if (config.init_data->num_consumer_supplies == 0)
  230. *external_dcvdd = true;
  231. ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
  232. of_node_put(config.of_node);
  233. if (IS_ERR(ldo1->regulator)) {
  234. ret = PTR_ERR(ldo1->regulator);
  235. dev_err(&pdev->dev, "Failed to register LDO1 supply: %d\n",
  236. ret);
  237. return ret;
  238. }
  239. platform_set_drvdata(pdev, ldo1);
  240. return 0;
  241. }
  242. static int arizona_ldo1_probe(struct platform_device *pdev)
  243. {
  244. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  245. struct arizona_ldo1 *ldo1;
  246. const struct regulator_desc *desc;
  247. bool external_dcvdd;
  248. int ret;
  249. ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
  250. if (!ldo1)
  251. return -ENOMEM;
  252. ldo1->regmap = arizona->regmap;
  253. /*
  254. * Since the chip usually supplies itself we provide some
  255. * default init_data for it. This will be overridden with
  256. * platform data if provided.
  257. */
  258. switch (arizona->type) {
  259. case WM5102:
  260. case WM8997:
  261. case WM8998:
  262. case WM1814:
  263. desc = &arizona_ldo1_hc;
  264. ldo1->init_data = arizona_ldo1_dvfs;
  265. break;
  266. case WM5110:
  267. case WM8280:
  268. desc = &arizona_ldo1;
  269. ldo1->init_data = arizona_ldo1_wm5110;
  270. break;
  271. default:
  272. desc = &arizona_ldo1;
  273. ldo1->init_data = arizona_ldo1_default;
  274. break;
  275. }
  276. ret = arizona_ldo1_common_init(pdev, ldo1, desc,
  277. &arizona->pdata.ldo1,
  278. &external_dcvdd);
  279. if (ret == 0)
  280. arizona->external_dcvdd = external_dcvdd;
  281. return ret;
  282. }
  283. static struct platform_driver arizona_ldo1_driver = {
  284. .probe = arizona_ldo1_probe,
  285. .driver = {
  286. .name = "arizona-ldo1",
  287. },
  288. };
  289. module_platform_driver(arizona_ldo1_driver);
  290. /* Module information */
  291. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  292. MODULE_DESCRIPTION("Arizona LDO1 driver");
  293. MODULE_LICENSE("GPL");
  294. MODULE_ALIAS("platform:arizona-ldo1");