2 * PCA953x 4/8/16 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/i2c/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 { "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, },
62 { "max7310", 8 | PCA953X_TYPE, },
63 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
64 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
65 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
66 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
67 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
68 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
69 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
72 MODULE_DEVICE_TABLE(i2c, pca953x_id);
78 struct mutex i2c_lock;
80 #ifdef CONFIG_GPIO_PCA953X_IRQ
81 struct mutex irq_lock;
87 struct irq_domain *domain;
90 struct i2c_client *client;
91 struct gpio_chip gpio_chip;
92 const char *const *names;
96 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, u32 val)
100 if (chip->gpio_chip.ngpio <= 8)
101 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
102 else if (chip->gpio_chip.ngpio == 24) {
104 ret = i2c_smbus_write_i2c_block_data(chip->client,
105 (reg << 2) | REG_ADDR_AI,
110 switch (chip->chip_type) {
112 ret = i2c_smbus_write_word_data(chip->client,
116 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
120 ret = i2c_smbus_write_byte_data(chip->client,
122 (val & 0xff00) >> 8);
128 dev_err(&chip->client->dev, "failed writing register\n");
135 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, u32 *val)
139 if (chip->gpio_chip.ngpio <= 8) {
140 ret = i2c_smbus_read_byte_data(chip->client, reg);
143 else if (chip->gpio_chip.ngpio == 24) {
145 ret = i2c_smbus_read_i2c_block_data(chip->client,
146 (reg << 2) | REG_ADDR_AI,
151 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
156 dev_err(&chip->client->dev, "failed reading register\n");
163 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
165 struct pca953x_chip *chip;
169 chip = container_of(gc, struct pca953x_chip, gpio_chip);
171 mutex_lock(&chip->i2c_lock);
172 reg_val = chip->reg_direction | (1u << off);
174 switch (chip->chip_type) {
176 offset = PCA953X_DIRECTION;
179 offset = PCA957X_CFG;
182 ret = pca953x_write_reg(chip, offset, reg_val);
186 chip->reg_direction = reg_val;
189 mutex_unlock(&chip->i2c_lock);
193 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
194 unsigned off, int val)
196 struct pca953x_chip *chip;
200 chip = container_of(gc, struct pca953x_chip, gpio_chip);
202 mutex_lock(&chip->i2c_lock);
203 /* set output level */
205 reg_val = chip->reg_output | (1u << off);
207 reg_val = chip->reg_output & ~(1u << off);
209 switch (chip->chip_type) {
211 offset = PCA953X_OUTPUT;
214 offset = PCA957X_OUT;
217 ret = pca953x_write_reg(chip, offset, reg_val);
221 chip->reg_output = reg_val;
224 reg_val = chip->reg_direction & ~(1u << off);
225 switch (chip->chip_type) {
227 offset = PCA953X_DIRECTION;
230 offset = PCA957X_CFG;
233 ret = pca953x_write_reg(chip, offset, reg_val);
237 chip->reg_direction = reg_val;
240 mutex_unlock(&chip->i2c_lock);
244 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
246 struct pca953x_chip *chip;
250 chip = container_of(gc, struct pca953x_chip, gpio_chip);
252 mutex_lock(&chip->i2c_lock);
253 switch (chip->chip_type) {
255 offset = PCA953X_INPUT;
261 ret = pca953x_read_reg(chip, offset, ®_val);
262 mutex_unlock(&chip->i2c_lock);
264 /* NOTE: diagnostic already emitted; that's all we should
265 * do unless gpio_*_value_cansleep() calls become different
266 * from their nonsleeping siblings (and report faults).
271 return (reg_val & (1u << off)) ? 1 : 0;
274 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
276 struct pca953x_chip *chip;
280 chip = container_of(gc, struct pca953x_chip, gpio_chip);
282 mutex_lock(&chip->i2c_lock);
284 reg_val = chip->reg_output | (1u << off);
286 reg_val = chip->reg_output & ~(1u << off);
288 switch (chip->chip_type) {
290 offset = PCA953X_OUTPUT;
293 offset = PCA957X_OUT;
296 ret = pca953x_write_reg(chip, offset, reg_val);
300 chip->reg_output = reg_val;
302 mutex_unlock(&chip->i2c_lock);
305 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
307 struct gpio_chip *gc;
309 gc = &chip->gpio_chip;
311 gc->direction_input = pca953x_gpio_direction_input;
312 gc->direction_output = pca953x_gpio_direction_output;
313 gc->get = pca953x_gpio_get_value;
314 gc->set = pca953x_gpio_set_value;
317 gc->base = chip->gpio_start;
319 gc->label = chip->client->name;
320 gc->dev = &chip->client->dev;
321 gc->owner = THIS_MODULE;
322 gc->names = chip->names;
325 #ifdef CONFIG_GPIO_PCA953X_IRQ
326 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
328 struct pca953x_chip *chip;
330 chip = container_of(gc, struct pca953x_chip, gpio_chip);
331 return chip->irq_base + off;
334 static void pca953x_irq_mask(struct irq_data *d)
336 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
338 chip->irq_mask &= ~(1 << d->hwirq);
341 static void pca953x_irq_unmask(struct irq_data *d)
343 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
345 chip->irq_mask |= 1 << d->hwirq;
348 static void pca953x_irq_bus_lock(struct irq_data *d)
350 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
352 mutex_lock(&chip->irq_lock);
355 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
357 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
361 /* Look for any newly setup interrupt */
362 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
363 new_irqs &= ~chip->reg_direction;
366 level = __ffs(new_irqs);
367 pca953x_gpio_direction_input(&chip->gpio_chip, level);
368 new_irqs &= ~(1 << level);
371 mutex_unlock(&chip->irq_lock);
374 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
376 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
377 u32 mask = 1 << d->hwirq;
379 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
380 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
385 if (type & IRQ_TYPE_EDGE_FALLING)
386 chip->irq_trig_fall |= mask;
388 chip->irq_trig_fall &= ~mask;
390 if (type & IRQ_TYPE_EDGE_RISING)
391 chip->irq_trig_raise |= mask;
393 chip->irq_trig_raise &= ~mask;
398 static struct irq_chip pca953x_irq_chip = {
400 .irq_mask = pca953x_irq_mask,
401 .irq_unmask = pca953x_irq_unmask,
402 .irq_bus_lock = pca953x_irq_bus_lock,
403 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
404 .irq_set_type = pca953x_irq_set_type,
407 static u32 pca953x_irq_pending(struct pca953x_chip *chip)
415 switch (chip->chip_type) {
417 offset = PCA953X_INPUT;
423 ret = pca953x_read_reg(chip, offset, &cur_stat);
427 /* Remove output pins from the equation */
428 cur_stat &= chip->reg_direction;
430 old_stat = chip->irq_stat;
431 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
436 chip->irq_stat = cur_stat;
438 pending = (old_stat & chip->irq_trig_fall) |
439 (cur_stat & chip->irq_trig_raise);
445 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
447 struct pca953x_chip *chip = devid;
451 pending = pca953x_irq_pending(chip);
457 level = __ffs(pending);
458 handle_nested_irq(irq_find_mapping(chip->domain, level));
460 pending &= ~(1 << level);
466 static int pca953x_irq_setup(struct pca953x_chip *chip,
467 const struct i2c_device_id *id,
470 struct i2c_client *client = chip->client;
475 && (id->driver_data & PCA_INT)) {
478 switch (chip->chip_type) {
480 offset = PCA953X_INPUT;
486 ret = pca953x_read_reg(chip, offset, &temporary);
487 chip->irq_stat = temporary;
492 * There is no way to know which GPIO line generated the
493 * interrupt. We have to rely on the previous read for
496 chip->irq_stat &= chip->reg_direction;
497 mutex_init(&chip->irq_lock);
499 chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
500 if (chip->irq_base < 0)
503 chip->domain = irq_domain_add_legacy(client->dev.of_node,
504 chip->gpio_chip.ngpio,
507 &irq_domain_simple_ops,
511 goto out_irqdesc_free;
514 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
515 int irq = lvl + chip->irq_base;
517 irq_clear_status_flags(irq, IRQ_NOREQUEST);
518 irq_set_chip_data(irq, chip);
519 irq_set_chip(irq, &pca953x_irq_chip);
520 irq_set_nested_thread(irq, true);
522 set_irq_flags(irq, IRQF_VALID);
524 irq_set_noprobe(irq);
528 ret = request_threaded_irq(client->irq,
531 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
532 dev_name(&client->dev), chip);
534 dev_err(&client->dev, "failed to request irq %d\n",
536 goto out_irqdesc_free;
539 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
545 irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
551 static void pca953x_irq_teardown(struct pca953x_chip *chip)
553 if (chip->irq_base != -1) {
554 irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
555 free_irq(chip->client->irq, chip);
558 #else /* CONFIG_GPIO_PCA953X_IRQ */
559 static int pca953x_irq_setup(struct pca953x_chip *chip,
560 const struct i2c_device_id *id,
563 struct i2c_client *client = chip->client;
565 if (irq_base != -1 && (id->driver_data & PCA_INT))
566 dev_warn(&client->dev, "interrupt support not compiled in\n");
571 static void pca953x_irq_teardown(struct pca953x_chip *chip)
577 * Handlers for alternative sources of platform_data
579 #ifdef CONFIG_OF_GPIO
581 * Translate OpenFirmware node properties into platform_data
582 * WARNING: This is DEPRECATED and will be removed eventually!
585 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
587 struct device_node *node;
591 node = client->dev.of_node;
596 val = of_get_property(node, "linux,gpio-base", &size);
597 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
599 if (size != sizeof(*val))
600 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
603 *gpio_base = be32_to_cpup(val);
606 val = of_get_property(node, "polarity", NULL);
607 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
613 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
619 static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
623 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
627 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
628 &chip->reg_direction);
632 /* set platform specific polarity inversion */
633 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
638 static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
643 /* Let every port in proper state, that could save power */
644 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
645 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
646 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
648 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
651 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
654 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
658 /* set platform specific polarity inversion */
659 pca953x_write_reg(chip, PCA957X_INVRT, invert);
661 /* To enable register 6, 7 to controll pull up and pull down */
662 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
669 static int pca953x_probe(struct i2c_client *client,
670 const struct i2c_device_id *id)
672 struct pca953x_platform_data *pdata;
673 struct pca953x_chip *chip;
678 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
682 pdata = client->dev.platform_data;
684 irq_base = pdata->irq_base;
685 chip->gpio_start = pdata->gpio_base;
686 invert = pdata->invert;
687 chip->names = pdata->names;
689 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
690 #ifdef CONFIG_OF_GPIO
691 /* If I2C node has no interrupts property, disable GPIO interrupts */
692 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
697 chip->client = client;
699 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
701 mutex_init(&chip->i2c_lock);
703 /* initialize cached registers from their original values.
704 * we can't share this chip with another i2c master.
706 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
708 if (chip->chip_type == PCA953X_TYPE)
709 ret = device_pca953x_init(chip, invert);
711 ret = device_pca957x_init(chip, invert);
715 ret = pca953x_irq_setup(chip, id, irq_base);
719 ret = gpiochip_add(&chip->gpio_chip);
723 if (pdata && pdata->setup) {
724 ret = pdata->setup(client, chip->gpio_chip.base,
725 chip->gpio_chip.ngpio, pdata->context);
727 dev_warn(&client->dev, "setup failed, %d\n", ret);
730 i2c_set_clientdata(client, chip);
734 pca953x_irq_teardown(chip);
740 static int pca953x_remove(struct i2c_client *client)
742 struct pca953x_platform_data *pdata = client->dev.platform_data;
743 struct pca953x_chip *chip = i2c_get_clientdata(client);
746 if (pdata && pdata->teardown) {
747 ret = pdata->teardown(client, chip->gpio_chip.base,
748 chip->gpio_chip.ngpio, pdata->context);
750 dev_err(&client->dev, "%s failed, %d\n",
756 ret = gpiochip_remove(&chip->gpio_chip);
758 dev_err(&client->dev, "%s failed, %d\n",
759 "gpiochip_remove()", ret);
763 pca953x_irq_teardown(chip);
768 static const struct of_device_id pca953x_dt_ids[] = {
769 { .compatible = "nxp,pca9534", },
770 { .compatible = "nxp,pca9535", },
771 { .compatible = "nxp,pca9536", },
772 { .compatible = "nxp,pca9537", },
773 { .compatible = "nxp,pca9538", },
774 { .compatible = "nxp,pca9539", },
775 { .compatible = "nxp,pca9554", },
776 { .compatible = "nxp,pca9555", },
777 { .compatible = "nxp,pca9556", },
778 { .compatible = "nxp,pca9557", },
779 { .compatible = "nxp,pca9574", },
780 { .compatible = "nxp,pca9575", },
782 { .compatible = "maxim,max7310", },
783 { .compatible = "maxim,max7312", },
784 { .compatible = "maxim,max7313", },
785 { .compatible = "maxim,max7315", },
787 { .compatible = "ti,pca6107", },
788 { .compatible = "ti,tca6408", },
789 { .compatible = "ti,tca6416", },
790 { .compatible = "ti,tca6424", },
794 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
796 static struct i2c_driver pca953x_driver = {
799 .of_match_table = pca953x_dt_ids,
801 .probe = pca953x_probe,
802 .remove = pca953x_remove,
803 .id_table = pca953x_id,
806 static int __init pca953x_init(void)
808 return i2c_add_driver(&pca953x_driver);
810 /* register after i2c postcore initcall and before
811 * subsys initcalls that may rely on these GPIOs
813 subsys_initcall(pca953x_init);
815 static void __exit pca953x_exit(void)
817 i2c_del_driver(&pca953x_driver);
819 module_exit(pca953x_exit);
821 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
822 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
823 MODULE_LICENSE("GPL");