blob: 84b49cfb81a8b3a12f0c8c3ab2e52c6cb83bf924 [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>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000018#include <linux/irqchip/chained_irq.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070019#include <linux/bitops.h>
Baruch Siach1e9c2852009-06-18 16:48:58 -070020#include <linux/gpio.h>
21#include <linux/device.h>
22#include <linux/amba/bus.h>
23#include <linux/amba/pl061.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Haojian Zhuang39b70ee2013-02-17 19:42:51 +080025#include <linux/pinctrl/consumer.h>
Deepak Sikrie198a8de2011-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 Sikrie198a8de2011-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
Baruch Siach1e9c2852009-06-18 16:48:58 -070049
Baruch Siach1e9c2852009-06-18 16:48:58 -070050struct pl061_gpio {
Baruch Siach835c1922012-11-22 11:46:14 +020051 spinlock_t lock;
Baruch Siach1e9c2852009-06-18 16:48:58 -070052
53 void __iomem *base;
Baruch Siach1e9c2852009-06-18 16:48:58 -070054 struct gpio_chip gc;
Deepak Sikrie198a8de2011-11-18 15:20:12 +053055
56#ifdef CONFIG_PM
57 struct pl061_context_save_regs csave_regs;
58#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -070059};
60
Haojian Zhuang39b70ee2013-02-17 19:42:51 +080061static int pl061_gpio_request(struct gpio_chip *chip, unsigned offset)
62{
63 /*
64 * Map back to global GPIO space and request muxing, the direction
65 * parameter does not matter for this controller.
66 */
67 int gpio = chip->base + offset;
68
69 return pinctrl_request_gpio(gpio);
70}
71
Axel Lin22ce4462013-03-15 20:52:07 +080072static void pl061_gpio_free(struct gpio_chip *chip, unsigned offset)
73{
74 int gpio = chip->base + offset;
75
76 pinctrl_free_gpio(gpio);
77}
78
Baruch Siach1e9c2852009-06-18 16:48:58 -070079static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
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 gpiodir = readb(chip->base + GPIODIR);
Javier Martinez Canillasbea41502014-04-27 02:00:50 +020090 gpiodir &= ~(BIT(offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -070091 writeb(gpiodir, chip->base + GPIODIR);
92 spin_unlock_irqrestore(&chip->lock, flags);
93
94 return 0;
95}
96
97static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
98 int value)
99{
100 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
101 unsigned long flags;
102 unsigned char gpiodir;
103
104 if (offset >= gc->ngpio)
105 return -EINVAL;
106
107 spin_lock_irqsave(&chip->lock, flags);
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200108 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700109 gpiodir = readb(chip->base + GPIODIR);
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200110 gpiodir |= BIT(offset);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700111 writeb(gpiodir, chip->base + GPIODIR);
viresh kumar64b997c52010-04-21 09:42:05 +0100112
113 /*
114 * gpio value is set again, because pl061 doesn't allow to set value of
115 * a gpio pin before configuring it in OUT mode.
116 */
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200117 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700118 spin_unlock_irqrestore(&chip->lock, flags);
119
120 return 0;
121}
122
123static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
124{
125 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
126
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200127 return !!readb(chip->base + (BIT(offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700128}
129
130static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
131{
132 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
133
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200134 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700135}
136
Lennert Buytenhekb2221862011-01-12 17:00:16 -0800137static int pl061_irq_type(struct irq_data *d, unsigned trigger)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700138{
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100139 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
140 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800141 int offset = irqd_to_hwirq(d);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700142 unsigned long flags;
143 u8 gpiois, gpioibe, gpioiev;
Linus Walleij438a2c92013-11-26 12:59:51 +0100144 u8 bit = BIT(offset);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700145
Axel Linc1cc9b92010-05-26 14:42:19 -0700146 if (offset < 0 || offset >= PL061_GPIO_NR)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700147 return -EINVAL;
148
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800149 spin_lock_irqsave(&chip->lock, flags);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700150
151 gpioiev = readb(chip->base + GPIOIEV);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700152 gpiois = readb(chip->base + GPIOIS);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700153 gpioibe = readb(chip->base + GPIOIBE);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700154
Linus Walleij438a2c92013-11-26 12:59:51 +0100155 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
156 gpiois |= bit;
157 if (trigger & IRQ_TYPE_LEVEL_HIGH)
158 gpioiev |= bit;
159 else
160 gpioiev &= ~bit;
161 } else
162 gpiois &= ~bit;
163
164 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
165 /* Setting this makes GPIOEV be ignored */
166 gpioibe |= bit;
167 else {
168 gpioibe &= ~bit;
169 if (trigger & IRQ_TYPE_EDGE_RISING)
170 gpioiev |= bit;
171 else if (trigger & IRQ_TYPE_EDGE_FALLING)
172 gpioiev &= ~bit;
173 }
174
175 writeb(gpiois, chip->base + GPIOIS);
176 writeb(gpioibe, chip->base + GPIOIBE);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700177 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;
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100188 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
189 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
Rob Herringdece9042011-12-09 14:12:53 -0600190 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700191
Rob Herringdece9042011-12-09 14:12:53 -0600192 chained_irq_enter(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700193
Rob Herring2de0dbc2012-01-04 10:36:07 -0600194 pending = readb(chip->base + GPIOMIS);
195 writeb(pending, chip->base + GPIOIC);
196 if (pending) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800197 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100198 generic_handle_irq(irq_find_mapping(gc->irqdomain,
199 offset));
Baruch Siach1e9c2852009-06-18 16:48:58 -0700200 }
Rob Herring2de0dbc2012-01-04 10:36:07 -0600201
Rob Herringdece9042011-12-09 14:12:53 -0600202 chained_irq_exit(irqchip, desc);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700203}
204
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800205static void pl061_irq_mask(struct irq_data *d)
Rob Herring3ab52472011-10-21 08:05:53 -0500206{
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100207 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
208 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200209 u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800210 u8 gpioie;
Rob Herring3ab52472011-10-21 08:05:53 -0500211
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800212 spin_lock(&chip->lock);
213 gpioie = readb(chip->base + GPIOIE) & ~mask;
214 writeb(gpioie, chip->base + GPIOIE);
215 spin_unlock(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700216}
217
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800218static void pl061_irq_unmask(struct irq_data *d)
219{
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100220 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
221 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200222 u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800223 u8 gpioie;
224
225 spin_lock(&chip->lock);
226 gpioie = readb(chip->base + GPIOIE) | mask;
227 writeb(gpioie, chip->base + GPIOIE);
228 spin_unlock(&chip->lock);
229}
230
231static struct irq_chip pl061_irqchip = {
Linus Walleij9ae7e9e2013-11-26 14:19:44 +0100232 .name = "pl061",
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800233 .irq_mask = pl061_irq_mask,
234 .irq_unmask = pl061_irq_unmask,
235 .irq_set_type = pl061_irq_type,
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800236};
237
Tobias Klauser8944df72012-10-05 11:45:28 +0200238static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
Baruch Siach1e9c2852009-06-18 16:48:58 -0700239{
Tobias Klauser8944df72012-10-05 11:45:28 +0200240 struct device *dev = &adev->dev;
Jingoo Hane56aee12013-07-30 17:08:05 +0900241 struct pl061_platform_data *pdata = dev_get_platdata(dev);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700242 struct pl061_gpio *chip;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800243 int ret, irq, i, irq_base;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700244
Tobias Klauser8944df72012-10-05 11:45:28 +0200245 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700246 if (chip == NULL)
247 return -ENOMEM;
248
Rob Herring76c05c82011-08-10 16:31:46 -0500249 if (pdata) {
250 chip->gc.base = pdata->gpio_base;
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800251 irq_base = pdata->irq_base;
Linus Walleij78087552013-11-22 10:11:49 +0100252 if (irq_base <= 0) {
253 dev_err(&adev->dev, "invalid IRQ base in pdata\n");
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800254 return -ENODEV;
Linus Walleij78087552013-11-22 10:11:49 +0100255 }
Haojian Zhuangf1f70472013-02-17 19:42:49 +0800256 } 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
Jingoo Han09bafc32014-02-12 11:53:58 +0900261 chip->base = devm_ioremap_resource(dev, &adev->res);
262 if (IS_ERR(chip->base))
263 return PTR_ERR(chip->base);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700264
265 spin_lock_init(&chip->lock);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700266
Haojian Zhuang39b70ee2013-02-17 19:42:51 +0800267 chip->gc.request = pl061_gpio_request;
Axel Lin22ce4462013-03-15 20:52:07 +0800268 chip->gc.free = pl061_gpio_free;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700269 chip->gc.direction_input = pl061_direction_input;
270 chip->gc.direction_output = pl061_direction_output;
271 chip->gc.get = pl061_get_value;
272 chip->gc.set = pl061_set_value;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700273 chip->gc.ngpio = PL061_GPIO_NR;
Tobias Klauser8944df72012-10-05 11:45:28 +0200274 chip->gc.label = dev_name(dev);
275 chip->gc.dev = dev;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700276 chip->gc.owner = THIS_MODULE;
277
Baruch Siach1e9c2852009-06-18 16:48:58 -0700278 ret = gpiochip_add(&chip->gc);
279 if (ret)
Tobias Klauser8944df72012-10-05 11:45:28 +0200280 return ret;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700281
282 /*
283 * irq_chip support
284 */
Baruch Siach1e9c2852009-06-18 16:48:58 -0700285 writeb(0, chip->base + GPIOIE); /* disable irqs */
Tobias Klauser8944df72012-10-05 11:45:28 +0200286 irq = adev->irq[0];
Linus Walleij78087552013-11-22 10:11:49 +0100287 if (irq < 0) {
288 dev_err(&adev->dev, "invalid IRQ\n");
Tobias Klauser8944df72012-10-05 11:45:28 +0200289 return -ENODEV;
Linus Walleij78087552013-11-22 10:11:49 +0100290 }
Tobias Klauser8944df72012-10-05 11:45:28 +0200291
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100292 ret = gpiochip_irqchip_add(&chip->gc, &pl061_irqchip,
293 irq_base, handle_simple_irq,
294 IRQ_TYPE_NONE);
295 if (ret) {
296 dev_info(&adev->dev, "could not add irqchip\n");
297 return ret;
Linus Walleij78087552013-11-22 10:11:49 +0100298 }
Linus Walleij8d5b24b2014-03-25 10:42:35 +0100299 gpiochip_set_chained_irqchip(&chip->gc, &pl061_irqchip,
300 irq, pl061_irq_handler);
Linus Walleij2ba31542013-11-27 08:47:02 +0100301
Baruch Siach1e9c2852009-06-18 16:48:58 -0700302 for (i = 0; i < PL061_GPIO_NR; i++) {
Rob Herring76c05c82011-08-10 16:31:46 -0500303 if (pdata) {
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200304 if (pdata->directions & (BIT(i)))
Rob Herring76c05c82011-08-10 16:31:46 -0500305 pl061_direction_output(&chip->gc, i,
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200306 pdata->values & (BIT(i)));
Rob Herring76c05c82011-08-10 16:31:46 -0500307 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);
Fabio Estevam76b36272014-02-26 08:12:37 -0300313 dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
314 &adev->res.start);
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530315
Baruch Siach1e9c2852009-06-18 16:48:58 -0700316 return 0;
Baruch Siach1e9c2852009-06-18 16:48:58 -0700317}
318
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530319#ifdef CONFIG_PM
320static int pl061_suspend(struct device *dev)
321{
322 struct pl061_gpio *chip = dev_get_drvdata(dev);
323 int offset;
324
325 chip->csave_regs.gpio_data = 0;
326 chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
327 chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
328 chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
329 chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
330 chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
331
332 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200333 if (chip->csave_regs.gpio_dir & (BIT(offset)))
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530334 chip->csave_regs.gpio_data |=
335 pl061_get_value(&chip->gc, offset) << offset;
336 }
337
338 return 0;
339}
340
341static int pl061_resume(struct device *dev)
342{
343 struct pl061_gpio *chip = dev_get_drvdata(dev);
344 int offset;
345
346 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200347 if (chip->csave_regs.gpio_dir & (BIT(offset)))
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530348 pl061_direction_output(&chip->gc, offset,
349 chip->csave_regs.gpio_data &
Javier Martinez Canillasbea41502014-04-27 02:00:50 +0200350 (BIT(offset)));
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530351 else
352 pl061_direction_input(&chip->gc, offset);
353 }
354
355 writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
356 writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
357 writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
358 writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
359
360 return 0;
361}
362
Viresh Kumar6e33ace2012-01-11 15:25:20 +0530363static const struct dev_pm_ops pl061_dev_pm_ops = {
364 .suspend = pl061_suspend,
365 .resume = pl061_resume,
366 .freeze = pl061_suspend,
367 .restore = pl061_resume,
368};
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530369#endif
370
Russell King2c39c9e2010-07-27 08:50:16 +0100371static struct amba_id pl061_ids[] = {
Baruch Siach1e9c2852009-06-18 16:48:58 -0700372 {
373 .id = 0x00041061,
374 .mask = 0x000fffff,
375 },
376 { 0, 0 },
377};
378
Dave Martin955b6782011-10-05 15:15:21 +0100379MODULE_DEVICE_TABLE(amba, pl061_ids);
380
Baruch Siach1e9c2852009-06-18 16:48:58 -0700381static struct amba_driver pl061_gpio_driver = {
382 .drv = {
383 .name = "pl061_gpio",
Deepak Sikrie198a8de2011-11-18 15:20:12 +0530384#ifdef CONFIG_PM
385 .pm = &pl061_dev_pm_ops,
386#endif
Baruch Siach1e9c2852009-06-18 16:48:58 -0700387 },
388 .id_table = pl061_ids,
389 .probe = pl061_probe,
390};
391
392static int __init pl061_gpio_init(void)
393{
394 return amba_driver_register(&pl061_gpio_driver);
395}
Haojian Zhuang5985d762013-01-18 15:31:13 +0800396module_init(pl061_gpio_init);
Baruch Siach1e9c2852009-06-18 16:48:58 -0700397
398MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
399MODULE_DESCRIPTION("PL061 GPIO driver");
400MODULE_LICENSE("GPL");