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[] = {
45 /* NYET: { "tca6424", 24, }, */
48 MODULE_DEVICE_TABLE(i2c, pca953x_id);
53 uint16_t reg_direction;
55 struct i2c_client *client;
56 struct pca953x_platform_data *dyn_pdata;
57 struct gpio_chip gpio_chip;
61 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
65 if (chip->gpio_chip.ngpio <= 8)
66 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
68 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
71 dev_err(&chip->client->dev, "failed writing register\n");
78 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
82 if (chip->gpio_chip.ngpio <= 8)
83 ret = i2c_smbus_read_byte_data(chip->client, reg);
85 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
88 dev_err(&chip->client->dev, "failed reading register\n");
96 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
98 struct pca953x_chip *chip;
102 chip = container_of(gc, struct pca953x_chip, gpio_chip);
104 reg_val = chip->reg_direction | (1u << off);
105 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
109 chip->reg_direction = reg_val;
113 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
114 unsigned off, int val)
116 struct pca953x_chip *chip;
120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
122 /* set output level */
124 reg_val = chip->reg_output | (1u << off);
126 reg_val = chip->reg_output & ~(1u << off);
128 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
132 chip->reg_output = reg_val;
135 reg_val = chip->reg_direction & ~(1u << off);
136 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
140 chip->reg_direction = reg_val;
144 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
146 struct pca953x_chip *chip;
150 chip = container_of(gc, struct pca953x_chip, gpio_chip);
152 ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val);
154 /* NOTE: diagnostic already emitted; that's all we should
155 * do unless gpio_*_value_cansleep() calls become different
156 * from their nonsleeping siblings (and report faults).
161 return (reg_val & (1u << off)) ? 1 : 0;
164 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
166 struct pca953x_chip *chip;
170 chip = container_of(gc, struct pca953x_chip, gpio_chip);
173 reg_val = chip->reg_output | (1u << off);
175 reg_val = chip->reg_output & ~(1u << off);
177 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
181 chip->reg_output = reg_val;
184 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
186 struct gpio_chip *gc;
188 gc = &chip->gpio_chip;
190 gc->direction_input = pca953x_gpio_direction_input;
191 gc->direction_output = pca953x_gpio_direction_output;
192 gc->get = pca953x_gpio_get_value;
193 gc->set = pca953x_gpio_set_value;
196 gc->base = chip->gpio_start;
198 gc->label = chip->client->name;
199 gc->dev = &chip->client->dev;
200 gc->owner = THIS_MODULE;
201 gc->names = chip->names;
205 * Handlers for alternative sources of platform_data
207 #ifdef CONFIG_OF_GPIO
209 * Translate OpenFirmware node properties into platform_data
211 static struct pca953x_platform_data *
212 pca953x_get_alt_pdata(struct i2c_client *client)
214 struct pca953x_platform_data *pdata;
215 struct device_node *node;
218 node = dev_archdata_get_node(&client->dev.archdata);
222 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
224 dev_err(&client->dev, "Unable to allocate platform_data\n");
228 pdata->gpio_base = -1;
229 val = of_get_property(node, "linux,gpio-base", NULL);
232 dev_warn(&client->dev,
233 "invalid gpio-base in device tree\n");
235 pdata->gpio_base = *val;
238 val = of_get_property(node, "polarity", NULL);
240 pdata->invert = *val;
245 static struct pca953x_platform_data *
246 pca953x_get_alt_pdata(struct i2c_client *client)
252 static int __devinit pca953x_probe(struct i2c_client *client,
253 const struct i2c_device_id *id)
255 struct pca953x_platform_data *pdata;
256 struct pca953x_chip *chip;
259 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
263 pdata = client->dev.platform_data;
265 pdata = pca953x_get_alt_pdata(client);
267 * Unlike normal platform_data, this is allocated
268 * dynamically and must be freed in the driver
270 chip->dyn_pdata = pdata;
274 dev_dbg(&client->dev, "no platform data\n");
279 chip->client = client;
281 chip->gpio_start = pdata->gpio_base;
283 chip->names = pdata->names;
285 /* initialize cached registers from their original values.
286 * we can't share this chip with another i2c master.
288 pca953x_setup_gpio(chip, id->driver_data);
290 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
294 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
298 /* set platform specific polarity inversion */
299 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
304 ret = gpiochip_add(&chip->gpio_chip);
309 ret = pdata->setup(client, chip->gpio_chip.base,
310 chip->gpio_chip.ngpio, pdata->context);
312 dev_warn(&client->dev, "setup failed, %d\n", ret);
315 i2c_set_clientdata(client, chip);
319 kfree(chip->dyn_pdata);
324 static int pca953x_remove(struct i2c_client *client)
326 struct pca953x_platform_data *pdata = client->dev.platform_data;
327 struct pca953x_chip *chip = i2c_get_clientdata(client);
330 if (pdata->teardown) {
331 ret = pdata->teardown(client, chip->gpio_chip.base,
332 chip->gpio_chip.ngpio, pdata->context);
334 dev_err(&client->dev, "%s failed, %d\n",
340 ret = gpiochip_remove(&chip->gpio_chip);
342 dev_err(&client->dev, "%s failed, %d\n",
343 "gpiochip_remove()", ret);
347 kfree(chip->dyn_pdata);
352 static struct i2c_driver pca953x_driver = {
356 .probe = pca953x_probe,
357 .remove = pca953x_remove,
358 .id_table = pca953x_id,
361 static int __init pca953x_init(void)
363 return i2c_add_driver(&pca953x_driver);
365 /* register after i2c postcore initcall and before
366 * subsys initcalls that may rely on these GPIOs
368 subsys_initcall(pca953x_init);
370 static void __exit pca953x_exit(void)
372 i2c_del_driver(&pca953x_driver);
374 module_exit(pca953x_exit);
376 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
377 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
378 MODULE_LICENSE("GPL");