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