gpio-pca953x.c 18.4 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 22 23
#ifdef CONFIG_OF_GPIO
#include <linux/of_platform.h>
#endif
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 41 42 43 44
#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

#define PCA_GPIO_MASK		0x00FF
#define PCA_INT			0x0100
#define PCA953X_TYPE		0x1000
#define PCA957X_TYPE		0x2000
45

46
static const struct i2c_device_id pca953x_id[] = {
47
	{ "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
48 49 50 51 52 53 54 55 56 57 58 59
	{ "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 已提交
60
	{ "pca9698", 40 | PCA953X_TYPE, },
61 62 63 64 65 66 67 68

	{ "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 已提交
69
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
A
Aaron Sierra 已提交
70
	{ "xra1202", 8  | PCA953X_TYPE },
71
	{ }
72
};
73
MODULE_DEVICE_TABLE(i2c, pca953x_id);
74

75 76 77 78 79
#define MAX_BANK 5
#define BANK_SZ 8

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

80
struct pca953x_chip {
81
	unsigned gpio_start;
82 83
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
84
	struct mutex i2c_lock;
85

86 87
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
88 89 90 91
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
92 93
#endif

94 95
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
96
	const char *const *names;
97
	int	chip_type;
98 99
};

100 101 102 103 104
static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
{
	return container_of(gc, struct pca953x_chip, gpio_chip);
}

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 133 134 135 136 137 138 139 140 141 142
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)
{
	int ret = 0;
	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)
143
{
144
	int ret = 0;
145 146

	if (chip->gpio_chip.ngpio <= 8)
147 148 149
		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);
150
		ret = i2c_smbus_write_i2c_block_data(chip->client,
151 152
					(reg << bank_shift) | REG_ADDR_AI,
					NBANK(chip), val);
153
	} else {
154 155 156
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			ret = i2c_smbus_write_word_data(chip->client,
157
							reg << 1, (u16) *val);
158 159 160
			break;
		case PCA957X_TYPE:
			ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
161
							val[0]);
162 163 164 165
			if (ret < 0)
				break;
			ret = i2c_smbus_write_byte_data(chip->client,
							(reg << 1) + 1,
166
							val[1]);
167 168 169
			break;
		}
	}
170 171 172

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

	return 0;
177 178
}

179
static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
180 181 182
{
	int ret;

183
	if (chip->gpio_chip.ngpio <= 8) {
184
		ret = i2c_smbus_read_byte_data(chip->client, reg);
185
		*val = ret;
186 187 188
	} else if (chip->gpio_chip.ngpio >= 24) {
		int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);

189
		ret = i2c_smbus_read_i2c_block_data(chip->client,
190 191
					(reg << bank_shift) | REG_ADDR_AI,
					NBANK(chip), val);
192
	} else {
193
		ret = i2c_smbus_read_word_data(chip->client, reg << 1);
194 195
		val[0] = (u16)ret & 0xFF;
		val[1] = (u16)ret >> 8;
196
	}
197 198
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed reading register\n");
199
		return ret;
200 201 202 203 204
	}

	return 0;
}

205
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
206
{
207
	struct pca953x_chip *chip = to_pca(gc);
208
	u8 reg_val;
209
	int ret, offset = 0;
210

211
	mutex_lock(&chip->i2c_lock);
212
	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
213 214 215 216 217 218 219 220 221

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_DIRECTION;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_CFG;
		break;
	}
222
	ret = pca953x_write_single(chip, offset, reg_val, off);
223
	if (ret)
224
		goto exit;
225

226
	chip->reg_direction[off / BANK_SZ] = reg_val;
227 228 229 230
	ret = 0;
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
231 232
}

233
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
234 235
		unsigned off, int val)
{
236
	struct pca953x_chip *chip = to_pca(gc);
237
	u8 reg_val;
238
	int ret, offset = 0;
239

240
	mutex_lock(&chip->i2c_lock);
241 242
	/* set output level */
	if (val)
243 244
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
245
	else
246 247
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
248

249 250 251 252 253 254 255 256
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}
257
	ret = pca953x_write_single(chip, offset, reg_val, off);
258
	if (ret)
259
		goto exit;
260

261
	chip->reg_output[off / BANK_SZ] = reg_val;
262 263

	/* then direction */
264
	reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
265 266 267 268 269 270 271 272
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_DIRECTION;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_CFG;
		break;
	}
273
	ret = pca953x_write_single(chip, offset, reg_val, off);
274
	if (ret)
275
		goto exit;
276

277
	chip->reg_direction[off / BANK_SZ] = reg_val;
278 279 280 281
	ret = 0;
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
282 283
}

284
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
285
{
286
	struct pca953x_chip *chip = to_pca(gc);
A
Andreas Schallenberg 已提交
287
	u32 reg_val;
288
	int ret, offset = 0;
289

290
	mutex_lock(&chip->i2c_lock);
291 292 293 294 295 296 297 298
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
299
	ret = pca953x_read_single(chip, offset, &reg_val, off);
300
	mutex_unlock(&chip->i2c_lock);
301 302 303 304 305 306 307 308
	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;
	}

