2 * Copyright (c) 2017 Intel Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/acpi.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
22 #define OV13858_REG_VALUE_08BIT 1
23 #define OV13858_REG_VALUE_16BIT 2
24 #define OV13858_REG_VALUE_24BIT 3
26 #define OV13858_REG_MODE_SELECT 0x0100
27 #define OV13858_MODE_STANDBY 0x00
28 #define OV13858_MODE_STREAMING 0x01
30 #define OV13858_REG_SOFTWARE_RST 0x0103
31 #define OV13858_SOFTWARE_RST 0x01
33 /* PLL1 generates PCLK and MIPI_PHY_CLK */
34 #define OV13858_REG_PLL1_CTRL_0 0x0300
35 #define OV13858_REG_PLL1_CTRL_1 0x0301
36 #define OV13858_REG_PLL1_CTRL_2 0x0302
37 #define OV13858_REG_PLL1_CTRL_3 0x0303
38 #define OV13858_REG_PLL1_CTRL_4 0x0304
39 #define OV13858_REG_PLL1_CTRL_5 0x0305
41 /* PLL2 generates DAC_CLK, SCLK and SRAM_CLK */
42 #define OV13858_REG_PLL2_CTRL_B 0x030b
43 #define OV13858_REG_PLL2_CTRL_C 0x030c
44 #define OV13858_REG_PLL2_CTRL_D 0x030d
45 #define OV13858_REG_PLL2_CTRL_E 0x030e
46 #define OV13858_REG_PLL2_CTRL_F 0x030f
47 #define OV13858_REG_PLL2_CTRL_12 0x0312
48 #define OV13858_REG_MIPI_SC_CTRL0 0x3016
49 #define OV13858_REG_MIPI_SC_CTRL1 0x3022
52 #define OV13858_REG_CHIP_ID 0x300a
53 #define OV13858_CHIP_ID 0x00d855
55 /* V_TIMING internal */
56 #define OV13858_REG_VTS 0x380e
57 #define OV13858_VTS_30FPS 0x0c8e /* 30 fps */
58 #define OV13858_VTS_60FPS 0x0648 /* 60 fps */
59 #define OV13858_VTS_MAX 0x7fff
61 /* HBLANK control - read only */
62 #define OV13858_PPL_270MHZ 2244
63 #define OV13858_PPL_540MHZ 4488
65 /* Exposure control */
66 #define OV13858_REG_EXPOSURE 0x3500
67 #define OV13858_EXPOSURE_MIN 4
68 #define OV13858_EXPOSURE_STEP 1
69 #define OV13858_EXPOSURE_DEFAULT 0x640
71 /* Analog gain control */
72 #define OV13858_REG_ANALOG_GAIN 0x3508
73 #define OV13858_ANA_GAIN_MIN 0
74 #define OV13858_ANA_GAIN_MAX 0x1fff
75 #define OV13858_ANA_GAIN_STEP 1
76 #define OV13858_ANA_GAIN_DEFAULT 0x80
78 /* Digital gain control */
79 #define OV13858_REG_B_MWB_GAIN 0x5100
80 #define OV13858_REG_G_MWB_GAIN 0x5102
81 #define OV13858_REG_R_MWB_GAIN 0x5104
82 #define OV13858_DGTL_GAIN_MIN 0
83 #define OV13858_DGTL_GAIN_MAX 16384 /* Max = 16 X */
84 #define OV13858_DGTL_GAIN_DEFAULT 1024 /* Default gain = 1 X */
85 #define OV13858_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
87 /* Test Pattern Control */
88 #define OV13858_REG_TEST_PATTERN 0x4503
89 #define OV13858_TEST_PATTERN_ENABLE BIT(7)
90 #define OV13858_TEST_PATTERN_MASK 0xfc
92 /* Number of frames to skip */
93 #define OV13858_NUM_OF_SKIP_FRAMES 2
100 struct ov13858_reg_list {
102 const struct ov13858_reg *regs;
105 /* Link frequency config */
106 struct ov13858_link_freq_config {
109 /* PLL registers for this link frequency */
110 struct ov13858_reg_list reg_list;
113 /* Mode : resolution and related config&values */
114 struct ov13858_mode {
124 /* Index of Link frequency config to be used */
126 /* Default register values */
127 struct ov13858_reg_list reg_list;
130 /* 4224x3136 needs 1080Mbps/lane, 4 lanes */
131 static const struct ov13858_reg mipi_data_rate_1080mbps[] = {
133 {OV13858_REG_PLL1_CTRL_0, 0x07},
134 {OV13858_REG_PLL1_CTRL_1, 0x01},
135 {OV13858_REG_PLL1_CTRL_2, 0xc2},
136 {OV13858_REG_PLL1_CTRL_3, 0x00},
137 {OV13858_REG_PLL1_CTRL_4, 0x00},
138 {OV13858_REG_PLL1_CTRL_5, 0x01},
141 {OV13858_REG_PLL2_CTRL_B, 0x05},
142 {OV13858_REG_PLL2_CTRL_C, 0x01},
143 {OV13858_REG_PLL2_CTRL_D, 0x0e},
144 {OV13858_REG_PLL2_CTRL_E, 0x05},
145 {OV13858_REG_PLL2_CTRL_F, 0x01},
146 {OV13858_REG_PLL2_CTRL_12, 0x01},
147 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
148 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
152 * 2112x1568, 2112x1188, 1056x784 need 540Mbps/lane,
155 static const struct ov13858_reg mipi_data_rate_540mbps[] = {
157 {OV13858_REG_PLL1_CTRL_0, 0x07},
158 {OV13858_REG_PLL1_CTRL_1, 0x01},
159 {OV13858_REG_PLL1_CTRL_2, 0xc2},
160 {OV13858_REG_PLL1_CTRL_3, 0x01},
161 {OV13858_REG_PLL1_CTRL_4, 0x00},
162 {OV13858_REG_PLL1_CTRL_5, 0x01},
165 {OV13858_REG_PLL2_CTRL_B, 0x05},
166 {OV13858_REG_PLL2_CTRL_C, 0x01},
167 {OV13858_REG_PLL2_CTRL_D, 0x0e},
168 {OV13858_REG_PLL2_CTRL_E, 0x05},
169 {OV13858_REG_PLL2_CTRL_F, 0x01},
170 {OV13858_REG_PLL2_CTRL_12, 0x01},
171 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
172 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
175 static const struct ov13858_reg mode_4224x3136_regs[] = {
366 static const struct ov13858_reg mode_2112x1568_regs[] = {
557 static const struct ov13858_reg mode_2112x1188_regs[] = {
748 static const struct ov13858_reg mode_1056x784_regs[] = {
939 static const char * const ov13858_test_pattern_menu[] = {
941 "Vertical Color Bar Type 1",
942 "Vertical Color Bar Type 2",
943 "Vertical Color Bar Type 3",
944 "Vertical Color Bar Type 4"
947 /* Configurations for supported link frequencies */
948 #define OV13858_NUM_OF_LINK_FREQS 2
949 #define OV13858_LINK_FREQ_540MHZ 540000000ULL
950 #define OV13858_LINK_FREQ_270MHZ 270000000ULL
951 #define OV13858_LINK_FREQ_INDEX_0 0
952 #define OV13858_LINK_FREQ_INDEX_1 1
955 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
956 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
958 static u64 link_freq_to_pixel_rate(u64 f)
966 /* Menu items for LINK_FREQ V4L2 control */
967 static const s64 link_freq_menu_items[OV13858_NUM_OF_LINK_FREQS] = {
968 OV13858_LINK_FREQ_540MHZ,
969 OV13858_LINK_FREQ_270MHZ
972 /* Link frequency configs */
973 static const struct ov13858_link_freq_config
974 link_freq_configs[OV13858_NUM_OF_LINK_FREQS] = {
976 .pixels_per_line = OV13858_PPL_540MHZ,
978 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1080mbps),
979 .regs = mipi_data_rate_1080mbps,
983 .pixels_per_line = OV13858_PPL_270MHZ,
985 .num_of_regs = ARRAY_SIZE(mipi_data_rate_540mbps),
986 .regs = mipi_data_rate_540mbps,
992 static const struct ov13858_mode supported_modes[] = {
996 .vts_def = OV13858_VTS_30FPS,
997 .vts_min = OV13858_VTS_30FPS,
999 .num_of_regs = ARRAY_SIZE(mode_4224x3136_regs),
1000 .regs = mode_4224x3136_regs,
1002 .link_freq_index = OV13858_LINK_FREQ_INDEX_0,
1007 .vts_def = OV13858_VTS_30FPS,
1010 .num_of_regs = ARRAY_SIZE(mode_2112x1568_regs),
1011 .regs = mode_2112x1568_regs,
1013 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1018 .vts_def = OV13858_VTS_30FPS,
1021 .num_of_regs = ARRAY_SIZE(mode_2112x1188_regs),
1022 .regs = mode_2112x1188_regs,
1024 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1029 .vts_def = OV13858_VTS_30FPS,
1032 .num_of_regs = ARRAY_SIZE(mode_1056x784_regs),
1033 .regs = mode_1056x784_regs,
1035 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1040 struct v4l2_subdev sd;
1041 struct media_pad pad;
1043 struct v4l2_ctrl_handler ctrl_handler;
1045 struct v4l2_ctrl *link_freq;
1046 struct v4l2_ctrl *pixel_rate;
1047 struct v4l2_ctrl *vblank;
1048 struct v4l2_ctrl *hblank;
1049 struct v4l2_ctrl *exposure;
1052 const struct ov13858_mode *cur_mode;
1054 /* Mutex for serialized access */
1057 /* Streaming on/off */
1061 #define to_ov13858(_sd) container_of(_sd, struct ov13858, sd)
1063 /* Read registers up to 4 at a time */
1064 static int ov13858_read_reg(struct ov13858 *ov13858, u16 reg, u32 len,
1067 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1068 struct i2c_msg msgs[2];
1072 __be16 reg_addr_be = cpu_to_be16(reg);
1077 data_be_p = (u8 *)&data_be;
1078 /* Write register address */
1079 msgs[0].addr = client->addr;
1082 msgs[0].buf = (u8 *)®_addr_be;
1084 /* Read data from register */
1085 msgs[1].addr = client->addr;
1086 msgs[1].flags = I2C_M_RD;
1088 msgs[1].buf = &data_be_p[4 - len];
1090 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1091 if (ret != ARRAY_SIZE(msgs))
1094 *val = be32_to_cpu(data_be);
1099 /* Write registers up to 4 at a time */
1100 static int ov13858_write_reg(struct ov13858 *ov13858, u16 reg, u32 len,
1103 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1112 buf[1] = reg & 0xff;
1114 val = cpu_to_be32(__val);
1120 buf[buf_i++] = val_p[val_i++];
1122 if (i2c_master_send(client, buf, len + 2) != len + 2)
1128 /* Write a list of registers */
1129 static int ov13858_write_regs(struct ov13858 *ov13858,
1130 const struct ov13858_reg *regs, u32 len)
1132 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1136 for (i = 0; i < len; i++) {
1137 ret = ov13858_write_reg(ov13858, regs[i].address, 1,
1140 dev_err_ratelimited(
1142 "Failed to write reg 0x%4.4x. error = %d\n",
1143 regs[i].address, ret);
1152 static int ov13858_write_reg_list(struct ov13858 *ov13858,
1153 const struct ov13858_reg_list *r_list)
1155 return ov13858_write_regs(ov13858, r_list->regs, r_list->num_of_regs);
1158 /* Open sub-device */
1159 static int ov13858_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1161 struct ov13858 *ov13858 = to_ov13858(sd);
1162 struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_get_try_format(sd,
1166 mutex_lock(&ov13858->mutex);
1168 /* Initialize try_fmt */
1169 try_fmt->width = ov13858->cur_mode->width;
1170 try_fmt->height = ov13858->cur_mode->height;
1171 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1172 try_fmt->field = V4L2_FIELD_NONE;
1174 /* No crop or compose */
1175 mutex_unlock(&ov13858->mutex);
1180 static int ov13858_update_digital_gain(struct ov13858 *ov13858, u32 d_gain)
1184 ret = ov13858_write_reg(ov13858, OV13858_REG_B_MWB_GAIN,
1185 OV13858_REG_VALUE_16BIT, d_gain);
1189 ret = ov13858_write_reg(ov13858, OV13858_REG_G_MWB_GAIN,
1190 OV13858_REG_VALUE_16BIT, d_gain);
1194 ret = ov13858_write_reg(ov13858, OV13858_REG_R_MWB_GAIN,
1195 OV13858_REG_VALUE_16BIT, d_gain);
1200 static int ov13858_enable_test_pattern(struct ov13858 *ov13858, u32 pattern)
1205 ret = ov13858_read_reg(ov13858, OV13858_REG_TEST_PATTERN,
1206 OV13858_REG_VALUE_08BIT, &val);
1211 val &= OV13858_TEST_PATTERN_MASK;
1212 val |= (pattern - 1) | OV13858_TEST_PATTERN_ENABLE;
1214 val &= ~OV13858_TEST_PATTERN_ENABLE;
1217 return ov13858_write_reg(ov13858, OV13858_REG_TEST_PATTERN,
1218 OV13858_REG_VALUE_08BIT, val);
1221 static int ov13858_set_ctrl(struct v4l2_ctrl *ctrl)
1223 struct ov13858 *ov13858 = container_of(ctrl->handler,
1224 struct ov13858, ctrl_handler);
1225 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1229 /* Propagate change of current control to all related controls */
1231 case V4L2_CID_VBLANK:
1232 /* Update max exposure while meeting expected vblanking */
1233 max = ov13858->cur_mode->height + ctrl->val - 8;
1234 __v4l2_ctrl_modify_range(ov13858->exposure,
1235 ov13858->exposure->minimum,
1236 max, ov13858->exposure->step, max);
1241 * Applying V4L2 control value only happens
1242 * when power is up for streaming
1244 if (pm_runtime_get_if_in_use(&client->dev) <= 0)
1249 case V4L2_CID_ANALOGUE_GAIN:
1250 ret = ov13858_write_reg(ov13858, OV13858_REG_ANALOG_GAIN,
1251 OV13858_REG_VALUE_16BIT, ctrl->val);
1253 case V4L2_CID_DIGITAL_GAIN:
1254 ret = ov13858_update_digital_gain(ov13858, ctrl->val);
1256 case V4L2_CID_EXPOSURE:
1257 ret = ov13858_write_reg(ov13858, OV13858_REG_EXPOSURE,
1258 OV13858_REG_VALUE_24BIT,
1261 case V4L2_CID_VBLANK:
1262 /* Update VTS that meets expected vertical blanking */
1263 ret = ov13858_write_reg(ov13858, OV13858_REG_VTS,
1264 OV13858_REG_VALUE_16BIT,
1265 ov13858->cur_mode->height
1268 case V4L2_CID_TEST_PATTERN:
1269 ret = ov13858_enable_test_pattern(ov13858, ctrl->val);
1272 dev_info(&client->dev,
1273 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1274 ctrl->id, ctrl->val);
1278 pm_runtime_put(&client->dev);
1283 static const struct v4l2_ctrl_ops ov13858_ctrl_ops = {
1284 .s_ctrl = ov13858_set_ctrl,
1287 static int ov13858_enum_mbus_code(struct v4l2_subdev *sd,
1288 struct v4l2_subdev_pad_config *cfg,
1289 struct v4l2_subdev_mbus_code_enum *code)
1291 /* Only one bayer order(GRBG) is supported */
1292 if (code->index > 0)
1295 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1300 static int ov13858_enum_frame_size(struct v4l2_subdev *sd,
1301 struct v4l2_subdev_pad_config *cfg,
1302 struct v4l2_subdev_frame_size_enum *fse)
1304 if (fse->index >= ARRAY_SIZE(supported_modes))
1307 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1310 fse->min_width = supported_modes[fse->index].width;
1311 fse->max_width = fse->min_width;
1312 fse->min_height = supported_modes[fse->index].height;
1313 fse->max_height = fse->min_height;
1318 static void ov13858_update_pad_format(const struct ov13858_mode *mode,
1319 struct v4l2_subdev_format *fmt)
1321 fmt->format.width = mode->width;
1322 fmt->format.height = mode->height;
1323 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1324 fmt->format.field = V4L2_FIELD_NONE;
1327 static int ov13858_do_get_pad_format(struct ov13858 *ov13858,
1328 struct v4l2_subdev_pad_config *cfg,
1329 struct v4l2_subdev_format *fmt)
1331 struct v4l2_mbus_framefmt *framefmt;
1332 struct v4l2_subdev *sd = &ov13858->sd;
1334 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1335 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1336 fmt->format = *framefmt;
1338 ov13858_update_pad_format(ov13858->cur_mode, fmt);
1344 static int ov13858_get_pad_format(struct v4l2_subdev *sd,
1345 struct v4l2_subdev_pad_config *cfg,
1346 struct v4l2_subdev_format *fmt)
1348 struct ov13858 *ov13858 = to_ov13858(sd);
1351 mutex_lock(&ov13858->mutex);
1352 ret = ov13858_do_get_pad_format(ov13858, cfg, fmt);
1353 mutex_unlock(&ov13858->mutex);
1359 ov13858_set_pad_format(struct v4l2_subdev *sd,
1360 struct v4l2_subdev_pad_config *cfg,
1361 struct v4l2_subdev_format *fmt)
1363 struct ov13858 *ov13858 = to_ov13858(sd);
1364 const struct ov13858_mode *mode;
1365 struct v4l2_mbus_framefmt *framefmt;
1372 mutex_lock(&ov13858->mutex);
1374 /* Only one raw bayer(GRBG) order is supported */
1375 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
1376 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1378 mode = v4l2_find_nearest_size(supported_modes, width, height,
1379 fmt->format.width, fmt->format.height);
1380 ov13858_update_pad_format(mode, fmt);
1381 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1382 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1383 *framefmt = fmt->format;
1385 ov13858->cur_mode = mode;
1386 __v4l2_ctrl_s_ctrl(ov13858->link_freq, mode->link_freq_index);
1387 link_freq = link_freq_menu_items[mode->link_freq_index];
1388 pixel_rate = link_freq_to_pixel_rate(link_freq);
1389 __v4l2_ctrl_s_ctrl_int64(ov13858->pixel_rate, pixel_rate);
1391 /* Update limits and set FPS to default */
1392 vblank_def = ov13858->cur_mode->vts_def -
1393 ov13858->cur_mode->height;
1394 vblank_min = ov13858->cur_mode->vts_min -
1395 ov13858->cur_mode->height;
1396 __v4l2_ctrl_modify_range(
1397 ov13858->vblank, vblank_min,
1398 OV13858_VTS_MAX - ov13858->cur_mode->height, 1,
1400 __v4l2_ctrl_s_ctrl(ov13858->vblank, vblank_def);
1402 link_freq_configs[mode->link_freq_index].pixels_per_line
1403 - ov13858->cur_mode->width;
1404 __v4l2_ctrl_modify_range(ov13858->hblank, h_blank,
1405 h_blank, 1, h_blank);
1408 mutex_unlock(&ov13858->mutex);
1413 static int ov13858_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1415 *frames = OV13858_NUM_OF_SKIP_FRAMES;
1420 /* Start streaming */
1421 static int ov13858_start_streaming(struct ov13858 *ov13858)
1423 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1424 const struct ov13858_reg_list *reg_list;
1425 int ret, link_freq_index;
1427 /* Get out of from software reset */
1428 ret = ov13858_write_reg(ov13858, OV13858_REG_SOFTWARE_RST,
1429 OV13858_REG_VALUE_08BIT, OV13858_SOFTWARE_RST);
1431 dev_err(&client->dev, "%s failed to set powerup registers\n",
1437 link_freq_index = ov13858->cur_mode->link_freq_index;
1438 reg_list = &link_freq_configs[link_freq_index].reg_list;
1439 ret = ov13858_write_reg_list(ov13858, reg_list);
1441 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1445 /* Apply default values of current mode */
1446 reg_list = &ov13858->cur_mode->reg_list;
1447 ret = ov13858_write_reg_list(ov13858, reg_list);
1449 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1453 /* Apply customized values from user */
1454 ret = __v4l2_ctrl_handler_setup(ov13858->sd.ctrl_handler);
1458 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1459 OV13858_REG_VALUE_08BIT,
1460 OV13858_MODE_STREAMING);
1463 /* Stop streaming */
1464 static int ov13858_stop_streaming(struct ov13858 *ov13858)
1466 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1467 OV13858_REG_VALUE_08BIT, OV13858_MODE_STANDBY);
1470 static int ov13858_set_stream(struct v4l2_subdev *sd, int enable)
1472 struct ov13858 *ov13858 = to_ov13858(sd);
1473 struct i2c_client *client = v4l2_get_subdevdata(sd);
1476 mutex_lock(&ov13858->mutex);
1477 if (ov13858->streaming == enable) {
1478 mutex_unlock(&ov13858->mutex);
1483 ret = pm_runtime_get_sync(&client->dev);
1485 pm_runtime_put_noidle(&client->dev);
1490 * Apply default & customized values
1491 * and then start streaming.
1493 ret = ov13858_start_streaming(ov13858);
1497 ov13858_stop_streaming(ov13858);
1498 pm_runtime_put(&client->dev);
1501 ov13858->streaming = enable;
1502 mutex_unlock(&ov13858->mutex);
1507 pm_runtime_put(&client->dev);
1509 mutex_unlock(&ov13858->mutex);
1514 static int __maybe_unused ov13858_suspend(struct device *dev)
1516 struct i2c_client *client = to_i2c_client(dev);
1517 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1518 struct ov13858 *ov13858 = to_ov13858(sd);
1520 if (ov13858->streaming)
1521 ov13858_stop_streaming(ov13858);
1526 static int __maybe_unused ov13858_resume(struct device *dev)
1528 struct i2c_client *client = to_i2c_client(dev);
1529 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1530 struct ov13858 *ov13858 = to_ov13858(sd);
1533 if (ov13858->streaming) {
1534 ret = ov13858_start_streaming(ov13858);
1542 ov13858_stop_streaming(ov13858);
1543 ov13858->streaming = false;
1547 /* Verify chip ID */
1548 static int ov13858_identify_module(struct ov13858 *ov13858)
1550 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1554 ret = ov13858_read_reg(ov13858, OV13858_REG_CHIP_ID,
1555 OV13858_REG_VALUE_24BIT, &val);
1559 if (val != OV13858_CHIP_ID) {
1560 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1561 OV13858_CHIP_ID, val);
1568 static const struct v4l2_subdev_video_ops ov13858_video_ops = {
1569 .s_stream = ov13858_set_stream,
1572 static const struct v4l2_subdev_pad_ops ov13858_pad_ops = {
1573 .enum_mbus_code = ov13858_enum_mbus_code,
1574 .get_fmt = ov13858_get_pad_format,
1575 .set_fmt = ov13858_set_pad_format,
1576 .enum_frame_size = ov13858_enum_frame_size,
1579 static const struct v4l2_subdev_sensor_ops ov13858_sensor_ops = {
1580 .g_skip_frames = ov13858_get_skip_frames,
1583 static const struct v4l2_subdev_ops ov13858_subdev_ops = {
1584 .video = &ov13858_video_ops,
1585 .pad = &ov13858_pad_ops,
1586 .sensor = &ov13858_sensor_ops,
1589 static const struct media_entity_operations ov13858_subdev_entity_ops = {
1590 .link_validate = v4l2_subdev_link_validate,
1593 static const struct v4l2_subdev_internal_ops ov13858_internal_ops = {
1594 .open = ov13858_open,
1597 /* Initialize control handlers */
1598 static int ov13858_init_controls(struct ov13858 *ov13858)
1600 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1601 struct v4l2_ctrl_handler *ctrl_hdlr;
1608 const struct ov13858_mode *mode;
1611 ctrl_hdlr = &ov13858->ctrl_handler;
1612 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1616 mutex_init(&ov13858->mutex);
1617 ctrl_hdlr->lock = &ov13858->mutex;
1618 ov13858->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1621 OV13858_NUM_OF_LINK_FREQS - 1,
1623 link_freq_menu_items);
1624 ov13858->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1626 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1627 pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
1628 /* By default, PIXEL_RATE is read only */
1629 ov13858->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops,
1630 V4L2_CID_PIXEL_RATE,
1631 pixel_rate_min, pixel_rate_max,
1634 mode = ov13858->cur_mode;
1635 vblank_def = mode->vts_def - mode->height;
1636 vblank_min = mode->vts_min - mode->height;
1637 ov13858->vblank = v4l2_ctrl_new_std(
1638 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_VBLANK,
1639 vblank_min, OV13858_VTS_MAX - mode->height, 1,
1642 hblank = link_freq_configs[mode->link_freq_index].pixels_per_line -
1644 ov13858->hblank = v4l2_ctrl_new_std(
1645 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_HBLANK,
1646 hblank, hblank, 1, hblank);
1647 ov13858->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1649 exposure_max = mode->vts_def - 8;
1650 ov13858->exposure = v4l2_ctrl_new_std(
1651 ctrl_hdlr, &ov13858_ctrl_ops,
1652 V4L2_CID_EXPOSURE, OV13858_EXPOSURE_MIN,
1653 exposure_max, OV13858_EXPOSURE_STEP,
1654 OV13858_EXPOSURE_DEFAULT);
1656 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1657 OV13858_ANA_GAIN_MIN, OV13858_ANA_GAIN_MAX,
1658 OV13858_ANA_GAIN_STEP, OV13858_ANA_GAIN_DEFAULT);
1661 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1662 OV13858_DGTL_GAIN_MIN, OV13858_DGTL_GAIN_MAX,
1663 OV13858_DGTL_GAIN_STEP, OV13858_DGTL_GAIN_DEFAULT);
1665 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov13858_ctrl_ops,
1666 V4L2_CID_TEST_PATTERN,
1667 ARRAY_SIZE(ov13858_test_pattern_menu) - 1,
1668 0, 0, ov13858_test_pattern_menu);
1669 if (ctrl_hdlr->error) {
1670 ret = ctrl_hdlr->error;
1671 dev_err(&client->dev, "%s control init failed (%d)\n",
1676 ov13858->sd.ctrl_handler = ctrl_hdlr;
1681 v4l2_ctrl_handler_free(ctrl_hdlr);
1682 mutex_destroy(&ov13858->mutex);
1687 static void ov13858_free_controls(struct ov13858 *ov13858)
1689 v4l2_ctrl_handler_free(ov13858->sd.ctrl_handler);
1690 mutex_destroy(&ov13858->mutex);
1693 static int ov13858_probe(struct i2c_client *client,
1694 const struct i2c_device_id *devid)
1696 struct ov13858 *ov13858;
1700 device_property_read_u32(&client->dev, "clock-frequency", &val);
1701 if (val != 19200000)
1704 ov13858 = devm_kzalloc(&client->dev, sizeof(*ov13858), GFP_KERNEL);
1708 /* Initialize subdev */
1709 v4l2_i2c_subdev_init(&ov13858->sd, client, &ov13858_subdev_ops);
1711 /* Check module identity */
1712 ret = ov13858_identify_module(ov13858);
1714 dev_err(&client->dev, "failed to find sensor: %d\n", ret);
1718 /* Set default mode to max resolution */
1719 ov13858->cur_mode = &supported_modes[0];
1721 ret = ov13858_init_controls(ov13858);
1725 /* Initialize subdev */
1726 ov13858->sd.internal_ops = &ov13858_internal_ops;
1727 ov13858->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1728 ov13858->sd.entity.ops = &ov13858_subdev_entity_ops;
1729 ov13858->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1731 /* Initialize source pad */
1732 ov13858->pad.flags = MEDIA_PAD_FL_SOURCE;
1733 ret = media_entity_pads_init(&ov13858->sd.entity, 1, &ov13858->pad);
1735 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1736 goto error_handler_free;
1739 ret = v4l2_async_register_subdev_sensor_common(&ov13858->sd);
1741 goto error_media_entity;
1744 * Device is already turned on by i2c-core with ACPI domain PM.
1745 * Enable runtime PM and turn off the device.
1747 pm_runtime_get_noresume(&client->dev);
1748 pm_runtime_set_active(&client->dev);
1749 pm_runtime_enable(&client->dev);
1750 pm_runtime_put(&client->dev);
1755 media_entity_cleanup(&ov13858->sd.entity);
1758 ov13858_free_controls(ov13858);
1759 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1764 static int ov13858_remove(struct i2c_client *client)
1766 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1767 struct ov13858 *ov13858 = to_ov13858(sd);
1769 v4l2_async_unregister_subdev(sd);
1770 media_entity_cleanup(&sd->entity);
1771 ov13858_free_controls(ov13858);
1774 * Disable runtime PM but keep the device turned on.
1775 * i2c-core with ACPI domain PM will turn off the device.
1777 pm_runtime_get_sync(&client->dev);
1778 pm_runtime_disable(&client->dev);
1779 pm_runtime_set_suspended(&client->dev);
1780 pm_runtime_put_noidle(&client->dev);
1785 static const struct i2c_device_id ov13858_id_table[] = {
1790 MODULE_DEVICE_TABLE(i2c, ov13858_id_table);
1792 static const struct dev_pm_ops ov13858_pm_ops = {
1793 SET_SYSTEM_SLEEP_PM_OPS(ov13858_suspend, ov13858_resume)
1797 static const struct acpi_device_id ov13858_acpi_ids[] = {
1802 MODULE_DEVICE_TABLE(acpi, ov13858_acpi_ids);
1805 static struct i2c_driver ov13858_i2c_driver = {
1808 .owner = THIS_MODULE,
1809 .pm = &ov13858_pm_ops,
1810 .acpi_match_table = ACPI_PTR(ov13858_acpi_ids),
1812 .probe = ov13858_probe,
1813 .remove = ov13858_remove,
1814 .id_table = ov13858_id_table,
1817 module_i2c_driver(ov13858_i2c_driver);
1819 MODULE_AUTHOR("Kan, Chris <chris.kan@intel.com>");
1820 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
1821 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
1822 MODULE_DESCRIPTION("Omnivision ov13858 sensor driver");
1823 MODULE_LICENSE("GPL v2");