axp20x-regulator.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * AXP20x regulators driver.
  3. *
  4. * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file "COPYING" in the main directory of this
  8. * archive for more details.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/axp20x.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/of_regulator.h>
  25. #define AXP20X_IO_ENABLED 0x03
  26. #define AXP20X_IO_DISABLED 0x07
  27. #define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
  28. #define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
  29. #define AXP20X_FREQ_DCDC_MASK 0x0f
  30. #define AXP20X_DESC_IO(_id, _supply, _min, _max, _step, _vreg, _vmask, _ereg, \
  31. _emask, _enable_val, _disable_val) \
  32. [AXP20X_##_id] = { \
  33. .name = #_id, \
  34. .supply_name = (_supply), \
  35. .type = REGULATOR_VOLTAGE, \
  36. .id = AXP20X_##_id, \
  37. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  38. .owner = THIS_MODULE, \
  39. .min_uV = (_min) * 1000, \
  40. .uV_step = (_step) * 1000, \
  41. .vsel_reg = (_vreg), \
  42. .vsel_mask = (_vmask), \
  43. .enable_reg = (_ereg), \
  44. .enable_mask = (_emask), \
  45. .enable_val = (_enable_val), \
  46. .disable_val = (_disable_val), \
  47. .ops = &axp20x_ops, \
  48. }
  49. #define AXP20X_DESC(_id, _supply, _min, _max, _step, _vreg, _vmask, _ereg, \
  50. _emask) \
  51. [AXP20X_##_id] = { \
  52. .name = #_id, \
  53. .supply_name = (_supply), \
  54. .type = REGULATOR_VOLTAGE, \
  55. .id = AXP20X_##_id, \
  56. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  57. .owner = THIS_MODULE, \
  58. .min_uV = (_min) * 1000, \
  59. .uV_step = (_step) * 1000, \
  60. .vsel_reg = (_vreg), \
  61. .vsel_mask = (_vmask), \
  62. .enable_reg = (_ereg), \
  63. .enable_mask = (_emask), \
  64. .ops = &axp20x_ops, \
  65. }
  66. #define AXP20X_DESC_FIXED(_id, _supply, _volt) \
  67. [AXP20X_##_id] = { \
  68. .name = #_id, \
  69. .supply_name = (_supply), \
  70. .type = REGULATOR_VOLTAGE, \
  71. .id = AXP20X_##_id, \
  72. .n_voltages = 1, \
  73. .owner = THIS_MODULE, \
  74. .min_uV = (_volt) * 1000, \
  75. .ops = &axp20x_ops_fixed \
  76. }
  77. #define AXP20X_DESC_TABLE(_id, _supply, _table, _vreg, _vmask, _ereg, _emask) \
  78. [AXP20X_##_id] = { \
  79. .name = #_id, \
  80. .supply_name = (_supply), \
  81. .type = REGULATOR_VOLTAGE, \
  82. .id = AXP20X_##_id, \
  83. .n_voltages = ARRAY_SIZE(_table), \
  84. .owner = THIS_MODULE, \
  85. .vsel_reg = (_vreg), \
  86. .vsel_mask = (_vmask), \
  87. .enable_reg = (_ereg), \
  88. .enable_mask = (_emask), \
  89. .volt_table = (_table), \
  90. .ops = &axp20x_ops_table, \
  91. }
  92. static const int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000,
  93. 1700000, 1800000, 1900000, 2000000, 2500000,
  94. 2700000, 2800000, 3000000, 3100000, 3200000,
  95. 3300000 };
  96. static struct regulator_ops axp20x_ops_fixed = {
  97. .list_voltage = regulator_list_voltage_linear,
  98. };
  99. static struct regulator_ops axp20x_ops_table = {
  100. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  101. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  102. .list_voltage = regulator_list_voltage_table,
  103. .map_voltage = regulator_map_voltage_ascend,
  104. .enable = regulator_enable_regmap,
  105. .disable = regulator_disable_regmap,
  106. .is_enabled = regulator_is_enabled_regmap,
  107. };
  108. static struct regulator_ops axp20x_ops = {
  109. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  110. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  111. .list_voltage = regulator_list_voltage_linear,
  112. .enable = regulator_enable_regmap,
  113. .disable = regulator_disable_regmap,
  114. .is_enabled = regulator_is_enabled_regmap,
  115. };
  116. static const struct regulator_desc axp20x_regulators[] = {
  117. AXP20X_DESC(DCDC2, "vin2", 700, 2275, 25, AXP20X_DCDC2_V_OUT, 0x3f,
  118. AXP20X_PWR_OUT_CTRL, 0x10),
  119. AXP20X_DESC(DCDC3, "vin3", 700, 3500, 25, AXP20X_DCDC3_V_OUT, 0x7f,
  120. AXP20X_PWR_OUT_CTRL, 0x02),
  121. AXP20X_DESC_FIXED(LDO1, "acin", 1300),
  122. AXP20X_DESC(LDO2, "ldo24in", 1800, 3300, 100, AXP20X_LDO24_V_OUT, 0xf0,
  123. AXP20X_PWR_OUT_CTRL, 0x04),
  124. AXP20X_DESC(LDO3, "ldo3in", 700, 3500, 25, AXP20X_LDO3_V_OUT, 0x7f,
  125. AXP20X_PWR_OUT_CTRL, 0x40),
  126. AXP20X_DESC_TABLE(LDO4, "ldo24in", axp20x_ldo4_data, AXP20X_LDO24_V_OUT, 0x0f,
  127. AXP20X_PWR_OUT_CTRL, 0x08),
  128. AXP20X_DESC_IO(LDO5, "ldo5in", 1800, 3300, 100, AXP20X_LDO5_V_OUT, 0xf0,
  129. AXP20X_GPIO0_CTRL, 0x07, AXP20X_IO_ENABLED,
  130. AXP20X_IO_DISABLED),
  131. };
  132. #define AXP_MATCH(_name, _id) \
  133. [AXP20X_##_id] = { \
  134. .name = #_name, \
  135. .driver_data = (void *) &axp20x_regulators[AXP20X_##_id], \
  136. }
  137. static struct of_regulator_match axp20x_matches[] = {
  138. AXP_MATCH(dcdc2, DCDC2),
  139. AXP_MATCH(dcdc3, DCDC3),
  140. AXP_MATCH(ldo1, LDO1),
  141. AXP_MATCH(ldo2, LDO2),
  142. AXP_MATCH(ldo3, LDO3),
  143. AXP_MATCH(ldo4, LDO4),
  144. AXP_MATCH(ldo5, LDO5),
  145. };
  146. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  147. {
  148. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  149. if (dcdcfreq < 750) {
  150. dcdcfreq = 750;
  151. dev_warn(&pdev->dev, "DCDC frequency too low. Set to 750kHz\n");
  152. }
  153. if (dcdcfreq > 1875) {
  154. dcdcfreq = 1875;
  155. dev_warn(&pdev->dev, "DCDC frequency too high. Set to 1875kHz\n");
  156. }
  157. dcdcfreq = (dcdcfreq - 750) / 75;
  158. return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
  159. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  160. }
  161. static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  162. {
  163. struct device_node *np, *regulators;
  164. int ret;
  165. u32 dcdcfreq;
  166. np = of_node_get(pdev->dev.parent->of_node);
  167. if (!np)
  168. return 0;
  169. regulators = of_get_child_by_name(np, "regulators");
  170. if (!regulators) {
  171. dev_warn(&pdev->dev, "regulators node not found\n");
  172. } else {
  173. ret = of_regulator_match(&pdev->dev, regulators, axp20x_matches,
  174. ARRAY_SIZE(axp20x_matches));
  175. if (ret < 0) {
  176. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
  177. return ret;
  178. }
  179. dcdcfreq = 1500;
  180. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  181. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  182. if (ret < 0) {
  183. dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  184. return ret;
  185. }
  186. of_node_put(regulators);
  187. }
  188. return 0;
  189. }
  190. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  191. {
  192. unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
  193. if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  194. return -EINVAL;
  195. if (id == AXP20X_DCDC3)
  196. mask = AXP20X_WORKMODE_DCDC3_MASK;
  197. workmode <<= ffs(mask) - 1;
  198. return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
  199. }
  200. static int axp20x_regulator_probe(struct platform_device *pdev)
  201. {
  202. struct regulator_dev *rdev;
  203. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  204. struct regulator_config config = { };
  205. struct regulator_init_data *init_data;
  206. int ret, i;
  207. u32 workmode;
  208. ret = axp20x_regulator_parse_dt(pdev);
  209. if (ret)
  210. return ret;
  211. for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
  212. init_data = axp20x_matches[i].init_data;
  213. config.dev = &pdev->dev;
  214. config.init_data = init_data;
  215. config.regmap = axp20x->regmap;
  216. config.of_node = axp20x_matches[i].of_node;
  217. rdev = devm_regulator_register(&pdev->dev, &axp20x_regulators[i],
  218. &config);
  219. if (IS_ERR(rdev)) {
  220. dev_err(&pdev->dev, "Failed to register %s\n",
  221. axp20x_regulators[i].name);
  222. return PTR_ERR(rdev);
  223. }
  224. ret = of_property_read_u32(axp20x_matches[i].of_node, "x-powers,dcdc-workmode",
  225. &workmode);
  226. if (!ret) {
  227. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  228. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  229. axp20x_regulators[i].name);
  230. }
  231. }
  232. return 0;
  233. }
  234. static struct platform_driver axp20x_regulator_driver = {
  235. .probe = axp20x_regulator_probe,
  236. .driver = {
  237. .name = "axp20x-regulator",
  238. .owner = THIS_MODULE,
  239. },
  240. };
  241. module_platform_driver(axp20x_regulator_driver);
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  244. MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");