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/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, },
62 { "pca9698", 40 | PCA953X_TYPE, },
64 { "max7310", 8 | PCA953X_TYPE, },
65 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
66 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
67 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
68 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
69 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
70 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
71 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
74 MODULE_DEVICE_TABLE(i2c, pca953x_id);
79 #define NBANK(chip) (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];
93 struct irq_domain *domain;
96 struct i2c_client *client;
97 struct gpio_chip gpio_chip;
98 const char *const *names;
102 static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
106 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
107 int offset = off / BANK_SZ;
109 ret = i2c_smbus_read_byte_data(chip->client,
110 (reg << bank_shift) + offset);
114 dev_err(&chip->client->dev, "failed reading register\n");
121 static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
125 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
126 int offset = off / BANK_SZ;
128 ret = i2c_smbus_write_byte_data(chip->client,
129 (reg << bank_shift) + offset, val);
132 dev_err(&chip->client->dev, "failed writing register\n");
139 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
143 if (chip->gpio_chip.ngpio <= 8)
144 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
145 else if (chip->gpio_chip.ngpio >= 24) {
146 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
147 ret = i2c_smbus_write_i2c_block_data(chip->client,
148 (reg << bank_shift) | REG_ADDR_AI,
151 switch (chip->chip_type) {
153 ret = i2c_smbus_write_word_data(chip->client,
154 reg << 1, (u16) *val);
157 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
161 ret = i2c_smbus_write_byte_data(chip->client,
169 dev_err(&chip->client->dev, "failed writing register\n");
176 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
180 if (chip->gpio_chip.ngpio <= 8) {
181 ret = i2c_smbus_read_byte_data(chip->client, reg);
183 } else if (chip->gpio_chip.ngpio >= 24) {
184 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
186 ret = i2c_smbus_read_i2c_block_data(chip->client,
187 (reg << bank_shift) | REG_ADDR_AI,
190 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
191 val[0] = (u16)ret & 0xFF;
192 val[1] = (u16)ret >> 8;
195 dev_err(&chip->client->dev, "failed reading register\n");
202 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
204 struct pca953x_chip *chip;
208 chip = container_of(gc, struct pca953x_chip, gpio_chip);
210 mutex_lock(&chip->i2c_lock);
211 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
213 switch (chip->chip_type) {
215 offset = PCA953X_DIRECTION;
218 offset = PCA957X_CFG;
221 ret = pca953x_write_single(chip, offset, reg_val, off);
225 chip->reg_direction[off / BANK_SZ] = reg_val;
228 mutex_unlock(&chip->i2c_lock);
232 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
233 unsigned off, int val)
235 struct pca953x_chip *chip;
239 chip = container_of(gc, struct pca953x_chip, gpio_chip);
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;
291 chip = container_of(gc, struct pca953x_chip, gpio_chip);
293 mutex_lock(&chip->i2c_lock);
294 switch (chip->chip_type) {
296 offset = PCA953X_INPUT;
302 ret = pca953x_read_single(chip, offset, ®_val, off);
303 mutex_unlock(&chip->i2c_lock);
305 /* NOTE: diagnostic already emitted; that's all we should
306 * do unless gpio_*_value_cansleep() calls become different
307 * from their nonsleeping siblings (and report faults).
312 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
315 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
317 struct pca953x_chip *chip;
321 chip = container_of(gc, struct pca953x_chip, gpio_chip);
323 mutex_lock(&chip->i2c_lock);
325 reg_val = chip->reg_output[off / BANK_SZ]
326 | (1u << (off % BANK_SZ));
328 reg_val = chip->reg_output[off / BANK_SZ]
329 & ~(1u << (off % BANK_SZ));
331 switch (chip->chip_type) {
333 offset = PCA953X_OUTPUT;
336 offset = PCA957X_OUT;
339 ret = pca953x_write_single(chip, offset, reg_val, off);
343 chip->reg_output[off / BANK_SZ] = reg_val;
345 mutex_unlock(&chip->i2c_lock);
348 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
350 struct gpio_chip *gc;
352 gc = &chip->gpio_chip;
354 gc->direction_input = pca953x_gpio_direction_input;
355 gc->direction_output = pca953x_gpio_direction_output;
356 gc->get = pca953x_gpio_get_value;
357 gc->set = pca953x_gpio_set_value;
358 gc->can_sleep = true;
360 gc->base = chip->gpio_start;
362 gc->label = chip->client->name;
363 gc->dev = &chip->client->dev;
364 gc->owner = THIS_MODULE;
365 gc->names = chip->names;
368 #ifdef CONFIG_GPIO_PCA953X_IRQ
369 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
371 struct pca953x_chip *chip;
373 chip = container_of(gc, struct pca953x_chip, gpio_chip);
374 return irq_create_mapping(chip->domain, off);
377 static void pca953x_irq_mask(struct irq_data *d)
379 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
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 pca953x_chip *chip = irq_data_get_irq_chip_data(d);
388 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
391 static void pca953x_irq_bus_lock(struct irq_data *d)
393 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
395 mutex_lock(&chip->irq_lock);
398 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
400 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
404 /* Look for any newly setup interrupt */
405 for (i = 0; i < NBANK(chip); i++) {
406 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
407 new_irqs &= ~chip->reg_direction[i];
410 level = __ffs(new_irqs);
411 pca953x_gpio_direction_input(&chip->gpio_chip,
412 level + (BANK_SZ * i));
413 new_irqs &= ~(1 << level);
417 mutex_unlock(&chip->irq_lock);
420 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
422 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
423 int bank_nb = d->hwirq / BANK_SZ;
424 u8 mask = 1 << (d->hwirq % BANK_SZ);
426 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
427 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
432 if (type & IRQ_TYPE_EDGE_FALLING)
433 chip->irq_trig_fall[bank_nb] |= mask;
435 chip->irq_trig_fall[bank_nb] &= ~mask;
437 if (type & IRQ_TYPE_EDGE_RISING)
438 chip->irq_trig_raise[bank_nb] |= mask;
440 chip->irq_trig_raise[bank_nb] &= ~mask;
445 static struct irq_chip pca953x_irq_chip = {
447 .irq_mask = pca953x_irq_mask,
448 .irq_unmask = pca953x_irq_unmask,
449 .irq_bus_lock = pca953x_irq_bus_lock,
450 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
451 .irq_set_type = pca953x_irq_set_type,
454 static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
456 u8 cur_stat[MAX_BANK];
457 u8 old_stat[MAX_BANK];
459 u8 trigger[MAX_BANK], triggers = 0;
460 int ret, i, offset = 0;
462 switch (chip->chip_type) {
464 offset = PCA953X_INPUT;
470 ret = pca953x_read_regs(chip, offset, cur_stat);
474 /* Remove output pins from the equation */
475 for (i = 0; i < NBANK(chip); i++)
476 cur_stat[i] &= chip->reg_direction[i];
478 memcpy(old_stat, chip->irq_stat, NBANK(chip));
480 for (i = 0; i < NBANK(chip); i++) {
481 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
482 triggers += trigger[i];
488 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
490 for (i = 0; i < NBANK(chip); i++) {
491 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
492 (cur_stat[i] & chip->irq_trig_raise[i]);
493 pending[i] &= trigger[i];
494 pendings += pending[i];
500 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
502 struct pca953x_chip *chip = devid;
503 u8 pending[MAX_BANK];
507 if (!pca953x_irq_pending(chip, pending))
510 for (i = 0; i < NBANK(chip); i++) {
512 level = __ffs(pending[i]);
513 handle_nested_irq(irq_find_mapping(chip->domain,
514 level + (BANK_SZ * i)));
515 pending[i] &= ~(1 << level);
522 static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
523 irq_hw_number_t hwirq)
525 irq_clear_status_flags(irq, IRQ_NOREQUEST);
526 irq_set_chip_data(irq, d->host_data);
527 irq_set_chip(irq, &pca953x_irq_chip);
528 irq_set_nested_thread(irq, true);
530 set_irq_flags(irq, IRQF_VALID);
532 irq_set_noprobe(irq);
538 static const struct irq_domain_ops pca953x_irq_simple_ops = {
539 .map = pca953x_gpio_irq_map,
540 .xlate = irq_domain_xlate_twocell,
543 static int pca953x_irq_setup(struct pca953x_chip *chip,
544 const struct i2c_device_id *id,
547 struct i2c_client *client = chip->client;
548 int ret, i, offset = 0;
551 && (id->driver_data & PCA_INT)) {
553 switch (chip->chip_type) {
555 offset = PCA953X_INPUT;
561 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
566 * There is no way to know which GPIO line generated the
567 * interrupt. We have to rely on the previous read for
570 for (i = 0; i < NBANK(chip); i++)
571 chip->irq_stat[i] &= chip->reg_direction[i];
572 mutex_init(&chip->irq_lock);
574 chip->domain = irq_domain_add_simple(client->dev.of_node,
575 chip->gpio_chip.ngpio,
577 &pca953x_irq_simple_ops,
582 ret = devm_request_threaded_irq(&client->dev,
586 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
587 dev_name(&client->dev), chip);
589 dev_err(&client->dev, "failed to request irq %d\n",
594 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
600 #else /* CONFIG_GPIO_PCA953X_IRQ */
601 static int pca953x_irq_setup(struct pca953x_chip *chip,
602 const struct i2c_device_id *id,
605 struct i2c_client *client = chip->client;
607 if (irq_base != -1 && (id->driver_data & PCA_INT))
608 dev_warn(&client->dev, "interrupt support not compiled in\n");
615 * Handlers for alternative sources of platform_data
617 #ifdef CONFIG_OF_GPIO
619 * Translate OpenFirmware node properties into platform_data
620 * WARNING: This is DEPRECATED and will be removed eventually!
623 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
625 struct device_node *node;
629 node = client->dev.of_node;
634 val = of_get_property(node, "linux,gpio-base", &size);
635 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
637 if (size != sizeof(*val))
638 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
641 *gpio_base = be32_to_cpup(val);
644 val = of_get_property(node, "polarity", NULL);
645 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
651 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
657 static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
662 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
666 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
667 chip->reg_direction);
671 /* set platform specific polarity inversion */
673 memset(val, 0xFF, NBANK(chip));
675 memset(val, 0, NBANK(chip));
677 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
682 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
687 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
690 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
694 /* set platform specific polarity inversion */
696 memset(val, 0xFF, NBANK(chip));
698 memset(val, 0, NBANK(chip));
699 pca953x_write_regs(chip, PCA957X_INVRT, val);
701 /* To enable register 6, 7 to controll pull up and pull down */
702 memset(val, 0x02, NBANK(chip));
703 pca953x_write_regs(chip, PCA957X_BKEN, val);
710 static int pca953x_probe(struct i2c_client *client,
711 const struct i2c_device_id *id)
713 struct pca953x_platform_data *pdata;
714 struct pca953x_chip *chip;
719 chip = devm_kzalloc(&client->dev,
720 sizeof(struct pca953x_chip), GFP_KERNEL);
724 pdata = dev_get_platdata(&client->dev);
726 irq_base = pdata->irq_base;
727 chip->gpio_start = pdata->gpio_base;
728 invert = pdata->invert;
729 chip->names = pdata->names;
731 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
732 #ifdef CONFIG_OF_GPIO
733 /* If I2C node has no interrupts property, disable GPIO interrupts */
734 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
739 chip->client = client;
741 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
743 mutex_init(&chip->i2c_lock);
745 /* initialize cached registers from their original values.
746 * we can't share this chip with another i2c master.
748 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
750 if (chip->chip_type == PCA953X_TYPE)
751 ret = device_pca953x_init(chip, invert);
753 ret = device_pca957x_init(chip, invert);
757 ret = pca953x_irq_setup(chip, id, irq_base);
761 ret = gpiochip_add(&chip->gpio_chip);
765 if (pdata && pdata->setup) {
766 ret = pdata->setup(client, chip->gpio_chip.base,
767 chip->gpio_chip.ngpio, pdata->context);
769 dev_warn(&client->dev, "setup failed, %d\n", ret);
772 i2c_set_clientdata(client, chip);
776 static int pca953x_remove(struct i2c_client *client)
778 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
779 struct pca953x_chip *chip = i2c_get_clientdata(client);
782 if (pdata && pdata->teardown) {
783 ret = pdata->teardown(client, chip->gpio_chip.base,
784 chip->gpio_chip.ngpio, pdata->context);
786 dev_err(&client->dev, "%s failed, %d\n",
792 ret = gpiochip_remove(&chip->gpio_chip);
794 dev_err(&client->dev, "%s failed, %d\n",
795 "gpiochip_remove()", ret);
802 static const struct of_device_id pca953x_dt_ids[] = {
803 { .compatible = "nxp,pca9505", },
804 { .compatible = "nxp,pca9534", },
805 { .compatible = "nxp,pca9535", },
806 { .compatible = "nxp,pca9536", },
807 { .compatible = "nxp,pca9537", },
808 { .compatible = "nxp,pca9538", },
809 { .compatible = "nxp,pca9539", },
810 { .compatible = "nxp,pca9554", },
811 { .compatible = "nxp,pca9555", },
812 { .compatible = "nxp,pca9556", },
813 { .compatible = "nxp,pca9557", },
814 { .compatible = "nxp,pca9574", },
815 { .compatible = "nxp,pca9575", },
816 { .compatible = "nxp,pca9698", },
818 { .compatible = "maxim,max7310", },
819 { .compatible = "maxim,max7312", },
820 { .compatible = "maxim,max7313", },
821 { .compatible = "maxim,max7315", },
823 { .compatible = "ti,pca6107", },
824 { .compatible = "ti,tca6408", },
825 { .compatible = "ti,tca6416", },
826 { .compatible = "ti,tca6424", },
830 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
832 static struct i2c_driver pca953x_driver = {
835 .of_match_table = pca953x_dt_ids,
837 .probe = pca953x_probe,
838 .remove = pca953x_remove,
839 .id_table = pca953x_id,
842 static int __init pca953x_init(void)
844 return i2c_add_driver(&pca953x_driver);
846 /* register after i2c postcore initcall and before
847 * subsys initcalls that may rely on these GPIOs
849 subsys_initcall(pca953x_init);
851 static void __exit pca953x_exit(void)
853 i2c_del_driver(&pca953x_driver);
855 module_exit(pca953x_exit);
857 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
858 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
859 MODULE_LICENSE("GPL");