blob: 9d2dba7199c9d91b1cc493cf27dfc40233b32651 [file] [log] [blame]
Shawn Guofba311f2010-12-18 21:39:31 +08001/*
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,
20 * MA 02110-1301, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/irq.h>
27#include <linux/gpio.h>
Shawn Guo8d7cf832011-06-06 09:37:58 -060028#include <linux/platform_device.h>
29#include <linux/slab.h>
Shawn Guo06f88a82011-06-06 22:31:29 +080030#include <linux/basic_mmio_gpio.h>
Shawn Guo8d7cf832011-06-06 09:37:58 -060031#include <mach/mxs.h>
Shawn Guofba311f2010-12-18 21:39:31 +080032
Shawn Guo8d7cf832011-06-06 09:37:58 -060033#define MXS_SET 0x4
34#define MXS_CLR 0x8
Shawn Guofba311f2010-12-18 21:39:31 +080035
36#define PINCTRL_DOUT(n) ((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10)
37#define PINCTRL_DIN(n) ((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10)
38#define PINCTRL_DOE(n) ((cpu_is_mx23() ? 0x0700 : 0x0b00) + (n) * 0x10)
39#define PINCTRL_PIN2IRQ(n) ((cpu_is_mx23() ? 0x0800 : 0x1000) + (n) * 0x10)
40#define PINCTRL_IRQEN(n) ((cpu_is_mx23() ? 0x0900 : 0x1100) + (n) * 0x10)
41#define PINCTRL_IRQLEV(n) ((cpu_is_mx23() ? 0x0a00 : 0x1200) + (n) * 0x10)
42#define PINCTRL_IRQPOL(n) ((cpu_is_mx23() ? 0x0b00 : 0x1300) + (n) * 0x10)
43#define PINCTRL_IRQSTAT(n) ((cpu_is_mx23() ? 0x0c00 : 0x1400) + (n) * 0x10)
44
45#define GPIO_INT_FALL_EDGE 0x0
46#define GPIO_INT_LOW_LEV 0x1
47#define GPIO_INT_RISE_EDGE 0x2
48#define GPIO_INT_HIGH_LEV 0x3
49#define GPIO_INT_LEV_MASK (1 << 0)
50#define GPIO_INT_POL_MASK (1 << 1)
51
Grant Likely7b2fa572011-06-06 09:37:58 -060052struct mxs_gpio_port {
53 void __iomem *base;
54 int id;
55 int irq;
56 int irq_high;
57 int virtual_irq_start;
Shawn Guo06f88a82011-06-06 22:31:29 +080058 struct bgpio_chip bgc;
Grant Likely7b2fa572011-06-06 09:37:58 -060059};
60
Shawn Guofba311f2010-12-18 21:39:31 +080061/* Note: This driver assumes 32 GPIOs are handled in one register */
62
63static void clear_gpio_irqstatus(struct mxs_gpio_port *port, u32 index)
64{
Shawn Guo8d7cf832011-06-06 09:37:58 -060065 writel(1 << index, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
Shawn Guofba311f2010-12-18 21:39:31 +080066}
67
68static void set_gpio_irqenable(struct mxs_gpio_port *port, u32 index,
69 int enable)
70{
71 if (enable) {
Shawn Guo8d7cf832011-06-06 09:37:58 -060072 writel(1 << index,
73 port->base + PINCTRL_IRQEN(port->id) + MXS_SET);
74 writel(1 << index,
75 port->base + PINCTRL_PIN2IRQ(port->id) + MXS_SET);
Shawn Guofba311f2010-12-18 21:39:31 +080076 } else {
Shawn Guo8d7cf832011-06-06 09:37:58 -060077 writel(1 << index,
78 port->base + PINCTRL_IRQEN(port->id) + MXS_CLR);
Shawn Guofba311f2010-12-18 21:39:31 +080079 }
80}
81
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010082static void mxs_gpio_ack_irq(struct irq_data *d)
Shawn Guofba311f2010-12-18 21:39:31 +080083{
Shawn Guo8d7cf832011-06-06 09:37:58 -060084 struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010085 u32 gpio = irq_to_gpio(d->irq);
Shawn Guo8d7cf832011-06-06 09:37:58 -060086 clear_gpio_irqstatus(port, gpio & 0x1f);
Shawn Guofba311f2010-12-18 21:39:31 +080087}
88
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010089static void mxs_gpio_mask_irq(struct irq_data *d)
Shawn Guofba311f2010-12-18 21:39:31 +080090{
Shawn Guo8d7cf832011-06-06 09:37:58 -060091 struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010092 u32 gpio = irq_to_gpio(d->irq);
Shawn Guo8d7cf832011-06-06 09:37:58 -060093 set_gpio_irqenable(port, gpio & 0x1f, 0);
Shawn Guofba311f2010-12-18 21:39:31 +080094}
95
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010096static void mxs_gpio_unmask_irq(struct irq_data *d)
Shawn Guofba311f2010-12-18 21:39:31 +080097{
Shawn Guo8d7cf832011-06-06 09:37:58 -060098 struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +010099 u32 gpio = irq_to_gpio(d->irq);
Shawn Guo8d7cf832011-06-06 09:37:58 -0600100 set_gpio_irqenable(port, gpio & 0x1f, 1);
Shawn Guofba311f2010-12-18 21:39:31 +0800101}
102
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +0100103static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
Shawn Guofba311f2010-12-18 21:39:31 +0800104{
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +0100105 u32 gpio = irq_to_gpio(d->irq);
Shawn Guofba311f2010-12-18 21:39:31 +0800106 u32 pin_mask = 1 << (gpio & 31);
Shawn Guo8d7cf832011-06-06 09:37:58 -0600107 struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
Shawn Guofba311f2010-12-18 21:39:31 +0800108 void __iomem *pin_addr;
109 int edge;
110
111 switch (type) {
112 case IRQ_TYPE_EDGE_RISING:
113 edge = GPIO_INT_RISE_EDGE;
114 break;
115 case IRQ_TYPE_EDGE_FALLING:
116 edge = GPIO_INT_FALL_EDGE;
117 break;
118 case IRQ_TYPE_LEVEL_LOW:
119 edge = GPIO_INT_LOW_LEV;
120 break;
121 case IRQ_TYPE_LEVEL_HIGH:
122 edge = GPIO_INT_HIGH_LEV;
123 break;
124 default:
125 return -EINVAL;
126 }
127
128 /* set level or edge */
129 pin_addr = port->base + PINCTRL_IRQLEV(port->id);
130 if (edge & GPIO_INT_LEV_MASK)
Shawn Guo8d7cf832011-06-06 09:37:58 -0600131 writel(pin_mask, pin_addr + MXS_SET);
Shawn Guofba311f2010-12-18 21:39:31 +0800132 else
Shawn Guo8d7cf832011-06-06 09:37:58 -0600133 writel(pin_mask, pin_addr + MXS_CLR);
Shawn Guofba311f2010-12-18 21:39:31 +0800134
135 /* set polarity */
136 pin_addr = port->base + PINCTRL_IRQPOL(port->id);
137 if (edge & GPIO_INT_POL_MASK)
Shawn Guo8d7cf832011-06-06 09:37:58 -0600138 writel(pin_mask, pin_addr + MXS_SET);
Shawn Guofba311f2010-12-18 21:39:31 +0800139 else
Shawn Guo8d7cf832011-06-06 09:37:58 -0600140 writel(pin_mask, pin_addr + MXS_CLR);
Shawn Guofba311f2010-12-18 21:39:31 +0800141
142 clear_gpio_irqstatus(port, gpio & 0x1f);
143
144 return 0;
145}
146
147/* MXS has one interrupt *per* gpio port */
148static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc)
149{
150 u32 irq_stat;
Shawn Guo8d7cf832011-06-06 09:37:58 -0600151 struct mxs_gpio_port *port = irq_get_handler_data(irq);
Shawn Guofba311f2010-12-18 21:39:31 +0800152 u32 gpio_irq_no_base = port->virtual_irq_start;
153
Uwe Kleine-König1f6b5dd2011-01-25 16:54:22 +0100154 desc->irq_data.chip->irq_ack(&desc->irq_data);
155
Shawn Guo8d7cf832011-06-06 09:37:58 -0600156 irq_stat = readl(port->base + PINCTRL_IRQSTAT(port->id)) &
157 readl(port->base + PINCTRL_IRQEN(port->id));
Shawn Guofba311f2010-12-18 21:39:31 +0800158
159 while (irq_stat != 0) {
160 int irqoffset = fls(irq_stat) - 1;
161 generic_handle_irq(gpio_irq_no_base + irqoffset);
162 irq_stat &= ~(1 << irqoffset);
163 }
164}
165
166/*
167 * Set interrupt number "irq" in the GPIO as a wake-up source.
168 * While system is running, all registered GPIO interrupts need to have
169 * wake-up enabled. When system is suspended, only selected GPIO interrupts
170 * need to have wake-up enabled.
171 * @param irq interrupt source number
172 * @param enable enable as wake-up if equal to non-zero
173 * @return This function returns 0 on success.
174 */
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +0100175static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
Shawn Guofba311f2010-12-18 21:39:31 +0800176{
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +0100177 u32 gpio = irq_to_gpio(d->irq);
Shawn Guofba311f2010-12-18 21:39:31 +0800178 u32 gpio_idx = gpio & 0x1f;
Shawn Guo8d7cf832011-06-06 09:37:58 -0600179 struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
Shawn Guofba311f2010-12-18 21:39:31 +0800180
181 if (enable) {
182 if (port->irq_high && (gpio_idx >= 16))
183 enable_irq_wake(port->irq_high);
184 else
185 enable_irq_wake(port->irq);
186 } else {
187 if (port->irq_high && (gpio_idx >= 16))
188 disable_irq_wake(port->irq_high);
189 else
190 disable_irq_wake(port->irq);
191 }
192
193 return 0;
194}
195
196static struct irq_chip gpio_irq_chip = {
Wolfram Sang761b6d12011-03-01 10:21:51 +0100197 .name = "mxs gpio",
Uwe Kleine-Königbf0c11182011-02-18 21:31:41 +0100198 .irq_ack = mxs_gpio_ack_irq,
199 .irq_mask = mxs_gpio_mask_irq,
200 .irq_unmask = mxs_gpio_unmask_irq,
201 .irq_set_type = mxs_gpio_set_irq_type,
202 .irq_set_wake = mxs_gpio_set_wake_irq,
Shawn Guofba311f2010-12-18 21:39:31 +0800203};
204
Shawn Guo06f88a82011-06-06 22:31:29 +0800205static int mxs_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
Shawn Guofba311f2010-12-18 21:39:31 +0800206{
Shawn Guo06f88a82011-06-06 22:31:29 +0800207 struct bgpio_chip *bgc = to_bgpio_chip(gc);
Shawn Guofba311f2010-12-18 21:39:31 +0800208 struct mxs_gpio_port *port =
Shawn Guo06f88a82011-06-06 22:31:29 +0800209 container_of(bgc, struct mxs_gpio_port, bgc);
Shawn Guofba311f2010-12-18 21:39:31 +0800210
211 return port->virtual_irq_start + offset;
212}
213
Shawn Guo8d7cf832011-06-06 09:37:58 -0600214static int __devinit mxs_gpio_probe(struct platform_device *pdev)
Shawn Guofba311f2010-12-18 21:39:31 +0800215{
Shawn Guo8d7cf832011-06-06 09:37:58 -0600216 static void __iomem *base;
217 struct mxs_gpio_port *port;
218 struct resource *iores = NULL;
219 int err, i;
Shawn Guofba311f2010-12-18 21:39:31 +0800220
Shawn Guo8d7cf832011-06-06 09:37:58 -0600221 port = kzalloc(sizeof(struct mxs_gpio_port), GFP_KERNEL);
222 if (!port)
223 return -ENOMEM;
Shawn Guofba311f2010-12-18 21:39:31 +0800224
Shawn Guo8d7cf832011-06-06 09:37:58 -0600225 port->id = pdev->id;
226 port->virtual_irq_start = MXS_GPIO_IRQ_START + port->id * 32;
Shawn Guofba311f2010-12-18 21:39:31 +0800227
Shawn Guo8d7cf832011-06-06 09:37:58 -0600228 /*
229 * map memory region only once, as all the gpio ports
230 * share the same one
231 */
232 if (!base) {
233 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
234 if (!iores) {
235 err = -ENODEV;
236 goto out_kfree;
Shawn Guofba311f2010-12-18 21:39:31 +0800237 }
238
Shawn Guo8d7cf832011-06-06 09:37:58 -0600239 if (!request_mem_region(iores->start, resource_size(iores),
240 pdev->name)) {
241 err = -EBUSY;
242 goto out_kfree;
243 }
Shawn Guofba311f2010-12-18 21:39:31 +0800244
Shawn Guo8d7cf832011-06-06 09:37:58 -0600245 base = ioremap(iores->start, resource_size(iores));
246 if (!base) {
247 err = -ENOMEM;
248 goto out_release_mem;
249 }
Shawn Guofba311f2010-12-18 21:39:31 +0800250 }
Shawn Guo8d7cf832011-06-06 09:37:58 -0600251 port->base = base;
252
253 port->irq = platform_get_irq(pdev, 0);
254 if (port->irq < 0) {
255 err = -EINVAL;
256 goto out_iounmap;
257 }
258
259 /* disable the interrupt and clear the status */
260 writel(0, port->base + PINCTRL_PIN2IRQ(port->id));
261 writel(0, port->base + PINCTRL_IRQEN(port->id));
262
263 /* clear address has to be used to clear IRQSTAT bits */
264 writel(~0U, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
265
266 for (i = port->virtual_irq_start;
267 i < port->virtual_irq_start + 32; i++) {
268 irq_set_chip_and_handler(i, &gpio_irq_chip,
269 handle_level_irq);
270 set_irq_flags(i, IRQF_VALID);
271 irq_set_chip_data(i, port);
272 }
273
274 /* setup one handler for each entry */
275 irq_set_chained_handler(port->irq, mxs_gpio_irq_handler);
276 irq_set_handler_data(port->irq, port);
277
Shawn Guo06f88a82011-06-06 22:31:29 +0800278 err = bgpio_init(&port->bgc, &pdev->dev, 4,
279 port->base + PINCTRL_DIN(port->id),
280 port->base + PINCTRL_DOUT(port->id), NULL,
281 port->base + PINCTRL_DOE(port->id), NULL, false);
Shawn Guo8d7cf832011-06-06 09:37:58 -0600282 if (err)
283 goto out_iounmap;
Shawn Guofba311f2010-12-18 21:39:31 +0800284
Shawn Guo06f88a82011-06-06 22:31:29 +0800285 port->bgc.gc.to_irq = mxs_gpio_to_irq;
286 port->bgc.gc.base = port->id * 32;
287
288 err = gpiochip_add(&port->bgc.gc);
289 if (err)
290 goto out_bgpio_remove;
291
Shawn Guofba311f2010-12-18 21:39:31 +0800292 return 0;
Shawn Guo8d7cf832011-06-06 09:37:58 -0600293
Shawn Guo06f88a82011-06-06 22:31:29 +0800294out_bgpio_remove:
295 bgpio_remove(&port->bgc);
Shawn Guo8d7cf832011-06-06 09:37:58 -0600296out_iounmap:
297 if (iores)
298 iounmap(port->base);
299out_release_mem:
300 if (iores)
301 release_mem_region(iores->start, resource_size(iores));
302out_kfree:
303 kfree(port);
304 dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
305 return err;
Shawn Guofba311f2010-12-18 21:39:31 +0800306}
307
Shawn Guo8d7cf832011-06-06 09:37:58 -0600308static struct platform_driver mxs_gpio_driver = {
309 .driver = {
310 .name = "gpio-mxs",
311 .owner = THIS_MODULE,
312 },
313 .probe = mxs_gpio_probe,
Shawn Guofba311f2010-12-18 21:39:31 +0800314};
Sascha Haueref196602011-01-24 12:57:46 +0100315
Shawn Guo8d7cf832011-06-06 09:37:58 -0600316static int __init mxs_gpio_init(void)
Sascha Haueref196602011-01-24 12:57:46 +0100317{
Shawn Guo8d7cf832011-06-06 09:37:58 -0600318 return platform_driver_register(&mxs_gpio_driver);
Sascha Haueref196602011-01-24 12:57:46 +0100319}
Shawn Guo8d7cf832011-06-06 09:37:58 -0600320postcore_initcall(mxs_gpio_init);
Shawn Guofba311f2010-12-18 21:39:31 +0800321
Shawn Guo8d7cf832011-06-06 09:37:58 -0600322MODULE_AUTHOR("Freescale Semiconductor, "
323 "Daniel Mack <danielncaiaq.de>, "
324 "Juergen Beisert <kernel@pengutronix.de>");
325MODULE_DESCRIPTION("Freescale MXS GPIO");
326MODULE_LICENSE("GPL");