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