309
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
310 311
}

312
static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
313
{
314
	struct pca953x_chip *chip = to_pca(gc);
315
	u8 reg_val;
316
	int ret, offset = 0;
317

318
	mutex_lock(&chip->i2c_lock);
319
	if (val)
320 321
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
322
	else
323 324
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
325

326 327 328 329 330 331 332 333
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}
334
	ret = pca953x_write_single(chip, offset, reg_val, off);
335
	if (ret)
336
		goto exit;
337

338
	chip->reg_output[off / BANK_SZ] = reg_val;
339 340
exit:
	mutex_unlock(&chip->i2c_lock);
341 342
}

343
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
344 345 346 347 348
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

349 350 351 352
	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;
353
	gc->can_sleep = true;
354 355

	gc->base = chip->gpio_start;
356 357
	gc->ngpio = gpios;
	gc->label = chip->client->name;
D
David Brownell 已提交
358
	gc->dev = &chip->client->dev;
359
	gc->owner = THIS_MODULE;
360
	gc->names = chip->names;
361 362
}

363
#ifdef CONFIG_GPIO_PCA953X_IRQ
364
static void pca953x_irq_mask(struct irq_data *d)
365
{
366 367
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
368

369
	chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
370 371
}

372
static void pca953x_irq_unmask(struct irq_data *d)
373
{
374 375
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
376

377
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
378 379
}

380
static void pca953x_irq_bus_lock(struct irq_data *d)
381
{
382 383
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
384 385 386 387

	mutex_lock(&chip->irq_lock);
}

388
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
389
{
390 391
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
392 393
	u8 new_irqs;
	int level, i;
394 395

	/* Look for any newly setup interrupt */
396 397 398 399 400 401 402 403 404 405
	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);
		}
406
	}
407 408 409 410

	mutex_unlock(&chip->irq_lock);
}

411
static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
412
{
413 414
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
415 416
	int bank_nb = d->hwirq / BANK_SZ;
	u8 mask = 1 << (d->hwirq % BANK_SZ);
417 418 419

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
420
			d->irq, type);
421 422 423 424
		return -EINVAL;
	}

	if (type & IRQ_TYPE_EDGE_FALLING)
425
		chip->irq_trig_fall[bank_nb] |= mask;
426
	else
427
		chip->irq_trig_fall[bank_nb] &= ~mask;
428 429

	if (type & IRQ_TYPE_EDGE_RISING)
430
		chip->irq_trig_raise[bank_nb] |= mask;
431
	else
432
		chip->irq_trig_raise[bank_nb] &= ~mask;
433

434
	return 0;
435 436 437 438
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
439 440 441 442 443
	.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,
444 445
};

446
static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
447
{
448 449
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
450 451 452
	bool pending_seen = false;
	bool trigger_seen = false;
	u8 trigger[MAX_BANK];
453
	int ret, i, offset = 0;
454 455 456 457 458 459 460 461 462

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
463
	ret = pca953x_read_regs(chip, offset, cur_stat);
464
	if (ret)
465
		return false;
466 467

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

471
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
472

473 474
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
475 476
		if (trigger[i])
			trigger_seen = true;
477 478
	}

479 480
	if (!trigger_seen)
		return false;
481

482
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
483

484 485 486 487
	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];
488 489
		if (pending[i])
			pending_seen = true;
490
	}
491

492
	return pending_seen;
493 494 495 496 497
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
498 499
	u8 pending[MAX_BANK];
	u8 level;
500
	unsigned nhandled = 0;
501
	int i;
502

503
	if (!pca953x_irq_pending(chip, pending))
504
		return IRQ_NONE;
505

506 507 508
	for (i = 0; i < NBANK(chip); i++) {
		while (pending[i]) {
			level = __ffs(pending[i]);
509
			handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
510 511
							level + (BANK_SZ * i)));
			pending[i] &= ~(1 << level);
512
			nhandled++;
513 514
		}
	}
515

516
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
517 518 519
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
520 521
			     const struct i2c_device_id *id,
			     int irq_base)
522 523
{
	struct i2c_client *client = chip->client;
524
	int ret, i, offset = 0;
525

526
	if (client->irq && irq_base != -1
527
			&& (id->driver_data & PCA_INT)) {
528

529 530 531 532 533 534 535 536
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			offset = PCA953X_INPUT;
			break;
		case PCA957X_TYPE:
			offset = PCA957X_IN;
			break;
		}
537
		ret = pca953x_read_regs(chip, offset, chip->irq_stat);
538
		if (ret)
539
			return ret;
540 541 542 543 544 545

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

550 551
		ret = devm_request_threaded_irq(&client->dev,
					client->irq,
552 553
					   NULL,
					   pca953x_irq_handler,
554 555
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT |
						   IRQF_SHARED,
556 557 558 559
					   dev_name(&client->dev), chip);
		if (ret) {
			dev_err(&client->dev, "failed to request irq %d\n",
				client->irq);
560
			return ret;
561 562
		}

563 564 565 566 567 568 569 570 571 572
		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;
		}
