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
[] = {
365 static const struct ov13858_reg mode_2112x1568_regs
[] = {
555 static const struct ov13858_reg mode_2112x1188_regs
[] = {
745 static const struct ov13858_reg mode_1056x784_regs
[] = {
935 static const char * const ov13858_test_pattern_menu
[] = {
937 "Vertical Color Bar Type 1",
938 "Vertical Color Bar Type 2",
939 "Vertical Color Bar Type 3",
940 "Vertical Color Bar Type 4"
943 /* Configurations for supported link frequencies */
944 #define OV13858_NUM_OF_LINK_FREQS 2
945 #define OV13858_LINK_FREQ_540MHZ 540000000ULL
946 #define OV13858_LINK_FREQ_270MHZ 270000000ULL
947 #define OV13858_LINK_FREQ_INDEX_0 0
948 #define OV13858_LINK_FREQ_INDEX_1 1
951 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
952 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
954 static u64
link_freq_to_pixel_rate(u64 f
)
962 /* Menu items for LINK_FREQ V4L2 control */
963 static const s64 link_freq_menu_items
[OV13858_NUM_OF_LINK_FREQS
] = {
964 OV13858_LINK_FREQ_540MHZ
,
965 OV13858_LINK_FREQ_270MHZ
968 /* Link frequency configs */
969 static const struct ov13858_link_freq_config
970 link_freq_configs
[OV13858_NUM_OF_LINK_FREQS
] = {
972 .pixels_per_line
= OV13858_PPL_540MHZ
,
974 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_1080mbps
),
975 .regs
= mipi_data_rate_1080mbps
,
979 .pixels_per_line
= OV13858_PPL_270MHZ
,
981 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_540mbps
),
982 .regs
= mipi_data_rate_540mbps
,
988 static const struct ov13858_mode supported_modes
[] = {
992 .vts_def
= OV13858_VTS_30FPS
,
993 .vts_min
= OV13858_VTS_30FPS
,
995 .num_of_regs
= ARRAY_SIZE(mode_4224x3136_regs
),
996 .regs
= mode_4224x3136_regs
,
998 .link_freq_index
= OV13858_LINK_FREQ_INDEX_0
,
1003 .vts_def
= OV13858_VTS_30FPS
,
1006 .num_of_regs
= ARRAY_SIZE(mode_2112x1568_regs
),
1007 .regs
= mode_2112x1568_regs
,
1009 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1014 .vts_def
= OV13858_VTS_30FPS
,
1017 .num_of_regs
= ARRAY_SIZE(mode_2112x1188_regs
),
1018 .regs
= mode_2112x1188_regs
,
1020 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1025 .vts_def
= OV13858_VTS_30FPS
,
1028 .num_of_regs
= ARRAY_SIZE(mode_1056x784_regs
),
1029 .regs
= mode_1056x784_regs
,
1031 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1036 struct v4l2_subdev sd
;
1037 struct media_pad pad
;
1039 struct v4l2_ctrl_handler ctrl_handler
;
1041 struct v4l2_ctrl
*link_freq
;
1042 struct v4l2_ctrl
*pixel_rate
;
1043 struct v4l2_ctrl
*vblank
;
1044 struct v4l2_ctrl
*hblank
;
1045 struct v4l2_ctrl
*exposure
;
1048 const struct ov13858_mode
*cur_mode
;
1050 /* Mutex for serialized access */
1053 /* Streaming on/off */
1057 #define to_ov13858(_sd) container_of(_sd, struct ov13858, sd)
1059 /* Read registers up to 4 at a time */
1060 static int ov13858_read_reg(struct ov13858
*ov13858
, u16 reg
, u32 len
, u32
*val
)
1062 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1063 struct i2c_msg msgs
[2];
1067 u16 reg_addr_be
= cpu_to_be16(reg
);
1072 data_be_p
= (u8
*)&data_be
;
1073 /* Write register address */
1074 msgs
[0].addr
= client
->addr
;
1077 msgs
[0].buf
= (u8
*)®_addr_be
;
1079 /* Read data from register */
1080 msgs
[1].addr
= client
->addr
;
1081 msgs
[1].flags
= I2C_M_RD
;
1083 msgs
[1].buf
= &data_be_p
[4 - len
];
1085 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1086 if (ret
!= ARRAY_SIZE(msgs
))
1089 *val
= be32_to_cpu(data_be
);
1094 /* Write registers up to 4 at a time */
1095 static int ov13858_write_reg(struct ov13858
*ov13858
, u16 reg
, u32 len
, u32 val
)
1097 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1105 buf
[1] = reg
& 0xff;
1107 val
= cpu_to_be32(val
);
1113 buf
[buf_i
++] = val_p
[val_i
++];
1115 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1121 /* Write a list of registers */
1122 static int ov13858_write_regs(struct ov13858
*ov13858
,
1123 const struct ov13858_reg
*regs
, u32 len
)
1125 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1129 for (i
= 0; i
< len
; i
++) {
1130 ret
= ov13858_write_reg(ov13858
, regs
[i
].address
, 1,
1133 dev_err_ratelimited(
1135 "Failed to write reg 0x%4.4x. error = %d\n",
1136 regs
[i
].address
, ret
);
1145 static int ov13858_write_reg_list(struct ov13858
*ov13858
,
1146 const struct ov13858_reg_list
*r_list
)
1148 return ov13858_write_regs(ov13858
, r_list
->regs
, r_list
->num_of_regs
);
1151 /* Open sub-device */
1152 static int ov13858_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1154 struct ov13858
*ov13858
= to_ov13858(sd
);
1155 struct v4l2_mbus_framefmt
*try_fmt
= v4l2_subdev_get_try_format(sd
,
1159 mutex_lock(&ov13858
->mutex
);
1161 /* Initialize try_fmt */
1162 try_fmt
->width
= ov13858
->cur_mode
->width
;
1163 try_fmt
->height
= ov13858
->cur_mode
->height
;
1164 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1165 try_fmt
->field
= V4L2_FIELD_NONE
;
1167 /* No crop or compose */
1168 mutex_unlock(&ov13858
->mutex
);
1173 static int ov13858_update_digital_gain(struct ov13858
*ov13858
, u32 d_gain
)
1177 ret
= ov13858_write_reg(ov13858
, OV13858_REG_B_MWB_GAIN
,
1178 OV13858_REG_VALUE_16BIT
, d_gain
);
1182 ret
= ov13858_write_reg(ov13858
, OV13858_REG_G_MWB_GAIN
,
1183 OV13858_REG_VALUE_16BIT
, d_gain
);
1187 ret
= ov13858_write_reg(ov13858
, OV13858_REG_R_MWB_GAIN
,
1188 OV13858_REG_VALUE_16BIT
, d_gain
);
1193 static int ov13858_enable_test_pattern(struct ov13858
*ov13858
, u32 pattern
)
1198 ret
= ov13858_read_reg(ov13858
, OV13858_REG_TEST_PATTERN
,
1199 OV13858_REG_VALUE_08BIT
, &val
);
1204 val
&= OV13858_TEST_PATTERN_MASK
;
1205 val
|= (pattern
- 1) | OV13858_TEST_PATTERN_ENABLE
;
1207 val
&= ~OV13858_TEST_PATTERN_ENABLE
;
1210 return ov13858_write_reg(ov13858
, OV13858_REG_TEST_PATTERN
,
1211 OV13858_REG_VALUE_08BIT
, val
);
1214 static int ov13858_set_ctrl(struct v4l2_ctrl
*ctrl
)
1216 struct ov13858
*ov13858
= container_of(ctrl
->handler
,
1217 struct ov13858
, ctrl_handler
);
1218 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1222 /* Propagate change of current control to all related controls */
1224 case V4L2_CID_VBLANK
:
1225 /* Update max exposure while meeting expected vblanking */
1226 max
= ov13858
->cur_mode
->height
+ ctrl
->val
- 8;
1227 __v4l2_ctrl_modify_range(ov13858
->exposure
,
1228 ov13858
->exposure
->minimum
,
1229 max
, ov13858
->exposure
->step
, max
);
1234 * Applying V4L2 control value only happens
1235 * when power is up for streaming
1237 if (pm_runtime_get_if_in_use(&client
->dev
) <= 0)
1242 case V4L2_CID_ANALOGUE_GAIN
:
1243 ret
= ov13858_write_reg(ov13858
, OV13858_REG_ANALOG_GAIN
,
1244 OV13858_REG_VALUE_16BIT
, ctrl
->val
);
1246 case V4L2_CID_DIGITAL_GAIN
:
1247 ret
= ov13858_update_digital_gain(ov13858
, ctrl
->val
);
1249 case V4L2_CID_EXPOSURE
:
1250 ret
= ov13858_write_reg(ov13858
, OV13858_REG_EXPOSURE
,
1251 OV13858_REG_VALUE_24BIT
,
1254 case V4L2_CID_VBLANK
:
1255 /* Update VTS that meets expected vertical blanking */
1256 ret
= ov13858_write_reg(ov13858
, OV13858_REG_VTS
,
1257 OV13858_REG_VALUE_16BIT
,
1258 ov13858
->cur_mode
->height
1261 case V4L2_CID_TEST_PATTERN
:
1262 ret
= ov13858_enable_test_pattern(ov13858
, ctrl
->val
);
1265 dev_info(&client
->dev
,
1266 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1267 ctrl
->id
, ctrl
->val
);
1271 pm_runtime_put(&client
->dev
);
1276 static const struct v4l2_ctrl_ops ov13858_ctrl_ops
= {
1277 .s_ctrl
= ov13858_set_ctrl
,
1280 static int ov13858_enum_mbus_code(struct v4l2_subdev
*sd
,
1281 struct v4l2_subdev_pad_config
*cfg
,
1282 struct v4l2_subdev_mbus_code_enum
*code
)
1284 /* Only one bayer order(GRBG) is supported */
1285 if (code
->index
> 0)
1288 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1293 static int ov13858_enum_frame_size(struct v4l2_subdev
*sd
,
1294 struct v4l2_subdev_pad_config
*cfg
,
1295 struct v4l2_subdev_frame_size_enum
*fse
)
1297 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1300 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1303 fse
->min_width
= supported_modes
[fse
->index
].width
;
1304 fse
->max_width
= fse
->min_width
;
1305 fse
->min_height
= supported_modes
[fse
->index
].height
;
1306 fse
->max_height
= fse
->min_height
;
1311 static void ov13858_update_pad_format(const struct ov13858_mode
*mode
,
1312 struct v4l2_subdev_format
*fmt
)
1314 fmt
->format
.width
= mode
->width
;
1315 fmt
->format
.height
= mode
->height
;
1316 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1317 fmt
->format
.field
= V4L2_FIELD_NONE
;
1320 static int ov13858_do_get_pad_format(struct ov13858
*ov13858
,
1321 struct v4l2_subdev_pad_config
*cfg
,
1322 struct v4l2_subdev_format
*fmt
)
1324 struct v4l2_mbus_framefmt
*framefmt
;
1325 struct v4l2_subdev
*sd
= &ov13858
->sd
;
1327 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1328 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1329 fmt
->format
= *framefmt
;
1331 ov13858_update_pad_format(ov13858
->cur_mode
, fmt
);
1337 static int ov13858_get_pad_format(struct v4l2_subdev
*sd
,
1338 struct v4l2_subdev_pad_config
*cfg
,
1339 struct v4l2_subdev_format
*fmt
)
1341 struct ov13858
*ov13858
= to_ov13858(sd
);
1344 mutex_lock(&ov13858
->mutex
);
1345 ret
= ov13858_do_get_pad_format(ov13858
, cfg
, fmt
);
1346 mutex_unlock(&ov13858
->mutex
);
1352 * Calculate resolution distance
1355 ov13858_get_resolution_dist(const struct ov13858_mode
*mode
,
1356 struct v4l2_mbus_framefmt
*framefmt
)
1358 return abs(mode
->width
- framefmt
->width
) +
1359 abs(mode
->height
- framefmt
->height
);
1363 * Find the closest supported resolution to the requested resolution
1365 static const struct ov13858_mode
*
1366 ov13858_find_best_fit(struct ov13858
*ov13858
,
1367 struct v4l2_subdev_format
*fmt
)
1369 int i
, dist
, cur_best_fit
= 0, cur_best_fit_dist
= -1;
1370 struct v4l2_mbus_framefmt
*framefmt
= &fmt
->format
;
1372 for (i
= 0; i
< ARRAY_SIZE(supported_modes
); i
++) {
1373 dist
= ov13858_get_resolution_dist(&supported_modes
[i
],
1375 if (cur_best_fit_dist
== -1 || dist
< cur_best_fit_dist
) {
1376 cur_best_fit_dist
= dist
;
1381 return &supported_modes
[cur_best_fit
];
1385 ov13858_set_pad_format(struct v4l2_subdev
*sd
,
1386 struct v4l2_subdev_pad_config
*cfg
,
1387 struct v4l2_subdev_format
*fmt
)
1389 struct ov13858
*ov13858
= to_ov13858(sd
);
1390 const struct ov13858_mode
*mode
;
1391 struct v4l2_mbus_framefmt
*framefmt
;
1398 mutex_lock(&ov13858
->mutex
);
1400 /* Only one raw bayer(GRBG) order is supported */
1401 if (fmt
->format
.code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1402 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1404 mode
= ov13858_find_best_fit(ov13858
, fmt
);
1405 ov13858_update_pad_format(mode
, fmt
);
1406 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1407 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1408 *framefmt
= fmt
->format
;
1410 ov13858
->cur_mode
= mode
;
1411 __v4l2_ctrl_s_ctrl(ov13858
->link_freq
, mode
->link_freq_index
);
1412 link_freq
= link_freq_menu_items
[mode
->link_freq_index
];
1413 pixel_rate
= link_freq_to_pixel_rate(link_freq
);
1414 __v4l2_ctrl_s_ctrl_int64(ov13858
->pixel_rate
, pixel_rate
);
1416 /* Update limits and set FPS to default */
1417 vblank_def
= ov13858
->cur_mode
->vts_def
-
1418 ov13858
->cur_mode
->height
;
1419 vblank_min
= ov13858
->cur_mode
->vts_min
-
1420 ov13858
->cur_mode
->height
;
1421 __v4l2_ctrl_modify_range(
1422 ov13858
->vblank
, vblank_min
,
1423 OV13858_VTS_MAX
- ov13858
->cur_mode
->height
, 1,
1425 __v4l2_ctrl_s_ctrl(ov13858
->vblank
, vblank_def
);
1427 link_freq_configs
[mode
->link_freq_index
].pixels_per_line
1428 - ov13858
->cur_mode
->width
;
1429 __v4l2_ctrl_modify_range(ov13858
->hblank
, h_blank
,
1430 h_blank
, 1, h_blank
);
1433 mutex_unlock(&ov13858
->mutex
);
1438 static int ov13858_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1440 *frames
= OV13858_NUM_OF_SKIP_FRAMES
;
1445 /* Start streaming */
1446 static int ov13858_start_streaming(struct ov13858
*ov13858
)
1448 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1449 const struct ov13858_reg_list
*reg_list
;
1450 int ret
, link_freq_index
;
1452 /* Get out of from software reset */
1453 ret
= ov13858_write_reg(ov13858
, OV13858_REG_SOFTWARE_RST
,
1454 OV13858_REG_VALUE_08BIT
, OV13858_SOFTWARE_RST
);
1456 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
1462 link_freq_index
= ov13858
->cur_mode
->link_freq_index
;
1463 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
1464 ret
= ov13858_write_reg_list(ov13858
, reg_list
);
1466 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
1470 /* Apply default values of current mode */
1471 reg_list
= &ov13858
->cur_mode
->reg_list
;
1472 ret
= ov13858_write_reg_list(ov13858
, reg_list
);
1474 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
1478 /* Apply customized values from user */
1479 ret
= __v4l2_ctrl_handler_setup(ov13858
->sd
.ctrl_handler
);
1483 return ov13858_write_reg(ov13858
, OV13858_REG_MODE_SELECT
,
1484 OV13858_REG_VALUE_08BIT
,
1485 OV13858_MODE_STREAMING
);
1488 /* Stop streaming */
1489 static int ov13858_stop_streaming(struct ov13858
*ov13858
)
1491 return ov13858_write_reg(ov13858
, OV13858_REG_MODE_SELECT
,
1492 OV13858_REG_VALUE_08BIT
, OV13858_MODE_STANDBY
);
1495 static int ov13858_set_stream(struct v4l2_subdev
*sd
, int enable
)
1497 struct ov13858
*ov13858
= to_ov13858(sd
);
1498 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1501 mutex_lock(&ov13858
->mutex
);
1502 if (ov13858
->streaming
== enable
) {
1503 mutex_unlock(&ov13858
->mutex
);
1508 ret
= pm_runtime_get_sync(&client
->dev
);
1510 pm_runtime_put_noidle(&client
->dev
);
1515 * Apply default & customized values
1516 * and then start streaming.
1518 ret
= ov13858_start_streaming(ov13858
);
1522 ov13858_stop_streaming(ov13858
);
1523 pm_runtime_put(&client
->dev
);
1526 ov13858
->streaming
= enable
;
1527 mutex_unlock(&ov13858
->mutex
);
1532 pm_runtime_put(&client
->dev
);
1534 mutex_unlock(&ov13858
->mutex
);
1539 static int __maybe_unused
ov13858_suspend(struct device
*dev
)
1541 struct i2c_client
*client
= to_i2c_client(dev
);
1542 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1543 struct ov13858
*ov13858
= to_ov13858(sd
);
1545 if (ov13858
->streaming
)
1546 ov13858_stop_streaming(ov13858
);
1551 static int __maybe_unused
ov13858_resume(struct device
*dev
)
1553 struct i2c_client
*client
= to_i2c_client(dev
);
1554 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1555 struct ov13858
*ov13858
= to_ov13858(sd
);
1558 if (ov13858
->streaming
) {
1559 ret
= ov13858_start_streaming(ov13858
);
1567 ov13858_stop_streaming(ov13858
);
1568 ov13858
->streaming
= 0;
1572 /* Verify chip ID */
1573 static int ov13858_identify_module(struct ov13858
*ov13858
)
1575 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1579 ret
= ov13858_read_reg(ov13858
, OV13858_REG_CHIP_ID
,
1580 OV13858_REG_VALUE_24BIT
, &val
);
1584 if (val
!= OV13858_CHIP_ID
) {
1585 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
1586 OV13858_CHIP_ID
, val
);
1593 static const struct v4l2_subdev_video_ops ov13858_video_ops
= {
1594 .s_stream
= ov13858_set_stream
,
1597 static const struct v4l2_subdev_pad_ops ov13858_pad_ops
= {
1598 .enum_mbus_code
= ov13858_enum_mbus_code
,
1599 .get_fmt
= ov13858_get_pad_format
,
1600 .set_fmt
= ov13858_set_pad_format
,
1601 .enum_frame_size
= ov13858_enum_frame_size
,
1604 static const struct v4l2_subdev_sensor_ops ov13858_sensor_ops
= {
1605 .g_skip_frames
= ov13858_get_skip_frames
,
1608 static const struct v4l2_subdev_ops ov13858_subdev_ops
= {
1609 .video
= &ov13858_video_ops
,
1610 .pad
= &ov13858_pad_ops
,
1611 .sensor
= &ov13858_sensor_ops
,
1614 static const struct media_entity_operations ov13858_subdev_entity_ops
= {
1615 .link_validate
= v4l2_subdev_link_validate
,
1618 static const struct v4l2_subdev_internal_ops ov13858_internal_ops
= {
1619 .open
= ov13858_open
,
1622 /* Initialize control handlers */
1623 static int ov13858_init_controls(struct ov13858
*ov13858
)
1625 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1626 struct v4l2_ctrl_handler
*ctrl_hdlr
;
1633 const struct ov13858_mode
*mode
;
1636 ctrl_hdlr
= &ov13858
->ctrl_handler
;
1637 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
1641 mutex_init(&ov13858
->mutex
);
1642 ctrl_hdlr
->lock
= &ov13858
->mutex
;
1643 ov13858
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
1646 OV13858_NUM_OF_LINK_FREQS
- 1,
1648 link_freq_menu_items
);
1649 ov13858
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1651 pixel_rate_max
= link_freq_to_pixel_rate(link_freq_menu_items
[0]);
1652 pixel_rate_min
= link_freq_to_pixel_rate(link_freq_menu_items
[1]);
1653 /* By default, PIXEL_RATE is read only */
1654 ov13858
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
,
1655 V4L2_CID_PIXEL_RATE
,
1656 pixel_rate_min
, pixel_rate_max
,
1659 mode
= ov13858
->cur_mode
;
1660 vblank_def
= mode
->vts_def
- mode
->height
;
1661 vblank_min
= mode
->vts_min
- mode
->height
;
1662 ov13858
->vblank
= v4l2_ctrl_new_std(
1663 ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_VBLANK
,
1664 vblank_min
, OV13858_VTS_MAX
- mode
->height
, 1,
1667 hblank
= link_freq_configs
[mode
->link_freq_index
].pixels_per_line
-
1669 ov13858
->hblank
= v4l2_ctrl_new_std(
1670 ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_HBLANK
,
1671 hblank
, hblank
, 1, hblank
);
1672 ov13858
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1674 exposure_max
= mode
->vts_def
- 8;
1675 ov13858
->exposure
= v4l2_ctrl_new_std(
1676 ctrl_hdlr
, &ov13858_ctrl_ops
,
1677 V4L2_CID_EXPOSURE
, OV13858_EXPOSURE_MIN
,
1678 exposure_max
, OV13858_EXPOSURE_STEP
,
1679 OV13858_EXPOSURE_DEFAULT
);
1681 v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
1682 OV13858_ANA_GAIN_MIN
, OV13858_ANA_GAIN_MAX
,
1683 OV13858_ANA_GAIN_STEP
, OV13858_ANA_GAIN_DEFAULT
);
1686 v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
1687 OV13858_DGTL_GAIN_MIN
, OV13858_DGTL_GAIN_MAX
,
1688 OV13858_DGTL_GAIN_STEP
, OV13858_DGTL_GAIN_DEFAULT
);
1690 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov13858_ctrl_ops
,
1691 V4L2_CID_TEST_PATTERN
,
1692 ARRAY_SIZE(ov13858_test_pattern_menu
) - 1,
1693 0, 0, ov13858_test_pattern_menu
);
1694 if (ctrl_hdlr
->error
) {
1695 ret
= ctrl_hdlr
->error
;
1696 dev_err(&client
->dev
, "%s control init failed (%d)\n",
1701 ov13858
->sd
.ctrl_handler
= ctrl_hdlr
;
1706 v4l2_ctrl_handler_free(ctrl_hdlr
);
1707 mutex_destroy(&ov13858
->mutex
);
1712 static void ov13858_free_controls(struct ov13858
*ov13858
)
1714 v4l2_ctrl_handler_free(ov13858
->sd
.ctrl_handler
);
1715 mutex_destroy(&ov13858
->mutex
);
1718 static int ov13858_probe(struct i2c_client
*client
,
1719 const struct i2c_device_id
*devid
)
1721 struct ov13858
*ov13858
;
1725 device_property_read_u32(&client
->dev
, "clock-frequency", &val
);
1726 if (val
!= 19200000)
1729 ov13858
= devm_kzalloc(&client
->dev
, sizeof(*ov13858
), GFP_KERNEL
);
1733 /* Initialize subdev */
1734 v4l2_i2c_subdev_init(&ov13858
->sd
, client
, &ov13858_subdev_ops
);
1736 /* Check module identity */
1737 ret
= ov13858_identify_module(ov13858
);
1739 dev_err(&client
->dev
, "failed to find sensor: %d\n", ret
);
1743 /* Set default mode to max resolution */
1744 ov13858
->cur_mode
= &supported_modes
[0];
1746 ret
= ov13858_init_controls(ov13858
);
1750 /* Initialize subdev */
1751 ov13858
->sd
.internal_ops
= &ov13858_internal_ops
;
1752 ov13858
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1753 ov13858
->sd
.entity
.ops
= &ov13858_subdev_entity_ops
;
1754 ov13858
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1756 /* Initialize source pad */
1757 ov13858
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1758 ret
= media_entity_pads_init(&ov13858
->sd
.entity
, 1, &ov13858
->pad
);
1760 dev_err(&client
->dev
, "%s failed:%d\n", __func__
, ret
);
1761 goto error_handler_free
;
1764 ret
= v4l2_async_register_subdev_sensor_common(&ov13858
->sd
);
1766 goto error_media_entity
;
1769 * Device is already turned on by i2c-core with ACPI domain PM.
1770 * Enable runtime PM and turn off the device.
1772 pm_runtime_get_noresume(&client
->dev
);
1773 pm_runtime_set_active(&client
->dev
);
1774 pm_runtime_enable(&client
->dev
);
1775 pm_runtime_put(&client
->dev
);
1780 media_entity_cleanup(&ov13858
->sd
.entity
);
1783 ov13858_free_controls(ov13858
);
1784 dev_err(&client
->dev
, "%s failed:%d\n", __func__
, ret
);
1789 static int ov13858_remove(struct i2c_client
*client
)
1791 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1792 struct ov13858
*ov13858
= to_ov13858(sd
);
1794 v4l2_async_unregister_subdev(sd
);
1795 media_entity_cleanup(&sd
->entity
);
1796 ov13858_free_controls(ov13858
);
1799 * Disable runtime PM but keep the device turned on.
1800 * i2c-core with ACPI domain PM will turn off the device.
1802 pm_runtime_get_sync(&client
->dev
);
1803 pm_runtime_disable(&client
->dev
);
1804 pm_runtime_set_suspended(&client
->dev
);
1805 pm_runtime_put_noidle(&client
->dev
);
1810 static const struct i2c_device_id ov13858_id_table
[] = {
1815 MODULE_DEVICE_TABLE(i2c
, ov13858_id_table
);
1817 static const struct dev_pm_ops ov13858_pm_ops
= {
1818 SET_SYSTEM_SLEEP_PM_OPS(ov13858_suspend
, ov13858_resume
)
1822 static const struct acpi_device_id ov13858_acpi_ids
[] = {
1827 MODULE_DEVICE_TABLE(acpi
, ov13858_acpi_ids
);
1830 static struct i2c_driver ov13858_i2c_driver
= {
1833 .owner
= THIS_MODULE
,
1834 .pm
= &ov13858_pm_ops
,
1835 .acpi_match_table
= ACPI_PTR(ov13858_acpi_ids
),
1837 .probe
= ov13858_probe
,
1838 .remove
= ov13858_remove
,
1839 .id_table
= ov13858_id_table
,
1842 module_i2c_driver(ov13858_i2c_driver
);
1844 MODULE_AUTHOR("Kan, Chris <chris.kan@intel.com>");
1845 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
1846 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
1847 MODULE_DESCRIPTION("Omnivision ov13858 sensor driver");
1848 MODULE_LICENSE("GPL v2");