gpio-pca953x.c 20.6 KB
Newer Older
1
/*
G
Grant Likely 已提交
2
 *  PCA953x 4/8/16 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 18
#include <linux/interrupt.h>
#include <linux/irq.h>
19
#include <linux/irqdomain.h>
20
#include <linux/i2c.h>
21
#include <linux/i2c/pca953x.h>
22
#include <linux/slab.h>
23 24 25
#ifdef CONFIG_OF_GPIO
#include <linux/of_platform.h>
#endif
26

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

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

34 35 36 37 38 39 40 41 42 43 44 45 46
#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
47

48
static const struct i2c_device_id pca953x_id[] = {
49 50 51 52 53 54 55 56 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, },

	{ "max7310", 8  | PCA953X_TYPE, },
	{ "max7312", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7313", 16 | PCA953X_TYPE | PCA_INT, },
	{ "max7315", 8  | PCA953X_TYPE | PCA_INT, },
	{ "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
	{ "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
A
Andreas Schallenberg 已提交
69
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
70
	{ }
71
};
72
MODULE_DEVICE_TABLE(i2c, pca953x_id);
73

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

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

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

85 86
#ifdef CONFIG_GPIO_PCA953X_IRQ
	struct mutex irq_lock;
87 88 89 90
	u8 irq_mask[MAX_BANK];
	u8 irq_stat[MAX_BANK];
	u8 irq_trig_raise[MAX_BANK];
	u8 irq_trig_fall[MAX_BANK];
91
	int	 irq_base;
92
	struct irq_domain *domain;
93 94
#endif

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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)
139
{
140
	int ret = 0;
141 142

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

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

	return 0;
174 175
}

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

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

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

	return 0;
}

202
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
203
{
204
	struct pca953x_chip *chip;
205
	u8 reg_val;
206
	int ret, offset = 0;
207

208
	chip = container_of(gc, struct pca953x_chip, gpio_chip);
209

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

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

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

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

239
	chip = container_of(gc, struct pca953x_chip, gpio_chip);
240

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

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

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

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

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

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

291
	chip = container_of(gc, struct pca953x_chip, gpio_chip);
292

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

	return (reg_val & (1u << off)) ? 1 : 0;
}

315
static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
316
{
317
	struct pca953x_chip *chip;
318
	u8 reg_val;
319
	int ret, offset = 0;
320

321
	chip = container_of(gc, struct pca953x_chip, gpio_chip);
322

323
	mutex_lock(&chip->i2c_lock);
324
	if (val)
325 326
		reg_val = chip->reg_output[off / BANK_SZ]
			| (1u << (off % BANK_SZ));
327
	else
328 329
		reg_val = chip->reg_output[off / BANK_SZ]
			& ~(1u << (off % BANK_SZ));
330

331 332 333 334 335 336 337 338
	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_OUTPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_OUT;
		break;
	}
339
	ret = pca953x_write_single(chip, offset, reg_val, off);
340
	if (ret)
341
		goto exit;
342

343
	chip->reg_output[off / BANK_SZ] = reg_val;
344 345
exit:
	mutex_unlock(&chip->i2c_lock);
346 347
}

348
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
349 350 351 352 353
{
	struct gpio_chip *gc;

	gc = &chip->gpio_chip;

354 355 356 357
	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;
358
	gc->can_sleep = 1;
359 360

	gc->base = chip->gpio_start;
361 362
	gc->ngpio = gpios;
	gc->label = chip->client->name;
D
David Brownell 已提交
363
	gc->dev = &chip->client->dev;
364
	gc->owner = THIS_MODULE;
365
	gc->names = chip->names;
366 367
}

368 369 370 371 372 373 374 375 376
#ifdef CONFIG_GPIO_PCA953X_IRQ
static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
{
	struct pca953x_chip *chip;

	chip = container_of(gc, struct pca953x_chip, gpio_chip);
	return chip->irq_base + off;
}

377
static void pca953x_irq_mask(struct irq_data *d)
378
{
379
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
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
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
387

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

391
static void pca953x_irq_bus_lock(struct irq_data *d)
392
{
393
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
394 395 396 397

	mutex_lock(&chip->irq_lock);
}

398
static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
399
{
400
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
401 402
	u8 new_irqs;
	int level, i;
403 404

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

	mutex_unlock(&chip->irq_lock);
}

420
static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
421
{
422
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
423 424
	int bank_nb = d->hwirq / BANK_SZ;
	u8 mask = 1 << (d->hwirq % BANK_SZ);
425 426 427

	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
428
			d->irq, type);
429 430 431 432
		return -EINVAL;
	}

	if (type & IRQ_TYPE_EDGE_FALLING)
433
		chip->irq_trig_fall[bank_nb] |= mask;
434
	else
435
		chip->irq_trig_fall[bank_nb] &= ~mask;
436 437

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

442
	return 0;
443 444 445 446
}

static struct irq_chip pca953x_irq_chip = {
	.name			= "pca953x",
447 448 449 450 451
	.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,
452 453
};

454
static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
455
{
456 457 458 459 460
	u8 cur_stat[MAX_BANK];
	u8 old_stat[MAX_BANK];
	u8 pendings = 0;
	u8 trigger[MAX_BANK], triggers = 0;
	int ret, i, offset = 0;
461 462 463 464 465 466 467 468 469

	switch (chip->chip_type) {
	case PCA953X_TYPE:
		offset = PCA953X_INPUT;
		break;
	case PCA957X_TYPE:
		offset = PCA957X_IN;
		break;
	}
470
	ret = pca953x_read_regs(chip, offset, cur_stat);
471 472 473 474
	if (ret)
		return 0;

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

478
	memcpy(old_stat, chip->irq_stat, NBANK(chip));
479

480 481 482 483 484 485
	for (i = 0; i < NBANK(chip); i++) {
		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
		triggers += trigger[i];
	}

	if (!triggers)
486 487
		return 0;

488
	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
489

490 491 492 493 494 495
	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];
		pendings += pending[i];
	}
496

497
	return pendings;
498 499 500 501 502
}

static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
	struct pca953x_chip *chip = devid;
503 504 505
	u8 pending[MAX_BANK];
	u8 level;
	int i;
506

507
	if (!pca953x_irq_pending(chip, pending))
508 509
		return IRQ_HANDLED;

510 511 512 513 514 515 516 517
	for (i = 0; i < NBANK(chip); i++) {
		while (pending[i]) {
			level = __ffs(pending[i]);
			handle_nested_irq(irq_find_mapping(chip->domain,
							level + (BANK_SZ * i)));
			pending[i] &= ~(1 << level);
		}
	}
518 519 520 521 522

	return IRQ_HANDLED;
}

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

529
	if (irq_base != -1
530
			&& (id->driver_data & PCA_INT)) {
531 532
		int lvl;

533 534 535 536 537 538 539 540
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			offset = PCA953X_INPUT;
			break;
		case PCA957X_TYPE:
			offset = PCA957X_IN;
			break;
		}
541
		ret = pca953x_read_regs(chip, offset, chip->irq_stat);
542 543 544 545 546 547 548 549
		if (ret)
			goto out_failed;

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

554
		chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
D
David Jander 已提交
555 556 557
		if (chip->irq_base < 0)
			goto out_failed;

558 559 560 561 562 563 564 565 566 567 568
		chip->domain = irq_domain_add_legacy(client->dev.of_node,
						chip->gpio_chip.ngpio,
						chip->irq_base,
						0,
						&irq_domain_simple_ops,
						NULL);
		if (!chip->domain) {
			ret = -ENODEV;
			goto out_irqdesc_free;
		}

569 570 571
		for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
			int irq = lvl + chip->irq_base;

D
David Jander 已提交
572
			irq_clear_status_flags(irq, IRQ_NOREQUEST);
T
Thomas Gleixner 已提交
573
			irq_set_chip_data(irq, chip);
574 575
			irq_set_chip(irq, &pca953x_irq_chip);
			irq_set_nested_thread(irq, true);
576 577 578
#ifdef CONFIG_ARM
			set_irq_flags(irq, IRQF_VALID);
#else
T
Thomas Gleixner 已提交
579
			irq_set_noprobe(irq);
580 581 582 583 584 585
#endif
		}

		ret = request_threaded_irq(client->irq,
					   NULL,
					   pca953x_irq_handler,
586
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
587 588 589 590
					   dev_name(&client->dev), chip);
		if (ret) {
			dev_err(&client->dev, "failed to request irq %d\n",
				client->irq);
591
			goto out_irqdesc_free;
592 593 594 595 596 597 598
		}

		chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
	}

	return 0;

599 600
out_irqdesc_free:
	irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
601
out_failed:
602
	chip->irq_base = -1;
603 604 605 606 607
	return ret;
}

static void pca953x_irq_teardown(struct pca953x_chip *chip)
{
D
David Jander 已提交
608 609
	if (chip->irq_base != -1) {
		irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
610
		free_irq(chip->client->irq, chip);
D
David Jander 已提交
611
	}
612 613 614
}
#else /* CONFIG_GPIO_PCA953X_IRQ */
static int pca953x_irq_setup(struct pca953x_chip *chip,
615 616
			     const struct i2c_device_id *id,
			     int irq_base)
