1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic Syscon LEDs Driver
5 * Copyright (c) 2014, Linaro Limited
6 * Author: Linus Walleij <linus.walleij@linaro.org>
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 led_init_data init_data = {};
59 struct device *dev = &pdev->dev;
60 struct device_node *np = dev_of_node(dev);
61 struct device *parent;
63 struct syscon_led *sled;
69 dev_err(dev, "no parent for syscon LED\n");
72 map = syscon_node_to_regmap(dev_of_node(parent));
74 dev_err(dev, "no regmap for syscon LED parent\n");
78 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
84 if (of_property_read_u32(np, "offset", &sled->offset))
86 if (of_property_read_u32(np, "mask", &sled->mask))
89 state = of_get_property(np, "default-state", NULL);
91 if (!strcmp(state, "keep")) {
94 ret = regmap_read(map, sled->offset, &val);
97 sled->state = !!(val & sled->mask);
98 } else if (!strcmp(state, "on")) {
100 ret = regmap_update_bits(map, sled->offset,
107 ret = regmap_update_bits(map, sled->offset,
113 sled->cdev.brightness_set = syscon_led_set;
115 init_data.fwnode = of_fwnode_handle(np);
117 ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data);
121 platform_set_drvdata(pdev, sled);
122 dev_info(dev, "registered LED %s\n", sled->cdev.name);
127 static const struct of_device_id of_syscon_leds_match[] = {
128 { .compatible = "register-bit-led", },
132 static struct platform_driver syscon_led_driver = {
133 .probe = syscon_led_probe,
135 .name = "leds-syscon",
136 .of_match_table = of_syscon_leds_match,
137 .suppress_bind_attrs = true,
140 builtin_platform_driver(syscon_led_driver);