gpio-pca953x.c 22.2 KB
Newer Older
1
/*
2
 *  PCA953x 4/8/16/24/40 bit I/O ports
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
 *  Copyright (C) 2007 Marvell International Ltd.
 *
 *  Derived from drivers/i2c/chips/pca9539.c
 *
 *  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; version 2 of the License.
 */

#include <linux/module.h>
#include <linux/init.h>
16
#include <linux/gpio.h>
17
#include <linux/interrupt.h>
18
#include <linux/i2c.h>
19
#include <linux/platform_data/pca953x.h>
20
#include <linux/slab.h>
21
#include <asm/unaligned.h>
22
#include <linux/of_platform.h>
23
#include <linux/acpi.h>
24

25 26 27 28 29
#define PCA953X_INPUT		0
#define PCA953X_OUTPUT		1
#define PCA953X_INVERT		2
#define PCA953X_DIRECTION	3

A
Andreas Schallenberg 已提交
30 31
#define REG_ADDR_AI		0x80

32 33 34 35 36 37 38 39 40
#define PCA957X_IN		0
#define PCA957X_INVRT		1
#define PCA957X_BKEN		2
#define PCA957X_PUPD		3
#define PCA957X_CFG		4
#define PCA957X_OUT		5
#define PCA957X_MSK		6
#define PCA957X_INTS		7

41 42 43 44
#define PCAL953X_IN_LATCH	34
#define PCAL953X_INT_MASK	37
#define PCAL953X_INT_STAT	38

45 46
#define PCA_GPIO_MASK		0x00FF
#define PCA_INT			0x0100
47
#define PCA_PCAL		0x0200
48 49
#define PCA953X_TYPE		0x1000
#define PCA957X_TYPE		0x2000
50 51 52
#define PCA_TYPE_MASK		0xF000

#define PCA_CHIP_TYPE(x)	((x) & PCA_TYPE_MASK)
53

