2023-12-20 12:41:03

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 0/5] media: i2c: alvium: store frame interval in subdev

Hi All,
The goal of this series as suggested by LPinchart is to remove the following
data from alvium_dev struct:

- struct v4l2_fract frame_interval;
- u64 dft_fr;
- u64 min_fr;
- u64 max_fr;
- u64 fr;

And use instead the the newly added storage for frame interval in the subdev state.
Last patch fix req_fr check into alvium_s_frame_interval().

This series is made on top of the media_stage tree[1]:
- media: i2c: mt9m114: use fsleep() in place of udelay() (02d4e62ae245)
- [1] https://git.linuxtv.org/media_stage.git/log/

Tommaso Merciai (5):
media: i2c: alvium: removal of dft_fr, min_fr and max_fr
media: i2c: alvium: remove the fr field of the alvium_dev structure
media: i2c: alvium: inline set_frame_interval into s_frame_interval
media: i2c: alvium: store frame interval in subdev state
media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

drivers/media/i2c/alvium-csi2.c | 99 +++++++++------------------------
drivers/media/i2c/alvium-csi2.h | 5 --
2 files changed, 27 insertions(+), 77 deletions(-)

--
2.34.1



2023-12-20 12:41:35

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 1/5] media: i2c: alvium: removal of dft_fr, min_fr and max_fr

The dft_fr, min_fr and max_fr fields of the alvium_dev structure are
only used to pass results from alvium_get_frame_interval() to its
caller. Replace them with function parameters.

Signed-off-by: Tommaso Merciai <[email protected]>
Reviewed-by: Laurent Pinchart <[email protected]>
---
Changes Since v1:
- Fixed commit body as suggested by LPinchart
- Fixed alvium_get_frame_interval just return ret as suggested by LPinchart
- Removed unrelated chnages from the commit as suggested by LPinchart

Changes Since v2:
- Collected tag from LPinchart

drivers/media/i2c/alvium-csi2.c | 27 +++++++++------------------
drivers/media/i2c/alvium-csi2.h | 3 ---
2 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
index 34ff7fad3877..0dcd69bf9f92 100644
--- a/drivers/media/i2c/alvium-csi2.c
+++ b/drivers/media/i2c/alvium-csi2.c
@@ -1170,25 +1170,19 @@ static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
return 0;
}

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

alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_RW,
- &dft_fr, &ret);
+ dft_fr, &ret);
alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
- &min_fr, &ret);
+ min_fr, &ret);
alvium_read(alvium, REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
- &max_fr, &ret);
- if (ret)
- return ret;
-
- alvium->dft_fr = dft_fr;
- alvium->min_fr = min_fr;
- alvium->max_fr = max_fr;
+ max_fr, &ret);

- return 0;
+ return ret;
}

