blob: 8ed7917d513da58822cca741b48c5b3dc7e9320e [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>
Lee Jonesa60b57e2012-04-19 21:36:31 +010025#include <linux/irqdomain.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Walleije98ea772012-04-26 23:57:25 +020027#include <linux/pinctrl/pinctrl.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010028
Will Deaconadfed152011-02-28 10:12:29 +000029#include <asm/mach/irq.h>
30
Rabin Vincent378be062010-06-02 06:06:29 +010031#include <plat/pincfg.h>
Linus Walleij0f332862011-08-22 08:33:30 +010032#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010033
Linus Walleije98ea772012-04-26 23:57:25 +020034#include "pinctrl-nomadik.h"
35
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010036/*
37 * The GPIO module in the Nomadik family of Systems-on-Chip is an
38 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020039 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010040 *
41 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
42 */
43
Rabin Vincent01727e62010-12-13 12:02:40 +053044#define NMK_GPIO_PER_CHIP 32
45
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010046struct nmk_gpio_chip {
47 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010048 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010049 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010050 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053051 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010052 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053053 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053054 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053055 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010056 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020057 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010058 /* Keep track of configured edges */
59 u32 edge_rising;
60 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053061 u32 real_wake;
62 u32 rwimsc;
63 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053064 u32 rimsc;
65 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020066 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053067 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010068};
69
Linus Walleije98ea772012-04-26 23:57:25 +020070struct nmk_pinctrl {
71 struct device *dev;
72 struct pinctrl_dev *pctl;
73 const struct nmk_pinctrl_soc_data *soc;
74};
75
Rabin Vincent01727e62010-12-13 12:02:40 +053076static struct nmk_gpio_chip *
77nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
78
79static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
80
81#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
82
Rabin Vincent6f9a9742010-06-02 05:50:28 +010083static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
84 unsigned offset, int gpio_mode)
85{
86 u32 bit = 1 << offset;
87 u32 afunc, bfunc;
88
89 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
90 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
91 if (gpio_mode & NMK_GPIO_ALT_A)
92 afunc |= bit;
93 if (gpio_mode & NMK_GPIO_ALT_B)
94 bfunc |= bit;
95 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
96 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
97}
98
Rabin Vincent81a3c292010-05-27 12:39:23 +010099static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
100 unsigned offset, enum nmk_gpio_slpm mode)
101{
102 u32 bit = 1 << offset;
103 u32 slpm;
104
105 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
106 if (mode == NMK_GPIO_SLPM_NOCHANGE)
107 slpm |= bit;
108 else
109 slpm &= ~bit;
110 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
111}
112
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100113static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
114 unsigned offset, enum nmk_gpio_pull pull)
115{
116 u32 bit = 1 << offset;
117 u32 pdis;
118
119 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200120 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100121 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200122 nmk_chip->pull_up &= ~bit;
123 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100124 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200125 }
126
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100127 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
128
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200129 if (pull == NMK_GPIO_PULL_UP) {
130 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100131 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200132 } else if (pull == NMK_GPIO_PULL_DOWN) {
133 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100134 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200135 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100136}
137
Rabin Vincentebc61782011-09-28 15:49:11 +0530138static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
139 unsigned offset, bool lowemi)
140{
141 u32 bit = BIT(offset);
142 bool enabled = nmk_chip->lowemi & bit;
143
144 if (lowemi == enabled)
145 return;
146
147 if (lowemi)
148 nmk_chip->lowemi |= bit;
149 else
150 nmk_chip->lowemi &= ~bit;
151
152 writel_relaxed(nmk_chip->lowemi,
153 nmk_chip->addr + NMK_GPIO_LOWEMI);
154}
155
Rabin Vincent378be062010-06-02 06:06:29 +0100156static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
157 unsigned offset)
158{
159 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
160}
161
Rabin Vincent6720db72010-09-02 11:28:48 +0100162static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
163 unsigned offset, int val)
164{
165 if (val)
166 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
167 else
168 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
169}
170
171static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
172 unsigned offset, int val)
173{
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
175 __nmk_gpio_set_output(nmk_chip, offset, val);
176}
177
Rabin Vincent01727e62010-12-13 12:02:40 +0530178static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
179 unsigned offset, int gpio_mode,
180 bool glitch)
181{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530182 u32 rwimsc = nmk_chip->rwimsc;
183 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530184
185 if (glitch && nmk_chip->set_ioforce) {
186 u32 bit = BIT(offset);
187
Rabin Vincent01727e62010-12-13 12:02:40 +0530188 /* Prevent spurious wakeups */
189 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
190 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
191
192 nmk_chip->set_ioforce(true);
193 }
194
195 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
196
197 if (glitch && nmk_chip->set_ioforce) {
198 nmk_chip->set_ioforce(false);
199
200 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
201 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
202 }
203}
204
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530205static void
206nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
207{
208 u32 falling = nmk_chip->fimsc & BIT(offset);
209 u32 rising = nmk_chip->rimsc & BIT(offset);
210 int gpio = nmk_chip->chip.base + offset;
211 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
212 struct irq_data *d = irq_get_irq_data(irq);
213
214 if (!rising && !falling)
215 return;
216
217 if (!d || !irqd_irq_disabled(d))
218 return;
219
220 if (rising) {
221 nmk_chip->rimsc &= ~BIT(offset);
222 writel_relaxed(nmk_chip->rimsc,
223 nmk_chip->addr + NMK_GPIO_RIMSC);
224 }
225
226 if (falling) {
227 nmk_chip->fimsc &= ~BIT(offset);
228 writel_relaxed(nmk_chip->fimsc,
229 nmk_chip->addr + NMK_GPIO_FIMSC);
230 }
231
232 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
233}
234
Rabin Vincent378be062010-06-02 06:06:29 +0100235static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530236 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100237{
238 static const char *afnames[] = {
239 [NMK_GPIO_ALT_GPIO] = "GPIO",
240 [NMK_GPIO_ALT_A] = "A",
241 [NMK_GPIO_ALT_B] = "B",
242 [NMK_GPIO_ALT_C] = "C"
243 };
244 static const char *pullnames[] = {
245 [NMK_GPIO_PULL_NONE] = "none",
246 [NMK_GPIO_PULL_UP] = "up",
247 [NMK_GPIO_PULL_DOWN] = "down",
248 [3] /* illegal */ = "??"
249 };
250 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100251 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
252 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100253 };
254
255 int pin = PIN_NUM(cfg);
256 int pull = PIN_PULL(cfg);
257 int af = PIN_ALT(cfg);
258 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100259 int output = PIN_DIR(cfg);
260 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530261 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100262
Rabin Vincentdacdc962010-12-03 20:35:37 +0530263 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
264 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100265 output ? "output " : "input",
266 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100267
Rabin Vincentdacdc962010-12-03 20:35:37 +0530268 if (sleep) {
269 int slpm_pull = PIN_SLPM_PULL(cfg);
270 int slpm_output = PIN_SLPM_DIR(cfg);
271 int slpm_val = PIN_SLPM_VAL(cfg);
272
Rabin Vincent3546d152010-11-25 11:38:27 +0530273 af = NMK_GPIO_ALT_GPIO;
274
Rabin Vincentdacdc962010-12-03 20:35:37 +0530275 /*
276 * The SLPM_* values are normal values + 1 to allow zero to
277 * mean "same as normal".
278 */
279 if (slpm_pull)
280 pull = slpm_pull - 1;
281 if (slpm_output)
282 output = slpm_output - 1;
283 if (slpm_val)
284 val = slpm_val - 1;
285
286 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
287 pin,
288 slpm_pull ? pullnames[pull] : "same",
289 slpm_output ? (output ? "output" : "input") : "same",
290 slpm_val ? (val ? "high" : "low") : "same");
291 }
292
Rabin Vincent6720db72010-09-02 11:28:48 +0100293 if (output)
294 __nmk_gpio_make_output(nmk_chip, offset, val);
295 else {
296 __nmk_gpio_make_input(nmk_chip, offset);
297 __nmk_gpio_set_pull(nmk_chip, offset, pull);
298 }
299
Rabin Vincentebc61782011-09-28 15:49:11 +0530300 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
301
Rabin Vincent01727e62010-12-13 12:02:40 +0530302 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530303 * If the pin is switching to altfunc, and there was an interrupt
304 * installed on it which has been lazy disabled, actually mask the
305 * interrupt to prevent spurious interrupts that would occur while the
306 * pin is under control of the peripheral. Only SKE does this.
307 */
308 if (af != NMK_GPIO_ALT_GPIO)
309 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
310
311 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530312 * If we've backed up the SLPM registers (glitch workaround), modify
313 * the backups since they will be restored.
314 */
315 if (slpmregs) {
316 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
317 slpmregs[nmk_chip->bank] |= BIT(offset);
318 else
319 slpmregs[nmk_chip->bank] &= ~BIT(offset);
320 } else
321 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
322
323 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
324}
325
326/*
327 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
328 * - Save SLPM registers
329 * - Set SLPM=0 for the IOs you want to switch and others to 1
330 * - Configure the GPIO registers for the IOs that are being switched
331 * - Set IOFORCE=1
332 * - Modify the AFLSA/B registers for the IOs that are being switched
333 * - Set IOFORCE=0
334 * - Restore SLPM registers
335 * - Any spurious wake up event during switch sequence to be ignored and
336 * cleared
337 */
338static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
339{
340 int i;
341
342 for (i = 0; i < NUM_BANKS; i++) {
343 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
344 unsigned int temp = slpm[i];
345
346 if (!chip)
347 break;
348
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200349 clk_enable(chip->clk);
350
Rabin Vincent01727e62010-12-13 12:02:40 +0530351 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
352 writel(temp, chip->addr + NMK_GPIO_SLPC);
353 }
354}
355
356static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
357{
358 int i;
359
360 for (i = 0; i < NUM_BANKS; i++) {
361 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
362
363 if (!chip)
364 break;
365
366 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200367
368 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530369 }
370}
371
372static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
373{
374 static unsigned int slpm[NUM_BANKS];
375 unsigned long flags;
376 bool glitch = false;
377 int ret = 0;
378 int i;
379
380 for (i = 0; i < num; i++) {
381 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
382 glitch = true;
383 break;
384 }
385 }
386
387 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
388
389 if (glitch) {
390 memset(slpm, 0xff, sizeof(slpm));
391
392 for (i = 0; i < num; i++) {
393 int pin = PIN_NUM(cfgs[i]);
394 int offset = pin % NMK_GPIO_PER_CHIP;
395
396 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
397 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
398 }
399
400 nmk_gpio_glitch_slpm_init(slpm);
401 }
402
403 for (i = 0; i < num; i++) {
404 struct nmk_gpio_chip *nmk_chip;
405 int pin = PIN_NUM(cfgs[i]);
406
Lee Jonesa60b57e2012-04-19 21:36:31 +0100407 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530408 if (!nmk_chip) {
409 ret = -EINVAL;
410 break;
411 }
412
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200413 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530414 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100415 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530416 cfgs[i], sleep, glitch ? slpm : NULL);
417 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200418 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530419 }
420
421 if (glitch)
422 nmk_gpio_glitch_slpm_restore(slpm);
423
424 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
425
426 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100427}
428
429/**
430 * nmk_config_pin - configure a pin's mux attributes
431 * @cfg: pin confguration
432 *
433 * Configures a pin's mode (alternate function or GPIO), its pull up status,
434 * and its sleep mode based on the specified configuration. The @cfg is
435 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
436 * are constructed using, and can be further enhanced with, the macros in
437 * plat/pincfg.h.
438 *
439 * If a pin's mode is set to GPIO, it is configured as an input to avoid
440 * side-effects. The gpio can be manipulated later using standard GPIO API
441 * calls.
442 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530443int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100444{
Rabin Vincent01727e62010-12-13 12:02:40 +0530445 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100446}
447EXPORT_SYMBOL(nmk_config_pin);
448
449/**
450 * nmk_config_pins - configure several pins at once
451 * @cfgs: array of pin configurations
452 * @num: number of elments in the array
453 *
454 * Configures several pins using nmk_config_pin(). Refer to that function for
455 * further information.
456 */
457int nmk_config_pins(pin_cfg_t *cfgs, int num)
458{
Rabin Vincent01727e62010-12-13 12:02:40 +0530459 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100460}
461EXPORT_SYMBOL(nmk_config_pins);
462
Rabin Vincentdacdc962010-12-03 20:35:37 +0530463int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
464{
Rabin Vincent01727e62010-12-13 12:02:40 +0530465 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530466}
467EXPORT_SYMBOL(nmk_config_pins_sleep);
468
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100469/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100470 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
471 * @gpio: pin number
472 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
473 *
Linus Walleij33d78642011-06-09 11:08:47 +0200474 * This register is actually in the pinmux layer, not the GPIO block itself.
475 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
476 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
477 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
478 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
479 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
480 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100481 *
Linus Walleij33d78642011-06-09 11:08:47 +0200482 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
483 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
484 * entered) regardless of the altfunction selected. Also wake-up detection is
485 * ENABLED.
486 *
487 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
488 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
489 * (for altfunction GPIO) or respective on-chip peripherals (for other
490 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
491 *
492 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100493 */
494int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
495{
496 struct nmk_gpio_chip *nmk_chip;
497 unsigned long flags;
498
Lee Jonesa60b57e2012-04-19 21:36:31 +0100499 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100500 if (!nmk_chip)
501 return -EINVAL;
502
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200503 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530504 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
505 spin_lock(&nmk_chip->lock);
506
Lee Jonesa60b57e2012-04-19 21:36:31 +0100507 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530508
509 spin_unlock(&nmk_chip->lock);
510 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200511 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100512
513 return 0;
514}
515
516/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100517 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
518 * @gpio: pin number
519 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
520 *
521 * Enables/disables pull up/down on a specified pin. This only takes effect if
522 * the pin is configured as an input (either explicitly or by the alternate
523 * function).
524 *
525 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
526 * configured as an input. Otherwise, due to the way the controller registers
527 * work, this function will change the value output on the pin.
528 */
529int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
530{
531 struct nmk_gpio_chip *nmk_chip;
532 unsigned long flags;
533
Lee Jonesa60b57e2012-04-19 21:36:31 +0100534 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100535 if (!nmk_chip)
536 return -EINVAL;
537
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200538 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100539 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100540 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100541 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200542 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100543
544 return 0;
545}
546
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200548/**
549 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
550 * @gpio: pin number
551 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
552 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
553 *
554 * Sets the mode of the specified pin to one of the alternate functions or
555 * plain GPIO.
556 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100557int nmk_gpio_set_mode(int gpio, int gpio_mode)
558{
559 struct nmk_gpio_chip *nmk_chip;
560 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100561
Lee Jonesa60b57e2012-04-19 21:36:31 +0100562 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100563 if (!nmk_chip)
564 return -EINVAL;
565
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200566 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100567 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100568 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100569 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200570 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100571
572 return 0;
573}
574EXPORT_SYMBOL(nmk_gpio_set_mode);
575
576int nmk_gpio_get_mode(int gpio)
577{
578 struct nmk_gpio_chip *nmk_chip;
579 u32 afunc, bfunc, bit;
580
Lee Jonesa60b57e2012-04-19 21:36:31 +0100581 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100582 if (!nmk_chip)
583 return -EINVAL;
584
Lee Jonesa60b57e2012-04-19 21:36:31 +0100585 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100586
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200587 clk_enable(nmk_chip->clk);
588
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100589 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
590 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
591
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200592 clk_disable(nmk_chip->clk);
593
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100594 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
595}
596EXPORT_SYMBOL(nmk_gpio_get_mode);
597
598
599/* IRQ functions */
600static inline int nmk_gpio_get_bitmask(int gpio)
601{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100602 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100603}
604
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100605static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100606{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607 struct nmk_gpio_chip *nmk_chip;
608
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100609 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100610 if (!nmk_chip)
611 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200612
613 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100614 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200615 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100616}
617
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100618enum nmk_gpio_irq_type {
619 NORMAL,
620 WAKE,
621};
622
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100623static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100624 int gpio, enum nmk_gpio_irq_type which,
625 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100626{
627 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530628 u32 *rimscval;
629 u32 *fimscval;
630 u32 rimscreg;
631 u32 fimscreg;
632
633 if (which == NORMAL) {
634 rimscreg = NMK_GPIO_RIMSC;
635 fimscreg = NMK_GPIO_FIMSC;
636 rimscval = &nmk_chip->rimsc;
637 fimscval = &nmk_chip->fimsc;
638 } else {
639 rimscreg = NMK_GPIO_RWIMSC;
640 fimscreg = NMK_GPIO_FWIMSC;
641 rimscval = &nmk_chip->rwimsc;
642 fimscval = &nmk_chip->fwimsc;
643 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100644
645 /* we must individually set/clear the two edges */
646 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100647 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530648 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100649 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530650 *rimscval &= ~bitmask;
651 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100652 }
653 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100654 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530655 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100656 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530657 *fimscval &= ~bitmask;
658 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100659 }
660}
661
Rabin Vincentb9df4682011-02-10 11:45:58 +0530662static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
663 int gpio, bool on)
664{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530665 /*
666 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
667 * disabled, since setting SLPM to 1 increases power consumption, and
668 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
669 */
670 if (nmk_chip->sleepmode && on) {
Lee Jonesa60b57e2012-04-19 21:36:31 +0100671 __nmk_gpio_set_slpm(nmk_chip, gpio % nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530672 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200673 }
674
Rabin Vincentb9df4682011-02-10 11:45:58 +0530675 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
676}
677
678static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100679{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680 struct nmk_gpio_chip *nmk_chip;
681 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100682 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100684 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100685 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100686 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100687 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100688
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200689 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530690 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
691 spin_lock(&nmk_chip->lock);
692
Lee Jonesa60b57e2012-04-19 21:36:31 +0100693 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530694
695 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100696 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530697
698 spin_unlock(&nmk_chip->lock);
699 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200700 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100701
702 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100703}
704
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100705static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100706{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530707 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100708}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100709
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100710static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530712 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100713}
714
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100715static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100716{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100717 struct nmk_gpio_chip *nmk_chip;
718 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530719 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100720
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100721 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100722 if (!nmk_chip)
723 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100724 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100725
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200726 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530727 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
728 spin_lock(&nmk_chip->lock);
729
Linus Walleij479a0c72011-09-20 10:50:15 +0200730 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100731 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530732
733 if (on)
734 nmk_chip->real_wake |= bitmask;
735 else
736 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530737
738 spin_unlock(&nmk_chip->lock);
739 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200740 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100741
742 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743}
744
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100745static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100746{
Linus Walleij479a0c72011-09-20 10:50:15 +0200747 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200748 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100749 struct nmk_gpio_chip *nmk_chip;
750 unsigned long flags;
751 u32 bitmask;
752
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100753 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100754 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100755 if (!nmk_chip)
756 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100757 if (type & IRQ_TYPE_LEVEL_HIGH)
758 return -EINVAL;
759 if (type & IRQ_TYPE_LEVEL_LOW)
760 return -EINVAL;
761
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200762 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100763 spin_lock_irqsave(&nmk_chip->lock, flags);
764
Rabin Vincent7a852d82010-05-06 10:43:55 +0100765 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100766 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100767
Rabin Vincentb9df4682011-02-10 11:45:58 +0530768 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100769 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100770
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100771 nmk_chip->edge_rising &= ~bitmask;
772 if (type & IRQ_TYPE_EDGE_RISING)
773 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100774
775 nmk_chip->edge_falling &= ~bitmask;
776 if (type & IRQ_TYPE_EDGE_FALLING)
777 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100778
779 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100780 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100781
Rabin Vincentb9df4682011-02-10 11:45:58 +0530782 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100783 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784
785 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200786 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100787
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788 return 0;
789}
790
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200791static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
792{
793 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
794
795 clk_enable(nmk_chip->clk);
796 nmk_gpio_irq_unmask(d);
797 return 0;
798}
799
800static void nmk_gpio_irq_shutdown(struct irq_data *d)
801{
802 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
803
804 nmk_gpio_irq_mask(d);
805 clk_disable(nmk_chip->clk);
806}
807
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100808static struct irq_chip nmk_gpio_irq_chip = {
809 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100810 .irq_ack = nmk_gpio_irq_ack,
811 .irq_mask = nmk_gpio_irq_mask,
812 .irq_unmask = nmk_gpio_irq_unmask,
813 .irq_set_type = nmk_gpio_irq_set_type,
814 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200815 .irq_startup = nmk_gpio_irq_startup,
816 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100817};
818
Rabin Vincent33b744b2010-10-14 10:38:03 +0530819static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
820 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100821{
822 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100823 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100824 unsigned int first_irq;
825
Will Deaconadfed152011-02-28 10:12:29 +0000826 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100827
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100828 nmk_chip = irq_get_handler_data(irq);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100829 first_irq = nmk_chip->domain->revmap_data.legacy.first_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530830 while (status) {
831 int bit = __ffs(status);
832
833 generic_handle_irq(first_irq + bit);
834 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100836
Will Deaconadfed152011-02-28 10:12:29 +0000837 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838}
839
Rabin Vincent33b744b2010-10-14 10:38:03 +0530840static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
841{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100842 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200843 u32 status;
844
845 clk_enable(nmk_chip->clk);
846 status = readl(nmk_chip->addr + NMK_GPIO_IS);
847 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530848
849 __nmk_gpio_irq_handler(irq, desc, status);
850}
851
852static void nmk_gpio_secondary_irq_handler(unsigned int irq,
853 struct irq_desc *desc)
854{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100855 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530856 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
857
858 __nmk_gpio_irq_handler(irq, desc, status);
859}
860
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100861static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
862{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100863 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
864 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530865
866 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100867 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530868 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100869 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530870 }
871
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100872 return 0;
873}
874
875/* I/O Functions */
876static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
877{
878 struct nmk_gpio_chip *nmk_chip =
879 container_of(chip, struct nmk_gpio_chip, chip);
880
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200881 clk_enable(nmk_chip->clk);
882
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100883 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200884
885 clk_disable(nmk_chip->clk);
886
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100887 return 0;
888}
889
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100890static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
891{
892 struct nmk_gpio_chip *nmk_chip =
893 container_of(chip, struct nmk_gpio_chip, chip);
894 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200895 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100896
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200897 clk_enable(nmk_chip->clk);
898
899 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
900
901 clk_disable(nmk_chip->clk);
902
903 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100904}
905
906static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
907 int val)
908{
909 struct nmk_gpio_chip *nmk_chip =
910 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100911
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200912 clk_enable(nmk_chip->clk);
913
Rabin Vincent6720db72010-09-02 11:28:48 +0100914 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200915
916 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100917}
918
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100919static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
920 int val)
921{
922 struct nmk_gpio_chip *nmk_chip =
923 container_of(chip, struct nmk_gpio_chip, chip);
924
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200925 clk_enable(nmk_chip->clk);
926
Rabin Vincent6720db72010-09-02 11:28:48 +0100927 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100928
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200929 clk_disable(nmk_chip->clk);
930
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100931 return 0;
932}
933
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100934static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
935{
936 struct nmk_gpio_chip *nmk_chip =
937 container_of(chip, struct nmk_gpio_chip, chip);
938
Lee Jonesa60b57e2012-04-19 21:36:31 +0100939 return irq_find_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100940}
941
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530942#ifdef CONFIG_DEBUG_FS
943
944#include <linux/seq_file.h>
945
Linus Walleij6f4350a2012-05-02 21:06:13 +0200946static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
947 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530948{
Linus Walleij6f4350a2012-05-02 21:06:13 +0200949 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530950 struct nmk_gpio_chip *nmk_chip =
951 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200952 int mode;
953 bool is_out;
954 bool pull;
955 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530956 const char *modes[] = {
957 [NMK_GPIO_ALT_GPIO] = "gpio",
958 [NMK_GPIO_ALT_A] = "altA",
959 [NMK_GPIO_ALT_B] = "altB",
960 [NMK_GPIO_ALT_C] = "altC",
961 };
962
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200963 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200964 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
965 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
966 mode = nmk_gpio_get_mode(gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200967
Linus Walleij6f4350a2012-05-02 21:06:13 +0200968 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
969 gpio, label ?: "(none)",
970 is_out ? "out" : "in ",
971 chip->get
972 ? (chip->get(chip, offset) ? "hi" : "lo")
973 : "? ",
974 (mode < 0) ? "unknown" : modes[mode],
975 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530976
Linus Walleij6f4350a2012-05-02 21:06:13 +0200977 if (label && !is_out) {
978 int irq = gpio_to_irq(gpio);
979 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200980
Linus Walleij6f4350a2012-05-02 21:06:13 +0200981 /* This races with request_irq(), set_irq_type(),
982 * and set_irq_wake() ... but those are "rare".
983 */
984 if (irq >= 0 && desc->action) {
985 char *trigger;
986 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200987
Linus Walleij6f4350a2012-05-02 21:06:13 +0200988 if (nmk_chip->edge_rising & bitmask)
989 trigger = "edge-rising";
990 else if (nmk_chip->edge_falling & bitmask)
991 trigger = "edge-falling";
992 else
993 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200994
Linus Walleij6f4350a2012-05-02 21:06:13 +0200995 seq_printf(s, " irq-%d %s%s",
996 irq, trigger,
997 irqd_is_wakeup_set(&desc->irq_data)
998 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200999 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301000 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001001 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301002}
1003
Linus Walleij6f4350a2012-05-02 21:06:13 +02001004static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1005{
1006 unsigned i;
1007 unsigned gpio = chip->base;
1008
1009 for (i = 0; i < chip->ngpio; i++, gpio++) {
1010 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1011 seq_printf(s, "\n");
1012 }
1013}
1014
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301015#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001016static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1017 struct gpio_chip *chip,
1018 unsigned offset, unsigned gpio)
1019{
1020}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301021#define nmk_gpio_dbg_show NULL
1022#endif
1023
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001024/* This structure is replicated for each GPIO block allocated at probe time */
1025static struct gpio_chip nmk_gpio_template = {
1026 .direction_input = nmk_gpio_make_input,
1027 .get = nmk_gpio_get_input,
1028 .direction_output = nmk_gpio_make_output,
1029 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001030 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301031 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001032 .can_sleep = 0,
1033};
1034
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001035void nmk_gpio_clocks_enable(void)
1036{
1037 int i;
1038
1039 for (i = 0; i < NUM_BANKS; i++) {
1040 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1041
1042 if (!chip)
1043 continue;
1044
1045 clk_enable(chip->clk);
1046 }
1047}
1048
1049void nmk_gpio_clocks_disable(void)
1050{
1051 int i;
1052
1053 for (i = 0; i < NUM_BANKS; i++) {
1054 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1055
1056 if (!chip)
1057 continue;
1058
1059 clk_disable(chip->clk);
1060 }
1061}
1062
Rabin Vincentb9df4682011-02-10 11:45:58 +05301063/*
1064 * Called from the suspend/resume path to only keep the real wakeup interrupts
1065 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1066 * and not the rest of the interrupts which we needed to have as wakeups for
1067 * cpuidle.
1068 *
1069 * PM ops are not used since this needs to be done at the end, after all the
1070 * other drivers are done with their suspend callbacks.
1071 */
1072void nmk_gpio_wakeups_suspend(void)
1073{
1074 int i;
1075
1076 for (i = 0; i < NUM_BANKS; i++) {
1077 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1078
1079 if (!chip)
1080 break;
1081
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001082 clk_enable(chip->clk);
1083
Rabin Vincentb9df4682011-02-10 11:45:58 +05301084 writel(chip->rwimsc & chip->real_wake,
1085 chip->addr + NMK_GPIO_RWIMSC);
1086 writel(chip->fwimsc & chip->real_wake,
1087 chip->addr + NMK_GPIO_FWIMSC);
1088
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001089 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301090 }
1091}
1092
1093void nmk_gpio_wakeups_resume(void)
1094{
1095 int i;
1096
1097 for (i = 0; i < NUM_BANKS; i++) {
1098 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1099
1100 if (!chip)
1101 break;
1102
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001103 clk_enable(chip->clk);
1104
Rabin Vincentb9df4682011-02-10 11:45:58 +05301105 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1106 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1107
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001108 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301109 }
1110}
1111
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001112/*
1113 * Read the pull up/pull down status.
1114 * A bit set in 'pull_up' means that pull up
1115 * is selected if pull is enabled in PDIS register.
1116 * Note: only pull up/down set via this driver can
1117 * be detected due to HW limitations.
1118 */
1119void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1120{
1121 if (gpio_bank < NUM_BANKS) {
1122 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1123
1124 if (!chip)
1125 return;
1126
1127 *pull_up = chip->pull_up;
1128 }
1129}
1130
Lee Jonesa60b57e2012-04-19 21:36:31 +01001131int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1132 irq_hw_number_t hwirq)
1133{
1134 struct nmk_gpio_chip *nmk_chip = d->host_data;
1135
1136 if (!nmk_chip)
1137 return -EINVAL;
1138
1139 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1140 set_irq_flags(irq, IRQF_VALID);
1141 irq_set_chip_data(irq, nmk_chip);
1142 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1143
1144 return 0;
1145}
1146
1147const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1148 .map = nmk_gpio_irq_map,
1149 .xlate = irq_domain_xlate_twocell,
1150};
1151
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001152static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001153{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001154 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001155 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001156 struct nmk_gpio_chip *nmk_chip;
1157 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001158 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001159 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301160 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001161 void __iomem *base;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001162 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001163 int ret;
1164
Lee Jones513c27f2012-04-13 15:05:05 +01001165 if (!pdata && !np) {
1166 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001167 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001168 }
1169
1170 if (np) {
1171 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1172 if (!pdata)
1173 return -ENOMEM;
1174
1175 if (of_get_property(np, "supports-sleepmode", NULL))
1176 pdata->supports_sleepmode = true;
1177
1178 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1179 dev_err(&dev->dev, "gpio-bank property not found\n");
1180 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001181 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001182 }
1183
1184 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1185 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1186 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001187
1188 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1189 if (!res) {
1190 ret = -ENOENT;
1191 goto out;
1192 }
1193
1194 irq = platform_get_irq(dev, 0);
1195 if (irq < 0) {
1196 ret = irq;
1197 goto out;
1198 }
1199
Rabin Vincent33b744b2010-10-14 10:38:03 +05301200 secondary_irq = platform_get_irq(dev, 1);
1201 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1202 ret = -EINVAL;
1203 goto out;
1204 }
1205
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001206 if (request_mem_region(res->start, resource_size(res),
1207 dev_name(&dev->dev)) == NULL) {
1208 ret = -EBUSY;
1209 goto out;
1210 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001211
Linus Walleij8d917712012-04-17 10:15:54 +02001212 base = ioremap(res->start, resource_size(res));
1213 if (!base) {
1214 ret = -ENOMEM;
1215 goto out_release;
1216 }
1217
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001218 clk = clk_get(&dev->dev, NULL);
1219 if (IS_ERR(clk)) {
1220 ret = PTR_ERR(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001221 goto out_unmap;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001222 }
1223
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001224 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1225 if (!nmk_chip) {
1226 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001227 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001228 }
Lee Jones513c27f2012-04-13 15:05:05 +01001229
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001230 /*
1231 * The virt address in nmk_chip->addr is in the nomadik register space,
1232 * so we can simply convert the resource address, without remapping
1233 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301234 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001235 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001236 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001237 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001238 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301239 nmk_chip->secondary_parent_irq = secondary_irq;
1240 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301241 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001242 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001243 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001244
1245 chip = &nmk_chip->chip;
1246 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301247 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301248 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001249 chip->dev = &dev->dev;
1250 chip->owner = THIS_MODULE;
1251
Rabin Vincentebc61782011-09-28 15:49:11 +05301252 clk_enable(nmk_chip->clk);
1253 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1254 clk_disable(nmk_chip->clk);
1255
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001256#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001257 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001258#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001259
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001260 ret = gpiochip_add(&nmk_chip->chip);
1261 if (ret)
1262 goto out_free;
1263
Rabin Vincent01727e62010-12-13 12:02:40 +05301264 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1265
1266 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001267
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001268 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001269
Lee Jonesa60b57e2012-04-19 21:36:31 +01001270 nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
1271 NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
1272 0, &nmk_gpio_irq_simple_ops, nmk_chip);
1273 if (!nmk_chip->domain) {
1274 pr_err("%s: Failed to create irqdomain\n", np->full_name);
1275 ret = -ENOSYS;
1276 goto out_free;
1277 }
1278
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001279 nmk_gpio_init_irq(nmk_chip);
1280
Lee Jones513c27f2012-04-13 15:05:05 +01001281 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1282
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001283 return 0;
1284
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001285out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001286 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001287out_clk:
1288 clk_disable(clk);
1289 clk_put(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001290out_unmap:
1291 iounmap(base);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001292out_release:
1293 release_mem_region(res->start, resource_size(res));
1294out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001295 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1296 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001297 if (np)
1298 kfree(pdata);
1299
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001300 return ret;
1301}
1302
Linus Walleije98ea772012-04-26 23:57:25 +02001303static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1304{
1305 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1306
1307 return npct->soc->ngroups;
1308}
1309
1310static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1311 unsigned selector)
1312{
1313 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1314
1315 return npct->soc->groups[selector].name;
1316}
1317
1318static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1319 const unsigned **pins,
1320 unsigned *num_pins)
1321{
1322 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1323
1324 *pins = npct->soc->groups[selector].pins;
1325 *num_pins = npct->soc->groups[selector].npins;
1326 return 0;
1327}
1328
1329static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1330 unsigned offset)
1331{
1332 seq_printf(s, " Nomadik GPIO");
1333}
1334
1335static struct pinctrl_ops nmk_pinctrl_ops = {
1336 .get_groups_count = nmk_get_groups_cnt,
1337 .get_group_name = nmk_get_group_name,
1338 .get_group_pins = nmk_get_group_pins,
1339 .pin_dbg_show = nmk_pin_dbg_show,
1340};
1341
1342static struct pinctrl_desc nmk_pinctrl_desc = {
1343 .name = "pinctrl-nomadik",
1344 .pctlops = &nmk_pinctrl_ops,
1345 .owner = THIS_MODULE,
1346};
1347
1348static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1349{
1350 const struct platform_device_id *platid = platform_get_device_id(pdev);
1351 struct nmk_pinctrl *npct;
1352 int i;
1353
1354 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1355 if (!npct)
1356 return -ENOMEM;
1357
1358 /* Poke in other ASIC variants here */
1359 if (platid->driver_data == PINCTRL_NMK_DB8500)
1360 nmk_pinctrl_db8500_init(&npct->soc);
1361
1362 /*
1363 * We need all the GPIO drivers to probe FIRST, or we will not be able
1364 * to obtain references to the struct gpio_chip * for them, and we
1365 * need this to proceed.
1366 */
1367 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1368 if (!nmk_gpio_chips[i]) {
1369 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
1370 devm_kfree(&pdev->dev, npct);
1371 return -EPROBE_DEFER;
1372 }
1373 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1374 }
1375
1376 nmk_pinctrl_desc.pins = npct->soc->pins;
1377 nmk_pinctrl_desc.npins = npct->soc->npins;
1378 npct->dev = &pdev->dev;
1379 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1380 if (!npct->pctl) {
1381 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1382 return -EINVAL;
1383 }
1384
1385 /* We will handle a range of GPIO pins */
1386 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1387 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1388
1389 platform_set_drvdata(pdev, npct);
1390 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1391
1392 return 0;
1393}
1394
Lee Jones513c27f2012-04-13 15:05:05 +01001395static const struct of_device_id nmk_gpio_match[] = {
1396 { .compatible = "st,nomadik-gpio", },
1397 {}
1398};
1399
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001400static struct platform_driver nmk_gpio_driver = {
1401 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001402 .owner = THIS_MODULE,
1403 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001404 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301405 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001406 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001407};
1408
Linus Walleije98ea772012-04-26 23:57:25 +02001409static const struct platform_device_id nmk_pinctrl_id[] = {
1410 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1411 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
1412};
1413
1414static struct platform_driver nmk_pinctrl_driver = {
1415 .driver = {
1416 .owner = THIS_MODULE,
1417 .name = "pinctrl-nomadik",
1418 },
1419 .probe = nmk_pinctrl_probe,
1420 .id_table = nmk_pinctrl_id,
1421};
1422
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001423static int __init nmk_gpio_init(void)
1424{
Linus Walleije98ea772012-04-26 23:57:25 +02001425 int ret;
1426
1427 ret = platform_driver_register(&nmk_gpio_driver);
1428 if (ret)
1429 return ret;
1430 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001431}
1432
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001433core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001434
1435MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1436MODULE_DESCRIPTION("Nomadik GPIO Driver");
1437MODULE_LICENSE("GPL");