blob: 681daee80291abb91aa43537a5662124e3eb332e [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#include <mach/hardware.h>
Russell King75482dc2011-07-26 10:57:37 +010032#include <asm/gpio.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010033
34/*
35 * The GPIO module in the Nomadik family of Systems-on-Chip is an
36 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020037 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010038 *
39 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
40 */
41
Rabin Vincent01727e62010-12-13 12:02:40 +053042#define NMK_GPIO_PER_CHIP 32
43
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010044struct nmk_gpio_chip {
45 struct gpio_chip chip;
46 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010047 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053048 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010049 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053050 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053051 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053052 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010053 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020054 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010055 /* Keep track of configured edges */
56 u32 edge_rising;
57 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053058 u32 real_wake;
59 u32 rwimsc;
60 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053061 u32 rimsc;
62 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020063 u32 pull_up;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010064};
65
Rabin Vincent01727e62010-12-13 12:02:40 +053066static struct nmk_gpio_chip *
67nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
68
69static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
70
71#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
72
Rabin Vincent6f9a9742010-06-02 05:50:28 +010073static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
74 unsigned offset, int gpio_mode)
75{
76 u32 bit = 1 << offset;
77 u32 afunc, bfunc;
78
79 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
80 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
81 if (gpio_mode & NMK_GPIO_ALT_A)
82 afunc |= bit;
83 if (gpio_mode & NMK_GPIO_ALT_B)
84 bfunc |= bit;
85 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
86 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
87}
88
Rabin Vincent81a3c292010-05-27 12:39:23 +010089static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
90 unsigned offset, enum nmk_gpio_slpm mode)
91{
92 u32 bit = 1 << offset;
93 u32 slpm;
94
95 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
96 if (mode == NMK_GPIO_SLPM_NOCHANGE)
97 slpm |= bit;
98 else
99 slpm &= ~bit;
100 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
101}
102
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100103static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
104 unsigned offset, enum nmk_gpio_pull pull)
105{
106 u32 bit = 1 << offset;
107 u32 pdis;
108
109 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200110 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100111 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200112 nmk_chip->pull_up &= ~bit;
113 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100114 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200115 }
116
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100117 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
118
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200119 if (pull == NMK_GPIO_PULL_UP) {
120 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100121 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200122 } else if (pull == NMK_GPIO_PULL_DOWN) {
123 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100124 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200125 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100126}
127
Rabin Vincent378be062010-06-02 06:06:29 +0100128static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
129 unsigned offset)
130{
131 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
132}
133
Rabin Vincent6720db72010-09-02 11:28:48 +0100134static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
135 unsigned offset, int val)
136{
137 if (val)
138 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
139 else
140 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
141}
142
143static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
144 unsigned offset, int val)
145{
146 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
147 __nmk_gpio_set_output(nmk_chip, offset, val);
148}
149
Rabin Vincent01727e62010-12-13 12:02:40 +0530150static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
151 unsigned offset, int gpio_mode,
152 bool glitch)
153{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530154 u32 rwimsc = nmk_chip->rwimsc;
155 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530156
157 if (glitch && nmk_chip->set_ioforce) {
158 u32 bit = BIT(offset);
159
Rabin Vincent01727e62010-12-13 12:02:40 +0530160 /* Prevent spurious wakeups */
161 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
162 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
163
164 nmk_chip->set_ioforce(true);
165 }
166
167 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
168
169 if (glitch && nmk_chip->set_ioforce) {
170 nmk_chip->set_ioforce(false);
171
172 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
173 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
174 }
175}
176
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530177static void
178nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
179{
180 u32 falling = nmk_chip->fimsc & BIT(offset);
181 u32 rising = nmk_chip->rimsc & BIT(offset);
182 int gpio = nmk_chip->chip.base + offset;
183 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
184 struct irq_data *d = irq_get_irq_data(irq);
185
186 if (!rising && !falling)
187 return;
188
189 if (!d || !irqd_irq_disabled(d))
190 return;
191
192 if (rising) {
193 nmk_chip->rimsc &= ~BIT(offset);
194 writel_relaxed(nmk_chip->rimsc,
195 nmk_chip->addr + NMK_GPIO_RIMSC);
196 }
197
198 if (falling) {
199 nmk_chip->fimsc &= ~BIT(offset);
200 writel_relaxed(nmk_chip->fimsc,
201 nmk_chip->addr + NMK_GPIO_FIMSC);
202 }
203
204 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
205}
206
Rabin Vincent378be062010-06-02 06:06:29 +0100207static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530208 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100209{
210 static const char *afnames[] = {
211 [NMK_GPIO_ALT_GPIO] = "GPIO",
212 [NMK_GPIO_ALT_A] = "A",
213 [NMK_GPIO_ALT_B] = "B",
214 [NMK_GPIO_ALT_C] = "C"
215 };
216 static const char *pullnames[] = {
217 [NMK_GPIO_PULL_NONE] = "none",
218 [NMK_GPIO_PULL_UP] = "up",
219 [NMK_GPIO_PULL_DOWN] = "down",
220 [3] /* illegal */ = "??"
221 };
222 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100223 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
224 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100225 };
226
227 int pin = PIN_NUM(cfg);
228 int pull = PIN_PULL(cfg);
229 int af = PIN_ALT(cfg);
230 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100231 int output = PIN_DIR(cfg);
232 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530233 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100234
Rabin Vincentdacdc962010-12-03 20:35:37 +0530235 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
236 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100237 output ? "output " : "input",
238 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100239
Rabin Vincentdacdc962010-12-03 20:35:37 +0530240 if (sleep) {
241 int slpm_pull = PIN_SLPM_PULL(cfg);
242 int slpm_output = PIN_SLPM_DIR(cfg);
243 int slpm_val = PIN_SLPM_VAL(cfg);
244
Rabin Vincent3546d152010-11-25 11:38:27 +0530245 af = NMK_GPIO_ALT_GPIO;
246
Rabin Vincentdacdc962010-12-03 20:35:37 +0530247 /*
248 * The SLPM_* values are normal values + 1 to allow zero to
249 * mean "same as normal".
250 */
251 if (slpm_pull)
252 pull = slpm_pull - 1;
253 if (slpm_output)
254 output = slpm_output - 1;
255 if (slpm_val)
256 val = slpm_val - 1;
257
258 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
259 pin,
260 slpm_pull ? pullnames[pull] : "same",
261 slpm_output ? (output ? "output" : "input") : "same",
262 slpm_val ? (val ? "high" : "low") : "same");
263 }
264
Rabin Vincent6720db72010-09-02 11:28:48 +0100265 if (output)
266 __nmk_gpio_make_output(nmk_chip, offset, val);
267 else {
268 __nmk_gpio_make_input(nmk_chip, offset);
269 __nmk_gpio_set_pull(nmk_chip, offset, pull);
270 }
271
Rabin Vincent01727e62010-12-13 12:02:40 +0530272 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530273 * If the pin is switching to altfunc, and there was an interrupt
274 * installed on it which has been lazy disabled, actually mask the
275 * interrupt to prevent spurious interrupts that would occur while the
276 * pin is under control of the peripheral. Only SKE does this.
277 */
278 if (af != NMK_GPIO_ALT_GPIO)
279 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
280
281 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530282 * If we've backed up the SLPM registers (glitch workaround), modify
283 * the backups since they will be restored.
284 */
285 if (slpmregs) {
286 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
287 slpmregs[nmk_chip->bank] |= BIT(offset);
288 else
289 slpmregs[nmk_chip->bank] &= ~BIT(offset);
290 } else
291 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
292
293 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
294}
295
296/*
297 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
298 * - Save SLPM registers
299 * - Set SLPM=0 for the IOs you want to switch and others to 1
300 * - Configure the GPIO registers for the IOs that are being switched
301 * - Set IOFORCE=1
302 * - Modify the AFLSA/B registers for the IOs that are being switched
303 * - Set IOFORCE=0
304 * - Restore SLPM registers
305 * - Any spurious wake up event during switch sequence to be ignored and
306 * cleared
307 */
308static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
309{
310 int i;
311
312 for (i = 0; i < NUM_BANKS; i++) {
313 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
314 unsigned int temp = slpm[i];
315
316 if (!chip)
317 break;
318
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200319 clk_enable(chip->clk);
320
Rabin Vincent01727e62010-12-13 12:02:40 +0530321 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
322 writel(temp, chip->addr + NMK_GPIO_SLPC);
323 }
324}
325
326static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
327{
328 int i;
329
330 for (i = 0; i < NUM_BANKS; i++) {
331 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
332
333 if (!chip)
334 break;
335
336 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200337
338 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530339 }
340}
341
342static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
343{
344 static unsigned int slpm[NUM_BANKS];
345 unsigned long flags;
346 bool glitch = false;
347 int ret = 0;
348 int i;
349
350 for (i = 0; i < num; i++) {
351 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
352 glitch = true;
353 break;
354 }
355 }
356
357 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
358
359 if (glitch) {
360 memset(slpm, 0xff, sizeof(slpm));
361
362 for (i = 0; i < num; i++) {
363 int pin = PIN_NUM(cfgs[i]);
364 int offset = pin % NMK_GPIO_PER_CHIP;
365
366 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
367 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
368 }
369
370 nmk_gpio_glitch_slpm_init(slpm);
371 }
372
373 for (i = 0; i < num; i++) {
374 struct nmk_gpio_chip *nmk_chip;
375 int pin = PIN_NUM(cfgs[i]);
376
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100377 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530378 if (!nmk_chip) {
379 ret = -EINVAL;
380 break;
381 }
382
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200383 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530384 spin_lock(&nmk_chip->lock);
385 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
386 cfgs[i], sleep, glitch ? slpm : NULL);
387 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200388 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530389 }
390
391 if (glitch)
392 nmk_gpio_glitch_slpm_restore(slpm);
393
394 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
395
396 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100397}
398
399/**
400 * nmk_config_pin - configure a pin's mux attributes
401 * @cfg: pin confguration
402 *
403 * Configures a pin's mode (alternate function or GPIO), its pull up status,
404 * and its sleep mode based on the specified configuration. The @cfg is
405 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
406 * are constructed using, and can be further enhanced with, the macros in
407 * plat/pincfg.h.
408 *
409 * If a pin's mode is set to GPIO, it is configured as an input to avoid
410 * side-effects. The gpio can be manipulated later using standard GPIO API
411 * calls.
412 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530413int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100414{
Rabin Vincent01727e62010-12-13 12:02:40 +0530415 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100416}
417EXPORT_SYMBOL(nmk_config_pin);
418
419/**
420 * nmk_config_pins - configure several pins at once
421 * @cfgs: array of pin configurations
422 * @num: number of elments in the array
423 *
424 * Configures several pins using nmk_config_pin(). Refer to that function for
425 * further information.
426 */
427int nmk_config_pins(pin_cfg_t *cfgs, int num)
428{
Rabin Vincent01727e62010-12-13 12:02:40 +0530429 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100430}
431EXPORT_SYMBOL(nmk_config_pins);
432
Rabin Vincentdacdc962010-12-03 20:35:37 +0530433int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
434{
Rabin Vincent01727e62010-12-13 12:02:40 +0530435 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530436}
437EXPORT_SYMBOL(nmk_config_pins_sleep);
438
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100439/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100440 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
441 * @gpio: pin number
442 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
443 *
Linus Walleij33d78642011-06-09 11:08:47 +0200444 * This register is actually in the pinmux layer, not the GPIO block itself.
445 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
446 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
447 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
448 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
449 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
450 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100451 *
Linus Walleij33d78642011-06-09 11:08:47 +0200452 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
453 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
454 * entered) regardless of the altfunction selected. Also wake-up detection is
455 * ENABLED.
456 *
457 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
458 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
459 * (for altfunction GPIO) or respective on-chip peripherals (for other
460 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
461 *
462 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100463 */
464int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
465{
466 struct nmk_gpio_chip *nmk_chip;
467 unsigned long flags;
468
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100469 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100470 if (!nmk_chip)
471 return -EINVAL;
472
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200473 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530474 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
475 spin_lock(&nmk_chip->lock);
476
Rabin Vincent81a3c292010-05-27 12:39:23 +0100477 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530478
479 spin_unlock(&nmk_chip->lock);
480 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200481 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100482
483 return 0;
484}
485
486/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100487 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
488 * @gpio: pin number
489 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
490 *
491 * Enables/disables pull up/down on a specified pin. This only takes effect if
492 * the pin is configured as an input (either explicitly or by the alternate
493 * function).
494 *
495 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
496 * configured as an input. Otherwise, due to the way the controller registers
497 * work, this function will change the value output on the pin.
498 */
499int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
500{
501 struct nmk_gpio_chip *nmk_chip;
502 unsigned long flags;
503
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100504 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100505 if (!nmk_chip)
506 return -EINVAL;
507
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200508 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100509 spin_lock_irqsave(&nmk_chip->lock, flags);
510 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
511 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200512 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100513
514 return 0;
515}
516
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100517/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200518/**
519 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
520 * @gpio: pin number
521 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
522 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
523 *
524 * Sets the mode of the specified pin to one of the alternate functions or
525 * plain GPIO.
526 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100527int nmk_gpio_set_mode(int gpio, int gpio_mode)
528{
529 struct nmk_gpio_chip *nmk_chip;
530 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100531
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100532 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100533 if (!nmk_chip)
534 return -EINVAL;
535
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200536 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100537 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100538 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100539 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200540 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100541
542 return 0;
543}
544EXPORT_SYMBOL(nmk_gpio_set_mode);
545
546int nmk_gpio_get_mode(int gpio)
547{
548 struct nmk_gpio_chip *nmk_chip;
549 u32 afunc, bfunc, bit;
550
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
555 bit = 1 << (gpio - nmk_chip->chip.base);
556
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200557 clk_enable(nmk_chip->clk);
558
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100559 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
560 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
561
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200562 clk_disable(nmk_chip->clk);
563
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100564 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
565}
566EXPORT_SYMBOL(nmk_gpio_get_mode);
567
568
569/* IRQ functions */
570static inline int nmk_gpio_get_bitmask(int gpio)
571{
572 return 1 << (gpio % 32);
573}
574
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100575static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100576{
577 int gpio;
578 struct nmk_gpio_chip *nmk_chip;
579
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100580 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
581 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100582 if (!nmk_chip)
583 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200584
585 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100586 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200587 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100588}
589
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100590enum nmk_gpio_irq_type {
591 NORMAL,
592 WAKE,
593};
594
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100595static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100596 int gpio, enum nmk_gpio_irq_type which,
597 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100598{
599 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530600 u32 *rimscval;
601 u32 *fimscval;
602 u32 rimscreg;
603 u32 fimscreg;
604
605 if (which == NORMAL) {
606 rimscreg = NMK_GPIO_RIMSC;
607 fimscreg = NMK_GPIO_FIMSC;
608 rimscval = &nmk_chip->rimsc;
609 fimscval = &nmk_chip->fimsc;
610 } else {
611 rimscreg = NMK_GPIO_RWIMSC;
612 fimscreg = NMK_GPIO_FWIMSC;
613 rimscval = &nmk_chip->rwimsc;
614 fimscval = &nmk_chip->fwimsc;
615 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100616
617 /* we must individually set/clear the two edges */
618 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100619 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530620 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100621 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530622 *rimscval &= ~bitmask;
623 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100624 }
625 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100626 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530627 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100628 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530629 *fimscval &= ~bitmask;
630 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100631 }
632}
633
Rabin Vincentb9df4682011-02-10 11:45:58 +0530634static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
635 int gpio, bool on)
636{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530637 /*
638 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
639 * disabled, since setting SLPM to 1 increases power consumption, and
640 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
641 */
642 if (nmk_chip->sleepmode && on) {
Linus Walleij33d78642011-06-09 11:08:47 +0200643 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530644 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200645 }
646
Rabin Vincentb9df4682011-02-10 11:45:58 +0530647 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
648}
649
650static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100651{
652 int gpio;
653 struct nmk_gpio_chip *nmk_chip;
654 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100655 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100656
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100657 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
658 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100659 bitmask = nmk_gpio_get_bitmask(gpio);
660 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100661 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100662
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200663 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530664 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
665 spin_lock(&nmk_chip->lock);
666
667 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
668
669 if (!(nmk_chip->real_wake & bitmask))
670 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
671
672 spin_unlock(&nmk_chip->lock);
673 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200674 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100675
676 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100677}
678
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100679static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100680{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530681 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100682}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100684static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100685{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530686 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100687}
688
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100689static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100690{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100691 struct nmk_gpio_chip *nmk_chip;
692 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530693 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100694 int gpio;
695
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100696 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
697 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100698 if (!nmk_chip)
699 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530700 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100701
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200702 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530703 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
704 spin_lock(&nmk_chip->lock);
705
Linus Walleij479a0c72011-09-20 10:50:15 +0200706 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530707 __nmk_gpio_set_wake(nmk_chip, gpio, on);
708
709 if (on)
710 nmk_chip->real_wake |= bitmask;
711 else
712 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530713
714 spin_unlock(&nmk_chip->lock);
715 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200716 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100717
718 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100719}
720
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100721static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100722{
Linus Walleij479a0c72011-09-20 10:50:15 +0200723 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200724 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100725 int gpio;
726 struct nmk_gpio_chip *nmk_chip;
727 unsigned long flags;
728 u32 bitmask;
729
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100730 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
731 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100732 bitmask = nmk_gpio_get_bitmask(gpio);
733 if (!nmk_chip)
734 return -EINVAL;
735
736 if (type & IRQ_TYPE_LEVEL_HIGH)
737 return -EINVAL;
738 if (type & IRQ_TYPE_LEVEL_LOW)
739 return -EINVAL;
740
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200741 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100742 spin_lock_irqsave(&nmk_chip->lock, flags);
743
Rabin Vincent7a852d82010-05-06 10:43:55 +0100744 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100745 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
746
Rabin Vincentb9df4682011-02-10 11:45:58 +0530747 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100748 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100749
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100750 nmk_chip->edge_rising &= ~bitmask;
751 if (type & IRQ_TYPE_EDGE_RISING)
752 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100753
754 nmk_chip->edge_falling &= ~bitmask;
755 if (type & IRQ_TYPE_EDGE_FALLING)
756 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100757
758 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100759 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
760
Rabin Vincentb9df4682011-02-10 11:45:58 +0530761 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100762 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100763
764 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200765 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100766
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100767 return 0;
768}
769
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200770static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
771{
772 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
773
774 clk_enable(nmk_chip->clk);
775 nmk_gpio_irq_unmask(d);
776 return 0;
777}
778
779static void nmk_gpio_irq_shutdown(struct irq_data *d)
780{
781 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
782
783 nmk_gpio_irq_mask(d);
784 clk_disable(nmk_chip->clk);
785}
786
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100787static struct irq_chip nmk_gpio_irq_chip = {
788 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100789 .irq_ack = nmk_gpio_irq_ack,
790 .irq_mask = nmk_gpio_irq_mask,
791 .irq_unmask = nmk_gpio_irq_unmask,
792 .irq_set_type = nmk_gpio_irq_set_type,
793 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200794 .irq_startup = nmk_gpio_irq_startup,
795 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100796};
797
Rabin Vincent33b744b2010-10-14 10:38:03 +0530798static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
799 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100800{
801 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100802 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100803 unsigned int first_irq;
804
Will Deaconadfed152011-02-28 10:12:29 +0000805 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100806
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100807 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100808 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530809 while (status) {
810 int bit = __ffs(status);
811
812 generic_handle_irq(first_irq + bit);
813 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100814 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100815
Will Deaconadfed152011-02-28 10:12:29 +0000816 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100817}
818
Rabin Vincent33b744b2010-10-14 10:38:03 +0530819static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
820{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100821 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200822 u32 status;
823
824 clk_enable(nmk_chip->clk);
825 status = readl(nmk_chip->addr + NMK_GPIO_IS);
826 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530827
828 __nmk_gpio_irq_handler(irq, desc, status);
829}
830
831static void nmk_gpio_secondary_irq_handler(unsigned int irq,
832 struct irq_desc *desc)
833{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100834 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530835 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
836
837 __nmk_gpio_irq_handler(irq, desc, status);
838}
839
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100840static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
841{
842 unsigned int first_irq;
843 int i;
844
845 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530846 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100847 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
848 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100849 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100850 irq_set_chip_data(i, nmk_chip);
851 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100852 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530853
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100854 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
855 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530856
857 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100858 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530859 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100860 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530861 }
862
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100863 return 0;
864}
865
866/* I/O Functions */
867static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
868{
869 struct nmk_gpio_chip *nmk_chip =
870 container_of(chip, struct nmk_gpio_chip, chip);
871
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200872 clk_enable(nmk_chip->clk);
873
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100874 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200875
876 clk_disable(nmk_chip->clk);
877
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100878 return 0;
879}
880
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100881static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
882{
883 struct nmk_gpio_chip *nmk_chip =
884 container_of(chip, struct nmk_gpio_chip, chip);
885 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200886 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100887
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200888 clk_enable(nmk_chip->clk);
889
890 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
891
892 clk_disable(nmk_chip->clk);
893
894 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100895}
896
897static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
898 int val)
899{
900 struct nmk_gpio_chip *nmk_chip =
901 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100902
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200903 clk_enable(nmk_chip->clk);
904
Rabin Vincent6720db72010-09-02 11:28:48 +0100905 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200906
907 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100908}
909
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100910static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
911 int val)
912{
913 struct nmk_gpio_chip *nmk_chip =
914 container_of(chip, struct nmk_gpio_chip, chip);
915
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200916 clk_enable(nmk_chip->clk);
917
Rabin Vincent6720db72010-09-02 11:28:48 +0100918 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100919
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200920 clk_disable(nmk_chip->clk);
921
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100922 return 0;
923}
924
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100925static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
926{
927 struct nmk_gpio_chip *nmk_chip =
928 container_of(chip, struct nmk_gpio_chip, chip);
929
930 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
931}
932
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530933#ifdef CONFIG_DEBUG_FS
934
935#include <linux/seq_file.h>
936
937static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
938{
939 int mode;
940 unsigned i;
941 unsigned gpio = chip->base;
942 int is_out;
943 struct nmk_gpio_chip *nmk_chip =
944 container_of(chip, struct nmk_gpio_chip, chip);
945 const char *modes[] = {
946 [NMK_GPIO_ALT_GPIO] = "gpio",
947 [NMK_GPIO_ALT_A] = "altA",
948 [NMK_GPIO_ALT_B] = "altB",
949 [NMK_GPIO_ALT_C] = "altC",
950 };
951
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200952 clk_enable(nmk_chip->clk);
953
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530954 for (i = 0; i < chip->ngpio; i++, gpio++) {
955 const char *label = gpiochip_is_requested(chip, i);
956 bool pull;
957 u32 bit = 1 << i;
958
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530959 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
960 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
961 mode = nmk_gpio_get_mode(gpio);
962 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200963 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530964 is_out ? "out" : "in ",
965 chip->get
966 ? (chip->get(chip, i) ? "hi" : "lo")
967 : "? ",
968 (mode < 0) ? "unknown" : modes[mode],
969 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200970
971 if (label && !is_out) {
972 int irq = gpio_to_irq(gpio);
973 struct irq_desc *desc = irq_to_desc(irq);
974
975 /* This races with request_irq(), set_irq_type(),
976 * and set_irq_wake() ... but those are "rare".
977 */
978 if (irq >= 0 && desc->action) {
979 char *trigger;
980 u32 bitmask = nmk_gpio_get_bitmask(gpio);
981
982 if (nmk_chip->edge_rising & bitmask)
983 trigger = "edge-rising";
984 else if (nmk_chip->edge_falling & bitmask)
985 trigger = "edge-falling";
986 else
987 trigger = "edge-undefined";
988
989 seq_printf(s, " irq-%d %s%s",
990 irq, trigger,
991 irqd_is_wakeup_set(&desc->irq_data)
992 ? " wakeup" : "");
993 }
994 }
995
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530996 seq_printf(s, "\n");
997 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200998
999 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301000}
1001
1002#else
1003#define nmk_gpio_dbg_show NULL
1004#endif
1005
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001006/* This structure is replicated for each GPIO block allocated at probe time */
1007static struct gpio_chip nmk_gpio_template = {
1008 .direction_input = nmk_gpio_make_input,
1009 .get = nmk_gpio_get_input,
1010 .direction_output = nmk_gpio_make_output,
1011 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001012 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301013 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001014 .can_sleep = 0,
1015};
1016
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001017void nmk_gpio_clocks_enable(void)
1018{
1019 int i;
1020
1021 for (i = 0; i < NUM_BANKS; i++) {
1022 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1023
1024 if (!chip)
1025 continue;
1026
1027 clk_enable(chip->clk);
1028 }
1029}
1030
1031void nmk_gpio_clocks_disable(void)
1032{
1033 int i;
1034
1035 for (i = 0; i < NUM_BANKS; i++) {
1036 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1037
1038 if (!chip)
1039 continue;
1040
1041 clk_disable(chip->clk);
1042 }
1043}
1044
Rabin Vincentb9df4682011-02-10 11:45:58 +05301045/*
1046 * Called from the suspend/resume path to only keep the real wakeup interrupts
1047 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1048 * and not the rest of the interrupts which we needed to have as wakeups for
1049 * cpuidle.
1050 *
1051 * PM ops are not used since this needs to be done at the end, after all the
1052 * other drivers are done with their suspend callbacks.
1053 */
1054void nmk_gpio_wakeups_suspend(void)
1055{
1056 int i;
1057
1058 for (i = 0; i < NUM_BANKS; i++) {
1059 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1060
1061 if (!chip)
1062 break;
1063
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001064 clk_enable(chip->clk);
1065
Rabin Vincentb9df4682011-02-10 11:45:58 +05301066 writel(chip->rwimsc & chip->real_wake,
1067 chip->addr + NMK_GPIO_RWIMSC);
1068 writel(chip->fwimsc & chip->real_wake,
1069 chip->addr + NMK_GPIO_FWIMSC);
1070
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001071 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301072 }
1073}
1074
1075void nmk_gpio_wakeups_resume(void)
1076{
1077 int i;
1078
1079 for (i = 0; i < NUM_BANKS; i++) {
1080 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1081
1082 if (!chip)
1083 break;
1084
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001085 clk_enable(chip->clk);
1086
Rabin Vincentb9df4682011-02-10 11:45:58 +05301087 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1088 writel(chip->fwimsc, 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
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001094/*
1095 * Read the pull up/pull down status.
1096 * A bit set in 'pull_up' means that pull up
1097 * is selected if pull is enabled in PDIS register.
1098 * Note: only pull up/down set via this driver can
1099 * be detected due to HW limitations.
1100 */
1101void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1102{
1103 if (gpio_bank < NUM_BANKS) {
1104 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1105
1106 if (!chip)
1107 return;
1108
1109 *pull_up = chip->pull_up;
1110 }
1111}
1112
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001113static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001114{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001115 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001116 struct nmk_gpio_chip *nmk_chip;
1117 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001118 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001119 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301120 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001121 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001122 int ret;
1123
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001124 if (!pdata)
1125 return -ENODEV;
1126
1127 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1128 if (!res) {
1129 ret = -ENOENT;
1130 goto out;
1131 }
1132
1133 irq = platform_get_irq(dev, 0);
1134 if (irq < 0) {
1135 ret = irq;
1136 goto out;
1137 }
1138
Rabin Vincent33b744b2010-10-14 10:38:03 +05301139 secondary_irq = platform_get_irq(dev, 1);
1140 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1141 ret = -EINVAL;
1142 goto out;
1143 }
1144
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001145 if (request_mem_region(res->start, resource_size(res),
1146 dev_name(&dev->dev)) == NULL) {
1147 ret = -EBUSY;
1148 goto out;
1149 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001150
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001151 clk = clk_get(&dev->dev, NULL);
1152 if (IS_ERR(clk)) {
1153 ret = PTR_ERR(clk);
1154 goto out_release;
1155 }
1156
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001157 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1158 if (!nmk_chip) {
1159 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001160 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001161 }
1162 /*
1163 * The virt address in nmk_chip->addr is in the nomadik register space,
1164 * so we can simply convert the resource address, without remapping
1165 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301166 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001167 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001168 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001169 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001170 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301171 nmk_chip->secondary_parent_irq = secondary_irq;
1172 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301173 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001174 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001175 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001176
1177 chip = &nmk_chip->chip;
1178 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301179 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301180 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001181 chip->dev = &dev->dev;
1182 chip->owner = THIS_MODULE;
1183
1184 ret = gpiochip_add(&nmk_chip->chip);
1185 if (ret)
1186 goto out_free;
1187
Rabin Vincent01727e62010-12-13 12:02:40 +05301188 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1189
1190 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001191 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001192
1193 nmk_gpio_init_irq(nmk_chip);
1194
Grant Likely64842aa2011-11-06 11:36:18 -07001195 dev_info(&dev->dev, "at address %p\n",
1196 nmk_chip->addr);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001197 return 0;
1198
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001199out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001200 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001201out_clk:
1202 clk_disable(clk);
1203 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001204out_release:
1205 release_mem_region(res->start, resource_size(res));
1206out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001207 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1208 pdata->first_gpio, pdata->first_gpio+31);
1209 return ret;
1210}
1211
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001212static struct platform_driver nmk_gpio_driver = {
1213 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001214 .owner = THIS_MODULE,
1215 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301216 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001217 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001218};
1219
1220static int __init nmk_gpio_init(void)
1221{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001222 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001223}
1224
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001225core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001226
1227MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1228MODULE_DESCRIPTION("Nomadik GPIO Driver");
1229MODULE_LICENSE("GPL");