Re: [PATCH v11 2/2] clk: gpio: Add driver for GPIO-signalled clocks
From: Jerome Brunet
Date: Fri Nov 27 2026 - 10:55:44 EST
> 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_prepared reports the
> current state of the GPIO.
>
> Signed-off-by: Vyacheslav Yurkov <V.Yurkov.EXT@xxxxxxxxxx>
> ---
> 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(+)
>
> diff --git a/drivers/clk/clk-gpio.c b/drivers/clk/clk-gpio.c
> index 918b2c6444ee..ae6ae8993d76 100644
> --- a/drivers/clk/clk-gpio.c
> +++ b/drivers/clk/clk-gpio.c
> @@ -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
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.
> +
> +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);
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)
> + 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,
The range check duplicates the schema bounds;
DT schema validation is the place for it, not the driver
--
Jerome