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.
20 * Timberdale FPGA GPIO
23 #include <linux/module.h>
24 #include <linux/gpio.h>
25 #include <linux/platform_device.h>
27 #include <linux/timb_gpio.h>
28 #include <linux/interrupt.h>
30 #define DRIVER_NAME "timb-gpio"
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
42 void __iomem
*membase
;
43 spinlock_t lock
; /* mutual exclusion */
44 struct gpio_chip gpio
;
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
);
54 spin_lock(&tgpio
->lock
);
55 reg
= ioread32(tgpio
->membase
+ offset
);
62 iowrite32(reg
, tgpio
->membase
+ offset
);
63 spin_unlock(&tgpio
->lock
);
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
);
78 value
= ioread32(tgpio
->membase
+ TGPIOVAL
);
79 return (value
& (1 << nr
)) ? 1 : 0;
82 static int timbgpio_gpio_direction_output(struct gpio_chip
*gpio
,
85 return timbgpio_update_bit(gpio
, nr
, TGPIODIR
, false);
88 static void timbgpio_gpio_set(struct gpio_chip
*gpio
,
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)
101 return tgpio
->irq_base
+ offset
;
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
;
130 if (offset
< 0 || offset
> tgpio
->gpio
.ngpio
)
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
)
143 lvr
&= ~(1 << offset
);
146 if ((trigger
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
)
150 /* opposite compared to the datasheet, but it mirrors the
153 if (trigger
& IRQ_TYPE_EDGE_FALLING
)
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
);
167 static void timbgpio_irq(unsigned int irq
, struct irq_desc
*desc
)
169 struct timbgpio
*tgpio
= get_irq_data(irq
);
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
= {
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
)
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) {
202 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
208 tgpio
= kzalloc(sizeof(*tgpio
), GFP_KERNEL
);
213 tgpio
->irq_base
= pdata
->irq_base
;
215 spin_lock_init(&tgpio
->lock
);
217 if (!request_mem_region(iomem
->start
, resource_size(iomem
),
223 tgpio
->membase
= ioremap(iomem
->start
, resource_size(iomem
));
224 if (!tgpio
->membase
) {
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
;
240 gc
->base
= pdata
->gpio_base
;
241 gc
->ngpio
= pdata
->nr_pins
;
244 err
= gpiochip_add(gc
);
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)
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
);
261 set_irq_flags(tgpio
->irq_base
+ i
, IRQF_VALID
| IRQF_PROBE
);
265 set_irq_data(irq
, tgpio
);
266 set_irq_chained_handler(irq
, timbgpio_irq
);
271 iounmap(tgpio
->membase
);
273 release_mem_region(iomem
->start
, resource_size(iomem
));
277 printk(KERN_ERR DRIVER_NAME
": Failed to register GPIOs: %d\n", err
);
282 static int __devexit
timbgpio_remove(struct platform_device
*pdev
)
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) {
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
);
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
));
309 platform_set_drvdata(pdev
, NULL
);
314 static struct platform_driver timbgpio_platform_driver
= {
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
);