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