Skip to content

Commit 76edc05

Browse files
author
André Apitzsch
committed
media: i2c: imx214: Make use of CCS PLL calculator
Calculate PLL parameters based on clock frequency and link frequency. Signed-off-by: André Apitzsch <git@apitzsch.eu>
1 parent 94bdaee commit 76edc05

File tree

2 files changed

+170
-39
lines changed

2 files changed

+170
-39
lines changed

drivers/media/i2c/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ config VIDEO_IMX214
141141
depends on GPIOLIB
142142
select REGMAP_I2C
143143
select V4L2_CCI_I2C
144+
select VIDEO_CCS_PLL
144145
help
145146
This is a Video4Linux2 sensor driver for the Sony
146147
IMX214 camera.

drivers/media/i2c/imx214.c

Lines changed: 169 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <media/v4l2-fwnode.h>
2121
#include <media/v4l2-subdev.h>
2222

23+
#include "ccs-pll.h"
24+
2325
/* Chip ID */
2426
#define IMX214_REG_CHIP_ID CCI_REG16(0x0016)
2527
#define IMX214_CHIP_ID 0x0214
@@ -34,7 +36,6 @@
3436
#define IMX214_DEFAULT_LINK_FREQ 600000000
3537
/* Keep wrong link frequency for backward compatibility */
3638
#define IMX214_DEFAULT_LINK_FREQ_LEGACY 480000000
37-
#define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10)
3839
#define IMX214_FPS 30
3940

4041
/* V-TIMING internal */
@@ -84,6 +85,7 @@
8485
#define IMX214_CSI_DATA_FORMAT_RAW10 0x0A0A
8586
#define IMX214_CSI_DATA_FORMAT_COMP6 0x0A06
8687
#define IMX214_CSI_DATA_FORMAT_COMP8 0x0A08
88+
#define IMX214_BITS_PER_PIXEL_MASK 0xFF
8789

8890
#define IMX214_REG_CSI_LANE_MODE CCI_REG8(0x0114)
8991
#define IMX214_CSI_2_LANE_MODE 1
@@ -249,6 +251,10 @@ struct imx214 {
249251
struct clk *xclk;
250252
struct regmap *regmap;
251253

254+
struct ccs_pll pll;
255+
256+
struct v4l2_fwnode_endpoint bus_cfg;
257+
252258
struct v4l2_subdev sd;
253259
struct media_pad pad;
254260

@@ -758,16 +764,22 @@ static int imx214_configure_pll(struct imx214 *imx214)
758764
{
759765
int ret = 0;
760766

761-
cci_write(imx214->regmap, IMX214_REG_VTPXCK_DIV, 5, &ret);
762-
cci_write(imx214->regmap, IMX214_REG_VTSYCK_DIV, 2, &ret);
763-
cci_write(imx214->regmap, IMX214_REG_PREPLLCK_VT_DIV, 3, &ret);
764-
cci_write(imx214->regmap, IMX214_REG_PLL_VT_MPY, 150, &ret);
765-
cci_write(imx214->regmap, IMX214_REG_OPPXCK_DIV, 10, &ret);
766-
cci_write(imx214->regmap, IMX214_REG_OPSYCK_DIV, 1, &ret);
767+
cci_write(imx214->regmap, IMX214_REG_VTPXCK_DIV,
768+
imx214->pll.vt_bk.pix_clk_div, &ret);
769+
cci_write(imx214->regmap, IMX214_REG_VTSYCK_DIV,
770+
imx214->pll.vt_bk.sys_clk_div, &ret);
771+
cci_write(imx214->regmap, IMX214_REG_PREPLLCK_VT_DIV,
772+
imx214->pll.vt_fr.pre_pll_clk_div, &ret);
773+
cci_write(imx214->regmap, IMX214_REG_PLL_VT_MPY,
774+
imx214->pll.vt_fr.pll_multiplier, &ret);
775+
cci_write(imx214->regmap, IMX214_REG_OPPXCK_DIV,
776+
imx214->pll.op_bk.pix_clk_div, &ret);
777+
cci_write(imx214->regmap, IMX214_REG_OPSYCK_DIV,
778+
imx214->pll.op_bk.sys_clk_div, &ret);
767779
cci_write(imx214->regmap, IMX214_REG_PLL_MULT_DRIV,
768780
IMX214_PLL_SINGLE, &ret);
769781
cci_write(imx214->regmap, IMX214_REG_EXCK_FREQ,
770-
IMX214_EXCK_FREQ(IMX214_DEFAULT_CLK_FREQ / 1000000), &ret);
782+
IMX214_EXCK_FREQ(imx214->pll.ext_clk_freq_hz / 1000000), &ret);
771783

772784
return ret;
773785
}
@@ -872,9 +884,6 @@ static const struct v4l2_ctrl_ops imx214_ctrl_ops = {
872884

873885
static int imx214_ctrls_init(struct imx214 *imx214)
874886
{
875-
static const s64 link_freq[] = {
876-
IMX214_DEFAULT_LINK_FREQ
877-
};
878887
static const struct v4l2_area unit_size = {
879888
.width = 1120,
880889
.height = 1120,
@@ -895,15 +904,14 @@ static int imx214_ctrls_init(struct imx214 *imx214)
895904
if (ret)
896905
return ret;
897906

898-
imx214->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, NULL,
899-
V4L2_CID_PIXEL_RATE, 0,
900-
IMX214_DEFAULT_PIXEL_RATE, 1,
901-
IMX214_DEFAULT_PIXEL_RATE);
907+
imx214->pixel_rate =
908+
v4l2_ctrl_new_std(ctrl_hdlr, NULL, V4L2_CID_PIXEL_RATE, 1,
909+
INT_MAX, 1, 1);
902910

903911
imx214->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, NULL,
904912
V4L2_CID_LINK_FREQ,
905-
ARRAY_SIZE(link_freq) - 1,
906-
0, link_freq);
913+
imx214->bus_cfg.nr_of_link_frequencies - 1,
914+
0, imx214->bus_cfg.link_frequencies);
907915
if (imx214->link_freq)
908916
imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
909917

