Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Atmel PIO2 Port Multiplexer support |
| 3 | * |
| 4 | * Copyright (C) 2004-2006 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/debugfs.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/platform_device.h> |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 15 | #include <linux/irq.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 16 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | |
Haavard Skinnemoen | 3663b73 | 2008-08-05 13:57:38 +0200 | [diff] [blame] | 20 | #include <mach/portmux.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 21 | |
| 22 | #include "pio.h" |
| 23 | |
| 24 | #define MAX_NR_PIO_DEVICES 8 |
| 25 | |
| 26 | struct pio_device { |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 27 | struct gpio_chip chip; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 28 | void __iomem *regs; |
| 29 | const struct platform_device *pdev; |
| 30 | struct clk *clk; |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 31 | u32 pinmux_mask; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 32 | char name[8]; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct pio_device pio_dev[MAX_NR_PIO_DEVICES]; |
| 36 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 37 | static struct pio_device *gpio_to_pio(unsigned int gpio) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 38 | { |
| 39 | struct pio_device *pio; |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 40 | unsigned int index; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 41 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 42 | index = gpio >> 5; |
| 43 | if (index >= MAX_NR_PIO_DEVICES) |
| 44 | return NULL; |
| 45 | pio = &pio_dev[index]; |
| 46 | if (!pio->regs) |
| 47 | return NULL; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 48 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 49 | return pio; |
| 50 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 51 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 52 | /* Pin multiplexing API */ |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 53 | static DEFINE_SPINLOCK(pio_lock); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 54 | |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 55 | void __init at32_select_periph(unsigned int port, u32 pin_mask, |
| 56 | unsigned int periph, unsigned long flags) |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 57 | { |
| 58 | struct pio_device *pio; |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 59 | |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 60 | /* assign and verify pio */ |
| 61 | pio = gpio_to_pio(port); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 62 | if (unlikely(!pio)) { |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 63 | printk(KERN_WARNING "pio: invalid port %u\n", port); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 64 | goto fail; |
| 65 | } |
| 66 | |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 67 | /* Test if any of the requested pins is already muxed */ |
| 68 | spin_lock(&pio_lock); |
| 69 | if (unlikely(pio->pinmux_mask & pin_mask)) { |
| 70 | printk(KERN_WARNING "%s: pin(s) busy (requested 0x%x, busy 0x%x)\n", |
| 71 | pio->name, pin_mask, pio->pinmux_mask & pin_mask); |
| 72 | spin_unlock(&pio_lock); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 73 | goto fail; |
| 74 | } |
| 75 | |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 76 | pio->pinmux_mask |= pin_mask; |
| 77 | |
| 78 | /* enable pull ups */ |
| 79 | pio_writel(pio, PUER, pin_mask); |
| 80 | |
| 81 | /* select either peripheral A or B */ |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 82 | if (periph) |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 83 | pio_writel(pio, BSR, pin_mask); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 84 | else |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 85 | pio_writel(pio, ASR, pin_mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 86 | |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 87 | /* enable peripheral control */ |
| 88 | pio_writel(pio, PDR, pin_mask); |
| 89 | |
| 90 | /* Disable pull ups if not requested. */ |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 91 | if (!(flags & AT32_GPIOF_PULLUP)) |
Julien May | caf18f1 | 2008-09-24 10:30:47 +0200 | [diff] [blame] | 92 | pio_writel(pio, PUDR, pin_mask); |
| 93 | |
| 94 | spin_unlock(&pio_lock); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 95 | |
| 96 | return; |
| 97 | |
| 98 | fail: |
| 99 | dump_stack(); |
| 100 | } |
| 101 | |
| 102 | void __init at32_select_gpio(unsigned int pin, unsigned long flags) |
| 103 | { |
| 104 | struct pio_device *pio; |
| 105 | unsigned int pin_index = pin & 0x1f; |
| 106 | u32 mask = 1 << pin_index; |
| 107 | |
| 108 | pio = gpio_to_pio(pin); |
| 109 | if (unlikely(!pio)) { |
| 110 | printk("pio: invalid pin %u\n", pin); |
| 111 | goto fail; |
| 112 | } |
| 113 | |
| 114 | if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) { |
| 115 | printk("%s: pin %u is busy\n", pio->name, pin_index); |
| 116 | goto fail; |
| 117 | } |
| 118 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 119 | if (flags & AT32_GPIOF_OUTPUT) { |
| 120 | if (flags & AT32_GPIOF_HIGH) |
| 121 | pio_writel(pio, SODR, mask); |
| 122 | else |
| 123 | pio_writel(pio, CODR, mask); |
Matteo Vit | 7808fa4 | 2007-08-09 14:55:34 +0200 | [diff] [blame] | 124 | if (flags & AT32_GPIOF_MULTIDRV) |
| 125 | pio_writel(pio, MDER, mask); |
| 126 | else |
| 127 | pio_writel(pio, MDDR, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 128 | pio_writel(pio, PUDR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 129 | pio_writel(pio, OER, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 130 | } else { |
| 131 | if (flags & AT32_GPIOF_PULLUP) |
| 132 | pio_writel(pio, PUER, mask); |
| 133 | else |
| 134 | pio_writel(pio, PUDR, mask); |
| 135 | if (flags & AT32_GPIOF_DEGLITCH) |
| 136 | pio_writel(pio, IFER, mask); |
| 137 | else |
| 138 | pio_writel(pio, IFDR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 139 | pio_writel(pio, ODR, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 140 | } |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 141 | |
| 142 | pio_writel(pio, PER, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 143 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 144 | return; |
| 145 | |
| 146 | fail: |
| 147 | dump_stack(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Haavard Skinnemoen | ece2678 | 2008-09-19 17:13:28 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Undo a previous pin reservation. Will not affect the hardware |
| 152 | * configuration. |
| 153 | */ |
| 154 | void at32_deselect_pin(unsigned int pin) |
| 155 | { |
| 156 | struct pio_device *pio; |
| 157 | unsigned int pin_index = pin & 0x1f; |
| 158 | |
| 159 | pio = gpio_to_pio(pin); |
| 160 | if (unlikely(!pio)) { |
| 161 | printk("pio: invalid pin %u\n", pin); |
| 162 | dump_stack(); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | clear_bit(pin_index, &pio->pinmux_mask); |
| 167 | } |
| 168 | |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 169 | /* Reserve a pin, preventing anyone else from changing its configuration. */ |
Alex Raimondi | adde42b | 2008-11-04 23:37:10 +0100 | [diff] [blame] | 170 | void __init at32_reserve_pin(unsigned int port, u32 pin_mask) |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 171 | { |
| 172 | struct pio_device *pio; |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 173 | |
Alex Raimondi | adde42b | 2008-11-04 23:37:10 +0100 | [diff] [blame] | 174 | /* assign and verify pio */ |
| 175 | pio = gpio_to_pio(port); |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 176 | if (unlikely(!pio)) { |
Alex Raimondi | adde42b | 2008-11-04 23:37:10 +0100 | [diff] [blame] | 177 | printk(KERN_WARNING "pio: invalid port %u\n", port); |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 178 | goto fail; |
| 179 | } |
| 180 | |
Alex Raimondi | adde42b | 2008-11-04 23:37:10 +0100 | [diff] [blame] | 181 | /* Test if any of the requested pins is already muxed */ |
| 182 | spin_lock(&pio_lock); |
| 183 | if (unlikely(pio->pinmux_mask & pin_mask)) { |
| 184 | printk(KERN_WARNING "%s: pin(s) busy (req. 0x%x, busy 0x%x)\n", |
| 185 | pio->name, pin_mask, pio->pinmux_mask & pin_mask); |
| 186 | spin_unlock(&pio_lock); |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 187 | goto fail; |
| 188 | } |
| 189 | |
Alex Raimondi | adde42b | 2008-11-04 23:37:10 +0100 | [diff] [blame] | 190 | /* Reserve pins */ |
| 191 | pio->pinmux_mask |= pin_mask; |
| 192 | spin_unlock(&pio_lock); |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 193 | return; |
| 194 | |
| 195 | fail: |
| 196 | dump_stack(); |
| 197 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 198 | |
| 199 | /*--------------------------------------------------------------------------*/ |
| 200 | |
| 201 | /* GPIO API */ |
| 202 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 203 | static int direction_input(struct gpio_chip *chip, unsigned offset) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 204 | { |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 205 | struct pio_device *pio = container_of(chip, struct pio_device, chip); |
| 206 | u32 mask = 1 << offset; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 207 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 208 | if (!(pio_readl(pio, PSR) & mask)) |
| 209 | return -EINVAL; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 210 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 211 | pio_writel(pio, ODR, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 214 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 215 | static int gpio_get(struct gpio_chip *chip, unsigned offset) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 216 | { |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 217 | struct pio_device *pio = container_of(chip, struct pio_device, chip); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 218 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 219 | return (pio_readl(pio, PDSR) >> offset) & 1; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 220 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 221 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 222 | static void gpio_set(struct gpio_chip *chip, unsigned offset, int value); |
| 223 | |
| 224 | static int direction_output(struct gpio_chip *chip, unsigned offset, int value) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 225 | { |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 226 | struct pio_device *pio = container_of(chip, struct pio_device, chip); |
| 227 | u32 mask = 1 << offset; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 228 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 229 | if (!(pio_readl(pio, PSR) & mask)) |
| 230 | return -EINVAL; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 231 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 232 | gpio_set(chip, offset, value); |
| 233 | pio_writel(pio, OER, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 234 | return 0; |
| 235 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 236 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 237 | static void gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 238 | { |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 239 | struct pio_device *pio = container_of(chip, struct pio_device, chip); |
| 240 | u32 mask = 1 << offset; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 241 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 242 | if (value) |
| 243 | pio_writel(pio, SODR, mask); |
| 244 | else |
| 245 | pio_writel(pio, CODR, mask); |
| 246 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 247 | |
| 248 | /*--------------------------------------------------------------------------*/ |
| 249 | |
| 250 | /* GPIO IRQ support */ |
| 251 | |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 252 | static void gpio_irq_mask(struct irq_data *d) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 253 | { |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 254 | unsigned gpio = irq_to_gpio(d->irq); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 255 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 256 | |
| 257 | pio_writel(pio, IDR, 1 << (gpio & 0x1f)); |
| 258 | } |
| 259 | |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 260 | static void gpio_irq_unmask(struct irq_data *d)) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 261 | { |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 262 | unsigned gpio = irq_to_gpio(d->irq); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 263 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 264 | |
| 265 | pio_writel(pio, IER, 1 << (gpio & 0x1f)); |
| 266 | } |
| 267 | |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 268 | static int gpio_irq_type(struct irq_data *d, unsigned type) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 269 | { |
| 270 | if (type != IRQ_TYPE_EDGE_BOTH && type != IRQ_TYPE_NONE) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static struct irq_chip gpio_irqchip = { |
| 277 | .name = "gpio", |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 278 | .irq_mask = gpio_irq_mask, |
| 279 | .irq_unmask = gpio_irq_unmask, |
| 280 | .irq_set_type = gpio_irq_type, |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static void gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
| 284 | { |
Thomas Gleixner | dd8ea6a | 2011-03-30 15:33:51 +0200 | [diff] [blame] | 285 | struct pio_device *pio = irq_desc_get_chip_data(desc); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 286 | unsigned gpio_irq; |
| 287 | |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 288 | gpio_irq = (unsigned) irq_get_handler_data(irq); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 289 | for (;;) { |
| 290 | u32 isr; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 291 | |
| 292 | /* ack pending GPIO interrupts */ |
| 293 | isr = pio_readl(pio, ISR) & pio_readl(pio, IMR); |
| 294 | if (!isr) |
| 295 | break; |
| 296 | do { |
| 297 | int i; |
| 298 | |
| 299 | i = ffs(isr) - 1; |
| 300 | isr &= ~(1 << i); |
| 301 | |
| 302 | i += gpio_irq; |
Thomas Gleixner | f2f9c65 | 2011-02-06 17:29:03 +0100 | [diff] [blame] | 303 | generic_handle_irq(i); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 304 | } while (isr); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void __init |
| 309 | gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq) |
| 310 | { |
| 311 | unsigned i; |
| 312 | |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 313 | irq_set_chip_data(irq, pio); |
| 314 | irq_set_handler_data(irq, (void *)gpio_irq); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 315 | |
| 316 | for (i = 0; i < 32; i++, gpio_irq++) { |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 317 | irq_set_chip_data(gpio_irq, pio); |
| 318 | irq_set_chip_and_handler(gpio_irq, &gpio_irqchip, |
| 319 | handle_simple_irq); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 322 | irq_set_chained_handler(irq, gpio_irq_handler); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /*--------------------------------------------------------------------------*/ |
| 326 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 327 | #ifdef CONFIG_DEBUG_FS |
| 328 | |
| 329 | #include <linux/seq_file.h> |
| 330 | |
| 331 | /* |
| 332 | * This shows more info than the generic gpio dump code: |
| 333 | * pullups, deglitching, open drain drive. |
| 334 | */ |
| 335 | static void pio_bank_show(struct seq_file *s, struct gpio_chip *chip) |
| 336 | { |
| 337 | struct pio_device *pio = container_of(chip, struct pio_device, chip); |
| 338 | u32 psr, osr, imr, pdsr, pusr, ifsr, mdsr; |
| 339 | unsigned i; |
| 340 | u32 mask; |
| 341 | char bank; |
| 342 | |
| 343 | psr = pio_readl(pio, PSR); |
| 344 | osr = pio_readl(pio, OSR); |
| 345 | imr = pio_readl(pio, IMR); |
| 346 | pdsr = pio_readl(pio, PDSR); |
| 347 | pusr = pio_readl(pio, PUSR); |
| 348 | ifsr = pio_readl(pio, IFSR); |
| 349 | mdsr = pio_readl(pio, MDSR); |
| 350 | |
| 351 | bank = 'A' + pio->pdev->id; |
| 352 | |
| 353 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
| 354 | const char *label; |
| 355 | |
| 356 | label = gpiochip_is_requested(chip, i); |
David Brownell | aafafdd | 2008-06-10 13:55:52 +0200 | [diff] [blame] | 357 | if (!label && (imr & mask)) |
| 358 | label = "[irq]"; |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 359 | if (!label) |
| 360 | continue; |
| 361 | |
| 362 | seq_printf(s, " gpio-%-3d P%c%-2d (%-12s) %s %s %s", |
| 363 | chip->base + i, bank, i, |
| 364 | label, |
| 365 | (osr & mask) ? "out" : "in ", |
| 366 | (mask & pdsr) ? "hi" : "lo", |
| 367 | (mask & pusr) ? " " : "up"); |
| 368 | if (ifsr & mask) |
| 369 | seq_printf(s, " deglitch"); |
| 370 | if ((osr & mdsr) & mask) |
| 371 | seq_printf(s, " open-drain"); |
| 372 | if (imr & mask) |
| 373 | seq_printf(s, " irq-%d edge-both", |
| 374 | gpio_to_irq(chip->base + i)); |
| 375 | seq_printf(s, "\n"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | #else |
| 380 | #define pio_bank_show NULL |
| 381 | #endif |
| 382 | |
| 383 | |
| 384 | /*--------------------------------------------------------------------------*/ |
| 385 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 386 | static int __init pio_probe(struct platform_device *pdev) |
| 387 | { |
| 388 | struct pio_device *pio = NULL; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 389 | int irq = platform_get_irq(pdev, 0); |
| 390 | int gpio_irq_base = GPIO_IRQ_BASE + pdev->id * 32; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 391 | |
| 392 | BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES); |
| 393 | pio = &pio_dev[pdev->id]; |
| 394 | BUG_ON(!pio->regs); |
| 395 | |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 396 | pio->chip.label = pio->name; |
| 397 | pio->chip.base = pdev->id * 32; |
| 398 | pio->chip.ngpio = 32; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 399 | pio->chip.dev = &pdev->dev; |
| 400 | pio->chip.owner = THIS_MODULE; |
David Brownell | b98348b | 2008-02-04 22:28:28 -0800 | [diff] [blame] | 401 | |
| 402 | pio->chip.direction_input = direction_input; |
| 403 | pio->chip.get = gpio_get; |
| 404 | pio->chip.direction_output = direction_output; |
| 405 | pio->chip.set = gpio_set; |
| 406 | pio->chip.dbg_show = pio_bank_show; |
| 407 | |
| 408 | gpiochip_add(&pio->chip); |
| 409 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 410 | gpio_irq_setup(pio, irq, gpio_irq_base); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 411 | |
| 412 | platform_set_drvdata(pdev, pio); |
| 413 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 414 | printk(KERN_DEBUG "%s: base 0x%p, irq %d chains %d..%d\n", |
| 415 | pio->name, pio->regs, irq, gpio_irq_base, gpio_irq_base + 31); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static struct platform_driver pio_driver = { |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 421 | .driver = { |
| 422 | .name = "pio", |
| 423 | }, |
| 424 | }; |
| 425 | |
| 426 | static int __init pio_init(void) |
| 427 | { |
Uwe Kleine-König | d6634db | 2008-09-21 23:52:07 +0200 | [diff] [blame] | 428 | return platform_driver_probe(&pio_driver, pio_probe); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 429 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 430 | postcore_initcall(pio_init); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 431 | |
| 432 | void __init at32_init_pio(struct platform_device *pdev) |
| 433 | { |
| 434 | struct resource *regs; |
| 435 | struct pio_device *pio; |
| 436 | |
| 437 | if (pdev->id > MAX_NR_PIO_DEVICES) { |
| 438 | dev_err(&pdev->dev, "only %d PIO devices supported\n", |
| 439 | MAX_NR_PIO_DEVICES); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | pio = &pio_dev[pdev->id]; |
| 444 | snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id); |
| 445 | |
| 446 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 447 | if (!regs) { |
| 448 | dev_err(&pdev->dev, "no mmio resource defined\n"); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | pio->clk = clk_get(&pdev->dev, "mck"); |
| 453 | if (IS_ERR(pio->clk)) |
| 454 | /* |
| 455 | * This is a fatal error, but if we continue we might |
| 456 | * be so lucky that we manage to initialize the |
| 457 | * console and display this message... |
| 458 | */ |
| 459 | dev_err(&pdev->dev, "no mck clock defined\n"); |
| 460 | else |
| 461 | clk_enable(pio->clk); |
| 462 | |
| 463 | pio->pdev = pdev; |
| 464 | pio->regs = ioremap(regs->start, regs->end - regs->start + 1); |
| 465 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 466 | /* start with irqs disabled and acked */ |
| 467 | pio_writel(pio, IDR, ~0UL); |
| 468 | (void) pio_readl(pio, ISR); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 469 | } |