Skip to contentSkip to conversation

pps-gpio: restore pin mux on unbind and shutdown

PATCH Superseded Eliav Farber 6 participants 69 messages Sep 16 – Nov 2

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover pps-gpio: restore pin mux on unbind and shutdown

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "idle" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. It is a no-op for boards that do not describe an "idle" state, and depends on CONFIG_PM (which performs the idle-state lookup).

Patch 1 documents the optional "default"/"idle" pinctrl-names in the binding; patch 2 implements the driver side.

Tested on an AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding PBS pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state.

Eliav Farber (2):
dt-bindings: pps: pps-gpio: document optional idle pinctrl state
pps: clients: gpio: release pins to idle state on remove and shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     | 15 ++++++++++++++-
 drivers/pps/clients/pps-gpio.c                | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 dt-bindings: pps: pps-gpio: document optional idle pinctrl state

pps-gpio can now release its pins to an "idle" pinctrl state when the driver is unbound or the system is shut down, in addition to the "default" state that the driver core applies before probe.

Document the optional "default" and "idle" pinctrl-names and show both in the example so boards can describe an alternate pin mux for when PPS is not active.

---
 .../devicetree/bindings/pps/pps-gpio.yaml         | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +14 -1
index 383a838744eb..035c780ecd04 100644
@@ -28,6 +28,18 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, name the "default"
+      state to select the PPS/GPIO function while the driver is bound. An
+      optional "idle" state, if present, is selected when the driver is
+      unbound or the system is shut down, handing the pins back to their
+      alternate function.
+    minItems: 1
+    items:
+      - const: default
+      - const: idle
+
 required:
   - compatible
   - gpios
@@ -40,8 +52,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "idle";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_idle>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 pps: clients: gpio: release pins to idle state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux it to another function when the pps-gpio driver is not bound. The driver core already applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound without any driver change.

Nothing, however, hands the pins back when the driver is unbound or the system is shut down. Select the "idle" pinctrl state in both remove() and shutdown() so a board can describe the alternate mux there and have it restored, for example before kexec.

pinctrl_pm_select_idle_state() is a no-op for devices that have no pin controller or no "idle" state, so boards that do not use this are unaffected. The "idle" state is only looked up and selectable when CONFIG_PM is enabled; without CONFIG_PM the helper compiles to a no-op and the pins simply retain their "default" mux.

---
 drivers/pps/clients/pps-gpio.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/pps/clients/pps-gpio.c +19 -0
index 73ec2c7335e5..bdedbed82d82 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -216,9 +217,26 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/*
+	 * Release the pins to their "idle" state, if the board defines one, so
+	 * they are handed back to whatever function uses them while pps-gpio is
+	 * not bound. Boards that do not describe an idle pinctrl state are
+	 * unaffected. The "default" (active) state is applied automatically by
+	 * the driver core before probe.
+	 */
+	pinctrl_pm_select_idle_state(&pdev->dev);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	/*
+	 * Leave the pins in their "idle" state on shutdown so a subsequent
+	 * kernel (e.g. after kexec) finds the pin controller in a known state.
+	 */
+	pinctrl_pm_select_idle_state(&pdev->dev);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +246,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/2] pps: clients: gpio: release pins to idle state on remove and shutdown

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] pps: missing IRQ and timer teardown in pps_gpio_shutdown() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pps: missing IRQ and timer teardown in pps_gpio_shutdown()
--

commit e71a89236222915be5ef5d5a822ad6e3bcc21b37
Author: Eliav Farber <[email protected]>

pps: clients: gpio: release pins to idle state on remove and shutdown

Some boards route the PPS input GPIO through a pin controller and mux it to another function when the driver is not bound. This patch selects the "idle" pinctrl state in both remove() and shutdown() so that the alternate mux is restored, for instance before kexec.

Quoted 4 lines
drivers/pps/clients/pps-gpio.c +0 -0
index 73ec2c7335e51..bdedbed82d82c 100644

[ ... ]

Quoted 12 lines
drivers/pps/clients/pps-gpio.c +8 -0
@@ -216,9 +217,26 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/*
+	 * Release the pins to their "idle" state, if the board defines one, so
+	 * they are handed back to whatever function uses them while pps-gpio is
+	 * not bound. Boards that do not describe an idle pinctrl state are
+	 * unaffected. The "default" (active) state is applied automatically by
+	 * the driver core before probe.
+	 */
+	pinctrl_pm_select_idle_state(&pdev->dev);

drivers/pps/clients/pps-gpio.c static void pps_gpio_remove(struct platform_device *pdev) · line 237

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +7 -0
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	/*
+	 * Leave the pins in their "idle" state on shutdown so a subsequent
+	 * kernel (e.g. after kexec) finds the pin controller in a known state.
+	 */
+	pinctrl_pm_select_idle_state(&pdev->dev);

[Severity: High]
Does this code risk causing an interrupt storm during shutdown or kexec?

When pps_gpio_shutdown() alters the physical pin state to "idle" while system interrupts are still enabled, it could leave the edge-triggered PPS input pin floating (e.g. if the "idle" state removes pull resistors). Because the IRQ is never explicitly disabled here and the echo timer is left running, electrical noise might generate rapid spurious edges.

Since the IRQ handler always returns IRQ_HANDLED, the kernel IRQ core won't detect and shut down a spurious storm. Could the CPU get stuck in an infinite interrupt loop, preventing shutdown or kexec from completing? Should the IRQ and timer be torn down here as they are in pps_gpio_remove()?

Quoted 2 lines
drivers/pps/clients/pps-gpio.c +2 -0
+}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#4 lore raw
In reply to Eliav Farber
Quoted 5 lines · Eliav Farber

This series lets pps-gpio select an optional "idle" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. It is a no-op for boards that do not describe an "idle" state, and depends on CONFIG_PM (which performs the idle-state lookup).

Thanks, the problem is real: the pinctrl core never reverts the mux on unbind, so the pins stay stuck in the GPIO function for whatever comes next, kexec included.

But "idle" does not mean "not bound", so are you sure this is the right-thing(TM) to do? pinctrl-state.h documents it as the runtime PM state and, AFAIK, that is how the rest of the tree uses it. If pps-gpio ever grows a real runtime PM or a .suspend(), "idle" is already taken with another meaning -- and 1/2 turns that choice into ABI.

Same question for the CONFIG_PM dependency: why should a CONFIG_PM=n kernel not get this? A PPS box built without PM is not an odd configuration, and there the board describes an "idle" state and nothing happens, silently.

Wouldn't looking the state up in the driver (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state()) avoid both, and leave you free to pick a name that says what it means?

One thing that does not depend on any of the above: your shutdown() changes the mux but shuts nothing down. The IRQ is still requested (request_irq() here is not devm-managed) and the echo timer may still be armed, so a timer callback can still poke a pin that by then belongs to somebody else, and the PPS handler stays attached to a line that other function is now driving. And device_shutdown() is not the end of the road: the kernel keeps running to load and start the kexec image, which is the case you are after. Shouldn't it tear down in the same order remove() does, free_irq() and timer_delete_sync() first and the mux change last?

Ciao,

Rodolfo

#5 lore raw
Quoted 6 lines · Rodolfo Giometti

But "idle" does not mean "not bound", so are you sure this is the right-thing(TM) to do? pinctrl-state.h documents it as the runtime PM state and, AFAIK, that is how the rest of the tree uses it. If pps-gpio ever grows a real runtime PM or a .suspend(), "idle" is already taken with another meaning -- and 1/2 turns that choice into ABI.

Right. Renamed to "inactive" in v2.

Quoted 8 lines

Same question for the CONFIG_PM dependency: why should a CONFIG_PM=n kernel not get this? A PPS box built without PM is not an odd configuration, and there the board describes an "idle" state and nothing happens, silently.

Wouldn't looking the state up in the driver (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state()) avoid both, and leave you free to pick a name that says what it means?

Done. v2 looks the state up in the driver and selects it with pinctrl_select_state(), dropping the CONFIG_PM dependency.

v2 also requires "default" when "inactive" is present and rejects the mismatch at probe.

Quoted 10 lines

One thing that does not depend on any of the above: your shutdown() changes the mux but shuts nothing down. The IRQ is still requested (request_irq() here is not devm-managed) and the echo timer may still be armed, so a timer callback can still poke a pin that by then belongs to somebody else, and the PPS handler stays attached to a line that other function is now driving. And device_shutdown() is not the end of the
road: the kernel keeps running to load and start the kexec image, which is the case you are after. Shouldn't it tear down in the same order remove() does, free_irq() and timer_delete_sync() first and the mux change last?

Fixed. shutdown() now does free_irq() and timer_delete_sync() first, mux change last. It does not call pps_unregister_source() -- that is a remove-time concern, not needed to quiesce the hardware for kexec. Say if you want it there anyway.

Thanks,
Eliav

#6 lore raw

v2 cover pps-gpio: restore pin mux on unbind and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Patch 1 documents the optional "default"/"inactive" pinctrl-names in the binding; patch 2 implements the driver side.

Tested on an AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding PBS pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state.

Changes in v2 (all addressing Rodolfo Giometti's review):
- Rename the released state from "idle" to "inactive". "idle" is the runtime-PM state in pinctrl-state.h; overloading it for "driver not active" would clash with any future runtime PM or .suspend() and was being baked into the binding as ABI. "inactive" is not a well-known state, so no generic PM helper will ever auto-select it -- the driver drives it explicitly
- Look the state up in the driver (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state()) instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency so a CONFIG_PM=n kernel that describes an "inactive" state now honors it instead of silently doing nothing
- Fix shutdown(): tear down in the same order as remove() -- free_irq() and timer_delete_sync() first, the mux change last -- so no IRQ or echo timer callback can drive a pin after it has been handed back to another function; shutdown() no longer changes the mux while the hardware is still live
- Require a "default" state whenever "inactive" is present and reject the mismatch, rather than releasing pins that were never put into a defined PPS state

Eliav Farber (2):
dt-bindings: pps: pps-gpio: document optional pinctrl states
pps: clients: gpio: release pins to an inactive state on remove and
shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     | 16 +++-
 drivers/pps/clients/pps-gpio.c                | 75 +++++++++++++++++++
 2 files changed, 90 insertions(+), 1 deletion(-)

base-commit: 9b87fdc9af2fbfcdb5c24a64139685ef80f6573f

Signature · 1 line
2.47.3
#7 lore raw

PATCH v2 1/2 dt-bindings: pps: pps-gpio: document optional pinctrl states

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. As "inactive" is only the state to restore after "default", it may only appear as the second entry, so a "default" state is required whenever "inactive" is given.

---
Changes in v2:
- Rename the released state from "idle" to "inactive"; document that "inactive" may only be the second entry, so a "default" state is required whenever "inactive" is given

 .../devicetree/bindings/pps/pps-gpio.yaml        | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +15 -1
index 383a838744eb..7d11ec4656ee 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the "default"
+      state selects the PPS/GPIO function and is applied by the driver core
+      before probe. The optional "inactive" state, when present, is selected
+      when the driver is unbound or the system is shut down, handing the pins
+      back to their alternate function. As "inactive" is the state to restore
+      after "default", it may only appear as the second entry.
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive
+
 required:
   - compatible
   - gpios
@@ -40,8 +53,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#10 lore raw

PATCH v2 2/2 pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() and timer_delete_sync() first, and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

---
Changes in v2:
- Rename the released state from "idle" to "inactive"
- Look the state up in the driver with devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state() instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown() to free_irq() and timer_delete_sync() before the mux change, matching remove(), so no IRQ or timer callback can drive a pin after it has been handed back
- Require a "default" state whenever "inactive" is present and reject the mismatch

 drivers/pps/clients/pps-gpio.c | 75 ++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
drivers/pps/clients/pps-gpio.c +75 -0
index 73ec2c7335e5..e619c7bb2f78 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,51 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state: the mux to restore when
+ * pps-gpio is unbound or the system is shut down. It is only meaningful
+ * paired with a "default" state, which the driver core applies before probe
+ * to mux the pins for PPS use. A board that describes neither is unaffected;
+ * one that describes "inactive" without "default" is rejected, since
+ * releasing pins that were never put into a defined PPS state is incoherent.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl))
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+
+	/* The "inactive" state is optional; without it there is nothing to do. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}
+
+	/* "inactive" requires a "default" state to release back from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default))
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+
+	return 0;
+}
+
+/*
+ * Release the pins to their "inactive" state, if the board describes one, so
+ * they are handed back to whatever function uses them while pps-gpio is not
+ * driving PPS. Boards without an "inactive" state are unaffected.
+ */
+static void pps_gpio_release_pins(struct pps_gpio_device_data *data)
+{
+	if (data->pins_inactive)
+		pinctrl_select_state(data->pinctrl, data->pins_inactive);
+}
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -161,6 +209,11 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
@@ -216,9 +269,30 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/* release the pins last, once nothing can drive them anymore */
+	pps_gpio_release_pins(data);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * Quiesce the hardware before touching the mux: stop the IRQ and the
+	 * echo timer first so nothing can drive the pins, then hand them back
+	 * to their "inactive" function. The kernel keeps running after
+	 * device_shutdown() (for example to load and start a kexec image), so
+	 * the pins must not be released while an IRQ or timer callback can
+	 * still reach them. The PPS source is left registered; unregistering
+	 * it is a remove-time concern and is unnecessary on shutdown.
+	 */
+	free_irq(data->irq, data);
+	timer_delete_sync(&data->echo_timer);
+	gpiod_set_value(data->echo_pin, 0);
+	pps_gpio_release_pins(data);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +302,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
#11 lore raw

Re: [PATCH v2 2/2] pps: clients: gpio: release pins to an inactive state on remove and shutdown

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] pps-gpio: unconditional probe failure for boards without pinctrl bindings - [Medium] pps-gpio…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] pps-gpio: unconditional probe failure for boards without pinctrl bindings
- [Medium] pps-gpio: pins left in 'default' state on probe failure
--

commit 8a9e09ee4e79423b318933ea7fb883c1d285a638
Author: Eliav Farber <[email protected]>

pps: clients: gpio: release pins to an inactive state on remove and shutdown

This patch looks up an optional "inactive" pinctrl state in probe and selects it on remove and shutdown to release the pins. The goal is to prevent pins from staying stuck in the GPIO function for subsequent executions like kexec or after the driver is unbound.

Quoted 4 lines
drivers/pps/clients/pps-gpio.c +0 -0
index 73ec2c7335e51..e619c7bb2f78a 100644

[ ... ]

Quoted 11 lines
drivers/pps/clients/pps-gpio.c +7 -0
@@ -96,6 +99,51 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state: the mux to restore when
+ * pps-gpio is unbound or the system is shut down. It is only meaningful
+ * paired with a "default" state, which the driver core applies before probe
+ * to mux the pins for PPS use. A board that describes neither is unaffected;
+ * one that describes "inactive" without "default" is rejected, since
+ * releasing pins that were never put into a defined PPS state is incoherent.

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 118

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +10 -0
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl))
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");

