mc13783.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Regulator Driver for Freescale MC13783 PMIC
  3. *
  4. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/mfd/mc13783-private.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/mc13783.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/err.h>
  18. struct mc13783_regulator {
  19. struct regulator_desc desc;
  20. int reg;
  21. int enable_bit;
  22. };
  23. static struct regulator_ops mc13783_regulator_ops;
  24. static struct mc13783_regulator mc13783_regulators[] = {
  25. [MC13783_SW_SW3] = {
  26. .desc = {
  27. .name = "SW_SW3",
  28. .ops = &mc13783_regulator_ops,
  29. .type = REGULATOR_VOLTAGE,
  30. .id = MC13783_SW_SW3,
  31. .owner = THIS_MODULE,
  32. },
  33. .reg = MC13783_REG_SWITCHERS_5,
  34. .enable_bit = MC13783_SWCTRL_SW3_EN,
  35. },
  36. [MC13783_SW_PLL] = {
  37. .desc = {
  38. .name = "SW_PLL",
  39. .ops = &mc13783_regulator_ops,
  40. .type = REGULATOR_VOLTAGE,
  41. .id = MC13783_SW_PLL,
  42. .owner = THIS_MODULE,
  43. },
  44. .reg = MC13783_REG_SWITCHERS_4,
  45. .enable_bit = MC13783_SWCTRL_PLL_EN,
  46. },
  47. [MC13783_REGU_VAUDIO] = {
  48. .desc = {
  49. .name = "REGU_VAUDIO",
  50. .ops = &mc13783_regulator_ops,
  51. .type = REGULATOR_VOLTAGE,
  52. .id = MC13783_REGU_VAUDIO,
  53. .owner = THIS_MODULE,
  54. },
  55. .reg = MC13783_REG_REGULATOR_MODE_0,
  56. .enable_bit = MC13783_REGCTRL_VAUDIO_EN,
  57. },
  58. [MC13783_REGU_VIOHI] = {
  59. .desc = {
  60. .name = "REGU_VIOHI",
  61. .ops = &mc13783_regulator_ops,
  62. .type = REGULATOR_VOLTAGE,
  63. .id = MC13783_REGU_VIOHI,
  64. .owner = THIS_MODULE,
  65. },
  66. .reg = MC13783_REG_REGULATOR_MODE_0,
  67. .enable_bit = MC13783_REGCTRL_VIOHI_EN,
  68. },
  69. [MC13783_REGU_VIOLO] = {
  70. .desc = {
  71. .name = "REGU_VIOLO",
  72. .ops = &mc13783_regulator_ops,
  73. .type = REGULATOR_VOLTAGE,
  74. .id = MC13783_REGU_VIOLO,
  75. .owner = THIS_MODULE,
  76. },
  77. .reg = MC13783_REG_REGULATOR_MODE_0,
  78. .enable_bit = MC13783_REGCTRL_VIOLO_EN,
  79. },
  80. [MC13783_REGU_VDIG] = {
  81. .desc = {
  82. .name = "REGU_VDIG",
  83. .ops = &mc13783_regulator_ops,
  84. .type = REGULATOR_VOLTAGE,
  85. .id = MC13783_REGU_VDIG,
  86. .owner = THIS_MODULE,
  87. },
  88. .reg = MC13783_REG_REGULATOR_MODE_0,
  89. .enable_bit = MC13783_REGCTRL_VDIG_EN,
  90. },
  91. [MC13783_REGU_VGEN] = {
  92. .desc = {
  93. .name = "REGU_VGEN",
  94. .ops = &mc13783_regulator_ops,
  95. .type = REGULATOR_VOLTAGE,
  96. .id = MC13783_REGU_VGEN,
  97. .owner = THIS_MODULE,
  98. },
  99. .reg = MC13783_REG_REGULATOR_MODE_0,
  100. .enable_bit = MC13783_REGCTRL_VGEN_EN,
  101. },
  102. [MC13783_REGU_VRFDIG] = {
  103. .desc = {
  104. .name = "REGU_VRFDIG",
  105. .ops = &mc13783_regulator_ops,
  106. .type = REGULATOR_VOLTAGE,
  107. .id = MC13783_REGU_VRFDIG,
  108. .owner = THIS_MODULE,
  109. },
  110. .reg = MC13783_REG_REGULATOR_MODE_0,
  111. .enable_bit = MC13783_REGCTRL_VRFDIG_EN,
  112. },
  113. [MC13783_REGU_VRFREF] = {
  114. .desc = {
  115. .name = "REGU_VRFREF",
  116. .ops = &mc13783_regulator_ops,
  117. .type = REGULATOR_VOLTAGE,
  118. .id = MC13783_REGU_VRFREF,
  119. .owner = THIS_MODULE,
  120. },
  121. .reg = MC13783_REG_REGULATOR_MODE_0,
  122. .enable_bit = MC13783_REGCTRL_VRFREF_EN,
  123. },
  124. [MC13783_REGU_VRFCP] = {
  125. .desc = {
  126. .name = "REGU_VRFCP",
  127. .ops = &mc13783_regulator_ops,
  128. .type = REGULATOR_VOLTAGE,
  129. .id = MC13783_REGU_VRFCP,
  130. .owner = THIS_MODULE,
  131. },
  132. .reg = MC13783_REG_REGULATOR_MODE_0,
  133. .enable_bit = MC13783_REGCTRL_VRFCP_EN,
  134. },
  135. [MC13783_REGU_VSIM] = {
  136. .desc = {
  137. .name = "REGU_VSIM",
  138. .ops = &mc13783_regulator_ops,
  139. .type = REGULATOR_VOLTAGE,
  140. .id = MC13783_REGU_VSIM,
  141. .owner = THIS_MODULE,
  142. },
  143. .reg = MC13783_REG_REGULATOR_MODE_1,
  144. .enable_bit = MC13783_REGCTRL_VSIM_EN,
  145. },
  146. [MC13783_REGU_VESIM] = {
  147. .desc = {
  148. .name = "REGU_VESIM",
  149. .ops = &mc13783_regulator_ops,
  150. .type = REGULATOR_VOLTAGE,
  151. .id = MC13783_REGU_VESIM,
  152. .owner = THIS_MODULE,
  153. },
  154. .reg = MC13783_REG_REGULATOR_MODE_1,
  155. .enable_bit = MC13783_REGCTRL_VESIM_EN,
  156. },
  157. [MC13783_REGU_VCAM] = {
  158. .desc = {
  159. .name = "REGU_VCAM",
  160. .ops = &mc13783_regulator_ops,
  161. .type = REGULATOR_VOLTAGE,
  162. .id = MC13783_REGU_VCAM,
  163. .owner = THIS_MODULE,
  164. },
  165. .reg = MC13783_REG_REGULATOR_MODE_1,
  166. .enable_bit = MC13783_REGCTRL_VCAM_EN,
  167. },
  168. [MC13783_REGU_VRFBG] = {
  169. .desc = {
  170. .name = "REGU_VRFBG",
  171. .ops = &mc13783_regulator_ops,
  172. .type = REGULATOR_VOLTAGE,
  173. .id = MC13783_REGU_VRFBG,
  174. .owner = THIS_MODULE,
  175. },
  176. .reg = MC13783_REG_REGULATOR_MODE_1,
  177. .enable_bit = MC13783_REGCTRL_VRFBG_EN,
  178. },
  179. [MC13783_REGU_VVIB] = {
  180. .desc = {
  181. .name = "REGU_VVIB",
  182. .ops = &mc13783_regulator_ops,
  183. .type = REGULATOR_VOLTAGE,
  184. .id = MC13783_REGU_VVIB,
  185. .owner = THIS_MODULE,
  186. },
  187. .reg = MC13783_REG_REGULATOR_MODE_1,
  188. .enable_bit = MC13783_REGCTRL_VVIB_EN,
  189. },
  190. [MC13783_REGU_VRF1] = {
  191. .desc = {
  192. .name = "REGU_VRF1",
  193. .ops = &mc13783_regulator_ops,
  194. .type = REGULATOR_VOLTAGE,
  195. .id = MC13783_REGU_VRF1,
  196. .owner = THIS_MODULE,
  197. },
  198. .reg = MC13783_REG_REGULATOR_MODE_1,
  199. .enable_bit = MC13783_REGCTRL_VRF1_EN,
  200. },
  201. [MC13783_REGU_VRF2] = {
  202. .desc = {
  203. .name = "REGU_VRF2",
  204. .ops = &mc13783_regulator_ops,
  205. .type = REGULATOR_VOLTAGE,
  206. .id = MC13783_REGU_VRF2,
  207. .owner = THIS_MODULE,
  208. },
  209. .reg = MC13783_REG_REGULATOR_MODE_1,
  210. .enable_bit = MC13783_REGCTRL_VRF2_EN,
  211. },
  212. [MC13783_REGU_VMMC1] = {
  213. .desc = {
  214. .name = "REGU_VMMC1",
  215. .ops = &mc13783_regulator_ops,
  216. .type = REGULATOR_VOLTAGE,
  217. .id = MC13783_REGU_VMMC1,
  218. .owner = THIS_MODULE,
  219. },
  220. .reg = MC13783_REG_REGULATOR_MODE_1,
  221. .enable_bit = MC13783_REGCTRL_VMMC1_EN,
  222. },
  223. [MC13783_REGU_VMMC2] = {
  224. .desc = {
  225. .name = "REGU_VMMC2",
  226. .ops = &mc13783_regulator_ops,
  227. .type = REGULATOR_VOLTAGE,
  228. .id = MC13783_REGU_VMMC2,
  229. .owner = THIS_MODULE,
  230. },
  231. .reg = MC13783_REG_REGULATOR_MODE_1,
  232. .enable_bit = MC13783_REGCTRL_VMMC2_EN,
  233. },
  234. [MC13783_REGU_GPO1] = {
  235. .desc = {
  236. .name = "REGU_GPO1",
  237. .ops = &mc13783_regulator_ops,
  238. .type = REGULATOR_VOLTAGE,
  239. .id = MC13783_REGU_GPO1,
  240. .owner = THIS_MODULE,
  241. },
  242. .reg = MC13783_REG_POWER_MISCELLANEOUS,
  243. .enable_bit = MC13783_REGCTRL_GPO1_EN,
  244. },
  245. [MC13783_REGU_GPO2] = {
  246. .desc = {
  247. .name = "REGU_GPO2",
  248. .ops = &mc13783_regulator_ops,
  249. .type = REGULATOR_VOLTAGE,
  250. .id = MC13783_REGU_GPO2,
  251. .owner = THIS_MODULE,
  252. },
  253. .reg = MC13783_REG_POWER_MISCELLANEOUS,
  254. .enable_bit = MC13783_REGCTRL_GPO2_EN,
  255. },
  256. [MC13783_REGU_GPO3] = {
  257. .desc = {
  258. .name = "REGU_GPO3",
  259. .ops = &mc13783_regulator_ops,
  260. .type = REGULATOR_VOLTAGE,
  261. .id = MC13783_REGU_GPO3,
  262. .owner = THIS_MODULE,
  263. },
  264. .reg = MC13783_REG_POWER_MISCELLANEOUS,
  265. .enable_bit = MC13783_REGCTRL_GPO3_EN,
  266. },
  267. [MC13783_REGU_GPO4] = {
  268. .desc = {
  269. .name = "REGU_GPO4",
  270. .ops = &mc13783_regulator_ops,
  271. .type = REGULATOR_VOLTAGE,
  272. .id = MC13783_REGU_GPO4,
  273. .owner = THIS_MODULE,
  274. },
  275. .reg = MC13783_REG_POWER_MISCELLANEOUS,
  276. .enable_bit = MC13783_REGCTRL_GPO4_EN,
  277. },
  278. };
  279. struct mc13783_priv {
  280. struct regulator_desc desc[ARRAY_SIZE(mc13783_regulators)];
  281. struct mc13783 *mc13783;
  282. struct regulator_dev *regulators[0];
  283. };
  284. static int mc13783_enable(struct regulator_dev *rdev)
  285. {
  286. struct mc13783_priv *priv = rdev_get_drvdata(rdev);
  287. int id = rdev_get_id(rdev);
  288. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  289. return mc13783_set_bits(priv->mc13783, mc13783_regulators[id].reg,
  290. mc13783_regulators[id].enable_bit,
  291. mc13783_regulators[id].enable_bit);
  292. }
  293. static int mc13783_disable(struct regulator_dev *rdev)
  294. {
  295. struct mc13783_priv *priv = rdev_get_drvdata(rdev);
  296. int id = rdev_get_id(rdev);
  297. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  298. return mc13783_set_bits(priv->mc13783, mc13783_regulators[id].reg,
  299. mc13783_regulators[id].enable_bit, 0);
  300. }
  301. static int mc13783_is_enabled(struct regulator_dev *rdev)
  302. {
  303. struct mc13783_priv *priv = rdev_get_drvdata(rdev);
  304. int ret, id = rdev_get_id(rdev);
  305. unsigned int val;
  306. ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
  307. if (ret)
  308. return ret;
  309. return (val & mc13783_regulators[id].enable_bit) != 0;
  310. }
  311. static struct regulator_ops mc13783_regulator_ops = {
  312. .enable = mc13783_enable,
  313. .disable = mc13783_disable,
  314. .is_enabled = mc13783_is_enabled,
  315. };
  316. static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
  317. {
  318. struct mc13783_priv *priv;
  319. struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent);
  320. struct mc13783_regulator_init_data *init_data;
  321. int i, ret;
  322. dev_dbg(&pdev->dev, "mc13783_regulator_probe id %d\n", pdev->id);
  323. priv = kzalloc(sizeof(*priv) + mc13783->num_regulators * sizeof(void *),
  324. GFP_KERNEL);
  325. if (!priv)
  326. return -ENOMEM;
  327. priv->mc13783 = mc13783;
  328. for (i = 0; i < mc13783->num_regulators; i++) {
  329. init_data = &mc13783->regulators[i];
  330. priv->regulators[i] = regulator_register(
  331. &mc13783_regulators[init_data->id].desc,
  332. &pdev->dev, init_data->init_data, priv);
  333. if (IS_ERR(priv->regulators[i])) {
  334. dev_err(&pdev->dev, "failed to register regulator %s\n",
  335. mc13783_regulators[i].desc.name);
  336. ret = PTR_ERR(priv->regulators[i]);
  337. goto err;
  338. }
  339. }
  340. platform_set_drvdata(pdev, priv);
  341. return 0;
  342. err:
  343. while (--i >= 0)
  344. regulator_unregister(priv->regulators[i]);
  345. kfree(priv);
  346. return ret;
  347. }
  348. static int __devexit mc13783_regulator_remove(struct platform_device *pdev)
  349. {
  350. struct mc13783_priv *priv = platform_get_drvdata(pdev);
  351. struct mc13783 *mc13783 = priv->mc13783;
  352. int i;
  353. for (i = 0; i < mc13783->num_regulators; i++)
  354. regulator_unregister(priv->regulators[i]);
  355. return 0;
  356. }
  357. static struct platform_driver mc13783_regulator_driver = {
  358. .driver = {
  359. .name = "mc13783-regulator",
  360. .owner = THIS_MODULE,
  361. },
  362. .remove = __devexit_p(mc13783_regulator_remove),
  363. };
  364. static int __init mc13783_regulator_init(void)
  365. {
  366. return platform_driver_probe(&mc13783_regulator_driver,
  367. mc13783_regulator_probe);
  368. }
  369. subsys_initcall(mc13783_regulator_init);
  370. static void __exit mc13783_regulator_exit(void)
  371. {
  372. platform_driver_unregister(&mc13783_regulator_driver);
  373. }
  374. module_exit(mc13783_regulator_exit);
  375. MODULE_LICENSE("GPL");
  376. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
  377. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
  378. MODULE_ALIAS("platform:mc13783-regulator");