blob: 70620426ee550fb4ecb8300e1ad144fd7d429623 [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>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010016#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010017#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010018#include <linux/clk.h>
19#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010020#include <linux/gpio.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010025
Rabin Vincent378be062010-06-02 06:06:29 +010026#include <plat/pincfg.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010027#include <mach/hardware.h>
28#include <mach/gpio.h>
29
30/*
31 * The GPIO module in the Nomadik family of Systems-on-Chip is an
32 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020033 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010034 *
35 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
36 */
37
Rabin Vincent01727e62010-12-13 12:02:40 +053038#define NMK_GPIO_PER_CHIP 32
39
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010040struct nmk_gpio_chip {
41 struct gpio_chip chip;
42 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010043 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053044 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010045 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053046 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053047 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053048 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010049 spinlock_t lock;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010050 /* Keep track of configured edges */
51 u32 edge_rising;
52 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053053 u32 real_wake;
54 u32 rwimsc;
55 u32 fwimsc;
56 u32 slpm;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010057};
58
Rabin Vincent01727e62010-12-13 12:02:40 +053059static struct nmk_gpio_chip *
60nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
61
62static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
63
64#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
65
Rabin Vincent6f9a9742010-06-02 05:50:28 +010066static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
67 unsigned offset, int gpio_mode)
68{
69 u32 bit = 1 << offset;
70 u32 afunc, bfunc;
71
72 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
73 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
74 if (gpio_mode & NMK_GPIO_ALT_A)
75 afunc |= bit;
76 if (gpio_mode & NMK_GPIO_ALT_B)
77 bfunc |= bit;
78 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
79 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
80}
81
Rabin Vincent81a3c292010-05-27 12:39:23 +010082static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
83 unsigned offset, enum nmk_gpio_slpm mode)
84{
85 u32 bit = 1 << offset;
86 u32 slpm;
87
88 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
89 if (mode == NMK_GPIO_SLPM_NOCHANGE)
90 slpm |= bit;
91 else
92 slpm &= ~bit;
93 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
94}
95
Rabin Vincent5b327ed2010-05-27 12:29:50 +010096static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
97 unsigned offset, enum nmk_gpio_pull pull)
98{
99 u32 bit = 1 << offset;
100 u32 pdis;
101
102 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
103 if (pull == NMK_GPIO_PULL_NONE)
104 pdis |= bit;
105 else
106 pdis &= ~bit;
107 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
108
Rabin Vincent5317e4d12011-02-10 09:29:53 +0530109 if (pull == NMK_GPIO_PULL_UP)
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100110 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rabin Vincent5317e4d12011-02-10 09:29:53 +0530111 else if (pull == NMK_GPIO_PULL_DOWN)
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100112 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
113}
114
Rabin Vincent378be062010-06-02 06:06:29 +0100115static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
116 unsigned offset)
117{
118 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
119}
120
Rabin Vincent6720db72010-09-02 11:28:48 +0100121static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
122 unsigned offset, int val)
123{
124 if (val)
125 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
126 else
127 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
128}
129
130static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
131 unsigned offset, int val)
132{
133 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
134 __nmk_gpio_set_output(nmk_chip, offset, val);
135}
136
Rabin Vincent01727e62010-12-13 12:02:40 +0530137static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
138 unsigned offset, int gpio_mode,
139 bool glitch)
140{
Linus Walleij3c4bee02011-02-16 10:37:52 +0100141 u32 rwimsc = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
142 u32 fwimsc = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
Rabin Vincent01727e62010-12-13 12:02:40 +0530143
144 if (glitch && nmk_chip->set_ioforce) {
145 u32 bit = BIT(offset);
146
Rabin Vincent01727e62010-12-13 12:02:40 +0530147 /* Prevent spurious wakeups */
148 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
149 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
150
151 nmk_chip->set_ioforce(true);
152 }
153
154 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
155
156 if (glitch && nmk_chip->set_ioforce) {
157 nmk_chip->set_ioforce(false);
158
159 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
160 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
161 }
162}
163
Rabin Vincent378be062010-06-02 06:06:29 +0100164static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530165 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100166{
167 static const char *afnames[] = {
168 [NMK_GPIO_ALT_GPIO] = "GPIO",
169 [NMK_GPIO_ALT_A] = "A",
170 [NMK_GPIO_ALT_B] = "B",
171 [NMK_GPIO_ALT_C] = "C"
172 };
173 static const char *pullnames[] = {
174 [NMK_GPIO_PULL_NONE] = "none",
175 [NMK_GPIO_PULL_UP] = "up",
176 [NMK_GPIO_PULL_DOWN] = "down",
177 [3] /* illegal */ = "??"
178 };
179 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100180 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
181 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100182 };
183
184 int pin = PIN_NUM(cfg);
185 int pull = PIN_PULL(cfg);
186 int af = PIN_ALT(cfg);
187 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100188 int output = PIN_DIR(cfg);
189 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530190 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100191
Rabin Vincentdacdc962010-12-03 20:35:37 +0530192 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
193 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100194 output ? "output " : "input",
195 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100196
Rabin Vincentdacdc962010-12-03 20:35:37 +0530197 if (sleep) {
198 int slpm_pull = PIN_SLPM_PULL(cfg);
199 int slpm_output = PIN_SLPM_DIR(cfg);
200 int slpm_val = PIN_SLPM_VAL(cfg);
201
Rabin Vincent3546d152010-11-25 11:38:27 +0530202 af = NMK_GPIO_ALT_GPIO;
203
Rabin Vincentdacdc962010-12-03 20:35:37 +0530204 /*
205 * The SLPM_* values are normal values + 1 to allow zero to
206 * mean "same as normal".
207 */
208 if (slpm_pull)
209 pull = slpm_pull - 1;
210 if (slpm_output)
211 output = slpm_output - 1;
212 if (slpm_val)
213 val = slpm_val - 1;
214
215 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
216 pin,
217 slpm_pull ? pullnames[pull] : "same",
218 slpm_output ? (output ? "output" : "input") : "same",
219 slpm_val ? (val ? "high" : "low") : "same");
220 }
221
Rabin Vincent6720db72010-09-02 11:28:48 +0100222 if (output)
223 __nmk_gpio_make_output(nmk_chip, offset, val);
224 else {
225 __nmk_gpio_make_input(nmk_chip, offset);
226 __nmk_gpio_set_pull(nmk_chip, offset, pull);
227 }
228
Rabin Vincent01727e62010-12-13 12:02:40 +0530229 /*
230 * If we've backed up the SLPM registers (glitch workaround), modify
231 * the backups since they will be restored.
232 */
233 if (slpmregs) {
234 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
235 slpmregs[nmk_chip->bank] |= BIT(offset);
236 else
237 slpmregs[nmk_chip->bank] &= ~BIT(offset);
238 } else
239 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
240
241 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
242}
243
244/*
245 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
246 * - Save SLPM registers
247 * - Set SLPM=0 for the IOs you want to switch and others to 1
248 * - Configure the GPIO registers for the IOs that are being switched
249 * - Set IOFORCE=1
250 * - Modify the AFLSA/B registers for the IOs that are being switched
251 * - Set IOFORCE=0
252 * - Restore SLPM registers
253 * - Any spurious wake up event during switch sequence to be ignored and
254 * cleared
255 */
256static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
257{
258 int i;
259
260 for (i = 0; i < NUM_BANKS; i++) {
261 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
262 unsigned int temp = slpm[i];
263
264 if (!chip)
265 break;
266
267 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
268 writel(temp, chip->addr + NMK_GPIO_SLPC);
269 }
270}
271
272static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
273{
274 int i;
275
276 for (i = 0; i < NUM_BANKS; i++) {
277 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
278
279 if (!chip)
280 break;
281
282 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
283 }
284}
285
286static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
287{
288 static unsigned int slpm[NUM_BANKS];
289 unsigned long flags;
290 bool glitch = false;
291 int ret = 0;
292 int i;
293
294 for (i = 0; i < num; i++) {
295 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
296 glitch = true;
297 break;
298 }
299 }
300
301 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
302
303 if (glitch) {
304 memset(slpm, 0xff, sizeof(slpm));
305
306 for (i = 0; i < num; i++) {
307 int pin = PIN_NUM(cfgs[i]);
308 int offset = pin % NMK_GPIO_PER_CHIP;
309
310 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
311 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
312 }
313
314 nmk_gpio_glitch_slpm_init(slpm);
315 }
316
317 for (i = 0; i < num; i++) {
318 struct nmk_gpio_chip *nmk_chip;
319 int pin = PIN_NUM(cfgs[i]);
320
321 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
322 if (!nmk_chip) {
323 ret = -EINVAL;
324 break;
325 }
326
327 spin_lock(&nmk_chip->lock);
328 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
329 cfgs[i], sleep, glitch ? slpm : NULL);
330 spin_unlock(&nmk_chip->lock);
331 }
332
333 if (glitch)
334 nmk_gpio_glitch_slpm_restore(slpm);
335
336 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
337
338 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100339}
340
341/**
342 * nmk_config_pin - configure a pin's mux attributes
343 * @cfg: pin confguration
344 *
345 * Configures a pin's mode (alternate function or GPIO), its pull up status,
346 * and its sleep mode based on the specified configuration. The @cfg is
347 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
348 * are constructed using, and can be further enhanced with, the macros in
349 * plat/pincfg.h.
350 *
351 * If a pin's mode is set to GPIO, it is configured as an input to avoid
352 * side-effects. The gpio can be manipulated later using standard GPIO API
353 * calls.
354 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530355int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100356{
Rabin Vincent01727e62010-12-13 12:02:40 +0530357 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100358}
359EXPORT_SYMBOL(nmk_config_pin);
360
361/**
362 * nmk_config_pins - configure several pins at once
363 * @cfgs: array of pin configurations
364 * @num: number of elments in the array
365 *
366 * Configures several pins using nmk_config_pin(). Refer to that function for
367 * further information.
368 */
369int nmk_config_pins(pin_cfg_t *cfgs, int num)
370{
Rabin Vincent01727e62010-12-13 12:02:40 +0530371 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100372}
373EXPORT_SYMBOL(nmk_config_pins);
374
Rabin Vincentdacdc962010-12-03 20:35:37 +0530375int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
376{
Rabin Vincent01727e62010-12-13 12:02:40 +0530377 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530378}
379EXPORT_SYMBOL(nmk_config_pins_sleep);
380
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100381/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100382 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
383 * @gpio: pin number
384 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
385 *
386 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
387 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
388 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
389 * configured even when in sleep and deep sleep.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100390 *
391 * On DB8500v2 onwards, this setting loses the previous meaning and instead
392 * indicates if wakeup detection is enabled on the pin. Note that
393 * enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100394 */
395int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
396{
397 struct nmk_gpio_chip *nmk_chip;
398 unsigned long flags;
399
400 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
401 if (!nmk_chip)
402 return -EINVAL;
403
Rabin Vincent01727e62010-12-13 12:02:40 +0530404 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
405 spin_lock(&nmk_chip->lock);
406
Rabin Vincent81a3c292010-05-27 12:39:23 +0100407 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530408
409 spin_unlock(&nmk_chip->lock);
410 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100411
412 return 0;
413}
414
415/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100416 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
417 * @gpio: pin number
418 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
419 *
420 * Enables/disables pull up/down on a specified pin. This only takes effect if
421 * the pin is configured as an input (either explicitly or by the alternate
422 * function).
423 *
424 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
425 * configured as an input. Otherwise, due to the way the controller registers
426 * work, this function will change the value output on the pin.
427 */
428int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
429{
430 struct nmk_gpio_chip *nmk_chip;
431 unsigned long flags;
432
433 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
434 if (!nmk_chip)
435 return -EINVAL;
436
437 spin_lock_irqsave(&nmk_chip->lock, flags);
438 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
439 spin_unlock_irqrestore(&nmk_chip->lock, flags);
440
441 return 0;
442}
443
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100444/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200445/**
446 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
447 * @gpio: pin number
448 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
449 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
450 *
451 * Sets the mode of the specified pin to one of the alternate functions or
452 * plain GPIO.
453 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100454int nmk_gpio_set_mode(int gpio, int gpio_mode)
455{
456 struct nmk_gpio_chip *nmk_chip;
457 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100458
459 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
460 if (!nmk_chip)
461 return -EINVAL;
462
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100463 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100464 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100465 spin_unlock_irqrestore(&nmk_chip->lock, flags);
466
467 return 0;
468}
469EXPORT_SYMBOL(nmk_gpio_set_mode);
470
471int nmk_gpio_get_mode(int gpio)
472{
473 struct nmk_gpio_chip *nmk_chip;
474 u32 afunc, bfunc, bit;
475
476 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
477 if (!nmk_chip)
478 return -EINVAL;
479
480 bit = 1 << (gpio - nmk_chip->chip.base);
481
482 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
483 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
484
485 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
486}
487EXPORT_SYMBOL(nmk_gpio_get_mode);
488
489
490/* IRQ functions */
491static inline int nmk_gpio_get_bitmask(int gpio)
492{
493 return 1 << (gpio % 32);
494}
495
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100496static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100497{
498 int gpio;
499 struct nmk_gpio_chip *nmk_chip;
500
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100501 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
502 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100503 if (!nmk_chip)
504 return;
505 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
506}
507
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100508enum nmk_gpio_irq_type {
509 NORMAL,
510 WAKE,
511};
512
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100513static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100514 int gpio, enum nmk_gpio_irq_type which,
515 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100516{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100517 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
518 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100519 u32 bitmask = nmk_gpio_get_bitmask(gpio);
520 u32 reg;
521
522 /* we must individually set/clear the two edges */
523 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100524 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100525 if (enable)
526 reg |= bitmask;
527 else
528 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100529 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100530 }
531 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100532 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100533 if (enable)
534 reg |= bitmask;
535 else
536 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100537 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100538 }
539}
540
Rabin Vincentb9df4682011-02-10 11:45:58 +0530541static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
542 int gpio, bool on)
543{
544#ifdef CONFIG_ARCH_U8500
545 if (cpu_is_u8500v2()) {
546 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
547 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
548 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
549 }
550#endif
551 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
552}
553
554static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100555{
556 int gpio;
557 struct nmk_gpio_chip *nmk_chip;
558 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100559 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100560
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100561 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
562 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100563 bitmask = nmk_gpio_get_bitmask(gpio);
564 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100565 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100566
Rabin Vincentb9df4682011-02-10 11:45:58 +0530567 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
568 spin_lock(&nmk_chip->lock);
569
570 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
571
572 if (!(nmk_chip->real_wake & bitmask))
573 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
574
575 spin_unlock(&nmk_chip->lock);
576 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100577
578 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100579}
580
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100581static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100582{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530583 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100584}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100585
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100586static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100587{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530588 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100589}
590
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100591static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100592{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530593 struct irq_desc *desc = irq_to_desc(d->irq);
594 bool enabled = !(desc->status & IRQ_DISABLED);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100595 struct nmk_gpio_chip *nmk_chip;
596 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530597 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100598 int gpio;
599
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100600 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
601 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100602 if (!nmk_chip)
603 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530604 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100605
Rabin Vincent01727e62010-12-13 12:02:40 +0530606 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
607 spin_lock(&nmk_chip->lock);
608
Rabin Vincentb9df4682011-02-10 11:45:58 +0530609 if (!enabled)
610 __nmk_gpio_set_wake(nmk_chip, gpio, on);
611
612 if (on)
613 nmk_chip->real_wake |= bitmask;
614 else
615 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530616
617 spin_unlock(&nmk_chip->lock);
618 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100619
620 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100621}
622
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100623static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100624{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100625 struct irq_desc *desc = irq_to_desc(d->irq);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100626 bool enabled = !(desc->status & IRQ_DISABLED);
627 bool wake = desc->wake_depth;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100628 int gpio;
629 struct nmk_gpio_chip *nmk_chip;
630 unsigned long flags;
631 u32 bitmask;
632
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100633 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
634 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100635 bitmask = nmk_gpio_get_bitmask(gpio);
636 if (!nmk_chip)
637 return -EINVAL;
638
639 if (type & IRQ_TYPE_LEVEL_HIGH)
640 return -EINVAL;
641 if (type & IRQ_TYPE_LEVEL_LOW)
642 return -EINVAL;
643
644 spin_lock_irqsave(&nmk_chip->lock, flags);
645
Rabin Vincent7a852d82010-05-06 10:43:55 +0100646 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100647 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
648
Rabin Vincentb9df4682011-02-10 11:45:58 +0530649 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100650 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100651
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100652 nmk_chip->edge_rising &= ~bitmask;
653 if (type & IRQ_TYPE_EDGE_RISING)
654 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100655
656 nmk_chip->edge_falling &= ~bitmask;
657 if (type & IRQ_TYPE_EDGE_FALLING)
658 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100659
660 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100661 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
662
Rabin Vincentb9df4682011-02-10 11:45:58 +0530663 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100664 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100665
666 spin_unlock_irqrestore(&nmk_chip->lock, flags);
667
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100668 return 0;
669}
670
671static struct irq_chip nmk_gpio_irq_chip = {
672 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100673 .irq_ack = nmk_gpio_irq_ack,
674 .irq_mask = nmk_gpio_irq_mask,
675 .irq_unmask = nmk_gpio_irq_unmask,
676 .irq_set_type = nmk_gpio_irq_set_type,
677 .irq_set_wake = nmk_gpio_irq_set_wake,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100678};
679
Rabin Vincent33b744b2010-10-14 10:38:03 +0530680static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
681 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100682{
683 struct nmk_gpio_chip *nmk_chip;
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100684 struct irq_chip *host_chip = get_irq_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100685 unsigned int first_irq;
686
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100687 if (host_chip->irq_mask_ack)
688 host_chip->irq_mask_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100689 else {
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100690 host_chip->irq_mask(&desc->irq_data);
691 if (host_chip->irq_ack)
692 host_chip->irq_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100693 }
694
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100695 nmk_chip = get_irq_data(irq);
696 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530697 while (status) {
698 int bit = __ffs(status);
699
700 generic_handle_irq(first_irq + bit);
701 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100702 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100703
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100704 host_chip->irq_unmask(&desc->irq_data);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100705}
706
Rabin Vincent33b744b2010-10-14 10:38:03 +0530707static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
708{
709 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
710 u32 status = readl(nmk_chip->addr + NMK_GPIO_IS);
711
712 __nmk_gpio_irq_handler(irq, desc, status);
713}
714
715static void nmk_gpio_secondary_irq_handler(unsigned int irq,
716 struct irq_desc *desc)
717{
718 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
719 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
720
721 __nmk_gpio_irq_handler(irq, desc, status);
722}
723
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100724static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
725{
726 unsigned int first_irq;
727 int i;
728
729 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530730 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100731 set_irq_chip(i, &nmk_gpio_irq_chip);
732 set_irq_handler(i, handle_edge_irq);
733 set_irq_flags(i, IRQF_VALID);
734 set_irq_chip_data(i, nmk_chip);
Rabin Vincent2210d642010-05-06 10:45:18 +0100735 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100736 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530737
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100738 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
739 set_irq_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530740
741 if (nmk_chip->secondary_parent_irq >= 0) {
742 set_irq_chained_handler(nmk_chip->secondary_parent_irq,
743 nmk_gpio_secondary_irq_handler);
744 set_irq_data(nmk_chip->secondary_parent_irq, nmk_chip);
745 }
746
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100747 return 0;
748}
749
750/* I/O Functions */
751static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
752{
753 struct nmk_gpio_chip *nmk_chip =
754 container_of(chip, struct nmk_gpio_chip, chip);
755
756 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
757 return 0;
758}
759
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100760static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
761{
762 struct nmk_gpio_chip *nmk_chip =
763 container_of(chip, struct nmk_gpio_chip, chip);
764 u32 bit = 1 << offset;
765
766 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
767}
768
769static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
770 int val)
771{
772 struct nmk_gpio_chip *nmk_chip =
773 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100774
Rabin Vincent6720db72010-09-02 11:28:48 +0100775 __nmk_gpio_set_output(nmk_chip, offset, val);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100776}
777
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100778static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
779 int val)
780{
781 struct nmk_gpio_chip *nmk_chip =
782 container_of(chip, struct nmk_gpio_chip, chip);
783
Rabin Vincent6720db72010-09-02 11:28:48 +0100784 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100785
786 return 0;
787}
788
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100789static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
790{
791 struct nmk_gpio_chip *nmk_chip =
792 container_of(chip, struct nmk_gpio_chip, chip);
793
794 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
795}
796
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530797#ifdef CONFIG_DEBUG_FS
798
799#include <linux/seq_file.h>
800
801static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
802{
803 int mode;
804 unsigned i;
805 unsigned gpio = chip->base;
806 int is_out;
807 struct nmk_gpio_chip *nmk_chip =
808 container_of(chip, struct nmk_gpio_chip, chip);
809 const char *modes[] = {
810 [NMK_GPIO_ALT_GPIO] = "gpio",
811 [NMK_GPIO_ALT_A] = "altA",
812 [NMK_GPIO_ALT_B] = "altB",
813 [NMK_GPIO_ALT_C] = "altC",
814 };
815
816 for (i = 0; i < chip->ngpio; i++, gpio++) {
817 const char *label = gpiochip_is_requested(chip, i);
818 bool pull;
819 u32 bit = 1 << i;
820
821 if (!label)
822 continue;
823
824 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
825 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
826 mode = nmk_gpio_get_mode(gpio);
827 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
828 gpio, label,
829 is_out ? "out" : "in ",
830 chip->get
831 ? (chip->get(chip, i) ? "hi" : "lo")
832 : "? ",
833 (mode < 0) ? "unknown" : modes[mode],
834 pull ? "pull" : "none");
835
836 if (!is_out) {
837 int irq = gpio_to_irq(gpio);
838 struct irq_desc *desc = irq_to_desc(irq);
839
840 /* This races with request_irq(), set_irq_type(),
841 * and set_irq_wake() ... but those are "rare".
842 *
843 * More significantly, trigger type flags aren't
844 * currently maintained by genirq.
845 */
846 if (irq >= 0 && desc->action) {
847 char *trigger;
848
849 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
850 case IRQ_TYPE_NONE:
851 trigger = "(default)";
852 break;
853 case IRQ_TYPE_EDGE_FALLING:
854 trigger = "edge-falling";
855 break;
856 case IRQ_TYPE_EDGE_RISING:
857 trigger = "edge-rising";
858 break;
859 case IRQ_TYPE_EDGE_BOTH:
860 trigger = "edge-both";
861 break;
862 case IRQ_TYPE_LEVEL_HIGH:
863 trigger = "level-high";
864 break;
865 case IRQ_TYPE_LEVEL_LOW:
866 trigger = "level-low";
867 break;
868 default:
869 trigger = "?trigger?";
870 break;
871 }
872
873 seq_printf(s, " irq-%d %s%s",
874 irq, trigger,
875 (desc->status & IRQ_WAKEUP)
876 ? " wakeup" : "");
877 }
878 }
879
880 seq_printf(s, "\n");
881 }
882}
883
884#else
885#define nmk_gpio_dbg_show NULL
886#endif
887
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100888/* This structure is replicated for each GPIO block allocated at probe time */
889static struct gpio_chip nmk_gpio_template = {
890 .direction_input = nmk_gpio_make_input,
891 .get = nmk_gpio_get_input,
892 .direction_output = nmk_gpio_make_output,
893 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100894 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530895 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100896 .can_sleep = 0,
897};
898
Rabin Vincentb9df4682011-02-10 11:45:58 +0530899/*
900 * Called from the suspend/resume path to only keep the real wakeup interrupts
901 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
902 * and not the rest of the interrupts which we needed to have as wakeups for
903 * cpuidle.
904 *
905 * PM ops are not used since this needs to be done at the end, after all the
906 * other drivers are done with their suspend callbacks.
907 */
908void nmk_gpio_wakeups_suspend(void)
909{
910 int i;
911
912 for (i = 0; i < NUM_BANKS; i++) {
913 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
914
915 if (!chip)
916 break;
917
918 chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
919 chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
920
921 writel(chip->rwimsc & chip->real_wake,
922 chip->addr + NMK_GPIO_RWIMSC);
923 writel(chip->fwimsc & chip->real_wake,
924 chip->addr + NMK_GPIO_FWIMSC);
925
926 if (cpu_is_u8500v2()) {
927 chip->slpm = readl(chip->addr + NMK_GPIO_SLPC);
928
929 /* 0 -> wakeup enable */
930 writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC);
931 }
932 }
933}
934
935void nmk_gpio_wakeups_resume(void)
936{
937 int i;
938
939 for (i = 0; i < NUM_BANKS; i++) {
940 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
941
942 if (!chip)
943 break;
944
945 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
946 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
947
948 if (cpu_is_u8500v2())
949 writel(chip->slpm, chip->addr + NMK_GPIO_SLPC);
950 }
951}
952
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +0100953static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100954{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100955 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100956 struct nmk_gpio_chip *nmk_chip;
957 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100958 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100959 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530960 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100961 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100962 int ret;
963
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100964 if (!pdata)
965 return -ENODEV;
966
967 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
968 if (!res) {
969 ret = -ENOENT;
970 goto out;
971 }
972
973 irq = platform_get_irq(dev, 0);
974 if (irq < 0) {
975 ret = irq;
976 goto out;
977 }
978
Rabin Vincent33b744b2010-10-14 10:38:03 +0530979 secondary_irq = platform_get_irq(dev, 1);
980 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
981 ret = -EINVAL;
982 goto out;
983 }
984
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100985 if (request_mem_region(res->start, resource_size(res),
986 dev_name(&dev->dev)) == NULL) {
987 ret = -EBUSY;
988 goto out;
989 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100990
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100991 clk = clk_get(&dev->dev, NULL);
992 if (IS_ERR(clk)) {
993 ret = PTR_ERR(clk);
994 goto out_release;
995 }
996
997 clk_enable(clk);
998
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100999 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1000 if (!nmk_chip) {
1001 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001002 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001003 }
1004 /*
1005 * The virt address in nmk_chip->addr is in the nomadik register space,
1006 * so we can simply convert the resource address, without remapping
1007 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301008 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001009 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001010 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001011 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001012 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301013 nmk_chip->secondary_parent_irq = secondary_irq;
1014 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301015 nmk_chip->set_ioforce = pdata->set_ioforce;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001016 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001017
1018 chip = &nmk_chip->chip;
1019 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301020 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301021 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001022 chip->dev = &dev->dev;
1023 chip->owner = THIS_MODULE;
1024
1025 ret = gpiochip_add(&nmk_chip->chip);
1026 if (ret)
1027 goto out_free;
1028
Rabin Vincent01727e62010-12-13 12:02:40 +05301029 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1030
1031 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001032 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001033
1034 nmk_gpio_init_irq(nmk_chip);
1035
1036 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
1037 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
1038 return 0;
1039
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001040out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001041 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001042out_clk:
1043 clk_disable(clk);
1044 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001045out_release:
1046 release_mem_region(res->start, resource_size(res));
1047out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001048 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1049 pdata->first_gpio, pdata->first_gpio+31);
1050 return ret;
1051}
1052
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001053static struct platform_driver nmk_gpio_driver = {
1054 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001055 .owner = THIS_MODULE,
1056 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301057 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001058 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001059};
1060
1061static int __init nmk_gpio_init(void)
1062{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001063 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001064}
1065
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001066core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001067
1068MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1069MODULE_DESCRIPTION("Nomadik GPIO Driver");
1070MODULE_LICENSE("GPL");
1071
1072