RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / leds / leds-ixp4xx-gpio.c
blob7dcf0b92c4603efe61c7d48c864d17351988360a
1 /*
2 * IXP4XX GPIO driver LED driver
4 * Author: John Bowler <jbowler@acm.org>
6 * Copyright (c) 2006 John Bowler
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:
18 * The above copyright notice and this permission notice
19 * shall be included in all copies or substantial portions
20 * of the Software.
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.
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/platform_device.h>
37 #include <linux/spinlock.h>
38 #include <linux/leds.h>
39 #include <asm/arch/hardware.h>
41 extern spinlock_t gpio_lock;
43 /* Up to 16 gpio lines are possible. */
44 #define GPIO_MAX 16
45 static struct ixp4xxgpioled_device {
46 struct led_classdev ancestor;
47 int flags;
48 } ixp4xxgpioled_devices[GPIO_MAX];
50 void ixp4xxgpioled_brightness_set(struct led_classdev *pled,
51 enum led_brightness value)
53 const struct ixp4xxgpioled_device *const ixp4xx_dev =
54 container_of(pled, struct ixp4xxgpioled_device, ancestor);
55 const u32 gpio_pin = ixp4xx_dev - ixp4xxgpioled_devices;
57 if (gpio_pin < GPIO_MAX && ixp4xx_dev->ancestor.name != 0) {
58 /* Set or clear the 'gpio_pin' bit according to the style
59 * and the required setting (value > 0 == on)
61 const int gpio_value =
62 (value > 0) == (ixp4xx_dev->flags != IXP4XX_GPIO_LOW) ?
63 IXP4XX_GPIO_HIGH : IXP4XX_GPIO_LOW;
66 unsigned long flags;
67 spin_lock_irqsave(&gpio_lock, flags);
68 gpio_line_set(gpio_pin, gpio_value);
69 spin_unlock_irqrestore(&gpio_lock, flags);
74 /* LEDs are described in resources, the following iterates over the valid
75 * LED resources.
77 #define for_all_leds(i, pdev) \
78 for (i=0; i<pdev->num_resources; ++i) \
79 if (pdev->resource[i].start < GPIO_MAX && \
80 pdev->resource[i].name != 0)
82 /* The following applies 'operation' to each LED from the given platform,
83 * the function always returns 0 to allow tail call elimination.
85 static int apply_to_all_leds(struct platform_device *pdev,
86 void (*operation)(struct led_classdev *pled))
88 int i;
90 for_all_leds(i, pdev)
91 operation(&ixp4xxgpioled_devices[pdev->resource[i].start].ancestor);
92 return 0;
95 #ifdef CONFIG_PM
96 static int ixp4xxgpioled_suspend(struct platform_device *pdev,
97 pm_message_t state)
99 return apply_to_all_leds(pdev, led_classdev_suspend);
102 static int ixp4xxgpioled_resume(struct platform_device *pdev)
104 return apply_to_all_leds(pdev, led_classdev_resume);
106 #endif
108 static void ixp4xxgpioled_remove_one_led(struct led_classdev *pled)
110 led_classdev_unregister(pled);
111 pled->name = 0;
114 static int ixp4xxgpioled_remove(struct platform_device *pdev)
116 return apply_to_all_leds(pdev, ixp4xxgpioled_remove_one_led);
119 static int ixp4xxgpioled_probe(struct platform_device *pdev)
121 /* The board level has to tell the driver where the
122 * LEDs are connected - there is no way to find out
123 * electrically. It must also say whether the GPIO
124 * lines are active high or active low.
126 * To do this read the num_resources (the number of
127 * LEDs) and the struct resource (the data for each
128 * LED). The name comes from the resource, and it
129 * isn't copied.
131 int i;
133 for_all_leds(i, pdev) {
134 const u8 gpio_pin = pdev->resource[i].start;
135 int rc;
137 if (ixp4xxgpioled_devices[gpio_pin].ancestor.name == 0) {
138 unsigned long flags;
140 spin_lock_irqsave(&gpio_lock, flags);
141 gpio_line_config(gpio_pin, IXP4XX_GPIO_OUT);
142 /* The config can, apparently, reset the state,
143 * I suspect the gpio line may be an input and
144 * the config may cause the line to be latched,
145 * so the setting depends on how the LED is
146 * connected to the line (which affects how it
147 * floats if not driven).
149 gpio_line_set(gpio_pin, IXP4XX_GPIO_HIGH);
150 spin_unlock_irqrestore(&gpio_lock, flags);
152 ixp4xxgpioled_devices[gpio_pin].flags =
153 pdev->resource[i].flags & IORESOURCE_BITS;
155 ixp4xxgpioled_devices[gpio_pin].ancestor.name =
156 pdev->resource[i].name;
158 /* This is how a board manufacturer makes the LED
159 * come on on reset - the GPIO line will be high, so
160 * make the LED light when the line is low...
162 if (ixp4xxgpioled_devices[gpio_pin].flags != IXP4XX_GPIO_LOW)
163 ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 100;
164 else
165 ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 0;
167 ixp4xxgpioled_devices[gpio_pin].ancestor.flags = 0;
169 ixp4xxgpioled_devices[gpio_pin].ancestor.brightness_set =
170 ixp4xxgpioled_brightness_set;
172 ixp4xxgpioled_devices[gpio_pin].ancestor.default_trigger = 0;
175 rc = led_classdev_register(&pdev->dev,
176 &ixp4xxgpioled_devices[gpio_pin].ancestor);
177 if (rc < 0) {
178 ixp4xxgpioled_devices[gpio_pin].ancestor.name = 0;
179 ixp4xxgpioled_remove(pdev);
180 return rc;
184 return 0;
187 static struct platform_driver ixp4xxgpioled_driver = {
188 .probe = ixp4xxgpioled_probe,
189 .remove = ixp4xxgpioled_remove,
190 #ifdef CONFIG_PM
191 .suspend = ixp4xxgpioled_suspend,
192 .resume = ixp4xxgpioled_resume,
193 #endif
194 .driver = {
195 .name = "IXP4XX-GPIO-LED",
199 static int __init ixp4xxgpioled_init(void)
201 return platform_driver_register(&ixp4xxgpioled_driver);
204 static void __exit ixp4xxgpioled_exit(void)
206 platform_driver_unregister(&ixp4xxgpioled_driver);
209 module_init(ixp4xxgpioled_init);
210 module_exit(ixp4xxgpioled_exit);
212 MODULE_AUTHOR("John Bowler <jbowler@acm.org>");
213 MODULE_DESCRIPTION("IXP4XX GPIO LED driver");
214 MODULE_LICENSE("Dual MIT/GPL");