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