Skip to contentSkip to conversation

Add link frequency to bitmap helper

PATCH Superseded Sakari Ailus 3 participants 11 messages Jan 8, 2024 – Jan 9, 2024

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

v2 cover Add link frequency to bitmap helper

Hi folks,

This set adds a little helper for sensor drivers to match link frequencies from system firmware with those supported the driver and generate a bitmap suitable for passing to the control framework.

Imx319, imx334 and imx355 drivers are converted to use the helper, too.

I've tested imx319 and imx355 drivers.

since v1:

- Call the helper v4l2_link_freq_to_bitmap, it's nicer to use that way and aligns with v4l2_get_link_freq, too.

- Fix kerneldoc issues and compiler warnings.

Sakari Ailus (4):
media: v4l: Add a helper for setting up link-frequencies control
media: imx334: Use v4l2_link_freq_to_bitmap helper
media: imx319: Use v4l2_link_freq_to_bitmap helper
media: imx355: Use v4l2_link_freq_to_bitmap helper

 drivers/media/i2c/imx319.c            | 53 +++++++--------------------
 drivers/media/i2c/imx334.c            | 41 ++++++---------------
 drivers/media/i2c/imx355.c            | 53 +++++++--------------------
 drivers/media/v4l2-core/v4l2-common.c | 47 ++++++++++++++++++++++++
 include/media/v4l2-common.h           | 25 +++++++++++++
 5 files changed, 112 insertions(+), 107 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v2 1/4 media: v4l: Add a helper for setting up link-frequencies control

Add a helper for obtaining supported link frequencies in form most drivers need them. The result is a bitmap of supported controls.

---
 drivers/media/v4l2-core/v4l2-common.c | 47 +++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 25 ++++++++++++++
 2 files changed, 72 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +47 -0
index 273d83de2a87..353e94f52f5d 100644
@@ -585,3 +585,50 @@ u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator)
 	return denominator ? numerator * multiplier / denominator : 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_fraction_to_interval);
+
+int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
+			     unsigned int num_of_fw_link_freqs,
+			     const s64 *driver_link_freqs,
+			     unsigned int num_of_driver_link_freqs,
+			     unsigned long *bitmap)
+{
+	unsigned int i;
+
+	*bitmap = 0;
+
+	if (!num_of_fw_link_freqs) {
+		dev_err(dev, "no link frequencies in firmware\n");
+		return -ENODATA;
+	}
+
+	for (i = 0; i < num_of_fw_link_freqs; i++) {
+		unsigned int j;
+
+		for (j = 0; j < num_of_driver_link_freqs; j++) {
+			if (fw_link_freqs[i] != driver_link_freqs[j])
+				continue;
+
+			dev_dbg(dev, "enabling link frequency %lld Hz\n",
+				driver_link_freqs[j]);
+			*bitmap |= BIT(j);
+			break;
+		}
+	}
+
+	if (!*bitmap) {
+		dev_err(dev, "no matching link frequencies found\n");
+
+		dev_dbg(dev, "specified in firmware:\n");
+		for (i = 0; i < num_of_fw_link_freqs; i++)
+			dev_dbg(dev, "\t%llu Hz\n", fw_link_freqs[i]);
+
+		dev_dbg(dev, "driver supported:\n");
+		for (i = 0; i < num_of_driver_link_freqs; i++)
+			dev_dbg(dev, "\t%lld Hz\n", driver_link_freqs[i]);
+
+		return -ENOENT;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_link_frequencies_to_bitmap);
include/media/v4l2-common.h +25 -0
index acf5be24a5ca..cd2163f24f8a 100644
@@ -547,6 +547,31 @@ void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
 		unsigned int n_terms, unsigned int threshold);
 u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator);
 
