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