diff mbox series

[v2,2/4] media: i2c: alvium: removal of fr field

Message ID 20231220085609.2595732-3-tomm.merciai@gmail.com (mailing list archive)
State New, archived
Headers show
Series media: i2c: alvium: store frame interval in subdev | expand

Commit Message

Tommaso Merciai Dec. 20, 2023, 8:56 a.m. UTC
The fr (frame rate) field of the alvium_dev structure is
only used to pass result from alvium_set_frame_interval() to
alvium_set_frame_rate() that writes this info into the hw reg.
Replace them with function parameter.

Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
---
 drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
 drivers/media/i2c/alvium-csi2.h |  1 -
 2 files changed, 12 insertions(+), 13 deletions(-)

Comments

Laurent Pinchart Dec. 20, 2023, 9:13 a.m. UTC | #1
Hi Tommaso,

Thank you for the patch.

Use the imperative in the subject line:

media: i2c: alvium: Remove the fr field of the alvium_dev structure

On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> The fr (frame rate) field of the alvium_dev structure is
> only used to pass result from alvium_set_frame_interval() to
> alvium_set_frame_rate() that writes this info into the hw reg.
> Replace them with function parameter.

Replace it with a function parameter.

> 
> Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> ---
>  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
>  drivers/media/i2c/alvium-csi2.h |  1 -
>  2 files changed, 12 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> index 0dcd69bf9f92..a9ff6cc97cff 100644
> --- a/drivers/media/i2c/alvium-csi2.c
> +++ b/drivers/media/i2c/alvium-csi2.c
> @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
>  	return ret;
>  }
>  
> -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
>  {
>  	struct device *dev = &alvium->i2c_client->dev;
>  	int ret;
>  
>  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> -				  alvium->fr);
> +				  fr);
>  	if (ret) {
>  		dev_err(dev, "Fail to set frame rate lanes reg\n");
>  		return ret;
>  	}
>  
> -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
>  
>  	return 0;
>  }
> @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
>  }
>  
>  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> -				     struct v4l2_subdev_frame_interval *fi)
> +				     struct v4l2_subdev_frame_interval *fi,
> +				     u64 *req_fr)
>  {
>  	struct device *dev = &alvium->i2c_client->dev;
> -	u64 req_fr, dft_fr, min_fr, max_fr;
> +	u64 dft_fr, min_fr, max_fr;
>  	int ret;
>  
>  	if (fi->interval.denominator == 0)
> @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
>  	dev_dbg(dev, "fi->interval.denominator = %d\n",
>  		fi->interval.denominator);
>  
> -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
>  		       fi->interval.numerator);
>  
> -	if (req_fr >= max_fr && req_fr <= min_fr)
> -		req_fr = dft_fr;
> +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> +		*req_fr = dft_fr;

Shouldn't we clamp the value to [min, max] instead of using the default
if it's out of range ? Something like

	*req_fr = clamp(*req_fr, min_fr, max_fr)

This makes me realize that the current code is wrong, req_fr can't be >=
max and <= min at the same time. You probably meant || instead of &&.

This should be fixed in a separate patch.

>  
> -	alvium->fr = req_fr;
>  	alvium->frame_interval.numerator = fi->interval.numerator;
>  	alvium->frame_interval.denominator = fi->interval.denominator;
>  
> @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
>  				   struct v4l2_subdev_frame_interval *fi)
>  {
>  	struct alvium_dev *alvium = sd_to_alvium(sd);
> +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;

Do you need to initialize the variable ? It doesn't seem to be required.

With these small issues fixed,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

>  	int ret;
>  
>  	/*
> @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
>  	if (alvium->streaming)
>  		return -EBUSY;
>  
> -	ret = alvium_set_frame_interval(alvium, fi);
> +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
>  	if (!ret)
> -		ret = alvium_set_frame_rate(alvium);
> +		ret = alvium_set_frame_rate(alvium, req_fr);
>  
>  	return ret;
>  }
> @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
>  	/* Setup initial frame interval*/
>  	alvium->frame_interval.numerator = 1;
>  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
>  
>  	/* Setup the initial mode */
>  	alvium->mode.fmt = alvium_csi2_default_fmt;
> diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> index 17f0bbbd1839..80066ac25047 100644
> --- a/drivers/media/i2c/alvium-csi2.h
> +++ b/drivers/media/i2c/alvium-csi2.h
> @@ -443,7 +443,6 @@ struct alvium_dev {
>  
>  	struct alvium_mode mode;
>  	struct v4l2_fract frame_interval;
> -	u64 fr;
>  
>  	u8 h_sup_csi_lanes;
>  	u64 link_freq;
Tommaso Merciai Dec. 20, 2023, 10:02 a.m. UTC | #2
Hi Laurent,
Thanks for your review.

On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> Hi Tommaso,
> 
> Thank you for the patch.
> 
> Use the imperative in the subject line:
> 
> media: i2c: alvium: Remove the fr field of the alvium_dev structure
> 
> On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > The fr (frame rate) field of the alvium_dev structure is
> > only used to pass result from alvium_set_frame_interval() to
> > alvium_set_frame_rate() that writes this info into the hw reg.
> > Replace them with function parameter.
> 
> Replace it with a function parameter.

Thanks I'll fix this in v3.

> 
> > 
> > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > ---
> >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> >  drivers/media/i2c/alvium-csi2.h |  1 -
> >  2 files changed, 12 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > --- a/drivers/media/i2c/alvium-csi2.c
> > +++ b/drivers/media/i2c/alvium-csi2.c
> > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> >  	return ret;
> >  }
> >  
> > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> >  {
> >  	struct device *dev = &alvium->i2c_client->dev;
> >  	int ret;
> >  
> >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > -				  alvium->fr);
> > +				  fr);
> >  	if (ret) {
> >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> >  		return ret;
> >  	}
> >  
> > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> >  
> >  	return 0;
> >  }
> > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> >  }
> >  
> >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > -				     struct v4l2_subdev_frame_interval *fi)
> > +				     struct v4l2_subdev_frame_interval *fi,
> > +				     u64 *req_fr)
> >  {
> >  	struct device *dev = &alvium->i2c_client->dev;
> > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > +	u64 dft_fr, min_fr, max_fr;
> >  	int ret;
> >  
> >  	if (fi->interval.denominator == 0)
> > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> >  		fi->interval.denominator);
> >  
> > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> >  		       fi->interval.numerator);
> >  
> > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > -		req_fr = dft_fr;
> > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > +		*req_fr = dft_fr;
> 
> Shouldn't we clamp the value to [min, max] instead of using the default
> if it's out of range ? Something like
> 
> 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> 
> This makes me realize that the current code is wrong, req_fr can't be >=
> max and <= min at the same time. You probably meant || instead of &&.
> 
> This should be fixed in a separate patch.

