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/i2c.h>
20 #include <linux/i2c/pca953x.h>
21 #include <linux/slab.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_gpio.h>
27 #define PCA953X_INPUT 0
28 #define PCA953X_OUTPUT 1
29 #define PCA953X_INVERT 2
30 #define PCA953X_DIRECTION 3
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
46 static const struct i2c_device_id pca953x_id[] = {
47 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
48 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
49 { "pca9536", 4 | PCA953X_TYPE, },
50 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
51 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
52 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
53 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
54 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
55 { "pca9556", 8 | PCA953X_TYPE, },
56 { "pca9557", 8 | PCA953X_TYPE, },
57 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
58 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
60 { "max7310", 8 | PCA953X_TYPE, },
61 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
62 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
63 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
64 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
65 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
66 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
67 /* NYET: { "tca6424", 24, }, */
70 MODULE_DEVICE_TABLE(i2c, pca953x_id);
75 uint16_t reg_direction;
76 struct mutex i2c_lock;
78 #ifdef CONFIG_GPIO_PCA953X_IRQ
79 struct mutex irq_lock;
82 uint16_t irq_trig_raise;
83 uint16_t irq_trig_fall;
87 struct i2c_client *client;
88 struct gpio_chip gpio_chip;
89 const char *const *names;
93 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
97 if (chip->gpio_chip.ngpio <= 8)
98 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
100 switch (chip->chip_type) {
102 ret = i2c_smbus_write_word_data(chip->client,
106 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
110 ret = i2c_smbus_write_byte_data(chip->client,
112 (val & 0xff00) >> 8);
118 dev_err(&chip->client->dev, "failed writing register\n");
125 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
129 if (chip->gpio_chip.ngpio <= 8)
130 ret = i2c_smbus_read_byte_data(chip->client, reg);
132 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
135 dev_err(&chip->client->dev, "failed reading register\n");
139 *val = (uint16_t)ret;
143 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
145 struct pca953x_chip *chip;
149 chip = container_of(gc, struct pca953x_chip, gpio_chip);
151 mutex_lock(&chip->i2c_lock);
152 reg_val = chip->reg_direction | (1u << off);
154 switch (chip->chip_type) {
156 offset = PCA953X_DIRECTION;
159 offset = PCA957X_CFG;
162 ret = pca953x_write_reg(chip, offset, reg_val);
166 chip->reg_direction = reg_val;
169 mutex_unlock(&chip->i2c_lock);
173 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
174 unsigned off, int val)
176 struct pca953x_chip *chip;
180 chip = container_of(gc, struct pca953x_chip, gpio_chip);
182 mutex_lock(&chip->i2c_lock);
183 /* set output level */
185 reg_val = chip->reg_output | (1u << off);
187 reg_val = chip->reg_output & ~(1u << off);
189 switch (chip->chip_type) {
191 offset = PCA953X_OUTPUT;
194 offset = PCA957X_OUT;
197 ret = pca953x_write_reg(chip, offset, reg_val);
201 chip->reg_output = reg_val;
204 reg_val = chip->reg_direction & ~(1u << off);
205 switch (chip->chip_type) {
207 offset = PCA953X_DIRECTION;
210 offset = PCA957X_CFG;
213 ret = pca953x_write_reg(chip, offset, reg_val);
217 chip->reg_direction = reg_val;
220 mutex_unlock(&chip->i2c_lock);
224 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
226 struct pca953x_chip *chip;
230 chip = container_of(gc, struct pca953x_chip, gpio_chip);
232 mutex_lock(&chip->i2c_lock);
233 switch (chip->chip_type) {
235 offset = PCA953X_INPUT;
241 ret = pca953x_read_reg(chip, offset, ®_val);
242 mutex_unlock(&chip->i2c_lock);
244 /* NOTE: diagnostic already emitted; that's all we should
245 * do unless gpio_*_value_cansleep() calls become different
246 * from their nonsleeping siblings (and report faults).
251 return (reg_val & (1u << off)) ? 1 : 0;
254 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
256 struct pca953x_chip *chip;
260 chip = container_of(gc, struct pca953x_chip, gpio_chip);
262 mutex_lock(&chip->i2c_lock);
264 reg_val = chip->reg_output | (1u << off);
266 reg_val = chip->reg_output & ~(1u << off);
268 switch (chip->chip_type) {
270 offset = PCA953X_OUTPUT;
273 offset = PCA957X_OUT;
276 ret = pca953x_write_reg(chip, offset, reg_val);
280 chip->reg_output = reg_val;
282 mutex_unlock(&chip->i2c_lock);
285 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
287 struct gpio_chip *gc;
289 gc = &chip->gpio_chip;
291 gc->direction_input = pca953x_gpio_direction_input;
292 gc->direction_output = pca953x_gpio_direction_output;
293 gc->get = pca953x_gpio_get_value;
294 gc->set = pca953x_gpio_set_value;
297 gc->base = chip->gpio_start;
299 gc->label = chip->client->name;
300 gc->dev = &chip->client->dev;
301 gc->owner = THIS_MODULE;
302 gc->names = chip->names;
305 #ifdef CONFIG_GPIO_PCA953X_IRQ
306 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
308 struct pca953x_chip *chip;
310 chip = container_of(gc, struct pca953x_chip, gpio_chip);
311 return chip->irq_base + off;
314 static void pca953x_irq_mask(struct irq_data *d)
316 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
318 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
321 static void pca953x_irq_unmask(struct irq_data *d)
323 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
325 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
328 static void pca953x_irq_bus_lock(struct irq_data *d)
330 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
332 mutex_lock(&chip->irq_lock);
335 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
337 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
341 /* Look for any newly setup interrupt */
342 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
343 new_irqs &= ~chip->reg_direction;
346 level = __ffs(new_irqs);
347 pca953x_gpio_direction_input(&chip->gpio_chip, level);
348 new_irqs &= ~(1 << level);
351 mutex_unlock(&chip->irq_lock);
354 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
356 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
357 uint16_t level = d->irq - chip->irq_base;
358 uint16_t mask = 1 << level;
360 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
361 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
366 if (type & IRQ_TYPE_EDGE_FALLING)
367 chip->irq_trig_fall |= mask;
369 chip->irq_trig_fall &= ~mask;
371 if (type & IRQ_TYPE_EDGE_RISING)
372 chip->irq_trig_raise |= mask;
374 chip->irq_trig_raise &= ~mask;
379 static struct irq_chip pca953x_irq_chip = {
381 .irq_mask = pca953x_irq_mask,
382 .irq_unmask = pca953x_irq_unmask,
383 .irq_bus_lock = pca953x_irq_bus_lock,
384 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
385 .irq_set_type = pca953x_irq_set_type,
388 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
396 switch (chip->chip_type) {
398 offset = PCA953X_INPUT;
404 ret = pca953x_read_reg(chip, offset, &cur_stat);
408 /* Remove output pins from the equation */
409 cur_stat &= chip->reg_direction;
411 old_stat = chip->irq_stat;
412 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
417 chip->irq_stat = cur_stat;
419 pending = (old_stat & chip->irq_trig_fall) |
420 (cur_stat & chip->irq_trig_raise);
426 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
428 struct pca953x_chip *chip = devid;
432 pending = pca953x_irq_pending(chip);
438 level = __ffs(pending);
439 handle_nested_irq(level + chip->irq_base);
441 pending &= ~(1 << level);
447 static int pca953x_irq_setup(struct pca953x_chip *chip,
448 const struct i2c_device_id *id,
451 struct i2c_client *client = chip->client;
455 && (id->driver_data & PCA_INT)) {
458 switch (chip->chip_type) {
460 offset = PCA953X_INPUT;
466 ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
471 * There is no way to know which GPIO line generated the
472 * interrupt. We have to rely on the previous read for
475 chip->irq_stat &= chip->reg_direction;
476 mutex_init(&chip->irq_lock);
478 chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
479 if (chip->irq_base < 0)
482 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
483 int irq = lvl + chip->irq_base;
485 irq_clear_status_flags(irq, IRQ_NOREQUEST);
486 irq_set_chip_data(irq, chip);
487 irq_set_chip(irq, &pca953x_irq_chip);
488 irq_set_nested_thread(irq, true);
490 set_irq_flags(irq, IRQF_VALID);
492 irq_set_noprobe(irq);
496 ret = request_threaded_irq(client->irq,
499 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
500 dev_name(&client->dev), chip);
502 dev_err(&client->dev, "failed to request irq %d\n",
507 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
517 static void pca953x_irq_teardown(struct pca953x_chip *chip)
519 if (chip->irq_base != -1) {
520 irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
521 free_irq(chip->client->irq, chip);
524 #else /* CONFIG_GPIO_PCA953X_IRQ */
525 static int pca953x_irq_setup(struct pca953x_chip *chip,
526 const struct i2c_device_id *id,
529 struct i2c_client *client = chip->client;
531 if (irq_base != -1 && (id->driver_data & PCA_INT))
532 dev_warn(&client->dev, "interrupt support not compiled in\n");
537 static void pca953x_irq_teardown(struct pca953x_chip *chip)
543 * Handlers for alternative sources of platform_data
545 #ifdef CONFIG_OF_GPIO
547 * Translate OpenFirmware node properties into platform_data
550 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
552 struct device_node *node;
556 node = client->dev.of_node;
561 val = of_get_property(node, "linux,gpio-base", &size);
563 if (size != sizeof(*val))
564 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
567 *gpio_base = be32_to_cpup(val);
570 val = of_get_property(node, "polarity", NULL);
576 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
581 static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
585 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
589 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
590 &chip->reg_direction);
594 /* set platform specific polarity inversion */
595 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
603 static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
608 /* Let every port in proper state, that could save power */
609 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
610 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
611 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
613 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
616 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
619 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
623 /* set platform specific polarity inversion */
624 pca953x_write_reg(chip, PCA957X_INVRT, invert);
626 /* To enable register 6, 7 to controll pull up and pull down */
627 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
634 static int __devinit pca953x_probe(struct i2c_client *client,
635 const struct i2c_device_id *id)
637 struct pca953x_platform_data *pdata;
638 struct pca953x_chip *chip;
639 int irq_base=-1, invert=0;
642 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
646 pdata = client->dev.platform_data;
648 irq_base = pdata->irq_base;
649 chip->gpio_start = pdata->gpio_base;
650 invert = pdata->invert;
651 chip->names = pdata->names;
653 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
656 chip->client = client;
658 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
660 mutex_init(&chip->i2c_lock);
662 /* initialize cached registers from their original values.
663 * we can't share this chip with another i2c master.
665 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
667 if (chip->chip_type == PCA953X_TYPE)
668 device_pca953x_init(chip, invert);
669 else if (chip->chip_type == PCA957X_TYPE)
670 device_pca957x_init(chip, invert);
674 ret = pca953x_irq_setup(chip, id, irq_base);
678 ret = gpiochip_add(&chip->gpio_chip);
682 if (pdata && pdata->setup) {
683 ret = pdata->setup(client, chip->gpio_chip.base,
684 chip->gpio_chip.ngpio, pdata->context);
686 dev_warn(&client->dev, "setup failed, %d\n", ret);
689 i2c_set_clientdata(client, chip);
693 pca953x_irq_teardown(chip);
699 static int pca953x_remove(struct i2c_client *client)
701 struct pca953x_platform_data *pdata = client->dev.platform_data;
702 struct pca953x_chip *chip = i2c_get_clientdata(client);
705 if (pdata && pdata->teardown) {
706 ret = pdata->teardown(client, chip->gpio_chip.base,
707 chip->gpio_chip.ngpio, pdata->context);
709 dev_err(&client->dev, "%s failed, %d\n",
715 ret = gpiochip_remove(&chip->gpio_chip);
717 dev_err(&client->dev, "%s failed, %d\n",
718 "gpiochip_remove()", ret);
722 pca953x_irq_teardown(chip);
727 static struct i2c_driver pca953x_driver = {
731 .probe = pca953x_probe,
732 .remove = pca953x_remove,
733 .id_table = pca953x_id,
736 static int __init pca953x_init(void)
738 return i2c_add_driver(&pca953x_driver);
740 /* register after i2c postcore initcall and before
741 * subsys initcalls that may rely on these GPIOs
743 subsys_initcall(pca953x_init);
745 static void __exit pca953x_exit(void)
747 i2c_del_driver(&pca953x_driver);
749 module_exit(pca953x_exit);
751 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
752 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
753 MODULE_LICENSE("GPL");