Skip to contentSkip to conversation

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

RFC PATCH Superseded Mehdi Djait 3 participants 7 messages Mar 10, 2025 – May 14, 2025 Media (V4L/DVB)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 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 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]

 drivers/media/v4l2-core/v4l2-common.c | 35 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 18 ++++++++++++++
 2 files changed, 53 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +35 -0
index 0a2f4f0d0a07..99d826acb213 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,35 @@ 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;
+
+	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;
+}
+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
kernel test robot noticed the following build errors:

Hi Mehdi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on sailus-media-tree/master v6.14-rc6 next-20250311] [cannot apply to sailus-media-tree/streams]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mehdi-Djait/media-v4l2-common-Add-a-helper-for-obtaining-the-clock-producer/20250310-202543
base:   linus/master
patch link:    https://lore.kernel.org/r/20250310122305.209534-1-mehdi.djait%40linux.intel.com
patch subject: [RFC PATCH v2] media: v4l2-common: Add a helper for obtaining the clock producer
config: i386-randconfig-054-20250311 (https://download.01.org/0day-ci/archive/20250311/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250311/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: drivers/media/v4l2-core/v4l2-common.o: in function `devm_v4l2_sensor_clk_get':

>> drivers/media/v4l2-core/v4l2-common.c:667: undefined reference to `__clk_hw_register_fixed_rate'

vim +667 drivers/media/v4l2-core/v4l2-common.c

642
643 struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
644 {
645 struct clk_hw *clk_hw;
646 struct clk *clk;
647 u32 rate;
648 int ret;
649
650 clk = devm_clk_get_optional(dev, id);
651 if (clk)
652 return clk;
653
654 if (!is_acpi_node(dev_fwnode(dev)))
655 return ERR_PTR(-ENOENT);
656
657 ret = device_property_read_u32(dev, "clock-frequency", &rate);
658 if (ret)
659 return ERR_PTR(ret);
660
661 if (!id) {
662 id = devm_kasprintf(dev, GFP_KERNEL, "clk-%s", dev_name(dev));
663 if (!id)
664 return ERR_PTR(-ENOMEM);
665 }
666

 > 667		clk_hw = devm_clk_hw_register_fixed_rate(dev, id, NULL, 0, rate);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Mehdi Djait
kernel test robot noticed the following build errors:

Hi Mehdi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on sailus-media-tree/master v6.14-rc6 next-20250311] [cannot apply to sailus-media-tree/streams]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mehdi-Djait/media-v4l2-common-Add-a-helper-for-obtaining-the-clock-producer/20250310-202543
base:   linus/master
patch link:    https://lore.kernel.org/r/20250310122305.209534-1-mehdi.djait%40linux.intel.com
patch subject: [RFC PATCH v2] media: v4l2-common: Add a helper for obtaining the clock producer
config: i386-randconfig-053-20250311 (https://download.01.org/0day-ci/archive/20250312/[email protected]/config)
compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250312/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: __clk_hw_register_fixed_rate
   >>> referenced by v4l2-common.c:667 (drivers/media/v4l2-core/v4l2-common.c:667)
   >>>               drivers/media/v4l2-core/v4l2-common.o:(devm_v4l2_sensor_clk_get) in archive vmlinux.a
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Mehdi Djait

Hi Mehdi,

Quoted 59 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 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]

 drivers/media/v4l2-core/v4l2-common.c | 35 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 18 ++++++++++++++
 2 files changed, 53 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +19 -0
index 0a2f4f0d0a07..99d826acb213 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,35 @@ 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;
+
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);

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

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

devm_clk_hw_register_fixed_rate() is only available when COMMON_CLK is enabled. You need #ifdefs here. In practice without CCF only devm_clk_get_optional() is useful I guess.

Another question is then how commonly COMMON_CLK is enabled e.g. on x86 systems. At least Debian kernel has it. Presumably it's common elsewhere, too.

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

Sakari Ailus
#4 lore raw

Hi Sakari,

Quoted 14 lines · Sakari Ailus

Hi Mehdi,

Quoted 11 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 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.

SNIP

Quoted 21 lines
Quoted 21 lines
drivers/media/v4l2-core/v4l2-common.c +21 -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;
+
+	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)
Quoted 9 lines

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

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+			return ERR_PTR(-ENOMEM);
+	}
+
+	clk_hw = devm_clk_hw_register_fixed_rate(dev, id, NULL, 0, rate);

devm_clk_hw_register_fixed_rate() is only available when COMMON_CLK is enabled. You need #ifdefs here. In practice without CCF only devm_clk_get_optional() is useful I guess.

I added a call to IS_REACHABLE(CONFIG_COMMON_CLK) in the v4 of this patch:
https://lore.kernel.org/linux-media/[email protected]/

Quoted 3 lines

Another question is then how commonly COMMON_CLK is enabled e.g. on x86 systems. At least Debian kernel has it. Presumably it's common elsewhere, too.

on Arch linux it is also enabled and Fedora also. I would also assume it is also enabled in the other linux distros.

--
Kind Regards
Mehdi Djait

#5 lore raw

Hi Mehdi,

Quoted 51 lines · Mehdi Djait

Hi Sakari,

Quoted 14 lines · Sakari Ailus

Hi Mehdi,

Quoted 11 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 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.

SNIP

Quoted 21 lines
Quoted 21 lines
drivers/media/v4l2-core/v4l2-common.c +21 -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;
+
+	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)
Quoted 9 lines

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

Quoted 4 lines
drivers/media/v4l2-core/v4l2-common.c +4 -0
+			return ERR_PTR(-ENOMEM);
+	}
+
+	clk_hw = devm_clk_hw_register_fixed_rate(dev, id, NULL, 0, rate);

devm_clk_hw_register_fixed_rate() is only available when COMMON_CLK is enabled. You need #ifdefs here. In practice without CCF only devm_clk_get_optional() is useful I guess.

Quoted 2 lines

I added a call to IS_REACHABLE(CONFIG_COMMON_CLK) in the v4 of this patch:
https://lore.kernel.org/linux-media/[email protected]/

I wonder if this approach works. Depending on the compiler implementation, the compiler could (or even should) still run into issues in finding an unresolvable symbol, even if the symbol is not reachable and can be optimised away.

Quoted 7 lines
Quoted 4 lines

Another question is then how commonly COMMON_CLK is enabled e.g. on x86 systems. At least Debian kernel has it. Presumably it's common elsewhere, too.

on Arch linux it is also enabled and Fedora also. I would also assume it is also enabled in the other linux distros.

Ack, thanks for checking.

Signature · 3 lines
Regards,

Sakari Ailus
#6 lore raw

Hi Sakari,

Quoted 54 lines · Sakari Ailus

Hi Mehdi,

Quoted 51 lines · Mehdi Djait

Hi Sakari,

Quoted 14 lines · Sakari Ailus

Hi Mehdi,

Quoted 11 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 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.

SNIP

Quoted 30 lines
Quoted 15 lines
drivers/media/v4l2-core/v4l2-common.c +15 -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;
+
+	if (!is_acpi_node(dev_fwnode(dev)))
+		return ERR_PTR(-ENOENT);
+
+	ret = device_property_read_u32(dev, "clock-frequency", &rate);

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

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

devm_clk_hw_register_fixed_rate() is only available when COMMON_CLK is enabled. You need #ifdefs here. In practice without CCF only devm_clk_get_optional() is useful I guess.

Quoted 8 lines
Quoted 2 lines

I added a call to IS_REACHABLE(CONFIG_COMMON_CLK) in the v4 of this patch:
https://lore.kernel.org/linux-media/[email protected]/

I wonder if this approach works. Depending on the compiler implementation, the compiler could (or even should) still run into issues in finding an unresolvable symbol, even if the symbol is not reachable and can be optimised away.

So I discussed with Arnd about this (He introduced IS_REACHABLE()):

- IS_REACHABLE() is actually discouraged [1]

- COFIG_COMMON_CLK is a bool, so IS_ENABLED() will be the right solution here

- usage of IS_ENABLED() is also encouraged in the coding style [2]

- we will not face compiler issues because the kernel relies on dead code elimination in the compiler. (Actually I remember this is one of the reasons why you cannot compile the kernel with optimization turned off. I don't know how much this argument holds today [3]

[1] https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/Documentation/kbuild/kconfig-language.rst?h=next-20250513&id=700bd25bd4f47a0f4e02e0a25dde05f1a6b16eea
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst#n1178
[3] https://lore.kernel.org/all/[email protected]/

Quoted 14 lines
Quoted 7 lines
Quoted 4 lines

Another question is then how commonly COMMON_CLK is enabled e.g. on x86 systems. At least Debian kernel has it. Presumably it's common elsewhere, too.

on Arch linux it is also enabled and Fedora also. I would also assume it is also enabled in the other linux distros.

Ack, thanks for checking.

Signature · 3 lines
Regards,

Sakari Ailus

--
Kind Regards
Mehdi Djait