wm8400-regulator.c 10 KB

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