Skip to contentSkip to conversation

A proposal to add a gpio-enabled clock driver.

PATCH Superseded Vyacheslav Yurkov 5 participants 10 messages Oct 15 – Nov 21

Subsystems (2)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover A proposal to add a gpio-enabled clock driver.

A gpio-enabled clock represents an input clock, which state is determined by a GPIO signal. It's similar to a gated-fixed-clock, but GPIO direction is inverted. Consumers can use the output clock to wait until the clock is ready 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 v9:
- Changed wordking from "locked" to "enabled" to be more generic
- Use a separate DT schema file similar to other clock drivers
- Add a timeout for polling in enable()/prepare()
- Depending on whether GPIO can sleep or not use prepare() or enable()
set of ops respectively.
- Decouple the implementation from exisiting drivers
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Fix return value of clk_gpio_locked_prepare()
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Fix compatibility strings and schema errors.
- Update GPIO pin name
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Debug messages dropped.
- The new driver is now an extension of exising clk-gpio drivers.
- The controller doesn't enable the parent clock, the consumer is
supposed to do that instead.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Use existing DT binding with an additional property instead of adding
a new one
- The driver is simplified in a way that it represents the actual HW
desgin without additional unrelated constructs
- Aggregation is removed, now it's one clock input, one clock output
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Removed driver specifics from DT binding
- Link to v3: https://lore.kernel.org/r/[email protected]

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: gpio-enabled-clock: Add a new binding
clk: Add gpio-enabled clock driver

 .../bindings/clock/gpio-enabled-clock.yaml         |  53 +++++++
 drivers/clk/clk-gpio.c                             | 161 +++++++++++++++++++++
 2 files changed, 214 insertions(+)

---
base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053
change-id: 20260318-feature-clock-guard-f20a2c35b965

Best regards,

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

PATCH v9 1/2 dt-bindings: gpio-enabled-clock: Add a new binding

Add the binding for gpio-enabled-clock. A GPIO-enabled clock provider exposes a clock (can be any type of clock) whose availability depends on a GPIO status signal.

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 enabled and producing a valid output.

---
 .../bindings/clock/gpio-enabled-clock.yaml         | 53 ++++++++++++++++++++++
 1 file changed, 53 insertions(+)
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +53 -0
new file mode 100644
index 000000000000..3ab6fa9dd412
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/gpio-enabled-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO Enabled Clock
+
+maintainers:
+  - Vyacheslav Yurkov <[email protected]>
+
+properties:
+  compatible:
+    const: gpio-enabled-clock
+
+  clocks:
+    maxItems: 1
+
+  "#clock-cells":
+    const: 0
+
+  clock-output-names:
+    maxItems: 1
+
+  enabled-gpios:
+    description: GPIO that indicates whether the clock is enabled or disabled.
+    maxItems: 1
+
+  poll-timeout-us:
+    description: Timeout in microseconds for waiting for a GPIO signal.
+    default: 10
+    minimum: 10
+    maximum: 1000
+
+required:
+  - compatible
+  - clocks
+  - "#clock-cells"
+  - enabled-gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    clock {
+        compatible = "gpio-enabled-clock";
+        #clock-cells = <0>;
+        clocks = <&pll>;
+        enabled-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
+        poll-timeout-us = <20>;
+    };
Signature · 1 line
2.34.1
maintainer Reviewed-by on v9 1/2
#2 lore raw

Re: [PATCH v9 1/2] dt-bindings: gpio-enabled-clock: Add a new binding

Quoted 9 lines · Vyacheslav Yurkov

Add the binding for gpio-enabled-clock. A GPIO-enabled clock provider exposes a clock (can be any type of clock) whose availability depends on a GPIO status signal.

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 enabled and producing a valid output.

Quoted 5 lines
---
 .../bindings/clock/gpio-enabled-clock.yaml         | 53 ++++++++++++++++++++++
 1 file changed, 53 insertions(+)
#3 lore raw

Re: [PATCH v9 1/2] dt-bindings: gpio-enabled-clock: Add a new binding

In reply to Vyacheslav Yurkov
Quoted 8 lines