54
static const struct i2c_device_id pca953x_id[] = {
55
	{ "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
56 57 58 59 60 61 62 63 64 65 66 67
	{ "pca9534", 8  | PCA953X_TYPE | PCA_INT, },
	{ "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
	{ "pca9536", 4  | PCA953X_TYPE, },
	{ "pca9537", 4  | PCA953X_TYPE | PCA_INT, },
	{ "pca9538", 8  | PCA953X_TYPE | PCA_INT, },
	{ "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
	{ "pca9554", 8  | PCA953X_TYPE | PCA_INT, },
	{ "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
	{ "pca9556", 8  | PCA953X_TYPE, },
	{ "pca9557", 8  | PCA953X_TYPE, },
	{ "pca9574", 8  | PCA957X_TYPE | PCA_INT, },
	{ "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
A
Aaron Sierra 已提交
68
	{ "pca9698", 40 | PCA953X_TYPE, },
69 70 71 72 73 74 75 76

	{ "max7310", 8  | PCA953X_TYPE, },
	{ "max7312", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7313", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7315", 8  | PCA953X_TYPE | PCA_INT, },
	{ "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
A
Andreas Schallenberg 已提交
77
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
78
	{ "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
A
Aaron Sierra 已提交
79
	{ "xra1202", 8  | PCA953X_TYPE },
80
	{ }
81
};
82
MODULE_DEVICE_TABLE(i2c, pca953x_id);
83

84
static const struct acpi_device_id pca953x_acpi_ids[] = {
85
	{ "INT3491", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
86 87 88 89
	{ }
};
MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);

90 91 92 93 94
#define MAX_BANK 5
#define BANK_SZ 8

#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)

95
struct pca953x_chip {
96
	unsigned gpio_start;
97 98
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
99
	struct mutex i2c_lock;
100

101 102
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
103 104 105 106
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
107 108
#endif

109 110
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
111
	const char *const *names;
112
	int	chip_type;
113
	unsigned long driver_data;
114 115
};

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
				int off)
{
	int ret;
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
	int offset = off / BANK_SZ;

	ret = i2c_smbus_read_byte_data(chip->client,
				(reg << bank_shift) + offset);
	*val = ret;

	if (ret < 0) {
		dev_err(&chip->client->dev, "failed reading register\n");
		return ret;
	}

	return 0;
}

static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
				int off)
{
138
	int ret;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
	int offset = off / BANK_SZ;

	ret = i2c_smbus_write_byte_data(chip->client,
					(reg << bank_shift) + offset, val);

	if (ret < 0) {
		dev_err(&chip->client->dev, "failed writing register\n");
		return ret;
	}

	return 0;
}

static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
154
{
155
	int ret = 0;
156 157

	if (chip->gpio_chip.ngpio <= 8)
158 159 160
		ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
	else if (chip->gpio_chip.ngpio >= 24) {
		int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
161
		ret = i2c_smbus_write_i2c_block_data(chip->client,
162 163
					(reg << bank_shift) | REG_ADDR_AI,
					NBANK(chip), val);
164
	} else {
165
		switch (chip->chip_type) {
166 167 168 169 170
		case PCA953X_TYPE: {
			__le16 word = cpu_to_le16(get_unaligned((u16 *)val));

			ret = i2c_smbus_write_word_data(chip->client, reg << 1,
							(__force u16)word);
171
			break;
172
		}
173 174
		case PCA957X_TYPE:
			ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
175
							val[0]);
176 177 178 179
			if (ret < 0)
				break;
			ret = i2c_smbus_write_byte_data(chip->client,
							(reg << 1) + 1,
180
							val[1]);
181 182 183
			break;
		}
	}
184 185 186

	if (ret < 0) {
		dev_err(&chip->client->dev, "failed writing register\n");
187
		return ret;
188 189 190
	}

	return 0;
191 192
}

193
static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
194 195 196
{
	int ret;

197
	if (chip->gpio_chip.ngpio <= 8) {
198
		ret = i2c_smbus_read_byte_data(chip->client, reg);
199
		*val = ret;
200 201 202
	} else if (chip->gpio_chip.ngpio >= 24) {
		int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);

203
		ret = i2c_smbus_read_i2c_block_data(chip->client,
204 205
					(reg << bank_shift) | REG_ADDR_AI,
					NBANK(chip), val);
206
	} else {
207
		ret = i2c_smbus_read_word_data(chip->client, reg << 1);
208 209
		val[0] = (u16)ret & 0xFF;
		val[1] = (u16)ret >> 8;
210
	}
211 212
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed reading register\n");
213
		return ret;
214 215 216 217 218
	}

	return 0;
}

219
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
220
{
221
	struct pca953x_chip *chip = gpiochip_get_data(gc);
222
	u8 reg_val;
223
	int ret, offset = 0;
224

225
	mutex_lock(&chip->i2c_lock);
226
	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
227 228 229 230 231 232 233 234 235

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_DIRECTION;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_CFG;
		break;
	}
236
	ret = pca953x_write_single(chip, offset, reg_val, off);
237
	if (ret)
238
		goto exit;
239

240
	chip->reg_direction[off / BANK_SZ] = reg_val;
241 242 243
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
244 245
}

246
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
247 248
		unsigned off, int val)
{
249
	struct pca953x_chip *chip = gpiochip_get_data(gc);
250
	u8 reg_val;
251
	int ret, offset = 0;
252

253
	mutex_lock(&chip->i2c_lock);
254 255
	/* set output level */
	if (val)
256 257
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
258
	else
259 260
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
261

262 263 264 265 266 267 268 269
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}
270
	ret = pca953x_write_single(chip, offset, reg_val, off);
271
	if (ret)
272
		goto exit;
273

274
	chip->reg_output[off / BANK_SZ] = reg_val;
275 276

	/* then direction */
277
	reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
278 279 280 281 282 283 284 285
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_DIRECTION;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_CFG;
		break;
	}
286
	ret = pca953x_write_single(chip, offset, reg_val, off);
287
	if (ret)
288
		goto exit;
289

290
	chip->reg_direction[off / BANK_SZ] = reg_val;
291 292 293
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
294 295
}

296
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
297
{
298
	struct pca953x_chip *chip = gpiochip_get_data(gc);
A
Andreas Schallenberg 已提交
299
	u32 reg_val;
300
	int ret, offset = 0;
301

302
	mutex_lock(&chip->i2c_lock);
303 304 305 306 307 308 309 310
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
311
	ret = pca953x_read_single(chip, offset, &reg_val, off);
312
	mutex_unlock(&chip->i2c_lock);
313 314 315 316 317 318 319 320
	if (ret < 0) {
		/* NOTE:  diagnostic already emitted; that's all we should
		 * do unless gpio_*_value_cansleep() calls become different
		 * from their nonsleeping siblings (and report faults).
		 */
		return 0;
	}

321
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
322 323
}

324
static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
325
{
326
	struct pca953x_chip *chip = gpiochip_get_data(gc);
327
	u8 reg_val;
328
	int ret, offset = 0;
329

330
	mutex_lock(&chip->i2c_lock);
331
	if (val)
332 333
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
334
	else
335 336
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
337

338 339 340 341 342 343 344 345
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}
346
	ret = pca953x_write_single(chip, offset, reg_val, off);
