A proposal to add a gpio-enabled clock driver.
Subsystems (2)
v11 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 v11:
- Address sashiko feedback
- Link to v10: https://lore.kernel.org/r/[email protected]
Changes in v10:
- Use only sleep capable ops
- Make commit messages more generic
- Improve error handling for clock_prepare
- Link to v9: https://lore.kernel.org/r/[email protected]
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: clock: Add gpio-enabled-clock
clk: gpio: Add driver for GPIO-signalled clocks
.../bindings/clock/gpio-enabled-clock.yaml | 55 +++++++++++ drivers/clk/clk-gpio.c | 107 +++++++++++++++++++++ 2 files changed, 162 insertions(+)
---
base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
change-id: 20260318-feature-clock-guard-f20a2c35b965
Best regards,
Signature · 1 line
Vyacheslav Yurkov <[email protected]>
PATCH v11 1/2 dt-bindings: clock: Add gpio-enabled-clock
Some clocks are switched on and off by hardware outside software control, which reports through a GPIO whether the clock is currently running. Software cannot enable such a clock, but its consumers must not use it before the GPIO indicates that it is running.
Add a binding for a clock that passes its parent clock through unchanged and is considered enabled only while a GPIO signal is asserted. The parent clock determines the rate. The status GPIO is given by enabled-gpios, and poll-timeout-us bounds how long a consumer waits for the clock to become available.
---
Our use case: peripheral clocks generated by FPGA PLLs, with the PLL-lock signals routed to GPIOs of the SoC.
--- .../bindings/clock/gpio-enabled-clock.yaml | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+)
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +55 -0
@@ -0,0 +1,55 @@ +# 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 + - "#clock-cells" + - clocks + - clock-output-names + - enabled-gpios + +additionalProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + + clock { + compatible = "gpio-enabled-clock"; + #clock-cells = <0>; + clocks = <&pll>; + clock-output-names = "gpio-enabled-clock"; + enabled-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>; + poll-timeout-us = <20>; + };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v11 1/2] dt-bindings: clock: Add gpio-enabled-clock
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] dt-bindings: clock: poll-timeout-us encodes software policy and bounds - [Low] dt-bindings: clock…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] dt-bindings: clock: poll-timeout-us encodes software policy and bounds
- [Low] dt-bindings: clock: clock-output-names required for single-output clock
- [Low] dt-bindings: clock: inconsistent property order in gpio-enabled-clock
--
commit 4240011b2e1aa7ee82ec94b5068bdebeb6cd0095
Author: Vyacheslav Yurkov <[email protected]>
dt-bindings: clock: Add gpio-enabled-clock
This commit adds a Devicetree binding schema for a clock that passes its parent rate through unchanged and reports its enabled state via a GPIO.
Quoted 6 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +0
-0@@ -0,0 +1,55 @@
[ ... ]
Quoted 12 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +12
-0+properties: + compatible: + const: gpio-enabled-clock + + clocks: + maxItems: 1 + + "#clock-cells": + const: 0 + + clock-output-names: + maxItems: 1
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml line 33
Quoted 10 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +10
-0+ + 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
[Severity: Medium]
Does poll-timeout-us encode OS driver polling policy rather than a hardware property? How long software polls a GPIO (as opposed to waiting on an interrupt or describing a hardware stabilization time) is a driver detail, and the 10 to 1000 us bounds prevent clock sources with lock times outside that window from passing schema validation.
Also, requiring a parent clocks phandle on a separate pass-through node forces a single clock generator with a status or lock GPIO to be split across two Devicetree nodes.
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml line 40
Quoted 7 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +7
-0+ +required: + - compatible + - "#clock-cells" + - clocks + - clock-output-names + - enabled-gpios
[Severity: Low]
Should clock-output-names be optional here? Because #clock-cells is 0, this node provides a single clock output that consumers reference directly by phandle, and sibling single-output clock bindings leave clock-output-names optional so the clock name can default to the node name.
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml line 51
Quoted 11 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +11
-0+ +additionalProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + + clock { + compatible = "gpio-enabled-clock"; + #clock-cells = <0>; + clocks = <&pll>;
[Severity: Low]
This isn't a bug, but properties lists clocks before #clock-cells, while required and the example node list #clock-cells before clocks. Keeping clocks before #clock-cells across all three sections matches canonical property ordering and sibling bindings such as gpio-gate-clock.yaml.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v11 1/2] dt-bindings: clock: Add gpio-enabled-clock
In reply to Vyacheslav YurkovQuoted 48 lines
Some clocks are switched on and off by hardware outside software control, which reports through a GPIO whether the clock is currently running. Software cannot enable such a clock, but its consumers must not use it before the GPIO indicates that it is running.
Add a binding for a clock that passes its parent clock through unchanged and is considered enabled only while a GPIO signal is asserted. The parent clock determines the rate. The status GPIO is given by enabled-gpios, and poll-timeout-us bounds how long a consumer waits for the clock to become available.
Signed-off-by Vyacheslav Yurkov <[email protected]>---
Our use case: peripheral clocks generated by FPGA PLLs, with the PLL-lock signals routed to GPIOs of the SoC.--- .../bindings/clock/gpio-enabled-clock.yaml | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+)Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +23
-0@@ -0,0 +1,55 @@ +# 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
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml line 33
Quoted 10 lines
Documentation/devicetree/bindings/clock/gpio-enabled-clock.yaml new file +10
-0+ + 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
Those limits are made up. I'm not sure they make sense for a generic device. It surely need a value, i'd prefer if it was required then.
I'll wait for the DT folks view on this before applying it
Signature · 1 line
Jerome
PATCH v11 2/2 clk: gpio: Add driver for GPIO-signalled clocks
In reply to Vyacheslav YurkovSome clocks are not under CPU control: they are generated and enabled by external hardware, which only reports through a GPIO whether the clock is running. There is currently no way to describe such a clock, so consumers either initialize too early or rely on ad-hoc delays.
Add a "gpio-enabled-clock" provider for this case. Its prepare callback polls the GPIO until it is asserted or a timeout expires, so a consumer's clk_prepare_enable() succeeds only once the clock is actually running, and fails otherwise. is_prepared reports the current state of the GPIO.
---
Our use case: peripheral clocks generated by FPGA PLLs, with the PLL-lock signals routed to GPIOs of the SoC.
--- drivers/clk/clk-gpio.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+)
drivers/clk/clk-gpio.c +107 -0
@@ -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,112 @@ static struct platform_driver gpio_clk_driver = { };builtin_platform_driver (gpio_clk_driver); +/** +* 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 whose status is determined by a GPIO signal. +* +* Implements .prepare and .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_is_prepared (struct clk_hw *hw) +{ +struct clk_gpio_enabled *clk =to_clk_gpio_enabled (hw); + +return gpiod_get_value_cansleep (clk->gpiod) >0 ; +} + +/* The clock cannot be turned on, only waited for. */ +static int clk_gpio_enabled_prepare (struct clk_hw *hw) +{ +struct clk_gpio_enabled *clk =to_clk_gpio_enabled (hw); +int value, ret; + + ret =readx_poll_timeout (gpiod_get_value_cansleep, clk->gpiod, value, + value !=0 ,10 , clk->timeout_us); +if (ret) +return ret; + +return value <0 ? value :0 ; +} + +static const struct clk_ops clk_gpio_enabled_ops = { + .prepare = clk_gpio_enabled_prepare, + .is_prepared = clk_gpio_enabled_is_prepared, +}; + +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_parent_data gpio_parent_data[] = { + { .index =0 }, + }; +u32 timeout_us =GPIO_ENABLED_TIMEOUT_DEFAULT ; +int ret; + + 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) +return dev_err_probe (dev, ret,"missing clock-output-names\n" ); + +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; + + clk->hw.init =CLK_HW_INIT_PARENTS_DATA (clk_name, gpio_parent_data, + &clk_gpio_enabled_ops,CLK_SET_RATE_PARENT ); + + 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
Re: [PATCH v11 2/2] clk: gpio: Add driver for GPIO-signalled clocks
Quoted 46 lines
Some clocks are not under CPU control: they are generated and enabled by external hardware, which only reports through a GPIO whether the clock is running. There is currently no way to describe such a clock, so consumers either initialize too early or rely on ad-hoc delays.
Add a "gpio-enabled-clock" provider for this case. Its prepare callback polls the GPIO until it is asserted or a timeout expires, so a consumer's
clk_prepare_enable()succeeds only once the clock is actually running, and fails otherwise.is_preparedreports the current state of the GPIO.Signed-off-by Vyacheslav Yurkov <[email protected]>---
Our use case: peripheral clocks generated by FPGA PLLs, with the PLL-lock signals routed to GPIOs of the SoC.--- drivers/clk/clk-gpio.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+)drivers/clk/clk-gpio.c +12
-0@@ -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,112 @@ static struct platform_driver gpio_clk_driver = { };builtin_platform_driver (gpio_clk_driver); +/** +* 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 whose status is determined by a GPIO signal. +* +* Implements .prepare and .is_prepared +*/
drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 265
Quoted 10 lines
drivers/clk/clk-gpio.c +10
-0+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
Those are definitely made up limits carried over from the bindings
I don't think the driver should copy this over. That's one thing that will eventually get mis-aligned. The driver should just be concerned about what it can or cannot do.
Quoted 29 lines
drivers/clk/clk-gpio.c +29
-0+ +static int clk_gpio_enabled_is_prepared (struct clk_hw *hw) +{ +struct clk_gpio_enabled *clk =to_clk_gpio_enabled (hw); + +return gpiod_get_value_cansleep (clk->gpiod) >0 ; +} + +/* The clock cannot be turned on, only waited for. */ +static int clk_gpio_enabled_prepare (struct clk_hw *hw) +{ +struct clk_gpio_enabled *clk =to_clk_gpio_enabled (hw); +int value, ret; + + ret =readx_poll_timeout (gpiod_get_value_cansleep, clk->gpiod, value, + value !=0 ,10 , clk->timeout_us); +if (ret) +return ret; + +return value <0 ? value :0 ; +} + +static const struct clk_ops clk_gpio_enabled_ops = { + .prepare = clk_gpio_enabled_prepare, + .is_prepared = clk_gpio_enabled_is_prepared, +}; + +static int gpio_enabled_clk_driver_probe (struct platform_device *pdev) +{
drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 304
Quoted 10 lines
drivers/clk/clk-gpio.c +10
-0+struct device *dev = &pdev->dev; +struct clk_gpio_enabled *clk; +const char *clk_name; +const struct clk_parent_data gpio_parent_data[] = { + { .index =0 }, + }; +u32 timeout_us =GPIO_ENABLED_TIMEOUT_DEFAULT ; +int ret; + + clk =devm_kzalloc (dev,sizeof (*clk),GFP_KERNEL );
in v7.4-rc2, there was a rework to devm_kzalloc_obj() please rebase on it when you repost
(but let's wait get a review from the DT guys on the bindings first)
Quoted 6 lines
drivers/clk/clk-gpio.c +6
-0+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),
drivers/clk/clk-gpio.c static struct platform_driver gpio_clk_driver = { · line 320
Quoted 10 lines
drivers/clk/clk-gpio.c +10
-0+ "Can't get enabled GPIO property\n"); + + ret =device_property_read_string (dev,"clock-output-names" , &clk_name); +if (ret) +return dev_err_probe (dev, ret,"missing clock-output-names\n" ); + +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,
The range check duplicates the schema bounds; DT schema validation is the place for it, not the driver
Signature · 1 line
Jerome