+/**
+ * v4l2_link_freq_to_bitmap - Figure out platform-supported link frequencies
+ * @dev: The struct device
+ * @fw_link_freqs: Array of link frequencies from firmware
+ * @num_of_fw_link_freqs: Number of entries in @fw_link_freqs
+ * @driver_link_freqs: Array of link frequencies supported by the driver
+ * @num_of_driver_link_freqs: Number of entries in @driver_link_freqs
+ * @bitmap: Bitmap of driver-supported link frequencies found in @fw_link_freqs
+ *
+ * This function checks which driver-supported link frequencies are enabled in
+ * system firmware and sets the corresponding bits in @bitmap (after first
+ * zeroing it).
+ *
+ * Return values:
+ *	0: Success
+ *	-ENOENT: No match found between driver-supported link frequencies and
+ *		 those available in firmware.
+ *	-ENODATA: No link frequencies were specified in firmware.
+ */
+int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
+			     unsigned int num_of_fw_link_freqs,
+			     const s64 *driver_link_freqs,
+			     unsigned int num_of_driver_link_freqs,
+			     unsigned long *bitmap);
+
 static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
 {
 	/*
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v2 2/4 media: imx334: Use v4l2_link_freq_to_bitmap helper

In reply to Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +12 -29
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 
Signature · 1 line
2.39.2
author maintainer
#9 lore raw

PATCH v2 3/4 media: imx319: Use v4l2_link_freq_to_bitmap helper

In reply to Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link freq can be used on a given system.

---
 drivers/media/i2c/imx319.c | 53 ++++++++++----------------------------
 1 file changed, 14 insertions(+), 39 deletions(-)
drivers/media/i2c/imx319.c +14 -39
index e47eff672e0c..8fe3933f3146 100644
@@ -70,7 +70,7 @@
 #define IMX319_REG_ORIENTATION		0x0101
 
 /* default link frequency and external clock */
-#define IMX319_LINK_FREQ_DEFAULT	482400000
+#define IMX319_LINK_FREQ_DEFAULT	482400000LL
 #define IMX319_EXT_CLK			19200000
 #define IMX319_LINK_FREQ_INDEX		0
 
@@ -107,8 +107,7 @@ struct imx319_mode {
 
 struct imx319_hwcfg {
 	u32 ext_clk;			/* sensor external clk */
-	s64 *link_freqs;		/* CSI-2 link frequencies */
-	unsigned int nr_of_link_freqs;
+	unsigned long link_freq_bitmap;
 };
 
 struct imx319 {
@@ -129,7 +128,6 @@ struct imx319 {
 	const struct imx319_mode *cur_mode;
 
 	struct imx319_hwcfg *hwcfg;
-	s64 link_def_freq;	/* CSI-2 link default frequency */
 
 	/*
 	 * Mutex for serialized access:
@@ -1654,7 +1652,10 @@ static const char * const imx319_test_pattern_menu[] = {
 	"Pseudorandom Sequence (PN9)",
 };
 
-/* supported link frequencies */
+/*
+ * When adding more than the one below, make sure the disallowed ones will
+ * actually be disabled in the LINK_FREQ control.
+ */
 static const s64 link_freq_menu_items[] = {
 	IMX319_LINK_FREQ_DEFAULT,
 };
@@ -2058,7 +2059,7 @@ imx319_set_pad_format(struct v4l2_subdev *sd,
 		*framefmt = fmt->format;
 	} else {
 		imx319->cur_mode = mode;
-		pixel_rate = imx319->link_def_freq * 2 * 4;
+		pixel_rate = IMX319_LINK_FREQ_DEFAULT * 2 * 4;
 		do_div(pixel_rate, 10);
 		__v4l2_ctrl_s_ctrl_int64(imx319->pixel_rate, pixel_rate);
 		/* Update limits and set FPS to default */
@@ -2255,7 +2256,7 @@ static int imx319_init_controls(struct imx319 *imx319)
 		imx319->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
 
 	/* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
-	pixel_rate = imx319->link_def_freq * 2 * 4;
+	pixel_rate = IMX319_LINK_FREQ_DEFAULT * 2 * 4;
 	do_div(pixel_rate, 10);
 	/* By default, PIXEL_RATE is read only */
 	imx319->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx319_ctrl_ops,
@@ -2332,7 +2333,6 @@ static struct imx319_hwcfg *imx319_get_hwcfg(struct device *dev)
 	};
 	struct fwnode_handle *ep;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
-	unsigned int i;
 	int ret;
 
 	if (!fwnode)
@@ -2364,24 +2364,14 @@ static struct imx319_hwcfg *imx319_get_hwcfg(struct device *dev)
 		goto out_err;
 	}
 
-	dev_dbg(dev, "num of link freqs: %d", bus_cfg.nr_of_link_frequencies);
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_warn(dev, "no link frequencies defined");
-		goto out_err;
-	}
-
-	cfg->nr_of_link_freqs = bus_cfg.nr_of_link_frequencies;
-	cfg->link_freqs = devm_kcalloc(dev,
-				       bus_cfg.nr_of_link_frequencies + 1,
-				       sizeof(*cfg->link_freqs), GFP_KERNEL);
-	if (!cfg->link_freqs)
+	ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq_menu_items,
+				       ARRAY_SIZE(link_freq_menu_items),
+				       &cfg->link_freq_bitmap);
+	if (ret)
 		goto out_err;
 
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		cfg->link_freqs[i] = bus_cfg.link_frequencies[i];
-		dev_dbg(dev, "link_freq[%d] = %lld", i, cfg->link_freqs[i]);
-	}
-
 	v4l2_fwnode_endpoint_free(&bus_cfg);
 	fwnode_handle_put(ep);
 	return cfg;
