1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic GPIO driver for logic cells found in the Nomadik SoC
4  *
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>
9  */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/io.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"
31 #include "../core.h"
32 #include "../pinctrl-utils.h"
33 
34 /*
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.
38  *
39  * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
40  */
41 
42 /*
43  * pin configurations are represented by 32-bit integers:
44  *
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
49  *	bit     14 - Direction
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)
55  *	bit	26 - Gpio mode
56  *	bit	27 - Sleep mode
57  *
58  * to facilitate the definition, the following macros are provided
59  *
60  * PIN_CFG_DEFAULT - default config (0):
61  *		     pull up/down = disabled
62  *		     sleep mode = input/wakeup
63  *		     direction = input
64  *		     value = low
65  *		     SLPM direction = same as normal
66  *		     SLPM pull = same as normal
67  *		     SLPM value = same as normal
68  *
69  * PIN_CFG	   - default config with alternate function
70  */
71 
72 typedef unsigned long pin_cfg_t;
73 
74 #define PIN_NUM_MASK		0x1ff
75 #define PIN_NUM(x)		((x) & PIN_NUM_MASK)
76 
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)
84 
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)
91 
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
101 
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 */
104 
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)
110 
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)
116 
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)
127 
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)
134 
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)
141 
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)
149 
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)
155 
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)
161 
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)
167 
168 
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)
175 
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)
181 
182 #define PIN_CFG_DEFAULT		(0)
183 
184 #define PIN_CFG(num, alt)		\
185 	(PIN_CFG_DEFAULT |\
186 	 (PIN_NUM(num) | PIN_##alt))
187 
188 #define PIN_CFG_INPUT(num, alt, pull)		\
189 	(PIN_CFG_DEFAULT |\
190 	 (PIN_NUM(num) | PIN_##alt | PIN_INPUT_##pull))
191 
192 #define PIN_CFG_OUTPUT(num, alt, val)		\
193 	(PIN_CFG_DEFAULT |\
194 	 (PIN_NUM(num) | PIN_##alt | PIN_OUTPUT_##val))
195 
196 /*
197  * "nmk_gpio" and "NMK_GPIO" stand for "Nomadik GPIO", leaving
198  * the "gpio" namespace for generic and cross-machine functions
199  */
200 
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)
204 
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
217 
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
228 
229 
230 /* Pull up/down values */
231 enum nmk_gpio_pull {
232 	NMK_GPIO_PULL_NONE,
233 	NMK_GPIO_PULL_UP,
234 	NMK_GPIO_PULL_DOWN,
235 };
236 
237 /* Sleep mode */
238 enum nmk_gpio_slpm {
239 	NMK_GPIO_SLPM_INPUT,
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,
243 };
244 
245 struct nmk_gpio_chip {
246 	struct gpio_chip chip;
247 	struct irq_chip irqchip;
248 	void __iomem *addr;
249 	struct clk *clk;
250 	unsigned int bank;
251 	void (*set_ioforce)(bool enable);
252 	spinlock_t lock;
253 	bool sleepmode;
254 	/* Keep track of configured edges */
255 	u32 edge_rising;
256 	u32 edge_falling;
257 	u32 real_wake;
258 	u32 rwimsc;
259 	u32 fwimsc;
260 	u32 rimsc;
261 	u32 fimsc;
262 	u32 pull_up;
263 	u32 lowemi;
264 };
265 
266 /**
267  * struct nmk_pinctrl - state container for the Nomadik pin controller
268  * @dev: containing device pointer
269  * @pctl: corresponding pin controller device
270  * @soc: SoC data for this specific chip
271  * @prcm_base: PRCM register range virtual base
272  */
273 struct nmk_pinctrl {
274 	struct device *dev;
275 	struct pinctrl_dev *pctl;
276 	const struct nmk_pinctrl_soc_data *soc;
277 	void __iomem *prcm_base;
278 };
279 
280 static struct nmk_gpio_chip *nmk_gpio_chips[NMK_MAX_BANKS];
281 
282 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
283 
284 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
285 
286 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
287 				unsigned offset, int gpio_mode)
288 {
289 	u32 afunc, bfunc;
290 
291 	afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~BIT(offset);
292 	bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~BIT(offset);
293 	if (gpio_mode & NMK_GPIO_ALT_A)
294 		afunc |= BIT(offset);
295 	if (gpio_mode & NMK_GPIO_ALT_B)
296 		bfunc |= BIT(offset);
297 	writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
298 	writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
299 }
300 
301 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
302 				unsigned offset, enum nmk_gpio_slpm mode)
303 {
304 	u32 slpm;
305 
306 	slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
307 	if (mode == NMK_GPIO_SLPM_NOCHANGE)
308 		slpm |= BIT(offset);
309 	else
310 		slpm &= ~BIT(offset);
311 	writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
312 }
313 
314 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
315 				unsigned offset, enum nmk_gpio_pull pull)
316 {
317 	u32 pdis;
318 
319 	pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
320 	if (pull == NMK_GPIO_PULL_NONE) {
321 		pdis |= BIT(offset);
322 		nmk_chip->pull_up &= ~BIT(offset);
323 	} else {
324 		pdis &= ~BIT(offset);
325 	}
326 
327 	writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
328 
329 	if (pull == NMK_GPIO_PULL_UP) {
330 		nmk_chip->pull_up |= BIT(offset);
331 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATS);
332 	} else if (pull == NMK_GPIO_PULL_DOWN) {
333 		nmk_chip->pull_up &= ~BIT(offset);
334 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATC);
335 	}
336 }
337 
338 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
339 				  unsigned offset, bool lowemi)
340 {
341 	bool enabled = nmk_chip->lowemi & BIT(offset);
342 
343 	if (lowemi == enabled)
344 		return;
345 
346 	if (lowemi)
347 		nmk_chip->lowemi |= BIT(offset);
348 	else
349 		nmk_chip->lowemi &= ~BIT(offset);
350 
351 	writel_relaxed(nmk_chip->lowemi,
352 		       nmk_chip->addr + NMK_GPIO_LOWEMI);
353 }
354 
355 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
356 				  unsigned offset)
357 {
358 	writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRC);
359 }
360 
361 static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
362 				  unsigned offset, int val)
363 {
364 	if (val)
365 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATS);
366 	else
367 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATC);
368 }
369 
370 static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
371 				  unsigned offset, int val)
372 {
373 	writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRS);
374 	__nmk_gpio_set_output(nmk_chip, offset, val);
375 }
376 
377 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
378 				     unsigned offset, int gpio_mode,
379 				     bool glitch)
380 {
381 	u32 rwimsc = nmk_chip->rwimsc;
382 	u32 fwimsc = nmk_chip->fwimsc;
383 
384 	if (glitch && nmk_chip->set_ioforce) {
385 		u32 bit = BIT(offset);
386 
387 		/* Prevent spurious wakeups */
388 		writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
389 		writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
390 
391 		nmk_chip->set_ioforce(true);
392 	}
393 
394 	__nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
395 
396 	if (glitch && nmk_chip->set_ioforce) {
397 		nmk_chip->set_ioforce(false);
398 
399 		writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
400 		writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
401 	}
402 }
403 
404 static void
405 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
406 {
407 	u32 falling = nmk_chip->fimsc & BIT(offset);
408 	u32 rising = nmk_chip->rimsc & BIT(offset);
409 	int gpio = nmk_chip->chip.base + offset;
410 	int irq = irq_find_mapping(nmk_chip->chip.irq.domain, offset);
411 	struct irq_data *d = irq_get_irq_data(irq);
412 
413 	if (!rising && !falling)
414 		return;
415 
416 	if (!d || !irqd_irq_disabled(d))
417 		return;
418 
419 	if (rising) {
420 		nmk_chip->rimsc &= ~BIT(offset);
421 		writel_relaxed(nmk_chip->rimsc,
422 			       nmk_chip->addr + NMK_GPIO_RIMSC);
423 	}
424 
425 	if (falling) {
426 		nmk_chip->fimsc &= ~BIT(offset);
427 		writel_relaxed(nmk_chip->fimsc,
428 			       nmk_chip->addr + NMK_GPIO_FIMSC);
429 	}
430 
431 	dev_dbg(nmk_chip->chip.parent, "%d: clearing interrupt mask\n", gpio);
432 }
433 
434 static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
435 {
436 	u32 val;
437 
438 	val = readl(reg);
439 	val = ((val & ~mask) | (value & mask));
440 	writel(val, reg);
441 }
442 
443 static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
444 	unsigned offset, unsigned alt_num)
445 {
446 	int i;
447 	u16 reg;
448 	u8 bit;
449 	u8 alt_index;
450 	const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
451 	const u16 *gpiocr_regs;
452 
453 	if (!npct->prcm_base)
454 		return;
455 
456 	if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
457 		dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
458 			alt_num);
459 		return;
460 	}
461 
462 	for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
463 		if (npct->soc->altcx_pins[i].pin == offset)
464 			break;
465 	}
466 	if (i == npct->soc->npins_altcx) {
467 		dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
468 			offset);
469 		return;
470 	}
471 
472 	pin_desc = npct->soc->altcx_pins + i;
473 	gpiocr_regs = npct->soc->prcm_gpiocr_registers;
474 
475 	/*
476 	 * If alt_num is NULL, just clear current ALTCx selection
477 	 * to make sure we come back to a pure ALTC selection
478 	 */
479 	if (!alt_num) {
480 		for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
481 			if (pin_desc->altcx[i].used == true) {
482 				reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
483 				bit = pin_desc->altcx[i].control_bit;
484 				if (readl(npct->prcm_base + reg) & BIT(bit)) {
485 					nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
486 					dev_dbg(npct->dev,
487 						"PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
488 						offset, i+1);
489 				}
490 			}
491 		}
492 		return;
493 	}
494 
495 	alt_index = alt_num - 1;
496 	if (pin_desc->altcx[alt_index].used == false) {
497 		dev_warn(npct->dev,
498 			"PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
499 			offset, alt_num);
500 		return;
501 	}
502 
503 	/*
504 	 * Check if any other ALTCx functions are activated on this pin
505 	 * and disable it first.
506 	 */
507 	for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
508 		if (i == alt_index)
509 			continue;
510 		if (pin_desc->altcx[i].used == true) {
511 			reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
512 			bit = pin_desc->altcx[i].control_bit;
513 			if (readl(npct->prcm_base + reg) & BIT(bit)) {
514 				nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
515 				dev_dbg(npct->dev,
516 					"PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
517 					offset, i+1);
518 			}
519 		}
520 	}
521 
522 	reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
523 	bit = pin_desc->altcx[alt_index].control_bit;
524 	dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
525 		offset, alt_index+1);
526 	nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
527 }
528 
529 /*
530  * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
531  *  - Save SLPM registers
532  *  - Set SLPM=0 for the IOs you want to switch and others to 1
533  *  - Configure the GPIO registers for the IOs that are being switched
534  *  - Set IOFORCE=1
535  *  - Modify the AFLSA/B registers for the IOs that are being switched
536  *  - Set IOFORCE=0
537  *  - Restore SLPM registers
538  *  - Any spurious wake up event during switch sequence to be ignored and
539  *    cleared
540  */
541 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
542 {
543 	int i;
544 
545 	for (i = 0; i < NUM_BANKS; i++) {
546 		struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
547 		unsigned int temp = slpm[i];
548 
549 		if (!chip)
550 			break;
551 
552 		clk_enable(chip->clk);
553 
554 		slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
555 		writel(temp, chip->addr + NMK_GPIO_SLPC);
556 	}
557 }
558 
559 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
560 {
561 	int i;
562 
563 	for (i = 0; i < NUM_BANKS; i++) {
564 		struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
565 
566 		if (!chip)
567 			break;
568 
569 		writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
570 
571 		clk_disable(chip->clk);
572 	}
573 }
574 
575 static int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
576 {
577 	int i;
578 	u16 reg;
579 	u8 bit;
580 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
581 	const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
582 	const u16 *gpiocr_regs;
583 
584 	if (!npct->prcm_base)
585 		return NMK_GPIO_ALT_C;
586 
587 	for (i = 0; i < npct->soc->npins_altcx; i++) {
588 		if (npct->soc->altcx_pins[i].pin == gpio)
589 			break;
590 	}
591 	if (i == npct->soc->npins_altcx)
592 		return NMK_GPIO_ALT_C;
593 
594 	pin_desc = npct->soc->altcx_pins + i;
595 	gpiocr_regs = npct->soc->prcm_gpiocr_registers;
596 	for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
597 		if (pin_desc->altcx[i].used == true) {
598 			reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
599 			bit = pin_desc->altcx[i].control_bit;
600 			if (readl(npct->prcm_base + reg) & BIT(bit))
601 				return NMK_GPIO_ALT_C+i+1;
602 		}
603 	}
604 	return NMK_GPIO_ALT_C;
605 }
606 
607 /* IRQ functions */
608 
609 static void nmk_gpio_irq_ack(struct irq_data *d)
610 {
611 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
612 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
613 
614 	clk_enable(nmk_chip->clk);
615 	writel(BIT(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
616 	clk_disable(nmk_chip->clk);
617 }
618 
619 enum nmk_gpio_irq_type {
620 	NORMAL,
621 	WAKE,
622 };
623 
624 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
625 				  int offset, enum nmk_gpio_irq_type which,
626 				  bool enable)
627 {
628 	u32 *rimscval;
629 	u32 *fimscval;
630 	u32 rimscreg;
631 	u32 fimscreg;
632 
633 	if (which == NORMAL) {
634 		rimscreg = NMK_GPIO_RIMSC;
635 		fimscreg = NMK_GPIO_FIMSC;
636 		rimscval = &nmk_chip->rimsc;
637 		fimscval = &nmk_chip->fimsc;
638 	} else  {
639 		rimscreg = NMK_GPIO_RWIMSC;
640 		fimscreg = NMK_GPIO_FWIMSC;
641 		rimscval = &nmk_chip->rwimsc;
642 		fimscval = &nmk_chip->fwimsc;
643 	}
644 
645 	/* we must individually set/clear the two edges */
646 	if (nmk_chip->edge_rising & BIT(offset)) {
647 		if (enable)
648 			*rimscval |= BIT(offset);
649 		else
650 			*rimscval &= ~BIT(offset);
651 		writel(*rimscval, nmk_chip->addr + rimscreg);
652 	}
653 	if (nmk_chip->edge_falling & BIT(offset)) {
654 		if (enable)
655 			*fimscval |= BIT(offset);
656 		else
657 			*fimscval &= ~BIT(offset);
658 		writel(*fimscval, nmk_chip->addr + fimscreg);
659 	}
660 }
661 
662 static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
663 				int offset, bool on)
664 {
665 	/*
666 	 * Ensure WAKEUP_ENABLE is on.  No need to disable it if wakeup is
667 	 * disabled, since setting SLPM to 1 increases power consumption, and
668 	 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
669 	 */
670 	if (nmk_chip->sleepmode && on) {
671 		__nmk_gpio_set_slpm(nmk_chip, offset,
672 				    NMK_GPIO_SLPM_WAKEUP_ENABLE);
673 	}
674 
675 	__nmk_gpio_irq_modify(nmk_chip, offset, WAKE, on);
676 }
677 
678 static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
679 {
680 	struct nmk_gpio_chip *nmk_chip;
681 	unsigned long flags;
682 
683 	nmk_chip = irq_data_get_irq_chip_data(d);
684 	if (!nmk_chip)
685 		return -EINVAL;
686 
687 	clk_enable(nmk_chip->clk);
688 	spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
689 	spin_lock(&nmk_chip->lock);
690 
691 	__nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
692 
693 	if (!(nmk_chip->real_wake & BIT(d->hwirq)))
694 		__nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
695 
696 	spin_unlock(&nmk_chip->lock);
697 	spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
698 	clk_disable(nmk_chip->clk);
699 
700 	return 0;
701 }
702 
703 static void nmk_gpio_irq_mask(struct irq_data *d)
704 {
705 	nmk_gpio_irq_maskunmask(d, false);
706 }
707 
708 static void nmk_gpio_irq_unmask(struct irq_data *d)
709 {
710 	nmk_gpio_irq_maskunmask(d, true);
711 }
712 
713 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
714 {
715 	struct nmk_gpio_chip *nmk_chip;
716 	unsigned long flags;
717 
718 	nmk_chip = irq_data_get_irq_chip_data(d);
719 	if (!nmk_chip)
720 		return -EINVAL;
721 
722 	clk_enable(nmk_chip->clk);
723 	spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
724 	spin_lock(&nmk_chip->lock);
725 
726 	if (irqd_irq_disabled(d))
727 		__nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
728 
729 	if (on)
730 		nmk_chip->real_wake |= BIT(d->hwirq);
731 	else
732 		nmk_chip->real_wake &= ~BIT(d->hwirq);
733 
734 	spin_unlock(&nmk_chip->lock);
735 	spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
736 	clk_disable(nmk_chip->clk);
737 
738 	return 0;
739 }
740 
741 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
742 {
743 	bool enabled = !irqd_irq_disabled(d);
744 	bool wake = irqd_is_wakeup_set(d);
745 	struct nmk_gpio_chip *nmk_chip;
746 	unsigned long flags;
747 
748 	nmk_chip = irq_data_get_irq_chip_data(d);
749 	if (!nmk_chip)
750 		return -EINVAL;
751 	if (type & IRQ_TYPE_LEVEL_HIGH)
752 		return -EINVAL;
753 	if (type & IRQ_TYPE_LEVEL_LOW)
754 		return -EINVAL;
755 
756 	clk_enable(nmk_chip->clk);
757 	spin_lock_irqsave(&nmk_chip->lock, flags);
758 
759 	if (enabled)
760 		__nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
761 
762 	if (enabled || wake)
763 		__nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
764 
765 	nmk_chip->edge_rising &= ~BIT(d->hwirq);
766 	if (type & IRQ_TYPE_EDGE_RISING)
767 		nmk_chip->edge_rising |= BIT(d->hwirq);
768 
769 	nmk_chip->edge_falling &= ~BIT(d->hwirq);
770 	if (type & IRQ_TYPE_EDGE_FALLING)
771 		nmk_chip->edge_falling |= BIT(d->hwirq);
772 
773 	if (enabled)
774 		__nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
775 
776 	if (enabled || wake)
777 		__nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
778 
779 	spin_unlock_irqrestore(&nmk_chip->lock, flags);
780 	clk_disable(nmk_chip->clk);
781 
782 	return 0;
783 }
784 
785 static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
786 {
787 	struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
788 
789 	clk_enable(nmk_chip->clk);
790 	nmk_gpio_irq_unmask(d);
791 	return 0;
792 }
793 
794 static void nmk_gpio_irq_shutdown(struct irq_data *d)
795 {
796 	struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
797 
798 	nmk_gpio_irq_mask(d);
799 	clk_disable(nmk_chip->clk);
800 }
801 
802 static void __nmk_gpio_irq_handler(struct irq_desc *desc, u32 status)
803 {
804 	struct irq_chip *host_chip = irq_desc_get_chip(desc);
805 	struct gpio_chip *chip = irq_desc_get_handler_data(desc);
806 
807 	chained_irq_enter(host_chip, desc);
808 
809 	while (status) {
810 		int bit = __ffs(status);
811 
812 		generic_handle_irq(irq_find_mapping(chip->irq.domain, bit));
813 		status &= ~BIT(bit);
814 	}
815 
816 	chained_irq_exit(host_chip, desc);
817 }
818 
819 static void nmk_gpio_irq_handler(struct irq_desc *desc)
820 {
821 	struct gpio_chip *chip = irq_desc_get_handler_data(desc);
822 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
823 	u32 status;
824 
825 	clk_enable(nmk_chip->clk);
826 	status = readl(nmk_chip->addr + NMK_GPIO_IS);
827 	clk_disable(nmk_chip->clk);
828 
829 	__nmk_gpio_irq_handler(desc, status);
830 }
831 
832 /* I/O Functions */
833 
834 static int nmk_gpio_get_dir(struct gpio_chip *chip, unsigned offset)
835 {
836 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
837 	int dir;
838 
839 	clk_enable(nmk_chip->clk);
840 
841 	dir = !(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset));
842 
843 	clk_disable(nmk_chip->clk);
844 
845 	return dir;
846 }
847 
848 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
849 {
850 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
851 
852 	clk_enable(nmk_chip->clk);
853 
854 	writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRC);
855 
856 	clk_disable(nmk_chip->clk);
857 
858 	return 0;
859 }
860 
861 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
862 {
863 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
864 	int value;
865 
866 	clk_enable(nmk_chip->clk);
867 
868 	value = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & BIT(offset));
869 
870 	clk_disable(nmk_chip->clk);
871 
872 	return value;
873 }
874 
875 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
876 				int val)
877 {
878 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
879 
880 	clk_enable(nmk_chip->clk);
881 
882 	__nmk_gpio_set_output(nmk_chip, offset, val);
883 
884 	clk_disable(nmk_chip->clk);
885 }
886 
887 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
888 				int val)
889 {
890 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
891 
892 	clk_enable(nmk_chip->clk);
893 
894 	__nmk_gpio_make_output(nmk_chip, offset, val);
895 
896 	clk_disable(nmk_chip->clk);
897 
898 	return 0;
899 }
900 
901 #ifdef CONFIG_DEBUG_FS
902 static int nmk_gpio_get_mode(struct nmk_gpio_chip *nmk_chip, int offset)
903 {
904 	u32 afunc, bfunc;
905 
906 	clk_enable(nmk_chip->clk);
907 
908 	afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & BIT(offset);
909 	bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & BIT(offset);
910 
911 	clk_disable(nmk_chip->clk);
912 
913 	return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
914 }
915 
916 #include <linux/seq_file.h>
917 
918 static void nmk_gpio_dbg_show_one(struct seq_file *s,
919 	struct pinctrl_dev *pctldev, struct gpio_chip *chip,
920 	unsigned offset, unsigned gpio)
921 {
922 	const char *label = gpiochip_is_requested(chip, offset);
923 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
924 	int mode;
925 	bool is_out;
926 	bool data_out;
927 	bool pull;
928 	const char *modes[] = {
929 		[NMK_GPIO_ALT_GPIO]	= "gpio",
930 		[NMK_GPIO_ALT_A]	= "altA",
931 		[NMK_GPIO_ALT_B]	= "altB",
932 		[NMK_GPIO_ALT_C]	= "altC",
933 		[NMK_GPIO_ALT_C+1]	= "altC1",
934 		[NMK_GPIO_ALT_C+2]	= "altC2",
935 		[NMK_GPIO_ALT_C+3]	= "altC3",
936 		[NMK_GPIO_ALT_C+4]	= "altC4",
937 	};
938 	const char *pulls[] = {
939 		"none     ",
940 		"pull down",
941 		"pull up  ",
942 	};
943 
944 	clk_enable(nmk_chip->clk);
945 	is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset));
946 	pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & BIT(offset));
947 	data_out = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & BIT(offset));
948 	mode = nmk_gpio_get_mode(nmk_chip, offset);
949 	if ((mode == NMK_GPIO_ALT_C) && pctldev)
950 		mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
951 
952 	if (is_out) {
953 		seq_printf(s, " gpio-%-3d (%-20.20s) out %s        %s",
954 			   gpio,
955 			   label ?: "(none)",
956 			   data_out ? "hi" : "lo",
957 			   (mode < 0) ? "unknown" : modes[mode]);
958 	} else {
959 		int irq = chip->to_irq(chip, offset);
960 		struct irq_desc	*desc = irq_to_desc(irq);
961 		int pullidx = 0;
962 		int val;
963 
964 		if (pull)
965 			pullidx = data_out ? 2 : 1;
966 
967 		seq_printf(s, " gpio-%-3d (%-20.20s) in  %s %s",
968 			   gpio,
969 			   label ?: "(none)",
970 			   pulls[pullidx],
971 			   (mode < 0) ? "unknown" : modes[mode]);
972 
973 		val = nmk_gpio_get_input(chip, offset);
974 		seq_printf(s, " VAL %d", val);
975 
976 		/*
977 		 * This races with request_irq(), set_irq_type(),
978 		 * and set_irq_wake() ... but those are "rare".
979 		 */
980 		if (irq > 0 && desc && desc->action) {
981 			char *trigger;
982 
983 			if (nmk_chip->edge_rising & BIT(offset))
984 				trigger = "edge-rising";
985 			else if (nmk_chip->edge_falling & BIT(offset))
986 				trigger = "edge-falling";
987 			else
988 				trigger = "edge-undefined";
989 
990 			seq_printf(s, " irq-%d %s%s",
991 				   irq, trigger,
992 				   irqd_is_wakeup_set(&desc->irq_data)
993 				   ? " wakeup" : "");
994 		}
995 	}
996 	clk_disable(nmk_chip->clk);
997 }
998 
999 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1000 {
1001 	unsigned		i;
1002 	unsigned		gpio = chip->base;
1003 
1004 	for (i = 0; i < chip->ngpio; i++, gpio++) {
1005 		nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
1006 		seq_printf(s, "\n");
1007 	}
1008 }
1009 
1010 #else
1011 static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1012 					 struct pinctrl_dev *pctldev,
1013 					 struct gpio_chip *chip,
1014 					 unsigned offset, unsigned gpio)
1015 {
1016 }
1017 #define nmk_gpio_dbg_show	NULL
1018 #endif
1019 
1020 /*
1021  * We will allocate memory for the state container using devm* allocators
1022  * binding to the first device reaching this point, it doesn't matter if
1023  * it is the pin controller or GPIO driver. However we need to use the right
1024  * platform device when looking up resources so pay attention to pdev.
1025  */
1026 static struct nmk_gpio_chip *nmk_gpio_populate_chip(struct device_node *np,
1027 						struct platform_device *pdev)
1028 {
1029 	struct nmk_gpio_chip *nmk_chip;
1030 	struct platform_device *gpio_pdev;
1031 	struct gpio_chip *chip;
1032 	struct resource *res;
1033 	struct clk *clk;
1034 	void __iomem *base;
1035 	u32 id;
1036 
1037 	gpio_pdev = of_find_device_by_node(np);
1038 	if (!gpio_pdev) {
1039 		pr_err("populate \"%pOFn\": device not found\n", np);
1040 		return ERR_PTR(-ENODEV);
1041 	}
1042 	if (of_property_read_u32(np, "gpio-bank", &id)) {
1043 		dev_err(&pdev->dev, "populate: gpio-bank property not found\n");
1044 		platform_device_put(gpio_pdev);
1045 		return ERR_PTR(-EINVAL);
1046 	}
1047 
1048 	/* Already populated? */
1049 	nmk_chip = nmk_gpio_chips[id];
1050 	if (nmk_chip) {
1051 		platform_device_put(gpio_pdev);
1052 		return nmk_chip;
1053 	}
1054 
1055 	nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL);
1056 	if (!nmk_chip) {
1057 		platform_device_put(gpio_pdev);
1058 		return ERR_PTR(-ENOMEM);
1059 	}
1060 
1061 	nmk_chip->bank = id;
1062 	chip = &nmk_chip->chip;
1063 	chip->base = id * NMK_GPIO_PER_CHIP;
1064 	chip->ngpio = NMK_GPIO_PER_CHIP;
1065 	chip->label = dev_name(&gpio_pdev->dev);
1066 	chip->parent = &gpio_pdev->dev;
1067 
1068 	res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
1069 	base = devm_ioremap_resource(&pdev->dev, res);
1070 	if (IS_ERR(base)) {
1071 		platform_device_put(gpio_pdev);
1072 		return ERR_CAST(base);
1073 	}
1074 	nmk_chip->addr = base;
1075 
1076 	clk = clk_get(&gpio_pdev->dev, NULL);
1077 	if (IS_ERR(clk)) {
1078 		platform_device_put(gpio_pdev);
1079 		return (void *) clk;
1080 	}
1081 	clk_prepare(clk);
1082 	nmk_chip->clk = clk;
1083 
1084 	BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1085 	nmk_gpio_chips[id] = nmk_chip;
1086 	return nmk_chip;
1087 }
1088 
1089 static int nmk_gpio_probe(struct platform_device *dev)
1090 {
1091 	struct device_node *np = dev->dev.of_node;
1092 	struct nmk_gpio_chip *nmk_chip;
1093 	struct gpio_chip *chip;
1094 	struct gpio_irq_chip *girq;
1095 	struct irq_chip *irqchip;
1096 	bool supports_sleepmode;
1097 	int irq;
1098 	int ret;
1099 
1100 	nmk_chip = nmk_gpio_populate_chip(np, dev);
1101 	if (IS_ERR(nmk_chip)) {
1102 		dev_err(&dev->dev, "could not populate nmk chip struct\n");
1103 		return PTR_ERR(nmk_chip);
1104 	}
1105 
1106 	supports_sleepmode =
1107 		of_property_read_bool(np, "st,supports-sleepmode");
1108 
1109 	/* Correct platform device ID */
1110 	dev->id = nmk_chip->bank;
1111 
1112 	irq = platform_get_irq(dev, 0);
1113 	if (irq < 0)
1114 		return irq;
1115 
1116 	/*
1117 	 * The virt address in nmk_chip->addr is in the nomadik register space,
1118 	 * so we can simply convert the resource address, without remapping
1119 	 */
1120 	nmk_chip->sleepmode = supports_sleepmode;
1121 	spin_lock_init(&nmk_chip->lock);
1122 
1123 	chip = &nmk_chip->chip;
1124 	chip->request = gpiochip_generic_request;
1125 	chip->free = gpiochip_generic_free;
1126 	chip->get_direction = nmk_gpio_get_dir;
1127 	chip->direction_input = nmk_gpio_make_input;
1128 	chip->get = nmk_gpio_get_input;
1129 	chip->direction_output = nmk_gpio_make_output;
1130 	chip->set = nmk_gpio_set_output;
1131 	chip->dbg_show = nmk_gpio_dbg_show;
1132 	chip->can_sleep = false;
1133 	chip->owner = THIS_MODULE;
1134 
1135 	irqchip = &nmk_chip->irqchip;
1136 	irqchip->irq_ack = nmk_gpio_irq_ack;
1137 	irqchip->irq_mask = nmk_gpio_irq_mask;
1138 	irqchip->irq_unmask = nmk_gpio_irq_unmask;
1139 	irqchip->irq_set_type = nmk_gpio_irq_set_type;
1140 	irqchip->irq_set_wake = nmk_gpio_irq_set_wake;
1141 	irqchip->irq_startup = nmk_gpio_irq_startup;
1142 	irqchip->irq_shutdown = nmk_gpio_irq_shutdown;
1143 	irqchip->flags = IRQCHIP_MASK_ON_SUSPEND;
1144 	irqchip->name = kasprintf(GFP_KERNEL, "nmk%u-%u-%u",
1145 				  dev->id,
1146 				  chip->base,
1147 				  chip->base + chip->ngpio - 1);
1148 
1149 	girq = &chip->irq;
1150 	girq->chip = irqchip;
1151 	girq->parent_handler = nmk_gpio_irq_handler;
1152 	girq->num_parents = 1;
1153 	girq->parents = devm_kcalloc(&dev->dev, 1,
1154 				     sizeof(*girq->parents),
1155 				     GFP_KERNEL);
1156 	if (!girq->parents)
1157 		return -ENOMEM;
1158 	girq->parents[0] = irq;
1159 	girq->default_type = IRQ_TYPE_NONE;
1160 	girq->handler = handle_edge_irq;
1161 
1162 	clk_enable(nmk_chip->clk);
1163 	nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1164 	clk_disable(nmk_chip->clk);
1165 	chip->of_node = np;
1166 
1167 	ret = gpiochip_add_data(chip, nmk_chip);
1168 	if (ret)
1169 		return ret;
1170 
1171 	platform_set_drvdata(dev, nmk_chip);
1172 
1173 	dev_info(&dev->dev, "chip registered\n");
1174 
1175 	return 0;
1176 }
1177 
1178 static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1179 {
1180 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1181 
1182 	return npct->soc->ngroups;
1183 }
1184 
1185 static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1186 				       unsigned selector)
1187 {
1188 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1189 
1190 	return npct->soc->groups[selector].name;
1191 }
1192 
1193 static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1194 			      const unsigned **pins,
1195 			      unsigned *num_pins)
1196 {
1197 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1198 
1199 	*pins = npct->soc->groups[selector].pins;
1200 	*num_pins = npct->soc->groups[selector].npins;
1201 	return 0;
1202 }
1203 
1204 static struct nmk_gpio_chip *find_nmk_gpio_from_pin(unsigned pin)
1205 {
1206 	int i;
1207 	struct nmk_gpio_chip *nmk_gpio;
1208 
1209 	for(i = 0; i < NMK_MAX_BANKS; i++) {
1210 		nmk_gpio = nmk_gpio_chips[i];
1211 		if (!nmk_gpio)
1212 			continue;
1213 		if (pin >= nmk_gpio->chip.base &&
1214 			pin < nmk_gpio->chip.base + nmk_gpio->chip.ngpio)
1215 			return nmk_gpio;
1216 	}
1217 	return NULL;
1218 }
1219 
1220 static struct gpio_chip *find_gc_from_pin(unsigned pin)
1221 {
1222 	struct nmk_gpio_chip *nmk_gpio = find_nmk_gpio_from_pin(pin);
1223 
1224 	if (nmk_gpio)
1225 		return &nmk_gpio->chip;
1226 	return NULL;
1227 }
1228 
1229 static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1230 		   unsigned offset)
1231 {
1232 	struct gpio_chip *chip = find_gc_from_pin(offset);
1233 
1234 	if (!chip) {
1235 		seq_printf(s, "invalid pin offset");
1236 		return;
1237 	}
1238 	nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
1239 }
1240 
1241 static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
1242 		unsigned *num_maps, const char *group,
1243 		const char *function)
1244 {
1245 	if (*num_maps == *reserved_maps)
1246 		return -ENOSPC;
1247 
1248 	(*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
1249 	(*map)[*num_maps].data.mux.group = group;
1250 	(*map)[*num_maps].data.mux.function = function;
1251 	(*num_maps)++;
1252 
1253 	return 0;
1254 }
1255 
1256 static int nmk_dt_add_map_configs(struct pinctrl_map **map,
1257 		unsigned *reserved_maps,
1258 		unsigned *num_maps, const char *group,
1259 		unsigned long *configs, unsigned num_configs)
1260 {
1261 	unsigned long *dup_configs;
1262 
1263 	if (*num_maps == *reserved_maps)
1264 		return -ENOSPC;
1265 
1266 	dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
1267 			      GFP_KERNEL);
1268 	if (!dup_configs)
1269 		return -ENOMEM;
1270 
1271 	(*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
1272 
1273 	(*map)[*num_maps].data.configs.group_or_pin = group;
1274 	(*map)[*num_maps].data.configs.configs = dup_configs;
1275 	(*map)[*num_maps].data.configs.num_configs = num_configs;
1276 	(*num_maps)++;
1277 
1278 	return 0;
1279 }
1280 
1281 #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
1282 #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
1283 	.size = ARRAY_SIZE(y), }
1284 
1285 static const unsigned long nmk_pin_input_modes[] = {
1286 	PIN_INPUT_NOPULL,
1287 	PIN_INPUT_PULLUP,
1288 	PIN_INPUT_PULLDOWN,
1289 };
1290 
1291 static const unsigned long nmk_pin_output_modes[] = {
1292 	PIN_OUTPUT_LOW,
1293 	PIN_OUTPUT_HIGH,
1294 	PIN_DIR_OUTPUT,
1295 };
1296 
1297 static const unsigned long nmk_pin_sleep_modes[] = {
1298 	PIN_SLEEPMODE_DISABLED,
1299 	PIN_SLEEPMODE_ENABLED,
1300 };
1301 
1302 static const unsigned long nmk_pin_sleep_input_modes[] = {
1303 	PIN_SLPM_INPUT_NOPULL,
1304 	PIN_SLPM_INPUT_PULLUP,
1305 	PIN_SLPM_INPUT_PULLDOWN,
1306 	PIN_SLPM_DIR_INPUT,
1307 };
1308 
1309 static const unsigned long nmk_pin_sleep_output_modes[] = {
1310 	PIN_SLPM_OUTPUT_LOW,
1311 	PIN_SLPM_OUTPUT_HIGH,
1312 	PIN_SLPM_DIR_OUTPUT,
1313 };
1314 
1315 static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
1316 	PIN_SLPM_WAKEUP_DISABLE,
1317 	PIN_SLPM_WAKEUP_ENABLE,
1318 };
1319 
1320 static const unsigned long nmk_pin_gpio_modes[] = {
1321 	PIN_GPIOMODE_DISABLED,
1322 	PIN_GPIOMODE_ENABLED,
1323 };
1324 
1325 static const unsigned long nmk_pin_sleep_pdis_modes[] = {
1326 	PIN_SLPM_PDIS_DISABLED,
1327 	PIN_SLPM_PDIS_ENABLED,
1328 };
1329 
1330 struct nmk_cfg_param {
1331 	const char *property;
1332 	unsigned long config;
1333 	const unsigned long *choice;
1334 	int size;
1335 };
1336 
1337 static const struct nmk_cfg_param nmk_cfg_params[] = {
1338 	NMK_CONFIG_PIN_ARRAY("ste,input",		nmk_pin_input_modes),
1339 	NMK_CONFIG_PIN_ARRAY("ste,output",		nmk_pin_output_modes),
1340 	NMK_CONFIG_PIN_ARRAY("ste,sleep",		nmk_pin_sleep_modes),
1341 	NMK_CONFIG_PIN_ARRAY("ste,sleep-input",		nmk_pin_sleep_input_modes),
1342 	NMK_CONFIG_PIN_ARRAY("ste,sleep-output",	nmk_pin_sleep_output_modes),
1343 	NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup",	nmk_pin_sleep_wakeup_modes),
1344 	NMK_CONFIG_PIN_ARRAY("ste,gpio",		nmk_pin_gpio_modes),
1345 	NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable",	nmk_pin_sleep_pdis_modes),
1346 };
1347 
1348 static int nmk_dt_pin_config(int index, int val, unsigned long *config)
1349 {
1350 	int ret = 0;
1351 
1352 	if (nmk_cfg_params[index].choice == NULL)
1353 		*config = nmk_cfg_params[index].config;
1354 	else {
1355 		/* test if out of range */
1356 		if  (val < nmk_cfg_params[index].size) {
1357 			*config = nmk_cfg_params[index].config |
1358 				nmk_cfg_params[index].choice[val];
1359 		}
1360 	}
1361 	return ret;
1362 }
1363 
1364 static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
1365 {
1366 	int i, pin_number;
1367 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1368 
1369 	if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
1370 		for (i = 0; i < npct->soc->npins; i++)
1371 			if (npct->soc->pins[i].number == pin_number)
1372 				return npct->soc->pins[i].name;
1373 	return NULL;
1374 }
1375 
1376 static bool nmk_pinctrl_dt_get_config(struct device_node *np,
1377 		unsigned long *configs)
1378 {
1379 	bool has_config = 0;
1380 	unsigned long cfg = 0;
1381 	int i, val, ret;
1382 
1383 	for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
1384 		ret = of_property_read_u32(np,
1385 				nmk_cfg_params[i].property, &val);
1386 		if (ret != -EINVAL) {
1387 			if (nmk_dt_pin_config(i, val, &cfg) == 0) {
1388 				*configs |= cfg;
1389 				has_config = 1;
1390 			}
1391 		}
1392 	}
1393 
1394 	return has_config;
1395 }
1396 
1397 static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
1398 		struct device_node *np,
1399 		struct pinctrl_map **map,
1400 		unsigned *reserved_maps,
1401 		unsigned *num_maps)
1402 {
1403 	int ret;
1404 	const char *function = NULL;
1405 	unsigned long configs = 0;
1406 	bool has_config = 0;
1407 	struct property *prop;
1408 	struct device_node *np_config;
1409 
1410 	ret = of_property_read_string(np, "function", &function);
1411 	if (ret >= 0) {
1412 		const char *group;
1413 
1414 		ret = of_property_count_strings(np, "groups");
1415 		if (ret < 0)
1416 			goto exit;
1417 
1418 		ret = pinctrl_utils_reserve_map(pctldev, map,
1419 						reserved_maps,
1420 						num_maps, ret);
1421 		if (ret < 0)
1422 			goto exit;
1423 
1424 		of_property_for_each_string(np, "groups", prop, group) {
1425 			ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
1426 					  group, function);
1427 			if (ret < 0)
1428 				goto exit;
1429 		}
1430 	}
1431 
1432 	has_config = nmk_pinctrl_dt_get_config(np, &configs);
1433 	np_config = of_parse_phandle(np, "ste,config", 0);
1434 	if (np_config)
1435 		has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
1436 	if (has_config) {
1437 		const char *gpio_name;
1438 		const char *pin;
1439 
1440 		ret = of_property_count_strings(np, "pins");
1441 		if (ret < 0)
1442 			goto exit;
1443 		ret = pinctrl_utils_reserve_map(pctldev, map,
1444 						reserved_maps,
1445 						num_maps, ret);
1446 		if (ret < 0)
1447 			goto exit;
1448 
1449 		of_property_for_each_string(np, "pins", prop, pin) {
1450 			gpio_name = nmk_find_pin_name(pctldev, pin);
1451 
1452 			ret = nmk_dt_add_map_configs(map, reserved_maps,
1453 						     num_maps,
1454 						     gpio_name, &configs, 1);
1455 			if (ret < 0)
1456 				goto exit;
1457 		}
1458 	}
1459 
1460 exit:
1461 	return ret;
1462 }
1463 
1464 static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
1465 				 struct device_node *np_config,
1466 				 struct pinctrl_map **map, unsigned *num_maps)
1467 {
1468 	unsigned reserved_maps;
1469 	struct device_node *np;
1470 	int ret;
1471 
1472 	reserved_maps = 0;
1473 	*map = NULL;
1474 	*num_maps = 0;
1475 
1476 	for_each_child_of_node(np_config, np) {
1477 		ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
1478 				&reserved_maps, num_maps);
1479 		if (ret < 0) {
1480 			pinctrl_utils_free_map(pctldev, *map, *num_maps);
1481 			of_node_put(np);
1482 			return ret;
1483 		}
1484 	}
1485 
1486 	return 0;
1487 }
1488 
1489 static const struct pinctrl_ops nmk_pinctrl_ops = {
1490 	.get_groups_count = nmk_get_groups_cnt,
1491 	.get_group_name = nmk_get_group_name,
1492 	.get_group_pins = nmk_get_group_pins,
1493 	.pin_dbg_show = nmk_pin_dbg_show,
1494 	.dt_node_to_map = nmk_pinctrl_dt_node_to_map,
1495 	.dt_free_map = pinctrl_utils_free_map,
1496 };
1497 
1498 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1499 {
1500 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1501 
1502 	return npct->soc->nfunctions;
1503 }
1504 
1505 static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1506 					 unsigned function)
1507 {
1508 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1509 
1510 	return npct->soc->functions[function].name;
1511 }
1512 
1513 static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1514 				   unsigned function,
1515 				   const char * const **groups,
1516 				   unsigned * const num_groups)
1517 {
1518 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1519 
1520 	*groups = npct->soc->functions[function].groups;
1521 	*num_groups = npct->soc->functions[function].ngroups;
1522 
1523 	return 0;
1524 }
1525 
1526 static int nmk_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
1527 		       unsigned group)
1528 {
1529 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1530 	const struct nmk_pingroup *g;
1531 	static unsigned int slpm[NUM_BANKS];
1532 	unsigned long flags = 0;
1533 	bool glitch;
1534 	int ret = -EINVAL;
1535 	int i;
1536 
1537 	g = &npct->soc->groups[group];
1538 
1539 	if (g->altsetting < 0)
1540 		return -EINVAL;
1541 
1542 	dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1543 
1544 	/*
1545 	 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1546 	 * we may pass through an undesired state. In this case we take
1547 	 * some extra care.
1548 	 *
1549 	 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1550 	 *  - Save SLPM registers (since we have a shadow register in the
1551 	 *    nmk_chip we're using that as backup)
1552 	 *  - Set SLPM=0 for the IOs you want to switch and others to 1
1553 	 *  - Configure the GPIO registers for the IOs that are being switched
1554 	 *  - Set IOFORCE=1
1555 	 *  - Modify the AFLSA/B registers for the IOs that are being switched
1556 	 *  - Set IOFORCE=0
1557 	 *  - Restore SLPM registers
1558 	 *  - Any spurious wake up event during switch sequence to be ignored
1559 	 *    and cleared
1560 	 *
1561 	 * We REALLY need to save ALL slpm registers, because the external
1562 	 * IOFORCE will switch *all* ports to their sleepmode setting to as
1563 	 * to avoid glitches. (Not just one port!)
1564 	 */
1565 	glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
1566 
1567 	if (glitch) {
1568 		spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1569 
1570 		/* Initially don't put any pins to sleep when switching */
1571 		memset(slpm, 0xff, sizeof(slpm));
1572 
1573 		/*
1574 		 * Then mask the pins that need to be sleeping now when we're
1575 		 * switching to the ALT C function.
1576 		 */
1577 		for (i = 0; i < g->npins; i++)
1578 			slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1579 		nmk_gpio_glitch_slpm_init(slpm);
1580 	}
1581 
1582 	for (i = 0; i < g->npins; i++) {
1583 		struct nmk_gpio_chip *nmk_chip;
1584 		unsigned bit;
1585 
1586 		nmk_chip = find_nmk_gpio_from_pin(g->pins[i]);
1587 		if (!nmk_chip) {
1588 			dev_err(npct->dev,
1589 				"invalid pin offset %d in group %s at index %d\n",
1590 				g->pins[i], g->name, i);
1591 			goto out_glitch;
1592 		}
1593 		dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1594 
1595 		clk_enable(nmk_chip->clk);
1596 		bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1597 		/*
1598 		 * If the pin is switching to altfunc, and there was an
1599 		 * interrupt installed on it which has been lazy disabled,
1600 		 * actually mask the interrupt to prevent spurious interrupts
1601 		 * that would occur while the pin is under control of the
1602 		 * peripheral. Only SKE does this.
1603 		 */
1604 		nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1605 
1606 		__nmk_gpio_set_mode_safe(nmk_chip, bit,
1607 			(g->altsetting & NMK_GPIO_ALT_C), glitch);
1608 		clk_disable(nmk_chip->clk);
1609 
1610 		/*
1611 		 * Call PRCM GPIOCR config function in case ALTC
1612 		 * has been selected:
1613 		 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1614 		 *   must be set.
1615 		 * - If selection is pure ALTC and previous selection was ALTCx,
1616 		 *   then some bits in PRCM GPIOCR registers must be cleared.
1617 		 */
1618 		if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1619 			nmk_prcm_altcx_set_mode(npct, g->pins[i],
1620 				g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
1621 	}
1622 
1623 	/* When all pins are successfully reconfigured we get here */
1624 	ret = 0;
1625 
1626 out_glitch:
1627 	if (glitch) {
1628 		nmk_gpio_glitch_slpm_restore(slpm);
1629 		spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1630 	}
1631 
1632 	return ret;
1633 }
1634 
1635 static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1636 				   struct pinctrl_gpio_range *range,
1637 				   unsigned offset)
1638 {
1639 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1640 	struct nmk_gpio_chip *nmk_chip;
1641 	struct gpio_chip *chip;
1642 	unsigned bit;
1643 
1644 	if (!range) {
1645 		dev_err(npct->dev, "invalid range\n");
1646 		return -EINVAL;
1647 	}
1648 	if (!range->gc) {
1649 		dev_err(npct->dev, "missing GPIO chip in range\n");
1650 		return -EINVAL;
1651 	}
1652 	chip = range->gc;
1653 	nmk_chip = gpiochip_get_data(chip);
1654 
1655 	dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1656 
1657 	clk_enable(nmk_chip->clk);
1658 	bit = offset % NMK_GPIO_PER_CHIP;
1659 	/* There is no glitch when converting any pin to GPIO */
1660 	__nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1661 	clk_disable(nmk_chip->clk);
1662 
1663 	return 0;
1664 }
1665 
1666 static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1667 				  struct pinctrl_gpio_range *range,
1668 				  unsigned offset)
1669 {
1670 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1671 
1672 	dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1673 	/* Set the pin to some default state, GPIO is usually default */
1674 }
1675 
1676 static const struct pinmux_ops nmk_pinmux_ops = {
1677 	.get_functions_count = nmk_pmx_get_funcs_cnt,
1678 	.get_function_name = nmk_pmx_get_func_name,
1679 	.get_function_groups = nmk_pmx_get_func_groups,
1680 	.set_mux = nmk_pmx_set,
1681 	.gpio_request_enable = nmk_gpio_request_enable,
1682 	.gpio_disable_free = nmk_gpio_disable_free,
1683 	.strict = true,
1684 };
1685 
1686 static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1687 			      unsigned long *config)
1688 {
1689 	/* Not implemented */
1690 	return -EINVAL;
1691 }
1692 
1693 static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1694 			      unsigned long *configs, unsigned num_configs)
1695 {
1696 	static const char *pullnames[] = {
1697 		[NMK_GPIO_PULL_NONE]	= "none",
1698 		[NMK_GPIO_PULL_UP]	= "up",
1699 		[NMK_GPIO_PULL_DOWN]	= "down",
1700 		[3] /* illegal */	= "??"
1701 	};
1702 	static const char *slpmnames[] = {
1703 		[NMK_GPIO_SLPM_INPUT]		= "input/wakeup",
1704 		[NMK_GPIO_SLPM_NOCHANGE]	= "no-change/no-wakeup",
1705 	};
1706 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1707 	struct nmk_gpio_chip *nmk_chip;
1708 	unsigned bit;
1709 	pin_cfg_t cfg;
1710 	int pull, slpm, output, val, i;
1711 	bool lowemi, gpiomode, sleep;
1712 
1713 	nmk_chip = find_nmk_gpio_from_pin(pin);
1714 	if (!nmk_chip) {
1715 		dev_err(npct->dev,
1716 			"invalid pin offset %d\n", pin);
1717 		return -EINVAL;
1718 	}
1719 
1720 	for (i = 0; i < num_configs; i++) {
1721 		/*
1722 		 * The pin config contains pin number and altfunction fields,
1723 		 * here we just ignore that part. It's being handled by the
1724 		 * framework and pinmux callback respectively.
1725 		 */
1726 		cfg = (pin_cfg_t) configs[i];
1727 		pull = PIN_PULL(cfg);
1728 		slpm = PIN_SLPM(cfg);
1729 		output = PIN_DIR(cfg);
1730 		val = PIN_VAL(cfg);
1731 		lowemi = PIN_LOWEMI(cfg);
1732 		gpiomode = PIN_GPIOMODE(cfg);
1733 		sleep = PIN_SLEEPMODE(cfg);
1734 
1735 		if (sleep) {
1736 			int slpm_pull = PIN_SLPM_PULL(cfg);
1737 			int slpm_output = PIN_SLPM_DIR(cfg);
1738 			int slpm_val = PIN_SLPM_VAL(cfg);
1739 
1740 			/* All pins go into GPIO mode at sleep */
1741 			gpiomode = true;
1742 
1743 			/*
1744 			 * The SLPM_* values are normal values + 1 to allow zero
1745 			 * to mean "same as normal".
1746 			 */
1747 			if (slpm_pull)
1748 				pull = slpm_pull - 1;
1749 			if (slpm_output)
1750 				output = slpm_output - 1;
1751 			if (slpm_val)
1752 				val = slpm_val - 1;
1753 
1754 			dev_dbg(nmk_chip->chip.parent,
1755 				"pin %d: sleep pull %s, dir %s, val %s\n",
1756 				pin,
1757 				slpm_pull ? pullnames[pull] : "same",
1758 				slpm_output ? (output ? "output" : "input")
1759 				: "same",
1760 				slpm_val ? (val ? "high" : "low") : "same");
1761 		}
1762 
1763 		dev_dbg(nmk_chip->chip.parent,
1764 			"pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1765 			pin, cfg, pullnames[pull], slpmnames[slpm],
1766 			output ? "output " : "input",
1767 			output ? (val ? "high" : "low") : "",
1768 			lowemi ? "on" : "off");
1769 
1770 		clk_enable(nmk_chip->clk);
1771 		bit = pin % NMK_GPIO_PER_CHIP;
1772 		if (gpiomode)
1773 			/* No glitch when going to GPIO mode */
1774 			__nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1775 		if (output)
1776 			__nmk_gpio_make_output(nmk_chip, bit, val);
1777 		else {
1778 			__nmk_gpio_make_input(nmk_chip, bit);
1779 			__nmk_gpio_set_pull(nmk_chip, bit, pull);
1780 		}
1781 		/* TODO: isn't this only applicable on output pins? */
1782 		__nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1783 
1784 		__nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1785 		clk_disable(nmk_chip->clk);
1786 	} /* for each config */
1787 
1788 	return 0;
1789 }
1790 
1791 static const struct pinconf_ops nmk_pinconf_ops = {
1792 	.pin_config_get = nmk_pin_config_get,
1793 	.pin_config_set = nmk_pin_config_set,
1794 };
1795 
1796 static struct pinctrl_desc nmk_pinctrl_desc = {
1797 	.name = "pinctrl-nomadik",
1798 	.pctlops = &nmk_pinctrl_ops,
1799 	.pmxops = &nmk_pinmux_ops,
1800 	.confops = &nmk_pinconf_ops,
1801 	.owner = THIS_MODULE,
1802 };
1803 
1804 static const struct of_device_id nmk_pinctrl_match[] = {
1805 	{
1806 		.compatible = "stericsson,stn8815-pinctrl",
1807 		.data = (void *)PINCTRL_NMK_STN8815,
1808 	},
1809 	{
1810 		.compatible = "stericsson,db8500-pinctrl",
1811 		.data = (void *)PINCTRL_NMK_DB8500,
1812 	},
1813 	{
1814 		.compatible = "stericsson,db8540-pinctrl",
1815 		.data = (void *)PINCTRL_NMK_DB8540,
1816 	},
1817 	{},
1818 };
1819 
1820 #ifdef CONFIG_PM_SLEEP
1821 static int nmk_pinctrl_suspend(struct device *dev)
1822 {
1823 	struct nmk_pinctrl *npct;
1824 
1825 	npct = dev_get_drvdata(dev);
1826 	if (!npct)
1827 		return -EINVAL;
1828 
1829 	return pinctrl_force_sleep(npct->pctl);
1830 }
1831 
1832 static int nmk_pinctrl_resume(struct device *dev)
1833 {
1834 	struct nmk_pinctrl *npct;
1835 
1836 	npct = dev_get_drvdata(dev);
1837 	if (!npct)
1838 		return -EINVAL;
1839 
1840 	return pinctrl_force_default(npct->pctl);
1841 }
1842 #endif
1843 
1844 static int nmk_pinctrl_probe(struct platform_device *pdev)
1845 {
1846 	const struct of_device_id *match;
1847 	struct device_node *np = pdev->dev.of_node;
1848 	struct device_node *prcm_np;
1849 	struct nmk_pinctrl *npct;
1850 	unsigned int version = 0;
1851 	int i;
1852 
1853 	npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1854 	if (!npct)
1855 		return -ENOMEM;
1856 
1857 	match = of_match_device(nmk_pinctrl_match, &pdev->dev);
1858 	if (!match)
1859 		return -ENODEV;
1860 	version = (unsigned int) match->data;
1861 
1862 	/* Poke in other ASIC variants here */
1863 	if (version == PINCTRL_NMK_STN8815)
1864 		nmk_pinctrl_stn8815_init(&npct->soc);
1865 	if (version == PINCTRL_NMK_DB8500)
1866 		nmk_pinctrl_db8500_init(&npct->soc);
1867 	if (version == PINCTRL_NMK_DB8540)
1868 		nmk_pinctrl_db8540_init(&npct->soc);
1869 
1870 	/*
1871 	 * Since we depend on the GPIO chips to provide clock and register base
1872 	 * for the pin control operations, make sure that we have these
1873 	 * populated before we continue. Follow the phandles to instantiate
1874 	 * them. The GPIO portion of the actual hardware may be probed before
1875 	 * or after this point: it shouldn't matter as the APIs are orthogonal.
1876 	 */
1877 	for (i = 0; i < NMK_MAX_BANKS; i++) {
1878 		struct device_node *gpio_np;
1879 		struct nmk_gpio_chip *nmk_chip;
1880 
1881 		gpio_np = of_parse_phandle(np, "nomadik-gpio-chips", i);
1882 		if (gpio_np) {
1883 			dev_info(&pdev->dev,
1884 				 "populate NMK GPIO %d \"%pOFn\"\n",
1885 				 i, gpio_np);
1886 			nmk_chip = nmk_gpio_populate_chip(gpio_np, pdev);
1887 			if (IS_ERR(nmk_chip))
1888 				dev_err(&pdev->dev,
1889 					"could not populate nmk chip struct "
1890 					"- continue anyway\n");
1891 			of_node_put(gpio_np);
1892 		}
1893 	}
1894 
1895 	prcm_np = of_parse_phandle(np, "prcm", 0);
1896 	if (prcm_np)
1897 		npct->prcm_base = of_iomap(prcm_np, 0);
1898 	if (!npct->prcm_base) {
1899 		if (version == PINCTRL_NMK_STN8815) {
1900 			dev_info(&pdev->dev,
1901 				 "No PRCM base, "
1902 				 "assuming no ALT-Cx control is available\n");
1903 		} else {
1904 			dev_err(&pdev->dev, "missing PRCM base address\n");
1905 			return -EINVAL;
1906 		}
1907 	}
1908 
1909 	nmk_pinctrl_desc.pins = npct->soc->pins;
1910 	nmk_pinctrl_desc.npins = npct->soc->npins;
1911 	npct->dev = &pdev->dev;
1912 
1913 	npct->pctl = devm_pinctrl_register(&pdev->dev, &nmk_pinctrl_desc, npct);
1914 	if (IS_ERR(npct->pctl)) {
1915 		dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1916 		return PTR_ERR(npct->pctl);
1917 	}
1918 
1919 	platform_set_drvdata(pdev, npct);
1920 	dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1921 
1922 	return 0;
1923 }
1924 
1925 static const struct of_device_id nmk_gpio_match[] = {
1926 	{ .compatible = "st,nomadik-gpio", },
1927 	{}
1928 };
1929 
1930 static struct platform_driver nmk_gpio_driver = {
1931 	.driver = {
1932 		.name = "gpio",
1933 		.of_match_table = nmk_gpio_match,
1934 	},
1935 	.probe = nmk_gpio_probe,
1936 };
1937 
1938 static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops,
1939 			nmk_pinctrl_suspend,
1940 			nmk_pinctrl_resume);
1941 
1942 static struct platform_driver nmk_pinctrl_driver = {
1943 	.driver = {
1944 		.name = "pinctrl-nomadik",
1945 		.of_match_table = nmk_pinctrl_match,
1946 		.pm = &nmk_pinctrl_pm_ops,
1947 	},
1948 	.probe = nmk_pinctrl_probe,
1949 };
1950 
1951 static int __init nmk_gpio_init(void)
1952 {
1953 	return platform_driver_register(&nmk_gpio_driver);
1954 }
1955 subsys_initcall(nmk_gpio_init);
1956 
1957 static int __init nmk_pinctrl_init(void)
1958 {
1959 	return platform_driver_register(&nmk_pinctrl_driver);
1960 }
1961 core_initcall(nmk_pinctrl_init);
1962