gpio-dwapb.c 20.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6
/*
 * Copyright (c) 2011 Jamie Iles
 *
 * All enquiries to support@picochip.com
 */
7
#include <linux/acpi.h>
8
#include <linux/clk.h>
9
#include <linux/err.h>
10
#include <linux/gpio/driver.h>
11 12 13 14 15 16 17 18 19
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
20
#include <linux/of_device.h>
21 22
#include <linux/of_irq.h>
#include <linux/platform_device.h>
23
#include <linux/property.h>
A
Alan Tull 已提交
24
#include <linux/reset.h>
25
#include <linux/spinlock.h>
26 27
#include <linux/platform_data/gpio-dwapb.h>
#include <linux/slab.h>
28

29
#include "gpiolib.h"
30
#include "gpiolib-acpi.h"
31

32 33 34 35 36 37 38 39 40 41 42 43 44
#define GPIO_SWPORTA_DR		0x00
#define GPIO_SWPORTA_DDR	0x04
#define GPIO_SWPORTB_DR		0x0c
#define GPIO_SWPORTB_DDR	0x10
#define GPIO_SWPORTC_DR		0x18
#define GPIO_SWPORTC_DDR	0x1c
#define GPIO_SWPORTD_DR		0x24
#define GPIO_SWPORTD_DDR	0x28
#define GPIO_INTEN		0x30
#define GPIO_INTMASK		0x34
#define GPIO_INTTYPE_LEVEL	0x38
#define GPIO_INT_POLARITY	0x3c
#define GPIO_INTSTATUS		0x40
W
Weike Chen 已提交
45
#define GPIO_PORTA_DEBOUNCE	0x48
46 47 48 49 50 51
#define GPIO_PORTA_EOI		0x4c
#define GPIO_EXT_PORTA		0x50
#define GPIO_EXT_PORTB		0x54
#define GPIO_EXT_PORTC		0x58
#define GPIO_EXT_PORTD		0x5c

52
#define DWAPB_DRIVER_NAME	"gpio-dwapb"
53
#define DWAPB_MAX_PORTS		4
54

55 56 57
#define GPIO_EXT_PORT_STRIDE	0x04 /* register stride 32 bits */
#define GPIO_SWPORT_DR_STRIDE	0x0c /* register stride 3*32 bits */
#define GPIO_SWPORT_DDR_STRIDE	0x0c /* register stride 3*32 bits */
58

59 60 61 62 63 64 65 66
#define GPIO_REG_OFFSET_V2	1

#define GPIO_INTMASK_V2		0x44
#define GPIO_INTTYPE_LEVEL_V2	0x34
#define GPIO_INT_POLARITY_V2	0x38
#define GPIO_INTSTATUS_V2	0x3c
#define GPIO_PORTA_EOI_V2	0x40

67 68
#define DWAPB_NR_CLOCKS		2

69 70
struct dwapb_gpio;

71 72 73 74 75 76 77 78 79 80 81
#ifdef CONFIG_PM_SLEEP
/* Store GPIO context across system-wide suspend/resume transitions */
struct dwapb_context {
	u32 data;
	u32 dir;
	u32 ext;
	u32 int_en;
	u32 int_mask;
	u32 int_type;
	u32 int_pol;
	u32 int_deb;
82
	u32 wake_en;
83 84 85
};
#endif

86
struct dwapb_gpio_port {
87
	struct gpio_chip	gc;
88 89
	bool			is_registered;
	struct dwapb_gpio	*gpio;
90 91 92 93
#ifdef CONFIG_PM_SLEEP
	struct dwapb_context	*ctx;
#endif
	unsigned int		idx;
94 95 96 97 98 99 100 101
};

