Linux 6.10-rc4
[linux-2.6-block.git] / drivers / leds / leds-syscon.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
535f09cc
LW
2/*
3 * Generic Syscon LEDs Driver
4 *
5 * Copyright (c) 2014, Linaro Limited
6 * Author: Linus Walleij <linus.walleij@linaro.org>
535f09cc
LW
7 */
8#include <linux/io.h>
f7d98a65 9#include <linux/init.h>
3192f141 10#include <linux/of.h>
535f09cc
LW
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>
17
18/**
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
25 */
26struct syscon_led {
27 struct led_classdev cdev;
28 struct regmap *map;
29 u32 offset;
30 u32 mask;
31 bool state;
32};
33
34static void syscon_led_set(struct led_classdev *led_cdev,
35 enum led_brightness value)
36{
37 struct syscon_led *sled =
38 container_of(led_cdev, struct syscon_led, cdev);
39 u32 val;
40 int ret;
41
42 if (value == LED_OFF) {
43 val = 0;
44 sled->state = false;
45 } else {
46 val = sled->mask;
47 sled->state = true;
48 }
49
50 ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
51 if (ret < 0)
52 dev_err(sled->cdev.dev, "error updating LED status\n");
53}
54
a917d4b4 55static int syscon_led_probe(struct platform_device *pdev)
535f09cc 56{
00663196 57 struct led_init_data init_data = {};
a917d4b4 58 struct device *dev = &pdev->dev;
8853c95e 59 struct device_node *np = dev_of_node(dev);
a917d4b4
LW
60 struct device *parent;
61 struct regmap *map;
62 struct syscon_led *sled;
265d313e
AS
63 enum led_default_state state;
64 u32 value;
535f09cc
LW
65 int ret;
66
a917d4b4
LW
67 parent = dev->parent;
68 if (!parent) {
69 dev_err(dev, "no parent for syscon LED\n");
70 return -ENODEV;
71 }
8853c95e 72 map = syscon_node_to_regmap(dev_of_node(parent));
991a3f61 73 if (IS_ERR(map)) {
a917d4b4 74 dev_err(dev, "no regmap for syscon LED parent\n");
991a3f61 75 return PTR_ERR(map);
a917d4b4
LW
76 }
77
78 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
79 if (!sled)
80 return -ENOMEM;
81
82 sled->map = map;
83
a82cc9b8
RH
84 if (of_property_read_u32(np, "reg", &sled->offset) &&
85 of_property_read_u32(np, "offset", &sled->offset))
a917d4b4
LW
86 return -EINVAL;
87 if (of_property_read_u32(np, "mask", &sled->mask))
88 return -EINVAL;
a917d4b4 89
265d313e
AS
90 init_data.fwnode = of_fwnode_handle(np);
91
92 state = led_init_default_state_get(init_data.fwnode);
93 switch (state) {
94 case LEDS_DEFSTATE_ON:
95 ret = regmap_update_bits(map, sled->offset, sled->mask, sled->mask);
96 if (ret < 0)
97 return ret;
98 sled->state = true;
99 break;
100 case LEDS_DEFSTATE_KEEP:
101 ret = regmap_read(map, sled->offset, &value);
102 if (ret < 0)
103 return ret;
104 sled->state = !!(value & sled->mask);
105 break;
106 default:
107 ret = regmap_update_bits(map, sled->offset, sled->mask, 0);
108 if (ret < 0)
109 return ret;
110 sled->state = false;
a917d4b4
LW
111 }
112 sled->cdev.brightness_set = syscon_led_set;
535f09cc 113
00663196 114 ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data);
a917d4b4
LW
115 if (ret < 0)
116 return ret;
117
118 platform_set_drvdata(pdev, sled);
119 dev_info(dev, "registered LED %s\n", sled->cdev.name);
535f09cc 120
3f6e42c8
LW
121 return 0;
122}
123
a917d4b4
LW
124static const struct of_device_id of_syscon_leds_match[] = {
125 { .compatible = "register-bit-led", },
126 {},
127};
3f6e42c8 128
a917d4b4
LW
129static struct platform_driver syscon_led_driver = {
130 .probe = syscon_led_probe,
a917d4b4
LW
131 .driver = {
132 .name = "leds-syscon",
133 .of_match_table = of_syscon_leds_match,
f7d98a65 134 .suppress_bind_attrs = true,
a917d4b4
LW
135 },
136};
f7d98a65 137builtin_platform_driver(syscon_led_driver);