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>
29 #include <asm/mach/irq.h>
31 #include <mach/gpio-tegra.h>
32 #include <mach/iomap.h>
33 #include <mach/suspend.h>
35 #define GPIO_BANK(x) ((x) >> 5)
36 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
37 #define GPIO_BIT(x) ((x) & 0x7)
39 #define GPIO_REG(x) (GPIO_BANK(x) * 0x80 + GPIO_PORT(x) * 4)
41 #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
42 #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
43 #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
44 #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
45 #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
46 #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
47 #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
48 #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
50 #define GPIO_MSK_CNF(x) (GPIO_REG(x) + 0x800)
51 #define GPIO_MSK_OE(x) (GPIO_REG(x) + 0x810)
52 #define GPIO_MSK_OUT(x) (GPIO_REG(x) + 0X820)
53 #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + 0x840)
54 #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + 0x850)
55 #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + 0x860)
57 #define GPIO_INT_LVL_MASK 0x010101
58 #define GPIO_INT_LVL_EDGE_RISING 0x000101
59 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
60 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
61 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
62 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
64 struct tegra_gpio_bank
{
67 spinlock_t lvl_lock
[4];
78 static void __iomem
*regs
;
79 static struct tegra_gpio_bank tegra_gpio_banks
[7];
81 static inline void tegra_gpio_writel(u32 val
, u32 reg
)
83 __raw_writel(val
, regs
+ reg
);
86 static inline u32
tegra_gpio_readl(u32 reg
)
88 return __raw_readl(regs
+ reg
);
91 static int tegra_gpio_compose(int bank
, int port
, int bit
)
93 return (bank
<< 5) | ((port
& 0x3) << 3) | (bit
& 0x7);
96 static void tegra_gpio_mask_write(u32 reg
, int gpio
, int value
)
100 val
= 0x100 << GPIO_BIT(gpio
);
102 val
|= 1 << GPIO_BIT(gpio
);
103 tegra_gpio_writel(val
, reg
);
106 void tegra_gpio_enable(int gpio
)
108 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 1);
111 void tegra_gpio_disable(int gpio
)
113 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio
), gpio
, 0);
116 static void tegra_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
118 tegra_gpio_mask_write(GPIO_MSK_OUT(offset
), offset
, value
);
121 static int tegra_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
123 return (tegra_gpio_readl(GPIO_IN(offset
)) >> GPIO_BIT(offset
)) & 0x1;
126 static int tegra_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
128 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 0);
132 static int tegra_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
135 tegra_gpio_set(chip
, offset
, value
);
136 tegra_gpio_mask_write(GPIO_MSK_OE(offset
), offset
, 1);
140 static int tegra_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
142 return TEGRA_GPIO_TO_IRQ(offset
);
145 static struct gpio_chip tegra_gpio_chip
= {
146 .label
= "tegra-gpio",
147 .direction_input
= tegra_gpio_direction_input
,
148 .get
= tegra_gpio_get
,
149 .direction_output
= tegra_gpio_direction_output
,
150 .set
= tegra_gpio_set
,
151 .to_irq
= tegra_gpio_to_irq
,
153 .ngpio
= TEGRA_NR_GPIOS
,
156 static void tegra_gpio_irq_ack(struct irq_data
*d
)
158 int gpio
= d
->irq
- INT_GPIO_BASE
;
160 tegra_gpio_writel(1 << GPIO_BIT(gpio
), GPIO_INT_CLR(gpio
));
163 static void tegra_gpio_irq_mask(struct irq_data
*d
)
165 int gpio
= d
->irq
- INT_GPIO_BASE
;
167 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 0);
170 static void tegra_gpio_irq_unmask(struct irq_data
*d
)
172 int gpio
= d
->irq
- INT_GPIO_BASE
;
174 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio
), gpio
, 1);
177 static int tegra_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
179 int gpio
= d
->irq
- INT_GPIO_BASE
;
180 struct tegra_gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
181 int port
= GPIO_PORT(gpio
);
186 switch (type
& IRQ_TYPE_SENSE_MASK
) {
187 case IRQ_TYPE_EDGE_RISING
:
188 lvl_type
= GPIO_INT_LVL_EDGE_RISING
;
191 case IRQ_TYPE_EDGE_FALLING
:
192 lvl_type
= GPIO_INT_LVL_EDGE_FALLING
;
195 case IRQ_TYPE_EDGE_BOTH
:
196 lvl_type
= GPIO_INT_LVL_EDGE_BOTH
;
199 case IRQ_TYPE_LEVEL_HIGH
:
200 lvl_type
= GPIO_INT_LVL_LEVEL_HIGH
;
203 case IRQ_TYPE_LEVEL_LOW
:
204 lvl_type
= GPIO_INT_LVL_LEVEL_LOW
;
211 spin_lock_irqsave(&bank
->lvl_lock
[port
], flags
);
213 val
= tegra_gpio_readl(GPIO_INT_LVL(gpio
));
214 val
&= ~(GPIO_INT_LVL_MASK
<< GPIO_BIT(gpio
));
215 val
|= lvl_type
<< GPIO_BIT(gpio
);
216 tegra_gpio_writel(val
, GPIO_INT_LVL(gpio
));
218 spin_unlock_irqrestore(&bank
->lvl_lock
[port
], flags
);
220 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
221 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
222 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
223 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
228 static void tegra_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
230 struct tegra_gpio_bank
*bank
;
234 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
236 chained_irq_enter(chip
, desc
);
238 bank
= irq_get_handler_data(irq
);
240 for (port
= 0; port
< 4; port
++) {
241 int gpio
= tegra_gpio_compose(bank
->bank
, port
, 0);
242 unsigned long sta
= tegra_gpio_readl(GPIO_INT_STA(gpio
)) &
243 tegra_gpio_readl(GPIO_INT_ENB(gpio
));
244 u32 lvl
= tegra_gpio_readl(GPIO_INT_LVL(gpio
));
246 for_each_set_bit(pin
, &sta
, 8) {
247 tegra_gpio_writel(1 << pin
, GPIO_INT_CLR(gpio
));
249 /* if gpio is edge triggered, clear condition
250 * before executing the hander so that we don't
253 if (lvl
& (0x100 << pin
)) {
255 chained_irq_exit(chip
, desc
);
258 generic_handle_irq(gpio_to_irq(gpio
+ pin
));
263 chained_irq_exit(chip
, desc
);
268 void tegra_gpio_resume(void)
274 local_irq_save(flags
);
276 for (b
= 0; b
< ARRAY_SIZE(tegra_gpio_banks
); b
++) {
277 struct tegra_gpio_bank
*bank
= &tegra_gpio_banks
[b
];
279 for (p
= 0; p
< ARRAY_SIZE(bank
->oe
); p
++) {
280 unsigned int gpio
= (b
<<5) | (p
<<3);
281 tegra_gpio_writel(bank
->cnf
[p
], GPIO_CNF(gpio
));
282 tegra_gpio_writel(bank
->out
[p
], GPIO_OUT(gpio
));
283 tegra_gpio_writel(bank
->oe
[p
], GPIO_OE(gpio
));
284 tegra_gpio_writel(bank
->int_lvl
[p
], GPIO_INT_LVL(gpio
));
285 tegra_gpio_writel(bank
->int_enb
[p
], GPIO_INT_ENB(gpio
));
289 local_irq_restore(flags
);
292 void tegra_gpio_suspend(void)
298 local_irq_save(flags
);
299 for (b
= 0; b
< ARRAY_SIZE(tegra_gpio_banks
); b
++) {
300 struct tegra_gpio_bank
*bank
= &tegra_gpio_banks
[b
];
302 for (p
= 0; p
< ARRAY_SIZE(bank
->oe
); p
++) {
303 unsigned int gpio
= (b
<<5) | (p
<<3);
304 bank
->cnf
[p
] = tegra_gpio_readl(GPIO_CNF(gpio
));
305 bank
->out
[p
] = tegra_gpio_readl(GPIO_OUT(gpio
));
306 bank
->oe
[p
] = tegra_gpio_readl(GPIO_OE(gpio
));
307 bank
->int_enb
[p
] = tegra_gpio_readl(GPIO_INT_ENB(gpio
));
308 bank
->int_lvl
[p
] = tegra_gpio_readl(GPIO_INT_LVL(gpio
));
311 local_irq_restore(flags
);
314 static int tegra_gpio_wake_enable(struct irq_data
*d
, unsigned int enable
)
316 struct tegra_gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
317 return irq_set_irq_wake(bank
->irq
, enable
);
321 static struct irq_chip tegra_gpio_irq_chip
= {
323 .irq_ack
= tegra_gpio_irq_ack
,
324 .irq_mask
= tegra_gpio_irq_mask
,
325 .irq_unmask
= tegra_gpio_irq_unmask
,
326 .irq_set_type
= tegra_gpio_irq_set_type
,
328 .irq_set_wake
= tegra_gpio_wake_enable
,
333 /* This lock class tells lockdep that GPIO irqs are in a different
334 * category than their parents, so it won't report false recursion.
336 static struct lock_class_key gpio_lock_class
;
338 static int __devinit
tegra_gpio_probe(struct platform_device
*pdev
)
340 struct resource
*res
;
341 struct tegra_gpio_bank
*bank
;
346 for (i
= 0; i
< ARRAY_SIZE(tegra_gpio_banks
); i
++) {
347 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
349 dev_err(&pdev
->dev
, "Missing IRQ resource\n");
353 bank
= &tegra_gpio_banks
[i
];
355 bank
->irq
= res
->start
;
358 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
360 dev_err(&pdev
->dev
, "Missing MEM resource\n");
364 regs
= devm_request_and_ioremap(&pdev
->dev
, res
);
366 dev_err(&pdev
->dev
, "Couldn't ioremap regs\n");
370 for (i
= 0; i
< 7; i
++) {
371 for (j
= 0; j
< 4; j
++) {
372 int gpio
= tegra_gpio_compose(i
, j
, 0);
373 tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio
));
377 #ifdef CONFIG_OF_GPIO
378 tegra_gpio_chip
.of_node
= pdev
->dev
.of_node
;
381 gpiochip_add(&tegra_gpio_chip
);
383 for (gpio
= 0; gpio
< TEGRA_NR_GPIOS
; gpio
++) {
384 int irq
= TEGRA_GPIO_TO_IRQ(gpio
);
385 /* No validity check; all Tegra GPIOs are valid IRQs */
387 bank
= &tegra_gpio_banks
[GPIO_BANK(gpio
)];
389 irq_set_lockdep_class(irq
, &gpio_lock_class
);
390 irq_set_chip_data(irq
, bank
);
391 irq_set_chip_and_handler(irq
, &tegra_gpio_irq_chip
,
393 set_irq_flags(irq
, IRQF_VALID
);
396 for (i
= 0; i
< ARRAY_SIZE(tegra_gpio_banks
); i
++) {
397 bank
= &tegra_gpio_banks
[i
];
399 irq_set_chained_handler(bank
->irq
, tegra_gpio_irq_handler
);
400 irq_set_handler_data(bank
->irq
, bank
);
402 for (j
= 0; j
< 4; j
++)
403 spin_lock_init(&bank
->lvl_lock
[j
]);
409 static struct of_device_id tegra_gpio_of_match
[] __devinitdata
= {
410 { .compatible
= "nvidia,tegra20-gpio", },
414 static struct platform_driver tegra_gpio_driver
= {
416 .name
= "tegra-gpio",
417 .owner
= THIS_MODULE
,
418 .of_match_table
= tegra_gpio_of_match
,
420 .probe
= tegra_gpio_probe
,
423 static int __init
tegra_gpio_init(void)
425 return platform_driver_register(&tegra_gpio_driver
);
427 postcore_initcall(tegra_gpio_init
);
429 void __init
tegra_gpio_config(struct tegra_gpio_table
*table
, int num
)
433 for (i
= 0; i
< num
; i
++) {
434 int gpio
= table
[i
].gpio
;
437 tegra_gpio_enable(gpio
);
439 tegra_gpio_disable(gpio
);
443 #ifdef CONFIG_DEBUG_FS
445 #include <linux/debugfs.h>
446 #include <linux/seq_file.h>
448 static int dbg_gpio_show(struct seq_file
*s
, void *unused
)
453 for (i
= 0; i
< 7; i
++) {
454 for (j
= 0; j
< 4; j
++) {
455 int gpio
= tegra_gpio_compose(i
, j
, 0);
457 "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
459 tegra_gpio_readl(GPIO_CNF(gpio
)),
460 tegra_gpio_readl(GPIO_OE(gpio
)),
461 tegra_gpio_readl(GPIO_OUT(gpio
)),
462 tegra_gpio_readl(GPIO_IN(gpio
)),
463 tegra_gpio_readl(GPIO_INT_STA(gpio
)),
464 tegra_gpio_readl(GPIO_INT_ENB(gpio
)),
465 tegra_gpio_readl(GPIO_INT_LVL(gpio
)));
471 static int dbg_gpio_open(struct inode
*inode
, struct file
*file
)
473 return single_open(file
, dbg_gpio_show
, &inode
->i_private
);
476 static const struct file_operations debug_fops
= {
477 .open
= dbg_gpio_open
,
480 .release
= single_release
,
483 static int __init
tegra_gpio_debuginit(void)
485 (void) debugfs_create_file("tegra_gpio", S_IRUGO
,
486 NULL
, NULL
, &debug_fops
);
489 late_initcall(tegra_gpio_debuginit
);