axp20x-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 AXP22X_IO_ENABLED 0x04
  28. #define AXP22X_IO_DISABLED 0x03
  29. #define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
  30. #define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
  31. #define AXP22X_WORKMODE_DCDCX_MASK(x) BIT(x)
  32. #define AXP20X_FREQ_DCDC_MASK 0x0f
  33. #define AXP_DESC_IO(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  34. _vmask, _ereg, _emask, _enable_val, _disable_val) \
  35. [_family##_##_id] = { \
  36. .name = #_id, \
  37. .supply_name = (_supply), \
  38. .of_match = of_match_ptr(_match), \
  39. .regulators_node = of_match_ptr("regulators"), \
  40. .type = REGULATOR_VOLTAGE, \
  41. .id = _family##_##_id, \
  42. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  43. .owner = THIS_MODULE, \
  44. .min_uV = (_min) * 1000, \
  45. .uV_step = (_step) * 1000, \
  46. .vsel_reg = (_vreg), \
  47. .vsel_mask = (_vmask), \
  48. .enable_reg = (_ereg), \
  49. .enable_mask = (_emask), \
  50. .enable_val = (_enable_val), \
  51. .disable_val = (_disable_val), \
  52. .ops = &axp20x_ops, \
  53. }
  54. #define AXP_DESC(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  55. _vmask, _ereg, _emask) \
  56. [_family##_##_id] = { \
  57. .name = #_id, \
  58. .supply_name = (_supply), \
  59. .of_match = of_match_ptr(_match), \
  60. .regulators_node = of_match_ptr("regulators"), \
  61. .type = REGULATOR_VOLTAGE, \
  62. .id = _family##_##_id, \
  63. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  64. .owner = THIS_MODULE, \
  65. .min_uV = (_min) * 1000, \
  66. .uV_step = (_step) * 1000, \
  67. .vsel_reg = (_vreg), \
  68. .vsel_mask = (_vmask), \
  69. .enable_reg = (_ereg), \
  70. .enable_mask = (_emask), \
  71. .ops = &axp20x_ops, \
  72. }
  73. #define AXP_DESC_SW(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  74. _vmask, _ereg, _emask) \
  75. [_family##_##_id] = { \
  76. .name = #_id, \
  77. .supply_name = (_supply), \
  78. .of_match = of_match_ptr(_match), \
  79. .regulators_node = of_match_ptr("regulators"), \
  80. .type = REGULATOR_VOLTAGE, \
  81. .id = _family##_##_id, \
  82. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  83. .owner = THIS_MODULE, \
  84. .min_uV = (_min) * 1000, \
  85. .uV_step = (_step) * 1000, \
  86. .vsel_reg = (_vreg), \
  87. .vsel_mask = (_vmask), \
  88. .enable_reg = (_ereg), \
  89. .enable_mask = (_emask), \
  90. .ops = &axp20x_ops_sw, \
  91. }
  92. #define AXP_DESC_FIXED(_family, _id, _match, _supply, _volt) \
  93. [_family##_##_id] = { \
  94. .name = #_id, \
  95. .supply_name = (_supply), \
  96. .of_match = of_match_ptr(_match), \
  97. .regulators_node = of_match_ptr("regulators"), \
  98. .type = REGULATOR_VOLTAGE, \
  99. .id = _family##_##_id, \
  100. .n_voltages = 1, \
  101. .owner = THIS_MODULE, \
  102. .min_uV = (_volt) * 1000, \
  103. .ops = &axp20x_ops_fixed \
  104. }
  105. #define AXP_DESC_TABLE(_family, _id, _match, _supply, _table, _vreg, _vmask, \
  106. _ereg, _emask) \
  107. [_family##_##_id] = { \
  108. .name = #_id, \
  109. .supply_name = (_supply), \
  110. .of_match = of_match_ptr(_match), \
  111. .regulators_node = of_match_ptr("regulators"), \
  112. .type = REGULATOR_VOLTAGE, \
  113. .id = _family##_##_id, \
  114. .n_voltages = ARRAY_SIZE(_table), \
  115. .owner = THIS_MODULE, \
  116. .vsel_reg = (_vreg), \
  117. .vsel_mask = (_vmask), \
  118. .enable_reg = (_ereg), \
  119. .enable_mask = (_emask), \
  120. .volt_table = (_table), \
  121. .ops = &axp20x_ops_table, \
  122. }
  123. static const int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000,
  124. 1700000, 1800000, 1900000, 2000000, 2500000,
  125. 2700000, 2800000, 3000000, 3100000, 3200000,
  126. 3300000 };
  127. static struct regulator_ops axp20x_ops_fixed = {
  128. .list_voltage = regulator_list_voltage_linear,
  129. };
  130. static struct regulator_ops axp20x_ops_table = {
  131. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  132. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  133. .list_voltage = regulator_list_voltage_table,
  134. .map_voltage = regulator_map_voltage_ascend,
  135. .enable = regulator_enable_regmap,
  136. .disable = regulator_disable_regmap,
  137. .is_enabled = regulator_is_enabled_regmap,
  138. };
  139. static struct regulator_ops axp20x_ops = {
  140. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  141. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  142. .list_voltage = regulator_list_voltage_linear,
  143. .enable = regulator_enable_regmap,
  144. .disable = regulator_disable_regmap,
  145. .is_enabled = regulator_is_enabled_regmap,
  146. };
  147. static struct regulator_ops axp20x_ops_sw = {
  148. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  149. .list_voltage = regulator_list_voltage_linear,
  150. .enable = regulator_enable_regmap,
  151. .disable = regulator_disable_regmap,
  152. .is_enabled = regulator_is_enabled_regmap,
  153. };
  154. static const struct regulator_desc axp20x_regulators[] = {
  155. AXP_DESC(AXP20X, DCDC2, "dcdc2", "vin2", 700, 2275, 25,
  156. AXP20X_DCDC2_V_OUT, 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
  157. AXP_DESC(AXP20X, DCDC3, "dcdc3", "vin3", 700, 3500, 25,
  158. AXP20X_DCDC3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
  159. AXP_DESC_FIXED(AXP20X, LDO1, "ldo1", "acin", 1300),
  160. AXP_DESC(AXP20X, LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
  161. AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
  162. AXP_DESC(AXP20X, LDO3, "ldo3", "ldo3in", 700, 3500, 25,
  163. AXP20X_LDO3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
  164. AXP_DESC_TABLE(AXP20X, LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
  165. AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
  166. AXP_DESC_IO(AXP20X, LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
  167. AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
  168. AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
  169. };
  170. static const struct regulator_desc axp22x_regulators[] = {
  171. AXP_DESC(AXP22X, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
  172. AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
  173. AXP_DESC(AXP22X, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
  174. AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
  175. AXP_DESC(AXP22X, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
  176. AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
  177. AXP_DESC(AXP22X, DCDC4, "dcdc4", "vin4", 600, 1540, 20,
  178. AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
  179. AXP_DESC(AXP22X, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
  180. AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(4)),
  181. /* secondary switchable output of DCDC1 */
  182. AXP_DESC_SW(AXP22X, DC1SW, "dc1sw", "dcdc1", 1600, 3400, 100,
  183. AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(7)),
  184. /* LDO regulator internally chained to DCDC5 */
  185. AXP_DESC(AXP22X, DC5LDO, "dc5ldo", "dcdc5", 700, 1400, 100,
  186. AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
  187. AXP_DESC(AXP22X, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  188. AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
  189. AXP_DESC(AXP22X, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
  190. AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
  191. AXP_DESC(AXP22X, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  192. AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
  193. AXP_DESC(AXP22X, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
  194. AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
  195. AXP_DESC(AXP22X, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
  196. AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
  197. AXP_DESC(AXP22X, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
  198. AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
  199. AXP_DESC(AXP22X, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
  200. AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
  201. AXP_DESC(AXP22X, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
  202. AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
  203. AXP_DESC(AXP22X, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
  204. AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
  205. AXP_DESC(AXP22X, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
  206. AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
  207. AXP_DESC_IO(AXP22X, LDO_IO0, "ldo_io0", "ips", 1800, 3300, 100,
  208. AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
  209. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  210. AXP_DESC_IO(AXP22X, LDO_IO1, "ldo_io1", "ips", 1800, 3300, 100,
  211. AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
  212. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  213. AXP_DESC_FIXED(AXP22X, RTC_LDO, "rtc_ldo", "ips", 3000),
  214. };
  215. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  216. {
  217. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  218. u32 min, max, def, step;
  219. switch (axp20x->variant) {
  220. case AXP202_ID:
  221. case AXP209_ID:
  222. min = 750;
  223. max = 1875;
  224. def = 1500;
  225. step = 75;
  226. break;
  227. case AXP221_ID:
  228. min = 1800;
  229. max = 4050;
  230. def = 3000;
  231. step = 150;
  232. break;
  233. default:
  234. dev_err(&pdev->dev,
  235. "Setting DCDC frequency for unsupported AXP variant\n");
  236. return -EINVAL;
  237. }
  238. if (dcdcfreq == 0)
  239. dcdcfreq = def;
  240. if (dcdcfreq < min) {
  241. dcdcfreq = min;
  242. dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
  243. min);
  244. }
  245. if (dcdcfreq > max) {
  246. dcdcfreq = max;
  247. dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
  248. max);
  249. }
  250. dcdcfreq = (dcdcfreq - min) / step;
  251. return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
  252. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  253. }
  254. static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  255. {
  256. struct device_node *np, *regulators;
  257. int ret;
  258. u32 dcdcfreq = 0;
  259. np = of_node_get(pdev->dev.parent->of_node);
  260. if (!np)
  261. return 0;
  262. regulators = of_get_child_by_name(np, "regulators");
  263. if (!regulators) {
  264. dev_warn(&pdev->dev, "regulators node not found\n");
  265. } else {
  266. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  267. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  268. if (ret < 0) {
  269. dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  270. return ret;
  271. }
  272. of_node_put(regulators);
  273. }
  274. return 0;
  275. }
  276. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  277. {
  278. struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
  279. unsigned int mask;
  280. switch (axp20x->variant) {
  281. case AXP202_ID:
  282. case AXP209_ID:
  283. if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  284. return -EINVAL;
  285. mask = AXP20X_WORKMODE_DCDC2_MASK;
  286. if (id == AXP20X_DCDC3)
  287. mask = AXP20X_WORKMODE_DCDC3_MASK;
  288. workmode <<= ffs(mask) - 1;
  289. break;
  290. case AXP221_ID:
  291. if (id < AXP22X_DCDC1 || id > AXP22X_DCDC5)
  292. return -EINVAL;
  293. mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP22X_DCDC1);
  294. workmode <<= id - AXP22X_DCDC1;
  295. break;
  296. default:
  297. /* should not happen */
  298. WARN_ON(1);
  299. return -EINVAL;
  300. }
  301. return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
  302. }
  303. static int axp20x_regulator_probe(struct platform_device *pdev)
  304. {
  305. struct regulator_dev *rdev;
  306. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  307. const struct regulator_desc *regulators;
  308. struct regulator_config config = {
  309. .dev = pdev->dev.parent,
  310. .regmap = axp20x->regmap,
  311. .driver_data = axp20x,
  312. };
  313. int ret, i, nregulators;
  314. u32 workmode;
  315. switch (axp20x->variant) {
  316. case AXP202_ID:
  317. case AXP209_ID:
  318. regulators = axp20x_regulators;
  319. nregulators = AXP20X_REG_ID_MAX;
  320. break;
  321. case AXP221_ID:
  322. regulators = axp22x_regulators;
  323. nregulators = AXP22X_REG_ID_MAX;
  324. break;
  325. default:
  326. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  327. axp20x->variant);
  328. return -EINVAL;
  329. }
  330. /* This only sets the dcdc freq. Ignore any errors */
  331. axp20x_regulator_parse_dt(pdev);
  332. for (i = 0; i < nregulators; i++) {
  333. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  334. &config);
  335. if (IS_ERR(rdev)) {
  336. dev_err(&pdev->dev, "Failed to register %s\n",
  337. regulators[i].name);
  338. return PTR_ERR(rdev);
  339. }
  340. ret = of_property_read_u32(rdev->dev.of_node,
  341. "x-powers,dcdc-workmode",
  342. &workmode);
  343. if (!ret) {
  344. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  345. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  346. rdev->desc->name);
  347. }
  348. }
  349. return 0;
  350. }
  351. static struct platform_driver axp20x_regulator_driver = {
  352. .probe = axp20x_regulator_probe,
  353. .driver = {
  354. .name = "axp20x-regulator",
  355. },
  356. };
  357. module_platform_driver(axp20x_regulator_driver);
  358. MODULE_LICENSE("GPL v2");
  359. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  360. MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");