gpio: pca953x: Get platform_data from OpenFirmware
[linux-block.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 #ifdef CONFIG_OF_GPIO
19 #include <linux/of_platform.h>
20 #include <linux/of_gpio.h>
21 #endif
22
23 #include <asm/gpio.h>
24
25 #define PCA953X_INPUT          0
26 #define PCA953X_OUTPUT         1
27 #define PCA953X_INVERT         2
28 #define PCA953X_DIRECTION      3
29
30 static const struct i2c_device_id pca953x_id[] = {
31         { "pca9534", 8, },
32         { "pca9535", 16, },
33         { "pca9536", 4, },
34         { "pca9537", 4, },
35         { "pca9538", 8, },
36         { "pca9539", 16, },
37         { "pca9554", 8, },
38         { "pca9555", 16, },
39         { "pca9557", 8, },
40
41         { "max7310", 8, },
42         { "pca6107", 8, },
43         { "tca6408", 8, },
44         { "tca6416", 16, },
45         /* NYET:  { "tca6424", 24, }, */
46         { }
47 };
48 MODULE_DEVICE_TABLE(i2c, pca953x_id);
49
50 struct pca953x_chip {
51         unsigned gpio_start;
52         uint16_t reg_output;
53         uint16_t reg_direction;
54
55         struct i2c_client *client;
56         struct pca953x_platform_data *dyn_pdata;
57         struct gpio_chip gpio_chip;
58         char **names;
59 };
60
61 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
62 {
63         int ret;
64
65         if (chip->gpio_chip.ngpio <= 8)
66                 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
67         else
68                 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
69
70         if (ret < 0) {
71                 dev_err(&chip->client->dev, "failed writing register\n");
72                 return ret;
73         }
74
75         return 0;
76 }
77
78 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
79 {
80         int ret;
81
82         if (chip->gpio_chip.ngpio <= 8)
83                 ret = i2c_smbus_read_byte_data(chip->client, reg);
84         else
85                 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
86
87         if (ret < 0) {
88                 dev_err(&chip->client->dev, "failed reading register\n");
89                 return ret;
90         }
91
92         *val = (uint16_t)ret;
93         return 0;
94 }
95
96 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
97 {
98         struct pca953x_chip *chip;
99         uint16_t reg_val;
100         int ret;
101
102         chip = container_of(gc, struct pca953x_chip, gpio_chip);
103
104         reg_val = chip->reg_direction | (1u << off);
105         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
106         if (ret)
107                 return ret;
108
109         chip->reg_direction = reg_val;
110         return 0;
111 }
112
113 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
114                 unsigned off, int val)
115 {
116         struct pca953x_chip *chip;
117         uint16_t reg_val;
118         int ret;
119
120         chip = container_of(gc, struct pca953x_chip, gpio_chip);
121
122         /* set output level */
123         if (val)
124                 reg_val = chip->reg_output | (1u << off);
125         else
126                 reg_val = chip->reg_output & ~(1u << off);
127
128         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
129         if (ret)
130                 return ret;
131
132         chip->reg_output = reg_val;
133
134         /* then direction */
135         reg_val = chip->reg_direction & ~(1u << off);
136         ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
137         if (ret)
138                 return ret;
139
140         chip->reg_direction = reg_val;
141         return 0;
142 }
143
144 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
145 {
146         struct pca953x_chip *chip;
147         uint16_t reg_val;
148         int ret;
149
150         chip = container_of(gc, struct pca953x_chip, gpio_chip);
151
152         ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
153         if (ret < 0) {
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).
157                  */
158                 return 0;
159         }
160
161         return (reg_val & (1u << off)) ? 1 : 0;
162 }
163
164 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
165 {
166         struct pca953x_chip *chip;
167         uint16_t reg_val;
168         int ret;
169
170         chip = container_of(gc, struct pca953x_chip, gpio_chip);
171
172         if (val)
173                 reg_val = chip->reg_output | (1u << off);
174         else
175                 reg_val = chip->reg_output & ~(1u << off);
176
177         ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
178         if (ret)
179                 return;
180
181         chip->reg_output = reg_val;
182 }
183
184 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
185 {
186         struct gpio_chip *gc;
187
188         gc = &chip->gpio_chip;
189
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;
194         gc->can_sleep = 1;
195
196         gc->base = chip->gpio_start;
197         gc->ngpio = gpios;
198         gc->label = chip->client->name;
199         gc->dev = &chip->client->dev;
200         gc->owner = THIS_MODULE;
201         gc->names = chip->names;
202 }
203
204 /*
205  * Handlers for alternative sources of platform_data
206  */
207 #ifdef CONFIG_OF_GPIO
208 /*
209  * Translate OpenFirmware node properties into platform_data
210  */
211 static struct pca953x_platform_data *
212 pca953x_get_alt_pdata(struct i2c_client *client)
213 {
214         struct pca953x_platform_data *pdata;
215         struct device_node *node;
216         const uint16_t *val;
217
218         node = dev_archdata_get_node(&client->dev.archdata);
219         if (node == NULL)
220                 return NULL;
221
222         pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
223         if (pdata == NULL) {
224                 dev_err(&client->dev, "Unable to allocate platform_data\n");
225                 return NULL;
226         }
227
228         pdata->gpio_base = -1;
229         val = of_get_property(node, "linux,gpio-base", NULL);
230         if (val) {
231                 if (*val < 0)
232                         dev_warn(&client->dev,
233                                  "invalid gpio-base in device tree\n");
234                 else
235                         pdata->gpio_base = *val;
236         }
237
238         val = of_get_property(node, "polarity", NULL);
239         if (val)
240                 pdata->invert = *val;
241
242         return pdata;
243 }
244 #else
245 static struct pca953x_platform_data *
246 pca953x_get_alt_pdata(struct i2c_client *client)
247 {
248         return NULL;
249 }
250 #endif
251
252 static int __devinit pca953x_probe(struct i2c_client *client,
253                                    const struct i2c_device_id *id)
254 {
255         struct pca953x_platform_data *pdata;
256         struct pca953x_chip *chip;
257         int ret;
258
259         chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
260         if (chip == NULL)
261                 return -ENOMEM;
262
263         pdata = client->dev.platform_data;
264         if (pdata == NULL) {
265                 pdata = pca953x_get_alt_pdata(client);
266                 /*
267                  * Unlike normal platform_data, this is allocated
268                  * dynamically and must be freed in the driver
269                  */
270                 chip->dyn_pdata = pdata;
271         }
272
273         if (pdata == NULL) {
274                 dev_dbg(&client->dev, "no platform data\n");
275                 ret = -EINVAL;
276                 goto out_failed;
277         }
278
279         chip->client = client;
280
281         chip->gpio_start = pdata->gpio_base;
282
283         chip->names = pdata->names;
284
285         /* initialize cached registers from their original values.
286          * we can't share this chip with another i2c master.
287          */
288         pca953x_setup_gpio(chip, id->driver_data);
289
290         ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
291         if (ret)
292                 goto out_failed;
293
294         ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
295         if (ret)
296                 goto out_failed;
297
298         /* set platform specific polarity inversion */
299         ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
300         if (ret)
301                 goto out_failed;
302
303
304         ret = gpiochip_add(&chip->gpio_chip);
305         if (ret)
306                 goto out_failed;
307
308         if (pdata->setup) {
309                 ret = pdata->setup(client, chip->gpio_chip.base,
310                                 chip->gpio_chip.ngpio, pdata->context);
311                 if (ret < 0)
312                         dev_warn(&client->dev, "setup failed, %d\n", ret);
313         }
314
315         i2c_set_clientdata(client, chip);
316         return 0;
317
318 out_failed:
319         kfree(chip->dyn_pdata);
320         kfree(chip);
321         return ret;
322 }
323
324 static int pca953x_remove(struct i2c_client *client)
325 {
326         struct pca953x_platform_data *pdata = client->dev.platform_data;
327         struct pca953x_chip *chip = i2c_get_clientdata(client);
328         int ret = 0;
329
330         if (pdata->teardown) {
331                 ret = pdata->teardown(client, chip->gpio_chip.base,
332                                 chip->gpio_chip.ngpio, pdata->context);
333                 if (ret < 0) {
334                         dev_err(&client->dev, "%s failed, %d\n",
335                                         "teardown", ret);
336                         return ret;
337                 }
338         }
339
340         ret = gpiochip_remove(&chip->gpio_chip);
341         if (ret) {
342                 dev_err(&client->dev, "%s failed, %d\n",
343                                 "gpiochip_remove()", ret);
344                 return ret;
345         }
346
347         kfree(chip->dyn_pdata);
348         kfree(chip);
349         return 0;
350 }
351
352 static struct i2c_driver pca953x_driver = {
353         .driver = {
354                 .name   = "pca953x",
355         },
356         .probe          = pca953x_probe,
357         .remove         = pca953x_remove,
358         .id_table       = pca953x_id,
359 };
360
361 static int __init pca953x_init(void)
362 {
363         return i2c_add_driver(&pca953x_driver);
364 }
365 /* register after i2c postcore initcall and before
366  * subsys initcalls that may rely on these GPIOs
367  */
368 subsys_initcall(pca953x_init);
369
370 static void __exit pca953x_exit(void)
371 {
372         i2c_del_driver(&pca953x_driver);
373 }
374 module_exit(pca953x_exit);
375
376 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
377 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
378 MODULE_LICENSE("GPL");