Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / gpio / pca953x.c
blob8b04343c8c0bb2dfd714cd5cf1674deae3ee08fc
1 /*
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 <asm/gpio.h>
21 #define PCA953X_INPUT 0
22 #define PCA953X_OUTPUT 1
23 #define PCA953X_INVERT 2
24 #define PCA953X_DIRECTION 3
26 /* This is temporary - in 2.6.26 i2c_driver_data should replace it. */
27 struct pca953x_desc {
28 char name[I2C_NAME_SIZE];
29 unsigned long driver_data;
32 static const struct pca953x_desc pca953x_descs[] = {
33 { "pca9534", 8, },
34 { "pca9535", 16, },
35 { "pca9536", 4, },
36 { "pca9537", 4, },
37 { "pca9538", 8, },
38 { "pca9539", 16, },
39 /* REVISIT several pca955x parts should work here too */
42 struct pca953x_chip {
43 unsigned gpio_start;
44 uint16_t reg_output;
45 uint16_t reg_direction;
47 struct i2c_client *client;
48 struct gpio_chip gpio_chip;
51 /* NOTE: we can't currently rely on fault codes to come from SMBus
52 * calls, so we map all errors to EIO here and return zero otherwise.
54 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
56 int ret;
58 if (chip->gpio_chip.ngpio <= 8)
59 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
60 else
61 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
63 if (ret < 0) {
64 dev_err(&chip->client->dev, "failed writing register\n");
65 return -EIO;
68 return 0;
71 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
73 int ret;
75 if (chip->gpio_chip.ngpio <= 8)
76 ret = i2c_smbus_read_byte_data(chip->client, reg);
77 else
78 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
80 if (ret < 0) {
81 dev_err(&chip->client->dev, "failed reading register\n");
82 return -EIO;
85 *val = (uint16_t)ret;
86 return 0;
89 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
91 struct pca953x_chip *chip;
92 uint16_t reg_val;
93 int ret;
95 chip = container_of(gc, struct pca953x_chip, gpio_chip);
97 reg_val = chip->reg_direction | (1u << off);
98 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
99 if (ret)
100 return ret;
102 chip->reg_direction = reg_val;
103 return 0;
106 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
107 unsigned off, int val)
109 struct pca953x_chip *chip;
110 uint16_t reg_val;
111 int ret;
113 chip = container_of(gc, struct pca953x_chip, gpio_chip);
115 /* set output level */
116 if (val)
117 reg_val = chip->reg_output | (1u << off);
118 else
119 reg_val = chip->reg_output & ~(1u << off);
121 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
122 if (ret)
123 return ret;
125 chip->reg_output = reg_val;
127 /* then direction */
128 reg_val = chip->reg_direction & ~(1u << off);
129 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
130 if (ret)
131 return ret;
133 chip->reg_direction = reg_val;
134 return 0;
137 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
139 struct pca953x_chip *chip;
140 uint16_t reg_val;
141 int ret;
143 chip = container_of(gc, struct pca953x_chip, gpio_chip);
145 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
146 if (ret < 0) {
147 /* NOTE: diagnostic already emitted; that's all we should
148 * do unless gpio_*_value_cansleep() calls become different
149 * from their nonsleeping siblings (and report faults).
151 return 0;
154 return (reg_val & (1u << off)) ? 1 : 0;
157 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
159 struct pca953x_chip *chip;
160 uint16_t reg_val;
161 int ret;
163 chip = container_of(gc, struct pca953x_chip, gpio_chip);
165 if (val)
166 reg_val = chip->reg_output | (1u << off);
167 else
168 reg_val = chip->reg_output & ~(1u << off);
170 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
171 if (ret)
172 return;
174 chip->reg_output = reg_val;
177 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
179 struct gpio_chip *gc;
181 gc = &chip->gpio_chip;
183 gc->direction_input = pca953x_gpio_direction_input;
184 gc->direction_output = pca953x_gpio_direction_output;
185 gc->get = pca953x_gpio_get_value;
186 gc->set = pca953x_gpio_set_value;
187 <<<<<<< HEAD:drivers/gpio/pca953x.c
188 =======
189 gc->can_sleep = 1;
190 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/gpio/pca953x.c
192 gc->base = chip->gpio_start;
193 gc->ngpio = gpios;
194 gc->label = chip->client->name;
197 static int __devinit pca953x_probe(struct i2c_client *client)
199 struct pca953x_platform_data *pdata;
200 struct pca953x_chip *chip;
201 int ret, i;
202 const struct pca953x_desc *id = NULL;
204 pdata = client->dev.platform_data;
205 if (pdata == NULL)
206 return -ENODEV;
208 /* this loop vanishes when we get i2c_device_id */
209 for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++)
210 if (!strcmp(pca953x_descs[i].name, client->name)) {
211 id = pca953x_descs + i;
212 break;
214 if (!id)
215 return -ENODEV;
217 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
218 if (chip == NULL)
219 return -ENOMEM;
221 chip->client = client;
223 chip->gpio_start = pdata->gpio_base;
225 /* initialize cached registers from their original values.
226 * we can't share this chip with another i2c master.
228 pca953x_setup_gpio(chip, id->driver_data);
230 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
231 if (ret)
232 goto out_failed;
234 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
235 if (ret)
236 goto out_failed;
238 /* set platform specific polarity inversion */
239 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
240 if (ret)
241 goto out_failed;
244 ret = gpiochip_add(&chip->gpio_chip);
245 if (ret)
246 goto out_failed;
248 if (pdata->setup) {
249 ret = pdata->setup(client, chip->gpio_chip.base,
250 chip->gpio_chip.ngpio, pdata->context);
251 if (ret < 0)
252 dev_warn(&client->dev, "setup failed, %d\n", ret);
255 i2c_set_clientdata(client, chip);
256 return 0;
258 out_failed:
259 kfree(chip);
260 return ret;
263 static int pca953x_remove(struct i2c_client *client)
265 struct pca953x_platform_data *pdata = client->dev.platform_data;
266 struct pca953x_chip *chip = i2c_get_clientdata(client);
267 int ret = 0;
269 if (pdata->teardown) {
270 ret = pdata->teardown(client, chip->gpio_chip.base,
271 chip->gpio_chip.ngpio, pdata->context);
272 if (ret < 0) {
273 dev_err(&client->dev, "%s failed, %d\n",
274 "teardown", ret);
275 return ret;
279 ret = gpiochip_remove(&chip->gpio_chip);
280 if (ret) {
281 dev_err(&client->dev, "%s failed, %d\n",
282 "gpiochip_remove()", ret);
283 return ret;
286 kfree(chip);
287 return 0;
290 static struct i2c_driver pca953x_driver = {
291 .driver = {
292 .name = "pca953x",
294 .probe = pca953x_probe,
295 .remove = pca953x_remove,
298 static int __init pca953x_init(void)
300 return i2c_add_driver(&pca953x_driver);
302 module_init(pca953x_init);
304 static void __exit pca953x_exit(void)
306 i2c_del_driver(&pca953x_driver);
308 module_exit(pca953x_exit);
310 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
311 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
312 MODULE_LICENSE("GPL");