blob: 1e04c1f27dcb337b2948cfdd70441fb67e70332a [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
Rabin Vincent378be062010-06-02 06:06:29 +010051#include <plat/pincfg.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
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010063struct nmk_gpio_chip {
64 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010065 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010066 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010067 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053068 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010069 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053070 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053071 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053072 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010073 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020074 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010075 /* Keep track of configured edges */
76 u32 edge_rising;
77 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053078 u32 real_wake;
79 u32 rwimsc;
80 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053081 u32 rimsc;
82 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020083 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053084 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010085};
86
Linus Walleije98ea772012-04-26 23:57:25 +020087struct nmk_pinctrl {
88 struct device *dev;
89 struct pinctrl_dev *pctl;
90 const struct nmk_pinctrl_soc_data *soc;
91};
92
Rabin Vincent01727e62010-12-13 12:02:40 +053093static struct nmk_gpio_chip *
94nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
95
96static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
97
98#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
99
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100100static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
101 unsigned offset, int gpio_mode)
102{
103 u32 bit = 1 << offset;
104 u32 afunc, bfunc;
105
106 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
107 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
108 if (gpio_mode & NMK_GPIO_ALT_A)
109 afunc |= bit;
110 if (gpio_mode & NMK_GPIO_ALT_B)
111 bfunc |= bit;
112 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
113 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
114}
115
Rabin Vincent81a3c292010-05-27 12:39:23 +0100116static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
117 unsigned offset, enum nmk_gpio_slpm mode)
118{
119 u32 bit = 1 << offset;
120 u32 slpm;
121
122 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
123 if (mode == NMK_GPIO_SLPM_NOCHANGE)
124 slpm |= bit;
125 else
126 slpm &= ~bit;
127 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
128}
129
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100130static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
131 unsigned offset, enum nmk_gpio_pull pull)
132{
133 u32 bit = 1 << offset;
134 u32 pdis;
135
136 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200137 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100138 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200139 nmk_chip->pull_up &= ~bit;
140 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100141 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200142 }
143
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100144 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
145
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200146 if (pull == NMK_GPIO_PULL_UP) {
147 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100148 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200149 } else if (pull == NMK_GPIO_PULL_DOWN) {
150 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100151 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200152 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100153}
154
Rabin Vincentebc61782011-09-28 15:49:11 +0530155static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
156 unsigned offset, bool lowemi)
157{
158 u32 bit = BIT(offset);
159 bool enabled = nmk_chip->lowemi & bit;
160
161 if (lowemi == enabled)
162 return;
163
164 if (lowemi)
165 nmk_chip->lowemi |= bit;
166 else
167 nmk_chip->lowemi &= ~bit;
168
169 writel_relaxed(nmk_chip->lowemi,
170 nmk_chip->addr + NMK_GPIO_LOWEMI);
171}
172
Rabin Vincent378be062010-06-02 06:06:29 +0100173static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
174 unsigned offset)
175{
176 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
177}
178
Rabin Vincent6720db72010-09-02 11:28:48 +0100179static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
180 unsigned offset, int val)
181{
182 if (val)
183 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
184 else
185 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
186}
187
188static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
189 unsigned offset, int val)
190{
191 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
192 __nmk_gpio_set_output(nmk_chip, offset, val);
193}
194
Rabin Vincent01727e62010-12-13 12:02:40 +0530195static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
196 unsigned offset, int gpio_mode,
197 bool glitch)
198{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530199 u32 rwimsc = nmk_chip->rwimsc;
200 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530201
202 if (glitch && nmk_chip->set_ioforce) {
203 u32 bit = BIT(offset);
204
Rabin Vincent01727e62010-12-13 12:02:40 +0530205 /* Prevent spurious wakeups */
206 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
207 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
208
209 nmk_chip->set_ioforce(true);
210 }
211
212 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
213
214 if (glitch && nmk_chip->set_ioforce) {
215 nmk_chip->set_ioforce(false);
216
217 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
218 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
219 }
220}
221
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530222static void
223nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
224{
225 u32 falling = nmk_chip->fimsc & BIT(offset);
226 u32 rising = nmk_chip->rimsc & BIT(offset);
227 int gpio = nmk_chip->chip.base + offset;
228 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
229 struct irq_data *d = irq_get_irq_data(irq);
230
231 if (!rising && !falling)
232 return;
233
234 if (!d || !irqd_irq_disabled(d))
235 return;
236
237 if (rising) {
238 nmk_chip->rimsc &= ~BIT(offset);
239 writel_relaxed(nmk_chip->rimsc,
240 nmk_chip->addr + NMK_GPIO_RIMSC);
241 }
242
243 if (falling) {
244 nmk_chip->fimsc &= ~BIT(offset);
245 writel_relaxed(nmk_chip->fimsc,
246 nmk_chip->addr + NMK_GPIO_FIMSC);
247 }
248
249 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
250}
251
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200252static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
253 unsigned offset, unsigned alt_num)
254{
255 int i;
256 u16 reg;
257 u8 bit;
258 u8 alt_index;
259 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
260 const u16 *gpiocr_regs;
261
262 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
263 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
264 alt_num);
265 return;
266 }
267
268 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
269 if (npct->soc->altcx_pins[i].pin == offset)
270 break;
271 }
272 if (i == npct->soc->npins_altcx) {
273 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
274 offset);
275 return;
276 }
277
278 pin_desc = npct->soc->altcx_pins + i;
279 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
280
281 /*
282 * If alt_num is NULL, just clear current ALTCx selection
283 * to make sure we come back to a pure ALTC selection
284 */
285 if (!alt_num) {
286 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
287 if (pin_desc->altcx[i].used == true) {
288 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
289 bit = pin_desc->altcx[i].control_bit;
290 if (prcmu_read(reg) & BIT(bit)) {
291 prcmu_write_masked(reg, BIT(bit), 0);
292 dev_dbg(npct->dev,
293 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
294 offset, i+1);
295 }
296 }
297 }
298 return;
299 }
300
301 alt_index = alt_num - 1;
302 if (pin_desc->altcx[alt_index].used == false) {
303 dev_warn(npct->dev,
304 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
305 offset, alt_num);
306 return;
307 }
308
309 /*
310 * Check if any other ALTCx functions are activated on this pin
311 * and disable it first.
312 */
313 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
314 if (i == alt_index)
315 continue;
316 if (pin_desc->altcx[i].used == true) {
317 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
318 bit = pin_desc->altcx[i].control_bit;
319 if (prcmu_read(reg) & BIT(bit)) {
320 prcmu_write_masked(reg, BIT(bit), 0);
321 dev_dbg(npct->dev,
322 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
323 offset, i+1);
324 }
325 }
326 }
327
328 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
329 bit = pin_desc->altcx[alt_index].control_bit;
330 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
331 offset, alt_index+1);
332 prcmu_write_masked(reg, BIT(bit), BIT(bit));
333}
334
Rabin Vincent378be062010-06-02 06:06:29 +0100335static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530336 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100337{
338 static const char *afnames[] = {
339 [NMK_GPIO_ALT_GPIO] = "GPIO",
340 [NMK_GPIO_ALT_A] = "A",
341 [NMK_GPIO_ALT_B] = "B",
342 [NMK_GPIO_ALT_C] = "C"
343 };
344 static const char *pullnames[] = {
345 [NMK_GPIO_PULL_NONE] = "none",
346 [NMK_GPIO_PULL_UP] = "up",
347 [NMK_GPIO_PULL_DOWN] = "down",
348 [3] /* illegal */ = "??"
349 };
350 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100351 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
352 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100353 };
354
355 int pin = PIN_NUM(cfg);
356 int pull = PIN_PULL(cfg);
357 int af = PIN_ALT(cfg);
358 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100359 int output = PIN_DIR(cfg);
360 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530361 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100362
Rabin Vincentdacdc962010-12-03 20:35:37 +0530363 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
364 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100365 output ? "output " : "input",
366 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100367
Rabin Vincentdacdc962010-12-03 20:35:37 +0530368 if (sleep) {
369 int slpm_pull = PIN_SLPM_PULL(cfg);
370 int slpm_output = PIN_SLPM_DIR(cfg);
371 int slpm_val = PIN_SLPM_VAL(cfg);
372
Rabin Vincent3546d152010-11-25 11:38:27 +0530373 af = NMK_GPIO_ALT_GPIO;
374
Rabin Vincentdacdc962010-12-03 20:35:37 +0530375 /*
376 * The SLPM_* values are normal values + 1 to allow zero to
377 * mean "same as normal".
378 */
379 if (slpm_pull)
380 pull = slpm_pull - 1;
381 if (slpm_output)
382 output = slpm_output - 1;
383 if (slpm_val)
384 val = slpm_val - 1;
385
386 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
387 pin,
388 slpm_pull ? pullnames[pull] : "same",
389 slpm_output ? (output ? "output" : "input") : "same",
390 slpm_val ? (val ? "high" : "low") : "same");
391 }
392
Rabin Vincent6720db72010-09-02 11:28:48 +0100393 if (output)
394 __nmk_gpio_make_output(nmk_chip, offset, val);
395 else {
396 __nmk_gpio_make_input(nmk_chip, offset);
397 __nmk_gpio_set_pull(nmk_chip, offset, pull);
398 }
399
Rabin Vincentebc61782011-09-28 15:49:11 +0530400 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
401
Rabin Vincent01727e62010-12-13 12:02:40 +0530402 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530403 * If the pin is switching to altfunc, and there was an interrupt
404 * installed on it which has been lazy disabled, actually mask the
405 * interrupt to prevent spurious interrupts that would occur while the
406 * pin is under control of the peripheral. Only SKE does this.
407 */
408 if (af != NMK_GPIO_ALT_GPIO)
409 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
410
411 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530412 * If we've backed up the SLPM registers (glitch workaround), modify
413 * the backups since they will be restored.
414 */
415 if (slpmregs) {
416 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
417 slpmregs[nmk_chip->bank] |= BIT(offset);
418 else
419 slpmregs[nmk_chip->bank] &= ~BIT(offset);
420 } else
421 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
422
423 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
424}
425
426/*
427 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
428 * - Save SLPM registers
429 * - Set SLPM=0 for the IOs you want to switch and others to 1
430 * - Configure the GPIO registers for the IOs that are being switched
431 * - Set IOFORCE=1
432 * - Modify the AFLSA/B registers for the IOs that are being switched
433 * - Set IOFORCE=0
434 * - Restore SLPM registers
435 * - Any spurious wake up event during switch sequence to be ignored and
436 * cleared
437 */
438static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
439{
440 int i;
441
442 for (i = 0; i < NUM_BANKS; i++) {
443 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
444 unsigned int temp = slpm[i];
445
446 if (!chip)
447 break;
448
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200449 clk_enable(chip->clk);
450
Rabin Vincent01727e62010-12-13 12:02:40 +0530451 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
452 writel(temp, chip->addr + NMK_GPIO_SLPC);
453 }
454}
455
456static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
457{
458 int i;
459
460 for (i = 0; i < NUM_BANKS; i++) {
461 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
462
463 if (!chip)
464 break;
465
466 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200467
468 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530469 }
470}
471
472static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
473{
474 static unsigned int slpm[NUM_BANKS];
475 unsigned long flags;
476 bool glitch = false;
477 int ret = 0;
478 int i;
479
480 for (i = 0; i < num; i++) {
481 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
482 glitch = true;
483 break;
484 }
485 }
486
487 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
488
489 if (glitch) {
490 memset(slpm, 0xff, sizeof(slpm));
491
492 for (i = 0; i < num; i++) {
493 int pin = PIN_NUM(cfgs[i]);
494 int offset = pin % NMK_GPIO_PER_CHIP;
495
496 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
497 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
498 }
499
500 nmk_gpio_glitch_slpm_init(slpm);
501 }
502
503 for (i = 0; i < num; i++) {
504 struct nmk_gpio_chip *nmk_chip;
505 int pin = PIN_NUM(cfgs[i]);
506
Lee Jonesa60b57e2012-04-19 21:36:31 +0100507 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530508 if (!nmk_chip) {
509 ret = -EINVAL;
510 break;
511 }
512
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200513 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530514 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100515 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530516 cfgs[i], sleep, glitch ? slpm : NULL);
517 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200518 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530519 }
520
521 if (glitch)
522 nmk_gpio_glitch_slpm_restore(slpm);
523
524 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
525
526 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100527}
528
529/**
530 * nmk_config_pin - configure a pin's mux attributes
531 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200532 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100533 * Configures a pin's mode (alternate function or GPIO), its pull up status,
534 * and its sleep mode based on the specified configuration. The @cfg is
535 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
536 * are constructed using, and can be further enhanced with, the macros in
537 * plat/pincfg.h.
538 *
539 * If a pin's mode is set to GPIO, it is configured as an input to avoid
540 * side-effects. The gpio can be manipulated later using standard GPIO API
541 * calls.
542 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530543int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100544{
Rabin Vincent01727e62010-12-13 12:02:40 +0530545 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100546}
547EXPORT_SYMBOL(nmk_config_pin);
548
549/**
550 * nmk_config_pins - configure several pins at once
551 * @cfgs: array of pin configurations
552 * @num: number of elments in the array
553 *
554 * Configures several pins using nmk_config_pin(). Refer to that function for
555 * further information.
556 */
557int nmk_config_pins(pin_cfg_t *cfgs, int num)
558{
Rabin Vincent01727e62010-12-13 12:02:40 +0530559 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100560}
561EXPORT_SYMBOL(nmk_config_pins);
562
Rabin Vincentdacdc962010-12-03 20:35:37 +0530563int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
564{
Rabin Vincent01727e62010-12-13 12:02:40 +0530565 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530566}
567EXPORT_SYMBOL(nmk_config_pins_sleep);
568
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100569/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100570 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
571 * @gpio: pin number
572 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
573 *
Linus Walleij33d78642011-06-09 11:08:47 +0200574 * This register is actually in the pinmux layer, not the GPIO block itself.
575 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
576 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
577 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
578 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
579 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
580 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100581 *
Linus Walleij33d78642011-06-09 11:08:47 +0200582 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
583 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
584 * entered) regardless of the altfunction selected. Also wake-up detection is
585 * ENABLED.
586 *
587 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
588 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
589 * (for altfunction GPIO) or respective on-chip peripherals (for other
590 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
591 *
592 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100593 */
594int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
595{
596 struct nmk_gpio_chip *nmk_chip;
597 unsigned long flags;
598
Lee Jonesa60b57e2012-04-19 21:36:31 +0100599 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100600 if (!nmk_chip)
601 return -EINVAL;
602
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200603 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530604 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
605 spin_lock(&nmk_chip->lock);
606
Lee Jonesa60b57e2012-04-19 21:36:31 +0100607 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530608
609 spin_unlock(&nmk_chip->lock);
610 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200611 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100612
613 return 0;
614}
615
616/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100617 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
618 * @gpio: pin number
619 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
620 *
621 * Enables/disables pull up/down on a specified pin. This only takes effect if
622 * the pin is configured as an input (either explicitly or by the alternate
623 * function).
624 *
625 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
626 * configured as an input. Otherwise, due to the way the controller registers
627 * work, this function will change the value output on the pin.
628 */
629int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
630{
631 struct nmk_gpio_chip *nmk_chip;
632 unsigned long flags;
633
Lee Jonesa60b57e2012-04-19 21:36:31 +0100634 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100635 if (!nmk_chip)
636 return -EINVAL;
637
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200638 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100639 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100640 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100641 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200642 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100643
644 return 0;
645}
646
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100647/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200648/**
649 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
650 * @gpio: pin number
651 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
652 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
653 *
654 * Sets the mode of the specified pin to one of the alternate functions or
655 * plain GPIO.
656 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100657int nmk_gpio_set_mode(int gpio, int gpio_mode)
658{
659 struct nmk_gpio_chip *nmk_chip;
660 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100661
Lee Jonesa60b57e2012-04-19 21:36:31 +0100662 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100663 if (!nmk_chip)
664 return -EINVAL;
665
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200666 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100668 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200670 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100671
672 return 0;
673}
674EXPORT_SYMBOL(nmk_gpio_set_mode);
675
676int nmk_gpio_get_mode(int gpio)
677{
678 struct nmk_gpio_chip *nmk_chip;
679 u32 afunc, bfunc, bit;
680
Lee Jonesa60b57e2012-04-19 21:36:31 +0100681 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100682 if (!nmk_chip)
683 return -EINVAL;
684
Lee Jonesa60b57e2012-04-19 21:36:31 +0100685 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100686
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200687 clk_enable(nmk_chip->clk);
688
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100689 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
690 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
691
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200692 clk_disable(nmk_chip->clk);
693
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100694 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
695}
696EXPORT_SYMBOL(nmk_gpio_get_mode);
697
698
699/* IRQ functions */
700static inline int nmk_gpio_get_bitmask(int gpio)
701{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100702 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100703}
704
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100705static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100706{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100707 struct nmk_gpio_chip *nmk_chip;
708
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100709 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100710 if (!nmk_chip)
711 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200712
713 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100714 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200715 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100716}
717
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100718enum nmk_gpio_irq_type {
719 NORMAL,
720 WAKE,
721};
722
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100723static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100724 int gpio, enum nmk_gpio_irq_type which,
725 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100726{
727 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530728 u32 *rimscval;
729 u32 *fimscval;
730 u32 rimscreg;
731 u32 fimscreg;
732
733 if (which == NORMAL) {
734 rimscreg = NMK_GPIO_RIMSC;
735 fimscreg = NMK_GPIO_FIMSC;
736 rimscval = &nmk_chip->rimsc;
737 fimscval = &nmk_chip->fimsc;
738 } else {
739 rimscreg = NMK_GPIO_RWIMSC;
740 fimscreg = NMK_GPIO_FWIMSC;
741 rimscval = &nmk_chip->rwimsc;
742 fimscval = &nmk_chip->fwimsc;
743 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100744
745 /* we must individually set/clear the two edges */
746 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100747 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530748 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100749 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530750 *rimscval &= ~bitmask;
751 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100752 }
753 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100754 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530755 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100756 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530757 *fimscval &= ~bitmask;
758 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100759 }
760}
761
Rabin Vincentb9df4682011-02-10 11:45:58 +0530762static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
763 int gpio, bool on)
764{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530765 /*
766 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
767 * disabled, since setting SLPM to 1 increases power consumption, and
768 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
769 */
770 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200771 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530772 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200773 }
774
Rabin Vincentb9df4682011-02-10 11:45:58 +0530775 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
776}
777
778static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100779{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100780 struct nmk_gpio_chip *nmk_chip;
781 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100782 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100783
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100784 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100785 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100786 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100787 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200789 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530790 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
791 spin_lock(&nmk_chip->lock);
792
Lee Jonesa60b57e2012-04-19 21:36:31 +0100793 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530794
795 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100796 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530797
798 spin_unlock(&nmk_chip->lock);
799 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200800 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100801
802 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100803}
804
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100805static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100806{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530807 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100808}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100809
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100810static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100811{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530812 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100813}
814
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100815static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100816{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100817 struct nmk_gpio_chip *nmk_chip;
818 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530819 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100820
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100821 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100822 if (!nmk_chip)
823 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100824 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100825
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200826 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530827 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
828 spin_lock(&nmk_chip->lock);
829
Linus Walleij479a0c72011-09-20 10:50:15 +0200830 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100831 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530832
833 if (on)
834 nmk_chip->real_wake |= bitmask;
835 else
836 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530837
838 spin_unlock(&nmk_chip->lock);
839 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200840 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100841
842 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100843}
844
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100845static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100846{
Linus Walleij479a0c72011-09-20 10:50:15 +0200847 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200848 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100849 struct nmk_gpio_chip *nmk_chip;
850 unsigned long flags;
851 u32 bitmask;
852
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100853 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100854 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100855 if (!nmk_chip)
856 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100857 if (type & IRQ_TYPE_LEVEL_HIGH)
858 return -EINVAL;
859 if (type & IRQ_TYPE_LEVEL_LOW)
860 return -EINVAL;
861
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200862 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100863 spin_lock_irqsave(&nmk_chip->lock, flags);
864
Rabin Vincent7a852d82010-05-06 10:43:55 +0100865 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100866 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100867
Rabin Vincentb9df4682011-02-10 11:45:58 +0530868 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100869 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100870
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100871 nmk_chip->edge_rising &= ~bitmask;
872 if (type & IRQ_TYPE_EDGE_RISING)
873 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100874
875 nmk_chip->edge_falling &= ~bitmask;
876 if (type & IRQ_TYPE_EDGE_FALLING)
877 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100878
879 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100880 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100881
Rabin Vincentb9df4682011-02-10 11:45:58 +0530882 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100883 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100884
885 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200886 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100887
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100888 return 0;
889}
890
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200891static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
892{
893 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
894
895 clk_enable(nmk_chip->clk);
896 nmk_gpio_irq_unmask(d);
897 return 0;
898}
899
900static void nmk_gpio_irq_shutdown(struct irq_data *d)
901{
902 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
903
904 nmk_gpio_irq_mask(d);
905 clk_disable(nmk_chip->clk);
906}
907
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100908static struct irq_chip nmk_gpio_irq_chip = {
909 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100910 .irq_ack = nmk_gpio_irq_ack,
911 .irq_mask = nmk_gpio_irq_mask,
912 .irq_unmask = nmk_gpio_irq_unmask,
913 .irq_set_type = nmk_gpio_irq_set_type,
914 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200915 .irq_startup = nmk_gpio_irq_startup,
916 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200917 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100918};
919
Rabin Vincent33b744b2010-10-14 10:38:03 +0530920static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
921 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100922{
923 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100924 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100925
Will Deaconadfed152011-02-28 10:12:29 +0000926 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100927
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100928 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530929 while (status) {
930 int bit = __ffs(status);
931
Linus Walleij95f0bc92012-09-27 14:14:09 +0200932 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530933 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100934 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100935
Will Deaconadfed152011-02-28 10:12:29 +0000936 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100937}
938
Rabin Vincent33b744b2010-10-14 10:38:03 +0530939static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
940{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100941 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200942 u32 status;
943
944 clk_enable(nmk_chip->clk);
945 status = readl(nmk_chip->addr + NMK_GPIO_IS);
946 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530947
948 __nmk_gpio_irq_handler(irq, desc, status);
949}
950
951static void nmk_gpio_secondary_irq_handler(unsigned int irq,
952 struct irq_desc *desc)
953{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100954 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530955 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
956
957 __nmk_gpio_irq_handler(irq, desc, status);
958}
959
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100960static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
961{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100962 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
963 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530964
965 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100966 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530967 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100968 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530969 }
970
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100971 return 0;
972}
973
974/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200975
976static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
977{
978 /*
979 * Map back to global GPIO space and request muxing, the direction
980 * parameter does not matter for this controller.
981 */
982 int gpio = chip->base + offset;
983
984 return pinctrl_request_gpio(gpio);
985}
986
987static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
988{
989 int gpio = chip->base + offset;
990
991 pinctrl_free_gpio(gpio);
992}
993
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100994static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
995{
996 struct nmk_gpio_chip *nmk_chip =
997 container_of(chip, struct nmk_gpio_chip, chip);
998
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200999 clk_enable(nmk_chip->clk);
1000
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001001 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001002
1003 clk_disable(nmk_chip->clk);
1004
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001005 return 0;
1006}
1007
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001008static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1009{
1010 struct nmk_gpio_chip *nmk_chip =
1011 container_of(chip, struct nmk_gpio_chip, chip);
1012 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001013 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001014
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001015 clk_enable(nmk_chip->clk);
1016
1017 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1018
1019 clk_disable(nmk_chip->clk);
1020
1021 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001022}
1023
1024static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1025 int val)
1026{
1027 struct nmk_gpio_chip *nmk_chip =
1028 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001029
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001030 clk_enable(nmk_chip->clk);
1031
Rabin Vincent6720db72010-09-02 11:28:48 +01001032 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001033
1034 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001035}
1036
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001037static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1038 int val)
1039{
1040 struct nmk_gpio_chip *nmk_chip =
1041 container_of(chip, struct nmk_gpio_chip, chip);
1042
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001043 clk_enable(nmk_chip->clk);
1044
Rabin Vincent6720db72010-09-02 11:28:48 +01001045 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001046
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001047 clk_disable(nmk_chip->clk);
1048
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001049 return 0;
1050}
1051
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001052static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1053{
1054 struct nmk_gpio_chip *nmk_chip =
1055 container_of(chip, struct nmk_gpio_chip, chip);
1056
Linus Walleij268300b2012-10-19 17:06:54 +02001057 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001058}
1059
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301060#ifdef CONFIG_DEBUG_FS
1061
1062#include <linux/seq_file.h>
1063
Linus Walleij6f4350a2012-05-02 21:06:13 +02001064static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
1065 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301066{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001067 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301068 struct nmk_gpio_chip *nmk_chip =
1069 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001070 int mode;
1071 bool is_out;
1072 bool pull;
1073 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301074 const char *modes[] = {
1075 [NMK_GPIO_ALT_GPIO] = "gpio",
1076 [NMK_GPIO_ALT_A] = "altA",
1077 [NMK_GPIO_ALT_B] = "altB",
1078 [NMK_GPIO_ALT_C] = "altC",
1079 };
1080
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001081 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001082 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1083 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1084 mode = nmk_gpio_get_mode(gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001085
Linus Walleij6f4350a2012-05-02 21:06:13 +02001086 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1087 gpio, label ?: "(none)",
1088 is_out ? "out" : "in ",
1089 chip->get
1090 ? (chip->get(chip, offset) ? "hi" : "lo")
1091 : "? ",
1092 (mode < 0) ? "unknown" : modes[mode],
1093 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301094
Linus Walleij6f4350a2012-05-02 21:06:13 +02001095 if (label && !is_out) {
1096 int irq = gpio_to_irq(gpio);
1097 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001098
Linus Walleij6f4350a2012-05-02 21:06:13 +02001099 /* This races with request_irq(), set_irq_type(),
1100 * and set_irq_wake() ... but those are "rare".
1101 */
1102 if (irq >= 0 && desc->action) {
1103 char *trigger;
1104 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001105
Linus Walleij6f4350a2012-05-02 21:06:13 +02001106 if (nmk_chip->edge_rising & bitmask)
1107 trigger = "edge-rising";
1108 else if (nmk_chip->edge_falling & bitmask)
1109 trigger = "edge-falling";
1110 else
1111 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001112
Linus Walleij6f4350a2012-05-02 21:06:13 +02001113 seq_printf(s, " irq-%d %s%s",
1114 irq, trigger,
1115 irqd_is_wakeup_set(&desc->irq_data)
1116 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001117 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301118 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001119 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301120}
1121
Linus Walleij6f4350a2012-05-02 21:06:13 +02001122static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1123{
1124 unsigned i;
1125 unsigned gpio = chip->base;
1126
1127 for (i = 0; i < chip->ngpio; i++, gpio++) {
1128 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1129 seq_printf(s, "\n");
1130 }
1131}
1132
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301133#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001134static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1135 struct gpio_chip *chip,
1136 unsigned offset, unsigned gpio)
1137{
1138}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301139#define nmk_gpio_dbg_show NULL
1140#endif
1141
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001142/* This structure is replicated for each GPIO block allocated at probe time */
1143static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001144 .request = nmk_gpio_request,
1145 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001146 .direction_input = nmk_gpio_make_input,
1147 .get = nmk_gpio_get_input,
1148 .direction_output = nmk_gpio_make_output,
1149 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001150 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301151 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001152 .can_sleep = 0,
1153};
1154
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001155void nmk_gpio_clocks_enable(void)
1156{
1157 int i;
1158
1159 for (i = 0; i < NUM_BANKS; i++) {
1160 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1161
1162 if (!chip)
1163 continue;
1164
1165 clk_enable(chip->clk);
1166 }
1167}
1168
1169void nmk_gpio_clocks_disable(void)
1170{
1171 int i;
1172
1173 for (i = 0; i < NUM_BANKS; i++) {
1174 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1175
1176 if (!chip)
1177 continue;
1178
1179 clk_disable(chip->clk);
1180 }
1181}
1182
Rabin Vincentb9df4682011-02-10 11:45:58 +05301183/*
1184 * Called from the suspend/resume path to only keep the real wakeup interrupts
1185 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1186 * and not the rest of the interrupts which we needed to have as wakeups for
1187 * cpuidle.
1188 *
1189 * PM ops are not used since this needs to be done at the end, after all the
1190 * other drivers are done with their suspend callbacks.
1191 */
1192void nmk_gpio_wakeups_suspend(void)
1193{
1194 int i;
1195
1196 for (i = 0; i < NUM_BANKS; i++) {
1197 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1198
1199 if (!chip)
1200 break;
1201
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001202 clk_enable(chip->clk);
1203
Rabin Vincentb9df4682011-02-10 11:45:58 +05301204 writel(chip->rwimsc & chip->real_wake,
1205 chip->addr + NMK_GPIO_RWIMSC);
1206 writel(chip->fwimsc & chip->real_wake,
1207 chip->addr + NMK_GPIO_FWIMSC);
1208
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001209 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301210 }
1211}
1212
1213void nmk_gpio_wakeups_resume(void)
1214{
1215 int i;
1216
1217 for (i = 0; i < NUM_BANKS; i++) {
1218 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1219
1220 if (!chip)
1221 break;
1222
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001223 clk_enable(chip->clk);
1224
Rabin Vincentb9df4682011-02-10 11:45:58 +05301225 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1226 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1227
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001228 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301229 }
1230}
1231
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001232/*
1233 * Read the pull up/pull down status.
1234 * A bit set in 'pull_up' means that pull up
1235 * is selected if pull is enabled in PDIS register.
1236 * Note: only pull up/down set via this driver can
1237 * be detected due to HW limitations.
1238 */
1239void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1240{
1241 if (gpio_bank < NUM_BANKS) {
1242 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1243
1244 if (!chip)
1245 return;
1246
1247 *pull_up = chip->pull_up;
1248 }
1249}
1250
Lee Jonesa60b57e2012-04-19 21:36:31 +01001251int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1252 irq_hw_number_t hwirq)
1253{
1254 struct nmk_gpio_chip *nmk_chip = d->host_data;
1255
1256 if (!nmk_chip)
1257 return -EINVAL;
1258
1259 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1260 set_irq_flags(irq, IRQF_VALID);
1261 irq_set_chip_data(irq, nmk_chip);
1262 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1263
1264 return 0;
1265}
1266
1267const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1268 .map = nmk_gpio_irq_map,
1269 .xlate = irq_domain_xlate_twocell,
1270};
1271
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001272static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001273{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001274 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001275 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001276 struct nmk_gpio_chip *nmk_chip;
1277 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001278 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001279 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301280 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001281 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001282 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001283 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001284 int ret;
1285
Lee Jones513c27f2012-04-13 15:05:05 +01001286 if (!pdata && !np) {
1287 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001288 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001289 }
1290
1291 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001292 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001293 if (!pdata)
1294 return -ENOMEM;
1295
Lee Jones612e1d52012-06-14 11:27:56 +01001296 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001297 pdata->supports_sleepmode = true;
1298
1299 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1300 dev_err(&dev->dev, "gpio-bank property not found\n");
1301 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001302 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001303 }
1304
1305 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1306 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1307 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001308
1309 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1310 if (!res) {
1311 ret = -ENOENT;
1312 goto out;
1313 }
1314
1315 irq = platform_get_irq(dev, 0);
1316 if (irq < 0) {
1317 ret = irq;
1318 goto out;
1319 }
1320
Rabin Vincent33b744b2010-10-14 10:38:03 +05301321 secondary_irq = platform_get_irq(dev, 1);
1322 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1323 ret = -EINVAL;
1324 goto out;
1325 }
1326
Linus Walleij5e754f32012-07-03 23:05:14 +02001327 base = devm_request_and_ioremap(&dev->dev, res);
1328 if (!base) {
1329 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001330 goto out;
1331 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001332
Linus Walleij5e754f32012-07-03 23:05:14 +02001333 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001334 if (IS_ERR(clk)) {
1335 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001336 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001337 }
Linus Walleijefec3812012-06-06 22:50:41 +02001338 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001339
Linus Walleij5e754f32012-07-03 23:05:14 +02001340 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001341 if (!nmk_chip) {
1342 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001343 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001344 }
Lee Jones513c27f2012-04-13 15:05:05 +01001345
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001346 /*
1347 * The virt address in nmk_chip->addr is in the nomadik register space,
1348 * so we can simply convert the resource address, without remapping
1349 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301350 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001351 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001352 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001353 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001354 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301355 nmk_chip->secondary_parent_irq = secondary_irq;
1356 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301357 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001358 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001359 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001360
1361 chip = &nmk_chip->chip;
1362 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301363 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301364 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001365 chip->dev = &dev->dev;
1366 chip->owner = THIS_MODULE;
1367
Rabin Vincentebc61782011-09-28 15:49:11 +05301368 clk_enable(nmk_chip->clk);
1369 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1370 clk_disable(nmk_chip->clk);
1371
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001372#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001373 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001374#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001375
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001376 ret = gpiochip_add(&nmk_chip->chip);
1377 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001378 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001379
Rabin Vincent01727e62010-12-13 12:02:40 +05301380 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1381
1382 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001383
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001384 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001385
Linus Walleij51f58c62012-10-11 16:33:44 +02001386 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001387 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001388 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001389 NMK_GPIO_PER_CHIP, irq_start,
1390 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001391 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001392 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001393 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001394 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001395 }
1396
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001397 nmk_gpio_init_irq(nmk_chip);
1398
Lee Jones513c27f2012-04-13 15:05:05 +01001399 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1400
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001401 return 0;
1402
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001403out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001404 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1405 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001406
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001407 return ret;
1408}
1409
Linus Walleije98ea772012-04-26 23:57:25 +02001410static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1411{
1412 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1413
1414 return npct->soc->ngroups;
1415}
1416
1417static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1418 unsigned selector)
1419{
1420 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1421
1422 return npct->soc->groups[selector].name;
1423}
1424
1425static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1426 const unsigned **pins,
1427 unsigned *num_pins)
1428{
1429 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1430
1431 *pins = npct->soc->groups[selector].pins;
1432 *num_pins = npct->soc->groups[selector].npins;
1433 return 0;
1434}
1435
Linus Walleij24cbdd72012-05-02 21:28:00 +02001436static struct pinctrl_gpio_range *
1437nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1438{
1439 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1440 int i;
1441
1442 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1443 struct pinctrl_gpio_range *range;
1444
1445 range = &npct->soc->gpio_ranges[i];
1446 if (offset >= range->pin_base &&
1447 offset <= (range->pin_base + range->npins - 1))
1448 return range;
1449 }
1450 return NULL;
1451}
1452
Linus Walleije98ea772012-04-26 23:57:25 +02001453static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1454 unsigned offset)
1455{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001456 struct pinctrl_gpio_range *range;
1457 struct gpio_chip *chip;
1458
1459 range = nmk_match_gpio_range(pctldev, offset);
1460 if (!range || !range->gc) {
1461 seq_printf(s, "invalid pin offset");
1462 return;
1463 }
1464 chip = range->gc;
1465 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001466}
1467
1468static struct pinctrl_ops nmk_pinctrl_ops = {
1469 .get_groups_count = nmk_get_groups_cnt,
1470 .get_group_name = nmk_get_group_name,
1471 .get_group_pins = nmk_get_group_pins,
1472 .pin_dbg_show = nmk_pin_dbg_show,
1473};
1474
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001475static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1476{
1477 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1478
1479 return npct->soc->nfunctions;
1480}
1481
1482static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1483 unsigned function)
1484{
1485 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1486
1487 return npct->soc->functions[function].name;
1488}
1489
1490static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1491 unsigned function,
1492 const char * const **groups,
1493 unsigned * const num_groups)
1494{
1495 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1496
1497 *groups = npct->soc->functions[function].groups;
1498 *num_groups = npct->soc->functions[function].ngroups;
1499
1500 return 0;
1501}
1502
1503static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1504 unsigned group)
1505{
1506 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1507 const struct nmk_pingroup *g;
1508 static unsigned int slpm[NUM_BANKS];
1509 unsigned long flags;
1510 bool glitch;
1511 int ret = -EINVAL;
1512 int i;
1513
1514 g = &npct->soc->groups[group];
1515
1516 if (g->altsetting < 0)
1517 return -EINVAL;
1518
1519 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1520
Linus Walleijdaf73172012-05-22 11:46:45 +02001521 /*
1522 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1523 * we may pass through an undesired state. In this case we take
1524 * some extra care.
1525 *
1526 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1527 * - Save SLPM registers (since we have a shadow register in the
1528 * nmk_chip we're using that as backup)
1529 * - Set SLPM=0 for the IOs you want to switch and others to 1
1530 * - Configure the GPIO registers for the IOs that are being switched
1531 * - Set IOFORCE=1
1532 * - Modify the AFLSA/B registers for the IOs that are being switched
1533 * - Set IOFORCE=0
1534 * - Restore SLPM registers
1535 * - Any spurious wake up event during switch sequence to be ignored
1536 * and cleared
1537 *
1538 * We REALLY need to save ALL slpm registers, because the external
1539 * IOFORCE will switch *all* ports to their sleepmode setting to as
1540 * to avoid glitches. (Not just one port!)
1541 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001542 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001543
1544 if (glitch) {
1545 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1546
1547 /* Initially don't put any pins to sleep when switching */
1548 memset(slpm, 0xff, sizeof(slpm));
1549
1550 /*
1551 * Then mask the pins that need to be sleeping now when we're
1552 * switching to the ALT C function.
1553 */
1554 for (i = 0; i < g->npins; i++)
1555 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1556 nmk_gpio_glitch_slpm_init(slpm);
1557 }
1558
1559 for (i = 0; i < g->npins; i++) {
1560 struct pinctrl_gpio_range *range;
1561 struct nmk_gpio_chip *nmk_chip;
1562 struct gpio_chip *chip;
1563 unsigned bit;
1564
1565 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1566 if (!range) {
1567 dev_err(npct->dev,
1568 "invalid pin offset %d in group %s at index %d\n",
1569 g->pins[i], g->name, i);
1570 goto out_glitch;
1571 }
1572 if (!range->gc) {
1573 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1574 g->pins[i], g->name, i);
1575 goto out_glitch;
1576 }
1577 chip = range->gc;
1578 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1579 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1580
1581 clk_enable(nmk_chip->clk);
1582 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1583 /*
1584 * If the pin is switching to altfunc, and there was an
1585 * interrupt installed on it which has been lazy disabled,
1586 * actually mask the interrupt to prevent spurious interrupts
1587 * that would occur while the pin is under control of the
1588 * peripheral. Only SKE does this.
1589 */
1590 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1591
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001592 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1593 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001594 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001595
1596 /*
1597 * Call PRCM GPIOCR config function in case ALTC
1598 * has been selected:
1599 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1600 * must be set.
1601 * - If selection is pure ALTC and previous selection was ALTCx,
1602 * then some bits in PRCM GPIOCR registers must be cleared.
1603 */
1604 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1605 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1606 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001607 }
1608
1609 /* When all pins are successfully reconfigured we get here */
1610 ret = 0;
1611
1612out_glitch:
1613 if (glitch) {
1614 nmk_gpio_glitch_slpm_restore(slpm);
1615 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1616 }
1617
1618 return ret;
1619}
1620
1621static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1622 unsigned function, unsigned group)
1623{
1624 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1625 const struct nmk_pingroup *g;
1626
1627 g = &npct->soc->groups[group];
1628
1629 if (g->altsetting < 0)
1630 return;
1631
1632 /* Poke out the mux, set the pin to some default state? */
1633 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1634}
1635
1636int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1637 struct pinctrl_gpio_range *range,
1638 unsigned offset)
1639{
1640 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1641 struct nmk_gpio_chip *nmk_chip;
1642 struct gpio_chip *chip;
1643 unsigned bit;
1644
1645 if (!range) {
1646 dev_err(npct->dev, "invalid range\n");
1647 return -EINVAL;
1648 }
1649 if (!range->gc) {
1650 dev_err(npct->dev, "missing GPIO chip in range\n");
1651 return -EINVAL;
1652 }
1653 chip = range->gc;
1654 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1655
1656 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1657
1658 clk_enable(nmk_chip->clk);
1659 bit = offset % NMK_GPIO_PER_CHIP;
1660 /* There is no glitch when converting any pin to GPIO */
1661 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1662 clk_disable(nmk_chip->clk);
1663
1664 return 0;
1665}
1666
1667void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1668 struct pinctrl_gpio_range *range,
1669 unsigned offset)
1670{
1671 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1672
1673 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1674 /* Set the pin to some default state, GPIO is usually default */
1675}
1676
1677static struct pinmux_ops nmk_pinmux_ops = {
1678 .get_functions_count = nmk_pmx_get_funcs_cnt,
1679 .get_function_name = nmk_pmx_get_func_name,
1680 .get_function_groups = nmk_pmx_get_func_groups,
1681 .enable = nmk_pmx_enable,
1682 .disable = nmk_pmx_disable,
1683 .gpio_request_enable = nmk_gpio_request_enable,
1684 .gpio_disable_free = nmk_gpio_disable_free,
1685};
1686
Linus Walleijd41af622012-05-03 15:58:12 +02001687int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1688 unsigned pin,
1689 unsigned long *config)
1690{
1691 /* Not implemented */
1692 return -EINVAL;
1693}
1694
1695int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1696 unsigned pin,
1697 unsigned long config)
1698{
1699 static const char *pullnames[] = {
1700 [NMK_GPIO_PULL_NONE] = "none",
1701 [NMK_GPIO_PULL_UP] = "up",
1702 [NMK_GPIO_PULL_DOWN] = "down",
1703 [3] /* illegal */ = "??"
1704 };
1705 static const char *slpmnames[] = {
1706 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1707 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1708 };
1709 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1710 struct nmk_gpio_chip *nmk_chip;
1711 struct pinctrl_gpio_range *range;
1712 struct gpio_chip *chip;
1713 unsigned bit;
1714
1715 /*
1716 * The pin config contains pin number and altfunction fields, here
1717 * we just ignore that part. It's being handled by the framework and
1718 * pinmux callback respectively.
1719 */
1720 pin_cfg_t cfg = (pin_cfg_t) config;
1721 int pull = PIN_PULL(cfg);
1722 int slpm = PIN_SLPM(cfg);
1723 int output = PIN_DIR(cfg);
1724 int val = PIN_VAL(cfg);
1725 bool lowemi = PIN_LOWEMI(cfg);
1726 bool gpiomode = PIN_GPIOMODE(cfg);
1727 bool sleep = PIN_SLEEPMODE(cfg);
1728
1729 range = nmk_match_gpio_range(pctldev, pin);
1730 if (!range) {
1731 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1732 return -EINVAL;
1733 }
1734 if (!range->gc) {
1735 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1736 pin);
1737 return -EINVAL;
1738 }
1739 chip = range->gc;
1740 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1741
1742 if (sleep) {
1743 int slpm_pull = PIN_SLPM_PULL(cfg);
1744 int slpm_output = PIN_SLPM_DIR(cfg);
1745 int slpm_val = PIN_SLPM_VAL(cfg);
1746
1747 /* All pins go into GPIO mode at sleep */
1748 gpiomode = true;
1749
1750 /*
1751 * The SLPM_* values are normal values + 1 to allow zero to
1752 * mean "same as normal".
1753 */
1754 if (slpm_pull)
1755 pull = slpm_pull - 1;
1756 if (slpm_output)
1757 output = slpm_output - 1;
1758 if (slpm_val)
1759 val = slpm_val - 1;
1760
1761 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1762 pin,
1763 slpm_pull ? pullnames[pull] : "same",
1764 slpm_output ? (output ? "output" : "input") : "same",
1765 slpm_val ? (val ? "high" : "low") : "same");
1766 }
1767
1768 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1769 pin, cfg, pullnames[pull], slpmnames[slpm],
1770 output ? "output " : "input",
1771 output ? (val ? "high" : "low") : "",
1772 lowemi ? "on" : "off" );
1773
1774 clk_enable(nmk_chip->clk);
1775 bit = pin % NMK_GPIO_PER_CHIP;
1776 if (gpiomode)
1777 /* No glitch when going to GPIO mode */
1778 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1779 if (output)
1780 __nmk_gpio_make_output(nmk_chip, bit, val);
1781 else {
1782 __nmk_gpio_make_input(nmk_chip, bit);
1783 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1784 }
1785 /* TODO: isn't this only applicable on output pins? */
1786 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1787
1788 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1789 clk_disable(nmk_chip->clk);
1790 return 0;
1791}
1792
1793static struct pinconf_ops nmk_pinconf_ops = {
1794 .pin_config_get = nmk_pin_config_get,
1795 .pin_config_set = nmk_pin_config_set,
1796};
1797
Linus Walleije98ea772012-04-26 23:57:25 +02001798static struct pinctrl_desc nmk_pinctrl_desc = {
1799 .name = "pinctrl-nomadik",
1800 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001801 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001802 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001803 .owner = THIS_MODULE,
1804};
1805
Lee Jones855f80c2012-05-26 06:09:29 +01001806static const struct of_device_id nmk_pinctrl_match[] = {
1807 {
1808 .compatible = "stericsson,nmk_pinctrl",
1809 .data = (void *)PINCTRL_NMK_DB8500,
1810 },
1811 {},
1812};
1813
Linus Walleije98ea772012-04-26 23:57:25 +02001814static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1815{
1816 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001817 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001818 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01001819 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001820 int i;
1821
1822 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1823 if (!npct)
1824 return -ENOMEM;
1825
Lee Jones855f80c2012-05-26 06:09:29 +01001826 if (platid)
1827 version = platid->driver_data;
1828 else if (np)
1829 version = (unsigned int)
1830 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1831
Linus Walleije98ea772012-04-26 23:57:25 +02001832 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001833 if (version == PINCTRL_NMK_STN8815)
1834 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001835 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001836 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001837 if (version == PINCTRL_NMK_DB8540)
1838 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001839
1840 /*
1841 * We need all the GPIO drivers to probe FIRST, or we will not be able
1842 * to obtain references to the struct gpio_chip * for them, and we
1843 * need this to proceed.
1844 */
1845 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001846 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001847 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001848 return -EPROBE_DEFER;
1849 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001850 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001851 }
1852
1853 nmk_pinctrl_desc.pins = npct->soc->pins;
1854 nmk_pinctrl_desc.npins = npct->soc->npins;
1855 npct->dev = &pdev->dev;
1856 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1857 if (!npct->pctl) {
1858 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1859 return -EINVAL;
1860 }
1861
1862 /* We will handle a range of GPIO pins */
1863 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1864 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1865
1866 platform_set_drvdata(pdev, npct);
1867 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1868
1869 return 0;
1870}
1871
Lee Jones513c27f2012-04-13 15:05:05 +01001872static const struct of_device_id nmk_gpio_match[] = {
1873 { .compatible = "st,nomadik-gpio", },
1874 {}
1875};
1876
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001877static struct platform_driver nmk_gpio_driver = {
1878 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001879 .owner = THIS_MODULE,
1880 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001881 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301882 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001883 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001884};
1885
Linus Walleije98ea772012-04-26 23:57:25 +02001886static const struct platform_device_id nmk_pinctrl_id[] = {
1887 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1888 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001889 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Linus Walleije98ea772012-04-26 23:57:25 +02001890};
1891
1892static struct platform_driver nmk_pinctrl_driver = {
1893 .driver = {
1894 .owner = THIS_MODULE,
1895 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001896 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001897 },
1898 .probe = nmk_pinctrl_probe,
1899 .id_table = nmk_pinctrl_id,
1900};
1901
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001902static int __init nmk_gpio_init(void)
1903{
Linus Walleije98ea772012-04-26 23:57:25 +02001904 int ret;
1905
1906 ret = platform_driver_register(&nmk_gpio_driver);
1907 if (ret)
1908 return ret;
1909 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001910}
1911
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001912core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001913
1914MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1915MODULE_DESCRIPTION("Nomadik GPIO Driver");
1916MODULE_LICENSE("GPL");