struct dwapb_gpio {
	struct	device		*dev;
	void __iomem		*regs;
	struct dwapb_gpio_port	*ports;
	unsigned int		nr_ports;
	struct irq_domain	*domain;
102
	unsigned int		flags;
A
Alan Tull 已提交
103
	struct reset_control	*rst;
104
	struct clk_bulk_data	clks[DWAPB_NR_CLOCKS];
105 106
};

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static inline u32 gpio_reg_v2_convert(unsigned int offset)
{
	switch (offset) {
	case GPIO_INTMASK:
		return GPIO_INTMASK_V2;
	case GPIO_INTTYPE_LEVEL:
		return GPIO_INTTYPE_LEVEL_V2;
	case GPIO_INT_POLARITY:
		return GPIO_INT_POLARITY_V2;
	case GPIO_INTSTATUS:
		return GPIO_INTSTATUS_V2;
	case GPIO_PORTA_EOI:
		return GPIO_PORTA_EOI_V2;
	}

	return offset;
}

static inline u32 gpio_reg_convert(struct dwapb_gpio *gpio, unsigned int offset)
{
	if (gpio->flags & GPIO_REG_OFFSET_V2)
		return gpio_reg_v2_convert(offset);

	return offset;
}

133 134
static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
{
135
	struct gpio_chip *gc	= &gpio->ports[0].gc;
136 137
	void __iomem *reg_base	= gpio->regs;

138
	return gc->read_reg(reg_base + gpio_reg_convert(gpio, offset));
139 140 141 142 143
}

static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
			       u32 val)
{
144
	struct gpio_chip *gc	= &gpio->ports[0].gc;
145 146
	void __iomem *reg_base	= gpio->regs;

147
	gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
148 149
}

150 151
static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
152
	struct dwapb_gpio_port *port = gpiochip_get_data(gc);
153 154 155 156 157
	struct dwapb_gpio *gpio = port->gpio;

	return irq_find_mapping(gpio->domain, offset);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171
static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsigned int offs)
{
	struct dwapb_gpio_port *port;
	int i;

	for (i = 0; i < gpio->nr_ports; i++) {
		port = &gpio->ports[i];
		if (port->idx == offs / 32)
			return port;
	}

	return NULL;
}

172 173
static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
{
174 175 176 177 178 179 180 181
	struct dwapb_gpio_port *port = dwapb_offs_to_port(gpio, offs);
	struct gpio_chip *gc;
	u32 pol;
	int val;

	if (!port)
		return;
	gc = &port->gc;
182

183 184 185 186 187
	pol = dwapb_read(gpio, GPIO_INT_POLARITY);
	/* Just read the current value right out of the data register */
	val = gc->get(gc, offs % 32);
	if (val)
		pol &= ~BIT(offs);
188
	else
189
		pol |= BIT(offs);
190

191
	dwapb_write(gpio, GPIO_INT_POLARITY, pol);
192 193
}

194
static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
195
{
196 197
	unsigned long irq_status;
	int hwirq;
198

199 200
	irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
	for_each_set_bit(hwirq, &irq_status, 32) {
201
		int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
202
		u32 irq_type = irq_get_trigger_type(gpio_irq);
203 204 205

		generic_handle_irq(gpio_irq);

206
		if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
207 208 209
			dwapb_toggle_trigger(gpio, hwirq);
	}

210
	return irq_status;
211 212
}

213
static void dwapb_irq_handler(struct irq_desc *desc)
214
{
215
	struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
216 217 218 219
	struct irq_chip *chip = irq_desc_get_chip(desc);

	dwapb_do_irq(gpio);

220 221 222 223 224 225 226 227
	if (chip->irq_eoi)
		chip->irq_eoi(irq_desc_get_irq_data(desc));
}

static void dwapb_irq_enable(struct irq_data *d)
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
228
	struct gpio_chip *gc = &gpio->ports[0].gc;
229 230 231
	unsigned long flags;
	u32 val;

232
	spin_lock_irqsave(&gc->bgpio_lock, flags);
233
	val = dwapb_read(gpio, GPIO_INTEN);
234
	val |= BIT(d->hwirq);
235
	dwapb_write(gpio, GPIO_INTEN, val);
236
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
237 238 239 240 241 242
}

static void dwapb_irq_disable(struct irq_data *d)
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
243
	struct gpio_chip *gc = &gpio->ports[0].gc;
