]>
Commit | Line | Data |
---|---|---|
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> |
403097f7 | 19 | #include <linux/of.h> |
4cc72346 | 20 | #include <linux/platform_device.h> |
a43f2cbb | 21 | #include <linux/property.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
00852279 DB |
23 | #include <linux/workqueue.h> |
24 | ||
22e03f3b RA |
25 | struct gpio_led_data { |
26 | struct led_classdev cdev; | |
5c51277a | 27 | struct gpio_desc *gpiod; |
00852279 DB |
28 | struct work_struct work; |
29 | u8 new_level; | |
30 | u8 can_sleep; | |
2146325d | 31 | u8 blinking; |
c673a2b4 | 32 | int (*platform_gpio_blink_set)(struct gpio_desc *desc, int state, |
ca3259b3 | 33 | unsigned long *delay_on, unsigned long *delay_off); |
22e03f3b RA |
34 | }; |
35 | ||
00852279 DB |
36 | static void gpio_led_work(struct work_struct *work) |
37 | { | |
a4c84e6a | 38 | struct gpio_led_data *led_dat = |
00852279 DB |
39 | container_of(work, struct gpio_led_data, work); |
40 | ||
2146325d | 41 | if (led_dat->blinking) { |
c673a2b4 MW |
42 | led_dat->platform_gpio_blink_set(led_dat->gpiod, |
43 | led_dat->new_level, NULL, NULL); | |
2146325d BH |
44 | led_dat->blinking = 0; |
45 | } else | |
5c51277a | 46 | gpiod_set_value_cansleep(led_dat->gpiod, led_dat->new_level); |
00852279 | 47 | } |
22e03f3b RA |
48 | |
49 | static 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 | ||
306dd85c DB |
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 | */ | |
00852279 | 65 | if (led_dat->can_sleep) { |
306dd85c DB |
66 | led_dat->new_level = level; |
67 | schedule_work(&led_dat->work); | |
2146325d BH |
68 | } else { |
69 | if (led_dat->blinking) { | |
c673a2b4 MW |
70 | led_dat->platform_gpio_blink_set(led_dat->gpiod, level, |
71 | NULL, NULL); | |
2146325d BH |
72 | led_dat->blinking = 0; |
73 | } else | |
5c51277a | 74 | gpiod_set_value(led_dat->gpiod, level); |
2146325d | 75 | } |
22e03f3b RA |
76 | } |
77 | ||
ca3259b3 HVR |
78 | static 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 | ||
2146325d | 84 | led_dat->blinking = 1; |
c673a2b4 | 85 | return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK, |
2146325d | 86 | delay_on, delay_off); |
ca3259b3 HVR |
87 | } |
88 | ||
98ea1ea2 | 89 | static int create_gpio_led(const struct gpio_led *template, |
a7d878af | 90 | struct gpio_led_data *led_dat, struct device *parent, |
c673a2b4 MW |
91 | int (*blink_set)(struct gpio_desc *, int, unsigned long *, |
92 | unsigned long *)) | |
a7d878af | 93 | { |
ed88bae6 | 94 | int ret, state; |
a7d878af | 95 | |
ec98a497 GU |
96 | led_dat->gpiod = template->gpiod; |
97 | if (!led_dat->gpiod) { | |
c673a2b4 MW |
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 | */ | |
5c51277a | 103 | unsigned long flags = 0; |
0b4634fc | 104 | |
5c51277a MW |
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 | } | |
d379ee8a | 111 | |
5c51277a MW |
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 | } | |
803d19d5 | 124 | |
a7d878af TP |
125 | led_dat->cdev.name = template->name; |
126 | led_dat->cdev.default_trigger = template->default_trigger; | |
ec98a497 | 127 | led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod); |
2146325d | 128 | led_dat->blinking = 0; |
a7d878af TP |
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; | |
ed88bae6 | 134 | if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) |
5c51277a | 135 | state = !!gpiod_get_value_cansleep(led_dat->gpiod); |
ed88bae6 TP |
136 | else |
137 | state = (template->default_state == LEDS_GPIO_DEFSTATE_ON); | |
138 | led_dat->cdev.brightness = state ? LED_FULL : LED_OFF; | |
defb512d RP |
139 | if (!template->retain_state_suspended) |
140 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; | |
a7d878af | 141 | |
5c51277a | 142 | ret = gpiod_direction_output(led_dat->gpiod, state); |
a7d878af | 143 | if (ret < 0) |
a99d76f9 JH |
144 | return ret; |
145 | ||
a7d878af TP |
146 | INIT_WORK(&led_dat->work, gpio_led_work); |
147 | ||
5c51277a | 148 | return led_classdev_register(parent, &led_dat->cdev); |
a7d878af TP |
149 | } |
150 | ||
151 | static void delete_gpio_led(struct gpio_led_data *led) | |
152 | { | |
153 | led_classdev_unregister(&led->cdev); | |
154 | cancel_work_sync(&led->work); | |
a7d878af TP |
155 | } |
156 | ||
a314c5c0 GL |
157 | struct gpio_leds_priv { |
158 | int num_leds; | |
159 | struct gpio_led_data leds[]; | |
160 | }; | |
22e03f3b | 161 | |
a314c5c0 | 162 | static inline int sizeof_gpio_leds_priv(int num_leds) |
22e03f3b | 163 | { |
a314c5c0 GL |
164 | return sizeof(struct gpio_leds_priv) + |
165 | (sizeof(struct gpio_led_data) * num_leds); | |
22e03f3b RA |
166 | } |
167 | ||
a43f2cbb | 168 | static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) |
a7d878af | 169 | { |
a43f2cbb RW |
170 | struct device *dev = &pdev->dev; |
171 | struct fwnode_handle *child; | |
a314c5c0 | 172 | struct gpio_leds_priv *priv; |
127aedc8 | 173 | int count, ret; |
29470ea8 | 174 | struct device_node *np; |
a7d878af | 175 | |
a43f2cbb | 176 | count = device_get_child_node_count(dev); |
a7d878af | 177 | if (!count) |
04553e92 RS |
178 | return ERR_PTR(-ENODEV); |
179 | ||
a43f2cbb | 180 | priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL); |
a314c5c0 | 181 | if (!priv) |
04553e92 | 182 | return ERR_PTR(-ENOMEM); |
a7d878af | 183 | |
a43f2cbb | 184 | device_for_each_child_node(dev, child) { |
0493a4ff | 185 | struct gpio_led led = {}; |
a43f2cbb RW |
186 | const char *state = NULL; |
187 | ||
1feb57a2 | 188 | led.gpiod = devm_get_gpiod_from_child(dev, NULL, child); |
a43f2cbb RW |
189 | if (IS_ERR(led.gpiod)) { |
190 | fwnode_handle_put(child); | |
c6e71f81 | 191 | ret = PTR_ERR(led.gpiod); |
a43f2cbb RW |
192 | goto err; |
193 | } | |
194 | ||
c181fb3e | 195 | np = to_of_node(child); |
29470ea8 FE |
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; | |
0e14e0bf JA |
202 | if (!led.name) { |
203 | ret = -EINVAL; | |
204 | goto err; | |
205 | } | |
29470ea8 | 206 | } |
a43f2cbb RW |
207 | fwnode_property_read_string(child, "linux,default-trigger", |
208 | &led.default_trigger); | |
209 | ||
d735d25e | 210 | if (!fwnode_property_read_string(child, "default-state", |
a43f2cbb | 211 | &state)) { |
ed88bae6 TP |
212 | if (!strcmp(state, "keep")) |
213 | led.default_state = LEDS_GPIO_DEFSTATE_KEEP; | |
a314c5c0 | 214 | else if (!strcmp(state, "on")) |
ed88bae6 TP |
215 | led.default_state = LEDS_GPIO_DEFSTATE_ON; |
216 | else | |
217 | led.default_state = LEDS_GPIO_DEFSTATE_OFF; | |
218 | } | |
a7d878af | 219 | |
a43f2cbb | 220 | if (fwnode_property_present(child, "retain-state-suspended")) |
4270a78d RG |
221 | led.retain_state_suspended = 1; |
222 | ||
65c6b7e3 | 223 | ret = create_gpio_led(&led, &priv->leds[priv->num_leds], |
a43f2cbb | 224 | dev, NULL); |
a7d878af | 225 | if (ret < 0) { |
a43f2cbb | 226 | fwnode_handle_put(child); |
a7d878af TP |
227 | goto err; |
228 | } | |
65c6b7e3 | 229 | priv->num_leds++; |
a7d878af TP |
230 | } |
231 | ||
a314c5c0 | 232 | return priv; |
a7d878af TP |
233 | |
234 | err: | |
65c6b7e3 | 235 | for (count = priv->num_leds - 1; count >= 0; count--) |
a314c5c0 | 236 | delete_gpio_led(&priv->leds[count]); |
c6e71f81 | 237 | return ERR_PTR(ret); |
a314c5c0 | 238 | } |
a7d878af | 239 | |
a314c5c0 GL |
240 | static const struct of_device_id of_gpio_leds_match[] = { |
241 | { .compatible = "gpio-leds", }, | |
242 | {}, | |
243 | }; | |
472b854b PP |
244 | |
245 | MODULE_DEVICE_TABLE(of, of_gpio_leds_match); | |
a7d878af | 246 | |
98ea1ea2 | 247 | static int gpio_led_probe(struct platform_device *pdev) |
a314c5c0 | 248 | { |
87aae1ea | 249 | struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev); |
a314c5c0 GL |
250 | struct gpio_leds_priv *priv; |
251 | int i, ret = 0; | |
252 | ||
253 | if (pdata && pdata->num_leds) { | |
198b8611 SK |
254 | priv = devm_kzalloc(&pdev->dev, |
255 | sizeof_gpio_leds_priv(pdata->num_leds), | |
256 | GFP_KERNEL); | |
a314c5c0 GL |
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]); | |
a314c5c0 GL |
269 | return ret; |
270 | } | |
271 | } | |
272 | } else { | |
a43f2cbb | 273 | priv = gpio_leds_create(pdev); |
04553e92 RS |
274 | if (IS_ERR(priv)) |
275 | return PTR_ERR(priv); | |
a314c5c0 GL |
276 | } |
277 | ||
278 | platform_set_drvdata(pdev, priv); | |
279 | ||
280 | return 0; | |
a7d878af TP |
281 | } |
282 | ||
678e8a6b | 283 | static int gpio_led_remove(struct platform_device *pdev) |
a7d878af | 284 | { |
59c4dce1 | 285 | struct gpio_leds_priv *priv = platform_get_drvdata(pdev); |
a7d878af TP |
286 | int i; |
287 | ||
a314c5c0 GL |
288 | for (i = 0; i < priv->num_leds; i++) |
289 | delete_gpio_led(&priv->leds[i]); | |
a7d878af | 290 | |
a7d878af TP |
291 | return 0; |
292 | } | |
293 | ||
707f33ed HS |
294 | static 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 | ||
a314c5c0 GL |
306 | static struct platform_driver gpio_led_driver = { |
307 | .probe = gpio_led_probe, | |
df07cf81 | 308 | .remove = gpio_led_remove, |
707f33ed | 309 | .shutdown = gpio_led_shutdown, |
a314c5c0 GL |
310 | .driver = { |
311 | .name = "leds-gpio", | |
a43f2cbb | 312 | .of_match_table = of_gpio_leds_match, |
a7d878af | 313 | }, |
a7d878af | 314 | }; |
a314c5c0 | 315 | |
892a8843 | 316 | module_platform_driver(gpio_led_driver); |
a7d878af TP |
317 | |
318 | MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>"); | |
22e03f3b RA |
319 | MODULE_DESCRIPTION("GPIO LED driver"); |
320 | MODULE_LICENSE("GPL"); | |
892a8843 | 321 | MODULE_ALIAS("platform:leds-gpio"); |