Merge branch 'imx/imx6q' into next/soc
[pandora-kernel.git] / drivers / gpio / gpio-mxc.c
1 /*
2  * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
3  * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
4  *
5  * Based on code from Freescale,
6  * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/basic_mmio_gpio.h>
30 #include <linux/of.h>
31 #include <linux/of_device.h>
32 #include <asm-generic/bug.h>
33
34 #define irq_to_gpio(irq)        ((irq) - MXC_GPIO_IRQ_START)
35
36 enum mxc_gpio_hwtype {
37         IMX1_GPIO,      /* runs on i.mx1 */
38         IMX21_GPIO,     /* runs on i.mx21 and i.mx27 */
39         IMX31_GPIO,     /* runs on all other i.mx */
40 };
41
42 /* device type dependent stuff */
43 struct mxc_gpio_hwdata {
44         unsigned dr_reg;
45         unsigned gdir_reg;
46         unsigned psr_reg;
47         unsigned icr1_reg;
48         unsigned icr2_reg;
49         unsigned imr_reg;
50         unsigned isr_reg;
51         unsigned low_level;
52         unsigned high_level;
53         unsigned rise_edge;
54         unsigned fall_edge;
55 };
56
57 struct mxc_gpio_port {
58         struct list_head node;
59         void __iomem *base;
60         int irq;
61         int irq_high;
62         int virtual_irq_start;
63         struct bgpio_chip bgc;
64         u32 both_edges;
65 };
66
67 static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
68         .dr_reg         = 0x1c,
69         .gdir_reg       = 0x00,
70         .psr_reg        = 0x24,
71         .icr1_reg       = 0x28,
72         .icr2_reg       = 0x2c,
73         .imr_reg        = 0x30,
74         .isr_reg        = 0x34,
75         .low_level      = 0x03,
76         .high_level     = 0x02,
77         .rise_edge      = 0x00,
78         .fall_edge      = 0x01,
79 };
80
81 static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
82         .dr_reg         = 0x00,
83         .gdir_reg       = 0x04,
84         .psr_reg        = 0x08,
85         .icr1_reg       = 0x0c,
86         .icr2_reg       = 0x10,
87         .imr_reg        = 0x14,
88         .isr_reg        = 0x18,
89         .low_level      = 0x00,
90         .high_level     = 0x01,
91         .rise_edge      = 0x02,
92         .fall_edge      = 0x03,
93 };
94
95 static enum mxc_gpio_hwtype mxc_gpio_hwtype;
96 static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
97
98 #define GPIO_DR                 (mxc_gpio_hwdata->dr_reg)
99 #define GPIO_GDIR               (mxc_gpio_hwdata->gdir_reg)
100 #define GPIO_PSR                (mxc_gpio_hwdata->psr_reg)
101 #define GPIO_ICR1               (mxc_gpio_hwdata->icr1_reg)
102 #define GPIO_ICR2               (mxc_gpio_hwdata->icr2_reg)
103 #define GPIO_IMR                (mxc_gpio_hwdata->imr_reg)
104 #define GPIO_ISR                (mxc_gpio_hwdata->isr_reg)
105
106 #define GPIO_INT_LOW_LEV        (mxc_gpio_hwdata->low_level)
107 #define GPIO_INT_HIGH_LEV       (mxc_gpio_hwdata->high_level)
108 #define GPIO_INT_RISE_EDGE      (mxc_gpio_hwdata->rise_edge)
109 #define GPIO_INT_FALL_EDGE      (mxc_gpio_hwdata->fall_edge)
110 #define GPIO_INT_NONE           0x4
111
112 static struct platform_device_id mxc_gpio_devtype[] = {
113         {
114                 .name = "imx1-gpio",
115                 .driver_data = IMX1_GPIO,
116         }, {
117                 .name = "imx21-gpio",
118                 .driver_data = IMX21_GPIO,
119         }, {
120                 .name = "imx31-gpio",
121                 .driver_data = IMX31_GPIO,
122         }, {
123                 /* sentinel */
124         }
125 };
126
127 static const struct of_device_id mxc_gpio_dt_ids[] = {
128         { .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
129         { .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
130         { .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
131         { /* sentinel */ }
132 };
133
134 /*
135  * MX2 has one interrupt *for all* gpio ports. The list is used
136  * to save the references to all ports, so that mx2_gpio_irq_handler
137  * can walk through all interrupt status registers.
138  */
139 static LIST_HEAD(mxc_gpio_ports);
140
141 /* Note: This driver assumes 32 GPIOs are handled in one register */
142
143 static int gpio_set_irq_type(struct irq_data *d, u32 type)
144 {
145         u32 gpio = irq_to_gpio(d->irq);
146         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
147         struct mxc_gpio_port *port = gc->private;
148         u32 bit, val;
149         int edge;
150         void __iomem *reg = port->base;
151
152         port->both_edges &= ~(1 << (gpio & 31));
153         switch (type) {
154         case IRQ_TYPE_EDGE_RISING:
155                 edge = GPIO_INT_RISE_EDGE;
156                 break;
157         case IRQ_TYPE_EDGE_FALLING:
158                 edge = GPIO_INT_FALL_EDGE;
159                 break;
160         case IRQ_TYPE_EDGE_BOTH:
161                 val = gpio_get_value(gpio);
162                 if (val) {
163                         edge = GPIO_INT_LOW_LEV;
164                         pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
165                 } else {
166                         edge = GPIO_INT_HIGH_LEV;
167                         pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
168                 }
169                 port->both_edges |= 1 << (gpio & 31);
170                 break;
171         case IRQ_TYPE_LEVEL_LOW:
172                 edge = GPIO_INT_LOW_LEV;
173                 break;
174         case IRQ_TYPE_LEVEL_HIGH:
175                 edge = GPIO_INT_HIGH_LEV;
176                 break;
177         default:
178                 return -EINVAL;
179         }
180
181         reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
182         bit = gpio & 0xf;
183         val = readl(reg) & ~(0x3 << (bit << 1));
184         writel(val | (edge << (bit << 1)), reg);
185         writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
186
187         return 0;
188 }
189
190 static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
191 {
192         void __iomem *reg = port->base;
193         u32 bit, val;
194         int edge;
195
196         reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
197         bit = gpio & 0xf;
198         val = readl(reg);
199         edge = (val >> (bit << 1)) & 3;
200         val &= ~(0x3 << (bit << 1));
201         if (edge == GPIO_INT_HIGH_LEV) {
202                 edge = GPIO_INT_LOW_LEV;
203                 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
204         } else if (edge == GPIO_INT_LOW_LEV) {
205                 edge = GPIO_INT_HIGH_LEV;
206                 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
207         } else {
208                 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
209                        gpio, edge);
210                 return;
211         }
212         writel(val | (edge << (bit << 1)), reg);
213 }
214
215 /* handle 32 interrupts in one status register */
216 static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
217 {
218         u32 gpio_irq_no_base = port->virtual_irq_start;
219
220         while (irq_stat != 0) {
221                 int irqoffset = fls(irq_stat) - 1;
222
223                 if (port->both_edges & (1 << irqoffset))
224                         mxc_flip_edge(port, irqoffset);
225
226                 generic_handle_irq(gpio_irq_no_base + irqoffset);
227
228                 irq_stat &= ~(1 << irqoffset);
229         }
230 }
231
232 /* MX1 and MX3 has one interrupt *per* gpio port */
233 static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
234 {
235         u32 irq_stat;
236         struct mxc_gpio_port *port = irq_get_handler_data(irq);
237
238         irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
239
240         mxc_gpio_irq_handler(port, irq_stat);
241 }
242
243 /* MX2 has one interrupt *for all* gpio ports */
244 static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
245 {
246         u32 irq_msk, irq_stat;
247         struct mxc_gpio_port *port;
248
249         /* walk through all interrupt status registers */
250         list_for_each_entry(port, &mxc_gpio_ports, node) {
251                 irq_msk = readl(port->base + GPIO_IMR);
252                 if (!irq_msk)
253                         continue;
254
255                 irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
256                 if (irq_stat)
257                         mxc_gpio_irq_handler(port, irq_stat);
258         }
259 }
260
261 /*
262  * Set interrupt number "irq" in the GPIO as a wake-up source.
263  * While system is running, all registered GPIO interrupts need to have
264  * wake-up enabled. When system is suspended, only selected GPIO interrupts
265  * need to have wake-up enabled.
266  * @param  irq          interrupt source number
267  * @param  enable       enable as wake-up if equal to non-zero
268  * @return       This function returns 0 on success.
269  */
270 static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
271 {
272         u32 gpio = irq_to_gpio(d->irq);
273         u32 gpio_idx = gpio & 0x1F;
274         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
275         struct mxc_gpio_port *port = gc->private;
276
277         if (enable) {
278                 if (port->irq_high && (gpio_idx >= 16))
279                         enable_irq_wake(port->irq_high);
280                 else
281                         enable_irq_wake(port->irq);
282         } else {
283                 if (port->irq_high && (gpio_idx >= 16))
284                         disable_irq_wake(port->irq_high);
285                 else
286                         disable_irq_wake(port->irq);
287         }
288
289         return 0;
290 }
291
292 static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
293 {
294         struct irq_chip_generic *gc;
295         struct irq_chip_type *ct;
296
297         gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
298                                     port->base, handle_level_irq);
299         gc->private = port;
300
301         ct = gc->chip_types;
302         ct->chip.irq_ack = irq_gc_ack_set_bit;
303         ct->chip.irq_mask = irq_gc_mask_clr_bit;
304         ct->chip.irq_unmask = irq_gc_mask_set_bit;
305         ct->chip.irq_set_type = gpio_set_irq_type;
306         ct->chip.irq_set_wake = gpio_set_wake_irq;
307         ct->regs.ack = GPIO_ISR;
308         ct->regs.mask = GPIO_IMR;
309
310         irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
311                                IRQ_NOREQUEST, 0);
312 }
313
314 static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
315 {
316         const struct of_device_id *of_id =
317                         of_match_device(mxc_gpio_dt_ids, &pdev->dev);
318         enum mxc_gpio_hwtype hwtype;
319
320         if (of_id)
321                 pdev->id_entry = of_id->data;
322         hwtype = pdev->id_entry->driver_data;
323
324         if (mxc_gpio_hwtype) {
325                 /*
326                  * The driver works with a reasonable presupposition,
327                  * that is all gpio ports must be the same type when
328                  * running on one soc.
329                  */
330                 BUG_ON(mxc_gpio_hwtype != hwtype);
331                 return;
332         }
333
334         if (hwtype == IMX31_GPIO)
335                 mxc_gpio_hwdata = &imx31_gpio_hwdata;
336         else
337                 mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
338
339         mxc_gpio_hwtype = hwtype;
340 }
341
342 static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
343 {
344         struct bgpio_chip *bgc = to_bgpio_chip(gc);
345         struct mxc_gpio_port *port =
346                 container_of(bgc, struct mxc_gpio_port, bgc);
347
348         return port->virtual_irq_start + offset;
349 }
350
351 static int __devinit mxc_gpio_probe(struct platform_device *pdev)
352 {
353         struct device_node *np = pdev->dev.of_node;
354         struct mxc_gpio_port *port;
355         struct resource *iores;
356         int err;
357
358         mxc_gpio_get_hw(pdev);
359
360         port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
361         if (!port)
362                 return -ENOMEM;
363
364         iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365         if (!iores) {
366                 err = -ENODEV;
367                 goto out_kfree;
368         }
369
370         if (!request_mem_region(iores->start, resource_size(iores),
371                                 pdev->name)) {
372                 err = -EBUSY;
373                 goto out_kfree;
374         }
375
376         port->base = ioremap(iores->start, resource_size(iores));
377         if (!port->base) {
378                 err = -ENOMEM;
379                 goto out_release_mem;
380         }
381
382         port->irq_high = platform_get_irq(pdev, 1);
383         port->irq = platform_get_irq(pdev, 0);
384         if (port->irq < 0) {
385                 err = -EINVAL;
386                 goto out_iounmap;
387         }
388
389         /* disable the interrupt and clear the status */
390         writel(0, port->base + GPIO_IMR);
391         writel(~0, port->base + GPIO_ISR);
392
393         if (mxc_gpio_hwtype == IMX21_GPIO) {
394                 /* setup one handler for all GPIO interrupts */
395                 if (pdev->id == 0)
396                         irq_set_chained_handler(port->irq,
397                                                 mx2_gpio_irq_handler);
398         } else {
399                 /* setup one handler for each entry */
400                 irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
401                 irq_set_handler_data(port->irq, port);
402                 if (port->irq_high > 0) {
403                         /* setup handler for GPIO 16 to 31 */
404                         irq_set_chained_handler(port->irq_high,
405                                                 mx3_gpio_irq_handler);
406                         irq_set_handler_data(port->irq_high, port);
407                 }
408         }
409
410         err = bgpio_init(&port->bgc, &pdev->dev, 4,
411                          port->base + GPIO_PSR,
412                          port->base + GPIO_DR, NULL,
413                          port->base + GPIO_GDIR, NULL, false);
414         if (err)
415                 goto out_iounmap;
416
417         port->bgc.gc.to_irq = mxc_gpio_to_irq;
418         port->bgc.gc.base = pdev->id * 32;
419         port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
420         port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
421
422         err = gpiochip_add(&port->bgc.gc);
423         if (err)
424                 goto out_bgpio_remove;
425
426         /*
427          * In dt case, we use gpio number range dynamically
428          * allocated by gpio core.
429          */
430         port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
431                                                              pdev->id * 32);
432
433         /* gpio-mxc can be a generic irq chip */
434         mxc_gpio_init_gc(port);
435
436         list_add_tail(&port->node, &mxc_gpio_ports);
437
438         return 0;
439
440 out_bgpio_remove:
441         bgpio_remove(&port->bgc);
442 out_iounmap:
443         iounmap(port->base);
444 out_release_mem:
445         release_mem_region(iores->start, resource_size(iores));
446 out_kfree:
447         kfree(port);
448         dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
449         return err;
450 }
451
452 static struct platform_driver mxc_gpio_driver = {
453         .driver         = {
454                 .name   = "gpio-mxc",
455                 .owner  = THIS_MODULE,
456                 .of_match_table = mxc_gpio_dt_ids,
457         },
458         .probe          = mxc_gpio_probe,
459         .id_table       = mxc_gpio_devtype,
460 };
461
462 static int __init gpio_mxc_init(void)
463 {
464         return platform_driver_register(&mxc_gpio_driver);
465 }
466 postcore_initcall(gpio_mxc_init);
467
468 MODULE_AUTHOR("Freescale Semiconductor, "
469               "Daniel Mack <danielncaiaq.de>, "
470               "Juergen Beisert <kernel@pengutronix.de>");
471 MODULE_DESCRIPTION("Freescale MXC GPIO");
472 MODULE_LICENSE("GPL");