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 v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1421 struct ov8856
*ov8856
= to_ov8856(sd
);
1423 mutex_lock(&ov8856
->mutex
);
1424 if (ov8856
->streaming
)
1425 ov8856_stop_streaming(ov8856
);
1427 __ov8856_power_off(ov8856
);
1428 mutex_unlock(&ov8856
->mutex
);
1433 static int __maybe_unused
ov8856_resume(struct device
*dev
)
1435 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1436 struct ov8856
*ov8856
= to_ov8856(sd
);
1439 mutex_lock(&ov8856
->mutex
);
1441 __ov8856_power_on(ov8856
);
1442 if (ov8856
->streaming
) {
1443 ret
= ov8856_start_streaming(ov8856
);
1445 ov8856
->streaming
= false;
1446 ov8856_stop_streaming(ov8856
);
1447 mutex_unlock(&ov8856
->mutex
);
1452 mutex_unlock(&ov8856
->mutex
);
1457 static int ov8856_set_format(struct v4l2_subdev
*sd
,
1458 struct v4l2_subdev_pad_config
*cfg
,
1459 struct v4l2_subdev_format
*fmt
)
1461 struct ov8856
*ov8856
= to_ov8856(sd
);
1462 const struct ov8856_mode
*mode
;
1463 s32 vblank_def
, h_blank
;
1465 mode
= v4l2_find_nearest_size(supported_modes
,
1466 ARRAY_SIZE(supported_modes
), width
,
1467 height
, fmt
->format
.width
,
1468 fmt
->format
.height
);
1470 mutex_lock(&ov8856
->mutex
);
1471 ov8856_update_pad_format(mode
, &fmt
->format
);
1472 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1473 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
1475 ov8856
->cur_mode
= mode
;
1476 __v4l2_ctrl_s_ctrl(ov8856
->link_freq
, mode
->link_freq_index
);
1477 __v4l2_ctrl_s_ctrl_int64(ov8856
->pixel_rate
,
1478 to_pixel_rate(mode
->link_freq_index
));
1480 /* Update limits and set FPS to default */
1481 vblank_def
= mode
->vts_def
- mode
->height
;
1482 __v4l2_ctrl_modify_range(ov8856
->vblank
,
1483 mode
->vts_min
- mode
->height
,
1484 OV8856_VTS_MAX
- mode
->height
, 1,
1486 __v4l2_ctrl_s_ctrl(ov8856
->vblank
, vblank_def
);
1487 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
1489 __v4l2_ctrl_modify_range(ov8856
->hblank
, h_blank
, h_blank
, 1,
1493 mutex_unlock(&ov8856
->mutex
);
1498 static int ov8856_get_format(struct v4l2_subdev
*sd
,
1499 struct v4l2_subdev_pad_config
*cfg
,
1500 struct v4l2_subdev_format
*fmt
)
1502 struct ov8856
*ov8856
= to_ov8856(sd
);
1504 mutex_lock(&ov8856
->mutex
);
1505 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1506 fmt
->format
= *v4l2_subdev_get_try_format(&ov8856
->sd
, cfg
,
1509 ov8856_update_pad_format(ov8856
->cur_mode
, &fmt
->format
);
1511 mutex_unlock(&ov8856
->mutex
);
1516 static int ov8856_enum_mbus_code(struct v4l2_subdev
*sd
,
1517 struct v4l2_subdev_pad_config
*cfg
,
1518 struct v4l2_subdev_mbus_code_enum
*code
)
1520 /* Only one bayer order GRBG is supported */
1521 if (code
->index
> 0)
1524 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1529 static int ov8856_enum_frame_size(struct v4l2_subdev
*sd
,
1530 struct v4l2_subdev_pad_config
*cfg
,
1531 struct v4l2_subdev_frame_size_enum
*fse
)
1533 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1536 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1539 fse
->min_width
= supported_modes
[fse
->index
].width
;
1540 fse
->max_width
= fse
->min_width
;
1541 fse
->min_height
= supported_modes
[fse
->index
].height
;
1542 fse
->max_height
= fse
->min_height
;
1547 static int ov8856_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1549 struct ov8856
*ov8856
= to_ov8856(sd
);
1551 mutex_lock(&ov8856
->mutex
);
1552 ov8856_update_pad_format(&supported_modes
[0],
1553 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0));
1554 mutex_unlock(&ov8856
->mutex
);
1559 static const struct v4l2_subdev_video_ops ov8856_video_ops
= {
1560 .s_stream
= ov8856_set_stream
,
1563 static const struct v4l2_subdev_pad_ops ov8856_pad_ops
= {
1564 .set_fmt
= ov8856_set_format
,
1565 .get_fmt
= ov8856_get_format
,
1566 .enum_mbus_code
= ov8856_enum_mbus_code
,
1567 .enum_frame_size
= ov8856_enum_frame_size
,
1570 static const struct v4l2_subdev_ops ov8856_subdev_ops
= {
1571 .video
= &ov8856_video_ops
,
1572 .pad
= &ov8856_pad_ops
,
1575 static const struct media_entity_operations ov8856_subdev_entity_ops
= {
1576 .link_validate
= v4l2_subdev_link_validate
,
1579 static const struct v4l2_subdev_internal_ops ov8856_internal_ops
= {
1580 .open
= ov8856_open
,
1583 static int ov8856_identify_module(struct ov8856
*ov8856
)
1585 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
1589 ret
= ov8856_read_reg(ov8856
, OV8856_REG_CHIP_ID
,
1590 OV8856_REG_VALUE_24BIT
, &val
);
1594 if (val
!= OV8856_CHIP_ID
) {
1595 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1596 OV8856_CHIP_ID
, val
);
1600 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
1601 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STREAMING
);
1605 ret
= ov8856_write_reg(ov8856
, OV8856_OTP_MODE_CTRL
,
1606 OV8856_REG_VALUE_08BIT
, OV8856_OTP_MODE_AUTO
);
1608 dev_err(&client
->dev
, "failed to set otp mode");
1612 ret
= ov8856_write_reg(ov8856
, OV8856_OTP_LOAD_CTRL
,
1613 OV8856_REG_VALUE_08BIT
,
1614 OV8856_OTP_LOAD_CTRL_ENABLE
);
1616 dev_err(&client
->dev
, "failed to enable load control");
1620 ret
= ov8856_read_reg(ov8856
, OV8856_MODULE_REVISION
,
1621 OV8856_REG_VALUE_08BIT
, &val
);
1623 dev_err(&client
->dev
, "failed to read module revision");
1627 dev_info(&client
->dev
, "OV8856 revision %x (%s) at address 0x%02x\n",
1629 val
== OV8856_2A_MODULE
? "2A" :
1630 val
== OV8856_1B_MODULE
? "1B" : "unknown revision",
1633 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
1634 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STANDBY
);
1636 dev_err(&client
->dev
, "failed to exit streaming mode");
1643 static int ov8856_get_hwcfg(struct ov8856
*ov8856
, struct device
*dev
)
1645 struct fwnode_handle
*ep
;
1646 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1647 struct v4l2_fwnode_endpoint bus_cfg
= {
1648 .bus_type
= V4L2_MBUS_CSI2_DPHY
1657 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &xvclk_rate
);
1661 if (!is_acpi_node(fwnode
)) {
1662 ov8856
->xvclk
= devm_clk_get(dev
, "xvclk");
1663 if (IS_ERR(ov8856
->xvclk
)) {
1664 dev_err(dev
, "could not get xvclk clock (%pe)\n",
1666 return PTR_ERR(ov8856
->xvclk
);
1669 clk_set_rate(ov8856
->xvclk
, xvclk_rate
);
1670 xvclk_rate
= clk_get_rate(ov8856
->xvclk
);
1673 if (xvclk_rate
!= OV8856_XVCLK_19_2
)
1674 dev_warn(dev
, "external clock rate %u is unsupported",
1677 ov8856
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
1679 if (IS_ERR(ov8856
->reset_gpio
))
1680 return PTR_ERR(ov8856
->reset_gpio
);
1682 for (i
= 0; i
< ARRAY_SIZE(ov8856_supply_names
); i
++)
1683 ov8856
->supplies
[i
].supply
= ov8856_supply_names
[i
];
1685 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(ov8856_supply_names
),
1690 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1694 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1695 fwnode_handle_put(ep
);
1699 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV8856_DATA_LANES
) {
1700 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
1701 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1703 goto check_hwcfg_error
;
1706 if (!bus_cfg
.nr_of_link_frequencies
) {
1707 dev_err(dev
, "no link frequencies defined");
1709 goto check_hwcfg_error
;
1712 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1713 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1714 if (link_freq_menu_items
[i
] ==
1715 bus_cfg
.link_frequencies
[j
])
1719 if (j
== bus_cfg
.nr_of_link_frequencies
) {
1720 dev_err(dev
, "no link frequency %lld supported",
1721 link_freq_menu_items
[i
]);
1723 goto check_hwcfg_error
;
1728 v4l2_fwnode_endpoint_free(&bus_cfg
);
1733 static int ov8856_remove(struct i2c_client
*client
)
1735 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1736 struct ov8856
*ov8856
= to_ov8856(sd
);
1738 v4l2_async_unregister_subdev(sd
);
1739 media_entity_cleanup(&sd
->entity
);
1740 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1741 pm_runtime_disable(&client
->dev
);
1742 mutex_destroy(&ov8856
->mutex
);
1744 __ov8856_power_off(ov8856
);
1749 static int ov8856_probe(struct i2c_client
*client
)
1751 struct ov8856
*ov8856
;
1754 ov8856
= devm_kzalloc(&client
->dev
, sizeof(*ov8856
), GFP_KERNEL
);
1758 ret
= ov8856_get_hwcfg(ov8856
, &client
->dev
);
1760 dev_err(&client
->dev
, "failed to get HW configuration: %d",
1765 v4l2_i2c_subdev_init(&ov8856
->sd
, client
, &ov8856_subdev_ops
);
1767 ret
= __ov8856_power_on(ov8856
);
1769 dev_err(&client
->dev
, "failed to power on\n");
1773 ret
= ov8856_identify_module(ov8856
);
1775 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1776 goto probe_power_off
;
1779 mutex_init(&ov8856
->mutex
);
1780 ov8856
->cur_mode
= &supported_modes
[0];
1781 ret
= ov8856_init_controls(ov8856
);
1783 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1784 goto probe_error_v4l2_ctrl_handler_free
;
1787 ov8856
->sd
.internal_ops
= &ov8856_internal_ops
;
1788 ov8856
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1789 ov8856
->sd
.entity
.ops
= &ov8856_subdev_entity_ops
;
1790 ov8856
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1791 ov8856
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1792 ret
= media_entity_pads_init(&ov8856
->sd
.entity
, 1, &ov8856
->pad
);
1794 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1795 goto probe_error_v4l2_ctrl_handler_free
;
1798 ret
= v4l2_async_register_subdev_sensor_common(&ov8856
->sd
);
1800 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1802 goto probe_error_media_entity_cleanup
;
1806 * Device is already turned on by i2c-core with ACPI domain PM.
1807 * Enable runtime PM and turn off the device.
1809 pm_runtime_set_active(&client
->dev
);
1810 pm_runtime_enable(&client
->dev
);
1811 pm_runtime_idle(&client
->dev
);
1815 probe_error_media_entity_cleanup
:
1816 media_entity_cleanup(&ov8856
->sd
.entity
);
1818 probe_error_v4l2_ctrl_handler_free
:
1819 v4l2_ctrl_handler_free(ov8856
->sd
.ctrl_handler
);
1820 mutex_destroy(&ov8856
->mutex
);
1823 __ov8856_power_off(ov8856
);
1828 static const struct dev_pm_ops ov8856_pm_ops
= {
1829 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend
, ov8856_resume
)
1833 static const struct acpi_device_id ov8856_acpi_ids
[] = {
1838 MODULE_DEVICE_TABLE(acpi
, ov8856_acpi_ids
);
1841 static const struct of_device_id ov8856_of_match
[] = {
1842 { .compatible
= "ovti,ov8856" },
1845 MODULE_DEVICE_TABLE(of
, ov8856_of_match
);
1847 static struct i2c_driver ov8856_i2c_driver
= {
1850 .pm
= &ov8856_pm_ops
,
1851 .acpi_match_table
= ACPI_PTR(ov8856_acpi_ids
),
1852 .of_match_table
= ov8856_of_match
,
1854 .probe_new
= ov8856_probe
,
1855 .remove
= ov8856_remove
,
1858 module_i2c_driver(ov8856_i2c_driver
);
1860 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1861 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1862 MODULE_LICENSE("GPL v2");