diff options
Diffstat (limited to 'drivers/gpio/adp5588-gpio.c')
-rw-r--r-- | drivers/gpio/adp5588-gpio.c | 278 |
1 files changed, 257 insertions, 21 deletions
diff --git a/drivers/gpio/adp5588-gpio.c b/drivers/gpio/adp5588-gpio.c index 2e8e9e24f887..3525ad918771 100644 --- a/drivers/gpio/adp5588-gpio.c +++ b/drivers/gpio/adp5588-gpio.c | |||
@@ -1,8 +1,8 @@ | |||
1 | /* | 1 | /* |
2 | * GPIO Chip driver for Analog Devices | 2 | * GPIO Chip driver for Analog Devices |
3 | * ADP5588 I/O Expander and QWERTY Keypad Controller | 3 | * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller |
4 | * | 4 | * |
5 | * Copyright 2009 Analog Devices Inc. | 5 | * Copyright 2009-2010 Analog Devices Inc. |
6 | * | 6 | * |
7 | * Licensed under the GPL-2 or later. | 7 | * Licensed under the GPL-2 or later. |
8 | */ | 8 | */ |
@@ -13,21 +13,34 @@ | |||
13 | #include <linux/init.h> | 13 | #include <linux/init.h> |
14 | #include <linux/i2c.h> | 14 | #include <linux/i2c.h> |
15 | #include <linux/gpio.h> | 15 | #include <linux/gpio.h> |
16 | #include <linux/interrupt.h> | ||
17 | #include <linux/irq.h> | ||
16 | 18 | ||
17 | #include <linux/i2c/adp5588.h> | 19 | #include <linux/i2c/adp5588.h> |
18 | 20 | ||
19 | #define DRV_NAME "adp5588-gpio" | 21 | #define DRV_NAME "adp5588-gpio" |
20 | #define MAXGPIO 18 | 22 | |
21 | #define ADP_BANK(offs) ((offs) >> 3) | 23 | /* |
22 | #define ADP_BIT(offs) (1u << ((offs) & 0x7)) | 24 | * Early pre 4.0 Silicon required to delay readout by at least 25ms, |
25 | * since the Event Counter Register updated 25ms after the interrupt | ||
26 | * asserted. | ||
27 | */ | ||
28 | #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4) | ||
23 | 29 | ||
24 | struct adp5588_gpio { | 30 | struct adp5588_gpio { |
25 | struct i2c_client *client; | 31 | struct i2c_client *client; |
26 | struct gpio_chip gpio_chip; | 32 | struct gpio_chip gpio_chip; |
27 | struct mutex lock; /* protect cached dir, dat_out */ | 33 | struct mutex lock; /* protect cached dir, dat_out */ |
34 | /* protect serialized access to the interrupt controller bus */ | ||
35 | struct mutex irq_lock; | ||
28 | unsigned gpio_start; | 36 | unsigned gpio_start; |
37 | unsigned irq_base; | ||
29 | uint8_t dat_out[3]; | 38 | uint8_t dat_out[3]; |
30 | uint8_t dir[3]; | 39 | uint8_t dir[3]; |
40 | uint8_t int_lvl[3]; | ||
41 | uint8_t int_en[3]; | ||
42 | uint8_t irq_mask[3]; | ||
43 | uint8_t irq_stat[3]; | ||
31 | }; | 44 | }; |
32 | 45 | ||
33 | static int adp5588_gpio_read(struct i2c_client *client, u8 reg) | 46 | static int adp5588_gpio_read(struct i2c_client *client, u8 reg) |
@@ -55,8 +68,8 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) | |||
55 | struct adp5588_gpio *dev = | 68 | struct adp5588_gpio *dev = |
56 | container_of(chip, struct adp5588_gpio, gpio_chip); | 69 | container_of(chip, struct adp5588_gpio, gpio_chip); |
57 | 70 | ||
58 | return !!(adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + ADP_BANK(off)) | 71 | return !!(adp5588_gpio_read(dev->client, |
59 | & ADP_BIT(off)); | 72 | GPIO_DAT_STAT1 + ADP5588_BANK(off)) & ADP5588_BIT(off)); |
60 | } | 73 | } |
61 | 74 | ||
62 | static void adp5588_gpio_set_value(struct gpio_chip *chip, | 75 | static void adp5588_gpio_set_value(struct gpio_chip *chip, |
@@ -66,8 +79,8 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, | |||
66 | struct adp5588_gpio *dev = | 79 | struct adp5588_gpio *dev = |
67 | container_of(chip, struct adp5588_gpio, gpio_chip); | 80 | container_of(chip, struct adp5588_gpio, gpio_chip); |
68 | 81 | ||
69 | bank = ADP_BANK(off); | 82 | bank = ADP5588_BANK(off); |
70 | bit = ADP_BIT(off); | 83 | bit = ADP5588_BIT(off); |
71 | 84 | ||
72 | mutex_lock(&dev->lock); | 85 | mutex_lock(&dev->lock); |
73 | if (val) | 86 | if (val) |
@@ -87,10 +100,10 @@ static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) | |||
87 | struct adp5588_gpio *dev = | 100 | struct adp5588_gpio *dev = |
88 | container_of(chip, struct adp5588_gpio, gpio_chip); | 101 | container_of(chip, struct adp5588_gpio, gpio_chip); |
89 | 102 | ||
90 | bank = ADP_BANK(off); | 103 | bank = ADP5588_BANK(off); |
91 | 104 | ||
92 | mutex_lock(&dev->lock); | 105 | mutex_lock(&dev->lock); |
93 | dev->dir[bank] &= ~ADP_BIT(off); | 106 | dev->dir[bank] &= ~ADP5588_BIT(off); |
94 | ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]); | 107 | ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]); |
95 | mutex_unlock(&dev->lock); | 108 | mutex_unlock(&dev->lock); |
96 | 109 | ||
@@ -105,8 +118,8 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, | |||
105 | struct adp5588_gpio *dev = | 118 | struct adp5588_gpio *dev = |
106 | container_of(chip, struct adp5588_gpio, gpio_chip); | 119 | container_of(chip, struct adp5588_gpio, gpio_chip); |
107 | 120 | ||
108 | bank = ADP_BANK(off); | 121 | bank = ADP5588_BANK(off); |
109 | bit = ADP_BIT(off); | 122 | bit = ADP5588_BIT(off); |
110 | 123 | ||
111 | mutex_lock(&dev->lock); | 124 | mutex_lock(&dev->lock); |
112 | dev->dir[bank] |= bit; | 125 | dev->dir[bank] |= bit; |
@@ -125,6 +138,214 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, | |||
125 | return ret; | 138 | return ret; |
126 | } | 139 | } |
127 | 140 | ||
141 | #ifdef CONFIG_GPIO_ADP5588_IRQ | ||
142 | static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off) | ||
143 | { | ||
144 | struct adp5588_gpio *dev = | ||
145 | container_of(chip, struct adp5588_gpio, gpio_chip); | ||
146 | return dev->irq_base + off; | ||
147 | } | ||
148 | |||
149 | static void adp5588_irq_bus_lock(struct irq_data *d) | ||
150 | { | ||
151 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); | ||
152 | |||
153 | mutex_lock(&dev->irq_lock); | ||
154 | } | ||
155 | |||
156 | /* | ||
157 | * genirq core code can issue chip->mask/unmask from atomic context. | ||
158 | * This doesn't work for slow busses where an access needs to sleep. | ||
159 | * bus_sync_unlock() is therefore called outside the atomic context, | ||
160 | * syncs the current irq mask state with the slow external controller | ||
161 | * and unlocks the bus. | ||
162 | */ | ||
163 | |||
164 | static void adp5588_irq_bus_sync_unlock(struct irq_data *d) | ||
165 | { | ||
166 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); | ||
167 | int i; | ||
168 | |||
169 | for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) | ||
170 | if (dev->int_en[i] ^ dev->irq_mask[i]) { | ||
171 | dev->int_en[i] = dev->irq_mask[i]; | ||
172 | adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i, | ||
173 | dev->int_en[i]); | ||
174 | } | ||
175 | |||
176 | mutex_unlock(&dev->irq_lock); | ||
177 | } | ||
178 | |||
179 | static void adp5588_irq_mask(struct irq_data *d) | ||
180 | { | ||
181 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); | ||
182 | unsigned gpio = d->irq - dev->irq_base; | ||
183 | |||
184 | dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio); | ||
185 | } | ||
186 | |||
187 | static void adp5588_irq_unmask(struct irq_data *d) | ||
188 | { | ||
189 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); | ||
190 | unsigned gpio = d->irq - dev->irq_base; | ||
191 | |||
192 | dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio); | ||
193 | } | ||
194 | |||
195 | static int adp5588_irq_set_type(struct irq_data *d, unsigned int type) | ||
196 | { | ||
197 | struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d); | ||
198 | uint16_t gpio = d->irq - dev->irq_base; | ||
199 | unsigned bank, bit; | ||
200 | |||
201 | if ((type & IRQ_TYPE_EDGE_BOTH)) { | ||
202 | dev_err(&dev->client->dev, "irq %d: unsupported type %d\n", | ||
203 | d->irq, type); | ||
204 | return -EINVAL; | ||
205 | } | ||
206 | |||
207 | bank = ADP5588_BANK(gpio); | ||
208 | bit = ADP5588_BIT(gpio); | ||
209 | |||
210 | if (type & IRQ_TYPE_LEVEL_HIGH) | ||
211 | dev->int_lvl[bank] |= bit; | ||
212 | else if (type & IRQ_TYPE_LEVEL_LOW) | ||
213 | dev->int_lvl[bank] &= ~bit; | ||
214 | else | ||
215 | return -EINVAL; | ||
216 | |||
217 | adp5588_gpio_direction_input(&dev->gpio_chip, gpio); | ||
218 | adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank, | ||
219 | dev->int_lvl[bank]); | ||
220 | |||
221 | return 0; | ||
222 | } | ||
223 | |||
224 | static struct irq_chip adp5588_irq_chip = { | ||
225 | .name = "adp5588", | ||
226 | .irq_mask = adp5588_irq_mask, | ||
227 | .irq_unmask = adp5588_irq_unmask, | ||
228 | .irq_bus_lock = adp5588_irq_bus_lock, | ||
229 | .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock, | ||
230 | .irq_set_type = adp5588_irq_set_type, | ||
231 | }; | ||
232 | |||
233 | static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf) | ||
234 | { | ||
235 | int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf); | ||
236 | |||
237 | if (ret < 0) | ||
238 | dev_err(&client->dev, "Read INT_STAT Error\n"); | ||
239 | |||
240 | return ret; | ||
241 | } | ||
242 | |||
243 | static irqreturn_t adp5588_irq_handler(int irq, void *devid) | ||
244 | { | ||
245 | struct adp5588_gpio *dev = devid; | ||
246 | unsigned status, bank, bit, pending; | ||
247 | int ret; | ||
248 | status = adp5588_gpio_read(dev->client, INT_STAT); | ||
249 | |||
250 | if (status & ADP5588_GPI_INT) { | ||
251 | ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat); | ||
252 | if (ret < 0) | ||
253 | memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat)); | ||
254 | |||
255 | for (bank = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO); | ||
256 | bank++, bit = 0) { | ||
257 | pending = dev->irq_stat[bank] & dev->irq_mask[bank]; | ||
258 | |||
259 | while (pending) { | ||
260 | if (pending & (1 << bit)) { | ||
261 | handle_nested_irq(dev->irq_base + | ||
262 | (bank << 3) + bit); | ||
263 | pending &= ~(1 << bit); | ||
264 | |||
265 | } | ||
266 | bit++; | ||
267 | } | ||
268 | } | ||
269 | } | ||
270 | |||
271 | adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */ | ||
272 | |||
273 | return IRQ_HANDLED; | ||
274 | } | ||
275 | |||
276 | static int adp5588_irq_setup(struct adp5588_gpio *dev) | ||
277 | { | ||
278 | struct i2c_client *client = dev->client; | ||
279 | struct adp5588_gpio_platform_data *pdata = client->dev.platform_data; | ||
280 | unsigned gpio; | ||
281 | int ret; | ||
282 | |||
283 | adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC); | ||
284 | adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */ | ||
285 | adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */ | ||
286 | |||
287 | dev->irq_base = pdata->irq_base; | ||
288 | mutex_init(&dev->irq_lock); | ||
289 | |||
290 | for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) { | ||
291 | int irq = gpio + dev->irq_base; | ||
292 | irq_set_chip_data(irq, dev); | ||
293 | irq_set_chip_and_handler(irq, &adp5588_irq_chip, | ||
294 | handle_level_irq); | ||
295 | irq_set_nested_thread(irq, 1); | ||
296 | #ifdef CONFIG_ARM | ||
297 | /* | ||
298 | * ARM needs us to explicitly flag the IRQ as VALID, | ||
299 | * once we do so, it will also set the noprobe. | ||
300 | */ | ||
301 | set_irq_flags(irq, IRQF_VALID); | ||
302 | #else | ||
303 | irq_set_noprobe(irq); | ||
304 | #endif | ||
305 | } | ||
306 | |||
307 | ret = request_threaded_irq(client->irq, | ||
308 | NULL, | ||
309 | adp5588_irq_handler, | ||
310 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, | ||
311 | dev_name(&client->dev), dev); | ||
312 | if (ret) { | ||
313 | dev_err(&client->dev, "failed to request irq %d\n", | ||
314 | client->irq); | ||
315 | goto out; | ||
316 | } | ||
317 | |||
318 | dev->gpio_chip.to_irq = adp5588_gpio_to_irq; | ||
319 | adp5588_gpio_write(client, CFG, | ||
320 | ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT); | ||
321 | |||
322 | return 0; | ||
323 | |||
324 | out: | ||
325 | dev->irq_base = 0; | ||
326 | return ret; | ||
327 | } | ||
328 | |||
329 | static void adp5588_irq_teardown(struct adp5588_gpio *dev) | ||
330 | { | ||
331 | if (dev->irq_base) | ||
332 | free_irq(dev->client->irq, dev); | ||
333 | } | ||
334 | |||
335 | #else | ||
336 | static int adp5588_irq_setup(struct adp5588_gpio *dev) | ||
337 | { | ||
338 | struct i2c_client *client = dev->client; | ||
339 | dev_warn(&client->dev, "interrupt support not compiled in\n"); | ||
340 | |||
341 | return 0; | ||
342 | } | ||
343 | |||
344 | static void adp5588_irq_teardown(struct adp5588_gpio *dev) | ||
345 | { | ||
346 | } | ||
347 | #endif /* CONFIG_GPIO_ADP5588_IRQ */ | ||
348 | |||
128 | static int __devinit adp5588_gpio_probe(struct i2c_client *client, | 349 | static int __devinit adp5588_gpio_probe(struct i2c_client *client, |
129 | const struct i2c_device_id *id) | 350 | const struct i2c_device_id *id) |
130 | { | 351 | { |
@@ -160,37 +381,46 @@ static int __devinit adp5588_gpio_probe(struct i2c_client *client, | |||
160 | gc->can_sleep = 1; | 381 | gc->can_sleep = 1; |
161 | 382 | ||
162 | gc->base = pdata->gpio_start; | 383 | gc->base = pdata->gpio_start; |
163 | gc->ngpio = MAXGPIO; | 384 | gc->ngpio = ADP5588_MAXGPIO; |
164 | gc->label = client->name; | 385 | gc->label = client->name; |
165 | gc->owner = THIS_MODULE; | 386 | gc->owner = THIS_MODULE; |
166 | 387 | ||
167 | mutex_init(&dev->lock); | 388 | mutex_init(&dev->lock); |
168 | 389 | ||
169 | |||
170 | ret = adp5588_gpio_read(dev->client, DEV_ID); | 390 | ret = adp5588_gpio_read(dev->client, DEV_ID); |
171 | if (ret < 0) | 391 | if (ret < 0) |
172 | goto err; | 392 | goto err; |
173 | 393 | ||
174 | revid = ret & ADP5588_DEVICE_ID_MASK; | 394 | revid = ret & ADP5588_DEVICE_ID_MASK; |
175 | 395 | ||
176 | for (i = 0, ret = 0; i <= ADP_BANK(MAXGPIO); i++) { | 396 | for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
177 | dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i); | 397 | dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i); |
178 | dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i); | 398 | dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i); |
179 | ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0); | 399 | ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0); |
180 | ret |= adp5588_gpio_write(client, GPIO_PULL1 + i, | 400 | ret |= adp5588_gpio_write(client, GPIO_PULL1 + i, |
181 | (pdata->pullup_dis_mask >> (8 * i)) & 0xFF); | 401 | (pdata->pullup_dis_mask >> (8 * i)) & 0xFF); |
182 | 402 | ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0); | |
183 | if (ret) | 403 | if (ret) |
184 | goto err; | 404 | goto err; |
185 | } | 405 | } |
186 | 406 | ||
407 | if (pdata->irq_base) { | ||
408 | if (WA_DELAYED_READOUT_REVID(revid)) { | ||
409 | dev_warn(&client->dev, "GPIO int not supported\n"); | ||
410 | } else { | ||
411 | ret = adp5588_irq_setup(dev); | ||
412 | if (ret) | ||
413 | goto err; | ||
414 | } | ||
415 | } | ||
416 | |||
187 | ret = gpiochip_add(&dev->gpio_chip); | 417 | ret = gpiochip_add(&dev->gpio_chip); |
188 | if (ret) | 418 | if (ret) |
189 | goto err; | 419 | goto err_irq; |
190 | 420 | ||
191 | dev_info(&client->dev, "gpios %d..%d on a %s Rev. %d\n", | 421 | dev_info(&client->dev, "gpios %d..%d (IRQ Base %d) on a %s Rev. %d\n", |
192 | gc->base, gc->base + gc->ngpio - 1, | 422 | gc->base, gc->base + gc->ngpio - 1, |
193 | client->name, revid); | 423 | pdata->irq_base, client->name, revid); |
194 | 424 | ||
195 | if (pdata->setup) { | 425 | if (pdata->setup) { |
196 | ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context); | 426 | ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context); |
@@ -199,8 +429,11 @@ static int __devinit adp5588_gpio_probe(struct i2c_client *client, | |||
199 | } | 429 | } |
200 | 430 | ||
201 | i2c_set_clientdata(client, dev); | 431 | i2c_set_clientdata(client, dev); |
432 | |||
202 | return 0; | 433 | return 0; |
203 | 434 | ||
435 | err_irq: | ||
436 | adp5588_irq_teardown(dev); | ||
204 | err: | 437 | err: |
205 | kfree(dev); | 438 | kfree(dev); |
206 | return ret; | 439 | return ret; |
@@ -222,6 +455,9 @@ static int __devexit adp5588_gpio_remove(struct i2c_client *client) | |||
222 | } | 455 | } |
223 | } | 456 | } |
224 | 457 | ||
458 | if (dev->irq_base) | ||
459 | free_irq(dev->client->irq, dev); | ||
460 | |||
225 | ret = gpiochip_remove(&dev->gpio_chip); | 461 | ret = gpiochip_remove(&dev->gpio_chip); |
226 | if (ret) { | 462 | if (ret) { |
227 | dev_err(&client->dev, "gpiochip_remove failed %d\n", ret); | 463 | dev_err(&client->dev, "gpiochip_remove failed %d\n", ret); |