1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
6 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
9 #include <linux/ctype.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/leds.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/timer.h>
21 #include <uapi/linux/uleds.h>
25 static DEFINE_MUTEX(leds_lookup_lock
);
26 static LIST_HEAD(leds_lookup_list
);
28 static ssize_t
brightness_show(struct device
*dev
,
29 struct device_attribute
*attr
, char *buf
)
31 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
33 /* no lock needed for this */
34 led_update_brightness(led_cdev
);
36 return sprintf(buf
, "%u\n", led_cdev
->brightness
);
39 static ssize_t
brightness_store(struct device
*dev
,
40 struct device_attribute
*attr
, const char *buf
, size_t size
)
42 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
46 mutex_lock(&led_cdev
->led_access
);
48 if (led_sysfs_is_disabled(led_cdev
)) {
53 ret
= kstrtoul(buf
, 10, &state
);
58 led_trigger_remove(led_cdev
);
59 led_set_brightness(led_cdev
, state
);
60 flush_work(&led_cdev
->set_brightness_work
);
64 mutex_unlock(&led_cdev
->led_access
);
67 static DEVICE_ATTR_RW(brightness
);
69 static ssize_t
max_brightness_show(struct device
*dev
,
70 struct device_attribute
*attr
, char *buf
)
72 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
74 return sprintf(buf
, "%u\n", led_cdev
->max_brightness
);
76 static DEVICE_ATTR_RO(max_brightness
);
78 #ifdef CONFIG_LEDS_TRIGGERS
79 static BIN_ATTR(trigger
, 0644, led_trigger_read
, led_trigger_write
, 0);
80 static struct bin_attribute
*led_trigger_bin_attrs
[] = {
84 static const struct attribute_group led_trigger_group
= {
85 .bin_attrs
= led_trigger_bin_attrs
,
89 static struct attribute
*led_class_attrs
[] = {
90 &dev_attr_brightness
.attr
,
91 &dev_attr_max_brightness
.attr
,
95 static const struct attribute_group led_group
= {
96 .attrs
= led_class_attrs
,
99 static const struct attribute_group
*led_groups
[] = {
101 #ifdef CONFIG_LEDS_TRIGGERS
107 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
108 static ssize_t
brightness_hw_changed_show(struct device
*dev
,
109 struct device_attribute
*attr
, char *buf
)
111 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
113 if (led_cdev
->brightness_hw_changed
== -1)
116 return sprintf(buf
, "%u\n", led_cdev
->brightness_hw_changed
);
119 static DEVICE_ATTR_RO(brightness_hw_changed
);
121 static int led_add_brightness_hw_changed(struct led_classdev
*led_cdev
)
123 struct device
*dev
= led_cdev
->dev
;
126 ret
= device_create_file(dev
, &dev_attr_brightness_hw_changed
);
128 dev_err(dev
, "Error creating brightness_hw_changed\n");
132 led_cdev
->brightness_hw_changed_kn
=
133 sysfs_get_dirent(dev
->kobj
.sd
, "brightness_hw_changed");
134 if (!led_cdev
->brightness_hw_changed_kn
) {
135 dev_err(dev
, "Error getting brightness_hw_changed kn\n");
136 device_remove_file(dev
, &dev_attr_brightness_hw_changed
);
143 static void led_remove_brightness_hw_changed(struct led_classdev
*led_cdev
)
145 sysfs_put(led_cdev
->brightness_hw_changed_kn
);
146 device_remove_file(led_cdev
->dev
, &dev_attr_brightness_hw_changed
);
149 void led_classdev_notify_brightness_hw_changed(struct led_classdev
*led_cdev
, unsigned int brightness
)
151 if (WARN_ON(!led_cdev
->brightness_hw_changed_kn
))
154 led_cdev
->brightness_hw_changed
= brightness
;
155 sysfs_notify_dirent(led_cdev
->brightness_hw_changed_kn
);
157 EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed
);
159 static int led_add_brightness_hw_changed(struct led_classdev
*led_cdev
)
163 static void led_remove_brightness_hw_changed(struct led_classdev
*led_cdev
)
169 * led_classdev_suspend - suspend an led_classdev.
170 * @led_cdev: the led_classdev to suspend.
172 void led_classdev_suspend(struct led_classdev
*led_cdev
)
174 led_cdev
->flags
|= LED_SUSPENDED
;
175 led_set_brightness_nopm(led_cdev
, 0);
176 flush_work(&led_cdev
->set_brightness_work
);
178 EXPORT_SYMBOL_GPL(led_classdev_suspend
);
181 * led_classdev_resume - resume an led_classdev.
182 * @led_cdev: the led_classdev to resume.
184 void led_classdev_resume(struct led_classdev
*led_cdev
)
186 led_set_brightness_nopm(led_cdev
, led_cdev
->brightness
);
188 if (led_cdev
->flash_resume
)
189 led_cdev
->flash_resume(led_cdev
);
191 led_cdev
->flags
&= ~LED_SUSPENDED
;
193 EXPORT_SYMBOL_GPL(led_classdev_resume
);
195 #ifdef CONFIG_PM_SLEEP
196 static int led_suspend(struct device
*dev
)
198 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
200 if (led_cdev
->flags
& LED_CORE_SUSPENDRESUME
)
201 led_classdev_suspend(led_cdev
);
206 static int led_resume(struct device
*dev
)
208 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
210 if (led_cdev
->flags
& LED_CORE_SUSPENDRESUME
)
211 led_classdev_resume(led_cdev
);
217 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops
, led_suspend
, led_resume
);
219 static struct led_classdev
*led_module_get(struct device
*led_dev
)
221 struct led_classdev
*led_cdev
;
224 return ERR_PTR(-EPROBE_DEFER
);
226 led_cdev
= dev_get_drvdata(led_dev
);
228 if (!try_module_get(led_cdev
->dev
->parent
->driver
->owner
)) {
229 put_device(led_cdev
->dev
);
230 return ERR_PTR(-ENODEV
);
236 static const struct class leds_class
= {
238 .dev_groups
= led_groups
,
239 .pm
= &leds_class_dev_pm_ops
,
243 * of_led_get() - request a LED device via the LED framework
244 * @np: device node to get the LED device from
245 * @index: the index of the LED
247 * Returns the LED device parsed from the phandle specified in the "leds"
248 * property of a device tree node or a negative error-code on failure.
250 struct led_classdev
*of_led_get(struct device_node
*np
, int index
)
252 struct device
*led_dev
;
253 struct device_node
*led_node
;
255 led_node
= of_parse_phandle(np
, "leds", index
);
257 return ERR_PTR(-ENOENT
);
259 led_dev
= class_find_device_by_of_node(&leds_class
, led_node
);
260 of_node_put(led_node
);
262 return led_module_get(led_dev
);
264 EXPORT_SYMBOL_GPL(of_led_get
);
267 * led_put() - release a LED device
268 * @led_cdev: LED device
270 void led_put(struct led_classdev
*led_cdev
)
272 module_put(led_cdev
->dev
->parent
->driver
->owner
);
273 put_device(led_cdev
->dev
);
275 EXPORT_SYMBOL_GPL(led_put
);
277 static void devm_led_release(struct device
*dev
, void *res
)
279 struct led_classdev
**p
= res
;
284 static struct led_classdev
*__devm_led_get(struct device
*dev
, struct led_classdev
*led
)
286 struct led_classdev
**dr
;
288 dr
= devres_alloc(devm_led_release
, sizeof(struct led_classdev
*), GFP_KERNEL
);
291 return ERR_PTR(-ENOMEM
);
301 * devm_of_led_get - Resource-managed request of a LED device
303 * @index: index of the LED to obtain in the consumer
305 * The device node of the device is parse to find the request LED device.
306 * The LED device returned from this function is automatically released
309 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
311 struct led_classdev
*__must_check
devm_of_led_get(struct device
*dev
,
314 struct led_classdev
*led
;
317 return ERR_PTR(-EINVAL
);
319 led
= of_led_get(dev
->of_node
, index
);
323 return __devm_led_get(dev
, led
);
325 EXPORT_SYMBOL_GPL(devm_of_led_get
);
328 * led_get() - request a LED device via the LED framework
329 * @dev: device for which to get the LED device
330 * @con_id: name of the LED from the device's point of view
332 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
334 struct led_classdev
*led_get(struct device
*dev
, char *con_id
)
336 struct led_lookup_data
*lookup
;
337 const char *provider
= NULL
;
338 struct device
*led_dev
;
340 mutex_lock(&leds_lookup_lock
);
341 list_for_each_entry(lookup
, &leds_lookup_list
, list
) {
342 if (!strcmp(lookup
->dev_id
, dev_name(dev
)) &&
343 !strcmp(lookup
->con_id
, con_id
)) {
344 provider
= kstrdup_const(lookup
->provider
, GFP_KERNEL
);
348 mutex_unlock(&leds_lookup_lock
);
351 return ERR_PTR(-ENOENT
);
353 led_dev
= class_find_device_by_name(&leds_class
, provider
);
354 kfree_const(provider
);
356 return led_module_get(led_dev
);
358 EXPORT_SYMBOL_GPL(led_get
);
361 * devm_led_get() - request a LED device via the LED framework
362 * @dev: device for which to get the LED device
363 * @con_id: name of the LED from the device's point of view
365 * The LED device returned from this function is automatically released
368 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
370 struct led_classdev
*devm_led_get(struct device
*dev
, char *con_id
)
372 struct led_classdev
*led
;
374 led
= led_get(dev
, con_id
);
378 return __devm_led_get(dev
, led
);
380 EXPORT_SYMBOL_GPL(devm_led_get
);
383 * led_add_lookup() - Add a LED lookup table entry
384 * @led_lookup: the lookup table entry to add
386 * Add a LED lookup table entry. On systems without devicetree the lookup table
387 * is used by led_get() to find LEDs.
389 void led_add_lookup(struct led_lookup_data
*led_lookup
)
391 mutex_lock(&leds_lookup_lock
);
392 list_add_tail(&led_lookup
->list
, &leds_lookup_list
);
393 mutex_unlock(&leds_lookup_lock
);
395 EXPORT_SYMBOL_GPL(led_add_lookup
);
398 * led_remove_lookup() - Remove a LED lookup table entry
399 * @led_lookup: the lookup table entry to remove
401 void led_remove_lookup(struct led_lookup_data
*led_lookup
)
403 mutex_lock(&leds_lookup_lock
);
404 list_del(&led_lookup
->list
);
405 mutex_unlock(&leds_lookup_lock
);
407 EXPORT_SYMBOL_GPL(led_remove_lookup
);
410 * devm_of_led_get_optional - Resource-managed request of an optional LED device
412 * @index: index of the LED to obtain in the consumer
414 * The device node of the device is parsed to find the requested LED device.
415 * The LED device returned from this function is automatically released
418 * @return a pointer to a LED device, ERR_PTR(errno) on failure and NULL if the
421 struct led_classdev
*__must_check
devm_of_led_get_optional(struct device
*dev
,
424 struct led_classdev
*led
;
426 led
= devm_of_led_get(dev
, index
);
427 if (IS_ERR(led
) && PTR_ERR(led
) == -ENOENT
)
432 EXPORT_SYMBOL_GPL(devm_of_led_get_optional
);
434 static int led_classdev_next_name(const char *init_name
, char *name
,
441 strscpy(name
, init_name
, len
);
443 while ((ret
< len
) &&
444 (dev
= class_find_device_by_name(&leds_class
, name
))) {
446 ret
= snprintf(name
, len
, "%s_%u", init_name
, ++i
);
456 * led_classdev_register_ext - register a new object of led_classdev class
459 * @parent: parent of LED device
460 * @led_cdev: the led_classdev structure for this device.
461 * @init_data: LED class device initialization data
463 int led_classdev_register_ext(struct device
*parent
,
464 struct led_classdev
*led_cdev
,
465 struct led_init_data
*init_data
)
467 char composed_name
[LED_MAX_NAME_SIZE
];
468 char final_name
[LED_MAX_NAME_SIZE
];
469 const char *proposed_name
= composed_name
;
473 if (init_data
->devname_mandatory
&& !init_data
->devicename
) {
474 dev_err(parent
, "Mandatory device name is missing");
477 ret
= led_compose_name(parent
, init_data
, composed_name
);
481 if (init_data
->fwnode
) {
482 fwnode_property_read_string(init_data
->fwnode
,
483 "linux,default-trigger",
484 &led_cdev
->default_trigger
);
486 if (fwnode_property_present(init_data
->fwnode
,
487 "retain-state-shutdown"))
488 led_cdev
->flags
|= LED_RETAIN_AT_SHUTDOWN
;
490 fwnode_property_read_u32(init_data
->fwnode
,
492 &led_cdev
->max_brightness
);
494 if (fwnode_property_present(init_data
->fwnode
, "color"))
495 fwnode_property_read_u32(init_data
->fwnode
, "color",
499 proposed_name
= led_cdev
->name
;
502 ret
= led_classdev_next_name(proposed_name
, final_name
, sizeof(final_name
));
505 else if (ret
&& led_cdev
->flags
& LED_REJECT_NAME_CONFLICT
)
508 dev_warn(parent
, "Led %s renamed to %s due to name collision\n",
509 proposed_name
, final_name
);
511 if (led_cdev
->color
>= LED_COLOR_ID_MAX
)
512 dev_warn(parent
, "LED %s color identifier out of range\n", final_name
);
514 mutex_init(&led_cdev
->led_access
);
515 mutex_lock(&led_cdev
->led_access
);
516 led_cdev
->dev
= device_create_with_groups(&leds_class
, parent
, 0,
517 led_cdev
, led_cdev
->groups
, "%s", final_name
);
518 if (IS_ERR(led_cdev
->dev
)) {
519 mutex_unlock(&led_cdev
->led_access
);
520 return PTR_ERR(led_cdev
->dev
);
522 if (init_data
&& init_data
->fwnode
)
523 device_set_node(led_cdev
->dev
, init_data
->fwnode
);
525 if (led_cdev
->flags
& LED_BRIGHT_HW_CHANGED
) {
526 ret
= led_add_brightness_hw_changed(led_cdev
);
528 device_unregister(led_cdev
->dev
);
529 led_cdev
->dev
= NULL
;
530 mutex_unlock(&led_cdev
->led_access
);
535 led_cdev
->work_flags
= 0;
536 #ifdef CONFIG_LEDS_TRIGGERS
537 init_rwsem(&led_cdev
->trigger_lock
);
539 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
540 led_cdev
->brightness_hw_changed
= -1;
542 /* add to the list of leds */
543 down_write(&leds_list_lock
);
544 list_add_tail(&led_cdev
->node
, &leds_list
);
545 up_write(&leds_list_lock
);
547 if (!led_cdev
->max_brightness
)
548 led_cdev
->max_brightness
= LED_FULL
;
550 led_update_brightness(led_cdev
);
552 led_init_core(led_cdev
);
554 #ifdef CONFIG_LEDS_TRIGGERS
555 led_trigger_set_default(led_cdev
);
558 mutex_unlock(&led_cdev
->led_access
);
560 dev_dbg(parent
, "Registered led device: %s\n",
565 EXPORT_SYMBOL_GPL(led_classdev_register_ext
);
568 * led_classdev_unregister - unregisters a object of led_properties class.
569 * @led_cdev: the led device to unregister
571 * Unregisters a previously registered via led_classdev_register object.
573 void led_classdev_unregister(struct led_classdev
*led_cdev
)
575 if (IS_ERR_OR_NULL(led_cdev
->dev
))
578 #ifdef CONFIG_LEDS_TRIGGERS
579 down_write(&led_cdev
->trigger_lock
);
580 if (led_cdev
->trigger
)
581 led_trigger_set(led_cdev
, NULL
);
582 up_write(&led_cdev
->trigger_lock
);
585 led_cdev
->flags
|= LED_UNREGISTERING
;
588 led_stop_software_blink(led_cdev
);
590 if (!(led_cdev
->flags
& LED_RETAIN_AT_SHUTDOWN
))
591 led_set_brightness(led_cdev
, LED_OFF
);
593 flush_work(&led_cdev
->set_brightness_work
);
595 if (led_cdev
->flags
& LED_BRIGHT_HW_CHANGED
)
596 led_remove_brightness_hw_changed(led_cdev
);
598 device_unregister(led_cdev
->dev
);
600 down_write(&leds_list_lock
);
601 list_del(&led_cdev
->node
);
602 up_write(&leds_list_lock
);
604 mutex_destroy(&led_cdev
->led_access
);
606 EXPORT_SYMBOL_GPL(led_classdev_unregister
);
608 static void devm_led_classdev_release(struct device
*dev
, void *res
)
610 led_classdev_unregister(*(struct led_classdev
**)res
);
614 * devm_led_classdev_register_ext - resource managed led_classdev_register_ext()
616 * @parent: parent of LED device
617 * @led_cdev: the led_classdev structure for this device.
618 * @init_data: LED class device initialization data
620 int devm_led_classdev_register_ext(struct device
*parent
,
621 struct led_classdev
*led_cdev
,
622 struct led_init_data
*init_data
)
624 struct led_classdev
**dr
;
627 dr
= devres_alloc(devm_led_classdev_release
, sizeof(*dr
), GFP_KERNEL
);
631 rc
= led_classdev_register_ext(parent
, led_cdev
, init_data
);
638 devres_add(parent
, dr
);
642 EXPORT_SYMBOL_GPL(devm_led_classdev_register_ext
);
644 static int devm_led_classdev_match(struct device
*dev
, void *res
, void *data
)
646 struct led_classdev
**p
= res
;
648 if (WARN_ON(!p
|| !*p
))
655 * devm_led_classdev_unregister() - resource managed led_classdev_unregister()
656 * @dev: The device to unregister.
657 * @led_cdev: the led_classdev structure for this device.
659 void devm_led_classdev_unregister(struct device
*dev
,
660 struct led_classdev
*led_cdev
)
662 WARN_ON(devres_release(dev
,
663 devm_led_classdev_release
,
664 devm_led_classdev_match
, led_cdev
));
666 EXPORT_SYMBOL_GPL(devm_led_classdev_unregister
);
668 static int __init
leds_init(void)
670 return class_register(&leds_class
);
673 static void __exit
leds_exit(void)
675 class_unregister(&leds_class
);
678 subsys_initcall(leds_init
);
679 module_exit(leds_exit
);
681 MODULE_AUTHOR("John Lenz, Richard Purdie");
682 MODULE_LICENSE("GPL");
683 MODULE_DESCRIPTION("LED Class Interface");