wm831x-isink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 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 __devinit int wm831x_isink_probe(struct platform_device *pdev)
  122. {
  123. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  124. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  125. struct wm831x_isink *isink;
  126. int id = pdev->id % ARRAY_SIZE(pdata->isink);
  127. struct resource *res;
  128. int ret, irq;
  129. dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1);
  130. if (pdata == NULL || pdata->isink[id] == NULL)
  131. return -ENODEV;
  132. isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink),
  133. GFP_KERNEL);
  134. if (isink == NULL) {
  135. dev_err(&pdev->dev, "Unable to allocate private data\n");
  136. return -ENOMEM;
  137. }
  138. isink->wm831x = wm831x;
  139. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  140. if (res == NULL) {
  141. dev_err(&pdev->dev, "No I/O resource\n");
  142. ret = -EINVAL;
  143. goto err;
  144. }
  145. isink->reg = res->start;
  146. /* For current parts this is correct; probably need to revisit
  147. * in future.
  148. */
  149. snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1);
  150. isink->desc.name = isink->name;
  151. isink->desc.id = id;
  152. isink->desc.ops = &wm831x_isink_ops;
  153. isink->desc.type = REGULATOR_CURRENT;
  154. isink->desc.owner = THIS_MODULE;
  155. isink->regulator = regulator_register(&isink->desc, &pdev->dev,
  156. pdata->isink[id], isink, NULL);
  157. if (IS_ERR(isink->regulator)) {
  158. ret = PTR_ERR(isink->regulator);
  159. dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n",
  160. id + 1, ret);
  161. goto err;
  162. }
  163. irq = platform_get_irq(pdev, 0);
  164. ret = request_threaded_irq(irq, NULL, wm831x_isink_irq,
  165. IRQF_TRIGGER_RISING, isink->name, isink);
  166. if (ret != 0) {
  167. dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n",
  168. irq, ret);
  169. goto err_regulator;
  170. }
  171. platform_set_drvdata(pdev, isink);
  172. return 0;
  173. err_regulator:
  174. regulator_unregister(isink->regulator);
  175. err:
  176. return ret;
  177. }
  178. static __devexit int wm831x_isink_remove(struct platform_device *pdev)
  179. {
  180. struct wm831x_isink *isink = platform_get_drvdata(pdev);
  181. platform_set_drvdata(pdev, NULL);
  182. free_irq(platform_get_irq(pdev, 0), isink);
  183. regulator_unregister(isink->regulator);
  184. return 0;
  185. }
  186. static struct platform_driver wm831x_isink_driver = {
  187. .probe = wm831x_isink_probe,
  188. .remove = __devexit_p(wm831x_isink_remove),
  189. .driver = {
  190. .name = "wm831x-isink",
  191. .owner = THIS_MODULE,
  192. },
  193. };
  194. static int __init wm831x_isink_init(void)
  195. {
  196. int ret;
  197. ret = platform_driver_register(&wm831x_isink_driver);
  198. if (ret != 0)
  199. pr_err("Failed to register WM831x ISINK driver: %d\n", ret);
  200. return ret;
  201. }
  202. subsys_initcall(wm831x_isink_init);
  203. static void __exit wm831x_isink_exit(void)
  204. {
  205. platform_driver_unregister(&wm831x_isink_driver);
  206. }
  207. module_exit(wm831x_isink_exit);
  208. /* Module information */
  209. MODULE_AUTHOR("Mark Brown");
  210. MODULE_DESCRIPTION("WM831x current sink driver");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:wm831x-isink");