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>
22 #include <linux/interrupt.h>
24 #include <linux/gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/module.h>
28 #include <linux/irqdomain.h>
30 #include <asm/mach/irq.h>
32 #include <mach/gpio-tegra.h>
33 #include <mach/iomap.h>
34 #include <mach/suspend.h>
36 #define GPIO_BANK(x) ((x) >> 5)
37 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
38 #define GPIO_BIT(x) ((x) & 0x7)
40 #define GPIO_REG(x) (GPIO_BANK(x) * 0x80 + GPIO_PORT(x) * 4)
42 #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
43 #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
44 #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
45 #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
46 #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
47 #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
48 #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
49 #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
51 #define GPIO_MSK_CNF(x) (GPIO_REG(x) + 0x800)
52 #define GPIO_MSK_OE(x) (GPIO_REG(x) + 0x810)
53 #define GPIO_MSK_OUT(x) (GPIO_REG(x) + 0X820)
54 #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + 0x840)
55 #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + 0x850)
56 #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + 0x860)
58 #define GPIO_INT_LVL_MASK 0x010101
59 #define GPIO_INT_LVL_EDGE_RISING 0x000101
60 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
61 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
62 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
63 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
65 struct tegra_gpio_bank
{
68 spinlock_t lvl_lock
[4];
78 static struct irq_domain
*irq_domain
;
79 static void __iomem
*regs
;
80 static u32 tegra_gpio_bank_count
;
81 static struct tegra_gpio_bank
*tegra_gpio_banks
;
83 static inline void tegra_gpio_writel(u32 val
, u32 reg
)
85 __raw_writel(val
, regs
+ reg
);
88 static inline u32
tegra_gpio_readl(u32 reg
)
90 return __raw_readl(regs
+ reg
);
93 static int tegra_gpio_compose(int bank
, int port
, int bit
)
95 return (bank
<< 5) | ((port
& 0x3) << 3) | (bit
& 0x7);
98 static void tegra_gpio_mask_write(u32 reg
, int gpio
, int value
)
102 val
= 0x100 << GPIO_BIT(gpio
);
104 val
|= 1 << GPIO_BIT(gpio
);
105 tegra_gpio_writel(val
, reg
);
108 void tegra_gpio_enable(int gpio
)
110 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 1);
113 void tegra_gpio_disable(int gpio
)
115 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 0);
118 static void tegra_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
120 tegra_gpio_mask_write(GPIO_MSK_OUT(offset
), offset
, value
);
123 static int tegra_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
125 return (tegra_gpio_readl(GPIO_IN(offset
)) >> GPIO_BIT(offset
)) & 0x1;
128 static int tegra_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
130 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 0);
134 static int tegra_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
137 tegra_gpio_set(chip
, offset
, value
);
138 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 1);
142 static int tegra_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
144 return irq_find_mapping(irq_domain
, offset
);
147 static struct gpio_chip tegra_gpio_chip
= {
148 .label
= "tegra-gpio",
149 .direction_input
= tegra_gpio_direction_input
,
150 .get
= tegra_gpio_get
,
151 .direction_output
= tegra_gpio_direction_output
,
152 .set
= tegra_gpio_set
,
153 .to_irq
= tegra_gpio_to_irq
,
155 .ngpio
= TEGRA_NR_GPIOS
,
158 static void tegra_gpio_irq_ack(struct irq_data
*d
)
162 tegra_gpio_writel(1 << GPIO_BIT(gpio
), GPIO_INT_CLR(gpio
));
165 static void tegra_gpio_irq_mask(struct irq_data
*d
)
169 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 0);
172 static void tegra_gpio_irq_unmask(struct irq_data
*d
)
176 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 1);
179 static int tegra_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
182 struct tegra_gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
183 int port
= GPIO_PORT(gpio
);
188 switch (type
& IRQ_TYPE_SENSE_MASK
) {
189 case IRQ_TYPE_EDGE_RISING
:
190 lvl_type
= GPIO_INT_LVL_EDGE_RISING
;
193 case IRQ_TYPE_EDGE_FALLING
:
194 lvl_type
= GPIO_INT_LVL_EDGE_FALLING
;
197 case IRQ_TYPE_EDGE_BOTH
:
198 lvl_type
= GPIO_INT_LVL_EDGE_BOTH
;
201 case IRQ_TYPE_LEVEL_HIGH
:
202 lvl_type
= GPIO_INT_LVL_LEVEL_HIGH
;
205 case IRQ_TYPE_LEVEL_LOW
:
206 lvl_type
= GPIO_INT_LVL_LEVEL_LOW
;
213 spin_lock_irqsave(&bank
->lvl_lock
[port
], flags
);
215 val
= tegra_gpio_readl(GPIO_INT_LVL(gpio
));
216 val
&= ~(GPIO_INT_LVL_MASK
<< GPIO_BIT(gpio
));
217 val
|= lvl_type
<< GPIO_BIT(gpio
);
218 tegra_gpio_writel(val
, GPIO_INT_LVL(gpio
));
220 spin_unlock_irqrestore(&bank
->lvl_lock
[port
], flags
);
222 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
223 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
224 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
225 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
230 static void tegra_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
232 struct tegra_gpio_bank
*bank
;
236 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
238 chained_irq_enter(chip
, desc
);
240 bank
= irq_get_handler_data(irq
);
242 for (port
= 0; port
< 4; port
++) {
243 int gpio
= tegra_gpio_compose(bank
->bank
, port
, 0);
244 unsigned long sta
= tegra_gpio_readl(GPIO_INT_STA(gpio
)) &
245 tegra_gpio_readl(GPIO_INT_ENB(gpio
));
246 u32 lvl
= tegra_gpio_readl(GPIO_INT_LVL(gpio
));
248 for_each_set_bit(pin
, &sta
, 8) {
249 tegra_gpio_writel(1 << pin
, GPIO_INT_CLR(gpio
));
251 /* if gpio is edge triggered, clear condition
252 * before executing the hander so that we don't
255 if (lvl
& (0x100 << pin
)) {
257 chained_irq_exit(chip
, desc
);
260 generic_handle_irq(gpio_to_irq(gpio
+ pin
));
265 chained_irq_exit(chip
, desc
);
270 void tegra_gpio_resume(void)
276 local_irq_save(flags
);
278 for (b
= 0; b
< tegra_gpio_bank_count
; b
++) {
279 struct tegra_gpio_bank
*bank
= &tegra_gpio_banks
[b
];
281 for (p
= 0; p
< ARRAY_SIZE(bank
->oe
); p
++) {
282 unsigned int gpio
= (b
<<5) | (p
<<3);
283 tegra_gpio_writel(bank
->cnf
[p
], GPIO_CNF(gpio
));
284 tegra_gpio_writel(bank
->out
[p
], GPIO_OUT(gpio
));
285 tegra_gpio_writel(bank
->oe
[p
], GPIO_OE(gpio
));
286 tegra_gpio_writel(bank
->int_lvl
[p
], GPIO_INT_LVL(gpio
));
287 tegra_gpio_writel(bank
->int_enb
[p
], GPIO_INT_ENB(gpio
));
291 local_irq_restore(flags
);
294 void tegra_gpio_suspend(void)
300 local_irq_save(flags
);
301 for (b
= 0; b
< tegra_gpio_bank_count
; b
++) {
302 struct tegra_gpio_bank
*bank
= &tegra_gpio_banks
[b
];
304 for (p
= 0; p
< ARRAY_SIZE(bank
->oe
); p
++) {
305 unsigned int gpio
= (b
<<5) | (p
<<3);
306 bank
->cnf
[p
] = tegra_gpio_readl(GPIO_CNF(gpio
));
307 bank
->out
[p
] = tegra_gpio_readl(GPIO_OUT(gpio
));
308 bank
->oe
[p
] = tegra_gpio_readl(GPIO_OE(gpio
));
309 bank
->int_enb
[p
] = tegra_gpio_readl(GPIO_INT_ENB(gpio
));
310 bank
->int_lvl
[p
] = tegra_gpio_readl(GPIO_INT_LVL(gpio
));
313 local_irq_restore(flags
);
316 static int tegra_gpio_wake_enable(struct irq_data
*d
, unsigned int enable
)
318 struct tegra_gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
319 return irq_set_irq_wake(bank
->irq
, enable
);
323 static struct irq_chip tegra_gpio_irq_chip
= {
325 .irq_ack
= tegra_gpio_irq_ack
,
326 .irq_mask
= tegra_gpio_irq_mask
,
327 .irq_unmask
= tegra_gpio_irq_unmask
,
328 .irq_set_type
= tegra_gpio_irq_set_type
,
330 .irq_set_wake
= tegra_gpio_wake_enable
,
335 /* This lock class tells lockdep that GPIO irqs are in a different
336 * category than their parents, so it won't report false recursion.
338 static struct lock_class_key gpio_lock_class
;
340 static int __devinit
tegra_gpio_probe(struct platform_device
*pdev
)
343 struct resource
*res
;
344 struct tegra_gpio_bank
*bank
;
350 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, tegra_gpio_bank_count
);
353 tegra_gpio_bank_count
++;
355 if (!tegra_gpio_bank_count
) {
356 dev_err(&pdev
->dev
, "Missing IRQ resource\n");
360 tegra_gpio_chip
.ngpio
= tegra_gpio_bank_count
* 32;
362 tegra_gpio_banks
= devm_kzalloc(&pdev
->dev
,
363 tegra_gpio_bank_count
* sizeof(*tegra_gpio_banks
),
365 if (!tegra_gpio_banks
) {
366 dev_err(&pdev
->dev
, "Couldn't allocate bank structure\n");
370 irq_base
= irq_alloc_descs(-1, 0, tegra_gpio_chip
.ngpio
, 0);
372 dev_err(&pdev
->dev
, "Couldn't allocate IRQ numbers\n");
375 irq_domain
= irq_domain_add_legacy(pdev
->dev
.of_node
,
376 tegra_gpio_chip
.ngpio
, irq_base
, 0,
377 &irq_domain_simple_ops
, NULL
);
379 for (i
= 0; i
< tegra_gpio_bank_count
; i
++) {
380 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
382 dev_err(&pdev
->dev
, "Missing IRQ resource\n");
386 bank
= &tegra_gpio_banks
[i
];
388 bank
->irq
= res
->start
;
391 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
393 dev_err(&pdev
->dev
, "Missing MEM resource\n");
397 regs
= devm_request_and_ioremap(&pdev
->dev
, res
);
399 dev_err(&pdev
->dev
, "Couldn't ioremap regs\n");
403 for (i
= 0; i
< 7; i
++) {
404 for (j
= 0; j
< 4; j
++) {
405 int gpio
= tegra_gpio_compose(i
, j
, 0);
406 tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio
));
410 #ifdef CONFIG_OF_GPIO
411 tegra_gpio_chip
.of_node
= pdev
->dev
.of_node
;
414 gpiochip_add(&tegra_gpio_chip
);
416 for (gpio
= 0; gpio
< tegra_gpio_chip
.ngpio
; gpio
++) {
417 int irq
= irq_find_mapping(irq_domain
, gpio
);
418 /* No validity check; all Tegra GPIOs are valid IRQs */
420 bank
= &tegra_gpio_banks
[GPIO_BANK(gpio
)];
422 irq_set_lockdep_class(irq
, &gpio_lock_class
);
423 irq_set_chip_data(irq
, bank
);
424 irq_set_chip_and_handler(irq
, &tegra_gpio_irq_chip
,
426 set_irq_flags(irq
, IRQF_VALID
);
429 for (i
= 0; i
< tegra_gpio_bank_count
; i
++) {
430 bank
= &tegra_gpio_banks
[i
];
432 irq_set_chained_handler(bank
->irq
, tegra_gpio_irq_handler
);
433 irq_set_handler_data(bank
->irq
, bank
);
435 for (j
= 0; j
< 4; j
++)
436 spin_lock_init(&bank
->lvl_lock
[j
]);
442 static struct of_device_id tegra_gpio_of_match
[] __devinitdata
= {
443 { .compatible
= "nvidia,tegra20-gpio", },
447 static struct platform_driver tegra_gpio_driver
= {
449 .name
= "tegra-gpio",
450 .owner
= THIS_MODULE
,
451 .of_match_table
= tegra_gpio_of_match
,
453 .probe
= tegra_gpio_probe
,
456 static int __init
tegra_gpio_init(void)
458 return platform_driver_register(&tegra_gpio_driver
);
460 postcore_initcall(tegra_gpio_init
);
462 void __init
tegra_gpio_config(struct tegra_gpio_table
*table
, int num
)
466 for (i
= 0; i
< num
; i
++) {
467 int gpio
= table
[i
].gpio
;
470 tegra_gpio_enable(gpio
);
472 tegra_gpio_disable(gpio
);
476 #ifdef CONFIG_DEBUG_FS
478 #include <linux/debugfs.h>
479 #include <linux/seq_file.h>
481 static int dbg_gpio_show(struct seq_file
*s
, void *unused
)
486 for (i
= 0; i
< 7; i
++) {
487 for (j
= 0; j
< 4; j
++) {
488 int gpio
= tegra_gpio_compose(i
, j
, 0);
490 "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
492 tegra_gpio_readl(GPIO_CNF(gpio
)),
493 tegra_gpio_readl(GPIO_OE(gpio
)),
494 tegra_gpio_readl(GPIO_OUT(gpio
)),
495 tegra_gpio_readl(GPIO_IN(gpio
)),
496 tegra_gpio_readl(GPIO_INT_STA(gpio
)),
497 tegra_gpio_readl(GPIO_INT_ENB(gpio
)),
498 tegra_gpio_readl(GPIO_INT_LVL(gpio
)));
504 static int dbg_gpio_open(struct inode
*inode
, struct file
*file
)
506 return single_open(file
, dbg_gpio_show
, &inode
->i_private
);
509 static const struct file_operations debug_fops
= {
510 .open
= dbg_gpio_open
,
513 .release
= single_release
,
516 static int __init
tegra_gpio_debuginit(void)
518 (void) debugfs_create_file("tegra_gpio", S_IRUGO
,
519 NULL
, NULL
, &debug_fops
);
522 late_initcall(tegra_gpio_debuginit
);