2 * arch/arm/mach-tegra/gpio.c
4 * Copyright (c) 2010 Google, Inc
7 * Erik Gilling <konkers@google.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/init.h>
21 #include <linux/irq.h>
24 #include <linux/gpio.h>
26 #include <mach/iomap.h>
28 #define GPIO_BANK(x) ((x) >> 5)
29 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
30 #define GPIO_BIT(x) ((x) & 0x7)
32 #define GPIO_REG(x) (IO_TO_VIRT(TEGRA_GPIO_BASE) + \
33 GPIO_BANK(x) * 0x80 + \
36 #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
37 #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
38 #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
39 #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
40 #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
41 #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
42 #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
43 #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
45 #define GPIO_MSK_CNF(x) (GPIO_REG(x) + 0x800)
46 #define GPIO_MSK_OE(x) (GPIO_REG(x) + 0x810)
47 #define GPIO_MSK_OUT(x) (GPIO_REG(x) + 0X820)
48 #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + 0x840)
49 #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + 0x850)
50 #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + 0x860)
52 #define GPIO_INT_LVL_MASK 0x010101
53 #define GPIO_INT_LVL_EDGE_RISING 0x000101
54 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
55 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
56 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
57 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
59 struct tegra_gpio_bank
{
62 spinlock_t lvl_lock
[4];
66 static struct tegra_gpio_bank tegra_gpio_banks
[] = {
67 {.bank
= 0, .irq
= INT_GPIO1
},
68 {.bank
= 1, .irq
= INT_GPIO2
},
69 {.bank
= 2, .irq
= INT_GPIO3
},
70 {.bank
= 3, .irq
= INT_GPIO4
},
71 {.bank
= 4, .irq
= INT_GPIO5
},
72 {.bank
= 5, .irq
= INT_GPIO6
},
73 {.bank
= 6, .irq
= INT_GPIO7
},
76 static int tegra_gpio_compose(int bank
, int port
, int bit
)
78 return (bank
<< 5) | ((port
& 0x3) << 3) | (bit
& 0x7);
81 static void tegra_gpio_mask_write(u32 reg
, int gpio
, int value
)
85 val
= 0x100 << GPIO_BIT(gpio
);
87 val
|= 1 << GPIO_BIT(gpio
);
88 __raw_writel(val
, reg
);
91 void tegra_gpio_enable(int gpio
)
93 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 1);
96 void tegra_gpio_disable(int gpio
)
98 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 0);
101 static void tegra_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
103 tegra_gpio_mask_write(GPIO_MSK_OUT(offset
), offset
, value
);
106 static int tegra_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
108 return (__raw_readl(GPIO_IN(offset
)) >> GPIO_BIT(offset
)) & 0x1;
111 static int tegra_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
113 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 0);
117 static int tegra_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
120 tegra_gpio_set(chip
, offset
, value
);
121 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 1);
127 static struct gpio_chip tegra_gpio_chip
= {
128 .label
= "tegra-gpio",
129 .direction_input
= tegra_gpio_direction_input
,
130 .get
= tegra_gpio_get
,
131 .direction_output
= tegra_gpio_direction_output
,
132 .set
= tegra_gpio_set
,
134 .ngpio
= ARCH_NR_GPIOS
,
137 static void tegra_gpio_irq_ack(unsigned int irq
)
139 int gpio
= irq
- INT_GPIO_BASE
;
141 __raw_writel(1 << GPIO_BIT(gpio
), GPIO_INT_CLR(gpio
));
144 static void tegra_gpio_irq_mask(unsigned int irq
)
146 int gpio
= irq
- INT_GPIO_BASE
;
148 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 0);
151 static void tegra_gpio_irq_unmask(unsigned int irq
)
153 int gpio
= irq
- INT_GPIO_BASE
;
155 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 1);
158 static int tegra_gpio_irq_set_type(unsigned int irq
, unsigned int type
)
160 int gpio
= irq
- INT_GPIO_BASE
;
161 struct tegra_gpio_bank
*bank
= get_irq_chip_data(irq
);
162 int port
= GPIO_PORT(gpio
);
167 switch (type
& IRQ_TYPE_SENSE_MASK
) {
168 case IRQ_TYPE_EDGE_RISING
:
169 lvl_type
= GPIO_INT_LVL_EDGE_RISING
;
172 case IRQ_TYPE_EDGE_FALLING
:
173 lvl_type
= GPIO_INT_LVL_EDGE_FALLING
;
176 case IRQ_TYPE_EDGE_BOTH
:
177 lvl_type
= GPIO_INT_LVL_EDGE_BOTH
;
180 case IRQ_TYPE_LEVEL_HIGH
:
181 lvl_type
= GPIO_INT_LVL_LEVEL_HIGH
;
184 case IRQ_TYPE_LEVEL_LOW
:
185 lvl_type
= GPIO_INT_LVL_LEVEL_LOW
;
192 spin_lock_irqsave(&bank
->lvl_lock
[port
], flags
);
194 val
= __raw_readl(GPIO_INT_LVL(gpio
));
195 val
&= ~(GPIO_INT_LVL_MASK
<< GPIO_BIT(gpio
));
196 val
|= lvl_type
<< GPIO_BIT(gpio
);
197 __raw_writel(val
, GPIO_INT_LVL(gpio
));
199 spin_unlock_irqrestore(&bank
->lvl_lock
[port
], flags
);
201 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
202 __set_irq_handler_unlocked(irq
, handle_level_irq
);
203 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
204 __set_irq_handler_unlocked(irq
, handle_edge_irq
);
209 static void tegra_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
211 struct tegra_gpio_bank
*bank
;
216 desc
->chip
->ack(irq
);
218 bank
= get_irq_data(irq
);
220 for (port
= 0; port
< 4; port
++) {
221 int gpio
= tegra_gpio_compose(bank
->bank
, port
, 0);
222 unsigned long sta
= __raw_readl(GPIO_INT_STA(gpio
)) &
223 __raw_readl(GPIO_INT_ENB(gpio
));
224 u32 lvl
= __raw_readl(GPIO_INT_LVL(gpio
));
226 for_each_set_bit(pin
, &sta
, 8) {
227 __raw_writel(1 << pin
, GPIO_INT_CLR(gpio
));
229 /* if gpio is edge triggered, clear condition
230 * before executing the hander so that we don't
233 if (lvl
& (0x100 << pin
)) {
235 desc
->chip
->unmask(irq
);
238 generic_handle_irq(gpio_to_irq(gpio
+ pin
));
243 desc
->chip
->unmask(irq
);
248 static struct irq_chip tegra_gpio_irq_chip
= {
250 .ack
= tegra_gpio_irq_ack
,
251 .mask
= tegra_gpio_irq_mask
,
252 .unmask
= tegra_gpio_irq_unmask
,
253 .set_type
= tegra_gpio_irq_set_type
,
257 /* This lock class tells lockdep that GPIO irqs are in a different
258 * category than their parents, so it won't report false recursion.
260 static struct lock_class_key gpio_lock_class
;
262 static int __init
tegra_gpio_init(void)
264 struct tegra_gpio_bank
*bank
;
268 for (i
= 0; i
< 7; i
++) {
269 for (j
= 0; j
< 4; j
++) {
270 int gpio
= tegra_gpio_compose(i
, j
, 0);
271 __raw_writel(0x00, GPIO_INT_ENB(gpio
));
275 gpiochip_add(&tegra_gpio_chip
);
277 for (i
= INT_GPIO_BASE
; i
< (INT_GPIO_BASE
+ ARCH_NR_GPIOS
); i
++) {
278 bank
= &tegra_gpio_banks
[GPIO_BANK(irq_to_gpio(i
))];
280 lockdep_set_class(&irq_desc
[i
].lock
, &gpio_lock_class
);
281 set_irq_chip_data(i
, bank
);
282 set_irq_chip(i
, &tegra_gpio_irq_chip
);
283 set_irq_handler(i
, handle_simple_irq
);
284 set_irq_flags(i
, IRQF_VALID
);
287 for (i
= 0; i
< ARRAY_SIZE(tegra_gpio_banks
); i
++) {
288 bank
= &tegra_gpio_banks
[i
];
290 set_irq_chained_handler(bank
->irq
, tegra_gpio_irq_handler
);
291 set_irq_data(bank
->irq
, bank
);
293 for (j
= 0; j
< 4; j
++)
294 spin_lock_init(&bank
->lvl_lock
[j
]);
300 postcore_initcall(tegra_gpio_init
);
302 #ifdef CONFIG_DEBUG_FS
304 #include <linux/debugfs.h>
305 #include <linux/seq_file.h>
307 static int dbg_gpio_show(struct seq_file
*s
, void *unused
)
312 for (i
= 0; i
< 7; i
++) {
313 for (j
= 0; j
< 4; j
++) {
314 int gpio
= tegra_gpio_compose(i
, j
, 0);
315 seq_printf(s
, "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
317 __raw_readl(GPIO_CNF(gpio
)),
318 __raw_readl(GPIO_OE(gpio
)),
319 __raw_readl(GPIO_OUT(gpio
)),
320 __raw_readl(GPIO_IN(gpio
)),
321 __raw_readl(GPIO_INT_STA(gpio
)),
322 __raw_readl(GPIO_INT_ENB(gpio
)),
323 __raw_readl(GPIO_INT_LVL(gpio
)));
329 static int dbg_gpio_open(struct inode
*inode
, struct file
*file
)
331 return single_open(file
, dbg_gpio_show
, &inode
->i_private
);
334 static const struct file_operations debug_fops
= {
335 .open
= dbg_gpio_open
,
338 .release
= single_release
,
341 static int __init
tegra_gpio_debuginit(void)
343 (void) debugfs_create_file("tegra_gpio", S_IRUGO
,
344 NULL
, NULL
, &debug_fops
);
347 late_initcall(tegra_gpio_debuginit
);