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