14 #include <linux/module.h>
27 #include <linux/slab.h>
31 #include <mach/irqs.h>
51 #define GPLR_OFFSET 0x00
52 #define GPDR_OFFSET 0x0C
53 #define GPSR_OFFSET 0x18
54 #define GPCR_OFFSET 0x24
55 #define GRER_OFFSET 0x30
56 #define GFER_OFFSET 0x3C
57 #define GEDR_OFFSET 0x48
58 #define GAFR_OFFSET 0x54
59 #define ED_MASK_OFFSET 0x9C
61 #define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
82 unsigned long saved_gplr;
83 unsigned long saved_gpdr;
84 unsigned long saved_grer;
85 unsigned long saved_gfer;
101 static void __iomem *gpio_reg_base;
103 #define for_each_gpio_chip(i, c) \
104 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
106 static inline void __iomem *gpio_chip_base(
struct gpio_chip *
c)
116 static inline int gpio_is_pxa_type(
int type)
121 static inline int gpio_is_mmp_type(
int type)
129 static inline int __gpio_is_inverted(
int gpio)
142 static inline int __gpio_is_occupied(
unsigned gpio)
146 unsigned long gafr = 0, gpdr = 0;
149 pxachip = gpio_to_pxachip(gpio);
150 base = gpio_chip_base(&pxachip->
chip);
158 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
161 if (__gpio_is_inverted(gpio))
162 ret = (af != 1) || (dir == 0);
164 ret = (af != 0) || (dir != 0);
173 static int pxa_gpio_to_irq(
struct gpio_chip *
chip,
unsigned offset)
175 return chip->base + offset + irq_base;
180 return irq - irq_base;
183 static int pxa_gpio_direction_input(
struct gpio_chip *
chip,
unsigned offset)
185 void __iomem *base = gpio_chip_base(chip);
192 if (__gpio_is_inverted(chip->base + offset))
198 spin_unlock_irqrestore(&gpio_lock, flags);
202 static int pxa_gpio_direction_output(
struct gpio_chip *chip,
203 unsigned offset,
int value)
205 void __iomem *base = gpio_chip_base(chip);
214 if (__gpio_is_inverted(chip->base + offset))
220 spin_unlock_irqrestore(&gpio_lock, flags);
224 static int pxa_gpio_get(
struct gpio_chip *chip,
unsigned offset)
229 static void pxa_gpio_set(
struct gpio_chip *chip,
unsigned offset,
int value)
231 writel_relaxed(1 << offset, gpio_chip_base(chip) +
235 #ifdef CONFIG_OF_GPIO
236 static int pxa_gpio_of_xlate(
struct gpio_chip *
gc,
243 if (gc != &pxa_gpio_chips[gpiospec->
args[0] / 32].
chip)
247 *flags = gpiospec->
args[1];
249 return gpiospec->
args[0] % 32;
253 static int __devinit pxa_init_gpio_chip(
int gpio_end,
254 int (*
set_wake)(
unsigned int,
unsigned int))
261 pr_err(
"%s: failed to allocate GPIO chips\n", __func__);
265 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
266 struct gpio_chip *
c = &chips[
i].
chip;
273 c->label = chips[
i].
label;
275 c->direction_input = pxa_gpio_direction_input;
276 c->direction_output = pxa_gpio_direction_output;
277 c->get = pxa_gpio_get;
278 c->set = pxa_gpio_set;
279 c->to_irq = pxa_gpio_to_irq;
280 #ifdef CONFIG_OF_GPIO
281 c->of_node = pxa_gpio_of_node;
282 c->of_xlate = pxa_gpio_of_xlate;
283 c->of_gpio_n_cells = 2;
287 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
290 pxa_gpio_chips =
chips;
309 static int pxa_gpio_irq_type(
struct irq_data *
d,
unsigned int type)
313 unsigned long gpdr, mask =
GPIO_bit(gpio);
315 c = gpio_to_pxachip(gpio);
324 if (__gpio_is_occupied(gpio))
332 if (__gpio_is_inverted(gpio))
347 update_edge_detect(c);
349 pr_debug(
"%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->
irq, gpio,
350 ((type & IRQ_TYPE_EDGE_RISING) ?
" rising" :
""),
351 ((type & IRQ_TYPE_EDGE_FALLING) ?
" falling" :
""));
355 static void pxa_gpio_demux_handler(
unsigned int irq,
struct irq_desc *
desc)
360 struct irq_chip *chip = irq_desc_get_chip(desc);
362 chained_irq_enter(chip, desc);
367 gpio_base = c->
chip.base;
381 chained_irq_exit(chip, desc);
384 static void pxa_ack_muxed_gpio(
struct irq_data *d)
392 static void pxa_mask_muxed_gpio(
struct irq_data *d)
406 static int pxa_gpio_set_wake(
struct irq_data *d,
unsigned int on)
417 static void pxa_unmask_muxed_gpio(
struct irq_data *d)
423 update_edge_detect(c);
426 static struct irq_chip pxa_muxed_gpio_chip = {
428 .irq_ack = pxa_ack_muxed_gpio,
429 .irq_mask = pxa_mask_muxed_gpio,
430 .irq_unmask = pxa_unmask_muxed_gpio,
431 .irq_set_type = pxa_gpio_irq_type,
432 .irq_set_wake = pxa_gpio_set_wake,
435 static int pxa_gpio_nums(
void)
439 #ifdef CONFIG_ARCH_PXA
441 #ifdef CONFIG_CPU_PXA26x
444 #elif defined(CONFIG_PXA25x)
460 #ifdef CONFIG_ARCH_MMP
475 { .compatible =
"mrvl,mmp-gpio", .data = (
void *)
MMP_GPIO },
479 static int pxa_irq_domain_map(
struct irq_domain *d,
unsigned int irq,
482 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
489 .
map = pxa_irq_domain_map,
495 int ret, nr_banks, nr_gpios;
501 dev_err(&pdev->
dev,
"Failed to find gpio controller\n");
509 dev_err(&pdev->
dev,
"Failed to find child gpio node\n");
513 for (nr_banks = 1; ; nr_banks++) {
520 nr_gpios = nr_banks << 5;
523 irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
525 dev_err(&pdev->
dev,
"Failed to allocate IRQ numbers\n");
529 &pxa_irq_domain_ops,
NULL);
530 pxa_gpio_of_node = np;
537 #define pxa_gpio_probe_dt(pdev) (-1)
546 int gpio, irq,
ret, use_of = 0;
547 int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
552 #ifdef CONFIG_ARCH_PXA
556 #ifdef CONFIG_ARCH_MMP
570 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
576 gpio_reg_base =
ioremap(res->
start, resource_size(res));
585 dev_err(&pdev->
dev,
"Error %ld to get gpio clock\n",
590 ret = clk_prepare_enable(clk);
598 info = dev_get_platdata(&pdev->
dev);
612 #ifdef CONFIG_ARCH_PXA
614 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
617 irq_set_chained_handler(
IRQ_GPIO0, pxa_gpio_demux_handler);
620 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
623 irq_set_chained_handler(
IRQ_GPIO1, pxa_gpio_demux_handler);
628 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
634 irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
639 .probe = pxa_gpio_probe,
646 static int __init pxa_gpio_init(
void)
686 #define pxa_gpio_suspend NULL
687 #define pxa_gpio_resume NULL
695 static int __init pxa_gpio_sysinit(
void)