mlx4: use bitmap_find_next_zero_area
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / timbgpio.c
bloba4d344ba8e5cd10868995f9edd6c3c98e4a09080
1 /*
2 * timbgpio.c timberdale FPGA GPIO driver
3 * Copyright (c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Supports:
20 * Timberdale FPGA GPIO
23 #include <linux/module.h>
24 #include <linux/gpio.h>
25 #include <linux/platform_device.h>
26 #include <linux/io.h>
27 #include <linux/timb_gpio.h>
28 #include <linux/interrupt.h>
30 #define DRIVER_NAME "timb-gpio"
32 #define TGPIOVAL 0x00
33 #define TGPIODIR 0x04
34 #define TGPIO_IER 0x08
35 #define TGPIO_ISR 0x0c
36 #define TGPIO_IPR 0x10
37 #define TGPIO_ICR 0x14
38 #define TGPIO_FLR 0x18
39 #define TGPIO_LVR 0x1c
41 struct timbgpio {
42 void __iomem *membase;
43 spinlock_t lock; /* mutual exclusion */
44 struct gpio_chip gpio;
45 int irq_base;
48 static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index,
49 unsigned offset, bool enabled)
51 struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
52 u32 reg;
54 spin_lock(&tgpio->lock);
55 reg = ioread32(tgpio->membase + offset);
57 if (enabled)
58 reg |= (1 << index);
59 else
60 reg &= ~(1 << index);
62 iowrite32(reg, tgpio->membase + offset);
63 spin_unlock(&tgpio->lock);
65 return 0;
68 static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
70 return timbgpio_update_bit(gpio, nr, TGPIODIR, true);
73 static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
75 struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
76 u32 value;
78 value = ioread32(tgpio->membase + TGPIOVAL);
79 return (value & (1 << nr)) ? 1 : 0;
82 static int timbgpio_gpio_direction_output(struct gpio_chip *gpio,
83 unsigned nr, int val)
85 return timbgpio_update_bit(gpio, nr, TGPIODIR, false);
88 static void timbgpio_gpio_set(struct gpio_chip *gpio,
89 unsigned nr, int val)
91 timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0);
94 static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset)
96 struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
98 if (tgpio->irq_base <= 0)
99 return -EINVAL;
101 return tgpio->irq_base + offset;
105 * GPIO IRQ
107 static void timbgpio_irq_disable(unsigned irq)
109 struct timbgpio *tgpio = get_irq_chip_data(irq);
110 int offset = irq - tgpio->irq_base;
112 timbgpio_update_bit(&tgpio->gpio, offset, TGPIO_IER, 0);
115 static void timbgpio_irq_enable(unsigned irq)
117 struct timbgpio *tgpio = get_irq_chip_data(irq);
118 int offset = irq - tgpio->irq_base;
120 timbgpio_update_bit(&tgpio->gpio, offset, TGPIO_IER, 1);
123 static int timbgpio_irq_type(unsigned irq, unsigned trigger)
125 struct timbgpio *tgpio = get_irq_chip_data(irq);
126 int offset = irq - tgpio->irq_base;
127 unsigned long flags;
128 u32 lvr, flr;
130 if (offset < 0 || offset > tgpio->gpio.ngpio)
131 return -EINVAL;
133 spin_lock_irqsave(&tgpio->lock, flags);
135 lvr = ioread32(tgpio->membase + TGPIO_LVR);
136 flr = ioread32(tgpio->membase + TGPIO_FLR);
138 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
139 flr &= ~(1 << offset);
140 if (trigger & IRQ_TYPE_LEVEL_HIGH)
141 lvr |= 1 << offset;
142 else
143 lvr &= ~(1 << offset);
146 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
147 return -EINVAL;
148 else {
149 flr |= 1 << offset;
150 /* opposite compared to the datasheet, but it mirrors the
151 * reality
153 if (trigger & IRQ_TYPE_EDGE_FALLING)
154 lvr |= 1 << offset;
155 else
156 lvr &= ~(1 << offset);
159 iowrite32(lvr, tgpio->membase + TGPIO_LVR);
160 iowrite32(flr, tgpio->membase + TGPIO_FLR);
161 iowrite32(1 << offset, tgpio->membase + TGPIO_ICR);
162 spin_unlock_irqrestore(&tgpio->lock, flags);
164 return 0;
167 static void timbgpio_irq(unsigned int irq, struct irq_desc *desc)
169 struct timbgpio *tgpio = get_irq_data(irq);
170 unsigned long ipr;
171 int offset;
173 desc->chip->ack(irq);
174 ipr = ioread32(tgpio->membase + TGPIO_IPR);
175 iowrite32(ipr, tgpio->membase + TGPIO_ICR);
177 for_each_bit(offset, &ipr, tgpio->gpio.ngpio)
178 generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset));
181 static struct irq_chip timbgpio_irqchip = {
182 .name = "GPIO",
183 .enable = timbgpio_irq_enable,
184 .disable = timbgpio_irq_disable,
185 .set_type = timbgpio_irq_type,
188 static int __devinit timbgpio_probe(struct platform_device *pdev)
190 int err, i;
191 struct gpio_chip *gc;
192 struct timbgpio *tgpio;
193 struct resource *iomem;
194 struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
195 int irq = platform_get_irq(pdev, 0);
197 if (!pdata || pdata->nr_pins > 32) {
198 err = -EINVAL;
199 goto err_mem;
202 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 if (!iomem) {
204 err = -EINVAL;
205 goto err_mem;
208 tgpio = kzalloc(sizeof(*tgpio), GFP_KERNEL);
209 if (!tgpio) {
210 err = -EINVAL;
211 goto err_mem;
213 tgpio->irq_base = pdata->irq_base;
215 spin_lock_init(&tgpio->lock);
217 if (!request_mem_region(iomem->start, resource_size(iomem),
218 DRIVER_NAME)) {
219 err = -EBUSY;
220 goto err_request;
223 tgpio->membase = ioremap(iomem->start, resource_size(iomem));
224 if (!tgpio->membase) {
225 err = -ENOMEM;
226 goto err_ioremap;
229 gc = &tgpio->gpio;
231 gc->label = dev_name(&pdev->dev);
232 gc->owner = THIS_MODULE;
233 gc->dev = &pdev->dev;
234 gc->direction_input = timbgpio_gpio_direction_input;
235 gc->get = timbgpio_gpio_get;
236 gc->direction_output = timbgpio_gpio_direction_output;
237 gc->set = timbgpio_gpio_set;
238 gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
239 gc->dbg_show = NULL;
240 gc->base = pdata->gpio_base;
241 gc->ngpio = pdata->nr_pins;
242 gc->can_sleep = 0;
244 err = gpiochip_add(gc);
245 if (err)
246 goto err_chipadd;
248 platform_set_drvdata(pdev, tgpio);
250 /* make sure to disable interrupts */
251 iowrite32(0x0, tgpio->membase + TGPIO_IER);
253 if (irq < 0 || tgpio->irq_base <= 0)
254 return 0;
256 for (i = 0; i < pdata->nr_pins; i++) {
257 set_irq_chip_and_handler_name(tgpio->irq_base + i,
258 &timbgpio_irqchip, handle_simple_irq, "mux");
259 set_irq_chip_data(tgpio->irq_base + i, tgpio);
260 #ifdef CONFIG_ARM
261 set_irq_flags(tgpio->irq_base + i, IRQF_VALID | IRQF_PROBE);
262 #endif
265 set_irq_data(irq, tgpio);
266 set_irq_chained_handler(irq, timbgpio_irq);
268 return 0;
270 err_chipadd:
271 iounmap(tgpio->membase);
272 err_ioremap:
273 release_mem_region(iomem->start, resource_size(iomem));
274 err_request:
275 kfree(tgpio);
276 err_mem:
277 printk(KERN_ERR DRIVER_NAME": Failed to register GPIOs: %d\n", err);
279 return err;
282 static int __devexit timbgpio_remove(struct platform_device *pdev)
284 int err;
285 struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
286 struct timbgpio *tgpio = platform_get_drvdata(pdev);
287 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
288 int irq = platform_get_irq(pdev, 0);
290 if (irq >= 0 && tgpio->irq_base > 0) {
291 int i;
292 for (i = 0; i < pdata->nr_pins; i++) {
293 set_irq_chip(tgpio->irq_base + i, NULL);
294 set_irq_chip_data(tgpio->irq_base + i, NULL);
297 set_irq_handler(irq, NULL);
298 set_irq_data(irq, NULL);
301 err = gpiochip_remove(&tgpio->gpio);
302 if (err)
303 printk(KERN_ERR DRIVER_NAME": failed to remove gpio_chip\n");
305 iounmap(tgpio->membase);
306 release_mem_region(iomem->start, resource_size(iomem));
307 kfree(tgpio);
309 platform_set_drvdata(pdev, NULL);
311 return 0;
314 static struct platform_driver timbgpio_platform_driver = {
315 .driver = {
316 .name = DRIVER_NAME,
317 .owner = THIS_MODULE,
319 .probe = timbgpio_probe,
320 .remove = timbgpio_remove,
323 /*--------------------------------------------------------------------------*/
325 static int __init timbgpio_init(void)
327 return platform_driver_register(&timbgpio_platform_driver);
330 static void __exit timbgpio_exit(void)
332 platform_driver_unregister(&timbgpio_platform_driver);
335 module_init(timbgpio_init);
336 module_exit(timbgpio_exit);
338 MODULE_DESCRIPTION("Timberdale GPIO driver");
339 MODULE_LICENSE("GPL v2");
340 MODULE_AUTHOR("Mocean Laboratories");
341 MODULE_ALIAS("platform:"DRIVER_NAME);