1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic GPIO driver for logic cells found in the Nomadik SoC
5 * Copyright (C) 2008,2009 STMicroelectronics
6 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
7 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
8 * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
21 #include <linux/of_device.h>
22 #include <linux/of_address.h>
23 #include <linux/bitops.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
27 #include <linux/pinctrl/pinconf.h>
28 /* Since we request GPIOs from ourself */
29 #include <linux/pinctrl/consumer.h>
30 #include "pinctrl-nomadik.h"
32 #include "../pinctrl-utils.h"
35 * The GPIO module in the Nomadik family of Systems-on-Chip is an
36 * AMBA device, managing 32 pins and alternate functions. The logic block
37 * is currently used in the Nomadik and ux500.
39 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
43 * pin configurations are represented by 32-bit integers:
45 * bit 0.. 8 - Pin Number (512 Pins Maximum)
46 * bit 9..10 - Alternate Function Selection
47 * bit 11..12 - Pull up/down state
48 * bit 13 - Sleep mode behaviour
50 * bit 15 - Value (if output)
51 * bit 16..18 - SLPM pull up/down state
52 * bit 19..20 - SLPM direction
53 * bit 21..22 - SLPM Value (if output)
54 * bit 23..25 - PDIS value (if input)
58 * to facilitate the definition, the following macros are provided
60 * PIN_CFG_DEFAULT - default config (0):
61 * pull up/down = disabled
62 * sleep mode = input/wakeup
65 * SLPM direction = same as normal
66 * SLPM pull = same as normal
67 * SLPM value = same as normal
69 * PIN_CFG - default config with alternate function
72 typedef unsigned long pin_cfg_t
;
74 #define PIN_NUM_MASK 0x1ff
75 #define PIN_NUM(x) ((x) & PIN_NUM_MASK)
77 #define PIN_ALT_SHIFT 9
78 #define PIN_ALT_MASK (0x3 << PIN_ALT_SHIFT)
79 #define PIN_ALT(x) (((x) & PIN_ALT_MASK) >> PIN_ALT_SHIFT)
80 #define PIN_GPIO (NMK_GPIO_ALT_GPIO << PIN_ALT_SHIFT)
81 #define PIN_ALT_A (NMK_GPIO_ALT_A << PIN_ALT_SHIFT)
82 #define PIN_ALT_B (NMK_GPIO_ALT_B << PIN_ALT_SHIFT)
83 #define PIN_ALT_C (NMK_GPIO_ALT_C << PIN_ALT_SHIFT)
85 #define PIN_PULL_SHIFT 11
86 #define PIN_PULL_MASK (0x3 << PIN_PULL_SHIFT)
87 #define PIN_PULL(x) (((x) & PIN_PULL_MASK) >> PIN_PULL_SHIFT)
88 #define PIN_PULL_NONE (NMK_GPIO_PULL_NONE << PIN_PULL_SHIFT)
89 #define PIN_PULL_UP (NMK_GPIO_PULL_UP << PIN_PULL_SHIFT)
90 #define PIN_PULL_DOWN (NMK_GPIO_PULL_DOWN << PIN_PULL_SHIFT)
92 #define PIN_SLPM_SHIFT 13
93 #define PIN_SLPM_MASK (0x1 << PIN_SLPM_SHIFT)
94 #define PIN_SLPM(x) (((x) & PIN_SLPM_MASK) >> PIN_SLPM_SHIFT)
95 #define PIN_SLPM_MAKE_INPUT (NMK_GPIO_SLPM_INPUT << PIN_SLPM_SHIFT)
96 #define PIN_SLPM_NOCHANGE (NMK_GPIO_SLPM_NOCHANGE << PIN_SLPM_SHIFT)
97 /* These two replace the above in DB8500v2+ */
98 #define PIN_SLPM_WAKEUP_ENABLE (NMK_GPIO_SLPM_WAKEUP_ENABLE << PIN_SLPM_SHIFT)
99 #define PIN_SLPM_WAKEUP_DISABLE (NMK_GPIO_SLPM_WAKEUP_DISABLE << PIN_SLPM_SHIFT)
100 #define PIN_SLPM_USE_MUX_SETTINGS_IN_SLEEP PIN_SLPM_WAKEUP_DISABLE
102 #define PIN_SLPM_GPIO PIN_SLPM_WAKEUP_ENABLE /* In SLPM, pin is a gpio */
103 #define PIN_SLPM_ALTFUNC PIN_SLPM_WAKEUP_DISABLE /* In SLPM, pin is altfunc */
105 #define PIN_DIR_SHIFT 14
106 #define PIN_DIR_MASK (0x1 << PIN_DIR_SHIFT)
107 #define PIN_DIR(x) (((x) & PIN_DIR_MASK) >> PIN_DIR_SHIFT)
108 #define PIN_DIR_INPUT (0 << PIN_DIR_SHIFT)
109 #define PIN_DIR_OUTPUT (1 << PIN_DIR_SHIFT)
111 #define PIN_VAL_SHIFT 15
112 #define PIN_VAL_MASK (0x1 << PIN_VAL_SHIFT)
113 #define PIN_VAL(x) (((x) & PIN_VAL_MASK) >> PIN_VAL_SHIFT)
114 #define PIN_VAL_LOW (0 << PIN_VAL_SHIFT)
115 #define PIN_VAL_HIGH (1 << PIN_VAL_SHIFT)
117 #define PIN_SLPM_PULL_SHIFT 16
118 #define PIN_SLPM_PULL_MASK (0x7 << PIN_SLPM_PULL_SHIFT)
119 #define PIN_SLPM_PULL(x) \
120 (((x) & PIN_SLPM_PULL_MASK) >> PIN_SLPM_PULL_SHIFT)
121 #define PIN_SLPM_PULL_NONE \
122 ((1 + NMK_GPIO_PULL_NONE) << PIN_SLPM_PULL_SHIFT)
123 #define PIN_SLPM_PULL_UP \
124 ((1 + NMK_GPIO_PULL_UP) << PIN_SLPM_PULL_SHIFT)
125 #define PIN_SLPM_PULL_DOWN \
126 ((1 + NMK_GPIO_PULL_DOWN) << PIN_SLPM_PULL_SHIFT)
128 #define PIN_SLPM_DIR_SHIFT 19
129 #define PIN_SLPM_DIR_MASK (0x3 << PIN_SLPM_DIR_SHIFT)
130 #define PIN_SLPM_DIR(x) \
131 (((x) & PIN_SLPM_DIR_MASK) >> PIN_SLPM_DIR_SHIFT)
132 #define PIN_SLPM_DIR_INPUT ((1 + 0) << PIN_SLPM_DIR_SHIFT)
133 #define PIN_SLPM_DIR_OUTPUT ((1 + 1) << PIN_SLPM_DIR_SHIFT)
135 #define PIN_SLPM_VAL_SHIFT 21
136 #define PIN_SLPM_VAL_MASK (0x3 << PIN_SLPM_VAL_SHIFT)
137 #define PIN_SLPM_VAL(x) \
138 (((x) & PIN_SLPM_VAL_MASK) >> PIN_SLPM_VAL_SHIFT)
139 #define PIN_SLPM_VAL_LOW ((1 + 0) << PIN_SLPM_VAL_SHIFT)
140 #define PIN_SLPM_VAL_HIGH ((1 + 1) << PIN_SLPM_VAL_SHIFT)
142 #define PIN_SLPM_PDIS_SHIFT 23
143 #define PIN_SLPM_PDIS_MASK (0x3 << PIN_SLPM_PDIS_SHIFT)
144 #define PIN_SLPM_PDIS(x) \
145 (((x) & PIN_SLPM_PDIS_MASK) >> PIN_SLPM_PDIS_SHIFT)
146 #define PIN_SLPM_PDIS_NO_CHANGE (0 << PIN_SLPM_PDIS_SHIFT)
147 #define PIN_SLPM_PDIS_DISABLED (1 << PIN_SLPM_PDIS_SHIFT)
148 #define PIN_SLPM_PDIS_ENABLED (2 << PIN_SLPM_PDIS_SHIFT)
150 #define PIN_LOWEMI_SHIFT 25
151 #define PIN_LOWEMI_MASK (0x1 << PIN_LOWEMI_SHIFT)
152 #define PIN_LOWEMI(x) (((x) & PIN_LOWEMI_MASK) >> PIN_LOWEMI_SHIFT)
153 #define PIN_LOWEMI_DISABLED (0 << PIN_LOWEMI_SHIFT)
154 #define PIN_LOWEMI_ENABLED (1 << PIN_LOWEMI_SHIFT)
156 #define PIN_GPIOMODE_SHIFT 26
157 #define PIN_GPIOMODE_MASK (0x1 << PIN_GPIOMODE_SHIFT)
158 #define PIN_GPIOMODE(x) (((x) & PIN_GPIOMODE_MASK) >> PIN_GPIOMODE_SHIFT)
159 #define PIN_GPIOMODE_DISABLED (0 << PIN_GPIOMODE_SHIFT)
160 #define PIN_GPIOMODE_ENABLED (1 << PIN_GPIOMODE_SHIFT)
162 #define PIN_SLEEPMODE_SHIFT 27
163 #define PIN_SLEEPMODE_MASK (0x1 << PIN_SLEEPMODE_SHIFT)
164 #define PIN_SLEEPMODE(x) (((x) & PIN_SLEEPMODE_MASK) >> PIN_SLEEPMODE_SHIFT)
165 #define PIN_SLEEPMODE_DISABLED (0 << PIN_SLEEPMODE_SHIFT)
166 #define PIN_SLEEPMODE_ENABLED (1 << PIN_SLEEPMODE_SHIFT)
169 /* Shortcuts. Use these instead of separate DIR, PULL, and VAL. */
170 #define PIN_INPUT_PULLDOWN (PIN_DIR_INPUT | PIN_PULL_DOWN)
171 #define PIN_INPUT_PULLUP (PIN_DIR_INPUT | PIN_PULL_UP)
172 #define PIN_INPUT_NOPULL (PIN_DIR_INPUT | PIN_PULL_NONE)
173 #define PIN_OUTPUT_LOW (PIN_DIR_OUTPUT | PIN_VAL_LOW)
174 #define PIN_OUTPUT_HIGH (PIN_DIR_OUTPUT | PIN_VAL_HIGH)
176 #define PIN_SLPM_INPUT_PULLDOWN (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_DOWN)
177 #define PIN_SLPM_INPUT_PULLUP (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_UP)
178 #define PIN_SLPM_INPUT_NOPULL (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_NONE)
179 #define PIN_SLPM_OUTPUT_LOW (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_LOW)
180 #define PIN_SLPM_OUTPUT_HIGH (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_HIGH)
182 #define PIN_CFG_DEFAULT (0)
184 #define PIN_CFG(num, alt) \
186 (PIN_NUM(num) | PIN_##alt))
188 #define PIN_CFG_INPUT(num, alt, pull) \
190 (PIN_NUM(num) | PIN_##alt | PIN_INPUT_##pull))
192 #define PIN_CFG_OUTPUT(num, alt, val) \
194 (PIN_NUM(num) | PIN_##alt | PIN_OUTPUT_##val))
197 * "nmk_gpio" and "NMK_GPIO" stand for "Nomadik GPIO", leaving
198 * the "gpio" namespace for generic and cross-machine functions
201 #define GPIO_BLOCK_SHIFT 5
202 #define NMK_GPIO_PER_CHIP (1 << GPIO_BLOCK_SHIFT)
203 #define NMK_MAX_BANKS DIV_ROUND_UP(512, NMK_GPIO_PER_CHIP)
205 /* Register in the logic block */
206 #define NMK_GPIO_DAT 0x00
207 #define NMK_GPIO_DATS 0x04
208 #define NMK_GPIO_DATC 0x08
209 #define NMK_GPIO_PDIS 0x0c
210 #define NMK_GPIO_DIR 0x10
211 #define NMK_GPIO_DIRS 0x14
212 #define NMK_GPIO_DIRC 0x18
213 #define NMK_GPIO_SLPC 0x1c
214 #define NMK_GPIO_AFSLA 0x20
215 #define NMK_GPIO_AFSLB 0x24
216 #define NMK_GPIO_LOWEMI 0x28
218 #define NMK_GPIO_RIMSC 0x40
219 #define NMK_GPIO_FIMSC 0x44
220 #define NMK_GPIO_IS 0x48
221 #define NMK_GPIO_IC 0x4c
222 #define NMK_GPIO_RWIMSC 0x50
223 #define NMK_GPIO_FWIMSC 0x54
224 #define NMK_GPIO_WKS 0x58
225 /* These appear in DB8540 and later ASICs */
226 #define NMK_GPIO_EDGELEVEL 0x5C
227 #define NMK_GPIO_LEVEL 0x60
230 /* Pull up/down values */
240 NMK_GPIO_SLPM_WAKEUP_ENABLE
= NMK_GPIO_SLPM_INPUT
,
241 NMK_GPIO_SLPM_NOCHANGE
,
242 NMK_GPIO_SLPM_WAKEUP_DISABLE
= NMK_GPIO_SLPM_NOCHANGE
,
245 struct nmk_gpio_chip
{
246 struct gpio_chip chip
;
247 struct irq_chip irqchip
;
251 unsigned int parent_irq
;
252 int latent_parent_irq
;
253 u32 (*get_latent_status
)(unsigned int bank
);
254 void (*set_ioforce
)(bool enable
);
257 /* Keep track of configured edges */
270 * struct nmk_pinctrl - state container for the Nomadik pin controller
271 * @dev: containing device pointer
272 * @pctl: corresponding pin controller device
273 * @soc: SoC data for this specific chip
274 * @prcm_base: PRCM register range virtual base
278 struct pinctrl_dev
*pctl
;
279 const struct nmk_pinctrl_soc_data
*soc
;
280 void __iomem
*prcm_base
;
283 static struct nmk_gpio_chip
*nmk_gpio_chips
[NMK_MAX_BANKS
];
285 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock
);
287 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
289 static void __nmk_gpio_set_mode(struct nmk_gpio_chip
*nmk_chip
,
290 unsigned offset
, int gpio_mode
)
294 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & ~BIT(offset
);
295 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & ~BIT(offset
);
296 if (gpio_mode
& NMK_GPIO_ALT_A
)
297 afunc
|= BIT(offset
);
298 if (gpio_mode
& NMK_GPIO_ALT_B
)
299 bfunc
|= BIT(offset
);
300 writel(afunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLA
);
301 writel(bfunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLB
);
304 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip
*nmk_chip
,
305 unsigned offset
, enum nmk_gpio_slpm mode
)
309 slpm
= readl(nmk_chip
->addr
+ NMK_GPIO_SLPC
);
310 if (mode
== NMK_GPIO_SLPM_NOCHANGE
)
313 slpm
&= ~BIT(offset
);
314 writel(slpm
, nmk_chip
->addr
+ NMK_GPIO_SLPC
);
317 static void __nmk_gpio_set_pull(struct nmk_gpio_chip
*nmk_chip
,
318 unsigned offset
, enum nmk_gpio_pull pull
)
322 pdis
= readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
);
323 if (pull
== NMK_GPIO_PULL_NONE
) {
325 nmk_chip
->pull_up
&= ~BIT(offset
);
327 pdis
&= ~BIT(offset
);
330 writel(pdis
, nmk_chip
->addr
+ NMK_GPIO_PDIS
);
332 if (pull
== NMK_GPIO_PULL_UP
) {
333 nmk_chip
->pull_up
|= BIT(offset
);
334 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATS
);
335 } else if (pull
== NMK_GPIO_PULL_DOWN
) {
336 nmk_chip
->pull_up
&= ~BIT(offset
);
337 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATC
);
341 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip
*nmk_chip
,
342 unsigned offset
, bool lowemi
)
344 bool enabled
= nmk_chip
->lowemi
& BIT(offset
);
346 if (lowemi
== enabled
)
350 nmk_chip
->lowemi
|= BIT(offset
);
352 nmk_chip
->lowemi
&= ~BIT(offset
);
354 writel_relaxed(nmk_chip
->lowemi
,
355 nmk_chip
->addr
+ NMK_GPIO_LOWEMI
);
358 static void __nmk_gpio_make_input(struct nmk_gpio_chip
*nmk_chip
,
361 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRC
);
364 static void __nmk_gpio_set_output(struct nmk_gpio_chip
*nmk_chip
,
365 unsigned offset
, int val
)
368 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATS
);
370 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DATC
);
373 static void __nmk_gpio_make_output(struct nmk_gpio_chip
*nmk_chip
,
374 unsigned offset
, int val
)
376 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRS
);
377 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
380 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip
*nmk_chip
,
381 unsigned offset
, int gpio_mode
,
384 u32 rwimsc
= nmk_chip
->rwimsc
;
385 u32 fwimsc
= nmk_chip
->fwimsc
;
387 if (glitch
&& nmk_chip
->set_ioforce
) {
388 u32 bit
= BIT(offset
);
390 /* Prevent spurious wakeups */
391 writel(rwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
392 writel(fwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
394 nmk_chip
->set_ioforce(true);
397 __nmk_gpio_set_mode(nmk_chip
, offset
, gpio_mode
);
399 if (glitch
&& nmk_chip
->set_ioforce
) {
400 nmk_chip
->set_ioforce(false);
402 writel(rwimsc
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
403 writel(fwimsc
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
408 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip
*nmk_chip
, unsigned offset
)
410 u32 falling
= nmk_chip
->fimsc
& BIT(offset
);
411 u32 rising
= nmk_chip
->rimsc
& BIT(offset
);
412 int gpio
= nmk_chip
->chip
.base
+ offset
;
413 int irq
= irq_find_mapping(nmk_chip
->chip
.irq
.domain
, offset
);
414 struct irq_data
*d
= irq_get_irq_data(irq
);
416 if (!rising
&& !falling
)
419 if (!d
|| !irqd_irq_disabled(d
))
423 nmk_chip
->rimsc
&= ~BIT(offset
);
424 writel_relaxed(nmk_chip
->rimsc
,
425 nmk_chip
->addr
+ NMK_GPIO_RIMSC
);
429 nmk_chip
->fimsc
&= ~BIT(offset
);
430 writel_relaxed(nmk_chip
->fimsc
,
431 nmk_chip
->addr
+ NMK_GPIO_FIMSC
);
434 dev_dbg(nmk_chip
->chip
.parent
, "%d: clearing interrupt mask\n", gpio
);
437 static void nmk_write_masked(void __iomem
*reg
, u32 mask
, u32 value
)
442 val
= ((val
& ~mask
) | (value
& mask
));
446 static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl
*npct
,
447 unsigned offset
, unsigned alt_num
)
453 const struct prcm_gpiocr_altcx_pin_desc
*pin_desc
;
454 const u16
*gpiocr_regs
;
456 if (!npct
->prcm_base
)
459 if (alt_num
> PRCM_IDX_GPIOCR_ALTC_MAX
) {
460 dev_err(npct
->dev
, "PRCM GPIOCR: alternate-C%i is invalid\n",
465 for (i
= 0 ; i
< npct
->soc
->npins_altcx
; i
++) {
466 if (npct
->soc
->altcx_pins
[i
].pin
== offset
)
469 if (i
== npct
->soc
->npins_altcx
) {
470 dev_dbg(npct
->dev
, "PRCM GPIOCR: pin %i is not found\n",
475 pin_desc
= npct
->soc
->altcx_pins
+ i
;
476 gpiocr_regs
= npct
->soc
->prcm_gpiocr_registers
;
479 * If alt_num is NULL, just clear current ALTCx selection
480 * to make sure we come back to a pure ALTC selection
483 for (i
= 0 ; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
484 if (pin_desc
->altcx
[i
].used
== true) {
485 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
486 bit
= pin_desc
->altcx
[i
].control_bit
;
487 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
)) {
488 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), 0);
490 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
498 alt_index
= alt_num
- 1;
499 if (pin_desc
->altcx
[alt_index
].used
== false) {
501 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
507 * Check if any other ALTCx functions are activated on this pin
508 * and disable it first.
510 for (i
= 0 ; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
513 if (pin_desc
->altcx
[i
].used
== true) {
514 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
515 bit
= pin_desc
->altcx
[i
].control_bit
;
516 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
)) {
517 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), 0);
519 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
525 reg
= gpiocr_regs
[pin_desc
->altcx
[alt_index
].reg_index
];
526 bit
= pin_desc
->altcx
[alt_index
].control_bit
;
527 dev_dbg(npct
->dev
, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
528 offset
, alt_index
+1);
529 nmk_write_masked(npct
->prcm_base
+ reg
, BIT(bit
), BIT(bit
));
533 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
534 * - Save SLPM registers
535 * - Set SLPM=0 for the IOs you want to switch and others to 1
536 * - Configure the GPIO registers for the IOs that are being switched
538 * - Modify the AFLSA/B registers for the IOs that are being switched
540 * - Restore SLPM registers
541 * - Any spurious wake up event during switch sequence to be ignored and
544 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm
)
548 for (i
= 0; i
< NUM_BANKS
; i
++) {
549 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
550 unsigned int temp
= slpm
[i
];
555 clk_enable(chip
->clk
);
557 slpm
[i
] = readl(chip
->addr
+ NMK_GPIO_SLPC
);
558 writel(temp
, chip
->addr
+ NMK_GPIO_SLPC
);
562 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm
)
566 for (i
= 0; i
< NUM_BANKS
; i
++) {
567 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
572 writel(slpm
[i
], chip
->addr
+ NMK_GPIO_SLPC
);
574 clk_disable(chip
->clk
);
578 static int __maybe_unused
nmk_prcm_gpiocr_get_mode(struct pinctrl_dev
*pctldev
, int gpio
)
583 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
584 const struct prcm_gpiocr_altcx_pin_desc
*pin_desc
;
585 const u16
*gpiocr_regs
;
587 if (!npct
->prcm_base
)
588 return NMK_GPIO_ALT_C
;
590 for (i
= 0; i
< npct
->soc
->npins_altcx
; i
++) {
591 if (npct
->soc
->altcx_pins
[i
].pin
== gpio
)
594 if (i
== npct
->soc
->npins_altcx
)
595 return NMK_GPIO_ALT_C
;
597 pin_desc
= npct
->soc
->altcx_pins
+ i
;
598 gpiocr_regs
= npct
->soc
->prcm_gpiocr_registers
;
599 for (i
= 0; i
< PRCM_IDX_GPIOCR_ALTC_MAX
; i
++) {
600 if (pin_desc
->altcx
[i
].used
== true) {
601 reg
= gpiocr_regs
[pin_desc
->altcx
[i
].reg_index
];
602 bit
= pin_desc
->altcx
[i
].control_bit
;
603 if (readl(npct
->prcm_base
+ reg
) & BIT(bit
))
604 return NMK_GPIO_ALT_C
+i
+1;
607 return NMK_GPIO_ALT_C
;
612 static void nmk_gpio_irq_ack(struct irq_data
*d
)
614 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
615 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
617 clk_enable(nmk_chip
->clk
);
618 writel(BIT(d
->hwirq
), nmk_chip
->addr
+ NMK_GPIO_IC
);
619 clk_disable(nmk_chip
->clk
);
622 enum nmk_gpio_irq_type
{
627 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip
*nmk_chip
,
628 int offset
, enum nmk_gpio_irq_type which
,
636 if (which
== NORMAL
) {
637 rimscreg
= NMK_GPIO_RIMSC
;
638 fimscreg
= NMK_GPIO_FIMSC
;
639 rimscval
= &nmk_chip
->rimsc
;
640 fimscval
= &nmk_chip
->fimsc
;
642 rimscreg
= NMK_GPIO_RWIMSC
;
643 fimscreg
= NMK_GPIO_FWIMSC
;
644 rimscval
= &nmk_chip
->rwimsc
;
645 fimscval
= &nmk_chip
->fwimsc
;
648 /* we must individually set/clear the two edges */
649 if (nmk_chip
->edge_rising
& BIT(offset
)) {
651 *rimscval
|= BIT(offset
);
653 *rimscval
&= ~BIT(offset
);
654 writel(*rimscval
, nmk_chip
->addr
+ rimscreg
);
656 if (nmk_chip
->edge_falling
& BIT(offset
)) {
658 *fimscval
|= BIT(offset
);
660 *fimscval
&= ~BIT(offset
);
661 writel(*fimscval
, nmk_chip
->addr
+ fimscreg
);
665 static void __nmk_gpio_set_wake(struct nmk_gpio_chip
*nmk_chip
,
669 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
670 * disabled, since setting SLPM to 1 increases power consumption, and
671 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
673 if (nmk_chip
->sleepmode
&& on
) {
674 __nmk_gpio_set_slpm(nmk_chip
, offset
,
675 NMK_GPIO_SLPM_WAKEUP_ENABLE
);
678 __nmk_gpio_irq_modify(nmk_chip
, offset
, WAKE
, on
);
681 static int nmk_gpio_irq_maskunmask(struct irq_data
*d
, bool enable
)
683 struct nmk_gpio_chip
*nmk_chip
;
686 nmk_chip
= irq_data_get_irq_chip_data(d
);
690 clk_enable(nmk_chip
->clk
);
691 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
692 spin_lock(&nmk_chip
->lock
);
694 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, NORMAL
, enable
);
696 if (!(nmk_chip
->real_wake
& BIT(d
->hwirq
)))
697 __nmk_gpio_set_wake(nmk_chip
, d
->hwirq
, enable
);
699 spin_unlock(&nmk_chip
->lock
);
700 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
701 clk_disable(nmk_chip
->clk
);
706 static void nmk_gpio_irq_mask(struct irq_data
*d
)
708 nmk_gpio_irq_maskunmask(d
, false);
711 static void nmk_gpio_irq_unmask(struct irq_data
*d
)
713 nmk_gpio_irq_maskunmask(d
, true);
716 static int nmk_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
718 struct nmk_gpio_chip
*nmk_chip
;
721 nmk_chip
= irq_data_get_irq_chip_data(d
);
725 clk_enable(nmk_chip
->clk
);
726 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
727 spin_lock(&nmk_chip
->lock
);
729 if (irqd_irq_disabled(d
))
730 __nmk_gpio_set_wake(nmk_chip
, d
->hwirq
, on
);
733 nmk_chip
->real_wake
|= BIT(d
->hwirq
);
735 nmk_chip
->real_wake
&= ~BIT(d
->hwirq
);
737 spin_unlock(&nmk_chip
->lock
);
738 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
739 clk_disable(nmk_chip
->clk
);
744 static int nmk_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
746 bool enabled
= !irqd_irq_disabled(d
);
747 bool wake
= irqd_is_wakeup_set(d
);
748 struct nmk_gpio_chip
*nmk_chip
;
751 nmk_chip
= irq_data_get_irq_chip_data(d
);
754 if (type
& IRQ_TYPE_LEVEL_HIGH
)
756 if (type
& IRQ_TYPE_LEVEL_LOW
)
759 clk_enable(nmk_chip
->clk
);
760 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
763 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, NORMAL
, false);
766 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, WAKE
, false);
768 nmk_chip
->edge_rising
&= ~BIT(d
->hwirq
);
769 if (type
& IRQ_TYPE_EDGE_RISING
)
770 nmk_chip
->edge_rising
|= BIT(d
->hwirq
);
772 nmk_chip
->edge_falling
&= ~BIT(d
->hwirq
);
773 if (type
& IRQ_TYPE_EDGE_FALLING
)
774 nmk_chip
->edge_falling
|= BIT(d
->hwirq
);
777 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, NORMAL
, true);
780 __nmk_gpio_irq_modify(nmk_chip
, d
->hwirq
, WAKE
, true);
782 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
783 clk_disable(nmk_chip
->clk
);
788 static unsigned int nmk_gpio_irq_startup(struct irq_data
*d
)
790 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
792 clk_enable(nmk_chip
->clk
);
793 nmk_gpio_irq_unmask(d
);
797 static void nmk_gpio_irq_shutdown(struct irq_data
*d
)
799 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
801 nmk_gpio_irq_mask(d
);
802 clk_disable(nmk_chip
->clk
);
805 static void __nmk_gpio_irq_handler(struct irq_desc
*desc
, u32 status
)
807 struct irq_chip
*host_chip
= irq_desc_get_chip(desc
);
808 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
810 chained_irq_enter(host_chip
, desc
);
813 int bit
= __ffs(status
);
815 generic_handle_irq(irq_find_mapping(chip
->irq
.domain
, bit
));
819 chained_irq_exit(host_chip
, desc
);
822 static void nmk_gpio_irq_handler(struct irq_desc
*desc
)
824 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
825 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
828 clk_enable(nmk_chip
->clk
);
829 status
= readl(nmk_chip
->addr
+ NMK_GPIO_IS
);
830 clk_disable(nmk_chip
->clk
);
832 __nmk_gpio_irq_handler(desc
, status
);
835 static void nmk_gpio_latent_irq_handler(struct irq_desc
*desc
)
837 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
838 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
839 u32 status
= nmk_chip
->get_latent_status(nmk_chip
->bank
);
841 __nmk_gpio_irq_handler(desc
, status
);
846 static int nmk_gpio_get_dir(struct gpio_chip
*chip
, unsigned offset
)
848 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
851 clk_enable(nmk_chip
->clk
);
853 dir
= !(readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & BIT(offset
));
855 clk_disable(nmk_chip
->clk
);
860 static int nmk_gpio_make_input(struct gpio_chip
*chip
, unsigned offset
)
862 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
864 clk_enable(nmk_chip
->clk
);
866 writel(BIT(offset
), nmk_chip
->addr
+ NMK_GPIO_DIRC
);
868 clk_disable(nmk_chip
->clk
);
873 static int nmk_gpio_get_input(struct gpio_chip
*chip
, unsigned offset
)
875 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
878 clk_enable(nmk_chip
->clk
);
880 value
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & BIT(offset
));
882 clk_disable(nmk_chip
->clk
);
887 static void nmk_gpio_set_output(struct gpio_chip
*chip
, unsigned offset
,
890 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
892 clk_enable(nmk_chip
->clk
);
894 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
896 clk_disable(nmk_chip
->clk
);
899 static int nmk_gpio_make_output(struct gpio_chip
*chip
, unsigned offset
,
902 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
904 clk_enable(nmk_chip
->clk
);
906 __nmk_gpio_make_output(nmk_chip
, offset
, val
);
908 clk_disable(nmk_chip
->clk
);
913 #ifdef CONFIG_DEBUG_FS
914 static int nmk_gpio_get_mode(struct nmk_gpio_chip
*nmk_chip
, int offset
)
918 clk_enable(nmk_chip
->clk
);
920 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & BIT(offset
);
921 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & BIT(offset
);
923 clk_disable(nmk_chip
->clk
);
925 return (afunc
? NMK_GPIO_ALT_A
: 0) | (bfunc
? NMK_GPIO_ALT_B
: 0);
928 #include <linux/seq_file.h>
930 static void nmk_gpio_dbg_show_one(struct seq_file
*s
,
931 struct pinctrl_dev
*pctldev
, struct gpio_chip
*chip
,
932 unsigned offset
, unsigned gpio
)
934 const char *label
= gpiochip_is_requested(chip
, offset
);
935 struct nmk_gpio_chip
*nmk_chip
= gpiochip_get_data(chip
);
940 const char *modes
[] = {
941 [NMK_GPIO_ALT_GPIO
] = "gpio",
942 [NMK_GPIO_ALT_A
] = "altA",
943 [NMK_GPIO_ALT_B
] = "altB",
944 [NMK_GPIO_ALT_C
] = "altC",
945 [NMK_GPIO_ALT_C
+1] = "altC1",
946 [NMK_GPIO_ALT_C
+2] = "altC2",
947 [NMK_GPIO_ALT_C
+3] = "altC3",
948 [NMK_GPIO_ALT_C
+4] = "altC4",
950 const char *pulls
[] = {
956 clk_enable(nmk_chip
->clk
);
957 is_out
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & BIT(offset
));
958 pull
= !(readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
) & BIT(offset
));
959 data_out
= !!(readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & BIT(offset
));
960 mode
= nmk_gpio_get_mode(nmk_chip
, offset
);
961 if ((mode
== NMK_GPIO_ALT_C
) && pctldev
)
962 mode
= nmk_prcm_gpiocr_get_mode(pctldev
, gpio
);
965 seq_printf(s
, " gpio-%-3d (%-20.20s) out %s %s",
968 data_out
? "hi" : "lo",
969 (mode
< 0) ? "unknown" : modes
[mode
]);
971 int irq
= chip
->to_irq(chip
, offset
);
972 struct irq_desc
*desc
= irq_to_desc(irq
);
977 pullidx
= data_out
? 2 : 1;
979 seq_printf(s
, " gpio-%-3d (%-20.20s) in %s %s",
983 (mode
< 0) ? "unknown" : modes
[mode
]);
985 val
= nmk_gpio_get_input(chip
, offset
);
986 seq_printf(s
, " VAL %d", val
);
989 * This races with request_irq(), set_irq_type(),
990 * and set_irq_wake() ... but those are "rare".
992 if (irq
> 0 && desc
&& desc
->action
) {
995 if (nmk_chip
->edge_rising
& BIT(offset
))
996 trigger
= "edge-rising";
997 else if (nmk_chip
->edge_falling
& BIT(offset
))
998 trigger
= "edge-falling";
1000 trigger
= "edge-undefined";
1002 seq_printf(s
, " irq-%d %s%s",
1004 irqd_is_wakeup_set(&desc
->irq_data
)
1008 clk_disable(nmk_chip
->clk
);
1011 static void nmk_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1014 unsigned gpio
= chip
->base
;
1016 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
1017 nmk_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
1018 seq_printf(s
, "\n");
1023 static inline void nmk_gpio_dbg_show_one(struct seq_file
*s
,
1024 struct pinctrl_dev
*pctldev
,
1025 struct gpio_chip
*chip
,
1026 unsigned offset
, unsigned gpio
)
1029 #define nmk_gpio_dbg_show NULL
1033 * We will allocate memory for the state container using devm* allocators
1034 * binding to the first device reaching this point, it doesn't matter if
1035 * it is the pin controller or GPIO driver. However we need to use the right
1036 * platform device when looking up resources so pay attention to pdev.
1038 static struct nmk_gpio_chip
*nmk_gpio_populate_chip(struct device_node
*np
,
1039 struct platform_device
*pdev
)
1041 struct nmk_gpio_chip
*nmk_chip
;
1042 struct platform_device
*gpio_pdev
;
1043 struct gpio_chip
*chip
;
1044 struct resource
*res
;
1049 gpio_pdev
= of_find_device_by_node(np
);
1051 pr_err("populate \"%pOFn\": device not found\n", np
);
1052 return ERR_PTR(-ENODEV
);
1054 if (of_property_read_u32(np
, "gpio-bank", &id
)) {
1055 dev_err(&pdev
->dev
, "populate: gpio-bank property not found\n");
1056 platform_device_put(gpio_pdev
);
1057 return ERR_PTR(-EINVAL
);
1060 /* Already populated? */
1061 nmk_chip
= nmk_gpio_chips
[id
];
1063 platform_device_put(gpio_pdev
);
1067 nmk_chip
= devm_kzalloc(&pdev
->dev
, sizeof(*nmk_chip
), GFP_KERNEL
);
1069 platform_device_put(gpio_pdev
);
1070 return ERR_PTR(-ENOMEM
);
1073 nmk_chip
->bank
= id
;
1074 chip
= &nmk_chip
->chip
;
1075 chip
->base
= id
* NMK_GPIO_PER_CHIP
;
1076 chip
->ngpio
= NMK_GPIO_PER_CHIP
;
1077 chip
->label
= dev_name(&gpio_pdev
->dev
);
1078 chip
->parent
= &gpio_pdev
->dev
;
1080 res
= platform_get_resource(gpio_pdev
, IORESOURCE_MEM
, 0);
1081 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1083 platform_device_put(gpio_pdev
);
1084 return ERR_CAST(base
);
1086 nmk_chip
->addr
= base
;
1088 clk
= clk_get(&gpio_pdev
->dev
, NULL
);
1090 platform_device_put(gpio_pdev
);
1091 return (void *) clk
;
1094 nmk_chip
->clk
= clk
;
1096 BUG_ON(nmk_chip
->bank
>= ARRAY_SIZE(nmk_gpio_chips
));
1097 nmk_gpio_chips
[id
] = nmk_chip
;
1101 static int nmk_gpio_probe(struct platform_device
*dev
)
1103 struct device_node
*np
= dev
->dev
.of_node
;
1104 struct nmk_gpio_chip
*nmk_chip
;
1105 struct gpio_chip
*chip
;
1106 struct irq_chip
*irqchip
;
1108 bool supports_sleepmode
;
1112 nmk_chip
= nmk_gpio_populate_chip(np
, dev
);
1113 if (IS_ERR(nmk_chip
)) {
1114 dev_err(&dev
->dev
, "could not populate nmk chip struct\n");
1115 return PTR_ERR(nmk_chip
);
1118 supports_sleepmode
=
1119 of_property_read_bool(np
, "st,supports-sleepmode");
1121 /* Correct platform device ID */
1122 dev
->id
= nmk_chip
->bank
;
1124 irq
= platform_get_irq(dev
, 0);
1128 /* It's OK for this IRQ not to be present */
1129 latent_irq
= platform_get_irq(dev
, 1);
1132 * The virt address in nmk_chip->addr is in the nomadik register space,
1133 * so we can simply convert the resource address, without remapping
1135 nmk_chip
->parent_irq
= irq
;
1136 nmk_chip
->latent_parent_irq
= latent_irq
;
1137 nmk_chip
->sleepmode
= supports_sleepmode
;
1138 spin_lock_init(&nmk_chip
->lock
);
1140 chip
= &nmk_chip
->chip
;
1141 chip
->request
= gpiochip_generic_request
;
1142 chip
->free
= gpiochip_generic_free
;
1143 chip
->get_direction
= nmk_gpio_get_dir
;
1144 chip
->direction_input
= nmk_gpio_make_input
;
1145 chip
->get
= nmk_gpio_get_input
;
1146 chip
->direction_output
= nmk_gpio_make_output
;
1147 chip
->set
= nmk_gpio_set_output
;
1148 chip
->dbg_show
= nmk_gpio_dbg_show
;
1149 chip
->can_sleep
= false;
1150 chip
->owner
= THIS_MODULE
;
1152 irqchip
= &nmk_chip
->irqchip
;
1153 irqchip
->irq_ack
= nmk_gpio_irq_ack
;
1154 irqchip
->irq_mask
= nmk_gpio_irq_mask
;
1155 irqchip
->irq_unmask
= nmk_gpio_irq_unmask
;
1156 irqchip
->irq_set_type
= nmk_gpio_irq_set_type
;
1157 irqchip
->irq_set_wake
= nmk_gpio_irq_set_wake
;
1158 irqchip
->irq_startup
= nmk_gpio_irq_startup
;
1159 irqchip
->irq_shutdown
= nmk_gpio_irq_shutdown
;
1160 irqchip
->flags
= IRQCHIP_MASK_ON_SUSPEND
;
1161 irqchip
->name
= kasprintf(GFP_KERNEL
, "nmk%u-%u-%u",
1164 chip
->base
+ chip
->ngpio
- 1);
1166 clk_enable(nmk_chip
->clk
);
1167 nmk_chip
->lowemi
= readl_relaxed(nmk_chip
->addr
+ NMK_GPIO_LOWEMI
);
1168 clk_disable(nmk_chip
->clk
);
1171 ret
= gpiochip_add_data(chip
, nmk_chip
);
1175 platform_set_drvdata(dev
, nmk_chip
);
1178 * Let the generic code handle this edge IRQ, the the chained
1179 * handler will perform the actual work of handling the parent
1182 ret
= gpiochip_irqchip_add(chip
,
1188 dev_err(&dev
->dev
, "could not add irqchip\n");
1189 gpiochip_remove(&nmk_chip
->chip
);
1192 /* Then register the chain on the parent IRQ */
1193 gpiochip_set_chained_irqchip(chip
,
1195 nmk_chip
->parent_irq
,
1196 nmk_gpio_irq_handler
);
1197 if (nmk_chip
->latent_parent_irq
> 0)
1198 gpiochip_set_chained_irqchip(chip
,
1200 nmk_chip
->latent_parent_irq
,
1201 nmk_gpio_latent_irq_handler
);
1203 dev_info(&dev
->dev
, "at address %p\n", nmk_chip
->addr
);
1208 static int nmk_get_groups_cnt(struct pinctrl_dev
*pctldev
)
1210 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1212 return npct
->soc
->ngroups
;
1215 static const char *nmk_get_group_name(struct pinctrl_dev
*pctldev
,
1218 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1220 return npct
->soc
->groups
[selector
].name
;
1223 static int nmk_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
1224 const unsigned **pins
,
1227 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1229 *pins
= npct
->soc
->groups
[selector
].pins
;
1230 *num_pins
= npct
->soc
->groups
[selector
].npins
;
1234 static struct nmk_gpio_chip
*find_nmk_gpio_from_pin(unsigned pin
)
1237 struct nmk_gpio_chip
*nmk_gpio
;
1239 for(i
= 0; i
< NMK_MAX_BANKS
; i
++) {
1240 nmk_gpio
= nmk_gpio_chips
[i
];
1243 if (pin
>= nmk_gpio
->chip
.base
&&
1244 pin
< nmk_gpio
->chip
.base
+ nmk_gpio
->chip
.ngpio
)
1250 static struct gpio_chip
*find_gc_from_pin(unsigned pin
)
1252 struct nmk_gpio_chip
*nmk_gpio
= find_nmk_gpio_from_pin(pin
);
1255 return &nmk_gpio
->chip
;
1259 static void nmk_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
1262 struct gpio_chip
*chip
= find_gc_from_pin(offset
);
1265 seq_printf(s
, "invalid pin offset");
1268 nmk_gpio_dbg_show_one(s
, pctldev
, chip
, offset
- chip
->base
, offset
);
1271 static int nmk_dt_add_map_mux(struct pinctrl_map
**map
, unsigned *reserved_maps
,
1272 unsigned *num_maps
, const char *group
,
1273 const char *function
)
1275 if (*num_maps
== *reserved_maps
)
1278 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
1279 (*map
)[*num_maps
].data
.mux
.group
= group
;
1280 (*map
)[*num_maps
].data
.mux
.function
= function
;
1286 static int nmk_dt_add_map_configs(struct pinctrl_map
**map
,
1287 unsigned *reserved_maps
,
1288 unsigned *num_maps
, const char *group
,
1289 unsigned long *configs
, unsigned num_configs
)
1291 unsigned long *dup_configs
;
1293 if (*num_maps
== *reserved_maps
)
1296 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
1301 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
1303 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
1304 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
1305 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
1311 #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
1312 #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
1313 .size = ARRAY_SIZE(y), }
1315 static const unsigned long nmk_pin_input_modes
[] = {
1321 static const unsigned long nmk_pin_output_modes
[] = {
1327 static const unsigned long nmk_pin_sleep_modes
[] = {
1328 PIN_SLEEPMODE_DISABLED
,
1329 PIN_SLEEPMODE_ENABLED
,
1332 static const unsigned long nmk_pin_sleep_input_modes
[] = {
1333 PIN_SLPM_INPUT_NOPULL
,
1334 PIN_SLPM_INPUT_PULLUP
,
1335 PIN_SLPM_INPUT_PULLDOWN
,
1339 static const unsigned long nmk_pin_sleep_output_modes
[] = {
1340 PIN_SLPM_OUTPUT_LOW
,
1341 PIN_SLPM_OUTPUT_HIGH
,
1342 PIN_SLPM_DIR_OUTPUT
,
1345 static const unsigned long nmk_pin_sleep_wakeup_modes
[] = {
1346 PIN_SLPM_WAKEUP_DISABLE
,
1347 PIN_SLPM_WAKEUP_ENABLE
,
1350 static const unsigned long nmk_pin_gpio_modes
[] = {
1351 PIN_GPIOMODE_DISABLED
,
1352 PIN_GPIOMODE_ENABLED
,
1355 static const unsigned long nmk_pin_sleep_pdis_modes
[] = {
1356 PIN_SLPM_PDIS_DISABLED
,
1357 PIN_SLPM_PDIS_ENABLED
,
1360 struct nmk_cfg_param
{
1361 const char *property
;
1362 unsigned long config
;
1363 const unsigned long *choice
;
1367 static const struct nmk_cfg_param nmk_cfg_params
[] = {
1368 NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes
),
1369 NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes
),
1370 NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes
),
1371 NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes
),
1372 NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes
),
1373 NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes
),
1374 NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes
),
1375 NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes
),
1378 static int nmk_dt_pin_config(int index
, int val
, unsigned long *config
)
1382 if (nmk_cfg_params
[index
].choice
== NULL
)
1383 *config
= nmk_cfg_params
[index
].config
;
1385 /* test if out of range */
1386 if (val
< nmk_cfg_params
[index
].size
) {
1387 *config
= nmk_cfg_params
[index
].config
|
1388 nmk_cfg_params
[index
].choice
[val
];
1394 static const char *nmk_find_pin_name(struct pinctrl_dev
*pctldev
, const char *pin_name
)
1397 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1399 if (sscanf((char *)pin_name
, "GPIO%d", &pin_number
) == 1)
1400 for (i
= 0; i
< npct
->soc
->npins
; i
++)
1401 if (npct
->soc
->pins
[i
].number
== pin_number
)
1402 return npct
->soc
->pins
[i
].name
;
1406 static bool nmk_pinctrl_dt_get_config(struct device_node
*np
,
1407 unsigned long *configs
)
1409 bool has_config
= 0;
1410 unsigned long cfg
= 0;
1413 for (i
= 0; i
< ARRAY_SIZE(nmk_cfg_params
); i
++) {
1414 ret
= of_property_read_u32(np
,
1415 nmk_cfg_params
[i
].property
, &val
);
1416 if (ret
!= -EINVAL
) {
1417 if (nmk_dt_pin_config(i
, val
, &cfg
) == 0) {
1427 static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
1428 struct device_node
*np
,
1429 struct pinctrl_map
**map
,
1430 unsigned *reserved_maps
,
1434 const char *function
= NULL
;
1435 unsigned long configs
= 0;
1436 bool has_config
= 0;
1437 struct property
*prop
;
1438 struct device_node
*np_config
;
1440 ret
= of_property_read_string(np
, "function", &function
);
1444 ret
= of_property_count_strings(np
, "groups");
1448 ret
= pinctrl_utils_reserve_map(pctldev
, map
,
1454 of_property_for_each_string(np
, "groups", prop
, group
) {
1455 ret
= nmk_dt_add_map_mux(map
, reserved_maps
, num_maps
,
1462 has_config
= nmk_pinctrl_dt_get_config(np
, &configs
);
1463 np_config
= of_parse_phandle(np
, "ste,config", 0);
1465 has_config
|= nmk_pinctrl_dt_get_config(np_config
, &configs
);
1467 const char *gpio_name
;
1470 ret
= of_property_count_strings(np
, "pins");
1473 ret
= pinctrl_utils_reserve_map(pctldev
, map
,
1479 of_property_for_each_string(np
, "pins", prop
, pin
) {
1480 gpio_name
= nmk_find_pin_name(pctldev
, pin
);
1482 ret
= nmk_dt_add_map_configs(map
, reserved_maps
,
1484 gpio_name
, &configs
, 1);
1494 static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
1495 struct device_node
*np_config
,
1496 struct pinctrl_map
**map
, unsigned *num_maps
)
1498 unsigned reserved_maps
;
1499 struct device_node
*np
;
1506 for_each_child_of_node(np_config
, np
) {
1507 ret
= nmk_pinctrl_dt_subnode_to_map(pctldev
, np
, map
,
1508 &reserved_maps
, num_maps
);
1510 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
1518 static const struct pinctrl_ops nmk_pinctrl_ops
= {
1519 .get_groups_count
= nmk_get_groups_cnt
,
1520 .get_group_name
= nmk_get_group_name
,
1521 .get_group_pins
= nmk_get_group_pins
,
1522 .pin_dbg_show
= nmk_pin_dbg_show
,
1523 .dt_node_to_map
= nmk_pinctrl_dt_node_to_map
,
1524 .dt_free_map
= pinctrl_utils_free_map
,
1527 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
1529 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1531 return npct
->soc
->nfunctions
;
1534 static const char *nmk_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
1537 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1539 return npct
->soc
->functions
[function
].name
;
1542 static int nmk_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
1544 const char * const **groups
,
1545 unsigned * const num_groups
)
1547 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1549 *groups
= npct
->soc
->functions
[function
].groups
;
1550 *num_groups
= npct
->soc
->functions
[function
].ngroups
;
1555 static int nmk_pmx_set(struct pinctrl_dev
*pctldev
, unsigned function
,
1558 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1559 const struct nmk_pingroup
*g
;
1560 static unsigned int slpm
[NUM_BANKS
];
1561 unsigned long flags
= 0;
1566 g
= &npct
->soc
->groups
[group
];
1568 if (g
->altsetting
< 0)
1571 dev_dbg(npct
->dev
, "enable group %s, %u pins\n", g
->name
, g
->npins
);
1574 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1575 * we may pass through an undesired state. In this case we take
1578 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1579 * - Save SLPM registers (since we have a shadow register in the
1580 * nmk_chip we're using that as backup)
1581 * - Set SLPM=0 for the IOs you want to switch and others to 1
1582 * - Configure the GPIO registers for the IOs that are being switched
1584 * - Modify the AFLSA/B registers for the IOs that are being switched
1586 * - Restore SLPM registers
1587 * - Any spurious wake up event during switch sequence to be ignored
1590 * We REALLY need to save ALL slpm registers, because the external
1591 * IOFORCE will switch *all* ports to their sleepmode setting to as
1592 * to avoid glitches. (Not just one port!)
1594 glitch
= ((g
->altsetting
& NMK_GPIO_ALT_C
) == NMK_GPIO_ALT_C
);
1597 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
1599 /* Initially don't put any pins to sleep when switching */
1600 memset(slpm
, 0xff, sizeof(slpm
));
1603 * Then mask the pins that need to be sleeping now when we're
1604 * switching to the ALT C function.
1606 for (i
= 0; i
< g
->npins
; i
++)
1607 slpm
[g
->pins
[i
] / NMK_GPIO_PER_CHIP
] &= ~BIT(g
->pins
[i
]);
1608 nmk_gpio_glitch_slpm_init(slpm
);
1611 for (i
= 0; i
< g
->npins
; i
++) {
1612 struct nmk_gpio_chip
*nmk_chip
;
1615 nmk_chip
= find_nmk_gpio_from_pin(g
->pins
[i
]);
1618 "invalid pin offset %d in group %s at index %d\n",
1619 g
->pins
[i
], g
->name
, i
);
1622 dev_dbg(npct
->dev
, "setting pin %d to altsetting %d\n", g
->pins
[i
], g
->altsetting
);
1624 clk_enable(nmk_chip
->clk
);
1625 bit
= g
->pins
[i
] % NMK_GPIO_PER_CHIP
;
1627 * If the pin is switching to altfunc, and there was an
1628 * interrupt installed on it which has been lazy disabled,
1629 * actually mask the interrupt to prevent spurious interrupts
1630 * that would occur while the pin is under control of the
1631 * peripheral. Only SKE does this.
1633 nmk_gpio_disable_lazy_irq(nmk_chip
, bit
);
1635 __nmk_gpio_set_mode_safe(nmk_chip
, bit
,
1636 (g
->altsetting
& NMK_GPIO_ALT_C
), glitch
);
1637 clk_disable(nmk_chip
->clk
);
1640 * Call PRCM GPIOCR config function in case ALTC
1641 * has been selected:
1642 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1644 * - If selection is pure ALTC and previous selection was ALTCx,
1645 * then some bits in PRCM GPIOCR registers must be cleared.
1647 if ((g
->altsetting
& NMK_GPIO_ALT_C
) == NMK_GPIO_ALT_C
)
1648 nmk_prcm_altcx_set_mode(npct
, g
->pins
[i
],
1649 g
->altsetting
>> NMK_GPIO_ALT_CX_SHIFT
);
1652 /* When all pins are successfully reconfigured we get here */
1657 nmk_gpio_glitch_slpm_restore(slpm
);
1658 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
1664 static int nmk_gpio_request_enable(struct pinctrl_dev
*pctldev
,
1665 struct pinctrl_gpio_range
*range
,
1668 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1669 struct nmk_gpio_chip
*nmk_chip
;
1670 struct gpio_chip
*chip
;
1674 dev_err(npct
->dev
, "invalid range\n");
1678 dev_err(npct
->dev
, "missing GPIO chip in range\n");
1682 nmk_chip
= gpiochip_get_data(chip
);
1684 dev_dbg(npct
->dev
, "enable pin %u as GPIO\n", offset
);
1686 clk_enable(nmk_chip
->clk
);
1687 bit
= offset
% NMK_GPIO_PER_CHIP
;
1688 /* There is no glitch when converting any pin to GPIO */
1689 __nmk_gpio_set_mode(nmk_chip
, bit
, NMK_GPIO_ALT_GPIO
);
1690 clk_disable(nmk_chip
->clk
);
1695 static void nmk_gpio_disable_free(struct pinctrl_dev
*pctldev
,
1696 struct pinctrl_gpio_range
*range
,
1699 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1701 dev_dbg(npct
->dev
, "disable pin %u as GPIO\n", offset
);
1702 /* Set the pin to some default state, GPIO is usually default */
1705 static const struct pinmux_ops nmk_pinmux_ops
= {
1706 .get_functions_count
= nmk_pmx_get_funcs_cnt
,
1707 .get_function_name
= nmk_pmx_get_func_name
,
1708 .get_function_groups
= nmk_pmx_get_func_groups
,
1709 .set_mux
= nmk_pmx_set
,
1710 .gpio_request_enable
= nmk_gpio_request_enable
,
1711 .gpio_disable_free
= nmk_gpio_disable_free
,
1715 static int nmk_pin_config_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
1716 unsigned long *config
)
1718 /* Not implemented */
1722 static int nmk_pin_config_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
1723 unsigned long *configs
, unsigned num_configs
)
1725 static const char *pullnames
[] = {
1726 [NMK_GPIO_PULL_NONE
] = "none",
1727 [NMK_GPIO_PULL_UP
] = "up",
1728 [NMK_GPIO_PULL_DOWN
] = "down",
1729 [3] /* illegal */ = "??"
1731 static const char *slpmnames
[] = {
1732 [NMK_GPIO_SLPM_INPUT
] = "input/wakeup",
1733 [NMK_GPIO_SLPM_NOCHANGE
] = "no-change/no-wakeup",
1735 struct nmk_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
1736 struct nmk_gpio_chip
*nmk_chip
;
1739 int pull
, slpm
, output
, val
, i
;
1740 bool lowemi
, gpiomode
, sleep
;
1742 nmk_chip
= find_nmk_gpio_from_pin(pin
);
1745 "invalid pin offset %d\n", pin
);
1749 for (i
= 0; i
< num_configs
; i
++) {
1751 * The pin config contains pin number and altfunction fields,
1752 * here we just ignore that part. It's being handled by the
1753 * framework and pinmux callback respectively.
1755 cfg
= (pin_cfg_t
) configs
[i
];
1756 pull
= PIN_PULL(cfg
);
1757 slpm
= PIN_SLPM(cfg
);
1758 output
= PIN_DIR(cfg
);
1760 lowemi
= PIN_LOWEMI(cfg
);
1761 gpiomode
= PIN_GPIOMODE(cfg
);
1762 sleep
= PIN_SLEEPMODE(cfg
);
1765 int slpm_pull
= PIN_SLPM_PULL(cfg
);
1766 int slpm_output
= PIN_SLPM_DIR(cfg
);
1767 int slpm_val
= PIN_SLPM_VAL(cfg
);
1769 /* All pins go into GPIO mode at sleep */
1773 * The SLPM_* values are normal values + 1 to allow zero
1774 * to mean "same as normal".
1777 pull
= slpm_pull
- 1;
1779 output
= slpm_output
- 1;
1783 dev_dbg(nmk_chip
->chip
.parent
,
1784 "pin %d: sleep pull %s, dir %s, val %s\n",
1786 slpm_pull
? pullnames
[pull
] : "same",
1787 slpm_output
? (output
? "output" : "input")
1789 slpm_val
? (val
? "high" : "low") : "same");
1792 dev_dbg(nmk_chip
->chip
.parent
,
1793 "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1794 pin
, cfg
, pullnames
[pull
], slpmnames
[slpm
],
1795 output
? "output " : "input",
1796 output
? (val
? "high" : "low") : "",
1797 lowemi
? "on" : "off");
1799 clk_enable(nmk_chip
->clk
);
1800 bit
= pin
% NMK_GPIO_PER_CHIP
;
1802 /* No glitch when going to GPIO mode */
1803 __nmk_gpio_set_mode(nmk_chip
, bit
, NMK_GPIO_ALT_GPIO
);
1805 __nmk_gpio_make_output(nmk_chip
, bit
, val
);
1807 __nmk_gpio_make_input(nmk_chip
, bit
);
1808 __nmk_gpio_set_pull(nmk_chip
, bit
, pull
);
1810 /* TODO: isn't this only applicable on output pins? */
1811 __nmk_gpio_set_lowemi(nmk_chip
, bit
, lowemi
);
1813 __nmk_gpio_set_slpm(nmk_chip
, bit
, slpm
);
1814 clk_disable(nmk_chip
->clk
);
1815 } /* for each config */
1820 static const struct pinconf_ops nmk_pinconf_ops
= {
1821 .pin_config_get
= nmk_pin_config_get
,
1822 .pin_config_set
= nmk_pin_config_set
,
1825 static struct pinctrl_desc nmk_pinctrl_desc
= {
1826 .name
= "pinctrl-nomadik",
1827 .pctlops
= &nmk_pinctrl_ops
,
1828 .pmxops
= &nmk_pinmux_ops
,
1829 .confops
= &nmk_pinconf_ops
,
1830 .owner
= THIS_MODULE
,
1833 static const struct of_device_id nmk_pinctrl_match
[] = {
1835 .compatible
= "stericsson,stn8815-pinctrl",
1836 .data
= (void *)PINCTRL_NMK_STN8815
,
1839 .compatible
= "stericsson,db8500-pinctrl",
1840 .data
= (void *)PINCTRL_NMK_DB8500
,
1843 .compatible
= "stericsson,db8540-pinctrl",
1844 .data
= (void *)PINCTRL_NMK_DB8540
,
1849 #ifdef CONFIG_PM_SLEEP
1850 static int nmk_pinctrl_suspend(struct device
*dev
)
1852 struct nmk_pinctrl
*npct
;
1854 npct
= dev_get_drvdata(dev
);
1858 return pinctrl_force_sleep(npct
->pctl
);
1861 static int nmk_pinctrl_resume(struct device
*dev
)
1863 struct nmk_pinctrl
*npct
;
1865 npct
= dev_get_drvdata(dev
);
1869 return pinctrl_force_default(npct
->pctl
);
1873 static int nmk_pinctrl_probe(struct platform_device
*pdev
)
1875 const struct of_device_id
*match
;
1876 struct device_node
*np
= pdev
->dev
.of_node
;
1877 struct device_node
*prcm_np
;
1878 struct nmk_pinctrl
*npct
;
1879 unsigned int version
= 0;
1882 npct
= devm_kzalloc(&pdev
->dev
, sizeof(*npct
), GFP_KERNEL
);
1886 match
= of_match_device(nmk_pinctrl_match
, &pdev
->dev
);
1889 version
= (unsigned int) match
->data
;
1891 /* Poke in other ASIC variants here */
1892 if (version
== PINCTRL_NMK_STN8815
)
1893 nmk_pinctrl_stn8815_init(&npct
->soc
);
1894 if (version
== PINCTRL_NMK_DB8500
)
1895 nmk_pinctrl_db8500_init(&npct
->soc
);
1896 if (version
== PINCTRL_NMK_DB8540
)
1897 nmk_pinctrl_db8540_init(&npct
->soc
);
1900 * Since we depend on the GPIO chips to provide clock and register base
1901 * for the pin control operations, make sure that we have these
1902 * populated before we continue. Follow the phandles to instantiate
1903 * them. The GPIO portion of the actual hardware may be probed before
1904 * or after this point: it shouldn't matter as the APIs are orthogonal.
1906 for (i
= 0; i
< NMK_MAX_BANKS
; i
++) {
1907 struct device_node
*gpio_np
;
1908 struct nmk_gpio_chip
*nmk_chip
;
1910 gpio_np
= of_parse_phandle(np
, "nomadik-gpio-chips", i
);
1912 dev_info(&pdev
->dev
,
1913 "populate NMK GPIO %d \"%pOFn\"\n",
1915 nmk_chip
= nmk_gpio_populate_chip(gpio_np
, pdev
);
1916 if (IS_ERR(nmk_chip
))
1918 "could not populate nmk chip struct "
1919 "- continue anyway\n");
1920 of_node_put(gpio_np
);
1924 prcm_np
= of_parse_phandle(np
, "prcm", 0);
1926 npct
->prcm_base
= of_iomap(prcm_np
, 0);
1927 if (!npct
->prcm_base
) {
1928 if (version
== PINCTRL_NMK_STN8815
) {
1929 dev_info(&pdev
->dev
,
1931 "assuming no ALT-Cx control is available\n");
1933 dev_err(&pdev
->dev
, "missing PRCM base address\n");
1938 nmk_pinctrl_desc
.pins
= npct
->soc
->pins
;
1939 nmk_pinctrl_desc
.npins
= npct
->soc
->npins
;
1940 npct
->dev
= &pdev
->dev
;
1942 npct
->pctl
= devm_pinctrl_register(&pdev
->dev
, &nmk_pinctrl_desc
, npct
);
1943 if (IS_ERR(npct
->pctl
)) {
1944 dev_err(&pdev
->dev
, "could not register Nomadik pinctrl driver\n");
1945 return PTR_ERR(npct
->pctl
);
1948 platform_set_drvdata(pdev
, npct
);
1949 dev_info(&pdev
->dev
, "initialized Nomadik pin control driver\n");
1954 static const struct of_device_id nmk_gpio_match
[] = {
1955 { .compatible
= "st,nomadik-gpio", },
1959 static struct platform_driver nmk_gpio_driver
= {
1962 .of_match_table
= nmk_gpio_match
,
1964 .probe
= nmk_gpio_probe
,
1967 static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops
,
1968 nmk_pinctrl_suspend
,
1969 nmk_pinctrl_resume
);
1971 static struct platform_driver nmk_pinctrl_driver
= {
1973 .name
= "pinctrl-nomadik",
1974 .of_match_table
= nmk_pinctrl_match
,
1975 .pm
= &nmk_pinctrl_pm_ops
,
1977 .probe
= nmk_pinctrl_probe
,
1980 static int __init
nmk_gpio_init(void)
1982 return platform_driver_register(&nmk_gpio_driver
);
1984 subsys_initcall(nmk_gpio_init
);
1986 static int __init
nmk_pinctrl_init(void)
1988 return platform_driver_register(&nmk_pinctrl_driver
);
1990 core_initcall(nmk_pinctrl_init
);