xref: /openbmc/linux/drivers/gpio/gpio-pca953x.c (revision 9cfc5c90)
1 /*
2  *  PCA953x 4/8/16/24/40 bit I/O ports
3  *
4  *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5  *  Copyright (C) 2007 Marvell International Ltd.
6  *
7  *  Derived from drivers/i2c/chips/pca9539.c
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  */
13 
14 #include <linux/module.h>
15 #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 #ifdef CONFIG_OF_GPIO
22 #include <linux/of_platform.h>
23 #endif
24 #include <linux/acpi.h>
25 
26 #define PCA953X_INPUT		0
27 #define PCA953X_OUTPUT		1
28 #define PCA953X_INVERT		2
29 #define PCA953X_DIRECTION	3
30 
31 #define REG_ADDR_AI		0x80
32 
33 #define PCA957X_IN		0
34 #define PCA957X_INVRT		1
35 #define PCA957X_BKEN		2
36 #define PCA957X_PUPD		3
37 #define PCA957X_CFG		4
38 #define PCA957X_OUT		5
39 #define PCA957X_MSK		6
40 #define PCA957X_INTS		7
41 
42 #define PCA_GPIO_MASK		0x00FF
43 #define PCA_INT			0x0100
44 #define PCA953X_TYPE		0x1000
45 #define PCA957X_TYPE		0x2000
46 #define PCA_TYPE_MASK		0xF000
47 
48 #define PCA_CHIP_TYPE(x)	((x) & PCA_TYPE_MASK)
49 
50 static const struct i2c_device_id pca953x_id[] = {
51 	{ "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
52 	{ "pca9534", 8  | PCA953X_TYPE | PCA_INT, },
53 	{ "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
54 	{ "pca9536", 4  | PCA953X_TYPE, },
55 	{ "pca9537", 4  | PCA953X_TYPE | PCA_INT, },
56 	{ "pca9538", 8  | PCA953X_TYPE | PCA_INT, },
57 	{ "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
58 	{ "pca9554", 8  | PCA953X_TYPE | PCA_INT, },
59 	{ "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
60 	{ "pca9556", 8  | PCA953X_TYPE, },
61 	{ "pca9557", 8  | PCA953X_TYPE, },
62 	{ "pca9574", 8  | PCA957X_TYPE | PCA_INT, },
63 	{ "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
64 	{ "pca9698", 40 | PCA953X_TYPE, },
65 
66 	{ "max7310", 8  | PCA953X_TYPE, },
67 	{ "max7312", 16 | PCA953X_TYPE | PCA_INT, },
68 	{ "max7313", 16 | PCA953X_TYPE | PCA_INT, },
69 	{ "max7315", 8  | PCA953X_TYPE | PCA_INT, },
70 	{ "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
71 	{ "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
72 	{ "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
73 	{ "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
74 	{ "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
75 	{ "xra1202", 8  | PCA953X_TYPE },
76 	{ }
77 };
78 MODULE_DEVICE_TABLE(i2c, pca953x_id);
79 
80 static const struct acpi_device_id pca953x_acpi_ids[] = {
81 	{ "INT3491", 16 | PCA953X_TYPE | PCA_INT, },
82 	{ }
83 };
84 MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
85 
86 #define MAX_BANK 5
87 #define BANK_SZ 8
88 
89 #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
90 
91 struct pca953x_chip {
92 	unsigned gpio_start;
93 	u8 reg_output[MAX_BANK];
94 	u8 reg_direction[MAX_BANK];
95 	struct mutex i2c_lock;
96 
97 #ifdef CONFIG_GPIO_PCA953X_IRQ
98 	struct mutex irq_lock;
99 	u8 irq_mask[MAX_BANK];
100 	u8 irq_stat[MAX_BANK];
101 	u8 irq_trig_raise[MAX_BANK];
102 	u8 irq_trig_fall[MAX_BANK];
103 #endif
104 
105 	struct i2c_client *client;
106 	struct gpio_chip gpio_chip;
107 	const char *const *names;
108 	int	chip_type;
109 	unsigned long driver_data;
110 };
111 
112 static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
113 {
114 	return container_of(gc, struct pca953x_chip, gpio_chip);
115 }
116 
117 static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
118 				int off)
119 {
120 	int ret;
121 	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
122 	int offset = off / BANK_SZ;
123 
124 	ret = i2c_smbus_read_byte_data(chip->client,
125 				(reg << bank_shift) + offset);
126 	*val = ret;
127 
128 	if (ret < 0) {
129 		dev_err(&chip->client->dev, "failed reading register\n");
130 		return ret;
131 	}
132 
133 	return 0;
134 }
135 
136 static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
137 				int off)
138 {
139 	int ret = 0;
140 	int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
141 	int offset = off / BANK_SZ;
142 
143 	ret = i2c_smbus_write_byte_data(chip->client,
144 					(reg << bank_shift) + offset, val);
145 
146 	if (ret < 0) {
147 		dev_err(&chip->client->dev, "failed writing register\n");
148 		return ret;
149 	}
150 
151 	return 0;
152 }
153 
154 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
155 {
156 	int ret = 0;
157 
158 	if (chip->gpio_chip.ngpio <= 8)
159 		ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
160 	else if (chip->gpio_chip.ngpio >= 24) {
161 		int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
162 		ret = i2c_smbus_write_i2c_block_data(chip->client,
163 					(reg << bank_shift) | REG_ADDR_AI,
164 					NBANK(chip), val);
165 	} else {
166 		switch (chip->chip_type) {
167 		case PCA953X_TYPE:
168 			ret = i2c_smbus_write_word_data(chip->client,
169 							reg << 1, (u16) *val);
170 			break;
171 		case PCA957X_TYPE:
172 			ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
173 							val[0]);
174 			if (ret < 0)
175 				break;
176 			ret = i2c_smbus_write_byte_data(chip->client,
177 							(reg << 1) + 1,
178 							val[1]);
179 			break;
180 		}
181 	}
182 
183 	if (ret < 0) {
184 		dev_err(&chip->client->dev, "failed writing register\n");
185 		return ret;
186 	}
187 
188 	return 0;
189 }
190 
191 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
192 {
193 	int ret;
194 
195 	if (chip->gpio_chip.ngpio <= 8) {
196 		ret = i2c_smbus_read_byte_data(chip->client, reg);
197 		*val = ret;
198 	} else if (chip->gpio_chip.ngpio >= 24) {
199 		int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
200 
201 		ret = i2c_smbus_read_i2c_block_data(chip->client,
202 					(reg << bank_shift) | REG_ADDR_AI,
203 					NBANK(chip), val);
204 	} else {
205 		ret = i2c_smbus_read_word_data(chip->client, reg << 1);
206 		val[0] = (u16)ret & 0xFF;
207 		val[1] = (u16)ret >> 8;
208 	}
209 	if (ret < 0) {
210 		dev_err(&chip->client->dev, "failed reading register\n");
211 		return ret;
212 	}
213 
214 	return 0;
215 }
216 
217 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
218 {
219 	struct pca953x_chip *chip = to_pca(gc);
220 	u8 reg_val;
221 	int ret, offset = 0;
222 
223 	mutex_lock(&chip->i2c_lock);
224 	reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
225 
226 	switch (chip->chip_type) {
227 	case PCA953X_TYPE:
228 		offset = PCA953X_DIRECTION;
229 		break;
230 	case PCA957X_TYPE:
231 		offset = PCA957X_CFG;
232 		break;
233 	}
234 	ret = pca953x_write_single(chip, offset, reg_val, off);
235 	if (ret)
236 		goto exit;
237 
238 	chip->reg_direction[off / BANK_SZ] = reg_val;
239 	ret = 0;
240 exit:
241 	mutex_unlock(&chip->i2c_lock);
242 	return ret;
243 }
244 
245 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
246 		unsigned off, int val)
247 {
248 	struct pca953x_chip *chip = to_pca(gc);
249 	u8 reg_val;
250 	int ret, offset = 0;
251 
252 	mutex_lock(&chip->i2c_lock);
253 	/* set output level */
254 	if (val)
255 		reg_val = chip->reg_output[off / BANK_SZ]
256 			| (1u << (off % BANK_SZ));
257 	else
258 		reg_val = chip->reg_output[off / BANK_SZ]
259 			& ~(1u << (off % BANK_SZ));
260 
261 	switch (chip->chip_type) {
262 	case PCA953X_TYPE:
263 		offset = PCA953X_OUTPUT;
264 		break;
265 	case PCA957X_TYPE:
266 		offset = PCA957X_OUT;
267 		break;
268 	}
269 	ret = pca953x_write_single(chip, offset, reg_val, off);
270 	if (ret)
271 		goto exit;
272 
273 	chip->reg_output[off / BANK_SZ] = reg_val;
274 
275 	/* then direction */
276 	reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
277 	switch (chip->chip_type) {
278 	case PCA953X_TYPE:
279 		offset = PCA953X_DIRECTION;
280 		break;
281 	case PCA957X_TYPE:
282 		offset = PCA957X_CFG;
283 		break;
284 	}
285 	ret = pca953x_write_single(chip, offset, reg_val, off);
286 	if (ret)
287 		goto exit;
288 
289 	chip->reg_direction[off / BANK_SZ] = reg_val;
290 	ret = 0;
291 exit:
292 	mutex_unlock(&chip->i2c_lock);
293 	return ret;
294 }
295 
296 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
297 {
298 	struct pca953x_chip *chip = to_pca(gc);
299 	u32 reg_val;
300 	int ret, offset = 0;
301 
302 	mutex_lock(&chip->i2c_lock);
303 	switch (chip->chip_type) {
304 	case PCA953X_TYPE:
305 		offset = PCA953X_INPUT;
306 		break;
307 	case PCA957X_TYPE:
308 		offset = PCA957X_IN;
309 		break;
310 	}
311 	ret = pca953x_read_single(chip, offset, &reg_val, off);
312 	mutex_unlock(&chip->i2c_lock);
313 	if (ret < 0) {
314 		/* NOTE:  diagnostic already emitted; that's all we should
315 		 * do unless gpio_*_value_cansleep() calls become different
316 		 * from their nonsleeping siblings (and report faults).
317 		 */
318 		return 0;
319 	}
320 
321 	return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
322 }
323 
324 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
325 {
326 	struct pca953x_chip *chip = to_pca(gc);
327 	u8 reg_val;
328 	int ret, offset = 0;
329 
330 	mutex_lock(&chip->i2c_lock);
331 	if (val)
332 		reg_val = chip->reg_output[off / BANK_SZ]
333 			| (1u << (off % BANK_SZ));
334 	else
335 		reg_val = chip->reg_output[off / BANK_SZ]
336 			& ~(1u << (off % BANK_SZ));
337 
338 	switch (chip->chip_type) {
339 	case PCA953X_TYPE:
340 		offset = PCA953X_OUTPUT;
341 		break;
342 	case PCA957X_TYPE:
343 		offset = PCA957X_OUT;
344 		break;
345 	}
346 	ret = pca953x_write_single(chip, offset, reg_val, off);
347 	if (ret)
348 		goto exit;
349 
350 	chip->reg_output[off / BANK_SZ] = reg_val;
351 exit:
352 	mutex_unlock(&chip->i2c_lock);
353 }
354 
355 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
356 {
357 	struct gpio_chip *gc;
358 
359 	gc = &chip->gpio_chip;
360 
361 	gc->direction_input  = pca953x_gpio_direction_input;
362 	gc->direction_output = pca953x_gpio_direction_output;
363 	gc->get = pca953x_gpio_get_value;
364 	gc->set = pca953x_gpio_set_value;
365 	gc->can_sleep = true;
366 
367 	gc->base = chip->gpio_start;
368 	gc->ngpio = gpios;
369 	gc->label = chip->client->name;
370 	gc->dev = &chip->client->dev;
371 	gc->owner = THIS_MODULE;
372 	gc->names = chip->names;
373 }
374 
375 #ifdef CONFIG_GPIO_PCA953X_IRQ
376 static void pca953x_irq_mask(struct irq_data *d)
377 {
378 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
379 	struct pca953x_chip *chip = to_pca(gc);
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 gpio_chip *gc = irq_data_get_irq_chip_data(d);
387 	struct pca953x_chip *chip = to_pca(gc);
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 gpio_chip *gc = irq_data_get_irq_chip_data(d);
395 	struct pca953x_chip *chip = to_pca(gc);
396 
397 	mutex_lock(&chip->irq_lock);
398 }
399 
400 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
401 {
402 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
403 	struct pca953x_chip *chip = to_pca(gc);
404 	u8 new_irqs;
405 	int level, i;
406 
407 	/* Look for any newly setup interrupt */
408 	for (i = 0; i < NBANK(chip); i++) {
409 		new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
410 		new_irqs &= ~chip->reg_direction[i];
411 
412 		while (new_irqs) {
413 			level = __ffs(new_irqs);
414 			pca953x_gpio_direction_input(&chip->gpio_chip,
415 							level + (BANK_SZ * i));
416 			new_irqs &= ~(1 << level);
417 		}
418 	}
419 
420 	mutex_unlock(&chip->irq_lock);
421 }
422 
423 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
424 {
425 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
426 	struct pca953x_chip *chip = to_pca(gc);
427 	int bank_nb = d->hwirq / BANK_SZ;
428 	u8 mask = 1 << (d->hwirq % BANK_SZ);
429 
430 	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
431 		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
432 			d->irq, type);
433 		return -EINVAL;
434 	}
435 
436 	if (type & IRQ_TYPE_EDGE_FALLING)
437 		chip->irq_trig_fall[bank_nb] |= mask;
438 	else
439 		chip->irq_trig_fall[bank_nb] &= ~mask;
440 
441 	if (type & IRQ_TYPE_EDGE_RISING)
442 		chip->irq_trig_raise[bank_nb] |= mask;
443 	else
444 		chip->irq_trig_raise[bank_nb] &= ~mask;
445 
446 	return 0;
447 }
448 
449 static struct irq_chip pca953x_irq_chip = {
450 	.name			= "pca953x",
451 	.irq_mask		= pca953x_irq_mask,
452 	.irq_unmask		= pca953x_irq_unmask,
453 	.irq_bus_lock		= pca953x_irq_bus_lock,
454 	.irq_bus_sync_unlock	= pca953x_irq_bus_sync_unlock,
455 	.irq_set_type		= pca953x_irq_set_type,
456 };
457 
458 static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
459 {
460 	u8 cur_stat[MAX_BANK];
461 	u8 old_stat[MAX_BANK];
462 	bool pending_seen = false;
463 	bool trigger_seen = false;
464 	u8 trigger[MAX_BANK];
465 	int ret, i, offset = 0;
466 
467 	switch (chip->chip_type) {
468 	case PCA953X_TYPE:
469 		offset = PCA953X_INPUT;
470 		break;
471 	case PCA957X_TYPE:
472 		offset = PCA957X_IN;
473 		break;
474 	}
475 	ret = pca953x_read_regs(chip, offset, cur_stat);
476 	if (ret)
477 		return false;
478 
479 	/* Remove output pins from the equation */
480 	for (i = 0; i < NBANK(chip); i++)
481 		cur_stat[i] &= chip->reg_direction[i];
482 
483 	memcpy(old_stat, chip->irq_stat, NBANK(chip));
484 
485 	for (i = 0; i < NBANK(chip); i++) {
486 		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
487 		if (trigger[i])
488 			trigger_seen = true;
489 	}
490 
491 	if (!trigger_seen)
492 		return false;
493 
494 	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
495 
496 	for (i = 0; i < NBANK(chip); i++) {
497 		pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
498 			(cur_stat[i] & chip->irq_trig_raise[i]);
499 		pending[i] &= trigger[i];
500 		if (pending[i])
501 			pending_seen = true;
502 	}
503 
504 	return pending_seen;
505 }
506 
507 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
508 {
509 	struct pca953x_chip *chip = devid;
510 	u8 pending[MAX_BANK];
511 	u8 level;
512 	unsigned nhandled = 0;
513 	int i;
514 
515 	if (!pca953x_irq_pending(chip, pending))
516 		return IRQ_NONE;
517 
518 	for (i = 0; i < NBANK(chip); i++) {
519 		while (pending[i]) {
520 			level = __ffs(pending[i]);
521 			handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
522 							level + (BANK_SZ * i)));
523 			pending[i] &= ~(1 << level);
524 			nhandled++;
525 		}
526 	}
527 
528 	return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
529 }
530 
531 static int pca953x_irq_setup(struct pca953x_chip *chip,
532 			     int irq_base)
533 {
534 	struct i2c_client *client = chip->client;
535 	int ret, i, offset = 0;
536 
537 	if (client->irq && irq_base != -1
538 			&& (chip->driver_data & PCA_INT)) {
539 
540 		switch (chip->chip_type) {
541 		case PCA953X_TYPE:
542 			offset = PCA953X_INPUT;
543 			break;
544 		case PCA957X_TYPE:
545 			offset = PCA957X_IN;
546 			break;
547 		}
548 		ret = pca953x_read_regs(chip, offset, chip->irq_stat);
549 		if (ret)
550 			return ret;
551 
552 		/*
553 		 * There is no way to know which GPIO line generated the
554 		 * interrupt.  We have to rely on the previous read for
555 		 * this purpose.
556 		 */
557 		for (i = 0; i < NBANK(chip); i++)
558 			chip->irq_stat[i] &= chip->reg_direction[i];
559 		mutex_init(&chip->irq_lock);
560 
561 		ret = devm_request_threaded_irq(&client->dev,
562 					client->irq,
563 					   NULL,
564 					   pca953x_irq_handler,
565 					   IRQF_TRIGGER_LOW | IRQF_ONESHOT |
566 						   IRQF_SHARED,
567 					   dev_name(&client->dev), chip);
568 		if (ret) {
569 			dev_err(&client->dev, "failed to request irq %d\n",
570 				client->irq);
571 			return ret;
572 		}
573 
574 		ret =  gpiochip_irqchip_add(&chip->gpio_chip,
575 					    &pca953x_irq_chip,
576 					    irq_base,
577 					    handle_simple_irq,
578 					    IRQ_TYPE_NONE);
579 		if (ret) {
580 			dev_err(&client->dev,
581 				"could not connect irqchip to gpiochip\n");
582 			return ret;
583 		}
584 
585 		gpiochip_set_chained_irqchip(&chip->gpio_chip,
586 					     &pca953x_irq_chip,
587 					     client->irq, NULL);
588 	}
589 
590 	return 0;
591 }
592 
593 #else /* CONFIG_GPIO_PCA953X_IRQ */
594 static int pca953x_irq_setup(struct pca953x_chip *chip,
595 			     int irq_base)
596 {
597 	struct i2c_client *client = chip->client;
598 
599 	if (irq_base != -1 && (chip->driver_data & PCA_INT))
600 		dev_warn(&client->dev, "interrupt support not compiled in\n");
601 
602 	return 0;
603 }
604 #endif
605 
606 static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
607 {
608 	int ret;
609 	u8 val[MAX_BANK];
610 
611 	ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
612 	if (ret)
613 		goto out;
614 
615 	ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
616 			       chip->reg_direction);
617 	if (ret)
618 		goto out;
619 
620 	/* set platform specific polarity inversion */
621 	if (invert)
622 		memset(val, 0xFF, NBANK(chip));
623 	else
624 		memset(val, 0, NBANK(chip));
625 
626 	ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
627 out:
628 	return ret;
629 }
630 
631 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
632 {
633 	int ret;
634 	u8 val[MAX_BANK];
635 
636 	ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
637 	if (ret)
638 		goto out;
639 	ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
640 	if (ret)
641 		goto out;
642 
643 	/* set platform specific polarity inversion */
644 	if (invert)
645 		memset(val, 0xFF, NBANK(chip));
646 	else
647 		memset(val, 0, NBANK(chip));
648 	ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
649 	if (ret)
650 		goto out;
651 
652 	/* To enable register 6, 7 to control pull up and pull down */
653 	memset(val, 0x02, NBANK(chip));
654 	ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
655 	if (ret)
656 		goto out;
657 
658 	return 0;
659 out:
660 	return ret;
661 }
662 
663 static int pca953x_probe(struct i2c_client *client,
664 				   const struct i2c_device_id *id)
665 {
666 	struct pca953x_platform_data *pdata;
667 	struct pca953x_chip *chip;
668 	int irq_base = 0;
669 	int ret;
670 	u32 invert = 0;
671 
672 	chip = devm_kzalloc(&client->dev,
673 			sizeof(struct pca953x_chip), GFP_KERNEL);
674 	if (chip == NULL)
675 		return -ENOMEM;
676 
677 	pdata = dev_get_platdata(&client->dev);
678 	if (pdata) {
679 		irq_base = pdata->irq_base;
680 		chip->gpio_start = pdata->gpio_base;
681 		invert = pdata->invert;
682 		chip->names = pdata->names;
683 	} else {
684 		chip->gpio_start = -1;
685 		irq_base = 0;
686 	}
687 
688 	chip->client = client;
689 
690 	if (id) {
691 		chip->driver_data = id->driver_data;
692 	} else {
693 		const struct acpi_device_id *id;
694 
695 		id = acpi_match_device(pca953x_acpi_ids, &client->dev);
696 		if (!id)
697 			return -ENODEV;
698 
699 		chip->driver_data = id->driver_data;
700 	}
701 
702 	chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
703 
704 	mutex_init(&chip->i2c_lock);
705 
706 	/* initialize cached registers from their original values.
707 	 * we can't share this chip with another i2c master.
708 	 */
709 	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
710 
711 	if (chip->chip_type == PCA953X_TYPE)
712 		ret = device_pca953x_init(chip, invert);
713 	else
714 		ret = device_pca957x_init(chip, invert);
715 	if (ret)
716 		return ret;
717 
718 	ret = gpiochip_add(&chip->gpio_chip);
719 	if (ret)
720 		return ret;
721 
722 	ret = pca953x_irq_setup(chip, irq_base);
723 	if (ret)
724 		return ret;
725 
726 	if (pdata && pdata->setup) {
727 		ret = pdata->setup(client, chip->gpio_chip.base,
728 				chip->gpio_chip.ngpio, pdata->context);
729 		if (ret < 0)
730 			dev_warn(&client->dev, "setup failed, %d\n", ret);
731 	}
732 
733 	i2c_set_clientdata(client, chip);
734 	return 0;
735 }
736 
737 static int pca953x_remove(struct i2c_client *client)
738 {
739 	struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
740 	struct pca953x_chip *chip = i2c_get_clientdata(client);
741 	int ret = 0;
742 
743 	if (pdata && pdata->teardown) {
744 		ret = pdata->teardown(client, chip->gpio_chip.base,
745 				chip->gpio_chip.ngpio, pdata->context);
746 		if (ret < 0) {
747 			dev_err(&client->dev, "%s failed, %d\n",
748 					"teardown", ret);
749 			return ret;
750 		}
751 	}
752 
753 	gpiochip_remove(&chip->gpio_chip);
754 
755 	return 0;
756 }
757 
758 static const struct of_device_id pca953x_dt_ids[] = {
759 	{ .compatible = "nxp,pca9505", },
760 	{ .compatible = "nxp,pca9534", },
761 	{ .compatible = "nxp,pca9535", },
762 	{ .compatible = "nxp,pca9536", },
763 	{ .compatible = "nxp,pca9537", },
764 	{ .compatible = "nxp,pca9538", },
765 	{ .compatible = "nxp,pca9539", },
766 	{ .compatible = "nxp,pca9554", },
767 	{ .compatible = "nxp,pca9555", },
768 	{ .compatible = "nxp,pca9556", },
769 	{ .compatible = "nxp,pca9557", },
770 	{ .compatible = "nxp,pca9574", },
771 	{ .compatible = "nxp,pca9575", },
772 	{ .compatible = "nxp,pca9698", },
773 
774 	{ .compatible = "maxim,max7310", },
775 	{ .compatible = "maxim,max7312", },
776 	{ .compatible = "maxim,max7313", },
777 	{ .compatible = "maxim,max7315", },
778 
779 	{ .compatible = "ti,pca6107", },
780 	{ .compatible = "ti,tca6408", },
781 	{ .compatible = "ti,tca6416", },
782 	{ .compatible = "ti,tca6424", },
783 
784 	{ .compatible = "exar,xra1202", },
785 	{ }
786 };
787 
788 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
789 
790 static struct i2c_driver pca953x_driver = {
791 	.driver = {
792 		.name	= "pca953x",
793 		.of_match_table = pca953x_dt_ids,
794 		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
795 	},
796 	.probe		= pca953x_probe,
797 	.remove		= pca953x_remove,
798 	.id_table	= pca953x_id,
799 };
800 
801 static int __init pca953x_init(void)
802 {
803 	return i2c_add_driver(&pca953x_driver);
804 }
805 /* register after i2c postcore initcall and before
806  * subsys initcalls that may rely on these GPIOs
807  */
808 subsys_initcall(pca953x_init);
809 
810 static void __exit pca953x_exit(void)
811 {
812 	i2c_del_driver(&pca953x_driver);
813 }
814 module_exit(pca953x_exit);
815 
816 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
817 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
818 MODULE_LICENSE("GPL");
819