mc13892-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Regulator Driver for Freescale MC13892 PMIC
  3. *
  4. * Copyright 2010 Yong Shen <yong.shen@linaro.org>
  5. *
  6. * Based on draft driver from Arnaud Patard <arnaud.patard@rtp-net.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/mfd/mc13892.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include "mc13xxx.h"
  22. #define MC13892_REVISION 7
  23. #define MC13892_POWERCTL0 13
  24. #define MC13892_POWERCTL0_USEROFFSPI 3
  25. #define MC13892_POWERCTL0_VCOINCELLVSEL 20
  26. #define MC13892_POWERCTL0_VCOINCELLVSEL_M (7<<20)
  27. #define MC13892_POWERCTL0_VCOINCELLEN (1<<23)
  28. #define MC13892_SWITCHERS0_SWxHI (1<<23)
  29. #define MC13892_SWITCHERS0 24
  30. #define MC13892_SWITCHERS0_SW1VSEL 0
  31. #define MC13892_SWITCHERS0_SW1VSEL_M (0x1f<<0)
  32. #define MC13892_SWITCHERS0_SW1HI (1<<23)
  33. #define MC13892_SWITCHERS0_SW1EN 0
  34. #define MC13892_SWITCHERS1 25
  35. #define MC13892_SWITCHERS1_SW2VSEL 0
  36. #define MC13892_SWITCHERS1_SW2VSEL_M (0x1f<<0)
  37. #define MC13892_SWITCHERS1_SW2HI (1<<23)
  38. #define MC13892_SWITCHERS1_SW2EN 0
  39. #define MC13892_SWITCHERS2 26
  40. #define MC13892_SWITCHERS2_SW3VSEL 0
  41. #define MC13892_SWITCHERS2_SW3VSEL_M (0x1f<<0)
  42. #define MC13892_SWITCHERS2_SW3HI (1<<23)
  43. #define MC13892_SWITCHERS2_SW3EN 0
  44. #define MC13892_SWITCHERS3 27
  45. #define MC13892_SWITCHERS3_SW4VSEL 0
  46. #define MC13892_SWITCHERS3_SW4VSEL_M (0x1f<<0)
  47. #define MC13892_SWITCHERS3_SW4HI (1<<23)
  48. #define MC13892_SWITCHERS3_SW4EN 0
  49. #define MC13892_SWITCHERS4 28
  50. #define MC13892_SWITCHERS4_SW1MODE 0
  51. #define MC13892_SWITCHERS4_SW1MODE_AUTO (8<<0)
  52. #define MC13892_SWITCHERS4_SW1MODE_M (0xf<<0)
  53. #define MC13892_SWITCHERS4_SW2MODE 10
  54. #define MC13892_SWITCHERS4_SW2MODE_AUTO (8<<10)
  55. #define MC13892_SWITCHERS4_SW2MODE_M (0xf<<10)
  56. #define MC13892_SWITCHERS5 29
  57. #define MC13892_SWITCHERS5_SW3MODE 0
  58. #define MC13892_SWITCHERS5_SW3MODE_AUTO (8<<0)
  59. #define MC13892_SWITCHERS5_SW3MODE_M (0xf<<0)
  60. #define MC13892_SWITCHERS5_SW4MODE 8
  61. #define MC13892_SWITCHERS5_SW4MODE_AUTO (8<<8)
  62. #define MC13892_SWITCHERS5_SW4MODE_M (0xf<<8)
  63. #define MC13892_SWITCHERS5_SWBSTEN (1<<20)
  64. #define MC13892_REGULATORSETTING0 30
  65. #define MC13892_REGULATORSETTING0_VGEN1VSEL 0
  66. #define MC13892_REGULATORSETTING0_VDIGVSEL 4
  67. #define MC13892_REGULATORSETTING0_VGEN2VSEL 6
  68. #define MC13892_REGULATORSETTING0_VPLLVSEL 9
  69. #define MC13892_REGULATORSETTING0_VUSB2VSEL 11
  70. #define MC13892_REGULATORSETTING0_VGEN3VSEL 14
  71. #define MC13892_REGULATORSETTING0_VCAMVSEL 16
  72. #define MC13892_REGULATORSETTING0_VGEN1VSEL_M (3<<0)
  73. #define MC13892_REGULATORSETTING0_VDIGVSEL_M (3<<4)
  74. #define MC13892_REGULATORSETTING0_VGEN2VSEL_M (7<<6)
  75. #define MC13892_REGULATORSETTING0_VPLLVSEL_M (3<<9)
  76. #define MC13892_REGULATORSETTING0_VUSB2VSEL_M (3<<11)
  77. #define MC13892_REGULATORSETTING0_VGEN3VSEL_M (1<<14)
  78. #define MC13892_REGULATORSETTING0_VCAMVSEL_M (3<<16)
  79. #define MC13892_REGULATORSETTING1 31
  80. #define MC13892_REGULATORSETTING1_VVIDEOVSEL 2
  81. #define MC13892_REGULATORSETTING1_VAUDIOVSEL 4
  82. #define MC13892_REGULATORSETTING1_VSDVSEL 6
  83. #define MC13892_REGULATORSETTING1_VVIDEOVSEL_M (3<<2)
  84. #define MC13892_REGULATORSETTING1_VAUDIOVSEL_M (3<<4)
  85. #define MC13892_REGULATORSETTING1_VSDVSEL_M (7<<6)
  86. #define MC13892_REGULATORMODE0 32
  87. #define MC13892_REGULATORMODE0_VGEN1EN (1<<0)
  88. #define MC13892_REGULATORMODE0_VGEN1STDBY (1<<1)
  89. #define MC13892_REGULATORMODE0_VGEN1MODE (1<<2)
  90. #define MC13892_REGULATORMODE0_VIOHIEN (1<<3)
  91. #define MC13892_REGULATORMODE0_VIOHISTDBY (1<<4)
  92. #define MC13892_REGULATORMODE0_VIOHIMODE (1<<5)
  93. #define MC13892_REGULATORMODE0_VDIGEN (1<<9)
  94. #define MC13892_REGULATORMODE0_VDIGSTDBY (1<<10)
  95. #define MC13892_REGULATORMODE0_VDIGMODE (1<<11)
  96. #define MC13892_REGULATORMODE0_VGEN2EN (1<<12)
  97. #define MC13892_REGULATORMODE0_VGEN2STDBY (1<<13)
  98. #define MC13892_REGULATORMODE0_VGEN2MODE (1<<14)
  99. #define MC13892_REGULATORMODE0_VPLLEN (1<<15)
  100. #define MC13892_REGULATORMODE0_VPLLSTDBY (1<<16)
  101. #define MC13892_REGULATORMODE0_VPLLMODE (1<<17)
  102. #define MC13892_REGULATORMODE0_VUSB2EN (1<<18)
  103. #define MC13892_REGULATORMODE0_VUSB2STDBY (1<<19)
  104. #define MC13892_REGULATORMODE0_VUSB2MODE (1<<20)
  105. #define MC13892_REGULATORMODE1 33
  106. #define MC13892_REGULATORMODE1_VGEN3EN (1<<0)
  107. #define MC13892_REGULATORMODE1_VGEN3STDBY (1<<1)
  108. #define MC13892_REGULATORMODE1_VGEN3MODE (1<<2)
  109. #define MC13892_REGULATORMODE1_VCAMEN (1<<6)
  110. #define MC13892_REGULATORMODE1_VCAMSTDBY (1<<7)
  111. #define MC13892_REGULATORMODE1_VCAMMODE (1<<8)
  112. #define MC13892_REGULATORMODE1_VCAMCONFIGEN (1<<9)
  113. #define MC13892_REGULATORMODE1_VVIDEOEN (1<<12)
  114. #define MC13892_REGULATORMODE1_VVIDEOSTDBY (1<<13)
  115. #define MC13892_REGULATORMODE1_VVIDEOMODE (1<<14)
  116. #define MC13892_REGULATORMODE1_VAUDIOEN (1<<15)
  117. #define MC13892_REGULATORMODE1_VAUDIOSTDBY (1<<16)
  118. #define MC13892_REGULATORMODE1_VAUDIOMODE (1<<17)
  119. #define MC13892_REGULATORMODE1_VSDEN (1<<18)
  120. #define MC13892_REGULATORMODE1_VSDSTDBY (1<<19)
  121. #define MC13892_REGULATORMODE1_VSDMODE (1<<20)
  122. #define MC13892_POWERMISC 34
  123. #define MC13892_POWERMISC_GPO1EN (1<<6)
  124. #define MC13892_POWERMISC_GPO2EN (1<<8)
  125. #define MC13892_POWERMISC_GPO3EN (1<<10)
  126. #define MC13892_POWERMISC_GPO4EN (1<<12)
  127. #define MC13892_POWERMISC_PWGT1SPIEN (1<<15)
  128. #define MC13892_POWERMISC_PWGT2SPIEN (1<<16)
  129. #define MC13892_POWERMISC_GPO4ADINEN (1<<21)
  130. #define MC13892_POWERMISC_PWGTSPI_M (3 << 15)
  131. #define MC13892_USB1 50
  132. #define MC13892_USB1_VUSBEN (1<<3)
  133. static const unsigned int mc13892_vcoincell[] = {
  134. 2500000, 2700000, 2800000, 2900000, 3000000, 3100000,
  135. 3200000, 3300000,
  136. };
  137. static const unsigned int mc13892_sw1[] = {
  138. 600000, 625000, 650000, 675000, 700000, 725000,
  139. 750000, 775000, 800000, 825000, 850000, 875000,
  140. 900000, 925000, 950000, 975000, 1000000, 1025000,
  141. 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  142. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
  143. 1350000, 1375000
  144. };
  145. /*
  146. * Note: this table is used to derive SWxVSEL by index into
  147. * the array. Offset the values by the index of 1100000uV
  148. * to get the actual register value for that voltage selector
  149. * if the HI bit is to be set as well.
  150. */
  151. #define MC13892_SWxHI_SEL_OFFSET 20
  152. static const unsigned int mc13892_sw[] = {
  153. 600000, 625000, 650000, 675000, 700000, 725000,
  154. 750000, 775000, 800000, 825000, 850000, 875000,
  155. 900000, 925000, 950000, 975000, 1000000, 1025000,
  156. 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  157. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
  158. 1350000, 1375000, 1400000, 1425000, 1450000, 1475000,
  159. 1500000, 1525000, 1550000, 1575000, 1600000, 1625000,
  160. 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
  161. 1800000, 1825000, 1850000, 1875000
  162. };
  163. static const unsigned int mc13892_swbst[] = {
  164. 5000000,
  165. };
  166. static const unsigned int mc13892_viohi[] = {
  167. 2775000,
  168. };
  169. static const unsigned int mc13892_vpll[] = {
  170. 1050000, 1250000, 1650000, 1800000,
  171. };
  172. static const unsigned int mc13892_vdig[] = {
  173. 1050000, 1250000, 1650000, 1800000,
  174. };
  175. static const unsigned int mc13892_vsd[] = {
  176. 1800000, 2000000, 2600000, 2700000,
  177. 2800000, 2900000, 3000000, 3150000,
  178. };
  179. static const unsigned int mc13892_vusb2[] = {
  180. 2400000, 2600000, 2700000, 2775000,
  181. };
  182. static const unsigned int mc13892_vvideo[] = {
  183. 2700000, 2775000, 2500000, 2600000,
  184. };
  185. static const unsigned int mc13892_vaudio[] = {
  186. 2300000, 2500000, 2775000, 3000000,
  187. };
  188. static const unsigned int mc13892_vcam[] = {
  189. 2500000, 2600000, 2750000, 3000000,
  190. };
  191. static const unsigned int mc13892_vgen1[] = {
  192. 1200000, 1500000, 2775000, 3150000,
  193. };
  194. static const unsigned int mc13892_vgen2[] = {
  195. 1200000, 1500000, 1600000, 1800000,
  196. 2700000, 2800000, 3000000, 3150000,
  197. };
  198. static const unsigned int mc13892_vgen3[] = {
  199. 1800000, 2900000,
  200. };
  201. static const unsigned int mc13892_vusb[] = {
  202. 3300000,
  203. };
  204. static const unsigned int mc13892_gpo[] = {
  205. 2750000,
  206. };
  207. static const unsigned int mc13892_pwgtdrv[] = {
  208. 5000000,
  209. };
  210. static struct regulator_ops mc13892_gpo_regulator_ops;
  211. static struct regulator_ops mc13892_sw_regulator_ops;
  212. #define MC13892_FIXED_DEFINE(name, reg, voltages) \
  213. MC13xxx_FIXED_DEFINE(MC13892_, name, reg, voltages, \
  214. mc13xxx_fixed_regulator_ops)
  215. #define MC13892_GPO_DEFINE(name, reg, voltages) \
  216. MC13xxx_GPO_DEFINE(MC13892_, name, reg, voltages, \
  217. mc13892_gpo_regulator_ops)
  218. #define MC13892_SW_DEFINE(name, reg, vsel_reg, voltages) \
  219. MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
  220. mc13892_sw_regulator_ops)
  221. #define MC13892_DEFINE_REGU(name, reg, vsel_reg, voltages) \
  222. MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
  223. mc13xxx_regulator_ops)
  224. static struct mc13xxx_regulator mc13892_regulators[] = {
  225. MC13892_DEFINE_REGU(VCOINCELL, POWERCTL0, POWERCTL0, mc13892_vcoincell),
  226. MC13892_SW_DEFINE(SW1, SWITCHERS0, SWITCHERS0, mc13892_sw1),
  227. MC13892_SW_DEFINE(SW2, SWITCHERS1, SWITCHERS1, mc13892_sw),
  228. MC13892_SW_DEFINE(SW3, SWITCHERS2, SWITCHERS2, mc13892_sw),
  229. MC13892_SW_DEFINE(SW4, SWITCHERS3, SWITCHERS3, mc13892_sw),
  230. MC13892_FIXED_DEFINE(SWBST, SWITCHERS5, mc13892_swbst),
  231. MC13892_FIXED_DEFINE(VIOHI, REGULATORMODE0, mc13892_viohi),
  232. MC13892_DEFINE_REGU(VPLL, REGULATORMODE0, REGULATORSETTING0,
  233. mc13892_vpll),
  234. MC13892_DEFINE_REGU(VDIG, REGULATORMODE0, REGULATORSETTING0,
  235. mc13892_vdig),
  236. MC13892_DEFINE_REGU(VSD, REGULATORMODE1, REGULATORSETTING1,
  237. mc13892_vsd),
  238. MC13892_DEFINE_REGU(VUSB2, REGULATORMODE0, REGULATORSETTING0,
  239. mc13892_vusb2),
  240. MC13892_DEFINE_REGU(VVIDEO, REGULATORMODE1, REGULATORSETTING1,
  241. mc13892_vvideo),
  242. MC13892_DEFINE_REGU(VAUDIO, REGULATORMODE1, REGULATORSETTING1,
  243. mc13892_vaudio),
  244. MC13892_DEFINE_REGU(VCAM, REGULATORMODE1, REGULATORSETTING0,
  245. mc13892_vcam),
  246. MC13892_DEFINE_REGU(VGEN1, REGULATORMODE0, REGULATORSETTING0,
  247. mc13892_vgen1),
  248. MC13892_DEFINE_REGU(VGEN2, REGULATORMODE0, REGULATORSETTING0,
  249. mc13892_vgen2),
  250. MC13892_DEFINE_REGU(VGEN3, REGULATORMODE1, REGULATORSETTING0,
  251. mc13892_vgen3),
  252. MC13892_FIXED_DEFINE(VUSB, USB1, mc13892_vusb),
  253. MC13892_GPO_DEFINE(GPO1, POWERMISC, mc13892_gpo),
  254. MC13892_GPO_DEFINE(GPO2, POWERMISC, mc13892_gpo),
  255. MC13892_GPO_DEFINE(GPO3, POWERMISC, mc13892_gpo),
  256. MC13892_GPO_DEFINE(GPO4, POWERMISC, mc13892_gpo),
  257. MC13892_GPO_DEFINE(PWGT1SPI, POWERMISC, mc13892_pwgtdrv),
  258. MC13892_GPO_DEFINE(PWGT2SPI, POWERMISC, mc13892_pwgtdrv),
  259. };
  260. static int mc13892_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
  261. u32 val)
  262. {
  263. struct mc13xxx *mc13892 = priv->mc13xxx;
  264. int ret;
  265. u32 valread;
  266. BUG_ON(val & ~mask);
  267. mc13xxx_lock(priv->mc13xxx);
  268. ret = mc13xxx_reg_read(mc13892, MC13892_POWERMISC, &valread);
  269. if (ret)
  270. goto out;
  271. /* Update the stored state for Power Gates. */
  272. priv->powermisc_pwgt_state =
  273. (priv->powermisc_pwgt_state & ~mask) | val;
  274. priv->powermisc_pwgt_state &= MC13892_POWERMISC_PWGTSPI_M;
  275. /* Construct the new register value */
  276. valread = (valread & ~mask) | val;
  277. /* Overwrite the PWGTxEN with the stored version */
  278. valread = (valread & ~MC13892_POWERMISC_PWGTSPI_M) |
  279. priv->powermisc_pwgt_state;
  280. ret = mc13xxx_reg_write(mc13892, MC13892_POWERMISC, valread);
  281. out:
  282. mc13xxx_unlock(priv->mc13xxx);
  283. return ret;
  284. }
  285. static int mc13892_gpo_regulator_enable(struct regulator_dev *rdev)
  286. {
  287. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  288. int id = rdev_get_id(rdev);
  289. u32 en_val = mc13892_regulators[id].enable_bit;
  290. u32 mask = mc13892_regulators[id].enable_bit;
  291. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  292. /* Power Gate enable value is 0 */
  293. if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
  294. en_val = 0;
  295. if (id == MC13892_GPO4)
  296. mask |= MC13892_POWERMISC_GPO4ADINEN;
  297. return mc13892_powermisc_rmw(priv, mask, en_val);
  298. }
  299. static int mc13892_gpo_regulator_disable(struct regulator_dev *rdev)
  300. {
  301. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  302. int id = rdev_get_id(rdev);
  303. u32 dis_val = 0;
  304. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  305. /* Power Gate disable value is 1 */
  306. if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
  307. dis_val = mc13892_regulators[id].enable_bit;
  308. return mc13892_powermisc_rmw(priv, mc13892_regulators[id].enable_bit,
  309. dis_val);
  310. }
  311. static int mc13892_gpo_regulator_is_enabled(struct regulator_dev *rdev)
  312. {
  313. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  314. int ret, id = rdev_get_id(rdev);
  315. unsigned int val;
  316. mc13xxx_lock(priv->mc13xxx);
  317. ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
  318. mc13xxx_unlock(priv->mc13xxx);
  319. if (ret)
  320. return ret;
  321. /* Power Gates state is stored in powermisc_pwgt_state
  322. * where the meaning of bits is negated */
  323. val = (val & ~MC13892_POWERMISC_PWGTSPI_M) |
  324. (priv->powermisc_pwgt_state ^ MC13892_POWERMISC_PWGTSPI_M);
  325. return (val & mc13892_regulators[id].enable_bit) != 0;
  326. }
  327. static struct regulator_ops mc13892_gpo_regulator_ops = {
  328. .enable = mc13892_gpo_regulator_enable,
  329. .disable = mc13892_gpo_regulator_disable,
  330. .is_enabled = mc13892_gpo_regulator_is_enabled,
  331. .list_voltage = regulator_list_voltage_table,
  332. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  333. };
  334. static int mc13892_sw_regulator_get_voltage_sel(struct regulator_dev *rdev)
  335. {
  336. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  337. int ret, id = rdev_get_id(rdev);
  338. unsigned int val, selector;
  339. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  340. mc13xxx_lock(priv->mc13xxx);
  341. ret = mc13xxx_reg_read(priv->mc13xxx,
  342. mc13892_regulators[id].vsel_reg, &val);
  343. mc13xxx_unlock(priv->mc13xxx);
  344. if (ret)
  345. return ret;
  346. /*
  347. * Figure out if the HI bit is set inside the switcher mode register
  348. * since this means the selector value we return is at a different
  349. * offset into the selector table.
  350. *
  351. * According to the MC13892 documentation note 59 (Table 47) the SW1
  352. * buck switcher does not support output range programming therefore
  353. * the HI bit must always remain 0. So do not do anything strange if
  354. * our register is MC13892_SWITCHERS0.
  355. */
  356. selector = val & mc13892_regulators[id].vsel_mask;
  357. if ((mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) &&
  358. (val & MC13892_SWITCHERS0_SWxHI)) {
  359. selector += MC13892_SWxHI_SEL_OFFSET;
  360. }
  361. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: 0x%08x selector: %d\n",
  362. __func__, id, val, selector);
  363. return selector;
  364. }
  365. static int mc13892_sw_regulator_set_voltage_sel(struct regulator_dev *rdev,
  366. unsigned selector)
  367. {
  368. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  369. int volt, mask, id = rdev_get_id(rdev);
  370. u32 reg_value;
  371. int ret;
  372. volt = rdev->desc->volt_table[selector];
  373. mask = mc13892_regulators[id].vsel_mask;
  374. reg_value = selector;
  375. /*
  376. * Don't mess with the HI bit or support HI voltage offsets for SW1.
  377. *
  378. * Since the get_voltage_sel callback has given a fudged value for
  379. * the selector offset, we need to back out that offset if HI is
  380. * to be set so we write the correct value to the register.
  381. *
  382. * The HI bit addition and selector offset handling COULD be more
  383. * complicated by shifting and masking off the voltage selector part
  384. * of the register then logical OR it back in, but since the selector
  385. * is at bits 4:0 there is very little point. This makes the whole
  386. * thing more readable and we do far less work.
  387. */
  388. if (mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) {
  389. mask |= MC13892_SWITCHERS0_SWxHI;
  390. if (volt > 1375000) {
  391. reg_value -= MC13892_SWxHI_SEL_OFFSET;
  392. reg_value |= MC13892_SWITCHERS0_SWxHI;
  393. } else {
  394. reg_value &= ~MC13892_SWITCHERS0_SWxHI;
  395. }
  396. }
  397. mc13xxx_lock(priv->mc13xxx);
  398. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].vsel_reg,
  399. mask, reg_value);
  400. mc13xxx_unlock(priv->mc13xxx);
  401. return ret;
  402. }
  403. static struct regulator_ops mc13892_sw_regulator_ops = {
  404. .list_voltage = regulator_list_voltage_table,
  405. .map_voltage = regulator_map_voltage_ascend,
  406. .set_voltage_sel = mc13892_sw_regulator_set_voltage_sel,
  407. .get_voltage_sel = mc13892_sw_regulator_get_voltage_sel,
  408. };
  409. static int mc13892_vcam_set_mode(struct regulator_dev *rdev, unsigned int mode)
  410. {
  411. unsigned int en_val = 0;
  412. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  413. int ret, id = rdev_get_id(rdev);
  414. if (mode == REGULATOR_MODE_FAST)
  415. en_val = MC13892_REGULATORMODE1_VCAMCONFIGEN;
  416. mc13xxx_lock(priv->mc13xxx);
  417. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].reg,
  418. MC13892_REGULATORMODE1_VCAMCONFIGEN, en_val);
  419. mc13xxx_unlock(priv->mc13xxx);
  420. return ret;
  421. }
  422. static unsigned int mc13892_vcam_get_mode(struct regulator_dev *rdev)
  423. {
  424. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  425. int ret, id = rdev_get_id(rdev);
  426. unsigned int val;
  427. mc13xxx_lock(priv->mc13xxx);
  428. ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
  429. mc13xxx_unlock(priv->mc13xxx);
  430. if (ret)
  431. return ret;
  432. if (val & MC13892_REGULATORMODE1_VCAMCONFIGEN)
  433. return REGULATOR_MODE_FAST;
  434. return REGULATOR_MODE_NORMAL;
  435. }
  436. static struct regulator_ops mc13892_vcam_ops;
  437. static int mc13892_regulator_probe(struct platform_device *pdev)
  438. {
  439. struct mc13xxx_regulator_priv *priv;
  440. struct mc13xxx *mc13892 = dev_get_drvdata(pdev->dev.parent);
  441. struct mc13xxx_regulator_platform_data *pdata =
  442. dev_get_platdata(&pdev->dev);
  443. struct mc13xxx_regulator_init_data *mc13xxx_data;
  444. struct regulator_config config = { };
  445. int i, ret;
  446. int num_regulators = 0;
  447. u32 val;
  448. num_regulators = mc13xxx_get_num_regulators_dt(pdev);
  449. if (num_regulators <= 0 && pdata)
  450. num_regulators = pdata->num_regulators;
  451. if (num_regulators <= 0)
  452. return -EINVAL;
  453. priv = devm_kzalloc(&pdev->dev, sizeof(*priv) +
  454. num_regulators * sizeof(priv->regulators[0]),
  455. GFP_KERNEL);
  456. if (!priv)
  457. return -ENOMEM;
  458. priv->num_regulators = num_regulators;
  459. priv->mc13xxx_regulators = mc13892_regulators;
  460. priv->mc13xxx = mc13892;
  461. platform_set_drvdata(pdev, priv);
  462. mc13xxx_lock(mc13892);
  463. ret = mc13xxx_reg_read(mc13892, MC13892_REVISION, &val);
  464. if (ret)
  465. goto err_unlock;
  466. /* enable switch auto mode (on 2.0A silicon only) */
  467. if ((val & 0x0000FFFF) == 0x45d0) {
  468. ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS4,
  469. MC13892_SWITCHERS4_SW1MODE_M |
  470. MC13892_SWITCHERS4_SW2MODE_M,
  471. MC13892_SWITCHERS4_SW1MODE_AUTO |
  472. MC13892_SWITCHERS4_SW2MODE_AUTO);
  473. if (ret)
  474. goto err_unlock;
  475. ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS5,
  476. MC13892_SWITCHERS5_SW3MODE_M |
  477. MC13892_SWITCHERS5_SW4MODE_M,
  478. MC13892_SWITCHERS5_SW3MODE_AUTO |
  479. MC13892_SWITCHERS5_SW4MODE_AUTO);
  480. if (ret)
  481. goto err_unlock;
  482. }
  483. mc13xxx_unlock(mc13892);
  484. /* update mc13892_vcam ops */
  485. memcpy(&mc13892_vcam_ops, mc13892_regulators[MC13892_VCAM].desc.ops,
  486. sizeof(struct regulator_ops));
  487. mc13892_vcam_ops.set_mode = mc13892_vcam_set_mode,
  488. mc13892_vcam_ops.get_mode = mc13892_vcam_get_mode,
  489. mc13892_regulators[MC13892_VCAM].desc.ops = &mc13892_vcam_ops;
  490. mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13892_regulators,
  491. ARRAY_SIZE(mc13892_regulators));
  492. for (i = 0; i < priv->num_regulators; i++) {
  493. struct regulator_init_data *init_data;
  494. struct regulator_desc *desc;
  495. struct device_node *node = NULL;
  496. int id;
  497. if (mc13xxx_data) {
  498. id = mc13xxx_data[i].id;
  499. init_data = mc13xxx_data[i].init_data;
  500. node = mc13xxx_data[i].node;
  501. } else {
  502. id = pdata->regulators[i].id;
  503. init_data = pdata->regulators[i].init_data;
  504. }
  505. desc = &mc13892_regulators[id].desc;
  506. config.dev = &pdev->dev;
  507. config.init_data = init_data;
  508. config.driver_data = priv;
  509. config.of_node = node;
  510. priv->regulators[i] = devm_regulator_register(&pdev->dev, desc,
  511. &config);
  512. if (IS_ERR(priv->regulators[i])) {
  513. dev_err(&pdev->dev, "failed to register regulator %s\n",
  514. mc13892_regulators[i].desc.name);
  515. return PTR_ERR(priv->regulators[i]);
  516. }
  517. }
  518. return 0;
  519. err_unlock:
  520. mc13xxx_unlock(mc13892);
  521. return ret;
  522. }
  523. static struct platform_driver mc13892_regulator_driver = {
  524. .driver = {
  525. .name = "mc13892-regulator",
  526. },
  527. .probe = mc13892_regulator_probe,
  528. };
  529. static int __init mc13892_regulator_init(void)
  530. {
  531. return platform_driver_register(&mc13892_regulator_driver);
  532. }
  533. subsys_initcall(mc13892_regulator_init);
  534. static void __exit mc13892_regulator_exit(void)
  535. {
  536. platform_driver_unregister(&mc13892_regulator_driver);
  537. }
  538. module_exit(mc13892_regulator_exit);
  539. MODULE_LICENSE("GPL v2");
  540. MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
  541. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13892 PMIC");
  542. MODULE_ALIAS("platform:mc13892-regulator");