@@ -2397,7 +2387,6 @@ static int imx319_probe(struct i2c_client *client)
 	struct imx319 *imx319;
 	bool full_power;
 	int ret;
-	u32 i;
 
 	imx319 = devm_kzalloc(&client->dev, sizeof(*imx319), GFP_KERNEL);
 	if (!imx319)
@@ -2425,20 +2414,6 @@ static int imx319_probe(struct i2c_client *client)
 		goto error_probe;
 	}
 
-	imx319->link_def_freq = link_freq_menu_items[IMX319_LINK_FREQ_INDEX];
-	for (i = 0; i < imx319->hwcfg->nr_of_link_freqs; i++) {
-		if (imx319->hwcfg->link_freqs[i] == imx319->link_def_freq) {
-			dev_dbg(&client->dev, "link freq index %d matched", i);
-			break;
-		}
-	}
-
-	if (i == imx319->hwcfg->nr_of_link_freqs) {
-		dev_err(&client->dev, "no link frequency supported");
-		ret = -EINVAL;
-		goto error_probe;
-	}
-
 	/* Set default mode to max resolution */
 	imx319->cur_mode = &supported_modes[0];
 
Signature · 1 line
2.39.2
author maintainer
#10 lore raw

PATCH v2 4/4 media: imx355: Use v4l2_link_freq_to_bitmap helper

In reply to Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link freq can be used on a given system.

---
 drivers/media/i2c/imx355.c | 53 ++++++++++----------------------------
 1 file changed, 14 insertions(+), 39 deletions(-)
drivers/media/i2c/imx355.c +14 -39
index 8c995c58743a..7e9c2f65fa08 100644
@@ -56,7 +56,7 @@
 #define IMX355_REG_ORIENTATION		0x0101
 
 /* default link frequency and external clock */
-#define IMX355_LINK_FREQ_DEFAULT	360000000
+#define IMX355_LINK_FREQ_DEFAULT	360000000LL
 #define IMX355_EXT_CLK			19200000
 #define IMX355_LINK_FREQ_INDEX		0
 
