gpio-mxs.c 8.4 KB
Newer Older
S
Shawn Guo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
 *
 * Based on code from Freescale,
 * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/gpio.h>
28 29
#include <linux/platform_device.h>
#include <linux/slab.h>
30
#include <linux/basic_mmio_gpio.h>
31
#include <mach/mxs.h>
S
Shawn Guo 已提交
32

33 34
#define MXS_SET		0x4
#define MXS_CLR		0x8
S
Shawn Guo 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#define PINCTRL_DOUT(n)		((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10)
#define PINCTRL_DIN(n)		((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10)
#define PINCTRL_DOE(n)		((cpu_is_mx23() ? 0x0700 : 0x0b00) + (n) * 0x10)
#define PINCTRL_PIN2IRQ(n)	((cpu_is_mx23() ? 0x0800 : 0x1000) + (n) * 0x10)
#define PINCTRL_IRQEN(n)	((cpu_is_mx23() ? 0x0900 : 0x1100) + (n) * 0x10)
#define PINCTRL_IRQLEV(n)	((cpu_is_mx23() ? 0x0a00 : 0x1200) + (n) * 0x10)
#define PINCTRL_IRQPOL(n)	((cpu_is_mx23() ? 0x0b00 : 0x1300) + (n) * 0x10)
#define PINCTRL_IRQSTAT(n)	((cpu_is_mx23() ? 0x0c00 : 0x1400) + (n) * 0x10)

#define GPIO_INT_FALL_EDGE	0x0
#define GPIO_INT_LOW_LEV	0x1
#define GPIO_INT_RISE_EDGE	0x2
#define GPIO_INT_HIGH_LEV	0x3
#define GPIO_INT_LEV_MASK	(1 << 0)
#define GPIO_INT_POL_MASK	(1 << 1)

52 53 54 55 56
struct mxs_gpio_port {
	void __iomem *base;
	int id;
	int irq;
	int virtual_irq_start;
57
	struct bgpio_chip bgc;
58 59
};

S
Shawn Guo 已提交
60 61 62 63
/* Note: This driver assumes 32 GPIOs are handled in one register */

