Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Driver model for leds and led triggers
0004  *
0005  * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
0006  * Copyright (C) 2005 Richard Purdie <rpurdie@openedhand.com>
0007  */
0008 #ifndef __LINUX_LEDS_H_INCLUDED
0009 #define __LINUX_LEDS_H_INCLUDED
0010 
0011 #include <dt-bindings/leds/common.h>
0012 #include <linux/device.h>
0013 #include <linux/kernfs.h>
0014 #include <linux/list.h>
0015 #include <linux/mutex.h>
0016 #include <linux/rwsem.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/timer.h>
0019 #include <linux/workqueue.h>
0020 
0021 struct device;
0022 struct led_pattern;
0023 struct device_node;
0024 /*
0025  * LED Core
0026  */
0027 
0028 /* This is obsolete/useless. We now support variable maximum brightness. */
0029 enum led_brightness {
0030     LED_OFF     = 0,
0031     LED_ON      = 1,
0032     LED_HALF    = 127,
0033     LED_FULL    = 255,
0034 };
0035 
0036 enum led_default_state {
0037     LEDS_DEFSTATE_OFF   = 0,
0038     LEDS_DEFSTATE_ON    = 1,
0039     LEDS_DEFSTATE_KEEP  = 2,
0040 };
0041 
0042 struct led_init_data {
0043     /* device fwnode handle */
0044     struct fwnode_handle *fwnode;
0045     /*
0046      * default <color:function> tuple, for backward compatibility
0047      * with in-driver hard-coded LED names used as a fallback when
0048      * DT "label" property is absent; it should be set to NULL
0049      * in new LED class drivers.
0050      */
0051     const char *default_label;
0052     /*
0053      * string to be used for devicename section of LED class device
0054      * either for label based LED name composition path or for fwnode
0055      * based when devname_mandatory is true
0056      */
0057     const char *devicename;
0058     /*
0059      * indicates if LED name should always comprise devicename section;
0060      * only LEDs exposed by drivers of hot-pluggable devices should
0061      * set it to true
0062      */
0063     bool devname_mandatory;
0064 };
0065 
0066 struct led_hw_trigger_type {
0067     int dummy;
0068 };
0069 
0070 struct led_classdev {
0071     const char      *name;
0072     unsigned int brightness;
0073     unsigned int max_brightness;
0074     int          flags;
0075 
0076     /* Lower 16 bits reflect status */
0077 #define LED_SUSPENDED       BIT(0)
0078 #define LED_UNREGISTERING   BIT(1)
0079     /* Upper 16 bits reflect control information */
0080 #define LED_CORE_SUSPENDRESUME  BIT(16)
0081 #define LED_SYSFS_DISABLE   BIT(17)
0082 #define LED_DEV_CAP_FLASH   BIT(18)
0083 #define LED_HW_PLUGGABLE    BIT(19)
0084 #define LED_PANIC_INDICATOR BIT(20)
0085 #define LED_BRIGHT_HW_CHANGED   BIT(21)
0086 #define LED_RETAIN_AT_SHUTDOWN  BIT(22)
0087 #define LED_INIT_DEFAULT_TRIGGER BIT(23)
0088 
0089     /* set_brightness_work / blink_timer flags, atomic, private. */
0090     unsigned long       work_flags;
0091 
0092 #define LED_BLINK_SW            0
0093 #define LED_BLINK_ONESHOT       1
0094 #define LED_BLINK_ONESHOT_STOP      2
0095 #define LED_BLINK_INVERT        3
0096 #define LED_BLINK_BRIGHTNESS_CHANGE     4
0097 #define LED_BLINK_DISABLE       5
0098 
0099     /* Set LED brightness level
0100      * Must not sleep. Use brightness_set_blocking for drivers
0101      * that can sleep while setting brightness.
0102      */
0103     void        (*brightness_set)(struct led_classdev *led_cdev,
0104                       enum led_brightness brightness);
0105     /*
0106      * Set LED brightness level immediately - it can block the caller for
0107      * the time required for accessing a LED device register.
0108      */
0109     int (*brightness_set_blocking)(struct led_classdev *led_cdev,
0110                        enum led_brightness brightness);
0111     /* Get LED brightness level */
0112     enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
0113 
0114     /*
0115      * Activate hardware accelerated blink, delays are in milliseconds
0116      * and if both are zero then a sensible default should be chosen.
0117      * The call should adjust the timings in that case and if it can't
0118      * match the values specified exactly.
0119      * Deactivate blinking again when the brightness is set to LED_OFF
0120      * via the brightness_set() callback.
0121      */
0122     int     (*blink_set)(struct led_classdev *led_cdev,
0123                      unsigned long *delay_on,
0124                      unsigned long *delay_off);
0125 
0126     int (*pattern_set)(struct led_classdev *led_cdev,
0127                struct led_pattern *pattern, u32 len, int repeat);
0128     int (*pattern_clear)(struct led_classdev *led_cdev);
0129 
0130     struct device       *dev;
0131     const struct attribute_group    **groups;
0132 
0133     struct list_head     node;          /* LED Device list */
0134     const char      *default_trigger;   /* Trigger to use */
0135 
0136     unsigned long        blink_delay_on, blink_delay_off;
0137     struct timer_list    blink_timer;
0138     int          blink_brightness;
0139     int          new_blink_brightness;
0140     void            (*flash_resume)(struct led_classdev *led_cdev);
0141 
0142     struct work_struct  set_brightness_work;
0143     int         delayed_set_value;
0144 
0145 #ifdef CONFIG_LEDS_TRIGGERS
0146     /* Protects the trigger data below */
0147     struct rw_semaphore  trigger_lock;
0148 
0149     struct led_trigger  *trigger;
0150     struct list_head     trig_list;
0151     void            *trigger_data;
0152     /* true if activated - deactivate routine uses it to do cleanup */
0153     bool            activated;
0154 
0155     /* LEDs that have private triggers have this set */
0156     struct led_hw_trigger_type  *trigger_type;
0157 #endif
0158 
0159 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
0160     int          brightness_hw_changed;
0161     struct kernfs_node  *brightness_hw_changed_kn;
0162 #endif
0163 
0164     /* Ensures consistent access to the LED Flash Class device */
0165     struct mutex        led_access;
0166 };
0167 
0168 /**
0169  * led_classdev_register_ext - register a new object of LED class with
0170  *                 init data
0171  * @parent: LED controller device this LED is driven by
0172  * @led_cdev: the led_classdev structure for this device
0173  * @init_data: the LED class device initialization data
0174  *
0175  * Register a new object of LED class, with name derived from init_data.
0176  *
0177  * Returns: 0 on success or negative error value on failure
0178  */
0179 int led_classdev_register_ext(struct device *parent,
0180                      struct led_classdev *led_cdev,
0181                      struct led_init_data *init_data);
0182 
0183 /**
0184  * led_classdev_register - register a new object of LED class
0185  * @parent: LED controller device this LED is driven by
0186  * @led_cdev: the led_classdev structure for this device
0187  *
0188  * Register a new object of LED class, with name derived from the name property
0189  * of passed led_cdev argument.
0190  *
0191  * Returns: 0 on success or negative error value on failure
0192  */
0193 static inline int led_classdev_register(struct device *parent,
0194                     struct led_classdev *led_cdev)
0195 {
0196     return led_classdev_register_ext(parent, led_cdev, NULL);
0197 }
0198 
0199 int devm_led_classdev_register_ext(struct device *parent,
0200                       struct led_classdev *led_cdev,
0201                       struct led_init_data *init_data);
0202 
0203 static inline int devm_led_classdev_register(struct device *parent,
0204                          struct led_classdev *led_cdev)
0205 {
0206     return devm_led_classdev_register_ext(parent, led_cdev, NULL);
0207 }
0208 void led_classdev_unregister(struct led_classdev *led_cdev);
0209 void devm_led_classdev_unregister(struct device *parent,
0210                   struct led_classdev *led_cdev);
0211 void led_classdev_suspend(struct led_classdev *led_cdev);
0212 void led_classdev_resume(struct led_classdev *led_cdev);
0213 
0214 extern struct led_classdev *of_led_get(struct device_node *np, int index);
0215 extern void led_put(struct led_classdev *led_cdev);
0216 struct led_classdev *__must_check devm_of_led_get(struct device *dev,
0217                           int index);
0218 
0219 /**
0220  * led_blink_set - set blinking with software fallback
0221  * @led_cdev: the LED to start blinking
0222  * @delay_on: the time it should be on (in ms)
0223  * @delay_off: the time it should ble off (in ms)
0224  *
0225  * This function makes the LED blink, attempting to use the
0226  * hardware acceleration if possible, but falling back to
0227  * software blinking if there is no hardware blinking or if
0228  * the LED refuses the passed values.
0229  *
0230  * Note that if software blinking is active, simply calling
0231  * led_cdev->brightness_set() will not stop the blinking,
0232  * use led_classdev_brightness_set() instead.
0233  */
0234 void led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on,
0235            unsigned long *delay_off);
0236 /**
0237  * led_blink_set_oneshot - do a oneshot software blink
0238  * @led_cdev: the LED to start blinking
0239  * @delay_on: the time it should be on (in ms)
0240  * @delay_off: the time it should ble off (in ms)
0241  * @invert: blink off, then on, leaving the led on
0242  *
0243  * This function makes the LED blink one time for delay_on +
0244  * delay_off time, ignoring the request if another one-shot
0245  * blink is already in progress.
0246  *
0247  * If invert is set, led blinks for delay_off first, then for
0248  * delay_on and leave the led on after the on-off cycle.
0249  */
0250 void led_blink_set_oneshot(struct led_classdev *led_cdev,
0251                unsigned long *delay_on, unsigned long *delay_off,
0252                int invert);
0253 /**
0254  * led_set_brightness - set LED brightness
0255  * @led_cdev: the LED to set
0256  * @brightness: the brightness to set it to
0257  *
0258  * Set an LED's brightness, and, if necessary, cancel the
0259  * software blink timer that implements blinking when the
0260  * hardware doesn't. This function is guaranteed not to sleep.
0261  */
0262 void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness);
0263 
0264 /**
0265  * led_set_brightness_sync - set LED brightness synchronously
0266  * @led_cdev: the LED to set
0267  * @value: the brightness to set it to
0268  *
0269  * Set an LED's brightness immediately. This function will block
0270  * the caller for the time required for accessing device registers,
0271  * and it can sleep.
0272  *
0273  * Returns: 0 on success or negative error value on failure
0274  */
0275 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value);
0276 
0277 /**
0278  * led_update_brightness - update LED brightness
0279  * @led_cdev: the LED to query
0280  *
0281  * Get an LED's current brightness and update led_cdev->brightness
0282  * member with the obtained value.
0283  *
0284  * Returns: 0 on success or negative error value on failure
0285  */
0286 int led_update_brightness(struct led_classdev *led_cdev);
0287 
0288 /**
0289  * led_get_default_pattern - return default pattern
0290  *
0291  * @led_cdev: the LED to get default pattern for
0292  * @size:     pointer for storing the number of elements in returned array,
0293  *            modified only if return != NULL
0294  *
0295  * Return:    Allocated array of integers with default pattern from device tree
0296  *            or NULL.  Caller is responsible for kfree().
0297  */
0298 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size);
0299 
0300 /**
0301  * led_sysfs_disable - disable LED sysfs interface
0302  * @led_cdev: the LED to set
0303  *
0304  * Disable the led_cdev's sysfs interface.
0305  */
0306 void led_sysfs_disable(struct led_classdev *led_cdev);
0307 
0308 /**
0309  * led_sysfs_enable - enable LED sysfs interface
0310  * @led_cdev: the LED to set
0311  *
0312  * Enable the led_cdev's sysfs interface.
0313  */
0314 void led_sysfs_enable(struct led_classdev *led_cdev);
0315 
0316 /**
0317  * led_compose_name - compose LED class device name
0318  * @dev: LED controller device object
0319  * @init_data: the LED class device initialization data
0320  * @led_classdev_name: composed LED class device name
0321  *
0322  * Create LED class device name basing on the provided init_data argument.
0323  * The name can have <devicename:color:function> or <color:function>.
0324  * form, depending on the init_data configuration.
0325  *
0326  * Returns: 0 on success or negative error value on failure
0327  */
0328 int led_compose_name(struct device *dev, struct led_init_data *init_data,
0329              char *led_classdev_name);
0330 
0331 /**
0332  * led_sysfs_is_disabled - check if LED sysfs interface is disabled
0333  * @led_cdev: the LED to query
0334  *
0335  * Returns: true if the led_cdev's sysfs interface is disabled.
0336  */
0337 static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
0338 {
0339     return led_cdev->flags & LED_SYSFS_DISABLE;
0340 }
0341 
0342 /*
0343  * LED Triggers
0344  */
0345 /* Registration functions for simple triggers */
0346 #define DEFINE_LED_TRIGGER(x)       static struct led_trigger *x;
0347 #define DEFINE_LED_TRIGGER_GLOBAL(x)    struct led_trigger *x;
0348 
0349 #ifdef CONFIG_LEDS_TRIGGERS
0350 
0351 #define TRIG_NAME_MAX 50
0352 
0353 struct led_trigger {
0354     /* Trigger Properties */
0355     const char   *name;
0356     int     (*activate)(struct led_classdev *led_cdev);
0357     void        (*deactivate)(struct led_classdev *led_cdev);
0358 
0359     /* LED-private triggers have this set */
0360     struct led_hw_trigger_type *trigger_type;
0361 
0362     /* LEDs under control by this trigger (for simple triggers) */
0363     spinlock_t    leddev_list_lock;
0364     struct list_head  led_cdevs;
0365 
0366     /* Link to next registered trigger */
0367     struct list_head  next_trig;
0368 
0369     const struct attribute_group **groups;
0370 };
0371 
0372 /*
0373  * Currently the attributes in struct led_trigger::groups are added directly to
0374  * the LED device. As this might change in the future, the following
0375  * macros abstract getting the LED device and its trigger_data from the dev
0376  * parameter passed to the attribute accessor functions.
0377  */
0378 #define led_trigger_get_led(dev)    ((struct led_classdev *)dev_get_drvdata((dev)))
0379 #define led_trigger_get_drvdata(dev)    (led_get_trigger_data(led_trigger_get_led(dev)))
0380 
0381 /* Registration functions for complex triggers */
0382 int led_trigger_register(struct led_trigger *trigger);
0383 void led_trigger_unregister(struct led_trigger *trigger);
0384 int devm_led_trigger_register(struct device *dev,
0385                      struct led_trigger *trigger);
0386 
0387 void led_trigger_register_simple(const char *name,
0388                 struct led_trigger **trigger);
0389 void led_trigger_unregister_simple(struct led_trigger *trigger);
0390 void led_trigger_event(struct led_trigger *trigger,  enum led_brightness event);
0391 void led_trigger_blink(struct led_trigger *trigger, unsigned long *delay_on,
0392                unsigned long *delay_off);
0393 void led_trigger_blink_oneshot(struct led_trigger *trigger,
0394                    unsigned long *delay_on,
0395                    unsigned long *delay_off,
0396                    int invert);
0397 void led_trigger_set_default(struct led_classdev *led_cdev);
0398 int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger);
0399 void led_trigger_remove(struct led_classdev *led_cdev);
0400 
0401 static inline void led_set_trigger_data(struct led_classdev *led_cdev,
0402                     void *trigger_data)
0403 {
0404     led_cdev->trigger_data = trigger_data;
0405 }
0406 
0407 static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
0408 {
0409     return led_cdev->trigger_data;
0410 }
0411 
0412 /**
0413  * led_trigger_rename_static - rename a trigger
0414  * @name: the new trigger name
0415  * @trig: the LED trigger to rename
0416  *
0417  * Change a LED trigger name by copying the string passed in
0418  * name into current trigger name, which MUST be large
0419  * enough for the new string.
0420  *
0421  * Note that name must NOT point to the same string used
0422  * during LED registration, as that could lead to races.
0423  *
0424  * This is meant to be used on triggers with statically
0425  * allocated name.
0426  */
0427 void led_trigger_rename_static(const char *name, struct led_trigger *trig);
0428 
0429 #define module_led_trigger(__led_trigger) \
0430     module_driver(__led_trigger, led_trigger_register, \
0431               led_trigger_unregister)
0432 
0433 #else
0434 
0435 /* Trigger has no members */
0436 struct led_trigger {};
0437 
0438 /* Trigger inline empty functions */
0439 static inline void led_trigger_register_simple(const char *name,
0440                     struct led_trigger **trigger) {}
0441 static inline void led_trigger_unregister_simple(struct led_trigger *trigger) {}
0442 static inline void led_trigger_event(struct led_trigger *trigger,
0443                 enum led_brightness event) {}
0444 static inline void led_trigger_blink(struct led_trigger *trigger,
0445                       unsigned long *delay_on,
0446                       unsigned long *delay_off) {}
0447 static inline void led_trigger_blink_oneshot(struct led_trigger *trigger,
0448                       unsigned long *delay_on,
0449                       unsigned long *delay_off,
0450                       int invert) {}
0451 static inline void led_trigger_set_default(struct led_classdev *led_cdev) {}
0452 static inline int led_trigger_set(struct led_classdev *led_cdev,
0453                   struct led_trigger *trigger)
0454 {
0455     return 0;
0456 }
0457 
0458 static inline void led_trigger_remove(struct led_classdev *led_cdev) {}
0459 static inline void led_set_trigger_data(struct led_classdev *led_cdev) {}
0460 static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
0461 {
0462     return NULL;
0463 }
0464 
0465 #endif /* CONFIG_LEDS_TRIGGERS */
0466 
0467 /* Trigger specific functions */
0468 #ifdef CONFIG_LEDS_TRIGGER_DISK
0469 void ledtrig_disk_activity(bool write);
0470 #else
0471 static inline void ledtrig_disk_activity(bool write) {}
0472 #endif
0473 
0474 #ifdef CONFIG_LEDS_TRIGGER_MTD
0475 void ledtrig_mtd_activity(void);
0476 #else
0477 static inline void ledtrig_mtd_activity(void) {}
0478 #endif
0479 
0480 #if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
0481 void ledtrig_flash_ctrl(bool on);
0482 void ledtrig_torch_ctrl(bool on);
0483 #else
0484 static inline void ledtrig_flash_ctrl(bool on) {}
0485 static inline void ledtrig_torch_ctrl(bool on) {}
0486 #endif
0487 
0488 /*
0489  * Generic LED platform data for describing LED names and default triggers.
0490  */
0491 struct led_info {
0492     const char  *name;
0493     const char  *default_trigger;
0494     int     flags;
0495 };
0496 
0497 struct led_platform_data {
0498     int     num_leds;
0499     struct led_info *leds;
0500 };
0501 
0502 struct led_properties {
0503     u32     color;
0504     bool        color_present;
0505     const char  *function;
0506     u32     func_enum;
0507     bool        func_enum_present;
0508     const char  *label;
0509 };
0510 
0511 struct gpio_desc;
0512 typedef int (*gpio_blink_set_t)(struct gpio_desc *desc, int state,
0513                 unsigned long *delay_on,
0514                 unsigned long *delay_off);
0515 
0516 /* For the leds-gpio driver */
0517 struct gpio_led {
0518     const char *name;
0519     const char *default_trigger;
0520     unsigned    gpio;
0521     unsigned    active_low : 1;
0522     unsigned    retain_state_suspended : 1;
0523     unsigned    panic_indicator : 1;
0524     unsigned    default_state : 2;
0525     unsigned    retain_state_shutdown : 1;
0526     /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
0527     struct gpio_desc *gpiod;
0528 };
0529 #define LEDS_GPIO_DEFSTATE_OFF      LEDS_DEFSTATE_OFF
0530 #define LEDS_GPIO_DEFSTATE_ON       LEDS_DEFSTATE_ON
0531 #define LEDS_GPIO_DEFSTATE_KEEP     LEDS_DEFSTATE_KEEP
0532 
0533 struct gpio_led_platform_data {
0534     int         num_leds;
0535     const struct gpio_led *leds;
0536 
0537 #define GPIO_LED_NO_BLINK_LOW   0   /* No blink GPIO state low */
0538 #define GPIO_LED_NO_BLINK_HIGH  1   /* No blink GPIO state high */
0539 #define GPIO_LED_BLINK      2   /* Please, blink */
0540     gpio_blink_set_t    gpio_blink_set;
0541 };
0542 
0543 #ifdef CONFIG_NEW_LEDS
0544 struct platform_device *gpio_led_register_device(
0545         int id, const struct gpio_led_platform_data *pdata);
0546 #else
0547 static inline struct platform_device *gpio_led_register_device(
0548         int id, const struct gpio_led_platform_data *pdata)
0549 {
0550     return 0;
0551 }
0552 #endif
0553 
0554 enum cpu_led_event {
0555     CPU_LED_IDLE_START, /* CPU enters idle */
0556     CPU_LED_IDLE_END,   /* CPU idle ends */
0557     CPU_LED_START,      /* Machine starts, especially resume */
0558     CPU_LED_STOP,       /* Machine stops, especially suspend */
0559     CPU_LED_HALTED,     /* Machine shutdown */
0560 };
0561 #ifdef CONFIG_LEDS_TRIGGER_CPU
0562 void ledtrig_cpu(enum cpu_led_event evt);
0563 #else
0564 static inline void ledtrig_cpu(enum cpu_led_event evt)
0565 {
0566     return;
0567 }
0568 #endif
0569 
0570 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
0571 void led_classdev_notify_brightness_hw_changed(
0572     struct led_classdev *led_cdev, unsigned int brightness);
0573 #else
0574 static inline void led_classdev_notify_brightness_hw_changed(
0575     struct led_classdev *led_cdev, enum led_brightness brightness) { }
0576 #endif
0577 
0578 /**
0579  * struct led_pattern - pattern interval settings
0580  * @delta_t: pattern interval delay, in milliseconds
0581  * @brightness: pattern interval brightness
0582  */
0583 struct led_pattern {
0584     u32 delta_t;
0585     int brightness;
0586 };
0587 
0588 enum led_audio {
0589     LED_AUDIO_MUTE,     /* master mute LED */
0590     LED_AUDIO_MICMUTE,  /* mic mute LED */
0591     NUM_AUDIO_LEDS
0592 };
0593 
0594 #if IS_ENABLED(CONFIG_LEDS_TRIGGER_AUDIO)
0595 enum led_brightness ledtrig_audio_get(enum led_audio type);
0596 void ledtrig_audio_set(enum led_audio type, enum led_brightness state);
0597 #else
0598 static inline enum led_brightness ledtrig_audio_get(enum led_audio type)
0599 {
0600     return LED_OFF;
0601 }
0602 static inline void ledtrig_audio_set(enum led_audio type,
0603                      enum led_brightness state)
0604 {
0605 }
0606 #endif
0607 
0608 #endif      /* __LINUX_LEDS_H_INCLUDED */