2 * PCA953x 4/8/16 bit I/O ports
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
7 * Derived from drivers/i2c/chips/pca9539.c
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.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/gpio.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_data/pca953x.h>
22 #include <linux/slab.h>
24 #include <linux/of_platform.h>
27 #define PCA953X_INPUT 0
28 #define PCA953X_OUTPUT 1
29 #define PCA953X_INVERT 2
30 #define PCA953X_DIRECTION 3
32 #define REG_ADDR_AI 0x80
35 #define PCA957X_INVRT 1
36 #define PCA957X_BKEN 2
37 #define PCA957X_PUPD 3
41 #define PCA957X_INTS 7
43 #define PCA_GPIO_MASK 0x00FF
44 #define PCA_INT 0x0100
45 #define PCA953X_TYPE 0x1000
46 #define PCA957X_TYPE 0x2000
48 static const struct i2c_device_id pca953x_id
[] = {
49 { "pca9505", 40 | PCA953X_TYPE
| PCA_INT
, },
50 { "pca9534", 8 | PCA953X_TYPE
| PCA_INT
, },
51 { "pca9535", 16 | PCA953X_TYPE
| PCA_INT
, },
52 { "pca9536", 4 | PCA953X_TYPE
, },
53 { "pca9537", 4 | PCA953X_TYPE
| PCA_INT
, },
54 { "pca9538", 8 | PCA953X_TYPE
| PCA_INT
, },
55 { "pca9539", 16 | PCA953X_TYPE
| PCA_INT
, },
56 { "pca9554", 8 | PCA953X_TYPE
| PCA_INT
, },
57 { "pca9555", 16 | PCA953X_TYPE
| PCA_INT
, },
58 { "pca9556", 8 | PCA953X_TYPE
, },
59 { "pca9557", 8 | PCA953X_TYPE
, },
60 { "pca9574", 8 | PCA957X_TYPE
| PCA_INT
, },
61 { "pca9575", 16 | PCA957X_TYPE
| PCA_INT
, },
63 { "max7310", 8 | PCA953X_TYPE
, },
64 { "max7312", 16 | PCA953X_TYPE
| PCA_INT
, },
65 { "max7313", 16 | PCA953X_TYPE
| PCA_INT
, },
66 { "max7315", 8 | PCA953X_TYPE
| PCA_INT
, },
67 { "pca6107", 8 | PCA953X_TYPE
| PCA_INT
, },
68 { "tca6408", 8 | PCA953X_TYPE
| PCA_INT
, },
69 { "tca6416", 16 | PCA953X_TYPE
| PCA_INT
, },
70 { "tca6424", 24 | PCA953X_TYPE
| PCA_INT
, },
73 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
78 #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
82 u8 reg_output
[MAX_BANK
];
83 u8 reg_direction
[MAX_BANK
];
84 struct mutex i2c_lock
;
86 #ifdef CONFIG_GPIO_PCA953X_IRQ
87 struct mutex irq_lock
;
88 u8 irq_mask
[MAX_BANK
];
89 u8 irq_stat
[MAX_BANK
];
90 u8 irq_trig_raise
[MAX_BANK
];
91 u8 irq_trig_fall
[MAX_BANK
];
92 struct irq_domain
*domain
;
95 struct i2c_client
*client
;
96 struct gpio_chip gpio_chip
;
97 const char *const *names
;
101 static int pca953x_read_single(struct pca953x_chip
*chip
, int reg
, u32
*val
,
105 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
106 int offset
= off
/ BANK_SZ
;
108 ret
= i2c_smbus_read_byte_data(chip
->client
,
109 (reg
<< bank_shift
) + offset
);
113 dev_err(&chip
->client
->dev
, "failed reading register\n");
120 static int pca953x_write_single(struct pca953x_chip
*chip
, int reg
, u32 val
,
124 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
125 int offset
= off
/ BANK_SZ
;
127 ret
= i2c_smbus_write_byte_data(chip
->client
,
128 (reg
<< bank_shift
) + offset
, val
);
131 dev_err(&chip
->client
->dev
, "failed writing register\n");
138 static int pca953x_write_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
142 if (chip
->gpio_chip
.ngpio
<= 8)
143 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
, *val
);
144 else if (chip
->gpio_chip
.ngpio
>= 24) {
145 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
146 ret
= i2c_smbus_write_i2c_block_data(chip
->client
,
147 (reg
<< bank_shift
) | REG_ADDR_AI
,
150 switch (chip
->chip_type
) {
152 ret
= i2c_smbus_write_word_data(chip
->client
,
153 reg
<< 1, (u16
) *val
);
156 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
<< 1,
160 ret
= i2c_smbus_write_byte_data(chip
->client
,
168 dev_err(&chip
->client
->dev
, "failed writing register\n");
175 static int pca953x_read_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
179 if (chip
->gpio_chip
.ngpio
<= 8) {
180 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
182 } else if (chip
->gpio_chip
.ngpio
>= 24) {
183 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
185 ret
= i2c_smbus_read_i2c_block_data(chip
->client
,
186 (reg
<< bank_shift
) | REG_ADDR_AI
,
189 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
190 val
[0] = (u16
)ret
& 0xFF;
191 val
[1] = (u16
)ret
>> 8;
194 dev_err(&chip
->client
->dev
, "failed reading register\n");
201 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
203 struct pca953x_chip
*chip
;
207 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
209 mutex_lock(&chip
->i2c_lock
);
210 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] | (1u << (off
% BANK_SZ
));
212 switch (chip
->chip_type
) {
214 offset
= PCA953X_DIRECTION
;
217 offset
= PCA957X_CFG
;
220 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
224 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
227 mutex_unlock(&chip
->i2c_lock
);
231 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
232 unsigned off
, int val
)
234 struct pca953x_chip
*chip
;
238 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
240 mutex_lock(&chip
->i2c_lock
);
241 /* set output level */
243 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
244 | (1u << (off
% BANK_SZ
));
246 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
247 & ~(1u << (off
% BANK_SZ
));
249 switch (chip
->chip_type
) {
251 offset
= PCA953X_OUTPUT
;
254 offset
= PCA957X_OUT
;
257 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
261 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
264 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] & ~(1u << (off
% BANK_SZ
));
265 switch (chip
->chip_type
) {
267 offset
= PCA953X_DIRECTION
;
270 offset
= PCA957X_CFG
;
273 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
277 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
280 mutex_unlock(&chip
->i2c_lock
);
284 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
286 struct pca953x_chip
*chip
;
290 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
292 mutex_lock(&chip
->i2c_lock
);
293 switch (chip
->chip_type
) {
295 offset
= PCA953X_INPUT
;
301 ret
= pca953x_read_single(chip
, offset
, ®_val
, off
);
302 mutex_unlock(&chip
->i2c_lock
);
304 /* NOTE: diagnostic already emitted; that's all we should
305 * do unless gpio_*_value_cansleep() calls become different
306 * from their nonsleeping siblings (and report faults).
311 return (reg_val
& (1u << (off
% BANK_SZ
))) ? 1 : 0;
314 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
316 struct pca953x_chip
*chip
;
320 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
322 mutex_lock(&chip
->i2c_lock
);
324 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
325 | (1u << (off
% BANK_SZ
));
327 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
328 & ~(1u << (off
% BANK_SZ
));
330 switch (chip
->chip_type
) {
332 offset
= PCA953X_OUTPUT
;
335 offset
= PCA957X_OUT
;
338 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
342 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
344 mutex_unlock(&chip
->i2c_lock
);
347 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
349 struct gpio_chip
*gc
;
351 gc
= &chip
->gpio_chip
;
353 gc
->direction_input
= pca953x_gpio_direction_input
;
354 gc
->direction_output
= pca953x_gpio_direction_output
;
355 gc
->get
= pca953x_gpio_get_value
;
356 gc
->set
= pca953x_gpio_set_value
;
357 gc
->can_sleep
= true;
359 gc
->base
= chip
->gpio_start
;
361 gc
->label
= chip
->client
->name
;
362 gc
->dev
= &chip
->client
->dev
;
363 gc
->owner
= THIS_MODULE
;
364 gc
->names
= chip
->names
;
367 #ifdef CONFIG_GPIO_PCA953X_IRQ
368 static int pca953x_gpio_to_irq(struct gpio_chip
*gc
, unsigned off
)
370 struct pca953x_chip
*chip
;
372 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
373 return irq_create_mapping(chip
->domain
, off
);
376 static void pca953x_irq_mask(struct irq_data
*d
)
378 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
380 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] &= ~(1 << (d
->hwirq
% BANK_SZ
));
383 static void pca953x_irq_unmask(struct irq_data
*d
)
385 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
387 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] |= 1 << (d
->hwirq
% BANK_SZ
);
390 static void pca953x_irq_bus_lock(struct irq_data
*d
)
392 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
394 mutex_lock(&chip
->irq_lock
);
397 static void pca953x_irq_bus_sync_unlock(struct irq_data
*d
)
399 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
403 /* Look for any newly setup interrupt */
404 for (i
= 0; i
< NBANK(chip
); i
++) {
405 new_irqs
= chip
->irq_trig_fall
[i
] | chip
->irq_trig_raise
[i
];
406 new_irqs
&= ~chip
->reg_direction
[i
];
409 level
= __ffs(new_irqs
);
410 pca953x_gpio_direction_input(&chip
->gpio_chip
,
411 level
+ (BANK_SZ
* i
));
412 new_irqs
&= ~(1 << level
);
416 mutex_unlock(&chip
->irq_lock
);
419 static int pca953x_irq_set_type(struct irq_data
*d
, unsigned int type
)
421 struct pca953x_chip
*chip
= irq_data_get_irq_chip_data(d
);
422 int bank_nb
= d
->hwirq
/ BANK_SZ
;
423 u8 mask
= 1 << (d
->hwirq
% BANK_SZ
);
425 if (!(type
& IRQ_TYPE_EDGE_BOTH
)) {
426 dev_err(&chip
->client
->dev
, "irq %d: unsupported type %d\n",
431 if (type
& IRQ_TYPE_EDGE_FALLING
)
432 chip
->irq_trig_fall
[bank_nb
] |= mask
;
434 chip
->irq_trig_fall
[bank_nb
] &= ~mask
;
436 if (type
& IRQ_TYPE_EDGE_RISING
)
437 chip
->irq_trig_raise
[bank_nb
] |= mask
;
439 chip
->irq_trig_raise
[bank_nb
] &= ~mask
;
444 static struct irq_chip pca953x_irq_chip
= {
446 .irq_mask
= pca953x_irq_mask
,
447 .irq_unmask
= pca953x_irq_unmask
,
448 .irq_bus_lock
= pca953x_irq_bus_lock
,
449 .irq_bus_sync_unlock
= pca953x_irq_bus_sync_unlock
,
450 .irq_set_type
= pca953x_irq_set_type
,
453 static u8
pca953x_irq_pending(struct pca953x_chip
*chip
, u8
*pending
)
455 u8 cur_stat
[MAX_BANK
];
456 u8 old_stat
[MAX_BANK
];
458 u8 trigger
[MAX_BANK
], triggers
= 0;
459 int ret
, i
, offset
= 0;
461 switch (chip
->chip_type
) {
463 offset
= PCA953X_INPUT
;
469 ret
= pca953x_read_regs(chip
, offset
, cur_stat
);
473 /* Remove output pins from the equation */
474 for (i
= 0; i
< NBANK(chip
); i
++)
475 cur_stat
[i
] &= chip
->reg_direction
[i
];
477 memcpy(old_stat
, chip
->irq_stat
, NBANK(chip
));
479 for (i
= 0; i
< NBANK(chip
); i
++) {
480 trigger
[i
] = (cur_stat
[i
] ^ old_stat
[i
]) & chip
->irq_mask
[i
];
481 triggers
+= trigger
[i
];
487 memcpy(chip
->irq_stat
, cur_stat
, NBANK(chip
));
489 for (i
= 0; i
< NBANK(chip
); i
++) {
490 pending
[i
] = (old_stat
[i
] & chip
->irq_trig_fall
[i
]) |
491 (cur_stat
[i
] & chip
->irq_trig_raise
[i
]);
492 pending
[i
] &= trigger
[i
];
493 pendings
+= pending
[i
];
499 static irqreturn_t
pca953x_irq_handler(int irq
, void *devid
)
501 struct pca953x_chip
*chip
= devid
;
502 u8 pending
[MAX_BANK
];
506 if (!pca953x_irq_pending(chip
, pending
))
509 for (i
= 0; i
< NBANK(chip
); i
++) {
511 level
= __ffs(pending
[i
]);
512 handle_nested_irq(irq_find_mapping(chip
->domain
,
513 level
+ (BANK_SZ
* i
)));
514 pending
[i
] &= ~(1 << level
);
521 static int pca953x_gpio_irq_map(struct irq_domain
*d
, unsigned int irq
,
522 irq_hw_number_t hwirq
)
524 irq_clear_status_flags(irq
, IRQ_NOREQUEST
);
525 irq_set_chip_data(irq
, d
->host_data
);
526 irq_set_chip(irq
, &pca953x_irq_chip
);
527 irq_set_nested_thread(irq
, true);
529 set_irq_flags(irq
, IRQF_VALID
);
531 irq_set_noprobe(irq
);
537 static const struct irq_domain_ops pca953x_irq_simple_ops
= {
538 .map
= pca953x_gpio_irq_map
,
539 .xlate
= irq_domain_xlate_twocell
,
542 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
543 const struct i2c_device_id
*id
,
546 struct i2c_client
*client
= chip
->client
;
547 int ret
, i
, offset
= 0;
550 && (id
->driver_data
& PCA_INT
)) {
552 switch (chip
->chip_type
) {
554 offset
= PCA953X_INPUT
;
560 ret
= pca953x_read_regs(chip
, offset
, chip
->irq_stat
);
565 * There is no way to know which GPIO line generated the
566 * interrupt. We have to rely on the previous read for
569 for (i
= 0; i
< NBANK(chip
); i
++)
570 chip
->irq_stat
[i
] &= chip
->reg_direction
[i
];
571 mutex_init(&chip
->irq_lock
);
573 chip
->domain
= irq_domain_add_simple(client
->dev
.of_node
,
574 chip
->gpio_chip
.ngpio
,
576 &pca953x_irq_simple_ops
,
581 ret
= devm_request_threaded_irq(&client
->dev
,
585 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
586 dev_name(&client
->dev
), chip
);
588 dev_err(&client
->dev
, "failed to request irq %d\n",
593 chip
->gpio_chip
.to_irq
= pca953x_gpio_to_irq
;
599 #else /* CONFIG_GPIO_PCA953X_IRQ */
600 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
601 const struct i2c_device_id
*id
,
604 struct i2c_client
*client
= chip
->client
;
606 if (irq_base
!= -1 && (id
->driver_data
& PCA_INT
))
607 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
614 * Handlers for alternative sources of platform_data
616 #ifdef CONFIG_OF_GPIO
618 * Translate OpenFirmware node properties into platform_data
619 * WARNING: This is DEPRECATED and will be removed eventually!
622 pca953x_get_alt_pdata(struct i2c_client
*client
, int *gpio_base
, u32
*invert
)
624 struct device_node
*node
;
628 node
= client
->dev
.of_node
;
633 val
= of_get_property(node
, "linux,gpio-base", &size
);
634 WARN(val
, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__
);
636 if (size
!= sizeof(*val
))
637 dev_warn(&client
->dev
, "%s: wrong linux,gpio-base\n",
640 *gpio_base
= be32_to_cpup(val
);
643 val
= of_get_property(node
, "polarity", NULL
);
644 WARN(val
, "%s: device-tree property 'polarity' is deprecated!", __func__
);
650 pca953x_get_alt_pdata(struct i2c_client
*client
, int *gpio_base
, u32
*invert
)
656 #if IS_ENABLED(CONFIG_IPE2ST_POWER)
657 /* We use sysfs interfaces for power management */
660 static ssize_t
pca953x_show_direction(struct device
*dev
,
661 struct device_attribute
*attr
, char *buf
)
663 struct i2c_client
*client
= to_i2c_client(dev
);
664 struct pca953x_chip
*chip
;
667 chip
= i2c_get_clientdata(client
);
668 for (i
= 0; i
< chip
->gpio_chip
.ngpio
/ 8 &&
669 i
< ARRAY_SIZE(chip
->reg_direction
); i
++)
670 ret
+= sprintf(buf
, "%x\n", chip
->reg_direction
[i
]);
674 static ssize_t
pca953x_store_direction(struct device
*dev
,
675 struct device_attribute
*attr
, const char *buf
, size_t count
)
677 struct i2c_client
*client
= to_i2c_client(dev
);
678 struct pca953x_chip
*chip
;
679 unsigned long reg_val
;
682 if ((strict_strtoul(buf
, 10, ®_val
) < 0) ||
686 chip
= i2c_get_clientdata(client
);
687 ret
= pca953x_write_single(chip
, PCA953X_DIRECTION
,
688 (unsigned short)reg_val
, 0);
690 printk (KERN_ERR
"Failed to write pca953x direction\n");
693 chip
->reg_direction
[0] = reg_val
;
698 static DEVICE_ATTR(direction
, S_IWUSR
| S_IRUGO
,
699 pca953x_show_direction
, pca953x_store_direction
);
702 static ssize_t
pca953x_show_output(struct device
*dev
,
703 struct device_attribute
*attr
, char *buf
)
705 struct i2c_client
*client
= to_i2c_client(dev
);
706 struct pca953x_chip
*chip
;
709 chip
= i2c_get_clientdata(client
);
710 for (i
= 0; i
< chip
->gpio_chip
.ngpio
/ 8 &&
711 i
< ARRAY_SIZE(chip
->reg_direction
); i
++)
712 ret
+= sprintf(buf
, "%x\n", chip
->reg_direction
[i
]);
716 static ssize_t
pca953x_store_output(struct device
*dev
,
717 struct device_attribute
*attr
, const char *buf
, size_t count
)
719 struct i2c_client
*client
= to_i2c_client(dev
);
720 struct pca953x_chip
*chip
;
724 if ((strict_strtoul(buf
, 10, &val
) < 0) ||
725 ((val
!= 1) && (val
!= 2) && (val
!= 4) && (val
!= 8)))
728 chip
= i2c_get_clientdata(client
);
729 ret
= pca953x_write_single(chip
, PCA953X_OUTPUT
, (u32
)val
, 0);
731 printk (KERN_ERR
"Failed to write pca953x output\n");
734 chip
->reg_output
[0] = val
;
739 static DEVICE_ATTR(output
, S_IWUSR
| S_IRUGO
,
740 pca953x_show_output
, pca953x_store_output
);
742 static struct attribute
*pca953x_attributes
[] = {
743 &dev_attr_direction
.attr
,
744 &dev_attr_output
.attr
,
748 static const struct attribute_group pca953x_attr_group
= {
749 .attrs
= pca953x_attributes
,
751 #endif /* IS_ENABLED(CONFIG_IPE2ST_POWER) */
753 static int device_pca953x_init(struct pca953x_chip
*chip
, u32 invert
)
758 ret
= pca953x_read_regs(chip
, PCA953X_OUTPUT
, chip
->reg_output
);
762 ret
= pca953x_read_regs(chip
, PCA953X_DIRECTION
,
763 chip
->reg_direction
);
767 /* set platform specific polarity inversion */
769 memset(val
, 0xFF, NBANK(chip
));
771 memset(val
, 0, NBANK(chip
));
773 ret
= pca953x_write_regs(chip
, PCA953X_INVERT
, val
);
778 static int device_pca957x_init(struct pca953x_chip
*chip
, u32 invert
)
783 ret
= pca953x_read_regs(chip
, PCA957X_OUT
, chip
->reg_output
);
786 ret
= pca953x_read_regs(chip
, PCA957X_CFG
, chip
->reg_direction
);
790 /* set platform specific polarity inversion */
792 memset(val
, 0xFF, NBANK(chip
));
794 memset(val
, 0, NBANK(chip
));
795 pca953x_write_regs(chip
, PCA957X_INVRT
, val
);
797 /* To enable register 6, 7 to controll pull up and pull down */
798 memset(val
, 0x02, NBANK(chip
));
799 pca953x_write_regs(chip
, PCA957X_BKEN
, val
);
806 static int pca953x_probe(struct i2c_client
*client
,
807 const struct i2c_device_id
*id
)
809 struct pca953x_platform_data
*pdata
;
810 struct pca953x_chip
*chip
;
814 #if IS_ENABLED(CONFIG_IPE2ST_POWER)
815 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
817 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
821 chip
= devm_kzalloc(&client
->dev
,
822 sizeof(struct pca953x_chip
), GFP_KERNEL
);
826 pdata
= dev_get_platdata(&client
->dev
);
828 irq_base
= pdata
->irq_base
;
829 chip
->gpio_start
= pdata
->gpio_base
;
830 invert
= pdata
->invert
;
831 chip
->names
= pdata
->names
;
833 pca953x_get_alt_pdata(client
, &chip
->gpio_start
, &invert
);
834 #ifdef CONFIG_OF_GPIO
835 /* If I2C node has no interrupts property, disable GPIO interrupts */
836 if (of_find_property(client
->dev
.of_node
, "interrupts", NULL
) == NULL
)
841 chip
->client
= client
;
843 chip
->chip_type
= id
->driver_data
& (PCA953X_TYPE
| PCA957X_TYPE
);
845 mutex_init(&chip
->i2c_lock
);
847 /* initialize cached registers from their original values.
848 * we can't share this chip with another i2c master.
850 pca953x_setup_gpio(chip
, id
->driver_data
& PCA_GPIO_MASK
);
852 if (chip
->chip_type
== PCA953X_TYPE
)
853 ret
= device_pca953x_init(chip
, invert
);
855 ret
= device_pca957x_init(chip
, invert
);
859 ret
= pca953x_irq_setup(chip
, id
, irq_base
);
863 ret
= gpiochip_add(&chip
->gpio_chip
);
867 if (pdata
&& pdata
->setup
) {
868 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
869 chip
->gpio_chip
.ngpio
, pdata
->context
);
871 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
874 i2c_set_clientdata(client
, chip
);
875 #if IS_ENABLED(CONFIG_IPE2ST_POWER)
876 /* register sysfs hooks */
877 ret
= sysfs_create_group(&client
->dev
.kobj
, &pca953x_attr_group
);
884 static int pca953x_remove(struct i2c_client
*client
)
886 struct pca953x_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
887 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
890 #if IS_ENABLED(CONFIG_IPE2ST_POWER)
891 sysfs_remove_group(&client
->dev
.kobj
, &pca953x_attr_group
);
894 if (pdata
&& pdata
->teardown
) {
895 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
896 chip
->gpio_chip
.ngpio
, pdata
->context
);
898 dev_err(&client
->dev
, "%s failed, %d\n",
904 ret
= gpiochip_remove(&chip
->gpio_chip
);
906 dev_err(&client
->dev
, "%s failed, %d\n",
907 "gpiochip_remove()", ret
);
914 static const struct of_device_id pca953x_dt_ids
[] = {
915 { .compatible
= "nxp,pca9505", },
916 { .compatible
= "nxp,pca9534", },
917 { .compatible
= "nxp,pca9535", },
918 { .compatible
= "nxp,pca9536", },
919 { .compatible
= "nxp,pca9537", },
920 { .compatible
= "nxp,pca9538", },
921 { .compatible
= "nxp,pca9539", },
922 { .compatible
= "nxp,pca9554", },
923 { .compatible
= "nxp,pca9555", },
924 { .compatible
= "nxp,pca9556", },
925 { .compatible
= "nxp,pca9557", },
926 { .compatible
= "nxp,pca9574", },
927 { .compatible
= "nxp,pca9575", },
929 { .compatible
= "maxim,max7310", },
930 { .compatible
= "maxim,max7312", },
931 { .compatible
= "maxim,max7313", },
932 { .compatible
= "maxim,max7315", },
934 { .compatible
= "ti,pca6107", },
935 { .compatible
= "ti,tca6408", },
936 { .compatible
= "ti,tca6416", },
937 { .compatible
= "ti,tca6424", },
941 MODULE_DEVICE_TABLE(of
, pca953x_dt_ids
);
943 static struct i2c_driver pca953x_driver
= {
946 .of_match_table
= pca953x_dt_ids
,
948 .probe
= pca953x_probe
,
949 .remove
= pca953x_remove
,
950 .id_table
= pca953x_id
,
953 static int __init
pca953x_init(void)
955 return i2c_add_driver(&pca953x_driver
);
957 /* register after i2c postcore initcall and before
958 * subsys initcalls that may rely on these GPIOs
960 subsys_initcall(pca953x_init
);
962 static void __exit
pca953x_exit(void)
964 i2c_del_driver(&pca953x_driver
);
966 module_exit(pca953x_exit
);
968 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
969 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
970 MODULE_LICENSE("GPL");