1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
17 #define OV8856_REG_VALUE_08BIT 1
18 #define OV8856_REG_VALUE_16BIT 2
19 #define OV8856_REG_VALUE_24BIT 3
21 #define OV8856_LINK_FREQ_360MHZ 360000000ULL
22 #define OV8856_LINK_FREQ_180MHZ 180000000ULL
23 #define OV8856_SCLK 144000000ULL
24 #define OV8856_XVCLK_19_2 19200000
25 #define OV8856_DATA_LANES 4
26 #define OV8856_RGB_DEPTH 10
28 #define OV8856_REG_CHIP_ID 0x300a
29 #define OV8856_CHIP_ID 0x00885a
31 #define OV8856_REG_MODE_SELECT 0x0100
32 #define OV8856_MODE_STANDBY 0x00
33 #define OV8856_MODE_STREAMING 0x01
35 /* module revisions */
36 #define OV8856_2A_MODULE 0x01
37 #define OV8856_1B_MODULE 0x02
39 /* the OTP read-out buffer is at 0x7000 and 0xf is the offset
40 * of the byte in the OTP that means the module revision
42 #define OV8856_MODULE_REVISION 0x700f
43 #define OV8856_OTP_MODE_CTRL 0x3d84
44 #define OV8856_OTP_LOAD_CTRL 0x3d81
45 #define OV8856_OTP_MODE_AUTO 0x00
46 #define OV8856_OTP_LOAD_CTRL_ENABLE BIT(0)
48 /* vertical-timings from sensor */
49 #define OV8856_REG_VTS 0x380e
50 #define OV8856_VTS_MAX 0x7fff
52 /* horizontal-timings from sensor */
53 #define OV8856_REG_HTS 0x380c
55 /* Exposure controls from sensor */
56 #define OV8856_REG_EXPOSURE 0x3500
57 #define OV8856_EXPOSURE_MIN 6
58 #define OV8856_EXPOSURE_MAX_MARGIN 6
59 #define OV8856_EXPOSURE_STEP 1
61 /* Analog gain controls from sensor */
62 #define OV8856_REG_ANALOG_GAIN 0x3508
63 #define OV8856_ANAL_GAIN_MIN 128
64 #define OV8856_ANAL_GAIN_MAX 2047
65 #define OV8856_ANAL_GAIN_STEP 1
67 /* Digital gain controls from sensor */
68 #define OV8856_REG_MWB_R_GAIN 0x5019
69 #define OV8856_REG_MWB_G_GAIN 0x501b
70 #define OV8856_REG_MWB_B_GAIN 0x501d
71 #define OV8856_DGTL_GAIN_MIN 0
72 #define OV8856_DGTL_GAIN_MAX 4095
73 #define OV8856_DGTL_GAIN_STEP 1
74 #define OV8856_DGTL_GAIN_DEFAULT 1024
76 /* Test Pattern Control */
77 #define OV8856_REG_TEST_PATTERN 0x5e00
78 #define OV8856_TEST_PATTERN_ENABLE BIT(7)
79 #define OV8856_TEST_PATTERN_BAR_SHIFT 2
81 #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
83 static const char * const ov8856_supply_names[] = {
84 "dovdd", /* Digital I/O power */
85 "avdd", /* Analog power */
86 "dvdd", /* Digital core power */
90 OV8856_LINK_FREQ_720MBPS,
91 OV8856_LINK_FREQ_360MBPS,
99 struct ov8856_reg_list {
101 const struct ov8856_reg *regs;
104 struct ov8856_link_freq_config {
105 const struct ov8856_reg_list reg_list;
109 /* Frame width in pixels */
112 /* Frame height in pixels */
115 /* Horizontal timining size */
118 /* Default vertical timining size */
121 /* Min vertical timining size */
124 /* Link frequency needed for this resolution */
127 /* Sensor register settings for this resolution */
128 const struct ov8856_reg_list reg_list;
131 static const struct ov8856_reg mipi_data_rate_720mbps[] = {
141 static const struct ov8856_reg mipi_data_rate_360mbps[] = {
151 static const struct ov8856_reg mode_3280x2464_regs[] = {
341 static const struct ov8856_reg mode_3264x2448_regs[] = {
544 static const struct ov8856_reg mode_1640x1232_regs[] = {
734 static const struct ov8856_reg mode_1632x1224_regs[] = {
937 static const char * const ov8856_test_pattern_menu[] = {
939 "Standard Color Bar",
940 "Top-Bottom Darker Color Bar",
941 "Right-Left Darker Color Bar",
942 "Bottom-Top Darker Color Bar"
945 static const s64 link_freq_menu_items[] = {
946 OV8856_LINK_FREQ_360MHZ,
947 OV8856_LINK_FREQ_180MHZ
950 static const struct ov8856_link_freq_config link_freq_configs[] = {
951 [OV8856_LINK_FREQ_720MBPS] = {
953 .num_of_regs = ARRAY_SIZE(mipi_data_rate_720mbps),
954 .regs = mipi_data_rate_720mbps,
957 [OV8856_LINK_FREQ_360MBPS] = {
959 .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
960 .regs = mipi_data_rate_360mbps,
965 static const struct ov8856_mode supported_modes[] = {
973 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
974 .regs = mode_3280x2464_regs,
976 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
985 .num_of_regs = ARRAY_SIZE(mode_3264x2448_regs),
986 .regs = mode_3264x2448_regs,
988 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
997 .num_of_regs = ARRAY_SIZE(mode_1640x1232_regs),
998 .regs = mode_1640x1232_regs,
1000 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
1009 .num_of_regs = ARRAY_SIZE(mode_1632x1224_regs),
1010 .regs = mode_1632x1224_regs,
1012 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
1017 struct v4l2_subdev sd;
1018 struct media_pad pad;
1019 struct v4l2_ctrl_handler ctrl_handler;
1022 struct gpio_desc *reset_gpio;
1023 struct regulator_bulk_data supplies[ARRAY_SIZE(ov8856_supply_names)];
1026 struct v4l2_ctrl *link_freq;
1027 struct v4l2_ctrl *pixel_rate;
1028 struct v4l2_ctrl *vblank;
1029 struct v4l2_ctrl *hblank;
1030 struct v4l2_ctrl *exposure;
1033 const struct ov8856_mode *cur_mode;
1035 /* To serialize asynchronus callbacks */
1038 /* Streaming on/off */
1042 static u64 to_pixel_rate(u32 f_index)
1044 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
1046 do_div(pixel_rate, OV8856_RGB_DEPTH);
1051 static u64 to_pixels_per_line(u32 hts, u32 f_index)
1053 u64 ppl = hts * to_pixel_rate(f_index);
1055 do_div(ppl, OV8856_SCLK);
1060 static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
1062 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1063 struct i2c_msg msgs[2];
1065 u8 data_buf[4] = {0};
1071 put_unaligned_be16(reg, addr_buf);
1072 msgs[0].addr = client->addr;
1074 msgs[0].len = sizeof(addr_buf);
1075 msgs[0].buf = addr_buf;
1076 msgs[1].addr = client->addr;
1077 msgs[1].flags = I2C_M_RD;
1079 msgs[1].buf = &data_buf[4 - len];
1081 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1082 if (ret != ARRAY_SIZE(msgs))
1085 *val = get_unaligned_be32(data_buf);
1090 static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
1092 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1098 put_unaligned_be16(reg, buf);
1099 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
1100 if (i2c_master_send(client, buf, len + 2) != len + 2)
1106 static int ov8856_write_reg_list(struct ov8856 *ov8856,
1107 const struct ov8856_reg_list *r_list)
1109 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1113 for (i = 0; i < r_list->num_of_regs; i++) {
1114 ret = ov8856_write_reg(ov8856, r_list->regs[i].address, 1,
1115 r_list->regs[i].val);
1117 dev_err_ratelimited(&client->dev,
1118 "failed to write reg 0x%4.4x. error = %d",
1119 r_list->regs[i].address, ret);
1127 static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
1131 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
1132 OV8856_REG_VALUE_16BIT, d_gain);
1136 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
1137 OV8856_REG_VALUE_16BIT, d_gain);
1141 return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
1142 OV8856_REG_VALUE_16BIT, d_gain);
1145 static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
1148 pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
1149 OV8856_TEST_PATTERN_ENABLE;
1151 return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
1152 OV8856_REG_VALUE_08BIT, pattern);
1155 static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
1157 struct ov8856 *ov8856 = container_of(ctrl->handler,
1158 struct ov8856, ctrl_handler);
1159 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1163 /* Propagate change of current control to all related controls */
1164 if (ctrl->id == V4L2_CID_VBLANK) {
1165 /* Update max exposure while meeting expected vblanking */
1166 exposure_max = ov8856->cur_mode->height + ctrl->val -
1167 OV8856_EXPOSURE_MAX_MARGIN;
1168 __v4l2_ctrl_modify_range(ov8856->exposure,
1169 ov8856->exposure->minimum,
1170 exposure_max, ov8856->exposure->step,
1174 /* V4L2 controls values will be applied only when power is already up */
1175 if (!pm_runtime_get_if_in_use(&client->dev))
1179 case V4L2_CID_ANALOGUE_GAIN:
1180 ret = ov8856_write_reg(ov8856, OV8856_REG_ANALOG_GAIN,
1181 OV8856_REG_VALUE_16BIT, ctrl->val);
1184 case V4L2_CID_DIGITAL_GAIN:
1185 ret = ov8856_update_digital_gain(ov8856, ctrl->val);
1188 case V4L2_CID_EXPOSURE:
1189 /* 4 least significant bits of expsoure are fractional part */
1190 ret = ov8856_write_reg(ov8856, OV8856_REG_EXPOSURE,
1191 OV8856_REG_VALUE_24BIT, ctrl->val << 4);
1194 case V4L2_CID_VBLANK:
1195 ret = ov8856_write_reg(ov8856, OV8856_REG_VTS,
1196 OV8856_REG_VALUE_16BIT,
1197 ov8856->cur_mode->height + ctrl->val);
1200 case V4L2_CID_TEST_PATTERN:
1201 ret = ov8856_test_pattern(ov8856, ctrl->val);
1209 pm_runtime_put(&client->dev);
1214 static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
1215 .s_ctrl = ov8856_set_ctrl,
1218 static int ov8856_init_controls(struct ov8856 *ov8856)
1220 struct v4l2_ctrl_handler *ctrl_hdlr;
1221 s64 exposure_max, h_blank;
1224 ctrl_hdlr = &ov8856->ctrl_handler;
1225 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1229 ctrl_hdlr->lock = &ov8856->mutex;
1230 ov8856->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov8856_ctrl_ops,
1232 ARRAY_SIZE(link_freq_menu_items) - 1,
1233 0, link_freq_menu_items);
1234 if (ov8856->link_freq)
1235 ov8856->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1237 ov8856->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1238 V4L2_CID_PIXEL_RATE, 0,
1239 to_pixel_rate(OV8856_LINK_FREQ_720MBPS),
1241 to_pixel_rate(OV8856_LINK_FREQ_720MBPS));
1242 ov8856->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1244 ov8856->cur_mode->vts_min - ov8856->cur_mode->height,
1245 OV8856_VTS_MAX - ov8856->cur_mode->height, 1,
1246 ov8856->cur_mode->vts_def - ov8856->cur_mode->height);
1247 h_blank = to_pixels_per_line(ov8856->cur_mode->hts,
1248 ov8856->cur_mode->link_freq_index) - ov8856->cur_mode->width;
1249 ov8856->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1250 V4L2_CID_HBLANK, h_blank, h_blank, 1,
1253 ov8856->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1255 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1256 OV8856_ANAL_GAIN_MIN, OV8856_ANAL_GAIN_MAX,
1257 OV8856_ANAL_GAIN_STEP, OV8856_ANAL_GAIN_MIN);
1258 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1259 OV8856_DGTL_GAIN_MIN, OV8856_DGTL_GAIN_MAX,
1260 OV8856_DGTL_GAIN_STEP, OV8856_DGTL_GAIN_DEFAULT);
1261 exposure_max = ov8856->cur_mode->vts_def - OV8856_EXPOSURE_MAX_MARGIN;
1262 ov8856->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1264 OV8856_EXPOSURE_MIN, exposure_max,
1265 OV8856_EXPOSURE_STEP,
1267 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov8856_ctrl_ops,
1268 V4L2_CID_TEST_PATTERN,
1269 ARRAY_SIZE(ov8856_test_pattern_menu) - 1,
1270 0, 0, ov8856_test_pattern_menu);
1271 if (ctrl_hdlr->error)
1272 return ctrl_hdlr->error;
1274 ov8856->sd.ctrl_handler = ctrl_hdlr;
1279 static void ov8856_update_pad_format(const struct ov8856_mode *mode,
1280 struct v4l2_mbus_framefmt *fmt)
1282 fmt->width = mode->width;
1283 fmt->height = mode->height;
1284 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1285 fmt->field = V4L2_FIELD_NONE;
1288 static int ov8856_start_streaming(struct ov8856 *ov8856)
1290 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1291 const struct ov8856_reg_list *reg_list;
1292 int link_freq_index, ret;
1294 link_freq_index = ov8856->cur_mode->link_freq_index;
1295 reg_list = &link_freq_configs[link_freq_index].reg_list;
1296 ret = ov8856_write_reg_list(ov8856, reg_list);
1298 dev_err(&client->dev, "failed to set plls");
1302 reg_list = &ov8856->cur_mode->reg_list;
1303 ret = ov8856_write_reg_list(ov8856, reg_list);
1305 dev_err(&client->dev, "failed to set mode");
1309 ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
1313 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1314 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
1316 dev_err(&client->dev, "failed to set stream");
1323 static void ov8856_stop_streaming(struct ov8856 *ov8856)
1325 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1327 if (ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1328 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY))
1329 dev_err(&client->dev, "failed to set stream");
1332 static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
1334 struct ov8856 *ov8856 = to_ov8856(sd);
1335 struct i2c_client *client = v4l2_get_subdevdata(sd);
1338 if (ov8856->streaming == enable)
1341 mutex_lock(&ov8856->mutex);
1343 ret = pm_runtime_get_sync(&client->dev);
1345 pm_runtime_put_noidle(&client->dev);
1346 mutex_unlock(&ov8856->mutex);
1350 ret = ov8856_start_streaming(ov8856);
1353 ov8856_stop_streaming(ov8856);
1354 pm_runtime_put(&client->dev);
1357 ov8856_stop_streaming(ov8856);
1358 pm_runtime_put(&client->dev);
1361 ov8856->streaming = enable;
1362 mutex_unlock(&ov8856->mutex);
1367 static int __ov8856_power_on(struct ov8856 *ov8856)
1369 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1372 if (is_acpi_node(dev_fwnode(&client->dev)))
1375 ret = clk_prepare_enable(ov8856->xvclk);
1377 dev_err(&client->dev, "failed to enable xvclk\n");
1381 if (ov8856->reset_gpio) {
1382 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1383 usleep_range(1000, 2000);
1386 ret = regulator_bulk_enable(ARRAY_SIZE(ov8856_supply_names),
1389 dev_err(&client->dev, "failed to enable regulators\n");
1393 gpiod_set_value_cansleep(ov8856->reset_gpio, 0);
1394 usleep_range(1500, 1800);
1399 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1400 clk_disable_unprepare(ov8856->xvclk);
1405 static void __ov8856_power_off(struct ov8856 *ov8856)
1407 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1409 if (is_acpi_node(dev_fwnode(&client->dev)))
1412 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1413 regulator_bulk_disable(ARRAY_SIZE(ov8856_supply_names),
1415 clk_disable_unprepare(ov8856->xvclk);
1418 static int __maybe_unused ov8856_suspend(struct device *dev)
1420 struct i2c_client *client = to_i2c_client(dev);
1421 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1422 struct ov8856 *ov8856 = to_ov8856(sd);
1424 mutex_lock(&ov8856->mutex);
1425 if (ov8856->streaming)
1426 ov8856_stop_streaming(ov8856);
1428 __ov8856_power_off(ov8856);
1429 mutex_unlock(&ov8856->mutex);
1434 static int __maybe_unused ov8856_resume(struct device *dev)
1436 struct i2c_client *client = to_i2c_client(dev);
1437 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1438 struct ov8856 *ov8856 = to_ov8856(sd);
1441 mutex_lock(&ov8856->mutex);
1443 __ov8856_power_on(ov8856);
1444 if (ov8856->streaming) {
1445 ret = ov8856_start_streaming(ov8856);
1447 ov8856->streaming = false;
1448 ov8856_stop_streaming(ov8856);
1449 mutex_unlock(&ov8856->mutex);
1454 mutex_unlock(&ov8856->mutex);
1459 static int ov8856_set_format(struct v4l2_subdev *sd,
1460 struct v4l2_subdev_pad_config *cfg,
1461 struct v4l2_subdev_format *fmt)
1463 struct ov8856 *ov8856 = to_ov8856(sd);
1464 const struct ov8856_mode *mode;
1465 s32 vblank_def, h_blank;
1467 mode = v4l2_find_nearest_size(supported_modes,
1468 ARRAY_SIZE(supported_modes), width,
1469 height, fmt->format.width,
1470 fmt->format.height);
1472 mutex_lock(&ov8856->mutex);
1473 ov8856_update_pad_format(mode, &fmt->format);
1474 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1475 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1477 ov8856->cur_mode = mode;
1478 __v4l2_ctrl_s_ctrl(ov8856->link_freq, mode->link_freq_index);
1479 __v4l2_ctrl_s_ctrl_int64(ov8856->pixel_rate,
1480 to_pixel_rate(mode->link_freq_index));
1482 /* Update limits and set FPS to default */
1483 vblank_def = mode->vts_def - mode->height;
1484 __v4l2_ctrl_modify_range(ov8856->vblank,
1485 mode->vts_min - mode->height,
1486 OV8856_VTS_MAX - mode->height, 1,
1488 __v4l2_ctrl_s_ctrl(ov8856->vblank, vblank_def);
1489 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1491 __v4l2_ctrl_modify_range(ov8856->hblank, h_blank, h_blank, 1,
1495 mutex_unlock(&ov8856->mutex);
1500 static int ov8856_get_format(struct v4l2_subdev *sd,
1501 struct v4l2_subdev_pad_config *cfg,
1502 struct v4l2_subdev_format *fmt)
1504 struct ov8856 *ov8856 = to_ov8856(sd);
1506 mutex_lock(&ov8856->mutex);
1507 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1508 fmt->format = *v4l2_subdev_get_try_format(&ov8856->sd, cfg,
1511 ov8856_update_pad_format(ov8856->cur_mode, &fmt->format);
1513 mutex_unlock(&ov8856->mutex);
1518 static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
1519 struct v4l2_subdev_pad_config *cfg,
1520 struct v4l2_subdev_mbus_code_enum *code)
1522 /* Only one bayer order GRBG is supported */
1523 if (code->index > 0)
1526 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1531 static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
1532 struct v4l2_subdev_pad_config *cfg,
1533 struct v4l2_subdev_frame_size_enum *fse)
1535 if (fse->index >= ARRAY_SIZE(supported_modes))
1538 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1541 fse->min_width = supported_modes[fse->index].width;
1542 fse->max_width = fse->min_width;
1543 fse->min_height = supported_modes[fse->index].height;
1544 fse->max_height = fse->min_height;
1549 static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1551 struct ov8856 *ov8856 = to_ov8856(sd);
1553 mutex_lock(&ov8856->mutex);
1554 ov8856_update_pad_format(&supported_modes[0],
1555 v4l2_subdev_get_try_format(sd, fh->pad, 0));
1556 mutex_unlock(&ov8856->mutex);
1561 static const struct v4l2_subdev_video_ops ov8856_video_ops = {
1562 .s_stream = ov8856_set_stream,
1565 static const struct v4l2_subdev_pad_ops ov8856_pad_ops = {
1566 .set_fmt = ov8856_set_format,
1567 .get_fmt = ov8856_get_format,
1568 .enum_mbus_code = ov8856_enum_mbus_code,
1569 .enum_frame_size = ov8856_enum_frame_size,
1572 static const struct v4l2_subdev_ops ov8856_subdev_ops = {
1573 .video = &ov8856_video_ops,
1574 .pad = &ov8856_pad_ops,
1577 static const struct media_entity_operations ov8856_subdev_entity_ops = {
1578 .link_validate = v4l2_subdev_link_validate,
1581 static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
1582 .open = ov8856_open,
1585 static int ov8856_identify_module(struct ov8856 *ov8856)
1587 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1591 ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
1592 OV8856_REG_VALUE_24BIT, &val);
1596 if (val != OV8856_CHIP_ID) {
1597 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1598 OV8856_CHIP_ID, val);
1602 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1603 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
1607 ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL,
1608 OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO);
1610 dev_err(&client->dev, "failed to set otp mode");
1614 ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL,
1615 OV8856_REG_VALUE_08BIT,
1616 OV8856_OTP_LOAD_CTRL_ENABLE);
1618 dev_err(&client->dev, "failed to enable load control");
1622 ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION,
1623 OV8856_REG_VALUE_08BIT, &val);
1625 dev_err(&client->dev, "failed to read module revision");
1629 dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n",
1631 val == OV8856_2A_MODULE ? "2A" :
1632 val == OV8856_1B_MODULE ? "1B" : "unknown revision",
1635 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1636 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY);
1638 dev_err(&client->dev, "failed to exit streaming mode");
1645 static int ov8856_get_hwcfg(struct ov8856 *ov8856, struct device *dev)
1647 struct fwnode_handle *ep;
1648 struct fwnode_handle *fwnode = dev_fwnode(dev);
1649 struct v4l2_fwnode_endpoint bus_cfg = {
1650 .bus_type = V4L2_MBUS_CSI2_DPHY
1659 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
1663 if (!is_acpi_node(fwnode)) {
1664 ov8856->xvclk = devm_clk_get(dev, "xvclk");
1665 if (IS_ERR(ov8856->xvclk)) {
1666 dev_err(dev, "could not get xvclk clock (%pe)\n",
1668 return PTR_ERR(ov8856->xvclk);
1671 clk_set_rate(ov8856->xvclk, xvclk_rate);
1672 xvclk_rate = clk_get_rate(ov8856->xvclk);
1675 if (xvclk_rate != OV8856_XVCLK_19_2)
1676 dev_warn(dev, "external clock rate %u is unsupported",
1679 ov8856->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1681 if (IS_ERR(ov8856->reset_gpio))
1682 return PTR_ERR(ov8856->reset_gpio);
1684 for (i = 0; i < ARRAY_SIZE(ov8856_supply_names); i++)
1685 ov8856->supplies[i].supply = ov8856_supply_names[i];
1687 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov8856_supply_names),
1692 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1696 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1697 fwnode_handle_put(ep);
1701 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
1702 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1703 bus_cfg.bus.mipi_csi2.num_data_lanes);
1705 goto check_hwcfg_error;
1708 if (!bus_cfg.nr_of_link_frequencies) {
1709 dev_err(dev, "no link frequencies defined");
1711 goto check_hwcfg_error;
1714 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1715 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1716 if (link_freq_menu_items[i] ==
1717 bus_cfg.link_frequencies[j])
1721 if (j == bus_cfg.nr_of_link_frequencies) {
1722 dev_err(dev, "no link frequency %lld supported",
1723 link_freq_menu_items[i]);
1725 goto check_hwcfg_error;
1730 v4l2_fwnode_endpoint_free(&bus_cfg);
1735 static int ov8856_remove(struct i2c_client *client)
1737 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1738 struct ov8856 *ov8856 = to_ov8856(sd);
1740 v4l2_async_unregister_subdev(sd);
1741 media_entity_cleanup(&sd->entity);
1742 v4l2_ctrl_handler_free(sd->ctrl_handler);
1743 pm_runtime_disable(&client->dev);
1744 mutex_destroy(&ov8856->mutex);
1746 __ov8856_power_off(ov8856);
1751 static int ov8856_probe(struct i2c_client *client)
1753 struct ov8856 *ov8856;
1756 ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
1760 ret = ov8856_get_hwcfg(ov8856, &client->dev);
1762 dev_err(&client->dev, "failed to get HW configuration: %d",
1767 v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
1769 ret = __ov8856_power_on(ov8856);
1771 dev_err(&client->dev, "failed to power on\n");
1775 ret = ov8856_identify_module(ov8856);
1777 dev_err(&client->dev, "failed to find sensor: %d", ret);
1778 goto probe_power_off;
1781 mutex_init(&ov8856->mutex);
1782 ov8856->cur_mode = &supported_modes[0];
1783 ret = ov8856_init_controls(ov8856);
1785 dev_err(&client->dev, "failed to init controls: %d", ret);
1786 goto probe_error_v4l2_ctrl_handler_free;
1789 ov8856->sd.internal_ops = &ov8856_internal_ops;
1790 ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1791 ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
1792 ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1793 ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
1794 ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
1796 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1797 goto probe_error_v4l2_ctrl_handler_free;
1800 ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
1802 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1804 goto probe_error_media_entity_cleanup;
1808 * Device is already turned on by i2c-core with ACPI domain PM.
1809 * Enable runtime PM and turn off the device.
1811 pm_runtime_set_active(&client->dev);
1812 pm_runtime_enable(&client->dev);
1813 pm_runtime_idle(&client->dev);
1817 probe_error_media_entity_cleanup:
1818 media_entity_cleanup(&ov8856->sd.entity);
1820 probe_error_v4l2_ctrl_handler_free:
1821 v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
1822 mutex_destroy(&ov8856->mutex);
1825 __ov8856_power_off(ov8856);
1830 static const struct dev_pm_ops ov8856_pm_ops = {
1831 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
1835 static const struct acpi_device_id ov8856_acpi_ids[] = {
1840 MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
1843 static const struct of_device_id ov8856_of_match[] = {
1844 { .compatible = "ovti,ov8856" },
1847 MODULE_DEVICE_TABLE(of, ov8856_of_match);
1849 static struct i2c_driver ov8856_i2c_driver = {
1852 .pm = &ov8856_pm_ops,
1853 .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
1854 .of_match_table = ov8856_of_match,
1856 .probe_new = ov8856_probe,
1857 .remove = ov8856_remove,
1860 module_i2c_driver(ov8856_i2c_driver);
1862 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1863 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1864 MODULE_LICENSE("GPL v2");