Add the binding for gpio-enabled-clock. A GPIO-enabled clock provider exposes a clock (can be any type of clock) whose availability depends on a GPIO status signal.

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 enabled and producing a valid output.

On v5 I pointed out that the input is not fixed in general. It might be your specific use-case but it does not generalize.

Signature · 1 line
Jerome
#4 lore raw

Re: [PATCH v9 1/2] dt-bindings: gpio-enabled-clock: Add a new binding

Quoted 11 lines · Jerome Brunet
Quoted 8 lines

Add the binding for gpio-enabled-clock. A GPIO-enabled clock provider exposes a clock (can be any type of clock) whose availability depends on a GPIO status signal.

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 enabled and producing a valid output.

On v5 I pointed out that the input is not fixed in general. It might be your specific use-case but it does not generalize.

Does this mean you are asking for the commit message to be reworded?

#5 lore raw

Re: [PATCH v9 1/2] dt-bindings: gpio-enabled-clock: Add a new binding

Quoted 10 lines · Conor Dooley
Quoted 9 lines · Jerome Brunet
Quoted 8 lines

Add the binding for gpio-enabled-clock. A GPIO-enabled clock provider exposes a clock (can be any type of clock) whose availability depends on a GPIO status signal.

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 enabled and producing a valid output.

Quoted 4 lines
Quoted 2 lines

On v5 I pointed out that the input is not fixed in general. It might be your specific use-case but it does not generalize.

Does this mean you are asking for the commit message to be reworded?

I would not have asked for this only but since another version is needed for the driver side anyway, I'd like that to happen yes.

Signature · 1 line
Jerome
#6 lore raw

PATCH v9 2/2 clk: Add gpio-enabled clock driver

In reply to Vyacheslav Yurkov

A gpio-enabled clock exposes a clock, which status is determined by a GPIO signal. The common use-case is 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 the input clock is enabled and only then initialize dependent peripherals.

---
 drivers/clk/clk-gpio.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 161 insertions(+)
drivers/clk/clk-gpio.c +161 -0
index 9099c57e2715..c968c1d4cb1c 100644
@@ -15,6 +15,7 @@
 #include <linux/gpio/consumer.h>
 #include <linux/err.h>
 #include <linux/device.h>
+#include <linux/iopoll.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/regulator/consumer.h>
@@ -241,6 +242,166 @@ static struct platform_driver gpio_clk_driver = {
 };
 builtin_platform_driver(gpio_clk_driver);
 