244 245 246
	unsigned long flags;
	u32 val;

247
	spin_lock_irqsave(&gc->bgpio_lock, flags);
248
	val = dwapb_read(gpio, GPIO_INTEN);
249
	val &= ~BIT(d->hwirq);
250
	dwapb_write(gpio, GPIO_INTEN, val);
251
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
252 253
}

254
static int dwapb_irq_reqres(struct irq_data *d)
255 256 257
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
258
	struct gpio_chip *gc = &gpio->ports[0].gc;
259
	int ret;
260

261 262
	ret = gpiochip_lock_as_irq(gc, irqd_to_hwirq(d));
	if (ret) {
263 264
		dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
			irqd_to_hwirq(d));
265
		return ret;
266
	}
267 268 269
	return 0;
}

270
static void dwapb_irq_relres(struct irq_data *d)
271 272 273
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
274
	struct gpio_chip *gc = &gpio->ports[0].gc;
275

276
	gpiochip_unlock_as_irq(gc, irqd_to_hwirq(d));
277 278 279 280 281 282
}

static int dwapb_irq_set_type(struct irq_data *d, u32 type)
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
283
	struct gpio_chip *gc = &gpio->ports[0].gc;
284 285 286 287 288 289 290
	int bit = d->hwirq;
	unsigned long level, polarity, flags;

	if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
		     IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
		return -EINVAL;

291
	spin_lock_irqsave(&gc->bgpio_lock, flags);
292 293
	level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
	polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

	switch (type) {
	case IRQ_TYPE_EDGE_BOTH:
		level |= BIT(bit);
		dwapb_toggle_trigger(gpio, bit);
		break;
	case IRQ_TYPE_EDGE_RISING:
		level |= BIT(bit);
		polarity |= BIT(bit);
		break;
	case IRQ_TYPE_EDGE_FALLING:
		level |= BIT(bit);
		polarity &= ~BIT(bit);
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		level &= ~BIT(bit);
		polarity |= BIT(bit);
		break;
	case IRQ_TYPE_LEVEL_LOW:
		level &= ~BIT(bit);
		polarity &= ~BIT(bit);
		break;
	}

318 319
	irq_setup_alt_chip(d, type);

320
	dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
321 322
	if (type != IRQ_TYPE_EDGE_BOTH)
		dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
323
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
324 325 326 327

	return 0;
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#ifdef CONFIG_PM_SLEEP
static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
{
	struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
	struct dwapb_gpio *gpio = igc->private;
	struct dwapb_context *ctx = gpio->ports[0].ctx;

	if (enable)
		ctx->wake_en |= BIT(d->hwirq);
	else
		ctx->wake_en &= ~BIT(d->hwirq);

	return 0;
}
#endif

W
Weike Chen 已提交
344 345 346
static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
				   unsigned offset, unsigned debounce)
{
347
	struct dwapb_gpio_port *port = gpiochip_get_data(gc);
W
Weike Chen 已提交
348 349
	struct dwapb_gpio *gpio = port->gpio;
	unsigned long flags, val_deb;
L
Linus Walleij 已提交
350
	unsigned long mask = BIT(offset);
W
Weike Chen 已提交
351

352
	spin_lock_irqsave(&gc->bgpio_lock, flags);
W
Weike Chen 已提交
353 354 355 356 357 358 359

	val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
	if (debounce)
		dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb | mask);
	else
		dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb & ~mask);

360
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
W
Weike Chen 已提交
361 362 363 364

	return 0;
}

365 366 367 368 369 370 371 372 373 374 375 376
static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
				 unsigned long config)
{
	u32 debounce;

	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
		return -ENOTSUPP;

	debounce = pinconf_to_config_argument(config);
	return dwapb_gpio_set_debounce(gc, offset, debounce);
}

377 378 379 380 381 382 383 384 385 386
static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
{
	u32 worked;
	struct dwapb_gpio *gpio = dev_id;

	worked = dwapb_do_irq(gpio);

	return worked ? IRQ_HANDLED : IRQ_NONE;
}

