blob: 5e6653f63286e8ce5bae178eccc91d2c014c2c2e [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;
53};
54
Rabin Vincent01727e62010-12-13 12:02:40 +053055static struct nmk_gpio_chip *
56nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
57
58static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
59
60#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
61
Rabin Vincent6f9a9742010-06-02 05:50:28 +010062static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
63 unsigned offset, int gpio_mode)
64{
65 u32 bit = 1 << offset;
66 u32 afunc, bfunc;
67
68 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
69 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
70 if (gpio_mode & NMK_GPIO_ALT_A)
71 afunc |= bit;
72 if (gpio_mode & NMK_GPIO_ALT_B)
73 bfunc |= bit;
74 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
75 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
76}
77
Rabin Vincent81a3c292010-05-27 12:39:23 +010078static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
79 unsigned offset, enum nmk_gpio_slpm mode)
80{
81 u32 bit = 1 << offset;
82 u32 slpm;
83
84 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
85 if (mode == NMK_GPIO_SLPM_NOCHANGE)
86 slpm |= bit;
87 else
88 slpm &= ~bit;
89 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
90}
91
Rabin Vincent5b327ed2010-05-27 12:29:50 +010092static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
93 unsigned offset, enum nmk_gpio_pull pull)
94{
95 u32 bit = 1 << offset;
96 u32 pdis;
97
98 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
99 if (pull == NMK_GPIO_PULL_NONE)
100 pdis |= bit;
101 else
102 pdis &= ~bit;
103 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
104
Rabin Vincent5317e4d12011-02-10 09:29:53 +0530105 if (pull == NMK_GPIO_PULL_UP)
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100106 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rabin Vincent5317e4d12011-02-10 09:29:53 +0530107 else if (pull == NMK_GPIO_PULL_DOWN)
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100108 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
109}
110
Rabin Vincent378be062010-06-02 06:06:29 +0100111static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
112 unsigned offset)
113{
114 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
115}
116
Rabin Vincent6720db72010-09-02 11:28:48 +0100117static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
118 unsigned offset, int val)
119{
120 if (val)
121 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
122 else
123 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
124}
125
126static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
127 unsigned offset, int val)
128{
129 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
130 __nmk_gpio_set_output(nmk_chip, offset, val);
131}
132
Rabin Vincent01727e62010-12-13 12:02:40 +0530133static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
134 unsigned offset, int gpio_mode,
135 bool glitch)
136{
137 u32 rwimsc;
138 u32 fwimsc;
139
140 if (glitch && nmk_chip->set_ioforce) {
141 u32 bit = BIT(offset);
142
143 rwimsc = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
144 fwimsc = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
145
146 /* Prevent spurious wakeups */
147 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
148 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
149
150 nmk_chip->set_ioforce(true);
151 }
152
153 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
154
155 if (glitch && nmk_chip->set_ioforce) {
156 nmk_chip->set_ioforce(false);
157
158 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
159 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
160 }
161}
162
Rabin Vincent378be062010-06-02 06:06:29 +0100163static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530164 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100165{
166 static const char *afnames[] = {
167 [NMK_GPIO_ALT_GPIO] = "GPIO",
168 [NMK_GPIO_ALT_A] = "A",
169 [NMK_GPIO_ALT_B] = "B",
170 [NMK_GPIO_ALT_C] = "C"
171 };
172 static const char *pullnames[] = {
173 [NMK_GPIO_PULL_NONE] = "none",
174 [NMK_GPIO_PULL_UP] = "up",
175 [NMK_GPIO_PULL_DOWN] = "down",
176 [3] /* illegal */ = "??"
177 };
178 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100179 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
180 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100181 };
182
183 int pin = PIN_NUM(cfg);
184 int pull = PIN_PULL(cfg);
185 int af = PIN_ALT(cfg);
186 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100187 int output = PIN_DIR(cfg);
188 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530189 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100190
Rabin Vincentdacdc962010-12-03 20:35:37 +0530191 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
192 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100193 output ? "output " : "input",
194 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100195
Rabin Vincentdacdc962010-12-03 20:35:37 +0530196 if (sleep) {
197 int slpm_pull = PIN_SLPM_PULL(cfg);
198 int slpm_output = PIN_SLPM_DIR(cfg);
199 int slpm_val = PIN_SLPM_VAL(cfg);
200
Rabin Vincent3546d152010-11-25 11:38:27 +0530201 af = NMK_GPIO_ALT_GPIO;
202
Rabin Vincentdacdc962010-12-03 20:35:37 +0530203 /*
204 * The SLPM_* values are normal values + 1 to allow zero to
205 * mean "same as normal".
206 */
207 if (slpm_pull)
208 pull = slpm_pull - 1;
209 if (slpm_output)
210 output = slpm_output - 1;
211 if (slpm_val)
212 val = slpm_val - 1;
213
214 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
215 pin,
216 slpm_pull ? pullnames[pull] : "same",
217 slpm_output ? (output ? "output" : "input") : "same",
218 slpm_val ? (val ? "high" : "low") : "same");
219 }
220
Rabin Vincent6720db72010-09-02 11:28:48 +0100221 if (output)
222 __nmk_gpio_make_output(nmk_chip, offset, val);
223 else {
224 __nmk_gpio_make_input(nmk_chip, offset);
225 __nmk_gpio_set_pull(nmk_chip, offset, pull);
226 }
227
Rabin Vincent01727e62010-12-13 12:02:40 +0530228 /*
229 * If we've backed up the SLPM registers (glitch workaround), modify
230 * the backups since they will be restored.
231 */
232 if (slpmregs) {
233 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
234 slpmregs[nmk_chip->bank] |= BIT(offset);
235 else
236 slpmregs[nmk_chip->bank] &= ~BIT(offset);
237 } else
238 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
239
240 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
241}
242
243/*
244 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
245 * - Save SLPM registers
246 * - Set SLPM=0 for the IOs you want to switch and others to 1
247 * - Configure the GPIO registers for the IOs that are being switched
248 * - Set IOFORCE=1
249 * - Modify the AFLSA/B registers for the IOs that are being switched
250 * - Set IOFORCE=0
251 * - Restore SLPM registers
252 * - Any spurious wake up event during switch sequence to be ignored and
253 * cleared
254 */
255static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
256{
257 int i;
258
259 for (i = 0; i < NUM_BANKS; i++) {
260 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
261 unsigned int temp = slpm[i];
262
263 if (!chip)
264 break;
265
266 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
267 writel(temp, chip->addr + NMK_GPIO_SLPC);
268 }
269}
270
271static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
272{
273 int i;
274
275 for (i = 0; i < NUM_BANKS; i++) {
276 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
277
278 if (!chip)
279 break;
280
281 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
282 }
283}
284
285static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
286{
287 static unsigned int slpm[NUM_BANKS];
288 unsigned long flags;
289 bool glitch = false;
290 int ret = 0;
291 int i;
292
293 for (i = 0; i < num; i++) {
294 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
295 glitch = true;
296 break;
297 }
298 }
299
300 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
301
302 if (glitch) {
303 memset(slpm, 0xff, sizeof(slpm));
304
305 for (i = 0; i < num; i++) {
306 int pin = PIN_NUM(cfgs[i]);
307 int offset = pin % NMK_GPIO_PER_CHIP;
308
309 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
310 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
311 }
312
313 nmk_gpio_glitch_slpm_init(slpm);
314 }
315
316 for (i = 0; i < num; i++) {
317 struct nmk_gpio_chip *nmk_chip;
318 int pin = PIN_NUM(cfgs[i]);
319
320 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
321 if (!nmk_chip) {
322 ret = -EINVAL;
323 break;
324 }
325
326 spin_lock(&nmk_chip->lock);
327 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
328 cfgs[i], sleep, glitch ? slpm : NULL);
329 spin_unlock(&nmk_chip->lock);
330 }
331
332 if (glitch)
333 nmk_gpio_glitch_slpm_restore(slpm);
334
335 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
336
337 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100338}
339
340/**
341 * nmk_config_pin - configure a pin's mux attributes
342 * @cfg: pin confguration
343 *
344 * Configures a pin's mode (alternate function or GPIO), its pull up status,
345 * and its sleep mode based on the specified configuration. The @cfg is
346 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
347 * are constructed using, and can be further enhanced with, the macros in
348 * plat/pincfg.h.
349 *
350 * If a pin's mode is set to GPIO, it is configured as an input to avoid
351 * side-effects. The gpio can be manipulated later using standard GPIO API
352 * calls.
353 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530354int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100355{
Rabin Vincent01727e62010-12-13 12:02:40 +0530356 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100357}
358EXPORT_SYMBOL(nmk_config_pin);
359
360/**
361 * nmk_config_pins - configure several pins at once
362 * @cfgs: array of pin configurations
363 * @num: number of elments in the array
364 *
365 * Configures several pins using nmk_config_pin(). Refer to that function for
366 * further information.
367 */
368int nmk_config_pins(pin_cfg_t *cfgs, int num)
369{
Rabin Vincent01727e62010-12-13 12:02:40 +0530370 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100371}
372EXPORT_SYMBOL(nmk_config_pins);
373
Rabin Vincentdacdc962010-12-03 20:35:37 +0530374int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
375{
Rabin Vincent01727e62010-12-13 12:02:40 +0530376 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530377}
378EXPORT_SYMBOL(nmk_config_pins_sleep);
379
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100380/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100381 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
382 * @gpio: pin number
383 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
384 *
385 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
386 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
387 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
388 * configured even when in sleep and deep sleep.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100389 *
390 * On DB8500v2 onwards, this setting loses the previous meaning and instead
391 * indicates if wakeup detection is enabled on the pin. Note that
392 * enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100393 */
394int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
395{
396 struct nmk_gpio_chip *nmk_chip;
397 unsigned long flags;
398
399 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
400 if (!nmk_chip)
401 return -EINVAL;
402
Rabin Vincent01727e62010-12-13 12:02:40 +0530403 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
404 spin_lock(&nmk_chip->lock);
405
Rabin Vincent81a3c292010-05-27 12:39:23 +0100406 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530407
408 spin_unlock(&nmk_chip->lock);
409 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100410
411 return 0;
412}
413
414/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100415 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
416 * @gpio: pin number
417 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
418 *
419 * Enables/disables pull up/down on a specified pin. This only takes effect if
420 * the pin is configured as an input (either explicitly or by the alternate
421 * function).
422 *
423 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
424 * configured as an input. Otherwise, due to the way the controller registers
425 * work, this function will change the value output on the pin.
426 */
427int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
428{
429 struct nmk_gpio_chip *nmk_chip;
430 unsigned long flags;
431
432 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
433 if (!nmk_chip)
434 return -EINVAL;
435
436 spin_lock_irqsave(&nmk_chip->lock, flags);
437 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
438 spin_unlock_irqrestore(&nmk_chip->lock, flags);
439
440 return 0;
441}
442
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100443/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200444/**
445 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
446 * @gpio: pin number
447 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
448 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
449 *
450 * Sets the mode of the specified pin to one of the alternate functions or
451 * plain GPIO.
452 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100453int nmk_gpio_set_mode(int gpio, int gpio_mode)
454{
455 struct nmk_gpio_chip *nmk_chip;
456 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100457
458 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
459 if (!nmk_chip)
460 return -EINVAL;
461
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100462 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100463 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100464 spin_unlock_irqrestore(&nmk_chip->lock, flags);
465
466 return 0;
467}
468EXPORT_SYMBOL(nmk_gpio_set_mode);
469
470int nmk_gpio_get_mode(int gpio)
471{
472 struct nmk_gpio_chip *nmk_chip;
473 u32 afunc, bfunc, bit;
474
475 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
476 if (!nmk_chip)
477 return -EINVAL;
478
479 bit = 1 << (gpio - nmk_chip->chip.base);
480
481 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
482 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
483
484 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
485}
486EXPORT_SYMBOL(nmk_gpio_get_mode);
487
488
489/* IRQ functions */
490static inline int nmk_gpio_get_bitmask(int gpio)
491{
492 return 1 << (gpio % 32);
493}
494
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100495static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100496{
497 int gpio;
498 struct nmk_gpio_chip *nmk_chip;
499
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100500 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
501 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100502 if (!nmk_chip)
503 return;
504 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
505}
506
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100507enum nmk_gpio_irq_type {
508 NORMAL,
509 WAKE,
510};
511
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100512static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100513 int gpio, enum nmk_gpio_irq_type which,
514 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100515{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100516 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
517 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100518 u32 bitmask = nmk_gpio_get_bitmask(gpio);
519 u32 reg;
520
521 /* we must individually set/clear the two edges */
522 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100523 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100524 if (enable)
525 reg |= bitmask;
526 else
527 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100528 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100529 }
530 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100531 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100532 if (enable)
533 reg |= bitmask;
534 else
535 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100536 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100537 }
538}
539
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100540static int nmk_gpio_irq_modify(struct irq_data *d, enum nmk_gpio_irq_type which,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100541 bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100542{
543 int gpio;
544 struct nmk_gpio_chip *nmk_chip;
545 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100546 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100548 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
549 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100550 bitmask = nmk_gpio_get_bitmask(gpio);
551 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100552 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100553
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100554 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100555 __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100556 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100557
558 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100559}
560
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100561static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100562{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100563 nmk_gpio_irq_modify(d, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100564}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100565
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100566static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100567{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100568 nmk_gpio_irq_modify(d, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100569}
570
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100571static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100572{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100573 struct nmk_gpio_chip *nmk_chip;
574 unsigned long flags;
575 int gpio;
576
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100577 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
578 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100579 if (!nmk_chip)
580 return -EINVAL;
581
Rabin Vincent01727e62010-12-13 12:02:40 +0530582 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
583 spin_lock(&nmk_chip->lock);
584
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100585#ifdef CONFIG_ARCH_U8500
586 if (cpu_is_u8500v2()) {
Rikard Olsson8b40eee2011-01-03 14:30:41 +0100587 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100588 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
589 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
590 }
591#endif
592 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
Rabin Vincent01727e62010-12-13 12:02:40 +0530593
594 spin_unlock(&nmk_chip->lock);
595 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100596
597 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100598}
599
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100600static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100601{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100602 struct irq_desc *desc = irq_to_desc(d->irq);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100603 bool enabled = !(desc->status & IRQ_DISABLED);
604 bool wake = desc->wake_depth;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100605 int gpio;
606 struct nmk_gpio_chip *nmk_chip;
607 unsigned long flags;
608 u32 bitmask;
609
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100610 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
611 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100612 bitmask = nmk_gpio_get_bitmask(gpio);
613 if (!nmk_chip)
614 return -EINVAL;
615
616 if (type & IRQ_TYPE_LEVEL_HIGH)
617 return -EINVAL;
618 if (type & IRQ_TYPE_LEVEL_LOW)
619 return -EINVAL;
620
621 spin_lock_irqsave(&nmk_chip->lock, flags);
622
Rabin Vincent7a852d82010-05-06 10:43:55 +0100623 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100624 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
625
626 if (wake)
627 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100628
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100629 nmk_chip->edge_rising &= ~bitmask;
630 if (type & IRQ_TYPE_EDGE_RISING)
631 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100632
633 nmk_chip->edge_falling &= ~bitmask;
634 if (type & IRQ_TYPE_EDGE_FALLING)
635 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100636
637 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100638 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
639
640 if (wake)
641 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100642
643 spin_unlock_irqrestore(&nmk_chip->lock, flags);
644
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100645 return 0;
646}
647
648static struct irq_chip nmk_gpio_irq_chip = {
649 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100650 .irq_ack = nmk_gpio_irq_ack,
651 .irq_mask = nmk_gpio_irq_mask,
652 .irq_unmask = nmk_gpio_irq_unmask,
653 .irq_set_type = nmk_gpio_irq_set_type,
654 .irq_set_wake = nmk_gpio_irq_set_wake,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100655};
656
Rabin Vincent33b744b2010-10-14 10:38:03 +0530657static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
658 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100659{
660 struct nmk_gpio_chip *nmk_chip;
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100661 struct irq_chip *host_chip = get_irq_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100662 unsigned int first_irq;
663
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100664 if (host_chip->irq_mask_ack)
665 host_chip->irq_mask_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100666 else {
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100667 host_chip->irq_mask(&desc->irq_data);
668 if (host_chip->irq_ack)
669 host_chip->irq_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100670 }
671
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100672 nmk_chip = get_irq_data(irq);
673 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530674 while (status) {
675 int bit = __ffs(status);
676
677 generic_handle_irq(first_irq + bit);
678 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100679 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100680
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100681 host_chip->irq_unmask(&desc->irq_data);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100682}
683
Rabin Vincent33b744b2010-10-14 10:38:03 +0530684static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
685{
686 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
687 u32 status = readl(nmk_chip->addr + NMK_GPIO_IS);
688
689 __nmk_gpio_irq_handler(irq, desc, status);
690}
691
692static void nmk_gpio_secondary_irq_handler(unsigned int irq,
693 struct irq_desc *desc)
694{
695 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
696 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
697
698 __nmk_gpio_irq_handler(irq, desc, status);
699}
700
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100701static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
702{
703 unsigned int first_irq;
704 int i;
705
706 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530707 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100708 set_irq_chip(i, &nmk_gpio_irq_chip);
709 set_irq_handler(i, handle_edge_irq);
710 set_irq_flags(i, IRQF_VALID);
711 set_irq_chip_data(i, nmk_chip);
Rabin Vincent2210d642010-05-06 10:45:18 +0100712 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100713 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530714
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100715 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
716 set_irq_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530717
718 if (nmk_chip->secondary_parent_irq >= 0) {
719 set_irq_chained_handler(nmk_chip->secondary_parent_irq,
720 nmk_gpio_secondary_irq_handler);
721 set_irq_data(nmk_chip->secondary_parent_irq, nmk_chip);
722 }
723
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100724 return 0;
725}
726
727/* I/O Functions */
728static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
729{
730 struct nmk_gpio_chip *nmk_chip =
731 container_of(chip, struct nmk_gpio_chip, chip);
732
733 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
734 return 0;
735}
736
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100737static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
738{
739 struct nmk_gpio_chip *nmk_chip =
740 container_of(chip, struct nmk_gpio_chip, chip);
741 u32 bit = 1 << offset;
742
743 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
744}
745
746static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
747 int val)
748{
749 struct nmk_gpio_chip *nmk_chip =
750 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100751
Rabin Vincent6720db72010-09-02 11:28:48 +0100752 __nmk_gpio_set_output(nmk_chip, offset, val);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100753}
754
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100755static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
756 int val)
757{
758 struct nmk_gpio_chip *nmk_chip =
759 container_of(chip, struct nmk_gpio_chip, chip);
760
Rabin Vincent6720db72010-09-02 11:28:48 +0100761 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100762
763 return 0;
764}
765
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100766static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
767{
768 struct nmk_gpio_chip *nmk_chip =
769 container_of(chip, struct nmk_gpio_chip, chip);
770
771 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
772}
773
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530774#ifdef CONFIG_DEBUG_FS
775
776#include <linux/seq_file.h>
777
778static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
779{
780 int mode;
781 unsigned i;
782 unsigned gpio = chip->base;
783 int is_out;
784 struct nmk_gpio_chip *nmk_chip =
785 container_of(chip, struct nmk_gpio_chip, chip);
786 const char *modes[] = {
787 [NMK_GPIO_ALT_GPIO] = "gpio",
788 [NMK_GPIO_ALT_A] = "altA",
789 [NMK_GPIO_ALT_B] = "altB",
790 [NMK_GPIO_ALT_C] = "altC",
791 };
792
793 for (i = 0; i < chip->ngpio; i++, gpio++) {
794 const char *label = gpiochip_is_requested(chip, i);
795 bool pull;
796 u32 bit = 1 << i;
797
798 if (!label)
799 continue;
800
801 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
802 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
803 mode = nmk_gpio_get_mode(gpio);
804 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
805 gpio, label,
806 is_out ? "out" : "in ",
807 chip->get
808 ? (chip->get(chip, i) ? "hi" : "lo")
809 : "? ",
810 (mode < 0) ? "unknown" : modes[mode],
811 pull ? "pull" : "none");
812
813 if (!is_out) {
814 int irq = gpio_to_irq(gpio);
815 struct irq_desc *desc = irq_to_desc(irq);
816
817 /* This races with request_irq(), set_irq_type(),
818 * and set_irq_wake() ... but those are "rare".
819 *
820 * More significantly, trigger type flags aren't
821 * currently maintained by genirq.
822 */
823 if (irq >= 0 && desc->action) {
824 char *trigger;
825
826 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
827 case IRQ_TYPE_NONE:
828 trigger = "(default)";
829 break;
830 case IRQ_TYPE_EDGE_FALLING:
831 trigger = "edge-falling";
832 break;
833 case IRQ_TYPE_EDGE_RISING:
834 trigger = "edge-rising";
835 break;
836 case IRQ_TYPE_EDGE_BOTH:
837 trigger = "edge-both";
838 break;
839 case IRQ_TYPE_LEVEL_HIGH:
840 trigger = "level-high";
841 break;
842 case IRQ_TYPE_LEVEL_LOW:
843 trigger = "level-low";
844 break;
845 default:
846 trigger = "?trigger?";
847 break;
848 }
849
850 seq_printf(s, " irq-%d %s%s",
851 irq, trigger,
852 (desc->status & IRQ_WAKEUP)
853 ? " wakeup" : "");
854 }
855 }
856
857 seq_printf(s, "\n");
858 }
859}
860
861#else
862#define nmk_gpio_dbg_show NULL
863#endif
864
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100865/* This structure is replicated for each GPIO block allocated at probe time */
866static struct gpio_chip nmk_gpio_template = {
867 .direction_input = nmk_gpio_make_input,
868 .get = nmk_gpio_get_input,
869 .direction_output = nmk_gpio_make_output,
870 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100871 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530872 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873 .can_sleep = 0,
874};
875
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +0100876static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100877{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100878 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100879 struct nmk_gpio_chip *nmk_chip;
880 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100881 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100882 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530883 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100884 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100885 int ret;
886
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100887 if (!pdata)
888 return -ENODEV;
889
890 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
891 if (!res) {
892 ret = -ENOENT;
893 goto out;
894 }
895
896 irq = platform_get_irq(dev, 0);
897 if (irq < 0) {
898 ret = irq;
899 goto out;
900 }
901
Rabin Vincent33b744b2010-10-14 10:38:03 +0530902 secondary_irq = platform_get_irq(dev, 1);
903 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
904 ret = -EINVAL;
905 goto out;
906 }
907
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100908 if (request_mem_region(res->start, resource_size(res),
909 dev_name(&dev->dev)) == NULL) {
910 ret = -EBUSY;
911 goto out;
912 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100913
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100914 clk = clk_get(&dev->dev, NULL);
915 if (IS_ERR(clk)) {
916 ret = PTR_ERR(clk);
917 goto out_release;
918 }
919
920 clk_enable(clk);
921
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100922 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
923 if (!nmk_chip) {
924 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100925 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100926 }
927 /*
928 * The virt address in nmk_chip->addr is in the nomadik register space,
929 * so we can simply convert the resource address, without remapping
930 */
Rabin Vincent33b744b2010-10-14 10:38:03 +0530931 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100932 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100933 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100934 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100935 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530936 nmk_chip->secondary_parent_irq = secondary_irq;
937 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +0530938 nmk_chip->set_ioforce = pdata->set_ioforce;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +0100939 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100940
941 chip = &nmk_chip->chip;
942 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +0530943 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +0530944 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100945 chip->dev = &dev->dev;
946 chip->owner = THIS_MODULE;
947
948 ret = gpiochip_add(&nmk_chip->chip);
949 if (ret)
950 goto out_free;
951
Rabin Vincent01727e62010-12-13 12:02:40 +0530952 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
953
954 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100955 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100956
957 nmk_gpio_init_irq(nmk_chip);
958
959 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
960 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
961 return 0;
962
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100963out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100964 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100965out_clk:
966 clk_disable(clk);
967 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100968out_release:
969 release_mem_region(res->start, resource_size(res));
970out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100971 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
972 pdata->first_gpio, pdata->first_gpio+31);
973 return ret;
974}
975
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100976static struct platform_driver nmk_gpio_driver = {
977 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100978 .owner = THIS_MODULE,
979 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +0530980 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100981 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100982};
983
984static int __init nmk_gpio_init(void)
985{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100986 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100987}
988
Rabin Vincent33f45ea2010-06-02 06:09:52 +0100989core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100990
991MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
992MODULE_DESCRIPTION("Nomadik GPIO Driver");
993MODULE_LICENSE("GPL");
994
995