leds.h 18 KB

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