gpio-pca953x.c 24.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
 *
 *  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.
 */

14
#include <linux/acpi.h>
15
#include <linux/gpio.h>
16
#include <linux/gpio/consumer.h>
17
#include <linux/i2c.h>
18 19 20 21
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of_platform.h>
22
#include <linux/platform_data/pca953x.h>
23
#include <linux/regulator/consumer.h>
24
#include <linux/slab.h>
25

26
#include <asm/unaligned.h>
27

28 29 30 31 32
#define PCA953X_INPUT		0
#define PCA953X_OUTPUT		1
#define PCA953X_INVERT		2
#define PCA953X_DIRECTION	3

A
Andreas Schallenberg 已提交
33 34
#define REG_ADDR_AI		0x80

35 36 37 38 39 40 41 42 43
#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

44 45 46 47
#define PCAL953X_IN_LATCH	34
#define PCAL953X_INT_MASK	37
#define PCAL953X_INT_STAT	38

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

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

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

73 74
	{ "pcal9555a", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },

75 76 77 78
	{ "max7310", 8  | PCA953X_TYPE, },
	{ "max7312", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7313", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7315", 8  | PCA953X_TYPE | PCA_INT, },
79
	{ "max7318", 16 | PCA953X_TYPE | PCA_INT, },
80 81 82
	{ "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
A
Andreas Schallenberg 已提交
83
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
84
	{ "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
A
Aaron Sierra 已提交
85
	{ "xra1202", 8  | PCA953X_TYPE },
86
	{ }
87
};
88
MODULE_DEVICE_TABLE(i2c, pca953x_id);
89

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

96 97 98
#define MAX_BANK 5
#define BANK_SZ 8

99
#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
100

B
Bartosz Golaszewski 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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,
};

119
struct pca953x_chip {
120
	unsigned gpio_start;
121 122
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
123
	struct mutex i2c_lock;
124

125 126
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
127 128 129 130
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
131 132
#endif

133 134
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
135
	const char *const *names;
136
	unsigned long driver_data;
137
	struct regulator *regulator;
B
Bartosz Golaszewski 已提交
138 139

	const struct pca953x_reg_config *regs;
140 141

	int (*write_regs)(struct pca953x_chip *, int, u8 *);
142
	int (*read_regs)(struct pca953x_chip *, int, u8 *);
143 144
};

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

182
static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
183
{
184 185
	return i2c_smbus_write_byte_data(chip->client, reg, *val);
}
186

187 188 189
static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
{
	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204
	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]);
}
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219
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);
220 221
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed writing register\n");
222
		return ret;
223 224 225
	}

	return 0;
226 227
}

228
static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
229 230 231
{
	int ret;

232 233
	ret = i2c_smbus_read_byte_data(chip->client, reg);
	*val = ret;
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 260 261 262
	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);
263 264
	if (ret < 0) {
		dev_err(&chip->client->dev, "failed reading register\n");
265
		return ret;
266 267 268 269 270
	}

	return 0;
}

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

277
	mutex_lock(&chip->i2c_lock);
278
	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
279

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

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

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

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

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

310
	chip->reg_output[off / BANK_SZ] = reg_val;
311 312

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

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

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

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

341
	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
342 343
}

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

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

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

362
	chip->reg_output[off / BANK_SZ] = reg_val;
363 364
exit:
	mutex_unlock(&chip->i2c_lock);
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
{
	struct pca953x_chip *chip = gpiochip_get_data(gc);
	u32 reg_val;
	int ret;

	mutex_lock(&chip->i2c_lock);
	ret = pca953x_read_single(chip, chip->regs->direction, &reg_val, off);
	mutex_unlock(&chip->i2c_lock);
	if (ret < 0)
		return ret;

	return !!(reg_val & (1u << (off % BANK_SZ)));
}

382
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
383
				      unsigned long *mask, unsigned long *bits)
