leds.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /**
  162. * led_trigger_rename_static - rename a trigger
  163. * @name: the new trigger name
  164. * @trig: the LED trigger to rename
  165. *
  166. * Change a LED trigger name by copying the string passed in
  167. * name into current trigger name, which MUST be large
  168. * enough for the new string.
  169. *
  170. * Note that name must NOT point to the same string used
  171. * during LED registration, as that could lead to races.
  172. *
  173. * This is meant to be used on triggers with statically
  174. * allocated name.
  175. */
  176. extern void led_trigger_rename_static(const char *name,
  177. struct led_trigger *trig);
  178. #else
  179. /* Triggers aren't active - null macros */
  180. #define DEFINE_LED_TRIGGER(x)
  181. #define DEFINE_LED_TRIGGER_GLOBAL(x)
  182. #define led_trigger_register_simple(x, y) do {} while(0)
  183. #define led_trigger_unregister_simple(x) do {} while(0)
  184. #define led_trigger_event(x, y) do {} while(0)
  185. #endif
  186. /* Trigger specific functions */
  187. #ifdef CONFIG_LEDS_TRIGGER_IDE_DISK
  188. extern void ledtrig_ide_activity(void);
  189. #else
  190. #define ledtrig_ide_activity() do {} while(0)
  191. #endif
  192. /*
  193. * Generic LED platform data for describing LED names and default triggers.
  194. */
  195. struct led_info {
  196. const char *name;
  197. const char *default_trigger;
  198. int flags;
  199. };
  200. struct led_platform_data {
  201. int num_leds;
  202. struct led_info *leds;
  203. };
  204. /* For the leds-gpio driver */
  205. struct gpio_led {
  206. const char *name;
  207. const char *default_trigger;
  208. unsigned gpio;
  209. unsigned active_low : 1;
  210. unsigned retain_state_suspended : 1;
  211. unsigned default_state : 2;
  212. /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
  213. };
  214. #define LEDS_GPIO_DEFSTATE_OFF 0
  215. #define LEDS_GPIO_DEFSTATE_ON 1
  216. #define LEDS_GPIO_DEFSTATE_KEEP 2
  217. struct gpio_led_platform_data {
  218. int num_leds;
  219. const struct gpio_led *leds;
  220. #define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
  221. #define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
  222. #define GPIO_LED_BLINK 2 /* Please, blink */
  223. int (*gpio_blink_set)(unsigned gpio, int state,
  224. unsigned long *delay_on,
  225. unsigned long *delay_off);
  226. };
  227. struct platform_device *gpio_led_register_device(
  228. int id, const struct gpio_led_platform_data *pdata);
  229. enum cpu_led_event {
  230. CPU_LED_IDLE_START, /* CPU enters idle */
  231. CPU_LED_IDLE_END, /* CPU idle ends */
  232. CPU_LED_START, /* Machine starts, especially resume */
  233. CPU_LED_STOP, /* Machine stops, especially suspend */
  234. CPU_LED_HALTED, /* Machine shutdown */
  235. };
  236. #ifdef CONFIG_LEDS_TRIGGER_CPU
  237. extern void ledtrig_cpu(enum cpu_led_event evt);
  238. #else
  239. static inline void ledtrig_cpu(enum cpu_led_event evt)
  240. {
  241. return;
  242. }
  243. #endif
  244. #endif /* __LINUX_LEDS_H_INCLUDED */