1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic Syscon LEDs Driver
5 * Copyright (c) 2014, Linaro Limited
9 #include <linux/init.h>
10 #include <linux/of_device.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13 #include <linux/stat.h>
14 #include <linux/slab.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/regmap.h>
17 #include <linux/leds.h>
20 * struct syscon_led - state container for syscon based LEDs
21 * @cdev: LED class device for this LED
22 * @map: regmap to access the syscon device backing this LED
23 * @offset: the offset into the syscon regmap for the LED register
24 * @mask: the bit in the register corresponding to the LED
25 * @state: current state of the LED
28 struct led_classdev cdev;
35 static void syscon_led_set(struct led_classdev *led_cdev,
36 enum led_brightness value)
38 struct syscon_led *sled =
39 container_of(led_cdev, struct syscon_led, cdev);
43 if (value == LED_OFF) {
51 ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
53 dev_err(sled->cdev.dev, "error updating LED status\n");
56 static int syscon_led_probe(struct platform_device *pdev)
58 struct device *dev = &pdev->dev;
59 struct device_node *np = dev->of_node;
60 struct device *parent;
62 struct syscon_led *sled;
68 dev_err(dev, "no parent for syscon LED\n");
71 map = syscon_node_to_regmap(parent->of_node);
73 dev_err(dev, "no regmap for syscon LED parent\n");
77 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
83 if (of_property_read_u32(np, "offset", &sled->offset))
85 if (of_property_read_u32(np, "mask", &sled->mask))
88 of_get_property(np, "label", NULL) ? : np->name;
89 sled->cdev.default_trigger =
90 of_get_property(np, "linux,default-trigger", NULL);
92 state = of_get_property(np, "default-state", NULL);
94 if (!strcmp(state, "keep")) {
97 ret = regmap_read(map, sled->offset, &val);
100 sled->state = !!(val & sled->mask);
101 } else if (!strcmp(state, "on")) {
103 ret = regmap_update_bits(map, sled->offset,
110 ret = regmap_update_bits(map, sled->offset,
116 sled->cdev.brightness_set = syscon_led_set;
118 ret = led_classdev_register(dev, &sled->cdev);
122 platform_set_drvdata(pdev, sled);
123 dev_info(dev, "registered LED %s\n", sled->cdev.name);
128 static const struct of_device_id of_syscon_leds_match[] = {
129 { .compatible = "register-bit-led", },
133 static struct platform_driver syscon_led_driver = {
134 .probe = syscon_led_probe,
136 .name = "leds-syscon",
137 .of_match_table = of_syscon_leds_match,
138 .suppress_bind_attrs = true,
141 builtin_platform_driver(syscon_led_driver);