aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/max732x.c
diff options
context:
space:
mode:
authorMarc Zyngier <maz@misterjones.org>2010-05-26 17:42:16 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-05-27 12:12:41 -0400
commita80a0bbee49872c296c9ed9d6af0f510fcd825a7 (patch)
treee617afb270575fbd3c05f534444fd8895fc8a0b9 /drivers/gpio/max732x.c
parent0af62f4d1eedaacf6a85e293958699540d09fa3e (diff)
gpio: add interrupt handling capability to max732x
Most of the GPIO expanders supported by the max732x driver have interrupt generation capability by reporting changes on input pins through an INT# pin. This patch implements the irq_chip functionnality (edge detection only). Signed-off-by: Marc Zyngier <maz@misterjones.org> Cc: Eric Miao <eric.y.miao@gmail.com> Cc: Jebediah Huang <jebediah.huang@gmail.com> Cc: David Brownell <david-b@pacbell.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/gpio/max732x.c')
-rw-r--r--drivers/gpio/max732x.c355
1 files changed, 336 insertions, 19 deletions
diff --git a/drivers/gpio/max732x.c b/drivers/gpio/max732x.c
index f7868243af89..2053ba9cbfae 100644
--- a/drivers/gpio/max732x.c
+++ b/drivers/gpio/max732x.c
@@ -17,7 +17,8 @@
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <linux/string.h> 18#include <linux/string.h>
19#include <linux/gpio.h> 19#include <linux/gpio.h>
20 20#include <linux/interrupt.h>
21#include <linux/irq.h>
21#include <linux/i2c.h> 22#include <linux/i2c.h>
22#include <linux/i2c/max732x.h> 23#include <linux/i2c/max732x.h>
23 24
@@ -31,7 +32,8 @@
31 * - Open Drain I/O 32 * - Open Drain I/O
32 * 33 *
33 * designated by 'O', 'I' and 'P' individually according to MAXIM's 34 * designated by 'O', 'I' and 'P' individually according to MAXIM's
34 * datasheets. 35 * datasheets. 'I' and 'P' ports are interrupt capables, some with
36 * a dedicated interrupt mask.
35 * 37 *
36 * There are two groups of I/O ports, each group usually includes 38 * There are two groups of I/O ports, each group usually includes
37 * up to 8 I/O ports, and is accessed by a specific I2C address: 39 * up to 8 I/O ports, and is accessed by a specific I2C address:
@@ -44,7 +46,8 @@
44 * 46 *
45 * Within each group of ports, there are five known combinations of 47 * Within each group of ports, there are five known combinations of
46 * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for 48 * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
47 * the detailed organization of these ports. 49 * the detailed organization of these ports. Only Goup A is interrupt
50 * capable.
48 * 51 *
49 * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16', 52 * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
50 * and GPIOs from GROUP_A are numbered before those from GROUP_B 53 * and GPIOs from GROUP_A are numbered before those from GROUP_B
@@ -68,16 +71,47 @@
68#define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */ 71#define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
69#define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */ 72#define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
70 73
74#define INT_NONE 0x0 /* No interrupt capability */
75#define INT_NO_MASK 0x1 /* Has interrupts, no mask */
76#define INT_INDEP_MASK 0x2 /* Has interrupts, independent mask */
77#define INT_MERGED_MASK 0x3 /* Has interrupts, merged mask */
78
79#define INT_CAPS(x) (((uint64_t)(x)) << 32)
80
81enum {
82 MAX7319,
83 MAX7320,
84 MAX7321,
85 MAX7322,
86 MAX7323,
87 MAX7324,
88 MAX7325,
89 MAX7326,
90 MAX7327,
91};
92
93static uint64_t max732x_features[] = {
94 [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
95 [MAX7320] = GROUP_B(IO_8O),
96 [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
97 [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
98 [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
99 [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
100 [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
101 [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
102 [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
103};
104
71static const struct i2c_device_id max732x_id[] = { 105static const struct i2c_device_id max732x_id[] = {
72 { "max7319", GROUP_A(IO_8I) }, 106 { "max7319", MAX7319 },
73 { "max7320", GROUP_B(IO_8O) }, 107 { "max7320", MAX7320 },
74 { "max7321", GROUP_A(IO_8P) }, 108 { "max7321", MAX7321 },
75 { "max7322", GROUP_A(IO_4I4O) }, 109 { "max7322", MAX7322 },
76 { "max7323", GROUP_A(IO_4P4O) }, 110 { "max7323", MAX7323 },
77 { "max7324", GROUP_A(IO_8I) | GROUP_B(IO_8O) }, 111 { "max7324", MAX7324 },
78 { "max7325", GROUP_A(IO_8P) | GROUP_B(IO_8O) }, 112 { "max7325", MAX7325 },
79 { "max7326", GROUP_A(IO_4I4O) | GROUP_B(IO_8O) }, 113 { "max7326", MAX7326 },
80 { "max7327", GROUP_A(IO_4P4O) | GROUP_B(IO_8O) }, 114 { "max7327", MAX7327 },
81 { }, 115 { },
82}; 116};
83MODULE_DEVICE_TABLE(i2c, max732x_id); 117MODULE_DEVICE_TABLE(i2c, max732x_id);
@@ -96,9 +130,19 @@ struct max732x_chip {
96 130
97 struct mutex lock; 131 struct mutex lock;
98 uint8_t reg_out[2]; 132 uint8_t reg_out[2];
133
134#ifdef CONFIG_GPIO_MAX732X_IRQ
135 struct mutex irq_lock;
136 int irq_base;
137 uint8_t irq_mask;
138 uint8_t irq_mask_cur;
139 uint8_t irq_trig_raise;
140 uint8_t irq_trig_fall;
141 uint8_t irq_features;
142#endif
99}; 143};
100 144
101static int max732x_write(struct max732x_chip *chip, int group_a, uint8_t val) 145static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
102{ 146{
103 struct i2c_client *client; 147 struct i2c_client *client;
104 int ret; 148 int ret;
@@ -113,7 +157,7 @@ static int max732x_write(struct max732x_chip *chip, int group_a, uint8_t val)
113 return 0; 157 return 0;
114} 158}
115 159
116static int max732x_read(struct max732x_chip *chip, int group_a, uint8_t *val) 160static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
117{ 161{
118 struct i2c_client *client; 162 struct i2c_client *client;
119 int ret; 163 int ret;
@@ -142,7 +186,7 @@ static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
142 186
143 chip = container_of(gc, struct max732x_chip, gpio_chip); 187 chip = container_of(gc, struct max732x_chip, gpio_chip);
144 188
145 ret = max732x_read(chip, is_group_a(chip, off), &reg_val); 189 ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
146 if (ret < 0) 190 if (ret < 0)
147 return 0; 191 return 0;
148 192
@@ -162,7 +206,7 @@ static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
162 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0]; 206 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
163 reg_out = (val) ? reg_out | mask : reg_out & ~mask; 207 reg_out = (val) ? reg_out | mask : reg_out & ~mask;
164 208
165 ret = max732x_write(chip, is_group_a(chip, off), reg_out); 209 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
166 if (ret < 0) 210 if (ret < 0)
167 goto out; 211 goto out;
168 212
@@ -209,12 +253,278 @@ static int max732x_gpio_direction_output(struct gpio_chip *gc,
209 return 0; 253 return 0;
210} 254}
211 255
256#ifdef CONFIG_GPIO_MAX732X_IRQ
257static int max732x_writew(struct max732x_chip *chip, uint16_t val)
258{
259 int ret;
260
261 val = cpu_to_le16(val);
262
263 ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
264 if (ret < 0) {
265 dev_err(&chip->client_group_a->dev, "failed writing\n");
266 return ret;
267 }
268
269 return 0;
270}
271
272static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
273{
274 int ret;
275
276 ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
277 if (ret < 0) {
278 dev_err(&chip->client_group_a->dev, "failed reading\n");
279 return ret;
280 }
281
282 *val = le16_to_cpu(*val);
283 return 0;
284}
285
286static void max732x_irq_update_mask(struct max732x_chip *chip)
287{
288 uint16_t msg;
289
290 if (chip->irq_mask == chip->irq_mask_cur)
291 return;
292
293 chip->irq_mask = chip->irq_mask_cur;
294
295 if (chip->irq_features == INT_NO_MASK)
296 return;
297
298 mutex_lock(&chip->lock);
299
300 switch (chip->irq_features) {
301 case INT_INDEP_MASK:
302 msg = (chip->irq_mask << 8) | chip->reg_out[0];
303 max732x_writew(chip, msg);
304 break;
305
306 case INT_MERGED_MASK:
307 msg = chip->irq_mask | chip->reg_out[0];
308 max732x_writeb(chip, 1, (uint8_t)msg);
309 break;
310 }
311
312 mutex_unlock(&chip->lock);
313}
314
315static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
316{
317 struct max732x_chip *chip;
318
319 chip = container_of(gc, struct max732x_chip, gpio_chip);
320 return chip->irq_base + off;
321}
322
323static void max732x_irq_mask(unsigned int irq)
324{
325 struct max732x_chip *chip = get_irq_chip_data(irq);
326
327 chip->irq_mask_cur &= ~(1 << (irq - chip->irq_base));
328}
329
330static void max732x_irq_unmask(unsigned int irq)
331{
332 struct max732x_chip *chip = get_irq_chip_data(irq);
333
334 chip->irq_mask_cur |= 1 << (irq - chip->irq_base);
335}
336
337static void max732x_irq_bus_lock(unsigned int irq)
338{
339 struct max732x_chip *chip = get_irq_chip_data(irq);
340
341 mutex_lock(&chip->irq_lock);
342 chip->irq_mask_cur = chip->irq_mask;
343}
344
345static void max732x_irq_bus_sync_unlock(unsigned int irq)
346{
347 struct max732x_chip *chip = get_irq_chip_data(irq);
348
349 max732x_irq_update_mask(chip);
350 mutex_unlock(&chip->irq_lock);
351}
352
353static int max732x_irq_set_type(unsigned int irq, unsigned int type)
354{
355 struct max732x_chip *chip = get_irq_chip_data(irq);
356 uint16_t off = irq - chip->irq_base;
357 uint16_t mask = 1 << off;
358
359 if (!(mask & chip->dir_input)) {
360 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
361 chip->client->name, off);
362 return -EACCES;
363 }
364
365 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
366 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
367 irq, type);
368 return -EINVAL;
369 }
370
371 if (type & IRQ_TYPE_EDGE_FALLING)
372 chip->irq_trig_fall |= mask;
373 else
374 chip->irq_trig_fall &= ~mask;
375
376 if (type & IRQ_TYPE_EDGE_RISING)
377 chip->irq_trig_raise |= mask;
378 else
379 chip->irq_trig_raise &= ~mask;
380
381 return max732x_gpio_direction_input(&chip->gpio_chip, off);
382}
383
384static struct irq_chip max732x_irq_chip = {
385 .name = "max732x",
386 .mask = max732x_irq_mask,
387 .unmask = max732x_irq_unmask,
388 .bus_lock = max732x_irq_bus_lock,
389 .bus_sync_unlock = max732x_irq_bus_sync_unlock,
390 .set_type = max732x_irq_set_type,
391};
392
393static uint8_t max732x_irq_pending(struct max732x_chip *chip)
394{
395 uint8_t cur_stat;
396 uint8_t old_stat;
397 uint8_t trigger;
398 uint8_t pending;
399 uint16_t status;
400 int ret;
401
402 ret = max732x_readw(chip, &status);
403 if (ret)
404 return 0;
405
406 trigger = status >> 8;
407 trigger &= chip->irq_mask;
408
409 if (!trigger)
410 return 0;
411
412 cur_stat = status & 0xFF;
413 cur_stat &= chip->irq_mask;
414
415 old_stat = cur_stat ^ trigger;
416
417 pending = (old_stat & chip->irq_trig_fall) |
418 (cur_stat & chip->irq_trig_raise);
419 pending &= trigger;
420
421 return pending;
422}
423
424static irqreturn_t max732x_irq_handler(int irq, void *devid)
425{
426 struct max732x_chip *chip = devid;
427 uint8_t pending;
428 uint8_t level;
429
430 pending = max732x_irq_pending(chip);
431
432 if (!pending)
433 return IRQ_HANDLED;
434
435 do {
436 level = __ffs(pending);
437 handle_nested_irq(level + chip->irq_base);
438
439 pending &= ~(1 << level);
440 } while (pending);
441
442 return IRQ_HANDLED;
443}
444
445static int max732x_irq_setup(struct max732x_chip *chip,
446 const struct i2c_device_id *id)
447{
448 struct i2c_client *client = chip->client;
449 struct max732x_platform_data *pdata = client->dev.platform_data;
450 int has_irq = max732x_features[id->driver_data] >> 32;
451 int ret;
452
453 if (pdata->irq_base && has_irq != INT_NONE) {
454 int lvl;
455
456 chip->irq_base = pdata->irq_base;
457 chip->irq_features = has_irq;
458 mutex_init(&chip->irq_lock);
459
460 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
461 int irq = lvl + chip->irq_base;
462
463 if (!(chip->dir_input & (1 << lvl)))
464 continue;
465
466 set_irq_chip_data(irq, chip);
467 set_irq_chip_and_handler(irq, &max732x_irq_chip,
468 handle_edge_irq);
469 set_irq_nested_thread(irq, 1);
470#ifdef CONFIG_ARM
471 set_irq_flags(irq, IRQF_VALID);
472#else
473 set_irq_noprobe(irq);
474#endif
475 }
476
477 ret = request_threaded_irq(client->irq,
478 NULL,
479 max732x_irq_handler,
480 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
481 dev_name(&client->dev), chip);
482 if (ret) {
483 dev_err(&client->dev, "failed to request irq %d\n",
484 client->irq);
485 goto out_failed;
486 }
487
488 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
489 }
490
491 return 0;
492
493out_failed:
494 chip->irq_base = 0;
495 return ret;
496}
497
498static void max732x_irq_teardown(struct max732x_chip *chip)
499{
500 if (chip->irq_base)
501 free_irq(chip->client->irq, chip);
502}
503#else /* CONFIG_GPIO_MAX732X_IRQ */
504static int max732x_irq_setup(struct max732x_chip *chip,
505 const struct i2c_device_id *id)
506{
507 struct i2c_client *client = chip->client;
508 struct max732x_platform_data *pdata = client->dev.platform_data;
509 int has_irq = max732x_features[id->driver_data] >> 32;
510
511 if (pdata->irq_base && has_irq != INT_NONE)
512 dev_warn(&client->dev, "interrupt support not compiled in\n");
513
514 return 0;
515}
516
517static void max732x_irq_teardown(struct max732x_chip *chip)
518{
519}
520#endif
521
212static int __devinit max732x_setup_gpio(struct max732x_chip *chip, 522static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
213 const struct i2c_device_id *id, 523 const struct i2c_device_id *id,
214 unsigned gpio_start) 524 unsigned gpio_start)
215{ 525{
216 struct gpio_chip *gc = &chip->gpio_chip; 526 struct gpio_chip *gc = &chip->gpio_chip;
217 uint32_t id_data = id->driver_data; 527 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
218 int i, port = 0; 528 int i, port = 0;
219 529
220 for (i = 0; i < 16; i++, id_data >>= 2) { 530 for (i = 0; i < 16; i++, id_data >>= 2) {
@@ -306,9 +616,13 @@ static int __devinit max732x_probe(struct i2c_client *client,
306 616
307 mutex_init(&chip->lock); 617 mutex_init(&chip->lock);
308 618
309 max732x_read(chip, is_group_a(chip, 0), &chip->reg_out[0]); 619 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
310 if (nr_port > 7) 620 if (nr_port > 7)
311 max732x_read(chip, is_group_a(chip, 8), &chip->reg_out[1]); 621 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
622
623 ret = max732x_irq_setup(chip, id);
624 if (ret)
625 goto out_failed;
312 626
313 ret = gpiochip_add(&chip->gpio_chip); 627 ret = gpiochip_add(&chip->gpio_chip);
314 if (ret) 628 if (ret)
@@ -325,6 +639,7 @@ static int __devinit max732x_probe(struct i2c_client *client,
325 return 0; 639 return 0;
326 640
327out_failed: 641out_failed:
642 max732x_irq_teardown(chip);
328 kfree(chip); 643 kfree(chip);
329 return ret; 644 return ret;
330} 645}
@@ -352,6 +667,8 @@ static int __devexit max732x_remove(struct i2c_client *client)
352 return ret; 667 return ret;
353 } 668 }
354 669
670 max732x_irq_teardown(chip);
671
355 /* unregister any dummy i2c_client */ 672 /* unregister any dummy i2c_client */
356 if (chip->client_dummy) 673 if (chip->client_dummy)
357 i2c_unregister_device(chip->client_dummy); 674 i2c_unregister_device(chip->client_dummy);