static int alvium_set_frame_rate(struct alvium_dev *alvium)
@@ -1670,21 +1664,18 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
struct v4l2_subdev_frame_interval *fi)
{
struct device *dev = &alvium->i2c_client->dev;
- u64 req_fr, min_fr, max_fr;
+ u64 req_fr, dft_fr, min_fr, max_fr;
int ret;

if (fi->interval.denominator == 0)
return -EINVAL;

- ret = alvium_get_frame_interval(alvium);
+ ret = alvium_get_frame_interval(alvium, &dft_fr, &min_fr, &max_fr);
if (ret) {
dev_err(dev, "Fail to get frame interval\n");
return ret;
}

- min_fr = alvium->min_fr;
- max_fr = alvium->max_fr;
-
dev_dbg(dev, "fi->interval.numerator = %d\n",
fi->interval.numerator);
dev_dbg(dev, "fi->interval.denominator = %d\n",
@@ -1694,7 +1685,7 @@ static int alvium_set_frame_interval(struct alvium_dev *alvium,
fi->interval.numerator);

if (req_fr >= max_fr && req_fr <= min_fr)
- req_fr = alvium->dft_fr;
+ req_fr = dft_fr;

alvium->fr = req_fr;
alvium->frame_interval.numerator = fi->interval.numerator;
diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
index b85a25169e79..17f0bbbd1839 100644
--- a/drivers/media/i2c/alvium-csi2.h
+++ b/drivers/media/i2c/alvium-csi2.h
@@ -443,9 +443,6 @@ struct alvium_dev {

struct alvium_mode mode;
struct v4l2_fract frame_interval;
- u64 dft_fr;
- u64 min_fr;
- u64 max_fr;
u64 fr;

u8 h_sup_csi_lanes;
--
2.34.1


2023-12-20 12:41:51

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 3/5] media: i2c: alvium: inline set_frame_interval into s_frame_interval

Inline alvium_set_frame_interval() into alvium_s_frame_interval().
The alvium_set_frame_interval() is called once only, by
alvium_s_frame_interval(). The latter is a thin wrapper around the
former. Inline the function in its caller to make the code more
readable.

Signed-off-by: Tommaso Merciai <[email protected]>
Reviewed-by: Laurent Pinchart <[email protected]>
---
Changes Since v1:
- Now this commit is the 3/4 of the series
- Fixed commit body as suggested by LPinchart

Changes Since v2:
- Removed req_fr initializzation as suggested by LPinchart
- Collected tag from LPinchart

drivers/media/i2c/alvium-csi2.c | 52 ++++++++++++---------------------
1 file changed, 19 insertions(+), 33 deletions(-)

diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
index 994372030be9..ef137e6a2811 100644
--- a/drivers/media/i2c/alvium-csi2.c
+++ b/drivers/media/i2c/alvium-csi2.c
@@ -1660,14 +1660,25 @@ static int alvium_g_frame_interval(struct v4l2_subdev *sd,
return 0;
}

-static int alvium_set_frame_interval(struct alvium_dev *alvium,
- struct v4l2_subdev_frame_interval *fi,
- u64 *req_fr)
+static int alvium_s_frame_interval(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *sd_state,
+ struct v4l2_subdev_frame_interval *fi)
{
+ struct alvium_dev *alvium = sd_to_alvium(sd);
struct device *dev = &alvium->i2c_client->dev;
- u64 dft_fr, min_fr, max_fr;
+ u64 req_fr, dft_fr, min_fr, max_fr;
int ret;

+ /*
+ * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
+ * subdev active state API.
+ */
+ if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
+ return -EINVAL;
+
+ if (alvium->streaming)
+ return -EBUSY;
+
if (fi->interval.denominator == 0)
return -EINVAL;

@@ -1682,41 +1693,16 @@ 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->frame_interval.numerator = fi->interval.numerator;
alvium->frame_interval.denominator = fi->interval.denominator;

- return 0;
-}
-
-static int alvium_s_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *sd_state,
- struct v4l2_subdev_frame_interval *fi)
-{
- struct alvium_dev *alvium = sd_to_alvium(sd);
- u64 req_fr;
- int ret;
-
- /*
- * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
- * subdev active state API.
- */
- if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
- return -EINVAL;
-
- if (alvium->streaming)
- return -EBUSY;
-
- ret = alvium_set_frame_interval(alvium, fi, &req_fr);
- if (!ret)
- ret = alvium_set_frame_rate(alvium, req_fr);
-
- return ret;
+ return alvium_set_frame_rate(alvium, req_fr);
}

static int alvium_enum_mbus_code(struct v4l2_subdev *sd,
--
2.34.1


2023-12-20 12:42:13

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 4/5] media: i2c: alvium: store frame interval in subdev state

Use the newly added storage for frame interval in the subdev state to
simplify the driver.

Signed-off-by: Tommaso Merciai <[email protected]>
Reviewed-by: Laurent Pinchart <[email protected]>
---
Changes Since v1:
- Removed FIXME comment and active state check into alvium_s_frame_interval
as suggested by LPinchart.
- Fixed call alvium_set_frame_rate() only for active state into alvium_s_frame_interval
as suggested by LPinchart.

Changes Since v2:
- Collected tag from LPinchart

drivers/media/i2c/alvium-csi2.c | 48 +++++++++++----------------------
drivers/media/i2c/alvium-csi2.h | 1 -
2 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
index ef137e6a2811..240bf991105e 100644
--- a/drivers/media/i2c/alvium-csi2.c
+++ b/drivers/media/i2c/alvium-csi2.c
@@ -1641,25 +1641,6 @@ static int alvium_hw_init(struct alvium_dev *alvium)
}

