gpio-pca953x.c 22.8 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
#include <linux/regulator/consumer.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
#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

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

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

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

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

71 72
	{ "pcal9555a", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },

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

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

93 94 95
#define MAX_BANK 5
#define BANK_SZ 8

96
#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
97

98
struct pca953x_chip {
99
	unsigned gpio_start;
100 101
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
102
	struct mutex i2c_lock;
103

104 105
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
106 107 108 109
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
110 111
#endif

112 113
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
114
	const char *const *names;
115
	int	chip_type;
116
	unsigned long driver_data;
117
	struct regulator *regulator;
118 119
};

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
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)
{
142
	int ret;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	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)
158
{
159
	int ret = 0;
160 161

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

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

	return 0;
195 196
}

197
static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
198 199 200
{
	int ret;

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

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

	return 0;
}

223
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
224
{
225
	struct pca953x_chip *chip = gpiochip_get_data(gc);
226
	u8 reg_val;
227
	int ret, offset = 0;
228

229
	mutex_lock(&chip->i2c_lock);
230
	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
231 232 233 234 235 236 237 238 239

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_DIRECTION;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_CFG;
		break;
	}
240
	ret = pca953x_write_single(chip, offset, reg_val, off);
241
	if (ret)
242
		goto exit;
243

244
	chip->reg_direction[off / BANK_SZ] = reg_val;
245 246 247
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
248 249
}

250
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
251 252
		unsigned off, int val)
{
253
	struct pca953x_chip *chip = gpiochip_get_data(gc);
254
	u8 reg_val;
255
	int ret, offset = 0;
256

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

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

278
	chip->reg_output[off / BANK_SZ] = reg_val;
279 280

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

294
	chip->reg_direction[off / BANK_SZ] = reg_val;
295 296 297
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
298 299
}

300
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
301
{
302
	struct pca953x_chip *chip = gpiochip_get_data(gc);
A
Andreas Schallenberg 已提交
303
	u32 reg_val;
304
	int ret, offset = 0;
305

306
	mutex_lock(&chip->i2c_lock);
307 308 309 310 311 312 313 314
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
315
	ret = pca953x_read_single(chip, offset, &reg_val, off);
316
	mutex_unlock(&chip->i2c_lock);
317 318 319 320 321 322 323 324
	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;
	}

325
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
326 327
}

328
static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
329
{
330
	struct pca953x_chip *chip = gpiochip_get_data(gc);
331
	u8 reg_val;
332
	int ret, offset = 0;
333

334
	mutex_lock(&chip->i2c_lock);
335
	if (val)
336 337
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
338
	else
339 340
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
341

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

354
	chip->reg_output[off / BANK_SZ] = reg_val;
355 356
exit:
	mutex_unlock(&chip->i2c_lock);
357 358
}

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

397
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
398 399 400 401 402
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

403 404 405 406
	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;
407
	gc->set_multiple = pca953x_gpio_set_multiple;
408
	gc->can_sleep = true;
409 410

	gc->base = chip->gpio_start;
411 412
	gc->ngpio = gpios;
	gc->label = chip->client->name;
413
	gc->parent = &chip->client->dev;
414
	gc->owner = THIS_MODULE;
415
	gc->names = chip->names;
416 417
}

418
#ifdef CONFIG_GPIO_PCA953X_IRQ
419
static void pca953x_irq_mask(struct irq_data *d)
420
{
421
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
422
	struct pca953x_chip *chip = gpiochip_get_data(gc);
423

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

427
static void pca953x_irq_unmask(struct irq_data *d)
428
{
429
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
430
	struct pca953x_chip *chip = gpiochip_get_data(gc);
431

432
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
433 434
}

435
static void pca953x_irq_bus_lock(struct irq_data *d)
436
{
437
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
438
	struct pca953x_chip *chip = gpiochip_get_data(gc);
439 440 441 442

	mutex_lock(&chip->irq_lock);
}

443
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
444
{
445
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
446
	struct pca953x_chip *chip = gpiochip_get_data(gc);
447 448
	u8 new_irqs;
	int level, i;
449 450 451 452 453 454 455 456 457 458 459 460
	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);
	}
461 462

	/* Look for any newly setup interrupt */
463 464 465 466 467 468 469 470 471 472
	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);
		}
473
	}
474 475 476 477

	mutex_unlock(&chip->irq_lock);
}

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

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
487
			d->irq, type);
488 489 490 491
		return -EINVAL;
	}

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

	if (type & IRQ_TYPE_EDGE_RISING)
497
		chip->irq_trig_raise[bank_nb] |= mask;
498
	else
499
		chip->irq_trig_raise[bank_nb] &= ~mask;
500

501
	return 0;
502 503 504 505
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
506 507 508 509 510
	.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,
511 512
};

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

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	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;
	}

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

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

561
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
562

563 564
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
565 566
		if (trigger[i])
			trigger_seen = true;
567 568
	}

569 570
	if (!trigger_seen)
		return false;
571

572
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
573

574 575 576 577
	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];
578 579
		if (pending[i])
			pending_seen = true;
580
	}
581

582
	return pending_seen;
583 584 585 586 587
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
588 589
	u8 pending[MAX_BANK];
	u8 level;
