gpio-mxs.c 9.78 KB
Newer Older
Shawn Guo's avatar
Shawn Guo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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.
 */

23
#include <linux/err.h>
Shawn Guo's avatar
Shawn Guo committed
24 25 26 27
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
28
#include <linux/irqdomain.h>
Shawn Guo's avatar
Shawn Guo committed
29 30 31
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
32 33
#include <linux/platform_device.h>
#include <linux/slab.h>
34 35 36
#include <linux/gpio/driver.h>
/* FIXME: for gpio_get_value(), replace this by direct register read */
#include <linux/gpio.h>
37
#include <linux/module.h>
Shawn Guo's avatar
Shawn Guo committed
38

39 40
#define MXS_SET		0x4
#define MXS_CLR		0x8
Shawn Guo's avatar
Shawn Guo committed
41

42 43 44 45 46 47 48 49
#define PINCTRL_DOUT(p)		((is_imx23_gpio(p) ? 0x0500 : 0x0700) + (p->id) * 0x10)
#define PINCTRL_DIN(p)		((is_imx23_gpio(p) ? 0x0600 : 0x0900) + (p->id) * 0x10)
#define PINCTRL_DOE(p)		((is_imx23_gpio(p) ? 0x0700 : 0x0b00) + (p->id) * 0x10)
#define PINCTRL_PIN2IRQ(p)	((is_imx23_gpio(p) ? 0x0800 : 0x1000) + (p->id) * 0x10)
#define PINCTRL_IRQEN(p)	((is_imx23_gpio(p) ? 0x0900 : 0x1100) + (p->id) * 0x10)
#define PINCTRL_IRQLEV(p)	((is_imx23_gpio(p) ? 0x0a00 : 0x1200) + (p->id) * 0x10)
#define PINCTRL_IRQPOL(p)	((is_imx23_gpio(p) ? 0x0b00 : 0x1300) + (p->id) * 0x10)
#define PINCTRL_IRQSTAT(p)	((is_imx23_gpio(p) ? 0x0c00 : 0x1400) + (p->id) * 0x10)
Shawn Guo's avatar
Shawn Guo committed
50 51 52 53 54 55 56 57

#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)

58 59 60 61 62
enum mxs_gpio_id {
	IMX23_GPIO,
	IMX28_GPIO,
};

63 64 65 66
struct mxs_gpio_port {
	void __iomem *base;
	int id;
	int irq;
67
	struct irq_domain *domain;
68
	struct gpio_chip gc;
69
	enum mxs_gpio_id devid;
70
	u32 both_edges;
71 72
};

73 74 75 76 77 78 79 80 81 82
static inline int is_imx23_gpio(struct mxs_gpio_port *port)
{
	return port->devid == IMX23_GPIO;
}

static inline int is_imx28_gpio(struct mxs_gpio_port *port)
{
	return port->devid == IMX28_GPIO;
}

Shawn Guo's avatar
Shawn Guo committed
83 84
/* Note: This driver assumes 32 GPIOs are handled in one register */

