1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-event.h>
11 #include <media/v4l2-fwnode.h>
13 #define OV13858_REG_VALUE_08BIT 1
14 #define OV13858_REG_VALUE_16BIT 2
15 #define OV13858_REG_VALUE_24BIT 3
17 #define OV13858_REG_MODE_SELECT 0x0100
18 #define OV13858_MODE_STANDBY 0x00
19 #define OV13858_MODE_STREAMING 0x01
21 #define OV13858_REG_SOFTWARE_RST 0x0103
22 #define OV13858_SOFTWARE_RST 0x01
24 /* PLL1 generates PCLK and MIPI_PHY_CLK */
25 #define OV13858_REG_PLL1_CTRL_0 0x0300
26 #define OV13858_REG_PLL1_CTRL_1 0x0301
27 #define OV13858_REG_PLL1_CTRL_2 0x0302
28 #define OV13858_REG_PLL1_CTRL_3 0x0303
29 #define OV13858_REG_PLL1_CTRL_4 0x0304
30 #define OV13858_REG_PLL1_CTRL_5 0x0305
32 /* PLL2 generates DAC_CLK, SCLK and SRAM_CLK */
33 #define OV13858_REG_PLL2_CTRL_B 0x030b
34 #define OV13858_REG_PLL2_CTRL_C 0x030c
35 #define OV13858_REG_PLL2_CTRL_D 0x030d
36 #define OV13858_REG_PLL2_CTRL_E 0x030e
37 #define OV13858_REG_PLL2_CTRL_F 0x030f
38 #define OV13858_REG_PLL2_CTRL_12 0x0312
39 #define OV13858_REG_MIPI_SC_CTRL0 0x3016
40 #define OV13858_REG_MIPI_SC_CTRL1 0x3022
43 #define OV13858_REG_CHIP_ID 0x300a
44 #define OV13858_CHIP_ID 0x00d855
46 /* V_TIMING internal */
47 #define OV13858_REG_VTS 0x380e
48 #define OV13858_VTS_30FPS 0x0c8e /* 30 fps */
49 #define OV13858_VTS_60FPS 0x0648 /* 60 fps */
50 #define OV13858_VTS_MAX 0x7fff
52 /* HBLANK control - read only */
53 #define OV13858_PPL_270MHZ 2244
54 #define OV13858_PPL_540MHZ 4488
56 /* Exposure control */
57 #define OV13858_REG_EXPOSURE 0x3500
58 #define OV13858_EXPOSURE_MIN 4
59 #define OV13858_EXPOSURE_STEP 1
60 #define OV13858_EXPOSURE_DEFAULT 0x640
62 /* Analog gain control */
63 #define OV13858_REG_ANALOG_GAIN 0x3508
64 #define OV13858_ANA_GAIN_MIN 0
65 #define OV13858_ANA_GAIN_MAX 0x1fff
66 #define OV13858_ANA_GAIN_STEP 1
67 #define OV13858_ANA_GAIN_DEFAULT 0x80
69 /* Digital gain control */
70 #define OV13858_REG_B_MWB_GAIN 0x5100
71 #define OV13858_REG_G_MWB_GAIN 0x5102
72 #define OV13858_REG_R_MWB_GAIN 0x5104
73 #define OV13858_DGTL_GAIN_MIN 0
74 #define OV13858_DGTL_GAIN_MAX 16384 /* Max = 16 X */
75 #define OV13858_DGTL_GAIN_DEFAULT 1024 /* Default gain = 1 X */
76 #define OV13858_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
78 /* Test Pattern Control */
79 #define OV13858_REG_TEST_PATTERN 0x4503
80 #define OV13858_TEST_PATTERN_ENABLE BIT(7)
81 #define OV13858_TEST_PATTERN_MASK 0xfc
83 /* Number of frames to skip */
84 #define OV13858_NUM_OF_SKIP_FRAMES 2
91 struct ov13858_reg_list {
93 const struct ov13858_reg *regs;
96 /* Link frequency config */
97 struct ov13858_link_freq_config {
100 /* PLL registers for this link frequency */
101 struct ov13858_reg_list reg_list;
104 /* Mode : resolution and related config&values */
105 struct ov13858_mode {
115 /* Index of Link frequency config to be used */
117 /* Default register values */
118 struct ov13858_reg_list reg_list;
121 /* 4224x3136 needs 1080Mbps/lane, 4 lanes */
122 static const struct ov13858_reg mipi_data_rate_1080mbps[] = {
124 {OV13858_REG_PLL1_CTRL_0, 0x07},
125 {OV13858_REG_PLL1_CTRL_1, 0x01},
126 {OV13858_REG_PLL1_CTRL_2, 0xc2},
127 {OV13858_REG_PLL1_CTRL_3, 0x00},
128 {OV13858_REG_PLL1_CTRL_4, 0x00},
129 {OV13858_REG_PLL1_CTRL_5, 0x01},
132 {OV13858_REG_PLL2_CTRL_B, 0x05},
133 {OV13858_REG_PLL2_CTRL_C, 0x01},
134 {OV13858_REG_PLL2_CTRL_D, 0x0e},
135 {OV13858_REG_PLL2_CTRL_E, 0x05},
136 {OV13858_REG_PLL2_CTRL_F, 0x01},
137 {OV13858_REG_PLL2_CTRL_12, 0x01},
138 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
139 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
143 * 2112x1568, 2112x1188, 1056x784 need 540Mbps/lane,
146 static const struct ov13858_reg mipi_data_rate_540mbps[] = {
148 {OV13858_REG_PLL1_CTRL_0, 0x07},
149 {OV13858_REG_PLL1_CTRL_1, 0x01},
150 {OV13858_REG_PLL1_CTRL_2, 0xc2},
151 {OV13858_REG_PLL1_CTRL_3, 0x01},
152 {OV13858_REG_PLL1_CTRL_4, 0x00},
153 {OV13858_REG_PLL1_CTRL_5, 0x01},
156 {OV13858_REG_PLL2_CTRL_B, 0x05},
157 {OV13858_REG_PLL2_CTRL_C, 0x01},
158 {OV13858_REG_PLL2_CTRL_D, 0x0e},
159 {OV13858_REG_PLL2_CTRL_E, 0x05},
160 {OV13858_REG_PLL2_CTRL_F, 0x01},
161 {OV13858_REG_PLL2_CTRL_12, 0x01},
162 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
163 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
166 static const struct ov13858_reg mode_4224x3136_regs[] = {
357 static const struct ov13858_reg mode_2112x1568_regs[] = {
548 static const struct ov13858_reg mode_2112x1188_regs[] = {
739 static const struct ov13858_reg mode_1056x784_regs[] = {
930 static const char * const ov13858_test_pattern_menu[] = {
932 "Vertical Color Bar Type 1",
933 "Vertical Color Bar Type 2",
934 "Vertical Color Bar Type 3",
935 "Vertical Color Bar Type 4"
938 /* Configurations for supported link frequencies */
939 #define OV13858_NUM_OF_LINK_FREQS 2
940 #define OV13858_LINK_FREQ_540MHZ 540000000ULL
941 #define OV13858_LINK_FREQ_270MHZ 270000000ULL
942 #define OV13858_LINK_FREQ_INDEX_0 0
943 #define OV13858_LINK_FREQ_INDEX_1 1
946 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
947 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
949 static u64 link_freq_to_pixel_rate(u64 f)
957 /* Menu items for LINK_FREQ V4L2 control */
958 static const s64 link_freq_menu_items[OV13858_NUM_OF_LINK_FREQS] = {
959 OV13858_LINK_FREQ_540MHZ,
960 OV13858_LINK_FREQ_270MHZ
963 /* Link frequency configs */
964 static const struct ov13858_link_freq_config
965 link_freq_configs[OV13858_NUM_OF_LINK_FREQS] = {
967 .pixels_per_line = OV13858_PPL_540MHZ,
969 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1080mbps),
970 .regs = mipi_data_rate_1080mbps,
974 .pixels_per_line = OV13858_PPL_270MHZ,
976 .num_of_regs = ARRAY_SIZE(mipi_data_rate_540mbps),
977 .regs = mipi_data_rate_540mbps,
983 static const struct ov13858_mode supported_modes[] = {
987 .vts_def = OV13858_VTS_30FPS,
988 .vts_min = OV13858_VTS_30FPS,
990 .num_of_regs = ARRAY_SIZE(mode_4224x3136_regs),
991 .regs = mode_4224x3136_regs,
993 .link_freq_index = OV13858_LINK_FREQ_INDEX_0,
998 .vts_def = OV13858_VTS_30FPS,
1001 .num_of_regs = ARRAY_SIZE(mode_2112x1568_regs),
1002 .regs = mode_2112x1568_regs,
1004 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1009 .vts_def = OV13858_VTS_30FPS,
1012 .num_of_regs = ARRAY_SIZE(mode_2112x1188_regs),
1013 .regs = mode_2112x1188_regs,
1015 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1020 .vts_def = OV13858_VTS_30FPS,
1023 .num_of_regs = ARRAY_SIZE(mode_1056x784_regs),
1024 .regs = mode_1056x784_regs,
1026 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1031 struct v4l2_subdev sd;
1032 struct media_pad pad;
1034 struct v4l2_ctrl_handler ctrl_handler;
1036 struct v4l2_ctrl *link_freq;
1037 struct v4l2_ctrl *pixel_rate;
1038 struct v4l2_ctrl *vblank;
1039 struct v4l2_ctrl *hblank;
1040 struct v4l2_ctrl *exposure;
1043 const struct ov13858_mode *cur_mode;
1045 /* Mutex for serialized access */
1048 /* Streaming on/off */
1052 #define to_ov13858(_sd) container_of(_sd, struct ov13858, sd)
1054 /* Read registers up to 4 at a time */
1055 static int ov13858_read_reg(struct ov13858 *ov13858, u16 reg, u32 len,
1058 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1059 struct i2c_msg msgs[2];
1063 __be16 reg_addr_be = cpu_to_be16(reg);
1068 data_be_p = (u8 *)&data_be;
1069 /* Write register address */
1070 msgs[0].addr = client->addr;
1073 msgs[0].buf = (u8 *)®_addr_be;
1075 /* Read data from register */
1076 msgs[1].addr = client->addr;
1077 msgs[1].flags = I2C_M_RD;
1079 msgs[1].buf = &data_be_p[4 - len];
1081 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1082 if (ret != ARRAY_SIZE(msgs))
1085 *val = be32_to_cpu(data_be);
1090 /* Write registers up to 4 at a time */
1091 static int ov13858_write_reg(struct ov13858 *ov13858, u16 reg, u32 len,
1094 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1103 buf[1] = reg & 0xff;
1105 val = cpu_to_be32(__val);
1111 buf[buf_i++] = val_p[val_i++];
1113 if (i2c_master_send(client, buf, len + 2) != len + 2)
1119 /* Write a list of registers */
1120 static int ov13858_write_regs(struct ov13858 *ov13858,
1121 const struct ov13858_reg *regs, u32 len)
1123 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1127 for (i = 0; i < len; i++) {
1128 ret = ov13858_write_reg(ov13858, regs[i].address, 1,
1131 dev_err_ratelimited(
1133 "Failed to write reg 0x%4.4x. error = %d\n",
1134 regs[i].address, ret);
1143 static int ov13858_write_reg_list(struct ov13858 *ov13858,
1144 const struct ov13858_reg_list *r_list)
1146 return ov13858_write_regs(ov13858, r_list->regs, r_list->num_of_regs);
1149 /* Open sub-device */
1150 static int ov13858_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1152 struct ov13858 *ov13858 = to_ov13858(sd);
1153 struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_get_try_format(sd,
1157 mutex_lock(&ov13858->mutex);
1159 /* Initialize try_fmt */
1160 try_fmt->width = ov13858->cur_mode->width;
1161 try_fmt->height = ov13858->cur_mode->height;
1162 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1163 try_fmt->field = V4L2_FIELD_NONE;
1165 /* No crop or compose */
1166 mutex_unlock(&ov13858->mutex);
1171 static int ov13858_update_digital_gain(struct ov13858 *ov13858, u32 d_gain)
1175 ret = ov13858_write_reg(ov13858, OV13858_REG_B_MWB_GAIN,
1176 OV13858_REG_VALUE_16BIT, d_gain);
1180 ret = ov13858_write_reg(ov13858, OV13858_REG_G_MWB_GAIN,
1181 OV13858_REG_VALUE_16BIT, d_gain);
1185 ret = ov13858_write_reg(ov13858, OV13858_REG_R_MWB_GAIN,
1186 OV13858_REG_VALUE_16BIT, d_gain);
1191 static int ov13858_enable_test_pattern(struct ov13858 *ov13858, u32 pattern)
1196 ret = ov13858_read_reg(ov13858, OV13858_REG_TEST_PATTERN,
1197 OV13858_REG_VALUE_08BIT, &val);
1202 val &= OV13858_TEST_PATTERN_MASK;
1203 val |= (pattern - 1) | OV13858_TEST_PATTERN_ENABLE;
1205 val &= ~OV13858_TEST_PATTERN_ENABLE;
1208 return ov13858_write_reg(ov13858, OV13858_REG_TEST_PATTERN,
1209 OV13858_REG_VALUE_08BIT, val);
1212 static int ov13858_set_ctrl(struct v4l2_ctrl *ctrl)
1214 struct ov13858 *ov13858 = container_of(ctrl->handler,
1215 struct ov13858, ctrl_handler);
1216 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1220 /* Propagate change of current control to all related controls */
1222 case V4L2_CID_VBLANK:
1223 /* Update max exposure while meeting expected vblanking */
1224 max = ov13858->cur_mode->height + ctrl->val - 8;
1225 __v4l2_ctrl_modify_range(ov13858->exposure,
1226 ov13858->exposure->minimum,
1227 max, ov13858->exposure->step, max);
1232 * Applying V4L2 control value only happens
1233 * when power is up for streaming
1235 if (!pm_runtime_get_if_in_use(&client->dev))
1240 case V4L2_CID_ANALOGUE_GAIN:
1241 ret = ov13858_write_reg(ov13858, OV13858_REG_ANALOG_GAIN,
1242 OV13858_REG_VALUE_16BIT, ctrl->val);
1244 case V4L2_CID_DIGITAL_GAIN:
1245 ret = ov13858_update_digital_gain(ov13858, ctrl->val);
1247 case V4L2_CID_EXPOSURE:
1248 ret = ov13858_write_reg(ov13858, OV13858_REG_EXPOSURE,
1249 OV13858_REG_VALUE_24BIT,
1252 case V4L2_CID_VBLANK:
1253 /* Update VTS that meets expected vertical blanking */
1254 ret = ov13858_write_reg(ov13858, OV13858_REG_VTS,
1255 OV13858_REG_VALUE_16BIT,
1256 ov13858->cur_mode->height
1259 case V4L2_CID_TEST_PATTERN:
1260 ret = ov13858_enable_test_pattern(ov13858, ctrl->val);
1263 dev_info(&client->dev,
1264 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1265 ctrl->id, ctrl->val);
1269 pm_runtime_put(&client->dev);
1274 static const struct v4l2_ctrl_ops ov13858_ctrl_ops = {
1275 .s_ctrl = ov13858_set_ctrl,
1278 static int ov13858_enum_mbus_code(struct v4l2_subdev *sd,
1279 struct v4l2_subdev_state *sd_state,
1280 struct v4l2_subdev_mbus_code_enum *code)
1282 /* Only one bayer order(GRBG) is supported */
1283 if (code->index > 0)
1286 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1291 static int ov13858_enum_frame_size(struct v4l2_subdev *sd,
1292 struct v4l2_subdev_state *sd_state,
1293 struct v4l2_subdev_frame_size_enum *fse)
1295 if (fse->index >= ARRAY_SIZE(supported_modes))
1298 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1301 fse->min_width = supported_modes[fse->index].width;
1302 fse->max_width = fse->min_width;
1303 fse->min_height = supported_modes[fse->index].height;
1304 fse->max_height = fse->min_height;
1309 static void ov13858_update_pad_format(const struct ov13858_mode *mode,
1310 struct v4l2_subdev_format *fmt)
1312 fmt->format.width = mode->width;
1313 fmt->format.height = mode->height;
1314 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1315 fmt->format.field = V4L2_FIELD_NONE;
1318 static int ov13858_do_get_pad_format(struct ov13858 *ov13858,
1319 struct v4l2_subdev_state *sd_state,
1320 struct v4l2_subdev_format *fmt)
1322 struct v4l2_mbus_framefmt *framefmt;
1323 struct v4l2_subdev *sd = &ov13858->sd;
1325 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1326 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1327 fmt->format = *framefmt;
1329 ov13858_update_pad_format(ov13858->cur_mode, fmt);
1335 static int ov13858_get_pad_format(struct v4l2_subdev *sd,
1336 struct v4l2_subdev_state *sd_state,
1337 struct v4l2_subdev_format *fmt)
1339 struct ov13858 *ov13858 = to_ov13858(sd);
1342 mutex_lock(&ov13858->mutex);
1343 ret = ov13858_do_get_pad_format(ov13858, sd_state, fmt);
1344 mutex_unlock(&ov13858->mutex);
1350 ov13858_set_pad_format(struct v4l2_subdev *sd,
1351 struct v4l2_subdev_state *sd_state,
1352 struct v4l2_subdev_format *fmt)
1354 struct ov13858 *ov13858 = to_ov13858(sd);
1355 const struct ov13858_mode *mode;
1356 struct v4l2_mbus_framefmt *framefmt;
1363 mutex_lock(&ov13858->mutex);
1365 /* Only one raw bayer(GRBG) order is supported */
1366 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
1367 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1369 mode = v4l2_find_nearest_size(supported_modes,
1370 ARRAY_SIZE(supported_modes),
1372 fmt->format.width, fmt->format.height);
1373 ov13858_update_pad_format(mode, fmt);
1374 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1375 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1376 *framefmt = fmt->format;
1378 ov13858->cur_mode = mode;
1379 __v4l2_ctrl_s_ctrl(ov13858->link_freq, mode->link_freq_index);
1380 link_freq = link_freq_menu_items[mode->link_freq_index];
1381 pixel_rate = link_freq_to_pixel_rate(link_freq);
1382 __v4l2_ctrl_s_ctrl_int64(ov13858->pixel_rate, pixel_rate);
1384 /* Update limits and set FPS to default */
1385 vblank_def = ov13858->cur_mode->vts_def -
1386 ov13858->cur_mode->height;
1387 vblank_min = ov13858->cur_mode->vts_min -
1388 ov13858->cur_mode->height;
1389 __v4l2_ctrl_modify_range(
1390 ov13858->vblank, vblank_min,
1391 OV13858_VTS_MAX - ov13858->cur_mode->height, 1,
1393 __v4l2_ctrl_s_ctrl(ov13858->vblank, vblank_def);
1395 link_freq_configs[mode->link_freq_index].pixels_per_line
1396 - ov13858->cur_mode->width;
1397 __v4l2_ctrl_modify_range(ov13858->hblank, h_blank,
1398 h_blank, 1, h_blank);
1401 mutex_unlock(&ov13858->mutex);
1406 static int ov13858_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1408 *frames = OV13858_NUM_OF_SKIP_FRAMES;
1413 /* Start streaming */
1414 static int ov13858_start_streaming(struct ov13858 *ov13858)
1416 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1417 const struct ov13858_reg_list *reg_list;
1418 int ret, link_freq_index;
1420 /* Get out of from software reset */
1421 ret = ov13858_write_reg(ov13858, OV13858_REG_SOFTWARE_RST,
1422 OV13858_REG_VALUE_08BIT, OV13858_SOFTWARE_RST);
1424 dev_err(&client->dev, "%s failed to set powerup registers\n",
1430 link_freq_index = ov13858->cur_mode->link_freq_index;
1431 reg_list = &link_freq_configs[link_freq_index].reg_list;
1432 ret = ov13858_write_reg_list(ov13858, reg_list);
1434 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1438 /* Apply default values of current mode */
1439 reg_list = &ov13858->cur_mode->reg_list;
1440 ret = ov13858_write_reg_list(ov13858, reg_list);
1442 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1446 /* Apply customized values from user */
1447 ret = __v4l2_ctrl_handler_setup(ov13858->sd.ctrl_handler);
1451 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1452 OV13858_REG_VALUE_08BIT,
1453 OV13858_MODE_STREAMING);
1456 /* Stop streaming */
1457 static int ov13858_stop_streaming(struct ov13858 *ov13858)
1459 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1460 OV13858_REG_VALUE_08BIT, OV13858_MODE_STANDBY);
1463 static int ov13858_set_stream(struct v4l2_subdev *sd, int enable)
1465 struct ov13858 *ov13858 = to_ov13858(sd);
1466 struct i2c_client *client = v4l2_get_subdevdata(sd);
1469 mutex_lock(&ov13858->mutex);
1470 if (ov13858->streaming == enable) {
1471 mutex_unlock(&ov13858->mutex);
1476 ret = pm_runtime_resume_and_get(&client->dev);
1481 * Apply default & customized values
1482 * and then start streaming.
1484 ret = ov13858_start_streaming(ov13858);
1488 ov13858_stop_streaming(ov13858);
1489 pm_runtime_put(&client->dev);
1492 ov13858->streaming = enable;
1493 mutex_unlock(&ov13858->mutex);
1498 pm_runtime_put(&client->dev);
1500 mutex_unlock(&ov13858->mutex);
1505 static int __maybe_unused ov13858_suspend(struct device *dev)
1507 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1508 struct ov13858 *ov13858 = to_ov13858(sd);
1510 if (ov13858->streaming)
1511 ov13858_stop_streaming(ov13858);
1516 static int __maybe_unused ov13858_resume(struct device *dev)
1518 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1519 struct ov13858 *ov13858 = to_ov13858(sd);
1522 if (ov13858->streaming) {
1523 ret = ov13858_start_streaming(ov13858);
1531 ov13858_stop_streaming(ov13858);
1532 ov13858->streaming = false;
1536 /* Verify chip ID */
1537 static int ov13858_identify_module(struct ov13858 *ov13858)
1539 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1543 ret = ov13858_read_reg(ov13858, OV13858_REG_CHIP_ID,
1544 OV13858_REG_VALUE_24BIT, &val);
1548 if (val != OV13858_CHIP_ID) {
1549 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1550 OV13858_CHIP_ID, val);
1557 static const struct v4l2_subdev_core_ops ov13858_core_ops = {
1558 .log_status = v4l2_ctrl_subdev_log_status,
1559 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1560 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1563 static const struct v4l2_subdev_video_ops ov13858_video_ops = {
1564 .s_stream = ov13858_set_stream,
1567 static const struct v4l2_subdev_pad_ops ov13858_pad_ops = {
1568 .enum_mbus_code = ov13858_enum_mbus_code,
1569 .get_fmt = ov13858_get_pad_format,
1570 .set_fmt = ov13858_set_pad_format,
1571 .enum_frame_size = ov13858_enum_frame_size,
1574 static const struct v4l2_subdev_sensor_ops ov13858_sensor_ops = {
1575 .g_skip_frames = ov13858_get_skip_frames,
1578 static const struct v4l2_subdev_ops ov13858_subdev_ops = {
1579 .core = &ov13858_core_ops,
1580 .video = &ov13858_video_ops,
1581 .pad = &ov13858_pad_ops,
1582 .sensor = &ov13858_sensor_ops,
1585 static const struct media_entity_operations ov13858_subdev_entity_ops = {
1586 .link_validate = v4l2_subdev_link_validate,
1589 static const struct v4l2_subdev_internal_ops ov13858_internal_ops = {
1590 .open = ov13858_open,
1593 /* Initialize control handlers */
1594 static int ov13858_init_controls(struct ov13858 *ov13858)
1596 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1597 struct v4l2_fwnode_device_properties props;
1598 struct v4l2_ctrl_handler *ctrl_hdlr;
1605 const struct ov13858_mode *mode;
1608 ctrl_hdlr = &ov13858->ctrl_handler;
1609 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1613 mutex_init(&ov13858->mutex);
1614 ctrl_hdlr->lock = &ov13858->mutex;
1615 ov13858->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1618 OV13858_NUM_OF_LINK_FREQS - 1,
1620 link_freq_menu_items);
1621 if (ov13858->link_freq)
1622 ov13858->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1624 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1625 pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
1626 /* By default, PIXEL_RATE is read only */
1627 ov13858->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops,
1628 V4L2_CID_PIXEL_RATE,
1629 pixel_rate_min, pixel_rate_max,
1632 mode = ov13858->cur_mode;
1633 vblank_def = mode->vts_def - mode->height;
1634 vblank_min = mode->vts_min - mode->height;
1635 ov13858->vblank = v4l2_ctrl_new_std(
1636 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_VBLANK,
1637 vblank_min, OV13858_VTS_MAX - mode->height, 1,
1640 hblank = link_freq_configs[mode->link_freq_index].pixels_per_line -
1642 ov13858->hblank = v4l2_ctrl_new_std(
1643 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_HBLANK,
1644 hblank, hblank, 1, hblank);
1645 if (ov13858->hblank)
1646 ov13858->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1648 exposure_max = mode->vts_def - 8;
1649 ov13858->exposure = v4l2_ctrl_new_std(
1650 ctrl_hdlr, &ov13858_ctrl_ops,
1651 V4L2_CID_EXPOSURE, OV13858_EXPOSURE_MIN,
1652 exposure_max, OV13858_EXPOSURE_STEP,
1653 OV13858_EXPOSURE_DEFAULT);
1655 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1656 OV13858_ANA_GAIN_MIN, OV13858_ANA_GAIN_MAX,
1657 OV13858_ANA_GAIN_STEP, OV13858_ANA_GAIN_DEFAULT);
1660 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1661 OV13858_DGTL_GAIN_MIN, OV13858_DGTL_GAIN_MAX,
1662 OV13858_DGTL_GAIN_STEP, OV13858_DGTL_GAIN_DEFAULT);
1664 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov13858_ctrl_ops,
1665 V4L2_CID_TEST_PATTERN,
1666 ARRAY_SIZE(ov13858_test_pattern_menu) - 1,
1667 0, 0, ov13858_test_pattern_menu);
1668 if (ctrl_hdlr->error) {
1669 ret = ctrl_hdlr->error;
1670 dev_err(&client->dev, "%s control init failed (%d)\n",
1675 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1679 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov13858_ctrl_ops,
1684 ov13858->sd.ctrl_handler = ctrl_hdlr;
1689 v4l2_ctrl_handler_free(ctrl_hdlr);
1690 mutex_destroy(&ov13858->mutex);
1695 static void ov13858_free_controls(struct ov13858 *ov13858)
1697 v4l2_ctrl_handler_free(ov13858->sd.ctrl_handler);
1698 mutex_destroy(&ov13858->mutex);
1701 static int ov13858_probe(struct i2c_client *client)
1703 struct ov13858 *ov13858;
1707 device_property_read_u32(&client->dev, "clock-frequency", &val);
1708 if (val != 19200000)
1711 ov13858 = devm_kzalloc(&client->dev, sizeof(*ov13858), GFP_KERNEL);
1715 /* Initialize subdev */
1716 v4l2_i2c_subdev_init(&ov13858->sd, client, &ov13858_subdev_ops);
1718 /* Check module identity */
1719 ret = ov13858_identify_module(ov13858);
1721 dev_err(&client->dev, "failed to find sensor: %d\n", ret);
1725 /* Set default mode to max resolution */
1726 ov13858->cur_mode = &supported_modes[0];
1728 ret = ov13858_init_controls(ov13858);
1732 /* Initialize subdev */
1733 ov13858->sd.internal_ops = &ov13858_internal_ops;
1734 ov13858->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1735 V4L2_SUBDEV_FL_HAS_EVENTS;
1736 ov13858->sd.entity.ops = &ov13858_subdev_entity_ops;
1737 ov13858->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1739 /* Initialize source pad */
1740 ov13858->pad.flags = MEDIA_PAD_FL_SOURCE;
1741 ret = media_entity_pads_init(&ov13858->sd.entity, 1, &ov13858->pad);
1743 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1744 goto error_handler_free;
1747 ret = v4l2_async_register_subdev_sensor(&ov13858->sd);
1749 goto error_media_entity;
1752 * Device is already turned on by i2c-core with ACPI domain PM.
1753 * Enable runtime PM and turn off the device.
1755 pm_runtime_set_active(&client->dev);
1756 pm_runtime_enable(&client->dev);
1757 pm_runtime_idle(&client->dev);
1762 media_entity_cleanup(&ov13858->sd.entity);
1765 ov13858_free_controls(ov13858);
1766 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1771 static void ov13858_remove(struct i2c_client *client)
1773 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1774 struct ov13858 *ov13858 = to_ov13858(sd);
1776 v4l2_async_unregister_subdev(sd);
1777 media_entity_cleanup(&sd->entity);
1778 ov13858_free_controls(ov13858);
1780 pm_runtime_disable(&client->dev);
1783 static const struct i2c_device_id ov13858_id_table[] = {
1788 MODULE_DEVICE_TABLE(i2c, ov13858_id_table);
1790 static const struct dev_pm_ops ov13858_pm_ops = {
1791 SET_SYSTEM_SLEEP_PM_OPS(ov13858_suspend, ov13858_resume)
1795 static const struct acpi_device_id ov13858_acpi_ids[] = {
1800 MODULE_DEVICE_TABLE(acpi, ov13858_acpi_ids);
1803 static struct i2c_driver ov13858_i2c_driver = {
1806 .pm = &ov13858_pm_ops,
1807 .acpi_match_table = ACPI_PTR(ov13858_acpi_ids),
1809 .probe = ov13858_probe,
1810 .remove = ov13858_remove,
1811 .id_table = ov13858_id_table,
1814 module_i2c_driver(ov13858_i2c_driver);
1818 MODULE_AUTHOR("Yang, Hyungwoo");
1819 MODULE_DESCRIPTION("Omnivision ov13858 sensor driver");
1820 MODULE_LICENSE("GPL v2");