293ff3dc96f7e02325738aa57d5afc638794e4de
[openwrt/staging/neocturne.git] /
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
5 in one place
6
7 Upstream commit 693b5cb598cc
8
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.
15
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>
20 ---
21 drivers/media/i2c/imx290.c | 43 +++++++++++++++++---------------------
22 1 file changed, 19 insertions(+), 24 deletions(-)
23
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
27 return 0;
28 }
29
30 -static u64 imx290_calc_pixel_rate(struct imx290 *imx290,
31 - const struct imx290_mode *mode)
32 -{
33 - s64 link_freq = imx290_link_freqs_ptr(imx290)[mode->link_freq_index];
34 - u64 pixel_rate;
35 -
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);
39 - return pixel_rate;
40 -}
41 -
42 /* ----------------------------------------------------------------------------
43 * Controls
44 */
45 @@ -632,6 +620,8 @@ static void imx290_ctrl_update(struct im
46 {
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];
50 + u64 pixel_rate;
51
52 /*
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)
56 return;
57
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);
61 +
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);
66
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)
71 {
72 struct v4l2_fwnode_device_properties props;
73 - unsigned int blank;
74 - u64 pixel_rate;
75 int ret;
76
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);
81
82 + /*
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().
86 + */
87 imx290->link_freq =
88 v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
89 V4L2_CID_LINK_FREQ,
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;
93
94 - pixel_rate = imx290_calc_pixel_rate(imx290, imx290->current_mode);
95 imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
96 V4L2_CID_PIXEL_RATE,
97 - 1, INT_MAX, 1, pixel_rate);
98 + 1, INT_MAX, 1, 1);
99
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);
104
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,
108 - blank);
109 + V4L2_CID_HBLANK, 1, 1, 1, 1);
110 if (imx290->hblank)
111 imx290->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
112
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,
116 - blank);
117 + V4L2_CID_VBLANK, 1, 1, 1, 1);
118 if (imx290->vblank)
119 imx290->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
120
121 @@ -725,6 +716,10 @@ static int imx290_ctrl_init(struct imx29
122 return ret;
123 }
124
125 + mutex_lock(imx290->ctrls.lock);
126 + imx290_ctrl_update(imx290, imx290->current_mode);
127 + mutex_unlock(imx290->ctrls.lock);
128 +
129 return 0;
130 }
131