387
static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
388 389
				 struct dwapb_gpio_port *port,
				 struct dwapb_port_property *pp)
390
{
391
	struct gpio_chip *gc = &port->gc;
392
	struct fwnode_handle  *fwnode = pp->fwnode;
393
	struct irq_chip_generic	*irq_gc = NULL;
394 395
	unsigned int hwirq, ngpio = gc->ngpio;
	struct irq_chip_type *ct;
396
	int err, i;
397

398 399
	gpio->domain = irq_domain_create_linear(fwnode, ngpio,
						 &irq_generic_chip_ops, gpio);
400 401 402
	if (!gpio->domain)
		return;

403
	err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
404
					     DWAPB_DRIVER_NAME, handle_level_irq,
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
					     IRQ_NOREQUEST, 0,
					     IRQ_GC_INIT_NESTED_LOCK);
	if (err) {
		dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
		irq_domain_remove(gpio->domain);
		gpio->domain = NULL;
		return;
	}

	irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
	if (!irq_gc) {
		irq_domain_remove(gpio->domain);
		gpio->domain = NULL;
		return;
	}

	irq_gc->reg_base = gpio->regs;
	irq_gc->private = gpio;

424 425 426 427 428 429 430 431 432 433
	for (i = 0; i < 2; i++) {
		ct = &irq_gc->chip_types[i];
		ct->chip.irq_ack = irq_gc_ack_set_bit;
		ct->chip.irq_mask = irq_gc_mask_set_bit;
		ct->chip.irq_unmask = irq_gc_mask_clr_bit;
		ct->chip.irq_set_type = dwapb_irq_set_type;
		ct->chip.irq_enable = dwapb_irq_enable;
		ct->chip.irq_disable = dwapb_irq_disable;
		ct->chip.irq_request_resources = dwapb_irq_reqres;
		ct->chip.irq_release_resources = dwapb_irq_relres;
434 435 436
#ifdef CONFIG_PM_SLEEP
		ct->chip.irq_set_wake = dwapb_irq_set_wake;
#endif
437 438
		ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI);
		ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK);
439 440 441 442 443 444
		ct->type = IRQ_TYPE_LEVEL_MASK;
	}

	irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
	irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
	irq_gc->chip_types[1].handler = handle_edge_irq;
445

446
	if (!pp->irq_shared) {
447 448 449
		int i;

		for (i = 0; i < pp->ngpio; i++) {
450
			if (pp->irq[i] >= 0)
451 452 453
				irq_set_chained_handler_and_data(pp->irq[i],
						dwapb_irq_handler, gpio);
		}
454 455 456 457 458
	} else {
		/*
		 * Request a shared IRQ since where MFD would have devices
		 * using the same irq pin
		 */
459
		err = devm_request_irq(gpio->dev, pp->irq[0],
460
				       dwapb_irq_handler_mfd,
461
				       IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
462 463 464 465 466 467 468
		if (err) {
			dev_err(gpio->dev, "error requesting IRQ\n");
			irq_domain_remove(gpio->domain);
			gpio->domain = NULL;
			return;
		}
	}
469 470 471 472

	for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
		irq_create_mapping(gpio->domain, hwirq);

473
	port->gc.to_irq = dwapb_gpio_to_irq;
474 475 476 477 478
}

static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
{
	struct dwapb_gpio_port *port = &gpio->ports[0];
479
	struct gpio_chip *gc = &port->gc;
480 481 482 483 484 485 486 487 488 489 490 491 492 493
	unsigned int ngpio = gc->ngpio;
	irq_hw_number_t hwirq;

	if (!gpio->domain)
		return;

	for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
		irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));

	irq_domain_remove(gpio->domain);
	gpio->domain = NULL;
}

static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
494
			       struct dwapb_port_property *pp,
