13 #include <linux/errno.h>
14 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
31 #include <mach/hardware.h>
47 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
49 static void at91_gpiolib_dbg_show(
struct seq_file *
s,
struct gpio_chip *
chip);
50 static void at91_gpiolib_set(
struct gpio_chip *
chip,
unsigned offset,
int val);
51 static int at91_gpiolib_get(
struct gpio_chip *
chip,
unsigned offset);
52 static int at91_gpiolib_direction_output(
struct gpio_chip *
chip,
54 static int at91_gpiolib_direction_input(
struct gpio_chip *
chip,
56 static int at91_gpiolib_to_irq(
struct gpio_chip *
chip,
unsigned offset);
58 #define AT91_GPIO_CHIP(name, nr_gpio) \
62 .direction_input = at91_gpiolib_direction_input, \
63 .direction_output = at91_gpiolib_direction_output, \
64 .get = at91_gpiolib_get, \
65 .set = at91_gpiolib_set, \
66 .dbg_show = at91_gpiolib_dbg_show, \
67 .to_irq = at91_gpiolib_to_irq, \
80 static int gpio_banks;
81 static unsigned long at91_gpio_caps;
84 #define AT91_GPIO_CAP_PIO3 (1 << 0)
86 #define has_pio3() (at91_gpio_caps & AT91_GPIO_CAP_PIO3)
90 static inline void __iomem *pin_to_controller(
unsigned pin)
93 if (
likely(pin < gpio_banks))
99 static inline unsigned pin_to_mask(
unsigned pin)
101 return 1 << (pin % 32);
145 void __iomem *pio = pin_to_controller(pin);
146 unsigned mask = pin_to_mask(pin);
163 void __iomem *pio = pin_to_controller(pin);
164 unsigned mask = pin_to_mask(pin);
190 void __iomem *pio = pin_to_controller(pin);
191 unsigned mask = pin_to_mask(pin);
217 void __iomem *pio = pin_to_controller(pin);
218 unsigned mask = pin_to_mask(pin);
238 void __iomem *pio = pin_to_controller(pin);
239 unsigned mask = pin_to_mask(pin);
260 void __iomem *pio = pin_to_controller(pin);
261 unsigned mask = pin_to_mask(pin);
281 void __iomem *pio = pin_to_controller(pin);
282 unsigned mask = pin_to_mask(pin);
302 void __iomem *pio = pin_to_controller(pin);
303 unsigned mask = pin_to_mask(pin);
320 void __iomem *pio = pin_to_controller(pin);
321 unsigned mask = pin_to_mask(pin);
343 void __iomem *pio = pin_to_controller(pin);
344 unsigned mask = pin_to_mask(pin);
360 void __iomem *pio = pin_to_controller(pin);
361 unsigned mask = pin_to_mask(pin);
378 void __iomem *pio = pin_to_controller(pin);
379 unsigned mask = pin_to_mask(pin);
394 void __iomem *pio = pin_to_controller(pin);
395 unsigned mask = pin_to_mask(pin);
410 void __iomem *pio = pin_to_controller(pin);
411 unsigned mask = pin_to_mask(pin);
417 return (pdsr & mask) != 0;
431 unsigned mask = 1 << d->
hwirq;
432 unsigned bank = at91_gpio->
pioc_idx;
438 wakeups[bank] |=
mask;
440 wakeups[bank] &= ~mask;
451 for (i = 0; i < gpio_banks; i++) {
462 #ifdef CONFIG_PM_DEBUG
473 for (i = 0; i < gpio_banks; i++) {
487 #define gpio_irq_set_wake NULL
505 static void gpio_irq_mask(
struct irq_data *d)
509 unsigned mask = 1 << d->
hwirq;
515 static void gpio_irq_unmask(
struct irq_data *d)
519 unsigned mask = 1 << d->
hwirq;
525 static int gpio_irq_type(
struct irq_data *d,
unsigned type)
537 static int alt_gpio_irq_type(
struct irq_data *d,
unsigned type)
541 unsigned mask = 1 << d->
hwirq;
579 static struct irq_chip gpio_irqchip = {
581 .irq_disable = gpio_irq_mask,
582 .irq_mask = gpio_irq_mask,
583 .irq_unmask = gpio_irq_unmask,
588 static void gpio_irq_handler(
unsigned irq,
struct irq_desc *
desc)
591 struct irq_data *idata = irq_desc_get_irq_data(desc);
592 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
597 chained_irq_enter(chip, desc);
605 if (!at91_gpio->
next)
607 at91_gpio = at91_gpio->
next;
618 chained_irq_exit(chip, desc);
624 #ifdef CONFIG_DEBUG_FS
626 static void gpio_printf(
struct seq_file *
s,
void __iomem *pio,
unsigned mask)
643 "rising" :
"falling";
646 seq_printf(s,
"IRQ:%s-%s\t", trigger, polarity);
659 for (bank = 0; bank < gpio_banks; bank++) {
665 for (j = 0; j < 32; j++) {
668 for (bank = 0; bank < gpio_banks; bank++) {
669 unsigned pin = (32 * bank) + j;
670 void __iomem *pio = pin_to_controller(pin);
671 unsigned mask = pin_to_mask(pin);
674 gpio_printf(s, pio, mask);
677 peripheral_function(pio, mask));
692 .
open = at91_gpio_open,
698 static int __init at91_gpio_debugfs_init(
void)
716 #if defined(CONFIG_OF)
717 static int at91_gpio_irq_map(
struct irq_domain *
h,
unsigned int virq,
722 irq_set_lockdep_class(virq, &gpio_lock_class);
728 irq_set_chip_and_handler(virq, &gpio_irqchip,
737 .
map = at91_gpio_irq_map,
759 &at91_gpio_ops, at91_gpio);
761 panic(
"at91_gpio.%d: couldn't allocate irq domain (DT).\n",
766 prev = &gpio_chip[at91_gpio->
pioc_idx - 1];
772 if (prev && prev->
next == at91_gpio)
778 irq_set_chained_handler(at91_gpio->
pioc_virq, gpio_irq_handler);
797 irq_base = irq_alloc_descs(-1, 0, at91_gpio->
chip.ngpio, 0);
799 panic(
"at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
805 panic(
"at91_gpio.%d: couldn't allocate irq domain.\n",
824 for (pioc = 0,
this = gpio_chip, prev =
NULL;
826 prev =
this,
this++) {
832 at91_gpio_irqdomain(
this);
834 for (offset = 0; offset < this->chip.ngpio; offset++) {
836 irq_set_lockdep_class(virq, &gpio_lock_class);
842 irq_set_chip_and_handler(virq, &gpio_irqchip,
854 if (prev && prev->
next ==
this)
859 irq_set_chained_handler(this->
pioc_virq, gpio_irq_handler);
861 pr_info(
"AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
865 static int at91_gpiolib_direction_input(
struct gpio_chip *chip,
870 unsigned mask = 1 <<
offset;
876 static int at91_gpiolib_direction_output(
struct gpio_chip *chip,
881 unsigned mask = 1 <<
offset;
888 static int at91_gpiolib_get(
struct gpio_chip *chip,
unsigned offset)
892 unsigned mask = 1 <<
offset;
896 return (pdsr & mask) != 0;
899 static void at91_gpiolib_set(
struct gpio_chip *chip,
unsigned offset,
int val)
903 unsigned mask = 1 <<
offset;
908 static void at91_gpiolib_dbg_show(
struct seq_file *s,
struct gpio_chip *chip)
912 for (i = 0; i < chip->ngpio; i++) {
913 unsigned pin = chip->base +
i;
914 void __iomem *pio = pin_to_controller(pin);
915 unsigned mask = pin_to_mask(pin);
916 const char *gpio_label;
921 gpio_label, chip->label, i);
928 peripheral_function(pio, mask));
933 static int at91_gpiolib_to_irq(
struct gpio_chip *chip,
unsigned offset)
943 dev_dbg(chip->dev,
"%s: request IRQ for GPIO %d, return %d\n",
944 chip->label, offset + chip->base, virq);
948 static int __init at91_gpio_setup_clk(
int idx)
954 if (IS_ERR(at91_gpio->
clock)) {
955 pr_err(
"at91_gpio.%d, failed to get clock, ignoring.\n", idx);
964 pr_err(
"at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
978 #ifdef CONFIG_OF_GPIO
989 pr_err(
"at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
994 at91_gpio = &gpio_chip[alias_idx];
995 at91_gpio->
chip.base = alias_idx * at91_gpio->
chip.ngpio;
999 pr_err(
"at91_gpio.%d, failed to map registers, ignoring.\n",
1005 if (of_property_read_u32(np,
"interrupts", &at91_gpio->
pioc_hwirq)) {
1006 pr_err(
"at91_gpio.%d, failed to get interrupts property, ignoring.\n",
1016 if (at91_gpio_setup_clk(alias_idx))
1019 at91_gpio->
chip.of_node = np;
1020 gpio_banks =
max(gpio_banks, alias_idx + 1);
1028 static int __init of_at91_gpio_init(
void)
1036 for_each_compatible_node(np,
NULL,
"atmel,at91rm9200-gpio")
1037 of_at91_gpio_init_one(np);
1042 static int __init of_at91_gpio_init(
void)
1048 static void __init at91_gpio_init_one(
int idx,
u32 regbase,
int pioc_hwirq)
1052 at91_gpio->
chip.base = idx * at91_gpio->
chip.ngpio;
1058 pr_err(
"at91_gpio.%d, failed to map registers, ignoring.\n", idx);
1062 if (at91_gpio_setup_clk(idx))
1065 gpio_banks =
max(gpio_banks, idx + 1);
1082 if (of_at91_gpio_init() < 0) {
1084 for (i = 0; i < nr_banks; i++)
1085 at91_gpio_init_one(i, data[i].regbase, data[i].
id);
1088 for (i = 0; i < gpio_banks; i++) {
1089 at91_gpio = &gpio_chip[
i];
1096 last->
next = at91_gpio;