mc13783-regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * Regulator Driver for Freescale MC13783 PMIC
  3. *
  4. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  5. * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/mfd/mc13783.h>
  12. #include <linux/regulator/machine.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/err.h>
  18. #define MC13783_REG_SWITCHERS5 29
  19. #define MC13783_REG_SWITCHERS5_SW3EN (1 << 20)
  20. #define MC13783_REG_SWITCHERS5_SW3VSEL 18
  21. #define MC13783_REG_SWITCHERS5_SW3VSEL_M (3 << 18)
  22. #define MC13783_REG_REGULATORSETTING0 30
  23. #define MC13783_REG_REGULATORSETTING0_VIOLOVSEL 2
  24. #define MC13783_REG_REGULATORSETTING0_VDIGVSEL 4
  25. #define MC13783_REG_REGULATORSETTING0_VGENVSEL 6
  26. #define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL 9
  27. #define MC13783_REG_REGULATORSETTING0_VRFREFVSEL 11
  28. #define MC13783_REG_REGULATORSETTING0_VRFCPVSEL 13
  29. #define MC13783_REG_REGULATORSETTING0_VSIMVSEL 14
  30. #define MC13783_REG_REGULATORSETTING0_VESIMVSEL 15
  31. #define MC13783_REG_REGULATORSETTING0_VCAMVSEL 16
  32. #define MC13783_REG_REGULATORSETTING0_VIOLOVSEL_M (3 << 2)
  33. #define MC13783_REG_REGULATORSETTING0_VDIGVSEL_M (3 << 4)
  34. #define MC13783_REG_REGULATORSETTING0_VGENVSEL_M (7 << 6)
  35. #define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL_M (3 << 9)
  36. #define MC13783_REG_REGULATORSETTING0_VRFREFVSEL_M (3 << 11)
  37. #define MC13783_REG_REGULATORSETTING0_VRFCPVSEL_M (1 << 13)
  38. #define MC13783_REG_REGULATORSETTING0_VSIMVSEL_M (1 << 14)
  39. #define MC13783_REG_REGULATORSETTING0_VESIMVSEL_M (1 << 15)
  40. #define MC13783_REG_REGULATORSETTING0_VCAMVSEL_M (7 << 16)
  41. #define MC13783_REG_REGULATORSETTING1 31
  42. #define MC13783_REG_REGULATORSETTING1_VVIBVSEL 0
  43. #define MC13783_REG_REGULATORSETTING1_VRF1VSEL 2
  44. #define MC13783_REG_REGULATORSETTING1_VRF2VSEL 4
  45. #define MC13783_REG_REGULATORSETTING1_VMMC1VSEL 6
  46. #define MC13783_REG_REGULATORSETTING1_VMMC2VSEL 9
  47. #define MC13783_REG_REGULATORSETTING1_VVIBVSEL_M (3 << 0)
  48. #define MC13783_REG_REGULATORSETTING1_VRF1VSEL_M (3 << 2)
  49. #define MC13783_REG_REGULATORSETTING1_VRF2VSEL_M (3 << 4)
  50. #define MC13783_REG_REGULATORSETTING1_VMMC1VSEL_M (7 << 6)
  51. #define MC13783_REG_REGULATORSETTING1_VMMC2VSEL_M (7 << 9)
  52. #define MC13783_REG_REGULATORMODE0 32
  53. #define MC13783_REG_REGULATORMODE0_VAUDIOEN (1 << 0)
  54. #define MC13783_REG_REGULATORMODE0_VIOHIEN (1 << 3)
  55. #define MC13783_REG_REGULATORMODE0_VIOLOEN (1 << 6)
  56. #define MC13783_REG_REGULATORMODE0_VDIGEN (1 << 9)
  57. #define MC13783_REG_REGULATORMODE0_VGENEN (1 << 12)
  58. #define MC13783_REG_REGULATORMODE0_VRFDIGEN (1 << 15)
  59. #define MC13783_REG_REGULATORMODE0_VRFREFEN (1 << 18)
  60. #define MC13783_REG_REGULATORMODE0_VRFCPEN (1 << 21)
  61. #define MC13783_REG_REGULATORMODE1 33
  62. #define MC13783_REG_REGULATORMODE1_VSIMEN (1 << 0)
  63. #define MC13783_REG_REGULATORMODE1_VESIMEN (1 << 3)
  64. #define MC13783_REG_REGULATORMODE1_VCAMEN (1 << 6)
  65. #define MC13783_REG_REGULATORMODE1_VRFBGEN (1 << 9)
  66. #define MC13783_REG_REGULATORMODE1_VVIBEN (1 << 11)
  67. #define MC13783_REG_REGULATORMODE1_VRF1EN (1 << 12)
  68. #define MC13783_REG_REGULATORMODE1_VRF2EN (1 << 15)
  69. #define MC13783_REG_REGULATORMODE1_VMMC1EN (1 << 18)
  70. #define MC13783_REG_REGULATORMODE1_VMMC2EN (1 << 21)
  71. #define MC13783_REG_POWERMISC 34
  72. #define MC13783_REG_POWERMISC_GPO1EN (1 << 6)
  73. #define MC13783_REG_POWERMISC_GPO2EN (1 << 8)
  74. #define MC13783_REG_POWERMISC_GPO3EN (1 << 10)
  75. #define MC13783_REG_POWERMISC_GPO4EN (1 << 12)
  76. #define MC13783_REG_POWERMISC_PWGT1SPIEN (1 << 15)
  77. #define MC13783_REG_POWERMISC_PWGT2SPIEN (1 << 16)
  78. #define MC13783_REG_POWERMISC_PWGTSPI_M (3 << 15)
  79. struct mc13783_regulator {
  80. struct regulator_desc desc;
  81. int reg;
  82. int enable_bit;
  83. int vsel_reg;
  84. int vsel_shift;
  85. int vsel_mask;
  86. int const *voltages;
  87. };
  88. /* Voltage Values */
  89. static const int const mc13783_sw3_val[] = {
  90. 5000000, 5000000, 5000000, 5500000,
  91. };
  92. static const int const mc13783_vaudio_val[] = {
  93. 2775000,
  94. };
  95. static const int const mc13783_viohi_val[] = {
  96. 2775000,
  97. };
  98. static const int const mc13783_violo_val[] = {
  99. 1200000, 1300000, 1500000, 1800000,
  100. };
  101. static const int const mc13783_vdig_val[] = {
  102. 1200000, 1300000, 1500000, 1800000,
  103. };
  104. static const int const mc13783_vgen_val[] = {
  105. 1200000, 1300000, 1500000, 1800000,
  106. 1100000, 2000000, 2775000, 2400000,
  107. };
  108. static const int const mc13783_vrfdig_val[] = {
  109. 1200000, 1500000, 1800000, 1875000,
  110. };
  111. static const int const mc13783_vrfref_val[] = {
  112. 2475000, 2600000, 2700000, 2775000,
  113. };
  114. static const int const mc13783_vrfcp_val[] = {
  115. 2700000, 2775000,
  116. };
  117. static const int const mc13783_vsim_val[] = {
  118. 1800000, 2900000, 3000000,
  119. };
  120. static const int const mc13783_vesim_val[] = {
  121. 1800000, 2900000,
  122. };
  123. static const int const mc13783_vcam_val[] = {
  124. 1500000, 1800000, 2500000, 2550000,
  125. 2600000, 2750000, 2800000, 3000000,
  126. };
  127. static const int const mc13783_vrfbg_val[] = {
  128. 1250000,
  129. };
  130. static const int const mc13783_vvib_val[] = {
  131. 1300000, 1800000, 2000000, 3000000,
  132. };
  133. static const int const mc13783_vmmc_val[] = {
  134. 1600000, 1800000, 2000000, 2600000,
  135. 2700000, 2800000, 2900000, 3000000,
  136. };
  137. static const int const mc13783_vrf_val[] = {
  138. 1500000, 1875000, 2700000, 2775000,
  139. };
  140. static const int const mc13783_gpo_val[] = {
  141. 3100000,
  142. };
  143. static const int const mc13783_pwgtdrv_val[] = {
  144. 5500000,
  145. };
  146. static struct regulator_ops mc13783_regulator_ops;
  147. static struct regulator_ops mc13783_fixed_regulator_ops;
  148. static struct regulator_ops mc13783_gpo_regulator_ops;
  149. #define MC13783_DEFINE(prefix, _name, _reg, _vsel_reg, _voltages) \
  150. [MC13783_ ## prefix ## _ ## _name] = { \
  151. .desc = { \
  152. .name = #prefix "_" #_name, \
  153. .n_voltages = ARRAY_SIZE(_voltages), \
  154. .ops = &mc13783_regulator_ops, \
  155. .type = REGULATOR_VOLTAGE, \
  156. .id = MC13783_ ## prefix ## _ ## _name, \
  157. .owner = THIS_MODULE, \
  158. }, \
  159. .reg = MC13783_REG_ ## _reg, \
  160. .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
  161. .vsel_reg = MC13783_REG_ ## _vsel_reg, \
  162. .vsel_shift = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL,\
  163. .vsel_mask = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL_M,\
  164. .voltages = _voltages, \
  165. }
  166. #define MC13783_FIXED_DEFINE(prefix, _name, _reg, _voltages) \
  167. [MC13783_ ## prefix ## _ ## _name] = { \
  168. .desc = { \
  169. .name = #prefix "_" #_name, \
  170. .n_voltages = ARRAY_SIZE(_voltages), \
  171. .ops = &mc13783_fixed_regulator_ops, \
  172. .type = REGULATOR_VOLTAGE, \
  173. .id = MC13783_ ## prefix ## _ ## _name, \
  174. .owner = THIS_MODULE, \
  175. }, \
  176. .reg = MC13783_REG_ ## _reg, \
  177. .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
  178. .voltages = _voltages, \
  179. }
  180. #define MC13783_GPO_DEFINE(prefix, _name, _reg, _voltages) \
  181. [MC13783_ ## prefix ## _ ## _name] = { \
  182. .desc = { \
  183. .name = #prefix "_" #_name, \
  184. .n_voltages = ARRAY_SIZE(_voltages), \
  185. .ops = &mc13783_gpo_regulator_ops, \
  186. .type = REGULATOR_VOLTAGE, \
  187. .id = MC13783_ ## prefix ## _ ## _name, \
  188. .owner = THIS_MODULE, \
  189. }, \
  190. .reg = MC13783_REG_ ## _reg, \
  191. .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
  192. .voltages = _voltages, \
  193. }
  194. #define MC13783_DEFINE_SW(_name, _reg, _vsel_reg, _voltages) \
  195. MC13783_DEFINE(SW, _name, _reg, _vsel_reg, _voltages)
  196. #define MC13783_DEFINE_REGU(_name, _reg, _vsel_reg, _voltages) \
  197. MC13783_DEFINE(REGU, _name, _reg, _vsel_reg, _voltages)
  198. static struct mc13783_regulator mc13783_regulators[] = {
  199. MC13783_DEFINE_SW(SW3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val),
  200. MC13783_FIXED_DEFINE(REGU, VAUDIO, REGULATORMODE0, mc13783_vaudio_val),
  201. MC13783_FIXED_DEFINE(REGU, VIOHI, REGULATORMODE0, mc13783_viohi_val),
  202. MC13783_DEFINE_REGU(VIOLO, REGULATORMODE0, REGULATORSETTING0, \
  203. mc13783_violo_val),
  204. MC13783_DEFINE_REGU(VDIG, REGULATORMODE0, REGULATORSETTING0, \
  205. mc13783_vdig_val),
  206. MC13783_DEFINE_REGU(VGEN, REGULATORMODE0, REGULATORSETTING0, \
  207. mc13783_vgen_val),
  208. MC13783_DEFINE_REGU(VRFDIG, REGULATORMODE0, REGULATORSETTING0, \
  209. mc13783_vrfdig_val),
  210. MC13783_DEFINE_REGU(VRFREF, REGULATORMODE0, REGULATORSETTING0, \
  211. mc13783_vrfref_val),
  212. MC13783_DEFINE_REGU(VRFCP, REGULATORMODE0, REGULATORSETTING0, \
  213. mc13783_vrfcp_val),
  214. MC13783_DEFINE_REGU(VSIM, REGULATORMODE1, REGULATORSETTING0, \
  215. mc13783_vsim_val),
  216. MC13783_DEFINE_REGU(VESIM, REGULATORMODE1, REGULATORSETTING0, \
  217. mc13783_vesim_val),
  218. MC13783_DEFINE_REGU(VCAM, REGULATORMODE1, REGULATORSETTING0, \
  219. mc13783_vcam_val),
  220. MC13783_FIXED_DEFINE(REGU, VRFBG, REGULATORMODE1, mc13783_vrfbg_val),
  221. MC13783_DEFINE_REGU(VVIB, REGULATORMODE1, REGULATORSETTING1, \
  222. mc13783_vvib_val),
  223. MC13783_DEFINE_REGU(VRF1, REGULATORMODE1, REGULATORSETTING1, \
  224. mc13783_vrf_val),
  225. MC13783_DEFINE_REGU(VRF2, REGULATORMODE1, REGULATORSETTING1, \
  226. mc13783_vrf_val),
  227. MC13783_DEFINE_REGU(VMMC1, REGULATORMODE1, REGULATORSETTING1, \
  228. mc13783_vmmc_val),
  229. MC13783_DEFINE_REGU(VMMC2, REGULATORMODE1, REGULATORSETTING1, \
  230. mc13783_vmmc_val),
  231. MC13783_GPO_DEFINE(REGU, GPO1, POWERMISC, mc13783_gpo_val),
  232. MC13783_GPO_DEFINE(REGU, GPO2, POWERMISC, mc13783_gpo_val),
  233. MC13783_GPO_DEFINE(REGU, GPO3, POWERMISC, mc13783_gpo_val),
  234. MC13783_GPO_DEFINE(REGU, GPO4, POWERMISC, mc13783_gpo_val),
  235. MC13783_GPO_DEFINE(REGU, PWGT1SPI, POWERMISC, mc13783_pwgtdrv_val),
  236. MC13783_GPO_DEFINE(REGU, PWGT2SPI, POWERMISC, mc13783_pwgtdrv_val),
  237. };
  238. struct mc13783_regulator_priv {
  239. struct mc13783 *mc13783;
  240. u32 powermisc_pwgt_state;
  241. struct regulator_dev *regulators[];
  242. };
  243. static int mc13783_regulator_enable(struct regulator_dev *rdev)
  244. {
  245. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  246. int id = rdev_get_id(rdev);
  247. int ret;
  248. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  249. mc13783_lock(priv->mc13783);
  250. ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
  251. mc13783_regulators[id].enable_bit,
  252. mc13783_regulators[id].enable_bit);
  253. mc13783_unlock(priv->mc13783);
  254. return ret;
  255. }
  256. static int mc13783_regulator_disable(struct regulator_dev *rdev)
  257. {
  258. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  259. int id = rdev_get_id(rdev);
  260. int ret;
  261. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  262. mc13783_lock(priv->mc13783);
  263. ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
  264. mc13783_regulators[id].enable_bit, 0);
  265. mc13783_unlock(priv->mc13783);
  266. return ret;
  267. }
  268. static int mc13783_regulator_is_enabled(struct regulator_dev *rdev)
  269. {
  270. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  271. int ret, id = rdev_get_id(rdev);
  272. unsigned int val;
  273. mc13783_lock(priv->mc13783);
  274. ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
  275. mc13783_unlock(priv->mc13783);
  276. if (ret)
  277. return ret;
  278. return (val & mc13783_regulators[id].enable_bit) != 0;
  279. }
  280. static int mc13783_regulator_list_voltage(struct regulator_dev *rdev,
  281. unsigned selector)
  282. {
  283. int id = rdev_get_id(rdev);
  284. if (selector >= mc13783_regulators[id].desc.n_voltages)
  285. return -EINVAL;
  286. return mc13783_regulators[id].voltages[selector];
  287. }
  288. static int mc13783_get_best_voltage_index(struct regulator_dev *rdev,
  289. int min_uV, int max_uV)
  290. {
  291. int reg_id = rdev_get_id(rdev);
  292. int i;
  293. int bestmatch;
  294. int bestindex;
  295. /*
  296. * Locate the minimum voltage fitting the criteria on
  297. * this regulator. The switchable voltages are not
  298. * in strict falling order so we need to check them
  299. * all for the best match.
  300. */
  301. bestmatch = INT_MAX;
  302. bestindex = -1;
  303. for (i = 0; i < mc13783_regulators[reg_id].desc.n_voltages; i++) {
  304. if (mc13783_regulators[reg_id].voltages[i] >= min_uV &&
  305. mc13783_regulators[reg_id].voltages[i] < bestmatch) {
  306. bestmatch = mc13783_regulators[reg_id].voltages[i];
  307. bestindex = i;
  308. }
  309. }
  310. if (bestindex < 0 || bestmatch > max_uV) {
  311. dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
  312. min_uV, max_uV);
  313. return -EINVAL;
  314. }
  315. return bestindex;
  316. }
  317. static int mc13783_regulator_set_voltage(struct regulator_dev *rdev,
  318. int min_uV, int max_uV)
  319. {
  320. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  321. int value, id = rdev_get_id(rdev);
  322. int ret;
  323. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  324. __func__, id, min_uV, max_uV);
  325. /* Find the best index */
  326. value = mc13783_get_best_voltage_index(rdev, min_uV, max_uV);
  327. dev_dbg(rdev_get_dev(rdev), "%s best value: %d \n", __func__, value);
  328. if (value < 0)
  329. return value;
  330. mc13783_lock(priv->mc13783);
  331. ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].vsel_reg,
  332. mc13783_regulators[id].vsel_mask,
  333. value << mc13783_regulators[id].vsel_shift);
  334. mc13783_unlock(priv->mc13783);
  335. return ret;
  336. }
  337. static int mc13783_regulator_get_voltage(struct regulator_dev *rdev)
  338. {
  339. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  340. int ret, id = rdev_get_id(rdev);
  341. unsigned int val;
  342. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  343. mc13783_lock(priv->mc13783);
  344. ret = mc13783_reg_read(priv->mc13783,
  345. mc13783_regulators[id].vsel_reg, &val);
  346. mc13783_unlock(priv->mc13783);
  347. if (ret)
  348. return ret;
  349. val = (val & mc13783_regulators[id].vsel_mask)
  350. >> mc13783_regulators[id].vsel_shift;
  351. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  352. BUG_ON(val < 0 || val > mc13783_regulators[id].desc.n_voltages);
  353. return mc13783_regulators[id].voltages[val];
  354. }
  355. static struct regulator_ops mc13783_regulator_ops = {
  356. .enable = mc13783_regulator_enable,
  357. .disable = mc13783_regulator_disable,
  358. .is_enabled = mc13783_regulator_is_enabled,
  359. .list_voltage = mc13783_regulator_list_voltage,
  360. .set_voltage = mc13783_regulator_set_voltage,
  361. .get_voltage = mc13783_regulator_get_voltage,
  362. };
  363. static int mc13783_fixed_regulator_set_voltage(struct regulator_dev *rdev,
  364. int min_uV, int max_uV)
  365. {
  366. int id = rdev_get_id(rdev);
  367. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  368. __func__, id, min_uV, max_uV);
  369. if (min_uV > mc13783_regulators[id].voltages[0] &&
  370. max_uV < mc13783_regulators[id].voltages[0])
  371. return 0;
  372. else
  373. return -EINVAL;
  374. }
  375. static int mc13783_fixed_regulator_get_voltage(struct regulator_dev *rdev)
  376. {
  377. int id = rdev_get_id(rdev);
  378. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  379. return mc13783_regulators[id].voltages[0];
  380. }
  381. static struct regulator_ops mc13783_fixed_regulator_ops = {
  382. .enable = mc13783_regulator_enable,
  383. .disable = mc13783_regulator_disable,
  384. .is_enabled = mc13783_regulator_is_enabled,
  385. .list_voltage = mc13783_regulator_list_voltage,
  386. .set_voltage = mc13783_fixed_regulator_set_voltage,
  387. .get_voltage = mc13783_fixed_regulator_get_voltage,
  388. };
  389. int mc13783_powermisc_rmw(struct mc13783_regulator_priv *priv, u32 mask,
  390. u32 val)
  391. {
  392. struct mc13783 *mc13783 = priv->mc13783;
  393. int ret;
  394. u32 valread;
  395. BUG_ON(val & ~mask);
  396. ret = mc13783_reg_read(mc13783, MC13783_REG_POWERMISC, &valread);
  397. if (ret)
  398. return ret;
  399. /* Update the stored state for Power Gates. */
  400. priv->powermisc_pwgt_state =
  401. (priv->powermisc_pwgt_state & ~mask) | val;
  402. priv->powermisc_pwgt_state &= MC13783_REG_POWERMISC_PWGTSPI_M;
  403. /* Construct the new register value */
  404. valread = (valread & ~mask) | val;
  405. /* Overwrite the PWGTxEN with the stored version */
  406. valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
  407. priv->powermisc_pwgt_state;
  408. return mc13783_reg_write(mc13783, MC13783_REG_POWERMISC, valread);
  409. }
  410. static int mc13783_gpo_regulator_enable(struct regulator_dev *rdev)
  411. {
  412. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  413. int id = rdev_get_id(rdev);
  414. int ret;
  415. u32 en_val = mc13783_regulators[id].enable_bit;
  416. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  417. /* Power Gate enable value is 0 */
  418. if (id == MC13783_REGU_PWGT1SPI ||
  419. id == MC13783_REGU_PWGT2SPI)
  420. en_val = 0;
  421. mc13783_lock(priv->mc13783);
  422. ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit,
  423. en_val);
  424. mc13783_unlock(priv->mc13783);
  425. return ret;
  426. }
  427. static int mc13783_gpo_regulator_disable(struct regulator_dev *rdev)
  428. {
  429. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  430. int id = rdev_get_id(rdev);
  431. int ret;
  432. u32 dis_val = 0;
  433. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  434. /* Power Gate disable value is 1 */
  435. if (id == MC13783_REGU_PWGT1SPI ||
  436. id == MC13783_REGU_PWGT2SPI)
  437. dis_val = mc13783_regulators[id].enable_bit;
  438. mc13783_lock(priv->mc13783);
  439. ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit,
  440. dis_val);
  441. mc13783_unlock(priv->mc13783);
  442. return ret;
  443. }
  444. static int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev)
  445. {
  446. struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
  447. int ret, id = rdev_get_id(rdev);
  448. unsigned int val;
  449. mc13783_lock(priv->mc13783);
  450. ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
  451. mc13783_unlock(priv->mc13783);
  452. if (ret)
  453. return ret;
  454. /* Power Gates state is stored in powermisc_pwgt_state
  455. * where the meaning of bits is negated */
  456. val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
  457. (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M);
  458. return (val & mc13783_regulators[id].enable_bit) != 0;
  459. }
  460. static struct regulator_ops mc13783_gpo_regulator_ops = {
  461. .enable = mc13783_gpo_regulator_enable,
  462. .disable = mc13783_gpo_regulator_disable,
  463. .is_enabled = mc13783_gpo_regulator_is_enabled,
  464. .list_voltage = mc13783_regulator_list_voltage,
  465. .set_voltage = mc13783_fixed_regulator_set_voltage,
  466. .get_voltage = mc13783_fixed_regulator_get_voltage,
  467. };
  468. static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
  469. {
  470. struct mc13783_regulator_priv *priv;
  471. struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent);
  472. struct mc13783_regulator_platform_data *pdata =
  473. dev_get_platdata(&pdev->dev);
  474. struct mc13783_regulator_init_data *init_data;
  475. int i, ret;
  476. dev_dbg(&pdev->dev, "mc13783_regulator_probe id %d\n", pdev->id);
  477. priv = kzalloc(sizeof(*priv) +
  478. pdata->num_regulators * sizeof(priv->regulators[0]),
  479. GFP_KERNEL);
  480. if (!priv)
  481. return -ENOMEM;
  482. priv->mc13783 = mc13783;
  483. for (i = 0; i < pdata->num_regulators; i++) {
  484. init_data = &pdata->regulators[i];
  485. priv->regulators[i] = regulator_register(
  486. &mc13783_regulators[init_data->id].desc,
  487. &pdev->dev, init_data->init_data, priv);
  488. if (IS_ERR(priv->regulators[i])) {
  489. dev_err(&pdev->dev, "failed to register regulator %s\n",
  490. mc13783_regulators[i].desc.name);
  491. ret = PTR_ERR(priv->regulators[i]);
  492. goto err;
  493. }
  494. }
  495. platform_set_drvdata(pdev, priv);
  496. return 0;
  497. err:
  498. while (--i >= 0)
  499. regulator_unregister(priv->regulators[i]);
  500. kfree(priv);
  501. return ret;
  502. }
  503. static int __devexit mc13783_regulator_remove(struct platform_device *pdev)
  504. {
  505. struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev);
  506. struct mc13783_regulator_platform_data *pdata =
  507. dev_get_platdata(&pdev->dev);
  508. int i;
  509. for (i = 0; i < pdata->num_regulators; i++)
  510. regulator_unregister(priv->regulators[i]);
  511. return 0;
  512. }
  513. static struct platform_driver mc13783_regulator_driver = {
  514. .driver = {
  515. .name = "mc13783-regulator",
  516. .owner = THIS_MODULE,
  517. },
  518. .remove = __devexit_p(mc13783_regulator_remove),
  519. .probe = mc13783_regulator_probe,
  520. };
  521. static int __init mc13783_regulator_init(void)
  522. {
  523. return platform_driver_register(&mc13783_regulator_driver);
  524. }
  525. subsys_initcall(mc13783_regulator_init);
  526. static void __exit mc13783_regulator_exit(void)
  527. {
  528. platform_driver_unregister(&mc13783_regulator_driver);
  529. }
  530. module_exit(mc13783_regulator_exit);
  531. MODULE_LICENSE("GPL v2");
  532. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
  533. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
  534. MODULE_ALIAS("platform:mc13783-regulator");