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-2013 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/init.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/of_device.h>
25 #include <linux/of_address.h>
26 #include <linux/bitops.h>
27 #include <linux/pinctrl/machine.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 "pinctrl-nomadik.h"
35 #include "../pinctrl-utils.h"
38 * The GPIO module in the Nomadik family of Systems-on-Chip is an
39 * AMBA device, managing 32 pins and alternate functions. The logic block
40 * is currently used in the Nomadik and ux500.
42 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
46 * pin configurations are represented by 32-bit integers:
48 * bit 0.. 8 - Pin Number (512 Pins Maximum)
49 * bit 9..10 - Alternate Function Selection
50 * bit 11..12 - Pull up/down state
51 * bit 13 - Sleep mode behaviour
53 * bit 15 - Value (if output)
54 * bit 16..18 - SLPM pull up/down state
55 * bit 19..20 - SLPM direction
56 * bit 21..22 - SLPM Value (if output)
57 * bit 23..25 - PDIS value (if input)
61 * to facilitate the definition, the following macros are provided
63 * PIN_CFG_DEFAULT - default config (0):
64 * pull up/down = disabled
65 * sleep mode = input/wakeup
68 * SLPM direction = same as normal
69 * SLPM pull = same as normal
70 * SLPM value = same as normal
72 * PIN_CFG - default config with alternate function
75 typedef unsigned long pin_cfg_t
;
77 #define PIN_NUM_MASK 0x1ff
78 #define PIN_NUM(x) ((x) & PIN_NUM_MASK)
80 #define PIN_ALT_SHIFT 9
81 #define PIN_ALT_MASK (0x3 << PIN_ALT_SHIFT)
82 #define PIN_ALT(x) (((x) & PIN_ALT_MASK) >> PIN_ALT_SHIFT)
83 #define PIN_GPIO (NMK_GPIO_ALT_GPIO << PIN_ALT_SHIFT)
84 #define PIN_ALT_A (NMK_GPIO_ALT_A << PIN_ALT_SHIFT)
85 #define PIN_ALT_B (NMK_GPIO_ALT_B << PIN_ALT_SHIFT)
86 #define PIN_ALT_C (NMK_GPIO_ALT_C << PIN_ALT_SHIFT)
88 #define PIN_PULL_SHIFT 11
89 #define PIN_PULL_MASK (0x3 << PIN_PULL_SHIFT)
90 #define PIN_PULL(x) (((x) & PIN_PULL_MASK) >> PIN_PULL_SHIFT)
91 #define PIN_PULL_NONE (NMK_GPIO_PULL_NONE << PIN_PULL_SHIFT)
92 #define PIN_PULL_UP (NMK_GPIO_PULL_UP << PIN_PULL_SHIFT)
93 #define PIN_PULL_DOWN (NMK_GPIO_PULL_DOWN << PIN_PULL_SHIFT)
95 #define PIN_SLPM_SHIFT 13
96 #define PIN_SLPM_MASK (0x1 << PIN_SLPM_SHIFT)
97 #define PIN_SLPM(x) (((x) & PIN_SLPM_MASK) >> PIN_SLPM_SHIFT)
98 #define PIN_SLPM_MAKE_INPUT (NMK_GPIO_SLPM_INPUT << PIN_SLPM_SHIFT)
99 #define PIN_SLPM_NOCHANGE (NMK_GPIO_SLPM_NOCHANGE << PIN_SLPM_SHIFT)
100 /* These two replace the above in DB8500v2+ */
101 #define PIN_SLPM_WAKEUP_ENABLE (NMK_GPIO_SLPM_WAKEUP_ENABLE << PIN_SLPM_SHIFT)
102 #define PIN_SLPM_WAKEUP_DISABLE (NMK_GPIO_SLPM_WAKEUP_DISABLE << PIN_SLPM_SHIFT)
103 #define PIN_SLPM_USE_MUX_SETTINGS_IN_SLEEP PIN_SLPM_WAKEUP_DISABLE
105 #define PIN_SLPM_GPIO PIN_SLPM_WAKEUP_ENABLE /* In SLPM, pin is a gpio */
106 #define PIN_SLPM_ALTFUNC PIN_SLPM_WAKEUP_DISABLE /* In SLPM, pin is altfunc */
108 #define PIN_DIR_SHIFT 14
109 #define PIN_DIR_MASK (0x1 << PIN_DIR_SHIFT)
110 #define PIN_DIR(x) (((x) & PIN_DIR_MASK) >> PIN_DIR_SHIFT)
111 #define PIN_DIR_INPUT (0 << PIN_DIR_SHIFT)
112 #define PIN_DIR_OUTPUT (1 << PIN_DIR_SHIFT)
114 #define PIN_VAL_SHIFT 15
115 #define PIN_VAL_MASK (0x1 << PIN_VAL_SHIFT)
116 #define PIN_VAL(x) (((x) & PIN_VAL_MASK) >> PIN_VAL_SHIFT)
117 #define PIN_VAL_LOW (0 << PIN_VAL_SHIFT)
118 #define PIN_VAL_HIGH (1 << PIN_VAL_SHIFT)
120 #define PIN_SLPM_PULL_SHIFT 16
121 #define PIN_SLPM_PULL_MASK (0x7 << PIN_SLPM_PULL_SHIFT)
122 #define PIN_SLPM_PULL(x) \
123 (((x) & PIN_SLPM_PULL_MASK) >> PIN_SLPM_PULL_SHIFT)
124 #define PIN_SLPM_PULL_NONE \
125 ((1 + NMK_GPIO_PULL_NONE) << PIN_SLPM_PULL_SHIFT)
126 #define PIN_SLPM_PULL_UP \
127 ((1 + NMK_GPIO_PULL_UP) << PIN_SLPM_PULL_SHIFT)
128 #define PIN_SLPM_PULL_DOWN \
129 ((1 + NMK_GPIO_PULL_DOWN) << PIN_SLPM_PULL_SHIFT)
131 #define PIN_SLPM_DIR_SHIFT 19
132 #define PIN_SLPM_DIR_MASK (0x3 << PIN_SLPM_DIR_SHIFT)
133 #define PIN_SLPM_DIR(x) \
134 (((x) & PIN_SLPM_DIR_MASK) >> PIN_SLPM_DIR_SHIFT)
135 #define PIN_SLPM_DIR_INPUT ((1 + 0) << PIN_SLPM_DIR_SHIFT)
136 #define PIN_SLPM_DIR_OUTPUT ((1 + 1) << PIN_SLPM_DIR_SHIFT)
138 #define PIN_SLPM_VAL_SHIFT 21
139 #define PIN_SLPM_VAL_MASK (0x3 << PIN_SLPM_VAL_SHIFT)
140 #define PIN_SLPM_VAL(x) \
141 (((x) & PIN_SLPM_VAL_MASK) >> PIN_SLPM_VAL_SHIFT)
142 #define PIN_SLPM_VAL_LOW ((1 + 0) << PIN_SLPM_VAL_SHIFT)
143 #define PIN_SLPM_VAL_HIGH ((1 + 1) << PIN_SLPM_VAL_SHIFT)
145 #define PIN_SLPM_PDIS_SHIFT 23
146 #define PIN_SLPM_PDIS_MASK (0x3 << PIN_SLPM_PDIS_SHIFT)
147 #define PIN_SLPM_PDIS(x) \
148 (((x) & PIN_SLPM_PDIS_MASK) >> PIN_SLPM_PDIS_SHIFT)
149 #define PIN_SLPM_PDIS_NO_CHANGE (0 << PIN_SLPM_PDIS_SHIFT)
150 #define PIN_SLPM_PDIS_DISABLED (1 << PIN_SLPM_PDIS_SHIFT)
151 #define PIN_SLPM_PDIS_ENABLED (2 << PIN_SLPM_PDIS_SHIFT)
153 #define PIN_LOWEMI_SHIFT 25
154 #define PIN_LOWEMI_MASK (0x1 << PIN_LOWEMI_SHIFT)
155 #define PIN_LOWEMI(x) (((x) & PIN_LOWEMI_MASK) >> PIN_LOWEMI_SHIFT)
156 #define PIN_LOWEMI_DISABLED (0 << PIN_LOWEMI_SHIFT)
157 #define PIN_LOWEMI_ENABLED (1 << PIN_LOWEMI_SHIFT)
159 #define PIN_GPIOMODE_SHIFT 26
160 #define PIN_GPIOMODE_MASK (0x1 << PIN_GPIOMODE_SHIFT)
161 #define PIN_GPIOMODE(x) (((x) & PIN_GPIOMODE_MASK) >> PIN_GPIOMODE_SHIFT)
162 #define PIN_GPIOMODE_DISABLED (0 << PIN_GPIOMODE_SHIFT)
163 #define PIN_GPIOMODE_ENABLED (1 << PIN_GPIOMODE_SHIFT)
165 #define PIN_SLEEPMODE_SHIFT 27
166 #define PIN_SLEEPMODE_MASK (0x1 << PIN_SLEEPMODE_SHIFT)
167 #define PIN_SLEEPMODE(x) (((x) & PIN_SLEEPMODE_MASK) >> PIN_SLEEPMODE_SHIFT)
168 #define PIN_SLEEPMODE_DISABLED (0 << PIN_SLEEPMODE_SHIFT)
169 #define PIN_SLEEPMODE_ENABLED (1 << PIN_SLEEPMODE_SHIFT)
172 /* Shortcuts. Use these instead of separate DIR, PULL, and VAL. */
173 #define PIN_INPUT_PULLDOWN (PIN_DIR_INPUT | PIN_PULL_DOWN)
174 #define PIN_INPUT_PULLUP (PIN_DIR_INPUT | PIN_PULL_UP)
175 #define PIN_INPUT_NOPULL (PIN_DIR_INPUT | PIN_PULL_NONE)
176 #define PIN_OUTPUT_LOW (PIN_DIR_OUTPUT | PIN_VAL_LOW)
177 #define PIN_OUTPUT_HIGH (PIN_DIR_OUTPUT | PIN_VAL_HIGH)
179 #define PIN_SLPM_INPUT_PULLDOWN (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_DOWN)
180 #define PIN_SLPM_INPUT_PULLUP (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_UP)
181 #define PIN_SLPM_INPUT_NOPULL (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_NONE)
182 #define PIN_SLPM_OUTPUT_LOW (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_LOW)
183 #define PIN_SLPM_OUTPUT_HIGH (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_HIGH)
185 #define PIN_CFG_DEFAULT (0)
187 #define PIN_CFG(num, alt) \
189 (PIN_NUM(num) | PIN_##alt))
191 #define PIN_CFG_INPUT(num, alt, pull) \
193 (PIN_NUM(num) | PIN_##alt | PIN_INPUT_##pull))
195 #define PIN_CFG_OUTPUT(num, alt, val) \
197 (PIN_NUM(num) | PIN_##alt | PIN_OUTPUT_##val))
200 * "nmk_gpio" and "NMK_GPIO" stand for "Nomadik GPIO", leaving
201 * the "gpio" namespace for generic and cross-machine functions
204 #define GPIO_BLOCK_SHIFT 5
205 #define NMK_GPIO_PER_CHIP (1 << GPIO_BLOCK_SHIFT)
206 #define NMK_MAX_BANKS DIV_ROUND_UP(512, NMK_GPIO_PER_CHIP)
208 /* Register in the logic block */
209 #define NMK_GPIO_DAT 0x00
210 #define NMK_GPIO_DATS 0x04
211 #define NMK_GPIO_DATC 0x08
212 #define NMK_GPIO_PDIS 0x0c
213 #define NMK_GPIO_DIR 0x10
214 #define NMK_GPIO_DIRS 0x14
215 #define NMK_GPIO_DIRC 0x18
216 #define NMK_GPIO_SLPC 0x1c
217 #define NMK_GPIO_AFSLA 0x20
218 #define NMK_GPIO_AFSLB 0x24
219 #define NMK_GPIO_LOWEMI 0x28
221 #define NMK_GPIO_RIMSC 0x40
222 #define NMK_GPIO_FIMSC 0x44
223 #define NMK_GPIO_IS 0x48
224 #define NMK_GPIO_IC 0x4c
225 #define NMK_GPIO_RWIMSC 0x50
226 #define NMK_GPIO_FWIMSC 0x54
227 #define NMK_GPIO_WKS 0x58
228 /* These appear in DB8540 and later ASICs */
229 #define NMK_GPIO_EDGELEVEL 0x5C
230 #define NMK_GPIO_LEVEL 0x60
233 /* Pull up/down values */
243 NMK_GPIO_SLPM_WAKEUP_ENABLE
= NMK_GPIO_SLPM_INPUT
,
244 NMK_GPIO_SLPM_NOCHANGE
,
245 NMK_GPIO_SLPM_WAKEUP_DISABLE
= NMK_GPIO_SLPM_NOCHANGE
,
248 struct nmk_gpio_chip
{
249 struct gpio_chip chip
;
250 struct irq_chip irqchip
;
254 unsigned int parent_irq
;
255 int latent_parent_irq
;
256 u32 (*get_latent_status
)(unsigned int bank
);
257 void (*set_ioforce
)(bool enable
);
260 /* Keep track of configured edges */
273 * struct nmk_pinctrl - state container for the Nomadik pin controller
274 * @dev: containing device pointer
275 * @pctl: corresponding pin controller device
276 * @soc: SoC data for this specific chip
277 * @prcm_base: PRCM register range virtual base
281 struct pinctrl_dev
*pctl
;
282 const struct nmk_pinctrl_soc_data
*soc
;
283 void __iomem
*prcm_base
;
286 static struct nmk_gpio_chip
*nmk_gpio_chips
[NMK_MAX_BANKS
];
288 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock
);
290 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
292 static void __nmk_gpio_set_mode(struct nmk_gpio_chip
*nmk_chip
,
293 unsigned offset
, int gpio_mode
)
297 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & ~BIT(offset
);
298 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & ~BIT(offset
);
299 if (gpio_mode
& NMK_GPIO_ALT_A
)
300 afunc
|= BIT(offset
);
301 if (gpio_mode
& NMK_GPIO_ALT_B
)
302 bfunc
|= BIT(offset
);
303 writel(afunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLA
);
304 writel(bfunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLB
);
307 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip
*nmk_chip
,
308 unsigned offset
, enum nmk_gpio_slpm mode
)
312 slpm
= readl(nmk_chip
->addr
+ NMK_GPIO_SLPC
);
313 if (mode
== NMK_GPIO_SLPM_NOCHANGE
)
316 slpm
&= ~BIT(offset
);
317 writel(slpm
, nmk_chip
->addr
+ NMK_GPIO_SLPC
);
320 static void __nmk_gpio_set_pull(struct nmk_gpio_chip
*nmk_chip
,
321 unsigned offset
, enum nmk_gpio_pull pull
)
325 pdis
= readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
);
326 if (pull
== NMK_GPIO_PULL_NONE
) {
328 nmk_chip
->pull_up
&= ~BIT(offset
);
330 pdis
&= ~BIT(offset
);
333 writel(pdis
, nmk_chip
->addr
+ NMK_GPIO_PDIS
);
335 if (pull
== NMK_GPIO_PULL_UP
) {
336 nmk_chip
->pull_up
|= BIT(offset
);
337 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATS
);
338 } else if (pull
== NMK_GPIO_PULL_DOWN
) {
339 nmk_chip
->pull_up
&= ~BIT(offset
);
340 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATC
);
344 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip
*nmk_chip
,
345 unsigned offset
, bool lowemi
)
347 bool enabled
= nmk_chip
->lowemi
& BIT(offset
);
349 if (lowemi
== enabled
)
353 nmk_chip
->lowemi
|= BIT(offset
);
355 nmk_chip
->lowemi
&= ~BIT(offset
);
357 writel_relaxed(nmk_chip
->lowemi
,
358 nmk_chip
->addr
+ NMK_GPIO_LOWEMI
);
361 static void __nmk_gpio_make_input(struct nmk_gpio_chip
*nmk_chip
,
364 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRC
);
367 static void __nmk_gpio_set_output(struct nmk_gpio_chip
*nmk_chip
,
368 unsigned offset
, int val
)
371 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATS
);
373 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATC
);
376 static void __nmk_gpio_make_output(struct nmk_gpio_chip
*nmk_chip
,
377 unsigned offset
, int val
)
379 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRS
);
380 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
383 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip
*nmk_chip
,
384 unsigned offset
, int gpio_mode
,
387 u32 rwimsc
= nmk_chip
->rwimsc
;
388 u32 fwimsc
= nmk_chip
->fwimsc
;
390 if (glitch
&& nmk_chip
->set_ioforce
) {
391 u32 bit
= BIT(offset
);
393 /* Prevent spurious wakeups */
394 writel(rwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
395 writel(fwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
397 nmk_chip
->set_ioforce(true);
400 __nmk_gpio_set_mode(nmk_chip
, offset
, gpio_mode
);
402 if (glitch
&& nmk_chip
->set_ioforce
) {
403 nmk_chip
->set_ioforce(false);
405 writel(rwimsc
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
406 writel(fwimsc
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
411 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip
*nmk_chip
, unsigned offset
)
413 u32 falling
= nmk_chip
->fimsc
& BIT(offset
);
414 u32 rising
= nmk_chip
->rimsc
& BIT(offset
);
415 int gpio
= nmk_chip
->chip
.base
+ offset
;
416 int irq
= irq_find_mapping(nmk_chip
->chip
.irq
.domain
, offset
);
417 struct irq_data
*d
= irq_get_irq_data(irq
);
419 if (!rising
&& !falling
)
422 if (!d
|| !irqd_irq_disabled(d
))
426 nmk_chip
->rimsc
&= ~BIT(offset
);
427 writel_relaxed(nmk_chip
->rimsc
,
428 nmk_chip
->addr
+ NMK_GPIO_RIMSC
);
432 nmk_chip
->fimsc
&= ~BIT(offset
);
433 writel_relaxed(nmk_chip
->fimsc
,
434 nmk_chip
->addr
+ NMK_GPIO_FIMSC
);
437 dev_dbg(nmk_chip
->chip
.parent
, "%d: clearing interrupt mask\n", gpio
);
440 static void nmk_write_masked(void __iomem
*reg
, u32 mask
, u32 value
)
445 val
= ((val
& ~mask
) | (value
& mask
));
449 static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl
*npct
,
450 unsigned offset
, unsigned alt_num
)
456 const struct prcm_gpiocr_altcx_pin_desc
*pin_desc
;
457 const u16
*gpiocr_regs
;
459 if (!npct
->prcm_base
)
462 if (alt_num
> PRCM_IDX_GPIOCR_ALTC_MAX
) {
463 dev_err(npct
->dev
, "PRCM GPIOCR: alternate-C%i is invalid\n",
468 for (i
= 0 ; i
< npct
->soc
->npins_altcx
; i
++) {
469 if (npct
->soc
->altcx_pins
[i
].pin
== offset
)
472 if (i
== npct
->soc
->npins_altcx
) {
473 dev_dbg(npct
->dev
, "PRCM GPIOCR: pin %i is not found\n",
478 pin_desc
= npct
->soc
->altcx_pins
+ i
;
479 gpiocr_regs
= npct
->soc
->prcm_gpiocr_registers
;
482 * If alt_num is NULL, just clear current ALTCx selection
483 * to make sure we come back to a pure ALTC selection
486 for (i
= 0 ; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
487 if (pin_desc
->altcx
[i
].used
== true) {
488 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
489 bit
= pin_desc
->altcx
[i
].control_bit
;
490 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
)) {
491 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), 0);
493 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
501 alt_index
= alt_num
- 1;
502 if (pin_desc
->altcx
[alt_index
].used
== false) {
504 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
510 * Check if any other ALTCx functions are activated on this pin
511 * and disable it first.
513 for (i
= 0 ; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
516 if (pin_desc
->altcx
[i
].used
== true) {
517 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
518 bit
= pin_desc
->altcx
[i
].control_bit
;
519 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
)) {
520 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), 0);
522 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
528 reg
= gpiocr_regs
[pin_desc
->altcx
[alt_index
].reg_index
];
529 bit
= pin_desc
->altcx
[alt_index
].control_bit
;
530 dev_dbg(npct
->dev
, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
531 offset
, alt_index
+1);
532 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), BIT(bit
));
536 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
537 * - Save SLPM registers
538 * - Set SLPM=0 for the IOs you want to switch and others to 1
539 * - Configure the GPIO registers for the IOs that are being switched
541 * - Modify the AFLSA/B registers for the IOs that are being switched
543 * - Restore SLPM registers
544 * - Any spurious wake up event during switch sequence to be ignored and
547 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm
)
551 for (i
= 0; i
< NUM_BANKS
; i
++) {
552 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
553 unsigned int temp
= slpm
[i
];
558 clk_enable(chip
->clk
);
560 slpm
[i
] = readl(chip
->addr
+ NMK_GPIO_SLPC
);
561 writel(temp
, chip
->addr
+ NMK_GPIO_SLPC
);
565 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm
)
569 for (i
= 0; i
< NUM_BANKS
; i
++) {
570 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
575 writel(slpm
[i
], chip
->addr
+ NMK_GPIO_SLPC
);
577 clk_disable(chip
->clk
);
581 static int __maybe_unused
nmk_prcm_gpiocr_get_mode(struct pinctrl_dev
*pctldev
, int gpio
)
586 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
587 const struct prcm_gpiocr_altcx_pin_desc
*pin_desc
;
588 const u16
*gpiocr_regs
;
590 if (!npct
->prcm_base
)
591 return NMK_GPIO_ALT_C
;
593 for (i
= 0; i
< npct
->soc
->npins_altcx
; i
++) {
594 if (npct
->soc
->altcx_pins
[i
].pin
== gpio
)
597 if (i
== npct
->soc
->npins_altcx
)
598 return NMK_GPIO_ALT_C
;
600 pin_desc
= npct
->soc
->altcx_pins
+ i
;
601 gpiocr_regs
= npct
->soc
->prcm_gpiocr_registers
;
602 for (i
= 0; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
603 if (pin_desc
->altcx
[i
].used
== true) {
604 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
605 bit
= pin_desc
->altcx
[i
].control_bit
;
606 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
))
607 return NMK_GPIO_ALT_C
+i
+1;
610 return NMK_GPIO_ALT_C
;
615 static void nmk_gpio_irq_ack(struct irq_data
*d
)
617 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
618 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
620 clk_enable(nmk_chip
->clk
);
621 writel(BIT(d
->hwirq
), nmk_chip
->addr
+ NMK_GPIO_IC
);
622 clk_disable(nmk_chip
->clk
);
625 enum nmk_gpio_irq_type
{
630 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip
*nmk_chip
,
631 int offset
, enum nmk_gpio_irq_type which
,
639 if (which
== NORMAL
) {
640 rimscreg
= NMK_GPIO_RIMSC
;
641 fimscreg
= NMK_GPIO_FIMSC
;
642 rimscval
= &nmk_chip
->rimsc
;
643 fimscval
= &nmk_chip
->fimsc
;
645 rimscreg
= NMK_GPIO_RWIMSC
;
646 fimscreg
= NMK_GPIO_FWIMSC
;
647 rimscval
= &nmk_chip
->rwimsc
;
648 fimscval
= &nmk_chip
->fwimsc
;
651 /* we must individually set/clear the two edges */
652 if (nmk_chip
->edge_rising
& BIT(offset
)) {
654 *rimscval
|= BIT(offset
);
656 *rimscval
&= ~BIT(offset
);
657 writel(*rimscval
, nmk_chip
->addr
+ rimscreg
);
659 if (nmk_chip
->edge_falling
& BIT(offset
)) {
661 *fimscval
|= BIT(offset
);
663 *fimscval
&= ~BIT(offset
);
664 writel(*fimscval
, nmk_chip
->addr
+ fimscreg
);
668 static void __nmk_gpio_set_wake(struct nmk_gpio_chip
*nmk_chip
,
672 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
673 * disabled, since setting SLPM to 1 increases power consumption, and
674 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
676 if (nmk_chip
->sleepmode
&& on
) {
677 __nmk_gpio_set_slpm(nmk_chip
, offset
,
678 NMK_GPIO_SLPM_WAKEUP_ENABLE
);
681 __nmk_gpio_irq_modify(nmk_chip
, offset
, WAKE
, on
);
684 static int nmk_gpio_irq_maskunmask(struct irq_data
*d
, bool enable
)
686 struct nmk_gpio_chip
*nmk_chip
;
689 nmk_chip
= irq_data_get_irq_chip_data(d
);
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
& BIT(d
->hwirq
)))
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
;
724 nmk_chip
= irq_data_get_irq_chip_data(d
);
728 clk_enable(nmk_chip
->clk
);
729 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
730 spin_lock(&nmk_chip
->lock
);
732 if (irqd_irq_disabled(d
))
733 __nmk_gpio_set_wake(nmk_chip
, d
->hwirq
, on
);
736 nmk_chip
->real_wake
|= BIT(d
->hwirq
);
738 nmk_chip
->real_wake
&= ~BIT(d
->hwirq
);
740 spin_unlock(&nmk_chip
->lock
);
741 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
742 clk_disable(nmk_chip
->clk
);
747 static int nmk_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
749 bool enabled
= !irqd_irq_disabled(d
);
750 bool wake
= irqd_is_wakeup_set(d
);
751 struct nmk_gpio_chip
*nmk_chip
;
754 nmk_chip
= irq_data_get_irq_chip_data(d
);
757 if (type
& IRQ_TYPE_LEVEL_HIGH
)
759 if (type
& IRQ_TYPE_LEVEL_LOW
)
762 clk_enable(nmk_chip
->clk
);
763 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
766 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, NORMAL
, false);
769 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, WAKE
, false);
771 nmk_chip
->edge_rising
&= ~BIT(d
->hwirq
);
772 if (type
& IRQ_TYPE_EDGE_RISING
)
773 nmk_chip
->edge_rising
|= BIT(d
->hwirq
);
775 nmk_chip
->edge_falling
&= ~BIT(d
->hwirq
);
776 if (type
& IRQ_TYPE_EDGE_FALLING
)
777 nmk_chip
->edge_falling
|= BIT(d
->hwirq
);
780 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, NORMAL
, true);
783 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, WAKE
, true);
785 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
786 clk_disable(nmk_chip
->clk
);
791 static unsigned int nmk_gpio_irq_startup(struct irq_data
*d
)
793 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
795 clk_enable(nmk_chip
->clk
);
796 nmk_gpio_irq_unmask(d
);
800 static void nmk_gpio_irq_shutdown(struct irq_data
*d
)
802 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
804 nmk_gpio_irq_mask(d
);
805 clk_disable(nmk_chip
->clk
);
808 static void __nmk_gpio_irq_handler(struct irq_desc
*desc
, u32 status
)
810 struct irq_chip
*host_chip
= irq_desc_get_chip(desc
);
811 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
813 chained_irq_enter(host_chip
, desc
);
816 int bit
= __ffs(status
);
818 generic_handle_irq(irq_find_mapping(chip
->irq
.domain
, bit
));
822 chained_irq_exit(host_chip
, desc
);
825 static void nmk_gpio_irq_handler(struct irq_desc
*desc
)
827 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
828 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
831 clk_enable(nmk_chip
->clk
);
832 status
= readl(nmk_chip
->addr
+ NMK_GPIO_IS
);
833 clk_disable(nmk_chip
->clk
);
835 __nmk_gpio_irq_handler(desc
, status
);
838 static void nmk_gpio_latent_irq_handler(struct irq_desc
*desc
)
840 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
841 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
842 u32 status
= nmk_chip
->get_latent_status(nmk_chip
->bank
);
844 __nmk_gpio_irq_handler(desc
, status
);
849 static int nmk_gpio_get_dir(struct gpio_chip
*chip
, unsigned offset
)
851 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
854 clk_enable(nmk_chip
->clk
);
856 dir
= !(readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & BIT(offset
));
858 clk_disable(nmk_chip
->clk
);
863 static int nmk_gpio_make_input(struct gpio_chip
*chip
, unsigned offset
)
865 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
867 clk_enable(nmk_chip
->clk
);
869 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRC
);
871 clk_disable(nmk_chip
->clk
);
876 static int nmk_gpio_get_input(struct gpio_chip
*chip
, unsigned offset
)
878 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
881 clk_enable(nmk_chip
->clk
);
883 value
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & BIT(offset
));
885 clk_disable(nmk_chip
->clk
);
890 static void nmk_gpio_set_output(struct gpio_chip
*chip
, unsigned offset
,
893 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
895 clk_enable(nmk_chip
->clk
);
897 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
899 clk_disable(nmk_chip
->clk
);
902 static int nmk_gpio_make_output(struct gpio_chip
*chip
, unsigned offset
,
905 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
907 clk_enable(nmk_chip
->clk
);
909 __nmk_gpio_make_output(nmk_chip
, offset
, val
);
911 clk_disable(nmk_chip
->clk
);
916 #ifdef CONFIG_DEBUG_FS
917 static int nmk_gpio_get_mode(struct nmk_gpio_chip
*nmk_chip
, int offset
)
921 clk_enable(nmk_chip
->clk
);
923 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & BIT(offset
);
924 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & BIT(offset
);
926 clk_disable(nmk_chip
->clk
);
928 return (afunc
? NMK_GPIO_ALT_A
: 0) | (bfunc
? NMK_GPIO_ALT_B
: 0);
931 #include <linux/seq_file.h>
933 static void nmk_gpio_dbg_show_one(struct seq_file
*s
,
934 struct pinctrl_dev
*pctldev
, struct gpio_chip
*chip
,
935 unsigned offset
, unsigned gpio
)
937 const char *label
= gpiochip_is_requested(chip
, offset
);
938 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
943 const char *modes
[] = {
944 [NMK_GPIO_ALT_GPIO
] = "gpio",
945 [NMK_GPIO_ALT_A
] = "altA",
946 [NMK_GPIO_ALT_B
] = "altB",
947 [NMK_GPIO_ALT_C
] = "altC",
948 [NMK_GPIO_ALT_C
+1] = "altC1",
949 [NMK_GPIO_ALT_C
+2] = "altC2",
950 [NMK_GPIO_ALT_C
+3] = "altC3",
951 [NMK_GPIO_ALT_C
+4] = "altC4",
953 const char *pulls
[] = {
959 clk_enable(nmk_chip
->clk
);
960 is_out
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & BIT(offset
));
961 pull
= !(readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
) & BIT(offset
));
962 data_out
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & BIT(offset
));
963 mode
= nmk_gpio_get_mode(nmk_chip
, offset
);
964 if ((mode
== NMK_GPIO_ALT_C
) && pctldev
)
965 mode
= nmk_prcm_gpiocr_get_mode(pctldev
, gpio
);
968 seq_printf(s
, " gpio-%-3d (%-20.20s) out %s %s",
971 data_out
? "hi" : "lo",
972 (mode
< 0) ? "unknown" : modes
[mode
]);
974 int irq
= chip
->to_irq(chip
, offset
);
975 struct irq_desc
*desc
= irq_to_desc(irq
);
980 pullidx
= data_out
? 2 : 1;
982 seq_printf(s
, " gpio-%-3d (%-20.20s) in %s %s",
986 (mode
< 0) ? "unknown" : modes
[mode
]);
988 val
= nmk_gpio_get_input(chip
, offset
);
989 seq_printf(s
, " VAL %d", val
);
992 * This races with request_irq(), set_irq_type(),
993 * and set_irq_wake() ... but those are "rare".
995 if (irq
> 0 && desc
&& desc
->action
) {
998 if (nmk_chip
->edge_rising
& BIT(offset
))
999 trigger
= "edge-rising";
1000 else if (nmk_chip
->edge_falling
& BIT(offset
))
1001 trigger
= "edge-falling";
1003 trigger
= "edge-undefined";
1005 seq_printf(s
, " irq-%d %s%s",
1007 irqd_is_wakeup_set(&desc
->irq_data
)
1011 clk_disable(nmk_chip
->clk
);
1014 static void nmk_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1017 unsigned gpio
= chip
->base
;
1019 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
1020 nmk_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
1021 seq_printf(s
, "\n");
1026 static inline void nmk_gpio_dbg_show_one(struct seq_file
*s
,
1027 struct pinctrl_dev
*pctldev
,
1028 struct gpio_chip
*chip
,
1029 unsigned offset
, unsigned gpio
)
1032 #define nmk_gpio_dbg_show NULL
1036 * We will allocate memory for the state container using devm* allocators
1037 * binding to the first device reaching this point, it doesn't matter if
1038 * it is the pin controller or GPIO driver. However we need to use the right
1039 * platform device when looking up resources so pay attention to pdev.
1041 static struct nmk_gpio_chip
*nmk_gpio_populate_chip(struct device_node
*np
,
1042 struct platform_device
*pdev
)
1044 struct nmk_gpio_chip
*nmk_chip
;
1045 struct platform_device
*gpio_pdev
;
1046 struct gpio_chip
*chip
;
1047 struct resource
*res
;
1052 gpio_pdev
= of_find_device_by_node(np
);
1054 pr_err("populate \"%pOFn\": device not found\n", np
);
1055 return ERR_PTR(-ENODEV
);
1057 if (of_property_read_u32(np
, "gpio-bank", &id
)) {
1058 dev_err(&pdev
->dev
, "populate: gpio-bank property not found\n");
1059 platform_device_put(gpio_pdev
);
1060 return ERR_PTR(-EINVAL
);
1063 /* Already populated? */
1064 nmk_chip
= nmk_gpio_chips
[id
];
1066 platform_device_put(gpio_pdev
);
1070 nmk_chip
= devm_kzalloc(&pdev
->dev
, sizeof(*nmk_chip
), GFP_KERNEL
);
1072 platform_device_put(gpio_pdev
);
1073 return ERR_PTR(-ENOMEM
);
1076 nmk_chip
->bank
= id
;
1077 chip
= &nmk_chip
->chip
;
1078 chip
->base
= id
* NMK_GPIO_PER_CHIP
;
1079 chip
->ngpio
= NMK_GPIO_PER_CHIP
;
1080 chip
->label
= dev_name(&gpio_pdev
->dev
);
1081 chip
->parent
= &gpio_pdev
->dev
;
1083 res
= platform_get_resource(gpio_pdev
, IORESOURCE_MEM
, 0);
1084 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1086 platform_device_put(gpio_pdev
);
1087 return ERR_CAST(base
);
1089 nmk_chip
->addr
= base
;
1091 clk
= clk_get(&gpio_pdev
->dev
, NULL
);
1093 platform_device_put(gpio_pdev
);
1094 return (void *) clk
;
1097 nmk_chip
->clk
= clk
;
1099 BUG_ON(nmk_chip
->bank
>= ARRAY_SIZE(nmk_gpio_chips
));
1100 nmk_gpio_chips
[id
] = nmk_chip
;
1104 static int nmk_gpio_probe(struct platform_device
*dev
)
1106 struct device_node
*np
= dev
->dev
.of_node
;
1107 struct nmk_gpio_chip
*nmk_chip
;
1108 struct gpio_chip
*chip
;
1109 struct irq_chip
*irqchip
;
1111 bool supports_sleepmode
;
1115 nmk_chip
= nmk_gpio_populate_chip(np
, dev
);
1116 if (IS_ERR(nmk_chip
)) {
1117 dev_err(&dev
->dev
, "could not populate nmk chip struct\n");
1118 return PTR_ERR(nmk_chip
);
1121 supports_sleepmode
=
1122 of_property_read_bool(np
, "st,supports-sleepmode");
1124 /* Correct platform device ID */
1125 dev
->id
= nmk_chip
->bank
;
1127 irq
= platform_get_irq(dev
, 0);
1131 /* It's OK for this IRQ not to be present */
1132 latent_irq
= platform_get_irq(dev
, 1);
1135 * The virt address in nmk_chip->addr is in the nomadik register space,
1136 * so we can simply convert the resource address, without remapping
1138 nmk_chip
->parent_irq
= irq
;
1139 nmk_chip
->latent_parent_irq
= latent_irq
;
1140 nmk_chip
->sleepmode
= supports_sleepmode
;
1141 spin_lock_init(&nmk_chip
->lock
);
1143 chip
= &nmk_chip
->chip
;
1144 chip
->request
= gpiochip_generic_request
;
1145 chip
->free
= gpiochip_generic_free
;
1146 chip
->get_direction
= nmk_gpio_get_dir
;
1147 chip
->direction_input
= nmk_gpio_make_input
;
1148 chip
->get
= nmk_gpio_get_input
;
1149 chip
->direction_output
= nmk_gpio_make_output
;
1150 chip
->set
= nmk_gpio_set_output
;
1151 chip
->dbg_show
= nmk_gpio_dbg_show
;
1152 chip
->can_sleep
= false;
1153 chip
->owner
= THIS_MODULE
;
1155 irqchip
= &nmk_chip
->irqchip
;
1156 irqchip
->irq_ack
= nmk_gpio_irq_ack
;
1157 irqchip
->irq_mask
= nmk_gpio_irq_mask
;
1158 irqchip
->irq_unmask
= nmk_gpio_irq_unmask
;
1159 irqchip
->irq_set_type
= nmk_gpio_irq_set_type
;
1160 irqchip
->irq_set_wake
= nmk_gpio_irq_set_wake
;
1161 irqchip
->irq_startup
= nmk_gpio_irq_startup
;
1162 irqchip
->irq_shutdown
= nmk_gpio_irq_shutdown
;
1163 irqchip
->flags
= IRQCHIP_MASK_ON_SUSPEND
;
1164 irqchip
->name
= kasprintf(GFP_KERNEL
, "nmk%u-%u-%u",
1167 chip
->base
+ chip
->ngpio
- 1);
1169 clk_enable(nmk_chip
->clk
);
1170 nmk_chip
->lowemi
= readl_relaxed(nmk_chip
->addr
+ NMK_GPIO_LOWEMI
);
1171 clk_disable(nmk_chip
->clk
);
1174 ret
= gpiochip_add_data(chip
, nmk_chip
);
1178 platform_set_drvdata(dev
, nmk_chip
);
1181 * Let the generic code handle this edge IRQ, the the chained
1182 * handler will perform the actual work of handling the parent
1185 ret
= gpiochip_irqchip_add(chip
,
1191 dev_err(&dev
->dev
, "could not add irqchip\n");
1192 gpiochip_remove(&nmk_chip
->chip
);
1195 /* Then register the chain on the parent IRQ */
1196 gpiochip_set_chained_irqchip(chip
,
1198 nmk_chip
->parent_irq
,
1199 nmk_gpio_irq_handler
);
1200 if (nmk_chip
->latent_parent_irq
> 0)
1201 gpiochip_set_chained_irqchip(chip
,
1203 nmk_chip
->latent_parent_irq
,
1204 nmk_gpio_latent_irq_handler
);
1206 dev_info(&dev
->dev
, "at address %p\n", nmk_chip
->addr
);
1211 static int nmk_get_groups_cnt(struct pinctrl_dev
*pctldev
)
1213 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1215 return npct
->soc
->ngroups
;
1218 static const char *nmk_get_group_name(struct pinctrl_dev
*pctldev
,
1221 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1223 return npct
->soc
->groups
[selector
].name
;
1226 static int nmk_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
1227 const unsigned **pins
,
1230 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1232 *pins
= npct
->soc
->groups
[selector
].pins
;
1233 *num_pins
= npct
->soc
->groups
[selector
].npins
;
1237 static struct nmk_gpio_chip
*find_nmk_gpio_from_pin(unsigned pin
)
1240 struct nmk_gpio_chip
*nmk_gpio
;
1242 for(i
= 0; i
< NMK_MAX_BANKS
; i
++) {
1243 nmk_gpio
= nmk_gpio_chips
[i
];
1246 if (pin
>= nmk_gpio
->chip
.base
&&
1247 pin
< nmk_gpio
->chip
.base
+ nmk_gpio
->chip
.ngpio
)
1253 static struct gpio_chip
*find_gc_from_pin(unsigned pin
)
1255 struct nmk_gpio_chip
*nmk_gpio
= find_nmk_gpio_from_pin(pin
);
1258 return &nmk_gpio
->chip
;
1262 static void nmk_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
1265 struct gpio_chip
*chip
= find_gc_from_pin(offset
);
1268 seq_printf(s
, "invalid pin offset");
1271 nmk_gpio_dbg_show_one(s
, pctldev
, chip
, offset
- chip
->base
, offset
);
1274 static int nmk_dt_add_map_mux(struct pinctrl_map
**map
, unsigned *reserved_maps
,
1275 unsigned *num_maps
, const char *group
,
1276 const char *function
)
1278 if (*num_maps
== *reserved_maps
)
1281 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
1282 (*map
)[*num_maps
].data
.mux
.group
= group
;
1283 (*map
)[*num_maps
].data
.mux
.function
= function
;
1289 static int nmk_dt_add_map_configs(struct pinctrl_map
**map
,
1290 unsigned *reserved_maps
,
1291 unsigned *num_maps
, const char *group
,
1292 unsigned long *configs
, unsigned num_configs
)
1294 unsigned long *dup_configs
;
1296 if (*num_maps
== *reserved_maps
)
1299 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
1304 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
1306 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
1307 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
1308 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
1314 #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
1315 #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
1316 .size = ARRAY_SIZE(y), }
1318 static const unsigned long nmk_pin_input_modes
[] = {
1324 static const unsigned long nmk_pin_output_modes
[] = {
1330 static const unsigned long nmk_pin_sleep_modes
[] = {
1331 PIN_SLEEPMODE_DISABLED
,
1332 PIN_SLEEPMODE_ENABLED
,
1335 static const unsigned long nmk_pin_sleep_input_modes
[] = {
1336 PIN_SLPM_INPUT_NOPULL
,
1337 PIN_SLPM_INPUT_PULLUP
,
1338 PIN_SLPM_INPUT_PULLDOWN
,
1342 static const unsigned long nmk_pin_sleep_output_modes
[] = {
1343 PIN_SLPM_OUTPUT_LOW
,
1344 PIN_SLPM_OUTPUT_HIGH
,
1345 PIN_SLPM_DIR_OUTPUT
,
1348 static const unsigned long nmk_pin_sleep_wakeup_modes
[] = {
1349 PIN_SLPM_WAKEUP_DISABLE
,
1350 PIN_SLPM_WAKEUP_ENABLE
,
1353 static const unsigned long nmk_pin_gpio_modes
[] = {
1354 PIN_GPIOMODE_DISABLED
,
1355 PIN_GPIOMODE_ENABLED
,
1358 static const unsigned long nmk_pin_sleep_pdis_modes
[] = {
1359 PIN_SLPM_PDIS_DISABLED
,
1360 PIN_SLPM_PDIS_ENABLED
,
1363 struct nmk_cfg_param
{
1364 const char *property
;
1365 unsigned long config
;
1366 const unsigned long *choice
;
1370 static const struct nmk_cfg_param nmk_cfg_params
[] = {
1371 NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes
),
1372 NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes
),
1373 NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes
),
1374 NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes
),
1375 NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes
),
1376 NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes
),
1377 NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes
),
1378 NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes
),
1381 static int nmk_dt_pin_config(int index
, int val
, unsigned long *config
)
1385 if (nmk_cfg_params
[index
].choice
== NULL
)
1386 *config
= nmk_cfg_params
[index
].config
;
1388 /* test if out of range */
1389 if (val
< nmk_cfg_params
[index
].size
) {
1390 *config
= nmk_cfg_params
[index
].config
|
1391 nmk_cfg_params
[index
].choice
[val
];
1397 static const char *nmk_find_pin_name(struct pinctrl_dev
*pctldev
, const char *pin_name
)
1400 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1402 if (sscanf((char *)pin_name
, "GPIO%d", &pin_number
) == 1)
1403 for (i
= 0; i
< npct
->soc
->npins
; i
++)
1404 if (npct
->soc
->pins
[i
].number
== pin_number
)
1405 return npct
->soc
->pins
[i
].name
;
1409 static bool nmk_pinctrl_dt_get_config(struct device_node
*np
,
1410 unsigned long *configs
)
1412 bool has_config
= 0;
1413 unsigned long cfg
= 0;
1416 for (i
= 0; i
< ARRAY_SIZE(nmk_cfg_params
); i
++) {
1417 ret
= of_property_read_u32(np
,
1418 nmk_cfg_params
[i
].property
, &val
);
1419 if (ret
!= -EINVAL
) {
1420 if (nmk_dt_pin_config(i
, val
, &cfg
) == 0) {
1430 static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
1431 struct device_node
*np
,
1432 struct pinctrl_map
**map
,
1433 unsigned *reserved_maps
,
1437 const char *function
= NULL
;
1438 unsigned long configs
= 0;
1439 bool has_config
= 0;
1440 struct property
*prop
;
1441 struct device_node
*np_config
;
1443 ret
= of_property_read_string(np
, "function", &function
);
1447 ret
= of_property_count_strings(np
, "groups");
1451 ret
= pinctrl_utils_reserve_map(pctldev
, map
,
1457 of_property_for_each_string(np
, "groups", prop
, group
) {
1458 ret
= nmk_dt_add_map_mux(map
, reserved_maps
, num_maps
,
1465 has_config
= nmk_pinctrl_dt_get_config(np
, &configs
);
1466 np_config
= of_parse_phandle(np
, "ste,config", 0);
1468 has_config
|= nmk_pinctrl_dt_get_config(np_config
, &configs
);
1470 const char *gpio_name
;
1473 ret
= of_property_count_strings(np
, "pins");
1476 ret
= pinctrl_utils_reserve_map(pctldev
, map
,
1482 of_property_for_each_string(np
, "pins", prop
, pin
) {
1483 gpio_name
= nmk_find_pin_name(pctldev
, pin
);
1485 ret
= nmk_dt_add_map_configs(map
, reserved_maps
,
1487 gpio_name
, &configs
, 1);
1497 static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
1498 struct device_node
*np_config
,
1499 struct pinctrl_map
**map
, unsigned *num_maps
)
1501 unsigned reserved_maps
;
1502 struct device_node
*np
;
1509 for_each_child_of_node(np_config
, np
) {
1510 ret
= nmk_pinctrl_dt_subnode_to_map(pctldev
, np
, map
,
1511 &reserved_maps
, num_maps
);
1513 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
1521 static const struct pinctrl_ops nmk_pinctrl_ops
= {
1522 .get_groups_count
= nmk_get_groups_cnt
,
1523 .get_group_name
= nmk_get_group_name
,
1524 .get_group_pins
= nmk_get_group_pins
,
1525 .pin_dbg_show
= nmk_pin_dbg_show
,
1526 .dt_node_to_map
= nmk_pinctrl_dt_node_to_map
,
1527 .dt_free_map
= pinctrl_utils_free_map
,
1530 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
1532 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1534 return npct
->soc
->nfunctions
;
1537 static const char *nmk_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
1540 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1542 return npct
->soc
->functions
[function
].name
;
1545 static int nmk_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
1547 const char * const **groups
,
1548 unsigned * const num_groups
)
1550 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1552 *groups
= npct
->soc
->functions
[function
].groups
;
1553 *num_groups
= npct
->soc
->functions
[function
].ngroups
;
1558 static int nmk_pmx_set(struct pinctrl_dev
*pctldev
, unsigned function
,
1561 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1562 const struct nmk_pingroup
*g
;
1563 static unsigned int slpm
[NUM_BANKS
];
1564 unsigned long flags
= 0;
1569 g
= &npct
->soc
->groups
[group
];
1571 if (g
->altsetting
< 0)
1574 dev_dbg(npct
->dev
, "enable group %s, %u pins\n", g
->name
, g
->npins
);
1577 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1578 * we may pass through an undesired state. In this case we take
1581 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1582 * - Save SLPM registers (since we have a shadow register in the
1583 * nmk_chip we're using that as backup)
1584 * - Set SLPM=0 for the IOs you want to switch and others to 1
1585 * - Configure the GPIO registers for the IOs that are being switched
1587 * - Modify the AFLSA/B registers for the IOs that are being switched
1589 * - Restore SLPM registers
1590 * - Any spurious wake up event during switch sequence to be ignored
1593 * We REALLY need to save ALL slpm registers, because the external
1594 * IOFORCE will switch *all* ports to their sleepmode setting to as
1595 * to avoid glitches. (Not just one port!)
1597 glitch
= ((g
->altsetting
& NMK_GPIO_ALT_C
) == NMK_GPIO_ALT_C
);
1600 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
1602 /* Initially don't put any pins to sleep when switching */
1603 memset(slpm
, 0xff, sizeof(slpm
));
1606 * Then mask the pins that need to be sleeping now when we're
1607 * switching to the ALT C function.
1609 for (i
= 0; i
< g
->npins
; i
++)
1610 slpm
[g
->pins
[i
] / NMK_GPIO_PER_CHIP
] &= ~BIT(g
->pins
[i
]);
1611 nmk_gpio_glitch_slpm_init(slpm
);
1614 for (i
= 0; i
< g
->npins
; i
++) {
1615 struct nmk_gpio_chip
*nmk_chip
;
1618 nmk_chip
= find_nmk_gpio_from_pin(g
->pins
[i
]);
1621 "invalid pin offset %d in group %s at index %d\n",
1622 g
->pins
[i
], g
->name
, i
);
1625 dev_dbg(npct
->dev
, "setting pin %d to altsetting %d\n", g
->pins
[i
], g
->altsetting
);
1627 clk_enable(nmk_chip
->clk
);
1628 bit
= g
->pins
[i
] % NMK_GPIO_PER_CHIP
;
1630 * If the pin is switching to altfunc, and there was an
1631 * interrupt installed on it which has been lazy disabled,
1632 * actually mask the interrupt to prevent spurious interrupts
1633 * that would occur while the pin is under control of the
1634 * peripheral. Only SKE does this.
1636 nmk_gpio_disable_lazy_irq(nmk_chip
, bit
);
1638 __nmk_gpio_set_mode_safe(nmk_chip
, bit
,
1639 (g
->altsetting
& NMK_GPIO_ALT_C
), glitch
);
1640 clk_disable(nmk_chip
->clk
);
1643 * Call PRCM GPIOCR config function in case ALTC
1644 * has been selected:
1645 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1647 * - If selection is pure ALTC and previous selection was ALTCx,
1648 * then some bits in PRCM GPIOCR registers must be cleared.
1650 if ((g
->altsetting
& NMK_GPIO_ALT_C
) == NMK_GPIO_ALT_C
)
1651 nmk_prcm_altcx_set_mode(npct
, g
->pins
[i
],
1652 g
->altsetting
>> NMK_GPIO_ALT_CX_SHIFT
);
1655 /* When all pins are successfully reconfigured we get here */
1660 nmk_gpio_glitch_slpm_restore(slpm
);
1661 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
1667 static int nmk_gpio_request_enable(struct pinctrl_dev
*pctldev
,
1668 struct pinctrl_gpio_range
*range
,
1671 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1672 struct nmk_gpio_chip
*nmk_chip
;
1673 struct gpio_chip
*chip
;
1677 dev_err(npct
->dev
, "invalid range\n");
1681 dev_err(npct
->dev
, "missing GPIO chip in range\n");
1685 nmk_chip
= gpiochip_get_data(chip
);
1687 dev_dbg(npct
->dev
, "enable pin %u as GPIO\n", offset
);
1689 clk_enable(nmk_chip
->clk
);
1690 bit
= offset
% NMK_GPIO_PER_CHIP
;
1691 /* There is no glitch when converting any pin to GPIO */
1692 __nmk_gpio_set_mode(nmk_chip
, bit
, NMK_GPIO_ALT_GPIO
);
1693 clk_disable(nmk_chip
->clk
);
1698 static void nmk_gpio_disable_free(struct pinctrl_dev
*pctldev
,
1699 struct pinctrl_gpio_range
*range
,
1702 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1704 dev_dbg(npct
->dev
, "disable pin %u as GPIO\n", offset
);
1705 /* Set the pin to some default state, GPIO is usually default */
1708 static const struct pinmux_ops nmk_pinmux_ops
= {
1709 .get_functions_count
= nmk_pmx_get_funcs_cnt
,
1710 .get_function_name
= nmk_pmx_get_func_name
,
1711 .get_function_groups
= nmk_pmx_get_func_groups
,
1712 .set_mux
= nmk_pmx_set
,
1713 .gpio_request_enable
= nmk_gpio_request_enable
,
1714 .gpio_disable_free
= nmk_gpio_disable_free
,
1718 static int nmk_pin_config_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
1719 unsigned long *config
)
1721 /* Not implemented */
1725 static int nmk_pin_config_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
1726 unsigned long *configs
, unsigned num_configs
)
1728 static const char *pullnames
[] = {
1729 [NMK_GPIO_PULL_NONE
] = "none",
1730 [NMK_GPIO_PULL_UP
] = "up",
1731 [NMK_GPIO_PULL_DOWN
] = "down",
1732 [3] /* illegal */ = "??"
1734 static const char *slpmnames
[] = {
1735 [NMK_GPIO_SLPM_INPUT
] = "input/wakeup",
1736 [NMK_GPIO_SLPM_NOCHANGE
] = "no-change/no-wakeup",
1738 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1739 struct nmk_gpio_chip
*nmk_chip
;
1742 int pull
, slpm
, output
, val
, i
;
1743 bool lowemi
, gpiomode
, sleep
;
1745 nmk_chip
= find_nmk_gpio_from_pin(pin
);
1748 "invalid pin offset %d\n", pin
);
1752 for (i
= 0; i
< num_configs
; i
++) {
1754 * The pin config contains pin number and altfunction fields,
1755 * here we just ignore that part. It's being handled by the
1756 * framework and pinmux callback respectively.
1758 cfg
= (pin_cfg_t
) configs
[i
];
1759 pull
= PIN_PULL(cfg
);
1760 slpm
= PIN_SLPM(cfg
);
1761 output
= PIN_DIR(cfg
);
1763 lowemi
= PIN_LOWEMI(cfg
);
1764 gpiomode
= PIN_GPIOMODE(cfg
);
1765 sleep
= PIN_SLEEPMODE(cfg
);
1768 int slpm_pull
= PIN_SLPM_PULL(cfg
);
1769 int slpm_output
= PIN_SLPM_DIR(cfg
);
1770 int slpm_val
= PIN_SLPM_VAL(cfg
);
1772 /* All pins go into GPIO mode at sleep */
1776 * The SLPM_* values are normal values + 1 to allow zero
1777 * to mean "same as normal".
1780 pull
= slpm_pull
- 1;
1782 output
= slpm_output
- 1;
1786 dev_dbg(nmk_chip
->chip
.parent
,
1787 "pin %d: sleep pull %s, dir %s, val %s\n",
1789 slpm_pull
? pullnames
[pull
] : "same",
1790 slpm_output
? (output
? "output" : "input")
1792 slpm_val
? (val
? "high" : "low") : "same");
1795 dev_dbg(nmk_chip
->chip
.parent
,
1796 "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1797 pin
, cfg
, pullnames
[pull
], slpmnames
[slpm
],
1798 output
? "output " : "input",
1799 output
? (val
? "high" : "low") : "",
1800 lowemi
? "on" : "off");
1802 clk_enable(nmk_chip
->clk
);
1803 bit
= pin
% NMK_GPIO_PER_CHIP
;
1805 /* No glitch when going to GPIO mode */
1806 __nmk_gpio_set_mode(nmk_chip
, bit
, NMK_GPIO_ALT_GPIO
);
1808 __nmk_gpio_make_output(nmk_chip
, bit
, val
);
1810 __nmk_gpio_make_input(nmk_chip
, bit
);
1811 __nmk_gpio_set_pull(nmk_chip
, bit
, pull
);
1813 /* TODO: isn't this only applicable on output pins? */
1814 __nmk_gpio_set_lowemi(nmk_chip
, bit
, lowemi
);
1816 __nmk_gpio_set_slpm(nmk_chip
, bit
, slpm
);
1817 clk_disable(nmk_chip
->clk
);
1818 } /* for each config */
1823 static const struct pinconf_ops nmk_pinconf_ops
= {
1824 .pin_config_get
= nmk_pin_config_get
,
1825 .pin_config_set
= nmk_pin_config_set
,
1828 static struct pinctrl_desc nmk_pinctrl_desc
= {
1829 .name
= "pinctrl-nomadik",
1830 .pctlops
= &nmk_pinctrl_ops
,
1831 .pmxops
= &nmk_pinmux_ops
,
1832 .confops
= &nmk_pinconf_ops
,
1833 .owner
= THIS_MODULE
,
1836 static const struct of_device_id nmk_pinctrl_match
[] = {
1838 .compatible
= "stericsson,stn8815-pinctrl",
1839 .data
= (void *)PINCTRL_NMK_STN8815
,
1842 .compatible
= "stericsson,db8500-pinctrl",
1843 .data
= (void *)PINCTRL_NMK_DB8500
,
1846 .compatible
= "stericsson,db8540-pinctrl",
1847 .data
= (void *)PINCTRL_NMK_DB8540
,
1852 #ifdef CONFIG_PM_SLEEP
1853 static int nmk_pinctrl_suspend(struct device
*dev
)
1855 struct nmk_pinctrl
*npct
;
1857 npct
= dev_get_drvdata(dev
);
1861 return pinctrl_force_sleep(npct
->pctl
);
1864 static int nmk_pinctrl_resume(struct device
*dev
)
1866 struct nmk_pinctrl
*npct
;
1868 npct
= dev_get_drvdata(dev
);
1872 return pinctrl_force_default(npct
->pctl
);
1876 static int nmk_pinctrl_probe(struct platform_device
*pdev
)
1878 const struct of_device_id
*match
;
1879 struct device_node
*np
= pdev
->dev
.of_node
;
1880 struct device_node
*prcm_np
;
1881 struct nmk_pinctrl
*npct
;
1882 unsigned int version
= 0;
1885 npct
= devm_kzalloc(&pdev
->dev
, sizeof(*npct
), GFP_KERNEL
);
1889 match
= of_match_device(nmk_pinctrl_match
, &pdev
->dev
);
1892 version
= (unsigned int) match
->data
;
1894 /* Poke in other ASIC variants here */
1895 if (version
== PINCTRL_NMK_STN8815
)
1896 nmk_pinctrl_stn8815_init(&npct
->soc
);
1897 if (version
== PINCTRL_NMK_DB8500
)
1898 nmk_pinctrl_db8500_init(&npct
->soc
);
1899 if (version
== PINCTRL_NMK_DB8540
)
1900 nmk_pinctrl_db8540_init(&npct
->soc
);
1903 * Since we depend on the GPIO chips to provide clock and register base
1904 * for the pin control operations, make sure that we have these
1905 * populated before we continue. Follow the phandles to instantiate
1906 * them. The GPIO portion of the actual hardware may be probed before
1907 * or after this point: it shouldn't matter as the APIs are orthogonal.
1909 for (i
= 0; i
< NMK_MAX_BANKS
; i
++) {
1910 struct device_node
*gpio_np
;
1911 struct nmk_gpio_chip
*nmk_chip
;
1913 gpio_np
= of_parse_phandle(np
, "nomadik-gpio-chips", i
);
1915 dev_info(&pdev
->dev
,
1916 "populate NMK GPIO %d \"%pOFn\"\n",
1918 nmk_chip
= nmk_gpio_populate_chip(gpio_np
, pdev
);
1919 if (IS_ERR(nmk_chip
))
1921 "could not populate nmk chip struct "
1922 "- continue anyway\n");
1923 of_node_put(gpio_np
);
1927 prcm_np
= of_parse_phandle(np
, "prcm", 0);
1929 npct
->prcm_base
= of_iomap(prcm_np
, 0);
1930 if (!npct
->prcm_base
) {
1931 if (version
== PINCTRL_NMK_STN8815
) {
1932 dev_info(&pdev
->dev
,
1934 "assuming no ALT-Cx control is available\n");
1936 dev_err(&pdev
->dev
, "missing PRCM base address\n");
1941 nmk_pinctrl_desc
.pins
= npct
->soc
->pins
;
1942 nmk_pinctrl_desc
.npins
= npct
->soc
->npins
;
1943 npct
->dev
= &pdev
->dev
;
1945 npct
->pctl
= devm_pinctrl_register(&pdev
->dev
, &nmk_pinctrl_desc
, npct
);
1946 if (IS_ERR(npct
->pctl
)) {
1947 dev_err(&pdev
->dev
, "could not register Nomadik pinctrl driver\n");
1948 return PTR_ERR(npct
->pctl
);
1951 platform_set_drvdata(pdev
, npct
);
1952 dev_info(&pdev
->dev
, "initialized Nomadik pin control driver\n");
1957 static const struct of_device_id nmk_gpio_match
[] = {
1958 { .compatible
= "st,nomadik-gpio", },
1962 static struct platform_driver nmk_gpio_driver
= {
1965 .of_match_table
= nmk_gpio_match
,
1967 .probe
= nmk_gpio_probe
,
1970 static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops
,
1971 nmk_pinctrl_suspend
,
1972 nmk_pinctrl_resume
);
1974 static struct platform_driver nmk_pinctrl_driver
= {
1976 .name
= "pinctrl-nomadik",
1977 .of_match_table
= nmk_pinctrl_match
,
1978 .pm
= &nmk_pinctrl_pm_ops
,
1980 .probe
= nmk_pinctrl_probe
,
1983 static int __init
nmk_gpio_init(void)
1985 return platform_driver_register(&nmk_gpio_driver
);
1987 subsys_initcall(nmk_gpio_init
);
1989 static int __init
nmk_pinctrl_init(void)
1991 return platform_driver_register(&nmk_pinctrl_driver
);
1993 core_initcall(nmk_pinctrl_init
);