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/of_device.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf.h>
31 /* Since we request GPIOs from ourself */
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/mfd/dbx500-prcmu.h>
35 #include <asm/mach/irq.h>
37 #include <plat/pincfg.h>
38 #include <plat/gpio-nomadik.h>
40 #include "pinctrl-nomadik.h"
43 * The GPIO module in the Nomadik family of Systems-on-Chip is an
44 * AMBA device, managing 32 pins and alternate functions. The logic block
45 * is currently used in the Nomadik and ux500.
47 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
50 #define NMK_GPIO_PER_CHIP 32
52 struct nmk_gpio_chip {
53 struct gpio_chip chip;
54 struct irq_domain *domain;
58 unsigned int parent_irq;
59 int secondary_parent_irq;
60 u32 (*get_secondary_status)(unsigned int bank);
61 void (*set_ioforce)(bool enable);
64 /* Keep track of configured edges */
78 struct pinctrl_dev *pctl;
79 const struct nmk_pinctrl_soc_data *soc;
82 static struct nmk_gpio_chip *
83 nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
85 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
87 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
89 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
90 unsigned offset, int gpio_mode)
92 u32 bit = 1 << offset;
95 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
96 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
97 if (gpio_mode & NMK_GPIO_ALT_A)
99 if (gpio_mode & NMK_GPIO_ALT_B)
101 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
102 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
105 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
106 unsigned offset, enum nmk_gpio_slpm mode)
108 u32 bit = 1 << offset;
111 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
112 if (mode == NMK_GPIO_SLPM_NOCHANGE)
116 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
119 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
120 unsigned offset, enum nmk_gpio_pull pull)
122 u32 bit = 1 << offset;
125 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
126 if (pull == NMK_GPIO_PULL_NONE) {
128 nmk_chip->pull_up &= ~bit;
133 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
135 if (pull == NMK_GPIO_PULL_UP) {
136 nmk_chip->pull_up |= bit;
137 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
138 } else if (pull == NMK_GPIO_PULL_DOWN) {
139 nmk_chip->pull_up &= ~bit;
140 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
144 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
145 unsigned offset, bool lowemi)
147 u32 bit = BIT(offset);
148 bool enabled = nmk_chip->lowemi & bit;
150 if (lowemi == enabled)
154 nmk_chip->lowemi |= bit;
156 nmk_chip->lowemi &= ~bit;
158 writel_relaxed(nmk_chip->lowemi,
159 nmk_chip->addr + NMK_GPIO_LOWEMI);
162 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
165 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
168 static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
169 unsigned offset, int val)
172 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
177 static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
178 unsigned offset, int val)
180 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
181 __nmk_gpio_set_output(nmk_chip, offset, val);
184 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
185 unsigned offset, int gpio_mode,
188 u32 rwimsc = nmk_chip->rwimsc;
189 u32 fwimsc = nmk_chip->fwimsc;
191 if (glitch && nmk_chip->set_ioforce) {
192 u32 bit = BIT(offset);
194 /* Prevent spurious wakeups */
195 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
196 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
198 nmk_chip->set_ioforce(true);
201 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
203 if (glitch && nmk_chip->set_ioforce) {
204 nmk_chip->set_ioforce(false);
206 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
207 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
212 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
214 u32 falling = nmk_chip->fimsc & BIT(offset);
215 u32 rising = nmk_chip->rimsc & BIT(offset);
216 int gpio = nmk_chip->chip.base + offset;
217 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
218 struct irq_data *d = irq_get_irq_data(irq);
220 if (!rising && !falling)
223 if (!d || !irqd_irq_disabled(d))
227 nmk_chip->rimsc &= ~BIT(offset);
228 writel_relaxed(nmk_chip->rimsc,
229 nmk_chip->addr + NMK_GPIO_RIMSC);
233 nmk_chip->fimsc &= ~BIT(offset);
234 writel_relaxed(nmk_chip->fimsc,
235 nmk_chip->addr + NMK_GPIO_FIMSC);
238 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
241 static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
242 unsigned offset, unsigned alt_num)
248 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
249 const u16 *gpiocr_regs;
251 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
252 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
257 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
258 if (npct->soc->altcx_pins[i].pin == offset)
261 if (i == npct->soc->npins_altcx) {
262 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
267 pin_desc = npct->soc->altcx_pins + i;
268 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
271 * If alt_num is NULL, just clear current ALTCx selection
272 * to make sure we come back to a pure ALTC selection
275 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
276 if (pin_desc->altcx[i].used == true) {
277 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
278 bit = pin_desc->altcx[i].control_bit;
279 if (prcmu_read(reg) & BIT(bit)) {
280 prcmu_write_masked(reg, BIT(bit), 0);
282 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
290 alt_index = alt_num - 1;
291 if (pin_desc->altcx[alt_index].used == false) {
293 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
299 * Check if any other ALTCx functions are activated on this pin
300 * and disable it first.
302 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
305 if (pin_desc->altcx[i].used == true) {
306 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
307 bit = pin_desc->altcx[i].control_bit;
308 if (prcmu_read(reg) & BIT(bit)) {
309 prcmu_write_masked(reg, BIT(bit), 0);
311 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
317 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
318 bit = pin_desc->altcx[alt_index].control_bit;
319 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
320 offset, alt_index+1);
321 prcmu_write_masked(reg, BIT(bit), BIT(bit));
324 static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
325 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
327 static const char *afnames[] = {
328 [NMK_GPIO_ALT_GPIO] = "GPIO",
329 [NMK_GPIO_ALT_A] = "A",
330 [NMK_GPIO_ALT_B] = "B",
331 [NMK_GPIO_ALT_C] = "C"
333 static const char *pullnames[] = {
334 [NMK_GPIO_PULL_NONE] = "none",
335 [NMK_GPIO_PULL_UP] = "up",
336 [NMK_GPIO_PULL_DOWN] = "down",
337 [3] /* illegal */ = "??"
339 static const char *slpmnames[] = {
340 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
341 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
344 int pin = PIN_NUM(cfg);
345 int pull = PIN_PULL(cfg);
346 int af = PIN_ALT(cfg);
347 int slpm = PIN_SLPM(cfg);
348 int output = PIN_DIR(cfg);
349 int val = PIN_VAL(cfg);
350 bool glitch = af == NMK_GPIO_ALT_C;
352 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
353 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
354 output ? "output " : "input",
355 output ? (val ? "high" : "low") : "");
358 int slpm_pull = PIN_SLPM_PULL(cfg);
359 int slpm_output = PIN_SLPM_DIR(cfg);
360 int slpm_val = PIN_SLPM_VAL(cfg);
362 af = NMK_GPIO_ALT_GPIO;
365 * The SLPM_* values are normal values + 1 to allow zero to
366 * mean "same as normal".
369 pull = slpm_pull - 1;
371 output = slpm_output - 1;
375 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
377 slpm_pull ? pullnames[pull] : "same",
378 slpm_output ? (output ? "output" : "input") : "same",
379 slpm_val ? (val ? "high" : "low") : "same");
383 __nmk_gpio_make_output(nmk_chip, offset, val);
385 __nmk_gpio_make_input(nmk_chip, offset);
386 __nmk_gpio_set_pull(nmk_chip, offset, pull);
389 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
392 * If the pin is switching to altfunc, and there was an interrupt
393 * installed on it which has been lazy disabled, actually mask the
394 * interrupt to prevent spurious interrupts that would occur while the
395 * pin is under control of the peripheral. Only SKE does this.
397 if (af != NMK_GPIO_ALT_GPIO)
398 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
401 * If we've backed up the SLPM registers (glitch workaround), modify
402 * the backups since they will be restored.
405 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
406 slpmregs[nmk_chip->bank] |= BIT(offset);
408 slpmregs[nmk_chip->bank] &= ~BIT(offset);
410 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
412 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
416 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
417 * - Save SLPM registers
418 * - Set SLPM=0 for the IOs you want to switch and others to 1
419 * - Configure the GPIO registers for the IOs that are being switched
421 * - Modify the AFLSA/B registers for the IOs that are being switched
423 * - Restore SLPM registers
424 * - Any spurious wake up event during switch sequence to be ignored and
427 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
431 for (i = 0; i < NUM_BANKS; i++) {
432 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
433 unsigned int temp = slpm[i];
438 clk_enable(chip->clk);
440 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
441 writel(temp, chip->addr + NMK_GPIO_SLPC);
445 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
449 for (i = 0; i < NUM_BANKS; i++) {
450 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
455 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
457 clk_disable(chip->clk);
461 static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
463 static unsigned int slpm[NUM_BANKS];
469 for (i = 0; i < num; i++) {
470 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
476 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
479 memset(slpm, 0xff, sizeof(slpm));
481 for (i = 0; i < num; i++) {
482 int pin = PIN_NUM(cfgs[i]);
483 int offset = pin % NMK_GPIO_PER_CHIP;
485 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
486 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
489 nmk_gpio_glitch_slpm_init(slpm);
492 for (i = 0; i < num; i++) {
493 struct nmk_gpio_chip *nmk_chip;
494 int pin = PIN_NUM(cfgs[i]);
496 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
502 clk_enable(nmk_chip->clk);
503 spin_lock(&nmk_chip->lock);
504 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
505 cfgs[i], sleep, glitch ? slpm : NULL);
506 spin_unlock(&nmk_chip->lock);
507 clk_disable(nmk_chip->clk);
511 nmk_gpio_glitch_slpm_restore(slpm);
513 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
519 * nmk_config_pin - configure a pin's mux attributes
520 * @cfg: pin confguration
521 * @sleep: Non-zero to apply the sleep mode configuration
522 * Configures a pin's mode (alternate function or GPIO), its pull up status,
523 * and its sleep mode based on the specified configuration. The @cfg is
524 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
525 * are constructed using, and can be further enhanced with, the macros in
528 * If a pin's mode is set to GPIO, it is configured as an input to avoid
529 * side-effects. The gpio can be manipulated later using standard GPIO API
532 int nmk_config_pin(pin_cfg_t cfg, bool sleep)
534 return __nmk_config_pins(&cfg, 1, sleep);
536 EXPORT_SYMBOL(nmk_config_pin);
539 * nmk_config_pins - configure several pins at once
540 * @cfgs: array of pin configurations
541 * @num: number of elments in the array
543 * Configures several pins using nmk_config_pin(). Refer to that function for
544 * further information.
546 int nmk_config_pins(pin_cfg_t *cfgs, int num)
548 return __nmk_config_pins(cfgs, num, false);
550 EXPORT_SYMBOL(nmk_config_pins);
552 int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
554 return __nmk_config_pins(cfgs, num, true);
556 EXPORT_SYMBOL(nmk_config_pins_sleep);
559 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
561 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
563 * This register is actually in the pinmux layer, not the GPIO block itself.
564 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
565 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
566 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
567 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
568 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
569 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
571 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
572 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
573 * entered) regardless of the altfunction selected. Also wake-up detection is
576 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
577 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
578 * (for altfunction GPIO) or respective on-chip peripherals (for other
579 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
581 * Note that enable_irq_wake() will automatically enable wakeup detection.
583 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
585 struct nmk_gpio_chip *nmk_chip;
588 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
592 clk_enable(nmk_chip->clk);
593 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
594 spin_lock(&nmk_chip->lock);
596 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
598 spin_unlock(&nmk_chip->lock);
599 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
600 clk_disable(nmk_chip->clk);
606 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
608 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
610 * Enables/disables pull up/down on a specified pin. This only takes effect if
611 * the pin is configured as an input (either explicitly or by the alternate
614 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
615 * configured as an input. Otherwise, due to the way the controller registers
616 * work, this function will change the value output on the pin.
618 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
620 struct nmk_gpio_chip *nmk_chip;
623 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
627 clk_enable(nmk_chip->clk);
628 spin_lock_irqsave(&nmk_chip->lock, flags);
629 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
630 spin_unlock_irqrestore(&nmk_chip->lock, flags);
631 clk_disable(nmk_chip->clk);
638 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
640 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
641 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
643 * Sets the mode of the specified pin to one of the alternate functions or
646 int nmk_gpio_set_mode(int gpio, int gpio_mode)
648 struct nmk_gpio_chip *nmk_chip;
651 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
655 clk_enable(nmk_chip->clk);
656 spin_lock_irqsave(&nmk_chip->lock, flags);
657 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
658 spin_unlock_irqrestore(&nmk_chip->lock, flags);
659 clk_disable(nmk_chip->clk);
663 EXPORT_SYMBOL(nmk_gpio_set_mode);
665 int nmk_gpio_get_mode(int gpio)
667 struct nmk_gpio_chip *nmk_chip;
668 u32 afunc, bfunc, bit;
670 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
674 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
676 clk_enable(nmk_chip->clk);
678 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
679 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
681 clk_disable(nmk_chip->clk);
683 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
685 EXPORT_SYMBOL(nmk_gpio_get_mode);
689 static inline int nmk_gpio_get_bitmask(int gpio)
691 return 1 << (gpio % NMK_GPIO_PER_CHIP);
694 static void nmk_gpio_irq_ack(struct irq_data *d)
696 struct nmk_gpio_chip *nmk_chip;
698 nmk_chip = irq_data_get_irq_chip_data(d);
702 clk_enable(nmk_chip->clk);
703 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
704 clk_disable(nmk_chip->clk);
707 enum nmk_gpio_irq_type {
712 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
713 int gpio, enum nmk_gpio_irq_type which,
716 u32 bitmask = nmk_gpio_get_bitmask(gpio);
722 if (which == NORMAL) {
723 rimscreg = NMK_GPIO_RIMSC;
724 fimscreg = NMK_GPIO_FIMSC;
725 rimscval = &nmk_chip->rimsc;
726 fimscval = &nmk_chip->fimsc;
728 rimscreg = NMK_GPIO_RWIMSC;
729 fimscreg = NMK_GPIO_FWIMSC;
730 rimscval = &nmk_chip->rwimsc;
731 fimscval = &nmk_chip->fwimsc;
734 /* we must individually set/clear the two edges */
735 if (nmk_chip->edge_rising & bitmask) {
737 *rimscval |= bitmask;
739 *rimscval &= ~bitmask;
740 writel(*rimscval, nmk_chip->addr + rimscreg);
742 if (nmk_chip->edge_falling & bitmask) {
744 *fimscval |= bitmask;
746 *fimscval &= ~bitmask;
747 writel(*fimscval, nmk_chip->addr + fimscreg);
751 static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
755 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
756 * disabled, since setting SLPM to 1 increases power consumption, and
757 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
759 if (nmk_chip->sleepmode && on) {
760 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
761 NMK_GPIO_SLPM_WAKEUP_ENABLE);
764 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
767 static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
769 struct nmk_gpio_chip *nmk_chip;
773 nmk_chip = irq_data_get_irq_chip_data(d);
774 bitmask = nmk_gpio_get_bitmask(d->hwirq);
778 clk_enable(nmk_chip->clk);
779 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
780 spin_lock(&nmk_chip->lock);
782 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
784 if (!(nmk_chip->real_wake & bitmask))
785 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
787 spin_unlock(&nmk_chip->lock);
788 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
789 clk_disable(nmk_chip->clk);
794 static void nmk_gpio_irq_mask(struct irq_data *d)
796 nmk_gpio_irq_maskunmask(d, false);
799 static void nmk_gpio_irq_unmask(struct irq_data *d)
801 nmk_gpio_irq_maskunmask(d, true);
804 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
806 struct nmk_gpio_chip *nmk_chip;
810 nmk_chip = irq_data_get_irq_chip_data(d);
813 bitmask = nmk_gpio_get_bitmask(d->hwirq);
815 clk_enable(nmk_chip->clk);
816 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
817 spin_lock(&nmk_chip->lock);
819 if (irqd_irq_disabled(d))
820 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
823 nmk_chip->real_wake |= bitmask;
825 nmk_chip->real_wake &= ~bitmask;
827 spin_unlock(&nmk_chip->lock);
828 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
829 clk_disable(nmk_chip->clk);
834 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
836 bool enabled = !irqd_irq_disabled(d);
837 bool wake = irqd_is_wakeup_set(d);
838 struct nmk_gpio_chip *nmk_chip;
842 nmk_chip = irq_data_get_irq_chip_data(d);
843 bitmask = nmk_gpio_get_bitmask(d->hwirq);
846 if (type & IRQ_TYPE_LEVEL_HIGH)
848 if (type & IRQ_TYPE_LEVEL_LOW)
851 clk_enable(nmk_chip->clk);
852 spin_lock_irqsave(&nmk_chip->lock, flags);
855 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
858 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
860 nmk_chip->edge_rising &= ~bitmask;
861 if (type & IRQ_TYPE_EDGE_RISING)
862 nmk_chip->edge_rising |= bitmask;
864 nmk_chip->edge_falling &= ~bitmask;
865 if (type & IRQ_TYPE_EDGE_FALLING)
866 nmk_chip->edge_falling |= bitmask;
869 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
872 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
874 spin_unlock_irqrestore(&nmk_chip->lock, flags);
875 clk_disable(nmk_chip->clk);
880 static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
882 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
884 clk_enable(nmk_chip->clk);
885 nmk_gpio_irq_unmask(d);
889 static void nmk_gpio_irq_shutdown(struct irq_data *d)
891 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
893 nmk_gpio_irq_mask(d);
894 clk_disable(nmk_chip->clk);
897 static struct irq_chip nmk_gpio_irq_chip = {
898 .name = "Nomadik-GPIO",
899 .irq_ack = nmk_gpio_irq_ack,
900 .irq_mask = nmk_gpio_irq_mask,
901 .irq_unmask = nmk_gpio_irq_unmask,
902 .irq_set_type = nmk_gpio_irq_set_type,
903 .irq_set_wake = nmk_gpio_irq_set_wake,
904 .irq_startup = nmk_gpio_irq_startup,
905 .irq_shutdown = nmk_gpio_irq_shutdown,
906 .flags = IRQCHIP_MASK_ON_SUSPEND,
909 static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
912 struct nmk_gpio_chip *nmk_chip;
913 struct irq_chip *host_chip = irq_get_chip(irq);
915 chained_irq_enter(host_chip, desc);
917 nmk_chip = irq_get_handler_data(irq);
919 int bit = __ffs(status);
921 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
925 chained_irq_exit(host_chip, desc);
928 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
930 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
933 clk_enable(nmk_chip->clk);
934 status = readl(nmk_chip->addr + NMK_GPIO_IS);
935 clk_disable(nmk_chip->clk);
937 __nmk_gpio_irq_handler(irq, desc, status);
940 static void nmk_gpio_secondary_irq_handler(unsigned int irq,
941 struct irq_desc *desc)
943 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
944 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
946 __nmk_gpio_irq_handler(irq, desc, status);
949 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
951 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
952 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
954 if (nmk_chip->secondary_parent_irq >= 0) {
955 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
956 nmk_gpio_secondary_irq_handler);
957 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
965 static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
968 * Map back to global GPIO space and request muxing, the direction
969 * parameter does not matter for this controller.
971 int gpio = chip->base + offset;
973 return pinctrl_request_gpio(gpio);
976 static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
978 int gpio = chip->base + offset;
980 pinctrl_free_gpio(gpio);
983 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
985 struct nmk_gpio_chip *nmk_chip =
986 container_of(chip, struct nmk_gpio_chip, chip);
988 clk_enable(nmk_chip->clk);
990 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
992 clk_disable(nmk_chip->clk);
997 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
999 struct nmk_gpio_chip *nmk_chip =
1000 container_of(chip, struct nmk_gpio_chip, chip);
1001 u32 bit = 1 << offset;
1004 clk_enable(nmk_chip->clk);
1006 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1008 clk_disable(nmk_chip->clk);
1013 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1016 struct nmk_gpio_chip *nmk_chip =
1017 container_of(chip, struct nmk_gpio_chip, chip);
1019 clk_enable(nmk_chip->clk);
1021 __nmk_gpio_set_output(nmk_chip, offset, val);
1023 clk_disable(nmk_chip->clk);
1026 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1029 struct nmk_gpio_chip *nmk_chip =
1030 container_of(chip, struct nmk_gpio_chip, chip);
1032 clk_enable(nmk_chip->clk);
1034 __nmk_gpio_make_output(nmk_chip, offset, val);
1036 clk_disable(nmk_chip->clk);
1041 static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1043 struct nmk_gpio_chip *nmk_chip =
1044 container_of(chip, struct nmk_gpio_chip, chip);
1046 return irq_find_mapping(nmk_chip->domain, offset);
1049 #ifdef CONFIG_DEBUG_FS
1051 #include <linux/seq_file.h>
1053 static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
1054 unsigned offset, unsigned gpio)
1056 const char *label = gpiochip_is_requested(chip, offset);
1057 struct nmk_gpio_chip *nmk_chip =
1058 container_of(chip, struct nmk_gpio_chip, chip);
1062 u32 bit = 1 << offset;
1063 const char *modes[] = {
1064 [NMK_GPIO_ALT_GPIO] = "gpio",
1065 [NMK_GPIO_ALT_A] = "altA",
1066 [NMK_GPIO_ALT_B] = "altB",
1067 [NMK_GPIO_ALT_C] = "altC",
1070 clk_enable(nmk_chip->clk);
1071 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1072 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1073 mode = nmk_gpio_get_mode(gpio);
1075 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1076 gpio, label ?: "(none)",
1077 is_out ? "out" : "in ",
1079 ? (chip->get(chip, offset) ? "hi" : "lo")
1081 (mode < 0) ? "unknown" : modes[mode],
1082 pull ? "pull" : "none");
1084 if (label && !is_out) {
1085 int irq = gpio_to_irq(gpio);
1086 struct irq_desc *desc = irq_to_desc(irq);
1088 /* This races with request_irq(), set_irq_type(),
1089 * and set_irq_wake() ... but those are "rare".
1091 if (irq >= 0 && desc->action) {
1093 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1095 if (nmk_chip->edge_rising & bitmask)
1096 trigger = "edge-rising";
1097 else if (nmk_chip->edge_falling & bitmask)
1098 trigger = "edge-falling";
1100 trigger = "edge-undefined";
1102 seq_printf(s, " irq-%d %s%s",
1104 irqd_is_wakeup_set(&desc->irq_data)
1108 clk_disable(nmk_chip->clk);
1111 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1114 unsigned gpio = chip->base;
1116 for (i = 0; i < chip->ngpio; i++, gpio++) {
1117 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1118 seq_printf(s, "\n");
1123 static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1124 struct gpio_chip *chip,
1125 unsigned offset, unsigned gpio)
1128 #define nmk_gpio_dbg_show NULL
1131 /* This structure is replicated for each GPIO block allocated at probe time */
1132 static struct gpio_chip nmk_gpio_template = {
1133 .request = nmk_gpio_request,
1134 .free = nmk_gpio_free,
1135 .direction_input = nmk_gpio_make_input,
1136 .get = nmk_gpio_get_input,
1137 .direction_output = nmk_gpio_make_output,
1138 .set = nmk_gpio_set_output,
1139 .to_irq = nmk_gpio_to_irq,
1140 .dbg_show = nmk_gpio_dbg_show,
1144 void nmk_gpio_clocks_enable(void)
1148 for (i = 0; i < NUM_BANKS; i++) {
1149 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1154 clk_enable(chip->clk);
1158 void nmk_gpio_clocks_disable(void)
1162 for (i = 0; i < NUM_BANKS; i++) {
1163 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1168 clk_disable(chip->clk);
1173 * Called from the suspend/resume path to only keep the real wakeup interrupts
1174 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1175 * and not the rest of the interrupts which we needed to have as wakeups for
1178 * PM ops are not used since this needs to be done at the end, after all the
1179 * other drivers are done with their suspend callbacks.
1181 void nmk_gpio_wakeups_suspend(void)
1185 for (i = 0; i < NUM_BANKS; i++) {
1186 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1191 clk_enable(chip->clk);
1193 writel(chip->rwimsc & chip->real_wake,
1194 chip->addr + NMK_GPIO_RWIMSC);
1195 writel(chip->fwimsc & chip->real_wake,
1196 chip->addr + NMK_GPIO_FWIMSC);
1198 clk_disable(chip->clk);
1202 void nmk_gpio_wakeups_resume(void)
1206 for (i = 0; i < NUM_BANKS; i++) {
1207 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1212 clk_enable(chip->clk);
1214 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1215 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1217 clk_disable(chip->clk);
1222 * Read the pull up/pull down status.
1223 * A bit set in 'pull_up' means that pull up
1224 * is selected if pull is enabled in PDIS register.
1225 * Note: only pull up/down set via this driver can
1226 * be detected due to HW limitations.
1228 void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1230 if (gpio_bank < NUM_BANKS) {
1231 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1236 *pull_up = chip->pull_up;
1240 int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1241 irq_hw_number_t hwirq)
1243 struct nmk_gpio_chip *nmk_chip = d->host_data;
1248 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1249 set_irq_flags(irq, IRQF_VALID);
1250 irq_set_chip_data(irq, nmk_chip);
1251 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1256 const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1257 .map = nmk_gpio_irq_map,
1258 .xlate = irq_domain_xlate_twocell,
1261 static int __devinit nmk_gpio_probe(struct platform_device *dev)
1263 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
1264 struct device_node *np = dev->dev.of_node;
1265 struct nmk_gpio_chip *nmk_chip;
1266 struct gpio_chip *chip;
1267 struct resource *res;
1274 if (!pdata && !np) {
1275 dev_err(&dev->dev, "No platform data or device tree found\n");
1280 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
1284 if (of_get_property(np, "st,supports-sleepmode", NULL))
1285 pdata->supports_sleepmode = true;
1287 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1288 dev_err(&dev->dev, "gpio-bank property not found\n");
1293 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1294 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1297 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1303 irq = platform_get_irq(dev, 0);
1309 secondary_irq = platform_get_irq(dev, 1);
1310 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1315 base = devm_request_and_ioremap(&dev->dev, res);
1321 clk = devm_clk_get(&dev->dev, NULL);
1328 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
1335 * The virt address in nmk_chip->addr is in the nomadik register space,
1336 * so we can simply convert the resource address, without remapping
1338 nmk_chip->bank = dev->id;
1339 nmk_chip->clk = clk;
1340 nmk_chip->addr = base;
1341 nmk_chip->chip = nmk_gpio_template;
1342 nmk_chip->parent_irq = irq;
1343 nmk_chip->secondary_parent_irq = secondary_irq;
1344 nmk_chip->get_secondary_status = pdata->get_secondary_status;
1345 nmk_chip->set_ioforce = pdata->set_ioforce;
1346 nmk_chip->sleepmode = pdata->supports_sleepmode;
1347 spin_lock_init(&nmk_chip->lock);
1349 chip = &nmk_chip->chip;
1350 chip->base = pdata->first_gpio;
1351 chip->ngpio = pdata->num_gpio;
1352 chip->label = pdata->name ?: dev_name(&dev->dev);
1353 chip->dev = &dev->dev;
1354 chip->owner = THIS_MODULE;
1356 clk_enable(nmk_chip->clk);
1357 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1358 clk_disable(nmk_chip->clk);
1360 #ifdef CONFIG_OF_GPIO
1364 ret = gpiochip_add(&nmk_chip->chip);
1368 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1370 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
1372 platform_set_drvdata(dev, nmk_chip);
1375 /* The DT case will just grab a set of IRQ numbers */
1376 nmk_chip->domain = irq_domain_add_linear(np, NMK_GPIO_PER_CHIP,
1377 &nmk_gpio_irq_simple_ops, nmk_chip);
1379 /* Non-DT legacy mode, use hardwired IRQ numbers */
1382 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
1383 nmk_chip->domain = irq_domain_add_simple(NULL,
1384 NMK_GPIO_PER_CHIP, irq_start,
1385 &nmk_gpio_irq_simple_ops, nmk_chip);
1387 if (!nmk_chip->domain) {
1388 dev_err(&dev->dev, "failed to create irqdomain\n");
1393 nmk_gpio_init_irq(nmk_chip);
1395 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1400 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1401 pdata->first_gpio, pdata->first_gpio+31);
1406 static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1408 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1410 return npct->soc->ngroups;
1413 static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1416 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1418 return npct->soc->groups[selector].name;
1421 static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1422 const unsigned **pins,
1425 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1427 *pins = npct->soc->groups[selector].pins;
1428 *num_pins = npct->soc->groups[selector].npins;
1432 static struct pinctrl_gpio_range *
1433 nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1435 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1438 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1439 struct pinctrl_gpio_range *range;
1441 range = &npct->soc->gpio_ranges[i];
1442 if (offset >= range->pin_base &&
1443 offset <= (range->pin_base + range->npins - 1))
1449 static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1452 struct pinctrl_gpio_range *range;
1453 struct gpio_chip *chip;
1455 range = nmk_match_gpio_range(pctldev, offset);
1456 if (!range || !range->gc) {
1457 seq_printf(s, "invalid pin offset");
1461 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
1464 static struct pinctrl_ops nmk_pinctrl_ops = {
1465 .get_groups_count = nmk_get_groups_cnt,
1466 .get_group_name = nmk_get_group_name,
1467 .get_group_pins = nmk_get_group_pins,
1468 .pin_dbg_show = nmk_pin_dbg_show,
1471 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1473 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1475 return npct->soc->nfunctions;
1478 static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1481 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1483 return npct->soc->functions[function].name;
1486 static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1488 const char * const **groups,
1489 unsigned * const num_groups)
1491 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1493 *groups = npct->soc->functions[function].groups;
1494 *num_groups = npct->soc->functions[function].ngroups;
1499 static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1502 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1503 const struct nmk_pingroup *g;
1504 static unsigned int slpm[NUM_BANKS];
1505 unsigned long flags;
1510 g = &npct->soc->groups[group];
1512 if (g->altsetting < 0)
1515 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1518 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1519 * we may pass through an undesired state. In this case we take
1522 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1523 * - Save SLPM registers (since we have a shadow register in the
1524 * nmk_chip we're using that as backup)
1525 * - Set SLPM=0 for the IOs you want to switch and others to 1
1526 * - Configure the GPIO registers for the IOs that are being switched
1528 * - Modify the AFLSA/B registers for the IOs that are being switched
1530 * - Restore SLPM registers
1531 * - Any spurious wake up event during switch sequence to be ignored
1534 * We REALLY need to save ALL slpm registers, because the external
1535 * IOFORCE will switch *all* ports to their sleepmode setting to as
1536 * to avoid glitches. (Not just one port!)
1538 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
1541 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1543 /* Initially don't put any pins to sleep when switching */
1544 memset(slpm, 0xff, sizeof(slpm));
1547 * Then mask the pins that need to be sleeping now when we're
1548 * switching to the ALT C function.
1550 for (i = 0; i < g->npins; i++)
1551 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1552 nmk_gpio_glitch_slpm_init(slpm);
1555 for (i = 0; i < g->npins; i++) {
1556 struct pinctrl_gpio_range *range;
1557 struct nmk_gpio_chip *nmk_chip;
1558 struct gpio_chip *chip;
1561 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1564 "invalid pin offset %d in group %s at index %d\n",
1565 g->pins[i], g->name, i);
1569 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1570 g->pins[i], g->name, i);
1574 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1575 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1577 clk_enable(nmk_chip->clk);
1578 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1580 * If the pin is switching to altfunc, and there was an
1581 * interrupt installed on it which has been lazy disabled,
1582 * actually mask the interrupt to prevent spurious interrupts
1583 * that would occur while the pin is under control of the
1584 * peripheral. Only SKE does this.
1586 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1588 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1589 (g->altsetting & NMK_GPIO_ALT_C), glitch);
1590 clk_disable(nmk_chip->clk);
1593 * Call PRCM GPIOCR config function in case ALTC
1594 * has been selected:
1595 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1597 * - If selection is pure ALTC and previous selection was ALTCx,
1598 * then some bits in PRCM GPIOCR registers must be cleared.
1600 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1601 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1602 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
1605 /* When all pins are successfully reconfigured we get here */
1610 nmk_gpio_glitch_slpm_restore(slpm);
1611 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1617 static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1618 unsigned function, unsigned group)
1620 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1621 const struct nmk_pingroup *g;
1623 g = &npct->soc->groups[group];
1625 if (g->altsetting < 0)
1628 /* Poke out the mux, set the pin to some default state? */
1629 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1632 int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1633 struct pinctrl_gpio_range *range,
1636 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1637 struct nmk_gpio_chip *nmk_chip;
1638 struct gpio_chip *chip;
1642 dev_err(npct->dev, "invalid range\n");
1646 dev_err(npct->dev, "missing GPIO chip in range\n");
1650 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1652 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1654 clk_enable(nmk_chip->clk);
1655 bit = offset % NMK_GPIO_PER_CHIP;
1656 /* There is no glitch when converting any pin to GPIO */
1657 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1658 clk_disable(nmk_chip->clk);
1663 void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1664 struct pinctrl_gpio_range *range,
1667 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1669 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1670 /* Set the pin to some default state, GPIO is usually default */
1673 static struct pinmux_ops nmk_pinmux_ops = {
1674 .get_functions_count = nmk_pmx_get_funcs_cnt,
1675 .get_function_name = nmk_pmx_get_func_name,
1676 .get_function_groups = nmk_pmx_get_func_groups,
1677 .enable = nmk_pmx_enable,
1678 .disable = nmk_pmx_disable,
1679 .gpio_request_enable = nmk_gpio_request_enable,
1680 .gpio_disable_free = nmk_gpio_disable_free,
1683 int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1685 unsigned long *config)
1687 /* Not implemented */
1691 int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1693 unsigned long config)
1695 static const char *pullnames[] = {
1696 [NMK_GPIO_PULL_NONE] = "none",
1697 [NMK_GPIO_PULL_UP] = "up",
1698 [NMK_GPIO_PULL_DOWN] = "down",
1699 [3] /* illegal */ = "??"
1701 static const char *slpmnames[] = {
1702 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1703 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1705 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1706 struct nmk_gpio_chip *nmk_chip;
1707 struct pinctrl_gpio_range *range;
1708 struct gpio_chip *chip;
1712 * The pin config contains pin number and altfunction fields, here
1713 * we just ignore that part. It's being handled by the framework and
1714 * pinmux callback respectively.
1716 pin_cfg_t cfg = (pin_cfg_t) config;
1717 int pull = PIN_PULL(cfg);
1718 int slpm = PIN_SLPM(cfg);
1719 int output = PIN_DIR(cfg);
1720 int val = PIN_VAL(cfg);
1721 bool lowemi = PIN_LOWEMI(cfg);
1722 bool gpiomode = PIN_GPIOMODE(cfg);
1723 bool sleep = PIN_SLEEPMODE(cfg);
1725 range = nmk_match_gpio_range(pctldev, pin);
1727 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1731 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1736 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1739 int slpm_pull = PIN_SLPM_PULL(cfg);
1740 int slpm_output = PIN_SLPM_DIR(cfg);
1741 int slpm_val = PIN_SLPM_VAL(cfg);
1743 /* All pins go into GPIO mode at sleep */
1747 * The SLPM_* values are normal values + 1 to allow zero to
1748 * mean "same as normal".
1751 pull = slpm_pull - 1;
1753 output = slpm_output - 1;
1757 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1759 slpm_pull ? pullnames[pull] : "same",
1760 slpm_output ? (output ? "output" : "input") : "same",
1761 slpm_val ? (val ? "high" : "low") : "same");
1764 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1765 pin, cfg, pullnames[pull], slpmnames[slpm],
1766 output ? "output " : "input",
1767 output ? (val ? "high" : "low") : "",
1768 lowemi ? "on" : "off" );
1770 clk_enable(nmk_chip->clk);
1771 bit = pin % NMK_GPIO_PER_CHIP;
1773 /* No glitch when going to GPIO mode */
1774 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1776 __nmk_gpio_make_output(nmk_chip, bit, val);
1778 __nmk_gpio_make_input(nmk_chip, bit);
1779 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1781 /* TODO: isn't this only applicable on output pins? */
1782 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1784 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1785 clk_disable(nmk_chip->clk);
1789 static struct pinconf_ops nmk_pinconf_ops = {
1790 .pin_config_get = nmk_pin_config_get,
1791 .pin_config_set = nmk_pin_config_set,
1794 static struct pinctrl_desc nmk_pinctrl_desc = {
1795 .name = "pinctrl-nomadik",
1796 .pctlops = &nmk_pinctrl_ops,
1797 .pmxops = &nmk_pinmux_ops,
1798 .confops = &nmk_pinconf_ops,
1799 .owner = THIS_MODULE,
1802 static const struct of_device_id nmk_pinctrl_match[] = {
1804 .compatible = "stericsson,nmk_pinctrl",
1805 .data = (void *)PINCTRL_NMK_DB8500,
1810 static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1812 const struct platform_device_id *platid = platform_get_device_id(pdev);
1813 struct device_node *np = pdev->dev.of_node;
1814 struct nmk_pinctrl *npct;
1815 unsigned int version = 0;
1818 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1823 version = platid->driver_data;
1825 version = (unsigned int)
1826 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1828 /* Poke in other ASIC variants here */
1829 if (version == PINCTRL_NMK_STN8815)
1830 nmk_pinctrl_stn8815_init(&npct->soc);
1831 if (version == PINCTRL_NMK_DB8500)
1832 nmk_pinctrl_db8500_init(&npct->soc);
1833 if (version == PINCTRL_NMK_DB8540)
1834 nmk_pinctrl_db8540_init(&npct->soc);
1837 * We need all the GPIO drivers to probe FIRST, or we will not be able
1838 * to obtain references to the struct gpio_chip * for them, and we
1839 * need this to proceed.
1841 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1842 if (!nmk_gpio_chips[i]) {
1843 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
1844 return -EPROBE_DEFER;
1846 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1849 nmk_pinctrl_desc.pins = npct->soc->pins;
1850 nmk_pinctrl_desc.npins = npct->soc->npins;
1851 npct->dev = &pdev->dev;
1852 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1854 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1858 /* We will handle a range of GPIO pins */
1859 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1860 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1862 platform_set_drvdata(pdev, npct);
1863 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1868 static const struct of_device_id nmk_gpio_match[] = {
1869 { .compatible = "st,nomadik-gpio", },
1873 static struct platform_driver nmk_gpio_driver = {
1875 .owner = THIS_MODULE,
1877 .of_match_table = nmk_gpio_match,
1879 .probe = nmk_gpio_probe,
1882 static const struct platform_device_id nmk_pinctrl_id[] = {
1883 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1884 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
1885 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
1888 static struct platform_driver nmk_pinctrl_driver = {
1890 .owner = THIS_MODULE,
1891 .name = "pinctrl-nomadik",
1892 .of_match_table = nmk_pinctrl_match,
1894 .probe = nmk_pinctrl_probe,
1895 .id_table = nmk_pinctrl_id,
1898 static int __init nmk_gpio_init(void)
1902 ret = platform_driver_register(&nmk_gpio_driver);
1905 return platform_driver_register(&nmk_pinctrl_driver);
1908 core_initcall(nmk_gpio_init);
1910 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1911 MODULE_DESCRIPTION("Nomadik GPIO Driver");
1912 MODULE_LICENSE("GPL");