xref: /openbmc/linux/drivers/leds/led-class.c (revision 6d21fb7d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LED Class Core
4  *
5  * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
6  * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
7  */
8 
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>
22 #include <linux/of.h>
23 #include "leds.h"
24 
25 static DEFINE_MUTEX(leds_lookup_lock);
26 static LIST_HEAD(leds_lookup_list);
27 
28 static ssize_t brightness_show(struct device *dev,
29 		struct device_attribute *attr, char *buf)
30 {
31 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
32 
33 	/* no lock needed for this */
34 	led_update_brightness(led_cdev);
35 
36 	return sprintf(buf, "%u\n", led_cdev->brightness);
37 }
38 
39 static ssize_t brightness_store(struct device *dev,
40 		struct device_attribute *attr, const char *buf, size_t size)
41 {
42 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
43 	unsigned long state;
44 	ssize_t ret;
45 
46 	mutex_lock(&led_cdev->led_access);
47 
48 	if (led_sysfs_is_disabled(led_cdev)) {
49 		ret = -EBUSY;
50 		goto unlock;
51 	}
52 
53 	ret = kstrtoul(buf, 10, &state);
54 	if (ret)
55 		goto unlock;
56 
57 	if (state == LED_OFF)
58 		led_trigger_remove(led_cdev);
59 	led_set_brightness(led_cdev, state);
60 	flush_work(&led_cdev->set_brightness_work);
61 
62 	ret = size;
63 unlock:
64 	mutex_unlock(&led_cdev->led_access);
65 	return ret;
66 }
67 static DEVICE_ATTR_RW(brightness);
68 
69 static ssize_t max_brightness_show(struct device *dev,
70 		struct device_attribute *attr, char *buf)
71 {
72 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
73 
74 	return sprintf(buf, "%u\n", led_cdev->max_brightness);
75 }
76 static DEVICE_ATTR_RO(max_brightness);
77 
78 static ssize_t color_show(struct device *dev,
79 		struct device_attribute *attr, char *buf)
80 {
81 	const char *color_text = "invalid";
82 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
83 
84 	if (led_cdev->color < LED_COLOR_ID_MAX)
85 		color_text = led_colors[led_cdev->color];
86 
87 	return sysfs_emit(buf, "%s\n", color_text);
88 }
89 static DEVICE_ATTR_RO(color);
90 
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[] = {
94 	&bin_attr_trigger,
95 	NULL,
96 };
97 static const struct attribute_group led_trigger_group = {
98 	.bin_attrs = led_trigger_bin_attrs,
99 };
100 #endif
101 
102 static struct attribute *led_class_attrs[] = {
103 	&dev_attr_brightness.attr,
104 	&dev_attr_max_brightness.attr,
105 	&dev_attr_color.attr,
106 	NULL,
107 };
108 
109 static const struct attribute_group led_group = {
110 	.attrs = led_class_attrs,
111 };
112 
113 static const struct attribute_group *led_groups[] = {
114 	&led_group,
115 #ifdef CONFIG_LEDS_TRIGGERS
116 	&led_trigger_group,
117 #endif
118 	NULL,
119 };
120 
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)
124 {
125 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
126 
127 	if (led_cdev->brightness_hw_changed == -1)
128 		return -ENODATA;
129 
130 	return sprintf(buf, "%u\n", led_cdev->brightness_hw_changed);
131 }
132 
133 static DEVICE_ATTR_RO(brightness_hw_changed);
134 
135 static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
136 {
137 	struct device *dev = led_cdev->dev;
138 	int ret;
139 
140 	ret = device_create_file(dev, &dev_attr_brightness_hw_changed);
141 	if (ret) {
142 		dev_err(dev, "Error creating brightness_hw_changed\n");
143 		return ret;
144 	}
145 
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);
151 		return -ENXIO;
152 	}
153 
154 	return 0;
155 }
156 
157 static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
158 {
159 	sysfs_put(led_cdev->brightness_hw_changed_kn);
160 	device_remove_file(led_cdev->dev, &dev_attr_brightness_hw_changed);
161 }
162 
163 void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev, unsigned int brightness)
164 {
165 	if (WARN_ON(!led_cdev->brightness_hw_changed_kn))
166 		return;
167 
168 	led_cdev->brightness_hw_changed = brightness;
169 	sysfs_notify_dirent(led_cdev->brightness_hw_changed_kn);
170 }
171 EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed);
172 #else
173 static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
174 {
175 	return 0;
176 }
177 static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
178 {
179 }
180 #endif
181 
182 /**
183  * led_classdev_suspend - suspend an led_classdev.
184  * @led_cdev: the led_classdev to suspend.
185  */
186 void led_classdev_suspend(struct led_classdev *led_cdev)
187 {
188 	led_cdev->flags |= LED_SUSPENDED;
189 	led_set_brightness_nopm(led_cdev, 0);
190 	flush_work(&led_cdev->set_brightness_work);
191 }
192 EXPORT_SYMBOL_GPL(led_classdev_suspend);
193 
194 /**
195  * led_classdev_resume - resume an led_classdev.
196  * @led_cdev: the led_classdev to resume.
197  */
198 void led_classdev_resume(struct led_classdev *led_cdev)
199 {
200 	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
201 
202 	if (led_cdev->flash_resume)
203 		led_cdev->flash_resume(led_cdev);
204 
205 	led_cdev->flags &= ~LED_SUSPENDED;
206 }
207 EXPORT_SYMBOL_GPL(led_classdev_resume);
208 
209 #ifdef CONFIG_PM_SLEEP
210 static int led_suspend(struct device *dev)
211 {
212 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
213 
214 	if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
215 		led_classdev_suspend(led_cdev);
216 
217 	return 0;
218 }
219 
220 static int led_resume(struct device *dev)
221 {
222 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
223 
224 	if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
225 		led_classdev_resume(led_cdev);
226 
227 	return 0;
228 }
229 #endif
230 
231 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
232 
233 static struct led_classdev *led_module_get(struct device *led_dev)
234 {
235 	struct led_classdev *led_cdev;
236 
237 	if (!led_dev)
238 		return ERR_PTR(-EPROBE_DEFER);
239 
240 	led_cdev = dev_get_drvdata(led_dev);
241 
242 	if (!try_module_get(led_cdev->dev->parent->driver->owner)) {
243 		put_device(led_cdev->dev);
244 		return ERR_PTR(-ENODEV);
245 	}
246 
247 	return led_cdev;
248 }
249 
250 static const struct class leds_class = {
251 	.name = "leds",
252 	.dev_groups = led_groups,
253 	.pm = &leds_class_dev_pm_ops,
254 };
255 
256 /**
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
260  *
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.
263  */
264 struct led_classdev *of_led_get(struct device_node *np, int index)
265 {
266 	struct device *led_dev;
267 	struct device_node *led_node;
268 
269 	led_node = of_parse_phandle(np, "leds", index);
270 	if (!led_node)
271 		return ERR_PTR(-ENOENT);
272 
273 	led_dev = class_find_device_by_of_node(&leds_class, led_node);
274 	of_node_put(led_node);
275 	put_device(led_dev);
276 
277 	return led_module_get(led_dev);
278 }
279 EXPORT_SYMBOL_GPL(of_led_get);
280 
281 /**
282  * led_put() - release a LED device
283  * @led_cdev: LED device
284  */
285 void led_put(struct led_classdev *led_cdev)
286 {
287 	module_put(led_cdev->dev->parent->driver->owner);
288 	put_device(led_cdev->dev);
289 }
290 EXPORT_SYMBOL_GPL(led_put);
291 
292 static void devm_led_release(struct device *dev, void *res)
293 {
294 	struct led_classdev **p = res;
295 
296 	led_put(*p);
297 }
298 
299 static struct led_classdev *__devm_led_get(struct device *dev, struct led_classdev *led)
300 {
301 	struct led_classdev **dr;
302 
303 	dr = devres_alloc(devm_led_release, sizeof(struct led_classdev *), GFP_KERNEL);
304 	if (!dr) {
305 		led_put(led);
306 		return ERR_PTR(-ENOMEM);
307 	}
308 
309 	*dr = led;
310 	devres_add(dev, dr);
311 
312 	return led;
313 }
314 
315 /**
316  * devm_of_led_get - Resource-managed request of a LED device
317  * @dev:	LED consumer
318  * @index:	index of the LED to obtain in the consumer
319  *
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
322  * on driver detach.
323  *
324  * @return a pointer to a LED device or ERR_PTR(errno) on failure.
325  */
326 struct led_classdev *__must_check devm_of_led_get(struct device *dev,
327 						  int index)
328 {
329 	struct led_classdev *led;
330 
331 	if (!dev)
332 		return ERR_PTR(-EINVAL);
333 
334 	led = of_led_get(dev->of_node, index);
335 	if (IS_ERR(led))
336 		return led;
337 
338 	return __devm_led_get(dev, led);
339 }
340 EXPORT_SYMBOL_GPL(devm_of_led_get);
341 
342 /**
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
346  *
347  * @return a pointer to a LED device or ERR_PTR(errno) on failure.
348  */
349 struct led_classdev *led_get(struct device *dev, char *con_id)
350 {
351 	struct led_lookup_data *lookup;
352 	const char *provider = NULL;
353 	struct device *led_dev;
354 
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);
360 			break;
361 		}
362 	}
363 	mutex_unlock(&leds_lookup_lock);
364 
365 	if (!provider)
366 		return ERR_PTR(-ENOENT);
367 
368 	led_dev = class_find_device_by_name(&leds_class, provider);
369 	kfree_const(provider);
370 
371 	return led_module_get(led_dev);
372 }
373 EXPORT_SYMBOL_GPL(led_get);
374 
375 /**
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
379  *
380  * The LED device returned from this function is automatically released
381  * on driver detach.
382  *
383  * @return a pointer to a LED device or ERR_PTR(errno) on failure.
384  */
385 struct led_classdev *devm_led_get(struct device *dev, char *con_id)
386 {
387 	struct led_classdev *led;
388 
389 	led = led_get(dev, con_id);
390 	if (IS_ERR(led))
391 		return led;
392 
393 	return __devm_led_get(dev, led);
394 }
395 EXPORT_SYMBOL_GPL(devm_led_get);
396 
397 /**
398  * led_add_lookup() - Add a LED lookup table entry
399  * @led_lookup: the lookup table entry to add
400  *
401  * Add a LED lookup table entry. On systems without devicetree the lookup table
402  * is used by led_get() to find LEDs.
403  */
404 void led_add_lookup(struct led_lookup_data *led_lookup)
405 {
406 	mutex_lock(&leds_lookup_lock);
407 	list_add_tail(&led_lookup->list, &leds_lookup_list);
408 	mutex_unlock(&leds_lookup_lock);
409 }
410 EXPORT_SYMBOL_GPL(led_add_lookup);
411 
412 /**
413  * led_remove_lookup() - Remove a LED lookup table entry
414  * @led_lookup: the lookup table entry to remove
415  */
416 void led_remove_lookup(struct led_lookup_data *led_lookup)
417 {
418 	mutex_lock(&leds_lookup_lock);
419 	list_del(&led_lookup->list);
420 	mutex_unlock(&leds_lookup_lock);
421 }
422 EXPORT_SYMBOL_GPL(led_remove_lookup);
423 
424 /**
425  * devm_of_led_get_optional - Resource-managed request of an optional LED device
426  * @dev:	LED consumer
427  * @index:	index of the LED to obtain in the consumer
428  *
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
431  * on driver detach.
432  *
433  * @return a pointer to a LED device, ERR_PTR(errno) on failure and NULL if the
434  * led was not found.
435  */
436 struct led_classdev *__must_check devm_of_led_get_optional(struct device *dev,
437 							int index)
438 {
439 	struct led_classdev *led;
440 
441 	led = devm_of_led_get(dev, index);
442 	if (IS_ERR(led) && PTR_ERR(led) == -ENOENT)
443 		return NULL;
444 
445 	return led;
446 }
447 EXPORT_SYMBOL_GPL(devm_of_led_get_optional);
448 
449 static int led_classdev_next_name(const char *init_name, char *name,
450 				  size_t len)
451 {
452 	unsigned int i = 0;
453 	int ret = 0;
454 	struct device *dev;
455 
456 	strscpy(name, init_name, len);
457 
458 	while ((ret < len) &&
459 	       (dev = class_find_device_by_name(&leds_class, name))) {
460 		put_device(dev);
461 		ret = snprintf(name, len, "%s_%u", init_name, ++i);
462 	}
463 
464 	if (ret >= len)
465 		return -ENOMEM;
466 
467 	return i;
468 }
469 
470 /**
471  * led_classdev_register_ext - register a new object of led_classdev class
472  *			       with init data.
473  *
474  * @parent: parent of LED device
475  * @led_cdev: the led_classdev structure for this device.
476  * @init_data: LED class device initialization data
477  */
478 int led_classdev_register_ext(struct device *parent,
479 			      struct led_classdev *led_cdev,
480 			      struct led_init_data *init_data)
481 {
482 	char composed_name[LED_MAX_NAME_SIZE];
483 	char final_name[LED_MAX_NAME_SIZE];
484 	const char *proposed_name = composed_name;
485 	int ret;
486 
487 	if (init_data) {
488 		if (init_data->devname_mandatory && !init_data->devicename) {
489 			dev_err(parent, "Mandatory device name is missing");
490 			return -EINVAL;
491 		}
492 		ret = led_compose_name(parent, init_data, composed_name);
493 		if (ret < 0)
494 			return ret;
495 
496 		if (init_data->fwnode) {
497 			fwnode_property_read_string(init_data->fwnode,
498 				"linux,default-trigger",
499 				&led_cdev->default_trigger);
500 
501 			if (fwnode_property_present(init_data->fwnode,
502 						    "retain-state-shutdown"))
503 				led_cdev->flags |= LED_RETAIN_AT_SHUTDOWN;
504 
505 			fwnode_property_read_u32(init_data->fwnode,
506 				"max-brightness",
507 				&led_cdev->max_brightness);
508 
509 			if (fwnode_property_present(init_data->fwnode, "color"))
510 				fwnode_property_read_u32(init_data->fwnode, "color",
511 							 &led_cdev->color);
512 		}
513 	} else {
514 		proposed_name = led_cdev->name;
515 	}
516 
517 	ret = led_classdev_next_name(proposed_name, final_name, sizeof(final_name));
518 	if (ret < 0)
519 		return ret;
520 
521 	if (led_cdev->color >= LED_COLOR_ID_MAX)
522 		dev_warn(parent, "LED %s color identifier out of range\n", final_name);
523 
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);
531 	}
532 	if (init_data && init_data->fwnode)
533 		device_set_node(led_cdev->dev, init_data->fwnode);
534 
535 	if (ret)
536 		dev_warn(parent, "Led %s renamed to %s due to name collision",
537 				proposed_name, dev_name(led_cdev->dev));
538 
539 	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
540 		ret = led_add_brightness_hw_changed(led_cdev);
541 		if (ret) {
542 			device_unregister(led_cdev->dev);
543 			led_cdev->dev = NULL;
544 			mutex_unlock(&led_cdev->led_access);
545 			return ret;
546 		}
547 	}
548 
549 	led_cdev->work_flags = 0;
550 #ifdef CONFIG_LEDS_TRIGGERS
551 	init_rwsem(&led_cdev->trigger_lock);
552 #endif
553 #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
554 	led_cdev->brightness_hw_changed = -1;
555 #endif
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);
560 
561 	if (!led_cdev->max_brightness)
562 		led_cdev->max_brightness = LED_FULL;
563 
564 	led_update_brightness(led_cdev);
565 
566 	led_init_core(led_cdev);
567 
568 #ifdef CONFIG_LEDS_TRIGGERS
569 	led_trigger_set_default(led_cdev);
570 #endif
571 
572 	mutex_unlock(&led_cdev->led_access);
573 
574 	dev_dbg(parent, "Registered led device: %s\n",
575 			led_cdev->name);
576 
577 	return 0;
578 }
579 EXPORT_SYMBOL_GPL(led_classdev_register_ext);
580 
581 /**
582  * led_classdev_unregister - unregisters a object of led_properties class.
583  * @led_cdev: the led device to unregister
584  *
585  * Unregisters a previously registered via led_classdev_register object.
586  */
587 void led_classdev_unregister(struct led_classdev *led_cdev)
588 {
589 	if (IS_ERR_OR_NULL(led_cdev->dev))
590 		return;
591 
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);
597 #endif
598 
599 	led_cdev->flags |= LED_UNREGISTERING;
600 
601 	/* Stop blinking */
602 	led_stop_software_blink(led_cdev);
603 
604 	if (!(led_cdev->flags & LED_RETAIN_AT_SHUTDOWN))
605 		led_set_brightness(led_cdev, LED_OFF);
606 
607 	flush_work(&led_cdev->set_brightness_work);
608 
609 	if (led_cdev->flags & LED_BRIGHT_HW_CHANGED)
610 		led_remove_brightness_hw_changed(led_cdev);
611 
612 	device_unregister(led_cdev->dev);
613 
614 	down_write(&leds_list_lock);
615 	list_del(&led_cdev->node);
616 	up_write(&leds_list_lock);
617 
618 	mutex_destroy(&led_cdev->led_access);
619 }
620 EXPORT_SYMBOL_GPL(led_classdev_unregister);
621 
622 static void devm_led_classdev_release(struct device *dev, void *res)
623 {
624 	led_classdev_unregister(*(struct led_classdev **)res);
625 }
626 
627 /**
628  * devm_led_classdev_register_ext - resource managed led_classdev_register_ext()
629  *
630  * @parent: parent of LED device
631  * @led_cdev: the led_classdev structure for this device.
632  * @init_data: LED class device initialization data
633  */
634 int devm_led_classdev_register_ext(struct device *parent,
635 				   struct led_classdev *led_cdev,
636 				   struct led_init_data *init_data)
637 {
638 	struct led_classdev **dr;
639 	int rc;
640 
641 	dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL);
642 	if (!dr)
643 		return -ENOMEM;
644 
645 	rc = led_classdev_register_ext(parent, led_cdev, init_data);
646 	if (rc) {
647 		devres_free(dr);
648 		return rc;
649 	}
650 
651 	*dr = led_cdev;
652 	devres_add(parent, dr);
653 
654 	return 0;
655 }
656 EXPORT_SYMBOL_GPL(devm_led_classdev_register_ext);
657 
658 static int devm_led_classdev_match(struct device *dev, void *res, void *data)
659 {
660 	struct led_classdev **p = res;
661 
662 	if (WARN_ON(!p || !*p))
663 		return 0;
664 
665 	return *p == data;
666 }
667 
668 /**
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.
672  */
673 void devm_led_classdev_unregister(struct device *dev,
674 				  struct led_classdev *led_cdev)
675 {
676 	WARN_ON(devres_release(dev,
677 			       devm_led_classdev_release,
678 			       devm_led_classdev_match, led_cdev));
679 }
680 EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
681 
682 static int __init leds_init(void)
683 {
684 	return class_register(&leds_class);
685 }
686 
687 static void __exit leds_exit(void)
688 {
689 	class_unregister(&leds_class);
690 }
691 
692 subsys_initcall(leds_init);
693 module_exit(leds_exit);
694 
695 MODULE_AUTHOR("John Lenz, Richard Purdie");
696 MODULE_LICENSE("GPL");
697 MODULE_DESCRIPTION("LED Class Interface");
698