wm8994-regulator.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * wm8994-regulator.c -- Regulator driver for the WM8994
  3. *
  4. * Copyright 2009 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 modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/wm8994/core.h>
  23. #include <linux/mfd/wm8994/registers.h>
  24. #include <linux/mfd/wm8994/pdata.h>
  25. struct wm8994_ldo {
  26. int enable;
  27. bool is_enabled;
  28. struct regulator_dev *regulator;
  29. struct wm8994 *wm8994;
  30. };
  31. #define WM8994_LDO1_MAX_SELECTOR 0x7
  32. #define WM8994_LDO2_MAX_SELECTOR 0x3
  33. static int wm8994_ldo_enable(struct regulator_dev *rdev)
  34. {
  35. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  36. /* If we have no soft control assume that the LDO is always enabled. */
  37. if (!ldo->enable)
  38. return 0;
  39. gpio_set_value_cansleep(ldo->enable, 1);
  40. ldo->is_enabled = true;
  41. return 0;
  42. }
  43. static int wm8994_ldo_disable(struct regulator_dev *rdev)
  44. {
  45. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  46. /* If we have no soft control assume that the LDO is always enabled. */
  47. if (!ldo->enable)
  48. return -EINVAL;
  49. gpio_set_value_cansleep(ldo->enable, 0);
  50. ldo->is_enabled = false;
  51. return 0;
  52. }
  53. static int wm8994_ldo_is_enabled(struct regulator_dev *rdev)
  54. {
  55. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  56. return ldo->is_enabled;
  57. }
  58. static int wm8994_ldo_enable_time(struct regulator_dev *rdev)
  59. {
  60. /* 3ms is fairly conservative but this shouldn't be too performance
  61. * critical; can be tweaked per-system if required. */
  62. return 3000;
  63. }
  64. static int wm8994_ldo1_list_voltage(struct regulator_dev *rdev,
  65. unsigned int selector)
  66. {
  67. if (selector > WM8994_LDO1_MAX_SELECTOR)
  68. return -EINVAL;
  69. return (selector * 100000) + 2400000;
  70. }
  71. static int wm8994_ldo1_get_voltage_sel(struct regulator_dev *rdev)
  72. {
  73. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  74. int val;
  75. val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_1);
  76. if (val < 0)
  77. return val;
  78. return (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
  79. }
  80. static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
  81. int min_uV, int max_uV, unsigned *s)
  82. {
  83. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  84. int selector, v;
  85. selector = (min_uV - 2400000) / 100000;
  86. v = wm8994_ldo1_list_voltage(rdev, selector);
  87. if (v < 0 || v > max_uV)
  88. return -EINVAL;
  89. *s = selector;
  90. selector <<= WM8994_LDO1_VSEL_SHIFT;
  91. return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
  92. WM8994_LDO1_VSEL_MASK, selector);
  93. }
  94. static struct regulator_ops wm8994_ldo1_ops = {
  95. .enable = wm8994_ldo_enable,
  96. .disable = wm8994_ldo_disable,
  97. .is_enabled = wm8994_ldo_is_enabled,
  98. .enable_time = wm8994_ldo_enable_time,
  99. .list_voltage = wm8994_ldo1_list_voltage,
  100. .get_voltage_sel = wm8994_ldo1_get_voltage_sel,
  101. .set_voltage = wm8994_ldo1_set_voltage,
  102. };
  103. static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
  104. unsigned int selector)
  105. {
  106. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  107. if (selector > WM8994_LDO2_MAX_SELECTOR)
  108. return -EINVAL;
  109. switch (ldo->wm8994->type) {
  110. case WM8994:
  111. return (selector * 100000) + 900000;
  112. case WM8958:
  113. return (selector * 100000) + 1000000;
  114. case WM1811:
  115. switch (selector) {
  116. case 0:
  117. return -EINVAL;
  118. default:
  119. return (selector * 100000) + 950000;
  120. }
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. }
  126. static int wm8994_ldo2_get_voltage_sel(struct regulator_dev *rdev)
  127. {
  128. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  129. int val;
  130. val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
  131. if (val < 0)
  132. return val;
  133. return (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
  134. }
  135. static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
  136. int min_uV, int max_uV, unsigned *s)
  137. {
  138. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  139. int selector, v;
  140. switch (ldo->wm8994->type) {
  141. case WM8994:
  142. selector = (min_uV - 900000) / 100000;
  143. break;
  144. case WM8958:
  145. selector = (min_uV - 1000000) / 100000;
  146. break;
  147. case WM1811:
  148. selector = (min_uV - 950000) / 100000;
  149. if (selector == 0)
  150. selector = 1;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. v = wm8994_ldo2_list_voltage(rdev, selector);
  156. if (v < 0 || v > max_uV)
  157. return -EINVAL;
  158. *s = selector;
  159. selector <<= WM8994_LDO2_VSEL_SHIFT;
  160. return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
  161. WM8994_LDO2_VSEL_MASK, selector);
  162. }
  163. static struct regulator_ops wm8994_ldo2_ops = {
  164. .enable = wm8994_ldo_enable,
  165. .disable = wm8994_ldo_disable,
  166. .is_enabled = wm8994_ldo_is_enabled,
  167. .enable_time = wm8994_ldo_enable_time,
  168. .list_voltage = wm8994_ldo2_list_voltage,
  169. .get_voltage_sel = wm8994_ldo2_get_voltage_sel,
  170. .set_voltage = wm8994_ldo2_set_voltage,
  171. };
  172. static struct regulator_desc wm8994_ldo_desc[] = {
  173. {
  174. .name = "LDO1",
  175. .id = 1,
  176. .type = REGULATOR_VOLTAGE,
  177. .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
  178. .ops = &wm8994_ldo1_ops,
  179. .owner = THIS_MODULE,
  180. },
  181. {
  182. .name = "LDO2",
  183. .id = 2,
  184. .type = REGULATOR_VOLTAGE,
  185. .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
  186. .ops = &wm8994_ldo2_ops,
  187. .owner = THIS_MODULE,
  188. },
  189. };
  190. static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
  191. {
  192. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  193. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  194. int id = pdev->id % ARRAY_SIZE(pdata->ldo);
  195. struct wm8994_ldo *ldo;
  196. int ret;
  197. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  198. if (!pdata)
  199. return -ENODEV;
  200. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
  201. if (ldo == NULL) {
  202. dev_err(&pdev->dev, "Unable to allocate private data\n");
  203. return -ENOMEM;
  204. }
  205. ldo->wm8994 = wm8994;
  206. if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
  207. ldo->enable = pdata->ldo[id].enable;
  208. ret = gpio_request(ldo->enable, "WM8994 LDO enable");
  209. if (ret < 0) {
  210. dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
  211. ret);
  212. goto err;
  213. }
  214. ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
  215. if (ret < 0) {
  216. dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
  217. ret);
  218. goto err_gpio;
  219. }
  220. } else
  221. ldo->is_enabled = true;
  222. ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
  223. pdata->ldo[id].init_data, ldo, NULL);
  224. if (IS_ERR(ldo->regulator)) {
  225. ret = PTR_ERR(ldo->regulator);
  226. dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
  227. id + 1, ret);
  228. goto err_gpio;
  229. }
  230. platform_set_drvdata(pdev, ldo);
  231. return 0;
  232. err_gpio:
  233. if (gpio_is_valid(ldo->enable))
  234. gpio_free(ldo->enable);
  235. err:
  236. return ret;
  237. }
  238. static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
  239. {
  240. struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
  241. platform_set_drvdata(pdev, NULL);
  242. regulator_unregister(ldo->regulator);
  243. if (gpio_is_valid(ldo->enable))
  244. gpio_free(ldo->enable);
  245. return 0;
  246. }
  247. static struct platform_driver wm8994_ldo_driver = {
  248. .probe = wm8994_ldo_probe,
  249. .remove = __devexit_p(wm8994_ldo_remove),
  250. .driver = {
  251. .name = "wm8994-ldo",
  252. .owner = THIS_MODULE,
  253. },
  254. };
  255. static int __init wm8994_ldo_init(void)
  256. {
  257. int ret;
  258. ret = platform_driver_register(&wm8994_ldo_driver);
  259. if (ret != 0)
  260. pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
  261. return ret;
  262. }
  263. subsys_initcall(wm8994_ldo_init);
  264. static void __exit wm8994_ldo_exit(void)
  265. {
  266. platform_driver_unregister(&wm8994_ldo_driver);
  267. }
  268. module_exit(wm8994_ldo_exit);
  269. /* Module information */
  270. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  271. MODULE_DESCRIPTION("WM8994 LDO driver");
  272. MODULE_LICENSE("GPL");
  273. MODULE_ALIAS("platform:wm8994-ldo");