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