blob: 06ed257c5d31186f5018072f971751c5f97d5dac [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>
Haojian Zhuang39b70ee2013-02-17 19:42:51 +080026#include <linux/pinctrl/consumer.h>
Deepak Sikrie198a8d2011-11-18 15:20:12 +053027#include <linux/pm.h>
Rob Herringdece9042011-12-09 14:12:53 -060028#include <asm/mach/irq.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070029
30#define GPIODIR 0x400
31#define GPIOIS 0x404
32#define GPIOIBE 0x408
33#define GPIOIEV 0x40C
34#define GPIOIE 0x410
35#define GPIORIS 0x414
36#define GPIOMIS 0x418
37#define GPIOIC 0x41C
38
39#define PL061_GPIO_NR 8
40
Deepak Sikrie198a8d2011-11-18 15:20:12 +053041#ifdef CONFIG_PM
42struct pl061_context_save_regs {
43 u8 gpio_data;
44 u8 gpio_dir;
45 u8 gpio_is;
46 u8 gpio_ibe;
47 u8 gpio_iev;
48 u8 gpio_ie;
49};
50#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070051
Baruch Siach1e9c2852009-06-18 16:48:58 -070052struct pl061_gpio {
Baruch Siach835c1922012-11-22 11:46:14 +020053 spinlock_t lock;
Baruch Siach1e9c2852009-06-18 16:48:58 -070054
55 void __iomem *base;
Haojian Zhuangf1f70472013-02-17 19:42:49 +080056 struct irq_domain *domain;
Baruch Siach1e9c2852009-06-18 16:48:58 -070057 struct gpio_chip gc;
Deepak Sikrie198a8d2011-11-18 15:20:12 +053058
59#ifdef CONFIG_PM
60 struct pl061_context_save_regs csave_regs;
61#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070062};
63
Haojian Zhuang39b70ee2013-02-17 19:42:51 +080064static int pl061_gpio_request(struct gpio_chip *chip, unsigned offset)
65{
66 /*
67 * Map back to global GPIO space and request muxing, the direction
68 * parameter does not matter for this controller.
69 */
70 int gpio = chip->base + offset;
71
72 return pinctrl_request_gpio(gpio);
73}
74
Baruch Siach1e9c2852009-06-18 16:48:58 -070075static 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
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800137 return irq_create_mapping(chip->domain, offset);
Baruch Siach50efacf2009-06-30 11:41:39 -0700138}
139
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800140static int pl061_irq_type(struct irq_data *d, unsigned trigger)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700141{
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800142 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
143 int offset = irqd_to_hwirq(d);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700144 unsigned long flags;
145 u8 gpiois, gpioibe, gpioiev;
146
Axel Linc1cc9b92010-05-26 14:42:19 -0700147 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700148 return -EINVAL;
149
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800150 spin_lock_irqsave(&chip->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700151
152 gpioiev = readb(chip->base + GPIOIEV);
153
154 gpiois = readb(chip->base + GPIOIS);
155 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
156 gpiois |= 1 << offset;
157 if (trigger & IRQ_TYPE_LEVEL_HIGH)
158 gpioiev |= 1 << offset;
159 else
160 gpioiev &= ~(1 << offset);
161 } else
162 gpiois &= ~(1 << offset);
163 writeb(gpiois, chip->base + GPIOIS);
164
165 gpioibe = readb(chip->base + GPIOIBE);
166 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
167 gpioibe |= 1 << offset;
168 else {
169 gpioibe &= ~(1 << offset);
170 if (trigger & IRQ_TYPE_EDGE_RISING)
171 gpioiev |= 1 << offset;
viresh kumardb7e1bc2010-04-29 12:22:52 +0100172 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700173 gpioiev &= ~(1 << offset);
174 }
175 writeb(gpioibe, chip->base + GPIOIBE);
176
177 writeb(gpioiev, chip->base + GPIOIEV);
178
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800179 spin_unlock_irqrestore(&chip->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700180
181 return 0;
182}
183
Baruch Siach1e9c2852009-06-18 16:48:58 -0700184static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
185{
Rob Herring2de0dbc2012-01-04 10:36:07 -0600186 unsigned long pending;
187 int offset;
188 struct pl061_gpio *chip = irq_desc_get_handler_data(desc);
Rob Herringdece9042011-12-09 14:12:53 -0600189 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700190
Rob Herringdece9042011-12-09 14:12:53 -0600191 chained_irq_enter(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700192
Rob Herring2de0dbc2012-01-04 10:36:07 -0600193 pending = readb(chip->base + GPIOMIS);
194 writeb(pending, chip->base + GPIOIC);
195 if (pending) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800196 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Baruch Siach50efacf2009-06-30 11:41:39 -0700197 generic_handle_irq(pl061_to_irq(&chip->gc, offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700198 }
Rob Herring2de0dbc2012-01-04 10:36:07 -0600199
Rob Herringdece9042011-12-09 14:12:53 -0600200 chained_irq_exit(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700201}
202
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800203static void pl061_irq_mask(struct irq_data *d)
Rob Herring3ab52472011-10-21 08:05:53 -0500204{
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800205 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;
Rob Herring3ab52472011-10-21 08:05:53 -0500208
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800209 spin_lock(&chip->lock);
210 gpioie = readb(chip->base + GPIOIE) & ~mask;
211 writeb(gpioie, chip->base + GPIOIE);
212 spin_unlock(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700213}
214
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800215static void pl061_irq_unmask(struct irq_data *d)
216{
217 struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
218 u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR);
219 u8 gpioie;
220
221 spin_lock(&chip->lock);
222 gpioie = readb(chip->base + GPIOIE) | mask;
223 writeb(gpioie, chip->base + GPIOIE);
224 spin_unlock(&chip->lock);
225}
226
227static struct irq_chip pl061_irqchip = {
228 .name = "pl061 gpio",
229 .irq_mask = pl061_irq_mask,
230 .irq_unmask = pl061_irq_unmask,
231 .irq_set_type = pl061_irq_type,
232};
233
234static int pl061_irq_map(struct irq_domain *d, unsigned int virq,
235 irq_hw_number_t hw)
236{
237 struct pl061_gpio *chip = d->host_data;
238
239 irq_set_chip_and_handler_name(virq, &pl061_irqchip, handle_simple_irq,
240 "pl061");
241 irq_set_chip_data(virq, chip);
242 irq_set_irq_type(virq, IRQ_TYPE_NONE);
243
244 return 0;
245}
246
247static const struct irq_domain_ops pl061_domain_ops = {
248 .map = pl061_irq_map,
249 .xlate = irq_domain_xlate_twocell,
250};
251
Tobias Klauser8944df72012-10-05 11:45:28 +0200252static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700253{
Tobias Klauser8944df72012-10-05 11:45:28 +0200254 struct device *dev = &adev->dev;
255 struct pl061_platform_data *pdata = dev->platform_data;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700256 struct pl061_gpio *chip;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800257 int ret, irq, i, irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700258
Tobias Klauser8944df72012-10-05 11:45:28 +0200259 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700260 if (chip == NULL)
261 return -ENOMEM;
262
Rob Herring76c05c82011-08-10 16:31:46 -0500263 if (pdata) {
264 chip->gc.base = pdata->gpio_base;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800265 irq_base = pdata->irq_base;
266 if (irq_base <= 0)
267 return -ENODEV;
268 } else {
Rob Herring76c05c82011-08-10 16:31:46 -0500269 chip->gc.base = -1;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800270 irq_base = 0;
271 }
Rob Herring76c05c82011-08-10 16:31:46 -0500272
Tobias Klauser8944df72012-10-05 11:45:28 +0200273 if (!devm_request_mem_region(dev, adev->res.start,
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800274 resource_size(&adev->res), "pl061"))
Tobias Klauser8944df72012-10-05 11:45:28 +0200275 return -EBUSY;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700276
Tobias Klauser8944df72012-10-05 11:45:28 +0200277 chip->base = devm_ioremap(dev, adev->res.start,
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800278 resource_size(&adev->res));
279 if (!chip->base)
Tobias Klauser8944df72012-10-05 11:45:28 +0200280 return -ENOMEM;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700281
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800282 chip->domain = irq_domain_add_simple(adev->dev.of_node, PL061_GPIO_NR,
283 irq_base, &pl061_domain_ops, chip);
284 if (!chip->domain)
285 return -ENODEV;
286
Baruch Siach1e9c2852009-06-18 16:48:58 -0700287 spin_lock_init(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700288
Haojian Zhuang39b70ee2013-02-17 19:42:51 +0800289 chip->gc.request = pl061_gpio_request;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700290 chip->gc.direction_input = pl061_direction_input;
291 chip->gc.direction_output = pl061_direction_output;
292 chip->gc.get = pl061_get_value;
293 chip->gc.set = pl061_set_value;
Baruch Siach50efacf2009-06-30 11:41:39 -0700294 chip->gc.to_irq = pl061_to_irq;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700295 chip->gc.ngpio = PL061_GPIO_NR;
Tobias Klauser8944df72012-10-05 11:45:28 +0200296 chip->gc.label = dev_name(dev);
297 chip->gc.dev = dev;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700298 chip->gc.owner = THIS_MODULE;
299
Baruch Siach1e9c2852009-06-18 16:48:58 -0700300 ret = gpiochip_add(&chip->gc);
301 if (ret)
Tobias Klauser8944df72012-10-05 11:45:28 +0200302 return ret;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700303
304 /*
305 * irq_chip support
306 */
Baruch Siach1e9c2852009-06-18 16:48:58 -0700307 writeb(0, chip->base + GPIOIE); /* disable irqs */
Tobias Klauser8944df72012-10-05 11:45:28 +0200308 irq = adev->irq[0];
309 if (irq < 0)
310 return -ENODEV;
311
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000312 irq_set_chained_handler(irq, pl061_irq_handler);
Rob Herring2de0dbc2012-01-04 10:36:07 -0600313 irq_set_handler_data(irq, chip);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700314
315 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500316 if (pdata) {
317 if (pdata->directions & (1 << i))
318 pl061_direction_output(&chip->gc, i,
319 pdata->values & (1 << i));
320 else
321 pl061_direction_input(&chip->gc, i);
322 }
Baruch Siach1e9c2852009-06-18 16:48:58 -0700323 }
324
Tobias Klauser8944df72012-10-05 11:45:28 +0200325 amba_set_drvdata(adev, chip);
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530326
Baruch Siach1e9c2852009-06-18 16:48:58 -0700327 return 0;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700328}
329
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530330#ifdef CONFIG_PM
331static int pl061_suspend(struct device *dev)
332{
333 struct pl061_gpio *chip = dev_get_drvdata(dev);
334 int offset;
335
336 chip->csave_regs.gpio_data = 0;
337 chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
338 chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
339 chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
340 chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
341 chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
342
343 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
344 if (chip->csave_regs.gpio_dir & (1 << offset))
345 chip->csave_regs.gpio_data |=
346 pl061_get_value(&chip->gc, offset) << offset;
347 }
348
349 return 0;
350}
351
352static int pl061_resume(struct device *dev)
353{
354 struct pl061_gpio *chip = dev_get_drvdata(dev);
355 int offset;
356
357 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
358 if (chip->csave_regs.gpio_dir & (1 << offset))
359 pl061_direction_output(&chip->gc, offset,
360 chip->csave_regs.gpio_data &
361 (1 << offset));
362 else
363 pl061_direction_input(&chip->gc, offset);
364 }
365
366 writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
367 writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
368 writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
369 writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
370
371 return 0;
372}
373
Viresh Kumar6e33ace2012-01-11 15:25:20 +0530374static const struct dev_pm_ops pl061_dev_pm_ops = {
375 .suspend = pl061_suspend,
376 .resume = pl061_resume,
377 .freeze = pl061_suspend,
378 .restore = pl061_resume,
379};
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530380#endif
381
Russell King2c39c9e2010-07-27 08:50:16 +0100382static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700383 {
384 .id = 0x00041061,
385 .mask = 0x000fffff,
386 },
387 { 0, 0 },
388};
389
Dave Martin955b6782011-10-05 15:15:21 +0100390MODULE_DEVICE_TABLE(amba, pl061_ids);
391
Baruch Siach1e9c2852009-06-18 16:48:58 -0700392static struct amba_driver pl061_gpio_driver = {
393 .drv = {
394 .name = "pl061_gpio",
Deepak Sikrie198a8d2011-11-18 15:20:12 +0530395#ifdef CONFIG_PM
396 .pm = &pl061_dev_pm_ops,
397#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -0700398 },
399 .id_table = pl061_ids,
400 .probe = pl061_probe,
401};
402
403static int __init pl061_gpio_init(void)
404{
405 return amba_driver_register(&pl061_gpio_driver);
406}
Haojian Zhuang5985d762013-01-18 15:31:13 +0800407module_init(pl061_gpio_init);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700408
409MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
410MODULE_DESCRIPTION("PL061 GPIO driver");
411MODULE_LICENSE("GPL");