tps6594-regulator.c 29 KB

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