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 | |
| 20 | #include <asm/arch/portmux.h> |
| 21 | |
| 22 | #include "pio.h" |
| 23 | |
| 24 | #define MAX_NR_PIO_DEVICES 8 |
| 25 | |
| 26 | struct pio_device { |
| 27 | void __iomem *regs; |
| 28 | const struct platform_device *pdev; |
| 29 | struct clk *clk; |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 30 | u32 pinmux_mask; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 31 | u32 gpio_mask; |
| 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 */ |
| 53 | |
| 54 | void __init at32_select_periph(unsigned int pin, unsigned int periph, |
| 55 | unsigned long flags) |
| 56 | { |
| 57 | struct pio_device *pio; |
| 58 | unsigned int pin_index = pin & 0x1f; |
| 59 | u32 mask = 1 << pin_index; |
| 60 | |
| 61 | pio = gpio_to_pio(pin); |
| 62 | if (unlikely(!pio)) { |
| 63 | printk("pio: invalid pin %u\n", pin); |
| 64 | goto fail; |
| 65 | } |
| 66 | |
| 67 | if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) { |
| 68 | printk("%s: pin %u is busy\n", pio->name, pin_index); |
| 69 | goto fail; |
| 70 | } |
| 71 | |
| 72 | pio_writel(pio, PUER, mask); |
| 73 | if (periph) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 74 | pio_writel(pio, BSR, mask); |
| 75 | else |
| 76 | pio_writel(pio, ASR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 77 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 78 | pio_writel(pio, PDR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 79 | if (!(flags & AT32_GPIOF_PULLUP)) |
| 80 | pio_writel(pio, PUDR, mask); |
| 81 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 82 | /* gpio_request NOT allowed */ |
| 83 | set_bit(pin_index, &pio->gpio_mask); |
| 84 | |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 85 | return; |
| 86 | |
| 87 | fail: |
| 88 | dump_stack(); |
| 89 | } |
| 90 | |
| 91 | void __init at32_select_gpio(unsigned int pin, unsigned long flags) |
| 92 | { |
| 93 | struct pio_device *pio; |
| 94 | unsigned int pin_index = pin & 0x1f; |
| 95 | u32 mask = 1 << pin_index; |
| 96 | |
| 97 | pio = gpio_to_pio(pin); |
| 98 | if (unlikely(!pio)) { |
| 99 | printk("pio: invalid pin %u\n", pin); |
| 100 | goto fail; |
| 101 | } |
| 102 | |
| 103 | if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) { |
| 104 | printk("%s: pin %u is busy\n", pio->name, pin_index); |
| 105 | goto fail; |
| 106 | } |
| 107 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 108 | if (flags & AT32_GPIOF_OUTPUT) { |
| 109 | if (flags & AT32_GPIOF_HIGH) |
| 110 | pio_writel(pio, SODR, mask); |
| 111 | else |
| 112 | pio_writel(pio, CODR, mask); |
Matteo Vit | 7808fa4 | 2007-08-09 14:55:34 +0200 | [diff] [blame] | 113 | if (flags & AT32_GPIOF_MULTIDRV) |
| 114 | pio_writel(pio, MDER, mask); |
| 115 | else |
| 116 | pio_writel(pio, MDDR, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 117 | pio_writel(pio, PUDR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 118 | pio_writel(pio, OER, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 119 | } else { |
| 120 | if (flags & AT32_GPIOF_PULLUP) |
| 121 | pio_writel(pio, PUER, mask); |
| 122 | else |
| 123 | pio_writel(pio, PUDR, mask); |
| 124 | if (flags & AT32_GPIOF_DEGLITCH) |
| 125 | pio_writel(pio, IFER, mask); |
| 126 | else |
| 127 | pio_writel(pio, IFDR, mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 128 | pio_writel(pio, ODR, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 129 | } |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 130 | |
| 131 | pio_writel(pio, PER, mask); |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 132 | |
| 133 | /* gpio_request now allowed */ |
| 134 | clear_bit(pin_index, &pio->gpio_mask); |
Haavard Skinnemoen | c3e2a79 | 2006-12-04 13:46:52 +0100 | [diff] [blame] | 135 | |
| 136 | return; |
| 137 | |
| 138 | fail: |
| 139 | dump_stack(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Haavard Skinnemoen | e7f70b8 | 2007-01-30 11:01:23 +0100 | [diff] [blame] | 142 | /* Reserve a pin, preventing anyone else from changing its configuration. */ |
| 143 | void __init at32_reserve_pin(unsigned int pin) |
| 144 | { |
| 145 | struct pio_device *pio; |
| 146 | unsigned int pin_index = pin & 0x1f; |
| 147 | |
| 148 | pio = gpio_to_pio(pin); |
| 149 | if (unlikely(!pio)) { |
| 150 | printk("pio: invalid pin %u\n", pin); |
| 151 | goto fail; |
| 152 | } |
| 153 | |
| 154 | if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) { |
| 155 | printk("%s: pin %u is busy\n", pio->name, pin_index); |
| 156 | goto fail; |
| 157 | } |
| 158 | |
| 159 | return; |
| 160 | |
| 161 | fail: |
| 162 | dump_stack(); |
| 163 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 164 | |
| 165 | /*--------------------------------------------------------------------------*/ |
| 166 | |
| 167 | /* GPIO API */ |
| 168 | |
| 169 | int gpio_request(unsigned int gpio, const char *label) |
| 170 | { |
| 171 | struct pio_device *pio; |
| 172 | unsigned int pin; |
| 173 | |
| 174 | pio = gpio_to_pio(gpio); |
| 175 | if (!pio) |
| 176 | return -ENODEV; |
| 177 | |
| 178 | pin = gpio & 0x1f; |
| 179 | if (test_and_set_bit(pin, &pio->gpio_mask)) |
| 180 | return -EBUSY; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | EXPORT_SYMBOL(gpio_request); |
| 185 | |
| 186 | void gpio_free(unsigned int gpio) |
| 187 | { |
| 188 | struct pio_device *pio; |
| 189 | unsigned int pin; |
| 190 | |
| 191 | pio = gpio_to_pio(gpio); |
| 192 | if (!pio) { |
| 193 | printk(KERN_ERR |
| 194 | "gpio: attempted to free invalid pin %u\n", gpio); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | pin = gpio & 0x1f; |
| 199 | if (!test_and_clear_bit(pin, &pio->gpio_mask)) |
| 200 | printk(KERN_ERR "gpio: freeing free or non-gpio pin %s-%u\n", |
| 201 | pio->name, pin); |
| 202 | } |
| 203 | EXPORT_SYMBOL(gpio_free); |
| 204 | |
| 205 | int gpio_direction_input(unsigned int gpio) |
| 206 | { |
| 207 | struct pio_device *pio; |
| 208 | unsigned int pin; |
| 209 | |
| 210 | pio = gpio_to_pio(gpio); |
| 211 | if (!pio) |
| 212 | return -ENODEV; |
| 213 | |
| 214 | pin = gpio & 0x1f; |
| 215 | pio_writel(pio, ODR, 1 << pin); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | EXPORT_SYMBOL(gpio_direction_input); |
| 220 | |
David Brownell | 28735a7 | 2007-03-16 13:38:14 -0800 | [diff] [blame] | 221 | int gpio_direction_output(unsigned int gpio, int value) |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 222 | { |
| 223 | struct pio_device *pio; |
| 224 | unsigned int pin; |
| 225 | |
| 226 | pio = gpio_to_pio(gpio); |
| 227 | if (!pio) |
| 228 | return -ENODEV; |
| 229 | |
David Brownell | 28735a7 | 2007-03-16 13:38:14 -0800 | [diff] [blame] | 230 | gpio_set_value(gpio, value); |
| 231 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 232 | pin = gpio & 0x1f; |
| 233 | pio_writel(pio, OER, 1 << pin); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | EXPORT_SYMBOL(gpio_direction_output); |
| 238 | |
| 239 | int gpio_get_value(unsigned int gpio) |
| 240 | { |
| 241 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 242 | |
| 243 | return (pio_readl(pio, PDSR) >> (gpio & 0x1f)) & 1; |
| 244 | } |
| 245 | EXPORT_SYMBOL(gpio_get_value); |
| 246 | |
| 247 | void gpio_set_value(unsigned int gpio, int value) |
| 248 | { |
| 249 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 250 | u32 mask; |
| 251 | |
| 252 | mask = 1 << (gpio & 0x1f); |
| 253 | if (value) |
| 254 | pio_writel(pio, SODR, mask); |
| 255 | else |
| 256 | pio_writel(pio, CODR, mask); |
| 257 | } |
| 258 | EXPORT_SYMBOL(gpio_set_value); |
| 259 | |
| 260 | /*--------------------------------------------------------------------------*/ |
| 261 | |
| 262 | /* GPIO IRQ support */ |
| 263 | |
| 264 | static void gpio_irq_mask(unsigned irq) |
| 265 | { |
| 266 | unsigned gpio = irq_to_gpio(irq); |
| 267 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 268 | |
| 269 | pio_writel(pio, IDR, 1 << (gpio & 0x1f)); |
| 270 | } |
| 271 | |
| 272 | static void gpio_irq_unmask(unsigned irq) |
| 273 | { |
| 274 | unsigned gpio = irq_to_gpio(irq); |
| 275 | struct pio_device *pio = &pio_dev[gpio >> 5]; |
| 276 | |
| 277 | pio_writel(pio, IER, 1 << (gpio & 0x1f)); |
| 278 | } |
| 279 | |
| 280 | static int gpio_irq_type(unsigned irq, unsigned type) |
| 281 | { |
| 282 | if (type != IRQ_TYPE_EDGE_BOTH && type != IRQ_TYPE_NONE) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static struct irq_chip gpio_irqchip = { |
| 289 | .name = "gpio", |
| 290 | .mask = gpio_irq_mask, |
| 291 | .unmask = gpio_irq_unmask, |
| 292 | .set_type = gpio_irq_type, |
| 293 | }; |
| 294 | |
| 295 | static void gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
| 296 | { |
| 297 | struct pio_device *pio = get_irq_chip_data(irq); |
| 298 | unsigned gpio_irq; |
| 299 | |
| 300 | gpio_irq = (unsigned) get_irq_data(irq); |
| 301 | for (;;) { |
| 302 | u32 isr; |
| 303 | struct irq_desc *d; |
| 304 | |
| 305 | /* ack pending GPIO interrupts */ |
| 306 | isr = pio_readl(pio, ISR) & pio_readl(pio, IMR); |
| 307 | if (!isr) |
| 308 | break; |
| 309 | do { |
| 310 | int i; |
| 311 | |
| 312 | i = ffs(isr) - 1; |
| 313 | isr &= ~(1 << i); |
| 314 | |
| 315 | i += gpio_irq; |
| 316 | d = &irq_desc[i]; |
| 317 | |
| 318 | d->handle_irq(i, d); |
| 319 | } while (isr); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void __init |
| 324 | gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq) |
| 325 | { |
| 326 | unsigned i; |
| 327 | |
| 328 | set_irq_chip_data(irq, pio); |
| 329 | set_irq_data(irq, (void *) gpio_irq); |
| 330 | |
| 331 | for (i = 0; i < 32; i++, gpio_irq++) { |
| 332 | set_irq_chip_data(gpio_irq, pio); |
| 333 | set_irq_chip_and_handler(gpio_irq, &gpio_irqchip, |
| 334 | handle_simple_irq); |
| 335 | } |
| 336 | |
| 337 | set_irq_chained_handler(irq, gpio_irq_handler); |
| 338 | } |
| 339 | |
| 340 | /*--------------------------------------------------------------------------*/ |
| 341 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 342 | static int __init pio_probe(struct platform_device *pdev) |
| 343 | { |
| 344 | struct pio_device *pio = NULL; |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 345 | int irq = platform_get_irq(pdev, 0); |
| 346 | int gpio_irq_base = GPIO_IRQ_BASE + pdev->id * 32; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 347 | |
| 348 | BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES); |
| 349 | pio = &pio_dev[pdev->id]; |
| 350 | BUG_ON(!pio->regs); |
| 351 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 352 | gpio_irq_setup(pio, irq, gpio_irq_base); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 353 | |
| 354 | platform_set_drvdata(pdev, pio); |
| 355 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 356 | printk(KERN_DEBUG "%s: base 0x%p, irq %d chains %d..%d\n", |
| 357 | pio->name, pio->regs, irq, gpio_irq_base, gpio_irq_base + 31); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static struct platform_driver pio_driver = { |
| 363 | .probe = pio_probe, |
| 364 | .driver = { |
| 365 | .name = "pio", |
| 366 | }, |
| 367 | }; |
| 368 | |
| 369 | static int __init pio_init(void) |
| 370 | { |
| 371 | return platform_driver_register(&pio_driver); |
| 372 | } |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 373 | postcore_initcall(pio_init); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 374 | |
| 375 | void __init at32_init_pio(struct platform_device *pdev) |
| 376 | { |
| 377 | struct resource *regs; |
| 378 | struct pio_device *pio; |
| 379 | |
| 380 | if (pdev->id > MAX_NR_PIO_DEVICES) { |
| 381 | dev_err(&pdev->dev, "only %d PIO devices supported\n", |
| 382 | MAX_NR_PIO_DEVICES); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | pio = &pio_dev[pdev->id]; |
| 387 | snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id); |
| 388 | |
| 389 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 390 | if (!regs) { |
| 391 | dev_err(&pdev->dev, "no mmio resource defined\n"); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | pio->clk = clk_get(&pdev->dev, "mck"); |
| 396 | if (IS_ERR(pio->clk)) |
| 397 | /* |
| 398 | * This is a fatal error, but if we continue we might |
| 399 | * be so lucky that we manage to initialize the |
| 400 | * console and display this message... |
| 401 | */ |
| 402 | dev_err(&pdev->dev, "no mck clock defined\n"); |
| 403 | else |
| 404 | clk_enable(pio->clk); |
| 405 | |
| 406 | pio->pdev = pdev; |
| 407 | pio->regs = ioremap(regs->start, regs->end - regs->start + 1); |
| 408 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 409 | /* |
| 410 | * request_gpio() is only valid for pins that have been |
| 411 | * explicitly configured as GPIO and not previously requested |
| 412 | */ |
| 413 | pio->gpio_mask = ~0UL; |
| 414 | |
Haavard Skinnemoen | 6a4e522 | 2007-02-05 16:57:13 +0100 | [diff] [blame] | 415 | /* start with irqs disabled and acked */ |
| 416 | pio_writel(pio, IDR, ~0UL); |
| 417 | (void) pio_readl(pio, ISR); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 418 | } |