Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 Provigent Ltd. |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061) |
| 9 | * |
| 10 | * Data sheet: ARM DDI 0190B, September 2000 |
| 11 | */ |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/ioport.h> |
| 18 | #include <linux/irq.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/amba/bus.h> |
| 24 | #include <linux/amba/pl061.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 26 | #include <asm/mach/irq.h> |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 27 | |
| 28 | #define GPIODIR 0x400 |
| 29 | #define GPIOIS 0x404 |
| 30 | #define GPIOIBE 0x408 |
| 31 | #define GPIOIEV 0x40C |
| 32 | #define GPIOIE 0x410 |
| 33 | #define GPIORIS 0x414 |
| 34 | #define GPIOMIS 0x418 |
| 35 | #define GPIOIC 0x41C |
| 36 | |
| 37 | #define PL061_GPIO_NR 8 |
| 38 | |
| 39 | struct pl061_gpio { |
| 40 | /* We use a list of pl061_gpio structs for each trigger IRQ in the main |
| 41 | * interrupts controller of the system. We need this to support systems |
| 42 | * in which more that one PL061s are connected to the same IRQ. The ISR |
| 43 | * interates through this list to find the source of the interrupt. |
| 44 | */ |
| 45 | struct list_head list; |
| 46 | |
| 47 | /* Each of the two spinlocks protects a different set of hardware |
| 48 | * regiters and data structurs. This decouples the code of the IRQ from |
| 49 | * the GPIO code. This also makes the case of a GPIO routine call from |
| 50 | * the IRQ code simpler. |
| 51 | */ |
| 52 | spinlock_t lock; /* GPIO registers */ |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 53 | |
| 54 | void __iomem *base; |
Rob Herring | f2ab2ba | 2011-12-09 14:11:41 -0600 | [diff] [blame] | 55 | int irq_base; |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 56 | struct irq_chip_generic *irq_gc; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 57 | struct gpio_chip gc; |
| 58 | }; |
| 59 | |
| 60 | static int pl061_direction_input(struct gpio_chip *gc, unsigned offset) |
| 61 | { |
| 62 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 63 | unsigned long flags; |
| 64 | unsigned char gpiodir; |
| 65 | |
| 66 | if (offset >= gc->ngpio) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | spin_lock_irqsave(&chip->lock, flags); |
| 70 | gpiodir = readb(chip->base + GPIODIR); |
| 71 | gpiodir &= ~(1 << offset); |
| 72 | writeb(gpiodir, chip->base + GPIODIR); |
| 73 | spin_unlock_irqrestore(&chip->lock, flags); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int pl061_direction_output(struct gpio_chip *gc, unsigned offset, |
| 79 | int value) |
| 80 | { |
| 81 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 82 | unsigned long flags; |
| 83 | unsigned char gpiodir; |
| 84 | |
| 85 | if (offset >= gc->ngpio) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | spin_lock_irqsave(&chip->lock, flags); |
| 89 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
| 90 | gpiodir = readb(chip->base + GPIODIR); |
| 91 | gpiodir |= 1 << offset; |
| 92 | writeb(gpiodir, chip->base + GPIODIR); |
viresh kumar | 64b997c | 2010-04-21 09:42:05 +0100 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * gpio value is set again, because pl061 doesn't allow to set value of |
| 96 | * a gpio pin before configuring it in OUT mode. |
| 97 | */ |
| 98 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 99 | spin_unlock_irqrestore(&chip->lock, flags); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int pl061_get_value(struct gpio_chip *gc, unsigned offset) |
| 105 | { |
| 106 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 107 | |
| 108 | return !!readb(chip->base + (1 << (offset + 2))); |
| 109 | } |
| 110 | |
| 111 | static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value) |
| 112 | { |
| 113 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 114 | |
| 115 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
| 116 | } |
| 117 | |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 118 | static int pl061_to_irq(struct gpio_chip *gc, unsigned offset) |
| 119 | { |
| 120 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 121 | |
Rob Herring | f2ab2ba | 2011-12-09 14:11:41 -0600 | [diff] [blame] | 122 | if (chip->irq_base <= 0) |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | |
| 125 | return chip->irq_base + offset; |
| 126 | } |
| 127 | |
Lennert Buytenhek | b222186 | 2011-01-12 17:00:16 -0800 | [diff] [blame] | 128 | static int pl061_irq_type(struct irq_data *d, unsigned trigger) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 129 | { |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 130 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 131 | struct pl061_gpio *chip = gc->private; |
Lennert Buytenhek | b222186 | 2011-01-12 17:00:16 -0800 | [diff] [blame] | 132 | int offset = d->irq - chip->irq_base; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 133 | unsigned long flags; |
| 134 | u8 gpiois, gpioibe, gpioiev; |
| 135 | |
Axel Lin | c1cc9b9 | 2010-05-26 14:42:19 -0700 | [diff] [blame] | 136 | if (offset < 0 || offset >= PL061_GPIO_NR) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 139 | raw_spin_lock_irqsave(&gc->lock, flags); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 140 | |
| 141 | gpioiev = readb(chip->base + GPIOIEV); |
| 142 | |
| 143 | gpiois = readb(chip->base + GPIOIS); |
| 144 | if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) { |
| 145 | gpiois |= 1 << offset; |
| 146 | if (trigger & IRQ_TYPE_LEVEL_HIGH) |
| 147 | gpioiev |= 1 << offset; |
| 148 | else |
| 149 | gpioiev &= ~(1 << offset); |
| 150 | } else |
| 151 | gpiois &= ~(1 << offset); |
| 152 | writeb(gpiois, chip->base + GPIOIS); |
| 153 | |
| 154 | gpioibe = readb(chip->base + GPIOIBE); |
| 155 | if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) |
| 156 | gpioibe |= 1 << offset; |
| 157 | else { |
| 158 | gpioibe &= ~(1 << offset); |
| 159 | if (trigger & IRQ_TYPE_EDGE_RISING) |
| 160 | gpioiev |= 1 << offset; |
viresh kumar | db7e1bc | 2010-04-29 12:22:52 +0100 | [diff] [blame] | 161 | else if (trigger & IRQ_TYPE_EDGE_FALLING) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 162 | gpioiev &= ~(1 << offset); |
| 163 | } |
| 164 | writeb(gpioibe, chip->base + GPIOIBE); |
| 165 | |
| 166 | writeb(gpioiev, chip->base + GPIOIEV); |
| 167 | |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 168 | raw_spin_unlock_irqrestore(&gc->lock, flags); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 173 | static void pl061_irq_handler(unsigned irq, struct irq_desc *desc) |
| 174 | { |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 175 | struct list_head *chip_list = irq_get_handler_data(irq); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 176 | struct list_head *ptr; |
| 177 | struct pl061_gpio *chip; |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 178 | struct irq_chip *irqchip = irq_desc_get_chip(desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 179 | |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 180 | chained_irq_enter(irqchip, desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 181 | list_for_each(ptr, chip_list) { |
| 182 | unsigned long pending; |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 183 | int offset; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 184 | |
| 185 | chip = list_entry(ptr, struct pl061_gpio, list); |
| 186 | pending = readb(chip->base + GPIOMIS); |
| 187 | writeb(pending, chip->base + GPIOIC); |
| 188 | |
| 189 | if (pending == 0) |
| 190 | continue; |
| 191 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 192 | for_each_set_bit(offset, &pending, PL061_GPIO_NR) |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 193 | generic_handle_irq(pl061_to_irq(&chip->gc, offset)); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 194 | } |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 195 | chained_irq_exit(irqchip, desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 198 | static void __init pl061_init_gc(struct pl061_gpio *chip, int irq_base) |
| 199 | { |
| 200 | struct irq_chip_type *ct; |
| 201 | |
| 202 | chip->irq_gc = irq_alloc_generic_chip("gpio-pl061", 1, irq_base, |
| 203 | chip->base, handle_simple_irq); |
| 204 | chip->irq_gc->private = chip; |
| 205 | |
| 206 | ct = chip->irq_gc->chip_types; |
| 207 | ct->chip.irq_mask = irq_gc_mask_clr_bit; |
| 208 | ct->chip.irq_unmask = irq_gc_mask_set_bit; |
| 209 | ct->chip.irq_set_type = pl061_irq_type; |
| 210 | ct->chip.irq_set_wake = irq_gc_set_wake; |
| 211 | ct->regs.mask = GPIOIE; |
| 212 | |
| 213 | irq_setup_generic_chip(chip->irq_gc, IRQ_MSK(PL061_GPIO_NR), |
| 214 | IRQ_GC_INIT_NESTED_LOCK, IRQ_NOREQUEST, 0); |
| 215 | } |
| 216 | |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 217 | static int pl061_probe(struct amba_device *dev, const struct amba_id *id) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 218 | { |
| 219 | struct pl061_platform_data *pdata; |
| 220 | struct pl061_gpio *chip; |
| 221 | struct list_head *chip_list; |
| 222 | int ret, irq, i; |
Baruch Siach | 79d7f4e | 2009-06-30 11:41:38 -0700 | [diff] [blame] | 223 | static DECLARE_BITMAP(init_irq, NR_IRQS); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 224 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 225 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 226 | if (chip == NULL) |
| 227 | return -ENOMEM; |
| 228 | |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 229 | pdata = dev->dev.platform_data; |
| 230 | if (pdata) { |
| 231 | chip->gc.base = pdata->gpio_base; |
| 232 | chip->irq_base = pdata->irq_base; |
| 233 | } else if (dev->dev.of_node) { |
| 234 | chip->gc.base = -1; |
Rob Herring | f2ab2ba | 2011-12-09 14:11:41 -0600 | [diff] [blame] | 235 | chip->irq_base = 0; |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 236 | } else { |
| 237 | ret = -ENODEV; |
| 238 | goto free_mem; |
| 239 | } |
| 240 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 241 | if (!request_mem_region(dev->res.start, |
| 242 | resource_size(&dev->res), "pl061")) { |
| 243 | ret = -EBUSY; |
| 244 | goto free_mem; |
| 245 | } |
| 246 | |
| 247 | chip->base = ioremap(dev->res.start, resource_size(&dev->res)); |
| 248 | if (chip->base == NULL) { |
| 249 | ret = -ENOMEM; |
| 250 | goto release_region; |
| 251 | } |
| 252 | |
| 253 | spin_lock_init(&chip->lock); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 254 | INIT_LIST_HEAD(&chip->list); |
| 255 | |
| 256 | chip->gc.direction_input = pl061_direction_input; |
| 257 | chip->gc.direction_output = pl061_direction_output; |
| 258 | chip->gc.get = pl061_get_value; |
| 259 | chip->gc.set = pl061_set_value; |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 260 | chip->gc.to_irq = pl061_to_irq; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 261 | chip->gc.ngpio = PL061_GPIO_NR; |
| 262 | chip->gc.label = dev_name(&dev->dev); |
| 263 | chip->gc.dev = &dev->dev; |
| 264 | chip->gc.owner = THIS_MODULE; |
| 265 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 266 | ret = gpiochip_add(&chip->gc); |
| 267 | if (ret) |
| 268 | goto iounmap; |
| 269 | |
| 270 | /* |
| 271 | * irq_chip support |
| 272 | */ |
| 273 | |
Rob Herring | f2ab2ba | 2011-12-09 14:11:41 -0600 | [diff] [blame] | 274 | if (chip->irq_base <= 0) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame^] | 277 | pl061_init_gc(chip, chip->irq_base); |
| 278 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 279 | writeb(0, chip->base + GPIOIE); /* disable irqs */ |
| 280 | irq = dev->irq[0]; |
| 281 | if (irq < 0) { |
| 282 | ret = -ENODEV; |
| 283 | goto iounmap; |
| 284 | } |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 285 | irq_set_chained_handler(irq, pl061_irq_handler); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 286 | if (!test_and_set_bit(irq, init_irq)) { /* list initialized? */ |
| 287 | chip_list = kmalloc(sizeof(*chip_list), GFP_KERNEL); |
| 288 | if (chip_list == NULL) { |
Baruch Siach | 79d7f4e | 2009-06-30 11:41:38 -0700 | [diff] [blame] | 289 | clear_bit(irq, init_irq); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 290 | ret = -ENOMEM; |
| 291 | goto iounmap; |
| 292 | } |
| 293 | INIT_LIST_HEAD(chip_list); |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 294 | irq_set_handler_data(irq, chip_list); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 295 | } else |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 296 | chip_list = irq_get_handler_data(irq); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 297 | list_add(&chip->list, chip_list); |
| 298 | |
| 299 | for (i = 0; i < PL061_GPIO_NR; i++) { |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 300 | if (pdata) { |
| 301 | if (pdata->directions & (1 << i)) |
| 302 | pl061_direction_output(&chip->gc, i, |
| 303 | pdata->values & (1 << i)); |
| 304 | else |
| 305 | pl061_direction_input(&chip->gc, i); |
| 306 | } |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | |
| 311 | iounmap: |
| 312 | iounmap(chip->base); |
| 313 | release_region: |
| 314 | release_mem_region(dev->res.start, resource_size(&dev->res)); |
| 315 | free_mem: |
| 316 | kfree(chip); |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
Russell King | 2c39c9e | 2010-07-27 08:50:16 +0100 | [diff] [blame] | 321 | static struct amba_id pl061_ids[] = { |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 322 | { |
| 323 | .id = 0x00041061, |
| 324 | .mask = 0x000fffff, |
| 325 | }, |
| 326 | { 0, 0 }, |
| 327 | }; |
| 328 | |
| 329 | static struct amba_driver pl061_gpio_driver = { |
| 330 | .drv = { |
| 331 | .name = "pl061_gpio", |
| 332 | }, |
| 333 | .id_table = pl061_ids, |
| 334 | .probe = pl061_probe, |
| 335 | }; |
| 336 | |
| 337 | static int __init pl061_gpio_init(void) |
| 338 | { |
| 339 | return amba_driver_register(&pl061_gpio_driver); |
| 340 | } |
| 341 | subsys_initcall(pl061_gpio_init); |
| 342 | |
| 343 | MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); |
| 344 | MODULE_DESCRIPTION("PL061 GPIO driver"); |
| 345 | MODULE_LICENSE("GPL"); |