/* --------------- Subdev Operations --------------- */
-
-static int alvium_g_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *sd_state,
- struct v4l2_subdev_frame_interval *fi)
-{
- struct alvium_dev *alvium = sd_to_alvium(sd);
-
- /*
- * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
- * subdev active state API.
- */
- if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
- return -EINVAL;
-
- fi->interval = alvium->frame_interval;
-
- return 0;
-}
-
static int alvium_s_frame_interval(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_frame_interval *fi)
@@ -1667,15 +1648,9 @@ 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;
+ struct v4l2_fract *interval;
int ret;

- /*
- * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
- * subdev active state API.
- */
- if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
- return -EINVAL;
-
if (alvium->streaming)
return -EBUSY;

@@ -1699,8 +1674,13 @@ static int alvium_s_frame_interval(struct v4l2_subdev *sd,
if (req_fr >= max_fr && req_fr <= min_fr)
req_fr = dft_fr;

- alvium->frame_interval.numerator = fi->interval.numerator;
- alvium->frame_interval.denominator = fi->interval.denominator;
+ interval = v4l2_subdev_state_get_interval(sd_state, 0);
+
+ interval->numerator = fi->interval.numerator;
+ interval->denominator = fi->interval.denominator;
+
+ if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
+ return 0;

return alvium_set_frame_rate(alvium, req_fr);
}
@@ -1850,6 +1830,7 @@ static int alvium_init_state(struct v4l2_subdev *sd,
{
struct alvium_dev *alvium = sd_to_alvium(sd);
struct alvium_mode *mode = &alvium->mode;
+ struct v4l2_fract *interval;
struct v4l2_subdev_format sd_fmt = {
.which = V4L2_SUBDEV_FORMAT_TRY,
.format = alvium_csi2_default_fmt,
@@ -1867,6 +1848,11 @@ static int alvium_init_state(struct v4l2_subdev *sd,
*v4l2_subdev_state_get_crop(state, 0) = sd_crop.rect;
*v4l2_subdev_state_get_format(state, 0) = sd_fmt.format;

+ /* Setup initial frame interval*/
+ interval = v4l2_subdev_state_get_interval(state, 0);
+ interval->numerator = 1;
+ interval->denominator = ALVIUM_DEFAULT_FR_HZ;
+
return 0;
}

@@ -2236,7 +2222,7 @@ static const struct v4l2_subdev_pad_ops alvium_pad_ops = {
.set_fmt = alvium_set_fmt,
.get_selection = alvium_get_selection,
.set_selection = alvium_set_selection,
- .get_frame_interval = alvium_g_frame_interval,
+ .get_frame_interval = v4l2_subdev_get_frame_interval,
.set_frame_interval = alvium_s_frame_interval,
};

@@ -2257,10 +2243,6 @@ static int alvium_subdev_init(struct alvium_dev *alvium)
struct v4l2_subdev *sd = &alvium->sd;
int ret;

- /* Setup initial frame interval*/
- alvium->frame_interval.numerator = 1;
- alvium->frame_interval.denominator = ALVIUM_DEFAULT_FR_HZ;
-
/* Setup the initial mode */
alvium->mode.fmt = alvium_csi2_default_fmt;
alvium->mode.width = alvium_csi2_default_fmt.width;
diff --git a/drivers/media/i2c/alvium-csi2.h b/drivers/media/i2c/alvium-csi2.h
index 80066ac25047..9463f8604fbc 100644
--- a/drivers/media/i2c/alvium-csi2.h
+++ b/drivers/media/i2c/alvium-csi2.h
@@ -442,7 +442,6 @@ struct alvium_dev {
s32 inc_sharp;

struct alvium_mode mode;
- struct v4l2_fract frame_interval;

u8 h_sup_csi_lanes;
u64 link_freq;
--
2.34.1


2023-12-20 12:42:29

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 5/5] media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

Actually req_fr check into alvium_s_frame_interval() is wrong.
In particular req_fr can't be >=max and <= min at the same time.
Fix this using clamp and remove dft_fr parameter from
alvium_get_frame_interval() not more used.

Signed-off-by: Tommaso Merciai <[email protected]>
---
drivers/media/i2c/alvium-csi2.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
index 240bf991105e..01111a00902d 100644
--- a/drivers/media/i2c/alvium-csi2.c
+++ 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);

--
2.34.1


2023-12-20 12:51:37

by Tommaso Merciai

[permalink] [raw]
Subject: [PATCH v3 2/5] media: i2c: alvium: remove the fr field of the alvium_dev structure

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 it with a function parameter.

Signed-off-by: Tommaso Merciai <[email protected]>
Reviewed-by: Laurent Pinchart <[email protected]>
---
Changes Since v2:
- Removed req_fr initializzation as suggested by LPinchart
- Collected tag from LPinchart
- Fixed commit msg and body as suggested by LPinchart

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..994372030be9 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;
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;
--
2.34.1


2023-12-20 13:02:48

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

Hi Tommaso,

Thank you for the patch.

On Wed, Dec 20, 2023 at 01:40:23PM +0100, Tommaso Merciai wrote:
> Actually req_fr check into alvium_s_frame_interval() is wrong.
> In particular req_fr can't be >=max and <= min at the same time.
> Fix this using clamp and remove dft_fr parameter from
> alvium_get_frame_interval() not more used.

The commit message should have explained why clamping is better than
picking a default value, as that's a functional change. If you propose
an updated commit message in a reply, I think Sakari can update the
patch when applying the series to his tree, there's no need for a v4.

> Signed-off-by: Tommaso Merciai <[email protected]>

Reviewed-by: Laurent Pinchart <[email protected]>

> ---
> drivers/media/i2c/alvium-csi2.c | 12 ++++--------
> 1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> index 240bf991105e..01111a00902d 100644
> --- a/drivers/media/i2c/alvium-csi2.c
> +++ 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);
>

--
Regards,

Laurent Pinchart

2023-12-20 13:53:11

by Tommaso Merciai

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

Hi Laurent,

On Wed, Dec 20, 2023 at 03:02:36PM +0200, Laurent Pinchart wrote:
> Hi Tommaso,
>
> Thank you for the patch.
>
> On Wed, Dec 20, 2023 at 01:40:23PM +0100, Tommaso Merciai wrote:
> > Actually req_fr check into alvium_s_frame_interval() is wrong.
> > In particular req_fr can't be >=max and <= min at the same time.
> > Fix this using clamp and remove dft_fr parameter from
> > alvium_get_frame_interval() not more used.
>
> The commit message should have explained why clamping is better than
> picking a default value, as that's a functional change. If you propose
> an updated commit message in a reply, I think Sakari can update the
> patch when applying the series to his tree, there's no need for a v4.

What about:

Actually req_fr check into alvium_s_frame_interval() is wrong.
In particular req_fr can't be >=max and <= min at the same time.
Fix this using clamp and remove dft_fr parameter from
alvium_get_frame_interval() not more used.

Clamp function make sure that if the setted value exceeds the limits is
replaced with min_fr/max_fr instead of setting the value readed back
from the hw.

What do you think?

Thanks & Regards,
Tommaso

>
> > Signed-off-by: Tommaso Merciai <[email protected]>
>
> Reviewed-by: Laurent Pinchart <[email protected]>
>
> > ---
> > drivers/media/i2c/alvium-csi2.c | 12 ++++--------
> > 1 file changed, 4 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/media/i2c/alvium-csi2.c b/drivers/media/i2c/alvium-csi2.c
> > index 240bf991105e..01111a00902d 100644
> > --- a/drivers/media/i2c/alvium-csi2.c
> > +++ 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);
> >
>
> --
> Regards,
>
> Laurent Pinchart