If this is ok for you, after this series I can put a patch with || fix
instead of clamping, because if we clamp dft_fr is not used any more.
After if you agree I will work on clamping.
Thanks for the catch! :)

> 
> >  
> > -	alvium->fr = req_fr;
> >  	alvium->frame_interval.numerator = fi->interval.numerator;
> >  	alvium->frame_interval.denominator = fi->interval.denominator;
> >  
> > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> >  				   struct v4l2_subdev_frame_interval *fi)
> >  {
> >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> 
> Do you need to initialize the variable ? It doesn't seem to be required.

Really not, it's just to maintain the logic of alvium->fr. I will drop
this in v3, thanks!

Regards,
Tommaso


> 
> With these small issues fixed,
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> >  	int ret;
> >  
> >  	/*
> > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> >  	if (alvium->streaming)
> >  		return -EBUSY;
> >  
> > -	ret = alvium_set_frame_interval(alvium, fi);
> > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> >  	if (!ret)
> > -		ret = alvium_set_frame_rate(alvium);
> > +		ret = alvium_set_frame_rate(alvium, req_fr);
> >  
> >  	return ret;
> >  }
> > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> >  	/* Setup initial frame interval*/
> >  	alvium->frame_interval.numerator = 1;
> >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> >  
> >  	/* Setup the initial mode */
> >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > index 17f0bbbd1839..80066ac25047 100644
> > --- a/drivers/media/i2c/alvium-csi2.h
> > +++ b/drivers/media/i2c/alvium-csi2.h
> > @@ -443,7 +443,6 @@ struct alvium_dev {
> >  
> >  	struct alvium_mode mode;
> >  	struct v4l2_fract frame_interval;
> > -	u64 fr;
> >  
> >  	u8 h_sup_csi_lanes;
> >  	u64 link_freq;
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Laurent Pinchart Dec. 20, 2023, 10:06 a.m. UTC | #3
On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> Hi Laurent,
> Thanks for your review.
> 
> On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > Hi Tommaso,
> > 
> > Thank you for the patch.
> > 
> > Use the imperative in the subject line:
> > 
> > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > 
> > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > The fr (frame rate) field of the alvium_dev structure is
> > > only used to pass result from alvium_set_frame_interval() to
> > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > Replace them with function parameter.
> > 
> > Replace it with a function parameter.
> 
> Thanks I'll fix this in v3.
> 
> > 
> > > 
> > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > ---
> > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > 
> > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > --- a/drivers/media/i2c/alvium-csi2.c
> > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > >  	return ret;
> > >  }
> > >  
> > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > >  {
> > >  	struct device *dev = &alvium->i2c_client->dev;
> > >  	int ret;
> > >  
> > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > -				  alvium->fr);
> > > +				  fr);
> > >  	if (ret) {
> > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > >  		return ret;
> > >  	}
> > >  
> > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > >  
> > >  	return 0;
> > >  }
> > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > >  }
> > >  
> > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > -				     struct v4l2_subdev_frame_interval *fi)
> > > +				     struct v4l2_subdev_frame_interval *fi,
> > > +				     u64 *req_fr)
> > >  {
> > >  	struct device *dev = &alvium->i2c_client->dev;
> > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > +	u64 dft_fr, min_fr, max_fr;
> > >  	int ret;
> > >  
> > >  	if (fi->interval.denominator == 0)
> > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > >  		fi->interval.denominator);
> > >  
> > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > >  		       fi->interval.numerator);
> > >  
> > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > -		req_fr = dft_fr;
> > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > +		*req_fr = dft_fr;
> > 
> > Shouldn't we clamp the value to [min, max] instead of using the default
> > if it's out of range ? Something like
> > 
> > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > 
> > This makes me realize that the current code is wrong, req_fr can't be >=
> > max and <= min at the same time. You probably meant || instead of &&.
> > 
> > This should be fixed in a separate patch.
> 
> If this is ok for you, after this series I can put a patch with || fix
> instead of clamping, because if we clamp dft_fr is not used any more.
> After if you agree I will work on clamping.
> Thanks for the catch! :)

It's fine to fix this on top of the series, but I don't see why you
would need to first use ||. You can call clamp() and remove dft_fr.

> > >  
> > > -	alvium->fr = req_fr;
> > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > >  
> > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > >  				   struct v4l2_subdev_frame_interval *fi)
> > >  {
> > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > 
> > Do you need to initialize the variable ? It doesn't seem to be required.
> 
> Really not, it's just to maintain the logic of alvium->fr. I will drop
> this in v3, thanks!
> 
> > With these small issues fixed,
> > 
> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > 
> > >  	int ret;
> > >  
> > >  	/*
> > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > >  	if (alvium->streaming)
> > >  		return -EBUSY;
> > >  
> > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > >  	if (!ret)
> > > -		ret = alvium_set_frame_rate(alvium);
> > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > >  
> > >  	return ret;
> > >  }
> > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > >  	/* Setup initial frame interval*/
> > >  	alvium->frame_interval.numerator = 1;
> > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > >  
> > >  	/* Setup the initial mode */
> > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > index 17f0bbbd1839..80066ac25047 100644
> > > --- a/drivers/media/i2c/alvium-csi2.h
> > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > >  
> > >  	struct alvium_mode mode;
> > >  	struct v4l2_fract frame_interval;
> > > -	u64 fr;
> > >  
> > >  	u8 h_sup_csi_lanes;
> > >  	u64 link_freq;
Tommaso Merciai Dec. 20, 2023, 11:19 a.m. UTC | #4
Hi Laurent,
Thanks for your comment.

On Wed, Dec 20, 2023 at 12:06:43PM +0200, Laurent Pinchart wrote:
> On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> > Hi Laurent,
> > Thanks for your review.
> > 
> > On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > > Hi Tommaso,
> > > 
> > > Thank you for the patch.
> > > 
> > > Use the imperative in the subject line:
> > > 
> > > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > > 
> > > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > > The fr (frame rate) field of the alvium_dev structure is
> > > > only used to pass result from alvium_set_frame_interval() to
> > > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > > Replace them with function parameter.
> > > 
> > > Replace it with a function parameter.
> > 
> > Thanks I'll fix this in v3.
> > 
> > > 
> > > > 
> > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > ---
> > > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > > 
> > > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > > --- a/drivers/media/i2c/alvium-csi2.c
> > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > >  	return ret;
> > > >  }
> > > >  
> > > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > > >  {
> > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > >  	int ret;
> > > >  
> > > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > -				  alvium->fr);
> > > > +				  fr);
> > > >  	if (ret) {
> > > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > > >  		return ret;
> > > >  	}
> > > >  
> > > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > > >  
> > > >  	return 0;
> > > >  }
> > > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > > >  }
> > > >  
> > > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > -				     struct v4l2_subdev_frame_interval *fi)
> > > > +				     struct v4l2_subdev_frame_interval *fi,
> > > > +				     u64 *req_fr)
> > > >  {
> > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > > +	u64 dft_fr, min_fr, max_fr;
> > > >  	int ret;
> > > >  
> > > >  	if (fi->interval.denominator == 0)
> > > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > > >  		fi->interval.denominator);
> > > >  
> > > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > >  		       fi->interval.numerator);
> > > >  
> > > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > > -		req_fr = dft_fr;
> > > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > > +		*req_fr = dft_fr;
> > > 
> > > Shouldn't we clamp the value to [min, max] instead of using the default
> > > if it's out of range ? Something like
> > > 
> > > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > > 
> > > This makes me realize that the current code is wrong, req_fr can't be >=
> > > max and <= min at the same time. You probably meant || instead of &&.
> > > 
> > > This should be fixed in a separate patch.
> > 
> > If this is ok for you, after this series I can put a patch with || fix
> > instead of clamping, because if we clamp dft_fr is not used any more.
> > After if you agree I will work on clamping.
> > Thanks for the catch! :)
> 
> It's fine to fix this on top of the series, but I don't see why you
> would need to first use ||. You can call clamp() and remove dft_fr.

