wm8994-regulator.c 6.0 KB

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