4 * Compaq ASIC3 support.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Copyright 2001 Compaq Computer Corporation.
11 * Copyright 2004-2005 Phil Blundell
12 * Copyright 2007-2008 OpenedHand Ltd.
14 * Authors: Phil Blundell <pb@handhelds.org>,
15 * Samuel Ortiz <sameo@openedhand.com>
19 #include <linux/kernel.h>
20 #include <linux/irq.h>
21 #include <linux/gpio.h>
23 #include <linux/spinlock.h>
24 #include <linux/platform_device.h>
26 #include <linux/mfd/asic3.h>
29 void __iomem
*mapping
;
30 unsigned int bus_shift
;
32 unsigned int irq_base
;
35 struct gpio_chip gpio
;
39 static int asic3_gpio_get(struct gpio_chip
*chip
, unsigned offset
);
41 static inline void asic3_write_register(struct asic3
*asic
,
42 unsigned int reg
, u32 value
)
44 iowrite16(value
, asic
->mapping
+
45 (reg
>> asic
->bus_shift
));
48 static inline u32
asic3_read_register(struct asic3
*asic
,
51 return ioread16(asic
->mapping
+
52 (reg
>> asic
->bus_shift
));
56 #define MAX_ASIC_ISR_LOOPS 20
57 #define ASIC3_GPIO_BASE_INCR \
58 (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
60 static void asic3_irq_flip_edge(struct asic3
*asic
,
66 spin_lock_irqsave(&asic
->lock
, flags
);
67 edge
= asic3_read_register(asic
,
68 base
+ ASIC3_GPIO_EDGE_TRIGGER
);
70 asic3_write_register(asic
,
71 base
+ ASIC3_GPIO_EDGE_TRIGGER
, edge
);
72 spin_unlock_irqrestore(&asic
->lock
, flags
);
75 static void asic3_irq_demux(unsigned int irq
, struct irq_desc
*desc
)
83 asic
= desc
->handler_data
;
85 for (iter
= 0 ; iter
< MAX_ASIC_ISR_LOOPS
; iter
++) {
89 spin_lock_irqsave(&asic
->lock
, flags
);
90 status
= asic3_read_register(asic
,
91 ASIC3_OFFSET(INTR
, P_INT_STAT
));
92 spin_unlock_irqrestore(&asic
->lock
, flags
);
94 /* Check all ten register bits */
95 if ((status
& 0x3ff) == 0)
98 /* Handle GPIO IRQs */
99 for (bank
= 0; bank
< ASIC3_NUM_GPIO_BANKS
; bank
++) {
100 if (status
& (1 << bank
)) {
101 unsigned long base
, istat
;
103 base
= ASIC3_GPIO_A_BASE
104 + bank
* ASIC3_GPIO_BASE_INCR
;
106 spin_lock_irqsave(&asic
->lock
, flags
);
107 istat
= asic3_read_register(asic
,
109 ASIC3_GPIO_INT_STATUS
);
110 /* Clearing IntStatus */
111 asic3_write_register(asic
,
113 ASIC3_GPIO_INT_STATUS
, 0);
114 spin_unlock_irqrestore(&asic
->lock
, flags
);
116 for (i
= 0; i
< ASIC3_GPIOS_PER_BANK
; i
++) {
123 irqnr
= asic
->irq_base
+
124 (ASIC3_GPIOS_PER_BANK
* bank
)
126 desc
= irq_desc
+ irqnr
;
127 desc
->handle_irq(irqnr
, desc
);
128 if (asic
->irq_bothedge
[bank
] & bit
)
129 asic3_irq_flip_edge(asic
, base
,
135 /* Handle remaining IRQs in the status register */
136 for (i
= ASIC3_NUM_GPIOS
; i
< ASIC3_NR_IRQS
; i
++) {
137 /* They start at bit 4 and go up */
138 if (status
& (1 << (i
- ASIC3_NUM_GPIOS
+ 4))) {
139 desc
= irq_desc
+ asic
->irq_base
+ i
;
140 desc
->handle_irq(asic
->irq_base
+ i
,
146 if (iter
>= MAX_ASIC_ISR_LOOPS
)
147 dev_err(asic
->dev
, "interrupt processing overrun\n");
150 static inline int asic3_irq_to_bank(struct asic3
*asic
, int irq
)
154 n
= (irq
- asic
->irq_base
) >> 4;
156 return (n
* (ASIC3_GPIO_B_BASE
- ASIC3_GPIO_A_BASE
));
159 static inline int asic3_irq_to_index(struct asic3
*asic
, int irq
)
161 return (irq
- asic
->irq_base
) & 0xf;
164 static void asic3_mask_gpio_irq(unsigned int irq
)
166 struct asic3
*asic
= get_irq_chip_data(irq
);
167 u32 val
, bank
, index
;
170 bank
= asic3_irq_to_bank(asic
, irq
);
171 index
= asic3_irq_to_index(asic
, irq
);
173 spin_lock_irqsave(&asic
->lock
, flags
);
174 val
= asic3_read_register(asic
, bank
+ ASIC3_GPIO_MASK
);
176 asic3_write_register(asic
, bank
+ ASIC3_GPIO_MASK
, val
);
177 spin_unlock_irqrestore(&asic
->lock
, flags
);
180 static void asic3_mask_irq(unsigned int irq
)
182 struct asic3
*asic
= get_irq_chip_data(irq
);
186 spin_lock_irqsave(&asic
->lock
, flags
);
187 regval
= asic3_read_register(asic
,
189 ASIC3_INTR_INT_MASK
);
191 regval
&= ~(ASIC3_INTMASK_MASK0
<<
192 (irq
- (asic
->irq_base
+ ASIC3_NUM_GPIOS
)));
194 asic3_write_register(asic
,
198 spin_unlock_irqrestore(&asic
->lock
, flags
);
201 static void asic3_unmask_gpio_irq(unsigned int irq
)
203 struct asic3
*asic
= get_irq_chip_data(irq
);
204 u32 val
, bank
, index
;
207 bank
= asic3_irq_to_bank(asic
, irq
);
208 index
= asic3_irq_to_index(asic
, irq
);
210 spin_lock_irqsave(&asic
->lock
, flags
);
211 val
= asic3_read_register(asic
, bank
+ ASIC3_GPIO_MASK
);
212 val
&= ~(1 << index
);
213 asic3_write_register(asic
, bank
+ ASIC3_GPIO_MASK
, val
);
214 spin_unlock_irqrestore(&asic
->lock
, flags
);
217 static void asic3_unmask_irq(unsigned int irq
)
219 struct asic3
*asic
= get_irq_chip_data(irq
);
223 spin_lock_irqsave(&asic
->lock
, flags
);
224 regval
= asic3_read_register(asic
,
226 ASIC3_INTR_INT_MASK
);
228 regval
|= (ASIC3_INTMASK_MASK0
<<
229 (irq
- (asic
->irq_base
+ ASIC3_NUM_GPIOS
)));
231 asic3_write_register(asic
,
235 spin_unlock_irqrestore(&asic
->lock
, flags
);
238 static int asic3_gpio_irq_type(unsigned int irq
, unsigned int type
)
240 struct asic3
*asic
= get_irq_chip_data(irq
);
242 u16 trigger
, level
, edge
, bit
;
245 bank
= asic3_irq_to_bank(asic
, irq
);
246 index
= asic3_irq_to_index(asic
, irq
);
249 spin_lock_irqsave(&asic
->lock
, flags
);
250 level
= asic3_read_register(asic
,
251 bank
+ ASIC3_GPIO_LEVEL_TRIGGER
);
252 edge
= asic3_read_register(asic
,
253 bank
+ ASIC3_GPIO_EDGE_TRIGGER
);
254 trigger
= asic3_read_register(asic
,
255 bank
+ ASIC3_GPIO_TRIGGER_TYPE
);
256 asic
->irq_bothedge
[(irq
- asic
->irq_base
) >> 4] &= ~bit
;
258 if (type
== IRQ_TYPE_EDGE_RISING
) {
261 } else if (type
== IRQ_TYPE_EDGE_FALLING
) {
264 } else if (type
== IRQ_TYPE_EDGE_BOTH
) {
266 if (asic3_gpio_get(&asic
->gpio
, irq
- asic
->irq_base
))
270 asic
->irq_bothedge
[(irq
- asic
->irq_base
) >> 4] |= bit
;
271 } else if (type
== IRQ_TYPE_LEVEL_LOW
) {
274 } else if (type
== IRQ_TYPE_LEVEL_HIGH
) {
279 * if type == IRQ_TYPE_NONE, we should mask interrupts, but
280 * be careful to not unmask them if mask was also called.
281 * Probably need internal state for mask.
283 dev_notice(asic
->dev
, "irq type not changed\n");
285 asic3_write_register(asic
, bank
+ ASIC3_GPIO_LEVEL_TRIGGER
,
287 asic3_write_register(asic
, bank
+ ASIC3_GPIO_EDGE_TRIGGER
,
289 asic3_write_register(asic
, bank
+ ASIC3_GPIO_TRIGGER_TYPE
,
291 spin_unlock_irqrestore(&asic
->lock
, flags
);
295 static struct irq_chip asic3_gpio_irq_chip
= {
296 .name
= "ASIC3-GPIO",
297 .ack
= asic3_mask_gpio_irq
,
298 .mask
= asic3_mask_gpio_irq
,
299 .unmask
= asic3_unmask_gpio_irq
,
300 .set_type
= asic3_gpio_irq_type
,
303 static struct irq_chip asic3_irq_chip
= {
305 .ack
= asic3_mask_irq
,
306 .mask
= asic3_mask_irq
,
307 .unmask
= asic3_unmask_irq
,
310 static int __init
asic3_irq_probe(struct platform_device
*pdev
)
312 struct asic3
*asic
= platform_get_drvdata(pdev
);
313 unsigned long clksel
= 0;
314 unsigned int irq
, irq_base
;
318 ret
= platform_get_irq(pdev
, 0);
323 /* turn on clock to IRQ controller */
324 clksel
|= CLOCK_SEL_CX
;
325 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
),
328 irq_base
= asic
->irq_base
;
330 for (irq
= irq_base
; irq
< irq_base
+ ASIC3_NR_IRQS
; irq
++) {
331 if (irq
< asic
->irq_base
+ ASIC3_NUM_GPIOS
)
332 set_irq_chip(irq
, &asic3_gpio_irq_chip
);
334 set_irq_chip(irq
, &asic3_irq_chip
);
336 set_irq_chip_data(irq
, asic
);
337 set_irq_handler(irq
, handle_level_irq
);
338 set_irq_flags(irq
, IRQF_VALID
| IRQF_PROBE
);
341 asic3_write_register(asic
, ASIC3_OFFSET(INTR
, INT_MASK
),
342 ASIC3_INTMASK_GINTMASK
);
344 set_irq_chained_handler(asic
->irq_nr
, asic3_irq_demux
);
345 set_irq_type(asic
->irq_nr
, IRQ_TYPE_EDGE_RISING
);
346 set_irq_data(asic
->irq_nr
, asic
);
351 static void asic3_irq_remove(struct platform_device
*pdev
)
353 struct asic3
*asic
= platform_get_drvdata(pdev
);
354 unsigned int irq
, irq_base
;
356 irq_base
= asic
->irq_base
;
358 for (irq
= irq_base
; irq
< irq_base
+ ASIC3_NR_IRQS
; irq
++) {
359 set_irq_flags(irq
, 0);
360 set_irq_handler(irq
, NULL
);
361 set_irq_chip(irq
, NULL
);
362 set_irq_chip_data(irq
, NULL
);
364 set_irq_chained_handler(asic
->irq_nr
, NULL
);
368 static int asic3_gpio_direction(struct gpio_chip
*chip
,
369 unsigned offset
, int out
)
371 u32 mask
= ASIC3_GPIO_TO_MASK(offset
), out_reg
;
372 unsigned int gpio_base
;
376 asic
= container_of(chip
, struct asic3
, gpio
);
377 gpio_base
= ASIC3_GPIO_TO_BASE(offset
);
379 if (gpio_base
> ASIC3_GPIO_D_BASE
) {
380 dev_err(asic
->dev
, "Invalid base (0x%x) for gpio %d\n",
385 spin_lock_irqsave(&asic
->lock
, flags
);
387 out_reg
= asic3_read_register(asic
, gpio_base
+ ASIC3_GPIO_DIRECTION
);
389 /* Input is 0, Output is 1 */
395 asic3_write_register(asic
, gpio_base
+ ASIC3_GPIO_DIRECTION
, out_reg
);
397 spin_unlock_irqrestore(&asic
->lock
, flags
);
403 static int asic3_gpio_direction_input(struct gpio_chip
*chip
,
406 return asic3_gpio_direction(chip
, offset
, 0);
409 static int asic3_gpio_direction_output(struct gpio_chip
*chip
,
410 unsigned offset
, int value
)
412 return asic3_gpio_direction(chip
, offset
, 1);
415 static int asic3_gpio_get(struct gpio_chip
*chip
,
418 unsigned int gpio_base
;
419 u32 mask
= ASIC3_GPIO_TO_MASK(offset
);
422 asic
= container_of(chip
, struct asic3
, gpio
);
423 gpio_base
= ASIC3_GPIO_TO_BASE(offset
);
425 if (gpio_base
> ASIC3_GPIO_D_BASE
) {
426 dev_err(asic
->dev
, "Invalid base (0x%x) for gpio %d\n",
431 return asic3_read_register(asic
, gpio_base
+ ASIC3_GPIO_STATUS
) & mask
;
434 static void asic3_gpio_set(struct gpio_chip
*chip
,
435 unsigned offset
, int value
)
438 unsigned int gpio_base
;
442 asic
= container_of(chip
, struct asic3
, gpio
);
443 gpio_base
= ASIC3_GPIO_TO_BASE(offset
);
445 if (gpio_base
> ASIC3_GPIO_D_BASE
) {
446 dev_err(asic
->dev
, "Invalid base (0x%x) for gpio %d\n",
451 mask
= ASIC3_GPIO_TO_MASK(offset
);
453 spin_lock_irqsave(&asic
->lock
, flags
);
455 out_reg
= asic3_read_register(asic
, gpio_base
+ ASIC3_GPIO_OUT
);
462 asic3_write_register(asic
, gpio_base
+ ASIC3_GPIO_OUT
, out_reg
);
464 spin_unlock_irqrestore(&asic
->lock
, flags
);
469 static __init
int asic3_gpio_probe(struct platform_device
*pdev
,
470 u16
*gpio_config
, int num
)
472 struct asic3
*asic
= platform_get_drvdata(pdev
);
473 u16 alt_reg
[ASIC3_NUM_GPIO_BANKS
];
474 u16 out_reg
[ASIC3_NUM_GPIO_BANKS
];
475 u16 dir_reg
[ASIC3_NUM_GPIO_BANKS
];
478 memzero(alt_reg
, ASIC3_NUM_GPIO_BANKS
* sizeof(u16
));
479 memzero(out_reg
, ASIC3_NUM_GPIO_BANKS
* sizeof(u16
));
480 memzero(dir_reg
, ASIC3_NUM_GPIO_BANKS
* sizeof(u16
));
482 /* Enable all GPIOs */
483 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(A
, MASK
), 0xffff);
484 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(B
, MASK
), 0xffff);
485 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(C
, MASK
), 0xffff);
486 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(D
, MASK
), 0xffff);
488 for (i
= 0; i
< num
; i
++) {
489 u8 alt
, pin
, dir
, init
, bank_num
, bit_num
;
490 u16 config
= gpio_config
[i
];
492 pin
= ASIC3_CONFIG_GPIO_PIN(config
);
493 alt
= ASIC3_CONFIG_GPIO_ALT(config
);
494 dir
= ASIC3_CONFIG_GPIO_DIR(config
);
495 init
= ASIC3_CONFIG_GPIO_INIT(config
);
497 bank_num
= ASIC3_GPIO_TO_BANK(pin
);
498 bit_num
= ASIC3_GPIO_TO_BIT(pin
);
500 alt_reg
[bank_num
] |= (alt
<< bit_num
);
501 out_reg
[bank_num
] |= (init
<< bit_num
);
502 dir_reg
[bank_num
] |= (dir
<< bit_num
);
505 for (i
= 0; i
< ASIC3_NUM_GPIO_BANKS
; i
++) {
506 asic3_write_register(asic
,
507 ASIC3_BANK_TO_BASE(i
) +
508 ASIC3_GPIO_DIRECTION
,
510 asic3_write_register(asic
,
511 ASIC3_BANK_TO_BASE(i
) + ASIC3_GPIO_OUT
,
513 asic3_write_register(asic
,
514 ASIC3_BANK_TO_BASE(i
) +
515 ASIC3_GPIO_ALT_FUNCTION
,
519 return gpiochip_add(&asic
->gpio
);
522 static int asic3_gpio_remove(struct platform_device
*pdev
)
524 struct asic3
*asic
= platform_get_drvdata(pdev
);
526 return gpiochip_remove(&asic
->gpio
);
531 static int __init
asic3_probe(struct platform_device
*pdev
)
533 struct asic3_platform_data
*pdata
= pdev
->dev
.platform_data
;
535 struct resource
*mem
;
536 unsigned long clksel
;
539 asic
= kzalloc(sizeof(struct asic3
), GFP_KERNEL
);
541 printk(KERN_ERR
"kzalloc failed\n");
545 spin_lock_init(&asic
->lock
);
546 platform_set_drvdata(pdev
, asic
);
547 asic
->dev
= &pdev
->dev
;
549 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
552 dev_err(asic
->dev
, "no MEM resource\n");
556 map_size
= mem
->end
- mem
->start
+ 1;
557 asic
->mapping
= ioremap(mem
->start
, map_size
);
558 if (!asic
->mapping
) {
560 dev_err(asic
->dev
, "Couldn't ioremap\n");
564 asic
->irq_base
= pdata
->irq_base
;
566 /* calculate bus shift from mem resource */
567 asic
->bus_shift
= 2 - (map_size
>> 12);
570 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
), clksel
);
572 ret
= asic3_irq_probe(pdev
);
574 dev_err(asic
->dev
, "Couldn't probe IRQs\n");
578 asic
->gpio
.base
= pdata
->gpio_base
;
579 asic
->gpio
.ngpio
= ASIC3_NUM_GPIOS
;
580 asic
->gpio
.get
= asic3_gpio_get
;
581 asic
->gpio
.set
= asic3_gpio_set
;
582 asic
->gpio
.direction_input
= asic3_gpio_direction_input
;
583 asic
->gpio
.direction_output
= asic3_gpio_direction_output
;
585 ret
= asic3_gpio_probe(pdev
,
587 pdata
->gpio_config_num
);
589 dev_err(asic
->dev
, "GPIO probe failed\n");
593 dev_info(asic
->dev
, "ASIC3 Core driver\n");
598 asic3_irq_remove(pdev
);
601 iounmap(asic
->mapping
);
609 static int asic3_remove(struct platform_device
*pdev
)
612 struct asic3
*asic
= platform_get_drvdata(pdev
);
614 ret
= asic3_gpio_remove(pdev
);
617 asic3_irq_remove(pdev
);
619 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
), 0);
621 iounmap(asic
->mapping
);
628 static void asic3_shutdown(struct platform_device
*pdev
)
632 static struct platform_driver asic3_device_driver
= {
636 .remove
= __devexit_p(asic3_remove
),
637 .shutdown
= asic3_shutdown
,
640 static int __init
asic3_init(void)
643 retval
= platform_driver_probe(&asic3_device_driver
, asic3_probe
);
647 subsys_initcall(asic3_init
);