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>
19 #include <linux/of_platform.h>
20 #include <linux/of_gpio.h>
25 #define PCA953X_INPUT 0
26 #define PCA953X_OUTPUT 1
27 #define PCA953X_INVERT 2
28 #define PCA953X_DIRECTION 3
30 static const struct i2c_device_id pca953x_id[] = {
47 /* NYET: { "tca6424", 24, }, */
50 MODULE_DEVICE_TABLE(i2c, pca953x_id);
55 uint16_t reg_direction;
57 struct i2c_client *client;
58 struct pca953x_platform_data *dyn_pdata;
59 struct gpio_chip gpio_chip;
63 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
67 if (chip->gpio_chip.ngpio <= 8)
68 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
70 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
73 dev_err(&chip->client->dev, "failed writing register\n");
80 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
84 if (chip->gpio_chip.ngpio <= 8)
85 ret = i2c_smbus_read_byte_data(chip->client, reg);
87 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
90 dev_err(&chip->client->dev, "failed reading register\n");
98 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
100 struct pca953x_chip *chip;
104 chip = container_of(gc, struct pca953x_chip, gpio_chip);
106 reg_val = chip->reg_direction | (1u << off);
107 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
111 chip->reg_direction = reg_val;
115 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
116 unsigned off, int val)
118 struct pca953x_chip *chip;
122 chip = container_of(gc, struct pca953x_chip, gpio_chip);
124 /* set output level */
126 reg_val = chip->reg_output | (1u << off);
128 reg_val = chip->reg_output & ~(1u << off);
130 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
134 chip->reg_output = reg_val;
137 reg_val = chip->reg_direction & ~(1u << off);
138 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
142 chip->reg_direction = reg_val;
146 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
148 struct pca953x_chip *chip;
152 chip = container_of(gc, struct pca953x_chip, gpio_chip);
154 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
156 /* NOTE: diagnostic already emitted; that's all we should
157 * do unless gpio_*_value_cansleep() calls become different
158 * from their nonsleeping siblings (and report faults).
163 return (reg_val & (1u << off)) ? 1 : 0;
166 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
168 struct pca953x_chip *chip;
172 chip = container_of(gc, struct pca953x_chip, gpio_chip);
175 reg_val = chip->reg_output | (1u << off);
177 reg_val = chip->reg_output & ~(1u << off);
179 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
183 chip->reg_output = reg_val;
186 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
188 struct gpio_chip *gc;
190 gc = &chip->gpio_chip;
192 gc->direction_input = pca953x_gpio_direction_input;
193 gc->direction_output = pca953x_gpio_direction_output;
194 gc->get = pca953x_gpio_get_value;
195 gc->set = pca953x_gpio_set_value;
198 gc->base = chip->gpio_start;
200 gc->label = chip->client->name;
201 gc->dev = &chip->client->dev;
202 gc->owner = THIS_MODULE;
203 gc->names = chip->names;
207 * Handlers for alternative sources of platform_data
209 #ifdef CONFIG_OF_GPIO
211 * Translate OpenFirmware node properties into platform_data
213 static struct pca953x_platform_data *
214 pca953x_get_alt_pdata(struct i2c_client *client)
216 struct pca953x_platform_data *pdata;
217 struct device_node *node;
220 node = dev_archdata_get_node(&client->dev.archdata);
224 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
226 dev_err(&client->dev, "Unable to allocate platform_data\n");
230 pdata->gpio_base = -1;
231 val = of_get_property(node, "linux,gpio-base", NULL);
234 dev_warn(&client->dev,
235 "invalid gpio-base in device tree\n");
237 pdata->gpio_base = *val;
240 val = of_get_property(node, "polarity", NULL);
242 pdata->invert = *val;
247 static struct pca953x_platform_data *
248 pca953x_get_alt_pdata(struct i2c_client *client)
254 static int __devinit pca953x_probe(struct i2c_client *client,
255 const struct i2c_device_id *id)
257 struct pca953x_platform_data *pdata;
258 struct pca953x_chip *chip;
261 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
265 pdata = client->dev.platform_data;
267 pdata = pca953x_get_alt_pdata(client);
269 * Unlike normal platform_data, this is allocated
270 * dynamically and must be freed in the driver
272 chip->dyn_pdata = pdata;
276 dev_dbg(&client->dev, "no platform data\n");
281 chip->client = client;
283 chip->gpio_start = pdata->gpio_base;
285 chip->names = pdata->names;
287 /* initialize cached registers from their original values.
288 * we can't share this chip with another i2c master.
290 pca953x_setup_gpio(chip, id->driver_data);
292 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
296 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
300 /* set platform specific polarity inversion */
301 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
306 ret = gpiochip_add(&chip->gpio_chip);
311 ret = pdata->setup(client, chip->gpio_chip.base,
312 chip->gpio_chip.ngpio, pdata->context);
314 dev_warn(&client->dev, "setup failed, %d\n", ret);
317 i2c_set_clientdata(client, chip);
321 kfree(chip->dyn_pdata);
326 static int pca953x_remove(struct i2c_client *client)
328 struct pca953x_platform_data *pdata = client->dev.platform_data;
329 struct pca953x_chip *chip = i2c_get_clientdata(client);
332 if (pdata->teardown) {
333 ret = pdata->teardown(client, chip->gpio_chip.base,
334 chip->gpio_chip.ngpio, pdata->context);
336 dev_err(&client->dev, "%s failed, %d\n",
342 ret = gpiochip_remove(&chip->gpio_chip);
344 dev_err(&client->dev, "%s failed, %d\n",
345 "gpiochip_remove()", ret);
349 kfree(chip->dyn_pdata);
354 static struct i2c_driver pca953x_driver = {
358 .probe = pca953x_probe,
359 .remove = pca953x_remove,
360 .id_table = pca953x_id,
363 static int __init pca953x_init(void)
365 return i2c_add_driver(&pca953x_driver);
367 /* register after i2c postcore initcall and before
368 * subsys initcalls that may rely on these GPIOs
370 subsys_initcall(pca953x_init);
372 static void __exit pca953x_exit(void)
374 i2c_del_driver(&pca953x_driver);
376 module_exit(pca953x_exit);
378 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
379 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
380 MODULE_LICENSE("GPL");