[Severity: High]
If a board doesn't have explicit pinctrl bindings, won't devm_pinctrl_get() return -ENODEV?

The driver core typically ignores -ENODEV to support devices without explicit pinmuxes. Would treating it as a fatal error here cause the probe to fail unconditionally on those boards and break existing setups?

Quoted 7 lines
drivers/pps/clients/pps-gpio.c +7 -0
+
+	/* The "inactive" state is optional; without it there is nothing to do. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}

[ ... ]

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 219

Quoted 12 lines
drivers/pps/clients/pps-gpio.c +5 -0
@@ -161,6 +209,11 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {

[Severity: Medium]
If an error occurs later in the probe function, for example in gpiod_to_irq() or pps_register_source(), the function returns early:

pps_gpio_probe() {
	...
	ret = gpiod_to_irq(data->gpio_pin);
	if (ret < 0) {
		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
		return -EINVAL;
	}
	...
}

Since the driver core applies the 'default' pinctrl state before probe begins, and these error paths exit without calling pps_gpio_release_pins(), would the pins be left stuck in the 'default' state if probe fails?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#8 lore raw

Re: [PATCH v2 1/2] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 6 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +0 -0
index 383a838744eb..7d11ec4656ee 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 38

Quoted 10 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +8 -0
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the "default"
+      state selects the PPS/GPIO function and is applied by the driver core
+      before probe. The optional "inactive" state, when present, is selected
+      when the driver is unbound or the system is shut down, handing the pins
+      back to their alternate function. As "inactive" is the state to restore
+      after "default", it may only appear as the second entry.

The driver looks the states up by name, not by index, and only requires that "default" exists. Why you say that?

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 42

Quoted 4 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

AFAIK the tuple rules out ["default", "sleep"] and ["init", "default"] for good, and this is ABI.

Quoted 5 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +1 -0
+
 required:
   - compatible
   - gpios

Documentation/devicetree/bindings/pps/pps-gpio.yaml examples: · line 61

Quoted 10 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +2 -1
@@ -40,8 +53,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;

Ciao,

Rodolfo

maintainer
#12 lore raw

Re: [PATCH v2 2/2] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber
Quoted 48 lines · Eliav Farber
drivers/pps/clients/pps-gpio.c +26 -0
index 73ec2c7335e5..e619c7bb2f78 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,51 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state: the mux to restore when
+ * pps-gpio is unbound or the system is shut down. It is only meaningful
+ * paired with a "default" state, which the driver core applies before probe
+ * to mux the pins for PPS use. A board that describes neither is unaffected;
+ * one that describes "inactive" without "default" is rejected, since
+ * releasing pins that were never put into a defined PPS state is incoherent.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl))
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+
+	/* The "inactive" state is optional; without it there is nothing to do. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 134

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +10 -0
+	}
+
+	/* "inactive" requires a "default" state to release back from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default))
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+
+	return 0;
+}

pinctrl_dt_to_map() returns -ENODEV when the node has no pinctrl-0 and create_pinctrl() forwards it. So this fails the probe on every pps-gpio board that describes no pinctrl at all, which is most of them; your setup has one, so the test does not show it. Shouldn't -ENODEV simply mean "no pinctrl here, nothing to do"?

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 145

Quoted 11 lines
drivers/pps/clients/pps-gpio.c +11 -0
+
+/*
+ * Release the pins to their "inactive" state, if the board describes one, so
+ * they are handed back to whatever function uses them while pps-gpio is not
+ * driving PPS. Boards without an "inactive" state are unaffected.
+ */
+static void pps_gpio_release_pins(struct pps_gpio_device_data *data)
+{
+	if (data->pins_inactive)
+		pinctrl_select_state(data->pinctrl, data->pins_inactive);
+}

The return value is the only sign that the mux was not restored, which is what the whole series is for. Why drop it?

Quoted 48 lines
drivers/pps/clients/pps-gpio.c +27 -0
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -161,6 +209,11 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
@@ -216,9 +269,30 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/* release the pins last, once nothing can drive them anymore */
+	pps_gpio_release_pins(data);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * Quiesce the hardware before touching the mux: stop the IRQ and the
+	 * echo timer first so nothing can drive the pins, then hand them back
+	 * to their "inactive" function. The kernel keeps running after
+	 * device_shutdown() (for example to load and start a kexec image), so
+	 * the pins must not be released while an IRQ or timer callback can
+	 * still reach them. The PPS source is left registered; unregistering
+	 * it is a remove-time concern and is unnecessary on shutdown.
+	 */
+	free_irq(data->irq, data);
+	timer_delete_sync(&data->echo_timer);
+	gpiod_set_value(data->echo_pin, 0);
+	pps_gpio_release_pins(data);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }

drivers/pps/clients/pps-gpio.c MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids); · line 308

Quoted 7 lines
drivers/pps/clients/pps-gpio.c +1 -0
@@ -228,6 +302,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,

Ciao,

Rodolfo

#9 lore raw

RE: [PATCH v2 1/2] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Rodolfo Giometti
Quoted 5 lines · Rodolfo Giometti

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 38

Quoted 2 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +2 -0
+      back to their alternate function. As "inactive" is the state to restore
+      after "default", it may only appear as the second entry.

The driver looks the states up by name, not by index, and only requires that "default" exists. Why you say that?

You're right, that was wrong. The driver looks both states up by name (pinctrl_lookup_state()), so "inactive" can be at any index; it only requires that a "default" state also exists. I'll drop the "second entry" wording.

Quoted 7 lines

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 42

Quoted 4 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

AFAIK the tuple rules out ["default", "sleep"] and ["init", "default"] for good, and this is ABI.

Agreed, the tuple is too strict and would bake that in. In v3 I drop it and only assert that a "default" state is present:

minItems: 1
contains:
const: default

so ["default", "sleep"], ["init", "default"], etc. all remain valid, and the description just notes that an optional "inactive" state, if present, requires "default" and may appear in any position.

Thanks,
Eliav

#13 lore raw

RE: [PATCH v2 2/2] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Rodolfo Giometti
Quoted 11 lines · Rodolfo Giometti

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 118

Quoted 5 lines
drivers/pps/clients/pps-gpio.c +5 -0
+static int pps_gpio_get_pins(struct device *dev) {
+     data->pinctrl = devm_pinctrl_get(dev);
+     if (IS_ERR(data->pinctrl))
+             return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+                                  "failed to get pinctrl\n");

pinctrl_dt_to_map() returns -ENODEV when the node has no pinctrl-0 and create_pinctrl() forwards it. So this fails the probe on every pps-gpio board that describes no pinctrl at all, which is most of them; your setup has one, so the test does not show it. Shouldn't -ENODEV simply mean "no pinctrl here, nothing to do"?

Agreed, that's a real regression for boards without pinctrl. In v3 -ENODEV is treated as "no pinctrl described", not an error, and everything else (including -EPROBE_DEFER) is still propagated:

        data->pinctrl = devm_pinctrl_get(dev);
        if (IS_ERR(data->pinctrl)) {
                /*
                 * A DT device without "pinctrl-0" yields -ENODEV, which
                 * is not an error here; propagate anything else.
                 */
                if (PTR_ERR(data->pinctrl) == -ENODEV) {
                        data->pinctrl = NULL;
                        return 0;
                }
                return dev_err_probe(dev, PTR_ERR(data->pinctrl),
                                     "failed to get pinctrl\n");
        }

I've also tested a pps-gpio node with no pinctrl at all to confirm probe, remove and shutdown behave as before.

Quoted 8 lines

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 145

Quoted 5 lines
drivers/pps/clients/pps-gpio.c +5 -0
+static void pps_gpio_release_pins(struct pps_gpio_device_data *data)
+{
+     if (data->pins_inactive)
+             pinctrl_select_state(data->pinctrl, data->pins_inactive);
+}

The return value is the only sign that the mux was not restored, which is what the whole series is for. Why drop it?

You're right. In v3 the failure is logged; the helper now takes the struct device so it can warn (remove()/shutdown() are void and the probe error path must keep the original error, so it warns rather than propagates):

        static void pps_gpio_release_pins(struct device *dev)
        {
                struct pps_gpio_device_data *data = dev_get_drvdata(dev);
                int ret;

                if (!data->pins_inactive)
                        return;

                ret = pinctrl_select_state(data->pinctrl, data->pins_inactive);
                if (ret)
                        dev_warn(dev, "failed to select inactive pinctrl state: %d\n",
                                 ret);
        }

Thanks,
Eliav

#14 lore raw

v3 cover pps-gpio: restore pin mux on unbind and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Patch 1 is a small preparatory fix to propagate the gpiod_to_irq() and request_threaded_irq() error codes (rather than a hardcoded -EINVAL) on the probe error paths that patch 3 then converts to gotos. Patch 2 documents the optional "default"/"inactive" pinctrl-names in the binding; patch 3 implements the driver side.

Tested on an AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding PBS pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state. Reading the mux register with the devices left unbound confirms the "inactive" mux persists. Also tested with a pps-gpio node that describes no pinctrl at all, where probe, remove and shutdown behave as before.

Changes in v3 (addressing Sashiko's and Rodolfo Giometti's review):
- New preparatory patch 1: propagate the gpiod_to_irq() and request_threaded_irq() error codes instead of overwriting them with -EINVAL, and log the errno on the request_threaded_irq() failure. The pinctrl patch only converts those returns into gotos, so fixing the discarded errors separately keeps them out of the pinctrl change
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple in the binding. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; the binding now only requires that a "default" state exists
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl described" rather than a probe failure. A DT device without a "pinctrl-0" property gets -ENODEV from the pinctrl core; that is expected, not an error. Real errors, including -EPROBE_DEFER, are still propagated
- Restore the "inactive" mux on probe failure too. The error paths after pps_gpio_get_pins() now go through a new err_release_pins label, so a probe that fails in gpiod_to_irq(), pps_register_source() or request_threaded_irq() no longer leaves the pins stuck in the core-applied "default" state
- Warn if applying the "inactive" state fails rather than ignoring the pinctrl_select_state() return silently
- Trim and de-duplicate the comments added in v2

Changes in v2 (all addressing Rodolfo Giometti's review):
- Rename the released state from "idle" to "inactive". "idle" is the runtime-PM state in pinctrl-state.h; overloading it for "driver not active" would clash with any future runtime PM or .suspend() and was being baked into the binding as ABI. "inactive" is not a well-known state, so no generic PM helper will ever auto-select it -- the driver drives it explicitly
- Look the state up in the driver (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state()) instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency so a CONFIG_PM=n kernel that describes an "inactive" state now honors it instead of silently doing nothing
- Fix shutdown(): tear down in the same order as remove() -- free_irq() and timer_delete_sync() first, the mux change last -- so no IRQ or echo timer callback can drive a pin after it has been handed back to another function; shutdown() no longer changes the mux while the hardware is still live
- Require a "default" state whenever "inactive" is present and reject the mismatch, rather than releasing pins that were never put into a defined PPS state

Eliav Farber (3):
pps: clients: gpio: propagate probe error codes
dt-bindings: pps: pps-gpio: document optional pinctrl states
pps: clients: gpio: release pins to an inactive state on remove and
shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     |  16 ++-
 drivers/pps/clients/pps-gpio.c                | 102 +++++++++++++++++-
 2 files changed, 113 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.3
#15 lore raw

PATCH v3 1/3 pps: clients: gpio: propagate probe error codes

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Return the actual error code from both paths, and add the errno to the request_threaded_irq() failure message.

---
Changes in v3:
- New patch, split out of the pinctrl change: while converting the probe error paths to a goto, Takashi Sakamoto noted that the hardcoded -EINVAL discards the real gpiod_to_irq()/request_threaded_irq() error, so fix that separately first

 drivers/pps/clients/pps-gpio.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +3 -3
index 73ec2c7335e5..038c55c5f7d4 100644
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
+		return ret;
 	}
 	data->irq = ret;
 
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+		return ret;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
Signature · 1 line
2.47.3
#19 lore raw

PATCH v3 2/3 dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. The driver looks the states up by name, so "inactive" may appear in any position; it only requires that a "default" state also exists.

---
Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml        | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +15 -1
index 383a838744eb..db6ecb17cb54 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the standard
+      "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. If a state named "inactive" is also present,
+      it is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function. The "inactive"
+      state, if used, requires a "default" state; its position among the
+      names does not matter.
+    minItems: 1
+    contains:
+      const: default
+
 required:
   - compatible
   - gpios
@@ -40,8 +53,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#24 lore raw

PATCH v3 3/3 pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() and timer_delete_sync() first, and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

---
Changes in v3:
- Treat -ENODEV from devm_pinctrl_get() (a DT device without a "pinctrl-0" property) as "no pinctrl", not a probe failure; keep propagating everything else, e.g. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure too: route the error paths after pps_gpio_get_pins() through a new err_release_pins label so a failed probe does not leave the pins stuck in the "default" state
- Warn if pinctrl_select_state() fails to apply the "inactive" state rather than silently ignoring the error (pps_gpio_release_pins() now takes the struct device to log against)
- Trim and de-duplicate the added comments

Changes in v2:
- Rename the released state from "idle" to "inactive"
- Look the state up in the driver with devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state() instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown() to free_irq() and timer_delete_sync() before the mux change, matching remove(), so no IRQ or timer callback can drive a pin after it has been handed back
- Require a "default" state whenever "inactive" is present and reject the mismatch

 drivers/pps/clients/pps-gpio.c | 100 ++++++++++++++++++++++++++++++++-
 1 file changed, 97 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +97 -3
index 038c55c5f7d4..2ee8fba8520c 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,66 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state. It requires a "default"
+ * state (applied by the driver core before probe) and is rejected without
+ * one. Absent pinctrl, or an absent "inactive" state, is not an error.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl)) {
+		/*
+		 * A DT device without "pinctrl-0" yields -ENODEV, which
+		 * is not an error here; propagate anything else.
+		 */
+		if (PTR_ERR(data->pinctrl) == -ENODEV) {
+			data->pinctrl = NULL;
+			return 0;
+		}
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+	}
+
+	/* The "inactive" state is optional. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}
+
+	/* "inactive" requires a "default" state to return from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default))
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+
+	return 0;
+}
+
+/*
+ * Restore the "inactive" pinctrl state, handing the pins back to whatever
+ * function uses them while pps-gpio is not driving PPS. This undoes the
+ * "default" state the driver core applied before probe. A no-op for boards
+ * that describe no "inactive" state.
+ */
+static void pps_gpio_release_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	int ret;
+
+	if (!data->pins_inactive)
+		return;
+
+	ret = pinctrl_select_state(data->pinctrl, data->pins_inactive);
+	if (ret)
+		dev_warn(dev, "failed to select inactive pinctrl state: %d\n",
+			 ret);
+}
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -161,11 +224,16 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return ret;
+		goto err_release_pins;
 	}
 	data->irq = ret;
 