static void clear_gpio_irqstatus(struct mxs_gpio_port *port, u32 index)
{
64
	writel(1 << index, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
S
Shawn Guo 已提交
65 66 67 68 69 70
}

static void set_gpio_irqenable(struct mxs_gpio_port *port, u32 index,
				int enable)
{
	if (enable) {
71 72 73 74
		writel(1 << index,
			port->base + PINCTRL_IRQEN(port->id) + MXS_SET);
		writel(1 << index,
			port->base + PINCTRL_PIN2IRQ(port->id) + MXS_SET);
S
Shawn Guo 已提交
75
	} else {
76 77
		writel(1 << index,
			port->base + PINCTRL_IRQEN(port->id) + MXS_CLR);
S
Shawn Guo 已提交
78 79 80
	}
}

81
static void mxs_gpio_ack_irq(struct irq_data *d)
S
Shawn Guo 已提交
82
{
83
	struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
84
	u32 gpio = irq_to_gpio(d->irq);
85
	clear_gpio_irqstatus(port, gpio & 0x1f);
S
Shawn Guo 已提交
86 87
}

88
static void mxs_gpio_mask_irq(struct irq_data *d)
S
Shawn Guo 已提交
89
{
90
	struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
91
	u32 gpio = irq_to_gpio(d->irq);
92
	set_gpio_irqenable(port, gpio & 0x1f, 0);
S
Shawn Guo 已提交
93 94
}

95
static void mxs_gpio_unmask_irq(struct irq_data *d)
S
Shawn Guo 已提交
96
{
97
	struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
98
	u32 gpio = irq_to_gpio(d->irq);
99
	set_gpio_irqenable(port, gpio & 0x1f, 1);
S
Shawn Guo 已提交
100 101
}

102
static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
S
Shawn Guo 已提交
103
{
104
	u32 gpio = irq_to_gpio(d->irq);
S
Shawn Guo 已提交
105
	u32 pin_mask = 1 << (gpio & 31);
106
	struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
S
Shawn Guo 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	void __iomem *pin_addr;
	int edge;

	switch (type) {
	case IRQ_TYPE_EDGE_RISING:
		edge = GPIO_INT_RISE_EDGE;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		edge = GPIO_INT_FALL_EDGE;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		edge = GPIO_INT_LOW_LEV;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		edge = GPIO_INT_HIGH_LEV;
		break;
	default:
		return -EINVAL;
	}

	/* set level or edge */
	pin_addr = port->base + PINCTRL_IRQLEV(port->id);
	if (edge & GPIO_INT_LEV_MASK)
130
		writel(pin_mask, pin_addr + MXS_SET);
S
Shawn Guo 已提交
131
	else
132
		writel(pin_mask, pin_addr + MXS_CLR);
S
Shawn Guo 已提交
133 134 135 136

	/* set polarity */
	pin_addr = port->base + PINCTRL_IRQPOL(port->id);
	if (edge & GPIO_INT_POL_MASK)
137
		writel(pin_mask, pin_addr + MXS_SET);
S
Shawn Guo 已提交
138
	else
139
		writel(pin_mask, pin_addr + MXS_CLR);
S
Shawn Guo 已提交
140 141 142 143 144 145 146 147 148 149

	clear_gpio_irqstatus(port, gpio & 0x1f);

	return 0;
}

/* MXS has one interrupt *per* gpio port */
static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc)
{
	u32 irq_stat;
150
	struct mxs_gpio_port *port = irq_get_handler_data(irq);
S
Shawn Guo 已提交
151 152
	u32 gpio_irq_no_base = port->virtual_irq_start;

153 154
	desc->irq_data.chip->irq_ack(&desc->irq_data);

155 156
	irq_stat = readl(port->base + PINCTRL_IRQSTAT(port->id)) &
			readl(port->base + PINCTRL_IRQEN(port->id));
S
Shawn Guo 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	while (irq_stat != 0) {
		int irqoffset = fls(irq_stat) - 1;
		generic_handle_irq(gpio_irq_no_base + irqoffset);
		irq_stat &= ~(1 << irqoffset);
	}
}

/*
 * Set interrupt number "irq" in the GPIO as a wake-up source.
 * While system is running, all registered GPIO interrupts need to have
 * wake-up enabled. When system is suspended, only selected GPIO interrupts
 * need to have wake-up enabled.
 * @param  irq          interrupt source number
 * @param  enable       enable as wake-up if equal to non-zero
 * @return       This function returns 0 on success.
 */
174
static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
S
Shawn Guo 已提交
175
{
176
	struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
S
Shawn Guo 已提交
177

178 179 180 181
	if (enable)
		enable_irq_wake(port->irq);
	else
		disable_irq_wake(port->irq);
S
Shawn Guo 已提交
182 183 184 185 186

	return 0;
}

static struct irq_chip gpio_irq_chip = {
187
	.name = "mxs gpio",
188 189 190 191 192
	.irq_ack = mxs_gpio_ack_irq,
	.irq_mask = mxs_gpio_mask_irq,
	.irq_unmask = mxs_gpio_unmask_irq,
	.irq_set_type = mxs_gpio_set_irq_type,
	.irq_set_wake = mxs_gpio_set_wake_irq,
S
Shawn Guo 已提交
193 194
};

195
static int mxs_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
S
Shawn Guo 已提交
196
{
197
	struct bgpio_chip *bgc = to_bgpio_chip(gc);
S
Shawn Guo 已提交
198
	struct mxs_gpio_port *port =
199
		container_of(bgc, struct mxs_gpio_port, bgc);
S
Shawn Guo 已提交
200 201 202 203

	return port->virtual_irq_start + offset;
}

