1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
5 * Adapted from the atomisp-ov5693 driver, with contributions from:
15 #include <asm/unaligned.h>
16 #include <linux/acpi.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-fwnode.h>
30 #define OV5693_REG_8BIT(n) ((1 << 16) | (n))
31 #define OV5693_REG_16BIT(n) ((2 << 16) | (n))
32 #define OV5693_REG_24BIT(n) ((3 << 16) | (n))
33 #define OV5693_REG_SIZE_SHIFT 16
34 #define OV5693_REG_ADDR_MASK 0xffff
37 #define OV5693_SW_RESET_REG OV5693_REG_8BIT(0x0103)
38 #define OV5693_SW_STREAM_REG OV5693_REG_8BIT(0x0100)
39 #define OV5693_START_STREAMING 0x01
40 #define OV5693_STOP_STREAMING 0x00
41 #define OV5693_SW_RESET 0x01
43 #define OV5693_REG_CHIP_ID OV5693_REG_16BIT(0x300a)
44 /* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
45 #define OV5693_CHIP_ID 0x5690
48 #define OV5693_EXPOSURE_CTRL_REG OV5693_REG_24BIT(0x3500)
49 #define OV5693_EXPOSURE_CTRL_MASK GENMASK(19, 4)
50 #define OV5693_INTEGRATION_TIME_MARGIN 8
51 #define OV5693_EXPOSURE_MIN 1
52 #define OV5693_EXPOSURE_STEP 1
55 #define OV5693_GAIN_CTRL_REG OV5693_REG_16BIT(0x350a)
56 #define OV5693_GAIN_CTRL_MASK GENMASK(10, 4)
57 #define OV5693_GAIN_MIN 1
58 #define OV5693_GAIN_MAX 127
59 #define OV5693_GAIN_DEF 8
60 #define OV5693_GAIN_STEP 1
63 #define OV5693_MWB_RED_GAIN_REG OV5693_REG_16BIT(0x3400)
64 #define OV5693_MWB_GREEN_GAIN_REG OV5693_REG_16BIT(0x3402)
65 #define OV5693_MWB_BLUE_GAIN_REG OV5693_REG_16BIT(0x3404)
66 #define OV5693_MWB_GAIN_MASK GENMASK(11, 0)
67 #define OV5693_MWB_GAIN_MAX 0x0fff
68 #define OV5693_DIGITAL_GAIN_MIN 1
69 #define OV5693_DIGITAL_GAIN_MAX 4095
70 #define OV5693_DIGITAL_GAIN_DEF 1024
71 #define OV5693_DIGITAL_GAIN_STEP 1
73 /* Timing and Format */
74 #define OV5693_CROP_START_X_REG OV5693_REG_16BIT(0x3800)
75 #define OV5693_CROP_START_Y_REG OV5693_REG_16BIT(0x3802)
76 #define OV5693_CROP_END_X_REG OV5693_REG_16BIT(0x3804)
77 #define OV5693_CROP_END_Y_REG OV5693_REG_16BIT(0x3806)
78 #define OV5693_OUTPUT_SIZE_X_REG OV5693_REG_16BIT(0x3808)
79 #define OV5693_OUTPUT_SIZE_Y_REG OV5693_REG_16BIT(0x380a)
81 #define OV5693_TIMING_HTS_REG OV5693_REG_16BIT(0x380c)
82 #define OV5693_FIXED_PPL 2688U
83 #define OV5693_TIMING_VTS_REG OV5693_REG_16BIT(0x380e)
84 #define OV5693_TIMING_MAX_VTS 0xffff
85 #define OV5693_TIMING_MIN_VTS 0x04
87 #define OV5693_OFFSET_START_X_REG OV5693_REG_16BIT(0x3810)
88 #define OV5693_OFFSET_START_Y_REG OV5693_REG_16BIT(0x3812)
90 #define OV5693_SUB_INC_X_REG OV5693_REG_8BIT(0x3814)
91 #define OV5693_SUB_INC_Y_REG OV5693_REG_8BIT(0x3815)
93 #define OV5693_FORMAT1_REG OV5693_REG_8BIT(0x3820)
94 #define OV5693_FORMAT1_FLIP_VERT_ISP_EN BIT(6)
95 #define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1)
96 #define OV5693_FORMAT1_VBIN_EN BIT(0)
97 #define OV5693_FORMAT2_REG OV5693_REG_8BIT(0x3821)
98 #define OV5693_FORMAT2_HDR_EN BIT(7)
99 #define OV5693_FORMAT2_FLIP_HORZ_ISP_EN BIT(2)
100 #define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1)
101 #define OV5693_FORMAT2_HBIN_EN BIT(0)
103 #define OV5693_ISP_CTRL2_REG OV5693_REG_8BIT(0x5002)
104 #define OV5693_ISP_SCALE_ENABLE BIT(7)
107 #define OV5693_NATIVE_WIDTH 2624
108 #define OV5693_NATIVE_HEIGHT 1956
109 #define OV5693_NATIVE_START_LEFT 0
110 #define OV5693_NATIVE_START_TOP 0
111 #define OV5693_ACTIVE_WIDTH 2592
112 #define OV5693_ACTIVE_HEIGHT 1944
113 #define OV5693_ACTIVE_START_LEFT 16
114 #define OV5693_ACTIVE_START_TOP 6
115 #define OV5693_MIN_CROP_WIDTH 2
116 #define OV5693_MIN_CROP_HEIGHT 2
119 #define OV5693_TEST_PATTERN_REG OV5693_REG_8BIT(0x5e00)
120 #define OV5693_TEST_PATTERN_ENABLE BIT(7)
121 #define OV5693_TEST_PATTERN_ROLLING BIT(6)
122 #define OV5693_TEST_PATTERN_RANDOM 0x01
123 #define OV5693_TEST_PATTERN_BARS 0x00
125 /* System Frequencies */
126 #define OV5693_XVCLK_FREQ 19200000
127 #define OV5693_LINK_FREQ_419_2MHZ 419200000
128 #define OV5693_PIXEL_RATE 167680000
130 #define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)
132 static const char * const ov5693_supply_names[] = {
133 "avdd", /* Analog power */
134 "dovdd", /* Digital I/O power */
135 "dvdd", /* Digital circuit power */
138 #define OV5693_NUM_SUPPLIES ARRAY_SIZE(ov5693_supply_names)
145 struct ov5693_reg_list {
147 const struct ov5693_reg *regs;
150 struct ov5693_device {
151 struct i2c_client *client;
154 /* Protect against concurrent changes to controls */
157 struct gpio_desc *reset;
158 struct gpio_desc *powerdown;
159 struct gpio_desc *privacy_led;
160 struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
164 struct v4l2_rect crop;
165 struct v4l2_mbus_framefmt format;
168 unsigned int inc_x_odd;
169 unsigned int inc_y_odd;
174 struct v4l2_subdev sd;
175 struct media_pad pad;
177 struct ov5693_v4l2_ctrls {
178 struct v4l2_ctrl_handler handler;
179 struct v4l2_ctrl *link_freq;
180 struct v4l2_ctrl *pixel_rate;
181 struct v4l2_ctrl *exposure;
182 struct v4l2_ctrl *analogue_gain;
183 struct v4l2_ctrl *digital_gain;
184 struct v4l2_ctrl *hflip;
185 struct v4l2_ctrl *vflip;
186 struct v4l2_ctrl *hblank;
187 struct v4l2_ctrl *vblank;
188 struct v4l2_ctrl *test_pattern;
192 static const struct ov5693_reg ov5693_global_regs[] = {
193 {OV5693_REG_8BIT(0x3016), 0xf0},
194 {OV5693_REG_8BIT(0x3017), 0xf0},
195 {OV5693_REG_8BIT(0x3018), 0xf0},
196 {OV5693_REG_8BIT(0x3022), 0x01},
197 {OV5693_REG_8BIT(0x3028), 0x44},
198 {OV5693_REG_8BIT(0x3098), 0x02},
199 {OV5693_REG_8BIT(0x3099), 0x19},
200 {OV5693_REG_8BIT(0x309a), 0x02},
201 {OV5693_REG_8BIT(0x309b), 0x01},
202 {OV5693_REG_8BIT(0x309c), 0x00},
203 {OV5693_REG_8BIT(0x30a0), 0xd2},
204 {OV5693_REG_8BIT(0x30a2), 0x01},
205 {OV5693_REG_8BIT(0x30b2), 0x00},
206 {OV5693_REG_8BIT(0x30b3), 0x83},
207 {OV5693_REG_8BIT(0x30b4), 0x03},
208 {OV5693_REG_8BIT(0x30b5), 0x04},
209 {OV5693_REG_8BIT(0x30b6), 0x01},
210 {OV5693_REG_8BIT(0x3080), 0x01},
211 {OV5693_REG_8BIT(0x3104), 0x21},
212 {OV5693_REG_8BIT(0x3106), 0x00},
213 {OV5693_REG_8BIT(0x3406), 0x01},
214 {OV5693_REG_8BIT(0x3503), 0x07},
215 {OV5693_REG_8BIT(0x350b), 0x40},
216 {OV5693_REG_8BIT(0x3601), 0x0a},
217 {OV5693_REG_8BIT(0x3602), 0x38},
218 {OV5693_REG_8BIT(0x3612), 0x80},
219 {OV5693_REG_8BIT(0x3620), 0x54},
220 {OV5693_REG_8BIT(0x3621), 0xc7},
221 {OV5693_REG_8BIT(0x3622), 0x0f},
222 {OV5693_REG_8BIT(0x3625), 0x10},
223 {OV5693_REG_8BIT(0x3630), 0x55},
224 {OV5693_REG_8BIT(0x3631), 0xf4},
225 {OV5693_REG_8BIT(0x3632), 0x00},
226 {OV5693_REG_8BIT(0x3633), 0x34},
227 {OV5693_REG_8BIT(0x3634), 0x02},
228 {OV5693_REG_8BIT(0x364d), 0x0d},
229 {OV5693_REG_8BIT(0x364f), 0xdd},
230 {OV5693_REG_8BIT(0x3660), 0x04},
231 {OV5693_REG_8BIT(0x3662), 0x10},
232 {OV5693_REG_8BIT(0x3663), 0xf1},
233 {OV5693_REG_8BIT(0x3665), 0x00},
234 {OV5693_REG_8BIT(0x3666), 0x20},
235 {OV5693_REG_8BIT(0x3667), 0x00},
236 {OV5693_REG_8BIT(0x366a), 0x80},
237 {OV5693_REG_8BIT(0x3680), 0xe0},
238 {OV5693_REG_8BIT(0x3681), 0x00},
239 {OV5693_REG_8BIT(0x3700), 0x42},
240 {OV5693_REG_8BIT(0x3701), 0x14},
241 {OV5693_REG_8BIT(0x3702), 0xa0},
242 {OV5693_REG_8BIT(0x3703), 0xd8},
243 {OV5693_REG_8BIT(0x3704), 0x78},
244 {OV5693_REG_8BIT(0x3705), 0x02},
245 {OV5693_REG_8BIT(0x370a), 0x00},
246 {OV5693_REG_8BIT(0x370b), 0x20},
247 {OV5693_REG_8BIT(0x370c), 0x0c},
248 {OV5693_REG_8BIT(0x370d), 0x11},
249 {OV5693_REG_8BIT(0x370e), 0x00},
250 {OV5693_REG_8BIT(0x370f), 0x40},
251 {OV5693_REG_8BIT(0x3710), 0x00},
252 {OV5693_REG_8BIT(0x371a), 0x1c},
253 {OV5693_REG_8BIT(0x371b), 0x05},
254 {OV5693_REG_8BIT(0x371c), 0x01},
255 {OV5693_REG_8BIT(0x371e), 0xa1},
256 {OV5693_REG_8BIT(0x371f), 0x0c},
257 {OV5693_REG_8BIT(0x3721), 0x00},
258 {OV5693_REG_8BIT(0x3724), 0x10},
259 {OV5693_REG_8BIT(0x3726), 0x00},
260 {OV5693_REG_8BIT(0x372a), 0x01},
261 {OV5693_REG_8BIT(0x3730), 0x10},
262 {OV5693_REG_8BIT(0x3738), 0x22},
263 {OV5693_REG_8BIT(0x3739), 0xe5},
264 {OV5693_REG_8BIT(0x373a), 0x50},
265 {OV5693_REG_8BIT(0x373b), 0x02},
266 {OV5693_REG_8BIT(0x373c), 0x41},
267 {OV5693_REG_8BIT(0x373f), 0x02},
268 {OV5693_REG_8BIT(0x3740), 0x42},
269 {OV5693_REG_8BIT(0x3741), 0x02},
270 {OV5693_REG_8BIT(0x3742), 0x18},
271 {OV5693_REG_8BIT(0x3743), 0x01},
272 {OV5693_REG_8BIT(0x3744), 0x02},
273 {OV5693_REG_8BIT(0x3747), 0x10},
274 {OV5693_REG_8BIT(0x374c), 0x04},
275 {OV5693_REG_8BIT(0x3751), 0xf0},
276 {OV5693_REG_8BIT(0x3752), 0x00},
277 {OV5693_REG_8BIT(0x3753), 0x00},
278 {OV5693_REG_8BIT(0x3754), 0xc0},
279 {OV5693_REG_8BIT(0x3755), 0x00},
280 {OV5693_REG_8BIT(0x3756), 0x1a},
281 {OV5693_REG_8BIT(0x3758), 0x00},
282 {OV5693_REG_8BIT(0x3759), 0x0f},
283 {OV5693_REG_8BIT(0x376b), 0x44},
284 {OV5693_REG_8BIT(0x375c), 0x04},
285 {OV5693_REG_8BIT(0x3774), 0x10},
286 {OV5693_REG_8BIT(0x3776), 0x00},
287 {OV5693_REG_8BIT(0x377f), 0x08},
288 {OV5693_REG_8BIT(0x3780), 0x22},
289 {OV5693_REG_8BIT(0x3781), 0x0c},
290 {OV5693_REG_8BIT(0x3784), 0x2c},
291 {OV5693_REG_8BIT(0x3785), 0x1e},
292 {OV5693_REG_8BIT(0x378f), 0xf5},
293 {OV5693_REG_8BIT(0x3791), 0xb0},
294 {OV5693_REG_8BIT(0x3795), 0x00},
295 {OV5693_REG_8BIT(0x3796), 0x64},
296 {OV5693_REG_8BIT(0x3797), 0x11},
297 {OV5693_REG_8BIT(0x3798), 0x30},
298 {OV5693_REG_8BIT(0x3799), 0x41},
299 {OV5693_REG_8BIT(0x379a), 0x07},
300 {OV5693_REG_8BIT(0x379b), 0xb0},
301 {OV5693_REG_8BIT(0x379c), 0x0c},
302 {OV5693_REG_8BIT(0x3a04), 0x06},
303 {OV5693_REG_8BIT(0x3a05), 0x14},
304 {OV5693_REG_8BIT(0x3e07), 0x20},
305 {OV5693_REG_8BIT(0x4000), 0x08},
306 {OV5693_REG_8BIT(0x4001), 0x04},
307 {OV5693_REG_8BIT(0x4004), 0x08},
308 {OV5693_REG_8BIT(0x4006), 0x20},
309 {OV5693_REG_8BIT(0x4008), 0x24},
310 {OV5693_REG_8BIT(0x4009), 0x10},
311 {OV5693_REG_8BIT(0x4058), 0x00},
312 {OV5693_REG_8BIT(0x4101), 0xb2},
313 {OV5693_REG_8BIT(0x4307), 0x31},
314 {OV5693_REG_8BIT(0x4511), 0x05},
315 {OV5693_REG_8BIT(0x4512), 0x01},
316 {OV5693_REG_8BIT(0x481f), 0x30},
317 {OV5693_REG_8BIT(0x4826), 0x2c},
318 {OV5693_REG_8BIT(0x4d02), 0xfd},
319 {OV5693_REG_8BIT(0x4d03), 0xf5},
320 {OV5693_REG_8BIT(0x4d04), 0x0c},
321 {OV5693_REG_8BIT(0x4d05), 0xcc},
322 {OV5693_REG_8BIT(0x4837), 0x0a},
323 {OV5693_REG_8BIT(0x5003), 0x20},
324 {OV5693_REG_8BIT(0x5013), 0x00},
325 {OV5693_REG_8BIT(0x5842), 0x01},
326 {OV5693_REG_8BIT(0x5843), 0x2b},
327 {OV5693_REG_8BIT(0x5844), 0x01},
328 {OV5693_REG_8BIT(0x5845), 0x92},
329 {OV5693_REG_8BIT(0x5846), 0x01},
330 {OV5693_REG_8BIT(0x5847), 0x8f},
331 {OV5693_REG_8BIT(0x5848), 0x01},
332 {OV5693_REG_8BIT(0x5849), 0x0c},
333 {OV5693_REG_8BIT(0x5e10), 0x0c},
334 {OV5693_REG_8BIT(0x3820), 0x00},
335 {OV5693_REG_8BIT(0x3821), 0x1e},
336 {OV5693_REG_8BIT(0x5041), 0x14}
339 static const struct ov5693_reg_list ov5693_global_setting = {
340 .num_regs = ARRAY_SIZE(ov5693_global_regs),
341 .regs = ov5693_global_regs,
344 static const struct v4l2_rect ov5693_default_crop = {
345 .left = OV5693_ACTIVE_START_LEFT,
346 .top = OV5693_ACTIVE_START_TOP,
347 .width = OV5693_ACTIVE_WIDTH,
348 .height = OV5693_ACTIVE_HEIGHT,
351 static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
352 .width = OV5693_ACTIVE_WIDTH,
353 .height = OV5693_ACTIVE_HEIGHT,
354 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
357 static const s64 link_freq_menu_items[] = {
358 OV5693_LINK_FREQ_419_2MHZ
361 static const char * const ov5693_test_pattern_menu[] = {
365 "Colour Bars with Rolling Bar"
368 static const u8 ov5693_test_pattern_bits[] = {
370 OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
371 OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
372 OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
373 OV5693_TEST_PATTERN_ROLLING,
376 /* I2C I/O Operations */
378 static int ov5693_read_reg(struct ov5693_device *ov5693, u32 addr, u32 *value)
380 struct i2c_client *client = ov5693->client;
383 struct i2c_msg msg[] = {
385 .addr = client->addr,
391 .addr = client->addr,
396 unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
400 reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);
404 ret = i2c_transfer(client->adapter, msg, 2);
406 return dev_err_probe(&client->dev, ret,
407 "Failed to read register 0x%04x\n",
408 addr & OV5693_REG_ADDR_MASK);
411 for (i = 0; i < len; ++i) {
419 static void ov5693_write_reg(struct ov5693_device *ov5693, u32 addr, u32 value,
422 struct i2c_client *client = ov5693->client;
427 struct i2c_msg msg = {
428 .addr = client->addr,
431 unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
438 buf.reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);
439 for (i = 0; i < len; ++i) {
440 buf.val[len - i - 1] = value & 0xff;
446 ret = i2c_transfer(client->adapter, &msg, 1);
448 dev_err(&client->dev, "Failed to write register 0x%04x: %d\n",
449 addr & OV5693_REG_ADDR_MASK, ret);
454 static int ov5693_write_reg_array(struct ov5693_device *ov5693,
455 const struct ov5693_reg_list *reglist)
460 for (i = 0; i < reglist->num_regs; i++)
461 ov5693_write_reg(ov5693, reglist->regs[i].reg,
462 reglist->regs[i].val, &ret);
467 static int ov5693_update_bits(struct ov5693_device *ov5693, u32 address,
473 ret = ov5693_read_reg(ov5693, address, &value);
480 ov5693_write_reg(ov5693, address, value, &ret);
485 /* V4L2 Controls Functions */
487 static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
490 u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
491 OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
494 ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG, bits,
502 static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
505 u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
506 OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
509 ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG, bits,
517 static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
522 ret = ov5693_read_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, &exposure);
526 /* The lowest 4 bits are unsupported fractional bits */
527 *value = exposure >> 4;
532 static int ov5693_exposure_configure(struct ov5693_device *ov5693,
537 exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;
539 ov5693_write_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);
544 static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
549 ret = ov5693_read_reg(ov5693, OV5693_GAIN_CTRL_REG, &value);
553 /* As with exposure, the lowest 4 bits are fractional bits. */
559 static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
564 gain &= OV5693_MWB_GAIN_MASK;
566 ov5693_write_reg(ov5693, OV5693_MWB_RED_GAIN_REG, gain, &ret);
567 ov5693_write_reg(ov5693, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
568 ov5693_write_reg(ov5693, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);
573 static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
577 gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;
579 ov5693_write_reg(ov5693, OV5693_GAIN_CTRL_REG, gain, &ret);
584 static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
586 u16 vts = ov5693->mode.format.height + vblank;
589 ov5693_write_reg(ov5693, OV5693_TIMING_VTS_REG, vts, &ret);
594 static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
598 ov5693_write_reg(ov5693, OV5693_TEST_PATTERN_REG,
599 ov5693_test_pattern_bits[idx], &ret);
604 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
606 struct ov5693_device *ov5693 =
607 container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
610 /* If VBLANK is altered we need to update exposure to compensate */
611 if (ctrl->id == V4L2_CID_VBLANK) {
614 exposure_max = ov5693->mode.format.height + ctrl->val -
615 OV5693_INTEGRATION_TIME_MARGIN;
616 __v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
617 ov5693->ctrls.exposure->minimum,
619 ov5693->ctrls.exposure->step,
620 min(ov5693->ctrls.exposure->val,
624 /* Only apply changes to the controls if the device is powered up */
625 if (!pm_runtime_get_if_in_use(ov5693->dev))
629 case V4L2_CID_EXPOSURE:
630 ret = ov5693_exposure_configure(ov5693, ctrl->val);
632 case V4L2_CID_ANALOGUE_GAIN:
633 ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
635 case V4L2_CID_DIGITAL_GAIN:
636 ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
639 ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
642 ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
644 case V4L2_CID_VBLANK:
645 ret = ov5693_vts_configure(ov5693, ctrl->val);
647 case V4L2_CID_TEST_PATTERN:
648 ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
654 pm_runtime_put(ov5693->dev);
659 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
661 struct ov5693_device *ov5693 = container_of(ctrl->handler,
662 struct ov5693_device,
666 case V4L2_CID_EXPOSURE_ABSOLUTE:
667 return ov5693_get_exposure(ov5693, &ctrl->val);
668 case V4L2_CID_AUTOGAIN:
669 return ov5693_get_gain(ov5693, &ctrl->val);
675 static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
676 .s_ctrl = ov5693_s_ctrl,
677 .g_volatile_ctrl = ov5693_g_volatile_ctrl
680 /* System Control Functions */
682 static int ov5693_mode_configure(struct ov5693_device *ov5693)
684 const struct ov5693_mode *mode = &ov5693->mode;
688 ov5693_write_reg(ov5693, OV5693_CROP_START_X_REG, mode->crop.left,
692 ov5693_write_reg(ov5693, OV5693_OFFSET_START_X_REG, 0, &ret);
695 ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
699 ov5693_write_reg(ov5693, OV5693_CROP_END_X_REG,
700 mode->crop.left + mode->crop.width, &ret);
702 /* Horizontal Total Size */
703 ov5693_write_reg(ov5693, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
707 ov5693_write_reg(ov5693, OV5693_CROP_START_Y_REG, mode->crop.top,
711 ov5693_write_reg(ov5693, OV5693_OFFSET_START_Y_REG, 0, &ret);
714 ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
718 ov5693_write_reg(ov5693, OV5693_CROP_END_Y_REG,
719 mode->crop.top + mode->crop.height, &ret);
721 /* Subsample X increase */
722 ov5693_write_reg(ov5693, OV5693_SUB_INC_X_REG,
723 ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
724 /* Subsample Y increase */
725 ov5693_write_reg(ov5693, OV5693_SUB_INC_Y_REG,
726 ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
732 ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG,
733 OV5693_FORMAT1_VBIN_EN,
734 mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0);
738 ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG,
739 OV5693_FORMAT2_HBIN_EN,
740 mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0);
745 static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
749 ov5693_write_reg(ov5693, OV5693_SW_STREAM_REG,
750 enable ? OV5693_START_STREAMING :
751 OV5693_STOP_STREAMING, &ret);
756 static int ov5693_sw_reset(struct ov5693_device *ov5693)
760 ov5693_write_reg(ov5693, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);
765 static int ov5693_sensor_init(struct ov5693_device *ov5693)
769 ret = ov5693_sw_reset(ov5693);
771 return dev_err_probe(ov5693->dev, ret,
772 "software reset error\n");
774 ret = ov5693_write_reg_array(ov5693, &ov5693_global_setting);
776 return dev_err_probe(ov5693->dev, ret,
777 "global settings error\n");
779 ret = ov5693_mode_configure(ov5693);
781 return dev_err_probe(ov5693->dev, ret,
782 "mode configure error\n");
784 ret = ov5693_enable_streaming(ov5693, false);
786 dev_err(ov5693->dev, "stop streaming error\n");
791 static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
793 gpiod_set_value_cansleep(ov5693->privacy_led, 0);
794 gpiod_set_value_cansleep(ov5693->reset, 1);
795 gpiod_set_value_cansleep(ov5693->powerdown, 1);
797 regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);
799 clk_disable_unprepare(ov5693->xvclk);
802 static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
806 gpiod_set_value_cansleep(ov5693->reset, 1);
807 gpiod_set_value_cansleep(ov5693->powerdown, 1);
809 ret = clk_prepare_enable(ov5693->xvclk);
811 dev_err(ov5693->dev, "Failed to enable clk\n");
815 ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
817 dev_err(ov5693->dev, "Failed to enable regulators\n");
821 gpiod_set_value_cansleep(ov5693->powerdown, 0);
822 gpiod_set_value_cansleep(ov5693->reset, 0);
823 gpiod_set_value_cansleep(ov5693->privacy_led, 1);
825 usleep_range(5000, 7500);
830 ov5693_sensor_powerdown(ov5693);
834 static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
836 struct v4l2_subdev *sd = dev_get_drvdata(dev);
837 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
839 ov5693_sensor_powerdown(ov5693);
844 static int __maybe_unused ov5693_sensor_resume(struct device *dev)
846 struct v4l2_subdev *sd = dev_get_drvdata(dev);
847 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
850 mutex_lock(&ov5693->lock);
852 ret = ov5693_sensor_powerup(ov5693);
856 ret = ov5693_sensor_init(ov5693);
858 dev_err(dev, "ov5693 sensor init failure\n");
865 ov5693_sensor_powerdown(ov5693);
867 mutex_unlock(&ov5693->lock);
871 static int ov5693_detect(struct ov5693_device *ov5693)
876 ret = ov5693_read_reg(ov5693, OV5693_REG_CHIP_ID, &id);
880 if (id != OV5693_CHIP_ID)
881 return dev_err_probe(ov5693->dev, -ENODEV,
882 "sensor ID mismatch. Found 0x%04x\n", id);
887 /* V4L2 Framework callbacks */
889 static unsigned int __ov5693_calc_vts(u32 height)
892 * We need to set a sensible default VTS for whatever format height we
893 * happen to be given from set_fmt(). This function just targets
894 * an even multiple of 30fps.
897 unsigned int tgt_fps;
899 tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);
901 return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
904 static struct v4l2_mbus_framefmt *
905 __ov5693_get_pad_format(struct ov5693_device *ov5693,
906 struct v4l2_subdev_state *state,
907 unsigned int pad, enum v4l2_subdev_format_whence which)
910 case V4L2_SUBDEV_FORMAT_TRY:
911 return v4l2_subdev_get_try_format(&ov5693->sd, state, pad);
912 case V4L2_SUBDEV_FORMAT_ACTIVE:
913 return &ov5693->mode.format;
919 static struct v4l2_rect *
920 __ov5693_get_pad_crop(struct ov5693_device *ov5693,
921 struct v4l2_subdev_state *state,
922 unsigned int pad, enum v4l2_subdev_format_whence which)
925 case V4L2_SUBDEV_FORMAT_TRY:
926 return v4l2_subdev_get_try_crop(&ov5693->sd, state, pad);
927 case V4L2_SUBDEV_FORMAT_ACTIVE:
928 return &ov5693->mode.crop;
934 static int ov5693_get_fmt(struct v4l2_subdev *sd,
935 struct v4l2_subdev_state *state,
936 struct v4l2_subdev_format *format)
938 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
940 format->format = ov5693->mode.format;
945 static int ov5693_set_fmt(struct v4l2_subdev *sd,
946 struct v4l2_subdev_state *state,
947 struct v4l2_subdev_format *format)
949 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
950 const struct v4l2_rect *crop;
951 struct v4l2_mbus_framefmt *fmt;
952 unsigned int hratio, vratio;
953 unsigned int width, height;
957 crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
960 * Align to two to simplify the binning calculations below, and clamp
961 * the requested format at the crop rectangle
963 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
964 OV5693_MIN_CROP_WIDTH, crop->width);
965 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
966 OV5693_MIN_CROP_HEIGHT, crop->height);
969 * We can only support setting either the dimensions of the crop rect
970 * or those dimensions binned (separately) by a factor of two.
972 hratio = clamp_t(unsigned int,
973 DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
974 vratio = clamp_t(unsigned int,
975 DIV_ROUND_CLOSEST(crop->height, height), 1, 2);
977 fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
980 fmt->width = crop->width / hratio;
981 fmt->height = crop->height / vratio;
982 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
984 format->format = *fmt;
986 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
989 mutex_lock(&ov5693->lock);
991 ov5693->mode.binning_x = hratio > 1;
992 ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
993 ov5693->mode.binning_y = vratio > 1;
994 ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;
996 ov5693->mode.vts = __ov5693_calc_vts(fmt->height);
998 __v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
999 OV5693_TIMING_MIN_VTS,
1000 OV5693_TIMING_MAX_VTS - fmt->height,
1001 1, ov5693->mode.vts - fmt->height);
1002 __v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
1003 ov5693->mode.vts - fmt->height);
1005 hblank = OV5693_FIXED_PPL - fmt->width;
1006 __v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
1009 exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
1010 __v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
1011 ov5693->ctrls.exposure->minimum, exposure_max,
1012 ov5693->ctrls.exposure->step,
1013 min(ov5693->ctrls.exposure->val,
1016 mutex_unlock(&ov5693->lock);
1020 static int ov5693_get_selection(struct v4l2_subdev *sd,
1021 struct v4l2_subdev_state *state,
1022 struct v4l2_subdev_selection *sel)
1024 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1026 switch (sel->target) {
1027 case V4L2_SEL_TGT_CROP:
1028 mutex_lock(&ov5693->lock);
1029 sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
1031 mutex_unlock(&ov5693->lock);
1033 case V4L2_SEL_TGT_NATIVE_SIZE:
1036 sel->r.width = OV5693_NATIVE_WIDTH;
1037 sel->r.height = OV5693_NATIVE_HEIGHT;
1039 case V4L2_SEL_TGT_CROP_BOUNDS:
1040 case V4L2_SEL_TGT_CROP_DEFAULT:
1041 sel->r.top = OV5693_ACTIVE_START_TOP;
1042 sel->r.left = OV5693_ACTIVE_START_LEFT;
1043 sel->r.width = OV5693_ACTIVE_WIDTH;
1044 sel->r.height = OV5693_ACTIVE_HEIGHT;
1053 static int ov5693_set_selection(struct v4l2_subdev *sd,
1054 struct v4l2_subdev_state *state,
1055 struct v4l2_subdev_selection *sel)
1057 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1058 struct v4l2_mbus_framefmt *format;
1059 struct v4l2_rect *__crop;
1060 struct v4l2_rect rect;
1062 if (sel->target != V4L2_SEL_TGT_CROP)
1066 * Clamp the boundaries of the crop rectangle to the size of the sensor
1067 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
1070 rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
1071 OV5693_NATIVE_WIDTH);
1072 rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
1073 OV5693_NATIVE_HEIGHT);
1074 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
1075 OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
1076 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
1077 OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);
1079 /* Make sure the crop rectangle isn't outside the bounds of the array */
1080 rect.width = min_t(unsigned int, rect.width,
1081 OV5693_NATIVE_WIDTH - rect.left);
1082 rect.height = min_t(unsigned int, rect.height,
1083 OV5693_NATIVE_HEIGHT - rect.top);
1085 __crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);
1087 if (rect.width != __crop->width || rect.height != __crop->height) {
1089 * Reset the output image size if the crop rectangle size has
1092 format = __ov5693_get_pad_format(ov5693, state, sel->pad,
1094 format->width = rect.width;
1095 format->height = rect.height;
1104 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1106 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1110 ret = pm_runtime_get_sync(ov5693->dev);
1112 goto err_power_down;
1114 mutex_lock(&ov5693->lock);
1115 ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
1117 mutex_unlock(&ov5693->lock);
1118 goto err_power_down;
1121 ret = ov5693_enable_streaming(ov5693, true);
1122 mutex_unlock(&ov5693->lock);
1124 mutex_lock(&ov5693->lock);
1125 ret = ov5693_enable_streaming(ov5693, false);
1126 mutex_unlock(&ov5693->lock);
1129 goto err_power_down;
1131 ov5693->streaming = !!enable;
1134 pm_runtime_put(ov5693->dev);
1138 pm_runtime_put_noidle(ov5693->dev);
1142 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1143 struct v4l2_subdev_frame_interval *interval)
1145 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1146 unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
1147 ov5693->ctrls.vblank->val);
1148 unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);
1150 interval->interval.numerator = 1;
1151 interval->interval.denominator = fps;
1156 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1157 struct v4l2_subdev_state *state,
1158 struct v4l2_subdev_mbus_code_enum *code)
1160 /* Only a single mbus format is supported */
1161 if (code->index > 0)
1164 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1168 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1169 struct v4l2_subdev_state *state,
1170 struct v4l2_subdev_frame_size_enum *fse)
1172 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1173 struct v4l2_rect *__crop;
1175 if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
1178 __crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
1182 fse->min_width = __crop->width / (fse->index + 1);
1183 fse->min_height = __crop->height / (fse->index + 1);
1184 fse->max_width = fse->min_width;
1185 fse->max_height = fse->min_height;
1190 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1191 .s_stream = ov5693_s_stream,
1192 .g_frame_interval = ov5693_g_frame_interval,
1195 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1196 .enum_mbus_code = ov5693_enum_mbus_code,
1197 .enum_frame_size = ov5693_enum_frame_size,
1198 .get_fmt = ov5693_get_fmt,
1199 .set_fmt = ov5693_set_fmt,
1200 .get_selection = ov5693_get_selection,
1201 .set_selection = ov5693_set_selection,
1204 static const struct v4l2_subdev_ops ov5693_ops = {
1205 .video = &ov5693_video_ops,
1206 .pad = &ov5693_pad_ops,
1209 /* Sensor and Driver Configuration Functions */
1211 static int ov5693_init_controls(struct ov5693_device *ov5693)
1213 const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
1214 struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
1215 struct v4l2_fwnode_device_properties props;
1216 int vblank_max, vblank_def;
1221 ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
1226 ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
1227 NULL, V4L2_CID_LINK_FREQ,
1228 0, 0, link_freq_menu_items);
1229 if (ctrls->link_freq)
1230 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1233 ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
1234 V4L2_CID_PIXEL_RATE, 0,
1235 OV5693_PIXEL_RATE, 1,
1239 exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
1240 ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
1242 OV5693_EXPOSURE_MIN, exposure_max,
1243 OV5693_EXPOSURE_STEP, exposure_max);
1246 ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
1247 ops, V4L2_CID_ANALOGUE_GAIN,
1253 ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
1254 V4L2_CID_DIGITAL_GAIN,
1255 OV5693_DIGITAL_GAIN_MIN,
1256 OV5693_DIGITAL_GAIN_MAX,
1257 OV5693_DIGITAL_GAIN_STEP,
1258 OV5693_DIGITAL_GAIN_DEF);
1261 ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
1262 V4L2_CID_HFLIP, 0, 1, 1, 0);
1264 ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
1265 V4L2_CID_VFLIP, 0, 1, 1, 0);
1267 hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
1268 ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
1269 V4L2_CID_HBLANK, hblank,
1273 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1275 vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
1276 vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
1277 ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
1279 OV5693_TIMING_MIN_VTS,
1280 vblank_max, 1, vblank_def);
1282 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
1283 &ctrls->handler, ops,
1284 V4L2_CID_TEST_PATTERN,
1285 ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
1286 0, 0, ov5693_test_pattern_menu);
1288 if (ctrls->handler.error) {
1289 dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
1290 ret = ctrls->handler.error;
1291 goto err_free_handler;
1294 /* set properties from fwnode (e.g. rotation, orientation) */
1295 ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
1297 goto err_free_handler;
1299 ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
1302 goto err_free_handler;
1304 /* Use same lock for controls as for everything else. */
1305 ctrls->handler.lock = &ov5693->lock;
1306 ov5693->sd.ctrl_handler = &ctrls->handler;
1311 v4l2_ctrl_handler_free(&ctrls->handler);
1315 static int ov5693_configure_gpios(struct ov5693_device *ov5693)
1317 ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
1319 if (IS_ERR(ov5693->reset)) {
1320 dev_err(ov5693->dev, "Error fetching reset GPIO\n");
1321 return PTR_ERR(ov5693->reset);
1324 ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
1326 if (IS_ERR(ov5693->powerdown)) {
1327 dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
1328 return PTR_ERR(ov5693->powerdown);
1331 ov5693->privacy_led = devm_gpiod_get_optional(ov5693->dev, "privacy-led",
1333 if (IS_ERR(ov5693->privacy_led)) {
1334 dev_err(ov5693->dev, "Error fetching privacy-led GPIO\n");
1335 return PTR_ERR(ov5693->privacy_led);
1341 static int ov5693_get_regulators(struct ov5693_device *ov5693)
1345 for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
1346 ov5693->supplies[i].supply = ov5693_supply_names[i];
1348 return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
1352 static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
1354 struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
1355 struct v4l2_fwnode_endpoint bus_cfg = {
1356 .bus_type = V4L2_MBUS_CSI2_DPHY,
1358 struct fwnode_handle *endpoint;
1362 endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
1364 return -EPROBE_DEFER; /* Could be provided by cio2-bridge */
1366 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
1367 fwnode_handle_put(endpoint);
1371 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1372 dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
1374 goto out_free_bus_cfg;
1377 if (!bus_cfg.nr_of_link_frequencies) {
1378 dev_err(ov5693->dev, "no link frequencies defined\n");
1380 goto out_free_bus_cfg;
1383 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1384 if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
1387 if (i == bus_cfg.nr_of_link_frequencies) {
1388 dev_err(ov5693->dev, "supported link freq %ull not found\n",
1389 OV5693_LINK_FREQ_419_2MHZ);
1391 goto out_free_bus_cfg;
1395 v4l2_fwnode_endpoint_free(&bus_cfg);
1400 static int ov5693_probe(struct i2c_client *client)
1402 struct ov5693_device *ov5693;
1406 ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
1410 ov5693->client = client;
1411 ov5693->dev = &client->dev;
1413 ret = ov5693_check_hwcfg(ov5693);
1417 mutex_init(&ov5693->lock);
1419 v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);
1421 ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
1422 if (IS_ERR(ov5693->xvclk))
1423 return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
1424 "failed to get xvclk: %ld\n",
1425 PTR_ERR(ov5693->xvclk));
1427 if (ov5693->xvclk) {
1428 xvclk_rate = clk_get_rate(ov5693->xvclk);
1430 ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
1435 dev_err(&client->dev, "can't get clock frequency");
1440 if (xvclk_rate != OV5693_XVCLK_FREQ)
1441 dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
1442 xvclk_rate, OV5693_XVCLK_FREQ);
1444 ret = ov5693_configure_gpios(ov5693);
1448 ret = ov5693_get_regulators(ov5693);
1450 return dev_err_probe(&client->dev, ret,
1451 "Error fetching regulators\n");
1453 ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1454 ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
1455 ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1457 ov5693->mode.crop = ov5693_default_crop;
1458 ov5693->mode.format = ov5693_default_fmt;
1459 ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);
1461 ret = ov5693_init_controls(ov5693);
1465 ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
1467 goto err_ctrl_handler_free;
1470 * We need the driver to work in the event that pm runtime is disable in
1471 * the kernel, so power up and verify the chip now. In the event that
1472 * runtime pm is disabled this will leave the chip on, so that streaming
1476 ret = ov5693_sensor_powerup(ov5693);
1478 goto err_media_entity_cleanup;
1480 ret = ov5693_detect(ov5693);
1484 pm_runtime_set_active(&client->dev);
1485 pm_runtime_get_noresume(&client->dev);
1486 pm_runtime_enable(&client->dev);
1488 ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
1490 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1492 goto err_pm_runtime;
1495 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1496 pm_runtime_use_autosuspend(&client->dev);
1497 pm_runtime_put_autosuspend(&client->dev);
1502 pm_runtime_disable(&client->dev);
1503 pm_runtime_put_noidle(&client->dev);
1505 ov5693_sensor_powerdown(ov5693);
1506 err_media_entity_cleanup:
1507 media_entity_cleanup(&ov5693->sd.entity);
1508 err_ctrl_handler_free:
1509 v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
1514 static void ov5693_remove(struct i2c_client *client)
1516 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1517 struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1519 v4l2_async_unregister_subdev(sd);
1520 media_entity_cleanup(&ov5693->sd.entity);
1521 v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
1522 mutex_destroy(&ov5693->lock);
1525 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1526 * make sure to turn power off manually.
1528 pm_runtime_disable(&client->dev);
1529 if (!pm_runtime_status_suspended(&client->dev))
1530 ov5693_sensor_powerdown(ov5693);
1531 pm_runtime_set_suspended(&client->dev);
1534 static const struct dev_pm_ops ov5693_pm_ops = {
1535 SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
1538 static const struct acpi_device_id ov5693_acpi_match[] = {
1542 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1544 static const struct of_device_id ov5693_of_match[] = {
1545 { .compatible = "ovti,ov5693", },
1548 MODULE_DEVICE_TABLE(of, ov5693_of_match);
1550 static struct i2c_driver ov5693_driver = {
1553 .acpi_match_table = ov5693_acpi_match,
1554 .of_match_table = ov5693_of_match,
1555 .pm = &ov5693_pm_ops,
1557 .probe = ov5693_probe,
1558 .remove = ov5693_remove,
1560 module_i2c_driver(ov5693_driver);
1562 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1563 MODULE_LICENSE("GPL");