2 * PCA953x 4/8/16/24/40 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/gpio/consumer.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_data/pca953x.h>
21 #include <linux/slab.h>
22 #include <asm/unaligned.h>
23 #include <linux/of_platform.h>
24 #include <linux/acpi.h>
25 #include <linux/regulator/consumer.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 PCAL953X_IN_LATCH 34
44 #define PCAL953X_INT_MASK 37
45 #define PCAL953X_INT_STAT 38
47 #define PCA_GPIO_MASK 0x00FF
48 #define PCA_INT 0x0100
49 #define PCA_PCAL 0x0200
50 #define PCA953X_TYPE 0x1000
51 #define PCA957X_TYPE 0x2000
52 #define PCA_TYPE_MASK 0xF000
54 #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
56 static const struct i2c_device_id pca953x_id
[] = {
57 { "pca9505", 40 | PCA953X_TYPE
| PCA_INT
, },
58 { "pca9534", 8 | PCA953X_TYPE
| PCA_INT
, },
59 { "pca9535", 16 | PCA953X_TYPE
| PCA_INT
, },
60 { "pca9536", 4 | PCA953X_TYPE
, },
61 { "pca9537", 4 | PCA953X_TYPE
| PCA_INT
, },
62 { "pca9538", 8 | PCA953X_TYPE
| PCA_INT
, },
63 { "pca9539", 16 | PCA953X_TYPE
| PCA_INT
, },
64 { "pca9554", 8 | PCA953X_TYPE
| PCA_INT
, },
65 { "pca9555", 16 | PCA953X_TYPE
| PCA_INT
, },
66 { "pca9556", 8 | PCA953X_TYPE
, },
67 { "pca9557", 8 | PCA953X_TYPE
, },
68 { "pca9574", 8 | PCA957X_TYPE
| PCA_INT
, },
69 { "pca9575", 16 | PCA957X_TYPE
| PCA_INT
, },
70 { "pca9698", 40 | PCA953X_TYPE
, },
72 { "pcal9555a", 16 | PCA953X_TYPE
| PCA_INT
| PCA_PCAL
, },
74 { "max7310", 8 | PCA953X_TYPE
, },
75 { "max7312", 16 | PCA953X_TYPE
| PCA_INT
, },
76 { "max7313", 16 | PCA953X_TYPE
| PCA_INT
, },
77 { "max7315", 8 | PCA953X_TYPE
| PCA_INT
, },
78 { "max7318", 16 | PCA953X_TYPE
| PCA_INT
, },
79 { "pca6107", 8 | PCA953X_TYPE
| PCA_INT
, },
80 { "tca6408", 8 | PCA953X_TYPE
| PCA_INT
, },
81 { "tca6416", 16 | PCA953X_TYPE
| PCA_INT
, },
82 { "tca6424", 24 | PCA953X_TYPE
| PCA_INT
, },
83 { "tca9539", 16 | PCA953X_TYPE
| PCA_INT
, },
84 { "xra1202", 8 | PCA953X_TYPE
},
87 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
89 static const struct acpi_device_id pca953x_acpi_ids
[] = {
90 { "INT3491", 16 | PCA953X_TYPE
| PCA_INT
| PCA_PCAL
, },
93 MODULE_DEVICE_TABLE(acpi
, pca953x_acpi_ids
);
98 #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
100 struct pca953x_reg_config
{
106 static const struct pca953x_reg_config pca953x_regs
= {
107 .direction
= PCA953X_DIRECTION
,
108 .output
= PCA953X_OUTPUT
,
109 .input
= PCA953X_INPUT
,
112 static const struct pca953x_reg_config pca957x_regs
= {
113 .direction
= PCA957X_CFG
,
114 .output
= PCA957X_OUT
,
118 struct pca953x_chip
{
120 u8 reg_output
[MAX_BANK
];
121 u8 reg_direction
[MAX_BANK
];
122 struct mutex i2c_lock
;
124 #ifdef CONFIG_GPIO_PCA953X_IRQ
125 struct mutex irq_lock
;
126 u8 irq_mask
[MAX_BANK
];
127 u8 irq_stat
[MAX_BANK
];
128 u8 irq_trig_raise
[MAX_BANK
];
129 u8 irq_trig_fall
[MAX_BANK
];
132 struct i2c_client
*client
;
133 struct gpio_chip gpio_chip
;
134 const char *const *names
;
135 unsigned long driver_data
;
136 struct regulator
*regulator
;
138 const struct pca953x_reg_config
*regs
;
140 int (*write_regs
)(struct pca953x_chip
*, int, u8
*);
141 int (*read_regs
)(struct pca953x_chip
*, int, u8
*);
144 static int pca953x_read_single(struct pca953x_chip
*chip
, int reg
, u32
*val
,
148 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
149 int offset
= off
/ BANK_SZ
;
151 ret
= i2c_smbus_read_byte_data(chip
->client
,
152 (reg
<< bank_shift
) + offset
);
156 dev_err(&chip
->client
->dev
, "failed reading register\n");
163 static int pca953x_write_single(struct pca953x_chip
*chip
, int reg
, u32 val
,
167 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
168 int offset
= off
/ BANK_SZ
;
170 ret
= i2c_smbus_write_byte_data(chip
->client
,
171 (reg
<< bank_shift
) + offset
, val
);
174 dev_err(&chip
->client
->dev
, "failed writing register\n");
181 static int pca953x_write_regs_8(struct pca953x_chip
*chip
, int reg
, u8
*val
)
183 return i2c_smbus_write_byte_data(chip
->client
, reg
, *val
);
186 static int pca953x_write_regs_16(struct pca953x_chip
*chip
, int reg
, u8
*val
)
188 __le16 word
= cpu_to_le16(get_unaligned((u16
*)val
));
190 return i2c_smbus_write_word_data(chip
->client
,
191 reg
<< 1, (__force u16
)word
);
194 static int pca957x_write_regs_16(struct pca953x_chip
*chip
, int reg
, u8
*val
)
198 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
<< 1, val
[0]);
202 return i2c_smbus_write_byte_data(chip
->client
, (reg
<< 1) + 1, val
[1]);
205 static int pca953x_write_regs_24(struct pca953x_chip
*chip
, int reg
, u8
*val
)
207 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
209 return i2c_smbus_write_i2c_block_data(chip
->client
,
210 (reg
<< bank_shift
) | REG_ADDR_AI
,
214 static int pca953x_write_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
218 ret
= chip
->write_regs(chip
, reg
, val
);
220 dev_err(&chip
->client
->dev
, "failed writing register\n");
227 static int pca953x_read_regs_8(struct pca953x_chip
*chip
, int reg
, u8
*val
)
231 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
237 static int pca953x_read_regs_16(struct pca953x_chip
*chip
, int reg
, u8
*val
)
241 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
242 val
[0] = (u16
)ret
& 0xFF;
243 val
[1] = (u16
)ret
>> 8;
248 static int pca953x_read_regs_24(struct pca953x_chip
*chip
, int reg
, u8
*val
)
250 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
252 return i2c_smbus_read_i2c_block_data(chip
->client
,
253 (reg
<< bank_shift
) | REG_ADDR_AI
,
257 static int pca953x_read_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
261 ret
= chip
->read_regs(chip
, reg
, val
);
263 dev_err(&chip
->client
->dev
, "failed reading register\n");
270 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
272 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
276 mutex_lock(&chip
->i2c_lock
);
277 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] | (1u << (off
% BANK_SZ
));
279 ret
= pca953x_write_single(chip
, chip
->regs
->direction
, reg_val
, off
);
283 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
285 mutex_unlock(&chip
->i2c_lock
);
289 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
290 unsigned off
, int val
)
292 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
296 mutex_lock(&chip
->i2c_lock
);
297 /* set output level */
299 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
300 | (1u << (off
% BANK_SZ
));
302 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
303 & ~(1u << (off
% BANK_SZ
));
305 ret
= pca953x_write_single(chip
, chip
->regs
->output
, reg_val
, off
);
309 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
312 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] & ~(1u << (off
% BANK_SZ
));
313 ret
= pca953x_write_single(chip
, chip
->regs
->direction
, reg_val
, off
);
317 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
319 mutex_unlock(&chip
->i2c_lock
);
323 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
325 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
329 mutex_lock(&chip
->i2c_lock
);
330 ret
= pca953x_read_single(chip
, chip
->regs
->input
, ®_val
, off
);
331 mutex_unlock(&chip
->i2c_lock
);
333 /* NOTE: diagnostic already emitted; that's all we should
334 * do unless gpio_*_value_cansleep() calls become different
335 * from their nonsleeping siblings (and report faults).
340 return (reg_val
& (1u << (off
% BANK_SZ
))) ? 1 : 0;
343 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
345 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
349 mutex_lock(&chip
->i2c_lock
);
351 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
352 | (1u << (off
% BANK_SZ
));
354 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
355 & ~(1u << (off
% BANK_SZ
));
357 ret
= pca953x_write_single(chip
, chip
->regs
->output
, reg_val
, off
);
361 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
363 mutex_unlock(&chip
->i2c_lock
);
366 static void pca953x_gpio_set_multiple(struct gpio_chip
*gc
,
367 unsigned long *mask
, unsigned long *bits
)
369 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
370 unsigned int bank_mask
, bank_val
;
371 int bank_shift
, bank
;
372 u8 reg_val
[MAX_BANK
];
375 bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
377 mutex_lock(&chip
->i2c_lock
);
378 memcpy(reg_val
, chip
->reg_output
, NBANK(chip
));
379 for (bank
= 0; bank
< NBANK(chip
); bank
++) {
380 bank_mask
= mask
[bank
/ sizeof(*mask
)] >>
381 ((bank
% sizeof(*mask
)) * 8);
383 bank_val
= bits
[bank
/ sizeof(*bits
)] >>
384 ((bank
% sizeof(*bits
)) * 8);
385 bank_val
&= bank_mask
;
386 reg_val
[bank
] = (reg_val
[bank
] & ~bank_mask
) | bank_val
;
390 ret
= i2c_smbus_write_i2c_block_data(chip
->client
,
391 chip
->regs
->output
<< bank_shift
,
392 NBANK(chip
), reg_val
);
396 memcpy(chip
->reg_output
, reg_val
, NBANK(chip
));
398 mutex_unlock(&chip
->i2c_lock
);
401 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
403 struct gpio_chip
*gc
;
405 gc
= &chip
->gpio_chip
;
407 gc
->direction_input
= pca953x_gpio_direction_input
;
408 gc
->direction_output
= pca953x_gpio_direction_output
;
409 gc
->get
= pca953x_gpio_get_value
;
410 gc
->set
= pca953x_gpio_set_value
;
411 gc
->set_multiple
= pca953x_gpio_set_multiple
;
412 gc
->can_sleep
= true;
414 gc
->base
= chip
->gpio_start
;
416 gc
->label
= chip
->client
->name
;
417 gc
->parent
= &chip
->client
->dev
;
418 gc
->owner
= THIS_MODULE
;
419 gc
->names
= chip
->names
;
422 #ifdef CONFIG_GPIO_PCA953X_IRQ
423 static void pca953x_irq_mask(struct irq_data
*d
)
425 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
426 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
428 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] &= ~(1 << (d
->hwirq
% BANK_SZ
));
431 static void pca953x_irq_unmask(struct irq_data
*d
)
433 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
434 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
436 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] |= 1 << (d
->hwirq
% BANK_SZ
);
439 static void pca953x_irq_bus_lock(struct irq_data
*d
)
441 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
442 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
444 mutex_lock(&chip
->irq_lock
);
447 static void pca953x_irq_bus_sync_unlock(struct irq_data
*d
)
449 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
450 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
453 u8 invert_irq_mask
[MAX_BANK
];
455 if (chip
->driver_data
& PCA_PCAL
) {
456 /* Enable latch on interrupt-enabled inputs */
457 pca953x_write_regs(chip
, PCAL953X_IN_LATCH
, chip
->irq_mask
);
459 for (i
= 0; i
< NBANK(chip
); i
++)
460 invert_irq_mask
[i
] = ~chip
->irq_mask
[i
];
462 /* Unmask enabled interrupts */
463 pca953x_write_regs(chip
, PCAL953X_INT_MASK
, invert_irq_mask
);
466 /* Look for any newly setup interrupt */
467 for (i
= 0; i
< NBANK(chip
); i
++) {
468 new_irqs
= chip
->irq_trig_fall
[i
] | chip
->irq_trig_raise
[i
];
469 new_irqs
&= ~chip
->reg_direction
[i
];
472 level
= __ffs(new_irqs
);
473 pca953x_gpio_direction_input(&chip
->gpio_chip
,
474 level
+ (BANK_SZ
* i
));
475 new_irqs
&= ~(1 << level
);
479 mutex_unlock(&chip
->irq_lock
);
482 static int pca953x_irq_set_type(struct irq_data
*d
, unsigned int type
)
484 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
485 struct pca953x_chip
*chip
= gpiochip_get_data(gc
);
486 int bank_nb
= d
->hwirq
/ BANK_SZ
;
487 u8 mask
= 1 << (d
->hwirq
% BANK_SZ
);
489 if (!(type
& IRQ_TYPE_EDGE_BOTH
)) {
490 dev_err(&chip
->client
->dev
, "irq %d: unsupported type %d\n",
495 if (type
& IRQ_TYPE_EDGE_FALLING
)
496 chip
->irq_trig_fall
[bank_nb
] |= mask
;
498 chip
->irq_trig_fall
[bank_nb
] &= ~mask
;
500 if (type
& IRQ_TYPE_EDGE_RISING
)
501 chip
->irq_trig_raise
[bank_nb
] |= mask
;
503 chip
->irq_trig_raise
[bank_nb
] &= ~mask
;
508 static struct irq_chip pca953x_irq_chip
= {
510 .irq_mask
= pca953x_irq_mask
,
511 .irq_unmask
= pca953x_irq_unmask
,
512 .irq_bus_lock
= pca953x_irq_bus_lock
,
513 .irq_bus_sync_unlock
= pca953x_irq_bus_sync_unlock
,
514 .irq_set_type
= pca953x_irq_set_type
,
517 static bool pca953x_irq_pending(struct pca953x_chip
*chip
, u8
*pending
)
519 u8 cur_stat
[MAX_BANK
];
520 u8 old_stat
[MAX_BANK
];
521 bool pending_seen
= false;
522 bool trigger_seen
= false;
523 u8 trigger
[MAX_BANK
];
526 if (chip
->driver_data
& PCA_PCAL
) {
527 /* Read the current interrupt status from the device */
528 ret
= pca953x_read_regs(chip
, PCAL953X_INT_STAT
, trigger
);
532 /* Check latched inputs and clear interrupt status */
533 ret
= pca953x_read_regs(chip
, PCA953X_INPUT
, cur_stat
);
537 for (i
= 0; i
< NBANK(chip
); i
++) {
538 /* Apply filter for rising/falling edge selection */
539 pending
[i
] = (~cur_stat
[i
] & chip
->irq_trig_fall
[i
]) |
540 (cur_stat
[i
] & chip
->irq_trig_raise
[i
]);
541 pending
[i
] &= trigger
[i
];
549 ret
= pca953x_read_regs(chip
, chip
->regs
->input
, cur_stat
);
553 /* Remove output pins from the equation */
554 for (i
= 0; i
< NBANK(chip
); i
++)
555 cur_stat
[i
] &= chip
->reg_direction
[i
];
557 memcpy(old_stat
, chip
->irq_stat
, NBANK(chip
));
559 for (i
= 0; i
< NBANK(chip
); i
++) {
560 trigger
[i
] = (cur_stat
[i
] ^ old_stat
[i
]) & chip
->irq_mask
[i
];
568 memcpy(chip
->irq_stat
, cur_stat
, NBANK(chip
));
570 for (i
= 0; i
< NBANK(chip
); i
++) {
571 pending
[i
] = (old_stat
[i
] & chip
->irq_trig_fall
[i
]) |
572 (cur_stat
[i
] & chip
->irq_trig_raise
[i
]);
573 pending
[i
] &= trigger
[i
];
581 static irqreturn_t
pca953x_irq_handler(int irq
, void *devid
)
583 struct pca953x_chip
*chip
= devid
;
584 u8 pending
[MAX_BANK
];
586 unsigned nhandled
= 0;
589 if (!pca953x_irq_pending(chip
, pending
))
592 for (i
= 0; i
< NBANK(chip
); i
++) {
594 level
= __ffs(pending
[i
]);
595 handle_nested_irq(irq_find_mapping(chip
->gpio_chip
.irqdomain
,
596 level
+ (BANK_SZ
* i
)));
597 pending
[i
] &= ~(1 << level
);
602 return (nhandled
> 0) ? IRQ_HANDLED
: IRQ_NONE
;
605 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
608 struct i2c_client
*client
= chip
->client
;
611 if (client
->irq
&& irq_base
!= -1
612 && (chip
->driver_data
& PCA_INT
)) {
613 ret
= pca953x_read_regs(chip
,
614 chip
->regs
->input
, chip
->irq_stat
);
619 * There is no way to know which GPIO line generated the
620 * interrupt. We have to rely on the previous read for
623 for (i
= 0; i
< NBANK(chip
); i
++)
624 chip
->irq_stat
[i
] &= chip
->reg_direction
[i
];
625 mutex_init(&chip
->irq_lock
);
627 ret
= devm_request_threaded_irq(&client
->dev
,
631 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
|
633 dev_name(&client
->dev
), chip
);
635 dev_err(&client
->dev
, "failed to request irq %d\n",
640 ret
= gpiochip_irqchip_add_nested(&chip
->gpio_chip
,
646 dev_err(&client
->dev
,
647 "could not connect irqchip to gpiochip\n");
651 gpiochip_set_nested_irqchip(&chip
->gpio_chip
,
659 #else /* CONFIG_GPIO_PCA953X_IRQ */
660 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
663 struct i2c_client
*client
= chip
->client
;
665 if (irq_base
!= -1 && (chip
->driver_data
& PCA_INT
))
666 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
672 static int device_pca953x_init(struct pca953x_chip
*chip
, u32 invert
)
677 chip
->regs
= &pca953x_regs
;
679 ret
= pca953x_read_regs(chip
, chip
->regs
->output
, chip
->reg_output
);
683 ret
= pca953x_read_regs(chip
, chip
->regs
->direction
,
684 chip
->reg_direction
);
688 /* set platform specific polarity inversion */
690 memset(val
, 0xFF, NBANK(chip
));
692 memset(val
, 0, NBANK(chip
));
694 ret
= pca953x_write_regs(chip
, PCA953X_INVERT
, val
);
699 static int device_pca957x_init(struct pca953x_chip
*chip
, u32 invert
)
704 chip
->regs
= &pca957x_regs
;
706 ret
= pca953x_read_regs(chip
, chip
->regs
->output
, chip
->reg_output
);
709 ret
= pca953x_read_regs(chip
, chip
->regs
->direction
,
710 chip
->reg_direction
);
714 /* set platform specific polarity inversion */
716 memset(val
, 0xFF, NBANK(chip
));
718 memset(val
, 0, NBANK(chip
));
719 ret
= pca953x_write_regs(chip
, PCA957X_INVRT
, val
);
723 /* To enable register 6, 7 to control pull up and pull down */
724 memset(val
, 0x02, NBANK(chip
));
725 ret
= pca953x_write_regs(chip
, PCA957X_BKEN
, val
);
734 static const struct of_device_id pca953x_dt_ids
[];
736 static int pca953x_probe(struct i2c_client
*client
,
737 const struct i2c_device_id
*i2c_id
)
739 struct pca953x_platform_data
*pdata
;
740 struct pca953x_chip
*chip
;
744 struct regulator
*reg
;
746 chip
= devm_kzalloc(&client
->dev
,
747 sizeof(struct pca953x_chip
), GFP_KERNEL
);
751 pdata
= dev_get_platdata(&client
->dev
);
753 irq_base
= pdata
->irq_base
;
754 chip
->gpio_start
= pdata
->gpio_base
;
755 invert
= pdata
->invert
;
756 chip
->names
= pdata
->names
;
758 struct gpio_desc
*reset_gpio
;
760 chip
->gpio_start
= -1;
763 /* See if we need to de-assert a reset pin */
764 reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
766 if (IS_ERR(reset_gpio
))
767 return PTR_ERR(reset_gpio
);
770 chip
->client
= client
;
772 reg
= devm_regulator_get(&client
->dev
, "vcc");
775 if (ret
!= -EPROBE_DEFER
)
776 dev_err(&client
->dev
, "reg get err: %d\n", ret
);
779 ret
= regulator_enable(reg
);
781 dev_err(&client
->dev
, "reg en err: %d\n", ret
);
784 chip
->regulator
= reg
;
787 chip
->driver_data
= i2c_id
->driver_data
;
789 const struct acpi_device_id
*acpi_id
;
790 const struct of_device_id
*match
;
792 match
= of_match_device(pca953x_dt_ids
, &client
->dev
);
794 chip
->driver_data
= (int)(uintptr_t)match
->data
;
796 acpi_id
= acpi_match_device(pca953x_acpi_ids
, &client
->dev
);
802 chip
->driver_data
= acpi_id
->driver_data
;
806 mutex_init(&chip
->i2c_lock
);
808 * In case we have an i2c-mux controlled by a GPIO provided by an
809 * expander using the same driver higher on the device tree, read the
810 * i2c adapter nesting depth and use the retrieved value as lockdep
811 * subclass for chip->i2c_lock.
813 * REVISIT: This solution is not complete. It protects us from lockdep
814 * false positives when the expander controlling the i2c-mux is on
815 * a different level on the device tree, but not when it's on the same
816 * level on a different branch (in which case the subclass number
817 * would be the same).
819 * TODO: Once a correct solution is developed, a similar fix should be
820 * applied to all other i2c-controlled GPIO expanders (and potentially
823 lockdep_set_subclass(&chip
->i2c_lock
,
824 i2c_adapter_depth(client
->adapter
));
826 /* initialize cached registers from their original values.
827 * we can't share this chip with another i2c master.
829 pca953x_setup_gpio(chip
, chip
->driver_data
& PCA_GPIO_MASK
);
831 if (chip
->gpio_chip
.ngpio
<= 8) {
832 chip
->write_regs
= pca953x_write_regs_8
;
833 chip
->read_regs
= pca953x_read_regs_8
;
834 } else if (chip
->gpio_chip
.ngpio
>= 24) {
835 chip
->write_regs
= pca953x_write_regs_24
;
836 chip
->read_regs
= pca953x_read_regs_24
;
838 if (PCA_CHIP_TYPE(chip
->driver_data
) == PCA953X_TYPE
)
839 chip
->write_regs
= pca953x_write_regs_16
;
841 chip
->write_regs
= pca957x_write_regs_16
;
842 chip
->read_regs
= pca953x_read_regs_16
;
845 if (PCA_CHIP_TYPE(chip
->driver_data
) == PCA953X_TYPE
)
846 ret
= device_pca953x_init(chip
, invert
);
848 ret
= device_pca957x_init(chip
, invert
);
852 ret
= devm_gpiochip_add_data(&client
->dev
, &chip
->gpio_chip
, chip
);
856 ret
= pca953x_irq_setup(chip
, irq_base
);
860 if (pdata
&& pdata
->setup
) {
861 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
862 chip
->gpio_chip
.ngpio
, pdata
->context
);
864 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
867 i2c_set_clientdata(client
, chip
);
871 regulator_disable(chip
->regulator
);
875 static int pca953x_remove(struct i2c_client
*client
)
877 struct pca953x_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
878 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
881 if (pdata
&& pdata
->teardown
) {
882 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
883 chip
->gpio_chip
.ngpio
, pdata
->context
);
885 dev_err(&client
->dev
, "%s failed, %d\n",
891 regulator_disable(chip
->regulator
);
896 /* convenience to stop overlong match-table lines */
897 #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
898 #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
900 static const struct of_device_id pca953x_dt_ids
[] = {
901 { .compatible
= "nxp,pca9505", .data
= OF_953X(40, PCA_INT
), },
902 { .compatible
= "nxp,pca9534", .data
= OF_953X( 8, PCA_INT
), },
903 { .compatible
= "nxp,pca9535", .data
= OF_953X(16, PCA_INT
), },
904 { .compatible
= "nxp,pca9536", .data
= OF_953X( 4, 0), },
905 { .compatible
= "nxp,pca9537", .data
= OF_953X( 4, PCA_INT
), },
906 { .compatible
= "nxp,pca9538", .data
= OF_953X( 8, PCA_INT
), },
907 { .compatible
= "nxp,pca9539", .data
= OF_953X(16, PCA_INT
), },
908 { .compatible
= "nxp,pca9554", .data
= OF_953X( 8, PCA_INT
), },
909 { .compatible
= "nxp,pca9555", .data
= OF_953X(16, PCA_INT
), },
910 { .compatible
= "nxp,pca9556", .data
= OF_953X( 8, 0), },
911 { .compatible
= "nxp,pca9557", .data
= OF_953X( 8, 0), },
912 { .compatible
= "nxp,pca9574", .data
= OF_957X( 8, PCA_INT
), },
913 { .compatible
= "nxp,pca9575", .data
= OF_957X(16, PCA_INT
), },
914 { .compatible
= "nxp,pca9698", .data
= OF_953X(40, 0), },
916 { .compatible
= "maxim,max7310", .data
= OF_953X( 8, 0), },
917 { .compatible
= "maxim,max7312", .data
= OF_953X(16, PCA_INT
), },
918 { .compatible
= "maxim,max7313", .data
= OF_953X(16, PCA_INT
), },
919 { .compatible
= "maxim,max7315", .data
= OF_953X( 8, PCA_INT
), },
920 { .compatible
= "maxim,max7318", .data
= OF_953X(16, PCA_INT
), },
922 { .compatible
= "ti,pca6107", .data
= OF_953X( 8, PCA_INT
), },
923 { .compatible
= "ti,pca9536", .data
= OF_953X( 4, 0), },
924 { .compatible
= "ti,tca6408", .data
= OF_953X( 8, PCA_INT
), },
925 { .compatible
= "ti,tca6416", .data
= OF_953X(16, PCA_INT
), },
926 { .compatible
= "ti,tca6424", .data
= OF_953X(24, PCA_INT
), },
928 { .compatible
= "onsemi,pca9654", .data
= OF_953X( 8, PCA_INT
), },
930 { .compatible
= "exar,xra1202", .data
= OF_953X( 8, 0), },
934 MODULE_DEVICE_TABLE(of
, pca953x_dt_ids
);
936 static struct i2c_driver pca953x_driver
= {
939 .of_match_table
= pca953x_dt_ids
,
940 .acpi_match_table
= ACPI_PTR(pca953x_acpi_ids
),
942 .probe
= pca953x_probe
,
943 .remove
= pca953x_remove
,
944 .id_table
= pca953x_id
,
947 static int __init
pca953x_init(void)
949 return i2c_add_driver(&pca953x_driver
);
951 /* register after i2c postcore initcall and before
952 * subsys initcalls that may rely on these GPIOs
954 subsys_initcall(pca953x_init
);
956 static void __exit
pca953x_exit(void)
958 i2c_del_driver(&pca953x_driver
);
960 module_exit(pca953x_exit
);
962 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
963 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
964 MODULE_LICENSE("GPL");