leds-regulator.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * leds-regulator.c - LED class driver for regulator driven LEDs.
  3. *
  4. * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
  5. *
  6. * Inspired by leds-wm8350 driver.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/leds.h>
  18. #include <linux/leds-regulator.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #define to_regulator_led(led_cdev) \
  22. container_of(led_cdev, struct regulator_led, cdev)
  23. struct regulator_led {
  24. struct led_classdev cdev;
  25. enum led_brightness value;
  26. int enabled;
  27. struct mutex mutex;
  28. struct work_struct work;
  29. struct regulator *vcc;
  30. };
  31. static inline int led_regulator_get_max_brightness(struct regulator *supply)
  32. {
  33. int ret;
  34. int voltage = regulator_list_voltage(supply, 0);
  35. if (voltage <= 0)
  36. return 1;
  37. /* even if regulator can't change voltages,
  38. * we still assume it can change status
  39. * and the LED can be turned on and off.
  40. */
  41. ret = regulator_set_voltage(supply, voltage, voltage);
  42. if (ret < 0)
  43. return 1;
  44. return regulator_count_voltages(supply);
  45. }
  46. static int led_regulator_get_voltage(struct regulator *supply,
  47. enum led_brightness brightness)
  48. {
  49. if (brightness == 0)
  50. return -EINVAL;
  51. return regulator_list_voltage(supply, brightness - 1);
  52. }
  53. static void regulator_led_enable(struct regulator_led *led)
  54. {
  55. int ret;
  56. if (led->enabled)
  57. return;
  58. ret = regulator_enable(led->vcc);
  59. if (ret != 0) {
  60. dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
  61. return;
  62. }
  63. led->enabled = 1;
  64. }
  65. static void regulator_led_disable(struct regulator_led *led)
  66. {
  67. int ret;
  68. if (!led->enabled)
  69. return;
  70. ret = regulator_disable(led->vcc);
  71. if (ret != 0) {
  72. dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
  73. return;
  74. }
  75. led->enabled = 0;
  76. }
  77. static void regulator_led_set_value(struct regulator_led *led)
  78. {
  79. int voltage;
  80. int ret;
  81. mutex_lock(&led->mutex);
  82. if (led->value == LED_OFF) {
  83. regulator_led_disable(led);
  84. goto out;
  85. }
  86. if (led->cdev.max_brightness > 1) {
  87. voltage = led_regulator_get_voltage(led->vcc, led->value);
  88. dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
  89. led->value, voltage);
  90. ret = regulator_set_voltage(led->vcc, voltage, voltage);
  91. if (ret != 0)
  92. dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
  93. voltage, ret);
  94. }
  95. regulator_led_enable(led);
  96. out:
  97. mutex_unlock(&led->mutex);
  98. }
  99. static void led_work(struct work_struct *work)
  100. {
  101. struct regulator_led *led;
  102. led = container_of(work, struct regulator_led, work);
  103. regulator_led_set_value(led);
  104. }
  105. static void regulator_led_brightness_set(struct led_classdev *led_cdev,
  106. enum led_brightness value)
  107. {
  108. struct regulator_led *led = to_regulator_led(led_cdev);
  109. led->value = value;
  110. schedule_work(&led->work);
  111. }
  112. static int __devinit regulator_led_probe(struct platform_device *pdev)
  113. {
  114. struct led_regulator_platform_data *pdata = pdev->dev.platform_data;
  115. struct regulator_led *led;
  116. struct regulator *vcc;
  117. int ret = 0;
  118. if (pdata == NULL) {
  119. dev_err(&pdev->dev, "no platform data\n");
  120. return -ENODEV;
  121. }
  122. vcc = regulator_get_exclusive(&pdev->dev, "vled");
  123. if (IS_ERR(vcc)) {
  124. dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
  125. return PTR_ERR(vcc);
  126. }
  127. led = kzalloc(sizeof(*led), GFP_KERNEL);
  128. if (led == NULL) {
  129. ret = -ENOMEM;
  130. goto err_vcc;
  131. }
  132. led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
  133. if (pdata->brightness > led->cdev.max_brightness) {
  134. dev_err(&pdev->dev, "Invalid default brightness %d\n",
  135. pdata->brightness);
  136. ret = -EINVAL;
  137. goto err_led;
  138. }
  139. led->value = pdata->brightness;
  140. led->cdev.brightness_set = regulator_led_brightness_set;
  141. led->cdev.name = pdata->name;
  142. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  143. led->vcc = vcc;
  144. /* to handle correctly an already enabled regulator */
  145. if (regulator_is_enabled(led->vcc))
  146. led->enabled = 1;
  147. mutex_init(&led->mutex);
  148. INIT_WORK(&led->work, led_work);
  149. platform_set_drvdata(pdev, led);
  150. ret = led_classdev_register(&pdev->dev, &led->cdev);
  151. if (ret < 0) {
  152. cancel_work_sync(&led->work);
  153. goto err_led;
  154. }
  155. /* to expose the default value to userspace */
  156. led->cdev.brightness = led->value;
  157. /* Set the default led status */
  158. regulator_led_set_value(led);
  159. return 0;
  160. err_led:
  161. kfree(led);
  162. err_vcc:
  163. regulator_put(vcc);
  164. return ret;
  165. }
  166. static int __devexit regulator_led_remove(struct platform_device *pdev)
  167. {
  168. struct regulator_led *led = platform_get_drvdata(pdev);
  169. led_classdev_unregister(&led->cdev);
  170. cancel_work_sync(&led->work);
  171. regulator_led_disable(led);
  172. regulator_put(led->vcc);
  173. kfree(led);
  174. return 0;
  175. }
  176. static struct platform_driver regulator_led_driver = {
  177. .driver = {
  178. .name = "leds-regulator",
  179. .owner = THIS_MODULE,
  180. },
  181. .probe = regulator_led_probe,
  182. .remove = __devexit_p(regulator_led_remove),
  183. };
  184. static int __init regulator_led_init(void)
  185. {
  186. return platform_driver_register(&regulator_led_driver);
  187. }
  188. module_init(regulator_led_init);
  189. static void __exit regulator_led_exit(void)
  190. {
  191. platform_driver_unregister(&regulator_led_driver);
  192. }
  193. module_exit(regulator_led_exit);
  194. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  195. MODULE_DESCRIPTION("Regulator driven LED driver");
  196. MODULE_LICENSE("GPL");
  197. MODULE_ALIAS("platform:leds-regulator");