Skip to contentSkip to conversation

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

RFC PATCH Superseded Mehdi Djait 3 participants 5 messages Mar 21, 2025 – Apr 16, 2025 Media (V4L/DVB)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 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 in ACPI-based platforms.

For ACPI-based platforms the function will read the "clock-frequency" ACPI _DSD property and register a fixed frequency clock with the frequency indicated in the property.

---
Link for discussion (where this patch was proposed): https://lore.kernel.org/linux-media/[email protected]/

v1 -> v2:
Suggested by Sakari:
- removed clk_name
- removed the IS_ERR() check
- improved the kernel-doc comment and commit msg
Link for v1: https://lore.kernel.org/linux-media/[email protected]

v2 -> v3:
- Added #ifdef CONFIG_COMMON_CLK for the ACPI case

 drivers/media/v4l2-core/v4l2-common.c | 39 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 18 +++++++++++++
 2 files changed, 57 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +39 -0
index 0a2f4f0d0a07..4e30f8b777b7 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,39 @@ 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;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk)
+		return clk;
+
+#ifdef CONFIG_COMMON_CLK
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!id) {
+		id = devm_kasprintf(dev, GFP_KERNEL, "clk-%s", dev_name(dev));
+		if (!id)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	clk_hw = devm_clk_hw_register_fixed_rate(dev, id, NULL, 0, rate);
+	if (IS_ERR(clk_hw))
+		return ERR_CAST(clk_hw);
+
+	return clk_hw->clk;
+#else
+	return ERR_PTR(-ENOENT);
+#endif
+}
+EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get);
include/media/v4l2-common.h +18 -0
index 63ad36f04f72..35b9ac698e8a 100644
@@ -573,6 +573,24 @@ 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"
+ * ACPI _DSD property and register a fixed-clock with the frequency indicated
+ * in the property.
+ *
+ * Return:
+ * * 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)
 {
 	/*
Signature · 1 line
2.48.1
#1 lore raw

Hi Mehdi,

Thank you for the patch.

Quoted 49 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 in ACPI-based platforms.

For ACPI-based platforms the function will read the "clock-frequency" ACPI _DSD property and register a fixed frequency clock with the frequency indicated in the property.

---
Link for discussion (where this patch was proposed): https://lore.kernel.org/linux-media/[email protected]/

v1 -> v2:
Suggested by Sakari:
- removed clk_name
- removed the IS_ERR() check
- improved the kernel-doc comment and commit msg
Link for v1: https://lore.kernel.org/linux-media/[email protected]

v2 -> v3:
- Added #ifdef CONFIG_COMMON_CLK for the ACPI case

 drivers/media/v4l2-core/v4l2-common.c | 39 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 18 +++++++++++++
 2 files changed, 57 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +6 -0
index 0a2f4f0d0a07..4e30f8b777b7 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,39 @@ 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)
+{

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

Quoted 10 lines
drivers/media/v4l2-core/v4l2-common.c +10 -0
+	struct clk_hw *clk_hw;
+	struct clk *clk;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk)
+		return clk;
+
+#ifdef CONFIG_COMMON_CLK

This patch will cause warnings when CONFIG_COMMON_CLK is disabled. Could you use

	if (IS_REACHABLE(CONFIG_COMMON_CLK)) {
		...
	}

instead ? It will also ensure that all code gets compile-tested, even when CONFIG_COMMON_CLK is disabled ?

If you want to minimize implementation, you could write

	if (!IS_REACHABLE(CONFIG_COMMON_CLK))
		return ERR_PTR(-ENOENT);

and keep the code below as-is.

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

Quoted 9 lines
drivers/media/v4l2-core/v4l2-common.c +9 -0
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!id) {
+		id = devm_kasprintf(dev, GFP_KERNEL, "clk-%s", dev_name(dev));

As far as I understand, the name doesn't need to stay valid after devm_clk_hw_register_fixed_rate() returns. You can call kasprintf here, and call kfree after devm_clk_hw_register_fixed_rate(). You could use __free to manage the memory life time:

	const char *clk_id __free(kfree) = NULL;

	if (!id) {
		clk_id = kasprintf(GFP_KERNEL, "clk-%s", dev_name(dev));
		if (!clk_id)
			return ERR_PTR(-ENOMEM);
		id = clk_id;
	}
Quoted 43 lines
drivers/media/v4l2-core/v4l2-common.c +14 -0
+		if (!id)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	clk_hw = devm_clk_hw_register_fixed_rate(dev, id, NULL, 0, rate);
+	if (IS_ERR(clk_hw))
+		return ERR_CAST(clk_hw);
+
+	return clk_hw->clk;
+#else
+	return ERR_PTR(-ENOENT);
+#endif
+}
+EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get);
include/media/v4l2-common.h +18 -0
index 63ad36f04f72..35b9ac698e8a 100644
@@ -573,6 +573,24 @@ 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"
+ * ACPI _DSD property and register a fixed-clock with the frequency indicated
+ * in the property.
+ *
+ * Return:
+ * * 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)
 {
 	/*
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

Hi Laurent,

thank you for the review!

Quoted 5 lines · Laurent Pinchart

Hi Mehdi,

Thank you for the patch.

On Fri, Mar 21, 2025 at 10:38:14AM +0100, Mehdi Djait wrote:

SNIP

Quoted 24 lines

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

Quoted 13 lines
drivers/media/v4l2-core/v4l2-common.c +13 -0
+
+struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
+{
+	struct clk_hw *clk_hw;
+	struct clk *clk;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk)
+		return clk;
+
+#ifdef CONFIG_COMMON_CLK

This patch will cause warnings when CONFIG_COMMON_CLK is disabled. Could you use

	if (IS_REACHABLE(CONFIG_COMMON_CLK)) {
		...
	}

instead ? It will also ensure that all code gets compile-tested, even when CONFIG_COMMON_CLK is disabled ?

Quoted 7 lines

If you want to minimize implementation, you could write

	if (!IS_REACHABLE(CONFIG_COMMON_CLK))
		return ERR_PTR(-ENOENT);

and keep the code below as-is.

this is indeed way better! I will change this in the v3

Quoted 15 lines

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

Quoted 9 lines
drivers/media/v4l2-core/v4l2-common.c +9 -0
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!id) {
+		id = devm_kasprintf(dev, GFP_KERNEL, "clk-%s", dev_name(dev));

As far as I understand, the name doesn't need to stay valid after devm_clk_hw_register_fixed_rate() returns. You can call kasprintf here, and call kfree after devm_clk_hw_register_fixed_rate(). You could use __free to manage the memory life time:

Quoted 9 lines
	const char *clk_id __free(kfree) = NULL;

	if (!id) {
		clk_id = kasprintf(GFP_KERNEL, "clk-%s", dev_name(dev));
		if (!clk_id)
			return ERR_PTR(-ENOMEM);
		id = clk_id;
	}

Ack.

--
Kind Regards
Mehdi Djait

#3 lore raw

Hi Laurent, Mehdi,

Quoted 39 lines · Mehdi Djait

Hi Laurent,

thank you for the review!

Quoted 5 lines · Laurent Pinchart

Hi Mehdi,

Thank you for the patch.

On Fri, Mar 21, 2025 at 10:38:14AM +0100, Mehdi Djait wrote:

SNIP

Quoted 26 lines

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

Quoted 13 lines
drivers/media/v4l2-core/v4l2-common.c +13 -0
+
+struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
+{
+	struct clk_hw *clk_hw;
+	struct clk *clk;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk)
+		return clk;
+
+#ifdef CONFIG_COMMON_CLK

This patch will cause warnings when CONFIG_COMMON_CLK is disabled. Could you use

	if (IS_REACHABLE(CONFIG_COMMON_CLK)) {
		...
	}

instead ? It will also ensure that all code gets compile-tested, even when CONFIG_COMMON_CLK is disabled ?

If you want to minimize implementation, you could write

Quoted 7 lines
Quoted 5 lines
	if (!IS_REACHABLE(CONFIG_COMMON_CLK))
		return ERR_PTR(-ENOENT);

and keep the code below as-is.

this is indeed way better! I will change this in the v3

This would work at the moment if devm_clk_hw_register_fixed_rate() was always available but it evaluates to __clk_hw_register_fixed_rate() that depends on COMMON_CLK.

I think t he best option would be to add a stub for it for !COMMON_CLK case. It may take some time to get that merged and into the media tree. C99 also allows declaring variables midst of a basic block so declaring rate and clkhw later should address the warning.

Quoted 28 lines
Quoted 16 lines

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

Quoted 10 lines
drivers/media/v4l2-core/v4l2-common.c +9 -0
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!id) {
+		id = devm_kasprintf(dev, GFP_KERNEL, "clk-%s", dev_name(dev));

As far as I understand, the name doesn't need to stay valid after devm_clk_hw_register_fixed_rate() returns. You can call kasprintf here, and call kfree after devm_clk_hw_register_fixed_rate(). You could use __free to manage the memory life time:

Quoted 9 lines
	const char *clk_id __free(kfree) = NULL;

	if (!id) {
		clk_id = kasprintf(GFP_KERNEL, "clk-%s", dev_name(dev));
		if (!clk_id)
			return ERR_PTR(-ENOMEM);
		id = clk_id;
	}

Ack.

Signature · 3 lines
Kind regards,

Sakari Ailus
#4 lore raw

Hi Sakari, Laurent,

Quoted 50 lines · Sakari Ailus

Hi Laurent, Mehdi,

Quoted 39 lines · Mehdi Djait

Hi Laurent,

thank you for the review!

Quoted 5 lines · Laurent Pinchart

Hi Mehdi,

Thank you for the patch.

On Fri, Mar 21, 2025 at 10:38:14AM +0100, Mehdi Djait wrote:

SNIP

Quoted 26 lines

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

Quoted 13 lines
drivers/media/v4l2-core/v4l2-common.c +13 -0
+
+struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
+{
+	struct clk_hw *clk_hw;
+	struct clk *clk;
+	u32 rate;
+	int ret;
+
+	clk = devm_clk_get_optional(dev, id);
+	if (clk)
+		return clk;
+
+#ifdef CONFIG_COMMON_CLK

This patch will cause warnings when CONFIG_COMMON_CLK is disabled. Could you use

	if (IS_REACHABLE(CONFIG_COMMON_CLK)) {
		...
	}

instead ? It will also ensure that all code gets compile-tested, even when CONFIG_COMMON_CLK is disabled ?

If you want to minimize implementation, you could write

Quoted 7 lines
Quoted 5 lines
	if (!IS_REACHABLE(CONFIG_COMMON_CLK))
		return ERR_PTR(-ENOENT);

and keep the code below as-is.

this is indeed way better! I will change this in the v3

Quoted 8 lines

This would work at the moment if devm_clk_hw_register_fixed_rate() was always available but it evaluates to __clk_hw_register_fixed_rate() that depends on COMMON_CLK.

I think t he best option would be to add a stub for it for !COMMON_CLK case. It may take some time to get that merged and into the media tree. C99 also allows declaring variables midst of a basic block so declaring rate and clkhw later should address the warning.

I don't fully understand the concern with the availability of the function. It is used by drivers in multiple subsystems and any change to it should take care of the API users.

Don't you think that adding a stub for !COMMON_CLK would make the series take a much longer time to get merged and complicate the situation ?

--
Kind Regards
Mehdi Djait