wm8400-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Regulator support for WM8400
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/mfd/wm8400-private.h>
  20. static int wm8400_ldo_is_enabled(struct regulator_dev *dev)
  21. {
  22. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  23. u16 val;
  24. val = wm8400_reg_read(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev));
  25. return (val & WM8400_LDO1_ENA) != 0;
  26. }
  27. static int wm8400_ldo_enable(struct regulator_dev *dev)
  28. {
  29. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  30. return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev),
  31. WM8400_LDO1_ENA, WM8400_LDO1_ENA);
  32. }
  33. static int wm8400_ldo_disable(struct regulator_dev *dev)
  34. {
  35. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  36. return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev),
  37. WM8400_LDO1_ENA, 0);
  38. }
  39. static int wm8400_ldo_list_voltage(struct regulator_dev *dev,
  40. unsigned selector)
  41. {
  42. if (selector > WM8400_LDO1_VSEL_MASK)
  43. return -EINVAL;
  44. if (selector < 15)
  45. return 900000 + (selector * 50000);
  46. else
  47. return 1600000 + ((selector - 14) * 100000);
  48. }
  49. static int wm8400_ldo_get_voltage_sel(struct regulator_dev *dev)
  50. {
  51. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  52. u16 val;
  53. val = wm8400_reg_read(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev));
  54. val &= WM8400_LDO1_VSEL_MASK;
  55. return val;
  56. }
  57. static int wm8400_ldo_set_voltage(struct regulator_dev *dev,
  58. int min_uV, int max_uV, unsigned *selector)
  59. {
  60. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  61. u16 val;
  62. if (min_uV < 900000 || min_uV > 3300000)
  63. return -EINVAL;
  64. if (min_uV < 1700000) {
  65. /* Steps of 50mV from 900mV; */
  66. val = DIV_ROUND_UP(min_uV - 900000, 50000);
  67. if ((val * 50000) + 900000 > max_uV)
  68. return -EINVAL;
  69. BUG_ON((val * 50000) + 900000 < min_uV);
  70. } else {
  71. /* Steps of 100mV from 1700mV */
  72. val = DIV_ROUND_UP(min_uV - 1700000, 100000);
  73. if ((val * 100000) + 1700000 > max_uV)
  74. return -EINVAL;
  75. BUG_ON((val * 100000) + 1700000 < min_uV);
  76. val += 0xf;
  77. }
  78. *selector = val;
  79. return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev),
  80. WM8400_LDO1_VSEL_MASK, val);
  81. }
  82. static struct regulator_ops wm8400_ldo_ops = {
  83. .is_enabled = wm8400_ldo_is_enabled,
  84. .enable = wm8400_ldo_enable,
  85. .disable = wm8400_ldo_disable,
  86. .list_voltage = wm8400_ldo_list_voltage,
  87. .get_voltage_sel = wm8400_ldo_get_voltage_sel,
  88. .set_voltage = wm8400_ldo_set_voltage,
  89. };
  90. static int wm8400_dcdc_is_enabled(struct regulator_dev *dev)
  91. {
  92. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  93. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  94. u16 val;
  95. val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset);
  96. return (val & WM8400_DC1_ENA) != 0;
  97. }
  98. static int wm8400_dcdc_enable(struct regulator_dev *dev)
  99. {
  100. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  101. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  102. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  103. WM8400_DC1_ENA, WM8400_DC1_ENA);
  104. }
  105. static int wm8400_dcdc_disable(struct regulator_dev *dev)
  106. {
  107. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  108. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  109. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  110. WM8400_DC1_ENA, 0);
  111. }
  112. static int wm8400_dcdc_list_voltage(struct regulator_dev *dev,
  113. unsigned selector)
  114. {
  115. if (selector > WM8400_DC1_VSEL_MASK)
  116. return -EINVAL;
  117. return 850000 + (selector * 25000);
  118. }
  119. static int wm8400_dcdc_get_voltage_sel(struct regulator_dev *dev)
  120. {
  121. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  122. u16 val;
  123. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  124. val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset);
  125. val &= WM8400_DC1_VSEL_MASK;
  126. return val;
  127. }
  128. static int wm8400_dcdc_set_voltage(struct regulator_dev *dev,
  129. int min_uV, int max_uV, unsigned *selector)
  130. {
  131. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  132. u16 val;
  133. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  134. if (min_uV < 850000)
  135. return -EINVAL;
  136. val = DIV_ROUND_UP(min_uV - 850000, 25000);
  137. if (850000 + (25000 * val) > max_uV)
  138. return -EINVAL;
  139. BUG_ON(850000 + (25000 * val) < min_uV);
  140. *selector = val;
  141. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  142. WM8400_DC1_VSEL_MASK, val);
  143. }
  144. static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
  145. {
  146. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  147. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  148. u16 data[2];
  149. int ret;
  150. ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
  151. data);
  152. if (ret != 0)
  153. return 0;
  154. /* Datasheet: hibernate */
  155. if (data[0] & WM8400_DC1_SLEEP)
  156. return REGULATOR_MODE_STANDBY;
  157. /* Datasheet: standby */
  158. if (!(data[0] & WM8400_DC1_ACTIVE))
  159. return REGULATOR_MODE_IDLE;
  160. /* Datasheet: active with or without force PWM */
  161. if (data[1] & WM8400_DC1_FRC_PWM)
  162. return REGULATOR_MODE_FAST;
  163. else
  164. return REGULATOR_MODE_NORMAL;
  165. }
  166. static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
  167. {
  168. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  169. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  170. int ret;
  171. switch (mode) {
  172. case REGULATOR_MODE_FAST:
  173. /* Datasheet: active with force PWM */
  174. ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
  175. WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
  176. if (ret != 0)
  177. return ret;
  178. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  179. WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
  180. WM8400_DC1_ACTIVE);
  181. case REGULATOR_MODE_NORMAL:
  182. /* Datasheet: active */
  183. ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
  184. WM8400_DC1_FRC_PWM, 0);
  185. if (ret != 0)
  186. return ret;
  187. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  188. WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
  189. WM8400_DC1_ACTIVE);
  190. case REGULATOR_MODE_IDLE:
  191. /* Datasheet: standby */
  192. ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  193. WM8400_DC1_ACTIVE, 0);
  194. if (ret != 0)
  195. return ret;
  196. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  197. WM8400_DC1_SLEEP, 0);
  198. default:
  199. return -EINVAL;
  200. }
  201. }
  202. static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
  203. int input_uV, int output_uV,
  204. int load_uA)
  205. {
  206. return REGULATOR_MODE_NORMAL;
  207. }
  208. static struct regulator_ops wm8400_dcdc_ops = {
  209. .is_enabled = wm8400_dcdc_is_enabled,
  210. .enable = wm8400_dcdc_enable,
  211. .disable = wm8400_dcdc_disable,
  212. .list_voltage = wm8400_dcdc_list_voltage,
  213. .get_voltage_sel = wm8400_dcdc_get_voltage_sel,
  214. .set_voltage = wm8400_dcdc_set_voltage,
  215. .get_mode = wm8400_dcdc_get_mode,
  216. .set_mode = wm8400_dcdc_set_mode,
  217. .get_optimum_mode = wm8400_dcdc_get_optimum_mode,
  218. };
  219. static struct regulator_desc regulators[] = {
  220. {
  221. .name = "LDO1",
  222. .id = WM8400_LDO1,
  223. .ops = &wm8400_ldo_ops,
  224. .n_voltages = WM8400_LDO1_VSEL_MASK + 1,
  225. .type = REGULATOR_VOLTAGE,
  226. .owner = THIS_MODULE,
  227. },
  228. {
  229. .name = "LDO2",
  230. .id = WM8400_LDO2,
  231. .ops = &wm8400_ldo_ops,
  232. .n_voltages = WM8400_LDO2_VSEL_MASK + 1,
  233. .type = REGULATOR_VOLTAGE,
  234. .owner = THIS_MODULE,
  235. },
  236. {
  237. .name = "LDO3",
  238. .id = WM8400_LDO3,
  239. .ops = &wm8400_ldo_ops,
  240. .n_voltages = WM8400_LDO3_VSEL_MASK + 1,
  241. .type = REGULATOR_VOLTAGE,
  242. .owner = THIS_MODULE,
  243. },
  244. {
  245. .name = "LDO4",
  246. .id = WM8400_LDO4,
  247. .ops = &wm8400_ldo_ops,
  248. .n_voltages = WM8400_LDO4_VSEL_MASK + 1,
  249. .type = REGULATOR_VOLTAGE,
  250. .owner = THIS_MODULE,
  251. },
  252. {
  253. .name = "DCDC1",
  254. .id = WM8400_DCDC1,
  255. .ops = &wm8400_dcdc_ops,
  256. .n_voltages = WM8400_DC1_VSEL_MASK + 1,
  257. .type = REGULATOR_VOLTAGE,
  258. .owner = THIS_MODULE,
  259. },
  260. {
  261. .name = "DCDC2",
  262. .id = WM8400_DCDC2,
  263. .ops = &wm8400_dcdc_ops,
  264. .n_voltages = WM8400_DC2_VSEL_MASK + 1,
  265. .type = REGULATOR_VOLTAGE,
  266. .owner = THIS_MODULE,
  267. },
  268. };
  269. static int __devinit wm8400_regulator_probe(struct platform_device *pdev)
  270. {
  271. struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
  272. struct regulator_dev *rdev;
  273. rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
  274. pdev->dev.platform_data, wm8400, NULL);
  275. if (IS_ERR(rdev))
  276. return PTR_ERR(rdev);
  277. platform_set_drvdata(pdev, rdev);
  278. return 0;
  279. }
  280. static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
  281. {
  282. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  283. platform_set_drvdata(pdev, NULL);
  284. regulator_unregister(rdev);
  285. return 0;
  286. }
  287. static struct platform_driver wm8400_regulator_driver = {
  288. .driver = {
  289. .name = "wm8400-regulator",
  290. },
  291. .probe = wm8400_regulator_probe,
  292. .remove = __devexit_p(wm8400_regulator_remove),
  293. };
  294. /**
  295. * wm8400_register_regulator - enable software control of a WM8400 regulator
  296. *
  297. * This function enables software control of a WM8400 regulator via
  298. * the regulator API. It is intended to be called from the
  299. * platform_init() callback of the WM8400 MFD driver.
  300. *
  301. * @param dev The WM8400 device to operate on.
  302. * @param reg The regulator to control.
  303. * @param initdata Regulator initdata for the regulator.
  304. */
  305. int wm8400_register_regulator(struct device *dev, int reg,
  306. struct regulator_init_data *initdata)
  307. {
  308. struct wm8400 *wm8400 = dev_get_drvdata(dev);
  309. if (wm8400->regulators[reg].name)
  310. return -EBUSY;
  311. initdata->driver_data = wm8400;
  312. wm8400->regulators[reg].name = "wm8400-regulator";
  313. wm8400->regulators[reg].id = reg;
  314. wm8400->regulators[reg].dev.parent = dev;
  315. wm8400->regulators[reg].dev.platform_data = initdata;
  316. return platform_device_register(&wm8400->regulators[reg]);
  317. }
  318. EXPORT_SYMBOL_GPL(wm8400_register_regulator);
  319. static int __init wm8400_regulator_init(void)
  320. {
  321. return platform_driver_register(&wm8400_regulator_driver);
  322. }
  323. subsys_initcall(wm8400_regulator_init);
  324. static void __exit wm8400_regulator_exit(void)
  325. {
  326. platform_driver_unregister(&wm8400_regulator_driver);
  327. }
  328. module_exit(wm8400_regulator_exit);
  329. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  330. MODULE_DESCRIPTION("WM8400 regulator driver");
  331. MODULE_LICENSE("GPL");
  332. MODULE_ALIAS("platform:wm8400-regulator");