+/**
+ * DOC: Clock which status is determined by a GPIO signal
+ * Traits of this clock:
+ * prepare - clk_prepare (is_prepared) checks for a sleeping gpio
+ * enable - clk_enable (is_enabled) checks for a non-sleeping gpio
+ */
+
+/**
+ * struct clk_gpio_enabled - gpio enabled clock
+ *
+ * @hw:		handle between common and hardware-specific interfaces
+ * @gpiod:	gpio descriptor
+ * @timeout_us:	timeout that is used to get a GPIO value
+ *
+ * Clock, which status is determined by a GPIO signal.
+ *
+ * Implements .enable, .is_enabled or .prepare, .is_prepared
+ */
+struct clk_gpio_enabled {
+	struct clk_hw hw;
+	struct gpio_desc *gpiod;
+	u32 timeout_us;
+};
+
+#define to_clk_gpio_enabled(_hw) container_of(_hw, struct clk_gpio_enabled, hw)
+#define GPIO_ENABLED_TIMEOUT_MIN 10
+#define GPIO_ENABLED_TIMEOUT_MAX 1000
+#define GPIO_ENABLED_TIMEOUT_DEFAULT 10
+
+static int clk_gpio_enabled_sleeping_is_prepared(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+
+	return gpiod_get_value_cansleep(clk->gpiod) > 0;
+}
+
+/* We can't prepare the clock, but the Common Clock Framework calls only
+ * prepare() not is_prepared(), therefore we fallback on the actual GPIO value.
+ * The function clk_gpio_enabled_sleeping_is_prepared() returns the raw boolean
+ * GPIO status (1 if enabled, 0 if not enabled), but the prepare callback
+ * clk_core_prepare() expects 0 for success and non-zero integer for failure.
+ */
+static int clk_gpio_enabled_sleeping_prepare(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+	int value = 0;
+
+	readx_poll_timeout(gpiod_get_value_cansleep, clk->gpiod, value,
+			value != 0, 10, clk->timeout_us);
+	if (value == 1)
+		return 0;
+
+	return -EBUSY;
+}
+
+static const struct clk_ops clk_gpio_enabled_sleeping_ops = {
+	.prepare =	clk_gpio_enabled_sleeping_prepare,
+	.is_prepared =	clk_gpio_enabled_sleeping_is_prepared,
+};
+
+static int clk_gpio_enabled_is_enabled(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+
+	return gpiod_get_value(clk->gpiod) > 0;
+}
+
+/* We can't enable the clock, but the Common Clock Framework calls only
+ * enable() not is_enabled(), therefore we fallback on the actual GPIO value.
+ * The function clk_gpio_enabled_is_enabled() returns the raw boolean
+ * GPIO status (1 if enabled, 0 if not enabled), but the enable callback
+ * clk_core_enable() expects 0 for success and non-zero integer for failure.
+ */
+static int clk_gpio_enabled_enable(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+	int value = 0;
+
+	readx_poll_timeout_atomic(gpiod_get_value, clk->gpiod, value,
+			value != 0, 10, clk->timeout_us);
+
+	/* If the reading timed out or return an error,
+	 * it means the clock is not ready
+	 */
+	if (value == 1)
+		return 0;
+
+	return -EBUSY;
+}
+
+static const struct clk_ops clk_gpio_enabled_ops = {
+	.enable =	clk_gpio_enabled_enable,
+	.is_enabled =	clk_gpio_enabled_is_enabled,
+};
+
+static int gpio_enabled_clk_driver_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct clk_gpio_enabled *clk;
+	const char *clk_name;
+	const struct clk_ops *ops;
+	const struct clk_parent_data gpio_parent_data[] = {
+		{ .index = 0 },
+	};
+	u32 timeout_us = GPIO_ENABLED_TIMEOUT_DEFAULT;
+	int ret = 0;
+
+	clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
+	if (!clk)
+		return -ENOMEM;
+
+	clk->gpiod = devm_gpiod_get(dev, "enabled", GPIOD_IN);
+	if (IS_ERR(clk->gpiod))
+		return dev_err_probe(dev, PTR_ERR(clk->gpiod),
+				     "Can't get enabled GPIO property\n");
+
+	ret = device_property_read_string(dev, "clock-output-names", &clk_name);
+	if (ret)
+		clk_name = fwnode_get_name(dev->fwnode);
+
+	device_property_read_u32(dev, "poll-timeout-us", &timeout_us);
+	if (timeout_us < GPIO_ENABLED_TIMEOUT_MIN ||
+	    timeout_us > GPIO_ENABLED_TIMEOUT_MAX)
+		return dev_err_probe(dev, -EINVAL,
+				     "poll-timeout-us is out of range [%u, %u]\n",
+				     GPIO_ENABLED_TIMEOUT_MIN,
+				     GPIO_ENABLED_TIMEOUT_MAX);
+	clk->timeout_us = timeout_us;
+
+	if (gpiod_cansleep(clk->gpiod))
+		ops = &clk_gpio_enabled_sleeping_ops;
+	else
+		ops = &clk_gpio_enabled_ops;
+
+	clk->hw.init = CLK_HW_INIT_PARENTS_DATA
+		(clk_name, gpio_parent_data, ops, 0);
+
+	/* register the clock */
+	ret = devm_clk_hw_register(dev, &clk->hw);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Failed to register clock\n");
+
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, &clk->hw);
+}
+
+static const struct of_device_id gpio_enabled_clk_match_table[] = {
+	{ .compatible = "gpio-enabled-clock" },
+	{ }
+};
+
+static struct platform_driver gpio_enabled_clk_driver = {
+	.probe		= gpio_enabled_clk_driver_probe,
+	.driver		= {
+		.name	= "gpio-enabled-clk",
+		.of_match_table = gpio_enabled_clk_match_table,
+	},
+};
+builtin_platform_driver(gpio_enabled_clk_driver);
+
 /**
  * DOC: gated fixed clock, controlled with a gpio output and a regulator
  * Traits of this clock:
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v9 2/2] clk: Add gpio-enabled clock driver

Quoted 6 lines

A gpio-enabled clock exposes a clock, which status is determined by a GPIO signal. The common use-case is 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 the input clock is enabled and only then initialize dependent peripherals.

Same feedback here. The is a generic driver, you should therefore describe the generic problem and how you are solving it

Quoted 3 lines

---

It is fine to describe you specific use-case here.

Quoted 16 lines
 drivers/clk/clk-gpio.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 161 insertions(+)
drivers/clk/clk-gpio.c +1 -0
index 918b2c6444ee..bda40fe87c8b 100644
@@ -15,6 +15,7 @@
 #include <linux/gpio/consumer.h>
 #include <linux/err.h>
 #include <linux/device.h>
+#include <linux/iopoll.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/regulator/consumer.h>

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 246

Quoted 5 lines
drivers/clk/clk-gpio.c +2 -0
@@ -241,6 +242,166 @@ static struct platform_driver gpio_clk_driver = {
 };
 builtin_platform_driver(gpio_clk_driver);
 
+/**
+ * DOC: Clock which status is determined by a GPIO signal

DOC ?

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 247

Quoted 1 line
drivers/clk/clk-gpio.c +1 -0
+ * Traits of this clock:

Traits ? this is not rust code ...

Quoted 29 lines
drivers/clk/clk-gpio.c +29 -0
+ * prepare - clk_prepare (is_prepared) checks for a sleeping gpio
+ * enable - clk_enable (is_enabled) checks for a non-sleeping gpio
+ */
+
+/**
+ * struct clk_gpio_enabled - gpio enabled clock
+ *
+ * @hw:		handle between common and hardware-specific interfaces
+ * @gpiod:	gpio descriptor
+ * @timeout_us:	timeout that is used to get a GPIO value
+ *
+ * Clock, which status is determined by a GPIO signal.
+ *
+ * Implements .enable, .is_enabled or .prepare, .is_prepared
+ */
+struct clk_gpio_enabled {
+	struct clk_hw hw;
+	struct gpio_desc *gpiod;
+	u32 timeout_us;
+};
+
+#define to_clk_gpio_enabled(_hw) container_of(_hw, struct clk_gpio_enabled, hw)
+#define GPIO_ENABLED_TIMEOUT_MIN 10
+#define GPIO_ENABLED_TIMEOUT_MAX 1000
+#define GPIO_ENABLED_TIMEOUT_DEFAULT 10
+
+static int clk_gpio_enabled_sleeping_is_prepared(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 286

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+
+	return gpiod_get_value_cansleep(clk->gpiod) > 0;
+}
+
+/* We can't prepare the clock, but the Common Clock Framework calls only
+ * prepare() not is_prepared(), therefore we fallback on the actual GPIO value.
+ * The function clk_gpio_enabled_sleeping_is_prepared() returns the raw boolean
+ * GPIO status (1 if enabled, 0 if not enabled), but the prepare callback
+ * clk_core_prepare() expects 0 for success and non-zero integer for failure.
+ */

