hi6421-regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Device driver for regulators in Hi6421 IC
  3. *
  4. * Copyright (c) <2011-2014> HiSilicon Technologies Co., Ltd.
  5. * http://www.hisilicon.com
  6. * Copyright (c) <2013-2014> Linaro Ltd.
  7. * http://www.linaro.org
  8. *
  9. * Author: Guodong Xu <guodong.xu@linaro.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/module.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regulator/of_regulator.h>
  25. #include <linux/mfd/hi6421-pmic.h>
  26. /*
  27. * struct hi6421_regulator_pdata - Hi6421 regulator data of platform device
  28. * @lock: mutex to serialize regulator enable
  29. */
  30. struct hi6421_regulator_pdata {
  31. struct mutex lock;
  32. };
  33. /*
  34. * struct hi6421_regulator_info - hi6421 regulator information
  35. * @desc: regulator description
  36. * @mode_mask: ECO mode bitmask of LDOs; for BUCKs, this masks sleep
  37. * @eco_microamp: eco mode load upper limit (in uA), valid for LDOs only
  38. */
  39. struct hi6421_regulator_info {
  40. struct regulator_desc desc;
  41. u8 mode_mask;
  42. u32 eco_microamp;
  43. };
  44. /* HI6421 regulators */
  45. enum hi6421_regulator_id {
  46. HI6421_LDO0,
  47. HI6421_LDO1,
  48. HI6421_LDO2,
  49. HI6421_LDO3,
  50. HI6421_LDO4,
  51. HI6421_LDO5,
  52. HI6421_LDO6,
  53. HI6421_LDO7,
  54. HI6421_LDO8,
  55. HI6421_LDO9,
  56. HI6421_LDO10,
  57. HI6421_LDO11,
  58. HI6421_LDO12,
  59. HI6421_LDO13,
  60. HI6421_LDO14,
  61. HI6421_LDO15,
  62. HI6421_LDO16,
  63. HI6421_LDO17,
  64. HI6421_LDO18,
  65. HI6421_LDO19,
  66. HI6421_LDO20,
  67. HI6421_LDOAUDIO,
  68. HI6421_BUCK0,
  69. HI6421_BUCK1,
  70. HI6421_BUCK2,
  71. HI6421_BUCK3,
  72. HI6421_BUCK4,
  73. HI6421_BUCK5,
  74. HI6421_NUM_REGULATORS,
  75. };
  76. #define HI6421_REGULATOR_OF_MATCH(_name, id) \
  77. { \
  78. .name = #_name, \
  79. .driver_data = (void *) HI6421_##id, \
  80. }
  81. static struct of_regulator_match hi6421_regulator_match[] = {
  82. HI6421_REGULATOR_OF_MATCH(hi6421_vout0, LDO0),
  83. HI6421_REGULATOR_OF_MATCH(hi6421_vout1, LDO1),
  84. HI6421_REGULATOR_OF_MATCH(hi6421_vout2, LDO2),
  85. HI6421_REGULATOR_OF_MATCH(hi6421_vout3, LDO3),
  86. HI6421_REGULATOR_OF_MATCH(hi6421_vout4, LDO4),
  87. HI6421_REGULATOR_OF_MATCH(hi6421_vout5, LDO5),
  88. HI6421_REGULATOR_OF_MATCH(hi6421_vout6, LDO6),
  89. HI6421_REGULATOR_OF_MATCH(hi6421_vout7, LDO7),
  90. HI6421_REGULATOR_OF_MATCH(hi6421_vout8, LDO8),
  91. HI6421_REGULATOR_OF_MATCH(hi6421_vout9, LDO9),
  92. HI6421_REGULATOR_OF_MATCH(hi6421_vout10, LDO10),
  93. HI6421_REGULATOR_OF_MATCH(hi6421_vout11, LDO11),
  94. HI6421_REGULATOR_OF_MATCH(hi6421_vout12, LDO12),
  95. HI6421_REGULATOR_OF_MATCH(hi6421_vout13, LDO13),
  96. HI6421_REGULATOR_OF_MATCH(hi6421_vout14, LDO14),
  97. HI6421_REGULATOR_OF_MATCH(hi6421_vout15, LDO15),
  98. HI6421_REGULATOR_OF_MATCH(hi6421_vout16, LDO16),
  99. HI6421_REGULATOR_OF_MATCH(hi6421_vout17, LDO17),
  100. HI6421_REGULATOR_OF_MATCH(hi6421_vout18, LDO18),
  101. HI6421_REGULATOR_OF_MATCH(hi6421_vout19, LDO19),
  102. HI6421_REGULATOR_OF_MATCH(hi6421_vout20, LDO20),
  103. HI6421_REGULATOR_OF_MATCH(hi6421_vout_audio, LDOAUDIO),
  104. HI6421_REGULATOR_OF_MATCH(hi6421_buck0, BUCK0),
  105. HI6421_REGULATOR_OF_MATCH(hi6421_buck1, BUCK1),
  106. HI6421_REGULATOR_OF_MATCH(hi6421_buck2, BUCK2),
  107. HI6421_REGULATOR_OF_MATCH(hi6421_buck3, BUCK3),
  108. HI6421_REGULATOR_OF_MATCH(hi6421_buck4, BUCK4),
  109. HI6421_REGULATOR_OF_MATCH(hi6421_buck5, BUCK5),
  110. };
  111. /* LDO 0, 4~7, 9~14, 16~20 have same voltage table. */
  112. static const unsigned int ldo_0_voltages[] = {
  113. 1500000, 1800000, 2400000, 2500000,
  114. 2600000, 2700000, 2850000, 3000000,
  115. };
  116. /* LDO 8, 15 have same voltage table. */
  117. static const unsigned int ldo_8_voltages[] = {
  118. 1500000, 1800000, 2400000, 2600000,
  119. 2700000, 2850000, 3000000, 3300000,
  120. };
  121. /* Ranges are sorted in ascending order. */
  122. static const struct regulator_linear_range ldo_audio_volt_range[] = {
  123. REGULATOR_LINEAR_RANGE(2800000, 0, 3, 50000),
  124. REGULATOR_LINEAR_RANGE(3000000, 4, 7, 100000),
  125. };
  126. static const unsigned int buck_3_voltages[] = {
  127. 950000, 1050000, 1100000, 1117000,
  128. 1134000, 1150000, 1167000, 1200000,
  129. };
  130. static const unsigned int buck_4_voltages[] = {
  131. 1150000, 1200000, 1250000, 1350000,
  132. 1700000, 1800000, 1900000, 2000000,
  133. };
  134. static const unsigned int buck_5_voltages[] = {
  135. 1150000, 1200000, 1250000, 1350000,
  136. 1600000, 1700000, 1800000, 1900000,
  137. };
  138. static const struct regulator_ops hi6421_ldo_ops;
  139. static const struct regulator_ops hi6421_ldo_linear_ops;
  140. static const struct regulator_ops hi6421_ldo_linear_range_ops;
  141. static const struct regulator_ops hi6421_buck012_ops;
  142. static const struct regulator_ops hi6421_buck345_ops;
  143. #define HI6421_LDO_ENABLE_TIME (350)
  144. /*
  145. * _id - LDO id name string
  146. * v_table - voltage table
  147. * vreg - voltage select register
  148. * vmask - voltage select mask
  149. * ereg - enable register
  150. * emask - enable mask
  151. * odelay - off/on delay time in uS
  152. * ecomask - eco mode mask
  153. * ecoamp - eco mode load uppler limit in uA
  154. */
  155. #define HI6421_LDO(_id, v_table, vreg, vmask, ereg, emask, \
  156. odelay, ecomask, ecoamp) \
  157. [HI6421_##_id] = { \
  158. .desc = { \
  159. .name = #_id, \
  160. .ops = &hi6421_ldo_ops, \
  161. .type = REGULATOR_VOLTAGE, \
  162. .id = HI6421_##_id, \
  163. .owner = THIS_MODULE, \
  164. .n_voltages = ARRAY_SIZE(v_table), \
  165. .volt_table = v_table, \
  166. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  167. .vsel_mask = vmask, \
  168. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  169. .enable_mask = emask, \
  170. .enable_time = HI6421_LDO_ENABLE_TIME, \
  171. .off_on_delay = odelay, \
  172. }, \
  173. .mode_mask = ecomask, \
  174. .eco_microamp = ecoamp, \
  175. }
  176. /* HI6421 LDO1~3 are linear voltage regulators at fixed uV_step
  177. *
  178. * _id - LDO id name string
  179. * _min_uV - minimum voltage supported in uV
  180. * n_volt - number of votages available
  181. * vstep - voltage increase in each linear step in uV
  182. * vreg - voltage select register
  183. * vmask - voltage select mask
  184. * ereg - enable register
  185. * emask - enable mask
  186. * odelay - off/on delay time in uS
  187. * ecomask - eco mode mask
  188. * ecoamp - eco mode load uppler limit in uA
  189. */
  190. #define HI6421_LDO_LINEAR(_id, _min_uV, n_volt, vstep, vreg, vmask, \
  191. ereg, emask, odelay, ecomask, ecoamp) \
  192. [HI6421_##_id] = { \
  193. .desc = { \
  194. .name = #_id, \
  195. .ops = &hi6421_ldo_linear_ops, \
  196. .type = REGULATOR_VOLTAGE, \
  197. .id = HI6421_##_id, \
  198. .owner = THIS_MODULE, \
  199. .min_uV = _min_uV, \
  200. .n_voltages = n_volt, \
  201. .uV_step = vstep, \
  202. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  203. .vsel_mask = vmask, \
  204. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  205. .enable_mask = emask, \
  206. .enable_time = HI6421_LDO_ENABLE_TIME, \
  207. .off_on_delay = odelay, \
  208. }, \
  209. .mode_mask = ecomask, \
  210. .eco_microamp = ecoamp, \
  211. }
  212. /* HI6421 LDOAUDIO is a linear voltage regulator with two 4-step ranges
  213. *
  214. * _id - LDO id name string
  215. * n_volt - number of votages available
  216. * volt_ranges - array of regulator_linear_range
  217. * vstep - voltage increase in each linear step in uV
  218. * vreg - voltage select register
  219. * vmask - voltage select mask
  220. * ereg - enable register
  221. * emask - enable mask
  222. * odelay - off/on delay time in uS
  223. * ecomask - eco mode mask
  224. * ecoamp - eco mode load uppler limit in uA
  225. */
  226. #define HI6421_LDO_LINEAR_RANGE(_id, n_volt, volt_ranges, vreg, vmask, \
  227. ereg, emask, odelay, ecomask, ecoamp) \
  228. [HI6421_##_id] = { \
  229. .desc = { \
  230. .name = #_id, \
  231. .ops = &hi6421_ldo_linear_range_ops, \
  232. .type = REGULATOR_VOLTAGE, \
  233. .id = HI6421_##_id, \
  234. .owner = THIS_MODULE, \
  235. .n_voltages = n_volt, \
  236. .linear_ranges = volt_ranges, \
  237. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  238. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  239. .vsel_mask = vmask, \
  240. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  241. .enable_mask = emask, \
  242. .enable_time = HI6421_LDO_ENABLE_TIME, \
  243. .off_on_delay = odelay, \
  244. }, \
  245. .mode_mask = ecomask, \
  246. .eco_microamp = ecoamp, \
  247. }
  248. /* HI6421 BUCK0/1/2 are linear voltage regulators at fixed uV_step
  249. *
  250. * _id - BUCK0/1/2 id name string
  251. * vreg - voltage select register
  252. * vmask - voltage select mask
  253. * ereg - enable register
  254. * emask - enable mask
  255. * sleepmask - mask of sleep mode
  256. * etime - enable time
  257. * odelay - off/on delay time in uS
  258. */
  259. #define HI6421_BUCK012(_id, vreg, vmask, ereg, emask, sleepmask, \
  260. etime, odelay) \
  261. [HI6421_##_id] = { \
  262. .desc = { \
  263. .name = #_id, \
  264. .ops = &hi6421_buck012_ops, \
  265. .type = REGULATOR_VOLTAGE, \
  266. .id = HI6421_##_id, \
  267. .owner = THIS_MODULE, \
  268. .min_uV = 700000, \
  269. .n_voltages = 128, \
  270. .uV_step = 7086, \
  271. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  272. .vsel_mask = vmask, \
  273. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  274. .enable_mask = emask, \
  275. .enable_time = etime, \
  276. .off_on_delay = odelay, \
  277. }, \
  278. .mode_mask = sleepmask, \
  279. }
  280. /* HI6421 BUCK3/4/5 share similar configurations as LDOs, with exception
  281. * that it supports SLEEP mode, so has different .ops.
  282. *
  283. * _id - LDO id name string
  284. * v_table - voltage table
  285. * vreg - voltage select register
  286. * vmask - voltage select mask
  287. * ereg - enable register
  288. * emask - enable mask
  289. * odelay - off/on delay time in uS
  290. * sleepmask - mask of sleep mode
  291. */
  292. #define HI6421_BUCK345(_id, v_table, vreg, vmask, ereg, emask, \
  293. odelay, sleepmask) \
  294. [HI6421_##_id] = { \
  295. .desc = { \
  296. .name = #_id, \
  297. .ops = &hi6421_buck345_ops, \
  298. .type = REGULATOR_VOLTAGE, \
  299. .id = HI6421_##_id, \
  300. .owner = THIS_MODULE, \
  301. .n_voltages = ARRAY_SIZE(v_table), \
  302. .volt_table = v_table, \
  303. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  304. .vsel_mask = vmask, \
  305. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  306. .enable_mask = emask, \
  307. .enable_time = HI6421_LDO_ENABLE_TIME, \
  308. .off_on_delay = odelay, \
  309. }, \
  310. .mode_mask = sleepmask, \
  311. }
  312. /* HI6421 regulator information */
  313. static struct hi6421_regulator_info
  314. hi6421_regulator_info[HI6421_NUM_REGULATORS] = {
  315. HI6421_LDO(LDO0, ldo_0_voltages, 0x20, 0x07, 0x20, 0x10,
  316. 10000, 0x20, 8000),
  317. HI6421_LDO_LINEAR(LDO1, 1700000, 4, 100000, 0x21, 0x03, 0x21, 0x10,
  318. 10000, 0x20, 5000),
  319. HI6421_LDO_LINEAR(LDO2, 1050000, 8, 50000, 0x22, 0x07, 0x22, 0x10,
  320. 20000, 0x20, 8000),
  321. HI6421_LDO_LINEAR(LDO3, 1050000, 8, 50000, 0x23, 0x07, 0x23, 0x10,
  322. 20000, 0x20, 8000),
  323. HI6421_LDO(LDO4, ldo_0_voltages, 0x24, 0x07, 0x24, 0x10,
  324. 20000, 0x20, 8000),
  325. HI6421_LDO(LDO5, ldo_0_voltages, 0x25, 0x07, 0x25, 0x10,
  326. 20000, 0x20, 8000),
  327. HI6421_LDO(LDO6, ldo_0_voltages, 0x26, 0x07, 0x26, 0x10,
  328. 20000, 0x20, 8000),
  329. HI6421_LDO(LDO7, ldo_0_voltages, 0x27, 0x07, 0x27, 0x10,
  330. 20000, 0x20, 5000),
  331. HI6421_LDO(LDO8, ldo_8_voltages, 0x28, 0x07, 0x28, 0x10,
  332. 20000, 0x20, 8000),
  333. HI6421_LDO(LDO9, ldo_0_voltages, 0x29, 0x07, 0x29, 0x10,
  334. 40000, 0x20, 8000),
  335. HI6421_LDO(LDO10, ldo_0_voltages, 0x2a, 0x07, 0x2a, 0x10,
  336. 40000, 0x20, 8000),
  337. HI6421_LDO(LDO11, ldo_0_voltages, 0x2b, 0x07, 0x2b, 0x10,
  338. 40000, 0x20, 8000),
  339. HI6421_LDO(LDO12, ldo_0_voltages, 0x2c, 0x07, 0x2c, 0x10,
  340. 40000, 0x20, 8000),
  341. HI6421_LDO(LDO13, ldo_0_voltages, 0x2d, 0x07, 0x2d, 0x10,
  342. 40000, 0x20, 8000),
  343. HI6421_LDO(LDO14, ldo_0_voltages, 0x2e, 0x07, 0x2e, 0x10,
  344. 40000, 0x20, 8000),
  345. HI6421_LDO(LDO15, ldo_8_voltages, 0x2f, 0x07, 0x2f, 0x10,
  346. 40000, 0x20, 8000),
  347. HI6421_LDO(LDO16, ldo_0_voltages, 0x30, 0x07, 0x30, 0x10,
  348. 40000, 0x20, 8000),
  349. HI6421_LDO(LDO17, ldo_0_voltages, 0x31, 0x07, 0x31, 0x10,
  350. 40000, 0x20, 8000),
  351. HI6421_LDO(LDO18, ldo_0_voltages, 0x32, 0x07, 0x32, 0x10,
  352. 40000, 0x20, 8000),
  353. HI6421_LDO(LDO19, ldo_0_voltages, 0x33, 0x07, 0x33, 0x10,
  354. 40000, 0x20, 8000),
  355. HI6421_LDO(LDO20, ldo_0_voltages, 0x34, 0x07, 0x34, 0x10,
  356. 40000, 0x20, 8000),
  357. HI6421_LDO_LINEAR_RANGE(LDOAUDIO, 8, ldo_audio_volt_range, 0x36,
  358. 0x70, 0x36, 0x01, 40000, 0x02, 5000),
  359. HI6421_BUCK012(BUCK0, 0x0d, 0x7f, 0x0c, 0x01, 0x10, 400, 20000),
  360. HI6421_BUCK012(BUCK1, 0x0f, 0x7f, 0x0e, 0x01, 0x10, 400, 20000),
  361. HI6421_BUCK012(BUCK2, 0x11, 0x7f, 0x10, 0x01, 0x10, 350, 100),
  362. HI6421_BUCK345(BUCK3, buck_3_voltages, 0x13, 0x07, 0x12, 0x01,
  363. 20000, 0x10),
  364. HI6421_BUCK345(BUCK4, buck_4_voltages, 0x15, 0x07, 0x14, 0x01,
  365. 20000, 0x10),
  366. HI6421_BUCK345(BUCK5, buck_5_voltages, 0x17, 0x07, 0x16, 0x01,
  367. 20000, 0x10),
  368. };
  369. static int hi6421_regulator_enable(struct regulator_dev *rdev)
  370. {
  371. struct hi6421_regulator_pdata *pdata;
  372. pdata = dev_get_drvdata(rdev->dev.parent);
  373. /* hi6421 spec requires regulator enablement must be serialized:
  374. * - Because when BUCK, LDO switching from off to on, it will have
  375. * a huge instantaneous current; so you can not turn on two or
  376. * more LDO or BUCKs simultaneously, or it may burn the chip.
  377. */
  378. mutex_lock(&pdata->lock);
  379. /* call regulator regmap helper */
  380. regulator_enable_regmap(rdev);
  381. mutex_unlock(&pdata->lock);
  382. return 0;
  383. }
  384. static unsigned int hi6421_regulator_ldo_get_mode(struct regulator_dev *rdev)
  385. {
  386. struct hi6421_regulator_info *info = rdev_get_drvdata(rdev);
  387. u32 reg_val;
  388. regmap_read(rdev->regmap, rdev->desc->enable_reg, &reg_val);
  389. if (reg_val & info->mode_mask)
  390. return REGULATOR_MODE_IDLE;
  391. return REGULATOR_MODE_NORMAL;
  392. }
  393. static unsigned int hi6421_regulator_buck_get_mode(struct regulator_dev *rdev)
  394. {
  395. struct hi6421_regulator_info *info = rdev_get_drvdata(rdev);
  396. u32 reg_val;
  397. regmap_read(rdev->regmap, rdev->desc->enable_reg, &reg_val);
  398. if (reg_val & info->mode_mask)
  399. return REGULATOR_MODE_STANDBY;
  400. return REGULATOR_MODE_NORMAL;
  401. }
  402. static int hi6421_regulator_ldo_set_mode(struct regulator_dev *rdev,
  403. unsigned int mode)
  404. {
  405. struct hi6421_regulator_info *info = rdev_get_drvdata(rdev);
  406. u32 new_mode;
  407. switch (mode) {
  408. case REGULATOR_MODE_NORMAL:
  409. new_mode = 0;
  410. break;
  411. case REGULATOR_MODE_IDLE:
  412. new_mode = info->mode_mask;
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. /* set mode */
  418. regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  419. info->mode_mask, new_mode);
  420. return 0;
  421. }
  422. static int hi6421_regulator_buck_set_mode(struct regulator_dev *rdev,
  423. unsigned int mode)
  424. {
  425. struct hi6421_regulator_info *info = rdev_get_drvdata(rdev);
  426. u32 new_mode;
  427. switch (mode) {
  428. case REGULATOR_MODE_NORMAL:
  429. new_mode = 0;
  430. break;
  431. case REGULATOR_MODE_STANDBY:
  432. new_mode = info->mode_mask;
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. /* set mode */
  438. regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  439. info->mode_mask, new_mode);
  440. return 0;
  441. }
  442. static unsigned int
  443. hi6421_regulator_ldo_get_optimum_mode(struct regulator_dev *rdev,
  444. int input_uV, int output_uV, int load_uA)
  445. {
  446. struct hi6421_regulator_info *info = rdev_get_drvdata(rdev);
  447. if (load_uA > info->eco_microamp)
  448. return REGULATOR_MODE_NORMAL;
  449. return REGULATOR_MODE_IDLE;
  450. }
  451. static const struct regulator_ops hi6421_ldo_ops = {
  452. .is_enabled = regulator_is_enabled_regmap,
  453. .enable = hi6421_regulator_enable,
  454. .disable = regulator_disable_regmap,
  455. .list_voltage = regulator_list_voltage_table,
  456. .map_voltage = regulator_map_voltage_ascend,
  457. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  458. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  459. .get_mode = hi6421_regulator_ldo_get_mode,
  460. .set_mode = hi6421_regulator_ldo_set_mode,
  461. .get_optimum_mode = hi6421_regulator_ldo_get_optimum_mode,
  462. };
  463. static const struct regulator_ops hi6421_ldo_linear_ops = {
  464. .is_enabled = regulator_is_enabled_regmap,
  465. .enable = hi6421_regulator_enable,
  466. .disable = regulator_disable_regmap,
  467. .list_voltage = regulator_list_voltage_linear,
  468. .map_voltage = regulator_map_voltage_linear,
  469. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  470. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  471. .get_mode = hi6421_regulator_ldo_get_mode,
  472. .set_mode = hi6421_regulator_ldo_set_mode,
  473. .get_optimum_mode = hi6421_regulator_ldo_get_optimum_mode,
  474. };
  475. static const struct regulator_ops hi6421_ldo_linear_range_ops = {
  476. .is_enabled = regulator_is_enabled_regmap,
  477. .enable = hi6421_regulator_enable,
  478. .disable = regulator_disable_regmap,
  479. .list_voltage = regulator_list_voltage_linear_range,
  480. .map_voltage = regulator_map_voltage_linear_range,
  481. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  482. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  483. .get_mode = hi6421_regulator_ldo_get_mode,
  484. .set_mode = hi6421_regulator_ldo_set_mode,
  485. .get_optimum_mode = hi6421_regulator_ldo_get_optimum_mode,
  486. };
  487. static const struct regulator_ops hi6421_buck012_ops = {
  488. .is_enabled = regulator_is_enabled_regmap,
  489. .enable = hi6421_regulator_enable,
  490. .disable = regulator_disable_regmap,
  491. .list_voltage = regulator_list_voltage_linear,
  492. .map_voltage = regulator_map_voltage_linear,
  493. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  494. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  495. .get_mode = hi6421_regulator_buck_get_mode,
  496. .set_mode = hi6421_regulator_buck_set_mode,
  497. };
  498. static const struct regulator_ops hi6421_buck345_ops = {
  499. .is_enabled = regulator_is_enabled_regmap,
  500. .enable = hi6421_regulator_enable,
  501. .disable = regulator_disable_regmap,
  502. .list_voltage = regulator_list_voltage_table,
  503. .map_voltage = regulator_map_voltage_ascend,
  504. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  505. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  506. .get_mode = hi6421_regulator_buck_get_mode,
  507. .set_mode = hi6421_regulator_buck_set_mode,
  508. };
  509. static int hi6421_regulator_register(struct platform_device *pdev,
  510. struct regmap *rmap,
  511. struct regulator_init_data *init_data,
  512. int id, struct device_node *np)
  513. {
  514. struct hi6421_regulator_info *info = NULL;
  515. struct regulator_config config = { };
  516. struct regulator_dev *rdev;
  517. /* assign per-regulator data */
  518. info = &hi6421_regulator_info[id];
  519. config.dev = &pdev->dev;
  520. config.init_data = init_data;
  521. config.driver_data = info;
  522. config.regmap = rmap;
  523. config.of_node = np;
  524. /* register regulator with framework */
  525. rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
  526. if (IS_ERR(rdev)) {
  527. dev_err(&pdev->dev, "failed to register regulator %s\n",
  528. info->desc.name);
  529. return PTR_ERR(rdev);
  530. }
  531. return 0;
  532. }
  533. static int hi6421_regulator_probe(struct platform_device *pdev)
  534. {
  535. struct device *dev = &pdev->dev;
  536. struct device_node *np;
  537. struct hi6421_pmic *pmic;
  538. struct hi6421_regulator_pdata *pdata;
  539. int i, ret = 0;
  540. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  541. if (!pdata)
  542. return -ENOMEM;
  543. mutex_init(&pdata->lock);
  544. platform_set_drvdata(pdev, pdata);
  545. np = of_get_child_by_name(dev->parent->of_node, "regulators");
  546. if (!np)
  547. return -ENODEV;
  548. ret = of_regulator_match(dev, np,
  549. hi6421_regulator_match,
  550. ARRAY_SIZE(hi6421_regulator_match));
  551. of_node_put(np);
  552. if (ret < 0) {
  553. dev_err(dev, "Error parsing regulator init data: %d\n", ret);
  554. return ret;
  555. }
  556. pmic = dev_get_drvdata(dev->parent);
  557. for (i = 0; i < ARRAY_SIZE(hi6421_regulator_info); i++) {
  558. ret = hi6421_regulator_register(pdev, pmic->regmap,
  559. hi6421_regulator_match[i].init_data, i,
  560. hi6421_regulator_match[i].of_node);
  561. if (ret)
  562. return ret;
  563. }
  564. return 0;
  565. }
  566. static struct platform_driver hi6421_regulator_driver = {
  567. .driver = {
  568. .name = "hi6421-regulator",
  569. },
  570. .probe = hi6421_regulator_probe,
  571. };
  572. module_platform_driver(hi6421_regulator_driver);
  573. MODULE_AUTHOR("Guodong Xu <guodong.xu@linaro.org>");
  574. MODULE_DESCRIPTION("Hi6421 regulator driver");
  575. MODULE_LICENSE("GPL v2");