blob: 97e9be94141be19c3b8a370787bb090675cb0a51 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010026
Will Deaconadfed152011-02-28 10:12:29 +000027#include <asm/mach/irq.h>
28
Rabin Vincent378be062010-06-02 06:06:29 +010029#include <plat/pincfg.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010030#include <mach/hardware.h>
31#include <mach/gpio.h>
32
33/*
34 * The GPIO module in the Nomadik family of Systems-on-Chip is an
35 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020036 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010037 *
38 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
39 */
40
Rabin Vincent01727e62010-12-13 12:02:40 +053041#define NMK_GPIO_PER_CHIP 32
42
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010043struct nmk_gpio_chip {
44 struct gpio_chip chip;
45 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010046 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053047 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010048 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053049 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053050 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053051 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010052 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020053 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010054 /* Keep track of configured edges */
55 u32 edge_rising;
56 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053057 u32 real_wake;
58 u32 rwimsc;
59 u32 fwimsc;
60 u32 slpm;
Thomas Gleixnerd1118f62011-03-24 12:38:50 +010061 u32 enabled;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020062 u32 pull_up;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010063};
64
Rabin Vincent01727e62010-12-13 12:02:40 +053065static struct nmk_gpio_chip *
66nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
67
68static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
69
70#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
71
Rabin Vincent6f9a9742010-06-02 05:50:28 +010072static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
73 unsigned offset, int gpio_mode)
74{
75 u32 bit = 1 << offset;
76 u32 afunc, bfunc;
77
78 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
79 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
80 if (gpio_mode & NMK_GPIO_ALT_A)
81 afunc |= bit;
82 if (gpio_mode & NMK_GPIO_ALT_B)
83 bfunc |= bit;
84 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
85 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
86}
87
Rabin Vincent81a3c292010-05-27 12:39:23 +010088static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
89 unsigned offset, enum nmk_gpio_slpm mode)
90{
91 u32 bit = 1 << offset;
92 u32 slpm;
93
94 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
95 if (mode == NMK_GPIO_SLPM_NOCHANGE)
96 slpm |= bit;
97 else
98 slpm &= ~bit;
99 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
100}
101
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100102static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
103 unsigned offset, enum nmk_gpio_pull pull)
104{
105 u32 bit = 1 << offset;
106 u32 pdis;
107
108 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200109 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100110 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200111 nmk_chip->pull_up &= ~bit;
112 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100113 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200114 }
115
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100116 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
117
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200118 if (pull == NMK_GPIO_PULL_UP) {
119 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100120 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200121 } else if (pull == NMK_GPIO_PULL_DOWN) {
122 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100123 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200124 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100125}
126
Rabin Vincent378be062010-06-02 06:06:29 +0100127static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
128 unsigned offset)
129{
130 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
131}
132
Rabin Vincent6720db72010-09-02 11:28:48 +0100133static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
134 unsigned offset, int val)
135{
136 if (val)
137 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
138 else
139 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
140}
141
142static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
143 unsigned offset, int val)
144{
145 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
146 __nmk_gpio_set_output(nmk_chip, offset, val);
147}
148
Rabin Vincent01727e62010-12-13 12:02:40 +0530149static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
150 unsigned offset, int gpio_mode,
151 bool glitch)
152{
Linus Walleij3c4bee02011-02-16 10:37:52 +0100153 u32 rwimsc = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
154 u32 fwimsc = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
Rabin Vincent01727e62010-12-13 12:02:40 +0530155
156 if (glitch && nmk_chip->set_ioforce) {
157 u32 bit = BIT(offset);
158
Rabin Vincent01727e62010-12-13 12:02:40 +0530159 /* Prevent spurious wakeups */
160 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
161 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
162
163 nmk_chip->set_ioforce(true);
164 }
165
166 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
167
168 if (glitch && nmk_chip->set_ioforce) {
169 nmk_chip->set_ioforce(false);
170
171 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
172 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
173 }
174}
175
Rabin Vincent378be062010-06-02 06:06:29 +0100176static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530177 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100178{
179 static const char *afnames[] = {
180 [NMK_GPIO_ALT_GPIO] = "GPIO",
181 [NMK_GPIO_ALT_A] = "A",
182 [NMK_GPIO_ALT_B] = "B",
183 [NMK_GPIO_ALT_C] = "C"
184 };
185 static const char *pullnames[] = {
186 [NMK_GPIO_PULL_NONE] = "none",
187 [NMK_GPIO_PULL_UP] = "up",
188 [NMK_GPIO_PULL_DOWN] = "down",
189 [3] /* illegal */ = "??"
190 };
191 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100192 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
193 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100194 };
195
196 int pin = PIN_NUM(cfg);
197 int pull = PIN_PULL(cfg);
198 int af = PIN_ALT(cfg);
199 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100200 int output = PIN_DIR(cfg);
201 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530202 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100203
Rabin Vincentdacdc962010-12-03 20:35:37 +0530204 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
205 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100206 output ? "output " : "input",
207 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100208
Rabin Vincentdacdc962010-12-03 20:35:37 +0530209 if (sleep) {
210 int slpm_pull = PIN_SLPM_PULL(cfg);
211 int slpm_output = PIN_SLPM_DIR(cfg);
212 int slpm_val = PIN_SLPM_VAL(cfg);
213
Rabin Vincent3546d152010-11-25 11:38:27 +0530214 af = NMK_GPIO_ALT_GPIO;
215
Rabin Vincentdacdc962010-12-03 20:35:37 +0530216 /*
217 * The SLPM_* values are normal values + 1 to allow zero to
218 * mean "same as normal".
219 */
220 if (slpm_pull)
221 pull = slpm_pull - 1;
222 if (slpm_output)
223 output = slpm_output - 1;
224 if (slpm_val)
225 val = slpm_val - 1;
226
227 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
228 pin,
229 slpm_pull ? pullnames[pull] : "same",
230 slpm_output ? (output ? "output" : "input") : "same",
231 slpm_val ? (val ? "high" : "low") : "same");
232 }
233
Rabin Vincent6720db72010-09-02 11:28:48 +0100234 if (output)
235 __nmk_gpio_make_output(nmk_chip, offset, val);
236 else {
237 __nmk_gpio_make_input(nmk_chip, offset);
238 __nmk_gpio_set_pull(nmk_chip, offset, pull);
239 }
240
Rabin Vincent01727e62010-12-13 12:02:40 +0530241 /*
242 * If we've backed up the SLPM registers (glitch workaround), modify
243 * the backups since they will be restored.
244 */
245 if (slpmregs) {
246 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
247 slpmregs[nmk_chip->bank] |= BIT(offset);
248 else
249 slpmregs[nmk_chip->bank] &= ~BIT(offset);
250 } else
251 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
252
253 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
254}
255
256/*
257 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
258 * - Save SLPM registers
259 * - Set SLPM=0 for the IOs you want to switch and others to 1
260 * - Configure the GPIO registers for the IOs that are being switched
261 * - Set IOFORCE=1
262 * - Modify the AFLSA/B registers for the IOs that are being switched
263 * - Set IOFORCE=0
264 * - Restore SLPM registers
265 * - Any spurious wake up event during switch sequence to be ignored and
266 * cleared
267 */
268static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
269{
270 int i;
271
272 for (i = 0; i < NUM_BANKS; i++) {
273 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
274 unsigned int temp = slpm[i];
275
276 if (!chip)
277 break;
278
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200279 clk_enable(chip->clk);
280
Rabin Vincent01727e62010-12-13 12:02:40 +0530281 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
282 writel(temp, chip->addr + NMK_GPIO_SLPC);
283 }
284}
285
286static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
287{
288 int i;
289
290 for (i = 0; i < NUM_BANKS; i++) {
291 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
292
293 if (!chip)
294 break;
295
296 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200297
298 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530299 }
300}
301
302static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
303{
304 static unsigned int slpm[NUM_BANKS];
305 unsigned long flags;
306 bool glitch = false;
307 int ret = 0;
308 int i;
309
310 for (i = 0; i < num; i++) {
311 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
312 glitch = true;
313 break;
314 }
315 }
316
317 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
318
319 if (glitch) {
320 memset(slpm, 0xff, sizeof(slpm));
321
322 for (i = 0; i < num; i++) {
323 int pin = PIN_NUM(cfgs[i]);
324 int offset = pin % NMK_GPIO_PER_CHIP;
325
326 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
327 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
328 }
329
330 nmk_gpio_glitch_slpm_init(slpm);
331 }
332
333 for (i = 0; i < num; i++) {
334 struct nmk_gpio_chip *nmk_chip;
335 int pin = PIN_NUM(cfgs[i]);
336
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100337 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530338 if (!nmk_chip) {
339 ret = -EINVAL;
340 break;
341 }
342
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200343 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530344 spin_lock(&nmk_chip->lock);
345 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
346 cfgs[i], sleep, glitch ? slpm : NULL);
347 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200348 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530349 }
350
351 if (glitch)
352 nmk_gpio_glitch_slpm_restore(slpm);
353
354 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
355
356 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100357}
358
359/**
360 * nmk_config_pin - configure a pin's mux attributes
361 * @cfg: pin confguration
362 *
363 * Configures a pin's mode (alternate function or GPIO), its pull up status,
364 * and its sleep mode based on the specified configuration. The @cfg is
365 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
366 * are constructed using, and can be further enhanced with, the macros in
367 * plat/pincfg.h.
368 *
369 * If a pin's mode is set to GPIO, it is configured as an input to avoid
370 * side-effects. The gpio can be manipulated later using standard GPIO API
371 * calls.
372 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530373int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100374{
Rabin Vincent01727e62010-12-13 12:02:40 +0530375 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100376}
377EXPORT_SYMBOL(nmk_config_pin);
378
379/**
380 * nmk_config_pins - configure several pins at once
381 * @cfgs: array of pin configurations
382 * @num: number of elments in the array
383 *
384 * Configures several pins using nmk_config_pin(). Refer to that function for
385 * further information.
386 */
387int nmk_config_pins(pin_cfg_t *cfgs, int num)
388{
Rabin Vincent01727e62010-12-13 12:02:40 +0530389 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100390}
391EXPORT_SYMBOL(nmk_config_pins);
392
Rabin Vincentdacdc962010-12-03 20:35:37 +0530393int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
394{
Rabin Vincent01727e62010-12-13 12:02:40 +0530395 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530396}
397EXPORT_SYMBOL(nmk_config_pins_sleep);
398
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100399/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100400 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
401 * @gpio: pin number
402 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
403 *
Linus Walleij33d78642011-06-09 11:08:47 +0200404 * This register is actually in the pinmux layer, not the GPIO block itself.
405 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
406 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
407 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
408 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
409 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
410 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100411 *
Linus Walleij33d78642011-06-09 11:08:47 +0200412 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
413 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
414 * entered) regardless of the altfunction selected. Also wake-up detection is
415 * ENABLED.
416 *
417 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
418 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
419 * (for altfunction GPIO) or respective on-chip peripherals (for other
420 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
421 *
422 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100423 */
424int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
425{
426 struct nmk_gpio_chip *nmk_chip;
427 unsigned long flags;
428
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100429 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100430 if (!nmk_chip)
431 return -EINVAL;
432
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200433 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530434 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
435 spin_lock(&nmk_chip->lock);
436
Rabin Vincent81a3c292010-05-27 12:39:23 +0100437 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530438
439 spin_unlock(&nmk_chip->lock);
440 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200441 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100442
443 return 0;
444}
445
446/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100447 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
448 * @gpio: pin number
449 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
450 *
451 * Enables/disables pull up/down on a specified pin. This only takes effect if
452 * the pin is configured as an input (either explicitly or by the alternate
453 * function).
454 *
455 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
456 * configured as an input. Otherwise, due to the way the controller registers
457 * work, this function will change the value output on the pin.
458 */
459int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
460{
461 struct nmk_gpio_chip *nmk_chip;
462 unsigned long flags;
463
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100464 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100465 if (!nmk_chip)
466 return -EINVAL;
467
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200468 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100469 spin_lock_irqsave(&nmk_chip->lock, flags);
470 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
471 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200472 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100473
474 return 0;
475}
476
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100477/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200478/**
479 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
480 * @gpio: pin number
481 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
482 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
483 *
484 * Sets the mode of the specified pin to one of the alternate functions or
485 * plain GPIO.
486 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100487int nmk_gpio_set_mode(int gpio, int gpio_mode)
488{
489 struct nmk_gpio_chip *nmk_chip;
490 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100491
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100492 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100493 if (!nmk_chip)
494 return -EINVAL;
495
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200496 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100497 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100498 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100499 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200500 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100501
502 return 0;
503}
504EXPORT_SYMBOL(nmk_gpio_set_mode);
505
506int nmk_gpio_get_mode(int gpio)
507{
508 struct nmk_gpio_chip *nmk_chip;
509 u32 afunc, bfunc, bit;
510
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100511 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100512 if (!nmk_chip)
513 return -EINVAL;
514
515 bit = 1 << (gpio - nmk_chip->chip.base);
516
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200517 clk_enable(nmk_chip->clk);
518
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100519 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
520 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
521
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200522 clk_disable(nmk_chip->clk);
523
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100524 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
525}
526EXPORT_SYMBOL(nmk_gpio_get_mode);
527
528
529/* IRQ functions */
530static inline int nmk_gpio_get_bitmask(int gpio)
531{
532 return 1 << (gpio % 32);
533}
534
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100535static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100536{
537 int gpio;
538 struct nmk_gpio_chip *nmk_chip;
539
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100540 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
541 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100542 if (!nmk_chip)
543 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200544
545 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100546 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200547 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100548}
549
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100550enum nmk_gpio_irq_type {
551 NORMAL,
552 WAKE,
553};
554
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100555static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100556 int gpio, enum nmk_gpio_irq_type which,
557 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100558{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100559 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
560 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100561 u32 bitmask = nmk_gpio_get_bitmask(gpio);
562 u32 reg;
563
564 /* we must individually set/clear the two edges */
565 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100566 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100567 if (enable)
568 reg |= bitmask;
569 else
570 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100571 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100572 }
573 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100574 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100575 if (enable)
576 reg |= bitmask;
577 else
578 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100579 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100580 }
581}
582
Rabin Vincentb9df4682011-02-10 11:45:58 +0530583static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
584 int gpio, bool on)
585{
Linus Walleij33d78642011-06-09 11:08:47 +0200586 if (nmk_chip->sleepmode) {
587 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
588 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
589 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
590 }
591
Rabin Vincentb9df4682011-02-10 11:45:58 +0530592 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
593}
594
595static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100596{
597 int gpio;
598 struct nmk_gpio_chip *nmk_chip;
599 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100600 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100601
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100602 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
603 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100604 bitmask = nmk_gpio_get_bitmask(gpio);
605 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100606 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607
Thomas Gleixnerd1118f62011-03-24 12:38:50 +0100608 if (enable)
609 nmk_chip->enabled |= bitmask;
610 else
611 nmk_chip->enabled &= ~bitmask;
612
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200613 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530614 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
615 spin_lock(&nmk_chip->lock);
616
617 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
618
619 if (!(nmk_chip->real_wake & bitmask))
620 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
621
622 spin_unlock(&nmk_chip->lock);
623 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200624 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100625
626 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100627}
628
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100629static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100630{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530631 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100632}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100633
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100634static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100635{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530636 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100637}
638
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100639static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100640{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100641 struct nmk_gpio_chip *nmk_chip;
642 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530643 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100644 int gpio;
645
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100646 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
647 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100648 if (!nmk_chip)
649 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530650 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100651
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200652 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530653 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
654 spin_lock(&nmk_chip->lock);
655
Thomas Gleixnerd1118f62011-03-24 12:38:50 +0100656 if (!(nmk_chip->enabled & bitmask))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530657 __nmk_gpio_set_wake(nmk_chip, gpio, on);
658
659 if (on)
660 nmk_chip->real_wake |= bitmask;
661 else
662 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530663
664 spin_unlock(&nmk_chip->lock);
665 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200666 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100667
668 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669}
670
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100671static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100672{
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200673 bool enabled;
674 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100675 int gpio;
676 struct nmk_gpio_chip *nmk_chip;
677 unsigned long flags;
678 u32 bitmask;
679
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100680 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
681 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100682 bitmask = nmk_gpio_get_bitmask(gpio);
683 if (!nmk_chip)
684 return -EINVAL;
685
686 if (type & IRQ_TYPE_LEVEL_HIGH)
687 return -EINVAL;
688 if (type & IRQ_TYPE_LEVEL_LOW)
689 return -EINVAL;
690
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200691 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100692 spin_lock_irqsave(&nmk_chip->lock, flags);
693
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200694 enabled = !!(nmk_chip->enabled & bitmask);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100695 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100696 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
697
Rabin Vincentb9df4682011-02-10 11:45:58 +0530698 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100699 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100700
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100701 nmk_chip->edge_rising &= ~bitmask;
702 if (type & IRQ_TYPE_EDGE_RISING)
703 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100704
705 nmk_chip->edge_falling &= ~bitmask;
706 if (type & IRQ_TYPE_EDGE_FALLING)
707 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100708
709 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100710 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
711
Rabin Vincentb9df4682011-02-10 11:45:58 +0530712 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100713 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100714
715 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200716 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100717
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100718 return 0;
719}
720
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200721static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
722{
723 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
724
725 clk_enable(nmk_chip->clk);
726 nmk_gpio_irq_unmask(d);
727 return 0;
728}
729
730static void nmk_gpio_irq_shutdown(struct irq_data *d)
731{
732 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
733
734 nmk_gpio_irq_mask(d);
735 clk_disable(nmk_chip->clk);
736}
737
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100738static struct irq_chip nmk_gpio_irq_chip = {
739 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100740 .irq_ack = nmk_gpio_irq_ack,
741 .irq_mask = nmk_gpio_irq_mask,
742 .irq_unmask = nmk_gpio_irq_unmask,
743 .irq_set_type = nmk_gpio_irq_set_type,
744 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200745 .irq_startup = nmk_gpio_irq_startup,
746 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100747};
748
Rabin Vincent33b744b2010-10-14 10:38:03 +0530749static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
750 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100751{
752 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100753 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100754 unsigned int first_irq;
755
Will Deaconadfed152011-02-28 10:12:29 +0000756 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100757
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100758 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100759 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530760 while (status) {
761 int bit = __ffs(status);
762
763 generic_handle_irq(first_irq + bit);
764 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100765 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100766
Will Deaconadfed152011-02-28 10:12:29 +0000767 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100768}
769
Rabin Vincent33b744b2010-10-14 10:38:03 +0530770static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
771{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100772 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200773 u32 status;
774
775 clk_enable(nmk_chip->clk);
776 status = readl(nmk_chip->addr + NMK_GPIO_IS);
777 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530778
779 __nmk_gpio_irq_handler(irq, desc, status);
780}
781
782static void nmk_gpio_secondary_irq_handler(unsigned int irq,
783 struct irq_desc *desc)
784{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100785 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530786 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
787
788 __nmk_gpio_irq_handler(irq, desc, status);
789}
790
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100791static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
792{
793 unsigned int first_irq;
794 int i;
795
796 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530797 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100798 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
799 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100800 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100801 irq_set_chip_data(i, nmk_chip);
802 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100803 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530804
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100805 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
806 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530807
808 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100809 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530810 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100811 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530812 }
813
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100814 return 0;
815}
816
817/* I/O Functions */
818static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
819{
820 struct nmk_gpio_chip *nmk_chip =
821 container_of(chip, struct nmk_gpio_chip, chip);
822
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200823 clk_enable(nmk_chip->clk);
824
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100825 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200826
827 clk_disable(nmk_chip->clk);
828
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100829 return 0;
830}
831
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100832static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
833{
834 struct nmk_gpio_chip *nmk_chip =
835 container_of(chip, struct nmk_gpio_chip, chip);
836 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200837 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200839 clk_enable(nmk_chip->clk);
840
841 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
842
843 clk_disable(nmk_chip->clk);
844
845 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100846}
847
848static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
849 int val)
850{
851 struct nmk_gpio_chip *nmk_chip =
852 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100853
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200854 clk_enable(nmk_chip->clk);
855
Rabin Vincent6720db72010-09-02 11:28:48 +0100856 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200857
858 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100859}
860
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100861static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
862 int val)
863{
864 struct nmk_gpio_chip *nmk_chip =
865 container_of(chip, struct nmk_gpio_chip, chip);
866
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200867 clk_enable(nmk_chip->clk);
868
Rabin Vincent6720db72010-09-02 11:28:48 +0100869 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100870
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200871 clk_disable(nmk_chip->clk);
872
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100873 return 0;
874}
875
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100876static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
877{
878 struct nmk_gpio_chip *nmk_chip =
879 container_of(chip, struct nmk_gpio_chip, chip);
880
881 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
882}
883
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530884#ifdef CONFIG_DEBUG_FS
885
886#include <linux/seq_file.h>
887
888static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
889{
890 int mode;
891 unsigned i;
892 unsigned gpio = chip->base;
893 int is_out;
894 struct nmk_gpio_chip *nmk_chip =
895 container_of(chip, struct nmk_gpio_chip, chip);
896 const char *modes[] = {
897 [NMK_GPIO_ALT_GPIO] = "gpio",
898 [NMK_GPIO_ALT_A] = "altA",
899 [NMK_GPIO_ALT_B] = "altB",
900 [NMK_GPIO_ALT_C] = "altC",
901 };
902
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200903 clk_enable(nmk_chip->clk);
904
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530905 for (i = 0; i < chip->ngpio; i++, gpio++) {
906 const char *label = gpiochip_is_requested(chip, i);
907 bool pull;
908 u32 bit = 1 << i;
909
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530910 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
911 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
912 mode = nmk_gpio_get_mode(gpio);
913 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200914 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530915 is_out ? "out" : "in ",
916 chip->get
917 ? (chip->get(chip, i) ? "hi" : "lo")
918 : "? ",
919 (mode < 0) ? "unknown" : modes[mode],
920 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200921
922 if (label && !is_out) {
923 int irq = gpio_to_irq(gpio);
924 struct irq_desc *desc = irq_to_desc(irq);
925
926 /* This races with request_irq(), set_irq_type(),
927 * and set_irq_wake() ... but those are "rare".
928 */
929 if (irq >= 0 && desc->action) {
930 char *trigger;
931 u32 bitmask = nmk_gpio_get_bitmask(gpio);
932
933 if (nmk_chip->edge_rising & bitmask)
934 trigger = "edge-rising";
935 else if (nmk_chip->edge_falling & bitmask)
936 trigger = "edge-falling";
937 else
938 trigger = "edge-undefined";
939
940 seq_printf(s, " irq-%d %s%s",
941 irq, trigger,
942 irqd_is_wakeup_set(&desc->irq_data)
943 ? " wakeup" : "");
944 }
945 }
946
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530947 seq_printf(s, "\n");
948 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200949
950 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530951}
952
953#else
954#define nmk_gpio_dbg_show NULL
955#endif
956
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100957/* This structure is replicated for each GPIO block allocated at probe time */
958static struct gpio_chip nmk_gpio_template = {
959 .direction_input = nmk_gpio_make_input,
960 .get = nmk_gpio_get_input,
961 .direction_output = nmk_gpio_make_output,
962 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100963 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530964 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100965 .can_sleep = 0,
966};
967
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200968void nmk_gpio_clocks_enable(void)
969{
970 int i;
971
972 for (i = 0; i < NUM_BANKS; i++) {
973 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
974
975 if (!chip)
976 continue;
977
978 clk_enable(chip->clk);
979 }
980}
981
982void nmk_gpio_clocks_disable(void)
983{
984 int i;
985
986 for (i = 0; i < NUM_BANKS; i++) {
987 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
988
989 if (!chip)
990 continue;
991
992 clk_disable(chip->clk);
993 }
994}
995
Rabin Vincentb9df4682011-02-10 11:45:58 +0530996/*
997 * Called from the suspend/resume path to only keep the real wakeup interrupts
998 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
999 * and not the rest of the interrupts which we needed to have as wakeups for
1000 * cpuidle.
1001 *
1002 * PM ops are not used since this needs to be done at the end, after all the
1003 * other drivers are done with their suspend callbacks.
1004 */
1005void nmk_gpio_wakeups_suspend(void)
1006{
1007 int i;
1008
1009 for (i = 0; i < NUM_BANKS; i++) {
1010 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1011
1012 if (!chip)
1013 break;
1014
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001015 clk_enable(chip->clk);
1016
Rabin Vincentb9df4682011-02-10 11:45:58 +05301017 chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
1018 chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
1019
1020 writel(chip->rwimsc & chip->real_wake,
1021 chip->addr + NMK_GPIO_RWIMSC);
1022 writel(chip->fwimsc & chip->real_wake,
1023 chip->addr + NMK_GPIO_FWIMSC);
1024
Linus Walleij33d78642011-06-09 11:08:47 +02001025 if (chip->sleepmode) {
Rabin Vincentb9df4682011-02-10 11:45:58 +05301026 chip->slpm = readl(chip->addr + NMK_GPIO_SLPC);
1027
1028 /* 0 -> wakeup enable */
1029 writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC);
1030 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001031
1032 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301033 }
1034}
1035
1036void nmk_gpio_wakeups_resume(void)
1037{
1038 int i;
1039
1040 for (i = 0; i < NUM_BANKS; i++) {
1041 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1042
1043 if (!chip)
1044 break;
1045
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001046 clk_enable(chip->clk);
1047
Rabin Vincentb9df4682011-02-10 11:45:58 +05301048 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1049 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1050
Linus Walleij33d78642011-06-09 11:08:47 +02001051 if (chip->sleepmode)
Rabin Vincentb9df4682011-02-10 11:45:58 +05301052 writel(chip->slpm, chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001053
1054 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301055 }
1056}
1057
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001058/*
1059 * Read the pull up/pull down status.
1060 * A bit set in 'pull_up' means that pull up
1061 * is selected if pull is enabled in PDIS register.
1062 * Note: only pull up/down set via this driver can
1063 * be detected due to HW limitations.
1064 */
1065void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1066{
1067 if (gpio_bank < NUM_BANKS) {
1068 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1069
1070 if (!chip)
1071 return;
1072
1073 *pull_up = chip->pull_up;
1074 }
1075}
1076
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001077static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001078{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001079 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001080 struct nmk_gpio_chip *nmk_chip;
1081 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001082 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001083 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301084 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001085 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001086 int ret;
1087
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001088 if (!pdata)
1089 return -ENODEV;
1090
1091 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1092 if (!res) {
1093 ret = -ENOENT;
1094 goto out;
1095 }
1096
1097 irq = platform_get_irq(dev, 0);
1098 if (irq < 0) {
1099 ret = irq;
1100 goto out;
1101 }
1102
Rabin Vincent33b744b2010-10-14 10:38:03 +05301103 secondary_irq = platform_get_irq(dev, 1);
1104 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1105 ret = -EINVAL;
1106 goto out;
1107 }
1108
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001109 if (request_mem_region(res->start, resource_size(res),
1110 dev_name(&dev->dev)) == NULL) {
1111 ret = -EBUSY;
1112 goto out;
1113 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001114
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001115 clk = clk_get(&dev->dev, NULL);
1116 if (IS_ERR(clk)) {
1117 ret = PTR_ERR(clk);
1118 goto out_release;
1119 }
1120
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001121 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1122 if (!nmk_chip) {
1123 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001124 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001125 }
1126 /*
1127 * The virt address in nmk_chip->addr is in the nomadik register space,
1128 * so we can simply convert the resource address, without remapping
1129 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301130 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001131 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001132 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001133 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001134 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301135 nmk_chip->secondary_parent_irq = secondary_irq;
1136 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301137 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001138 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001139 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001140
1141 chip = &nmk_chip->chip;
1142 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301143 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301144 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001145 chip->dev = &dev->dev;
1146 chip->owner = THIS_MODULE;
1147
1148 ret = gpiochip_add(&nmk_chip->chip);
1149 if (ret)
1150 goto out_free;
1151
Rabin Vincent01727e62010-12-13 12:02:40 +05301152 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1153
1154 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001155 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001156
1157 nmk_gpio_init_irq(nmk_chip);
1158
1159 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
1160 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
1161 return 0;
1162
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001163out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001164 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001165out_clk:
1166 clk_disable(clk);
1167 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001168out_release:
1169 release_mem_region(res->start, resource_size(res));
1170out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001171 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1172 pdata->first_gpio, pdata->first_gpio+31);
1173 return ret;
1174}
1175
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001176static struct platform_driver nmk_gpio_driver = {
1177 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001178 .owner = THIS_MODULE,
1179 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301180 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001181 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001182};
1183
1184static int __init nmk_gpio_init(void)
1185{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001186 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001187}
1188
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001189core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001190
1191MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1192MODULE_DESCRIPTION("Nomadik GPIO Driver");
1193MODULE_LICENSE("GPL");