384
{
385
	struct pca953x_chip *chip = gpiochip_get_data(gc);
386 387
	unsigned int bank_mask, bank_val;
	int bank_shift, bank;
388
	u8 reg_val[MAX_BANK];
B
Bartosz Golaszewski 已提交
389
	int ret;
390 391

	bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
392 393

	mutex_lock(&chip->i2c_lock);
394
	memcpy(reg_val, chip->reg_output, NBANK(chip));
395 396 397 398 399 400
	for (bank = 0; bank < NBANK(chip); bank++) {
		bank_mask = mask[bank / sizeof(*mask)] >>
			   ((bank % sizeof(*mask)) * 8);
		if (bank_mask) {
			bank_val = bits[bank / sizeof(*bits)] >>
				  ((bank % sizeof(*bits)) * 8);
401
			bank_val &= bank_mask;
402
			reg_val[bank] = (reg_val[bank] & ~bank_mask) | bank_val;
403 404
		}
	}
405

B
Bartosz Golaszewski 已提交
406 407 408
	ret = i2c_smbus_write_i2c_block_data(chip->client,
					     chip->regs->output << bank_shift,
					     NBANK(chip), reg_val);
409 410 411 412 413 414 415 416
	if (ret)
		goto exit;

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

417
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
418 419 420 421 422
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

423 424 425 426
	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;
427
	gc->get_direction = pca953x_gpio_get_direction;
428
	gc->set_multiple = pca953x_gpio_set_multiple;
429
	gc->can_sleep = true;
430 431

	gc->base = chip->gpio_start;
432 433
	gc->ngpio = gpios;
	gc->label = chip->client->name;
434
	gc->parent = &chip->client->dev;
435
	gc->owner = THIS_MODULE;
436
	gc->names = chip->names;
437 438
}

439
#ifdef CONFIG_GPIO_PCA953X_IRQ
440
static void pca953x_irq_mask(struct irq_data *d)
441
{
442
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
443
	struct pca953x_chip *chip = gpiochip_get_data(gc);
444

445
	chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
446 447
}

448
static void pca953x_irq_unmask(struct irq_data *d)
449
{
450
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
451
	struct pca953x_chip *chip = gpiochip_get_data(gc);
452

453
	chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
454 455
}

456
static void pca953x_irq_bus_lock(struct irq_data *d)
457
{
458
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
459
	struct pca953x_chip *chip = gpiochip_get_data(gc);
460 461 462 463

	mutex_lock(&chip->irq_lock);
}

464
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
465
{
466
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
467
	struct pca953x_chip *chip = gpiochip_get_data(gc);
468 469
	u8 new_irqs;
	int level, i;
470 471 472 473 474 475 476 477 478 479 480 481
	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);
	}
482 483

	/* Look for any newly setup interrupt */
484 485 486 487 488 489 490 491 492 493
	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);
		}
494
	}
495 496 497 498

	mutex_unlock(&chip->irq_lock);
}

499
static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
500
{
501
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
502
	struct pca953x_chip *chip = gpiochip_get_data(gc);
503 504
	int bank_nb = d->hwirq / BANK_SZ;
	u8 mask = 1 << (d->hwirq % BANK_SZ);
505 506 507

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
508
			d->irq, type);
509 510 511 512
		return -EINVAL;
	}

	if (type & IRQ_TYPE_EDGE_FALLING)
513
		chip->irq_trig_fall[bank_nb] |= mask;
514
	else
515
		chip->irq_trig_fall[bank_nb] &= ~mask;
516 517

	if (type & IRQ_TYPE_EDGE_RISING)
518
		chip->irq_trig_raise[bank_nb] |= mask;
519
	else
520
		chip->irq_trig_raise[bank_nb] &= ~mask;
521

522
	return 0;
523 524 525 526
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
527 528 529 530 531
	.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,
532 533
};

534
static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
535
{
536 537
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
538 539 540
	bool pending_seen = false;
	bool trigger_seen = false;
	u8 trigger[MAX_BANK];
B
Bartosz Golaszewski 已提交
541
	int ret, i;
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	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 已提交
566
	ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
567
	if (ret)
568
		return false;
569 570

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

574
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
575

576 577
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
578 579
		if (trigger[i])
			trigger_seen = true;
580 581
	}

582 583
	if (!trigger_seen)
		return false;
584

585
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
586

587 588 589 590
	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];
591 592
		if (pending[i])
			pending_seen = true;
593
	}
594

595
	return pending_seen;
596 597 598 599 600
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
601 602
	u8 pending[MAX_BANK];
	u8 level;
603
	unsigned nhandled = 0;
604
	int i;
605