@@ -93,8 +93,7 @@ struct imx355_mode {
 
 struct imx355_hwcfg {
 	u32 ext_clk;			/* sensor external clk */
-	s64 *link_freqs;		/* CSI-2 link frequencies */
-	unsigned int nr_of_link_freqs;
+	unsigned long link_freq_bitmap;
 };
 
 struct imx355 {
@@ -115,7 +114,6 @@ struct imx355 {
 	const struct imx355_mode *cur_mode;
 
 	struct imx355_hwcfg *hwcfg;
-	s64 link_def_freq;	/* CSI-2 link default frequency */
 
 	/*
 	 * Mutex for serialized access:
@@ -879,7 +877,10 @@ static const char * const imx355_test_pattern_menu[] = {
 	"Pseudorandom Sequence (PN9)",
 };
 
-/* supported link frequencies */
+/*
+ * When adding more than the one below, make sure the disallowed ones will
+ * actually be disabled in the LINK_FREQ control.
+ */
 static const s64 link_freq_menu_items[] = {
 	IMX355_LINK_FREQ_DEFAULT,
 };
@@ -1356,7 +1357,7 @@ imx355_set_pad_format(struct v4l2_subdev *sd,
 		*framefmt = fmt->format;
 	} else {
 		imx355->cur_mode = mode;
-		pixel_rate = imx355->link_def_freq * 2 * 4;
+		pixel_rate = IMX355_LINK_FREQ_DEFAULT * 2 * 4;
 		do_div(pixel_rate, 10);
 		__v4l2_ctrl_s_ctrl_int64(imx355->pixel_rate, pixel_rate);
 		/* Update limits and set FPS to default */
@@ -1543,7 +1544,7 @@ static int imx355_init_controls(struct imx355 *imx355)
 		imx355->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
 
 	/* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
-	pixel_rate = imx355->link_def_freq * 2 * 4;
+	pixel_rate = IMX355_LINK_FREQ_DEFAULT * 2 * 4;
 	do_div(pixel_rate, 10);
 	/* By default, PIXEL_RATE is read only */
 	imx355->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
@@ -1620,7 +1621,6 @@ static struct imx355_hwcfg *imx355_get_hwcfg(struct device *dev)
 	};
 	struct fwnode_handle *ep;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
-	unsigned int i;
 	int ret;
 
 	if (!fwnode)
@@ -1652,24 +1652,14 @@ static struct imx355_hwcfg *imx355_get_hwcfg(struct device *dev)
 		goto out_err;
 	}
 
-	dev_dbg(dev, "num of link freqs: %d", bus_cfg.nr_of_link_frequencies);
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_warn(dev, "no link frequencies defined");
-		goto out_err;
-	}
-
-	cfg->nr_of_link_freqs = bus_cfg.nr_of_link_frequencies;
-	cfg->link_freqs = devm_kcalloc(dev,
-				       bus_cfg.nr_of_link_frequencies + 1,
-				       sizeof(*cfg->link_freqs), GFP_KERNEL);
-	if (!cfg->link_freqs)
+	ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq_menu_items,
+				       ARRAY_SIZE(link_freq_menu_items),
+				       &cfg->link_freq_bitmap);
+	if (ret)
 		goto out_err;
 
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		cfg->link_freqs[i] = bus_cfg.link_frequencies[i];
-		dev_dbg(dev, "link_freq[%d] = %lld", i, cfg->link_freqs[i]);
-	}
-
 	v4l2_fwnode_endpoint_free(&bus_cfg);
 	fwnode_handle_put(ep);
 	return cfg;
@@ -1684,7 +1674,6 @@ static int imx355_probe(struct i2c_client *client)
 {
 	struct imx355 *imx355;
 	int ret;
-	u32 i;
 
 	imx355 = devm_kzalloc(&client->dev, sizeof(*imx355), GFP_KERNEL);
 	if (!imx355)
@@ -1709,20 +1698,6 @@ static int imx355_probe(struct i2c_client *client)
 		goto error_probe;
 	}
 
-	imx355->link_def_freq = link_freq_menu_items[IMX355_LINK_FREQ_INDEX];
-	for (i = 0; i < imx355->hwcfg->nr_of_link_freqs; i++) {
-		if (imx355->hwcfg->link_freqs[i] == imx355->link_def_freq) {
-			dev_dbg(&client->dev, "link freq index %d matched", i);
-			break;
-		}
-	}
-
-	if (i == imx355->hwcfg->nr_of_link_freqs) {
-		dev_err(&client->dev, "no link frequency supported");
-		ret = -EINVAL;
-		goto error_probe;
-	}
-
 	/* Set default mode to max resolution */
 	imx355->cur_mode = &supported_modes[0];
 
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH v2 1/4] media: v4l: Add a helper for setting up link-frequencies control

