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