leds.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. const struct attribute_group **groups;
  59. struct list_head node; /* LED Device list */
  60. const char *default_trigger; /* Trigger to use */
  61. unsigned long blink_delay_on, blink_delay_off;
  62. struct timer_list blink_timer;
  63. int blink_brightness;
  64. struct work_struct set_brightness_work;
  65. int delayed_set_value;
  66. #ifdef CONFIG_LEDS_TRIGGERS
  67. /* Protects the trigger data below */
  68. struct rw_semaphore trigger_lock;
  69. struct led_trigger *trigger;
  70. struct list_head trig_list;
  71. void *trigger_data;
  72. /* true if activated - deactivate routine uses it to do cleanup */
  73. bool activated;
  74. #endif
  75. };
  76. extern int led_classdev_register(struct device *parent,
  77. struct led_classdev *led_cdev);
  78. extern void led_classdev_unregister(struct led_classdev *led_cdev);
  79. extern void led_classdev_suspend(struct led_classdev *led_cdev);
  80. extern void led_classdev_resume(struct led_classdev *led_cdev);
  81. /**
  82. * led_blink_set - set blinking with software fallback
  83. * @led_cdev: the LED to start blinking
  84. * @delay_on: the time it should be on (in ms)
  85. * @delay_off: the time it should ble off (in ms)
  86. *
  87. * This function makes the LED blink, attempting to use the
  88. * hardware acceleration if possible, but falling back to
  89. * software blinking if there is no hardware blinking or if
  90. * the LED refuses the passed values.
  91. *
  92. * Note that if software blinking is active, simply calling
  93. * led_cdev->brightness_set() will not stop the blinking,
  94. * use led_classdev_brightness_set() instead.
  95. */
  96. extern void led_blink_set(struct led_classdev *led_cdev,
  97. unsigned long *delay_on,
  98. unsigned long *delay_off);
  99. /**
  100. * led_blink_set_oneshot - do a oneshot software blink
  101. * @led_cdev: the LED to start blinking
  102. * @delay_on: the time it should be on (in ms)
  103. * @delay_off: the time it should ble off (in ms)
  104. * @invert: blink off, then on, leaving the led on
  105. *
  106. * This function makes the LED blink one time for delay_on +
  107. * delay_off time, ignoring the request if another one-shot
  108. * blink is already in progress.
  109. *
  110. * If invert is set, led blinks for delay_off first, then for
  111. * delay_on and leave the led on after the on-off cycle.
  112. */
  113. extern void led_blink_set_oneshot(struct led_classdev *led_cdev,
  114. unsigned long *delay_on,
  115. unsigned long *delay_off,
  116. int invert);
  117. /**
  118. * led_set_brightness - set LED brightness
  119. * @led_cdev: the LED to set
  120. * @brightness: the brightness to set it to
  121. *
  122. * Set an LED's brightness, and, if necessary, cancel the
  123. * software blink timer that implements blinking when the
  124. * hardware doesn't.
  125. */
  126. extern void led_set_brightness(struct led_classdev *led_cdev,
  127. enum led_brightness brightness);
  128. /*
  129. * LED Triggers
  130. */
  131. /* Registration functions for simple triggers */
  132. #define DEFINE_LED_TRIGGER(x) static struct led_trigger *x;
  133. #define DEFINE_LED_TRIGGER_GLOBAL(x) struct led_trigger *x;
  134. #ifdef CONFIG_LEDS_TRIGGERS
  135. #define TRIG_NAME_MAX 50
  136. struct led_trigger {
  137. /* Trigger Properties */
  138. const char *name;
  139. void (*activate)(struct led_classdev *led_cdev);
  140. void (*deactivate)(struct led_classdev *led_cdev);
  141. /* LEDs under control by this trigger (for simple triggers) */
  142. rwlock_t leddev_list_lock;
  143. struct list_head led_cdevs;
  144. /* Link to next registered trigger */
  145. struct list_head next_trig;
  146. };
  147. /* Registration functions for complex triggers */
  148. extern int led_trigger_register(struct led_trigger *trigger);
  149. extern void led_trigger_unregister(struct led_trigger *trigger);
  150. extern void led_trigger_register_simple(const char *name,
  151. struct led_trigger **trigger);
  152. extern void led_trigger_unregister_simple(struct led_trigger *trigger);
  153. extern void led_trigger_event(struct led_trigger *trigger,
  154. enum led_brightness event);
  155. extern void led_trigger_blink(struct led_trigger *trigger,
  156. unsigned long *delay_on,
  157. unsigned long *delay_off);
  158. extern void led_trigger_blink_oneshot(struct led_trigger *trigger,
  159. unsigned long *delay_on,
  160. unsigned long *delay_off,
  161. int invert);
  162. /**
  163. * led_trigger_rename_static - rename a trigger
  164. * @name: the new trigger name
  165. * @trig: the LED trigger to rename
  166. *
  167. * Change a LED trigger name by copying the string passed in
  168. * name into current trigger name, which MUST be large
  169. * enough for the new string.
  170. *
  171. * Note that name must NOT point to the same string used
  172. * during LED registration, as that could lead to races.
  173. *
  174. * This is meant to be used on triggers with statically
  175. * allocated name.
  176. */
  177. extern void led_trigger_rename_static(const char *name,
  178. struct led_trigger *trig);
  179. #else
  180. /* Trigger has no members */
  181. struct led_trigger {};
  182. /* Trigger inline empty functions */
  183. static inline void led_trigger_register_simple(const char *name,
  184. struct led_trigger **trigger) {}
  185. static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
  186. static inline void led_trigger_event(struct led_trigger *trigger,
  187. enum led_brightness event) {}
  188. #endif /* CONFIG_LEDS_TRIGGERS */
  189. /* Trigger specific functions */
  190. #ifdef CONFIG_LEDS_TRIGGER_IDE_DISK
  191. extern void ledtrig_ide_activity(void);
  192. #else
  193. static inline void ledtrig_ide_activity(void) {}
  194. #endif
  195. #if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
  196. extern void ledtrig_flash_ctrl(bool on);
  197. extern void ledtrig_torch_ctrl(bool on);
  198. #else
  199. static inline void ledtrig_flash_ctrl(bool on) {}
  200. static inline void ledtrig_torch_ctrl(bool on) {}
  201. #endif
  202. /*
  203. * Generic LED platform data for describing LED names and default triggers.
  204. */
  205. struct led_info {
  206. const char *name;
  207. const char *default_trigger;
  208. int flags;
  209. };
  210. struct led_platform_data {
  211. int num_leds;
  212. struct led_info *leds;
  213. };
  214. /* For the leds-gpio driver */
  215. struct gpio_led {
  216. const char *name;
  217. const char *default_trigger;
  218. unsigned gpio;
  219. unsigned active_low : 1;
  220. unsigned retain_state_suspended : 1;
  221. unsigned default_state : 2;
  222. /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
  223. };
  224. #define LEDS_GPIO_DEFSTATE_OFF 0
  225. #define LEDS_GPIO_DEFSTATE_ON 1
  226. #define LEDS_GPIO_DEFSTATE_KEEP 2
  227. struct gpio_led_platform_data {
  228. int num_leds;
  229. const struct gpio_led *leds;
  230. #define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
  231. #define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
  232. #define GPIO_LED_BLINK 2 /* Please, blink */
  233. int (*gpio_blink_set)(unsigned gpio, int state,
  234. unsigned long *delay_on,
  235. unsigned long *delay_off);
  236. };
  237. struct platform_device *gpio_led_register_device(
  238. int id, const struct gpio_led_platform_data *pdata);
  239. enum cpu_led_event {
  240. CPU_LED_IDLE_START, /* CPU enters idle */
  241. CPU_LED_IDLE_END, /* CPU idle ends */
  242. CPU_LED_START, /* Machine starts, especially resume */
  243. CPU_LED_STOP, /* Machine stops, especially suspend */
  244. CPU_LED_HALTED, /* Machine shutdown */
  245. };
  246. #ifdef CONFIG_LEDS_TRIGGER_CPU
  247. extern void ledtrig_cpu(enum cpu_led_event evt);
  248. #else
  249. static inline void ledtrig_cpu(enum cpu_led_event evt)
  250. {
  251. return;
  252. }
  253. #endif
  254. #endif /* __LINUX_LEDS_H_INCLUDED */