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