gpio-pca953x.c 19.0 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
#include <linux/acpi.h>
25

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

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

33 34 35 36 37 38 39 40 41 42 43 44 45
#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
46 47 48
#define PCA_TYPE_MASK		0xF000

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

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

	{ "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 已提交
73
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
74
	{ "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
A
Aaron Sierra 已提交
75
	{ "xra1202", 8  | PCA953X_TYPE },
76
	{ }
77
};
78
MODULE_DEVICE_TABLE(i2c, pca953x_id);
79

80 81 82 83 84 85
static const struct acpi_device_id pca953x_acpi_ids[] = {
	{ "INT3491", 16 | PCA953X_TYPE | PCA_INT, },
	{ }
};
MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);

86 87 88 89 90
#define MAX_BANK 5
#define BANK_SZ 8

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

91
struct pca953x_chip {
92
	unsigned gpio_start;
93 94
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
95
	struct mutex i2c_lock;
96

97 98
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
99 100 101 102
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
103 104
#endif

105 106
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
107
	const char *const *names;
108
	int	chip_type;
109
	unsigned long driver_data;
110 111
};

112 113 114 115 116
static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
{
	return container_of(gc, struct pca953x_chip, gpio_chip);
}

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 143 144 145 146 147 148 149 150 151 152 153 154
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)
155
{
156
	int ret = 0;
157 158

	if (chip->gpio_chip.ngpio <= 8)
159 160 161
		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);
162
		ret = i2c_smbus_write_i2c_block_data(chip->client,
163 164
					(reg << bank_shift) | REG_ADDR_AI,
					NBANK(chip), val);
165
	} else {
166 167 168
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			ret = i2c_smbus_write_word_data(chip->client,
169
							reg << 1, (u16) *val);
170 171 172
			break;
		case PCA957X_TYPE:
			ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
173
							val[0]);
174 175 176 177
			if (ret < 0)
				break;
			ret = i2c_smbus_write_byte_data(chip->client,
							(reg << 1) + 1,
178
							val[1]);
179 180 181
			break;
		}
	}
182 183 184

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

	return 0;
189 190
}

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

289
	chip->reg_direction[off / BANK_SZ] = reg_val;
290 291 292 293
	ret = 0;
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 = to_pca(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 = to_pca(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
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
356 357 358 359 360
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

361 362 363 364
	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;
365
	gc->can_sleep = true;
366 367

	gc->base = chip->gpio_start;
368 369
	gc->ngpio = gpios;
	gc->label = chip->client->name;
370
	gc->parent = &chip->client->dev;
371
	gc->owner = THIS_MODULE;
372
	gc->names = chip->names;
373 374
}

375
#ifdef CONFIG_GPIO_PCA953X_IRQ
376
static void pca953x_irq_mask(struct irq_data *d)
377
{
378 379
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
380

381
	chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
382 383
}

384
static void pca953x_irq_unmask(struct irq_data *d)
385
{
386 387
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
388

389
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
390 391
}

392
static void pca953x_irq_bus_lock(struct irq_data *d)
393
{
394 395
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
396 397 398 399

	mutex_lock(&chip->irq_lock);
}

400
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
401
{
402 403
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
404 405
	u8 new_irqs;
	int level, i;
406 407

	/* Look for any newly setup interrupt */
408 409 410 411 412 413 414 415 416 417
	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);
		}
418
	}
419 420 421 422

	mutex_unlock(&chip->irq_lock);
}

423
static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
424
{
425 426
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct pca953x_chip *chip = to_pca(gc);
427 428
	int bank_nb = d->hwirq / BANK_SZ;
	u8 mask = 1 << (d->hwirq % BANK_SZ);
429 430 431

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
432
			d->irq, type);
433 434 435 436
		return -EINVAL;
	}

	if (type & IRQ_TYPE_EDGE_FALLING)
437
		chip->irq_trig_fall[bank_nb] |= mask;
438
	else
439
		chip->irq_trig_fall[bank_nb] &= ~mask;
440 441

	if (type & IRQ_TYPE_EDGE_RISING)
442
		chip->irq_trig_raise[bank_nb] |= mask;
443
	else
444
		chip->irq_trig_raise[bank_nb] &= ~mask;
445

446
	return 0;
447 448 449 450
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
451 452 453 454 455
	.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,
456 457
};

458
static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
459
{
460 461
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
462 463 464
	bool pending_seen = false;
	bool trigger_seen = false;
	u8 trigger[MAX_BANK];
465
	int ret, i, offset = 0;
466 467 468 469 470 471 472 473 474

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
475
	ret = pca953x_read_regs(chip, offset, cur_stat);
476
	if (ret)
477
		return false;
478 479

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

483
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
484

485 486
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
487 488
		if (trigger[i])
			trigger_seen = true;
489 490
	}

491 492
	if (!trigger_seen)
		return false;
493

494
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
495

496 497 498 499
	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];
500 501
		if (pending[i])
			pending_seen = true;
502
	}
503

504
	return pending_seen;
505 506 507 508 509
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
510 511
	u8 pending[MAX_BANK];
	u8 level;
512
	unsigned nhandled = 0;
513
	int i;
514

515
	if (!pca953x_irq_pending(chip, pending))
516
		return IRQ_NONE;
517

