leds.h 15 KB

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