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>
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
32 #define PCA953X_GPIOS 0x00FF
33 #define PCA953X_INT 0x0100
35 static const struct i2c_device_id pca953x_id[] = {
36 { "pca9534", 8 | PCA953X_INT, },
37 { "pca9535", 16 | PCA953X_INT, },
39 { "pca9537", 4 | PCA953X_INT, },
40 { "pca9538", 8 | PCA953X_INT, },
41 { "pca9539", 16 | PCA953X_INT, },
42 { "pca9554", 8 | PCA953X_INT, },
43 { "pca9555", 16 | PCA953X_INT, },
48 { "max7312", 16 | PCA953X_INT, },
49 { "max7313", 16 | PCA953X_INT, },
50 { "max7315", 8 | PCA953X_INT, },
51 { "pca6107", 8 | PCA953X_INT, },
52 { "tca6408", 8 | PCA953X_INT, },
53 { "tca6416", 16 | PCA953X_INT, },
54 /* NYET: { "tca6424", 24, }, */
57 MODULE_DEVICE_TABLE(i2c, pca953x_id);
62 uint16_t reg_direction;
64 #ifdef CONFIG_GPIO_PCA953X_IRQ
65 struct mutex irq_lock;
68 uint16_t irq_trig_raise;
69 uint16_t irq_trig_fall;
73 struct i2c_client *client;
74 struct pca953x_platform_data *dyn_pdata;
75 struct gpio_chip gpio_chip;
79 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
83 if (chip->gpio_chip.ngpio <= 8)
84 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
86 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
89 dev_err(&chip->client->dev, "failed writing register\n");
96 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
100 if (chip->gpio_chip.ngpio <= 8)
101 ret = i2c_smbus_read_byte_data(chip->client, reg);
103 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
106 dev_err(&chip->client->dev, "failed reading register\n");
110 *val = (uint16_t)ret;
114 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
116 struct pca953x_chip *chip;
120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
122 reg_val = chip->reg_direction | (1u << off);
123 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
127 chip->reg_direction = reg_val;
131 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
132 unsigned off, int val)
134 struct pca953x_chip *chip;
138 chip = container_of(gc, struct pca953x_chip, gpio_chip);
140 /* set output level */
142 reg_val = chip->reg_output | (1u << off);
144 reg_val = chip->reg_output & ~(1u << off);
146 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
150 chip->reg_output = reg_val;
153 reg_val = chip->reg_direction & ~(1u << off);
154 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
158 chip->reg_direction = reg_val;
162 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
164 struct pca953x_chip *chip;
168 chip = container_of(gc, struct pca953x_chip, gpio_chip);
170 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
172 /* NOTE: diagnostic already emitted; that's all we should
173 * do unless gpio_*_value_cansleep() calls become different
174 * from their nonsleeping siblings (and report faults).
179 return (reg_val & (1u << off)) ? 1 : 0;
182 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
184 struct pca953x_chip *chip;
188 chip = container_of(gc, struct pca953x_chip, gpio_chip);
191 reg_val = chip->reg_output | (1u << off);
193 reg_val = chip->reg_output & ~(1u << off);
195 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
199 chip->reg_output = reg_val;
202 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
204 struct gpio_chip *gc;
206 gc = &chip->gpio_chip;
208 gc->direction_input = pca953x_gpio_direction_input;
209 gc->direction_output = pca953x_gpio_direction_output;
210 gc->get = pca953x_gpio_get_value;
211 gc->set = pca953x_gpio_set_value;
214 gc->base = chip->gpio_start;
216 gc->label = chip->client->name;
217 gc->dev = &chip->client->dev;
218 gc->owner = THIS_MODULE;
219 gc->names = chip->names;
222 #ifdef CONFIG_GPIO_PCA953X_IRQ
223 static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
225 struct pca953x_chip *chip;
227 chip = container_of(gc, struct pca953x_chip, gpio_chip);
228 return chip->irq_base + off;
231 static void pca953x_irq_mask(unsigned int irq)
233 struct pca953x_chip *chip = get_irq_chip_data(irq);
235 chip->irq_mask &= ~(1 << (irq - chip->irq_base));
238 static void pca953x_irq_unmask(unsigned int irq)
240 struct pca953x_chip *chip = get_irq_chip_data(irq);
242 chip->irq_mask |= 1 << (irq - chip->irq_base);
245 static void pca953x_irq_bus_lock(unsigned int irq)
247 struct pca953x_chip *chip = get_irq_chip_data(irq);
249 mutex_lock(&chip->irq_lock);
252 static void pca953x_irq_bus_sync_unlock(unsigned int irq)
254 struct pca953x_chip *chip = get_irq_chip_data(irq);
256 mutex_unlock(&chip->irq_lock);
259 static int pca953x_irq_set_type(unsigned int irq, unsigned int type)
261 struct pca953x_chip *chip = get_irq_chip_data(irq);
262 uint16_t level = irq - chip->irq_base;
263 uint16_t mask = 1 << level;
265 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
266 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
271 if (type & IRQ_TYPE_EDGE_FALLING)
272 chip->irq_trig_fall |= mask;
274 chip->irq_trig_fall &= ~mask;
276 if (type & IRQ_TYPE_EDGE_RISING)
277 chip->irq_trig_raise |= mask;
279 chip->irq_trig_raise &= ~mask;
281 return pca953x_gpio_direction_input(&chip->gpio_chip, level);
284 static struct irq_chip pca953x_irq_chip = {
286 .mask = pca953x_irq_mask,
287 .unmask = pca953x_irq_unmask,
288 .bus_lock = pca953x_irq_bus_lock,
289 .bus_sync_unlock = pca953x_irq_bus_sync_unlock,
290 .set_type = pca953x_irq_set_type,
293 static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
301 ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
305 /* Remove output pins from the equation */
306 cur_stat &= chip->reg_direction;
308 old_stat = chip->irq_stat;
309 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
314 chip->irq_stat = cur_stat;
316 pending = (old_stat & chip->irq_trig_fall) |
317 (cur_stat & chip->irq_trig_raise);
323 static irqreturn_t pca953x_irq_handler(int irq, void *devid)
325 struct pca953x_chip *chip = devid;
329 pending = pca953x_irq_pending(chip);
335 level = __ffs(pending);
336 handle_nested_irq(level + chip->irq_base);
338 pending &= ~(1 << level);
344 static int pca953x_irq_setup(struct pca953x_chip *chip,
345 const struct i2c_device_id *id)
347 struct i2c_client *client = chip->client;
348 struct pca953x_platform_data *pdata = client->dev.platform_data;
351 if (pdata->irq_base && (id->driver_data & PCA953X_INT)) {
354 ret = pca953x_read_reg(chip, PCA953X_INPUT,
360 * There is no way to know which GPIO line generated the
361 * interrupt. We have to rely on the previous read for
364 chip->irq_stat &= chip->reg_direction;
365 chip->irq_base = pdata->irq_base;
366 mutex_init(&chip->irq_lock);
368 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
369 int irq = lvl + chip->irq_base;
371 set_irq_chip_data(irq, chip);
372 set_irq_chip_and_handler(irq, &pca953x_irq_chip,
374 set_irq_nested_thread(irq, 1);
376 set_irq_flags(irq, IRQF_VALID);
378 set_irq_noprobe(irq);
382 ret = request_threaded_irq(client->irq,
385 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
386 dev_name(&client->dev), chip);
388 dev_err(&client->dev, "failed to request irq %d\n",
393 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
403 static void pca953x_irq_teardown(struct pca953x_chip *chip)
406 free_irq(chip->client->irq, chip);
408 #else /* CONFIG_GPIO_PCA953X_IRQ */
409 static int pca953x_irq_setup(struct pca953x_chip *chip,
410 const struct i2c_device_id *id)
412 struct i2c_client *client = chip->client;
413 struct pca953x_platform_data *pdata = client->dev.platform_data;
415 if (pdata->irq_base && (id->driver_data & PCA953X_INT))
416 dev_warn(&client->dev, "interrupt support not compiled in\n");
421 static void pca953x_irq_teardown(struct pca953x_chip *chip)
427 * Handlers for alternative sources of platform_data
429 #ifdef CONFIG_OF_GPIO
431 * Translate OpenFirmware node properties into platform_data
433 static struct pca953x_platform_data *
434 pca953x_get_alt_pdata(struct i2c_client *client)
436 struct pca953x_platform_data *pdata;
437 struct device_node *node;
440 node = dev_archdata_get_node(&client->dev.archdata);
444 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
446 dev_err(&client->dev, "Unable to allocate platform_data\n");
450 pdata->gpio_base = -1;
451 val = of_get_property(node, "linux,gpio-base", NULL);
454 dev_warn(&client->dev,
455 "invalid gpio-base in device tree\n");
457 pdata->gpio_base = *val;
460 val = of_get_property(node, "polarity", NULL);
462 pdata->invert = *val;
467 static struct pca953x_platform_data *
468 pca953x_get_alt_pdata(struct i2c_client *client)
474 static int __devinit pca953x_probe(struct i2c_client *client,
475 const struct i2c_device_id *id)
477 struct pca953x_platform_data *pdata;
478 struct pca953x_chip *chip;
481 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
485 pdata = client->dev.platform_data;
487 pdata = pca953x_get_alt_pdata(client);
489 * Unlike normal platform_data, this is allocated
490 * dynamically and must be freed in the driver
492 chip->dyn_pdata = pdata;
496 dev_dbg(&client->dev, "no platform data\n");
501 chip->client = client;
503 chip->gpio_start = pdata->gpio_base;
505 chip->names = pdata->names;
507 /* initialize cached registers from their original values.
508 * we can't share this chip with another i2c master.
510 pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
512 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
516 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
520 /* set platform specific polarity inversion */
521 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
525 ret = pca953x_irq_setup(chip, id);
529 ret = gpiochip_add(&chip->gpio_chip);
534 ret = pdata->setup(client, chip->gpio_chip.base,
535 chip->gpio_chip.ngpio, pdata->context);
537 dev_warn(&client->dev, "setup failed, %d\n", ret);
540 i2c_set_clientdata(client, chip);
544 pca953x_irq_teardown(chip);
545 kfree(chip->dyn_pdata);
550 static int pca953x_remove(struct i2c_client *client)
552 struct pca953x_platform_data *pdata = client->dev.platform_data;
553 struct pca953x_chip *chip = i2c_get_clientdata(client);
556 if (pdata->teardown) {
557 ret = pdata->teardown(client, chip->gpio_chip.base,
558 chip->gpio_chip.ngpio, pdata->context);
560 dev_err(&client->dev, "%s failed, %d\n",
566 ret = gpiochip_remove(&chip->gpio_chip);
568 dev_err(&client->dev, "%s failed, %d\n",
569 "gpiochip_remove()", ret);
573 pca953x_irq_teardown(chip);
574 kfree(chip->dyn_pdata);
579 static struct i2c_driver pca953x_driver = {
583 .probe = pca953x_probe,
584 .remove = pca953x_remove,
585 .id_table = pca953x_id,
588 static int __init pca953x_init(void)
590 return i2c_add_driver(&pca953x_driver);
592 /* register after i2c postcore initcall and before
593 * subsys initcalls that may rely on these GPIOs
595 subsys_initcall(pca953x_init);
597 static void __exit pca953x_exit(void)
599 i2c_del_driver(&pca953x_driver);
601 module_exit(pca953x_exit);
603 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
604 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
605 MODULE_LICENSE("GPL");