347
	if (ret)
348
		goto exit;
349

350
	chip->reg_output[off / BANK_SZ] = reg_val;
351 352
exit:
	mutex_unlock(&chip->i2c_lock);
353 354
}

355 356 357
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
		unsigned long *mask, unsigned long *bits)
{
358
	struct pca953x_chip *chip = gpiochip_get_data(gc);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	u8 reg_val[MAX_BANK];
	int ret, offset = 0;
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
	int bank;

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}

	memcpy(reg_val, chip->reg_output, NBANK(chip));
	mutex_lock(&chip->i2c_lock);
	for(bank=0; bank<NBANK(chip); bank++) {
376 377
		unsigned bankmask = mask[bank / sizeof(*mask)] >>
				    ((bank % sizeof(*mask)) * 8);
378
		if(bankmask) {
379 380
			unsigned bankval  = bits[bank / sizeof(*bits)] >>
					    ((bank % sizeof(*bits)) * 8);
381 382 383 384 385 386 387 388 389 390 391 392
			reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
		}
	}
	ret = i2c_smbus_write_i2c_block_data(chip->client, offset << bank_shift, NBANK(chip), reg_val);
	if (ret)
		goto exit;

	memcpy(chip->reg_output, reg_val, NBANK(chip));
exit:
	mutex_unlock(&chip->i2c_lock);
}

393
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
394 395 396 397 398
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

399 400 401 402
	gc->direction_input  = pca953x_gpio_direction_input;
	gc->direction_output = pca953x_gpio_direction_output;
	gc->get = pca953x_gpio_get_value;
	gc->set = pca953x_gpio_set_value;
403
	gc->set_multiple = pca953x_gpio_set_multiple;
404
	gc->can_sleep = true;
405 406

	gc->base = chip->gpio_start;
407 408
	gc->ngpio = gpios;
	gc->label = chip->client->name;
409
	gc->parent = &chip->client->dev;
410
	gc->owner = THIS_MODULE;
411
	gc->names = chip->names;
412 413
}

414
#ifdef CONFIG_GPIO_PCA953X_IRQ
415
static void pca953x_irq_mask(struct irq_data *d)
416
{
417
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
418
	struct pca953x_chip *chip = gpiochip_get_data(gc);
419

420
	chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
421 422
}

423
static void pca953x_irq_unmask(struct irq_data *d)
424
{
425
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
426
	struct pca953x_chip *chip = gpiochip_get_data(gc);
427

428
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
429 430
}

431
static void pca953x_irq_bus_lock(struct irq_data *d)
432
{
433
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
434
	struct pca953x_chip *chip = gpiochip_get_data(gc);
435 436 437 438

	mutex_lock(&chip->irq_lock);
}

439
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
440
{
441
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
442
	struct pca953x_chip *chip = gpiochip_get_data(gc);
443 444
	u8 new_irqs;
	int level, i;
445 446 447 448 449 450 451 452 453 454 455 456
	u8 invert_irq_mask[MAX_BANK];

	if (chip->driver_data & PCA_PCAL) {
		/* Enable latch on interrupt-enabled inputs */
		pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);

		for (i = 0; i < NBANK(chip); i++)
			invert_irq_mask[i] = ~chip->irq_mask[i];

		/* Unmask enabled interrupts */
		pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
	}
457 458

	/* Look for any newly setup interrupt */
459 460 461 462 463 464 465 466 467 468
	for (i = 0; i < NBANK(chip); i++) {
		new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
		new_irqs &= ~chip->reg_direction[i];

		while (new_irqs) {
			level = __ffs(new_irqs);
			pca953x_gpio_direction_input(&chip->gpio_chip,
							level + (BANK_SZ * i));
			new_irqs &= ~(1 << level);
		}
469
	}
470 471 472 473

	mutex_unlock(&chip->irq_lock);
}

474
static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
475
{
476
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
477
	struct pca953x_chip *chip = gpiochip_get_data(gc);
478 479
	int bank_nb = d->hwirq / BANK_SZ;
	u8 mask = 1 << (d->hwirq % BANK_SZ);
480 481 482

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
483
			d->irq, type);
484 485 486 487
		return -EINVAL;
	}

	if (type & IRQ_TYPE_EDGE_FALLING)
488
		chip->irq_trig_fall[bank_nb] |= mask;
489
	else
490
		chip->irq_trig_fall[bank_nb] &= ~mask;
491 492

	if (type & IRQ_TYPE_EDGE_RISING)
493
		chip->irq_trig_raise[bank_nb] |= mask;
494
	else
495
		chip->irq_trig_raise[bank_nb] &= ~mask;