@@ -187,7 +255,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (IS_ERR(data->pps)) {
 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
 			data->irq);
-		return PTR_ERR(data->pps);
+		ret = PTR_ERR(data->pps);
+		goto err_release_pins;
 	}
 
 	/* register IRQ interrupt handler */
@@ -198,13 +267,18 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret) {
 		pps_unregister_source(data->pps);
 		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
-		return ret;
+		goto err_release_pins;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
 		data->irq);
 
 	return 0;
+
+err_release_pins:
+	/* Restore the inactive mux on probe failure; safe to do last here. */
+	pps_gpio_release_pins(dev);
+	return ret;
 }
 
 static void pps_gpio_remove(struct platform_device *pdev)
@@ -216,9 +290,28 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/* release the pins last, once nothing can drive them */
+	pps_gpio_release_pins(&pdev->dev);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * The kernel keeps running after device_shutdown() (e.g. to load and
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	timer_delete_sync(&data->echo_timer);
+	gpiod_set_value(data->echo_pin, 0);
+	pps_gpio_release_pins(&pdev->dev);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +321,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
#16 lore raw

Re: [PATCH v3 1/3] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] pps: log spam on deferred probe due to dev_err() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] pps: log spam on deferred probe due to dev_err()
--

commit 7ffb1c8320d0b6933d1ffc7e8877c00811256618
Author: Eliav Farber <[email protected]>

pps: clients: gpio: propagate probe error codes

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 168

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +1 -1
index 73ec2c7335e51..038c55c5f7d4a 100644
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
+		return ret;

[Severity: Low]
Does propagating ret here risk console log spam if -EPROBE_DEFER is returned? Since dev_err() is used unconditionally, it could cause log spam during system boot if probe deferrals happen repeatedly.

Would dev_err_probe() be more appropriate to handle deferred probes silently?

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 201

Quoted 11 lines
drivers/pps/clients/pps-gpio.c +2 -2
 	}
 	data->irq = ret;
 
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+		return ret;

[Severity: Low]
Similarly, does propagating the real error here risk logging spam on deferred probes? Repeatedly deferring could trigger this dev_err() multiple times.

Would dev_err_probe() be more appropriate here as well?

Quoted 1 line
drivers/pps/clients/pps-gpio.c +0 -0
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#25 lore raw

Re: [PATCH v3 3/3] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] pps: probe failure in pps_gpio_setup() leaves pins in default state --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] pps: probe failure in pps_gpio_setup() leaves pins in default state --

commit c39f475f46d4d918a014f1945bfb60ecc434ce54
Author: Eliav Farber <[email protected]>

pps: clients: gpio: release pins to an inactive state on remove and shutdown

This commit introduces logic to transition PPS GPIO pins to an "inactive" pinctrl state when the driver is unbound, shut down, or fails to probe.

Quoted 2 lines
drivers/pps/clients/pps-gpio.c +0 -0

[ ... ]

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 225

Quoted 3 lines
drivers/pps/clients/pps-gpio.c +0 -0
@@ -161,11 +224,16 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;

[Severity: Medium]
If pps_gpio_setup() fails here, will this early return bypass the newly introduced err_release_pins label? Since the pinctrl "default" state is applied by the driver core before probe(), it seems returning directly here might leave the hardware pins stuck in the "default" state instead of putting them into the intended "inactive" state on probe failure.

Could pps_gpio_get_pins() be moved earlier in the probe sequence so that the inactive state is available to be restored consistently across all failure paths?

Quoted 8 lines
drivers/pps/clients/pps-gpio.c +5 -0
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v3 1/3
#17 lore raw

Re: [PATCH v3 1/3] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Quoted 11 lines · Eliav Farber

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Return the actual error code from both paths, and add the errno to the request_threaded_irq() failure message.

---

#20 lore raw

Re: [PATCH v3 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 21 lines · Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. The driver looks the states up by name, so "inactive" may appear in any position; it only requires that a "default" state also exists.

---
Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Quoted 6 lines

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml        | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/pps/pps-gpio.example.dtb: pps (pps-gpio): pinctrl-names: ['default', 'inactive'] is too long
from schema $id: http://devicetree.org/schemas/pps/pps-gpio.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.kernel.org/project/devicetree/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

maintainer
#21 lore raw

Re: [PATCH v3 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 34 lines · Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. The driver looks the states up by name, so "inactive" may appear in any position; it only requires that a "default" state also exists.

---
Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml        | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +0 -0
index 383a838744eb..db6ecb17cb54 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 39

Quoted 10 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +9 -0
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the standard
+      "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. If a state named "inactive" is also present,
+      it is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function. The "inactive"
+      state, if used, requires a "default" state; its position among the
+      names does not matter.

Don't express in prose what can be defined in schema.

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 42

Quoted 3 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +3 -0
+    minItems: 1
+    contains:
+      const: default

minItems: 1
items:
- const: default
- const: inactive

And yes, position does matter. pinctrl-0 was already implicitly defined as 'default'. You can't change it to pinctrl-1 now.

Rob

maintainer
#18 lore raw

Re: [PATCH v3 1/3] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Quoted 19 lines · Eliav Farber

Return the actual error code from both paths, and add the errno to the request_threaded_irq() failure message.

---

drivers/pps/clients/pps-gpio.c +1 -1
index 73ec2c7335e5..038c55c5f7d4 100644
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
+		return ret;
 	}
 	data->irq = ret;
 

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 204

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +2 -2
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+		return ret;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",

Looks good to me.

Since this restores the -EPROBE_DEFER that gpiod_to_irq() can return, which the hardcoded -EINVAL was turning into a permanent probe failure, it is probably worth adding:

Fixes 4461d65176b4 ("pps: descriptor-based gpio")

Ciao,

Rodolfo

maintainer
#22 lore raw

Re: [PATCH v3 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 30 lines · Eliav Farber

Document the optional "default" and "inactive" pinctrl-names and show both in the example.

---

Documentation/devicetree/bindings/pps/pps-gpio.yaml +13 -0
index 383a838744eb..db6ecb17cb54 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the standard
+      "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. If a state named "inactive" is also present,
+      it is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function. The "inactive"
+      state, if used, requires a "default" state; its position among the
+      names does not matter.
+    minItems: 1
+    contains:
+      const: default
+
 required:
   - compatible
   - gpios

Documentation/devicetree/bindings/pps/pps-gpio.yaml examples: · line 61

Quoted 10 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +2 -1
@@ -40,8 +53,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;

I tried:

  $ make DT_SCHEMA_FILES=Documentation/devicetree/bindings/pps/pps-gpio.yaml \
         dt_binding_check

and it gave me:

pps-gpio.example.dtb: pps (pps-gpio): pinctrl-names:
['default', 'inactive'] is too long
from schema $id: http://devicetree.org/schemas/pps/pps-gpio.yaml

Ciao,

Rodolfo

maintainer
#23 lore raw

Re: [PATCH v3 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

Quoted 44 lines · Rodolfo Giometti
Quoted 30 lines · Eliav Farber

Document the optional "default" and "inactive" pinctrl-names and show both in the example.

---

Documentation/devicetree/bindings/pps/pps-gpio.yaml +13 -0
index 383a838744eb..db6ecb17cb54 100644
@@ -28,6 +28,19 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      When the PPS input is muxed through a pin controller, the standard
+      "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. If a state named "inactive" is also present,
+      it is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function. The "inactive"
+      state, if used, requires a "default" state; its position among the
+      names does not matter.
+    minItems: 1
+    contains:
+      const: default
+
 required:
   - compatible
   - gpios

Documentation/devicetree/bindings/pps/pps-gpio.yaml examples: · line 61

Quoted 10 lines
Documentation/devicetree/bindings/pps/pps-gpio.yaml +2 -1
@@ -40,8 +53,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;

I tried:

Quoted 8 lines
  $ make DT_SCHEMA_FILES=Documentation/devicetree/bindings/pps/pps-gpio.yaml \
         dt_binding_check

and it gave me:

pps-gpio.example.dtb: pps (pps-gpio): pinctrl-names:
['default', 'inactive'] is too long
from schema $id: http://devicetree.org/schemas/pps/pps-gpio.yaml

I can't help you without the schema you used. Sounds like something wrong with 'items'.

Rob

#26 lore raw

v4 cover pps-gpio: restore pin mux on unbind and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Patch 1 is a small preparatory fix to propagate the gpiod_to_irq() and request_threaded_irq() error codes (rather than a hardcoded -EINVAL) on the probe error paths that patch 3 then converts to gotos. Patch 2 documents the optional "default"/"inactive" pinctrl-names in the binding; patch 3 implements the driver side.

Tested on an AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding PBS pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state. Reading the mux register with the devices left unbound confirms the "inactive" mux persists. Also tested with a pps-gpio node that describes no pinctrl at all, where probe, remove and shutdown behave as before.

Open question for the maintainers (patch 3): a probe that fails before pps_gpio_get_pins() has run -- devm_kzalloc() or pps_gpio_setup() -- returns without going through err_release_pins, so the pins are left in the core-applied "default" state rather than "inactive". I did not change this in v4 as I would like your guidance on the preferred approach; the trade-offs are laid out at the end of patch 3's changelog. In short:

A. Move pps_gpio_get_pins() to the top of probe and route the
pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". (The devm_kzalloc() failure is inherently before the driver holds any pinctrl handle, so it cannot be covered by the driver in any option.)
B. Keep v4 as-is and treat "inactive" as a successful-ownership concern:
a probe that never looked up the state never took the pins, so leaving the core-applied "default" (long-standing behaviour) is acceptable.
C. As A, but keep the release helper's existing NULL guard so it is robust
regardless of ordering (belt and braces).

I lean towards B (the restore is meaningful only once the driver has taken ownership), but I am happy to implement A/C if you prefer uniform failure paths.

Changes in v4:
- Patch 1: add "Fixes: 161520451dfa (\"pps: new client driver using GPIO\")" and Bartosz Golaszewski's Reviewed-by. See the reply on the v3 1/3 thread for why the tag points at the original driver rather than the descriptor conversion (4461d65176b4) Rodolfo suggested: the hardcoded -EINVAL on both error paths predates 4461d65176b4, which only switched gpio_to_irq() to gpiod_to_irq() and left those returns as context
- Patch 2: rework the binding per Rob Herring. Do not express the ordering in prose; use an ordered "items" list ("default" then "inactive") with minItems: 1, since pinctrl-0 is already implicitly "default" and its position is fixed. This also fixes the "['default', 'inactive'] is too long" dt_binding_check error seen on v3. Reword the commit message accordingly
- Patch 3: unchanged from v3

Changes in v3 (addressing Sashiko's and Rodolfo Giometti's review):
- New preparatory patch 1: propagate the gpiod_to_irq() and request_threaded_irq() error codes instead of overwriting them with -EINVAL, and log the errno on the request_threaded_irq() failure. The pinctrl patch only converts those returns into gotos, so fixing the discarded errors separately keeps them out of the pinctrl change
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple in the binding. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; the binding now only requires that a "default" state exists
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl described" rather than a probe failure. A DT device without a "pinctrl-0" property gets -ENODEV from the pinctrl core; that is expected, not an error. Real errors, including -EPROBE_DEFER, are still propagated
- Restore the "inactive" mux on probe failure too. The error paths after pps_gpio_get_pins() now go through a new err_release_pins label, so a probe that fails in gpiod_to_irq(), pps_register_source() or request_threaded_irq() no longer leaves the pins stuck in the core-applied "default" state
- Warn if applying the "inactive" state fails rather than ignoring the pinctrl_select_state() return silently
- Trim and de-duplicate the comments added in v2

Changes in v2 (all addressing Rodolfo Giometti's review):
- Rename the released state from "idle" to "inactive". "idle" is the runtime-PM state in pinctrl-state.h; overloading it for "driver not active" would clash with any future runtime PM or .suspend() and was being baked into the binding as ABI. "inactive" is not a well-known state, so no generic PM helper will ever auto-select it -- the driver drives it explicitly
- Look the state up in the driver (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state()) instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency so a CONFIG_PM=n kernel that describes an "inactive" state now honors it instead of silently doing nothing
- Fix shutdown(): tear down in the same order as remove() -- free_irq() and timer_delete_sync() first, the mux change last -- so no IRQ or echo timer callback can drive a pin after it has been handed back to another function; shutdown() no longer changes the mux while the hardware is still live
- Require a "default" state whenever "inactive" is present and reject the mismatch, rather than releasing pins that were never put into a defined PPS state

Eliav Farber (3):
pps: clients: gpio: propagate probe error codes
dt-bindings: pps: pps-gpio: document optional pinctrl states
pps: clients: gpio: release pins to an inactive state on remove and
shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     |  14 ++-
 drivers/pps/clients/pps-gpio.c                | 102 +++++++++++++++++-
 2 files changed, 111 insertions(+), 5 deletions(-)

base-commit: 4982d3552a3bf94de503acf93433277d08421de6

Signature · 1 line
2.47.3
#58 lore raw

PATCH v4 1/3 pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Return the actual error code from both paths, and add the errno to the request_threaded_irq() failure message.

Fixes 161520451dfa ("pps: new client driver using GPIO")

---
Changes in v4:
- Add Fixes: 161520451dfa ("pps: new client driver using GPIO") and Bartosz Golaszewski's Reviewed-by. The hardcoded -EINVAL on both error paths predates 4461d65176b4 (which only switched gpio_to_irq() to gpiod_to_irq() and left those returns as context), so the tag points at the original driver rather than the descriptor conversion

Changes in v3:
- New patch, split out of the pinctrl change: while converting the probe error paths to a goto, Takashi Sakamoto noted that the hardcoded -EINVAL discards the real gpiod_to_irq()/request_threaded_irq() error, so fix that separately first

 drivers/pps/clients/pps-gpio.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +3 -3
index 73ec2c7335e5..038c55c5f7d4 100644
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
+		return ret;
 	}
 	data->irq = ret;
 
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+		return ret;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
Signature · 1 line
2.47.3
#63 lore raw

PATCH v4 2/3 dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. "default" is pinctrl-0, matching the implicit ordering the pinctrl core already assigns it, and "inactive" is pinctrl-1; the driver looks each state up by name.

---
Changes in v4:
- Rework per Rob Herring: do not express the ordering in prose; use an ordered "items" list ("default" then "inactive") with minItems: 1, since pinctrl-0 is already implicitly "default" and its position is fixed. This also fixes the "['default', 'inactive'] is too long" dt_binding_check error seen on v3. Reword the commit message accordingly

Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml          | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +13 -1
index 383a838744eb..61b5de6724ea 100644
@@ -28,6 +28,17 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      The "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. The optional "inactive" state, when present,
+      is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function.
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive
+
 required:
   - compatible
   - gpios
@@ -40,8 +51,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#66 lore raw

PATCH v4 3/3 pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() and timer_delete_sync() first, and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

---
Changes in v4:
- No functional change. Raising an open question on the probe-failure path for maintainer guidance rather than changing it (Sashiko [Medium] on v3 3/3): a probe that fails before pps_gpio_get_pins() has run (devm_kzalloc() or pps_gpio_setup()) returns without going through err_release_pins, so the pins are left in the core-applied "default" state rather than "inactive". Note the pins are physically muxed to "default" by the driver core before probe(), and devres release on a failed probe frees the pinctrl handle without selecting a prior state, so the "default" mux persists. Options:
A. Move pps_gpio_get_pins() to the top of probe and route the
pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". The devm_kzalloc() failure is inherently before the driver holds any pinctrl handle, so no option can cover it in the driver.
B. Keep as-is: treat "inactive" as a successful-ownership concern. A
probe that never looked up the state never took the pins, so leaving the long-standing core-applied "default" is acceptable.
C. As A, but keep the release helper's NULL guard so it is robust
regardless of ordering.
I lean towards B; happy to implement A/C if uniform failure paths are preferred.

Changes in v3:
- Treat -ENODEV from devm_pinctrl_get() (a DT device without a "pinctrl-0" property) as "no pinctrl", not a probe failure; keep propagating everything else, e.g. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure too: route the error paths after pps_gpio_get_pins() through a new err_release_pins label so a failed probe does not leave the pins stuck in the "default" state
- Warn if pinctrl_select_state() fails to apply the "inactive" state rather than silently ignoring the error (pps_gpio_release_pins() now takes the struct device to log against)
- Trim and de-duplicate the added comments

Changes in v2:
- Rename the released state from "idle" to "inactive"
- Look the state up in the driver with devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state() instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown() to free_irq() and timer_delete_sync() before the mux change, matching remove(), so no IRQ or timer callback can drive a pin after it has been handed back
- Require a "default" state whenever "inactive" is present and reject the mismatch

 drivers/pps/clients/pps-gpio.c | 100 ++++++++++++++++++++++++++++++++-
 1 file changed, 97 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +97 -3
index 038c55c5f7d4..2ee8fba8520c 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,66 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state. It requires a "default"
+ * state (applied by the driver core before probe) and is rejected without
+ * one. Absent pinctrl, or an absent "inactive" state, is not an error.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl)) {
+		/*
+		 * A DT device without "pinctrl-0" yields -ENODEV, which
+		 * is not an error here; propagate anything else.
+		 */
+		if (PTR_ERR(data->pinctrl) == -ENODEV) {
+			data->pinctrl = NULL;
+			return 0;
+		}
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+	}
+
+	/* The "inactive" state is optional. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}
+
+	/* "inactive" requires a "default" state to return from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default))
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+
+	return 0;
+}
+
+/*
+ * Restore the "inactive" pinctrl state, handing the pins back to whatever
+ * function uses them while pps-gpio is not driving PPS. This undoes the
+ * "default" state the driver core applied before probe. A no-op for boards
+ * that describe no "inactive" state.
+ */
+static void pps_gpio_release_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	int ret;
+
+	if (!data->pins_inactive)
+		return;
+
+	ret = pinctrl_select_state(data->pinctrl, data->pins_inactive);
+	if (ret)
+		dev_warn(dev, "failed to select inactive pinctrl state: %d\n",
+			 ret);
+}
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -161,11 +224,16 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	/* pinctrl setup (optional states) */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return ret;
+		goto err_release_pins;
 	}
 	data->irq = ret;
 
