bd96801-regulator.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2024 ROHM Semiconductors
  3. // bd96801-regulator.c ROHM BD96801 regulator driver
  4. /*
  5. * This version of the "BD86801 scalable PMIC"'s driver supports only very
  6. * basic set of the PMIC features. Most notably, there is no support for
  7. * the ERRB interrupt and the configurations which should be done when the
  8. * PMIC is in STBY mode.
  9. *
  10. * Supporting the ERRB interrupt would require dropping the regmap-IRQ
  11. * usage or working around (or accepting a presense of) a naming conflict
  12. * in debugFS IRQs.
  13. *
  14. * Being able to reliably do the configurations like changing the
  15. * regulator safety limits (like limits for the over/under -voltages, over
  16. * current, thermal protection) would require the configuring driver to be
  17. * synchronized with entity causing the PMIC state transitions. Eg, one
  18. * should be able to ensure the PMIC is in STBY state when the
  19. * configurations are applied to the hardware. How and when the PMIC state
  20. * transitions are to be done is likely to be very system specific, as will
  21. * be the need to configure these safety limits. Hence it's not simple to
  22. * come up with a generic solution.
  23. *
  24. * Users who require the ERRB handling and STBY state configurations can
  25. * have a look at the original RFC:
  26. * https://lore.kernel.org/all/cover.1712920132.git.mazziesaccount@gmail.com/
  27. * which implements a workaround to debugFS naming conflict and some of
  28. * the safety limit configurations - but leaves the state change handling
  29. * and synchronization to be implemented.
  30. *
  31. * It would be great to hear (and receive a patch!) if you implement the
  32. * STBY configuration support or a proper fix to the debugFS naming
  33. * conflict in your downstream driver ;)
  34. */
  35. #include <linux/cleanup.h>
  36. #include <linux/delay.h>
  37. #include <linux/err.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kernel.h>
  40. #include <linux/linear_range.h>
  41. #include <linux/mfd/rohm-generic.h>
  42. #include <linux/mfd/rohm-bd96801.h>
  43. #include <linux/module.h>
  44. #include <linux/of.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/regmap.h>
  47. #include <linux/regulator/coupler.h>
  48. #include <linux/regulator/driver.h>
  49. #include <linux/regulator/machine.h>
  50. #include <linux/regulator/of_regulator.h>
  51. #include <linux/slab.h>
  52. #include <linux/timer.h>
  53. enum {
  54. BD96801_BUCK1,
  55. BD96801_BUCK2,
  56. BD96801_BUCK3,
  57. BD96801_BUCK4,
  58. BD96801_LDO5,
  59. BD96801_LDO6,
  60. BD96801_LDO7,
  61. BD96801_REGULATOR_AMOUNT,
  62. };
  63. enum {
  64. BD96801_PROT_OVP,
  65. BD96801_PROT_UVP,
  66. BD96801_PROT_OCP,
  67. BD96801_PROT_TEMP,
  68. BD96801_NUM_PROT,
  69. };
  70. #define BD96801_ALWAYS_ON_REG 0x3c
  71. #define BD96801_REG_ENABLE 0x0b
  72. #define BD96801_BUCK1_EN_MASK BIT(0)
  73. #define BD96801_BUCK2_EN_MASK BIT(1)
  74. #define BD96801_BUCK3_EN_MASK BIT(2)
  75. #define BD96801_BUCK4_EN_MASK BIT(3)
  76. #define BD96801_LDO5_EN_MASK BIT(4)
  77. #define BD96801_LDO6_EN_MASK BIT(5)
  78. #define BD96801_LDO7_EN_MASK BIT(6)
  79. #define BD96801_BUCK1_VSEL_REG 0x28
  80. #define BD96801_BUCK2_VSEL_REG 0x29
  81. #define BD96801_BUCK3_VSEL_REG 0x2a
  82. #define BD96801_BUCK4_VSEL_REG 0x2b
  83. #define BD96801_LDO5_VSEL_REG 0x25
  84. #define BD96801_LDO6_VSEL_REG 0x26
  85. #define BD96801_LDO7_VSEL_REG 0x27
  86. #define BD96801_BUCK_VSEL_MASK 0x1F
  87. #define BD96801_LDO_VSEL_MASK 0xff
  88. #define BD96801_MASK_RAMP_DELAY 0xc0
  89. #define BD96801_INT_VOUT_BASE_REG 0x21
  90. #define BD96801_BUCK_INT_VOUT_MASK 0xff
  91. #define BD96801_BUCK_VOLTS 256
  92. #define BD96801_LDO_VOLTS 256
  93. #define BD96801_OVP_MASK 0x03
  94. #define BD96801_MASK_BUCK1_OVP_SHIFT 0x00
  95. #define BD96801_MASK_BUCK2_OVP_SHIFT 0x02
  96. #define BD96801_MASK_BUCK3_OVP_SHIFT 0x04
  97. #define BD96801_MASK_BUCK4_OVP_SHIFT 0x06
  98. #define BD96801_MASK_LDO5_OVP_SHIFT 0x00
  99. #define BD96801_MASK_LDO6_OVP_SHIFT 0x02
  100. #define BD96801_MASK_LDO7_OVP_SHIFT 0x04
  101. #define BD96801_PROT_LIMIT_OCP_MIN 0x00
  102. #define BD96801_PROT_LIMIT_LOW 0x01
  103. #define BD96801_PROT_LIMIT_MID 0x02
  104. #define BD96801_PROT_LIMIT_HI 0x03
  105. #define BD96801_REG_BUCK1_OCP 0x32
  106. #define BD96801_REG_BUCK2_OCP 0x32
  107. #define BD96801_REG_BUCK3_OCP 0x33
  108. #define BD96801_REG_BUCK4_OCP 0x33
  109. #define BD96801_MASK_BUCK1_OCP_SHIFT 0x00
  110. #define BD96801_MASK_BUCK2_OCP_SHIFT 0x04
  111. #define BD96801_MASK_BUCK3_OCP_SHIFT 0x00
  112. #define BD96801_MASK_BUCK4_OCP_SHIFT 0x04
  113. #define BD96801_REG_LDO5_OCP 0x34
  114. #define BD96801_REG_LDO6_OCP 0x34
  115. #define BD96801_REG_LDO7_OCP 0x34
  116. #define BD96801_MASK_LDO5_OCP_SHIFT 0x00
  117. #define BD96801_MASK_LDO6_OCP_SHIFT 0x02
  118. #define BD96801_MASK_LDO7_OCP_SHIFT 0x04
  119. #define BD96801_MASK_SHD_INTB BIT(7)
  120. #define BD96801_INTB_FATAL BIT(7)
  121. #define BD96801_NUM_REGULATORS 7
  122. #define BD96801_NUM_LDOS 4
  123. /*
  124. * Ramp rates for bucks are controlled by bits [7:6] as follows:
  125. * 00 => 1 mV/uS
  126. * 01 => 5 mV/uS
  127. * 10 => 10 mV/uS
  128. * 11 => 20 mV/uS
  129. */
  130. static const unsigned int buck_ramp_table[] = { 1000, 5000, 10000, 20000 };
  131. /*
  132. * This is a voltage range that get's appended to selected
  133. * bd96801_buck_init_volts value. The range from 0x0 to 0xF is actually
  134. * bd96801_buck_init_volts + 0 ... bd96801_buck_init_volts + 150mV
  135. * and the range from 0x10 to 0x1f is bd96801_buck_init_volts - 150mV ...
  136. * bd96801_buck_init_volts - 0. But as the members of linear_range
  137. * are all unsigned I will apply offset of -150 mV to value in
  138. * linear_range - which should increase these ranges with
  139. * 150 mV getting all the values to >= 0.
  140. */
  141. static const struct linear_range bd96801_tune_volts[] = {
  142. REGULATOR_LINEAR_RANGE(150000, 0x00, 0xF, 10000),
  143. REGULATOR_LINEAR_RANGE(0, 0x10, 0x1F, 10000),
  144. };
  145. static const struct linear_range bd96801_buck_init_volts[] = {
  146. REGULATOR_LINEAR_RANGE(500000 - 150000, 0x00, 0xc8, 5000),
  147. REGULATOR_LINEAR_RANGE(1550000 - 150000, 0xc9, 0xec, 50000),
  148. REGULATOR_LINEAR_RANGE(3300000 - 150000, 0xed, 0xff, 0),
  149. };
  150. static const struct linear_range bd96801_ldo_int_volts[] = {
  151. REGULATOR_LINEAR_RANGE(300000, 0x00, 0x78, 25000),
  152. REGULATOR_LINEAR_RANGE(3300000, 0x79, 0xff, 0),
  153. };
  154. #define BD96801_LDO_SD_VOLT_MASK 0x1
  155. #define BD96801_LDO_MODE_MASK 0x6
  156. #define BD96801_LDO_MODE_INT 0x0
  157. #define BD96801_LDO_MODE_SD 0x2
  158. #define BD96801_LDO_MODE_DDR 0x4
  159. static int ldo_ddr_volt_table[] = {500000, 300000};
  160. static int ldo_sd_volt_table[] = {3300000, 1800000};
  161. /* Constant IRQ initialization data (templates) */
  162. struct bd96801_irqinfo {
  163. int type;
  164. struct regulator_irq_desc irq_desc;
  165. int err_cfg;
  166. int wrn_cfg;
  167. const char *irq_name;
  168. };
  169. #define BD96801_IRQINFO(_type, _name, _irqoff_ms, _irqname) \
  170. { \
  171. .type = (_type), \
  172. .err_cfg = -1, \
  173. .wrn_cfg = -1, \
  174. .irq_name = (_irqname), \
  175. .irq_desc = { \
  176. .name = (_name), \
  177. .irq_off_ms = (_irqoff_ms), \
  178. .map_event = regulator_irq_map_event_simple, \
  179. }, \
  180. }
  181. static const struct bd96801_irqinfo buck1_irqinfo[] = {
  182. BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-h", 500,
  183. "bd96801-buck1-overcurr-h"),
  184. BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-l", 500,
  185. "bd96801-buck1-overcurr-l"),
  186. BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-n", 500,
  187. "bd96801-buck1-overcurr-n"),
  188. BD96801_IRQINFO(BD96801_PROT_OVP, "buck1-over-voltage", 500,
  189. "bd96801-buck1-overvolt"),
  190. BD96801_IRQINFO(BD96801_PROT_UVP, "buck1-under-voltage", 500,
  191. "bd96801-buck1-undervolt"),
  192. BD96801_IRQINFO(BD96801_PROT_TEMP, "buck1-over-temp", 500,
  193. "bd96801-buck1-thermal")
  194. };
  195. static const struct bd96801_irqinfo buck2_irqinfo[] = {
  196. BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-h", 500,
  197. "bd96801-buck2-overcurr-h"),
  198. BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-l", 500,
  199. "bd96801-buck2-overcurr-l"),
  200. BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-n", 500,
  201. "bd96801-buck2-overcurr-n"),
  202. BD96801_IRQINFO(BD96801_PROT_OVP, "buck2-over-voltage", 500,
  203. "bd96801-buck2-overvolt"),
  204. BD96801_IRQINFO(BD96801_PROT_UVP, "buck2-under-voltage", 500,
  205. "bd96801-buck2-undervolt"),
  206. BD96801_IRQINFO(BD96801_PROT_TEMP, "buck2-over-temp", 500,
  207. "bd96801-buck2-thermal")
  208. };
  209. static const struct bd96801_irqinfo buck3_irqinfo[] = {
  210. BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-h", 500,
  211. "bd96801-buck3-overcurr-h"),
  212. BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-l", 500,
  213. "bd96801-buck3-overcurr-l"),
  214. BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-n", 500,
  215. "bd96801-buck3-overcurr-n"),
  216. BD96801_IRQINFO(BD96801_PROT_OVP, "buck3-over-voltage", 500,
  217. "bd96801-buck3-overvolt"),
  218. BD96801_IRQINFO(BD96801_PROT_UVP, "buck3-under-voltage", 500,
  219. "bd96801-buck3-undervolt"),
  220. BD96801_IRQINFO(BD96801_PROT_TEMP, "buck3-over-temp", 500,
  221. "bd96801-buck3-thermal")
  222. };
  223. static const struct bd96801_irqinfo buck4_irqinfo[] = {
  224. BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-h", 500,
  225. "bd96801-buck4-overcurr-h"),
  226. BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-l", 500,
  227. "bd96801-buck4-overcurr-l"),
  228. BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-n", 500,
  229. "bd96801-buck4-overcurr-n"),
  230. BD96801_IRQINFO(BD96801_PROT_OVP, "buck4-over-voltage", 500,
  231. "bd96801-buck4-overvolt"),
  232. BD96801_IRQINFO(BD96801_PROT_UVP, "buck4-under-voltage", 500,
  233. "bd96801-buck4-undervolt"),
  234. BD96801_IRQINFO(BD96801_PROT_TEMP, "buck4-over-temp", 500,
  235. "bd96801-buck4-thermal")
  236. };
  237. static const struct bd96801_irqinfo ldo5_irqinfo[] = {
  238. BD96801_IRQINFO(BD96801_PROT_OCP, "ldo5-overcurr", 500,
  239. "bd96801-ldo5-overcurr"),
  240. BD96801_IRQINFO(BD96801_PROT_OVP, "ldo5-over-voltage", 500,
  241. "bd96801-ldo5-overvolt"),
  242. BD96801_IRQINFO(BD96801_PROT_UVP, "ldo5-under-voltage", 500,
  243. "bd96801-ldo5-undervolt"),
  244. };
  245. static const struct bd96801_irqinfo ldo6_irqinfo[] = {
  246. BD96801_IRQINFO(BD96801_PROT_OCP, "ldo6-overcurr", 500,
  247. "bd96801-ldo6-overcurr"),
  248. BD96801_IRQINFO(BD96801_PROT_OVP, "ldo6-over-voltage", 500,
  249. "bd96801-ldo6-overvolt"),
  250. BD96801_IRQINFO(BD96801_PROT_UVP, "ldo6-under-voltage", 500,
  251. "bd96801-ldo6-undervolt"),
  252. };
  253. static const struct bd96801_irqinfo ldo7_irqinfo[] = {
  254. BD96801_IRQINFO(BD96801_PROT_OCP, "ldo7-overcurr", 500,
  255. "bd96801-ldo7-overcurr"),
  256. BD96801_IRQINFO(BD96801_PROT_OVP, "ldo7-over-voltage", 500,
  257. "bd96801-ldo7-overvolt"),
  258. BD96801_IRQINFO(BD96801_PROT_UVP, "ldo7-under-voltage", 500,
  259. "bd96801-ldo7-undervolt"),
  260. };
  261. struct bd96801_irq_desc {
  262. struct bd96801_irqinfo *irqinfo;
  263. int num_irqs;
  264. };
  265. struct bd96801_regulator_data {
  266. struct regulator_desc desc;
  267. const struct linear_range *init_ranges;
  268. int num_ranges;
  269. struct bd96801_irq_desc irq_desc;
  270. int initial_voltage;
  271. int ldo_vol_lvl;
  272. int ldo_errs;
  273. };
  274. struct bd96801_pmic_data {
  275. struct bd96801_regulator_data regulator_data[BD96801_NUM_REGULATORS];
  276. struct regmap *regmap;
  277. int fatal_ind;
  278. };
  279. static int ldo_map_notif(int irq, struct regulator_irq_data *rid,
  280. unsigned long *dev_mask)
  281. {
  282. int i;
  283. for (i = 0; i < rid->num_states; i++) {
  284. struct bd96801_regulator_data *rdata;
  285. struct regulator_dev *rdev;
  286. rdev = rid->states[i].rdev;
  287. rdata = container_of(rdev->desc, struct bd96801_regulator_data,
  288. desc);
  289. rid->states[i].notifs = regulator_err2notif(rdata->ldo_errs);
  290. rid->states[i].errors = rdata->ldo_errs;
  291. *dev_mask |= BIT(i);
  292. }
  293. return 0;
  294. }
  295. static int bd96801_list_voltage_lr(struct regulator_dev *rdev,
  296. unsigned int selector)
  297. {
  298. int voltage;
  299. struct bd96801_regulator_data *data;
  300. data = container_of(rdev->desc, struct bd96801_regulator_data, desc);
  301. /*
  302. * The BD096801 has voltage setting in two registers. One giving the
  303. * "initial voltage" (can be changed only when regulator is disabled.
  304. * This driver caches the value and sets it only at startup. The other
  305. * register is voltage tuning value which applies -150 mV ... +150 mV
  306. * offset to the voltage.
  307. *
  308. * Note that the cached initial voltage stored in regulator data is
  309. * 'scaled down' by the 150 mV so that all of our tuning values are
  310. * >= 0. This is done because the linear_ranges uses unsigned values.
  311. *
  312. * As a result, we increase the tuning voltage which we get based on
  313. * the selector by the stored initial_voltage.
  314. */
  315. voltage = regulator_list_voltage_linear_range(rdev, selector);
  316. if (voltage < 0)
  317. return voltage;
  318. return voltage + data->initial_voltage;
  319. }
  320. static const struct regulator_ops bd96801_ldo_table_ops = {
  321. .is_enabled = regulator_is_enabled_regmap,
  322. .list_voltage = regulator_list_voltage_table,
  323. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  324. };
  325. static const struct regulator_ops bd96801_buck_ops = {
  326. .is_enabled = regulator_is_enabled_regmap,
  327. .list_voltage = bd96801_list_voltage_lr,
  328. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  329. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  330. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  331. .set_ramp_delay = regulator_set_ramp_delay_regmap,
  332. };
  333. static const struct regulator_ops bd96801_ldo_ops = {
  334. .is_enabled = regulator_is_enabled_regmap,
  335. .list_voltage = regulator_list_voltage_linear_range,
  336. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  337. };
  338. static int buck_get_initial_voltage(struct regmap *regmap, struct device *dev,
  339. struct bd96801_regulator_data *data)
  340. {
  341. int ret = 0, sel, initial_uv;
  342. int reg = BD96801_INT_VOUT_BASE_REG + data->desc.id;
  343. if (data->num_ranges) {
  344. ret = regmap_read(regmap, reg, &sel);
  345. sel &= BD96801_BUCK_INT_VOUT_MASK;
  346. ret = linear_range_get_value_array(data->init_ranges,
  347. data->num_ranges, sel,
  348. &initial_uv);
  349. if (ret)
  350. return ret;
  351. data->initial_voltage = initial_uv;
  352. dev_dbg(dev, "Tune-scaled initial voltage %u\n",
  353. data->initial_voltage);
  354. }
  355. return 0;
  356. }
  357. static int get_ldo_initial_voltage(struct regmap *regmap,
  358. struct device *dev,
  359. struct bd96801_regulator_data *data)
  360. {
  361. int ret;
  362. int cfgreg;
  363. ret = regmap_read(regmap, data->ldo_vol_lvl, &cfgreg);
  364. if (ret)
  365. return ret;
  366. switch (cfgreg & BD96801_LDO_MODE_MASK) {
  367. case BD96801_LDO_MODE_DDR:
  368. data->desc.volt_table = ldo_ddr_volt_table;
  369. data->desc.n_voltages = ARRAY_SIZE(ldo_ddr_volt_table);
  370. break;
  371. case BD96801_LDO_MODE_SD:
  372. data->desc.volt_table = ldo_sd_volt_table;
  373. data->desc.n_voltages = ARRAY_SIZE(ldo_sd_volt_table);
  374. break;
  375. default:
  376. dev_info(dev, "Leaving LDO to normal mode");
  377. return 0;
  378. }
  379. /* SD or DDR mode => override default ops */
  380. data->desc.ops = &bd96801_ldo_table_ops,
  381. data->desc.vsel_mask = 1;
  382. data->desc.vsel_reg = data->ldo_vol_lvl;
  383. return 0;
  384. }
  385. static int get_initial_voltage(struct device *dev, struct regmap *regmap,
  386. struct bd96801_regulator_data *data)
  387. {
  388. /* BUCK */
  389. if (data->desc.id <= BD96801_BUCK4)
  390. return buck_get_initial_voltage(regmap, dev, data);
  391. /* LDO */
  392. return get_ldo_initial_voltage(regmap, dev, data);
  393. }
  394. static int bd96801_walk_regulator_dt(struct device *dev, struct regmap *regmap,
  395. struct bd96801_regulator_data *data,
  396. int num)
  397. {
  398. int i, ret;
  399. struct device_node *nproot __free(device_node) =
  400. of_get_child_by_name(dev->parent->of_node, "regulators");
  401. if (!nproot) {
  402. dev_err(dev, "failed to find regulators node\n");
  403. return -ENODEV;
  404. }
  405. for_each_child_of_node_scoped(nproot, np) {
  406. for (i = 0; i < num; i++) {
  407. if (!of_node_name_eq(np, data[i].desc.of_match))
  408. continue;
  409. /*
  410. * If STBY configs are supported, we must pass node
  411. * here to extract the initial voltages from the DT.
  412. * Thus we do the initial voltage getting in this
  413. * loop.
  414. */
  415. ret = get_initial_voltage(dev, regmap, &data[i]);
  416. if (ret) {
  417. dev_err(dev,
  418. "Initializing voltages for %s failed\n",
  419. data[i].desc.name);
  420. return ret;
  421. }
  422. if (of_property_read_bool(np, "rohm,keep-on-stby")) {
  423. ret = regmap_set_bits(regmap,
  424. BD96801_ALWAYS_ON_REG,
  425. 1 << data[i].desc.id);
  426. if (ret) {
  427. dev_err(dev,
  428. "failed to set %s on-at-stby\n",
  429. data[i].desc.name);
  430. return ret;
  431. }
  432. }
  433. }
  434. }
  435. return 0;
  436. }
  437. /*
  438. * Template for regulator data. Probe will allocate dynamic / driver instance
  439. * struct so we should be on a safe side even if there were multiple PMICs to
  440. * control. Note that there is a plan to allow multiple PMICs to be used so
  441. * systems can scale better. I am however still slightly unsure how the
  442. * multi-PMIC case will be handled. I don't know if the processor will have I2C
  443. * acces to all of the PMICs or only the first one. I'd guess there will be
  444. * access provided to all PMICs for voltage scaling - but the errors will only
  445. * be informed via the master PMIC. Eg, we should prepare to support multiple
  446. * driver instances - either with or without the IRQs... Well, let's first
  447. * just support the simple and clear single-PMIC setup and ponder the multi PMIC
  448. * case later. What we can easly do for preparing is to not use static global
  449. * data for regulators though.
  450. */
  451. static const struct bd96801_pmic_data bd96801_data = {
  452. .regulator_data = {
  453. {
  454. .desc = {
  455. .name = "buck1",
  456. .of_match = of_match_ptr("buck1"),
  457. .regulators_node = of_match_ptr("regulators"),
  458. .id = BD96801_BUCK1,
  459. .ops = &bd96801_buck_ops,
  460. .type = REGULATOR_VOLTAGE,
  461. .linear_ranges = bd96801_tune_volts,
  462. .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
  463. .n_voltages = BD96801_BUCK_VOLTS,
  464. .enable_reg = BD96801_REG_ENABLE,
  465. .enable_mask = BD96801_BUCK1_EN_MASK,
  466. .enable_is_inverted = true,
  467. .vsel_reg = BD96801_BUCK1_VSEL_REG,
  468. .vsel_mask = BD96801_BUCK_VSEL_MASK,
  469. .ramp_reg = BD96801_BUCK1_VSEL_REG,
  470. .ramp_mask = BD96801_MASK_RAMP_DELAY,
  471. .ramp_delay_table = &buck_ramp_table[0],
  472. .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
  473. .owner = THIS_MODULE,
  474. },
  475. .init_ranges = bd96801_buck_init_volts,
  476. .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
  477. .irq_desc = {
  478. .irqinfo = (struct bd96801_irqinfo *)&buck1_irqinfo[0],
  479. .num_irqs = ARRAY_SIZE(buck1_irqinfo),
  480. },
  481. }, {
  482. .desc = {
  483. .name = "buck2",
  484. .of_match = of_match_ptr("buck2"),
  485. .regulators_node = of_match_ptr("regulators"),
  486. .id = BD96801_BUCK2,
  487. .ops = &bd96801_buck_ops,
  488. .type = REGULATOR_VOLTAGE,
  489. .linear_ranges = bd96801_tune_volts,
  490. .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
  491. .n_voltages = BD96801_BUCK_VOLTS,
  492. .enable_reg = BD96801_REG_ENABLE,
  493. .enable_mask = BD96801_BUCK2_EN_MASK,
  494. .enable_is_inverted = true,
  495. .vsel_reg = BD96801_BUCK2_VSEL_REG,
  496. .vsel_mask = BD96801_BUCK_VSEL_MASK,
  497. .ramp_reg = BD96801_BUCK2_VSEL_REG,
  498. .ramp_mask = BD96801_MASK_RAMP_DELAY,
  499. .ramp_delay_table = &buck_ramp_table[0],
  500. .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
  501. .owner = THIS_MODULE,
  502. },
  503. .irq_desc = {
  504. .irqinfo = (struct bd96801_irqinfo *)&buck2_irqinfo[0],
  505. .num_irqs = ARRAY_SIZE(buck2_irqinfo),
  506. },
  507. .init_ranges = bd96801_buck_init_volts,
  508. .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
  509. }, {
  510. .desc = {
  511. .name = "buck3",
  512. .of_match = of_match_ptr("buck3"),
  513. .regulators_node = of_match_ptr("regulators"),
  514. .id = BD96801_BUCK3,
  515. .ops = &bd96801_buck_ops,
  516. .type = REGULATOR_VOLTAGE,
  517. .linear_ranges = bd96801_tune_volts,
  518. .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
  519. .n_voltages = BD96801_BUCK_VOLTS,
  520. .enable_reg = BD96801_REG_ENABLE,
  521. .enable_mask = BD96801_BUCK3_EN_MASK,
  522. .enable_is_inverted = true,
  523. .vsel_reg = BD96801_BUCK3_VSEL_REG,
  524. .vsel_mask = BD96801_BUCK_VSEL_MASK,
  525. .ramp_reg = BD96801_BUCK3_VSEL_REG,
  526. .ramp_mask = BD96801_MASK_RAMP_DELAY,
  527. .ramp_delay_table = &buck_ramp_table[0],
  528. .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
  529. .owner = THIS_MODULE,
  530. },
  531. .irq_desc = {
  532. .irqinfo = (struct bd96801_irqinfo *)&buck3_irqinfo[0],
  533. .num_irqs = ARRAY_SIZE(buck3_irqinfo),
  534. },
  535. .init_ranges = bd96801_buck_init_volts,
  536. .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
  537. }, {
  538. .desc = {
  539. .name = "buck4",
  540. .of_match = of_match_ptr("buck4"),
  541. .regulators_node = of_match_ptr("regulators"),
  542. .id = BD96801_BUCK4,
  543. .ops = &bd96801_buck_ops,
  544. .type = REGULATOR_VOLTAGE,
  545. .linear_ranges = bd96801_tune_volts,
  546. .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
  547. .n_voltages = BD96801_BUCK_VOLTS,
  548. .enable_reg = BD96801_REG_ENABLE,
  549. .enable_mask = BD96801_BUCK4_EN_MASK,
  550. .enable_is_inverted = true,
  551. .vsel_reg = BD96801_BUCK4_VSEL_REG,
  552. .vsel_mask = BD96801_BUCK_VSEL_MASK,
  553. .ramp_reg = BD96801_BUCK4_VSEL_REG,
  554. .ramp_mask = BD96801_MASK_RAMP_DELAY,
  555. .ramp_delay_table = &buck_ramp_table[0],
  556. .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
  557. .owner = THIS_MODULE,
  558. },
  559. .irq_desc = {
  560. .irqinfo = (struct bd96801_irqinfo *)&buck4_irqinfo[0],
  561. .num_irqs = ARRAY_SIZE(buck4_irqinfo),
  562. },
  563. .init_ranges = bd96801_buck_init_volts,
  564. .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
  565. }, {
  566. .desc = {
  567. .name = "ldo5",
  568. .of_match = of_match_ptr("ldo5"),
  569. .regulators_node = of_match_ptr("regulators"),
  570. .id = BD96801_LDO5,
  571. .ops = &bd96801_ldo_ops,
  572. .type = REGULATOR_VOLTAGE,
  573. .linear_ranges = bd96801_ldo_int_volts,
  574. .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
  575. .n_voltages = BD96801_LDO_VOLTS,
  576. .enable_reg = BD96801_REG_ENABLE,
  577. .enable_mask = BD96801_LDO5_EN_MASK,
  578. .enable_is_inverted = true,
  579. .vsel_reg = BD96801_LDO5_VSEL_REG,
  580. .vsel_mask = BD96801_LDO_VSEL_MASK,
  581. .owner = THIS_MODULE,
  582. },
  583. .irq_desc = {
  584. .irqinfo = (struct bd96801_irqinfo *)&ldo5_irqinfo[0],
  585. .num_irqs = ARRAY_SIZE(ldo5_irqinfo),
  586. },
  587. .ldo_vol_lvl = BD96801_LDO5_VOL_LVL_REG,
  588. }, {
  589. .desc = {
  590. .name = "ldo6",
  591. .of_match = of_match_ptr("ldo6"),
  592. .regulators_node = of_match_ptr("regulators"),
  593. .id = BD96801_LDO6,
  594. .ops = &bd96801_ldo_ops,
  595. .type = REGULATOR_VOLTAGE,
  596. .linear_ranges = bd96801_ldo_int_volts,
  597. .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
  598. .n_voltages = BD96801_LDO_VOLTS,
  599. .enable_reg = BD96801_REG_ENABLE,
  600. .enable_mask = BD96801_LDO6_EN_MASK,
  601. .enable_is_inverted = true,
  602. .vsel_reg = BD96801_LDO6_VSEL_REG,
  603. .vsel_mask = BD96801_LDO_VSEL_MASK,
  604. .owner = THIS_MODULE,
  605. },
  606. .irq_desc = {
  607. .irqinfo = (struct bd96801_irqinfo *)&ldo6_irqinfo[0],
  608. .num_irqs = ARRAY_SIZE(ldo6_irqinfo),
  609. },
  610. .ldo_vol_lvl = BD96801_LDO6_VOL_LVL_REG,
  611. }, {
  612. .desc = {
  613. .name = "ldo7",
  614. .of_match = of_match_ptr("ldo7"),
  615. .regulators_node = of_match_ptr("regulators"),
  616. .id = BD96801_LDO7,
  617. .ops = &bd96801_ldo_ops,
  618. .type = REGULATOR_VOLTAGE,
  619. .linear_ranges = bd96801_ldo_int_volts,
  620. .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
  621. .n_voltages = BD96801_LDO_VOLTS,
  622. .enable_reg = BD96801_REG_ENABLE,
  623. .enable_mask = BD96801_LDO7_EN_MASK,
  624. .enable_is_inverted = true,
  625. .vsel_reg = BD96801_LDO7_VSEL_REG,
  626. .vsel_mask = BD96801_LDO_VSEL_MASK,
  627. .owner = THIS_MODULE,
  628. },
  629. .irq_desc = {
  630. .irqinfo = (struct bd96801_irqinfo *)&ldo7_irqinfo[0],
  631. .num_irqs = ARRAY_SIZE(ldo7_irqinfo),
  632. },
  633. .ldo_vol_lvl = BD96801_LDO7_VOL_LVL_REG,
  634. },
  635. },
  636. };
  637. static int initialize_pmic_data(struct device *dev,
  638. struct bd96801_pmic_data *pdata)
  639. {
  640. int r, i;
  641. /*
  642. * Allocate and initialize IRQ data for all of the regulators. We
  643. * wish to modify IRQ information independently for each driver
  644. * instance.
  645. */
  646. for (r = 0; r < BD96801_NUM_REGULATORS; r++) {
  647. const struct bd96801_irqinfo *template;
  648. struct bd96801_irqinfo *new;
  649. int num_infos;
  650. template = pdata->regulator_data[r].irq_desc.irqinfo;
  651. num_infos = pdata->regulator_data[r].irq_desc.num_irqs;
  652. new = devm_kcalloc(dev, num_infos, sizeof(*new), GFP_KERNEL);
  653. if (!new)
  654. return -ENOMEM;
  655. pdata->regulator_data[r].irq_desc.irqinfo = new;
  656. for (i = 0; i < num_infos; i++)
  657. new[i] = template[i];
  658. }
  659. return 0;
  660. }
  661. static int bd96801_rdev_intb_irqs(struct platform_device *pdev,
  662. struct bd96801_pmic_data *pdata,
  663. struct bd96801_irqinfo *iinfo,
  664. struct regulator_dev *rdev)
  665. {
  666. struct regulator_dev *rdev_arr[1];
  667. void *retp;
  668. int err = 0;
  669. int irq;
  670. int err_flags[] = {
  671. [BD96801_PROT_OVP] = REGULATOR_ERROR_REGULATION_OUT,
  672. [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE,
  673. [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT,
  674. [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP,
  675. };
  676. int wrn_flags[] = {
  677. [BD96801_PROT_OVP] = REGULATOR_ERROR_OVER_VOLTAGE_WARN,
  678. [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE_WARN,
  679. [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT_WARN,
  680. [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP_WARN,
  681. };
  682. /*
  683. * Don't install IRQ handler if both error and warning
  684. * notifications are explicitly disabled
  685. */
  686. if (!iinfo->err_cfg && !iinfo->wrn_cfg)
  687. return 0;
  688. if (WARN_ON(iinfo->type >= BD96801_NUM_PROT))
  689. return -EINVAL;
  690. if (iinfo->err_cfg)
  691. err = err_flags[iinfo->type];
  692. else if (iinfo->wrn_cfg)
  693. err = wrn_flags[iinfo->type];
  694. iinfo->irq_desc.data = pdata;
  695. irq = platform_get_irq_byname(pdev, iinfo->irq_name);
  696. if (irq < 0)
  697. return irq;
  698. /* Find notifications for this IRQ (WARN/ERR) */
  699. rdev_arr[0] = rdev;
  700. retp = devm_regulator_irq_helper(&pdev->dev,
  701. &iinfo->irq_desc, irq,
  702. 0, err, NULL, rdev_arr,
  703. 1);
  704. if (IS_ERR(retp))
  705. return PTR_ERR(retp);
  706. return 0;
  707. }
  708. static int bd96801_probe(struct platform_device *pdev)
  709. {
  710. struct regulator_dev *ldo_errs_rdev_arr[BD96801_NUM_LDOS];
  711. struct bd96801_regulator_data *rdesc;
  712. struct regulator_config config = {};
  713. int ldo_errs_arr[BD96801_NUM_LDOS];
  714. struct bd96801_pmic_data *pdata;
  715. int temp_notif_ldos = 0;
  716. struct device *parent;
  717. int i, ret;
  718. void *retp;
  719. parent = pdev->dev.parent;
  720. pdata = devm_kmemdup(&pdev->dev, &bd96801_data, sizeof(bd96801_data),
  721. GFP_KERNEL);
  722. if (!pdata)
  723. return -ENOMEM;
  724. if (initialize_pmic_data(&pdev->dev, pdata))
  725. return -ENOMEM;
  726. pdata->regmap = dev_get_regmap(parent, NULL);
  727. if (!pdata->regmap) {
  728. dev_err(&pdev->dev, "No register map found\n");
  729. return -ENODEV;
  730. }
  731. rdesc = &pdata->regulator_data[0];
  732. config.driver_data = pdata;
  733. config.regmap = pdata->regmap;
  734. config.dev = parent;
  735. ret = bd96801_walk_regulator_dt(&pdev->dev, pdata->regmap, rdesc,
  736. BD96801_NUM_REGULATORS);
  737. if (ret)
  738. return ret;
  739. for (i = 0; i < ARRAY_SIZE(pdata->regulator_data); i++) {
  740. struct regulator_dev *rdev;
  741. struct bd96801_irq_desc *idesc = &rdesc[i].irq_desc;
  742. int j;
  743. rdev = devm_regulator_register(&pdev->dev,
  744. &rdesc[i].desc, &config);
  745. if (IS_ERR(rdev)) {
  746. dev_err(&pdev->dev,
  747. "failed to register %s regulator\n",
  748. rdesc[i].desc.name);
  749. return PTR_ERR(rdev);
  750. }
  751. /*
  752. * LDOs don't have own temperature monitoring. If temperature
  753. * notification was requested for this LDO from DT then we will
  754. * add the regulator to be notified if central IC temperature
  755. * exceeds threshold.
  756. */
  757. if (rdesc[i].ldo_errs) {
  758. ldo_errs_rdev_arr[temp_notif_ldos] = rdev;
  759. ldo_errs_arr[temp_notif_ldos] = rdesc[i].ldo_errs;
  760. temp_notif_ldos++;
  761. }
  762. /* Register INTB handlers for configured protections */
  763. for (j = 0; j < idesc->num_irqs; j++) {
  764. ret = bd96801_rdev_intb_irqs(pdev, pdata,
  765. &idesc->irqinfo[j], rdev);
  766. if (ret)
  767. return ret;
  768. }
  769. }
  770. if (temp_notif_ldos) {
  771. int irq;
  772. struct regulator_irq_desc tw_desc = {
  773. .name = "bd96801-core-thermal",
  774. .irq_off_ms = 500,
  775. .map_event = ldo_map_notif,
  776. };
  777. irq = platform_get_irq_byname(pdev, "bd96801-core-thermal");
  778. if (irq < 0)
  779. return irq;
  780. retp = devm_regulator_irq_helper(&pdev->dev, &tw_desc, irq, 0,
  781. 0, &ldo_errs_arr[0],
  782. &ldo_errs_rdev_arr[0],
  783. temp_notif_ldos);
  784. if (IS_ERR(retp))
  785. return PTR_ERR(retp);
  786. }
  787. return 0;
  788. }
  789. static const struct platform_device_id bd96801_pmic_id[] = {
  790. { "bd96801-regulator", },
  791. { }
  792. };
  793. MODULE_DEVICE_TABLE(platform, bd96801_pmic_id);
  794. static struct platform_driver bd96801_regulator = {
  795. .driver = {
  796. .name = "bd96801-pmic"
  797. },
  798. .probe = bd96801_probe,
  799. .id_table = bd96801_pmic_id,
  800. };
  801. module_platform_driver(bd96801_regulator);
  802. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  803. MODULE_DESCRIPTION("BD96801 voltage regulator driver");
  804. MODULE_LICENSE("GPL");