I'm just thinking out loud eh :)

Maybe in the future we need to expose fr infos to the user to play with
that. But we are writing for now, then we can replan to readd dft_fr
read later.

I think this is what your are suggesting:

+++ b/drivers/media/i2c/alvium-csi2.c
@@ -1171,12 +1171,10 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
 }

 static int alvium_get_frame_interval(struct alvium_dev *alvium,
-                                    u64 *dft_fr, u64 *min_fr, u64 *max_fr)
+                                    u64 *min_fr, u64 *max_fr)
 {
        int ret = 0;

-       alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
-                   dft_fr, &ret);
        alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
                    min_fr, &ret);
        alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
@@ -1647,7 +1645,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
 {
        struct alvium_dev *alvium = sd_to_alvium(sd);
        struct device *dev = &alvium->i2c_client->dev;
-       u64 req_fr, dft_fr, min_fr, max_fr;
+       u64 req_fr, min_fr, max_fr;
        struct v4l2_fract *interval;
        int ret;

@@ -1657,7 +1655,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
        if (fi->interval.denominator == 0)
                return -EINVAL;

-       ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
+       ret = alvium_get_frame_interval(alvium, &min_fr, &max_fr);
        if (ret) {
                dev_err(dev, "Fail to get frame interval\n");
                return ret;
@@ -1670,9 +1668,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,

        req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
                       fi->interval.numerator);
-
-       if (req_fr >= max_fr && req_fr <= min_fr)
-               req_fr = dft_fr;
+       req_fr = clamp(req_fr, min_fr, max_fr);

        interval = v4l2_subdev_state_get_interval(sd_state, 0);

right?
Thanks again.

Regards,
Tommaso

> 
> > > >  
> > > > -	alvium->fr = req_fr;
> > > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > > >  
> > > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > >  				   struct v4l2_subdev_frame_interval *fi)
> > > >  {
> > > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > > 
> > > Do you need to initialize the variable ? It doesn't seem to be required.
> > 
> > Really not, it's just to maintain the logic of alvium->fr. I will drop
> > this in v3, thanks!
> > 
> > > With these small issues fixed,
> > > 
> > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > 
> > > >  	int ret;
> > > >  
> > > >  	/*
> > > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > >  	if (alvium->streaming)
> > > >  		return -EBUSY;
> > > >  
> > > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > > >  	if (!ret)
> > > > -		ret = alvium_set_frame_rate(alvium);
> > > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > > >  
> > > >  	return ret;
> > > >  }
> > > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > > >  	/* Setup initial frame interval*/
> > > >  	alvium->frame_interval.numerator = 1;
> > > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > > >  
> > > >  	/* Setup the initial mode */
> > > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > > index 17f0bbbd1839..80066ac25047 100644
> > > > --- a/drivers/media/i2c/alvium-csi2.h
> > > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > > >  
> > > >  	struct alvium_mode mode;
> > > >  	struct v4l2_fract frame_interval;
> > > > -	u64 fr;
> > > >  
> > > >  	u8 h_sup_csi_lanes;
> > > >  	u64 link_freq;
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Laurent Pinchart Dec. 20, 2023, 11:29 a.m. UTC | #5
On Wed, Dec 20, 2023 at 12:19:23PM +0100, Tommaso Merciai wrote:
> On Wed, Dec 20, 2023 at 12:06:43PM +0200, Laurent Pinchart wrote:
> > On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> > > On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > > > Hi Tommaso,
> > > > 
> > > > Thank you for the patch.
> > > > 
> > > > Use the imperative in the subject line:
> > > > 
> > > > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > > > 
> > > > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > > > The fr (frame rate) field of the alvium_dev structure is
> > > > > only used to pass result from alvium_set_frame_interval() to
> > > > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > > > Replace them with function parameter.
> > > > 
> > > > Replace it with a function parameter.
> > > 
> > > Thanks I'll fix this in v3.
> > > 
> > > > > 
> > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > ---
> > > > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > > > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > > > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > > > 
> > > > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > > > --- a/drivers/media/i2c/alvium-csi2.c
> > > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > > >  	return ret;
> > > > >  }
> > > > >  
> > > > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > > > >  {
> > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > >  	int ret;
> > > > >  
> > > > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > > -				  alvium->fr);
> > > > > +				  fr);
> > > > >  	if (ret) {
> > > > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > > > >  		return ret;
> > > > >  	}
> > > > >  
> > > > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > > > >  
> > > > >  	return 0;
> > > > >  }
> > > > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > > > >  }
> > > > >  
> > > > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > -				     struct v4l2_subdev_frame_interval *fi)
> > > > > +				     struct v4l2_subdev_frame_interval *fi,
> > > > > +				     u64 *req_fr)
> > > > >  {
> > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > > > +	u64 dft_fr, min_fr, max_fr;
> > > > >  	int ret;
> > > > >  
> > > > >  	if (fi->interval.denominator == 0)
> > > > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > > > >  		fi->interval.denominator);
> > > > >  
> > > > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > >  		       fi->interval.numerator);
> > > > >  
> > > > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > > > -		req_fr = dft_fr;
> > > > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > > > +		*req_fr = dft_fr;
> > > > 
> > > > Shouldn't we clamp the value to [min, max] instead of using the default
> > > > if it's out of range ? Something like
> > > > 
> > > > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > > > 
> > > > This makes me realize that the current code is wrong, req_fr can't be >=
> > > > max and <= min at the same time. You probably meant || instead of &&.
> > > > 
> > > > This should be fixed in a separate patch.
> > > 
> > > If this is ok for you, after this series I can put a patch with || fix
> > > instead of clamping, because if we clamp dft_fr is not used any more.
> > > After if you agree I will work on clamping.
> > > Thanks for the catch! :)
> > 
> > It's fine to fix this on top of the series, but I don't see why you
> > would need to first use ||. You can call clamp() and remove dft_fr.
> 
> I'm just thinking out loud eh :)
> 
> Maybe in the future we need to expose fr infos to the user to play with
> that. But we are writing for now, then we can replan to readd dft_fr
> read later.
> 
> I think this is what your are suggesting:
> 
> +++ b/drivers/media/i2c/alvium-csi2.c
> @@ -1171,12 +1171,10 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
>  }
> 
>  static int alvium_get_frame_interval(struct alvium_dev *alvium,
> -                                    u64 *dft_fr, u64 *min_fr, u64 *max_fr)
> +                                    u64 *min_fr, u64 *max_fr)
>  {
>         int ret = 0;
> 
> -       alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> -                   dft_fr, &ret);
>         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
>                     min_fr, &ret);
>         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
> @@ -1647,7 +1645,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
>  {
>         struct alvium_dev *alvium = sd_to_alvium(sd);
>         struct device *dev = &alvium->i2c_client->dev;
> -       u64 req_fr, dft_fr, min_fr, max_fr;
> +       u64 req_fr, min_fr, max_fr;
>         struct v4l2_fract *interval;
>         int ret;
> 
> @@ -1657,7 +1655,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
>         if (fi->interval.denominator == 0)
>                 return -EINVAL;
> 
> -       ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
> +       ret = alvium_get_frame_interval(alvium, &min_fr, &max_fr);
>         if (ret) {
>                 dev_err(dev, "Fail to get frame interval\n");
>                 return ret;
> @@ -1670,9 +1668,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> 
>         req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
>                        fi->interval.numerator);
> -
> -       if (req_fr >= max_fr && req_fr <= min_fr)
> -               req_fr = dft_fr;
> +       req_fr = clamp(req_fr, min_fr, max_fr);
> 
>         interval = v4l2_subdev_state_get_interval(sd_state, 0);
> 
> right?

Yes this looks good to me.

> > > > >  
> > > > > -	alvium->fr = req_fr;
> > > > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > > > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > > > >  
> > > > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > >  				   struct v4l2_subdev_frame_interval *fi)
> > > > >  {
> > > > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > > > 
> > > > Do you need to initialize the variable ? It doesn't seem to be required.
> > > 
> > > Really not, it's just to maintain the logic of alvium->fr. I will drop
> > > this in v3, thanks!
> > > 
> > > > With these small issues fixed,
> > > > 
> > > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > > 
> > > > >  	int ret;
> > > > >  
> > > > >  	/*
> > > > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > >  	if (alvium->streaming)
> > > > >  		return -EBUSY;
> > > > >  
> > > > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > > > >  	if (!ret)
> > > > > -		ret = alvium_set_frame_rate(alvium);
> > > > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > > > >  
> > > > >  	return ret;
> > > > >  }
> > > > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > > > >  	/* Setup initial frame interval*/
> > > > >  	alvium->frame_interval.numerator = 1;
> > > > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > > > >  
> > > > >  	/* Setup the initial mode */
> > > > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > > > index 17f0bbbd1839..80066ac25047 100644
> > > > > --- a/drivers/media/i2c/alvium-csi2.h
> > > > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > > > >  
> > > > >  	struct alvium_mode mode;
> > > > >  	struct v4l2_fract frame_interval;
> > > > > -	u64 fr;
> > > > >  
> > > > >  	u8 h_sup_csi_lanes;
> > > > >  	u64 link_freq;
Tommaso Merciai Dec. 20, 2023, 12:03 p.m. UTC | #6
On Wed, Dec 20, 2023 at 01:29:02PM +0200, Laurent Pinchart wrote:
> On Wed, Dec 20, 2023 at 12:19:23PM +0100, Tommaso Merciai wrote:
> > On Wed, Dec 20, 2023 at 12:06:43PM +0200, Laurent Pinchart wrote:
> > > On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> > > > On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > > > > Hi Tommaso,
> > > > > 
> > > > > Thank you for the patch.
> > > > > 
> > > > > Use the imperative in the subject line:
> > > > > 
> > > > > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > > > > 
> > > > > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > > > > The fr (frame rate) field of the alvium_dev structure is
> > > > > > only used to pass result from alvium_set_frame_interval() to
> > > > > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > > > > Replace them with function parameter.
> > > > > 
> > > > > Replace it with a function parameter.
> > > > 
> > > > Thanks I'll fix this in v3.
> > > > 
> > > > > > 
> > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > ---
> > > > > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > > > > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > > > > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > > > > 
> > > > > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > > > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > > > > --- a/drivers/media/i2c/alvium-csi2.c
> > > > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > > > >  	return ret;
> > > > > >  }
> > > > > >  
> > > > > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > > > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > > > > >  {
> > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > >  	int ret;
> > > > > >  
> > > > > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > > > -				  alvium->fr);
> > > > > > +				  fr);
> > > > > >  	if (ret) {
> > > > > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > > > > >  		return ret;
> > > > > >  	}
> > > > > >  
> > > > > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > > > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > > > > >  
> > > > > >  	return 0;
> > > > > >  }
> > > > > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > > > > >  }
> > > > > >  
> > > > > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > > -				     struct v4l2_subdev_frame_interval *fi)
> > > > > > +				     struct v4l2_subdev_frame_interval *fi,
> > > > > > +				     u64 *req_fr)
> > > > > >  {
> > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > > > > +	u64 dft_fr, min_fr, max_fr;
> > > > > >  	int ret;
> > > > > >  
> > > > > >  	if (fi->interval.denominator == 0)
> > > > > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > > > > >  		fi->interval.denominator);
> > > > > >  
> > > > > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > >  		       fi->interval.numerator);
> > > > > >  
> > > > > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > > > > -		req_fr = dft_fr;
> > > > > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > > > > +		*req_fr = dft_fr;
> > > > > 
> > > > > Shouldn't we clamp the value to [min, max] instead of using the default
> > > > > if it's out of range ? Something like
> > > > > 
> > > > > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > > > > 
> > > > > This makes me realize that the current code is wrong, req_fr can't be >=
> > > > > max and <= min at the same time. You probably meant || instead of &&.
> > > > > 
> > > > > This should be fixed in a separate patch.
> > > > 
> > > > If this is ok for you, after this series I can put a patch with || fix
> > > > instead of clamping, because if we clamp dft_fr is not used any more.
> > > > After if you agree I will work on clamping.
> > > > Thanks for the catch! :)
> > > 
> > > It's fine to fix this on top of the series, but I don't see why you
> > > would need to first use ||. You can call clamp() and remove dft_fr.
> > 
> > I'm just thinking out loud eh :)
> > 
> > Maybe in the future we need to expose fr infos to the user to play with
> > that. But we are writing for now, then we can replan to readd dft_fr
> > read later.
> > 
> > I think this is what your are suggesting:
> > 
> > +++ b/drivers/media/i2c/alvium-csi2.c
> > @@ -1171,12 +1171,10 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
> >  }
> > 
> >  static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > -                                    u64 *dft_fr, u64 *min_fr, u64 *max_fr)
> > +                                    u64 *min_fr, u64 *max_fr)
> >  {
> >         int ret = 0;
> > 
> > -       alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > -                   dft_fr, &ret);
> >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
> >                     min_fr, &ret);
> >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
> > @@ -1647,7 +1645,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> >  {
> >         struct alvium_dev *alvium = sd_to_alvium(sd);
> >         struct device *dev = &alvium->i2c_client->dev;
> > -       u64 req_fr, dft_fr, min_fr, max_fr;
> > +       u64 req_fr, min_fr, max_fr;
> >         struct v4l2_fract *interval;
> >         int ret;
> > 
> > @@ -1657,7 +1655,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> >         if (fi->interval.denominator == 0)
> >                 return -EINVAL;
> > 
> > -       ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
> > +       ret = alvium_get_frame_interval(alvium, &min_fr, &max_fr);
> >         if (ret) {
> >                 dev_err(dev, "Fail to get frame interval\n");
> >                 return ret;
> > @@ -1670,9 +1668,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > 
> >         req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> >                        fi->interval.numerator);
> > -
> > -       if (req_fr >= max_fr && req_fr <= min_fr)
> > -               req_fr = dft_fr;
> > +       req_fr = clamp(req_fr, min_fr, max_fr);
> > 
> >         interval = v4l2_subdev_state_get_interval(sd_state, 0);
> > 
> > right?
> 
> Yes this looks good to me.

Just an info.
Can I proceed to send v3 of this series and then the fix or better to
wait?

Thanks,
Tommaso


> 
> > > > > >  
> > > > > > -	alvium->fr = req_fr;
> > > > > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > > > > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > > > > >  
> > > > > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > >  				   struct v4l2_subdev_frame_interval *fi)
> > > > > >  {
> > > > > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > > > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > 
> > > > > Do you need to initialize the variable ? It doesn't seem to be required.
> > > > 
> > > > Really not, it's just to maintain the logic of alvium->fr. I will drop
> > > > this in v3, thanks!
> > > > 
> > > > > With these small issues fixed,
> > > > > 
> > > > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > > > 
> > > > > >  	int ret;
> > > > > >  
> > > > > >  	/*
> > > > > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > >  	if (alvium->streaming)
> > > > > >  		return -EBUSY;
> > > > > >  
> > > > > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > > > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > > > > >  	if (!ret)
> > > > > > -		ret = alvium_set_frame_rate(alvium);
> > > > > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > > > > >  
> > > > > >  	return ret;
> > > > > >  }
> > > > > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > > > > >  	/* Setup initial frame interval*/
> > > > > >  	alvium->frame_interval.numerator = 1;
> > > > > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > > > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > >  
> > > > > >  	/* Setup the initial mode */
> > > > > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > > > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > > > > index 17f0bbbd1839..80066ac25047 100644
> > > > > > --- a/drivers/media/i2c/alvium-csi2.h
> > > > > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > > > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > > > > >  
> > > > > >  	struct alvium_mode mode;
> > > > > >  	struct v4l2_fract frame_interval;
> > > > > > -	u64 fr;
> > > > > >  
> > > > > >  	u8 h_sup_csi_lanes;
> > > > > >  	u64 link_freq;
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Laurent Pinchart Dec. 20, 2023, 12:19 p.m. UTC | #7
On Wed, Dec 20, 2023 at 01:03:12PM +0100, Tommaso Merciai wrote:
> On Wed, Dec 20, 2023 at 01:29:02PM +0200, Laurent Pinchart wrote:
> > On Wed, Dec 20, 2023 at 12:19:23PM +0100, Tommaso Merciai wrote:
> > > On Wed, Dec 20, 2023 at 12:06:43PM +0200, Laurent Pinchart wrote:
> > > > On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> > > > > On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > > > > > Hi Tommaso,
> > > > > > 
> > > > > > Thank you for the patch.
> > > > > > 
> > > > > > Use the imperative in the subject line:
> > > > > > 
> > > > > > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > > > > > 
> > > > > > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > > > > > The fr (frame rate) field of the alvium_dev structure is
> > > > > > > only used to pass result from alvium_set_frame_interval() to
> > > > > > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > > > > > Replace them with function parameter.
> > > > > > 
> > > > > > Replace it with a function parameter.
> > > > > 
> > > > > Thanks I'll fix this in v3.
> > > > > 
> > > > > > > 
> > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > ---
> > > > > > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > > > > > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > > > > > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > > > > > 
> > > > > > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > > > > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > > > > > --- a/drivers/media/i2c/alvium-csi2.c
> > > > > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > > > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > > > > >  	return ret;
> > > > > > >  }
> > > > > > >  
> > > > > > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > > > > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > > > > > >  {
> > > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > > >  	int ret;
> > > > > > >  
> > > > > > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > > > > -				  alvium->fr);
> > > > > > > +				  fr);
> > > > > > >  	if (ret) {
> > > > > > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > > > > > >  		return ret;
> > > > > > >  	}
> > > > > > >  
> > > > > > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > > > > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > > > > > >  
> > > > > > >  	return 0;
> > > > > > >  }
> > > > > > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > > > > > >  }
> > > > > > >  
> > > > > > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > > > -				     struct v4l2_subdev_frame_interval *fi)
> > > > > > > +				     struct v4l2_subdev_frame_interval *fi,
> > > > > > > +				     u64 *req_fr)
> > > > > > >  {
> > > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > > > > > +	u64 dft_fr, min_fr, max_fr;
> > > > > > >  	int ret;
> > > > > > >  
> > > > > > >  	if (fi->interval.denominator == 0)
> > > > > > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > > > > > >  		fi->interval.denominator);
> > > > > > >  
> > > > > > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > > >  		       fi->interval.numerator);
> > > > > > >  
> > > > > > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > > > > > -		req_fr = dft_fr;
> > > > > > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > > > > > +		*req_fr = dft_fr;
> > > > > > 
> > > > > > Shouldn't we clamp the value to [min, max] instead of using the default
> > > > > > if it's out of range ? Something like
> > > > > > 
> > > > > > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > > > > > 
> > > > > > This makes me realize that the current code is wrong, req_fr can't be >=
> > > > > > max and <= min at the same time. You probably meant || instead of &&.
> > > > > > 
> > > > > > This should be fixed in a separate patch.
> > > > > 
> > > > > If this is ok for you, after this series I can put a patch with || fix
> > > > > instead of clamping, because if we clamp dft_fr is not used any more.
> > > > > After if you agree I will work on clamping.
> > > > > Thanks for the catch! :)
> > > > 
> > > > It's fine to fix this on top of the series, but I don't see why you
> > > > would need to first use ||. You can call clamp() and remove dft_fr.
> > > 
> > > I'm just thinking out loud eh :)
> > > 
> > > Maybe in the future we need to expose fr infos to the user to play with
> > > that. But we are writing for now, then we can replan to readd dft_fr
> > > read later.
> > > 
> > > I think this is what your are suggesting:
> > > 
> > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > @@ -1171,12 +1171,10 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
> > >  }
> > > 
> > >  static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > -                                    u64 *dft_fr, u64 *min_fr, u64 *max_fr)
> > > +                                    u64 *min_fr, u64 *max_fr)
> > >  {
> > >         int ret = 0;
> > > 
> > > -       alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > -                   dft_fr, &ret);
> > >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
> > >                     min_fr, &ret);
> > >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
> > > @@ -1647,7 +1645,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > >  {
> > >         struct alvium_dev *alvium = sd_to_alvium(sd);
> > >         struct device *dev = &alvium->i2c_client->dev;
> > > -       u64 req_fr, dft_fr, min_fr, max_fr;
> > > +       u64 req_fr, min_fr, max_fr;
> > >         struct v4l2_fract *interval;
> > >         int ret;
> > > 
> > > @@ -1657,7 +1655,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > >         if (fi->interval.denominator == 0)
> > >                 return -EINVAL;
> > > 
> > > -       ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
> > > +       ret = alvium_get_frame_interval(alvium, &min_fr, &max_fr);
> > >         if (ret) {
> > >                 dev_err(dev, "Fail to get frame interval\n");
> > >                 return ret;
> > > @@ -1670,9 +1668,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > 
> > >         req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > >                        fi->interval.numerator);
> > > -
> > > -       if (req_fr >= max_fr && req_fr <= min_fr)
> > > -               req_fr = dft_fr;
> > > +       req_fr = clamp(req_fr, min_fr, max_fr);
> > > 
> > >         interval = v4l2_subdev_state_get_interval(sd_state, 0);
> > > 
> > > right?
> > 
> > Yes this looks good to me.
> 
> Just an info.
> Can I proceed to send v3 of this series and then the fix or better to
> wait?

For me you can send v3. Bonus points if you include the above fix in v3
as a patch at the end :-) I think we would then be ready to merge the
whole series.

