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>
26 #include <linux/irq.h>
28 #include <linux/timb_gpio.h>
29 #include <linux/interrupt.h>
31 #define DRIVER_NAME "timb-gpio"
35 #define TGPIO_IER 0x08
36 #define TGPIO_ISR 0x0c
37 #define TGPIO_IPR 0x10
38 #define TGPIO_ICR 0x14
39 #define TGPIO_FLR 0x18
40 #define TGPIO_LVR 0x1c
41 #define TGPIO_VER 0x20
42 #define TGPIO_BFLR 0x24
45 void __iomem
*membase
;
46 spinlock_t lock
; /* mutual exclusion */
47 struct gpio_chip gpio
;
51 static int timbgpio_update_bit(struct gpio_chip
*gpio
, unsigned index
,
52 unsigned offset
, bool enabled
)
54 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
57 spin_lock(&tgpio
->lock
);
58 reg
= ioread32(tgpio
->membase
+ offset
);
65 iowrite32(reg
, tgpio
->membase
+ offset
);
66 spin_unlock(&tgpio
->lock
);
71 static int timbgpio_gpio_direction_input(struct gpio_chip
*gpio
, unsigned nr
)
73 return timbgpio_update_bit(gpio
, nr
, TGPIODIR
, true);
76 static int timbgpio_gpio_get(struct gpio_chip
*gpio
, unsigned nr
)
78 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
81 value
= ioread32(tgpio
->membase
+ TGPIOVAL
);
82 return (value
& (1 << nr
)) ? 1 : 0;
85 static int timbgpio_gpio_direction_output(struct gpio_chip
*gpio
,
88 return timbgpio_update_bit(gpio
, nr
, TGPIODIR
, false);
91 static void timbgpio_gpio_set(struct gpio_chip
*gpio
,
94 timbgpio_update_bit(gpio
, nr
, TGPIOVAL
, val
!= 0);
97 static int timbgpio_to_irq(struct gpio_chip
*gpio
, unsigned offset
)
99 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
101 if (tgpio
->irq_base
<= 0)
104 return tgpio
->irq_base
+ offset
;
110 static void timbgpio_irq_disable(unsigned irq
)
112 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
113 int offset
= irq
- tgpio
->irq_base
;
115 timbgpio_update_bit(&tgpio
->gpio
, offset
, TGPIO_IER
, 0);
118 static void timbgpio_irq_enable(unsigned irq
)
120 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
121 int offset
= irq
- tgpio
->irq_base
;
123 timbgpio_update_bit(&tgpio
->gpio
, offset
, TGPIO_IER
, 1);
126 static int timbgpio_irq_type(unsigned irq
, unsigned trigger
)
128 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
129 int offset
= irq
- tgpio
->irq_base
;
131 u32 lvr
, flr
, bflr
= 0;
134 if (offset
< 0 || offset
> tgpio
->gpio
.ngpio
)
137 ver
= ioread32(tgpio
->membase
+ TGPIO_VER
);
139 spin_lock_irqsave(&tgpio
->lock
, flags
);
141 lvr
= ioread32(tgpio
->membase
+ TGPIO_LVR
);
142 flr
= ioread32(tgpio
->membase
+ TGPIO_FLR
);
144 bflr
= ioread32(tgpio
->membase
+ TGPIO_BFLR
);
146 if (trigger
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
)) {
147 bflr
&= ~(1 << offset
);
148 flr
&= ~(1 << offset
);
149 if (trigger
& IRQ_TYPE_LEVEL_HIGH
)
152 lvr
&= ~(1 << offset
);
155 if ((trigger
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
163 bflr
&= ~(1 << offset
);
165 if (trigger
& IRQ_TYPE_EDGE_FALLING
)
166 lvr
&= ~(1 << offset
);
171 iowrite32(lvr
, tgpio
->membase
+ TGPIO_LVR
);
172 iowrite32(flr
, tgpio
->membase
+ TGPIO_FLR
);
174 iowrite32(bflr
, tgpio
->membase
+ TGPIO_BFLR
);
176 iowrite32(1 << offset
, tgpio
->membase
+ TGPIO_ICR
);
177 spin_unlock_irqrestore(&tgpio
->lock
, flags
);
182 static void timbgpio_irq(unsigned int irq
, struct irq_desc
*desc
)
184 struct timbgpio
*tgpio
= get_irq_data(irq
);
188 desc
->chip
->ack(irq
);
189 ipr
= ioread32(tgpio
->membase
+ TGPIO_IPR
);
190 iowrite32(ipr
, tgpio
->membase
+ TGPIO_ICR
);
192 for_each_set_bit(offset
, &ipr
, tgpio
->gpio
.ngpio
)
193 generic_handle_irq(timbgpio_to_irq(&tgpio
->gpio
, offset
));
196 static struct irq_chip timbgpio_irqchip
= {
198 .enable
= timbgpio_irq_enable
,
199 .disable
= timbgpio_irq_disable
,
200 .set_type
= timbgpio_irq_type
,
203 static int __devinit
timbgpio_probe(struct platform_device
*pdev
)
206 struct gpio_chip
*gc
;
207 struct timbgpio
*tgpio
;
208 struct resource
*iomem
;
209 struct timbgpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
210 int irq
= platform_get_irq(pdev
, 0);
212 if (!pdata
|| pdata
->nr_pins
> 32) {
217 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
223 tgpio
= kzalloc(sizeof(*tgpio
), GFP_KERNEL
);
228 tgpio
->irq_base
= pdata
->irq_base
;
230 spin_lock_init(&tgpio
->lock
);
232 if (!request_mem_region(iomem
->start
, resource_size(iomem
),
238 tgpio
->membase
= ioremap(iomem
->start
, resource_size(iomem
));
239 if (!tgpio
->membase
) {
246 gc
->label
= dev_name(&pdev
->dev
);
247 gc
->owner
= THIS_MODULE
;
248 gc
->dev
= &pdev
->dev
;
249 gc
->direction_input
= timbgpio_gpio_direction_input
;
250 gc
->get
= timbgpio_gpio_get
;
251 gc
->direction_output
= timbgpio_gpio_direction_output
;
252 gc
->set
= timbgpio_gpio_set
;
253 gc
->to_irq
= (irq
>= 0 && tgpio
->irq_base
> 0) ? timbgpio_to_irq
: NULL
;
255 gc
->base
= pdata
->gpio_base
;
256 gc
->ngpio
= pdata
->nr_pins
;
259 err
= gpiochip_add(gc
);
263 platform_set_drvdata(pdev
, tgpio
);
265 /* make sure to disable interrupts */
266 iowrite32(0x0, tgpio
->membase
+ TGPIO_IER
);
268 if (irq
< 0 || tgpio
->irq_base
<= 0)
271 for (i
= 0; i
< pdata
->nr_pins
; i
++) {
272 set_irq_chip_and_handler_name(tgpio
->irq_base
+ i
,
273 &timbgpio_irqchip
, handle_simple_irq
, "mux");
274 set_irq_chip_data(tgpio
->irq_base
+ i
, tgpio
);
276 set_irq_flags(tgpio
->irq_base
+ i
, IRQF_VALID
| IRQF_PROBE
);
280 set_irq_data(irq
, tgpio
);
281 set_irq_chained_handler(irq
, timbgpio_irq
);
286 iounmap(tgpio
->membase
);
288 release_mem_region(iomem
->start
, resource_size(iomem
));
292 printk(KERN_ERR DRIVER_NAME
": Failed to register GPIOs: %d\n", err
);
297 static int __devexit
timbgpio_remove(struct platform_device
*pdev
)
300 struct timbgpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
301 struct timbgpio
*tgpio
= platform_get_drvdata(pdev
);
302 struct resource
*iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
303 int irq
= platform_get_irq(pdev
, 0);
305 if (irq
>= 0 && tgpio
->irq_base
> 0) {
307 for (i
= 0; i
< pdata
->nr_pins
; i
++) {
308 set_irq_chip(tgpio
->irq_base
+ i
, NULL
);
309 set_irq_chip_data(tgpio
->irq_base
+ i
, NULL
);
312 set_irq_handler(irq
, NULL
);
313 set_irq_data(irq
, NULL
);
316 err
= gpiochip_remove(&tgpio
->gpio
);
318 printk(KERN_ERR DRIVER_NAME
": failed to remove gpio_chip\n");
320 iounmap(tgpio
->membase
);
321 release_mem_region(iomem
->start
, resource_size(iomem
));
324 platform_set_drvdata(pdev
, NULL
);
329 static struct platform_driver timbgpio_platform_driver
= {
332 .owner
= THIS_MODULE
,
334 .probe
= timbgpio_probe
,
335 .remove
= timbgpio_remove
,
338 /*--------------------------------------------------------------------------*/
340 static int __init
timbgpio_init(void)
342 return platform_driver_register(&timbgpio_platform_driver
);
345 static void __exit
timbgpio_exit(void)
347 platform_driver_unregister(&timbgpio_platform_driver
);
350 module_init(timbgpio_init
);
351 module_exit(timbgpio_exit
);
353 MODULE_DESCRIPTION("Timberdale GPIO driver");
354 MODULE_LICENSE("GPL v2");
355 MODULE_AUTHOR("Mocean Laboratories");
356 MODULE_ALIAS("platform:"DRIVER_NAME
);