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>
30 #include <linux/slab.h>
32 #define DRIVER_NAME "timb-gpio"
36 #define TGPIO_IER 0x08
37 #define TGPIO_ISR 0x0c
38 #define TGPIO_IPR 0x10
39 #define TGPIO_ICR 0x14
40 #define TGPIO_FLR 0x18
41 #define TGPIO_LVR 0x1c
42 #define TGPIO_VER 0x20
43 #define TGPIO_BFLR 0x24
46 void __iomem
*membase
;
47 spinlock_t lock
; /* mutual exclusion */
48 struct gpio_chip gpio
;
52 static int timbgpio_update_bit(struct gpio_chip
*gpio
, unsigned index
,
53 unsigned offset
, bool enabled
)
55 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
58 spin_lock(&tgpio
->lock
);
59 reg
= ioread32(tgpio
->membase
+ offset
);
66 iowrite32(reg
, tgpio
->membase
+ offset
);
67 spin_unlock(&tgpio
->lock
);
72 static int timbgpio_gpio_direction_input(struct gpio_chip
*gpio
, unsigned nr
)
74 return timbgpio_update_bit(gpio
, nr
, TGPIODIR
, true);
77 static int timbgpio_gpio_get(struct gpio_chip
*gpio
, unsigned nr
)
79 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
82 value
= ioread32(tgpio
->membase
+ TGPIOVAL
);
83 return (value
& (1 << nr
)) ? 1 : 0;
86 static int timbgpio_gpio_direction_output(struct gpio_chip
*gpio
,
89 return timbgpio_update_bit(gpio
, nr
, TGPIODIR
, false);
92 static void timbgpio_gpio_set(struct gpio_chip
*gpio
,
95 timbgpio_update_bit(gpio
, nr
, TGPIOVAL
, val
!= 0);
98 static int timbgpio_to_irq(struct gpio_chip
*gpio
, unsigned offset
)
100 struct timbgpio
*tgpio
= container_of(gpio
, struct timbgpio
, gpio
);
102 if (tgpio
->irq_base
<= 0)
105 return tgpio
->irq_base
+ offset
;
111 static void timbgpio_irq_disable(unsigned irq
)
113 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
114 int offset
= irq
- tgpio
->irq_base
;
116 timbgpio_update_bit(&tgpio
->gpio
, offset
, TGPIO_IER
, 0);
119 static void timbgpio_irq_enable(unsigned irq
)
121 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
122 int offset
= irq
- tgpio
->irq_base
;
124 timbgpio_update_bit(&tgpio
->gpio
, offset
, TGPIO_IER
, 1);
127 static int timbgpio_irq_type(unsigned irq
, unsigned trigger
)
129 struct timbgpio
*tgpio
= get_irq_chip_data(irq
);
130 int offset
= irq
- tgpio
->irq_base
;
132 u32 lvr
, flr
, bflr
= 0;
136 if (offset
< 0 || offset
> tgpio
->gpio
.ngpio
)
139 ver
= ioread32(tgpio
->membase
+ TGPIO_VER
);
141 spin_lock_irqsave(&tgpio
->lock
, flags
);
143 lvr
= ioread32(tgpio
->membase
+ TGPIO_LVR
);
144 flr
= ioread32(tgpio
->membase
+ TGPIO_FLR
);
146 bflr
= ioread32(tgpio
->membase
+ TGPIO_BFLR
);
148 if (trigger
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
)) {
149 bflr
&= ~(1 << offset
);
150 flr
&= ~(1 << offset
);
151 if (trigger
& IRQ_TYPE_LEVEL_HIGH
)
154 lvr
&= ~(1 << offset
);
157 if ((trigger
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
167 bflr
&= ~(1 << offset
);
169 if (trigger
& IRQ_TYPE_EDGE_FALLING
)
170 lvr
&= ~(1 << offset
);
175 iowrite32(lvr
, tgpio
->membase
+ TGPIO_LVR
);
176 iowrite32(flr
, tgpio
->membase
+ TGPIO_FLR
);
178 iowrite32(bflr
, tgpio
->membase
+ TGPIO_BFLR
);
180 iowrite32(1 << offset
, tgpio
->membase
+ TGPIO_ICR
);
183 spin_unlock_irqrestore(&tgpio
->lock
, flags
);
187 static void timbgpio_irq(unsigned int irq
, struct irq_desc
*desc
)
189 struct timbgpio
*tgpio
= get_irq_data(irq
);
193 desc
->chip
->ack(irq
);
194 ipr
= ioread32(tgpio
->membase
+ TGPIO_IPR
);
195 iowrite32(ipr
, tgpio
->membase
+ TGPIO_ICR
);
197 for_each_set_bit(offset
, &ipr
, tgpio
->gpio
.ngpio
)
198 generic_handle_irq(timbgpio_to_irq(&tgpio
->gpio
, offset
));
201 static struct irq_chip timbgpio_irqchip
= {
203 .enable
= timbgpio_irq_enable
,
204 .disable
= timbgpio_irq_disable
,
205 .set_type
= timbgpio_irq_type
,
208 static int __devinit
timbgpio_probe(struct platform_device
*pdev
)
211 struct gpio_chip
*gc
;
212 struct timbgpio
*tgpio
;
213 struct resource
*iomem
;
214 struct timbgpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
215 int irq
= platform_get_irq(pdev
, 0);
217 if (!pdata
|| pdata
->nr_pins
> 32) {
222 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
228 tgpio
= kzalloc(sizeof(*tgpio
), GFP_KERNEL
);
233 tgpio
->irq_base
= pdata
->irq_base
;
235 spin_lock_init(&tgpio
->lock
);
237 if (!request_mem_region(iomem
->start
, resource_size(iomem
),
243 tgpio
->membase
= ioremap(iomem
->start
, resource_size(iomem
));
244 if (!tgpio
->membase
) {
251 gc
->label
= dev_name(&pdev
->dev
);
252 gc
->owner
= THIS_MODULE
;
253 gc
->dev
= &pdev
->dev
;
254 gc
->direction_input
= timbgpio_gpio_direction_input
;
255 gc
->get
= timbgpio_gpio_get
;
256 gc
->direction_output
= timbgpio_gpio_direction_output
;
257 gc
->set
= timbgpio_gpio_set
;
258 gc
->to_irq
= (irq
>= 0 && tgpio
->irq_base
> 0) ? timbgpio_to_irq
: NULL
;
260 gc
->base
= pdata
->gpio_base
;
261 gc
->ngpio
= pdata
->nr_pins
;
264 err
= gpiochip_add(gc
);
268 platform_set_drvdata(pdev
, tgpio
);
270 /* make sure to disable interrupts */
271 iowrite32(0x0, tgpio
->membase
+ TGPIO_IER
);
273 if (irq
< 0 || tgpio
->irq_base
<= 0)
276 for (i
= 0; i
< pdata
->nr_pins
; i
++) {
277 set_irq_chip_and_handler_name(tgpio
->irq_base
+ i
,
278 &timbgpio_irqchip
, handle_simple_irq
, "mux");
279 set_irq_chip_data(tgpio
->irq_base
+ i
, tgpio
);
281 set_irq_flags(tgpio
->irq_base
+ i
, IRQF_VALID
| IRQF_PROBE
);
285 set_irq_data(irq
, tgpio
);
286 set_irq_chained_handler(irq
, timbgpio_irq
);
291 iounmap(tgpio
->membase
);
293 release_mem_region(iomem
->start
, resource_size(iomem
));
297 printk(KERN_ERR DRIVER_NAME
": Failed to register GPIOs: %d\n", err
);
302 static int __devexit
timbgpio_remove(struct platform_device
*pdev
)
305 struct timbgpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
306 struct timbgpio
*tgpio
= platform_get_drvdata(pdev
);
307 struct resource
*iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
308 int irq
= platform_get_irq(pdev
, 0);
310 if (irq
>= 0 && tgpio
->irq_base
> 0) {
312 for (i
= 0; i
< pdata
->nr_pins
; i
++) {
313 set_irq_chip(tgpio
->irq_base
+ i
, NULL
);
314 set_irq_chip_data(tgpio
->irq_base
+ i
, NULL
);
317 set_irq_handler(irq
, NULL
);
318 set_irq_data(irq
, NULL
);
321 err
= gpiochip_remove(&tgpio
->gpio
);
323 printk(KERN_ERR DRIVER_NAME
": failed to remove gpio_chip\n");
325 iounmap(tgpio
->membase
);
326 release_mem_region(iomem
->start
, resource_size(iomem
));
329 platform_set_drvdata(pdev
, NULL
);
334 static struct platform_driver timbgpio_platform_driver
= {
337 .owner
= THIS_MODULE
,
339 .probe
= timbgpio_probe
,
340 .remove
= timbgpio_remove
,
343 /*--------------------------------------------------------------------------*/
345 static int __init
timbgpio_init(void)
347 return platform_driver_register(&timbgpio_platform_driver
);
350 static void __exit
timbgpio_exit(void)
352 platform_driver_unregister(&timbgpio_platform_driver
);
355 module_init(timbgpio_init
);
356 module_exit(timbgpio_exit
);
358 MODULE_DESCRIPTION("Timberdale GPIO driver");
359 MODULE_LICENSE("GPL v2");
360 MODULE_AUTHOR("Mocean Laboratories");
361 MODULE_ALIAS("platform:"DRIVER_NAME
);