gpio: add interrupt handling capability to max732x
PATCH 1/1 gpio: add interrupt handling capability to max732x
Eric,
Following on Jebediah's remark last month that max732x didn't support interrupt handling, I gave it a shot and hacked something that is similar to what pca953x does.
It would be nice if someone (Jebediah?) could give it a shot, as it's been only compile-tested (need to find some time this WE to solder the chips to a board...).
Thanks,
M.
Signature · 1 line
Who you jivin' with that Cosmik Debris?
From e1677d4ac8aa7365a4c7ea27348f9e8a14baaf42 Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Mon, 15 Mar 2010 12:24:36 +0000
Subject: [PATCH] gpio: add interrupt handling capability to max732x
Most the GPIO expander 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).
--- drivers/gpio/Kconfig | 7 + drivers/gpio/max732x.c | 355 ++++++++++++++++++++++++++++++++++++++++--- include/linux/i2c/max732x.h | 3 + 3 files changed, 346 insertions(+), 19 deletions(-)
drivers/gpio/Kconfig +7 -0
@@ -139,6 +139,13 @@ config GPIO_MAX732X Board setup code must specify the model to use, and the start number for these GPIOs. +config GPIO_MAX732X_IRQ + bool "Interrupt controller support for MAX732x" + depends on GPIO_MAX732X=y && GENERIC_HARDIRQS + help + Say yes here to enable the max732x to be used as an interrupt + controller. It requires the driver to be built in the kernel. + config GPIO_PCA953X tristate "PCA953x, PCA955x, TCA64xx, and MAX7310 I/O ports" depends on I2C
drivers/gpio/max732x.c +336 -19
436 lines not on this page · raw message
include/linux/i2c/max732x.h +3 -0
@@ -7,6 +7,9 @@ struct max732x_platform_data {/* number of the first GPIO */ unsigned gpio_base; +/* interrupt base */ +int irq_base; +void *context;/* param to setup/teardown */ int (*setup)(struct i2c_client *client,
Signature · 1 line
1.7.0.2
drivers/gpio/max732x.c static int max732x_gpio_direction_output(struct gpio_chip *gc, · line 443
Quoted 10 lines · On Tue, 16 Mar 2010 11:34:52 +0100 Marc Zyngier <[email protected]> wrote:
drivers/gpio/max732x.c +10
-0+static irqreturn_t max732x_irq_handler (int irq,void *devid) +{ +struct max732x_chip *chip = devid; +uint8_t pending; +uint8_t level; + + pending =max732x_irq_pending (chip); + +if (!pending) +return IRQ_HANDLED ;
Should be IRQ_NONE?
If this device was on a shared interrupt line and the interrupt was caused by some other device, and this handler is called first then the incorrect IRQ_HANDLED return could cause the irq core to do wrong things.
Quoted 9 lines
drivers/gpio/max732x.c +9
-0+do { + level =__ffs (pending); +handle_nested_irq (level + chip->irq_base); + + pending &= ~(1 << level); + }while (pending); + +return IRQ_HANDLED ; +}
Quoted 15 lines · On Fri, 19 Mar 2010 16:41:56 -0700 Andrew Morton <[email protected]>…
drivers/gpio/max732x.c static int max732x_gpio_direction_output(struct gpio_chip *gc, · line 443
Quoted 10 lines · On Tue, 16 Mar 2010 11:34:52 +0100 Marc Zyngier <[email protected]> wrote:
drivers/gpio/max732x.c +10
-0+static irqreturn_t max732x_irq_handler (int irq,void *devid) +{ +struct max732x_chip *chip = devid; +uint8_t pending; +uint8_t level; + + pending =max732x_irq_pending (chip); + +if (!pending) +return IRQ_HANDLED ;Should be
IRQ_NONE?
The main problem is that some of the supported expanders do not have a hardware interrupt mask, so the IRQ will fire on each input pin toggle, including the ones we don't care about (pca953x has the same problem).
Returning IRQ_NONE will cause the irq core to quickly disable the line. Not good either. I could add a test for the mask feature bit, and return IRQ_NONE only in that case.
Quoted 3 lines
If this device was on a shared interrupt line and the interrupt was caused by some other device, and this handler is called first then the incorrect
IRQ_HANDLEDreturn could cause the irq core to do wrong things.
This is exactly why the irq handler is requested without the SHARED flag:
+ ret = request_threaded_irq(client->irq,
+ NULL, + max732x_irq_handler, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT, + dev_name(&client->dev), chip);
The same mask feature bit could be used to allow shared interrupts with devices that support it.
Thanks,
M.
Signature · 1 line
I'm the slime oozin' out from your TV set...
Quoted 16 lines · On Fri, 19 Mar 2010 16:41:56 -0700 Andrew Morton <[email protected]>…
Review
Quoted 10 lines · On Tue, 16 Mar 2010 11:34:52 +0100 Marc Zyngier <[email protected]> wrote:
(quoted diff) +10
-0+static irqreturn_t max732x_irq_handler(int irq, void *devid) +{ + struct max732x_chip *chip = devid; + uint8_t pending; + uint8_t level; + + pending = max732x_irq_pending(chip); + + if (!pending) + return IRQ_HANDLED;Should be
IRQ_NONE?
Quoted 3 lines
If this device was on a shared interrupt line and the interrupt was caused by some other device, and this handler is called first then the incorrect
IRQ_HANDLEDreturn could cause the irq core to do wrong things.
Hi Andrew,
Would the following patch (against the original version) address your concerns?
Thanks,
M.
Quoted 1 line
From
a8d40580ad5fe23e0737bac5f8b04dfd8cb93fe5Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Mon, 22 Mar 2010 07:28:30 +0000
Subject: [PATCH] gpio: max732x: allow IRQF_SHARED when hardware mask is available
--- drivers/gpio/max732x.c | 25 ++++++++++++++++++++++--- 1 files changed, 22 insertions(+), 3 deletions(-)
drivers/gpio/max732x.c +22 -3
@@ -293,6 +293,14 @@ static int max732x_readw(struct max732x_chip *chip, uint16_t *val)return 0 ; } +static inline int max732x_has_irqmask (struct max732x_chip *chip) +{ +if (chip->irq_features ==INT_NO_MASK ) +return 0 ; + +return 1 ; +} +static void max732x_irq_update_mask (struct max732x_chip *chip) {uint16_t msg;
@@ -302,7 +310,7 @@ static void max732x_irq_update_mask(struct max732x_chip *chip) chip->irq_mask = chip->irq_mask_cur; -if (chip->irq_features ==INT_NO_MASK ) +if (!max732x_has_irqmask (chip))return ;mutex_lock (&chip->lock);
@@ -439,8 +447,14 @@ static irqreturn_t max732x_irq_handler(int irq, void *devid) pending =max732x_irq_pending (chip); -if (!pending) +if (!pending) { +/* if we have a hardware mask, then the interrupt must +* come from another device */ +if (max732x_has_irqmask (chip)) +return IRQ_NONE ; +return IRQ_HANDLED ; + } do { level = __ffs(pending);
@@ -462,6 +476,7 @@ static int max732x_irq_setup(struct max732x_chip *chip,if (pdata->irq_base && has_irq !=INT_NONE ) {int lvl; +int irq_flags; chip->irq_base = pdata->irq_base; chip->irq_features = has_irq;
@@ -484,10 +499,14 @@ static int max732x_irq_setup(struct max732x_chip *chip,#endif } + irq_flags =IRQF_TRIGGER_FALLING |IRQF_ONESHOT ; +if (max732x_has_irqmask (chip)) + irq_flags |=IRQF_SHARED ; + ret =request_threaded_irq (client->irq,NULL , max732x_irq_handler, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , + irq_flags,dev_name (&client->dev), chip);if (ret) { dev_err(&client->dev, "failed to request irq %d\n",
Signature · 6 lines
1.7.0.2 -- I'm the slime oozin' out from your TV set...
Quoted 2 lines · On Mon, 22 Mar 2010 07:39:46 +0000 Marc Zyngier <[email protected]> wrote:
Would the following patch (against the original version) address your concerns?
Gee, I dunno. Is it necessary? I thought your earlier explanation covered everything OK.
I wouldn't say I had concerns really - I just poke people to verify that problems didn't sneak through. If you think it improves the driver, sure, add it?