@@ -187,7 +255,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (IS_ERR(data->pps)) {
 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
 			data->irq);
-		return PTR_ERR(data->pps);
+		ret = PTR_ERR(data->pps);
+		goto err_release_pins;
 	}
 
 	/* register IRQ interrupt handler */
@@ -198,13 +267,18 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (ret) {
 		pps_unregister_source(data->pps);
 		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
-		return ret;
+		goto err_release_pins;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
 		data->irq);
 
 	return 0;
+
+err_release_pins:
+	/* Restore the inactive mux on probe failure; safe to do last here. */
+	pps_gpio_release_pins(dev);
+	return ret;
 }
 
 static void pps_gpio_remove(struct platform_device *pdev)
@@ -216,9 +290,28 @@ static void pps_gpio_remove(struct platform_device *pdev)
 	timer_delete_sync(&data->echo_timer);
 	/* reset echo pin in any case */
 	gpiod_set_value(data->echo_pin, 0);
+	/* release the pins last, once nothing can drive them */
+	pps_gpio_release_pins(&pdev->dev);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * The kernel keeps running after device_shutdown() (e.g. to load and
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	timer_delete_sync(&data->echo_timer);
+	gpiod_set_value(data->echo_pin, 0);
+	pps_gpio_release_pins(&pdev->dev);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +321,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
maintainer
#27 lore raw
In reply to Eliav Farber
Quoted 18 lines · Eliav Farber

Open question for the maintainers (patch 3): a probe that fails before pps_gpio_get_pins() has run -- devm_kzalloc() or pps_gpio_setup() -- returns without going through err_release_pins, so the pins are left in the core-applied "default" state rather than "inactive". I did not change this in v4 as I would like your guidance on the preferred approach; the trade-offs are laid out at the end of patch 3's changelog. In short:

A. Move pps_gpio_get_pins() to the top of probe and route the
pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". (The devm_kzalloc() failure is inherently before the driver holds any pinctrl handle, so it cannot be covered by the driver in any option.)
B. Keep v4 as-is and treat "inactive" as a successful-ownership concern:
a probe that never looked up the state never took the pins, so leaving the core-applied "default" (long-standing behaviour) is acceptable.
C. As A, but keep the release helper's existing NULL guard so it is robust
regardless of ordering (belt and braces).

Quoted 3 lines

I lean towards B (the restore is meaningful only once the driver has taken ownership), but I am happy to implement A/C if you prefer uniform failure paths.

I would choose option A, keeping the NULL check in the release helper; I believe this corresponds to your option C. In fact, to me, it represents the symmetrical counterpart to what the core did on the driver's behalf.

One thing I would verify on your board before proceeding is this: with option A, an -EPROBE_DEFER error returned by pps_gpio_setup() would result in the "inactive" state being selected, and the core would re-apply "default" on the next attempt. I assume this mux switching is harmless, but the hardware is available to you, not me.

I wouldn't worry too much about the devm_kzalloc() case.

I also have a couple of observations regarding patches 1/3 and 3/3; I will send them directly in response to the patches themselves.

Ciao,

Rodolfo

maintainer
#64 lore raw

Re: [PATCH v4 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

Just to close what I reported on v3: I ran the binding check on this one and the "['default', 'inactive'] is too long" failure is gone, so consider my objection withdrawn. The binding itself is Rob's call.

Ciao,

Rodolfo

maintainer
#59 lore raw

Re: [PATCH v4 1/3] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Quoted 3 lines · Eliav Farber
Fixes 161520451dfa ("pps: new client driver using GPIO")

You are right and I was wrong on v3. I went and looked: the hardcoded -EINVAL on the gpio_to_irq() path is already there in 161520451dfa, and 4461d65176b4 only turned gpio_to_irq() into gpiod_to_irq() while leaving those returns alone. The tag points where it should.

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 170

Quoted 8 lines
drivers/pps/clients/pps-gpio.c +1 -1
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	ret = gpiod_to_irq(data->gpio_pin);
 	if (ret < 0) {
 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
+		return ret;
 	}
 	data->irq = ret;

Now that the real error survives, -EPROBE_DEFER survives with it -- and this dev_err() will then shout once per retry, which is precisely what dev_err_probe() exists to avoid. Wouldn't it be the right thing here? pps_gpio_setup() in this same file already uses it, and so does pps_gpio_get_pins() in your patch 3, so it would also leave the file consistent with itself.

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 202

Quoted 9 lines
drivers/pps/clients/pps-gpio.c +2 -2
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+		return ret;
 	}

Same question here, although I agree a deferral is far less likely on this one.

Either way this is a nit, not an objection: the patch is right as it stands.

Ciao,

Rodolfo

maintainer
#67 lore raw

Re: [PATCH v4 3/3] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber
Quoted 6 lines · Eliav Farber
drivers/pps/clients/pps-gpio.c +6 -0
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * The kernel keeps running after device_shutdown() (e.g. to load and

drivers/pps/clients/pps-gpio.c static void pps_gpio_remove(struct platform_device *pdev) · line 313

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +10 -0
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	timer_delete_sync(&data->echo_timer);
+	gpiod_set_value(data->echo_pin, 0);
+	pps_gpio_release_pins(&pdev->dev);
+}

The ordering argument convinced me, and I checked it against remove():
free_irq() really is the first thing remove() does, so releasing the pins last is safe in both. Good.

The timer_delete_sync() worries me a little, though. timer_setup() only runs when data->echo_pin is set, so on a board without echo-gpios this touches a timer_list that was never initialised. remove() has carried the same line for years, but remove() only happens on an unbind, which hardly anyone does; .shutdown runs on every reboot and every kexec, on every pps-gpio board there is. And as far as I can tell no in-tree DT using pps-gpio describes echo-gpios at all, so that is the ordinary case rather than the corner one.

Would you mind guarding it with if (data->echo_pin)? The gpiod_set_value() beside it is already a no-op for a NULL descriptor, so that one is fine as it stands. Whether you want to give remove() the same guard while you are there is up to you -- it is pre-existing, so I would not insist on it in this series.

drivers/pps/clients/pps-gpio.c static void pps_gpio_echo_timer_callback(struct timer_list *t) · line 107

Quoted 6 lines
drivers/pps/clients/pps-gpio.c +6 -0
+/*
+ * Look up the optional "inactive" pinctrl state. It requires a "default"
+ * state (applied by the driver core before probe) and is rejected without
+ * one. Absent pinctrl, or an absent "inactive" state, is not an error.
+ */
+static int pps_gpio_get_pins(struct device *dev)

The rest of the patch reads well to me, and the comments are the right length now. My answer on the probe-failure question is on the cover letter.

Ciao,

Rodolfo

#60 lore raw

RE: [PATCH v4 1/3] pps: clients: gpio: propagate probe error codes

In reply to Rodolfo Giometti
Quoted 7 lines · Rodolfo Giometti
Quoted 7 lines
drivers/pps/clients/pps-gpio.c +1 -1
@@ -165,7 +165,7 @@ static int pps_gpio_probe(struct platform_device *pdev)
      ret = gpiod_to_irq(data->gpio_pin);
      if (ret < 0) {
              dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-             return -EINVAL;
+             return ret;
      }
Quoted 8 lines

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 170

Quoted 1 line
drivers/pps/clients/pps-gpio.c +0 -0
      data->irq = ret;

Now that the real error survives, -EPROBE_DEFER survives with it -- and this dev_err() will then shout once per retry, which is precisely what dev_err_probe() exists to avoid. Wouldn't it be the right thing here? pps_gpio_setup() in this same file already uses it, and so does pps_gpio_get_pins() in your patch 3, so it would also leave the file consistent with itself.

Agreed on silencing the deferral spam. I avoided return dev_err_probe() because patch 3 turns both returns into goto err_release_pins, and dev_err_probe()'s idiom is to return the error, not goto.

To get the same suppression without the return, I'd open-code what its kernel-doc says it replaces, minus the return:

        if (ret != -EPROBE_DEFER)
                dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
        else
                dev_dbg(dev, "failed to map GPIO to IRQ: %d\n", ret);
        goto err_release_pins;

Would you like me to change it to this?

Quoted 4 lines
Quoted 4 lines
drivers/pps/clients/pps-gpio.c +0 -0
@@ -197,8 +197,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
                        data->info.name, data);
      if (ret) {
              pps_unregister_source(data->pps);
Quoted 8 lines

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 202

Quoted 5 lines
drivers/pps/clients/pps-gpio.c +2 -2
-             dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-             return -EINVAL;
+             dev_err(dev, "failed to acquire IRQ %d: %d\n", data->irq, ret);
+             return ret;
      }

Same question here, although I agree a deferral is far less likely on this one.

Same treatment here if you want it.

Thanks,
Eliav

maintainer
#61 lore raw

Re: [PATCH v4 1/3] pps: clients: gpio: propagate probe error codes

Quoted 3 lines · Eliav

Agreed on silencing the deferral spam. I avoided return dev_err_probe() because patch 3 turns both returns into goto err_release_pins, and dev_err_probe()'s idiom is to return the error, not goto.

You don't have to return it. Calling it just for the message and then jumping is fine, and ret already carries the code the label needs:

	if (ret < 0) {
		dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
		goto err_release_pins;
	}

But please don't open-code it: on the -EPROBE_DEFER branch it also calls device_set_deferred_probe_reason(), so the hand-written version silently drops the reason from /sys/kernel/debug/devices_deferred.

One more thing: dev_err_probe() already prints the code as %pe, so drop the ": %d" from both messages -- data->irq of course stays in the second one.

In this patch it can still be a plain

	return dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");

and patch 3 turns it into the goto form above. Same for the request_irq one.

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming
#62 lore raw

PATCH v4 1/3 pps: clients: gpio: propagate probe error codes

Quoted 8 lines · Rodolfo Giometti
Quoted 3 lines · Eliav

Agreed on silencing the deferral spam. I avoided return dev_err_probe() because patch 3 turns both returns into goto err_release_pins, and dev_err_probe()'s idiom is to return the error, not goto.

You don't have to return it. Calling it just for the message and then jumping is fine, and ret already carries the code the label needs:

Quoted 8 lines
        if (ret < 0) {
                dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
                goto err_release_pins;
        }

But please don't open-code it: on the -EPROBE_DEFER branch it also calls device_set_deferred_probe_reason(), so the hand-written version silently drops the reason from /sys/kernel/debug/devices_deferred.

Done in v5: this patch uses return dev_err_probe(), and the pinctrl patch turns it into the log-only dev_err_probe() + goto form you show above. Also dropped the ": %d" from both messages (kept data->irq).

Thanks,
Eliav

#28 lore raw
In reply to Rodolfo Giometti
Quoted 19 lines · Rodolfo Giometti
Quoted 18 lines · Eliav Farber

Open question for the maintainers (patch 3): a probe that fails before pps_gpio_get_pins() has run -- devm_kzalloc() or pps_gpio_setup() -- returns without going through err_release_pins, so the pins are left in the core-applied "default" state rather than "inactive". I did not change this in v4 as I would like your guidance on the preferred approach; the trade-offs are laid out at the end of patch 3's changelog. In short:

A. Move pps_gpio_get_pins() to the top of probe and route the
pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". (The devm_kzalloc() failure is inherently before the driver holds any pinctrl handle, so it cannot be covered by the driver in any option.)
B. Keep v4 as-is and treat "inactive" as a successful-ownership concern:
a probe that never looked up the state never took the pins, so leaving the core-applied "default" (long-standing behaviour) is acceptable.
C. As A, but keep the release helper's existing NULL guard so it is robust
regardless of ordering (belt and braces).

Quoted 8 lines
Quoted 3 lines

I lean towards B (the restore is meaningful only once the driver has taken ownership), but I am happy to implement A/C if you prefer uniform failure paths.

I would choose option A, keeping the NULL check in the release helper; I believe this corresponds to your option C. In fact, to me, it represents the symmetrical counterpart to what the core did on the driver's behalf.

Done in v5: pps_gpio_get_pins() now runs first in probe(), the pps_gpio_setup() failure goes through err_release_pins, and the release helper keeps its NULL guard (option C).

Quoted 5 lines

One thing I would verify on your board before proceeding is this: with option A, an -EPROBE_DEFER error returned by pps_gpio_setup() would result in the "inactive" state being selected, and the core would re-apply "default" on the next attempt. I assume this mux switching is harmless, but the hardware is available to you, not me.

Verified on the AL11 K2V6 JRD10: I forced pps_gpio_setup() to return -EPROBE_DEFER a few times and watched the pin-mux register. Each failed attempt releases the pins to "inactive", the core re-applies "default" before the next, and it settles at "default" once probe succeeds -- no spurious PPS event or warning across the cycles.

Thanks,
Eliav

#65 lore raw

RE: [PATCH v4 2/3] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Rodolfo Giometti
Quoted 9 lines · Rodolfo Giometti

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

Just to close what I reported on v3: I ran the binding check on this one and the "['default', 'inactive'] is too long" failure is gone, so consider my objection withdrawn. The binding itself is Rob's call.

Right, the v4 rework to the ordered "items" list (per Rob) fixed that. v5 carries the binding unchanged.

Thanks,
Eliav

#68 lore raw

RE: [PATCH v4 3/3] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Rodolfo Giometti
Quoted 22 lines · Rodolfo Giometti
Quoted 6 lines · Eliav Farber
drivers/pps/clients/pps-gpio.c +6 -0
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+     struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+     /*
+      * The kernel keeps running after device_shutdown() (e.g. to load and

drivers/pps/clients/pps-gpio.c static void pps_gpio_remove(struct platform_device *pdev) · line 313

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +10 -0
+      * start a kexec image), so quiesce the hardware before touching the
+      * mux: free the IRQ and stop the echo timer first, then release the
+      * pins last, so no callback can drive a pin after it is handed back.
+      * The PPS source is left registered; that is a remove-time concern.
+      */
+     free_irq(data->irq, data);
+     timer_delete_sync(&data->echo_timer);
+     gpiod_set_value(data->echo_pin, 0);
+     pps_gpio_release_pins(&pdev->dev);
+}

The timer_delete_sync() worries me a little, though. timer_setup() only runs when data->echo_pin is set, so on a board without echo-gpios this touches a timer_list that was never initialised.

Quoted 4 lines

Would you mind guarding it with if (data->echo_pin)? [...] Whether you want to give remove() the same guard while you are there is up to you -- it is pre-existing, so I would not insist on it in this series.

Done in v5. shutdown() now guards the echo teardown with data->echo_pin, and I fixed remove() too -- as a separate preparatory patch, since it is pre-existing (Fixes: fde046a8c490, which had dropped that guard). It also trips debug_assert_init() under CONFIG_DEBUG_OBJECTS_TIMERS, not just an uninitialised-timer concern.

Thanks,
Eliav

#29 lore raw

v5 cover pps-gpio: restore pin mux on unbind and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Patch 1 is a small preparatory fix to propagate the gpiod_to_irq() and request_threaded_irq() error codes (rather than a hardcoded -EINVAL) on the probe error paths that patch 4 then converts to gotos. Patch 2 is a small preparatory fix restoring the echo-GPIO guard in remove() that a later cleanup dropped, since patch 4 mirrors remove()'s teardown in the new shutdown() and would otherwise carry the same latent issue. Patch 3 documents the optional "default"/"inactive" pinctrl-names in the binding; patch 4 implements the driver side.

Tested on an Amazon AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state. Reading the mux register with the device left unbound confirms the "inactive" mux persists. Also tested with a pps-gpio node that describes no pinctrl at all, where probe, remove and shutdown behave as before.

For the probe-failure path (see below), a forced-defer test confirmed that a probe failing after pps_gpio_get_pins() releases the pins to "inactive", the core re-applies "default" before the next attempt, and the mux settles back at "default" once probe finally succeeds, with no spurious PPS event or warning across the cycles.

Changes in v5:
- Patch 1: use dev_err_probe() on both error paths instead of a bare dev_err() + return, so the propagated -EPROBE_DEFER is logged at debug level rather than spamming the console and the code is emitted symbolically. Drop Bartosz's Reviewed-by as the patch changed materially (Rodolfo Giometti)
- New patch 2: restore the data->echo_pin guard around the echo-timer teardown in remove(), dropped by commit fde046a8c490. timer_delete_sync() on a never-initialised timer trips debug_assert_init() under CONFIG_DEBUG_OBJECTS_TIMERS. Split out so patch 4's new shutdown() does not reintroduce it (Rodolfo Giometti)
- Patch 4: resolve the v4 probe-failure open question by taking option "A
+ keep the NULL guard": look the pinctrl states up first in probe(), before pps_gpio_setup(), and route the setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". Guard the echo-timer teardown in the new shutdown() with data->echo_pin, matching remove() (Rodolfo Giometti)

Changes in v4:
- Patch 1: add Fixes: 161520451dfa and Bartosz Golaszewski's Reviewed-by. The hardcoded -EINVAL predates 4461d65176b4, so the tag points at the original driver rather than the descriptor conversion
- Patch (binding): rework per Rob Herring - drop the prose, use an ordered "items" list ("default" then "inactive") with minItems: 1, fixing the "['default', 'inactive'] is too long" dt_binding_check error

Changes in v3 (addressing Sashiko's and Rodolfo Giometti's review):
- New preparatory patch to propagate the gpiod_to_irq() and request_threaded_irq() error codes instead of overwriting them with -EINVAL
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl described" rather than a probe failure; keep propagating everything else incl. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure via a new err_release_pins label
- Warn if applying the "inactive" state fails rather than ignoring the pinctrl_select_state() return

Changes in v2 (all addressing Rodolfo Giometti's review):
- Rename the released state from "idle" to "inactive" ("idle" is the runtime-PM state in pinctrl-state.h)
- Look the state up in the driver instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown(): tear down (free_irq/timer) before the mux change, matching remove()
- Require a "default" state whenever "inactive" is present and reject the mismatch

Eliav Farber (4):
pps: clients: gpio: propagate probe error codes
pps: clients: gpio: only tear down the echo timer when it exists
dt-bindings: pps: pps-gpio: document optional pinctrl states
pps: clients: gpio: release pins to an inactive state on remove and
shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     |  14 +-
 drivers/pps/clients/pps-gpio.c                | 123 ++++++++++++++++--
 2 files changed, 127 insertions(+), 10 deletions(-)

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.47.3
#30 lore raw

PATCH v5 1/4 pps: clients: gpio: propagate probe error codes

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Switch both paths to dev_err_probe() so the actual error code is returned and logged symbolically, and so a repeated -EPROBE_DEFER during boot is logged at debug level rather than spamming the console. This also matches pps_gpio_setup() in the same file, which already uses dev_err_probe().

Fixes 161520451dfa ("pps: new client driver using GPIO")

---
Changes in v5:
- Use dev_err_probe() on both error paths instead of dev_err() + return, so a propagated -EPROBE_DEFER is logged at debug level (no console spam on repeated deferral) and the code is emitted symbolically. This also matches pps_gpio_setup() in the same file. Drop Bartosz Golaszewski's Reviewed-by as the patch changed materially

Changes in v4:
- Add Fixes: 161520451dfa ("pps: new client driver using GPIO") and Bartosz Golaszewski's Reviewed-by. The hardcoded -EINVAL on both error paths predates 4461d65176b4 (which only switched gpio_to_irq() to gpiod_to_irq() and left those returns as context), so the tag points at the original driver rather than the descriptor conversion

Changes in v3:
- New patch, split out of the pinctrl change: while converting the probe error paths to a goto, Takashi Sakamoto noted that the hardcoded -EINVAL discards the real gpiod_to_irq()/request_threaded_irq() error, so fix that separately first

 drivers/pps/clients/pps-gpio.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/pps/clients/pps-gpio.c +4 -6
index 73ec2c7335e5..ccc2fb470b7e 100644
@@ -163,10 +163,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
-	if (ret < 0) {
-		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
-	}
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
 	data->irq = ret;
 
 	/* initialize PPS specific parts of the bookkeeping data structure. */
@@ -197,8 +195,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		return dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
+				     data->irq);
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
Signature · 1 line
2.47.3
#31 lore raw

PATCH v5 2/4 pps: clients: gpio: only tear down the echo timer when it exists

In reply to Eliav Farber

remove() calls timer_delete_sync() on data->echo_timer unconditionally, but the timer is only initialised by timer_setup() in probe() when the board describes an "echo" GPIO. On a board without echo-gpios the timer is never set up, so remove() operates on a timer_list that was never initialised.

The guard used to be there: it was dropped by commit fde046a8c490 ("pps:
clients: gpio: Remove redundant condition in ->remove()") on the grounds that "the timer along with GPIO API are NULL-aware". That is true for the GPIO API - gpiod_set_value() is a no-op for a NULL descriptor - but not for the timer: timer_delete_sync() on a timer that was never timer_setup() initialised trips the debug_assert_init() check and emits a debugobjects "not initialized" warning under CONFIG_DEBUG_OBJECTS_TIMERS.

Restore the data->echo_pin guard around the echo teardown, mirroring the condition under which the timer is set up in probe(). gpiod_set_value() is kept under the same guard as it only makes sense together with the echo timer.

Fixes fde046a8c490 ("pps: clients: gpio: Remove redundant condition in ->remove()")

---
Changes in v5:
- New patch. Split out because patch 4 mirrors remove()'s teardown in the new shutdown(); guarding the echo teardown here first keeps that latent issue out of both paths (Rodolfo Giometti)

 drivers/pps/clients/pps-gpio.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +5 -3
index ccc2fb470b7e..aec534c246af 100644
@@ -211,9 +211,11 @@ static void pps_gpio_remove(struct platform_device *pdev)
 
 	free_irq(data->irq, data);
 	pps_unregister_source(data->pps);
-	timer_delete_sync(&data->echo_timer);
-	/* reset echo pin in any case */
-	gpiod_set_value(data->echo_pin, 0);
+	/* reset the echo state, if the board has an echo GPIO */
+	if (data->echo_pin) {
+		timer_delete_sync(&data->echo_timer);
+		gpiod_set_value(data->echo_pin, 0);
+	}
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
Signature · 1 line
2.47.3
#32 lore raw

PATCH v5 3/4 dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. "default" is pinctrl-0, matching the implicit ordering the pinctrl core already assigns it, and "inactive" is pinctrl-1; the driver looks each state up by name.

---
Changes in v4:
- Rework per Rob Herring: do not express the ordering in prose; use an ordered "items" list ("default" then "inactive") with minItems: 1, since pinctrl-0 is already implicitly "default" and its position is fixed. This also fixes the "['default', 'inactive'] is too long" dt_binding_check error seen on v3. Reword the commit message accordingly

Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml          | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +13 -1
index 383a838744eb..61b5de6724ea 100644
@@ -28,6 +28,17 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      The "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. The optional "inactive" state, when present,
+      is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function.
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive
+
 required:
   - compatible
   - gpios
@@ -40,8 +51,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#37 lore raw

PATCH v5 4/4 pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

Look up the pinctrl states first in probe(), before pps_gpio_setup(), and route every subsequent failure through a common err_release_pins label. The driver core applies the "default" mux before probe(), so a probe that fails after this point would otherwise leave the pins stuck in "default"; releasing them to "inactive" on the error path is the symmetrical counterpart to what the core did on the driver's behalf. A failure in pps_gpio_get_pins() itself returns directly, as no state was taken yet; pps_gpio_release_pins() is a no-op when no "inactive" state was found.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() first, then the echo timer (only when the board has an echo GPIO, as in remove()), and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

---
Changes in v5:
- Resolve the v4 probe-failure open question by taking option "A + keep the NULL guard": look the pinctrl states up first in probe(), before pps_gpio_setup(), and route the pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". pps_gpio_release_pins() keeps its NULL guard, so it is a no-op when no "inactive" state was found. Verified on the AL11 K2V6 JRD10 with a forced-defer test: the pins are released to "inactive" on each failed attempt, the core re-applies "default" before the next, and the mux settles at "default" on the eventual success, with no spurious PPS event (Rodolfo Giometti)
- Clear data->pins_inactive when rejecting an "inactive" state that has no "default", so a board deliberately rejected here can never have its pins released to "inactive"
- Guard the echo-timer teardown in the new shutdown() with data->echo_pin, matching remove() after the preceding patch (Rodolfo Giometti)
- Convert the gpiod_to_irq()/request_threaded_irq() error paths to the log-only dev_err_probe() + goto err_release_pins form, following patch 1

Changes in v4:
- No functional change (probe-failure raised as an open question, now resolved in v5 above)

Changes in v3:
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl", not a probe failure; keep propagating everything else, e.g. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure via a new err_release_pins label
- Warn if pinctrl_select_state() fails to apply the "inactive" state rather than silently ignoring the error
- Trim and de-duplicate the added comments

Changes in v2:
- Rename the released state from "idle" to "inactive"
- Look the state up in the driver with devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state() instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown() to free_irq() and the echo teardown before the mux change, matching remove()
- Require a "default" state whenever "inactive" is present and reject the mismatch

 drivers/pps/clients/pps-gpio.c | 117 +++++++++++++++++++++++++++++++--
 1 file changed, 111 insertions(+), 6 deletions(-)
drivers/pps/clients/pps-gpio.c +111 -6
index aec534c246af..203bf465ed8b 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,68 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state. It requires a "default"
+ * state (applied by the driver core before probe) and is rejected without
+ * one. Absent pinctrl, or an absent "inactive" state, is not an error.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl)) {
+		/*
+		 * A DT device without "pinctrl-0" yields -ENODEV, which
+		 * is not an error here; propagate anything else.
+		 */
+		if (PTR_ERR(data->pinctrl) == -ENODEV) {
+			data->pinctrl = NULL;
+			return 0;
+		}
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+	}
+
+	/* The "inactive" state is optional. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}
+
+	/* "inactive" requires a "default" state to return from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default)) {
+		data->pins_inactive = NULL;
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+	}
+
+	return 0;
+}
+
+/*
+ * Restore the "inactive" pinctrl state, handing the pins back to whatever
+ * function uses them while pps-gpio is not driving PPS. This undoes the
+ * "default" state the driver core applied before probe. A no-op for boards
+ * that describe no "inactive" state.
+ */
+static void pps_gpio_release_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	int ret;
+
+	if (!data->pins_inactive)
+		return;
+
+	ret = pinctrl_select_state(data->pinctrl, data->pins_inactive);
+	if (ret)
+		dev_warn(dev, "failed to select inactive pinctrl state: %d\n",
+			 ret);
+}
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -156,15 +221,25 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	dev_set_drvdata(dev, data);
 
