blob: 7b45d88b4f44256b845a7505639cd43502303b93 [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;
Rabin Vincentebc61782011-09-28 15:49:11 +053064 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010065};
66
Rabin Vincent01727e62010-12-13 12:02:40 +053067static struct nmk_gpio_chip *
68nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
69
70static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
71
72#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
73
Rabin Vincent6f9a9742010-06-02 05:50:28 +010074static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
75 unsigned offset, int gpio_mode)
76{
77 u32 bit = 1 << offset;
78 u32 afunc, bfunc;
79
80 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
81 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
82 if (gpio_mode & NMK_GPIO_ALT_A)
83 afunc |= bit;
84 if (gpio_mode & NMK_GPIO_ALT_B)
85 bfunc |= bit;
86 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
87 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
88}
89
Rabin Vincent81a3c292010-05-27 12:39:23 +010090static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
91 unsigned offset, enum nmk_gpio_slpm mode)
92{
93 u32 bit = 1 << offset;
94 u32 slpm;
95
96 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
97 if (mode == NMK_GPIO_SLPM_NOCHANGE)
98 slpm |= bit;
99 else
100 slpm &= ~bit;
101 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
102}
103
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100104static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
105 unsigned offset, enum nmk_gpio_pull pull)
106{
107 u32 bit = 1 << offset;
108 u32 pdis;
109
110 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200111 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100112 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200113 nmk_chip->pull_up &= ~bit;
114 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100115 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200116 }
117
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100118 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
119
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200120 if (pull == NMK_GPIO_PULL_UP) {
121 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100122 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200123 } else if (pull == NMK_GPIO_PULL_DOWN) {
124 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100125 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200126 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100127}
128
Rabin Vincentebc61782011-09-28 15:49:11 +0530129static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
130 unsigned offset, bool lowemi)
131{
132 u32 bit = BIT(offset);
133 bool enabled = nmk_chip->lowemi & bit;
134
135 if (lowemi == enabled)
136 return;
137
138 if (lowemi)
139 nmk_chip->lowemi |= bit;
140 else
141 nmk_chip->lowemi &= ~bit;
142
143 writel_relaxed(nmk_chip->lowemi,
144 nmk_chip->addr + NMK_GPIO_LOWEMI);
145}
146
Rabin Vincent378be062010-06-02 06:06:29 +0100147static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
148 unsigned offset)
149{
150 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
151}
152
Rabin Vincent6720db72010-09-02 11:28:48 +0100153static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
154 unsigned offset, int val)
155{
156 if (val)
157 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
158 else
159 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
160}
161
162static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
163 unsigned offset, int val)
164{
165 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
166 __nmk_gpio_set_output(nmk_chip, offset, val);
167}
168
Rabin Vincent01727e62010-12-13 12:02:40 +0530169static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
170 unsigned offset, int gpio_mode,
171 bool glitch)
172{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530173 u32 rwimsc = nmk_chip->rwimsc;
174 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530175
176 if (glitch && nmk_chip->set_ioforce) {
177 u32 bit = BIT(offset);
178
Rabin Vincent01727e62010-12-13 12:02:40 +0530179 /* Prevent spurious wakeups */
180 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
181 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
182
183 nmk_chip->set_ioforce(true);
184 }
185
186 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
187
188 if (glitch && nmk_chip->set_ioforce) {
189 nmk_chip->set_ioforce(false);
190
191 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
192 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
193 }
194}
195
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530196static void
197nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
198{
199 u32 falling = nmk_chip->fimsc & BIT(offset);
200 u32 rising = nmk_chip->rimsc & BIT(offset);
201 int gpio = nmk_chip->chip.base + offset;
202 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
203 struct irq_data *d = irq_get_irq_data(irq);
204
205 if (!rising && !falling)
206 return;
207
208 if (!d || !irqd_irq_disabled(d))
209 return;
210
211 if (rising) {
212 nmk_chip->rimsc &= ~BIT(offset);
213 writel_relaxed(nmk_chip->rimsc,
214 nmk_chip->addr + NMK_GPIO_RIMSC);
215 }
216
217 if (falling) {
218 nmk_chip->fimsc &= ~BIT(offset);
219 writel_relaxed(nmk_chip->fimsc,
220 nmk_chip->addr + NMK_GPIO_FIMSC);
221 }
222
223 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
224}
225
Rabin Vincent378be062010-06-02 06:06:29 +0100226static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530227 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100228{
229 static const char *afnames[] = {
230 [NMK_GPIO_ALT_GPIO] = "GPIO",
231 [NMK_GPIO_ALT_A] = "A",
232 [NMK_GPIO_ALT_B] = "B",
233 [NMK_GPIO_ALT_C] = "C"
234 };
235 static const char *pullnames[] = {
236 [NMK_GPIO_PULL_NONE] = "none",
237 [NMK_GPIO_PULL_UP] = "up",
238 [NMK_GPIO_PULL_DOWN] = "down",
239 [3] /* illegal */ = "??"
240 };
241 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100242 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
243 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100244 };
245
246 int pin = PIN_NUM(cfg);
247 int pull = PIN_PULL(cfg);
248 int af = PIN_ALT(cfg);
249 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100250 int output = PIN_DIR(cfg);
251 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530252 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100253
Rabin Vincentdacdc962010-12-03 20:35:37 +0530254 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
255 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100256 output ? "output " : "input",
257 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100258
Rabin Vincentdacdc962010-12-03 20:35:37 +0530259 if (sleep) {
260 int slpm_pull = PIN_SLPM_PULL(cfg);
261 int slpm_output = PIN_SLPM_DIR(cfg);
262 int slpm_val = PIN_SLPM_VAL(cfg);
263
Rabin Vincent3546d152010-11-25 11:38:27 +0530264 af = NMK_GPIO_ALT_GPIO;
265
Rabin Vincentdacdc962010-12-03 20:35:37 +0530266 /*
267 * The SLPM_* values are normal values + 1 to allow zero to
268 * mean "same as normal".
269 */
270 if (slpm_pull)
271 pull = slpm_pull - 1;
272 if (slpm_output)
273 output = slpm_output - 1;
274 if (slpm_val)
275 val = slpm_val - 1;
276
277 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
278 pin,
279 slpm_pull ? pullnames[pull] : "same",
280 slpm_output ? (output ? "output" : "input") : "same",
281 slpm_val ? (val ? "high" : "low") : "same");
282 }
283
Rabin Vincent6720db72010-09-02 11:28:48 +0100284 if (output)
285 __nmk_gpio_make_output(nmk_chip, offset, val);
286 else {
287 __nmk_gpio_make_input(nmk_chip, offset);
288 __nmk_gpio_set_pull(nmk_chip, offset, pull);
289 }
290
Rabin Vincentebc61782011-09-28 15:49:11 +0530291 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
292
Rabin Vincent01727e62010-12-13 12:02:40 +0530293 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530294 * If the pin is switching to altfunc, and there was an interrupt
295 * installed on it which has been lazy disabled, actually mask the
296 * interrupt to prevent spurious interrupts that would occur while the
297 * pin is under control of the peripheral. Only SKE does this.
298 */
299 if (af != NMK_GPIO_ALT_GPIO)
300 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
301
302 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530303 * If we've backed up the SLPM registers (glitch workaround), modify
304 * the backups since they will be restored.
305 */
306 if (slpmregs) {
307 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
308 slpmregs[nmk_chip->bank] |= BIT(offset);
309 else
310 slpmregs[nmk_chip->bank] &= ~BIT(offset);
311 } else
312 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
313
314 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
315}
316
317/*
318 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
319 * - Save SLPM registers
320 * - Set SLPM=0 for the IOs you want to switch and others to 1
321 * - Configure the GPIO registers for the IOs that are being switched
322 * - Set IOFORCE=1
323 * - Modify the AFLSA/B registers for the IOs that are being switched
324 * - Set IOFORCE=0
325 * - Restore SLPM registers
326 * - Any spurious wake up event during switch sequence to be ignored and
327 * cleared
328 */
329static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
330{
331 int i;
332
333 for (i = 0; i < NUM_BANKS; i++) {
334 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
335 unsigned int temp = slpm[i];
336
337 if (!chip)
338 break;
339
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200340 clk_enable(chip->clk);
341
Rabin Vincent01727e62010-12-13 12:02:40 +0530342 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
343 writel(temp, chip->addr + NMK_GPIO_SLPC);
344 }
345}
346
347static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
348{
349 int i;
350
351 for (i = 0; i < NUM_BANKS; i++) {
352 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
353
354 if (!chip)
355 break;
356
357 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200358
359 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530360 }
361}
362
363static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
364{
365 static unsigned int slpm[NUM_BANKS];
366 unsigned long flags;
367 bool glitch = false;
368 int ret = 0;
369 int i;
370
371 for (i = 0; i < num; i++) {
372 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
373 glitch = true;
374 break;
375 }
376 }
377
378 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
379
380 if (glitch) {
381 memset(slpm, 0xff, sizeof(slpm));
382
383 for (i = 0; i < num; i++) {
384 int pin = PIN_NUM(cfgs[i]);
385 int offset = pin % NMK_GPIO_PER_CHIP;
386
387 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
388 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
389 }
390
391 nmk_gpio_glitch_slpm_init(slpm);
392 }
393
394 for (i = 0; i < num; i++) {
395 struct nmk_gpio_chip *nmk_chip;
396 int pin = PIN_NUM(cfgs[i]);
397
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100398 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530399 if (!nmk_chip) {
400 ret = -EINVAL;
401 break;
402 }
403
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200404 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530405 spin_lock(&nmk_chip->lock);
406 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
407 cfgs[i], sleep, glitch ? slpm : NULL);
408 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200409 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530410 }
411
412 if (glitch)
413 nmk_gpio_glitch_slpm_restore(slpm);
414
415 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
416
417 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100418}
419
420/**
421 * nmk_config_pin - configure a pin's mux attributes
422 * @cfg: pin confguration
423 *
424 * Configures a pin's mode (alternate function or GPIO), its pull up status,
425 * and its sleep mode based on the specified configuration. The @cfg is
426 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
427 * are constructed using, and can be further enhanced with, the macros in
428 * plat/pincfg.h.
429 *
430 * If a pin's mode is set to GPIO, it is configured as an input to avoid
431 * side-effects. The gpio can be manipulated later using standard GPIO API
432 * calls.
433 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530434int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100435{
Rabin Vincent01727e62010-12-13 12:02:40 +0530436 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100437}
438EXPORT_SYMBOL(nmk_config_pin);
439
440/**
441 * nmk_config_pins - configure several pins at once
442 * @cfgs: array of pin configurations
443 * @num: number of elments in the array
444 *
445 * Configures several pins using nmk_config_pin(). Refer to that function for
446 * further information.
447 */
448int nmk_config_pins(pin_cfg_t *cfgs, int num)
449{
Rabin Vincent01727e62010-12-13 12:02:40 +0530450 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100451}
452EXPORT_SYMBOL(nmk_config_pins);
453
Rabin Vincentdacdc962010-12-03 20:35:37 +0530454int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
455{
Rabin Vincent01727e62010-12-13 12:02:40 +0530456 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530457}
458EXPORT_SYMBOL(nmk_config_pins_sleep);
459
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100460/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100461 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
462 * @gpio: pin number
463 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
464 *
Linus Walleij33d78642011-06-09 11:08:47 +0200465 * This register is actually in the pinmux layer, not the GPIO block itself.
466 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
467 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
468 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
469 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
470 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
471 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100472 *
Linus Walleij33d78642011-06-09 11:08:47 +0200473 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
474 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
475 * entered) regardless of the altfunction selected. Also wake-up detection is
476 * ENABLED.
477 *
478 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
479 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
480 * (for altfunction GPIO) or respective on-chip peripherals (for other
481 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
482 *
483 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100484 */
485int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
486{
487 struct nmk_gpio_chip *nmk_chip;
488 unsigned long flags;
489
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100490 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100491 if (!nmk_chip)
492 return -EINVAL;
493
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200494 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530495 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
496 spin_lock(&nmk_chip->lock);
497
Rabin Vincent81a3c292010-05-27 12:39:23 +0100498 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530499
500 spin_unlock(&nmk_chip->lock);
501 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200502 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100503
504 return 0;
505}
506
507/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100508 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
509 * @gpio: pin number
510 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
511 *
512 * Enables/disables pull up/down on a specified pin. This only takes effect if
513 * the pin is configured as an input (either explicitly or by the alternate
514 * function).
515 *
516 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
517 * configured as an input. Otherwise, due to the way the controller registers
518 * work, this function will change the value output on the pin.
519 */
520int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
521{
522 struct nmk_gpio_chip *nmk_chip;
523 unsigned long flags;
524
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100525 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100526 if (!nmk_chip)
527 return -EINVAL;
528
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200529 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100530 spin_lock_irqsave(&nmk_chip->lock, flags);
531 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
532 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200533 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100534
535 return 0;
536}
537
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100538/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200539/**
540 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
541 * @gpio: pin number
542 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
543 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
544 *
545 * Sets the mode of the specified pin to one of the alternate functions or
546 * plain GPIO.
547 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100548int nmk_gpio_set_mode(int gpio, int gpio_mode)
549{
550 struct nmk_gpio_chip *nmk_chip;
551 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100552
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100553 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100554 if (!nmk_chip)
555 return -EINVAL;
556
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200557 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100558 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100559 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100560 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200561 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100562
563 return 0;
564}
565EXPORT_SYMBOL(nmk_gpio_set_mode);
566
567int nmk_gpio_get_mode(int gpio)
568{
569 struct nmk_gpio_chip *nmk_chip;
570 u32 afunc, bfunc, bit;
571
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100572 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100573 if (!nmk_chip)
574 return -EINVAL;
575
576 bit = 1 << (gpio - nmk_chip->chip.base);
577
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200578 clk_enable(nmk_chip->clk);
579
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100580 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
581 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
582
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200583 clk_disable(nmk_chip->clk);
584
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100585 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
586}
587EXPORT_SYMBOL(nmk_gpio_get_mode);
588
589
590/* IRQ functions */
591static inline int nmk_gpio_get_bitmask(int gpio)
592{
593 return 1 << (gpio % 32);
594}
595
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100596static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100597{
598 int gpio;
599 struct nmk_gpio_chip *nmk_chip;
600
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100601 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
602 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100603 if (!nmk_chip)
604 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200605
606 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200608 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100609}
610
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100611enum nmk_gpio_irq_type {
612 NORMAL,
613 WAKE,
614};
615
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100616static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100617 int gpio, enum nmk_gpio_irq_type which,
618 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100619{
620 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530621 u32 *rimscval;
622 u32 *fimscval;
623 u32 rimscreg;
624 u32 fimscreg;
625
626 if (which == NORMAL) {
627 rimscreg = NMK_GPIO_RIMSC;
628 fimscreg = NMK_GPIO_FIMSC;
629 rimscval = &nmk_chip->rimsc;
630 fimscval = &nmk_chip->fimsc;
631 } else {
632 rimscreg = NMK_GPIO_RWIMSC;
633 fimscreg = NMK_GPIO_FWIMSC;
634 rimscval = &nmk_chip->rwimsc;
635 fimscval = &nmk_chip->fwimsc;
636 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100637
638 /* we must individually set/clear the two edges */
639 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100640 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530641 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100642 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530643 *rimscval &= ~bitmask;
644 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100645 }
646 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100647 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530648 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100649 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530650 *fimscval &= ~bitmask;
651 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100652 }
653}
654
Rabin Vincentb9df4682011-02-10 11:45:58 +0530655static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
656 int gpio, bool on)
657{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530658 /*
659 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
660 * disabled, since setting SLPM to 1 increases power consumption, and
661 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
662 */
663 if (nmk_chip->sleepmode && on) {
Linus Walleij33d78642011-06-09 11:08:47 +0200664 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530665 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200666 }
667
Rabin Vincentb9df4682011-02-10 11:45:58 +0530668 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
669}
670
671static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100672{
673 int gpio;
674 struct nmk_gpio_chip *nmk_chip;
675 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100676 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100677
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100678 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
679 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680 bitmask = nmk_gpio_get_bitmask(gpio);
681 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100682 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200684 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530685 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
686 spin_lock(&nmk_chip->lock);
687
688 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
689
690 if (!(nmk_chip->real_wake & bitmask))
691 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
692
693 spin_unlock(&nmk_chip->lock);
694 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200695 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100696
697 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100698}
699
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100700static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100701{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530702 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100703}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100704
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100705static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100706{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530707 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100708}
709
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100710static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100711{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100712 struct nmk_gpio_chip *nmk_chip;
713 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530714 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100715 int gpio;
716
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100717 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
718 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100719 if (!nmk_chip)
720 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530721 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100722
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200723 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530724 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
725 spin_lock(&nmk_chip->lock);
726
Linus Walleij479a0c72011-09-20 10:50:15 +0200727 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530728 __nmk_gpio_set_wake(nmk_chip, gpio, on);
729
730 if (on)
731 nmk_chip->real_wake |= bitmask;
732 else
733 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530734
735 spin_unlock(&nmk_chip->lock);
736 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200737 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100738
739 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100740}
741
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100742static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743{
Linus Walleij479a0c72011-09-20 10:50:15 +0200744 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200745 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100746 int gpio;
747 struct nmk_gpio_chip *nmk_chip;
748 unsigned long flags;
749 u32 bitmask;
750
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100751 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
752 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100753 bitmask = nmk_gpio_get_bitmask(gpio);
754 if (!nmk_chip)
755 return -EINVAL;
756
757 if (type & IRQ_TYPE_LEVEL_HIGH)
758 return -EINVAL;
759 if (type & IRQ_TYPE_LEVEL_LOW)
760 return -EINVAL;
761
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200762 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100763 spin_lock_irqsave(&nmk_chip->lock, flags);
764
Rabin Vincent7a852d82010-05-06 10:43:55 +0100765 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100766 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
767
Rabin Vincentb9df4682011-02-10 11:45:58 +0530768 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100769 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100770
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100771 nmk_chip->edge_rising &= ~bitmask;
772 if (type & IRQ_TYPE_EDGE_RISING)
773 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100774
775 nmk_chip->edge_falling &= ~bitmask;
776 if (type & IRQ_TYPE_EDGE_FALLING)
777 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100778
779 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100780 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
781
Rabin Vincentb9df4682011-02-10 11:45:58 +0530782 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100783 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784
785 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200786 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100787
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788 return 0;
789}
790
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200791static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
792{
793 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
794
795 clk_enable(nmk_chip->clk);
796 nmk_gpio_irq_unmask(d);
797 return 0;
798}
799
800static void nmk_gpio_irq_shutdown(struct irq_data *d)
801{
802 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
803
804 nmk_gpio_irq_mask(d);
805 clk_disable(nmk_chip->clk);
806}
807
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100808static struct irq_chip nmk_gpio_irq_chip = {
809 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100810 .irq_ack = nmk_gpio_irq_ack,
811 .irq_mask = nmk_gpio_irq_mask,
812 .irq_unmask = nmk_gpio_irq_unmask,
813 .irq_set_type = nmk_gpio_irq_set_type,
814 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200815 .irq_startup = nmk_gpio_irq_startup,
816 .irq_shutdown = nmk_gpio_irq_shutdown,
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 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100821{
822 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100823 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100824 unsigned int first_irq;
825
Will Deaconadfed152011-02-28 10:12:29 +0000826 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100827
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100828 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100829 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530830 while (status) {
831 int bit = __ffs(status);
832
833 generic_handle_irq(first_irq + bit);
834 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100836
Will Deaconadfed152011-02-28 10:12:29 +0000837 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838}
839
Rabin Vincent33b744b2010-10-14 10:38:03 +0530840static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
841{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100842 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200843 u32 status;
844
845 clk_enable(nmk_chip->clk);
846 status = readl(nmk_chip->addr + NMK_GPIO_IS);
847 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530848
849 __nmk_gpio_irq_handler(irq, desc, status);
850}
851
852static void nmk_gpio_secondary_irq_handler(unsigned int irq,
853 struct irq_desc *desc)
854{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100855 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530856 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
857
858 __nmk_gpio_irq_handler(irq, desc, status);
859}
860
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100861static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
862{
863 unsigned int first_irq;
864 int i;
865
866 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530867 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100868 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
869 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100870 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100871 irq_set_chip_data(i, nmk_chip);
872 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530874
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100875 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
876 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530877
878 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100879 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530880 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100881 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530882 }
883
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100884 return 0;
885}
886
887/* I/O Functions */
888static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
889{
890 struct nmk_gpio_chip *nmk_chip =
891 container_of(chip, struct nmk_gpio_chip, chip);
892
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200893 clk_enable(nmk_chip->clk);
894
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100895 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200896
897 clk_disable(nmk_chip->clk);
898
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100899 return 0;
900}
901
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100902static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
903{
904 struct nmk_gpio_chip *nmk_chip =
905 container_of(chip, struct nmk_gpio_chip, chip);
906 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200907 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100908
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200909 clk_enable(nmk_chip->clk);
910
911 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
912
913 clk_disable(nmk_chip->clk);
914
915 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100916}
917
918static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
919 int val)
920{
921 struct nmk_gpio_chip *nmk_chip =
922 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100923
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200924 clk_enable(nmk_chip->clk);
925
Rabin Vincent6720db72010-09-02 11:28:48 +0100926 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200927
928 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100929}
930
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100931static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
932 int val)
933{
934 struct nmk_gpio_chip *nmk_chip =
935 container_of(chip, struct nmk_gpio_chip, chip);
936
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200937 clk_enable(nmk_chip->clk);
938
Rabin Vincent6720db72010-09-02 11:28:48 +0100939 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100940
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200941 clk_disable(nmk_chip->clk);
942
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100943 return 0;
944}
945
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100946static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
947{
948 struct nmk_gpio_chip *nmk_chip =
949 container_of(chip, struct nmk_gpio_chip, chip);
950
951 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
952}
953
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530954#ifdef CONFIG_DEBUG_FS
955
956#include <linux/seq_file.h>
957
958static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
959{
960 int mode;
961 unsigned i;
962 unsigned gpio = chip->base;
963 int is_out;
964 struct nmk_gpio_chip *nmk_chip =
965 container_of(chip, struct nmk_gpio_chip, chip);
966 const char *modes[] = {
967 [NMK_GPIO_ALT_GPIO] = "gpio",
968 [NMK_GPIO_ALT_A] = "altA",
969 [NMK_GPIO_ALT_B] = "altB",
970 [NMK_GPIO_ALT_C] = "altC",
971 };
972
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200973 clk_enable(nmk_chip->clk);
974
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530975 for (i = 0; i < chip->ngpio; i++, gpio++) {
976 const char *label = gpiochip_is_requested(chip, i);
977 bool pull;
978 u32 bit = 1 << i;
979
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530980 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
981 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
982 mode = nmk_gpio_get_mode(gpio);
983 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200984 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530985 is_out ? "out" : "in ",
986 chip->get
987 ? (chip->get(chip, i) ? "hi" : "lo")
988 : "? ",
989 (mode < 0) ? "unknown" : modes[mode],
990 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200991
992 if (label && !is_out) {
993 int irq = gpio_to_irq(gpio);
994 struct irq_desc *desc = irq_to_desc(irq);
995
996 /* This races with request_irq(), set_irq_type(),
997 * and set_irq_wake() ... but those are "rare".
998 */
999 if (irq >= 0 && desc->action) {
1000 char *trigger;
1001 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1002
1003 if (nmk_chip->edge_rising & bitmask)
1004 trigger = "edge-rising";
1005 else if (nmk_chip->edge_falling & bitmask)
1006 trigger = "edge-falling";
1007 else
1008 trigger = "edge-undefined";
1009
1010 seq_printf(s, " irq-%d %s%s",
1011 irq, trigger,
1012 irqd_is_wakeup_set(&desc->irq_data)
1013 ? " wakeup" : "");
1014 }
1015 }
1016
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301017 seq_printf(s, "\n");
1018 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001019
1020 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301021}
1022
1023#else
1024#define nmk_gpio_dbg_show NULL
1025#endif
1026
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001027/* This structure is replicated for each GPIO block allocated at probe time */
1028static struct gpio_chip nmk_gpio_template = {
1029 .direction_input = nmk_gpio_make_input,
1030 .get = nmk_gpio_get_input,
1031 .direction_output = nmk_gpio_make_output,
1032 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001033 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301034 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001035 .can_sleep = 0,
1036};
1037
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001038void nmk_gpio_clocks_enable(void)
1039{
1040 int i;
1041
1042 for (i = 0; i < NUM_BANKS; i++) {
1043 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1044
1045 if (!chip)
1046 continue;
1047
1048 clk_enable(chip->clk);
1049 }
1050}
1051
1052void nmk_gpio_clocks_disable(void)
1053{
1054 int i;
1055
1056 for (i = 0; i < NUM_BANKS; i++) {
1057 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1058
1059 if (!chip)
1060 continue;
1061
1062 clk_disable(chip->clk);
1063 }
1064}
1065
Rabin Vincentb9df4682011-02-10 11:45:58 +05301066/*
1067 * Called from the suspend/resume path to only keep the real wakeup interrupts
1068 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1069 * and not the rest of the interrupts which we needed to have as wakeups for
1070 * cpuidle.
1071 *
1072 * PM ops are not used since this needs to be done at the end, after all the
1073 * other drivers are done with their suspend callbacks.
1074 */
1075void nmk_gpio_wakeups_suspend(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->real_wake,
1088 chip->addr + NMK_GPIO_RWIMSC);
1089 writel(chip->fwimsc & chip->real_wake,
1090 chip->addr + NMK_GPIO_FWIMSC);
1091
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001092 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301093 }
1094}
1095
1096void nmk_gpio_wakeups_resume(void)
1097{
1098 int i;
1099
1100 for (i = 0; i < NUM_BANKS; i++) {
1101 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1102
1103 if (!chip)
1104 break;
1105
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001106 clk_enable(chip->clk);
1107
Rabin Vincentb9df4682011-02-10 11:45:58 +05301108 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1109 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1110
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001111 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301112 }
1113}
1114
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001115/*
1116 * Read the pull up/pull down status.
1117 * A bit set in 'pull_up' means that pull up
1118 * is selected if pull is enabled in PDIS register.
1119 * Note: only pull up/down set via this driver can
1120 * be detected due to HW limitations.
1121 */
1122void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1123{
1124 if (gpio_bank < NUM_BANKS) {
1125 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1126
1127 if (!chip)
1128 return;
1129
1130 *pull_up = chip->pull_up;
1131 }
1132}
1133
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001134static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001135{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001136 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001137 struct nmk_gpio_chip *nmk_chip;
1138 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001139 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001140 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301141 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001142 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001143 int ret;
1144
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001145 if (!pdata)
1146 return -ENODEV;
1147
1148 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1149 if (!res) {
1150 ret = -ENOENT;
1151 goto out;
1152 }
1153
1154 irq = platform_get_irq(dev, 0);
1155 if (irq < 0) {
1156 ret = irq;
1157 goto out;
1158 }
1159
Rabin Vincent33b744b2010-10-14 10:38:03 +05301160 secondary_irq = platform_get_irq(dev, 1);
1161 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1162 ret = -EINVAL;
1163 goto out;
1164 }
1165
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001166 if (request_mem_region(res->start, resource_size(res),
1167 dev_name(&dev->dev)) == NULL) {
1168 ret = -EBUSY;
1169 goto out;
1170 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001171
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001172 clk = clk_get(&dev->dev, NULL);
1173 if (IS_ERR(clk)) {
1174 ret = PTR_ERR(clk);
1175 goto out_release;
1176 }
1177
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001178 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1179 if (!nmk_chip) {
1180 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001181 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001182 }
1183 /*
1184 * The virt address in nmk_chip->addr is in the nomadik register space,
1185 * so we can simply convert the resource address, without remapping
1186 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301187 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001188 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001189 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001190 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001191 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301192 nmk_chip->secondary_parent_irq = secondary_irq;
1193 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301194 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001195 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001196 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001197
1198 chip = &nmk_chip->chip;
1199 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301200 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301201 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001202 chip->dev = &dev->dev;
1203 chip->owner = THIS_MODULE;
1204
Rabin Vincentebc61782011-09-28 15:49:11 +05301205 clk_enable(nmk_chip->clk);
1206 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1207 clk_disable(nmk_chip->clk);
1208
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001209 ret = gpiochip_add(&nmk_chip->chip);
1210 if (ret)
1211 goto out_free;
1212
Rabin Vincent01727e62010-12-13 12:02:40 +05301213 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1214
1215 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001216 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001217
1218 nmk_gpio_init_irq(nmk_chip);
1219
Grant Likely64842aa2011-11-06 11:36:18 -07001220 dev_info(&dev->dev, "at address %p\n",
1221 nmk_chip->addr);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001222 return 0;
1223
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001224out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001225 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001226out_clk:
1227 clk_disable(clk);
1228 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001229out_release:
1230 release_mem_region(res->start, resource_size(res));
1231out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001232 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1233 pdata->first_gpio, pdata->first_gpio+31);
1234 return ret;
1235}
1236
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001237static struct platform_driver nmk_gpio_driver = {
1238 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001239 .owner = THIS_MODULE,
1240 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301241 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001242 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001243};
1244
1245static int __init nmk_gpio_init(void)
1246{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001247 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001248}
1249
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001250core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001251
1252MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1253MODULE_DESCRIPTION("Nomadik GPIO Driver");
1254MODULE_LICENSE("GPL");