In reply to Sakari Ailus
kernel test robot noticed the following build errors:

Hi Sakari,

kernel test robot noticed the following build errors:

[auto build test ERROR on media-tree/master]
[also build test ERROR on linuxtv-media-stage/master sailus-media-tree/streams linus/master v6.7 next-20240108] [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/Sakari-Ailus/media-v4l-Add-a-helper-for-setting-up-link-frequencies-control/20240108-155716
base:   git://linuxtv.org/media_tree.git master
patch link:    https://lore.kernel.org/r/20240108075221.15757-2-sakari.ailus%40linux.intel.com
patch subject: [PATCH v2 1/4] media: v4l: Add a helper for setting up link-frequencies control
config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20240108/[email protected]/config)
compiler: ClangBuiltLinux clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240108/[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 >>):

>> drivers/media/v4l2-core/v4l2-common.c:634:19: error: use of undeclared identifier 'v4l2_link_frequencies_to_bitmap'; did you mean 'v4l2_link_freq_to_bitmap'?
     634 | EXPORT_SYMBOL_GPL(v4l2_link_frequencies_to_bitmap);
         |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                   v4l2_link_freq_to_bitmap
   include/linux/export.h:87:48: note: expanded from macro 'EXPORT_SYMBOL_GPL'
      87 | #define EXPORT_SYMBOL_GPL(sym)          _EXPORT_SYMBOL(sym, "GPL")
         |                                                        ^
   include/linux/export.h:83:54: note: expanded from macro '_EXPORT_SYMBOL'
      83 | #define _EXPORT_SYMBOL(sym, license)    __EXPORT_SYMBOL(sym, license, "")
         |                                                         ^
   include/linux/export.h:74:16: note: expanded from macro '__EXPORT_SYMBOL'
      74 |         extern typeof(sym) sym;                                 \
         |                       ^

drivers/media/v4l2-core/v4l2-common.c:589:5: note: 'v4l2_link_freq_to_bitmap' declared here
589 | int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
| ^
1 error generated.

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

588
589 int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,

   590				     unsigned int num_of_fw_link_freqs,
   591				     const s64 *driver_link_freqs,
   592				     unsigned int num_of_driver_link_freqs,
   593				     unsigned long *bitmap)
   594	{
   595		unsigned int i;
   596	
   597		*bitmap = 0;
   598	
   599		if (!num_of_fw_link_freqs) {
   600			dev_err(dev, "no link frequencies in firmware\n");
   601			return -ENODATA;
   602		}
   603	
   604		for (i = 0; i < num_of_fw_link_freqs; i++) {
   605			unsigned int j;
   606	
   607			for (j = 0; j < num_of_driver_link_freqs; j++) {
   608				if (fw_link_freqs[i] != driver_link_freqs[j])
   609					continue;
   610	
   611				dev_dbg(dev, "enabling link frequency %lld Hz\n",
   612					driver_link_freqs[j]);
   613				*bitmap |= BIT(j);
   614				break;
   615			}
   616		}
   617	
   618		if (!*bitmap) {
   619			dev_err(dev, "no matching link frequencies found\n");
   620	
   621			dev_dbg(dev, "specified in firmware:\n");
   622			for (i = 0; i < num_of_fw_link_freqs; i++)
   623				dev_dbg(dev, "\t%llu Hz\n", fw_link_freqs[i]);
   624	
   625			dev_dbg(dev, "driver supported:\n");
   626			for (i = 0; i < num_of_driver_link_freqs; i++)
   627				dev_dbg(dev, "\t%lld Hz\n", driver_link_freqs[i]);
   628	
   629			return -ENOENT;
   630		}
   631	
   632		return 0;
   633	}
 > 634	EXPORT_SYMBOL_GPL(v4l2_link_frequencies_to_bitmap);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [PATCH v2 2/4] media: imx334: Use v4l2_link_freq_to_bitmap helper

