2 * PCA953x 4/8/16 bit I/O ports
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>
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 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
49 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
50 { "pca9536", 4 | PCA953X_TYPE, },
51 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
52 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
53 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
54 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
55 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
56 { "pca9556", 8 | PCA953X_TYPE, },
57 { "pca9557", 8 | PCA953X_TYPE, },
58 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
59 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
61 { "max7310", 8 | PCA953X_TYPE, },
62 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
63 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
64 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
65 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
66 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
67 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
68 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
71 MODULE_DEVICE_TABLE(i2c, pca953x_id);
77 struct mutex i2c_lock;
79 #ifdef CONFIG_GPIO_PCA953X_IRQ
80 struct mutex irq_lock;
88 struct i2c_client *client;
89 struct gpio_chip gpio_chip;
90 const char *const *names;
94 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, u32 val)
98 if (chip->gpio_chip.ngpio <= 8)
99 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
100 else if (chip->gpio_chip.ngpio == 24) {
102 ret = i2c_smbus_write_i2c_block_data(chip->client,
103 (reg << 2) | REG_ADDR_AI,
108 switch (chip->chip_type) {
110 ret = i2c_smbus_write_word_data(chip->client,
114 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
118 ret = i2c_smbus_write_byte_data(chip->client,
120 (val & 0xff00) >> 8);
126 dev_err(&chip->client->dev, "failed writing register\n");
133 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, u32 *val)
137 if (chip->gpio_chip.ngpio <= 8) {
138 ret = i2c_smbus_read_byte_data(chip->client, reg);
141 else if (chip->gpio_chip.ngpio == 24) {
143 ret = i2c_smbus_read_i2c_block_data(chip->client,
144 (reg << 2) | REG_ADDR_AI,
149 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
154 dev_err(&chip->client->dev, "failed reading register\n");
161 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
163 struct pca953x_chip *chip;
167 chip = container_of(gc, struct pca953x_chip, gpio_chip);
169 mutex_lock(&chip->i2c_lock);
170 reg_val = chip->reg_direction | (1u << off);
172 switch (chip->chip_type) {
174 offset = PCA953X_DIRECTION;
177 offset = PCA957X_CFG;
180 ret = pca953x_write_reg(chip, offset, reg_val);
184 chip->reg_direction = reg_val;
187 mutex_unlock(&chip->i2c_lock);
191 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
192 unsigned off, int val)
194 struct pca953x_chip *chip;
198 chip = container_of(gc, struct pca953x_chip, gpio_chip);
200 mutex_lock(&chip->i2c_lock);
201 /* set output level */
203 reg_val = chip->reg_output | (1u << off);
205 reg_val = chip->reg_output & ~(1u << off);
207 switch (chip->chip_type) {
209 offset = PCA953X_OUTPUT;
212 offset = PCA957X_OUT;
215 ret = pca953x_write_reg(chip, offset, reg_val);
219 chip->reg_output = reg_val;
222 reg_val = chip->reg_direction & ~(1u << off);
223 switch (chip->chip_type) {
225 offset = PCA953X_DIRECTION;
228 offset = PCA957X_CFG;
231 ret = pca953x_write_reg(chip, offset, reg_val);
235 chip->reg_direction = reg_val;
238 mutex_unlock(&chip->i2c_lock);
242 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
244 struct pca953x_chip *chip;
248 chip = container_of(gc, struct pca953x_chip, gpio_chip);
250 mutex_lock(&chip->i2c_lock);
251 switch (chip->chip_type) {
253 offset = PCA953X_INPUT;
259 ret = pca953x_read_reg(chip, offset, ®_val);
260 mutex_unlock(&chip->i2c_lock);
262 /* NOTE: diagnostic already emitted; that's all we should
263 * do unless gpio_*_value_cansleep() calls become different
264 * from their nonsleeping siblings (and report faults).
269 return (reg_val & (1u << off)) ? 1 : 0;
272 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
274 struct pca953x_chip *chip;
278 chip = container_of(gc, struct pca953x_chip, gpio_chip);
280 mutex_lock(&chip->i2c_lock);
282 reg_val = chip->reg_output | (1u << off);
284 reg_val = chip->reg_output & ~(1u << off);
286 switch (chip->chip_type) {
288 offset = PCA953X_OUTPUT;
291 offset = PCA957X_OUT;
294 ret = pca953x_write_reg(chip, offset, reg_val);
298 chip->reg_output = reg_val;
300 mutex_unlock(&chip->i2c_lock);
303 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
305 struct gpio_chip *gc;
307 gc = &chip->gpio_chip;
309 gc->direction_input = pca953x_gpio_direction_input;
310 gc->direction_output = pca953x_gpio_direction_output;
311 gc->get = pca953x_gpio_get_value;
312 gc->set = pca953x_gpio_set_value;
315 gc->base = chip->gpio_start;
317 gc->label = chip->client->name;
318 gc->dev = &chip->client->dev;
319 gc->owner = THIS_MODULE;
320 gc->names = chip->names;
323 #ifdef CONFIG_GPIO_PCA953X_IRQ
324 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
326 struct pca953x_chip *chip;
328 chip = container_of(gc, struct pca953x_chip, gpio_chip);
329 return chip->irq_base + off;
332 static void pca953x_irq_mask(struct irq_data *d)
334 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
336 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
339 static void pca953x_irq_unmask(struct irq_data *d)
341 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
343 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
346 static void pca953x_irq_bus_lock(struct irq_data *d)
348 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
350 mutex_lock(&chip->irq_lock);
353 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
355 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
359 /* Look for any newly setup interrupt */
360 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
361 new_irqs &= ~chip->reg_direction;
364 level = __ffs(new_irqs);
365 pca953x_gpio_direction_input(&chip->gpio_chip, level);
366 new_irqs &= ~(1 << level);
369 mutex_unlock(&chip->irq_lock);
372 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
374 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
375 u32 level = d->irq - chip->irq_base;
376 u32 mask = 1 << level;
378 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
379 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
384 if (type & IRQ_TYPE_EDGE_FALLING)
385 chip->irq_trig_fall |= mask;
387 chip->irq_trig_fall &= ~mask;
389 if (type & IRQ_TYPE_EDGE_RISING)
390 chip->irq_trig_raise |= mask;
392 chip->irq_trig_raise &= ~mask;
397 static struct irq_chip pca953x_irq_chip = {
399 .irq_mask = pca953x_irq_mask,
400 .irq_unmask = pca953x_irq_unmask,
401 .irq_bus_lock = pca953x_irq_bus_lock,
402 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
403 .irq_set_type = pca953x_irq_set_type,
406 static u32 pca953x_irq_pending(struct pca953x_chip *chip)
414 switch (chip->chip_type) {
416 offset = PCA953X_INPUT;
422 ret = pca953x_read_reg(chip, offset, &cur_stat);
426 /* Remove output pins from the equation */
427 cur_stat &= chip->reg_direction;
429 old_stat = chip->irq_stat;
430 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
435 chip->irq_stat = cur_stat;
437 pending = (old_stat & chip->irq_trig_fall) |
438 (cur_stat & chip->irq_trig_raise);
444 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
446 struct pca953x_chip *chip = devid;
450 pending = pca953x_irq_pending(chip);
456 level = __ffs(pending);
457 handle_nested_irq(level + chip->irq_base);
459 pending &= ~(1 << level);
465 static int pca953x_irq_setup(struct pca953x_chip *chip,
466 const struct i2c_device_id *id,
469 struct i2c_client *client = chip->client;
474 && (id->driver_data & PCA_INT)) {
477 switch (chip->chip_type) {
479 offset = PCA953X_INPUT;
485 ret = pca953x_read_reg(chip, offset, &temporary);
486 chip->irq_stat = temporary;
491 * There is no way to know which GPIO line generated the
492 * interrupt. We have to rely on the previous read for
495 chip->irq_stat &= chip->reg_direction;
496 mutex_init(&chip->irq_lock);
498 chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
499 if (chip->irq_base < 0)
502 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
503 int irq = lvl + chip->irq_base;
505 irq_clear_status_flags(irq, IRQ_NOREQUEST);
506 irq_set_chip_data(irq, chip);
507 irq_set_chip(irq, &pca953x_irq_chip);
508 irq_set_nested_thread(irq, true);
510 set_irq_flags(irq, IRQF_VALID);
512 irq_set_noprobe(irq);
516 ret = request_threaded_irq(client->irq,
519 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
520 dev_name(&client->dev), chip);
522 dev_err(&client->dev, "failed to request irq %d\n",
527 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
537 static void pca953x_irq_teardown(struct pca953x_chip *chip)
539 if (chip->irq_base != -1) {
540 irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
541 free_irq(chip->client->irq, chip);
544 #else /* CONFIG_GPIO_PCA953X_IRQ */
545 static int pca953x_irq_setup(struct pca953x_chip *chip,
546 const struct i2c_device_id *id,
549 struct i2c_client *client = chip->client;
551 if (irq_base != -1 && (id->driver_data & PCA_INT))
552 dev_warn(&client->dev, "interrupt support not compiled in\n");
557 static void pca953x_irq_teardown(struct pca953x_chip *chip)
563 * Handlers for alternative sources of platform_data
565 #ifdef CONFIG_OF_GPIO
567 * Translate OpenFirmware node properties into platform_data
568 * WARNING: This is DEPRECATED and will be removed eventually!
571 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
573 struct device_node *node;
577 node = client->dev.of_node;
582 val = of_get_property(node, "linux,gpio-base", &size);
583 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
585 if (size != sizeof(*val))
586 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
589 *gpio_base = be32_to_cpup(val);
592 val = of_get_property(node, "polarity", NULL);
593 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
599 pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
605 static int __devinit device_pca953x_init(struct pca953x_chip *chip, u32 invert)
609 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
613 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
614 &chip->reg_direction);
618 /* set platform specific polarity inversion */
619 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
624 static int __devinit device_pca957x_init(struct pca953x_chip *chip, u32 invert)
629 /* Let every port in proper state, that could save power */
630 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
631 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
632 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
634 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
637 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
640 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
644 /* set platform specific polarity inversion */
645 pca953x_write_reg(chip, PCA957X_INVRT, invert);
647 /* To enable register 6, 7 to controll pull up and pull down */
648 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
655 static int __devinit pca953x_probe(struct i2c_client *client,
656 const struct i2c_device_id *id)
658 struct pca953x_platform_data *pdata;
659 struct pca953x_chip *chip;
664 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
668 pdata = client->dev.platform_data;
670 irq_base = pdata->irq_base;
671 chip->gpio_start = pdata->gpio_base;
672 invert = pdata->invert;
673 chip->names = pdata->names;
675 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
676 #ifdef CONFIG_OF_GPIO
677 /* If I2C node has no interrupts property, disable GPIO interrupts */
678 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
683 chip->client = client;
685 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
687 mutex_init(&chip->i2c_lock);
689 /* initialize cached registers from their original values.
690 * we can't share this chip with another i2c master.
692 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
694 if (chip->chip_type == PCA953X_TYPE)
695 ret = device_pca953x_init(chip, invert);
697 ret = device_pca957x_init(chip, invert);
701 ret = pca953x_irq_setup(chip, id, irq_base);
705 ret = gpiochip_add(&chip->gpio_chip);
709 if (pdata && pdata->setup) {
710 ret = pdata->setup(client, chip->gpio_chip.base,
711 chip->gpio_chip.ngpio, pdata->context);
713 dev_warn(&client->dev, "setup failed, %d\n", ret);
716 i2c_set_clientdata(client, chip);
720 pca953x_irq_teardown(chip);
726 static int pca953x_remove(struct i2c_client *client)
728 struct pca953x_platform_data *pdata = client->dev.platform_data;
729 struct pca953x_chip *chip = i2c_get_clientdata(client);
732 if (pdata && pdata->teardown) {
733 ret = pdata->teardown(client, chip->gpio_chip.base,
734 chip->gpio_chip.ngpio, pdata->context);
736 dev_err(&client->dev, "%s failed, %d\n",
742 ret = gpiochip_remove(&chip->gpio_chip);
744 dev_err(&client->dev, "%s failed, %d\n",
745 "gpiochip_remove()", ret);
749 pca953x_irq_teardown(chip);
754 static struct i2c_driver pca953x_driver = {
758 .probe = pca953x_probe,
759 .remove = pca953x_remove,
760 .id_table = pca953x_id,
763 static int __init pca953x_init(void)
765 return i2c_add_driver(&pca953x_driver);
767 /* register after i2c postcore initcall and before
768 * subsys initcalls that may rely on these GPIOs
770 subsys_initcall(pca953x_init);
772 static void __exit pca953x_exit(void)
774 i2c_del_driver(&pca953x_driver);
776 module_exit(pca953x_exit);
779 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
780 MODULE_LICENSE("GPL");