blob: 96ff6b28e9ad770a05e81985753a39e509cb33fd [file] [log] [blame]
Baruch Siach1e9c2852009-06-18 16:48:58 -07001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * Copyright (C) 2008, 2009 Provigent Ltd.
Baruch Siach1e9c2852009-06-18 16:48:58 -07003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Rob Herringdece9042011-12-09 14:12:53 -060026#include <asm/mach/irq.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070027
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
39struct 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 Siach1e9c2852009-06-18 16:48:58 -070053
54 void __iomem *base;
Rob Herringf2ab2ba2011-12-09 14:11:41 -060055 int irq_base;
Rob Herring3ab52472011-10-21 08:05:53 -050056 struct irq_chip_generic *irq_gc;
Baruch Siach1e9c2852009-06-18 16:48:58 -070057 struct gpio_chip gc;
58};
59
60static 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
78static 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 kumar64b997c2010-04-21 09:42:05 +010093
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 Siach1e9c2852009-06-18 16:48:58 -070099 spin_unlock_irqrestore(&chip->lock, flags);
100
101 return 0;
102}
103
104static 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
111static 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 Siach50efacf2009-06-30 11:41:39 -0700118static 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 Herringf2ab2ba2011-12-09 14:11:41 -0600122 if (chip->irq_base <= 0)
Baruch Siach50efacf2009-06-30 11:41:39 -0700123 return -EINVAL;
124
125 return chip->irq_base + offset;
126}
127
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800128static int pl061_irq_type(struct irq_data *d, unsigned trigger)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700129{
Rob Herring3ab52472011-10-21 08:05:53 -0500130 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
131 struct pl061_gpio *chip = gc->private;
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800132 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700133 unsigned long flags;
134 u8 gpiois, gpioibe, gpioiev;
135
Axel Linc1cc9b92010-05-26 14:42:19 -0700136 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700137 return -EINVAL;
138
Rob Herring3ab52472011-10-21 08:05:53 -0500139 raw_spin_lock_irqsave(&gc->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700140
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 kumardb7e1bc2010-04-29 12:22:52 +0100161 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700162 gpioiev &= ~(1 << offset);
163 }
164 writeb(gpioibe, chip->base + GPIOIBE);
165
166 writeb(gpioiev, chip->base + GPIOIEV);
167
Rob Herring3ab52472011-10-21 08:05:53 -0500168 raw_spin_unlock_irqrestore(&gc->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700169
170 return 0;
171}
172
Baruch Siach1e9c2852009-06-18 16:48:58 -0700173static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
174{
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000175 struct list_head *chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700176 struct list_head *ptr;
177 struct pl061_gpio *chip;
Rob Herringdece9042011-12-09 14:12:53 -0600178 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700179
Rob Herringdece9042011-12-09 14:12:53 -0600180 chained_irq_enter(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700181 list_for_each(ptr, chip_list) {
182 unsigned long pending;
Baruch Siach50efacf2009-06-30 11:41:39 -0700183 int offset;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700184
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 Mita984b3f52010-03-05 13:41:37 -0800192 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Baruch Siach50efacf2009-06-30 11:41:39 -0700193 generic_handle_irq(pl061_to_irq(&chip->gc, offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700194 }
Rob Herringdece9042011-12-09 14:12:53 -0600195 chained_irq_exit(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700196}
197
Rob Herring3ab52472011-10-21 08:05:53 -0500198static 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 Kingaa25afa2011-02-19 15:55:00 +0000217static int pl061_probe(struct amba_device *dev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700218{
219 struct pl061_platform_data *pdata;
220 struct pl061_gpio *chip;
221 struct list_head *chip_list;
222 int ret, irq, i;
Baruch Siach79d7f4e2009-06-30 11:41:38 -0700223 static DECLARE_BITMAP(init_irq, NR_IRQS);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700224
Baruch Siach1e9c2852009-06-18 16:48:58 -0700225 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
226 if (chip == NULL)
227 return -ENOMEM;
228
Rob Herring76c05c82011-08-10 16:31:46 -0500229 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 Herringf2ab2ba2011-12-09 14:11:41 -0600235 chip->irq_base = 0;
Rob Herring76c05c82011-08-10 16:31:46 -0500236 } else {
237 ret = -ENODEV;
238 goto free_mem;
239 }
240
Baruch Siach1e9c2852009-06-18 16:48:58 -0700241 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 Siach1e9c2852009-06-18 16:48:58 -0700254 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 Siach50efacf2009-06-30 11:41:39 -0700260 chip->gc.to_irq = pl061_to_irq;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700261 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 Siach1e9c2852009-06-18 16:48:58 -0700266 ret = gpiochip_add(&chip->gc);
267 if (ret)
268 goto iounmap;
269
270 /*
271 * irq_chip support
272 */
273
Rob Herringf2ab2ba2011-12-09 14:11:41 -0600274 if (chip->irq_base <= 0)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700275 return 0;
276
Rob Herring3ab52472011-10-21 08:05:53 -0500277 pl061_init_gc(chip, chip->irq_base);
278
Baruch Siach1e9c2852009-06-18 16:48:58 -0700279 writeb(0, chip->base + GPIOIE); /* disable irqs */
280 irq = dev->irq[0];
281 if (irq < 0) {
282 ret = -ENODEV;
283 goto iounmap;
284 }
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000285 irq_set_chained_handler(irq, pl061_irq_handler);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700286 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 Siach79d7f4e2009-06-30 11:41:38 -0700289 clear_bit(irq, init_irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700290 ret = -ENOMEM;
291 goto iounmap;
292 }
293 INIT_LIST_HEAD(chip_list);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000294 irq_set_handler_data(irq, chip_list);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700295 } else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000296 chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700297 list_add(&chip->list, chip_list);
298
299 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500300 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 Siach1e9c2852009-06-18 16:48:58 -0700307 }
308
309 return 0;
310
311iounmap:
312 iounmap(chip->base);
313release_region:
314 release_mem_region(dev->res.start, resource_size(&dev->res));
315free_mem:
316 kfree(chip);
317
318 return ret;
319}
320
Russell King2c39c9e2010-07-27 08:50:16 +0100321static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700322 {
323 .id = 0x00041061,
324 .mask = 0x000fffff,
325 },
326 { 0, 0 },
327};
328
329static struct amba_driver pl061_gpio_driver = {
330 .drv = {
331 .name = "pl061_gpio",
332 },
333 .id_table = pl061_ids,
334 .probe = pl061_probe,
335};
336
337static int __init pl061_gpio_init(void)
338{
339 return amba_driver_register(&pl061_gpio_driver);
340}
341subsys_initcall(pl061_gpio_init);
342
343MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
344MODULE_DESCRIPTION("PL061 GPIO driver");
345MODULE_LICENSE("GPL");