leds.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Driver model for leds and led triggers
  3. *
  4. * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
  5. * Copyright (C) 2005 Richard Purdie <rpurdie@openedhand.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #ifndef __LINUX_LEDS_H_INCLUDED
  13. #define __LINUX_LEDS_H_INCLUDED
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/timer.h>
  18. struct device;
  19. /*
  20. * LED Core
  21. */
  22. enum led_brightness {
  23. LED_OFF = 0,
  24. LED_HALF = 127,
  25. LED_FULL = 255,
  26. };
  27. struct led_classdev {
  28. const char *name;
  29. int brightness;
  30. int max_brightness;
  31. int flags;
  32. /* Lower 16 bits reflect status */
  33. #define LED_SUSPENDED (1 << 0)
  34. /* Upper 16 bits reflect control information */
  35. #define LED_CORE_SUSPENDRESUME (1 << 16)
  36. #define LED_BLINK_ONESHOT (1 << 17)
  37. #define LED_BLINK_ONESHOT_STOP (1 << 18)
  38. #define LED_BLINK_INVERT (1 << 19)
  39. /* Set LED brightness level */
  40. /* Must not sleep, use a workqueue if needed */
  41. void (*brightness_set)(struct led_classdev *led_cdev,
  42. enum led_brightness brightness);
  43. /* Get LED brightness level */
  44. enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
  45. /*
  46. * Activate hardware accelerated blink, delays are in milliseconds
  47. * and if both are zero then a sensible default should be chosen.
  48. * The call should adjust the timings in that case and if it can't
  49. * match the values specified exactly.
  50. * Deactivate blinking again when the brightness is set to a fixed
  51. * value via the brightness_set() callback.
  52. */
  53. int (*blink_set)(struct led_classdev *led_cdev,
  54. unsigned long *delay_on,
  55. unsigned long *delay_off);
  56. struct device *dev;
  57. struct list_head node; /* LED Device list */
  58. const char *default_trigger; /* Trigger to use */
  59. unsigned long blink_delay_on, blink_delay_off;
  60. struct timer_list blink_timer;
  61. int blink_brightness;
  62. #ifdef CONFIG_LEDS_TRIGGERS
  63. /* Protects the trigger data below */
  64. struct rw_semaphore trigger_lock;
  65. struct led_trigger *trigger;
  66. struct list_head trig_list;
  67. void *trigger_data;
  68. /* true if activated - deactivate routine uses it to do cleanup */
  69. bool activated;
  70. #endif
  71. };
  72. extern int led_classdev_register(struct device *parent,
  73. struct led_classdev *led_cdev);
  74. extern void led_classdev_unregister(struct led_classdev *led_cdev);
  75. extern void led_classdev_suspend(struct led_classdev *led_cdev);
  76. extern void led_classdev_resume(struct led_classdev *led_cdev);
  77. /**
  78. * led_blink_set - set blinking with software fallback
  79. * @led_cdev: the LED to start blinking
  80. * @delay_on: the time it should be on (in ms)
  81. * @delay_off: the time it should ble off (in ms)
  82. *
  83. * This function makes the LED blink, attempting to use the
  84. * hardware acceleration if possible, but falling back to
  85. * software blinking if there is no hardware blinking or if
  86. * the LED refuses the passed values.
  87. *
  88. * Note that if software blinking is active, simply calling
  89. * led_cdev->brightness_set() will not stop the blinking,
  90. * use led_classdev_brightness_set() instead.
  91. */
  92. extern void led_blink_set(struct led_classdev *led_cdev,
  93. unsigned long *delay_on,
  94. unsigned long *delay_off);
  95. /**
  96. * led_blink_set_oneshot - do a oneshot software blink
  97. * @led_cdev: the LED to start blinking
  98. * @delay_on: the time it should be on (in ms)
  99. * @delay_off: the time it should ble off (in ms)
  100. * @invert: blink off, then on, leaving the led on
  101. *
  102. * This function makes the LED blink one time for delay_on +
  103. * delay_off time, ignoring the request if another one-shot
  104. * blink is already in progress.
  105. *
  106. * If invert is set, led blinks for delay_off first, then for
  107. * delay_on and leave the led on after the on-off cycle.
  108. */
  109. extern void led_blink_set_oneshot(struct led_classdev *led_cdev,
  110. unsigned long *delay_on,
  111. unsigned long *delay_off,
  112. int invert);
  113. /**
  114. * led_brightness_set - set LED brightness
  115. * @led_cdev: the LED to set
  116. * @brightness: the brightness to set it to
  117. *
  118. * Set an LED's brightness, and, if necessary, cancel the
  119. * software blink timer that implements blinking when the
  120. * hardware doesn't.
  121. */
  122. extern void led_brightness_set(struct led_classdev *led_cdev,
  123. enum led_brightness brightness);
  124. /*
  125. * LED Triggers
  126. */
  127. #ifdef CONFIG_LEDS_TRIGGERS
  128. #define TRIG_NAME_MAX 50
  129. struct led_trigger {
  130. /* Trigger Properties */
  131. const char *name;
  132. void (*activate)(struct led_classdev *led_cdev);
  133. void (*deactivate)(struct led_classdev *led_cdev);
  134. /* LEDs under control by this trigger (for simple triggers) */
  135. rwlock_t leddev_list_lock;
  136. struct list_head led_cdevs;
  137. /* Link to next registered trigger */
  138. struct list_head next_trig;
  139. };
  140. /* Registration functions for complex triggers */
  141. extern int led_trigger_register(struct led_trigger *trigger);
  142. extern void led_trigger_unregister(struct led_trigger *trigger);
  143. /* Registration functions for simple triggers */
  144. #define DEFINE_LED_TRIGGER(x) static struct led_trigger *x;
  145. #define DEFINE_LED_TRIGGER_GLOBAL(x) struct led_trigger *x;
  146. extern void led_trigger_register_simple(const char *name,
  147. struct led_trigger **trigger);
  148. extern void led_trigger_unregister_simple(struct led_trigger *trigger);
  149. extern void led_trigger_event(struct led_trigger *trigger,
  150. enum led_brightness event);
  151. extern void led_trigger_blink(struct led_trigger *trigger,
  152. unsigned long *delay_on,
  153. unsigned long *delay_off);
  154. extern void led_trigger_blink_oneshot(struct led_trigger *trigger,
  155. unsigned long *delay_on,
  156. unsigned long *delay_off,
  157. int invert);
  158. #else
  159. /* Triggers aren't active - null macros */
  160. #define DEFINE_LED_TRIGGER(x)
  161. #define DEFINE_LED_TRIGGER_GLOBAL(x)
  162. #define led_trigger_register_simple(x, y) do {} while(0)
  163. #define led_trigger_unregister_simple(x) do {} while(0)
  164. #define led_trigger_event(x, y) do {} while(0)
  165. #endif
  166. /* Trigger specific functions */
  167. #ifdef CONFIG_LEDS_TRIGGER_IDE_DISK
  168. extern void ledtrig_ide_activity(void);
  169. #else
  170. #define ledtrig_ide_activity() do {} while(0)
  171. #endif
  172. /*
  173. * Generic LED platform data for describing LED names and default triggers.
  174. */
  175. struct led_info {
  176. const char *name;
  177. const char *default_trigger;
  178. int flags;
  179. };
  180. struct led_platform_data {
  181. int num_leds;
  182. struct led_info *leds;
  183. };
  184. /* For the leds-gpio driver */
  185. struct gpio_led {
  186. const char *name;
  187. const char *default_trigger;
  188. unsigned gpio;
  189. unsigned active_low : 1;
  190. unsigned retain_state_suspended : 1;
  191. unsigned default_state : 2;
  192. /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
  193. };
  194. #define LEDS_GPIO_DEFSTATE_OFF 0
  195. #define LEDS_GPIO_DEFSTATE_ON 1
  196. #define LEDS_GPIO_DEFSTATE_KEEP 2
  197. struct gpio_led_platform_data {
  198. int num_leds;
  199. const struct gpio_led *leds;
  200. #define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
  201. #define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
  202. #define GPIO_LED_BLINK 2 /* Please, blink */
  203. int (*gpio_blink_set)(unsigned gpio, int state,
  204. unsigned long *delay_on,
  205. unsigned long *delay_off);
  206. };
  207. struct platform_device *gpio_led_register_device(
  208. int id, const struct gpio_led_platform_data *pdata);
  209. #endif /* __LINUX_LEDS_H_INCLUDED */