leds.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Driver model for leds and led triggers
  4. *
  5. * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
  6. * Copyright (C) 2005 Richard Purdie <rpurdie@openedhand.com>
  7. */
  8. #ifndef __LINUX_LEDS_H_INCLUDED
  9. #define __LINUX_LEDS_H_INCLUDED
  10. #include <dt-bindings/leds/common.h>
  11. #include <linux/device.h>
  12. #include <linux/kernfs.h>
  13. #include <linux/list.h>
  14. #include <linux/mutex.h>
  15. #include <linux/rwsem.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/timer.h>
  18. #include <linux/workqueue.h>
  19. struct device;
  20. struct led_pattern;
  21. struct device_node;
  22. /*
  23. * LED Core
  24. */
  25. /* This is obsolete/useless. We now support variable maximum brightness. */
  26. enum led_brightness {
  27. LED_OFF = 0,
  28. LED_ON = 1,
  29. LED_HALF = 127,
  30. LED_FULL = 255,
  31. };
  32. struct led_init_data {
  33. /* device fwnode handle */
  34. struct fwnode_handle *fwnode;
  35. /*
  36. * default <color:function> tuple, for backward compatibility
  37. * with in-driver hard-coded LED names used as a fallback when
  38. * DT "label" property is absent; it should be set to NULL
  39. * in new LED class drivers.
  40. */
  41. const char *default_label;
  42. /*
  43. * string to be used for devicename section of LED class device
  44. * either for label based LED name composition path or for fwnode
  45. * based when devname_mandatory is true
  46. */
  47. const char *devicename;
  48. /*
  49. * indicates if LED name should always comprise devicename section;
  50. * only LEDs exposed by drivers of hot-pluggable devices should
  51. * set it to true
  52. */
  53. bool devname_mandatory;
  54. };
  55. struct led_hw_trigger_type {
  56. int dummy;
  57. };
  58. struct led_classdev {
  59. const char *name;
  60. enum led_brightness brightness;
  61. enum led_brightness max_brightness;
  62. int flags;
  63. /* Lower 16 bits reflect status */
  64. #define LED_SUSPENDED BIT(0)
  65. #define LED_UNREGISTERING BIT(1)
  66. /* Upper 16 bits reflect control information */
  67. #define LED_CORE_SUSPENDRESUME BIT(16)
  68. #define LED_SYSFS_DISABLE BIT(17)
  69. #define LED_DEV_CAP_FLASH BIT(18)
  70. #define LED_HW_PLUGGABLE BIT(19)
  71. #define LED_PANIC_INDICATOR BIT(20)
  72. #define LED_BRIGHT_HW_CHANGED BIT(21)
  73. #define LED_RETAIN_AT_SHUTDOWN BIT(22)
  74. #define LED_INIT_DEFAULT_TRIGGER BIT(23)
  75. /* set_brightness_work / blink_timer flags, atomic, private. */
  76. unsigned long work_flags;
  77. #define LED_BLINK_SW 0
  78. #define LED_BLINK_ONESHOT 1
  79. #define LED_BLINK_ONESHOT_STOP 2
  80. #define LED_BLINK_INVERT 3
  81. #define LED_BLINK_BRIGHTNESS_CHANGE 4
  82. #define LED_BLINK_DISABLE 5
  83. /* Set LED brightness level
  84. * Must not sleep. Use brightness_set_blocking for drivers
  85. * that can sleep while setting brightness.
  86. */
  87. void (*brightness_set)(struct led_classdev *led_cdev,
  88. enum led_brightness brightness);
  89. /*
  90. * Set LED brightness level immediately - it can block the caller for
  91. * the time required for accessing a LED device register.
  92. */
  93. int (*brightness_set_blocking)(struct led_classdev *led_cdev,
  94. enum led_brightness brightness);
  95. /* Get LED brightness level */
  96. enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
  97. /*
  98. * Activate hardware accelerated blink, delays are in milliseconds
  99. * and if both are zero then a sensible default should be chosen.
  100. * The call should adjust the timings in that case and if it can't
  101. * match the values specified exactly.
  102. * Deactivate blinking again when the brightness is set to LED_OFF
  103. * via the brightness_set() callback.
  104. */
  105. int (*blink_set)(struct led_classdev *led_cdev,
  106. unsigned long *delay_on,
  107. unsigned long *delay_off);
  108. int (*pattern_set)(struct led_classdev *led_cdev,
  109. struct led_pattern *pattern, u32 len, int repeat);
  110. int (*pattern_clear)(struct led_classdev *led_cdev);
  111. struct device *dev;
  112. const struct attribute_group **groups;
  113. struct list_head node; /* LED Device list */
  114. const char *default_trigger; /* Trigger to use */
  115. unsigned long blink_delay_on, blink_delay_off;
  116. struct timer_list blink_timer;
  117. int blink_brightness;
  118. int new_blink_brightness;
  119. void (*flash_resume)(struct led_classdev *led_cdev);
  120. struct work_struct set_brightness_work;
  121. int delayed_set_value;
  122. #ifdef CONFIG_LEDS_TRIGGERS
  123. /* Protects the trigger data below */
  124. struct rw_semaphore trigger_lock;
  125. struct led_trigger *trigger;
  126. struct list_head trig_list;
  127. void *trigger_data;
  128. /* true if activated - deactivate routine uses it to do cleanup */
  129. bool activated;
  130. /* LEDs that have private triggers have this set */
  131. struct led_hw_trigger_type *trigger_type;
  132. #endif
  133. #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
  134. int brightness_hw_changed;
  135. struct kernfs_node *brightness_hw_changed_kn;
  136. #endif
  137. /* Ensures consistent access to the LED Flash Class device */
  138. struct mutex led_access;
  139. };
  140. /**
  141. * led_classdev_register_ext - register a new object of LED class with
  142. * init data
  143. * @parent: LED controller device this LED is driven by
  144. * @led_cdev: the led_classdev structure for this device
  145. * @init_data: the LED class device initialization data
  146. *
  147. * Register a new object of LED class, with name derived from init_data.
  148. *
  149. * Returns: 0 on success or negative error value on failure
  150. */
  151. int led_classdev_register_ext(struct device *parent,
  152. struct led_classdev *led_cdev,
  153. struct led_init_data *init_data);
  154. /**
  155. * led_classdev_register - register a new object of LED class
  156. * @parent: LED controller device this LED is driven by
  157. * @led_cdev: the led_classdev structure for this device
  158. *
  159. * Register a new object of LED class, with name derived from the name property
  160. * of passed led_cdev argument.
  161. *
  162. * Returns: 0 on success or negative error value on failure
  163. */
  164. static inline int led_classdev_register(struct device *parent,
  165. struct led_classdev *led_cdev)
  166. {
  167. return led_classdev_register_ext(parent, led_cdev, NULL);
  168. }
  169. int devm_led_classdev_register_ext(struct device *parent,
  170. struct led_classdev *led_cdev,
  171. struct led_init_data *init_data);
  172. static inline int devm_led_classdev_register(struct device *parent,
  173. struct led_classdev *led_cdev)
  174. {
  175. return devm_led_classdev_register_ext(parent, led_cdev, NULL);
  176. }
  177. void led_classdev_unregister(struct led_classdev *led_cdev);
  178. void devm_led_classdev_unregister(struct device *parent,
  179. struct led_classdev *led_cdev);
  180. void led_classdev_suspend(struct led_classdev *led_cdev);
  181. void led_classdev_resume(struct led_classdev *led_cdev);
  182. extern struct led_classdev *of_led_get(struct device_node *np, int index);
  183. extern void led_put(struct led_classdev *led_cdev);
  184. struct led_classdev *__must_check devm_of_led_get(struct device *dev,
  185. int index);
  186. /**
  187. * led_blink_set - set blinking with software fallback
  188. * @led_cdev: the LED to start blinking
  189. * @delay_on: the time it should be on (in ms)
  190. * @delay_off: the time it should ble off (in ms)
  191. *
  192. * This function makes the LED blink, attempting to use the
  193. * hardware acceleration if possible, but falling back to
  194. * software blinking if there is no hardware blinking or if
  195. * the LED refuses the passed values.
  196. *
  197. * Note that if software blinking is active, simply calling
  198. * led_cdev->brightness_set() will not stop the blinking,
  199. * use led_classdev_brightness_set() instead.
  200. */
  201. void led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on,
  202. unsigned long *delay_off);
  203. /**
  204. * led_blink_set_oneshot - do a oneshot software blink
  205. * @led_cdev: the LED to start blinking
  206. * @delay_on: the time it should be on (in ms)
  207. * @delay_off: the time it should ble off (in ms)
  208. * @invert: blink off, then on, leaving the led on
  209. *
  210. * This function makes the LED blink one time for delay_on +
  211. * delay_off time, ignoring the request if another one-shot
  212. * blink is already in progress.
  213. *
  214. * If invert is set, led blinks for delay_off first, then for
  215. * delay_on and leave the led on after the on-off cycle.
  216. */
  217. void led_blink_set_oneshot(struct led_classdev *led_cdev,
  218. unsigned long *delay_on, unsigned long *delay_off,
  219. int invert);
  220. /**
  221. * led_set_brightness - set LED brightness
  222. * @led_cdev: the LED to set
  223. * @brightness: the brightness to set it to
  224. *
  225. * Set an LED's brightness, and, if necessary, cancel the
  226. * software blink timer that implements blinking when the
  227. * hardware doesn't. This function is guaranteed not to sleep.
  228. */
  229. void led_set_brightness(struct led_classdev *led_cdev,
  230. enum led_brightness brightness);
  231. /**
  232. * led_set_brightness_sync - set LED brightness synchronously
  233. * @led_cdev: the LED to set
  234. * @value: the brightness to set it to
  235. *
  236. * Set an LED's brightness immediately. This function will block
  237. * the caller for the time required for accessing device registers,
  238. * and it can sleep.
  239. *
  240. * Returns: 0 on success or negative error value on failure
  241. */
  242. int led_set_brightness_sync(struct led_classdev *led_cdev,
  243. enum led_brightness value);
  244. /**
  245. * led_update_brightness - update LED brightness
  246. * @led_cdev: the LED to query
  247. *
  248. * Get an LED's current brightness and update led_cdev->brightness
  249. * member with the obtained value.
  250. *
  251. * Returns: 0 on success or negative error value on failure
  252. */
  253. int led_update_brightness(struct led_classdev *led_cdev);
  254. /**
  255. * led_get_default_pattern - return default pattern
  256. *
  257. * @led_cdev: the LED to get default pattern for
  258. * @size: pointer for storing the number of elements in returned array,
  259. * modified only if return != NULL
  260. *
  261. * Return: Allocated array of integers with default pattern from device tree
  262. * or NULL. Caller is responsible for kfree().
  263. */
  264. u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size);
  265. /**
  266. * led_sysfs_disable - disable LED sysfs interface
  267. * @led_cdev: the LED to set
  268. *
  269. * Disable the led_cdev's sysfs interface.
  270. */
  271. void led_sysfs_disable(struct led_classdev *led_cdev);
  272. /**
  273. * led_sysfs_enable - enable LED sysfs interface
  274. * @led_cdev: the LED to set
  275. *
  276. * Enable the led_cdev's sysfs interface.
  277. */
  278. void led_sysfs_enable(struct led_classdev *led_cdev);
  279. /**
  280. * led_compose_name - compose LED class device name
  281. * @dev: LED controller device object
  282. * @init_data: the LED class device initialization data
  283. * @led_classdev_name: composed LED class device name
  284. *
  285. * Create LED class device name basing on the provided init_data argument.
  286. * The name can have <devicename:color:function> or <color:function>.
  287. * form, depending on the init_data configuration.
  288. *
  289. * Returns: 0 on success or negative error value on failure
  290. */
  291. int led_compose_name(struct device *dev, struct led_init_data *init_data,
  292. char *led_classdev_name);
  293. /**
  294. * led_sysfs_is_disabled - check if LED sysfs interface is disabled
  295. * @led_cdev: the LED to query
  296. *
  297. * Returns: true if the led_cdev's sysfs interface is disabled.
  298. */
  299. static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
  300. {
  301. return led_cdev->flags & LED_SYSFS_DISABLE;
  302. }
  303. /*
  304. * LED Triggers
  305. */
  306. /* Registration functions for simple triggers */
  307. #define DEFINE_LED_TRIGGER(x) static struct led_trigger *x;
  308. #define DEFINE_LED_TRIGGER_GLOBAL(x) struct led_trigger *x;
  309. #ifdef CONFIG_LEDS_TRIGGERS
  310. #define TRIG_NAME_MAX 50
  311. struct led_trigger {
  312. /* Trigger Properties */
  313. const char *name;
  314. int (*activate)(struct led_classdev *led_cdev);
  315. void (*deactivate)(struct led_classdev *led_cdev);
  316. /* LED-private triggers have this set */
  317. struct led_hw_trigger_type *trigger_type;
  318. /* LEDs under control by this trigger (for simple triggers) */
  319. rwlock_t leddev_list_lock;
  320. struct list_head led_cdevs;
  321. /* Link to next registered trigger */
  322. struct list_head next_trig;
  323. const struct attribute_group **groups;
  324. };
  325. /*
  326. * Currently the attributes in struct led_trigger::groups are added directly to
  327. * the LED device. As this might change in the future, the following
  328. * macros abstract getting the LED device and its trigger_data from the dev
  329. * parameter passed to the attribute accessor functions.
  330. */
  331. #define led_trigger_get_led(dev) ((struct led_classdev *)dev_get_drvdata((dev)))
  332. #define led_trigger_get_drvdata(dev) (led_get_trigger_data(led_trigger_get_led(dev)))
  333. /* Registration functions for complex triggers */
  334. int led_trigger_register(struct led_trigger *trigger);
  335. void led_trigger_unregister(struct led_trigger *trigger);
  336. int devm_led_trigger_register(struct device *dev,
  337. struct led_trigger *trigger);
  338. void led_trigger_register_simple(const char *name,
  339. struct led_trigger **trigger);
  340. void led_trigger_unregister_simple(struct led_trigger *trigger);
  341. void led_trigger_event(struct led_trigger *trigger, enum led_brightness event);
  342. void led_trigger_blink(struct led_trigger *trigger, unsigned long *delay_on,
  343. unsigned long *delay_off);
  344. void led_trigger_blink_oneshot(struct led_trigger *trigger,
  345. unsigned long *delay_on,
  346. unsigned long *delay_off,
  347. int invert);
  348. void led_trigger_set_default(struct led_classdev *led_cdev);
  349. int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger);
  350. void led_trigger_remove(struct led_classdev *led_cdev);
  351. static inline void led_set_trigger_data(struct led_classdev *led_cdev,
  352. void *trigger_data)
  353. {
  354. led_cdev->trigger_data = trigger_data;
  355. }
  356. static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
  357. {
  358. return led_cdev->trigger_data;
  359. }
  360. /**
  361. * led_trigger_rename_static - rename a trigger
  362. * @name: the new trigger name
  363. * @trig: the LED trigger to rename
  364. *
  365. * Change a LED trigger name by copying the string passed in
  366. * name into current trigger name, which MUST be large
  367. * enough for the new string.
  368. *
  369. * Note that name must NOT point to the same string used
  370. * during LED registration, as that could lead to races.
  371. *
  372. * This is meant to be used on triggers with statically
  373. * allocated name.
  374. */
  375. void led_trigger_rename_static(const char *name, struct led_trigger *trig);
  376. #define module_led_trigger(__led_trigger) \
  377. module_driver(__led_trigger, led_trigger_register, \
  378. led_trigger_unregister)
  379. #else
  380. /* Trigger has no members */
  381. struct led_trigger {};
  382. /* Trigger inline empty functions */
  383. static inline void led_trigger_register_simple(const char *name,
  384. struct led_trigger **trigger) {}
  385. static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
  386. static inline void led_trigger_event(struct led_trigger *trigger,
  387. enum led_brightness event) {}
  388. static inline void led_trigger_blink(struct led_trigger *trigger,
  389. unsigned long *delay_on,
  390. unsigned long *delay_off) {}
  391. static inline void led_trigger_blink_oneshot(struct led_trigger *trigger,
  392. unsigned long *delay_on,
  393. unsigned long *delay_off,
  394. int invert) {}
  395. static inline void led_trigger_set_default(struct led_classdev *led_cdev) {}
  396. static inline int led_trigger_set(struct led_classdev *led_cdev,
  397. struct led_trigger *trigger)
  398. {
  399. return 0;
  400. }
  401. static inline void led_trigger_remove(struct led_classdev *led_cdev) {}
  402. static inline void led_set_trigger_data(struct led_classdev *led_cdev) {}
  403. static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
  404. {
  405. return NULL;
  406. }
  407. #endif /* CONFIG_LEDS_TRIGGERS */
  408. /* Trigger specific functions */
  409. #ifdef CONFIG_LEDS_TRIGGER_DISK
  410. void ledtrig_disk_activity(bool write);
  411. #else
  412. static inline void ledtrig_disk_activity(bool write) {}
  413. #endif
  414. #ifdef CONFIG_LEDS_TRIGGER_MTD
  415. void ledtrig_mtd_activity(void);
  416. #else
  417. static inline void ledtrig_mtd_activity(void) {}
  418. #endif
  419. #if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
  420. void ledtrig_flash_ctrl(bool on);
  421. void ledtrig_torch_ctrl(bool on);
  422. #else
  423. static inline void ledtrig_flash_ctrl(bool on) {}
  424. static inline void ledtrig_torch_ctrl(bool on) {}
  425. #endif
  426. /*
  427. * Generic LED platform data for describing LED names and default triggers.
  428. */
  429. struct led_info {
  430. const char *name;
  431. const char *default_trigger;
  432. int flags;
  433. };
  434. struct led_platform_data {
  435. int num_leds;
  436. struct led_info *leds;
  437. };
  438. struct led_properties {
  439. u32 color;
  440. bool color_present;
  441. const char *function;
  442. u32 func_enum;
  443. bool func_enum_present;
  444. const char *label;
  445. };
  446. struct gpio_desc;
  447. typedef int (*gpio_blink_set_t)(struct gpio_desc *desc, int state,
  448. unsigned long *delay_on,
  449. unsigned long *delay_off);
  450. /* For the leds-gpio driver */
  451. struct gpio_led {
  452. const char *name;
  453. const char *default_trigger;
  454. unsigned gpio;
  455. unsigned active_low : 1;
  456. unsigned retain_state_suspended : 1;
  457. unsigned panic_indicator : 1;
  458. unsigned default_state : 2;
  459. unsigned retain_state_shutdown : 1;
  460. /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
  461. struct gpio_desc *gpiod;
  462. };
  463. #define LEDS_GPIO_DEFSTATE_OFF 0
  464. #define LEDS_GPIO_DEFSTATE_ON 1
  465. #define LEDS_GPIO_DEFSTATE_KEEP 2
  466. struct gpio_led_platform_data {
  467. int num_leds;
  468. const struct gpio_led *leds;
  469. #define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
  470. #define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
  471. #define GPIO_LED_BLINK 2 /* Please, blink */
  472. gpio_blink_set_t gpio_blink_set;
  473. };
  474. #ifdef CONFIG_NEW_LEDS
  475. struct platform_device *gpio_led_register_device(
  476. int id, const struct gpio_led_platform_data *pdata);
  477. #else
  478. static inline struct platform_device *gpio_led_register_device(
  479. int id, const struct gpio_led_platform_data *pdata)
  480. {
  481. return 0;
  482. }
  483. #endif
  484. enum cpu_led_event {
  485. CPU_LED_IDLE_START, /* CPU enters idle */
  486. CPU_LED_IDLE_END, /* CPU idle ends */
  487. CPU_LED_START, /* Machine starts, especially resume */
  488. CPU_LED_STOP, /* Machine stops, especially suspend */
  489. CPU_LED_HALTED, /* Machine shutdown */
  490. };
  491. #ifdef CONFIG_LEDS_TRIGGER_CPU
  492. void ledtrig_cpu(enum cpu_led_event evt);
  493. #else
  494. static inline void ledtrig_cpu(enum cpu_led_event evt)
  495. {
  496. return;
  497. }
  498. #endif
  499. #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
  500. void led_classdev_notify_brightness_hw_changed(
  501. struct led_classdev *led_cdev, enum led_brightness brightness);
  502. #else
  503. static inline void led_classdev_notify_brightness_hw_changed(
  504. struct led_classdev *led_cdev, enum led_brightness brightness) { }
  505. #endif
  506. /**
  507. * struct led_pattern - pattern interval settings
  508. * @delta_t: pattern interval delay, in milliseconds
  509. * @brightness: pattern interval brightness
  510. */
  511. struct led_pattern {
  512. u32 delta_t;
  513. int brightness;
  514. };
  515. enum led_audio {
  516. LED_AUDIO_MUTE, /* master mute LED */
  517. LED_AUDIO_MICMUTE, /* mic mute LED */
  518. NUM_AUDIO_LEDS
  519. };
  520. #if IS_ENABLED(CONFIG_LEDS_TRIGGER_AUDIO)
  521. enum led_brightness ledtrig_audio_get(enum led_audio type);
  522. void ledtrig_audio_set(enum led_audio type, enum led_brightness state);
  523. #else
  524. static inline enum led_brightness ledtrig_audio_get(enum led_audio type)
  525. {
  526. return LED_OFF;
  527. }
  528. static inline void ledtrig_audio_set(enum led_audio type,
  529. enum led_brightness state)
  530. {
  531. }
  532. #endif
  533. #endif /* __LINUX_LEDS_H_INCLUDED */