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[] = {
33 /* REVISIT several pca955x parts should work here too */
36 MODULE_DEVICE_TABLE(i2c, pca953x_id);
41 uint16_t reg_direction;
43 struct i2c_client *client;
44 struct gpio_chip gpio_chip;
47 /* NOTE: we can't currently rely on fault codes to come from SMBus
48 * calls, so we map all errors to EIO here and return zero otherwise.
50 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
54 if (chip->gpio_chip.ngpio <= 8)
55 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
57 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
60 dev_err(&chip->client->dev, "failed writing register\n");
67 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
71 if (chip->gpio_chip.ngpio <= 8)
72 ret = i2c_smbus_read_byte_data(chip->client, reg);
74 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
77 dev_err(&chip->client->dev, "failed reading register\n");
85 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
87 struct pca953x_chip *chip;
91 chip = container_of(gc, struct pca953x_chip, gpio_chip);
93 reg_val = chip->reg_direction | (1u << off);
94 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
98 chip->reg_direction = reg_val;
102 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
103 unsigned off, int val)
105 struct pca953x_chip *chip;
109 chip = container_of(gc, struct pca953x_chip, gpio_chip);
111 /* set output level */
113 reg_val = chip->reg_output | (1u << off);
115 reg_val = chip->reg_output & ~(1u << off);
117 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
121 chip->reg_output = reg_val;
124 reg_val = chip->reg_direction & ~(1u << off);
125 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
129 chip->reg_direction = reg_val;
133 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
135 struct pca953x_chip *chip;
139 chip = container_of(gc, struct pca953x_chip, gpio_chip);
141 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
143 /* NOTE: diagnostic already emitted; that's all we should
144 * do unless gpio_*_value_cansleep() calls become different
145 * from their nonsleeping siblings (and report faults).
150 return (reg_val & (1u << off)) ? 1 : 0;
153 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
155 struct pca953x_chip *chip;
159 chip = container_of(gc, struct pca953x_chip, gpio_chip);
162 reg_val = chip->reg_output | (1u << off);
164 reg_val = chip->reg_output & ~(1u << off);
166 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
170 chip->reg_output = reg_val;
173 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
175 struct gpio_chip *gc;
177 gc = &chip->gpio_chip;
179 gc->direction_input = pca953x_gpio_direction_input;
180 gc->direction_output = pca953x_gpio_direction_output;
181 gc->get = pca953x_gpio_get_value;
182 gc->set = pca953x_gpio_set_value;
185 gc->base = chip->gpio_start;
187 gc->label = chip->client->name;
188 gc->owner = THIS_MODULE;
191 static int __devinit pca953x_probe(struct i2c_client *client,
192 const struct i2c_device_id *id)
194 struct pca953x_platform_data *pdata;
195 struct pca953x_chip *chip;
198 pdata = client->dev.platform_data;
202 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
206 chip->client = client;
208 chip->gpio_start = pdata->gpio_base;
210 /* initialize cached registers from their original values.
211 * we can't share this chip with another i2c master.
213 pca953x_setup_gpio(chip, id->driver_data);
215 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
219 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
223 /* set platform specific polarity inversion */
224 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
229 ret = gpiochip_add(&chip->gpio_chip);
234 ret = pdata->setup(client, chip->gpio_chip.base,
235 chip->gpio_chip.ngpio, pdata->context);
237 dev_warn(&client->dev, "setup failed, %d\n", ret);
240 i2c_set_clientdata(client, chip);
248 static int pca953x_remove(struct i2c_client *client)
250 struct pca953x_platform_data *pdata = client->dev.platform_data;
251 struct pca953x_chip *chip = i2c_get_clientdata(client);
254 if (pdata->teardown) {
255 ret = pdata->teardown(client, chip->gpio_chip.base,
256 chip->gpio_chip.ngpio, pdata->context);
258 dev_err(&client->dev, "%s failed, %d\n",
264 ret = gpiochip_remove(&chip->gpio_chip);
266 dev_err(&client->dev, "%s failed, %d\n",
267 "gpiochip_remove()", ret);
275 static struct i2c_driver pca953x_driver = {
279 .probe = pca953x_probe,
280 .remove = pca953x_remove,
281 .id_table = pca953x_id,
284 static int __init pca953x_init(void)
286 return i2c_add_driver(&pca953x_driver);
288 module_init(pca953x_init);
290 static void __exit pca953x_exit(void)
292 i2c_del_driver(&pca953x_driver);
294 module_exit(pca953x_exit);
296 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
297 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
298 MODULE_LICENSE("GPL");