diff options
Diffstat (limited to 'drivers/leds/leds-ixp4xx-gpio.c')
-rw-r--r-- | drivers/leds/leds-ixp4xx-gpio.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/drivers/leds/leds-ixp4xx-gpio.c b/drivers/leds/leds-ixp4xx-gpio.c new file mode 100644 index 000000000000..30ced150e4cf --- /dev/null +++ b/drivers/leds/leds-ixp4xx-gpio.c | |||
@@ -0,0 +1,215 @@ | |||
1 | /* | ||
2 | * IXP4XX GPIO driver LED driver | ||
3 | * | ||
4 | * Author: John Bowler <jbowler@acm.org> | ||
5 | * | ||
6 | * Copyright (c) 2006 John Bowler | ||
7 | * | ||
8 | * Permission is hereby granted, free of charge, to any | ||
9 | * person obtaining a copy of this software and associated | ||
10 | * documentation files (the "Software"), to deal in the | ||
11 | * Software without restriction, including without | ||
12 | * limitation the rights to use, copy, modify, merge, | ||
13 | * publish, distribute, sublicense, and/or sell copies of | ||
14 | * the Software, and to permit persons to whom the | ||
15 | * Software is furnished to do so, subject to the | ||
16 | * following conditions: | ||
17 | * | ||
18 | * The above copyright notice and this permission notice | ||
19 | * shall be included in all copies or substantial portions | ||
20 | * of the Software. | ||
21 | * | ||
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
23 | * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
24 | * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
25 | * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
26 | * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR | ||
27 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
30 | * OTHER DEALINGS IN THE SOFTWARE. | ||
31 | * | ||
32 | */ | ||
33 | |||
34 | #include <linux/config.h> | ||
35 | #include <linux/kernel.h> | ||
36 | #include <linux/init.h> | ||
37 | #include <linux/platform_device.h> | ||
38 | #include <linux/spinlock.h> | ||
39 | #include <linux/leds.h> | ||
40 | #include <asm/arch/hardware.h> | ||
41 | |||
42 | extern spinlock_t gpio_lock; | ||
43 | |||
44 | /* Up to 16 gpio lines are possible. */ | ||
45 | #define GPIO_MAX 16 | ||
46 | static struct ixp4xxgpioled_device { | ||
47 | struct led_classdev ancestor; | ||
48 | int flags; | ||
49 | } ixp4xxgpioled_devices[GPIO_MAX]; | ||
50 | |||
51 | void ixp4xxgpioled_brightness_set(struct led_classdev *pled, | ||
52 | enum led_brightness value) | ||
53 | { | ||
54 | const struct ixp4xxgpioled_device *const ixp4xx_dev = | ||
55 | container_of(pled, struct ixp4xxgpioled_device, ancestor); | ||
56 | const u32 gpio_pin = ixp4xx_dev - ixp4xxgpioled_devices; | ||
57 | |||
58 | if (gpio_pin < GPIO_MAX && ixp4xx_dev->ancestor.name != 0) { | ||
59 | /* Set or clear the 'gpio_pin' bit according to the style | ||
60 | * and the required setting (value > 0 == on) | ||
61 | */ | ||
62 | const int gpio_value = | ||
63 | (value > 0) == (ixp4xx_dev->flags != IXP4XX_GPIO_LOW) ? | ||
64 | IXP4XX_GPIO_HIGH : IXP4XX_GPIO_LOW; | ||
65 | |||
66 | { | ||
67 | unsigned long flags; | ||
68 | spin_lock_irqsave(&gpio_lock, flags); | ||
69 | gpio_line_set(gpio_pin, gpio_value); | ||
70 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | /* LEDs are described in resources, the following iterates over the valid | ||
76 | * LED resources. | ||
77 | */ | ||
78 | #define for_all_leds(i, pdev) \ | ||
79 | for (i=0; i<pdev->num_resources; ++i) \ | ||
80 | if (pdev->resource[i].start < GPIO_MAX && \ | ||
81 | pdev->resource[i].name != 0) | ||
82 | |||
83 | /* The following applies 'operation' to each LED from the given platform, | ||
84 | * the function always returns 0 to allow tail call elimination. | ||
85 | */ | ||
86 | static int apply_to_all_leds(struct platform_device *pdev, | ||
87 | void (*operation)(struct led_classdev *pled)) | ||
88 | { | ||
89 | int i; | ||
90 | |||
91 | for_all_leds(i, pdev) | ||
92 | operation(&ixp4xxgpioled_devices[pdev->resource[i].start].ancestor); | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | #ifdef CONFIG_PM | ||
97 | static int ixp4xxgpioled_suspend(struct platform_device *pdev, | ||
98 | pm_message_t state) | ||
99 | { | ||
100 | return apply_to_all_leds(pdev, led_classdev_suspend); | ||
101 | } | ||
102 | |||
103 | static int ixp4xxgpioled_resume(struct platform_device *pdev) | ||
104 | { | ||
105 | return apply_to_all_leds(pdev, led_classdev_resume); | ||
106 | } | ||
107 | #endif | ||
108 | |||
109 | static void ixp4xxgpioled_remove_one_led(struct led_classdev *pled) | ||
110 | { | ||
111 | led_classdev_unregister(pled); | ||
112 | pled->name = 0; | ||
113 | } | ||
114 | |||
115 | static int ixp4xxgpioled_remove(struct platform_device *pdev) | ||
116 | { | ||
117 | return apply_to_all_leds(pdev, ixp4xxgpioled_remove_one_led); | ||
118 | } | ||
119 | |||
120 | static int ixp4xxgpioled_probe(struct platform_device *pdev) | ||
121 | { | ||
122 | /* The board level has to tell the driver where the | ||
123 | * LEDs are connected - there is no way to find out | ||
124 | * electrically. It must also say whether the GPIO | ||
125 | * lines are active high or active low. | ||
126 | * | ||
127 | * To do this read the num_resources (the number of | ||
128 | * LEDs) and the struct resource (the data for each | ||
129 | * LED). The name comes from the resource, and it | ||
130 | * isn't copied. | ||
131 | */ | ||
132 | int i; | ||
133 | |||
134 | for_all_leds(i, pdev) { | ||
135 | const u8 gpio_pin = pdev->resource[i].start; | ||
136 | int rc; | ||
137 | |||
138 | if (ixp4xxgpioled_devices[gpio_pin].ancestor.name == 0) { | ||
139 | unsigned long flags; | ||
140 | |||
141 | spin_lock_irqsave(&gpio_lock, flags); | ||
142 | gpio_line_config(gpio_pin, IXP4XX_GPIO_OUT); | ||
143 | /* The config can, apparently, reset the state, | ||
144 | * I suspect the gpio line may be an input and | ||
145 | * the config may cause the line to be latched, | ||
146 | * so the setting depends on how the LED is | ||
147 | * connected to the line (which affects how it | ||
148 | * floats if not driven). | ||
149 | */ | ||
150 | gpio_line_set(gpio_pin, IXP4XX_GPIO_HIGH); | ||
151 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
152 | |||
153 | ixp4xxgpioled_devices[gpio_pin].flags = | ||
154 | pdev->resource[i].flags & IORESOURCE_BITS; | ||
155 | |||
156 | ixp4xxgpioled_devices[gpio_pin].ancestor.name = | ||
157 | pdev->resource[i].name; | ||
158 | |||
159 | /* This is how a board manufacturer makes the LED | ||
160 | * come on on reset - the GPIO line will be high, so | ||
161 | * make the LED light when the line is low... | ||
162 | */ | ||
163 | if (ixp4xxgpioled_devices[gpio_pin].flags != IXP4XX_GPIO_LOW) | ||
164 | ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 100; | ||
165 | else | ||
166 | ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 0; | ||
167 | |||
168 | ixp4xxgpioled_devices[gpio_pin].ancestor.flags = 0; | ||
169 | |||
170 | ixp4xxgpioled_devices[gpio_pin].ancestor.brightness_set = | ||
171 | ixp4xxgpioled_brightness_set; | ||
172 | |||
173 | ixp4xxgpioled_devices[gpio_pin].ancestor.default_trigger = 0; | ||
174 | } | ||
175 | |||
176 | rc = led_classdev_register(&pdev->dev, | ||
177 | &ixp4xxgpioled_devices[gpio_pin].ancestor); | ||
178 | if (rc < 0) { | ||
179 | ixp4xxgpioled_devices[gpio_pin].ancestor.name = 0; | ||
180 | ixp4xxgpioled_remove(pdev); | ||
181 | return rc; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | static struct platform_driver ixp4xxgpioled_driver = { | ||
189 | .probe = ixp4xxgpioled_probe, | ||
190 | .remove = ixp4xxgpioled_remove, | ||
191 | #ifdef CONFIG_PM | ||
192 | .suspend = ixp4xxgpioled_suspend, | ||
193 | .resume = ixp4xxgpioled_resume, | ||
194 | #endif | ||
195 | .driver = { | ||
196 | .name = "IXP4XX-GPIO-LED", | ||
197 | }, | ||
198 | }; | ||
199 | |||
200 | static int __init ixp4xxgpioled_init(void) | ||
201 | { | ||
202 | return platform_driver_register(&ixp4xxgpioled_driver); | ||
203 | } | ||
204 | |||
205 | static void __exit ixp4xxgpioled_exit(void) | ||
206 | { | ||
207 | platform_driver_unregister(&ixp4xxgpioled_driver); | ||
208 | } | ||
209 | |||
210 | module_init(ixp4xxgpioled_init); | ||
211 | module_exit(ixp4xxgpioled_exit); | ||
212 | |||
213 | MODULE_AUTHOR("John Bowler <jbowler@acm.org>"); | ||
214 | MODULE_DESCRIPTION("IXP4XX GPIO LED driver"); | ||
215 | MODULE_LICENSE("Dual MIT/GPL"); | ||