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 pca953x_platform_data *dyn_pdata;
89 struct gpio_chip gpio_chip;
90 const char *const *names;
94 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
98 if (chip->gpio_chip.ngpio <= 8)
99 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
101 switch (chip->chip_type) {
103 ret = i2c_smbus_write_word_data(chip->client,
107 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
111 ret = i2c_smbus_write_byte_data(chip->client,
113 (val & 0xff00) >> 8);
119 dev_err(&chip->client->dev, "failed writing register\n");
126 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
130 if (chip->gpio_chip.ngpio <= 8)
131 ret = i2c_smbus_read_byte_data(chip->client, reg);
133 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
136 dev_err(&chip->client->dev, "failed reading register\n");
140 *val = (uint16_t)ret;
144 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
146 struct pca953x_chip *chip;
150 chip = container_of(gc, struct pca953x_chip, gpio_chip);
152 mutex_lock(&chip->i2c_lock);
153 reg_val = chip->reg_direction | (1u << off);
155 switch (chip->chip_type) {
157 offset = PCA953X_DIRECTION;
160 offset = PCA957X_CFG;
163 ret = pca953x_write_reg(chip, offset, reg_val);
167 chip->reg_direction = reg_val;
170 mutex_unlock(&chip->i2c_lock);
174 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
175 unsigned off, int val)
177 struct pca953x_chip *chip;
181 chip = container_of(gc, struct pca953x_chip, gpio_chip);
183 mutex_lock(&chip->i2c_lock);
184 /* set output level */
186 reg_val = chip->reg_output | (1u << off);
188 reg_val = chip->reg_output & ~(1u << off);
190 switch (chip->chip_type) {
192 offset = PCA953X_OUTPUT;
195 offset = PCA957X_OUT;
198 ret = pca953x_write_reg(chip, offset, reg_val);
202 chip->reg_output = reg_val;
205 reg_val = chip->reg_direction & ~(1u << off);
206 switch (chip->chip_type) {
208 offset = PCA953X_DIRECTION;
211 offset = PCA957X_CFG;
214 ret = pca953x_write_reg(chip, offset, reg_val);
218 chip->reg_direction = reg_val;
221 mutex_unlock(&chip->i2c_lock);
225 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
227 struct pca953x_chip *chip;
231 chip = container_of(gc, struct pca953x_chip, gpio_chip);
233 mutex_lock(&chip->i2c_lock);
234 switch (chip->chip_type) {
236 offset = PCA953X_INPUT;
242 ret = pca953x_read_reg(chip, offset, ®_val);
243 mutex_unlock(&chip->i2c_lock);
245 /* NOTE: diagnostic already emitted; that's all we should
246 * do unless gpio_*_value_cansleep() calls become different
247 * from their nonsleeping siblings (and report faults).
252 return (reg_val & (1u << off)) ? 1 : 0;
255 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
257 struct pca953x_chip *chip;
261 chip = container_of(gc, struct pca953x_chip, gpio_chip);
263 mutex_lock(&chip->i2c_lock);
265 reg_val = chip->reg_output | (1u << off);
267 reg_val = chip->reg_output & ~(1u << off);
269 switch (chip->chip_type) {
271 offset = PCA953X_OUTPUT;
274 offset = PCA957X_OUT;
277 ret = pca953x_write_reg(chip, offset, reg_val);
281 chip->reg_output = reg_val;
283 mutex_unlock(&chip->i2c_lock);
286 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
288 struct gpio_chip *gc;
290 gc = &chip->gpio_chip;
292 gc->direction_input = pca953x_gpio_direction_input;
293 gc->direction_output = pca953x_gpio_direction_output;
294 gc->get = pca953x_gpio_get_value;
295 gc->set = pca953x_gpio_set_value;
298 gc->base = chip->gpio_start;
300 gc->label = chip->client->name;
301 gc->dev = &chip->client->dev;
302 gc->owner = THIS_MODULE;
303 gc->names = chip->names;
306 #ifdef CONFIG_GPIO_PCA953X_IRQ
307 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
309 struct pca953x_chip *chip;
311 chip = container_of(gc, struct pca953x_chip, gpio_chip);
312 return chip->irq_base + off;
315 static void pca953x_irq_mask(struct irq_data *d)
317 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
319 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
322 static void pca953x_irq_unmask(struct irq_data *d)
324 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
326 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
329 static void pca953x_irq_bus_lock(struct irq_data *d)
331 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
333 mutex_lock(&chip->irq_lock);
336 static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
338 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
342 /* Look for any newly setup interrupt */
343 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
344 new_irqs &= ~chip->reg_direction;
347 level = __ffs(new_irqs);
348 pca953x_gpio_direction_input(&chip->gpio_chip, level);
349 new_irqs &= ~(1 << level);
352 mutex_unlock(&chip->irq_lock);
355 static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
357 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
358 uint16_t level = d->irq - chip->irq_base;
359 uint16_t mask = 1 << level;
361 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
362 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
367 if (type & IRQ_TYPE_EDGE_FALLING)
368 chip->irq_trig_fall |= mask;
370 chip->irq_trig_fall &= ~mask;
372 if (type & IRQ_TYPE_EDGE_RISING)
373 chip->irq_trig_raise |= mask;
375 chip->irq_trig_raise &= ~mask;
380 static struct irq_chip pca953x_irq_chip = {
382 .irq_mask = pca953x_irq_mask,
383 .irq_unmask = pca953x_irq_unmask,
384 .irq_bus_lock = pca953x_irq_bus_lock,
385 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
386 .irq_set_type = pca953x_irq_set_type,
389 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
397 switch (chip->chip_type) {
399 offset = PCA953X_INPUT;
405 ret = pca953x_read_reg(chip, offset, &cur_stat);
409 /* Remove output pins from the equation */
410 cur_stat &= chip->reg_direction;
412 old_stat = chip->irq_stat;
413 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
418 chip->irq_stat = cur_stat;
420 pending = (old_stat & chip->irq_trig_fall) |
421 (cur_stat & chip->irq_trig_raise);
427 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
429 struct pca953x_chip *chip = devid;
433 pending = pca953x_irq_pending(chip);
439 level = __ffs(pending);
440 generic_handle_irq(level + chip->irq_base);
442 pending &= ~(1 << level);
448 static int pca953x_irq_setup(struct pca953x_chip *chip,
449 const struct i2c_device_id *id)
451 struct i2c_client *client = chip->client;
452 struct pca953x_platform_data *pdata = client->dev.platform_data;
455 if (pdata->irq_base != -1
456 && (id->driver_data & PCA_INT)) {
459 switch (chip->chip_type) {
461 offset = PCA953X_INPUT;
467 ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
472 * There is no way to know which GPIO line generated the
473 * interrupt. We have to rely on the previous read for
476 chip->irq_stat &= chip->reg_direction;
477 mutex_init(&chip->irq_lock);
479 chip->irq_base = irq_alloc_descs(-1, pdata->irq_base, chip->gpio_chip.ngpio, -1);
480 if (chip->irq_base < 0)
483 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
484 int irq = lvl + chip->irq_base;
486 irq_clear_status_flags(irq, IRQ_NOREQUEST);
487 irq_set_chip_data(irq, chip);
488 irq_set_chip_and_handler(irq, &pca953x_irq_chip,
491 set_irq_flags(irq, IRQF_VALID);
493 irq_set_noprobe(irq);
497 ret = request_threaded_irq(client->irq,
500 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
501 dev_name(&client->dev), chip);
503 dev_err(&client->dev, "failed to request irq %d\n",
508 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
518 static void pca953x_irq_teardown(struct pca953x_chip *chip)
520 if (chip->irq_base != -1)
521 free_irq(chip->client->irq, chip);
523 #else /* CONFIG_GPIO_PCA953X_IRQ */
524 static int pca953x_irq_setup(struct pca953x_chip *chip,
525 const struct i2c_device_id *id)
527 struct i2c_client *client = chip->client;
528 struct pca953x_platform_data *pdata = client->dev.platform_data;
530 if (pdata->irq_base != -1 && (id->driver_data & PCA_INT))
531 dev_warn(&client->dev, "interrupt support not compiled in\n");
536 static void pca953x_irq_teardown(struct pca953x_chip *chip)
542 * Handlers for alternative sources of platform_data
544 #ifdef CONFIG_OF_GPIO
546 * Translate OpenFirmware node properties into platform_data
548 static struct pca953x_platform_data *
549 pca953x_get_alt_pdata(struct i2c_client *client)
551 struct pca953x_platform_data *pdata;
552 struct device_node *node;
556 node = client->dev.of_node;
560 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
562 dev_err(&client->dev, "Unable to allocate platform_data\n");
566 pdata->gpio_base = -1;
567 val = of_get_property(node, "linux,gpio-base", &size);
569 if (size != sizeof(*val))
570 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
573 pdata->gpio_base = be32_to_cpup(val);
576 val = of_get_property(node, "polarity", NULL);
578 pdata->invert = *val;
583 static struct pca953x_platform_data *
584 pca953x_get_alt_pdata(struct i2c_client *client)
590 static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
594 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
598 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
599 &chip->reg_direction);
603 /* set platform specific polarity inversion */
604 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
612 static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
617 /* Let every port in proper state, that could save power */
618 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
619 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
620 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
622 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
625 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
628 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
632 /* set platform specific polarity inversion */
633 pca953x_write_reg(chip, PCA957X_INVRT, invert);
635 /* To enable register 6, 7 to controll pull up and pull down */
636 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
643 static int __devinit pca953x_probe(struct i2c_client *client,
644 const struct i2c_device_id *id)
646 struct pca953x_platform_data *pdata;
647 struct pca953x_chip *chip;
650 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
654 pdata = client->dev.platform_data;
656 pdata = pca953x_get_alt_pdata(client);
658 * Unlike normal platform_data, this is allocated
659 * dynamically and must be freed in the driver
661 chip->dyn_pdata = pdata;
665 dev_dbg(&client->dev, "no platform data\n");
670 chip->client = client;
672 chip->gpio_start = pdata->gpio_base;
674 chip->names = pdata->names;
675 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
677 mutex_init(&chip->i2c_lock);
679 /* initialize cached registers from their original values.
680 * we can't share this chip with another i2c master.
682 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
684 if (chip->chip_type == PCA953X_TYPE)
685 device_pca953x_init(chip, pdata->invert);
686 else if (chip->chip_type == PCA957X_TYPE)
687 device_pca957x_init(chip, pdata->invert);
691 ret = pca953x_irq_setup(chip, id);
695 ret = gpiochip_add(&chip->gpio_chip);
700 ret = pdata->setup(client, chip->gpio_chip.base,
701 chip->gpio_chip.ngpio, pdata->context);
703 dev_warn(&client->dev, "setup failed, %d\n", ret);
706 i2c_set_clientdata(client, chip);
710 pca953x_irq_teardown(chip);
712 kfree(chip->dyn_pdata);
717 static int pca953x_remove(struct i2c_client *client)
719 struct pca953x_platform_data *pdata = client->dev.platform_data;
720 struct pca953x_chip *chip = i2c_get_clientdata(client);
723 if (pdata->teardown) {
724 ret = pdata->teardown(client, chip->gpio_chip.base,
725 chip->gpio_chip.ngpio, pdata->context);
727 dev_err(&client->dev, "%s failed, %d\n",
733 ret = gpiochip_remove(&chip->gpio_chip);
735 dev_err(&client->dev, "%s failed, %d\n",
736 "gpiochip_remove()", ret);
740 pca953x_irq_teardown(chip);
741 kfree(chip->dyn_pdata);
746 static struct i2c_driver pca953x_driver = {
750 .probe = pca953x_probe,
751 .remove = pca953x_remove,
752 .id_table = pca953x_id,
755 static int __init pca953x_init(void)
757 return i2c_add_driver(&pca953x_driver);
759 /* register after i2c postcore initcall and before
760 * subsys initcalls that may rely on these GPIOs
762 subsys_initcall(pca953x_init);
764 static void __exit pca953x_exit(void)
766 i2c_del_driver(&pca953x_driver);
768 module_exit(pca953x_exit);
770 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
771 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
772 MODULE_LICENSE("GPL");