This comment has been carried since v5, where the reasoning was already
answered: returning an error from .prepare()/.enable() when the clock isn't running is simply how a provider reports failure, not a workaround for the framework. The claim is also wrong: the framework does call .is_prepared() (clk_unprepare_unused_subtree(), clk_hw_is_prepared()). The rest describes return conventions. Something like this would be enough:

	/* The clock cannot be turned on, only waited for. */

Also, multi-line comments style is wrong.

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 293

Quoted 7 lines
drivers/clk/clk-gpio.c +7 -0
+static int clk_gpio_enabled_sleeping_prepare(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+	int value = 0;
+
+	readx_poll_timeout(gpiod_get_value_cansleep, clk->gpiod, value,
+			value != 0, 10, clk->timeout_us);

^ alignement ?

errors get flattened here: a negative value (e.g. -EIO from an I2C expander) satisfies "value != 0", ends the poll, and is reported as -EBUSY. A timeout is also reported as -EBUSY rather than the -ETIMEDOUT that readx_poll_timeout() already returns.

Quoted 14 lines
drivers/clk/clk-gpio.c +14 -0
+	if (value == 1)
+		return 0;
+
+	return -EBUSY;
+}
+
+static const struct clk_ops clk_gpio_enabled_sleeping_ops = {
+	.prepare =	clk_gpio_enabled_sleeping_prepare,
+	.is_prepared =	clk_gpio_enabled_sleeping_is_prepared,
+};
+
+static int clk_gpio_enabled_is_enabled(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 317

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+
+	return gpiod_get_value(clk->gpiod) > 0;
+}
+
+/* We can't enable the clock, but the Common Clock Framework calls only
+ * enable() not is_enabled(), therefore we fallback on the actual GPIO value.
+ * The function clk_gpio_enabled_is_enabled() returns the raw boolean
+ * GPIO status (1 if enabled, 0 if not enabled), but the enable callback
+ * clk_core_enable() expects 0 for success and non-zero integer for failure.
+ */

