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[] = {
41 /* NYET: { "tca6424", 24, }, */
44 MODULE_DEVICE_TABLE(i2c, pca953x_id);
49 uint16_t reg_direction;
51 struct i2c_client *client;
52 struct gpio_chip gpio_chip;
56 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
60 if (chip->gpio_chip.ngpio <= 8)
61 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
63 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
66 dev_err(&chip->client->dev, "failed writing register\n");
73 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
77 if (chip->gpio_chip.ngpio <= 8)
78 ret = i2c_smbus_read_byte_data(chip->client, reg);
80 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
83 dev_err(&chip->client->dev, "failed reading register\n");
91 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
93 struct pca953x_chip *chip;
97 chip = container_of(gc, struct pca953x_chip, gpio_chip);
99 reg_val = chip->reg_direction | (1u << off);
100 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
104 chip->reg_direction = reg_val;
108 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
109 unsigned off, int val)
111 struct pca953x_chip *chip;
115 chip = container_of(gc, struct pca953x_chip, gpio_chip);
117 /* set output level */
119 reg_val = chip->reg_output | (1u << off);
121 reg_val = chip->reg_output & ~(1u << off);
123 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
127 chip->reg_output = reg_val;
130 reg_val = chip->reg_direction & ~(1u << off);
131 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
135 chip->reg_direction = reg_val;
139 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
141 struct pca953x_chip *chip;
145 chip = container_of(gc, struct pca953x_chip, gpio_chip);
147 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
149 /* NOTE: diagnostic already emitted; that's all we should
150 * do unless gpio_*_value_cansleep() calls become different
151 * from their nonsleeping siblings (and report faults).
156 return (reg_val & (1u << off)) ? 1 : 0;
159 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
161 struct pca953x_chip *chip;
165 chip = container_of(gc, struct pca953x_chip, gpio_chip);
168 reg_val = chip->reg_output | (1u << off);
170 reg_val = chip->reg_output & ~(1u << off);
172 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
176 chip->reg_output = reg_val;
179 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
181 struct gpio_chip *gc;
183 gc = &chip->gpio_chip;
185 gc->direction_input = pca953x_gpio_direction_input;
186 gc->direction_output = pca953x_gpio_direction_output;
187 gc->get = pca953x_gpio_get_value;
188 gc->set = pca953x_gpio_set_value;
191 gc->base = chip->gpio_start;
193 gc->label = chip->client->name;
194 gc->dev = &chip->client->dev;
195 gc->owner = THIS_MODULE;
196 gc->names = chip->names;
199 static int __devinit pca953x_probe(struct i2c_client *client,
200 const struct i2c_device_id *id)
202 struct pca953x_platform_data *pdata;
203 struct pca953x_chip *chip;
206 pdata = client->dev.platform_data;
208 dev_dbg(&client->dev, "no platform data\n");
212 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
216 chip->client = client;
218 chip->gpio_start = pdata->gpio_base;
220 chip->names = pdata->names;
222 /* initialize cached registers from their original values.
223 * we can't share this chip with another i2c master.
225 pca953x_setup_gpio(chip, id->driver_data);
227 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
231 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
235 /* set platform specific polarity inversion */
236 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
241 ret = gpiochip_add(&chip->gpio_chip);
246 ret = pdata->setup(client, chip->gpio_chip.base,
247 chip->gpio_chip.ngpio, pdata->context);
249 dev_warn(&client->dev, "setup failed, %d\n", ret);
252 i2c_set_clientdata(client, chip);
260 static int pca953x_remove(struct i2c_client *client)
262 struct pca953x_platform_data *pdata = client->dev.platform_data;
263 struct pca953x_chip *chip = i2c_get_clientdata(client);
266 if (pdata->teardown) {
267 ret = pdata->teardown(client, chip->gpio_chip.base,
268 chip->gpio_chip.ngpio, pdata->context);
270 dev_err(&client->dev, "%s failed, %d\n",
276 ret = gpiochip_remove(&chip->gpio_chip);
278 dev_err(&client->dev, "%s failed, %d\n",
279 "gpiochip_remove()", ret);
287 static struct i2c_driver pca953x_driver = {
291 .probe = pca953x_probe,
292 .remove = pca953x_remove,
293 .id_table = pca953x_id,
296 static int __init pca953x_init(void)
298 return i2c_add_driver(&pca953x_driver);
300 /* register after i2c postcore initcall and before
301 * subsys initcalls that may rely on these GPIOs
303 subsys_initcall(pca953x_init);
305 static void __exit pca953x_exit(void)
307 i2c_del_driver(&pca953x_driver);
309 module_exit(pca953x_exit);
311 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
312 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
313 MODULE_LICENSE("GPL");