[PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread
From: Alexey Charkov
Date: Fri Nov 27 2026 - 05:41:54 EST
The threaded IRQ handler only disables the IRQ and queues irq_work, which
then does all the handling. The extra hop costs a context switch, and
leaves the handling at normal priority behind whatever else shares the
system workqueue.
Handle the interrupt in the IRQ thread instead, as suggested in review of
the switch to a threaded handler [1]. IRQF_ONESHOT keeps the line masked
meanwhile. Before resume, the I2C adapter may still be suspended: disable
the IRQ then, and have resume handle it before enabling it again, so that
nothing handles it twice at once and it stays a wakeup source. Make
irq_lock a mutex for that, as disabling an IRQ behind an I2C GPIO
expander may sleep.
Verified both at runtime and across suspend on a Rockchip RK3576 board.
Link: https://lore.kernel.org/r/20260312120418.99U0NPWL@xxxxxxxxxxxxx [1]
Suggested-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Suggested-by: Sebastian Reichel <sebastian.reichel@xxxxxxxxxxxxx>
Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
drivers/usb/typec/tcpm/fusb302.c | 82 +++++++++++++++++++++-------------------
1 file changed, 43 insertions(+), 39 deletions(-)
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 5cf1b78e7669..aa0e4dbe31db 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -87,9 +87,13 @@ struct fusb302_chip {
struct regulator *vbus;
- spinlock_t irq_lock;
- struct work_struct irq_work;
+ /*
+ * A mutex, as disabling an IRQ behind an I2C GPIO expander, which
+ * fusb302_irq_intn() does under it, may sleep
+ */
+ struct mutex irq_lock;
bool irq_suspended;
+ /* The IRQ came before resume, which handles it and enables it again */
bool irq_while_suspended;
struct gpio_desc *gpio_int_n;
int gpio_int_n_irq;
@@ -1943,24 +1947,6 @@ static int fusb302_pd_rx_start(struct fusb302_chip *chip)
return ret;
}
-static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
-{
- struct fusb302_chip *chip = dev_id;
- unsigned long flags;
-
- /* Disable our level triggered IRQ until our irq_work has cleared it */
- disable_irq_nosync(chip->gpio_int_n_irq);
-
- spin_lock_irqsave(&chip->irq_lock, flags);
- if (chip->irq_suspended)
- chip->irq_while_suspended = true;
- else
- schedule_work(&chip->irq_work);
- spin_unlock_irqrestore(&chip->irq_lock, flags);
-
- return IRQ_HANDLED;
-}
-
/*
* Reading an interrupt register clears its flags in the chip, so keep them
* pending until handled: a bus error on a later read would otherwise lose
@@ -1998,10 +1984,8 @@ static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
return ret;
}
-static void fusb302_irq_work(struct work_struct *work)
+static void fusb302_irq_handle(struct fusb302_chip *chip)
{
- struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
- irq_work);
int ret = 0;
u8 interrupt;
u8 interrupta;
@@ -2128,7 +2112,26 @@ static void fusb302_irq_work(struct work_struct *work)
}
done:
mutex_unlock(&chip->lock);
- enable_irq(chip->gpio_int_n_irq);
+}
+
+static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
+{
+ struct fusb302_chip *chip = dev_id;
+
+ mutex_lock(&chip->irq_lock);
+ if (chip->irq_suspended) {
+ /* Only once, as resume enables it once */
+ if (!chip->irq_while_suspended)
+ disable_irq_nosync(chip->gpio_int_n_irq);
+ chip->irq_while_suspended = true;
+ mutex_unlock(&chip->irq_lock);
+ return IRQ_HANDLED;
+ }
+ mutex_unlock(&chip->irq_lock);
+
+ fusb302_irq_handle(chip);
+
+ return IRQ_HANDLED;
}
static int init_gpio(struct fusb302_chip *chip)
@@ -2226,8 +2229,7 @@ static int fusb302_probe(struct i2c_client *client)
if (!chip->wq)
return -ENOMEM;
- spin_lock_init(&chip->irq_lock);
- INIT_WORK(&chip->irq_work, fusb302_irq_work);
+ mutex_init(&chip->irq_lock);
INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
INIT_DELAYED_WORK(&chip->rx_recheck, fusb302_pd_rx_recheck_work);
init_tcpc_dev(&chip->tcpc_dev);
@@ -2294,7 +2296,6 @@ static void fusb302_remove(struct i2c_client *client)
disable_irq_wake(chip->gpio_int_n_irq);
free_irq(chip->gpio_int_n_irq, chip);
- cancel_work_sync(&chip->irq_work);
cancel_delayed_work_sync(&chip->bc_lvl_handler);
cancel_delayed_work_sync(&chip->rx_recheck);
tcpm_unregister_port(chip->tcpm_port);
@@ -2306,14 +2307,13 @@ static void fusb302_remove(struct i2c_client *client)
static int fusb302_pm_suspend(struct device *dev)
{
struct fusb302_chip *chip = dev->driver_data;
- unsigned long flags;
- spin_lock_irqsave(&chip->irq_lock, flags);
+ mutex_lock(&chip->irq_lock);
chip->irq_suspended = true;
- spin_unlock_irqrestore(&chip->irq_lock, flags);
+ mutex_unlock(&chip->irq_lock);
- /* Make sure any pending irq_work is finished before the bus suspends */
- flush_work(&chip->irq_work);
+ /* Let an IRQ being handled finish before the bus suspends */
+ synchronize_irq(chip->gpio_int_n_irq);
/* No RX FIFO recheck may run until the bus is back either */
cancel_delayed_work_sync(&chip->rx_recheck);
return 0;
@@ -2322,15 +2322,19 @@ static int fusb302_pm_suspend(struct device *dev)
static int fusb302_pm_resume(struct device *dev)
{
struct fusb302_chip *chip = dev->driver_data;
- unsigned long flags;
+ bool pending;
- spin_lock_irqsave(&chip->irq_lock, flags);
- if (chip->irq_while_suspended) {
- schedule_work(&chip->irq_work);
- chip->irq_while_suspended = false;
- }
+ mutex_lock(&chip->irq_lock);
+ pending = chip->irq_while_suspended;
+ chip->irq_while_suspended = false;
chip->irq_suspended = false;
- spin_unlock_irqrestore(&chip->irq_lock, flags);
+ mutex_unlock(&chip->irq_lock);
+
+ /* The IRQ stays disabled until then, so nothing else handles it */
+ if (pending) {
+ fusb302_irq_handle(chip);
+ enable_irq(chip->gpio_int_n_irq);
+ }
/* Look at the RX FIFO again, in case suspend cut a recheck short */
mutex_lock(&chip->lock);
--
2.56.0