blob: fe19dec4b117b1af367d0a5cde77440be2b62e0d [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 */
53 spinlock_t irq_lock; /* IRQ registers */
54
55 void __iomem *base;
Rob Herringf2ab2ba2011-12-09 14:11:41 -060056 int irq_base;
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 kumar64b997c52010-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
Baruch Siach1e9c2852009-06-18 16:48:58 -0700128/*
129 * PL061 GPIO IRQ
130 */
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800131static void pl061_irq_disable(struct irq_data *d)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700132{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800133 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
134 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700135 unsigned long flags;
136 u8 gpioie;
137
138 spin_lock_irqsave(&chip->irq_lock, flags);
139 gpioie = readb(chip->base + GPIOIE);
140 gpioie &= ~(1 << offset);
141 writeb(gpioie, chip->base + GPIOIE);
142 spin_unlock_irqrestore(&chip->irq_lock, flags);
143}
144
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800145static void pl061_irq_enable(struct irq_data *d)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700146{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800147 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
148 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700149 unsigned long flags;
150 u8 gpioie;
151
152 spin_lock_irqsave(&chip->irq_lock, flags);
153 gpioie = readb(chip->base + GPIOIE);
154 gpioie |= 1 << offset;
155 writeb(gpioie, chip->base + GPIOIE);
156 spin_unlock_irqrestore(&chip->irq_lock, flags);
157}
158
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800159static int pl061_irq_type(struct irq_data *d, unsigned trigger)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700160{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800161 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
162 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700163 unsigned long flags;
164 u8 gpiois, gpioibe, gpioiev;
165
Axel Linc1cc9b92010-05-26 14:42:19 -0700166 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700167 return -EINVAL;
168
169 spin_lock_irqsave(&chip->irq_lock, flags);
170
171 gpioiev = readb(chip->base + GPIOIEV);
172
173 gpiois = readb(chip->base + GPIOIS);
174 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
175 gpiois |= 1 << offset;
176 if (trigger & IRQ_TYPE_LEVEL_HIGH)
177 gpioiev |= 1 << offset;
178 else
179 gpioiev &= ~(1 << offset);
180 } else
181 gpiois &= ~(1 << offset);
182 writeb(gpiois, chip->base + GPIOIS);
183
184 gpioibe = readb(chip->base + GPIOIBE);
185 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
186 gpioibe |= 1 << offset;
187 else {
188 gpioibe &= ~(1 << offset);
189 if (trigger & IRQ_TYPE_EDGE_RISING)
190 gpioiev |= 1 << offset;
viresh kumardb7e1bc2010-04-29 12:22:52 +0100191 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700192 gpioiev &= ~(1 << offset);
193 }
194 writeb(gpioibe, chip->base + GPIOIBE);
195
196 writeb(gpioiev, chip->base + GPIOIEV);
197
198 spin_unlock_irqrestore(&chip->irq_lock, flags);
199
200 return 0;
201}
202
203static struct irq_chip pl061_irqchip = {
204 .name = "GPIO",
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800205 .irq_enable = pl061_irq_enable,
206 .irq_disable = pl061_irq_disable,
207 .irq_set_type = pl061_irq_type,
Baruch Siach1e9c2852009-06-18 16:48:58 -0700208};
209
210static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
211{
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000212 struct list_head *chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700213 struct list_head *ptr;
214 struct pl061_gpio *chip;
Rob Herringdece9042011-12-09 14:12:53 -0600215 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700216
Rob Herringdece9042011-12-09 14:12:53 -0600217 chained_irq_enter(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700218 list_for_each(ptr, chip_list) {
219 unsigned long pending;
Baruch Siach50efacf2009-06-30 11:41:39 -0700220 int offset;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700221
222 chip = list_entry(ptr, struct pl061_gpio, list);
223 pending = readb(chip->base + GPIOMIS);
224 writeb(pending, chip->base + GPIOIC);
225
226 if (pending == 0)
227 continue;
228
Akinobu Mita984b3f52010-03-05 13:41:37 -0800229 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Baruch Siach50efacf2009-06-30 11:41:39 -0700230 generic_handle_irq(pl061_to_irq(&chip->gc, offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700231 }
Rob Herringdece9042011-12-09 14:12:53 -0600232 chained_irq_exit(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700233}
234
Russell Kingaa25afa2011-02-19 15:55:00 +0000235static int pl061_probe(struct amba_device *dev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700236{
237 struct pl061_platform_data *pdata;
238 struct pl061_gpio *chip;
239 struct list_head *chip_list;
240 int ret, irq, i;
Baruch Siach79d7f4e2009-06-30 11:41:38 -0700241 static DECLARE_BITMAP(init_irq, NR_IRQS);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700242
Baruch Siach1e9c2852009-06-18 16:48:58 -0700243 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
244 if (chip == NULL)
245 return -ENOMEM;
246
Rob Herring76c05c82011-08-10 16:31:46 -0500247 pdata = dev->dev.platform_data;
248 if (pdata) {
249 chip->gc.base = pdata->gpio_base;
250 chip->irq_base = pdata->irq_base;
251 } else if (dev->dev.of_node) {
252 chip->gc.base = -1;
Rob Herringf2ab2ba2011-12-09 14:11:41 -0600253 chip->irq_base = 0;
Rob Herring76c05c82011-08-10 16:31:46 -0500254 } else {
255 ret = -ENODEV;
256 goto free_mem;
257 }
258
Baruch Siach1e9c2852009-06-18 16:48:58 -0700259 if (!request_mem_region(dev->res.start,
260 resource_size(&dev->res), "pl061")) {
261 ret = -EBUSY;
262 goto free_mem;
263 }
264
265 chip->base = ioremap(dev->res.start, resource_size(&dev->res));
266 if (chip->base == NULL) {
267 ret = -ENOMEM;
268 goto release_region;
269 }
270
271 spin_lock_init(&chip->lock);
272 spin_lock_init(&chip->irq_lock);
273 INIT_LIST_HEAD(&chip->list);
274
275 chip->gc.direction_input = pl061_direction_input;
276 chip->gc.direction_output = pl061_direction_output;
277 chip->gc.get = pl061_get_value;
278 chip->gc.set = pl061_set_value;
Baruch Siach50efacf2009-06-30 11:41:39 -0700279 chip->gc.to_irq = pl061_to_irq;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700280 chip->gc.ngpio = PL061_GPIO_NR;
281 chip->gc.label = dev_name(&dev->dev);
282 chip->gc.dev = &dev->dev;
283 chip->gc.owner = THIS_MODULE;
284
Baruch Siach1e9c2852009-06-18 16:48:58 -0700285 ret = gpiochip_add(&chip->gc);
286 if (ret)
287 goto iounmap;
288
289 /*
290 * irq_chip support
291 */
292
Rob Herringf2ab2ba2011-12-09 14:11:41 -0600293 if (chip->irq_base <= 0)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700294 return 0;
295
296 writeb(0, chip->base + GPIOIE); /* disable irqs */
297 irq = dev->irq[0];
298 if (irq < 0) {
299 ret = -ENODEV;
300 goto iounmap;
301 }
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000302 irq_set_chained_handler(irq, pl061_irq_handler);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700303 if (!test_and_set_bit(irq, init_irq)) { /* list initialized? */
304 chip_list = kmalloc(sizeof(*chip_list), GFP_KERNEL);
305 if (chip_list == NULL) {
Baruch Siach79d7f4e2009-06-30 11:41:38 -0700306 clear_bit(irq, init_irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700307 ret = -ENOMEM;
308 goto iounmap;
309 }
310 INIT_LIST_HEAD(chip_list);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000311 irq_set_handler_data(irq, chip_list);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700312 } else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000313 chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700314 list_add(&chip->list, chip_list);
315
316 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500317 if (pdata) {
318 if (pdata->directions & (1 << i))
319 pl061_direction_output(&chip->gc, i,
320 pdata->values & (1 << i));
321 else
322 pl061_direction_input(&chip->gc, i);
323 }
Baruch Siach1e9c2852009-06-18 16:48:58 -0700324
Thomas Gleixner08f1b802011-03-24 21:27:37 +0000325 irq_set_chip_and_handler(i + chip->irq_base, &pl061_irqchip,
326 handle_simple_irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700327 set_irq_flags(i+chip->irq_base, IRQF_VALID);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000328 irq_set_chip_data(i + chip->irq_base, chip);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700329 }
330
331 return 0;
332
333iounmap:
334 iounmap(chip->base);
335release_region:
336 release_mem_region(dev->res.start, resource_size(&dev->res));
337free_mem:
338 kfree(chip);
339
340 return ret;
341}
342
Russell King2c39c9e2010-07-27 08:50:16 +0100343static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700344 {
345 .id = 0x00041061,
346 .mask = 0x000fffff,
347 },
348 { 0, 0 },
349};
350
351static struct amba_driver pl061_gpio_driver = {
352 .drv = {
353 .name = "pl061_gpio",
354 },
355 .id_table = pl061_ids,
356 .probe = pl061_probe,
357};
358
359static int __init pl061_gpio_init(void)
360{
361 return amba_driver_register(&pl061_gpio_driver);
362}
363subsys_initcall(pl061_gpio_init);
364
365MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
366MODULE_DESCRIPTION("PL061 GPIO driver");
367MODULE_LICENSE("GPL");