Seems like a repeat

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 324

Quoted 7 lines
drivers/clk/clk-gpio.c +7 -0
+static int clk_gpio_enabled_enable(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+	int value = 0;
+
+	readx_poll_timeout_atomic(gpiod_get_value, clk->gpiod, value,
+			value != 0, 10, clk->timeout_us);

Same error handling as in the prepare variant above.

Also looking at it more closely, the binding allow 1ms max. It is lot of time holding a spinlock without IRQ. If you don't stricly need this variant please drop it for now.

Quoted 29 lines
drivers/clk/clk-gpio.c +29 -0
+
+	/* If the reading timed out or return an error,
+	 * it means the clock is not ready
+	 */
+	if (value == 1)
+		return 0;
+
+	return -EBUSY;
+}
+
+static const struct clk_ops clk_gpio_enabled_ops = {
+	.enable =	clk_gpio_enabled_enable,
+	.is_enabled =	clk_gpio_enabled_is_enabled,
+};
+
+static int gpio_enabled_clk_driver_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct clk_gpio_enabled *clk;
+	const char *clk_name;
+	const struct clk_ops *ops;
+	const struct clk_parent_data gpio_parent_data[] = {
+		{ .index = 0 },
+	};
+	u32 timeout_us = GPIO_ENABLED_TIMEOUT_DEFAULT;
+	int ret = 0;
+
+	clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
+	if (!clk)

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 363

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+		return -ENOMEM;
+
+	clk->gpiod = devm_gpiod_get(dev, "enabled", GPIOD_IN);
+	if (IS_ERR(clk->gpiod))
+		return dev_err_probe(dev, PTR_ERR(clk->gpiod),
+				     "Can't get enabled GPIO property\n");
+
+	ret = device_property_read_string(dev, "clock-output-names", &clk_name);
+	if (ret)
+		clk_name = fwnode_get_name(dev->fwnode);

Just make it a required property

Quoted 4 lines
drivers/clk/clk-gpio.c +4 -0
+
+	device_property_read_u32(dev, "poll-timeout-us", &timeout_us);
+	if (timeout_us < GPIO_ENABLED_TIMEOUT_MIN ||
+	    timeout_us > GPIO_ENABLED_TIMEOUT_MAX)

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 377

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+		return dev_err_probe(dev, -EINVAL,
+				     "poll-timeout-us is out of range [%u, %u]\n",
+				     GPIO_ENABLED_TIMEOUT_MIN,
+				     GPIO_ENABLED_TIMEOUT_MAX);
+	clk->timeout_us = timeout_us;
+
+	if (gpiod_cansleep(clk->gpiod))
+		ops = &clk_gpio_enabled_sleeping_ops;
+	else
+		ops = &clk_gpio_enabled_ops;

As I noted above, I prefer if you dropped the enabled variant that run with irq off, unless you scrictly need it. Return -ENOTSUPP if the gpio can't sleep.

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 380