85
static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
Shawn Guo's avatar
Shawn Guo committed
86
{
87
	u32 val;
88
	u32 pin_mask = 1 << d->hwirq;
89 90
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mxs_gpio_port *port = gc->private;
Shawn Guo's avatar
Shawn Guo committed
91 92 93
	void __iomem *pin_addr;
	int edge;

94
	port->both_edges &= ~pin_mask;
Shawn Guo's avatar
Shawn Guo committed
95
	switch (type) {
96
	case IRQ_TYPE_EDGE_BOTH:
97
		val = gpio_get_value(port->gc.base + d->hwirq);
98 99 100 101 102 103
		if (val)
			edge = GPIO_INT_FALL_EDGE;
		else
			edge = GPIO_INT_RISE_EDGE;
		port->both_edges |= pin_mask;
		break;
Shawn Guo's avatar
Shawn Guo committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	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 */
121
	pin_addr = port->base + PINCTRL_IRQLEV(port);
Shawn Guo's avatar
Shawn Guo committed
122
	if (edge & GPIO_INT_LEV_MASK)
123
		writel(pin_mask, pin_addr + MXS_SET);
Shawn Guo's avatar
Shawn Guo committed
124
	else
125
		writel(pin_mask, pin_addr + MXS_CLR);
Shawn Guo's avatar
Shawn Guo committed
126 127

	/* set polarity */
128
	pin_addr = port->base + PINCTRL_IRQPOL(port);
Shawn Guo's avatar
Shawn Guo committed
129
	if (edge & GPIO_INT_POL_MASK)
130
		writel(pin_mask, pin_addr + MXS_SET);
Shawn Guo's avatar
Shawn Guo committed
131
	else
132
		writel(pin_mask, pin_addr + MXS_CLR);
Shawn Guo's avatar
Shawn Guo committed
133

134
	writel(pin_mask,
135
	       port->base + PINCTRL_IRQSTAT(port) + MXS_CLR);
Shawn Guo's avatar
Shawn Guo committed
136 137 138 139

	return 0;
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static void mxs_flip_edge(struct mxs_gpio_port *port, u32 gpio)
{
	u32 bit, val, edge;
	void __iomem *pin_addr;

	bit = 1 << gpio;

	pin_addr = port->base + PINCTRL_IRQPOL(port);
	val = readl(pin_addr);
	edge = val & bit;

	if (edge)
		writel(bit, pin_addr + MXS_CLR);
	else
		writel(bit, pin_addr + MXS_SET);
}

Shawn Guo's avatar
Shawn Guo committed
157
/* MXS has one interrupt *per* gpio port */
158
static void mxs_gpio_irq_handler(struct irq_desc *desc)
Shawn Guo's avatar
Shawn Guo committed
159 160
{
	u32 irq_stat;
161
	struct mxs_gpio_port *port = irq_desc_get_handler_data(desc);
Shawn Guo's avatar
Shawn Guo committed
162

163 164
	desc->irq_data.chip->irq_ack(&desc->irq_data);

165 166
	irq_stat = readl(port->base + PINCTRL_IRQSTAT(port)) &
			readl(port->base + PINCTRL_IRQEN(port));
Shawn Guo's avatar
Shawn Guo committed
167 168 169

	while (irq_stat != 0) {
		int irqoffset = fls(irq_stat) - 1;
170 171 172
		if (port->both_edges & (1 << irqoffset))
			mxs_flip_edge(port, irqoffset);

173
		generic_handle_irq(irq_find_mapping(port->domain, irqoffset));
Shawn Guo's avatar
Shawn Guo committed
174 175 176 177 178 179 180 181 182 183 184 185 186
		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.
 */
187
static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
Shawn Guo's avatar
Shawn Guo committed
188
{
189 190
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mxs_gpio_port *port = gc->private;
Shawn Guo's avatar
Shawn Guo committed
191

192 193 194 195
	if (enable)
		enable_irq_wake(port->irq);
	else
		disable_irq_wake(port->irq);
Shawn Guo's avatar
Shawn Guo committed
196 197 198 199

	return 0;
}

200
static int __init mxs_gpio_init_gc(struct mxs_gpio_port *port, int irq_base)
201 202 203 204
{
	struct irq_chip_generic *gc;
	struct irq_chip_type *ct;

205
	gc = irq_alloc_generic_chip("gpio-mxs", 1, irq_base,
206
				    port->base, handle_level_irq);
207 208 209
	if (!gc)
		return -ENOMEM;

210 211 212
	gc->private = port;

	ct = gc->chip_types;
213
	ct->chip.irq_ack = irq_gc_ack_set_bit;
214 215 216
	ct->chip.irq_mask = irq_gc_mask_clr_bit;
	ct->chip.irq_unmask = irq_gc_mask_set_bit;
	ct->chip.irq_set_type = mxs_gpio_set_irq_type;
217
	ct->chip.irq_set_wake = mxs_gpio_set_wake_irq;
218 219
	ct->regs.ack = PINCTRL_IRQSTAT(port) + MXS_CLR;
	ct->regs.mask = PINCTRL_IRQEN(port);
220

221 222
	irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
			       IRQ_NOREQUEST, 0);
223 224

	return 0;
225
}
Shawn Guo's avatar
Shawn Guo committed
226

227
static int mxs_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
Shawn Guo's avatar
Shawn Guo committed
228
{
229
	struct mxs_gpio_port *port = gpiochip_get_data(gc);
Shawn Guo's avatar
Shawn Guo committed
230

231
	return irq_find_mapping(port->domain, offset);
Shawn Guo's avatar
Shawn Guo committed
232 233
}

234 235
static int mxs_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
{
236
	struct mxs_gpio_port *port = gpiochip_get_data(gc);
237 238 239 240 241 242 243
	u32 mask = 1 << offset;
	u32 dir;

	dir = readl(port->base + PINCTRL_DOE(port));
	return !(dir & mask);
}

244
static const struct platform_device_id mxs_gpio_ids[] = {
245 246 247 248 249 250 251 252 253 254 255 256
	{
		.name = "imx23-gpio",
		.driver_data = IMX23_GPIO,
	}, {
		.name = "imx28-gpio",
		.driver_data = IMX28_GPIO,
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(platform, mxs_gpio_ids);

Shawn Guo's avatar
Shawn Guo committed
257 258 259 260 261 262 263
static const struct of_device_id mxs_gpio_dt_ids[] = {
	{ .compatible = "fsl,imx23-gpio", .data = (void *) IMX23_GPIO, },
	{ .compatible = "fsl,imx28-gpio", .data = (void *) IMX28_GPIO, },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mxs_gpio_dt_ids);

264
static int mxs_gpio_probe(struct platform_device *pdev)
Shawn Guo's avatar
Shawn Guo committed
265
{
Shawn Guo's avatar
Shawn Guo committed
266 267 268 269
	const struct of_device_id *of_id =
			of_match_device(mxs_gpio_dt_ids, &pdev->dev);
	struct device_node *np = pdev->dev.of_node;
	struct device_node *parent;
270 271
	static void __iomem *base;
	struct mxs_gpio_port *port;
272
	int irq_base;
273
	int err;
274

275
	port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
276 277 278
	if (!port)
		return -ENOMEM;

279 280 281 282
	port->id = of_alias_get_id(np, "gpio");
	if (port->id < 0)
		return port->id;
	port->devid = (enum mxs_gpio_id) of_id->data;
283 284 285 286
	port->irq = platform_get_irq(pdev, 0);
	if (port->irq < 0)
		return port->irq;

287 288 289 290 291
	/*
	 * map memory region only once, as all the gpio ports
	 * share the same one
	 */
	if (!base) {
292 293 294 295 296
		parent = of_get_parent(np);
		base = of_iomap(parent, 0);
		of_node_put(parent);
		if (!base)
			return -EADDRNOTAVAIL;
297 298
	}
	port->base = base;
Shawn Guo's avatar
Shawn Guo committed
299

300 301 302 303
	/*
	 * select the pin interrupt functionality but initially
	 * disable the interrupts
	 */
304 305
	writel(~0U, port->base + PINCTRL_PIN2IRQ(port));
	writel(0, port->base + PINCTRL_IRQEN(port));
Shawn Guo's avatar
Shawn Guo committed
306

307
	/* clear address has to be used to clear IRQSTAT bits */
308
	writel(~0U, port->base + PINCTRL_IRQSTAT(port) + MXS_CLR);
Shawn Guo's avatar
Shawn Guo committed
309

310 311 312 313 314 315 316 317 318 319 320
	irq_base = irq_alloc_descs(-1, 0, 32, numa_node_id());
	if (irq_base < 0)
		return irq_base;

	port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
					     &irq_domain_simple_ops, NULL);
	if (!port->domain) {
		err = -ENODEV;
		goto out_irqdesc_free;
	}

321
	/* gpio-mxs can be a generic irq chip */
322 323 324
	err = mxs_gpio_init_gc(port, irq_base);
	if (err < 0)
		goto out_irqdomain_remove;
Shawn Guo's avatar
Shawn Guo committed
325

326
	/* setup one handler for each entry */
327 328
	irq_set_chained_handler_and_data(port->irq, mxs_gpio_irq_handler,
					 port);
Shawn Guo's avatar
Shawn Guo committed
329

330
	err = bgpio_init(&port->gc, &pdev->dev, 4,
331
			 port->base + PINCTRL_DIN(port),
332 333
			 port->base + PINCTRL_DOUT(port) + MXS_SET,
			 port->base + PINCTRL_DOUT(port) + MXS_CLR,
334
			 port->base + PINCTRL_DOE(port), NULL, 0);
335
	if (err)
336
		goto out_irqdomain_remove;
Shawn Guo's avatar
Shawn Guo committed
337

338 339 340
	port->gc.to_irq = mxs_gpio_to_irq;
	port->gc.get_direction = mxs_gpio_get_direction;
	port->gc.base = port->id * 32;
341

342
	err = gpiochip_add_data(&port->gc, port);
343
	if (err)
344
		goto out_irqdomain_remove;
345

346
	return 0;
347

348 349
out_irqdomain_remove:
	irq_domain_remove(port->domain);
350 351 352
out_irqdesc_free:
	irq_free_descs(irq_base, 32);
	return err;
353
}
354 355 356 357

static struct platform_driver mxs_gpio_driver = {
	.driver		= {
		.name	= "gpio-mxs",
Shawn Guo's avatar
Shawn Guo committed
358
		.of_match_table = mxs_gpio_dt_ids,
359 360
	},
	.probe		= mxs_gpio_probe,
361
	.id_table	= mxs_gpio_ids,
Shawn Guo's avatar
Shawn Guo committed
362
};
363

364
static int __init mxs_gpio_init(void)
365
{
366
	return platform_driver_register(&mxs_gpio_driver);
367
}
368 369 370 371 372 373 374
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");