617 618 619
{
	struct i2c_client *client = chip->client;

620
	if (irq_base != -1 && (id->driver_data & PCA_INT))
621 622 623 624 625 626 627 628 629 630
		dev_warn(&client->dev, "interrupt support not compiled in\n");

	return 0;
}

static void pca953x_irq_teardown(struct pca953x_chip *chip)
{
}
#endif

631 632 633 634 635 636
/*
 * Handlers for alternative sources of platform_data
 */
#ifdef CONFIG_OF_GPIO
/*
 * Translate OpenFirmware node properties into platform_data
637
 * WARNING: This is DEPRECATED and will be removed eventually!
638
 */
639
static void
640
pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
641 642
{
	struct device_node *node;
643 644
	const __be32 *val;
	int size;
645

646
	node = client->dev.of_node;
647
	if (node == NULL)
648
		return;
649

650
	*gpio_base = -1;
651
	val = of_get_property(node, "linux,gpio-base", &size);
652
	WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
653
	if (val) {
654 655 656
		if (size != sizeof(*val))
			dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
				 node->full_name);
657
		else
658
			*gpio_base = be32_to_cpup(val);
659 660 661
	}

	val = of_get_property(node, "polarity", NULL);
662
	WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
663
	if (val)
664
		*invert = *val;