495 496 497 498 499 500 501 502
			       unsigned int offs)
{
	struct dwapb_gpio_port *port;
	void __iomem *dat, *set, *dirout;
	int err;

	port = &gpio->ports[offs];
	port->gpio = gpio;
503 504 505 506 507 508 509
	port->idx = pp->idx;

#ifdef CONFIG_PM_SLEEP
	port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
	if (!port->ctx)
		return -ENOMEM;
#endif
510

511 512
	dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_STRIDE);
	set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_STRIDE);
513
	dirout = gpio->regs + GPIO_SWPORTA_DDR +
514
		(pp->idx * GPIO_SWPORT_DDR_STRIDE);
515

516
	/* This registers 32 GPIO lines per port */
517
	err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
L
Linus Walleij 已提交
518
			 NULL, 0);
519
	if (err) {
520 521
		dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
			port->idx);
522 523 524
		return err;
	}

525
#ifdef CONFIG_OF_GPIO
526
	port->gc.of_node = to_of_node(pp->fwnode);
527
#endif
528 529
	port->gc.ngpio = pp->ngpio;
	port->gc.base = pp->gpio_base;
530

W
Weike Chen 已提交
531 532
	/* Only port A support debounce */
	if (pp->idx == 0)
533
		port->gc.set_config = dwapb_gpio_set_config;
W
Weike Chen 已提交
534

535
	if (pp->has_irq)
536
		dwapb_configure_irqs(gpio, port, pp);
537

538
	err = gpiochip_add_data(&port->gc, port);
539
	if (err)
540 541
		dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
			port->idx);
542 543 544
	else
		port->is_registered = true;

545
	/* Add GPIO-signaled ACPI event support */
546
	if (pp->has_irq)
547 548
		acpi_gpiochip_request_interrupts(&port->gc);

549 550 551 552 553 554 555 556 557
	return err;
}

static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
{
	unsigned int m;

	for (m = 0; m < gpio->nr_ports; ++m)
		if (gpio->ports[m].is_registered)
558
			gpiochip_remove(&gpio->ports[m].gc);
559 560
}

