Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * LED Class Core |
| 3 | * |
| 4 | * Copyright 2005-2006 Openedhand Ltd. |
| 5 | * |
| 6 | * Author: Richard Purdie <rpurdie@openedhand.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 15 | #include <linux/leds.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 19 | #include <linux/rwsem.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 20 | #include "leds.h" |
| 21 | |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 22 | DECLARE_RWSEM(leds_list_lock); |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 23 | EXPORT_SYMBOL_GPL(leds_list_lock); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 24 | |
| 25 | LIST_HEAD(leds_list); |
| 26 | EXPORT_SYMBOL_GPL(leds_list); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 27 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 28 | static int __led_set_brightness(struct led_classdev *led_cdev, |
| 29 | enum led_brightness value) |
| 30 | { |
| 31 | if (!led_cdev->brightness_set) |
| 32 | return -ENOTSUPP; |
| 33 | |
| 34 | led_cdev->brightness_set(led_cdev, value); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int __led_set_brightness_blocking(struct led_classdev *led_cdev, |
| 40 | enum led_brightness value) |
| 41 | { |
| 42 | if (!led_cdev->brightness_set_blocking) |
| 43 | return -ENOTSUPP; |
| 44 | |
| 45 | return led_cdev->brightness_set_blocking(led_cdev, value); |
| 46 | } |
| 47 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 48 | static void led_timer_function(unsigned long data) |
| 49 | { |
| 50 | struct led_classdev *led_cdev = (void *)data; |
| 51 | unsigned long brightness; |
| 52 | unsigned long delay; |
| 53 | |
| 54 | if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 55 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 56 | led_cdev->flags &= ~LED_BLINK_SW; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) { |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 61 | led_cdev->flags &= ~(LED_BLINK_ONESHOT_STOP | LED_BLINK_SW); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | brightness = led_get_brightness(led_cdev); |
| 66 | if (!brightness) { |
| 67 | /* Time to switch the LED on. */ |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 68 | brightness = led_cdev->blink_brightness; |
| 69 | delay = led_cdev->blink_delay_on; |
| 70 | } else { |
| 71 | /* Store the current brightness value to be able |
| 72 | * to restore it when the delay_off period is over. |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 73 | * Do it only if there is no pending blink brightness |
| 74 | * change, to avoid overwriting the new value. |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 75 | */ |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 76 | if (!(led_cdev->flags & LED_BLINK_BRIGHTNESS_CHANGE)) |
| 77 | led_cdev->blink_brightness = brightness; |
| 78 | else |
| 79 | led_cdev->flags &= ~LED_BLINK_BRIGHTNESS_CHANGE; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 80 | brightness = LED_OFF; |
| 81 | delay = led_cdev->blink_delay_off; |
| 82 | } |
| 83 | |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 84 | led_set_brightness_nosleep(led_cdev, brightness); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 85 | |
| 86 | /* Return in next iteration if led is in one-shot mode and we are in |
| 87 | * the final blink state so that the led is toggled each delay_on + |
| 88 | * delay_off milliseconds in worst case. |
| 89 | */ |
| 90 | if (led_cdev->flags & LED_BLINK_ONESHOT) { |
| 91 | if (led_cdev->flags & LED_BLINK_INVERT) { |
| 92 | if (brightness) |
| 93 | led_cdev->flags |= LED_BLINK_ONESHOT_STOP; |
| 94 | } else { |
| 95 | if (!brightness) |
| 96 | led_cdev->flags |= LED_BLINK_ONESHOT_STOP; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); |
| 101 | } |
| 102 | |
| 103 | static void set_brightness_delayed(struct work_struct *ws) |
| 104 | { |
| 105 | struct led_classdev *led_cdev = |
| 106 | container_of(ws, struct led_classdev, set_brightness_work); |
Jacek Anaszewski | 1afcadf | 2015-10-19 09:04:01 +0200 | [diff] [blame] | 107 | int ret = 0; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 108 | |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 109 | if (led_cdev->flags & LED_BLINK_DISABLE) { |
| 110 | led_cdev->delayed_set_value = LED_OFF; |
| 111 | led_stop_software_blink(led_cdev); |
| 112 | led_cdev->flags &= ~LED_BLINK_DISABLE; |
| 113 | } |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 114 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 115 | ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value); |
| 116 | if (ret == -ENOTSUPP) |
| 117 | ret = __led_set_brightness_blocking(led_cdev, |
| 118 | led_cdev->delayed_set_value); |
Heiner Kallweit | d84d80f | 2016-01-22 21:43:48 +0100 | [diff] [blame] | 119 | if (ret < 0 && |
| 120 | /* LED HW might have been unplugged, therefore don't warn */ |
| 121 | !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) && |
| 122 | (led_cdev->flags & LED_HW_PLUGGABLE))) |
Jacek Anaszewski | 1afcadf | 2015-10-19 09:04:01 +0200 | [diff] [blame] | 123 | dev_err(led_cdev->dev, |
| 124 | "Setting an LED's brightness failed (%d)\n", ret); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 127 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 128 | unsigned long delay_on, |
| 129 | unsigned long delay_off) |
| 130 | { |
| 131 | int current_brightness; |
| 132 | |
| 133 | current_brightness = led_get_brightness(led_cdev); |
| 134 | if (current_brightness) |
| 135 | led_cdev->blink_brightness = current_brightness; |
| 136 | if (!led_cdev->blink_brightness) |
| 137 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 138 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 139 | led_cdev->blink_delay_on = delay_on; |
| 140 | led_cdev->blink_delay_off = delay_off; |
| 141 | |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 142 | /* never on - just set to off */ |
| 143 | if (!delay_on) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 144 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 145 | return; |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 146 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 147 | |
| 148 | /* never off - just set to brightness */ |
| 149 | if (!delay_off) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 150 | led_set_brightness_nosleep(led_cdev, |
| 151 | led_cdev->blink_brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 155 | led_cdev->flags |= LED_BLINK_SW; |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 156 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 160 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 161 | unsigned long *delay_on, |
| 162 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 163 | { |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 164 | if (!(led_cdev->flags & LED_BLINK_ONESHOT) && |
| 165 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 166 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 167 | return; |
| 168 | |
| 169 | /* blink with 1 Hz as default if nothing specified */ |
| 170 | if (!*delay_on && !*delay_off) |
| 171 | *delay_on = *delay_off = 500; |
| 172 | |
| 173 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 174 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 175 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 176 | void led_init_core(struct led_classdev *led_cdev) |
| 177 | { |
| 178 | INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); |
| 179 | |
| 180 | setup_timer(&led_cdev->blink_timer, led_timer_function, |
| 181 | (unsigned long)led_cdev); |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(led_init_core); |
| 184 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 185 | void led_blink_set(struct led_classdev *led_cdev, |
| 186 | unsigned long *delay_on, |
| 187 | unsigned long *delay_off) |
| 188 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 189 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 190 | |
| 191 | led_cdev->flags &= ~LED_BLINK_ONESHOT; |
| 192 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 193 | |
| 194 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 195 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(led_blink_set); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 197 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 198 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 199 | unsigned long *delay_on, |
| 200 | unsigned long *delay_off, |
| 201 | int invert) |
| 202 | { |
| 203 | if ((led_cdev->flags & LED_BLINK_ONESHOT) && |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 204 | timer_pending(&led_cdev->blink_timer)) |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 205 | return; |
| 206 | |
| 207 | led_cdev->flags |= LED_BLINK_ONESHOT; |
| 208 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 209 | |
| 210 | if (invert) |
| 211 | led_cdev->flags |= LED_BLINK_INVERT; |
| 212 | else |
| 213 | led_cdev->flags &= ~LED_BLINK_INVERT; |
| 214 | |
| 215 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 216 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(led_blink_set_oneshot); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 218 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 219 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 220 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 221 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 222 | led_cdev->blink_delay_on = 0; |
| 223 | led_cdev->blink_delay_off = 0; |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 224 | led_cdev->flags &= ~LED_BLINK_SW; |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 225 | } |
| 226 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 227 | |
| 228 | void led_set_brightness(struct led_classdev *led_cdev, |
| 229 | enum led_brightness brightness) |
| 230 | { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 231 | /* |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 232 | * If software blink is active, delay brightness setting |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 233 | * until the next timer tick. |
| 234 | */ |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 235 | if (led_cdev->flags & LED_BLINK_SW) { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 236 | /* |
| 237 | * If we need to disable soft blinking delegate this to the |
| 238 | * work queue task to avoid problems in case we are called |
| 239 | * from hard irq context. |
| 240 | */ |
| 241 | if (brightness == LED_OFF) { |
| 242 | led_cdev->flags |= LED_BLINK_DISABLE; |
Stas Sergeev | 76931ed | 2015-05-14 08:24:02 -0700 | [diff] [blame] | 243 | schedule_work(&led_cdev->set_brightness_work); |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 244 | } else { |
| 245 | led_cdev->flags |= LED_BLINK_BRIGHTNESS_CHANGE; |
| 246 | led_cdev->blink_brightness = brightness; |
| 247 | } |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 248 | return; |
| 249 | } |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 250 | |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 251 | led_set_brightness_nosleep(led_cdev, brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 252 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 253 | EXPORT_SYMBOL_GPL(led_set_brightness); |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 254 | |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 255 | void led_set_brightness_nopm(struct led_classdev *led_cdev, |
| 256 | enum led_brightness value) |
| 257 | { |
| 258 | /* Use brightness_set op if available, it is guaranteed not to sleep */ |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 259 | if (!__led_set_brightness(led_cdev, value)) |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 260 | return; |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 261 | |
| 262 | /* If brightness setting can sleep, delegate it to a work queue task */ |
| 263 | led_cdev->delayed_set_value = value; |
| 264 | schedule_work(&led_cdev->set_brightness_work); |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(led_set_brightness_nopm); |
| 267 | |
| 268 | void led_set_brightness_nosleep(struct led_classdev *led_cdev, |
| 269 | enum led_brightness value) |
| 270 | { |
| 271 | led_cdev->brightness = min(value, led_cdev->max_brightness); |
| 272 | |
| 273 | if (led_cdev->flags & LED_SUSPENDED) |
| 274 | return; |
| 275 | |
| 276 | led_set_brightness_nopm(led_cdev, led_cdev->brightness); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(led_set_brightness_nosleep); |
| 279 | |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 280 | int led_set_brightness_sync(struct led_classdev *led_cdev, |
| 281 | enum led_brightness value) |
| 282 | { |
| 283 | if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) |
| 284 | return -EBUSY; |
| 285 | |
| 286 | led_cdev->brightness = min(value, led_cdev->max_brightness); |
| 287 | |
| 288 | if (led_cdev->flags & LED_SUSPENDED) |
| 289 | return 0; |
| 290 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 291 | return __led_set_brightness_blocking(led_cdev, led_cdev->brightness); |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 292 | } |
| 293 | EXPORT_SYMBOL_GPL(led_set_brightness_sync); |
| 294 | |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 295 | int led_update_brightness(struct led_classdev *led_cdev) |
| 296 | { |
| 297 | int ret = 0; |
| 298 | |
| 299 | if (led_cdev->brightness_get) { |
| 300 | ret = led_cdev->brightness_get(led_cdev); |
| 301 | if (ret >= 0) { |
| 302 | led_cdev->brightness = ret; |
| 303 | return 0; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return ret; |
| 308 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(led_update_brightness); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 310 | |
| 311 | /* Caller must ensure led_cdev->led_access held */ |
| 312 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 313 | { |
| 314 | lockdep_assert_held(&led_cdev->led_access); |
| 315 | |
| 316 | led_cdev->flags |= LED_SYSFS_DISABLE; |
| 317 | } |
| 318 | EXPORT_SYMBOL_GPL(led_sysfs_disable); |
| 319 | |
| 320 | /* Caller must ensure led_cdev->led_access held */ |
| 321 | void led_sysfs_enable(struct led_classdev *led_cdev) |
| 322 | { |
| 323 | lockdep_assert_held(&led_cdev->led_access); |
| 324 | |
| 325 | led_cdev->flags &= ~LED_SYSFS_DISABLE; |
| 326 | } |
| 327 | EXPORT_SYMBOL_GPL(led_sysfs_enable); |