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