2dae94fbabf4f94c21ec8aab08cedca312daa732
[safe/jmp/linux-2.6] / drivers / gpio / pca953x.c
1 /*
2  *  pca953x.c - 4/8/16 bit I/O ports
3  *
4  *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5  *  Copyright (C) 2007 Marvell International Ltd.
6  *
7  *  Derived from drivers/i2c/chips/pca9539.c
8  *
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.
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/i2c/pca953x.h>
18
19 #include <asm/gpio.h>
20
21 #define PCA953X_INPUT          0
22 #define PCA953X_OUTPUT         1
23 #define PCA953X_INVERT         2
24 #define PCA953X_DIRECTION      3
25
26 static const struct i2c_device_id pca953x_id[] = {
27         { "pca9534", 8, },
28         { "pca9535", 16, },
29         { "pca9536", 4, },
30         { "pca9537", 4, },
31         { "pca9538", 8, },
32         { "pca9539", 16, },
33         { "pca9554", 8, },
34         { "pca9555", 16, },
35         { "pca9557", 8, },
36
37         { "max7310", 8, },
38         { "pca6107", 8, },
39         { "tca6408", 8, },
40         { "tca6416", 16, },
41         /* NYET:  { "tca6424", 24, }, */
42         { }
43 };
44 MODULE_DEVICE_TABLE(i2c, pca953x_id);
45
46 struct pca953x_chip {
47         unsigned gpio_start;
48         uint16_t reg_output;
49         uint16_t reg_direction;
50
51         struct i2c_client *client;
52         struct gpio_chip gpio_chip;
53         char **names;
54 };
55
56 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
57 {
58         int ret;
59
60         if (chip->gpio_chip.ngpio <= 8)
61                 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
62         else
63                 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
64
65         if (ret < 0) {
66                 dev_err(&chip->client->dev, "failed writing register\n");
67                 return ret;
68         }
69
70         return 0;
71 }
72
73 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
74 {
75         int ret;
76
77         if (chip->gpio_chip.ngpio <= 8)
78                 ret = i2c_smbus_read_byte_data(chip->client, reg);
79         else
80                 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
81
82         if (ret < 0) {
83                 dev_err(&chip->client->dev, "failed reading register\n");
84                 return ret;
85         }
86
87         *val = (uint16_t)ret;
88         return 0;
89 }
90
91 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
92 {
93         struct pca953x_chip *chip;
94         uint16_t reg_val;
95         int ret;
96
97         chip = container_of(gc, struct pca953x_chip, gpio_chip);
98
99         reg_val = chip->reg_direction | (1u << off);
100         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
101         if (ret)
102                 return ret;
103
104         chip->reg_direction = reg_val;
105         return 0;
106 }
107
108 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
109                 unsigned off, int val)
110 {
111         struct pca953x_chip *chip;
112         uint16_t reg_val;
113         int ret;
114
115         chip = container_of(gc, struct pca953x_chip, gpio_chip);
116
117         /* set output level */
118         if (val)
119                 reg_val = chip->reg_output | (1u << off);
120         else
121                 reg_val = chip->reg_output & ~(1u << off);
122
123         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
124         if (ret)
125                 return ret;
126
127         chip->reg_output = reg_val;
128
129         /* then direction */
130         reg_val = chip->reg_direction & ~(1u << off);
131         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
132         if (ret)
133                 return ret;
134
135         chip->reg_direction = reg_val;
136         return 0;
137 }
138
139 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
140 {
141         struct pca953x_chip *chip;
142         uint16_t reg_val;
143         int ret;
144
145         chip = container_of(gc, struct pca953x_chip, gpio_chip);
146
147         ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
148         if (ret < 0) {
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).
152                  */
153                 return 0;
154         }
155
156         return (reg_val & (1u << off)) ? 1 : 0;
157 }
158
159 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
160 {
161         struct pca953x_chip *chip;
162         uint16_t reg_val;
163         int ret;
164
165         chip = container_of(gc, struct pca953x_chip, gpio_chip);
166
167         if (val)
168                 reg_val = chip->reg_output | (1u << off);
169         else
170                 reg_val = chip->reg_output & ~(1u << off);
171
172         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
173         if (ret)
174                 return;
175
176         chip->reg_output = reg_val;
177 }
178
179 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
180 {
181         struct gpio_chip *gc;
182
183         gc = &chip->gpio_chip;
184
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;
189         gc->can_sleep = 1;
190
191         gc->base = chip->gpio_start;
192         gc->ngpio = gpios;
193         gc->label = chip->client->name;
194         gc->dev = &chip->client->dev;
195         gc->owner = THIS_MODULE;
196         gc->names = chip->names;
197 }
198
199 static int __devinit pca953x_probe(struct i2c_client *client,
200                                    const struct i2c_device_id *id)
201 {
202         struct pca953x_platform_data *pdata;
203         struct pca953x_chip *chip;
204         int ret;
205
206         pdata = client->dev.platform_data;
207         if (pdata == NULL) {
208                 dev_dbg(&client->dev, "no platform data\n");
209                 return -EINVAL;
210         }
211
212         chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
213         if (chip == NULL)
214                 return -ENOMEM;
215
216         chip->client = client;
217
218         chip->gpio_start = pdata->gpio_base;
219
220         chip->names = pdata->names;
221
222         /* initialize cached registers from their original values.
223          * we can't share this chip with another i2c master.
224          */
225         pca953x_setup_gpio(chip, id->driver_data);
226
227         ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
228         if (ret)
229                 goto out_failed;
230
231         ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
232         if (ret)
233                 goto out_failed;
234
235         /* set platform specific polarity inversion */
236         ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
237         if (ret)
238                 goto out_failed;
239
240
241         ret = gpiochip_add(&chip->gpio_chip);
242         if (ret)
243                 goto out_failed;
244
245         if (pdata->setup) {
246                 ret = pdata->setup(client, chip->gpio_chip.base,
247                                 chip->gpio_chip.ngpio, pdata->context);
248                 if (ret < 0)
249                         dev_warn(&client->dev, "setup failed, %d\n", ret);
250         }
251
252         i2c_set_clientdata(client, chip);
253         return 0;
254
255 out_failed:
256         kfree(chip);
257         return ret;
258 }
259
260 static int pca953x_remove(struct i2c_client *client)
261 {
262         struct pca953x_platform_data *pdata = client->dev.platform_data;
263         struct pca953x_chip *chip = i2c_get_clientdata(client);
264         int ret = 0;
265
266         if (pdata->teardown) {
267                 ret = pdata->teardown(client, chip->gpio_chip.base,
268                                 chip->gpio_chip.ngpio, pdata->context);
269                 if (ret < 0) {
270                         dev_err(&client->dev, "%s failed, %d\n",
271                                         "teardown", ret);
272                         return ret;
273                 }
274         }
275
276         ret = gpiochip_remove(&chip->gpio_chip);
277         if (ret) {
278                 dev_err(&client->dev, "%s failed, %d\n",
279                                 "gpiochip_remove()", ret);
280                 return ret;
281         }
282
283         kfree(chip);
284         return 0;
285 }
286
287 static struct i2c_driver pca953x_driver = {
288         .driver = {
289                 .name   = "pca953x",
290         },
291         .probe          = pca953x_probe,
292         .remove         = pca953x_remove,
293         .id_table       = pca953x_id,
294 };
295
296 static int __init pca953x_init(void)
297 {
298         return i2c_add_driver(&pca953x_driver);
299 }
300 /* register after i2c postcore initcall and before
301  * subsys initcalls that may rely on these GPIOs
302  */
303 subsys_initcall(pca953x_init);
304
305 static void __exit pca953x_exit(void)
306 {
307         i2c_del_driver(&pca953x_driver);
308 }
309 module_exit(pca953x_exit);
310
311 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
312 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
313 MODULE_LICENSE("GPL");