Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
24struct device;
25struct led_pattern;
26/*
27 * LED Core
28 */
29
30enum led_brightness {
31 LED_OFF = 0,
32 LED_ON = 1,
33 LED_HALF = 127,
34 LED_FULL = 255,
35};
36
37struct 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
132extern 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)
137extern 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)
142extern void led_classdev_unregister(struct led_classdev *led_cdev);
143extern void devm_led_classdev_unregister(struct device *parent,
144 struct led_classdev *led_cdev);
145extern void led_classdev_suspend(struct led_classdev *led_cdev);
146extern 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 */
163extern 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 */
180extern 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 */
193extern 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 */
207extern 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 */
219extern int led_update_brightness(struct led_classdev *led_cdev);
220
221/**
222 * led_sysfs_disable - disable LED sysfs interface
223 * @led_cdev: the LED to set
224 *
225 * Disable the led_cdev's sysfs interface.
226 */
227extern void led_sysfs_disable(struct led_classdev *led_cdev);
228
229/**
230 * led_sysfs_enable - enable LED sysfs interface
231 * @led_cdev: the LED to set
232 *
233 * Enable the led_cdev's sysfs interface.
234 */
235extern void led_sysfs_enable(struct led_classdev *led_cdev);
236
237/**
238 * led_sysfs_is_disabled - check if LED sysfs interface is disabled
239 * @led_cdev: the LED to query
240 *
241 * Returns: true if the led_cdev's sysfs interface is disabled.
242 */
243static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
244{
245 return led_cdev->flags & LED_SYSFS_DISABLE;
246}
247
248/*
249 * LED Triggers
250 */
251/* Registration functions for simple triggers */
252#define DEFINE_LED_TRIGGER(x) static struct led_trigger *x;
253#define DEFINE_LED_TRIGGER_GLOBAL(x) struct led_trigger *x;
254
255#ifdef CONFIG_LEDS_TRIGGERS
256
257#define TRIG_NAME_MAX 50
258
259struct led_trigger {
260 /* Trigger Properties */
261 const char *name;
262 int (*activate)(struct led_classdev *led_cdev);
263 void (*deactivate)(struct led_classdev *led_cdev);
264
265 /* LEDs under control by this trigger (for simple triggers) */
266 rwlock_t leddev_list_lock;
267 struct list_head led_cdevs;
268
269 /* Link to next registered trigger */
270 struct list_head next_trig;
271
272 const struct attribute_group **groups;
273};
274
275/*
276 * Currently the attributes in struct led_trigger::groups are added directly to
277 * the LED device. As this might change in the future, the following
278 * macros abstract getting the LED device and its trigger_data from the dev
279 * parameter passed to the attribute accessor functions.
280 */
281#define led_trigger_get_led(dev) ((struct led_classdev *)dev_get_drvdata((dev)))
282#define led_trigger_get_drvdata(dev) (led_get_trigger_data(led_trigger_get_led(dev)))
283
284ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t count);
286ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
287 char *buf);
288
289/* Registration functions for complex triggers */
290extern int led_trigger_register(struct led_trigger *trigger);
291extern void led_trigger_unregister(struct led_trigger *trigger);
292extern int devm_led_trigger_register(struct device *dev,
293 struct led_trigger *trigger);
294
295extern void led_trigger_register_simple(const char *name,
296 struct led_trigger **trigger);
297extern void led_trigger_unregister_simple(struct led_trigger *trigger);
298extern void led_trigger_event(struct led_trigger *trigger,
299 enum led_brightness event);
300extern void led_trigger_blink(struct led_trigger *trigger,
301 unsigned long *delay_on,
302 unsigned long *delay_off);
303extern void led_trigger_blink_oneshot(struct led_trigger *trigger,
304 unsigned long *delay_on,
305 unsigned long *delay_off,
306 int invert);
307extern void led_trigger_set_default(struct led_classdev *led_cdev);
308extern int led_trigger_set(struct led_classdev *led_cdev,
309 struct led_trigger *trigger);
310extern void led_trigger_remove(struct led_classdev *led_cdev);
311
312static inline void led_set_trigger_data(struct led_classdev *led_cdev,
313 void *trigger_data)
314{
315 led_cdev->trigger_data = trigger_data;
316}
317
318static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
319{
320 return led_cdev->trigger_data;
321}
322
323/**
324 * led_trigger_rename_static - rename a trigger
325 * @name: the new trigger name
326 * @trig: the LED trigger to rename
327 *
328 * Change a LED trigger name by copying the string passed in
329 * name into current trigger name, which MUST be large
330 * enough for the new string.
331 *
332 * Note that name must NOT point to the same string used
333 * during LED registration, as that could lead to races.
334 *
335 * This is meant to be used on triggers with statically
336 * allocated name.
337 */
338extern void led_trigger_rename_static(const char *name,
339 struct led_trigger *trig);
340
341#define module_led_trigger(__led_trigger) \
342 module_driver(__led_trigger, led_trigger_register, \
343 led_trigger_unregister)
344
345#else
346
347/* Trigger has no members */
348struct led_trigger {};
349
350/* Trigger inline empty functions */
351static inline void led_trigger_register_simple(const char *name,
352 struct led_trigger **trigger) {}
353static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
354static inline void led_trigger_event(struct led_trigger *trigger,
355 enum led_brightness event) {}
356static inline void led_trigger_blink(struct led_trigger *trigger,
357 unsigned long *delay_on,
358 unsigned long *delay_off) {}
359static inline void led_trigger_blink_oneshot(struct led_trigger *trigger,
360 unsigned long *delay_on,
361 unsigned long *delay_off,
362 int invert) {}
363static inline void led_trigger_set_default(struct led_classdev *led_cdev) {}
364static inline int led_trigger_set(struct led_classdev *led_cdev,
365 struct led_trigger *trigger)
366{
367 return 0;
368}
369
370static inline void led_trigger_remove(struct led_classdev *led_cdev) {}
371static inline void led_set_trigger_data(struct led_classdev *led_cdev) {}
372static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
373{
374 return NULL;
375}
376
377#endif /* CONFIG_LEDS_TRIGGERS */
378
379/* Trigger specific functions */
380#ifdef CONFIG_LEDS_TRIGGER_DISK
381extern void ledtrig_disk_activity(bool write);
382#else
383static inline void ledtrig_disk_activity(bool write) {}
384#endif
385
386#ifdef CONFIG_LEDS_TRIGGER_MTD
387extern void ledtrig_mtd_activity(void);
388#else
389static inline void ledtrig_mtd_activity(void) {}
390#endif
391
392#if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
393extern void ledtrig_flash_ctrl(bool on);
394extern void ledtrig_torch_ctrl(bool on);
395#else
396static inline void ledtrig_flash_ctrl(bool on) {}
397static inline void ledtrig_torch_ctrl(bool on) {}
398#endif
399
400/*
401 * Generic LED platform data for describing LED names and default triggers.
402 */
403struct led_info {
404 const char *name;
405 const char *default_trigger;
406 int flags;
407};
408
409struct led_platform_data {
410 int num_leds;
411 struct led_info *leds;
412};
413
414struct gpio_desc;
415typedef int (*gpio_blink_set_t)(struct gpio_desc *desc, int state,
416 unsigned long *delay_on,
417 unsigned long *delay_off);
418
419/* For the leds-gpio driver */
420struct gpio_led {
421 const char *name;
422 const char *default_trigger;
423 unsigned gpio;
424 unsigned active_low : 1;
425 unsigned retain_state_suspended : 1;
426 unsigned panic_indicator : 1;
427 unsigned default_state : 2;
428 unsigned retain_state_shutdown : 1;
429 /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
430 struct gpio_desc *gpiod;
431};
432#define LEDS_GPIO_DEFSTATE_OFF 0
433#define LEDS_GPIO_DEFSTATE_ON 1
434#define LEDS_GPIO_DEFSTATE_KEEP 2
435
436struct gpio_led_platform_data {
437 int num_leds;
438 const struct gpio_led *leds;
439
440#define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
441#define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
442#define GPIO_LED_BLINK 2 /* Please, blink */
443 gpio_blink_set_t gpio_blink_set;
444};
445
446#ifdef CONFIG_NEW_LEDS
447struct platform_device *gpio_led_register_device(
448 int id, const struct gpio_led_platform_data *pdata);
449#else
450static inline struct platform_device *gpio_led_register_device(
451 int id, const struct gpio_led_platform_data *pdata)
452{
453 return 0;
454}
455#endif
456
457enum cpu_led_event {
458 CPU_LED_IDLE_START, /* CPU enters idle */
459 CPU_LED_IDLE_END, /* CPU idle ends */
460 CPU_LED_START, /* Machine starts, especially resume */
461 CPU_LED_STOP, /* Machine stops, especially suspend */
462 CPU_LED_HALTED, /* Machine shutdown */
463};
464#ifdef CONFIG_LEDS_TRIGGER_CPU
465extern void ledtrig_cpu(enum cpu_led_event evt);
466#else
467static inline void ledtrig_cpu(enum cpu_led_event evt)
468{
469 return;
470}
471#endif
472
473#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
474extern void led_classdev_notify_brightness_hw_changed(
475 struct led_classdev *led_cdev, enum led_brightness brightness);
476#else
477static inline void led_classdev_notify_brightness_hw_changed(
478 struct led_classdev *led_cdev, enum led_brightness brightness) { }
479#endif
480
481/**
482 * struct led_pattern - pattern interval settings
483 * @delta_t: pattern interval delay, in milliseconds
484 * @brightness: pattern interval brightness
485 */
486struct led_pattern {
487 u32 delta_t;
488 int brightness;
489};
490
491enum led_audio {
492 LED_AUDIO_MUTE, /* master mute LED */
493 LED_AUDIO_MICMUTE, /* mic mute LED */
494 NUM_AUDIO_LEDS
495};
496
497#if IS_ENABLED(CONFIG_LEDS_TRIGGER_AUDIO)
498enum led_brightness ledtrig_audio_get(enum led_audio type);
499void ledtrig_audio_set(enum led_audio type, enum led_brightness state);
500#else
501static inline enum led_brightness ledtrig_audio_get(enum led_audio type)
502{
503 return LED_OFF;
504}
505static inline void ledtrig_audio_set(enum led_audio type,
506 enum led_brightness state)
507{
508}
509#endif
510
511#endif /* __LINUX_LEDS_H_INCLUDED */