@@ -1004,6 +1012,7 @@ static int imx214_start_streaming(struct imx214 *imx214)
10041012
const struct v4l2_mbus_framefmt *fmt;
10051013
struct v4l2_subdev_state *state;
10061014
const struct imx214_mode *mode;
1015+
int bit_rate_mbps;
10071016
int ret;
10081017

10091018
ret = cci_multi_reg_write(imx214->regmap, mode_table_common,
@@ -1019,8 +1028,10 @@ static int imx214_start_streaming(struct imx214 *imx214)
10191028
return ret;
10201029
}
10211030

1031+
bit_rate_mbps = (imx214->pll.pixel_rate_pixel_array / 1000000)
1032+
* imx214->pll.bits_per_pixel;
10221033
ret = cci_write(imx214->regmap, IMX214_REG_REQ_LINK_BIT_RATE,
1023-
IMX214_LINK_BIT_RATE_MBPS(4800), NULL);
1034+
IMX214_LINK_BIT_RATE_MBPS(bit_rate_mbps), NULL);
10241035
if (ret) {
10251036
dev_err(imx214->dev, "failed to configure link bit rate\n");
10261037
return ret;
@@ -1103,6 +1114,108 @@ static int imx214_s_stream(struct v4l2_subdev *subdev, int enable)
11031114
return ret;
11041115
}
11051116

