Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 2 | /* |
| 3 | * LED Class Core |
| 4 | * |
| 5 | * Copyright 2005-2006 Openedhand Ltd. |
| 6 | * |
| 7 | * Author: Richard Purdie <rpurdie@openedhand.com> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 11 | #include <linux/leds.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 12 | #include <linux/list.h> |
| 13 | #include <linux/module.h> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 15 | #include <linux/of.h> |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 16 | #include <linux/rwsem.h> |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 18 | #include "leds.h" |
| 19 | |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 20 | DECLARE_RWSEM(leds_list_lock); |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 21 | EXPORT_SYMBOL_GPL(leds_list_lock); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 22 | |
| 23 | LIST_HEAD(leds_list); |
| 24 | EXPORT_SYMBOL_GPL(leds_list); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 25 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 26 | static int __led_set_brightness(struct led_classdev *led_cdev, |
| 27 | enum led_brightness value) |
| 28 | { |
| 29 | if (!led_cdev->brightness_set) |
| 30 | return -ENOTSUPP; |
| 31 | |
| 32 | led_cdev->brightness_set(led_cdev, value); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int __led_set_brightness_blocking(struct led_classdev *led_cdev, |
| 38 | enum led_brightness value) |
| 39 | { |
| 40 | if (!led_cdev->brightness_set_blocking) |
| 41 | return -ENOTSUPP; |
| 42 | |
| 43 | return led_cdev->brightness_set_blocking(led_cdev, value); |
| 44 | } |
| 45 | |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 46 | static void led_timer_function(struct timer_list *t) |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 47 | { |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 48 | struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 49 | unsigned long brightness; |
| 50 | unsigned long delay; |
| 51 | |
| 52 | if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 53 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 54 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 58 | if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP, |
| 59 | &led_cdev->work_flags)) { |
| 60 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | brightness = led_get_brightness(led_cdev); |
| 65 | if (!brightness) { |
| 66 | /* Time to switch the LED on. */ |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 67 | if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, |
| 68 | &led_cdev->work_flags)) |
| 69 | brightness = led_cdev->new_blink_brightness; |
| 70 | else |
| 71 | brightness = led_cdev->blink_brightness; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 72 | delay = led_cdev->blink_delay_on; |
| 73 | } else { |
| 74 | /* Store the current brightness value to be able |
| 75 | * to restore it when the delay_off period is over. |
| 76 | */ |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 77 | led_cdev->blink_brightness = brightness; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 78 | brightness = LED_OFF; |
| 79 | delay = led_cdev->blink_delay_off; |
| 80 | } |
| 81 | |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 82 | led_set_brightness_nosleep(led_cdev, brightness); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 83 | |
| 84 | /* Return in next iteration if led is in one-shot mode and we are in |
| 85 | * the final blink state so that the led is toggled each delay_on + |
| 86 | * delay_off milliseconds in worst case. |
| 87 | */ |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 88 | if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) { |
| 89 | if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) { |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 90 | if (brightness) |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 91 | set_bit(LED_BLINK_ONESHOT_STOP, |
| 92 | &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 93 | } else { |
| 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 | } |
| 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 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 109 | if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 110 | led_cdev->delayed_set_value = LED_OFF; |
| 111 | led_stop_software_blink(led_cdev); |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 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 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 154 | set_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 155 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 159 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 160 | unsigned long *delay_on, |
| 161 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 162 | { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 163 | if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 164 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 165 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 166 | return; |
| 167 | |
| 168 | /* blink with 1 Hz as default if nothing specified */ |
| 169 | if (!*delay_on && !*delay_off) |
| 170 | *delay_on = *delay_off = 500; |
| 171 | |
| 172 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 173 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 174 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 175 | void led_init_core(struct led_classdev *led_cdev) |
| 176 | { |
| 177 | INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); |
| 178 | |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 179 | timer_setup(&led_cdev->blink_timer, led_timer_function, 0); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 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 | { |
Jacek Anaszewski | 7b6af2c | 2018-01-03 21:13:45 +0100 | [diff] [blame] | 187 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 188 | |
Jacek Anaszewski | 7b6af2c | 2018-01-03 21:13:45 +0100 | [diff] [blame] | 189 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 190 | clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); |
| 191 | clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 192 | |
| 193 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 194 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 195 | EXPORT_SYMBOL_GPL(led_blink_set); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 196 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 197 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 198 | unsigned long *delay_on, |
| 199 | unsigned long *delay_off, |
| 200 | int invert) |
| 201 | { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 202 | if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 203 | timer_pending(&led_cdev->blink_timer)) |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 204 | return; |
| 205 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 206 | set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); |
| 207 | clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 208 | |
| 209 | if (invert) |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 210 | set_bit(LED_BLINK_INVERT, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 211 | else |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 212 | clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 213 | |
| 214 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 215 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 216 | EXPORT_SYMBOL_GPL(led_blink_set_oneshot); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 217 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 218 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 219 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 220 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 221 | led_cdev->blink_delay_on = 0; |
| 222 | led_cdev->blink_delay_off = 0; |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 223 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 224 | } |
| 225 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 226 | |
| 227 | void led_set_brightness(struct led_classdev *led_cdev, |
| 228 | enum led_brightness brightness) |
| 229 | { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 230 | /* |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 231 | * If software blink is active, delay brightness setting |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 232 | * until the next timer tick. |
| 233 | */ |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 234 | if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 235 | /* |
| 236 | * If we need to disable soft blinking delegate this to the |
| 237 | * work queue task to avoid problems in case we are called |
| 238 | * from hard irq context. |
| 239 | */ |
| 240 | if (brightness == LED_OFF) { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 241 | set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags); |
Stas Sergeev | 76931ed | 2015-05-14 08:24:02 -0700 | [diff] [blame] | 242 | schedule_work(&led_cdev->set_brightness_work); |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 243 | } else { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 244 | set_bit(LED_BLINK_BRIGHTNESS_CHANGE, |
| 245 | &led_cdev->work_flags); |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 246 | led_cdev->new_blink_brightness = brightness; |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 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 | |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 311 | u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) |
| 312 | { |
| 313 | struct device_node *np = dev_of_node(led_cdev->dev); |
| 314 | u32 *pattern; |
| 315 | int count; |
| 316 | |
| 317 | if (!np) |
| 318 | return NULL; |
| 319 | |
| 320 | count = of_property_count_u32_elems(np, "led-pattern"); |
| 321 | if (count < 0) |
| 322 | return NULL; |
| 323 | |
| 324 | pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); |
| 325 | if (!pattern) |
| 326 | return NULL; |
| 327 | |
| 328 | if (of_property_read_u32_array(np, "led-pattern", pattern, count)) { |
| 329 | kfree(pattern); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | *size = count; |
| 334 | |
| 335 | return pattern; |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(led_get_default_pattern); |
| 338 | |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 339 | /* Caller must ensure led_cdev->led_access held */ |
| 340 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 341 | { |
| 342 | lockdep_assert_held(&led_cdev->led_access); |
| 343 | |
| 344 | led_cdev->flags |= LED_SYSFS_DISABLE; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(led_sysfs_disable); |
| 347 | |
| 348 | /* Caller must ensure led_cdev->led_access held */ |
| 349 | void led_sysfs_enable(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_enable); |