gpio-mxc.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8
// SPDX-License-Identifier: GPL-2.0+
//
// MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
// Copyright 2008 Juergen Beisert, kernel@pengutronix.de
//
// Based on code from Freescale Semiconductor,
// Authors: Daniel Mack, Juergen Beisert.
// Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9

Anson Huang's avatar
Anson Huang committed
10
#include <linux/clk.h>
11
#include <linux/err.h>
12
#include <linux/init.h>
13
#include <linux/interrupt.h>
14 15
#include <linux/io.h>
#include <linux/irq.h>
16
#include <linux/irqdomain.h>
17
#include <linux/irqchip/chained_irq.h>
18 19
#include <linux/platform_device.h>
#include <linux/slab.h>
20
#include <linux/syscore_ops.h>
21
#include <linux/gpio/driver.h>
22 23
#include <linux/of.h>
#include <linux/of_device.h>
24
#include <linux/bug.h>
25
#include <linux/ipipe.h>
26

27 28 29
enum mxc_gpio_hwtype {
	IMX1_GPIO,	/* runs on i.mx1 */
	IMX21_GPIO,	/* runs on i.mx21 and i.mx27 */
30 31
	IMX31_GPIO,	/* runs on i.mx31 */
	IMX35_GPIO,	/* runs on all other i.mx */
32 33 34 35 36 37 38 39 40 41 42
};

/* device type dependent stuff */
struct mxc_gpio_hwdata {
	unsigned dr_reg;
	unsigned gdir_reg;
	unsigned psr_reg;
	unsigned icr1_reg;
	unsigned icr2_reg;
	unsigned imr_reg;
	unsigned isr_reg;
43
	int edge_sel_reg;
44 45 46 47 48 49
	unsigned low_level;
	unsigned high_level;
	unsigned rise_edge;
	unsigned fall_edge;
};

50 51 52 53 54 55 56 57 58
struct mxc_gpio_reg_saved {
	u32 icr1;
	u32 icr2;
	u32 imr;
	u32 gdir;
	u32 edge_sel;
	u32 dr;
};

59 60 61
struct mxc_gpio_port {
	struct list_head node;
	void __iomem *base;
Anson Huang's avatar
Anson Huang committed
62
	struct clk *clk;
63 64
	int irq;
	int irq_high;
65
	struct irq_domain *domain;
66
	struct gpio_chip gc;
67
	struct device *dev;
68
	u32 both_edges;
69 70
	struct mxc_gpio_reg_saved gpio_saved_reg;
	bool power_off;
71 72
};

73 74 75 76 77 78 79 80
static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
	.dr_reg		= 0x1c,
	.gdir_reg	= 0x00,
	.psr_reg	= 0x24,
	.icr1_reg	= 0x28,
	.icr2_reg	= 0x2c,
	.imr_reg	= 0x30,
	.isr_reg	= 0x34,
81
	.edge_sel_reg	= -EINVAL,
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	.low_level	= 0x03,
	.high_level	= 0x02,
	.rise_edge	= 0x00,
	.fall_edge	= 0x01,
};

static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
	.dr_reg		= 0x00,
	.gdir_reg	= 0x04,
	.psr_reg	= 0x08,
	.icr1_reg	= 0x0c,
	.icr2_reg	= 0x10,
	.imr_reg	= 0x14,
	.isr_reg	= 0x18,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	.edge_sel_reg	= -EINVAL,
	.low_level	= 0x00,
	.high_level	= 0x01,
	.rise_edge	= 0x02,
	.fall_edge	= 0x03,
};

static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
	.dr_reg		= 0x00,
	.gdir_reg	= 0x04,
	.psr_reg	= 0x08,
	.icr1_reg	= 0x0c,
	.icr2_reg	= 0x10,
	.imr_reg	= 0x14,
	.isr_reg	= 0x18,
	.edge_sel_reg	= 0x1c,
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	.low_level	= 0x00,
	.high_level	= 0x01,
	.rise_edge	= 0x02,
	.fall_edge	= 0x03,
};

static enum mxc_gpio_hwtype mxc_gpio_hwtype;
static struct mxc_gpio_hwdata *mxc_gpio_hwdata;