606
	if (!pca953x_irq_pending(chip, pending))
607
		return IRQ_NONE;
608

609 610 611
	for (i = 0; i < NBANK(chip); i++) {
		while (pending[i]) {
			level = __ffs(pending[i]);
612
			handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
613 614
							level + (BANK_SZ * i)));
			pending[i] &= ~(1 << level);
615
			nhandled++;
616 617
		}
	}
618

619
	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
620 621 622
}

static int pca953x_irq_setup(struct pca953x_chip *chip,
623
			     int irq_base)
624 625
{
	struct i2c_client *client = chip->client;
B
Bartosz Golaszewski 已提交
626
	int ret, i;
627

628
	if (client->irq && irq_base != -1
629
			&& (chip->driver_data & PCA_INT)) {
B
Bartosz Golaszewski 已提交
630 631
		ret = pca953x_read_regs(chip,
					chip->regs->input, chip->irq_stat);
632
		if (ret)
633
			return ret;
634 635 636 637 638 639

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

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

657 658 659 660 661
		ret =  gpiochip_irqchip_add_nested(&chip->gpio_chip,
						   &pca953x_irq_chip,
						   irq_base,
						   handle_simple_irq,
						   IRQ_TYPE_NONE);
662 663 664 665 666
		if (ret) {
			dev_err(&client->dev,
				"could not connect irqchip to gpiochip\n");
			return ret;
		}
667

668 669 670
		gpiochip_set_nested_irqchip(&chip->gpio_chip,
					    &pca953x_irq_chip,
					    client->irq);
671 672 673 674 675 676 677
	}

	return 0;
}

#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
678
			     int irq_base)
679 680 681
{
	struct i2c_client *client = chip->client;

682
	if (irq_base != -1 && (chip->driver_data & PCA_INT))
683 684 685 686 687 688
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}
#endif

B
Bill Pemberton 已提交
689
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
690 691
{
	int ret;
692
	u8 val[MAX_BANK];
693

B
Bartosz Golaszewski 已提交
694 695 696
	chip->regs = &pca953x_regs;

	ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
697 698 699
	if (ret)
		goto out;

B
Bartosz Golaszewski 已提交
700 701
	ret = pca953x_read_regs(chip, chip->regs->direction,
				chip->reg_direction);
702 703 704 705
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
706 707 708 709 710 711
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
712 713 714 715
out:
	return ret;
}

B
Bill Pemberton 已提交
716
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
717 718
{
	int ret;
719
	u8 val[MAX_BANK];
720

B
Bartosz Golaszewski 已提交
721 722 723
	chip->regs = &pca957x_regs;

	ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
724 725
	if (ret)
		goto out;
B
Bartosz Golaszewski 已提交
726 727
	ret = pca953x_read_regs(chip, chip->regs->direction,
				chip->reg_direction);
728 729 730 731
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
732 733 734 735
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));
736 737 738
	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
	if (ret)
		goto out;
739

740
	/* To enable register 6, 7 to control pull up and pull down */
741
	memset(val, 0x02, NBANK(chip));
742 743 744
	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
	if (ret)
		goto out;
745 746 747 748 749 750

	return 0;
out:
	return ret;
}

751 752
static const struct of_device_id pca953x_dt_ids[];

B
Bill Pemberton 已提交
753
static int pca953x_probe(struct i2c_client *client,
754
				   const struct i2c_device_id *i2c_id)
755
{
756 757
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
758
	int irq_base = 0;
759
	int ret;
760
	u32 invert = 0;
761
	struct regulator *reg;
762

763 764
	chip = devm_kzalloc(&client->dev,
			sizeof(struct pca953x_chip), GFP_KERNEL);
765 766 767
	if (chip == NULL)
		return -ENOMEM;

J
Jingoo Han 已提交
768
	pdata = dev_get_platdata(&client->dev);
769 770 771 772 773 774
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
775 776
		struct gpio_desc *reset_gpio;

777 778
		chip->gpio_start = -1;
		irq_base = 0;
779 780 781 782 783 784

		/* See if we need to de-assert a reset pin */
		reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
						     GPIOD_OUT_LOW);
		if (IS_ERR(reset_gpio))
			return PTR_ERR(reset_gpio);
785
	}
786 787 788

	chip->client = client;

