mc13892-regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 int mc13892_vcoincell[] = {
  134. 2500000, 2700000, 2800000, 2900000, 3000000, 3100000,
  135. 3200000, 3300000,
  136. };
  137. static const 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. static const int mc13892_sw[] = {
  146. 600000, 625000, 650000, 675000, 700000, 725000,
  147. 750000, 775000, 800000, 825000, 850000, 875000,
  148. 900000, 925000, 950000, 975000, 1000000, 1025000,
  149. 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
  150. 1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
  151. 1350000, 1375000, 1400000, 1425000, 1450000, 1475000,
  152. 1500000, 1525000, 1550000, 1575000, 1600000, 1625000,
  153. 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
  154. 1800000, 1825000, 1850000, 1875000
  155. };
  156. static const int mc13892_swbst[] = {
  157. 5000000,
  158. };
  159. static const int mc13892_viohi[] = {
  160. 2775000,
  161. };
  162. static const int mc13892_vpll[] = {
  163. 1050000, 1250000, 1650000, 1800000,
  164. };
  165. static const int mc13892_vdig[] = {
  166. 1050000, 1250000, 1650000, 1800000,
  167. };
  168. static const int mc13892_vsd[] = {
  169. 1800000, 2000000, 2600000, 2700000,
  170. 2800000, 2900000, 3000000, 3150000,
  171. };
  172. static const int mc13892_vusb2[] = {
  173. 2400000, 2600000, 2700000, 2775000,
  174. };
  175. static const int mc13892_vvideo[] = {
  176. 2700000, 2775000, 2500000, 2600000,
  177. };
  178. static const int mc13892_vaudio[] = {
  179. 2300000, 2500000, 2775000, 3000000,
  180. };
  181. static const int mc13892_vcam[] = {
  182. 2500000, 2600000, 2750000, 3000000,
  183. };
  184. static const int mc13892_vgen1[] = {
  185. 1200000, 1500000, 2775000, 3150000,
  186. };
  187. static const int mc13892_vgen2[] = {
  188. 1200000, 1500000, 1600000, 1800000,
  189. 2700000, 2800000, 3000000, 3150000,
  190. };
  191. static const int mc13892_vgen3[] = {
  192. 1800000, 2900000,
  193. };
  194. static const int mc13892_vusb[] = {
  195. 3300000,
  196. };
  197. static const int mc13892_gpo[] = {
  198. 2750000,
  199. };
  200. static const int mc13892_pwgtdrv[] = {
  201. 5000000,
  202. };
  203. static struct regulator_ops mc13892_gpo_regulator_ops;
  204. /* sw regulators need special care due to the "hi bit" */
  205. static struct regulator_ops mc13892_sw_regulator_ops;
  206. #define MC13892_FIXED_DEFINE(name, reg, voltages) \
  207. MC13xxx_FIXED_DEFINE(MC13892_, name, reg, voltages, \
  208. mc13xxx_fixed_regulator_ops)
  209. #define MC13892_GPO_DEFINE(name, reg, voltages) \
  210. MC13xxx_GPO_DEFINE(MC13892_, name, reg, voltages, \
  211. mc13892_gpo_regulator_ops)
  212. #define MC13892_SW_DEFINE(name, reg, vsel_reg, voltages) \
  213. MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
  214. mc13892_sw_regulator_ops)
  215. #define MC13892_DEFINE_REGU(name, reg, vsel_reg, voltages) \
  216. MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
  217. mc13xxx_regulator_ops)
  218. static struct mc13xxx_regulator mc13892_regulators[] = {
  219. MC13892_DEFINE_REGU(VCOINCELL, POWERCTL0, POWERCTL0, mc13892_vcoincell),
  220. MC13892_SW_DEFINE(SW1, SWITCHERS0, SWITCHERS0, mc13892_sw1),
  221. MC13892_SW_DEFINE(SW2, SWITCHERS1, SWITCHERS1, mc13892_sw),
  222. MC13892_SW_DEFINE(SW3, SWITCHERS2, SWITCHERS2, mc13892_sw),
  223. MC13892_SW_DEFINE(SW4, SWITCHERS3, SWITCHERS3, mc13892_sw),
  224. MC13892_FIXED_DEFINE(SWBST, SWITCHERS5, mc13892_swbst),
  225. MC13892_FIXED_DEFINE(VIOHI, REGULATORMODE0, mc13892_viohi),
  226. MC13892_DEFINE_REGU(VPLL, REGULATORMODE0, REGULATORSETTING0, \
  227. mc13892_vpll),
  228. MC13892_DEFINE_REGU(VDIG, REGULATORMODE0, REGULATORSETTING0, \
  229. mc13892_vdig),
  230. MC13892_DEFINE_REGU(VSD, REGULATORMODE1, REGULATORSETTING1, \
  231. mc13892_vsd),
  232. MC13892_DEFINE_REGU(VUSB2, REGULATORMODE0, REGULATORSETTING0, \
  233. mc13892_vusb2),
  234. MC13892_DEFINE_REGU(VVIDEO, REGULATORMODE1, REGULATORSETTING1, \
  235. mc13892_vvideo),
  236. MC13892_DEFINE_REGU(VAUDIO, REGULATORMODE1, REGULATORSETTING1, \
  237. mc13892_vaudio),
  238. MC13892_DEFINE_REGU(VCAM, REGULATORMODE1, REGULATORSETTING0, \
  239. mc13892_vcam),
  240. MC13892_DEFINE_REGU(VGEN1, REGULATORMODE0, REGULATORSETTING0, \
  241. mc13892_vgen1),
  242. MC13892_DEFINE_REGU(VGEN2, REGULATORMODE0, REGULATORSETTING0, \
  243. mc13892_vgen2),
  244. MC13892_DEFINE_REGU(VGEN3, REGULATORMODE1, REGULATORSETTING0, \
  245. mc13892_vgen3),
  246. MC13892_FIXED_DEFINE(VUSB, USB1, mc13892_vusb),
  247. MC13892_GPO_DEFINE(GPO1, POWERMISC, mc13892_gpo),
  248. MC13892_GPO_DEFINE(GPO2, POWERMISC, mc13892_gpo),
  249. MC13892_GPO_DEFINE(GPO3, POWERMISC, mc13892_gpo),
  250. MC13892_GPO_DEFINE(GPO4, POWERMISC, mc13892_gpo),
  251. MC13892_GPO_DEFINE(PWGT1SPI, POWERMISC, mc13892_pwgtdrv),
  252. MC13892_GPO_DEFINE(PWGT2SPI, POWERMISC, mc13892_pwgtdrv),
  253. };
  254. static int mc13892_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
  255. u32 val)
  256. {
  257. struct mc13xxx *mc13892 = priv->mc13xxx;
  258. int ret;
  259. u32 valread;
  260. BUG_ON(val & ~mask);
  261. ret = mc13xxx_reg_read(mc13892, MC13892_POWERMISC, &valread);
  262. if (ret)
  263. return ret;
  264. /* Update the stored state for Power Gates. */
  265. priv->powermisc_pwgt_state =
  266. (priv->powermisc_pwgt_state & ~mask) | val;
  267. priv->powermisc_pwgt_state &= MC13892_POWERMISC_PWGTSPI_M;
  268. /* Construct the new register value */
  269. valread = (valread & ~mask) | val;
  270. /* Overwrite the PWGTxEN with the stored version */
  271. valread = (valread & ~MC13892_POWERMISC_PWGTSPI_M) |
  272. priv->powermisc_pwgt_state;
  273. return mc13xxx_reg_write(mc13892, MC13892_POWERMISC, valread);
  274. }
  275. static int mc13892_gpo_regulator_enable(struct regulator_dev *rdev)
  276. {
  277. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  278. int id = rdev_get_id(rdev);
  279. int ret;
  280. u32 en_val = mc13892_regulators[id].enable_bit;
  281. u32 mask = mc13892_regulators[id].enable_bit;
  282. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  283. /* Power Gate enable value is 0 */
  284. if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
  285. en_val = 0;
  286. if (id == MC13892_GPO4)
  287. mask |= MC13892_POWERMISC_GPO4ADINEN;
  288. mc13xxx_lock(priv->mc13xxx);
  289. ret = mc13892_powermisc_rmw(priv, mask, en_val);
  290. mc13xxx_unlock(priv->mc13xxx);
  291. return ret;
  292. }
  293. static int mc13892_gpo_regulator_disable(struct regulator_dev *rdev)
  294. {
  295. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  296. int id = rdev_get_id(rdev);
  297. int ret;
  298. u32 dis_val = 0;
  299. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  300. /* Power Gate disable value is 1 */
  301. if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
  302. dis_val = mc13892_regulators[id].enable_bit;
  303. mc13xxx_lock(priv->mc13xxx);
  304. ret = mc13892_powermisc_rmw(priv, mc13892_regulators[id].enable_bit,
  305. dis_val);
  306. mc13xxx_unlock(priv->mc13xxx);
  307. return ret;
  308. }
  309. static int mc13892_gpo_regulator_is_enabled(struct regulator_dev *rdev)
  310. {
  311. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  312. int ret, id = rdev_get_id(rdev);
  313. unsigned int val;
  314. mc13xxx_lock(priv->mc13xxx);
  315. ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
  316. mc13xxx_unlock(priv->mc13xxx);
  317. if (ret)
  318. return ret;
  319. /* Power Gates state is stored in powermisc_pwgt_state
  320. * where the meaning of bits is negated */
  321. val = (val & ~MC13892_POWERMISC_PWGTSPI_M) |
  322. (priv->powermisc_pwgt_state ^ MC13892_POWERMISC_PWGTSPI_M);
  323. return (val & mc13892_regulators[id].enable_bit) != 0;
  324. }
  325. static struct regulator_ops mc13892_gpo_regulator_ops = {
  326. .enable = mc13892_gpo_regulator_enable,
  327. .disable = mc13892_gpo_regulator_disable,
  328. .is_enabled = mc13892_gpo_regulator_is_enabled,
  329. .list_voltage = mc13xxx_regulator_list_voltage,
  330. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  331. .get_voltage = mc13xxx_fixed_regulator_get_voltage,
  332. };
  333. static int mc13892_sw_regulator_get_voltage(struct regulator_dev *rdev)
  334. {
  335. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  336. int ret, id = rdev_get_id(rdev);
  337. unsigned int val, hi;
  338. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  339. mc13xxx_lock(priv->mc13xxx);
  340. ret = mc13xxx_reg_read(priv->mc13xxx,
  341. mc13892_regulators[id].vsel_reg, &val);
  342. mc13xxx_unlock(priv->mc13xxx);
  343. if (ret)
  344. return ret;
  345. hi = val & MC13892_SWITCHERS0_SWxHI;
  346. val = (val & mc13892_regulators[id].vsel_mask)
  347. >> mc13892_regulators[id].vsel_shift;
  348. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  349. if (hi)
  350. val = (25000 * val) + 1100000;
  351. else
  352. val = (25000 * val) + 600000;
  353. return val;
  354. }
  355. static int mc13892_sw_regulator_set_voltage(struct regulator_dev *rdev,
  356. int min_uV, int max_uV, unsigned *selector)
  357. {
  358. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  359. int hi, value, mask, id = rdev_get_id(rdev);
  360. u32 valread;
  361. int ret;
  362. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  363. __func__, id, min_uV, max_uV);
  364. /* Find the best index */
  365. value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
  366. dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
  367. if (value < 0)
  368. return value;
  369. value = mc13892_regulators[id].voltages[value];
  370. mc13xxx_lock(priv->mc13xxx);
  371. ret = mc13xxx_reg_read(priv->mc13xxx,
  372. mc13892_regulators[id].vsel_reg, &valread);
  373. if (ret)
  374. goto err;
  375. if (value > 1375000)
  376. hi = 1;
  377. else if (value < 1100000)
  378. hi = 0;
  379. else
  380. hi = valread & MC13892_SWITCHERS0_SWxHI;
  381. if (hi) {
  382. value = (value - 1100000) / 25000;
  383. value |= MC13892_SWITCHERS0_SWxHI;
  384. } else
  385. value = (value - 600000) / 25000;
  386. mask = mc13892_regulators[id].vsel_mask | MC13892_SWITCHERS0_SWxHI;
  387. valread = (valread & ~mask) |
  388. (value << mc13892_regulators[id].vsel_shift);
  389. ret = mc13xxx_reg_write(priv->mc13xxx, mc13892_regulators[id].vsel_reg,
  390. valread);
  391. err:
  392. mc13xxx_unlock(priv->mc13xxx);
  393. return ret;
  394. }
  395. static struct regulator_ops mc13892_sw_regulator_ops = {
  396. .is_enabled = mc13xxx_sw_regulator_is_enabled,
  397. .list_voltage = mc13xxx_regulator_list_voltage,
  398. .set_voltage = mc13892_sw_regulator_set_voltage,
  399. .get_voltage = mc13892_sw_regulator_get_voltage,
  400. };
  401. static int mc13892_vcam_set_mode(struct regulator_dev *rdev, unsigned int mode)
  402. {
  403. unsigned int en_val = 0;
  404. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  405. int ret, id = rdev_get_id(rdev);
  406. if (mode == REGULATOR_MODE_FAST)
  407. en_val = MC13892_REGULATORMODE1_VCAMCONFIGEN;
  408. mc13xxx_lock(priv->mc13xxx);
  409. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].reg,
  410. MC13892_REGULATORMODE1_VCAMCONFIGEN, en_val);
  411. mc13xxx_unlock(priv->mc13xxx);
  412. return ret;
  413. }
  414. static unsigned int mc13892_vcam_get_mode(struct regulator_dev *rdev)
  415. {
  416. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  417. int ret, id = rdev_get_id(rdev);
  418. unsigned int val;
  419. mc13xxx_lock(priv->mc13xxx);
  420. ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
  421. mc13xxx_unlock(priv->mc13xxx);
  422. if (ret)
  423. return ret;
  424. if (val & MC13892_REGULATORMODE1_VCAMCONFIGEN)
  425. return REGULATOR_MODE_FAST;
  426. return REGULATOR_MODE_NORMAL;
  427. }
  428. static int __devinit mc13892_regulator_probe(struct platform_device *pdev)
  429. {
  430. struct mc13xxx_regulator_priv *priv;
  431. struct mc13xxx *mc13892 = dev_get_drvdata(pdev->dev.parent);
  432. struct mc13xxx_regulator_platform_data *pdata =
  433. dev_get_platdata(&pdev->dev);
  434. struct mc13xxx_regulator_init_data *mc13xxx_data;
  435. int i, ret;
  436. int num_regulators = 0;
  437. u32 val;
  438. num_regulators = mc13xxx_get_num_regulators_dt(pdev);
  439. if (num_regulators <= 0 && pdata)
  440. num_regulators = pdata->num_regulators;
  441. if (num_regulators <= 0)
  442. return -EINVAL;
  443. priv = devm_kzalloc(&pdev->dev, sizeof(*priv) +
  444. num_regulators * sizeof(priv->regulators[0]),
  445. GFP_KERNEL);
  446. if (!priv)
  447. return -ENOMEM;
  448. priv->num_regulators = num_regulators;
  449. priv->mc13xxx_regulators = mc13892_regulators;
  450. priv->mc13xxx = mc13892;
  451. platform_set_drvdata(pdev, priv);
  452. mc13xxx_lock(mc13892);
  453. ret = mc13xxx_reg_read(mc13892, MC13892_REVISION, &val);
  454. if (ret)
  455. goto err_unlock;
  456. /* enable switch auto mode */
  457. if ((val & 0x0000FFFF) == 0x45d0) {
  458. ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS4,
  459. MC13892_SWITCHERS4_SW1MODE_M |
  460. MC13892_SWITCHERS4_SW2MODE_M,
  461. MC13892_SWITCHERS4_SW1MODE_AUTO |
  462. MC13892_SWITCHERS4_SW2MODE_AUTO);
  463. if (ret)
  464. goto err_unlock;
  465. ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS5,
  466. MC13892_SWITCHERS5_SW3MODE_M |
  467. MC13892_SWITCHERS5_SW4MODE_M,
  468. MC13892_SWITCHERS5_SW3MODE_AUTO |
  469. MC13892_SWITCHERS5_SW4MODE_AUTO);
  470. if (ret)
  471. goto err_unlock;
  472. }
  473. mc13xxx_unlock(mc13892);
  474. mc13892_regulators[MC13892_VCAM].desc.ops->set_mode
  475. = mc13892_vcam_set_mode;
  476. mc13892_regulators[MC13892_VCAM].desc.ops->get_mode
  477. = mc13892_vcam_get_mode;
  478. mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13892_regulators,
  479. ARRAY_SIZE(mc13892_regulators));
  480. for (i = 0; i < num_regulators; i++) {
  481. struct regulator_init_data *init_data;
  482. struct regulator_desc *desc;
  483. struct device_node *node = NULL;
  484. int id;
  485. if (mc13xxx_data) {
  486. id = mc13xxx_data[i].id;
  487. init_data = mc13xxx_data[i].init_data;
  488. node = mc13xxx_data[i].node;
  489. } else {
  490. id = pdata->regulators[i].id;
  491. init_data = pdata->regulators[i].init_data;
  492. }
  493. desc = &mc13892_regulators[id].desc;
  494. priv->regulators[i] = regulator_register(
  495. desc, &pdev->dev, init_data, priv, node);
  496. if (IS_ERR(priv->regulators[i])) {
  497. dev_err(&pdev->dev, "failed to register regulator %s\n",
  498. mc13892_regulators[i].desc.name);
  499. ret = PTR_ERR(priv->regulators[i]);
  500. goto err;
  501. }
  502. }
  503. return 0;
  504. err:
  505. while (--i >= 0)
  506. regulator_unregister(priv->regulators[i]);
  507. return ret;
  508. err_unlock:
  509. mc13xxx_unlock(mc13892);
  510. return ret;
  511. }
  512. static int __devexit mc13892_regulator_remove(struct platform_device *pdev)
  513. {
  514. struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
  515. int i;
  516. platform_set_drvdata(pdev, NULL);
  517. for (i = 0; i < priv->num_regulators; i++)
  518. regulator_unregister(priv->regulators[i]);
  519. return 0;
  520. }
  521. static struct platform_driver mc13892_regulator_driver = {
  522. .driver = {
  523. .name = "mc13892-regulator",
  524. .owner = THIS_MODULE,
  525. },
  526. .remove = __devexit_p(mc13892_regulator_remove),
  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");