Quoted 3 lines
drivers/clk/clk-gpio.c +3 -0
+
+	clk->hw.init = CLK_HW_INIT_PARENTS_DATA
+		(clk_name, gpio_parent_data, ops, 0);

CLK_SET_RATE_PARENT please.

Quoted 13 lines
drivers/clk/clk-gpio.c +13 -0
+
+	/* register the clock */
+	ret = devm_clk_hw_register(dev, &clk->hw);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Failed to register clock\n");
+
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, &clk->hw);
+}
+
+static const struct of_device_id gpio_enabled_clk_match_table[] = {
+	{ .compatible = "gpio-enabled-clock" },
+	{ }

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 403

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+};
+
+static struct platform_driver gpio_enabled_clk_driver = {
+	.probe		= gpio_enabled_clk_driver_probe,
+	.driver		= {
+		.name	= "gpio-enabled-clk",
+		.of_match_table = gpio_enabled_clk_match_table,
+	},
+};
+builtin_platform_driver(gpio_enabled_clk_driver);

module_platform_driver

Signature · 1 line
Jerome
#8 lore raw

Re: [PATCH v9 2/2] clk: Add gpio-enabled clock driver

Quoted 9 lines · Jerome Brunet

This comment has been carried since v5, where the reasoning was already
answered: returning an error from .prepare()/.enable() when the clock isn't running is simply how a provider reports failure, not a workaround for the framework. The claim is also wrong: the framework does call .is_prepared() (clk_unprepare_unused_subtree(), clk_hw_is_prepared()). The rest describes return conventions. Something like this would be enough:

	/* The clock cannot be turned on, only waited for. */

Perhaps the claim is not clear enough then, the .is_prepared() is not called _before_ prepare. I didn't mean it's not called at all.

Quoted 24 lines

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 324

Quoted 7 lines
drivers/clk/clk-gpio.c +7 -0
+static int clk_gpio_enabled_enable(struct clk_hw *hw)
+{
+	struct clk_gpio_enabled *clk = to_clk_gpio_enabled(hw);
+	int value = 0;
+
+	readx_poll_timeout_atomic(gpiod_get_value, clk->gpiod, value,
+			value != 0, 10, clk->timeout_us);

Same error handling as in the prepare variant above.

Also looking at it more closely, the binding allow 1ms max. It is lot of time holding a spinlock without IRQ. If you don't stricly need this variant please drop it for now.

Quoted 9 lines
drivers/clk/clk-gpio.c +9 -0
+
+	device_property_read_u32(dev, "poll-timeout-us", &timeout_us);
+	if (timeout_us < GPIO_ENABLED_TIMEOUT_MIN ||
+	    timeout_us > GPIO_ENABLED_TIMEOUT_MAX)
+		return dev_err_probe(dev, -EINVAL,
+				     "poll-timeout-us is out of range [%u, %u]\n",
+				     GPIO_ENABLED_TIMEOUT_MIN,
+				     GPIO_ENABLED_TIMEOUT_MAX);
+	clk->timeout_us = timeout_us;
Quoted 8 lines

drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 377

Quoted 5 lines
drivers/clk/clk-gpio.c +5 -0
+
+	if (gpiod_cansleep(clk->gpiod))
+		ops = &clk_gpio_enabled_sleeping_ops;
+	else
+		ops = &clk_gpio_enabled_ops;

As I noted above, I prefer if you dropped the enabled variant that run with irq off, unless you scrictly need it. Return -ENOTSUPP if the gpio can't sleep.

I would rather keep only 1 variant in general, because my GPIO controller can't sleep, but gpiod_get_value_cansleep works for both type of controllers.

Slava

#9 lore raw

Re: [PATCH v9 2/2] clk: Add gpio-enabled clock driver

In reply to Jerome Brunet
Quoted 3 lines · Jerome Brunet
Quoted 1 line

+builtin_platform_driver(gpio_enabled_clk_driver);

module_platform_driver

Any explanation on that? All other drivers in this file use builtin_platform_driver. If you'd like to change that, perhaps it should be changed for other clocks as well?

Slava