mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
@ 2025-02-27  9:26 Mehdi Djait
  2025-02-27  9:35 ` Mehdi Djait
  2025-03-07 11:51 ` Sakari Ailus
  0 siblings, 2 replies; 6+ messages in thread
From: Mehdi Djait @ 2025-02-27  9:26 UTC (permalink / raw)
  To: sakari.ailus, laurent.pinchart
  Cc: tomi.valkeinen, jacopo.mondi, hverkuil, kieran.bingham, naush,
	mchehab, hdegoede, dave.stevenson, linux-media, linux-kernel,
	Mehdi Djait

Introduce a helper for v4l2 sensor drivers on both DT- and ACPI-based
platforms to retrieve a reference to the clock producer from firmware.

This helper behaves the same as clk_get_optional() except where there is
no clock producer like typically in ACPI-based platforms.
For ACPI-based platforms the function will read the "clock-frequency"
property (_DSD ACPI property) and register a fixed frequency clock with
the frequency indicated in the property.

Signed-off-by: Mehdi Djait <mehdi.djait@linux.intel.com>
---
 drivers/media/v4l2-core/v4l2-common.c | 38 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 19 ++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index 0a2f4f0d0a07..308a13536ac3 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -34,6 +34,9 @@
  * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
  */
 
+#include <linux/clk.h>
+#include <linux/clkdev.h>
+#include <linux/clk-provider.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
@@ -636,3 +639,38 @@ int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
 	return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_link_freq_to_bitmap);
+
+struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
+{
+	struct clk_hw *clk_hw;
+	struct clk *clk;
+	char *clk_name;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk || IS_ERR(clk))
+		return clk;
+
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-EINVAL);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!id) {
+		clk_name = devm_kasprintf(dev, GFP_KERNEL, "clk-%s",
+					  dev_name(dev));
+		if (!clk_name)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	clk_hw = devm_clk_hw_register_fixed_rate(dev, id ? id : clk_name, NULL,
+						 0, rate);
+	if (IS_ERR(clk_hw))
+		return ERR_CAST(clk_hw);
+
+	return clk_hw->clk;
+}
+EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get);
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 63ad36f04f72..6446575b2a75 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -573,6 +573,25 @@ int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
 			     unsigned int num_of_driver_link_freqs,
 			     unsigned long *bitmap);
 
