Skip to contentSkip to conversation

media: v4l2-common: Add a helper for obtaining the clock producer

RFC PATCH Superseded Mehdi Djait 2 participants 6 messages Feb 27, 2025 – Mar 10, 2025 Media (V4L/DVB)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 media: v4l2-common: Add a helper for obtaining the clock producer

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.

---
 drivers/media/v4l2-core/v4l2-common.c | 38 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 19 ++++++++++++++
 2 files changed, 57 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +38 -0
index 0a2f4f0d0a07..308a13536ac3 100644
@@ -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);
include/media/v4l2-common.h +19 -0
index 63ad36f04f72..6446575b2a75 100644
@@ -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)
 {
 	/*
Signature · 1 line
2.48.1
#1 lore raw

Hi everyone,

Quoted 8 lines · 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.

link for relevant discussion I forgot to include:
https://lore.kernel.org/linux-media/[email protected]/

#2 lore raw
In reply to Mehdi Djait

Hi Mehdi,

Quoted 46 lines · 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.

---
 drivers/media/v4l2-core/v4l2-common.c | 38 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 19 ++++++++++++++
 2 files changed, 57 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +16 -0
index 0a2f4f0d0a07..308a13536ac3 100644
@@ -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;
+

drivers/media/v4l2-core/v4l2-common.c int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 664

Quoted 10 lines
drivers/media/v4l2-core/v4l2-common.c +10 -0
+	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.

Quoted 21 lines
drivers/media/v4l2-core/v4l2-common.c +12 -0
+		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);
include/media/v4l2-common.h +1 -0
index 63ad36f04f72..6446575b2a75 100644
@@ -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);
 
+/**

include/media/v4l2-common.h int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 586

Quoted 10 lines
include/media/v4l2-common.h +10 -0
+ * 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/

include/media/v4l2-common.h int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 591

Quoted 5 lines
include/media/v4l2-common.h +5 -0
+ * 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.

Quoted 6 lines
include/media/v4l2-common.h +3 -0
+ */
+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)
 {
 	/*
Signature · 3 lines
Kind regards,

Sakari Ailus
#3 lore raw
Quoted 4 lines · Sakari Ailus

drivers/media/v4l2-core/v4l2-common.c int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 652

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+	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.

Quoted 4 lines

drivers/media/v4l2-core/v4l2-common.c int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 656

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+		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.

Signature · 1 line
Sakari Ailus
#4 lore raw

Hi Sakari,

Quoted 8 lines · Sakari Ailus
Quoted 4 lines · Sakari Ailus

drivers/media/v4l2-core/v4l2-common.c int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 652

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+	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

#5 lore raw

Hi Mehdi,

Quoted 12 lines · Mehdi Djait

Hi Sakari,

Quoted 8 lines · Sakari Ailus
Quoted 4 lines · Sakari Ailus

drivers/media/v4l2-core/v4l2-common.c int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs, · line 652

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+	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.

Quoted 3 lines

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.

Signature · 3 lines
Regards,

Sakari Ailus