blob: d1d603585a9380d031e0811b9c5b8e602b0249fe [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>
Baruch Siach1e9c2852009-06-18 16:48:58 -070015#include <linux/io.h>
16#include <linux/ioport.h>
17#include <linux/irq.h>
Haojian Zhuangf1f70472013-02-17 19:42:49 +080018#include <linux/irqdomain.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070019#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 Sikrie198a8de2011-11-18 15:20:12 +053026#include <linux/pm.h>
Rob Herringdece9042011-12-09 14:12:53 -060027#include <asm/mach/irq.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070028
29#define GPIODIR 0x400
30#define GPIOIS 0x404
31#define GPIOIBE 0x408
32#define GPIOIEV 0x40C
33#define GPIOIE 0x410
34#define GPIORIS 0x414
35#define GPIOMIS 0x418
36#define GPIOIC 0x41C
37
38#define PL061_GPIO_NR 8
39
Deepak Sikrie198a8de2011-11-18 15:20:12 +053040#ifdef CONFIG_PM
41struct pl061_context_save_regs {
42 u8 gpio_data;
43 u8 gpio_dir;
44 u8 gpio_is;
45 u8 gpio_ibe;
46 u8 gpio_iev;
47 u8 gpio_ie;
48};
49#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070050
Baruch Siach1e9c2852009-06-18 16:48:58 -070051struct pl061_gpio {
Baruch Siach835c1922012-11-22 11:46:14 +020052 spinlock_t lock;
Baruch Siach1e9c2852009-06-18 16:48:58 -070053
54 void __iomem *base;
Haojian Zhuangf1f70472013-02-17 19:42:49 +080055 struct irq_domain *domain;
Baruch Siach1e9c2852009-06-18 16:48:58 -070056 struct gpio_chip gc;
Deepak Sikrie198a8de2011-11-18 15:20:12 +053057
58#ifdef CONFIG_PM
59 struct pl061_context_save_regs csave_regs;
60#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070061};
62
63static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
64{
65 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
66 unsigned long flags;
67 unsigned char gpiodir;
68
69 if (offset >= gc->ngpio)
70 return -EINVAL;
71
72 spin_lock_irqsave(&chip->lock, flags);
73 gpiodir = readb(chip->base + GPIODIR);
74 gpiodir &= ~(1 << offset);
75 writeb(gpiodir, chip->base + GPIODIR);
76 spin_unlock_irqrestore(&chip->lock, flags);
77
78 return 0;
79}
80
81static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
82 int value)
83{
84 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
85 unsigned long flags;
86 unsigned char gpiodir;
87
88 if (offset >= gc->ngpio)
89 return -EINVAL;
90
91 spin_lock_irqsave(&chip->lock, flags);
92 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
93 gpiodir = readb(chip->base + GPIODIR);
94 gpiodir |= 1 << offset;
95 writeb(gpiodir, chip->base + GPIODIR);
viresh kumar64b997c52010-04-21 09:42:05 +010096
97 /*
98 * gpio value is set again, because pl061 doesn't allow to set value of
99 * a gpio pin before configuring it in OUT mode.
100 */
101 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700102 spin_unlock_irqrestore(&chip->lock, flags);
103
104 return 0;
105}
106
107static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
108{
109 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
110
111 return !!readb(chip->base + (1 << (offset + 2)));
112}
113
114static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
115{
116 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
117
118 writeb(!!value << offset, chip->base + (1 << (offset + 2)));
119}
120
Baruch Siach50efacf2009-06-30 11:41:39 -0700121static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
122{
123 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
124
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800125 return irq_create_mapping(chip->domain, offset);
Baruch Siach50efacf2009-06-30 11:41:39 -0700126}
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{
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800130 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
131 int offset = irqd_to_hwirq(d);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700132 unsigned long flags;
133 u8 gpiois, gpioibe, gpioiev;
134
Axel Linc1cc9b92010-05-26 14:42:19 -0700135 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700136 return -EINVAL;
137
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800138 spin_lock_irqsave(&chip->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700139
140 gpioiev = readb(chip->base + GPIOIEV);
141
142 gpiois = readb(chip->base + GPIOIS);
143 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
144 gpiois |= 1 << offset;
145 if (trigger & IRQ_TYPE_LEVEL_HIGH)
146 gpioiev |= 1 << offset;
147 else
148 gpioiev &= ~(1 << offset);
149 } else
150 gpiois &= ~(1 << offset);
151 writeb(gpiois, chip->base + GPIOIS);
152
153 gpioibe = readb(chip->base + GPIOIBE);
154 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
155 gpioibe |= 1 << offset;
156 else {
157 gpioibe &= ~(1 << offset);
158 if (trigger & IRQ_TYPE_EDGE_RISING)
159 gpioiev |= 1 << offset;
viresh kumardb7e1bc2010-04-29 12:22:52 +0100160 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700161 gpioiev &= ~(1 << offset);
162 }
163 writeb(gpioibe, chip->base + GPIOIBE);
164
165 writeb(gpioiev, chip->base + GPIOIEV);
166
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800167 spin_unlock_irqrestore(&chip->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700168
169 return 0;
170}
171
Baruch Siach1e9c2852009-06-18 16:48:58 -0700172static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
173{
Rob Herring2de0dbc2012-01-04 10:36:07 -0600174 unsigned long pending;
175 int offset;
176 struct pl061_gpio *chip = irq_desc_get_handler_data(desc);
Rob Herringdece9042011-12-09 14:12:53 -0600177 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700178
Rob Herringdece9042011-12-09 14:12:53 -0600179 chained_irq_enter(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700180
Rob Herring2de0dbc2012-01-04 10:36:07 -0600181 pending = readb(chip->base + GPIOMIS);
182 writeb(pending, chip->base + GPIOIC);
183 if (pending) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800184 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Baruch Siach50efacf2009-06-30 11:41:39 -0700185 generic_handle_irq(pl061_to_irq(&chip->gc, offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700186 }
Rob Herring2de0dbc2012-01-04 10:36:07 -0600187
Rob Herringdece9042011-12-09 14:12:53 -0600188 chained_irq_exit(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700189}
190
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800191static void pl061_irq_mask(struct irq_data *d)
Rob Herring3ab52472011-10-21 08:05:53 -0500192{
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800193 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
194 u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR);
195 u8 gpioie;
Rob Herring3ab52472011-10-21 08:05:53 -0500196
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800197 spin_lock(&chip->lock);
198 gpioie = readb(chip->base + GPIOIE) & ~mask;
199 writeb(gpioie, chip->base + GPIOIE);
200 spin_unlock(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700201}
202
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800203static void pl061_irq_unmask(struct irq_data *d)
204{
205 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
206 u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR);
207 u8 gpioie;
208
209 spin_lock(&chip->lock);
210 gpioie = readb(chip->base + GPIOIE) | mask;
211 writeb(gpioie, chip->base + GPIOIE);
212 spin_unlock(&chip->lock);
213}
214
215static struct irq_chip pl061_irqchip = {
216 .name = "pl061 gpio",
217 .irq_mask = pl061_irq_mask,
218 .irq_unmask = pl061_irq_unmask,
219 .irq_set_type = pl061_irq_type,
220};
221
222static int pl061_irq_map(struct irq_domain *d, unsigned int virq,
223 irq_hw_number_t hw)
224{
225 struct pl061_gpio *chip = d->host_data;
226
227 irq_set_chip_and_handler_name(virq, &pl061_irqchip, handle_simple_irq,
228 "pl061");
229 irq_set_chip_data(virq, chip);
230 irq_set_irq_type(virq, IRQ_TYPE_NONE);
231
232 return 0;
233}
234
235static const struct irq_domain_ops pl061_domain_ops = {
236 .map = pl061_irq_map,
237 .xlate = irq_domain_xlate_twocell,
238};
239
Tobias Klauser8944df72012-10-05 11:45:28 +0200240static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700241{
Tobias Klauser8944df72012-10-05 11:45:28 +0200242 struct device *dev = &adev->dev;
243 struct pl061_platform_data *pdata = dev->platform_data;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700244 struct pl061_gpio *chip;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800245 int ret, irq, i, irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700246
Tobias Klauser8944df72012-10-05 11:45:28 +0200247 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700248 if (chip == NULL)
249 return -ENOMEM;
250
Rob Herring76c05c82011-08-10 16:31:46 -0500251 if (pdata) {
252 chip->gc.base = pdata->gpio_base;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800253 irq_base = pdata->irq_base;
254 if (irq_base <= 0)
255 return -ENODEV;
256 } else {
Rob Herring76c05c82011-08-10 16:31:46 -0500257 chip->gc.base = -1;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800258 irq_base = 0;
259 }
Rob Herring76c05c82011-08-10 16:31:46 -0500260
Tobias Klauser8944df72012-10-05 11:45:28 +0200261 if (!devm_request_mem_region(dev, adev->res.start,
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800262 resource_size(&adev->res), "pl061"))
Tobias Klauser8944df72012-10-05 11:45:28 +0200263 return -EBUSY;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700264
Tobias Klauser8944df72012-10-05 11:45:28 +0200265 chip->base = devm_ioremap(dev, adev->res.start,
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800266 resource_size(&adev->res));
267 if (!chip->base)
Tobias Klauser8944df72012-10-05 11:45:28 +0200268 return -ENOMEM;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700269
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800270 chip->domain = irq_domain_add_simple(adev->dev.of_node, PL061_GPIO_NR,
271 irq_base, &pl061_domain_ops, chip);
272 if (!chip->domain)
273 return -ENODEV;
274
Baruch Siach1e9c2852009-06-18 16:48:58 -0700275 spin_lock_init(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700276
277 chip->gc.direction_input = pl061_direction_input;
278 chip->gc.direction_output = pl061_direction_output;
279 chip->gc.get = pl061_get_value;
280 chip->gc.set = pl061_set_value;
Baruch Siach50efacf2009-06-30 11:41:39 -0700281 chip->gc.to_irq = pl061_to_irq;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700282 chip->gc.ngpio = PL061_GPIO_NR;
Tobias Klauser8944df72012-10-05 11:45:28 +0200283 chip->gc.label = dev_name(dev);
284 chip->gc.dev = dev;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700285 chip->gc.owner = THIS_MODULE;
286
Baruch Siach1e9c2852009-06-18 16:48:58 -0700287 ret = gpiochip_add(&chip->gc);
288 if (ret)
Tobias Klauser8944df72012-10-05 11:45:28 +0200289 return ret;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700290
291 /*
292 * irq_chip support
293 */
Baruch Siach1e9c2852009-06-18 16:48:58 -0700294 writeb(0, chip->base + GPIOIE); /* disable irqs */
Tobias Klauser8944df72012-10-05 11:45:28 +0200295 irq = adev->irq[0];
296 if (irq < 0)
297 return -ENODEV;
298
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000299 irq_set_chained_handler(irq, pl061_irq_handler);
Rob Herring2de0dbc2012-01-04 10:36:07 -0600300 irq_set_handler_data(irq, chip);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700301
302 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500303 if (pdata) {
304 if (pdata->directions & (1 << i))
305 pl061_direction_output(&chip->gc, i,
306 pdata->values & (1 << i));
307 else
308 pl061_direction_input(&chip->gc, i);
309 }
Baruch Siach1e9c2852009-06-18 16:48:58 -0700310 }
311
Tobias Klauser8944df72012-10-05 11:45:28 +0200312 amba_set_drvdata(adev, chip);
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530313
Baruch Siach1e9c2852009-06-18 16:48:58 -0700314 return 0;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700315}
316
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530317#ifdef CONFIG_PM
318static int pl061_suspend(struct device *dev)
319{
320 struct pl061_gpio *chip = dev_get_drvdata(dev);
321 int offset;
322
323 chip->csave_regs.gpio_data = 0;
324 chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
325 chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
326 chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
327 chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
328 chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
329
330 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
331 if (chip->csave_regs.gpio_dir & (1 << offset))
332 chip->csave_regs.gpio_data |=
333 pl061_get_value(&chip->gc, offset) << offset;
334 }
335
336 return 0;
337}
338
339static int pl061_resume(struct device *dev)
340{
341 struct pl061_gpio *chip = dev_get_drvdata(dev);
342 int offset;
343
344 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
345 if (chip->csave_regs.gpio_dir & (1 << offset))
346 pl061_direction_output(&chip->gc, offset,
347 chip->csave_regs.gpio_data &
348 (1 << offset));
349 else
350 pl061_direction_input(&chip->gc, offset);
351 }
352
353 writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
354 writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
355 writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
356 writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
357
358 return 0;
359}
360
Viresh Kumar6e33ace2012-01-11 15:25:20 +0530361static const struct dev_pm_ops pl061_dev_pm_ops = {
362 .suspend = pl061_suspend,
363 .resume = pl061_resume,
364 .freeze = pl061_suspend,
365 .restore = pl061_resume,
366};
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530367#endif
368
Russell King2c39c9e2010-07-27 08:50:16 +0100369static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700370 {
371 .id = 0x00041061,
372 .mask = 0x000fffff,
373 },
374 { 0, 0 },
375};
376
Dave Martin955b6782011-10-05 15:15:21 +0100377MODULE_DEVICE_TABLE(amba, pl061_ids);
378
Baruch Siach1e9c2852009-06-18 16:48:58 -0700379static struct amba_driver pl061_gpio_driver = {
380 .drv = {
381 .name = "pl061_gpio",
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530382#ifdef CONFIG_PM
383 .pm = &pl061_dev_pm_ops,
384#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -0700385 },
386 .id_table = pl061_ids,
387 .probe = pl061_probe,
388};
389
390static int __init pl061_gpio_init(void)
391{
392 return amba_driver_register(&pl061_gpio_driver);
393}
Haojian Zhuang5985d762013-01-18 15:31:13 +0800394module_init(pl061_gpio_init);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700395
396MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
397MODULE_DESCRIPTION("PL061 GPIO driver");
398MODULE_LICENSE("GPL");