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>
25 #define PCA953X_INPUT 0
26 #define PCA953X_OUTPUT 1
27 #define PCA953X_INVERT 2
28 #define PCA953X_DIRECTION 3
30 #define REG_ADDR_AI 0x80
33 #define PCA957X_INVRT 1
34 #define PCA957X_BKEN 2
35 #define PCA957X_PUPD 3
39 #define PCA957X_INTS 7
41 #define PCA_GPIO_MASK 0x00FF
42 #define PCA_INT 0x0100
43 #define PCA953X_TYPE 0x1000
44 #define PCA957X_TYPE 0x2000
45 #define PCA_TYPE_MASK 0xF000
47 #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
49 static const struct i2c_device_id pca953x_id[] = {
50 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
51 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
52 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
53 { "pca9536", 4 | PCA953X_TYPE, },
54 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
55 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
56 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
57 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
58 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
59 { "pca9556", 8 | PCA953X_TYPE, },
60 { "pca9557", 8 | PCA953X_TYPE, },
61 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
62 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
63 { "pca9698", 40 | PCA953X_TYPE, },
65 { "max7310", 8 | PCA953X_TYPE, },
66 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
67 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
68 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
69 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
70 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
71 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
72 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
73 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
74 { "xra1202", 8 | PCA953X_TYPE },
77 MODULE_DEVICE_TABLE(i2c, pca953x_id);
82 #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
86 u8 reg_output[MAX_BANK];
87 u8 reg_direction[MAX_BANK];
88 struct mutex i2c_lock;
90 #ifdef CONFIG_GPIO_PCA953X_IRQ
91 struct mutex irq_lock;
92 u8 irq_mask[MAX_BANK];
93 u8 irq_stat[MAX_BANK];
94 u8 irq_trig_raise[MAX_BANK];
95 u8 irq_trig_fall[MAX_BANK];
98 struct i2c_client *client;
99 struct gpio_chip gpio_chip;
100 const char *const *names;
102 unsigned long driver_data;
105 static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
107 return container_of(gc, struct pca953x_chip, gpio_chip);
110 static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
114 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
115 int offset = off / BANK_SZ;
117 ret = i2c_smbus_read_byte_data(chip->client,
118 (reg << bank_shift) + offset);
122 dev_err(&chip->client->dev, "failed reading register\n");
129 static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
133 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
134 int offset = off / BANK_SZ;
136 ret = i2c_smbus_write_byte_data(chip->client,
137 (reg << bank_shift) + offset, val);
140 dev_err(&chip->client->dev, "failed writing register\n");
147 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
151 if (chip->gpio_chip.ngpio <= 8)
152 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
153 else if (chip->gpio_chip.ngpio >= 24) {
154 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
155 ret = i2c_smbus_write_i2c_block_data(chip->client,
156 (reg << bank_shift) | REG_ADDR_AI,
159 switch (chip->chip_type) {
161 ret = i2c_smbus_write_word_data(chip->client,
162 reg << 1, (u16) *val);
165 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
169 ret = i2c_smbus_write_byte_data(chip->client,
177 dev_err(&chip->client->dev, "failed writing register\n");
184 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
188 if (chip->gpio_chip.ngpio <= 8) {
189 ret = i2c_smbus_read_byte_data(chip->client, reg);
191 } else if (chip->gpio_chip.ngpio >= 24) {
192 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
194 ret = i2c_smbus_read_i2c_block_data(chip->client,
195 (reg << bank_shift) | REG_ADDR_AI,
198 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
199 val[0] = (u16)ret & 0xFF;
200 val[1] = (u16)ret >> 8;
203 dev_err(&chip->client->dev, "failed reading register\n");
210 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
212 struct pca953x_chip *chip = to_pca(gc);
216 mutex_lock(&chip->i2c_lock);
217 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
219 switch (chip->chip_type) {
221 offset = PCA953X_DIRECTION;
224 offset = PCA957X_CFG;
227 ret = pca953x_write_single(chip, offset, reg_val, off);
231 chip->reg_direction[off / BANK_SZ] = reg_val;
234 mutex_unlock(&chip->i2c_lock);
238 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
239 unsigned off, int val)
241 struct pca953x_chip *chip = to_pca(gc);
245 mutex_lock(&chip->i2c_lock);
246 /* set output level */
248 reg_val = chip->reg_output[off / BANK_SZ]
249 | (1u << (off % BANK_SZ));
251 reg_val = chip->reg_output[off / BANK_SZ]
252 & ~(1u << (off % BANK_SZ));
254 switch (chip->chip_type) {
256 offset = PCA953X_OUTPUT;
259 offset = PCA957X_OUT;
262 ret = pca953x_write_single(chip, offset, reg_val, off);
266 chip->reg_output[off / BANK_SZ] = reg_val;
269 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
270 switch (chip->chip_type) {
272 offset = PCA953X_DIRECTION;
275 offset = PCA957X_CFG;
278 ret = pca953x_write_single(chip, offset, reg_val, off);
282 chip->reg_direction[off / BANK_SZ] = reg_val;
285 mutex_unlock(&chip->i2c_lock);
289 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
291 struct pca953x_chip *chip = to_pca(gc);
295 mutex_lock(&chip->i2c_lock);
296 switch (chip->chip_type) {
298 offset = PCA953X_INPUT;
304 ret = pca953x_read_single(chip, offset, ®_val, off);
305 mutex_unlock(&chip->i2c_lock);
307 /* NOTE: diagnostic already emitted; that's all we should
308 * do unless gpio_*_value_cansleep() calls become different
309 * from their nonsleeping siblings (and report faults).
314 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
317 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
319 struct pca953x_chip *chip = to_pca(gc);
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 void pca953x_irq_mask(struct irq_data *d)
371 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
372 struct pca953x_chip *chip = to_pca(gc);
374 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
377 static void pca953x_irq_unmask(struct irq_data *d)
379 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
380 struct pca953x_chip *chip = to_pca(gc);
382 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
385 static void pca953x_irq_bus_lock(struct irq_data *d)
387 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
388 struct pca953x_chip *chip = to_pca(gc);
390 mutex_lock(&chip->irq_lock);
393 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
395 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
396 struct pca953x_chip *chip = to_pca(gc);
400 /* Look for any newly setup interrupt */
401 for (i = 0; i < NBANK(chip); i++) {
402 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
403 new_irqs &= ~chip->reg_direction[i];
406 level = __ffs(new_irqs);
407 pca953x_gpio_direction_input(&chip->gpio_chip,
408 level + (BANK_SZ * i));
409 new_irqs &= ~(1 << level);
413 mutex_unlock(&chip->irq_lock);
416 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
418 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
419 struct pca953x_chip *chip = to_pca(gc);
420 int bank_nb = d->hwirq / BANK_SZ;
421 u8 mask = 1 << (d->hwirq % BANK_SZ);
423 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
424 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
429 if (type & IRQ_TYPE_EDGE_FALLING)
430 chip->irq_trig_fall[bank_nb] |= mask;
432 chip->irq_trig_fall[bank_nb] &= ~mask;
434 if (type & IRQ_TYPE_EDGE_RISING)
435 chip->irq_trig_raise[bank_nb] |= mask;
437 chip->irq_trig_raise[bank_nb] &= ~mask;
442 static struct irq_chip pca953x_irq_chip = {
444 .irq_mask = pca953x_irq_mask,
445 .irq_unmask = pca953x_irq_unmask,
446 .irq_bus_lock = pca953x_irq_bus_lock,
447 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
448 .irq_set_type = pca953x_irq_set_type,
451 static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
453 u8 cur_stat[MAX_BANK];
454 u8 old_stat[MAX_BANK];
455 bool pending_seen = false;
456 bool trigger_seen = false;
457 u8 trigger[MAX_BANK];
458 int ret, i, offset = 0;
460 switch (chip->chip_type) {
462 offset = PCA953X_INPUT;
468 ret = pca953x_read_regs(chip, offset, cur_stat);
472 /* Remove output pins from the equation */
473 for (i = 0; i < NBANK(chip); i++)
474 cur_stat[i] &= chip->reg_direction[i];
476 memcpy(old_stat, chip->irq_stat, NBANK(chip));
478 for (i = 0; i < NBANK(chip); i++) {
479 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[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];
500 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
502 struct pca953x_chip *chip = devid;
503 u8 pending[MAX_BANK];
505 unsigned nhandled = 0;
508 if (!pca953x_irq_pending(chip, pending))
511 for (i = 0; i < NBANK(chip); i++) {
513 level = __ffs(pending[i]);
514 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
515 level + (BANK_SZ * i)));
516 pending[i] &= ~(1 << level);
521 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
524 static int pca953x_irq_setup(struct pca953x_chip *chip,
527 struct i2c_client *client = chip->client;
528 int ret, i, offset = 0;
530 if (client->irq && irq_base != -1
531 && (chip->driver_data & PCA_INT)) {
533 switch (chip->chip_type) {
535 offset = PCA953X_INPUT;
541 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
546 * There is no way to know which GPIO line generated the
547 * interrupt. We have to rely on the previous read for
550 for (i = 0; i < NBANK(chip); i++)
551 chip->irq_stat[i] &= chip->reg_direction[i];
552 mutex_init(&chip->irq_lock);
554 ret = devm_request_threaded_irq(&client->dev,
558 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
560 dev_name(&client->dev), chip);
562 dev_err(&client->dev, "failed to request irq %d\n",
567 ret = gpiochip_irqchip_add(&chip->gpio_chip,
573 dev_err(&client->dev,
574 "could not connect irqchip to gpiochip\n");
578 gpiochip_set_chained_irqchip(&chip->gpio_chip,
586 #else /* CONFIG_GPIO_PCA953X_IRQ */
587 static int pca953x_irq_setup(struct pca953x_chip *chip,
590 struct i2c_client *client = chip->client;
592 if (irq_base != -1 && (chip->driver_data & PCA_INT))
593 dev_warn(&client->dev, "interrupt support not compiled in\n");
599 static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
604 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
608 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
609 chip->reg_direction);
613 /* set platform specific polarity inversion */
615 memset(val, 0xFF, NBANK(chip));
617 memset(val, 0, NBANK(chip));
619 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
624 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
629 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
632 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
636 /* set platform specific polarity inversion */
638 memset(val, 0xFF, NBANK(chip));
640 memset(val, 0, NBANK(chip));
641 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
645 /* To enable register 6, 7 to control pull up and pull down */
646 memset(val, 0x02, NBANK(chip));
647 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
656 static int pca953x_probe(struct i2c_client *client,
657 const struct i2c_device_id *id)
659 struct pca953x_platform_data *pdata;
660 struct pca953x_chip *chip;
665 chip = devm_kzalloc(&client->dev,
666 sizeof(struct pca953x_chip), GFP_KERNEL);
670 pdata = dev_get_platdata(&client->dev);
672 irq_base = pdata->irq_base;
673 chip->gpio_start = pdata->gpio_base;
674 invert = pdata->invert;
675 chip->names = pdata->names;
677 chip->gpio_start = -1;
681 chip->client = client;
683 chip->driver_data = id->driver_data;
684 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
686 mutex_init(&chip->i2c_lock);
688 /* initialize cached registers from their original values.
689 * we can't share this chip with another i2c master.
691 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
693 if (chip->chip_type == PCA953X_TYPE)
694 ret = device_pca953x_init(chip, invert);
696 ret = device_pca957x_init(chip, invert);
700 ret = gpiochip_add(&chip->gpio_chip);
704 ret = pca953x_irq_setup(chip, irq_base);
708 if (pdata && pdata->setup) {
709 ret = pdata->setup(client, chip->gpio_chip.base,
710 chip->gpio_chip.ngpio, pdata->context);
712 dev_warn(&client->dev, "setup failed, %d\n", ret);
715 i2c_set_clientdata(client, chip);
719 static int pca953x_remove(struct i2c_client *client)
721 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
722 struct pca953x_chip *chip = i2c_get_clientdata(client);
725 if (pdata && pdata->teardown) {
726 ret = pdata->teardown(client, chip->gpio_chip.base,
727 chip->gpio_chip.ngpio, pdata->context);
729 dev_err(&client->dev, "%s failed, %d\n",
735 gpiochip_remove(&chip->gpio_chip);
740 static const struct of_device_id pca953x_dt_ids[] = {
741 { .compatible = "nxp,pca9505", },
742 { .compatible = "nxp,pca9534", },
743 { .compatible = "nxp,pca9535", },
744 { .compatible = "nxp,pca9536", },
745 { .compatible = "nxp,pca9537", },
746 { .compatible = "nxp,pca9538", },
747 { .compatible = "nxp,pca9539", },
748 { .compatible = "nxp,pca9554", },
749 { .compatible = "nxp,pca9555", },
750 { .compatible = "nxp,pca9556", },
751 { .compatible = "nxp,pca9557", },
752 { .compatible = "nxp,pca9574", },
753 { .compatible = "nxp,pca9575", },
754 { .compatible = "nxp,pca9698", },
756 { .compatible = "maxim,max7310", },
757 { .compatible = "maxim,max7312", },
758 { .compatible = "maxim,max7313", },
759 { .compatible = "maxim,max7315", },
761 { .compatible = "ti,pca6107", },
762 { .compatible = "ti,tca6408", },
763 { .compatible = "ti,tca6416", },
764 { .compatible = "ti,tca6424", },
766 { .compatible = "exar,xra1202", },
770 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
772 static struct i2c_driver pca953x_driver = {
775 .of_match_table = pca953x_dt_ids,
777 .probe = pca953x_probe,
778 .remove = pca953x_remove,
779 .id_table = pca953x_id,
782 static int __init pca953x_init(void)
784 return i2c_add_driver(&pca953x_driver);
786 /* register after i2c postcore initcall and before
787 * subsys initcalls that may rely on these GPIOs
789 subsys_initcall(pca953x_init);
791 static void __exit pca953x_exit(void)
793 i2c_del_driver(&pca953x_driver);
795 module_exit(pca953x_exit);
797 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
798 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
799 MODULE_LICENSE("GPL");