In reply to Sakari Ailus

Hi Sakari

Quoted 83 lines · Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +9 -26
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/imx334.c static int imx334_init_controls(struct imx334 *imx334) · line 1297

Quoted 7 lines
drivers/media/i2c/imx334.c +2 -2
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),

How does this work if

driver_freqs = {
        FREQ1,
        FREQ2,
        FREQ3,
        FREQ4
};
fw_freqs = {
        FREQ2,
        FREQ4
};

Will FREQ3 be selectable by userspace ?

Quoted 15 lines
drivers/media/i2c/imx334.c +1 -2
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 
--

2.39.2

author maintainer
#5 lore raw

Re: [PATCH v2 2/4] media: imx334: Use v4l2_link_freq_to_bitmap helper

Hi Jacopo,

Quoted 103 lines · Jacopo Mondi

Hi Sakari

Quoted 83 lines · Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +9 -26
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/imx334.c static int imx334_init_controls(struct imx334 *imx334) · line 1297

Quoted 7 lines
drivers/media/i2c/imx334.c +2 -2
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),

How does this work if

driver_freqs = {
        FREQ1,
        FREQ2,
        FREQ3,
        FREQ4
};
Quoted 6 lines
fw_freqs = {
        FREQ2,
        FREQ4
};

Will FREQ3 be selectable by userspace ?

That's a good question. Drivers that support more than two frequencies should use v4l2_ctrl_modify_range() to provide a bitmap of supported frequencies, v4l2_ctrl_new_int_menu() doesn't have an argument for that.

Maybe we could add one? :-)

Quoted 13 lines
Quoted 13 lines
drivers/media/i2c/imx334.c +1 -1
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 
Signature · 3 lines
Regards,

Sakari Ailus
#6 lore raw

Re: Re: [PATCH v2 2/4] media: imx334: Use v4l2_link_freq_to_bitmap helper

Hi Sakari

Quoted 111 lines · Sakari Ailus

Hi Jacopo,

Quoted 108 lines · Jacopo Mondi

Hi Sakari

Quoted 83 lines · Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +9 -26
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/imx334.c static int imx334_init_controls(struct imx334 *imx334) · line 1297

Quoted 7 lines
drivers/media/i2c/imx334.c +2 -2
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),

How does this work if

driver_freqs = {
        FREQ1,
        FREQ2,
        FREQ3,
        FREQ4
};
fw_freqs = {
        FREQ2,
        FREQ4
};
Quoted 8 lines
Quoted 1 line

Will FREQ3 be selectable by userspace ?

That's a good question. Drivers that support more than two frequencies should use v4l2_ctrl_modify_range() to provide a bitmap of supported frequencies, v4l2_ctrl_new_int_menu() doesn't have an argument for that.

Maybe we could add one? :-)

How would you feel about v4l2_link_freq_to_bitmap() returning an s64[] instead so that the caller can pass it to v4l2_ctrl_new_int_menu() directly ? I know, the driver wuold then need to remember to release it, which is not an ideal pattern (allocate in the core, release in the driver).

Alternatively, could v4l2_fwnode_endpoint_alloc_parse() be passed the list of driver supported frequencies and store in vep->link_frequencies[] the intersection between the driver and firmware frequencies ? After all, the _alloc_parse() version of v4l2_fwnode_endpoint_parse() it's only there to support link_frequencies parsing, hence passing to it the driver's supported frequencies might not look that weird...

Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
drivers/media/i2c/imx334.c +0 -0
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
Quoted 6 lines

drivers/media/i2c/imx334.c static int imx334_probe(struct i2c_client *client) · line 1375

Quoted 6 lines
drivers/media/i2c/imx334.c +1 -1
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 

--
Regards,

Sakari Ailus

author maintainer
#7 lore raw

Re: Re: [PATCH v2 2/4] media: imx334: Use v4l2_link_freq_to_bitmap helper

Hi Jacopo,

