2 * Generic GPIO driver for logic cells found in the Nomadik SoC
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
7 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
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.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/irqdomain.h>
26 #include <linux/slab.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/pinctrl/pinconf.h>
30 /* Since we request GPIOs from ourself */
31 #include <linux/pinctrl/consumer.h>
33 #include <asm/mach/irq.h>
35 #include <plat/pincfg.h>
36 #include <plat/gpio-nomadik.h>
38 #include "pinctrl-nomadik.h"
41 * The GPIO module in the Nomadik family of Systems-on-Chip is an
42 * AMBA device, managing 32 pins and alternate functions. The logic block
43 * is currently used in the Nomadik and ux500.
45 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
48 #define NMK_GPIO_PER_CHIP 32
50 struct nmk_gpio_chip {
51 struct gpio_chip chip;
52 struct irq_domain *domain;
56 unsigned int parent_irq;
57 int secondary_parent_irq;
58 u32 (*get_secondary_status)(unsigned int bank);
59 void (*set_ioforce)(bool enable);
62 /* Keep track of configured edges */
76 struct pinctrl_dev *pctl;
77 const struct nmk_pinctrl_soc_data *soc;
80 static struct nmk_gpio_chip *
81 nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
83 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
85 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
87 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
88 unsigned offset, int gpio_mode)
90 u32 bit = 1 << offset;
93 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
94 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
95 if (gpio_mode & NMK_GPIO_ALT_A)
97 if (gpio_mode & NMK_GPIO_ALT_B)
99 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
100 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
103 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
104 unsigned offset, enum nmk_gpio_slpm mode)
106 u32 bit = 1 << offset;
109 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
110 if (mode == NMK_GPIO_SLPM_NOCHANGE)
114 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
117 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
118 unsigned offset, enum nmk_gpio_pull pull)
120 u32 bit = 1 << offset;
123 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
124 if (pull == NMK_GPIO_PULL_NONE) {
126 nmk_chip->pull_up &= ~bit;
131 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
133 if (pull == NMK_GPIO_PULL_UP) {
134 nmk_chip->pull_up |= bit;
135 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
136 } else if (pull == NMK_GPIO_PULL_DOWN) {
137 nmk_chip->pull_up &= ~bit;
138 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
142 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
143 unsigned offset, bool lowemi)
145 u32 bit = BIT(offset);
146 bool enabled = nmk_chip->lowemi & bit;
148 if (lowemi == enabled)
152 nmk_chip->lowemi |= bit;
154 nmk_chip->lowemi &= ~bit;
156 writel_relaxed(nmk_chip->lowemi,
157 nmk_chip->addr + NMK_GPIO_LOWEMI);
160 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
163 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
166 static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
167 unsigned offset, int val)
170 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
172 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
175 static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
176 unsigned offset, int val)
178 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
179 __nmk_gpio_set_output(nmk_chip, offset, val);
182 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
183 unsigned offset, int gpio_mode,
186 u32 rwimsc = nmk_chip->rwimsc;
187 u32 fwimsc = nmk_chip->fwimsc;
189 if (glitch && nmk_chip->set_ioforce) {
190 u32 bit = BIT(offset);
192 /* Prevent spurious wakeups */
193 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
194 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
196 nmk_chip->set_ioforce(true);
199 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
201 if (glitch && nmk_chip->set_ioforce) {
202 nmk_chip->set_ioforce(false);
204 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
205 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
210 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
212 u32 falling = nmk_chip->fimsc & BIT(offset);
213 u32 rising = nmk_chip->rimsc & BIT(offset);
214 int gpio = nmk_chip->chip.base + offset;
215 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
216 struct irq_data *d = irq_get_irq_data(irq);
218 if (!rising && !falling)
221 if (!d || !irqd_irq_disabled(d))
225 nmk_chip->rimsc &= ~BIT(offset);
226 writel_relaxed(nmk_chip->rimsc,
227 nmk_chip->addr + NMK_GPIO_RIMSC);
231 nmk_chip->fimsc &= ~BIT(offset);
232 writel_relaxed(nmk_chip->fimsc,
233 nmk_chip->addr + NMK_GPIO_FIMSC);
236 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
239 static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
240 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
242 static const char *afnames[] = {
243 [NMK_GPIO_ALT_GPIO] = "GPIO",
244 [NMK_GPIO_ALT_A] = "A",
245 [NMK_GPIO_ALT_B] = "B",
246 [NMK_GPIO_ALT_C] = "C"
248 static const char *pullnames[] = {
249 [NMK_GPIO_PULL_NONE] = "none",
250 [NMK_GPIO_PULL_UP] = "up",
251 [NMK_GPIO_PULL_DOWN] = "down",
252 [3] /* illegal */ = "??"
254 static const char *slpmnames[] = {
255 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
256 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
259 int pin = PIN_NUM(cfg);
260 int pull = PIN_PULL(cfg);
261 int af = PIN_ALT(cfg);
262 int slpm = PIN_SLPM(cfg);
263 int output = PIN_DIR(cfg);
264 int val = PIN_VAL(cfg);
265 bool glitch = af == NMK_GPIO_ALT_C;
267 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
268 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
269 output ? "output " : "input",
270 output ? (val ? "high" : "low") : "");
273 int slpm_pull = PIN_SLPM_PULL(cfg);
274 int slpm_output = PIN_SLPM_DIR(cfg);
275 int slpm_val = PIN_SLPM_VAL(cfg);
277 af = NMK_GPIO_ALT_GPIO;
280 * The SLPM_* values are normal values + 1 to allow zero to
281 * mean "same as normal".
284 pull = slpm_pull - 1;
286 output = slpm_output - 1;
290 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
292 slpm_pull ? pullnames[pull] : "same",
293 slpm_output ? (output ? "output" : "input") : "same",
294 slpm_val ? (val ? "high" : "low") : "same");
298 __nmk_gpio_make_output(nmk_chip, offset, val);
300 __nmk_gpio_make_input(nmk_chip, offset);
301 __nmk_gpio_set_pull(nmk_chip, offset, pull);
304 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
307 * If the pin is switching to altfunc, and there was an interrupt
308 * installed on it which has been lazy disabled, actually mask the
309 * interrupt to prevent spurious interrupts that would occur while the
310 * pin is under control of the peripheral. Only SKE does this.
312 if (af != NMK_GPIO_ALT_GPIO)
313 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
316 * If we've backed up the SLPM registers (glitch workaround), modify
317 * the backups since they will be restored.
320 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
321 slpmregs[nmk_chip->bank] |= BIT(offset);
323 slpmregs[nmk_chip->bank] &= ~BIT(offset);
325 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
327 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
331 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
332 * - Save SLPM registers
333 * - Set SLPM=0 for the IOs you want to switch and others to 1
334 * - Configure the GPIO registers for the IOs that are being switched
336 * - Modify the AFLSA/B registers for the IOs that are being switched
338 * - Restore SLPM registers
339 * - Any spurious wake up event during switch sequence to be ignored and
342 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
346 for (i = 0; i < NUM_BANKS; i++) {
347 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
348 unsigned int temp = slpm[i];
353 clk_enable(chip->clk);
355 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
356 writel(temp, chip->addr + NMK_GPIO_SLPC);
360 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
364 for (i = 0; i < NUM_BANKS; i++) {
365 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
370 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
372 clk_disable(chip->clk);
376 static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
378 static unsigned int slpm[NUM_BANKS];
384 for (i = 0; i < num; i++) {
385 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
391 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
394 memset(slpm, 0xff, sizeof(slpm));
396 for (i = 0; i < num; i++) {
397 int pin = PIN_NUM(cfgs[i]);
398 int offset = pin % NMK_GPIO_PER_CHIP;
400 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
401 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
404 nmk_gpio_glitch_slpm_init(slpm);
407 for (i = 0; i < num; i++) {
408 struct nmk_gpio_chip *nmk_chip;
409 int pin = PIN_NUM(cfgs[i]);
411 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
417 clk_enable(nmk_chip->clk);
418 spin_lock(&nmk_chip->lock);
419 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
420 cfgs[i], sleep, glitch ? slpm : NULL);
421 spin_unlock(&nmk_chip->lock);
422 clk_disable(nmk_chip->clk);
426 nmk_gpio_glitch_slpm_restore(slpm);
428 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
434 * nmk_config_pin - configure a pin's mux attributes
435 * @cfg: pin confguration
437 * Configures a pin's mode (alternate function or GPIO), its pull up status,
438 * and its sleep mode based on the specified configuration. The @cfg is
439 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
440 * are constructed using, and can be further enhanced with, the macros in
443 * If a pin's mode is set to GPIO, it is configured as an input to avoid
444 * side-effects. The gpio can be manipulated later using standard GPIO API
447 int nmk_config_pin(pin_cfg_t cfg, bool sleep)
449 return __nmk_config_pins(&cfg, 1, sleep);
451 EXPORT_SYMBOL(nmk_config_pin);
454 * nmk_config_pins - configure several pins at once
455 * @cfgs: array of pin configurations
456 * @num: number of elments in the array
458 * Configures several pins using nmk_config_pin(). Refer to that function for
459 * further information.
461 int nmk_config_pins(pin_cfg_t *cfgs, int num)
463 return __nmk_config_pins(cfgs, num, false);
465 EXPORT_SYMBOL(nmk_config_pins);
467 int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
469 return __nmk_config_pins(cfgs, num, true);
471 EXPORT_SYMBOL(nmk_config_pins_sleep);
474 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
476 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
478 * This register is actually in the pinmux layer, not the GPIO block itself.
479 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
480 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
481 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
482 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
483 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
484 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
486 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
487 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
488 * entered) regardless of the altfunction selected. Also wake-up detection is
491 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
492 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
493 * (for altfunction GPIO) or respective on-chip peripherals (for other
494 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
496 * Note that enable_irq_wake() will automatically enable wakeup detection.
498 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
500 struct nmk_gpio_chip *nmk_chip;
503 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
507 clk_enable(nmk_chip->clk);
508 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
509 spin_lock(&nmk_chip->lock);
511 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
513 spin_unlock(&nmk_chip->lock);
514 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
515 clk_disable(nmk_chip->clk);
521 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
523 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
525 * Enables/disables pull up/down on a specified pin. This only takes effect if
526 * the pin is configured as an input (either explicitly or by the alternate
529 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
530 * configured as an input. Otherwise, due to the way the controller registers
531 * work, this function will change the value output on the pin.
533 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
535 struct nmk_gpio_chip *nmk_chip;
538 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
542 clk_enable(nmk_chip->clk);
543 spin_lock_irqsave(&nmk_chip->lock, flags);
544 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
545 spin_unlock_irqrestore(&nmk_chip->lock, flags);
546 clk_disable(nmk_chip->clk);
553 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
555 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
556 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
558 * Sets the mode of the specified pin to one of the alternate functions or
561 int nmk_gpio_set_mode(int gpio, int gpio_mode)
563 struct nmk_gpio_chip *nmk_chip;
566 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
570 clk_enable(nmk_chip->clk);
571 spin_lock_irqsave(&nmk_chip->lock, flags);
572 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
573 spin_unlock_irqrestore(&nmk_chip->lock, flags);
574 clk_disable(nmk_chip->clk);
578 EXPORT_SYMBOL(nmk_gpio_set_mode);
580 int nmk_gpio_get_mode(int gpio)
582 struct nmk_gpio_chip *nmk_chip;
583 u32 afunc, bfunc, bit;
585 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
589 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
591 clk_enable(nmk_chip->clk);
593 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
594 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
596 clk_disable(nmk_chip->clk);
598 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
600 EXPORT_SYMBOL(nmk_gpio_get_mode);
604 static inline int nmk_gpio_get_bitmask(int gpio)
606 return 1 << (gpio % NMK_GPIO_PER_CHIP);
609 static void nmk_gpio_irq_ack(struct irq_data *d)
611 struct nmk_gpio_chip *nmk_chip;
613 nmk_chip = irq_data_get_irq_chip_data(d);
617 clk_enable(nmk_chip->clk);
618 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
619 clk_disable(nmk_chip->clk);
622 enum nmk_gpio_irq_type {
627 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
628 int gpio, enum nmk_gpio_irq_type which,
631 u32 bitmask = nmk_gpio_get_bitmask(gpio);
637 if (which == NORMAL) {
638 rimscreg = NMK_GPIO_RIMSC;
639 fimscreg = NMK_GPIO_FIMSC;
640 rimscval = &nmk_chip->rimsc;
641 fimscval = &nmk_chip->fimsc;
643 rimscreg = NMK_GPIO_RWIMSC;
644 fimscreg = NMK_GPIO_FWIMSC;
645 rimscval = &nmk_chip->rwimsc;
646 fimscval = &nmk_chip->fwimsc;
649 /* we must individually set/clear the two edges */
650 if (nmk_chip->edge_rising & bitmask) {
652 *rimscval |= bitmask;
654 *rimscval &= ~bitmask;
655 writel(*rimscval, nmk_chip->addr + rimscreg);
657 if (nmk_chip->edge_falling & bitmask) {
659 *fimscval |= bitmask;
661 *fimscval &= ~bitmask;
662 writel(*fimscval, nmk_chip->addr + fimscreg);
666 static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
670 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
671 * disabled, since setting SLPM to 1 increases power consumption, and
672 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
674 if (nmk_chip->sleepmode && on) {
675 __nmk_gpio_set_slpm(nmk_chip, gpio % nmk_chip->chip.base,
676 NMK_GPIO_SLPM_WAKEUP_ENABLE);
679 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
682 static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
684 struct nmk_gpio_chip *nmk_chip;
688 nmk_chip = irq_data_get_irq_chip_data(d);
689 bitmask = nmk_gpio_get_bitmask(d->hwirq);
693 clk_enable(nmk_chip->clk);
694 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
695 spin_lock(&nmk_chip->lock);
697 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
699 if (!(nmk_chip->real_wake & bitmask))
700 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
702 spin_unlock(&nmk_chip->lock);
703 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
704 clk_disable(nmk_chip->clk);
709 static void nmk_gpio_irq_mask(struct irq_data *d)
711 nmk_gpio_irq_maskunmask(d, false);
714 static void nmk_gpio_irq_unmask(struct irq_data *d)
716 nmk_gpio_irq_maskunmask(d, true);
719 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
721 struct nmk_gpio_chip *nmk_chip;
725 nmk_chip = irq_data_get_irq_chip_data(d);
728 bitmask = nmk_gpio_get_bitmask(d->hwirq);
730 clk_enable(nmk_chip->clk);
731 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
732 spin_lock(&nmk_chip->lock);
734 if (irqd_irq_disabled(d))
735 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
738 nmk_chip->real_wake |= bitmask;
740 nmk_chip->real_wake &= ~bitmask;
742 spin_unlock(&nmk_chip->lock);
743 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
744 clk_disable(nmk_chip->clk);
749 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
751 bool enabled = !irqd_irq_disabled(d);
752 bool wake = irqd_is_wakeup_set(d);
753 struct nmk_gpio_chip *nmk_chip;
757 nmk_chip = irq_data_get_irq_chip_data(d);
758 bitmask = nmk_gpio_get_bitmask(d->hwirq);
761 if (type & IRQ_TYPE_LEVEL_HIGH)
763 if (type & IRQ_TYPE_LEVEL_LOW)
766 clk_enable(nmk_chip->clk);
767 spin_lock_irqsave(&nmk_chip->lock, flags);
770 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
773 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
775 nmk_chip->edge_rising &= ~bitmask;
776 if (type & IRQ_TYPE_EDGE_RISING)
777 nmk_chip->edge_rising |= bitmask;
779 nmk_chip->edge_falling &= ~bitmask;
780 if (type & IRQ_TYPE_EDGE_FALLING)
781 nmk_chip->edge_falling |= bitmask;
784 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
787 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
789 spin_unlock_irqrestore(&nmk_chip->lock, flags);
790 clk_disable(nmk_chip->clk);
795 static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
797 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
799 clk_enable(nmk_chip->clk);
800 nmk_gpio_irq_unmask(d);
804 static void nmk_gpio_irq_shutdown(struct irq_data *d)
806 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
808 nmk_gpio_irq_mask(d);
809 clk_disable(nmk_chip->clk);
812 static struct irq_chip nmk_gpio_irq_chip = {
813 .name = "Nomadik-GPIO",
814 .irq_ack = nmk_gpio_irq_ack,
815 .irq_mask = nmk_gpio_irq_mask,
816 .irq_unmask = nmk_gpio_irq_unmask,
817 .irq_set_type = nmk_gpio_irq_set_type,
818 .irq_set_wake = nmk_gpio_irq_set_wake,
819 .irq_startup = nmk_gpio_irq_startup,
820 .irq_shutdown = nmk_gpio_irq_shutdown,
823 static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
826 struct nmk_gpio_chip *nmk_chip;
827 struct irq_chip *host_chip = irq_get_chip(irq);
828 unsigned int first_irq;
830 chained_irq_enter(host_chip, desc);
832 nmk_chip = irq_get_handler_data(irq);
833 first_irq = nmk_chip->domain->revmap_data.legacy.first_irq;
835 int bit = __ffs(status);
837 generic_handle_irq(first_irq + bit);
841 chained_irq_exit(host_chip, desc);
844 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
846 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
849 clk_enable(nmk_chip->clk);
850 status = readl(nmk_chip->addr + NMK_GPIO_IS);
851 clk_disable(nmk_chip->clk);
853 __nmk_gpio_irq_handler(irq, desc, status);
856 static void nmk_gpio_secondary_irq_handler(unsigned int irq,
857 struct irq_desc *desc)
859 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
860 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
862 __nmk_gpio_irq_handler(irq, desc, status);
865 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
867 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
868 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
870 if (nmk_chip->secondary_parent_irq >= 0) {
871 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
872 nmk_gpio_secondary_irq_handler);
873 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
881 static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
884 * Map back to global GPIO space and request muxing, the direction
885 * parameter does not matter for this controller.
887 int gpio = chip->base + offset;
889 return pinctrl_request_gpio(gpio);
892 static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
894 int gpio = chip->base + offset;
896 pinctrl_free_gpio(gpio);
899 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
901 struct nmk_gpio_chip *nmk_chip =
902 container_of(chip, struct nmk_gpio_chip, chip);
904 clk_enable(nmk_chip->clk);
906 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
908 clk_disable(nmk_chip->clk);
913 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
915 struct nmk_gpio_chip *nmk_chip =
916 container_of(chip, struct nmk_gpio_chip, chip);
917 u32 bit = 1 << offset;
920 clk_enable(nmk_chip->clk);
922 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
924 clk_disable(nmk_chip->clk);
929 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
932 struct nmk_gpio_chip *nmk_chip =
933 container_of(chip, struct nmk_gpio_chip, chip);
935 clk_enable(nmk_chip->clk);
937 __nmk_gpio_set_output(nmk_chip, offset, val);
939 clk_disable(nmk_chip->clk);
942 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
945 struct nmk_gpio_chip *nmk_chip =
946 container_of(chip, struct nmk_gpio_chip, chip);
948 clk_enable(nmk_chip->clk);
950 __nmk_gpio_make_output(nmk_chip, offset, val);
952 clk_disable(nmk_chip->clk);
957 static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
959 struct nmk_gpio_chip *nmk_chip =
960 container_of(chip, struct nmk_gpio_chip, chip);
962 return irq_find_mapping(nmk_chip->domain, offset);
965 #ifdef CONFIG_DEBUG_FS
967 #include <linux/seq_file.h>
969 static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
970 unsigned offset, unsigned gpio)
972 const char *label = gpiochip_is_requested(chip, offset);
973 struct nmk_gpio_chip *nmk_chip =
974 container_of(chip, struct nmk_gpio_chip, chip);
978 u32 bit = 1 << offset;
979 const char *modes[] = {
980 [NMK_GPIO_ALT_GPIO] = "gpio",
981 [NMK_GPIO_ALT_A] = "altA",
982 [NMK_GPIO_ALT_B] = "altB",
983 [NMK_GPIO_ALT_C] = "altC",
986 clk_enable(nmk_chip->clk);
987 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
988 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
989 mode = nmk_gpio_get_mode(gpio);
991 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
992 gpio, label ?: "(none)",
993 is_out ? "out" : "in ",
995 ? (chip->get(chip, offset) ? "hi" : "lo")
997 (mode < 0) ? "unknown" : modes[mode],
998 pull ? "pull" : "none");
1000 if (label && !is_out) {
1001 int irq = gpio_to_irq(gpio);
1002 struct irq_desc *desc = irq_to_desc(irq);
1004 /* This races with request_irq(), set_irq_type(),
1005 * and set_irq_wake() ... but those are "rare".
1007 if (irq >= 0 && desc->action) {
1009 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1011 if (nmk_chip->edge_rising & bitmask)
1012 trigger = "edge-rising";
1013 else if (nmk_chip->edge_falling & bitmask)
1014 trigger = "edge-falling";
1016 trigger = "edge-undefined";
1018 seq_printf(s, " irq-%d %s%s",
1020 irqd_is_wakeup_set(&desc->irq_data)
1024 clk_disable(nmk_chip->clk);
1027 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1030 unsigned gpio = chip->base;
1032 for (i = 0; i < chip->ngpio; i++, gpio++) {
1033 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1034 seq_printf(s, "\n");
1039 static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1040 struct gpio_chip *chip,
1041 unsigned offset, unsigned gpio)
1044 #define nmk_gpio_dbg_show NULL
1047 /* This structure is replicated for each GPIO block allocated at probe time */
1048 static struct gpio_chip nmk_gpio_template = {
1049 .request = nmk_gpio_request,
1050 .free = nmk_gpio_free,
1051 .direction_input = nmk_gpio_make_input,
1052 .get = nmk_gpio_get_input,
1053 .direction_output = nmk_gpio_make_output,
1054 .set = nmk_gpio_set_output,
1055 .to_irq = nmk_gpio_to_irq,
1056 .dbg_show = nmk_gpio_dbg_show,
1060 void nmk_gpio_clocks_enable(void)
1064 for (i = 0; i < NUM_BANKS; i++) {
1065 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1070 clk_enable(chip->clk);
1074 void nmk_gpio_clocks_disable(void)
1078 for (i = 0; i < NUM_BANKS; i++) {
1079 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1084 clk_disable(chip->clk);
1089 * Called from the suspend/resume path to only keep the real wakeup interrupts
1090 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1091 * and not the rest of the interrupts which we needed to have as wakeups for
1094 * PM ops are not used since this needs to be done at the end, after all the
1095 * other drivers are done with their suspend callbacks.
1097 void nmk_gpio_wakeups_suspend(void)
1101 for (i = 0; i < NUM_BANKS; i++) {
1102 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1107 clk_enable(chip->clk);
1109 writel(chip->rwimsc & chip->real_wake,
1110 chip->addr + NMK_GPIO_RWIMSC);
1111 writel(chip->fwimsc & chip->real_wake,
1112 chip->addr + NMK_GPIO_FWIMSC);
1114 clk_disable(chip->clk);
1118 void nmk_gpio_wakeups_resume(void)
1122 for (i = 0; i < NUM_BANKS; i++) {
1123 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1128 clk_enable(chip->clk);
1130 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1131 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1133 clk_disable(chip->clk);
1138 * Read the pull up/pull down status.
1139 * A bit set in 'pull_up' means that pull up
1140 * is selected if pull is enabled in PDIS register.
1141 * Note: only pull up/down set via this driver can
1142 * be detected due to HW limitations.
1144 void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1146 if (gpio_bank < NUM_BANKS) {
1147 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1152 *pull_up = chip->pull_up;
1156 int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1157 irq_hw_number_t hwirq)
1159 struct nmk_gpio_chip *nmk_chip = d->host_data;
1164 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1165 set_irq_flags(irq, IRQF_VALID);
1166 irq_set_chip_data(irq, nmk_chip);
1167 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1172 const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1173 .map = nmk_gpio_irq_map,
1174 .xlate = irq_domain_xlate_twocell,
1177 static int __devinit nmk_gpio_probe(struct platform_device *dev)
1179 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
1180 struct device_node *np = dev->dev.of_node;
1181 struct nmk_gpio_chip *nmk_chip;
1182 struct gpio_chip *chip;
1183 struct resource *res;
1190 if (!pdata && !np) {
1191 dev_err(&dev->dev, "No platform data or device tree found\n");
1196 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1200 if (of_get_property(np, "supports-sleepmode", NULL))
1201 pdata->supports_sleepmode = true;
1203 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1204 dev_err(&dev->dev, "gpio-bank property not found\n");
1209 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1210 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1213 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1219 irq = platform_get_irq(dev, 0);
1225 secondary_irq = platform_get_irq(dev, 1);
1226 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1231 if (request_mem_region(res->start, resource_size(res),
1232 dev_name(&dev->dev)) == NULL) {
1237 base = ioremap(res->start, resource_size(res));
1243 clk = clk_get(&dev->dev, NULL);
1249 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1256 * The virt address in nmk_chip->addr is in the nomadik register space,
1257 * so we can simply convert the resource address, without remapping
1259 nmk_chip->bank = dev->id;
1260 nmk_chip->clk = clk;
1261 nmk_chip->addr = base;
1262 nmk_chip->chip = nmk_gpio_template;
1263 nmk_chip->parent_irq = irq;
1264 nmk_chip->secondary_parent_irq = secondary_irq;
1265 nmk_chip->get_secondary_status = pdata->get_secondary_status;
1266 nmk_chip->set_ioforce = pdata->set_ioforce;
1267 nmk_chip->sleepmode = pdata->supports_sleepmode;
1268 spin_lock_init(&nmk_chip->lock);
1270 chip = &nmk_chip->chip;
1271 chip->base = pdata->first_gpio;
1272 chip->ngpio = pdata->num_gpio;
1273 chip->label = pdata->name ?: dev_name(&dev->dev);
1274 chip->dev = &dev->dev;
1275 chip->owner = THIS_MODULE;
1277 clk_enable(nmk_chip->clk);
1278 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1279 clk_disable(nmk_chip->clk);
1281 #ifdef CONFIG_OF_GPIO
1285 ret = gpiochip_add(&nmk_chip->chip);
1289 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1291 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
1293 platform_set_drvdata(dev, nmk_chip);
1295 nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
1296 NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
1297 0, &nmk_gpio_irq_simple_ops, nmk_chip);
1298 if (!nmk_chip->domain) {
1299 pr_err("%s: Failed to create irqdomain\n", np->full_name);
1304 nmk_gpio_init_irq(nmk_chip);
1306 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1318 release_mem_region(res->start, resource_size(res));
1320 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1321 pdata->first_gpio, pdata->first_gpio+31);
1328 static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1330 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1332 return npct->soc->ngroups;
1335 static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1338 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1340 return npct->soc->groups[selector].name;
1343 static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1344 const unsigned **pins,
1347 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1349 *pins = npct->soc->groups[selector].pins;
1350 *num_pins = npct->soc->groups[selector].npins;
1354 static struct pinctrl_gpio_range *
1355 nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1357 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1360 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1361 struct pinctrl_gpio_range *range;
1363 range = &npct->soc->gpio_ranges[i];
1364 if (offset >= range->pin_base &&
1365 offset <= (range->pin_base + range->npins - 1))
1371 static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1374 struct pinctrl_gpio_range *range;
1375 struct gpio_chip *chip;
1377 range = nmk_match_gpio_range(pctldev, offset);
1378 if (!range || !range->gc) {
1379 seq_printf(s, "invalid pin offset");
1383 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
1386 static struct pinctrl_ops nmk_pinctrl_ops = {
1387 .get_groups_count = nmk_get_groups_cnt,
1388 .get_group_name = nmk_get_group_name,
1389 .get_group_pins = nmk_get_group_pins,
1390 .pin_dbg_show = nmk_pin_dbg_show,
1393 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1395 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1397 return npct->soc->nfunctions;
1400 static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1403 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1405 return npct->soc->functions[function].name;
1408 static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1410 const char * const **groups,
1411 unsigned * const num_groups)
1413 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1415 *groups = npct->soc->functions[function].groups;
1416 *num_groups = npct->soc->functions[function].ngroups;
1421 static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1424 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1425 const struct nmk_pingroup *g;
1426 static unsigned int slpm[NUM_BANKS];
1427 unsigned long flags;
1432 g = &npct->soc->groups[group];
1434 if (g->altsetting < 0)
1437 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1439 /* Handle this special glitch on altfunction C */
1440 glitch = (g->altsetting == NMK_GPIO_ALT_C);
1443 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1445 /* Initially don't put any pins to sleep when switching */
1446 memset(slpm, 0xff, sizeof(slpm));
1449 * Then mask the pins that need to be sleeping now when we're
1450 * switching to the ALT C function.
1452 for (i = 0; i < g->npins; i++)
1453 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1454 nmk_gpio_glitch_slpm_init(slpm);
1457 for (i = 0; i < g->npins; i++) {
1458 struct pinctrl_gpio_range *range;
1459 struct nmk_gpio_chip *nmk_chip;
1460 struct gpio_chip *chip;
1463 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1466 "invalid pin offset %d in group %s at index %d\n",
1467 g->pins[i], g->name, i);
1471 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1472 g->pins[i], g->name, i);
1476 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1477 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1479 clk_enable(nmk_chip->clk);
1480 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1482 * If the pin is switching to altfunc, and there was an
1483 * interrupt installed on it which has been lazy disabled,
1484 * actually mask the interrupt to prevent spurious interrupts
1485 * that would occur while the pin is under control of the
1486 * peripheral. Only SKE does this.
1488 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1490 __nmk_gpio_set_mode_safe(nmk_chip, bit, g->altsetting, glitch);
1491 clk_disable(nmk_chip->clk);
1494 /* When all pins are successfully reconfigured we get here */
1499 nmk_gpio_glitch_slpm_restore(slpm);
1500 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1506 static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1507 unsigned function, unsigned group)
1509 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1510 const struct nmk_pingroup *g;
1512 g = &npct->soc->groups[group];
1514 if (g->altsetting < 0)
1517 /* Poke out the mux, set the pin to some default state? */
1518 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1521 int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1522 struct pinctrl_gpio_range *range,
1525 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1526 struct nmk_gpio_chip *nmk_chip;
1527 struct gpio_chip *chip;
1531 dev_err(npct->dev, "invalid range\n");
1535 dev_err(npct->dev, "missing GPIO chip in range\n");
1539 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1541 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1543 clk_enable(nmk_chip->clk);
1544 bit = offset % NMK_GPIO_PER_CHIP;
1545 /* There is no glitch when converting any pin to GPIO */
1546 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1547 clk_disable(nmk_chip->clk);
1552 void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1553 struct pinctrl_gpio_range *range,
1556 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1558 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1559 /* Set the pin to some default state, GPIO is usually default */
1562 static struct pinmux_ops nmk_pinmux_ops = {
1563 .get_functions_count = nmk_pmx_get_funcs_cnt,
1564 .get_function_name = nmk_pmx_get_func_name,
1565 .get_function_groups = nmk_pmx_get_func_groups,
1566 .enable = nmk_pmx_enable,
1567 .disable = nmk_pmx_disable,
1568 .gpio_request_enable = nmk_gpio_request_enable,
1569 .gpio_disable_free = nmk_gpio_disable_free,
1572 int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1574 unsigned long *config)
1576 /* Not implemented */
1580 int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1582 unsigned long config)
1584 static const char *pullnames[] = {
1585 [NMK_GPIO_PULL_NONE] = "none",
1586 [NMK_GPIO_PULL_UP] = "up",
1587 [NMK_GPIO_PULL_DOWN] = "down",
1588 [3] /* illegal */ = "??"
1590 static const char *slpmnames[] = {
1591 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1592 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1594 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1595 struct nmk_gpio_chip *nmk_chip;
1596 struct pinctrl_gpio_range *range;
1597 struct gpio_chip *chip;
1601 * The pin config contains pin number and altfunction fields, here
1602 * we just ignore that part. It's being handled by the framework and
1603 * pinmux callback respectively.
1605 pin_cfg_t cfg = (pin_cfg_t) config;
1606 int pull = PIN_PULL(cfg);
1607 int slpm = PIN_SLPM(cfg);
1608 int output = PIN_DIR(cfg);
1609 int val = PIN_VAL(cfg);
1610 bool lowemi = PIN_LOWEMI(cfg);
1611 bool gpiomode = PIN_GPIOMODE(cfg);
1612 bool sleep = PIN_SLEEPMODE(cfg);
1614 range = nmk_match_gpio_range(pctldev, pin);
1616 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1620 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1625 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1628 int slpm_pull = PIN_SLPM_PULL(cfg);
1629 int slpm_output = PIN_SLPM_DIR(cfg);
1630 int slpm_val = PIN_SLPM_VAL(cfg);
1632 /* All pins go into GPIO mode at sleep */
1636 * The SLPM_* values are normal values + 1 to allow zero to
1637 * mean "same as normal".
1640 pull = slpm_pull - 1;
1642 output = slpm_output - 1;
1646 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1648 slpm_pull ? pullnames[pull] : "same",
1649 slpm_output ? (output ? "output" : "input") : "same",
1650 slpm_val ? (val ? "high" : "low") : "same");
1653 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1654 pin, cfg, pullnames[pull], slpmnames[slpm],
1655 output ? "output " : "input",
1656 output ? (val ? "high" : "low") : "",
1657 lowemi ? "on" : "off" );
1659 clk_enable(nmk_chip->clk);
1660 bit = pin % NMK_GPIO_PER_CHIP;
1662 /* No glitch when going to GPIO mode */
1663 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1665 __nmk_gpio_make_output(nmk_chip, bit, val);
1667 __nmk_gpio_make_input(nmk_chip, bit);
1668 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1670 /* TODO: isn't this only applicable on output pins? */
1671 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1673 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1674 clk_disable(nmk_chip->clk);
1678 static struct pinconf_ops nmk_pinconf_ops = {
1679 .pin_config_get = nmk_pin_config_get,
1680 .pin_config_set = nmk_pin_config_set,
1683 static struct pinctrl_desc nmk_pinctrl_desc = {
1684 .name = "pinctrl-nomadik",
1685 .pctlops = &nmk_pinctrl_ops,
1686 .pmxops = &nmk_pinmux_ops,
1687 .confops = &nmk_pinconf_ops,
1688 .owner = THIS_MODULE,
1691 static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1693 const struct platform_device_id *platid = platform_get_device_id(pdev);
1694 struct nmk_pinctrl *npct;
1697 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1701 /* Poke in other ASIC variants here */
1702 if (platid->driver_data == PINCTRL_NMK_DB8500)
1703 nmk_pinctrl_db8500_init(&npct->soc);
1706 * We need all the GPIO drivers to probe FIRST, or we will not be able
1707 * to obtain references to the struct gpio_chip * for them, and we
1708 * need this to proceed.
1710 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1711 if (!nmk_gpio_chips[i]) {
1712 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
1713 devm_kfree(&pdev->dev, npct);
1714 return -EPROBE_DEFER;
1716 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1719 nmk_pinctrl_desc.pins = npct->soc->pins;
1720 nmk_pinctrl_desc.npins = npct->soc->npins;
1721 npct->dev = &pdev->dev;
1722 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1724 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1728 /* We will handle a range of GPIO pins */
1729 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1730 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1732 platform_set_drvdata(pdev, npct);
1733 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1738 static const struct of_device_id nmk_gpio_match[] = {
1739 { .compatible = "st,nomadik-gpio", },
1743 static struct platform_driver nmk_gpio_driver = {
1745 .owner = THIS_MODULE,
1747 .of_match_table = nmk_gpio_match,
1749 .probe = nmk_gpio_probe,
1752 static const struct platform_device_id nmk_pinctrl_id[] = {
1753 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1754 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
1757 static struct platform_driver nmk_pinctrl_driver = {
1759 .owner = THIS_MODULE,
1760 .name = "pinctrl-nomadik",
1762 .probe = nmk_pinctrl_probe,
1763 .id_table = nmk_pinctrl_id,
1766 static int __init nmk_gpio_init(void)
1770 ret = platform_driver_register(&nmk_gpio_driver);
1773 return platform_driver_register(&nmk_pinctrl_driver);
1776 core_initcall(nmk_gpio_init);
1778 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1779 MODULE_DESCRIPTION("Nomadik GPIO Driver");
1780 MODULE_LICENSE("GPL");