Skip to content

Commit

Permalink
drivers: gpio: mcp23xxx: support single-edge interrupts
Browse files Browse the repository at this point in the history
An interrupt is triggered for every edge, but only the desired edges cause
a callback to be called.

Signed-off-by: Armin Brauns <[email protected]>
  • Loading branch information
arbrauns authored and carlescufi committed Mar 20, 2023
1 parent 0db9785 commit 07af23c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions drivers/gpio/gpio_mcp23xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,16 @@ static int mcp23xxx_pin_interrupt_configure(const struct device *dev, gpio_pin_t

switch (trig) {
case GPIO_INT_TRIG_LOW:
drv_data->rising_edge_ints &= ~BIT(pin);
drv_data->falling_edge_ints |= BIT(pin);
break;
case GPIO_INT_TRIG_HIGH:
LOG_ERR("mcp23xxx does not support single-edge interrupts");
ret = -ENOTSUP;
goto done;
drv_data->rising_edge_ints |= BIT(pin);
drv_data->falling_edge_ints &= ~BIT(pin);
break;
case GPIO_INT_TRIG_BOTH:
drv_data->rising_edge_ints |= BIT(pin);
drv_data->falling_edge_ints |= BIT(pin);
break;
}
break;
Expand Down Expand Up @@ -421,6 +426,12 @@ static void mcp23xxx_work_handler(struct k_work *work)
goto done;
}

/* mcp23xxx does not support single-edge interrupts in hardware, filter them out manually */
uint16_t level_ints = drv_data->reg_cache.gpinten & drv_data->reg_cache.intcon;

intf &= level_ints | (intcap & drv_data->rising_edge_ints) |
(~intcap & drv_data->falling_edge_ints);

gpio_fire_callbacks(&drv_data->callbacks, dev, intf);
done:
k_sem_give(&drv_data->lock);
Expand Down
3 changes: 3 additions & 0 deletions drivers/gpio/gpio_mcp23xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ struct mcp23xxx_drv_data {
struct gpio_callback int_gpio_cb;
struct k_work work;

uint16_t rising_edge_ints;
uint16_t falling_edge_ints;

struct {
uint16_t iodir;
uint16_t ipol;
Expand Down

0 comments on commit 07af23c

Please sign in to comment.