Quoted 120 lines · Jacopo Mondi

Hi Sakari

Quoted 117 lines · Sakari Ailus

Hi Jacopo,

Quoted 103 lines · Jacopo Mondi

Hi Sakari

Quoted 83 lines · Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +9 -26
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/imx334.c static int imx334_init_controls(struct imx334 *imx334) · line 1297

Quoted 7 lines
drivers/media/i2c/imx334.c +2 -2
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),

How does this work if

driver_freqs = {
        FREQ1,
        FREQ2,
        FREQ3,
        FREQ4
};
Quoted 6 lines
fw_freqs = {
        FREQ2,
        FREQ4
};

Will FREQ3 be selectable by userspace ?

That's a good question. Drivers that support more than two frequencies should use v4l2_ctrl_modify_range() to provide a bitmap of supported frequencies, v4l2_ctrl_new_int_menu() doesn't have an argument for that.

Quoted 8 lines
Quoted 2 lines

Maybe we could add one? :-)

How would you feel about v4l2_link_freq_to_bitmap() returning an s64[] instead so that the caller can pass it to v4l2_ctrl_new_int_menu() directly ? I know, the driver wuold then need to remember to release it, which is not an ideal pattern (allocate in the core, release in the driver).

Given that this is a single 64-bit value, I don't think it's useful to allocate it in v4l2_link_freq_to_bitmap().

Quoted 7 lines

Alternatively, could v4l2_fwnode_endpoint_alloc_parse() be passed the list of driver supported frequencies and store in vep->link_frequencies[] the intersection between the driver and firmware frequencies ? After all, the _alloc_parse() version of v4l2_fwnode_endpoint_parse() it's only there to support link_frequencies parsing, hence passing to it the driver's supported frequencies might not look that weird...

It's not a good fit for the functional scope of v4l2_fwnode_endpoint_alloc_parse(), but maybe we could add a new function to do both (and possibly more)? If this one isn't eventually be needed in drivers, it can be made static.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
drivers/media/i2c/imx334.c +1 -1
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 
Signature · 3 lines
Regards,

Sakari Ailus
#8 lore raw

Re: Re: Re: [PATCH v2 2/4] media: imx334: Use v4l2_link_freq_to_bitmap helper

Hi Sakari

Quoted 126 lines · Sakari Ailus

Hi Jacopo,

Quoted 123 lines · Jacopo Mondi

Hi Sakari

Quoted 119 lines · Sakari Ailus

Hi Jacopo,

Quoted 103 lines · Jacopo Mondi

Hi Sakari

Quoted 83 lines · Sakari Ailus

Use the v4l2_link_freq_to_bitmap() helper to figure out which driver-supported link frequencies can be used on a given system.

---
 drivers/media/i2c/imx334.c | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/media/i2c/imx334.c +9 -26
index 6725b3e2a73e..40863d87d341 100644
@@ -136,7 +136,7 @@ struct imx334_mode {
  * @vblank: Vertical blanking in lines
  * @cur_mode: Pointer to current selected sensor mode
  * @mutex: Mutex for serializing sensor controls
- * @menu_skip_mask: Menu skip mask for link_freq_ctrl
+ * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  * @cur_code: current selected format code
  */
 struct imx334 {
@@ -158,7 +158,7 @@ struct imx334 {
 	u32 vblank;
 	const struct imx334_mode *cur_mode;
 	struct mutex mutex;
-	unsigned long menu_skip_mask;
+	unsigned long link_freq_bitmap;
 	u32 cur_code;
 };
 
@@ -954,9 +954,9 @@ static int imx334_init_state(struct v4l2_subdev *sd,
 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
 
 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
-				 __fls(imx334->menu_skip_mask),
-				 ~(imx334->menu_skip_mask),
-				 __ffs(imx334->menu_skip_mask));
+				 __fls(imx334->link_freq_bitmap),
+				 ~(imx334->link_freq_bitmap),
+				 __ffs(imx334->link_freq_bitmap));
 
 	mutex_unlock(&imx334->mutex);
 
@@ -1112,7 +1112,6 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 	};
 	struct fwnode_handle *ep;
 	unsigned long rate;
