1 // SPDX-License-Identifier: GPL-2.0-only
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 static ssize_t color_show(struct device *dev,
79 struct device_attribute *attr, char *buf)
81 const char *color_text = "invalid";
82 struct led_classdev *led_cdev = dev_get_drvdata(dev);
84 if (led_cdev->color < LED_COLOR_ID_MAX)
85 color_text = led_colors[led_cdev->color];
87 return sysfs_emit(buf, "%s\n", color_text);
89 static DEVICE_ATTR_RO(color);
91 #ifdef CONFIG_LEDS_TRIGGERS
92 static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
93 static struct bin_attribute *led_trigger_bin_attrs[] = {
97 static const struct attribute_group led_trigger_group = {
98 .bin_attrs = led_trigger_bin_attrs,
102 static struct attribute *led_class_attrs[] = {
103 &dev_attr_brightness.attr,
104 &dev_attr_max_brightness.attr,
105 &dev_attr_color.attr,
109 static const struct attribute_group led_group = {
110 .attrs = led_class_attrs,
113 static const struct attribute_group *led_groups[] = {
115 #ifdef CONFIG_LEDS_TRIGGERS
121 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
122 static ssize_t brightness_hw_changed_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 struct led_classdev *led_cdev = dev_get_drvdata(dev);
127 if (led_cdev->brightness_hw_changed == -1)
130 return sprintf(buf, "%u\n", led_cdev->brightness_hw_changed);
133 static DEVICE_ATTR_RO(brightness_hw_changed);
135 static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
137 struct device *dev = led_cdev->dev;
140 ret = device_create_file(dev, &dev_attr_brightness_hw_changed);
142 dev_err(dev, "Error creating brightness_hw_changed\n");
146 led_cdev->brightness_hw_changed_kn =
147 sysfs_get_dirent(dev->kobj.sd, "brightness_hw_changed");
148 if (!led_cdev->brightness_hw_changed_kn) {
149 dev_err(dev, "Error getting brightness_hw_changed kn\n");
150 device_remove_file(dev, &dev_attr_brightness_hw_changed);
157 static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
159 sysfs_put(led_cdev->brightness_hw_changed_kn);
160 device_remove_file(led_cdev->dev, &dev_attr_brightness_hw_changed);
163 void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev, unsigned int brightness)
165 if (WARN_ON(!led_cdev->brightness_hw_changed_kn))
168 led_cdev->brightness_hw_changed = brightness;
169 sysfs_notify_dirent(led_cdev->brightness_hw_changed_kn);
171 EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed);
173 static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
177 static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
183 * led_classdev_suspend - suspend an led_classdev.
184 * @led_cdev: the led_classdev to suspend.
186 void led_classdev_suspend(struct led_classdev *led_cdev)
188 led_cdev->flags |= LED_SUSPENDED;
189 led_set_brightness_nopm(led_cdev, 0);
190 flush_work(&led_cdev->set_brightness_work);
192 EXPORT_SYMBOL_GPL(led_classdev_suspend);
195 * led_classdev_resume - resume an led_classdev.
196 * @led_cdev: the led_classdev to resume.
198 void led_classdev_resume(struct led_classdev *led_cdev)
200 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
202 if (led_cdev->flash_resume)
203 led_cdev->flash_resume(led_cdev);
205 led_cdev->flags &= ~LED_SUSPENDED;
207 EXPORT_SYMBOL_GPL(led_classdev_resume);
209 #ifdef CONFIG_PM_SLEEP
210 static int led_suspend(struct device *dev)
212 struct led_classdev *led_cdev = dev_get_drvdata(dev);
214 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
215 led_classdev_suspend(led_cdev);
220 static int led_resume(struct device *dev)
222 struct led_classdev *led_cdev = dev_get_drvdata(dev);
224 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
225 led_classdev_resume(led_cdev);
231 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
233 static struct led_classdev *led_module_get(struct device *led_dev)
235 struct led_classdev *led_cdev;
238 return ERR_PTR(-EPROBE_DEFER);
240 led_cdev = dev_get_drvdata(led_dev);
242 if (!try_module_get(led_cdev->dev->parent->driver->owner)) {
243 put_device(led_cdev->dev);
244 return ERR_PTR(-ENODEV);
250 static const struct class leds_class = {
252 .dev_groups = led_groups,
253 .pm = &leds_class_dev_pm_ops,
257 * of_led_get() - request a LED device via the LED framework
258 * @np: device node to get the LED device from
259 * @index: the index of the LED
261 * Returns the LED device parsed from the phandle specified in the "leds"
262 * property of a device tree node or a negative error-code on failure.
264 struct led_classdev *of_led_get(struct device_node *np, int index)
266 struct device *led_dev;
267 struct device_node *led_node;
269 led_node = of_parse_phandle(np, "leds", index);
271 return ERR_PTR(-ENOENT);
273 led_dev = class_find_device_by_of_node(&leds_class, led_node);
274 of_node_put(led_node);
277 return led_module_get(led_dev);
279 EXPORT_SYMBOL_GPL(of_led_get);
282 * led_put() - release a LED device
283 * @led_cdev: LED device
285 void led_put(struct led_classdev *led_cdev)
287 module_put(led_cdev->dev->parent->driver->owner);
288 put_device(led_cdev->dev);
290 EXPORT_SYMBOL_GPL(led_put);
292 static void devm_led_release(struct device *dev, void *res)
294 struct led_classdev **p = res;
299 static struct led_classdev *__devm_led_get(struct device *dev, struct led_classdev *led)
301 struct led_classdev **dr;
303 dr = devres_alloc(devm_led_release, sizeof(struct led_classdev *), GFP_KERNEL);
306 return ERR_PTR(-ENOMEM);
316 * devm_of_led_get - Resource-managed request of a LED device
318 * @index: index of the LED to obtain in the consumer
320 * The device node of the device is parse to find the request LED device.
321 * The LED device returned from this function is automatically released
324 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
326 struct led_classdev *__must_check devm_of_led_get(struct device *dev,
329 struct led_classdev *led;
332 return ERR_PTR(-EINVAL);
334 led = of_led_get(dev->of_node, index);
338 return __devm_led_get(dev, led);
340 EXPORT_SYMBOL_GPL(devm_of_led_get);
343 * led_get() - request a LED device via the LED framework
344 * @dev: device for which to get the LED device
345 * @con_id: name of the LED from the device's point of view
347 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
349 struct led_classdev *led_get(struct device *dev, char *con_id)
351 struct led_lookup_data *lookup;
352 const char *provider = NULL;
353 struct device *led_dev;
355 mutex_lock(&leds_lookup_lock);
356 list_for_each_entry(lookup, &leds_lookup_list, list) {
357 if (!strcmp(lookup->dev_id, dev_name(dev)) &&
358 !strcmp(lookup->con_id, con_id)) {
359 provider = kstrdup_const(lookup->provider, GFP_KERNEL);
363 mutex_unlock(&leds_lookup_lock);
366 return ERR_PTR(-ENOENT);
368 led_dev = class_find_device_by_name(&leds_class, provider);
369 kfree_const(provider);
371 return led_module_get(led_dev);
373 EXPORT_SYMBOL_GPL(led_get);
376 * devm_led_get() - request a LED device via the LED framework
377 * @dev: device for which to get the LED device
378 * @con_id: name of the LED from the device's point of view
380 * The LED device returned from this function is automatically released
383 * @return a pointer to a LED device or ERR_PTR(errno) on failure.
385 struct led_classdev *devm_led_get(struct device *dev, char *con_id)
387 struct led_classdev *led;
389 led = led_get(dev, con_id);
393 return __devm_led_get(dev, led);
395 EXPORT_SYMBOL_GPL(devm_led_get);
398 * led_add_lookup() - Add a LED lookup table entry
399 * @led_lookup: the lookup table entry to add
401 * Add a LED lookup table entry. On systems without devicetree the lookup table
402 * is used by led_get() to find LEDs.
404 void led_add_lookup(struct led_lookup_data *led_lookup)
406 mutex_lock(&leds_lookup_lock);
407 list_add_tail(&led_lookup->list, &leds_lookup_list);
408 mutex_unlock(&leds_lookup_lock);
410 EXPORT_SYMBOL_GPL(led_add_lookup);
413 * led_remove_lookup() - Remove a LED lookup table entry
414 * @led_lookup: the lookup table entry to remove
416 void led_remove_lookup(struct led_lookup_data *led_lookup)
418 mutex_lock(&leds_lookup_lock);
419 list_del(&led_lookup->list);
420 mutex_unlock(&leds_lookup_lock);
422 EXPORT_SYMBOL_GPL(led_remove_lookup);
425 * devm_of_led_get_optional - Resource-managed request of an optional LED device
427 * @index: index of the LED to obtain in the consumer
429 * The device node of the device is parsed to find the requested LED device.
430 * The LED device returned from this function is automatically released
433 * @return a pointer to a LED device, ERR_PTR(errno) on failure and NULL if the
436 struct led_classdev *__must_check devm_of_led_get_optional(struct device *dev,
439 struct led_classdev *led;
441 led = devm_of_led_get(dev, index);
442 if (IS_ERR(led) && PTR_ERR(led) == -ENOENT)
447 EXPORT_SYMBOL_GPL(devm_of_led_get_optional);
449 static int led_classdev_next_name(const char *init_name, char *name,
456 strscpy(name, init_name, len);
458 while ((ret < len) &&
459 (dev = class_find_device_by_name(&leds_class, name))) {
461 ret = snprintf(name, len, "%s_%u", init_name, ++i);
471 * led_classdev_register_ext - register a new object of led_classdev class
474 * @parent: parent of LED device
475 * @led_cdev: the led_classdev structure for this device.
476 * @init_data: LED class device initialization data
478 int led_classdev_register_ext(struct device *parent,
479 struct led_classdev *led_cdev,
480 struct led_init_data *init_data)
482 char composed_name[LED_MAX_NAME_SIZE];
483 char final_name[LED_MAX_NAME_SIZE];
484 const char *proposed_name = composed_name;
488 if (init_data->devname_mandatory && !init_data->devicename) {
489 dev_err(parent, "Mandatory device name is missing");
492 ret = led_compose_name(parent, init_data, composed_name);
496 if (init_data->fwnode) {
497 fwnode_property_read_string(init_data->fwnode,
498 "linux,default-trigger",
499 &led_cdev->default_trigger);
501 if (fwnode_property_present(init_data->fwnode,
502 "retain-state-shutdown"))
503 led_cdev->flags |= LED_RETAIN_AT_SHUTDOWN;
505 fwnode_property_read_u32(init_data->fwnode,
507 &led_cdev->max_brightness);
509 if (fwnode_property_present(init_data->fwnode, "color"))
510 fwnode_property_read_u32(init_data->fwnode, "color",
514 proposed_name = led_cdev->name;
517 ret = led_classdev_next_name(proposed_name, final_name, sizeof(final_name));
521 if (led_cdev->color >= LED_COLOR_ID_MAX)
522 dev_warn(parent, "LED %s color identifier out of range\n", final_name);
524 mutex_init(&led_cdev->led_access);
525 mutex_lock(&led_cdev->led_access);
526 led_cdev->dev = device_create_with_groups(&leds_class, parent, 0,
527 led_cdev, led_cdev->groups, "%s", final_name);
528 if (IS_ERR(led_cdev->dev)) {
529 mutex_unlock(&led_cdev->led_access);
530 return PTR_ERR(led_cdev->dev);
532 if (init_data && init_data->fwnode)
533 device_set_node(led_cdev->dev, init_data->fwnode);
536 dev_warn(parent, "Led %s renamed to %s due to name collision",
537 proposed_name, dev_name(led_cdev->dev));
539 if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
540 ret = led_add_brightness_hw_changed(led_cdev);
542 device_unregister(led_cdev->dev);
543 led_cdev->dev = NULL;
544 mutex_unlock(&led_cdev->led_access);
549 led_cdev->work_flags = 0;
550 #ifdef CONFIG_LEDS_TRIGGERS
551 init_rwsem(&led_cdev->trigger_lock);
553 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
554 led_cdev->brightness_hw_changed = -1;
556 /* add to the list of leds */
557 down_write(&leds_list_lock);
558 list_add_tail(&led_cdev->node, &leds_list);
559 up_write(&leds_list_lock);
561 if (!led_cdev->max_brightness)
562 led_cdev->max_brightness = LED_FULL;
564 led_update_brightness(led_cdev);
566 led_init_core(led_cdev);
568 #ifdef CONFIG_LEDS_TRIGGERS
569 led_trigger_set_default(led_cdev);
572 mutex_unlock(&led_cdev->led_access);
574 dev_dbg(parent, "Registered led device: %s\n",
579 EXPORT_SYMBOL_GPL(led_classdev_register_ext);
582 * led_classdev_unregister - unregisters a object of led_properties class.
583 * @led_cdev: the led device to unregister
585 * Unregisters a previously registered via led_classdev_register object.
587 void led_classdev_unregister(struct led_classdev *led_cdev)
589 if (IS_ERR_OR_NULL(led_cdev->dev))
592 #ifdef CONFIG_LEDS_TRIGGERS
593 down_write(&led_cdev->trigger_lock);
594 if (led_cdev->trigger)
595 led_trigger_set(led_cdev, NULL);
596 up_write(&led_cdev->trigger_lock);
599 led_cdev->flags |= LED_UNREGISTERING;
602 led_stop_software_blink(led_cdev);
604 if (!(led_cdev->flags & LED_RETAIN_AT_SHUTDOWN))
605 led_set_brightness(led_cdev, LED_OFF);
607 flush_work(&led_cdev->set_brightness_work);
609 if (led_cdev->flags & LED_BRIGHT_HW_CHANGED)
610 led_remove_brightness_hw_changed(led_cdev);
612 device_unregister(led_cdev->dev);
614 down_write(&leds_list_lock);
615 list_del(&led_cdev->node);
616 up_write(&leds_list_lock);
618 mutex_destroy(&led_cdev->led_access);
620 EXPORT_SYMBOL_GPL(led_classdev_unregister);
622 static void devm_led_classdev_release(struct device *dev, void *res)
624 led_classdev_unregister(*(struct led_classdev **)res);
628 * devm_led_classdev_register_ext - resource managed led_classdev_register_ext()
630 * @parent: parent of LED device
631 * @led_cdev: the led_classdev structure for this device.
632 * @init_data: LED class device initialization data
634 int devm_led_classdev_register_ext(struct device *parent,
635 struct led_classdev *led_cdev,
636 struct led_init_data *init_data)
638 struct led_classdev **dr;
641 dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL);
645 rc = led_classdev_register_ext(parent, led_cdev, init_data);
652 devres_add(parent, dr);
656 EXPORT_SYMBOL_GPL(devm_led_classdev_register_ext);
658 static int devm_led_classdev_match(struct device *dev, void *res, void *data)
660 struct led_classdev **p = res;
662 if (WARN_ON(!p || !*p))
669 * devm_led_classdev_unregister() - resource managed led_classdev_unregister()
670 * @dev: The device to unregister.
671 * @led_cdev: the led_classdev structure for this device.
673 void devm_led_classdev_unregister(struct device *dev,
674 struct led_classdev *led_cdev)
676 WARN_ON(devres_release(dev,
677 devm_led_classdev_release,
678 devm_led_classdev_match, led_cdev));
680 EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
682 static int __init leds_init(void)
684 return class_register(&leds_class);
687 static void __exit leds_exit(void)
689 class_unregister(&leds_class);
692 subsys_initcall(leds_init);
693 module_exit(leds_exit);
695 MODULE_AUTHOR("John Lenz, Richard Purdie");
696 MODULE_LICENSE("GPL");
697 MODULE_DESCRIPTION("LED Class Interface");