leds.h 12 KB

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