wm8994-regulator.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 struct regulator_ops wm8994_ldo1_ops = {
  72. .enable = wm8994_ldo_enable,
  73. .disable = wm8994_ldo_disable,
  74. .is_enabled = wm8994_ldo_is_enabled,
  75. .enable_time = wm8994_ldo_enable_time,
  76. .list_voltage = wm8994_ldo1_list_voltage,
  77. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  78. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  79. };
  80. static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
  81. unsigned int selector)
  82. {
  83. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  84. if (selector > WM8994_LDO2_MAX_SELECTOR)
  85. return -EINVAL;
  86. switch (ldo->wm8994->type) {
  87. case WM8994:
  88. return (selector * 100000) + 900000;
  89. case WM8958:
  90. return (selector * 100000) + 1000000;
  91. case WM1811:
  92. switch (selector) {
  93. case 0:
  94. return -EINVAL;
  95. default:
  96. return (selector * 100000) + 950000;
  97. }
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. }
  103. static struct regulator_ops wm8994_ldo2_ops = {
  104. .enable = wm8994_ldo_enable,
  105. .disable = wm8994_ldo_disable,
  106. .is_enabled = wm8994_ldo_is_enabled,
  107. .enable_time = wm8994_ldo_enable_time,
  108. .list_voltage = wm8994_ldo2_list_voltage,
  109. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  110. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  111. };
  112. static const struct regulator_desc wm8994_ldo_desc[] = {
  113. {
  114. .name = "LDO1",
  115. .id = 1,
  116. .type = REGULATOR_VOLTAGE,
  117. .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
  118. .vsel_reg = WM8994_LDO_1,
  119. .vsel_mask = WM8994_LDO1_VSEL_MASK,
  120. .ops = &wm8994_ldo1_ops,
  121. .owner = THIS_MODULE,
  122. },
  123. {
  124. .name = "LDO2",
  125. .id = 2,
  126. .type = REGULATOR_VOLTAGE,
  127. .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
  128. .vsel_reg = WM8994_LDO_2,
  129. .vsel_mask = WM8994_LDO2_VSEL_MASK,
  130. .ops = &wm8994_ldo2_ops,
  131. .owner = THIS_MODULE,
  132. },
  133. };
  134. static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
  135. {
  136. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  137. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  138. int id = pdev->id % ARRAY_SIZE(pdata->ldo);
  139. struct regulator_config config = { };
  140. struct wm8994_ldo *ldo;
  141. int ret;
  142. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  143. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
  144. if (ldo == NULL) {
  145. dev_err(&pdev->dev, "Unable to allocate private data\n");
  146. return -ENOMEM;
  147. }
  148. ldo->wm8994 = wm8994;
  149. if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
  150. ldo->enable = pdata->ldo[id].enable;
  151. ret = gpio_request_one(ldo->enable, 0, "WM8994 LDO enable");
  152. if (ret < 0) {
  153. dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
  154. ret);
  155. goto err;
  156. }
  157. } else
  158. ldo->is_enabled = true;
  159. config.dev = wm8994->dev;
  160. config.driver_data = ldo;
  161. config.regmap = wm8994->regmap;
  162. if (pdata)
  163. config.init_data = pdata->ldo[id].init_data;
  164. ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
  165. if (IS_ERR(ldo->regulator)) {
  166. ret = PTR_ERR(ldo->regulator);
  167. dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
  168. id + 1, ret);
  169. goto err_gpio;
  170. }
  171. platform_set_drvdata(pdev, ldo);
  172. return 0;
  173. err_gpio:
  174. if (gpio_is_valid(ldo->enable))
  175. gpio_free(ldo->enable);
  176. err:
  177. return ret;
  178. }
  179. static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
  180. {
  181. struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
  182. platform_set_drvdata(pdev, NULL);
  183. regulator_unregister(ldo->regulator);
  184. if (gpio_is_valid(ldo->enable))
  185. gpio_free(ldo->enable);
  186. return 0;
  187. }
  188. static struct platform_driver wm8994_ldo_driver = {
  189. .probe = wm8994_ldo_probe,
  190. .remove = __devexit_p(wm8994_ldo_remove),
  191. .driver = {
  192. .name = "wm8994-ldo",
  193. .owner = THIS_MODULE,
  194. },
  195. };
  196. static int __init wm8994_ldo_init(void)
  197. {
  198. int ret;
  199. ret = platform_driver_register(&wm8994_ldo_driver);
  200. if (ret != 0)
  201. pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
  202. return ret;
  203. }
  204. subsys_initcall(wm8994_ldo_init);
  205. static void __exit wm8994_ldo_exit(void)
  206. {
  207. platform_driver_unregister(&wm8994_ldo_driver);
  208. }
  209. module_exit(wm8994_ldo_exit);
  210. /* Module information */
  211. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  212. MODULE_DESCRIPTION("WM8994 LDO driver");
  213. MODULE_LICENSE("GPL");
  214. MODULE_ALIAS("platform:wm8994-ldo");