2024-02-14 15:24:44

by Tommaso Merciai

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

Hi Sakari,
Sorry for delay.

On Wed, Feb 14, 2024 at 11:14:35AM +0000, Sakari Ailus wrote:
> Hi Tommaso,
>
> On Wed, Dec 20, 2023 at 02:52:53PM +0100, Tommaso Merciai wrote:
> > Hi Laurent,
> >
> > On Wed, Dec 20, 2023 at 03:02:36PM +0200, Laurent Pinchart wrote:
> > > Hi Tommaso,
> > >
> > > Thank you for the patch.
> > >
> > > On Wed, Dec 20, 2023 at 01:40:23PM +0100, Tommaso Merciai wrote:
> > > > Actually req_fr check into alvium_s_frame_interval() is wrong.
> > > > In particular req_fr can't be >=max and <= min at the same time.
> > > > Fix this using clamp and remove dft_fr parameter from
> > > > alvium_get_frame_interval() not more used.
> > >
> > > The commit message should have explained why clamping is better than
> > > picking a default value, as that's a functional change. If you propose
> > > an updated commit message in a reply, I think Sakari can update the
> > > patch when applying the series to his tree, there's no need for a v4.
> >
> > What about:
> >
> > Actually req_fr check into alvium_s_frame_interval() is wrong.
> > In particular req_fr can't be >=max and <= min at the same time.
> > Fix this using clamp and remove dft_fr parameter from
> > alvium_get_frame_interval() not more used.
> >
> > Clamp function make sure that if the setted value exceeds the limits is
> > replaced with min_fr/max_fr instead of setting the value readed back
> > from the hw.
> >
> > What do you think?
>
> I used this, hopefully it's ok:
>
> media: i2c: alvium: fix req_fr check in alvium_s_frame_interval()
>
> req_fr check in alvium_s_frame_interval() is incorrect. In particular
> req_fr can't be >=max and <= min at the same time. Ensure the requested
> frame rate remains within the supported range between min_fr and max_fr by
> clamping it.
>
> Also remove the unused dft_fr argument of alvium_get_frame_interval().

