tps6594-regulator.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Regulator driver for tps6594 PMIC
  4. //
  5. // Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/mfd/tps6594.h>
  18. #define BUCK_NB 5
  19. #define LDO_NB 4
  20. #define MULTI_PHASE_NB 4
  21. /* TPS6593 and LP8764 supports OV, UV, SC, ILIM */
  22. #define REGS_INT_NB 4
  23. /* TPS65224 supports OV or UV */
  24. #define TPS65224_REGS_INT_NB 1
  25. enum tps6594_regulator_id {
  26. /* DCDC's */
  27. TPS6594_BUCK_1,
  28. TPS6594_BUCK_2,
  29. TPS6594_BUCK_3,
  30. TPS6594_BUCK_4,
  31. TPS6594_BUCK_5,
  32. /* LDOs */
  33. TPS6594_LDO_1,
  34. TPS6594_LDO_2,
  35. TPS6594_LDO_3,
  36. TPS6594_LDO_4,
  37. };
  38. enum tps6594_multi_regulator_id {
  39. /* Multi-phase DCDC's */
  40. TPS6594_BUCK_12,
  41. TPS6594_BUCK_34,
  42. TPS6594_BUCK_123,
  43. TPS6594_BUCK_1234,
  44. };
  45. struct tps6594_regulator_irq_type {
  46. const char *irq_name;
  47. const char *regulator_name;
  48. const char *event_name;
  49. unsigned long event;
  50. };
  51. static struct tps6594_regulator_irq_type tps6594_ext_regulator_irq_types[] = {
  52. { TPS6594_IRQ_NAME_VCCA_OV, "VCCA", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  53. { TPS6594_IRQ_NAME_VCCA_UV, "VCCA", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  54. { TPS6594_IRQ_NAME_VMON1_OV, "VMON1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  55. { TPS6594_IRQ_NAME_VMON1_UV, "VMON1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  56. { TPS6594_IRQ_NAME_VMON1_RV, "VMON1", "residual voltage",
  57. REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  58. { TPS6594_IRQ_NAME_VMON2_OV, "VMON2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  59. { TPS6594_IRQ_NAME_VMON2_UV, "VMON2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  60. { TPS6594_IRQ_NAME_VMON2_RV, "VMON2", "residual voltage",
  61. REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  62. };
  63. static struct tps6594_regulator_irq_type tps65224_ext_regulator_irq_types[] = {
  64. { TPS65224_IRQ_NAME_VCCA_UVOV, "VCCA", "voltage out of range",
  65. REGULATOR_EVENT_REGULATION_OUT },
  66. { TPS65224_IRQ_NAME_VMON1_UVOV, "VMON1", "voltage out of range",
  67. REGULATOR_EVENT_REGULATION_OUT },
  68. { TPS65224_IRQ_NAME_VMON2_UVOV, "VMON2", "voltage out of range",
  69. REGULATOR_EVENT_REGULATION_OUT },
  70. };
  71. struct tps6594_regulator_irq_data {
  72. struct device *dev;
  73. struct tps6594_regulator_irq_type *type;
  74. struct regulator_dev *rdev;
  75. };
  76. struct tps6594_ext_regulator_irq_data {
  77. struct device *dev;
  78. struct tps6594_regulator_irq_type *type;
  79. };
  80. #define TPS6594_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
  81. _em, _cr, _cm, _lr, _nlr, _delay, _fuv, \
  82. _ct, _ncl, _bpm) \
  83. { \
  84. .name = _name, \
  85. .of_match = _of, \
  86. .regulators_node = of_match_ptr("regulators"), \
  87. .supply_name = _of, \
  88. .id = _id, \
  89. .ops = &(_ops), \
  90. .n_voltages = _n, \
  91. .type = _type, \
  92. .owner = THIS_MODULE, \
  93. .vsel_reg = _vr, \
  94. .vsel_mask = _vm, \
  95. .csel_reg = _cr, \
  96. .csel_mask = _cm, \
  97. .curr_table = _ct, \
  98. .n_current_limits = _ncl, \
  99. .enable_reg = _er, \
  100. .enable_mask = _em, \
  101. .volt_table = NULL, \
  102. .linear_ranges = _lr, \
  103. .n_linear_ranges = _nlr, \
  104. .ramp_delay = _delay, \
  105. .fixed_uV = _fuv, \
  106. .bypass_reg = _vr, \
  107. .bypass_mask = _bpm, \
  108. } \
  109. static const struct linear_range bucks_ranges[] = {
  110. REGULATOR_LINEAR_RANGE(300000, 0x0, 0xe, 20000),
  111. REGULATOR_LINEAR_RANGE(600000, 0xf, 0x72, 5000),
  112. REGULATOR_LINEAR_RANGE(1100000, 0x73, 0xaa, 10000),
  113. REGULATOR_LINEAR_RANGE(1660000, 0xab, 0xff, 20000),
  114. };
  115. static const struct linear_range ldos_1_2_3_ranges[] = {
  116. REGULATOR_LINEAR_RANGE(600000, 0x4, 0x3a, 50000),
  117. };
  118. static const struct linear_range ldos_4_ranges[] = {
  119. REGULATOR_LINEAR_RANGE(1200000, 0x20, 0x74, 25000),
  120. };
  121. /* Voltage range for TPS65224 Bucks and LDOs */
  122. static const struct linear_range tps65224_bucks_1_ranges[] = {
  123. REGULATOR_LINEAR_RANGE(500000, 0x0a, 0x0e, 20000),
  124. REGULATOR_LINEAR_RANGE(600000, 0x0f, 0x72, 5000),
  125. REGULATOR_LINEAR_RANGE(1100000, 0x73, 0xaa, 10000),
  126. REGULATOR_LINEAR_RANGE(1660000, 0xab, 0xfd, 20000),
  127. };
  128. static const struct linear_range tps65224_bucks_2_3_4_ranges[] = {
  129. REGULATOR_LINEAR_RANGE(500000, 0x0, 0x1a, 25000),
  130. REGULATOR_LINEAR_RANGE(1200000, 0x1b, 0x45, 50000),
  131. };
  132. static const struct linear_range tps65224_ldos_1_ranges[] = {
  133. REGULATOR_LINEAR_RANGE(1200000, 0xC, 0x36, 50000),
  134. };
  135. static const struct linear_range tps65224_ldos_2_3_ranges[] = {
  136. REGULATOR_LINEAR_RANGE(600000, 0x0, 0x38, 50000),
  137. };
  138. /* Operations permitted on BUCK1/2/3/4/5 */
  139. static const struct regulator_ops tps6594_bucks_ops = {
  140. .is_enabled = regulator_is_enabled_regmap,
  141. .enable = regulator_enable_regmap,
  142. .disable = regulator_disable_regmap,
  143. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  144. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  145. .list_voltage = regulator_list_voltage_linear_range,
  146. .map_voltage = regulator_map_voltage_linear_range,
  147. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  148. };
  149. /* Operations permitted on LDO1/2/3 */
  150. static const struct regulator_ops tps6594_ldos_1_2_3_ops = {
  151. .is_enabled = regulator_is_enabled_regmap,
  152. .enable = regulator_enable_regmap,
  153. .disable = regulator_disable_regmap,
  154. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  155. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  156. .list_voltage = regulator_list_voltage_linear_range,
  157. .map_voltage = regulator_map_voltage_linear_range,
  158. .set_bypass = regulator_set_bypass_regmap,
  159. .get_bypass = regulator_get_bypass_regmap,
  160. };
  161. /* Operations permitted on LDO4 */
  162. static const struct regulator_ops tps6594_ldos_4_ops = {
  163. .is_enabled = regulator_is_enabled_regmap,
  164. .enable = regulator_enable_regmap,
  165. .disable = regulator_disable_regmap,
  166. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  167. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  168. .list_voltage = regulator_list_voltage_linear_range,
  169. .map_voltage = regulator_map_voltage_linear_range,
  170. };
  171. static const struct regulator_desc buck_regs[] = {
  172. TPS6594_REGULATOR("BUCK1", "buck1", TPS6594_BUCK_1,
  173. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  174. TPS6594_REG_BUCKX_VOUT_1(0),
  175. TPS6594_MASK_BUCKS_VSET,
  176. TPS6594_REG_BUCKX_CTRL(0),
  177. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  178. 4, 0, 0, NULL, 0, 0),
  179. TPS6594_REGULATOR("BUCK2", "buck2", TPS6594_BUCK_2,
  180. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  181. TPS6594_REG_BUCKX_VOUT_1(1),
  182. TPS6594_MASK_BUCKS_VSET,
  183. TPS6594_REG_BUCKX_CTRL(1),
  184. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  185. 4, 0, 0, NULL, 0, 0),
  186. TPS6594_REGULATOR("BUCK3", "buck3", TPS6594_BUCK_3,
  187. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  188. TPS6594_REG_BUCKX_VOUT_1(2),
  189. TPS6594_MASK_BUCKS_VSET,
  190. TPS6594_REG_BUCKX_CTRL(2),
  191. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  192. 4, 0, 0, NULL, 0, 0),
  193. TPS6594_REGULATOR("BUCK4", "buck4", TPS6594_BUCK_4,
  194. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  195. TPS6594_REG_BUCKX_VOUT_1(3),
  196. TPS6594_MASK_BUCKS_VSET,
  197. TPS6594_REG_BUCKX_CTRL(3),
  198. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  199. 4, 0, 0, NULL, 0, 0),
  200. TPS6594_REGULATOR("BUCK5", "buck5", TPS6594_BUCK_5,
  201. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  202. TPS6594_REG_BUCKX_VOUT_1(4),
  203. TPS6594_MASK_BUCKS_VSET,
  204. TPS6594_REG_BUCKX_CTRL(4),
  205. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  206. 4, 0, 0, NULL, 0, 0),
  207. };
  208. /* Buck configuration for TPS65224 */
  209. static const struct regulator_desc tps65224_buck_regs[] = {
  210. TPS6594_REGULATOR("BUCK1", "buck1", TPS6594_BUCK_1,
  211. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCK1_VSET,
  212. TPS6594_REG_BUCKX_VOUT_1(0),
  213. TPS65224_MASK_BUCK1_VSET,
  214. TPS6594_REG_BUCKX_CTRL(0),
  215. TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_1_ranges,
  216. 4, 0, 0, NULL, 0, 0),
  217. TPS6594_REGULATOR("BUCK2", "buck2", TPS6594_BUCK_2,
  218. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
  219. TPS6594_REG_BUCKX_VOUT_1(1),
  220. TPS65224_MASK_BUCKS_VSET,
  221. TPS6594_REG_BUCKX_CTRL(1),
  222. TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
  223. 4, 0, 0, NULL, 0, 0),
  224. TPS6594_REGULATOR("BUCK3", "buck3", TPS6594_BUCK_3,
  225. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
  226. TPS6594_REG_BUCKX_VOUT_1(2),
  227. TPS65224_MASK_BUCKS_VSET,
  228. TPS6594_REG_BUCKX_CTRL(2),
  229. TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
  230. 4, 0, 0, NULL, 0, 0),
  231. TPS6594_REGULATOR("BUCK4", "buck4", TPS6594_BUCK_4,
  232. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
  233. TPS6594_REG_BUCKX_VOUT_1(3),
  234. TPS65224_MASK_BUCKS_VSET,
  235. TPS6594_REG_BUCKX_CTRL(3),
  236. TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
  237. 4, 0, 0, NULL, 0, 0),
  238. };
  239. static struct tps6594_regulator_irq_type tps6594_buck1_irq_types[] = {
  240. { TPS6594_IRQ_NAME_BUCK1_OV, "BUCK1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  241. { TPS6594_IRQ_NAME_BUCK1_UV, "BUCK1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  242. { TPS6594_IRQ_NAME_BUCK1_SC, "BUCK1", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  243. { TPS6594_IRQ_NAME_BUCK1_ILIM, "BUCK1", "reach ilim, overcurrent",
  244. REGULATOR_EVENT_OVER_CURRENT },
  245. };
  246. static struct tps6594_regulator_irq_type tps6594_buck2_irq_types[] = {
  247. { TPS6594_IRQ_NAME_BUCK2_OV, "BUCK2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  248. { TPS6594_IRQ_NAME_BUCK2_UV, "BUCK2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  249. { TPS6594_IRQ_NAME_BUCK2_SC, "BUCK2", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  250. { TPS6594_IRQ_NAME_BUCK2_ILIM, "BUCK2", "reach ilim, overcurrent",
  251. REGULATOR_EVENT_OVER_CURRENT },
  252. };
  253. static struct tps6594_regulator_irq_type tps6594_buck3_irq_types[] = {
  254. { TPS6594_IRQ_NAME_BUCK3_OV, "BUCK3", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  255. { TPS6594_IRQ_NAME_BUCK3_UV, "BUCK3", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  256. { TPS6594_IRQ_NAME_BUCK3_SC, "BUCK3", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  257. { TPS6594_IRQ_NAME_BUCK3_ILIM, "BUCK3", "reach ilim, overcurrent",
  258. REGULATOR_EVENT_OVER_CURRENT },
  259. };
  260. static struct tps6594_regulator_irq_type tps6594_buck4_irq_types[] = {
  261. { TPS6594_IRQ_NAME_BUCK4_OV, "BUCK4", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  262. { TPS6594_IRQ_NAME_BUCK4_UV, "BUCK4", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  263. { TPS6594_IRQ_NAME_BUCK4_SC, "BUCK4", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  264. { TPS6594_IRQ_NAME_BUCK4_ILIM, "BUCK4", "reach ilim, overcurrent",
  265. REGULATOR_EVENT_OVER_CURRENT },
  266. };
  267. static struct tps6594_regulator_irq_type tps6594_buck5_irq_types[] = {
  268. { TPS6594_IRQ_NAME_BUCK5_OV, "BUCK5", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  269. { TPS6594_IRQ_NAME_BUCK5_UV, "BUCK5", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  270. { TPS6594_IRQ_NAME_BUCK5_SC, "BUCK5", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  271. { TPS6594_IRQ_NAME_BUCK5_ILIM, "BUCK5", "reach ilim, overcurrent",
  272. REGULATOR_EVENT_OVER_CURRENT },
  273. };
  274. static struct tps6594_regulator_irq_type tps6594_ldo1_irq_types[] = {
  275. { TPS6594_IRQ_NAME_LDO1_OV, "LDO1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  276. { TPS6594_IRQ_NAME_LDO1_UV, "LDO1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  277. { TPS6594_IRQ_NAME_LDO1_SC, "LDO1", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  278. { TPS6594_IRQ_NAME_LDO1_ILIM, "LDO1", "reach ilim, overcurrent",
  279. REGULATOR_EVENT_OVER_CURRENT },
  280. };
  281. static struct tps6594_regulator_irq_type tps6594_ldo2_irq_types[] = {
  282. { TPS6594_IRQ_NAME_LDO2_OV, "LDO2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  283. { TPS6594_IRQ_NAME_LDO2_UV, "LDO2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  284. { TPS6594_IRQ_NAME_LDO2_SC, "LDO2", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  285. { TPS6594_IRQ_NAME_LDO2_ILIM, "LDO2", "reach ilim, overcurrent",
  286. REGULATOR_EVENT_OVER_CURRENT },
  287. };
  288. static struct tps6594_regulator_irq_type tps6594_ldo3_irq_types[] = {
  289. { TPS6594_IRQ_NAME_LDO3_OV, "LDO3", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  290. { TPS6594_IRQ_NAME_LDO3_UV, "LDO3", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  291. { TPS6594_IRQ_NAME_LDO3_SC, "LDO3", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  292. { TPS6594_IRQ_NAME_LDO3_ILIM, "LDO3", "reach ilim, overcurrent",
  293. REGULATOR_EVENT_OVER_CURRENT },
  294. };
  295. static struct tps6594_regulator_irq_type tps6594_ldo4_irq_types[] = {
  296. { TPS6594_IRQ_NAME_LDO4_OV, "LDO4", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
  297. { TPS6594_IRQ_NAME_LDO4_UV, "LDO4", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
  298. { TPS6594_IRQ_NAME_LDO4_SC, "LDO4", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
  299. { TPS6594_IRQ_NAME_LDO4_ILIM, "LDO4", "reach ilim, overcurrent",
  300. REGULATOR_EVENT_OVER_CURRENT },
  301. };
  302. static struct tps6594_regulator_irq_type tps65224_buck1_irq_types[] = {
  303. { TPS65224_IRQ_NAME_BUCK1_UVOV, "BUCK1", "voltage out of range",
  304. REGULATOR_EVENT_REGULATION_OUT },
  305. };
  306. static struct tps6594_regulator_irq_type tps65224_buck2_irq_types[] = {
  307. { TPS65224_IRQ_NAME_BUCK2_UVOV, "BUCK2", "voltage out of range",
  308. REGULATOR_EVENT_REGULATION_OUT },
  309. };
  310. static struct tps6594_regulator_irq_type tps65224_buck3_irq_types[] = {
  311. { TPS65224_IRQ_NAME_BUCK3_UVOV, "BUCK3", "voltage out of range",
  312. REGULATOR_EVENT_REGULATION_OUT },
  313. };
  314. static struct tps6594_regulator_irq_type tps65224_buck4_irq_types[] = {
  315. { TPS65224_IRQ_NAME_BUCK4_UVOV, "BUCK4", "voltage out of range",
  316. REGULATOR_EVENT_REGULATION_OUT },
  317. };
  318. static struct tps6594_regulator_irq_type tps65224_ldo1_irq_types[] = {
  319. { TPS65224_IRQ_NAME_LDO1_UVOV, "LDO1", "voltage out of range",
  320. REGULATOR_EVENT_REGULATION_OUT },
  321. };
  322. static struct tps6594_regulator_irq_type tps65224_ldo2_irq_types[] = {
  323. { TPS65224_IRQ_NAME_LDO2_UVOV, "LDO2", "voltage out of range",
  324. REGULATOR_EVENT_REGULATION_OUT },
  325. };
  326. static struct tps6594_regulator_irq_type tps65224_ldo3_irq_types[] = {
  327. { TPS65224_IRQ_NAME_LDO3_UVOV, "LDO3", "voltage out of range",
  328. REGULATOR_EVENT_REGULATION_OUT },
  329. };
  330. static struct tps6594_regulator_irq_type *tps6594_bucks_irq_types[] = {
  331. tps6594_buck1_irq_types,
  332. tps6594_buck2_irq_types,
  333. tps6594_buck3_irq_types,
  334. tps6594_buck4_irq_types,
  335. tps6594_buck5_irq_types,
  336. };
  337. static struct tps6594_regulator_irq_type *tps6594_ldos_irq_types[] = {
  338. tps6594_ldo1_irq_types,
  339. tps6594_ldo2_irq_types,
  340. tps6594_ldo3_irq_types,
  341. tps6594_ldo4_irq_types,
  342. };
  343. static struct tps6594_regulator_irq_type *tps65224_bucks_irq_types[] = {
  344. tps65224_buck1_irq_types,
  345. tps65224_buck2_irq_types,
  346. tps65224_buck3_irq_types,
  347. tps65224_buck4_irq_types,
  348. };
  349. static struct tps6594_regulator_irq_type *tps65224_ldos_irq_types[] = {
  350. tps65224_ldo1_irq_types,
  351. tps65224_ldo2_irq_types,
  352. tps65224_ldo3_irq_types,
  353. };
  354. static const struct regulator_desc tps6594_multi_regs[] = {
  355. TPS6594_REGULATOR("BUCK12", "buck12", TPS6594_BUCK_1,
  356. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  357. TPS6594_REG_BUCKX_VOUT_1(0),
  358. TPS6594_MASK_BUCKS_VSET,
  359. TPS6594_REG_BUCKX_CTRL(0),
  360. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  361. 4, 4000, 0, NULL, 0, 0),
  362. TPS6594_REGULATOR("BUCK34", "buck34", TPS6594_BUCK_3,
  363. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  364. TPS6594_REG_BUCKX_VOUT_1(2),
  365. TPS6594_MASK_BUCKS_VSET,
  366. TPS6594_REG_BUCKX_CTRL(2),
  367. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  368. 4, 0, 0, NULL, 0, 0),
  369. TPS6594_REGULATOR("BUCK123", "buck123", TPS6594_BUCK_1,
  370. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  371. TPS6594_REG_BUCKX_VOUT_1(0),
  372. TPS6594_MASK_BUCKS_VSET,
  373. TPS6594_REG_BUCKX_CTRL(0),
  374. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  375. 4, 4000, 0, NULL, 0, 0),
  376. TPS6594_REGULATOR("BUCK1234", "buck1234", TPS6594_BUCK_1,
  377. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
  378. TPS6594_REG_BUCKX_VOUT_1(0),
  379. TPS6594_MASK_BUCKS_VSET,
  380. TPS6594_REG_BUCKX_CTRL(0),
  381. TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
  382. 4, 4000, 0, NULL, 0, 0),
  383. };
  384. static const struct regulator_desc tps65224_multi_regs[] = {
  385. TPS6594_REGULATOR("BUCK12", "buck12", TPS6594_BUCK_1,
  386. REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCK1_VSET,
  387. TPS6594_REG_BUCKX_VOUT_1(0),
  388. TPS65224_MASK_BUCK1_VSET,
  389. TPS6594_REG_BUCKX_CTRL(0),
  390. TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_1_ranges,
  391. 4, 4000, 0, NULL, 0, 0),
  392. };
  393. static const struct regulator_desc tps6594_ldo_regs[] = {
  394. TPS6594_REGULATOR("LDO1", "ldo1", TPS6594_LDO_1,
  395. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  396. TPS6594_REG_LDOX_VOUT(0),
  397. TPS6594_MASK_LDO123_VSET,
  398. TPS6594_REG_LDOX_CTRL(0),
  399. TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
  400. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  401. TPS6594_REGULATOR("LDO2", "ldo2", TPS6594_LDO_2,
  402. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  403. TPS6594_REG_LDOX_VOUT(1),
  404. TPS6594_MASK_LDO123_VSET,
  405. TPS6594_REG_LDOX_CTRL(1),
  406. TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
  407. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  408. TPS6594_REGULATOR("LDO3", "ldo3", TPS6594_LDO_3,
  409. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  410. TPS6594_REG_LDOX_VOUT(2),
  411. TPS6594_MASK_LDO123_VSET,
  412. TPS6594_REG_LDOX_CTRL(2),
  413. TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
  414. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  415. TPS6594_REGULATOR("LDO4", "ldo4", TPS6594_LDO_4,
  416. REGULATOR_VOLTAGE, tps6594_ldos_4_ops, TPS6594_MASK_LDO4_VSET >> 1,
  417. TPS6594_REG_LDOX_VOUT(3),
  418. TPS6594_MASK_LDO4_VSET,
  419. TPS6594_REG_LDOX_CTRL(3),
  420. TPS6594_BIT_LDO_EN, 0, 0, ldos_4_ranges,
  421. 1, 0, 0, NULL, 0, 0),
  422. };
  423. static const struct regulator_desc tps65224_ldo_regs[] = {
  424. TPS6594_REGULATOR("LDO1", "ldo1", TPS6594_LDO_1,
  425. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  426. TPS6594_REG_LDOX_VOUT(0),
  427. TPS6594_MASK_LDO123_VSET,
  428. TPS6594_REG_LDOX_CTRL(0),
  429. TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_1_ranges,
  430. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  431. TPS6594_REGULATOR("LDO2", "ldo2", TPS6594_LDO_2,
  432. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  433. TPS6594_REG_LDOX_VOUT(1),
  434. TPS6594_MASK_LDO123_VSET,
  435. TPS6594_REG_LDOX_CTRL(1),
  436. TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_2_3_ranges,
  437. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  438. TPS6594_REGULATOR("LDO3", "ldo3", TPS6594_LDO_3,
  439. REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
  440. TPS6594_REG_LDOX_VOUT(2),
  441. TPS6594_MASK_LDO123_VSET,
  442. TPS6594_REG_LDOX_CTRL(2),
  443. TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_2_3_ranges,
  444. 1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
  445. };
  446. static irqreturn_t tps6594_regulator_irq_handler(int irq, void *data)
  447. {
  448. struct tps6594_regulator_irq_data *irq_data = data;
  449. if (irq_data->type->event_name[0] == '\0') {
  450. /* This is the timeout interrupt no specific regulator */
  451. dev_err(irq_data->dev,
  452. "System was put in shutdown due to timeout during an active or standby transition.\n");
  453. return IRQ_HANDLED;
  454. }
  455. dev_err(irq_data->dev, "Error IRQ trap %s for %s\n",
  456. irq_data->type->event_name, irq_data->type->regulator_name);
  457. regulator_notifier_call_chain(irq_data->rdev,
  458. irq_data->type->event, NULL);
  459. return IRQ_HANDLED;
  460. }
  461. static int tps6594_request_reg_irqs(struct platform_device *pdev,
  462. struct regulator_dev *rdev,
  463. struct tps6594_regulator_irq_data *irq_data,
  464. struct tps6594_regulator_irq_type *regs_irq_types,
  465. size_t interrupt_cnt,
  466. int *irq_idx)
  467. {
  468. struct tps6594_regulator_irq_type *irq_type;
  469. struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
  470. size_t j;
  471. int irq;
  472. int error;
  473. for (j = 0; j < interrupt_cnt; j++) {
  474. irq_type = &regs_irq_types[j];
  475. irq = platform_get_irq_byname(pdev, irq_type->irq_name);
  476. if (irq < 0)
  477. return -EINVAL;
  478. irq_data[*irq_idx].dev = tps->dev;
  479. irq_data[*irq_idx].type = irq_type;
  480. irq_data[*irq_idx].rdev = rdev;
  481. error = devm_request_threaded_irq(tps->dev, irq, NULL,
  482. tps6594_regulator_irq_handler, IRQF_ONESHOT,
  483. irq_type->irq_name, &irq_data[*irq_idx]);
  484. if (error) {
  485. dev_err(tps->dev, "tps6594 failed to request %s IRQ %d: %d\n",
  486. irq_type->irq_name, irq, error);
  487. return error;
  488. }
  489. (*irq_idx)++;
  490. }
  491. return 0;
  492. }
  493. static int tps6594_regulator_probe(struct platform_device *pdev)
  494. {
  495. struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
  496. struct regulator_dev *rdev;
  497. struct device_node *np = NULL;
  498. struct device_node *np_pmic_parent = NULL;
  499. struct regulator_config config = {};
  500. struct tps6594_regulator_irq_data *irq_data;
  501. struct tps6594_ext_regulator_irq_data *irq_ext_reg_data;
  502. struct tps6594_regulator_irq_type *irq_type;
  503. struct tps6594_regulator_irq_type *irq_types;
  504. bool buck_configured[BUCK_NB] = { false };
  505. bool buck_multi[MULTI_PHASE_NB] = { false };
  506. static const char *npname;
  507. int error, i, irq, multi;
  508. int irq_idx = 0;
  509. int buck_idx = 0;
  510. int nr_ldo;
  511. int nr_buck;
  512. int nr_types;
  513. unsigned int irq_count;
  514. unsigned int multi_phase_cnt;
  515. size_t reg_irq_nb;
  516. struct tps6594_regulator_irq_type **bucks_irq_types;
  517. const struct regulator_desc *multi_regs;
  518. struct tps6594_regulator_irq_type **ldos_irq_types;
  519. const struct regulator_desc *ldo_regs;
  520. size_t interrupt_count;
  521. if (tps->chip_id == TPS65224) {
  522. bucks_irq_types = tps65224_bucks_irq_types;
  523. interrupt_count = ARRAY_SIZE(tps65224_buck1_irq_types);
  524. multi_regs = tps65224_multi_regs;
  525. ldos_irq_types = tps65224_ldos_irq_types;
  526. ldo_regs = tps65224_ldo_regs;
  527. multi_phase_cnt = ARRAY_SIZE(tps65224_multi_regs);
  528. } else {
  529. bucks_irq_types = tps6594_bucks_irq_types;
  530. interrupt_count = ARRAY_SIZE(tps6594_buck1_irq_types);
  531. multi_regs = tps6594_multi_regs;
  532. ldos_irq_types = tps6594_ldos_irq_types;
  533. ldo_regs = tps6594_ldo_regs;
  534. multi_phase_cnt = ARRAY_SIZE(tps6594_multi_regs);
  535. }
  536. enum {
  537. MULTI_BUCK12,
  538. MULTI_BUCK12_34,
  539. MULTI_BUCK123,
  540. MULTI_BUCK1234,
  541. };
  542. config.dev = tps->dev;
  543. config.driver_data = tps;
  544. config.regmap = tps->regmap;
  545. /*
  546. * Switch case defines different possible multi phase config
  547. * This is based on dts buck node name.
  548. * Buck node name must be chosen accordingly.
  549. * Default case is no Multiphase buck.
  550. * In case of Multiphase configuration, value should be defined for
  551. * buck_configured to avoid creating bucks for every buck in multiphase
  552. */
  553. for (multi = 0; multi < multi_phase_cnt; multi++) {
  554. np = of_find_node_by_name(tps->dev->of_node, multi_regs[multi].supply_name);
  555. npname = of_node_full_name(np);
  556. np_pmic_parent = of_get_parent(of_get_parent(np));
  557. if (of_node_cmp(of_node_full_name(np_pmic_parent), tps->dev->of_node->full_name))
  558. continue;
  559. if (strcmp(npname, multi_regs[multi].supply_name) == 0) {
  560. switch (multi) {
  561. case MULTI_BUCK12:
  562. buck_multi[0] = true;
  563. buck_configured[0] = true;
  564. buck_configured[1] = true;
  565. break;
  566. /* multiphase buck34 is supported only with buck12 */
  567. case MULTI_BUCK12_34:
  568. buck_multi[0] = true;
  569. buck_multi[1] = true;
  570. buck_configured[0] = true;
  571. buck_configured[1] = true;
  572. buck_configured[2] = true;
  573. buck_configured[3] = true;
  574. break;
  575. case MULTI_BUCK123:
  576. buck_multi[2] = true;
  577. buck_configured[0] = true;
  578. buck_configured[1] = true;
  579. buck_configured[2] = true;
  580. break;
  581. case MULTI_BUCK1234:
  582. buck_multi[3] = true;
  583. buck_configured[0] = true;
  584. buck_configured[1] = true;
  585. buck_configured[2] = true;
  586. buck_configured[3] = true;
  587. break;
  588. }
  589. }
  590. }
  591. if (tps->chip_id == TPS65224) {
  592. nr_buck = ARRAY_SIZE(tps65224_buck_regs);
  593. nr_ldo = ARRAY_SIZE(tps65224_ldo_regs);
  594. nr_types = TPS65224_REGS_INT_NB;
  595. } else {
  596. nr_buck = ARRAY_SIZE(buck_regs);
  597. nr_ldo = (tps->chip_id == LP8764) ? 0 : ARRAY_SIZE(tps6594_ldo_regs);
  598. nr_types = REGS_INT_NB;
  599. }
  600. reg_irq_nb = nr_types * (nr_buck + nr_ldo);
  601. irq_data = devm_kmalloc_array(tps->dev, reg_irq_nb,
  602. sizeof(struct tps6594_regulator_irq_data), GFP_KERNEL);
  603. if (!irq_data)
  604. return -ENOMEM;
  605. for (i = 0; i < multi_phase_cnt; i++) {
  606. if (!buck_multi[i])
  607. continue;
  608. rdev = devm_regulator_register(&pdev->dev, &multi_regs[i], &config);
  609. if (IS_ERR(rdev))
  610. return dev_err_probe(tps->dev, PTR_ERR(rdev),
  611. "failed to register %s regulator\n",
  612. pdev->name);
  613. /* config multiphase buck12+buck34 */
  614. if (i == MULTI_BUCK12_34)
  615. buck_idx = 2;
  616. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  617. bucks_irq_types[buck_idx],
  618. interrupt_count, &irq_idx);
  619. if (error)
  620. return error;
  621. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  622. bucks_irq_types[buck_idx + 1],
  623. interrupt_count, &irq_idx);
  624. if (error)
  625. return error;
  626. if (i == MULTI_BUCK123 || i == MULTI_BUCK1234) {
  627. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  628. tps6594_bucks_irq_types[buck_idx + 2],
  629. interrupt_count,
  630. &irq_idx);
  631. if (error)
  632. return error;
  633. }
  634. if (i == MULTI_BUCK1234) {
  635. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  636. tps6594_bucks_irq_types[buck_idx + 3],
  637. interrupt_count,
  638. &irq_idx);
  639. if (error)
  640. return error;
  641. }
  642. }
  643. for (i = 0; i < nr_buck; i++) {
  644. if (buck_configured[i])
  645. continue;
  646. const struct regulator_desc *buck_cfg = (tps->chip_id == TPS65224) ?
  647. tps65224_buck_regs : buck_regs;
  648. rdev = devm_regulator_register(&pdev->dev, &buck_cfg[i], &config);
  649. if (IS_ERR(rdev))
  650. return dev_err_probe(tps->dev, PTR_ERR(rdev),
  651. "failed to register %s regulator\n", pdev->name);
  652. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  653. bucks_irq_types[i], interrupt_count, &irq_idx);
  654. if (error)
  655. return error;
  656. }
  657. /* LP8764 doesn't have LDO */
  658. if (tps->chip_id != LP8764) {
  659. for (i = 0; i < nr_ldo; i++) {
  660. rdev = devm_regulator_register(&pdev->dev, &ldo_regs[i], &config);
  661. if (IS_ERR(rdev))
  662. return dev_err_probe(tps->dev, PTR_ERR(rdev),
  663. "failed to register %s regulator\n",
  664. pdev->name);
  665. error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
  666. ldos_irq_types[i], interrupt_count,
  667. &irq_idx);
  668. if (error)
  669. return error;
  670. }
  671. }
  672. if (tps->chip_id == TPS65224) {
  673. irq_types = tps65224_ext_regulator_irq_types;
  674. irq_count = ARRAY_SIZE(tps65224_ext_regulator_irq_types);
  675. } else {
  676. irq_types = tps6594_ext_regulator_irq_types;
  677. if (tps->chip_id == LP8764)
  678. irq_count = ARRAY_SIZE(tps6594_ext_regulator_irq_types);
  679. else
  680. /* TPS6593 supports only VCCA OV and UV */
  681. irq_count = 2;
  682. }
  683. irq_ext_reg_data = devm_kmalloc_array(tps->dev,
  684. irq_count,
  685. sizeof(struct tps6594_ext_regulator_irq_data),
  686. GFP_KERNEL);
  687. if (!irq_ext_reg_data)
  688. return -ENOMEM;
  689. for (i = 0; i < irq_count; ++i) {
  690. irq_type = &irq_types[i];
  691. irq = platform_get_irq_byname(pdev, irq_type->irq_name);
  692. if (irq < 0)
  693. return -EINVAL;
  694. irq_ext_reg_data[i].dev = tps->dev;
  695. irq_ext_reg_data[i].type = irq_type;
  696. error = devm_request_threaded_irq(tps->dev, irq, NULL,
  697. tps6594_regulator_irq_handler,
  698. IRQF_ONESHOT,
  699. irq_type->irq_name,
  700. &irq_ext_reg_data[i]);
  701. if (error)
  702. return dev_err_probe(tps->dev, error,
  703. "failed to request %s IRQ %d\n",
  704. irq_type->irq_name, irq);
  705. }
  706. return 0;
  707. }
  708. static struct platform_driver tps6594_regulator_driver = {
  709. .driver = {
  710. .name = "tps6594-regulator",
  711. },
  712. .probe = tps6594_regulator_probe,
  713. };
  714. module_platform_driver(tps6594_regulator_driver);
  715. MODULE_ALIAS("platform:tps6594-regulator");
  716. MODULE_AUTHOR("Jerome Neanne <jneanne@baylibre.com>");
  717. MODULE_AUTHOR("Nirmala Devi Mal Nadar <m.nirmaladevi@ltts.com>");
  718. MODULE_DESCRIPTION("TPS6594 voltage regulator driver");
  719. MODULE_LICENSE("GPL");