#define GPIO_DR			(mxc_gpio_hwdata->dr_reg)
#define GPIO_GDIR		(mxc_gpio_hwdata->gdir_reg)
#define GPIO_PSR		(mxc_gpio_hwdata->psr_reg)
#define GPIO_ICR1		(mxc_gpio_hwdata->icr1_reg)
#define GPIO_ICR2		(mxc_gpio_hwdata->icr2_reg)
#define GPIO_IMR		(mxc_gpio_hwdata->imr_reg)
#define GPIO_ISR		(mxc_gpio_hwdata->isr_reg)
128
#define GPIO_EDGE_SEL		(mxc_gpio_hwdata->edge_sel_reg)
129 130 131 132 133

#define GPIO_INT_LOW_LEV	(mxc_gpio_hwdata->low_level)
#define GPIO_INT_HIGH_LEV	(mxc_gpio_hwdata->high_level)
#define GPIO_INT_RISE_EDGE	(mxc_gpio_hwdata->rise_edge)
#define GPIO_INT_FALL_EDGE	(mxc_gpio_hwdata->fall_edge)
134
#define GPIO_INT_BOTH_EDGES	0x4
135

136
static const struct platform_device_id mxc_gpio_devtype[] = {
137 138 139 140 141 142 143 144 145
	{
		.name = "imx1-gpio",
		.driver_data = IMX1_GPIO,
	}, {
		.name = "imx21-gpio",
		.driver_data = IMX21_GPIO,
	}, {
		.name = "imx31-gpio",
		.driver_data = IMX31_GPIO,
146 147 148
	}, {
		.name = "imx35-gpio",
		.driver_data = IMX35_GPIO,
149 150 151 152 153
	}, {
		/* sentinel */
	}
};