1117+
static int imx214_pll_calculate(struct imx214 *imx214, struct ccs_pll *pll,
1118+
unsigned int link_freq)
1119+
{
1120+
struct ccs_pll_limits limits = {
1121+
.min_ext_clk_freq_hz = 6000000,
1122+
.max_ext_clk_freq_hz = 27000000,
1123+
1124+
.vt_fr = {
1125+
.min_pre_pll_clk_div = 1,
1126+
.max_pre_pll_clk_div = 15,
1127+
/* min_pll_op_clk_freq_hz / max_pll_multiplier */
1128+
.min_pll_ip_clk_freq_hz = 281667,
1129+
/* max_pll_op_clk_freq_hz / min_pll_multiplier */
1130+
.max_pll_ip_clk_freq_hz = 100000000,
1131+
.min_pll_multiplier = 12,
1132+
.max_pll_multiplier = 1200,
1133+
.min_pll_op_clk_freq_hz = 338000000,
1134+
.max_pll_op_clk_freq_hz = 1200000000,
1135+
},
1136+
.vt_bk = {
1137+
.min_sys_clk_div = 2,
1138+
.max_sys_clk_div = 4,
1139+
.min_pix_clk_div = 5,
1140+
.max_pix_clk_div = 10,
1141+
.min_pix_clk_freq_hz = 30000000,
1142+
.max_pix_clk_freq_hz = 120000000,
1143+
},
1144+
.op_bk = {
1145+
.min_sys_clk_div = 1,
1146+
.max_sys_clk_div = 2,
1147+
.min_pix_clk_div = 6,
1148+
.max_pix_clk_div = 10,
1149+
.min_pix_clk_freq_hz = 30000000,
1150+
.max_pix_clk_freq_hz = 120000000,
1151+
},
1152+
1153+
.min_line_length_pck_bin = IMX214_PPL_DEFAULT,
1154+
.min_line_length_pck = IMX214_PPL_DEFAULT,
1155+
};
1156+
unsigned int num_lanes = imx214->bus_cfg.bus.mipi_csi2.num_data_lanes;
1157+
int ret;
1158+
1159+
/*
1160+
* There are no documented constraints on the sys clock frequency, for
1161+
* either branch. Recover them based on the PLL output clock frequency
1162+
* and sys_clk_div limits on one hand, and the pix clock frequency and
1163+
* the pix_clk_div limits on the other hand.
1164+
*/
1165+
limits.vt_bk.min_sys_clk_freq_hz =
1166+
max(limits.vt_fr.min_pll_op_clk_freq_hz / limits.vt_bk.max_sys_clk_div,
1167+
limits.vt_bk.min_pix_clk_freq_hz * limits.vt_bk.min_pix_clk_div);
1168+
limits.vt_bk.max_sys_clk_freq_hz =
1169+
min(limits.vt_fr.max_pll_op_clk_freq_hz / limits.vt_bk.min_sys_clk_div,
1170+
limits.vt_bk.max_pix_clk_freq_hz * limits.vt_bk.max_pix_clk_div);
1171+
1172+
limits.op_bk.min_sys_clk_freq_hz =
1173+
max(limits.vt_fr.min_pll_op_clk_freq_hz / limits.op_bk.max_sys_clk_div,
1174+
limits.op_bk.min_pix_clk_freq_hz * limits.op_bk.min_pix_clk_div);
1175+
limits.op_bk.max_sys_clk_freq_hz =
1176+
min(limits.vt_fr.max_pll_op_clk_freq_hz / limits.op_bk.min_sys_clk_div,
1177+
limits.op_bk.max_pix_clk_freq_hz * limits.op_bk.max_pix_clk_div);
1178+
1179+
memset(pll, 0, sizeof(*pll));
1180+
1181+
pll->bus_type = CCS_PLL_BUS_TYPE_CSI2_DPHY;
1182+
pll->op_lanes = num_lanes;
1183+
pll->vt_lanes = num_lanes;
1184+
pll->csi2.lanes = num_lanes;
1185+
1186+
pll->binning_horizontal = 1;
1187+
pll->binning_vertical = 1;
1188+
pll->scale_m = 1;
1189+
pll->scale_n = 1;
1190+
pll->bits_per_pixel =
1191+
IMX214_CSI_DATA_FORMAT_RAW10 & IMX214_BITS_PER_PIXEL_MASK;
1192+
pll->flags = CCS_PLL_FLAG_LANE_SPEED_MODEL;
1193+
pll->link_freq = link_freq;
1194+
pll->ext_clk_freq_hz = clk_get_rate(imx214->xclk);
1195+
1196+
ret = ccs_pll_calculate(imx214->dev, &limits, pll);
1197+
1198+
return ret;
1199+
}
1200+
1201+
static int imx214_pll_update(struct imx214 *imx214)
1202+
{
1203+
u64 link_freq;
1204+
int ret;
1205+
1206+
link_freq = imx214->bus_cfg.link_frequencies[imx214->link_freq->val];
1207+
ret = imx214_pll_calculate(imx214, &imx214->pll, link_freq);
1208+
if (ret) {
1209+
dev_err(imx214->dev, "PLL calculations failed: %d\n", ret);
1210+
return ret;
1211+
}
1212+
1213+
__v4l2_ctrl_s_ctrl_int64(imx214->pixel_rate,
1214+
imx214->pll.pixel_rate_pixel_array);
1215+
1216+
return 0;
1217+
}
1218+
11061219
static int imx214_get_frame_interval(struct v4l2_subdev *subdev,
11071220
struct v4l2_subdev_state *sd_state,
11081221
struct v4l2_subdev_frame_interval *fival)
@@ -1209,55 +1322,63 @@ static int imx214_identify_module(struct imx214 *imx214)
12091322
return 0;
12101323
}
12111324

1212-
static int imx214_parse_fwnode(struct device *dev)
1325+
static int imx214_parse_fwnode(struct device *dev, struct imx214 *imx214)
12131326
{
1327+
struct v4l2_fwnode_endpoint *bus_cfg = &imx214->bus_cfg;
12141328
struct fwnode_handle *endpoint;
1215-
struct v4l2_fwnode_endpoint bus_cfg = {
1216-
.bus_type = V4L2_MBUS_CSI2_DPHY,
1217-
};
12181329
unsigned int i;
12191330
int ret;
12201331

12211332
endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
12221333
if (!endpoint)
12231334
return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
12241335

1225-
ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
1336+
bus_cfg->bus_type = V4L2_MBUS_CSI2_DPHY;
1337+
ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, bus_cfg);
1338+
fwnode_handle_put(endpoint);
12261339
if (ret) {
12271340
dev_err_probe(dev, ret, "parsing endpoint node failed\n");
1228-
goto done;
1341+
goto error;
12291342
}
12301343