561
static struct dwapb_platform_data *
562
dwapb_gpio_get_pdata(struct device *dev)
563
{
564
	struct fwnode_handle *fwnode;
565 566 567
	struct dwapb_platform_data *pdata;
	struct dwapb_port_property *pp;
	int nports;
568
	int i, j;
569

570
	nports = device_get_child_node_count(dev);
571 572 573
	if (nports == 0)
		return ERR_PTR(-ENODEV);

574
	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
575 576 577
	if (!pdata)
		return ERR_PTR(-ENOMEM);

578 579
	pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
	if (!pdata->properties)
580 581 582 583 584
		return ERR_PTR(-ENOMEM);

	pdata->nports = nports;

	i = 0;
585
	device_for_each_child_node(dev, fwnode)  {
586 587
		struct device_node *np = NULL;

588
		pp = &pdata->properties[i++];
589
		pp->fwnode = fwnode;
590

591
		if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
592
		    pp->idx >= DWAPB_MAX_PORTS) {
593 594
			dev_err(dev,
				"missing/invalid port index for port%d\n", i);
595
			fwnode_handle_put(fwnode);
596 597 598
			return ERR_PTR(-EINVAL);
		}

599
		if (fwnode_property_read_u32(fwnode, "snps,nr-gpios",
600
					 &pp->ngpio)) {
601 602 603
			dev_info(dev,
				 "failed to get number of gpios for port%d\n",
				 i);
604 605 606
			pp->ngpio = 32;
		}

607 608 609
		pp->irq_shared	= false;
		pp->gpio_base	= -1;

610 611 612 613
		/*
		 * Only port A can provide interrupts in all configurations of
		 * the IP.
		 */
614 615
		if (pp->idx != 0)
			continue;
616

617 618 619
		if (dev->of_node && fwnode_property_read_bool(fwnode,
						  "interrupt-controller")) {
			np = to_of_node(fwnode);
620 621
		}

622 623
		for (j = 0; j < pp->ngpio; j++) {
			pp->irq[j] = -ENXIO;
624

625 626 627
			if (np)
				pp->irq[j] = of_irq_get(np, j);
			else if (has_acpi_companion(dev))
628
				pp->irq[j] = platform_get_irq(to_platform_device(dev), j);
629 630 631

			if (pp->irq[j] >= 0)
				pp->has_irq = true;
632
		}
633

634 635
		if (!pp->has_irq)
			dev_warn(dev, "no irq for port%d\n", pp->idx);
636 637 638 639 640
	}

	return pdata;
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
static const struct of_device_id dwapb_of_match[] = {
	{ .compatible = "snps,dw-apb-gpio", .data = (void *)0},
	{ .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
	{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, dwapb_of_match);

static const struct acpi_device_id dwapb_acpi_match[] = {
	{"HISI0181", 0},
	{"APMC0D07", 0},
	{"APMC0D81", GPIO_REG_OFFSET_V2},
	{ }
};
MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);

656 657
static int dwapb_gpio_probe(struct platform_device *pdev)
{
658
	unsigned int i;
659 660
	struct dwapb_gpio *gpio;
	int err;
661 662 663
	struct device *dev = &pdev->dev;
	struct dwapb_platform_data *pdata = dev_get_platdata(dev);

664
	if (!pdata) {
665
		pdata = dwapb_gpio_get_pdata(dev);
666 667 668
		if (IS_ERR(pdata))
			return PTR_ERR(pdata);
	}
669

670 671
	if (!pdata->nports)
		return -ENODEV;
672

673
	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
674 675 676
	if (!gpio)
		return -ENOMEM;

677 678 679
	gpio->dev = &pdev->dev;
	gpio->nr_ports = pdata->nports;

A
Alan Tull 已提交
680 681 682 683 684 685
	gpio->rst = devm_reset_control_get_optional_shared(dev, NULL);
	if (IS_ERR(gpio->rst))
		return PTR_ERR(gpio->rst);

	reset_control_deassert(gpio->rst);

686
	gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
687
				   sizeof(*gpio->ports), GFP_KERNEL);
688 689
	if (!gpio->ports)
		return -ENOMEM;
690

691
	gpio->regs = devm_platform_ioremap_resource(pdev, 0);
692 693
	if (IS_ERR(gpio->regs))
		return PTR_ERR(gpio->regs);
694

695 696 697 698 699 700 701 702
	/* Optional bus and debounce clocks */
	gpio->clks[0].id = "bus";
	gpio->clks[1].id = "db";
	err = devm_clk_bulk_get_optional(&pdev->dev, DWAPB_NR_CLOCKS,
					 gpio->clks);
	if (err) {
		dev_err(&pdev->dev, "Cannot get APB/Debounce clocks\n");
		return err;
703 704
	}

705
	err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
706
	if (err) {
707
		dev_err(&pdev->dev, "Cannot enable APB/Debounce clocks\n");
708
		return err;
709 710
	}

711 712
	gpio->flags = 0;
	if (dev->of_node) {
713
		gpio->flags = (uintptr_t)of_device_get_match_data(dev);
714 715 716 717 718 719 720 721 722 723
	} else if (has_acpi_companion(dev)) {
		const struct acpi_device_id *acpi_id;

		acpi_id = acpi_match_device(dwapb_acpi_match, dev);
		if (acpi_id) {
			if (acpi_id->driver_data)
				gpio->flags = acpi_id->driver_data;
		}
	}

724 725
	for (i = 0; i < gpio->nr_ports; i++) {
		err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
726 727 728 729 730
		if (err)
			goto out_unregister;
	}
	platform_set_drvdata(pdev, gpio);

731
	return 0;
732 733 734 735

out_unregister:
	dwapb_gpio_unregister(gpio);
	dwapb_irq_teardown(gpio);
736
	clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
737 738 739 740 741 742 743 744 745 746

	return err;
}

