leds-regulator.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 regulator_led_probe(struct platform_device *pdev)
  113. {
  114. struct led_regulator_platform_data *pdata =
  115. dev_get_platdata(&pdev->dev);
  116. struct regulator_led *led;
  117. struct regulator *vcc;
  118. int ret = 0;
  119. if (pdata == NULL) {
  120. dev_err(&pdev->dev, "no platform data\n");
  121. return -ENODEV;
  122. }
  123. vcc = devm_regulator_get_exclusive(&pdev->dev, "vled");
  124. if (IS_ERR(vcc)) {
  125. dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
  126. return PTR_ERR(vcc);
  127. }
  128. led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
  129. if (led == NULL)
  130. return -ENOMEM;
  131. led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
  132. if (pdata->brightness > led->cdev.max_brightness) {
  133. dev_err(&pdev->dev, "Invalid default brightness %d\n",
  134. pdata->brightness);
  135. return -EINVAL;
  136. }
  137. led->value = pdata->brightness;
  138. led->cdev.brightness_set = regulator_led_brightness_set;
  139. led->cdev.name = pdata->name;
  140. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  141. led->vcc = vcc;
  142. /* to handle correctly an already enabled regulator */
  143. if (regulator_is_enabled(led->vcc))
  144. led->enabled = 1;
  145. mutex_init(&led->mutex);
  146. INIT_WORK(&led->work, led_work);
  147. platform_set_drvdata(pdev, led);
  148. ret = led_classdev_register(&pdev->dev, &led->cdev);
  149. if (ret < 0) {
  150. cancel_work_sync(&led->work);
  151. return ret;
  152. }
  153. /* to expose the default value to userspace */
  154. led->cdev.brightness = led->value;
  155. /* Set the default led status */
  156. regulator_led_set_value(led);
  157. return 0;
  158. }
  159. static int regulator_led_remove(struct platform_device *pdev)
  160. {
  161. struct regulator_led *led = platform_get_drvdata(pdev);
  162. led_classdev_unregister(&led->cdev);
  163. cancel_work_sync(&led->work);
  164. regulator_led_disable(led);
  165. return 0;
  166. }
  167. static struct platform_driver regulator_led_driver = {
  168. .driver = {
  169. .name = "leds-regulator",
  170. },
  171. .probe = regulator_led_probe,
  172. .remove = regulator_led_remove,
  173. };
  174. module_platform_driver(regulator_led_driver);
  175. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  176. MODULE_DESCRIPTION("Regulator driven LED driver");
  177. MODULE_LICENSE("GPL");
  178. MODULE_ALIAS("platform:leds-regulator");