blob: 67a5ef637ea7f4f93649978e84d8eb4cf7e2b6bf [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>
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +020033#include <linux/mfd/dbx500-prcmu.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010034
Will Deaconadfed152011-02-28 10:12:29 +000035#include <asm/mach/irq.h>
36
Rabin Vincent378be062010-06-02 06:06:29 +010037#include <plat/pincfg.h>
Linus Walleij0f332862011-08-22 08:33:30 +010038#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010039
Linus Walleije98ea772012-04-26 23:57:25 +020040#include "pinctrl-nomadik.h"
41
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010042/*
43 * The GPIO module in the Nomadik family of Systems-on-Chip is an
44 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020045 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010046 *
47 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
48 */
49
Rabin Vincent01727e62010-12-13 12:02:40 +053050#define NMK_GPIO_PER_CHIP 32
51
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010052struct nmk_gpio_chip {
53 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010054 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010055 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010056 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053057 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010058 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053059 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053060 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053061 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010062 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020063 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010064 /* Keep track of configured edges */
65 u32 edge_rising;
66 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053067 u32 real_wake;
68 u32 rwimsc;
69 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053070 u32 rimsc;
71 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020072 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053073 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010074};
75
Linus Walleije98ea772012-04-26 23:57:25 +020076struct nmk_pinctrl {
77 struct device *dev;
78 struct pinctrl_dev *pctl;
79 const struct nmk_pinctrl_soc_data *soc;
80};
81
Rabin Vincent01727e62010-12-13 12:02:40 +053082static struct nmk_gpio_chip *
83nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
84
85static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
86
87#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
88
Rabin Vincent6f9a9742010-06-02 05:50:28 +010089static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
90 unsigned offset, int gpio_mode)
91{
92 u32 bit = 1 << offset;
93 u32 afunc, bfunc;
94
95 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
96 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
97 if (gpio_mode & NMK_GPIO_ALT_A)
98 afunc |= bit;
99 if (gpio_mode & NMK_GPIO_ALT_B)
100 bfunc |= bit;
101 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
102 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
103}
104
Rabin Vincent81a3c292010-05-27 12:39:23 +0100105static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
106 unsigned offset, enum nmk_gpio_slpm mode)
107{
108 u32 bit = 1 << offset;
109 u32 slpm;
110
111 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
112 if (mode == NMK_GPIO_SLPM_NOCHANGE)
113 slpm |= bit;
114 else
115 slpm &= ~bit;
116 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
117}
118
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100119static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
120 unsigned offset, enum nmk_gpio_pull pull)
121{
122 u32 bit = 1 << offset;
123 u32 pdis;
124
125 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200126 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100127 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200128 nmk_chip->pull_up &= ~bit;
129 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100130 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200131 }
132
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100133 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
134
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200135 if (pull == NMK_GPIO_PULL_UP) {
136 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100137 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200138 } else if (pull == NMK_GPIO_PULL_DOWN) {
139 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100140 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200141 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100142}
143
Rabin Vincentebc61782011-09-28 15:49:11 +0530144static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
145 unsigned offset, bool lowemi)
146{
147 u32 bit = BIT(offset);
148 bool enabled = nmk_chip->lowemi & bit;
149
150 if (lowemi == enabled)
151 return;
152
153 if (lowemi)
154 nmk_chip->lowemi |= bit;
155 else
156 nmk_chip->lowemi &= ~bit;
157
158 writel_relaxed(nmk_chip->lowemi,
159 nmk_chip->addr + NMK_GPIO_LOWEMI);
160}
161
Rabin Vincent378be062010-06-02 06:06:29 +0100162static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
163 unsigned offset)
164{
165 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
166}
167
Rabin Vincent6720db72010-09-02 11:28:48 +0100168static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
169 unsigned offset, int val)
170{
171 if (val)
172 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
173 else
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
175}
176
177static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
178 unsigned offset, int val)
179{
180 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
181 __nmk_gpio_set_output(nmk_chip, offset, val);
182}
183
Rabin Vincent01727e62010-12-13 12:02:40 +0530184static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
185 unsigned offset, int gpio_mode,
186 bool glitch)
187{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530188 u32 rwimsc = nmk_chip->rwimsc;
189 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530190
191 if (glitch && nmk_chip->set_ioforce) {
192 u32 bit = BIT(offset);
193
Rabin Vincent01727e62010-12-13 12:02:40 +0530194 /* Prevent spurious wakeups */
195 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
196 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
197
198 nmk_chip->set_ioforce(true);
199 }
200
201 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
202
203 if (glitch && nmk_chip->set_ioforce) {
204 nmk_chip->set_ioforce(false);
205
206 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
207 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
208 }
209}
210
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530211static void
212nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
213{
214 u32 falling = nmk_chip->fimsc & BIT(offset);
215 u32 rising = nmk_chip->rimsc & BIT(offset);
216 int gpio = nmk_chip->chip.base + offset;
217 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
218 struct irq_data *d = irq_get_irq_data(irq);
219
220 if (!rising && !falling)
221 return;
222
223 if (!d || !irqd_irq_disabled(d))
224 return;
225
226 if (rising) {
227 nmk_chip->rimsc &= ~BIT(offset);
228 writel_relaxed(nmk_chip->rimsc,
229 nmk_chip->addr + NMK_GPIO_RIMSC);
230 }
231
232 if (falling) {
233 nmk_chip->fimsc &= ~BIT(offset);
234 writel_relaxed(nmk_chip->fimsc,
235 nmk_chip->addr + NMK_GPIO_FIMSC);
236 }
237
238 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
239}
240
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200241static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
242 unsigned offset, unsigned alt_num)
243{
244 int i;
245 u16 reg;
246 u8 bit;
247 u8 alt_index;
248 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
249 const u16 *gpiocr_regs;
250
251 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
252 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
253 alt_num);
254 return;
255 }
256
257 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
258 if (npct->soc->altcx_pins[i].pin == offset)
259 break;
260 }
261 if (i == npct->soc->npins_altcx) {
262 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
263 offset);
264 return;
265 }
266
267 pin_desc = npct->soc->altcx_pins + i;
268 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
269
270 /*
271 * If alt_num is NULL, just clear current ALTCx selection
272 * to make sure we come back to a pure ALTC selection
273 */
274 if (!alt_num) {
275 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
276 if (pin_desc->altcx[i].used == true) {
277 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
278 bit = pin_desc->altcx[i].control_bit;
279 if (prcmu_read(reg) & BIT(bit)) {
280 prcmu_write_masked(reg, BIT(bit), 0);
281 dev_dbg(npct->dev,
282 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
283 offset, i+1);
284 }
285 }
286 }
287 return;
288 }
289
290 alt_index = alt_num - 1;
291 if (pin_desc->altcx[alt_index].used == false) {
292 dev_warn(npct->dev,
293 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
294 offset, alt_num);
295 return;
296 }
297
298 /*
299 * Check if any other ALTCx functions are activated on this pin
300 * and disable it first.
301 */
302 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
303 if (i == alt_index)
304 continue;
305 if (pin_desc->altcx[i].used == true) {
306 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
307 bit = pin_desc->altcx[i].control_bit;
308 if (prcmu_read(reg) & BIT(bit)) {
309 prcmu_write_masked(reg, BIT(bit), 0);
310 dev_dbg(npct->dev,
311 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
312 offset, i+1);
313 }
314 }
315 }
316
317 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
318 bit = pin_desc->altcx[alt_index].control_bit;
319 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
320 offset, alt_index+1);
321 prcmu_write_masked(reg, BIT(bit), BIT(bit));
322}
323
Rabin Vincent378be062010-06-02 06:06:29 +0100324static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530325 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100326{
327 static const char *afnames[] = {
328 [NMK_GPIO_ALT_GPIO] = "GPIO",
329 [NMK_GPIO_ALT_A] = "A",
330 [NMK_GPIO_ALT_B] = "B",
331 [NMK_GPIO_ALT_C] = "C"
332 };
333 static const char *pullnames[] = {
334 [NMK_GPIO_PULL_NONE] = "none",
335 [NMK_GPIO_PULL_UP] = "up",
336 [NMK_GPIO_PULL_DOWN] = "down",
337 [3] /* illegal */ = "??"
338 };
339 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100340 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
341 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100342 };
343
344 int pin = PIN_NUM(cfg);
345 int pull = PIN_PULL(cfg);
346 int af = PIN_ALT(cfg);
347 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100348 int output = PIN_DIR(cfg);
349 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530350 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100351
Rabin Vincentdacdc962010-12-03 20:35:37 +0530352 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
353 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100354 output ? "output " : "input",
355 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100356
Rabin Vincentdacdc962010-12-03 20:35:37 +0530357 if (sleep) {
358 int slpm_pull = PIN_SLPM_PULL(cfg);
359 int slpm_output = PIN_SLPM_DIR(cfg);
360 int slpm_val = PIN_SLPM_VAL(cfg);
361
Rabin Vincent3546d152010-11-25 11:38:27 +0530362 af = NMK_GPIO_ALT_GPIO;
363
Rabin Vincentdacdc962010-12-03 20:35:37 +0530364 /*
365 * The SLPM_* values are normal values + 1 to allow zero to
366 * mean "same as normal".
367 */
368 if (slpm_pull)
369 pull = slpm_pull - 1;
370 if (slpm_output)
371 output = slpm_output - 1;
372 if (slpm_val)
373 val = slpm_val - 1;
374
375 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
376 pin,
377 slpm_pull ? pullnames[pull] : "same",
378 slpm_output ? (output ? "output" : "input") : "same",
379 slpm_val ? (val ? "high" : "low") : "same");
380 }
381
Rabin Vincent6720db72010-09-02 11:28:48 +0100382 if (output)
383 __nmk_gpio_make_output(nmk_chip, offset, val);
384 else {
385 __nmk_gpio_make_input(nmk_chip, offset);
386 __nmk_gpio_set_pull(nmk_chip, offset, pull);
387 }
388
Rabin Vincentebc61782011-09-28 15:49:11 +0530389 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
390
Rabin Vincent01727e62010-12-13 12:02:40 +0530391 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530392 * If the pin is switching to altfunc, and there was an interrupt
393 * installed on it which has been lazy disabled, actually mask the
394 * interrupt to prevent spurious interrupts that would occur while the
395 * pin is under control of the peripheral. Only SKE does this.
396 */
397 if (af != NMK_GPIO_ALT_GPIO)
398 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
399
400 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530401 * If we've backed up the SLPM registers (glitch workaround), modify
402 * the backups since they will be restored.
403 */
404 if (slpmregs) {
405 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
406 slpmregs[nmk_chip->bank] |= BIT(offset);
407 else
408 slpmregs[nmk_chip->bank] &= ~BIT(offset);
409 } else
410 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
411
412 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
413}
414
415/*
416 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
417 * - Save SLPM registers
418 * - Set SLPM=0 for the IOs you want to switch and others to 1
419 * - Configure the GPIO registers for the IOs that are being switched
420 * - Set IOFORCE=1
421 * - Modify the AFLSA/B registers for the IOs that are being switched
422 * - Set IOFORCE=0
423 * - Restore SLPM registers
424 * - Any spurious wake up event during switch sequence to be ignored and
425 * cleared
426 */
427static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
428{
429 int i;
430
431 for (i = 0; i < NUM_BANKS; i++) {
432 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
433 unsigned int temp = slpm[i];
434
435 if (!chip)
436 break;
437
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200438 clk_enable(chip->clk);
439
Rabin Vincent01727e62010-12-13 12:02:40 +0530440 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
441 writel(temp, chip->addr + NMK_GPIO_SLPC);
442 }
443}
444
445static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
446{
447 int i;
448
449 for (i = 0; i < NUM_BANKS; i++) {
450 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
451
452 if (!chip)
453 break;
454
455 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200456
457 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530458 }
459}
460
461static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
462{
463 static unsigned int slpm[NUM_BANKS];
464 unsigned long flags;
465 bool glitch = false;
466 int ret = 0;
467 int i;
468
469 for (i = 0; i < num; i++) {
470 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
471 glitch = true;
472 break;
473 }
474 }
475
476 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
477
478 if (glitch) {
479 memset(slpm, 0xff, sizeof(slpm));
480
481 for (i = 0; i < num; i++) {
482 int pin = PIN_NUM(cfgs[i]);
483 int offset = pin % NMK_GPIO_PER_CHIP;
484
485 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
486 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
487 }
488
489 nmk_gpio_glitch_slpm_init(slpm);
490 }
491
492 for (i = 0; i < num; i++) {
493 struct nmk_gpio_chip *nmk_chip;
494 int pin = PIN_NUM(cfgs[i]);
495
Lee Jonesa60b57e2012-04-19 21:36:31 +0100496 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530497 if (!nmk_chip) {
498 ret = -EINVAL;
499 break;
500 }
501
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200502 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530503 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100504 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530505 cfgs[i], sleep, glitch ? slpm : NULL);
506 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200507 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530508 }
509
510 if (glitch)
511 nmk_gpio_glitch_slpm_restore(slpm);
512
513 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
514
515 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100516}
517
518/**
519 * nmk_config_pin - configure a pin's mux attributes
520 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200521 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100522 * Configures a pin's mode (alternate function or GPIO), its pull up status,
523 * and its sleep mode based on the specified configuration. The @cfg is
524 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
525 * are constructed using, and can be further enhanced with, the macros in
526 * plat/pincfg.h.
527 *
528 * If a pin's mode is set to GPIO, it is configured as an input to avoid
529 * side-effects. The gpio can be manipulated later using standard GPIO API
530 * calls.
531 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530532int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100533{
Rabin Vincent01727e62010-12-13 12:02:40 +0530534 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100535}
536EXPORT_SYMBOL(nmk_config_pin);
537
538/**
539 * nmk_config_pins - configure several pins at once
540 * @cfgs: array of pin configurations
541 * @num: number of elments in the array
542 *
543 * Configures several pins using nmk_config_pin(). Refer to that function for
544 * further information.
545 */
546int nmk_config_pins(pin_cfg_t *cfgs, int num)
547{
Rabin Vincent01727e62010-12-13 12:02:40 +0530548 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100549}
550EXPORT_SYMBOL(nmk_config_pins);
551
Rabin Vincentdacdc962010-12-03 20:35:37 +0530552int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
553{
Rabin Vincent01727e62010-12-13 12:02:40 +0530554 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530555}
556EXPORT_SYMBOL(nmk_config_pins_sleep);
557
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100558/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100559 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
560 * @gpio: pin number
561 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
562 *
Linus Walleij33d78642011-06-09 11:08:47 +0200563 * This register is actually in the pinmux layer, not the GPIO block itself.
564 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
565 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
566 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
567 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
568 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
569 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100570 *
Linus Walleij33d78642011-06-09 11:08:47 +0200571 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
572 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
573 * entered) regardless of the altfunction selected. Also wake-up detection is
574 * ENABLED.
575 *
576 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
577 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
578 * (for altfunction GPIO) or respective on-chip peripherals (for other
579 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
580 *
581 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100582 */
583int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
584{
585 struct nmk_gpio_chip *nmk_chip;
586 unsigned long flags;
587
Lee Jonesa60b57e2012-04-19 21:36:31 +0100588 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100589 if (!nmk_chip)
590 return -EINVAL;
591
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200592 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530593 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
594 spin_lock(&nmk_chip->lock);
595
Lee Jonesa60b57e2012-04-19 21:36:31 +0100596 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530597
598 spin_unlock(&nmk_chip->lock);
599 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200600 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100601
602 return 0;
603}
604
605/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100606 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
607 * @gpio: pin number
608 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
609 *
610 * Enables/disables pull up/down on a specified pin. This only takes effect if
611 * the pin is configured as an input (either explicitly or by the alternate
612 * function).
613 *
614 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
615 * configured as an input. Otherwise, due to the way the controller registers
616 * work, this function will change the value output on the pin.
617 */
618int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
619{
620 struct nmk_gpio_chip *nmk_chip;
621 unsigned long flags;
622
Lee Jonesa60b57e2012-04-19 21:36:31 +0100623 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100624 if (!nmk_chip)
625 return -EINVAL;
626
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200627 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100628 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100629 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100630 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200631 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100632
633 return 0;
634}
635
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100636/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200637/**
638 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
639 * @gpio: pin number
640 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
641 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
642 *
643 * Sets the mode of the specified pin to one of the alternate functions or
644 * plain GPIO.
645 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100646int nmk_gpio_set_mode(int gpio, int gpio_mode)
647{
648 struct nmk_gpio_chip *nmk_chip;
649 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100650
Lee Jonesa60b57e2012-04-19 21:36:31 +0100651 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100652 if (!nmk_chip)
653 return -EINVAL;
654
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200655 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100656 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100657 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100658 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200659 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100660
661 return 0;
662}
663EXPORT_SYMBOL(nmk_gpio_set_mode);
664
665int nmk_gpio_get_mode(int gpio)
666{
667 struct nmk_gpio_chip *nmk_chip;
668 u32 afunc, bfunc, bit;
669
Lee Jonesa60b57e2012-04-19 21:36:31 +0100670 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100671 if (!nmk_chip)
672 return -EINVAL;
673
Lee Jonesa60b57e2012-04-19 21:36:31 +0100674 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100675
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200676 clk_enable(nmk_chip->clk);
677
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100678 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
679 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
680
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200681 clk_disable(nmk_chip->clk);
682
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
684}
685EXPORT_SYMBOL(nmk_gpio_get_mode);
686
687
688/* IRQ functions */
689static inline int nmk_gpio_get_bitmask(int gpio)
690{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100691 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100692}
693
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100694static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100695{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100696 struct nmk_gpio_chip *nmk_chip;
697
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100698 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100699 if (!nmk_chip)
700 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200701
702 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100703 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200704 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100705}
706
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100707enum nmk_gpio_irq_type {
708 NORMAL,
709 WAKE,
710};
711
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100712static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100713 int gpio, enum nmk_gpio_irq_type which,
714 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100715{
716 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530717 u32 *rimscval;
718 u32 *fimscval;
719 u32 rimscreg;
720 u32 fimscreg;
721
722 if (which == NORMAL) {
723 rimscreg = NMK_GPIO_RIMSC;
724 fimscreg = NMK_GPIO_FIMSC;
725 rimscval = &nmk_chip->rimsc;
726 fimscval = &nmk_chip->fimsc;
727 } else {
728 rimscreg = NMK_GPIO_RWIMSC;
729 fimscreg = NMK_GPIO_FWIMSC;
730 rimscval = &nmk_chip->rwimsc;
731 fimscval = &nmk_chip->fwimsc;
732 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100733
734 /* we must individually set/clear the two edges */
735 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100736 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530737 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100738 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530739 *rimscval &= ~bitmask;
740 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100741 }
742 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100743 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530744 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100745 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530746 *fimscval &= ~bitmask;
747 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100748 }
749}
750
Rabin Vincentb9df4682011-02-10 11:45:58 +0530751static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
752 int gpio, bool on)
753{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530754 /*
755 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
756 * disabled, since setting SLPM to 1 increases power consumption, and
757 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
758 */
759 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200760 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530761 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200762 }
763
Rabin Vincentb9df4682011-02-10 11:45:58 +0530764 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
765}
766
767static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100768{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100769 struct nmk_gpio_chip *nmk_chip;
770 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100771 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100772
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100773 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100774 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100775 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100776 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100777
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200778 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530779 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
780 spin_lock(&nmk_chip->lock);
781
Lee Jonesa60b57e2012-04-19 21:36:31 +0100782 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530783
784 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100785 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530786
787 spin_unlock(&nmk_chip->lock);
788 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200789 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100790
791 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100792}
793
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100794static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100795{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530796 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100797}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100798
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100799static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100800{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530801 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100802}
803
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100804static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100805{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100806 struct nmk_gpio_chip *nmk_chip;
807 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530808 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100809
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100810 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100811 if (!nmk_chip)
812 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100813 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100814
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200815 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530816 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
817 spin_lock(&nmk_chip->lock);
818
Linus Walleij479a0c72011-09-20 10:50:15 +0200819 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100820 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530821
822 if (on)
823 nmk_chip->real_wake |= bitmask;
824 else
825 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530826
827 spin_unlock(&nmk_chip->lock);
828 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200829 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100830
831 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100832}
833
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100834static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835{
Linus Walleij479a0c72011-09-20 10:50:15 +0200836 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200837 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838 struct nmk_gpio_chip *nmk_chip;
839 unsigned long flags;
840 u32 bitmask;
841
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100842 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100843 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100844 if (!nmk_chip)
845 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100846 if (type & IRQ_TYPE_LEVEL_HIGH)
847 return -EINVAL;
848 if (type & IRQ_TYPE_LEVEL_LOW)
849 return -EINVAL;
850
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200851 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100852 spin_lock_irqsave(&nmk_chip->lock, flags);
853
Rabin Vincent7a852d82010-05-06 10:43:55 +0100854 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100855 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100856
Rabin Vincentb9df4682011-02-10 11:45:58 +0530857 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100858 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100859
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100860 nmk_chip->edge_rising &= ~bitmask;
861 if (type & IRQ_TYPE_EDGE_RISING)
862 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100863
864 nmk_chip->edge_falling &= ~bitmask;
865 if (type & IRQ_TYPE_EDGE_FALLING)
866 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100867
868 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100869 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100870
Rabin Vincentb9df4682011-02-10 11:45:58 +0530871 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100872 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100873
874 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200875 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100876
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100877 return 0;
878}
879
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200880static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
881{
882 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
883
884 clk_enable(nmk_chip->clk);
885 nmk_gpio_irq_unmask(d);
886 return 0;
887}
888
889static void nmk_gpio_irq_shutdown(struct irq_data *d)
890{
891 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
892
893 nmk_gpio_irq_mask(d);
894 clk_disable(nmk_chip->clk);
895}
896
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100897static struct irq_chip nmk_gpio_irq_chip = {
898 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100899 .irq_ack = nmk_gpio_irq_ack,
900 .irq_mask = nmk_gpio_irq_mask,
901 .irq_unmask = nmk_gpio_irq_unmask,
902 .irq_set_type = nmk_gpio_irq_set_type,
903 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200904 .irq_startup = nmk_gpio_irq_startup,
905 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200906 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100907};
908
Rabin Vincent33b744b2010-10-14 10:38:03 +0530909static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
910 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100911{
912 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100913 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100914
Will Deaconadfed152011-02-28 10:12:29 +0000915 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100916
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100917 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530918 while (status) {
919 int bit = __ffs(status);
920
Linus Walleij95f0bc92012-09-27 14:14:09 +0200921 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530922 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100923 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100924
Will Deaconadfed152011-02-28 10:12:29 +0000925 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100926}
927
Rabin Vincent33b744b2010-10-14 10:38:03 +0530928static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
929{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100930 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200931 u32 status;
932
933 clk_enable(nmk_chip->clk);
934 status = readl(nmk_chip->addr + NMK_GPIO_IS);
935 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530936
937 __nmk_gpio_irq_handler(irq, desc, status);
938}
939
940static void nmk_gpio_secondary_irq_handler(unsigned int irq,
941 struct irq_desc *desc)
942{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100943 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530944 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
945
946 __nmk_gpio_irq_handler(irq, desc, status);
947}
948
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100949static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
950{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100951 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
952 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530953
954 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100955 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530956 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100957 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530958 }
959
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100960 return 0;
961}
962
963/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200964
965static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
966{
967 /*
968 * Map back to global GPIO space and request muxing, the direction
969 * parameter does not matter for this controller.
970 */
971 int gpio = chip->base + offset;
972
973 return pinctrl_request_gpio(gpio);
974}
975
976static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
977{
978 int gpio = chip->base + offset;
979
980 pinctrl_free_gpio(gpio);
981}
982
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100983static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
984{
985 struct nmk_gpio_chip *nmk_chip =
986 container_of(chip, struct nmk_gpio_chip, chip);
987
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200988 clk_enable(nmk_chip->clk);
989
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100990 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200991
992 clk_disable(nmk_chip->clk);
993
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100994 return 0;
995}
996
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100997static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
998{
999 struct nmk_gpio_chip *nmk_chip =
1000 container_of(chip, struct nmk_gpio_chip, chip);
1001 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001002 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001003
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001004 clk_enable(nmk_chip->clk);
1005
1006 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1007
1008 clk_disable(nmk_chip->clk);
1009
1010 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001011}
1012
1013static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1014 int val)
1015{
1016 struct nmk_gpio_chip *nmk_chip =
1017 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001018
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001019 clk_enable(nmk_chip->clk);
1020
Rabin Vincent6720db72010-09-02 11:28:48 +01001021 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001022
1023 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001024}
1025
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001026static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1027 int val)
1028{
1029 struct nmk_gpio_chip *nmk_chip =
1030 container_of(chip, struct nmk_gpio_chip, chip);
1031
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001032 clk_enable(nmk_chip->clk);
1033
Rabin Vincent6720db72010-09-02 11:28:48 +01001034 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001035
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001036 clk_disable(nmk_chip->clk);
1037
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001038 return 0;
1039}
1040
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001041static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1042{
1043 struct nmk_gpio_chip *nmk_chip =
1044 container_of(chip, struct nmk_gpio_chip, chip);
1045
Lee Jonesa60b57e2012-04-19 21:36:31 +01001046 return irq_find_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001047}
1048
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301049#ifdef CONFIG_DEBUG_FS
1050
1051#include <linux/seq_file.h>
1052
Linus Walleij6f4350a2012-05-02 21:06:13 +02001053static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
1054 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301055{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001056 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301057 struct nmk_gpio_chip *nmk_chip =
1058 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001059 int mode;
1060 bool is_out;
1061 bool pull;
1062 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301063 const char *modes[] = {
1064 [NMK_GPIO_ALT_GPIO] = "gpio",
1065 [NMK_GPIO_ALT_A] = "altA",
1066 [NMK_GPIO_ALT_B] = "altB",
1067 [NMK_GPIO_ALT_C] = "altC",
1068 };
1069
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001070 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001071 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1072 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1073 mode = nmk_gpio_get_mode(gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001074
Linus Walleij6f4350a2012-05-02 21:06:13 +02001075 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1076 gpio, label ?: "(none)",
1077 is_out ? "out" : "in ",
1078 chip->get
1079 ? (chip->get(chip, offset) ? "hi" : "lo")
1080 : "? ",
1081 (mode < 0) ? "unknown" : modes[mode],
1082 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301083
Linus Walleij6f4350a2012-05-02 21:06:13 +02001084 if (label && !is_out) {
1085 int irq = gpio_to_irq(gpio);
1086 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001087
Linus Walleij6f4350a2012-05-02 21:06:13 +02001088 /* This races with request_irq(), set_irq_type(),
1089 * and set_irq_wake() ... but those are "rare".
1090 */
1091 if (irq >= 0 && desc->action) {
1092 char *trigger;
1093 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001094
Linus Walleij6f4350a2012-05-02 21:06:13 +02001095 if (nmk_chip->edge_rising & bitmask)
1096 trigger = "edge-rising";
1097 else if (nmk_chip->edge_falling & bitmask)
1098 trigger = "edge-falling";
1099 else
1100 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001101
Linus Walleij6f4350a2012-05-02 21:06:13 +02001102 seq_printf(s, " irq-%d %s%s",
1103 irq, trigger,
1104 irqd_is_wakeup_set(&desc->irq_data)
1105 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001106 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301107 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001108 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301109}
1110
Linus Walleij6f4350a2012-05-02 21:06:13 +02001111static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1112{
1113 unsigned i;
1114 unsigned gpio = chip->base;
1115
1116 for (i = 0; i < chip->ngpio; i++, gpio++) {
1117 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1118 seq_printf(s, "\n");
1119 }
1120}
1121
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301122#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001123static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
1124 struct gpio_chip *chip,
1125 unsigned offset, unsigned gpio)
1126{
1127}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301128#define nmk_gpio_dbg_show NULL
1129#endif
1130
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001131/* This structure is replicated for each GPIO block allocated at probe time */
1132static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001133 .request = nmk_gpio_request,
1134 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001135 .direction_input = nmk_gpio_make_input,
1136 .get = nmk_gpio_get_input,
1137 .direction_output = nmk_gpio_make_output,
1138 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001139 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301140 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001141 .can_sleep = 0,
1142};
1143
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001144void nmk_gpio_clocks_enable(void)
1145{
1146 int i;
1147
1148 for (i = 0; i < NUM_BANKS; i++) {
1149 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1150
1151 if (!chip)
1152 continue;
1153
1154 clk_enable(chip->clk);
1155 }
1156}
1157
1158void nmk_gpio_clocks_disable(void)
1159{
1160 int i;
1161
1162 for (i = 0; i < NUM_BANKS; i++) {
1163 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1164
1165 if (!chip)
1166 continue;
1167
1168 clk_disable(chip->clk);
1169 }
1170}
1171
Rabin Vincentb9df4682011-02-10 11:45:58 +05301172/*
1173 * Called from the suspend/resume path to only keep the real wakeup interrupts
1174 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1175 * and not the rest of the interrupts which we needed to have as wakeups for
1176 * cpuidle.
1177 *
1178 * PM ops are not used since this needs to be done at the end, after all the
1179 * other drivers are done with their suspend callbacks.
1180 */
1181void nmk_gpio_wakeups_suspend(void)
1182{
1183 int i;
1184
1185 for (i = 0; i < NUM_BANKS; i++) {
1186 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1187
1188 if (!chip)
1189 break;
1190
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001191 clk_enable(chip->clk);
1192
Rabin Vincentb9df4682011-02-10 11:45:58 +05301193 writel(chip->rwimsc & chip->real_wake,
1194 chip->addr + NMK_GPIO_RWIMSC);
1195 writel(chip->fwimsc & chip->real_wake,
1196 chip->addr + NMK_GPIO_FWIMSC);
1197
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001198 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301199 }
1200}
1201
1202void nmk_gpio_wakeups_resume(void)
1203{
1204 int i;
1205
1206 for (i = 0; i < NUM_BANKS; i++) {
1207 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1208
1209 if (!chip)
1210 break;
1211
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001212 clk_enable(chip->clk);
1213
Rabin Vincentb9df4682011-02-10 11:45:58 +05301214 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1215 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1216
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001217 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301218 }
1219}
1220
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001221/*
1222 * Read the pull up/pull down status.
1223 * A bit set in 'pull_up' means that pull up
1224 * is selected if pull is enabled in PDIS register.
1225 * Note: only pull up/down set via this driver can
1226 * be detected due to HW limitations.
1227 */
1228void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1229{
1230 if (gpio_bank < NUM_BANKS) {
1231 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1232
1233 if (!chip)
1234 return;
1235
1236 *pull_up = chip->pull_up;
1237 }
1238}
1239
Lee Jonesa60b57e2012-04-19 21:36:31 +01001240int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1241 irq_hw_number_t hwirq)
1242{
1243 struct nmk_gpio_chip *nmk_chip = d->host_data;
1244
1245 if (!nmk_chip)
1246 return -EINVAL;
1247
1248 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1249 set_irq_flags(irq, IRQF_VALID);
1250 irq_set_chip_data(irq, nmk_chip);
1251 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1252
1253 return 0;
1254}
1255
1256const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1257 .map = nmk_gpio_irq_map,
1258 .xlate = irq_domain_xlate_twocell,
1259};
1260
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001261static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001262{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001263 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001264 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001265 struct nmk_gpio_chip *nmk_chip;
1266 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001267 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001268 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301269 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001270 void __iomem *base;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001271 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001272 int ret;
1273
Lee Jones513c27f2012-04-13 15:05:05 +01001274 if (!pdata && !np) {
1275 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001276 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001277 }
1278
1279 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001280 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001281 if (!pdata)
1282 return -ENOMEM;
1283
Lee Jones612e1d52012-06-14 11:27:56 +01001284 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001285 pdata->supports_sleepmode = true;
1286
1287 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1288 dev_err(&dev->dev, "gpio-bank property not found\n");
1289 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001290 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001291 }
1292
1293 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1294 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1295 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001296
1297 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1298 if (!res) {
1299 ret = -ENOENT;
1300 goto out;
1301 }
1302
1303 irq = platform_get_irq(dev, 0);
1304 if (irq < 0) {
1305 ret = irq;
1306 goto out;
1307 }
1308
Rabin Vincent33b744b2010-10-14 10:38:03 +05301309 secondary_irq = platform_get_irq(dev, 1);
1310 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1311 ret = -EINVAL;
1312 goto out;
1313 }
1314
Linus Walleij5e754f32012-07-03 23:05:14 +02001315 base = devm_request_and_ioremap(&dev->dev, res);
1316 if (!base) {
1317 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001318 goto out;
1319 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001320
Linus Walleij5e754f32012-07-03 23:05:14 +02001321 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001322 if (IS_ERR(clk)) {
1323 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001324 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001325 }
Linus Walleijefec3812012-06-06 22:50:41 +02001326 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001327
Linus Walleij5e754f32012-07-03 23:05:14 +02001328 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001329 if (!nmk_chip) {
1330 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001331 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001332 }
Lee Jones513c27f2012-04-13 15:05:05 +01001333
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001334 /*
1335 * The virt address in nmk_chip->addr is in the nomadik register space,
1336 * so we can simply convert the resource address, without remapping
1337 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301338 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001339 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001340 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001341 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001342 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301343 nmk_chip->secondary_parent_irq = secondary_irq;
1344 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301345 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001346 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001347 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001348
1349 chip = &nmk_chip->chip;
1350 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301351 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301352 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001353 chip->dev = &dev->dev;
1354 chip->owner = THIS_MODULE;
1355
Rabin Vincentebc61782011-09-28 15:49:11 +05301356 clk_enable(nmk_chip->clk);
1357 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1358 clk_disable(nmk_chip->clk);
1359
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001360#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001361 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001362#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001363
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001364 ret = gpiochip_add(&nmk_chip->chip);
1365 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001366 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001367
Rabin Vincent01727e62010-12-13 12:02:40 +05301368 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1369
1370 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001371
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001372 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001373
Lee Jonesa60b57e2012-04-19 21:36:31 +01001374 nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
1375 NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
1376 0, &nmk_gpio_irq_simple_ops, nmk_chip);
1377 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001378 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001379 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001380 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001381 }
1382
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001383 nmk_gpio_init_irq(nmk_chip);
1384
Lee Jones513c27f2012-04-13 15:05:05 +01001385 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1386
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001387 return 0;
1388
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001389out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001390 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1391 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001392
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001393 return ret;
1394}
1395
Linus Walleije98ea772012-04-26 23:57:25 +02001396static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1397{
1398 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1399
1400 return npct->soc->ngroups;
1401}
1402
1403static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1404 unsigned selector)
1405{
1406 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1407
1408 return npct->soc->groups[selector].name;
1409}
1410
1411static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1412 const unsigned **pins,
1413 unsigned *num_pins)
1414{
1415 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1416
1417 *pins = npct->soc->groups[selector].pins;
1418 *num_pins = npct->soc->groups[selector].npins;
1419 return 0;
1420}
1421
Linus Walleij24cbdd72012-05-02 21:28:00 +02001422static struct pinctrl_gpio_range *
1423nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1424{
1425 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1426 int i;
1427
1428 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1429 struct pinctrl_gpio_range *range;
1430
1431 range = &npct->soc->gpio_ranges[i];
1432 if (offset >= range->pin_base &&
1433 offset <= (range->pin_base + range->npins - 1))
1434 return range;
1435 }
1436 return NULL;
1437}
1438
Linus Walleije98ea772012-04-26 23:57:25 +02001439static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1440 unsigned offset)
1441{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001442 struct pinctrl_gpio_range *range;
1443 struct gpio_chip *chip;
1444
1445 range = nmk_match_gpio_range(pctldev, offset);
1446 if (!range || !range->gc) {
1447 seq_printf(s, "invalid pin offset");
1448 return;
1449 }
1450 chip = range->gc;
1451 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001452}
1453
1454static struct pinctrl_ops nmk_pinctrl_ops = {
1455 .get_groups_count = nmk_get_groups_cnt,
1456 .get_group_name = nmk_get_group_name,
1457 .get_group_pins = nmk_get_group_pins,
1458 .pin_dbg_show = nmk_pin_dbg_show,
1459};
1460
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001461static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1462{
1463 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1464
1465 return npct->soc->nfunctions;
1466}
1467
1468static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1469 unsigned function)
1470{
1471 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1472
1473 return npct->soc->functions[function].name;
1474}
1475
1476static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1477 unsigned function,
1478 const char * const **groups,
1479 unsigned * const num_groups)
1480{
1481 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1482
1483 *groups = npct->soc->functions[function].groups;
1484 *num_groups = npct->soc->functions[function].ngroups;
1485
1486 return 0;
1487}
1488
1489static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1490 unsigned group)
1491{
1492 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1493 const struct nmk_pingroup *g;
1494 static unsigned int slpm[NUM_BANKS];
1495 unsigned long flags;
1496 bool glitch;
1497 int ret = -EINVAL;
1498 int i;
1499
1500 g = &npct->soc->groups[group];
1501
1502 if (g->altsetting < 0)
1503 return -EINVAL;
1504
1505 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1506
Linus Walleijdaf73172012-05-22 11:46:45 +02001507 /*
1508 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1509 * we may pass through an undesired state. In this case we take
1510 * some extra care.
1511 *
1512 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1513 * - Save SLPM registers (since we have a shadow register in the
1514 * nmk_chip we're using that as backup)
1515 * - Set SLPM=0 for the IOs you want to switch and others to 1
1516 * - Configure the GPIO registers for the IOs that are being switched
1517 * - Set IOFORCE=1
1518 * - Modify the AFLSA/B registers for the IOs that are being switched
1519 * - Set IOFORCE=0
1520 * - Restore SLPM registers
1521 * - Any spurious wake up event during switch sequence to be ignored
1522 * and cleared
1523 *
1524 * We REALLY need to save ALL slpm registers, because the external
1525 * IOFORCE will switch *all* ports to their sleepmode setting to as
1526 * to avoid glitches. (Not just one port!)
1527 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001528 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001529
1530 if (glitch) {
1531 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1532
1533 /* Initially don't put any pins to sleep when switching */
1534 memset(slpm, 0xff, sizeof(slpm));
1535
1536 /*
1537 * Then mask the pins that need to be sleeping now when we're
1538 * switching to the ALT C function.
1539 */
1540 for (i = 0; i < g->npins; i++)
1541 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1542 nmk_gpio_glitch_slpm_init(slpm);
1543 }
1544
1545 for (i = 0; i < g->npins; i++) {
1546 struct pinctrl_gpio_range *range;
1547 struct nmk_gpio_chip *nmk_chip;
1548 struct gpio_chip *chip;
1549 unsigned bit;
1550
1551 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1552 if (!range) {
1553 dev_err(npct->dev,
1554 "invalid pin offset %d in group %s at index %d\n",
1555 g->pins[i], g->name, i);
1556 goto out_glitch;
1557 }
1558 if (!range->gc) {
1559 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1560 g->pins[i], g->name, i);
1561 goto out_glitch;
1562 }
1563 chip = range->gc;
1564 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1565 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1566
1567 clk_enable(nmk_chip->clk);
1568 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1569 /*
1570 * If the pin is switching to altfunc, and there was an
1571 * interrupt installed on it which has been lazy disabled,
1572 * actually mask the interrupt to prevent spurious interrupts
1573 * that would occur while the pin is under control of the
1574 * peripheral. Only SKE does this.
1575 */
1576 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1577
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001578 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1579 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001580 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001581
1582 /*
1583 * Call PRCM GPIOCR config function in case ALTC
1584 * has been selected:
1585 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1586 * must be set.
1587 * - If selection is pure ALTC and previous selection was ALTCx,
1588 * then some bits in PRCM GPIOCR registers must be cleared.
1589 */
1590 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1591 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1592 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001593 }
1594
1595 /* When all pins are successfully reconfigured we get here */
1596 ret = 0;
1597
1598out_glitch:
1599 if (glitch) {
1600 nmk_gpio_glitch_slpm_restore(slpm);
1601 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1602 }
1603
1604 return ret;
1605}
1606
1607static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1608 unsigned function, unsigned group)
1609{
1610 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1611 const struct nmk_pingroup *g;
1612
1613 g = &npct->soc->groups[group];
1614
1615 if (g->altsetting < 0)
1616 return;
1617
1618 /* Poke out the mux, set the pin to some default state? */
1619 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1620}
1621
1622int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1623 struct pinctrl_gpio_range *range,
1624 unsigned offset)
1625{
1626 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1627 struct nmk_gpio_chip *nmk_chip;
1628 struct gpio_chip *chip;
1629 unsigned bit;
1630
1631 if (!range) {
1632 dev_err(npct->dev, "invalid range\n");
1633 return -EINVAL;
1634 }
1635 if (!range->gc) {
1636 dev_err(npct->dev, "missing GPIO chip in range\n");
1637 return -EINVAL;
1638 }
1639 chip = range->gc;
1640 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1641
1642 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1643
1644 clk_enable(nmk_chip->clk);
1645 bit = offset % NMK_GPIO_PER_CHIP;
1646 /* There is no glitch when converting any pin to GPIO */
1647 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1648 clk_disable(nmk_chip->clk);
1649
1650 return 0;
1651}
1652
1653void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1654 struct pinctrl_gpio_range *range,
1655 unsigned offset)
1656{
1657 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1658
1659 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1660 /* Set the pin to some default state, GPIO is usually default */
1661}
1662
1663static struct pinmux_ops nmk_pinmux_ops = {
1664 .get_functions_count = nmk_pmx_get_funcs_cnt,
1665 .get_function_name = nmk_pmx_get_func_name,
1666 .get_function_groups = nmk_pmx_get_func_groups,
1667 .enable = nmk_pmx_enable,
1668 .disable = nmk_pmx_disable,
1669 .gpio_request_enable = nmk_gpio_request_enable,
1670 .gpio_disable_free = nmk_gpio_disable_free,
1671};
1672
Linus Walleijd41af622012-05-03 15:58:12 +02001673int nmk_pin_config_get(struct pinctrl_dev *pctldev,
1674 unsigned pin,
1675 unsigned long *config)
1676{
1677 /* Not implemented */
1678 return -EINVAL;
1679}
1680
1681int nmk_pin_config_set(struct pinctrl_dev *pctldev,
1682 unsigned pin,
1683 unsigned long config)
1684{
1685 static const char *pullnames[] = {
1686 [NMK_GPIO_PULL_NONE] = "none",
1687 [NMK_GPIO_PULL_UP] = "up",
1688 [NMK_GPIO_PULL_DOWN] = "down",
1689 [3] /* illegal */ = "??"
1690 };
1691 static const char *slpmnames[] = {
1692 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1693 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1694 };
1695 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1696 struct nmk_gpio_chip *nmk_chip;
1697 struct pinctrl_gpio_range *range;
1698 struct gpio_chip *chip;
1699 unsigned bit;
1700
1701 /*
1702 * The pin config contains pin number and altfunction fields, here
1703 * we just ignore that part. It's being handled by the framework and
1704 * pinmux callback respectively.
1705 */
1706 pin_cfg_t cfg = (pin_cfg_t) config;
1707 int pull = PIN_PULL(cfg);
1708 int slpm = PIN_SLPM(cfg);
1709 int output = PIN_DIR(cfg);
1710 int val = PIN_VAL(cfg);
1711 bool lowemi = PIN_LOWEMI(cfg);
1712 bool gpiomode = PIN_GPIOMODE(cfg);
1713 bool sleep = PIN_SLEEPMODE(cfg);
1714
1715 range = nmk_match_gpio_range(pctldev, pin);
1716 if (!range) {
1717 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1718 return -EINVAL;
1719 }
1720 if (!range->gc) {
1721 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1722 pin);
1723 return -EINVAL;
1724 }
1725 chip = range->gc;
1726 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1727
1728 if (sleep) {
1729 int slpm_pull = PIN_SLPM_PULL(cfg);
1730 int slpm_output = PIN_SLPM_DIR(cfg);
1731 int slpm_val = PIN_SLPM_VAL(cfg);
1732
1733 /* All pins go into GPIO mode at sleep */
1734 gpiomode = true;
1735
1736 /*
1737 * The SLPM_* values are normal values + 1 to allow zero to
1738 * mean "same as normal".
1739 */
1740 if (slpm_pull)
1741 pull = slpm_pull - 1;
1742 if (slpm_output)
1743 output = slpm_output - 1;
1744 if (slpm_val)
1745 val = slpm_val - 1;
1746
1747 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1748 pin,
1749 slpm_pull ? pullnames[pull] : "same",
1750 slpm_output ? (output ? "output" : "input") : "same",
1751 slpm_val ? (val ? "high" : "low") : "same");
1752 }
1753
1754 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1755 pin, cfg, pullnames[pull], slpmnames[slpm],
1756 output ? "output " : "input",
1757 output ? (val ? "high" : "low") : "",
1758 lowemi ? "on" : "off" );
1759
1760 clk_enable(nmk_chip->clk);
1761 bit = pin % NMK_GPIO_PER_CHIP;
1762 if (gpiomode)
1763 /* No glitch when going to GPIO mode */
1764 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1765 if (output)
1766 __nmk_gpio_make_output(nmk_chip, bit, val);
1767 else {
1768 __nmk_gpio_make_input(nmk_chip, bit);
1769 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1770 }
1771 /* TODO: isn't this only applicable on output pins? */
1772 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1773
1774 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1775 clk_disable(nmk_chip->clk);
1776 return 0;
1777}
1778
1779static struct pinconf_ops nmk_pinconf_ops = {
1780 .pin_config_get = nmk_pin_config_get,
1781 .pin_config_set = nmk_pin_config_set,
1782};
1783
Linus Walleije98ea772012-04-26 23:57:25 +02001784static struct pinctrl_desc nmk_pinctrl_desc = {
1785 .name = "pinctrl-nomadik",
1786 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001787 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001788 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001789 .owner = THIS_MODULE,
1790};
1791
Lee Jones855f80c2012-05-26 06:09:29 +01001792static const struct of_device_id nmk_pinctrl_match[] = {
1793 {
1794 .compatible = "stericsson,nmk_pinctrl",
1795 .data = (void *)PINCTRL_NMK_DB8500,
1796 },
1797 {},
1798};
1799
Linus Walleije98ea772012-04-26 23:57:25 +02001800static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1801{
1802 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001803 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001804 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01001805 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001806 int i;
1807
1808 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1809 if (!npct)
1810 return -ENOMEM;
1811
Lee Jones855f80c2012-05-26 06:09:29 +01001812 if (platid)
1813 version = platid->driver_data;
1814 else if (np)
1815 version = (unsigned int)
1816 of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
1817
Linus Walleije98ea772012-04-26 23:57:25 +02001818 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001819 if (version == PINCTRL_NMK_STN8815)
1820 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001821 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001822 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001823 if (version == PINCTRL_NMK_DB8540)
1824 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001825
1826 /*
1827 * We need all the GPIO drivers to probe FIRST, or we will not be able
1828 * to obtain references to the struct gpio_chip * for them, and we
1829 * need this to proceed.
1830 */
1831 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1832 if (!nmk_gpio_chips[i]) {
1833 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001834 return -EPROBE_DEFER;
1835 }
1836 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1837 }
1838
1839 nmk_pinctrl_desc.pins = npct->soc->pins;
1840 nmk_pinctrl_desc.npins = npct->soc->npins;
1841 npct->dev = &pdev->dev;
1842 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1843 if (!npct->pctl) {
1844 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1845 return -EINVAL;
1846 }
1847
1848 /* We will handle a range of GPIO pins */
1849 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1850 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1851
1852 platform_set_drvdata(pdev, npct);
1853 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1854
1855 return 0;
1856}
1857
Lee Jones513c27f2012-04-13 15:05:05 +01001858static const struct of_device_id nmk_gpio_match[] = {
1859 { .compatible = "st,nomadik-gpio", },
1860 {}
1861};
1862
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001863static struct platform_driver nmk_gpio_driver = {
1864 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001865 .owner = THIS_MODULE,
1866 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001867 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301868 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001869 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001870};
1871
Linus Walleije98ea772012-04-26 23:57:25 +02001872static const struct platform_device_id nmk_pinctrl_id[] = {
1873 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1874 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001875 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Linus Walleije98ea772012-04-26 23:57:25 +02001876};
1877
1878static struct platform_driver nmk_pinctrl_driver = {
1879 .driver = {
1880 .owner = THIS_MODULE,
1881 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001882 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001883 },
1884 .probe = nmk_pinctrl_probe,
1885 .id_table = nmk_pinctrl_id,
1886};
1887
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001888static int __init nmk_gpio_init(void)
1889{
Linus Walleije98ea772012-04-26 23:57:25 +02001890 int ret;
1891
1892 ret = platform_driver_register(&nmk_gpio_driver);
1893 if (ret)
1894 return ret;
1895 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001896}
1897
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001898core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001899
1900MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1901MODULE_DESCRIPTION("Nomadik GPIO Driver");
1902MODULE_LICENSE("GPL");