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 166 167
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			ret = i2c_smbus_write_word_data(chip->client,
168
			    reg << 1, cpu_to_le16(get_unaligned((u16 *)val)));
169 170 171
			break;
		case PCA957X_TYPE:
			ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
172
							val[0]);
173 174 175 176
			if (ret < 0)
				break;
			ret = i2c_smbus_write_byte_data(chip->client,
							(reg << 1) + 1,
177
							val[1]);
178 179 180
			break;
		}
	}
181 182 183

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

	return 0;
188 189
}

190
static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
191 192 193
{
	int ret;

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

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

	return 0;
}

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

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

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

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

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

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

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

271
	chip->reg_output[off / BANK_SZ] = reg_val;
272 273

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

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

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

299
	mutex_lock(&chip->i2c_lock);
300 301 302 303 304 305 306 307
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
308
	ret = pca953x_read_single(chip, offset, &reg_val, off);
309
	mutex_unlock(&chip->i2c_lock);
310 311 312 313 314 315 316 317
	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;
	}

318
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
319 320
}

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

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

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

347
	chip->reg_output[off / BANK_SZ] = reg_val;
348 349
exit:
	mutex_unlock(&chip->i2c_lock);
350 351
}

352 353 354
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
		unsigned long *mask, unsigned long *bits)
{
355
	struct pca953x_chip *chip = gpiochip_get_data(gc);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	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++) {
373 374
		unsigned bankmask = mask[bank / sizeof(*mask)] >>
				    ((bank % sizeof(*mask)) * 8);
375
		if(bankmask) {
376 377
			unsigned bankval  = bits[bank / sizeof(*bits)] >>
					    ((bank % sizeof(*bits)) * 8);
378 379 380 381 382 383 384 385 386 387 388 389
			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);
}

390
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
391 392 393 394 395
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

396 397 398 399
	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;
400
	gc->set_multiple = pca953x_gpio_set_multiple;
401
	gc->can_sleep = true;
402 403

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

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

417
	chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
418 419
}

420
static void pca953x_irq_unmask(struct irq_data *d)
421
{
422
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
423
	struct pca953x_chip *chip = gpiochip_get_data(gc);
424

425
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
426 427
}

428
static void pca953x_irq_bus_lock(struct irq_data *d)
429
{
430
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
431
	struct pca953x_chip *chip = gpiochip_get_data(gc);
432 433 434 435

	mutex_lock(&chip->irq_lock);
}

436
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
437
{
438
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
439
	struct pca953x_chip *chip = gpiochip_get_data(gc);
440 441
	u8 new_irqs;
	int level, i;
442 443 444 445 446 447 448 449 450 451 452 453
	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);
	}
454 455

	/* Look for any newly setup interrupt */
456 457 458 459 460 461 462 463 464 465
	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);
		}
466
	}
467 468 469 470

	mutex_unlock(&chip->irq_lock);
}

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

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

	if (type & IRQ_TYPE_EDGE_FALLING)
485
		chip->irq_trig_fall[bank_nb] |= mask;
486
	else
487
		chip->irq_trig_fall[bank_nb] &= ~mask;
488 489

	if (type & IRQ_TYPE_EDGE_RISING)
490
		chip->irq_trig_raise[bank_nb] |= mask;
491
	else
492
		chip->irq_trig_raise[bank_nb] &= ~mask;
493

494
	return 0;
495 496 497 498
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
499 500 501 502 503
	.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,
504 505
};

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

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	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;
	}

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

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

554
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
555

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

562 563
	if (!trigger_seen)
		return false;
564

565
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
566

567 568 569 570
	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];
571 572
		if (pending[i])
			pending_seen = true;
573
	}
574

575
	return pending_seen;
576 577 578 579 580
}

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

586
	if (!pca953x_irq_pending(chip, pending))
587
		return IRQ_NONE;
588

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

599
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
600 601 602
}

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

608
	if (client->irq && irq_base != -1
609
			&& (chip->driver_data & PCA_INT)) {
610

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

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

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

645 646 647 648 649 650 651 652 653 654
		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;
		}
655 656 657 658

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
659 660 661 662 663 664 665
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
666
			     int irq_base)
667 668 669
{
	struct i2c_client *client = chip->client;

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

	return 0;
}
#endif

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

682
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
683 684 685
	if (ret)
		goto out;

686 687
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
688 689 690 691
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
692 693 694 695 696 697
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
698 699 700 701
out:
	return ret;
}

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

707
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
708 709
	if (ret)
		goto out;
710
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
711 712 713 714
	if (ret)
		goto out;

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

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

	return 0;
out:
	return ret;
}

734 735
static const struct of_device_id pca953x_dt_ids[];

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

745 746
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
747 748 749
	if (chip == NULL)
		return -ENOMEM;

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

	chip->client = client;

763 764 765 766
	if (id) {
		chip->driver_data = id->driver_data;
	} else {
		const struct acpi_device_id *id;
767
		const struct of_device_id *match;
768

769 770 771 772 773 774 775
		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;
776

777 778
			chip->driver_data = id->driver_data;
		}
779 780
	}

781
	chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
782

783 784
	mutex_init(&chip->i2c_lock);

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

790
	if (chip->chip_type == PCA953X_TYPE)
791
		ret = device_pca953x_init(chip, invert);
792
	else
793 794
		ret = device_pca957x_init(chip, invert);
	if (ret)
795
		return ret;
796

797
	ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
798
	if (ret)
799
		return ret;
800

801
	ret = pca953x_irq_setup(chip, irq_base);
802
	if (ret)
803
		return ret;
804

805
	if (pdata && pdata->setup) {
806 807 808 809 810 811 812 813 814 815
		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;
}

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

822
	if (pdata && pdata->teardown) {
823 824 825 826 827 828 829 830 831 832 833 834
		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;
}

835 836 837 838
/* 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)

839
static const struct of_device_id pca953x_dt_ids[] = {
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	{ .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), },
861
	{ .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
862 863 864 865 866 867 868
	{ .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), },
869 870 871 872 873
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

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

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

894
static void __exit pca953x_exit(void)
895
{
896
	i2c_del_driver(&pca953x_driver);
897
}
898
module_exit(pca953x_exit);
899 900

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