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/irqdomain.h>
19 #include <linux/irqchip/chained_irq.h>
20 #include <linux/bitops.h>
21 #include <linux/workqueue.h>
22 #include <linux/gpio.h>
23 #include <linux/device.h>
24 #include <linux/amba/bus.h>
25 #include <linux/amba/pl061.h>
26 #include <linux/slab.h>
27 #include <linux/pinctrl/consumer.h>
39 #define PL061_GPIO_NR 8
42 struct pl061_context_save_regs
{
56 struct irq_domain
*domain
;
60 struct pl061_context_save_regs csave_regs
;
64 static int pl061_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
67 * Map back to global GPIO space and request muxing, the direction
68 * parameter does not matter for this controller.
70 int gpio
= chip
->base
+ offset
;
72 return pinctrl_request_gpio(gpio
);
75 static void pl061_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
77 int gpio
= chip
->base
+ offset
;
79 pinctrl_free_gpio(gpio
);
82 static int pl061_direction_input(struct gpio_chip
*gc
, unsigned offset
)
84 struct pl061_gpio
*chip
= container_of(gc
, struct pl061_gpio
, gc
);
86 unsigned char gpiodir
;
88 if (offset
>= gc
->ngpio
)
91 spin_lock_irqsave(&chip
->lock
, flags
);
92 gpiodir
= readb(chip
->base
+ GPIODIR
);
93 gpiodir
&= ~(1 << offset
);
94 writeb(gpiodir
, chip
->base
+ GPIODIR
);
95 spin_unlock_irqrestore(&chip
->lock
, flags
);
100 static int pl061_direction_output(struct gpio_chip
*gc
, unsigned offset
,
103 struct pl061_gpio
*chip
= container_of(gc
, struct pl061_gpio
, gc
);
105 unsigned char gpiodir
;
107 if (offset
>= gc
->ngpio
)
110 spin_lock_irqsave(&chip
->lock
, flags
);
111 writeb(!!value
<< offset
, chip
->base
+ (1 << (offset
+ 2)));
112 gpiodir
= readb(chip
->base
+ GPIODIR
);
113 gpiodir
|= 1 << offset
;
114 writeb(gpiodir
, chip
->base
+ GPIODIR
);
117 * gpio value is set again, because pl061 doesn't allow to set value of
118 * a gpio pin before configuring it in OUT mode.
120 writeb(!!value
<< offset
, chip
->base
+ (1 << (offset
+ 2)));
121 spin_unlock_irqrestore(&chip
->lock
, flags
);
126 static int pl061_get_value(struct gpio_chip
*gc
, unsigned offset
)
128 struct pl061_gpio
*chip
= container_of(gc
, struct pl061_gpio
, gc
);
130 return !!readb(chip
->base
+ (1 << (offset
+ 2)));
133 static void pl061_set_value(struct gpio_chip
*gc
, unsigned offset
, int value
)
135 struct pl061_gpio
*chip
= container_of(gc
, struct pl061_gpio
, gc
);
137 writeb(!!value
<< offset
, chip
->base
+ (1 << (offset
+ 2)));
140 static int pl061_to_irq(struct gpio_chip
*gc
, unsigned offset
)
142 struct pl061_gpio
*chip
= container_of(gc
, struct pl061_gpio
, gc
);
144 return irq_create_mapping(chip
->domain
, offset
);
147 static int pl061_irq_type(struct irq_data
*d
, unsigned trigger
)
149 struct pl061_gpio
*chip
= irq_data_get_irq_chip_data(d
);
150 int offset
= irqd_to_hwirq(d
);
152 u8 gpiois
, gpioibe
, gpioiev
;
154 if (offset
< 0 || offset
>= PL061_GPIO_NR
)
157 spin_lock_irqsave(&chip
->lock
, flags
);
159 gpioiev
= readb(chip
->base
+ GPIOIEV
);
161 gpiois
= readb(chip
->base
+ GPIOIS
);
162 if (trigger
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
)) {
163 gpiois
|= 1 << offset
;
164 if (trigger
& IRQ_TYPE_LEVEL_HIGH
)
165 gpioiev
|= 1 << offset
;
167 gpioiev
&= ~(1 << offset
);
169 gpiois
&= ~(1 << offset
);
170 writeb(gpiois
, chip
->base
+ GPIOIS
);
172 gpioibe
= readb(chip
->base
+ GPIOIBE
);
173 if ((trigger
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
)
174 gpioibe
|= 1 << offset
;
176 gpioibe
&= ~(1 << offset
);
177 if (trigger
& IRQ_TYPE_EDGE_RISING
)
178 gpioiev
|= 1 << offset
;
179 else if (trigger
& IRQ_TYPE_EDGE_FALLING
)
180 gpioiev
&= ~(1 << offset
);
182 writeb(gpioibe
, chip
->base
+ GPIOIBE
);
184 writeb(gpioiev
, chip
->base
+ GPIOIEV
);
186 spin_unlock_irqrestore(&chip
->lock
, flags
);
191 static void pl061_irq_handler(unsigned irq
, struct irq_desc
*desc
)
193 unsigned long pending
;
195 struct pl061_gpio
*chip
= irq_desc_get_handler_data(desc
);
196 struct irq_chip
*irqchip
= irq_desc_get_chip(desc
);
198 chained_irq_enter(irqchip
, desc
);
200 pending
= readb(chip
->base
+ GPIOMIS
);
201 writeb(pending
, chip
->base
+ GPIOIC
);
203 for_each_set_bit(offset
, &pending
, PL061_GPIO_NR
)
204 generic_handle_irq(pl061_to_irq(&chip
->gc
, offset
));
207 chained_irq_exit(irqchip
, desc
);
210 static void pl061_irq_mask(struct irq_data
*d
)
212 struct pl061_gpio
*chip
= irq_data_get_irq_chip_data(d
);
213 u8 mask
= 1 << (irqd_to_hwirq(d
) % PL061_GPIO_NR
);
216 spin_lock(&chip
->lock
);
217 gpioie
= readb(chip
->base
+ GPIOIE
) & ~mask
;
218 writeb(gpioie
, chip
->base
+ GPIOIE
);
219 spin_unlock(&chip
->lock
);
222 static void pl061_irq_unmask(struct irq_data
*d
)
224 struct pl061_gpio
*chip
= irq_data_get_irq_chip_data(d
);
225 u8 mask
= 1 << (irqd_to_hwirq(d
) % PL061_GPIO_NR
);
228 spin_lock(&chip
->lock
);
229 gpioie
= readb(chip
->base
+ GPIOIE
) | mask
;
230 writeb(gpioie
, chip
->base
+ GPIOIE
);
231 spin_unlock(&chip
->lock
);
234 static struct irq_chip pl061_irqchip
= {
235 .name
= "pl061 gpio",
236 .irq_mask
= pl061_irq_mask
,
237 .irq_unmask
= pl061_irq_unmask
,
238 .irq_set_type
= pl061_irq_type
,
241 static int pl061_irq_map(struct irq_domain
*d
, unsigned int irq
,
242 irq_hw_number_t hwirq
)
244 struct pl061_gpio
*chip
= d
->host_data
;
246 irq_set_chip_and_handler_name(irq
, &pl061_irqchip
, handle_simple_irq
,
248 irq_set_chip_data(irq
, chip
);
249 irq_set_irq_type(irq
, IRQ_TYPE_NONE
);
254 static const struct irq_domain_ops pl061_domain_ops
= {
255 .map
= pl061_irq_map
,
256 .xlate
= irq_domain_xlate_twocell
,
259 static int pl061_probe(struct amba_device
*adev
, const struct amba_id
*id
)
261 struct device
*dev
= &adev
->dev
;
262 struct pl061_platform_data
*pdata
= dev_get_platdata(dev
);
263 struct pl061_gpio
*chip
;
264 int ret
, irq
, i
, irq_base
;
266 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
271 chip
->gc
.base
= pdata
->gpio_base
;
272 irq_base
= pdata
->irq_base
;
280 if (!devm_request_mem_region(dev
, adev
->res
.start
,
281 resource_size(&adev
->res
), "pl061"))
284 chip
->base
= devm_ioremap(dev
, adev
->res
.start
,
285 resource_size(&adev
->res
));
289 spin_lock_init(&chip
->lock
);
291 chip
->gc
.request
= pl061_gpio_request
;
292 chip
->gc
.free
= pl061_gpio_free
;
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
;
297 chip
->gc
.to_irq
= pl061_to_irq
;
298 chip
->gc
.ngpio
= PL061_GPIO_NR
;
299 chip
->gc
.label
= dev_name(dev
);
301 chip
->gc
.owner
= THIS_MODULE
;
303 ret
= gpiochip_add(&chip
->gc
);
310 writeb(0, chip
->base
+ GPIOIE
); /* disable irqs */
315 irq_set_chained_handler(irq
, pl061_irq_handler
);
316 irq_set_handler_data(irq
, chip
);
318 chip
->domain
= irq_domain_add_simple(adev
->dev
.of_node
, PL061_GPIO_NR
,
319 irq_base
, &pl061_domain_ops
, chip
);
323 for (i
= 0; i
< PL061_GPIO_NR
; i
++) {
325 if (pdata
->directions
& (1 << i
))
326 pl061_direction_output(&chip
->gc
, i
,
327 pdata
->values
& (1 << i
));
329 pl061_direction_input(&chip
->gc
, i
);
333 amba_set_drvdata(adev
, chip
);
339 static int pl061_suspend(struct device
*dev
)
341 struct pl061_gpio
*chip
= dev_get_drvdata(dev
);
344 chip
->csave_regs
.gpio_data
= 0;
345 chip
->csave_regs
.gpio_dir
= readb(chip
->base
+ GPIODIR
);
346 chip
->csave_regs
.gpio_is
= readb(chip
->base
+ GPIOIS
);
347 chip
->csave_regs
.gpio_ibe
= readb(chip
->base
+ GPIOIBE
);
348 chip
->csave_regs
.gpio_iev
= readb(chip
->base
+ GPIOIEV
);
349 chip
->csave_regs
.gpio_ie
= readb(chip
->base
+ GPIOIE
);
351 for (offset
= 0; offset
< PL061_GPIO_NR
; offset
++) {
352 if (chip
->csave_regs
.gpio_dir
& (1 << offset
))
353 chip
->csave_regs
.gpio_data
|=
354 pl061_get_value(&chip
->gc
, offset
) << offset
;
360 static int pl061_resume(struct device
*dev
)
362 struct pl061_gpio
*chip
= dev_get_drvdata(dev
);
365 for (offset
= 0; offset
< PL061_GPIO_NR
; offset
++) {
366 if (chip
->csave_regs
.gpio_dir
& (1 << offset
))
367 pl061_direction_output(&chip
->gc
, offset
,
368 chip
->csave_regs
.gpio_data
&
371 pl061_direction_input(&chip
->gc
, offset
);
374 writeb(chip
->csave_regs
.gpio_is
, chip
->base
+ GPIOIS
);
375 writeb(chip
->csave_regs
.gpio_ibe
, chip
->base
+ GPIOIBE
);
376 writeb(chip
->csave_regs
.gpio_iev
, chip
->base
+ GPIOIEV
);
377 writeb(chip
->csave_regs
.gpio_ie
, chip
->base
+ GPIOIE
);
382 static const struct dev_pm_ops pl061_dev_pm_ops
= {
383 .suspend
= pl061_suspend
,
384 .resume
= pl061_resume
,
385 .freeze
= pl061_suspend
,
386 .restore
= pl061_resume
,
390 static struct amba_id pl061_ids
[] = {
398 MODULE_DEVICE_TABLE(amba
, pl061_ids
);
400 static struct amba_driver pl061_gpio_driver
= {
402 .name
= "pl061_gpio",
404 .pm
= &pl061_dev_pm_ops
,
407 .id_table
= pl061_ids
,
408 .probe
= pl061_probe
,
411 static int __init
pl061_gpio_init(void)
413 return amba_driver_register(&pl061_gpio_driver
);
415 module_init(pl061_gpio_init
);
417 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
418 MODULE_DESCRIPTION("PL061 GPIO driver");
419 MODULE_LICENSE("GPL");