static int dwapb_gpio_remove(struct platform_device *pdev)
{
	struct dwapb_gpio *gpio = platform_get_drvdata(pdev);

	dwapb_gpio_unregister(gpio);
	dwapb_irq_teardown(gpio);
A
Alan Tull 已提交
747
	reset_control_assert(gpio->rst);
748
	clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
749 750 751 752

	return 0;
}

753 754 755
#ifdef CONFIG_PM_SLEEP
static int dwapb_gpio_suspend(struct device *dev)
{
756
	struct dwapb_gpio *gpio = dev_get_drvdata(dev);
757
	struct gpio_chip *gc	= &gpio->ports[0].gc;
758 759 760
	unsigned long flags;
	int i;

761
	spin_lock_irqsave(&gc->bgpio_lock, flags);
762 763 764 765 766
	for (i = 0; i < gpio->nr_ports; i++) {
		unsigned int offset;
		unsigned int idx = gpio->ports[i].idx;
		struct dwapb_context *ctx = gpio->ports[i].ctx;

767
		BUG_ON(!ctx);
768

769
		offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
770 771
		ctx->dir = dwapb_read(gpio, offset);

772
		offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
773 774
		ctx->data = dwapb_read(gpio, offset);

775
		offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
776 777 778 779 780 781 782 783 784 785 786
		ctx->ext = dwapb_read(gpio, offset);

		/* Only port A can provide interrupts */
		if (idx == 0) {
			ctx->int_mask	= dwapb_read(gpio, GPIO_INTMASK);
			ctx->int_en	= dwapb_read(gpio, GPIO_INTEN);
			ctx->int_pol	= dwapb_read(gpio, GPIO_INT_POLARITY);
			ctx->int_type	= dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
			ctx->int_deb	= dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);

			/* Mask out interrupts */
787 788
			dwapb_write(gpio, GPIO_INTMASK,
				    0xffffffff & ~ctx->wake_en);
789 790
		}
	}
791
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
792

793
	clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
794

795 796 797 798 799
	return 0;
}

static int dwapb_gpio_resume(struct device *dev)
{
800
	struct dwapb_gpio *gpio = dev_get_drvdata(dev);
801
	struct gpio_chip *gc	= &gpio->ports[0].gc;
802
	unsigned long flags;
803
	int i, err;
804

805 806 807 808 809
	err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
	if (err) {
		dev_err(gpio->dev, "Cannot reenable APB/Debounce clocks\n");
		return err;
	}
810

811
	spin_lock_irqsave(&gc->bgpio_lock, flags);
812 813 814 815 816
	for (i = 0; i < gpio->nr_ports; i++) {
		unsigned int offset;
		unsigned int idx = gpio->ports[i].idx;
		struct dwapb_context *ctx = gpio->ports[i].ctx;

817
		BUG_ON(!ctx);
818

819
		offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
820 821
		dwapb_write(gpio, offset, ctx->data);

822
		offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
823 824
		dwapb_write(gpio, offset, ctx->dir);

825
		offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
826 827 828 829 830 831 832 833 834 835 836 837 838 839
		dwapb_write(gpio, offset, ctx->ext);

		/* Only port A can provide interrupts */
		if (idx == 0) {
			dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
			dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
			dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
			dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
			dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);

			/* Clear out spurious interrupts */
			dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
		}
	}
840
	spin_unlock_irqrestore(&gc->bgpio_lock, flags);
841 842 843 844 845 846 847 848

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
			 dwapb_gpio_resume);

849 850
static struct platform_driver dwapb_gpio_driver = {
	.driver		= {
851
		.name	= DWAPB_DRIVER_NAME,
852
		.pm	= &dwapb_gpio_pm_ops,
853
		.of_match_table = of_match_ptr(dwapb_of_match),
854
		.acpi_match_table = ACPI_PTR(dwapb_acpi_match),
855 856 857 858 859 860 861 862 863 864
	},
	.probe		= dwapb_gpio_probe,
	.remove		= dwapb_gpio_remove,
};

module_platform_driver(dwapb_gpio_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jamie Iles");
MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");
865
MODULE_ALIAS("platform:" DWAPB_DRIVER_NAME);