blob: 5d8a0066ab60a9a56310072a2313280746fcd781 [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>
Will Deaconadfed152011-02-28 10:12:29 +000034#include <asm/mach/irq.h>
Linus Walleijc3b9d1d2012-10-18 11:08:05 +020035#include <mach/irqs.h>
Linus Walleije98ea772012-04-26 23:57:25 +020036#include "pinctrl-nomadik.h"
Julien Delacou8d99b322012-12-11 09:17:47 +010037#include "core.h"
Linus Walleije98ea772012-04-26 23:57:25 +020038
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
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100263 if (!npct->prcm_base)
264 return;
265
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200266 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
267 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
268 alt_num);
269 return;
270 }
271
272 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
273 if (npct->soc->altcx_pins[i].pin == offset)
274 break;
275 }
276 if (i == npct->soc->npins_altcx) {
277 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
278 offset);
279 return;
280 }
281
282 pin_desc = npct->soc->altcx_pins + i;
283 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
284
285 /*
286 * If alt_num is NULL, just clear current ALTCx selection
287 * to make sure we come back to a pure ALTC selection
288 */
289 if (!alt_num) {
290 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
291 if (pin_desc->altcx[i].used == true) {
292 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
293 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200294 if (readl(npct->prcm_base + reg) & BIT(bit)) {
295 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200296 dev_dbg(npct->dev,
297 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
298 offset, i+1);
299 }
300 }
301 }
302 return;
303 }
304
305 alt_index = alt_num - 1;
306 if (pin_desc->altcx[alt_index].used == false) {
307 dev_warn(npct->dev,
308 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
309 offset, alt_num);
310 return;
311 }
312
313 /*
314 * Check if any other ALTCx functions are activated on this pin
315 * and disable it first.
316 */
317 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
318 if (i == alt_index)
319 continue;
320 if (pin_desc->altcx[i].used == true) {
321 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
322 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200323 if (readl(npct->prcm_base + reg) & BIT(bit)) {
324 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200325 dev_dbg(npct->dev,
326 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
327 offset, i+1);
328 }
329 }
330 }
331
332 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
333 bit = pin_desc->altcx[alt_index].control_bit;
334 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
335 offset, alt_index+1);
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200336 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200337}
338
Rabin Vincent378be062010-06-02 06:06:29 +0100339static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530340 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100341{
342 static const char *afnames[] = {
343 [NMK_GPIO_ALT_GPIO] = "GPIO",
344 [NMK_GPIO_ALT_A] = "A",
345 [NMK_GPIO_ALT_B] = "B",
346 [NMK_GPIO_ALT_C] = "C"
347 };
348 static const char *pullnames[] = {
349 [NMK_GPIO_PULL_NONE] = "none",
350 [NMK_GPIO_PULL_UP] = "up",
351 [NMK_GPIO_PULL_DOWN] = "down",
352 [3] /* illegal */ = "??"
353 };
354 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100355 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
356 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100357 };
358
359 int pin = PIN_NUM(cfg);
360 int pull = PIN_PULL(cfg);
361 int af = PIN_ALT(cfg);
362 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100363 int output = PIN_DIR(cfg);
364 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530365 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100366
Rabin Vincentdacdc962010-12-03 20:35:37 +0530367 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
368 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100369 output ? "output " : "input",
370 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100371
Rabin Vincentdacdc962010-12-03 20:35:37 +0530372 if (sleep) {
373 int slpm_pull = PIN_SLPM_PULL(cfg);
374 int slpm_output = PIN_SLPM_DIR(cfg);
375 int slpm_val = PIN_SLPM_VAL(cfg);
376
Rabin Vincent3546d152010-11-25 11:38:27 +0530377 af = NMK_GPIO_ALT_GPIO;
378
Rabin Vincentdacdc962010-12-03 20:35:37 +0530379 /*
380 * The SLPM_* values are normal values + 1 to allow zero to
381 * mean "same as normal".
382 */
383 if (slpm_pull)
384 pull = slpm_pull - 1;
385 if (slpm_output)
386 output = slpm_output - 1;
387 if (slpm_val)
388 val = slpm_val - 1;
389
390 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
391 pin,
392 slpm_pull ? pullnames[pull] : "same",
393 slpm_output ? (output ? "output" : "input") : "same",
394 slpm_val ? (val ? "high" : "low") : "same");
395 }
396
Rabin Vincent6720db72010-09-02 11:28:48 +0100397 if (output)
398 __nmk_gpio_make_output(nmk_chip, offset, val);
399 else {
400 __nmk_gpio_make_input(nmk_chip, offset);
401 __nmk_gpio_set_pull(nmk_chip, offset, pull);
402 }
403
Rabin Vincentebc61782011-09-28 15:49:11 +0530404 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
405
Rabin Vincent01727e62010-12-13 12:02:40 +0530406 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530407 * If the pin is switching to altfunc, and there was an interrupt
408 * installed on it which has been lazy disabled, actually mask the
409 * interrupt to prevent spurious interrupts that would occur while the
410 * pin is under control of the peripheral. Only SKE does this.
411 */
412 if (af != NMK_GPIO_ALT_GPIO)
413 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
414
415 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530416 * If we've backed up the SLPM registers (glitch workaround), modify
417 * the backups since they will be restored.
418 */
419 if (slpmregs) {
420 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
421 slpmregs[nmk_chip->bank] |= BIT(offset);
422 else
423 slpmregs[nmk_chip->bank] &= ~BIT(offset);
424 } else
425 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
426
427 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
428}
429
430/*
431 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
432 * - Save SLPM registers
433 * - Set SLPM=0 for the IOs you want to switch and others to 1
434 * - Configure the GPIO registers for the IOs that are being switched
435 * - Set IOFORCE=1
436 * - Modify the AFLSA/B registers for the IOs that are being switched
437 * - Set IOFORCE=0
438 * - Restore SLPM registers
439 * - Any spurious wake up event during switch sequence to be ignored and
440 * cleared
441 */
442static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
443{
444 int i;
445
446 for (i = 0; i < NUM_BANKS; i++) {
447 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
448 unsigned int temp = slpm[i];
449
450 if (!chip)
451 break;
452
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200453 clk_enable(chip->clk);
454
Rabin Vincent01727e62010-12-13 12:02:40 +0530455 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
456 writel(temp, chip->addr + NMK_GPIO_SLPC);
457 }
458}
459
460static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
461{
462 int i;
463
464 for (i = 0; i < NUM_BANKS; i++) {
465 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
466
467 if (!chip)
468 break;
469
470 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200471
472 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530473 }
474}
475
476static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
477{
478 static unsigned int slpm[NUM_BANKS];
479 unsigned long flags;
480 bool glitch = false;
481 int ret = 0;
482 int i;
483
484 for (i = 0; i < num; i++) {
485 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
486 glitch = true;
487 break;
488 }
489 }
490
491 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
492
493 if (glitch) {
494 memset(slpm, 0xff, sizeof(slpm));
495
496 for (i = 0; i < num; i++) {
497 int pin = PIN_NUM(cfgs[i]);
498 int offset = pin % NMK_GPIO_PER_CHIP;
499
500 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
501 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
502 }
503
504 nmk_gpio_glitch_slpm_init(slpm);
505 }
506
507 for (i = 0; i < num; i++) {
508 struct nmk_gpio_chip *nmk_chip;
509 int pin = PIN_NUM(cfgs[i]);
510
Lee Jonesa60b57e2012-04-19 21:36:31 +0100511 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530512 if (!nmk_chip) {
513 ret = -EINVAL;
514 break;
515 }
516
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200517 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530518 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100519 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530520 cfgs[i], sleep, glitch ? slpm : NULL);
521 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200522 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530523 }
524
525 if (glitch)
526 nmk_gpio_glitch_slpm_restore(slpm);
527
528 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
529
530 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100531}
532
533/**
534 * nmk_config_pin - configure a pin's mux attributes
535 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200536 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100537 * Configures a pin's mode (alternate function or GPIO), its pull up status,
538 * and its sleep mode based on the specified configuration. The @cfg is
539 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
540 * are constructed using, and can be further enhanced with, the macros in
Linus Walleij287f1212012-10-10 14:35:17 +0200541 * <linux/platform_data/pinctrl-nomadik.h>
Rabin Vincent378be062010-06-02 06:06:29 +0100542 *
543 * If a pin's mode is set to GPIO, it is configured as an input to avoid
544 * side-effects. The gpio can be manipulated later using standard GPIO API
545 * calls.
546 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530547int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100548{
Rabin Vincent01727e62010-12-13 12:02:40 +0530549 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100550}
551EXPORT_SYMBOL(nmk_config_pin);
552
553/**
554 * nmk_config_pins - configure several pins at once
555 * @cfgs: array of pin configurations
556 * @num: number of elments in the array
557 *
558 * Configures several pins using nmk_config_pin(). Refer to that function for
559 * further information.
560 */
561int nmk_config_pins(pin_cfg_t *cfgs, int num)
562{
Rabin Vincent01727e62010-12-13 12:02:40 +0530563 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100564}
565EXPORT_SYMBOL(nmk_config_pins);
566
Rabin Vincentdacdc962010-12-03 20:35:37 +0530567int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
568{
Rabin Vincent01727e62010-12-13 12:02:40 +0530569 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530570}
571EXPORT_SYMBOL(nmk_config_pins_sleep);
572
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100573/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100574 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
575 * @gpio: pin number
576 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
577 *
Linus Walleij33d78642011-06-09 11:08:47 +0200578 * This register is actually in the pinmux layer, not the GPIO block itself.
579 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
580 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
581 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
582 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
583 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
584 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100585 *
Linus Walleij33d78642011-06-09 11:08:47 +0200586 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
587 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
588 * entered) regardless of the altfunction selected. Also wake-up detection is
589 * ENABLED.
590 *
591 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
592 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
593 * (for altfunction GPIO) or respective on-chip peripherals (for other
594 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
595 *
596 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100597 */
598int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
599{
600 struct nmk_gpio_chip *nmk_chip;
601 unsigned long flags;
602
Lee Jonesa60b57e2012-04-19 21:36:31 +0100603 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100604 if (!nmk_chip)
605 return -EINVAL;
606
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200607 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530608 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
609 spin_lock(&nmk_chip->lock);
610
Lee Jonesa60b57e2012-04-19 21:36:31 +0100611 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530612
613 spin_unlock(&nmk_chip->lock);
614 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200615 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100616
617 return 0;
618}
619
620/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100621 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
622 * @gpio: pin number
623 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
624 *
625 * Enables/disables pull up/down on a specified pin. This only takes effect if
626 * the pin is configured as an input (either explicitly or by the alternate
627 * function).
628 *
629 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
630 * configured as an input. Otherwise, due to the way the controller registers
631 * work, this function will change the value output on the pin.
632 */
633int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
634{
635 struct nmk_gpio_chip *nmk_chip;
636 unsigned long flags;
637
Lee Jonesa60b57e2012-04-19 21:36:31 +0100638 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100639 if (!nmk_chip)
640 return -EINVAL;
641
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200642 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100643 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100644 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100645 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200646 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100647
648 return 0;
649}
650
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100651/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200652/**
653 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
654 * @gpio: pin number
655 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
656 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
657 *
658 * Sets the mode of the specified pin to one of the alternate functions or
659 * plain GPIO.
660 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100661int nmk_gpio_set_mode(int gpio, int gpio_mode)
662{
663 struct nmk_gpio_chip *nmk_chip;
664 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100665
Lee Jonesa60b57e2012-04-19 21:36:31 +0100666 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667 if (!nmk_chip)
668 return -EINVAL;
669
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200670 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100671 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100672 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100673 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200674 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100675
676 return 0;
677}
678EXPORT_SYMBOL(nmk_gpio_set_mode);
679
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200680static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
681{
682 int i;
683 u16 reg;
684 u8 bit;
685 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
686 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
687 const u16 *gpiocr_regs;
688
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100689 if (!npct->prcm_base)
690 return NMK_GPIO_ALT_C;
691
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200692 for (i = 0; i < npct->soc->npins_altcx; i++) {
693 if (npct->soc->altcx_pins[i].pin == gpio)
694 break;
695 }
696 if (i == npct->soc->npins_altcx)
697 return NMK_GPIO_ALT_C;
698
699 pin_desc = npct->soc->altcx_pins + i;
700 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
701 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
702 if (pin_desc->altcx[i].used == true) {
703 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
704 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200705 if (readl(npct->prcm_base + reg) & BIT(bit))
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200706 return NMK_GPIO_ALT_C+i+1;
707 }
708 }
709 return NMK_GPIO_ALT_C;
710}
711
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100712int nmk_gpio_get_mode(int gpio)
713{
714 struct nmk_gpio_chip *nmk_chip;
715 u32 afunc, bfunc, bit;
716
Lee Jonesa60b57e2012-04-19 21:36:31 +0100717 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100718 if (!nmk_chip)
719 return -EINVAL;
720
Lee Jonesa60b57e2012-04-19 21:36:31 +0100721 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100722
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200723 clk_enable(nmk_chip->clk);
724
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100725 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
726 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
727
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200728 clk_disable(nmk_chip->clk);
729
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100730 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
731}
732EXPORT_SYMBOL(nmk_gpio_get_mode);
733
734
735/* IRQ functions */
736static inline int nmk_gpio_get_bitmask(int gpio)
737{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100738 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100739}
740
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100741static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100742{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743 struct nmk_gpio_chip *nmk_chip;
744
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100745 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100746 if (!nmk_chip)
747 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200748
749 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100750 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200751 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100752}
753
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100754enum nmk_gpio_irq_type {
755 NORMAL,
756 WAKE,
757};
758
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100759static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100760 int gpio, enum nmk_gpio_irq_type which,
761 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100762{
763 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530764 u32 *rimscval;
765 u32 *fimscval;
766 u32 rimscreg;
767 u32 fimscreg;
768
769 if (which == NORMAL) {
770 rimscreg = NMK_GPIO_RIMSC;
771 fimscreg = NMK_GPIO_FIMSC;
772 rimscval = &nmk_chip->rimsc;
773 fimscval = &nmk_chip->fimsc;
774 } else {
775 rimscreg = NMK_GPIO_RWIMSC;
776 fimscreg = NMK_GPIO_FWIMSC;
777 rimscval = &nmk_chip->rwimsc;
778 fimscval = &nmk_chip->fwimsc;
779 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100780
781 /* we must individually set/clear the two edges */
782 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100783 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530784 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100785 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530786 *rimscval &= ~bitmask;
787 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100788 }
789 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100790 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530791 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100792 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530793 *fimscval &= ~bitmask;
794 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100795 }
796}
797
Rabin Vincentb9df4682011-02-10 11:45:58 +0530798static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
799 int gpio, bool on)
800{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530801 /*
802 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
803 * disabled, since setting SLPM to 1 increases power consumption, and
804 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
805 */
806 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200807 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530808 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200809 }
810
Rabin Vincentb9df4682011-02-10 11:45:58 +0530811 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
812}
813
814static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100815{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100816 struct nmk_gpio_chip *nmk_chip;
817 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100818 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100819
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100820 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100821 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100822 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100823 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100824
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200825 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530826 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
827 spin_lock(&nmk_chip->lock);
828
Lee Jonesa60b57e2012-04-19 21:36:31 +0100829 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530830
831 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100832 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530833
834 spin_unlock(&nmk_chip->lock);
835 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200836 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100837
838 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100839}
840
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100841static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100842{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530843 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100844}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100845
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100846static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100847{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530848 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100849}
850
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100851static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100852{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100853 struct nmk_gpio_chip *nmk_chip;
854 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530855 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100856
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100857 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100858 if (!nmk_chip)
859 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100860 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100861
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200862 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530863 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
864 spin_lock(&nmk_chip->lock);
865
Linus Walleij479a0c72011-09-20 10:50:15 +0200866 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100867 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530868
869 if (on)
870 nmk_chip->real_wake |= bitmask;
871 else
872 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530873
874 spin_unlock(&nmk_chip->lock);
875 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200876 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100877
878 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100879}
880
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100881static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100882{
Linus Walleij479a0c72011-09-20 10:50:15 +0200883 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200884 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100885 struct nmk_gpio_chip *nmk_chip;
886 unsigned long flags;
887 u32 bitmask;
888
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100889 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100890 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100891 if (!nmk_chip)
892 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100893 if (type & IRQ_TYPE_LEVEL_HIGH)
894 return -EINVAL;
895 if (type & IRQ_TYPE_LEVEL_LOW)
896 return -EINVAL;
897
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200898 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100899 spin_lock_irqsave(&nmk_chip->lock, flags);
900
Rabin Vincent7a852d82010-05-06 10:43:55 +0100901 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100902 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100903
Rabin Vincentb9df4682011-02-10 11:45:58 +0530904 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100905 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100906
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100907 nmk_chip->edge_rising &= ~bitmask;
908 if (type & IRQ_TYPE_EDGE_RISING)
909 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100910
911 nmk_chip->edge_falling &= ~bitmask;
912 if (type & IRQ_TYPE_EDGE_FALLING)
913 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100914
915 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100916 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100917
Rabin Vincentb9df4682011-02-10 11:45:58 +0530918 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100919 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100920
921 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200922 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100923
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100924 return 0;
925}
926
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200927static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
928{
929 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
930
931 clk_enable(nmk_chip->clk);
932 nmk_gpio_irq_unmask(d);
933 return 0;
934}
935
936static void nmk_gpio_irq_shutdown(struct irq_data *d)
937{
938 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
939
940 nmk_gpio_irq_mask(d);
941 clk_disable(nmk_chip->clk);
942}
943
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100944static struct irq_chip nmk_gpio_irq_chip = {
945 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100946 .irq_ack = nmk_gpio_irq_ack,
947 .irq_mask = nmk_gpio_irq_mask,
948 .irq_unmask = nmk_gpio_irq_unmask,
949 .irq_set_type = nmk_gpio_irq_set_type,
950 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200951 .irq_startup = nmk_gpio_irq_startup,
952 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200953 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100954};
955
Rabin Vincent33b744b2010-10-14 10:38:03 +0530956static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
957 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100958{
959 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100960 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100961
Will Deaconadfed152011-02-28 10:12:29 +0000962 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100963
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100964 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530965 while (status) {
966 int bit = __ffs(status);
967
Linus Walleij95f0bc92012-09-27 14:14:09 +0200968 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530969 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100970 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100971
Will Deaconadfed152011-02-28 10:12:29 +0000972 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100973}
974
Rabin Vincent33b744b2010-10-14 10:38:03 +0530975static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
976{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100977 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200978 u32 status;
979
980 clk_enable(nmk_chip->clk);
981 status = readl(nmk_chip->addr + NMK_GPIO_IS);
982 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530983
984 __nmk_gpio_irq_handler(irq, desc, status);
985}
986
987static void nmk_gpio_secondary_irq_handler(unsigned int irq,
988 struct irq_desc *desc)
989{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100990 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530991 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
992
993 __nmk_gpio_irq_handler(irq, desc, status);
994}
995
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100996static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
997{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100998 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
999 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +05301000
1001 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001002 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +05301003 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001004 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +05301005 }
1006
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001007 return 0;
1008}
1009
1010/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001011
1012static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
1013{
1014 /*
1015 * Map back to global GPIO space and request muxing, the direction
1016 * parameter does not matter for this controller.
1017 */
1018 int gpio = chip->base + offset;
1019
1020 return pinctrl_request_gpio(gpio);
1021}
1022
1023static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
1024{
1025 int gpio = chip->base + offset;
1026
1027 pinctrl_free_gpio(gpio);
1028}
1029
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001030static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
1031{
1032 struct nmk_gpio_chip *nmk_chip =
1033 container_of(chip, struct nmk_gpio_chip, chip);
1034
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001035 clk_enable(nmk_chip->clk);
1036
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001037 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001038
1039 clk_disable(nmk_chip->clk);
1040
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001041 return 0;
1042}
1043
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001044static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1045{
1046 struct nmk_gpio_chip *nmk_chip =
1047 container_of(chip, struct nmk_gpio_chip, chip);
1048 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001049 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001050
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001051 clk_enable(nmk_chip->clk);
1052
1053 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1054
1055 clk_disable(nmk_chip->clk);
1056
1057 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001058}
1059
1060static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1061 int val)
1062{
1063 struct nmk_gpio_chip *nmk_chip =
1064 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001065
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001066 clk_enable(nmk_chip->clk);
1067
Rabin Vincent6720db72010-09-02 11:28:48 +01001068 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001069
1070 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001071}
1072
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001073static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1074 int val)
1075{
1076 struct nmk_gpio_chip *nmk_chip =
1077 container_of(chip, struct nmk_gpio_chip, chip);
1078
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001079 clk_enable(nmk_chip->clk);
1080
Rabin Vincent6720db72010-09-02 11:28:48 +01001081 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001082
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001083 clk_disable(nmk_chip->clk);
1084
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001085 return 0;
1086}
1087
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001088static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1089{
1090 struct nmk_gpio_chip *nmk_chip =
1091 container_of(chip, struct nmk_gpio_chip, chip);
1092
Linus Walleij268300b2012-10-19 17:06:54 +02001093 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001094}
1095
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301096#ifdef CONFIG_DEBUG_FS
1097
1098#include <linux/seq_file.h>
1099
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001100static void nmk_gpio_dbg_show_one(struct seq_file *s,
1101 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
1102 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301103{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001104 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301105 struct nmk_gpio_chip *nmk_chip =
1106 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001107 int mode;
1108 bool is_out;
1109 bool pull;
1110 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301111 const char *modes[] = {
1112 [NMK_GPIO_ALT_GPIO] = "gpio",
1113 [NMK_GPIO_ALT_A] = "altA",
1114 [NMK_GPIO_ALT_B] = "altB",
1115 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001116 [NMK_GPIO_ALT_C+1] = "altC1",
1117 [NMK_GPIO_ALT_C+2] = "altC2",
1118 [NMK_GPIO_ALT_C+3] = "altC3",
1119 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301120 };
1121
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001122 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001123 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1124 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1125 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001126 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1127 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001128
Linus Walleij6f4350a2012-05-02 21:06:13 +02001129 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1130 gpio, label ?: "(none)",
1131 is_out ? "out" : "in ",
1132 chip->get
1133 ? (chip->get(chip, offset) ? "hi" : "lo")
1134 : "? ",
1135 (mode < 0) ? "unknown" : modes[mode],
1136 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301137
Linus Walleij6f4350a2012-05-02 21:06:13 +02001138 if (label && !is_out) {
1139 int irq = gpio_to_irq(gpio);
1140 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001141
Linus Walleij6f4350a2012-05-02 21:06:13 +02001142 /* This races with request_irq(), set_irq_type(),
1143 * and set_irq_wake() ... but those are "rare".
1144 */
1145 if (irq >= 0 && desc->action) {
1146 char *trigger;
1147 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001148
Linus Walleij6f4350a2012-05-02 21:06:13 +02001149 if (nmk_chip->edge_rising & bitmask)
1150 trigger = "edge-rising";
1151 else if (nmk_chip->edge_falling & bitmask)
1152 trigger = "edge-falling";
1153 else
1154 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001155
Linus Walleij6f4350a2012-05-02 21:06:13 +02001156 seq_printf(s, " irq-%d %s%s",
1157 irq, trigger,
1158 irqd_is_wakeup_set(&desc->irq_data)
1159 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001160 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301161 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001162 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301163}
1164
Linus Walleij6f4350a2012-05-02 21:06:13 +02001165static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1166{
1167 unsigned i;
1168 unsigned gpio = chip->base;
1169
1170 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001171 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001172 seq_printf(s, "\n");
1173 }
1174}
1175
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301176#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001177static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001178 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +02001179 struct gpio_chip *chip,
1180 unsigned offset, unsigned gpio)
1181{
1182}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301183#define nmk_gpio_dbg_show NULL
1184#endif
1185
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001186/* This structure is replicated for each GPIO block allocated at probe time */
1187static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001188 .request = nmk_gpio_request,
1189 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001190 .direction_input = nmk_gpio_make_input,
1191 .get = nmk_gpio_get_input,
1192 .direction_output = nmk_gpio_make_output,
1193 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001194 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301195 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001196 .can_sleep = 0,
1197};
1198
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001199void nmk_gpio_clocks_enable(void)
1200{
1201 int i;
1202
1203 for (i = 0; i < NUM_BANKS; i++) {
1204 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1205
1206 if (!chip)
1207 continue;
1208
1209 clk_enable(chip->clk);
1210 }
1211}
1212
1213void nmk_gpio_clocks_disable(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 continue;
1222
1223 clk_disable(chip->clk);
1224 }
1225}
1226
Rabin Vincentb9df4682011-02-10 11:45:58 +05301227/*
1228 * Called from the suspend/resume path to only keep the real wakeup interrupts
1229 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1230 * and not the rest of the interrupts which we needed to have as wakeups for
1231 * cpuidle.
1232 *
1233 * PM ops are not used since this needs to be done at the end, after all the
1234 * other drivers are done with their suspend callbacks.
1235 */
1236void nmk_gpio_wakeups_suspend(void)
1237{
1238 int i;
1239
1240 for (i = 0; i < NUM_BANKS; i++) {
1241 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1242
1243 if (!chip)
1244 break;
1245
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001246 clk_enable(chip->clk);
1247
Rabin Vincentb9df4682011-02-10 11:45:58 +05301248 writel(chip->rwimsc & chip->real_wake,
1249 chip->addr + NMK_GPIO_RWIMSC);
1250 writel(chip->fwimsc & chip->real_wake,
1251 chip->addr + NMK_GPIO_FWIMSC);
1252
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001253 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301254 }
1255}
1256
1257void nmk_gpio_wakeups_resume(void)
1258{
1259 int i;
1260
1261 for (i = 0; i < NUM_BANKS; i++) {
1262 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1263
1264 if (!chip)
1265 break;
1266
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001267 clk_enable(chip->clk);
1268
Rabin Vincentb9df4682011-02-10 11:45:58 +05301269 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1270 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1271
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001272 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301273 }
1274}
1275
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001276/*
1277 * Read the pull up/pull down status.
1278 * A bit set in 'pull_up' means that pull up
1279 * is selected if pull is enabled in PDIS register.
1280 * Note: only pull up/down set via this driver can
1281 * be detected due to HW limitations.
1282 */
1283void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1284{
1285 if (gpio_bank < NUM_BANKS) {
1286 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1287
1288 if (!chip)
1289 return;
1290
1291 *pull_up = chip->pull_up;
1292 }
1293}
1294
Axel Lin5212d092012-11-16 00:01:35 +08001295static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1296 irq_hw_number_t hwirq)
Lee Jonesa60b57e2012-04-19 21:36:31 +01001297{
1298 struct nmk_gpio_chip *nmk_chip = d->host_data;
1299
1300 if (!nmk_chip)
1301 return -EINVAL;
1302
1303 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1304 set_irq_flags(irq, IRQF_VALID);
1305 irq_set_chip_data(irq, nmk_chip);
1306 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1307
1308 return 0;
1309}
1310
1311const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1312 .map = nmk_gpio_irq_map,
1313 .xlate = irq_domain_xlate_twocell,
1314};
1315
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001316static int nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001317{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001318 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001319 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001320 struct nmk_gpio_chip *nmk_chip;
1321 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001322 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001323 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301324 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001325 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001326 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001327 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001328 int ret;
1329
Lee Jones513c27f2012-04-13 15:05:05 +01001330 if (!pdata && !np) {
1331 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001332 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001333 }
1334
1335 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001336 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001337 if (!pdata)
1338 return -ENOMEM;
1339
Lee Jones612e1d52012-06-14 11:27:56 +01001340 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001341 pdata->supports_sleepmode = true;
1342
1343 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1344 dev_err(&dev->dev, "gpio-bank property not found\n");
1345 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001346 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001347 }
1348
1349 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1350 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1351 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001352
1353 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1354 if (!res) {
1355 ret = -ENOENT;
1356 goto out;
1357 }
1358
1359 irq = platform_get_irq(dev, 0);
1360 if (irq < 0) {
1361 ret = irq;
1362 goto out;
1363 }
1364
Rabin Vincent33b744b2010-10-14 10:38:03 +05301365 secondary_irq = platform_get_irq(dev, 1);
1366 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1367 ret = -EINVAL;
1368 goto out;
1369 }
1370
Linus Walleij5e754f32012-07-03 23:05:14 +02001371 base = devm_request_and_ioremap(&dev->dev, res);
1372 if (!base) {
1373 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001374 goto out;
1375 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001376
Linus Walleij5e754f32012-07-03 23:05:14 +02001377 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001378 if (IS_ERR(clk)) {
1379 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001380 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001381 }
Linus Walleijefec3812012-06-06 22:50:41 +02001382 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001383
Linus Walleij5e754f32012-07-03 23:05:14 +02001384 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001385 if (!nmk_chip) {
1386 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001387 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001388 }
Lee Jones513c27f2012-04-13 15:05:05 +01001389
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001390 /*
1391 * The virt address in nmk_chip->addr is in the nomadik register space,
1392 * so we can simply convert the resource address, without remapping
1393 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301394 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001395 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001396 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001397 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001398 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301399 nmk_chip->secondary_parent_irq = secondary_irq;
1400 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301401 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001402 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001403 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001404
1405 chip = &nmk_chip->chip;
1406 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301407 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301408 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001409 chip->dev = &dev->dev;
1410 chip->owner = THIS_MODULE;
1411
Rabin Vincentebc61782011-09-28 15:49:11 +05301412 clk_enable(nmk_chip->clk);
1413 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1414 clk_disable(nmk_chip->clk);
1415
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001416#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001417 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001418#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001419
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001420 ret = gpiochip_add(&nmk_chip->chip);
1421 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001422 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001423
Rabin Vincent01727e62010-12-13 12:02:40 +05301424 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1425
1426 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001427
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001428 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001429
Linus Walleij51f58c62012-10-11 16:33:44 +02001430 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001431 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001432 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001433 NMK_GPIO_PER_CHIP, irq_start,
1434 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001435 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001436 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001437 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001438 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001439 }
1440
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001441 nmk_gpio_init_irq(nmk_chip);
1442
Lee Jones513c27f2012-04-13 15:05:05 +01001443 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1444
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001445 return 0;
1446
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001447out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001448 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1449 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001450
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001451 return ret;
1452}
1453
Linus Walleije98ea772012-04-26 23:57:25 +02001454static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1455{
1456 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1457
1458 return npct->soc->ngroups;
1459}
1460
1461static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1462 unsigned selector)
1463{
1464 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1465
1466 return npct->soc->groups[selector].name;
1467}
1468
1469static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1470 const unsigned **pins,
1471 unsigned *num_pins)
1472{
1473 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1474
1475 *pins = npct->soc->groups[selector].pins;
1476 *num_pins = npct->soc->groups[selector].npins;
1477 return 0;
1478}
1479
Linus Walleij24cbdd72012-05-02 21:28:00 +02001480static struct pinctrl_gpio_range *
1481nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1482{
1483 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1484 int i;
1485
1486 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1487 struct pinctrl_gpio_range *range;
1488
1489 range = &npct->soc->gpio_ranges[i];
1490 if (offset >= range->pin_base &&
1491 offset <= (range->pin_base + range->npins - 1))
1492 return range;
1493 }
1494 return NULL;
1495}
1496
Linus Walleije98ea772012-04-26 23:57:25 +02001497static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1498 unsigned offset)
1499{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001500 struct pinctrl_gpio_range *range;
1501 struct gpio_chip *chip;
1502
1503 range = nmk_match_gpio_range(pctldev, offset);
1504 if (!range || !range->gc) {
1505 seq_printf(s, "invalid pin offset");
1506 return;
1507 }
1508 chip = range->gc;
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001509 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001510}
1511
1512static struct pinctrl_ops nmk_pinctrl_ops = {
1513 .get_groups_count = nmk_get_groups_cnt,
1514 .get_group_name = nmk_get_group_name,
1515 .get_group_pins = nmk_get_group_pins,
1516 .pin_dbg_show = nmk_pin_dbg_show,
1517};
1518
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001519static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1520{
1521 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1522
1523 return npct->soc->nfunctions;
1524}
1525
1526static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1527 unsigned function)
1528{
1529 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1530
1531 return npct->soc->functions[function].name;
1532}
1533
1534static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1535 unsigned function,
1536 const char * const **groups,
1537 unsigned * const num_groups)
1538{
1539 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1540
1541 *groups = npct->soc->functions[function].groups;
1542 *num_groups = npct->soc->functions[function].ngroups;
1543
1544 return 0;
1545}
1546
1547static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1548 unsigned group)
1549{
1550 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1551 const struct nmk_pingroup *g;
1552 static unsigned int slpm[NUM_BANKS];
1553 unsigned long flags;
1554 bool glitch;
1555 int ret = -EINVAL;
1556 int i;
1557
1558 g = &npct->soc->groups[group];
1559
1560 if (g->altsetting < 0)
1561 return -EINVAL;
1562
1563 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1564
Linus Walleijdaf73172012-05-22 11:46:45 +02001565 /*
1566 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1567 * we may pass through an undesired state. In this case we take
1568 * some extra care.
1569 *
1570 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1571 * - Save SLPM registers (since we have a shadow register in the
1572 * nmk_chip we're using that as backup)
1573 * - Set SLPM=0 for the IOs you want to switch and others to 1
1574 * - Configure the GPIO registers for the IOs that are being switched
1575 * - Set IOFORCE=1
1576 * - Modify the AFLSA/B registers for the IOs that are being switched
1577 * - Set IOFORCE=0
1578 * - Restore SLPM registers
1579 * - Any spurious wake up event during switch sequence to be ignored
1580 * and cleared
1581 *
1582 * We REALLY need to save ALL slpm registers, because the external
1583 * IOFORCE will switch *all* ports to their sleepmode setting to as
1584 * to avoid glitches. (Not just one port!)
1585 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001586 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001587
1588 if (glitch) {
1589 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1590
1591 /* Initially don't put any pins to sleep when switching */
1592 memset(slpm, 0xff, sizeof(slpm));
1593
1594 /*
1595 * Then mask the pins that need to be sleeping now when we're
1596 * switching to the ALT C function.
1597 */
1598 for (i = 0; i < g->npins; i++)
1599 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1600 nmk_gpio_glitch_slpm_init(slpm);
1601 }
1602
1603 for (i = 0; i < g->npins; i++) {
1604 struct pinctrl_gpio_range *range;
1605 struct nmk_gpio_chip *nmk_chip;
1606 struct gpio_chip *chip;
1607 unsigned bit;
1608
1609 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1610 if (!range) {
1611 dev_err(npct->dev,
1612 "invalid pin offset %d in group %s at index %d\n",
1613 g->pins[i], g->name, i);
1614 goto out_glitch;
1615 }
1616 if (!range->gc) {
1617 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1618 g->pins[i], g->name, i);
1619 goto out_glitch;
1620 }
1621 chip = range->gc;
1622 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1623 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1624
1625 clk_enable(nmk_chip->clk);
1626 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1627 /*
1628 * If the pin is switching to altfunc, and there was an
1629 * interrupt installed on it which has been lazy disabled,
1630 * actually mask the interrupt to prevent spurious interrupts
1631 * that would occur while the pin is under control of the
1632 * peripheral. Only SKE does this.
1633 */
1634 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1635
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001636 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1637 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001638 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001639
1640 /*
1641 * Call PRCM GPIOCR config function in case ALTC
1642 * has been selected:
1643 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1644 * must be set.
1645 * - If selection is pure ALTC and previous selection was ALTCx,
1646 * then some bits in PRCM GPIOCR registers must be cleared.
1647 */
1648 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1649 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1650 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001651 }
1652
1653 /* When all pins are successfully reconfigured we get here */
1654 ret = 0;
1655
1656out_glitch:
1657 if (glitch) {
1658 nmk_gpio_glitch_slpm_restore(slpm);
1659 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1660 }
1661
1662 return ret;
1663}
1664
1665static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1666 unsigned function, unsigned group)
1667{
1668 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1669 const struct nmk_pingroup *g;
1670
1671 g = &npct->soc->groups[group];
1672
1673 if (g->altsetting < 0)
1674 return;
1675
1676 /* Poke out the mux, set the pin to some default state? */
1677 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1678}
1679
Axel Lin5212d092012-11-16 00:01:35 +08001680static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1681 struct pinctrl_gpio_range *range,
1682 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001683{
1684 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1685 struct nmk_gpio_chip *nmk_chip;
1686 struct gpio_chip *chip;
1687 unsigned bit;
1688
1689 if (!range) {
1690 dev_err(npct->dev, "invalid range\n");
1691 return -EINVAL;
1692 }
1693 if (!range->gc) {
1694 dev_err(npct->dev, "missing GPIO chip in range\n");
1695 return -EINVAL;
1696 }
1697 chip = range->gc;
1698 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1699
1700 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1701
1702 clk_enable(nmk_chip->clk);
1703 bit = offset % NMK_GPIO_PER_CHIP;
1704 /* There is no glitch when converting any pin to GPIO */
1705 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1706 clk_disable(nmk_chip->clk);
1707
1708 return 0;
1709}
1710
Axel Lin5212d092012-11-16 00:01:35 +08001711static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1712 struct pinctrl_gpio_range *range,
1713 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001714{
1715 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1716
1717 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1718 /* Set the pin to some default state, GPIO is usually default */
1719}
1720
1721static struct pinmux_ops nmk_pinmux_ops = {
1722 .get_functions_count = nmk_pmx_get_funcs_cnt,
1723 .get_function_name = nmk_pmx_get_func_name,
1724 .get_function_groups = nmk_pmx_get_func_groups,
1725 .enable = nmk_pmx_enable,
1726 .disable = nmk_pmx_disable,
1727 .gpio_request_enable = nmk_gpio_request_enable,
1728 .gpio_disable_free = nmk_gpio_disable_free,
1729};
1730
Axel Lin5212d092012-11-16 00:01:35 +08001731static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1732 unsigned long *config)
Linus Walleijd41af622012-05-03 15:58:12 +02001733{
1734 /* Not implemented */
1735 return -EINVAL;
1736}
1737
Axel Lin5212d092012-11-16 00:01:35 +08001738static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1739 unsigned long config)
Linus Walleijd41af622012-05-03 15:58:12 +02001740{
1741 static const char *pullnames[] = {
1742 [NMK_GPIO_PULL_NONE] = "none",
1743 [NMK_GPIO_PULL_UP] = "up",
1744 [NMK_GPIO_PULL_DOWN] = "down",
1745 [3] /* illegal */ = "??"
1746 };
1747 static const char *slpmnames[] = {
1748 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1749 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1750 };
1751 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1752 struct nmk_gpio_chip *nmk_chip;
1753 struct pinctrl_gpio_range *range;
1754 struct gpio_chip *chip;
1755 unsigned bit;
1756
1757 /*
1758 * The pin config contains pin number and altfunction fields, here
1759 * we just ignore that part. It's being handled by the framework and
1760 * pinmux callback respectively.
1761 */
1762 pin_cfg_t cfg = (pin_cfg_t) config;
1763 int pull = PIN_PULL(cfg);
1764 int slpm = PIN_SLPM(cfg);
1765 int output = PIN_DIR(cfg);
1766 int val = PIN_VAL(cfg);
1767 bool lowemi = PIN_LOWEMI(cfg);
1768 bool gpiomode = PIN_GPIOMODE(cfg);
1769 bool sleep = PIN_SLEEPMODE(cfg);
1770
1771 range = nmk_match_gpio_range(pctldev, pin);
1772 if (!range) {
1773 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1774 return -EINVAL;
1775 }
1776 if (!range->gc) {
1777 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1778 pin);
1779 return -EINVAL;
1780 }
1781 chip = range->gc;
1782 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1783
1784 if (sleep) {
1785 int slpm_pull = PIN_SLPM_PULL(cfg);
1786 int slpm_output = PIN_SLPM_DIR(cfg);
1787 int slpm_val = PIN_SLPM_VAL(cfg);
1788
1789 /* All pins go into GPIO mode at sleep */
1790 gpiomode = true;
1791
1792 /*
1793 * The SLPM_* values are normal values + 1 to allow zero to
1794 * mean "same as normal".
1795 */
1796 if (slpm_pull)
1797 pull = slpm_pull - 1;
1798 if (slpm_output)
1799 output = slpm_output - 1;
1800 if (slpm_val)
1801 val = slpm_val - 1;
1802
1803 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1804 pin,
1805 slpm_pull ? pullnames[pull] : "same",
1806 slpm_output ? (output ? "output" : "input") : "same",
1807 slpm_val ? (val ? "high" : "low") : "same");
1808 }
1809
1810 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1811 pin, cfg, pullnames[pull], slpmnames[slpm],
1812 output ? "output " : "input",
1813 output ? (val ? "high" : "low") : "",
1814 lowemi ? "on" : "off" );
1815
1816 clk_enable(nmk_chip->clk);
1817 bit = pin % NMK_GPIO_PER_CHIP;
1818 if (gpiomode)
1819 /* No glitch when going to GPIO mode */
1820 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1821 if (output)
1822 __nmk_gpio_make_output(nmk_chip, bit, val);
1823 else {
1824 __nmk_gpio_make_input(nmk_chip, bit);
1825 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1826 }
1827 /* TODO: isn't this only applicable on output pins? */
1828 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1829
1830 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1831 clk_disable(nmk_chip->clk);
1832 return 0;
1833}
1834
1835static struct pinconf_ops nmk_pinconf_ops = {
1836 .pin_config_get = nmk_pin_config_get,
1837 .pin_config_set = nmk_pin_config_set,
1838};
1839
Linus Walleije98ea772012-04-26 23:57:25 +02001840static struct pinctrl_desc nmk_pinctrl_desc = {
1841 .name = "pinctrl-nomadik",
1842 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001843 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001844 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001845 .owner = THIS_MODULE,
1846};
1847
Lee Jones855f80c2012-05-26 06:09:29 +01001848static const struct of_device_id nmk_pinctrl_match[] = {
1849 {
1850 .compatible = "stericsson,nmk_pinctrl",
1851 .data = (void *)PINCTRL_NMK_DB8500,
1852 },
1853 {},
1854};
1855
Julien Delacou8d99b322012-12-11 09:17:47 +01001856static int nmk_pinctrl_suspend(struct platform_device *pdev, pm_message_t state)
1857{
1858 struct nmk_pinctrl *npct;
1859
1860 npct = platform_get_drvdata(pdev);
1861 if (!npct)
1862 return -EINVAL;
1863
1864 return pinctrl_force_sleep(npct->pctl);
1865}
1866
1867static int nmk_pinctrl_resume(struct platform_device *pdev)
1868{
1869 struct nmk_pinctrl *npct;
1870
1871 npct = platform_get_drvdata(pdev);
1872 if (!npct)
1873 return -EINVAL;
1874
1875 return pinctrl_force_default(npct->pctl);
1876}
1877
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001878static int nmk_pinctrl_probe(struct platform_device *pdev)
Linus Walleije98ea772012-04-26 23:57:25 +02001879{
1880 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001881 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001882 struct nmk_pinctrl *npct;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001883 struct resource *res;
Lee Jones855f80c2012-05-26 06:09:29 +01001884 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001885 int i;
1886
1887 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1888 if (!npct)
1889 return -ENOMEM;
1890
Lee Jones855f80c2012-05-26 06:09:29 +01001891 if (platid)
1892 version = platid->driver_data;
Axel Lin953e9e92012-11-15 12:56:05 +08001893 else if (np) {
1894 const struct of_device_id *match;
1895
1896 match = of_match_device(nmk_pinctrl_match, &pdev->dev);
1897 if (!match)
1898 return -ENODEV;
1899 version = (unsigned int) match->data;
1900 }
Lee Jones855f80c2012-05-26 06:09:29 +01001901
Linus Walleije98ea772012-04-26 23:57:25 +02001902 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001903 if (version == PINCTRL_NMK_STN8815)
1904 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001905 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001906 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001907 if (version == PINCTRL_NMK_DB8540)
1908 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001909
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001910 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1911 if (res) {
1912 npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
1913 resource_size(res));
1914 if (!npct->prcm_base) {
1915 dev_err(&pdev->dev,
1916 "failed to ioremap PRCM registers\n");
1917 return -ENOMEM;
1918 }
Fabio Baltieri4ca075d2012-12-18 10:12:11 +01001919 } else if (version == PINCTRL_NMK_STN8815) {
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001920 dev_info(&pdev->dev,
1921 "No PRCM base, assume no ALT-Cx control is available\n");
Fabio Baltieri4ca075d2012-12-18 10:12:11 +01001922 } else {
1923 dev_err(&pdev->dev, "missing PRCM base address\n");
1924 return -EINVAL;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001925 }
1926
Linus Walleije98ea772012-04-26 23:57:25 +02001927 /*
1928 * We need all the GPIO drivers to probe FIRST, or we will not be able
1929 * to obtain references to the struct gpio_chip * for them, and we
1930 * need this to proceed.
1931 */
1932 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001933 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001934 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001935 return -EPROBE_DEFER;
1936 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001937 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001938 }
1939
1940 nmk_pinctrl_desc.pins = npct->soc->pins;
1941 nmk_pinctrl_desc.npins = npct->soc->npins;
1942 npct->dev = &pdev->dev;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001943
Linus Walleije98ea772012-04-26 23:57:25 +02001944 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1945 if (!npct->pctl) {
1946 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1947 return -EINVAL;
1948 }
1949
1950 /* We will handle a range of GPIO pins */
1951 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1952 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1953
1954 platform_set_drvdata(pdev, npct);
1955 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1956
1957 return 0;
1958}
1959
Lee Jones513c27f2012-04-13 15:05:05 +01001960static const struct of_device_id nmk_gpio_match[] = {
1961 { .compatible = "st,nomadik-gpio", },
1962 {}
1963};
1964
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001965static struct platform_driver nmk_gpio_driver = {
1966 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001967 .owner = THIS_MODULE,
1968 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001969 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301970 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001971 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001972};
1973
Linus Walleije98ea772012-04-26 23:57:25 +02001974static const struct platform_device_id nmk_pinctrl_id[] = {
1975 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1976 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001977 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Axel Lin8c995d62012-11-04 23:30:42 +08001978 { }
Linus Walleije98ea772012-04-26 23:57:25 +02001979};
1980
1981static struct platform_driver nmk_pinctrl_driver = {
1982 .driver = {
1983 .owner = THIS_MODULE,
1984 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001985 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001986 },
1987 .probe = nmk_pinctrl_probe,
1988 .id_table = nmk_pinctrl_id,
Julien Delacou8d99b322012-12-11 09:17:47 +01001989#ifdef CONFIG_PM
1990 .suspend = nmk_pinctrl_suspend,
1991 .resume = nmk_pinctrl_resume,
1992#endif
Linus Walleije98ea772012-04-26 23:57:25 +02001993};
1994
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001995static int __init nmk_gpio_init(void)
1996{
Linus Walleije98ea772012-04-26 23:57:25 +02001997 int ret;
1998
1999 ret = platform_driver_register(&nmk_gpio_driver);
2000 if (ret)
2001 return ret;
2002 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002003}
2004
Rabin Vincent33f45ea2010-06-02 06:09:52 +01002005core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002006
2007MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
2008MODULE_DESCRIPTION("Nomadik GPIO Driver");
2009MODULE_LICENSE("GPL");