518 519 520
	for (i = 0; i < NBANK(chip); i++) {
		while (pending[i]) {
			level = __ffs(pending[i]);
521
			handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
522 523
							level + (BANK_SZ * i)));
			pending[i] &= ~(1 << level);
524
			nhandled++;
525 526
		}
	}
527

528
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
529 530 531
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
532
			     int irq_base)
533 534
{
	struct i2c_client *client = chip->client;
535
	int ret, i, offset = 0;
536

537
	if (client->irq && irq_base != -1
538
			&& (chip->driver_data & PCA_INT)) {
539

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

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

561 562
		ret = devm_request_threaded_irq(&client->dev,
					client->irq,
563 564
					   NULL,
					   pca953x_irq_handler,
565 566
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT |
						   IRQF_SHARED,
567 568 569 570
					   dev_name(&client->dev), chip);
		if (ret) {
			dev_err(&client->dev, "failed to request irq %d\n",
				client->irq);
571
			return ret;
572 573
		}

574 575 576 577 578 579 580 581 582 583
		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;
		}
584 585 586 587

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
588 589 590 591 592 593 594
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
595
			     int irq_base)
596 597 598
{
	struct i2c_client *client = chip->client;

599
	if (irq_base != -1 && (chip->driver_data & PCA_INT))
600 601 602 603 604 605
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
606
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
607 608
{
	int ret;
609
	u8 val[MAX_BANK];
610

611
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
612 613 614
	if (ret)
		goto out;

615 616
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
617 618 619 620
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
621 622 623 624 625 626
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
627 628 629 630
out:
	return ret;
}

B
Bill Pemberton 已提交
631
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
632 633
{
	int ret;
634
	u8 val[MAX_BANK];
635

636
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
637 638
	if (ret)
		goto out;
639
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
640 641 642 643
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
644 645 646 647
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
648 649 650
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
651

652
	/* To enable register 6, 7 to control pull up and pull down */
653
	memset(val, 0x02, NBANK(chip));
654 655 656
	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
	if (ret)
		goto out;
657 658 659 660 661 662

	return 0;
out:
	return ret;
}

B
Bill Pemberton 已提交
663
static int pca953x_probe(struct i2c_client *client,
664
				   const struct i2c_device_id *id)
665
{
666 667
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
668
	int irq_base = 0;
669
	int ret;
670
	u32 invert = 0;
671

672 673
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
674 675 676
	if (chip == NULL)
		return -ENOMEM;

J
Jingoo Han 已提交
677
	pdata = dev_get_platdata(&client->dev);
678 679 680 681 682 683
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
684 685
		chip->gpio_start = -1;
		irq_base = 0;
686
	}
687 688 689

	chip->client = client;

690 691 692 693 694 695 696 697 698 699 700 701
	if (id) {
		chip->driver_data = id->driver_data;
	} else {
		const struct acpi_device_id *id;

		id = acpi_match_device(pca953x_acpi_ids, &client->dev);
		if (!id)
			return -ENODEV;

		chip->driver_data = id->driver_data;
	}

702
	chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
703

704 705
	mutex_init(&chip->i2c_lock);

706 707 708
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
709
	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
710

711
	if (chip->chip_type == PCA953X_TYPE)
712
		ret = device_pca953x_init(chip, invert);
713
	else
714 715
		ret = device_pca957x_init(chip, invert);
	if (ret)
716
		return ret;
717

718
	ret = gpiochip_add(&chip->gpio_chip);
719
	if (ret)
720
		return ret;
721

722
	ret = pca953x_irq_setup(chip, irq_base);
723
	if (ret)
724
		return ret;
725

726
	if (pdata && pdata->setup) {
727 728 729 730 731 732 733 734 735 736
		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;
}

737
static int pca953x_remove(struct i2c_client *client)
738
{
J
Jingoo Han 已提交
739
	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
740
	struct pca953x_chip *chip = i2c_get_clientdata(client);
741 742
	int ret = 0;

743
	if (pdata && pdata->teardown) {
744 745 746 747 748 749 750 751 752
		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;
		}
	}

753
	gpiochip_remove(&chip->gpio_chip);
754 755 756 757

	return 0;
}

758
static const struct of_device_id pca953x_dt_ids[] = {
759
	{ .compatible = "nxp,pca9505", },
760 761 762 763 764 765 766 767 768 769 770 771
	{ .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 已提交
772
	{ .compatible = "nxp,pca9698", },
773 774 775 776 777 778 779 780 781 782

	{ .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 已提交
783

B
Ben Dooks 已提交
784 785
	{ .compatible = "onsemi,pca9654" },

A
Aaron Sierra 已提交
786
	{ .compatible = "exar,xra1202", },
787 788 789 790 791
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

792
static struct i2c_driver pca953x_driver = {
793
	.driver = {
794
		.name	= "pca953x",
795
		.of_match_table = pca953x_dt_ids,
796
		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
797
	},
798 799
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
800
	.id_table	= pca953x_id,
801 802
};

803
static int __init pca953x_init(void)
804
{
805
	return i2c_add_driver(&pca953x_driver);
806
}
807 808 809 810
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
811

812
static void __exit pca953x_exit(void)
813
{
814
	i2c_del_driver(&pca953x_driver);
815
}
816
module_exit(pca953x_exit);
817 818

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