154 155 156 157
static const struct of_device_id mxc_gpio_dt_ids[] = {
	{ .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
	{ .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
	{ .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
158
	{ .compatible = "fsl,imx35-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
159
	{ .compatible = "fsl,imx7d-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
160 161 162
	{ /* sentinel */ }
};

163 164 165 166 167 168
/*
 * MX2 has one interrupt *for all* gpio ports. The list is used
 * to save the references to all ports, so that mx2_gpio_irq_handler
 * can walk through all interrupt status registers.
 */
static LIST_HEAD(mxc_gpio_ports);
169 170 171

/* Note: This driver assumes 32 GPIOs are handled in one register */

172
static int gpio_set_irq_type(struct irq_data *d, u32 type)
173
{
174 175
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mxc_gpio_port *port = gc->private;
176
	u32 bit, val;
177
	u32 gpio_idx = d->hwirq;
178 179 180
	int edge;
	void __iomem *reg = port->base;

181
	port->both_edges &= ~(1 << gpio_idx);
182
	switch (type) {
183
	case IRQ_TYPE_EDGE_RISING:
184 185
		edge = GPIO_INT_RISE_EDGE;
		break;
186
	case IRQ_TYPE_EDGE_FALLING:
187 188
		edge = GPIO_INT_FALL_EDGE;
		break;
189
	case IRQ_TYPE_EDGE_BOTH:
190 191
		if (GPIO_EDGE_SEL >= 0) {
			edge = GPIO_INT_BOTH_EDGES;
192
		} else {
193
			val = port->gc.get(&port->gc, gpio_idx);
194 195
			if (val) {
				edge = GPIO_INT_LOW_LEV;
196
				pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx);
197 198
			} else {
				edge = GPIO_INT_HIGH_LEV;
199
				pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
200
			}
201
			port->both_edges |= 1 << gpio_idx;
202 203
		}
		break;
204
	case IRQ_TYPE_LEVEL_LOW:
205 206
		edge = GPIO_INT_LOW_LEV;
		break;
207
	case IRQ_TYPE_LEVEL_HIGH:
208 209
		edge = GPIO_INT_HIGH_LEV;
		break;
210
	default:
211 212 213
		return -EINVAL;
	}

214 215 216
	if (GPIO_EDGE_SEL >= 0) {
		val = readl(port->base + GPIO_EDGE_SEL);
		if (edge == GPIO_INT_BOTH_EDGES)
217
			writel(val | (1 << gpio_idx),
218 219
				port->base + GPIO_EDGE_SEL);
		else
220
			writel(val & ~(1 << gpio_idx),
221 222 223 224
				port->base + GPIO_EDGE_SEL);
	}

	if (edge != GPIO_INT_BOTH_EDGES) {
225 226
		reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
		bit = gpio_idx & 0xf;
227 228 229 230
		val = readl(reg) & ~(0x3 << (bit << 1));
		writel(val | (edge << (bit << 1)), reg);
	}

231
	writel(1 << gpio_idx, port->base + GPIO_ISR);
232 233 234 235

	return 0;
}

236 237 238 239 240 241 242 243
static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
{
	void __iomem *reg = port->base;
	u32 bit, val;
	int edge;

	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
	bit = gpio & 0xf;
244
	val = readl(reg);
245 246
	edge = (val >> (bit << 1)) & 3;
	val &= ~(0x3 << (bit << 1));
247
	if (edge == GPIO_INT_HIGH_LEV) {
248 249
		edge = GPIO_INT_LOW_LEV;
		pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
250
	} else if (edge == GPIO_INT_LOW_LEV) {
251 252
		edge = GPIO_INT_HIGH_LEV;
		pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
253
	} else {
254 255 256 257
		pr_err("mxc: invalid configuration for GPIO %d: %x\n",
		       gpio, edge);
		return;
	}
258
	writel(val | (edge << (bit << 1)), reg);
259 260
}

261
/* handle 32 interrupts in one status register */
262 263
static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
{
264 265
	while (irq_stat != 0) {
		int irqoffset = fls(irq_stat) - 1;
266

267 268
		if (port->both_edges & (1 << irqoffset))
			mxc_flip_edge(port, irqoffset);
269

270
		ipipe_handle_demuxed_irq(irq_find_mapping(port->domain, irqoffset));
271

272
		irq_stat &= ~(1 << irqoffset);
273 274 275
	}
}

Paulius Zaleckas's avatar
Paulius Zaleckas committed
276
/* MX1 and MX3 has one interrupt *per* gpio port */
277
static void mx3_gpio_irq_handler(struct irq_desc *desc)
278 279
{
	u32 irq_stat;
280 281
	struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
	struct irq_chip *chip = irq_desc_get_chip(desc);
282 283

	chained_irq_enter(chip, desc);
284

285
	irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
286

287
	mxc_gpio_irq_handler(port, irq_stat);
288 289

	chained_irq_exit(chip, desc);
290 291 292
}

/* MX2 has one interrupt *for all* gpio ports */
293
static void mx2_gpio_irq_handler(struct irq_desc *desc)
294 295
{
	u32 irq_msk, irq_stat;
296
	struct mxc_gpio_port *port;
297
	struct irq_chip *chip = irq_desc_get_chip(desc);
298 299

	chained_irq_enter(chip, desc);
300 301

	/* walk through all interrupt status registers */
302 303
	list_for_each_entry(port, &mxc_gpio_ports, node) {
		irq_msk = readl(port->base + GPIO_IMR);
304 305 306
		if (!irq_msk)
			continue;

307
		irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
308
		if (irq_stat)
309
			mxc_gpio_irq_handler(port, irq_stat);
310
	}
311
	chained_irq_exit(chip, desc);
312 313
}

314 315 316 317 318 319 320 321 322
/*
 * 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.
 */
323
static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
324
{
325 326
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mxc_gpio_port *port = gc->private;
327
	u32 gpio_idx = d->hwirq;
328
	int ret;
329 330 331

	if (enable) {
		if (port->irq_high && (gpio_idx >= 16))
332
			ret = enable_irq_wake(port->irq_high);
333
		else
334
			ret = enable_irq_wake(port->irq);
335 336
	} else {
		if (port->irq_high && (gpio_idx >= 16))
337
			ret = disable_irq_wake(port->irq_high);
338
		else
339
			ret = disable_irq_wake(port->irq);
340 341
	}

342
	return ret;
343 344
}

345
static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
346 347 348
{
	struct irq_chip_generic *gc;
	struct irq_chip_type *ct;
349
	int rv;
350

351 352
	gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base,
					 port->base, handle_level_irq);
353 354
	if (!gc)
		return -ENOMEM;
355 356 357
	gc->private = port;

	ct = gc->chip_types;
358
	ct->chip.irq_ack = irq_gc_ack_set_bit;
359 360 361
	ct->chip.irq_mask = irq_gc_mask_clr_bit;
	ct->chip.irq_unmask = irq_gc_mask_set_bit;
	ct->chip.irq_set_type = gpio_set_irq_type;
362
	ct->chip.irq_set_wake = gpio_set_wake_irq;
363
	ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_PIPELINE_SAFE;
364 365 366
	ct->regs.ack = GPIO_ISR;
	ct->regs.mask = GPIO_IMR;

367 368 369
	rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
					 IRQ_GC_INIT_NESTED_LOCK,
					 IRQ_NOREQUEST, 0);
370

371
	return rv;
372
}
373

Bill Pemberton's avatar
Bill Pemberton committed
374
static void mxc_gpio_get_hw(struct platform_device *pdev)
375
{
376 377 378 379 380 381 382
	const struct of_device_id *of_id =
			of_match_device(mxc_gpio_dt_ids, &pdev->dev);
	enum mxc_gpio_hwtype hwtype;

	if (of_id)
		pdev->id_entry = of_id->data;
	hwtype = pdev->id_entry->driver_data;
383 384 385 386 387 388 389 390 391 392 393

	if (mxc_gpio_hwtype) {
		/*
		 * The driver works with a reasonable presupposition,
		 * that is all gpio ports must be the same type when
		 * running on one soc.
		 */
		BUG_ON(mxc_gpio_hwtype != hwtype);
		return;
	}

394 395 396
	if (hwtype == IMX35_GPIO)
		mxc_gpio_hwdata = &imx35_gpio_hwdata;
	else if (hwtype == IMX31_GPIO)
397 398 399 400 401 402 403
		mxc_gpio_hwdata = &imx31_gpio_hwdata;
	else
		mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;

	mxc_gpio_hwtype = hwtype;
}

404 405
static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
406
	struct mxc_gpio_port *port = gpiochip_get_data(gc);
407

408
	return irq_find_mapping(port->domain, offset);
409 410
}

Bill Pemberton's avatar
Bill Pemberton committed
411
static int mxc_gpio_probe(struct platform_device *pdev)
412
{
413
	struct device_node *np = pdev->dev.of_node;
414 415
	struct mxc_gpio_port *port;
	struct resource *iores;
416
	int irq_base;
417
	int err;
418

419 420
	mxc_gpio_get_hw(pdev);

421
	port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
422 423
	if (!port)
		return -ENOMEM;
424

425 426
	port->dev = &pdev->dev;

427
	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 429 430
	port->base = devm_ioremap_resource(&pdev->dev, iores);
	if (IS_ERR(port->base))
		return PTR_ERR(port->base);
431 432

	port->irq_high = platform_get_irq(pdev, 1);
433 434 435
	if (port->irq_high < 0)
		port->irq_high = 0;

436
	port->irq = platform_get_irq(pdev, 0);
437
	if (port->irq < 0)
438
		return port->irq;
439

Anson Huang's avatar
Anson Huang committed
440 441 442 443 444 445 446 447 448 449 450
	/* the controller clock is optional */
	port->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(port->clk))
		port->clk = NULL;

	err = clk_prepare_enable(port->clk);
	if (err) {
		dev_err(&pdev->dev, "Unable to enable clock.\n");
		return err;
	}

451 452 453
	if (of_device_is_compatible(np, "fsl,imx7d-gpio"))
		port->power_off = true;

454 455 456 457
	/* disable the interrupt and clear the status */
	writel(0, port->base + GPIO_IMR);
	writel(~0, port->base + GPIO_ISR);

458
	if (mxc_gpio_hwtype == IMX21_GPIO) {
459 460 461 462 463 464
		/*
		 * Setup one handler for all GPIO interrupts. Actually setting
		 * the handler is needed only once, but doing it for every port
		 * is more robust and easier.
		 */
		irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
465 466
	} else {
		/* setup one handler for each entry */
467 468 469
		irq_set_chained_handler_and_data(port->irq,
						 mx3_gpio_irq_handler, port);
		if (port->irq_high > 0)
470
			/* setup handler for GPIO 16 to 31 */
471 472 473
			irq_set_chained_handler_and_data(port->irq_high,
							 mx3_gpio_irq_handler,
							 port);
474 475
	}

476
	err = bgpio_init(&port->gc, &pdev->dev, 4,
477 478
			 port->base + GPIO_PSR,
			 port->base + GPIO_DR, NULL,
479 480
			 port->base + GPIO_GDIR, NULL,
			 BGPIOF_READ_OUTPUT_REG_SET);
481
	if (err)
482
		goto out_bgio;
483

484 485 486 487 488
	if (of_property_read_bool(np, "gpio-ranges")) {
		port->gc.request = gpiochip_generic_request;
		port->gc.free = gpiochip_generic_free;
	}

489 490
	port->gc.to_irq = mxc_gpio_to_irq;
	port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
491
					     pdev->id * 32;
492

493
	err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
494
	if (err)
495
		goto out_bgio;
496

497
	irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
498 499
	if (irq_base < 0) {
		err = irq_base;
500
		goto out_bgio;
501 502 503 504 505 506
	}

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

	/* gpio-mxc can be a generic irq chip */
511 512 513
	err = mxc_gpio_init_gc(port, irq_base);
	if (err < 0)
		goto out_irqdomain_remove;
514

515 516
	list_add_tail(&port->node, &mxc_gpio_ports);

517 518
	platform_set_drvdata(pdev, port);

519
	return 0;
520

521 522
out_irqdomain_remove:
	irq_domain_remove(port->domain);
523
out_bgio:
Anson Huang's avatar
Anson Huang committed
524
	clk_disable_unprepare(port->clk);
525 526
	dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
	return err;
527
}
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
{
	if (!port->power_off)
		return;

	port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
	port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
	port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
	port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
	port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
	port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
}

