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