blob: 984a2eccfa3c6ec650c64fb705a79acdb1683eb0 [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
674int nmk_gpio_get_mode(int gpio)
675{
676 struct nmk_gpio_chip *nmk_chip;
677 u32 afunc, bfunc, bit;
678
Lee Jonesa60b57e2012-04-19 21:36:31 +0100679 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680 if (!nmk_chip)
681 return -EINVAL;
682
Lee Jonesa60b57e2012-04-19 21:36:31 +0100683 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100684
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200685 clk_enable(nmk_chip->clk);
686
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100687 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
688 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
689
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200690 clk_disable(nmk_chip->clk);
691
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100692 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
693}
694EXPORT_SYMBOL(nmk_gpio_get_mode);
695
696
697/* IRQ functions */
698static inline int nmk_gpio_get_bitmask(int gpio)
699{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100700 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100701}
702
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100703static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100704{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100705 struct nmk_gpio_chip *nmk_chip;
706
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100707 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100708 if (!nmk_chip)
709 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200710
711 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100712 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200713 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100714}
715
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100716enum nmk_gpio_irq_type {
717 NORMAL,
718 WAKE,
719};
720
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100721static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100722 int gpio, enum nmk_gpio_irq_type which,
723 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100724{
725 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530726 u32 *rimscval;
727 u32 *fimscval;
728 u32 rimscreg;
729 u32 fimscreg;
730
731 if (which == NORMAL) {
732 rimscreg = NMK_GPIO_RIMSC;
733 fimscreg = NMK_GPIO_FIMSC;
734 rimscval = &nmk_chip->rimsc;
735 fimscval = &nmk_chip->fimsc;
736 } else {
737 rimscreg = NMK_GPIO_RWIMSC;
738 fimscreg = NMK_GPIO_FWIMSC;
739 rimscval = &nmk_chip->rwimsc;
740 fimscval = &nmk_chip->fwimsc;
741 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100742
743 /* we must individually set/clear the two edges */
744 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100745 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530746 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100747 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530748 *rimscval &= ~bitmask;
749 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100750 }
751 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100752 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530753 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100754 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530755 *fimscval &= ~bitmask;
756 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100757 }
758}
759
Rabin Vincentb9df4682011-02-10 11:45:58 +0530760static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
761 int gpio, bool on)
762{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530763 /*
764 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
765 * disabled, since setting SLPM to 1 increases power consumption, and
766 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
767 */
768 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200769 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530770 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200771 }
772
Rabin Vincentb9df4682011-02-10 11:45:58 +0530773 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
774}
775
776static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100777{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100778 struct nmk_gpio_chip *nmk_chip;
779 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100780 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100781
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100782 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100783 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100785 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100786
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200787 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530788 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
789 spin_lock(&nmk_chip->lock);
790
Lee Jonesa60b57e2012-04-19 21:36:31 +0100791 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530792
793 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100794 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530795
796 spin_unlock(&nmk_chip->lock);
797 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200798 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100799
800 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100801}
802
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100803static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100804{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530805 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100806}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100807
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100808static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100809{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530810 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100811}
812
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100813static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100814{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100815 struct nmk_gpio_chip *nmk_chip;
816 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530817 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100818
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100819 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100820 if (!nmk_chip)
821 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100822 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100823
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200824 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530825 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
826 spin_lock(&nmk_chip->lock);
827
Linus Walleij479a0c72011-09-20 10:50:15 +0200828 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100829 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530830
831 if (on)
832 nmk_chip->real_wake |= bitmask;
833 else
834 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530835
836 spin_unlock(&nmk_chip->lock);
837 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200838 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100839
840 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100841}
842
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100843static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100844{
Linus Walleij479a0c72011-09-20 10:50:15 +0200845 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200846 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100847 struct nmk_gpio_chip *nmk_chip;
848 unsigned long flags;
849 u32 bitmask;
850
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100851 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100852 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100853 if (!nmk_chip)
854 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100855 if (type & IRQ_TYPE_LEVEL_HIGH)
856 return -EINVAL;
857 if (type & IRQ_TYPE_LEVEL_LOW)
858 return -EINVAL;
859
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200860 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100861 spin_lock_irqsave(&nmk_chip->lock, flags);
862
Rabin Vincent7a852d82010-05-06 10:43:55 +0100863 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100864 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100865
Rabin Vincentb9df4682011-02-10 11:45:58 +0530866 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100867 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100868
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100869 nmk_chip->edge_rising &= ~bitmask;
870 if (type & IRQ_TYPE_EDGE_RISING)
871 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100872
873 nmk_chip->edge_falling &= ~bitmask;
874 if (type & IRQ_TYPE_EDGE_FALLING)
875 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100876
877 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100878 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100879
Rabin Vincentb9df4682011-02-10 11:45:58 +0530880 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100881 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100882
883 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200884 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100885
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100886 return 0;
887}
888
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200889static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
890{
891 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
892
893 clk_enable(nmk_chip->clk);
894 nmk_gpio_irq_unmask(d);
895 return 0;
896}
897
898static void nmk_gpio_irq_shutdown(struct irq_data *d)
899{
900 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
901
902 nmk_gpio_irq_mask(d);
903 clk_disable(nmk_chip->clk);
904}
905
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100906static struct irq_chip nmk_gpio_irq_chip = {
907 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100908 .irq_ack = nmk_gpio_irq_ack,
909 .irq_mask = nmk_gpio_irq_mask,
910 .irq_unmask = nmk_gpio_irq_unmask,
911 .irq_set_type = nmk_gpio_irq_set_type,
912 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200913 .irq_startup = nmk_gpio_irq_startup,
914 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200915 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100916};
917
Rabin Vincent33b744b2010-10-14 10:38:03 +0530918static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
919 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100920{
921 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100922 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100923
Will Deaconadfed152011-02-28 10:12:29 +0000924 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100925
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100926 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530927 while (status) {
928 int bit = __ffs(status);
929
Linus Walleij95f0bc92012-09-27 14:14:09 +0200930 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530931 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100932 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100933
Will Deaconadfed152011-02-28 10:12:29 +0000934 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100935}
936
Rabin Vincent33b744b2010-10-14 10:38:03 +0530937static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
938{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100939 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200940 u32 status;
941
942 clk_enable(nmk_chip->clk);
943 status = readl(nmk_chip->addr + NMK_GPIO_IS);
944 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530945
946 __nmk_gpio_irq_handler(irq, desc, status);
947}
948
949static void nmk_gpio_secondary_irq_handler(unsigned int irq,
950 struct irq_desc *desc)
951{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100952 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530953 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
954
955 __nmk_gpio_irq_handler(irq, desc, status);
956}
957
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100958static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
959{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100960 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
961 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530962
963 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100964 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530965 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100966 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530967 }
968
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100969 return 0;
970}
971
972/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200973
974static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
975{
976 /*
977 * Map back to global GPIO space and request muxing, the direction
978 * parameter does not matter for this controller.
979 */
980 int gpio = chip->base + offset;
981
982 return pinctrl_request_gpio(gpio);
983}
984
985static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
986{
987 int gpio = chip->base + offset;
988
989 pinctrl_free_gpio(gpio);
990}
991
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100992static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
993{
994 struct nmk_gpio_chip *nmk_chip =
995 container_of(chip, struct nmk_gpio_chip, chip);
996
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200997 clk_enable(nmk_chip->clk);
998
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100999 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001000
1001 clk_disable(nmk_chip->clk);
1002
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001003 return 0;
1004}
1005
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001006static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1007{
1008 struct nmk_gpio_chip *nmk_chip =
1009 container_of(chip, struct nmk_gpio_chip, chip);
1010 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001011 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001012
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001013 clk_enable(nmk_chip->clk);
1014
1015 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1016
1017 clk_disable(nmk_chip->clk);
1018
1019 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001020}
1021
1022static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1023 int val)
1024{
1025 struct nmk_gpio_chip *nmk_chip =
1026 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001027
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001028 clk_enable(nmk_chip->clk);
1029
Rabin Vincent6720db72010-09-02 11:28:48 +01001030 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001031
1032 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001033}
1034
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001035static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1036 int val)
1037{
1038 struct nmk_gpio_chip *nmk_chip =
1039 container_of(chip, struct nmk_gpio_chip, chip);
1040
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001041 clk_enable(nmk_chip->clk);
1042
Rabin Vincent6720db72010-09-02 11:28:48 +01001043 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001044
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001045 clk_disable(nmk_chip->clk);
1046
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001047 return 0;
1048}
1049
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001050static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1051{
1052 struct nmk_gpio_chip *nmk_chip =
1053 container_of(chip, struct nmk_gpio_chip, chip);
1054
Linus Walleij268300b2012-10-19 17:06:54 +02001055 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001056}
1057
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301058#ifdef CONFIG_DEBUG_FS
1059
1060#include <linux/seq_file.h>
1061
Linus Walleij6f4350a2012-05-02 21:06:13 +02001062static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
1063 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301064{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001065 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301066 struct nmk_gpio_chip *nmk_chip =
1067 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001068 int mode;
1069 bool is_out;
1070 bool pull;
1071 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301072 const char *modes[] = {
1073 [NMK_GPIO_ALT_GPIO] = "gpio",
1074 [NMK_GPIO_ALT_A] = "altA",
1075 [NMK_GPIO_ALT_B] = "altB",
1076 [NMK_GPIO_ALT_C] = "altC",
1077 };
1078
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001079 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001080 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1081 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1082 mode = nmk_gpio_get_mode(gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001083
Linus Walleij6f4350a2012-05-02 21:06:13 +02001084 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1085 gpio, label ?: "(none)",
1086 is_out ? "out" : "in ",
1087 chip->get
1088 ? (chip->get(chip, offset) ? "hi" : "lo")
1089 : "? ",
1090 (mode < 0) ? "unknown" : modes[mode],
1091 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301092
Linus Walleij6f4350a2012-05-02 21:06:13 +02001093 if (label && !is_out) {
1094 int irq = gpio_to_irq(gpio);
1095 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001096
Linus Walleij6f4350a2012-05-02 21:06:13 +02001097 /* This races with request_irq(), set_irq_type(),
1098 * and set_irq_wake() ... but those are "rare".
1099 */
1100 if (irq >= 0 && desc->action) {
1101 char *trigger;
1102 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001103
Linus Walleij6f4350a2012-05-02 21:06:13 +02001104 if (nmk_chip->edge_rising & bitmask)
1105 trigger = "edge-rising";
1106 else if (nmk_chip->edge_falling & bitmask)
1107 trigger = "edge-falling";
1108 else
1109 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001110
Linus Walleij6f4350a2012-05-02 21:06:13 +02001111 seq_printf(s, " irq-%d %s%s",
1112 irq, trigger,
1113 irqd_is_wakeup_set(&desc->irq_data)
1114 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001115 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301116 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001117 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301118}
1119
Linus Walleij6f4350a2012-05-02 21:06:13 +02001120static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1121{
1122 unsigned i;
1123 unsigned gpio = chip->base;
1124
1125 for (i = 0; i < chip->ngpio; i++, gpio++) {
1126 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1127 seq_printf(s, "\n");
1128 }
1129}
1130
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301131#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001132static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1133 struct gpio_chip *chip,
1134 unsigned offset, unsigned gpio)
1135{
1136}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301137#define nmk_gpio_dbg_show NULL
1138#endif
1139
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001140/* This structure is replicated for each GPIO block allocated at probe time */
1141static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001142 .request = nmk_gpio_request,
1143 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001144 .direction_input = nmk_gpio_make_input,
1145 .get = nmk_gpio_get_input,
1146 .direction_output = nmk_gpio_make_output,
1147 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001148 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301149 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001150 .can_sleep = 0,
1151};
1152
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001153void nmk_gpio_clocks_enable(void)
1154{
1155 int i;
1156
1157 for (i = 0; i < NUM_BANKS; i++) {
1158 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1159
1160 if (!chip)
1161 continue;
1162
1163 clk_enable(chip->clk);
1164 }
1165}
1166
1167void nmk_gpio_clocks_disable(void)
1168{
1169 int i;
1170
1171 for (i = 0; i < NUM_BANKS; i++) {
1172 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1173
1174 if (!chip)
1175 continue;
1176
1177 clk_disable(chip->clk);
1178 }
1179}
1180
Rabin Vincentb9df4682011-02-10 11:45:58 +05301181/*
1182 * Called from the suspend/resume path to only keep the real wakeup interrupts
1183 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1184 * and not the rest of the interrupts which we needed to have as wakeups for
1185 * cpuidle.
1186 *
1187 * PM ops are not used since this needs to be done at the end, after all the
1188 * other drivers are done with their suspend callbacks.
1189 */
1190void nmk_gpio_wakeups_suspend(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 break;
1199
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001200 clk_enable(chip->clk);
1201
Rabin Vincentb9df4682011-02-10 11:45:58 +05301202 writel(chip->rwimsc & chip->real_wake,
1203 chip->addr + NMK_GPIO_RWIMSC);
1204 writel(chip->fwimsc & chip->real_wake,
1205 chip->addr + NMK_GPIO_FWIMSC);
1206
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001207 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301208 }
1209}
1210
1211void nmk_gpio_wakeups_resume(void)
1212{
1213 int i;
1214
1215 for (i = 0; i < NUM_BANKS; i++) {
1216 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1217
1218 if (!chip)
1219 break;
1220
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001221 clk_enable(chip->clk);
1222
Rabin Vincentb9df4682011-02-10 11:45:58 +05301223 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1224 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1225
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001226 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301227 }
1228}
1229
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001230/*
1231 * Read the pull up/pull down status.
1232 * A bit set in 'pull_up' means that pull up
1233 * is selected if pull is enabled in PDIS register.
1234 * Note: only pull up/down set via this driver can
1235 * be detected due to HW limitations.
1236 */
1237void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1238{
1239 if (gpio_bank < NUM_BANKS) {
1240 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1241
1242 if (!chip)
1243 return;
1244
1245 *pull_up = chip->pull_up;
1246 }
1247}
1248
Lee Jonesa60b57e2012-04-19 21:36:31 +01001249int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1250 irq_hw_number_t hwirq)
1251{
1252 struct nmk_gpio_chip *nmk_chip = d->host_data;
1253
1254 if (!nmk_chip)
1255 return -EINVAL;
1256
1257 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1258 set_irq_flags(irq, IRQF_VALID);
1259 irq_set_chip_data(irq, nmk_chip);
1260 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1261
1262 return 0;
1263}
1264
1265const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1266 .map = nmk_gpio_irq_map,
1267 .xlate = irq_domain_xlate_twocell,
1268};
1269
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001270static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001271{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001272 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001273 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001274 struct nmk_gpio_chip *nmk_chip;
1275 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001276 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001277 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301278 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001279 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001280 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001281 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001282 int ret;
1283
Lee Jones513c27f2012-04-13 15:05:05 +01001284 if (!pdata && !np) {
1285 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001286 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001287 }
1288
1289 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001290 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001291 if (!pdata)
1292 return -ENOMEM;
1293
Lee Jones612e1d52012-06-14 11:27:56 +01001294 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001295 pdata->supports_sleepmode = true;
1296
1297 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1298 dev_err(&dev->dev, "gpio-bank property not found\n");
1299 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001300 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001301 }
1302
1303 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1304 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1305 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001306
1307 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1308 if (!res) {
1309 ret = -ENOENT;
1310 goto out;
1311 }
1312
1313 irq = platform_get_irq(dev, 0);
1314 if (irq < 0) {
1315 ret = irq;
1316 goto out;
1317 }
1318
Rabin Vincent33b744b2010-10-14 10:38:03 +05301319 secondary_irq = platform_get_irq(dev, 1);
1320 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1321 ret = -EINVAL;
1322 goto out;
1323 }
1324
Linus Walleij5e754f32012-07-03 23:05:14 +02001325 base = devm_request_and_ioremap(&dev->dev, res);
1326 if (!base) {
1327 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001328 goto out;
1329 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001330
Linus Walleij5e754f32012-07-03 23:05:14 +02001331 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001332 if (IS_ERR(clk)) {
1333 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001334 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001335 }
Linus Walleijefec3812012-06-06 22:50:41 +02001336 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001337
Linus Walleij5e754f32012-07-03 23:05:14 +02001338 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001339 if (!nmk_chip) {
1340 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001341 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001342 }
Lee Jones513c27f2012-04-13 15:05:05 +01001343
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001344 /*
1345 * The virt address in nmk_chip->addr is in the nomadik register space,
1346 * so we can simply convert the resource address, without remapping
1347 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301348 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001349 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001350 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001351 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001352 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301353 nmk_chip->secondary_parent_irq = secondary_irq;
1354 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301355 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001356 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001357 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001358
1359 chip = &nmk_chip->chip;
1360 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301361 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301362 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001363 chip->dev = &dev->dev;
1364 chip->owner = THIS_MODULE;
1365
Rabin Vincentebc61782011-09-28 15:49:11 +05301366 clk_enable(nmk_chip->clk);
1367 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1368 clk_disable(nmk_chip->clk);
1369
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001370#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001371 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001372#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001373
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001374 ret = gpiochip_add(&nmk_chip->chip);
1375 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001376 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001377
Rabin Vincent01727e62010-12-13 12:02:40 +05301378 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1379
1380 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001381
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001382 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001383
Linus Walleij51f58c62012-10-11 16:33:44 +02001384 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001385 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001386 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001387 NMK_GPIO_PER_CHIP, irq_start,
1388 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001389 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001390 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001391 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001392 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001393 }
1394
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001395 nmk_gpio_init_irq(nmk_chip);
1396
Lee Jones513c27f2012-04-13 15:05:05 +01001397 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1398
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001399 return 0;
1400
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001401out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001402 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1403 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001404
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001405 return ret;
1406}
1407
Linus Walleije98ea772012-04-26 23:57:25 +02001408static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1409{
1410 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1411
1412 return npct->soc->ngroups;
1413}
1414
1415static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1416 unsigned selector)
1417{
1418 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1419
1420 return npct->soc->groups[selector].name;
1421}
1422
1423static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1424 const unsigned **pins,
1425 unsigned *num_pins)
1426{
1427 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1428
1429 *pins = npct->soc->groups[selector].pins;
1430 *num_pins = npct->soc->groups[selector].npins;
1431 return 0;
1432}
1433
Linus Walleij24cbdd72012-05-02 21:28:00 +02001434static struct pinctrl_gpio_range *
1435nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1436{
1437 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1438 int i;
1439
1440 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1441 struct pinctrl_gpio_range *range;
1442
1443 range = &npct->soc->gpio_ranges[i];
1444 if (offset >= range->pin_base &&
1445 offset <= (range->pin_base + range->npins - 1))
1446 return range;
1447 }
1448 return NULL;
1449}
1450
Linus Walleije98ea772012-04-26 23:57:25 +02001451static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1452 unsigned offset)
1453{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001454 struct pinctrl_gpio_range *range;
1455 struct gpio_chip *chip;
1456
1457 range = nmk_match_gpio_range(pctldev, offset);
1458 if (!range || !range->gc) {
1459 seq_printf(s, "invalid pin offset");
1460 return;
1461 }
1462 chip = range->gc;
1463 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001464}
1465
1466static struct pinctrl_ops nmk_pinctrl_ops = {
1467 .get_groups_count = nmk_get_groups_cnt,
1468 .get_group_name = nmk_get_group_name,
1469 .get_group_pins = nmk_get_group_pins,
1470 .pin_dbg_show = nmk_pin_dbg_show,
1471};
1472
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001473static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1474{
1475 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1476
1477 return npct->soc->nfunctions;
1478}
1479
1480static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1481 unsigned function)
1482{
1483 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1484
1485 return npct->soc->functions[function].name;
1486}
1487
1488static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1489 unsigned function,
1490 const char * const **groups,
1491 unsigned * const num_groups)
1492{
1493 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1494
1495 *groups = npct->soc->functions[function].groups;
1496 *num_groups = npct->soc->functions[function].ngroups;
1497
1498 return 0;
1499}
1500
1501static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1502 unsigned group)
1503{
1504 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1505 const struct nmk_pingroup *g;
1506 static unsigned int slpm[NUM_BANKS];
1507 unsigned long flags;
1508 bool glitch;
1509 int ret = -EINVAL;
1510 int i;
1511
1512 g = &npct->soc->groups[group];
1513
1514 if (g->altsetting < 0)
1515 return -EINVAL;
1516
1517 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1518
Linus Walleijdaf73172012-05-22 11:46:45 +02001519 /*
1520 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1521 * we may pass through an undesired state. In this case we take
1522 * some extra care.
1523 *
1524 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1525 * - Save SLPM registers (since we have a shadow register in the
1526 * nmk_chip we're using that as backup)
1527 * - Set SLPM=0 for the IOs you want to switch and others to 1
1528 * - Configure the GPIO registers for the IOs that are being switched
1529 * - Set IOFORCE=1
1530 * - Modify the AFLSA/B registers for the IOs that are being switched
1531 * - Set IOFORCE=0
1532 * - Restore SLPM registers
1533 * - Any spurious wake up event during switch sequence to be ignored
1534 * and cleared
1535 *
1536 * We REALLY need to save ALL slpm registers, because the external
1537 * IOFORCE will switch *all* ports to their sleepmode setting to as
1538 * to avoid glitches. (Not just one port!)
1539 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001540 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001541
1542 if (glitch) {
1543 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1544
1545 /* Initially don't put any pins to sleep when switching */
1546 memset(slpm, 0xff, sizeof(slpm));
1547
1548 /*
1549 * Then mask the pins that need to be sleeping now when we're
1550 * switching to the ALT C function.
1551 */
1552 for (i = 0; i < g->npins; i++)
1553 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1554 nmk_gpio_glitch_slpm_init(slpm);
1555 }
1556
1557 for (i = 0; i < g->npins; i++) {
1558 struct pinctrl_gpio_range *range;
1559 struct nmk_gpio_chip *nmk_chip;
1560 struct gpio_chip *chip;
1561 unsigned bit;
1562
1563 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1564 if (!range) {
1565 dev_err(npct->dev,
1566 "invalid pin offset %d in group %s at index %d\n",
1567 g->pins[i], g->name, i);
1568 goto out_glitch;
1569 }
1570 if (!range->gc) {
1571 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1572 g->pins[i], g->name, i);
1573 goto out_glitch;
1574 }
1575 chip = range->gc;
1576 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1577 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1578
1579 clk_enable(nmk_chip->clk);
1580 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1581 /*
1582 * If the pin is switching to altfunc, and there was an
1583 * interrupt installed on it which has been lazy disabled,
1584 * actually mask the interrupt to prevent spurious interrupts
1585 * that would occur while the pin is under control of the
1586 * peripheral. Only SKE does this.
1587 */
1588 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1589
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001590 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1591 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001592 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001593
1594 /*
1595 * Call PRCM GPIOCR config function in case ALTC
1596 * has been selected:
1597 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1598 * must be set.
1599 * - If selection is pure ALTC and previous selection was ALTCx,
1600 * then some bits in PRCM GPIOCR registers must be cleared.
1601 */
1602 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1603 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1604 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001605 }
1606
1607 /* When all pins are successfully reconfigured we get here */
1608 ret = 0;
1609
1610out_glitch:
1611 if (glitch) {
1612 nmk_gpio_glitch_slpm_restore(slpm);
1613 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1614 }
1615
1616 return ret;
1617}
1618
1619static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1620 unsigned function, unsigned group)
1621{
1622 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1623 const struct nmk_pingroup *g;
1624
1625 g = &npct->soc->groups[group];
1626
1627 if (g->altsetting < 0)
1628 return;
1629
1630 /* Poke out the mux, set the pin to some default state? */
1631 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1632}
1633
1634int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1635 struct pinctrl_gpio_range *range,
1636 unsigned offset)
1637{
1638 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1639 struct nmk_gpio_chip *nmk_chip;
1640 struct gpio_chip *chip;
1641 unsigned bit;
1642
1643 if (!range) {
1644 dev_err(npct->dev, "invalid range\n");
1645 return -EINVAL;
1646 }
1647 if (!range->gc) {
1648 dev_err(npct->dev, "missing GPIO chip in range\n");
1649 return -EINVAL;
1650 }
1651 chip = range->gc;
1652 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1653
1654 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1655
1656 clk_enable(nmk_chip->clk);
1657 bit = offset % NMK_GPIO_PER_CHIP;
1658 /* There is no glitch when converting any pin to GPIO */
1659 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1660 clk_disable(nmk_chip->clk);
1661
1662 return 0;
1663}
1664
1665void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1666 struct pinctrl_gpio_range *range,
1667 unsigned offset)
1668{
1669 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1670
1671 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1672 /* Set the pin to some default state, GPIO is usually default */
1673}
1674
1675static struct pinmux_ops nmk_pinmux_ops = {
1676 .get_functions_count = nmk_pmx_get_funcs_cnt,
1677 .get_function_name = nmk_pmx_get_func_name,
1678 .get_function_groups = nmk_pmx_get_func_groups,
1679 .enable = nmk_pmx_enable,
1680 .disable = nmk_pmx_disable,
1681 .gpio_request_enable = nmk_gpio_request_enable,
1682 .gpio_disable_free = nmk_gpio_disable_free,
1683};
1684
Linus Walleijd41af622012-05-03 15:58:12 +02001685int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1686 unsigned pin,
1687 unsigned long *config)
1688{
1689 /* Not implemented */
1690 return -EINVAL;
1691}
1692
1693int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1694 unsigned pin,
1695 unsigned long config)
1696{
1697 static const char *pullnames[] = {
1698 [NMK_GPIO_PULL_NONE] = "none",
1699 [NMK_GPIO_PULL_UP] = "up",
1700 [NMK_GPIO_PULL_DOWN] = "down",
1701 [3] /* illegal */ = "??"
1702 };
1703 static const char *slpmnames[] = {
1704 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1705 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1706 };
1707 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1708 struct nmk_gpio_chip *nmk_chip;
1709 struct pinctrl_gpio_range *range;
1710 struct gpio_chip *chip;
1711 unsigned bit;
1712
1713 /*
1714 * The pin config contains pin number and altfunction fields, here
1715 * we just ignore that part. It's being handled by the framework and
1716 * pinmux callback respectively.
1717 */
1718 pin_cfg_t cfg = (pin_cfg_t) config;
1719 int pull = PIN_PULL(cfg);
1720 int slpm = PIN_SLPM(cfg);
1721 int output = PIN_DIR(cfg);
1722 int val = PIN_VAL(cfg);
1723 bool lowemi = PIN_LOWEMI(cfg);
1724 bool gpiomode = PIN_GPIOMODE(cfg);
1725 bool sleep = PIN_SLEEPMODE(cfg);
1726
1727 range = nmk_match_gpio_range(pctldev, pin);
1728 if (!range) {
1729 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1730 return -EINVAL;
1731 }
1732 if (!range->gc) {
1733 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1734 pin);
1735 return -EINVAL;
1736 }
1737 chip = range->gc;
1738 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1739
1740 if (sleep) {
1741 int slpm_pull = PIN_SLPM_PULL(cfg);
1742 int slpm_output = PIN_SLPM_DIR(cfg);
1743 int slpm_val = PIN_SLPM_VAL(cfg);
1744
1745 /* All pins go into GPIO mode at sleep */
1746 gpiomode = true;
1747
1748 /*
1749 * The SLPM_* values are normal values + 1 to allow zero to
1750 * mean "same as normal".
1751 */
1752 if (slpm_pull)
1753 pull = slpm_pull - 1;
1754 if (slpm_output)
1755 output = slpm_output - 1;
1756 if (slpm_val)
1757 val = slpm_val - 1;
1758
1759 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1760 pin,
1761 slpm_pull ? pullnames[pull] : "same",
1762 slpm_output ? (output ? "output" : "input") : "same",
1763 slpm_val ? (val ? "high" : "low") : "same");
1764 }
1765
1766 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1767 pin, cfg, pullnames[pull], slpmnames[slpm],
1768 output ? "output " : "input",
1769 output ? (val ? "high" : "low") : "",
1770 lowemi ? "on" : "off" );
1771
1772 clk_enable(nmk_chip->clk);
1773 bit = pin % NMK_GPIO_PER_CHIP;
1774 if (gpiomode)
1775 /* No glitch when going to GPIO mode */
1776 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1777 if (output)
1778 __nmk_gpio_make_output(nmk_chip, bit, val);
1779 else {
1780 __nmk_gpio_make_input(nmk_chip, bit);
1781 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1782 }
1783 /* TODO: isn't this only applicable on output pins? */
1784 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1785
1786 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1787 clk_disable(nmk_chip->clk);
1788 return 0;
1789}
1790
1791static struct pinconf_ops nmk_pinconf_ops = {
1792 .pin_config_get = nmk_pin_config_get,
1793 .pin_config_set = nmk_pin_config_set,
1794};
1795
Linus Walleije98ea772012-04-26 23:57:25 +02001796static struct pinctrl_desc nmk_pinctrl_desc = {
1797 .name = "pinctrl-nomadik",
1798 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001799 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001800 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001801 .owner = THIS_MODULE,
1802};
1803
Lee Jones855f80c2012-05-26 06:09:29 +01001804static const struct of_device_id nmk_pinctrl_match[] = {
1805 {
1806 .compatible = "stericsson,nmk_pinctrl",
1807 .data = (void *)PINCTRL_NMK_DB8500,
1808 },
1809 {},
1810};
1811
Linus Walleije98ea772012-04-26 23:57:25 +02001812static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1813{
1814 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001815 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001816 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01001817 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001818 int i;
1819
1820 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1821 if (!npct)
1822 return -ENOMEM;
1823
Lee Jones855f80c2012-05-26 06:09:29 +01001824 if (platid)
1825 version = platid->driver_data;
1826 else if (np)
1827 version = (unsigned int)
1828 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1829
Linus Walleije98ea772012-04-26 23:57:25 +02001830 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001831 if (version == PINCTRL_NMK_STN8815)
1832 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001833 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001834 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001835 if (version == PINCTRL_NMK_DB8540)
1836 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001837
1838 /*
1839 * We need all the GPIO drivers to probe FIRST, or we will not be able
1840 * to obtain references to the struct gpio_chip * for them, and we
1841 * need this to proceed.
1842 */
1843 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001844 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001845 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001846 return -EPROBE_DEFER;
1847 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001848 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001849 }
1850
1851 nmk_pinctrl_desc.pins = npct->soc->pins;
1852 nmk_pinctrl_desc.npins = npct->soc->npins;
1853 npct->dev = &pdev->dev;
1854 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1855 if (!npct->pctl) {
1856 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1857 return -EINVAL;
1858 }
1859
1860 /* We will handle a range of GPIO pins */
1861 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1862 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1863
1864 platform_set_drvdata(pdev, npct);
1865 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1866
1867 return 0;
1868}
1869
Lee Jones513c27f2012-04-13 15:05:05 +01001870static const struct of_device_id nmk_gpio_match[] = {
1871 { .compatible = "st,nomadik-gpio", },
1872 {}
1873};
1874
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001875static struct platform_driver nmk_gpio_driver = {
1876 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001877 .owner = THIS_MODULE,
1878 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001879 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301880 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001881 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001882};
1883
Linus Walleije98ea772012-04-26 23:57:25 +02001884static const struct platform_device_id nmk_pinctrl_id[] = {
1885 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1886 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001887 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Linus Walleije98ea772012-04-26 23:57:25 +02001888};
1889
1890static struct platform_driver nmk_pinctrl_driver = {
1891 .driver = {
1892 .owner = THIS_MODULE,
1893 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001894 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001895 },
1896 .probe = nmk_pinctrl_probe,
1897 .id_table = nmk_pinctrl_id,
1898};
1899
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001900static int __init nmk_gpio_init(void)
1901{
Linus Walleije98ea772012-04-26 23:57:25 +02001902 int ret;
1903
1904 ret = platform_driver_register(&nmk_gpio_driver);
1905 if (ret)
1906 return ret;
1907 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001908}
1909
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001910core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001911
1912MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1913MODULE_DESCRIPTION("Nomadik GPIO Driver");
1914MODULE_LICENSE("GPL");