gpio-pca953x.c 20.7 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
	{ "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	{ "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 已提交
70
	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
71
	{ }
72
};
73
MODULE_DEVICE_TABLE(i2c, pca953x_id);
74

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

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

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

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

96 97
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
98
	const char *const *names;
99
	int	chip_type;
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 139
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)
140
{
141
	int ret = 0;
142 143

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

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

	return 0;
175 176
}

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	gc = &chip->gpio_chip;

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

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

369 370 371 372 373 374 375 376 377
#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;
}

378
static void pca953x_irq_mask(struct irq_data *d)
379
{
380
	struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
381

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

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

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

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

	mutex_lock(&chip->irq_lock);
}

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

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

	mutex_unlock(&chip->irq_lock);
}

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

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

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

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

443
	return 0;
444 445 446 447
}

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

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

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

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

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

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

	if (!triggers)
487 488
		return 0;

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

491 492 493 494 495 496
	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];
	}
497

498
	return pendings;
499 500 501 502 503
}

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

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

511 512 513 514 515 516 517 518
	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);
		}
	}
519 520 521 522 523

	return IRQ_HANDLED;
}

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

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

534 535 536 537 538 539 540 541
		switch (chip->chip_type) {
		case PCA953X_TYPE:
			offset = PCA953X_INPUT;
			break;
		case PCA957X_TYPE:
			offset = PCA957X_IN;
			break;
		}
542
		ret = pca953x_read_regs(chip, offset, chip->irq_stat);
543 544 545 546 547 548 549 550
		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.
		 */
551 552
		for (i = 0; i < NBANK(chip); i++)
			chip->irq_stat[i] &= chip->reg_direction[i];
553 554
		mutex_init(&chip->irq_lock);

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

559 560 561 562 563 564 565 566 567 568 569
		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;
		}

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

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

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

		chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
	}

	return 0;

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

	/* Let every port in proper state, that could save power */
706 707 708 709 710 711 712 713
	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);
714 715
	if (ret)
		goto out;
716
	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
717 718
	if (ret)
		goto out;
719
	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
720 721 722 723
	if (ret)
		goto out;

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

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

	return 0;
out:
	return ret;
}

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

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

752
	pdata = client->dev.platform_data;
753 754 755 756 757 758 759
	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);
760 761 762 763 764
#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
765
	}
766 767 768

	chip->client = client;

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

771 772
	mutex_init(&chip->i2c_lock);

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

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

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

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

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

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

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

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

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

838
static const struct of_device_id pca953x_dt_ids[] = {
839
	{ .compatible = "nxp,pca9505", },
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 865 866
	{ .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);

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

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

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

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