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