1 From 317b043056ef595bf7d4af2c807946afb405c117 Mon Sep 17 00:00:00 2001
2 From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 Date: Mon, 16 Jan 2023 15:44:43 +0100
4 Subject: [PATCH] media: i2c: imx290: Compute pixel rate and blanking
7 Upstream commit 693b5cb598cc
9 The hblank, vblank, pixel rate and link frequency values and limits are
10 currently computed when creating controls, in imx290_ctrl_init(), and
11 updated in imx290_ctrl_update(). This duplicates the logic in different
12 places. Simplify the code by setting the control values and limits to
13 hardcoded values when creating the controls, and call
14 imx290_ctrl_update() to then update them.
16 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
17 Acked-by: Alexander Stein <alexander.stein@ew.tq-group.com>
18 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
19 Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
21 drivers/media/i2c/imx290.c | 43 +++++++++++++++++---------------------
22 1 file changed, 19 insertions(+), 24 deletions(-)
24 --- a/drivers/media/i2c/imx290.c
25 +++ b/drivers/media/i2c/imx290.c
26 @@ -547,18 +547,6 @@ static int imx290_write_current_format(s
30 -static u64 imx290_calc_pixel_rate(struct imx290 *imx290,
31 - const struct imx290_mode *mode)
33 - s64 link_freq = imx290_link_freqs_ptr(imx290)[mode->link_freq_index];
36 - /* pixel rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
37 - pixel_rate = link_freq * 2 * imx290->nlanes;
38 - do_div(pixel_rate, imx290->bpp);
42 /* ----------------------------------------------------------------------------
45 @@ -632,6 +620,8 @@ static void imx290_ctrl_update(struct im
47 unsigned int hblank = mode->hmax - mode->width;
48 unsigned int vblank = IMX290_VMAX_DEFAULT - mode->height;
49 + s64 link_freq = imx290_link_freqs_ptr(imx290)[mode->link_freq_index];
53 * This function may be called from imx290_set_fmt() before controls
54 @@ -640,9 +630,12 @@ static void imx290_ctrl_update(struct im
55 if (!imx290->ctrls.lock)
58 + /* pixel rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
59 + pixel_rate = link_freq * 2 * imx290->nlanes;
60 + do_div(pixel_rate, imx290->bpp);
62 __v4l2_ctrl_s_ctrl(imx290->link_freq, mode->link_freq_index);
63 - __v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate,
64 - imx290_calc_pixel_rate(imx290, mode));
65 + __v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate, pixel_rate);
67 __v4l2_ctrl_modify_range(imx290->hblank, hblank, hblank, 1, hblank);
68 __v4l2_ctrl_modify_range(imx290->vblank, vblank, vblank, 1, vblank);
69 @@ -651,8 +644,6 @@ static void imx290_ctrl_update(struct im
70 static int imx290_ctrl_init(struct imx290 *imx290)
72 struct v4l2_fwnode_device_properties props;
77 ret = v4l2_fwnode_device_parse(imx290->dev, &props);
78 @@ -682,6 +673,11 @@ static int imx290_ctrl_init(struct imx29
79 V4L2_CID_EXPOSURE, 1, IMX290_VMAX_DEFAULT - 2, 1,
80 IMX290_VMAX_DEFAULT - 2);
83 + * Set the link frequency, pixel rate, horizontal blanking and vertical
84 + * blanking to hardcoded values, they will be updated by
85 + * imx290_ctrl_update().
88 v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
90 @@ -690,27 +686,22 @@ static int imx290_ctrl_init(struct imx29
91 if (imx290->link_freq)
92 imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
94 - pixel_rate = imx290_calc_pixel_rate(imx290, imx290->current_mode);
95 imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
97 - 1, INT_MAX, 1, pixel_rate);
100 v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
101 V4L2_CID_TEST_PATTERN,
102 ARRAY_SIZE(imx290_test_pattern_menu) - 1,
103 0, 0, imx290_test_pattern_menu);
105 - blank = imx290->current_mode->hmax - imx290->current_mode->width;
106 imx290->hblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
107 - V4L2_CID_HBLANK, blank, blank, 1,
109 + V4L2_CID_HBLANK, 1, 1, 1, 1);
111 imx290->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
113 - blank = IMX290_VMAX_DEFAULT - imx290->current_mode->height;
114 imx290->vblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
115 - V4L2_CID_VBLANK, blank, blank, 1,
117 + V4L2_CID_VBLANK, 1, 1, 1, 1);
119 imx290->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
121 @@ -725,6 +716,10 @@ static int imx290_ctrl_init(struct imx29
125 + mutex_lock(imx290->ctrls.lock);
126 + imx290_ctrl_update(imx290, imx290->current_mode);
127 + mutex_unlock(imx290->ctrls.lock);