From: Alexey Charkov <alchark@flipper.net>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Guenter Roeck <linux@roeck-us.net>,
Yueyao Zhu <yueyao@google.com>, Heiko Stuebner <heiko@sntech.de>
Cc: Sebastian Reichel <sebastian.reichel@collabora.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org,
linux-kernel@vger.kernel.org,
Alexey Charkov <alchark@flipper.net>
Subject: [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread
Date: Fri, 27 Nov 2026 14:40:49 +0400 [thread overview]
Message-ID: <20261127-fusb302-getsourcecap-v2-4-6770eb1e7945@flipper.net> (raw)
In-Reply-To: <20261127-fusb302-getsourcecap-v2-0-6770eb1e7945@flipper.net>
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@linutronix.de [1]
Suggested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Suggested-by: Sebastian Reichel <sebastian.reichel@collabora.com>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
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
next prev parent reply other threads:[~2026-11-27 10:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on Alexey Charkov
2026-11-27 10:40 ` Alexey Charkov [this message]
2026-11-27 10:40 ` [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time Alexey Charkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127-fusb302-getsourcecap-v2-4-6770eb1e7945@flipper.net \
--to=alchark@flipper.net \
--cc=bigeasy@linutronix.de \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=heiko@sntech.de \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=sebastian.reichel@collabora.com \
--cc=yueyao@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®