Skip to content

Commit 98e0500

Browse files
Mani-Sadhasivammchehab
authored andcommitted
media: i2c: imx290: Add configurable link frequency and pixel rate
IMX290 operates with multiple link frequency and pixel rate combinations. The initial driver used a single setting for both but since we now have the lane count support in place, let's add configurable link frequency and pixel rate. Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
1 parent 97589ad commit 98e0500

File tree

1 file changed

+109
-39
lines changed

1 file changed

+109
-39
lines changed

drivers/media/i2c/imx290.c

Lines changed: 109 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#define IMX290_PHY_LANE_NUM 0x3407
3333
#define IMX290_CSI_LANE_MODE 0x3443
3434

35-
#define IMX290_DEFAULT_LINK_FREQ 445500000
36-
3735
static const char * const imx290_supply_name[] = {
3836
"vdda",
3937
"vddd",
@@ -51,8 +49,7 @@ struct imx290_mode {
5149
u32 width;
5250
u32 height;
5351
u32 hmax;
54-
u32 pixel_rate;
55-
u32 link_freq_index;
52+
u8 link_freq_index;
5653

5754
const struct imx290_regval *data;
5855
u32 data_size;
@@ -243,29 +240,54 @@ static const struct imx290_regval imx290_10bit_settings[] = {
243240
};
244241

245242
/* supported link frequencies */
246-
static const s64 imx290_link_freq[] = {
247-
IMX290_DEFAULT_LINK_FREQ,
243+
#define FREQ_INDEX_1080P 0
244+
#define FREQ_INDEX_720P 1
245+
static const s64 imx290_link_freq_2lanes[] = {
246+
[FREQ_INDEX_1080P] = 445500000,
247+
[FREQ_INDEX_720P] = 297000000,
248+
};
249+
static const s64 imx290_link_freq_4lanes[] = {
250+
[FREQ_INDEX_1080P] = 222750000,
251+
[FREQ_INDEX_720P] = 148500000,
248252
};
249253

254+
/*
255+
* In this function and in the similar ones below We rely on imx290_probe()
256+
* to ensure that nlanes is either 2 or 4.
257+
*/
258+
static inline const s64 *imx290_link_freqs_ptr(const struct imx290 *imx290)
259+
{
260+
if (imx290->nlanes == 2)
261+
return imx290_link_freq_2lanes;
262+
else
263+
return imx290_link_freq_4lanes;
264+
}
265+
266+
static inline int imx290_link_freqs_num(const struct imx290 *imx290)
267+
{
268+
if (imx290->nlanes == 2)
269+
return ARRAY_SIZE(imx290_link_freq_2lanes);
270+
else
271+
return ARRAY_SIZE(imx290_link_freq_4lanes);
272+
}
273+
250274
/* Mode configs */
251275
static const struct imx290_mode imx290_modes_2lanes[] = {
252276
{
253277
.width = 1920,
254278
.height = 1080,
255279
.hmax = 0x1130,
280+
.link_freq_index = FREQ_INDEX_1080P,
256281
.data = imx290_1080p_settings,
257282
.data_size = ARRAY_SIZE(imx290_1080p_settings),
258-
.pixel_rate = 178200000,
259-
.link_freq_index = 0,
260283
},
261284
{
262285
.width = 1280,
263286
.height = 720,
264287
.hmax = 0x19c8,
288+
.link_freq_index = FREQ_INDEX_720P,
265289
.data = imx290_720p_settings,
266290
.data_size = ARRAY_SIZE(imx290_720p_settings),
267-
.pixel_rate = 178200000,
268-
.link_freq_index = 0,
269291
},
270292
};
271293

@@ -274,25 +296,22 @@ static const struct imx290_mode imx290_modes_4lanes[] = {
274296
.width = 1920,
275297
.height = 1080,
276298
.hmax = 0x0898,
299+
.link_freq_index = FREQ_INDEX_1080P,
277300
.data = imx290_1080p_settings,
278301
.data_size = ARRAY_SIZE(imx290_1080p_settings),
279-
.pixel_rate = 178200000,
280-
.link_freq_index = 0,
281302
},
282303
{
283304
.width = 1280,
284305
.height = 720,
285306
.hmax = 0x0ce4,
307+
.link_freq_index = FREQ_INDEX_720P,
286308
.data = imx290_720p_settings,
287309
.data_size = ARRAY_SIZE(imx290_720p_settings),
288-
.pixel_rate = 178200000,
289-
.link_freq_index = 0,
290310
},
291311
};
292312

293313
static inline const struct imx290_mode *imx290_modes_ptr(const struct imx290 *imx290)
294314
{
295-
/* We rely on imx290_probe() to ensure that nlanes is either 2 or 4 */
296315
if (imx290->nlanes == 2)
297316
return imx290_modes_2lanes;
298317
else
@@ -477,6 +496,30 @@ static int imx290_get_fmt(struct v4l2_subdev *sd,
477496
return 0;
478497
}
479498

499+
static inline u8 imx290_get_link_freq_index(struct imx290 *imx290)
500+
{
501+
return imx290->current_mode->link_freq_index;
502+
}
503+
504+
static s64 imx290_get_link_freq(struct imx290 *imx290)
505+
{
506+
u8 index = imx290_get_link_freq_index(imx290);
507+
508+
return *(imx290_link_freqs_ptr(imx290) + index);
509+
}
510+
511+
static u64 imx290_calc_pixel_rate(struct imx290 *imx290)
512+
{
513+
s64 link_freq = imx290_get_link_freq(imx290);
514+
u8 nlanes = imx290->nlanes;
515+
u64 pixel_rate;
516+
517+
/* pixel rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
518+
pixel_rate = link_freq * 2 * nlanes;
519+
do_div(pixel_rate, 10);
520+
return pixel_rate;
521+
}
522+
480523
static int imx290_set_fmt(struct v4l2_subdev *sd,
481524
struct v4l2_subdev_pad_config *cfg,
482525
struct v4l2_subdev_format *fmt)
@@ -509,10 +552,14 @@ static int imx290_set_fmt(struct v4l2_subdev *sd,
509552
format = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
510553
} else {
511554
format = &imx290->current_format;
512-
__v4l2_ctrl_s_ctrl(imx290->link_freq, mode->link_freq_index);
513-
__v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate, mode->pixel_rate);
514-
515555
imx290->current_mode = mode;
556+
557+
if (imx290->link_freq)
558+
__v4l2_ctrl_s_ctrl(imx290->link_freq,
559+
imx290_get_link_freq_index(imx290));
560+
if (imx290->pixel_rate)
561+
__v4l2_ctrl_s_ctrl_int64(imx290->pixel_rate,
562+
imx290_calc_pixel_rate(imx290));
516563
}
517564

518565
*format = fmt->format;
@@ -536,12 +583,11 @@ static int imx290_entity_init_cfg(struct v4l2_subdev *subdev,
536583
return 0;
537584
}
538585

539-
static int imx290_write_current_format(struct imx290 *imx290,
540-
struct v4l2_mbus_framefmt *format)
586+
static int imx290_write_current_format(struct imx290 *imx290)
541587
{
542588
int ret;
543589

544-
switch (format->code) {
590+
switch (imx290->current_format.code) {
545591
case MEDIA_BUS_FMT_SRGGB10_1X10:
546592
ret = imx290_set_register_array(imx290, imx290_10bit_settings,
547593
ARRAY_SIZE(
@@ -592,8 +638,8 @@ static int imx290_start_streaming(struct imx290 *imx290)
592638
return ret;
593639
}
594640

595-
/* Set current frame format */
596-
ret = imx290_write_current_format(imx290, &imx290->current_format);
641+
/* Apply the register values related to current frame format */
642+
ret = imx290_write_current_format(imx290);
597643
if (ret < 0) {
598644
dev_err(imx290->dev, "Could not set frame format\n");
599645
return ret;
@@ -776,13 +822,34 @@ static const struct media_entity_operations imx290_subdev_entity_ops = {
776822
.link_validate = v4l2_subdev_link_validate,
777823
};
778824

825+
/*
826+
* Returns 0 if all link frequencies used by the driver for the given number
827+
* of MIPI data lanes are mentioned in the device tree, or the value of the
828+
* first missing frequency otherwise.
829+
*/
830+
static s64 imx290_check_link_freqs(const struct imx290 *imx290)
831+
{
832+
int i, j;
833+
const s64 *freqs = imx290_link_freqs_ptr(imx290);
834+
int freqs_count = imx290_link_freqs_num(imx290);
835+
836+
for (i = 0; i < freqs_count; i++) {
837+
for (j = 0; j < imx290->ep.nr_of_link_frequencies; j++)
838+
if (freqs[i] == imx290->ep.link_frequencies[j])
839+
break;
840+
if (j == imx290->ep.nr_of_link_frequencies)
841+
return freqs[i];
842+
}
843+
return 0;
844+
}
845+
779846
static int imx290_probe(struct i2c_client *client)
780847
{
781848
struct device *dev = &client->dev;
782849
struct fwnode_handle *endpoint;
783850
struct imx290 *imx290;
784851
u32 xclk_freq;
785-
u32 default_pixel_rate;
852+
s64 fq;
786853
int ret;
787854

788855
imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
@@ -825,8 +892,10 @@ static int imx290_probe(struct i2c_client *client)
825892
goto free_err;
826893
}
827894

828-
if (imx290->ep.link_frequencies[0] != IMX290_DEFAULT_LINK_FREQ) {
829-
dev_err(dev, "Unsupported link frequency\n");
895+
/* Check that link frequences for all the modes are in device tree */
896+
fq = imx290_check_link_freqs(imx290);
897+
if (fq) {
898+
dev_err(dev, "Link frequency of %lld is not supported\n", fq);
830899
ret = -EINVAL;
831900
goto free_err;
832901
}
@@ -883,26 +952,30 @@ static int imx290_probe(struct i2c_client *client)
883952

884953
mutex_init(&imx290->lock);
885954

955+
/*
956+
* Initialize the frame format. In particular, imx290->current_mode
957+
* and imx290->bpp are set to defaults: imx290_calc_pixel_rate() call
958+
* below relies on these fields.
959+
*/
960+
imx290_entity_init_cfg(&imx290->sd, NULL);
961+
886962
v4l2_ctrl_handler_init(&imx290->ctrls, 3);
887963

888964
v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
889965
V4L2_CID_GAIN, 0, 72, 1, 0);
966+
890967
imx290->link_freq =
891-
v4l2_ctrl_new_int_menu(&imx290->ctrls,
892-
&imx290_ctrl_ops,
968+
v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
893969
V4L2_CID_LINK_FREQ,
894-
ARRAY_SIZE(imx290_link_freq) - 1,
895-
0, imx290_link_freq);
970+
imx290_link_freqs_num(imx290) - 1, 0,
971+
imx290_link_freqs_ptr(imx290));
896972
if (imx290->link_freq)
897973
imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
898974

899-
default_pixel_rate = imx290->nlanes == 2 ?
900-
imx290_modes_2lanes[0].pixel_rate :
901-
imx290_modes_4lanes[0].pixel_rate;
902975
imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
903-
V4L2_CID_PIXEL_RATE, 1,
904-
INT_MAX, 1,
905-
default_pixel_rate);
976+
V4L2_CID_PIXEL_RATE,
977+
1, INT_MAX, 1,
978+
imx290_calc_pixel_rate(imx290));
906979

907980
imx290->sd.ctrl_handler = &imx290->ctrls;
908981

@@ -926,9 +999,6 @@ static int imx290_probe(struct i2c_client *client)
926999
goto free_ctrl;
9271000
}
9281001

929-
/* Initialize the frame format (this also sets imx290->current_mode) */
930-
imx290_entity_init_cfg(&imx290->sd, NULL);
931-
9321002
ret = v4l2_async_register_subdev(&imx290->sd);
9331003
if (ret < 0) {
9341004
dev_err(dev, "Could not register v4l2 device\n");

0 commit comments

Comments
 (0)