]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpio/gpio-pl061.c
gpio: pl061: detail IRQ trigger handling
[karo-tx-linux.git] / drivers / gpio / gpio-pl061.c
1 /*
2  * Copyright (C) 2008, 2009 Provigent Ltd.
3  *
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>
15 #include <linux/io.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>
26 #include <linux/pm.h>
27
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
39 #ifdef CONFIG_PM
40 struct 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
49
50 struct pl061_gpio {
51         spinlock_t              lock;
52
53         void __iomem            *base;
54         struct gpio_chip        gc;
55         bool                    uses_pinctrl;
56
57 #ifdef CONFIG_PM
58         struct pl061_context_save_regs csave_regs;
59 #endif
60 };
61
62 static int pl061_gpio_request(struct gpio_chip *gc, unsigned offset)
63 {
64         /*
65          * Map back to global GPIO space and request muxing, the direction
66          * parameter does not matter for this controller.
67          */
68         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
69         int gpio = gc->base + offset;
70
71         if (chip->uses_pinctrl)
72                 return pinctrl_request_gpio(gpio);
73         return 0;
74 }
75
76 static void pl061_gpio_free(struct gpio_chip *gc, unsigned offset)
77 {
78         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
79         int gpio = gc->base + offset;
80
81         if (chip->uses_pinctrl)
82                 pinctrl_free_gpio(gpio);
83 }
84
85 static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
86 {
87         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
88         unsigned long flags;
89         unsigned char gpiodir;
90
91         if (offset >= gc->ngpio)
92                 return -EINVAL;
93
94         spin_lock_irqsave(&chip->lock, flags);
95         gpiodir = readb(chip->base + GPIODIR);
96         gpiodir &= ~(BIT(offset));
97         writeb(gpiodir, chip->base + GPIODIR);
98         spin_unlock_irqrestore(&chip->lock, flags);
99
100         return 0;
101 }
102
103 static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
104                 int value)
105 {
106         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
107         unsigned long flags;
108         unsigned char gpiodir;
109
110         if (offset >= gc->ngpio)
111                 return -EINVAL;
112
113         spin_lock_irqsave(&chip->lock, flags);
114         writeb(!!value << offset, chip->base + (BIT(offset + 2)));
115         gpiodir = readb(chip->base + GPIODIR);
116         gpiodir |= BIT(offset);
117         writeb(gpiodir, chip->base + GPIODIR);
118
119         /*
120          * gpio value is set again, because pl061 doesn't allow to set value of
121          * a gpio pin before configuring it in OUT mode.
122          */
123         writeb(!!value << offset, chip->base + (BIT(offset + 2)));
124         spin_unlock_irqrestore(&chip->lock, flags);
125
126         return 0;
127 }
128
129 static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
130 {
131         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
132
133         return !!readb(chip->base + (BIT(offset + 2)));
134 }
135
136 static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
137 {
138         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
139
140         writeb(!!value << offset, chip->base + (BIT(offset + 2)));
141 }
142
143 static int pl061_irq_type(struct irq_data *d, unsigned trigger)
144 {
145         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
146         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
147         int offset = irqd_to_hwirq(d);
148         unsigned long flags;
149         u8 gpiois, gpioibe, gpioiev;
150         u8 bit = BIT(offset);
151
152         if (offset < 0 || offset >= PL061_GPIO_NR)
153                 return -EINVAL;
154
155         spin_lock_irqsave(&chip->lock, flags);
156
157         gpioiev = readb(chip->base + GPIOIEV);
158         gpiois = readb(chip->base + GPIOIS);
159         gpioibe = readb(chip->base + GPIOIBE);
160
161         if ((trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) &&
162             (trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)))
163         {
164                 dev_err(gc->dev,
165                         "trying to configure line %d for both level and edge "
166                         "detection, choose one!\n",
167                         offset);
168                 return -EINVAL;
169         }
170
171         if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
172                 bool polarity = trigger & IRQ_TYPE_LEVEL_HIGH;
173
174                 /* Disable edge detection */
175                 gpioibe &= ~bit;
176                 /* Enable level detection */
177                 gpiois |= bit;
178                 /* Select polarity */
179                 if (polarity)
180                         gpioiev |= bit;
181                 else
182                         gpioiev &= ~bit;
183
184                 dev_dbg(gc->dev, "line %d: IRQ on %s level\n",
185                         offset,
186                         polarity ? "HIGH" : "LOW");
187         } else if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
188                 /* Disable level detection */
189                 gpiois &= ~bit;
190                 /* Select both edges, setting this makes GPIOEV be ignored */
191                 gpioibe |= bit;
192
193                 dev_dbg(gc->dev, "line %d: IRQ on both edges\n", offset);
194         } else if ((trigger & IRQ_TYPE_EDGE_RISING) ||
195                    (trigger & IRQ_TYPE_EDGE_FALLING)) {
196                 bool rising = trigger & IRQ_TYPE_EDGE_RISING;
197
198                 /* Disable level detection */
199                 gpiois &= ~bit;
200                 /* Clear detection on both edges */
201                 gpioibe &= ~bit;
202                 /* Select edge */
203                 if (rising)
204                         gpioiev |= bit;
205                 else
206                         gpioiev &= ~bit;
207
208                 dev_dbg(gc->dev, "line %d: IRQ on %s edge\n",
209                         offset,
210                         rising ? "RISING" : "FALLING");
211         } else {
212                 /* No trigger: disable everything */
213                 gpiois &= ~bit;
214                 gpioibe &= ~bit;
215                 gpioiev &= ~bit;
216                 dev_warn(gc->dev, "no trigger selected for line %d\n",
217                          offset);
218         }
219
220         writeb(gpiois, chip->base + GPIOIS);
221         writeb(gpioibe, chip->base + GPIOIBE);
222         writeb(gpioiev, chip->base + GPIOIEV);
223
224         spin_unlock_irqrestore(&chip->lock, flags);
225
226         return 0;
227 }
228
229 static void pl061_irq_handler(struct irq_desc *desc)
230 {
231         unsigned long pending;
232         int offset;
233         struct gpio_chip *gc = irq_desc_get_handler_data(desc);
234         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
235         struct irq_chip *irqchip = irq_desc_get_chip(desc);
236
237         chained_irq_enter(irqchip, desc);
238
239         pending = readb(chip->base + GPIOMIS);
240         writeb(pending, chip->base + GPIOIC);
241         if (pending) {
242                 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
243                         generic_handle_irq(irq_find_mapping(gc->irqdomain,
244                                                             offset));
245         }
246
247         chained_irq_exit(irqchip, desc);
248 }
249
250 static void pl061_irq_mask(struct irq_data *d)
251 {
252         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
253         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
254         u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
255         u8 gpioie;
256
257         spin_lock(&chip->lock);
258         gpioie = readb(chip->base + GPIOIE) & ~mask;
259         writeb(gpioie, chip->base + GPIOIE);
260         spin_unlock(&chip->lock);
261 }
262
263 static void pl061_irq_unmask(struct irq_data *d)
264 {
265         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
266         struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
267         u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
268         u8 gpioie;
269
270         spin_lock(&chip->lock);
271         gpioie = readb(chip->base + GPIOIE) | mask;
272         writeb(gpioie, chip->base + GPIOIE);
273         spin_unlock(&chip->lock);
274 }
275
276 static struct irq_chip pl061_irqchip = {
277         .name           = "pl061",
278         .irq_mask       = pl061_irq_mask,
279         .irq_unmask     = pl061_irq_unmask,
280         .irq_set_type   = pl061_irq_type,
281 };
282
283 static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
284 {
285         struct device *dev = &adev->dev;
286         struct pl061_platform_data *pdata = dev_get_platdata(dev);
287         struct pl061_gpio *chip;
288         int ret, irq, i, irq_base;
289
290         chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
291         if (chip == NULL)
292                 return -ENOMEM;
293
294         if (pdata) {
295                 chip->gc.base = pdata->gpio_base;
296                 irq_base = pdata->irq_base;
297                 if (irq_base <= 0) {
298                         dev_err(&adev->dev, "invalid IRQ base in pdata\n");
299                         return -ENODEV;
300                 }
301         } else {
302                 chip->gc.base = -1;
303                 irq_base = 0;
304         }
305
306         chip->base = devm_ioremap_resource(dev, &adev->res);
307         if (IS_ERR(chip->base))
308                 return PTR_ERR(chip->base);
309
310         spin_lock_init(&chip->lock);
311         if (of_property_read_bool(dev->of_node, "gpio-ranges"))
312                 chip->uses_pinctrl = true;
313
314         chip->gc.request = pl061_gpio_request;
315         chip->gc.free = pl061_gpio_free;
316         chip->gc.direction_input = pl061_direction_input;
317         chip->gc.direction_output = pl061_direction_output;
318         chip->gc.get = pl061_get_value;
319         chip->gc.set = pl061_set_value;
320         chip->gc.ngpio = PL061_GPIO_NR;
321         chip->gc.label = dev_name(dev);
322         chip->gc.dev = dev;
323         chip->gc.owner = THIS_MODULE;
324
325         ret = gpiochip_add(&chip->gc);
326         if (ret)
327                 return ret;
328
329         /*
330          * irq_chip support
331          */
332         writeb(0, chip->base + GPIOIE); /* disable irqs */
333         irq = adev->irq[0];
334         if (irq < 0) {
335                 dev_err(&adev->dev, "invalid IRQ\n");
336                 return -ENODEV;
337         }
338
339         ret = gpiochip_irqchip_add(&chip->gc, &pl061_irqchip,
340                                    irq_base, handle_simple_irq,
341                                    IRQ_TYPE_NONE);
342         if (ret) {
343                 dev_info(&adev->dev, "could not add irqchip\n");
344                 return ret;
345         }
346         gpiochip_set_chained_irqchip(&chip->gc, &pl061_irqchip,
347                                      irq, pl061_irq_handler);
348
349         for (i = 0; i < PL061_GPIO_NR; i++) {
350                 if (pdata) {
351                         if (pdata->directions & (BIT(i)))
352                                 pl061_direction_output(&chip->gc, i,
353                                                 pdata->values & (BIT(i)));
354                         else
355                                 pl061_direction_input(&chip->gc, i);
356                 }
357         }
358
359         amba_set_drvdata(adev, chip);
360         dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
361                  &adev->res.start);
362
363         return 0;
364 }
365
366 #ifdef CONFIG_PM
367 static int pl061_suspend(struct device *dev)
368 {
369         struct pl061_gpio *chip = dev_get_drvdata(dev);
370         int offset;
371
372         chip->csave_regs.gpio_data = 0;
373         chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
374         chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
375         chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
376         chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
377         chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
378
379         for (offset = 0; offset < PL061_GPIO_NR; offset++) {
380                 if (chip->csave_regs.gpio_dir & (BIT(offset)))
381                         chip->csave_regs.gpio_data |=
382                                 pl061_get_value(&chip->gc, offset) << offset;
383         }
384
385         return 0;
386 }
387
388 static int pl061_resume(struct device *dev)
389 {
390         struct pl061_gpio *chip = dev_get_drvdata(dev);
391         int offset;
392
393         for (offset = 0; offset < PL061_GPIO_NR; offset++) {
394                 if (chip->csave_regs.gpio_dir & (BIT(offset)))
395                         pl061_direction_output(&chip->gc, offset,
396                                         chip->csave_regs.gpio_data &
397                                         (BIT(offset)));
398                 else
399                         pl061_direction_input(&chip->gc, offset);
400         }
401
402         writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
403         writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
404         writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
405         writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
406
407         return 0;
408 }
409
410 static const struct dev_pm_ops pl061_dev_pm_ops = {
411         .suspend = pl061_suspend,
412         .resume = pl061_resume,
413         .freeze = pl061_suspend,
414         .restore = pl061_resume,
415 };
416 #endif
417
418 static struct amba_id pl061_ids[] = {
419         {
420                 .id     = 0x00041061,
421                 .mask   = 0x000fffff,
422         },
423         { 0, 0 },
424 };
425
426 MODULE_DEVICE_TABLE(amba, pl061_ids);
427
428 static struct amba_driver pl061_gpio_driver = {
429         .drv = {
430                 .name   = "pl061_gpio",
431 #ifdef CONFIG_PM
432                 .pm     = &pl061_dev_pm_ops,
433 #endif
434         },
435         .id_table       = pl061_ids,
436         .probe          = pl061_probe,
437 };
438
439 static int __init pl061_gpio_init(void)
440 {
441         return amba_driver_register(&pl061_gpio_driver);
442 }
443 module_init(pl061_gpio_init);
444
445 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
446 MODULE_DESCRIPTION("PL061 GPIO driver");
447 MODULE_LICENSE("GPL");