]>
Commit | Line | Data |
---|---|---|
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> |
9e60fdcf | 18 | |
19 | #include <asm/gpio.h> | |
20 | ||
f5e8ff48 GL |
21 | #define PCA953X_INPUT 0 |
22 | #define PCA953X_OUTPUT 1 | |
23 | #define PCA953X_INVERT 2 | |
24 | #define PCA953X_DIRECTION 3 | |
9e60fdcf | 25 | |
3760f736 | 26 | static const struct i2c_device_id pca953x_id[] = { |
f5e8ff48 GL |
27 | { "pca9534", 8, }, |
28 | { "pca9535", 16, }, | |
29 | { "pca9536", 4, }, | |
30 | { "pca9537", 4, }, | |
31 | { "pca9538", 8, }, | |
32 | { "pca9539", 16, }, | |
69292b34 | 33 | { "pca9554", 8, }, |
f39e5781 WN |
34 | { "pca9555", 16, }, |
35 | { "pca9557", 8, }, | |
ab5dc372 | 36 | |
7059d4b0 | 37 | { "max7310", 8, }, |
ab5dc372 DB |
38 | { "pca6107", 8, }, |
39 | { "tca6408", 8, }, | |
40 | { "tca6416", 16, }, | |
41 | /* NYET: { "tca6424", 24, }, */ | |
3760f736 | 42 | { } |
f5e8ff48 | 43 | }; |
3760f736 | 44 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
9e60fdcf | 45 | |
f3dc3630 | 46 | struct pca953x_chip { |
9e60fdcf | 47 | unsigned gpio_start; |
48 | uint16_t reg_output; | |
49 | uint16_t reg_direction; | |
50 | ||
51 | struct i2c_client *client; | |
52 | struct gpio_chip gpio_chip; | |
53 | }; | |
54 | ||
f3dc3630 | 55 | static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) |
9e60fdcf | 56 | { |
f5e8ff48 GL |
57 | int ret; |
58 | ||
59 | if (chip->gpio_chip.ngpio <= 8) | |
60 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); | |
9e60fdcf | 61 | else |
f5e8ff48 GL |
62 | ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); |
63 | ||
64 | if (ret < 0) { | |
65 | dev_err(&chip->client->dev, "failed writing register\n"); | |
ab5dc372 | 66 | return ret; |
f5e8ff48 GL |
67 | } |
68 | ||
69 | return 0; | |
9e60fdcf | 70 | } |
71 | ||
f3dc3630 | 72 | static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val) |
9e60fdcf | 73 | { |
74 | int ret; | |
75 | ||
f5e8ff48 GL |
76 | if (chip->gpio_chip.ngpio <= 8) |
77 | ret = i2c_smbus_read_byte_data(chip->client, reg); | |
78 | else | |
79 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); | |
80 | ||
9e60fdcf | 81 | if (ret < 0) { |
82 | dev_err(&chip->client->dev, "failed reading register\n"); | |
ab5dc372 | 83 | return ret; |
9e60fdcf | 84 | } |
85 | ||
86 | *val = (uint16_t)ret; | |
87 | return 0; | |
88 | } | |
89 | ||
f3dc3630 | 90 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
9e60fdcf | 91 | { |
f3dc3630 | 92 | struct pca953x_chip *chip; |
9e60fdcf | 93 | uint16_t reg_val; |
94 | int ret; | |
95 | ||
f3dc3630 | 96 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
9e60fdcf | 97 | |
98 | reg_val = chip->reg_direction | (1u << off); | |
f3dc3630 | 99 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
9e60fdcf | 100 | if (ret) |
101 | return ret; | |
102 | ||
103 | chip->reg_direction = reg_val; | |
104 | return 0; | |
105 | } | |
106 | ||
f3dc3630 | 107 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
9e60fdcf | 108 | unsigned off, int val) |
109 | { | |
f3dc3630 | 110 | struct pca953x_chip *chip; |
9e60fdcf | 111 | uint16_t reg_val; |
112 | int ret; | |
113 | ||
f3dc3630 | 114 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
9e60fdcf | 115 | |
116 | /* set output level */ | |
117 | if (val) | |
118 | reg_val = chip->reg_output | (1u << off); | |
119 | else | |
120 | reg_val = chip->reg_output & ~(1u << off); | |
121 | ||
f3dc3630 | 122 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
9e60fdcf | 123 | if (ret) |
124 | return ret; | |
125 | ||
126 | chip->reg_output = reg_val; | |
127 | ||
128 | /* then direction */ | |
129 | reg_val = chip->reg_direction & ~(1u << off); | |
f3dc3630 | 130 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
9e60fdcf | 131 | if (ret) |
132 | return ret; | |
133 | ||
134 | chip->reg_direction = reg_val; | |
135 | return 0; | |
136 | } | |
137 | ||
f3dc3630 | 138 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
9e60fdcf | 139 | { |
f3dc3630 | 140 | struct pca953x_chip *chip; |
9e60fdcf | 141 | uint16_t reg_val; |
142 | int ret; | |
143 | ||
f3dc3630 | 144 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
9e60fdcf | 145 | |
f3dc3630 | 146 | ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val); |
9e60fdcf | 147 | if (ret < 0) { |
148 | /* NOTE: diagnostic already emitted; that's all we should | |
149 | * do unless gpio_*_value_cansleep() calls become different | |
150 | * from their nonsleeping siblings (and report faults). | |
151 | */ | |
152 | return 0; | |
153 | } | |
154 | ||
155 | return (reg_val & (1u << off)) ? 1 : 0; | |
156 | } | |
157 | ||
f3dc3630 | 158 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) |
9e60fdcf | 159 | { |
f3dc3630 | 160 | struct pca953x_chip *chip; |
9e60fdcf | 161 | uint16_t reg_val; |
162 | int ret; | |
163 | ||
f3dc3630 | 164 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
9e60fdcf | 165 | |
166 | if (val) | |
167 | reg_val = chip->reg_output | (1u << off); | |
168 | else | |
169 | reg_val = chip->reg_output & ~(1u << off); | |
170 | ||
f3dc3630 | 171 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
9e60fdcf | 172 | if (ret) |
173 | return; | |
174 | ||
175 | chip->reg_output = reg_val; | |
176 | } | |
177 | ||
f5e8ff48 | 178 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
9e60fdcf | 179 | { |
180 | struct gpio_chip *gc; | |
181 | ||
182 | gc = &chip->gpio_chip; | |
183 | ||
f3dc3630 GL |
184 | gc->direction_input = pca953x_gpio_direction_input; |
185 | gc->direction_output = pca953x_gpio_direction_output; | |
186 | gc->get = pca953x_gpio_get_value; | |
187 | gc->set = pca953x_gpio_set_value; | |
84207805 | 188 | gc->can_sleep = 1; |
9e60fdcf | 189 | |
190 | gc->base = chip->gpio_start; | |
f5e8ff48 GL |
191 | gc->ngpio = gpios; |
192 | gc->label = chip->client->name; | |
d8f388d8 | 193 | gc->dev = &chip->client->dev; |
d72cbed0 | 194 | gc->owner = THIS_MODULE; |
9e60fdcf | 195 | } |
196 | ||
d2653e92 | 197 | static int __devinit pca953x_probe(struct i2c_client *client, |
3760f736 | 198 | const struct i2c_device_id *id) |
9e60fdcf | 199 | { |
f3dc3630 GL |
200 | struct pca953x_platform_data *pdata; |
201 | struct pca953x_chip *chip; | |
f39e5781 | 202 | int ret; |
9e60fdcf | 203 | |
204 | pdata = client->dev.platform_data; | |
a342d215 BD |
205 | if (pdata == NULL) { |
206 | dev_dbg(&client->dev, "no platform data\n"); | |
207 | return -EINVAL; | |
208 | } | |
9e60fdcf | 209 | |
f3dc3630 | 210 | chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); |
9e60fdcf | 211 | if (chip == NULL) |
212 | return -ENOMEM; | |
213 | ||
214 | chip->client = client; | |
215 | ||
216 | chip->gpio_start = pdata->gpio_base; | |
217 | ||
218 | /* initialize cached registers from their original values. | |
219 | * we can't share this chip with another i2c master. | |
220 | */ | |
f5e8ff48 GL |
221 | pca953x_setup_gpio(chip, id->driver_data); |
222 | ||
f3dc3630 | 223 | ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); |
9e60fdcf | 224 | if (ret) |
225 | goto out_failed; | |
226 | ||
f3dc3630 | 227 | ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction); |
9e60fdcf | 228 | if (ret) |
229 | goto out_failed; | |
230 | ||
231 | /* set platform specific polarity inversion */ | |
f3dc3630 | 232 | ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert); |
9e60fdcf | 233 | if (ret) |
234 | goto out_failed; | |
235 | ||
f5e8ff48 GL |
236 | |
237 | ret = gpiochip_add(&chip->gpio_chip); | |
9e60fdcf | 238 | if (ret) |
239 | goto out_failed; | |
240 | ||
241 | if (pdata->setup) { | |
242 | ret = pdata->setup(client, chip->gpio_chip.base, | |
243 | chip->gpio_chip.ngpio, pdata->context); | |
244 | if (ret < 0) | |
245 | dev_warn(&client->dev, "setup failed, %d\n", ret); | |
246 | } | |
247 | ||
248 | i2c_set_clientdata(client, chip); | |
249 | return 0; | |
250 | ||
251 | out_failed: | |
252 | kfree(chip); | |
253 | return ret; | |
254 | } | |
255 | ||
f3dc3630 | 256 | static int pca953x_remove(struct i2c_client *client) |
9e60fdcf | 257 | { |
f3dc3630 GL |
258 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
259 | struct pca953x_chip *chip = i2c_get_clientdata(client); | |
9e60fdcf | 260 | int ret = 0; |
261 | ||
262 | if (pdata->teardown) { | |
263 | ret = pdata->teardown(client, chip->gpio_chip.base, | |
264 | chip->gpio_chip.ngpio, pdata->context); | |
265 | if (ret < 0) { | |
266 | dev_err(&client->dev, "%s failed, %d\n", | |
267 | "teardown", ret); | |
268 | return ret; | |
269 | } | |
270 | } | |
271 | ||
272 | ret = gpiochip_remove(&chip->gpio_chip); | |
273 | if (ret) { | |
274 | dev_err(&client->dev, "%s failed, %d\n", | |
275 | "gpiochip_remove()", ret); | |
276 | return ret; | |
277 | } | |
278 | ||
279 | kfree(chip); | |
280 | return 0; | |
281 | } | |
282 | ||
f3dc3630 | 283 | static struct i2c_driver pca953x_driver = { |
9e60fdcf | 284 | .driver = { |
f3dc3630 | 285 | .name = "pca953x", |
9e60fdcf | 286 | }, |
f3dc3630 GL |
287 | .probe = pca953x_probe, |
288 | .remove = pca953x_remove, | |
3760f736 | 289 | .id_table = pca953x_id, |
9e60fdcf | 290 | }; |
291 | ||
f3dc3630 | 292 | static int __init pca953x_init(void) |
9e60fdcf | 293 | { |
f3dc3630 | 294 | return i2c_add_driver(&pca953x_driver); |
9e60fdcf | 295 | } |
2f8d1197 DB |
296 | /* register after i2c postcore initcall and before |
297 | * subsys initcalls that may rely on these GPIOs | |
298 | */ | |
299 | subsys_initcall(pca953x_init); | |
9e60fdcf | 300 | |
f3dc3630 | 301 | static void __exit pca953x_exit(void) |
9e60fdcf | 302 | { |
f3dc3630 | 303 | i2c_del_driver(&pca953x_driver); |
9e60fdcf | 304 | } |
f3dc3630 | 305 | module_exit(pca953x_exit); |
9e60fdcf | 306 | |
307 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); | |
f3dc3630 | 308 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
9e60fdcf | 309 | MODULE_LICENSE("GPL"); |