Skip to contentSkip to conversation

A proposal to add a gpio-locked fixed clock driver.

PATCH Superseded Vyacheslav Yurkov 5 participants 8 messages Jun 3 – Jun 16

Subsystems (2)

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v3 cover A proposal to add a gpio-locked fixed clock driver.

A gpio-locked fixed clock aggregates one or more input clocks and/or one or more GPIOs. It's similar to a gated-fixed-clock, but GPIO direction is inverted. Consumers can use the output clock to wait until all input clocks are locked and only then initialize / access dependent peripherals.

The usage example for such a driver is when peripherals depend on PLLs in a FPGA, which can't be directly accessed by the CPU, but need a GPIO pin to check whether clock is actually usable. E.g. some of the IPs might not have a proper split between registers and IP core, which means that if an external clock and/or PLL lock is missing and one tries to access the registers, the response never comes, thus the CPU stalls.

---
Changes in v3:
- Removed unnecessary dt bindings
- Improved HW description and commit messages
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Renamed to clk-gpio-locked to express intent.
- Provide enable() / is_enabled() operations so the clock behaves as
expected
- Fixed DTS errors / warnings
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Vyacheslav Yurkov (2):
dt-bindings: Add GPIO-locked fixed clock
clk: Add gpio-locked fixed clock driver

 .../bindings/clock/gpio-locked-fixed-clock.yaml    |  70 +++++
 drivers/clk/Makefile                               |   1 +
 drivers/clk/clk-gpio-locked.c                      | 306 +++++++++++++++++++++
 3 files changed, 377 insertions(+)

---
base-commit: ba3e43a9e601636f5edb54e259a74f96ca3b8fd8
change-id: 20260318-feature-clock-guard-f20a2c35b965

Best regards,

Signature · 1 line
Vyacheslav Yurkov <[email protected]>
#1 lore raw

PATCH v3 1/2 dt-bindings: Add GPIO-locked fixed clock

Some hardware designs provide fixed-frequency clocks generated outside software control, such as by FPGA-resident PLLs. While the clock rate is fixed, a separate GPIO signal indicates whether the clock source is locked and producing a valid output.

Describe a GPIO-locked fixed clock provider that exposes a fixed-rate clock whose availability depends on one or more GPIO lock-status signals.

---
 .../bindings/clock/gpio-locked-fixed-clock.yaml    | 70 ++++++++++++++++++++++
 1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +70 -0
new file mode 100644
index 000000000000..9106b800b673
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/gpio-locked-fixed-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO Locked Fixed Clock
+
+maintainers:
+  - Vyacheslav Yurkov <[email protected]>
+
+description: |
+  Provides a clock output whose availability depends on a set of
+  prerequisite conditions. These conditions include the presence of
+  one or more parent clocks and the asserted state of one or more
+  GPIO lock indicators. An example of such clocks is FPGA clock that
+  are outside CPU control, with the lock status exposed through GPIO
+  signal.
+
+  The output clock is considered available only when all configured
+  prerequisites are satisfied.
+
+properties:
+  compatible:
+    const: gpio-locked-fixed-clock
+
+  "#clock-cells":
+    const: 0
+
+  clocks:
+    description: Input clocks whose validity is monitored by this provider.
+
+  clock-output-names:
+    description: Names of the clock provided by this controller.
+    maxItems: 1
+
+  locked-gpios:
+    description: |
+      GPIOs to check the lock state.
+    minItems: 1
+    maxItems: 32
+
+required:
+  - compatible
+  - "#clock-cells"
+
+anyOf:
+  - required:
+      - clocks
+  - required:
+      - locked-gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    clk_gpio_locked: gpio-locked-fixed-clock {
+        compatible = "gpio-locked-fixed-clock";
+        #clock-cells = <0>;
+
+        clocks = <&clk0 0>, <&pll 0>;
+
+        locked-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>,
+                <&gpio0 5 GPIO_ACTIVE_HIGH>,
+                <&gpio1 2 GPIO_ACTIVE_LOW>;
+
+        clock-output-names = "clkout0";
+    };
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: Add GPIO-locked fixed clock

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] The driver fails to probe with `-EINVAL` because it incorrectly uses `devm_clk_bulk_get_all` IDs…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] The driver fails to probe with -EINVAL because it incorrectly uses devm_clk_bulk_get_all IDs as CCF parent_names.
- [High] The driver registers multiple required input clocks as CCF parents, causing clk_enable() to permanently fail.
- [High] The binding lacks a clock-frequency property, resulting in a 0 Hz clock when no parents are specified.
- [High] The .is_enabled callback incorrectly returns 0 when the clock is ready and a negative error code when it is not.
- [Medium] The driver reads the 'gpio-names' property which is missing from the dt-binding schema.
- [Low] The clocks property is missing an array size constraint (maxItems or items).
- [Low] The example uses a device-specific node name instead of a generic node name.
--

