leds.h 7.6 KB

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