1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * LED Class Core 4 * 5 * Copyright 2005-2006 Openedhand Ltd. 6 * 7 * Author: Richard Purdie <rpurdie@openedhand.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/leds.h> 12 #include <linux/list.h> 13 #include <linux/module.h> 14 #include <linux/mutex.h> 15 #include <linux/of.h> 16 #include <linux/property.h> 17 #include <linux/rwsem.h> 18 #include <linux/slab.h> 19 #include <uapi/linux/uleds.h> 20 #include "leds.h" 21 22 DECLARE_RWSEM(leds_list_lock); 23 EXPORT_SYMBOL_GPL(leds_list_lock); 24 25 LIST_HEAD(leds_list); 26 EXPORT_SYMBOL_GPL(leds_list); 27 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", 37 [LED_COLOR_ID_MULTI] = "multicolor", 38 [LED_COLOR_ID_RGB] = "rgb", 39 }; 40 EXPORT_SYMBOL_GPL(led_colors); 41 42 static int __led_set_brightness(struct led_classdev *led_cdev, unsigned int value) 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 52 static int __led_set_brightness_blocking(struct led_classdev *led_cdev, unsigned int value) 53 { 54 if (!led_cdev->brightness_set_blocking) 55 return -ENOTSUPP; 56 57 return led_cdev->brightness_set_blocking(led_cdev, value); 58 } 59 60 static void led_timer_function(struct timer_list *t) 61 { 62 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer); 63 unsigned long brightness; 64 unsigned long delay; 65 66 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { 67 led_set_brightness_nosleep(led_cdev, LED_OFF); 68 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 69 return; 70 } 71 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); 75 return; 76 } 77 78 brightness = led_get_brightness(led_cdev); 79 if (!brightness) { 80 /* Time to switch the LED on. */ 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; 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 */ 91 led_cdev->blink_brightness = brightness; 92 brightness = LED_OFF; 93 delay = led_cdev->blink_delay_off; 94 } 95 96 led_set_brightness_nosleep(led_cdev, brightness); 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 */ 102 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) { 103 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) { 104 if (brightness) 105 set_bit(LED_BLINK_ONESHOT_STOP, 106 &led_cdev->work_flags); 107 } else { 108 if (!brightness) 109 set_bit(LED_BLINK_ONESHOT_STOP, 110 &led_cdev->work_flags); 111 } 112 } 113 114 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); 115 } 116 117 static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev, 118 unsigned int value) 119 { 120 int ret = 0; 121 122 ret = __led_set_brightness(led_cdev, value); 123 if (ret == -ENOTSUPP) 124 ret = __led_set_brightness_blocking(led_cdev, value); 125 if (ret < 0 && 126 /* LED HW might have been unplugged, therefore don't warn */ 127 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) && 128 (led_cdev->flags & LED_HW_PLUGGABLE))) 129 dev_err(led_cdev->dev, 130 "Setting an LED's brightness failed (%d)\n", ret); 131 } 132 133 static void set_brightness_delayed(struct work_struct *ws) 134 { 135 struct led_classdev *led_cdev = 136 container_of(ws, struct led_classdev, set_brightness_work); 137 138 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) { 139 led_stop_software_blink(led_cdev); 140 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags); 141 } 142 143 /* 144 * Triggers may call led_set_brightness(LED_OFF), 145 * led_set_brightness(LED_FULL) in quick succession to disable blinking 146 * and turn the LED on. Both actions may have been scheduled to run 147 * before this work item runs once. To make sure this works properly 148 * handle LED_SET_BRIGHTNESS_OFF first. 149 */ 150 if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags)) 151 set_brightness_delayed_set_brightness(led_cdev, LED_OFF); 152 153 if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags)) 154 set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value); 155 156 if (test_and_clear_bit(LED_SET_BLINK, &led_cdev->work_flags)) { 157 unsigned long delay_on = led_cdev->delayed_delay_on; 158 unsigned long delay_off = led_cdev->delayed_delay_off; 159 160 led_blink_set(led_cdev, &delay_on, &delay_off); 161 } 162 } 163 164 static void led_set_software_blink(struct led_classdev *led_cdev, 165 unsigned long delay_on, 166 unsigned long delay_off) 167 { 168 int current_brightness; 169 170 current_brightness = led_get_brightness(led_cdev); 171 if (current_brightness) 172 led_cdev->blink_brightness = current_brightness; 173 if (!led_cdev->blink_brightness) 174 led_cdev->blink_brightness = led_cdev->max_brightness; 175 176 led_cdev->blink_delay_on = delay_on; 177 led_cdev->blink_delay_off = delay_off; 178 179 /* never on - just set to off */ 180 if (!delay_on) { 181 led_set_brightness_nosleep(led_cdev, LED_OFF); 182 return; 183 } 184 185 /* never off - just set to brightness */ 186 if (!delay_off) { 187 led_set_brightness_nosleep(led_cdev, 188 led_cdev->blink_brightness); 189 return; 190 } 191 192 set_bit(LED_BLINK_SW, &led_cdev->work_flags); 193 mod_timer(&led_cdev->blink_timer, jiffies + 1); 194 } 195 196 197 static void led_blink_setup(struct led_classdev *led_cdev, 198 unsigned long *delay_on, 199 unsigned long *delay_off) 200 { 201 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 202 led_cdev->blink_set && 203 !led_cdev->blink_set(led_cdev, delay_on, delay_off)) 204 return; 205 206 /* blink with 1 Hz as default if nothing specified */ 207 if (!*delay_on && !*delay_off) 208 *delay_on = *delay_off = 500; 209 210 led_set_software_blink(led_cdev, *delay_on, *delay_off); 211 } 212 213 void led_init_core(struct led_classdev *led_cdev) 214 { 215 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); 216 217 timer_setup(&led_cdev->blink_timer, led_timer_function, 0); 218 } 219 EXPORT_SYMBOL_GPL(led_init_core); 220 221 void led_blink_set(struct led_classdev *led_cdev, 222 unsigned long *delay_on, 223 unsigned long *delay_off) 224 { 225 del_timer_sync(&led_cdev->blink_timer); 226 227 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 228 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 229 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 230 231 led_blink_setup(led_cdev, delay_on, delay_off); 232 } 233 EXPORT_SYMBOL_GPL(led_blink_set); 234 235 void led_blink_set_oneshot(struct led_classdev *led_cdev, 236 unsigned long *delay_on, 237 unsigned long *delay_off, 238 int invert) 239 { 240 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 241 timer_pending(&led_cdev->blink_timer)) 242 return; 243 244 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 245 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 246 247 if (invert) 248 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 249 else 250 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 251 252 led_blink_setup(led_cdev, delay_on, delay_off); 253 } 254 EXPORT_SYMBOL_GPL(led_blink_set_oneshot); 255 256 void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on, 257 unsigned long delay_off) 258 { 259 /* If necessary delegate to a work queue task. */ 260 if (led_cdev->blink_set && led_cdev->brightness_set_blocking) { 261 led_cdev->delayed_delay_on = delay_on; 262 led_cdev->delayed_delay_off = delay_off; 263 set_bit(LED_SET_BLINK, &led_cdev->work_flags); 264 schedule_work(&led_cdev->set_brightness_work); 265 return; 266 } 267 268 led_blink_set(led_cdev, &delay_on, &delay_off); 269 } 270 EXPORT_SYMBOL_GPL(led_blink_set_nosleep); 271 272 void led_stop_software_blink(struct led_classdev *led_cdev) 273 { 274 del_timer_sync(&led_cdev->blink_timer); 275 led_cdev->blink_delay_on = 0; 276 led_cdev->blink_delay_off = 0; 277 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 278 } 279 EXPORT_SYMBOL_GPL(led_stop_software_blink); 280 281 void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness) 282 { 283 /* 284 * If software blink is active, delay brightness setting 285 * until the next timer tick. 286 */ 287 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) { 288 /* 289 * If we need to disable soft blinking delegate this to the 290 * work queue task to avoid problems in case we are called 291 * from hard irq context. 292 */ 293 if (!brightness) { 294 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags); 295 schedule_work(&led_cdev->set_brightness_work); 296 } else { 297 set_bit(LED_BLINK_BRIGHTNESS_CHANGE, 298 &led_cdev->work_flags); 299 led_cdev->new_blink_brightness = brightness; 300 } 301 return; 302 } 303 304 led_set_brightness_nosleep(led_cdev, brightness); 305 } 306 EXPORT_SYMBOL_GPL(led_set_brightness); 307 308 void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value) 309 { 310 /* Use brightness_set op if available, it is guaranteed not to sleep */ 311 if (!__led_set_brightness(led_cdev, value)) 312 return; 313 314 /* 315 * Brightness setting can sleep, delegate it to a work queue task. 316 * value 0 / LED_OFF is special, since it also disables hw-blinking 317 * (sw-blink disable is handled in led_set_brightness()). 318 * To avoid a hw-blink-disable getting lost when a second brightness 319 * change is done immediately afterwards (before the work runs), 320 * it uses a separate work_flag. 321 */ 322 if (value) { 323 led_cdev->delayed_set_value = value; 324 set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags); 325 } else { 326 clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags); 327 clear_bit(LED_SET_BLINK, &led_cdev->work_flags); 328 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags); 329 } 330 331 schedule_work(&led_cdev->set_brightness_work); 332 } 333 EXPORT_SYMBOL_GPL(led_set_brightness_nopm); 334 335 void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value) 336 { 337 led_cdev->brightness = min(value, led_cdev->max_brightness); 338 339 if (led_cdev->flags & LED_SUSPENDED) 340 return; 341 342 led_set_brightness_nopm(led_cdev, led_cdev->brightness); 343 } 344 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep); 345 346 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value) 347 { 348 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) 349 return -EBUSY; 350 351 led_cdev->brightness = min(value, led_cdev->max_brightness); 352 353 if (led_cdev->flags & LED_SUSPENDED) 354 return 0; 355 356 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness); 357 } 358 EXPORT_SYMBOL_GPL(led_set_brightness_sync); 359 360 int led_update_brightness(struct led_classdev *led_cdev) 361 { 362 int ret = 0; 363 364 if (led_cdev->brightness_get) { 365 ret = led_cdev->brightness_get(led_cdev); 366 if (ret >= 0) { 367 led_cdev->brightness = ret; 368 return 0; 369 } 370 } 371 372 return ret; 373 } 374 EXPORT_SYMBOL_GPL(led_update_brightness); 375 376 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) 377 { 378 struct fwnode_handle *fwnode = led_cdev->dev->fwnode; 379 u32 *pattern; 380 int count; 381 382 count = fwnode_property_count_u32(fwnode, "led-pattern"); 383 if (count < 0) 384 return NULL; 385 386 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); 387 if (!pattern) 388 return NULL; 389 390 if (fwnode_property_read_u32_array(fwnode, "led-pattern", pattern, count)) { 391 kfree(pattern); 392 return NULL; 393 } 394 395 *size = count; 396 397 return pattern; 398 } 399 EXPORT_SYMBOL_GPL(led_get_default_pattern); 400 401 /* Caller must ensure led_cdev->led_access held */ 402 void led_sysfs_disable(struct led_classdev *led_cdev) 403 { 404 lockdep_assert_held(&led_cdev->led_access); 405 406 led_cdev->flags |= LED_SYSFS_DISABLE; 407 } 408 EXPORT_SYMBOL_GPL(led_sysfs_disable); 409 410 /* Caller must ensure led_cdev->led_access held */ 411 void led_sysfs_enable(struct led_classdev *led_cdev) 412 { 413 lockdep_assert_held(&led_cdev->led_access); 414 415 led_cdev->flags &= ~LED_SYSFS_DISABLE; 416 } 417 EXPORT_SYMBOL_GPL(led_sysfs_enable); 418 419 static void led_parse_fwnode_props(struct device *dev, 420 struct fwnode_handle *fwnode, 421 struct led_properties *props) 422 { 423 int ret; 424 425 if (!fwnode) 426 return; 427 428 if (fwnode_property_present(fwnode, "label")) { 429 ret = fwnode_property_read_string(fwnode, "label", &props->label); 430 if (ret) 431 dev_err(dev, "Error parsing 'label' property (%d)\n", ret); 432 return; 433 } 434 435 if (fwnode_property_present(fwnode, "color")) { 436 ret = fwnode_property_read_u32(fwnode, "color", &props->color); 437 if (ret) 438 dev_err(dev, "Error parsing 'color' property (%d)\n", ret); 439 else if (props->color >= LED_COLOR_ID_MAX) 440 dev_err(dev, "LED color identifier out of range\n"); 441 else 442 props->color_present = true; 443 } 444 445 446 if (!fwnode_property_present(fwnode, "function")) 447 return; 448 449 ret = fwnode_property_read_string(fwnode, "function", &props->function); 450 if (ret) { 451 dev_err(dev, 452 "Error parsing 'function' property (%d)\n", 453 ret); 454 } 455 456 if (!fwnode_property_present(fwnode, "function-enumerator")) 457 return; 458 459 ret = fwnode_property_read_u32(fwnode, "function-enumerator", 460 &props->func_enum); 461 if (ret) { 462 dev_err(dev, 463 "Error parsing 'function-enumerator' property (%d)\n", 464 ret); 465 } else { 466 props->func_enum_present = true; 467 } 468 } 469 470 int led_compose_name(struct device *dev, struct led_init_data *init_data, 471 char *led_classdev_name) 472 { 473 struct led_properties props = {}; 474 struct fwnode_handle *fwnode = init_data->fwnode; 475 const char *devicename = init_data->devicename; 476 477 if (!led_classdev_name) 478 return -EINVAL; 479 480 led_parse_fwnode_props(dev, fwnode, &props); 481 482 /* We want to label LEDs that can produce full range of colors 483 * as RGB, not multicolor */ 484 BUG_ON(props.color == LED_COLOR_ID_MULTI); 485 486 if (props.label) { 487 /* 488 * If init_data.devicename is NULL, then it indicates that 489 * DT label should be used as-is for LED class device name. 490 * Otherwise the label is prepended with devicename to compose 491 * the final LED class device name. 492 */ 493 if (!devicename) { 494 strscpy(led_classdev_name, props.label, 495 LED_MAX_NAME_SIZE); 496 } else { 497 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 498 devicename, props.label); 499 } 500 } else if (props.function || props.color_present) { 501 char tmp_buf[LED_MAX_NAME_SIZE]; 502 503 if (props.func_enum_present) { 504 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s-%d", 505 props.color_present ? led_colors[props.color] : "", 506 props.function ?: "", props.func_enum); 507 } else { 508 snprintf(tmp_buf, LED_MAX_NAME_SIZE, "%s:%s", 509 props.color_present ? led_colors[props.color] : "", 510 props.function ?: ""); 511 } 512 if (init_data->devname_mandatory) { 513 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 514 devicename, tmp_buf); 515 } else { 516 strscpy(led_classdev_name, tmp_buf, LED_MAX_NAME_SIZE); 517 518 } 519 } else if (init_data->default_label) { 520 if (!devicename) { 521 dev_err(dev, "Legacy LED naming requires devicename segment"); 522 return -EINVAL; 523 } 524 snprintf(led_classdev_name, LED_MAX_NAME_SIZE, "%s:%s", 525 devicename, init_data->default_label); 526 } else if (is_of_node(fwnode)) { 527 strscpy(led_classdev_name, to_of_node(fwnode)->name, 528 LED_MAX_NAME_SIZE); 529 } else 530 return -EINVAL; 531 532 return 0; 533 } 534 EXPORT_SYMBOL_GPL(led_compose_name); 535 536 enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode) 537 { 538 const char *state = NULL; 539 540 if (!fwnode_property_read_string(fwnode, "default-state", &state)) { 541 if (!strcmp(state, "keep")) 542 return LEDS_DEFSTATE_KEEP; 543 if (!strcmp(state, "on")) 544 return LEDS_DEFSTATE_ON; 545 } 546 547 return LEDS_DEFSTATE_OFF; 548 } 549 EXPORT_SYMBOL_GPL(led_init_default_state_get); 550