arizona-ldo1.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. .owner = THIS_MODULE,
  111. };
  112. static struct regulator_ops arizona_ldo1_ops = {
  113. .list_voltage = regulator_list_voltage_linear,
  114. .map_voltage = regulator_map_voltage_linear,
  115. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  116. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  117. };
  118. static const struct regulator_desc arizona_ldo1 = {
  119. .name = "LDO1",
  120. .supply_name = "LDOVDD",
  121. .type = REGULATOR_VOLTAGE,
  122. .ops = &arizona_ldo1_ops,
  123. .vsel_reg = ARIZONA_LDO1_CONTROL_1,
  124. .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
  125. .min_uV = 900000,
  126. .uV_step = 25000,
  127. .n_voltages = 13,
  128. .enable_time = 500,
  129. .owner = THIS_MODULE,
  130. };
  131. static const struct regulator_init_data arizona_ldo1_dvfs = {
  132. .constraints = {
  133. .min_uV = 1200000,
  134. .max_uV = 1800000,
  135. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  136. REGULATOR_CHANGE_VOLTAGE,
  137. },
  138. .num_consumer_supplies = 1,
  139. };
  140. static const struct regulator_init_data arizona_ldo1_default = {
  141. .constraints = {
  142. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  143. },
  144. .num_consumer_supplies = 1,
  145. };
  146. static const struct regulator_init_data arizona_ldo1_wm5110 = {
  147. .constraints = {
  148. .min_uV = 1175000,
  149. .max_uV = 1200000,
  150. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  151. REGULATOR_CHANGE_VOLTAGE,
  152. },
  153. .num_consumer_supplies = 1,
  154. };
  155. static int arizona_ldo1_of_get_pdata(struct arizona *arizona,
  156. struct regulator_config *config,
  157. const struct regulator_desc *desc)
  158. {
  159. struct arizona_pdata *pdata = &arizona->pdata;
  160. struct arizona_ldo1 *ldo1 = config->driver_data;
  161. struct device_node *np = arizona->dev->of_node;
  162. struct device_node *init_node, *dcvdd_node;
  163. struct regulator_init_data *init_data;
  164. pdata->ldoena = of_get_named_gpio(np, "wlf,ldoena", 0);
  165. if (pdata->ldoena < 0) {
  166. dev_warn(arizona->dev,
  167. "LDOENA GPIO property missing/malformed: %d\n",
  168. pdata->ldoena);
  169. pdata->ldoena = 0;
  170. } else {
  171. config->ena_gpio_initialized = true;
  172. }
  173. init_node = of_get_child_by_name(np, "ldo1");
  174. dcvdd_node = of_parse_phandle(np, "DCVDD-supply", 0);
  175. if (init_node) {
  176. config->of_node = init_node;
  177. init_data = of_get_regulator_init_data(arizona->dev, init_node,
  178. desc);
  179. if (init_data) {
  180. init_data->consumer_supplies = &ldo1->supply;
  181. init_data->num_consumer_supplies = 1;
  182. if (dcvdd_node && dcvdd_node != init_node)
  183. arizona->external_dcvdd = true;
  184. pdata->ldo1 = init_data;
  185. }
  186. } else if (dcvdd_node) {
  187. arizona->external_dcvdd = true;
  188. }
  189. of_node_put(dcvdd_node);
  190. return 0;
  191. }
  192. static int arizona_ldo1_probe(struct platform_device *pdev)
  193. {
  194. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  195. const struct regulator_desc *desc;
  196. struct regulator_config config = { };
  197. struct arizona_ldo1 *ldo1;
  198. int ret;
  199. arizona->external_dcvdd = false;
  200. ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
  201. if (!ldo1)
  202. return -ENOMEM;
  203. ldo1->arizona = arizona;
  204. /*
  205. * Since the chip usually supplies itself we provide some
  206. * default init_data for it. This will be overridden with
  207. * platform data if provided.
  208. */
  209. switch (arizona->type) {
  210. case WM5102:
  211. case WM8997:
  212. case WM8998:
  213. case WM1814:
  214. desc = &arizona_ldo1_hc;
  215. ldo1->init_data = arizona_ldo1_dvfs;
  216. break;
  217. case WM5110:
  218. case WM8280:
  219. desc = &arizona_ldo1;
  220. ldo1->init_data = arizona_ldo1_wm5110;
  221. break;
  222. default:
  223. desc = &arizona_ldo1;
  224. ldo1->init_data = arizona_ldo1_default;
  225. break;
  226. }
  227. ldo1->init_data.consumer_supplies = &ldo1->supply;
  228. ldo1->supply.supply = "DCVDD";
  229. ldo1->supply.dev_name = dev_name(arizona->dev);
  230. config.dev = arizona->dev;
  231. config.driver_data = ldo1;
  232. config.regmap = arizona->regmap;
  233. if (IS_ENABLED(CONFIG_OF)) {
  234. if (!dev_get_platdata(arizona->dev)) {
  235. ret = arizona_ldo1_of_get_pdata(arizona, &config, desc);
  236. if (ret < 0)
  237. return ret;
  238. }
  239. }
  240. config.ena_gpio = arizona->pdata.ldoena;
  241. if (arizona->pdata.ldo1)
  242. config.init_data = arizona->pdata.ldo1;
  243. else
  244. config.init_data = &ldo1->init_data;
  245. /*
  246. * LDO1 can only be used to supply DCVDD so if it has no
  247. * consumers then DCVDD is supplied externally.
  248. */
  249. if (config.init_data->num_consumer_supplies == 0)
  250. arizona->external_dcvdd = true;
  251. ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
  252. of_node_put(config.of_node);
  253. if (IS_ERR(ldo1->regulator)) {
  254. ret = PTR_ERR(ldo1->regulator);
  255. dev_err(arizona->dev, "Failed to register LDO1 supply: %d\n",
  256. ret);
  257. return ret;
  258. }
  259. platform_set_drvdata(pdev, ldo1);
  260. return 0;
  261. }
  262. static struct platform_driver arizona_ldo1_driver = {
  263. .probe = arizona_ldo1_probe,
  264. .driver = {
  265. .name = "arizona-ldo1",
  266. },
  267. };
  268. module_platform_driver(arizona_ldo1_driver);
  269. /* Module information */
  270. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  271. MODULE_DESCRIPTION("Arizona LDO1 driver");
  272. MODULE_LICENSE("GPL");
  273. MODULE_ALIAS("platform:arizona-ldo1");