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