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