Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * LEDs driver for GPIOs |
| 3 | * |
| 4 | * Copyright (C) 2007 8D Technologies inc. |
| 5 | * Raphael Assenat <raph@8d.com> |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 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/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/leds.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 18 | #include <linux/workqueue.h> |
| 19 | |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 20 | #include <asm/gpio.h> |
| 21 | |
| 22 | struct gpio_led_data { |
| 23 | struct led_classdev cdev; |
| 24 | unsigned gpio; |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 25 | struct work_struct work; |
| 26 | u8 new_level; |
| 27 | u8 can_sleep; |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 28 | u8 active_low; |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 29 | u8 blinking; |
| 30 | int (*platform_gpio_blink_set)(unsigned gpio, int state, |
Herbert Valerio Riedel | ca3259b | 2008-03-09 23:48:25 +0000 | [diff] [blame] | 31 | unsigned long *delay_on, unsigned long *delay_off); |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 34 | static void gpio_led_work(struct work_struct *work) |
| 35 | { |
| 36 | struct gpio_led_data *led_dat = |
| 37 | container_of(work, struct gpio_led_data, work); |
| 38 | |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 39 | if (led_dat->blinking) { |
| 40 | led_dat->platform_gpio_blink_set(led_dat->gpio, |
| 41 | led_dat->new_level, |
| 42 | NULL, NULL); |
| 43 | led_dat->blinking = 0; |
| 44 | } else |
| 45 | gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level); |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 46 | } |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 47 | |
| 48 | static 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 | |
| 60 | if (led_dat->active_low) |
| 61 | level = !level; |
| 62 | |
David Brownell | 306dd85 | 2008-03-27 00:59:02 +0000 | [diff] [blame] | 63 | /* Setting GPIOs with I2C/etc requires a task context, and we don't |
| 64 | * seem to have a reliable way to know if we're already in one; so |
| 65 | * let's just assume the worst. |
| 66 | */ |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 67 | if (led_dat->can_sleep) { |
David Brownell | 306dd85 | 2008-03-27 00:59:02 +0000 | [diff] [blame] | 68 | led_dat->new_level = level; |
| 69 | schedule_work(&led_dat->work); |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 70 | } else { |
| 71 | if (led_dat->blinking) { |
| 72 | led_dat->platform_gpio_blink_set(led_dat->gpio, level, |
| 73 | NULL, NULL); |
| 74 | led_dat->blinking = 0; |
| 75 | } else |
| 76 | gpio_set_value(led_dat->gpio, level); |
| 77 | } |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Herbert Valerio Riedel | ca3259b | 2008-03-09 23:48:25 +0000 | [diff] [blame] | 80 | static int gpio_blink_set(struct led_classdev *led_cdev, |
| 81 | unsigned long *delay_on, unsigned long *delay_off) |
| 82 | { |
| 83 | struct gpio_led_data *led_dat = |
| 84 | container_of(led_cdev, struct gpio_led_data, cdev); |
| 85 | |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 86 | led_dat->blinking = 1; |
| 87 | return led_dat->platform_gpio_blink_set(led_dat->gpio, GPIO_LED_BLINK, |
| 88 | delay_on, delay_off); |
Herbert Valerio Riedel | ca3259b | 2008-03-09 23:48:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 91 | static int __devinit create_gpio_led(const struct gpio_led *template, |
| 92 | struct gpio_led_data *led_dat, struct device *parent, |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 93 | int (*blink_set)(unsigned, int, unsigned long *, unsigned long *)) |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 94 | { |
Trent Piepho | ed88bae | 2009-05-12 15:33:12 -0700 | [diff] [blame] | 95 | int ret, state; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 96 | |
Dmitry Eremin-Solenikov | 0b4634f | 2009-11-16 01:48:32 +0300 | [diff] [blame] | 97 | led_dat->gpio = -1; |
| 98 | |
David Brownell | d379ee8 | 2009-03-05 16:46:44 -0800 | [diff] [blame] | 99 | /* skip leds that aren't available */ |
| 100 | if (!gpio_is_valid(template->gpio)) { |
Michal Simek | 2fea092 | 2009-08-06 16:04:51 -0700 | [diff] [blame] | 101 | printk(KERN_INFO "Skipping unavailable LED gpio %d (%s)\n", |
David Brownell | d379ee8 | 2009-03-05 16:46:44 -0800 | [diff] [blame] | 102 | template->gpio, template->name); |
David Brownell | ac15e95 | 2009-04-07 17:51:49 -0700 | [diff] [blame] | 103 | return 0; |
David Brownell | d379ee8 | 2009-03-05 16:46:44 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 106 | ret = gpio_request(template->gpio, template->name); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | led_dat->cdev.name = template->name; |
| 111 | led_dat->cdev.default_trigger = template->default_trigger; |
| 112 | led_dat->gpio = template->gpio; |
| 113 | led_dat->can_sleep = gpio_cansleep(template->gpio); |
| 114 | led_dat->active_low = template->active_low; |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 115 | led_dat->blinking = 0; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 116 | if (blink_set) { |
| 117 | led_dat->platform_gpio_blink_set = blink_set; |
| 118 | led_dat->cdev.blink_set = gpio_blink_set; |
| 119 | } |
| 120 | led_dat->cdev.brightness_set = gpio_led_set; |
Trent Piepho | ed88bae | 2009-05-12 15:33:12 -0700 | [diff] [blame] | 121 | if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) |
| 122 | state = !!gpio_get_value(led_dat->gpio) ^ led_dat->active_low; |
| 123 | else |
| 124 | state = (template->default_state == LEDS_GPIO_DEFSTATE_ON); |
| 125 | led_dat->cdev.brightness = state ? LED_FULL : LED_OFF; |
Richard Purdie | defb512 | 2009-02-17 15:04:07 +0000 | [diff] [blame] | 126 | if (!template->retain_state_suspended) |
| 127 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 128 | |
Trent Piepho | ed88bae | 2009-05-12 15:33:12 -0700 | [diff] [blame] | 129 | ret = gpio_direction_output(led_dat->gpio, led_dat->active_low ^ state); |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | goto err; |
Benjamin Herrenschmidt | 2146325 | 2010-05-22 20:54:55 +1000 | [diff] [blame] | 132 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 133 | INIT_WORK(&led_dat->work, gpio_led_work); |
| 134 | |
| 135 | ret = led_classdev_register(parent, &led_dat->cdev); |
| 136 | if (ret < 0) |
| 137 | goto err; |
| 138 | |
| 139 | return 0; |
| 140 | err: |
| 141 | gpio_free(led_dat->gpio); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static void delete_gpio_led(struct gpio_led_data *led) |
| 146 | { |
David Brownell | d379ee8 | 2009-03-05 16:46:44 -0800 | [diff] [blame] | 147 | if (!gpio_is_valid(led->gpio)) |
| 148 | return; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 149 | led_classdev_unregister(&led->cdev); |
| 150 | cancel_work_sync(&led->work); |
| 151 | gpio_free(led->gpio); |
| 152 | } |
| 153 | |
| 154 | #ifdef CONFIG_LEDS_GPIO_PLATFORM |
Zhenwen Xu | 7fd0217 | 2009-06-10 12:48:51 -0700 | [diff] [blame] | 155 | static int __devinit gpio_led_probe(struct platform_device *pdev) |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 156 | { |
| 157 | struct gpio_led_platform_data *pdata = pdev->dev.platform_data; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 158 | struct gpio_led_data *leds_data; |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 159 | int i, ret = 0; |
| 160 | |
| 161 | if (!pdata) |
| 162 | return -EBUSY; |
| 163 | |
| 164 | leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds, |
| 165 | GFP_KERNEL); |
| 166 | if (!leds_data) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | for (i = 0; i < pdata->num_leds; i++) { |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 170 | ret = create_gpio_led(&pdata->leds[i], &leds_data[i], |
| 171 | &pdev->dev, pdata->gpio_blink_set); |
Uwe Kleine-König | d95cbe6 | 2008-03-09 20:42:27 +0000 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | goto err; |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | platform_set_drvdata(pdev, leds_data); |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | err: |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 181 | for (i = i - 1; i >= 0; i--) |
| 182 | delete_gpio_led(&leds_data[i]); |
David Brownell | 0085227 | 2007-05-10 10:51:41 +0100 | [diff] [blame] | 183 | |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 184 | kfree(leds_data); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
David Brownell | 199fb21 | 2007-10-31 10:37:37 +0100 | [diff] [blame] | 189 | static int __devexit gpio_led_remove(struct platform_device *pdev) |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 190 | { |
| 191 | int i; |
| 192 | struct gpio_led_platform_data *pdata = pdev->dev.platform_data; |
| 193 | struct gpio_led_data *leds_data; |
| 194 | |
| 195 | leds_data = platform_get_drvdata(pdev); |
| 196 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 197 | for (i = 0; i < pdata->num_leds; i++) |
| 198 | delete_gpio_led(&leds_data[i]); |
David Brownell | 199fb21 | 2007-10-31 10:37:37 +0100 | [diff] [blame] | 199 | |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 200 | kfree(leds_data); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 205 | static struct platform_driver gpio_led_driver = { |
David Brownell | 199fb21 | 2007-10-31 10:37:37 +0100 | [diff] [blame] | 206 | .probe = gpio_led_probe, |
| 207 | .remove = __devexit_p(gpio_led_remove), |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 208 | .driver = { |
| 209 | .name = "leds-gpio", |
| 210 | .owner = THIS_MODULE, |
| 211 | }, |
| 212 | }; |
| 213 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 214 | MODULE_ALIAS("platform:leds-gpio"); |
| 215 | #endif /* CONFIG_LEDS_GPIO_PLATFORM */ |
| 216 | |
| 217 | /* Code to create from OpenFirmware platform devices */ |
| 218 | #ifdef CONFIG_LEDS_GPIO_OF |
| 219 | #include <linux/of_platform.h> |
| 220 | #include <linux/of_gpio.h> |
| 221 | |
| 222 | struct gpio_led_of_platform_data { |
| 223 | int num_leds; |
| 224 | struct gpio_led_data led_data[]; |
| 225 | }; |
| 226 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 227 | static int __devinit of_gpio_leds_probe(struct platform_device *ofdev, |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 228 | const struct of_device_id *match) |
| 229 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 230 | struct device_node *np = ofdev->dev.of_node, *child; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 231 | struct gpio_led_of_platform_data *pdata; |
| 232 | int count = 0, ret; |
| 233 | |
| 234 | /* count LEDs defined by this device, so we know how much to allocate */ |
| 235 | for_each_child_of_node(np, child) |
| 236 | count++; |
| 237 | if (!count) |
| 238 | return 0; /* or ENODEV? */ |
| 239 | |
| 240 | pdata = kzalloc(sizeof(*pdata) + sizeof(struct gpio_led_data) * count, |
| 241 | GFP_KERNEL); |
| 242 | if (!pdata) |
| 243 | return -ENOMEM; |
| 244 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 245 | for_each_child_of_node(np, child) { |
Anton Vorontsov | 0493a4f | 2010-03-11 13:58:47 -0800 | [diff] [blame] | 246 | struct gpio_led led = {}; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 247 | enum of_gpio_flags flags; |
Trent Piepho | ed88bae | 2009-05-12 15:33:12 -0700 | [diff] [blame] | 248 | const char *state; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 249 | |
| 250 | led.gpio = of_get_gpio_flags(child, 0, &flags); |
| 251 | led.active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 252 | led.name = of_get_property(child, "label", NULL) ? : child->name; |
| 253 | led.default_trigger = |
| 254 | of_get_property(child, "linux,default-trigger", NULL); |
Trent Piepho | ed88bae | 2009-05-12 15:33:12 -0700 | [diff] [blame] | 255 | state = of_get_property(child, "default-state", NULL); |
| 256 | if (state) { |
| 257 | if (!strcmp(state, "keep")) |
| 258 | led.default_state = LEDS_GPIO_DEFSTATE_KEEP; |
| 259 | else if(!strcmp(state, "on")) |
| 260 | led.default_state = LEDS_GPIO_DEFSTATE_ON; |
| 261 | else |
| 262 | led.default_state = LEDS_GPIO_DEFSTATE_OFF; |
| 263 | } |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 264 | |
| 265 | ret = create_gpio_led(&led, &pdata->led_data[pdata->num_leds++], |
| 266 | &ofdev->dev, NULL); |
| 267 | if (ret < 0) { |
| 268 | of_node_put(child); |
| 269 | goto err; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | dev_set_drvdata(&ofdev->dev, pdata); |
| 274 | |
| 275 | return 0; |
| 276 | |
| 277 | err: |
| 278 | for (count = pdata->num_leds - 2; count >= 0; count--) |
| 279 | delete_gpio_led(&pdata->led_data[count]); |
| 280 | |
| 281 | kfree(pdata); |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 286 | static int __devexit of_gpio_leds_remove(struct platform_device *ofdev) |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 287 | { |
| 288 | struct gpio_led_of_platform_data *pdata = dev_get_drvdata(&ofdev->dev); |
| 289 | int i; |
| 290 | |
| 291 | for (i = 0; i < pdata->num_leds; i++) |
| 292 | delete_gpio_led(&pdata->led_data[i]); |
| 293 | |
| 294 | kfree(pdata); |
| 295 | |
| 296 | dev_set_drvdata(&ofdev->dev, NULL); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static const struct of_device_id of_gpio_leds_match[] = { |
| 302 | { .compatible = "gpio-leds", }, |
| 303 | {}, |
| 304 | }; |
| 305 | |
| 306 | static struct of_platform_driver of_gpio_leds_driver = { |
| 307 | .driver = { |
| 308 | .name = "of_gpio_leds", |
| 309 | .owner = THIS_MODULE, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 310 | .of_match_table = of_gpio_leds_match, |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 311 | }, |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 312 | .probe = of_gpio_leds_probe, |
| 313 | .remove = __devexit_p(of_gpio_leds_remove), |
| 314 | }; |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 315 | #endif |
| 316 | |
Richard Purdie | b2bdc3e | 2009-02-02 23:04:42 +0000 | [diff] [blame] | 317 | static int __init gpio_led_init(void) |
| 318 | { |
Davidlohr Bueso | 25672b9 | 2010-11-11 14:05:24 -0800 | [diff] [blame] | 319 | int ret = 0; |
Richard Purdie | b2bdc3e | 2009-02-02 23:04:42 +0000 | [diff] [blame] | 320 | |
| 321 | #ifdef CONFIG_LEDS_GPIO_PLATFORM |
| 322 | ret = platform_driver_register(&gpio_led_driver); |
| 323 | if (ret) |
| 324 | return ret; |
| 325 | #endif |
| 326 | #ifdef CONFIG_LEDS_GPIO_OF |
| 327 | ret = of_register_platform_driver(&of_gpio_leds_driver); |
| 328 | #endif |
| 329 | #ifdef CONFIG_LEDS_GPIO_PLATFORM |
| 330 | if (ret) |
| 331 | platform_driver_unregister(&gpio_led_driver); |
| 332 | #endif |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static void __exit gpio_led_exit(void) |
| 338 | { |
| 339 | #ifdef CONFIG_LEDS_GPIO_PLATFORM |
| 340 | platform_driver_unregister(&gpio_led_driver); |
| 341 | #endif |
| 342 | #ifdef CONFIG_LEDS_GPIO_OF |
| 343 | of_unregister_platform_driver(&of_gpio_leds_driver); |
| 344 | #endif |
| 345 | } |
| 346 | |
| 347 | module_init(gpio_led_init); |
| 348 | module_exit(gpio_led_exit); |
| 349 | |
Trent Piepho | a7d878a | 2009-01-10 17:26:01 +0000 | [diff] [blame] | 350 | MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>"); |
Raphael Assenat | 22e03f3 | 2007-02-27 19:49:53 +0000 | [diff] [blame] | 351 | MODULE_DESCRIPTION("GPIO LED driver"); |
| 352 | MODULE_LICENSE("GPL"); |