static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
{
	if (!port->power_off)
		return;

	writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
	writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
	writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
	writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
	writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
	writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
}

555
static int mxc_gpio_syscore_suspend(void)
556
{
557
	struct mxc_gpio_port *port;
558

559 560 561 562 563
	/* walk through all ports */
	list_for_each_entry(port, &mxc_gpio_ports, node) {
		mxc_gpio_save_regs(port);
		clk_disable_unprepare(port->clk);
	}
564 565 566 567

	return 0;
}

568
static void mxc_gpio_syscore_resume(void)
569
{
570
	struct mxc_gpio_port *port;
571 572
	int ret;

573 574 575 576 577 578 579 580 581
	/* walk through all ports */
	list_for_each_entry(port, &mxc_gpio_ports, node) {
		ret = clk_prepare_enable(port->clk);
		if (ret) {
			pr_err("mxc: failed to enable gpio clock %d\n", ret);
			return;
		}
		mxc_gpio_restore_regs(port);
	}
582 583
}

584 585 586
static struct syscore_ops mxc_gpio_syscore_ops = {
	.suspend = mxc_gpio_syscore_suspend,
	.resume = mxc_gpio_syscore_resume,
587 588
};

589 590 591
static struct platform_driver mxc_gpio_driver = {
	.driver		= {
		.name	= "gpio-mxc",
592
		.of_match_table = mxc_gpio_dt_ids,
593
		.suppress_bind_attrs = true,
594 595
	},
	.probe		= mxc_gpio_probe,
596
	.id_table	= mxc_gpio_devtype,
597 598 599 600
};

static int __init gpio_mxc_init(void)
{
601 602
	register_syscore_ops(&mxc_gpio_syscore_ops);

603 604
	return platform_driver_register(&mxc_gpio_driver);
}
605
subsys_initcall(gpio_mxc_init);