[ARM] 5133/2: at91sam9g20 defconfig file
[sfrench/cifs-2.6.git] / 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         { "pca9555", 16, },
34         { "pca9557", 8, },
35         /* REVISIT several pca955x parts should work here too */
36         { }
37 };
38 MODULE_DEVICE_TABLE(i2c, pca953x_id);
39
40 struct pca953x_chip {
41         unsigned gpio_start;
42         uint16_t reg_output;
43         uint16_t reg_direction;
44
45         struct i2c_client *client;
46         struct gpio_chip gpio_chip;
47 };
48
49 /* NOTE:  we can't currently rely on fault codes to come from SMBus
50  * calls, so we map all errors to EIO here and return zero otherwise.
51  */
52 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
53 {
54         int ret;
55
56         if (chip->gpio_chip.ngpio <= 8)
57                 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
58         else
59                 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
60
61         if (ret < 0) {
62                 dev_err(&chip->client->dev, "failed writing register\n");
63                 return -EIO;
64         }
65
66         return 0;
67 }
68
69 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
70 {
71         int ret;
72
73         if (chip->gpio_chip.ngpio <= 8)
74                 ret = i2c_smbus_read_byte_data(chip->client, reg);
75         else
76                 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
77
78         if (ret < 0) {
79                 dev_err(&chip->client->dev, "failed reading register\n");
80                 return -EIO;
81         }
82
83         *val = (uint16_t)ret;
84         return 0;
85 }
86
87 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
88 {
89         struct pca953x_chip *chip;
90         uint16_t reg_val;
91         int ret;
92
93         chip = container_of(gc, struct pca953x_chip, gpio_chip);
94
95         reg_val = chip->reg_direction | (1u << off);
96         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
97         if (ret)
98                 return ret;
99
100         chip->reg_direction = reg_val;
101         return 0;
102 }
103
104 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
105                 unsigned off, int val)
106 {
107         struct pca953x_chip *chip;
108         uint16_t reg_val;
109         int ret;
110
111         chip = container_of(gc, struct pca953x_chip, gpio_chip);
112
113         /* set output level */
114         if (val)
115                 reg_val = chip->reg_output | (1u << off);
116         else
117                 reg_val = chip->reg_output & ~(1u << off);
118
119         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
120         if (ret)
121                 return ret;
122
123         chip->reg_output = reg_val;
124
125         /* then direction */
126         reg_val = chip->reg_direction & ~(1u << off);
127         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
128         if (ret)
129                 return ret;
130
131         chip->reg_direction = reg_val;
132         return 0;
133 }
134
135 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
136 {
137         struct pca953x_chip *chip;
138         uint16_t reg_val;
139         int ret;
140
141         chip = container_of(gc, struct pca953x_chip, gpio_chip);
142
143         ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
144         if (ret < 0) {
145                 /* NOTE:  diagnostic already emitted; that's all we should
146                  * do unless gpio_*_value_cansleep() calls become different
147                  * from their nonsleeping siblings (and report faults).
148                  */
149                 return 0;
150         }
151
152         return (reg_val & (1u << off)) ? 1 : 0;
153 }
154
155 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
156 {
157         struct pca953x_chip *chip;
158         uint16_t reg_val;
159         int ret;
160
161         chip = container_of(gc, struct pca953x_chip, gpio_chip);
162
163         if (val)
164                 reg_val = chip->reg_output | (1u << off);
165         else
166                 reg_val = chip->reg_output & ~(1u << off);
167
168         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
169         if (ret)
170                 return;
171
172         chip->reg_output = reg_val;
173 }
174
175 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
176 {
177         struct gpio_chip *gc;
178
179         gc = &chip->gpio_chip;
180
181         gc->direction_input  = pca953x_gpio_direction_input;
182         gc->direction_output = pca953x_gpio_direction_output;
183         gc->get = pca953x_gpio_get_value;
184         gc->set = pca953x_gpio_set_value;
185         gc->can_sleep = 1;
186
187         gc->base = chip->gpio_start;
188         gc->ngpio = gpios;
189         gc->label = chip->client->name;
190         gc->owner = THIS_MODULE;
191 }
192
193 static int __devinit pca953x_probe(struct i2c_client *client,
194                                    const struct i2c_device_id *id)
195 {
196         struct pca953x_platform_data *pdata;
197         struct pca953x_chip *chip;
198         int ret;
199
200         pdata = client->dev.platform_data;
201         if (pdata == NULL)
202                 return -ENODEV;
203
204         chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
205         if (chip == NULL)
206                 return -ENOMEM;
207
208         chip->client = client;
209
210         chip->gpio_start = pdata->gpio_base;
211
212         /* initialize cached registers from their original values.
213          * we can't share this chip with another i2c master.
214          */
215         pca953x_setup_gpio(chip, id->driver_data);
216
217         ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
218         if (ret)
219                 goto out_failed;
220
221         ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
222         if (ret)
223                 goto out_failed;
224
225         /* set platform specific polarity inversion */
226         ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
227         if (ret)
228                 goto out_failed;
229
230
231         ret = gpiochip_add(&chip->gpio_chip);
232         if (ret)
233                 goto out_failed;
234
235         if (pdata->setup) {
236                 ret = pdata->setup(client, chip->gpio_chip.base,
237                                 chip->gpio_chip.ngpio, pdata->context);
238                 if (ret < 0)
239                         dev_warn(&client->dev, "setup failed, %d\n", ret);
240         }
241
242         i2c_set_clientdata(client, chip);
243         return 0;
244
245 out_failed:
246         kfree(chip);
247         return ret;
248 }
249
250 static int pca953x_remove(struct i2c_client *client)
251 {
252         struct pca953x_platform_data *pdata = client->dev.platform_data;
253         struct pca953x_chip *chip = i2c_get_clientdata(client);
254         int ret = 0;
255
256         if (pdata->teardown) {
257                 ret = pdata->teardown(client, chip->gpio_chip.base,
258                                 chip->gpio_chip.ngpio, pdata->context);
259                 if (ret < 0) {
260                         dev_err(&client->dev, "%s failed, %d\n",
261                                         "teardown", ret);
262                         return ret;
263                 }
264         }
265
266         ret = gpiochip_remove(&chip->gpio_chip);
267         if (ret) {
268                 dev_err(&client->dev, "%s failed, %d\n",
269                                 "gpiochip_remove()", ret);
270                 return ret;
271         }
272
273         kfree(chip);
274         return 0;
275 }
276
277 static struct i2c_driver pca953x_driver = {
278         .driver = {
279                 .name   = "pca953x",
280         },
281         .probe          = pca953x_probe,
282         .remove         = pca953x_remove,
283         .id_table       = pca953x_id,
284 };
285
286 static int __init pca953x_init(void)
287 {
288         return i2c_add_driver(&pca953x_driver);
289 }
290 module_init(pca953x_init);
291
292 static void __exit pca953x_exit(void)
293 {
294         i2c_del_driver(&pca953x_driver);
295 }
296 module_exit(pca953x_exit);
297
298 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
299 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
300 MODULE_LICENSE("GPL");