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 OpenedHand Ltd.
14 * Authors: Phil Blundell <pb@handhelds.org>,
15 * Samuel Ortiz <sameo@openedhand.com>
19 #include <linux/version.h>
20 #include <linux/kernel.h>
21 #include <linux/irq.h>
23 #include <linux/spinlock.h>
24 #include <linux/platform_device.h>
26 #include <linux/mfd/asic3.h>
28 static inline void asic3_write_register(struct asic3
*asic
,
29 unsigned int reg
, u32 value
)
31 iowrite16(value
, (unsigned long)asic
->mapping
+
32 (reg
>> asic
->bus_shift
));
35 static inline u32
asic3_read_register(struct asic3
*asic
,
38 return ioread16((unsigned long)asic
->mapping
+
39 (reg
>> asic
->bus_shift
));
43 #define MAX_ASIC_ISR_LOOPS 20
44 #define ASIC3_GPIO_Base_INCR \
45 (ASIC3_GPIO_B_Base - ASIC3_GPIO_A_Base)
47 static void asic3_irq_flip_edge(struct asic3
*asic
,
53 spin_lock_irqsave(&asic
->lock
, flags
);
54 edge
= asic3_read_register(asic
,
55 base
+ ASIC3_GPIO_EdgeTrigger
);
57 asic3_write_register(asic
,
58 base
+ ASIC3_GPIO_EdgeTrigger
, edge
);
59 spin_unlock_irqrestore(&asic
->lock
, flags
);
62 static void asic3_irq_demux(unsigned int irq
, struct irq_desc
*desc
)
70 asic
= desc
->handler_data
;
72 for (iter
= 0 ; iter
< MAX_ASIC_ISR_LOOPS
; iter
++) {
76 spin_lock_irqsave(&asic
->lock
, flags
);
77 status
= asic3_read_register(asic
,
78 ASIC3_OFFSET(INTR
, PIntStat
));
79 spin_unlock_irqrestore(&asic
->lock
, flags
);
81 /* Check all ten register bits */
82 if ((status
& 0x3ff) == 0)
85 /* Handle GPIO IRQs */
86 for (bank
= 0; bank
< ASIC3_NUM_GPIO_BANKS
; bank
++) {
87 if (status
& (1 << bank
)) {
88 unsigned long base
, istat
;
90 base
= ASIC3_GPIO_A_Base
91 + bank
* ASIC3_GPIO_Base_INCR
;
93 spin_lock_irqsave(&asic
->lock
, flags
);
94 istat
= asic3_read_register(asic
,
96 ASIC3_GPIO_IntStatus
);
97 /* Clearing IntStatus */
98 asic3_write_register(asic
,
100 ASIC3_GPIO_IntStatus
, 0);
101 spin_unlock_irqrestore(&asic
->lock
, flags
);
103 for (i
= 0; i
< ASIC3_GPIOS_PER_BANK
; i
++) {
110 irqnr
= asic
->irq_base
+
111 (ASIC3_GPIOS_PER_BANK
* bank
)
113 desc
= irq_desc
+ irqnr
;
114 desc
->handle_irq(irqnr
, desc
);
115 if (asic
->irq_bothedge
[bank
] & bit
)
116 asic3_irq_flip_edge(asic
, base
,
122 /* Handle remaining IRQs in the status register */
123 for (i
= ASIC3_NUM_GPIOS
; i
< ASIC3_NR_IRQS
; i
++) {
124 /* They start at bit 4 and go up */
125 if (status
& (1 << (i
- ASIC3_NUM_GPIOS
+ 4))) {
126 desc
= irq_desc
+ + i
;
127 desc
->handle_irq(asic
->irq_base
+ i
,
133 if (iter
>= MAX_ASIC_ISR_LOOPS
)
134 printk(KERN_ERR
"%s: interrupt processing overrun\n",
138 static inline int asic3_irq_to_bank(struct asic3
*asic
, int irq
)
142 n
= (irq
- asic
->irq_base
) >> 4;
144 return (n
* (ASIC3_GPIO_B_Base
- ASIC3_GPIO_A_Base
));
147 static inline int asic3_irq_to_index(struct asic3
*asic
, int irq
)
149 return (irq
- asic
->irq_base
) & 0xf;
152 static void asic3_mask_gpio_irq(unsigned int irq
)
154 struct asic3
*asic
= get_irq_chip_data(irq
);
155 u32 val
, bank
, index
;
158 bank
= asic3_irq_to_bank(asic
, irq
);
159 index
= asic3_irq_to_index(asic
, irq
);
161 spin_lock_irqsave(&asic
->lock
, flags
);
162 val
= asic3_read_register(asic
, bank
+ ASIC3_GPIO_Mask
);
164 asic3_write_register(asic
, bank
+ ASIC3_GPIO_Mask
, val
);
165 spin_unlock_irqrestore(&asic
->lock
, flags
);
168 static void asic3_mask_irq(unsigned int irq
)
170 struct asic3
*asic
= get_irq_chip_data(irq
);
174 spin_lock_irqsave(&asic
->lock
, flags
);
175 regval
= asic3_read_register(asic
,
179 regval
&= ~(ASIC3_INTMASK_MASK0
<<
180 (irq
- (asic
->irq_base
+ ASIC3_NUM_GPIOS
)));
182 asic3_write_register(asic
,
186 spin_unlock_irqrestore(&asic
->lock
, flags
);
189 static void asic3_unmask_gpio_irq(unsigned int irq
)
191 struct asic3
*asic
= get_irq_chip_data(irq
);
192 u32 val
, bank
, index
;
195 bank
= asic3_irq_to_bank(asic
, irq
);
196 index
= asic3_irq_to_index(asic
, irq
);
198 spin_lock_irqsave(&asic
->lock
, flags
);
199 val
= asic3_read_register(asic
, bank
+ ASIC3_GPIO_Mask
);
200 val
&= ~(1 << index
);
201 asic3_write_register(asic
, bank
+ ASIC3_GPIO_Mask
, val
);
202 spin_unlock_irqrestore(&asic
->lock
, flags
);
205 static void asic3_unmask_irq(unsigned int irq
)
207 struct asic3
*asic
= get_irq_chip_data(irq
);
211 spin_lock_irqsave(&asic
->lock
, flags
);
212 regval
= asic3_read_register(asic
,
216 regval
|= (ASIC3_INTMASK_MASK0
<<
217 (irq
- (asic
->irq_base
+ ASIC3_NUM_GPIOS
)));
219 asic3_write_register(asic
,
223 spin_unlock_irqrestore(&asic
->lock
, flags
);
226 static int asic3_gpio_irq_type(unsigned int irq
, unsigned int type
)
228 struct asic3
*asic
= get_irq_chip_data(irq
);
230 u16 trigger
, level
, edge
, bit
;
233 bank
= asic3_irq_to_bank(asic
, irq
);
234 index
= asic3_irq_to_index(asic
, irq
);
237 spin_lock_irqsave(&asic
->lock
, flags
);
238 level
= asic3_read_register(asic
,
239 bank
+ ASIC3_GPIO_LevelTrigger
);
240 edge
= asic3_read_register(asic
,
241 bank
+ ASIC3_GPIO_EdgeTrigger
);
242 trigger
= asic3_read_register(asic
,
243 bank
+ ASIC3_GPIO_TriggerType
);
244 asic
->irq_bothedge
[(irq
- asic
->irq_base
) >> 4] &= ~bit
;
246 if (type
== IRQT_RISING
) {
249 } else if (type
== IRQT_FALLING
) {
252 } else if (type
== IRQT_BOTHEDGE
) {
254 if (asic3_gpio_get_value(asic
, irq
- asic
->irq_base
))
258 asic
->irq_bothedge
[(irq
- asic
->irq_base
) >> 4] |= bit
;
259 } else if (type
== IRQT_LOW
) {
262 } else if (type
== IRQT_HIGH
) {
267 * if type == IRQT_NOEDGE, we should mask interrupts, but
268 * be careful to not unmask them if mask was also called.
269 * Probably need internal state for mask.
271 printk(KERN_NOTICE
"asic3: irq type not changed.\n");
273 asic3_write_register(asic
, bank
+ ASIC3_GPIO_LevelTrigger
,
275 asic3_write_register(asic
, bank
+ ASIC3_GPIO_EdgeTrigger
,
277 asic3_write_register(asic
, bank
+ ASIC3_GPIO_TriggerType
,
279 spin_unlock_irqrestore(&asic
->lock
, flags
);
283 static struct irq_chip asic3_gpio_irq_chip
= {
284 .name
= "ASIC3-GPIO",
285 .ack
= asic3_mask_gpio_irq
,
286 .mask
= asic3_mask_gpio_irq
,
287 .unmask
= asic3_unmask_gpio_irq
,
288 .set_type
= asic3_gpio_irq_type
,
291 static struct irq_chip asic3_irq_chip
= {
293 .ack
= asic3_mask_irq
,
294 .mask
= asic3_mask_irq
,
295 .unmask
= asic3_unmask_irq
,
298 static int asic3_irq_probe(struct platform_device
*pdev
)
300 struct asic3
*asic
= platform_get_drvdata(pdev
);
301 unsigned long clksel
= 0;
302 unsigned int irq
, irq_base
;
304 asic
->irq_nr
= platform_get_irq(pdev
, 0);
305 if (asic
->irq_nr
< 0)
308 /* turn on clock to IRQ controller */
309 clksel
|= CLOCK_SEL_CX
;
310 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
),
313 irq_base
= asic
->irq_base
;
315 for (irq
= irq_base
; irq
< irq_base
+ ASIC3_NR_IRQS
; irq
++) {
316 if (irq
< asic
->irq_base
+ ASIC3_NUM_GPIOS
)
317 set_irq_chip(irq
, &asic3_gpio_irq_chip
);
319 set_irq_chip(irq
, &asic3_irq_chip
);
321 set_irq_chip_data(irq
, asic
);
322 set_irq_handler(irq
, handle_level_irq
);
323 set_irq_flags(irq
, IRQF_VALID
| IRQF_PROBE
);
326 asic3_write_register(asic
, ASIC3_OFFSET(INTR
, IntMask
),
327 ASIC3_INTMASK_GINTMASK
);
329 set_irq_chained_handler(asic
->irq_nr
, asic3_irq_demux
);
330 set_irq_type(asic
->irq_nr
, IRQT_RISING
);
331 set_irq_data(asic
->irq_nr
, asic
);
336 static void asic3_irq_remove(struct platform_device
*pdev
)
338 struct asic3
*asic
= platform_get_drvdata(pdev
);
339 unsigned int irq
, irq_base
;
341 irq_base
= asic
->irq_base
;
343 for (irq
= irq_base
; irq
< irq_base
+ ASIC3_NR_IRQS
; irq
++) {
344 set_irq_flags(irq
, 0);
345 set_irq_handler(irq
, NULL
);
346 set_irq_chip(irq
, NULL
);
347 set_irq_chip_data(irq
, NULL
);
349 set_irq_chained_handler(asic
->irq_nr
, NULL
);
353 static inline u32
asic3_get_gpio(struct asic3
*asic
, unsigned int base
,
354 unsigned int function
)
356 return asic3_read_register(asic
, base
+ function
);
359 static void asic3_set_gpio(struct asic3
*asic
, unsigned int base
,
360 unsigned int function
, u32 bits
, u32 val
)
364 spin_lock_irqsave(&asic
->lock
, flags
);
365 val
|= (asic3_read_register(asic
, base
+ function
) & ~bits
);
367 asic3_write_register(asic
, base
+ function
, val
);
368 spin_unlock_irqrestore(&asic
->lock
, flags
);
371 #define asic3_get_gpio_a(asic, fn) \
372 asic3_get_gpio(asic, ASIC3_GPIO_A_Base, ASIC3_GPIO_##fn)
373 #define asic3_get_gpio_b(asic, fn) \
374 asic3_get_gpio(asic, ASIC3_GPIO_B_Base, ASIC3_GPIO_##fn)
375 #define asic3_get_gpio_c(asic, fn) \
376 asic3_get_gpio(asic, ASIC3_GPIO_C_Base, ASIC3_GPIO_##fn)
377 #define asic3_get_gpio_d(asic, fn) \
378 asic3_get_gpio(asic, ASIC3_GPIO_D_Base, ASIC3_GPIO_##fn)
380 #define asic3_set_gpio_a(asic, fn, bits, val) \
381 asic3_set_gpio(asic, ASIC3_GPIO_A_Base, ASIC3_GPIO_##fn, bits, val)
382 #define asic3_set_gpio_b(asic, fn, bits, val) \
383 asic3_set_gpio(asic, ASIC3_GPIO_B_Base, ASIC3_GPIO_##fn, bits, val)
384 #define asic3_set_gpio_c(asic, fn, bits, val) \
385 asic3_set_gpio(asic, ASIC3_GPIO_C_Base, ASIC3_GPIO_##fn, bits, val)
386 #define asic3_set_gpio_d(asic, fn, bits, val) \
387 asic3_set_gpio(asic, ASIC3_GPIO_D_Base, ASIC3_GPIO_##fn, bits, val)
389 #define asic3_set_gpio_banks(asic, fn, bits, pdata, field) \
391 asic3_set_gpio_a((asic), fn, (bits), (pdata)->gpio_a.field); \
392 asic3_set_gpio_b((asic), fn, (bits), (pdata)->gpio_b.field); \
393 asic3_set_gpio_c((asic), fn, (bits), (pdata)->gpio_c.field); \
394 asic3_set_gpio_d((asic), fn, (bits), (pdata)->gpio_d.field); \
397 int asic3_gpio_get_value(struct asic3
*asic
, unsigned gpio
)
399 u32 mask
= ASIC3_GPIO_bit(gpio
);
402 case ASIC3_GPIO_BANK_A
:
403 return asic3_get_gpio_a(asic
, Status
) & mask
;
404 case ASIC3_GPIO_BANK_B
:
405 return asic3_get_gpio_b(asic
, Status
) & mask
;
406 case ASIC3_GPIO_BANK_C
:
407 return asic3_get_gpio_c(asic
, Status
) & mask
;
408 case ASIC3_GPIO_BANK_D
:
409 return asic3_get_gpio_d(asic
, Status
) & mask
;
411 printk(KERN_ERR
"%s: invalid GPIO value 0x%x",
416 EXPORT_SYMBOL(asic3_gpio_get_value
);
418 void asic3_gpio_set_value(struct asic3
*asic
, unsigned gpio
, int val
)
420 u32 mask
= ASIC3_GPIO_bit(gpio
);
426 case ASIC3_GPIO_BANK_A
:
427 asic3_set_gpio_a(asic
, Out
, mask
, bitval
);
429 case ASIC3_GPIO_BANK_B
:
430 asic3_set_gpio_b(asic
, Out
, mask
, bitval
);
432 case ASIC3_GPIO_BANK_C
:
433 asic3_set_gpio_c(asic
, Out
, mask
, bitval
);
435 case ASIC3_GPIO_BANK_D
:
436 asic3_set_gpio_d(asic
, Out
, mask
, bitval
);
439 printk(KERN_ERR
"%s: invalid GPIO value 0x%x",
444 EXPORT_SYMBOL(asic3_gpio_set_value
);
446 static int asic3_gpio_probe(struct platform_device
*pdev
)
448 struct asic3_platform_data
*pdata
= pdev
->dev
.platform_data
;
449 struct asic3
*asic
= platform_get_drvdata(pdev
);
451 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(A
, Mask
), 0xffff);
452 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(B
, Mask
), 0xffff);
453 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(C
, Mask
), 0xffff);
454 asic3_write_register(asic
, ASIC3_GPIO_OFFSET(D
, Mask
), 0xffff);
456 asic3_set_gpio_a(asic
, SleepMask
, 0xffff, 0xffff);
457 asic3_set_gpio_b(asic
, SleepMask
, 0xffff, 0xffff);
458 asic3_set_gpio_c(asic
, SleepMask
, 0xffff, 0xffff);
459 asic3_set_gpio_d(asic
, SleepMask
, 0xffff, 0xffff);
462 asic3_set_gpio_banks(asic
, Out
, 0xffff, pdata
, init
);
463 asic3_set_gpio_banks(asic
, Direction
, 0xffff, pdata
, dir
);
464 asic3_set_gpio_banks(asic
, SleepMask
, 0xffff, pdata
,
466 asic3_set_gpio_banks(asic
, SleepOut
, 0xffff, pdata
, sleep_out
);
467 asic3_set_gpio_banks(asic
, BattFaultOut
, 0xffff, pdata
,
469 asic3_set_gpio_banks(asic
, SleepConf
, 0xffff, pdata
,
471 asic3_set_gpio_banks(asic
, AltFunction
, 0xffff, pdata
,
478 static void asic3_gpio_remove(struct platform_device
*pdev
)
485 static int asic3_probe(struct platform_device
*pdev
)
487 struct asic3_platform_data
*pdata
= pdev
->dev
.platform_data
;
489 struct resource
*mem
;
490 unsigned long clksel
;
493 asic
= kzalloc(sizeof(struct asic3
), GFP_KERNEL
);
497 spin_lock_init(&asic
->lock
);
498 platform_set_drvdata(pdev
, asic
);
499 asic
->dev
= &pdev
->dev
;
501 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
504 printk(KERN_ERR
"asic3: no MEM resource\n");
508 asic
->mapping
= ioremap(mem
->start
, PAGE_SIZE
);
509 if (!asic
->mapping
) {
511 printk(KERN_ERR
"asic3: couldn't ioremap\n");
515 asic
->irq_base
= pdata
->irq_base
;
517 if (pdata
&& pdata
->bus_shift
)
518 asic
->bus_shift
= 2 - pdata
->bus_shift
;
523 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
), clksel
);
525 ret
= asic3_irq_probe(pdev
);
527 printk(KERN_ERR
"asic3: couldn't probe IRQs\n");
530 asic3_gpio_probe(pdev
);
532 if (pdata
->children
) {
534 for (i
= 0; i
< pdata
->n_children
; i
++) {
535 pdata
->children
[i
]->dev
.parent
= &pdev
->dev
;
536 platform_device_register(pdata
->children
[i
]);
540 printk(KERN_INFO
"ASIC3 Core driver\n");
545 iounmap(asic
->mapping
);
552 static int asic3_remove(struct platform_device
*pdev
)
554 struct asic3
*asic
= platform_get_drvdata(pdev
);
556 asic3_gpio_remove(pdev
);
557 asic3_irq_remove(pdev
);
559 asic3_write_register(asic
, ASIC3_OFFSET(CLOCK
, SEL
), 0);
561 iounmap(asic
->mapping
);
568 static void asic3_shutdown(struct platform_device
*pdev
)
572 static struct platform_driver asic3_device_driver
= {
576 .probe
= asic3_probe
,
577 .remove
= __devexit_p(asic3_remove
),
578 .shutdown
= asic3_shutdown
,
581 static int __init
asic3_init(void)
584 retval
= platform_driver_register(&asic3_device_driver
);
588 subsys_initcall(asic3_init
);