2 * pca953x.c - 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>
22 #include <linux/of_platform.h>
23 #include <linux/of_gpio.h>
26 #define PCA953X_INPUT 0
27 #define PCA953X_OUTPUT 1
28 #define PCA953X_INVERT 2
29 #define PCA953X_DIRECTION 3
31 #define PCA953X_GPIOS 0x00FF
32 #define PCA953X_INT 0x0100
34 static const struct i2c_device_id pca953x_id[] = {
35 { "pca9534", 8 | PCA953X_INT, },
36 { "pca9535", 16 | PCA953X_INT, },
38 { "pca9537", 4 | PCA953X_INT, },
39 { "pca9538", 8 | PCA953X_INT, },
40 { "pca9539", 16 | PCA953X_INT, },
41 { "pca9554", 8 | PCA953X_INT, },
42 { "pca9555", 16 | PCA953X_INT, },
47 { "max7312", 16 | PCA953X_INT, },
48 { "max7313", 16 | PCA953X_INT, },
49 { "max7315", 8 | PCA953X_INT, },
50 { "pca6107", 8 | PCA953X_INT, },
51 { "tca6408", 8 | PCA953X_INT, },
52 { "tca6416", 16 | PCA953X_INT, },
53 /* NYET: { "tca6424", 24, }, */
56 MODULE_DEVICE_TABLE(i2c, pca953x_id);
61 uint16_t reg_direction;
63 #ifdef CONFIG_GPIO_PCA953X_IRQ
64 struct mutex irq_lock;
67 uint16_t irq_trig_raise;
68 uint16_t irq_trig_fall;
72 struct i2c_client *client;
73 struct pca953x_platform_data *dyn_pdata;
74 struct gpio_chip gpio_chip;
78 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
82 if (chip->gpio_chip.ngpio <= 8)
83 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
85 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
88 dev_err(&chip->client->dev, "failed writing register\n");
95 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
99 if (chip->gpio_chip.ngpio <= 8)
100 ret = i2c_smbus_read_byte_data(chip->client, reg);
102 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
105 dev_err(&chip->client->dev, "failed reading register\n");
109 *val = (uint16_t)ret;
113 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
115 struct pca953x_chip *chip;
119 chip = container_of(gc, struct pca953x_chip, gpio_chip);
121 reg_val = chip->reg_direction | (1u << off);
122 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
126 chip->reg_direction = reg_val;
130 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
131 unsigned off, int val)
133 struct pca953x_chip *chip;
137 chip = container_of(gc, struct pca953x_chip, gpio_chip);
139 /* set output level */
141 reg_val = chip->reg_output | (1u << off);
143 reg_val = chip->reg_output & ~(1u << off);
145 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
149 chip->reg_output = reg_val;
152 reg_val = chip->reg_direction & ~(1u << off);
153 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
157 chip->reg_direction = reg_val;
161 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
163 struct pca953x_chip *chip;
167 chip = container_of(gc, struct pca953x_chip, gpio_chip);
169 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
171 /* NOTE: diagnostic already emitted; that's all we should
172 * do unless gpio_*_value_cansleep() calls become different
173 * from their nonsleeping siblings (and report faults).
178 return (reg_val & (1u << off)) ? 1 : 0;
181 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
183 struct pca953x_chip *chip;
187 chip = container_of(gc, struct pca953x_chip, gpio_chip);
190 reg_val = chip->reg_output | (1u << off);
192 reg_val = chip->reg_output & ~(1u << off);
194 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
198 chip->reg_output = reg_val;
201 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
203 struct gpio_chip *gc;
205 gc = &chip->gpio_chip;
207 gc->direction_input = pca953x_gpio_direction_input;
208 gc->direction_output = pca953x_gpio_direction_output;
209 gc->get = pca953x_gpio_get_value;
210 gc->set = pca953x_gpio_set_value;
213 gc->base = chip->gpio_start;
215 gc->label = chip->client->name;
216 gc->dev = &chip->client->dev;
217 gc->owner = THIS_MODULE;
218 gc->names = chip->names;
221 #ifdef CONFIG_GPIO_PCA953X_IRQ
222 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
224 struct pca953x_chip *chip;
226 chip = container_of(gc, struct pca953x_chip, gpio_chip);
227 return chip->irq_base + off;
230 static void pca953x_irq_mask(unsigned int irq)
232 struct pca953x_chip *chip = get_irq_chip_data(irq);
234 chip->irq_mask &= ~(1 << (irq - chip->irq_base));
237 static void pca953x_irq_unmask(unsigned int irq)
239 struct pca953x_chip *chip = get_irq_chip_data(irq);
241 chip->irq_mask |= 1 << (irq - chip->irq_base);
244 static void pca953x_irq_bus_lock(unsigned int irq)
246 struct pca953x_chip *chip = get_irq_chip_data(irq);
248 mutex_lock(&chip->irq_lock);
251 static void pca953x_irq_bus_sync_unlock(unsigned int irq)
253 struct pca953x_chip *chip = get_irq_chip_data(irq);
255 mutex_unlock(&chip->irq_lock);
258 static int pca953x_irq_set_type(unsigned int irq, unsigned int type)
260 struct pca953x_chip *chip = get_irq_chip_data(irq);
261 uint16_t level = irq - chip->irq_base;
262 uint16_t mask = 1 << level;
264 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
265 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
270 if (type & IRQ_TYPE_EDGE_FALLING)
271 chip->irq_trig_fall |= mask;
273 chip->irq_trig_fall &= ~mask;
275 if (type & IRQ_TYPE_EDGE_RISING)
276 chip->irq_trig_raise |= mask;
278 chip->irq_trig_raise &= ~mask;
280 return pca953x_gpio_direction_input(&chip->gpio_chip, level);
283 static struct irq_chip pca953x_irq_chip = {
285 .mask = pca953x_irq_mask,
286 .unmask = pca953x_irq_unmask,
287 .bus_lock = pca953x_irq_bus_lock,
288 .bus_sync_unlock = pca953x_irq_bus_sync_unlock,
289 .set_type = pca953x_irq_set_type,
292 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
300 ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
304 /* Remove output pins from the equation */
305 cur_stat &= chip->reg_direction;
307 old_stat = chip->irq_stat;
308 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
313 chip->irq_stat = cur_stat;
315 pending = (old_stat & chip->irq_trig_fall) |
316 (cur_stat & chip->irq_trig_raise);
322 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
324 struct pca953x_chip *chip = devid;
328 pending = pca953x_irq_pending(chip);
334 level = __ffs(pending);
335 handle_nested_irq(level + chip->irq_base);
337 pending &= ~(1 << level);
343 static int pca953x_irq_setup(struct pca953x_chip *chip,
344 const struct i2c_device_id *id)
346 struct i2c_client *client = chip->client;
347 struct pca953x_platform_data *pdata = client->dev.platform_data;
350 if (pdata->irq_base && (id->driver_data & PCA953X_INT)) {
353 ret = pca953x_read_reg(chip, PCA953X_INPUT,
359 * There is no way to know which GPIO line generated the
360 * interrupt. We have to rely on the previous read for
363 chip->irq_stat &= chip->reg_direction;
364 chip->irq_base = pdata->irq_base;
365 mutex_init(&chip->irq_lock);
367 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
368 int irq = lvl + chip->irq_base;
370 set_irq_chip_data(irq, chip);
371 set_irq_chip_and_handler(irq, &pca953x_irq_chip,
373 set_irq_nested_thread(irq, 1);
375 set_irq_flags(irq, IRQF_VALID);
377 set_irq_noprobe(irq);
381 ret = request_threaded_irq(client->irq,
384 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
385 dev_name(&client->dev), chip);
387 dev_err(&client->dev, "failed to request irq %d\n",
392 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
402 static void pca953x_irq_teardown(struct pca953x_chip *chip)
405 free_irq(chip->client->irq, chip);
407 #else /* CONFIG_GPIO_PCA953X_IRQ */
408 static int pca953x_irq_setup(struct pca953x_chip *chip,
409 const struct i2c_device_id *id)
411 struct i2c_client *client = chip->client;
412 struct pca953x_platform_data *pdata = client->dev.platform_data;
414 if (pdata->irq_base && (id->driver_data & PCA953X_INT))
415 dev_warn(&client->dev, "interrupt support not compiled in\n");
420 static void pca953x_irq_teardown(struct pca953x_chip *chip)
426 * Handlers for alternative sources of platform_data
428 #ifdef CONFIG_OF_GPIO
430 * Translate OpenFirmware node properties into platform_data
432 static struct pca953x_platform_data *
433 pca953x_get_alt_pdata(struct i2c_client *client)
435 struct pca953x_platform_data *pdata;
436 struct device_node *node;
439 node = dev_archdata_get_node(&client->dev.archdata);
443 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
445 dev_err(&client->dev, "Unable to allocate platform_data\n");
449 pdata->gpio_base = -1;
450 val = of_get_property(node, "linux,gpio-base", NULL);
453 dev_warn(&client->dev,
454 "invalid gpio-base in device tree\n");
456 pdata->gpio_base = *val;
459 val = of_get_property(node, "polarity", NULL);
461 pdata->invert = *val;
466 static struct pca953x_platform_data *
467 pca953x_get_alt_pdata(struct i2c_client *client)
473 static int __devinit pca953x_probe(struct i2c_client *client,
474 const struct i2c_device_id *id)
476 struct pca953x_platform_data *pdata;
477 struct pca953x_chip *chip;
480 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
484 pdata = client->dev.platform_data;
486 pdata = pca953x_get_alt_pdata(client);
488 * Unlike normal platform_data, this is allocated
489 * dynamically and must be freed in the driver
491 chip->dyn_pdata = pdata;
495 dev_dbg(&client->dev, "no platform data\n");
500 chip->client = client;
502 chip->gpio_start = pdata->gpio_base;
504 chip->names = pdata->names;
506 /* initialize cached registers from their original values.
507 * we can't share this chip with another i2c master.
509 pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
511 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
515 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
519 /* set platform specific polarity inversion */
520 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
524 ret = pca953x_irq_setup(chip, id);
528 ret = gpiochip_add(&chip->gpio_chip);
533 ret = pdata->setup(client, chip->gpio_chip.base,
534 chip->gpio_chip.ngpio, pdata->context);
536 dev_warn(&client->dev, "setup failed, %d\n", ret);
539 i2c_set_clientdata(client, chip);
543 pca953x_irq_teardown(chip);
544 kfree(chip->dyn_pdata);
549 static int pca953x_remove(struct i2c_client *client)
551 struct pca953x_platform_data *pdata = client->dev.platform_data;
552 struct pca953x_chip *chip = i2c_get_clientdata(client);
555 if (pdata->teardown) {
556 ret = pdata->teardown(client, chip->gpio_chip.base,
557 chip->gpio_chip.ngpio, pdata->context);
559 dev_err(&client->dev, "%s failed, %d\n",
565 ret = gpiochip_remove(&chip->gpio_chip);
567 dev_err(&client->dev, "%s failed, %d\n",
568 "gpiochip_remove()", ret);
572 pca953x_irq_teardown(chip);
573 kfree(chip->dyn_pdata);
578 static struct i2c_driver pca953x_driver = {
582 .probe = pca953x_probe,
583 .remove = pca953x_remove,
584 .id_table = pca953x_id,
587 static int __init pca953x_init(void)
589 return i2c_add_driver(&pca953x_driver);
591 /* register after i2c postcore initcall and before
592 * subsys initcalls that may rely on these GPIOs
594 subsys_initcall(pca953x_init);
596 static void __exit pca953x_exit(void)
598 i2c_del_driver(&pca953x_driver);
600 module_exit(pca953x_exit);
602 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
603 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
604 MODULE_LICENSE("GPL");