789 790 791 792 793 794 795 796 797 798 799 800 801 802
	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;

803 804
	if (i2c_id) {
		chip->driver_data = i2c_id->driver_data;
805
	} else {
806
		const struct acpi_device_id *acpi_id;
807
		const struct of_device_id *match;
808

809 810 811 812
		match = of_match_device(pca953x_dt_ids, &client->dev);
		if (match) {
			chip->driver_data = (int)(uintptr_t)match->data;
		} else {
813
			acpi_id = acpi_match_device(pca953x_acpi_ids, &client->dev);
814
			if (!acpi_id) {
815 816 817
				ret = -ENODEV;
				goto err_exit;
			}
818

819
			chip->driver_data = acpi_id->driver_data;
820
		}
821 822
	}

823
	mutex_init(&chip->i2c_lock);
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
	/*
	 * In case we have an i2c-mux controlled by a GPIO provided by an
	 * expander using the same driver higher on the device tree, read the
	 * i2c adapter nesting depth and use the retrieved value as lockdep
	 * subclass for chip->i2c_lock.
	 *
	 * REVISIT: This solution is not complete. It protects us from lockdep
	 * false positives when the expander controlling the i2c-mux is on
	 * a different level on the device tree, but not when it's on the same
	 * level on a different branch (in which case the subclass number
	 * would be the same).
	 *
	 * TODO: Once a correct solution is developed, a similar fix should be
	 * applied to all other i2c-controlled GPIO expanders (and potentially
	 * regmap-i2c).
	 */
840 841
	lockdep_set_subclass(&chip->i2c_lock,
			     i2c_adapter_depth(client->adapter));
842

843 844 845
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
846
	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
847

848 849
	if (chip->gpio_chip.ngpio <= 8) {
		chip->write_regs = pca953x_write_regs_8;
850
		chip->read_regs = pca953x_read_regs_8;
851 852
	} else if (chip->gpio_chip.ngpio >= 24) {
		chip->write_regs = pca953x_write_regs_24;
853
		chip->read_regs = pca953x_read_regs_24;
854 855 856 857 858
	} 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;
859
		chip->read_regs = pca953x_read_regs_16;
860 861
	}

862
	if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
863
		ret = device_pca953x_init(chip, invert);
864
	else
865 866
		ret = device_pca957x_init(chip, invert);
	if (ret)
867
		goto err_exit;
868

869
	ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
870
	if (ret)
871
		goto err_exit;
872

873
	ret = pca953x_irq_setup(chip, irq_base);
874
	if (ret)
875
		goto err_exit;
876

877
	if (pdata && pdata->setup) {
878 879 880 881 882 883 884 885
		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;
886 887 888 889

err_exit:
	regulator_disable(chip->regulator);
	return ret;
890 891
}

892
static int pca953x_remove(struct i2c_client *client)
893
{
J
Jingoo Han 已提交
894
	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
895
	struct pca953x_chip *chip = i2c_get_clientdata(client);
896
	int ret;
897

898
	if (pdata && pdata->teardown) {
899 900
		ret = pdata->teardown(client, chip->gpio_chip.base,
				chip->gpio_chip.ngpio, pdata->context);
901
		if (ret < 0)
902 903
			dev_err(&client->dev, "%s failed, %d\n",
					"teardown", ret);
904 905
	} else {
		ret = 0;
906 907
	}

908 909 910
	regulator_disable(chip->regulator);

	return ret;
911 912
}

913 914 915 916
/* 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)

917
static const struct of_device_id pca953x_dt_ids[] = {
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	{ .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), },
937
	{ .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
938 939

	{ .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
940
	{ .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
941 942 943 944 945 946 947
	{ .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), },
948 949 950 951 952
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

953
static struct i2c_driver pca953x_driver = {
954
	.driver = {
955
		.name	= "pca953x",
956
		.of_match_table = pca953x_dt_ids,
957
		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
958
	},
959 960
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
961
	.id_table	= pca953x_id,
962 963
};

964
static int __init pca953x_init(void)
965
{
966
	return i2c_add_driver(&pca953x_driver);
967
}
968 969 970 971
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
972

973
static void __exit pca953x_exit(void)
974
{
975
	i2c_del_driver(&pca953x_driver);
976
}
977
module_exit(pca953x_exit);
978 979

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