blob: 8bfbad60fcee1183a646288b89d498434ab6c87f [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
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010047
Will Deaconadfed152011-02-28 10:12:29 +000048#include <asm/mach/irq.h>
49
Rabin Vincent378be062010-06-02 06:06:29 +010050#include <plat/pincfg.h>
Linus Walleij0f332862011-08-22 08:33:30 +010051#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010052
Linus Walleije98ea772012-04-26 23:57:25 +020053#include "pinctrl-nomadik.h"
54
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010055/*
56 * The GPIO module in the Nomadik family of Systems-on-Chip is an
57 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020058 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010059 *
60 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
61 */
62
Rabin Vincent01727e62010-12-13 12:02:40 +053063#define NMK_GPIO_PER_CHIP 32
64
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010065struct nmk_gpio_chip {
66 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010067 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010068 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010069 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053070 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010071 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053072 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053073 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053074 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010075 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020076 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010077 /* Keep track of configured edges */
78 u32 edge_rising;
79 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053080 u32 real_wake;
81 u32 rwimsc;
82 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053083 u32 rimsc;
84 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020085 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053086 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010087};
88
Linus Walleije98ea772012-04-26 23:57:25 +020089struct nmk_pinctrl {
90 struct device *dev;
91 struct pinctrl_dev *pctl;
92 const struct nmk_pinctrl_soc_data *soc;
93};
94
Rabin Vincent01727e62010-12-13 12:02:40 +053095static struct nmk_gpio_chip *
96nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
97
98static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
99
100#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
101
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100102static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
103 unsigned offset, int gpio_mode)
104{
105 u32 bit = 1 << offset;
106 u32 afunc, bfunc;
107
108 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
109 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
110 if (gpio_mode & NMK_GPIO_ALT_A)
111 afunc |= bit;
112 if (gpio_mode & NMK_GPIO_ALT_B)
113 bfunc |= bit;
114 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
115 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
116}
117
Rabin Vincent81a3c292010-05-27 12:39:23 +0100118static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
119 unsigned offset, enum nmk_gpio_slpm mode)
120{
121 u32 bit = 1 << offset;
122 u32 slpm;
123
124 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
125 if (mode == NMK_GPIO_SLPM_NOCHANGE)
126 slpm |= bit;
127 else
128 slpm &= ~bit;
129 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
130}
131
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100132static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
133 unsigned offset, enum nmk_gpio_pull pull)
134{
135 u32 bit = 1 << offset;
136 u32 pdis;
137
138 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200139 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100140 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200141 nmk_chip->pull_up &= ~bit;
142 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100143 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200144 }
145
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100146 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
147
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200148 if (pull == NMK_GPIO_PULL_UP) {
149 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100150 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200151 } else if (pull == NMK_GPIO_PULL_DOWN) {
152 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100153 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200154 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100155}
156
Rabin Vincentebc61782011-09-28 15:49:11 +0530157static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
158 unsigned offset, bool lowemi)
159{
160 u32 bit = BIT(offset);
161 bool enabled = nmk_chip->lowemi & bit;
162
163 if (lowemi == enabled)
164 return;
165
166 if (lowemi)
167 nmk_chip->lowemi |= bit;
168 else
169 nmk_chip->lowemi &= ~bit;
170
171 writel_relaxed(nmk_chip->lowemi,
172 nmk_chip->addr + NMK_GPIO_LOWEMI);
173}
174
Rabin Vincent378be062010-06-02 06:06:29 +0100175static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
176 unsigned offset)
177{
178 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
179}
180
Rabin Vincent6720db72010-09-02 11:28:48 +0100181static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
182 unsigned offset, int val)
183{
184 if (val)
185 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
186 else
187 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
188}
189
190static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
191 unsigned offset, int val)
192{
193 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
194 __nmk_gpio_set_output(nmk_chip, offset, val);
195}
196
Rabin Vincent01727e62010-12-13 12:02:40 +0530197static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
198 unsigned offset, int gpio_mode,
199 bool glitch)
200{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530201 u32 rwimsc = nmk_chip->rwimsc;
202 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530203
204 if (glitch && nmk_chip->set_ioforce) {
205 u32 bit = BIT(offset);
206
Rabin Vincent01727e62010-12-13 12:02:40 +0530207 /* Prevent spurious wakeups */
208 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
209 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
210
211 nmk_chip->set_ioforce(true);
212 }
213
214 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
215
216 if (glitch && nmk_chip->set_ioforce) {
217 nmk_chip->set_ioforce(false);
218
219 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
220 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
221 }
222}
223
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530224static void
225nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
226{
227 u32 falling = nmk_chip->fimsc & BIT(offset);
228 u32 rising = nmk_chip->rimsc & BIT(offset);
229 int gpio = nmk_chip->chip.base + offset;
230 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
231 struct irq_data *d = irq_get_irq_data(irq);
232
233 if (!rising && !falling)
234 return;
235
236 if (!d || !irqd_irq_disabled(d))
237 return;
238
239 if (rising) {
240 nmk_chip->rimsc &= ~BIT(offset);
241 writel_relaxed(nmk_chip->rimsc,
242 nmk_chip->addr + NMK_GPIO_RIMSC);
243 }
244
245 if (falling) {
246 nmk_chip->fimsc &= ~BIT(offset);
247 writel_relaxed(nmk_chip->fimsc,
248 nmk_chip->addr + NMK_GPIO_FIMSC);
249 }
250
251 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
252}
253
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200254static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
255 unsigned offset, unsigned alt_num)
256{
257 int i;
258 u16 reg;
259 u8 bit;
260 u8 alt_index;
261 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
262 const u16 *gpiocr_regs;
263
264 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
265 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
266 alt_num);
267 return;
268 }
269
270 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
271 if (npct->soc->altcx_pins[i].pin == offset)
272 break;
273 }
274 if (i == npct->soc->npins_altcx) {
275 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
276 offset);
277 return;
278 }
279
280 pin_desc = npct->soc->altcx_pins + i;
281 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
282
283 /*
284 * If alt_num is NULL, just clear current ALTCx selection
285 * to make sure we come back to a pure ALTC selection
286 */
287 if (!alt_num) {
288 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
289 if (pin_desc->altcx[i].used == true) {
290 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
291 bit = pin_desc->altcx[i].control_bit;
292 if (prcmu_read(reg) & BIT(bit)) {
293 prcmu_write_masked(reg, BIT(bit), 0);
294 dev_dbg(npct->dev,
295 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
296 offset, i+1);
297 }
298 }
299 }
300 return;
301 }
302
303 alt_index = alt_num - 1;
304 if (pin_desc->altcx[alt_index].used == false) {
305 dev_warn(npct->dev,
306 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
307 offset, alt_num);
308 return;
309 }
310
311 /*
312 * Check if any other ALTCx functions are activated on this pin
313 * and disable it first.
314 */
315 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
316 if (i == alt_index)
317 continue;
318 if (pin_desc->altcx[i].used == true) {
319 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
320 bit = pin_desc->altcx[i].control_bit;
321 if (prcmu_read(reg) & BIT(bit)) {
322 prcmu_write_masked(reg, BIT(bit), 0);
323 dev_dbg(npct->dev,
324 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
325 offset, i+1);
326 }
327 }
328 }
329
330 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
331 bit = pin_desc->altcx[alt_index].control_bit;
332 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
333 offset, alt_index+1);
334 prcmu_write_masked(reg, BIT(bit), BIT(bit));
335}
336
Rabin Vincent378be062010-06-02 06:06:29 +0100337static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530338 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100339{
340 static const char *afnames[] = {
341 [NMK_GPIO_ALT_GPIO] = "GPIO",
342 [NMK_GPIO_ALT_A] = "A",
343 [NMK_GPIO_ALT_B] = "B",
344 [NMK_GPIO_ALT_C] = "C"
345 };
346 static const char *pullnames[] = {
347 [NMK_GPIO_PULL_NONE] = "none",
348 [NMK_GPIO_PULL_UP] = "up",
349 [NMK_GPIO_PULL_DOWN] = "down",
350 [3] /* illegal */ = "??"
351 };
352 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100353 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
354 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100355 };
356
357 int pin = PIN_NUM(cfg);
358 int pull = PIN_PULL(cfg);
359 int af = PIN_ALT(cfg);
360 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100361 int output = PIN_DIR(cfg);
362 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530363 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100364
Rabin Vincentdacdc962010-12-03 20:35:37 +0530365 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
366 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100367 output ? "output " : "input",
368 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100369
Rabin Vincentdacdc962010-12-03 20:35:37 +0530370 if (sleep) {
371 int slpm_pull = PIN_SLPM_PULL(cfg);
372 int slpm_output = PIN_SLPM_DIR(cfg);
373 int slpm_val = PIN_SLPM_VAL(cfg);
374
Rabin Vincent3546d152010-11-25 11:38:27 +0530375 af = NMK_GPIO_ALT_GPIO;
376
Rabin Vincentdacdc962010-12-03 20:35:37 +0530377 /*
378 * The SLPM_* values are normal values + 1 to allow zero to
379 * mean "same as normal".
380 */
381 if (slpm_pull)
382 pull = slpm_pull - 1;
383 if (slpm_output)
384 output = slpm_output - 1;
385 if (slpm_val)
386 val = slpm_val - 1;
387
388 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
389 pin,
390 slpm_pull ? pullnames[pull] : "same",
391 slpm_output ? (output ? "output" : "input") : "same",
392 slpm_val ? (val ? "high" : "low") : "same");
393 }
394
Rabin Vincent6720db72010-09-02 11:28:48 +0100395 if (output)
396 __nmk_gpio_make_output(nmk_chip, offset, val);
397 else {
398 __nmk_gpio_make_input(nmk_chip, offset);
399 __nmk_gpio_set_pull(nmk_chip, offset, pull);
400 }
401
Rabin Vincentebc61782011-09-28 15:49:11 +0530402 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
403
Rabin Vincent01727e62010-12-13 12:02:40 +0530404 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530405 * If the pin is switching to altfunc, and there was an interrupt
406 * installed on it which has been lazy disabled, actually mask the
407 * interrupt to prevent spurious interrupts that would occur while the
408 * pin is under control of the peripheral. Only SKE does this.
409 */
410 if (af != NMK_GPIO_ALT_GPIO)
411 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
412
413 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530414 * If we've backed up the SLPM registers (glitch workaround), modify
415 * the backups since they will be restored.
416 */
417 if (slpmregs) {
418 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
419 slpmregs[nmk_chip->bank] |= BIT(offset);
420 else
421 slpmregs[nmk_chip->bank] &= ~BIT(offset);
422 } else
423 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
424
425 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
426}
427
428/*
429 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
430 * - Save SLPM registers
431 * - Set SLPM=0 for the IOs you want to switch and others to 1
432 * - Configure the GPIO registers for the IOs that are being switched
433 * - Set IOFORCE=1
434 * - Modify the AFLSA/B registers for the IOs that are being switched
435 * - Set IOFORCE=0
436 * - Restore SLPM registers
437 * - Any spurious wake up event during switch sequence to be ignored and
438 * cleared
439 */
440static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
441{
442 int i;
443
444 for (i = 0; i < NUM_BANKS; i++) {
445 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
446 unsigned int temp = slpm[i];
447
448 if (!chip)
449 break;
450
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200451 clk_enable(chip->clk);
452
Rabin Vincent01727e62010-12-13 12:02:40 +0530453 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
454 writel(temp, chip->addr + NMK_GPIO_SLPC);
455 }
456}
457
458static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
459{
460 int i;
461
462 for (i = 0; i < NUM_BANKS; i++) {
463 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
464
465 if (!chip)
466 break;
467
468 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200469
470 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530471 }
472}
473
474static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
475{
476 static unsigned int slpm[NUM_BANKS];
477 unsigned long flags;
478 bool glitch = false;
479 int ret = 0;
480 int i;
481
482 for (i = 0; i < num; i++) {
483 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
484 glitch = true;
485 break;
486 }
487 }
488
489 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
490
491 if (glitch) {
492 memset(slpm, 0xff, sizeof(slpm));
493
494 for (i = 0; i < num; i++) {
495 int pin = PIN_NUM(cfgs[i]);
496 int offset = pin % NMK_GPIO_PER_CHIP;
497
498 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
499 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
500 }
501
502 nmk_gpio_glitch_slpm_init(slpm);
503 }
504
505 for (i = 0; i < num; i++) {
506 struct nmk_gpio_chip *nmk_chip;
507 int pin = PIN_NUM(cfgs[i]);
508
Lee Jonesa60b57e2012-04-19 21:36:31 +0100509 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530510 if (!nmk_chip) {
511 ret = -EINVAL;
512 break;
513 }
514
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200515 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530516 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100517 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530518 cfgs[i], sleep, glitch ? slpm : NULL);
519 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200520 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530521 }
522
523 if (glitch)
524 nmk_gpio_glitch_slpm_restore(slpm);
525
526 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
527
528 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100529}
530
531/**
532 * nmk_config_pin - configure a pin's mux attributes
533 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200534 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100535 * Configures a pin's mode (alternate function or GPIO), its pull up status,
536 * and its sleep mode based on the specified configuration. The @cfg is
537 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
538 * are constructed using, and can be further enhanced with, the macros in
539 * plat/pincfg.h.
540 *
541 * If a pin's mode is set to GPIO, it is configured as an input to avoid
542 * side-effects. The gpio can be manipulated later using standard GPIO API
543 * calls.
544 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530545int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100546{
Rabin Vincent01727e62010-12-13 12:02:40 +0530547 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100548}
549EXPORT_SYMBOL(nmk_config_pin);
550
551/**
552 * nmk_config_pins - configure several pins at once
553 * @cfgs: array of pin configurations
554 * @num: number of elments in the array
555 *
556 * Configures several pins using nmk_config_pin(). Refer to that function for
557 * further information.
558 */
559int nmk_config_pins(pin_cfg_t *cfgs, int num)
560{
Rabin Vincent01727e62010-12-13 12:02:40 +0530561 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100562}
563EXPORT_SYMBOL(nmk_config_pins);
564
Rabin Vincentdacdc962010-12-03 20:35:37 +0530565int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
566{
Rabin Vincent01727e62010-12-13 12:02:40 +0530567 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530568}
569EXPORT_SYMBOL(nmk_config_pins_sleep);
570
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100571/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100572 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
573 * @gpio: pin number
574 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
575 *
Linus Walleij33d78642011-06-09 11:08:47 +0200576 * This register is actually in the pinmux layer, not the GPIO block itself.
577 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
578 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
579 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
580 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
581 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
582 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100583 *
Linus Walleij33d78642011-06-09 11:08:47 +0200584 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
585 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
586 * entered) regardless of the altfunction selected. Also wake-up detection is
587 * ENABLED.
588 *
589 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
590 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
591 * (for altfunction GPIO) or respective on-chip peripherals (for other
592 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
593 *
594 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100595 */
596int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
597{
598 struct nmk_gpio_chip *nmk_chip;
599 unsigned long flags;
600
Lee Jonesa60b57e2012-04-19 21:36:31 +0100601 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100602 if (!nmk_chip)
603 return -EINVAL;
604
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200605 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530606 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
607 spin_lock(&nmk_chip->lock);
608
Lee Jonesa60b57e2012-04-19 21:36:31 +0100609 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530610
611 spin_unlock(&nmk_chip->lock);
612 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200613 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100614
615 return 0;
616}
617
618/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100619 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
620 * @gpio: pin number
621 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
622 *
623 * Enables/disables pull up/down on a specified pin. This only takes effect if
624 * the pin is configured as an input (either explicitly or by the alternate
625 * function).
626 *
627 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
628 * configured as an input. Otherwise, due to the way the controller registers
629 * work, this function will change the value output on the pin.
630 */
631int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
632{
633 struct nmk_gpio_chip *nmk_chip;
634 unsigned long flags;
635
Lee Jonesa60b57e2012-04-19 21:36:31 +0100636 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100637 if (!nmk_chip)
638 return -EINVAL;
639
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200640 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100641 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100642 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100643 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200644 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100645
646 return 0;
647}
648
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100649/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200650/**
651 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
652 * @gpio: pin number
653 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
654 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
655 *
656 * Sets the mode of the specified pin to one of the alternate functions or
657 * plain GPIO.
658 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100659int nmk_gpio_set_mode(int gpio, int gpio_mode)
660{
661 struct nmk_gpio_chip *nmk_chip;
662 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100663
Lee Jonesa60b57e2012-04-19 21:36:31 +0100664 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100665 if (!nmk_chip)
666 return -EINVAL;
667
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200668 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100670 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100671 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200672 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100673
674 return 0;
675}
676EXPORT_SYMBOL(nmk_gpio_set_mode);
677
678int nmk_gpio_get_mode(int gpio)
679{
680 struct nmk_gpio_chip *nmk_chip;
681 u32 afunc, bfunc, bit;
682
Lee Jonesa60b57e2012-04-19 21:36:31 +0100683 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100684 if (!nmk_chip)
685 return -EINVAL;
686
Lee Jonesa60b57e2012-04-19 21:36:31 +0100687 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100688
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200689 clk_enable(nmk_chip->clk);
690
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100691 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
692 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
693
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200694 clk_disable(nmk_chip->clk);
695
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100696 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
697}
698EXPORT_SYMBOL(nmk_gpio_get_mode);
699
700
701/* IRQ functions */
702static inline int nmk_gpio_get_bitmask(int gpio)
703{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100704 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100705}
706
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100707static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100708{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100709 struct nmk_gpio_chip *nmk_chip;
710
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100711 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100712 if (!nmk_chip)
713 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200714
715 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100716 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200717 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100718}
719
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100720enum nmk_gpio_irq_type {
721 NORMAL,
722 WAKE,
723};
724
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100725static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100726 int gpio, enum nmk_gpio_irq_type which,
727 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100728{
729 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530730 u32 *rimscval;
731 u32 *fimscval;
732 u32 rimscreg;
733 u32 fimscreg;
734
735 if (which == NORMAL) {
736 rimscreg = NMK_GPIO_RIMSC;
737 fimscreg = NMK_GPIO_FIMSC;
738 rimscval = &nmk_chip->rimsc;
739 fimscval = &nmk_chip->fimsc;
740 } else {
741 rimscreg = NMK_GPIO_RWIMSC;
742 fimscreg = NMK_GPIO_FWIMSC;
743 rimscval = &nmk_chip->rwimsc;
744 fimscval = &nmk_chip->fwimsc;
745 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100746
747 /* we must individually set/clear the two edges */
748 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100749 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530750 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100751 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530752 *rimscval &= ~bitmask;
753 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100754 }
755 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100756 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530757 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100758 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530759 *fimscval &= ~bitmask;
760 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100761 }
762}
763
Rabin Vincentb9df4682011-02-10 11:45:58 +0530764static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
765 int gpio, bool on)
766{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530767 /*
768 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
769 * disabled, since setting SLPM to 1 increases power consumption, and
770 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
771 */
772 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200773 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530774 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200775 }
776
Rabin Vincentb9df4682011-02-10 11:45:58 +0530777 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
778}
779
780static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100781{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100782 struct nmk_gpio_chip *nmk_chip;
783 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100784 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100785
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100786 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100787 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100789 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100790
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200791 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530792 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
793 spin_lock(&nmk_chip->lock);
794
Lee Jonesa60b57e2012-04-19 21:36:31 +0100795 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530796
797 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100798 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530799
800 spin_unlock(&nmk_chip->lock);
801 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200802 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100803
804 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100805}
806
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100807static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100808{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530809 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100810}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100811
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100812static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100813{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530814 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100815}
816
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100817static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100818{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100819 struct nmk_gpio_chip *nmk_chip;
820 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530821 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100822
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100823 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100824 if (!nmk_chip)
825 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100826 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100827
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200828 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530829 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
830 spin_lock(&nmk_chip->lock);
831
Linus Walleij479a0c72011-09-20 10:50:15 +0200832 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100833 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530834
835 if (on)
836 nmk_chip->real_wake |= bitmask;
837 else
838 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530839
840 spin_unlock(&nmk_chip->lock);
841 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200842 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100843
844 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100845}
846
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100847static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100848{
Linus Walleij479a0c72011-09-20 10:50:15 +0200849 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200850 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100851 struct nmk_gpio_chip *nmk_chip;
852 unsigned long flags;
853 u32 bitmask;
854
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100855 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100856 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100857 if (!nmk_chip)
858 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100859 if (type & IRQ_TYPE_LEVEL_HIGH)
860 return -EINVAL;
861 if (type & IRQ_TYPE_LEVEL_LOW)
862 return -EINVAL;
863
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200864 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100865 spin_lock_irqsave(&nmk_chip->lock, flags);
866
Rabin Vincent7a852d82010-05-06 10:43:55 +0100867 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100868 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100869
Rabin Vincentb9df4682011-02-10 11:45:58 +0530870 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100871 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100872
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873 nmk_chip->edge_rising &= ~bitmask;
874 if (type & IRQ_TYPE_EDGE_RISING)
875 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100876
877 nmk_chip->edge_falling &= ~bitmask;
878 if (type & IRQ_TYPE_EDGE_FALLING)
879 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100880
881 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100882 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100883
Rabin Vincentb9df4682011-02-10 11:45:58 +0530884 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100885 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100886
887 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200888 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100889
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100890 return 0;
891}
892
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200893static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
894{
895 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
896
897 clk_enable(nmk_chip->clk);
898 nmk_gpio_irq_unmask(d);
899 return 0;
900}
901
902static void nmk_gpio_irq_shutdown(struct irq_data *d)
903{
904 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
905
906 nmk_gpio_irq_mask(d);
907 clk_disable(nmk_chip->clk);
908}
909
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100910static struct irq_chip nmk_gpio_irq_chip = {
911 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100912 .irq_ack = nmk_gpio_irq_ack,
913 .irq_mask = nmk_gpio_irq_mask,
914 .irq_unmask = nmk_gpio_irq_unmask,
915 .irq_set_type = nmk_gpio_irq_set_type,
916 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200917 .irq_startup = nmk_gpio_irq_startup,
918 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200919 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100920};
921
Rabin Vincent33b744b2010-10-14 10:38:03 +0530922static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
923 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100924{
925 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100926 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100927
Will Deaconadfed152011-02-28 10:12:29 +0000928 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100929
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100930 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530931 while (status) {
932 int bit = __ffs(status);
933
Linus Walleij95f0bc92012-09-27 14:14:09 +0200934 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530935 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100936 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100937
Will Deaconadfed152011-02-28 10:12:29 +0000938 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100939}
940
Rabin Vincent33b744b2010-10-14 10:38:03 +0530941static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
942{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100943 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200944 u32 status;
945
946 clk_enable(nmk_chip->clk);
947 status = readl(nmk_chip->addr + NMK_GPIO_IS);
948 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530949
950 __nmk_gpio_irq_handler(irq, desc, status);
951}
952
953static void nmk_gpio_secondary_irq_handler(unsigned int irq,
954 struct irq_desc *desc)
955{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100956 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530957 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
958
959 __nmk_gpio_irq_handler(irq, desc, status);
960}
961
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100962static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
963{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100964 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
965 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530966
967 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100968 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530969 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100970 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530971 }
972
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100973 return 0;
974}
975
976/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200977
978static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
979{
980 /*
981 * Map back to global GPIO space and request muxing, the direction
982 * parameter does not matter for this controller.
983 */
984 int gpio = chip->base + offset;
985
986 return pinctrl_request_gpio(gpio);
987}
988
989static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
990{
991 int gpio = chip->base + offset;
992
993 pinctrl_free_gpio(gpio);
994}
995
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100996static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
997{
998 struct nmk_gpio_chip *nmk_chip =
999 container_of(chip, struct nmk_gpio_chip, chip);
1000
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001001 clk_enable(nmk_chip->clk);
1002
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001003 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001004
1005 clk_disable(nmk_chip->clk);
1006
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001007 return 0;
1008}
1009
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001010static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1011{
1012 struct nmk_gpio_chip *nmk_chip =
1013 container_of(chip, struct nmk_gpio_chip, chip);
1014 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001015 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001016
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001017 clk_enable(nmk_chip->clk);
1018
1019 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1020
1021 clk_disable(nmk_chip->clk);
1022
1023 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001024}
1025
1026static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1027 int val)
1028{
1029 struct nmk_gpio_chip *nmk_chip =
1030 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001031
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001032 clk_enable(nmk_chip->clk);
1033
Rabin Vincent6720db72010-09-02 11:28:48 +01001034 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001035
1036 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001037}
1038
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001039static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1040 int val)
1041{
1042 struct nmk_gpio_chip *nmk_chip =
1043 container_of(chip, struct nmk_gpio_chip, chip);
1044
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001045 clk_enable(nmk_chip->clk);
1046
Rabin Vincent6720db72010-09-02 11:28:48 +01001047 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001048
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001049 clk_disable(nmk_chip->clk);
1050
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001051 return 0;
1052}
1053
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001054static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1055{
1056 struct nmk_gpio_chip *nmk_chip =
1057 container_of(chip, struct nmk_gpio_chip, chip);
1058
Linus Walleij268300b2012-10-19 17:06:54 +02001059 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001060}
1061
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301062#ifdef CONFIG_DEBUG_FS
1063
1064#include <linux/seq_file.h>
1065
Linus Walleij6f4350a2012-05-02 21:06:13 +02001066static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
1067 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301068{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001069 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301070 struct nmk_gpio_chip *nmk_chip =
1071 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001072 int mode;
1073 bool is_out;
1074 bool pull;
1075 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301076 const char *modes[] = {
1077 [NMK_GPIO_ALT_GPIO] = "gpio",
1078 [NMK_GPIO_ALT_A] = "altA",
1079 [NMK_GPIO_ALT_B] = "altB",
1080 [NMK_GPIO_ALT_C] = "altC",
1081 };
1082
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001083 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001084 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1085 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1086 mode = nmk_gpio_get_mode(gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001087
Linus Walleij6f4350a2012-05-02 21:06:13 +02001088 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1089 gpio, label ?: "(none)",
1090 is_out ? "out" : "in ",
1091 chip->get
1092 ? (chip->get(chip, offset) ? "hi" : "lo")
1093 : "? ",
1094 (mode < 0) ? "unknown" : modes[mode],
1095 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301096
Linus Walleij6f4350a2012-05-02 21:06:13 +02001097 if (label && !is_out) {
1098 int irq = gpio_to_irq(gpio);
1099 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001100
Linus Walleij6f4350a2012-05-02 21:06:13 +02001101 /* This races with request_irq(), set_irq_type(),
1102 * and set_irq_wake() ... but those are "rare".
1103 */
1104 if (irq >= 0 && desc->action) {
1105 char *trigger;
1106 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001107
Linus Walleij6f4350a2012-05-02 21:06:13 +02001108 if (nmk_chip->edge_rising & bitmask)
1109 trigger = "edge-rising";
1110 else if (nmk_chip->edge_falling & bitmask)
1111 trigger = "edge-falling";
1112 else
1113 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001114
Linus Walleij6f4350a2012-05-02 21:06:13 +02001115 seq_printf(s, " irq-%d %s%s",
1116 irq, trigger,
1117 irqd_is_wakeup_set(&desc->irq_data)
1118 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001119 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301120 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001121 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301122}
1123
Linus Walleij6f4350a2012-05-02 21:06:13 +02001124static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1125{
1126 unsigned i;
1127 unsigned gpio = chip->base;
1128
1129 for (i = 0; i < chip->ngpio; i++, gpio++) {
1130 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1131 seq_printf(s, "\n");
1132 }
1133}
1134
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301135#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001136static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1137 struct gpio_chip *chip,
1138 unsigned offset, unsigned gpio)
1139{
1140}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301141#define nmk_gpio_dbg_show NULL
1142#endif
1143
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001144/* This structure is replicated for each GPIO block allocated at probe time */
1145static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001146 .request = nmk_gpio_request,
1147 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001148 .direction_input = nmk_gpio_make_input,
1149 .get = nmk_gpio_get_input,
1150 .direction_output = nmk_gpio_make_output,
1151 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001152 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301153 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001154 .can_sleep = 0,
1155};
1156
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001157void nmk_gpio_clocks_enable(void)
1158{
1159 int i;
1160
1161 for (i = 0; i < NUM_BANKS; i++) {
1162 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1163
1164 if (!chip)
1165 continue;
1166
1167 clk_enable(chip->clk);
1168 }
1169}
1170
1171void nmk_gpio_clocks_disable(void)
1172{
1173 int i;
1174
1175 for (i = 0; i < NUM_BANKS; i++) {
1176 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1177
1178 if (!chip)
1179 continue;
1180
1181 clk_disable(chip->clk);
1182 }
1183}
1184
Rabin Vincentb9df4682011-02-10 11:45:58 +05301185/*
1186 * Called from the suspend/resume path to only keep the real wakeup interrupts
1187 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1188 * and not the rest of the interrupts which we needed to have as wakeups for
1189 * cpuidle.
1190 *
1191 * PM ops are not used since this needs to be done at the end, after all the
1192 * other drivers are done with their suspend callbacks.
1193 */
1194void nmk_gpio_wakeups_suspend(void)
1195{
1196 int i;
1197
1198 for (i = 0; i < NUM_BANKS; i++) {
1199 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1200
1201 if (!chip)
1202 break;
1203
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001204 clk_enable(chip->clk);
1205
Rabin Vincentb9df4682011-02-10 11:45:58 +05301206 writel(chip->rwimsc & chip->real_wake,
1207 chip->addr + NMK_GPIO_RWIMSC);
1208 writel(chip->fwimsc & chip->real_wake,
1209 chip->addr + NMK_GPIO_FWIMSC);
1210
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001211 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301212 }
1213}
1214
1215void nmk_gpio_wakeups_resume(void)
1216{
1217 int i;
1218
1219 for (i = 0; i < NUM_BANKS; i++) {
1220 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1221
1222 if (!chip)
1223 break;
1224
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001225 clk_enable(chip->clk);
1226
Rabin Vincentb9df4682011-02-10 11:45:58 +05301227 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1228 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1229
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001230 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301231 }
1232}
1233
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001234/*
1235 * Read the pull up/pull down status.
1236 * A bit set in 'pull_up' means that pull up
1237 * is selected if pull is enabled in PDIS register.
1238 * Note: only pull up/down set via this driver can
1239 * be detected due to HW limitations.
1240 */
1241void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1242{
1243 if (gpio_bank < NUM_BANKS) {
1244 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1245
1246 if (!chip)
1247 return;
1248
1249 *pull_up = chip->pull_up;
1250 }
1251}
1252
Lee Jonesa60b57e2012-04-19 21:36:31 +01001253int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1254 irq_hw_number_t hwirq)
1255{
1256 struct nmk_gpio_chip *nmk_chip = d->host_data;
1257
1258 if (!nmk_chip)
1259 return -EINVAL;
1260
1261 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1262 set_irq_flags(irq, IRQF_VALID);
1263 irq_set_chip_data(irq, nmk_chip);
1264 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1265
1266 return 0;
1267}
1268
1269const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1270 .map = nmk_gpio_irq_map,
1271 .xlate = irq_domain_xlate_twocell,
1272};
1273
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001274static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001275{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001276 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001277 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001278 struct nmk_gpio_chip *nmk_chip;
1279 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001280 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001281 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301282 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001283 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001284 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001285 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001286 int ret;
1287
Lee Jones513c27f2012-04-13 15:05:05 +01001288 if (!pdata && !np) {
1289 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001290 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001291 }
1292
1293 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001294 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001295 if (!pdata)
1296 return -ENOMEM;
1297
Lee Jones612e1d52012-06-14 11:27:56 +01001298 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001299 pdata->supports_sleepmode = true;
1300
1301 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1302 dev_err(&dev->dev, "gpio-bank property not found\n");
1303 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001304 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001305 }
1306
1307 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1308 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1309 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001310
1311 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1312 if (!res) {
1313 ret = -ENOENT;
1314 goto out;
1315 }
1316
1317 irq = platform_get_irq(dev, 0);
1318 if (irq < 0) {
1319 ret = irq;
1320 goto out;
1321 }
1322
Rabin Vincent33b744b2010-10-14 10:38:03 +05301323 secondary_irq = platform_get_irq(dev, 1);
1324 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1325 ret = -EINVAL;
1326 goto out;
1327 }
1328
Linus Walleij5e754f32012-07-03 23:05:14 +02001329 base = devm_request_and_ioremap(&dev->dev, res);
1330 if (!base) {
1331 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001332 goto out;
1333 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001334
Linus Walleij5e754f32012-07-03 23:05:14 +02001335 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001336 if (IS_ERR(clk)) {
1337 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001338 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001339 }
Linus Walleijefec3812012-06-06 22:50:41 +02001340 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001341
Linus Walleij5e754f32012-07-03 23:05:14 +02001342 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001343 if (!nmk_chip) {
1344 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001345 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001346 }
Lee Jones513c27f2012-04-13 15:05:05 +01001347
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001348 /*
1349 * The virt address in nmk_chip->addr is in the nomadik register space,
1350 * so we can simply convert the resource address, without remapping
1351 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301352 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001353 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001354 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001355 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001356 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301357 nmk_chip->secondary_parent_irq = secondary_irq;
1358 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301359 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001360 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001361 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001362
1363 chip = &nmk_chip->chip;
1364 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301365 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301366 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001367 chip->dev = &dev->dev;
1368 chip->owner = THIS_MODULE;
1369
Rabin Vincentebc61782011-09-28 15:49:11 +05301370 clk_enable(nmk_chip->clk);
1371 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1372 clk_disable(nmk_chip->clk);
1373
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001374#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001375 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001376#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001377
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001378 ret = gpiochip_add(&nmk_chip->chip);
1379 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001380 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001381
Rabin Vincent01727e62010-12-13 12:02:40 +05301382 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1383
1384 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001385
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001386 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001387
Linus Walleij51f58c62012-10-11 16:33:44 +02001388 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001389 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij51f58c62012-10-11 16:33:44 +02001390 nmk_chip->domain = irq_domain_add_simple(NULL,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001391 NMK_GPIO_PER_CHIP, irq_start,
1392 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001393 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001394 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001395 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001396 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001397 }
1398
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001399 nmk_gpio_init_irq(nmk_chip);
1400
Lee Jones513c27f2012-04-13 15:05:05 +01001401 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1402
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001403 return 0;
1404
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001405out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001406 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1407 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001408
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001409 return ret;
1410}
1411
Linus Walleije98ea772012-04-26 23:57:25 +02001412static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1413{
1414 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1415
1416 return npct->soc->ngroups;
1417}
1418
1419static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1420 unsigned selector)
1421{
1422 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1423
1424 return npct->soc->groups[selector].name;
1425}
1426
1427static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1428 const unsigned **pins,
1429 unsigned *num_pins)
1430{
1431 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1432
1433 *pins = npct->soc->groups[selector].pins;
1434 *num_pins = npct->soc->groups[selector].npins;
1435 return 0;
1436}
1437
Linus Walleij24cbdd72012-05-02 21:28:00 +02001438static struct pinctrl_gpio_range *
1439nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1440{
1441 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1442 int i;
1443
1444 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1445 struct pinctrl_gpio_range *range;
1446
1447 range = &npct->soc->gpio_ranges[i];
1448 if (offset >= range->pin_base &&
1449 offset <= (range->pin_base + range->npins - 1))
1450 return range;
1451 }
1452 return NULL;
1453}
1454
Linus Walleije98ea772012-04-26 23:57:25 +02001455static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1456 unsigned offset)
1457{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001458 struct pinctrl_gpio_range *range;
1459 struct gpio_chip *chip;
1460
1461 range = nmk_match_gpio_range(pctldev, offset);
1462 if (!range || !range->gc) {
1463 seq_printf(s, "invalid pin offset");
1464 return;
1465 }
1466 chip = range->gc;
1467 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001468}
1469
1470static struct pinctrl_ops nmk_pinctrl_ops = {
1471 .get_groups_count = nmk_get_groups_cnt,
1472 .get_group_name = nmk_get_group_name,
1473 .get_group_pins = nmk_get_group_pins,
1474 .pin_dbg_show = nmk_pin_dbg_show,
1475};
1476
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001477static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1478{
1479 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1480
1481 return npct->soc->nfunctions;
1482}
1483
1484static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1485 unsigned function)
1486{
1487 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1488
1489 return npct->soc->functions[function].name;
1490}
1491
1492static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1493 unsigned function,
1494 const char * const **groups,
1495 unsigned * const num_groups)
1496{
1497 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1498
1499 *groups = npct->soc->functions[function].groups;
1500 *num_groups = npct->soc->functions[function].ngroups;
1501
1502 return 0;
1503}
1504
1505static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1506 unsigned group)
1507{
1508 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1509 const struct nmk_pingroup *g;
1510 static unsigned int slpm[NUM_BANKS];
1511 unsigned long flags;
1512 bool glitch;
1513 int ret = -EINVAL;
1514 int i;
1515
1516 g = &npct->soc->groups[group];
1517
1518 if (g->altsetting < 0)
1519 return -EINVAL;
1520
1521 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1522
Linus Walleijdaf73172012-05-22 11:46:45 +02001523 /*
1524 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1525 * we may pass through an undesired state. In this case we take
1526 * some extra care.
1527 *
1528 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1529 * - Save SLPM registers (since we have a shadow register in the
1530 * nmk_chip we're using that as backup)
1531 * - Set SLPM=0 for the IOs you want to switch and others to 1
1532 * - Configure the GPIO registers for the IOs that are being switched
1533 * - Set IOFORCE=1
1534 * - Modify the AFLSA/B registers for the IOs that are being switched
1535 * - Set IOFORCE=0
1536 * - Restore SLPM registers
1537 * - Any spurious wake up event during switch sequence to be ignored
1538 * and cleared
1539 *
1540 * We REALLY need to save ALL slpm registers, because the external
1541 * IOFORCE will switch *all* ports to their sleepmode setting to as
1542 * to avoid glitches. (Not just one port!)
1543 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001544 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001545
1546 if (glitch) {
1547 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1548
1549 /* Initially don't put any pins to sleep when switching */
1550 memset(slpm, 0xff, sizeof(slpm));
1551
1552 /*
1553 * Then mask the pins that need to be sleeping now when we're
1554 * switching to the ALT C function.
1555 */
1556 for (i = 0; i < g->npins; i++)
1557 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1558 nmk_gpio_glitch_slpm_init(slpm);
1559 }
1560
1561 for (i = 0; i < g->npins; i++) {
1562 struct pinctrl_gpio_range *range;
1563 struct nmk_gpio_chip *nmk_chip;
1564 struct gpio_chip *chip;
1565 unsigned bit;
1566
1567 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1568 if (!range) {
1569 dev_err(npct->dev,
1570 "invalid pin offset %d in group %s at index %d\n",
1571 g->pins[i], g->name, i);
1572 goto out_glitch;
1573 }
1574 if (!range->gc) {
1575 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1576 g->pins[i], g->name, i);
1577 goto out_glitch;
1578 }
1579 chip = range->gc;
1580 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1581 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1582
1583 clk_enable(nmk_chip->clk);
1584 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1585 /*
1586 * If the pin is switching to altfunc, and there was an
1587 * interrupt installed on it which has been lazy disabled,
1588 * actually mask the interrupt to prevent spurious interrupts
1589 * that would occur while the pin is under control of the
1590 * peripheral. Only SKE does this.
1591 */
1592 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1593
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001594 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1595 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001596 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001597
1598 /*
1599 * Call PRCM GPIOCR config function in case ALTC
1600 * has been selected:
1601 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1602 * must be set.
1603 * - If selection is pure ALTC and previous selection was ALTCx,
1604 * then some bits in PRCM GPIOCR registers must be cleared.
1605 */
1606 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1607 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1608 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001609 }
1610
1611 /* When all pins are successfully reconfigured we get here */
1612 ret = 0;
1613
1614out_glitch:
1615 if (glitch) {
1616 nmk_gpio_glitch_slpm_restore(slpm);
1617 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1618 }
1619
1620 return ret;
1621}
1622
1623static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1624 unsigned function, unsigned group)
1625{
1626 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1627 const struct nmk_pingroup *g;
1628
1629 g = &npct->soc->groups[group];
1630
1631 if (g->altsetting < 0)
1632 return;
1633
1634 /* Poke out the mux, set the pin to some default state? */
1635 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1636}
1637
1638int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1639 struct pinctrl_gpio_range *range,
1640 unsigned offset)
1641{
1642 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1643 struct nmk_gpio_chip *nmk_chip;
1644 struct gpio_chip *chip;
1645 unsigned bit;
1646
1647 if (!range) {
1648 dev_err(npct->dev, "invalid range\n");
1649 return -EINVAL;
1650 }
1651 if (!range->gc) {
1652 dev_err(npct->dev, "missing GPIO chip in range\n");
1653 return -EINVAL;
1654 }
1655 chip = range->gc;
1656 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1657
1658 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1659
1660 clk_enable(nmk_chip->clk);
1661 bit = offset % NMK_GPIO_PER_CHIP;
1662 /* There is no glitch when converting any pin to GPIO */
1663 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1664 clk_disable(nmk_chip->clk);
1665
1666 return 0;
1667}
1668
1669void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1670 struct pinctrl_gpio_range *range,
1671 unsigned offset)
1672{
1673 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1674
1675 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1676 /* Set the pin to some default state, GPIO is usually default */
1677}
1678
1679static struct pinmux_ops nmk_pinmux_ops = {
1680 .get_functions_count = nmk_pmx_get_funcs_cnt,
1681 .get_function_name = nmk_pmx_get_func_name,
1682 .get_function_groups = nmk_pmx_get_func_groups,
1683 .enable = nmk_pmx_enable,
1684 .disable = nmk_pmx_disable,
1685 .gpio_request_enable = nmk_gpio_request_enable,
1686 .gpio_disable_free = nmk_gpio_disable_free,
1687};
1688
Linus Walleijd41af622012-05-03 15:58:12 +02001689int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1690 unsigned pin,
1691 unsigned long *config)
1692{
1693 /* Not implemented */
1694 return -EINVAL;
1695}
1696
1697int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1698 unsigned pin,
1699 unsigned long config)
1700{
1701 static const char *pullnames[] = {
1702 [NMK_GPIO_PULL_NONE] = "none",
1703 [NMK_GPIO_PULL_UP] = "up",
1704 [NMK_GPIO_PULL_DOWN] = "down",
1705 [3] /* illegal */ = "??"
1706 };
1707 static const char *slpmnames[] = {
1708 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1709 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1710 };
1711 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1712 struct nmk_gpio_chip *nmk_chip;
1713 struct pinctrl_gpio_range *range;
1714 struct gpio_chip *chip;
1715 unsigned bit;
1716
1717 /*
1718 * The pin config contains pin number and altfunction fields, here
1719 * we just ignore that part. It's being handled by the framework and
1720 * pinmux callback respectively.
1721 */
1722 pin_cfg_t cfg = (pin_cfg_t) config;
1723 int pull = PIN_PULL(cfg);
1724 int slpm = PIN_SLPM(cfg);
1725 int output = PIN_DIR(cfg);
1726 int val = PIN_VAL(cfg);
1727 bool lowemi = PIN_LOWEMI(cfg);
1728 bool gpiomode = PIN_GPIOMODE(cfg);
1729 bool sleep = PIN_SLEEPMODE(cfg);
1730
1731 range = nmk_match_gpio_range(pctldev, pin);
1732 if (!range) {
1733 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1734 return -EINVAL;
1735 }
1736 if (!range->gc) {
1737 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1738 pin);
1739 return -EINVAL;
1740 }
1741 chip = range->gc;
1742 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1743
1744 if (sleep) {
1745 int slpm_pull = PIN_SLPM_PULL(cfg);
1746 int slpm_output = PIN_SLPM_DIR(cfg);
1747 int slpm_val = PIN_SLPM_VAL(cfg);
1748
1749 /* All pins go into GPIO mode at sleep */
1750 gpiomode = true;
1751
1752 /*
1753 * The SLPM_* values are normal values + 1 to allow zero to
1754 * mean "same as normal".
1755 */
1756 if (slpm_pull)
1757 pull = slpm_pull - 1;
1758 if (slpm_output)
1759 output = slpm_output - 1;
1760 if (slpm_val)
1761 val = slpm_val - 1;
1762
1763 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1764 pin,
1765 slpm_pull ? pullnames[pull] : "same",
1766 slpm_output ? (output ? "output" : "input") : "same",
1767 slpm_val ? (val ? "high" : "low") : "same");
1768 }
1769
1770 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1771 pin, cfg, pullnames[pull], slpmnames[slpm],
1772 output ? "output " : "input",
1773 output ? (val ? "high" : "low") : "",
1774 lowemi ? "on" : "off" );
1775
1776 clk_enable(nmk_chip->clk);
1777 bit = pin % NMK_GPIO_PER_CHIP;
1778 if (gpiomode)
1779 /* No glitch when going to GPIO mode */
1780 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1781 if (output)
1782 __nmk_gpio_make_output(nmk_chip, bit, val);
1783 else {
1784 __nmk_gpio_make_input(nmk_chip, bit);
1785 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1786 }
1787 /* TODO: isn't this only applicable on output pins? */
1788 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1789
1790 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1791 clk_disable(nmk_chip->clk);
1792 return 0;
1793}
1794
1795static struct pinconf_ops nmk_pinconf_ops = {
1796 .pin_config_get = nmk_pin_config_get,
1797 .pin_config_set = nmk_pin_config_set,
1798};
1799
Linus Walleije98ea772012-04-26 23:57:25 +02001800static struct pinctrl_desc nmk_pinctrl_desc = {
1801 .name = "pinctrl-nomadik",
1802 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001803 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001804 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001805 .owner = THIS_MODULE,
1806};
1807
Lee Jones855f80c2012-05-26 06:09:29 +01001808static const struct of_device_id nmk_pinctrl_match[] = {
1809 {
1810 .compatible = "stericsson,nmk_pinctrl",
1811 .data = (void *)PINCTRL_NMK_DB8500,
1812 },
1813 {},
1814};
1815
Linus Walleije98ea772012-04-26 23:57:25 +02001816static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1817{
1818 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001819 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001820 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01001821 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001822 int i;
1823
1824 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1825 if (!npct)
1826 return -ENOMEM;
1827
Lee Jones855f80c2012-05-26 06:09:29 +01001828 if (platid)
1829 version = platid->driver_data;
1830 else if (np)
1831 version = (unsigned int)
1832 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1833
Linus Walleije98ea772012-04-26 23:57:25 +02001834 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001835 if (version == PINCTRL_NMK_STN8815)
1836 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001837 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001838 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001839 if (version == PINCTRL_NMK_DB8540)
1840 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001841
1842 /*
1843 * We need all the GPIO drivers to probe FIRST, or we will not be able
1844 * to obtain references to the struct gpio_chip * for them, and we
1845 * need this to proceed.
1846 */
1847 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1848 if (!nmk_gpio_chips[i]) {
1849 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001850 return -EPROBE_DEFER;
1851 }
1852 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1853 }
1854
1855 nmk_pinctrl_desc.pins = npct->soc->pins;
1856 nmk_pinctrl_desc.npins = npct->soc->npins;
1857 npct->dev = &pdev->dev;
1858 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1859 if (!npct->pctl) {
1860 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1861 return -EINVAL;
1862 }
1863
1864 /* We will handle a range of GPIO pins */
1865 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1866 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1867
1868 platform_set_drvdata(pdev, npct);
1869 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1870
1871 return 0;
1872}
1873
Lee Jones513c27f2012-04-13 15:05:05 +01001874static const struct of_device_id nmk_gpio_match[] = {
1875 { .compatible = "st,nomadik-gpio", },
1876 {}
1877};
1878
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001879static struct platform_driver nmk_gpio_driver = {
1880 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001881 .owner = THIS_MODULE,
1882 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001883 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301884 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001885 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001886};
1887
Linus Walleije98ea772012-04-26 23:57:25 +02001888static const struct platform_device_id nmk_pinctrl_id[] = {
1889 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1890 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001891 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Linus Walleije98ea772012-04-26 23:57:25 +02001892};
1893
1894static struct platform_driver nmk_pinctrl_driver = {
1895 .driver = {
1896 .owner = THIS_MODULE,
1897 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001898 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001899 },
1900 .probe = nmk_pinctrl_probe,
1901 .id_table = nmk_pinctrl_id,
1902};
1903
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001904static int __init nmk_gpio_init(void)
1905{
Linus Walleije98ea772012-04-26 23:57:25 +02001906 int ret;
1907
1908 ret = platform_driver_register(&nmk_gpio_driver);
1909 if (ret)
1910 return ret;
1911 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001912}
1913
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001914core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001915
1916MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1917MODULE_DESCRIPTION("Nomadik GPIO Driver");
1918MODULE_LICENSE("GPL");