blob: 98c088ae7f4a3c580accde293302f054707a54fd [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 Walleijbb16bd92012-10-10 14:27:58 +020033#include <linux/platform_data/pinctrl-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010034
Will Deaconadfed152011-02-28 10:12:29 +000035#include <asm/mach/irq.h>
36
Linus Walleije98ea772012-04-26 23:57:25 +020037#include "pinctrl-nomadik.h"
38
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010039/*
40 * The GPIO module in the Nomadik family of Systems-on-Chip is an
41 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020042 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010043 *
44 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
45 */
46
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010047struct nmk_gpio_chip {
48 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010049 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010050 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010051 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053052 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010053 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053054 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053055 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053056 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010057 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020058 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010059 /* Keep track of configured edges */
60 u32 edge_rising;
61 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053062 u32 real_wake;
63 u32 rwimsc;
64 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053065 u32 rimsc;
66 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020067 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053068 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010069};
70
Jonas Aabergf1671bf2012-10-25 08:40:42 +020071/**
72 * struct nmk_pinctrl - state container for the Nomadik pin controller
73 * @dev: containing device pointer
74 * @pctl: corresponding pin controller device
75 * @soc: SoC data for this specific chip
76 * @prcm_base: PRCM register range virtual base
77 */
Linus Walleije98ea772012-04-26 23:57:25 +020078struct nmk_pinctrl {
79 struct device *dev;
80 struct pinctrl_dev *pctl;
81 const struct nmk_pinctrl_soc_data *soc;
Jonas Aabergf1671bf2012-10-25 08:40:42 +020082 void __iomem *prcm_base;
Linus Walleije98ea772012-04-26 23:57:25 +020083};
84
Rabin Vincent01727e62010-12-13 12:02:40 +053085static struct nmk_gpio_chip *
86nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
87
88static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
89
90#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
91
Rabin Vincent6f9a9742010-06-02 05:50:28 +010092static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
93 unsigned offset, int gpio_mode)
94{
95 u32 bit = 1 << offset;
96 u32 afunc, bfunc;
97
98 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
99 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
100 if (gpio_mode & NMK_GPIO_ALT_A)
101 afunc |= bit;
102 if (gpio_mode & NMK_GPIO_ALT_B)
103 bfunc |= bit;
104 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
105 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
106}
107
Rabin Vincent81a3c292010-05-27 12:39:23 +0100108static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
109 unsigned offset, enum nmk_gpio_slpm mode)
110{
111 u32 bit = 1 << offset;
112 u32 slpm;
113
114 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
115 if (mode == NMK_GPIO_SLPM_NOCHANGE)
116 slpm |= bit;
117 else
118 slpm &= ~bit;
119 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
120}
121
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100122static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
123 unsigned offset, enum nmk_gpio_pull pull)
124{
125 u32 bit = 1 << offset;
126 u32 pdis;
127
128 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200129 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100130 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200131 nmk_chip->pull_up &= ~bit;
132 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100133 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200134 }
135
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100136 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
137
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200138 if (pull == NMK_GPIO_PULL_UP) {
139 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100140 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200141 } else if (pull == NMK_GPIO_PULL_DOWN) {
142 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100143 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200144 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100145}
146
Rabin Vincentebc61782011-09-28 15:49:11 +0530147static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
148 unsigned offset, bool lowemi)
149{
150 u32 bit = BIT(offset);
151 bool enabled = nmk_chip->lowemi & bit;
152
153 if (lowemi == enabled)
154 return;
155
156 if (lowemi)
157 nmk_chip->lowemi |= bit;
158 else
159 nmk_chip->lowemi &= ~bit;
160
161 writel_relaxed(nmk_chip->lowemi,
162 nmk_chip->addr + NMK_GPIO_LOWEMI);
163}
164
Rabin Vincent378be062010-06-02 06:06:29 +0100165static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
166 unsigned offset)
167{
168 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
169}
170
Rabin Vincent6720db72010-09-02 11:28:48 +0100171static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
172 unsigned offset, int val)
173{
174 if (val)
175 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
176 else
177 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
178}
179
180static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
181 unsigned offset, int val)
182{
183 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
184 __nmk_gpio_set_output(nmk_chip, offset, val);
185}
186
Rabin Vincent01727e62010-12-13 12:02:40 +0530187static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
188 unsigned offset, int gpio_mode,
189 bool glitch)
190{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530191 u32 rwimsc = nmk_chip->rwimsc;
192 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530193
194 if (glitch && nmk_chip->set_ioforce) {
195 u32 bit = BIT(offset);
196
Rabin Vincent01727e62010-12-13 12:02:40 +0530197 /* Prevent spurious wakeups */
198 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
199 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
200
201 nmk_chip->set_ioforce(true);
202 }
203
204 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
205
206 if (glitch && nmk_chip->set_ioforce) {
207 nmk_chip->set_ioforce(false);
208
209 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
210 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
211 }
212}
213
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530214static void
215nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
216{
217 u32 falling = nmk_chip->fimsc & BIT(offset);
218 u32 rising = nmk_chip->rimsc & BIT(offset);
219 int gpio = nmk_chip->chip.base + offset;
220 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
221 struct irq_data *d = irq_get_irq_data(irq);
222
223 if (!rising && !falling)
224 return;
225
226 if (!d || !irqd_irq_disabled(d))
227 return;
228
229 if (rising) {
230 nmk_chip->rimsc &= ~BIT(offset);
231 writel_relaxed(nmk_chip->rimsc,
232 nmk_chip->addr + NMK_GPIO_RIMSC);
233 }
234
235 if (falling) {
236 nmk_chip->fimsc &= ~BIT(offset);
237 writel_relaxed(nmk_chip->fimsc,
238 nmk_chip->addr + NMK_GPIO_FIMSC);
239 }
240
241 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
242}
243
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200244static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
245{
246 u32 val;
247
248 val = readl(reg);
249 val = ((val & ~mask) | (value & mask));
250 writel(val, reg);
251}
252
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200253static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
254 unsigned offset, unsigned alt_num)
255{
256 int i;
257 u16 reg;
258 u8 bit;
259 u8 alt_index;
260 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
261 const u16 *gpiocr_regs;
262
263 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
264 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
265 alt_num);
266 return;
267 }
268
269 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
270 if (npct->soc->altcx_pins[i].pin == offset)
271 break;
272 }
273 if (i == npct->soc->npins_altcx) {
274 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
275 offset);
276 return;
277 }
278
279 pin_desc = npct->soc->altcx_pins + i;
280 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
281
282 /*
283 * If alt_num is NULL, just clear current ALTCx selection
284 * to make sure we come back to a pure ALTC selection
285 */
286 if (!alt_num) {
287 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
288 if (pin_desc->altcx[i].used == true) {
289 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
290 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200291 if (readl(npct->prcm_base + reg) & BIT(bit)) {
292 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200293 dev_dbg(npct->dev,
294 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
295 offset, i+1);
296 }
297 }
298 }
299 return;
300 }
301
302 alt_index = alt_num - 1;
303 if (pin_desc->altcx[alt_index].used == false) {
304 dev_warn(npct->dev,
305 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
306 offset, alt_num);
307 return;
308 }
309
310 /*
311 * Check if any other ALTCx functions are activated on this pin
312 * and disable it first.
313 */
314 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
315 if (i == alt_index)
316 continue;
317 if (pin_desc->altcx[i].used == true) {
318 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
319 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200320 if (readl(npct->prcm_base + reg) & BIT(bit)) {
321 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200322 dev_dbg(npct->dev,
323 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
324 offset, i+1);
325 }
326 }
327 }
328
329 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
330 bit = pin_desc->altcx[alt_index].control_bit;
331 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
332 offset, alt_index+1);
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200333 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200334}
335
Rabin Vincent378be062010-06-02 06:06:29 +0100336static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530337 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100338{
339 static const char *afnames[] = {
340 [NMK_GPIO_ALT_GPIO] = "GPIO",
341 [NMK_GPIO_ALT_A] = "A",
342 [NMK_GPIO_ALT_B] = "B",
343 [NMK_GPIO_ALT_C] = "C"
344 };
345 static const char *pullnames[] = {
346 [NMK_GPIO_PULL_NONE] = "none",
347 [NMK_GPIO_PULL_UP] = "up",
348 [NMK_GPIO_PULL_DOWN] = "down",
349 [3] /* illegal */ = "??"
350 };
351 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100352 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
353 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100354 };
355
356 int pin = PIN_NUM(cfg);
357 int pull = PIN_PULL(cfg);
358 int af = PIN_ALT(cfg);
359 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100360 int output = PIN_DIR(cfg);
361 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530362 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100363
Rabin Vincentdacdc962010-12-03 20:35:37 +0530364 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
365 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100366 output ? "output " : "input",
367 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100368
Rabin Vincentdacdc962010-12-03 20:35:37 +0530369 if (sleep) {
370 int slpm_pull = PIN_SLPM_PULL(cfg);
371 int slpm_output = PIN_SLPM_DIR(cfg);
372 int slpm_val = PIN_SLPM_VAL(cfg);
373
Rabin Vincent3546d152010-11-25 11:38:27 +0530374 af = NMK_GPIO_ALT_GPIO;
375
Rabin Vincentdacdc962010-12-03 20:35:37 +0530376 /*
377 * The SLPM_* values are normal values + 1 to allow zero to
378 * mean "same as normal".
379 */
380 if (slpm_pull)
381 pull = slpm_pull - 1;
382 if (slpm_output)
383 output = slpm_output - 1;
384 if (slpm_val)
385 val = slpm_val - 1;
386
387 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
388 pin,
389 slpm_pull ? pullnames[pull] : "same",
390 slpm_output ? (output ? "output" : "input") : "same",
391 slpm_val ? (val ? "high" : "low") : "same");
392 }
393
Rabin Vincent6720db72010-09-02 11:28:48 +0100394 if (output)
395 __nmk_gpio_make_output(nmk_chip, offset, val);
396 else {
397 __nmk_gpio_make_input(nmk_chip, offset);
398 __nmk_gpio_set_pull(nmk_chip, offset, pull);
399 }
400
Rabin Vincentebc61782011-09-28 15:49:11 +0530401 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
402
Rabin Vincent01727e62010-12-13 12:02:40 +0530403 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530404 * If the pin is switching to altfunc, and there was an interrupt
405 * installed on it which has been lazy disabled, actually mask the
406 * interrupt to prevent spurious interrupts that would occur while the
407 * pin is under control of the peripheral. Only SKE does this.
408 */
409 if (af != NMK_GPIO_ALT_GPIO)
410 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
411
412 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530413 * If we've backed up the SLPM registers (glitch workaround), modify
414 * the backups since they will be restored.
415 */
416 if (slpmregs) {
417 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
418 slpmregs[nmk_chip->bank] |= BIT(offset);
419 else
420 slpmregs[nmk_chip->bank] &= ~BIT(offset);
421 } else
422 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
423
424 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
425}
426
427/*
428 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
429 * - Save SLPM registers
430 * - Set SLPM=0 for the IOs you want to switch and others to 1
431 * - Configure the GPIO registers for the IOs that are being switched
432 * - Set IOFORCE=1
433 * - Modify the AFLSA/B registers for the IOs that are being switched
434 * - Set IOFORCE=0
435 * - Restore SLPM registers
436 * - Any spurious wake up event during switch sequence to be ignored and
437 * cleared
438 */
439static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
440{
441 int i;
442
443 for (i = 0; i < NUM_BANKS; i++) {
444 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
445 unsigned int temp = slpm[i];
446
447 if (!chip)
448 break;
449
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200450 clk_enable(chip->clk);
451
Rabin Vincent01727e62010-12-13 12:02:40 +0530452 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
453 writel(temp, chip->addr + NMK_GPIO_SLPC);
454 }
455}
456
457static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
458{
459 int i;
460
461 for (i = 0; i < NUM_BANKS; i++) {
462 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
463
464 if (!chip)
465 break;
466
467 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200468
469 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530470 }
471}
472
473static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
474{
475 static unsigned int slpm[NUM_BANKS];
476 unsigned long flags;
477 bool glitch = false;
478 int ret = 0;
479 int i;
480
481 for (i = 0; i < num; i++) {
482 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
483 glitch = true;
484 break;
485 }
486 }
487
488 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
489
490 if (glitch) {
491 memset(slpm, 0xff, sizeof(slpm));
492
493 for (i = 0; i < num; i++) {
494 int pin = PIN_NUM(cfgs[i]);
495 int offset = pin % NMK_GPIO_PER_CHIP;
496
497 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
498 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
499 }
500
501 nmk_gpio_glitch_slpm_init(slpm);
502 }
503
504 for (i = 0; i < num; i++) {
505 struct nmk_gpio_chip *nmk_chip;
506 int pin = PIN_NUM(cfgs[i]);
507
Lee Jonesa60b57e2012-04-19 21:36:31 +0100508 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530509 if (!nmk_chip) {
510 ret = -EINVAL;
511 break;
512 }
513
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200514 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530515 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100516 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530517 cfgs[i], sleep, glitch ? slpm : NULL);
518 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200519 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530520 }
521
522 if (glitch)
523 nmk_gpio_glitch_slpm_restore(slpm);
524
525 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
526
527 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100528}
529
530/**
531 * nmk_config_pin - configure a pin's mux attributes
532 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200533 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100534 * Configures a pin's mode (alternate function or GPIO), its pull up status,
535 * and its sleep mode based on the specified configuration. The @cfg is
536 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
537 * are constructed using, and can be further enhanced with, the macros in
Linus Walleij287f1212012-10-10 14:35:17 +0200538 * <linux/platform_data/pinctrl-nomadik.h>
Rabin Vincent378be062010-06-02 06:06:29 +0100539 *
540 * If a pin's mode is set to GPIO, it is configured as an input to avoid
541 * side-effects. The gpio can be manipulated later using standard GPIO API
542 * calls.
543 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530544int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100545{
Rabin Vincent01727e62010-12-13 12:02:40 +0530546 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100547}
548EXPORT_SYMBOL(nmk_config_pin);
549
550/**
551 * nmk_config_pins - configure several pins at once
552 * @cfgs: array of pin configurations
553 * @num: number of elments in the array
554 *
555 * Configures several pins using nmk_config_pin(). Refer to that function for
556 * further information.
557 */
558int nmk_config_pins(pin_cfg_t *cfgs, int num)
559{
Rabin Vincent01727e62010-12-13 12:02:40 +0530560 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100561}
562EXPORT_SYMBOL(nmk_config_pins);
563
Rabin Vincentdacdc962010-12-03 20:35:37 +0530564int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
565{
Rabin Vincent01727e62010-12-13 12:02:40 +0530566 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530567}
568EXPORT_SYMBOL(nmk_config_pins_sleep);
569
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100570/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100571 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
572 * @gpio: pin number
573 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
574 *
Linus Walleij33d78642011-06-09 11:08:47 +0200575 * This register is actually in the pinmux layer, not the GPIO block itself.
576 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
577 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
578 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
579 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
580 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
581 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100582 *
Linus Walleij33d78642011-06-09 11:08:47 +0200583 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
584 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
585 * entered) regardless of the altfunction selected. Also wake-up detection is
586 * ENABLED.
587 *
588 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
589 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
590 * (for altfunction GPIO) or respective on-chip peripherals (for other
591 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
592 *
593 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100594 */
595int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
596{
597 struct nmk_gpio_chip *nmk_chip;
598 unsigned long flags;
599
Lee Jonesa60b57e2012-04-19 21:36:31 +0100600 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100601 if (!nmk_chip)
602 return -EINVAL;
603
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200604 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530605 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
606 spin_lock(&nmk_chip->lock);
607
Lee Jonesa60b57e2012-04-19 21:36:31 +0100608 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530609
610 spin_unlock(&nmk_chip->lock);
611 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200612 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100613
614 return 0;
615}
616
617/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100618 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
619 * @gpio: pin number
620 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
621 *
622 * Enables/disables pull up/down on a specified pin. This only takes effect if
623 * the pin is configured as an input (either explicitly or by the alternate
624 * function).
625 *
626 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
627 * configured as an input. Otherwise, due to the way the controller registers
628 * work, this function will change the value output on the pin.
629 */
630int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
631{
632 struct nmk_gpio_chip *nmk_chip;
633 unsigned long flags;
634
Lee Jonesa60b57e2012-04-19 21:36:31 +0100635 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100636 if (!nmk_chip)
637 return -EINVAL;
638
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200639 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100640 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100641 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100642 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200643 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100644
645 return 0;
646}
647
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100648/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200649/**
650 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
651 * @gpio: pin number
652 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
653 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
654 *
655 * Sets the mode of the specified pin to one of the alternate functions or
656 * plain GPIO.
657 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100658int nmk_gpio_set_mode(int gpio, int gpio_mode)
659{
660 struct nmk_gpio_chip *nmk_chip;
661 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100662
Lee Jonesa60b57e2012-04-19 21:36:31 +0100663 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100664 if (!nmk_chip)
665 return -EINVAL;
666
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200667 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100668 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100669 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100670 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200671 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100672
673 return 0;
674}
675EXPORT_SYMBOL(nmk_gpio_set_mode);
676
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200677static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
678{
679 int i;
680 u16 reg;
681 u8 bit;
682 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
683 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
684 const u16 *gpiocr_regs;
685
686 for (i = 0; i < npct->soc->npins_altcx; i++) {
687 if (npct->soc->altcx_pins[i].pin == gpio)
688 break;
689 }
690 if (i == npct->soc->npins_altcx)
691 return NMK_GPIO_ALT_C;
692
693 pin_desc = npct->soc->altcx_pins + i;
694 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
695 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
696 if (pin_desc->altcx[i].used == true) {
697 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
698 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200699 if (readl(npct->prcm_base + reg) & BIT(bit))
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200700 return NMK_GPIO_ALT_C+i+1;
701 }
702 }
703 return NMK_GPIO_ALT_C;
704}
705
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100706int nmk_gpio_get_mode(int gpio)
707{
708 struct nmk_gpio_chip *nmk_chip;
709 u32 afunc, bfunc, bit;
710
Lee Jonesa60b57e2012-04-19 21:36:31 +0100711 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100712 if (!nmk_chip)
713 return -EINVAL;
714
Lee Jonesa60b57e2012-04-19 21:36:31 +0100715 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100716
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200717 clk_enable(nmk_chip->clk);
718
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100719 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
720 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
721
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200722 clk_disable(nmk_chip->clk);
723
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100724 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
725}
726EXPORT_SYMBOL(nmk_gpio_get_mode);
727
728
729/* IRQ functions */
730static inline int nmk_gpio_get_bitmask(int gpio)
731{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100732 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100733}
734
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100735static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100736{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100737 struct nmk_gpio_chip *nmk_chip;
738
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100739 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100740 if (!nmk_chip)
741 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200742
743 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100744 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200745 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100746}
747
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100748enum nmk_gpio_irq_type {
749 NORMAL,
750 WAKE,
751};
752
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100753static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100754 int gpio, enum nmk_gpio_irq_type which,
755 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100756{
757 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530758 u32 *rimscval;
759 u32 *fimscval;
760 u32 rimscreg;
761 u32 fimscreg;
762
763 if (which == NORMAL) {
764 rimscreg = NMK_GPIO_RIMSC;
765 fimscreg = NMK_GPIO_FIMSC;
766 rimscval = &nmk_chip->rimsc;
767 fimscval = &nmk_chip->fimsc;
768 } else {
769 rimscreg = NMK_GPIO_RWIMSC;
770 fimscreg = NMK_GPIO_FWIMSC;
771 rimscval = &nmk_chip->rwimsc;
772 fimscval = &nmk_chip->fwimsc;
773 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100774
775 /* we must individually set/clear the two edges */
776 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100777 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530778 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100779 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530780 *rimscval &= ~bitmask;
781 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100782 }
783 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100784 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530785 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100786 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530787 *fimscval &= ~bitmask;
788 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100789 }
790}
791
Rabin Vincentb9df4682011-02-10 11:45:58 +0530792static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
793 int gpio, bool on)
794{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530795 /*
796 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
797 * disabled, since setting SLPM to 1 increases power consumption, and
798 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
799 */
800 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200801 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530802 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200803 }
804
Rabin Vincentb9df4682011-02-10 11:45:58 +0530805 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
806}
807
808static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100809{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100810 struct nmk_gpio_chip *nmk_chip;
811 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100812 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100813
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100814 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100815 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100816 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100817 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100818
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200819 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530820 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
821 spin_lock(&nmk_chip->lock);
822
Lee Jonesa60b57e2012-04-19 21:36:31 +0100823 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530824
825 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100826 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530827
828 spin_unlock(&nmk_chip->lock);
829 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200830 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100831
832 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100833}
834
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100835static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100836{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530837 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100838}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100839
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100840static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100841{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530842 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100843}
844
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100845static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100846{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100847 struct nmk_gpio_chip *nmk_chip;
848 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530849 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100850
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100851 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100852 if (!nmk_chip)
853 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100854 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100855
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200856 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530857 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
858 spin_lock(&nmk_chip->lock);
859
Linus Walleij479a0c72011-09-20 10:50:15 +0200860 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100861 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530862
863 if (on)
864 nmk_chip->real_wake |= bitmask;
865 else
866 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530867
868 spin_unlock(&nmk_chip->lock);
869 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200870 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100871
872 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873}
874
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100875static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100876{
Linus Walleij479a0c72011-09-20 10:50:15 +0200877 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200878 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100879 struct nmk_gpio_chip *nmk_chip;
880 unsigned long flags;
881 u32 bitmask;
882
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100883 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100884 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100885 if (!nmk_chip)
886 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100887 if (type & IRQ_TYPE_LEVEL_HIGH)
888 return -EINVAL;
889 if (type & IRQ_TYPE_LEVEL_LOW)
890 return -EINVAL;
891
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200892 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100893 spin_lock_irqsave(&nmk_chip->lock, flags);
894
Rabin Vincent7a852d82010-05-06 10:43:55 +0100895 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100896 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100897
Rabin Vincentb9df4682011-02-10 11:45:58 +0530898 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100899 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100900
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100901 nmk_chip->edge_rising &= ~bitmask;
902 if (type & IRQ_TYPE_EDGE_RISING)
903 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100904
905 nmk_chip->edge_falling &= ~bitmask;
906 if (type & IRQ_TYPE_EDGE_FALLING)
907 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100908
909 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100910 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100911
Rabin Vincentb9df4682011-02-10 11:45:58 +0530912 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100913 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100914
915 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200916 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100917
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100918 return 0;
919}
920
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200921static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
922{
923 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
924
925 clk_enable(nmk_chip->clk);
926 nmk_gpio_irq_unmask(d);
927 return 0;
928}
929
930static void nmk_gpio_irq_shutdown(struct irq_data *d)
931{
932 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
933
934 nmk_gpio_irq_mask(d);
935 clk_disable(nmk_chip->clk);
936}
937
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100938static struct irq_chip nmk_gpio_irq_chip = {
939 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100940 .irq_ack = nmk_gpio_irq_ack,
941 .irq_mask = nmk_gpio_irq_mask,
942 .irq_unmask = nmk_gpio_irq_unmask,
943 .irq_set_type = nmk_gpio_irq_set_type,
944 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200945 .irq_startup = nmk_gpio_irq_startup,
946 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200947 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100948};
949
Rabin Vincent33b744b2010-10-14 10:38:03 +0530950static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
951 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100952{
953 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100954 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100955
Will Deaconadfed152011-02-28 10:12:29 +0000956 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100957
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100958 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530959 while (status) {
960 int bit = __ffs(status);
961
Linus Walleij95f0bc92012-09-27 14:14:09 +0200962 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530963 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100964 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100965
Will Deaconadfed152011-02-28 10:12:29 +0000966 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100967}
968
Rabin Vincent33b744b2010-10-14 10:38:03 +0530969static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
970{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100971 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200972 u32 status;
973
974 clk_enable(nmk_chip->clk);
975 status = readl(nmk_chip->addr + NMK_GPIO_IS);
976 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530977
978 __nmk_gpio_irq_handler(irq, desc, status);
979}
980
981static void nmk_gpio_secondary_irq_handler(unsigned int irq,
982 struct irq_desc *desc)
983{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100984 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530985 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
986
987 __nmk_gpio_irq_handler(irq, desc, status);
988}
989
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100990static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
991{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100992 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
993 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530994
995 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100996 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530997 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100998 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530999 }
1000
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001001 return 0;
1002}
1003
1004/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001005
1006static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
1007{
1008 /*
1009 * Map back to global GPIO space and request muxing, the direction
1010 * parameter does not matter for this controller.
1011 */
1012 int gpio = chip->base + offset;
1013
1014 return pinctrl_request_gpio(gpio);
1015}
1016
1017static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
1018{
1019 int gpio = chip->base + offset;
1020
1021 pinctrl_free_gpio(gpio);
1022}
1023
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001024static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
1025{
1026 struct nmk_gpio_chip *nmk_chip =
1027 container_of(chip, struct nmk_gpio_chip, chip);
1028
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001029 clk_enable(nmk_chip->clk);
1030
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001031 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001032
1033 clk_disable(nmk_chip->clk);
1034
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001035 return 0;
1036}
1037
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001038static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1039{
1040 struct nmk_gpio_chip *nmk_chip =
1041 container_of(chip, struct nmk_gpio_chip, chip);
1042 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001043 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001044
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001045 clk_enable(nmk_chip->clk);
1046
1047 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1048
1049 clk_disable(nmk_chip->clk);
1050
1051 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001052}
1053
1054static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1055 int val)
1056{
1057 struct nmk_gpio_chip *nmk_chip =
1058 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001059
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001060 clk_enable(nmk_chip->clk);
1061
Rabin Vincent6720db72010-09-02 11:28:48 +01001062 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001063
1064 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001065}
1066
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001067static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1068 int val)
1069{
1070 struct nmk_gpio_chip *nmk_chip =
1071 container_of(chip, struct nmk_gpio_chip, chip);
1072
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001073 clk_enable(nmk_chip->clk);
1074
Rabin Vincent6720db72010-09-02 11:28:48 +01001075 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001076
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001077 clk_disable(nmk_chip->clk);
1078
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001079 return 0;
1080}
1081
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001082static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1083{
1084 struct nmk_gpio_chip *nmk_chip =
1085 container_of(chip, struct nmk_gpio_chip, chip);
1086
Linus Walleij268300b2012-10-19 17:06:54 +02001087 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001088}
1089
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301090#ifdef CONFIG_DEBUG_FS
1091
1092#include <linux/seq_file.h>
1093
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001094static void nmk_gpio_dbg_show_one(struct seq_file *s,
1095 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
1096 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301097{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001098 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301099 struct nmk_gpio_chip *nmk_chip =
1100 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001101 int mode;
1102 bool is_out;
1103 bool pull;
1104 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301105 const char *modes[] = {
1106 [NMK_GPIO_ALT_GPIO] = "gpio",
1107 [NMK_GPIO_ALT_A] = "altA",
1108 [NMK_GPIO_ALT_B] = "altB",
1109 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001110 [NMK_GPIO_ALT_C+1] = "altC1",
1111 [NMK_GPIO_ALT_C+2] = "altC2",
1112 [NMK_GPIO_ALT_C+3] = "altC3",
1113 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301114 };
1115
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001116 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001117 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1118 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1119 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001120 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1121 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001122
Linus Walleij6f4350a2012-05-02 21:06:13 +02001123 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1124 gpio, label ?: "(none)",
1125 is_out ? "out" : "in ",
1126 chip->get
1127 ? (chip->get(chip, offset) ? "hi" : "lo")
1128 : "? ",
1129 (mode < 0) ? "unknown" : modes[mode],
1130 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301131
Linus Walleij6f4350a2012-05-02 21:06:13 +02001132 if (label && !is_out) {
1133 int irq = gpio_to_irq(gpio);
1134 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001135
Linus Walleij6f4350a2012-05-02 21:06:13 +02001136 /* This races with request_irq(), set_irq_type(),
1137 * and set_irq_wake() ... but those are "rare".
1138 */
1139 if (irq >= 0 && desc->action) {
1140 char *trigger;
1141 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001142
Linus Walleij6f4350a2012-05-02 21:06:13 +02001143 if (nmk_chip->edge_rising & bitmask)
1144 trigger = "edge-rising";
1145 else if (nmk_chip->edge_falling & bitmask)
1146 trigger = "edge-falling";
1147 else
1148 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001149
Linus Walleij6f4350a2012-05-02 21:06:13 +02001150 seq_printf(s, " irq-%d %s%s",
1151 irq, trigger,
1152 irqd_is_wakeup_set(&desc->irq_data)
1153 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001154 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301155 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001156 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301157}
1158
Linus Walleij6f4350a2012-05-02 21:06:13 +02001159static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1160{
1161 unsigned i;
1162 unsigned gpio = chip->base;
1163
1164 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001165 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001166 seq_printf(s, "\n");
1167 }
1168}
1169
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301170#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001171static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001172 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +02001173 struct gpio_chip *chip,
1174 unsigned offset, unsigned gpio)
1175{
1176}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301177#define nmk_gpio_dbg_show NULL
1178#endif
1179
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001180/* This structure is replicated for each GPIO block allocated at probe time */
1181static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001182 .request = nmk_gpio_request,
1183 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001184 .direction_input = nmk_gpio_make_input,
1185 .get = nmk_gpio_get_input,
1186 .direction_output = nmk_gpio_make_output,
1187 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001188 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301189 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001190 .can_sleep = 0,
1191};
1192
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001193void nmk_gpio_clocks_enable(void)
1194{
1195 int i;
1196
1197 for (i = 0; i < NUM_BANKS; i++) {
1198 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1199
1200 if (!chip)
1201 continue;
1202
1203 clk_enable(chip->clk);
1204 }
1205}
1206
1207void nmk_gpio_clocks_disable(void)
1208{
1209 int i;
1210
1211 for (i = 0; i < NUM_BANKS; i++) {
1212 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1213
1214 if (!chip)
1215 continue;
1216
1217 clk_disable(chip->clk);
1218 }
1219}
1220
Rabin Vincentb9df4682011-02-10 11:45:58 +05301221/*
1222 * Called from the suspend/resume path to only keep the real wakeup interrupts
1223 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1224 * and not the rest of the interrupts which we needed to have as wakeups for
1225 * cpuidle.
1226 *
1227 * PM ops are not used since this needs to be done at the end, after all the
1228 * other drivers are done with their suspend callbacks.
1229 */
1230void nmk_gpio_wakeups_suspend(void)
1231{
1232 int i;
1233
1234 for (i = 0; i < NUM_BANKS; i++) {
1235 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1236
1237 if (!chip)
1238 break;
1239
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001240 clk_enable(chip->clk);
1241
Rabin Vincentb9df4682011-02-10 11:45:58 +05301242 writel(chip->rwimsc & chip->real_wake,
1243 chip->addr + NMK_GPIO_RWIMSC);
1244 writel(chip->fwimsc & chip->real_wake,
1245 chip->addr + NMK_GPIO_FWIMSC);
1246
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001247 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301248 }
1249}
1250
1251void nmk_gpio_wakeups_resume(void)
1252{
1253 int i;
1254
1255 for (i = 0; i < NUM_BANKS; i++) {
1256 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1257
1258 if (!chip)
1259 break;
1260
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001261 clk_enable(chip->clk);
1262
Rabin Vincentb9df4682011-02-10 11:45:58 +05301263 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1264 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1265
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001266 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301267 }
1268}
1269
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001270/*
1271 * Read the pull up/pull down status.
1272 * A bit set in 'pull_up' means that pull up
1273 * is selected if pull is enabled in PDIS register.
1274 * Note: only pull up/down set via this driver can
1275 * be detected due to HW limitations.
1276 */
1277void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1278{
1279 if (gpio_bank < NUM_BANKS) {
1280 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1281
1282 if (!chip)
1283 return;
1284
1285 *pull_up = chip->pull_up;
1286 }
1287}
1288
Lee Jonesa60b57e2012-04-19 21:36:31 +01001289int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1290 irq_hw_number_t hwirq)
1291{
1292 struct nmk_gpio_chip *nmk_chip = d->host_data;
1293
1294 if (!nmk_chip)
1295 return -EINVAL;
1296
1297 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1298 set_irq_flags(irq, IRQF_VALID);
1299 irq_set_chip_data(irq, nmk_chip);
1300 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1301
1302 return 0;
1303}
1304
1305const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1306 .map = nmk_gpio_irq_map,
1307 .xlate = irq_domain_xlate_twocell,
1308};
1309
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001310static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001311{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001312 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001313 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001314 struct nmk_gpio_chip *nmk_chip;
1315 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001316 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001317 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301318 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001319 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001320 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001321 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001322 int ret;
1323
Lee Jones513c27f2012-04-13 15:05:05 +01001324 if (!pdata && !np) {
1325 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001326 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001327 }
1328
1329 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001330 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001331 if (!pdata)
1332 return -ENOMEM;
1333
Lee Jones612e1d52012-06-14 11:27:56 +01001334 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001335 pdata->supports_sleepmode = true;
1336
1337 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1338 dev_err(&dev->dev, "gpio-bank property not found\n");
1339 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001340 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001341 }
1342
1343 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1344 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1345 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001346
1347 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1348 if (!res) {
1349 ret = -ENOENT;
1350 goto out;
1351 }
1352
1353 irq = platform_get_irq(dev, 0);
1354 if (irq < 0) {
1355 ret = irq;
1356 goto out;
1357 }
1358
Rabin Vincent33b744b2010-10-14 10:38:03 +05301359 secondary_irq = platform_get_irq(dev, 1);
1360 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364
Linus Walleij5e754f32012-07-03 23:05:14 +02001365 base = devm_request_and_ioremap(&dev->dev, res);
1366 if (!base) {
1367 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001368 goto out;
1369 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001370
Linus Walleij5e754f32012-07-03 23:05:14 +02001371 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001372 if (IS_ERR(clk)) {
1373 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001374 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001375 }
Linus Walleijefec3812012-06-06 22:50:41 +02001376 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001377
Linus Walleij5e754f32012-07-03 23:05:14 +02001378 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001379 if (!nmk_chip) {
1380 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001381 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001382 }
Lee Jones513c27f2012-04-13 15:05:05 +01001383
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001384 /*
1385 * The virt address in nmk_chip->addr is in the nomadik register space,
1386 * so we can simply convert the resource address, without remapping
1387 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301388 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001389 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001390 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001391 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001392 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301393 nmk_chip->secondary_parent_irq = secondary_irq;
1394 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301395 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001396 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001397 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001398
1399 chip = &nmk_chip->chip;
1400 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301401 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301402 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001403 chip->dev = &dev->dev;
1404 chip->owner = THIS_MODULE;
1405
Rabin Vincentebc61782011-09-28 15:49:11 +05301406 clk_enable(nmk_chip->clk);
1407 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1408 clk_disable(nmk_chip->clk);
1409
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001410#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001411 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001412#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001413
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001414 ret = gpiochip_add(&nmk_chip->chip);
1415 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001416 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001417
Rabin Vincent01727e62010-12-13 12:02:40 +05301418 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1419
1420 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001421
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001422 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001423
Linus Walleij51f58c62012-10-11 16:33:44 +02001424 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001425 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001426 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001427 NMK_GPIO_PER_CHIP, irq_start,
1428 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001429 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001430 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001431 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001432 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001433 }
1434
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001435 nmk_gpio_init_irq(nmk_chip);
1436
Lee Jones513c27f2012-04-13 15:05:05 +01001437 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1438
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001439 return 0;
1440
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001441out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001442 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1443 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001444
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001445 return ret;
1446}
1447
Linus Walleije98ea772012-04-26 23:57:25 +02001448static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1449{
1450 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1451
1452 return npct->soc->ngroups;
1453}
1454
1455static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1456 unsigned selector)
1457{
1458 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1459
1460 return npct->soc->groups[selector].name;
1461}
1462
1463static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1464 const unsigned **pins,
1465 unsigned *num_pins)
1466{
1467 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1468
1469 *pins = npct->soc->groups[selector].pins;
1470 *num_pins = npct->soc->groups[selector].npins;
1471 return 0;
1472}
1473
Linus Walleij24cbdd72012-05-02 21:28:00 +02001474static struct pinctrl_gpio_range *
1475nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1476{
1477 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1478 int i;
1479
1480 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1481 struct pinctrl_gpio_range *range;
1482
1483 range = &npct->soc->gpio_ranges[i];
1484 if (offset >= range->pin_base &&
1485 offset <= (range->pin_base + range->npins - 1))
1486 return range;
1487 }
1488 return NULL;
1489}
1490
Linus Walleije98ea772012-04-26 23:57:25 +02001491static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1492 unsigned offset)
1493{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001494 struct pinctrl_gpio_range *range;
1495 struct gpio_chip *chip;
1496
1497 range = nmk_match_gpio_range(pctldev, offset);
1498 if (!range || !range->gc) {
1499 seq_printf(s, "invalid pin offset");
1500 return;
1501 }
1502 chip = range->gc;
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001503 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001504}
1505
1506static struct pinctrl_ops nmk_pinctrl_ops = {
1507 .get_groups_count = nmk_get_groups_cnt,
1508 .get_group_name = nmk_get_group_name,
1509 .get_group_pins = nmk_get_group_pins,
1510 .pin_dbg_show = nmk_pin_dbg_show,
1511};
1512
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001513static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1514{
1515 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1516
1517 return npct->soc->nfunctions;
1518}
1519
1520static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1521 unsigned function)
1522{
1523 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1524
1525 return npct->soc->functions[function].name;
1526}
1527
1528static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1529 unsigned function,
1530 const char * const **groups,
1531 unsigned * const num_groups)
1532{
1533 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1534
1535 *groups = npct->soc->functions[function].groups;
1536 *num_groups = npct->soc->functions[function].ngroups;
1537
1538 return 0;
1539}
1540
1541static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1542 unsigned group)
1543{
1544 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1545 const struct nmk_pingroup *g;
1546 static unsigned int slpm[NUM_BANKS];
1547 unsigned long flags;
1548 bool glitch;
1549 int ret = -EINVAL;
1550 int i;
1551
1552 g = &npct->soc->groups[group];
1553
1554 if (g->altsetting < 0)
1555 return -EINVAL;
1556
1557 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1558
Linus Walleijdaf73172012-05-22 11:46:45 +02001559 /*
1560 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1561 * we may pass through an undesired state. In this case we take
1562 * some extra care.
1563 *
1564 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1565 * - Save SLPM registers (since we have a shadow register in the
1566 * nmk_chip we're using that as backup)
1567 * - Set SLPM=0 for the IOs you want to switch and others to 1
1568 * - Configure the GPIO registers for the IOs that are being switched
1569 * - Set IOFORCE=1
1570 * - Modify the AFLSA/B registers for the IOs that are being switched
1571 * - Set IOFORCE=0
1572 * - Restore SLPM registers
1573 * - Any spurious wake up event during switch sequence to be ignored
1574 * and cleared
1575 *
1576 * We REALLY need to save ALL slpm registers, because the external
1577 * IOFORCE will switch *all* ports to their sleepmode setting to as
1578 * to avoid glitches. (Not just one port!)
1579 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001580 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001581
1582 if (glitch) {
1583 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1584
1585 /* Initially don't put any pins to sleep when switching */
1586 memset(slpm, 0xff, sizeof(slpm));
1587
1588 /*
1589 * Then mask the pins that need to be sleeping now when we're
1590 * switching to the ALT C function.
1591 */
1592 for (i = 0; i < g->npins; i++)
1593 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1594 nmk_gpio_glitch_slpm_init(slpm);
1595 }
1596
1597 for (i = 0; i < g->npins; i++) {
1598 struct pinctrl_gpio_range *range;
1599 struct nmk_gpio_chip *nmk_chip;
1600 struct gpio_chip *chip;
1601 unsigned bit;
1602
1603 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1604 if (!range) {
1605 dev_err(npct->dev,
1606 "invalid pin offset %d in group %s at index %d\n",
1607 g->pins[i], g->name, i);
1608 goto out_glitch;
1609 }
1610 if (!range->gc) {
1611 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1612 g->pins[i], g->name, i);
1613 goto out_glitch;
1614 }
1615 chip = range->gc;
1616 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1617 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1618
1619 clk_enable(nmk_chip->clk);
1620 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1621 /*
1622 * If the pin is switching to altfunc, and there was an
1623 * interrupt installed on it which has been lazy disabled,
1624 * actually mask the interrupt to prevent spurious interrupts
1625 * that would occur while the pin is under control of the
1626 * peripheral. Only SKE does this.
1627 */
1628 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1629
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001630 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1631 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001632 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001633
1634 /*
1635 * Call PRCM GPIOCR config function in case ALTC
1636 * has been selected:
1637 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1638 * must be set.
1639 * - If selection is pure ALTC and previous selection was ALTCx,
1640 * then some bits in PRCM GPIOCR registers must be cleared.
1641 */
1642 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1643 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1644 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001645 }
1646
1647 /* When all pins are successfully reconfigured we get here */
1648 ret = 0;
1649
1650out_glitch:
1651 if (glitch) {
1652 nmk_gpio_glitch_slpm_restore(slpm);
1653 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1654 }
1655
1656 return ret;
1657}
1658
1659static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1660 unsigned function, unsigned group)
1661{
1662 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1663 const struct nmk_pingroup *g;
1664
1665 g = &npct->soc->groups[group];
1666
1667 if (g->altsetting < 0)
1668 return;
1669
1670 /* Poke out the mux, set the pin to some default state? */
1671 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1672}
1673
1674int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1675 struct pinctrl_gpio_range *range,
1676 unsigned offset)
1677{
1678 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1679 struct nmk_gpio_chip *nmk_chip;
1680 struct gpio_chip *chip;
1681 unsigned bit;
1682
1683 if (!range) {
1684 dev_err(npct->dev, "invalid range\n");
1685 return -EINVAL;
1686 }
1687 if (!range->gc) {
1688 dev_err(npct->dev, "missing GPIO chip in range\n");
1689 return -EINVAL;
1690 }
1691 chip = range->gc;
1692 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1693
1694 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1695
1696 clk_enable(nmk_chip->clk);
1697 bit = offset % NMK_GPIO_PER_CHIP;
1698 /* There is no glitch when converting any pin to GPIO */
1699 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1700 clk_disable(nmk_chip->clk);
1701
1702 return 0;
1703}
1704
1705void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1706 struct pinctrl_gpio_range *range,
1707 unsigned offset)
1708{
1709 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1710
1711 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1712 /* Set the pin to some default state, GPIO is usually default */
1713}
1714
1715static struct pinmux_ops nmk_pinmux_ops = {
1716 .get_functions_count = nmk_pmx_get_funcs_cnt,
1717 .get_function_name = nmk_pmx_get_func_name,
1718 .get_function_groups = nmk_pmx_get_func_groups,
1719 .enable = nmk_pmx_enable,
1720 .disable = nmk_pmx_disable,
1721 .gpio_request_enable = nmk_gpio_request_enable,
1722 .gpio_disable_free = nmk_gpio_disable_free,
1723};
1724
Linus Walleijd41af622012-05-03 15:58:12 +02001725int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1726 unsigned pin,
1727 unsigned long *config)
1728{
1729 /* Not implemented */
1730 return -EINVAL;
1731}
1732
1733int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1734 unsigned pin,
1735 unsigned long config)
1736{
1737 static const char *pullnames[] = {
1738 [NMK_GPIO_PULL_NONE] = "none",
1739 [NMK_GPIO_PULL_UP] = "up",
1740 [NMK_GPIO_PULL_DOWN] = "down",
1741 [3] /* illegal */ = "??"
1742 };
1743 static const char *slpmnames[] = {
1744 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1745 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1746 };
1747 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1748 struct nmk_gpio_chip *nmk_chip;
1749 struct pinctrl_gpio_range *range;
1750 struct gpio_chip *chip;
1751 unsigned bit;
1752
1753 /*
1754 * The pin config contains pin number and altfunction fields, here
1755 * we just ignore that part. It's being handled by the framework and
1756 * pinmux callback respectively.
1757 */
1758 pin_cfg_t cfg = (pin_cfg_t) config;
1759 int pull = PIN_PULL(cfg);
1760 int slpm = PIN_SLPM(cfg);
1761 int output = PIN_DIR(cfg);
1762 int val = PIN_VAL(cfg);
1763 bool lowemi = PIN_LOWEMI(cfg);
1764 bool gpiomode = PIN_GPIOMODE(cfg);
1765 bool sleep = PIN_SLEEPMODE(cfg);
1766
1767 range = nmk_match_gpio_range(pctldev, pin);
1768 if (!range) {
1769 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1770 return -EINVAL;
1771 }
1772 if (!range->gc) {
1773 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1774 pin);
1775 return -EINVAL;
1776 }
1777 chip = range->gc;
1778 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1779
1780 if (sleep) {
1781 int slpm_pull = PIN_SLPM_PULL(cfg);
1782 int slpm_output = PIN_SLPM_DIR(cfg);
1783 int slpm_val = PIN_SLPM_VAL(cfg);
1784
1785 /* All pins go into GPIO mode at sleep */
1786 gpiomode = true;
1787
1788 /*
1789 * The SLPM_* values are normal values + 1 to allow zero to
1790 * mean "same as normal".
1791 */
1792 if (slpm_pull)
1793 pull = slpm_pull - 1;
1794 if (slpm_output)
1795 output = slpm_output - 1;
1796 if (slpm_val)
1797 val = slpm_val - 1;
1798
1799 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1800 pin,
1801 slpm_pull ? pullnames[pull] : "same",
1802 slpm_output ? (output ? "output" : "input") : "same",
1803 slpm_val ? (val ? "high" : "low") : "same");
1804 }
1805
1806 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1807 pin, cfg, pullnames[pull], slpmnames[slpm],
1808 output ? "output " : "input",
1809 output ? (val ? "high" : "low") : "",
1810 lowemi ? "on" : "off" );
1811
1812 clk_enable(nmk_chip->clk);
1813 bit = pin % NMK_GPIO_PER_CHIP;
1814 if (gpiomode)
1815 /* No glitch when going to GPIO mode */
1816 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1817 if (output)
1818 __nmk_gpio_make_output(nmk_chip, bit, val);
1819 else {
1820 __nmk_gpio_make_input(nmk_chip, bit);
1821 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1822 }
1823 /* TODO: isn't this only applicable on output pins? */
1824 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1825
1826 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1827 clk_disable(nmk_chip->clk);
1828 return 0;
1829}
1830
1831static struct pinconf_ops nmk_pinconf_ops = {
1832 .pin_config_get = nmk_pin_config_get,
1833 .pin_config_set = nmk_pin_config_set,
1834};
1835
Linus Walleije98ea772012-04-26 23:57:25 +02001836static struct pinctrl_desc nmk_pinctrl_desc = {
1837 .name = "pinctrl-nomadik",
1838 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001839 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001840 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001841 .owner = THIS_MODULE,
1842};
1843
Lee Jones855f80c2012-05-26 06:09:29 +01001844static const struct of_device_id nmk_pinctrl_match[] = {
1845 {
1846 .compatible = "stericsson,nmk_pinctrl",
1847 .data = (void *)PINCTRL_NMK_DB8500,
1848 },
1849 {},
1850};
1851
Linus Walleije98ea772012-04-26 23:57:25 +02001852static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1853{
1854 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001855 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001856 struct nmk_pinctrl *npct;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001857 struct resource *res;
Lee Jones855f80c2012-05-26 06:09:29 +01001858 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001859 int i;
1860
1861 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1862 if (!npct)
1863 return -ENOMEM;
1864
Lee Jones855f80c2012-05-26 06:09:29 +01001865 if (platid)
1866 version = platid->driver_data;
Axel Lin953e9e92012-11-15 12:56:05 +08001867 else if (np) {
1868 const struct of_device_id *match;
1869
1870 match = of_match_device(nmk_pinctrl_match, &pdev->dev);
1871 if (!match)
1872 return -ENODEV;
1873 version = (unsigned int) match->data;
1874 }
Lee Jones855f80c2012-05-26 06:09:29 +01001875
Linus Walleije98ea772012-04-26 23:57:25 +02001876 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001877 if (version == PINCTRL_NMK_STN8815)
1878 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001879 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001880 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001881 if (version == PINCTRL_NMK_DB8540)
1882 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001883
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001884 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1885 if (res) {
1886 npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
1887 resource_size(res));
1888 if (!npct->prcm_base) {
1889 dev_err(&pdev->dev,
1890 "failed to ioremap PRCM registers\n");
1891 return -ENOMEM;
1892 }
1893 } else {
1894 dev_info(&pdev->dev,
1895 "No PRCM base, assume no ALT-Cx control is available\n");
1896 }
1897
Linus Walleije98ea772012-04-26 23:57:25 +02001898 /*
1899 * We need all the GPIO drivers to probe FIRST, or we will not be able
1900 * to obtain references to the struct gpio_chip * for them, and we
1901 * need this to proceed.
1902 */
1903 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001904 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001905 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001906 return -EPROBE_DEFER;
1907 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001908 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001909 }
1910
1911 nmk_pinctrl_desc.pins = npct->soc->pins;
1912 nmk_pinctrl_desc.npins = npct->soc->npins;
1913 npct->dev = &pdev->dev;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001914
Linus Walleije98ea772012-04-26 23:57:25 +02001915 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1916 if (!npct->pctl) {
1917 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1918 return -EINVAL;
1919 }
1920
1921 /* We will handle a range of GPIO pins */
1922 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1923 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1924
1925 platform_set_drvdata(pdev, npct);
1926 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1927
1928 return 0;
1929}
1930
Lee Jones513c27f2012-04-13 15:05:05 +01001931static const struct of_device_id nmk_gpio_match[] = {
1932 { .compatible = "st,nomadik-gpio", },
1933 {}
1934};
1935
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001936static struct platform_driver nmk_gpio_driver = {
1937 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001938 .owner = THIS_MODULE,
1939 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001940 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301941 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001942 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001943};
1944
Linus Walleije98ea772012-04-26 23:57:25 +02001945static const struct platform_device_id nmk_pinctrl_id[] = {
1946 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1947 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001948 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Axel Lin8c995d62012-11-04 23:30:42 +08001949 { }
Linus Walleije98ea772012-04-26 23:57:25 +02001950};
1951
1952static struct platform_driver nmk_pinctrl_driver = {
1953 .driver = {
1954 .owner = THIS_MODULE,
1955 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001956 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001957 },
1958 .probe = nmk_pinctrl_probe,
1959 .id_table = nmk_pinctrl_id,
1960};
1961
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001962static int __init nmk_gpio_init(void)
1963{
Linus Walleije98ea772012-04-26 23:57:25 +02001964 int ret;
1965
1966 ret = platform_driver_register(&nmk_gpio_driver);
1967 if (ret)
1968 return ret;
1969 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001970}
1971
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001972core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001973
1974MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1975MODULE_DESCRIPTION("Nomadik GPIO Driver");
1976MODULE_LICENSE("GPL");