[PATCH v2 1/3] pps: pps-gpio: split IRQ handler into hardirq and threaded parts

From: Michael Byczkowski

Date: Mon Apr 06 2026 - 17:04:43 EST


On PREEMPT_RT, all IRQ handlers are force-threaded. The current
pps_gpio_irq_handler captures the PPS timestamp via pps_get_ts()
inside the handler, but on RT this runs in thread context — after
a scheduling delay that adds variable latency (jitter) to the
timestamp.

Split the handler into a hardirq primary (pps_gpio_irq_hardirq)
that only captures the timestamp, and a threaded handler
(pps_gpio_irq_thread) that processes the event. With
request_threaded_irq(), the primary handler runs in hardirq context
even on PREEMPT_RT, preserving nanosecond timestamp precision.

On non-RT kernels, request_threaded_irq with an explicit primary
handler behaves identically to the previous request_irq call.

Signed-off-by: Michael Byczkowski <by@xxxxxxxxxxxx>
Acked-by: Rodolfo Giometti <giometti@xxxxxxxxxxxx>
Tested-by: Michael Byczkowski <by@xxxxxxxxxxxx>
Tested-by: Calvin Owens <calvin@xxxxxxxxxx>
---
drivers/pps/clients/pps-gpio.c | 37 +++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/drivers/pps/clients/pps-gpio.c b/drivers/pps/clients/pps-gpio.c
index 935da68610c7..f37398fd6b10 100644
--- a/drivers/pps/clients/pps-gpio.c
+++ b/drivers/pps/clients/pps-gpio.c
@@ -35,33 +35,44 @@ struct pps_gpio_device_data {
bool capture_clear;
unsigned int echo_active_ms; /* PPS echo active duration */
unsigned long echo_timeout; /* timer timeout value in jiffies */
+ struct pps_event_time ts; /* timestamp captured in hardirq */
};

/*
* Report the PPS event
*/

-static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
+/*
+ * Primary hardirq handler — runs in hardirq context even on PREEMPT_RT.
+ * Only captures the timestamp; all other work is deferred to the thread.
+ */
+static irqreturn_t pps_gpio_irq_hardirq(int irq, void *data)
{
- const struct pps_gpio_device_data *info;
- struct pps_event_time ts;
- int rising_edge;
+ struct pps_gpio_device_data *info = data;
+
+ pps_get_ts(&info->ts);

- /* Get the time stamp first */
- pps_get_ts(&ts);
+ return IRQ_WAKE_THREAD;
+}

- info = data;
+/*
+ * Threaded handler — processes the PPS event using the timestamp
+ * captured in hardirq context above.
+ */
+static irqreturn_t pps_gpio_irq_thread(int irq, void *data)
+{
+ struct pps_gpio_device_data *info = data;
+ int rising_edge;

- /* Small trick to bypass the check on edge's direction when capture_clear is unset */
rising_edge = info->capture_clear ?
gpiod_get_value(info->gpio_pin) : !info->assert_falling_edge;
if ((rising_edge && !info->assert_falling_edge) ||
(!rising_edge && info->assert_falling_edge))
- pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
+ pps_event(info->pps, &info->ts, PPS_CAPTUREASSERT, data);
else if (info->capture_clear &&
((rising_edge && info->assert_falling_edge) ||
(!rising_edge && !info->assert_falling_edge)))
- pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
+ pps_event(info->pps, &info->ts, PPS_CAPTURECLEAR, data);
else
dev_warn_ratelimited(&info->pps->dev, "IRQ did not trigger any PPS event\n");

@@ -210,8 +221,10 @@ static int pps_gpio_probe(struct platform_device *pdev)
}

/* register IRQ interrupt handler */
- ret = request_irq(data->irq, pps_gpio_irq_handler,
- get_irqf_trigger_flags(data), data->info.name, data);
+ ret = request_threaded_irq(data->irq,
+ pps_gpio_irq_hardirq, pps_gpio_irq_thread,
+ get_irqf_trigger_flags(data) | IRQF_ONESHOT,
+ data->info.name, data);
if (ret) {
pps_unregister_source(data->pps);
dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
--
2.47.3