> > > > > > >  
> > > > > > > -	alvium->fr = req_fr;
> > > > > > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > > > > > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > > > > > >  
> > > > > > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > > >  				   struct v4l2_subdev_frame_interval *fi)
> > > > > > >  {
> > > > > > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > > > > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > > 
> > > > > > Do you need to initialize the variable ? It doesn't seem to be required.
> > > > > 
> > > > > Really not, it's just to maintain the logic of alvium->fr. I will drop
> > > > > this in v3, thanks!
> > > > > 
> > > > > > With these small issues fixed,
> > > > > > 
> > > > > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > > > > 
> > > > > > >  	int ret;
> > > > > > >  
> > > > > > >  	/*
> > > > > > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > > >  	if (alvium->streaming)
> > > > > > >  		return -EBUSY;
> > > > > > >  
> > > > > > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > > > > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > > > > > >  	if (!ret)
> > > > > > > -		ret = alvium_set_frame_rate(alvium);
> > > > > > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > > > > > >  
> > > > > > >  	return ret;
> > > > > > >  }
> > > > > > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > > > > > >  	/* Setup initial frame interval*/
> > > > > > >  	alvium->frame_interval.numerator = 1;
> > > > > > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > > > > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > > >  
> > > > > > >  	/* Setup the initial mode */
> > > > > > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > > > > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > > > > > index 17f0bbbd1839..80066ac25047 100644
> > > > > > > --- a/drivers/media/i2c/alvium-csi2.h
> > > > > > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > > > > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > > > > > >  
> > > > > > >  	struct alvium_mode mode;
> > > > > > >  	struct v4l2_fract frame_interval;
> > > > > > > -	u64 fr;
> > > > > > >  
> > > > > > >  	u8 h_sup_csi_lanes;
> > > > > > >  	u64 link_freq;
Tommaso Merciai Dec. 20, 2023, 12:42 p.m. UTC | #8
On Wed, Dec 20, 2023 at 02:19:44PM +0200, Laurent Pinchart wrote:
> On Wed, Dec 20, 2023 at 01:03:12PM +0100, Tommaso Merciai wrote:
> > On Wed, Dec 20, 2023 at 01:29:02PM +0200, Laurent Pinchart wrote:
> > > On Wed, Dec 20, 2023 at 12:19:23PM +0100, Tommaso Merciai wrote:
> > > > On Wed, Dec 20, 2023 at 12:06:43PM +0200, Laurent Pinchart wrote:
> > > > > On Wed, Dec 20, 2023 at 11:02:51AM +0100, Tommaso Merciai wrote:
> > > > > > On Wed, Dec 20, 2023 at 11:13:09AM +0200, Laurent Pinchart wrote:
> > > > > > > Hi Tommaso,
> > > > > > > 
> > > > > > > Thank you for the patch.
> > > > > > > 
> > > > > > > Use the imperative in the subject line:
> > > > > > > 
> > > > > > > media: i2c: alvium: Remove the fr field of the alvium_dev structure
> > > > > > > 
> > > > > > > On Wed, Dec 20, 2023 at 09:56:07AM +0100, Tommaso Merciai wrote:
> > > > > > > > The fr (frame rate) field of the alvium_dev structure is
> > > > > > > > only used to pass result from alvium_set_frame_interval() to
> > > > > > > > alvium_set_frame_rate() that writes this info into the hw reg.
> > > > > > > > Replace them with function parameter.
> > > > > > > 
> > > > > > > Replace it with a function parameter.
> > > > > > 
> > > > > > Thanks I'll fix this in v3.
> > > > > > 
> > > > > > > > 
> > > > > > > > Signed-off-by: Tommaso Merciai <tomm.merciai@gmail.com>
> > > > > > > > ---
> > > > > > > >  drivers/media/i2c/alvium-csi2.c | 24 ++++++++++++------------
> > > > > > > >  drivers/media/i2c/alvium-csi2.h |  1 -
> > > > > > > >  2 files changed, 12 insertions(+), 13 deletions(-)
> > > > > > > > 
> > > > > > > > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > > > > > > > index 0dcd69bf9f92..a9ff6cc97cff 100644
> > > > > > > > --- a/drivers/media/i2c/alvium-csi2.c
> > > > > > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > > > > > @@ -1185,19 +1185,19 @@ static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > > > > > >  	return ret;
> > > > > > > >  }
> > > > > > > >  
> > > > > > > > -static int alvium_set_frame_rate(struct alvium_dev *alvium)
> > > > > > > > +static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
> > > > > > > >  {
> > > > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > > > >  	int ret;
> > > > > > > >  
> > > > > > > >  	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > > > > > -				  alvium->fr);
> > > > > > > > +				  fr);
> > > > > > > >  	if (ret) {
> > > > > > > >  		dev_err(dev, "Fail to set frame rate lanes reg\n");
> > > > > > > >  		return ret;
> > > > > > > >  	}
> > > > > > > >  
> > > > > > > > -	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
> > > > > > > > +	dev_dbg(dev, "set frame rate: %llu us\n", fr);
> > > > > > > >  
> > > > > > > >  	return 0;
> > > > > > > >  }
> > > > > > > > @@ -1661,10 +1661,11 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
> > > > > > > >  }
> > > > > > > >  
> > > > > > > >  static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > > > > -				     struct v4l2_subdev_frame_interval *fi)
> > > > > > > > +				     struct v4l2_subdev_frame_interval *fi,
> > > > > > > > +				     u64 *req_fr)
> > > > > > > >  {
> > > > > > > >  	struct device *dev = &alvium->i2c_client->dev;
> > > > > > > > -	u64 req_fr, dft_fr, min_fr, max_fr;
> > > > > > > > +	u64 dft_fr, min_fr, max_fr;
> > > > > > > >  	int ret;
> > > > > > > >  
> > > > > > > >  	if (fi->interval.denominator == 0)
> > > > > > > > @@ -1681,13 +1682,12 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
> > > > > > > >  	dev_dbg(dev, "fi->interval.denominator = %d\n",
> > > > > > > >  		fi->interval.denominator);
> > > > > > > >  
> > > > > > > > -	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > > > > +	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > > > > > >  		       fi->interval.numerator);
> > > > > > > >  
> > > > > > > > -	if (req_fr >= max_fr && req_fr <= min_fr)
> > > > > > > > -		req_fr = dft_fr;
> > > > > > > > +	if (*req_fr >= max_fr && *req_fr <= min_fr)
> > > > > > > > +		*req_fr = dft_fr;
> > > > > > > 
> > > > > > > Shouldn't we clamp the value to [min, max] instead of using the default
> > > > > > > if it's out of range ? Something like
> > > > > > > 
> > > > > > > 	*req_fr = clamp(*req_fr, min_fr, max_fr)
> > > > > > > 
> > > > > > > This makes me realize that the current code is wrong, req_fr can't be >=
> > > > > > > max and <= min at the same time. You probably meant || instead of &&.
> > > > > > > 
> > > > > > > This should be fixed in a separate patch.
> > > > > > 
> > > > > > If this is ok for you, after this series I can put a patch with || fix
> > > > > > instead of clamping, because if we clamp dft_fr is not used any more.
> > > > > > After if you agree I will work on clamping.
> > > > > > Thanks for the catch! :)
> > > > > 
> > > > > It's fine to fix this on top of the series, but I don't see why you
> > > > > would need to first use ||. You can call clamp() and remove dft_fr.
> > > > 
> > > > I'm just thinking out loud eh :)
> > > > 
> > > > Maybe in the future we need to expose fr infos to the user to play with
> > > > that. But we are writing for now, then we can replan to readd dft_fr
> > > > read later.
> > > > 
> > > > I think this is what your are suggesting:
> > > > 
> > > > +++ b/drivers/media/i2c/alvium-csi2.c
> > > > @@ -1171,12 +1171,10 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
> > > >  }
> > > > 
> > > >  static int alvium_get_frame_interval(struct alvium_dev *alvium,
> > > > -                                    u64 *dft_fr, u64 *min_fr, u64 *max_fr)
> > > > +                                    u64 *min_fr, u64 *max_fr)
> > > >  {
> > > >         int ret = 0;
> > > > 
> > > > -       alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
> > > > -                   dft_fr, &ret);
> > > >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
> > > >                     min_fr, &ret);
> > > >         alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
> > > > @@ -1647,7 +1645,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > >  {
> > > >         struct alvium_dev *alvium = sd_to_alvium(sd);
> > > >         struct device *dev = &alvium->i2c_client->dev;
> > > > -       u64 req_fr, dft_fr, min_fr, max_fr;
> > > > +       u64 req_fr, min_fr, max_fr;
> > > >         struct v4l2_fract *interval;
> > > >         int ret;
> > > > 
> > > > @@ -1657,7 +1655,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > >         if (fi->interval.denominator == 0)
> > > >                 return -EINVAL;
> > > > 
> > > > -       ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
> > > > +       ret = alvium_get_frame_interval(alvium, &min_fr, &max_fr);
> > > >         if (ret) {
> > > >                 dev_err(dev, "Fail to get frame interval\n");
> > > >                 return ret;
> > > > @@ -1670,9 +1668,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > 
> > > >         req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
> > > >                        fi->interval.numerator);
> > > > -
> > > > -       if (req_fr >= max_fr && req_fr <= min_fr)
> > > > -               req_fr = dft_fr;
> > > > +       req_fr = clamp(req_fr, min_fr, max_fr);
> > > > 
> > > >         interval = v4l2_subdev_state_get_interval(sd_state, 0);
> > > > 
> > > > right?
> > > 
> > > Yes this looks good to me.
> > 
> > Just an info.
> > Can I proceed to send v3 of this series and then the fix or better to
> > wait?
> 
> For me you can send v3. Bonus points if you include the above fix in v3
> as a patch at the end :-) I think we would then be ready to merge the
> whole series.

