[PATCH] m68k: Add the generic dma API functions
[linux-2.6/linux-loongson.git] / drivers / leds / leds-ixp4xx-gpio.c
blob30ced150e4cfb8d2ea687a9f9487d11d690049e8
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/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>
42 extern spinlock_t gpio_lock;
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];
51 void ixp4xxgpioled_brightness_set(struct led_classdev *pled,
52 enum led_brightness value)
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;
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)
62 const int gpio_value =
63 (value > 0) == (ixp4xx_dev->flags != IXP4XX_GPIO_LOW) ?
64 IXP4XX_GPIO_HIGH : IXP4XX_GPIO_LOW;
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);
75 /* LEDs are described in resources, the following iterates over the valid
76 * LED resources.
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)
83 /* The following applies 'operation' to each LED from the given platform,
84 * the function always returns 0 to allow tail call elimination.
86 static int apply_to_all_leds(struct platform_device *pdev,
87 void (*operation)(struct led_classdev *pled))
89 int i;
91 for_all_leds(i, pdev)
92 operation(&ixp4xxgpioled_devices[pdev->resource[i].start].ancestor);
93 return 0;
96 #ifdef CONFIG_PM
97 static int ixp4xxgpioled_suspend(struct platform_device *pdev,
98 pm_message_t state)
100 return apply_to_all_leds(pdev, led_classdev_suspend);
103 static int ixp4xxgpioled_resume(struct platform_device *pdev)
105 return apply_to_all_leds(pdev, led_classdev_resume);
107 #endif
109 static void ixp4xxgpioled_remove_one_led(struct led_classdev *pled)
111 led_classdev_unregister(pled);
112 pled->name = 0;
115 static int ixp4xxgpioled_remove(struct platform_device *pdev)
117 return apply_to_all_leds(pdev, ixp4xxgpioled_remove_one_led);
120 static int ixp4xxgpioled_probe(struct platform_device *pdev)
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.
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.
132 int i;
134 for_all_leds(i, pdev) {
135 const u8 gpio_pin = pdev->resource[i].start;
136 int rc;
138 if (ixp4xxgpioled_devices[gpio_pin].ancestor.name == 0) {
139 unsigned long flags;
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).
150 gpio_line_set(gpio_pin, IXP4XX_GPIO_HIGH);
151 spin_unlock_irqrestore(&gpio_lock, flags);
153 ixp4xxgpioled_devices[gpio_pin].flags =
154 pdev->resource[i].flags & IORESOURCE_BITS;
156 ixp4xxgpioled_devices[gpio_pin].ancestor.name =
157 pdev->resource[i].name;
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...
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;
168 ixp4xxgpioled_devices[gpio_pin].ancestor.flags = 0;
170 ixp4xxgpioled_devices[gpio_pin].ancestor.brightness_set =
171 ixp4xxgpioled_brightness_set;
173 ixp4xxgpioled_devices[gpio_pin].ancestor.default_trigger = 0;
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;
185 return 0;
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",
200 static int __init ixp4xxgpioled_init(void)
202 return platform_driver_register(&ixp4xxgpioled_driver);
205 static void __exit ixp4xxgpioled_exit(void)
207 platform_driver_unregister(&ixp4xxgpioled_driver);
210 module_init(ixp4xxgpioled_init);
211 module_exit(ixp4xxgpioled_exit);
213 MODULE_AUTHOR("John Bowler <jbowler@acm.org>");
214 MODULE_DESCRIPTION("IXP4XX GPIO LED driver");
215 MODULE_LICENSE("Dual MIT/GPL");