1/*
2 * LEDs driver for GPIOs
3 *
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
6 * Copyright (C) 2008 Freescale Semiconductor, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13#include <linux/err.h>
14#include <linux/gpio.h>
15#include <linux/gpio/consumer.h>
16#include <linux/kernel.h>
17#include <linux/leds.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/platform_device.h>
21#include <linux/property.h>
22#include <linux/slab.h>
23#include <linux/workqueue.h>
24
25struct gpio_led_data {
26	struct led_classdev cdev;
27	struct gpio_desc *gpiod;
28	struct work_struct work;
29	u8 new_level;
30	u8 can_sleep;
31	u8 blinking;
32	int (*platform_gpio_blink_set)(struct gpio_desc *desc, int state,
33			unsigned long *delay_on, unsigned long *delay_off);
34};
35
36static void gpio_led_work(struct work_struct *work)
37{
38	struct gpio_led_data *led_dat =
39		container_of(work, struct gpio_led_data, work);
40
41	if (led_dat->blinking) {
42		led_dat->platform_gpio_blink_set(led_dat->gpiod,
43					led_dat->new_level, NULL, NULL);
44		led_dat->blinking = 0;
45	} else
46		gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level);
47}
48
49static void gpio_led_set(struct led_classdev *led_cdev,
50	enum led_brightness value)
51{
52	struct gpio_led_data *led_dat =
53		container_of(led_cdev, struct gpio_led_data, cdev);
54	int level;
55
56	if (value == LED_OFF)
57		level = 0;
58	else
59		level = 1;
60
61	/* Setting GPIOs with I2C/etc requires a task context, and we don't
62	 * seem to have a reliable way to know if we're already in one; so
63	 * let's just assume the worst.
64	 */
65	if (led_dat->can_sleep) {
66		led_dat->new_level = level;
67		schedule_work(&led_dat->work);
68	} else {
69		if (led_dat->blinking) {
70			led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
71							 NULL, NULL);
72			led_dat->blinking = 0;
73		} else
74			gpiod_set_value(led_dat->gpiod, level);
75	}
76}
77
78static int gpio_blink_set(struct led_classdev *led_cdev,
79	unsigned long *delay_on, unsigned long *delay_off)
80{
81	struct gpio_led_data *led_dat =
82		container_of(led_cdev, struct gpio_led_data, cdev);
83
84	led_dat->blinking = 1;
85	return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
86						delay_on, delay_off);
87}
88
89static int create_gpio_led(const struct gpio_led *template,
90	struct gpio_led_data *led_dat, struct device *parent,
91	int (*blink_set)(struct gpio_desc *, int, unsigned long *,
92			 unsigned long *))
93{
94	int ret, state;
95
96	led_dat->gpiod = template->gpiod;
97	if (!led_dat->gpiod) {
98		/*
99		 * This is the legacy code path for platform code that
100		 * still uses GPIO numbers. Ultimately we would like to get
101		 * rid of this block completely.
102		 */
103		unsigned long flags = 0;
104
105		/* skip leds that aren't available */
106		if (!gpio_is_valid(template->gpio)) {
107			dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
108					template->gpio, template->name);
109			return 0;
110		}
111
112		if (template->active_low)
113			flags |= GPIOF_ACTIVE_LOW;
114
115		ret = devm_gpio_request_one(parent, template->gpio, flags,
116					    template->name);
117		if (ret < 0)
118			return ret;
119
120		led_dat->gpiod = gpio_to_desc(template->gpio);
121		if (IS_ERR(led_dat->gpiod))
122			return PTR_ERR(led_dat->gpiod);
123	}
124
125	led_dat->cdev.name = template->name;
126	led_dat->cdev.default_trigger = template->default_trigger;
127	led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
128	led_dat->blinking = 0;
129	if (blink_set) {
130		led_dat->platform_gpio_blink_set = blink_set;
131		led_dat->cdev.blink_set = gpio_blink_set;
132	}
133	led_dat->cdev.brightness_set = gpio_led_set;
134	if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP)
135		state = !!gpiod_get_value_cansleep(led_dat->gpiod);
136	else
137		state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
138	led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
139	if (!template->retain_state_suspended)
140		led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
141
142	ret = gpiod_direction_output(led_dat->gpiod, state);
143	if (ret < 0)
144		return ret;
145
146	INIT_WORK(&led_dat->work, gpio_led_work);
147
148	return led_classdev_register(parent, &led_dat->cdev);
149}
150
151static void delete_gpio_led(struct gpio_led_data *led)
152{
153	led_classdev_unregister(&led->cdev);
154	cancel_work_sync(&led->work);
155}
156
157struct gpio_leds_priv {
158	int num_leds;
159	struct gpio_led_data leds[];
160};
161
162static inline int sizeof_gpio_leds_priv(int num_leds)
163{
164	return sizeof(struct gpio_leds_priv) +
165		(sizeof(struct gpio_led_data) * num_leds);
166}
167
168static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
169{
170	struct device *dev = &pdev->dev;
171	struct fwnode_handle *child;
172	struct gpio_leds_priv *priv;
173	int count, ret;
174	struct device_node *np;
175
176	count = device_get_child_node_count(dev);
177	if (!count)
178		return ERR_PTR(-ENODEV);
179
180	priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
181	if (!priv)
182		return ERR_PTR(-ENOMEM);
183
184	device_for_each_child_node(dev, child) {
185		struct gpio_led led = {};
186		const char *state = NULL;
187
188		led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
189		if (IS_ERR(led.gpiod)) {
190			fwnode_handle_put(child);
191			ret = PTR_ERR(led.gpiod);
192			goto err;
193		}
194
195		np = to_of_node(child);
196
197		if (fwnode_property_present(child, "label")) {
198			fwnode_property_read_string(child, "label", &led.name);
199		} else {
200			if (IS_ENABLED(CONFIG_OF) && !led.name && np)
201				led.name = np->name;
202			if (!led.name) {
203				ret = -EINVAL;
204				goto err;
205			}
206		}
207		fwnode_property_read_string(child, "linux,default-trigger",
208					    &led.default_trigger);
209
210		if (!fwnode_property_read_string(child, "default-state",
211						 &state)) {
212			if (!strcmp(state, "keep"))
213				led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
214			else if (!strcmp(state, "on"))
215				led.default_state = LEDS_GPIO_DEFSTATE_ON;
216			else
217				led.default_state = LEDS_GPIO_DEFSTATE_OFF;
218		}
219
220		if (fwnode_property_present(child, "retain-state-suspended"))
221			led.retain_state_suspended = 1;
222
223		ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
224				      dev, NULL);
225		if (ret < 0) {
226			fwnode_handle_put(child);
227			goto err;
228		}
229		priv->num_leds++;
230	}
231
232	return priv;
233
234err:
235	for (count = priv->num_leds - 1; count >= 0; count--)
236		delete_gpio_led(&priv->leds[count]);
237	return ERR_PTR(ret);
238}
239
240static const struct of_device_id of_gpio_leds_match[] = {
241	{ .compatible = "gpio-leds", },
242	{},
243};
244
245MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
246
247static 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,
255				sizeof_gpio_leds_priv(pdata->num_leds),
256					GFP_KERNEL);
257		if (!priv)
258			return -ENOMEM;
259
260		priv->num_leds = pdata->num_leds;
261		for (i = 0; i < priv->num_leds; i++) {
262			ret = create_gpio_led(&pdata->leds[i],
263					      &priv->leds[i],
264					      &pdev->dev, pdata->gpio_blink_set);
265			if (ret < 0) {
266				/* On failure: unwind the led creations */
267				for (i = i - 1; i >= 0; i--)
268					delete_gpio_led(&priv->leds[i]);
269				return ret;
270			}
271		}
272	} else {
273		priv = gpio_leds_create(pdev);
274		if (IS_ERR(priv))
275			return PTR_ERR(priv);
276	}
277
278	platform_set_drvdata(pdev, priv);
279
280	return 0;
281}
282
283static int gpio_led_remove(struct platform_device *pdev)
284{
285	struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
286	int i;
287
288	for (i = 0; i < priv->num_leds; i++)
289		delete_gpio_led(&priv->leds[i]);
290
291	return 0;
292}
293
294static void gpio_led_shutdown(struct platform_device *pdev)
295{
296	struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
297	int i;
298
299	for (i = 0; i < priv->num_leds; i++) {
300		struct gpio_led_data *led = &priv->leds[i];
301
302		gpio_led_set(&led->cdev, LED_OFF);
303	}
304}
305
306static struct platform_driver gpio_led_driver = {
307	.probe		= gpio_led_probe,
308	.remove		= gpio_led_remove,
309	.shutdown	= gpio_led_shutdown,
310	.driver		= {
311		.name	= "leds-gpio",
312		.of_match_table = of_gpio_leds_match,
313	},
314};
315
316module_platform_driver(gpio_led_driver);
317
318MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
319MODULE_DESCRIPTION("GPIO LED driver");
320MODULE_LICENSE("GPL");
321MODULE_ALIAS("platform:leds-gpio");
322