diff mbox series

[02/25] clock: Add new clock_has_source() function

Message ID 20210121190622.22000-3-peter.maydell@linaro.org
State New
Headers show
Series Convert CMSDK timer, watchdog, dualtimer to Clock framework | expand

Commit Message

Peter Maydell Jan. 21, 2021, 7:05 p.m. UTC
Add a function for checking whether a clock has a source.  This is
useful for devices which have input clocks that must be wired up by
the board as it allows them to fail in realize rather than ploughing
on with a zero-period clock.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 docs/devel/clocks.rst | 16 ++++++++++++++++
 include/hw/clock.h    | 15 +++++++++++++++
 2 files changed, 31 insertions(+)

Comments

Luc Michel Jan. 22, 2021, 2:15 p.m. UTC | #1
On 19:05 Thu 21 Jan     , Peter Maydell wrote:
> Add a function for checking whether a clock has a source.  This is
> useful for devices which have input clocks that must be wired up by
> the board as it allows them to fail in realize rather than ploughing
> on with a zero-period clock.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Luc Michel <luc@lmichel.fr>

> ---
>  docs/devel/clocks.rst | 16 ++++++++++++++++
>  include/hw/clock.h    | 15 +++++++++++++++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/docs/devel/clocks.rst b/docs/devel/clocks.rst
> index 2548d842322..c54bbb82409 100644
> --- a/docs/devel/clocks.rst
> +++ b/docs/devel/clocks.rst
> @@ -235,6 +235,22 @@ object during device instance init. For example:
>      /* set initial value to 10ns / 100MHz */
>      clock_set_ns(clk, 10);
>  
> +To enforce that the clock is wired up by the board code, you can
> +call ``clock_has_source()`` in your device's realize method:
> +
> +.. code-block:: c
> +
> +   if (!clock_has_source(s->clk)) {
> +       error_setg(errp, "MyDevice: clk input must be connected");
> +       return;
> +   }
> +
> +Note that this only checks that the clock has been wired up; it is
> +still possible that the output clock connected to it is disabled
> +or has not yet been configured, in which case the period will be
> +zero. You should use the clock callback to find out when the clock
> +period changes.
> +
>  Fetching clock frequency/period
>  -------------------------------
>  
> diff --git a/include/hw/clock.h b/include/hw/clock.h
> index 6382f346569..e5f45e2626d 100644
> --- a/include/hw/clock.h
> +++ b/include/hw/clock.h
> @@ -139,6 +139,21 @@ void clock_clear_callback(Clock *clk);
>   */
>  void clock_set_source(Clock *clk, Clock *src);
>  
> +/**
> + * clock_has_source:
> + * @clk: the clock
> + *
> + * Returns true if the clock has a source clock connected to it.
> + * This is useful for devices which have input clocks which must
> + * be connected by the board/SoC code which creates them. The
> + * device code can use this to check in its realize method that
> + * the clock has been connected.
> + */
> +static inline bool clock_has_source(const Clock *clk)
> +{
> +    return clk->source != NULL;
> +}
> +
>  /**
>   * clock_set:
>   * @clk: the clock to initialize.
> -- 
> 2.20.1
> 

--
Philippe Mathieu-Daudé Jan. 27, 2021, 9:56 p.m. UTC | #2
On 1/21/21 8:05 PM, Peter Maydell wrote:
> Add a function for checking whether a clock has a source.  This is
> useful for devices which have input clocks that must be wired up by
> the board as it allows them to fail in realize rather than ploughing
> on with a zero-period clock.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  docs/devel/clocks.rst | 16 ++++++++++++++++
>  include/hw/clock.h    | 15 +++++++++++++++
>  2 files changed, 31 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
diff mbox series

Patch

diff --git a/docs/devel/clocks.rst b/docs/devel/clocks.rst
index 2548d842322..c54bbb82409 100644
--- a/docs/devel/clocks.rst
+++ b/docs/devel/clocks.rst
@@ -235,6 +235,22 @@  object during device instance init. For example:
     /* set initial value to 10ns / 100MHz */
     clock_set_ns(clk, 10);
 
+To enforce that the clock is wired up by the board code, you can
+call ``clock_has_source()`` in your device's realize method:
+
+.. code-block:: c
+
+   if (!clock_has_source(s->clk)) {
+       error_setg(errp, "MyDevice: clk input must be connected");
+       return;
+   }
+
+Note that this only checks that the clock has been wired up; it is
+still possible that the output clock connected to it is disabled
+or has not yet been configured, in which case the period will be
+zero. You should use the clock callback to find out when the clock
+period changes.
+
 Fetching clock frequency/period
 -------------------------------
 
diff --git a/include/hw/clock.h b/include/hw/clock.h
index 6382f346569..e5f45e2626d 100644
--- a/include/hw/clock.h
+++ b/include/hw/clock.h
@@ -139,6 +139,21 @@  void clock_clear_callback(Clock *clk);
  */
 void clock_set_source(Clock *clk, Clock *src);
 
+/**
+ * clock_has_source:
+ * @clk: the clock
+ *
+ * Returns true if the clock has a source clock connected to it.
+ * This is useful for devices which have input clocks which must
+ * be connected by the board/SoC code which creates them. The
+ * device code can use this to check in its realize method that
+ * the clock has been connected.
+ */
+static inline bool clock_has_source(const Clock *clk)
+{
+    return clk->source != NULL;
+}
+
 /**
  * clock_set:
  * @clk: the clock to initialize.