leds-regulator.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/leds.h>
  17. #include <linux/leds-regulator.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #define to_regulator_led(led_cdev) \
  21. container_of(led_cdev, struct regulator_led, cdev)
  22. struct regulator_led {
  23. struct led_classdev cdev;
  24. int enabled;
  25. struct mutex mutex;
  26. struct regulator *vcc;
  27. };
  28. static inline int led_regulator_get_max_brightness(struct regulator *supply)
  29. {
  30. int ret;
  31. int voltage = regulator_list_voltage(supply, 0);
  32. if (voltage <= 0)
  33. return 1;
  34. /* even if regulator can't change voltages,
  35. * we still assume it can change status
  36. * and the LED can be turned on and off.
  37. */
  38. ret = regulator_set_voltage(supply, voltage, voltage);
  39. if (ret < 0)
  40. return 1;
  41. return regulator_count_voltages(supply);
  42. }
  43. static int led_regulator_get_voltage(struct regulator *supply,
  44. enum led_brightness brightness)
  45. {
  46. if (brightness == 0)
  47. return -EINVAL;
  48. return regulator_list_voltage(supply, brightness - 1);
  49. }
  50. static void regulator_led_enable(struct regulator_led *led)
  51. {
  52. int ret;
  53. if (led->enabled)
  54. return;
  55. ret = regulator_enable(led->vcc);
  56. if (ret != 0) {
  57. dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
  58. return;
  59. }
  60. led->enabled = 1;
  61. }
  62. static void regulator_led_disable(struct regulator_led *led)
  63. {
  64. int ret;
  65. if (!led->enabled)
  66. return;
  67. ret = regulator_disable(led->vcc);
  68. if (ret != 0) {
  69. dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
  70. return;
  71. }
  72. led->enabled = 0;
  73. }
  74. static int regulator_led_brightness_set(struct led_classdev *led_cdev,
  75. enum led_brightness value)
  76. {
  77. struct regulator_led *led = to_regulator_led(led_cdev);
  78. int voltage;
  79. int ret = 0;
  80. mutex_lock(&led->mutex);
  81. if (value == LED_OFF) {
  82. regulator_led_disable(led);
  83. goto out;
  84. }
  85. if (led->cdev.max_brightness > 1) {
  86. voltage = led_regulator_get_voltage(led->vcc, value);
  87. dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
  88. value, voltage);
  89. ret = regulator_set_voltage(led->vcc, voltage, voltage);
  90. if (ret != 0)
  91. dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
  92. voltage, ret);
  93. }
  94. regulator_led_enable(led);
  95. out:
  96. mutex_unlock(&led->mutex);
  97. return ret;
  98. }
  99. static int regulator_led_probe(struct platform_device *pdev)
  100. {
  101. struct led_regulator_platform_data *pdata =
  102. dev_get_platdata(&pdev->dev);
  103. struct regulator_led *led;
  104. struct regulator *vcc;
  105. int ret = 0;
  106. if (pdata == NULL) {
  107. dev_err(&pdev->dev, "no platform data\n");
  108. return -ENODEV;
  109. }
  110. vcc = devm_regulator_get_exclusive(&pdev->dev, "vled");
  111. if (IS_ERR(vcc)) {
  112. dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
  113. return PTR_ERR(vcc);
  114. }
  115. led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
  116. if (led == NULL)
  117. return -ENOMEM;
  118. led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
  119. if (pdata->brightness > led->cdev.max_brightness) {
  120. dev_err(&pdev->dev, "Invalid default brightness %d\n",
  121. pdata->brightness);
  122. return -EINVAL;
  123. }
  124. led->cdev.brightness_set_blocking = regulator_led_brightness_set;
  125. led->cdev.name = pdata->name;
  126. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  127. led->vcc = vcc;
  128. /* to handle correctly an already enabled regulator */
  129. if (regulator_is_enabled(led->vcc))
  130. led->enabled = 1;
  131. mutex_init(&led->mutex);
  132. platform_set_drvdata(pdev, led);
  133. ret = led_classdev_register(&pdev->dev, &led->cdev);
  134. if (ret < 0)
  135. return ret;
  136. /* to expose the default value to userspace */
  137. led->cdev.brightness = pdata->brightness;
  138. /* Set the default led status */
  139. regulator_led_brightness_set(&led->cdev, led->cdev.brightness);
  140. return 0;
  141. }
  142. static int regulator_led_remove(struct platform_device *pdev)
  143. {
  144. struct regulator_led *led = platform_get_drvdata(pdev);
  145. led_classdev_unregister(&led->cdev);
  146. regulator_led_disable(led);
  147. return 0;
  148. }
  149. static struct platform_driver regulator_led_driver = {
  150. .driver = {
  151. .name = "leds-regulator",
  152. },
  153. .probe = regulator_led_probe,
  154. .remove = regulator_led_remove,
  155. };
  156. module_platform_driver(regulator_led_driver);
  157. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  158. MODULE_DESCRIPTION("Regulator driven LED driver");
  159. MODULE_LICENSE("GPL");
  160. MODULE_ALIAS("platform:leds-regulator");