xref: /linux/drivers/leds/leds-gpio.c (revision c79c3c34f75d72a066e292b10aa50fc758c97c89)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LEDs driver for GPIOs
4  *
5  * Copyright (C) 2007 8D Technologies inc.
6  * Raphael Assenat <raph@8d.com>
7  * Copyright (C) 2008 Freescale Semiconductor, Inc.
8  */
9 #include <linux/err.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/kernel.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
19 
20 struct gpio_led_data {
21 	struct led_classdev cdev;
22 	struct gpio_desc *gpiod;
23 	u8 can_sleep;
24 	u8 blinking;
25 	gpio_blink_set_t platform_gpio_blink_set;
26 };
27 
28 static inline struct gpio_led_data *
29 			cdev_to_gpio_led_data(struct led_classdev *led_cdev)
30 {
31 	return container_of(led_cdev, struct gpio_led_data, cdev);
32 }
33 
34 static void gpio_led_set(struct led_classdev *led_cdev,
35 	enum led_brightness value)
36 {
37 	struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
38 	int level;
39 
40 	if (value == LED_OFF)
41 		level = 0;
42 	else
43 		level = 1;
44 
45 	if (led_dat->blinking) {
46 		led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
47 						 NULL, NULL);
48 		led_dat->blinking = 0;
49 	} else {
50 		if (led_dat->can_sleep)
51 			gpiod_set_value_cansleep(led_dat->gpiod, level);
52 		else
53 			gpiod_set_value(led_dat->gpiod, level);
54 	}
55 }
56 
57 static int gpio_led_set_blocking(struct led_classdev *led_cdev,
58 	enum led_brightness value)
59 {
60 	gpio_led_set(led_cdev, value);
61 	return 0;
62 }
63 
64 static int gpio_blink_set(struct led_classdev *led_cdev,
65 	unsigned long *delay_on, unsigned long *delay_off)
66 {
67 	struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
68 
69 	led_dat->blinking = 1;
70 	return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
71 						delay_on, delay_off);
72 }
73 
74 static int create_gpio_led(const struct gpio_led *template,
75 	struct gpio_led_data *led_dat, struct device *parent,
76 	struct fwnode_handle *fwnode, gpio_blink_set_t blink_set)
77 {
78 	struct led_init_data init_data = {};
79 	int ret, state;
80 
81 	led_dat->cdev.default_trigger = template->default_trigger;
82 	led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
83 	if (!led_dat->can_sleep)
84 		led_dat->cdev.brightness_set = gpio_led_set;
85 	else
86 		led_dat->cdev.brightness_set_blocking = gpio_led_set_blocking;
87 	led_dat->blinking = 0;
88 	if (blink_set) {
89 		led_dat->platform_gpio_blink_set = blink_set;
90 		led_dat->cdev.blink_set = gpio_blink_set;
91 	}
92 	if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
93 		state = gpiod_get_value_cansleep(led_dat->gpiod);
94 		if (state < 0)
95 			return state;
96 	} else {
97 		state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
98 	}
99 	led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
100 	if (!template->retain_state_suspended)
101 		led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
102 	if (template->panic_indicator)
103 		led_dat->cdev.flags |= LED_PANIC_INDICATOR;
104 	if (template->retain_state_shutdown)
105 		led_dat->cdev.flags |= LED_RETAIN_AT_SHUTDOWN;
106 
107 	ret = gpiod_direction_output(led_dat->gpiod, state);
108 	if (ret < 0)
109 		return ret;
110 
111 	if (template->name) {
112 		led_dat->cdev.name = template->name;
113 		ret = devm_led_classdev_register(parent, &led_dat->cdev);
114 	} else {
115 		init_data.fwnode = fwnode;
116 		ret = devm_led_classdev_register_ext(parent, &led_dat->cdev,
117 						     &init_data);
118 	}
119 
120 	return ret;
121 }
122 
123 struct gpio_leds_priv {
124 	int num_leds;
125 	struct gpio_led_data leds[];
126 };
127 
128 static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
129 {
130 	struct device *dev = &pdev->dev;
131 	struct fwnode_handle *child;
132 	struct gpio_leds_priv *priv;
133 	int count, ret;
134 
135 	count = device_get_child_node_count(dev);
136 	if (!count)
137 		return ERR_PTR(-ENODEV);
138 
139 	priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL);
140 	if (!priv)
141 		return ERR_PTR(-ENOMEM);
142 
143 	device_for_each_child_node(dev, child) {
144 		struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
145 		struct gpio_led led = {};
146 		const char *state = NULL;
147 
148 		/*
149 		 * Acquire gpiod from DT with uninitialized label, which
150 		 * will be updated after LED class device is registered,
151 		 * Only then the final LED name is known.
152 		 */
153 		led.gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL, child,
154 							     GPIOD_ASIS,
155 							     NULL);
156 		if (IS_ERR(led.gpiod)) {
157 			fwnode_handle_put(child);
158 			return ERR_CAST(led.gpiod);
159 		}
160 
161 		led_dat->gpiod = led.gpiod;
162 
163 		if (!fwnode_property_read_string(child, "default-state",
164 						 &state)) {
165 			if (!strcmp(state, "keep"))
166 				led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
167 			else if (!strcmp(state, "on"))
168 				led.default_state = LEDS_GPIO_DEFSTATE_ON;
169 			else
170 				led.default_state = LEDS_GPIO_DEFSTATE_OFF;
171 		}
172 
173 		if (fwnode_property_present(child, "retain-state-suspended"))
174 			led.retain_state_suspended = 1;
175 		if (fwnode_property_present(child, "retain-state-shutdown"))
176 			led.retain_state_shutdown = 1;
177 		if (fwnode_property_present(child, "panic-indicator"))
178 			led.panic_indicator = 1;
179 
180 		ret = create_gpio_led(&led, led_dat, dev, child, NULL);
181 		if (ret < 0) {
182 			fwnode_handle_put(child);
183 			return ERR_PTR(ret);
184 		}
185 		/* Set gpiod label to match the corresponding LED name. */
186 		gpiod_set_consumer_name(led_dat->gpiod,
187 					led_dat->cdev.dev->kobj.name);
188 		priv->num_leds++;
189 	}
190 
191 	return priv;
192 }
193 
194 static const struct of_device_id of_gpio_leds_match[] = {
195 	{ .compatible = "gpio-leds", },
196 	{},
197 };
198 
199 MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
200 
201 static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
202 					    const struct gpio_led *template)
203 {
204 	struct gpio_desc *gpiod;
205 	unsigned long flags = GPIOF_OUT_INIT_LOW;
206 	int ret;
207 
208 	/*
209 	 * This means the LED does not come from the device tree
210 	 * or ACPI, so let's try just getting it by index from the
211 	 * device, this will hit the board file, if any and get
212 	 * the GPIO from there.
213 	 */
214 	gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOD_OUT_LOW);
215 	if (!IS_ERR(gpiod)) {
216 		gpiod_set_consumer_name(gpiod, template->name);
217 		return gpiod;
218 	}
219 	if (PTR_ERR(gpiod) != -ENOENT)
220 		return gpiod;
221 
222 	/*
223 	 * This is the legacy code path for platform code that
224 	 * still uses GPIO numbers. Ultimately we would like to get
225 	 * rid of this block completely.
226 	 */
227 
228 	/* skip leds that aren't available */
229 	if (!gpio_is_valid(template->gpio))
230 		return ERR_PTR(-ENOENT);
231 
232 	if (template->active_low)
233 		flags |= GPIOF_ACTIVE_LOW;
234 
235 	ret = devm_gpio_request_one(dev, template->gpio, flags,
236 				    template->name);
237 	if (ret < 0)
238 		return ERR_PTR(ret);
239 
240 	gpiod = gpio_to_desc(template->gpio);
241 	if (!gpiod)
242 		return ERR_PTR(-EINVAL);
243 
244 	return gpiod;
245 }
246 
247 static int gpio_led_probe(struct platform_device *pdev)
248 {
249 	struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
250 	struct gpio_leds_priv *priv;
251 	int i, ret = 0;
252 
253 	if (pdata && pdata->num_leds) {
254 		priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds),
255 				    GFP_KERNEL);
256 		if (!priv)
257 			return -ENOMEM;
258 
259 		priv->num_leds = pdata->num_leds;
260 		for (i = 0; i < priv->num_leds; i++) {
261 			const struct gpio_led *template = &pdata->leds[i];
262 			struct gpio_led_data *led_dat = &priv->leds[i];
263 
264 			if (template->gpiod)
265 				led_dat->gpiod = template->gpiod;
266 			else
267 				led_dat->gpiod =
268 					gpio_led_get_gpiod(&pdev->dev,
269 							   i, template);
270 			if (IS_ERR(led_dat->gpiod)) {
271 				dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n",
272 					 template->gpio, template->name);
273 				continue;
274 			}
275 
276 			ret = create_gpio_led(template, led_dat,
277 					      &pdev->dev, NULL,
278 					      pdata->gpio_blink_set);
279 			if (ret < 0)
280 				return ret;
281 		}
282 	} else {
283 		priv = gpio_leds_create(pdev);
284 		if (IS_ERR(priv))
285 			return PTR_ERR(priv);
286 	}
287 
288 	platform_set_drvdata(pdev, priv);
289 
290 	return 0;
291 }
292 
293 static void gpio_led_shutdown(struct platform_device *pdev)
294 {
295 	struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
296 	int i;
297 
298 	for (i = 0; i < priv->num_leds; i++) {
299 		struct gpio_led_data *led = &priv->leds[i];
300 
301 		if (!(led->cdev.flags & LED_RETAIN_AT_SHUTDOWN))
302 			gpio_led_set(&led->cdev, LED_OFF);
303 	}
304 }
305 
306 static struct platform_driver gpio_led_driver = {
307 	.probe		= gpio_led_probe,
308 	.shutdown	= gpio_led_shutdown,
309 	.driver		= {
310 		.name	= "leds-gpio",
311 		.of_match_table = of_gpio_leds_match,
312 	},
313 };
314 
315 module_platform_driver(gpio_led_driver);
316 
317 MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
318 MODULE_DESCRIPTION("GPIO LED driver");
319 MODULE_LICENSE("GPL");
320 MODULE_ALIAS("platform:leds-gpio");
321