blob: f1ebd05a9c502bf32fbf734f2930c8ac4972e6d8 [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>
Linus Walleij0f332862011-08-22 08:33:30 +010030#include <plat/gpio-nomadik.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010031#include <mach/hardware.h>
Russell King75482dc2011-07-26 10:57:37 +010032#include <asm/gpio.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010033
34/*
35 * The GPIO module in the Nomadik family of Systems-on-Chip is an
36 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020037 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010038 *
39 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
40 */
41
Rabin Vincent01727e62010-12-13 12:02:40 +053042#define NMK_GPIO_PER_CHIP 32
43
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010044struct nmk_gpio_chip {
45 struct gpio_chip chip;
46 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010047 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053048 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010049 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053050 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053051 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053052 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010053 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020054 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010055 /* Keep track of configured edges */
56 u32 edge_rising;
57 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053058 u32 real_wake;
59 u32 rwimsc;
60 u32 fwimsc;
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{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530585 /*
586 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
587 * disabled, since setting SLPM to 1 increases power consumption, and
588 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
589 */
590 if (nmk_chip->sleepmode && on) {
Linus Walleij33d78642011-06-09 11:08:47 +0200591 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530592 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200593 }
594
Rabin Vincentb9df4682011-02-10 11:45:58 +0530595 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
596}
597
598static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100599{
600 int gpio;
601 struct nmk_gpio_chip *nmk_chip;
602 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100603 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100604
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100605 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
606 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100607 bitmask = nmk_gpio_get_bitmask(gpio);
608 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100609 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100610
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200611 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530612 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
613 spin_lock(&nmk_chip->lock);
614
615 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
616
617 if (!(nmk_chip->real_wake & bitmask))
618 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
619
620 spin_unlock(&nmk_chip->lock);
621 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200622 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100623
624 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100625}
626
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100627static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100628{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530629 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100630}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100631
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100632static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100633{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530634 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100635}
636
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100637static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100638{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100639 struct nmk_gpio_chip *nmk_chip;
640 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530641 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100642 int gpio;
643
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100644 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
645 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100646 if (!nmk_chip)
647 return -EINVAL;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530648 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100649
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200650 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530651 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
652 spin_lock(&nmk_chip->lock);
653
Linus Walleij479a0c72011-09-20 10:50:15 +0200654 if (irqd_irq_disabled(d))
Rabin Vincentb9df4682011-02-10 11:45:58 +0530655 __nmk_gpio_set_wake(nmk_chip, gpio, on);
656
657 if (on)
658 nmk_chip->real_wake |= bitmask;
659 else
660 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530661
662 spin_unlock(&nmk_chip->lock);
663 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200664 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100665
666 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667}
668
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100669static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100670{
Linus Walleij479a0c72011-09-20 10:50:15 +0200671 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200672 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100673 int gpio;
674 struct nmk_gpio_chip *nmk_chip;
675 unsigned long flags;
676 u32 bitmask;
677
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100678 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
679 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100680 bitmask = nmk_gpio_get_bitmask(gpio);
681 if (!nmk_chip)
682 return -EINVAL;
683
684 if (type & IRQ_TYPE_LEVEL_HIGH)
685 return -EINVAL;
686 if (type & IRQ_TYPE_LEVEL_LOW)
687 return -EINVAL;
688
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200689 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100690 spin_lock_irqsave(&nmk_chip->lock, flags);
691
Rabin Vincent7a852d82010-05-06 10:43:55 +0100692 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100693 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
694
Rabin Vincentb9df4682011-02-10 11:45:58 +0530695 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100696 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100697
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100698 nmk_chip->edge_rising &= ~bitmask;
699 if (type & IRQ_TYPE_EDGE_RISING)
700 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100701
702 nmk_chip->edge_falling &= ~bitmask;
703 if (type & IRQ_TYPE_EDGE_FALLING)
704 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100705
706 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100707 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
708
Rabin Vincentb9df4682011-02-10 11:45:58 +0530709 if (enabled || wake)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100710 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711
712 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200713 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100714
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100715 return 0;
716}
717
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200718static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
719{
720 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
721
722 clk_enable(nmk_chip->clk);
723 nmk_gpio_irq_unmask(d);
724 return 0;
725}
726
727static void nmk_gpio_irq_shutdown(struct irq_data *d)
728{
729 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
730
731 nmk_gpio_irq_mask(d);
732 clk_disable(nmk_chip->clk);
733}
734
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100735static struct irq_chip nmk_gpio_irq_chip = {
736 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100737 .irq_ack = nmk_gpio_irq_ack,
738 .irq_mask = nmk_gpio_irq_mask,
739 .irq_unmask = nmk_gpio_irq_unmask,
740 .irq_set_type = nmk_gpio_irq_set_type,
741 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200742 .irq_startup = nmk_gpio_irq_startup,
743 .irq_shutdown = nmk_gpio_irq_shutdown,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100744};
745
Rabin Vincent33b744b2010-10-14 10:38:03 +0530746static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
747 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100748{
749 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100750 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100751 unsigned int first_irq;
752
Will Deaconadfed152011-02-28 10:12:29 +0000753 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100754
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100755 nmk_chip = irq_get_handler_data(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100756 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530757 while (status) {
758 int bit = __ffs(status);
759
760 generic_handle_irq(first_irq + bit);
761 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100762 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100763
Will Deaconadfed152011-02-28 10:12:29 +0000764 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100765}
766
Rabin Vincent33b744b2010-10-14 10:38:03 +0530767static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
768{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100769 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200770 u32 status;
771
772 clk_enable(nmk_chip->clk);
773 status = readl(nmk_chip->addr + NMK_GPIO_IS);
774 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530775
776 __nmk_gpio_irq_handler(irq, desc, status);
777}
778
779static void nmk_gpio_secondary_irq_handler(unsigned int irq,
780 struct irq_desc *desc)
781{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100782 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530783 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
784
785 __nmk_gpio_irq_handler(irq, desc, status);
786}
787
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100788static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
789{
790 unsigned int first_irq;
791 int i;
792
793 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530794 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100795 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
796 handle_edge_irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100797 set_irq_flags(i, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100798 irq_set_chip_data(i, nmk_chip);
799 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100800 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530801
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100802 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
803 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530804
805 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100806 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530807 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100808 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530809 }
810
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100811 return 0;
812}
813
814/* I/O Functions */
815static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
816{
817 struct nmk_gpio_chip *nmk_chip =
818 container_of(chip, struct nmk_gpio_chip, chip);
819
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200820 clk_enable(nmk_chip->clk);
821
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100822 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200823
824 clk_disable(nmk_chip->clk);
825
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100826 return 0;
827}
828
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100829static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
830{
831 struct nmk_gpio_chip *nmk_chip =
832 container_of(chip, struct nmk_gpio_chip, chip);
833 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200834 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100835
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200836 clk_enable(nmk_chip->clk);
837
838 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
839
840 clk_disable(nmk_chip->clk);
841
842 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100843}
844
845static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
846 int val)
847{
848 struct nmk_gpio_chip *nmk_chip =
849 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100850
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200851 clk_enable(nmk_chip->clk);
852
Rabin Vincent6720db72010-09-02 11:28:48 +0100853 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200854
855 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100856}
857
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100858static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
859 int val)
860{
861 struct nmk_gpio_chip *nmk_chip =
862 container_of(chip, struct nmk_gpio_chip, chip);
863
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200864 clk_enable(nmk_chip->clk);
865
Rabin Vincent6720db72010-09-02 11:28:48 +0100866 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100867
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200868 clk_disable(nmk_chip->clk);
869
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100870 return 0;
871}
872
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100873static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
874{
875 struct nmk_gpio_chip *nmk_chip =
876 container_of(chip, struct nmk_gpio_chip, chip);
877
878 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
879}
880
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530881#ifdef CONFIG_DEBUG_FS
882
883#include <linux/seq_file.h>
884
885static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
886{
887 int mode;
888 unsigned i;
889 unsigned gpio = chip->base;
890 int is_out;
891 struct nmk_gpio_chip *nmk_chip =
892 container_of(chip, struct nmk_gpio_chip, chip);
893 const char *modes[] = {
894 [NMK_GPIO_ALT_GPIO] = "gpio",
895 [NMK_GPIO_ALT_A] = "altA",
896 [NMK_GPIO_ALT_B] = "altB",
897 [NMK_GPIO_ALT_C] = "altC",
898 };
899
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200900 clk_enable(nmk_chip->clk);
901
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530902 for (i = 0; i < chip->ngpio; i++, gpio++) {
903 const char *label = gpiochip_is_requested(chip, i);
904 bool pull;
905 u32 bit = 1 << i;
906
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530907 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
908 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
909 mode = nmk_gpio_get_mode(gpio);
910 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200911 gpio, label ?: "(none)",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530912 is_out ? "out" : "in ",
913 chip->get
914 ? (chip->get(chip, i) ? "hi" : "lo")
915 : "? ",
916 (mode < 0) ? "unknown" : modes[mode],
917 pull ? "pull" : "none");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200918
919 if (label && !is_out) {
920 int irq = gpio_to_irq(gpio);
921 struct irq_desc *desc = irq_to_desc(irq);
922
923 /* This races with request_irq(), set_irq_type(),
924 * and set_irq_wake() ... but those are "rare".
925 */
926 if (irq >= 0 && desc->action) {
927 char *trigger;
928 u32 bitmask = nmk_gpio_get_bitmask(gpio);
929
930 if (nmk_chip->edge_rising & bitmask)
931 trigger = "edge-rising";
932 else if (nmk_chip->edge_falling & bitmask)
933 trigger = "edge-falling";
934 else
935 trigger = "edge-undefined";
936
937 seq_printf(s, " irq-%d %s%s",
938 irq, trigger,
939 irqd_is_wakeup_set(&desc->irq_data)
940 ? " wakeup" : "");
941 }
942 }
943
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530944 seq_printf(s, "\n");
945 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200946
947 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530948}
949
950#else
951#define nmk_gpio_dbg_show NULL
952#endif
953
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100954/* This structure is replicated for each GPIO block allocated at probe time */
955static struct gpio_chip nmk_gpio_template = {
956 .direction_input = nmk_gpio_make_input,
957 .get = nmk_gpio_get_input,
958 .direction_output = nmk_gpio_make_output,
959 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100960 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530961 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100962 .can_sleep = 0,
963};
964
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200965void nmk_gpio_clocks_enable(void)
966{
967 int i;
968
969 for (i = 0; i < NUM_BANKS; i++) {
970 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
971
972 if (!chip)
973 continue;
974
975 clk_enable(chip->clk);
976 }
977}
978
979void nmk_gpio_clocks_disable(void)
980{
981 int i;
982
983 for (i = 0; i < NUM_BANKS; i++) {
984 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
985
986 if (!chip)
987 continue;
988
989 clk_disable(chip->clk);
990 }
991}
992
Rabin Vincentb9df4682011-02-10 11:45:58 +0530993/*
994 * Called from the suspend/resume path to only keep the real wakeup interrupts
995 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
996 * and not the rest of the interrupts which we needed to have as wakeups for
997 * cpuidle.
998 *
999 * PM ops are not used since this needs to be done at the end, after all the
1000 * other drivers are done with their suspend callbacks.
1001 */
1002void nmk_gpio_wakeups_suspend(void)
1003{
1004 int i;
1005
1006 for (i = 0; i < NUM_BANKS; i++) {
1007 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1008
1009 if (!chip)
1010 break;
1011
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001012 clk_enable(chip->clk);
1013
Rabin Vincentb9df4682011-02-10 11:45:58 +05301014 chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
1015 chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
1016
1017 writel(chip->rwimsc & chip->real_wake,
1018 chip->addr + NMK_GPIO_RWIMSC);
1019 writel(chip->fwimsc & chip->real_wake,
1020 chip->addr + NMK_GPIO_FWIMSC);
1021
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001022 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301023 }
1024}
1025
1026void nmk_gpio_wakeups_resume(void)
1027{
1028 int i;
1029
1030 for (i = 0; i < NUM_BANKS; i++) {
1031 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1032
1033 if (!chip)
1034 break;
1035
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001036 clk_enable(chip->clk);
1037
Rabin Vincentb9df4682011-02-10 11:45:58 +05301038 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1039 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1040
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001041 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301042 }
1043}
1044
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001045/*
1046 * Read the pull up/pull down status.
1047 * A bit set in 'pull_up' means that pull up
1048 * is selected if pull is enabled in PDIS register.
1049 * Note: only pull up/down set via this driver can
1050 * be detected due to HW limitations.
1051 */
1052void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1053{
1054 if (gpio_bank < NUM_BANKS) {
1055 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1056
1057 if (!chip)
1058 return;
1059
1060 *pull_up = chip->pull_up;
1061 }
1062}
1063
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +01001064static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001065{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001066 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001067 struct nmk_gpio_chip *nmk_chip;
1068 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001069 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001070 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301071 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001072 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001073 int ret;
1074
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001075 if (!pdata)
1076 return -ENODEV;
1077
1078 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1079 if (!res) {
1080 ret = -ENOENT;
1081 goto out;
1082 }
1083
1084 irq = platform_get_irq(dev, 0);
1085 if (irq < 0) {
1086 ret = irq;
1087 goto out;
1088 }
1089
Rabin Vincent33b744b2010-10-14 10:38:03 +05301090 secondary_irq = platform_get_irq(dev, 1);
1091 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1092 ret = -EINVAL;
1093 goto out;
1094 }
1095
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001096 if (request_mem_region(res->start, resource_size(res),
1097 dev_name(&dev->dev)) == NULL) {
1098 ret = -EBUSY;
1099 goto out;
1100 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001101
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001102 clk = clk_get(&dev->dev, NULL);
1103 if (IS_ERR(clk)) {
1104 ret = PTR_ERR(clk);
1105 goto out_release;
1106 }
1107
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001108 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1109 if (!nmk_chip) {
1110 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001111 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001112 }
1113 /*
1114 * The virt address in nmk_chip->addr is in the nomadik register space,
1115 * so we can simply convert the resource address, without remapping
1116 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301117 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001118 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001119 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001120 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001121 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301122 nmk_chip->secondary_parent_irq = secondary_irq;
1123 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301124 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001125 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001126 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001127
1128 chip = &nmk_chip->chip;
1129 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301130 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301131 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001132 chip->dev = &dev->dev;
1133 chip->owner = THIS_MODULE;
1134
1135 ret = gpiochip_add(&nmk_chip->chip);
1136 if (ret)
1137 goto out_free;
1138
Rabin Vincent01727e62010-12-13 12:02:40 +05301139 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1140
1141 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001142 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001143
1144 nmk_gpio_init_irq(nmk_chip);
1145
Grant Likely64842aa2011-11-06 11:36:18 -07001146 dev_info(&dev->dev, "at address %p\n",
1147 nmk_chip->addr);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001148 return 0;
1149
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001150out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001151 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001152out_clk:
1153 clk_disable(clk);
1154 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001155out_release:
1156 release_mem_region(res->start, resource_size(res));
1157out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001158 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1159 pdata->first_gpio, pdata->first_gpio+31);
1160 return ret;
1161}
1162
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001163static struct platform_driver nmk_gpio_driver = {
1164 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001165 .owner = THIS_MODULE,
1166 .name = "gpio",
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301167 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001168 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001169};
1170
1171static int __init nmk_gpio_init(void)
1172{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001173 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001174}
1175
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001176core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001177
1178MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1179MODULE_DESCRIPTION("Nomadik GPIO Driver");
1180MODULE_LICENSE("GPL");