plat-nomadik: add custom dbg_show for GPIO
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / plat-nomadik / gpio.c
blob5b5fd1ee3c044510e473d80ec77885f494a5a40a
1 /*
2 * Generic GPIO driver for logic cells found in the Nomadik SoC
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>
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/io.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/gpio.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
26 #include <plat/pincfg.h>
27 #include <mach/hardware.h>
28 #include <mach/gpio.h>
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.
35 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
38 struct nmk_gpio_chip {
39 struct gpio_chip chip;
40 void __iomem *addr;
41 struct clk *clk;
42 unsigned int parent_irq;
43 spinlock_t lock;
44 /* Keep track of configured edges */
45 u32 edge_rising;
46 u32 edge_falling;
49 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
50 unsigned offset, int gpio_mode)
52 u32 bit = 1 << offset;
53 u32 afunc, bfunc;
55 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
56 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
57 if (gpio_mode & NMK_GPIO_ALT_A)
58 afunc |= bit;
59 if (gpio_mode & NMK_GPIO_ALT_B)
60 bfunc |= bit;
61 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
62 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
65 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
66 unsigned offset, enum nmk_gpio_slpm mode)
68 u32 bit = 1 << offset;
69 u32 slpm;
71 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
72 if (mode == NMK_GPIO_SLPM_NOCHANGE)
73 slpm |= bit;
74 else
75 slpm &= ~bit;
76 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
79 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
80 unsigned offset, enum nmk_gpio_pull pull)
82 u32 bit = 1 << offset;
83 u32 pdis;
85 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
86 if (pull == NMK_GPIO_PULL_NONE)
87 pdis |= bit;
88 else
89 pdis &= ~bit;
90 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
92 if (pull == NMK_GPIO_PULL_UP)
93 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
94 else if (pull == NMK_GPIO_PULL_DOWN)
95 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
98 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
99 unsigned offset)
101 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
104 static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
105 unsigned offset, int val)
107 if (val)
108 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
109 else
110 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
113 static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
114 unsigned offset, int val)
116 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
117 __nmk_gpio_set_output(nmk_chip, offset, val);
120 static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
121 pin_cfg_t cfg, bool sleep)
123 static const char *afnames[] = {
124 [NMK_GPIO_ALT_GPIO] = "GPIO",
125 [NMK_GPIO_ALT_A] = "A",
126 [NMK_GPIO_ALT_B] = "B",
127 [NMK_GPIO_ALT_C] = "C"
129 static const char *pullnames[] = {
130 [NMK_GPIO_PULL_NONE] = "none",
131 [NMK_GPIO_PULL_UP] = "up",
132 [NMK_GPIO_PULL_DOWN] = "down",
133 [3] /* illegal */ = "??"
135 static const char *slpmnames[] = {
136 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
137 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
140 int pin = PIN_NUM(cfg);
141 int pull = PIN_PULL(cfg);
142 int af = PIN_ALT(cfg);
143 int slpm = PIN_SLPM(cfg);
144 int output = PIN_DIR(cfg);
145 int val = PIN_VAL(cfg);
147 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
148 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
149 output ? "output " : "input",
150 output ? (val ? "high" : "low") : "");
152 if (sleep) {
153 int slpm_pull = PIN_SLPM_PULL(cfg);
154 int slpm_output = PIN_SLPM_DIR(cfg);
155 int slpm_val = PIN_SLPM_VAL(cfg);
158 * The SLPM_* values are normal values + 1 to allow zero to
159 * mean "same as normal".
161 if (slpm_pull)
162 pull = slpm_pull - 1;
163 if (slpm_output)
164 output = slpm_output - 1;
165 if (slpm_val)
166 val = slpm_val - 1;
168 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
169 pin,
170 slpm_pull ? pullnames[pull] : "same",
171 slpm_output ? (output ? "output" : "input") : "same",
172 slpm_val ? (val ? "high" : "low") : "same");
175 if (output)
176 __nmk_gpio_make_output(nmk_chip, offset, val);
177 else {
178 __nmk_gpio_make_input(nmk_chip, offset);
179 __nmk_gpio_set_pull(nmk_chip, offset, pull);
182 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
183 __nmk_gpio_set_mode(nmk_chip, offset, af);
187 * nmk_config_pin - configure a pin's mux attributes
188 * @cfg: pin confguration
190 * Configures a pin's mode (alternate function or GPIO), its pull up status,
191 * and its sleep mode based on the specified configuration. The @cfg is
192 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
193 * are constructed using, and can be further enhanced with, the macros in
194 * plat/pincfg.h.
196 * If a pin's mode is set to GPIO, it is configured as an input to avoid
197 * side-effects. The gpio can be manipulated later using standard GPIO API
198 * calls.
200 int nmk_config_pin(pin_cfg_t cfg, bool sleep)
202 struct nmk_gpio_chip *nmk_chip;
203 int gpio = PIN_NUM(cfg);
204 unsigned long flags;
206 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
207 if (!nmk_chip)
208 return -EINVAL;
210 spin_lock_irqsave(&nmk_chip->lock, flags);
211 __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg, sleep);
212 spin_unlock_irqrestore(&nmk_chip->lock, flags);
214 return 0;
216 EXPORT_SYMBOL(nmk_config_pin);
219 * nmk_config_pins - configure several pins at once
220 * @cfgs: array of pin configurations
221 * @num: number of elments in the array
223 * Configures several pins using nmk_config_pin(). Refer to that function for
224 * further information.
226 int nmk_config_pins(pin_cfg_t *cfgs, int num)
228 int ret = 0;
229 int i;
231 for (i = 0; i < num; i++) {
232 ret = nmk_config_pin(cfgs[i], false);
233 if (ret)
234 break;
237 return ret;
239 EXPORT_SYMBOL(nmk_config_pins);
241 int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
243 int ret = 0;
244 int i;
246 for (i = 0; i < num; i++) {
247 ret = nmk_config_pin(cfgs[i], true);
248 if (ret)
249 break;
252 return ret;
254 EXPORT_SYMBOL(nmk_config_pins_sleep);
257 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
258 * @gpio: pin number
259 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
261 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
262 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
263 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
264 * configured even when in sleep and deep sleep.
266 * On DB8500v2 onwards, this setting loses the previous meaning and instead
267 * indicates if wakeup detection is enabled on the pin. Note that
268 * enable_irq_wake() will automatically enable wakeup detection.
270 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
272 struct nmk_gpio_chip *nmk_chip;
273 unsigned long flags;
275 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
276 if (!nmk_chip)
277 return -EINVAL;
279 spin_lock_irqsave(&nmk_chip->lock, flags);
280 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
281 spin_unlock_irqrestore(&nmk_chip->lock, flags);
283 return 0;
287 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
288 * @gpio: pin number
289 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
291 * Enables/disables pull up/down on a specified pin. This only takes effect if
292 * the pin is configured as an input (either explicitly or by the alternate
293 * function).
295 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
296 * configured as an input. Otherwise, due to the way the controller registers
297 * work, this function will change the value output on the pin.
299 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
301 struct nmk_gpio_chip *nmk_chip;
302 unsigned long flags;
304 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
305 if (!nmk_chip)
306 return -EINVAL;
308 spin_lock_irqsave(&nmk_chip->lock, flags);
309 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
310 spin_unlock_irqrestore(&nmk_chip->lock, flags);
312 return 0;
315 /* Mode functions */
316 int nmk_gpio_set_mode(int gpio, int gpio_mode)
318 struct nmk_gpio_chip *nmk_chip;
319 unsigned long flags;
321 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
322 if (!nmk_chip)
323 return -EINVAL;
325 spin_lock_irqsave(&nmk_chip->lock, flags);
326 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
327 spin_unlock_irqrestore(&nmk_chip->lock, flags);
329 return 0;
331 EXPORT_SYMBOL(nmk_gpio_set_mode);
333 int nmk_gpio_get_mode(int gpio)
335 struct nmk_gpio_chip *nmk_chip;
336 u32 afunc, bfunc, bit;
338 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
339 if (!nmk_chip)
340 return -EINVAL;
342 bit = 1 << (gpio - nmk_chip->chip.base);
344 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
345 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
347 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
349 EXPORT_SYMBOL(nmk_gpio_get_mode);
352 /* IRQ functions */
353 static inline int nmk_gpio_get_bitmask(int gpio)
355 return 1 << (gpio % 32);
358 static void nmk_gpio_irq_ack(struct irq_data *d)
360 int gpio;
361 struct nmk_gpio_chip *nmk_chip;
363 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
364 nmk_chip = irq_data_get_irq_chip_data(d);
365 if (!nmk_chip)
366 return;
367 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
370 enum nmk_gpio_irq_type {
371 NORMAL,
372 WAKE,
375 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
376 int gpio, enum nmk_gpio_irq_type which,
377 bool enable)
379 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
380 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
381 u32 bitmask = nmk_gpio_get_bitmask(gpio);
382 u32 reg;
384 /* we must individually set/clear the two edges */
385 if (nmk_chip->edge_rising & bitmask) {
386 reg = readl(nmk_chip->addr + rimsc);
387 if (enable)
388 reg |= bitmask;
389 else
390 reg &= ~bitmask;
391 writel(reg, nmk_chip->addr + rimsc);
393 if (nmk_chip->edge_falling & bitmask) {
394 reg = readl(nmk_chip->addr + fimsc);
395 if (enable)
396 reg |= bitmask;
397 else
398 reg &= ~bitmask;
399 writel(reg, nmk_chip->addr + fimsc);
403 static int nmk_gpio_irq_modify(struct irq_data *d, enum nmk_gpio_irq_type which,
404 bool enable)
406 int gpio;
407 struct nmk_gpio_chip *nmk_chip;
408 unsigned long flags;
409 u32 bitmask;
411 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
412 nmk_chip = irq_data_get_irq_chip_data(d);
413 bitmask = nmk_gpio_get_bitmask(gpio);
414 if (!nmk_chip)
415 return -EINVAL;
417 spin_lock_irqsave(&nmk_chip->lock, flags);
418 __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
419 spin_unlock_irqrestore(&nmk_chip->lock, flags);
421 return 0;
424 static void nmk_gpio_irq_mask(struct irq_data *d)
426 nmk_gpio_irq_modify(d, NORMAL, false);
429 static void nmk_gpio_irq_unmask(struct irq_data *d)
431 nmk_gpio_irq_modify(d, NORMAL, true);
434 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
436 struct nmk_gpio_chip *nmk_chip;
437 unsigned long flags;
438 int gpio;
440 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
441 nmk_chip = irq_data_get_irq_chip_data(d);
442 if (!nmk_chip)
443 return -EINVAL;
445 spin_lock_irqsave(&nmk_chip->lock, flags);
446 #ifdef CONFIG_ARCH_U8500
447 if (cpu_is_u8500v2()) {
448 __nmk_gpio_set_slpm(nmk_chip, gpio,
449 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
450 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
452 #endif
453 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
454 spin_unlock_irqrestore(&nmk_chip->lock, flags);
456 return 0;
459 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
461 struct irq_desc *desc = irq_to_desc(d->irq);
462 bool enabled = !(desc->status & IRQ_DISABLED);
463 bool wake = desc->wake_depth;
464 int gpio;
465 struct nmk_gpio_chip *nmk_chip;
466 unsigned long flags;
467 u32 bitmask;
469 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
470 nmk_chip = irq_data_get_irq_chip_data(d);
471 bitmask = nmk_gpio_get_bitmask(gpio);
472 if (!nmk_chip)
473 return -EINVAL;
475 if (type & IRQ_TYPE_LEVEL_HIGH)
476 return -EINVAL;
477 if (type & IRQ_TYPE_LEVEL_LOW)
478 return -EINVAL;
480 spin_lock_irqsave(&nmk_chip->lock, flags);
482 if (enabled)
483 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
485 if (wake)
486 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
488 nmk_chip->edge_rising &= ~bitmask;
489 if (type & IRQ_TYPE_EDGE_RISING)
490 nmk_chip->edge_rising |= bitmask;
492 nmk_chip->edge_falling &= ~bitmask;
493 if (type & IRQ_TYPE_EDGE_FALLING)
494 nmk_chip->edge_falling |= bitmask;
496 if (enabled)
497 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
499 if (wake)
500 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
502 spin_unlock_irqrestore(&nmk_chip->lock, flags);
504 return 0;
507 static struct irq_chip nmk_gpio_irq_chip = {
508 .name = "Nomadik-GPIO",
509 .irq_ack = nmk_gpio_irq_ack,
510 .irq_mask = nmk_gpio_irq_mask,
511 .irq_unmask = nmk_gpio_irq_unmask,
512 .irq_set_type = nmk_gpio_irq_set_type,
513 .irq_set_wake = nmk_gpio_irq_set_wake,
516 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
518 struct nmk_gpio_chip *nmk_chip;
519 struct irq_chip *host_chip = get_irq_chip(irq);
520 unsigned int gpio_irq;
521 u32 pending;
522 unsigned int first_irq;
524 if (host_chip->irq_mask_ack)
525 host_chip->irq_mask_ack(&desc->irq_data);
526 else {
527 host_chip->irq_mask(&desc->irq_data);
528 if (host_chip->irq_ack)
529 host_chip->irq_ack(&desc->irq_data);
532 nmk_chip = get_irq_data(irq);
533 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
534 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
535 gpio_irq = first_irq + __ffs(pending);
536 generic_handle_irq(gpio_irq);
539 host_chip->irq_unmask(&desc->irq_data);
542 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
544 unsigned int first_irq;
545 int i;
547 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
548 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
549 set_irq_chip(i, &nmk_gpio_irq_chip);
550 set_irq_handler(i, handle_edge_irq);
551 set_irq_flags(i, IRQF_VALID);
552 set_irq_chip_data(i, nmk_chip);
553 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
555 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
556 set_irq_data(nmk_chip->parent_irq, nmk_chip);
557 return 0;
560 /* I/O Functions */
561 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
563 struct nmk_gpio_chip *nmk_chip =
564 container_of(chip, struct nmk_gpio_chip, chip);
566 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
567 return 0;
570 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
572 struct nmk_gpio_chip *nmk_chip =
573 container_of(chip, struct nmk_gpio_chip, chip);
574 u32 bit = 1 << offset;
576 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
579 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
580 int val)
582 struct nmk_gpio_chip *nmk_chip =
583 container_of(chip, struct nmk_gpio_chip, chip);
585 __nmk_gpio_set_output(nmk_chip, offset, val);
588 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
589 int val)
591 struct nmk_gpio_chip *nmk_chip =
592 container_of(chip, struct nmk_gpio_chip, chip);
594 __nmk_gpio_make_output(nmk_chip, offset, val);
596 return 0;
599 static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
601 struct nmk_gpio_chip *nmk_chip =
602 container_of(chip, struct nmk_gpio_chip, chip);
604 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
607 #ifdef CONFIG_DEBUG_FS
609 #include <linux/seq_file.h>
611 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
613 int mode;
614 unsigned i;
615 unsigned gpio = chip->base;
616 int is_out;
617 struct nmk_gpio_chip *nmk_chip =
618 container_of(chip, struct nmk_gpio_chip, chip);
619 const char *modes[] = {
620 [NMK_GPIO_ALT_GPIO] = "gpio",
621 [NMK_GPIO_ALT_A] = "altA",
622 [NMK_GPIO_ALT_B] = "altB",
623 [NMK_GPIO_ALT_C] = "altC",
626 for (i = 0; i < chip->ngpio; i++, gpio++) {
627 const char *label = gpiochip_is_requested(chip, i);
628 bool pull;
629 u32 bit = 1 << i;
631 if (!label)
632 continue;
634 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
635 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
636 mode = nmk_gpio_get_mode(gpio);
637 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
638 gpio, label,
639 is_out ? "out" : "in ",
640 chip->get
641 ? (chip->get(chip, i) ? "hi" : "lo")
642 : "? ",
643 (mode < 0) ? "unknown" : modes[mode],
644 pull ? "pull" : "none");
646 if (!is_out) {
647 int irq = gpio_to_irq(gpio);
648 struct irq_desc *desc = irq_to_desc(irq);
650 /* This races with request_irq(), set_irq_type(),
651 * and set_irq_wake() ... but those are "rare".
653 * More significantly, trigger type flags aren't
654 * currently maintained by genirq.
656 if (irq >= 0 && desc->action) {
657 char *trigger;
659 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
660 case IRQ_TYPE_NONE:
661 trigger = "(default)";
662 break;
663 case IRQ_TYPE_EDGE_FALLING:
664 trigger = "edge-falling";
665 break;
666 case IRQ_TYPE_EDGE_RISING:
667 trigger = "edge-rising";
668 break;
669 case IRQ_TYPE_EDGE_BOTH:
670 trigger = "edge-both";
671 break;
672 case IRQ_TYPE_LEVEL_HIGH:
673 trigger = "level-high";
674 break;
675 case IRQ_TYPE_LEVEL_LOW:
676 trigger = "level-low";
677 break;
678 default:
679 trigger = "?trigger?";
680 break;
683 seq_printf(s, " irq-%d %s%s",
684 irq, trigger,
685 (desc->status & IRQ_WAKEUP)
686 ? " wakeup" : "");
690 seq_printf(s, "\n");
694 #else
695 #define nmk_gpio_dbg_show NULL
696 #endif
698 /* This structure is replicated for each GPIO block allocated at probe time */
699 static struct gpio_chip nmk_gpio_template = {
700 .direction_input = nmk_gpio_make_input,
701 .get = nmk_gpio_get_input,
702 .direction_output = nmk_gpio_make_output,
703 .set = nmk_gpio_set_output,
704 .to_irq = nmk_gpio_to_irq,
705 .dbg_show = nmk_gpio_dbg_show,
706 .can_sleep = 0,
709 static int __devinit nmk_gpio_probe(struct platform_device *dev)
711 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
712 struct nmk_gpio_chip *nmk_chip;
713 struct gpio_chip *chip;
714 struct resource *res;
715 struct clk *clk;
716 int irq;
717 int ret;
719 if (!pdata)
720 return -ENODEV;
722 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
723 if (!res) {
724 ret = -ENOENT;
725 goto out;
728 irq = platform_get_irq(dev, 0);
729 if (irq < 0) {
730 ret = irq;
731 goto out;
734 if (request_mem_region(res->start, resource_size(res),
735 dev_name(&dev->dev)) == NULL) {
736 ret = -EBUSY;
737 goto out;
740 clk = clk_get(&dev->dev, NULL);
741 if (IS_ERR(clk)) {
742 ret = PTR_ERR(clk);
743 goto out_release;
746 clk_enable(clk);
748 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
749 if (!nmk_chip) {
750 ret = -ENOMEM;
751 goto out_clk;
754 * The virt address in nmk_chip->addr is in the nomadik register space,
755 * so we can simply convert the resource address, without remapping
757 nmk_chip->clk = clk;
758 nmk_chip->addr = io_p2v(res->start);
759 nmk_chip->chip = nmk_gpio_template;
760 nmk_chip->parent_irq = irq;
761 spin_lock_init(&nmk_chip->lock);
763 chip = &nmk_chip->chip;
764 chip->base = pdata->first_gpio;
765 chip->ngpio = pdata->num_gpio;
766 chip->label = pdata->name ?: dev_name(&dev->dev);
767 chip->dev = &dev->dev;
768 chip->owner = THIS_MODULE;
770 ret = gpiochip_add(&nmk_chip->chip);
771 if (ret)
772 goto out_free;
774 platform_set_drvdata(dev, nmk_chip);
776 nmk_gpio_init_irq(nmk_chip);
778 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
779 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
780 return 0;
782 out_free:
783 kfree(nmk_chip);
784 out_clk:
785 clk_disable(clk);
786 clk_put(clk);
787 out_release:
788 release_mem_region(res->start, resource_size(res));
789 out:
790 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
791 pdata->first_gpio, pdata->first_gpio+31);
792 return ret;
795 static struct platform_driver nmk_gpio_driver = {
796 .driver = {
797 .owner = THIS_MODULE,
798 .name = "gpio",
800 .probe = nmk_gpio_probe,
801 .suspend = NULL, /* to be done */
802 .resume = NULL,
805 static int __init nmk_gpio_init(void)
807 return platform_driver_register(&nmk_gpio_driver);
810 core_initcall(nmk_gpio_init);
812 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
813 MODULE_DESCRIPTION("Nomadik GPIO Driver");
814 MODULE_LICENSE("GPL");