• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Pinmux & pinconf driver for the IP block found in the Nomadik SoC. This
4  * depends on gpio-nomadik and some handling is intertwined; see nmk_gpio_chips
5  * which is used by this driver to access the GPIO banks array.
6  *
7  * Copyright (C) 2008,2009 STMicroelectronics
8  * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
9  *   Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
10  * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org>
11  */
12 
13 #include <linux/bitops.h>
14 #include <linux/cleanup.h>
15 #include <linux/clk.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/platform_device.h>
27 #include <linux/property.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/types.h>
32 
33 /* Since we request GPIOs from ourself */
34 #include <linux/pinctrl/consumer.h>
35 #include <linux/pinctrl/machine.h>
36 #include <linux/pinctrl/pinconf.h>
37 #include <linux/pinctrl/pinctrl.h>
38 #include <linux/pinctrl/pinmux.h>
39 
40 #include "../core.h"
41 #include "../pinctrl-utils.h"
42 
43 #include <linux/gpio/gpio-nomadik.h>
44 
45 /*
46  * pin configurations are represented by 32-bit integers:
47  *
48  *	bit  0.. 8 - Pin Number (512 Pins Maximum)
49  *	bit  9..10 - Alternate Function Selection
50  *	bit 11..12 - Pull up/down state
51  *	bit     13 - Sleep mode behaviour
52  *	bit     14 - Direction
53  *	bit     15 - Value (if output)
54  *	bit 16..18 - SLPM pull up/down state
55  *	bit 19..20 - SLPM direction
56  *	bit 21..22 - SLPM Value (if output)
57  *	bit 23..25 - PDIS value (if input)
58  *	bit	26 - Gpio mode
59  *	bit	27 - Sleep mode
60  *
61  * to facilitate the definition, the following macros are provided
62  *
63  * PIN_CFG_DEFAULT - default config (0):
64  *		     pull up/down = disabled
65  *		     sleep mode = input/wakeup
66  *		     direction = input
67  *		     value = low
68  *		     SLPM direction = same as normal
69  *		     SLPM pull = same as normal
70  *		     SLPM value = same as normal
71  *
72  * PIN_CFG	   - default config with alternate function
73  */
74 
75 #define PIN_NUM_MASK		0x1ff
76 #define PIN_NUM(x)		((x) & PIN_NUM_MASK)
77 
78 #define PIN_ALT_SHIFT		9
79 #define PIN_ALT_MASK		(0x3 << PIN_ALT_SHIFT)
80 #define PIN_ALT(x)		(((x) & PIN_ALT_MASK) >> PIN_ALT_SHIFT)
81 #define PIN_GPIO		(NMK_GPIO_ALT_GPIO << PIN_ALT_SHIFT)
82 #define PIN_ALT_A		(NMK_GPIO_ALT_A << PIN_ALT_SHIFT)
83 #define PIN_ALT_B		(NMK_GPIO_ALT_B << PIN_ALT_SHIFT)
84 #define PIN_ALT_C		(NMK_GPIO_ALT_C << PIN_ALT_SHIFT)
85 
86 #define PIN_PULL_SHIFT		11
87 #define PIN_PULL_MASK		(0x3 << PIN_PULL_SHIFT)
88 #define PIN_PULL(x)		(((x) & PIN_PULL_MASK) >> PIN_PULL_SHIFT)
89 #define PIN_PULL_NONE		(NMK_GPIO_PULL_NONE << PIN_PULL_SHIFT)
90 #define PIN_PULL_UP		(NMK_GPIO_PULL_UP << PIN_PULL_SHIFT)
91 #define PIN_PULL_DOWN		(NMK_GPIO_PULL_DOWN << PIN_PULL_SHIFT)
92 
93 #define PIN_SLPM_SHIFT		13
94 #define PIN_SLPM_MASK		(0x1 << PIN_SLPM_SHIFT)
95 #define PIN_SLPM(x)		(((x) & PIN_SLPM_MASK) >> PIN_SLPM_SHIFT)
96 #define PIN_SLPM_MAKE_INPUT	(NMK_GPIO_SLPM_INPUT << PIN_SLPM_SHIFT)
97 #define PIN_SLPM_NOCHANGE	(NMK_GPIO_SLPM_NOCHANGE << PIN_SLPM_SHIFT)
98 /* These two replace the above in DB8500v2+ */
99 #define PIN_SLPM_WAKEUP_ENABLE	(NMK_GPIO_SLPM_WAKEUP_ENABLE << PIN_SLPM_SHIFT)
100 #define PIN_SLPM_WAKEUP_DISABLE	(NMK_GPIO_SLPM_WAKEUP_DISABLE << PIN_SLPM_SHIFT)
101 #define PIN_SLPM_USE_MUX_SETTINGS_IN_SLEEP PIN_SLPM_WAKEUP_DISABLE
102 
103 #define PIN_SLPM_GPIO  PIN_SLPM_WAKEUP_ENABLE /* In SLPM, pin is a gpio */
104 #define PIN_SLPM_ALTFUNC PIN_SLPM_WAKEUP_DISABLE /* In SLPM, pin is altfunc */
105 
106 #define PIN_DIR_SHIFT		14
107 #define PIN_DIR_MASK		(0x1 << PIN_DIR_SHIFT)
108 #define PIN_DIR(x)		(((x) & PIN_DIR_MASK) >> PIN_DIR_SHIFT)
109 #define PIN_DIR_INPUT		(0 << PIN_DIR_SHIFT)
110 #define PIN_DIR_OUTPUT		(1 << PIN_DIR_SHIFT)
111 
112 #define PIN_VAL_SHIFT		15
113 #define PIN_VAL_MASK		(0x1 << PIN_VAL_SHIFT)
114 #define PIN_VAL(x)		(((x) & PIN_VAL_MASK) >> PIN_VAL_SHIFT)
115 #define PIN_VAL_LOW		(0 << PIN_VAL_SHIFT)
116 #define PIN_VAL_HIGH		(1 << PIN_VAL_SHIFT)
117 
118 #define PIN_SLPM_PULL_SHIFT	16
119 #define PIN_SLPM_PULL_MASK	(0x7 << PIN_SLPM_PULL_SHIFT)
120 #define PIN_SLPM_PULL(x)	\
121 	(((x) & PIN_SLPM_PULL_MASK) >> PIN_SLPM_PULL_SHIFT)
122 #define PIN_SLPM_PULL_NONE	\
123 	((1 + NMK_GPIO_PULL_NONE) << PIN_SLPM_PULL_SHIFT)
124 #define PIN_SLPM_PULL_UP	\
125 	((1 + NMK_GPIO_PULL_UP) << PIN_SLPM_PULL_SHIFT)
126 #define PIN_SLPM_PULL_DOWN	\
127 	((1 + NMK_GPIO_PULL_DOWN) << PIN_SLPM_PULL_SHIFT)
128 
129 #define PIN_SLPM_DIR_SHIFT	19
130 #define PIN_SLPM_DIR_MASK	(0x3 << PIN_SLPM_DIR_SHIFT)
131 #define PIN_SLPM_DIR(x)		\
132 	(((x) & PIN_SLPM_DIR_MASK) >> PIN_SLPM_DIR_SHIFT)
133 #define PIN_SLPM_DIR_INPUT	((1 + 0) << PIN_SLPM_DIR_SHIFT)
134 #define PIN_SLPM_DIR_OUTPUT	((1 + 1) << PIN_SLPM_DIR_SHIFT)
135 
136 #define PIN_SLPM_VAL_SHIFT	21
137 #define PIN_SLPM_VAL_MASK	(0x3 << PIN_SLPM_VAL_SHIFT)
138 #define PIN_SLPM_VAL(x)		\
139 	(((x) & PIN_SLPM_VAL_MASK) >> PIN_SLPM_VAL_SHIFT)
140 #define PIN_SLPM_VAL_LOW	((1 + 0) << PIN_SLPM_VAL_SHIFT)
141 #define PIN_SLPM_VAL_HIGH	((1 + 1) << PIN_SLPM_VAL_SHIFT)
142 
143 #define PIN_SLPM_PDIS_SHIFT		23
144 #define PIN_SLPM_PDIS_MASK		(0x3 << PIN_SLPM_PDIS_SHIFT)
145 #define PIN_SLPM_PDIS(x)	\
146 	(((x) & PIN_SLPM_PDIS_MASK) >> PIN_SLPM_PDIS_SHIFT)
147 #define PIN_SLPM_PDIS_NO_CHANGE		(0 << PIN_SLPM_PDIS_SHIFT)
148 #define PIN_SLPM_PDIS_DISABLED		(1 << PIN_SLPM_PDIS_SHIFT)
149 #define PIN_SLPM_PDIS_ENABLED		(2 << PIN_SLPM_PDIS_SHIFT)
150 
151 #define PIN_LOWEMI_SHIFT	25
152 #define PIN_LOWEMI_MASK		(0x1 << PIN_LOWEMI_SHIFT)
153 #define PIN_LOWEMI(x)		(((x) & PIN_LOWEMI_MASK) >> PIN_LOWEMI_SHIFT)
154 #define PIN_LOWEMI_DISABLED	(0 << PIN_LOWEMI_SHIFT)
155 #define PIN_LOWEMI_ENABLED	(1 << PIN_LOWEMI_SHIFT)
156 
157 #define PIN_GPIOMODE_SHIFT	26
158 #define PIN_GPIOMODE_MASK	(0x1 << PIN_GPIOMODE_SHIFT)
159 #define PIN_GPIOMODE(x)		(((x) & PIN_GPIOMODE_MASK) >> PIN_GPIOMODE_SHIFT)
160 #define PIN_GPIOMODE_DISABLED	(0 << PIN_GPIOMODE_SHIFT)
161 #define PIN_GPIOMODE_ENABLED	(1 << PIN_GPIOMODE_SHIFT)
162 
163 #define PIN_SLEEPMODE_SHIFT	27
164 #define PIN_SLEEPMODE_MASK	(0x1 << PIN_SLEEPMODE_SHIFT)
165 #define PIN_SLEEPMODE(x)	(((x) & PIN_SLEEPMODE_MASK) >> PIN_SLEEPMODE_SHIFT)
166 #define PIN_SLEEPMODE_DISABLED	(0 << PIN_SLEEPMODE_SHIFT)
167 #define PIN_SLEEPMODE_ENABLED	(1 << PIN_SLEEPMODE_SHIFT)
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  * struct nmk_pinctrl - state container for the Nomadik pin controller
198  * @dev: containing device pointer
199  * @pctl: corresponding pin controller device
200  * @soc: SoC data for this specific chip
201  * @prcm_base: PRCM register range virtual base
202  */
203 struct nmk_pinctrl {
204 	struct device *dev;
205 	struct pinctrl_dev *pctl;
206 	const struct nmk_pinctrl_soc_data *soc;
207 	void __iomem *prcm_base;
208 };
209 
210 /* See nmk_gpio_populate_chip() that fills this array. */
211 struct nmk_gpio_chip *nmk_gpio_chips[NMK_MAX_BANKS];
212 
213 DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
214 
__nmk_gpio_set_mode(struct nmk_gpio_chip * nmk_chip,unsigned int offset,int gpio_mode)215 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
216 				unsigned int offset, int gpio_mode)
217 {
218 	u32 afunc, bfunc;
219 
220 	afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~BIT(offset);
221 	bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~BIT(offset);
222 	if (gpio_mode & NMK_GPIO_ALT_A)
223 		afunc |= BIT(offset);
224 	if (gpio_mode & NMK_GPIO_ALT_B)
225 		bfunc |= BIT(offset);
226 	writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
227 	writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
228 }
229 
__nmk_gpio_set_pull(struct nmk_gpio_chip * nmk_chip,unsigned int offset,enum nmk_gpio_pull pull)230 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
231 				unsigned int offset, enum nmk_gpio_pull pull)
232 {
233 	u32 pdis;
234 
235 	pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
236 	if (pull == NMK_GPIO_PULL_NONE) {
237 		pdis |= BIT(offset);
238 		nmk_chip->pull_up &= ~BIT(offset);
239 	} else {
240 		pdis &= ~BIT(offset);
241 	}
242 
243 	writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
244 
245 	if (pull == NMK_GPIO_PULL_UP) {
246 		nmk_chip->pull_up |= BIT(offset);
247 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATS);
248 	} else if (pull == NMK_GPIO_PULL_DOWN) {
249 		nmk_chip->pull_up &= ~BIT(offset);
250 		writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATC);
251 	}
252 }
253 
__nmk_gpio_set_lowemi(struct nmk_gpio_chip * nmk_chip,unsigned int offset,bool lowemi)254 static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
255 				  unsigned int offset, bool lowemi)
256 {
257 	bool enabled = nmk_chip->lowemi & BIT(offset);
258 
259 	if (lowemi == enabled)
260 		return;
261 
262 	if (lowemi)
263 		nmk_chip->lowemi |= BIT(offset);
264 	else
265 		nmk_chip->lowemi &= ~BIT(offset);
266 
267 	writel_relaxed(nmk_chip->lowemi,
268 		       nmk_chip->addr + NMK_GPIO_LOWEMI);
269 }
270 
__nmk_gpio_make_input(struct nmk_gpio_chip * nmk_chip,unsigned int offset)271 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
272 				  unsigned int offset)
273 {
274 	writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRC);
275 }
276 
__nmk_gpio_set_mode_safe(struct nmk_gpio_chip * nmk_chip,unsigned int offset,int gpio_mode,bool glitch)277 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
278 				     unsigned int offset, int gpio_mode,
279 				     bool glitch)
280 {
281 	u32 rwimsc = nmk_chip->rwimsc;
282 	u32 fwimsc = nmk_chip->fwimsc;
283 
284 	if (glitch && nmk_chip->set_ioforce) {
285 		u32 bit = BIT(offset);
286 
287 		/* Prevent spurious wakeups */
288 		writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
289 		writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
290 
291 		nmk_chip->set_ioforce(true);
292 	}
293 
294 	__nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
295 
296 	if (glitch && nmk_chip->set_ioforce) {
297 		nmk_chip->set_ioforce(false);
298 
299 		writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
300 		writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
301 	}
302 }
303 
304 static void
nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip * nmk_chip,unsigned int offset)305 nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned int offset)
306 {
307 	u32 falling = nmk_chip->fimsc & BIT(offset);
308 	u32 rising = nmk_chip->rimsc & BIT(offset);
309 	int gpio = nmk_chip->chip.base + offset;
310 	int irq = irq_find_mapping(nmk_chip->chip.irq.domain, offset);
311 	struct irq_data *d = irq_get_irq_data(irq);
312 
313 	if (!rising && !falling)
314 		return;
315 
316 	if (!d || !irqd_irq_disabled(d))
317 		return;
318 
319 	if (rising) {
320 		nmk_chip->rimsc &= ~BIT(offset);
321 		writel_relaxed(nmk_chip->rimsc,
322 			       nmk_chip->addr + NMK_GPIO_RIMSC);
323 	}
324 
325 	if (falling) {
326 		nmk_chip->fimsc &= ~BIT(offset);
327 		writel_relaxed(nmk_chip->fimsc,
328 			       nmk_chip->addr + NMK_GPIO_FIMSC);
329 	}
330 
331 	dev_dbg(nmk_chip->chip.parent, "%d: clearing interrupt mask\n", gpio);
332 }
333 
nmk_write_masked(void __iomem * reg,u32 mask,u32 value)334 static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
335 {
336 	u32 val;
337 
338 	val = readl(reg);
339 	val = ((val & ~mask) | (value & mask));
340 	writel(val, reg);
341 }
342 
nmk_prcm_altcx_set_mode(struct nmk_pinctrl * npct,unsigned int offset,unsigned int alt_num)343 static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
344 				    unsigned int offset, unsigned int alt_num)
345 {
346 	int i;
347 	u16 reg;
348 	u8 bit;
349 	u8 alt_index;
350 	const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
351 	const u16 *gpiocr_regs;
352 
353 	if (!npct->prcm_base)
354 		return;
355 
356 	if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
357 		dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
358 			alt_num);
359 		return;
360 	}
361 
362 	for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
363 		if (npct->soc->altcx_pins[i].pin == offset)
364 			break;
365 	}
366 	if (i == npct->soc->npins_altcx) {
367 		dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
368 			offset);
369 		return;
370 	}
371 
372 	pin_desc = npct->soc->altcx_pins + i;
373 	gpiocr_regs = npct->soc->prcm_gpiocr_registers;
374 
375 	/*
376 	 * If alt_num is NULL, just clear current ALTCx selection
377 	 * to make sure we come back to a pure ALTC selection
378 	 */
379 	if (!alt_num) {
380 		for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
381 			if (pin_desc->altcx[i].used) {
382 				reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
383 				bit = pin_desc->altcx[i].control_bit;
384 				if (readl(npct->prcm_base + reg) & BIT(bit)) {
385 					nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
386 					dev_dbg(npct->dev,
387 						"PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
388 						offset, i + 1);
389 				}
390 			}
391 		}
392 		return;
393 	}
394 
395 	alt_index = alt_num - 1;
396 	if (!pin_desc->altcx[alt_index].used) {
397 		dev_warn(npct->dev,
398 			 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
399 			 offset, alt_num);
400 		return;
401 	}
402 
403 	/*
404 	 * Check if any other ALTCx functions are activated on this pin
405 	 * and disable it first.
406 	 */
407 	for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
408 		if (i == alt_index)
409 			continue;
410 		if (pin_desc->altcx[i].used) {
411 			reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
412 			bit = pin_desc->altcx[i].control_bit;
413 			if (readl(npct->prcm_base + reg) & BIT(bit)) {
414 				nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
415 				dev_dbg(npct->dev,
416 					"PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
417 					offset, i + 1);
418 			}
419 		}
420 	}
421 
422 	reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
423 	bit = pin_desc->altcx[alt_index].control_bit;
424 	dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
425 		offset, alt_index + 1);
426 	nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
427 }
428 
429 /*
430  * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
431  *  - Save SLPM registers
432  *  - Set SLPM=0 for the IOs you want to switch and others to 1
433  *  - Configure the GPIO registers for the IOs that are being switched
434  *  - Set IOFORCE=1
435  *  - Modify the AFLSA/B registers for the IOs that are being switched
436  *  - Set IOFORCE=0
437  *  - Restore SLPM registers
438  *  - Any spurious wake up event during switch sequence to be ignored and
439  *    cleared
440  */
nmk_gpio_glitch_slpm_init(unsigned int * slpm)441 static int nmk_gpio_glitch_slpm_init(unsigned int *slpm)
442 {
443 	int i, j, ret;
444 
445 	for (i = 0; i < NMK_MAX_BANKS; i++) {
446 		struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
447 		unsigned int temp = slpm[i];
448 
449 		if (!chip)
450 			break;
451 
452 		ret = clk_enable(chip->clk);
453 		if (ret) {
454 			for (j = 0; j < i; j++) {
455 				chip = nmk_gpio_chips[j];
456 				clk_disable(chip->clk);
457 			}
458 
459 			return ret;
460 		}
461 
462 		slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
463 		writel(temp, chip->addr + NMK_GPIO_SLPC);
464 	}
465 
466 	return 0;
467 }
468 
nmk_gpio_glitch_slpm_restore(unsigned int * slpm)469 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
470 {
471 	int i;
472 
473 	for (i = 0; i < NMK_MAX_BANKS; i++) {
474 		struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
475 
476 		if (!chip)
477 			break;
478 
479 		writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
480 
481 		clk_disable(chip->clk);
482 	}
483 }
484 
485 /* Only called by gpio-nomadik but requires knowledge of struct nmk_pinctrl. */
nmk_prcm_gpiocr_get_mode(struct pinctrl_dev * pctldev,int gpio)486 int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
487 {
488 	int i;
489 	u16 reg;
490 	u8 bit;
491 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
492 	const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
493 	const u16 *gpiocr_regs;
494 
495 	if (!npct->prcm_base)
496 		return NMK_GPIO_ALT_C;
497 
498 	for (i = 0; i < npct->soc->npins_altcx; i++) {
499 		if (npct->soc->altcx_pins[i].pin == gpio)
500 			break;
501 	}
502 	if (i == npct->soc->npins_altcx)
503 		return NMK_GPIO_ALT_C;
504 
505 	pin_desc = npct->soc->altcx_pins + i;
506 	gpiocr_regs = npct->soc->prcm_gpiocr_registers;
507 	for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
508 		if (pin_desc->altcx[i].used) {
509 			reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
510 			bit = pin_desc->altcx[i].control_bit;
511 			if (readl(npct->prcm_base + reg) & BIT(bit))
512 				return NMK_GPIO_ALT_C + i + 1;
513 		}
514 	}
515 	return NMK_GPIO_ALT_C;
516 }
517 
nmk_get_groups_cnt(struct pinctrl_dev * pctldev)518 static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
519 {
520 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
521 
522 	return npct->soc->ngroups;
523 }
524 
nmk_get_group_name(struct pinctrl_dev * pctldev,unsigned int selector)525 static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
526 				      unsigned int selector)
527 {
528 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
529 
530 	return npct->soc->groups[selector].grp.name;
531 }
532 
nmk_get_group_pins(struct pinctrl_dev * pctldev,unsigned int selector,const unsigned int ** pins,unsigned int * num_pins)533 static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
534 			      const unsigned int **pins,
535 			      unsigned int *num_pins)
536 {
537 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
538 
539 	*pins = npct->soc->groups[selector].grp.pins;
540 	*num_pins = npct->soc->groups[selector].grp.npins;
541 	return 0;
542 }
543 
544 /* This makes the mapping from pin number to a GPIO chip. We also return the pin
545  * offset in the GPIO chip for convenience (and to avoid a second loop).
546  */
find_nmk_gpio_from_pin(unsigned int pin,unsigned int * offset)547 static struct nmk_gpio_chip *find_nmk_gpio_from_pin(unsigned int pin,
548 						    unsigned int *offset)
549 {
550 	int i, j = 0;
551 	struct nmk_gpio_chip *nmk_gpio;
552 
553 	/* We assume that pins are allocated in bank order. */
554 	for (i = 0; i < NMK_MAX_BANKS; i++) {
555 		nmk_gpio = nmk_gpio_chips[i];
556 		if (!nmk_gpio)
557 			continue;
558 		if (pin >= j && pin < j + nmk_gpio->chip.ngpio) {
559 			if (offset)
560 				*offset = pin - j;
561 			return nmk_gpio;
562 		}
563 		j += nmk_gpio->chip.ngpio;
564 	}
565 	return NULL;
566 }
567 
find_gc_from_pin(unsigned int pin)568 static struct gpio_chip *find_gc_from_pin(unsigned int pin)
569 {
570 	struct nmk_gpio_chip *nmk_gpio = find_nmk_gpio_from_pin(pin, NULL);
571 
572 	if (nmk_gpio)
573 		return &nmk_gpio->chip;
574 	return NULL;
575 }
576 
nmk_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int offset)577 static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
578 			     unsigned int offset)
579 {
580 	struct gpio_chip *chip = find_gc_from_pin(offset);
581 
582 	if (!chip) {
583 		seq_printf(s, "invalid pin offset");
584 		return;
585 	}
586 	nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
587 }
588 
nmk_dt_add_map_mux(struct pinctrl_map ** map,unsigned int * reserved_maps,unsigned int * num_maps,const char * group,const char * function)589 static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned int *reserved_maps,
590 			      unsigned int *num_maps, const char *group,
591 			      const char *function)
592 {
593 	if (*num_maps == *reserved_maps)
594 		return -ENOSPC;
595 
596 	(*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
597 	(*map)[*num_maps].data.mux.group = group;
598 	(*map)[*num_maps].data.mux.function = function;
599 	(*num_maps)++;
600 
601 	return 0;
602 }
603 
nmk_dt_add_map_configs(struct pinctrl_map ** map,unsigned int * reserved_maps,unsigned int * num_maps,const char * group,unsigned long * configs,unsigned int num_configs)604 static int nmk_dt_add_map_configs(struct pinctrl_map **map,
605 				  unsigned int *reserved_maps,
606 				  unsigned int *num_maps, const char *group,
607 				  unsigned long *configs, unsigned int num_configs)
608 {
609 	unsigned long *dup_configs;
610 
611 	if (*num_maps == *reserved_maps)
612 		return -ENOSPC;
613 
614 	dup_configs = kmemdup_array(configs, num_configs, sizeof(*dup_configs), GFP_KERNEL);
615 	if (!dup_configs)
616 		return -ENOMEM;
617 
618 	(*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
619 
620 	(*map)[*num_maps].data.configs.group_or_pin = group;
621 	(*map)[*num_maps].data.configs.configs = dup_configs;
622 	(*map)[*num_maps].data.configs.num_configs = num_configs;
623 	(*num_maps)++;
624 
625 	return 0;
626 }
627 
628 #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
629 #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
630 	.size = ARRAY_SIZE(y), }
631 
632 static const unsigned long nmk_pin_input_modes[] = {
633 	PIN_INPUT_NOPULL,
634 	PIN_INPUT_PULLUP,
635 	PIN_INPUT_PULLDOWN,
636 };
637 
638 static const unsigned long nmk_pin_output_modes[] = {
639 	PIN_OUTPUT_LOW,
640 	PIN_OUTPUT_HIGH,
641 	PIN_DIR_OUTPUT,
642 };
643 
644 static const unsigned long nmk_pin_sleep_modes[] = {
645 	PIN_SLEEPMODE_DISABLED,
646 	PIN_SLEEPMODE_ENABLED,
647 };
648 
649 static const unsigned long nmk_pin_sleep_input_modes[] = {
650 	PIN_SLPM_INPUT_NOPULL,
651 	PIN_SLPM_INPUT_PULLUP,
652 	PIN_SLPM_INPUT_PULLDOWN,
653 	PIN_SLPM_DIR_INPUT,
654 };
655 
656 static const unsigned long nmk_pin_sleep_output_modes[] = {
657 	PIN_SLPM_OUTPUT_LOW,
658 	PIN_SLPM_OUTPUT_HIGH,
659 	PIN_SLPM_DIR_OUTPUT,
660 };
661 
662 static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
663 	PIN_SLPM_WAKEUP_DISABLE,
664 	PIN_SLPM_WAKEUP_ENABLE,
665 };
666 
667 static const unsigned long nmk_pin_gpio_modes[] = {
668 	PIN_GPIOMODE_DISABLED,
669 	PIN_GPIOMODE_ENABLED,
670 };
671 
672 static const unsigned long nmk_pin_sleep_pdis_modes[] = {
673 	PIN_SLPM_PDIS_DISABLED,
674 	PIN_SLPM_PDIS_ENABLED,
675 };
676 
677 struct nmk_cfg_param {
678 	const char *property;
679 	unsigned long config;
680 	const unsigned long *choice;
681 	int size;
682 };
683 
684 static const struct nmk_cfg_param nmk_cfg_params[] = {
685 	NMK_CONFIG_PIN_ARRAY("ste,input",		nmk_pin_input_modes),
686 	NMK_CONFIG_PIN_ARRAY("ste,output",		nmk_pin_output_modes),
687 	NMK_CONFIG_PIN_ARRAY("ste,sleep",		nmk_pin_sleep_modes),
688 	NMK_CONFIG_PIN_ARRAY("ste,sleep-input",		nmk_pin_sleep_input_modes),
689 	NMK_CONFIG_PIN_ARRAY("ste,sleep-output",	nmk_pin_sleep_output_modes),
690 	NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup",	nmk_pin_sleep_wakeup_modes),
691 	NMK_CONFIG_PIN_ARRAY("ste,gpio",		nmk_pin_gpio_modes),
692 	NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable",	nmk_pin_sleep_pdis_modes),
693 };
694 
nmk_dt_pin_config(int index,int val,unsigned long * config)695 static int nmk_dt_pin_config(int index, int val, unsigned long *config)
696 {
697 	if (!nmk_cfg_params[index].choice) {
698 		*config = nmk_cfg_params[index].config;
699 	} else {
700 		/* test if out of range */
701 		if  (val < nmk_cfg_params[index].size) {
702 			*config = nmk_cfg_params[index].config |
703 				nmk_cfg_params[index].choice[val];
704 		}
705 	}
706 	return 0;
707 }
708 
nmk_find_pin_name(struct pinctrl_dev * pctldev,const char * pin_name)709 static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
710 {
711 	int i, pin_number;
712 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
713 
714 	if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
715 		for (i = 0; i < npct->soc->npins; i++)
716 			if (npct->soc->pins[i].number == pin_number)
717 				return npct->soc->pins[i].name;
718 	return NULL;
719 }
720 
nmk_pinctrl_dt_get_config(struct device_node * np,unsigned long * configs)721 static bool nmk_pinctrl_dt_get_config(struct device_node *np,
722 				      unsigned long *configs)
723 {
724 	bool has_config = 0;
725 	unsigned long cfg = 0;
726 	int i, val, ret;
727 
728 	for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
729 		ret = of_property_read_u32(np, nmk_cfg_params[i].property, &val);
730 		if (ret != -EINVAL) {
731 			if (nmk_dt_pin_config(i, val, &cfg) == 0) {
732 				*configs |= cfg;
733 				has_config = 1;
734 			}
735 		}
736 	}
737 
738 	return has_config;
739 }
740 
nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned int * reserved_maps,unsigned int * num_maps)741 static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
742 					 struct device_node *np,
743 					 struct pinctrl_map **map,
744 					 unsigned int *reserved_maps,
745 					 unsigned int *num_maps)
746 {
747 	int ret;
748 	const char *function = NULL;
749 	unsigned long configs = 0;
750 	bool has_config = 0;
751 	struct property *prop;
752 	struct device_node *np_config;
753 
754 	ret = of_property_read_string(np, "function", &function);
755 	if (ret >= 0) {
756 		const char *group;
757 
758 		ret = of_property_count_strings(np, "groups");
759 		if (ret < 0)
760 			goto exit;
761 
762 		ret = pinctrl_utils_reserve_map(pctldev, map,
763 						reserved_maps,
764 						num_maps, ret);
765 		if (ret < 0)
766 			goto exit;
767 
768 		of_property_for_each_string(np, "groups", prop, group) {
769 			ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
770 						 group, function);
771 			if (ret < 0)
772 				goto exit;
773 		}
774 	}
775 
776 	has_config = nmk_pinctrl_dt_get_config(np, &configs);
777 	np_config = of_parse_phandle(np, "ste,config", 0);
778 	if (np_config) {
779 		has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
780 		of_node_put(np_config);
781 	}
782 	if (has_config) {
783 		const char *gpio_name;
784 		const char *pin;
785 
786 		ret = of_property_count_strings(np, "pins");
787 		if (ret < 0)
788 			goto exit;
789 		ret = pinctrl_utils_reserve_map(pctldev, map,
790 						reserved_maps,
791 						num_maps, ret);
792 		if (ret < 0)
793 			goto exit;
794 
795 		of_property_for_each_string(np, "pins", prop, pin) {
796 			gpio_name = nmk_find_pin_name(pctldev, pin);
797 
798 			ret = nmk_dt_add_map_configs(map, reserved_maps,
799 						     num_maps,
800 						     gpio_name, &configs, 1);
801 			if (ret < 0)
802 				goto exit;
803 		}
804 	}
805 
806 exit:
807 	return ret;
808 }
809 
nmk_pinctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned int * num_maps)810 static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
811 				      struct device_node *np_config,
812 				      struct pinctrl_map **map,
813 				      unsigned int *num_maps)
814 {
815 	unsigned int reserved_maps;
816 	int ret;
817 
818 	reserved_maps = 0;
819 	*map = NULL;
820 	*num_maps = 0;
821 
822 	for_each_child_of_node_scoped(np_config, np) {
823 		ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
824 						    &reserved_maps, num_maps);
825 		if (ret < 0) {
826 			pinctrl_utils_free_map(pctldev, *map, *num_maps);
827 			return ret;
828 		}
829 	}
830 
831 	return 0;
832 }
833 
834 static const struct pinctrl_ops nmk_pinctrl_ops = {
835 	.get_groups_count = nmk_get_groups_cnt,
836 	.get_group_name = nmk_get_group_name,
837 	.get_group_pins = nmk_get_group_pins,
838 	.pin_dbg_show = nmk_pin_dbg_show,
839 	.dt_node_to_map = nmk_pinctrl_dt_node_to_map,
840 	.dt_free_map = pinctrl_utils_free_map,
841 };
842 
nmk_pmx_get_funcs_cnt(struct pinctrl_dev * pctldev)843 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
844 {
845 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
846 
847 	return npct->soc->nfunctions;
848 }
849 
nmk_pmx_get_func_name(struct pinctrl_dev * pctldev,unsigned int function)850 static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
851 					 unsigned int function)
852 {
853 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
854 
855 	return npct->soc->functions[function].name;
856 }
857 
nmk_pmx_get_func_groups(struct pinctrl_dev * pctldev,unsigned int function,const char * const ** groups,unsigned * const num_groups)858 static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
859 				   unsigned int function,
860 				   const char * const **groups,
861 				   unsigned * const num_groups)
862 {
863 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
864 
865 	*groups = npct->soc->functions[function].groups;
866 	*num_groups = npct->soc->functions[function].ngroups;
867 
868 	return 0;
869 }
870 
nmk_pmx_set(struct pinctrl_dev * pctldev,unsigned int function,unsigned int group)871 static int nmk_pmx_set(struct pinctrl_dev *pctldev, unsigned int function,
872 		       unsigned int group)
873 {
874 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
875 	const struct nmk_pingroup *g;
876 	static unsigned int slpm[NMK_MAX_BANKS];
877 	unsigned long flags = 0;
878 	bool glitch;
879 	int ret = -EINVAL;
880 	int i;
881 
882 	g = &npct->soc->groups[group];
883 
884 	if (g->altsetting < 0)
885 		return -EINVAL;
886 
887 	dev_dbg(npct->dev, "enable group %s, %zu pins\n", g->grp.name, g->grp.npins);
888 
889 	/*
890 	 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
891 	 * we may pass through an undesired state. In this case we take
892 	 * some extra care.
893 	 *
894 	 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
895 	 *  - Save SLPM registers (since we have a shadow register in the
896 	 *    nmk_chip we're using that as backup)
897 	 *  - Set SLPM=0 for the IOs you want to switch and others to 1
898 	 *  - Configure the GPIO registers for the IOs that are being switched
899 	 *  - Set IOFORCE=1
900 	 *  - Modify the AFLSA/B registers for the IOs that are being switched
901 	 *  - Set IOFORCE=0
902 	 *  - Restore SLPM registers
903 	 *  - Any spurious wake up event during switch sequence to be ignored
904 	 *    and cleared
905 	 *
906 	 * We REALLY need to save ALL slpm registers, because the external
907 	 * IOFORCE will switch *all* ports to their sleepmode setting to as
908 	 * to avoid glitches. (Not just one port!)
909 	 */
910 	glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
911 
912 	if (glitch) {
913 		spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
914 
915 		/* Initially don't put any pins to sleep when switching */
916 		memset(slpm, 0xff, sizeof(slpm));
917 
918 		/*
919 		 * Then mask the pins that need to be sleeping now when we're
920 		 * switching to the ALT C function.
921 		 */
922 		for (i = 0; i < g->grp.npins; i++) {
923 			struct nmk_gpio_chip *nmk_chip;
924 			unsigned int bit;
925 
926 			nmk_chip = find_nmk_gpio_from_pin(g->grp.pins[i], &bit);
927 			if (!nmk_chip) {
928 				dev_err(npct->dev,
929 					"invalid pin offset %d in group %s at index %d\n",
930 					g->grp.pins[i], g->grp.name, i);
931 				goto out_pre_slpm_init;
932 			}
933 
934 			slpm[nmk_chip->bank] &= ~BIT(bit);
935 		}
936 		ret = nmk_gpio_glitch_slpm_init(slpm);
937 		if (ret)
938 			goto out_pre_slpm_init;
939 	}
940 
941 	for (i = 0; i < g->grp.npins; i++) {
942 		struct nmk_gpio_chip *nmk_chip;
943 		unsigned int bit;
944 
945 		nmk_chip = find_nmk_gpio_from_pin(g->grp.pins[i], &bit);
946 		if (!nmk_chip) {
947 			dev_err(npct->dev,
948 				"invalid pin offset %d in group %s at index %d\n",
949 				g->grp.pins[i], g->grp.name, i);
950 			goto out_glitch;
951 		}
952 		dev_dbg(npct->dev, "setting pin %d to altsetting %d\n",
953 			g->grp.pins[i], g->altsetting);
954 
955 		ret = clk_enable(nmk_chip->clk);
956 		if (ret)
957 			goto out_glitch;
958 
959 		/*
960 		 * If the pin is switching to altfunc, and there was an
961 		 * interrupt installed on it which has been lazy disabled,
962 		 * actually mask the interrupt to prevent spurious interrupts
963 		 * that would occur while the pin is under control of the
964 		 * peripheral. Only SKE does this.
965 		 */
966 		nmk_gpio_disable_lazy_irq(nmk_chip, bit);
967 
968 		__nmk_gpio_set_mode_safe(nmk_chip, bit,
969 					 (g->altsetting & NMK_GPIO_ALT_C), glitch);
970 		clk_disable(nmk_chip->clk);
971 
972 		/*
973 		 * Call PRCM GPIOCR config function in case ALTC
974 		 * has been selected:
975 		 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
976 		 *   must be set.
977 		 * - If selection is pure ALTC and previous selection was ALTCx,
978 		 *   then some bits in PRCM GPIOCR registers must be cleared.
979 		 */
980 		if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
981 			nmk_prcm_altcx_set_mode(npct, g->grp.pins[i],
982 						g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
983 	}
984 
985 	/* When all pins are successfully reconfigured we get here */
986 	ret = 0;
987 
988 out_glitch:
989 	if (glitch)
990 		nmk_gpio_glitch_slpm_restore(slpm);
991 out_pre_slpm_init:
992 	if (glitch)
993 		spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
994 
995 	return ret;
996 }
997 
nmk_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)998 static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
999 				   struct pinctrl_gpio_range *range,
1000 				   unsigned int pin)
1001 {
1002 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1003 	struct nmk_gpio_chip *nmk_chip;
1004 	struct gpio_chip *chip;
1005 	unsigned int bit;
1006 	int ret;
1007 
1008 	if (!range) {
1009 		dev_err(npct->dev, "invalid range\n");
1010 		return -EINVAL;
1011 	}
1012 	if (!range->gc) {
1013 		dev_err(npct->dev, "missing GPIO chip in range\n");
1014 		return -EINVAL;
1015 	}
1016 	chip = range->gc;
1017 	nmk_chip = gpiochip_get_data(chip);
1018 
1019 	dev_dbg(npct->dev, "enable pin %u as GPIO\n", pin);
1020 
1021 	find_nmk_gpio_from_pin(pin, &bit);
1022 
1023 	ret = clk_enable(nmk_chip->clk);
1024 	if (ret)
1025 		return ret;
1026 	/* There is no glitch when converting any pin to GPIO */
1027 	__nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1028 	clk_disable(nmk_chip->clk);
1029 
1030 	return 0;
1031 }
1032 
nmk_gpio_disable_free(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)1033 static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1034 				  struct pinctrl_gpio_range *range,
1035 				  unsigned int pin)
1036 {
1037 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1038 
1039 	dev_dbg(npct->dev, "disable pin %u as GPIO\n", pin);
1040 	/* Set the pin to some default state, GPIO is usually default */
1041 }
1042 
1043 static const struct pinmux_ops nmk_pinmux_ops = {
1044 	.get_functions_count = nmk_pmx_get_funcs_cnt,
1045 	.get_function_name = nmk_pmx_get_func_name,
1046 	.get_function_groups = nmk_pmx_get_func_groups,
1047 	.set_mux = nmk_pmx_set,
1048 	.gpio_request_enable = nmk_gpio_request_enable,
1049 	.gpio_disable_free = nmk_gpio_disable_free,
1050 	.strict = true,
1051 };
1052 
nmk_pin_config_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)1053 static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
1054 			      unsigned long *config)
1055 {
1056 	/* Not implemented */
1057 	return -EINVAL;
1058 }
1059 
nmk_pin_config_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)1060 static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
1061 			      unsigned long *configs, unsigned int num_configs)
1062 {
1063 	static const char * const pullnames[] = {
1064 		[NMK_GPIO_PULL_NONE]	= "none",
1065 		[NMK_GPIO_PULL_UP]	= "up",
1066 		[NMK_GPIO_PULL_DOWN]	= "down",
1067 		[3] /* illegal */	= "??"
1068 	};
1069 	static const char * const slpmnames[] = {
1070 		[NMK_GPIO_SLPM_INPUT]		= "input/wakeup",
1071 		[NMK_GPIO_SLPM_NOCHANGE]	= "no-change/no-wakeup",
1072 	};
1073 	struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1074 	struct nmk_gpio_chip *nmk_chip;
1075 	unsigned int bit;
1076 	unsigned long cfg;
1077 	int pull, slpm, output, val, i;
1078 	bool lowemi, gpiomode, sleep;
1079 	int ret;
1080 
1081 	nmk_chip = find_nmk_gpio_from_pin(pin, &bit);
1082 	if (!nmk_chip) {
1083 		dev_err(npct->dev,
1084 			"invalid pin offset %d\n", pin);
1085 		return -EINVAL;
1086 	}
1087 
1088 	for (i = 0; i < num_configs; i++) {
1089 		/*
1090 		 * The pin config contains pin number and altfunction fields,
1091 		 * here we just ignore that part. It's being handled by the
1092 		 * framework and pinmux callback respectively.
1093 		 */
1094 		cfg = configs[i];
1095 		pull = PIN_PULL(cfg);
1096 		slpm = PIN_SLPM(cfg);
1097 		output = PIN_DIR(cfg);
1098 		val = PIN_VAL(cfg);
1099 		lowemi = PIN_LOWEMI(cfg);
1100 		gpiomode = PIN_GPIOMODE(cfg);
1101 		sleep = PIN_SLEEPMODE(cfg);
1102 
1103 		if (sleep) {
1104 			int slpm_pull = PIN_SLPM_PULL(cfg);
1105 			int slpm_output = PIN_SLPM_DIR(cfg);
1106 			int slpm_val = PIN_SLPM_VAL(cfg);
1107 
1108 			/* All pins go into GPIO mode at sleep */
1109 			gpiomode = true;
1110 
1111 			/*
1112 			 * The SLPM_* values are normal values + 1 to allow zero
1113 			 * to mean "same as normal".
1114 			 */
1115 			if (slpm_pull)
1116 				pull = slpm_pull - 1;
1117 			if (slpm_output)
1118 				output = slpm_output - 1;
1119 			if (slpm_val)
1120 				val = slpm_val - 1;
1121 
1122 			dev_dbg(nmk_chip->chip.parent,
1123 				"pin %d: sleep pull %s, dir %s, val %s\n",
1124 				pin,
1125 				slpm_pull ? pullnames[pull] : "same",
1126 				slpm_output ? (output ? "output" : "input")
1127 				: "same",
1128 				slpm_val ? (val ? "high" : "low") : "same");
1129 		}
1130 
1131 		dev_dbg(nmk_chip->chip.parent,
1132 			"pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1133 			pin, cfg, pullnames[pull], slpmnames[slpm],
1134 			output ? "output " : "input",
1135 			output ? (val ? "high" : "low") : "",
1136 			lowemi ? "on" : "off");
1137 
1138 		ret = clk_enable(nmk_chip->clk);
1139 		if (ret)
1140 			return ret;
1141 		if (gpiomode)
1142 			/* No glitch when going to GPIO mode */
1143 			__nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1144 		if (output) {
1145 			__nmk_gpio_make_output(nmk_chip, bit, val);
1146 		} else {
1147 			__nmk_gpio_make_input(nmk_chip, bit);
1148 			__nmk_gpio_set_pull(nmk_chip, bit, pull);
1149 		}
1150 		/* TODO: isn't this only applicable on output pins? */
1151 		__nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1152 
1153 		__nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1154 		clk_disable(nmk_chip->clk);
1155 	} /* for each config */
1156 
1157 	return 0;
1158 }
1159 
1160 static const struct pinconf_ops nmk_pinconf_ops = {
1161 	.pin_config_get = nmk_pin_config_get,
1162 	.pin_config_set = nmk_pin_config_set,
1163 };
1164 
1165 static struct pinctrl_desc nmk_pinctrl_desc = {
1166 	.name = "pinctrl-nomadik",
1167 	.pctlops = &nmk_pinctrl_ops,
1168 	.pmxops = &nmk_pinmux_ops,
1169 	.confops = &nmk_pinconf_ops,
1170 	.owner = THIS_MODULE,
1171 };
1172 
1173 static const struct of_device_id nmk_pinctrl_match[] = {
1174 	{
1175 		.compatible = "stericsson,stn8815-pinctrl",
1176 		.data = (void *)PINCTRL_NMK_STN8815,
1177 	},
1178 	{
1179 		.compatible = "stericsson,db8500-pinctrl",
1180 		.data = (void *)PINCTRL_NMK_DB8500,
1181 	},
1182 	{},
1183 };
1184 
1185 #ifdef CONFIG_PM_SLEEP
nmk_pinctrl_suspend(struct device * dev)1186 static int nmk_pinctrl_suspend(struct device *dev)
1187 {
1188 	struct nmk_pinctrl *npct;
1189 
1190 	npct = dev_get_drvdata(dev);
1191 	if (!npct)
1192 		return -EINVAL;
1193 
1194 	return pinctrl_force_sleep(npct->pctl);
1195 }
1196 
nmk_pinctrl_resume(struct device * dev)1197 static int nmk_pinctrl_resume(struct device *dev)
1198 {
1199 	struct nmk_pinctrl *npct;
1200 
1201 	npct = dev_get_drvdata(dev);
1202 	if (!npct)
1203 		return -EINVAL;
1204 
1205 	return pinctrl_force_default(npct->pctl);
1206 }
1207 #endif
1208 
nmk_pinctrl_probe(struct platform_device * pdev)1209 static int nmk_pinctrl_probe(struct platform_device *pdev)
1210 {
1211 	struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
1212 	struct fwnode_handle *prcm_fwnode;
1213 	struct nmk_pinctrl *npct;
1214 	uintptr_t version = 0;
1215 	int i;
1216 
1217 	npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1218 	if (!npct)
1219 		return -ENOMEM;
1220 
1221 	version = (uintptr_t)device_get_match_data(&pdev->dev);
1222 
1223 	/* Poke in other ASIC variants here */
1224 	if (version == PINCTRL_NMK_STN8815)
1225 		nmk_pinctrl_stn8815_init(&npct->soc);
1226 	if (version == PINCTRL_NMK_DB8500)
1227 		nmk_pinctrl_db8500_init(&npct->soc);
1228 
1229 	/*
1230 	 * Since we depend on the GPIO chips to provide clock and register base
1231 	 * for the pin control operations, make sure that we have these
1232 	 * populated before we continue. Follow the phandles to instantiate
1233 	 * them. The GPIO portion of the actual hardware may be probed before
1234 	 * or after this point: it shouldn't matter as the APIs are orthogonal.
1235 	 */
1236 	for (i = 0; i < NMK_MAX_BANKS; i++) {
1237 		struct fwnode_handle *gpio_fwnode;
1238 		struct nmk_gpio_chip *nmk_chip;
1239 
1240 		gpio_fwnode = fwnode_find_reference(fwnode, "nomadik-gpio-chips", i);
1241 		if (IS_ERR(gpio_fwnode))
1242 			continue;
1243 
1244 		dev_info(&pdev->dev, "populate NMK GPIO %d \"%pfwP\"\n", i, gpio_fwnode);
1245 		nmk_chip = nmk_gpio_populate_chip(gpio_fwnode, pdev);
1246 		if (IS_ERR(nmk_chip))
1247 			dev_err(&pdev->dev,
1248 				"could not populate nmk chip struct - continue anyway\n");
1249 		else
1250 			/* We are NOT compatible with mobileye,eyeq5-gpio. */
1251 			BUG_ON(nmk_chip->is_mobileye_soc);
1252 		fwnode_handle_put(gpio_fwnode);
1253 	}
1254 
1255 	prcm_fwnode = fwnode_find_reference(fwnode, "prcm", 0);
1256 	if (!IS_ERR(prcm_fwnode)) {
1257 		npct->prcm_base = fwnode_iomap(prcm_fwnode, 0);
1258 		fwnode_handle_put(prcm_fwnode);
1259 	}
1260 	if (!npct->prcm_base) {
1261 		if (version == PINCTRL_NMK_STN8815) {
1262 			dev_info(&pdev->dev,
1263 				 "No PRCM base, assuming no ALT-Cx control is available\n");
1264 		} else {
1265 			dev_err(&pdev->dev, "missing PRCM base address\n");
1266 			return -EINVAL;
1267 		}
1268 	}
1269 
1270 	nmk_pinctrl_desc.pins = npct->soc->pins;
1271 	nmk_pinctrl_desc.npins = npct->soc->npins;
1272 	npct->dev = &pdev->dev;
1273 
1274 	npct->pctl = devm_pinctrl_register(&pdev->dev, &nmk_pinctrl_desc, npct);
1275 	if (IS_ERR(npct->pctl)) {
1276 		dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1277 		return PTR_ERR(npct->pctl);
1278 	}
1279 
1280 	platform_set_drvdata(pdev, npct);
1281 	dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1282 
1283 	return 0;
1284 }
1285 
1286 static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops,
1287 			nmk_pinctrl_suspend,
1288 			nmk_pinctrl_resume);
1289 
1290 static struct platform_driver nmk_pinctrl_driver = {
1291 	.driver = {
1292 		.name = "pinctrl-nomadik",
1293 		.of_match_table = nmk_pinctrl_match,
1294 		.pm = &nmk_pinctrl_pm_ops,
1295 	},
1296 	.probe = nmk_pinctrl_probe,
1297 };
1298 
nmk_pinctrl_init(void)1299 static int __init nmk_pinctrl_init(void)
1300 {
1301 	return platform_driver_register(&nmk_pinctrl_driver);
1302 }
1303 core_initcall(nmk_pinctrl_init);
1304