blob: 2e3df08b649b6c2a3a2dd83b62233bc3505a1459 [file] [log] [blame]
Raphael Assenat22e03f32007-02-27 19:49:53 +00001/*
2 * LEDs driver for GPIOs
3 *
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/platform_device.h>
15#include <linux/leds.h>
David Brownell00852272007-05-10 10:51:41 +010016#include <linux/workqueue.h>
17
Raphael Assenat22e03f32007-02-27 19:49:53 +000018#include <asm/gpio.h>
19
20struct gpio_led_data {
21 struct led_classdev cdev;
22 unsigned gpio;
David Brownell00852272007-05-10 10:51:41 +010023 struct work_struct work;
24 u8 new_level;
25 u8 can_sleep;
Raphael Assenat22e03f32007-02-27 19:49:53 +000026 u8 active_low;
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000027 int (*platform_gpio_blink_set)(unsigned gpio,
28 unsigned long *delay_on, unsigned long *delay_off);
Raphael Assenat22e03f32007-02-27 19:49:53 +000029};
30
David Brownell00852272007-05-10 10:51:41 +010031static void gpio_led_work(struct work_struct *work)
32{
33 struct gpio_led_data *led_dat =
34 container_of(work, struct gpio_led_data, work);
35
36 gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
37}
Raphael Assenat22e03f32007-02-27 19:49:53 +000038
39static void gpio_led_set(struct led_classdev *led_cdev,
40 enum led_brightness value)
41{
42 struct gpio_led_data *led_dat =
43 container_of(led_cdev, struct gpio_led_data, cdev);
44 int level;
45
46 if (value == LED_OFF)
47 level = 0;
48 else
49 level = 1;
50
51 if (led_dat->active_low)
52 level = !level;
53
David Brownell306dd852008-03-27 00:59:02 +000054 /* Setting GPIOs with I2C/etc requires a task context, and we don't
55 * seem to have a reliable way to know if we're already in one; so
56 * let's just assume the worst.
57 */
David Brownell00852272007-05-10 10:51:41 +010058 if (led_dat->can_sleep) {
David Brownell306dd852008-03-27 00:59:02 +000059 led_dat->new_level = level;
60 schedule_work(&led_dat->work);
David Brownell00852272007-05-10 10:51:41 +010061 } else
62 gpio_set_value(led_dat->gpio, level);
Raphael Assenat22e03f32007-02-27 19:49:53 +000063}
64
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +000065static int gpio_blink_set(struct led_classdev *led_cdev,
66 unsigned long *delay_on, unsigned long *delay_off)
67{
68 struct gpio_led_data *led_dat =
69 container_of(led_cdev, struct gpio_led_data, cdev);
70
71 return led_dat->platform_gpio_blink_set(led_dat->gpio, delay_on, delay_off);
72}
73
David Brownell199fb212007-10-31 10:37:37 +010074static int gpio_led_probe(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +000075{
76 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
77 struct gpio_led *cur_led;
78 struct gpio_led_data *leds_data, *led_dat;
79 int i, ret = 0;
80
81 if (!pdata)
82 return -EBUSY;
83
84 leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,
85 GFP_KERNEL);
86 if (!leds_data)
87 return -ENOMEM;
88
89 for (i = 0; i < pdata->num_leds; i++) {
90 cur_led = &pdata->leds[i];
91 led_dat = &leds_data[i];
92
Uwe Kleine-Königd95cbe62008-03-09 20:42:27 +000093 ret = gpio_request(cur_led->gpio, cur_led->name);
94 if (ret < 0)
95 goto err;
96
Raphael Assenat22e03f32007-02-27 19:49:53 +000097 led_dat->cdev.name = cur_led->name;
98 led_dat->cdev.default_trigger = cur_led->default_trigger;
99 led_dat->gpio = cur_led->gpio;
David Brownell00852272007-05-10 10:51:41 +0100100 led_dat->can_sleep = gpio_cansleep(cur_led->gpio);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000101 led_dat->active_low = cur_led->active_low;
Herbert Valerio Riedelca3259b2008-03-09 23:48:25 +0000102 if (pdata->gpio_blink_set) {
103 led_dat->platform_gpio_blink_set = pdata->gpio_blink_set;
104 led_dat->cdev.blink_set = gpio_blink_set;
105 }
Raphael Assenat22e03f32007-02-27 19:49:53 +0000106 led_dat->cdev.brightness_set = gpio_led_set;
Raphael Assenat2b7f1b82008-01-13 22:55:18 +0000107 led_dat->cdev.brightness = LED_OFF;
Richard Purdie859cb7f2009-01-08 17:55:03 +0000108 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
Raphael Assenat22e03f32007-02-27 19:49:53 +0000109
Raphael Assenat22e03f32007-02-27 19:49:53 +0000110 gpio_direction_output(led_dat->gpio, led_dat->active_low);
111
David Brownell199fb212007-10-31 10:37:37 +0100112 INIT_WORK(&led_dat->work, gpio_led_work);
113
Raphael Assenat22e03f32007-02-27 19:49:53 +0000114 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
115 if (ret < 0) {
116 gpio_free(led_dat->gpio);
117 goto err;
118 }
119 }
120
121 platform_set_drvdata(pdev, leds_data);
122
123 return 0;
124
125err:
126 if (i > 0) {
127 for (i = i - 1; i >= 0; i--) {
128 led_classdev_unregister(&leds_data[i].cdev);
David Brownell199fb212007-10-31 10:37:37 +0100129 cancel_work_sync(&leds_data[i].work);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000130 gpio_free(leds_data[i].gpio);
131 }
132 }
David Brownell00852272007-05-10 10:51:41 +0100133
Raphael Assenat22e03f32007-02-27 19:49:53 +0000134 kfree(leds_data);
135
136 return ret;
137}
138
David Brownell199fb212007-10-31 10:37:37 +0100139static int __devexit gpio_led_remove(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000140{
141 int i;
142 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
143 struct gpio_led_data *leds_data;
144
145 leds_data = platform_get_drvdata(pdev);
146
147 for (i = 0; i < pdata->num_leds; i++) {
148 led_classdev_unregister(&leds_data[i].cdev);
David Brownell199fb212007-10-31 10:37:37 +0100149 cancel_work_sync(&leds_data[i].work);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000150 gpio_free(leds_data[i].gpio);
151 }
David Brownell199fb212007-10-31 10:37:37 +0100152
Raphael Assenat22e03f32007-02-27 19:49:53 +0000153 kfree(leds_data);
154
155 return 0;
156}
157
Raphael Assenat22e03f32007-02-27 19:49:53 +0000158static struct platform_driver gpio_led_driver = {
David Brownell199fb212007-10-31 10:37:37 +0100159 .probe = gpio_led_probe,
160 .remove = __devexit_p(gpio_led_remove),
Raphael Assenat22e03f32007-02-27 19:49:53 +0000161 .driver = {
162 .name = "leds-gpio",
163 .owner = THIS_MODULE,
164 },
165};
166
167static int __init gpio_led_init(void)
168{
David Brownell199fb212007-10-31 10:37:37 +0100169 return platform_driver_register(&gpio_led_driver);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000170}
171
172static void __exit gpio_led_exit(void)
173{
174 platform_driver_unregister(&gpio_led_driver);
175}
176
177module_init(gpio_led_init);
178module_exit(gpio_led_exit);
179
180MODULE_AUTHOR("Raphael Assenat <raph@8d.com>");
181MODULE_DESCRIPTION("GPIO LED driver");
182MODULE_LICENSE("GPL");
Kay Sievers3c4ded92008-04-15 14:34:30 -0700183MODULE_ALIAS("platform:leds-gpio");