blob: 45a0e0a3d6e9471a318629c84a6cfc9c96090d59 [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 Vincent378be062010-06-02 06:06:29 +0100177static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530178 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100179{
180 static const char *afnames[] = {
181 [NMK_GPIO_ALT_GPIO] = "GPIO",
182 [NMK_GPIO_ALT_A] = "A",
183 [NMK_GPIO_ALT_B] = "B",
184 [NMK_GPIO_ALT_C] = "C"
185 };
186 static const char *pullnames[] = {
187 [NMK_GPIO_PULL_NONE] = "none",
188 [NMK_GPIO_PULL_UP] = "up",
189 [NMK_GPIO_PULL_DOWN] = "down",
190 [3] /* illegal */ = "??"
191 };
192 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100193 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
194 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100195 };
196
197 int pin = PIN_NUM(cfg);
198 int pull = PIN_PULL(cfg);
199 int af = PIN_ALT(cfg);
200 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100201 int output = PIN_DIR(cfg);
202 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530203 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100204
Rabin Vincentdacdc962010-12-03 20:35:37 +0530205 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
206 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100207 output ? "output " : "input",
208 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100209
Rabin Vincentdacdc962010-12-03 20:35:37 +0530210 if (sleep) {
211 int slpm_pull = PIN_SLPM_PULL(cfg);
212 int slpm_output = PIN_SLPM_DIR(cfg);
213 int slpm_val = PIN_SLPM_VAL(cfg);
214
Rabin Vincent3546d152010-11-25 11:38:27 +0530215 af = NMK_GPIO_ALT_GPIO;
216
Rabin Vincentdacdc962010-12-03 20:35:37 +0530217 /*
218 * The SLPM_* values are normal values + 1 to allow zero to
219 * mean "same as normal".
220 */
221 if (slpm_pull)
222 pull = slpm_pull - 1;
223 if (slpm_output)
224 output = slpm_output - 1;
225 if (slpm_val)
226 val = slpm_val - 1;
227
228 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
229 pin,
230 slpm_pull ? pullnames[pull] : "same",
231 slpm_output ? (output ? "output" : "input") : "same",
232 slpm_val ? (val ? "high" : "low") : "same");
233 }
234
Rabin Vincent6720db72010-09-02 11:28:48 +0100235 if (output)
236 __nmk_gpio_make_output(nmk_chip, offset, val);
237 else {
238 __nmk_gpio_make_input(nmk_chip, offset);
239 __nmk_gpio_set_pull(nmk_chip, offset, pull);
240 }
241
Rabin Vincent01727e62010-12-13 12:02:40 +0530242 /*
243 * If we've backed up the SLPM registers (glitch workaround), modify
244 * the backups since they will be restored.
245 */
246 if (slpmregs) {
247 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
248 slpmregs[nmk_chip->bank] |= BIT(offset);
249 else
250 slpmregs[nmk_chip->bank] &= ~BIT(offset);
251 } else
252 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
253
254 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
255}
256
257/*
258 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
259 * - Save SLPM registers
260 * - Set SLPM=0 for the IOs you want to switch and others to 1
261 * - Configure the GPIO registers for the IOs that are being switched
262 * - Set IOFORCE=1
263 * - Modify the AFLSA/B registers for the IOs that are being switched
264 * - Set IOFORCE=0
265 * - Restore SLPM registers
266 * - Any spurious wake up event during switch sequence to be ignored and
267 * cleared
268 */
269static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
270{
271 int i;
272
273 for (i = 0; i < NUM_BANKS; i++) {
274 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
275 unsigned int temp = slpm[i];
276
277 if (!chip)
278 break;
279
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200280 clk_enable(chip->clk);
281
Rabin Vincent01727e62010-12-13 12:02:40 +0530282 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
283 writel(temp, chip->addr + NMK_GPIO_SLPC);
284 }
285}
286
287static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
288{
289 int i;
290
291 for (i = 0; i < NUM_BANKS; i++) {
292 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
293
294 if (!chip)
295 break;
296
297 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200298
299 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530300 }
301}
302
303static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
304{
305 static unsigned int slpm[NUM_BANKS];
306 unsigned long flags;
307 bool glitch = false;
308 int ret = 0;
309 int i;
310
311 for (i = 0; i < num; i++) {
312 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
313 glitch = true;
314 break;
315 }
316 }
317
318 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
319
320 if (glitch) {
321 memset(slpm, 0xff, sizeof(slpm));
322
323 for (i = 0; i < num; i++) {
324 int pin = PIN_NUM(cfgs[i]);
325 int offset = pin % NMK_GPIO_PER_CHIP;
326
327 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
328 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
329 }
330
331 nmk_gpio_glitch_slpm_init(slpm);
332 }
333
334 for (i = 0; i < num; i++) {
335 struct nmk_gpio_chip *nmk_chip;
336 int pin = PIN_NUM(cfgs[i]);
337
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100338 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530339 if (!nmk_chip) {
340 ret = -EINVAL;
341 break;
342 }
343
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200344 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530345 spin_lock(&nmk_chip->lock);
346 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
347 cfgs[i], sleep, glitch ? slpm : NULL);
348 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200349 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530350 }
351
352 if (glitch)
353 nmk_gpio_glitch_slpm_restore(slpm);
354
355 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
356
357 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100358}
359
360/**
361 * nmk_config_pin - configure a pin's mux attributes
362 * @cfg: pin confguration
363 *
364 * Configures a pin's mode (alternate function or GPIO), its pull up status,
365 * and its sleep mode based on the specified configuration. The @cfg is
366 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
367 * are constructed using, and can be further enhanced with, the macros in
368 * plat/pincfg.h.
369 *
370 * If a pin's mode is set to GPIO, it is configured as an input to avoid
371 * side-effects. The gpio can be manipulated later using standard GPIO API
372 * calls.
373 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530374int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100375{
Rabin Vincent01727e62010-12-13 12:02:40 +0530376 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100377}
378EXPORT_SYMBOL(nmk_config_pin);
379
380/**
381 * nmk_config_pins - configure several pins at once
382 * @cfgs: array of pin configurations
383 * @num: number of elments in the array
384 *
385 * Configures several pins using nmk_config_pin(). Refer to that function for
386 * further information.
387 */
388int nmk_config_pins(pin_cfg_t *cfgs, int num)
389{
Rabin Vincent01727e62010-12-13 12:02:40 +0530390 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100391}
392EXPORT_SYMBOL(nmk_config_pins);
393
Rabin Vincentdacdc962010-12-03 20:35:37 +0530394int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
395{
Rabin Vincent01727e62010-12-13 12:02:40 +0530396 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530397}
398EXPORT_SYMBOL(nmk_config_pins_sleep);
399
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100400/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100401 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
402 * @gpio: pin number
403 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
404 *
Linus Walleij33d78642011-06-09 11:08:47 +0200405 * This register is actually in the pinmux layer, not the GPIO block itself.
406 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
407 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
408 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
409 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
410 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
411 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100412 *
Linus Walleij33d78642011-06-09 11:08:47 +0200413 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
414 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
415 * entered) regardless of the altfunction selected. Also wake-up detection is
416 * ENABLED.
417 *
418 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
419 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
420 * (for altfunction GPIO) or respective on-chip peripherals (for other
421 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
422 *
423 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100424 */
425int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
426{
427 struct nmk_gpio_chip *nmk_chip;
428 unsigned long flags;
429
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100430 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100431 if (!nmk_chip)
432 return -EINVAL;
433
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200434 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530435 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
436 spin_lock(&nmk_chip->lock);
437
Rabin Vincent81a3c292010-05-27 12:39:23 +0100438 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530439
440 spin_unlock(&nmk_chip->lock);
441 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200442 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100443
444 return 0;
445}
446
447/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100448 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
449 * @gpio: pin number
450 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
451 *
452 * Enables/disables pull up/down on a specified pin. This only takes effect if
453 * the pin is configured as an input (either explicitly or by the alternate
454 * function).
455 *
456 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
457 * configured as an input. Otherwise, due to the way the controller registers
458 * work, this function will change the value output on the pin.
459 */
460int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
461{
462 struct nmk_gpio_chip *nmk_chip;
463 unsigned long flags;
464
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100465 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100466 if (!nmk_chip)
467 return -EINVAL;
468
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200469 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100470 spin_lock_irqsave(&nmk_chip->lock, flags);
471 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
472 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200473 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100474
475 return 0;
476}
477
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100478/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200479/**
480 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
481 * @gpio: pin number
482 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
483 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
484 *
485 * Sets the mode of the specified pin to one of the alternate functions or
486 * plain GPIO.
487 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100488int nmk_gpio_set_mode(int gpio, int gpio_mode)
489{
490 struct nmk_gpio_chip *nmk_chip;
491 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100492
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100493 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100494 if (!nmk_chip)
495 return -EINVAL;
496
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200497 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100498 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100499 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100500 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200501 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100502
503 return 0;
504}
505EXPORT_SYMBOL(nmk_gpio_set_mode);
506
507int nmk_gpio_get_mode(int gpio)
508{
509 struct nmk_gpio_chip *nmk_chip;
510 u32 afunc, bfunc, bit;
511
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100512 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100513 if (!nmk_chip)
514 return -EINVAL;
515
516 bit = 1 << (gpio - nmk_chip->chip.base);
517
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200518 clk_enable(nmk_chip->clk);
519
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100520 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
521 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
522
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200523 clk_disable(nmk_chip->clk);
524
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100525 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
526}
527EXPORT_SYMBOL(nmk_gpio_get_mode);
528
529
530/* IRQ functions */
531static inline int nmk_gpio_get_bitmask(int gpio)
532{
533 return 1 << (gpio % 32);
534}
535
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100536static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100537{
538 int gpio;
539 struct nmk_gpio_chip *nmk_chip;
540
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100541 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
542 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100543 if (!nmk_chip)
544 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200545
546 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200548 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100549}
550
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100551enum nmk_gpio_irq_type {
552 NORMAL,
553 WAKE,
554};
555
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100556static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100557 int gpio, enum nmk_gpio_irq_type which,
558 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100559{
560 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530561 u32 *rimscval;
562 u32 *fimscval;
563 u32 rimscreg;
564 u32 fimscreg;
565
566 if (which == NORMAL) {
567 rimscreg = NMK_GPIO_RIMSC;
568 fimscreg = NMK_GPIO_FIMSC;
569 rimscval = &nmk_chip->rimsc;
570 fimscval = &nmk_chip->fimsc;
571 } else {
572 rimscreg = NMK_GPIO_RWIMSC;
573 fimscreg = NMK_GPIO_FWIMSC;
574 rimscval = &nmk_chip->rwimsc;
575 fimscval = &nmk_chip->fwimsc;
576 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100577
578 /* we must individually set/clear the two edges */
579 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100580 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530581 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100582 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530583 *rimscval &= ~bitmask;
584 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100585 }
586 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100587 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530588 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100589 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530590 *fimscval &= ~bitmask;
591 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100592 }
593}
594
Rabin Vincentb9df4682011-02-10 11:45:58 +0530595static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
596 int gpio, bool on)
597{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530598 /*
599 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
600 * disabled, since setting SLPM to 1 increases power consumption, and
601 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
602 */
603 if (nmk_chip->sleepmode && on) {
Linus Walleij33d78642011-06-09 11:08:47 +0200604 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530605 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200606 }
607
Rabin Vincentb9df4682011-02-10 11:45:58 +0530608 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
609}
610
611static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100612{
613 int gpio;
614 struct nmk_gpio_chip *nmk_chip;
615 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100616 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100617
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100618 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
619 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100620 bitmask = nmk_gpio_get_bitmask(gpio);
621 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100622 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100623
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200624 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530625 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
626 spin_lock(&nmk_chip->lock);
627
628 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
629
630 if (!(nmk_chip->real_wake & bitmask))
631 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
632
633 spin_unlock(&nmk_chip->lock);
634 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200635 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100636
637 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100638}
639
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100640static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100641{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530642 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100643}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100644
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100645static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100646{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530647 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100648}
649
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100650static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100651{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100652 struct nmk_gpio_chip *nmk_chip;
653 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530654 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100655 int gpio;
656
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);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100659 if (!nmk_chip)
660 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530661 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100662
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200663 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530664 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
665 spin_lock(&nmk_chip->lock);
666
Linus Walleij479a0c72011-09-20 10:50:15 +0200667 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530668 __nmk_gpio_set_wake(nmk_chip, gpio, on);
669
670 if (on)
671 nmk_chip->real_wake |= bitmask;
672 else
673 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530674
675 spin_unlock(&nmk_chip->lock);
676 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200677 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100678
679 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680}
681
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100682static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683{
Linus Walleij479a0c72011-09-20 10:50:15 +0200684 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200685 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100686 int gpio;
687 struct nmk_gpio_chip *nmk_chip;
688 unsigned long flags;
689 u32 bitmask;
690
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100691 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
692 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100693 bitmask = nmk_gpio_get_bitmask(gpio);
694 if (!nmk_chip)
695 return -EINVAL;
696
697 if (type & IRQ_TYPE_LEVEL_HIGH)
698 return -EINVAL;
699 if (type & IRQ_TYPE_LEVEL_LOW)
700 return -EINVAL;
701
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200702 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100703 spin_lock_irqsave(&nmk_chip->lock, flags);
704
Rabin Vincent7a852d82010-05-06 10:43:55 +0100705 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100706 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
707
Rabin Vincentb9df4682011-02-10 11:45:58 +0530708 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100709 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100710
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711 nmk_chip->edge_rising &= ~bitmask;
712 if (type & IRQ_TYPE_EDGE_RISING)
713 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100714
715 nmk_chip->edge_falling &= ~bitmask;
716 if (type & IRQ_TYPE_EDGE_FALLING)
717 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100718
719 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100720 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
721
Rabin Vincentb9df4682011-02-10 11:45:58 +0530722 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100723 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100724
725 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200726 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100727
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100728 return 0;
729}
730
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200731static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
732{
733 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
734
735 clk_enable(nmk_chip->clk);
736 nmk_gpio_irq_unmask(d);
737 return 0;
738}
739
740static void nmk_gpio_irq_shutdown(struct irq_data *d)
741{
742 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
743
744 nmk_gpio_irq_mask(d);
745 clk_disable(nmk_chip->clk);
746}
747
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100748static struct irq_chip nmk_gpio_irq_chip = {
749 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100750 .irq_ack = nmk_gpio_irq_ack,
751 .irq_mask = nmk_gpio_irq_mask,
752 .irq_unmask = nmk_gpio_irq_unmask,
753 .irq_set_type = nmk_gpio_irq_set_type,
754 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200755 .irq_startup = nmk_gpio_irq_startup,
756 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100757};
758
Rabin Vincent33b744b2010-10-14 10:38:03 +0530759static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
760 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100761{
762 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100763 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100764 unsigned int first_irq;
765
Will Deaconadfed152011-02-28 10:12:29 +0000766 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100767
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100768 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100769 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530770 while (status) {
771 int bit = __ffs(status);
772
773 generic_handle_irq(first_irq + bit);
774 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100775 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100776
Will Deaconadfed152011-02-28 10:12:29 +0000777 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100778}
779
Rabin Vincent33b744b2010-10-14 10:38:03 +0530780static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
781{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100782 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200783 u32 status;
784
785 clk_enable(nmk_chip->clk);
786 status = readl(nmk_chip->addr + NMK_GPIO_IS);
787 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530788
789 __nmk_gpio_irq_handler(irq, desc, status);
790}
791
792static void nmk_gpio_secondary_irq_handler(unsigned int irq,
793 struct irq_desc *desc)
794{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100795 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530796 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
797
798 __nmk_gpio_irq_handler(irq, desc, status);
799}
800
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100801static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
802{
803 unsigned int first_irq;
804 int i;
805
806 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530807 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100808 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
809 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100810 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100811 irq_set_chip_data(i, nmk_chip);
812 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100813 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530814
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100815 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
816 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530817
818 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100819 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530820 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100821 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530822 }
823
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100824 return 0;
825}
826
827/* I/O Functions */
828static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
829{
830 struct nmk_gpio_chip *nmk_chip =
831 container_of(chip, struct nmk_gpio_chip, chip);
832
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200833 clk_enable(nmk_chip->clk);
834
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200836
837 clk_disable(nmk_chip->clk);
838
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100839 return 0;
840}
841
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100842static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
843{
844 struct nmk_gpio_chip *nmk_chip =
845 container_of(chip, struct nmk_gpio_chip, chip);
846 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200847 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100848
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200849 clk_enable(nmk_chip->clk);
850
851 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
852
853 clk_disable(nmk_chip->clk);
854
855 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100856}
857
858static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
859 int val)
860{
861 struct nmk_gpio_chip *nmk_chip =
862 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100863
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200864 clk_enable(nmk_chip->clk);
865
Rabin Vincent6720db72010-09-02 11:28:48 +0100866 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200867
868 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100869}
870
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100871static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
872 int val)
873{
874 struct nmk_gpio_chip *nmk_chip =
875 container_of(chip, struct nmk_gpio_chip, chip);
876
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200877 clk_enable(nmk_chip->clk);
878
Rabin Vincent6720db72010-09-02 11:28:48 +0100879 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100880
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200881 clk_disable(nmk_chip->clk);
882
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100883 return 0;
884}
885
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100886static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
887{
888 struct nmk_gpio_chip *nmk_chip =
889 container_of(chip, struct nmk_gpio_chip, chip);
890
891 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
892}
893
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530894#ifdef CONFIG_DEBUG_FS
895
896#include <linux/seq_file.h>
897
898static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
899{
900 int mode;
901 unsigned i;
902 unsigned gpio = chip->base;
903 int is_out;
904 struct nmk_gpio_chip *nmk_chip =
905 container_of(chip, struct nmk_gpio_chip, chip);
906 const char *modes[] = {
907 [NMK_GPIO_ALT_GPIO] = "gpio",
908 [NMK_GPIO_ALT_A] = "altA",
909 [NMK_GPIO_ALT_B] = "altB",
910 [NMK_GPIO_ALT_C] = "altC",
911 };
912
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200913 clk_enable(nmk_chip->clk);
914
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530915 for (i = 0; i < chip->ngpio; i++, gpio++) {
916 const char *label = gpiochip_is_requested(chip, i);
917 bool pull;
918 u32 bit = 1 << i;
919
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530920 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
921 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
922 mode = nmk_gpio_get_mode(gpio);
923 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200924 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530925 is_out ? "out" : "in ",
926 chip->get
927 ? (chip->get(chip, i) ? "hi" : "lo")
928 : "? ",
929 (mode < 0) ? "unknown" : modes[mode],
930 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200931
932 if (label && !is_out) {
933 int irq = gpio_to_irq(gpio);
934 struct irq_desc *desc = irq_to_desc(irq);
935
936 /* This races with request_irq(), set_irq_type(),
937 * and set_irq_wake() ... but those are "rare".
938 */
939 if (irq >= 0 && desc->action) {
940 char *trigger;
941 u32 bitmask = nmk_gpio_get_bitmask(gpio);
942
943 if (nmk_chip->edge_rising & bitmask)
944 trigger = "edge-rising";
945 else if (nmk_chip->edge_falling & bitmask)
946 trigger = "edge-falling";
947 else
948 trigger = "edge-undefined";
949
950 seq_printf(s, " irq-%d %s%s",
951 irq, trigger,
952 irqd_is_wakeup_set(&desc->irq_data)
953 ? " wakeup" : "");
954 }
955 }
956
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530957 seq_printf(s, "\n");
958 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200959
960 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530961}
962
963#else
964#define nmk_gpio_dbg_show NULL
965#endif
966
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100967/* This structure is replicated for each GPIO block allocated at probe time */
968static struct gpio_chip nmk_gpio_template = {
969 .direction_input = nmk_gpio_make_input,
970 .get = nmk_gpio_get_input,
971 .direction_output = nmk_gpio_make_output,
972 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100973 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530974 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100975 .can_sleep = 0,
976};
977
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200978void nmk_gpio_clocks_enable(void)
979{
980 int i;
981
982 for (i = 0; i < NUM_BANKS; i++) {
983 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
984
985 if (!chip)
986 continue;
987
988 clk_enable(chip->clk);
989 }
990}
991
992void nmk_gpio_clocks_disable(void)
993{
994 int i;
995
996 for (i = 0; i < NUM_BANKS; i++) {
997 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
998
999 if (!chip)
1000 continue;
1001
1002 clk_disable(chip->clk);
1003 }
1004}
1005
Rabin Vincentb9df4682011-02-10 11:45:58 +05301006/*
1007 * Called from the suspend/resume path to only keep the real wakeup interrupts
1008 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1009 * and not the rest of the interrupts which we needed to have as wakeups for
1010 * cpuidle.
1011 *
1012 * PM ops are not used since this needs to be done at the end, after all the
1013 * other drivers are done with their suspend callbacks.
1014 */
1015void nmk_gpio_wakeups_suspend(void)
1016{
1017 int i;
1018
1019 for (i = 0; i < NUM_BANKS; i++) {
1020 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1021
1022 if (!chip)
1023 break;
1024
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001025 clk_enable(chip->clk);
1026
Rabin Vincentb9df4682011-02-10 11:45:58 +05301027 writel(chip->rwimsc & chip->real_wake,
1028 chip->addr + NMK_GPIO_RWIMSC);
1029 writel(chip->fwimsc & chip->real_wake,
1030 chip->addr + NMK_GPIO_FWIMSC);
1031
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001032 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301033 }
1034}
1035
1036void nmk_gpio_wakeups_resume(void)
1037{
1038 int i;
1039
1040 for (i = 0; i < NUM_BANKS; i++) {
1041 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1042
1043 if (!chip)
1044 break;
1045
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001046 clk_enable(chip->clk);
1047
Rabin Vincentb9df4682011-02-10 11:45:58 +05301048 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1049 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1050
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001051 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301052 }
1053}
1054
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001055/*
1056 * Read the pull up/pull down status.
1057 * A bit set in 'pull_up' means that pull up
1058 * is selected if pull is enabled in PDIS register.
1059 * Note: only pull up/down set via this driver can
1060 * be detected due to HW limitations.
1061 */
1062void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1063{
1064 if (gpio_bank < NUM_BANKS) {
1065 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1066
1067 if (!chip)
1068 return;
1069
1070 *pull_up = chip->pull_up;
1071 }
1072}
1073
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001074static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001075{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001076 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001077 struct nmk_gpio_chip *nmk_chip;
1078 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001079 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001080 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301081 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001082 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001083 int ret;
1084
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001085 if (!pdata)
1086 return -ENODEV;
1087
1088 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1089 if (!res) {
1090 ret = -ENOENT;
1091 goto out;
1092 }
1093
1094 irq = platform_get_irq(dev, 0);
1095 if (irq < 0) {
1096 ret = irq;
1097 goto out;
1098 }
1099
Rabin Vincent33b744b2010-10-14 10:38:03 +05301100 secondary_irq = platform_get_irq(dev, 1);
1101 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1102 ret = -EINVAL;
1103 goto out;
1104 }
1105
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001106 if (request_mem_region(res->start, resource_size(res),
1107 dev_name(&dev->dev)) == NULL) {
1108 ret = -EBUSY;
1109 goto out;
1110 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001111
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001112 clk = clk_get(&dev->dev, NULL);
1113 if (IS_ERR(clk)) {
1114 ret = PTR_ERR(clk);
1115 goto out_release;
1116 }
1117
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001118 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1119 if (!nmk_chip) {
1120 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001121 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001122 }
1123 /*
1124 * The virt address in nmk_chip->addr is in the nomadik register space,
1125 * so we can simply convert the resource address, without remapping
1126 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301127 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001128 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001129 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001130 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001131 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301132 nmk_chip->secondary_parent_irq = secondary_irq;
1133 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301134 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001135 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001136 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001137
1138 chip = &nmk_chip->chip;
1139 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301140 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301141 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001142 chip->dev = &dev->dev;
1143 chip->owner = THIS_MODULE;
1144
1145 ret = gpiochip_add(&nmk_chip->chip);
1146 if (ret)
1147 goto out_free;
1148
Rabin Vincent01727e62010-12-13 12:02:40 +05301149 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1150
1151 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001152 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001153
1154 nmk_gpio_init_irq(nmk_chip);
1155
Grant Likely64842aa2011-11-06 11:36:18 -07001156 dev_info(&dev->dev, "at address %p\n",
1157 nmk_chip->addr);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001158 return 0;
1159
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001160out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001161 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001162out_clk:
1163 clk_disable(clk);
1164 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001165out_release:
1166 release_mem_region(res->start, resource_size(res));
1167out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001168 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1169 pdata->first_gpio, pdata->first_gpio+31);
1170 return ret;
1171}
1172
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001173static struct platform_driver nmk_gpio_driver = {
1174 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001175 .owner = THIS_MODULE,
1176 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301177 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001178 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001179};
1180
1181static int __init nmk_gpio_init(void)
1182{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001183 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001184}
1185
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001186core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001187
1188MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1189MODULE_DESCRIPTION("Nomadik GPIO Driver");
1190MODULE_LICENSE("GPL");