netfilter: nf_conntrack: remove unnecessary function declaration
[linux-2.6/libata-dev.git] / drivers / gpio / pca953x.c
blob93f916720b139e40b20591839126609db0e778a0
1 /*
2 * pca953x.c - 4/8/16 bit I/O ports
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
7 * Derived from drivers/i2c/chips/pca9539.c
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.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/i2c/pca953x.h>
19 #include <asm/gpio.h>
21 #define PCA953X_INPUT 0
22 #define PCA953X_OUTPUT 1
23 #define PCA953X_INVERT 2
24 #define PCA953X_DIRECTION 3
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 { }
38 MODULE_DEVICE_TABLE(i2c, pca953x_id);
40 struct pca953x_chip {
41 unsigned gpio_start;
42 uint16_t reg_output;
43 uint16_t reg_direction;
45 struct i2c_client *client;
46 struct gpio_chip gpio_chip;
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.
52 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
54 int ret;
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);
61 if (ret < 0) {
62 dev_err(&chip->client->dev, "failed writing register\n");
63 return -EIO;
66 return 0;
69 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
71 int ret;
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);
78 if (ret < 0) {
79 dev_err(&chip->client->dev, "failed reading register\n");
80 return -EIO;
83 *val = (uint16_t)ret;
84 return 0;
87 static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
89 struct pca953x_chip *chip;
90 uint16_t reg_val;
91 int ret;
93 chip = container_of(gc, struct pca953x_chip, gpio_chip);
95 reg_val = chip->reg_direction | (1u << off);
96 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
97 if (ret)
98 return ret;
100 chip->reg_direction = reg_val;
101 return 0;
104 static int pca953x_gpio_direction_output(struct gpio_chip *gc,
105 unsigned off, int val)
107 struct pca953x_chip *chip;
108 uint16_t reg_val;
109 int ret;
111 chip = container_of(gc, struct pca953x_chip, gpio_chip);
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);
119 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
120 if (ret)
121 return ret;
123 chip->reg_output = reg_val;
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;
131 chip->reg_direction = reg_val;
132 return 0;
135 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
137 struct pca953x_chip *chip;
138 uint16_t reg_val;
139 int ret;
141 chip = container_of(gc, struct pca953x_chip, gpio_chip);
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).
149 return 0;
152 return (reg_val & (1u << off)) ? 1 : 0;
155 static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
157 struct pca953x_chip *chip;
158 uint16_t reg_val;
159 int ret;
161 chip = container_of(gc, struct pca953x_chip, gpio_chip);
163 if (val)
164 reg_val = chip->reg_output | (1u << off);
165 else
166 reg_val = chip->reg_output & ~(1u << off);
168 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
169 if (ret)
170 return;
172 chip->reg_output = reg_val;
175 static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
177 struct gpio_chip *gc;
179 gc = &chip->gpio_chip;
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;
187 gc->base = chip->gpio_start;
188 gc->ngpio = gpios;
189 gc->label = chip->client->name;
190 gc->owner = THIS_MODULE;
193 static int __devinit pca953x_probe(struct i2c_client *client,
194 const struct i2c_device_id *id)
196 struct pca953x_platform_data *pdata;
197 struct pca953x_chip *chip;
198 int ret;
200 pdata = client->dev.platform_data;
201 if (pdata == NULL)
202 return -ENODEV;
204 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
205 if (chip == NULL)
206 return -ENOMEM;
208 chip->client = client;
210 chip->gpio_start = pdata->gpio_base;
212 /* initialize cached registers from their original values.
213 * we can't share this chip with another i2c master.
215 pca953x_setup_gpio(chip, id->driver_data);
217 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
218 if (ret)
219 goto out_failed;
221 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
222 if (ret)
223 goto out_failed;
225 /* set platform specific polarity inversion */
226 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
227 if (ret)
228 goto out_failed;
231 ret = gpiochip_add(&chip->gpio_chip);
232 if (ret)
233 goto out_failed;
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);
242 i2c_set_clientdata(client, chip);
243 return 0;
245 out_failed:
246 kfree(chip);
247 return ret;
250 static int pca953x_remove(struct i2c_client *client)
252 struct pca953x_platform_data *pdata = client->dev.platform_data;
253 struct pca953x_chip *chip = i2c_get_clientdata(client);
254 int ret = 0;
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;
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;
273 kfree(chip);
274 return 0;
277 static struct i2c_driver pca953x_driver = {
278 .driver = {
279 .name = "pca953x",
281 .probe = pca953x_probe,
282 .remove = pca953x_remove,
283 .id_table = pca953x_id,
286 static int __init pca953x_init(void)
288 return i2c_add_driver(&pca953x_driver);
290 module_init(pca953x_init);
292 static void __exit pca953x_exit(void)
294 i2c_del_driver(&pca953x_driver);
296 module_exit(pca953x_exit);
298 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
299 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
300 MODULE_LICENSE("GPL");