axp20x-regulator.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 const struct regulator_ops axp20x_ops_fixed = {
  120. .list_voltage = regulator_list_voltage_linear,
  121. };
  122. static const 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 const 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 const 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 axp803_dcdc234_ranges[] = {
  228. REGULATOR_LINEAR_RANGE(500000, 0x0, 0x46, 10000),
  229. REGULATOR_LINEAR_RANGE(1220000, 0x47, 0x4b, 20000),
  230. };
  231. static const struct regulator_linear_range axp803_dcdc5_ranges[] = {
  232. REGULATOR_LINEAR_RANGE(800000, 0x0, 0x20, 10000),
  233. REGULATOR_LINEAR_RANGE(1140000, 0x21, 0x44, 20000),
  234. };
  235. static const struct regulator_linear_range axp803_dcdc6_ranges[] = {
  236. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x32, 10000),
  237. REGULATOR_LINEAR_RANGE(1120000, 0x33, 0x47, 20000),
  238. };
  239. /* AXP806's CLDO2 and AXP809's DLDO1 shares the same range */
  240. static const struct regulator_linear_range axp803_dldo2_ranges[] = {
  241. REGULATOR_LINEAR_RANGE(700000, 0x0, 0x1a, 100000),
  242. REGULATOR_LINEAR_RANGE(3400000, 0x1b, 0x1f, 200000),
  243. };
  244. static const struct regulator_desc axp803_regulators[] = {
  245. AXP_DESC(AXP803, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
  246. AXP803_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(0)),
  247. AXP_DESC_RANGES(AXP803, DCDC2, "dcdc2", "vin2", axp803_dcdc234_ranges,
  248. 76, AXP803_DCDC2_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
  249. BIT(1)),
  250. AXP_DESC_RANGES(AXP803, DCDC3, "dcdc3", "vin3", axp803_dcdc234_ranges,
  251. 76, AXP803_DCDC3_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
  252. BIT(2)),
  253. AXP_DESC_RANGES(AXP803, DCDC4, "dcdc4", "vin4", axp803_dcdc234_ranges,
  254. 76, AXP803_DCDC4_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
  255. BIT(3)),
  256. AXP_DESC_RANGES(AXP803, DCDC5, "dcdc5", "vin5", axp803_dcdc5_ranges,
  257. 68, AXP803_DCDC5_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
  258. BIT(4)),
  259. AXP_DESC_RANGES(AXP803, DCDC6, "dcdc6", "vin6", axp803_dcdc6_ranges,
  260. 72, AXP803_DCDC6_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
  261. BIT(5)),
  262. /* secondary switchable output of DCDC1 */
  263. AXP_DESC_SW(AXP803, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
  264. BIT(7)),
  265. AXP_DESC(AXP803, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  266. AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(5)),
  267. AXP_DESC(AXP803, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
  268. AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(6)),
  269. AXP_DESC(AXP803, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  270. AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
  271. AXP_DESC(AXP803, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
  272. AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
  273. AXP_DESC_RANGES(AXP803, DLDO2, "dldo2", "dldoin", axp803_dldo2_ranges,
  274. 32, AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
  275. BIT(4)),
  276. AXP_DESC(AXP803, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
  277. AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
  278. AXP_DESC(AXP803, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
  279. AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
  280. AXP_DESC(AXP803, ELDO1, "eldo1", "eldoin", 700, 1900, 50,
  281. AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
  282. AXP_DESC(AXP803, ELDO2, "eldo2", "eldoin", 700, 1900, 50,
  283. AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
  284. AXP_DESC(AXP803, ELDO3, "eldo3", "eldoin", 700, 1900, 50,
  285. AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
  286. AXP_DESC(AXP803, FLDO1, "fldo1", "fldoin", 700, 1450, 50,
  287. AXP803_FLDO1_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(2)),
  288. AXP_DESC(AXP803, FLDO2, "fldo2", "fldoin", 700, 1450, 50,
  289. AXP803_FLDO2_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(3)),
  290. AXP_DESC_IO(AXP803, LDO_IO0, "ldo-io0", "ips", 700, 3300, 100,
  291. AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
  292. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  293. AXP_DESC_IO(AXP803, LDO_IO1, "ldo-io1", "ips", 700, 3300, 100,
  294. AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
  295. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  296. AXP_DESC_FIXED(AXP803, RTC_LDO, "rtc-ldo", "ips", 3000),
  297. };
  298. static const struct regulator_linear_range axp806_dcdca_ranges[] = {
  299. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x32, 10000),
  300. REGULATOR_LINEAR_RANGE(1120000, 0x33, 0x47, 20000),
  301. };
  302. static const struct regulator_linear_range axp806_dcdcd_ranges[] = {
  303. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2d, 20000),
  304. REGULATOR_LINEAR_RANGE(1600000, 0x2e, 0x3f, 100000),
  305. };
  306. static const struct regulator_desc axp806_regulators[] = {
  307. AXP_DESC_RANGES(AXP806, DCDCA, "dcdca", "vina", axp806_dcdca_ranges,
  308. 72, AXP806_DCDCA_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
  309. BIT(0)),
  310. AXP_DESC(AXP806, DCDCB, "dcdcb", "vinb", 1000, 2550, 50,
  311. AXP806_DCDCB_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(1)),
  312. AXP_DESC_RANGES(AXP806, DCDCC, "dcdcc", "vinc", axp806_dcdca_ranges,
  313. 72, AXP806_DCDCC_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
  314. BIT(2)),
  315. AXP_DESC_RANGES(AXP806, DCDCD, "dcdcd", "vind", axp806_dcdcd_ranges,
  316. 64, AXP806_DCDCD_V_CTRL, 0x3f, AXP806_PWR_OUT_CTRL1,
  317. BIT(3)),
  318. AXP_DESC(AXP806, DCDCE, "dcdce", "vine", 1100, 3400, 100,
  319. AXP806_DCDCE_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(4)),
  320. AXP_DESC(AXP806, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  321. AXP806_ALDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(5)),
  322. AXP_DESC(AXP806, ALDO2, "aldo2", "aldoin", 700, 3400, 100,
  323. AXP806_ALDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(6)),
  324. AXP_DESC(AXP806, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  325. AXP806_ALDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(7)),
  326. AXP_DESC(AXP806, BLDO1, "bldo1", "bldoin", 700, 1900, 100,
  327. AXP806_BLDO1_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(0)),
  328. AXP_DESC(AXP806, BLDO2, "bldo2", "bldoin", 700, 1900, 100,
  329. AXP806_BLDO2_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(1)),
  330. AXP_DESC(AXP806, BLDO3, "bldo3", "bldoin", 700, 1900, 100,
  331. AXP806_BLDO3_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(2)),
  332. AXP_DESC(AXP806, BLDO4, "bldo4", "bldoin", 700, 1900, 100,
  333. AXP806_BLDO4_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(3)),
  334. AXP_DESC(AXP806, CLDO1, "cldo1", "cldoin", 700, 3300, 100,
  335. AXP806_CLDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(4)),
  336. AXP_DESC_RANGES(AXP806, CLDO2, "cldo2", "cldoin", axp803_dldo2_ranges,
  337. 32, AXP806_CLDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2,
  338. BIT(5)),
  339. AXP_DESC(AXP806, CLDO3, "cldo3", "cldoin", 700, 3300, 100,
  340. AXP806_CLDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(6)),
  341. AXP_DESC_SW(AXP806, SW, "sw", "swin", AXP806_PWR_OUT_CTRL2, BIT(7)),
  342. };
  343. static const struct regulator_linear_range axp809_dcdc4_ranges[] = {
  344. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2f, 20000),
  345. REGULATOR_LINEAR_RANGE(1800000, 0x30, 0x38, 100000),
  346. };
  347. static const struct regulator_desc axp809_regulators[] = {
  348. AXP_DESC(AXP809, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
  349. AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
  350. AXP_DESC(AXP809, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
  351. AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
  352. AXP_DESC(AXP809, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
  353. AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
  354. AXP_DESC_RANGES(AXP809, DCDC4, "dcdc4", "vin4", axp809_dcdc4_ranges,
  355. 57, AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1,
  356. BIT(4)),
  357. AXP_DESC(AXP809, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
  358. AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(5)),
  359. /* secondary switchable output of DCDC1 */
  360. AXP_DESC_SW(AXP809, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
  361. BIT(7)),
  362. /* LDO regulator internally chained to DCDC5 */
  363. AXP_DESC(AXP809, DC5LDO, "dc5ldo", NULL, 700, 1400, 100,
  364. AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
  365. AXP_DESC(AXP809, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
  366. AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
  367. AXP_DESC(AXP809, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
  368. AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
  369. AXP_DESC(AXP809, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
  370. AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
  371. AXP_DESC_RANGES(AXP809, DLDO1, "dldo1", "dldoin", axp803_dldo2_ranges,
  372. 32, AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
  373. BIT(3)),
  374. AXP_DESC(AXP809, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
  375. AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
  376. AXP_DESC(AXP809, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
  377. AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
  378. AXP_DESC(AXP809, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
  379. AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
  380. AXP_DESC(AXP809, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
  381. AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
  382. /*
  383. * Note the datasheet only guarantees reliable operation up to
  384. * 3.3V, this needs to be enforced via dts provided constraints
  385. */
  386. AXP_DESC_IO(AXP809, LDO_IO0, "ldo_io0", "ips", 700, 3800, 100,
  387. AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
  388. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  389. /*
  390. * Note the datasheet only guarantees reliable operation up to
  391. * 3.3V, this needs to be enforced via dts provided constraints
  392. */
  393. AXP_DESC_IO(AXP809, LDO_IO1, "ldo_io1", "ips", 700, 3800, 100,
  394. AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
  395. AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
  396. AXP_DESC_FIXED(AXP809, RTC_LDO, "rtc_ldo", "ips", 1800),
  397. AXP_DESC_SW(AXP809, SW, "sw", "swin", AXP22X_PWR_OUT_CTRL2, BIT(6)),
  398. };
  399. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  400. {
  401. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  402. unsigned int reg = AXP20X_DCDC_FREQ;
  403. u32 min, max, def, step;
  404. switch (axp20x->variant) {
  405. case AXP202_ID:
  406. case AXP209_ID:
  407. min = 750;
  408. max = 1875;
  409. def = 1500;
  410. step = 75;
  411. break;
  412. case AXP803_ID:
  413. /*
  414. * AXP803 DCDC work frequency setting has the same range and
  415. * step as AXP22X, but at a different register.
  416. * Fall through to the check below.
  417. * (See include/linux/mfd/axp20x.h)
  418. */
  419. reg = AXP803_DCDC_FREQ_CTRL;
  420. case AXP806_ID:
  421. /*
  422. * AXP806 also have DCDC work frequency setting register at a
  423. * different position.
  424. */
  425. if (axp20x->variant == AXP806_ID)
  426. reg = AXP806_DCDC_FREQ_CTRL;
  427. case AXP221_ID:
  428. case AXP223_ID:
  429. case AXP809_ID:
  430. min = 1800;
  431. max = 4050;
  432. def = 3000;
  433. step = 150;
  434. break;
  435. default:
  436. dev_err(&pdev->dev,
  437. "Setting DCDC frequency for unsupported AXP variant\n");
  438. return -EINVAL;
  439. }
  440. if (dcdcfreq == 0)
  441. dcdcfreq = def;
  442. if (dcdcfreq < min) {
  443. dcdcfreq = min;
  444. dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
  445. min);
  446. }
  447. if (dcdcfreq > max) {
  448. dcdcfreq = max;
  449. dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
  450. max);
  451. }
  452. dcdcfreq = (dcdcfreq - min) / step;
  453. return regmap_update_bits(axp20x->regmap, reg,
  454. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  455. }
  456. static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  457. {
  458. struct device_node *np, *regulators;
  459. int ret = 0;
  460. u32 dcdcfreq = 0;
  461. np = of_node_get(pdev->dev.parent->of_node);
  462. if (!np)
  463. return 0;
  464. regulators = of_get_child_by_name(np, "regulators");
  465. if (!regulators) {
  466. dev_warn(&pdev->dev, "regulators node not found\n");
  467. } else {
  468. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  469. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  470. if (ret < 0) {
  471. dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  472. }
  473. of_node_put(regulators);
  474. }
  475. of_node_put(np);
  476. return ret;
  477. }
  478. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  479. {
  480. struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
  481. unsigned int reg = AXP20X_DCDC_MODE;
  482. unsigned int mask;
  483. switch (axp20x->variant) {
  484. case AXP202_ID:
  485. case AXP209_ID:
  486. if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  487. return -EINVAL;
  488. mask = AXP20X_WORKMODE_DCDC2_MASK;
  489. if (id == AXP20X_DCDC3)
  490. mask = AXP20X_WORKMODE_DCDC3_MASK;
  491. workmode <<= ffs(mask) - 1;
  492. break;
  493. case AXP806_ID:
  494. reg = AXP806_DCDC_MODE_CTRL2;
  495. /*
  496. * AXP806 DCDC regulator IDs have the same range as AXP22X.
  497. * Fall through to the check below.
  498. * (See include/linux/mfd/axp20x.h)
  499. */
  500. case AXP221_ID:
  501. case AXP223_ID:
  502. case AXP809_ID:
  503. if (id < AXP22X_DCDC1 || id > AXP22X_DCDC5)
  504. return -EINVAL;
  505. mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP22X_DCDC1);
  506. workmode <<= id - AXP22X_DCDC1;
  507. break;
  508. case AXP803_ID:
  509. if (id < AXP803_DCDC1 || id > AXP803_DCDC6)
  510. return -EINVAL;
  511. mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP803_DCDC1);
  512. workmode <<= id - AXP803_DCDC1;
  513. break;
  514. default:
  515. /* should not happen */
  516. WARN_ON(1);
  517. return -EINVAL;
  518. }
  519. return regmap_update_bits(rdev->regmap, reg, mask, workmode);
  520. }
  521. /*
  522. * This function checks whether a regulator is part of a poly-phase
  523. * output setup based on the registers settings. Returns true if it is.
  524. */
  525. static bool axp20x_is_polyphase_slave(struct axp20x_dev *axp20x, int id)
  526. {
  527. u32 reg = 0;
  528. /*
  529. * Currently in our supported AXP variants, only AXP803 and AXP806
  530. * have polyphase regulators.
  531. */
  532. switch (axp20x->variant) {
  533. case AXP803_ID:
  534. regmap_read(axp20x->regmap, AXP803_POLYPHASE_CTRL, &reg);
  535. switch (id) {
  536. case AXP803_DCDC3:
  537. return !!(reg & BIT(6));
  538. case AXP803_DCDC6:
  539. return !!(reg & BIT(5));
  540. }
  541. break;
  542. case AXP806_ID:
  543. regmap_read(axp20x->regmap, AXP806_DCDC_MODE_CTRL2, &reg);
  544. switch (id) {
  545. case AXP806_DCDCB:
  546. return (((reg & GENMASK(7, 6)) == BIT(6)) ||
  547. ((reg & GENMASK(7, 6)) == BIT(7)));
  548. case AXP806_DCDCC:
  549. return ((reg & GENMASK(7, 6)) == BIT(7));
  550. case AXP806_DCDCE:
  551. return !!(reg & BIT(5));
  552. }
  553. break;
  554. default:
  555. return false;
  556. }
  557. return false;
  558. }
  559. static int axp20x_regulator_probe(struct platform_device *pdev)
  560. {
  561. struct regulator_dev *rdev;
  562. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  563. const struct regulator_desc *regulators;
  564. struct regulator_config config = {
  565. .dev = pdev->dev.parent,
  566. .regmap = axp20x->regmap,
  567. .driver_data = axp20x,
  568. };
  569. int ret, i, nregulators;
  570. u32 workmode;
  571. const char *dcdc1_name = axp22x_regulators[AXP22X_DCDC1].name;
  572. const char *dcdc5_name = axp22x_regulators[AXP22X_DCDC5].name;
  573. bool drivevbus = false;
  574. switch (axp20x->variant) {
  575. case AXP202_ID:
  576. case AXP209_ID:
  577. regulators = axp20x_regulators;
  578. nregulators = AXP20X_REG_ID_MAX;
  579. break;
  580. case AXP221_ID:
  581. case AXP223_ID:
  582. regulators = axp22x_regulators;
  583. nregulators = AXP22X_REG_ID_MAX;
  584. drivevbus = of_property_read_bool(pdev->dev.parent->of_node,
  585. "x-powers,drive-vbus-en");
  586. break;
  587. case AXP803_ID:
  588. regulators = axp803_regulators;
  589. nregulators = AXP803_REG_ID_MAX;
  590. break;
  591. case AXP806_ID:
  592. regulators = axp806_regulators;
  593. nregulators = AXP806_REG_ID_MAX;
  594. break;
  595. case AXP809_ID:
  596. regulators = axp809_regulators;
  597. nregulators = AXP809_REG_ID_MAX;
  598. break;
  599. default:
  600. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  601. axp20x->variant);
  602. return -EINVAL;
  603. }
  604. /* This only sets the dcdc freq. Ignore any errors */
  605. axp20x_regulator_parse_dt(pdev);
  606. for (i = 0; i < nregulators; i++) {
  607. const struct regulator_desc *desc = &regulators[i];
  608. struct regulator_desc *new_desc;
  609. /*
  610. * If this regulator is a slave in a poly-phase setup,
  611. * skip it, as its controls are bound to the master
  612. * regulator and won't work.
  613. */
  614. if (axp20x_is_polyphase_slave(axp20x, i))
  615. continue;
  616. /*
  617. * Regulators DC1SW and DC5LDO are connected internally,
  618. * so we have to handle their supply names separately.
  619. *
  620. * We always register the regulators in proper sequence,
  621. * so the supply names are correctly read. See the last
  622. * part of this loop to see where we save the DT defined
  623. * name.
  624. */
  625. if ((regulators == axp22x_regulators && i == AXP22X_DC1SW) ||
  626. (regulators == axp803_regulators && i == AXP803_DC1SW) ||
  627. (regulators == axp809_regulators && i == AXP809_DC1SW)) {
  628. new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
  629. GFP_KERNEL);
  630. if (!new_desc)
  631. return -ENOMEM;
  632. *new_desc = regulators[i];
  633. new_desc->supply_name = dcdc1_name;
  634. desc = new_desc;
  635. }
  636. if ((regulators == axp22x_regulators && i == AXP22X_DC5LDO) ||
  637. (regulators == axp809_regulators && i == AXP809_DC5LDO)) {
  638. new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
  639. GFP_KERNEL);
  640. if (!new_desc)
  641. return -ENOMEM;
  642. *new_desc = regulators[i];
  643. new_desc->supply_name = dcdc5_name;
  644. desc = new_desc;
  645. }
  646. rdev = devm_regulator_register(&pdev->dev, desc, &config);
  647. if (IS_ERR(rdev)) {
  648. dev_err(&pdev->dev, "Failed to register %s\n",
  649. regulators[i].name);
  650. return PTR_ERR(rdev);
  651. }
  652. ret = of_property_read_u32(rdev->dev.of_node,
  653. "x-powers,dcdc-workmode",
  654. &workmode);
  655. if (!ret) {
  656. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  657. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  658. rdev->desc->name);
  659. }
  660. /*
  661. * Save AXP22X DCDC1 / DCDC5 regulator names for later.
  662. */
  663. if ((regulators == axp22x_regulators && i == AXP22X_DCDC1) ||
  664. (regulators == axp809_regulators && i == AXP809_DCDC1))
  665. of_property_read_string(rdev->dev.of_node,
  666. "regulator-name",
  667. &dcdc1_name);
  668. if ((regulators == axp22x_regulators && i == AXP22X_DCDC5) ||
  669. (regulators == axp809_regulators && i == AXP809_DCDC5))
  670. of_property_read_string(rdev->dev.of_node,
  671. "regulator-name",
  672. &dcdc5_name);
  673. }
  674. if (drivevbus) {
  675. /* Change N_VBUSEN sense pin to DRIVEVBUS output pin */
  676. regmap_update_bits(axp20x->regmap, AXP20X_OVER_TMP,
  677. AXP22X_MISC_N_VBUSEN_FUNC, 0);
  678. rdev = devm_regulator_register(&pdev->dev,
  679. &axp22x_drivevbus_regulator,
  680. &config);
  681. if (IS_ERR(rdev)) {
  682. dev_err(&pdev->dev, "Failed to register drivevbus\n");
  683. return PTR_ERR(rdev);
  684. }
  685. }
  686. return 0;
  687. }
  688. static struct platform_driver axp20x_regulator_driver = {
  689. .probe = axp20x_regulator_probe,
  690. .driver = {
  691. .name = "axp20x-regulator",
  692. },
  693. };
  694. module_platform_driver(axp20x_regulator_driver);
  695. MODULE_LICENSE("GPL v2");
  696. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  697. MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");
  698. MODULE_ALIAS("platform:axp20x-regulator");