2 * TI DaVinci GPIO Support
4 * Copyright (c) 2006-2007 David Brownell
5 * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
20 #include <linux/irq.h>
21 #include <linux/bitops.h>
23 #include <mach/cputype.h>
24 #include <mach/irqs.h>
25 #include <mach/hardware.h>
26 #include <mach/gpio.h>
28 #include <asm/mach/irq.h>
31 static DEFINE_SPINLOCK(gpio_lock);
34 struct gpio_chip chip;
35 struct gpio_controller *__iomem regs;
38 static struct davinci_gpio chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
40 static unsigned __initdata ngpio;
42 /* create a non-inlined version */
43 static struct gpio_controller __iomem * __init gpio2controller(unsigned gpio)
45 return __gpio_to_controller(gpio);
49 /*--------------------------------------------------------------------------*/
52 * board setup code *MUST* set PINMUX0 and PINMUX1 as
53 * needed, and enable the GPIO clock.
56 static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
58 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
59 struct gpio_controller *__iomem g = d->regs;
62 spin_lock(&gpio_lock);
63 temp = __raw_readl(&g->dir);
64 temp |= (1 << offset);
65 __raw_writel(temp, &g->dir);
66 spin_unlock(&gpio_lock);
72 * Read the pin's value (works even if it's set up as output);
73 * returns zero/nonzero.
75 * Note that changes are synched to the GPIO clock, so reading values back
76 * right after you've set them may give old values.
78 static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
80 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
81 struct gpio_controller *__iomem g = d->regs;
83 return (1 << offset) & __raw_readl(&g->in_data);
87 davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
89 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
90 struct gpio_controller *__iomem g = d->regs;
92 u32 mask = 1 << offset;
94 spin_lock(&gpio_lock);
95 temp = __raw_readl(&g->dir);
97 __raw_writel(mask, value ? &g->set_data : &g->clr_data);
98 __raw_writel(temp, &g->dir);
99 spin_unlock(&gpio_lock);
104 * Assuming the pin is muxed as a gpio output, set its output value.
107 davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
109 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
110 struct gpio_controller *__iomem g = d->regs;
112 __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
115 static int __init davinci_gpio_setup(void)
119 /* The gpio banks conceptually expose a segmented bitmap,
120 * and "ngpio" is one more than the largest zero-based
121 * bit index that's valid.
123 if (cpu_is_davinci_dm355()) { /* or dm335() */
125 } else if (cpu_is_davinci_dm644x()) { /* or dm337() */
127 } else if (cpu_is_davinci_dm646x()) {
128 /* NOTE: each bank has several "reserved" bits,
129 * unusable as GPIOs. Only 33 of the GPIO numbers
130 * are usable, and we're not rejecting the others.
134 /* if cpu_is_davinci_dm643x() ngpio = 111 */
135 pr_err("GPIO setup: how many GPIOs?\n");
139 if (WARN_ON(DAVINCI_N_GPIO < ngpio))
140 ngpio = DAVINCI_N_GPIO;
142 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
143 chips[i].chip.label = "DaVinci";
145 chips[i].chip.direction_input = davinci_direction_in;
146 chips[i].chip.get = davinci_gpio_get;
147 chips[i].chip.direction_output = davinci_direction_out;
148 chips[i].chip.set = davinci_gpio_set;
150 chips[i].chip.base = base;
151 chips[i].chip.ngpio = ngpio - base;
152 if (chips[i].chip.ngpio > 32)
153 chips[i].chip.ngpio = 32;
155 chips[i].regs = gpio2controller(base);
157 gpiochip_add(&chips[i].chip);
162 pure_initcall(davinci_gpio_setup);
164 /*--------------------------------------------------------------------------*/
166 * We expect irqs will normally be set up as input pins, but they can also be
167 * used as output pins ... which is convenient for testing.
169 * NOTE: The first few GPIOs also have direct INTC hookups in addition
170 * to their GPIOBNK0 irq, with a bit less overhead but less flexibility
171 * on triggering (e.g. no edge options). We don't try to use those.
173 * All those INTC hookups (direct, plus several IRQ banks) can also
174 * serve as EDMA event triggers.
177 static void gpio_irq_disable(unsigned irq)
179 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
180 u32 mask = __gpio_mask(irq_to_gpio(irq));
182 __raw_writel(mask, &g->clr_falling);
183 __raw_writel(mask, &g->clr_rising);
186 static void gpio_irq_enable(unsigned irq)
188 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
189 u32 mask = __gpio_mask(irq_to_gpio(irq));
191 if (irq_desc[irq].status & IRQ_TYPE_EDGE_FALLING)
192 __raw_writel(mask, &g->set_falling);
193 if (irq_desc[irq].status & IRQ_TYPE_EDGE_RISING)
194 __raw_writel(mask, &g->set_rising);
197 static int gpio_irq_type(unsigned irq, unsigned trigger)
199 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
200 u32 mask = __gpio_mask(irq_to_gpio(irq));
202 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
205 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
206 irq_desc[irq].status |= trigger;
208 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
209 ? &g->set_falling : &g->clr_falling);
210 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
211 ? &g->set_rising : &g->clr_rising);
215 static struct irq_chip gpio_irqchip = {
217 .enable = gpio_irq_enable,
218 .disable = gpio_irq_disable,
219 .set_type = gpio_irq_type,
223 gpio_irq_handler(unsigned irq, struct irq_desc *desc)
225 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
228 /* we only care about one bank */
232 /* temporarily mask (level sensitive) parent IRQ */
233 desc->chip->ack(irq);
240 status = __raw_readl(&g->intstat) & mask;
243 __raw_writel(status, &g->intstat);
247 /* now demux them to the right lowlevel handler */
248 n = (int)get_irq_data(irq);
252 generic_handle_irq(n - 1);
256 desc->chip->unmask(irq);
257 /* now it may re-trigger */
261 * NOTE: for suspend/resume, probably best to make a platform_device with
262 * suspend_late/resume_resume calls hooking into results of the set_wake()
263 * calls ... so if no gpios are wakeup events the clock can be disabled,
264 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
265 * (dm6446) can be set appropriately for GPIOV33 pins.
268 static int __init davinci_gpio_irq_setup(void)
270 unsigned gpio, irq, bank;
275 if (cpu_is_davinci_dm355()) { /* or dm335() */
276 bank_irq = IRQ_DM355_GPIOBNK0;
277 } else if (cpu_is_davinci_dm644x()) {
278 bank_irq = IRQ_GPIOBNK0;
279 } else if (cpu_is_davinci_dm646x()) {
280 bank_irq = IRQ_DM646X_GPIOBNK0;
282 printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
286 clk = clk_get(NULL, "gpio");
288 printk(KERN_ERR "Error %ld getting gpio clock?\n",
294 for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
296 bank++, bank_irq++) {
297 struct gpio_controller *__iomem g = gpio2controller(gpio);
300 __raw_writel(~0, &g->clr_falling);
301 __raw_writel(~0, &g->clr_rising);
303 /* set up all irqs in this bank */
304 set_irq_chained_handler(bank_irq, gpio_irq_handler);
305 set_irq_chip_data(bank_irq, g);
306 set_irq_data(bank_irq, (void *)irq);
308 for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
309 set_irq_chip(irq, &gpio_irqchip);
310 set_irq_chip_data(irq, g);
311 set_irq_handler(irq, handle_simple_irq);
312 set_irq_flags(irq, IRQF_VALID);
318 /* BINTEN -- per-bank interrupt enable. genirq would also let these
319 * bits be set/cleared dynamically.
321 __raw_writel(binten, (void *__iomem)
322 IO_ADDRESS(DAVINCI_GPIO_BASE + 0x08));
324 printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
328 arch_initcall(davinci_gpio_irq_setup);