2 * Copyright (C) 2008, 2009 Provigent Ltd.
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.
8 * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
10 * Data sheet: ARM DDI 0190B, September 2000
12 #include <linux/spinlock.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/irq.h>
18 #include <linux/irqchip/chained_irq.h>
19 #include <linux/bitops.h>
20 #include <linux/gpio.h>
21 #include <linux/device.h>
22 #include <linux/amba/bus.h>
23 #include <linux/amba/pl061.h>
24 #include <linux/slab.h>
25 #include <linux/pinctrl/consumer.h>
37 #define PL061_GPIO_NR 8
40 struct pl061_context_save_regs {
57 struct pl061_context_save_regs csave_regs;
61 static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
63 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
65 unsigned char gpiodir;
67 if (offset >= gc->ngpio)
70 spin_lock_irqsave(&chip->lock, flags);
71 gpiodir = readb(chip->base + GPIODIR);
72 gpiodir &= ~(BIT(offset));
73 writeb(gpiodir, chip->base + GPIODIR);
74 spin_unlock_irqrestore(&chip->lock, flags);
79 static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
82 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
84 unsigned char gpiodir;
86 if (offset >= gc->ngpio)
89 spin_lock_irqsave(&chip->lock, flags);
90 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
91 gpiodir = readb(chip->base + GPIODIR);
92 gpiodir |= BIT(offset);
93 writeb(gpiodir, chip->base + GPIODIR);
96 * gpio value is set again, because pl061 doesn't allow to set value of
97 * a gpio pin before configuring it in OUT mode.
99 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
100 spin_unlock_irqrestore(&chip->lock, flags);
105 static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
107 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
109 return !!readb(chip->base + (BIT(offset + 2)));
112 static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
114 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
116 writeb(!!value << offset, chip->base + (BIT(offset + 2)));
119 static int pl061_irq_type(struct irq_data *d, unsigned trigger)
121 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
122 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
123 int offset = irqd_to_hwirq(d);
125 u8 gpiois, gpioibe, gpioiev;
126 u8 bit = BIT(offset);
128 if (offset < 0 || offset >= PL061_GPIO_NR)
131 if ((trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) &&
132 (trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)))
135 "trying to configure line %d for both level and edge "
136 "detection, choose one!\n",
142 spin_lock_irqsave(&chip->lock, flags);
144 gpioiev = readb(chip->base + GPIOIEV);
145 gpiois = readb(chip->base + GPIOIS);
146 gpioibe = readb(chip->base + GPIOIBE);
148 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
149 bool polarity = trigger & IRQ_TYPE_LEVEL_HIGH;
151 /* Disable edge detection */
153 /* Enable level detection */
155 /* Select polarity */
160 irq_set_handler_locked(d, handle_level_irq);
161 dev_dbg(gc->dev, "line %d: IRQ on %s level\n",
163 polarity ? "HIGH" : "LOW");
164 } else if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
165 /* Disable level detection */
167 /* Select both edges, setting this makes GPIOEV be ignored */
169 irq_set_handler_locked(d, handle_edge_irq);
170 dev_dbg(gc->dev, "line %d: IRQ on both edges\n", offset);
171 } else if ((trigger & IRQ_TYPE_EDGE_RISING) ||
172 (trigger & IRQ_TYPE_EDGE_FALLING)) {
173 bool rising = trigger & IRQ_TYPE_EDGE_RISING;
175 /* Disable level detection */
177 /* Clear detection on both edges */
184 irq_set_handler_locked(d, handle_edge_irq);
185 dev_dbg(gc->dev, "line %d: IRQ on %s edge\n",
187 rising ? "RISING" : "FALLING");
189 /* No trigger: disable everything */
193 irq_set_handler_locked(d, handle_bad_irq);
194 dev_warn(gc->dev, "no trigger selected for line %d\n",
198 writeb(gpiois, chip->base + GPIOIS);
199 writeb(gpioibe, chip->base + GPIOIBE);
200 writeb(gpioiev, chip->base + GPIOIEV);
202 spin_unlock_irqrestore(&chip->lock, flags);
207 static void pl061_irq_handler(struct irq_desc *desc)
209 unsigned long pending;
211 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
212 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
213 struct irq_chip *irqchip = irq_desc_get_chip(desc);
215 chained_irq_enter(irqchip, desc);
217 pending = readb(chip->base + GPIOMIS);
219 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
220 generic_handle_irq(irq_find_mapping(gc->irqdomain,
224 chained_irq_exit(irqchip, desc);
227 static void pl061_irq_mask(struct irq_data *d)
229 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
230 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
231 u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
234 spin_lock(&chip->lock);
235 gpioie = readb(chip->base + GPIOIE) & ~mask;
236 writeb(gpioie, chip->base + GPIOIE);
237 spin_unlock(&chip->lock);
240 static void pl061_irq_unmask(struct irq_data *d)
242 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
243 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
244 u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
247 spin_lock(&chip->lock);
248 gpioie = readb(chip->base + GPIOIE) | mask;
249 writeb(gpioie, chip->base + GPIOIE);
250 spin_unlock(&chip->lock);
254 * pl061_irq_ack() - ACK an edge IRQ
255 * @d: IRQ data for this IRQ
257 * This gets called from the edge IRQ handler to ACK the edge IRQ
258 * in the GPIOIC (interrupt-clear) register. For level IRQs this is
259 * not needed: these go away when the level signal goes away.
261 static void pl061_irq_ack(struct irq_data *d)
263 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
264 struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
265 u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
267 spin_lock(&chip->lock);
268 writeb(mask, chip->base + GPIOIC);
269 spin_unlock(&chip->lock);
272 static struct irq_chip pl061_irqchip = {
274 .irq_ack = pl061_irq_ack,
275 .irq_mask = pl061_irq_mask,
276 .irq_unmask = pl061_irq_unmask,
277 .irq_set_type = pl061_irq_type,
280 static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
282 struct device *dev = &adev->dev;
283 struct pl061_platform_data *pdata = dev_get_platdata(dev);
284 struct pl061_gpio *chip;
285 int ret, irq, i, irq_base;
287 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
292 chip->gc.base = pdata->gpio_base;
293 irq_base = pdata->irq_base;
295 dev_err(&adev->dev, "invalid IRQ base in pdata\n");
303 chip->base = devm_ioremap_resource(dev, &adev->res);
304 if (IS_ERR(chip->base))
305 return PTR_ERR(chip->base);
307 spin_lock_init(&chip->lock);
308 if (of_property_read_bool(dev->of_node, "gpio-ranges")) {
309 chip->gc.request = gpiochip_generic_request;
310 chip->gc.free = gpiochip_generic_free;
313 chip->gc.direction_input = pl061_direction_input;
314 chip->gc.direction_output = pl061_direction_output;
315 chip->gc.get = pl061_get_value;
316 chip->gc.set = pl061_set_value;
317 chip->gc.ngpio = PL061_GPIO_NR;
318 chip->gc.label = dev_name(dev);
320 chip->gc.owner = THIS_MODULE;
322 ret = gpiochip_add(&chip->gc);
329 writeb(0, chip->base + GPIOIE); /* disable irqs */
332 dev_err(&adev->dev, "invalid IRQ\n");
336 ret = gpiochip_irqchip_add(&chip->gc, &pl061_irqchip,
337 irq_base, handle_bad_irq,
340 dev_info(&adev->dev, "could not add irqchip\n");
343 gpiochip_set_chained_irqchip(&chip->gc, &pl061_irqchip,
344 irq, pl061_irq_handler);
346 for (i = 0; i < PL061_GPIO_NR; i++) {
348 if (pdata->directions & (BIT(i)))
349 pl061_direction_output(&chip->gc, i,
350 pdata->values & (BIT(i)));
352 pl061_direction_input(&chip->gc, i);
356 amba_set_drvdata(adev, chip);
357 dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
364 static int pl061_suspend(struct device *dev)
366 struct pl061_gpio *chip = dev_get_drvdata(dev);
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);
376 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
377 if (chip->csave_regs.gpio_dir & (BIT(offset)))
378 chip->csave_regs.gpio_data |=
379 pl061_get_value(&chip->gc, offset) << offset;
385 static int pl061_resume(struct device *dev)
387 struct pl061_gpio *chip = dev_get_drvdata(dev);
390 for (offset = 0; offset < PL061_GPIO_NR; offset++) {
391 if (chip->csave_regs.gpio_dir & (BIT(offset)))
392 pl061_direction_output(&chip->gc, offset,
393 chip->csave_regs.gpio_data &
396 pl061_direction_input(&chip->gc, offset);
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);
407 static const struct dev_pm_ops pl061_dev_pm_ops = {
408 .suspend = pl061_suspend,
409 .resume = pl061_resume,
410 .freeze = pl061_suspend,
411 .restore = pl061_resume,
415 static struct amba_id pl061_ids[] = {
423 MODULE_DEVICE_TABLE(amba, pl061_ids);
425 static struct amba_driver pl061_gpio_driver = {
427 .name = "pl061_gpio",
429 .pm = &pl061_dev_pm_ops,
432 .id_table = pl061_ids,
433 .probe = pl061_probe,
436 static int __init pl061_gpio_init(void)
438 return amba_driver_register(&pl061_gpio_driver);
440 module_init(pl061_gpio_init);
442 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
443 MODULE_DESCRIPTION("PL061 GPIO driver");
444 MODULE_LICENSE("GPL");