2 * linux/arch/arm/mach-msm/gpio.c
4 * Copyright (C) 2005 HP Labs
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2009 Pavel Machek <pavel@ucw.cz>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/interrupt.h>
19 #include <linux/gpio.h>
21 #include "board-trout.h"
23 static uint8_t trout_int_mask
[2] = {
24 [0] = 0xff, /* mask all interrupts */
27 static uint8_t trout_sleep_int_mask
[] = {
32 struct msm_gpio_chip
{
33 struct gpio_chip chip
;
34 void __iomem
*reg
; /* Base of register bank */
38 #define to_msm_gpio_chip(c) container_of(c, struct msm_gpio_chip, chip)
40 static int msm_gpiolib_get(struct gpio_chip
*chip
, unsigned offset
)
42 struct msm_gpio_chip
*msm_gpio
= to_msm_gpio_chip(chip
);
43 unsigned mask
= 1 << offset
;
45 return !!(readb(msm_gpio
->reg
) & mask
);
48 static void msm_gpiolib_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
50 struct msm_gpio_chip
*msm_gpio
= to_msm_gpio_chip(chip
);
51 unsigned mask
= 1 << offset
;
54 msm_gpio
->shadow
|= mask
;
56 msm_gpio
->shadow
&= ~mask
;
58 writeb(msm_gpio
->shadow
, msm_gpio
->reg
);
61 static int msm_gpiolib_direction_input(struct gpio_chip
*chip
,
64 msm_gpiolib_set(chip
, offset
, 0);
68 static int msm_gpiolib_direction_output(struct gpio_chip
*chip
,
69 unsigned offset
, int val
)
71 msm_gpiolib_set(chip
, offset
, val
);
75 static int trout_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
77 struct msm_gpio_chip
*msm_gpio
= to_msm_gpio_chip(chip
);
79 return TROUT_GPIO_TO_INT(offset
+ chip
->base
);
82 #define TROUT_GPIO_BANK(name, reg_num, base_gpio, shadow_val) \
86 .direction_input = msm_gpiolib_direction_input,\
87 .direction_output = msm_gpiolib_direction_output, \
88 .get = msm_gpiolib_get, \
89 .set = msm_gpiolib_set, \
90 .to_irq = trout_gpio_to_irq, \
94 .reg = (void *) reg_num + TROUT_CPLD_BASE, \
95 .shadow = shadow_val, \
98 static struct msm_gpio_chip msm_gpio_banks
[] = {
99 #if defined(CONFIG_MSM_DEBUG_UART1)
100 /* H2W pins <-> UART1 */
101 TROUT_GPIO_BANK("MISC2", 0x00, TROUT_GPIO_MISC2_BASE
, 0x40),
103 /* H2W pins <-> UART3, Bluetooth <-> UART1 */
104 TROUT_GPIO_BANK("MISC2", 0x00, TROUT_GPIO_MISC2_BASE
, 0x80),
107 TROUT_GPIO_BANK("MISC3", 0x02, TROUT_GPIO_MISC3_BASE
, 0x04),
108 TROUT_GPIO_BANK("MISC4", 0x04, TROUT_GPIO_MISC4_BASE
, 0),
110 TROUT_GPIO_BANK("MISC5", 0x06, TROUT_GPIO_MISC5_BASE
, 0x04),
111 TROUT_GPIO_BANK("INT2", 0x08, TROUT_GPIO_INT2_BASE
, 0),
112 TROUT_GPIO_BANK("MISC1", 0x0a, TROUT_GPIO_MISC1_BASE
, 0),
113 TROUT_GPIO_BANK("VIRTUAL", 0x12, TROUT_GPIO_VIRTUAL_BASE
, 0),
116 static void trout_gpio_irq_ack(struct irq_data
*d
)
118 int bank
= TROUT_INT_TO_BANK(d
->irq
);
119 uint8_t mask
= TROUT_INT_TO_MASK(d
->irq
);
120 int reg
= TROUT_BANK_TO_STAT_REG(bank
);
121 /*printk(KERN_INFO "trout_gpio_irq_ack irq %d\n", d->irq);*/
122 writeb(mask
, TROUT_CPLD_BASE
+ reg
);
125 static void trout_gpio_irq_mask(struct irq_data
*d
)
129 int bank
= TROUT_INT_TO_BANK(d
->irq
);
130 uint8_t mask
= TROUT_INT_TO_MASK(d
->irq
);
131 int reg
= TROUT_BANK_TO_MASK_REG(bank
);
133 local_irq_save(flags
);
134 reg_val
= trout_int_mask
[bank
] |= mask
;
135 /*printk(KERN_INFO "trout_gpio_irq_mask irq %d => %d:%02x\n",
136 d->irq, bank, reg_val);*/
137 writeb(reg_val
, TROUT_CPLD_BASE
+ reg
);
138 local_irq_restore(flags
);
141 static void trout_gpio_irq_unmask(struct irq_data
*d
)
145 int bank
= TROUT_INT_TO_BANK(d
->irq
);
146 uint8_t mask
= TROUT_INT_TO_MASK(d
->irq
);
147 int reg
= TROUT_BANK_TO_MASK_REG(bank
);
149 local_irq_save(flags
);
150 reg_val
= trout_int_mask
[bank
] &= ~mask
;
151 /*printk(KERN_INFO "trout_gpio_irq_unmask irq %d => %d:%02x\n",
152 d->irq, bank, reg_val);*/
153 writeb(reg_val
, TROUT_CPLD_BASE
+ reg
);
154 local_irq_restore(flags
);
157 int trout_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
160 int bank
= TROUT_INT_TO_BANK(d
->irq
);
161 uint8_t mask
= TROUT_INT_TO_MASK(d
->irq
);
163 local_irq_save(flags
);
165 trout_sleep_int_mask
[bank
] &= ~mask
;
167 trout_sleep_int_mask
[bank
] |= mask
;
168 local_irq_restore(flags
);
172 static void trout_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
178 int int_base
= TROUT_INT_START
;
181 for (bank
= 0; bank
< 2; bank
++) {
182 stat_reg
= TROUT_BANK_TO_STAT_REG(bank
);
183 v
= readb(TROUT_CPLD_BASE
+ stat_reg
);
184 int_mask
= trout_int_mask
[bank
];
186 writeb(v
& int_mask
, TROUT_CPLD_BASE
+ stat_reg
);
187 printk(KERN_ERR
"trout_gpio_irq_handler: got masked "
188 "interrupt: %d:%02x\n", bank
, v
& int_mask
);
194 /*printk(KERN_INFO "msm_gpio_irq_handler %d:%02x %02x b"
195 "it %d irq %d\n", bank, v, m, j, int_base + j);*/
197 generic_handle_irq(int_base
+ j
);
199 int_base
+= TROUT_INT_BANK0_COUNT
;
201 desc
->irq_data
.chip
->irq_ack(&desc
->irq_data
);
204 static struct irq_chip trout_gpio_irq_chip
= {
206 .irq_ack
= trout_gpio_irq_ack
,
207 .irq_mask
= trout_gpio_irq_mask
,
208 .irq_unmask
= trout_gpio_irq_unmask
,
209 .irq_set_wake
= trout_gpio_irq_set_wake
,
213 * Called from the processor-specific init to enable GPIO pin support.
215 int __init
trout_init_gpio(void)
218 for(i
= TROUT_INT_START
; i
<= TROUT_INT_END
; i
++) {
219 set_irq_chip(i
, &trout_gpio_irq_chip
);
220 set_irq_handler(i
, handle_edge_irq
);
221 set_irq_flags(i
, IRQF_VALID
);
224 for (i
= 0; i
< ARRAY_SIZE(msm_gpio_banks
); i
++)
225 gpiochip_add(&msm_gpio_banks
[i
].chip
);
227 set_irq_type(MSM_GPIO_TO_INT(17), IRQF_TRIGGER_HIGH
);
228 set_irq_chained_handler(MSM_GPIO_TO_INT(17), trout_gpio_irq_handler
);
229 set_irq_wake(MSM_GPIO_TO_INT(17), 1);
234 postcore_initcall(trout_init_gpio
);