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