gpio: pca953x: Get platform_data from OpenFirmware
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpio / pca953x.c
CommitLineData
9e60fdcf 1/*
f5e8ff48 2 * pca953x.c - 4/8/16 bit I/O ports
9e60fdcf 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>
d1c057e3 17#include <linux/i2c/pca953x.h>
1965d303
NC
18#ifdef CONFIG_OF_GPIO
19#include <linux/of_platform.h>
20#include <linux/of_gpio.h>
21#endif
9e60fdcf 22
23#include <asm/gpio.h>
24
f5e8ff48
GL
25#define PCA953X_INPUT 0
26#define PCA953X_OUTPUT 1
27#define PCA953X_INVERT 2
28#define PCA953X_DIRECTION 3
9e60fdcf 29
3760f736 30static const struct i2c_device_id pca953x_id[] = {
f5e8ff48
GL
31 { "pca9534", 8, },
32 { "pca9535", 16, },
33 { "pca9536", 4, },
34 { "pca9537", 4, },
35 { "pca9538", 8, },
36 { "pca9539", 16, },
69292b34 37 { "pca9554", 8, },
f39e5781
WN
38 { "pca9555", 16, },
39 { "pca9557", 8, },
ab5dc372 40
7059d4b0 41 { "max7310", 8, },
ab5dc372
DB
42 { "pca6107", 8, },
43 { "tca6408", 8, },
44 { "tca6416", 16, },
45 /* NYET: { "tca6424", 24, }, */
3760f736 46 { }
f5e8ff48 47};
3760f736 48MODULE_DEVICE_TABLE(i2c, pca953x_id);
9e60fdcf 49
f3dc3630 50struct pca953x_chip {
9e60fdcf 51 unsigned gpio_start;
52 uint16_t reg_output;
53 uint16_t reg_direction;
54
55 struct i2c_client *client;
1965d303 56 struct pca953x_platform_data *dyn_pdata;
9e60fdcf 57 struct gpio_chip gpio_chip;
77906a54 58 char **names;
9e60fdcf 59};
60
f3dc3630 61static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
9e60fdcf 62{
f5e8ff48
GL
63 int ret;
64
65 if (chip->gpio_chip.ngpio <= 8)
66 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
9e60fdcf 67 else
f5e8ff48
GL
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");
ab5dc372 72 return ret;
f5e8ff48
GL
73 }
74
75 return 0;
9e60fdcf 76}
77
f3dc3630 78static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
9e60fdcf 79{
80 int ret;
81
f5e8ff48
GL
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
9e60fdcf 87 if (ret < 0) {
88 dev_err(&chip->client->dev, "failed reading register\n");
ab5dc372 89 return ret;
9e60fdcf 90 }
91
92 *val = (uint16_t)ret;
93 return 0;
94}
95
f3dc3630 96static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
9e60fdcf 97{
f3dc3630 98 struct pca953x_chip *chip;
9e60fdcf 99 uint16_t reg_val;
100 int ret;
101
f3dc3630 102 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 103
104 reg_val = chip->reg_direction | (1u << off);
f3dc3630 105 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 106 if (ret)
107 return ret;
108
109 chip->reg_direction = reg_val;
110 return 0;
111}
112
f3dc3630 113static int pca953x_gpio_direction_output(struct gpio_chip *gc,
9e60fdcf 114 unsigned off, int val)
115{
f3dc3630 116 struct pca953x_chip *chip;
9e60fdcf 117 uint16_t reg_val;
118 int ret;
119
f3dc3630 120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 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
f3dc3630 128 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 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);
f3dc3630 136 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 137 if (ret)
138 return ret;
139
140 chip->reg_direction = reg_val;
141 return 0;
142}
143
f3dc3630 144static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
9e60fdcf 145{
f3dc3630 146 struct pca953x_chip *chip;
9e60fdcf 147 uint16_t reg_val;
148 int ret;
149
f3dc3630 150 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 151
f3dc3630 152 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
9e60fdcf 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
f3dc3630 164static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
9e60fdcf 165{
f3dc3630 166 struct pca953x_chip *chip;
9e60fdcf 167 uint16_t reg_val;
168 int ret;
169
f3dc3630 170 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 171
172 if (val)
173 reg_val = chip->reg_output | (1u << off);
174 else
175 reg_val = chip->reg_output & ~(1u << off);
176
f3dc3630 177 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 178 if (ret)
179 return;
180
181 chip->reg_output = reg_val;
182}
183
f5e8ff48 184static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
9e60fdcf 185{
186 struct gpio_chip *gc;
187
188 gc = &chip->gpio_chip;
189
f3dc3630
GL
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;
84207805 194 gc->can_sleep = 1;
9e60fdcf 195
196 gc->base = chip->gpio_start;
f5e8ff48
GL
197 gc->ngpio = gpios;
198 gc->label = chip->client->name;
d8f388d8 199 gc->dev = &chip->client->dev;
d72cbed0 200 gc->owner = THIS_MODULE;
77906a54 201 gc->names = chip->names;
9e60fdcf 202}
203
1965d303
NC
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 */
211static struct pca953x_platform_data *
212pca953x_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
245static struct pca953x_platform_data *
246pca953x_get_alt_pdata(struct i2c_client *client)
247{
248 return NULL;
249}
250#endif
251
d2653e92 252static int __devinit pca953x_probe(struct i2c_client *client,
3760f736 253 const struct i2c_device_id *id)
9e60fdcf 254{
f3dc3630
GL
255 struct pca953x_platform_data *pdata;
256 struct pca953x_chip *chip;
f39e5781 257 int ret;
9e60fdcf 258
1965d303
NC
259 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
260 if (chip == NULL)
261 return -ENOMEM;
262
9e60fdcf 263 pdata = client->dev.platform_data;
a342d215 264 if (pdata == NULL) {
1965d303
NC
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;
a342d215 271 }
9e60fdcf 272
1965d303
NC
273 if (pdata == NULL) {
274 dev_dbg(&client->dev, "no platform data\n");
275 ret = -EINVAL;
276 goto out_failed;
277 }
9e60fdcf 278
279 chip->client = client;
280
281 chip->gpio_start = pdata->gpio_base;
282
77906a54
DS
283 chip->names = pdata->names;
284
9e60fdcf 285 /* initialize cached registers from their original values.
286 * we can't share this chip with another i2c master.
287 */
f5e8ff48
GL
288 pca953x_setup_gpio(chip, id->driver_data);
289
f3dc3630 290 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
9e60fdcf 291 if (ret)
292 goto out_failed;
293
f3dc3630 294 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
9e60fdcf 295 if (ret)
296 goto out_failed;
297
298 /* set platform specific polarity inversion */
f3dc3630 299 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
9e60fdcf 300 if (ret)
301 goto out_failed;
302
f5e8ff48
GL
303
304 ret = gpiochip_add(&chip->gpio_chip);
9e60fdcf 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
318out_failed:
1965d303 319 kfree(chip->dyn_pdata);
9e60fdcf 320 kfree(chip);
321 return ret;
322}
323
f3dc3630 324static int pca953x_remove(struct i2c_client *client)
9e60fdcf 325{
f3dc3630
GL
326 struct pca953x_platform_data *pdata = client->dev.platform_data;
327 struct pca953x_chip *chip = i2c_get_clientdata(client);
9e60fdcf 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
1965d303 347 kfree(chip->dyn_pdata);
9e60fdcf 348 kfree(chip);
349 return 0;
350}
351
f3dc3630 352static struct i2c_driver pca953x_driver = {
9e60fdcf 353 .driver = {
f3dc3630 354 .name = "pca953x",
9e60fdcf 355 },
f3dc3630
GL
356 .probe = pca953x_probe,
357 .remove = pca953x_remove,
3760f736 358 .id_table = pca953x_id,
9e60fdcf 359};
360
f3dc3630 361static int __init pca953x_init(void)
9e60fdcf 362{
f3dc3630 363 return i2c_add_driver(&pca953x_driver);
9e60fdcf 364}
2f8d1197
DB
365/* register after i2c postcore initcall and before
366 * subsys initcalls that may rely on these GPIOs
367 */
368subsys_initcall(pca953x_init);
9e60fdcf 369
f3dc3630 370static void __exit pca953x_exit(void)
9e60fdcf 371{
f3dc3630 372 i2c_del_driver(&pca953x_driver);
9e60fdcf 373}
f3dc3630 374module_exit(pca953x_exit);
9e60fdcf 375
376MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
f3dc3630 377MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
9e60fdcf 378MODULE_LICENSE("GPL");