590
	unsigned nhandled = 0;
591
	int i;
592

593
	if (!pca953x_irq_pending(chip, pending))
594
		return IRQ_NONE;
595

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

606
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
607 608 609
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
610
			     int irq_base)
611 612
{
	struct i2c_client *client = chip->client;
613
	int ret, i, offset = 0;
614

615
	if (client->irq && irq_base != -1
616
			&& (chip->driver_data & PCA_INT)) {
617

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

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

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

652 653 654 655 656 657 658 659 660 661
		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;
		}
662 663 664 665

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
666 667 668 669 670 671 672
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
673
			     int irq_base)
674 675 676
{
	struct i2c_client *client = chip->client;

677
	if (irq_base != -1 && (chip->driver_data & PCA_INT))
678 679 680 681 682 683
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
684
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
685 686
{
	int ret;
687
	u8 val[MAX_BANK];
688

689
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
690 691 692
	if (ret)
		goto out;

693 694
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
695 696 697 698
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
699 700 701 702 703 704
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
705 706 707 708
out:
	return ret;
}

B
Bill Pemberton 已提交
709
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
710 711
{
	int ret;
712
	u8 val[MAX_BANK];
713

714
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
715 716
	if (ret)
		goto out;
717
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
718 719 720 721
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
722 723 724 725
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
726 727 728
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
729

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

	return 0;
out:
	return ret;
}

741 742
static const struct of_device_id pca953x_dt_ids[];

B
Bill Pemberton 已提交
743
static int pca953x_probe(struct i2c_client *client,
744
				   const struct i2c_device_id *id)
745
{
746 747
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
748
	int irq_base = 0;
749
	int ret;
750
	u32 invert = 0;
751
	struct regulator *reg;
752

753 754
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
755 756 757
	if (chip == NULL)
		return -ENOMEM;

J
Jingoo Han 已提交
758
	pdata = dev_get_platdata(&client->dev);
759 760 761 762 763 764
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
765 766
		chip->gpio_start = -1;
		irq_base = 0;
767
	}
768 769 770

	chip->client = client;

771 772 773 774 775 776 777 778 779 780 781 782 783 784
	reg = devm_regulator_get(&client->dev, "vcc");
	if (IS_ERR(reg)) {
		ret = PTR_ERR(reg);
		if (ret != -EPROBE_DEFER)
			dev_err(&client->dev, "reg get err: %d\n", ret);
		return ret;
	}
	ret = regulator_enable(reg);
	if (ret) {
		dev_err(&client->dev, "reg en err: %d\n", ret);
		return ret;
	}
	chip->regulator = reg;

785 786 787 788
	if (id) {
		chip->driver_data = id->driver_data;
	} else {
		const struct acpi_device_id *id;
789
		const struct of_device_id *match;
790

791 792 793 794 795
		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);
796 797 798 799
			if (!id) {
				ret = -ENODEV;
				goto err_exit;
			}
800

801 802
			chip->driver_data = id->driver_data;
		}
803 804
	}

805
	chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
806

807 808
	mutex_init(&chip->i2c_lock);

809 810 811
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
812
	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
813

814
	if (chip->chip_type == PCA953X_TYPE)
815
		ret = device_pca953x_init(chip, invert);
816
	else
817 818
		ret = device_pca957x_init(chip, invert);
	if (ret)
819
		goto err_exit;
820

821
	ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
822
	if (ret)
823
		goto err_exit;
824

825
	ret = pca953x_irq_setup(chip, irq_base);
826
	if (ret)
827
		goto err_exit;
828

829
	if (pdata && pdata->setup) {
830 831 832 833 834 835 836 837
		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;
838 839 840 841

err_exit:
	regulator_disable(chip->regulator);
	return ret;
842 843
}

844
static int pca953x_remove(struct i2c_client *client)
845
{
J
Jingoo Han 已提交
846
	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
847
	struct pca953x_chip *chip = i2c_get_clientdata(client);
848
	int ret;
849

850
	if (pdata && pdata->teardown) {
851 852
		ret = pdata->teardown(client, chip->gpio_chip.base,
				chip->gpio_chip.ngpio, pdata->context);
853
		if (ret < 0)
854 855 856 857
			dev_err(&client->dev, "%s failed, %d\n",
					"teardown", ret);
	}

858 859 860
	regulator_disable(chip->regulator);

	return ret;
861 862
}

863 864 865 866
/* 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)

867
static const struct of_device_id pca953x_dt_ids[] = {
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	{ .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), },
889
	{ .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
890 891 892 893 894 895 896
	{ .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), },
897 898 899 900 901
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

902
static struct i2c_driver pca953x_driver = {
903
	.driver = {
904
		.name	= "pca953x",
905
		.of_match_table = pca953x_dt_ids,
906
		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
907
	},
908 909
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
910
	.id_table	= pca953x_id,
911 912
};

913
static int __init pca953x_init(void)
914
{
915
	return i2c_add_driver(&pca953x_driver);
916
}
917 918 919 920
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
921

922
static void __exit pca953x_exit(void)
923
{
924
	i2c_del_driver(&pca953x_driver);
925
}
926
module_exit(pca953x_exit);
927 928

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