Perfect, thanks :)

> 
> > > > > > > >  
> > > > > > > > -	alvium->fr = req_fr;
> > > > > > > >  	alvium->frame_interval.numerator = fi->interval.numerator;
> > > > > > > >  	alvium->frame_interval.denominator = fi->interval.denominator;
> > > > > > > >  
> > > > > > > > @@ -1699,6 +1699,7 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > > > >  				   struct v4l2_subdev_frame_interval *fi)
> > > > > > > >  {
> > > > > > > >  	struct alvium_dev *alvium = sd_to_alvium(sd);
> > > > > > > > +	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > > > 
> > > > > > > Do you need to initialize the variable ? It doesn't seem to be required.
> > > > > > 
> > > > > > Really not, it's just to maintain the logic of alvium->fr. I will drop
> > > > > > this in v3, thanks!
> > > > > > 
> > > > > > > With these small issues fixed,
> > > > > > > 
> > > > > > > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > > > > > 
> > > > > > > >  	int ret;
> > > > > > > >  
> > > > > > > >  	/*
> > > > > > > > @@ -1711,9 +1712,9 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
> > > > > > > >  	if (alvium->streaming)
> > > > > > > >  		return -EBUSY;
> > > > > > > >  
> > > > > > > > -	ret = alvium_set_frame_interval(alvium, fi);
> > > > > > > > +	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
> > > > > > > >  	if (!ret)
> > > > > > > > -		ret = alvium_set_frame_rate(alvium);
> > > > > > > > +		ret = alvium_set_frame_rate(alvium, req_fr);
> > > > > > > >  
> > > > > > > >  	return ret;
> > > > > > > >  }
> > > > > > > > @@ -2273,7 +2274,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
> > > > > > > >  	/* Setup initial frame interval*/
> > > > > > > >  	alvium->frame_interval.numerator = 1;
> > > > > > > >  	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
> > > > > > > > -	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
> > > > > > > >  
> > > > > > > >  	/* Setup the initial mode */
> > > > > > > >  	alvium->mode.fmt = alvium_csi2_default_fmt;
> > > > > > > > diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
> > > > > > > > index 17f0bbbd1839..80066ac25047 100644
> > > > > > > > --- a/drivers/media/i2c/alvium-csi2.h
> > > > > > > > +++ b/drivers/media/i2c/alvium-csi2.h
> > > > > > > > @@ -443,7 +443,6 @@ struct alvium_dev {
> > > > > > > >  
> > > > > > > >  	struct alvium_mode mode;
> > > > > > > >  	struct v4l2_fract frame_interval;
> > > > > > > > -	u64 fr;
> > > > > > > >  
> > > > > > > >  	u8 h_sup_csi_lanes;
> > > > > > > >  	u64 link_freq;
> 
> -- 
> Regards,
> 
> Laurent Pinchart
diff mbox series

Patch

diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
index 0dcd69bf9f92..a9ff6cc97cff 100644
--- a/drivers/media/i2c/alvium-csi2.c
+++ b/drivers/media/i2c/alvium-csi2.c
@@ -1185,19 +1185,19 @@  static int alvium_get_frame_interval(struct alvium_dev *alvium,
 	return ret;
 }
 
-static int alvium_set_frame_rate(struct alvium_dev *alvium)
+static int alvium_set_frame_rate(struct alvium_dev *alvium, u64 fr)
 {
 	struct device *dev = &alvium->i2c_client->dev;
 	int ret;
 
 	ret = alvium_write_hshake(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
-				  alvium->fr);
+				  fr);
 	if (ret) {
 		dev_err(dev, "Fail to set frame rate lanes reg\n");
 		return ret;
 	}
 
-	dev_dbg(dev, "set frame rate: %llu us\n", alvium->fr);
+	dev_dbg(dev, "set frame rate: %llu us\n", fr);
 
 	return 0;
 }
@@ -1661,10 +1661,11 @@  static int alvium_g_frame_interval(struct v4l2_subdev *sd,
 }
 
 static int alvium_set_frame_interval(struct alvium_dev *alvium,
-				     struct v4l2_subdev_frame_interval *fi)
+				     struct v4l2_subdev_frame_interval *fi,
+				     u64 *req_fr)
 {
 	struct device *dev = &alvium->i2c_client->dev;
-	u64 req_fr, dft_fr, min_fr, max_fr;
+	u64 dft_fr, min_fr, max_fr;
 	int ret;
 
 	if (fi->interval.denominator == 0)
@@ -1681,13 +1682,12 @@  static int alvium_set_frame_interval(struct alvium_dev *alvium,
 	dev_dbg(dev, "fi->interval.denominator = %d\n",
 		fi->interval.denominator);
 
-	req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
+	*req_fr = (u64)((fi->interval.denominator * USEC_PER_SEC) /
 		       fi->interval.numerator);
 
-	if (req_fr >= max_fr && req_fr <= min_fr)
-		req_fr = dft_fr;
+	if (*req_fr >= max_fr && *req_fr <= min_fr)
+		*req_fr = dft_fr;
 
-	alvium->fr = req_fr;
 	alvium->frame_interval.numerator = fi->interval.numerator;
 	alvium->frame_interval.denominator = fi->interval.denominator;
 
@@ -1699,6 +1699,7 @@  static int alvium_s_frame_interval(struct v4l2_subdev *sd,
 				   struct v4l2_subdev_frame_interval *fi)
 {
 	struct alvium_dev *alvium = sd_to_alvium(sd);
+	u64 req_fr = ALVIUM_DEFAULT_FR_HZ;
 	int ret;
 
 	/*
@@ -1711,9 +1712,9 @@  static int alvium_s_frame_interval(struct v4l2_subdev *sd,
 	if (alvium->streaming)
 		return -EBUSY;
 
-	ret = alvium_set_frame_interval(alvium, fi);
+	ret = alvium_set_frame_interval(alvium, fi, &req_fr);
 	if (!ret)
-		ret = alvium_set_frame_rate(alvium);
+		ret = alvium_set_frame_rate(alvium, req_fr);
 
 	return ret;
 }
@@ -2273,7 +2274,6 @@  static int alvium_subdev_init(struct alvium_dev *alvium)
 	/* Setup initial frame interval*/
 	alvium->frame_interval.numerator = 1;
 	alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
-	alvium->fr = ALVIUM_DEFAULT_FR_HZ;
 
 	/* Setup the initial mode */
 	alvium->mode.fmt = alvium_csi2_default_fmt;
diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
index 17f0bbbd1839..80066ac25047 100644
--- a/drivers/media/i2c/alvium-csi2.h
+++ b/drivers/media/i2c/alvium-csi2.h
@@ -443,7 +443,6 @@  struct alvium_dev {
 
 	struct alvium_mode mode;
 	struct v4l2_fract frame_interval;
-	u64 fr;
 
 	u8 h_sup_csi_lanes;
 	u64 link_freq;