blob: 42a5d5672694d845669d366b4ef558b3cc53015f [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>
Deepak Sikrie198a8d2011-11-18 15:20:12 +053026#include <linux/pm.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
Deepak Sikrie198a8d2011-11-18 15:20:12 +053039#ifdef CONFIG_PM
40struct pl061_context_save_regs {
41 u8 gpio_data;
42 u8 gpio_dir;
43 u8 gpio_is;
44 u8 gpio_ibe;
45 u8 gpio_iev;
46 u8 gpio_ie;
47};
48#endif
49
Baruch Siach1e9c2852009-06-18 16:48:58 -070050struct pl061_gpio {
51 /* We use a list of pl061_gpio structs for each trigger IRQ in the main
52 * interrupts controller of the system. We need this to support systems
53 * in which more that one PL061s are connected to the same IRQ. The ISR
54 * interates through this list to find the source of the interrupt.
55 */
56 struct list_head list;
57
58 /* Each of the two spinlocks protects a different set of hardware
59 * regiters and data structurs. This decouples the code of the IRQ from
60 * the GPIO code. This also makes the case of a GPIO routine call from
61 * the IRQ code simpler.
62 */
63 spinlock_t lock; /* GPIO registers */
64 spinlock_t irq_lock; /* IRQ registers */
65
66 void __iomem *base;
67 unsigned irq_base;
68 struct gpio_chip gc;
Deepak Sikrie198a8d2011-11-18 15:20:12 +053069
70#ifdef CONFIG_PM
71 struct pl061_context_save_regs csave_regs;
72#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070073};
74
75static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
76{
77 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
78 unsigned long flags;
79 unsigned char gpiodir;
80
81 if (offset >= gc->ngpio)
82 return -EINVAL;
83
84 spin_lock_irqsave(&chip->lock, flags);
85 gpiodir = readb(chip->base + GPIODIR);
86 gpiodir &= ~(1 << offset);
87 writeb(gpiodir, chip->base + GPIODIR);
88 spin_unlock_irqrestore(&chip->lock, flags);
89
90 return 0;
91}
92
93static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
94 int value)
95{
96 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
97 unsigned long flags;
98 unsigned char gpiodir;
99
100 if (offset >= gc->ngpio)
101 return -EINVAL;
102
103 spin_lock_irqsave(&chip->lock, flags);
104 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
105 gpiodir = readb(chip->base + GPIODIR);
106 gpiodir |= 1 << offset;
107 writeb(gpiodir, chip->base + GPIODIR);
viresh kumar64b997c2010-04-21 09:42:05 +0100108
109 /*
110 * gpio value is set again, because pl061 doesn't allow to set value of
111 * a gpio pin before configuring it in OUT mode.
112 */
113 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700114 spin_unlock_irqrestore(&chip->lock, flags);
115
116 return 0;
117}
118
119static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
120{
121 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
122
123 return !!readb(chip->base + (1 << (offset + 2)));
124}
125
126static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
127{
128 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
129
130 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
131}
132
Baruch Siach50efacf2009-06-30 11:41:39 -0700133static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
134{
135 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
136
Rob Herring76c05c82011-08-10 16:31:46 -0500137 if (chip->irq_base == NO_IRQ)
Baruch Siach50efacf2009-06-30 11:41:39 -0700138 return -EINVAL;
139
140 return chip->irq_base + offset;
141}
142
Baruch Siach1e9c2852009-06-18 16:48:58 -0700143/*
144 * PL061 GPIO IRQ
145 */
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800146static void pl061_irq_disable(struct irq_data *d)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700147{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800148 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
149 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700150 unsigned long flags;
151 u8 gpioie;
152
153 spin_lock_irqsave(&chip->irq_lock, flags);
154 gpioie = readb(chip->base + GPIOIE);
155 gpioie &= ~(1 << offset);
156 writeb(gpioie, chip->base + GPIOIE);
157 spin_unlock_irqrestore(&chip->irq_lock, flags);
158}
159
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800160static void pl061_irq_enable(struct irq_data *d)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700161{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800162 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
163 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700164 unsigned long flags;
165 u8 gpioie;
166
167 spin_lock_irqsave(&chip->irq_lock, flags);
168 gpioie = readb(chip->base + GPIOIE);
169 gpioie |= 1 << offset;
170 writeb(gpioie, chip->base + GPIOIE);
171 spin_unlock_irqrestore(&chip->irq_lock, flags);
172}
173
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800174static int pl061_irq_type(struct irq_data *d, unsigned trigger)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700175{
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800176 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
177 int offset = d->irq - chip->irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700178 unsigned long flags;
179 u8 gpiois, gpioibe, gpioiev;
180
Axel Linc1cc9b92010-05-26 14:42:19 -0700181 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700182 return -EINVAL;
183
184 spin_lock_irqsave(&chip->irq_lock, flags);
185
186 gpioiev = readb(chip->base + GPIOIEV);
187
188 gpiois = readb(chip->base + GPIOIS);
189 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
190 gpiois |= 1 << offset;
191 if (trigger & IRQ_TYPE_LEVEL_HIGH)
192 gpioiev |= 1 << offset;
193 else
194 gpioiev &= ~(1 << offset);
195 } else
196 gpiois &= ~(1 << offset);
197 writeb(gpiois, chip->base + GPIOIS);
198
199 gpioibe = readb(chip->base + GPIOIBE);
200 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
201 gpioibe |= 1 << offset;
202 else {
203 gpioibe &= ~(1 << offset);
204 if (trigger & IRQ_TYPE_EDGE_RISING)
205 gpioiev |= 1 << offset;
viresh kumardb7e1bc2010-04-29 12:22:52 +0100206 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700207 gpioiev &= ~(1 << offset);
208 }
209 writeb(gpioibe, chip->base + GPIOIBE);
210
211 writeb(gpioiev, chip->base + GPIOIEV);
212
213 spin_unlock_irqrestore(&chip->irq_lock, flags);
214
215 return 0;
216}
217
218static struct irq_chip pl061_irqchip = {
219 .name = "GPIO",
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800220 .irq_enable = pl061_irq_enable,
221 .irq_disable = pl061_irq_disable,
222 .irq_set_type = pl061_irq_type,
Baruch Siach1e9c2852009-06-18 16:48:58 -0700223};
224
225static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
226{
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000227 struct list_head *chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700228 struct list_head *ptr;
229 struct pl061_gpio *chip;
230
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800231 desc->irq_data.chip->irq_ack(&desc->irq_data);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700232 list_for_each(ptr, chip_list) {
233 unsigned long pending;
Baruch Siach50efacf2009-06-30 11:41:39 -0700234 int offset;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700235
236 chip = list_entry(ptr, struct pl061_gpio, list);
237 pending = readb(chip->base + GPIOMIS);
238 writeb(pending, chip->base + GPIOIC);
239
240 if (pending == 0)
241 continue;
242
Akinobu Mita984b3f52010-03-05 13:41:37 -0800243 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Baruch Siach50efacf2009-06-30 11:41:39 -0700244 generic_handle_irq(pl061_to_irq(&chip->gc, offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700245 }
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800246 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700247}
248
Russell Kingaa25afa2011-02-19 15:55:00 +0000249static int pl061_probe(struct amba_device *dev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700250{
251 struct pl061_platform_data *pdata;
252 struct pl061_gpio *chip;
253 struct list_head *chip_list;
254 int ret, irq, i;
Baruch Siach79d7f4e2009-06-30 11:41:38 -0700255 static DECLARE_BITMAP(init_irq, NR_IRQS);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700256
257 pdata = dev->dev.platform_data;
258 if (pdata == NULL)
259 return -ENODEV;
260
261 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
262 if (chip == NULL)
263 return -ENOMEM;
264
Rob Herring76c05c82011-08-10 16:31:46 -0500265 pdata = dev->dev.platform_data;
266 if (pdata) {
267 chip->gc.base = pdata->gpio_base;
268 chip->irq_base = pdata->irq_base;
269 } else if (dev->dev.of_node) {
270 chip->gc.base = -1;
271 chip->irq_base = NO_IRQ;
272 } else {
273 ret = -ENODEV;
274 goto free_mem;
275 }
276
Baruch Siach1e9c2852009-06-18 16:48:58 -0700277 if (!request_mem_region(dev->res.start,
278 resource_size(&dev->res), "pl061")) {
279 ret = -EBUSY;
280 goto free_mem;
281 }
282
283 chip->base = ioremap(dev->res.start, resource_size(&dev->res));
284 if (chip->base == NULL) {
285 ret = -ENOMEM;
286 goto release_region;
287 }
288
289 spin_lock_init(&chip->lock);
290 spin_lock_init(&chip->irq_lock);
291 INIT_LIST_HEAD(&chip->list);
292
293 chip->gc.direction_input = pl061_direction_input;
294 chip->gc.direction_output = pl061_direction_output;
295 chip->gc.get = pl061_get_value;
296 chip->gc.set = pl061_set_value;
Baruch Siach50efacf2009-06-30 11:41:39 -0700297 chip->gc.to_irq = pl061_to_irq;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700298 chip->gc.ngpio = PL061_GPIO_NR;
299 chip->gc.label = dev_name(&dev->dev);
300 chip->gc.dev = &dev->dev;
301 chip->gc.owner = THIS_MODULE;
302
Baruch Siach1e9c2852009-06-18 16:48:58 -0700303 ret = gpiochip_add(&chip->gc);
304 if (ret)
305 goto iounmap;
306
307 /*
308 * irq_chip support
309 */
310
Rob Herring76c05c82011-08-10 16:31:46 -0500311 if (chip->irq_base == NO_IRQ)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700312 return 0;
313
314 writeb(0, chip->base + GPIOIE); /* disable irqs */
315 irq = dev->irq[0];
316 if (irq < 0) {
317 ret = -ENODEV;
318 goto iounmap;
319 }
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000320 irq_set_chained_handler(irq, pl061_irq_handler);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700321 if (!test_and_set_bit(irq, init_irq)) { /* list initialized? */
322 chip_list = kmalloc(sizeof(*chip_list), GFP_KERNEL);
323 if (chip_list == NULL) {
Baruch Siach79d7f4e2009-06-30 11:41:38 -0700324 clear_bit(irq, init_irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700325 ret = -ENOMEM;
326 goto iounmap;
327 }
328 INIT_LIST_HEAD(chip_list);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000329 irq_set_handler_data(irq, chip_list);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700330 } else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000331 chip_list = irq_get_handler_data(irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700332 list_add(&chip->list, chip_list);
333
334 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500335 if (pdata) {
336 if (pdata->directions & (1 << i))
337 pl061_direction_output(&chip->gc, i,
338 pdata->values & (1 << i));
339 else
340 pl061_direction_input(&chip->gc, i);
341 }
Baruch Siach1e9c2852009-06-18 16:48:58 -0700342
Thomas Gleixner08f1b802011-03-24 21:27:37 +0000343 irq_set_chip_and_handler(i + chip->irq_base, &pl061_irqchip,
344 handle_simple_irq);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700345 set_irq_flags(i+chip->irq_base, IRQF_VALID);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000346 irq_set_chip_data(i + chip->irq_base, chip);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700347 }
348
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530349 amba_set_drvdata(dev, chip);
350
Baruch Siach1e9c2852009-06-18 16:48:58 -0700351 return 0;
352
353iounmap:
354 iounmap(chip->base);
355release_region:
356 release_mem_region(dev->res.start, resource_size(&dev->res));
357free_mem:
358 kfree(chip);
359
360 return ret;
361}
362
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530363#ifdef CONFIG_PM
364static int pl061_suspend(struct device *dev)
365{
366 struct pl061_gpio *chip = dev_get_drvdata(dev);
367 int offset;
368
369 chip->csave_regs.gpio_data = 0;
370 chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
371 chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
372 chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
373 chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
374 chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
375
376 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
377 if (chip->csave_regs.gpio_dir & (1 << offset))
378 chip->csave_regs.gpio_data |=
379 pl061_get_value(&chip->gc, offset) << offset;
380 }
381
382 return 0;
383}
384
385static int pl061_resume(struct device *dev)
386{
387 struct pl061_gpio *chip = dev_get_drvdata(dev);
388 int offset;
389
390 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
391 if (chip->csave_regs.gpio_dir & (1 << offset))
392 pl061_direction_output(&chip->gc, offset,
393 chip->csave_regs.gpio_data &
394 (1 << offset));
395 else
396 pl061_direction_input(&chip->gc, offset);
397 }
398
399 writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
400 writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
401 writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
402 writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
403
404 return 0;
405}
406
407static SIMPLE_DEV_PM_OPS(pl061_dev_pm_ops, pl061_suspend, pl061_resume);
408#endif
409
Russell King2c39c9e2010-07-27 08:50:16 +0100410static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700411 {
412 .id = 0x00041061,
413 .mask = 0x000fffff,
414 },
415 { 0, 0 },
416};
417
418static struct amba_driver pl061_gpio_driver = {
419 .drv = {
420 .name = "pl061_gpio",
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530421#ifdef CONFIG_PM
422 .pm = &pl061_dev_pm_ops,
423#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -0700424 },
425 .id_table = pl061_ids,
426 .probe = pl061_probe,
427};
428
429static int __init pl061_gpio_init(void)
430{
431 return amba_driver_register(&pl061_gpio_driver);
432}
433subsys_initcall(pl061_gpio_init);
434
435MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
436MODULE_DESCRIPTION("PL061 GPIO driver");
437MODULE_LICENSE("GPL");