wm831x-isink.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * wm831x-isink.c -- Current sink driver for the WM831x series
  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/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/wm831x/core.h>
  23. #include <linux/mfd/wm831x/regulator.h>
  24. #include <linux/mfd/wm831x/pdata.h>
  25. #define WM831X_ISINK_MAX_NAME 7
  26. struct wm831x_isink {
  27. char name[WM831X_ISINK_MAX_NAME];
  28. struct regulator_desc desc;
  29. int reg;
  30. struct wm831x *wm831x;
  31. struct regulator_dev *regulator;
  32. };
  33. static int wm831x_isink_enable(struct regulator_dev *rdev)
  34. {
  35. struct wm831x_isink *isink = rdev_get_drvdata(rdev);
  36. struct wm831x *wm831x = isink->wm831x;
  37. int ret;
  38. /* We have a two stage enable: first start the ISINK... */
  39. ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA,
  40. WM831X_CS1_ENA);
  41. if (ret != 0)
  42. return ret;
  43. /* ...then enable drive */
  44. ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE,
  45. WM831X_CS1_DRIVE);
  46. if (ret != 0)
  47. wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
  48. return ret;
  49. }
  50. static int wm831x_isink_disable(struct regulator_dev *rdev)
  51. {
  52. struct wm831x_isink *isink = rdev_get_drvdata(rdev);
  53. struct wm831x *wm831x = isink->wm831x;
  54. int ret;
  55. ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, 0);
  56. if (ret < 0)
  57. return ret;
  58. ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0);
  59. if (ret < 0)
  60. return ret;
  61. return ret;
  62. }
  63. static int wm831x_isink_is_enabled(struct regulator_dev *rdev)
  64. {
  65. struct wm831x_isink *isink = rdev_get_drvdata(rdev);
  66. struct wm831x *wm831x = isink->wm831x;
  67. int ret;
  68. ret = wm831x_reg_read(wm831x, isink->reg);
  69. if (ret < 0)
  70. return ret;
  71. if ((ret & (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) ==
  72. (WM831X_CS1_ENA | WM831X_CS1_DRIVE))
  73. return 1;
  74. else
  75. return 0;
  76. }
  77. static int wm831x_isink_set_current(struct regulator_dev *rdev,
  78. int min_uA, int max_uA)
  79. {
  80. struct wm831x_isink *isink = rdev_get_drvdata(rdev);
  81. struct wm831x *wm831x = isink->wm831x;
  82. int ret, i;
  83. for (i = 0; i < ARRAY_SIZE(wm831x_isinkv_values); i++) {
  84. int val = wm831x_isinkv_values[i];
  85. if (min_uA <= val && val <= max_uA) {
  86. ret = wm831x_set_bits(wm831x, isink->reg,
  87. WM831X_CS1_ISEL_MASK, i);
  88. return ret;
  89. }
  90. }
  91. return -EINVAL;
  92. }
  93. static int wm831x_isink_get_current(struct regulator_dev *rdev)
  94. {
  95. struct wm831x_isink *isink = rdev_get_drvdata(rdev);
  96. struct wm831x *wm831x = isink->wm831x;
  97. int ret;
  98. ret = wm831x_reg_read(wm831x, isink->reg);
  99. if (ret < 0)
  100. return ret;
  101. ret &= WM831X_CS1_ISEL_MASK;
  102. if (ret > WM831X_ISINK_MAX_ISEL)
  103. ret = WM831X_ISINK_MAX_ISEL;
  104. return wm831x_isinkv_values[ret];
  105. }
  106. static const struct regulator_ops wm831x_isink_ops = {
  107. .is_enabled = wm831x_isink_is_enabled,
  108. .enable = wm831x_isink_enable,
  109. .disable = wm831x_isink_disable,
  110. .set_current_limit = wm831x_isink_set_current,
  111. .get_current_limit = wm831x_isink_get_current,
  112. };
  113. static irqreturn_t wm831x_isink_irq(int irq, void *data)
  114. {
  115. struct wm831x_isink *isink = data;
  116. regulator_notifier_call_chain(isink->regulator,
  117. REGULATOR_EVENT_OVER_CURRENT,
  118. NULL);
  119. return IRQ_HANDLED;
  120. }
  121. static int wm831x_isink_probe(struct platform_device *pdev)
  122. {
  123. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  124. struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
  125. struct wm831x_isink *isink;
  126. int id = pdev->id % ARRAY_SIZE(pdata->isink);
  127. struct regulator_config config = { };
  128. struct resource *res;
  129. int ret, irq;
  130. dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1);
  131. if (pdata == NULL || pdata->isink[id] == NULL)
  132. return -ENODEV;
  133. isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink),
  134. GFP_KERNEL);
  135. if (!isink)
  136. return -ENOMEM;
  137. isink->wm831x = wm831x;
  138. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  139. if (res == NULL) {
  140. dev_err(&pdev->dev, "No REG resource\n");
  141. ret = -EINVAL;
  142. goto err;
  143. }
  144. isink->reg = res->start;
  145. /* For current parts this is correct; probably need to revisit
  146. * in future.
  147. */
  148. snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1);
  149. isink->desc.name = isink->name;
  150. isink->desc.id = id;
  151. isink->desc.ops = &wm831x_isink_ops;
  152. isink->desc.type = REGULATOR_CURRENT;
  153. isink->desc.owner = THIS_MODULE;
  154. config.dev = pdev->dev.parent;
  155. config.init_data = pdata->isink[id];
  156. config.driver_data = isink;
  157. isink->regulator = devm_regulator_register(&pdev->dev, &isink->desc,
  158. &config);
  159. if (IS_ERR(isink->regulator)) {
  160. ret = PTR_ERR(isink->regulator);
  161. dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n",
  162. id + 1, ret);
  163. goto err;
  164. }
  165. irq = wm831x_irq(wm831x, platform_get_irq(pdev, 0));
  166. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  167. wm831x_isink_irq,
  168. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  169. isink->name,
  170. isink);
  171. if (ret != 0) {
  172. dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n",
  173. irq, ret);
  174. goto err;
  175. }
  176. platform_set_drvdata(pdev, isink);
  177. return 0;
  178. err:
  179. return ret;
  180. }
  181. static struct platform_driver wm831x_isink_driver = {
  182. .probe = wm831x_isink_probe,
  183. .driver = {
  184. .name = "wm831x-isink",
  185. },
  186. };
  187. static int __init wm831x_isink_init(void)
  188. {
  189. int ret;
  190. ret = platform_driver_register(&wm831x_isink_driver);
  191. if (ret != 0)
  192. pr_err("Failed to register WM831x ISINK driver: %d\n", ret);
  193. return ret;
  194. }
  195. subsys_initcall(wm831x_isink_init);
  196. static void __exit wm831x_isink_exit(void)
  197. {
  198. platform_driver_unregister(&wm831x_isink_driver);
  199. }
  200. module_exit(wm831x_isink_exit);
  201. /* Module information */
  202. MODULE_AUTHOR("Mark Brown");
  203. MODULE_DESCRIPTION("WM831x current sink driver");
  204. MODULE_LICENSE("GPL");
  205. MODULE_ALIAS("platform:wm831x-isink");