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