+	/*
+	 * pinctrl setup (optional states) first, so the "inactive" mux can be
+	 * restored on any later probe-failure path via err_release_pins.
+	 */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* GPIO setup */
 	ret = pps_gpio_setup(dev);
 	if (ret)
-		return ret;
+		goto err_release_pins;
 
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
-	if (ret < 0)
-		return dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
+	if (ret < 0) {
+		dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
+		goto err_release_pins;
+	}
 	data->irq = ret;
 
 	/* initialize PPS specific parts of the bookkeeping data structure. */
@@ -185,7 +260,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	if (IS_ERR(data->pps)) {
 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
 			data->irq);
-		return PTR_ERR(data->pps);
+		ret = PTR_ERR(data->pps);
+		goto err_release_pins;
 	}
 
 	/* register IRQ interrupt handler */
@@ -195,14 +271,20 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		return dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
-				     data->irq);
+		dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
+			      data->irq);
+		goto err_release_pins;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
 		data->irq);
 
 	return 0;
+
+err_release_pins:
+	/* Restore the inactive mux on probe failure; safe to do last here. */
+	pps_gpio_release_pins(dev);
+	return ret;
 }
 
 static void pps_gpio_remove(struct platform_device *pdev)
@@ -216,9 +298,31 @@ static void pps_gpio_remove(struct platform_device *pdev)
 		timer_delete_sync(&data->echo_timer);
 		gpiod_set_value(data->echo_pin, 0);
 	}
+	/* release the pins last, once nothing can drive them */
+	pps_gpio_release_pins(&pdev->dev);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * The kernel keeps running after device_shutdown() (e.g. to load and
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	/* reset the echo state, if the board has an echo GPIO */
+	if (data->echo_pin) {
+		timer_delete_sync(&data->echo_timer);
+		gpiod_set_value(data->echo_pin, 0);
+	}
+	pps_gpio_release_pins(&pdev->dev);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +332,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
#38 lore raw

Re: [PATCH v5 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] timers: use of timer_delete_sync() instead of timer_shutdown_sync() in teardown --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] timers: use of timer_delete_sync() instead of timer_shutdown_sync() in teardown --

commit c434ce52f848555ba764fe5cc2d58d2fed47b99f
Author: Eliav Farber <[email protected]>

pps: clients: gpio: release pins to an inactive state on remove and shutdown

This commit introduces logic to look up and apply an "inactive" pinctrl state when the driver is unbound or the system is shut down. This ensures pins are not left stuck in their default GPIO function for subsequent kernels like kexec. It introduces a new shutdown callback that tears down the IRQ and the echo timer before restoring the mux state.

Quoted 4 lines
drivers/pps/clients/pps-gpio.c +0 -0
index aec534c246af1..203bf465ed8bf 100644

[ ... ]

Quoted 1 line
drivers/pps/clients/pps-gpio.c +0 -0
@@ -216,9 +298,31 @@ static void pps_gpio_remove(struct platform_device *pdev)

[ ... ]

Quoted 5 lines
drivers/pps/clients/pps-gpio.c +5 -0
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*

drivers/pps/clients/pps-gpio.c static void pps_gpio_remove(struct platform_device *pdev) · line 320

