Drivers: pinctrl: remove __dev* attributes.
[deliverable/linux.git] / drivers / pinctrl / pinctrl-nomadik.c
CommitLineData
2ec1d359
AR
1/*
2 * Generic GPIO driver for logic cells found in the Nomadik SoC
3 *
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
33d78647 7 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
2ec1d359
AR
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
3e3c62ca 17#include <linux/platform_device.h>
2ec1d359 18#include <linux/io.h>
af7dc228
RV
19#include <linux/clk.h>
20#include <linux/err.h>
2ec1d359
AR
21#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
a60b57ed 25#include <linux/irqdomain.h>
5a0e3ad6 26#include <linux/slab.h>
855f80cd 27#include <linux/of_device.h>
e98ea774 28#include <linux/pinctrl/pinctrl.h>
dbfe8ca2 29#include <linux/pinctrl/pinmux.h>
d41af627 30#include <linux/pinctrl/pinconf.h>
dbfe8ca2
LW
31/* Since we request GPIOs from ourself */
32#include <linux/pinctrl/consumer.h>
bb16bd9b 33#include <linux/platform_data/pinctrl-nomadik.h>
adfed159 34#include <asm/mach/irq.h>
c3b9d1db 35#include <mach/irqs.h>
e98ea774
LW
36#include "pinctrl-nomadik.h"
37
2ec1d359
AR
38/*
39 * The GPIO module in the Nomadik family of Systems-on-Chip is an
40 * AMBA device, managing 32 pins and alternate functions. The logic block
9c66ee6f 41 * is currently used in the Nomadik and ux500.
2ec1d359
AR
42 *
43 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
44 */
45
2ec1d359
AR
46struct nmk_gpio_chip {
47 struct gpio_chip chip;
a60b57ed 48 struct irq_domain *domain;
2ec1d359 49 void __iomem *addr;
af7dc228 50 struct clk *clk;
33b744b3 51 unsigned int bank;
2ec1d359 52 unsigned int parent_irq;
2c8bb0eb 53 int secondary_parent_irq;
33b744b3 54 u32 (*get_secondary_status)(unsigned int bank);
01727e61 55 void (*set_ioforce)(bool enable);
c0fcb8db 56 spinlock_t lock;
33d78647 57 bool sleepmode;
2ec1d359
AR
58 /* Keep track of configured edges */
59 u32 edge_rising;
60 u32 edge_falling;
b9df468d
RV
61 u32 real_wake;
62 u32 rwimsc;
63 u32 fwimsc;
6c12fe88
RV
64 u32 rimsc;
65 u32 fimsc;
bc6f5cf6 66 u32 pull_up;
ebc6178d 67 u32 lowemi;
2ec1d359
AR
68};
69
f1671bf5
JA
70/**
71 * struct nmk_pinctrl - state container for the Nomadik pin controller
72 * @dev: containing device pointer
73 * @pctl: corresponding pin controller device
74 * @soc: SoC data for this specific chip
75 * @prcm_base: PRCM register range virtual base
76 */
e98ea774
LW
77struct nmk_pinctrl {
78 struct device *dev;
79 struct pinctrl_dev *pctl;
80 const struct nmk_pinctrl_soc_data *soc;
f1671bf5 81 void __iomem *prcm_base;
e98ea774
LW
82};
83
01727e61
RV
84static struct nmk_gpio_chip *
85nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
86
87static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
88
89#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
90
6f9a974c
RV
91static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
92 unsigned offset, int gpio_mode)
93{
94 u32 bit = 1 << offset;
95 u32 afunc, bfunc;
96
97 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
98 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
99 if (gpio_mode & NMK_GPIO_ALT_A)
100 afunc |= bit;
101 if (gpio_mode & NMK_GPIO_ALT_B)
102 bfunc |= bit;
103 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
104 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
105}
106
81a3c298
RV
107static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
108 unsigned offset, enum nmk_gpio_slpm mode)
109{
110 u32 bit = 1 << offset;
111 u32 slpm;
112
113 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
114 if (mode == NMK_GPIO_SLPM_NOCHANGE)
115 slpm |= bit;
116 else
117 slpm &= ~bit;
118 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
119}
120
5b327edf
RV
121static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
122 unsigned offset, enum nmk_gpio_pull pull)
123{
124 u32 bit = 1 << offset;
125 u32 pdis;
126
127 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
bc6f5cf6 128 if (pull == NMK_GPIO_PULL_NONE) {
5b327edf 129 pdis |= bit;
bc6f5cf6
RA
130 nmk_chip->pull_up &= ~bit;
131 } else {
5b327edf 132 pdis &= ~bit;
bc6f5cf6
RA
133 }
134
5b327edf
RV
135 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
136
bc6f5cf6
RA
137 if (pull == NMK_GPIO_PULL_UP) {
138 nmk_chip->pull_up |= bit;
5b327edf 139 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
bc6f5cf6
RA
140 } else if (pull == NMK_GPIO_PULL_DOWN) {
141 nmk_chip->pull_up &= ~bit;
5b327edf 142 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
bc6f5cf6 143 }
5b327edf
RV
144}
145
ebc6178d
RV
146static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
147 unsigned offset, bool lowemi)
148{
149 u32 bit = BIT(offset);
150 bool enabled = nmk_chip->lowemi & bit;
151
152 if (lowemi == enabled)
153 return;
154
155 if (lowemi)
156 nmk_chip->lowemi |= bit;
157 else
158 nmk_chip->lowemi &= ~bit;
159
160 writel_relaxed(nmk_chip->lowemi,
161 nmk_chip->addr + NMK_GPIO_LOWEMI);
162}
163
378be066
RV
164static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
165 unsigned offset)
166{
167 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
168}
169
6720db7c
RV
170static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
171 unsigned offset, int val)
172{
173 if (val)
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
175 else
176 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
177}
178
179static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
180 unsigned offset, int val)
181{
182 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
183 __nmk_gpio_set_output(nmk_chip, offset, val);
184}
185
01727e61
RV
186static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
187 unsigned offset, int gpio_mode,
188 bool glitch)
189{
6c12fe88
RV
190 u32 rwimsc = nmk_chip->rwimsc;
191 u32 fwimsc = nmk_chip->fwimsc;
01727e61
RV
192
193 if (glitch && nmk_chip->set_ioforce) {
194 u32 bit = BIT(offset);
195
01727e61
RV
196 /* Prevent spurious wakeups */
197 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
198 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
199
200 nmk_chip->set_ioforce(true);
201 }
202
203 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
204
205 if (glitch && nmk_chip->set_ioforce) {
206 nmk_chip->set_ioforce(false);
207
208 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
209 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
210 }
211}
212
6c42ad1c
RV
213static void
214nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
215{
216 u32 falling = nmk_chip->fimsc & BIT(offset);
217 u32 rising = nmk_chip->rimsc & BIT(offset);
218 int gpio = nmk_chip->chip.base + offset;
219 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
220 struct irq_data *d = irq_get_irq_data(irq);
221
222 if (!rising && !falling)
223 return;
224
225 if (!d || !irqd_irq_disabled(d))
226 return;
227
228 if (rising) {
229 nmk_chip->rimsc &= ~BIT(offset);
230 writel_relaxed(nmk_chip->rimsc,
231 nmk_chip->addr + NMK_GPIO_RIMSC);
232 }
233
234 if (falling) {
235 nmk_chip->fimsc &= ~BIT(offset);
236 writel_relaxed(nmk_chip->fimsc,
237 nmk_chip->addr + NMK_GPIO_FIMSC);
238 }
239
240 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
241}
242
f1671bf5
JA
243static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
244{
245 u32 val;
246
247 val = readl(reg);
248 val = ((val & ~mask) | (value & mask));
249 writel(val, reg);
250}
251
c22df08c
JNG
252static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
253 unsigned offset, unsigned alt_num)
254{
255 int i;
256 u16 reg;
257 u8 bit;
258 u8 alt_index;
259 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
260 const u16 *gpiocr_regs;
261
262 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
263 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
264 alt_num);
265 return;
266 }
267
268 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
269 if (npct->soc->altcx_pins[i].pin == offset)
270 break;
271 }
272 if (i == npct->soc->npins_altcx) {
273 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
274 offset);
275 return;
276 }
277
278 pin_desc = npct->soc->altcx_pins + i;
279 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
280
281 /*
282 * If alt_num is NULL, just clear current ALTCx selection
283 * to make sure we come back to a pure ALTC selection
284 */
285 if (!alt_num) {
286 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
287 if (pin_desc->altcx[i].used == true) {
288 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
289 bit = pin_desc->altcx[i].control_bit;
f1671bf5
JA
290 if (readl(npct->prcm_base + reg) & BIT(bit)) {
291 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
c22df08c
JNG
292 dev_dbg(npct->dev,
293 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
294 offset, i+1);
295 }
296 }
297 }
298 return;
299 }
300
301 alt_index = alt_num - 1;
302 if (pin_desc->altcx[alt_index].used == false) {
303 dev_warn(npct->dev,
304 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
305 offset, alt_num);
306 return;
307 }
308
309 /*
310 * Check if any other ALTCx functions are activated on this pin
311 * and disable it first.
312 */
313 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
314 if (i == alt_index)
315 continue;
316 if (pin_desc->altcx[i].used == true) {
317 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
318 bit = pin_desc->altcx[i].control_bit;
f1671bf5
JA
319 if (readl(npct->prcm_base + reg) & BIT(bit)) {
320 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
c22df08c
JNG
321 dev_dbg(npct->dev,
322 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
323 offset, i+1);
324 }
325 }
326 }
327
328 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
329 bit = pin_desc->altcx[alt_index].control_bit;
330 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
331 offset, alt_index+1);
f1671bf5 332 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
c22df08c
JNG
333}
334
378be066 335static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
01727e61 336 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
378be066
RV
337{
338 static const char *afnames[] = {
339 [NMK_GPIO_ALT_GPIO] = "GPIO",
340 [NMK_GPIO_ALT_A] = "A",
341 [NMK_GPIO_ALT_B] = "B",
342 [NMK_GPIO_ALT_C] = "C"
343 };
344 static const char *pullnames[] = {
345 [NMK_GPIO_PULL_NONE] = "none",
346 [NMK_GPIO_PULL_UP] = "up",
347 [NMK_GPIO_PULL_DOWN] = "down",
348 [3] /* illegal */ = "??"
349 };
350 static const char *slpmnames[] = {
7e3f7e59
RV
351 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
352 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
378be066
RV
353 };
354
355 int pin = PIN_NUM(cfg);
356 int pull = PIN_PULL(cfg);
357 int af = PIN_ALT(cfg);
358 int slpm = PIN_SLPM(cfg);
6720db7c
RV
359 int output = PIN_DIR(cfg);
360 int val = PIN_VAL(cfg);
01727e61 361 bool glitch = af == NMK_GPIO_ALT_C;
378be066 362
dacdc96c
RV
363 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
364 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
6720db7c
RV
365 output ? "output " : "input",
366 output ? (val ? "high" : "low") : "");
367
dacdc96c
RV
368 if (sleep) {
369 int slpm_pull = PIN_SLPM_PULL(cfg);
370 int slpm_output = PIN_SLPM_DIR(cfg);
371 int slpm_val = PIN_SLPM_VAL(cfg);
372
3546d15c
RV
373 af = NMK_GPIO_ALT_GPIO;
374
dacdc96c
RV
375 /*
376 * The SLPM_* values are normal values + 1 to allow zero to
377 * mean "same as normal".
378 */
379 if (slpm_pull)
380 pull = slpm_pull - 1;
381 if (slpm_output)
382 output = slpm_output - 1;
383 if (slpm_val)
384 val = slpm_val - 1;
385
386 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
387 pin,
388 slpm_pull ? pullnames[pull] : "same",
389 slpm_output ? (output ? "output" : "input") : "same",
390 slpm_val ? (val ? "high" : "low") : "same");
391 }
392
6720db7c
RV
393 if (output)
394 __nmk_gpio_make_output(nmk_chip, offset, val);
395 else {
396 __nmk_gpio_make_input(nmk_chip, offset);
397 __nmk_gpio_set_pull(nmk_chip, offset, pull);
398 }
378be066 399
ebc6178d
RV
400 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
401
6c42ad1c
RV
402 /*
403 * If the pin is switching to altfunc, and there was an interrupt
404 * installed on it which has been lazy disabled, actually mask the
405 * interrupt to prevent spurious interrupts that would occur while the
406 * pin is under control of the peripheral. Only SKE does this.
407 */
408 if (af != NMK_GPIO_ALT_GPIO)
409 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
410
01727e61
RV
411 /*
412 * If we've backed up the SLPM registers (glitch workaround), modify
413 * the backups since they will be restored.
414 */
415 if (slpmregs) {
416 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
417 slpmregs[nmk_chip->bank] |= BIT(offset);
418 else
419 slpmregs[nmk_chip->bank] &= ~BIT(offset);
420 } else
421 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
422
423 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
424}
425
426/*
427 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
428 * - Save SLPM registers
429 * - Set SLPM=0 for the IOs you want to switch and others to 1
430 * - Configure the GPIO registers for the IOs that are being switched
431 * - Set IOFORCE=1
432 * - Modify the AFLSA/B registers for the IOs that are being switched
433 * - Set IOFORCE=0
434 * - Restore SLPM registers
435 * - Any spurious wake up event during switch sequence to be ignored and
436 * cleared
437 */
438static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
439{
440 int i;
441
442 for (i = 0; i < NUM_BANKS; i++) {
443 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
444 unsigned int temp = slpm[i];
445
446 if (!chip)
447 break;
448
3c0227d2
RV
449 clk_enable(chip->clk);
450
01727e61
RV
451 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
452 writel(temp, chip->addr + NMK_GPIO_SLPC);
453 }
454}
455
456static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
457{
458 int i;
459
460 for (i = 0; i < NUM_BANKS; i++) {
461 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
462
463 if (!chip)
464 break;
465
466 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
3c0227d2
RV
467
468 clk_disable(chip->clk);
01727e61
RV
469 }
470}
471
472static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
473{
474 static unsigned int slpm[NUM_BANKS];
475 unsigned long flags;
476 bool glitch = false;
477 int ret = 0;
478 int i;
479
480 for (i = 0; i < num; i++) {
481 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
482 glitch = true;
483 break;
484 }
485 }
486
487 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
488
489 if (glitch) {
490 memset(slpm, 0xff, sizeof(slpm));
491
492 for (i = 0; i < num; i++) {
493 int pin = PIN_NUM(cfgs[i]);
494 int offset = pin % NMK_GPIO_PER_CHIP;
495
496 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
497 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
498 }
499
500 nmk_gpio_glitch_slpm_init(slpm);
501 }
502
503 for (i = 0; i < num; i++) {
504 struct nmk_gpio_chip *nmk_chip;
505 int pin = PIN_NUM(cfgs[i]);
506
a60b57ed 507 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
01727e61
RV
508 if (!nmk_chip) {
509 ret = -EINVAL;
510 break;
511 }
512
3c0227d2 513 clk_enable(nmk_chip->clk);
01727e61 514 spin_lock(&nmk_chip->lock);
a60b57ed 515 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
01727e61
RV
516 cfgs[i], sleep, glitch ? slpm : NULL);
517 spin_unlock(&nmk_chip->lock);
3c0227d2 518 clk_disable(nmk_chip->clk);
01727e61
RV
519 }
520
521 if (glitch)
522 nmk_gpio_glitch_slpm_restore(slpm);
523
524 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
525
526 return ret;
378be066
RV
527}
528
529/**
530 * nmk_config_pin - configure a pin's mux attributes
531 * @cfg: pin confguration
50bcd47c 532 * @sleep: Non-zero to apply the sleep mode configuration
378be066
RV
533 * Configures a pin's mode (alternate function or GPIO), its pull up status,
534 * and its sleep mode based on the specified configuration. The @cfg is
535 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
536 * are constructed using, and can be further enhanced with, the macros in
287f121c 537 * <linux/platform_data/pinctrl-nomadik.h>
378be066
RV
538 *
539 * If a pin's mode is set to GPIO, it is configured as an input to avoid
540 * side-effects. The gpio can be manipulated later using standard GPIO API
541 * calls.
542 */
dacdc96c 543int nmk_config_pin(pin_cfg_t cfg, bool sleep)
378be066 544{
01727e61 545 return __nmk_config_pins(&cfg, 1, sleep);
378be066
RV
546}
547EXPORT_SYMBOL(nmk_config_pin);
548
549/**
550 * nmk_config_pins - configure several pins at once
551 * @cfgs: array of pin configurations
552 * @num: number of elments in the array
553 *
554 * Configures several pins using nmk_config_pin(). Refer to that function for
555 * further information.
556 */
557int nmk_config_pins(pin_cfg_t *cfgs, int num)
558{
01727e61 559 return __nmk_config_pins(cfgs, num, false);
378be066
RV
560}
561EXPORT_SYMBOL(nmk_config_pins);
562
dacdc96c
RV
563int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
564{
01727e61 565 return __nmk_config_pins(cfgs, num, true);
dacdc96c
RV
566}
567EXPORT_SYMBOL(nmk_config_pins_sleep);
568
81a3c298
RV
569/**
570 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
571 * @gpio: pin number
572 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
573 *
33d78647
LW
574 * This register is actually in the pinmux layer, not the GPIO block itself.
575 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
576 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
577 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
578 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
579 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
580 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
7e3f7e59 581 *
33d78647
LW
582 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
583 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
584 * entered) regardless of the altfunction selected. Also wake-up detection is
585 * ENABLED.
586 *
587 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
588 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
589 * (for altfunction GPIO) or respective on-chip peripherals (for other
590 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
591 *
592 * Note that enable_irq_wake() will automatically enable wakeup detection.
81a3c298
RV
593 */
594int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
595{
596 struct nmk_gpio_chip *nmk_chip;
597 unsigned long flags;
598
a60b57ed 599 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
81a3c298
RV
600 if (!nmk_chip)
601 return -EINVAL;
602
3c0227d2 603 clk_enable(nmk_chip->clk);
01727e61
RV
604 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
605 spin_lock(&nmk_chip->lock);
606
a60b57ed 607 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
01727e61
RV
608
609 spin_unlock(&nmk_chip->lock);
610 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 611 clk_disable(nmk_chip->clk);
81a3c298
RV
612
613 return 0;
614}
615
5b327edf
RV
616/**
617 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
618 * @gpio: pin number
619 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
620 *
621 * Enables/disables pull up/down on a specified pin. This only takes effect if
622 * the pin is configured as an input (either explicitly or by the alternate
623 * function).
624 *
625 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
626 * configured as an input. Otherwise, due to the way the controller registers
627 * work, this function will change the value output on the pin.
628 */
629int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
630{
631 struct nmk_gpio_chip *nmk_chip;
632 unsigned long flags;
633
a60b57ed 634 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
5b327edf
RV
635 if (!nmk_chip)
636 return -EINVAL;
637
3c0227d2 638 clk_enable(nmk_chip->clk);
5b327edf 639 spin_lock_irqsave(&nmk_chip->lock, flags);
a60b57ed 640 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
5b327edf 641 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 642 clk_disable(nmk_chip->clk);
5b327edf
RV
643
644 return 0;
645}
646
2ec1d359 647/* Mode functions */
9c66ee6f
JA
648/**
649 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
650 * @gpio: pin number
651 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
652 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
653 *
654 * Sets the mode of the specified pin to one of the alternate functions or
655 * plain GPIO.
656 */
2ec1d359
AR
657int nmk_gpio_set_mode(int gpio, int gpio_mode)
658{
659 struct nmk_gpio_chip *nmk_chip;
660 unsigned long flags;
2ec1d359 661
a60b57ed 662 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
2ec1d359
AR
663 if (!nmk_chip)
664 return -EINVAL;
665
3c0227d2 666 clk_enable(nmk_chip->clk);
2ec1d359 667 spin_lock_irqsave(&nmk_chip->lock, flags);
a60b57ed 668 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
2ec1d359 669 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 670 clk_disable(nmk_chip->clk);
2ec1d359
AR
671
672 return 0;
673}
674EXPORT_SYMBOL(nmk_gpio_set_mode);
675
2249b19f
JNG
676static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
677{
678 int i;
679 u16 reg;
680 u8 bit;
681 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
682 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
683 const u16 *gpiocr_regs;
684
685 for (i = 0; i < npct->soc->npins_altcx; i++) {
686 if (npct->soc->altcx_pins[i].pin == gpio)
687 break;
688 }
689 if (i == npct->soc->npins_altcx)
690 return NMK_GPIO_ALT_C;
691
692 pin_desc = npct->soc->altcx_pins + i;
693 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
694 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
695 if (pin_desc->altcx[i].used == true) {
696 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
697 bit = pin_desc->altcx[i].control_bit;
f1671bf5 698 if (readl(npct->prcm_base + reg) & BIT(bit))
2249b19f
JNG
699 return NMK_GPIO_ALT_C+i+1;
700 }
701 }
702 return NMK_GPIO_ALT_C;
703}
704
2ec1d359
AR
705int nmk_gpio_get_mode(int gpio)
706{
707 struct nmk_gpio_chip *nmk_chip;
708 u32 afunc, bfunc, bit;
709
a60b57ed 710 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
2ec1d359
AR
711 if (!nmk_chip)
712 return -EINVAL;
713
a60b57ed 714 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
2ec1d359 715
3c0227d2
RV
716 clk_enable(nmk_chip->clk);
717
2ec1d359
AR
718 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
719 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
720
3c0227d2
RV
721 clk_disable(nmk_chip->clk);
722
2ec1d359
AR
723 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
724}
725EXPORT_SYMBOL(nmk_gpio_get_mode);
726
727
728/* IRQ functions */
729static inline int nmk_gpio_get_bitmask(int gpio)
730{
a60b57ed 731 return 1 << (gpio % NMK_GPIO_PER_CHIP);
2ec1d359
AR
732}
733
f272c00e 734static void nmk_gpio_irq_ack(struct irq_data *d)
2ec1d359 735{
2ec1d359
AR
736 struct nmk_gpio_chip *nmk_chip;
737
f272c00e 738 nmk_chip = irq_data_get_irq_chip_data(d);
2ec1d359
AR
739 if (!nmk_chip)
740 return;
3c0227d2
RV
741
742 clk_enable(nmk_chip->clk);
a60b57ed 743 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
3c0227d2 744 clk_disable(nmk_chip->clk);
2ec1d359
AR
745}
746
4d4e20f7
RV
747enum nmk_gpio_irq_type {
748 NORMAL,
749 WAKE,
750};
751
040e5ecd 752static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
4d4e20f7
RV
753 int gpio, enum nmk_gpio_irq_type which,
754 bool enable)
2ec1d359 755{
040e5ecd 756 u32 bitmask = nmk_gpio_get_bitmask(gpio);
6c12fe88
RV
757 u32 *rimscval;
758 u32 *fimscval;
759 u32 rimscreg;
760 u32 fimscreg;
761
762 if (which == NORMAL) {
763 rimscreg = NMK_GPIO_RIMSC;
764 fimscreg = NMK_GPIO_FIMSC;
765 rimscval = &nmk_chip->rimsc;
766 fimscval = &nmk_chip->fimsc;
767 } else {
768 rimscreg = NMK_GPIO_RWIMSC;
769 fimscreg = NMK_GPIO_FWIMSC;
770 rimscval = &nmk_chip->rwimsc;
771 fimscval = &nmk_chip->fwimsc;
772 }
2ec1d359 773
040e5ecd 774 /* we must individually set/clear the two edges */
2ec1d359 775 if (nmk_chip->edge_rising & bitmask) {
040e5ecd 776 if (enable)
6c12fe88 777 *rimscval |= bitmask;
040e5ecd 778 else
6c12fe88
RV
779 *rimscval &= ~bitmask;
780 writel(*rimscval, nmk_chip->addr + rimscreg);
2ec1d359
AR
781 }
782 if (nmk_chip->edge_falling & bitmask) {
040e5ecd 783 if (enable)
6c12fe88 784 *fimscval |= bitmask;
040e5ecd 785 else
6c12fe88
RV
786 *fimscval &= ~bitmask;
787 writel(*fimscval, nmk_chip->addr + fimscreg);
2ec1d359 788 }
040e5ecd 789}
2ec1d359 790
b9df468d
RV
791static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
792 int gpio, bool on)
793{
b982ff0e
RV
794 /*
795 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
796 * disabled, since setting SLPM to 1 increases power consumption, and
797 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
798 */
799 if (nmk_chip->sleepmode && on) {
e85bbc19 800 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
b982ff0e 801 NMK_GPIO_SLPM_WAKEUP_ENABLE);
33d78647
LW
802 }
803
b9df468d
RV
804 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
805}
806
807static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
2ec1d359 808{
2ec1d359
AR
809 struct nmk_gpio_chip *nmk_chip;
810 unsigned long flags;
040e5ecd 811 u32 bitmask;
2ec1d359 812
f272c00e 813 nmk_chip = irq_data_get_irq_chip_data(d);
a60b57ed 814 bitmask = nmk_gpio_get_bitmask(d->hwirq);
2ec1d359 815 if (!nmk_chip)
4d4e20f7 816 return -EINVAL;
2ec1d359 817
3c0227d2 818 clk_enable(nmk_chip->clk);
b9df468d
RV
819 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
820 spin_lock(&nmk_chip->lock);
821
a60b57ed 822 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
b9df468d
RV
823
824 if (!(nmk_chip->real_wake & bitmask))
a60b57ed 825 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
b9df468d
RV
826
827 spin_unlock(&nmk_chip->lock);
828 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 829 clk_disable(nmk_chip->clk);
4d4e20f7
RV
830
831 return 0;
2ec1d359
AR
832}
833
f272c00e 834static void nmk_gpio_irq_mask(struct irq_data *d)
040e5ecd 835{
b9df468d 836 nmk_gpio_irq_maskunmask(d, false);
4d4e20f7 837}
040e5ecd 838
f272c00e 839static void nmk_gpio_irq_unmask(struct irq_data *d)
040e5ecd 840{
b9df468d 841 nmk_gpio_irq_maskunmask(d, true);
4d4e20f7
RV
842}
843
f272c00e 844static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
4d4e20f7 845{
7e3f7e59
RV
846 struct nmk_gpio_chip *nmk_chip;
847 unsigned long flags;
b9df468d 848 u32 bitmask;
7e3f7e59 849
f272c00e 850 nmk_chip = irq_data_get_irq_chip_data(d);
7e3f7e59
RV
851 if (!nmk_chip)
852 return -EINVAL;
a60b57ed 853 bitmask = nmk_gpio_get_bitmask(d->hwirq);
7e3f7e59 854
3c0227d2 855 clk_enable(nmk_chip->clk);
01727e61
RV
856 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
857 spin_lock(&nmk_chip->lock);
858
479a0c7e 859 if (irqd_irq_disabled(d))
a60b57ed 860 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
b9df468d
RV
861
862 if (on)
863 nmk_chip->real_wake |= bitmask;
864 else
865 nmk_chip->real_wake &= ~bitmask;
01727e61
RV
866
867 spin_unlock(&nmk_chip->lock);
868 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
3c0227d2 869 clk_disable(nmk_chip->clk);
7e3f7e59
RV
870
871 return 0;
040e5ecd
RV
872}
873
f272c00e 874static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
2ec1d359 875{
479a0c7e 876 bool enabled = !irqd_irq_disabled(d);
3c0227d2 877 bool wake = irqd_is_wakeup_set(d);
2ec1d359
AR
878 struct nmk_gpio_chip *nmk_chip;
879 unsigned long flags;
880 u32 bitmask;
881
f272c00e 882 nmk_chip = irq_data_get_irq_chip_data(d);
a60b57ed 883 bitmask = nmk_gpio_get_bitmask(d->hwirq);
2ec1d359
AR
884 if (!nmk_chip)
885 return -EINVAL;
2ec1d359
AR
886 if (type & IRQ_TYPE_LEVEL_HIGH)
887 return -EINVAL;
888 if (type & IRQ_TYPE_LEVEL_LOW)
889 return -EINVAL;
890
3c0227d2 891 clk_enable(nmk_chip->clk);
2ec1d359
AR
892 spin_lock_irqsave(&nmk_chip->lock, flags);
893
7a852d80 894 if (enabled)
a60b57ed 895 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
4d4e20f7 896
b9df468d 897 if (enabled || wake)
a60b57ed 898 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
7a852d80 899
2ec1d359
AR
900 nmk_chip->edge_rising &= ~bitmask;
901 if (type & IRQ_TYPE_EDGE_RISING)
902 nmk_chip->edge_rising |= bitmask;
2ec1d359
AR
903
904 nmk_chip->edge_falling &= ~bitmask;
905 if (type & IRQ_TYPE_EDGE_FALLING)
906 nmk_chip->edge_falling |= bitmask;
2ec1d359 907
7a852d80 908 if (enabled)
a60b57ed 909 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
4d4e20f7 910
b9df468d 911 if (enabled || wake)
a60b57ed 912 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
2ec1d359 913
7a852d80 914 spin_unlock_irqrestore(&nmk_chip->lock, flags);
3c0227d2 915 clk_disable(nmk_chip->clk);
2ec1d359
AR
916
917 return 0;
918}
919
3c0227d2
RV
920static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
921{
922 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
2ec1d359 923
3c0227d2
RV
924 clk_enable(nmk_chip->clk);
925 nmk_gpio_irq_unmask(d);
2ec1d359
AR
926 return 0;
927}
928
3c0227d2
RV
929static void nmk_gpio_irq_shutdown(struct irq_data *d)
930{
931 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
932
933 nmk_gpio_irq_mask(d);
934 clk_disable(nmk_chip->clk);
935}
936
2ec1d359
AR
937static struct irq_chip nmk_gpio_irq_chip = {
938 .name = "Nomadik-GPIO",
f272c00e
LB
939 .irq_ack = nmk_gpio_irq_ack,
940 .irq_mask = nmk_gpio_irq_mask,
941 .irq_unmask = nmk_gpio_irq_unmask,
942 .irq_set_type = nmk_gpio_irq_set_type,
943 .irq_set_wake = nmk_gpio_irq_set_wake,
3c0227d2
RV
944 .irq_startup = nmk_gpio_irq_startup,
945 .irq_shutdown = nmk_gpio_irq_shutdown,
4921e745 946 .flags = IRQCHIP_MASK_ON_SUSPEND,
2ec1d359
AR
947};
948
33b744b3
RV
949static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
950 u32 status)
2ec1d359
AR
951{
952 struct nmk_gpio_chip *nmk_chip;
6845664a 953 struct irq_chip *host_chip = irq_get_chip(irq);
2ec1d359 954
adfed159 955 chained_irq_enter(host_chip, desc);
aaedaa2b 956
6845664a 957 nmk_chip = irq_get_handler_data(irq);
33b744b3
RV
958 while (status) {
959 int bit = __ffs(status);
960
95f0bc9b 961 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
33b744b3 962 status &= ~BIT(bit);
2ec1d359 963 }
aaedaa2b 964
adfed159 965 chained_irq_exit(host_chip, desc);
2ec1d359
AR
966}
967
33b744b3
RV
968static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
969{
6845664a 970 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
3c0227d2
RV
971 u32 status;
972
973 clk_enable(nmk_chip->clk);
974 status = readl(nmk_chip->addr + NMK_GPIO_IS);
975 clk_disable(nmk_chip->clk);
33b744b3
RV
976
977 __nmk_gpio_irq_handler(irq, desc, status);
978}
979
980static void nmk_gpio_secondary_irq_handler(unsigned int irq,
981 struct irq_desc *desc)
982{
6845664a 983 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
33b744b3
RV
984 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
985
986 __nmk_gpio_irq_handler(irq, desc, status);
987}
988
2ec1d359
AR
989static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
990{
6845664a
TG
991 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
992 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
33b744b3
RV
993
994 if (nmk_chip->secondary_parent_irq >= 0) {
6845664a 995 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
33b744b3 996 nmk_gpio_secondary_irq_handler);
6845664a 997 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
33b744b3
RV
998 }
999
2ec1d359
AR
1000 return 0;
1001}
1002
1003/* I/O Functions */
dbfe8ca2
LW
1004
1005static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
1006{
1007 /*
1008 * Map back to global GPIO space and request muxing, the direction
1009 * parameter does not matter for this controller.
1010 */
1011 int gpio = chip->base + offset;
1012
1013 return pinctrl_request_gpio(gpio);
1014}
1015
1016static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
1017{
1018 int gpio = chip->base + offset;
1019
1020 pinctrl_free_gpio(gpio);
1021}
1022
2ec1d359
AR
1023static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
1024{
1025 struct nmk_gpio_chip *nmk_chip =
1026 container_of(chip, struct nmk_gpio_chip, chip);
1027
3c0227d2
RV
1028 clk_enable(nmk_chip->clk);
1029
2ec1d359 1030 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
3c0227d2
RV
1031
1032 clk_disable(nmk_chip->clk);
1033
2ec1d359
AR
1034 return 0;
1035}
1036
2ec1d359
AR
1037static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1038{
1039 struct nmk_gpio_chip *nmk_chip =
1040 container_of(chip, struct nmk_gpio_chip, chip);
1041 u32 bit = 1 << offset;
3c0227d2
RV
1042 int value;
1043
1044 clk_enable(nmk_chip->clk);
2ec1d359 1045
3c0227d2 1046 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
2ec1d359 1047
3c0227d2
RV
1048 clk_disable(nmk_chip->clk);
1049
1050 return value;
2ec1d359
AR
1051}
1052
1053static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1054 int val)
1055{
1056 struct nmk_gpio_chip *nmk_chip =
1057 container_of(chip, struct nmk_gpio_chip, chip);
2ec1d359 1058
3c0227d2
RV
1059 clk_enable(nmk_chip->clk);
1060
6720db7c 1061 __nmk_gpio_set_output(nmk_chip, offset, val);
3c0227d2
RV
1062
1063 clk_disable(nmk_chip->clk);
2ec1d359
AR
1064}
1065
6647c6c0
RV
1066static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1067 int val)
1068{
1069 struct nmk_gpio_chip *nmk_chip =
1070 container_of(chip, struct nmk_gpio_chip, chip);
1071
3c0227d2
RV
1072 clk_enable(nmk_chip->clk);
1073
6720db7c 1074 __nmk_gpio_make_output(nmk_chip, offset, val);
6647c6c0 1075
3c0227d2
RV
1076 clk_disable(nmk_chip->clk);
1077
6647c6c0
RV
1078 return 0;
1079}
1080
0d2aec9c
RV
1081static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1082{
1083 struct nmk_gpio_chip *nmk_chip =
1084 container_of(chip, struct nmk_gpio_chip, chip);
1085
268300be 1086 return irq_create_mapping(nmk_chip->domain, offset);
0d2aec9c
RV
1087}
1088
d0b543c7
RV
1089#ifdef CONFIG_DEBUG_FS
1090
1091#include <linux/seq_file.h>
1092
2249b19f
JNG
1093static void nmk_gpio_dbg_show_one(struct seq_file *s,
1094 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
1095 unsigned offset, unsigned gpio)
d0b543c7 1096{
6f4350a6 1097 const char *label = gpiochip_is_requested(chip, offset);
d0b543c7
RV
1098 struct nmk_gpio_chip *nmk_chip =
1099 container_of(chip, struct nmk_gpio_chip, chip);
6f4350a6
LW
1100 int mode;
1101 bool is_out;
1102 bool pull;
1103 u32 bit = 1 << offset;
d0b543c7
RV
1104 const char *modes[] = {
1105 [NMK_GPIO_ALT_GPIO] = "gpio",
1106 [NMK_GPIO_ALT_A] = "altA",
1107 [NMK_GPIO_ALT_B] = "altB",
1108 [NMK_GPIO_ALT_C] = "altC",
2249b19f
JNG
1109 [NMK_GPIO_ALT_C+1] = "altC1",
1110 [NMK_GPIO_ALT_C+2] = "altC2",
1111 [NMK_GPIO_ALT_C+3] = "altC3",
1112 [NMK_GPIO_ALT_C+4] = "altC4",
d0b543c7
RV
1113 };
1114
3c0227d2 1115 clk_enable(nmk_chip->clk);
6f4350a6
LW
1116 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1117 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1118 mode = nmk_gpio_get_mode(gpio);
2249b19f
JNG
1119 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1120 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
6f4350a6
LW
1121
1122 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1123 gpio, label ?: "(none)",
1124 is_out ? "out" : "in ",
1125 chip->get
1126 ? (chip->get(chip, offset) ? "hi" : "lo")
1127 : "? ",
1128 (mode < 0) ? "unknown" : modes[mode],
1129 pull ? "pull" : "none");
1130
1131 if (label && !is_out) {
1132 int irq = gpio_to_irq(gpio);
1133 struct irq_desc *desc = irq_to_desc(irq);
1134
1135 /* This races with request_irq(), set_irq_type(),
1136 * and set_irq_wake() ... but those are "rare".
1137 */
1138 if (irq >= 0 && desc->action) {
1139 char *trigger;
1140 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1141
1142 if (nmk_chip->edge_rising & bitmask)
1143 trigger = "edge-rising";
1144 else if (nmk_chip->edge_falling & bitmask)
1145 trigger = "edge-falling";
1146 else
1147 trigger = "edge-undefined";
1148
1149 seq_printf(s, " irq-%d %s%s",
1150 irq, trigger,
1151 irqd_is_wakeup_set(&desc->irq_data)
1152 ? " wakeup" : "");
8ea72a30 1153 }
6f4350a6
LW
1154 }
1155 clk_disable(nmk_chip->clk);
1156}
1157
1158static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1159{
1160 unsigned i;
1161 unsigned gpio = chip->base;
8ea72a30 1162
6f4350a6 1163 for (i = 0; i < chip->ngpio; i++, gpio++) {
2249b19f 1164 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
d0b543c7
RV
1165 seq_printf(s, "\n");
1166 }
1167}
1168
1169#else
6f4350a6 1170static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
2249b19f 1171 struct pinctrl_dev *pctldev,
6f4350a6
LW
1172 struct gpio_chip *chip,
1173 unsigned offset, unsigned gpio)
1174{
1175}
d0b543c7
RV
1176#define nmk_gpio_dbg_show NULL
1177#endif
1178
2ec1d359
AR
1179/* This structure is replicated for each GPIO block allocated at probe time */
1180static struct gpio_chip nmk_gpio_template = {
dbfe8ca2
LW
1181 .request = nmk_gpio_request,
1182 .free = nmk_gpio_free,
2ec1d359
AR
1183 .direction_input = nmk_gpio_make_input,
1184 .get = nmk_gpio_get_input,
1185 .direction_output = nmk_gpio_make_output,
1186 .set = nmk_gpio_set_output,
0d2aec9c 1187 .to_irq = nmk_gpio_to_irq,
d0b543c7 1188 .dbg_show = nmk_gpio_dbg_show,
2ec1d359
AR
1189 .can_sleep = 0,
1190};
1191
3c0227d2
RV
1192void nmk_gpio_clocks_enable(void)
1193{
1194 int i;
1195
1196 for (i = 0; i < NUM_BANKS; i++) {
1197 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1198
1199 if (!chip)
1200 continue;
1201
1202 clk_enable(chip->clk);
1203 }
1204}
1205
1206void nmk_gpio_clocks_disable(void)
1207{
1208 int i;
1209
1210 for (i = 0; i < NUM_BANKS; i++) {
1211 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1212
1213 if (!chip)
1214 continue;
1215
1216 clk_disable(chip->clk);
1217 }
1218}
1219
b9df468d
RV
1220/*
1221 * Called from the suspend/resume path to only keep the real wakeup interrupts
1222 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1223 * and not the rest of the interrupts which we needed to have as wakeups for
1224 * cpuidle.
1225 *
1226 * PM ops are not used since this needs to be done at the end, after all the
1227 * other drivers are done with their suspend callbacks.
1228 */
1229void nmk_gpio_wakeups_suspend(void)
1230{
1231 int i;
1232
1233 for (i = 0; i < NUM_BANKS; i++) {
1234 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1235
1236 if (!chip)
1237 break;
1238
3c0227d2
RV
1239 clk_enable(chip->clk);
1240
b9df468d
RV
1241 writel(chip->rwimsc & chip->real_wake,
1242 chip->addr + NMK_GPIO_RWIMSC);
1243 writel(chip->fwimsc & chip->real_wake,
1244 chip->addr + NMK_GPIO_FWIMSC);
1245
3c0227d2 1246 clk_disable(chip->clk);
b9df468d
RV
1247 }
1248}
1249
1250void nmk_gpio_wakeups_resume(void)
1251{
1252 int i;
1253
1254 for (i = 0; i < NUM_BANKS; i++) {
1255 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1256
1257 if (!chip)
1258 break;
1259
3c0227d2
RV
1260 clk_enable(chip->clk);
1261
b9df468d
RV
1262 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1263 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1264
3c0227d2 1265 clk_disable(chip->clk);
b9df468d
RV
1266 }
1267}
1268
bc6f5cf6
RA
1269/*
1270 * Read the pull up/pull down status.
1271 * A bit set in 'pull_up' means that pull up
1272 * is selected if pull is enabled in PDIS register.
1273 * Note: only pull up/down set via this driver can
1274 * be detected due to HW limitations.
1275 */
1276void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1277{
1278 if (gpio_bank < NUM_BANKS) {
1279 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1280
1281 if (!chip)
1282 return;
1283
1284 *pull_up = chip->pull_up;
1285 }
1286}
1287
5212d096
AL
1288static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1289 irq_hw_number_t hwirq)
a60b57ed
LJ
1290{
1291 struct nmk_gpio_chip *nmk_chip = d->host_data;
1292
1293 if (!nmk_chip)
1294 return -EINVAL;
1295
1296 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1297 set_irq_flags(irq, IRQF_VALID);
1298 irq_set_chip_data(irq, nmk_chip);
1299 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1300
1301 return 0;
1302}
1303
1304const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1305 .map = nmk_gpio_irq_map,
1306 .xlate = irq_domain_xlate_twocell,
1307};
1308
150632b0 1309static int nmk_gpio_probe(struct platform_device *dev)
2ec1d359 1310{
3e3c62ca 1311 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
513c27f8 1312 struct device_node *np = dev->dev.of_node;
2ec1d359
AR
1313 struct nmk_gpio_chip *nmk_chip;
1314 struct gpio_chip *chip;
3e3c62ca 1315 struct resource *res;
af7dc228 1316 struct clk *clk;
33b744b3 1317 int secondary_irq;
8d91771c 1318 void __iomem *base;
832b6cdf 1319 int irq_start = 0;
3e3c62ca 1320 int irq;
2ec1d359
AR
1321 int ret;
1322
513c27f8
LJ
1323 if (!pdata && !np) {
1324 dev_err(&dev->dev, "No platform data or device tree found\n");
3e3c62ca 1325 return -ENODEV;
513c27f8
LJ
1326 }
1327
1328 if (np) {
5e754f33 1329 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
513c27f8
LJ
1330 if (!pdata)
1331 return -ENOMEM;
1332
612e1d5f 1333 if (of_get_property(np, "st,supports-sleepmode", NULL))
513c27f8
LJ
1334 pdata->supports_sleepmode = true;
1335
1336 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1337 dev_err(&dev->dev, "gpio-bank property not found\n");
1338 ret = -EINVAL;
a60b57ed 1339 goto out;
513c27f8
LJ
1340 }
1341
1342 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1343 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1344 }
3e3c62ca
RV
1345
1346 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1347 if (!res) {
1348 ret = -ENOENT;
1349 goto out;
1350 }
1351
1352 irq = platform_get_irq(dev, 0);
1353 if (irq < 0) {
1354 ret = irq;
1355 goto out;
1356 }
1357
33b744b3
RV
1358 secondary_irq = platform_get_irq(dev, 1);
1359 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1360 ret = -EINVAL;
1361 goto out;
1362 }
1363
5e754f33 1364 base = devm_request_and_ioremap(&dev->dev, res);
8d91771c
LW
1365 if (!base) {
1366 ret = -ENOMEM;
5e754f33 1367 goto out;
8d91771c
LW
1368 }
1369
5e754f33 1370 clk = devm_clk_get(&dev->dev, NULL);
af7dc228
RV
1371 if (IS_ERR(clk)) {
1372 ret = PTR_ERR(clk);
5e754f33 1373 goto out;
af7dc228 1374 }
efec381c 1375 clk_prepare(clk);
af7dc228 1376
5e754f33 1377 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
2ec1d359
AR
1378 if (!nmk_chip) {
1379 ret = -ENOMEM;
5e754f33 1380 goto out;
2ec1d359 1381 }
513c27f8 1382
2ec1d359
AR
1383 /*
1384 * The virt address in nmk_chip->addr is in the nomadik register space,
1385 * so we can simply convert the resource address, without remapping
1386 */
33b744b3 1387 nmk_chip->bank = dev->id;
af7dc228 1388 nmk_chip->clk = clk;
8d91771c 1389 nmk_chip->addr = base;
2ec1d359 1390 nmk_chip->chip = nmk_gpio_template;
3e3c62ca 1391 nmk_chip->parent_irq = irq;
33b744b3
RV
1392 nmk_chip->secondary_parent_irq = secondary_irq;
1393 nmk_chip->get_secondary_status = pdata->get_secondary_status;
01727e61 1394 nmk_chip->set_ioforce = pdata->set_ioforce;
33d78647 1395 nmk_chip->sleepmode = pdata->supports_sleepmode;
c0fcb8db 1396 spin_lock_init(&nmk_chip->lock);
2ec1d359
AR
1397
1398 chip = &nmk_chip->chip;
1399 chip->base = pdata->first_gpio;
e493e06f 1400 chip->ngpio = pdata->num_gpio;
8d568ae5 1401 chip->label = pdata->name ?: dev_name(&dev->dev);
2ec1d359
AR
1402 chip->dev = &dev->dev;
1403 chip->owner = THIS_MODULE;
1404
ebc6178d
RV
1405 clk_enable(nmk_chip->clk);
1406 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1407 clk_disable(nmk_chip->clk);
1408
072e82a1 1409#ifdef CONFIG_OF_GPIO
513c27f8 1410 chip->of_node = np;
072e82a1 1411#endif
513c27f8 1412
2ec1d359
AR
1413 ret = gpiochip_add(&nmk_chip->chip);
1414 if (ret)
5e754f33 1415 goto out;
2ec1d359 1416
01727e61
RV
1417 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1418
1419 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
513c27f8 1420
3e3c62ca 1421 platform_set_drvdata(dev, nmk_chip);
2ec1d359 1422
51f58c68 1423 if (!np)
6054b9ca 1424 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
38843e29 1425 nmk_chip->domain = irq_domain_add_simple(np,
6054b9ca
LW
1426 NMK_GPIO_PER_CHIP, irq_start,
1427 &nmk_gpio_irq_simple_ops, nmk_chip);
a60b57ed 1428 if (!nmk_chip->domain) {
2ee38d4d 1429 dev_err(&dev->dev, "failed to create irqdomain\n");
a60b57ed 1430 ret = -ENOSYS;
5e754f33 1431 goto out;
a60b57ed
LJ
1432 }
1433
2ec1d359
AR
1434 nmk_gpio_init_irq(nmk_chip);
1435
513c27f8
LJ
1436 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1437
2ec1d359
AR
1438 return 0;
1439
3e3c62ca 1440out:
2ec1d359
AR
1441 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1442 pdata->first_gpio, pdata->first_gpio+31);
513c27f8 1443
2ec1d359
AR
1444 return ret;
1445}
1446
e98ea774
LW
1447static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1448{
1449 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1450
1451 return npct->soc->ngroups;
1452}
1453
1454static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1455 unsigned selector)
1456{
1457 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1458
1459 return npct->soc->groups[selector].name;
1460}
1461
1462static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1463 const unsigned **pins,
1464 unsigned *num_pins)
1465{
1466 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1467
1468 *pins = npct->soc->groups[selector].pins;
1469 *num_pins = npct->soc->groups[selector].npins;
1470 return 0;
1471}
1472
24cbdd75
LW
1473static struct pinctrl_gpio_range *
1474nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1475{
1476 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1477 int i;
1478
1479 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1480 struct pinctrl_gpio_range *range;
1481
1482 range = &npct->soc->gpio_ranges[i];
1483 if (offset >= range->pin_base &&
1484 offset <= (range->pin_base + range->npins - 1))
1485 return range;
1486 }
1487 return NULL;
1488}
1489
e98ea774
LW
1490static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1491 unsigned offset)
1492{
24cbdd75
LW
1493 struct pinctrl_gpio_range *range;
1494 struct gpio_chip *chip;
1495
1496 range = nmk_match_gpio_range(pctldev, offset);
1497 if (!range || !range->gc) {
1498 seq_printf(s, "invalid pin offset");
1499 return;
1500 }
1501 chip = range->gc;
2249b19f 1502 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
e98ea774
LW
1503}
1504
1505static struct pinctrl_ops nmk_pinctrl_ops = {
1506 .get_groups_count = nmk_get_groups_cnt,
1507 .get_group_name = nmk_get_group_name,
1508 .get_group_pins = nmk_get_group_pins,
1509 .pin_dbg_show = nmk_pin_dbg_show,
1510};
1511
dbfe8ca2
LW
1512static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1513{
1514 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1515
1516 return npct->soc->nfunctions;
1517}
1518
1519static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1520 unsigned function)
1521{
1522 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1523
1524 return npct->soc->functions[function].name;
1525}
1526
1527static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1528 unsigned function,
1529 const char * const **groups,
1530 unsigned * const num_groups)
1531{
1532 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1533
1534 *groups = npct->soc->functions[function].groups;
1535 *num_groups = npct->soc->functions[function].ngroups;
1536
1537 return 0;
1538}
1539
1540static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1541 unsigned group)
1542{
1543 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1544 const struct nmk_pingroup *g;
1545 static unsigned int slpm[NUM_BANKS];
1546 unsigned long flags;
1547 bool glitch;
1548 int ret = -EINVAL;
1549 int i;
1550
1551 g = &npct->soc->groups[group];
1552
1553 if (g->altsetting < 0)
1554 return -EINVAL;
1555
1556 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1557
daf73174
LW
1558 /*
1559 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1560 * we may pass through an undesired state. In this case we take
1561 * some extra care.
1562 *
1563 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1564 * - Save SLPM registers (since we have a shadow register in the
1565 * nmk_chip we're using that as backup)
1566 * - Set SLPM=0 for the IOs you want to switch and others to 1
1567 * - Configure the GPIO registers for the IOs that are being switched
1568 * - Set IOFORCE=1
1569 * - Modify the AFLSA/B registers for the IOs that are being switched
1570 * - Set IOFORCE=0
1571 * - Restore SLPM registers
1572 * - Any spurious wake up event during switch sequence to be ignored
1573 * and cleared
1574 *
1575 * We REALLY need to save ALL slpm registers, because the external
1576 * IOFORCE will switch *all* ports to their sleepmode setting to as
1577 * to avoid glitches. (Not just one port!)
1578 */
c22df08c 1579 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
dbfe8ca2
LW
1580
1581 if (glitch) {
1582 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1583
1584 /* Initially don't put any pins to sleep when switching */
1585 memset(slpm, 0xff, sizeof(slpm));
1586
1587 /*
1588 * Then mask the pins that need to be sleeping now when we're
1589 * switching to the ALT C function.
1590 */
1591 for (i = 0; i < g->npins; i++)
1592 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1593 nmk_gpio_glitch_slpm_init(slpm);
1594 }
1595
1596 for (i = 0; i < g->npins; i++) {
1597 struct pinctrl_gpio_range *range;
1598 struct nmk_gpio_chip *nmk_chip;
1599 struct gpio_chip *chip;
1600 unsigned bit;
1601
1602 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1603 if (!range) {
1604 dev_err(npct->dev,
1605 "invalid pin offset %d in group %s at index %d\n",
1606 g->pins[i], g->name, i);
1607 goto out_glitch;
1608 }
1609 if (!range->gc) {
1610 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1611 g->pins[i], g->name, i);
1612 goto out_glitch;
1613 }
1614 chip = range->gc;
1615 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1616 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1617
1618 clk_enable(nmk_chip->clk);
1619 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1620 /*
1621 * If the pin is switching to altfunc, and there was an
1622 * interrupt installed on it which has been lazy disabled,
1623 * actually mask the interrupt to prevent spurious interrupts
1624 * that would occur while the pin is under control of the
1625 * peripheral. Only SKE does this.
1626 */
1627 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1628
c22df08c
JNG
1629 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1630 (g->altsetting & NMK_GPIO_ALT_C), glitch);
dbfe8ca2 1631 clk_disable(nmk_chip->clk);
c22df08c
JNG
1632
1633 /*
1634 * Call PRCM GPIOCR config function in case ALTC
1635 * has been selected:
1636 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1637 * must be set.
1638 * - If selection is pure ALTC and previous selection was ALTCx,
1639 * then some bits in PRCM GPIOCR registers must be cleared.
1640 */
1641 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1642 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1643 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
dbfe8ca2
LW
1644 }
1645
1646 /* When all pins are successfully reconfigured we get here */
1647 ret = 0;
1648
1649out_glitch:
1650 if (glitch) {
1651 nmk_gpio_glitch_slpm_restore(slpm);
1652 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1653 }
1654
1655 return ret;
1656}
1657
1658static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1659 unsigned function, unsigned group)
1660{
1661 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1662 const struct nmk_pingroup *g;
1663
1664 g = &npct->soc->groups[group];
1665
1666 if (g->altsetting < 0)
1667 return;
1668
1669 /* Poke out the mux, set the pin to some default state? */
1670 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1671}
1672
5212d096
AL
1673static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1674 struct pinctrl_gpio_range *range,
1675 unsigned offset)
dbfe8ca2
LW
1676{
1677 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1678 struct nmk_gpio_chip *nmk_chip;
1679 struct gpio_chip *chip;
1680 unsigned bit;
1681
1682 if (!range) {
1683 dev_err(npct->dev, "invalid range\n");
1684 return -EINVAL;
1685 }
1686 if (!range->gc) {
1687 dev_err(npct->dev, "missing GPIO chip in range\n");
1688 return -EINVAL;
1689 }
1690 chip = range->gc;
1691 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1692
1693 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1694
1695 clk_enable(nmk_chip->clk);
1696 bit = offset % NMK_GPIO_PER_CHIP;
1697 /* There is no glitch when converting any pin to GPIO */
1698 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1699 clk_disable(nmk_chip->clk);
1700
1701 return 0;
1702}
1703
5212d096
AL
1704static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1705 struct pinctrl_gpio_range *range,
1706 unsigned offset)
dbfe8ca2
LW
1707{
1708 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1709
1710 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1711 /* Set the pin to some default state, GPIO is usually default */
1712}
1713
1714static struct pinmux_ops nmk_pinmux_ops = {
1715 .get_functions_count = nmk_pmx_get_funcs_cnt,
1716 .get_function_name = nmk_pmx_get_func_name,
1717 .get_function_groups = nmk_pmx_get_func_groups,
1718 .enable = nmk_pmx_enable,
1719 .disable = nmk_pmx_disable,
1720 .gpio_request_enable = nmk_gpio_request_enable,
1721 .gpio_disable_free = nmk_gpio_disable_free,
1722};
1723
5212d096
AL
1724static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1725 unsigned long *config)
d41af627
LW
1726{
1727 /* Not implemented */
1728 return -EINVAL;
1729}
1730
5212d096
AL
1731static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1732 unsigned long config)
d41af627
LW
1733{
1734 static const char *pullnames[] = {
1735 [NMK_GPIO_PULL_NONE] = "none",
1736 [NMK_GPIO_PULL_UP] = "up",
1737 [NMK_GPIO_PULL_DOWN] = "down",
1738 [3] /* illegal */ = "??"
1739 };
1740 static const char *slpmnames[] = {
1741 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1742 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1743 };
1744 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1745 struct nmk_gpio_chip *nmk_chip;
1746 struct pinctrl_gpio_range *range;
1747 struct gpio_chip *chip;
1748 unsigned bit;
1749
1750 /*
1751 * The pin config contains pin number and altfunction fields, here
1752 * we just ignore that part. It's being handled by the framework and
1753 * pinmux callback respectively.
1754 */
1755 pin_cfg_t cfg = (pin_cfg_t) config;
1756 int pull = PIN_PULL(cfg);
1757 int slpm = PIN_SLPM(cfg);
1758 int output = PIN_DIR(cfg);
1759 int val = PIN_VAL(cfg);
1760 bool lowemi = PIN_LOWEMI(cfg);
1761 bool gpiomode = PIN_GPIOMODE(cfg);
1762 bool sleep = PIN_SLEEPMODE(cfg);
1763
1764 range = nmk_match_gpio_range(pctldev, pin);
1765 if (!range) {
1766 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1767 return -EINVAL;
1768 }
1769 if (!range->gc) {
1770 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1771 pin);
1772 return -EINVAL;
1773 }
1774 chip = range->gc;
1775 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1776
1777 if (sleep) {
1778 int slpm_pull = PIN_SLPM_PULL(cfg);
1779 int slpm_output = PIN_SLPM_DIR(cfg);
1780 int slpm_val = PIN_SLPM_VAL(cfg);
1781
1782 /* All pins go into GPIO mode at sleep */
1783 gpiomode = true;
1784
1785 /*
1786 * The SLPM_* values are normal values + 1 to allow zero to
1787 * mean "same as normal".
1788 */
1789 if (slpm_pull)
1790 pull = slpm_pull - 1;
1791 if (slpm_output)
1792 output = slpm_output - 1;
1793 if (slpm_val)
1794 val = slpm_val - 1;
1795
1796 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1797 pin,
1798 slpm_pull ? pullnames[pull] : "same",
1799 slpm_output ? (output ? "output" : "input") : "same",
1800 slpm_val ? (val ? "high" : "low") : "same");
1801 }
1802
1803 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1804 pin, cfg, pullnames[pull], slpmnames[slpm],
1805 output ? "output " : "input",
1806 output ? (val ? "high" : "low") : "",
1807 lowemi ? "on" : "off" );
1808
1809 clk_enable(nmk_chip->clk);
1810 bit = pin % NMK_GPIO_PER_CHIP;
1811 if (gpiomode)
1812 /* No glitch when going to GPIO mode */
1813 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1814 if (output)
1815 __nmk_gpio_make_output(nmk_chip, bit, val);
1816 else {
1817 __nmk_gpio_make_input(nmk_chip, bit);
1818 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1819 }
1820 /* TODO: isn't this only applicable on output pins? */
1821 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1822
1823 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1824 clk_disable(nmk_chip->clk);
1825 return 0;
1826}
1827
1828static struct pinconf_ops nmk_pinconf_ops = {
1829 .pin_config_get = nmk_pin_config_get,
1830 .pin_config_set = nmk_pin_config_set,
1831};
1832
e98ea774
LW
1833static struct pinctrl_desc nmk_pinctrl_desc = {
1834 .name = "pinctrl-nomadik",
1835 .pctlops = &nmk_pinctrl_ops,
dbfe8ca2 1836 .pmxops = &nmk_pinmux_ops,
d41af627 1837 .confops = &nmk_pinconf_ops,
e98ea774
LW
1838 .owner = THIS_MODULE,
1839};
1840
855f80cd
LJ
1841static const struct of_device_id nmk_pinctrl_match[] = {
1842 {
1843 .compatible = "stericsson,nmk_pinctrl",
1844 .data = (void *)PINCTRL_NMK_DB8500,
1845 },
1846 {},
1847};
1848
150632b0 1849static int nmk_pinctrl_probe(struct platform_device *pdev)
e98ea774
LW
1850{
1851 const struct platform_device_id *platid = platform_get_device_id(pdev);
855f80cd 1852 struct device_node *np = pdev->dev.of_node;
e98ea774 1853 struct nmk_pinctrl *npct;
f1671bf5 1854 struct resource *res;
855f80cd 1855 unsigned int version = 0;
e98ea774
LW
1856 int i;
1857
1858 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1859 if (!npct)
1860 return -ENOMEM;
1861
855f80cd
LJ
1862 if (platid)
1863 version = platid->driver_data;
953e9e93
AL
1864 else if (np) {
1865 const struct of_device_id *match;
1866
1867 match = of_match_device(nmk_pinctrl_match, &pdev->dev);
1868 if (!match)
1869 return -ENODEV;
1870 version = (unsigned int) match->data;
1871 }
855f80cd 1872
e98ea774 1873 /* Poke in other ASIC variants here */
f79c5ed9
LW
1874 if (version == PINCTRL_NMK_STN8815)
1875 nmk_pinctrl_stn8815_init(&npct->soc);
855f80cd 1876 if (version == PINCTRL_NMK_DB8500)
e98ea774 1877 nmk_pinctrl_db8500_init(&npct->soc);
45a1b531
PC
1878 if (version == PINCTRL_NMK_DB8540)
1879 nmk_pinctrl_db8540_init(&npct->soc);
e98ea774 1880
f1671bf5
JA
1881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1882 if (res) {
1883 npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
1884 resource_size(res));
1885 if (!npct->prcm_base) {
1886 dev_err(&pdev->dev,
1887 "failed to ioremap PRCM registers\n");
1888 return -ENOMEM;
1889 }
1890 } else {
1891 dev_info(&pdev->dev,
1892 "No PRCM base, assume no ALT-Cx control is available\n");
1893 }
1894
e98ea774
LW
1895 /*
1896 * We need all the GPIO drivers to probe FIRST, or we will not be able
1897 * to obtain references to the struct gpio_chip * for them, and we
1898 * need this to proceed.
1899 */
1900 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1d853ca5 1901 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
e98ea774 1902 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
e98ea774
LW
1903 return -EPROBE_DEFER;
1904 }
1d853ca5 1905 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
e98ea774
LW
1906 }
1907
1908 nmk_pinctrl_desc.pins = npct->soc->pins;
1909 nmk_pinctrl_desc.npins = npct->soc->npins;
1910 npct->dev = &pdev->dev;
f1671bf5 1911
e98ea774
LW
1912 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1913 if (!npct->pctl) {
1914 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1915 return -EINVAL;
1916 }
1917
1918 /* We will handle a range of GPIO pins */
1919 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1920 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1921
1922 platform_set_drvdata(pdev, npct);
1923 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1924
1925 return 0;
1926}
1927
513c27f8
LJ
1928static const struct of_device_id nmk_gpio_match[] = {
1929 { .compatible = "st,nomadik-gpio", },
1930 {}
1931};
1932
3e3c62ca
RV
1933static struct platform_driver nmk_gpio_driver = {
1934 .driver = {
2ec1d359
AR
1935 .owner = THIS_MODULE,
1936 .name = "gpio",
513c27f8 1937 .of_match_table = nmk_gpio_match,
5317e4d1 1938 },
2ec1d359 1939 .probe = nmk_gpio_probe,
2ec1d359
AR
1940};
1941
e98ea774
LW
1942static const struct platform_device_id nmk_pinctrl_id[] = {
1943 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1944 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
45a1b531 1945 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
8c995d6d 1946 { }
e98ea774
LW
1947};
1948
1949static struct platform_driver nmk_pinctrl_driver = {
1950 .driver = {
1951 .owner = THIS_MODULE,
1952 .name = "pinctrl-nomadik",
855f80cd 1953 .of_match_table = nmk_pinctrl_match,
e98ea774
LW
1954 },
1955 .probe = nmk_pinctrl_probe,
1956 .id_table = nmk_pinctrl_id,
1957};
1958
2ec1d359
AR
1959static int __init nmk_gpio_init(void)
1960{
e98ea774
LW
1961 int ret;
1962
1963 ret = platform_driver_register(&nmk_gpio_driver);
1964 if (ret)
1965 return ret;
1966 return platform_driver_register(&nmk_pinctrl_driver);
2ec1d359
AR
1967}
1968
33f45ea9 1969core_initcall(nmk_gpio_init);
2ec1d359
AR
1970
1971MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1972MODULE_DESCRIPTION("Nomadik GPIO Driver");
1973MODULE_LICENSE("GPL");
This page took 0.289959 seconds and 5 git commands to generate.