Looks good to me! :)
Thanks for this.

Regards,
Tommaso


>
> --
> Kind regards,
>
> Sakari Ailus

2024-02-14 16:31:24

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] media: i2c: alvium: fix req_fr check into alvium_s_frame_interval()

Hi Tommaso,

On Wed, Dec 20, 2023 at 02:52:53PM +0100, Tommaso Merciai wrote:
> Hi Laurent,
>
> On Wed, Dec 20, 2023 at 03:02:36PM +0200, Laurent Pinchart wrote:
> > Hi Tommaso,
> >
> > Thank you for the patch.
> >
> > On Wed, Dec 20, 2023 at 01:40:23PM +0100, Tommaso Merciai wrote:
> > > Actually req_fr check into alvium_s_frame_interval() is wrong.
> > > In particular req_fr can't be >=max and <= min at the same time.
> > > Fix this using clamp and remove dft_fr parameter from
> > > alvium_get_frame_interval() not more used.
> >
> > The commit message should have explained why clamping is better than
> > picking a default value, as that's a functional change. If you propose
> > an updated commit message in a reply, I think Sakari can update the
> > patch when applying the series to his tree, there's no need for a v4.
>
> What about:
>
> Actually req_fr check into alvium_s_frame_interval() is wrong.
> In particular req_fr can't be >=max and <= min at the same time.
> Fix this using clamp and remove dft_fr parameter from
> alvium_get_frame_interval() not more used.
>
> Clamp function make sure that if the setted value exceeds the limits is
> replaced with min_fr/max_fr instead of setting the value readed back
> from the hw.
>
> What do you think?

I used this, hopefully it's ok:

media: i2c: alvium: fix req_fr check in alvium_s_frame_interval()

req_fr check in alvium_s_frame_interval() is incorrect. In particular
req_fr can't be >=max and <= min at the same time. Ensure the requested
frame rate remains within the supported range between min_fr and max_fr by
clamping it.

Also remove the unused dft_fr argument of alvium_get_frame_interval().

--
Kind regards,

Sakari Ailus