665 666
}
#else
667
static void
668
pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
669
{
H
Hartmut Knaack 已提交
670
	*gpio_base = -1;
671 672 673
}
#endif

B
Bill Pemberton 已提交
674
static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
675 676
{
	int ret;
677
	u8 val[MAX_BANK];
678

679
	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
680 681 682
	if (ret)
		goto out;

683 684
	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
			       chip->reg_direction);
685 686 687 688
	if (ret)
		goto out;

	/* set platform specific polarity inversion */
689 690 691 692 693 694
	if (invert)
		memset(val, 0xFF, NBANK(chip));
	else
		memset(val, 0, NBANK(chip));

	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
695 696 697 698
out:
	return ret;
}

B
Bill Pemberton 已提交
699
static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
700 701
{
	int ret;
702
	u8 val[MAX_BANK];
703 704

	/* Let every port in proper state, that could save power */
705 706 707 708 709 710 711 712
	memset(val, 0, NBANK(chip));
	pca953x_write_regs(chip, PCA957X_PUPD, val);
	memset(val, 0xFF, NBANK(chip));
	pca953x_write_regs(chip, PCA957X_CFG, val);
	memset(val, 0, NBANK(chip));
	pca953x_write_regs(chip, PCA957X_OUT, val);

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

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

	/* To enable register 6, 7 to controll pull up and pull down */
730 731
	memset(val, 0x02, NBANK(chip));
	pca953x_write_regs(chip, PCA957X_BKEN, val);
732 733 734 735 736 737

	return 0;
out:
	return ret;
}

