blob: f6ea9db11f417bee1882ab32d3ed1b35230b632d [file] [log] [blame]
Vladimir Barinov3d9edf02007-07-10 13:03:43 +01001/*
2 * TI DaVinci GPIO Support
3 *
David Brownelldce11152008-09-07 23:41:04 -07004 * Copyright (c) 2006-2007 David Brownell
Vladimir Barinov3d9edf02007-07-10 13:03:43 +01005 * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
6 *
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.
11 */
12
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>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/bitops.h>
22
David Brownell474dad52008-12-07 11:46:23 -080023#include <mach/cputype.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/irqs.h>
25#include <mach/hardware.h>
Mark A. Greera9949552009-04-15 12:40:35 -070026#include <mach/common.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/gpio.h>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010028
29#include <asm/mach/irq.h>
30
David Brownelldce11152008-09-07 23:41:04 -070031
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010032static DEFINE_SPINLOCK(gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010033
David Brownelldce11152008-09-07 23:41:04 -070034struct davinci_gpio {
35 struct gpio_chip chip;
36 struct gpio_controller *__iomem regs;
David Brownell7a360712009-06-25 17:01:31 -070037 int irq_base;
David Brownelldce11152008-09-07 23:41:04 -070038};
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010039
David Brownelldce11152008-09-07 23:41:04 -070040static struct davinci_gpio chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010041
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010042/* create a non-inlined version */
David Brownell474dad52008-12-07 11:46:23 -080043static struct gpio_controller __iomem * __init gpio2controller(unsigned gpio)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010044{
45 return __gpio_to_controller(gpio);
46}
47
Kevin Hilmandc756022009-05-11 11:04:53 -070048static int __init davinci_gpio_irq_setup(void);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010049
50/*--------------------------------------------------------------------------*/
51
52/*
53 * board setup code *MUST* set PINMUX0 and PINMUX1 as
54 * needed, and enable the GPIO clock.
55 */
56
David Brownelldce11152008-09-07 23:41:04 -070057static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010058{
David Brownelldce11152008-09-07 23:41:04 -070059 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
60 struct gpio_controller *__iomem g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010061 u32 temp;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010062
63 spin_lock(&gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010064 temp = __raw_readl(&g->dir);
David Brownelldce11152008-09-07 23:41:04 -070065 temp |= (1 << offset);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010066 __raw_writel(temp, &g->dir);
67 spin_unlock(&gpio_lock);
David Brownelldce11152008-09-07 23:41:04 -070068
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010069 return 0;
70}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010071
David Brownelldce11152008-09-07 23:41:04 -070072/*
73 * Read the pin's value (works even if it's set up as output);
74 * returns zero/nonzero.
75 *
76 * Note that changes are synched to the GPIO clock, so reading values back
77 * right after you've set them may give old values.
78 */
79static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010080{
David Brownelldce11152008-09-07 23:41:04 -070081 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
82 struct gpio_controller *__iomem g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010083
David Brownelldce11152008-09-07 23:41:04 -070084 return (1 << offset) & __raw_readl(&g->in_data);
85}
86
87static int
88davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
89{
90 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
91 struct gpio_controller *__iomem g = d->regs;
92 u32 temp;
93 u32 mask = 1 << offset;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010094
95 spin_lock(&gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010096 temp = __raw_readl(&g->dir);
97 temp &= ~mask;
98 __raw_writel(mask, value ? &g->set_data : &g->clr_data);
99 __raw_writel(temp, &g->dir);
100 spin_unlock(&gpio_lock);
101 return 0;
102}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100103
104/*
David Brownelldce11152008-09-07 23:41:04 -0700105 * Assuming the pin is muxed as a gpio output, set its output value.
106 */
107static void
108davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
109{
110 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
111 struct gpio_controller *__iomem g = d->regs;
112
113 __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
114}
115
116static int __init davinci_gpio_setup(void)
117{
118 int i, base;
Mark A. Greera9949552009-04-15 12:40:35 -0700119 unsigned ngpio;
120 struct davinci_soc_info *soc_info = &davinci_soc_info;
David Brownelldce11152008-09-07 23:41:04 -0700121
Mark A. Greera9949552009-04-15 12:40:35 -0700122 /*
123 * The gpio banks conceptually expose a segmented bitmap,
David Brownell474dad52008-12-07 11:46:23 -0800124 * and "ngpio" is one more than the largest zero-based
125 * bit index that's valid.
126 */
Mark A. Greera9949552009-04-15 12:40:35 -0700127 ngpio = soc_info->gpio_num;
128 if (ngpio == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800129 pr_err("GPIO setup: how many GPIOs?\n");
130 return -EINVAL;
131 }
132
133 if (WARN_ON(DAVINCI_N_GPIO < ngpio))
134 ngpio = DAVINCI_N_GPIO;
135
136 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
David Brownelldce11152008-09-07 23:41:04 -0700137 chips[i].chip.label = "DaVinci";
138
139 chips[i].chip.direction_input = davinci_direction_in;
140 chips[i].chip.get = davinci_gpio_get;
141 chips[i].chip.direction_output = davinci_direction_out;
142 chips[i].chip.set = davinci_gpio_set;
143
144 chips[i].chip.base = base;
David Brownell474dad52008-12-07 11:46:23 -0800145 chips[i].chip.ngpio = ngpio - base;
David Brownelldce11152008-09-07 23:41:04 -0700146 if (chips[i].chip.ngpio > 32)
147 chips[i].chip.ngpio = 32;
148
149 chips[i].regs = gpio2controller(base);
150
151 gpiochip_add(&chips[i].chip);
152 }
153
Kevin Hilmandc756022009-05-11 11:04:53 -0700154 davinci_gpio_irq_setup();
David Brownelldce11152008-09-07 23:41:04 -0700155 return 0;
156}
157pure_initcall(davinci_gpio_setup);
158
159/*--------------------------------------------------------------------------*/
160/*
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100161 * We expect irqs will normally be set up as input pins, but they can also be
162 * used as output pins ... which is convenient for testing.
163 *
David Brownell474dad52008-12-07 11:46:23 -0800164 * NOTE: The first few GPIOs also have direct INTC hookups in addition
David Brownell7a360712009-06-25 17:01:31 -0700165 * to their GPIOBNK0 irq, with a bit less overhead.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100166 *
David Brownell474dad52008-12-07 11:46:23 -0800167 * All those INTC hookups (direct, plus several IRQ banks) can also
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100168 * serve as EDMA event triggers.
169 */
170
171static void gpio_irq_disable(unsigned irq)
172{
173 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
David Brownell7a360712009-06-25 17:01:31 -0700174 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100175
176 __raw_writel(mask, &g->clr_falling);
177 __raw_writel(mask, &g->clr_rising);
178}
179
180static void gpio_irq_enable(unsigned irq)
181{
182 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
David Brownell7a360712009-06-25 17:01:31 -0700183 u32 mask = (u32) get_irq_data(irq);
David Brownelldf4aab42009-05-04 13:14:27 -0700184 unsigned status = irq_desc[irq].status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100185
David Brownelldf4aab42009-05-04 13:14:27 -0700186 status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
187 if (!status)
188 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
189
190 if (status & IRQ_TYPE_EDGE_FALLING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100191 __raw_writel(mask, &g->set_falling);
David Brownelldf4aab42009-05-04 13:14:27 -0700192 if (status & IRQ_TYPE_EDGE_RISING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100193 __raw_writel(mask, &g->set_rising);
194}
195
196static int gpio_irq_type(unsigned irq, unsigned trigger)
197{
198 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
David Brownell7a360712009-06-25 17:01:31 -0700199 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100200
201 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
202 return -EINVAL;
203
204 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
205 irq_desc[irq].status |= trigger;
206
David Brownelldf4aab42009-05-04 13:14:27 -0700207 /* don't enable the IRQ if it's currently disabled */
208 if (irq_desc[irq].depth == 0) {
209 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
210 ? &g->set_falling : &g->clr_falling);
211 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
212 ? &g->set_rising : &g->clr_rising);
213 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100214 return 0;
215}
216
217static struct irq_chip gpio_irqchip = {
218 .name = "GPIO",
219 .enable = gpio_irq_enable,
220 .disable = gpio_irq_disable,
221 .set_type = gpio_irq_type,
222};
223
224static void
225gpio_irq_handler(unsigned irq, struct irq_desc *desc)
226{
227 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
228 u32 mask = 0xffff;
229
230 /* we only care about one bank */
231 if (irq & 1)
232 mask <<= 16;
233
234 /* temporarily mask (level sensitive) parent IRQ */
Kevin Hilmandc756022009-05-11 11:04:53 -0700235 desc->chip->mask(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100236 desc->chip->ack(irq);
237 while (1) {
238 u32 status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100239 int n;
240 int res;
241
242 /* ack any irqs */
243 status = __raw_readl(&g->intstat) & mask;
244 if (!status)
245 break;
246 __raw_writel(status, &g->intstat);
247 if (irq & 1)
248 status >>= 16;
249
250 /* now demux them to the right lowlevel handler */
251 n = (int)get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100252 while (status) {
253 res = ffs(status);
254 n += res;
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100255 generic_handle_irq(n - 1);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100256 status >>= res;
257 }
258 }
259 desc->chip->unmask(irq);
260 /* now it may re-trigger */
261}
262
David Brownell7a360712009-06-25 17:01:31 -0700263static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
264{
265 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
266
267 if (d->irq_base >= 0)
268 return d->irq_base + offset;
269 else
270 return -ENODEV;
271}
272
273static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
274{
275 struct davinci_soc_info *soc_info = &davinci_soc_info;
276
277 /* NOTE: we assume for now that only irqs in the first gpio_chip
278 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
279 */
280 if (offset < soc_info->gpio_unbanked)
281 return soc_info->gpio_irq + offset;
282 else
283 return -ENODEV;
284}
285
286static int gpio_irq_type_unbanked(unsigned irq, unsigned trigger)
287{
288 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
289 u32 mask = (u32) get_irq_data(irq);
290
291 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
292 return -EINVAL;
293
294 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
295 ? &g->set_falling : &g->clr_falling);
296 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
297 ? &g->set_rising : &g->clr_rising);
298
299 return 0;
300}
301
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100302/*
David Brownell474dad52008-12-07 11:46:23 -0800303 * NOTE: for suspend/resume, probably best to make a platform_device with
304 * suspend_late/resume_resume calls hooking into results of the set_wake()
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100305 * calls ... so if no gpios are wakeup events the clock can be disabled,
306 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
David Brownell474dad52008-12-07 11:46:23 -0800307 * (dm6446) can be set appropriately for GPIOV33 pins.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100308 */
309
310static int __init davinci_gpio_irq_setup(void)
311{
312 unsigned gpio, irq, bank;
313 struct clk *clk;
David Brownell474dad52008-12-07 11:46:23 -0800314 u32 binten = 0;
Mark A. Greera9949552009-04-15 12:40:35 -0700315 unsigned ngpio, bank_irq;
316 struct davinci_soc_info *soc_info = &davinci_soc_info;
David Brownell7a360712009-06-25 17:01:31 -0700317 struct gpio_controller *__iomem g;
David Brownell474dad52008-12-07 11:46:23 -0800318
Mark A. Greera9949552009-04-15 12:40:35 -0700319 ngpio = soc_info->gpio_num;
320
321 bank_irq = soc_info->gpio_irq;
322 if (bank_irq == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800323 printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
324 return -EINVAL;
325 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100326
327 clk = clk_get(NULL, "gpio");
328 if (IS_ERR(clk)) {
329 printk(KERN_ERR "Error %ld getting gpio clock?\n",
330 PTR_ERR(clk));
David Brownell474dad52008-12-07 11:46:23 -0800331 return PTR_ERR(clk);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100332 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100333 clk_enable(clk);
334
David Brownell7a360712009-06-25 17:01:31 -0700335 /* Arrange gpio_to_irq() support, handling either direct IRQs or
336 * banked IRQs. Having GPIOs in the first GPIO bank use direct
337 * IRQs, while the others use banked IRQs, would need some setup
338 * tweaks to recognize hardware which can do that.
339 */
340 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
341 chips[bank].chip.to_irq = gpio_to_irq_banked;
342 chips[bank].irq_base = soc_info->gpio_unbanked
343 ? -EINVAL
344 : (soc_info->intc_irq_num + gpio);
345 }
346
347 /*
348 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
349 * controller only handling trigger modes. We currently assume no
350 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
351 */
352 if (soc_info->gpio_unbanked) {
353 static struct irq_chip gpio_irqchip_unbanked;
354
355 /* pass "bank 0" GPIO IRQs to AINTC */
356 chips[0].chip.to_irq = gpio_to_irq_unbanked;
357 binten = BIT(0);
358
359 /* AINTC handles mask/unmask; GPIO handles triggering */
360 irq = bank_irq;
361 gpio_irqchip_unbanked = *get_irq_desc_chip(irq_to_desc(irq));
362 gpio_irqchip_unbanked.name = "GPIO-AINTC";
363 gpio_irqchip_unbanked.set_type = gpio_irq_type_unbanked;
364
365 /* default trigger: both edges */
366 g = gpio2controller(0);
367 __raw_writel(~0, &g->set_falling);
368 __raw_writel(~0, &g->set_rising);
369
370 /* set the direct IRQs up to use that irqchip */
371 for (gpio = 0; gpio < soc_info->gpio_unbanked; gpio++, irq++) {
372 set_irq_chip(irq, &gpio_irqchip_unbanked);
373 set_irq_data(irq, (void *) __gpio_mask(gpio));
374 set_irq_chip_data(irq, g);
375 irq_desc[irq].status |= IRQ_TYPE_EDGE_BOTH;
376 }
377
378 goto done;
379 }
380
381 /*
382 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
383 * then chain through our own handler.
384 */
David Brownell474dad52008-12-07 11:46:23 -0800385 for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
386 gpio < ngpio;
387 bank++, bank_irq++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100388 unsigned i;
389
David Brownell7a360712009-06-25 17:01:31 -0700390 /* disabled by default, enabled only as needed */
391 g = gpio2controller(gpio);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100392 __raw_writel(~0, &g->clr_falling);
393 __raw_writel(~0, &g->clr_rising);
394
395 /* set up all irqs in this bank */
David Brownell474dad52008-12-07 11:46:23 -0800396 set_irq_chained_handler(bank_irq, gpio_irq_handler);
397 set_irq_chip_data(bank_irq, g);
398 set_irq_data(bank_irq, (void *)irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100399
David Brownell474dad52008-12-07 11:46:23 -0800400 for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100401 set_irq_chip(irq, &gpio_irqchip);
402 set_irq_chip_data(irq, g);
David Brownell7a360712009-06-25 17:01:31 -0700403 set_irq_data(irq, (void *) __gpio_mask(gpio));
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100404 set_irq_handler(irq, handle_simple_irq);
405 set_irq_flags(irq, IRQF_VALID);
406 }
David Brownell474dad52008-12-07 11:46:23 -0800407
408 binten |= BIT(bank);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100409 }
410
David Brownell7a360712009-06-25 17:01:31 -0700411done:
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100412 /* BINTEN -- per-bank interrupt enable. genirq would also let these
413 * bits be set/cleared dynamically.
414 */
Mark A. Greera9949552009-04-15 12:40:35 -0700415 __raw_writel(binten, soc_info->gpio_base + 0x08);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100416
417 printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
418
419 return 0;
420}