blob: 744755b53236adcaf6f9cfeab0159d5f0ad7d92e [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>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010015#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/io.h>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010018
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/gpio.h>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010020
21#include <asm/mach/irq.h>
22
23static DEFINE_SPINLOCK(gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010024
David Brownelldce11152008-09-07 23:41:04 -070025struct davinci_gpio {
26 struct gpio_chip chip;
27 struct gpio_controller *__iomem regs;
David Brownell7a360712009-06-25 17:01:31 -070028 int irq_base;
David Brownelldce11152008-09-07 23:41:04 -070029};
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010030
David Brownelldce11152008-09-07 23:41:04 -070031static struct davinci_gpio chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010032
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010033/* create a non-inlined version */
David Brownell474dad52008-12-07 11:46:23 -080034static struct gpio_controller __iomem * __init gpio2controller(unsigned gpio)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010035{
36 return __gpio_to_controller(gpio);
37}
38
Kevin Hilmandc756022009-05-11 11:04:53 -070039static int __init davinci_gpio_irq_setup(void);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010040
41/*--------------------------------------------------------------------------*/
42
43/*
44 * board setup code *MUST* set PINMUX0 and PINMUX1 as
45 * needed, and enable the GPIO clock.
46 */
47
David Brownelldce11152008-09-07 23:41:04 -070048static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010049{
David Brownelldce11152008-09-07 23:41:04 -070050 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
51 struct gpio_controller *__iomem g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010052 u32 temp;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010053
54 spin_lock(&gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010055 temp = __raw_readl(&g->dir);
David Brownelldce11152008-09-07 23:41:04 -070056 temp |= (1 << offset);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010057 __raw_writel(temp, &g->dir);
58 spin_unlock(&gpio_lock);
David Brownelldce11152008-09-07 23:41:04 -070059
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010060 return 0;
61}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010062
David Brownelldce11152008-09-07 23:41:04 -070063/*
64 * Read the pin's value (works even if it's set up as output);
65 * returns zero/nonzero.
66 *
67 * Note that changes are synched to the GPIO clock, so reading values back
68 * right after you've set them may give old values.
69 */
70static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010071{
David Brownelldce11152008-09-07 23:41:04 -070072 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
73 struct gpio_controller *__iomem g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010074
David Brownelldce11152008-09-07 23:41:04 -070075 return (1 << offset) & __raw_readl(&g->in_data);
76}
77
78static int
79davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
80{
81 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
82 struct gpio_controller *__iomem g = d->regs;
83 u32 temp;
84 u32 mask = 1 << offset;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010085
86 spin_lock(&gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010087 temp = __raw_readl(&g->dir);
88 temp &= ~mask;
89 __raw_writel(mask, value ? &g->set_data : &g->clr_data);
90 __raw_writel(temp, &g->dir);
91 spin_unlock(&gpio_lock);
92 return 0;
93}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010094
95/*
David Brownelldce11152008-09-07 23:41:04 -070096 * Assuming the pin is muxed as a gpio output, set its output value.
97 */
98static void
99davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
100{
101 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
102 struct gpio_controller *__iomem g = d->regs;
103
104 __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
105}
106
107static int __init davinci_gpio_setup(void)
108{
109 int i, base;
Mark A. Greera9949552009-04-15 12:40:35 -0700110 unsigned ngpio;
111 struct davinci_soc_info *soc_info = &davinci_soc_info;
David Brownelldce11152008-09-07 23:41:04 -0700112
Mark A. Greera9949552009-04-15 12:40:35 -0700113 /*
114 * The gpio banks conceptually expose a segmented bitmap,
David Brownell474dad52008-12-07 11:46:23 -0800115 * and "ngpio" is one more than the largest zero-based
116 * bit index that's valid.
117 */
Mark A. Greera9949552009-04-15 12:40:35 -0700118 ngpio = soc_info->gpio_num;
119 if (ngpio == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800120 pr_err("GPIO setup: how many GPIOs?\n");
121 return -EINVAL;
122 }
123
124 if (WARN_ON(DAVINCI_N_GPIO < ngpio))
125 ngpio = DAVINCI_N_GPIO;
126
127 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
David Brownelldce11152008-09-07 23:41:04 -0700128 chips[i].chip.label = "DaVinci";
129
130 chips[i].chip.direction_input = davinci_direction_in;
131 chips[i].chip.get = davinci_gpio_get;
132 chips[i].chip.direction_output = davinci_direction_out;
133 chips[i].chip.set = davinci_gpio_set;
134
135 chips[i].chip.base = base;
David Brownell474dad52008-12-07 11:46:23 -0800136 chips[i].chip.ngpio = ngpio - base;
David Brownelldce11152008-09-07 23:41:04 -0700137 if (chips[i].chip.ngpio > 32)
138 chips[i].chip.ngpio = 32;
139
140 chips[i].regs = gpio2controller(base);
141
142 gpiochip_add(&chips[i].chip);
143 }
144
Kevin Hilmandc756022009-05-11 11:04:53 -0700145 davinci_gpio_irq_setup();
David Brownelldce11152008-09-07 23:41:04 -0700146 return 0;
147}
148pure_initcall(davinci_gpio_setup);
149
150/*--------------------------------------------------------------------------*/
151/*
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100152 * We expect irqs will normally be set up as input pins, but they can also be
153 * used as output pins ... which is convenient for testing.
154 *
David Brownell474dad52008-12-07 11:46:23 -0800155 * NOTE: The first few GPIOs also have direct INTC hookups in addition
David Brownell7a360712009-06-25 17:01:31 -0700156 * to their GPIOBNK0 irq, with a bit less overhead.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100157 *
David Brownell474dad52008-12-07 11:46:23 -0800158 * All those INTC hookups (direct, plus several IRQ banks) can also
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100159 * serve as EDMA event triggers.
160 */
161
162static void gpio_irq_disable(unsigned irq)
163{
164 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
David Brownell7a360712009-06-25 17:01:31 -0700165 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100166
167 __raw_writel(mask, &g->clr_falling);
168 __raw_writel(mask, &g->clr_rising);
169}
170
171static void gpio_irq_enable(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);
David Brownelldf4aab42009-05-04 13:14:27 -0700175 unsigned status = irq_desc[irq].status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100176
David Brownelldf4aab42009-05-04 13:14:27 -0700177 status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
178 if (!status)
179 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
180
181 if (status & IRQ_TYPE_EDGE_FALLING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100182 __raw_writel(mask, &g->set_falling);
David Brownelldf4aab42009-05-04 13:14:27 -0700183 if (status & IRQ_TYPE_EDGE_RISING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100184 __raw_writel(mask, &g->set_rising);
185}
186
187static int gpio_irq_type(unsigned irq, unsigned trigger)
188{
189 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
David Brownell7a360712009-06-25 17:01:31 -0700190 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100191
192 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
193 return -EINVAL;
194
195 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
196 irq_desc[irq].status |= trigger;
197
David Brownelldf4aab42009-05-04 13:14:27 -0700198 /* don't enable the IRQ if it's currently disabled */
199 if (irq_desc[irq].depth == 0) {
200 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
201 ? &g->set_falling : &g->clr_falling);
202 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
203 ? &g->set_rising : &g->clr_rising);
204 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100205 return 0;
206}
207
208static struct irq_chip gpio_irqchip = {
209 .name = "GPIO",
210 .enable = gpio_irq_enable,
211 .disable = gpio_irq_disable,
212 .set_type = gpio_irq_type,
213};
214
215static void
216gpio_irq_handler(unsigned irq, struct irq_desc *desc)
217{
218 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
219 u32 mask = 0xffff;
220
221 /* we only care about one bank */
222 if (irq & 1)
223 mask <<= 16;
224
225 /* temporarily mask (level sensitive) parent IRQ */
Kevin Hilmandc756022009-05-11 11:04:53 -0700226 desc->chip->mask(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100227 desc->chip->ack(irq);
228 while (1) {
229 u32 status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100230 int n;
231 int res;
232
233 /* ack any irqs */
234 status = __raw_readl(&g->intstat) & mask;
235 if (!status)
236 break;
237 __raw_writel(status, &g->intstat);
238 if (irq & 1)
239 status >>= 16;
240
241 /* now demux them to the right lowlevel handler */
242 n = (int)get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100243 while (status) {
244 res = ffs(status);
245 n += res;
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100246 generic_handle_irq(n - 1);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100247 status >>= res;
248 }
249 }
250 desc->chip->unmask(irq);
251 /* now it may re-trigger */
252}
253
David Brownell7a360712009-06-25 17:01:31 -0700254static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
255{
256 struct davinci_gpio *d = container_of(chip, struct davinci_gpio, chip);
257
258 if (d->irq_base >= 0)
259 return d->irq_base + offset;
260 else
261 return -ENODEV;
262}
263
264static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
265{
266 struct davinci_soc_info *soc_info = &davinci_soc_info;
267
268 /* NOTE: we assume for now that only irqs in the first gpio_chip
269 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
270 */
271 if (offset < soc_info->gpio_unbanked)
272 return soc_info->gpio_irq + offset;
273 else
274 return -ENODEV;
275}
276
277static int gpio_irq_type_unbanked(unsigned irq, unsigned trigger)
278{
279 struct gpio_controller *__iomem g = get_irq_chip_data(irq);
280 u32 mask = (u32) get_irq_data(irq);
281
282 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
283 return -EINVAL;
284
285 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
286 ? &g->set_falling : &g->clr_falling);
287 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
288 ? &g->set_rising : &g->clr_rising);
289
290 return 0;
291}
292
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100293/*
David Brownell474dad52008-12-07 11:46:23 -0800294 * NOTE: for suspend/resume, probably best to make a platform_device with
295 * suspend_late/resume_resume calls hooking into results of the set_wake()
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100296 * calls ... so if no gpios are wakeup events the clock can be disabled,
297 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
David Brownell474dad52008-12-07 11:46:23 -0800298 * (dm6446) can be set appropriately for GPIOV33 pins.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100299 */
300
301static int __init davinci_gpio_irq_setup(void)
302{
303 unsigned gpio, irq, bank;
304 struct clk *clk;
David Brownell474dad52008-12-07 11:46:23 -0800305 u32 binten = 0;
Mark A. Greera9949552009-04-15 12:40:35 -0700306 unsigned ngpio, bank_irq;
307 struct davinci_soc_info *soc_info = &davinci_soc_info;
David Brownell7a360712009-06-25 17:01:31 -0700308 struct gpio_controller *__iomem g;
David Brownell474dad52008-12-07 11:46:23 -0800309
Mark A. Greera9949552009-04-15 12:40:35 -0700310 ngpio = soc_info->gpio_num;
311
312 bank_irq = soc_info->gpio_irq;
313 if (bank_irq == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800314 printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
315 return -EINVAL;
316 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100317
318 clk = clk_get(NULL, "gpio");
319 if (IS_ERR(clk)) {
320 printk(KERN_ERR "Error %ld getting gpio clock?\n",
321 PTR_ERR(clk));
David Brownell474dad52008-12-07 11:46:23 -0800322 return PTR_ERR(clk);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100323 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100324 clk_enable(clk);
325
David Brownell7a360712009-06-25 17:01:31 -0700326 /* Arrange gpio_to_irq() support, handling either direct IRQs or
327 * banked IRQs. Having GPIOs in the first GPIO bank use direct
328 * IRQs, while the others use banked IRQs, would need some setup
329 * tweaks to recognize hardware which can do that.
330 */
331 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
332 chips[bank].chip.to_irq = gpio_to_irq_banked;
333 chips[bank].irq_base = soc_info->gpio_unbanked
334 ? -EINVAL
335 : (soc_info->intc_irq_num + gpio);
336 }
337
338 /*
339 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
340 * controller only handling trigger modes. We currently assume no
341 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
342 */
343 if (soc_info->gpio_unbanked) {
344 static struct irq_chip gpio_irqchip_unbanked;
345
346 /* pass "bank 0" GPIO IRQs to AINTC */
347 chips[0].chip.to_irq = gpio_to_irq_unbanked;
348 binten = BIT(0);
349
350 /* AINTC handles mask/unmask; GPIO handles triggering */
351 irq = bank_irq;
352 gpio_irqchip_unbanked = *get_irq_desc_chip(irq_to_desc(irq));
353 gpio_irqchip_unbanked.name = "GPIO-AINTC";
354 gpio_irqchip_unbanked.set_type = gpio_irq_type_unbanked;
355
356 /* default trigger: both edges */
357 g = gpio2controller(0);
358 __raw_writel(~0, &g->set_falling);
359 __raw_writel(~0, &g->set_rising);
360
361 /* set the direct IRQs up to use that irqchip */
362 for (gpio = 0; gpio < soc_info->gpio_unbanked; gpio++, irq++) {
363 set_irq_chip(irq, &gpio_irqchip_unbanked);
364 set_irq_data(irq, (void *) __gpio_mask(gpio));
365 set_irq_chip_data(irq, g);
366 irq_desc[irq].status |= IRQ_TYPE_EDGE_BOTH;
367 }
368
369 goto done;
370 }
371
372 /*
373 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
374 * then chain through our own handler.
375 */
David Brownell474dad52008-12-07 11:46:23 -0800376 for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
377 gpio < ngpio;
378 bank++, bank_irq++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100379 unsigned i;
380
David Brownell7a360712009-06-25 17:01:31 -0700381 /* disabled by default, enabled only as needed */
382 g = gpio2controller(gpio);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100383 __raw_writel(~0, &g->clr_falling);
384 __raw_writel(~0, &g->clr_rising);
385
386 /* set up all irqs in this bank */
David Brownell474dad52008-12-07 11:46:23 -0800387 set_irq_chained_handler(bank_irq, gpio_irq_handler);
388 set_irq_chip_data(bank_irq, g);
389 set_irq_data(bank_irq, (void *)irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100390
David Brownell474dad52008-12-07 11:46:23 -0800391 for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100392 set_irq_chip(irq, &gpio_irqchip);
393 set_irq_chip_data(irq, g);
David Brownell7a360712009-06-25 17:01:31 -0700394 set_irq_data(irq, (void *) __gpio_mask(gpio));
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100395 set_irq_handler(irq, handle_simple_irq);
396 set_irq_flags(irq, IRQF_VALID);
397 }
David Brownell474dad52008-12-07 11:46:23 -0800398
399 binten |= BIT(bank);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100400 }
401
David Brownell7a360712009-06-25 17:01:31 -0700402done:
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100403 /* BINTEN -- per-bank interrupt enable. genirq would also let these
404 * bits be set/cleared dynamically.
405 */
Mark A. Greera9949552009-04-15 12:40:35 -0700406 __raw_writel(binten, soc_info->gpio_base + 0x08);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100407
408 printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
409
410 return 0;
411}