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> |
Jacek Anaszewski | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 16 | #include <linux/property.h> |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 17 | #include <linux/rwsem.h> |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 18 | #include <linux/slab.h> |
Jacek Anaszewski | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 19 | #include <uapi/linux/uleds.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 | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 28 | const char * const led_colors[LED_COLOR_ID_MAX] = { |
| 29 | [LED_COLOR_ID_WHITE] = "white", |
| 30 | [LED_COLOR_ID_RED] = "red", |
| 31 | [LED_COLOR_ID_GREEN] = "green", |
| 32 | [LED_COLOR_ID_BLUE] = "blue", |
| 33 | [LED_COLOR_ID_AMBER] = "amber", |
| 34 | [LED_COLOR_ID_VIOLET] = "violet", |
| 35 | [LED_COLOR_ID_YELLOW] = "yellow", |
| 36 | [LED_COLOR_ID_IR] = "ir", |
Dan Murphy | 10d3e0d | 2020-07-13 10:45:32 -0500 | [diff] [blame] | 37 | [LED_COLOR_ID_MULTI] = "multicolor", |
Pavel Machek | 54212f5 | 2020-08-03 13:20:06 +0200 | [diff] [blame] | 38 | [LED_COLOR_ID_RGB] = "rgb", |
Jacek Anaszewski | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 39 | }; |
| 40 | EXPORT_SYMBOL_GPL(led_colors); |
| 41 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 42 | static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value) |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 43 | { |
| 44 | if (!led_cdev->brightness_set) |
| 45 | return -ENOTSUPP; |
| 46 | |
| 47 | led_cdev->brightness_set(led_cdev, value); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 52 | static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value) |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 53 | { |
| 54 | if (!led_cdev->brightness_set_blocking) |
| 55 | return -ENOTSUPP; |
| 56 | |
| 57 | return led_cdev->brightness_set_blocking(led_cdev, value); |
| 58 | } |
| 59 | |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 60 | static void led_timer_function(struct timer_list *t) |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 61 | { |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 62 | struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 63 | unsigned long brightness; |
| 64 | unsigned long delay; |
| 65 | |
| 66 | if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 67 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 68 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 72 | if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP, |
| 73 | &led_cdev->work_flags)) { |
| 74 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | brightness = led_get_brightness(led_cdev); |
| 79 | if (!brightness) { |
| 80 | /* Time to switch the LED on. */ |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 81 | if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, |
| 82 | &led_cdev->work_flags)) |
| 83 | brightness = led_cdev->new_blink_brightness; |
| 84 | else |
| 85 | brightness = led_cdev->blink_brightness; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 86 | delay = led_cdev->blink_delay_on; |
| 87 | } else { |
| 88 | /* Store the current brightness value to be able |
| 89 | * to restore it when the delay_off period is over. |
| 90 | */ |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 91 | led_cdev->blink_brightness = brightness; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 92 | brightness = LED_OFF; |
| 93 | delay = led_cdev->blink_delay_off; |
| 94 | } |
| 95 | |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 96 | led_set_brightness_nosleep(led_cdev, brightness); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 97 | |
| 98 | /* Return in next iteration if led is in one-shot mode and we are in |
| 99 | * the final blink state so that the led is toggled each delay_on + |
| 100 | * delay_off milliseconds in worst case. |
| 101 | */ |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 102 | if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) { |
| 103 | if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) { |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 104 | if (brightness) |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 105 | set_bit(LED_BLINK_ONESHOT_STOP, |
| 106 | &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 107 | } else { |
| 108 | if (!brightness) |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 109 | set_bit(LED_BLINK_ONESHOT_STOP, |
| 110 | &led_cdev->work_flags); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); |
| 115 | } |
| 116 | |
| 117 | static void set_brightness_delayed(struct work_struct *ws) |
| 118 | { |
| 119 | struct led_classdev *led_cdev = |
| 120 | container_of(ws, struct led_classdev, set_brightness_work); |
Jacek Anaszewski | 1afcadf | 2015-10-19 09:04:01 +0200 | [diff] [blame] | 121 | int ret = 0; |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 122 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 123 | if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 124 | led_cdev->delayed_set_value = LED_OFF; |
| 125 | led_stop_software_blink(led_cdev); |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 126 | } |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 127 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 128 | ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value); |
| 129 | if (ret == -ENOTSUPP) |
| 130 | ret = __led_set_brightness_blocking(led_cdev, |
| 131 | led_cdev->delayed_set_value); |
Heiner Kallweit | d84d80f | 2016-01-22 21:43:48 +0100 | [diff] [blame] | 132 | if (ret < 0 && |
| 133 | /* LED HW might have been unplugged, therefore don't warn */ |
| 134 | !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) && |
| 135 | (led_cdev->flags & LED_HW_PLUGGABLE))) |
Jacek Anaszewski | 1afcadf | 2015-10-19 09:04:01 +0200 | [diff] [blame] | 136 | dev_err(led_cdev->dev, |
| 137 | "Setting an LED's brightness failed (%d)\n", ret); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 140 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 141 | unsigned long delay_on, |
| 142 | unsigned long delay_off) |
| 143 | { |
| 144 | int current_brightness; |
| 145 | |
| 146 | current_brightness = led_get_brightness(led_cdev); |
| 147 | if (current_brightness) |
| 148 | led_cdev->blink_brightness = current_brightness; |
| 149 | if (!led_cdev->blink_brightness) |
| 150 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 151 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 152 | led_cdev->blink_delay_on = delay_on; |
| 153 | led_cdev->blink_delay_off = delay_off; |
| 154 | |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 155 | /* never on - just set to off */ |
| 156 | if (!delay_on) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 157 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 158 | return; |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 159 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 160 | |
| 161 | /* never off - just set to brightness */ |
| 162 | if (!delay_off) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 163 | led_set_brightness_nosleep(led_cdev, |
| 164 | led_cdev->blink_brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 168 | set_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 169 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 173 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 174 | unsigned long *delay_on, |
| 175 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 176 | { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 177 | if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 178 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 179 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 180 | return; |
| 181 | |
| 182 | /* blink with 1 Hz as default if nothing specified */ |
| 183 | if (!*delay_on && !*delay_off) |
| 184 | *delay_on = *delay_off = 500; |
| 185 | |
| 186 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 187 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 188 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 189 | void led_init_core(struct led_classdev *led_cdev) |
| 190 | { |
| 191 | INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); |
| 192 | |
Kees Cook | 4940466 | 2017-10-25 03:30:01 -0700 | [diff] [blame] | 193 | timer_setup(&led_cdev->blink_timer, led_timer_function, 0); |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 194 | } |
| 195 | EXPORT_SYMBOL_GPL(led_init_core); |
| 196 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 197 | void led_blink_set(struct led_classdev *led_cdev, |
| 198 | unsigned long *delay_on, |
| 199 | unsigned long *delay_off) |
| 200 | { |
Jacek Anaszewski | 7b6af2c | 2018-01-03 21:13:45 +0100 | [diff] [blame] | 201 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 202 | |
Jacek Anaszewski | 7b6af2c | 2018-01-03 21:13:45 +0100 | [diff] [blame] | 203 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 204 | clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); |
| 205 | clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 206 | |
| 207 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 208 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(led_blink_set); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 210 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 211 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 212 | unsigned long *delay_on, |
| 213 | unsigned long *delay_off, |
| 214 | int invert) |
| 215 | { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 216 | if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 217 | timer_pending(&led_cdev->blink_timer)) |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 218 | return; |
| 219 | |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 220 | set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); |
| 221 | clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 222 | |
| 223 | if (invert) |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 224 | set_bit(LED_BLINK_INVERT, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 225 | else |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 226 | clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 227 | |
| 228 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 229 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(led_blink_set_oneshot); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 231 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 232 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 233 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 234 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 235 | led_cdev->blink_delay_on = 0; |
| 236 | led_cdev->blink_delay_off = 0; |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 237 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 240 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 241 | void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness) |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 242 | { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 243 | /* |
Tony Makkiel | 7cfe749 | 2016-05-18 17:22:45 +0100 | [diff] [blame] | 244 | * If software blink is active, delay brightness setting |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 245 | * until the next timer tick. |
| 246 | */ |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 247 | if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) { |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 248 | /* |
| 249 | * If we need to disable soft blinking delegate this to the |
| 250 | * work queue task to avoid problems in case we are called |
| 251 | * from hard irq context. |
| 252 | */ |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 253 | if (!brightness) { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 254 | set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags); |
Stas Sergeev | 76931ed | 2015-05-14 08:24:02 -0700 | [diff] [blame] | 255 | schedule_work(&led_cdev->set_brightness_work); |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 256 | } else { |
Hans de Goede | a9c6ce5 | 2016-11-08 14:38:46 +0100 | [diff] [blame] | 257 | set_bit(LED_BLINK_BRIGHTNESS_CHANGE, |
| 258 | &led_cdev->work_flags); |
Hans de Goede | eb1610b | 2016-10-23 21:47:26 +0200 | [diff] [blame] | 259 | led_cdev->new_blink_brightness = brightness; |
Jacek Anaszewski | f1e80c0 | 2015-10-07 11:10:39 +0200 | [diff] [blame] | 260 | } |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 261 | return; |
| 262 | } |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 263 | |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 264 | led_set_brightness_nosleep(led_cdev, brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 265 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 266 | EXPORT_SYMBOL_GPL(led_set_brightness); |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 267 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 268 | void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value) |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 269 | { |
| 270 | /* Use brightness_set op if available, it is guaranteed not to sleep */ |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 271 | if (!__led_set_brightness(led_cdev, value)) |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 272 | return; |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 273 | |
| 274 | /* If brightness setting can sleep, delegate it to a work queue task */ |
| 275 | led_cdev->delayed_set_value = value; |
| 276 | schedule_work(&led_cdev->set_brightness_work); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(led_set_brightness_nopm); |
| 279 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 280 | void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value) |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 281 | { |
| 282 | led_cdev->brightness = min(value, led_cdev->max_brightness); |
| 283 | |
| 284 | if (led_cdev->flags & LED_SUSPENDED) |
| 285 | return; |
| 286 | |
| 287 | led_set_brightness_nopm(led_cdev, led_cdev->brightness); |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(led_set_brightness_nosleep); |
| 290 | |
Abanoub Sameh | af0bfab | 2020-12-11 22:42:08 +0200 | [diff] [blame] | 291 | int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value) |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 292 | { |
| 293 | if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) |
| 294 | return -EBUSY; |
| 295 | |
| 296 | led_cdev->brightness = min(value, led_cdev->max_brightness); |
| 297 | |
| 298 | if (led_cdev->flags & LED_SUSPENDED) |
| 299 | return 0; |
| 300 | |
Heiner Kallweit | d4887af | 2016-02-16 20:21:56 +0100 | [diff] [blame] | 301 | return __led_set_brightness_blocking(led_cdev, led_cdev->brightness); |
Jacek Anaszewski | 13ae79b | 2015-10-07 11:10:43 +0200 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL_GPL(led_set_brightness_sync); |
| 304 | |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 305 | int led_update_brightness(struct led_classdev *led_cdev) |
| 306 | { |
| 307 | int ret = 0; |
| 308 | |
| 309 | if (led_cdev->brightness_get) { |
| 310 | ret = led_cdev->brightness_get(led_cdev); |
| 311 | if (ret >= 0) { |
| 312 | led_cdev->brightness = ret; |
| 313 | return 0; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
Jacek Anaszewski | 2806e2f | 2015-09-28 15:07:10 +0200 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(led_update_brightness); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 320 | |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 321 | u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) |
| 322 | { |
Andy Shevchenko | fd81d7e | 2019-08-22 18:19:28 +0300 | [diff] [blame] | 323 | struct fwnode_handle *fwnode = led_cdev->dev->fwnode; |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 324 | u32 *pattern; |
| 325 | int count; |
| 326 | |
Andy Shevchenko | fd81d7e | 2019-08-22 18:19:28 +0300 | [diff] [blame] | 327 | count = fwnode_property_count_u32(fwnode, "led-pattern"); |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 328 | if (count < 0) |
| 329 | return NULL; |
| 330 | |
| 331 | pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); |
| 332 | if (!pattern) |
| 333 | return NULL; |
| 334 | |
Andy Shevchenko | fd81d7e | 2019-08-22 18:19:28 +0300 | [diff] [blame] | 335 | if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) { |
Krzysztof Kozlowski | 8e1f4561 | 2019-01-09 15:44:46 +0100 | [diff] [blame] | 336 | kfree(pattern); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | *size = count; |
| 341 | |
| 342 | return pattern; |
| 343 | } |
| 344 | EXPORT_SYMBOL_GPL(led_get_default_pattern); |
| 345 | |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 346 | /* Caller must ensure led_cdev->led_access held */ |
| 347 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 348 | { |
| 349 | lockdep_assert_held(&led_cdev->led_access); |
| 350 | |
| 351 | led_cdev->flags |= LED_SYSFS_DISABLE; |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(led_sysfs_disable); |
| 354 | |
| 355 | /* Caller must ensure led_cdev->led_access held */ |
| 356 | void led_sysfs_enable(struct led_classdev *led_cdev) |
| 357 | { |
| 358 | lockdep_assert_held(&led_cdev->led_access); |
| 359 | |
| 360 | led_cdev->flags &= ~LED_SYSFS_DISABLE; |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(led_sysfs_enable); |
Jacek Anaszewski | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 363 | |
| 364 | static void led_parse_fwnode_props(struct device *dev, |
| 365 | struct fwnode_handle *fwnode, |
| 366 | struct led_properties *props) |
| 367 | { |
| 368 | int ret; |
| 369 | |
| 370 | if (!fwnode) |
| 371 | return; |
| 372 | |
| 373 | if (fwnode_property_present(fwnode, "label")) { |
| 374 | ret = fwnode_property_read_string(fwnode, "label", &props->label); |
| 375 | if (ret) |
| 376 | dev_err(dev, "Error parsing 'label' property (%d)\n", ret); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | if (fwnode_property_present(fwnode, "color")) { |
| 381 | ret = fwnode_property_read_u32(fwnode, "color", &props->color); |
| 382 | if (ret) |
| 383 | dev_err(dev, "Error parsing 'color' property (%d)\n", ret); |
| 384 | else if (props->color >= LED_COLOR_ID_MAX) |
| 385 | dev_err(dev, "LED color identifier out of range\n"); |
| 386 | else |
| 387 | props->color_present = true; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | if (!fwnode_property_present(fwnode, "function")) |
| 392 | return; |
| 393 | |
| 394 | ret = fwnode_property_read_string(fwnode, "function", &props->function); |
| 395 | if (ret) { |
| 396 | dev_err(dev, |
| 397 | "Error parsing 'function' property (%d)\n", |
| 398 | ret); |
| 399 | } |
| 400 | |
| 401 | if (!fwnode_property_present(fwnode, "function-enumerator")) |
| 402 | return; |
| 403 | |
| 404 | ret = fwnode_property_read_u32(fwnode, "function-enumerator", |
| 405 | &props->func_enum); |
| 406 | if (ret) { |
| 407 | dev_err(dev, |
| 408 | "Error parsing 'function-enumerator' property (%d)\n", |
| 409 | ret); |
| 410 | } else { |
| 411 | props->func_enum_present = true; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | int led_compose_name(struct device *dev, struct led_init_data *init_data, |
| 416 | char *led_classdev_name) |
| 417 | { |
| 418 | struct led_properties props = {}; |
| 419 | struct fwnode_handle *fwnode = init_data->fwnode; |
| 420 | const char *devicename = init_data->devicename; |
| 421 | |
Pavel Machek | 77dce3a | 2020-08-03 14:00:06 +0200 | [diff] [blame] | 422 | /* We want to label LEDs that can produce full range of colors |
| 423 | * as RGB, not multicolor */ |
| 424 | BUG_ON(props.color == LED_COLOR_ID_MULTI); |
| 425 | |
Jacek Anaszewski | bb4e9af | 2019-06-09 20:19:04 +0200 | [diff] [blame] | 426 | if (!led_classdev_name) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | led_parse_fwnode_props(dev, fwnode, &props); |
| 430 | |
| 431 | if (props.label) { |
| 432 | /* |
| 433 | * If init_data.devicename is NULL, then it indicates that |
| 434 | * DT label should be used as-is for LED class device name. |
| 435 | * Otherwise the label is prepended with devicename to compose |
| 436 | * the final LED class device name. |
| 437 | */ |
| 438 | if (!devicename) { |
| 439 | strscpy(led_classdev_name, props.label, |
| 440 | LED_MAX_NAME_SIZE); |
| 441 | } else { |
| 442 | snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", |
| 443 | devicename, props.label); |
| 444 | } |
| 445 | } else if (props.function || props.color_present) { |
| 446 | char tmp_buf[LED_MAX_NAME_SIZE]; |
| 447 | |
| 448 | if (props.func_enum_present) { |
| 449 | snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d", |
| 450 | props.color_present ? led_colors[props.color] : "", |
| 451 | props.function ?: "", props.func_enum); |
| 452 | } else { |
| 453 | snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s", |
| 454 | props.color_present ? led_colors[props.color] : "", |
| 455 | props.function ?: ""); |
| 456 | } |
| 457 | if (init_data->devname_mandatory) { |
| 458 | snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", |
| 459 | devicename, tmp_buf); |
| 460 | } else { |
| 461 | strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE); |
| 462 | |
| 463 | } |
| 464 | } else if (init_data->default_label) { |
| 465 | if (!devicename) { |
| 466 | dev_err(dev, "Legacy LED naming requires devicename segment"); |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", |
| 470 | devicename, init_data->default_label); |
| 471 | } else if (is_of_node(fwnode)) { |
| 472 | strscpy(led_classdev_name, to_of_node(fwnode)->name, |
| 473 | LED_MAX_NAME_SIZE); |
| 474 | } else |
| 475 | return -EINVAL; |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(led_compose_name); |
Denis Osterland-Heim | 791bc41 | 2021-06-08 08:35:53 +0200 | [diff] [blame] | 480 | |
| 481 | enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode) |
| 482 | { |
| 483 | const char *state = NULL; |
| 484 | |
| 485 | if (!fwnode_property_read_string(fwnode, "default-state", &state)) { |
| 486 | if (!strcmp(state, "keep")) |
| 487 | return LEDS_DEFSTATE_KEEP; |
| 488 | if (!strcmp(state, "on")) |
| 489 | return LEDS_DEFSTATE_ON; |
| 490 | } |
| 491 | |
| 492 | return LEDS_DEFSTATE_OFF; |
| 493 | } |
| 494 | EXPORT_SYMBOL_GPL(led_init_default_state_get); |