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