blob: 0b7ec1258c8e30aec82686b482f7827c0b20c0ad [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;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020061 u32 pull_up;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010062};
63
Rabin Vincent01727e62010-12-13 12:02:40 +053064static struct nmk_gpio_chip *
65nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
66
67static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
68
69#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
70
Rabin Vincent6f9a9742010-06-02 05:50:28 +010071static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
72 unsigned offset, int gpio_mode)
73{
74 u32 bit = 1 << offset;
75 u32 afunc, bfunc;
76
77 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
78 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
79 if (gpio_mode & NMK_GPIO_ALT_A)
80 afunc |= bit;
81 if (gpio_mode & NMK_GPIO_ALT_B)
82 bfunc |= bit;
83 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
84 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
85}
86
Rabin Vincent81a3c292010-05-27 12:39:23 +010087static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
88 unsigned offset, enum nmk_gpio_slpm mode)
89{
90 u32 bit = 1 << offset;
91 u32 slpm;
92
93 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
94 if (mode == NMK_GPIO_SLPM_NOCHANGE)
95 slpm |= bit;
96 else
97 slpm &= ~bit;
98 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
99}
100
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100101static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
102 unsigned offset, enum nmk_gpio_pull pull)
103{
104 u32 bit = 1 << offset;
105 u32 pdis;
106
107 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200108 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100109 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200110 nmk_chip->pull_up &= ~bit;
111 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100112 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200113 }
114
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100115 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
116
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200117 if (pull == NMK_GPIO_PULL_UP) {
118 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100119 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200120 } else if (pull == NMK_GPIO_PULL_DOWN) {
121 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100122 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200123 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100124}
125
Rabin Vincent378be062010-06-02 06:06:29 +0100126static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
127 unsigned offset)
128{
129 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
130}
131
Rabin Vincent6720db72010-09-02 11:28:48 +0100132static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
133 unsigned offset, int val)
134{
135 if (val)
136 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
137 else
138 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
139}
140
141static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
142 unsigned offset, int val)
143{
144 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
145 __nmk_gpio_set_output(nmk_chip, offset, val);
146}
147
Rabin Vincent01727e62010-12-13 12:02:40 +0530148static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
149 unsigned offset, int gpio_mode,
150 bool glitch)
151{
Linus Walleij3c4bee02011-02-16 10:37:52 +0100152 u32 rwimsc = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
153 u32 fwimsc = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
Rabin Vincent01727e62010-12-13 12:02:40 +0530154
155 if (glitch && nmk_chip->set_ioforce) {
156 u32 bit = BIT(offset);
157
Rabin Vincent01727e62010-12-13 12:02:40 +0530158 /* Prevent spurious wakeups */
159 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
160 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
161
162 nmk_chip->set_ioforce(true);
163 }
164
165 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
166
167 if (glitch && nmk_chip->set_ioforce) {
168 nmk_chip->set_ioforce(false);
169
170 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
171 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
172 }
173}
174
Rabin Vincent378be062010-06-02 06:06:29 +0100175static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530176 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100177{
178 static const char *afnames[] = {
179 [NMK_GPIO_ALT_GPIO] = "GPIO",
180 [NMK_GPIO_ALT_A] = "A",
181 [NMK_GPIO_ALT_B] = "B",
182 [NMK_GPIO_ALT_C] = "C"
183 };
184 static const char *pullnames[] = {
185 [NMK_GPIO_PULL_NONE] = "none",
186 [NMK_GPIO_PULL_UP] = "up",
187 [NMK_GPIO_PULL_DOWN] = "down",
188 [3] /* illegal */ = "??"
189 };
190 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100191 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
192 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100193 };
194
195 int pin = PIN_NUM(cfg);
196 int pull = PIN_PULL(cfg);
197 int af = PIN_ALT(cfg);
198 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100199 int output = PIN_DIR(cfg);
200 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530201 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100202
Rabin Vincentdacdc962010-12-03 20:35:37 +0530203 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
204 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100205 output ? "output " : "input",
206 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100207
Rabin Vincentdacdc962010-12-03 20:35:37 +0530208 if (sleep) {
209 int slpm_pull = PIN_SLPM_PULL(cfg);
210 int slpm_output = PIN_SLPM_DIR(cfg);
211 int slpm_val = PIN_SLPM_VAL(cfg);
212
Rabin Vincent3546d152010-11-25 11:38:27 +0530213 af = NMK_GPIO_ALT_GPIO;
214
Rabin Vincentdacdc962010-12-03 20:35:37 +0530215 /*
216 * The SLPM_* values are normal values + 1 to allow zero to
217 * mean "same as normal".
218 */
219 if (slpm_pull)
220 pull = slpm_pull - 1;
221 if (slpm_output)
222 output = slpm_output - 1;
223 if (slpm_val)
224 val = slpm_val - 1;
225
226 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
227 pin,
228 slpm_pull ? pullnames[pull] : "same",
229 slpm_output ? (output ? "output" : "input") : "same",
230 slpm_val ? (val ? "high" : "low") : "same");
231 }
232
Rabin Vincent6720db72010-09-02 11:28:48 +0100233 if (output)
234 __nmk_gpio_make_output(nmk_chip, offset, val);
235 else {
236 __nmk_gpio_make_input(nmk_chip, offset);
237 __nmk_gpio_set_pull(nmk_chip, offset, pull);
238 }
239
Rabin Vincent01727e62010-12-13 12:02:40 +0530240 /*
241 * If we've backed up the SLPM registers (glitch workaround), modify
242 * the backups since they will be restored.
243 */
244 if (slpmregs) {
245 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
246 slpmregs[nmk_chip->bank] |= BIT(offset);
247 else
248 slpmregs[nmk_chip->bank] &= ~BIT(offset);
249 } else
250 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
251
252 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
253}
254
255/*
256 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
257 * - Save SLPM registers
258 * - Set SLPM=0 for the IOs you want to switch and others to 1
259 * - Configure the GPIO registers for the IOs that are being switched
260 * - Set IOFORCE=1
261 * - Modify the AFLSA/B registers for the IOs that are being switched
262 * - Set IOFORCE=0
263 * - Restore SLPM registers
264 * - Any spurious wake up event during switch sequence to be ignored and
265 * cleared
266 */
267static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
268{
269 int i;
270
271 for (i = 0; i < NUM_BANKS; i++) {
272 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
273 unsigned int temp = slpm[i];
274
275 if (!chip)
276 break;
277
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200278 clk_enable(chip->clk);
279
Rabin Vincent01727e62010-12-13 12:02:40 +0530280 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
281 writel(temp, chip->addr + NMK_GPIO_SLPC);
282 }
283}
284
285static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
286{
287 int i;
288
289 for (i = 0; i < NUM_BANKS; i++) {
290 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
291
292 if (!chip)
293 break;
294
295 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200296
297 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530298 }
299}
300
301static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
302{
303 static unsigned int slpm[NUM_BANKS];
304 unsigned long flags;
305 bool glitch = false;
306 int ret = 0;
307 int i;
308
309 for (i = 0; i < num; i++) {
310 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
311 glitch = true;
312 break;
313 }
314 }
315
316 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
317
318 if (glitch) {
319 memset(slpm, 0xff, sizeof(slpm));
320
321 for (i = 0; i < num; i++) {
322 int pin = PIN_NUM(cfgs[i]);
323 int offset = pin % NMK_GPIO_PER_CHIP;
324
325 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
326 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
327 }
328
329 nmk_gpio_glitch_slpm_init(slpm);
330 }
331
332 for (i = 0; i < num; i++) {
333 struct nmk_gpio_chip *nmk_chip;
334 int pin = PIN_NUM(cfgs[i]);
335
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100336 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
Rabin Vincent01727e62010-12-13 12:02:40 +0530337 if (!nmk_chip) {
338 ret = -EINVAL;
339 break;
340 }
341
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200342 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530343 spin_lock(&nmk_chip->lock);
344 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
345 cfgs[i], sleep, glitch ? slpm : NULL);
346 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200347 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530348 }
349
350 if (glitch)
351 nmk_gpio_glitch_slpm_restore(slpm);
352
353 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
354
355 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100356}
357
358/**
359 * nmk_config_pin - configure a pin's mux attributes
360 * @cfg: pin confguration
361 *
362 * Configures a pin's mode (alternate function or GPIO), its pull up status,
363 * and its sleep mode based on the specified configuration. The @cfg is
364 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
365 * are constructed using, and can be further enhanced with, the macros in
366 * plat/pincfg.h.
367 *
368 * If a pin's mode is set to GPIO, it is configured as an input to avoid
369 * side-effects. The gpio can be manipulated later using standard GPIO API
370 * calls.
371 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530372int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100373{
Rabin Vincent01727e62010-12-13 12:02:40 +0530374 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100375}
376EXPORT_SYMBOL(nmk_config_pin);
377
378/**
379 * nmk_config_pins - configure several pins at once
380 * @cfgs: array of pin configurations
381 * @num: number of elments in the array
382 *
383 * Configures several pins using nmk_config_pin(). Refer to that function for
384 * further information.
385 */
386int nmk_config_pins(pin_cfg_t *cfgs, int num)
387{
Rabin Vincent01727e62010-12-13 12:02:40 +0530388 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100389}
390EXPORT_SYMBOL(nmk_config_pins);
391
Rabin Vincentdacdc962010-12-03 20:35:37 +0530392int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
393{
Rabin Vincent01727e62010-12-13 12:02:40 +0530394 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530395}
396EXPORT_SYMBOL(nmk_config_pins_sleep);
397
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100398/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100399 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
400 * @gpio: pin number
401 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
402 *
Linus Walleij33d78642011-06-09 11:08:47 +0200403 * This register is actually in the pinmux layer, not the GPIO block itself.
404 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
405 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
406 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
407 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
408 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
409 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100410 *
Linus Walleij33d78642011-06-09 11:08:47 +0200411 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
412 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
413 * entered) regardless of the altfunction selected. Also wake-up detection is
414 * ENABLED.
415 *
416 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
417 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
418 * (for altfunction GPIO) or respective on-chip peripherals (for other
419 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
420 *
421 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100422 */
423int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
424{
425 struct nmk_gpio_chip *nmk_chip;
426 unsigned long flags;
427
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100428 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent81a3c292010-05-27 12:39:23 +0100429 if (!nmk_chip)
430 return -EINVAL;
431
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200432 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530433 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
434 spin_lock(&nmk_chip->lock);
435
Rabin Vincent81a3c292010-05-27 12:39:23 +0100436 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530437
438 spin_unlock(&nmk_chip->lock);
439 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200440 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100441
442 return 0;
443}
444
445/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100446 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
447 * @gpio: pin number
448 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
449 *
450 * Enables/disables pull up/down on a specified pin. This only takes effect if
451 * the pin is configured as an input (either explicitly or by the alternate
452 * function).
453 *
454 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
455 * configured as an input. Otherwise, due to the way the controller registers
456 * work, this function will change the value output on the pin.
457 */
458int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
459{
460 struct nmk_gpio_chip *nmk_chip;
461 unsigned long flags;
462
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100463 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100464 if (!nmk_chip)
465 return -EINVAL;
466
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200467 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100468 spin_lock_irqsave(&nmk_chip->lock, flags);
469 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
470 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200471 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100472
473 return 0;
474}
475
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100476/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200477/**
478 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
479 * @gpio: pin number
480 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
481 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
482 *
483 * Sets the mode of the specified pin to one of the alternate functions or
484 * plain GPIO.
485 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100486int nmk_gpio_set_mode(int gpio, int gpio_mode)
487{
488 struct nmk_gpio_chip *nmk_chip;
489 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100490
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100491 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100492 if (!nmk_chip)
493 return -EINVAL;
494
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200495 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100496 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100497 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100498 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200499 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100500
501 return 0;
502}
503EXPORT_SYMBOL(nmk_gpio_set_mode);
504
505int nmk_gpio_get_mode(int gpio)
506{
507 struct nmk_gpio_chip *nmk_chip;
508 u32 afunc, bfunc, bit;
509
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100510 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100511 if (!nmk_chip)
512 return -EINVAL;
513
514 bit = 1 << (gpio - nmk_chip->chip.base);
515
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200516 clk_enable(nmk_chip->clk);
517
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100518 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
519 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
520
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200521 clk_disable(nmk_chip->clk);
522
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100523 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
524}
525EXPORT_SYMBOL(nmk_gpio_get_mode);
526
527
528/* IRQ functions */
529static inline int nmk_gpio_get_bitmask(int gpio)
530{
531 return 1 << (gpio % 32);
532}
533
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100534static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100535{
536 int gpio;
537 struct nmk_gpio_chip *nmk_chip;
538
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100539 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
540 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100541 if (!nmk_chip)
542 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200543
544 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100545 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200546 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547}
548
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100549enum nmk_gpio_irq_type {
550 NORMAL,
551 WAKE,
552};
553
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100554static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100555 int gpio, enum nmk_gpio_irq_type which,
556 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100557{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100558 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
559 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100560 u32 bitmask = nmk_gpio_get_bitmask(gpio);
561 u32 reg;
562
563 /* we must individually set/clear the two edges */
564 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100565 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100566 if (enable)
567 reg |= bitmask;
568 else
569 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100570 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100571 }
572 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100573 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100574 if (enable)
575 reg |= bitmask;
576 else
577 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100578 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100579 }
580}
581
Rabin Vincentb9df4682011-02-10 11:45:58 +0530582static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
583 int gpio, bool on)
584{
Linus Walleij33d78642011-06-09 11:08:47 +0200585 if (nmk_chip->sleepmode) {
586 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
587 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
588 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
589 }
590
Rabin Vincentb9df4682011-02-10 11:45:58 +0530591 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
592}
593
594static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100595{
596 int gpio;
597 struct nmk_gpio_chip *nmk_chip;
598 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100599 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100600
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100601 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
602 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100603 bitmask = nmk_gpio_get_bitmask(gpio);
604 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100605 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100606
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200607 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530608 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
609 spin_lock(&nmk_chip->lock);
610
611 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
612
613 if (!(nmk_chip->real_wake & bitmask))
614 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
615
616 spin_unlock(&nmk_chip->lock);
617 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200618 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100619
620 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100621}
622
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100623static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100624{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530625 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100626}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100627
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100628static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100629{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530630 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100631}
632
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100633static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100634{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100635 struct nmk_gpio_chip *nmk_chip;
636 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530637 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100638 int gpio;
639
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100640 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
641 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100642 if (!nmk_chip)
643 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530644 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100645
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200646 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530647 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
648 spin_lock(&nmk_chip->lock);
649
Linus Walleij479a0c72011-09-20 10:50:15 +0200650 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530651 __nmk_gpio_set_wake(nmk_chip, gpio, on);
652
653 if (on)
654 nmk_chip->real_wake |= bitmask;
655 else
656 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530657
658 spin_unlock(&nmk_chip->lock);
659 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200660 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100661
662 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100663}
664
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100665static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100666{
Linus Walleij479a0c72011-09-20 10:50:15 +0200667 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200668 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669 int gpio;
670 struct nmk_gpio_chip *nmk_chip;
671 unsigned long flags;
672 u32 bitmask;
673
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100674 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
675 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100676 bitmask = nmk_gpio_get_bitmask(gpio);
677 if (!nmk_chip)
678 return -EINVAL;
679
680 if (type & IRQ_TYPE_LEVEL_HIGH)
681 return -EINVAL;
682 if (type & IRQ_TYPE_LEVEL_LOW)
683 return -EINVAL;
684
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200685 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100686 spin_lock_irqsave(&nmk_chip->lock, flags);
687
Rabin Vincent7a852d82010-05-06 10:43:55 +0100688 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100689 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
690
Rabin Vincentb9df4682011-02-10 11:45:58 +0530691 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100692 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100693
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100694 nmk_chip->edge_rising &= ~bitmask;
695 if (type & IRQ_TYPE_EDGE_RISING)
696 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100697
698 nmk_chip->edge_falling &= ~bitmask;
699 if (type & IRQ_TYPE_EDGE_FALLING)
700 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100701
702 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100703 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
704
Rabin Vincentb9df4682011-02-10 11:45:58 +0530705 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100706 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100707
708 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200709 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100710
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711 return 0;
712}
713
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200714static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
715{
716 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
717
718 clk_enable(nmk_chip->clk);
719 nmk_gpio_irq_unmask(d);
720 return 0;
721}
722
723static void nmk_gpio_irq_shutdown(struct irq_data *d)
724{
725 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
726
727 nmk_gpio_irq_mask(d);
728 clk_disable(nmk_chip->clk);
729}
730
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100731static struct irq_chip nmk_gpio_irq_chip = {
732 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100733 .irq_ack = nmk_gpio_irq_ack,
734 .irq_mask = nmk_gpio_irq_mask,
735 .irq_unmask = nmk_gpio_irq_unmask,
736 .irq_set_type = nmk_gpio_irq_set_type,
737 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200738 .irq_startup = nmk_gpio_irq_startup,
739 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100740};
741
Rabin Vincent33b744b2010-10-14 10:38:03 +0530742static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
743 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100744{
745 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100746 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100747 unsigned int first_irq;
748
Will Deaconadfed152011-02-28 10:12:29 +0000749 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100750
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100751 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100752 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530753 while (status) {
754 int bit = __ffs(status);
755
756 generic_handle_irq(first_irq + bit);
757 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100758 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100759
Will Deaconadfed152011-02-28 10:12:29 +0000760 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100761}
762
Rabin Vincent33b744b2010-10-14 10:38:03 +0530763static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
764{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100765 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200766 u32 status;
767
768 clk_enable(nmk_chip->clk);
769 status = readl(nmk_chip->addr + NMK_GPIO_IS);
770 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530771
772 __nmk_gpio_irq_handler(irq, desc, status);
773}
774
775static void nmk_gpio_secondary_irq_handler(unsigned int irq,
776 struct irq_desc *desc)
777{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100778 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530779 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
780
781 __nmk_gpio_irq_handler(irq, desc, status);
782}
783
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
785{
786 unsigned int first_irq;
787 int i;
788
789 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530790 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100791 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
792 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100793 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100794 irq_set_chip_data(i, nmk_chip);
795 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100796 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530797
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100798 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
799 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530800
801 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100802 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530803 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100804 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530805 }
806
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100807 return 0;
808}
809
810/* I/O Functions */
811static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
812{
813 struct nmk_gpio_chip *nmk_chip =
814 container_of(chip, struct nmk_gpio_chip, chip);
815
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200816 clk_enable(nmk_chip->clk);
817
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100818 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200819
820 clk_disable(nmk_chip->clk);
821
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100822 return 0;
823}
824
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100825static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
826{
827 struct nmk_gpio_chip *nmk_chip =
828 container_of(chip, struct nmk_gpio_chip, chip);
829 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200830 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100831
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200832 clk_enable(nmk_chip->clk);
833
834 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
835
836 clk_disable(nmk_chip->clk);
837
838 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100839}
840
841static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
842 int val)
843{
844 struct nmk_gpio_chip *nmk_chip =
845 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100846
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200847 clk_enable(nmk_chip->clk);
848
Rabin Vincent6720db72010-09-02 11:28:48 +0100849 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200850
851 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100852}
853
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100854static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
855 int val)
856{
857 struct nmk_gpio_chip *nmk_chip =
858 container_of(chip, struct nmk_gpio_chip, chip);
859
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200860 clk_enable(nmk_chip->clk);
861
Rabin Vincent6720db72010-09-02 11:28:48 +0100862 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100863
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200864 clk_disable(nmk_chip->clk);
865
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100866 return 0;
867}
868
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100869static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
870{
871 struct nmk_gpio_chip *nmk_chip =
872 container_of(chip, struct nmk_gpio_chip, chip);
873
874 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
875}
876
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530877#ifdef CONFIG_DEBUG_FS
878
879#include <linux/seq_file.h>
880
881static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
882{
883 int mode;
884 unsigned i;
885 unsigned gpio = chip->base;
886 int is_out;
887 struct nmk_gpio_chip *nmk_chip =
888 container_of(chip, struct nmk_gpio_chip, chip);
889 const char *modes[] = {
890 [NMK_GPIO_ALT_GPIO] = "gpio",
891 [NMK_GPIO_ALT_A] = "altA",
892 [NMK_GPIO_ALT_B] = "altB",
893 [NMK_GPIO_ALT_C] = "altC",
894 };
895
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200896 clk_enable(nmk_chip->clk);
897
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530898 for (i = 0; i < chip->ngpio; i++, gpio++) {
899 const char *label = gpiochip_is_requested(chip, i);
900 bool pull;
901 u32 bit = 1 << i;
902
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530903 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
904 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
905 mode = nmk_gpio_get_mode(gpio);
906 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200907 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530908 is_out ? "out" : "in ",
909 chip->get
910 ? (chip->get(chip, i) ? "hi" : "lo")
911 : "? ",
912 (mode < 0) ? "unknown" : modes[mode],
913 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200914
915 if (label && !is_out) {
916 int irq = gpio_to_irq(gpio);
917 struct irq_desc *desc = irq_to_desc(irq);
918
919 /* This races with request_irq(), set_irq_type(),
920 * and set_irq_wake() ... but those are "rare".
921 */
922 if (irq >= 0 && desc->action) {
923 char *trigger;
924 u32 bitmask = nmk_gpio_get_bitmask(gpio);
925
926 if (nmk_chip->edge_rising & bitmask)
927 trigger = "edge-rising";
928 else if (nmk_chip->edge_falling & bitmask)
929 trigger = "edge-falling";
930 else
931 trigger = "edge-undefined";
932
933 seq_printf(s, " irq-%d %s%s",
934 irq, trigger,
935 irqd_is_wakeup_set(&desc->irq_data)
936 ? " wakeup" : "");
937 }
938 }
939
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530940 seq_printf(s, "\n");
941 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200942
943 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530944}
945
946#else
947#define nmk_gpio_dbg_show NULL
948#endif
949
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100950/* This structure is replicated for each GPIO block allocated at probe time */
951static struct gpio_chip nmk_gpio_template = {
952 .direction_input = nmk_gpio_make_input,
953 .get = nmk_gpio_get_input,
954 .direction_output = nmk_gpio_make_output,
955 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100956 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530957 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100958 .can_sleep = 0,
959};
960
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200961void nmk_gpio_clocks_enable(void)
962{
963 int i;
964
965 for (i = 0; i < NUM_BANKS; i++) {
966 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
967
968 if (!chip)
969 continue;
970
971 clk_enable(chip->clk);
972 }
973}
974
975void nmk_gpio_clocks_disable(void)
976{
977 int i;
978
979 for (i = 0; i < NUM_BANKS; i++) {
980 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
981
982 if (!chip)
983 continue;
984
985 clk_disable(chip->clk);
986 }
987}
988
Rabin Vincentb9df4682011-02-10 11:45:58 +0530989/*
990 * Called from the suspend/resume path to only keep the real wakeup interrupts
991 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
992 * and not the rest of the interrupts which we needed to have as wakeups for
993 * cpuidle.
994 *
995 * PM ops are not used since this needs to be done at the end, after all the
996 * other drivers are done with their suspend callbacks.
997 */
998void nmk_gpio_wakeups_suspend(void)
999{
1000 int i;
1001
1002 for (i = 0; i < NUM_BANKS; i++) {
1003 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1004
1005 if (!chip)
1006 break;
1007
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001008 clk_enable(chip->clk);
1009
Rabin Vincentb9df4682011-02-10 11:45:58 +05301010 chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
1011 chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
1012
1013 writel(chip->rwimsc & chip->real_wake,
1014 chip->addr + NMK_GPIO_RWIMSC);
1015 writel(chip->fwimsc & chip->real_wake,
1016 chip->addr + NMK_GPIO_FWIMSC);
1017
Linus Walleij33d78642011-06-09 11:08:47 +02001018 if (chip->sleepmode) {
Rabin Vincentb9df4682011-02-10 11:45:58 +05301019 chip->slpm = readl(chip->addr + NMK_GPIO_SLPC);
1020
1021 /* 0 -> wakeup enable */
1022 writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC);
1023 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001024
1025 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301026 }
1027}
1028
1029void nmk_gpio_wakeups_resume(void)
1030{
1031 int i;
1032
1033 for (i = 0; i < NUM_BANKS; i++) {
1034 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1035
1036 if (!chip)
1037 break;
1038
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001039 clk_enable(chip->clk);
1040
Rabin Vincentb9df4682011-02-10 11:45:58 +05301041 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1042 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1043
Linus Walleij33d78642011-06-09 11:08:47 +02001044 if (chip->sleepmode)
Rabin Vincentb9df4682011-02-10 11:45:58 +05301045 writel(chip->slpm, chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001046
1047 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301048 }
1049}
1050
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001051/*
1052 * Read the pull up/pull down status.
1053 * A bit set in 'pull_up' means that pull up
1054 * is selected if pull is enabled in PDIS register.
1055 * Note: only pull up/down set via this driver can
1056 * be detected due to HW limitations.
1057 */
1058void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1059{
1060 if (gpio_bank < NUM_BANKS) {
1061 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1062
1063 if (!chip)
1064 return;
1065
1066 *pull_up = chip->pull_up;
1067 }
1068}
1069
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001070static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001071{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001072 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001073 struct nmk_gpio_chip *nmk_chip;
1074 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001075 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001076 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301077 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001078 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001079 int ret;
1080
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001081 if (!pdata)
1082 return -ENODEV;
1083
1084 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1085 if (!res) {
1086 ret = -ENOENT;
1087 goto out;
1088 }
1089
1090 irq = platform_get_irq(dev, 0);
1091 if (irq < 0) {
1092 ret = irq;
1093 goto out;
1094 }
1095
Rabin Vincent33b744b2010-10-14 10:38:03 +05301096 secondary_irq = platform_get_irq(dev, 1);
1097 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1098 ret = -EINVAL;
1099 goto out;
1100 }
1101
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001102 if (request_mem_region(res->start, resource_size(res),
1103 dev_name(&dev->dev)) == NULL) {
1104 ret = -EBUSY;
1105 goto out;
1106 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001107
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001108 clk = clk_get(&dev->dev, NULL);
1109 if (IS_ERR(clk)) {
1110 ret = PTR_ERR(clk);
1111 goto out_release;
1112 }
1113
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001114 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1115 if (!nmk_chip) {
1116 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001117 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001118 }
1119 /*
1120 * The virt address in nmk_chip->addr is in the nomadik register space,
1121 * so we can simply convert the resource address, without remapping
1122 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301123 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001124 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001125 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001126 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001127 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301128 nmk_chip->secondary_parent_irq = secondary_irq;
1129 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301130 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001131 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001132 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001133
1134 chip = &nmk_chip->chip;
1135 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301136 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301137 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001138 chip->dev = &dev->dev;
1139 chip->owner = THIS_MODULE;
1140
1141 ret = gpiochip_add(&nmk_chip->chip);
1142 if (ret)
1143 goto out_free;
1144
Rabin Vincent01727e62010-12-13 12:02:40 +05301145 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1146
1147 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001148 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001149
1150 nmk_gpio_init_irq(nmk_chip);
1151
1152 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
1153 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
1154 return 0;
1155
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001156out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001157 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001158out_clk:
1159 clk_disable(clk);
1160 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001161out_release:
1162 release_mem_region(res->start, resource_size(res));
1163out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001164 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1165 pdata->first_gpio, pdata->first_gpio+31);
1166 return ret;
1167}
1168
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001169static struct platform_driver nmk_gpio_driver = {
1170 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001171 .owner = THIS_MODULE,
1172 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301173 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001174 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001175};
1176
1177static int __init nmk_gpio_init(void)
1178{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001179 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001180}
1181
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001182core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001183
1184MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1185MODULE_DESCRIPTION("Nomadik GPIO Driver");
1186MODULE_LICENSE("GPL");