1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-fwnode.h>
12 #define OV13858_REG_VALUE_08BIT 1
13 #define OV13858_REG_VALUE_16BIT 2
14 #define OV13858_REG_VALUE_24BIT 3
16 #define OV13858_REG_MODE_SELECT 0x0100
17 #define OV13858_MODE_STANDBY 0x00
18 #define OV13858_MODE_STREAMING 0x01
20 #define OV13858_REG_SOFTWARE_RST 0x0103
21 #define OV13858_SOFTWARE_RST 0x01
23 /* PLL1 generates PCLK and MIPI_PHY_CLK */
24 #define OV13858_REG_PLL1_CTRL_0 0x0300
25 #define OV13858_REG_PLL1_CTRL_1 0x0301
26 #define OV13858_REG_PLL1_CTRL_2 0x0302
27 #define OV13858_REG_PLL1_CTRL_3 0x0303
28 #define OV13858_REG_PLL1_CTRL_4 0x0304
29 #define OV13858_REG_PLL1_CTRL_5 0x0305
31 /* PLL2 generates DAC_CLK, SCLK and SRAM_CLK */
32 #define OV13858_REG_PLL2_CTRL_B 0x030b
33 #define OV13858_REG_PLL2_CTRL_C 0x030c
34 #define OV13858_REG_PLL2_CTRL_D 0x030d
35 #define OV13858_REG_PLL2_CTRL_E 0x030e
36 #define OV13858_REG_PLL2_CTRL_F 0x030f
37 #define OV13858_REG_PLL2_CTRL_12 0x0312
38 #define OV13858_REG_MIPI_SC_CTRL0 0x3016
39 #define OV13858_REG_MIPI_SC_CTRL1 0x3022
42 #define OV13858_REG_CHIP_ID 0x300a
43 #define OV13858_CHIP_ID 0x00d855
45 /* V_TIMING internal */
46 #define OV13858_REG_VTS 0x380e
47 #define OV13858_VTS_30FPS 0x0c8e /* 30 fps */
48 #define OV13858_VTS_60FPS 0x0648 /* 60 fps */
49 #define OV13858_VTS_MAX 0x7fff
51 /* HBLANK control - read only */
52 #define OV13858_PPL_270MHZ 2244
53 #define OV13858_PPL_540MHZ 4488
55 /* Exposure control */
56 #define OV13858_REG_EXPOSURE 0x3500
57 #define OV13858_EXPOSURE_MIN 4
58 #define OV13858_EXPOSURE_STEP 1
59 #define OV13858_EXPOSURE_DEFAULT 0x640
61 /* Analog gain control */
62 #define OV13858_REG_ANALOG_GAIN 0x3508
63 #define OV13858_ANA_GAIN_MIN 0
64 #define OV13858_ANA_GAIN_MAX 0x1fff
65 #define OV13858_ANA_GAIN_STEP 1
66 #define OV13858_ANA_GAIN_DEFAULT 0x80
68 /* Digital gain control */
69 #define OV13858_REG_B_MWB_GAIN 0x5100
70 #define OV13858_REG_G_MWB_GAIN 0x5102
71 #define OV13858_REG_R_MWB_GAIN 0x5104
72 #define OV13858_DGTL_GAIN_MIN 0
73 #define OV13858_DGTL_GAIN_MAX 16384 /* Max = 16 X */
74 #define OV13858_DGTL_GAIN_DEFAULT 1024 /* Default gain = 1 X */
75 #define OV13858_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
77 /* Test Pattern Control */
78 #define OV13858_REG_TEST_PATTERN 0x4503
79 #define OV13858_TEST_PATTERN_ENABLE BIT(7)
80 #define OV13858_TEST_PATTERN_MASK 0xfc
82 /* Number of frames to skip */
83 #define OV13858_NUM_OF_SKIP_FRAMES 2
90 struct ov13858_reg_list
{
92 const struct ov13858_reg
*regs
;
95 /* Link frequency config */
96 struct ov13858_link_freq_config
{
99 /* PLL registers for this link frequency */
100 struct ov13858_reg_list reg_list
;
103 /* Mode : resolution and related config&values */
104 struct ov13858_mode
{
114 /* Index of Link frequency config to be used */
116 /* Default register values */
117 struct ov13858_reg_list reg_list
;
120 /* 4224x3136 needs 1080Mbps/lane, 4 lanes */
121 static const struct ov13858_reg mipi_data_rate_1080mbps
[] = {
123 {OV13858_REG_PLL1_CTRL_0
, 0x07},
124 {OV13858_REG_PLL1_CTRL_1
, 0x01},
125 {OV13858_REG_PLL1_CTRL_2
, 0xc2},
126 {OV13858_REG_PLL1_CTRL_3
, 0x00},
127 {OV13858_REG_PLL1_CTRL_4
, 0x00},
128 {OV13858_REG_PLL1_CTRL_5
, 0x01},
131 {OV13858_REG_PLL2_CTRL_B
, 0x05},
132 {OV13858_REG_PLL2_CTRL_C
, 0x01},
133 {OV13858_REG_PLL2_CTRL_D
, 0x0e},
134 {OV13858_REG_PLL2_CTRL_E
, 0x05},
135 {OV13858_REG_PLL2_CTRL_F
, 0x01},
136 {OV13858_REG_PLL2_CTRL_12
, 0x01},
137 {OV13858_REG_MIPI_SC_CTRL0
, 0x72},
138 {OV13858_REG_MIPI_SC_CTRL1
, 0x01},
142 * 2112x1568, 2112x1188, 1056x784 need 540Mbps/lane,
145 static const struct ov13858_reg mipi_data_rate_540mbps
[] = {
147 {OV13858_REG_PLL1_CTRL_0
, 0x07},
148 {OV13858_REG_PLL1_CTRL_1
, 0x01},
149 {OV13858_REG_PLL1_CTRL_2
, 0xc2},
150 {OV13858_REG_PLL1_CTRL_3
, 0x01},
151 {OV13858_REG_PLL1_CTRL_4
, 0x00},
152 {OV13858_REG_PLL1_CTRL_5
, 0x01},
155 {OV13858_REG_PLL2_CTRL_B
, 0x05},
156 {OV13858_REG_PLL2_CTRL_C
, 0x01},
157 {OV13858_REG_PLL2_CTRL_D
, 0x0e},
158 {OV13858_REG_PLL2_CTRL_E
, 0x05},
159 {OV13858_REG_PLL2_CTRL_F
, 0x01},
160 {OV13858_REG_PLL2_CTRL_12
, 0x01},
161 {OV13858_REG_MIPI_SC_CTRL0
, 0x72},
162 {OV13858_REG_MIPI_SC_CTRL1
, 0x01},
165 static const struct ov13858_reg mode_4224x3136_regs
[] = {
356 static const struct ov13858_reg mode_2112x1568_regs
[] = {
547 static const struct ov13858_reg mode_2112x1188_regs
[] = {
738 static const struct ov13858_reg mode_1056x784_regs
[] = {
929 static const char * const ov13858_test_pattern_menu
[] = {
931 "Vertical Color Bar Type 1",
932 "Vertical Color Bar Type 2",
933 "Vertical Color Bar Type 3",
934 "Vertical Color Bar Type 4"
937 /* Configurations for supported link frequencies */
938 #define OV13858_NUM_OF_LINK_FREQS 2
939 #define OV13858_LINK_FREQ_540MHZ 540000000ULL
940 #define OV13858_LINK_FREQ_270MHZ 270000000ULL
941 #define OV13858_LINK_FREQ_INDEX_0 0
942 #define OV13858_LINK_FREQ_INDEX_1 1
945 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
946 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
948 static u64
link_freq_to_pixel_rate(u64 f
)
956 /* Menu items for LINK_FREQ V4L2 control */
957 static const s64 link_freq_menu_items
[OV13858_NUM_OF_LINK_FREQS
] = {
958 OV13858_LINK_FREQ_540MHZ
,
959 OV13858_LINK_FREQ_270MHZ
962 /* Link frequency configs */
963 static const struct ov13858_link_freq_config
964 link_freq_configs
[OV13858_NUM_OF_LINK_FREQS
] = {
966 .pixels_per_line
= OV13858_PPL_540MHZ
,
968 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_1080mbps
),
969 .regs
= mipi_data_rate_1080mbps
,
973 .pixels_per_line
= OV13858_PPL_270MHZ
,
975 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_540mbps
),
976 .regs
= mipi_data_rate_540mbps
,
982 static const struct ov13858_mode supported_modes
[] = {
986 .vts_def
= OV13858_VTS_30FPS
,
987 .vts_min
= OV13858_VTS_30FPS
,
989 .num_of_regs
= ARRAY_SIZE(mode_4224x3136_regs
),
990 .regs
= mode_4224x3136_regs
,
992 .link_freq_index
= OV13858_LINK_FREQ_INDEX_0
,
997 .vts_def
= OV13858_VTS_30FPS
,
1000 .num_of_regs
= ARRAY_SIZE(mode_2112x1568_regs
),
1001 .regs
= mode_2112x1568_regs
,
1003 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1008 .vts_def
= OV13858_VTS_30FPS
,
1011 .num_of_regs
= ARRAY_SIZE(mode_2112x1188_regs
),
1012 .regs
= mode_2112x1188_regs
,
1014 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1019 .vts_def
= OV13858_VTS_30FPS
,
1022 .num_of_regs
= ARRAY_SIZE(mode_1056x784_regs
),
1023 .regs
= mode_1056x784_regs
,
1025 .link_freq_index
= OV13858_LINK_FREQ_INDEX_1
,
1030 struct v4l2_subdev sd
;
1031 struct media_pad pad
;
1033 struct v4l2_ctrl_handler ctrl_handler
;
1035 struct v4l2_ctrl
*link_freq
;
1036 struct v4l2_ctrl
*pixel_rate
;
1037 struct v4l2_ctrl
*vblank
;
1038 struct v4l2_ctrl
*hblank
;
1039 struct v4l2_ctrl
*exposure
;
1042 const struct ov13858_mode
*cur_mode
;
1044 /* Mutex for serialized access */
1047 /* Streaming on/off */
1051 #define to_ov13858(_sd) container_of(_sd, struct ov13858, sd)
1053 /* Read registers up to 4 at a time */
1054 static int ov13858_read_reg(struct ov13858
*ov13858
, u16 reg
, u32 len
,
1057 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1058 struct i2c_msg msgs
[2];
1062 __be16 reg_addr_be
= cpu_to_be16(reg
);
1067 data_be_p
= (u8
*)&data_be
;
1068 /* Write register address */
1069 msgs
[0].addr
= client
->addr
;
1072 msgs
[0].buf
= (u8
*)®_addr_be
;
1074 /* Read data from register */
1075 msgs
[1].addr
= client
->addr
;
1076 msgs
[1].flags
= I2C_M_RD
;
1078 msgs
[1].buf
= &data_be_p
[4 - len
];
1080 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1081 if (ret
!= ARRAY_SIZE(msgs
))
1084 *val
= be32_to_cpu(data_be
);
1089 /* Write registers up to 4 at a time */
1090 static int ov13858_write_reg(struct ov13858
*ov13858
, u16 reg
, u32 len
,
1093 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1102 buf
[1] = reg
& 0xff;
1104 val
= cpu_to_be32(__val
);
1110 buf
[buf_i
++] = val_p
[val_i
++];
1112 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1118 /* Write a list of registers */
1119 static int ov13858_write_regs(struct ov13858
*ov13858
,
1120 const struct ov13858_reg
*regs
, u32 len
)
1122 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1126 for (i
= 0; i
< len
; i
++) {
1127 ret
= ov13858_write_reg(ov13858
, regs
[i
].address
, 1,
1130 dev_err_ratelimited(
1132 "Failed to write reg 0x%4.4x. error = %d\n",
1133 regs
[i
].address
, ret
);
1142 static int ov13858_write_reg_list(struct ov13858
*ov13858
,
1143 const struct ov13858_reg_list
*r_list
)
1145 return ov13858_write_regs(ov13858
, r_list
->regs
, r_list
->num_of_regs
);
1148 /* Open sub-device */
1149 static int ov13858_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1151 struct ov13858
*ov13858
= to_ov13858(sd
);
1152 struct v4l2_mbus_framefmt
*try_fmt
= v4l2_subdev_get_try_format(sd
,
1156 mutex_lock(&ov13858
->mutex
);
1158 /* Initialize try_fmt */
1159 try_fmt
->width
= ov13858
->cur_mode
->width
;
1160 try_fmt
->height
= ov13858
->cur_mode
->height
;
1161 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1162 try_fmt
->field
= V4L2_FIELD_NONE
;
1164 /* No crop or compose */
1165 mutex_unlock(&ov13858
->mutex
);
1170 static int ov13858_update_digital_gain(struct ov13858
*ov13858
, u32 d_gain
)
1174 ret
= ov13858_write_reg(ov13858
, OV13858_REG_B_MWB_GAIN
,
1175 OV13858_REG_VALUE_16BIT
, d_gain
);
1179 ret
= ov13858_write_reg(ov13858
, OV13858_REG_G_MWB_GAIN
,
1180 OV13858_REG_VALUE_16BIT
, d_gain
);
1184 ret
= ov13858_write_reg(ov13858
, OV13858_REG_R_MWB_GAIN
,
1185 OV13858_REG_VALUE_16BIT
, d_gain
);
1190 static int ov13858_enable_test_pattern(struct ov13858
*ov13858
, u32 pattern
)
1195 ret
= ov13858_read_reg(ov13858
, OV13858_REG_TEST_PATTERN
,
1196 OV13858_REG_VALUE_08BIT
, &val
);
1201 val
&= OV13858_TEST_PATTERN_MASK
;
1202 val
|= (pattern
- 1) | OV13858_TEST_PATTERN_ENABLE
;
1204 val
&= ~OV13858_TEST_PATTERN_ENABLE
;
1207 return ov13858_write_reg(ov13858
, OV13858_REG_TEST_PATTERN
,
1208 OV13858_REG_VALUE_08BIT
, val
);
1211 static int ov13858_set_ctrl(struct v4l2_ctrl
*ctrl
)
1213 struct ov13858
*ov13858
= container_of(ctrl
->handler
,
1214 struct ov13858
, ctrl_handler
);
1215 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1219 /* Propagate change of current control to all related controls */
1221 case V4L2_CID_VBLANK
:
1222 /* Update max exposure while meeting expected vblanking */
1223 max
= ov13858
->cur_mode
->height
+ ctrl
->val
- 8;
1224 __v4l2_ctrl_modify_range(ov13858
->exposure
,
1225 ov13858
->exposure
->minimum
,
1226 max
, ov13858
->exposure
->step
, max
);
1231 * Applying V4L2 control value only happens
1232 * when power is up for streaming
1234 if (!pm_runtime_get_if_in_use(&client
->dev
))
1239 case V4L2_CID_ANALOGUE_GAIN
:
1240 ret
= ov13858_write_reg(ov13858
, OV13858_REG_ANALOG_GAIN
,
1241 OV13858_REG_VALUE_16BIT
, ctrl
->val
);
1243 case V4L2_CID_DIGITAL_GAIN
:
1244 ret
= ov13858_update_digital_gain(ov13858
, ctrl
->val
);
1246 case V4L2_CID_EXPOSURE
:
1247 ret
= ov13858_write_reg(ov13858
, OV13858_REG_EXPOSURE
,
1248 OV13858_REG_VALUE_24BIT
,
1251 case V4L2_CID_VBLANK
:
1252 /* Update VTS that meets expected vertical blanking */
1253 ret
= ov13858_write_reg(ov13858
, OV13858_REG_VTS
,
1254 OV13858_REG_VALUE_16BIT
,
1255 ov13858
->cur_mode
->height
1258 case V4L2_CID_TEST_PATTERN
:
1259 ret
= ov13858_enable_test_pattern(ov13858
, ctrl
->val
);
1262 dev_info(&client
->dev
,
1263 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1264 ctrl
->id
, ctrl
->val
);
1268 pm_runtime_put(&client
->dev
);
1273 static const struct v4l2_ctrl_ops ov13858_ctrl_ops
= {
1274 .s_ctrl
= ov13858_set_ctrl
,
1277 static int ov13858_enum_mbus_code(struct v4l2_subdev
*sd
,
1278 struct v4l2_subdev_pad_config
*cfg
,
1279 struct v4l2_subdev_mbus_code_enum
*code
)
1281 /* Only one bayer order(GRBG) is supported */
1282 if (code
->index
> 0)
1285 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1290 static int ov13858_enum_frame_size(struct v4l2_subdev
*sd
,
1291 struct v4l2_subdev_pad_config
*cfg
,
1292 struct v4l2_subdev_frame_size_enum
*fse
)
1294 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1297 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1300 fse
->min_width
= supported_modes
[fse
->index
].width
;
1301 fse
->max_width
= fse
->min_width
;
1302 fse
->min_height
= supported_modes
[fse
->index
].height
;
1303 fse
->max_height
= fse
->min_height
;
1308 static void ov13858_update_pad_format(const struct ov13858_mode
*mode
,
1309 struct v4l2_subdev_format
*fmt
)
1311 fmt
->format
.width
= mode
->width
;
1312 fmt
->format
.height
= mode
->height
;
1313 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1314 fmt
->format
.field
= V4L2_FIELD_NONE
;
1317 static int ov13858_do_get_pad_format(struct ov13858
*ov13858
,
1318 struct v4l2_subdev_pad_config
*cfg
,
1319 struct v4l2_subdev_format
*fmt
)
1321 struct v4l2_mbus_framefmt
*framefmt
;
1322 struct v4l2_subdev
*sd
= &ov13858
->sd
;
1324 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1325 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1326 fmt
->format
= *framefmt
;
1328 ov13858_update_pad_format(ov13858
->cur_mode
, fmt
);
1334 static int ov13858_get_pad_format(struct v4l2_subdev
*sd
,
1335 struct v4l2_subdev_pad_config
*cfg
,
1336 struct v4l2_subdev_format
*fmt
)
1338 struct ov13858
*ov13858
= to_ov13858(sd
);
1341 mutex_lock(&ov13858
->mutex
);
1342 ret
= ov13858_do_get_pad_format(ov13858
, cfg
, fmt
);
1343 mutex_unlock(&ov13858
->mutex
);
1349 ov13858_set_pad_format(struct v4l2_subdev
*sd
,
1350 struct v4l2_subdev_pad_config
*cfg
,
1351 struct v4l2_subdev_format
*fmt
)
1353 struct ov13858
*ov13858
= to_ov13858(sd
);
1354 const struct ov13858_mode
*mode
;
1355 struct v4l2_mbus_framefmt
*framefmt
;
1362 mutex_lock(&ov13858
->mutex
);
1364 /* Only one raw bayer(GRBG) order is supported */
1365 if (fmt
->format
.code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1366 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1368 mode
= v4l2_find_nearest_size(supported_modes
,
1369 ARRAY_SIZE(supported_modes
),
1371 fmt
->format
.width
, fmt
->format
.height
);
1372 ov13858_update_pad_format(mode
, fmt
);
1373 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1374 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1375 *framefmt
= fmt
->format
;
1377 ov13858
->cur_mode
= mode
;
1378 __v4l2_ctrl_s_ctrl(ov13858
->link_freq
, mode
->link_freq_index
);
1379 link_freq
= link_freq_menu_items
[mode
->link_freq_index
];
1380 pixel_rate
= link_freq_to_pixel_rate(link_freq
);
1381 __v4l2_ctrl_s_ctrl_int64(ov13858
->pixel_rate
, pixel_rate
);
1383 /* Update limits and set FPS to default */
1384 vblank_def
= ov13858
->cur_mode
->vts_def
-
1385 ov13858
->cur_mode
->height
;
1386 vblank_min
= ov13858
->cur_mode
->vts_min
-
1387 ov13858
->cur_mode
->height
;
1388 __v4l2_ctrl_modify_range(
1389 ov13858
->vblank
, vblank_min
,
1390 OV13858_VTS_MAX
- ov13858
->cur_mode
->height
, 1,
1392 __v4l2_ctrl_s_ctrl(ov13858
->vblank
, vblank_def
);
1394 link_freq_configs
[mode
->link_freq_index
].pixels_per_line
1395 - ov13858
->cur_mode
->width
;
1396 __v4l2_ctrl_modify_range(ov13858
->hblank
, h_blank
,
1397 h_blank
, 1, h_blank
);
1400 mutex_unlock(&ov13858
->mutex
);
1405 static int ov13858_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1407 *frames
= OV13858_NUM_OF_SKIP_FRAMES
;
1412 /* Start streaming */
1413 static int ov13858_start_streaming(struct ov13858
*ov13858
)
1415 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1416 const struct ov13858_reg_list
*reg_list
;
1417 int ret
, link_freq_index
;
1419 /* Get out of from software reset */
1420 ret
= ov13858_write_reg(ov13858
, OV13858_REG_SOFTWARE_RST
,
1421 OV13858_REG_VALUE_08BIT
, OV13858_SOFTWARE_RST
);
1423 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
1429 link_freq_index
= ov13858
->cur_mode
->link_freq_index
;
1430 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
1431 ret
= ov13858_write_reg_list(ov13858
, reg_list
);
1433 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
1437 /* Apply default values of current mode */
1438 reg_list
= &ov13858
->cur_mode
->reg_list
;
1439 ret
= ov13858_write_reg_list(ov13858
, reg_list
);
1441 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
1445 /* Apply customized values from user */
1446 ret
= __v4l2_ctrl_handler_setup(ov13858
->sd
.ctrl_handler
);
1450 return ov13858_write_reg(ov13858
, OV13858_REG_MODE_SELECT
,
1451 OV13858_REG_VALUE_08BIT
,
1452 OV13858_MODE_STREAMING
);
1455 /* Stop streaming */
1456 static int ov13858_stop_streaming(struct ov13858
*ov13858
)
1458 return ov13858_write_reg(ov13858
, OV13858_REG_MODE_SELECT
,
1459 OV13858_REG_VALUE_08BIT
, OV13858_MODE_STANDBY
);
1462 static int ov13858_set_stream(struct v4l2_subdev
*sd
, int enable
)
1464 struct ov13858
*ov13858
= to_ov13858(sd
);
1465 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1468 mutex_lock(&ov13858
->mutex
);
1469 if (ov13858
->streaming
== enable
) {
1470 mutex_unlock(&ov13858
->mutex
);
1475 ret
= pm_runtime_get_sync(&client
->dev
);
1477 pm_runtime_put_noidle(&client
->dev
);
1482 * Apply default & customized values
1483 * and then start streaming.
1485 ret
= ov13858_start_streaming(ov13858
);
1489 ov13858_stop_streaming(ov13858
);
1490 pm_runtime_put(&client
->dev
);
1493 ov13858
->streaming
= enable
;
1494 mutex_unlock(&ov13858
->mutex
);
1499 pm_runtime_put(&client
->dev
);
1501 mutex_unlock(&ov13858
->mutex
);
1506 static int __maybe_unused
ov13858_suspend(struct device
*dev
)
1508 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1509 struct ov13858
*ov13858
= to_ov13858(sd
);
1511 if (ov13858
->streaming
)
1512 ov13858_stop_streaming(ov13858
);
1517 static int __maybe_unused
ov13858_resume(struct device
*dev
)
1519 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1520 struct ov13858
*ov13858
= to_ov13858(sd
);
1523 if (ov13858
->streaming
) {
1524 ret
= ov13858_start_streaming(ov13858
);
1532 ov13858_stop_streaming(ov13858
);
1533 ov13858
->streaming
= false;
1537 /* Verify chip ID */
1538 static int ov13858_identify_module(struct ov13858
*ov13858
)
1540 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1544 ret
= ov13858_read_reg(ov13858
, OV13858_REG_CHIP_ID
,
1545 OV13858_REG_VALUE_24BIT
, &val
);
1549 if (val
!= OV13858_CHIP_ID
) {
1550 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
1551 OV13858_CHIP_ID
, val
);
1558 static const struct v4l2_subdev_video_ops ov13858_video_ops
= {
1559 .s_stream
= ov13858_set_stream
,
1562 static const struct v4l2_subdev_pad_ops ov13858_pad_ops
= {
1563 .enum_mbus_code
= ov13858_enum_mbus_code
,
1564 .get_fmt
= ov13858_get_pad_format
,
1565 .set_fmt
= ov13858_set_pad_format
,
1566 .enum_frame_size
= ov13858_enum_frame_size
,
1569 static const struct v4l2_subdev_sensor_ops ov13858_sensor_ops
= {
1570 .g_skip_frames
= ov13858_get_skip_frames
,
1573 static const struct v4l2_subdev_ops ov13858_subdev_ops
= {
1574 .video
= &ov13858_video_ops
,
1575 .pad
= &ov13858_pad_ops
,
1576 .sensor
= &ov13858_sensor_ops
,
1579 static const struct media_entity_operations ov13858_subdev_entity_ops
= {
1580 .link_validate
= v4l2_subdev_link_validate
,
1583 static const struct v4l2_subdev_internal_ops ov13858_internal_ops
= {
1584 .open
= ov13858_open
,
1587 /* Initialize control handlers */
1588 static int ov13858_init_controls(struct ov13858
*ov13858
)
1590 struct i2c_client
*client
= v4l2_get_subdevdata(&ov13858
->sd
);
1591 struct v4l2_fwnode_device_properties props
;
1592 struct v4l2_ctrl_handler
*ctrl_hdlr
;
1599 const struct ov13858_mode
*mode
;
1602 ctrl_hdlr
= &ov13858
->ctrl_handler
;
1603 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 10);
1607 mutex_init(&ov13858
->mutex
);
1608 ctrl_hdlr
->lock
= &ov13858
->mutex
;
1609 ov13858
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
1612 OV13858_NUM_OF_LINK_FREQS
- 1,
1614 link_freq_menu_items
);
1615 if (ov13858
->link_freq
)
1616 ov13858
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1618 pixel_rate_max
= link_freq_to_pixel_rate(link_freq_menu_items
[0]);
1619 pixel_rate_min
= link_freq_to_pixel_rate(link_freq_menu_items
[1]);
1620 /* By default, PIXEL_RATE is read only */
1621 ov13858
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
,
1622 V4L2_CID_PIXEL_RATE
,
1623 pixel_rate_min
, pixel_rate_max
,
1626 mode
= ov13858
->cur_mode
;
1627 vblank_def
= mode
->vts_def
- mode
->height
;
1628 vblank_min
= mode
->vts_min
- mode
->height
;
1629 ov13858
->vblank
= v4l2_ctrl_new_std(
1630 ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_VBLANK
,
1631 vblank_min
, OV13858_VTS_MAX
- mode
->height
, 1,
1634 hblank
= link_freq_configs
[mode
->link_freq_index
].pixels_per_line
-
1636 ov13858
->hblank
= v4l2_ctrl_new_std(
1637 ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_HBLANK
,
1638 hblank
, hblank
, 1, hblank
);
1639 if (ov13858
->hblank
)
1640 ov13858
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1642 exposure_max
= mode
->vts_def
- 8;
1643 ov13858
->exposure
= v4l2_ctrl_new_std(
1644 ctrl_hdlr
, &ov13858_ctrl_ops
,
1645 V4L2_CID_EXPOSURE
, OV13858_EXPOSURE_MIN
,
1646 exposure_max
, OV13858_EXPOSURE_STEP
,
1647 OV13858_EXPOSURE_DEFAULT
);
1649 v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
1650 OV13858_ANA_GAIN_MIN
, OV13858_ANA_GAIN_MAX
,
1651 OV13858_ANA_GAIN_STEP
, OV13858_ANA_GAIN_DEFAULT
);
1654 v4l2_ctrl_new_std(ctrl_hdlr
, &ov13858_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
1655 OV13858_DGTL_GAIN_MIN
, OV13858_DGTL_GAIN_MAX
,
1656 OV13858_DGTL_GAIN_STEP
, OV13858_DGTL_GAIN_DEFAULT
);
1658 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov13858_ctrl_ops
,
1659 V4L2_CID_TEST_PATTERN
,
1660 ARRAY_SIZE(ov13858_test_pattern_menu
) - 1,
1661 0, 0, ov13858_test_pattern_menu
);
1662 if (ctrl_hdlr
->error
) {
1663 ret
= ctrl_hdlr
->error
;
1664 dev_err(&client
->dev
, "%s control init failed (%d)\n",
1669 ret
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
1673 ret
= v4l2_ctrl_new_fwnode_properties(ctrl_hdlr
, &ov13858_ctrl_ops
,
1678 ov13858
->sd
.ctrl_handler
= ctrl_hdlr
;
1683 v4l2_ctrl_handler_free(ctrl_hdlr
);
1684 mutex_destroy(&ov13858
->mutex
);
1689 static void ov13858_free_controls(struct ov13858
*ov13858
)
1691 v4l2_ctrl_handler_free(ov13858
->sd
.ctrl_handler
);
1692 mutex_destroy(&ov13858
->mutex
);
1695 static int ov13858_probe(struct i2c_client
*client
,
1696 const struct i2c_device_id
*devid
)
1698 struct ov13858
*ov13858
;
1702 device_property_read_u32(&client
->dev
, "clock-frequency", &val
);
1703 if (val
!= 19200000)
1706 ov13858
= devm_kzalloc(&client
->dev
, sizeof(*ov13858
), GFP_KERNEL
);
1710 /* Initialize subdev */
1711 v4l2_i2c_subdev_init(&ov13858
->sd
, client
, &ov13858_subdev_ops
);
1713 /* Check module identity */
1714 ret
= ov13858_identify_module(ov13858
);
1716 dev_err(&client
->dev
, "failed to find sensor: %d\n", ret
);
1720 /* Set default mode to max resolution */
1721 ov13858
->cur_mode
= &supported_modes
[0];
1723 ret
= ov13858_init_controls(ov13858
);
1727 /* Initialize subdev */
1728 ov13858
->sd
.internal_ops
= &ov13858_internal_ops
;
1729 ov13858
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1730 ov13858
->sd
.entity
.ops
= &ov13858_subdev_entity_ops
;
1731 ov13858
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1733 /* Initialize source pad */
1734 ov13858
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1735 ret
= media_entity_pads_init(&ov13858
->sd
.entity
, 1, &ov13858
->pad
);
1737 dev_err(&client
->dev
, "%s failed:%d\n", __func__
, ret
);
1738 goto error_handler_free
;
1741 ret
= v4l2_async_register_subdev_sensor_common(&ov13858
->sd
);
1743 goto error_media_entity
;
1746 * Device is already turned on by i2c-core with ACPI domain PM.
1747 * Enable runtime PM and turn off the device.
1749 pm_runtime_set_active(&client
->dev
);
1750 pm_runtime_enable(&client
->dev
);
1751 pm_runtime_idle(&client
->dev
);
1756 media_entity_cleanup(&ov13858
->sd
.entity
);
1759 ov13858_free_controls(ov13858
);
1760 dev_err(&client
->dev
, "%s failed:%d\n", __func__
, ret
);
1765 static int ov13858_remove(struct i2c_client
*client
)
1767 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1768 struct ov13858
*ov13858
= to_ov13858(sd
);
1770 v4l2_async_unregister_subdev(sd
);
1771 media_entity_cleanup(&sd
->entity
);
1772 ov13858_free_controls(ov13858
);
1774 pm_runtime_disable(&client
->dev
);
1779 static const struct i2c_device_id ov13858_id_table
[] = {
1784 MODULE_DEVICE_TABLE(i2c
, ov13858_id_table
);
1786 static const struct dev_pm_ops ov13858_pm_ops
= {
1787 SET_SYSTEM_SLEEP_PM_OPS(ov13858_suspend
, ov13858_resume
)
1791 static const struct acpi_device_id ov13858_acpi_ids
[] = {
1796 MODULE_DEVICE_TABLE(acpi
, ov13858_acpi_ids
);
1799 static struct i2c_driver ov13858_i2c_driver
= {
1802 .pm
= &ov13858_pm_ops
,
1803 .acpi_match_table
= ACPI_PTR(ov13858_acpi_ids
),
1805 .probe
= ov13858_probe
,
1806 .remove
= ov13858_remove
,
1807 .id_table
= ov13858_id_table
,
1810 module_i2c_driver(ov13858_i2c_driver
);
1812 MODULE_AUTHOR("Kan, Chris <chris.kan@intel.com>");
1813 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
1814 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
1815 MODULE_DESCRIPTION("Omnivision ov13858 sensor driver");
1816 MODULE_LICENSE("GPL v2");