leds.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/mutex.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/timer.h>
  19. #include <linux/workqueue.h>
  20. struct device;
  21. /*
  22. * LED Core
  23. */
  24. enum led_brightness {
  25. LED_OFF = 0,
  26. LED_HALF = 127,
  27. LED_FULL = 255,
  28. };
  29. struct led_classdev {
  30. const char *name;
  31. enum led_brightness brightness;
  32. enum led_brightness max_brightness;
  33. int flags;
  34. /* Lower 16 bits reflect status */
  35. #define LED_SUSPENDED (1 << 0)
  36. /* Upper 16 bits reflect control information */
  37. #define LED_CORE_SUSPENDRESUME (1 << 16)
  38. #define LED_BLINK_ONESHOT (1 << 17)
  39. #define LED_BLINK_ONESHOT_STOP (1 << 18)
  40. #define LED_BLINK_INVERT (1 << 19)
  41. #define LED_SYSFS_DISABLE (1 << 20)
  42. #define SET_BRIGHTNESS_ASYNC (1 << 21)
  43. #define SET_BRIGHTNESS_SYNC (1 << 22)
  44. #define LED_DEV_CAP_FLASH (1 << 23)
  45. #define LED_DEV_CAP_SYNC_STROBE (1 << 24)
  46. /* Set LED brightness level */
  47. /* Must not sleep, use a workqueue if needed */
  48. void (*brightness_set)(struct led_classdev *led_cdev,
  49. enum led_brightness brightness);
  50. /*
  51. * Set LED brightness level immediately - it can block the caller for
  52. * the time required for accessing a LED device register.
  53. */
  54. int (*brightness_set_sync)(struct led_classdev *led_cdev,
  55. enum led_brightness brightness);
  56. /* Get LED brightness level */
  57. enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
  58. /*
  59. * Activate hardware accelerated blink, delays are in milliseconds
  60. * and if both are zero then a sensible default should be chosen.
  61. * The call should adjust the timings in that case and if it can't
  62. * match the values specified exactly.
  63. * Deactivate blinking again when the brightness is set to a fixed
  64. * value via the brightness_set() callback.
  65. */
  66. int (*blink_set)(struct led_classdev *led_cdev,
  67. unsigned long *delay_on,
  68. unsigned long *delay_off);
  69. struct device *dev;
  70. const struct attribute_group **groups;
  71. struct list_head node; /* LED Device list */
  72. const char *default_trigger; /* Trigger to use */
  73. unsigned long blink_delay_on, blink_delay_off;
  74. struct timer_list blink_timer;
  75. int blink_brightness;
  76. void (*flash_resume)(struct led_classdev *led_cdev);
  77. struct work_struct set_brightness_work;
  78. int delayed_set_value;
  79. #ifdef CONFIG_LEDS_TRIGGERS
  80. /* Protects the trigger data below */
  81. struct rw_semaphore trigger_lock;
  82. struct led_trigger *trigger;
  83. struct list_head trig_list;
  84. void *trigger_data;
  85. /* true if activated - deactivate routine uses it to do cleanup */
  86. bool activated;
  87. #endif
  88. /* Ensures consistent access to the LED Flash Class device */
  89. struct mutex led_access;
  90. };
  91. extern int led_classdev_register(struct device *parent,
  92. struct led_classdev *led_cdev);
  93. extern int devm_led_classdev_register(struct device *parent,
  94. struct led_classdev *led_cdev);
  95. extern void led_classdev_unregister(struct led_classdev *led_cdev);
  96. extern void devm_led_classdev_unregister(struct device *parent,
  97. struct led_classdev *led_cdev);
  98. extern void led_classdev_suspend(struct led_classdev *led_cdev);
  99. extern void led_classdev_resume(struct led_classdev *led_cdev);
  100. /**
  101. * led_blink_set - set blinking with software fallback
  102. * @led_cdev: the LED to start blinking
  103. * @delay_on: the time it should be on (in ms)
  104. * @delay_off: the time it should ble off (in ms)
  105. *
  106. * This function makes the LED blink, attempting to use the
  107. * hardware acceleration if possible, but falling back to
  108. * software blinking if there is no hardware blinking or if
  109. * the LED refuses the passed values.
  110. *
  111. * Note that if software blinking is active, simply calling
  112. * led_cdev->brightness_set() will not stop the blinking,
  113. * use led_classdev_brightness_set() instead.
  114. */
  115. extern void led_blink_set(struct led_classdev *led_cdev,
  116. unsigned long *delay_on,
  117. unsigned long *delay_off);
  118. /**
  119. * led_blink_set_oneshot - do a oneshot software blink
  120. * @led_cdev: the LED to start blinking
  121. * @delay_on: the time it should be on (in ms)
  122. * @delay_off: the time it should ble off (in ms)
  123. * @invert: blink off, then on, leaving the led on
  124. *
  125. * This function makes the LED blink one time for delay_on +
  126. * delay_off time, ignoring the request if another one-shot
  127. * blink is already in progress.
  128. *
  129. * If invert is set, led blinks for delay_off first, then for
  130. * delay_on and leave the led on after the on-off cycle.
  131. */
  132. extern void led_blink_set_oneshot(struct led_classdev *led_cdev,
  133. unsigned long *delay_on,
  134. unsigned long *delay_off,
  135. int invert);
  136. /**
  137. * led_set_brightness - set LED brightness
  138. * @led_cdev: the LED to set
  139. * @brightness: the brightness to set it to
  140. *
  141. * Set an LED's brightness, and, if necessary, cancel the
  142. * software blink timer that implements blinking when the
  143. * hardware doesn't.
  144. */
  145. extern void led_set_brightness(struct led_classdev *led_cdev,
  146. enum led_brightness brightness);
  147. /**
  148. * led_update_brightness - update LED brightness
  149. * @led_cdev: the LED to query
  150. *
  151. * Get an LED's current brightness and update led_cdev->brightness
  152. * member with the obtained value.
  153. *
  154. * Returns: 0 on success or negative error value on failure
  155. */
  156. extern int led_update_brightness(struct led_classdev *led_cdev);
  157. /**
  158. * led_sysfs_disable - disable LED sysfs interface
  159. * @led_cdev: the LED to set
  160. *
  161. * Disable the led_cdev's sysfs interface.
  162. */
  163. extern void led_sysfs_disable(struct led_classdev *led_cdev);
  164. /**
  165. * led_sysfs_enable - enable LED sysfs interface
  166. * @led_cdev: the LED to set
  167. *
  168. * Enable the led_cdev's sysfs interface.
  169. */
  170. extern void led_sysfs_enable(struct led_classdev *led_cdev);
  171. /**
  172. * led_sysfs_is_disabled - check if LED sysfs interface is disabled
  173. * @led_cdev: the LED to query
  174. *
  175. * Returns: true if the led_cdev's sysfs interface is disabled.
  176. */
  177. static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
  178. {
  179. return led_cdev->flags & LED_SYSFS_DISABLE;
  180. }
  181. /*
  182. * LED Triggers
  183. */
  184. /* Registration functions for simple triggers */
  185. #define DEFINE_LED_TRIGGER(x) static struct led_trigger *x;
  186. #define DEFINE_LED_TRIGGER_GLOBAL(x) struct led_trigger *x;
  187. #ifdef CONFIG_LEDS_TRIGGERS
  188. #define TRIG_NAME_MAX 50
  189. struct led_trigger {
  190. /* Trigger Properties */
  191. const char *name;
  192. void (*activate)(struct led_classdev *led_cdev);
  193. void (*deactivate)(struct led_classdev *led_cdev);
  194. /* LEDs under control by this trigger (for simple triggers) */
  195. rwlock_t leddev_list_lock;
  196. struct list_head led_cdevs;
  197. /* Link to next registered trigger */
  198. struct list_head next_trig;
  199. };
  200. /* Registration functions for complex triggers */
  201. extern int led_trigger_register(struct led_trigger *trigger);
  202. extern void led_trigger_unregister(struct led_trigger *trigger);
  203. extern void led_trigger_register_simple(const char *name,
  204. struct led_trigger **trigger);
  205. extern void led_trigger_unregister_simple(struct led_trigger *trigger);
  206. extern void led_trigger_event(struct led_trigger *trigger,
  207. enum led_brightness event);
  208. extern void led_trigger_blink(struct led_trigger *trigger,
  209. unsigned long *delay_on,
  210. unsigned long *delay_off);
  211. extern void led_trigger_blink_oneshot(struct led_trigger *trigger,
  212. unsigned long *delay_on,
  213. unsigned long *delay_off,
  214. int invert);
  215. /**
  216. * led_trigger_rename_static - rename a trigger
  217. * @name: the new trigger name
  218. * @trig: the LED trigger to rename
  219. *
  220. * Change a LED trigger name by copying the string passed in
  221. * name into current trigger name, which MUST be large
  222. * enough for the new string.
  223. *
  224. * Note that name must NOT point to the same string used
  225. * during LED registration, as that could lead to races.
  226. *
  227. * This is meant to be used on triggers with statically
  228. * allocated name.
  229. */
  230. extern void led_trigger_rename_static(const char *name,
  231. struct led_trigger *trig);
  232. #else
  233. /* Trigger has no members */
  234. struct led_trigger {};
  235. /* Trigger inline empty functions */
  236. static inline void led_trigger_register_simple(const char *name,
  237. struct led_trigger **trigger) {}
  238. static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
  239. static inline void led_trigger_event(struct led_trigger *trigger,
  240. enum led_brightness event) {}
  241. #endif /* CONFIG_LEDS_TRIGGERS */
  242. /* Trigger specific functions */
  243. #ifdef CONFIG_LEDS_TRIGGER_IDE_DISK
  244. extern void ledtrig_ide_activity(void);
  245. #else
  246. static inline void ledtrig_ide_activity(void) {}
  247. #endif
  248. #if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
  249. extern void ledtrig_flash_ctrl(bool on);
  250. extern void ledtrig_torch_ctrl(bool on);
  251. #else
  252. static inline void ledtrig_flash_ctrl(bool on) {}
  253. static inline void ledtrig_torch_ctrl(bool on) {}
  254. #endif
  255. /*
  256. * Generic LED platform data for describing LED names and default triggers.
  257. */
  258. struct led_info {
  259. const char *name;
  260. const char *default_trigger;
  261. int flags;
  262. };
  263. struct led_platform_data {
  264. int num_leds;
  265. struct led_info *leds;
  266. };
  267. /* For the leds-gpio driver */
  268. struct gpio_led {
  269. const char *name;
  270. const char *default_trigger;
  271. unsigned gpio;
  272. unsigned active_low : 1;
  273. unsigned retain_state_suspended : 1;
  274. unsigned default_state : 2;
  275. /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
  276. struct gpio_desc *gpiod;
  277. };
  278. #define LEDS_GPIO_DEFSTATE_OFF 0
  279. #define LEDS_GPIO_DEFSTATE_ON 1
  280. #define LEDS_GPIO_DEFSTATE_KEEP 2
  281. struct gpio_led_platform_data {
  282. int num_leds;
  283. const struct gpio_led *leds;
  284. #define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
  285. #define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
  286. #define GPIO_LED_BLINK 2 /* Please, blink */
  287. int (*gpio_blink_set)(struct gpio_desc *desc, int state,
  288. unsigned long *delay_on,
  289. unsigned long *delay_off);
  290. };
  291. struct platform_device *gpio_led_register_device(
  292. int id, const struct gpio_led_platform_data *pdata);
  293. enum cpu_led_event {
  294. CPU_LED_IDLE_START, /* CPU enters idle */
  295. CPU_LED_IDLE_END, /* CPU idle ends */
  296. CPU_LED_START, /* Machine starts, especially resume */
  297. CPU_LED_STOP, /* Machine stops, especially suspend */
  298. CPU_LED_HALTED, /* Machine shutdown */
  299. };
  300. #ifdef CONFIG_LEDS_TRIGGER_CPU
  301. extern void ledtrig_cpu(enum cpu_led_event evt);
  302. #else
  303. static inline void ledtrig_cpu(enum cpu_led_event evt)
  304. {
  305. return;
  306. }
  307. #endif
  308. #endif /* __LINUX_LEDS_H_INCLUDED */