bd71815-regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright 2014 Embest Technology Co. Ltd. Inc.
  4. // bd71815-regulator.c ROHM BD71815 regulator driver
  5. //
  6. // Author: Tony Luo <luofc@embedinfo.com>
  7. //
  8. // Partially rewritten at 2021 by
  9. // Matti Vaittinen <matti.vaitinen@fi.rohmeurope.com>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/mfd/rohm-generic.h>
  21. #include <linux/mfd/rohm-bd71815.h>
  22. #include <linux/regulator/of_regulator.h>
  23. struct bd71815_regulator {
  24. struct regulator_desc desc;
  25. const struct rohm_dvs_config *dvs;
  26. };
  27. static const int bd7181x_wled_currents[] = {
  28. 10, 20, 30, 50, 70, 100, 200, 300, 500, 700, 1000, 2000, 3000, 4000,
  29. 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000,
  30. 16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000,
  31. };
  32. static const struct rohm_dvs_config buck1_dvs = {
  33. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  34. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  35. .run_reg = BD71815_REG_BUCK1_VOLT_H,
  36. .run_mask = BD71815_VOLT_MASK,
  37. .run_on_mask = BD71815_BUCK_RUN_ON,
  38. .snvs_on_mask = BD71815_BUCK_SNVS_ON,
  39. .suspend_reg = BD71815_REG_BUCK1_VOLT_L,
  40. .suspend_mask = BD71815_VOLT_MASK,
  41. .suspend_on_mask = BD71815_BUCK_SUSP_ON,
  42. .lpsr_reg = BD71815_REG_BUCK1_VOLT_L,
  43. .lpsr_mask = BD71815_VOLT_MASK,
  44. .lpsr_on_mask = BD71815_BUCK_LPSR_ON,
  45. };
  46. static const struct rohm_dvs_config buck2_dvs = {
  47. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  48. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  49. .run_reg = BD71815_REG_BUCK2_VOLT_H,
  50. .run_mask = BD71815_VOLT_MASK,
  51. .run_on_mask = BD71815_BUCK_RUN_ON,
  52. .snvs_on_mask = BD71815_BUCK_SNVS_ON,
  53. .suspend_reg = BD71815_REG_BUCK2_VOLT_L,
  54. .suspend_mask = BD71815_VOLT_MASK,
  55. .suspend_on_mask = BD71815_BUCK_SUSP_ON,
  56. .lpsr_reg = BD71815_REG_BUCK2_VOLT_L,
  57. .lpsr_mask = BD71815_VOLT_MASK,
  58. .lpsr_on_mask = BD71815_BUCK_LPSR_ON,
  59. };
  60. static const struct rohm_dvs_config buck3_dvs = {
  61. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  62. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  63. .run_reg = BD71815_REG_BUCK3_VOLT,
  64. .run_mask = BD71815_VOLT_MASK,
  65. .run_on_mask = BD71815_BUCK_RUN_ON,
  66. .snvs_on_mask = BD71815_BUCK_SNVS_ON,
  67. .suspend_on_mask = BD71815_BUCK_SUSP_ON,
  68. .lpsr_on_mask = BD71815_BUCK_LPSR_ON,
  69. };
  70. static const struct rohm_dvs_config buck4_dvs = {
  71. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  72. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  73. .run_reg = BD71815_REG_BUCK4_VOLT,
  74. .run_mask = BD71815_VOLT_MASK,
  75. .run_on_mask = BD71815_BUCK_RUN_ON,
  76. .snvs_on_mask = BD71815_BUCK_SNVS_ON,
  77. .suspend_on_mask = BD71815_BUCK_SUSP_ON,
  78. .lpsr_on_mask = BD71815_BUCK_LPSR_ON,
  79. };
  80. static const struct rohm_dvs_config ldo1_dvs = {
  81. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  82. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  83. .run_reg = BD71815_REG_LDO_MODE1,
  84. .run_mask = BD71815_VOLT_MASK,
  85. .run_on_mask = LDO1_RUN_ON,
  86. .snvs_on_mask = LDO1_SNVS_ON,
  87. .suspend_on_mask = LDO1_SUSP_ON,
  88. .lpsr_on_mask = LDO1_LPSR_ON,
  89. };
  90. static const struct rohm_dvs_config ldo2_dvs = {
  91. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  92. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  93. .run_reg = BD71815_REG_LDO_MODE2,
  94. .run_mask = BD71815_VOLT_MASK,
  95. .run_on_mask = LDO2_RUN_ON,
  96. .snvs_on_mask = LDO2_SNVS_ON,
  97. .suspend_on_mask = LDO2_SUSP_ON,
  98. .lpsr_on_mask = LDO2_LPSR_ON,
  99. };
  100. static const struct rohm_dvs_config ldo3_dvs = {
  101. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  102. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  103. .run_reg = BD71815_REG_LDO_MODE2,
  104. .run_mask = BD71815_VOLT_MASK,
  105. .run_on_mask = LDO3_RUN_ON,
  106. .snvs_on_mask = LDO3_SNVS_ON,
  107. .suspend_on_mask = LDO3_SUSP_ON,
  108. .lpsr_on_mask = LDO3_LPSR_ON,
  109. };
  110. static const struct rohm_dvs_config ldo4_dvs = {
  111. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  112. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  113. .run_reg = BD71815_REG_LDO_MODE3,
  114. .run_mask = BD71815_VOLT_MASK,
  115. .run_on_mask = LDO4_RUN_ON,
  116. .snvs_on_mask = LDO4_SNVS_ON,
  117. .suspend_on_mask = LDO4_SUSP_ON,
  118. .lpsr_on_mask = LDO4_LPSR_ON,
  119. };
  120. static const struct rohm_dvs_config ldo5_dvs = {
  121. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  122. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  123. .run_reg = BD71815_REG_LDO_MODE3,
  124. .run_mask = BD71815_VOLT_MASK,
  125. .run_on_mask = LDO5_RUN_ON,
  126. .snvs_on_mask = LDO5_SNVS_ON,
  127. .suspend_on_mask = LDO5_SUSP_ON,
  128. .lpsr_on_mask = LDO5_LPSR_ON,
  129. };
  130. static const struct rohm_dvs_config dvref_dvs = {
  131. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  132. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  133. .run_on_mask = DVREF_RUN_ON,
  134. .snvs_on_mask = DVREF_SNVS_ON,
  135. .suspend_on_mask = DVREF_SUSP_ON,
  136. .lpsr_on_mask = DVREF_LPSR_ON,
  137. };
  138. static const struct rohm_dvs_config ldolpsr_dvs = {
  139. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  140. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  141. .run_on_mask = DVREF_RUN_ON,
  142. .snvs_on_mask = DVREF_SNVS_ON,
  143. .suspend_on_mask = DVREF_SUSP_ON,
  144. .lpsr_on_mask = DVREF_LPSR_ON,
  145. };
  146. static const struct rohm_dvs_config buck5_dvs = {
  147. .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
  148. ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
  149. .run_reg = BD71815_REG_BUCK5_VOLT,
  150. .run_mask = BD71815_VOLT_MASK,
  151. .run_on_mask = BD71815_BUCK_RUN_ON,
  152. .snvs_on_mask = BD71815_BUCK_SNVS_ON,
  153. .suspend_on_mask = BD71815_BUCK_SUSP_ON,
  154. .lpsr_on_mask = BD71815_BUCK_LPSR_ON,
  155. };
  156. static int set_hw_dvs_levels(struct device_node *np,
  157. const struct regulator_desc *desc,
  158. struct regulator_config *cfg)
  159. {
  160. struct bd71815_regulator *data;
  161. data = container_of(desc, struct bd71815_regulator, desc);
  162. return rohm_regulator_set_dvs_levels(data->dvs, np, desc, cfg->regmap);
  163. }
  164. /*
  165. * Bucks 1 and 2 have two voltage selection registers where selected
  166. * voltage can be set. Which of the registers is used can be either controlled
  167. * by a control bit in register - or by HW state. If HW state specific voltages
  168. * are given - then we assume HW state based control should be used.
  169. *
  170. * If volatge value is updated to currently selected register - then output
  171. * voltage is immediately changed no matter what is set as ramp rate. Thus we
  172. * default changing voltage by writing new value to inactive register and
  173. * then updating the 'register selection' bit. This naturally only works when
  174. * HW state machine is not used to select the voltage.
  175. */
  176. static int buck12_set_hw_dvs_levels(struct device_node *np,
  177. const struct regulator_desc *desc,
  178. struct regulator_config *cfg)
  179. {
  180. struct bd71815_regulator *data;
  181. int ret = 0, val;
  182. data = container_of(desc, struct bd71815_regulator, desc);
  183. if (of_property_present(np, "rohm,dvs-run-voltage") ||
  184. of_property_present(np, "rohm,dvs-suspend-voltage") ||
  185. of_property_present(np, "rohm,dvs-lpsr-voltage") ||
  186. of_property_present(np, "rohm,dvs-snvs-voltage")) {
  187. ret = regmap_read(cfg->regmap, desc->vsel_reg, &val);
  188. if (ret)
  189. return ret;
  190. if (!(BD71815_BUCK_STBY_DVS & val) &&
  191. !(BD71815_BUCK_DVSSEL & val)) {
  192. int val2;
  193. /*
  194. * We are currently using voltage from _L.
  195. * We'd better copy it to _H and switch to it to
  196. * avoid shutting us down if LPSR or SUSPEND is set to
  197. * disabled. _L value is at reg _H + 1
  198. */
  199. ret = regmap_read(cfg->regmap, desc->vsel_reg + 1,
  200. &val2);
  201. if (ret)
  202. return ret;
  203. ret = regmap_update_bits(cfg->regmap, desc->vsel_reg,
  204. BD71815_VOLT_MASK |
  205. BD71815_BUCK_DVSSEL,
  206. val2 | BD71815_BUCK_DVSSEL);
  207. if (ret)
  208. return ret;
  209. }
  210. ret = rohm_regulator_set_dvs_levels(data->dvs, np, desc,
  211. cfg->regmap);
  212. if (ret)
  213. return ret;
  214. /*
  215. * DVS levels were given => use HW-state machine for voltage
  216. * controls. NOTE: AFAIK, This means that if voltage is changed
  217. * by SW the ramp-rate is not respected. Should we disable
  218. * SW voltage control when the HW state machine is used?
  219. */
  220. ret = regmap_update_bits(cfg->regmap, desc->vsel_reg,
  221. BD71815_BUCK_STBY_DVS,
  222. BD71815_BUCK_STBY_DVS);
  223. }
  224. return ret;
  225. }
  226. /*
  227. * BUCK1/2
  228. * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
  229. * 00: 10.00mV/usec 10mV 1uS
  230. * 01: 5.00mV/usec 10mV 2uS
  231. * 10: 2.50mV/usec 10mV 4uS
  232. * 11: 1.25mV/usec 10mV 8uS
  233. */
  234. static const unsigned int bd7181x_ramp_table[] = { 10000, 5000, 2500, 1250 };
  235. static int bd7181x_led_set_current_limit(struct regulator_dev *rdev,
  236. int min_uA, int max_uA)
  237. {
  238. int ret;
  239. int onstatus;
  240. onstatus = regulator_is_enabled_regmap(rdev);
  241. ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
  242. if (!ret) {
  243. int newstatus;
  244. newstatus = regulator_is_enabled_regmap(rdev);
  245. if (onstatus != newstatus) {
  246. /*
  247. * HW FIX: spurious led status change detected. Toggle
  248. * state as a workaround
  249. */
  250. if (onstatus)
  251. ret = regulator_enable_regmap(rdev);
  252. else
  253. ret = regulator_disable_regmap(rdev);
  254. if (ret)
  255. dev_err(rdev_get_dev(rdev),
  256. "failed to revert the LED state (%d)\n",
  257. ret);
  258. }
  259. }
  260. return ret;
  261. }
  262. static int bd7181x_buck12_get_voltage_sel(struct regulator_dev *rdev)
  263. {
  264. int rid = rdev_get_id(rdev);
  265. int ret, regh, regl, val;
  266. regh = BD71815_REG_BUCK1_VOLT_H + rid * 0x2;
  267. regl = BD71815_REG_BUCK1_VOLT_L + rid * 0x2;
  268. ret = regmap_read(rdev->regmap, regh, &val);
  269. if (ret)
  270. return ret;
  271. /*
  272. * If we use HW state machine based voltage reg selection - then we
  273. * return BD71815_REG_BUCK1_VOLT_H which is used at RUN.
  274. * Else we do return the BD71815_REG_BUCK1_VOLT_H or
  275. * BD71815_REG_BUCK1_VOLT_L depending on which is selected to be used
  276. * by BD71815_BUCK_DVSSEL bit
  277. */
  278. if ((!(val & BD71815_BUCK_STBY_DVS)) && (!(val & BD71815_BUCK_DVSSEL)))
  279. ret = regmap_read(rdev->regmap, regl, &val);
  280. if (ret)
  281. return ret;
  282. return val & BD71815_VOLT_MASK;
  283. }
  284. /*
  285. * For Buck 1/2.
  286. */
  287. static int bd7181x_buck12_set_voltage_sel(struct regulator_dev *rdev,
  288. unsigned int sel)
  289. {
  290. int rid = rdev_get_id(rdev);
  291. int ret, val, reg, regh, regl;
  292. regh = BD71815_REG_BUCK1_VOLT_H + rid*0x2;
  293. regl = BD71815_REG_BUCK1_VOLT_L + rid*0x2;
  294. ret = regmap_read(rdev->regmap, regh, &val);
  295. if (ret)
  296. return ret;
  297. /*
  298. * If bucks 1 & 2 are controlled by state machine - then the RUN state
  299. * voltage is set to BD71815_REG_BUCK1_VOLT_H. Changing SUSPEND/LPSR
  300. * voltages at runtime is not supported by this driver.
  301. */
  302. if (((val & BD71815_BUCK_STBY_DVS))) {
  303. return regmap_update_bits(rdev->regmap, regh, BD71815_VOLT_MASK,
  304. sel);
  305. }
  306. /* Update new voltage to the register which is not selected now */
  307. if (val & BD71815_BUCK_DVSSEL)
  308. reg = regl;
  309. else
  310. reg = regh;
  311. ret = regmap_update_bits(rdev->regmap, reg, BD71815_VOLT_MASK, sel);
  312. if (ret)
  313. return ret;
  314. /* Select the other DVS register to be used */
  315. return regmap_update_bits(rdev->regmap, regh, BD71815_BUCK_DVSSEL,
  316. ~val);
  317. }
  318. static const struct regulator_ops bd7181x_ldo_regulator_ops = {
  319. .enable = regulator_enable_regmap,
  320. .disable = regulator_disable_regmap,
  321. .is_enabled = regulator_is_enabled_regmap,
  322. .list_voltage = regulator_list_voltage_linear,
  323. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  324. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  325. };
  326. static const struct regulator_ops bd7181x_fixed_regulator_ops = {
  327. .enable = regulator_enable_regmap,
  328. .disable = regulator_disable_regmap,
  329. .is_enabled = regulator_is_enabled_regmap,
  330. .list_voltage = regulator_list_voltage_linear,
  331. };
  332. static const struct regulator_ops bd7181x_buck_regulator_ops = {
  333. .enable = regulator_enable_regmap,
  334. .disable = regulator_disable_regmap,
  335. .is_enabled = regulator_is_enabled_regmap,
  336. .list_voltage = regulator_list_voltage_linear,
  337. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  338. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  339. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  340. };
  341. static const struct regulator_ops bd7181x_buck12_regulator_ops = {
  342. .enable = regulator_enable_regmap,
  343. .disable = regulator_disable_regmap,
  344. .is_enabled = regulator_is_enabled_regmap,
  345. .list_voltage = regulator_list_voltage_linear,
  346. .set_voltage_sel = bd7181x_buck12_set_voltage_sel,
  347. .get_voltage_sel = bd7181x_buck12_get_voltage_sel,
  348. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  349. .set_ramp_delay = regulator_set_ramp_delay_regmap,
  350. };
  351. static const struct regulator_ops bd7181x_led_regulator_ops = {
  352. .enable = regulator_enable_regmap,
  353. .disable = regulator_disable_regmap,
  354. .is_enabled = regulator_is_enabled_regmap,
  355. .set_current_limit = bd7181x_led_set_current_limit,
  356. .get_current_limit = regulator_get_current_limit_regmap,
  357. };
  358. #define BD71815_FIXED_REG(_name, _id, ereg, emsk, voltage, _dvs) \
  359. [(_id)] = { \
  360. .desc = { \
  361. .name = #_name, \
  362. .of_match = of_match_ptr(#_name), \
  363. .regulators_node = of_match_ptr("regulators"), \
  364. .n_voltages = 1, \
  365. .ops = &bd7181x_fixed_regulator_ops, \
  366. .type = REGULATOR_VOLTAGE, \
  367. .id = (_id), \
  368. .owner = THIS_MODULE, \
  369. .min_uV = (voltage), \
  370. .enable_reg = (ereg), \
  371. .enable_mask = (emsk), \
  372. .of_parse_cb = set_hw_dvs_levels, \
  373. }, \
  374. .dvs = (_dvs), \
  375. }
  376. #define BD71815_BUCK_REG(_name, _id, vsel, ereg, min, max, step, _dvs) \
  377. [(_id)] = { \
  378. .desc = { \
  379. .name = #_name, \
  380. .of_match = of_match_ptr(#_name), \
  381. .regulators_node = of_match_ptr("regulators"), \
  382. .n_voltages = ((max) - (min)) / (step) + 1, \
  383. .ops = &bd7181x_buck_regulator_ops, \
  384. .type = REGULATOR_VOLTAGE, \
  385. .id = (_id), \
  386. .owner = THIS_MODULE, \
  387. .min_uV = (min), \
  388. .uV_step = (step), \
  389. .vsel_reg = (vsel), \
  390. .vsel_mask = BD71815_VOLT_MASK, \
  391. .enable_reg = (ereg), \
  392. .enable_mask = BD71815_BUCK_RUN_ON, \
  393. .of_parse_cb = set_hw_dvs_levels, \
  394. }, \
  395. .dvs = (_dvs), \
  396. }
  397. #define BD71815_BUCK12_REG(_name, _id, vsel, ereg, min, max, step, \
  398. _dvs) \
  399. [(_id)] = { \
  400. .desc = { \
  401. .name = #_name, \
  402. .of_match = of_match_ptr(#_name), \
  403. .regulators_node = of_match_ptr("regulators"), \
  404. .n_voltages = ((max) - (min)) / (step) + 1, \
  405. .ops = &bd7181x_buck12_regulator_ops, \
  406. .type = REGULATOR_VOLTAGE, \
  407. .id = (_id), \
  408. .owner = THIS_MODULE, \
  409. .min_uV = (min), \
  410. .uV_step = (step), \
  411. .vsel_reg = (vsel), \
  412. .vsel_mask = BD71815_VOLT_MASK, \
  413. .enable_reg = (ereg), \
  414. .enable_mask = BD71815_BUCK_RUN_ON, \
  415. .ramp_reg = (ereg), \
  416. .ramp_mask = BD71815_BUCK_RAMPRATE_MASK, \
  417. .ramp_delay_table = bd7181x_ramp_table, \
  418. .n_ramp_values = ARRAY_SIZE(bd7181x_ramp_table),\
  419. .of_parse_cb = buck12_set_hw_dvs_levels, \
  420. }, \
  421. .dvs = (_dvs), \
  422. }
  423. #define BD71815_LED_REG(_name, _id, csel, mask, ereg, emsk, currents) \
  424. [(_id)] = { \
  425. .desc = { \
  426. .name = #_name, \
  427. .of_match = of_match_ptr(#_name), \
  428. .regulators_node = of_match_ptr("regulators"), \
  429. .n_current_limits = ARRAY_SIZE(currents), \
  430. .ops = &bd7181x_led_regulator_ops, \
  431. .type = REGULATOR_CURRENT, \
  432. .id = (_id), \
  433. .owner = THIS_MODULE, \
  434. .curr_table = currents, \
  435. .csel_reg = (csel), \
  436. .csel_mask = (mask), \
  437. .enable_reg = (ereg), \
  438. .enable_mask = (emsk), \
  439. }, \
  440. }
  441. #define BD71815_LDO_REG(_name, _id, vsel, ereg, emsk, min, max, step, \
  442. _dvs) \
  443. [(_id)] = { \
  444. .desc = { \
  445. .name = #_name, \
  446. .of_match = of_match_ptr(#_name), \
  447. .regulators_node = of_match_ptr("regulators"), \
  448. .n_voltages = ((max) - (min)) / (step) + 1, \
  449. .ops = &bd7181x_ldo_regulator_ops, \
  450. .type = REGULATOR_VOLTAGE, \
  451. .id = (_id), \
  452. .owner = THIS_MODULE, \
  453. .min_uV = (min), \
  454. .uV_step = (step), \
  455. .vsel_reg = (vsel), \
  456. .vsel_mask = BD71815_VOLT_MASK, \
  457. .enable_reg = (ereg), \
  458. .enable_mask = (emsk), \
  459. .of_parse_cb = set_hw_dvs_levels, \
  460. }, \
  461. .dvs = (_dvs), \
  462. }
  463. static const struct bd71815_regulator bd71815_regulators[] = {
  464. BD71815_BUCK12_REG(buck1, BD71815_BUCK1, BD71815_REG_BUCK1_VOLT_H,
  465. BD71815_REG_BUCK1_MODE, 800000, 2000000, 25000,
  466. &buck1_dvs),
  467. BD71815_BUCK12_REG(buck2, BD71815_BUCK2, BD71815_REG_BUCK2_VOLT_H,
  468. BD71815_REG_BUCK2_MODE, 800000, 2000000, 25000,
  469. &buck2_dvs),
  470. BD71815_BUCK_REG(buck3, BD71815_BUCK3, BD71815_REG_BUCK3_VOLT,
  471. BD71815_REG_BUCK3_MODE, 1200000, 2700000, 50000,
  472. &buck3_dvs),
  473. BD71815_BUCK_REG(buck4, BD71815_BUCK4, BD71815_REG_BUCK4_VOLT,
  474. BD71815_REG_BUCK4_MODE, 1100000, 1850000, 25000,
  475. &buck4_dvs),
  476. BD71815_BUCK_REG(buck5, BD71815_BUCK5, BD71815_REG_BUCK5_VOLT,
  477. BD71815_REG_BUCK5_MODE, 1800000, 3300000, 50000,
  478. &buck5_dvs),
  479. BD71815_LDO_REG(ldo1, BD71815_LDO1, BD71815_REG_LDO1_VOLT,
  480. BD71815_REG_LDO_MODE1, LDO1_RUN_ON, 800000, 3300000,
  481. 50000, &ldo1_dvs),
  482. BD71815_LDO_REG(ldo2, BD71815_LDO2, BD71815_REG_LDO2_VOLT,
  483. BD71815_REG_LDO_MODE2, LDO2_RUN_ON, 800000, 3300000,
  484. 50000, &ldo2_dvs),
  485. /*
  486. * Let's default LDO3 to be enabled by SW. We can override ops if DT
  487. * says LDO3 should be enabled by HW when DCIN is connected.
  488. */
  489. BD71815_LDO_REG(ldo3, BD71815_LDO3, BD71815_REG_LDO3_VOLT,
  490. BD71815_REG_LDO_MODE2, LDO3_RUN_ON, 800000, 3300000,
  491. 50000, &ldo3_dvs),
  492. BD71815_LDO_REG(ldo4, BD71815_LDO4, BD71815_REG_LDO4_VOLT,
  493. BD71815_REG_LDO_MODE3, LDO4_RUN_ON, 800000, 3300000,
  494. 50000, &ldo4_dvs),
  495. BD71815_LDO_REG(ldo5, BD71815_LDO5, BD71815_REG_LDO5_VOLT_H,
  496. BD71815_REG_LDO_MODE3, LDO5_RUN_ON, 800000, 3300000,
  497. 50000, &ldo5_dvs),
  498. BD71815_FIXED_REG(ldodvref, BD71815_LDODVREF, BD71815_REG_LDO_MODE4,
  499. DVREF_RUN_ON, 3000000, &dvref_dvs),
  500. BD71815_FIXED_REG(ldolpsr, BD71815_LDOLPSR, BD71815_REG_LDO_MODE4,
  501. LDO_LPSR_RUN_ON, 1800000, &ldolpsr_dvs),
  502. BD71815_LED_REG(wled, BD71815_WLED, BD71815_REG_LED_DIMM, LED_DIMM_MASK,
  503. BD71815_REG_LED_CTRL, LED_RUN_ON,
  504. bd7181x_wled_currents),
  505. };
  506. static int bd7181x_probe(struct platform_device *pdev)
  507. {
  508. struct regulator_config config = {};
  509. int i, ret;
  510. struct gpio_desc *ldo4_en;
  511. struct regmap *regmap;
  512. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  513. if (!regmap) {
  514. dev_err(&pdev->dev, "No parent regmap\n");
  515. return -ENODEV;
  516. }
  517. ldo4_en = devm_fwnode_gpiod_get(&pdev->dev,
  518. dev_fwnode(pdev->dev.parent),
  519. "rohm,vsel", GPIOD_ASIS, "ldo4-en");
  520. if (IS_ERR(ldo4_en)) {
  521. ret = PTR_ERR(ldo4_en);
  522. if (ret != -ENOENT)
  523. return ret;
  524. ldo4_en = NULL;
  525. }
  526. /* Disable to go to ship-mode */
  527. ret = regmap_update_bits(regmap, BD71815_REG_PWRCTRL, RESTARTEN, 0);
  528. if (ret)
  529. return ret;
  530. config.dev = pdev->dev.parent;
  531. config.regmap = regmap;
  532. for (i = 0; i < BD71815_REGULATOR_CNT; i++) {
  533. const struct regulator_desc *desc;
  534. struct regulator_dev *rdev;
  535. desc = &bd71815_regulators[i].desc;
  536. if (i == BD71815_LDO4)
  537. config.ena_gpiod = ldo4_en;
  538. else
  539. config.ena_gpiod = NULL;
  540. rdev = devm_regulator_register(&pdev->dev, desc, &config);
  541. if (IS_ERR(rdev))
  542. return dev_err_probe(&pdev->dev, PTR_ERR(rdev),
  543. "failed to register %s regulator\n",
  544. desc->name);
  545. }
  546. return 0;
  547. }
  548. static const struct platform_device_id bd7181x_pmic_id[] = {
  549. { "bd71815-pmic", ROHM_CHIP_TYPE_BD71815 },
  550. { },
  551. };
  552. MODULE_DEVICE_TABLE(platform, bd7181x_pmic_id);
  553. static struct platform_driver bd7181x_regulator = {
  554. .driver = {
  555. .name = "bd7181x-pmic",
  556. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  557. },
  558. .probe = bd7181x_probe,
  559. .id_table = bd7181x_pmic_id,
  560. };
  561. module_platform_driver(bd7181x_regulator);
  562. MODULE_AUTHOR("Tony Luo <luofc@embedinfo.com>");
  563. MODULE_DESCRIPTION("BD71815 voltage regulator driver");
  564. MODULE_LICENSE("GPL v2");
  565. MODULE_ALIAS("platform:bd7181x-pmic");