arizona-ldo1.c 8.1 KB

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