204
static int __devinit mxs_gpio_probe(struct platform_device *pdev)
S
Shawn Guo 已提交
205
{
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	static void __iomem *base;
	struct mxs_gpio_port *port;
	struct resource *iores = NULL;
	int err, i;

	port = kzalloc(sizeof(struct mxs_gpio_port), GFP_KERNEL);
	if (!port)
		return -ENOMEM;

	port->id = pdev->id;
	port->virtual_irq_start = MXS_GPIO_IRQ_START + port->id * 32;

	/*
	 * map memory region only once, as all the gpio ports
	 * share the same one
	 */
	if (!base) {
		iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
		if (!iores) {
			err = -ENODEV;
			goto out_kfree;
		}
S
Shawn Guo 已提交
228

229 230 231 232 233
		if (!request_mem_region(iores->start, resource_size(iores),
					pdev->name)) {
			err = -EBUSY;
			goto out_kfree;
		}
S
Shawn Guo 已提交
234

235 236 237 238 239 240 241
		base = ioremap(iores->start, resource_size(iores));
		if (!base) {
			err = -ENOMEM;
			goto out_release_mem;
		}
	}
	port->base = base;
S
Shawn Guo 已提交
242

243 244 245 246 247
	port->irq = platform_get_irq(pdev, 0);
	if (port->irq < 0) {
		err = -EINVAL;
		goto out_iounmap;
	}
S
Shawn Guo 已提交
248

249 250 251
	/* disable the interrupt and clear the status */
	writel(0, port->base + PINCTRL_PIN2IRQ(port->id));
	writel(0, port->base + PINCTRL_IRQEN(port->id));
S
Shawn Guo 已提交
252

253 254
	/* clear address has to be used to clear IRQSTAT bits */
	writel(~0U, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
S
Shawn Guo 已提交
255

256 257 258 259 260 261
	for (i = port->virtual_irq_start;
		i < port->virtual_irq_start + 32; i++) {
		irq_set_chip_and_handler(i, &gpio_irq_chip,
					 handle_level_irq);
		set_irq_flags(i, IRQF_VALID);
		irq_set_chip_data(i, port);
S
Shawn Guo 已提交
262 263
	}

264 265 266
	/* setup one handler for each entry */
	irq_set_chained_handler(port->irq, mxs_gpio_irq_handler);
	irq_set_handler_data(port->irq, port);
S
Shawn Guo 已提交
267

268 269 270 271
	err = bgpio_init(&port->bgc, &pdev->dev, 4,
			 port->base + PINCTRL_DIN(port->id),
			 port->base + PINCTRL_DOUT(port->id), NULL,
			 port->base + PINCTRL_DOE(port->id), NULL, false);
272 273
	if (err)
		goto out_iounmap;
S
Shawn Guo 已提交
274

275 276 277 278 279 280 281
	port->bgc.gc.to_irq = mxs_gpio_to_irq;
	port->bgc.gc.base = port->id * 32;

	err = gpiochip_add(&port->bgc.gc);
	if (err)
		goto out_bgpio_remove;

282
	return 0;
283

284 285
out_bgpio_remove:
	bgpio_remove(&port->bgc);
286 287 288 289 290 291 292 293 294 295
out_iounmap:
	if (iores)
		iounmap(port->base);
out_release_mem:
	if (iores)
		release_mem_region(iores->start, resource_size(iores));
out_kfree:
	kfree(port);
	dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
	return err;
296
}
297 298 299 300 301 302 303

static struct platform_driver mxs_gpio_driver = {
	.driver		= {
		.name	= "gpio-mxs",
		.owner	= THIS_MODULE,
	},
	.probe		= mxs_gpio_probe,
S
Shawn Guo 已提交
304
};
305

306
static int __init mxs_gpio_init(void)
307
{
308
	return platform_driver_register(&mxs_gpio_driver);
309
}
310 311 312 313 314 315 316
postcore_initcall(mxs_gpio_init);

MODULE_AUTHOR("Freescale Semiconductor, "
	      "Daniel Mack <danielncaiaq.de>, "
	      "Juergen Beisert <kernel@pengutronix.de>");
MODULE_DESCRIPTION("Freescale MXS GPIO");
MODULE_LICENSE("GPL");