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/i2c.h>
17 #include <linux/i2c/pca953x.h>
21 #define PCA953X_INPUT 0
22 #define PCA953X_OUTPUT 1
23 #define PCA953X_INVERT 2
24 #define PCA953X_DIRECTION 3
26 static const struct i2c_device_id pca953x_id[] = {
39 MODULE_DEVICE_TABLE(i2c, pca953x_id);
44 uint16_t reg_direction;
46 struct i2c_client *client;
47 struct gpio_chip gpio_chip;
50 /* NOTE: we can't currently rely on fault codes to come from SMBus
51 * calls, so we map all errors to EIO here and return zero otherwise.
53 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
57 if (chip->gpio_chip.ngpio <= 8)
58 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
60 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
63 dev_err(&chip->client->dev, "failed writing register\n");
70 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
74 if (chip->gpio_chip.ngpio <= 8)
75 ret = i2c_smbus_read_byte_data(chip->client, reg);
77 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
80 dev_err(&chip->client->dev, "failed reading register\n");
88 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
90 struct pca953x_chip *chip;
94 chip = container_of(gc, struct pca953x_chip, gpio_chip);
96 reg_val = chip->reg_direction | (1u << off);
97 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
101 chip->reg_direction = reg_val;
105 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
106 unsigned off, int val)
108 struct pca953x_chip *chip;
112 chip = container_of(gc, struct pca953x_chip, gpio_chip);
114 /* set output level */
116 reg_val = chip->reg_output | (1u << off);
118 reg_val = chip->reg_output & ~(1u << off);
120 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
124 chip->reg_output = reg_val;
127 reg_val = chip->reg_direction & ~(1u << off);
128 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
132 chip->reg_direction = reg_val;
136 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
138 struct pca953x_chip *chip;
142 chip = container_of(gc, struct pca953x_chip, gpio_chip);
144 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
146 /* NOTE: diagnostic already emitted; that's all we should
147 * do unless gpio_*_value_cansleep() calls become different
148 * from their nonsleeping siblings (and report faults).
153 return (reg_val & (1u << off)) ? 1 : 0;
156 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
158 struct pca953x_chip *chip;
162 chip = container_of(gc, struct pca953x_chip, gpio_chip);
165 reg_val = chip->reg_output | (1u << off);
167 reg_val = chip->reg_output & ~(1u << off);
169 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
173 chip->reg_output = reg_val;
176 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
178 struct gpio_chip *gc;
180 gc = &chip->gpio_chip;
182 gc->direction_input = pca953x_gpio_direction_input;
183 gc->direction_output = pca953x_gpio_direction_output;
184 gc->get = pca953x_gpio_get_value;
185 gc->set = pca953x_gpio_set_value;
188 gc->base = chip->gpio_start;
190 gc->label = chip->client->name;
191 gc->owner = THIS_MODULE;
194 static int __devinit pca953x_probe(struct i2c_client *client,
195 const struct i2c_device_id *id)
197 struct pca953x_platform_data *pdata;
198 struct pca953x_chip *chip;
201 pdata = client->dev.platform_data;
205 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
209 chip->client = client;
211 chip->gpio_start = pdata->gpio_base;
213 /* initialize cached registers from their original values.
214 * we can't share this chip with another i2c master.
216 pca953x_setup_gpio(chip, id->driver_data);
218 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
222 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
226 /* set platform specific polarity inversion */
227 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
232 ret = gpiochip_add(&chip->gpio_chip);
237 ret = pdata->setup(client, chip->gpio_chip.base,
238 chip->gpio_chip.ngpio, pdata->context);
240 dev_warn(&client->dev, "setup failed, %d\n", ret);
243 i2c_set_clientdata(client, chip);
251 static int pca953x_remove(struct i2c_client *client)
253 struct pca953x_platform_data *pdata = client->dev.platform_data;
254 struct pca953x_chip *chip = i2c_get_clientdata(client);
257 if (pdata->teardown) {
258 ret = pdata->teardown(client, chip->gpio_chip.base,
259 chip->gpio_chip.ngpio, pdata->context);
261 dev_err(&client->dev, "%s failed, %d\n",
267 ret = gpiochip_remove(&chip->gpio_chip);
269 dev_err(&client->dev, "%s failed, %d\n",
270 "gpiochip_remove()", ret);
278 static struct i2c_driver pca953x_driver = {
282 .probe = pca953x_probe,
283 .remove = pca953x_remove,
284 .id_table = pca953x_id,
287 static int __init pca953x_init(void)
289 return i2c_add_driver(&pca953x_driver);
291 module_init(pca953x_init);
293 static void __exit pca953x_exit(void)
295 i2c_del_driver(&pca953x_driver);
297 module_exit(pca953x_exit);
299 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
300 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
301 MODULE_LICENSE("GPL");