blob: 85e6fd212a414efa02526762123b0b4d20653d9a [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>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010016#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010017#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010018#include <linux/clk.h>
19#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010020#include <linux/gpio.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010025
Rabin Vincent378be062010-06-02 06:06:29 +010026#include <plat/pincfg.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010027#include <mach/hardware.h>
28#include <mach/gpio.h>
29
30/*
31 * The GPIO module in the Nomadik family of Systems-on-Chip is an
32 * AMBA device, managing 32 pins and alternate functions. The logic block
33 * is currently only used in the Nomadik.
34 *
35 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
36 */
37
38#define NMK_GPIO_PER_CHIP 32
39struct nmk_gpio_chip {
40 struct gpio_chip chip;
41 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010042 struct clk *clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010043 unsigned int parent_irq;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010044 spinlock_t lock;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010045 /* Keep track of configured edges */
46 u32 edge_rising;
47 u32 edge_falling;
48};
49
Rabin Vincent6f9a9742010-06-02 05:50:28 +010050static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
51 unsigned offset, int gpio_mode)
52{
53 u32 bit = 1 << offset;
54 u32 afunc, bfunc;
55
56 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
57 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
58 if (gpio_mode & NMK_GPIO_ALT_A)
59 afunc |= bit;
60 if (gpio_mode & NMK_GPIO_ALT_B)
61 bfunc |= bit;
62 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
63 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
64}
65
Rabin Vincent81a3c292010-05-27 12:39:23 +010066static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
67 unsigned offset, enum nmk_gpio_slpm mode)
68{
69 u32 bit = 1 << offset;
70 u32 slpm;
71
72 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
73 if (mode == NMK_GPIO_SLPM_NOCHANGE)
74 slpm |= bit;
75 else
76 slpm &= ~bit;
77 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
78}
79
Rabin Vincent5b327ed2010-05-27 12:29:50 +010080static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
81 unsigned offset, enum nmk_gpio_pull pull)
82{
83 u32 bit = 1 << offset;
84 u32 pdis;
85
86 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
87 if (pull == NMK_GPIO_PULL_NONE)
88 pdis |= bit;
89 else
90 pdis &= ~bit;
91 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
92
93 if (pull == NMK_GPIO_PULL_UP)
94 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
95 else if (pull == NMK_GPIO_PULL_DOWN)
96 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
97}
98
Rabin Vincent378be062010-06-02 06:06:29 +010099static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
100 unsigned offset)
101{
102 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
103}
104
Rabin Vincent6720db72010-09-02 11:28:48 +0100105static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
106 unsigned offset, int val)
107{
108 if (val)
109 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
110 else
111 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
112}
113
114static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
115 unsigned offset, int val)
116{
117 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
118 __nmk_gpio_set_output(nmk_chip, offset, val);
119}
120
Rabin Vincent378be062010-06-02 06:06:29 +0100121static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
122 pin_cfg_t cfg)
123{
124 static const char *afnames[] = {
125 [NMK_GPIO_ALT_GPIO] = "GPIO",
126 [NMK_GPIO_ALT_A] = "A",
127 [NMK_GPIO_ALT_B] = "B",
128 [NMK_GPIO_ALT_C] = "C"
129 };
130 static const char *pullnames[] = {
131 [NMK_GPIO_PULL_NONE] = "none",
132 [NMK_GPIO_PULL_UP] = "up",
133 [NMK_GPIO_PULL_DOWN] = "down",
134 [3] /* illegal */ = "??"
135 };
136 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100137 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
138 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100139 };
140
141 int pin = PIN_NUM(cfg);
142 int pull = PIN_PULL(cfg);
143 int af = PIN_ALT(cfg);
144 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100145 int output = PIN_DIR(cfg);
146 int val = PIN_VAL(cfg);
Rabin Vincent378be062010-06-02 06:06:29 +0100147
Rabin Vincent6720db72010-09-02 11:28:48 +0100148 dev_dbg(nmk_chip->chip.dev, "pin %d: af %s, pull %s, slpm %s (%s%s)\n",
149 pin, afnames[af], pullnames[pull], slpmnames[slpm],
150 output ? "output " : "input",
151 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100152
Rabin Vincent6720db72010-09-02 11:28:48 +0100153 if (output)
154 __nmk_gpio_make_output(nmk_chip, offset, val);
155 else {
156 __nmk_gpio_make_input(nmk_chip, offset);
157 __nmk_gpio_set_pull(nmk_chip, offset, pull);
158 }
159
Rabin Vincent378be062010-06-02 06:06:29 +0100160 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
161 __nmk_gpio_set_mode(nmk_chip, offset, af);
162}
163
164/**
165 * nmk_config_pin - configure a pin's mux attributes
166 * @cfg: pin confguration
167 *
168 * Configures a pin's mode (alternate function or GPIO), its pull up status,
169 * and its sleep mode based on the specified configuration. The @cfg is
170 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
171 * are constructed using, and can be further enhanced with, the macros in
172 * plat/pincfg.h.
173 *
174 * If a pin's mode is set to GPIO, it is configured as an input to avoid
175 * side-effects. The gpio can be manipulated later using standard GPIO API
176 * calls.
177 */
178int nmk_config_pin(pin_cfg_t cfg)
179{
180 struct nmk_gpio_chip *nmk_chip;
181 int gpio = PIN_NUM(cfg);
182 unsigned long flags;
183
184 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
185 if (!nmk_chip)
186 return -EINVAL;
187
188 spin_lock_irqsave(&nmk_chip->lock, flags);
189 __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg);
190 spin_unlock_irqrestore(&nmk_chip->lock, flags);
191
192 return 0;
193}
194EXPORT_SYMBOL(nmk_config_pin);
195
196/**
197 * nmk_config_pins - configure several pins at once
198 * @cfgs: array of pin configurations
199 * @num: number of elments in the array
200 *
201 * Configures several pins using nmk_config_pin(). Refer to that function for
202 * further information.
203 */
204int nmk_config_pins(pin_cfg_t *cfgs, int num)
205{
206 int ret = 0;
207 int i;
208
209 for (i = 0; i < num; i++) {
210 int ret = nmk_config_pin(cfgs[i]);
211 if (ret)
212 break;
213 }
214
215 return ret;
216}
217EXPORT_SYMBOL(nmk_config_pins);
218
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100219/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100220 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
221 * @gpio: pin number
222 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
223 *
224 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
225 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
226 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
227 * configured even when in sleep and deep sleep.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100228 *
229 * On DB8500v2 onwards, this setting loses the previous meaning and instead
230 * indicates if wakeup detection is enabled on the pin. Note that
231 * enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100232 */
233int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
234{
235 struct nmk_gpio_chip *nmk_chip;
236 unsigned long flags;
237
238 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
239 if (!nmk_chip)
240 return -EINVAL;
241
242 spin_lock_irqsave(&nmk_chip->lock, flags);
243 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
244 spin_unlock_irqrestore(&nmk_chip->lock, flags);
245
246 return 0;
247}
248
249/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100250 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
251 * @gpio: pin number
252 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
253 *
254 * Enables/disables pull up/down on a specified pin. This only takes effect if
255 * the pin is configured as an input (either explicitly or by the alternate
256 * function).
257 *
258 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
259 * configured as an input. Otherwise, due to the way the controller registers
260 * work, this function will change the value output on the pin.
261 */
262int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
263{
264 struct nmk_gpio_chip *nmk_chip;
265 unsigned long flags;
266
267 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
268 if (!nmk_chip)
269 return -EINVAL;
270
271 spin_lock_irqsave(&nmk_chip->lock, flags);
272 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
273 spin_unlock_irqrestore(&nmk_chip->lock, flags);
274
275 return 0;
276}
277
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100278/* Mode functions */
279int nmk_gpio_set_mode(int gpio, int gpio_mode)
280{
281 struct nmk_gpio_chip *nmk_chip;
282 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100283
284 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
285 if (!nmk_chip)
286 return -EINVAL;
287
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100288 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100289 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100290 spin_unlock_irqrestore(&nmk_chip->lock, flags);
291
292 return 0;
293}
294EXPORT_SYMBOL(nmk_gpio_set_mode);
295
296int nmk_gpio_get_mode(int gpio)
297{
298 struct nmk_gpio_chip *nmk_chip;
299 u32 afunc, bfunc, bit;
300
301 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
302 if (!nmk_chip)
303 return -EINVAL;
304
305 bit = 1 << (gpio - nmk_chip->chip.base);
306
307 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
308 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
309
310 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
311}
312EXPORT_SYMBOL(nmk_gpio_get_mode);
313
314
315/* IRQ functions */
316static inline int nmk_gpio_get_bitmask(int gpio)
317{
318 return 1 << (gpio % 32);
319}
320
321static void nmk_gpio_irq_ack(unsigned int irq)
322{
323 int gpio;
324 struct nmk_gpio_chip *nmk_chip;
325
326 gpio = NOMADIK_IRQ_TO_GPIO(irq);
327 nmk_chip = get_irq_chip_data(irq);
328 if (!nmk_chip)
329 return;
330 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
331}
332
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100333enum nmk_gpio_irq_type {
334 NORMAL,
335 WAKE,
336};
337
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100338static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100339 int gpio, enum nmk_gpio_irq_type which,
340 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100341{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100342 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
343 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100344 u32 bitmask = nmk_gpio_get_bitmask(gpio);
345 u32 reg;
346
347 /* we must individually set/clear the two edges */
348 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100349 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100350 if (enable)
351 reg |= bitmask;
352 else
353 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100354 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100355 }
356 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100357 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100358 if (enable)
359 reg |= bitmask;
360 else
361 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100362 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100363 }
364}
365
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100366static int nmk_gpio_irq_modify(unsigned int irq, enum nmk_gpio_irq_type which,
367 bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100368{
369 int gpio;
370 struct nmk_gpio_chip *nmk_chip;
371 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100372 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100373
374 gpio = NOMADIK_IRQ_TO_GPIO(irq);
375 nmk_chip = get_irq_chip_data(irq);
376 bitmask = nmk_gpio_get_bitmask(gpio);
377 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100378 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100379
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100380 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100381 __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100382 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100383
384 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100385}
386
387static void nmk_gpio_irq_mask(unsigned int irq)
388{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100389 nmk_gpio_irq_modify(irq, NORMAL, false);
390}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100391
392static void nmk_gpio_irq_unmask(unsigned int irq)
393{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100394 nmk_gpio_irq_modify(irq, NORMAL, true);
395}
396
397static int nmk_gpio_irq_set_wake(unsigned int irq, unsigned int on)
398{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100399 struct nmk_gpio_chip *nmk_chip;
400 unsigned long flags;
401 int gpio;
402
403 gpio = NOMADIK_IRQ_TO_GPIO(irq);
404 nmk_chip = get_irq_chip_data(irq);
405 if (!nmk_chip)
406 return -EINVAL;
407
408 spin_lock_irqsave(&nmk_chip->lock, flags);
409#ifdef CONFIG_ARCH_U8500
410 if (cpu_is_u8500v2()) {
411 __nmk_gpio_set_slpm(nmk_chip, gpio,
412 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
413 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
414 }
415#endif
416 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
417 spin_unlock_irqrestore(&nmk_chip->lock, flags);
418
419 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100420}
421
422static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
423{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100424 struct irq_desc *desc = irq_to_desc(irq);
425 bool enabled = !(desc->status & IRQ_DISABLED);
426 bool wake = desc->wake_depth;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100427 int gpio;
428 struct nmk_gpio_chip *nmk_chip;
429 unsigned long flags;
430 u32 bitmask;
431
432 gpio = NOMADIK_IRQ_TO_GPIO(irq);
433 nmk_chip = get_irq_chip_data(irq);
434 bitmask = nmk_gpio_get_bitmask(gpio);
435 if (!nmk_chip)
436 return -EINVAL;
437
438 if (type & IRQ_TYPE_LEVEL_HIGH)
439 return -EINVAL;
440 if (type & IRQ_TYPE_LEVEL_LOW)
441 return -EINVAL;
442
443 spin_lock_irqsave(&nmk_chip->lock, flags);
444
Rabin Vincent7a852d82010-05-06 10:43:55 +0100445 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100446 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
447
448 if (wake)
449 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100450
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100451 nmk_chip->edge_rising &= ~bitmask;
452 if (type & IRQ_TYPE_EDGE_RISING)
453 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100454
455 nmk_chip->edge_falling &= ~bitmask;
456 if (type & IRQ_TYPE_EDGE_FALLING)
457 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100458
459 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100460 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
461
462 if (wake)
463 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100464
465 spin_unlock_irqrestore(&nmk_chip->lock, flags);
466
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100467 return 0;
468}
469
470static struct irq_chip nmk_gpio_irq_chip = {
471 .name = "Nomadik-GPIO",
472 .ack = nmk_gpio_irq_ack,
473 .mask = nmk_gpio_irq_mask,
474 .unmask = nmk_gpio_irq_unmask,
475 .set_type = nmk_gpio_irq_set_type,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100476 .set_wake = nmk_gpio_irq_set_wake,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100477};
478
479static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
480{
481 struct nmk_gpio_chip *nmk_chip;
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100482 struct irq_chip *host_chip = get_irq_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100483 unsigned int gpio_irq;
484 u32 pending;
485 unsigned int first_irq;
486
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100487 if (host_chip->mask_ack)
488 host_chip->mask_ack(irq);
489 else {
490 host_chip->mask(irq);
491 if (host_chip->ack)
492 host_chip->ack(irq);
493 }
494
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100495 nmk_chip = get_irq_data(irq);
496 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
497 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
498 gpio_irq = first_irq + __ffs(pending);
499 generic_handle_irq(gpio_irq);
500 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100501
502 host_chip->unmask(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100503}
504
505static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
506{
507 unsigned int first_irq;
508 int i;
509
510 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
511 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
512 set_irq_chip(i, &nmk_gpio_irq_chip);
513 set_irq_handler(i, handle_edge_irq);
514 set_irq_flags(i, IRQF_VALID);
515 set_irq_chip_data(i, nmk_chip);
Rabin Vincent2210d642010-05-06 10:45:18 +0100516 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100517 }
518 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
519 set_irq_data(nmk_chip->parent_irq, nmk_chip);
520 return 0;
521}
522
523/* I/O Functions */
524static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
525{
526 struct nmk_gpio_chip *nmk_chip =
527 container_of(chip, struct nmk_gpio_chip, chip);
528
529 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
530 return 0;
531}
532
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100533static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
534{
535 struct nmk_gpio_chip *nmk_chip =
536 container_of(chip, struct nmk_gpio_chip, chip);
537 u32 bit = 1 << offset;
538
539 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
540}
541
542static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
543 int val)
544{
545 struct nmk_gpio_chip *nmk_chip =
546 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100547
Rabin Vincent6720db72010-09-02 11:28:48 +0100548 __nmk_gpio_set_output(nmk_chip, offset, val);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100549}
550
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100551static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
552 int val)
553{
554 struct nmk_gpio_chip *nmk_chip =
555 container_of(chip, struct nmk_gpio_chip, chip);
556
Rabin Vincent6720db72010-09-02 11:28:48 +0100557 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100558
559 return 0;
560}
561
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100562static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
563{
564 struct nmk_gpio_chip *nmk_chip =
565 container_of(chip, struct nmk_gpio_chip, chip);
566
567 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
568}
569
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100570/* This structure is replicated for each GPIO block allocated at probe time */
571static struct gpio_chip nmk_gpio_template = {
572 .direction_input = nmk_gpio_make_input,
573 .get = nmk_gpio_get_input,
574 .direction_output = nmk_gpio_make_output,
575 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100576 .to_irq = nmk_gpio_to_irq,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100577 .ngpio = NMK_GPIO_PER_CHIP,
578 .can_sleep = 0,
579};
580
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +0100581static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100582{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100583 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100584 struct nmk_gpio_chip *nmk_chip;
585 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100586 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100587 struct clk *clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100588 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100589 int ret;
590
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100591 if (!pdata)
592 return -ENODEV;
593
594 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
595 if (!res) {
596 ret = -ENOENT;
597 goto out;
598 }
599
600 irq = platform_get_irq(dev, 0);
601 if (irq < 0) {
602 ret = irq;
603 goto out;
604 }
605
606 if (request_mem_region(res->start, resource_size(res),
607 dev_name(&dev->dev)) == NULL) {
608 ret = -EBUSY;
609 goto out;
610 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100611
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100612 clk = clk_get(&dev->dev, NULL);
613 if (IS_ERR(clk)) {
614 ret = PTR_ERR(clk);
615 goto out_release;
616 }
617
618 clk_enable(clk);
619
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100620 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
621 if (!nmk_chip) {
622 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100623 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100624 }
625 /*
626 * The virt address in nmk_chip->addr is in the nomadik register space,
627 * so we can simply convert the resource address, without remapping
628 */
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100629 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100630 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100631 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100632 nmk_chip->parent_irq = irq;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +0100633 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100634
635 chip = &nmk_chip->chip;
636 chip->base = pdata->first_gpio;
637 chip->label = pdata->name;
638 chip->dev = &dev->dev;
639 chip->owner = THIS_MODULE;
640
641 ret = gpiochip_add(&nmk_chip->chip);
642 if (ret)
643 goto out_free;
644
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100645 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100646
647 nmk_gpio_init_irq(nmk_chip);
648
649 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
650 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
651 return 0;
652
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100653out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100654 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100655out_clk:
656 clk_disable(clk);
657 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100658out_release:
659 release_mem_region(res->start, resource_size(res));
660out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100661 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
662 pdata->first_gpio, pdata->first_gpio+31);
663 return ret;
664}
665
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100666static struct platform_driver nmk_gpio_driver = {
667 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100668 .owner = THIS_MODULE,
669 .name = "gpio",
670 },
671 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100672 .suspend = NULL, /* to be done */
673 .resume = NULL,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100674};
675
676static int __init nmk_gpio_init(void)
677{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100678 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100679}
680
Rabin Vincent33f45ea2010-06-02 06:09:52 +0100681core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100682
683MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
684MODULE_DESCRIPTION("Nomadik GPIO Driver");
685MODULE_LICENSE("GPL");
686
687