All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Mehdi Djait <mehdi.djait@linux.intel.com>
Cc: linux-media@vger.kernel.org,
	"Sakari Ailus" <sakari.ailus@linux.intel.com>,
	"Hans Verkuil" <hverkuil@kernel.org>,
	"Nicolas Dufresne" <nicolas.dufresne@collabora.com>,
	"Tomi Valkeinen" <tomi.valkeinen@ideasonboard.com>,
	"Jonas Karlman" <jonas@kwiboo.se>,
	"Matthew Majewski" <mattwmajewski@gmail.com>,
	"Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>
Subject: Re: [PATCH 55/72] media: v4l2-common: Add legacy camera sensor clock helper
Date: Mon, 14 Jul 2025 17:09:47 +0300	[thread overview]
Message-ID: <20250714140947.GG8243@pendragon.ideasonboard.com> (raw)
In-Reply-To: <3rs65tdti5p7qdrj4f5fx5rkdfk5xz5xcjbr6aupdhd6zpu5xe@fm5qpcvy3ab7>

Hi Mehdi,

On Mon, Jul 14, 2025 at 03:12:28PM +0200, Mehdi Djait wrote:
> On Mon, Jul 14, 2025 at 02:55:02PM +0200, Mehdi Djait wrote:
> > On Thu, Jul 10, 2025 at 08:47:51PM +0300, Laurent Pinchart wrote:
> > > The recently introduced devm_v4l2_sensor_clk_get() helper aims at
> > > simplifying sensor drivers by centralizing clock handling code, as well
> > > as reducing cargo-cult and deprecated behaviour.
> > > 
> > > A set of drivers implement external clock handling in a non-standard
> > > way. This can't be changed as there is a high risk of breaking existing
> > > platforms, but keeping the code as-is creates a risk of new drivers
> > > copying deprecated behaviour.
> > > 
> > > To fix this, introduce a new devm_v4l2_sensor_clk_get_legacy() helper
> > > and use it in those driver. Compared to devm_v4l2_sensor_clk_get(), the
> > > new helper takes the "clock-frequency" property into account and sets
> > > the external clock rate on OF platforms, and adds the ability to specify
> > > a fixed default or fallback clock rate in case the "clock-frequency"
> > > property is not present.
> > > 
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > ---
> > >  drivers/media/v4l2-core/v4l2-common.c | 39 +++++++++++++++++++------
> > >  include/media/v4l2-common.h           | 41 ++++++++++++++++++++++++++-
> > >  2 files changed, 70 insertions(+), 10 deletions(-)
> > > 
> > > diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> > > index cfc78ea6276c..6f140a78e683 100644
> > > --- a/drivers/media/v4l2-core/v4l2-common.c
> > > +++ b/drivers/media/v4l2-core/v4l2-common.c
> > > @@ -703,24 +703,40 @@ int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
> > >  }
> > >  EXPORT_SYMBOL_GPL(v4l2_link_freq_to_bitmap);
> > >  
> > > -struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
> > > +struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id,
> > > +				       bool legacy, bool fixed_rate,
> > > +				       unsigned long clk_rate)
> > >  {
> > > +	bool of_node = is_of_node(dev_fwnode(dev));
> > >  	const char *clk_id __free(kfree) = NULL;
> > >  	struct clk_hw *clk_hw;
> > >  	struct clk *clk;
> > > -	bool of_node;
> > > -	u32 rate;
> > > -	int ret;
> > > +	u32 rate = clk_rate;
> > 
> > [..]
> > 
> > > +	int ret = 0;
> > >  
> > >  	clk = devm_clk_get_optional(dev, id);
> > >  	if (IS_ERR(clk))
> > >  		return clk;
> > >  
> > > -	ret = device_property_read_u32(dev, "clock-frequency", &rate);
> > > -	of_node = is_of_node(dev_fwnode(dev));
> > > +	/*
> > > +	 * If the caller didn't request a fixed rate, retrieve it from the
> > > +	 * clock-frequency property. -EINVAL indicates the property is absent,
> > > +	 * and is not a failure. Other errors, or success with a clock-frequency
> > > +	 * value of 0, are hard failures.
> > > +	 */
> > > +	if (!fixed_rate || !clk_rate) {
> > > +		ret = device_property_read_u32(dev, "clock-frequency", &rate);
> > > +		if (ret != -EINVAL || !rate)
> 
> so according to the above comment, I think it should be:
> 		if (ret != -EINVAL || (!rate && !ret))

You're right, there's an issue here.

We need to return an error iif

- clock-frequency can't be read for a reason different than the property
  being absent (to support ACPI platforms where the property could be
  created dynamically at a later time) ; or

- clock-frequency can be read, and returns a 0 rate

That translates to

		if ((ret && ret != -EINVAL) || (!ret && !rate))

Omitting the initial ret test would return -EINVAL when clock-frequency
can be read correctly, which is not right. Do you agree with that ?

> > 
> > [..]
> > 
> > Let's take the case of camera sensor on a DT-system:
> > 
> > it will call the following:
> > devm_v4l2_sensor_clk_get(dev, NULL);
> > -> __devm_v4l2_sensor_clk_get(dev, id, false, false, 0);
> > --> with fixed_rate = 0 and clk_rate = 0 which will be assigned to
> > u32 rate
> > 
> > so it will go into the if statement and try to read the
> > "clock-frequency" property, which does not exist.
> > 
> > ret from device_property_read_u32() is -EINVAL and that is handled but
> > rate will still be 0 so we will return ERR_PTR(-EINVAL)
> > 
> > > +			return ERR_PTR(-EINVAL);
> > > +	}
> > >  
> > >  	if (clk) {
> > > -		if (!ret && !of_node) {
> > > +		/*
> > > +		 * On non-OF platforms, or when legacy behaviour is requested,
> > > +		 * set the clock rate if a rate has been specified by the caller
> > > +		 * of by the clock-frequency property.
> > 		   |
> > nit:		   +-> or ? 

Oops. I'll fix the typo.

> > > +		 */
> > > +		if (rate && (!of_node || legacy)) {
> > >  			ret = clk_set_rate(clk, rate);
> > >  			if (ret) {
> > >  				dev_err(dev, "Failed to set clock rate: %u\n",
> > > @@ -731,9 +747,14 @@ struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
> > >  		return clk;
> > >  	}
> > >  
> > > -	if (!IS_ENABLED(CONFIG_COMMON_CLK) || of_node)
> > > +	/*
> > > +	 * Register a dummy fixed clock on non-OF platforms or when legacy
> > > +	 * behaviour is requested. This required the common clock framework.
> > > +	 */
> > > +	if (!IS_ENABLED(CONFIG_COMMON_CLK) || (of_node && !legacy))
> > >  		return ERR_PTR(-ENOENT);
> > >  
> > > +	/* We need a rate to create a clock. */
> > >  	if (ret)
> > >  		return ERR_PTR(ret == -EINVAL ? -EPROBE_DEFER : ret);
> > >  
> > > @@ -750,4 +771,4 @@ struct clk *devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
> > >  
> > >  	return clk_hw->clk;
> > >  }
> > > -EXPORT_SYMBOL_GPL(devm_v4l2_sensor_clk_get);
> > > +EXPORT_SYMBOL_GPL(__devm_v4l2_sensor_clk_get);

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2025-07-14 14:10 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-10 17:46 [PATCH 00/72] media: i2c: Reduce cargo-cult Laurent Pinchart
2025-07-10 17:46 ` [PATCH 01/72] dt-bindings: media: Deprecate clock-frequency property for camera sensors Laurent Pinchart
2025-07-10 23:10   ` Rob Herring (Arm)
2025-07-10 17:46 ` [PATCH 02/72] dt-bindings: media: et8ek8: Deprecate clock-frequency property Laurent Pinchart
2025-07-10 23:11   ` Rob Herring (Arm)
2025-07-10 17:46 ` [PATCH 03/72] dt-bindings: media: imx258: Make clocks property required Laurent Pinchart
2025-07-10 23:11   ` Rob Herring (Arm)
2025-07-10 17:47 ` [PATCH 04/72] dt-bindings: media: imx274: " Laurent Pinchart
2025-07-10 19:37   ` Rob Herring (Arm)
2025-07-10 20:33     ` Laurent Pinchart
2025-07-10 17:47 ` [PATCH 05/72] ARM: dts: nxp: imx6qdl-pico: Replace clock-frequency in camera sensor node Laurent Pinchart
2025-07-10 20:19   ` Frank Li
2025-07-10 17:47 ` [PATCH 06/72] ARM: dts: nxp: imx6qdl-wandboard: " Laurent Pinchart
2025-07-10 20:18   ` Frank Li
2025-07-10 20:34     ` Laurent Pinchart
2025-07-11  5:17       ` Frank Li
2025-07-10 17:47 ` [PATCH 07/72] ARM: dts: samsung: exynos4210-i9100: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 08/72] ARM: dts: samsung: exynos4412-midas: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 09/72] ARM: dts: ti: omap3-n900: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 10/72] ARM: dts: ti: omap3-n950: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 11/72] ARM: dts: ti: omap3-n9: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 12/72] arm64: dts: qcom: sdm845-db845c-navigation-mezzanine: " Laurent Pinchart
2025-07-11 12:25   ` Konrad Dybcio
2025-07-11 12:45     ` Laurent Pinchart
2025-07-11 12:47       ` Konrad Dybcio
2025-07-10 17:47 ` [PATCH 13/72] arm64: dts: renesas: aistarvision-mipi-adapter-2.1: Drop clock-frequency from " Laurent Pinchart
2025-07-28 14:19   ` Geert Uytterhoeven
2025-07-28 15:08     ` Laurent Pinchart
2025-07-10 17:47 ` [PATCH 14/72] arm64: dts: renesas: rzg2l-smarc: " Laurent Pinchart
2025-07-28 14:20   ` Geert Uytterhoeven
2025-07-10 17:47 ` [PATCH 15/72] media: i2c: mt9v022: Drop unused mt9v022.h header Laurent Pinchart
2025-07-10 17:47 ` [PATCH 16/72] media: i2c: mt9v032: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 17/72] media: i2c: mt9v032: Drop support for platform data Laurent Pinchart
2025-07-10 17:47 ` [PATCH 18/72] media: i2c: mt9v111: Do not set clock rate manually Laurent Pinchart
2025-07-10 17:47 ` [PATCH 19/72] media: i2c: ov6650: Drop unused driver Laurent Pinchart
2025-07-10 17:47 ` [PATCH 20/72] media: i2c: hi556: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 21/72] media: i2c: hi556: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 22/72] media: i2c: hi847: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 23/72] media: i2c: hi847: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 24/72] media: i2c: imx208: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 25/72] media: i2c: imx208: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 26/72] media: i2c: imx319: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 27/72] media: i2c: imx319: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 28/72] media: i2c: imx355: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 29/72] media: i2c: imx335: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 30/72] media: i2c: og01a1b: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 31/72] media: i2c: og01a1b: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-15 11:44   ` Mehdi Djait
2025-07-15 18:41     ` Laurent Pinchart
2025-07-10 17:47 ` [PATCH 32/72] media: i2c: ov02c10: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 33/72] media: i2c: ov02c10: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 34/72] media: i2c: ov02e10: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 35/72] media: i2c: ov02e10: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 36/72] media: i2c: ov08d10: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 37/72] media: i2c: ov08d10: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 38/72] media: i2c: ov08x40: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 39/72] media: i2c: ov08x40: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 40/72] media: i2c: ov13858: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 41/72] media: i2c: ov13858: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 42/72] media: i2c: ov13b10: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 43/72] media: i2c: ov13b10: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 44/72] media: i2c: ov2740: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 45/72] media: i2c: ov2740: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 46/72] media: i2c: ov4689: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 47/72] media: i2c: ov5670: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 48/72] media: i2c: ov5670: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 49/72] media: i2c: ov5675: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 50/72] media: i2c: ov5675: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 51/72] media: i2c: ov5693: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 52/72] media: i2c: ov7251: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 53/72] media: i2c: ov9734: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 54/72] media: i2c: ov9734: Use V4L2 sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 55/72] media: v4l2-common: Add legacy camera " Laurent Pinchart
2025-07-14 12:54   ` Mehdi Djait
2025-07-14 13:12     ` Mehdi Djait
2025-07-14 14:09       ` Laurent Pinchart [this message]
2025-07-14 21:15         ` Mehdi Djait
2025-07-10 17:47 ` [PATCH 56/72] media: i2c: et8ek8: Drop support for per-mode external clock frequency Laurent Pinchart
2025-07-10 17:47 ` [PATCH 57/72] media: i2c: et8ek8: Use V4L2 legacy sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 58/72] media: i2c: gc05a2: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 59/72] media: i2c: gc08a3: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 60/72] media: i2c: imx258: Replace client->dev usage Laurent Pinchart
2025-07-10 17:47 ` [PATCH 61/72] media: i2c: imx258: Use V4L2 legacy sensor clock helper Laurent Pinchart
2025-07-10 17:47 ` [PATCH 62/72] media: i2c: imx290: " Laurent Pinchart
2025-07-10 17:47 ` [PATCH 63/72] media: i2c: ov02a10: Replace client->dev usage Laurent Pinchart
2025-07-10 17:48 ` [PATCH 64/72] media: i2c: ov02a10: Use V4L2 legacy sensor clock helper Laurent Pinchart
2025-07-10 17:48 ` [PATCH 65/72] media: i2c: ov2685: " Laurent Pinchart
2025-07-10 17:48 ` [PATCH 66/72] media: i2c: ov5645: " Laurent Pinchart
2025-07-10 17:48 ` [PATCH 67/72] media: i2c: ov5695: " Laurent Pinchart
2025-07-10 17:48 ` [PATCH 68/72] media: i2c: ov8856: Replace client->dev usage Laurent Pinchart
2025-07-10 17:48 ` [PATCH 69/72] media: i2c: ov8856: Use V4L2 legacy sensor clock helper Laurent Pinchart
2025-07-10 17:48 ` [PATCH 70/72] media: i2c: s5c73m3: " Laurent Pinchart
2025-07-10 17:48 ` [PATCH 71/72] media: i2c: s5k5baf: " Laurent Pinchart
2025-07-10 17:48 ` [PATCH 72/72] media: i2c: s5k6a3: " Laurent Pinchart
2025-07-24 11:42 ` [PATCH 00/72] media: i2c: Reduce cargo-cult Tarang Raval
2025-07-24 11:52   ` Laurent Pinchart
     [not found]     ` <PN3P287MB1829C9E8C78ADD70259A68F08B5EA@PN3P287MB1829.INDP287.PROD.OUTLOOK.COM>
2025-07-24 13:37       ` Mark Brown
2025-07-24 13:52       ` Laurent Pinchart
2025-07-24 14:20         ` Tarang Raval
2025-07-24 14:26           ` Mark Brown
2025-07-24 15:44           ` Laurent Pinchart
2025-07-25  7:00             ` Tarang Raval
2025-07-25  9:38               ` Laurent Pinchart
2025-07-25 10:35                 ` Tarang Raval
2025-07-25 11:00                   ` Laurent Pinchart
2025-07-25 11:31                     ` Tarang Raval
2025-07-25 12:35               ` Mark Brown
2025-07-26  6:17                 ` Tarang Raval
2025-08-11 23:27 ` (subset) " Bjorn Andersson
2025-08-12  8:51   ` Laurent Pinchart
2025-08-12  8:58     ` Krzysztof Kozlowski
2025-08-12  9:39       ` Laurent Pinchart
2025-08-12 10:28         ` Krzysztof Kozlowski
2025-08-12 10:34           ` Laurent Pinchart
2025-08-12 20:10           ` Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250714140947.GG8243@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=hverkuil@kernel.org \
    --cc=jonas@kwiboo.se \
    --cc=linux-media@vger.kernel.org \
    --cc=mattwmajewski@gmail.com \
    --cc=mehdi.djait@linux.intel.com \
    --cc=nicolas.dufresne@collabora.com \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    --cc=sakari.ailus@linux.intel.com \
    --cc=tomi.valkeinen@ideasonboard.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.