496

497
	return 0;
498 499 500 501
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
502 503 504 505 506
	.irq_mask		= pca953x_irq_mask,
	.irq_unmask		= pca953x_irq_unmask,
	.irq_bus_lock		= pca953x_irq_bus_lock,
	.irq_bus_sync_unlock	= pca953x_irq_bus_sync_unlock,
	.irq_set_type		= pca953x_irq_set_type,
507 508
};

509
static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
510
{
511 512
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
513 514 515
	bool pending_seen = false;
	bool trigger_seen = false;
	u8 trigger[MAX_BANK];
516
	int ret, i, offset = 0;
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	if (chip->driver_data & PCA_PCAL) {
		/* Read the current interrupt status from the device */
		ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
		if (ret)
			return false;

		/* Check latched inputs and clear interrupt status */
		ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
		if (ret)
			return false;

		for (i = 0; i < NBANK(chip); i++) {
			/* Apply filter for rising/falling edge selection */
			pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
				(cur_stat[i] & chip->irq_trig_raise[i]);
			pending[i] &= trigger[i];
			if (pending[i])
				pending_seen = true;
		}

		return pending_seen;
	}

541 542 543 544 545 546 547 548
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
549
	ret = pca953x_read_regs(chip, offset, cur_stat);
550
	if (ret)
551
		return false;
552 553

	/* Remove output pins from the equation */
554 555
	for (i = 0; i < NBANK(chip); i++)
		cur_stat[i] &= chip->reg_direction[i];
556

557
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
558

559 560
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
561 562
		if (trigger[i])
			trigger_seen = true;
563 564
	}

565 566
	if (!trigger_seen)
		return false;
567

568
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
569

570 571 572 573
	for (i = 0; i < NBANK(chip); i++) {
		pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
			(cur_stat[i] & chip->irq_trig_raise[i]);
		pending[i] &= trigger[i];
574 575
		if (pending[i])
			pending_seen = true;
576
	}
577

578
	return pending_seen;
579 580 581 582 583
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
584 585
	u8 pending[MAX_BANK];
	u8 level;
586
	unsigned nhandled = 0;
587
	int i;
588

589
	if (!pca953x_irq_pending(chip, pending))
590
		return IRQ_NONE;
591

592 593 594
	for (i = 0; i < NBANK(chip); i++) {
		while (pending[i]) {
			level = __ffs(pending[i]);
595
			handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
596 597
							level + (BANK_SZ * i)));
			pending[i] &= ~(1 << level);
598
			nhandled++;
599 600
		}
	}
601

602
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
603 604 605
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
606
			     int irq_base)
607 608
{
	struct i2c_client *client = chip->client;
609
	int ret, i, offset = 0;
610

611
	if (client->irq && irq_base != -1
612
			&& (chip->driver_data & PCA_INT)) {
613

614 615 616 617 618 619 620 621
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			offset = PCA953X_INPUT;
			break;
		case PCA957X_TYPE:
			offset = PCA957X_IN;
			break;
		}
622
		ret = pca953x_read_regs(chip, offset, chip->irq_stat);
623
		if (ret)
624
			return ret;
625 626 627 628 629 630

		/*
		 * There is no way to know which GPIO line generated the
		 * interrupt.  We have to rely on the previous read for
		 * this purpose.
		 */
631 632
		for (i = 0; i < NBANK(chip); i++)
			chip->irq_stat[i] &= chip->reg_direction[i];
633 634
		mutex_init(&chip->irq_lock);

635 636
		ret = devm_request_threaded_irq(&client->dev,
					client->irq,
637 638
					   NULL,
					   pca953x_irq_handler,
639 640
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT |
						   IRQF_SHARED,
641 642 643 644
					   dev_name(&client->dev), chip);
		if (ret) {
			dev_err(&client->dev, "failed to request irq %d\n",
				client->irq);
645
			return ret;
646 647
		}

648 649 650 651 652 653 654 655 656 657
		ret =  gpiochip_irqchip_add(&chip->gpio_chip,
					    &pca953x_irq_chip,
					    irq_base,
					    handle_simple_irq,
					    IRQ_TYPE_NONE);
		if (ret) {
			dev_err(&client->dev,
				"could not connect irqchip to gpiochip\n");
			return ret;
		}
658 659 660 661

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
662 663 664 665 666 667 668
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
669
			     int irq_base)
