pinctrl: sirf: add lost chained_irq_enter and exit in sirfsoc_gpio_handle_irq
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pinctrl / pinctrl-nomadik.c
CommitLineData
2ec1d359
AR
1/*
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>
33d78647 7 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
2ec1d359
AR
8 *
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>
3e3c62ca 17#include <linux/platform_device.h>
2ec1d359 18#include <linux/io.h>
af7dc228
RV
19#include <linux/clk.h>
20#include <linux/err.h>
2ec1d359
AR
21#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
a60b57ed 25#include <linux/irqdomain.h>
5a0e3ad6 26#include <linux/slab.h>
855f80cd 27#include <linux/of_device.h>
e98ea774 28#include <linux/pinctrl/pinctrl.h>
dbfe8ca2 29#include <linux/pinctrl/pinmux.h>
d41af627 30#include <linux/pinctrl/pinconf.h>
dbfe8ca2
LW
31/* Since we request GPIOs from ourself */
32#include <linux/pinctrl/consumer.h>
2ec1d359 33
adfed159
WD
34#include <asm/mach/irq.h>
35
378be066 36#include <plat/pincfg.h>
0f332861 37#include <plat/gpio-nomadik.h>
2ec1d359 38
e98ea774
LW
39#include "pinctrl-nomadik.h"
40
2ec1d359
AR
41/*
42 * The GPIO module in the Nomadik family of Systems-on-Chip is an
43 * AMBA device, managing 32 pins and alternate functions. The logic block
9c66ee6f 44 * is currently used in the Nomadik and ux500.
2ec1d359
AR
45 *
46 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
47 */
48
01727e61
RV
49#define NMK_GPIO_PER_CHIP 32
50
2ec1d359
AR
51struct nmk_gpio_chip {
52 struct gpio_chip chip;
a60b57ed 53 struct irq_domain *domain;
2ec1d359 54 void __iomem *addr;
af7dc228 55 struct clk *clk;
33b744b3 56 unsigned int bank;
2ec1d359 57 unsigned int parent_irq;
2c8bb0eb 58 int secondary_parent_irq;
33b744b3 59 u32 (*get_secondary_status)(unsigned int bank);
01727e61 60 void (*set_ioforce)(bool enable);
c0fcb8db 61 spinlock_t lock;
33d78647 62 bool sleepmode;
2ec1d359
AR
63 /* Keep track of configured edges */
64 u32 edge_rising;
65 u32 edge_falling;
b9df468d
RV
66 u32 real_wake;
67 u32 rwimsc;
68 u32 fwimsc;
6c12fe88
RV
69 u32 rimsc;
70 u32 fimsc;
bc6f5cf6 71 u32 pull_up;
ebc6178d 72 u32 lowemi;
2ec1d359
AR
73};
74
e98ea774
LW
75struct nmk_pinctrl {
76 struct device *dev;
77 struct pinctrl_dev *pctl;
78 const struct nmk_pinctrl_soc_data *soc;
79};
80
01727e61
RV
81static struct nmk_gpio_chip *
82nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
83
84static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
85
86#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
87
6f9a974c
RV
88static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
89 unsigned offset, int gpio_mode)
90{
91 u32 bit = 1 << offset;
92 u32 afunc, bfunc;
93
94 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
95 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
96 if (gpio_mode & NMK_GPIO_ALT_A)
97 afunc |= bit;
98 if (gpio_mode & NMK_GPIO_ALT_B)
99 bfunc |= bit;
100 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
101 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
102}
103
81a3c298
RV
104static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
105 unsigned offset, enum nmk_gpio_slpm mode)
106{
107 u32 bit = 1 << offset;
108 u32 slpm;
109
110 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
111 if (mode == NMK_GPIO_SLPM_NOCHANGE)
112 slpm |= bit;
113 else
114 slpm &= ~bit;
115 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
116}
117
5b327edf
RV
118static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
119 unsigned offset, enum nmk_gpio_pull pull)
120{
121 u32 bit = 1 << offset;
122 u32 pdis;
123
124 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
bc6f5cf6 125 if (pull == NMK_GPIO_PULL_NONE) {
5b327edf 126 pdis |= bit;
bc6f5cf6
RA
127 nmk_chip->pull_up &= ~bit;
128 } else {
5b327edf 129 pdis &= ~bit;
bc6f5cf6
RA
130 }
131
5b327edf
RV
132 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
133
bc6f5cf6
RA
134 if (pull == NMK_GPIO_PULL_UP) {
135 nmk_chip->pull_up |= bit;
5b327edf 136 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
bc6f5cf6
RA
137 } else if (pull == NMK_GPIO_PULL_DOWN) {
138 nmk_chip->pull_up &= ~bit;
5b327edf 139 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
bc6f5cf6 140 }
5b327edf
RV
141}
142
ebc6178d
RV
143static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
144 unsigned offset, bool lowemi)
145{
146 u32 bit = BIT(offset);
147 bool enabled = nmk_chip->lowemi & bit;
148
149 if (lowemi == enabled)
150 return;
151
152 if (lowemi)
153 nmk_chip->lowemi |= bit;
154 else
155 nmk_chip->lowemi &= ~bit;
156
157 writel_relaxed(nmk_chip->lowemi,
158 nmk_chip->addr + NMK_GPIO_LOWEMI);
159}
160
378be066
RV
161static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
162 unsigned offset)
163{
164 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
165}
166
6720db7c
RV
167static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
168 unsigned offset, int val)
169{
170 if (val)
171 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
172 else
173 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
174}
175
176static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
177 unsigned offset, int val)
178{
179 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
180 __nmk_gpio_set_output(nmk_chip, offset, val);
181}
182
01727e61
RV
183static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
184 unsigned offset, int gpio_mode,
185 bool glitch)
186{
6c12fe88
RV
187 u32 rwimsc = nmk_chip->rwimsc;
188 u32 fwimsc = nmk_chip->fwimsc;
01727e61
RV
189
190 if (glitch && nmk_chip->set_ioforce) {
191 u32 bit = BIT(offset);
192
01727e61
RV
193 /* Prevent spurious wakeups */
194 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
195 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
196
197 nmk_chip->set_ioforce(true);
198 }
199
200 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
201
202 if (glitch && nmk_chip->set_ioforce) {
203 nmk_chip->set_ioforce(false);
204
205 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
206 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
207 }
208}
209
6c42ad1c
RV
210static void
211nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
212{
213 u32 falling = nmk_chip->fimsc & BIT(offset);
214 u32 rising = nmk_chip->rimsc & BIT(offset);
215 int gpio = nmk_chip->chip.base + offset;
216 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
217 struct irq_data *d = irq_get_irq_data(irq);
218
219 if (!rising && !falling)
220 return;
221
222 if (!d || !irqd_irq_disabled(d))
223 return;
224
225 if (rising) {
226 nmk_chip->rimsc &= ~BIT(offset);
227 writel_relaxed(nmk_chip->rimsc,
228 nmk_chip->addr + NMK_GPIO_RIMSC);
229 }
230
231 if (falling) {
232 nmk_chip->fimsc &= ~BIT(offset);
233 writel_relaxed(nmk_chip->fimsc,
234 nmk_chip->addr + NMK_GPIO_FIMSC);
235 }
236
237 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
238}
239
378be066 240static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
01727e61 241 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
378be066
RV
242{
243 static const char *afnames[] = {
244 [NMK_GPIO_ALT_GPIO] = "GPIO",
245 [NMK_GPIO_ALT_A] = "A",
246 [NMK_GPIO_ALT_B] = "B",
247 [NMK_GPIO_ALT_C] = "C"
248 };
249 static const char *pullnames[] = {
250 [NMK_GPIO_PULL_NONE] = "none",
251 [NMK_GPIO_PULL_UP] = "up",
252 [NMK_GPIO_PULL_DOWN] = "down",
253 [3] /* illegal */ = "??"
254 };
255 static const char *slpmnames[] = {
7e3f7e59
RV
256 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
257 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
378be066
RV
258 };
259
260 int pin = PIN_NUM(cfg);
261 int pull = PIN_PULL(cfg);
262 int af = PIN_ALT(cfg);
263 int slpm = PIN_SLPM(cfg);
6720db7c
RV
264 int output = PIN_DIR(cfg);
265 int val = PIN_VAL(cfg);
01727e61 266 bool glitch = af == NMK_GPIO_ALT_C;
378be066 267
dacdc96c
RV
268 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
269 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
6720db7c
RV
270 output ? "output " : "input",
271 output ? (val ? "high" : "low") : "");
272
dacdc96c
RV
273 if (sleep) {
274 int slpm_pull = PIN_SLPM_PULL(cfg);
275 int slpm_output = PIN_SLPM_DIR(cfg);
276 int slpm_val = PIN_SLPM_VAL(cfg);
277
3546d15c
RV
278 af = NMK_GPIO_ALT_GPIO;
279
dacdc96c
RV
280 /*
281 * The SLPM_* values are normal values + 1 to allow zero to
282 * mean "same as normal".
283 */
284 if (slpm_pull)
285 pull = slpm_pull - 1;
286 if (slpm_output)
287 output = slpm_output - 1;
288 if (slpm_val)
289 val = slpm_val - 1;
290
291 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
292 pin,
293 slpm_pull ? pullnames[pull] : "same",
294 slpm_output ? (output ? "output" : "input") : "same",
295 slpm_val ? (val ? "high" : "low") : "same");
296 }
297
6720db7c
RV
298 if (output)
299 __nmk_gpio_make_output(nmk_chip, offset, val);
300 else {
301 __nmk_gpio_make_input(nmk_chip, offset);
302 __nmk_gpio_set_pull(nmk_chip, offset, pull);
303 }
378be066 304
ebc6178d
RV
305 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
306
6c42ad1c
RV
307 /*
308 * If the pin is switching to altfunc, and there was an interrupt
309 * installed on it which has been lazy disabled, actually mask the
310 * interrupt to prevent spurious interrupts that would occur while the
311 * pin is under control of the peripheral. Only SKE does this.
312 */
313 if (af != NMK_GPIO_ALT_GPIO)
314 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
315
01727e61
RV
316 /*
317 * If we've backed up the SLPM registers (glitch workaround), modify
318 * the backups since they will be restored.
319 */
320 if (slpmregs) {
321 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
322 slpmregs[nmk_chip->bank] |= BIT(offset);
323 else
324 slpmregs[nmk_chip->bank] &= ~BIT(offset);
325 } else
326 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
327
328 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
329}
330
331/*
332 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
333 * - Save SLPM registers
334 * - Set SLPM=0 for the IOs you want to switch and others to 1
335 * - Configure the GPIO registers for the IOs that are being switched
336 * - Set IOFORCE=1
337 * - Modify the AFLSA/B registers for the IOs that are being switched
338 * - Set IOFORCE=0
339 * - Restore SLPM registers
340 * - Any spurious wake up event during switch sequence to be ignored and
341 * cleared
342 */
343static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
344{
345 int i;
346
347 for (i = 0; i < NUM_BANKS; i++) {
348 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
349 unsigned int temp = slpm[i];
350
351 if (!chip)
352 break;
353
3c0227d2
RV
354 clk_enable(chip->clk);
355
01727e61
RV
356 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
357 writel(temp, chip->addr + NMK_GPIO_SLPC);
358 }
359}
360
361static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
362{
363 int i;
364
365 for (i = 0; i < NUM_BANKS; i++) {
366 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
367
368 if (!chip)
369 break;
370
371 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
3c0227d2
RV
372
373 clk_disable(chip->clk);
01727e61
RV
374 }
375}
376
377static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
378{
379 static unsigned int slpm[NUM_BANKS];
380 unsigned long flags;
381 bool glitch = false;
382 int ret = 0;
383 int i;
384
385 for (i = 0; i < num; i++) {
386 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
387 glitch = true;
388 break;
389 }
390 }
391
392 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
393
394 if (glitch) {
395 memset(slpm, 0xff, sizeof(slpm));
396
397 for (i = 0; i < num; i++) {
398 int pin = PIN_NUM(cfgs[i]);
399 int offset = pin % NMK_GPIO_PER_CHIP;
400
401 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
402 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
403 }
404
405 nmk_gpio_glitch_slpm_init(slpm);
406 }
407
408 for (i = 0; i < num; i++) {
409 struct nmk_gpio_chip *nmk_chip;
410 int pin = PIN_NUM(cfgs[i]);
411
a60b57ed 412 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
01727e61
RV
413 if (!nmk_chip) {
414 ret = -EINVAL;
415 break;
416 }
417
3c0227d2 418 clk_enable(nmk_chip->clk);
01727e61 419 spin_lock(&nmk_chip->lock);
a60b57ed 420 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
01727e61
RV
421 cfgs[i], sleep, glitch ? slpm : NULL);
422 spin_unlock(&nmk_chip->lock);
3c0227d2 423 clk_disable(nmk_chip->clk);
01727e61
RV
424 }
425
426 if (glitch)
427 nmk_gpio_glitch_slpm_restore(slpm);
428
429 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
430
431 return ret;
378be066
RV
432}
433
434/**
435 * nmk_config_pin - configure a pin's mux attributes
436 * @cfg: pin confguration
50bcd47c 437 * @sleep: Non-zero to apply the sleep mode configuration
378be066
RV
438 * Configures a pin's mode (alternate function or GPIO), its pull up status,
439 * and its sleep mode based on the specified configuration. The @cfg is
440 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
441 * are constructed using, and can be further enhanced with, the macros in
442 * plat/pincfg.h.
443 *
444 * If a pin's mode is set to GPIO, it is configured as an input to avoid
445 * side-effects. The gpio can be manipulated later using standard GPIO API
446 * calls.
447 */
dacdc96c 448int nmk_config_pin(pin_cfg_t cfg, bool sleep)
378be066 449{
01727e61 450 return __nmk_config_pins(&cfg, 1, sleep);
378be066
RV
451}
452EXPORT_SYMBOL(nmk_config_pin);
453
454/**
455 * nmk_config_pins - configure several pins at once
456 * @cfgs: array of pin configurations
457 * @num: number of elments in the array
458 *
459 * Configures several pins using nmk_config_pin(). Refer to that function for
460 * further information.
461 */
462int nmk_config_pins(pin_cfg_t *cfgs, int num)
463{
01727e61 464 return __nmk_config_pins(cfgs, num, false);
378be066
RV
465}
466EXPORT_SYMBOL(nmk_config_pins);
467
dacdc96c
RV
468int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
469{
01727e61 470 return __nmk_config_pins(cfgs, num, true);
dacdc96c
RV
471}
472EXPORT_SYMBOL(nmk_config_pins_sleep);
473
81a3c298
RV
474/**
475 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
476 * @gpio: pin number
477 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
478 *
33d78647
LW
479 * This register is actually in the pinmux layer, not the GPIO block itself.
480 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
481 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
482 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
483 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
484 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
485 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
7e3f7e59 486 *
33d78647
LW
487 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
488 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
489 * entered) regardless of the altfunction selected. Also wake-up detection is
490 * ENABLED.
491 *
492 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
493 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
494 * (for altfunction GPIO) or respective on-chip peripherals (for other
495 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
496 *
497 * Note that enable_irq_wake() will automatically enable wakeup detection.
81a3c298
RV
498 */
499int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
500{
501 struct nmk_gpio_chip *nmk_chip;
502 unsigned long flags;
503
a60b57ed 504 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
81a3c298
RV
505 if (!nmk_chip)
506 return -EINVAL;
507
3c0227d2 508 clk_enable(nmk_chip->clk);
01727e61
RV
509 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
510 spin_lock(&nmk_chip->lock);
511
a60b57ed 512 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
01727e61
RV
513
514 spin_unlock(&nmk_chip->lock);
515 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 516 clk_disable(nmk_chip->clk);
81a3c298
RV
517
518 return 0;
519}
520
5b327edf
RV
521/**
522 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
523 * @gpio: pin number
524 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
525 *
526 * Enables/disables pull up/down on a specified pin. This only takes effect if
527 * the pin is configured as an input (either explicitly or by the alternate
528 * function).
529 *
530 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
531 * configured as an input. Otherwise, due to the way the controller registers
532 * work, this function will change the value output on the pin.
533 */
534int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
535{
536 struct nmk_gpio_chip *nmk_chip;
537 unsigned long flags;
538
a60b57ed 539 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
5b327edf
RV
540 if (!nmk_chip)
541 return -EINVAL;
542
3c0227d2 543 clk_enable(nmk_chip->clk);
5b327edf 544 spin_lock_irqsave(&nmk_chip->lock, flags);
a60b57ed 545 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
5b327edf 546 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 547 clk_disable(nmk_chip->clk);
5b327edf
RV
548
549 return 0;
550}
551
2ec1d359 552/* Mode functions */
9c66ee6f
JA
553/**
554 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
555 * @gpio: pin number
556 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
557 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
558 *
559 * Sets the mode of the specified pin to one of the alternate functions or
560 * plain GPIO.
561 */
2ec1d359
AR
562int nmk_gpio_set_mode(int gpio, int gpio_mode)
563{
564 struct nmk_gpio_chip *nmk_chip;
565 unsigned long flags;
2ec1d359 566
a60b57ed 567 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
2ec1d359
AR
568 if (!nmk_chip)
569 return -EINVAL;
570
3c0227d2 571 clk_enable(nmk_chip->clk);
2ec1d359 572 spin_lock_irqsave(&nmk_chip->lock, flags);
a60b57ed 573 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
2ec1d359 574 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 575 clk_disable(nmk_chip->clk);
2ec1d359
AR
576
577 return 0;
578}
579EXPORT_SYMBOL(nmk_gpio_set_mode);
580
581int nmk_gpio_get_mode(int gpio)
582{
583 struct nmk_gpio_chip *nmk_chip;
584 u32 afunc, bfunc, bit;
585
a60b57ed 586 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
2ec1d359
AR
587 if (!nmk_chip)
588 return -EINVAL;
589
a60b57ed 590 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
2ec1d359 591
3c0227d2
RV
592 clk_enable(nmk_chip->clk);
593
2ec1d359
AR
594 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
595 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
596
3c0227d2
RV
597 clk_disable(nmk_chip->clk);
598
2ec1d359
AR
599 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
600}
601EXPORT_SYMBOL(nmk_gpio_get_mode);
602
603
604/* IRQ functions */
605static inline int nmk_gpio_get_bitmask(int gpio)
606{
a60b57ed 607 return 1 << (gpio % NMK_GPIO_PER_CHIP);
2ec1d359
AR
608}
609
f272c00e 610static void nmk_gpio_irq_ack(struct irq_data *d)
2ec1d359 611{
2ec1d359
AR
612 struct nmk_gpio_chip *nmk_chip;
613
f272c00e 614 nmk_chip = irq_data_get_irq_chip_data(d);
2ec1d359
AR
615 if (!nmk_chip)
616 return;
3c0227d2
RV
617
618 clk_enable(nmk_chip->clk);
a60b57ed 619 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
3c0227d2 620 clk_disable(nmk_chip->clk);
2ec1d359
AR
621}
622
4d4e20f7
RV
623enum nmk_gpio_irq_type {
624 NORMAL,
625 WAKE,
626};
627
040e5ecd 628static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
4d4e20f7
RV
629 int gpio, enum nmk_gpio_irq_type which,
630 bool enable)
2ec1d359 631{
040e5ecd 632 u32 bitmask = nmk_gpio_get_bitmask(gpio);
6c12fe88
RV
633 u32 *rimscval;
634 u32 *fimscval;
635 u32 rimscreg;
636 u32 fimscreg;
637
638 if (which == NORMAL) {
639 rimscreg = NMK_GPIO_RIMSC;
640 fimscreg = NMK_GPIO_FIMSC;
641 rimscval = &nmk_chip->rimsc;
642 fimscval = &nmk_chip->fimsc;
643 } else {
644 rimscreg = NMK_GPIO_RWIMSC;
645 fimscreg = NMK_GPIO_FWIMSC;
646 rimscval = &nmk_chip->rwimsc;
647 fimscval = &nmk_chip->fwimsc;
648 }
2ec1d359 649
040e5ecd 650 /* we must individually set/clear the two edges */
2ec1d359 651 if (nmk_chip->edge_rising & bitmask) {
040e5ecd 652 if (enable)
6c12fe88 653 *rimscval |= bitmask;
040e5ecd 654 else
6c12fe88
RV
655 *rimscval &= ~bitmask;
656 writel(*rimscval, nmk_chip->addr + rimscreg);
2ec1d359
AR
657 }
658 if (nmk_chip->edge_falling & bitmask) {
040e5ecd 659 if (enable)
6c12fe88 660 *fimscval |= bitmask;
040e5ecd 661 else
6c12fe88
RV
662 *fimscval &= ~bitmask;
663 writel(*fimscval, nmk_chip->addr + fimscreg);
2ec1d359 664 }
040e5ecd 665}
2ec1d359 666
b9df468d
RV
667static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
668 int gpio, bool on)
669{
b982ff0e
RV
670 /*
671 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
672 * disabled, since setting SLPM to 1 increases power consumption, and
673 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
674 */
675 if (nmk_chip->sleepmode && on) {
e85bbc19 676 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
b982ff0e 677 NMK_GPIO_SLPM_WAKEUP_ENABLE);
33d78647
LW
678 }
679
b9df468d
RV
680 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
681}
682
683static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
2ec1d359 684{
2ec1d359
AR
685 struct nmk_gpio_chip *nmk_chip;
686 unsigned long flags;
040e5ecd 687 u32 bitmask;
2ec1d359 688
f272c00e 689 nmk_chip = irq_data_get_irq_chip_data(d);
a60b57ed 690 bitmask = nmk_gpio_get_bitmask(d->hwirq);
2ec1d359 691 if (!nmk_chip)
4d4e20f7 692 return -EINVAL;
2ec1d359 693
3c0227d2 694 clk_enable(nmk_chip->clk);
b9df468d
RV
695 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
696 spin_lock(&nmk_chip->lock);
697
a60b57ed 698 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
b9df468d
RV
699
700 if (!(nmk_chip->real_wake & bitmask))
a60b57ed 701 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
b9df468d
RV
702
703 spin_unlock(&nmk_chip->lock);
704 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 705 clk_disable(nmk_chip->clk);
4d4e20f7
RV
706
707 return 0;
2ec1d359
AR
708}
709
f272c00e 710static void nmk_gpio_irq_mask(struct irq_data *d)
040e5ecd 711{
b9df468d 712 nmk_gpio_irq_maskunmask(d, false);
4d4e20f7 713}
040e5ecd 714
f272c00e 715static void nmk_gpio_irq_unmask(struct irq_data *d)
040e5ecd 716{
b9df468d 717 nmk_gpio_irq_maskunmask(d, true);
4d4e20f7
RV
718}
719
f272c00e 720static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
4d4e20f7 721{
7e3f7e59
RV
722 struct nmk_gpio_chip *nmk_chip;
723 unsigned long flags;
b9df468d 724 u32 bitmask;
7e3f7e59 725
f272c00e 726 nmk_chip = irq_data_get_irq_chip_data(d);
7e3f7e59
RV
727 if (!nmk_chip)
728 return -EINVAL;
a60b57ed 729 bitmask = nmk_gpio_get_bitmask(d->hwirq);
7e3f7e59 730
3c0227d2 731 clk_enable(nmk_chip->clk);
01727e61
RV
732 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
733 spin_lock(&nmk_chip->lock);
734
479a0c7e 735 if (irqd_irq_disabled(d))
a60b57ed 736 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
b9df468d
RV
737
738 if (on)
739 nmk_chip->real_wake |= bitmask;
740 else
741 nmk_chip->real_wake &= ~bitmask;
01727e61
RV
742
743 spin_unlock(&nmk_chip->lock);
744 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 745 clk_disable(nmk_chip->clk);
7e3f7e59
RV
746
747 return 0;
040e5ecd
RV
748}
749
f272c00e 750static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
2ec1d359 751{
479a0c7e 752 bool enabled = !irqd_irq_disabled(d);
3c0227d2 753 bool wake = irqd_is_wakeup_set(d);
2ec1d359
AR
754 struct nmk_gpio_chip *nmk_chip;
755 unsigned long flags;
756 u32 bitmask;
757
f272c00e 758 nmk_chip = irq_data_get_irq_chip_data(d);
a60b57ed 759 bitmask = nmk_gpio_get_bitmask(d->hwirq);
2ec1d359
AR
760 if (!nmk_chip)
761 return -EINVAL;
2ec1d359
AR
762 if (type & IRQ_TYPE_LEVEL_HIGH)
763 return -EINVAL;
764 if (type & IRQ_TYPE_LEVEL_LOW)
765 return -EINVAL;
766
3c0227d2 767 clk_enable(nmk_chip->clk);
2ec1d359
AR
768 spin_lock_irqsave(&nmk_chip->lock, flags);
769
7a852d80 770 if (enabled)
a60b57ed 771 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
4d4e20f7 772
b9df468d 773 if (enabled || wake)
a60b57ed 774 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
7a852d80 775
2ec1d359
AR
776 nmk_chip->edge_rising &= ~bitmask;
777 if (type & IRQ_TYPE_EDGE_RISING)
778 nmk_chip->edge_rising |= bitmask;
2ec1d359
AR
779
780 nmk_chip->edge_falling &= ~bitmask;
781 if (type & IRQ_TYPE_EDGE_FALLING)
782 nmk_chip->edge_falling |= bitmask;
2ec1d359 783
7a852d80 784 if (enabled)
a60b57ed 785 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
4d4e20f7 786
b9df468d 787 if (enabled || wake)
a60b57ed 788 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
2ec1d359 789
7a852d80 790 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 791 clk_disable(nmk_chip->clk);
2ec1d359
AR
792
793 return 0;
794}
795
3c0227d2
RV
796static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
797{
798 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
2ec1d359 799
3c0227d2
RV
800 clk_enable(nmk_chip->clk);
801 nmk_gpio_irq_unmask(d);
2ec1d359
AR
802 return 0;
803}
804
3c0227d2
RV
805static void nmk_gpio_irq_shutdown(struct irq_data *d)
806{
807 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
808
809 nmk_gpio_irq_mask(d);
810 clk_disable(nmk_chip->clk);
811}
812
2ec1d359
AR
813static struct irq_chip nmk_gpio_irq_chip = {
814 .name = "Nomadik-GPIO",
f272c00e
LB
815 .irq_ack = nmk_gpio_irq_ack,
816 .irq_mask = nmk_gpio_irq_mask,
817 .irq_unmask = nmk_gpio_irq_unmask,
818 .irq_set_type = nmk_gpio_irq_set_type,
819 .irq_set_wake = nmk_gpio_irq_set_wake,
3c0227d2
RV
820 .irq_startup = nmk_gpio_irq_startup,
821 .irq_shutdown = nmk_gpio_irq_shutdown,
4921e745 822 .flags = IRQCHIP_MASK_ON_SUSPEND,
2ec1d359
AR
823};
824
33b744b3
RV
825static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
826 u32 status)
2ec1d359
AR
827{
828 struct nmk_gpio_chip *nmk_chip;
6845664a 829 struct irq_chip *host_chip = irq_get_chip(irq);
2ec1d359
AR
830 unsigned int first_irq;
831
adfed159 832 chained_irq_enter(host_chip, desc);
aaedaa2b 833
6845664a 834 nmk_chip = irq_get_handler_data(irq);
a60b57ed 835 first_irq = nmk_chip->domain->revmap_data.legacy.first_irq;
33b744b3
RV
836 while (status) {
837 int bit = __ffs(status);
838
839 generic_handle_irq(first_irq + bit);
840 status &= ~BIT(bit);
2ec1d359 841 }
aaedaa2b 842
adfed159 843 chained_irq_exit(host_chip, desc);
2ec1d359
AR
844}
845
33b744b3
RV
846static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
847{
6845664a 848 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
3c0227d2
RV
849 u32 status;
850
851 clk_enable(nmk_chip->clk);
852 status = readl(nmk_chip->addr + NMK_GPIO_IS);
853 clk_disable(nmk_chip->clk);
33b744b3
RV
854
855 __nmk_gpio_irq_handler(irq, desc, status);
856}
857
858static void nmk_gpio_secondary_irq_handler(unsigned int irq,
859 struct irq_desc *desc)
860{
6845664a 861 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
33b744b3
RV
862 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
863
864 __nmk_gpio_irq_handler(irq, desc, status);
865}
866
2ec1d359
AR
867static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
868{
6845664a
TG
869 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
870 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
33b744b3
RV
871
872 if (nmk_chip->secondary_parent_irq >= 0) {
6845664a 873 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
33b744b3 874 nmk_gpio_secondary_irq_handler);
6845664a 875 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
33b744b3
RV
876 }
877
2ec1d359
AR
878 return 0;
879}
880
881/* I/O Functions */
dbfe8ca2
LW
882
883static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
884{
885 /*
886 * Map back to global GPIO space and request muxing, the direction
887 * parameter does not matter for this controller.
888 */
889 int gpio = chip->base + offset;
890
891 return pinctrl_request_gpio(gpio);
892}
893
894static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
895{
896 int gpio = chip->base + offset;
897
898 pinctrl_free_gpio(gpio);
899}
900
2ec1d359
AR
901static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
902{
903 struct nmk_gpio_chip *nmk_chip =
904 container_of(chip, struct nmk_gpio_chip, chip);
905
3c0227d2
RV
906 clk_enable(nmk_chip->clk);
907
2ec1d359 908 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
3c0227d2
RV
909
910 clk_disable(nmk_chip->clk);
911
2ec1d359
AR
912 return 0;
913}
914
2ec1d359
AR
915static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
916{
917 struct nmk_gpio_chip *nmk_chip =
918 container_of(chip, struct nmk_gpio_chip, chip);
919 u32 bit = 1 << offset;
3c0227d2
RV
920 int value;
921
922 clk_enable(nmk_chip->clk);
2ec1d359 923
3c0227d2 924 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
2ec1d359 925
3c0227d2
RV
926 clk_disable(nmk_chip->clk);
927
928 return value;
2ec1d359
AR
929}
930
931static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
932 int val)
933{
934 struct nmk_gpio_chip *nmk_chip =
935 container_of(chip, struct nmk_gpio_chip, chip);
2ec1d359 936
3c0227d2
RV
937 clk_enable(nmk_chip->clk);
938
6720db7c 939 __nmk_gpio_set_output(nmk_chip, offset, val);
3c0227d2
RV
940
941 clk_disable(nmk_chip->clk);
2ec1d359
AR
942}
943
6647c6c0
RV
944static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
945 int val)
946{
947 struct nmk_gpio_chip *nmk_chip =
948 container_of(chip, struct nmk_gpio_chip, chip);
949
3c0227d2
RV
950 clk_enable(nmk_chip->clk);
951
6720db7c 952 __nmk_gpio_make_output(nmk_chip, offset, val);
6647c6c0 953
3c0227d2
RV
954 clk_disable(nmk_chip->clk);
955
6647c6c0
RV
956 return 0;
957}
958
0d2aec9c
RV
959static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
960{
961 struct nmk_gpio_chip *nmk_chip =
962 container_of(chip, struct nmk_gpio_chip, chip);
963
a60b57ed 964 return irq_find_mapping(nmk_chip->domain, offset);
0d2aec9c
RV
965}
966
d0b543c7
RV
967#ifdef CONFIG_DEBUG_FS
968
969#include <linux/seq_file.h>
970
6f4350a6
LW
971static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
972 unsigned offset, unsigned gpio)
d0b543c7 973{
6f4350a6 974 const char *label = gpiochip_is_requested(chip, offset);
d0b543c7
RV
975 struct nmk_gpio_chip *nmk_chip =
976 container_of(chip, struct nmk_gpio_chip, chip);
6f4350a6
LW
977 int mode;
978 bool is_out;
979 bool pull;
980 u32 bit = 1 << offset;
d0b543c7
RV
981 const char *modes[] = {
982 [NMK_GPIO_ALT_GPIO] = "gpio",
983 [NMK_GPIO_ALT_A] = "altA",
984 [NMK_GPIO_ALT_B] = "altB",
985 [NMK_GPIO_ALT_C] = "altC",
986 };
987
3c0227d2 988 clk_enable(nmk_chip->clk);
6f4350a6
LW
989 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
990 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
991 mode = nmk_gpio_get_mode(gpio);
992
993 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
994 gpio, label ?: "(none)",
995 is_out ? "out" : "in ",
996 chip->get
997 ? (chip->get(chip, offset) ? "hi" : "lo")
998 : "? ",
999 (mode < 0) ? "unknown" : modes[mode],
1000 pull ? "pull" : "none");
1001
1002 if (label && !is_out) {
1003 int irq = gpio_to_irq(gpio);
1004 struct irq_desc *desc = irq_to_desc(irq);
1005
1006 /* This races with request_irq(), set_irq_type(),
1007 * and set_irq_wake() ... but those are "rare".
1008 */
1009 if (irq >= 0 && desc->action) {
1010 char *trigger;
1011 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1012
1013 if (nmk_chip->edge_rising & bitmask)
1014 trigger = "edge-rising";
1015 else if (nmk_chip->edge_falling & bitmask)
1016 trigger = "edge-falling";
1017 else
1018 trigger = "edge-undefined";
1019
1020 seq_printf(s, " irq-%d %s%s",
1021 irq, trigger,
1022 irqd_is_wakeup_set(&desc->irq_data)
1023 ? " wakeup" : "");
8ea72a30 1024 }
6f4350a6
LW
1025 }
1026 clk_disable(nmk_chip->clk);
1027}
1028
1029static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1030{
1031 unsigned i;
1032 unsigned gpio = chip->base;
8ea72a30 1033
6f4350a6
LW
1034 for (i = 0; i < chip->ngpio; i++, gpio++) {
1035 nmk_gpio_dbg_show_one(s, chip, i, gpio);
d0b543c7
RV
1036 seq_printf(s, "\n");
1037 }
1038}
1039
1040#else
6f4350a6
LW
1041static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1042 struct gpio_chip *chip,
1043 unsigned offset, unsigned gpio)
1044{
1045}
d0b543c7
RV
1046#define nmk_gpio_dbg_show NULL
1047#endif
1048
2ec1d359
AR
1049/* This structure is replicated for each GPIO block allocated at probe time */
1050static struct gpio_chip nmk_gpio_template = {
dbfe8ca2
LW
1051 .request = nmk_gpio_request,
1052 .free = nmk_gpio_free,
2ec1d359
AR
1053 .direction_input = nmk_gpio_make_input,
1054 .get = nmk_gpio_get_input,
1055 .direction_output = nmk_gpio_make_output,
1056 .set = nmk_gpio_set_output,
0d2aec9c 1057 .to_irq = nmk_gpio_to_irq,
d0b543c7 1058 .dbg_show = nmk_gpio_dbg_show,
2ec1d359
AR
1059 .can_sleep = 0,
1060};
1061
3c0227d2
RV
1062void nmk_gpio_clocks_enable(void)
1063{
1064 int i;
1065
1066 for (i = 0; i < NUM_BANKS; i++) {
1067 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1068
1069 if (!chip)
1070 continue;
1071
1072 clk_enable(chip->clk);
1073 }
1074}
1075
1076void nmk_gpio_clocks_disable(void)
1077{
1078 int i;
1079
1080 for (i = 0; i < NUM_BANKS; i++) {
1081 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1082
1083 if (!chip)
1084 continue;
1085
1086 clk_disable(chip->clk);
1087 }
1088}
1089
b9df468d
RV
1090/*
1091 * Called from the suspend/resume path to only keep the real wakeup interrupts
1092 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1093 * and not the rest of the interrupts which we needed to have as wakeups for
1094 * cpuidle.
1095 *
1096 * PM ops are not used since this needs to be done at the end, after all the
1097 * other drivers are done with their suspend callbacks.
1098 */
1099void nmk_gpio_wakeups_suspend(void)
1100{
1101 int i;
1102
1103 for (i = 0; i < NUM_BANKS; i++) {
1104 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1105
1106 if (!chip)
1107 break;
1108
3c0227d2
RV
1109 clk_enable(chip->clk);
1110
b9df468d
RV
1111 writel(chip->rwimsc & chip->real_wake,
1112 chip->addr + NMK_GPIO_RWIMSC);
1113 writel(chip->fwimsc & chip->real_wake,
1114 chip->addr + NMK_GPIO_FWIMSC);
1115
3c0227d2 1116 clk_disable(chip->clk);
b9df468d
RV
1117 }
1118}
1119
1120void nmk_gpio_wakeups_resume(void)
1121{
1122 int i;
1123
1124 for (i = 0; i < NUM_BANKS; i++) {
1125 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1126
1127 if (!chip)
1128 break;
1129
3c0227d2
RV
1130 clk_enable(chip->clk);
1131
b9df468d
RV
1132 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1133 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1134
3c0227d2 1135 clk_disable(chip->clk);
b9df468d
RV
1136 }
1137}
1138
bc6f5cf6
RA
1139/*
1140 * Read the pull up/pull down status.
1141 * A bit set in 'pull_up' means that pull up
1142 * is selected if pull is enabled in PDIS register.
1143 * Note: only pull up/down set via this driver can
1144 * be detected due to HW limitations.
1145 */
1146void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1147{
1148 if (gpio_bank < NUM_BANKS) {
1149 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1150
1151 if (!chip)
1152 return;
1153
1154 *pull_up = chip->pull_up;
1155 }
1156}
1157
a60b57ed
LJ
1158int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1159 irq_hw_number_t hwirq)
1160{
1161 struct nmk_gpio_chip *nmk_chip = d->host_data;
1162
1163 if (!nmk_chip)
1164 return -EINVAL;
1165
1166 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1167 set_irq_flags(irq, IRQF_VALID);
1168 irq_set_chip_data(irq, nmk_chip);
1169 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1170
1171 return 0;
1172}
1173
1174const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1175 .map = nmk_gpio_irq_map,
1176 .xlate = irq_domain_xlate_twocell,
1177};
1178
fd0d67d6 1179static int __devinit nmk_gpio_probe(struct platform_device *dev)
2ec1d359 1180{
3e3c62ca 1181 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
513c27f8 1182 struct device_node *np = dev->dev.of_node;
2ec1d359
AR
1183 struct nmk_gpio_chip *nmk_chip;
1184 struct gpio_chip *chip;
3e3c62ca 1185 struct resource *res;
af7dc228 1186 struct clk *clk;
33b744b3 1187 int secondary_irq;
8d91771c 1188 void __iomem *base;
3e3c62ca 1189 int irq;
2ec1d359
AR
1190 int ret;
1191
513c27f8
LJ
1192 if (!pdata && !np) {
1193 dev_err(&dev->dev, "No platform data or device tree found\n");
3e3c62ca 1194 return -ENODEV;
513c27f8
LJ
1195 }
1196
1197 if (np) {
5e754f33 1198 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
513c27f8
LJ
1199 if (!pdata)
1200 return -ENOMEM;
1201
612e1d5f 1202 if (of_get_property(np, "st,supports-sleepmode", NULL))
513c27f8
LJ
1203 pdata->supports_sleepmode = true;
1204
1205 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1206 dev_err(&dev->dev, "gpio-bank property not found\n");
1207 ret = -EINVAL;
a60b57ed 1208 goto out;
513c27f8
LJ
1209 }
1210
1211 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1212 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1213 }
3e3c62ca
RV
1214
1215 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1216 if (!res) {
1217 ret = -ENOENT;
1218 goto out;
1219 }
1220
1221 irq = platform_get_irq(dev, 0);
1222 if (irq < 0) {
1223 ret = irq;
1224 goto out;
1225 }
1226
33b744b3
RV
1227 secondary_irq = platform_get_irq(dev, 1);
1228 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1229 ret = -EINVAL;
1230 goto out;
1231 }
1232
5e754f33 1233 base = devm_request_and_ioremap(&dev->dev, res);
8d91771c
LW
1234 if (!base) {
1235 ret = -ENOMEM;
5e754f33 1236 goto out;
8d91771c
LW
1237 }
1238
5e754f33 1239 clk = devm_clk_get(&dev->dev, NULL);
af7dc228
RV
1240 if (IS_ERR(clk)) {
1241 ret = PTR_ERR(clk);
5e754f33 1242 goto out;
af7dc228 1243 }
efec381c 1244 clk_prepare(clk);
af7dc228 1245
5e754f33 1246 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
2ec1d359
AR
1247 if (!nmk_chip) {
1248 ret = -ENOMEM;
5e754f33 1249 goto out;
2ec1d359 1250 }
513c27f8 1251
2ec1d359
AR
1252 /*
1253 * The virt address in nmk_chip->addr is in the nomadik register space,
1254 * so we can simply convert the resource address, without remapping
1255 */
33b744b3 1256 nmk_chip->bank = dev->id;
af7dc228 1257 nmk_chip->clk = clk;
8d91771c 1258 nmk_chip->addr = base;
2ec1d359 1259 nmk_chip->chip = nmk_gpio_template;
3e3c62ca 1260 nmk_chip->parent_irq = irq;
33b744b3
RV
1261 nmk_chip->secondary_parent_irq = secondary_irq;
1262 nmk_chip->get_secondary_status = pdata->get_secondary_status;
01727e61 1263 nmk_chip->set_ioforce = pdata->set_ioforce;
33d78647 1264 nmk_chip->sleepmode = pdata->supports_sleepmode;
c0fcb8db 1265 spin_lock_init(&nmk_chip->lock);
2ec1d359
AR
1266
1267 chip = &nmk_chip->chip;
1268 chip->base = pdata->first_gpio;
e493e06f 1269 chip->ngpio = pdata->num_gpio;
8d568ae5 1270 chip->label = pdata->name ?: dev_name(&dev->dev);
2ec1d359
AR
1271 chip->dev = &dev->dev;
1272 chip->owner = THIS_MODULE;
1273
ebc6178d
RV
1274 clk_enable(nmk_chip->clk);
1275 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1276 clk_disable(nmk_chip->clk);
1277
072e82a1 1278#ifdef CONFIG_OF_GPIO
513c27f8 1279 chip->of_node = np;
072e82a1 1280#endif
513c27f8 1281
2ec1d359
AR
1282 ret = gpiochip_add(&nmk_chip->chip);
1283 if (ret)
5e754f33 1284 goto out;
2ec1d359 1285
01727e61
RV
1286 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1287
1288 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
513c27f8 1289
3e3c62ca 1290 platform_set_drvdata(dev, nmk_chip);
2ec1d359 1291
a60b57ed
LJ
1292 nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
1293 NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
1294 0, &nmk_gpio_irq_simple_ops, nmk_chip);
1295 if (!nmk_chip->domain) {
2ee38d4d 1296 dev_err(&dev->dev, "failed to create irqdomain\n");
a60b57ed 1297 ret = -ENOSYS;
5e754f33 1298 goto out;
a60b57ed
LJ
1299 }
1300
2ec1d359
AR
1301 nmk_gpio_init_irq(nmk_chip);
1302
513c27f8
LJ
1303 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1304
2ec1d359
AR
1305 return 0;
1306
3e3c62ca 1307out:
2ec1d359
AR
1308 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1309 pdata->first_gpio, pdata->first_gpio+31);
513c27f8 1310
2ec1d359
AR
1311 return ret;
1312}
1313
e98ea774
LW
1314static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1315{
1316 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1317
1318 return npct->soc->ngroups;
1319}
1320
1321static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1322 unsigned selector)
1323{
1324 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1325
1326 return npct->soc->groups[selector].name;
1327}
1328
1329static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1330 const unsigned **pins,
1331 unsigned *num_pins)
1332{
1333 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1334
1335 *pins = npct->soc->groups[selector].pins;
1336 *num_pins = npct->soc->groups[selector].npins;
1337 return 0;
1338}
1339
24cbdd75
LW
1340static struct pinctrl_gpio_range *
1341nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1342{
1343 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1344 int i;
1345
1346 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1347 struct pinctrl_gpio_range *range;
1348
1349 range = &npct->soc->gpio_ranges[i];
1350 if (offset >= range->pin_base &&
1351 offset <= (range->pin_base + range->npins - 1))
1352 return range;
1353 }
1354 return NULL;
1355}
1356
e98ea774
LW
1357static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1358 unsigned offset)
1359{
24cbdd75
LW
1360 struct pinctrl_gpio_range *range;
1361 struct gpio_chip *chip;
1362
1363 range = nmk_match_gpio_range(pctldev, offset);
1364 if (!range || !range->gc) {
1365 seq_printf(s, "invalid pin offset");
1366 return;
1367 }
1368 chip = range->gc;
1369 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
e98ea774
LW
1370}
1371
1372static struct pinctrl_ops nmk_pinctrl_ops = {
1373 .get_groups_count = nmk_get_groups_cnt,
1374 .get_group_name = nmk_get_group_name,
1375 .get_group_pins = nmk_get_group_pins,
1376 .pin_dbg_show = nmk_pin_dbg_show,
1377};
1378
dbfe8ca2
LW
1379static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1380{
1381 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1382
1383 return npct->soc->nfunctions;
1384}
1385
1386static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1387 unsigned function)
1388{
1389 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1390
1391 return npct->soc->functions[function].name;
1392}
1393
1394static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1395 unsigned function,
1396 const char * const **groups,
1397 unsigned * const num_groups)
1398{
1399 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1400
1401 *groups = npct->soc->functions[function].groups;
1402 *num_groups = npct->soc->functions[function].ngroups;
1403
1404 return 0;
1405}
1406
1407static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1408 unsigned group)
1409{
1410 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1411 const struct nmk_pingroup *g;
1412 static unsigned int slpm[NUM_BANKS];
1413 unsigned long flags;
1414 bool glitch;
1415 int ret = -EINVAL;
1416 int i;
1417
1418 g = &npct->soc->groups[group];
1419
1420 if (g->altsetting < 0)
1421 return -EINVAL;
1422
1423 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1424
daf73174
LW
1425 /*
1426 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1427 * we may pass through an undesired state. In this case we take
1428 * some extra care.
1429 *
1430 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1431 * - Save SLPM registers (since we have a shadow register in the
1432 * nmk_chip we're using that as backup)
1433 * - Set SLPM=0 for the IOs you want to switch and others to 1
1434 * - Configure the GPIO registers for the IOs that are being switched
1435 * - Set IOFORCE=1
1436 * - Modify the AFLSA/B registers for the IOs that are being switched
1437 * - Set IOFORCE=0
1438 * - Restore SLPM registers
1439 * - Any spurious wake up event during switch sequence to be ignored
1440 * and cleared
1441 *
1442 * We REALLY need to save ALL slpm registers, because the external
1443 * IOFORCE will switch *all* ports to their sleepmode setting to as
1444 * to avoid glitches. (Not just one port!)
1445 */
dbfe8ca2
LW
1446 glitch = (g->altsetting == NMK_GPIO_ALT_C);
1447
1448 if (glitch) {
1449 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1450
1451 /* Initially don't put any pins to sleep when switching */
1452 memset(slpm, 0xff, sizeof(slpm));
1453
1454 /*
1455 * Then mask the pins that need to be sleeping now when we're
1456 * switching to the ALT C function.
1457 */
1458 for (i = 0; i < g->npins; i++)
1459 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1460 nmk_gpio_glitch_slpm_init(slpm);
1461 }
1462
1463 for (i = 0; i < g->npins; i++) {
1464 struct pinctrl_gpio_range *range;
1465 struct nmk_gpio_chip *nmk_chip;
1466 struct gpio_chip *chip;
1467 unsigned bit;
1468
1469 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1470 if (!range) {
1471 dev_err(npct->dev,
1472 "invalid pin offset %d in group %s at index %d\n",
1473 g->pins[i], g->name, i);
1474 goto out_glitch;
1475 }
1476 if (!range->gc) {
1477 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1478 g->pins[i], g->name, i);
1479 goto out_glitch;
1480 }
1481 chip = range->gc;
1482 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1483 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1484
1485 clk_enable(nmk_chip->clk);
1486 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1487 /*
1488 * If the pin is switching to altfunc, and there was an
1489 * interrupt installed on it which has been lazy disabled,
1490 * actually mask the interrupt to prevent spurious interrupts
1491 * that would occur while the pin is under control of the
1492 * peripheral. Only SKE does this.
1493 */
1494 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1495
1496 __nmk_gpio_set_mode_safe(nmk_chip, bit, g->altsetting, glitch);
1497 clk_disable(nmk_chip->clk);
1498 }
1499
1500 /* When all pins are successfully reconfigured we get here */
1501 ret = 0;
1502
1503out_glitch:
1504 if (glitch) {
1505 nmk_gpio_glitch_slpm_restore(slpm);
1506 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1507 }
1508
1509 return ret;
1510}
1511
1512static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1513 unsigned function, unsigned group)
1514{
1515 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1516 const struct nmk_pingroup *g;
1517
1518 g = &npct->soc->groups[group];
1519
1520 if (g->altsetting < 0)
1521 return;
1522
1523 /* Poke out the mux, set the pin to some default state? */
1524 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1525}
1526
1527int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1528 struct pinctrl_gpio_range *range,
1529 unsigned offset)
1530{
1531 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1532 struct nmk_gpio_chip *nmk_chip;
1533 struct gpio_chip *chip;
1534 unsigned bit;
1535
1536 if (!range) {
1537 dev_err(npct->dev, "invalid range\n");
1538 return -EINVAL;
1539 }
1540 if (!range->gc) {
1541 dev_err(npct->dev, "missing GPIO chip in range\n");
1542 return -EINVAL;
1543 }
1544 chip = range->gc;
1545 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1546
1547 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1548
1549 clk_enable(nmk_chip->clk);
1550 bit = offset % NMK_GPIO_PER_CHIP;
1551 /* There is no glitch when converting any pin to GPIO */
1552 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1553 clk_disable(nmk_chip->clk);
1554
1555 return 0;
1556}
1557
1558void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1559 struct pinctrl_gpio_range *range,
1560 unsigned offset)
1561{
1562 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1563
1564 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1565 /* Set the pin to some default state, GPIO is usually default */
1566}
1567
1568static struct pinmux_ops nmk_pinmux_ops = {
1569 .get_functions_count = nmk_pmx_get_funcs_cnt,
1570 .get_function_name = nmk_pmx_get_func_name,
1571 .get_function_groups = nmk_pmx_get_func_groups,
1572 .enable = nmk_pmx_enable,
1573 .disable = nmk_pmx_disable,
1574 .gpio_request_enable = nmk_gpio_request_enable,
1575 .gpio_disable_free = nmk_gpio_disable_free,
1576};
1577
d41af627
LW
1578int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1579 unsigned pin,
1580 unsigned long *config)
1581{
1582 /* Not implemented */
1583 return -EINVAL;
1584}
1585
1586int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1587 unsigned pin,
1588 unsigned long config)
1589{
1590 static const char *pullnames[] = {
1591 [NMK_GPIO_PULL_NONE] = "none",
1592 [NMK_GPIO_PULL_UP] = "up",
1593 [NMK_GPIO_PULL_DOWN] = "down",
1594 [3] /* illegal */ = "??"
1595 };
1596 static const char *slpmnames[] = {
1597 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1598 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1599 };
1600 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1601 struct nmk_gpio_chip *nmk_chip;
1602 struct pinctrl_gpio_range *range;
1603 struct gpio_chip *chip;
1604 unsigned bit;
1605
1606 /*
1607 * The pin config contains pin number and altfunction fields, here
1608 * we just ignore that part. It's being handled by the framework and
1609 * pinmux callback respectively.
1610 */
1611 pin_cfg_t cfg = (pin_cfg_t) config;
1612 int pull = PIN_PULL(cfg);
1613 int slpm = PIN_SLPM(cfg);
1614 int output = PIN_DIR(cfg);
1615 int val = PIN_VAL(cfg);
1616 bool lowemi = PIN_LOWEMI(cfg);
1617 bool gpiomode = PIN_GPIOMODE(cfg);
1618 bool sleep = PIN_SLEEPMODE(cfg);
1619
1620 range = nmk_match_gpio_range(pctldev, pin);
1621 if (!range) {
1622 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1623 return -EINVAL;
1624 }
1625 if (!range->gc) {
1626 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1627 pin);
1628 return -EINVAL;
1629 }
1630 chip = range->gc;
1631 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1632
1633 if (sleep) {
1634 int slpm_pull = PIN_SLPM_PULL(cfg);
1635 int slpm_output = PIN_SLPM_DIR(cfg);
1636 int slpm_val = PIN_SLPM_VAL(cfg);
1637
1638 /* All pins go into GPIO mode at sleep */
1639 gpiomode = true;
1640
1641 /*
1642 * The SLPM_* values are normal values + 1 to allow zero to
1643 * mean "same as normal".
1644 */
1645 if (slpm_pull)
1646 pull = slpm_pull - 1;
1647 if (slpm_output)
1648 output = slpm_output - 1;
1649 if (slpm_val)
1650 val = slpm_val - 1;
1651
1652 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1653 pin,
1654 slpm_pull ? pullnames[pull] : "same",
1655 slpm_output ? (output ? "output" : "input") : "same",
1656 slpm_val ? (val ? "high" : "low") : "same");
1657 }
1658
1659 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1660 pin, cfg, pullnames[pull], slpmnames[slpm],
1661 output ? "output " : "input",
1662 output ? (val ? "high" : "low") : "",
1663 lowemi ? "on" : "off" );
1664
1665 clk_enable(nmk_chip->clk);
1666 bit = pin % NMK_GPIO_PER_CHIP;
1667 if (gpiomode)
1668 /* No glitch when going to GPIO mode */
1669 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1670 if (output)
1671 __nmk_gpio_make_output(nmk_chip, bit, val);
1672 else {
1673 __nmk_gpio_make_input(nmk_chip, bit);
1674 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1675 }
1676 /* TODO: isn't this only applicable on output pins? */
1677 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1678
1679 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1680 clk_disable(nmk_chip->clk);
1681 return 0;
1682}
1683
1684static struct pinconf_ops nmk_pinconf_ops = {
1685 .pin_config_get = nmk_pin_config_get,
1686 .pin_config_set = nmk_pin_config_set,
1687};
1688
e98ea774
LW
1689static struct pinctrl_desc nmk_pinctrl_desc = {
1690 .name = "pinctrl-nomadik",
1691 .pctlops = &nmk_pinctrl_ops,
dbfe8ca2 1692 .pmxops = &nmk_pinmux_ops,
d41af627 1693 .confops = &nmk_pinconf_ops,
e98ea774
LW
1694 .owner = THIS_MODULE,
1695};
1696
855f80cd
LJ
1697static const struct of_device_id nmk_pinctrl_match[] = {
1698 {
1699 .compatible = "stericsson,nmk_pinctrl",
1700 .data = (void *)PINCTRL_NMK_DB8500,
1701 },
1702 {},
1703};
1704
e98ea774
LW
1705static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1706{
1707 const struct platform_device_id *platid = platform_get_device_id(pdev);
855f80cd 1708 struct device_node *np = pdev->dev.of_node;
e98ea774 1709 struct nmk_pinctrl *npct;
855f80cd 1710 unsigned int version = 0;
e98ea774
LW
1711 int i;
1712
1713 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1714 if (!npct)
1715 return -ENOMEM;
1716
855f80cd
LJ
1717 if (platid)
1718 version = platid->driver_data;
1719 else if (np)
1720 version = (unsigned int)
1721 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1722
e98ea774 1723 /* Poke in other ASIC variants here */
f79c5ed9
LW
1724 if (version == PINCTRL_NMK_STN8815)
1725 nmk_pinctrl_stn8815_init(&npct->soc);
855f80cd 1726 if (version == PINCTRL_NMK_DB8500)
e98ea774 1727 nmk_pinctrl_db8500_init(&npct->soc);
45a1b531
PC
1728 if (version == PINCTRL_NMK_DB8540)
1729 nmk_pinctrl_db8540_init(&npct->soc);
e98ea774
LW
1730
1731 /*
1732 * We need all the GPIO drivers to probe FIRST, or we will not be able
1733 * to obtain references to the struct gpio_chip * for them, and we
1734 * need this to proceed.
1735 */
1736 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1737 if (!nmk_gpio_chips[i]) {
1738 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
e98ea774
LW
1739 return -EPROBE_DEFER;
1740 }
1741 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1742 }
1743
1744 nmk_pinctrl_desc.pins = npct->soc->pins;
1745 nmk_pinctrl_desc.npins = npct->soc->npins;
1746 npct->dev = &pdev->dev;
1747 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1748 if (!npct->pctl) {
1749 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1750 return -EINVAL;
1751 }
1752
1753 /* We will handle a range of GPIO pins */
1754 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1755 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1756
1757 platform_set_drvdata(pdev, npct);
1758 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1759
1760 return 0;
1761}
1762
513c27f8
LJ
1763static const struct of_device_id nmk_gpio_match[] = {
1764 { .compatible = "st,nomadik-gpio", },
1765 {}
1766};
1767
3e3c62ca
RV
1768static struct platform_driver nmk_gpio_driver = {
1769 .driver = {
2ec1d359
AR
1770 .owner = THIS_MODULE,
1771 .name = "gpio",
513c27f8 1772 .of_match_table = nmk_gpio_match,
5317e4d1 1773 },
2ec1d359 1774 .probe = nmk_gpio_probe,
2ec1d359
AR
1775};
1776
e98ea774
LW
1777static const struct platform_device_id nmk_pinctrl_id[] = {
1778 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1779 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
45a1b531 1780 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
e98ea774
LW
1781};
1782
1783static struct platform_driver nmk_pinctrl_driver = {
1784 .driver = {
1785 .owner = THIS_MODULE,
1786 .name = "pinctrl-nomadik",
855f80cd 1787 .of_match_table = nmk_pinctrl_match,
e98ea774
LW
1788 },
1789 .probe = nmk_pinctrl_probe,
1790 .id_table = nmk_pinctrl_id,
1791};
1792
2ec1d359
AR
1793static int __init nmk_gpio_init(void)
1794{
e98ea774
LW
1795 int ret;
1796
1797 ret = platform_driver_register(&nmk_gpio_driver);
1798 if (ret)
1799 return ret;
1800 return platform_driver_register(&nmk_pinctrl_driver);
2ec1d359
AR
1801}
1802
33f45ea9 1803core_initcall(nmk_gpio_init);
2ec1d359
AR
1804
1805MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1806MODULE_DESCRIPTION("Nomadik GPIO Driver");
1807MODULE_LICENSE("GPL");