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