670 671 672
{
	struct i2c_client *client = chip->client;

673
	if (irq_base != -1 && (chip->driver_data & PCA_INT))
674 675 676 677 678 679
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
680
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
681 682
{
	int ret;
683
	u8 val[MAX_BANK];
684

685
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
686 687 688
	if (ret)
		goto out;

689 690
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
691 692 693 694
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
695 696 697 698 699 700
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
701 702 703 704
out:
	return ret;
}

B
Bill Pemberton 已提交
705
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
706 707
{
	int ret;
708
	u8 val[MAX_BANK];
709

710
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
711 712
	if (ret)
		goto out;
713
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
714 715 716 717
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
718 719 720 721
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
722 723 724
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
725

726
	/* To enable register 6, 7 to control pull up and pull down */
727
	memset(val, 0x02, NBANK(chip));
728 729 730
	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
	if (ret)
		goto out;
731 732 733 734 735 736

	return 0;
out:
	return ret;
}

737 738
static const struct of_device_id pca953x_dt_ids[];

B
Bill Pemberton 已提交
739
static int pca953x_probe(struct i2c_client *client,
740
				   const struct i2c_device_id *id)
741
{
742 743
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
744
	int irq_base = 0;
745
	int ret;
746
	u32 invert = 0;
747

748 749
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
750 751 752
	if (chip == NULL)
		return -ENOMEM;

J
Jingoo Han 已提交
753
	pdata = dev_get_platdata(&client->dev);
754 755 756 757 758 759
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
760 761
		chip->gpio_start = -1;
		irq_base = 0;
762
	}
763 764 765

	chip->client = client;

766 767 768 769
	if (id) {
		chip->driver_data = id->driver_data;
	} else {
		const struct acpi_device_id *id;
770
		const struct of_device_id *match;
771

772 773 774 775 776 777 778
		match = of_match_device(pca953x_dt_ids, &client->dev);
		if (match) {
			chip->driver_data = (int)(uintptr_t)match->data;
		} else {
			id = acpi_match_device(pca953x_acpi_ids, &client->dev);
			if (!id)
				return -ENODEV;
779

780 781
			chip->driver_data = id->driver_data;
		}
782 783
	}

784
	chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
785

786 787
	mutex_init(&chip->i2c_lock);

788 789 790
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
791
	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
792

793
	if (chip->chip_type == PCA953X_TYPE)
794
		ret = device_pca953x_init(chip, invert);
795
	else
796 797
		ret = device_pca957x_init(chip, invert);
	if (ret)
798
		return ret;
799

800
	ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
801
	if (ret)
802
		return ret;
803

804
	ret = pca953x_irq_setup(chip, irq_base);
805
	if (ret)
806
		return ret;
807

808
	if (pdata && pdata->setup) {
809 810 811 812 813 814 815 816 817 818
		ret = pdata->setup(client, chip->gpio_chip.base,
				chip->gpio_chip.ngpio, pdata->context);
		if (ret < 0)
			dev_warn(&client->dev, "setup failed, %d\n", ret);
	}

	i2c_set_clientdata(client, chip);
	return 0;
}

819
static int pca953x_remove(struct i2c_client *client)
820
{
J
Jingoo Han 已提交
821
	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
822
	struct pca953x_chip *chip = i2c_get_clientdata(client);
823
	int ret;
824

825
	if (pdata && pdata->teardown) {
826 827 828 829 830 831 832 833 834 835 836 837
		ret = pdata->teardown(client, chip->gpio_chip.base,
				chip->gpio_chip.ngpio, pdata->context);
		if (ret < 0) {
			dev_err(&client->dev, "%s failed, %d\n",
					"teardown", ret);
			return ret;
		}
	}

	return 0;
}

838 839 840 841
/* convenience to stop overlong match-table lines */
#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)

842
static const struct of_device_id pca953x_dt_ids[] = {
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
	{ .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
	{ .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
	{ .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
	{ .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
	{ .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
	{ .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
	{ .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
	{ .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
	{ .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
	{ .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
	{ .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },

	{ .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
	{ .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },

	{ .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
864
	{ .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
865 866 867 868 869 870 871
	{ .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
	{ .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
	{ .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },

	{ .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), },

	{ .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
872 873 874 875 876
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

877
static struct i2c_driver pca953x_driver = {
878
	.driver = {
879
		.name	= "pca953x",
880
		.of_match_table = pca953x_dt_ids,
881
		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
882
	},
883 884
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
885
	.id_table	= pca953x_id,
886 887
};

888
static int __init pca953x_init(void)
889
{
890
	return i2c_add_driver(&pca953x_driver);
891
}
892 893 894 895
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
896

897
static void __exit pca953x_exit(void)
898
{
899
	i2c_del_driver(&pca953x_driver);
900
}
901
module_exit(pca953x_exit);
902 903

MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
904
MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
905
MODULE_LICENSE("GPL");