gpio-pca953x.c 23.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
#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

B
Bartosz Golaszewski 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
struct pca953x_reg_config {
	int direction;
	int output;
	int input;
};

static const struct pca953x_reg_config pca953x_regs = {
	.direction = PCA953X_DIRECTION,
	.output = PCA953X_OUTPUT,
	.input = PCA953X_INPUT,
};

static const struct pca953x_reg_config pca957x_regs = {
	.direction = PCA957X_CFG,
	.output = PCA957X_OUT,
	.input = PCA957X_IN,
};

116
struct pca953x_chip {
117
	unsigned gpio_start;
118 119
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
120
	struct mutex i2c_lock;
121

122 123
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
124 125 126 127
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
128 129
#endif

130 131
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
132
	const char *const *names;
133
	unsigned long driver_data;
134
	struct regulator *regulator;
B
Bartosz Golaszewski 已提交
135 136

	const struct pca953x_reg_config *regs;
137 138

	int (*write_regs)(struct pca953x_chip *, int, u8 *);
139
	int (*read_regs)(struct pca953x_chip *, int, u8 *);
140 141
};

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
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)
{
164
	int ret;
165 166 167 168 169 170 171 172 173 174 175 176 177 178
	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;
}

179
static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
180
{
181 182
	return i2c_smbus_write_byte_data(chip->client, reg, *val);
}
183

184 185 186
static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
{
	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201
	return i2c_smbus_write_word_data(chip->client,
					 reg << 1, (__force u16)word);
}

static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
{
	int ret;

	ret = i2c_smbus_write_byte_data(chip->client, reg << 1, val[0]);
	if (ret < 0)
		return ret;

	return i2c_smbus_write_byte_data(chip->client, (reg << 1) + 1, val[1]);
}
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216
static int pca953x_write_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
{
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);

	return i2c_smbus_write_i2c_block_data(chip->client,
					      (reg << bank_shift) | REG_ADDR_AI,
					      NBANK(chip), val);
}

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

	ret = chip->write_regs(chip, reg, val);
217 218
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed writing register\n");
219
		return ret;
220 221 222
	}

	return 0;
223 224
}

225
static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
226 227 228
{
	int ret;

229 230
	ret = i2c_smbus_read_byte_data(chip->client, reg);
	*val = ret;
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	return ret;
}

static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
{
	int ret;

	ret = i2c_smbus_read_word_data(chip->client, reg << 1);
	val[0] = (u16)ret & 0xFF;
	val[1] = (u16)ret >> 8;

	return ret;
}

static int pca953x_read_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
{
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);

	return i2c_smbus_read_i2c_block_data(chip->client,
					     (reg << bank_shift) | REG_ADDR_AI,
					     NBANK(chip), val);
}

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

	ret = chip->read_regs(chip, reg, val);
260 261
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed reading register\n");
262
		return ret;
263 264 265 266 267
	}

	return 0;
}

268
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
269
{
270
	struct pca953x_chip *chip = gpiochip_get_data(gc);
271
	u8 reg_val;
B
Bartosz Golaszewski 已提交
272
	int ret;
273

274
	mutex_lock(&chip->i2c_lock);
275
	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
276

B
Bartosz Golaszewski 已提交
277
	ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
278
	if (ret)
279
		goto exit;
280

281
	chip->reg_direction[off / BANK_SZ] = reg_val;
282 283 284
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
285 286
}

287
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
288 289
		unsigned off, int val)
{
290
	struct pca953x_chip *chip = gpiochip_get_data(gc);
291
	u8 reg_val;
B
Bartosz Golaszewski 已提交
292
	int ret;
293

294
	mutex_lock(&chip->i2c_lock);
295 296
	/* set output level */
	if (val)
297 298
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
299
	else
300 301
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
302

B
Bartosz Golaszewski 已提交
303
	ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
304
	if (ret)
305
		goto exit;
306

307
	chip->reg_output[off / BANK_SZ] = reg_val;
308 309

	/* then direction */
310
	reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
B
Bartosz Golaszewski 已提交
311
	ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
312
	if (ret)
313
		goto exit;
314

315
	chip->reg_direction[off / BANK_SZ] = reg_val;
316 317 318
exit:
	mutex_unlock(&chip->i2c_lock);
	return ret;
319 320
}

321
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
322
{
323
	struct pca953x_chip *chip = gpiochip_get_data(gc);
A
Andreas Schallenberg 已提交
324
	u32 reg_val;
B
Bartosz Golaszewski 已提交
325
	int ret;
326

327
	mutex_lock(&chip->i2c_lock);
B
Bartosz Golaszewski 已提交
328
	ret = pca953x_read_single(chip, chip->regs->input, &reg_val, off);
329
	mutex_unlock(&chip->i2c_lock);
330 331 332 333 334 335 336 337
	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;
	}

338
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
339 340
}

341
static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
342
{
343
	struct pca953x_chip *chip = gpiochip_get_data(gc);
344
	u8 reg_val;
B
Bartosz Golaszewski 已提交
345
	int ret;
346

347
	mutex_lock(&chip->i2c_lock);
348
	if (val)
349 350
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
351
	else
352 353
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
354

B
Bartosz Golaszewski 已提交
355
	ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
356
	if (ret)
357
		goto exit;
358

359
	chip->reg_output[off / BANK_SZ] = reg_val;
360 361
exit:
	mutex_unlock(&chip->i2c_lock);
362 363
}