B
Bill Pemberton 已提交
738
static int pca953x_probe(struct i2c_client *client,
739
				   const struct i2c_device_id *id)
740
{
741 742
	struct pca953x_platform_data *pdata;
	struct pca953x_chip *chip;
743
	int irq_base = 0;
744
	int ret;
745
	u32 invert = 0;
746

747 748 749 750
	chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
	if (chip == NULL)
		return -ENOMEM;

751
	pdata = client->dev.platform_data;
752 753 754 755 756 757 758
	if (pdata) {
		irq_base = pdata->irq_base;
		chip->gpio_start = pdata->gpio_base;
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
		pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
759 760 761 762 763
#ifdef CONFIG_OF_GPIO
		/* If I2C node has no interrupts property, disable GPIO interrupts */
		if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
			irq_base = -1;
#endif
764
	}
765 766 767

	chip->client = client;

768
	chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
769

770 771
	mutex_init(&chip->i2c_lock);

772 773 774
	/* initialize cached registers from their original values.
	 * we can't share this chip with another i2c master.
	 */
775
	pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
776

777
	if (chip->chip_type == PCA953X_TYPE)
778
		ret = device_pca953x_init(chip, invert);
779
	else
780 781
		ret = device_pca957x_init(chip, invert);
	if (ret)
782 783
		goto out_failed;

784
	ret = pca953x_irq_setup(chip, id, irq_base);
785 786
	if (ret)
		goto out_failed;
787 788

	ret = gpiochip_add(&chip->gpio_chip);
789
	if (ret)
790
		goto out_failed_irq;
791

792
	if (pdata && pdata->setup) {
793 794 795 796 797 798 799 800 801
		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;

802
out_failed_irq:
803
	pca953x_irq_teardown(chip);
804
out_failed:
805 806 807 808
	kfree(chip);
	return ret;
}

809
static int pca953x_remove(struct i2c_client *client)
810
{
811 812
	struct pca953x_platform_data *pdata = client->dev.platform_data;
	struct pca953x_chip *chip = i2c_get_clientdata(client);
813 814
	int ret = 0;

815
	if (pdata && pdata->teardown) {
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
		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;
		}
	}

	ret = gpiochip_remove(&chip->gpio_chip);
	if (ret) {
		dev_err(&client->dev, "%s failed, %d\n",
				"gpiochip_remove()", ret);
		return ret;
	}

832
	pca953x_irq_teardown(chip);
833 834 835 836
	kfree(chip);
	return 0;
}

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
static const struct of_device_id pca953x_dt_ids[] = {
	{ .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", },

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

	{ .compatible = "ti,pca6107", },
	{ .compatible = "ti,tca6408", },
	{ .compatible = "ti,tca6416", },
	{ .compatible = "ti,tca6424", },
	{ }
};

MODULE_DEVICE_TABLE(of, pca953x_dt_ids);

865
static struct i2c_driver pca953x_driver = {
866
	.driver = {
867
		.name	= "pca953x",
868
		.of_match_table = pca953x_dt_ids,
869
	},
870 871
	.probe		= pca953x_probe,
	.remove		= pca953x_remove,
872
	.id_table	= pca953x_id,
873 874
};

875
static int __init pca953x_init(void)
876
{
877
	return i2c_add_driver(&pca953x_driver);
878
}
879 880 881 882
/* register after i2c postcore initcall and before
 * subsys initcalls that may rely on these GPIOs
 */
subsys_initcall(pca953x_init);
883

884
static void __exit pca953x_exit(void)
885
{
886
	i2c_del_driver(&pca953x_driver);
887
}
888
module_exit(pca953x_exit);
889 890

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