+/**
+ * devm_v4l2_sensor_clk_get - lookup and obtain a reference to an optional clock
+ *			      producer for a camera sensor.
+ *
+ * @dev: device for v4l2 sensor clock "consumer"
+ * @id: clock consumer ID
+ *
+ * This function behaves the same way as clk_get_optional() except where there
+ * is no clock producer like in ACPI-based platforms.
+ * For ACPI-based platforms, the function will read the "clock-frequency"
+ * property (_DSD ACPI property) and register a fixed-clock with the frequency
+ * indicated in the property.
+ *
+ * Return:
+ * * %pointer to a struct clk: Success
+ * * %valid IS_ERR() condition containing errno: Failure
+ */
+struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id);
+
 static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
 {
 	/*
-- 
2.48.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
  2025-02-27  9:26 [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer Mehdi Djait
@ 2025-02-27  9:35 ` Mehdi Djait
  2025-03-07 11:51 ` Sakari Ailus
  1 sibling, 0 replies; 6+ messages in thread
From: Mehdi Djait @ 2025-02-27  9:35 UTC (permalink / raw)
  To: sakari.ailus, laurent.pinchart
  Cc: tomi.valkeinen, jacopo.mondi, hverkuil, kieran.bingham, naush,
	mchehab, hdegoede, dave.stevenson, linux-media, linux-kernel

Hi everyone,

On Thu, Feb 27, 2025 at 10:26:43AM +0100, Mehdi Djait wrote:
> Introduce a helper for v4l2 sensor drivers on both DT- and ACPI-based
> platforms to retrieve a reference to the clock producer from firmware.
> 
> This helper behaves the same as clk_get_optional() except where there is
> no clock producer like typically in ACPI-based platforms.
> For ACPI-based platforms the function will read the "clock-frequency"
> property (_DSD ACPI property) and register a fixed frequency clock with
> the frequency indicated in the property.

link for relevant discussion I forgot to include:
https://lore.kernel.org/linux-media/20250220154909.152538-1-mehdi.djait@linux.intel.com/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
  2025-02-27  9:26 [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer Mehdi Djait
  2025-02-27  9:35 ` Mehdi Djait
@ 2025-03-07 11:51 ` Sakari Ailus
  2025-03-07 11:57   ` Sakari Ailus
  1 sibling, 1 reply; 6+ messages in thread
From: Sakari Ailus @ 2025-03-07 11:51 UTC (permalink / raw)
  To: Mehdi Djait
  Cc: laurent.pinchart, tomi.valkeinen, jacopo.mondi, hverkuil,
	kieran.bingham, naush, mchehab, hdegoede, dave.stevenson,
	linux-media, linux-kernel

Hi Mehdi,

On Thu, Feb 27, 2025 at 10:26:43AM +0100, Mehdi Djait wrote:
> Introduce a helper for v4l2 sensor drivers on both DT- and ACPI-based
> platforms to retrieve a reference to the clock producer from firmware.
> 
> This helper behaves the same as clk_get_optional() except where there is
> no clock producer like typically in ACPI-based platforms.
> For ACPI-based platforms the function will read the "clock-frequency"
> property (_DSD ACPI property) and register a fixed frequency clock with
> the frequency indicated in the property.
> 
> Signed-off-by: Mehdi Djait <mehdi.djait@linux.intel.com>
> ---
>  drivers/media/v4l2-core/v4l2-common.c | 38 +++++++++++++++++++++++++++
>  include/media/v4l2-common.h           | 19 ++++++++++++++
>  2 files changed, 57 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> index 0a2f4f0d0a07..308a13536ac3 100644
> --- a/drivers/media/v4l2-core/v4l2-common.c
> +++ b/drivers/media/v4l2-core/v4l2-common.c
> @@ -34,6 +34,9 @@
>   * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
>   */
>  
> +#include <linux/clk.h>
> +#include <linux/clkdev.h>
> +#include <linux/clk-provider.h>
>  #include <linux/module.h>
>  #include <linux/types.h>
>  #include <linux/kernel.h>
> @@ -636,3 +639,38 @@ int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(v4l2_link_freq_to_bitmap);
> +
> +struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
> +{
> +	struct clk_hw *clk_hw;
> +	struct clk *clk;
> +	char *clk_name;
> +	u32 rate;
> +	int ret;
> +
> +	clk = devm_clk_get_optional(dev, id);
> +	if (clk || IS_ERR(clk))
> +		return clk;
> +
> +	if (!is_acpi_node(dev_fwnode(dev)))
> +		return ERR_PTR(-EINVAL);
> +
> +	ret = device_property_read_u32(dev, "clock-frequency", &rate);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	if (!id) {
> +		clk_name = devm_kasprintf(dev, GFP_KERNEL, "clk-%s",
> +					  dev_name(dev));

You could just assign to "id" and omit clk_name altogether.

> +		if (!clk_name)
> +			return ERR_PTR(-ENOMEM);
> +	}
> +
> +	clk_hw = devm_clk_hw_register_fixed_rate(dev, id ? id : clk_name, NULL,
> +						 0, rate);
> +	if (IS_ERR(clk_hw))
> +		return ERR_CAST(clk_hw);
> +
> +	return clk_hw->clk;
> +}
> +EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get);
> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> index 63ad36f04f72..6446575b2a75 100644
> --- a/include/media/v4l2-common.h
> +++ b/include/media/v4l2-common.h
> @@ -573,6 +573,25 @@ int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
>  			     unsigned int num_of_driver_link_freqs,
>  			     unsigned long *bitmap);
>  
> +/**
> + * devm_v4l2_sensor_clk_get - lookup and obtain a reference to an optional clock
> + *			      producer for a camera sensor.
> + *
> + * @dev: device for v4l2 sensor clock "consumer"
> + * @id: clock consumer ID
> + *
> + * This function behaves the same way as clk_get_optional() except where there
> + * is no clock producer like in ACPI-based platforms.
> + * For ACPI-based platforms, the function will read the "clock-frequency"
> + * property (_DSD ACPI property) and register a fixed-clock with the frequency

s/property (_DSD ACPI property)/ACPI _DSD property/

> + * indicated in the property.
> + *
> + * Return:
> + * * %pointer to a struct clk: Success
> + * * %valid IS_ERR() condition containing errno: Failure

% should precede a value, but neither is describing a value here. How
about:

Return:
A pointer to a struct clk on success or an error code on failure.

> + */
> +struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id);
> +
>  static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
>  {
>  	/*

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
  2025-03-07 11:51 ` Sakari Ailus
@ 2025-03-07 11:57   ` Sakari Ailus
  2025-03-10 12:11     ` Mehdi Djait
  0 siblings, 1 reply; 6+ messages in thread
From: Sakari Ailus @ 2025-03-07 11:57 UTC (permalink / raw)
  To: Mehdi Djait
  Cc: laurent.pinchart, tomi.valkeinen, jacopo.mondi, hverkuil,
	kieran.bingham, naush, mchehab, hdegoede, dave.stevenson,
	linux-media, linux-kernel

On Fri, Mar 07, 2025 at 11:51:58AM +0000, Sakari Ailus wrote:
> > +	int ret;
> > +
> > +	clk = devm_clk_get_optional(dev, id);
> > +	if (clk || IS_ERR(clk))

I forgot this bit earlier. The IS_ERR() check is redundant.

> > +		return clk;
> > +
> > +	if (!is_acpi_node(dev_fwnode(dev)))
> > +		return ERR_PTR(-EINVAL);

How about -ENOENT? That's what devm_clk_get() returns when there's no such
clock.

-- 
Sakari Ailus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
  2025-03-07 11:57   ` Sakari Ailus
@ 2025-03-10 12:11     ` Mehdi Djait
  2025-03-10 12:56       ` Sakari Ailus
  0 siblings, 1 reply; 6+ messages in thread
From: Mehdi Djait @ 2025-03-10 12:11 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: laurent.pinchart, tomi.valkeinen, jacopo.mondi, hverkuil,
	kieran.bingham, naush, mchehab, hdegoede, dave.stevenson,
	linux-media, linux-kernel

Hi Sakari,

On Fri, Mar 07, 2025 at 11:57:01AM +0000, Sakari Ailus wrote:
> On Fri, Mar 07, 2025 at 11:51:58AM +0000, Sakari Ailus wrote:
> > > +	int ret;
> > > +
> > > +	clk = devm_clk_get_optional(dev, id);
> > > +	if (clk || IS_ERR(clk))
> 
> I forgot this bit earlier. The IS_ERR() check is redundant.
> 

Wouldn't you say that this helps with readability even if it is
redundant ? This will warn the users of this helper that the call
may fail and return early ?

--
Kind Regards
Mehdi Djait

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer
  2025-03-10 12:11     ` Mehdi Djait
@ 2025-03-10 12:56       ` Sakari Ailus
  0 siblings, 0 replies; 6+ messages in thread
From: Sakari Ailus @ 2025-03-10 12:56 UTC (permalink / raw)
  To: Mehdi Djait
  Cc: laurent.pinchart, tomi.valkeinen, jacopo.mondi, hverkuil,
	kieran.bingham, naush, mchehab, hdegoede, dave.stevenson,
	linux-media, linux-kernel

Hi Mehdi,

On Mon, Mar 10, 2025 at 01:11:33PM +0100, Mehdi Djait wrote:
> Hi Sakari,
> 
> On Fri, Mar 07, 2025 at 11:57:01AM +0000, Sakari Ailus wrote:
> > On Fri, Mar 07, 2025 at 11:51:58AM +0000, Sakari Ailus wrote:
> > > > +	int ret;
> > > > +
> > > > +	clk = devm_clk_get_optional(dev, id);
> > > > +	if (clk || IS_ERR(clk))
> > 
> > I forgot this bit earlier. The IS_ERR() check is redundant.
> > 
> 
> Wouldn't you say that this helps with readability even if it is
> redundant ? This will warn the users of this helper that the call
> may fail and return early ?

If you want to note that the returned clk may be valid or an error, I'd add
a comment for it.

-- 
Regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-10 12:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-27  9:26 [RFC PATCH v1] media: v4l2-common: Add a helper for obtaining the clock producer Mehdi Djait
2025-02-27  9:35 ` Mehdi Djait
2025-03-07 11:51 ` Sakari Ailus
2025-03-07 11:57   ` Sakari Ailus
2025-03-10 12:11     ` Mehdi Djait
2025-03-10 12:56       ` Sakari Ailus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®