commit f442ab0fb77267ab8b660fa86301ab0aa027e186
Author: Vyacheslav Yurkov <[email protected]>
Subject: dt-bindings: Add GPIO-locked fixed clock

This commit introduces a new device tree binding and corresponding driver support for a GPIO-locked fixed clock provider. The clock's availability depends on prerequisite conditions, including parent clocks and GPIO lock indicators. This allows modeling FPGA-generated clocks that expose lock status through GPIOs.

Quoted 5 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +0 -0
new file mode 100644
@@ -0,0 +1,70 @@

[ ... ]

Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml line 31

Quoted 9 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +9 -0
+properties:
+  compatible:
+    const: gpio-locked-fixed-clock
+
+  "#clock-cells":
+    const: 0
+
+  clocks:
+    description: Input clocks whose validity is monitored by this provider.

[Severity: Low]
Does the clocks property need an array size constraint such as maxItems or items? Without it, dt_binding_check might emit a meta-schema validation error.

[ ... ]

Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml line 59

Quoted 7 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +7 -0
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    clk_gpio_locked: gpio-locked-fixed-clock {

[Severity: Low]
Is gpio-locked-fixed-clock the appropriate node name here? Standard device tree rules typically require generic node names (e.g., clock-controller { or clock {) rather than device-specific names.

Quoted 1 line

+ compatible = "gpio-locked-fixed-clock";

[ ... ]

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +0 -0
new file mode 100644
@@ -0,0 +1,306 @@

[ ... ]

Quoted 14 lines
drivers/clk/clk-gpio-locked.c new file +14 -0
+static int gpio_locked_clk_is_enabled(struct clk_hw *hw)
+{
+	struct gpio_locked_clk_priv *priv = to_gpio_locked_clk_priv(hw);
+	int ret = 0;
+
+	if (priv->num_gpios > 0) {
+		ret = is_gpio_ready(priv);
+		if (ret < 0)
+			return ret;
+	}
+
+	// Now check for the clocks
+	for (int i = 0; i < priv->num_clks; i++) {
+		struct clk_hw *hw_clk = __clk_get_hw(priv->clks[i].clk);

drivers/clk/clk-gpio-locked.c

Quoted 10 lines
drivers/clk/clk-gpio-locked.c new file +10 -0
+
+		if (!clk_hw_is_enabled(hw_clk)) {
+			dev_dbg(priv->dev, "Clock %i (%s) is not ready",
+				i, priv->clks[i].id);
+			return -EBUSY;
+		}
+	}
+
+	return 0;
+}

[Severity: High]
Does .is_enabled correctly return the clock status here?

The Common Clock Framework expects this callback to return a non-zero value (true) when the clock is ready, and 0 (false) when it is disabled. Returning 0 on success and negative error codes (like -EBUSY) on failure might cause CCF to implicitly cast the negative error codes to true, falsely assuming the clock is enabled when it is actually unavailable.

[ ... ]

drivers/clk/clk-gpio-locked.c

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +5 -0
+static const struct clk_ops gpio_locked_clk_ops = {
+	.enable =	gpio_locked_clk_enable,
+	.is_enabled =	gpio_locked_clk_is_enabled,
+	.get_parent =	gpio_locked_clk_get_parent,
+};

[Severity: High]
The commit message mentions this provider exposes a fixed-rate clock, but the clock-frequency property is missing from the binding, and .recalc_rate is not implemented here.

If the clocks property is omitted in the device tree (which the schema allows), won't the root clock default to 0 Hz? How will downstream consumers know the correct clock rate without a way to define it?

[ ... ]

Quoted 2 lines
drivers/clk/clk-gpio-locked.c new file +2 -0
+static int gpio_locked_clk_parse_gpios(struct gpio_locked_clk_priv *priv)
+{

[ ... ]

drivers/clk/clk-gpio-locked.c

Quoted 3 lines
drivers/clk/clk-gpio-locked.c new file +3 -0
+	for (i = 0; i < priv->num_gpios; i++) {
+		of_property_read_string_index(np, "gpio-names", i,
+				&priv->gpio_names[i]);

[Severity: Medium]
Will this logic cause dt_binding_check to fail if a device tree includes gpio-names?

The driver attempts to read the gpio-names property, but it is missing from the dt-binding schema, which strictly enforces additionalProperties:
false. If it is omitted from the device tree to pass validation, won't priv->gpio_names[i] remain NULL and cause debug warnings?

[ ... ]

Quoted 2 lines
drivers/clk/clk-gpio-locked.c new file +2 -0
+static int gpio_locked_clk_parse_outputs(struct gpio_locked_clk_priv *priv)
+{

[ ... ]

drivers/clk/clk-gpio-locked.c

Quoted 4 lines
drivers/clk/clk-gpio-locked.c new file +4 -0
+	init.name = priv->output_clock_name;
+	init.ops = &gpio_locked_clk_ops;
+	init.flags = 0;
+	init.num_parents = priv->num_clks;

[Severity: High]
Can clk_enable() permanently fail if there are multiple input clocks?

By setting init.num_parents = priv->num_clks, this configures the clock as a CCF multiplexer. However, CCF's clk_core_enable() only enables the single active parent returned by .get_parent() (which the driver hardcodes to return 0). Since the .is_enabled() callback mandates that all input clocks must be enabled, won't the check fail and return -EBUSY for the other unenabled parents?

drivers/clk/clk-gpio-locked.c

Quoted 13 lines
drivers/clk/clk-gpio-locked.c new file +13 -0
+
+	if (priv->num_clks) {
+		const char **parent_names;
+		int j;
+
+		parent_names = devm_kcalloc(dev, priv->num_clks,
+					    sizeof(*parent_names),
+					    GFP_KERNEL);
+		if (!parent_names)
+			return -ENOMEM;
+
+		for (j = 0; j < priv->num_clks; j++)
+			parent_names[j] = priv->clks[j].id;

[Severity: High]
Is it safe to assign priv->clks[j].id to parent_names[j] here?

Since devm_clk_bulk_get_all() populates clks[i].id from the clock-names property, and clock-names is explicitly omitted from the DT binding schema (and forbidden by additionalProperties: false), won't clks[i].id evaluate to NULL?

This could cause clk_core_populate_parent_map() to emit a WARN and return -EINVAL during registration. Would using .parent_hws instead of .parent_names be a more reliable approach?

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

Re: [PATCH v3 1/2] dt-bindings: Add GPIO-locked fixed clock

In reply to Vyacheslav Yurkov

Hi Vyacheslav,

Quoted 34 lines · On Wed, Jun 03, 2026 at 11:16:42AM +0000, Vyacheslav Yurkov via B4 Relay wrote:

From: Vyacheslav Yurkov <[email protected]>

Some hardware designs provide fixed-frequency clocks generated outside software control, such as by FPGA-resident PLLs. While the clock rate is fixed, a separate GPIO signal indicates whether the clock source is locked and producing a valid output.

Describe a GPIO-locked fixed clock provider that exposes a fixed-rate clock whose availability depends on one or more GPIO lock-status signals.

---
 .../bindings/clock/gpio-locked-fixed-clock.yaml    | 70 ++++++++++++++++++++++
 1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +11 -0
new file mode 100644
index 000000000000..9106b800b673
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/gpio-locked-fixed-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO Locked Fixed Clock
+
+maintainers:
+  - Vyacheslav Yurkov <[email protected]>
+

Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml line 21

Quoted 10 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +10 -0
+description: |
+  Provides a clock output whose availability depends on a set of
+  prerequisite conditions. These conditions include the presence of
+  one or more parent clocks and the asserted state of one or more
+  GPIO lock indicators. An example of such clocks is FPGA clock that
+  are outside CPU control, with the lock status exposed through GPIO
+  signal.
+
+  The output clock is considered available only when all configured
+  prerequisites are satisfied.

I'm stepping outside my usual review of just the clk drivers. Krzysztof in v1 and v2 asked for more detailed hardware explanation. This feels to me like this is a policy that says to not use these clocks until the GPIO says they are ready. My gut feeling is that details like this should live in a clk driver instead of a dt-binding.

Alternatively, if this is generic enough, then could Documentation/devicetree/bindings/clock/gated-fixed-clock.yaml be extended?

Brian

Quoted 54 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +49 -0
+
+properties:
+  compatible:
+    const: gpio-locked-fixed-clock
+
+  "#clock-cells":
+    const: 0
+
+  clocks:
+    description: Input clocks whose validity is monitored by this provider.
+
+  clock-output-names:
+    description: Names of the clock provided by this controller.
+    maxItems: 1
+
+  locked-gpios:
+    description: |
+      GPIOs to check the lock state.
+    minItems: 1
+    maxItems: 32
+
+required:
+  - compatible
+  - "#clock-cells"
+
+anyOf:
+  - required:
+      - clocks
+  - required:
+      - locked-gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    clk_gpio_locked: gpio-locked-fixed-clock {
+        compatible = "gpio-locked-fixed-clock";
+        #clock-cells = <0>;
+
+        clocks = <&clk0 0>, <&pll 0>;
+
+        locked-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>,
+                <&gpio0 5 GPIO_ACTIVE_HIGH>,
+                <&gpio1 2 GPIO_ACTIVE_LOW>;
+
+        clock-output-names = "clkout0";
+    };
Signature · 2 lines
-- 
2.34.1
#4 lore raw

Re: [PATCH v3 1/2] dt-bindings: Add GPIO-locked fixed clock

Hi Brian

Quoted 54 lines · Brian Masney

Hi Vyacheslav,

Quoted 34 lines · On Wed, Jun 03, 2026 at 11:16:42AM +0000, Vyacheslav Yurkov via B4 Relay wrote:

From: Vyacheslav Yurkov <[email protected]>

Some hardware designs provide fixed-frequency clocks generated outside software control, such as by FPGA-resident PLLs. While the clock rate is fixed, a separate GPIO signal indicates whether the clock source is locked and producing a valid output.

Describe a GPIO-locked fixed clock provider that exposes a fixed-rate clock whose availability depends on one or more GPIO lock-status signals.

---
  .../bindings/clock/gpio-locked-fixed-clock.yaml    | 70 ++++++++++++++++++++++
  1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +11 -0
new file mode 100644
index 000000000000..9106b800b673
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/gpio-locked-fixed-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO Locked Fixed Clock
+
+maintainers:
+  - Vyacheslav Yurkov <[email protected]>
+

Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml line 21

Quoted 10 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +10 -0
+description: |
+  Provides a clock output whose availability depends on a set of
+  prerequisite conditions. These conditions include the presence of
+  one or more parent clocks and the asserted state of one or more
+  GPIO lock indicators. An example of such clocks is FPGA clock that
+  are outside CPU control, with the lock status exposed through GPIO
+  signal.
+
+  The output clock is considered available only when all configured
+  prerequisites are satisfied.

I'm stepping outside my usual review of just the clk drivers. Krzysztof in v1 and v2 asked for more detailed hardware explanation. This feels to me like this is a policy that says to not use these clocks until the GPIO says they are ready. My gut feeling is that details like this should live in a clk driver instead of a dt-binding.

Quoted 5 lines

Alternatively, if this is generic enough, then could Documentation/devicetree/bindings/clock/gated-fixed-clock.yaml be extended?

Brian

Does it mean I should drop driver description from the DT schema?

So you are implying that gated-fixed-clock.yaml can be extended with another compatibility string? When another compatibility string is used, then some other bindings are expected by the driver, not what is used by gated-fixed-clock driver. Is that a common pattern among dt-bindings to share the bindings between different compatibility strings?

Slava

#5 lore raw

Re: [PATCH v3 1/2] dt-bindings: Add GPIO-locked fixed clock

In reply to Brian Masney
Quoted 54 lines · Brian Masney

Hi Vyacheslav,

Quoted 34 lines · On Wed, Jun 03, 2026 at 11:16:42AM +0000, Vyacheslav Yurkov via B4 Relay wrote:

From: Vyacheslav Yurkov <[email protected]>

Some hardware designs provide fixed-frequency clocks generated outside software control, such as by FPGA-resident PLLs. While the clock rate is fixed, a separate GPIO signal indicates whether the clock source is locked and producing a valid output.

Describe a GPIO-locked fixed clock provider that exposes a fixed-rate clock whose availability depends on one or more GPIO lock-status signals.

---
 .../bindings/clock/gpio-locked-fixed-clock.yaml    | 70 ++++++++++++++++++++++
 1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +11 -0
new file mode 100644
index 000000000000..9106b800b673
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/gpio-locked-fixed-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO Locked Fixed Clock
+
+maintainers:
+  - Vyacheslav Yurkov <[email protected]>
+

Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml line 21

Quoted 10 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +10 -0
+description: |
+  Provides a clock output whose availability depends on a set of
+  prerequisite conditions. These conditions include the presence of
+  one or more parent clocks and the asserted state of one or more
+  GPIO lock indicators. An example of such clocks is FPGA clock that
+  are outside CPU control, with the lock status exposed through GPIO
+  signal.
+
+  The output clock is considered available only when all configured
+  prerequisites are satisfied.

I'm stepping outside my usual review of just the clk drivers. Krzysztof in v1 and v2 asked for more detailed hardware explanation. This feels to me like this is a policy that says to not use these clocks until the GPIO says they are ready. My gut feeling is that details like this should live in a clk driver instead of a dt-binding.

Quoted 3 lines

Alternatively, if this is generic enough, then could Documentation/devicetree/bindings/clock/gated-fixed-clock.yaml be extended?

FWIW this came up in an earlier revision:
https://lore.kernel.org/all/20260407-fling-scouring-dbe2141cc79b@spud/

IMO it's an inverted gpio-gate-clock, where the gpio is an input rather than an output. I suppose you could extend gpio-gate-clock with it and have mutually exclusive enable-gpios and status-gpios?

Quoted 59 lines

Brian

Quoted 54 lines
Documentation/devicetree/bindings/clock/gpio-locked-fixed-clock.yaml new file +49 -0
+
+properties:
+  compatible:
+    const: gpio-locked-fixed-clock
+
+  "#clock-cells":
+    const: 0
+
+  clocks:
+    description: Input clocks whose validity is monitored by this provider.
+
+  clock-output-names:
+    description: Names of the clock provided by this controller.
+    maxItems: 1
+
+  locked-gpios:
+    description: |
+      GPIOs to check the lock state.
+    minItems: 1
+    maxItems: 32
+
+required:
+  - compatible
+  - "#clock-cells"
+
+anyOf:
+  - required:
+      - clocks
+  - required:
+      - locked-gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    clk_gpio_locked: gpio-locked-fixed-clock {
+        compatible = "gpio-locked-fixed-clock";
+        #clock-cells = <0>;
+
+        clocks = <&clk0 0>, <&pll 0>;
+
+        locked-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>,
+                <&gpio0 5 GPIO_ACTIVE_HIGH>,
+                <&gpio1 2 GPIO_ACTIVE_LOW>;
+
+        clock-output-names = "clkout0";
+    };
Signature · 2 lines
-- 
2.34.1
#6 lore raw

PATCH v3 2/2 clk: Add gpio-locked fixed clock driver

In reply to Vyacheslav Yurkov

A gpio-locked clock aggregates one or more input clocks and/or one or more GPIOs. It's a FPGA-assisted clocking design where peripheral clocks are generated by FPGA PLLs that are outside CPU control, with clock-valid/PLL-lock status exposed through GPIO signals. Consumers can use the output clock to wait until all input clocks are locked and only then initialize dependent peripherals.

---
 drivers/clk/Makefile          |   1 +
 drivers/clk/clk-gpio-locked.c | 306 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 307 insertions(+)
drivers/clk/Makefile +1 -0
index a3e2862ebd7e..464ca1c1d8a8 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_CLK_FD_KUNIT_TEST) += clk-fractional-divider_test.o
 obj-$(CONFIG_COMMON_CLK)	+= clk-gpio.o
 ifeq ($(CONFIG_OF), y)
 obj-$(CONFIG_COMMON_CLK)	+= clk-conf.o
+obj-$(CONFIG_COMMON_CLK)	+= clk-gpio-locked.o
 endif
 
 # KUnit specific helpers
drivers/clk/clk-gpio-locked.c new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v3 2/2] clk: Add gpio-locked fixed clock driver

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] The driver reuses its `.enable` function for `.is_enabled`, returning 0 for ready and `-EBUSY` for…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] The driver reuses its .enable function for .is_enabled, returning 0 for ready and -EBUSY for not ready, which inverts CCF's .is_enabled boolean semantics.
- [High] The driver returns -EBUSY immediately from .enable() instead of internally waiting for the clock to lock, inappropriately forcing consumers to poll.
- [High] The driver attempts to aggregate multiple input clocks using num_parents > 1, but CCF treats multiple parents as a multiplexer (MUX), not a logical AND gate.
- [High] The driver populates init.parent_names with local consumer connection IDs instead of globally registered clock names, breaking parent clock resolution.
- [High] The driver violates API boundaries by using __clk_get_hw() on consumer struct clk pointers to check clk_hw_is_enabled(), which only checks software state and risks use-after-free.
- [High] Calling gpiod_get_array_value() from .enable() and .is_enabled() triggers a sleeping-in-atomic BUG if the GPIOs are on a slow bus.
- [Medium] Input GPIOs are requested with GPIOD_ASIS instead of GPIOD_IN, failing to guarantee the lines are configured as inputs.
--

commit a891e5fe0dbdc27c3cfbbf69bfe6b147ef67ce9c
Author: Vyacheslav Yurkov <[email protected]>

clk: Add gpio-locked fixed clock driver

A gpio-locked clock aggregates one or more input clocks and/or one or more GPIOs. It's a FPGA-assisted clocking design where peripheral clocks are generated by FPGA PLLs that are outside CPU control, with clock-valid/PLL-lock status exposed through GPIO signals. Consumers can use the output clock to wait until all input clocks are locked and only then initialize dependent peripherals.

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +0 -0
new file mode 100644
index 0000000000000..79098f9b65326

[ ... ]

drivers/clk/clk-gpio-locked.c line 61

Quoted 12 lines
drivers/clk/clk-gpio-locked.c new file +12 -0
+static int is_gpio_ready(struct gpio_locked_clk_priv *priv)
+{
+	unsigned long values[BITS_TO_LONGS(MAX_INPUT_GPIO_COUNT)] = {0};
+	int ret = 0;
+
+	if (priv->num_gpios == 0)
+		return 0;
+
+	ret = gpiod_get_array_value(priv->gpios->ndescs,
+		priv->gpios->desc,
+		priv->gpios->info,
+		values);

[Severity: High]
Will this trigger a sleeping in atomic context warning if the GPIOs are on a slow bus?

CCF .enable() and .is_enabled() callbacks execute in an atomic context under the global enable_lock spinlock with IRQs disabled. If the GPIOs are on an I2C/SPI expander, gpiod_get_array_value() requires a sleepable context.

Should .prepare() and .is_prepared() be used instead?

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +5 -0
+
+	if (ret) {
+		dev_err(priv->dev, "Failed to read GPIOs");
+		return -EIO;
+	}

[ ... ]

drivers/clk/clk-gpio-locked.c line 87

Quoted 10 lines
drivers/clk/clk-gpio-locked.c new file +10 -0
+static int gpio_locked_clk_is_enabled(struct clk_hw *hw)
+{
+	struct gpio_locked_clk_priv *priv = to_gpio_locked_clk_priv(hw);
+	int ret = 0;
+
+	if (priv->num_gpios > 0) {
+		ret = is_gpio_ready(priv);
+		if (ret < 0)
+			return ret;
+	}

[Severity: High]
Does this logic invert the CCF state machine?

The framework expects .is_enabled() to return non-zero if the clock is enabled and 0 if disabled. This function returns 0 when ready, and -EBUSY when not ready. Because -EBUSY (-16) is non-zero, CCF interprets a 'not ready' state as 'enabled', and a 'ready' state as 'disabled'.

drivers/clk/clk-gpio-locked.c line 93

Quoted 6 lines
drivers/clk/clk-gpio-locked.c new file +6 -0
+
+	// Now check for the clocks
+	for (int i = 0; i < priv->num_clks; i++) {
+		struct clk_hw *hw_clk = __clk_get_hw(priv->clks[i].clk);
+
+		if (!clk_hw_is_enabled(hw_clk)) {

[Severity: High]
Is it safe to use __clk_get_hw() on arbitrary consumer struct clk pointers?

This internal CCF macro returns a raw pointer that could be freed if the provider unbinds, creating a use-after-free risk. Additionally, clk_hw_is_enabled() only checks the software enable_count rather than actual hardware PLL lock states.

Quoted 6 lines
drivers/clk/clk-gpio-locked.c new file +6 -0
+			dev_dbg(priv->dev, "Clock %i (%s) is not ready",
+				i, priv->clks[i].id);
+			return -EBUSY;
+		}
+	}
+

drivers/clk/clk-gpio-locked.c line 109

Quoted 10 lines
drivers/clk/clk-gpio-locked.c new file +10 -0
+	return 0;
+}
+
+/* We can't enable the clock, but the Common Clock Framework calls only
+ * enable() not is_enabled()
+ */
+static int gpio_locked_clk_enable(struct clk_hw *hw)
+{
+	return gpio_locked_clk_is_enabled(hw);
+}

[Severity: High]
Can this cause consumers to fail instantly without waiting for the clock to lock?

Standard clock consumers expect clk_prepare_enable() to block until the clock is stable or return a timeout error. By returning -EBUSY immediately without polling, consumer drivers calling clk_prepare_enable() during probe will instantly fail if the clock isn't already locked.

Should the clock provider poll the lock status internally (preferably within its .prepare() callback)?

drivers/clk/clk-gpio-locked.c line 117

Quoted 8 lines
drivers/clk/clk-gpio-locked.c new file +8 -0
+
+/* We have to implement it, but we are not going to control
+ * parent clock selection
+ */
+static u8 gpio_locked_clk_get_parent(struct clk_hw *hw)
+{
+	return 0;
+}

[Severity: High]
Does CCF treat num_parents > 1 as a multiplexer rather than an AND-gate?

When CCF propagates clk_enable(), it only enables the single active parent determined by .get_parent(), which hardcodes to 0 here. This means input clocks 1 through N-1 are never automatically enabled, causing the checks in gpio_locked_clk_is_enabled() to permanently fail unless those clocks happen to be enabled externally.

Quoted 6 lines
drivers/clk/clk-gpio-locked.c new file +6 -0
+
+static const struct clk_ops gpio_locked_clk_ops = {
+	.enable =	gpio_locked_clk_enable,
+	.is_enabled =	gpio_locked_clk_is_enabled,
+	.get_parent =	gpio_locked_clk_get_parent,
+};

[ ... ]

drivers/clk/clk-gpio-locked.c line 155

Quoted 7 lines
drivers/clk/clk-gpio-locked.c new file +7 -0
+static int gpio_locked_clk_parse_gpios(struct gpio_locked_clk_priv *priv)
+{
+	struct device *dev = priv->dev;
+	struct device_node *np = dev->of_node;
+	int i;
+
+	priv->gpios = devm_gpiod_get_array_optional(dev, "locked", GPIOD_ASIS);

[Severity: Medium]
Does requesting with GPIOD_ASIS guarantee the lines are configured as inputs?

If the bootloader left the pins in an output state, reading them might return the internal output latch value instead of the external signal. Should GPIOD_IN be used instead?

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +5 -0
+	if (IS_ERR(priv->gpios)) {
+		dev_err(dev, "failed to get GPIO array: %ld\n",
+				PTR_ERR(priv->gpios));
+		return PTR_ERR(priv->gpios);
+	}

[ ... ]

Quoted 6 lines
drivers/clk/clk-gpio-locked.c new file +6 -0
+static int gpio_locked_clk_parse_outputs(struct gpio_locked_clk_priv *priv)
+{
+	struct device *dev = priv->dev;
+	struct device_node *np = dev->of_node;
+	struct clk_init_data init = {};
+	int ret;

[ ... ]

Quoted 4 lines
drivers/clk/clk-gpio-locked.c new file +4 -0
+	if (priv->num_clks) {
+		const char **parent_names;
+		int j;
+

drivers/clk/clk-gpio-locked.c line 222

Quoted 10 lines
drivers/clk/clk-gpio-locked.c new file +10 -0
+		parent_names = devm_kcalloc(dev, priv->num_clks,
+					    sizeof(*parent_names),
+					    GFP_KERNEL);
+		if (!parent_names)
+			return -ENOMEM;
+
+		for (j = 0; j < priv->num_clks; j++)
+			parent_names[j] = priv->clks[j].id;
+
+		init.parent_names = parent_names;

[Severity: High]
Does this assign local consumer connection names to init.parent_names instead of globally registered clock names?

devm_clk_bulk_get_all() fills the id field with the local consumer connection name from the clock-names DT property, but init.parent_names expects global provider names. This could prevent CCF from linking the clock hierarchy correctly.

Could init.parent_hws or init.parent_data be used instead?

Quoted 5 lines
drivers/clk/clk-gpio-locked.c new file +5 -0
+	}
+
+	priv->output_hw_clk.init = &init;
+
+	ret = devm_clk_hw_register(dev, &priv->output_hw_clk);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2