blob: 7d5484fee5f3441082ac15db0fb8f6deba2a17e5 [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>
Linus Walleije98ea772012-04-26 23:57:25 +020027#include <linux/pinctrl/pinctrl.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010028
Will Deaconadfed152011-02-28 10:12:29 +000029#include <asm/mach/irq.h>
30
Rabin Vincent378be062010-06-02 06:06:29 +010031#include <plat/pincfg.h>
Linus Walleij0f332862011-08-22 08:33:30 +010032#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010033
Linus Walleije98ea772012-04-26 23:57:25 +020034#include "pinctrl-nomadik.h"
35
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010036/*
37 * The GPIO module in the Nomadik family of Systems-on-Chip is an
38 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020039 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010040 *
41 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
42 */
43
Rabin Vincent01727e62010-12-13 12:02:40 +053044#define NMK_GPIO_PER_CHIP 32
45
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010046struct nmk_gpio_chip {
47 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010048 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010049 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010050 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053051 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010052 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053053 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053054 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053055 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010056 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020057 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010058 /* Keep track of configured edges */
59 u32 edge_rising;
60 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053061 u32 real_wake;
62 u32 rwimsc;
63 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053064 u32 rimsc;
65 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020066 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053067 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010068};
69
Linus Walleije98ea772012-04-26 23:57:25 +020070struct nmk_pinctrl {
71 struct device *dev;
72 struct pinctrl_dev *pctl;
73 const struct nmk_pinctrl_soc_data *soc;
74};
75
Rabin Vincent01727e62010-12-13 12:02:40 +053076static struct nmk_gpio_chip *
77nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
78
79static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
80
81#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
82
Rabin Vincent6f9a9742010-06-02 05:50:28 +010083static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
84 unsigned offset, int gpio_mode)
85{
86 u32 bit = 1 << offset;
87 u32 afunc, bfunc;
88
89 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
90 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
91 if (gpio_mode & NMK_GPIO_ALT_A)
92 afunc |= bit;
93 if (gpio_mode & NMK_GPIO_ALT_B)
94 bfunc |= bit;
95 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
96 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
97}
98
Rabin Vincent81a3c292010-05-27 12:39:23 +010099static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
100 unsigned offset, enum nmk_gpio_slpm mode)
101{
102 u32 bit = 1 << offset;
103 u32 slpm;
104
105 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
106 if (mode == NMK_GPIO_SLPM_NOCHANGE)
107 slpm |= bit;
108 else
109 slpm &= ~bit;
110 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
111}
112
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100113static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
114 unsigned offset, enum nmk_gpio_pull pull)
115{
116 u32 bit = 1 << offset;
117 u32 pdis;
118
119 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200120 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100121 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200122 nmk_chip->pull_up &= ~bit;
123 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100124 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200125 }
126
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100127 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
128
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200129 if (pull == NMK_GPIO_PULL_UP) {
130 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100131 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200132 } else if (pull == NMK_GPIO_PULL_DOWN) {
133 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100134 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200135 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100136}
137
Rabin Vincentebc61782011-09-28 15:49:11 +0530138static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
139 unsigned offset, bool lowemi)
140{
141 u32 bit = BIT(offset);
142 bool enabled = nmk_chip->lowemi & bit;
143
144 if (lowemi == enabled)
145 return;
146
147 if (lowemi)
148 nmk_chip->lowemi |= bit;
149 else
150 nmk_chip->lowemi &= ~bit;
151
152 writel_relaxed(nmk_chip->lowemi,
153 nmk_chip->addr + NMK_GPIO_LOWEMI);
154}
155
Rabin Vincent378be062010-06-02 06:06:29 +0100156static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
157 unsigned offset)
158{
159 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
160}
161
Rabin Vincent6720db72010-09-02 11:28:48 +0100162static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
163 unsigned offset, int val)
164{
165 if (val)
166 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
167 else
168 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
169}
170
171static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
172 unsigned offset, int val)
173{
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
175 __nmk_gpio_set_output(nmk_chip, offset, val);
176}
177
Rabin Vincent01727e62010-12-13 12:02:40 +0530178static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
179 unsigned offset, int gpio_mode,
180 bool glitch)
181{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530182 u32 rwimsc = nmk_chip->rwimsc;
183 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530184
185 if (glitch && nmk_chip->set_ioforce) {
186 u32 bit = BIT(offset);
187
Rabin Vincent01727e62010-12-13 12:02:40 +0530188 /* Prevent spurious wakeups */
189 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
190 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
191
192 nmk_chip->set_ioforce(true);
193 }
194
195 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
196
197 if (glitch && nmk_chip->set_ioforce) {
198 nmk_chip->set_ioforce(false);
199
200 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
201 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
202 }
203}
204
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530205static void
206nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
207{
208 u32 falling = nmk_chip->fimsc & BIT(offset);
209 u32 rising = nmk_chip->rimsc & BIT(offset);
210 int gpio = nmk_chip->chip.base + offset;
211 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
212 struct irq_data *d = irq_get_irq_data(irq);
213
214 if (!rising && !falling)
215 return;
216
217 if (!d || !irqd_irq_disabled(d))
218 return;
219
220 if (rising) {
221 nmk_chip->rimsc &= ~BIT(offset);
222 writel_relaxed(nmk_chip->rimsc,
223 nmk_chip->addr + NMK_GPIO_RIMSC);
224 }
225
226 if (falling) {
227 nmk_chip->fimsc &= ~BIT(offset);
228 writel_relaxed(nmk_chip->fimsc,
229 nmk_chip->addr + NMK_GPIO_FIMSC);
230 }
231
232 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
233}
234
Rabin Vincent378be062010-06-02 06:06:29 +0100235static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530236 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100237{
238 static const char *afnames[] = {
239 [NMK_GPIO_ALT_GPIO] = "GPIO",
240 [NMK_GPIO_ALT_A] = "A",
241 [NMK_GPIO_ALT_B] = "B",
242 [NMK_GPIO_ALT_C] = "C"
243 };
244 static const char *pullnames[] = {
245 [NMK_GPIO_PULL_NONE] = "none",
246 [NMK_GPIO_PULL_UP] = "up",
247 [NMK_GPIO_PULL_DOWN] = "down",
248 [3] /* illegal */ = "??"
249 };
250 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100251 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
252 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100253 };
254
255 int pin = PIN_NUM(cfg);
256 int pull = PIN_PULL(cfg);
257 int af = PIN_ALT(cfg);
258 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100259 int output = PIN_DIR(cfg);
260 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530261 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100262
Rabin Vincentdacdc962010-12-03 20:35:37 +0530263 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
264 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100265 output ? "output " : "input",
266 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100267
Rabin Vincentdacdc962010-12-03 20:35:37 +0530268 if (sleep) {
269 int slpm_pull = PIN_SLPM_PULL(cfg);
270 int slpm_output = PIN_SLPM_DIR(cfg);
271 int slpm_val = PIN_SLPM_VAL(cfg);
272
Rabin Vincent3546d152010-11-25 11:38:27 +0530273 af = NMK_GPIO_ALT_GPIO;
274
Rabin Vincentdacdc962010-12-03 20:35:37 +0530275 /*
276 * The SLPM_* values are normal values + 1 to allow zero to
277 * mean "same as normal".
278 */
279 if (slpm_pull)
280 pull = slpm_pull - 1;
281 if (slpm_output)
282 output = slpm_output - 1;
283 if (slpm_val)
284 val = slpm_val - 1;
285
286 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
287 pin,
288 slpm_pull ? pullnames[pull] : "same",
289 slpm_output ? (output ? "output" : "input") : "same",
290 slpm_val ? (val ? "high" : "low") : "same");
291 }
292
Rabin Vincent6720db72010-09-02 11:28:48 +0100293 if (output)
294 __nmk_gpio_make_output(nmk_chip, offset, val);
295 else {
296 __nmk_gpio_make_input(nmk_chip, offset);
297 __nmk_gpio_set_pull(nmk_chip, offset, pull);
298 }
299
Rabin Vincentebc61782011-09-28 15:49:11 +0530300 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
301
Rabin Vincent01727e62010-12-13 12:02:40 +0530302 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530303 * If the pin is switching to altfunc, and there was an interrupt
304 * installed on it which has been lazy disabled, actually mask the
305 * interrupt to prevent spurious interrupts that would occur while the
306 * pin is under control of the peripheral. Only SKE does this.
307 */
308 if (af != NMK_GPIO_ALT_GPIO)
309 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
310
311 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530312 * If we've backed up the SLPM registers (glitch workaround), modify
313 * the backups since they will be restored.
314 */
315 if (slpmregs) {
316 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
317 slpmregs[nmk_chip->bank] |= BIT(offset);
318 else
319 slpmregs[nmk_chip->bank] &= ~BIT(offset);
320 } else
321 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
322
323 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
324}
325
326/*
327 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
328 * - Save SLPM registers
329 * - Set SLPM=0 for the IOs you want to switch and others to 1
330 * - Configure the GPIO registers for the IOs that are being switched
331 * - Set IOFORCE=1
332 * - Modify the AFLSA/B registers for the IOs that are being switched
333 * - Set IOFORCE=0
334 * - Restore SLPM registers
335 * - Any spurious wake up event during switch sequence to be ignored and
336 * cleared
337 */
338static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
339{
340 int i;
341
342 for (i = 0; i < NUM_BANKS; i++) {
343 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
344 unsigned int temp = slpm[i];
345
346 if (!chip)
347 break;
348
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200349 clk_enable(chip->clk);
350
Rabin Vincent01727e62010-12-13 12:02:40 +0530351 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
352 writel(temp, chip->addr + NMK_GPIO_SLPC);
353 }
354}
355
356static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
357{
358 int i;
359
360 for (i = 0; i < NUM_BANKS; i++) {
361 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
362
363 if (!chip)
364 break;
365
366 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200367
368 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530369 }
370}
371
372static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
373{
374 static unsigned int slpm[NUM_BANKS];
375 unsigned long flags;
376 bool glitch = false;
377 int ret = 0;
378 int i;
379
380 for (i = 0; i < num; i++) {
381 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
382 glitch = true;
383 break;
384 }
385 }
386
387 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
388
389 if (glitch) {
390 memset(slpm, 0xff, sizeof(slpm));
391
392 for (i = 0; i < num; i++) {
393 int pin = PIN_NUM(cfgs[i]);
394 int offset = pin % NMK_GPIO_PER_CHIP;
395
396 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
397 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
398 }
399
400 nmk_gpio_glitch_slpm_init(slpm);
401 }
402
403 for (i = 0; i < num; i++) {
404 struct nmk_gpio_chip *nmk_chip;
405 int pin = PIN_NUM(cfgs[i]);
406
Lee Jonesa60b57e2012-04-19 21:36:31 +0100407 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530408 if (!nmk_chip) {
409 ret = -EINVAL;
410 break;
411 }
412
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200413 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530414 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100415 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530416 cfgs[i], sleep, glitch ? slpm : NULL);
417 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200418 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530419 }
420
421 if (glitch)
422 nmk_gpio_glitch_slpm_restore(slpm);
423
424 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
425
426 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100427}
428
429/**
430 * nmk_config_pin - configure a pin's mux attributes
431 * @cfg: pin confguration
432 *
433 * Configures a pin's mode (alternate function or GPIO), its pull up status,
434 * and its sleep mode based on the specified configuration. The @cfg is
435 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
436 * are constructed using, and can be further enhanced with, the macros in
437 * plat/pincfg.h.
438 *
439 * If a pin's mode is set to GPIO, it is configured as an input to avoid
440 * side-effects. The gpio can be manipulated later using standard GPIO API
441 * calls.
442 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530443int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100444{
Rabin Vincent01727e62010-12-13 12:02:40 +0530445 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100446}
447EXPORT_SYMBOL(nmk_config_pin);
448
449/**
450 * nmk_config_pins - configure several pins at once
451 * @cfgs: array of pin configurations
452 * @num: number of elments in the array
453 *
454 * Configures several pins using nmk_config_pin(). Refer to that function for
455 * further information.
456 */
457int nmk_config_pins(pin_cfg_t *cfgs, int num)
458{
Rabin Vincent01727e62010-12-13 12:02:40 +0530459 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100460}
461EXPORT_SYMBOL(nmk_config_pins);
462
Rabin Vincentdacdc962010-12-03 20:35:37 +0530463int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
464{
Rabin Vincent01727e62010-12-13 12:02:40 +0530465 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530466}
467EXPORT_SYMBOL(nmk_config_pins_sleep);
468
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100469/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100470 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
471 * @gpio: pin number
472 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
473 *
Linus Walleij33d78642011-06-09 11:08:47 +0200474 * This register is actually in the pinmux layer, not the GPIO block itself.
475 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
476 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
477 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
478 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
479 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
480 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100481 *
Linus Walleij33d78642011-06-09 11:08:47 +0200482 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
483 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
484 * entered) regardless of the altfunction selected. Also wake-up detection is
485 * ENABLED.
486 *
487 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
488 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
489 * (for altfunction GPIO) or respective on-chip peripherals (for other
490 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
491 *
492 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100493 */
494int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
495{
496 struct nmk_gpio_chip *nmk_chip;
497 unsigned long flags;
498
Lee Jonesa60b57e2012-04-19 21:36:31 +0100499 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100500 if (!nmk_chip)
501 return -EINVAL;
502
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200503 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530504 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
505 spin_lock(&nmk_chip->lock);
506
Lee Jonesa60b57e2012-04-19 21:36:31 +0100507 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530508
509 spin_unlock(&nmk_chip->lock);
510 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200511 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100512
513 return 0;
514}
515
516/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100517 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
518 * @gpio: pin number
519 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
520 *
521 * Enables/disables pull up/down on a specified pin. This only takes effect if
522 * the pin is configured as an input (either explicitly or by the alternate
523 * function).
524 *
525 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
526 * configured as an input. Otherwise, due to the way the controller registers
527 * work, this function will change the value output on the pin.
528 */
529int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
530{
531 struct nmk_gpio_chip *nmk_chip;
532 unsigned long flags;
533
Lee Jonesa60b57e2012-04-19 21:36:31 +0100534 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100535 if (!nmk_chip)
536 return -EINVAL;
537
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200538 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100539 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100540 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100541 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200542 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100543
544 return 0;
545}
546
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200548/**
549 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
550 * @gpio: pin number
551 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
552 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
553 *
554 * Sets the mode of the specified pin to one of the alternate functions or
555 * plain GPIO.
556 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100557int nmk_gpio_set_mode(int gpio, int gpio_mode)
558{
559 struct nmk_gpio_chip *nmk_chip;
560 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100561
Lee Jonesa60b57e2012-04-19 21:36:31 +0100562 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100563 if (!nmk_chip)
564 return -EINVAL;
565
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200566 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100567 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100568 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100569 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200570 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100571
572 return 0;
573}
574EXPORT_SYMBOL(nmk_gpio_set_mode);
575
576int nmk_gpio_get_mode(int gpio)
577{
578 struct nmk_gpio_chip *nmk_chip;
579 u32 afunc, bfunc, bit;
580
Lee Jonesa60b57e2012-04-19 21:36:31 +0100581 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100582 if (!nmk_chip)
583 return -EINVAL;
584
Lee Jonesa60b57e2012-04-19 21:36:31 +0100585 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100586
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200587 clk_enable(nmk_chip->clk);
588
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100589 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
590 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
591
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200592 clk_disable(nmk_chip->clk);
593
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100594 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
595}
596EXPORT_SYMBOL(nmk_gpio_get_mode);
597
598
599/* IRQ functions */
600static inline int nmk_gpio_get_bitmask(int gpio)
601{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100602 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100603}
604
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100605static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100606{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607 struct nmk_gpio_chip *nmk_chip;
608
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100609 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100610 if (!nmk_chip)
611 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200612
613 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100614 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200615 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100616}
617
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100618enum nmk_gpio_irq_type {
619 NORMAL,
620 WAKE,
621};
622
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100623static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100624 int gpio, enum nmk_gpio_irq_type which,
625 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100626{
627 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530628 u32 *rimscval;
629 u32 *fimscval;
630 u32 rimscreg;
631 u32 fimscreg;
632
633 if (which == NORMAL) {
634 rimscreg = NMK_GPIO_RIMSC;
635 fimscreg = NMK_GPIO_FIMSC;
636 rimscval = &nmk_chip->rimsc;
637 fimscval = &nmk_chip->fimsc;
638 } else {
639 rimscreg = NMK_GPIO_RWIMSC;
640 fimscreg = NMK_GPIO_FWIMSC;
641 rimscval = &nmk_chip->rwimsc;
642 fimscval = &nmk_chip->fwimsc;
643 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100644
645 /* we must individually set/clear the two edges */
646 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100647 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530648 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100649 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530650 *rimscval &= ~bitmask;
651 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100652 }
653 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100654 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530655 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100656 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530657 *fimscval &= ~bitmask;
658 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100659 }
660}
661
Rabin Vincentb9df4682011-02-10 11:45:58 +0530662static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
663 int gpio, bool on)
664{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530665 /*
666 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
667 * disabled, since setting SLPM to 1 increases power consumption, and
668 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
669 */
670 if (nmk_chip->sleepmode && on) {
Lee Jonesa60b57e2012-04-19 21:36:31 +0100671 __nmk_gpio_set_slpm(nmk_chip, gpio % nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530672 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200673 }
674
Rabin Vincentb9df4682011-02-10 11:45:58 +0530675 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
676}
677
678static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100679{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680 struct nmk_gpio_chip *nmk_chip;
681 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100682 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100683
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100684 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100685 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100686 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100687 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100688
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200689 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530690 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
691 spin_lock(&nmk_chip->lock);
692
Lee Jonesa60b57e2012-04-19 21:36:31 +0100693 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530694
695 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100696 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530697
698 spin_unlock(&nmk_chip->lock);
699 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200700 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100701
702 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100703}
704
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100705static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100706{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530707 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100708}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100709
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100710static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530712 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100713}
714
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100715static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100716{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100717 struct nmk_gpio_chip *nmk_chip;
718 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530719 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100720
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100721 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100722 if (!nmk_chip)
723 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100724 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100725
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200726 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530727 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
728 spin_lock(&nmk_chip->lock);
729
Linus Walleij479a0c72011-09-20 10:50:15 +0200730 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100731 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530732
733 if (on)
734 nmk_chip->real_wake |= bitmask;
735 else
736 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530737
738 spin_unlock(&nmk_chip->lock);
739 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200740 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100741
742 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743}
744
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100745static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100746{
Linus Walleij479a0c72011-09-20 10:50:15 +0200747 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200748 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100749 struct nmk_gpio_chip *nmk_chip;
750 unsigned long flags;
751 u32 bitmask;
752
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100753 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100754 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100755 if (!nmk_chip)
756 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100757 if (type & IRQ_TYPE_LEVEL_HIGH)
758 return -EINVAL;
759 if (type & IRQ_TYPE_LEVEL_LOW)
760 return -EINVAL;
761
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200762 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100763 spin_lock_irqsave(&nmk_chip->lock, flags);
764
Rabin Vincent7a852d82010-05-06 10:43:55 +0100765 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100766 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100767
Rabin Vincentb9df4682011-02-10 11:45:58 +0530768 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100769 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100770
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100771 nmk_chip->edge_rising &= ~bitmask;
772 if (type & IRQ_TYPE_EDGE_RISING)
773 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100774
775 nmk_chip->edge_falling &= ~bitmask;
776 if (type & IRQ_TYPE_EDGE_FALLING)
777 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100778
779 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100780 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100781
Rabin Vincentb9df4682011-02-10 11:45:58 +0530782 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100783 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784
785 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200786 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100787
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788 return 0;
789}
790
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200791static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
792{
793 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
794
795 clk_enable(nmk_chip->clk);
796 nmk_gpio_irq_unmask(d);
797 return 0;
798}
799
800static void nmk_gpio_irq_shutdown(struct irq_data *d)
801{
802 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
803
804 nmk_gpio_irq_mask(d);
805 clk_disable(nmk_chip->clk);
806}
807
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100808static struct irq_chip nmk_gpio_irq_chip = {
809 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100810 .irq_ack = nmk_gpio_irq_ack,
811 .irq_mask = nmk_gpio_irq_mask,
812 .irq_unmask = nmk_gpio_irq_unmask,
813 .irq_set_type = nmk_gpio_irq_set_type,
814 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200815 .irq_startup = nmk_gpio_irq_startup,
816 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100817};
818
Rabin Vincent33b744b2010-10-14 10:38:03 +0530819static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
820 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100821{
822 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100823 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100824 unsigned int first_irq;
825
Will Deaconadfed152011-02-28 10:12:29 +0000826 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100827
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100828 nmk_chip = irq_get_handler_data(irq);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100829 first_irq = nmk_chip->domain->revmap_data.legacy.first_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530830 while (status) {
831 int bit = __ffs(status);
832
833 generic_handle_irq(first_irq + bit);
834 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100836
Will Deaconadfed152011-02-28 10:12:29 +0000837 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838}
839
Rabin Vincent33b744b2010-10-14 10:38:03 +0530840static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
841{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100842 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200843 u32 status;
844
845 clk_enable(nmk_chip->clk);
846 status = readl(nmk_chip->addr + NMK_GPIO_IS);
847 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530848
849 __nmk_gpio_irq_handler(irq, desc, status);
850}
851
852static void nmk_gpio_secondary_irq_handler(unsigned int irq,
853 struct irq_desc *desc)
854{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100855 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530856 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
857
858 __nmk_gpio_irq_handler(irq, desc, status);
859}
860
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100861static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
862{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100863 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
864 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530865
866 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100867 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530868 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100869 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530870 }
871
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100872 return 0;
873}
874
875/* I/O Functions */
876static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
877{
878 struct nmk_gpio_chip *nmk_chip =
879 container_of(chip, struct nmk_gpio_chip, chip);
880
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200881 clk_enable(nmk_chip->clk);
882
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100883 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200884
885 clk_disable(nmk_chip->clk);
886
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100887 return 0;
888}
889
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100890static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
891{
892 struct nmk_gpio_chip *nmk_chip =
893 container_of(chip, struct nmk_gpio_chip, chip);
894 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200895 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100896
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200897 clk_enable(nmk_chip->clk);
898
899 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
900
901 clk_disable(nmk_chip->clk);
902
903 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100904}
905
906static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
907 int val)
908{
909 struct nmk_gpio_chip *nmk_chip =
910 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100911
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200912 clk_enable(nmk_chip->clk);
913
Rabin Vincent6720db72010-09-02 11:28:48 +0100914 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200915
916 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100917}
918
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100919static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
920 int val)
921{
922 struct nmk_gpio_chip *nmk_chip =
923 container_of(chip, struct nmk_gpio_chip, chip);
924
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200925 clk_enable(nmk_chip->clk);
926
Rabin Vincent6720db72010-09-02 11:28:48 +0100927 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100928
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200929 clk_disable(nmk_chip->clk);
930
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100931 return 0;
932}
933
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100934static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
935{
936 struct nmk_gpio_chip *nmk_chip =
937 container_of(chip, struct nmk_gpio_chip, chip);
938
Lee Jonesa60b57e2012-04-19 21:36:31 +0100939 return irq_find_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100940}
941
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530942#ifdef CONFIG_DEBUG_FS
943
944#include <linux/seq_file.h>
945
946static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
947{
948 int mode;
949 unsigned i;
950 unsigned gpio = chip->base;
951 int is_out;
952 struct nmk_gpio_chip *nmk_chip =
953 container_of(chip, struct nmk_gpio_chip, chip);
954 const char *modes[] = {
955 [NMK_GPIO_ALT_GPIO] = "gpio",
956 [NMK_GPIO_ALT_A] = "altA",
957 [NMK_GPIO_ALT_B] = "altB",
958 [NMK_GPIO_ALT_C] = "altC",
959 };
960
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200961 clk_enable(nmk_chip->clk);
962
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530963 for (i = 0; i < chip->ngpio; i++, gpio++) {
964 const char *label = gpiochip_is_requested(chip, i);
965 bool pull;
966 u32 bit = 1 << i;
967
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530968 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
969 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
970 mode = nmk_gpio_get_mode(gpio);
971 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200972 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530973 is_out ? "out" : "in ",
974 chip->get
975 ? (chip->get(chip, i) ? "hi" : "lo")
976 : "? ",
977 (mode < 0) ? "unknown" : modes[mode],
978 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200979
980 if (label && !is_out) {
981 int irq = gpio_to_irq(gpio);
982 struct irq_desc *desc = irq_to_desc(irq);
983
984 /* This races with request_irq(), set_irq_type(),
985 * and set_irq_wake() ... but those are "rare".
986 */
987 if (irq >= 0 && desc->action) {
988 char *trigger;
989 u32 bitmask = nmk_gpio_get_bitmask(gpio);
990
991 if (nmk_chip->edge_rising & bitmask)
992 trigger = "edge-rising";
993 else if (nmk_chip->edge_falling & bitmask)
994 trigger = "edge-falling";
995 else
996 trigger = "edge-undefined";
997
998 seq_printf(s, " irq-%d %s%s",
999 irq, trigger,
1000 irqd_is_wakeup_set(&desc->irq_data)
1001 ? " wakeup" : "");
1002 }
1003 }
1004
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301005 seq_printf(s, "\n");
1006 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001007
1008 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301009}
1010
1011#else
1012#define nmk_gpio_dbg_show NULL
1013#endif
1014
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001015/* This structure is replicated for each GPIO block allocated at probe time */
1016static struct gpio_chip nmk_gpio_template = {
1017 .direction_input = nmk_gpio_make_input,
1018 .get = nmk_gpio_get_input,
1019 .direction_output = nmk_gpio_make_output,
1020 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001021 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301022 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001023 .can_sleep = 0,
1024};
1025
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001026void nmk_gpio_clocks_enable(void)
1027{
1028 int i;
1029
1030 for (i = 0; i < NUM_BANKS; i++) {
1031 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1032
1033 if (!chip)
1034 continue;
1035
1036 clk_enable(chip->clk);
1037 }
1038}
1039
1040void nmk_gpio_clocks_disable(void)
1041{
1042 int i;
1043
1044 for (i = 0; i < NUM_BANKS; i++) {
1045 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1046
1047 if (!chip)
1048 continue;
1049
1050 clk_disable(chip->clk);
1051 }
1052}
1053
Rabin Vincentb9df4682011-02-10 11:45:58 +05301054/*
1055 * Called from the suspend/resume path to only keep the real wakeup interrupts
1056 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1057 * and not the rest of the interrupts which we needed to have as wakeups for
1058 * cpuidle.
1059 *
1060 * PM ops are not used since this needs to be done at the end, after all the
1061 * other drivers are done with their suspend callbacks.
1062 */
1063void nmk_gpio_wakeups_suspend(void)
1064{
1065 int i;
1066
1067 for (i = 0; i < NUM_BANKS; i++) {
1068 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1069
1070 if (!chip)
1071 break;
1072
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001073 clk_enable(chip->clk);
1074
Rabin Vincentb9df4682011-02-10 11:45:58 +05301075 writel(chip->rwimsc & chip->real_wake,
1076 chip->addr + NMK_GPIO_RWIMSC);
1077 writel(chip->fwimsc & chip->real_wake,
1078 chip->addr + NMK_GPIO_FWIMSC);
1079
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001080 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301081 }
1082}
1083
1084void nmk_gpio_wakeups_resume(void)
1085{
1086 int i;
1087
1088 for (i = 0; i < NUM_BANKS; i++) {
1089 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1090
1091 if (!chip)
1092 break;
1093
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001094 clk_enable(chip->clk);
1095
Rabin Vincentb9df4682011-02-10 11:45:58 +05301096 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1097 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1098
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001099 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301100 }
1101}
1102
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001103/*
1104 * Read the pull up/pull down status.
1105 * A bit set in 'pull_up' means that pull up
1106 * is selected if pull is enabled in PDIS register.
1107 * Note: only pull up/down set via this driver can
1108 * be detected due to HW limitations.
1109 */
1110void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1111{
1112 if (gpio_bank < NUM_BANKS) {
1113 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1114
1115 if (!chip)
1116 return;
1117
1118 *pull_up = chip->pull_up;
1119 }
1120}
1121
Lee Jonesa60b57e2012-04-19 21:36:31 +01001122int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1123 irq_hw_number_t hwirq)
1124{
1125 struct nmk_gpio_chip *nmk_chip = d->host_data;
1126
1127 if (!nmk_chip)
1128 return -EINVAL;
1129
1130 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1131 set_irq_flags(irq, IRQF_VALID);
1132 irq_set_chip_data(irq, nmk_chip);
1133 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1134
1135 return 0;
1136}
1137
1138const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1139 .map = nmk_gpio_irq_map,
1140 .xlate = irq_domain_xlate_twocell,
1141};
1142
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001143static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001144{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001145 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001146 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001147 struct nmk_gpio_chip *nmk_chip;
1148 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001149 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001150 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301151 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001152 void __iomem *base;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001153 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001154 int ret;
1155
Lee Jones513c27f2012-04-13 15:05:05 +01001156 if (!pdata && !np) {
1157 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001158 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001159 }
1160
1161 if (np) {
1162 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1163 if (!pdata)
1164 return -ENOMEM;
1165
1166 if (of_get_property(np, "supports-sleepmode", NULL))
1167 pdata->supports_sleepmode = true;
1168
1169 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1170 dev_err(&dev->dev, "gpio-bank property not found\n");
1171 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001172 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001173 }
1174
1175 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1176 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1177 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001178
1179 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1180 if (!res) {
1181 ret = -ENOENT;
1182 goto out;
1183 }
1184
1185 irq = platform_get_irq(dev, 0);
1186 if (irq < 0) {
1187 ret = irq;
1188 goto out;
1189 }
1190
Rabin Vincent33b744b2010-10-14 10:38:03 +05301191 secondary_irq = platform_get_irq(dev, 1);
1192 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1193 ret = -EINVAL;
1194 goto out;
1195 }
1196
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001197 if (request_mem_region(res->start, resource_size(res),
1198 dev_name(&dev->dev)) == NULL) {
1199 ret = -EBUSY;
1200 goto out;
1201 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001202
Linus Walleij8d917712012-04-17 10:15:54 +02001203 base = ioremap(res->start, resource_size(res));
1204 if (!base) {
1205 ret = -ENOMEM;
1206 goto out_release;
1207 }
1208
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001209 clk = clk_get(&dev->dev, NULL);
1210 if (IS_ERR(clk)) {
1211 ret = PTR_ERR(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001212 goto out_unmap;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001213 }
1214
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001215 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1216 if (!nmk_chip) {
1217 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001218 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001219 }
Lee Jones513c27f2012-04-13 15:05:05 +01001220
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001221 /*
1222 * The virt address in nmk_chip->addr is in the nomadik register space,
1223 * so we can simply convert the resource address, without remapping
1224 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301225 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001226 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001227 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001228 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001229 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301230 nmk_chip->secondary_parent_irq = secondary_irq;
1231 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301232 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001233 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001234 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001235
1236 chip = &nmk_chip->chip;
1237 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301238 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301239 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001240 chip->dev = &dev->dev;
1241 chip->owner = THIS_MODULE;
1242
Rabin Vincentebc61782011-09-28 15:49:11 +05301243 clk_enable(nmk_chip->clk);
1244 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1245 clk_disable(nmk_chip->clk);
1246
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001247#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001248 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001249#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001250
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001251 ret = gpiochip_add(&nmk_chip->chip);
1252 if (ret)
1253 goto out_free;
1254
Rabin Vincent01727e62010-12-13 12:02:40 +05301255 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1256
1257 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001258
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001259 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001260
Lee Jonesa60b57e2012-04-19 21:36:31 +01001261 nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
1262 NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
1263 0, &nmk_gpio_irq_simple_ops, nmk_chip);
1264 if (!nmk_chip->domain) {
1265 pr_err("%s: Failed to create irqdomain\n", np->full_name);
1266 ret = -ENOSYS;
1267 goto out_free;
1268 }
1269
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001270 nmk_gpio_init_irq(nmk_chip);
1271
Lee Jones513c27f2012-04-13 15:05:05 +01001272 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1273
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001274 return 0;
1275
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001276out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001277 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001278out_clk:
1279 clk_disable(clk);
1280 clk_put(clk);
Linus Walleij8d917712012-04-17 10:15:54 +02001281out_unmap:
1282 iounmap(base);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001283out_release:
1284 release_mem_region(res->start, resource_size(res));
1285out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001286 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1287 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001288 if (np)
1289 kfree(pdata);
1290
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001291 return ret;
1292}
1293
Linus Walleije98ea772012-04-26 23:57:25 +02001294static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1295{
1296 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1297
1298 return npct->soc->ngroups;
1299}
1300
1301static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1302 unsigned selector)
1303{
1304 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1305
1306 return npct->soc->groups[selector].name;
1307}
1308
1309static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1310 const unsigned **pins,
1311 unsigned *num_pins)
1312{
1313 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1314
1315 *pins = npct->soc->groups[selector].pins;
1316 *num_pins = npct->soc->groups[selector].npins;
1317 return 0;
1318}
1319
1320static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1321 unsigned offset)
1322{
1323 seq_printf(s, " Nomadik GPIO");
1324}
1325
1326static struct pinctrl_ops nmk_pinctrl_ops = {
1327 .get_groups_count = nmk_get_groups_cnt,
1328 .get_group_name = nmk_get_group_name,
1329 .get_group_pins = nmk_get_group_pins,
1330 .pin_dbg_show = nmk_pin_dbg_show,
1331};
1332
1333static struct pinctrl_desc nmk_pinctrl_desc = {
1334 .name = "pinctrl-nomadik",
1335 .pctlops = &nmk_pinctrl_ops,
1336 .owner = THIS_MODULE,
1337};
1338
1339static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
1340{
1341 const struct platform_device_id *platid = platform_get_device_id(pdev);
1342 struct nmk_pinctrl *npct;
1343 int i;
1344
1345 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1346 if (!npct)
1347 return -ENOMEM;
1348
1349 /* Poke in other ASIC variants here */
1350 if (platid->driver_data == PINCTRL_NMK_DB8500)
1351 nmk_pinctrl_db8500_init(&npct->soc);
1352
1353 /*
1354 * We need all the GPIO drivers to probe FIRST, or we will not be able
1355 * to obtain references to the struct gpio_chip * for them, and we
1356 * need this to proceed.
1357 */
1358 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1359 if (!nmk_gpio_chips[i]) {
1360 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
1361 devm_kfree(&pdev->dev, npct);
1362 return -EPROBE_DEFER;
1363 }
1364 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
1365 }
1366
1367 nmk_pinctrl_desc.pins = npct->soc->pins;
1368 nmk_pinctrl_desc.npins = npct->soc->npins;
1369 npct->dev = &pdev->dev;
1370 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1371 if (!npct->pctl) {
1372 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1373 return -EINVAL;
1374 }
1375
1376 /* We will handle a range of GPIO pins */
1377 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1378 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1379
1380 platform_set_drvdata(pdev, npct);
1381 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1382
1383 return 0;
1384}
1385
Lee Jones513c27f2012-04-13 15:05:05 +01001386static const struct of_device_id nmk_gpio_match[] = {
1387 { .compatible = "st,nomadik-gpio", },
1388 {}
1389};
1390
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001391static struct platform_driver nmk_gpio_driver = {
1392 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001393 .owner = THIS_MODULE,
1394 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001395 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301396 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001397 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001398};
1399
Linus Walleije98ea772012-04-26 23:57:25 +02001400static const struct platform_device_id nmk_pinctrl_id[] = {
1401 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1402 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
1403};
1404
1405static struct platform_driver nmk_pinctrl_driver = {
1406 .driver = {
1407 .owner = THIS_MODULE,
1408 .name = "pinctrl-nomadik",
1409 },
1410 .probe = nmk_pinctrl_probe,
1411 .id_table = nmk_pinctrl_id,
1412};
1413
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001414static int __init nmk_gpio_init(void)
1415{
Linus Walleije98ea772012-04-26 23:57:25 +02001416 int ret;
1417
1418 ret = platform_driver_register(&nmk_gpio_driver);
1419 if (ret)
1420 return ret;
1421 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001422}
1423
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001424core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001425
1426MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1427MODULE_DESCRIPTION("Nomadik GPIO Driver");
1428MODULE_LICENSE("GPL");