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