blob: 93f7c686153a86108b430ab370fc8ab5e3033c6a [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;
Kevin Hilman28552c22010-02-25 15:36:38 -080027 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
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040031#define chip2controller(chip) \
32 container_of(chip, struct davinci_gpio, chip)
33
David Brownelldce11152008-09-07 23:41:04 -070034static struct davinci_gpio chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010035
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010036/* create a non-inlined version */
Kevin Hilman28552c22010-02-25 15:36:38 -080037static struct gpio_controller __iomem __init *gpio2controller(unsigned gpio)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010038{
39 return __gpio_to_controller(gpio);
40}
41
Kevin Hilman21ce8732010-02-25 16:49:56 -080042static inline struct gpio_controller __iomem *irq2controller(int irq)
43{
44 struct gpio_controller __iomem *g;
45
46 g = (__force struct gpio_controller __iomem *)get_irq_chip_data(irq);
47
48 return g;
49}
50
Kevin Hilmandc756022009-05-11 11:04:53 -070051static int __init davinci_gpio_irq_setup(void);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010052
53/*--------------------------------------------------------------------------*/
54
55/*
56 * board setup code *MUST* set PINMUX0 and PINMUX1 as
57 * needed, and enable the GPIO clock.
58 */
59
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040060static inline int __davinci_direction(struct gpio_chip *chip,
61 unsigned offset, bool out, int value)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010062{
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040063 struct davinci_gpio *d = chip2controller(chip);
Kevin Hilman28552c22010-02-25 15:36:38 -080064 struct gpio_controller __iomem *g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010065 u32 temp;
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040066 u32 mask = 1 << offset;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010067
68 spin_lock(&gpio_lock);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010069 temp = __raw_readl(&g->dir);
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040070 if (out) {
71 temp &= ~mask;
72 __raw_writel(mask, value ? &g->set_data : &g->clr_data);
73 } else {
74 temp |= mask;
75 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010076 __raw_writel(temp, &g->dir);
77 spin_unlock(&gpio_lock);
David Brownelldce11152008-09-07 23:41:04 -070078
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010079 return 0;
80}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010081
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040082static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
83{
84 return __davinci_direction(chip, offset, false, 0);
85}
86
87static int
88davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
89{
90 return __davinci_direction(chip, offset, true, value);
91}
92
David Brownelldce11152008-09-07 23:41:04 -070093/*
94 * Read the pin's value (works even if it's set up as output);
95 * returns zero/nonzero.
96 *
97 * Note that changes are synched to the GPIO clock, so reading values back
98 * right after you've set them may give old values.
99 */
100static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100101{
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400102 struct davinci_gpio *d = chip2controller(chip);
Kevin Hilman28552c22010-02-25 15:36:38 -0800103 struct gpio_controller __iomem *g = d->regs;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100104
David Brownelldce11152008-09-07 23:41:04 -0700105 return (1 << offset) & __raw_readl(&g->in_data);
106}
107
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100108/*
David Brownelldce11152008-09-07 23:41:04 -0700109 * Assuming the pin is muxed as a gpio output, set its output value.
110 */
111static void
112davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
113{
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400114 struct davinci_gpio *d = chip2controller(chip);
Kevin Hilman28552c22010-02-25 15:36:38 -0800115 struct gpio_controller __iomem *g = d->regs;
David Brownelldce11152008-09-07 23:41:04 -0700116
117 __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
118}
119
120static int __init davinci_gpio_setup(void)
121{
122 int i, base;
Mark A. Greera9949552009-04-15 12:40:35 -0700123 unsigned ngpio;
124 struct davinci_soc_info *soc_info = &davinci_soc_info;
David Brownelldce11152008-09-07 23:41:04 -0700125
Mark A. Greera9949552009-04-15 12:40:35 -0700126 /*
127 * The gpio banks conceptually expose a segmented bitmap,
David Brownell474dad52008-12-07 11:46:23 -0800128 * and "ngpio" is one more than the largest zero-based
129 * bit index that's valid.
130 */
Mark A. Greera9949552009-04-15 12:40:35 -0700131 ngpio = soc_info->gpio_num;
132 if (ngpio == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800133 pr_err("GPIO setup: how many GPIOs?\n");
134 return -EINVAL;
135 }
136
137 if (WARN_ON(DAVINCI_N_GPIO < ngpio))
138 ngpio = DAVINCI_N_GPIO;
139
140 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
David Brownelldce11152008-09-07 23:41:04 -0700141 chips[i].chip.label = "DaVinci";
142
143 chips[i].chip.direction_input = davinci_direction_in;
144 chips[i].chip.get = davinci_gpio_get;
145 chips[i].chip.direction_output = davinci_direction_out;
146 chips[i].chip.set = davinci_gpio_set;
147
148 chips[i].chip.base = base;
David Brownell474dad52008-12-07 11:46:23 -0800149 chips[i].chip.ngpio = ngpio - base;
David Brownelldce11152008-09-07 23:41:04 -0700150 if (chips[i].chip.ngpio > 32)
151 chips[i].chip.ngpio = 32;
152
153 chips[i].regs = gpio2controller(base);
154
155 gpiochip_add(&chips[i].chip);
156 }
157
Kevin Hilmandc756022009-05-11 11:04:53 -0700158 davinci_gpio_irq_setup();
David Brownelldce11152008-09-07 23:41:04 -0700159 return 0;
160}
161pure_initcall(davinci_gpio_setup);
162
163/*--------------------------------------------------------------------------*/
164/*
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100165 * We expect irqs will normally be set up as input pins, but they can also be
166 * used as output pins ... which is convenient for testing.
167 *
David Brownell474dad52008-12-07 11:46:23 -0800168 * NOTE: The first few GPIOs also have direct INTC hookups in addition
David Brownell7a360712009-06-25 17:01:31 -0700169 * to their GPIOBNK0 irq, with a bit less overhead.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100170 *
David Brownell474dad52008-12-07 11:46:23 -0800171 * All those INTC hookups (direct, plus several IRQ banks) can also
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100172 * serve as EDMA event triggers.
173 */
174
175static void gpio_irq_disable(unsigned irq)
176{
Kevin Hilman21ce8732010-02-25 16:49:56 -0800177 struct gpio_controller __iomem *g = irq2controller(irq);
David Brownell7a360712009-06-25 17:01:31 -0700178 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100179
180 __raw_writel(mask, &g->clr_falling);
181 __raw_writel(mask, &g->clr_rising);
182}
183
184static void gpio_irq_enable(unsigned irq)
185{
Kevin Hilman21ce8732010-02-25 16:49:56 -0800186 struct gpio_controller __iomem *g = irq2controller(irq);
David Brownell7a360712009-06-25 17:01:31 -0700187 u32 mask = (u32) get_irq_data(irq);
David Brownelldf4aab42009-05-04 13:14:27 -0700188 unsigned status = irq_desc[irq].status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100189
David Brownelldf4aab42009-05-04 13:14:27 -0700190 status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
191 if (!status)
192 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
193
194 if (status & IRQ_TYPE_EDGE_FALLING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100195 __raw_writel(mask, &g->set_falling);
David Brownelldf4aab42009-05-04 13:14:27 -0700196 if (status & IRQ_TYPE_EDGE_RISING)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100197 __raw_writel(mask, &g->set_rising);
198}
199
200static int gpio_irq_type(unsigned irq, unsigned trigger)
201{
Kevin Hilman21ce8732010-02-25 16:49:56 -0800202 struct gpio_controller __iomem *g = irq2controller(irq);
David Brownell7a360712009-06-25 17:01:31 -0700203 u32 mask = (u32) get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100204
205 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
206 return -EINVAL;
207
208 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
209 irq_desc[irq].status |= trigger;
210
David Brownelldf4aab42009-05-04 13:14:27 -0700211 /* don't enable the IRQ if it's currently disabled */
212 if (irq_desc[irq].depth == 0) {
213 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
214 ? &g->set_falling : &g->clr_falling);
215 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
216 ? &g->set_rising : &g->clr_rising);
217 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100218 return 0;
219}
220
221static struct irq_chip gpio_irqchip = {
222 .name = "GPIO",
223 .enable = gpio_irq_enable,
224 .disable = gpio_irq_disable,
225 .set_type = gpio_irq_type,
226};
227
228static void
229gpio_irq_handler(unsigned irq, struct irq_desc *desc)
230{
Kevin Hilman21ce8732010-02-25 16:49:56 -0800231 struct gpio_controller __iomem *g = irq2controller(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100232 u32 mask = 0xffff;
233
234 /* we only care about one bank */
235 if (irq & 1)
236 mask <<= 16;
237
238 /* temporarily mask (level sensitive) parent IRQ */
Kevin Hilmandc756022009-05-11 11:04:53 -0700239 desc->chip->mask(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100240 desc->chip->ack(irq);
241 while (1) {
242 u32 status;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100243 int n;
244 int res;
245
246 /* ack any irqs */
247 status = __raw_readl(&g->intstat) & mask;
248 if (!status)
249 break;
250 __raw_writel(status, &g->intstat);
251 if (irq & 1)
252 status >>= 16;
253
254 /* now demux them to the right lowlevel handler */
255 n = (int)get_irq_data(irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100256 while (status) {
257 res = ffs(status);
258 n += res;
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100259 generic_handle_irq(n - 1);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100260 status >>= res;
261 }
262 }
263 desc->chip->unmask(irq);
264 /* now it may re-trigger */
265}
266
David Brownell7a360712009-06-25 17:01:31 -0700267static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
268{
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400269 struct davinci_gpio *d = chip2controller(chip);
David Brownell7a360712009-06-25 17:01:31 -0700270
271 if (d->irq_base >= 0)
272 return d->irq_base + offset;
273 else
274 return -ENODEV;
275}
276
277static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
278{
279 struct davinci_soc_info *soc_info = &davinci_soc_info;
280
281 /* NOTE: we assume for now that only irqs in the first gpio_chip
282 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
283 */
284 if (offset < soc_info->gpio_unbanked)
285 return soc_info->gpio_irq + offset;
286 else
287 return -ENODEV;
288}
289
290static int gpio_irq_type_unbanked(unsigned irq, unsigned trigger)
291{
Kevin Hilman21ce8732010-02-25 16:49:56 -0800292 struct gpio_controller __iomem *g = irq2controller(irq);
David Brownell7a360712009-06-25 17:01:31 -0700293 u32 mask = (u32) get_irq_data(irq);
294
295 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
296 return -EINVAL;
297
298 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
299 ? &g->set_falling : &g->clr_falling);
300 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
301 ? &g->set_rising : &g->clr_rising);
302
303 return 0;
304}
305
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100306/*
David Brownell474dad52008-12-07 11:46:23 -0800307 * NOTE: for suspend/resume, probably best to make a platform_device with
308 * suspend_late/resume_resume calls hooking into results of the set_wake()
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100309 * calls ... so if no gpios are wakeup events the clock can be disabled,
310 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
David Brownell474dad52008-12-07 11:46:23 -0800311 * (dm6446) can be set appropriately for GPIOV33 pins.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100312 */
313
314static int __init davinci_gpio_irq_setup(void)
315{
316 unsigned gpio, irq, bank;
317 struct clk *clk;
David Brownell474dad52008-12-07 11:46:23 -0800318 u32 binten = 0;
Mark A. Greera9949552009-04-15 12:40:35 -0700319 unsigned ngpio, bank_irq;
320 struct davinci_soc_info *soc_info = &davinci_soc_info;
Kevin Hilman28552c22010-02-25 15:36:38 -0800321 struct gpio_controller __iomem *g;
David Brownell474dad52008-12-07 11:46:23 -0800322
Mark A. Greera9949552009-04-15 12:40:35 -0700323 ngpio = soc_info->gpio_num;
324
325 bank_irq = soc_info->gpio_irq;
326 if (bank_irq == 0) {
David Brownell474dad52008-12-07 11:46:23 -0800327 printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
328 return -EINVAL;
329 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100330
331 clk = clk_get(NULL, "gpio");
332 if (IS_ERR(clk)) {
333 printk(KERN_ERR "Error %ld getting gpio clock?\n",
334 PTR_ERR(clk));
David Brownell474dad52008-12-07 11:46:23 -0800335 return PTR_ERR(clk);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100336 }
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100337 clk_enable(clk);
338
David Brownell7a360712009-06-25 17:01:31 -0700339 /* Arrange gpio_to_irq() support, handling either direct IRQs or
340 * banked IRQs. Having GPIOs in the first GPIO bank use direct
341 * IRQs, while the others use banked IRQs, would need some setup
342 * tweaks to recognize hardware which can do that.
343 */
344 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
345 chips[bank].chip.to_irq = gpio_to_irq_banked;
346 chips[bank].irq_base = soc_info->gpio_unbanked
347 ? -EINVAL
348 : (soc_info->intc_irq_num + gpio);
349 }
350
351 /*
352 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
353 * controller only handling trigger modes. We currently assume no
354 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
355 */
356 if (soc_info->gpio_unbanked) {
357 static struct irq_chip gpio_irqchip_unbanked;
358
359 /* pass "bank 0" GPIO IRQs to AINTC */
360 chips[0].chip.to_irq = gpio_to_irq_unbanked;
361 binten = BIT(0);
362
363 /* AINTC handles mask/unmask; GPIO handles triggering */
364 irq = bank_irq;
365 gpio_irqchip_unbanked = *get_irq_desc_chip(irq_to_desc(irq));
366 gpio_irqchip_unbanked.name = "GPIO-AINTC";
367 gpio_irqchip_unbanked.set_type = gpio_irq_type_unbanked;
368
369 /* default trigger: both edges */
370 g = gpio2controller(0);
371 __raw_writel(~0, &g->set_falling);
372 __raw_writel(~0, &g->set_rising);
373
374 /* set the direct IRQs up to use that irqchip */
375 for (gpio = 0; gpio < soc_info->gpio_unbanked; gpio++, irq++) {
376 set_irq_chip(irq, &gpio_irqchip_unbanked);
377 set_irq_data(irq, (void *) __gpio_mask(gpio));
Kevin Hilman21ce8732010-02-25 16:49:56 -0800378 set_irq_chip_data(irq, (__force void *) g);
David Brownell7a360712009-06-25 17:01:31 -0700379 irq_desc[irq].status |= IRQ_TYPE_EDGE_BOTH;
380 }
381
382 goto done;
383 }
384
385 /*
386 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
387 * then chain through our own handler.
388 */
David Brownell474dad52008-12-07 11:46:23 -0800389 for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
390 gpio < ngpio;
391 bank++, bank_irq++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100392 unsigned i;
393
David Brownell7a360712009-06-25 17:01:31 -0700394 /* disabled by default, enabled only as needed */
395 g = gpio2controller(gpio);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100396 __raw_writel(~0, &g->clr_falling);
397 __raw_writel(~0, &g->clr_rising);
398
399 /* set up all irqs in this bank */
David Brownell474dad52008-12-07 11:46:23 -0800400 set_irq_chained_handler(bank_irq, gpio_irq_handler);
Kevin Hilman21ce8732010-02-25 16:49:56 -0800401 set_irq_chip_data(bank_irq, (__force void *) g);
402 set_irq_data(bank_irq, (void *) irq);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100403
David Brownell474dad52008-12-07 11:46:23 -0800404 for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100405 set_irq_chip(irq, &gpio_irqchip);
Kevin Hilman21ce8732010-02-25 16:49:56 -0800406 set_irq_chip_data(irq, (__force void *) g);
David Brownell7a360712009-06-25 17:01:31 -0700407 set_irq_data(irq, (void *) __gpio_mask(gpio));
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100408 set_irq_handler(irq, handle_simple_irq);
409 set_irq_flags(irq, IRQF_VALID);
410 }
David Brownell474dad52008-12-07 11:46:23 -0800411
412 binten |= BIT(bank);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100413 }
414
David Brownell7a360712009-06-25 17:01:31 -0700415done:
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100416 /* BINTEN -- per-bank interrupt enable. genirq would also let these
417 * bits be set/cleared dynamically.
418 */
Mark A. Greera9949552009-04-15 12:40:35 -0700419 __raw_writel(binten, soc_info->gpio_base + 0x08);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100420
421 printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
422
423 return 0;
424}