-	unsigned int i, j;
 	int ret;
 
 	if (!fwnode)
@@ -1157,26 +1156,10 @@ static int imx334_parse_hw_config(struct imx334 *imx334)
 		goto done_endpoint_free;
 	}
 
-	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(imx334->dev, "no link frequencies defined");
-		ret = -EINVAL;
-		goto done_endpoint_free;
-	}
-
-	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
-		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
-			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
-				set_bit(j, &imx334->menu_skip_mask);
-				break;
-			}
-		}
-
-		if (j == ARRAY_SIZE(link_freq)) {
-			ret = dev_err_probe(imx334->dev, -EINVAL,
-					    "no supported link freq found\n");
-			goto done_endpoint_free;
-		}
-	}
+	ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       link_freq, ARRAY_SIZE(link_freq),
+				       &imx334->link_freq_bitmap);
 
 done_endpoint_free:
 	v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/imx334.c static int imx334_init_controls(struct imx334 *imx334) · line 1297

Quoted 7 lines
drivers/media/i2c/imx334.c +2 -2
@@ -1310,8 +1293,8 @@ static int imx334_init_controls(struct imx334 *imx334)
 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
 							&imx334_ctrl_ops,
 							V4L2_CID_LINK_FREQ,
-							__fls(imx334->menu_skip_mask),
-							__ffs(imx334->menu_skip_mask),
+							__fls(imx334->link_freq_bitmap),
+							__ffs(imx334->link_freq_bitmap),

How does this work if

driver_freqs = {
        FREQ1,
        FREQ2,
        FREQ3,
        FREQ4
};
Quoted 6 lines
fw_freqs = {
        FREQ2,
        FREQ4
};

Will FREQ3 be selectable by userspace ?

That's a good question. Drivers that support more than two frequencies should use v4l2_ctrl_modify_range() to provide a bitmap of supported frequencies, v4l2_ctrl_new_int_menu() doesn't have an argument for that.

Maybe we could add one? :-)

Quoted 9 lines
Quoted 5 lines

How would you feel about v4l2_link_freq_to_bitmap() returning an s64[] instead so that the caller can pass it to v4l2_ctrl_new_int_menu() directly ? I know, the driver wuold then need to remember to release it, which is not an ideal pattern (allocate in the core, release in the driver).

Given that this is a single 64-bit value, I don't think it's useful to allocate it in v4l2_link_freq_to_bitmap().

For this driver it's a single s64, yes, but in the general case the intersection between the driver and the firmware frequencies is a list of 64-bit values..

Quoted 8 lines
Quoted 7 lines

Alternatively, could v4l2_fwnode_endpoint_alloc_parse() be passed the list of driver supported frequencies and store in vep->link_frequencies[] the intersection between the driver and firmware frequencies ? After all, the _alloc_parse() version of v4l2_fwnode_endpoint_parse() it's only there to support link_frequencies parsing, hence passing to it the driver's supported frequencies might not look that weird...

Quoted 5 lines

It's not a good fit for the functional scope of v4l2_fwnode_endpoint_alloc_parse(), but maybe we could add a new function to do both (and possibly more)? If this one isn't eventually be needed in drivers, it can be made static.

if it's needed in drivers then we're not adding anything useful to the core :)

Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/media/i2c/imx334.c +0 -0
 							link_freq);
 
 	if (imx334->link_freq_ctrl)
@@ -1386,7 +1369,7 @@ static int imx334_probe(struct i2c_client *client)
 	}
 
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

drivers/media/i2c/imx334.c static int imx334_probe(struct i2c_client *client) · line 1375

Quoted 6 lines
drivers/media/i2c/imx334.c +1 -1
 	/* Set default mode to max resolution */
-	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
+	imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
 	imx334->cur_code = imx334_mbus_codes[0];
 	imx334->vblank = imx334->cur_mode->vblank;
 

--
Regards,

Sakari Ailus