blob: b35eb25209ecdc8b9e3212ff95e0414aaf9307de [file] [log] [blame]
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001/*
2 * Generic GPIO driver for logic cells found in the Nomadik SoC
3 *
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
Linus Walleij33d78642011-06-09 11:08:47 +02007 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010017#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010018#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010019#include <linux/clk.h>
20#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010021#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010026
Will Deaconadfed152011-02-28 10:12:29 +000027#include <asm/mach/irq.h>
28
Rabin Vincent378be062010-06-02 06:06:29 +010029#include <plat/pincfg.h>
Linus Walleij0f332862011-08-22 08:33:30 +010030#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010031
32/*
33 * The GPIO module in the Nomadik family of Systems-on-Chip is an
34 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020035 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010036 *
37 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
38 */
39
Rabin Vincent01727e62010-12-13 12:02:40 +053040#define NMK_GPIO_PER_CHIP 32
41
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010042struct nmk_gpio_chip {
43 struct gpio_chip chip;
44 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010045 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053046 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010047 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053048 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053049 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053050 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010051 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020052 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010053 /* Keep track of configured edges */
54 u32 edge_rising;
55 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053056 u32 real_wake;
57 u32 rwimsc;
58 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053059 u32 rimsc;
60 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020061 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053062 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010063};
64
Rabin Vincent01727e62010-12-13 12:02:40 +053065static struct nmk_gpio_chip *
66nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
67
68static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
69
70#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
71
Rabin Vincent6f9a9742010-06-02 05:50:28 +010072static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
73 unsigned offset, int gpio_mode)
74{
75 u32 bit = 1 << offset;
76 u32 afunc, bfunc;
77
78 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
79 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
80 if (gpio_mode & NMK_GPIO_ALT_A)
81 afunc |= bit;
82 if (gpio_mode & NMK_GPIO_ALT_B)
83 bfunc |= bit;
84 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
85 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
86}
87
Rabin Vincent81a3c292010-05-27 12:39:23 +010088static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
89 unsigned offset, enum nmk_gpio_slpm mode)
90{
91 u32 bit = 1 << offset;
92 u32 slpm;
93
94 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
95 if (mode == NMK_GPIO_SLPM_NOCHANGE)
96 slpm |= bit;
97 else
98 slpm &= ~bit;
99 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
100}
101
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100102static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
103 unsigned offset, enum nmk_gpio_pull pull)
104{
105 u32 bit = 1 << offset;
106 u32 pdis;
107
108 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200109 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100110 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200111 nmk_chip->pull_up &= ~bit;
112 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100113 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200114 }
115
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100116 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
117
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200118 if (pull == NMK_GPIO_PULL_UP) {
119 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100120 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200121 } else if (pull == NMK_GPIO_PULL_DOWN) {
122 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100123 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200124 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100125}
126
Rabin Vincentebc61782011-09-28 15:49:11 +0530127static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
128 unsigned offset, bool lowemi)
129{
130 u32 bit = BIT(offset);
131 bool enabled = nmk_chip->lowemi & bit;
132
133 if (lowemi == enabled)
134 return;
135
136 if (lowemi)
137 nmk_chip->lowemi |= bit;
138 else
139 nmk_chip->lowemi &= ~bit;
140
141 writel_relaxed(nmk_chip->lowemi,
142 nmk_chip->addr + NMK_GPIO_LOWEMI);
143}
144
Rabin Vincent378be062010-06-02 06:06:29 +0100145static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
146 unsigned offset)
147{
148 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
149}
150
Rabin Vincent6720db72010-09-02 11:28:48 +0100151static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
152 unsigned offset, int val)
153{
154 if (val)
155 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
156 else
157 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
158}
159
160static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
161 unsigned offset, int val)
162{
163 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
164 __nmk_gpio_set_output(nmk_chip, offset, val);
165}
166
Rabin Vincent01727e62010-12-13 12:02:40 +0530167static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
168 unsigned offset, int gpio_mode,
169 bool glitch)
170{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530171 u32 rwimsc = nmk_chip->rwimsc;
172 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530173
174 if (glitch && nmk_chip->set_ioforce) {
175 u32 bit = BIT(offset);
176
Rabin Vincent01727e62010-12-13 12:02:40 +0530177 /* Prevent spurious wakeups */
178 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
179 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
180
181 nmk_chip->set_ioforce(true);
182 }
183
184 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
185
186 if (glitch && nmk_chip->set_ioforce) {
187 nmk_chip->set_ioforce(false);
188
189 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
190 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
191 }
192}
193
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530194static void
195nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
196{
197 u32 falling = nmk_chip->fimsc & BIT(offset);
198 u32 rising = nmk_chip->rimsc & BIT(offset);
199 int gpio = nmk_chip->chip.base + offset;
200 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
201 struct irq_data *d = irq_get_irq_data(irq);
202
203 if (!rising && !falling)
204 return;
205
206 if (!d || !irqd_irq_disabled(d))
207 return;
208
209 if (rising) {
210 nmk_chip->rimsc &= ~BIT(offset);
211 writel_relaxed(nmk_chip->rimsc,
212 nmk_chip->addr + NMK_GPIO_RIMSC);
213 }
214
215 if (falling) {
216 nmk_chip->fimsc &= ~BIT(offset);
217 writel_relaxed(nmk_chip->fimsc,
218 nmk_chip->addr + NMK_GPIO_FIMSC);
219 }
220
221 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
222}
223
Rabin Vincent378be062010-06-02 06:06:29 +0100224static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530225 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100226{
227 static const char *afnames[] = {
228 [NMK_GPIO_ALT_GPIO] = "GPIO",
229 [NMK_GPIO_ALT_A] = "A",
230 [NMK_GPIO_ALT_B] = "B",
231 [NMK_GPIO_ALT_C] = "C"
232 };
233 static const char *pullnames[] = {
234 [NMK_GPIO_PULL_NONE] = "none",
235 [NMK_GPIO_PULL_UP] = "up",
236 [NMK_GPIO_PULL_DOWN] = "down",
237 [3] /* illegal */ = "??"
238 };
239 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100240 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
241 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100242 };
243
244 int pin = PIN_NUM(cfg);
245 int pull = PIN_PULL(cfg);
246 int af = PIN_ALT(cfg);
247 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100248 int output = PIN_DIR(cfg);
249 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530250 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100251
Rabin Vincentdacdc962010-12-03 20:35:37 +0530252 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
253 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100254 output ? "output " : "input",
255 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100256
Rabin Vincentdacdc962010-12-03 20:35:37 +0530257 if (sleep) {
258 int slpm_pull = PIN_SLPM_PULL(cfg);
259 int slpm_output = PIN_SLPM_DIR(cfg);
260 int slpm_val = PIN_SLPM_VAL(cfg);
261
Rabin Vincent3546d152010-11-25 11:38:27 +0530262 af = NMK_GPIO_ALT_GPIO;
263
Rabin Vincentdacdc962010-12-03 20:35:37 +0530264 /*
265 * The SLPM_* values are normal values + 1 to allow zero to
266 * mean "same as normal".
267 */
268 if (slpm_pull)
269 pull = slpm_pull - 1;
270 if (slpm_output)
271 output = slpm_output - 1;
272 if (slpm_val)
273 val = slpm_val - 1;
274
275 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
276 pin,
277 slpm_pull ? pullnames[pull] : "same",
278 slpm_output ? (output ? "output" : "input") : "same",
279 slpm_val ? (val ? "high" : "low") : "same");
280 }
281
Rabin Vincent6720db72010-09-02 11:28:48 +0100282 if (output)
283 __nmk_gpio_make_output(nmk_chip, offset, val);
284 else {
285 __nmk_gpio_make_input(nmk_chip, offset);
286 __nmk_gpio_set_pull(nmk_chip, offset, pull);
287 }
288
Rabin Vincentebc61782011-09-28 15:49:11 +0530289 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
290
Rabin Vincent01727e62010-12-13 12:02:40 +0530291 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530292 * If the pin is switching to altfunc, and there was an interrupt
293 * installed on it which has been lazy disabled, actually mask the
294 * interrupt to prevent spurious interrupts that would occur while the
295 * pin is under control of the peripheral. Only SKE does this.
296 */
297 if (af != NMK_GPIO_ALT_GPIO)
298 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
299
300 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530301 * If we've backed up the SLPM registers (glitch workaround), modify
302 * the backups since they will be restored.
303 */
304 if (slpmregs) {
305 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
306 slpmregs[nmk_chip->bank] |= BIT(offset);
307 else
308 slpmregs[nmk_chip->bank] &= ~BIT(offset);
309 } else
310 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
311
312 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
313}
314
315/*
316 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
317 * - Save SLPM registers
318 * - Set SLPM=0 for the IOs you want to switch and others to 1
319 * - Configure the GPIO registers for the IOs that are being switched
320 * - Set IOFORCE=1
321 * - Modify the AFLSA/B registers for the IOs that are being switched
322 * - Set IOFORCE=0
323 * - Restore SLPM registers
324 * - Any spurious wake up event during switch sequence to be ignored and
325 * cleared
326 */
327static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
328{
329 int i;
330
331 for (i = 0; i < NUM_BANKS; i++) {
332 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
333 unsigned int temp = slpm[i];
334
335 if (!chip)
336 break;
337
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200338 clk_enable(chip->clk);
339
Rabin Vincent01727e62010-12-13 12:02:40 +0530340 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
341 writel(temp, chip->addr + NMK_GPIO_SLPC);
342 }
343}
344
345static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
346{
347 int i;
348
349 for (i = 0; i < NUM_BANKS; i++) {
350 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
351
352 if (!chip)
353 break;
354
355 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200356
357 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530358 }
359}
360
361static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
362{
363 static unsigned int slpm[NUM_BANKS];
364 unsigned long flags;
365 bool glitch = false;
366 int ret = 0;
367 int i;
368
369 for (i = 0; i < num; i++) {
370 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
371 glitch = true;
372 break;
373 }
374 }
375
376 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
377
378 if (glitch) {
379 memset(slpm, 0xff, sizeof(slpm));
380
381 for (i = 0; i < num; i++) {
382 int pin = PIN_NUM(cfgs[i]);
383 int offset = pin % NMK_GPIO_PER_CHIP;
384
385 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
386 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
387 }
388
389 nmk_gpio_glitch_slpm_init(slpm);
390 }
391
392 for (i = 0; i < num; i++) {
393 struct nmk_gpio_chip *nmk_chip;
394 int pin = PIN_NUM(cfgs[i]);
395
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100396 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530397 if (!nmk_chip) {
398 ret = -EINVAL;
399 break;
400 }
401
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200402 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530403 spin_lock(&nmk_chip->lock);
404 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
405 cfgs[i], sleep, glitch ? slpm : NULL);
406 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200407 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530408 }
409
410 if (glitch)
411 nmk_gpio_glitch_slpm_restore(slpm);
412
413 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
414
415 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100416}
417
418/**
419 * nmk_config_pin - configure a pin's mux attributes
420 * @cfg: pin confguration
421 *
422 * Configures a pin's mode (alternate function or GPIO), its pull up status,
423 * and its sleep mode based on the specified configuration. The @cfg is
424 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
425 * are constructed using, and can be further enhanced with, the macros in
426 * plat/pincfg.h.
427 *
428 * If a pin's mode is set to GPIO, it is configured as an input to avoid
429 * side-effects. The gpio can be manipulated later using standard GPIO API
430 * calls.
431 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530432int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100433{
Rabin Vincent01727e62010-12-13 12:02:40 +0530434 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100435}
436EXPORT_SYMBOL(nmk_config_pin);
437
438/**
439 * nmk_config_pins - configure several pins at once
440 * @cfgs: array of pin configurations
441 * @num: number of elments in the array
442 *
443 * Configures several pins using nmk_config_pin(). Refer to that function for
444 * further information.
445 */
446int nmk_config_pins(pin_cfg_t *cfgs, int num)
447{
Rabin Vincent01727e62010-12-13 12:02:40 +0530448 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100449}
450EXPORT_SYMBOL(nmk_config_pins);
451
Rabin Vincentdacdc962010-12-03 20:35:37 +0530452int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
453{
Rabin Vincent01727e62010-12-13 12:02:40 +0530454 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530455}
456EXPORT_SYMBOL(nmk_config_pins_sleep);
457
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100458/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100459 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
460 * @gpio: pin number
461 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
462 *
Linus Walleij33d78642011-06-09 11:08:47 +0200463 * This register is actually in the pinmux layer, not the GPIO block itself.
464 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
465 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
466 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
467 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
468 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
469 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100470 *
Linus Walleij33d78642011-06-09 11:08:47 +0200471 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
472 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
473 * entered) regardless of the altfunction selected. Also wake-up detection is
474 * ENABLED.
475 *
476 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
477 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
478 * (for altfunction GPIO) or respective on-chip peripherals (for other
479 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
480 *
481 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100482 */
483int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
484{
485 struct nmk_gpio_chip *nmk_chip;
486 unsigned long flags;
487
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100488 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100489 if (!nmk_chip)
490 return -EINVAL;
491
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200492 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530493 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
494 spin_lock(&nmk_chip->lock);
495
Rabin Vincent81a3c292010-05-27 12:39:23 +0100496 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530497
498 spin_unlock(&nmk_chip->lock);
499 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200500 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100501
502 return 0;
503}
504
505/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100506 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
507 * @gpio: pin number
508 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
509 *
510 * Enables/disables pull up/down on a specified pin. This only takes effect if
511 * the pin is configured as an input (either explicitly or by the alternate
512 * function).
513 *
514 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
515 * configured as an input. Otherwise, due to the way the controller registers
516 * work, this function will change the value output on the pin.
517 */
518int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
519{
520 struct nmk_gpio_chip *nmk_chip;
521 unsigned long flags;
522
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100523 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100524 if (!nmk_chip)
525 return -EINVAL;
526
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200527 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100528 spin_lock_irqsave(&nmk_chip->lock, flags);
529 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
530 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200531 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100532
533 return 0;
534}
535
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100536/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200537/**
538 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
539 * @gpio: pin number
540 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
541 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
542 *
543 * Sets the mode of the specified pin to one of the alternate functions or
544 * plain GPIO.
545 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100546int nmk_gpio_set_mode(int gpio, int gpio_mode)
547{
548 struct nmk_gpio_chip *nmk_chip;
549 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100550
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100551 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100552 if (!nmk_chip)
553 return -EINVAL;
554
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200555 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100556 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100557 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100558 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200559 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100560
561 return 0;
562}
563EXPORT_SYMBOL(nmk_gpio_set_mode);
564
565int nmk_gpio_get_mode(int gpio)
566{
567 struct nmk_gpio_chip *nmk_chip;
568 u32 afunc, bfunc, bit;
569
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100570 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100571 if (!nmk_chip)
572 return -EINVAL;
573
574 bit = 1 << (gpio - nmk_chip->chip.base);
575
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200576 clk_enable(nmk_chip->clk);
577
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100578 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
579 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
580
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200581 clk_disable(nmk_chip->clk);
582
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100583 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
584}
585EXPORT_SYMBOL(nmk_gpio_get_mode);
586
587
588/* IRQ functions */
589static inline int nmk_gpio_get_bitmask(int gpio)
590{
591 return 1 << (gpio % 32);
592}
593
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100594static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100595{
596 int gpio;
597 struct nmk_gpio_chip *nmk_chip;
598
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100599 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
600 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100601 if (!nmk_chip)
602 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200603
604 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100605 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200606 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607}
608
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100609enum nmk_gpio_irq_type {
610 NORMAL,
611 WAKE,
612};
613
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100614static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100615 int gpio, enum nmk_gpio_irq_type which,
616 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100617{
618 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530619 u32 *rimscval;
620 u32 *fimscval;
621 u32 rimscreg;
622 u32 fimscreg;
623
624 if (which == NORMAL) {
625 rimscreg = NMK_GPIO_RIMSC;
626 fimscreg = NMK_GPIO_FIMSC;
627 rimscval = &nmk_chip->rimsc;
628 fimscval = &nmk_chip->fimsc;
629 } else {
630 rimscreg = NMK_GPIO_RWIMSC;
631 fimscreg = NMK_GPIO_FWIMSC;
632 rimscval = &nmk_chip->rwimsc;
633 fimscval = &nmk_chip->fwimsc;
634 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100635
636 /* we must individually set/clear the two edges */
637 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100638 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530639 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100640 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530641 *rimscval &= ~bitmask;
642 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100643 }
644 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100645 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530646 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100647 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530648 *fimscval &= ~bitmask;
649 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100650 }
651}
652
Rabin Vincentb9df4682011-02-10 11:45:58 +0530653static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
654 int gpio, bool on)
655{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530656 /*
657 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
658 * disabled, since setting SLPM to 1 increases power consumption, and
659 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
660 */
661 if (nmk_chip->sleepmode && on) {
Linus Walleij33d78642011-06-09 11:08:47 +0200662 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530663 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200664 }
665
Rabin Vincentb9df4682011-02-10 11:45:58 +0530666 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
667}
668
669static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100670{
671 int gpio;
672 struct nmk_gpio_chip *nmk_chip;
673 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100674 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100675
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100676 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
677 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100678 bitmask = nmk_gpio_get_bitmask(gpio);
679 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100680 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100681
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200682 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530683 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
684 spin_lock(&nmk_chip->lock);
685
686 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
687
688 if (!(nmk_chip->real_wake & bitmask))
689 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
690
691 spin_unlock(&nmk_chip->lock);
692 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200693 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100694
695 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100696}
697
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100698static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100699{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530700 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100701}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100702
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100703static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100704{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530705 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100706}
707
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100708static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100709{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100710 struct nmk_gpio_chip *nmk_chip;
711 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530712 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100713 int gpio;
714
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100715 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
716 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100717 if (!nmk_chip)
718 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530719 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100720
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200721 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530722 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
723 spin_lock(&nmk_chip->lock);
724
Linus Walleij479a0c72011-09-20 10:50:15 +0200725 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530726 __nmk_gpio_set_wake(nmk_chip, gpio, on);
727
728 if (on)
729 nmk_chip->real_wake |= bitmask;
730 else
731 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530732
733 spin_unlock(&nmk_chip->lock);
734 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200735 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100736
737 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100738}
739
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100740static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100741{
Linus Walleij479a0c72011-09-20 10:50:15 +0200742 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200743 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100744 int gpio;
745 struct nmk_gpio_chip *nmk_chip;
746 unsigned long flags;
747 u32 bitmask;
748
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100749 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
750 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100751 bitmask = nmk_gpio_get_bitmask(gpio);
752 if (!nmk_chip)
753 return -EINVAL;
754
755 if (type & IRQ_TYPE_LEVEL_HIGH)
756 return -EINVAL;
757 if (type & IRQ_TYPE_LEVEL_LOW)
758 return -EINVAL;
759
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200760 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100761 spin_lock_irqsave(&nmk_chip->lock, flags);
762
Rabin Vincent7a852d82010-05-06 10:43:55 +0100763 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100764 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
765
Rabin Vincentb9df4682011-02-10 11:45:58 +0530766 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100767 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100768
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100769 nmk_chip->edge_rising &= ~bitmask;
770 if (type & IRQ_TYPE_EDGE_RISING)
771 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100772
773 nmk_chip->edge_falling &= ~bitmask;
774 if (type & IRQ_TYPE_EDGE_FALLING)
775 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100776
777 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100778 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
779
Rabin Vincentb9df4682011-02-10 11:45:58 +0530780 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100781 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100782
783 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200784 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100785
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100786 return 0;
787}
788
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200789static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
790{
791 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
792
793 clk_enable(nmk_chip->clk);
794 nmk_gpio_irq_unmask(d);
795 return 0;
796}
797
798static void nmk_gpio_irq_shutdown(struct irq_data *d)
799{
800 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
801
802 nmk_gpio_irq_mask(d);
803 clk_disable(nmk_chip->clk);
804}
805
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100806static struct irq_chip nmk_gpio_irq_chip = {
807 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100808 .irq_ack = nmk_gpio_irq_ack,
809 .irq_mask = nmk_gpio_irq_mask,
810 .irq_unmask = nmk_gpio_irq_unmask,
811 .irq_set_type = nmk_gpio_irq_set_type,
812 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200813 .irq_startup = nmk_gpio_irq_startup,
814 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100815};
816
Rabin Vincent33b744b2010-10-14 10:38:03 +0530817static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
818 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100819{
820 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100821 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100822 unsigned int first_irq;
823
Will Deaconadfed152011-02-28 10:12:29 +0000824 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100825
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100826 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100827 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530828 while (status) {
829 int bit = __ffs(status);
830
831 generic_handle_irq(first_irq + bit);
832 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100833 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100834
Will Deaconadfed152011-02-28 10:12:29 +0000835 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100836}
837
Rabin Vincent33b744b2010-10-14 10:38:03 +0530838static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
839{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100840 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200841 u32 status;
842
843 clk_enable(nmk_chip->clk);
844 status = readl(nmk_chip->addr + NMK_GPIO_IS);
845 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530846
847 __nmk_gpio_irq_handler(irq, desc, status);
848}
849
850static void nmk_gpio_secondary_irq_handler(unsigned int irq,
851 struct irq_desc *desc)
852{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100853 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530854 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
855
856 __nmk_gpio_irq_handler(irq, desc, status);
857}
858
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100859static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
860{
861 unsigned int first_irq;
862 int i;
863
864 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530865 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100866 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
867 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100868 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100869 irq_set_chip_data(i, nmk_chip);
870 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100871 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530872
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100873 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
874 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530875
876 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100877 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530878 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100879 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530880 }
881
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100882 return 0;
883}
884
885/* I/O Functions */
886static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
887{
888 struct nmk_gpio_chip *nmk_chip =
889 container_of(chip, struct nmk_gpio_chip, chip);
890
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200891 clk_enable(nmk_chip->clk);
892
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100893 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200894
895 clk_disable(nmk_chip->clk);
896
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100897 return 0;
898}
899
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100900static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
901{
902 struct nmk_gpio_chip *nmk_chip =
903 container_of(chip, struct nmk_gpio_chip, chip);
904 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200905 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100906
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200907 clk_enable(nmk_chip->clk);
908
909 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
910
911 clk_disable(nmk_chip->clk);
912
913 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100914}
915
916static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
917 int val)
918{
919 struct nmk_gpio_chip *nmk_chip =
920 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100921
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200922 clk_enable(nmk_chip->clk);
923
Rabin Vincent6720db72010-09-02 11:28:48 +0100924 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200925
926 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100927}
928
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100929static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
930 int val)
931{
932 struct nmk_gpio_chip *nmk_chip =
933 container_of(chip, struct nmk_gpio_chip, chip);
934
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200935 clk_enable(nmk_chip->clk);
936
Rabin Vincent6720db72010-09-02 11:28:48 +0100937 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100938
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200939 clk_disable(nmk_chip->clk);
940
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100941 return 0;
942}
943
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100944static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
945{
946 struct nmk_gpio_chip *nmk_chip =
947 container_of(chip, struct nmk_gpio_chip, chip);
948
949 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
950}
951
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530952#ifdef CONFIG_DEBUG_FS
953
954#include <linux/seq_file.h>
955
956static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
957{
958 int mode;
959 unsigned i;
960 unsigned gpio = chip->base;
961 int is_out;
962 struct nmk_gpio_chip *nmk_chip =
963 container_of(chip, struct nmk_gpio_chip, chip);
964 const char *modes[] = {
965 [NMK_GPIO_ALT_GPIO] = "gpio",
966 [NMK_GPIO_ALT_A] = "altA",
967 [NMK_GPIO_ALT_B] = "altB",
968 [NMK_GPIO_ALT_C] = "altC",
969 };
970
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200971 clk_enable(nmk_chip->clk);
972
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530973 for (i = 0; i < chip->ngpio; i++, gpio++) {
974 const char *label = gpiochip_is_requested(chip, i);
975 bool pull;
976 u32 bit = 1 << i;
977
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530978 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
979 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
980 mode = nmk_gpio_get_mode(gpio);
981 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200982 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530983 is_out ? "out" : "in ",
984 chip->get
985 ? (chip->get(chip, i) ? "hi" : "lo")
986 : "? ",
987 (mode < 0) ? "unknown" : modes[mode],
988 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200989
990 if (label && !is_out) {
991 int irq = gpio_to_irq(gpio);
992 struct irq_desc *desc = irq_to_desc(irq);
993
994 /* This races with request_irq(), set_irq_type(),
995 * and set_irq_wake() ... but those are "rare".
996 */
997 if (irq >= 0 && desc->action) {
998 char *trigger;
999 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1000
1001 if (nmk_chip->edge_rising & bitmask)
1002 trigger = "edge-rising";
1003 else if (nmk_chip->edge_falling & bitmask)
1004 trigger = "edge-falling";
1005 else
1006 trigger = "edge-undefined";
1007
1008 seq_printf(s, " irq-%d %s%s",
1009 irq, trigger,
1010 irqd_is_wakeup_set(&desc->irq_data)
1011 ? " wakeup" : "");
1012 }
1013 }
1014
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301015 seq_printf(s, "\n");
1016 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001017
1018 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301019}
1020
1021#else
1022#define nmk_gpio_dbg_show NULL
1023#endif
1024
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001025/* This structure is replicated for each GPIO block allocated at probe time */
1026static struct gpio_chip nmk_gpio_template = {
1027 .direction_input = nmk_gpio_make_input,
1028 .get = nmk_gpio_get_input,
1029 .direction_output = nmk_gpio_make_output,
1030 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001031 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301032 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001033 .can_sleep = 0,
1034};
1035
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001036void nmk_gpio_clocks_enable(void)
1037{
1038 int i;
1039
1040 for (i = 0; i < NUM_BANKS; i++) {
1041 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1042
1043 if (!chip)
1044 continue;
1045
1046 clk_enable(chip->clk);
1047 }
1048}
1049
1050void nmk_gpio_clocks_disable(void)
1051{
1052 int i;
1053
1054 for (i = 0; i < NUM_BANKS; i++) {
1055 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1056
1057 if (!chip)
1058 continue;
1059
1060 clk_disable(chip->clk);
1061 }
1062}
1063
Rabin Vincentb9df4682011-02-10 11:45:58 +05301064/*
1065 * Called from the suspend/resume path to only keep the real wakeup interrupts
1066 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1067 * and not the rest of the interrupts which we needed to have as wakeups for
1068 * cpuidle.
1069 *
1070 * PM ops are not used since this needs to be done at the end, after all the
1071 * other drivers are done with their suspend callbacks.
1072 */
1073void nmk_gpio_wakeups_suspend(void)
1074{
1075 int i;
1076
1077 for (i = 0; i < NUM_BANKS; i++) {
1078 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1079
1080 if (!chip)
1081 break;
1082
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001083 clk_enable(chip->clk);
1084
Rabin Vincentb9df4682011-02-10 11:45:58 +05301085 writel(chip->rwimsc & chip->real_wake,
1086 chip->addr + NMK_GPIO_RWIMSC);
1087 writel(chip->fwimsc & chip->real_wake,
1088 chip->addr + NMK_GPIO_FWIMSC);
1089
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001090 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301091 }
1092}
1093
1094void nmk_gpio_wakeups_resume(void)
1095{
1096 int i;
1097
1098 for (i = 0; i < NUM_BANKS; i++) {
1099 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1100
1101 if (!chip)
1102 break;
1103
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001104 clk_enable(chip->clk);
1105
Rabin Vincentb9df4682011-02-10 11:45:58 +05301106 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1107 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1108
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001109 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301110 }
1111}
1112
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001113/*
1114 * Read the pull up/pull down status.
1115 * A bit set in 'pull_up' means that pull up
1116 * is selected if pull is enabled in PDIS register.
1117 * Note: only pull up/down set via this driver can
1118 * be detected due to HW limitations.
1119 */
1120void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1121{
1122 if (gpio_bank < NUM_BANKS) {
1123 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1124
1125 if (!chip)
1126 return;
1127
1128 *pull_up = chip->pull_up;
1129 }
1130}
1131
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001132static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001133{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001134 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001135 struct nmk_gpio_chip *nmk_chip;
1136 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001137 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001138 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301139 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001140 void __iomem *base;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001141 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001142 int ret;
1143
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001144 if (!pdata)
1145 return -ENODEV;
1146
1147 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1148 if (!res) {
1149 ret = -ENOENT;
1150 goto out;
1151 }
1152
1153 irq = platform_get_irq(dev, 0);
1154 if (irq < 0) {
1155 ret = irq;
1156 goto out;
1157 }
1158
Rabin Vincent33b744b2010-10-14 10:38:03 +05301159 secondary_irq = platform_get_irq(dev, 1);
1160 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1161 ret = -EINVAL;
1162 goto out;
1163 }
1164
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001165 if (request_mem_region(res->start, resource_size(res),
1166 dev_name(&dev->dev)) == NULL) {
1167 ret = -EBUSY;
1168 goto out;
1169 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001170
Linus Walleij8d917712012-04-17 10:15:54 +02001171 base = ioremap(res->start, resource_size(res));
1172 if (!base) {
1173 ret = -ENOMEM;
1174 goto out_release;
1175 }
1176
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001177 clk = clk_get(&dev->dev, NULL);
1178 if (IS_ERR(clk)) {
1179 ret = PTR_ERR(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001180 goto out_unmap;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001181 }
1182
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001183 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1184 if (!nmk_chip) {
1185 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001186 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001187 }
1188 /*
1189 * The virt address in nmk_chip->addr is in the nomadik register space,
1190 * so we can simply convert the resource address, without remapping
1191 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301192 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001193 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001194 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001195 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001196 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301197 nmk_chip->secondary_parent_irq = secondary_irq;
1198 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301199 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001200 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001201 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001202
1203 chip = &nmk_chip->chip;
1204 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301205 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301206 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001207 chip->dev = &dev->dev;
1208 chip->owner = THIS_MODULE;
1209
Rabin Vincentebc61782011-09-28 15:49:11 +05301210 clk_enable(nmk_chip->clk);
1211 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1212 clk_disable(nmk_chip->clk);
1213
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001214 ret = gpiochip_add(&nmk_chip->chip);
1215 if (ret)
1216 goto out_free;
1217
Rabin Vincent01727e62010-12-13 12:02:40 +05301218 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1219
1220 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001221 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001222
1223 nmk_gpio_init_irq(nmk_chip);
1224
Grant Likely64842aa2011-11-06 11:36:18 -07001225 dev_info(&dev->dev, "at address %p\n",
1226 nmk_chip->addr);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001227 return 0;
1228
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001229out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001230 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001231out_clk:
1232 clk_disable(clk);
1233 clk_put(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001234out_unmap:
1235 iounmap(base);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001236out_release:
1237 release_mem_region(res->start, resource_size(res));
1238out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001239 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1240 pdata->first_gpio, pdata->first_gpio+31);
1241 return ret;
1242}
1243
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001244static struct platform_driver nmk_gpio_driver = {
1245 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001246 .owner = THIS_MODULE,
1247 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301248 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001249 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001250};
1251
1252static int __init nmk_gpio_init(void)
1253{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001254 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001255}
1256
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001257core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001258
1259MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1260MODULE_DESCRIPTION("Nomadik GPIO Driver");
1261MODULE_LICENSE("GPL");