blob: 1aae8b3321349e53802a280d402a36ff1e82d2c3 [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;
27};
28
David Brownell00852272007-05-10 10:51:41 +010029static void gpio_led_work(struct work_struct *work)
30{
31 struct gpio_led_data *led_dat =
32 container_of(work, struct gpio_led_data, work);
33
34 gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
35}
Raphael Assenat22e03f32007-02-27 19:49:53 +000036
37static void gpio_led_set(struct led_classdev *led_cdev,
38 enum led_brightness value)
39{
40 struct gpio_led_data *led_dat =
41 container_of(led_cdev, struct gpio_led_data, cdev);
42 int level;
43
44 if (value == LED_OFF)
45 level = 0;
46 else
47 level = 1;
48
49 if (led_dat->active_low)
50 level = !level;
51
David Brownell306dd852008-03-27 00:59:02 +000052 /* Setting GPIOs with I2C/etc requires a task context, and we don't
53 * seem to have a reliable way to know if we're already in one; so
54 * let's just assume the worst.
55 */
David Brownell00852272007-05-10 10:51:41 +010056 if (led_dat->can_sleep) {
David Brownell306dd852008-03-27 00:59:02 +000057 led_dat->new_level = level;
58 schedule_work(&led_dat->work);
David Brownell00852272007-05-10 10:51:41 +010059 } else
60 gpio_set_value(led_dat->gpio, level);
Raphael Assenat22e03f32007-02-27 19:49:53 +000061}
62
David Brownell199fb212007-10-31 10:37:37 +010063static int gpio_led_probe(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +000064{
65 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
66 struct gpio_led *cur_led;
67 struct gpio_led_data *leds_data, *led_dat;
68 int i, ret = 0;
69
70 if (!pdata)
71 return -EBUSY;
72
73 leds_data = kzalloc(sizeof(struct gpio_led_data) * pdata->num_leds,
74 GFP_KERNEL);
75 if (!leds_data)
76 return -ENOMEM;
77
78 for (i = 0; i < pdata->num_leds; i++) {
79 cur_led = &pdata->leds[i];
80 led_dat = &leds_data[i];
81
Uwe Kleine-Königd95cbe62008-03-09 20:42:27 +000082 ret = gpio_request(cur_led->gpio, cur_led->name);
83 if (ret < 0)
84 goto err;
85
Raphael Assenat22e03f32007-02-27 19:49:53 +000086 led_dat->cdev.name = cur_led->name;
87 led_dat->cdev.default_trigger = cur_led->default_trigger;
88 led_dat->gpio = cur_led->gpio;
David Brownell00852272007-05-10 10:51:41 +010089 led_dat->can_sleep = gpio_cansleep(cur_led->gpio);
Raphael Assenat22e03f32007-02-27 19:49:53 +000090 led_dat->active_low = cur_led->active_low;
91 led_dat->cdev.brightness_set = gpio_led_set;
Raphael Assenat2b7f1b82008-01-13 22:55:18 +000092 led_dat->cdev.brightness = LED_OFF;
Raphael Assenat22e03f32007-02-27 19:49:53 +000093
Raphael Assenat22e03f32007-02-27 19:49:53 +000094 gpio_direction_output(led_dat->gpio, led_dat->active_low);
95
David Brownell199fb212007-10-31 10:37:37 +010096 INIT_WORK(&led_dat->work, gpio_led_work);
97
Raphael Assenat22e03f32007-02-27 19:49:53 +000098 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
99 if (ret < 0) {
100 gpio_free(led_dat->gpio);
101 goto err;
102 }
103 }
104
105 platform_set_drvdata(pdev, leds_data);
106
107 return 0;
108
109err:
110 if (i > 0) {
111 for (i = i - 1; i >= 0; i--) {
112 led_classdev_unregister(&leds_data[i].cdev);
David Brownell199fb212007-10-31 10:37:37 +0100113 cancel_work_sync(&leds_data[i].work);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000114 gpio_free(leds_data[i].gpio);
115 }
116 }
David Brownell00852272007-05-10 10:51:41 +0100117
Raphael Assenat22e03f32007-02-27 19:49:53 +0000118 kfree(leds_data);
119
120 return ret;
121}
122
David Brownell199fb212007-10-31 10:37:37 +0100123static int __devexit gpio_led_remove(struct platform_device *pdev)
Raphael Assenat22e03f32007-02-27 19:49:53 +0000124{
125 int i;
126 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
127 struct gpio_led_data *leds_data;
128
129 leds_data = platform_get_drvdata(pdev);
130
131 for (i = 0; i < pdata->num_leds; i++) {
132 led_classdev_unregister(&leds_data[i].cdev);
David Brownell199fb212007-10-31 10:37:37 +0100133 cancel_work_sync(&leds_data[i].work);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000134 gpio_free(leds_data[i].gpio);
135 }
David Brownell199fb212007-10-31 10:37:37 +0100136
Raphael Assenat22e03f32007-02-27 19:49:53 +0000137 kfree(leds_data);
138
139 return 0;
140}
141
142#ifdef CONFIG_PM
143static int gpio_led_suspend(struct platform_device *pdev, pm_message_t state)
144{
145 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
146 struct gpio_led_data *leds_data;
147 int i;
David Brownell199fb212007-10-31 10:37:37 +0100148
Raphael Assenat22e03f32007-02-27 19:49:53 +0000149 leds_data = platform_get_drvdata(pdev);
150
151 for (i = 0; i < pdata->num_leds; i++)
152 led_classdev_suspend(&leds_data[i].cdev);
153
154 return 0;
155}
156
157static int gpio_led_resume(struct platform_device *pdev)
158{
159 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
160 struct gpio_led_data *leds_data;
161 int i;
162
163 leds_data = platform_get_drvdata(pdev);
164
165 for (i = 0; i < pdata->num_leds; i++)
166 led_classdev_resume(&leds_data[i].cdev);
167
168 return 0;
169}
170#else
171#define gpio_led_suspend NULL
172#define gpio_led_resume NULL
173#endif
174
175static struct platform_driver gpio_led_driver = {
David Brownell199fb212007-10-31 10:37:37 +0100176 .probe = gpio_led_probe,
177 .remove = __devexit_p(gpio_led_remove),
Raphael Assenat22e03f32007-02-27 19:49:53 +0000178 .suspend = gpio_led_suspend,
179 .resume = gpio_led_resume,
180 .driver = {
181 .name = "leds-gpio",
182 .owner = THIS_MODULE,
183 },
184};
185
186static int __init gpio_led_init(void)
187{
David Brownell199fb212007-10-31 10:37:37 +0100188 return platform_driver_register(&gpio_led_driver);
Raphael Assenat22e03f32007-02-27 19:49:53 +0000189}
190
191static void __exit gpio_led_exit(void)
192{
193 platform_driver_unregister(&gpio_led_driver);
194}
195
196module_init(gpio_led_init);
197module_exit(gpio_led_exit);
198
199MODULE_AUTHOR("Raphael Assenat <raph@8d.com>");
200MODULE_DESCRIPTION("GPIO LED driver");
201MODULE_LICENSE("GPL");
Kay Sievers3c4ded92008-04-15 14:34:30 -0700202MODULE_ALIAS("platform:leds-gpio");