364 365 366
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
		unsigned long *mask, unsigned long *bits)
{
367
	struct pca953x_chip *chip = gpiochip_get_data(gc);
368
	u8 reg_val[MAX_BANK];
B
Bartosz Golaszewski 已提交
369
	int ret;
370 371 372 373 374 375
	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
	int bank;

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

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

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

	gc = &chip->gpio_chip;

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

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

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

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

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

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

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

	mutex_lock(&chip->irq_lock);
}

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

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

	mutex_unlock(&chip->irq_lock);
}

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

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

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

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

499
	return 0;
500 501 502 503
}

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

511
static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
512
{
513 514
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
515 516 517
	bool pending_seen = false;
	bool trigger_seen = false;
	u8 trigger[MAX_BANK];
B
Bartosz Golaszewski 已提交
518
	int ret, i;
519

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

B
Bartosz Golaszewski 已提交
543
	ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
544
	if (ret)
545
		return false;
546 547

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

551
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
552

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

559 560
	if (!trigger_seen)
		return false;
561

562
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
563

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

572
	return pending_seen;
573 574 575 576 577
}

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

583
	if (!pca953x_irq_pending(chip, pending))
584
		return IRQ_NONE;
585

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

596
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
597 598 599
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
600
			     int irq_base)
601 602
{
	struct i2c_client *client = chip->client;
B
Bartosz Golaszewski 已提交
603
	int ret, i;
604

605
	if (client->irq && irq_base != -1
606
			&& (chip->driver_data & PCA_INT)) {
607

B
Bartosz Golaszewski 已提交
608 609
		ret = pca953x_read_regs(chip,
					chip->regs->input, chip->irq_stat);
610
		if (ret)
611
			return ret;
612 613 614 615 616 617

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

622 623
		ret = devm_request_threaded_irq(&client->dev,
					client->irq,
624 625
					   NULL,
					   pca953x_irq_handler,
626 627
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT |
						   IRQF_SHARED,
628 629 630 631
					   dev_name(&client->dev), chip);
		if (ret) {
			dev_err(&client->dev, "failed to request irq %d\n",
				client->irq);
632
			return ret;
633 634
		}

635 636 637 638 639 640 641 642 643 644
		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;
		}
645 646 647 648

		gpiochip_set_chained_irqchip(&chip->gpio_chip,
					     &pca953x_irq_chip,
					     client->irq, NULL);
649 650 651 652 653 654 655
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
656
			     int irq_base)
657 658 659
{
	struct i2c_client *client = chip->client;

660
	if (irq_base != -1 && (chip->driver_data & PCA_INT))
661 662 663 664 665 666
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
667
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
668 669
{
	int ret;
670
	u8 val[MAX_BANK];
671

B
Bartosz Golaszewski 已提交
672 673 674
	chip->regs = &pca953x_regs;

	ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
675 676 677
	if (ret)
		goto out;

B
Bartosz Golaszewski 已提交
678 679
	ret = pca953x_read_regs(chip, chip->regs->direction,
				chip->reg_direction);
680 681 682 683
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
684 685 686 687 688 689
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
690 691 692 693
out:
	return ret;
}

B
Bill Pemberton 已提交
694
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
695 696
{
	int ret;
697
	u8 val[MAX_BANK];
698

B
Bartosz Golaszewski 已提交
699 700 701
	chip->regs = &pca957x_regs;

	ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
702 703
	if (ret)
		goto out;
B
Bartosz Golaszewski 已提交
704 705
	ret = pca953x_read_regs(chip, chip->regs->direction,
				chip->reg_direction);
706 707 708 709
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
710 711 712 713
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
714 715 716
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
717

718
	/* To enable register 6, 7 to control pull up and pull down */
719
	memset(val, 0x02, NBANK(chip));
720 721 722
	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
	if (ret)
		goto out;
723 724 725 726 727 728

	return 0;
out:
	return ret;
}

729 730
static const struct of_device_id pca953x_dt_ids[];

B
Bill Pemberton 已提交
731
static int pca953x_probe(struct i2c_client *client,
732
				   const struct i2c_device_id *id)
733
{
734 735
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
736
	int irq_base = 0;
737
	int ret;
738
	u32 invert = 0;
739
	struct regulator *reg;
740

741 742
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
743 744 745
	if (chip == NULL)
		return -ENOMEM;

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

	chip->client = client;

759 760 761 762 763 764 765 766 767 768 769 770 771 772
	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;

773 774 775 776
	if (id) {
		chip->driver_data = id->driver_data;
	} else {
		const struct acpi_device_id *id;
777
		const struct of_device_id *match;
778

779 780 781 782 783
		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);
784 785 786 787
			if (!id) {
				ret = -ENODEV;
				goto err_exit;
			}
788

789 790
			chip->driver_data = id->driver_data;
		}
791 792
	}

793 794
	mutex_init(&chip->i2c_lock);

795 796 797
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
798
	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
799

800 801
	if (chip->gpio_chip.ngpio <= 8) {
		chip->write_regs = pca953x_write_regs_8;
802
		chip->read_regs = pca953x_read_regs_8;
803 804
	} else if (chip->gpio_chip.ngpio >= 24) {
		chip->write_regs = pca953x_write_regs_24;
805
		chip->read_regs = pca953x_read_regs_24;
806 807 808 809 810
	} else {
		if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
			chip->write_regs = pca953x_write_regs_16;
		else
			chip->write_regs = pca957x_write_regs_16;
811
		chip->read_regs = pca953x_read_regs_16;
812 813
	}

814
	if (PCA_CHIP_TYPE(chip->driver_data) == 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
			dev_err(&client->dev, "%s failed, %d\n",
					"teardown", ret);
856 857
	} else {
		ret = 0;
858 859
	}

860 861 862
	regulator_disable(chip->regulator);

	return ret;
863 864
}

865 866 867 868
/* 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)

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

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

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

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

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

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