573 574 575 576

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
577 578 579 580 581 582 583
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
584 585
			     const struct i2c_device_id *id,
			     int irq_base)
586 587 588
{
	struct i2c_client *client = chip->client;

589
	if (irq_base != -1 && (id->driver_data & PCA_INT))
590 591 592 593 594 595
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
596
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
597 598
{
	int ret;
599
	u8 val[MAX_BANK];
600

601
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
602 603 604
	if (ret)
		goto out;

605 606
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
607 608 609 610
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
611 612 613 614 615 616
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
617 618 619 620
out:
	return ret;
}

B
Bill Pemberton 已提交
621
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
622 623
{
	int ret;
624
	u8 val[MAX_BANK];
625

626
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
627 628
	if (ret)
		goto out;
629
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
630 631 632 633
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
634 635 636 637
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
638 639 640
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
641

642
	/* To enable register 6, 7 to control pull up and pull down */
643
	memset(val, 0x02, NBANK(chip));
644 645 646
	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
	if (ret)
		goto out;
647 648 649 650 651 652

	return 0;
out:
	return ret;
}

B
Bill Pemberton 已提交
653
static int pca953x_probe(struct i2c_client *client,
654
				   const struct i2c_device_id *id)
655
{
656 657
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
658
	int irq_base = 0;
659
	int ret;
660
	u32 invert = 0;
661

662 663
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
664 665 666
	if (chip == NULL)
		return -ENOMEM;

J
Jingoo Han 已提交
667
	pdata = dev_get_platdata(&client->dev);
668 669 670 671 672 673
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
674 675
		chip->gpio_start = -1;
		irq_base = 0;
676
	}
677 678 679

	chip->client = client;

680
	chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
681

682 683
	mutex_init(&chip->i2c_lock);

684 685 686
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
687
	pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
688

689
	if (chip->chip_type == PCA953X_TYPE)
690
		ret = device_pca953x_init(chip, invert);
691
	else
692 693
		ret = device_pca957x_init(chip, invert);
	if (ret)
694
		return ret;
695

696
	ret = gpiochip_add(&chip->gpio_chip);
697
	if (ret)
698
		return ret;
699

700
	ret = pca953x_irq_setup(chip, id, irq_base);
701
	if (ret)
702
		return ret;
703

704
	if (pdata && pdata->setup) {
705 706 707 708 709 710 711 712 713 714
		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;
}

715
static int pca953x_remove(struct i2c_client *client)
716
{
J
Jingoo Han 已提交
717
	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
718
	struct pca953x_chip *chip = i2c_get_clientdata(client);
719 720
	int ret = 0;

721
	if (pdata && pdata->teardown) {
722 723 724 725 726 727 728 729 730
		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;
		}
	}

731
	gpiochip_remove(&chip->gpio_chip);
732 733 734 735

	return 0;
}

736
static const struct of_device_id pca953x_dt_ids[] = {
737
	{ .compatible = "nxp,pca9505", },
738 739 740 741 742 743 744 745 746 747 748 749
	{ .compatible = "nxp,pca9534", },
	{ .compatible = "nxp,pca9535", },
	{ .compatible = "nxp,pca9536", },
	{ .compatible = "nxp,pca9537", },
	{ .compatible = "nxp,pca9538", },
	{ .compatible = "nxp,pca9539", },
	{ .compatible = "nxp,pca9554", },
	{ .compatible = "nxp,pca9555", },
	{ .compatible = "nxp,pca9556", },
	{ .compatible = "nxp,pca9557", },
	{ .compatible = "nxp,pca9574", },
	{ .compatible = "nxp,pca9575", },
A
Aaron Sierra 已提交
750
	{ .compatible = "nxp,pca9698", },
751 752 753 754 755 756 757 758 759 760

	{ .compatible = "maxim,max7310", },
	{ .compatible = "maxim,max7312", },
	{ .compatible = "maxim,max7313", },
	{ .compatible = "maxim,max7315", },

	{ .compatible = "ti,pca6107", },
	{ .compatible = "ti,tca6408", },
	{ .compatible = "ti,tca6416", },
	{ .compatible = "ti,tca6424", },
A
Aaron Sierra 已提交
761 762

	{ .compatible = "exar,xra1202", },
763 764 765 766 767
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

768
static struct i2c_driver pca953x_driver = {
769
	.driver = {
770
		.name	= "pca953x",
771
		.of_match_table = pca953x_dt_ids,
772
	},
773 774
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
775
	.id_table	= pca953x_id,
776 777
};

778
static int __init pca953x_init(void)
779
{
780
	return i2c_add_driver(&pca953x_driver);
781
}
782 783 784 785
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
786

787
static void __exit pca953x_exit(void)
788
{
789
	i2c_del_driver(&pca953x_driver);
790
}
791
module_exit(pca953x_exit);
792 793

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