12311344
/* Check the number of MIPI CSI2 data lanes */
1232-
if (bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
1345+
if (bus_cfg->bus.mipi_csi2.num_data_lanes != 4) {
12331346
ret = dev_err_probe(dev, -EINVAL,
12341347
"only 4 data lanes are currently supported\n");
1235-
goto done;
1348+
goto error;
12361349
}
12371350

1238-
if (bus_cfg.nr_of_link_frequencies != 1)
1351+
if (bus_cfg->nr_of_link_frequencies != 1)
12391352
dev_warn(dev, "Only one link-frequency supported, please review your DT. Continuing anyway\n");
12401353

1241-
for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
1242-
if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ)
1354+
for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) {
1355+
u64 freq = bus_cfg->link_frequencies[i];
1356+
struct ccs_pll pll;
1357+
1358+
if (!imx214_pll_calculate(imx214, &pll, freq))
12431359
break;
1244-
if (bus_cfg.link_frequencies[i] ==
1245-
IMX214_DEFAULT_LINK_FREQ_LEGACY) {
1360+
if (freq == IMX214_DEFAULT_LINK_FREQ_LEGACY) {
12461361
dev_warn(dev,
12471362
"link-frequencies %d not supported, please review your DT. Continuing anyway\n",
12481363
IMX214_DEFAULT_LINK_FREQ);
1364+
freq = IMX214_DEFAULT_LINK_FREQ;
1365+
if (imx214_pll_calculate(imx214, &pll, freq))
1366+
continue;
1367+
bus_cfg->link_frequencies[i] = freq;
12491368
break;
12501369
}
12511370
}
12521371

1253-
if (i == bus_cfg.nr_of_link_frequencies)
1372+
if (i == bus_cfg->nr_of_link_frequencies)
12541373
ret = dev_err_probe(dev, -EINVAL,
1255-
"link-frequencies %d not supported, please review your DT\n",
1256-
IMX214_DEFAULT_LINK_FREQ);
1374+
"link-frequencies %lld not supported, please review your DT\n",
1375+
bus_cfg->nr_of_link_frequencies ?
1376+
bus_cfg->link_frequencies[0] : 0);
12571377

1258-
done:
1259-
v4l2_fwnode_endpoint_free(&bus_cfg);
1260-
fwnode_handle_put(endpoint);
1378+
return 0;
1379+
1380+
error:
1381+
v4l2_fwnode_endpoint_free(&imx214->bus_cfg);
12611382
return ret;
12621383
}
12631384

@@ -1297,7 +1418,7 @@ static int imx214_probe(struct i2c_client *client)
12971418
return dev_err_probe(dev, PTR_ERR(imx214->regmap),
12981419
"failed to initialize CCI\n");
12991420

1300-
ret = imx214_parse_fwnode(dev);
1421+
ret = imx214_parse_fwnode(dev, imx214);
13011422
if (ret)
13021423
return ret;
13031424

@@ -1308,7 +1429,9 @@ static int imx214_probe(struct i2c_client *client)
13081429
* Enable power initially, to avoid warnings
13091430
* from clk_disable on power_off
13101431
*/
1311-
imx214_power_on(imx214->dev);
1432+
ret = imx214_power_on(imx214->dev);
1433+
if (ret < 0)
1434+
goto error_fwnode;
13121435

13131436
ret = imx214_identify_module(imx214);
13141437
if (ret)
@@ -1339,6 +1462,8 @@ static int imx214_probe(struct i2c_client *client)
13391462
pm_runtime_set_active(imx214->dev);
13401463
pm_runtime_enable(imx214->dev);
13411464

1465+
imx214_pll_update(imx214);
1466+
13421467
ret = v4l2_async_register_subdev_sensor(&imx214->sd);
13431468
if (ret < 0) {
13441469
dev_err_probe(dev, ret,
@@ -1364,6 +1489,9 @@ static int imx214_probe(struct i2c_client *client)
13641489
error_power_off:
13651490
imx214_power_off(imx214->dev);
13661491

1492+
error_fwnode:
1493+
v4l2_fwnode_endpoint_free(&imx214->bus_cfg);
1494+
13671495
return ret;
13681496
}
13691497

@@ -1376,6 +1504,8 @@ static void imx214_remove(struct i2c_client *client)
13761504
v4l2_subdev_cleanup(sd);
13771505
media_entity_cleanup(&imx214->sd.entity);
13781506
v4l2_ctrl_handler_free(&imx214->ctrls);
1507+
v4l2_fwnode_endpoint_free(&imx214->bus_cfg);
1508+
13791509
pm_runtime_disable(&client->dev);
13801510
if (!pm_runtime_status_suspended(&client->dev)) {
13811511
imx214_power_off(imx214->dev);

0 commit comments

Comments
 (0)