xref: /openbmc/linux/drivers/leds/leds-syscon.c (revision 8795a739)
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 device *dev = &pdev->dev;
59 	struct device_node *np = dev->of_node;
60 	struct device *parent;
61 	struct regmap *map;
62 	struct syscon_led *sled;
63 	const char *state;
64 	int ret;
65 
66 	parent = dev->parent;
67 	if (!parent) {
68 		dev_err(dev, "no parent for syscon LED\n");
69 		return -ENODEV;
70 	}
71 	map = syscon_node_to_regmap(parent->of_node);
72 	if (IS_ERR(map)) {
73 		dev_err(dev, "no regmap for syscon LED parent\n");
74 		return PTR_ERR(map);
75 	}
76 
77 	sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
78 	if (!sled)
79 		return -ENOMEM;
80 
81 	sled->map = map;
82 
83 	if (of_property_read_u32(np, "offset", &sled->offset))
84 		return -EINVAL;
85 	if (of_property_read_u32(np, "mask", &sled->mask))
86 		return -EINVAL;
87 	sled->cdev.name =
88 		of_get_property(np, "label", NULL) ? : np->name;
89 	sled->cdev.default_trigger =
90 		of_get_property(np, "linux,default-trigger", NULL);
91 
92 	state = of_get_property(np, "default-state", NULL);
93 	if (state) {
94 		if (!strcmp(state, "keep")) {
95 			u32 val;
96 
97 			ret = regmap_read(map, sled->offset, &val);
98 			if (ret < 0)
99 				return ret;
100 			sled->state = !!(val & sled->mask);
101 		} else if (!strcmp(state, "on")) {
102 			sled->state = true;
103 			ret = regmap_update_bits(map, sled->offset,
104 						 sled->mask,
105 						 sled->mask);
106 			if (ret < 0)
107 				return ret;
108 		} else {
109 			sled->state = false;
110 			ret = regmap_update_bits(map, sled->offset,
111 						 sled->mask, 0);
112 			if (ret < 0)
113 				return ret;
114 		}
115 	}
116 	sled->cdev.brightness_set = syscon_led_set;
117 
118 	ret = devm_led_classdev_register(dev, &sled->cdev);
119 	if (ret < 0)
120 		return ret;
121 
122 	platform_set_drvdata(pdev, sled);
123 	dev_info(dev, "registered LED %s\n", sled->cdev.name);
124 
125 	return 0;
126 }
127 
128 static const struct of_device_id of_syscon_leds_match[] = {
129 	{ .compatible = "register-bit-led", },
130 	{},
131 };
132 
133 static struct platform_driver syscon_led_driver = {
134 	.probe		= syscon_led_probe,
135 	.driver		= {
136 		.name	= "leds-syscon",
137 		.of_match_table = of_syscon_leds_match,
138 		.suppress_bind_attrs = true,
139 	},
140 };
141 builtin_platform_driver(syscon_led_driver);
142