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>
11 #include <linux/platform_device.h>
12 #include <linux/stat.h>
13 #include <linux/slab.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
16 #include <linux/leds.h>
19 * struct syscon_led - state container for syscon based LEDs
20 * @cdev: LED class device for this LED
21 * @map: regmap to access the syscon device backing this LED
22 * @offset: the offset into the syscon regmap for the LED register
23 * @mask: the bit in the register corresponding to the LED
24 * @state: current state of the LED
27 struct led_classdev cdev
;
34 static void syscon_led_set(struct led_classdev
*led_cdev
,
35 enum led_brightness value
)
37 struct syscon_led
*sled
=
38 container_of(led_cdev
, struct syscon_led
, cdev
);
42 if (value
== LED_OFF
) {
50 ret
= regmap_update_bits(sled
->map
, sled
->offset
, sled
->mask
, val
);
52 dev_err(sled
->cdev
.dev
, "error updating LED status\n");
55 static int syscon_led_probe(struct platform_device
*pdev
)
57 struct led_init_data init_data
= {};
58 struct device
*dev
= &pdev
->dev
;
59 struct device_node
*np
= dev_of_node(dev
);
60 struct device
*parent
;
62 struct syscon_led
*sled
;
63 enum led_default_state state
;
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
, "reg", &sled
->offset
) &&
85 of_property_read_u32(np
, "offset", &sled
->offset
))
87 if (of_property_read_u32(np
, "mask", &sled
->mask
))
90 init_data
.fwnode
= of_fwnode_handle(np
);
92 state
= led_init_default_state_get(init_data
.fwnode
);
94 case LEDS_DEFSTATE_ON
:
95 ret
= regmap_update_bits(map
, sled
->offset
, sled
->mask
, sled
->mask
);
100 case LEDS_DEFSTATE_KEEP
:
101 ret
= regmap_read(map
, sled
->offset
, &value
);
104 sled
->state
= !!(value
& sled
->mask
);
107 ret
= regmap_update_bits(map
, sled
->offset
, sled
->mask
, 0);
112 sled
->cdev
.brightness_set
= syscon_led_set
;
114 ret
= devm_led_classdev_register_ext(dev
, &sled
->cdev
, &init_data
);
118 platform_set_drvdata(pdev
, sled
);
119 dev_info(dev
, "registered LED %s\n", sled
->cdev
.name
);
124 static const struct of_device_id of_syscon_leds_match
[] = {
125 { .compatible
= "register-bit-led", },
129 static struct platform_driver syscon_led_driver
= {
130 .probe
= syscon_led_probe
,
132 .name
= "leds-syscon",
133 .of_match_table
= of_syscon_leds_match
,
134 .suppress_bind_attrs
= true,
137 builtin_platform_driver(syscon_led_driver
);