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>
21 #include <asm/unaligned.h>
23 #include <linux/of_platform.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
47 static const struct i2c_device_id pca953x_id[] = {
48 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
49 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
50 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
51 { "pca9536", 4 | PCA953X_TYPE, },
52 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
53 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
54 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
55 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
56 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
57 { "pca9556", 8 | PCA953X_TYPE, },
58 { "pca9557", 8 | PCA953X_TYPE, },
59 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
60 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
61 { "pca9698", 40 | PCA953X_TYPE, },
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, },
71 { "xra1202", 8 | PCA953X_TYPE },
74 MODULE_DEVICE_TABLE(i2c, pca953x_id);
79 #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
83 u8 reg_output[MAX_BANK];
84 u8 reg_direction[MAX_BANK];
85 struct mutex i2c_lock;
87 #ifdef CONFIG_GPIO_PCA953X_IRQ
88 struct mutex irq_lock;
89 u8 irq_mask[MAX_BANK];
90 u8 irq_stat[MAX_BANK];
91 u8 irq_trig_raise[MAX_BANK];
92 u8 irq_trig_fall[MAX_BANK];
95 struct i2c_client *client;
96 struct gpio_chip gpio_chip;
97 const char *const *names;
101 static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
103 return container_of(gc, struct pca953x_chip, gpio_chip);
106 static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
110 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
111 int offset = off / BANK_SZ;
113 ret = i2c_smbus_read_byte_data(chip->client,
114 (reg << bank_shift) + offset);
118 dev_err(&chip->client->dev, "failed reading register\n");
125 static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
129 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
130 int offset = off / BANK_SZ;
132 ret = i2c_smbus_write_byte_data(chip->client,
133 (reg << bank_shift) + offset, val);
136 dev_err(&chip->client->dev, "failed writing register\n");
143 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
147 if (chip->gpio_chip.ngpio <= 8)
148 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
149 else if (chip->gpio_chip.ngpio >= 24) {
150 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
151 ret = i2c_smbus_write_i2c_block_data(chip->client,
152 (reg << bank_shift) | REG_ADDR_AI,
155 switch (chip->chip_type) {
157 ret = i2c_smbus_write_word_data(chip->client,
158 reg << 1, cpu_to_le16(get_unaligned((u16 *)val)));
161 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
165 ret = i2c_smbus_write_byte_data(chip->client,
173 dev_err(&chip->client->dev, "failed writing register\n");
180 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
184 if (chip->gpio_chip.ngpio <= 8) {
185 ret = i2c_smbus_read_byte_data(chip->client, reg);
187 } else if (chip->gpio_chip.ngpio >= 24) {
188 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
190 ret = i2c_smbus_read_i2c_block_data(chip->client,
191 (reg << bank_shift) | REG_ADDR_AI,
194 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
195 val[0] = (u16)ret & 0xFF;
196 val[1] = (u16)ret >> 8;
199 dev_err(&chip->client->dev, "failed reading register\n");
206 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
208 struct pca953x_chip *chip = to_pca(gc);
212 mutex_lock(&chip->i2c_lock);
213 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
215 switch (chip->chip_type) {
217 offset = PCA953X_DIRECTION;
220 offset = PCA957X_CFG;
223 ret = pca953x_write_single(chip, offset, reg_val, off);
227 chip->reg_direction[off / BANK_SZ] = reg_val;
230 mutex_unlock(&chip->i2c_lock);
234 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
235 unsigned off, int val)
237 struct pca953x_chip *chip = to_pca(gc);
241 mutex_lock(&chip->i2c_lock);
242 /* set output level */
244 reg_val = chip->reg_output[off / BANK_SZ]
245 | (1u << (off % BANK_SZ));
247 reg_val = chip->reg_output[off / BANK_SZ]
248 & ~(1u << (off % BANK_SZ));
250 switch (chip->chip_type) {
252 offset = PCA953X_OUTPUT;
255 offset = PCA957X_OUT;
258 ret = pca953x_write_single(chip, offset, reg_val, off);
262 chip->reg_output[off / BANK_SZ] = reg_val;
265 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
266 switch (chip->chip_type) {
268 offset = PCA953X_DIRECTION;
271 offset = PCA957X_CFG;
274 ret = pca953x_write_single(chip, offset, reg_val, off);
278 chip->reg_direction[off / BANK_SZ] = reg_val;
281 mutex_unlock(&chip->i2c_lock);
285 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
287 struct pca953x_chip *chip = to_pca(gc);
291 mutex_lock(&chip->i2c_lock);
292 switch (chip->chip_type) {
294 offset = PCA953X_INPUT;
300 ret = pca953x_read_single(chip, offset, ®_val, off);
301 mutex_unlock(&chip->i2c_lock);
303 /* NOTE: diagnostic already emitted; that's all we should
304 * do unless gpio_*_value_cansleep() calls become different
305 * from their nonsleeping siblings (and report faults).
310 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
313 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
315 struct pca953x_chip *chip = to_pca(gc);
319 mutex_lock(&chip->i2c_lock);
321 reg_val = chip->reg_output[off / BANK_SZ]
322 | (1u << (off % BANK_SZ));
324 reg_val = chip->reg_output[off / BANK_SZ]
325 & ~(1u << (off % BANK_SZ));
327 switch (chip->chip_type) {
329 offset = PCA953X_OUTPUT;
332 offset = PCA957X_OUT;
335 ret = pca953x_write_single(chip, offset, reg_val, off);
339 chip->reg_output[off / BANK_SZ] = reg_val;
341 mutex_unlock(&chip->i2c_lock);
344 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
346 struct gpio_chip *gc;
348 gc = &chip->gpio_chip;
350 gc->direction_input = pca953x_gpio_direction_input;
351 gc->direction_output = pca953x_gpio_direction_output;
352 gc->get = pca953x_gpio_get_value;
353 gc->set = pca953x_gpio_set_value;
354 gc->can_sleep = true;
356 gc->base = chip->gpio_start;
358 gc->label = chip->client->name;
359 gc->dev = &chip->client->dev;
360 gc->owner = THIS_MODULE;
361 gc->names = chip->names;
364 #ifdef CONFIG_GPIO_PCA953X_IRQ
365 static void pca953x_irq_mask(struct irq_data *d)
367 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
368 struct pca953x_chip *chip = to_pca(gc);
370 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
373 static void pca953x_irq_unmask(struct irq_data *d)
375 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
376 struct pca953x_chip *chip = to_pca(gc);
378 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
381 static void pca953x_irq_bus_lock(struct irq_data *d)
383 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
384 struct pca953x_chip *chip = to_pca(gc);
386 mutex_lock(&chip->irq_lock);
389 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
391 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
392 struct pca953x_chip *chip = to_pca(gc);
396 /* Look for any newly setup interrupt */
397 for (i = 0; i < NBANK(chip); i++) {
398 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
399 new_irqs &= ~chip->reg_direction[i];
402 level = __ffs(new_irqs);
403 pca953x_gpio_direction_input(&chip->gpio_chip,
404 level + (BANK_SZ * i));
405 new_irqs &= ~(1 << level);
409 mutex_unlock(&chip->irq_lock);
412 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
414 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
415 struct pca953x_chip *chip = to_pca(gc);
416 int bank_nb = d->hwirq / BANK_SZ;
417 u8 mask = 1 << (d->hwirq % BANK_SZ);
419 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
420 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
425 if (type & IRQ_TYPE_EDGE_FALLING)
426 chip->irq_trig_fall[bank_nb] |= mask;
428 chip->irq_trig_fall[bank_nb] &= ~mask;
430 if (type & IRQ_TYPE_EDGE_RISING)
431 chip->irq_trig_raise[bank_nb] |= mask;
433 chip->irq_trig_raise[bank_nb] &= ~mask;
438 static struct irq_chip pca953x_irq_chip = {
440 .irq_mask = pca953x_irq_mask,
441 .irq_unmask = pca953x_irq_unmask,
442 .irq_bus_lock = pca953x_irq_bus_lock,
443 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
444 .irq_set_type = pca953x_irq_set_type,
447 static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
449 u8 cur_stat[MAX_BANK];
450 u8 old_stat[MAX_BANK];
452 u8 trigger[MAX_BANK], triggers = 0;
453 int ret, i, offset = 0;
455 switch (chip->chip_type) {
457 offset = PCA953X_INPUT;
463 ret = pca953x_read_regs(chip, offset, cur_stat);
467 /* Remove output pins from the equation */
468 for (i = 0; i < NBANK(chip); i++)
469 cur_stat[i] &= chip->reg_direction[i];
471 memcpy(old_stat, chip->irq_stat, NBANK(chip));
473 for (i = 0; i < NBANK(chip); i++) {
474 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
475 triggers += trigger[i];
481 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
483 for (i = 0; i < NBANK(chip); i++) {
484 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
485 (cur_stat[i] & chip->irq_trig_raise[i]);
486 pending[i] &= trigger[i];
487 pendings += pending[i];
493 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
495 struct pca953x_chip *chip = devid;
496 u8 pending[MAX_BANK];
498 unsigned nhandled = 0;
501 if (!pca953x_irq_pending(chip, pending))
504 for (i = 0; i < NBANK(chip); i++) {
506 level = __ffs(pending[i]);
507 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
508 level + (BANK_SZ * i)));
509 pending[i] &= ~(1 << level);
514 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
517 static int pca953x_irq_setup(struct pca953x_chip *chip,
518 const struct i2c_device_id *id,
521 struct i2c_client *client = chip->client;
522 int ret, i, offset = 0;
525 && (id->driver_data & PCA_INT)) {
527 switch (chip->chip_type) {
529 offset = PCA953X_INPUT;
535 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
540 * There is no way to know which GPIO line generated the
541 * interrupt. We have to rely on the previous read for
544 for (i = 0; i < NBANK(chip); i++)
545 chip->irq_stat[i] &= chip->reg_direction[i];
546 mutex_init(&chip->irq_lock);
548 ret = devm_request_threaded_irq(&client->dev,
552 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
554 dev_name(&client->dev), chip);
556 dev_err(&client->dev, "failed to request irq %d\n",
561 ret = gpiochip_irqchip_add(&chip->gpio_chip,
567 dev_err(&client->dev,
568 "could not connect irqchip to gpiochip\n");
576 #else /* CONFIG_GPIO_PCA953X_IRQ */
577 static int pca953x_irq_setup(struct pca953x_chip *chip,
578 const struct i2c_device_id *id,
581 struct i2c_client *client = chip->client;
583 if (irq_base != -1 && (id->driver_data & PCA_INT))
584 dev_warn(&client->dev, "interrupt support not compiled in\n");
591 * Handlers for alternative sources of platform_data
593 #ifdef CONFIG_OF_GPIO
595 * Translate OpenFirmware node properties into platform_data
596 * WARNING: This is DEPRECATED and will be removed eventually!
599 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
601 struct device_node *node;
607 node = client->dev.of_node;
611 val = of_get_property(node, "linux,gpio-base", &size);
612 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
614 if (size != sizeof(*val))
615 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
618 *gpio_base = be32_to_cpup(val);
621 val = of_get_property(node, "polarity", NULL);
622 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
628 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
634 static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
639 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
643 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
644 chip->reg_direction);
648 /* set platform specific polarity inversion */
650 memset(val, 0xFF, NBANK(chip));
652 memset(val, 0, NBANK(chip));
654 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
659 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
664 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
667 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
671 /* set platform specific polarity inversion */
673 memset(val, 0xFF, NBANK(chip));
675 memset(val, 0, NBANK(chip));
676 pca953x_write_regs(chip, PCA957X_INVRT, val);
678 /* To enable register 6, 7 to controll pull up and pull down */
679 memset(val, 0x02, NBANK(chip));
680 pca953x_write_regs(chip, PCA957X_BKEN, val);
687 static int pca953x_probe(struct i2c_client *client,
688 const struct i2c_device_id *id)
690 struct pca953x_platform_data *pdata;
691 struct pca953x_chip *chip;
696 chip = devm_kzalloc(&client->dev,
697 sizeof(struct pca953x_chip), GFP_KERNEL);
701 pdata = dev_get_platdata(&client->dev);
703 irq_base = pdata->irq_base;
704 chip->gpio_start = pdata->gpio_base;
705 invert = pdata->invert;
706 chip->names = pdata->names;
708 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
709 #ifdef CONFIG_OF_GPIO
710 /* If I2C node has no interrupts property, disable GPIO interrupts */
711 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
716 chip->client = client;
718 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
720 mutex_init(&chip->i2c_lock);
722 /* initialize cached registers from their original values.
723 * we can't share this chip with another i2c master.
725 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
727 if (chip->chip_type == PCA953X_TYPE)
728 ret = device_pca953x_init(chip, invert);
730 ret = device_pca957x_init(chip, invert);
734 ret = gpiochip_add(&chip->gpio_chip);
738 ret = pca953x_irq_setup(chip, id, irq_base);
742 if (pdata && pdata->setup) {
743 ret = pdata->setup(client, chip->gpio_chip.base,
744 chip->gpio_chip.ngpio, pdata->context);
746 dev_warn(&client->dev, "setup failed, %d\n", ret);
749 i2c_set_clientdata(client, chip);
753 static int pca953x_remove(struct i2c_client *client)
755 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
756 struct pca953x_chip *chip = i2c_get_clientdata(client);
759 if (pdata && pdata->teardown) {
760 ret = pdata->teardown(client, chip->gpio_chip.base,
761 chip->gpio_chip.ngpio, pdata->context);
763 dev_err(&client->dev, "%s failed, %d\n",
769 ret = gpiochip_remove(&chip->gpio_chip);
771 dev_err(&client->dev, "%s failed, %d\n",
772 "gpiochip_remove()", ret);
779 static const struct of_device_id pca953x_dt_ids[] = {
780 { .compatible = "nxp,pca9505", },
781 { .compatible = "nxp,pca9534", },
782 { .compatible = "nxp,pca9535", },
783 { .compatible = "nxp,pca9536", },
784 { .compatible = "nxp,pca9537", },
785 { .compatible = "nxp,pca9538", },
786 { .compatible = "nxp,pca9539", },
787 { .compatible = "nxp,pca9554", },
788 { .compatible = "nxp,pca9555", },
789 { .compatible = "nxp,pca9556", },
790 { .compatible = "nxp,pca9557", },
791 { .compatible = "nxp,pca9574", },
792 { .compatible = "nxp,pca9575", },
793 { .compatible = "nxp,pca9698", },
795 { .compatible = "maxim,max7310", },
796 { .compatible = "maxim,max7312", },
797 { .compatible = "maxim,max7313", },
798 { .compatible = "maxim,max7315", },
800 { .compatible = "ti,pca6107", },
801 { .compatible = "ti,tca6408", },
802 { .compatible = "ti,tca6416", },
803 { .compatible = "ti,tca6424", },
805 { .compatible = "exar,xra1202", },
809 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
811 static struct i2c_driver pca953x_driver = {
814 .of_match_table = pca953x_dt_ids,
816 .probe = pca953x_probe,
817 .remove = pca953x_remove,
818 .id_table = pca953x_id,
821 static int __init pca953x_init(void)
823 return i2c_add_driver(&pca953x_driver);
825 /* register after i2c postcore initcall and before
826 * subsys initcalls that may rely on these GPIOs
828 subsys_initcall(pca953x_init);
830 static void __exit pca953x_exit(void)
832 i2c_del_driver(&pca953x_driver);
834 module_exit(pca953x_exit);
836 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
837 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
838 MODULE_LICENSE("GPL");