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/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_data/pca953x.h>
20 #include <linux/slab.h>
22 #include <linux/of_platform.h>
24 #include <linux/acpi.h>
26 #define PCA953X_INPUT 0
27 #define PCA953X_OUTPUT 1
28 #define PCA953X_INVERT 2
29 #define PCA953X_DIRECTION 3
31 #define REG_ADDR_AI 0x80
34 #define PCA957X_INVRT 1
35 #define PCA957X_BKEN 2
36 #define PCA957X_PUPD 3
40 #define PCA957X_INTS 7
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
48 #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
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
, },
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
},
78 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
80 static const struct acpi_device_id pca953x_acpi_ids
[] = {
81 { "INT3491", 16 | PCA953X_TYPE
| PCA_INT
, },
84 MODULE_DEVICE_TABLE(acpi
, pca953x_acpi_ids
);
89 #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
93 u8 reg_output
[MAX_BANK
];
94 u8 reg_direction
[MAX_BANK
];
95 struct mutex i2c_lock
;
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
];
105 struct i2c_client
*client
;
106 struct gpio_chip gpio_chip
;
107 const char *const *names
;
109 unsigned long driver_data
;
112 static inline struct pca953x_chip
*to_pca(struct gpio_chip
*gc
)
114 return container_of(gc
, struct pca953x_chip
, gpio_chip
);
117 static int pca953x_read_single(struct pca953x_chip
*chip
, int reg
, u32
*val
,
121 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
122 int offset
= off
/ BANK_SZ
;
124 ret
= i2c_smbus_read_byte_data(chip
->client
,
125 (reg
<< bank_shift
) + offset
);
129 dev_err(&chip
->client
->dev
, "failed reading register\n");
136 static int pca953x_write_single(struct pca953x_chip
*chip
, int reg
, u32 val
,
140 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
141 int offset
= off
/ BANK_SZ
;
143 ret
= i2c_smbus_write_byte_data(chip
->client
,
144 (reg
<< bank_shift
) + offset
, val
);
147 dev_err(&chip
->client
->dev
, "failed writing register\n");
154 static int pca953x_write_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
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
,
166 switch (chip
->chip_type
) {
168 ret
= i2c_smbus_write_word_data(chip
->client
,
169 reg
<< 1, (u16
) *val
);
172 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
<< 1,
176 ret
= i2c_smbus_write_byte_data(chip
->client
,
184 dev_err(&chip
->client
->dev
, "failed writing register\n");
191 static int pca953x_read_regs(struct pca953x_chip
*chip
, int reg
, u8
*val
)
195 if (chip
->gpio_chip
.ngpio
<= 8) {
196 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
198 } else if (chip
->gpio_chip
.ngpio
>= 24) {
199 int bank_shift
= fls((chip
->gpio_chip
.ngpio
- 1) / BANK_SZ
);
201 ret
= i2c_smbus_read_i2c_block_data(chip
->client
,
202 (reg
<< bank_shift
) | REG_ADDR_AI
,
205 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
206 val
[0] = (u16
)ret
& 0xFF;
207 val
[1] = (u16
)ret
>> 8;
210 dev_err(&chip
->client
->dev
, "failed reading register\n");
217 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
219 struct pca953x_chip
*chip
= to_pca(gc
);
223 mutex_lock(&chip
->i2c_lock
);
224 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] | (1u << (off
% BANK_SZ
));
226 switch (chip
->chip_type
) {
228 offset
= PCA953X_DIRECTION
;
231 offset
= PCA957X_CFG
;
234 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
238 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
241 mutex_unlock(&chip
->i2c_lock
);
245 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
246 unsigned off
, int val
)
248 struct pca953x_chip
*chip
= to_pca(gc
);
252 mutex_lock(&chip
->i2c_lock
);
253 /* set output level */
255 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
256 | (1u << (off
% BANK_SZ
));
258 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
259 & ~(1u << (off
% BANK_SZ
));
261 switch (chip
->chip_type
) {
263 offset
= PCA953X_OUTPUT
;
266 offset
= PCA957X_OUT
;
269 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
273 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
276 reg_val
= chip
->reg_direction
[off
/ BANK_SZ
] & ~(1u << (off
% BANK_SZ
));
277 switch (chip
->chip_type
) {
279 offset
= PCA953X_DIRECTION
;
282 offset
= PCA957X_CFG
;
285 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
289 chip
->reg_direction
[off
/ BANK_SZ
] = reg_val
;
292 mutex_unlock(&chip
->i2c_lock
);
296 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
298 struct pca953x_chip
*chip
= to_pca(gc
);
302 mutex_lock(&chip
->i2c_lock
);
303 switch (chip
->chip_type
) {
305 offset
= PCA953X_INPUT
;
311 ret
= pca953x_read_single(chip
, offset
, ®_val
, off
);
312 mutex_unlock(&chip
->i2c_lock
);
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).
321 return (reg_val
& (1u << (off
% BANK_SZ
))) ? 1 : 0;
324 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
326 struct pca953x_chip
*chip
= to_pca(gc
);
330 mutex_lock(&chip
->i2c_lock
);
332 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
333 | (1u << (off
% BANK_SZ
));
335 reg_val
= chip
->reg_output
[off
/ BANK_SZ
]
336 & ~(1u << (off
% BANK_SZ
));
338 switch (chip
->chip_type
) {
340 offset
= PCA953X_OUTPUT
;
343 offset
= PCA957X_OUT
;
346 ret
= pca953x_write_single(chip
, offset
, reg_val
, off
);
350 chip
->reg_output
[off
/ BANK_SZ
] = reg_val
;
352 mutex_unlock(&chip
->i2c_lock
);
355 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
357 struct gpio_chip
*gc
;
359 gc
= &chip
->gpio_chip
;
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;
367 gc
->base
= chip
->gpio_start
;
369 gc
->label
= chip
->client
->name
;
370 gc
->dev
= &chip
->client
->dev
;
371 gc
->owner
= THIS_MODULE
;
372 gc
->names
= chip
->names
;
375 #ifdef CONFIG_GPIO_PCA953X_IRQ
376 static void pca953x_irq_mask(struct irq_data
*d
)
378 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
379 struct pca953x_chip
*chip
= to_pca(gc
);
381 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] &= ~(1 << (d
->hwirq
% BANK_SZ
));
384 static void pca953x_irq_unmask(struct irq_data
*d
)
386 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
387 struct pca953x_chip
*chip
= to_pca(gc
);
389 chip
->irq_mask
[d
->hwirq
/ BANK_SZ
] |= 1 << (d
->hwirq
% BANK_SZ
);
392 static void pca953x_irq_bus_lock(struct irq_data
*d
)
394 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
395 struct pca953x_chip
*chip
= to_pca(gc
);
397 mutex_lock(&chip
->irq_lock
);
400 static void pca953x_irq_bus_sync_unlock(struct irq_data
*d
)
402 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
403 struct pca953x_chip
*chip
= to_pca(gc
);
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
];
413 level
= __ffs(new_irqs
);
414 pca953x_gpio_direction_input(&chip
->gpio_chip
,
415 level
+ (BANK_SZ
* i
));
416 new_irqs
&= ~(1 << level
);
420 mutex_unlock(&chip
->irq_lock
);
423 static int pca953x_irq_set_type(struct irq_data
*d
, unsigned int type
)
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
);
430 if (!(type
& IRQ_TYPE_EDGE_BOTH
)) {
431 dev_err(&chip
->client
->dev
, "irq %d: unsupported type %d\n",
436 if (type
& IRQ_TYPE_EDGE_FALLING
)
437 chip
->irq_trig_fall
[bank_nb
] |= mask
;
439 chip
->irq_trig_fall
[bank_nb
] &= ~mask
;
441 if (type
& IRQ_TYPE_EDGE_RISING
)
442 chip
->irq_trig_raise
[bank_nb
] |= mask
;
444 chip
->irq_trig_raise
[bank_nb
] &= ~mask
;
449 static struct irq_chip pca953x_irq_chip
= {
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
,
458 static bool pca953x_irq_pending(struct pca953x_chip
*chip
, u8
*pending
)
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;
467 switch (chip
->chip_type
) {
469 offset
= PCA953X_INPUT
;
475 ret
= pca953x_read_regs(chip
, offset
, cur_stat
);
479 /* Remove output pins from the equation */
480 for (i
= 0; i
< NBANK(chip
); i
++)
481 cur_stat
[i
] &= chip
->reg_direction
[i
];
483 memcpy(old_stat
, chip
->irq_stat
, NBANK(chip
));
485 for (i
= 0; i
< NBANK(chip
); i
++) {
486 trigger
[i
] = (cur_stat
[i
] ^ old_stat
[i
]) & chip
->irq_mask
[i
];
494 memcpy(chip
->irq_stat
, cur_stat
, NBANK(chip
));
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
];
507 static irqreturn_t
pca953x_irq_handler(int irq
, void *devid
)
509 struct pca953x_chip
*chip
= devid
;
510 u8 pending
[MAX_BANK
];
512 unsigned nhandled
= 0;
515 if (!pca953x_irq_pending(chip
, pending
))
518 for (i
= 0; i
< NBANK(chip
); 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
);
528 return (nhandled
> 0) ? IRQ_HANDLED
: IRQ_NONE
;
531 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
534 struct i2c_client
*client
= chip
->client
;
535 int ret
, i
, offset
= 0;
537 if (client
->irq
&& irq_base
!= -1
538 && (chip
->driver_data
& PCA_INT
)) {
540 switch (chip
->chip_type
) {
542 offset
= PCA953X_INPUT
;
548 ret
= pca953x_read_regs(chip
, offset
, chip
->irq_stat
);
553 * There is no way to know which GPIO line generated the
554 * interrupt. We have to rely on the previous read for
557 for (i
= 0; i
< NBANK(chip
); i
++)
558 chip
->irq_stat
[i
] &= chip
->reg_direction
[i
];
559 mutex_init(&chip
->irq_lock
);
561 ret
= devm_request_threaded_irq(&client
->dev
,
565 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
|
567 dev_name(&client
->dev
), chip
);
569 dev_err(&client
->dev
, "failed to request irq %d\n",
574 ret
= gpiochip_irqchip_add(&chip
->gpio_chip
,
580 dev_err(&client
->dev
,
581 "could not connect irqchip to gpiochip\n");
585 gpiochip_set_chained_irqchip(&chip
->gpio_chip
,
593 #else /* CONFIG_GPIO_PCA953X_IRQ */
594 static int pca953x_irq_setup(struct pca953x_chip
*chip
,
597 struct i2c_client
*client
= chip
->client
;
599 if (irq_base
!= -1 && (chip
->driver_data
& PCA_INT
))
600 dev_warn(&client
->dev
, "interrupt support not compiled in\n");
606 static int device_pca953x_init(struct pca953x_chip
*chip
, u32 invert
)
611 ret
= pca953x_read_regs(chip
, PCA953X_OUTPUT
, chip
->reg_output
);
615 ret
= pca953x_read_regs(chip
, PCA953X_DIRECTION
,
616 chip
->reg_direction
);
620 /* set platform specific polarity inversion */
622 memset(val
, 0xFF, NBANK(chip
));
624 memset(val
, 0, NBANK(chip
));
626 ret
= pca953x_write_regs(chip
, PCA953X_INVERT
, val
);
631 static int device_pca957x_init(struct pca953x_chip
*chip
, u32 invert
)
636 ret
= pca953x_read_regs(chip
, PCA957X_OUT
, chip
->reg_output
);
639 ret
= pca953x_read_regs(chip
, PCA957X_CFG
, chip
->reg_direction
);
643 /* set platform specific polarity inversion */
645 memset(val
, 0xFF, NBANK(chip
));
647 memset(val
, 0, NBANK(chip
));
648 ret
= pca953x_write_regs(chip
, PCA957X_INVRT
, val
);
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
);
663 static int pca953x_probe(struct i2c_client
*client
,
664 const struct i2c_device_id
*id
)
666 struct pca953x_platform_data
*pdata
;
667 struct pca953x_chip
*chip
;
672 chip
= devm_kzalloc(&client
->dev
,
673 sizeof(struct pca953x_chip
), GFP_KERNEL
);
677 pdata
= dev_get_platdata(&client
->dev
);
679 irq_base
= pdata
->irq_base
;
680 chip
->gpio_start
= pdata
->gpio_base
;
681 invert
= pdata
->invert
;
682 chip
->names
= pdata
->names
;
684 chip
->gpio_start
= -1;
688 chip
->client
= client
;
691 chip
->driver_data
= id
->driver_data
;
693 const struct acpi_device_id
*id
;
695 id
= acpi_match_device(pca953x_acpi_ids
, &client
->dev
);
699 chip
->driver_data
= id
->driver_data
;
702 chip
->chip_type
= PCA_CHIP_TYPE(chip
->driver_data
);
704 mutex_init(&chip
->i2c_lock
);
706 /* initialize cached registers from their original values.
707 * we can't share this chip with another i2c master.
709 pca953x_setup_gpio(chip
, chip
->driver_data
& PCA_GPIO_MASK
);
711 if (chip
->chip_type
== PCA953X_TYPE
)
712 ret
= device_pca953x_init(chip
, invert
);
714 ret
= device_pca957x_init(chip
, invert
);
718 ret
= gpiochip_add(&chip
->gpio_chip
);
722 ret
= pca953x_irq_setup(chip
, irq_base
);
726 if (pdata
&& pdata
->setup
) {
727 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
728 chip
->gpio_chip
.ngpio
, pdata
->context
);
730 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
733 i2c_set_clientdata(client
, chip
);
737 static int pca953x_remove(struct i2c_client
*client
)
739 struct pca953x_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
740 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
743 if (pdata
&& pdata
->teardown
) {
744 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
745 chip
->gpio_chip
.ngpio
, pdata
->context
);
747 dev_err(&client
->dev
, "%s failed, %d\n",
753 gpiochip_remove(&chip
->gpio_chip
);
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", },
774 { .compatible
= "maxim,max7310", },
775 { .compatible
= "maxim,max7312", },
776 { .compatible
= "maxim,max7313", },
777 { .compatible
= "maxim,max7315", },
779 { .compatible
= "ti,pca6107", },
780 { .compatible
= "ti,tca6408", },
781 { .compatible
= "ti,tca6416", },
782 { .compatible
= "ti,tca6424", },
784 { .compatible
= "exar,xra1202", },
788 MODULE_DEVICE_TABLE(of
, pca953x_dt_ids
);
790 static struct i2c_driver pca953x_driver
= {
793 .of_match_table
= pca953x_dt_ids
,
794 .acpi_match_table
= ACPI_PTR(pca953x_acpi_ids
),
796 .probe
= pca953x_probe
,
797 .remove
= pca953x_remove
,
798 .id_table
= pca953x_id
,
801 static int __init
pca953x_init(void)
803 return i2c_add_driver(&pca953x_driver
);
805 /* register after i2c postcore initcall and before
806 * subsys initcalls that may rely on these GPIOs
808 subsys_initcall(pca953x_init
);
810 static void __exit
pca953x_exit(void)
812 i2c_del_driver(&pca953x_driver
);
814 module_exit(pca953x_exit
);
816 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
817 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
818 MODULE_LICENSE("GPL");