Quoted 10 lines
drivers/pps/clients/pps-gpio.c +10 -0
+	 * The kernel keeps running after device_shutdown() (e.g. to load and
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	/* reset the echo state, if the board has an echo GPIO */
+	if (data->echo_pin) {
+		timer_delete_sync(&data->echo_timer);

[Severity: Low]
Should this teardown sequence in pps_gpio_shutdown() use timer_shutdown_sync() instead of timer_delete_sync()?

The modern timer API guidelines mandate using timer_shutdown_sync() in device teardown paths to robustly prevent the timer from being re-armed. While free_irq() runs prior and stops the interrupt handler from running concurrently, timer_shutdown_sync() remains the documented standard for fully shutting down a timer.

Quoted 4 lines
drivers/pps/clients/pps-gpio.c +4 -0
+		gpiod_set_value(data->echo_pin, 0);
+	}
+	pps_gpio_release_pins(&pdev->dev);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#33 lore raw

Re: [PATCH v5 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

One observation while testing the series, entirely a devicetree call:
dtschema derives maxItems from the items list, so this also caps pinctrl-names at two and fixes the order --

  "default", "inactive", "sleep"   ->  'is too long'
  "default", "sleep"               ->  'inactive' was expected

I have no opinion on whether that matters for this binding, I just wanted it on the record.

Ciao,

Rodolfo

maintainer
#39 lore raw

Re: [PATCH v5 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

On Tue, Sep 22, 2026 at 10:30:51AM +0000, Eliav Farber wrote:

The probe-failure path looks right to me now. Only small things left.

Quoted 1 line

+static void pps_gpio_shutdown(struct platform_device *pdev)

This runs on every board, including the ones that describe no "inactive" state, where the free_irq() and the echo teardown buy nothing. Harmless as far as I can see, but the commit message only speaks about the mux -- worth a line there?

drivers/pps/clients/pps-gpio.c static int pps_gpio_probe(struct platform_device *pdev) · line 262

Quoted 3 lines
drivers/pps/clients/pps-gpio.c +0 -0
 	if (IS_ERR(data->pps)) {
 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
 			data->irq);

This stayed dev_err() while both its neighbours became dev_err_probe(). Nothing is broken, but "matches the rest of the file" was the argument for patch 1.

I would keep timer_delete_sync() here rather than moving to timer_shutdown_sync(): the kernel-doc motivates the latter with the circular-dependency case, which does not apply here -- the only rearm path is the echo, behind free_irq().

Ciao,

Rodolfo

#40 lore raw

RE: [PATCH v5 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

Quoted 8 lines · Rodolfo Giometti

Review

Quoted 3 lines · Eliav Farber
(quoted diff) +3 -0
+     if (IS_ERR(data->pps)) {
+             dev_err(dev, "failed to register IRQ %d as PPS source\n",
+                     data->irq);

This stayed dev_err() while both its neighbours became dev_err_probe(). Nothing is broken, but "matches the rest of the file" was the argument for patch 1.

I noticed this too while doing the changes, but wasn't sure where it should go, so I would rather ask than guess.

The awkward part is that patch 1's actual fix is propagating the real error code -- the -EINVAL -> ret change on the gpiod_to_irq() and request_threaded_irq() paths. The dev_err_probe() form there is a consequence of that (and of your v4 nit), not the point of the patch. The pps_register_source() path never had the errno bug: it already did return PTR_ERR(data->pps), so converting it is purely a logging/style consistency change, unrelated to what patch 1 sets out to fix. Patch 4 is not an obvious home either: the dev_err()/dev_err_probe() split predates it (it exists from patch 1 onward), so patch 4 would only be tidying it up in passing rather than introducing it.

So I see three ways to place it:

1. Fold it into patch 1. Pro: patch 1 is where the file first gains dev_err_probe(), so all three probe error paths become consistent in the same commit. Con: it widens patch 1 beyond "propagate the error code" into a path that never masked the error, muddying an otherwise focused fix. It would use the return-form (return dev_err_probe(...)), which patch 4 then rewrites into the log-only + goto err_release_pins form like the other two.

2. Fold it into patch 4. Pro: patch 4 already rewrites these paths into the log-only dev_err_probe() + goto form, so the conversion rides along with churn it is already making. Con: the dev_err()/dev_err_probe() split predates patch 4 -- it exists from patch 1 onward -- so patch 4 is not really where the inconsistency originates.

3. A small separate patch after patch 1, "pps: clients: gpio: use dev_err_probe() consistently in probe" (or similar). Pro: keeps each patch single-purpose -- patch 1 stays "propagate the code", the style unification is its own reviewable change.

Which do you prefer?

Thanks,
Eliav

maintainer
#41 lore raw

Re: [PATCH v5 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

Quoted 1 line · Eliav Farber

Which do you prefer?

I think option 2 is the best, since patch 4 has to modify that block anyway, and it's simply an extra line within a section of code that is being changed regardless.

You just need to mention it in the commit message for patch 4 so it doesn't look like a superfluous or random change.

In any case, this doesn't block anything.

Ciao,

Rodolfo

maintainer
#34 lore raw

Re: [PATCH v5 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Rodolfo Giometti
Quoted 9 lines · Rodolfo Giometti

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

One observation while testing the series, entirely a devicetree call:
dtschema derives maxItems from the items list, so this also caps pinctrl-names at two and fixes the order --

Yes, that's exactly what was said was needed here.

Quoted 3 lines
  "default", "inactive", "sleep"   ->  'is too long'
  "default", "sleep"               ->  'inactive' was expected

Well, that's something different.

items:
- const: default
- enum: [ inactive, sleep ]
- const: sleep

The common pinctrl-names schema should require unique names so 'sleep' can't be repeated.

Quoted 3 lines

I have no opinion on whether that matters for this binding, I just wanted it on the record.

We don't even require pinctrl properties to be documented. They are implicitly allowed, but good to document what users (drivers) expect/require.

Rob

#35 lore raw

RE: [PATCH v5 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

Quoted 18 lines · Rob Herring
Quoted 9 lines · Rodolfo Giometti

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

One observation while testing the series, entirely a devicetree call:
dtschema derives maxItems from the items list, so this also caps pinctrl-names at two and fixes the order --

Yes, that's exactly what was said was needed here.

Quoted 2 lines
  "default", "inactive", "sleep"   ->  'is too long'
  "default", "sleep"               ->  'inactive' was expected

Well, that's something different.

Quoted 7 lines

items:
- const: default
- enum: [ inactive, sleep ]
- const: sleep

The common pinctrl-names schema should require unique names so 'sleep' can't be repeated.

Thanks. Just so I pick the right thing for v6 -- do you want me to change patch 3 to that form, or is the current

minItems: 1
items:
- const: default
- const: inactive

fine as-is?

This binding only defines "default" and "inactive"; the driver looks both up by name and does not use a "sleep" state, so I had deliberately capped the list at those two. Your snippet reads to me as how the common pinctrl-names schema would accommodate a "sleep" state in general, rather than something this binding needs -- but I would rather confirm than guess.

If you would prefer the more permissive form so a board can also declare "sleep", I will switch to it (with unique-name enforcement) in v6. Otherwise I will keep the two-entry list.

Quoted 3 lines

We don't even require pinctrl properties to be documented. They are implicitly allowed, but good to document what users (drivers) expect/require.

Right -- documenting the two names the driver actually consumes was the intent here.

Thanks,
Eliav

maintainer
#36 lore raw

Re: [PATCH v5 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

Quoted 30 lines · Eliav
Quoted 18 lines · Rob Herring
Quoted 9 lines · Rodolfo Giometti

Documentation/devicetree/bindings/pps/pps-gpio.yaml properties: · line 40

Quoted 4 lines · Eliav Farber
Documentation/devicetree/bindings/pps/pps-gpio.yaml +4 -0
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive

One observation while testing the series, entirely a devicetree call:
dtschema derives maxItems from the items list, so this also caps pinctrl-names at two and fixes the order --

Yes, that's exactly what was said was needed here.

Quoted 2 lines
  "default", "inactive", "sleep"   ->  'is too long'
  "default", "sleep"               ->  'inactive' was expected

Well, that's something different.

Quoted 7 lines

items:
- const: default
- enum: [ inactive, sleep ]
- const: sleep

The common pinctrl-names schema should require unique names so 'sleep' can't be repeated.

Thanks. Just so I pick the right thing for v6 -- do you want me to change patch 3 to that form, or is the current

Quoted 6 lines

minItems: 1
items:
- const: default
- const: inactive

fine as-is?

Yes.

Quoted 11 lines

This binding only defines "default" and "inactive"; the driver looks both up by name and does not use a "sleep" state, so I had deliberately capped the list at those two. Your snippet reads to me as how the common pinctrl-names schema would accommodate a "sleep" state in general, rather than something this binding needs -- but I would rather confirm than guess.

If you would prefer the more permissive form so a board can also declare "sleep", I will switch to it (with unique-name enforcement) in v6. Otherwise I will keep the two-entry list.

Quoted 6 lines
Quoted 3 lines

We don't even require pinctrl properties to be documented. They are implicitly allowed, but good to document what users (drivers) expect/require.

Right -- documenting the two names the driver actually consumes was the intent here.

I have no clue why "sleep" was added to the discussion...

Rob

#42 lore raw

v6 cover pps-gpio: restore pin mux on unbind and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Patch 1 is a small preparatory fix to propagate the gpiod_to_irq() and request_threaded_irq() error codes (rather than a hardcoded -EINVAL) on the probe error paths that patch 4 then converts to gotos. Patch 2 is a small preparatory fix restoring the echo-GPIO guard in remove() that a later cleanup dropped, since patch 4 mirrors remove()'s teardown in the new shutdown() and would otherwise carry the same latent issue. Patch 3 documents the optional "default"/"inactive" pinctrl-names in the binding; patch 4 implements the driver side.

Tested on an Amazon AL11 K2V6 JRD10 board: binding/unbinding each pps-gpio device toggles the corresponding pin-mux register between the GPIO function and the alternate ec_ptp_trigger_in function as expected, and re-binding restores the GPIO function via the core-applied "default" state. Reading the mux register with the device left unbound confirms the "inactive" mux persists. Also tested with a pps-gpio node that describes no pinctrl at all, where probe, remove and shutdown behave as before.

For the probe-failure path (see below), a forced-defer test confirmed that a probe failing after pps_gpio_get_pins() releases the pins to "inactive", the core re-applies "default" before the next attempt, and the mux settles back at "default" once probe finally succeeds, with no spurious PPS event or warning across the cycles.

Changes in v6:
- Patch 4: fold the pps_register_source() failure path into the same dev_err_probe() + goto err_release_pins style as the other two paths sharing that label, with a commit-message note that it is a logging change, not a fix (Rodolfo Giometti)
- Patch 3: unchanged. Rob Herring confirmed the two-entry ["default", "inactive"] items list is fine as-is; the "sleep" state discussed on the list is not something this binding needs
- No other functional changes

Changes in v5:
- Patch 1: use dev_err_probe() on both error paths instead of a bare dev_err() + return, so the propagated -EPROBE_DEFER is logged at debug level rather than spamming the console and the code is emitted symbolically. Drop Bartosz's Reviewed-by as the patch changed materially (Rodolfo Giometti)
- New patch 2: restore the data->echo_pin guard around the echo-timer teardown in remove(), dropped by commit fde046a8c490. timer_delete_sync() on a never-initialised timer trips debug_assert_init() under CONFIG_DEBUG_OBJECTS_TIMERS. Split out so patch 4's new shutdown() does not reintroduce it (Rodolfo Giometti)
- Patch 4: resolve the v4 probe-failure open question by taking option "A
+ keep the NULL guard": look the pinctrl states up first in probe(), before pps_gpio_setup(), and route the setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". Guard the echo-timer teardown in the new shutdown() with data->echo_pin, matching remove() (Rodolfo Giometti)

Changes in v4:
- Patch 1: add Fixes: 161520451dfa and Bartosz Golaszewski's Reviewed-by. The hardcoded -EINVAL predates 4461d65176b4, so the tag points at the original driver rather than the descriptor conversion
- Patch (binding): rework per Rob Herring - drop the prose, use an ordered "items" list ("default" then "inactive") with minItems: 1, fixing the "['default', 'inactive'] is too long" dt_binding_check error

Changes in v3 (addressing Sashiko's and Rodolfo Giometti's review):
- New preparatory patch to propagate the gpiod_to_irq() and request_threaded_irq() error codes instead of overwriting them with -EINVAL
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl described" rather than a probe failure; keep propagating everything else incl. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure via a new err_release_pins label
- Warn if applying the "inactive" state fails rather than ignoring the pinctrl_select_state() return

Changes in v2 (all addressing Rodolfo Giometti's review):
- Rename the released state from "idle" to "inactive" ("idle" is the runtime-PM state in pinctrl-state.h)
- Look the state up in the driver instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown(): tear down (free_irq/timer) before the mux change, matching remove()
- Require a "default" state whenever "inactive" is present and reject the mismatch

Eliav Farber (4):
pps: clients: gpio: propagate probe error codes
pps: clients: gpio: only tear down the echo timer when it exists
dt-bindings: pps: pps-gpio: document optional pinctrl states
pps: clients: gpio: release pins to an inactive state on remove and
shutdown

 .../devicetree/bindings/pps/pps-gpio.yaml     |  14 +-
 drivers/pps/clients/pps-gpio.c                | 127 ++++++++++++++++--
 2 files changed, 129 insertions(+), 12 deletions(-)

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.47.3
#43 lore raw

PATCH v6 1/4 pps: clients: gpio: propagate probe error codes

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Switch both paths to dev_err_probe() so the actual error code is returned and logged symbolically, and so a repeated -EPROBE_DEFER during boot is logged at debug level rather than spamming the console. This also matches pps_gpio_setup() in the same file, which already uses dev_err_probe().

Fixes 161520451dfa ("pps: new client driver using GPIO")

---
Changes in v6:
- No change

Changes in v5:
- Use dev_err_probe() on both error paths instead of dev_err() + return, so a propagated -EPROBE_DEFER is logged at debug level (no console spam on repeated deferral) and the code is emitted symbolically. This also matches pps_gpio_setup() in the same file. Drop Bartosz Golaszewski's Reviewed-by as the patch changed materially

Changes in v4:
- Add Fixes: 161520451dfa ("pps: new client driver using GPIO") and Bartosz Golaszewski's Reviewed-by. The hardcoded -EINVAL on both error paths predates 4461d65176b4 (which only switched gpio_to_irq() to gpiod_to_irq() and left those returns as context), so the tag points at the original driver rather than the descriptor conversion

Changes in v3:
- New patch, split out of the pinctrl change: while converting the probe error paths to a goto, Takashi Sakamoto noted that the hardcoded -EINVAL discards the real gpiod_to_irq()/request_threaded_irq() error, so fix that separately first

 drivers/pps/clients/pps-gpio.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/pps/clients/pps-gpio.c +4 -6
index 73ec2c7335e5..ccc2fb470b7e 100644
@@ -163,10 +163,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
-	if (ret < 0) {
-		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
-		return -EINVAL;
-	}
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
 	data->irq = ret;
 
 	/* initialize PPS specific parts of the bookkeeping data structure. */
@@ -197,8 +195,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
-		return -EINVAL;
+		return dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
+				     data->irq);
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
Signature · 1 line
2.47.3
#49 lore raw

PATCH v6 3/4 dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. "default" is pinctrl-0, matching the implicit ordering the pinctrl core already assigns it, and "inactive" is pinctrl-1; the driver looks each state up by name.

---
Changes in v6:
- No change. Rob Herring confirmed the two-entry ["default", "inactive"] items list is fine as-is; the "sleep" state discussed on the list is not something this binding needs

Changes in v4:
- Rework per Rob Herring: do not express the ordering in prose; use an ordered "items" list ("default" then "inactive") with minItems: 1, since pinctrl-0 is already implicitly "default" and its position is fixed. This also fixes the "['default', 'inactive'] is too long" dt_binding_check error seen on v3. Reword the commit message accordingly

Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml          | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pps/pps-gpio.yaml +13 -1
index 383a838744eb..61b5de6724ea 100644
@@ -28,6 +28,17 @@ properties:
     description: Indicates a falling edge assert, when present. Rising edge if absent.
     type: boolean
 
+  pinctrl-names:
+    description:
+      The "default" state selects the PPS/GPIO function and is applied by the
+      driver core before probe. The optional "inactive" state, when present,
+      is selected when the driver is unbound or the system is shut down,
+      handing the pins back to their alternate function.
+    minItems: 1
+    items:
+      - const: default
+      - const: inactive
+
 required:
   - compatible
   - gpios
@@ -40,8 +51,9 @@ examples:
 
     pps {
         compatible = "pps-gpio";
-        pinctrl-names = "default";
+        pinctrl-names = "default", "inactive";
         pinctrl-0 = <&pinctrl_pps>;
+        pinctrl-1 = <&pinctrl_pps_inactive>;
         gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
         assert-falling-edge;
         echo-gpios = <&gpio1 27 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.47.3
#46 lore raw

PATCH v6 2/4 pps: clients: gpio: only tear down the echo timer when it exists

In reply to Eliav Farber

remove() calls timer_delete_sync() on data->echo_timer unconditionally, but the timer is only initialised by timer_setup() in probe() when the board describes an "echo" GPIO. On a board without echo-gpios the timer is never set up, so remove() operates on a timer_list that was never initialised.

The guard used to be there: it was dropped by commit fde046a8c490 ("pps:
clients: gpio: Remove redundant condition in ->remove()") on the grounds that "the timer along with GPIO API are NULL-aware". That is true for the GPIO API - gpiod_set_value() is a no-op for a NULL descriptor - but not for the timer: timer_delete_sync() on a timer that was never timer_setup() initialised trips the debug_assert_init() check and emits a debugobjects "not initialized" warning under CONFIG_DEBUG_OBJECTS_TIMERS.

Restore the data->echo_pin guard around the echo teardown, mirroring the condition under which the timer is set up in probe(). gpiod_set_value() is kept under the same guard as it only makes sense together with the echo timer.

Fixes fde046a8c490 ("pps: clients: gpio: Remove redundant condition in ->remove()")

---
Changes in v6:
- No change

Changes in v5:
- New patch. Split out because patch 4 mirrors remove()'s teardown in the new shutdown(); guarding the echo teardown here first keeps that latent issue out of both paths (Rodolfo Giometti)

 drivers/pps/clients/pps-gpio.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/pps/clients/pps-gpio.c +5 -3
index ccc2fb470b7e..aec534c246af 100644
@@ -211,9 +211,11 @@ static void pps_gpio_remove(struct platform_device *pdev)
 
 	free_irq(data->irq, data);
 	pps_unregister_source(data->pps);
-	timer_delete_sync(&data->echo_timer);
-	/* reset echo pin in any case */
-	gpiod_set_value(data->echo_pin, 0);
+	/* reset the echo state, if the board has an echo GPIO */
+	if (data->echo_pin) {
+		timer_delete_sync(&data->echo_timer);
+		gpiod_set_value(data->echo_pin, 0);
+	}
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
Signature · 1 line
2.47.3
#52 lore raw

PATCH v6 4/4 pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

Look up the pinctrl states first in probe(), before pps_gpio_setup(), and route every subsequent failure through a common err_release_pins label. The driver core applies the "default" mux before probe(), so a probe that fails after this point would otherwise leave the pins stuck in "default"; releasing them to "inactive" on the error path is the symmetrical counterpart to what the core did on the driver's behalf. A failure in pps_gpio_get_pins() itself returns directly, as no state was taken yet; pps_gpio_release_pins() is a no-op when no "inactive" state was found.

Convert the pps_register_source() failure path to dev_err_probe() too, so all three probe error paths that share err_release_pins log the same way. This path already returned PTR_ERR(data->pps), so this is a logging change, not a fix.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() first, then the echo timer (only when the board has an echo GPIO, as in remove()), and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

---
Changes in v6:
- Convert the pps_register_source() failure path to dev_err_probe() + goto err_release_pins as well, so all three probe error paths sharing err_release_pins log the same way. It already returned PTR_ERR(data->pps), so this is a logging change, not a fix. Fold in here rather than a separate patch since this block is rewritten by this patch anyway, with a commit-message note (Rodolfo Giometti)

Changes in v5:
- Resolve the v4 probe-failure open question by taking option "A + keep the NULL guard": look the pinctrl states up first in probe(), before pps_gpio_setup(), and route the pps_gpio_setup() failure through err_release_pins too, so every path the driver can act on restores "inactive". pps_gpio_release_pins() keeps its NULL guard, so it is a no-op when no "inactive" state was found. Verified on the AL11 K2V6 JRD10 with a forced-defer test: the pins are released to "inactive" on each failed attempt, the core re-applies "default" before the next, and the mux settles at "default" on the eventual success, with no spurious PPS event (Rodolfo Giometti)
- Clear data->pins_inactive when rejecting an "inactive" state that has no "default", so a board deliberately rejected here can never have its pins released to "inactive"
- Guard the echo-timer teardown in the new shutdown() with data->echo_pin, matching remove() after the preceding patch (Rodolfo Giometti)
- Convert the gpiod_to_irq()/request_threaded_irq() error paths to the log-only dev_err_probe() + goto err_release_pins form, following patch 1

Changes in v4:
- No functional change (probe-failure raised as an open question, now resolved in v5 above)

Changes in v3:
- Treat -ENODEV from devm_pinctrl_get() as "no pinctrl", not a probe failure; keep propagating everything else, e.g. -EPROBE_DEFER
- Restore the "inactive" mux on probe failure via a new err_release_pins label
- Warn if pinctrl_select_state() fails to apply the "inactive" state rather than silently ignoring the error
- Trim and de-duplicate the added comments

Changes in v2:
- Rename the released state from "idle" to "inactive"
- Look the state up in the driver with devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_state() instead of pinctrl_pm_select_idle_state(), removing the CONFIG_PM dependency
- Fix shutdown() to free_irq() and the echo teardown before the mux change, matching remove()
- Require a "default" state whenever "inactive" is present and reject the mismatch

 drivers/pps/clients/pps-gpio.c | 121 ++++++++++++++++++++++++++++++---
 1 file changed, 113 insertions(+), 8 deletions(-)
drivers/pps/clients/pps-gpio.c +113 -8
index aec534c246af..e6d39ca6777d 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/pps_kernel.h>
 #include <linux/gpio/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/list.h>
 #include <linux/property.h>
 #include <linux/timer.h>
@@ -30,6 +31,8 @@ struct pps_gpio_device_data {
 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
+	struct pinctrl *pinctrl;	/* pin control handle */
+	struct pinctrl_state *pins_inactive;	/* pins released when unbound */
 	bool assert_falling_edge;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
@@ -96,6 +99,68 @@ static void pps_gpio_echo_timer_callback(struct timer_list *t)
 	gpiod_set_value(info->echo_pin, 0);
 }
 
+/*
+ * Look up the optional "inactive" pinctrl state. It requires a "default"
+ * state (applied by the driver core before probe) and is rejected without
+ * one. Absent pinctrl, or an absent "inactive" state, is not an error.
+ */
+static int pps_gpio_get_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	struct pinctrl_state *pins_default;
+
+	data->pinctrl = devm_pinctrl_get(dev);
+	if (IS_ERR(data->pinctrl)) {
+		/*
+		 * A DT device without "pinctrl-0" yields -ENODEV, which
+		 * is not an error here; propagate anything else.
+		 */
+		if (PTR_ERR(data->pinctrl) == -ENODEV) {
+			data->pinctrl = NULL;
+			return 0;
+		}
+		return dev_err_probe(dev, PTR_ERR(data->pinctrl),
+				     "failed to get pinctrl\n");
+	}
+
+	/* The "inactive" state is optional. */
+	data->pins_inactive = pinctrl_lookup_state(data->pinctrl, "inactive");
+	if (IS_ERR(data->pins_inactive)) {
+		data->pins_inactive = NULL;
+		return 0;
+	}
+
+	/* "inactive" requires a "default" state to return from. */
+	pins_default = pinctrl_lookup_state(data->pinctrl, "default");
+	if (IS_ERR(pins_default)) {
+		data->pins_inactive = NULL;
+		return dev_err_probe(dev, PTR_ERR(pins_default),
+				     "\"inactive\" pinctrl state requires a \"default\" state\n");
+	}
+
+	return 0;
+}
+
+/*
+ * Restore the "inactive" pinctrl state, handing the pins back to whatever
+ * function uses them while pps-gpio is not driving PPS. This undoes the
+ * "default" state the driver core applied before probe. A no-op for boards
+ * that describe no "inactive" state.
+ */
+static void pps_gpio_release_pins(struct device *dev)
+{
+	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
+	int ret;
+
+	if (!data->pins_inactive)
+		return;
+
+	ret = pinctrl_select_state(data->pinctrl, data->pins_inactive);
+	if (ret)
+		dev_warn(dev, "failed to select inactive pinctrl state: %d\n",
+			 ret);
+}
+
 static int pps_gpio_setup(struct device *dev)
 {
 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
@@ -156,15 +221,25 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	dev_set_drvdata(dev, data);
 
+	/*
+	 * pinctrl setup (optional states) first, so the "inactive" mux can be
+	 * restored on any later probe-failure path via err_release_pins.
+	 */
+	ret = pps_gpio_get_pins(dev);
+	if (ret)
+		return ret;
+
 	/* GPIO setup */
 	ret = pps_gpio_setup(dev);
 	if (ret)
-		return ret;
+		goto err_release_pins;
 
 	/* IRQ setup */
 	ret = gpiod_to_irq(data->gpio_pin);
-	if (ret < 0)
-		return dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
+	if (ret < 0) {
+		dev_err_probe(dev, ret, "failed to map GPIO to IRQ\n");
+		goto err_release_pins;
+	}
 	data->irq = ret;
 
 	/* initialize PPS specific parts of the bookkeeping data structure. */
@@ -183,9 +258,10 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
 	data->pps = pps_register_source(&data->info, pps_default_params);
 	if (IS_ERR(data->pps)) {
-		dev_err(dev, "failed to register IRQ %d as PPS source\n",
-			data->irq);
-		return PTR_ERR(data->pps);
+		ret = PTR_ERR(data->pps);
+		dev_err_probe(dev, ret, "failed to register IRQ %d as PPS source\n",
+			      data->irq);
+		goto err_release_pins;
 	}
 
 	/* register IRQ interrupt handler */
@@ -195,14 +271,20 @@ static int pps_gpio_probe(struct platform_device *pdev)
 			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-		return dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
-				     data->irq);
+		dev_err_probe(dev, ret, "failed to acquire IRQ %d\n",
+			      data->irq);
+		goto err_release_pins;
 	}
 
 	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
 		data->irq);
 
 	return 0;
+
+err_release_pins:
+	/* Restore the inactive mux on probe failure; safe to do last here. */
+	pps_gpio_release_pins(dev);
+	return ret;
 }
 
 static void pps_gpio_remove(struct platform_device *pdev)
@@ -216,9 +298,31 @@ static void pps_gpio_remove(struct platform_device *pdev)
 		timer_delete_sync(&data->echo_timer);
 		gpiod_set_value(data->echo_pin, 0);
 	}
+	/* release the pins last, once nothing can drive them */
+	pps_gpio_release_pins(&pdev->dev);
 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
 }
 
+static void pps_gpio_shutdown(struct platform_device *pdev)
+{
+	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
+
+	/*
+	 * The kernel keeps running after device_shutdown() (e.g. to load and
+	 * start a kexec image), so quiesce the hardware before touching the
+	 * mux: free the IRQ and stop the echo timer first, then release the
+	 * pins last, so no callback can drive a pin after it is handed back.
+	 * The PPS source is left registered; that is a remove-time concern.
+	 */
+	free_irq(data->irq, data);
+	/* reset the echo state, if the board has an echo GPIO */
+	if (data->echo_pin) {
+		timer_delete_sync(&data->echo_timer);
+		gpiod_set_value(data->echo_pin, 0);
+	}
+	pps_gpio_release_pins(&pdev->dev);
+}
+
 static const struct of_device_id pps_gpio_dt_ids[] = {
 	{ .compatible = "pps-gpio", },
 	{ /* sentinel */ }
@@ -228,6 +332,7 @@ MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
 static struct platform_driver pps_gpio_driver = {
 	.probe		= pps_gpio_probe,
 	.remove		= pps_gpio_remove,
+	.shutdown	= pps_gpio_shutdown,
 	.driver		= {
 		.name	= PPS_GPIO_NAME,
 		.of_match_table	= pps_gpio_dt_ids,
Signature · 1 line
2.47.3
maintainer Reviewed-by on v6 3/4
#50 lore raw

Re: [PATCH v6 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 33 lines · Eliav Farber

When the PPS input GPIO is routed through a pin controller, a board may need to mux those pins to a different function while pps-gpio is not driving PPS (for example after the driver is unbound or across a kexec).

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. "default" is pinctrl-0, matching the implicit ordering the pinctrl core already assigns it, and "inactive" is pinctrl-1; the driver looks each state up by name.

---
Changes in v6:
- No change. Rob Herring confirmed the two-entry ["default", "inactive"] items list is fine as-is; the "sleep" state discussed on the list is not something this binding needs

Changes in v4:
- Rework per Rob Herring: do not express the ordering in prose; use an ordered "items" list ("default" then "inactive") with minItems: 1, since pinctrl-0 is already implicitly "default" and its position is fixed. This also fixes the "['default', 'inactive'] is too long" dt_binding_check error seen on v3. Reword the commit message accordingly

Changes in v3:
- Do not constrain pinctrl-names to a fixed ["default", "inactive"] tuple. The driver looks the states up by name, so "inactive" may appear in any position and other states may coexist; only require (via "contains") that a "default" state exists, and reword the description accordingly

Quoted 5 lines

Changes in v2:
- Rename the released state from "idle" to "inactive"

 .../devicetree/bindings/pps/pps-gpio.yaml          | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

In the future, don't thread new versions to old versions.

maintainer
#55 lore raw
In reply to Eliav Farber
Quoted 5 lines · Eliav Farber

Changes in v6:
- Patch 4: fold the pps_register_source() failure path into the same dev_err_probe() + goto err_release_pins style as the other two paths sharing that label, with a commit-message note that it is a logging change, not a fix (Rodolfo Giometti)

That is exactly what I had in mind, thanks.

I applied the series here on v7.3-rc4: it applies cleanly, checkpatch --strict is quiet on all four patches, and drivers/pps builds with W=1 and CONFIG_DEBUG_OBJECTS_TIMERS=y. I have acked the four patches individually.

Andrew, could you please pick this up? LinuxPPS has no tree of its own.

One leftover from the v5 review, not a blocker: the new shutdown() runs on every board, including the ones that describe no "inactive" state, where the free_irq() and the echo teardown buy nothing, and the commit message of patch 4 still only speaks about the mux. If the series needs a respin for some other reason, a line there would be worth having -- otherwise it is fine as it stands.

Ciao,

Rodolfo

maintainer Acked-by on v6 1/4
#44 lore raw

Re: [PATCH v6 1/4] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Quoted 5 lines · Eliav Farber

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

maintainer Acked-by on v6 2/4
#47 lore raw

Re: [PATCH v6 2/4] pps: clients: gpio: only tear down the echo timer when it exists

In reply to Eliav Farber
Quoted 5 lines · Eliav Farber

remove() calls timer_delete_sync() on data->echo_timer unconditionally, but the timer is only initialised by timer_setup() in probe() when the board describes an "echo" GPIO. On a board without echo-gpios the timer is never set up, so remove() operates on a timer_list that was never initialised.

maintainer Acked-by on v6 3/4
#51 lore raw

Re: [PATCH v6 3/4] dt-bindings: pps: pps-gpio: document optional pinctrl states

In reply to Eliav Farber
Quoted 7 lines · Eliav Farber

Document the optional "default" and "inactive" pinctrl-names and show both in the example. The "default" state selects the PPS/GPIO function and is applied by the driver core before probe; the optional "inactive" state, when present, describes the mux to restore when the driver is unbound or the system is shut down. "default" is pinctrl-0, matching the implicit ordering the pinctrl core already assigns it, and "inactive" is pinctrl-1; the driver looks each state up by name.

maintainer Acked-by on v6 4/4
#53 lore raw

Re: [PATCH v6 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber
Quoted 4 lines · Eliav Farber

Convert the pps_register_source() failure path to dev_err_probe() too, so all three probe error paths that share err_release_pins log the same way. This path already returned PTR_ERR(data->pps), so this is a logging change, not a fix.

That reads right, thanks.

Ciao,

Rodolfo

maintainer Reviewed-by on v6 1/4
#45 lore raw

Re: [PATCH v6 1/4] pps: clients: gpio: propagate probe error codes

In reply to Eliav Farber
Quoted 6 lines · Eliav Farber

On the two probe error paths that map and request the interrupt, probe overwrote the error from gpiod_to_irq() and request_threaded_irq() with a hardcoded -EINVAL, hiding meaningful codes such as -EBUSY, -ENOMEM or -EPROBE_DEFER from the caller. The request_threaded_irq() failure message also logged the IRQ number but not the errno.

Quoted 6 lines

Switch both paths to dev_err_probe() so the actual error code is returned and logged symbolically, and so a repeated -EPROBE_DEFER during boot is logged at debug level rather than spamming the console. This also matches pps_gpio_setup() in the same file, which already uses dev_err_probe().

Fixes 161520451dfa ("pps: new client driver using GPIO")
maintainer Reviewed-by on v6 2/4
#48 lore raw

Re: [PATCH v6 2/4] pps: clients: gpio: only tear down the echo timer when it exists

In reply to Eliav Farber
Quoted 14 lines · Eliav Farber

remove() calls timer_delete_sync() on data->echo_timer unconditionally, but the timer is only initialised by timer_setup() in probe() when the board describes an "echo" GPIO. On a board without echo-gpios the timer is never set up, so remove() operates on a timer_list that was never initialised.

The guard used to be there: it was dropped by commit fde046a8c490 ("pps:
clients: gpio: Remove redundant condition in ->remove()") on the grounds that "the timer along with GPIO API are NULL-aware". That is true for the GPIO API - gpiod_set_value() is a no-op for a NULL descriptor - but not for the timer: timer_delete_sync() on a timer that was never timer_setup() initialised trips the debug_assert_init() check and emits a debugobjects "not initialized" warning under CONFIG_DEBUG_OBJECTS_TIMERS.

Quoted 8 lines

Restore the data->echo_pin guard around the echo teardown, mirroring the condition under which the timer is set up in probe(). gpiod_set_value() is kept under the same guard as it only makes sense together with the echo timer.

Fixes fde046a8c490 ("pps: clients: gpio: Remove redundant condition in ->remove()")

---

maintainer Reviewed-by on v6 4/4
#54 lore raw

Re: [PATCH v6 4/4] pps: clients: gpio: release pins to an inactive state on remove and shutdown

In reply to Eliav Farber
Quoted 45 lines · Eliav Farber

Some boards route the PPS input GPIO through a pin controller and need to mux it to another function when pps-gpio is not driving PPS. The driver core applies the "default" pinctrl state before probe, so the pins are muxed for GPIO/PPS use while the driver is bound. Nothing, however, hands the pins back when the driver is unbound or the system is shut down, so they stay stuck in the GPIO function for whatever runs next, kexec included.

Look up an optional "inactive" pinctrl state in probe via devm_pinctrl_get() and pinctrl_lookup_state(), and select it with pinctrl_select_state() in remove() and shutdown(). The state is looked up and selected by the driver itself rather than reusing the runtime-PM "idle"/"sleep" states, so its meaning is unambiguous and it does not depend on CONFIG_PM. Boards that do not describe an "inactive" state are unaffected.

Since "inactive" is only meaningful as the mux to restore after the core-applied "default" state, reject an "inactive" state that is not paired with a "default" one rather than releasing pins that were never put into a defined PPS state.

Look up the pinctrl states first in probe(), before pps_gpio_setup(), and route every subsequent failure through a common err_release_pins label. The driver core applies the "default" mux before probe(), so a probe that fails after this point would otherwise leave the pins stuck in "default"; releasing them to "inactive" on the error path is the symmetrical counterpart to what the core did on the driver's behalf. A failure in pps_gpio_get_pins() itself returns directly, as no state was taken yet; pps_gpio_release_pins() is a no-op when no "inactive" state was found.

Convert the pps_register_source() failure path to dev_err_probe() too, so all three probe error paths that share err_release_pins log the same way. This path already returned PTR_ERR(data->pps), so this is a logging change, not a fix.

The mux must not change while something can still drive the pins. On shutdown() the requested IRQ and the echo timer would otherwise outlive the mux change -- device_shutdown() is not the end of the road, the kernel keeps running to load and start the kexec image -- so a timer callback or the PPS handler could poke a line that by then belongs to another function. Tear down in the same order as remove(): free_irq() first, then the echo timer (only when the board has an echo GPIO, as in remove()), and the mux change last. shutdown() does not unregister the PPS source, which is a remove-time concern.

Quoted 1 line

v6 looks reasonable to me!

Yours,
Linus Walleij

author Reviewed-by Bartosz Golaszewski Reviewed-by Bartosz Golaszewski Reviewed-by Rob Herring (Arm) Reviewed-by Linus Walleij
#56 lore raw
In reply to Rodolfo Giometti
Quoted 7 lines · Rodolfo Giometti

I applied the series here on v7.3-rc4: it applies cleanly, checkpatch --strict is quiet on all four patches, and drivers/pps builds with W=1 and CONFIG_DEBUG_OBJECTS_TIMERS=y. I have acked the four patches individually.

Andrew, could you please pick this up? LinuxPPS has no tree of its own.

Hi Andrew,

Following up on Rodolfo's request above. The awkward part here is mine: v6 had you on Cc rather than To, and I posted it as a reply to v5 rather than its own thread, so the patches are addressed to other people and buried mid-thread. Happy to fix that if it helps.

The series is unchanged since v6 and has no outstanding review comments. I re-checked it today and all four patches still apply cleanly on v7.4-rc1. v6 is nested in the v5 thread, here:

https://lore.kernel.org/all/[email protected]/T/

Tags collected after v6 was posted:

Patch 1: Acked-by: Rodolfo Giometti <[email protected]>

Patch 2: Acked-by: Rodolfo Giometti <[email protected]>

Patch 3: Acked-by: Rodolfo Giometti <[email protected]>

Patch 4: Acked-by: Rodolfo Giometti <[email protected]>

Patches 1 and 2 are fixes with Fixes: tags; 3 and 4 are the feature.

Quoted 4 lines

One leftover from the v5 review, not a blocker: the new shutdown() runs on every board, including the ones that describe no "inactive" state [...] If the series needs a respin for some other reason, a line there would be worth having -- otherwise it is fine as it stands.

Noted, and nothing in the code depends on it.

Would you rather I repost as a standalone v7 thread, with that note folded into patch 4 and the tags above applied? That would also put the patches in front of you properly addressed. Happy either way -- just say which you prefer.

Thanks,
Eliav

#57 lore raw
In reply to Eliav Farber
Quoted 9 lines · Eliav Farber

Some boards route the PPS input GPIO through a pin controller and mux the pins to a different function when the pps-gpio driver is not active.

The driver core already selects the "default" pinctrl state before probe, so the pins can be muxed for GPIO/PPS use while the driver is bound without any driver change. Nothing, however, hands the pins back when the driver is unbound or the system is shut down (for example before kexec), leaving them stuck in the GPIO mux for the next kernel.

Quoted 8 lines

This series lets pps-gpio select an optional "inactive" pinctrl state in remove() and shutdown(), so a board can describe the alternate mux there and have it restored. The state is looked up and selected by the driver itself (devm_pinctrl_get() + pinctrl_lookup_state() + pinctrl_select_ state()), so its meaning is unambiguous and it does not depend on CONFIG_PM. It is a no-op for boards that do not describe an "inactive" state.

Please stop posting new iterations in reply to previous ones. Preferably use b4 for managing patch series. Each new iteration should be its own thread.

Thanks,
Bartosz