axp20x-regulator.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 0x03
  28. #define AXP22X_IO_DISABLED 0x04
  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 AXP22X_MISC_N_VBUSEN_FUNC BIT(4)
  34. #define AXP_DESC_IO(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  35. _vmask, _ereg, _emask, _enable_val, _disable_val) \
  36. [_family##_##_id] = { \
  37. .name = (_match), \
  38. .supply_name = (_supply), \
  39. .of_match = of_match_ptr(_match), \
  40. .regulators_node = of_match_ptr("regulators"), \
  41. .type = REGULATOR_VOLTAGE, \
  42. .id = _family##_##_id, \
  43. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  44. .owner = THIS_MODULE, \
  45. .min_uV = (_min) * 1000, \
  46. .uV_step = (_step) * 1000, \
  47. .vsel_reg = (_vreg), \
  48. .vsel_mask = (_vmask), \
  49. .enable_reg = (_ereg), \
  50. .enable_mask = (_emask), \
  51. .enable_val = (_enable_val), \
  52. .disable_val = (_disable_val), \
  53. .ops = &axp20x_ops, \
  54. }
  55. #define AXP_DESC(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  56. _vmask, _ereg, _emask) \
  57. [_family##_##_id] = { \
  58. .name = (_match), \
  59. .supply_name = (_supply), \
  60. .of_match = of_match_ptr(_match), \
  61. .regulators_node = of_match_ptr("regulators"), \
  62. .type = REGULATOR_VOLTAGE, \
  63. .id = _family##_##_id, \
  64. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  65. .owner = THIS_MODULE, \
  66. .min_uV = (_min) * 1000, \
  67. .uV_step = (_step) * 1000, \
  68. .vsel_reg = (_vreg), \
  69. .vsel_mask = (_vmask), \
  70. .enable_reg = (_ereg), \
  71. .enable_mask = (_emask), \
  72. .ops = &axp20x_ops, \
  73. }
  74. #define AXP_DESC_SW(_family, _id, _match, _supply, _ereg, _emask) \
  75. [_family##_##_id] = { \
  76. .name = (_match), \
  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. .owner = THIS_MODULE, \
  83. .enable_reg = (_ereg), \
  84. .enable_mask = (_emask), \
  85. .ops = &axp20x_ops_sw, \
  86. }
  87. #define AXP_DESC_FIXED(_family, _id, _match, _supply, _volt) \
  88. [_family##_##_id] = { \
  89. .name = (_match), \
  90. .supply_name = (_supply), \
  91. .of_match = of_match_ptr(_match), \
  92. .regulators_node = of_match_ptr("regulators"), \
  93. .type = REGULATOR_VOLTAGE, \
  94. .id = _family##_##_id, \
  95. .n_voltages = 1, \
  96. .owner = THIS_MODULE, \
  97. .min_uV = (_volt) * 1000, \
  98. .ops = &axp20x_ops_fixed \
  99. }
  100. #define AXP_DESC_RANGES(_family, _id, _match, _supply, _ranges, _n_voltages, \
  101. _vreg, _vmask, _ereg, _emask) \
  102. [_family##_##_id] = { \
  103. .name = (_match), \
  104. .supply_name = (_supply), \
  105. .of_match = of_match_ptr(_match), \
  106. .regulators_node = of_match_ptr("regulators"), \
  107. .type = REGULATOR_VOLTAGE, \
  108. .id = _family##_##_id, \
  109. .n_voltages = (_n_voltages), \
  110. .owner = THIS_MODULE, \
  111. .vsel_reg = (_vreg), \
  112. .vsel_mask = (_vmask), \
  113. .enable_reg = (_ereg), \
  114. .enable_mask = (_emask), \
  115. .linear_ranges = (_ranges), \
  116. .n_linear_ranges = ARRAY_SIZE(_ranges), \
  117. .ops = &axp20x_ops_range, \
  118. }
  119. static struct regulator_ops axp20x_ops_fixed = {
  120. .list_voltage = regulator_list_voltage_linear,
  121. };
  122. static struct regulator_ops axp20x_ops_range = {
  123. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  124. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  125. .list_voltage = regulator_list_voltage_linear_range,
  126. .enable = regulator_enable_regmap,
  127. .disable = regulator_disable_regmap,
  128. .is_enabled = regulator_is_enabled_regmap,
  129. };
  130. static struct regulator_ops axp20x_ops = {
  131. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  132. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  133. .list_voltage = regulator_list_voltage_linear,
  134. .enable = regulator_enable_regmap,
  135. .disable = regulator_disable_regmap,
  136. .is_enabled = regulator_is_enabled_regmap,
  137. };
  138. static struct regulator_ops axp20x_ops_sw = {
  139. .enable = regulator_enable_regmap,
  140. .disable = regulator_disable_regmap,
  141. .is_enabled = regulator_is_enabled_regmap,
  142. };
  143. static const struct regulator_linear_range axp20x_ldo4_ranges[] = {
  144. REGULATOR_LINEAR_RANGE(1250000, 0x0, 0x0, 0),
  145. REGULATOR_LINEAR_RANGE(1300000, 0x1, 0x8, 100000),
  146. REGULATOR_LINEAR_RANGE(2500000, 0x9, 0x9, 0),
  147. REGULATOR_LINEAR_RANGE(2700000, 0xa, 0xb, 100000),
  148. REGULATOR_LINEAR_RANGE(3000000, 0xc, 0xf, 100000),
  149. };
  150. static const struct regulator_desc axp20x_regulators[] = {
  151. AXP_DESC(AXP20X, DCDC2, "dcdc2", "vin2", 700, 2275, 25,
  152. AXP20X_DCDC2_V_OUT, 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
  153. AXP_DESC(AXP20X, DCDC3, "dcdc3", "vin3", 700, 3500, 25,
  154. AXP20X_DCDC3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
  155. AXP_DESC_FIXED(AXP20X, LDO1, "ldo1", "acin", 1300),
  156. AXP_DESC(AXP20X, LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
  157. AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
  158. AXP_DESC(AXP20X, LDO3, "ldo3", "ldo3in", 700, 3500, 25,
  159. AXP20X_LDO3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
  160. AXP_DESC_RANGES(AXP20X, LDO4, "ldo4", "ldo24in", axp20x_ldo4_ranges,
  161. 16, AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL,
  162. 0x08),
  163. AXP_DESC_IO(AXP20X, LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
  164. AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
  165. AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
  166. };
  167. static const struct regulator_desc axp22x_regulators[] = {
  168. AXP_DESC(AXP22X, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
  169. AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
  170. AXP_DESC(AXP22X, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
  171. AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
  172. AXP_DESC(AXP22X, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
  173. AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
  174. AXP_DESC(AXP22X, DCDC4, "dcdc4", "vin4", 600, 1540, 20,
  175. AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(4)),
  176. AXP_DESC(AXP22X, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
  177. AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(5)),
  178. /* secondary switchable output of DCDC1 */
  179. AXP_DESC_SW(AXP22X, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
  180. BIT(7)),
  181. /* LDO regulator internally chained to DCDC5 */
  182. AXP_DESC(AXP22X, DC5LDO, "dc5ldo", NULL, 700, 1400, 100,
  183. AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
  184. AXP_DESC(AXP22X, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  185. AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
  186. AXP_DESC(AXP22X, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
  187. AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
  188. AXP_DESC(AXP22X, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  189. AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
  190. AXP_DESC(AXP22X, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
  191. AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
  192. AXP_DESC(AXP22X, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
  193. AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
  194. AXP_DESC(AXP22X, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
  195. AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
  196. AXP_DESC(AXP22X, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
  197. AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
  198. AXP_DESC(AXP22X, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
  199. AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
  200. AXP_DESC(AXP22X, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
  201. AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
  202. AXP_DESC(AXP22X, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
  203. AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
  204. /* Note the datasheet only guarantees reliable operation up to
  205. * 3.3V, this needs to be enforced via dts provided constraints */
  206. AXP_DESC_IO(AXP22X, LDO_IO0, "ldo_io0", "ips", 700, 3800, 100,
  207. AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
  208. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  209. /* Note the datasheet only guarantees reliable operation up to
  210. * 3.3V, this needs to be enforced via dts provided constraints */
  211. AXP_DESC_IO(AXP22X, LDO_IO1, "ldo_io1", "ips", 700, 3800, 100,
  212. AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
  213. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  214. AXP_DESC_FIXED(AXP22X, RTC_LDO, "rtc_ldo", "ips", 3000),
  215. };
  216. static const struct regulator_desc axp22x_drivevbus_regulator = {
  217. .name = "drivevbus",
  218. .supply_name = "drivevbus",
  219. .of_match = of_match_ptr("drivevbus"),
  220. .regulators_node = of_match_ptr("regulators"),
  221. .type = REGULATOR_VOLTAGE,
  222. .owner = THIS_MODULE,
  223. .enable_reg = AXP20X_VBUS_IPSOUT_MGMT,
  224. .enable_mask = BIT(2),
  225. .ops = &axp20x_ops_sw,
  226. };
  227. static const struct regulator_linear_range axp806_dcdca_ranges[] = {
  228. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x32, 10000),
  229. REGULATOR_LINEAR_RANGE(1120000, 0x33, 0x47, 20000),
  230. };
  231. static const struct regulator_linear_range axp806_dcdcd_ranges[] = {
  232. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2d, 20000),
  233. REGULATOR_LINEAR_RANGE(1600000, 0x2e, 0x3f, 100000),
  234. };
  235. static const struct regulator_linear_range axp806_cldo2_ranges[] = {
  236. REGULATOR_LINEAR_RANGE(700000, 0x0, 0x1a, 100000),
  237. REGULATOR_LINEAR_RANGE(3400000, 0x1b, 0x1f, 200000),
  238. };
  239. static const struct regulator_desc axp806_regulators[] = {
  240. AXP_DESC_RANGES(AXP806, DCDCA, "dcdca", "vina", axp806_dcdca_ranges,
  241. 72, AXP806_DCDCA_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
  242. BIT(0)),
  243. AXP_DESC(AXP806, DCDCB, "dcdcb", "vinb", 1000, 2550, 50,
  244. AXP806_DCDCB_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(1)),
  245. AXP_DESC_RANGES(AXP806, DCDCC, "dcdcc", "vinc", axp806_dcdca_ranges,
  246. 72, AXP806_DCDCC_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
  247. BIT(2)),
  248. AXP_DESC_RANGES(AXP806, DCDCD, "dcdcd", "vind", axp806_dcdcd_ranges,
  249. 64, AXP806_DCDCD_V_CTRL, 0x3f, AXP806_PWR_OUT_CTRL1,
  250. BIT(3)),
  251. AXP_DESC(AXP806, DCDCE, "dcdce", "vine", 1100, 3400, 100,
  252. AXP806_DCDCE_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(4)),
  253. AXP_DESC(AXP806, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  254. AXP806_ALDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(5)),
  255. AXP_DESC(AXP806, ALDO2, "aldo2", "aldoin", 700, 3400, 100,
  256. AXP806_ALDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(6)),
  257. AXP_DESC(AXP806, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  258. AXP806_ALDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(7)),
  259. AXP_DESC(AXP806, BLDO1, "bldo1", "bldoin", 700, 1900, 100,
  260. AXP806_BLDO1_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(0)),
  261. AXP_DESC(AXP806, BLDO2, "bldo2", "bldoin", 700, 1900, 100,
  262. AXP806_BLDO2_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(1)),
  263. AXP_DESC(AXP806, BLDO3, "bldo3", "bldoin", 700, 1900, 100,
  264. AXP806_BLDO3_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(2)),
  265. AXP_DESC(AXP806, BLDO4, "bldo4", "bldoin", 700, 1900, 100,
  266. AXP806_BLDO4_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(3)),
  267. AXP_DESC(AXP806, CLDO1, "cldo1", "cldoin", 700, 3300, 100,
  268. AXP806_CLDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(4)),
  269. AXP_DESC_RANGES(AXP806, CLDO2, "cldo2", "cldoin", axp806_cldo2_ranges,
  270. 32, AXP806_CLDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2,
  271. BIT(5)),
  272. AXP_DESC(AXP806, CLDO3, "cldo3", "cldoin", 700, 3300, 100,
  273. AXP806_CLDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(6)),
  274. AXP_DESC_SW(AXP806, SW, "sw", "swin", AXP806_PWR_OUT_CTRL2, BIT(7)),
  275. };
  276. static const struct regulator_linear_range axp809_dcdc4_ranges[] = {
  277. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2f, 20000),
  278. REGULATOR_LINEAR_RANGE(1800000, 0x30, 0x38, 100000),
  279. };
  280. static const struct regulator_desc axp809_regulators[] = {
  281. AXP_DESC(AXP809, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
  282. AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
  283. AXP_DESC(AXP809, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
  284. AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
  285. AXP_DESC(AXP809, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
  286. AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
  287. AXP_DESC_RANGES(AXP809, DCDC4, "dcdc4", "vin4", axp809_dcdc4_ranges,
  288. 57, AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1,
  289. BIT(4)),
  290. AXP_DESC(AXP809, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
  291. AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(5)),
  292. /* secondary switchable output of DCDC1 */
  293. AXP_DESC_SW(AXP809, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
  294. BIT(7)),
  295. /* LDO regulator internally chained to DCDC5 */
  296. AXP_DESC(AXP809, DC5LDO, "dc5ldo", NULL, 700, 1400, 100,
  297. AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
  298. AXP_DESC(AXP809, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  299. AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
  300. AXP_DESC(AXP809, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
  301. AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
  302. AXP_DESC(AXP809, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  303. AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
  304. AXP_DESC_RANGES(AXP809, DLDO1, "dldo1", "dldoin", axp806_cldo2_ranges,
  305. 32, AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
  306. BIT(3)),
  307. AXP_DESC(AXP809, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
  308. AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
  309. AXP_DESC(AXP809, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
  310. AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
  311. AXP_DESC(AXP809, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
  312. AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
  313. AXP_DESC(AXP809, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
  314. AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
  315. /*
  316. * Note the datasheet only guarantees reliable operation up to
  317. * 3.3V, this needs to be enforced via dts provided constraints
  318. */
  319. AXP_DESC_IO(AXP809, LDO_IO0, "ldo_io0", "ips", 700, 3800, 100,
  320. AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
  321. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  322. /*
  323. * Note the datasheet only guarantees reliable operation up to
  324. * 3.3V, this needs to be enforced via dts provided constraints
  325. */
  326. AXP_DESC_IO(AXP809, LDO_IO1, "ldo_io1", "ips", 700, 3800, 100,
  327. AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
  328. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  329. AXP_DESC_FIXED(AXP809, RTC_LDO, "rtc_ldo", "ips", 1800),
  330. AXP_DESC_SW(AXP809, SW, "sw", "swin", AXP22X_PWR_OUT_CTRL2, BIT(6)),
  331. };
  332. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  333. {
  334. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  335. unsigned int reg = AXP20X_DCDC_FREQ;
  336. u32 min, max, def, step;
  337. switch (axp20x->variant) {
  338. case AXP202_ID:
  339. case AXP209_ID:
  340. min = 750;
  341. max = 1875;
  342. def = 1500;
  343. step = 75;
  344. break;
  345. case AXP806_ID:
  346. /*
  347. * AXP806 DCDC work frequency setting has the same range and
  348. * step as AXP22X, but at a different register.
  349. * Fall through to the check below.
  350. * (See include/linux/mfd/axp20x.h)
  351. */
  352. reg = AXP806_DCDC_FREQ_CTRL;
  353. case AXP221_ID:
  354. case AXP223_ID:
  355. case AXP809_ID:
  356. min = 1800;
  357. max = 4050;
  358. def = 3000;
  359. step = 150;
  360. break;
  361. default:
  362. dev_err(&pdev->dev,
  363. "Setting DCDC frequency for unsupported AXP variant\n");
  364. return -EINVAL;
  365. }
  366. if (dcdcfreq == 0)
  367. dcdcfreq = def;
  368. if (dcdcfreq < min) {
  369. dcdcfreq = min;
  370. dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
  371. min);
  372. }
  373. if (dcdcfreq > max) {
  374. dcdcfreq = max;
  375. dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
  376. max);
  377. }
  378. dcdcfreq = (dcdcfreq - min) / step;
  379. return regmap_update_bits(axp20x->regmap, reg,
  380. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  381. }
  382. static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  383. {
  384. struct device_node *np, *regulators;
  385. int ret;
  386. u32 dcdcfreq = 0;
  387. np = of_node_get(pdev->dev.parent->of_node);
  388. if (!np)
  389. return 0;
  390. regulators = of_get_child_by_name(np, "regulators");
  391. if (!regulators) {
  392. dev_warn(&pdev->dev, "regulators node not found\n");
  393. } else {
  394. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  395. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  396. if (ret < 0) {
  397. dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  398. return ret;
  399. }
  400. of_node_put(regulators);
  401. }
  402. return 0;
  403. }
  404. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  405. {
  406. struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
  407. unsigned int reg = AXP20X_DCDC_MODE;
  408. unsigned int mask;
  409. switch (axp20x->variant) {
  410. case AXP202_ID:
  411. case AXP209_ID:
  412. if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  413. return -EINVAL;
  414. mask = AXP20X_WORKMODE_DCDC2_MASK;
  415. if (id == AXP20X_DCDC3)
  416. mask = AXP20X_WORKMODE_DCDC3_MASK;
  417. workmode <<= ffs(mask) - 1;
  418. break;
  419. case AXP806_ID:
  420. reg = AXP806_DCDC_MODE_CTRL2;
  421. /*
  422. * AXP806 DCDC regulator IDs have the same range as AXP22X.
  423. * Fall through to the check below.
  424. * (See include/linux/mfd/axp20x.h)
  425. */
  426. case AXP221_ID:
  427. case AXP223_ID:
  428. case AXP809_ID:
  429. if (id < AXP22X_DCDC1 || id > AXP22X_DCDC5)
  430. return -EINVAL;
  431. mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP22X_DCDC1);
  432. workmode <<= id - AXP22X_DCDC1;
  433. break;
  434. default:
  435. /* should not happen */
  436. WARN_ON(1);
  437. return -EINVAL;
  438. }
  439. return regmap_update_bits(rdev->regmap, reg, mask, workmode);
  440. }
  441. /*
  442. * This function checks whether a regulator is part of a poly-phase
  443. * output setup based on the registers settings. Returns true if it is.
  444. */
  445. static bool axp20x_is_polyphase_slave(struct axp20x_dev *axp20x, int id)
  446. {
  447. u32 reg = 0;
  448. /* Only AXP806 has poly-phase outputs */
  449. if (axp20x->variant != AXP806_ID)
  450. return false;
  451. regmap_read(axp20x->regmap, AXP806_DCDC_MODE_CTRL2, &reg);
  452. switch (id) {
  453. case AXP806_DCDCB:
  454. return (((reg & GENMASK(7, 6)) == BIT(6)) ||
  455. ((reg & GENMASK(7, 6)) == BIT(7)));
  456. case AXP806_DCDCC:
  457. return ((reg & GENMASK(7, 6)) == BIT(7));
  458. case AXP806_DCDCE:
  459. return !!(reg & BIT(5));
  460. }
  461. return false;
  462. }
  463. static int axp20x_regulator_probe(struct platform_device *pdev)
  464. {
  465. struct regulator_dev *rdev;
  466. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  467. const struct regulator_desc *regulators;
  468. struct regulator_config config = {
  469. .dev = pdev->dev.parent,
  470. .regmap = axp20x->regmap,
  471. .driver_data = axp20x,
  472. };
  473. int ret, i, nregulators;
  474. u32 workmode;
  475. const char *dcdc1_name = axp22x_regulators[AXP22X_DCDC1].name;
  476. const char *dcdc5_name = axp22x_regulators[AXP22X_DCDC5].name;
  477. bool drivevbus = false;
  478. switch (axp20x->variant) {
  479. case AXP202_ID:
  480. case AXP209_ID:
  481. regulators = axp20x_regulators;
  482. nregulators = AXP20X_REG_ID_MAX;
  483. break;
  484. case AXP221_ID:
  485. case AXP223_ID:
  486. regulators = axp22x_regulators;
  487. nregulators = AXP22X_REG_ID_MAX;
  488. drivevbus = of_property_read_bool(pdev->dev.parent->of_node,
  489. "x-powers,drive-vbus-en");
  490. break;
  491. case AXP806_ID:
  492. regulators = axp806_regulators;
  493. nregulators = AXP806_REG_ID_MAX;
  494. break;
  495. case AXP809_ID:
  496. regulators = axp809_regulators;
  497. nregulators = AXP809_REG_ID_MAX;
  498. break;
  499. default:
  500. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  501. axp20x->variant);
  502. return -EINVAL;
  503. }
  504. /* This only sets the dcdc freq. Ignore any errors */
  505. axp20x_regulator_parse_dt(pdev);
  506. for (i = 0; i < nregulators; i++) {
  507. const struct regulator_desc *desc = &regulators[i];
  508. struct regulator_desc *new_desc;
  509. /*
  510. * If this regulator is a slave in a poly-phase setup,
  511. * skip it, as its controls are bound to the master
  512. * regulator and won't work.
  513. */
  514. if (axp20x_is_polyphase_slave(axp20x, i))
  515. continue;
  516. /*
  517. * Regulators DC1SW and DC5LDO are connected internally,
  518. * so we have to handle their supply names separately.
  519. *
  520. * We always register the regulators in proper sequence,
  521. * so the supply names are correctly read. See the last
  522. * part of this loop to see where we save the DT defined
  523. * name.
  524. */
  525. if ((regulators == axp22x_regulators && i == AXP22X_DC1SW) ||
  526. (regulators == axp809_regulators && i == AXP809_DC1SW)) {
  527. new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
  528. GFP_KERNEL);
  529. *new_desc = regulators[i];
  530. new_desc->supply_name = dcdc1_name;
  531. desc = new_desc;
  532. }
  533. if ((regulators == axp22x_regulators && i == AXP22X_DC5LDO) ||
  534. (regulators == axp809_regulators && i == AXP809_DC5LDO)) {
  535. new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
  536. GFP_KERNEL);
  537. *new_desc = regulators[i];
  538. new_desc->supply_name = dcdc5_name;
  539. desc = new_desc;
  540. }
  541. rdev = devm_regulator_register(&pdev->dev, desc, &config);
  542. if (IS_ERR(rdev)) {
  543. dev_err(&pdev->dev, "Failed to register %s\n",
  544. regulators[i].name);
  545. return PTR_ERR(rdev);
  546. }
  547. ret = of_property_read_u32(rdev->dev.of_node,
  548. "x-powers,dcdc-workmode",
  549. &workmode);
  550. if (!ret) {
  551. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  552. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  553. rdev->desc->name);
  554. }
  555. /*
  556. * Save AXP22X DCDC1 / DCDC5 regulator names for later.
  557. */
  558. if ((regulators == axp22x_regulators && i == AXP22X_DCDC1) ||
  559. (regulators == axp809_regulators && i == AXP809_DCDC1))
  560. of_property_read_string(rdev->dev.of_node,
  561. "regulator-name",
  562. &dcdc1_name);
  563. if ((regulators == axp22x_regulators && i == AXP22X_DCDC5) ||
  564. (regulators == axp809_regulators && i == AXP809_DCDC5))
  565. of_property_read_string(rdev->dev.of_node,
  566. "regulator-name",
  567. &dcdc5_name);
  568. }
  569. if (drivevbus) {
  570. /* Change N_VBUSEN sense pin to DRIVEVBUS output pin */
  571. regmap_update_bits(axp20x->regmap, AXP20X_OVER_TMP,
  572. AXP22X_MISC_N_VBUSEN_FUNC, 0);
  573. rdev = devm_regulator_register(&pdev->dev,
  574. &axp22x_drivevbus_regulator,
  575. &config);
  576. if (IS_ERR(rdev)) {
  577. dev_err(&pdev->dev, "Failed to register drivevbus\n");
  578. return PTR_ERR(rdev);
  579. }
  580. }
  581. return 0;
  582. }
  583. static struct platform_driver axp20x_regulator_driver = {
  584. .probe = axp20x_regulator_probe,
  585. .driver = {
  586. .name = "axp20x-regulator",
  587. },
  588. };
  589. module_platform_driver(axp20x_regulator_driver);
  590. MODULE_LICENSE("GPL v2");
  591. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  592. MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");
  593. MODULE_ALIAS("platform:axp20x-regulator");