axp20x-regulator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, _match, _supply, _min, _max, _step, _vreg, _vmask, \
  31. _ereg, _emask, _enable_val, _disable_val) \
  32. [AXP20X_##_id] = { \
  33. .name = #_id, \
  34. .supply_name = (_supply), \
  35. .of_match = of_match_ptr(_match), \
  36. .regulators_node = of_match_ptr("regulators"), \
  37. .type = REGULATOR_VOLTAGE, \
  38. .id = AXP20X_##_id, \
  39. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  40. .owner = THIS_MODULE, \
  41. .min_uV = (_min) * 1000, \
  42. .uV_step = (_step) * 1000, \
  43. .vsel_reg = (_vreg), \
  44. .vsel_mask = (_vmask), \
  45. .enable_reg = (_ereg), \
  46. .enable_mask = (_emask), \
  47. .enable_val = (_enable_val), \
  48. .disable_val = (_disable_val), \
  49. .ops = &axp20x_ops, \
  50. }
  51. #define AXP20X_DESC(_id, _match, _supply, _min, _max, _step, _vreg, _vmask, \
  52. _ereg, _emask) \
  53. [AXP20X_##_id] = { \
  54. .name = #_id, \
  55. .supply_name = (_supply), \
  56. .of_match = of_match_ptr(_match), \
  57. .regulators_node = of_match_ptr("regulators"), \
  58. .type = REGULATOR_VOLTAGE, \
  59. .id = AXP20X_##_id, \
  60. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  61. .owner = THIS_MODULE, \
  62. .min_uV = (_min) * 1000, \
  63. .uV_step = (_step) * 1000, \
  64. .vsel_reg = (_vreg), \
  65. .vsel_mask = (_vmask), \
  66. .enable_reg = (_ereg), \
  67. .enable_mask = (_emask), \
  68. .ops = &axp20x_ops, \
  69. }
  70. #define AXP20X_DESC_FIXED(_id, _match, _supply, _volt) \
  71. [AXP20X_##_id] = { \
  72. .name = #_id, \
  73. .supply_name = (_supply), \
  74. .of_match = of_match_ptr(_match), \
  75. .regulators_node = of_match_ptr("regulators"), \
  76. .type = REGULATOR_VOLTAGE, \
  77. .id = AXP20X_##_id, \
  78. .n_voltages = 1, \
  79. .owner = THIS_MODULE, \
  80. .min_uV = (_volt) * 1000, \
  81. .ops = &axp20x_ops_fixed \
  82. }
  83. #define AXP20X_DESC_TABLE(_id, _match, _supply, _table, _vreg, _vmask, _ereg, \
  84. _emask) \
  85. [AXP20X_##_id] = { \
  86. .name = #_id, \
  87. .supply_name = (_supply), \
  88. .of_match = of_match_ptr(_match), \
  89. .regulators_node = of_match_ptr("regulators"), \
  90. .type = REGULATOR_VOLTAGE, \
  91. .id = AXP20X_##_id, \
  92. .n_voltages = ARRAY_SIZE(_table), \
  93. .owner = THIS_MODULE, \
  94. .vsel_reg = (_vreg), \
  95. .vsel_mask = (_vmask), \
  96. .enable_reg = (_ereg), \
  97. .enable_mask = (_emask), \
  98. .volt_table = (_table), \
  99. .ops = &axp20x_ops_table, \
  100. }
  101. static const int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000,
  102. 1700000, 1800000, 1900000, 2000000, 2500000,
  103. 2700000, 2800000, 3000000, 3100000, 3200000,
  104. 3300000 };
  105. static struct regulator_ops axp20x_ops_fixed = {
  106. .list_voltage = regulator_list_voltage_linear,
  107. };
  108. static struct regulator_ops axp20x_ops_table = {
  109. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  110. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  111. .list_voltage = regulator_list_voltage_table,
  112. .map_voltage = regulator_map_voltage_ascend,
  113. .enable = regulator_enable_regmap,
  114. .disable = regulator_disable_regmap,
  115. .is_enabled = regulator_is_enabled_regmap,
  116. };
  117. static struct regulator_ops axp20x_ops = {
  118. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  119. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  120. .list_voltage = regulator_list_voltage_linear,
  121. .enable = regulator_enable_regmap,
  122. .disable = regulator_disable_regmap,
  123. .is_enabled = regulator_is_enabled_regmap,
  124. };
  125. static const struct regulator_desc axp20x_regulators[] = {
  126. AXP20X_DESC(DCDC2, "dcdc2", "vin2", 700, 2275, 25, AXP20X_DCDC2_V_OUT,
  127. 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
  128. AXP20X_DESC(DCDC3, "dcdc3", "vin3", 700, 3500, 25, AXP20X_DCDC3_V_OUT,
  129. 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
  130. AXP20X_DESC_FIXED(LDO1, "ldo1", "acin", 1300),
  131. AXP20X_DESC(LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
  132. AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
  133. AXP20X_DESC(LDO3, "ldo3", "ldo3in", 700, 3500, 25, AXP20X_LDO3_V_OUT,
  134. 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
  135. AXP20X_DESC_TABLE(LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
  136. AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
  137. AXP20X_DESC_IO(LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
  138. AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
  139. AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
  140. };
  141. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  142. {
  143. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  144. if (dcdcfreq < 750) {
  145. dcdcfreq = 750;
  146. dev_warn(&pdev->dev, "DCDC frequency too low. Set to 750kHz\n");
  147. }
  148. if (dcdcfreq > 1875) {
  149. dcdcfreq = 1875;
  150. dev_warn(&pdev->dev, "DCDC frequency too high. Set to 1875kHz\n");
  151. }
  152. dcdcfreq = (dcdcfreq - 750) / 75;
  153. return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
  154. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  155. }
  156. static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  157. {
  158. struct device_node *np, *regulators;
  159. int ret;
  160. u32 dcdcfreq;
  161. np = of_node_get(pdev->dev.parent->of_node);
  162. if (!np)
  163. return 0;
  164. regulators = of_get_child_by_name(np, "regulators");
  165. if (!regulators) {
  166. dev_warn(&pdev->dev, "regulators node not found\n");
  167. } else {
  168. dcdcfreq = 1500;
  169. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  170. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  171. if (ret < 0) {
  172. dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  173. return ret;
  174. }
  175. of_node_put(regulators);
  176. }
  177. return 0;
  178. }
  179. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  180. {
  181. unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
  182. if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  183. return -EINVAL;
  184. if (id == AXP20X_DCDC3)
  185. mask = AXP20X_WORKMODE_DCDC3_MASK;
  186. workmode <<= ffs(mask) - 1;
  187. return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
  188. }
  189. static int axp20x_regulator_probe(struct platform_device *pdev)
  190. {
  191. struct regulator_dev *rdev;
  192. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  193. struct regulator_config config = {
  194. .dev = pdev->dev.parent,
  195. .regmap = axp20x->regmap,
  196. };
  197. int ret, i;
  198. u32 workmode;
  199. /* This only sets the dcdc freq. Ignore any errors */
  200. axp20x_regulator_parse_dt(pdev);
  201. for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
  202. rdev = devm_regulator_register(&pdev->dev, &axp20x_regulators[i],
  203. &config);
  204. if (IS_ERR(rdev)) {
  205. dev_err(&pdev->dev, "Failed to register %s\n",
  206. axp20x_regulators[i].name);
  207. return PTR_ERR(rdev);
  208. }
  209. ret = of_property_read_u32(rdev->dev.of_node,
  210. "x-powers,dcdc-workmode",
  211. &workmode);
  212. if (!ret) {
  213. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  214. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  215. axp20x_regulators[i].name);
  216. }
  217. }
  218. return 0;
  219. }
  220. static struct platform_driver axp20x_regulator_driver = {
  221. .probe = axp20x_regulator_probe,
  222. .driver = {
  223. .name = "axp20x-regulator",
  224. },
  225. };
  226. module_platform_driver(axp20x_regulator_driver);
  227. MODULE_LICENSE("GPL v2");
  228. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  229. MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");