blob: f2fd99ba40e35ef16e128e1b69800316ef02cf6a [file] [log] [blame]
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001/*
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>
Linus Walleij33d78642011-06-09 11:08:47 +02007 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01008 *
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>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010017#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010018#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010019#include <linux/clk.h>
20#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010021#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
Lee Jonesa60b57e2012-04-19 21:36:31 +010025#include <linux/irqdomain.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Lee Jones855f80c2012-05-26 06:09:29 +010027#include <linux/of_device.h>
Linus Walleije98ea772012-04-26 23:57:25 +020028#include <linux/pinctrl/pinctrl.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020029#include <linux/pinctrl/pinmux.h>
Linus Walleijd41af622012-05-03 15:58:12 +020030#include <linux/pinctrl/pinconf.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020031/* Since we request GPIOs from ourself */
32#include <linux/pinctrl/consumer.h>
Linus Walleijb7213702012-10-11 14:33:42 +020033/*
34 * For the U8500 archs, use the PRCMU register interface, for the older
35 * Nomadik, provide some stubs. The functions using these will only be
36 * called on the U8500 series.
37 */
38#ifdef CONFIG_ARCH_U8500
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +020039#include <linux/mfd/dbx500-prcmu.h>
Linus Walleijb7213702012-10-11 14:33:42 +020040#else
41static inline u32 prcmu_read(unsigned int reg) {
42 return 0;
43}
44static inline void prcmu_write(unsigned int reg, u32 value) {}
45static inline void prcmu_write_masked(unsigned int reg, u32 mask, u32 value) {}
46#endif
Linus Walleijbb16bd92012-10-10 14:27:58 +020047#include <linux/platform_data/pinctrl-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010048
Will Deaconadfed152011-02-28 10:12:29 +000049#include <asm/mach/irq.h>
50
Linus Walleije98ea772012-04-26 23:57:25 +020051#include "pinctrl-nomadik.h"
52
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010053/*
54 * The GPIO module in the Nomadik family of Systems-on-Chip is an
55 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020056 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010057 *
58 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
59 */
60
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010061struct nmk_gpio_chip {
62 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010063 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010064 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010065 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053066 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010067 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053068 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053069 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053070 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010071 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020072 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010073 /* Keep track of configured edges */
74 u32 edge_rising;
75 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053076 u32 real_wake;
77 u32 rwimsc;
78 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053079 u32 rimsc;
80 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020081 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053082 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010083};
84
Linus Walleije98ea772012-04-26 23:57:25 +020085struct nmk_pinctrl {
86 struct device *dev;
87 struct pinctrl_dev *pctl;
88 const struct nmk_pinctrl_soc_data *soc;
89};
90
Rabin Vincent01727e62010-12-13 12:02:40 +053091static struct nmk_gpio_chip *
92nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
93
94static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
95
96#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
97
Rabin Vincent6f9a9742010-06-02 05:50:28 +010098static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
99 unsigned offset, int gpio_mode)
100{
101 u32 bit = 1 << offset;
102 u32 afunc, bfunc;
103
104 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
105 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
106 if (gpio_mode & NMK_GPIO_ALT_A)
107 afunc |= bit;
108 if (gpio_mode & NMK_GPIO_ALT_B)
109 bfunc |= bit;
110 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
111 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
112}
113
Rabin Vincent81a3c292010-05-27 12:39:23 +0100114static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
115 unsigned offset, enum nmk_gpio_slpm mode)
116{
117 u32 bit = 1 << offset;
118 u32 slpm;
119
120 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
121 if (mode == NMK_GPIO_SLPM_NOCHANGE)
122 slpm |= bit;
123 else
124 slpm &= ~bit;
125 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
126}
127
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100128static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
129 unsigned offset, enum nmk_gpio_pull pull)
130{
131 u32 bit = 1 << offset;
132 u32 pdis;
133
134 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200135 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100136 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200137 nmk_chip->pull_up &= ~bit;
138 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100139 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200140 }
141
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100142 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
143
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200144 if (pull == NMK_GPIO_PULL_UP) {
145 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100146 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200147 } else if (pull == NMK_GPIO_PULL_DOWN) {
148 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100149 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200150 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100151}
152
Rabin Vincentebc61782011-09-28 15:49:11 +0530153static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
154 unsigned offset, bool lowemi)
155{
156 u32 bit = BIT(offset);
157 bool enabled = nmk_chip->lowemi & bit;
158
159 if (lowemi == enabled)
160 return;
161
162 if (lowemi)
163 nmk_chip->lowemi |= bit;
164 else
165 nmk_chip->lowemi &= ~bit;
166
167 writel_relaxed(nmk_chip->lowemi,
168 nmk_chip->addr + NMK_GPIO_LOWEMI);
169}
170
Rabin Vincent378be062010-06-02 06:06:29 +0100171static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
172 unsigned offset)
173{
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
175}
176
Rabin Vincent6720db72010-09-02 11:28:48 +0100177static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
178 unsigned offset, int val)
179{
180 if (val)
181 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
182 else
183 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
184}
185
186static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
187 unsigned offset, int val)
188{
189 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
190 __nmk_gpio_set_output(nmk_chip, offset, val);
191}
192
Rabin Vincent01727e62010-12-13 12:02:40 +0530193static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
194 unsigned offset, int gpio_mode,
195 bool glitch)
196{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530197 u32 rwimsc = nmk_chip->rwimsc;
198 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530199
200 if (glitch && nmk_chip->set_ioforce) {
201 u32 bit = BIT(offset);
202
Rabin Vincent01727e62010-12-13 12:02:40 +0530203 /* Prevent spurious wakeups */
204 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
205 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
206
207 nmk_chip->set_ioforce(true);
208 }
209
210 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
211
212 if (glitch && nmk_chip->set_ioforce) {
213 nmk_chip->set_ioforce(false);
214
215 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
216 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
217 }
218}
219
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530220static void
221nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
222{
223 u32 falling = nmk_chip->fimsc & BIT(offset);
224 u32 rising = nmk_chip->rimsc & BIT(offset);
225 int gpio = nmk_chip->chip.base + offset;
226 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
227 struct irq_data *d = irq_get_irq_data(irq);
228
229 if (!rising && !falling)
230 return;
231
232 if (!d || !irqd_irq_disabled(d))
233 return;
234
235 if (rising) {
236 nmk_chip->rimsc &= ~BIT(offset);
237 writel_relaxed(nmk_chip->rimsc,
238 nmk_chip->addr + NMK_GPIO_RIMSC);
239 }
240
241 if (falling) {
242 nmk_chip->fimsc &= ~BIT(offset);
243 writel_relaxed(nmk_chip->fimsc,
244 nmk_chip->addr + NMK_GPIO_FIMSC);
245 }
246
247 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
248}
249
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200250static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
251 unsigned offset, unsigned alt_num)
252{
253 int i;
254 u16 reg;
255 u8 bit;
256 u8 alt_index;
257 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
258 const u16 *gpiocr_regs;
259
260 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
261 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
262 alt_num);
263 return;
264 }
265
266 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
267 if (npct->soc->altcx_pins[i].pin == offset)
268 break;
269 }
270 if (i == npct->soc->npins_altcx) {
271 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
272 offset);
273 return;
274 }
275
276 pin_desc = npct->soc->altcx_pins + i;
277 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
278
279 /*
280 * If alt_num is NULL, just clear current ALTCx selection
281 * to make sure we come back to a pure ALTC selection
282 */
283 if (!alt_num) {
284 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
285 if (pin_desc->altcx[i].used == true) {
286 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
287 bit = pin_desc->altcx[i].control_bit;
288 if (prcmu_read(reg) & BIT(bit)) {
289 prcmu_write_masked(reg, BIT(bit), 0);
290 dev_dbg(npct->dev,
291 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
292 offset, i+1);
293 }
294 }
295 }
296 return;
297 }
298
299 alt_index = alt_num - 1;
300 if (pin_desc->altcx[alt_index].used == false) {
301 dev_warn(npct->dev,
302 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
303 offset, alt_num);
304 return;
305 }
306
307 /*
308 * Check if any other ALTCx functions are activated on this pin
309 * and disable it first.
310 */
311 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
312 if (i == alt_index)
313 continue;
314 if (pin_desc->altcx[i].used == true) {
315 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
316 bit = pin_desc->altcx[i].control_bit;
317 if (prcmu_read(reg) & BIT(bit)) {
318 prcmu_write_masked(reg, BIT(bit), 0);
319 dev_dbg(npct->dev,
320 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
321 offset, i+1);
322 }
323 }
324 }
325
326 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
327 bit = pin_desc->altcx[alt_index].control_bit;
328 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
329 offset, alt_index+1);
330 prcmu_write_masked(reg, BIT(bit), BIT(bit));
331}
332
Rabin Vincent378be062010-06-02 06:06:29 +0100333static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530334 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100335{
336 static const char *afnames[] = {
337 [NMK_GPIO_ALT_GPIO] = "GPIO",
338 [NMK_GPIO_ALT_A] = "A",
339 [NMK_GPIO_ALT_B] = "B",
340 [NMK_GPIO_ALT_C] = "C"
341 };
342 static const char *pullnames[] = {
343 [NMK_GPIO_PULL_NONE] = "none",
344 [NMK_GPIO_PULL_UP] = "up",
345 [NMK_GPIO_PULL_DOWN] = "down",
346 [3] /* illegal */ = "??"
347 };
348 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100349 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
350 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100351 };
352
353 int pin = PIN_NUM(cfg);
354 int pull = PIN_PULL(cfg);
355 int af = PIN_ALT(cfg);
356 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100357 int output = PIN_DIR(cfg);
358 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530359 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100360
Rabin Vincentdacdc962010-12-03 20:35:37 +0530361 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
362 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100363 output ? "output " : "input",
364 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100365
Rabin Vincentdacdc962010-12-03 20:35:37 +0530366 if (sleep) {
367 int slpm_pull = PIN_SLPM_PULL(cfg);
368 int slpm_output = PIN_SLPM_DIR(cfg);
369 int slpm_val = PIN_SLPM_VAL(cfg);
370
Rabin Vincent3546d152010-11-25 11:38:27 +0530371 af = NMK_GPIO_ALT_GPIO;
372
Rabin Vincentdacdc962010-12-03 20:35:37 +0530373 /*
374 * The SLPM_* values are normal values + 1 to allow zero to
375 * mean "same as normal".
376 */
377 if (slpm_pull)
378 pull = slpm_pull - 1;
379 if (slpm_output)
380 output = slpm_output - 1;
381 if (slpm_val)
382 val = slpm_val - 1;
383
384 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
385 pin,
386 slpm_pull ? pullnames[pull] : "same",
387 slpm_output ? (output ? "output" : "input") : "same",
388 slpm_val ? (val ? "high" : "low") : "same");
389 }
390
Rabin Vincent6720db72010-09-02 11:28:48 +0100391 if (output)
392 __nmk_gpio_make_output(nmk_chip, offset, val);
393 else {
394 __nmk_gpio_make_input(nmk_chip, offset);
395 __nmk_gpio_set_pull(nmk_chip, offset, pull);
396 }
397
Rabin Vincentebc61782011-09-28 15:49:11 +0530398 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
399
Rabin Vincent01727e62010-12-13 12:02:40 +0530400 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530401 * If the pin is switching to altfunc, and there was an interrupt
402 * installed on it which has been lazy disabled, actually mask the
403 * interrupt to prevent spurious interrupts that would occur while the
404 * pin is under control of the peripheral. Only SKE does this.
405 */
406 if (af != NMK_GPIO_ALT_GPIO)
407 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
408
409 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530410 * If we've backed up the SLPM registers (glitch workaround), modify
411 * the backups since they will be restored.
412 */
413 if (slpmregs) {
414 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
415 slpmregs[nmk_chip->bank] |= BIT(offset);
416 else
417 slpmregs[nmk_chip->bank] &= ~BIT(offset);
418 } else
419 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
420
421 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
422}
423
424/*
425 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
426 * - Save SLPM registers
427 * - Set SLPM=0 for the IOs you want to switch and others to 1
428 * - Configure the GPIO registers for the IOs that are being switched
429 * - Set IOFORCE=1
430 * - Modify the AFLSA/B registers for the IOs that are being switched
431 * - Set IOFORCE=0
432 * - Restore SLPM registers
433 * - Any spurious wake up event during switch sequence to be ignored and
434 * cleared
435 */
436static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
437{
438 int i;
439
440 for (i = 0; i < NUM_BANKS; i++) {
441 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
442 unsigned int temp = slpm[i];
443
444 if (!chip)
445 break;
446
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200447 clk_enable(chip->clk);
448
Rabin Vincent01727e62010-12-13 12:02:40 +0530449 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
450 writel(temp, chip->addr + NMK_GPIO_SLPC);
451 }
452}
453
454static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
455{
456 int i;
457
458 for (i = 0; i < NUM_BANKS; i++) {
459 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
460
461 if (!chip)
462 break;
463
464 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200465
466 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530467 }
468}
469
470static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
471{
472 static unsigned int slpm[NUM_BANKS];
473 unsigned long flags;
474 bool glitch = false;
475 int ret = 0;
476 int i;
477
478 for (i = 0; i < num; i++) {
479 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
480 glitch = true;
481 break;
482 }
483 }
484
485 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
486
487 if (glitch) {
488 memset(slpm, 0xff, sizeof(slpm));
489
490 for (i = 0; i < num; i++) {
491 int pin = PIN_NUM(cfgs[i]);
492 int offset = pin % NMK_GPIO_PER_CHIP;
493
494 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
495 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
496 }
497
498 nmk_gpio_glitch_slpm_init(slpm);
499 }
500
501 for (i = 0; i < num; i++) {
502 struct nmk_gpio_chip *nmk_chip;
503 int pin = PIN_NUM(cfgs[i]);
504
Lee Jonesa60b57e2012-04-19 21:36:31 +0100505 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530506 if (!nmk_chip) {
507 ret = -EINVAL;
508 break;
509 }
510
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200511 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530512 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100513 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530514 cfgs[i], sleep, glitch ? slpm : NULL);
515 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200516 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530517 }
518
519 if (glitch)
520 nmk_gpio_glitch_slpm_restore(slpm);
521
522 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
523
524 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100525}
526
527/**
528 * nmk_config_pin - configure a pin's mux attributes
529 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200530 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100531 * Configures a pin's mode (alternate function or GPIO), its pull up status,
532 * and its sleep mode based on the specified configuration. The @cfg is
533 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
534 * are constructed using, and can be further enhanced with, the macros in
Linus Walleij287f1212012-10-10 14:35:17 +0200535 * <linux/platform_data/pinctrl-nomadik.h>
Rabin Vincent378be062010-06-02 06:06:29 +0100536 *
537 * If a pin's mode is set to GPIO, it is configured as an input to avoid
538 * side-effects. The gpio can be manipulated later using standard GPIO API
539 * calls.
540 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530541int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100542{
Rabin Vincent01727e62010-12-13 12:02:40 +0530543 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100544}
545EXPORT_SYMBOL(nmk_config_pin);
546
547/**
548 * nmk_config_pins - configure several pins at once
549 * @cfgs: array of pin configurations
550 * @num: number of elments in the array
551 *
552 * Configures several pins using nmk_config_pin(). Refer to that function for
553 * further information.
554 */
555int nmk_config_pins(pin_cfg_t *cfgs, int num)
556{
Rabin Vincent01727e62010-12-13 12:02:40 +0530557 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100558}
559EXPORT_SYMBOL(nmk_config_pins);
560
Rabin Vincentdacdc962010-12-03 20:35:37 +0530561int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
562{
Rabin Vincent01727e62010-12-13 12:02:40 +0530563 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530564}
565EXPORT_SYMBOL(nmk_config_pins_sleep);
566
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100567/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100568 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
569 * @gpio: pin number
570 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
571 *
Linus Walleij33d78642011-06-09 11:08:47 +0200572 * This register is actually in the pinmux layer, not the GPIO block itself.
573 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
574 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
575 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
576 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
577 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
578 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100579 *
Linus Walleij33d78642011-06-09 11:08:47 +0200580 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
581 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
582 * entered) regardless of the altfunction selected. Also wake-up detection is
583 * ENABLED.
584 *
585 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
586 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
587 * (for altfunction GPIO) or respective on-chip peripherals (for other
588 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
589 *
590 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100591 */
592int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
593{
594 struct nmk_gpio_chip *nmk_chip;
595 unsigned long flags;
596
Lee Jonesa60b57e2012-04-19 21:36:31 +0100597 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100598 if (!nmk_chip)
599 return -EINVAL;
600
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200601 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530602 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
603 spin_lock(&nmk_chip->lock);
604
Lee Jonesa60b57e2012-04-19 21:36:31 +0100605 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530606
607 spin_unlock(&nmk_chip->lock);
608 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200609 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100610
611 return 0;
612}
613
614/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100615 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
616 * @gpio: pin number
617 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
618 *
619 * Enables/disables pull up/down on a specified pin. This only takes effect if
620 * the pin is configured as an input (either explicitly or by the alternate
621 * function).
622 *
623 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
624 * configured as an input. Otherwise, due to the way the controller registers
625 * work, this function will change the value output on the pin.
626 */
627int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
628{
629 struct nmk_gpio_chip *nmk_chip;
630 unsigned long flags;
631
Lee Jonesa60b57e2012-04-19 21:36:31 +0100632 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100633 if (!nmk_chip)
634 return -EINVAL;
635
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200636 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100637 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100638 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100639 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200640 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100641
642 return 0;
643}
644
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100645/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200646/**
647 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
648 * @gpio: pin number
649 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
650 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
651 *
652 * Sets the mode of the specified pin to one of the alternate functions or
653 * plain GPIO.
654 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100655int nmk_gpio_set_mode(int gpio, int gpio_mode)
656{
657 struct nmk_gpio_chip *nmk_chip;
658 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100659
Lee Jonesa60b57e2012-04-19 21:36:31 +0100660 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100661 if (!nmk_chip)
662 return -EINVAL;
663
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200664 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100665 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100666 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200668 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669
670 return 0;
671}
672EXPORT_SYMBOL(nmk_gpio_set_mode);
673
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200674static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
675{
676 int i;
677 u16 reg;
678 u8 bit;
679 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
680 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
681 const u16 *gpiocr_regs;
682
683 for (i = 0; i < npct->soc->npins_altcx; i++) {
684 if (npct->soc->altcx_pins[i].pin == gpio)
685 break;
686 }
687 if (i == npct->soc->npins_altcx)
688 return NMK_GPIO_ALT_C;
689
690 pin_desc = npct->soc->altcx_pins + i;
691 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
692 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
693 if (pin_desc->altcx[i].used == true) {
694 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
695 bit = pin_desc->altcx[i].control_bit;
696 if (prcmu_read(reg) & BIT(bit))
697 return NMK_GPIO_ALT_C+i+1;
698 }
699 }
700 return NMK_GPIO_ALT_C;
701}
702
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100703int nmk_gpio_get_mode(int gpio)
704{
705 struct nmk_gpio_chip *nmk_chip;
706 u32 afunc, bfunc, bit;
707
Lee Jonesa60b57e2012-04-19 21:36:31 +0100708 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100709 if (!nmk_chip)
710 return -EINVAL;
711
Lee Jonesa60b57e2012-04-19 21:36:31 +0100712 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100713
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200714 clk_enable(nmk_chip->clk);
715
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100716 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
717 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
718
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200719 clk_disable(nmk_chip->clk);
720
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100721 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
722}
723EXPORT_SYMBOL(nmk_gpio_get_mode);
724
725
726/* IRQ functions */
727static inline int nmk_gpio_get_bitmask(int gpio)
728{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100729 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100730}
731
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100732static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100733{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100734 struct nmk_gpio_chip *nmk_chip;
735
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100736 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100737 if (!nmk_chip)
738 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200739
740 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100741 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200742 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743}
744
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100745enum nmk_gpio_irq_type {
746 NORMAL,
747 WAKE,
748};
749
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100750static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100751 int gpio, enum nmk_gpio_irq_type which,
752 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100753{
754 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530755 u32 *rimscval;
756 u32 *fimscval;
757 u32 rimscreg;
758 u32 fimscreg;
759
760 if (which == NORMAL) {
761 rimscreg = NMK_GPIO_RIMSC;
762 fimscreg = NMK_GPIO_FIMSC;
763 rimscval = &nmk_chip->rimsc;
764 fimscval = &nmk_chip->fimsc;
765 } else {
766 rimscreg = NMK_GPIO_RWIMSC;
767 fimscreg = NMK_GPIO_FWIMSC;
768 rimscval = &nmk_chip->rwimsc;
769 fimscval = &nmk_chip->fwimsc;
770 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100771
772 /* we must individually set/clear the two edges */
773 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100774 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530775 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100776 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530777 *rimscval &= ~bitmask;
778 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100779 }
780 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100781 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530782 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100783 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530784 *fimscval &= ~bitmask;
785 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100786 }
787}
788
Rabin Vincentb9df4682011-02-10 11:45:58 +0530789static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
790 int gpio, bool on)
791{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530792 /*
793 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
794 * disabled, since setting SLPM to 1 increases power consumption, and
795 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
796 */
797 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200798 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530799 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200800 }
801
Rabin Vincentb9df4682011-02-10 11:45:58 +0530802 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
803}
804
805static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100806{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100807 struct nmk_gpio_chip *nmk_chip;
808 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100809 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100810
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100811 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100812 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100813 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100814 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100815
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200816 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530817 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
818 spin_lock(&nmk_chip->lock);
819
Lee Jonesa60b57e2012-04-19 21:36:31 +0100820 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530821
822 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100823 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530824
825 spin_unlock(&nmk_chip->lock);
826 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200827 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100828
829 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100830}
831
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100832static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100833{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530834 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100835}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100836
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100837static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530839 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100840}
841
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100842static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100843{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100844 struct nmk_gpio_chip *nmk_chip;
845 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530846 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100847
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100848 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100849 if (!nmk_chip)
850 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100851 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100852
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200853 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530854 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
855 spin_lock(&nmk_chip->lock);
856
Linus Walleij479a0c72011-09-20 10:50:15 +0200857 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100858 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530859
860 if (on)
861 nmk_chip->real_wake |= bitmask;
862 else
863 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530864
865 spin_unlock(&nmk_chip->lock);
866 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200867 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100868
869 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100870}
871
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100872static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873{
Linus Walleij479a0c72011-09-20 10:50:15 +0200874 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200875 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100876 struct nmk_gpio_chip *nmk_chip;
877 unsigned long flags;
878 u32 bitmask;
879
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100880 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100881 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100882 if (!nmk_chip)
883 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100884 if (type & IRQ_TYPE_LEVEL_HIGH)
885 return -EINVAL;
886 if (type & IRQ_TYPE_LEVEL_LOW)
887 return -EINVAL;
888
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200889 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100890 spin_lock_irqsave(&nmk_chip->lock, flags);
891
Rabin Vincent7a852d82010-05-06 10:43:55 +0100892 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100893 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100894
Rabin Vincentb9df4682011-02-10 11:45:58 +0530895 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100896 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100897
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100898 nmk_chip->edge_rising &= ~bitmask;
899 if (type & IRQ_TYPE_EDGE_RISING)
900 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100901
902 nmk_chip->edge_falling &= ~bitmask;
903 if (type & IRQ_TYPE_EDGE_FALLING)
904 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100905
906 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100907 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100908
Rabin Vincentb9df4682011-02-10 11:45:58 +0530909 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100910 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100911
912 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200913 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100914
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100915 return 0;
916}
917
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200918static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
919{
920 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
921
922 clk_enable(nmk_chip->clk);
923 nmk_gpio_irq_unmask(d);
924 return 0;
925}
926
927static void nmk_gpio_irq_shutdown(struct irq_data *d)
928{
929 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
930
931 nmk_gpio_irq_mask(d);
932 clk_disable(nmk_chip->clk);
933}
934
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100935static struct irq_chip nmk_gpio_irq_chip = {
936 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100937 .irq_ack = nmk_gpio_irq_ack,
938 .irq_mask = nmk_gpio_irq_mask,
939 .irq_unmask = nmk_gpio_irq_unmask,
940 .irq_set_type = nmk_gpio_irq_set_type,
941 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200942 .irq_startup = nmk_gpio_irq_startup,
943 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200944 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100945};
946
Rabin Vincent33b744b2010-10-14 10:38:03 +0530947static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
948 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100949{
950 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100951 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100952
Will Deaconadfed152011-02-28 10:12:29 +0000953 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100954
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100955 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530956 while (status) {
957 int bit = __ffs(status);
958
Linus Walleij95f0bc92012-09-27 14:14:09 +0200959 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530960 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100961 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100962
Will Deaconadfed152011-02-28 10:12:29 +0000963 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100964}
965
Rabin Vincent33b744b2010-10-14 10:38:03 +0530966static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
967{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100968 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200969 u32 status;
970
971 clk_enable(nmk_chip->clk);
972 status = readl(nmk_chip->addr + NMK_GPIO_IS);
973 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530974
975 __nmk_gpio_irq_handler(irq, desc, status);
976}
977
978static void nmk_gpio_secondary_irq_handler(unsigned int irq,
979 struct irq_desc *desc)
980{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100981 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530982 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
983
984 __nmk_gpio_irq_handler(irq, desc, status);
985}
986
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100987static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
988{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100989 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
990 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530991
992 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100993 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530994 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100995 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530996 }
997
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100998 return 0;
999}
1000
1001/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001002
1003static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
1004{
1005 /*
1006 * Map back to global GPIO space and request muxing, the direction
1007 * parameter does not matter for this controller.
1008 */
1009 int gpio = chip->base + offset;
1010
1011 return pinctrl_request_gpio(gpio);
1012}
1013
1014static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
1015{
1016 int gpio = chip->base + offset;
1017
1018 pinctrl_free_gpio(gpio);
1019}
1020
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001021static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
1022{
1023 struct nmk_gpio_chip *nmk_chip =
1024 container_of(chip, struct nmk_gpio_chip, chip);
1025
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001026 clk_enable(nmk_chip->clk);
1027
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001028 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001029
1030 clk_disable(nmk_chip->clk);
1031
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001032 return 0;
1033}
1034
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001035static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1036{
1037 struct nmk_gpio_chip *nmk_chip =
1038 container_of(chip, struct nmk_gpio_chip, chip);
1039 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001040 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001041
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001042 clk_enable(nmk_chip->clk);
1043
1044 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1045
1046 clk_disable(nmk_chip->clk);
1047
1048 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001049}
1050
1051static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1052 int val)
1053{
1054 struct nmk_gpio_chip *nmk_chip =
1055 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001056
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001057 clk_enable(nmk_chip->clk);
1058
Rabin Vincent6720db72010-09-02 11:28:48 +01001059 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001060
1061 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001062}
1063
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001064static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1065 int val)
1066{
1067 struct nmk_gpio_chip *nmk_chip =
1068 container_of(chip, struct nmk_gpio_chip, chip);
1069
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001070 clk_enable(nmk_chip->clk);
1071
Rabin Vincent6720db72010-09-02 11:28:48 +01001072 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001073
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001074 clk_disable(nmk_chip->clk);
1075
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001076 return 0;
1077}
1078
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001079static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1080{
1081 struct nmk_gpio_chip *nmk_chip =
1082 container_of(chip, struct nmk_gpio_chip, chip);
1083
Linus Walleij268300b2012-10-19 17:06:54 +02001084 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001085}
1086
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301087#ifdef CONFIG_DEBUG_FS
1088
1089#include <linux/seq_file.h>
1090
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001091static void nmk_gpio_dbg_show_one(struct seq_file *s,
1092 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
1093 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301094{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001095 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301096 struct nmk_gpio_chip *nmk_chip =
1097 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001098 int mode;
1099 bool is_out;
1100 bool pull;
1101 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301102 const char *modes[] = {
1103 [NMK_GPIO_ALT_GPIO] = "gpio",
1104 [NMK_GPIO_ALT_A] = "altA",
1105 [NMK_GPIO_ALT_B] = "altB",
1106 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001107 [NMK_GPIO_ALT_C+1] = "altC1",
1108 [NMK_GPIO_ALT_C+2] = "altC2",
1109 [NMK_GPIO_ALT_C+3] = "altC3",
1110 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301111 };
1112
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001113 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001114 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1115 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1116 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001117 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1118 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001119
Linus Walleij6f4350a2012-05-02 21:06:13 +02001120 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1121 gpio, label ?: "(none)",
1122 is_out ? "out" : "in ",
1123 chip->get
1124 ? (chip->get(chip, offset) ? "hi" : "lo")
1125 : "? ",
1126 (mode < 0) ? "unknown" : modes[mode],
1127 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301128
Linus Walleij6f4350a2012-05-02 21:06:13 +02001129 if (label && !is_out) {
1130 int irq = gpio_to_irq(gpio);
1131 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001132
Linus Walleij6f4350a2012-05-02 21:06:13 +02001133 /* This races with request_irq(), set_irq_type(),
1134 * and set_irq_wake() ... but those are "rare".
1135 */
1136 if (irq >= 0 && desc->action) {
1137 char *trigger;
1138 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001139
Linus Walleij6f4350a2012-05-02 21:06:13 +02001140 if (nmk_chip->edge_rising & bitmask)
1141 trigger = "edge-rising";
1142 else if (nmk_chip->edge_falling & bitmask)
1143 trigger = "edge-falling";
1144 else
1145 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001146
Linus Walleij6f4350a2012-05-02 21:06:13 +02001147 seq_printf(s, " irq-%d %s%s",
1148 irq, trigger,
1149 irqd_is_wakeup_set(&desc->irq_data)
1150 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001151 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301152 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001153 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301154}
1155
Linus Walleij6f4350a2012-05-02 21:06:13 +02001156static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1157{
1158 unsigned i;
1159 unsigned gpio = chip->base;
1160
1161 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001162 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001163 seq_printf(s, "\n");
1164 }
1165}
1166
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301167#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001168static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001169 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +02001170 struct gpio_chip *chip,
1171 unsigned offset, unsigned gpio)
1172{
1173}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301174#define nmk_gpio_dbg_show NULL
1175#endif
1176
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001177/* This structure is replicated for each GPIO block allocated at probe time */
1178static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001179 .request = nmk_gpio_request,
1180 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001181 .direction_input = nmk_gpio_make_input,
1182 .get = nmk_gpio_get_input,
1183 .direction_output = nmk_gpio_make_output,
1184 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001185 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301186 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001187 .can_sleep = 0,
1188};
1189
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001190void nmk_gpio_clocks_enable(void)
1191{
1192 int i;
1193
1194 for (i = 0; i < NUM_BANKS; i++) {
1195 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1196
1197 if (!chip)
1198 continue;
1199
1200 clk_enable(chip->clk);
1201 }
1202}
1203
1204void nmk_gpio_clocks_disable(void)
1205{
1206 int i;
1207
1208 for (i = 0; i < NUM_BANKS; i++) {
1209 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1210
1211 if (!chip)
1212 continue;
1213
1214 clk_disable(chip->clk);
1215 }
1216}
1217
Rabin Vincentb9df4682011-02-10 11:45:58 +05301218/*
1219 * Called from the suspend/resume path to only keep the real wakeup interrupts
1220 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1221 * and not the rest of the interrupts which we needed to have as wakeups for
1222 * cpuidle.
1223 *
1224 * PM ops are not used since this needs to be done at the end, after all the
1225 * other drivers are done with their suspend callbacks.
1226 */
1227void nmk_gpio_wakeups_suspend(void)
1228{
1229 int i;
1230
1231 for (i = 0; i < NUM_BANKS; i++) {
1232 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1233
1234 if (!chip)
1235 break;
1236
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001237 clk_enable(chip->clk);
1238
Rabin Vincentb9df4682011-02-10 11:45:58 +05301239 writel(chip->rwimsc & chip->real_wake,
1240 chip->addr + NMK_GPIO_RWIMSC);
1241 writel(chip->fwimsc & chip->real_wake,
1242 chip->addr + NMK_GPIO_FWIMSC);
1243
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001244 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301245 }
1246}
1247
1248void nmk_gpio_wakeups_resume(void)
1249{
1250 int i;
1251
1252 for (i = 0; i < NUM_BANKS; i++) {
1253 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1254
1255 if (!chip)
1256 break;
1257
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001258 clk_enable(chip->clk);
1259
Rabin Vincentb9df4682011-02-10 11:45:58 +05301260 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1261 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1262
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001263 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301264 }
1265}
1266
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001267/*
1268 * Read the pull up/pull down status.
1269 * A bit set in 'pull_up' means that pull up
1270 * is selected if pull is enabled in PDIS register.
1271 * Note: only pull up/down set via this driver can
1272 * be detected due to HW limitations.
1273 */
1274void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1275{
1276 if (gpio_bank < NUM_BANKS) {
1277 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1278
1279 if (!chip)
1280 return;
1281
1282 *pull_up = chip->pull_up;
1283 }
1284}
1285
Lee Jonesa60b57e2012-04-19 21:36:31 +01001286int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1287 irq_hw_number_t hwirq)
1288{
1289 struct nmk_gpio_chip *nmk_chip = d->host_data;
1290
1291 if (!nmk_chip)
1292 return -EINVAL;
1293
1294 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1295 set_irq_flags(irq, IRQF_VALID);
1296 irq_set_chip_data(irq, nmk_chip);
1297 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1298
1299 return 0;
1300}
1301
1302const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1303 .map = nmk_gpio_irq_map,
1304 .xlate = irq_domain_xlate_twocell,
1305};
1306
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001307static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001308{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001309 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001310 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001311 struct nmk_gpio_chip *nmk_chip;
1312 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001313 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001314 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301315 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001316 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001317 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001318 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001319 int ret;
1320
Lee Jones513c27f2012-04-13 15:05:05 +01001321 if (!pdata && !np) {
1322 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001323 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001324 }
1325
1326 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001327 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001328 if (!pdata)
1329 return -ENOMEM;
1330
Lee Jones612e1d52012-06-14 11:27:56 +01001331 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001332 pdata->supports_sleepmode = true;
1333
1334 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1335 dev_err(&dev->dev, "gpio-bank property not found\n");
1336 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001337 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001338 }
1339
1340 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1341 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1342 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001343
1344 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1345 if (!res) {
1346 ret = -ENOENT;
1347 goto out;
1348 }
1349
1350 irq = platform_get_irq(dev, 0);
1351 if (irq < 0) {
1352 ret = irq;
1353 goto out;
1354 }
1355
Rabin Vincent33b744b2010-10-14 10:38:03 +05301356 secondary_irq = platform_get_irq(dev, 1);
1357 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361
Linus Walleij5e754f32012-07-03 23:05:14 +02001362 base = devm_request_and_ioremap(&dev->dev, res);
1363 if (!base) {
1364 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001365 goto out;
1366 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001367
Linus Walleij5e754f32012-07-03 23:05:14 +02001368 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001369 if (IS_ERR(clk)) {
1370 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001371 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001372 }
Linus Walleijefec3812012-06-06 22:50:41 +02001373 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001374
Linus Walleij5e754f32012-07-03 23:05:14 +02001375 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001376 if (!nmk_chip) {
1377 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001378 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001379 }
Lee Jones513c27f2012-04-13 15:05:05 +01001380
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001381 /*
1382 * The virt address in nmk_chip->addr is in the nomadik register space,
1383 * so we can simply convert the resource address, without remapping
1384 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301385 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001386 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001387 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001388 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001389 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301390 nmk_chip->secondary_parent_irq = secondary_irq;
1391 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301392 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001393 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001394 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001395
1396 chip = &nmk_chip->chip;
1397 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301398 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301399 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001400 chip->dev = &dev->dev;
1401 chip->owner = THIS_MODULE;
1402
Rabin Vincentebc61782011-09-28 15:49:11 +05301403 clk_enable(nmk_chip->clk);
1404 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1405 clk_disable(nmk_chip->clk);
1406
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001407#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001408 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001409#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001410
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001411 ret = gpiochip_add(&nmk_chip->chip);
1412 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001413 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001414
Rabin Vincent01727e62010-12-13 12:02:40 +05301415 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1416
1417 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001418
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001419 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001420
Linus Walleij51f58c62012-10-11 16:33:44 +02001421 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001422 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001423 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001424 NMK_GPIO_PER_CHIP, irq_start,
1425 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001426 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001427 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001428 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001429 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001430 }
1431
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001432 nmk_gpio_init_irq(nmk_chip);
1433
Lee Jones513c27f2012-04-13 15:05:05 +01001434 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1435
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001436 return 0;
1437
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001438out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001439 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1440 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001441
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001442 return ret;
1443}
1444
Linus Walleije98ea772012-04-26 23:57:25 +02001445static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1446{
1447 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1448
1449 return npct->soc->ngroups;
1450}
1451
1452static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1453 unsigned selector)
1454{
1455 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1456
1457 return npct->soc->groups[selector].name;
1458}
1459
1460static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1461 const unsigned **pins,
1462 unsigned *num_pins)
1463{
1464 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1465
1466 *pins = npct->soc->groups[selector].pins;
1467 *num_pins = npct->soc->groups[selector].npins;
1468 return 0;
1469}
1470
Linus Walleij24cbdd72012-05-02 21:28:00 +02001471static struct pinctrl_gpio_range *
1472nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1473{
1474 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1475 int i;
1476
1477 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1478 struct pinctrl_gpio_range *range;
1479
1480 range = &npct->soc->gpio_ranges[i];
1481 if (offset >= range->pin_base &&
1482 offset <= (range->pin_base + range->npins - 1))
1483 return range;
1484 }
1485 return NULL;
1486}
1487
Linus Walleije98ea772012-04-26 23:57:25 +02001488static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1489 unsigned offset)
1490{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001491 struct pinctrl_gpio_range *range;
1492 struct gpio_chip *chip;
1493
1494 range = nmk_match_gpio_range(pctldev, offset);
1495 if (!range || !range->gc) {
1496 seq_printf(s, "invalid pin offset");
1497 return;
1498 }
1499 chip = range->gc;
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001500 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001501}
1502
1503static struct pinctrl_ops nmk_pinctrl_ops = {
1504 .get_groups_count = nmk_get_groups_cnt,
1505 .get_group_name = nmk_get_group_name,
1506 .get_group_pins = nmk_get_group_pins,
1507 .pin_dbg_show = nmk_pin_dbg_show,
1508};
1509
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001510static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1511{
1512 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1513
1514 return npct->soc->nfunctions;
1515}
1516
1517static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1518 unsigned function)
1519{
1520 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1521
1522 return npct->soc->functions[function].name;
1523}
1524
1525static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1526 unsigned function,
1527 const char * const **groups,
1528 unsigned * const num_groups)
1529{
1530 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1531
1532 *groups = npct->soc->functions[function].groups;
1533 *num_groups = npct->soc->functions[function].ngroups;
1534
1535 return 0;
1536}
1537
1538static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1539 unsigned group)
1540{
1541 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1542 const struct nmk_pingroup *g;
1543 static unsigned int slpm[NUM_BANKS];
1544 unsigned long flags;
1545 bool glitch;
1546 int ret = -EINVAL;
1547 int i;
1548
1549 g = &npct->soc->groups[group];
1550
1551 if (g->altsetting < 0)
1552 return -EINVAL;
1553
1554 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1555
Linus Walleijdaf73172012-05-22 11:46:45 +02001556 /*
1557 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1558 * we may pass through an undesired state. In this case we take
1559 * some extra care.
1560 *
1561 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1562 * - Save SLPM registers (since we have a shadow register in the
1563 * nmk_chip we're using that as backup)
1564 * - Set SLPM=0 for the IOs you want to switch and others to 1
1565 * - Configure the GPIO registers for the IOs that are being switched
1566 * - Set IOFORCE=1
1567 * - Modify the AFLSA/B registers for the IOs that are being switched
1568 * - Set IOFORCE=0
1569 * - Restore SLPM registers
1570 * - Any spurious wake up event during switch sequence to be ignored
1571 * and cleared
1572 *
1573 * We REALLY need to save ALL slpm registers, because the external
1574 * IOFORCE will switch *all* ports to their sleepmode setting to as
1575 * to avoid glitches. (Not just one port!)
1576 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001577 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001578
1579 if (glitch) {
1580 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1581
1582 /* Initially don't put any pins to sleep when switching */
1583 memset(slpm, 0xff, sizeof(slpm));
1584
1585 /*
1586 * Then mask the pins that need to be sleeping now when we're
1587 * switching to the ALT C function.
1588 */
1589 for (i = 0; i < g->npins; i++)
1590 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1591 nmk_gpio_glitch_slpm_init(slpm);
1592 }
1593
1594 for (i = 0; i < g->npins; i++) {
1595 struct pinctrl_gpio_range *range;
1596 struct nmk_gpio_chip *nmk_chip;
1597 struct gpio_chip *chip;
1598 unsigned bit;
1599
1600 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1601 if (!range) {
1602 dev_err(npct->dev,
1603 "invalid pin offset %d in group %s at index %d\n",
1604 g->pins[i], g->name, i);
1605 goto out_glitch;
1606 }
1607 if (!range->gc) {
1608 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1609 g->pins[i], g->name, i);
1610 goto out_glitch;
1611 }
1612 chip = range->gc;
1613 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1614 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1615
1616 clk_enable(nmk_chip->clk);
1617 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1618 /*
1619 * If the pin is switching to altfunc, and there was an
1620 * interrupt installed on it which has been lazy disabled,
1621 * actually mask the interrupt to prevent spurious interrupts
1622 * that would occur while the pin is under control of the
1623 * peripheral. Only SKE does this.
1624 */
1625 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1626
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001627 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1628 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001629 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001630
1631 /*
1632 * Call PRCM GPIOCR config function in case ALTC
1633 * has been selected:
1634 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1635 * must be set.
1636 * - If selection is pure ALTC and previous selection was ALTCx,
1637 * then some bits in PRCM GPIOCR registers must be cleared.
1638 */
1639 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1640 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1641 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001642 }
1643
1644 /* When all pins are successfully reconfigured we get here */
1645 ret = 0;
1646
1647out_glitch:
1648 if (glitch) {
1649 nmk_gpio_glitch_slpm_restore(slpm);
1650 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1651 }
1652
1653 return ret;
1654}
1655
1656static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1657 unsigned function, unsigned group)
1658{
1659 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1660 const struct nmk_pingroup *g;
1661
1662 g = &npct->soc->groups[group];
1663
1664 if (g->altsetting < 0)
1665 return;
1666
1667 /* Poke out the mux, set the pin to some default state? */
1668 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1669}
1670
1671int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1672 struct pinctrl_gpio_range *range,
1673 unsigned offset)
1674{
1675 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1676 struct nmk_gpio_chip *nmk_chip;
1677 struct gpio_chip *chip;
1678 unsigned bit;
1679
1680 if (!range) {
1681 dev_err(npct->dev, "invalid range\n");
1682 return -EINVAL;
1683 }
1684 if (!range->gc) {
1685 dev_err(npct->dev, "missing GPIO chip in range\n");
1686 return -EINVAL;
1687 }
1688 chip = range->gc;
1689 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1690
1691 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1692
1693 clk_enable(nmk_chip->clk);
1694 bit = offset % NMK_GPIO_PER_CHIP;
1695 /* There is no glitch when converting any pin to GPIO */
1696 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1697 clk_disable(nmk_chip->clk);
1698
1699 return 0;
1700}
1701
1702void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1703 struct pinctrl_gpio_range *range,
1704 unsigned offset)
1705{
1706 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1707
1708 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1709 /* Set the pin to some default state, GPIO is usually default */
1710}
1711
1712static struct pinmux_ops nmk_pinmux_ops = {
1713 .get_functions_count = nmk_pmx_get_funcs_cnt,
1714 .get_function_name = nmk_pmx_get_func_name,
1715 .get_function_groups = nmk_pmx_get_func_groups,
1716 .enable = nmk_pmx_enable,
1717 .disable = nmk_pmx_disable,
1718 .gpio_request_enable = nmk_gpio_request_enable,
1719 .gpio_disable_free = nmk_gpio_disable_free,
1720};
1721
Linus Walleijd41af622012-05-03 15:58:12 +02001722int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1723 unsigned pin,
1724 unsigned long *config)
1725{
1726 /* Not implemented */
1727 return -EINVAL;
1728}
1729
1730int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1731 unsigned pin,
1732 unsigned long config)
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
Linus Walleije98ea772012-04-26 23:57:25 +02001833static struct pinctrl_desc nmk_pinctrl_desc = {
1834 .name = "pinctrl-nomadik",
1835 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001836 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001837 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001838 .owner = THIS_MODULE,
1839};
1840
Lee Jones855f80c2012-05-26 06:09:29 +01001841static const struct of_device_id nmk_pinctrl_match[] = {
1842 {
1843 .compatible = "stericsson,nmk_pinctrl",
1844 .data = (void *)PINCTRL_NMK_DB8500,
1845 },
1846 {},
1847};
1848
Linus Walleije98ea772012-04-26 23:57:25 +02001849static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1850{
1851 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001852 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001853 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01001854 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001855 int i;
1856
1857 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1858 if (!npct)
1859 return -ENOMEM;
1860
Lee Jones855f80c2012-05-26 06:09:29 +01001861 if (platid)
1862 version = platid->driver_data;
1863 else if (np)
1864 version = (unsigned int)
1865 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1866
Linus Walleije98ea772012-04-26 23:57:25 +02001867 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001868 if (version == PINCTRL_NMK_STN8815)
1869 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001870 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001871 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001872 if (version == PINCTRL_NMK_DB8540)
1873 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001874
1875 /*
1876 * We need all the GPIO drivers to probe FIRST, or we will not be able
1877 * to obtain references to the struct gpio_chip * for them, and we
1878 * need this to proceed.
1879 */
1880 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001881 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001882 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001883 return -EPROBE_DEFER;
1884 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001885 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001886 }
1887
1888 nmk_pinctrl_desc.pins = npct->soc->pins;
1889 nmk_pinctrl_desc.npins = npct->soc->npins;
1890 npct->dev = &pdev->dev;
1891 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1892 if (!npct->pctl) {
1893 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1894 return -EINVAL;
1895 }
1896
1897 /* We will handle a range of GPIO pins */
1898 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1899 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1900
1901 platform_set_drvdata(pdev, npct);
1902 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1903
1904 return 0;
1905}
1906
Lee Jones513c27f2012-04-13 15:05:05 +01001907static const struct of_device_id nmk_gpio_match[] = {
1908 { .compatible = "st,nomadik-gpio", },
1909 {}
1910};
1911
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001912static struct platform_driver nmk_gpio_driver = {
1913 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001914 .owner = THIS_MODULE,
1915 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001916 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301917 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001918 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001919};
1920
Linus Walleije98ea772012-04-26 23:57:25 +02001921static const struct platform_device_id nmk_pinctrl_id[] = {
1922 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1923 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001924 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Linus Walleije98ea772012-04-26 23:57:25 +02001925};
1926
1927static struct platform_driver nmk_pinctrl_driver = {
1928 .driver = {
1929 .owner = THIS_MODULE,
1930 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001931 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001932 },
1933 .probe = nmk_pinctrl_probe,
1934 .id_table = nmk_pinctrl_id,
1935};
1936
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001937static int __init nmk_gpio_init(void)
1938{
Linus Walleije98ea772012-04-26 23:57:25 +02001939 int ret;
1940
1941 ret = platform_driver_register(&nmk_gpio_driver);
1942 if (ret)
1943 return ret;
1944 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001945}
1946
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001947core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001948
1949MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1950MODULE_DESCRIPTION("Nomadik GPIO Driver");
1951MODULE_LICENSE("GPL");