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 <linux/acpi.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <media/v4l2-cci.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-fwnode.h>
32 #define OV5693_SW_RESET_REG CCI_REG8(0x0103)
33 #define OV5693_SW_STREAM_REG CCI_REG8(0x0100)
34 #define OV5693_START_STREAMING 0x01
35 #define OV5693_STOP_STREAMING 0x00
36 #define OV5693_SW_RESET 0x01
38 #define OV5693_REG_CHIP_ID CCI_REG16(0x300a)
39 /* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
40 #define OV5693_CHIP_ID 0x5690
43 #define OV5693_EXPOSURE_CTRL_REG CCI_REG24(0x3500)
44 #define OV5693_EXPOSURE_CTRL_MASK GENMASK(19, 4)
45 #define OV5693_INTEGRATION_TIME_MARGIN 8
46 #define OV5693_EXPOSURE_MIN 1
47 #define OV5693_EXPOSURE_STEP 1
50 #define OV5693_GAIN_CTRL_REG CCI_REG16(0x350a)
51 #define OV5693_GAIN_CTRL_MASK GENMASK(10, 4)
52 #define OV5693_GAIN_MIN 1
53 #define OV5693_GAIN_MAX 127
54 #define OV5693_GAIN_DEF 8
55 #define OV5693_GAIN_STEP 1
58 #define OV5693_MWB_RED_GAIN_REG CCI_REG16(0x3400)
59 #define OV5693_MWB_GREEN_GAIN_REG CCI_REG16(0x3402)
60 #define OV5693_MWB_BLUE_GAIN_REG CCI_REG16(0x3404)
61 #define OV5693_MWB_GAIN_MASK GENMASK(11, 0)
62 #define OV5693_MWB_GAIN_MAX 0x0fff
63 #define OV5693_DIGITAL_GAIN_MIN 1
64 #define OV5693_DIGITAL_GAIN_MAX 4095
65 #define OV5693_DIGITAL_GAIN_DEF 1024
66 #define OV5693_DIGITAL_GAIN_STEP 1
68 /* Timing and Format */
69 #define OV5693_CROP_START_X_REG CCI_REG16(0x3800)
70 #define OV5693_CROP_START_Y_REG CCI_REG16(0x3802)
71 #define OV5693_CROP_END_X_REG CCI_REG16(0x3804)
72 #define OV5693_CROP_END_Y_REG CCI_REG16(0x3806)
73 #define OV5693_OUTPUT_SIZE_X_REG CCI_REG16(0x3808)
74 #define OV5693_OUTPUT_SIZE_Y_REG CCI_REG16(0x380a)
76 #define OV5693_TIMING_HTS_REG CCI_REG16(0x380c)
77 #define OV5693_FIXED_PPL 2688U
78 #define OV5693_TIMING_VTS_REG CCI_REG16(0x380e)
79 #define OV5693_TIMING_MAX_VTS 0xffff
80 #define OV5693_TIMING_MIN_VTS 0x04
82 #define OV5693_OFFSET_START_X_REG CCI_REG16(0x3810)
83 #define OV5693_OFFSET_START_Y_REG CCI_REG16(0x3812)
85 #define OV5693_SUB_INC_X_REG CCI_REG8(0x3814)
86 #define OV5693_SUB_INC_Y_REG CCI_REG8(0x3815)
88 #define OV5693_FORMAT1_REG CCI_REG8(0x3820)
89 #define OV5693_FORMAT1_FLIP_VERT_ISP_EN BIT(6)
90 #define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1)
91 #define OV5693_FORMAT1_VBIN_EN BIT(0)
92 #define OV5693_FORMAT2_REG CCI_REG8(0x3821)
93 #define OV5693_FORMAT2_HDR_EN BIT(7)
94 #define OV5693_FORMAT2_FLIP_HORZ_ISP_EN BIT(2)
95 #define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1)
96 #define OV5693_FORMAT2_HBIN_EN BIT(0)
98 #define OV5693_ISP_CTRL2_REG CCI_REG8(0x5002)
99 #define OV5693_ISP_SCALE_ENABLE BIT(7)
102 #define OV5693_NATIVE_WIDTH 2624
103 #define OV5693_NATIVE_HEIGHT 1956
104 #define OV5693_NATIVE_START_LEFT 0
105 #define OV5693_NATIVE_START_TOP 0
106 #define OV5693_ACTIVE_WIDTH 2592
107 #define OV5693_ACTIVE_HEIGHT 1944
108 #define OV5693_ACTIVE_START_LEFT 16
109 #define OV5693_ACTIVE_START_TOP 6
110 #define OV5693_MIN_CROP_WIDTH 2
111 #define OV5693_MIN_CROP_HEIGHT 2
114 #define OV5693_TEST_PATTERN_REG CCI_REG8(0x5e00)
115 #define OV5693_TEST_PATTERN_ENABLE BIT(7)
116 #define OV5693_TEST_PATTERN_ROLLING BIT(6)
117 #define OV5693_TEST_PATTERN_RANDOM 0x01
118 #define OV5693_TEST_PATTERN_BARS 0x00
120 /* System Frequencies */
121 #define OV5693_XVCLK_FREQ 19200000
122 #define OV5693_LINK_FREQ_419_2MHZ 419200000
123 #define OV5693_PIXEL_RATE 167680000
125 #define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)
127 static const char * const ov5693_supply_names
[] = {
128 "avdd", /* Analog power */
129 "dovdd", /* Digital I/O power */
130 "dvdd", /* Digital circuit power */
133 #define OV5693_NUM_SUPPLIES ARRAY_SIZE(ov5693_supply_names)
135 struct ov5693_device
{
137 struct regmap
*regmap
;
139 /* Protect against concurrent changes to controls */
142 struct gpio_desc
*reset
;
143 struct gpio_desc
*powerdown
;
144 struct regulator_bulk_data supplies
[OV5693_NUM_SUPPLIES
];
148 struct v4l2_rect crop
;
149 struct v4l2_mbus_framefmt format
;
152 unsigned int inc_x_odd
;
153 unsigned int inc_y_odd
;
157 struct v4l2_subdev sd
;
158 struct media_pad pad
;
160 struct ov5693_v4l2_ctrls
{
161 struct v4l2_ctrl_handler handler
;
162 struct v4l2_ctrl
*link_freq
;
163 struct v4l2_ctrl
*pixel_rate
;
164 struct v4l2_ctrl
*exposure
;
165 struct v4l2_ctrl
*analogue_gain
;
166 struct v4l2_ctrl
*digital_gain
;
167 struct v4l2_ctrl
*hflip
;
168 struct v4l2_ctrl
*vflip
;
169 struct v4l2_ctrl
*hblank
;
170 struct v4l2_ctrl
*vblank
;
171 struct v4l2_ctrl
*test_pattern
;
175 static const struct cci_reg_sequence ov5693_global_regs
[] = {
176 {CCI_REG8(0x3016), 0xf0},
177 {CCI_REG8(0x3017), 0xf0},
178 {CCI_REG8(0x3018), 0xf0},
179 {CCI_REG8(0x3022), 0x01},
180 {CCI_REG8(0x3028), 0x44},
181 {CCI_REG8(0x3098), 0x02},
182 {CCI_REG8(0x3099), 0x19},
183 {CCI_REG8(0x309a), 0x02},
184 {CCI_REG8(0x309b), 0x01},
185 {CCI_REG8(0x309c), 0x00},
186 {CCI_REG8(0x30a0), 0xd2},
187 {CCI_REG8(0x30a2), 0x01},
188 {CCI_REG8(0x30b2), 0x00},
189 {CCI_REG8(0x30b3), 0x83},
190 {CCI_REG8(0x30b4), 0x03},
191 {CCI_REG8(0x30b5), 0x04},
192 {CCI_REG8(0x30b6), 0x01},
193 {CCI_REG8(0x3080), 0x01},
194 {CCI_REG8(0x3104), 0x21},
195 {CCI_REG8(0x3106), 0x00},
196 {CCI_REG8(0x3406), 0x01},
197 {CCI_REG8(0x3503), 0x07},
198 {CCI_REG8(0x350b), 0x40},
199 {CCI_REG8(0x3601), 0x0a},
200 {CCI_REG8(0x3602), 0x38},
201 {CCI_REG8(0x3612), 0x80},
202 {CCI_REG8(0x3620), 0x54},
203 {CCI_REG8(0x3621), 0xc7},
204 {CCI_REG8(0x3622), 0x0f},
205 {CCI_REG8(0x3625), 0x10},
206 {CCI_REG8(0x3630), 0x55},
207 {CCI_REG8(0x3631), 0xf4},
208 {CCI_REG8(0x3632), 0x00},
209 {CCI_REG8(0x3633), 0x34},
210 {CCI_REG8(0x3634), 0x02},
211 {CCI_REG8(0x364d), 0x0d},
212 {CCI_REG8(0x364f), 0xdd},
213 {CCI_REG8(0x3660), 0x04},
214 {CCI_REG8(0x3662), 0x10},
215 {CCI_REG8(0x3663), 0xf1},
216 {CCI_REG8(0x3665), 0x00},
217 {CCI_REG8(0x3666), 0x20},
218 {CCI_REG8(0x3667), 0x00},
219 {CCI_REG8(0x366a), 0x80},
220 {CCI_REG8(0x3680), 0xe0},
221 {CCI_REG8(0x3681), 0x00},
222 {CCI_REG8(0x3700), 0x42},
223 {CCI_REG8(0x3701), 0x14},
224 {CCI_REG8(0x3702), 0xa0},
225 {CCI_REG8(0x3703), 0xd8},
226 {CCI_REG8(0x3704), 0x78},
227 {CCI_REG8(0x3705), 0x02},
228 {CCI_REG8(0x370a), 0x00},
229 {CCI_REG8(0x370b), 0x20},
230 {CCI_REG8(0x370c), 0x0c},
231 {CCI_REG8(0x370d), 0x11},
232 {CCI_REG8(0x370e), 0x00},
233 {CCI_REG8(0x370f), 0x40},
234 {CCI_REG8(0x3710), 0x00},
235 {CCI_REG8(0x371a), 0x1c},
236 {CCI_REG8(0x371b), 0x05},
237 {CCI_REG8(0x371c), 0x01},
238 {CCI_REG8(0x371e), 0xa1},
239 {CCI_REG8(0x371f), 0x0c},
240 {CCI_REG8(0x3721), 0x00},
241 {CCI_REG8(0x3724), 0x10},
242 {CCI_REG8(0x3726), 0x00},
243 {CCI_REG8(0x372a), 0x01},
244 {CCI_REG8(0x3730), 0x10},
245 {CCI_REG8(0x3738), 0x22},
246 {CCI_REG8(0x3739), 0xe5},
247 {CCI_REG8(0x373a), 0x50},
248 {CCI_REG8(0x373b), 0x02},
249 {CCI_REG8(0x373c), 0x41},
250 {CCI_REG8(0x373f), 0x02},
251 {CCI_REG8(0x3740), 0x42},
252 {CCI_REG8(0x3741), 0x02},
253 {CCI_REG8(0x3742), 0x18},
254 {CCI_REG8(0x3743), 0x01},
255 {CCI_REG8(0x3744), 0x02},
256 {CCI_REG8(0x3747), 0x10},
257 {CCI_REG8(0x374c), 0x04},
258 {CCI_REG8(0x3751), 0xf0},
259 {CCI_REG8(0x3752), 0x00},
260 {CCI_REG8(0x3753), 0x00},
261 {CCI_REG8(0x3754), 0xc0},
262 {CCI_REG8(0x3755), 0x00},
263 {CCI_REG8(0x3756), 0x1a},
264 {CCI_REG8(0x3758), 0x00},
265 {CCI_REG8(0x3759), 0x0f},
266 {CCI_REG8(0x376b), 0x44},
267 {CCI_REG8(0x375c), 0x04},
268 {CCI_REG8(0x3774), 0x10},
269 {CCI_REG8(0x3776), 0x00},
270 {CCI_REG8(0x377f), 0x08},
271 {CCI_REG8(0x3780), 0x22},
272 {CCI_REG8(0x3781), 0x0c},
273 {CCI_REG8(0x3784), 0x2c},
274 {CCI_REG8(0x3785), 0x1e},
275 {CCI_REG8(0x378f), 0xf5},
276 {CCI_REG8(0x3791), 0xb0},
277 {CCI_REG8(0x3795), 0x00},
278 {CCI_REG8(0x3796), 0x64},
279 {CCI_REG8(0x3797), 0x11},
280 {CCI_REG8(0x3798), 0x30},
281 {CCI_REG8(0x3799), 0x41},
282 {CCI_REG8(0x379a), 0x07},
283 {CCI_REG8(0x379b), 0xb0},
284 {CCI_REG8(0x379c), 0x0c},
285 {CCI_REG8(0x3a04), 0x06},
286 {CCI_REG8(0x3a05), 0x14},
287 {CCI_REG8(0x3e07), 0x20},
288 {CCI_REG8(0x4000), 0x08},
289 {CCI_REG8(0x4001), 0x04},
290 {CCI_REG8(0x4004), 0x08},
291 {CCI_REG8(0x4006), 0x20},
292 {CCI_REG8(0x4008), 0x24},
293 {CCI_REG8(0x4009), 0x10},
294 {CCI_REG8(0x4058), 0x00},
295 {CCI_REG8(0x4101), 0xb2},
296 {CCI_REG8(0x4307), 0x31},
297 {CCI_REG8(0x4511), 0x05},
298 {CCI_REG8(0x4512), 0x01},
299 {CCI_REG8(0x481f), 0x30},
300 {CCI_REG8(0x4826), 0x2c},
301 {CCI_REG8(0x4d02), 0xfd},
302 {CCI_REG8(0x4d03), 0xf5},
303 {CCI_REG8(0x4d04), 0x0c},
304 {CCI_REG8(0x4d05), 0xcc},
305 {CCI_REG8(0x4837), 0x0a},
306 {CCI_REG8(0x5003), 0x20},
307 {CCI_REG8(0x5013), 0x00},
308 {CCI_REG8(0x5842), 0x01},
309 {CCI_REG8(0x5843), 0x2b},
310 {CCI_REG8(0x5844), 0x01},
311 {CCI_REG8(0x5845), 0x92},
312 {CCI_REG8(0x5846), 0x01},
313 {CCI_REG8(0x5847), 0x8f},
314 {CCI_REG8(0x5848), 0x01},
315 {CCI_REG8(0x5849), 0x0c},
316 {CCI_REG8(0x5e10), 0x0c},
317 {CCI_REG8(0x3820), 0x00},
318 {CCI_REG8(0x3821), 0x1e},
319 {CCI_REG8(0x5041), 0x14}
322 static const struct v4l2_rect ov5693_default_crop
= {
323 .left
= OV5693_ACTIVE_START_LEFT
,
324 .top
= OV5693_ACTIVE_START_TOP
,
325 .width
= OV5693_ACTIVE_WIDTH
,
326 .height
= OV5693_ACTIVE_HEIGHT
,
329 static const struct v4l2_mbus_framefmt ov5693_default_fmt
= {
330 .width
= OV5693_ACTIVE_WIDTH
,
331 .height
= OV5693_ACTIVE_HEIGHT
,
332 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
335 static const s64 link_freq_menu_items
[] = {
336 OV5693_LINK_FREQ_419_2MHZ
339 static const char * const ov5693_test_pattern_menu
[] = {
343 "Colour Bars with Rolling Bar"
346 static const u8 ov5693_test_pattern_bits
[] = {
348 OV5693_TEST_PATTERN_ENABLE
| OV5693_TEST_PATTERN_RANDOM
,
349 OV5693_TEST_PATTERN_ENABLE
| OV5693_TEST_PATTERN_BARS
,
350 OV5693_TEST_PATTERN_ENABLE
| OV5693_TEST_PATTERN_BARS
|
351 OV5693_TEST_PATTERN_ROLLING
,
354 /* V4L2 Controls Functions */
356 static int ov5693_flip_vert_configure(struct ov5693_device
*ov5693
,
359 u8 bits
= OV5693_FORMAT1_FLIP_VERT_ISP_EN
|
360 OV5693_FORMAT1_FLIP_VERT_SENSOR_EN
;
363 ret
= cci_update_bits(ov5693
->regmap
, OV5693_FORMAT1_REG
, bits
,
364 enable
? bits
: 0, NULL
);
371 static int ov5693_flip_horz_configure(struct ov5693_device
*ov5693
,
374 u8 bits
= OV5693_FORMAT2_FLIP_HORZ_ISP_EN
|
375 OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN
;
378 ret
= cci_update_bits(ov5693
->regmap
, OV5693_FORMAT2_REG
, bits
,
379 enable
? bits
: 0, NULL
);
386 static int ov5693_get_exposure(struct ov5693_device
*ov5693
, s32
*value
)
391 ret
= cci_read(ov5693
->regmap
, OV5693_EXPOSURE_CTRL_REG
, &exposure
,
396 /* The lowest 4 bits are unsupported fractional bits */
397 *value
= exposure
>> 4;
402 static int ov5693_exposure_configure(struct ov5693_device
*ov5693
,
407 exposure
= (exposure
<< 4) & OV5693_EXPOSURE_CTRL_MASK
;
409 cci_write(ov5693
->regmap
, OV5693_EXPOSURE_CTRL_REG
, exposure
, &ret
);
414 static int ov5693_get_gain(struct ov5693_device
*ov5693
, u32
*gain
)
419 ret
= cci_read(ov5693
->regmap
, OV5693_GAIN_CTRL_REG
, &value
, NULL
);
423 /* As with exposure, the lowest 4 bits are fractional bits. */
429 static int ov5693_digital_gain_configure(struct ov5693_device
*ov5693
,
434 gain
&= OV5693_MWB_GAIN_MASK
;
436 cci_write(ov5693
->regmap
, OV5693_MWB_RED_GAIN_REG
, gain
, &ret
);
437 cci_write(ov5693
->regmap
, OV5693_MWB_GREEN_GAIN_REG
, gain
, &ret
);
438 cci_write(ov5693
->regmap
, OV5693_MWB_BLUE_GAIN_REG
, gain
, &ret
);
443 static int ov5693_analog_gain_configure(struct ov5693_device
*ov5693
, u32 gain
)
447 gain
= (gain
<< 4) & OV5693_GAIN_CTRL_MASK
;
449 cci_write(ov5693
->regmap
, OV5693_GAIN_CTRL_REG
, gain
, &ret
);
454 static int ov5693_vts_configure(struct ov5693_device
*ov5693
, u32 vblank
)
456 u16 vts
= ov5693
->mode
.format
.height
+ vblank
;
459 cci_write(ov5693
->regmap
, OV5693_TIMING_VTS_REG
, vts
, &ret
);
464 static int ov5693_test_pattern_configure(struct ov5693_device
*ov5693
, u32 idx
)
468 cci_write(ov5693
->regmap
, OV5693_TEST_PATTERN_REG
,
469 ov5693_test_pattern_bits
[idx
], &ret
);
474 static int ov5693_s_ctrl(struct v4l2_ctrl
*ctrl
)
476 struct ov5693_device
*ov5693
=
477 container_of(ctrl
->handler
, struct ov5693_device
, ctrls
.handler
);
480 /* If VBLANK is altered we need to update exposure to compensate */
481 if (ctrl
->id
== V4L2_CID_VBLANK
) {
484 exposure_max
= ov5693
->mode
.format
.height
+ ctrl
->val
-
485 OV5693_INTEGRATION_TIME_MARGIN
;
486 __v4l2_ctrl_modify_range(ov5693
->ctrls
.exposure
,
487 ov5693
->ctrls
.exposure
->minimum
,
489 ov5693
->ctrls
.exposure
->step
,
490 min(ov5693
->ctrls
.exposure
->val
,
494 /* Only apply changes to the controls if the device is powered up */
495 if (!pm_runtime_get_if_in_use(ov5693
->dev
))
499 case V4L2_CID_EXPOSURE
:
500 ret
= ov5693_exposure_configure(ov5693
, ctrl
->val
);
502 case V4L2_CID_ANALOGUE_GAIN
:
503 ret
= ov5693_analog_gain_configure(ov5693
, ctrl
->val
);
505 case V4L2_CID_DIGITAL_GAIN
:
506 ret
= ov5693_digital_gain_configure(ov5693
, ctrl
->val
);
509 ret
= ov5693_flip_horz_configure(ov5693
, !!ctrl
->val
);
512 ret
= ov5693_flip_vert_configure(ov5693
, !!ctrl
->val
);
514 case V4L2_CID_VBLANK
:
515 ret
= ov5693_vts_configure(ov5693
, ctrl
->val
);
517 case V4L2_CID_TEST_PATTERN
:
518 ret
= ov5693_test_pattern_configure(ov5693
, ctrl
->val
);
524 pm_runtime_put(ov5693
->dev
);
529 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
531 struct ov5693_device
*ov5693
= container_of(ctrl
->handler
,
532 struct ov5693_device
,
536 case V4L2_CID_EXPOSURE_ABSOLUTE
:
537 return ov5693_get_exposure(ov5693
, &ctrl
->val
);
538 case V4L2_CID_AUTOGAIN
:
539 return ov5693_get_gain(ov5693
, &ctrl
->val
);
545 static const struct v4l2_ctrl_ops ov5693_ctrl_ops
= {
546 .s_ctrl
= ov5693_s_ctrl
,
547 .g_volatile_ctrl
= ov5693_g_volatile_ctrl
550 /* System Control Functions */
552 static int ov5693_mode_configure(struct ov5693_device
*ov5693
)
554 const struct ov5693_mode
*mode
= &ov5693
->mode
;
558 cci_write(ov5693
->regmap
, OV5693_CROP_START_X_REG
, mode
->crop
.left
,
562 cci_write(ov5693
->regmap
, OV5693_OFFSET_START_X_REG
, 0, &ret
);
565 cci_write(ov5693
->regmap
, OV5693_OUTPUT_SIZE_X_REG
, mode
->format
.width
,
569 cci_write(ov5693
->regmap
, OV5693_CROP_END_X_REG
,
570 mode
->crop
.left
+ mode
->crop
.width
, &ret
);
572 /* Horizontal Total Size */
573 cci_write(ov5693
->regmap
, OV5693_TIMING_HTS_REG
, OV5693_FIXED_PPL
,
577 cci_write(ov5693
->regmap
, OV5693_CROP_START_Y_REG
, mode
->crop
.top
,
581 cci_write(ov5693
->regmap
, OV5693_OFFSET_START_Y_REG
, 0, &ret
);
584 cci_write(ov5693
->regmap
, OV5693_OUTPUT_SIZE_Y_REG
, mode
->format
.height
,
588 cci_write(ov5693
->regmap
, OV5693_CROP_END_Y_REG
,
589 mode
->crop
.top
+ mode
->crop
.height
, &ret
);
591 /* Subsample X increase */
592 cci_write(ov5693
->regmap
, OV5693_SUB_INC_X_REG
,
593 ((mode
->inc_x_odd
<< 4) & 0xf0) | 0x01, &ret
);
594 /* Subsample Y increase */
595 cci_write(ov5693
->regmap
, OV5693_SUB_INC_Y_REG
,
596 ((mode
->inc_y_odd
<< 4) & 0xf0) | 0x01, &ret
);
599 cci_update_bits(ov5693
->regmap
, OV5693_FORMAT1_REG
,
600 OV5693_FORMAT1_VBIN_EN
,
601 mode
->binning_y
? OV5693_FORMAT1_VBIN_EN
: 0, &ret
);
603 cci_update_bits(ov5693
->regmap
, OV5693_FORMAT2_REG
,
604 OV5693_FORMAT2_HBIN_EN
,
605 mode
->binning_x
? OV5693_FORMAT2_HBIN_EN
: 0, &ret
);
610 static int ov5693_enable_streaming(struct ov5693_device
*ov5693
, bool enable
)
614 cci_write(ov5693
->regmap
, OV5693_SW_STREAM_REG
,
615 enable
? OV5693_START_STREAMING
: OV5693_STOP_STREAMING
,
621 static int ov5693_sw_reset(struct ov5693_device
*ov5693
)
625 cci_write(ov5693
->regmap
, OV5693_SW_RESET_REG
, OV5693_SW_RESET
, &ret
);
630 static int ov5693_sensor_init(struct ov5693_device
*ov5693
)
634 ret
= ov5693_sw_reset(ov5693
);
636 return dev_err_probe(ov5693
->dev
, ret
,
637 "software reset error\n");
639 ret
= cci_multi_reg_write(ov5693
->regmap
, ov5693_global_regs
,
640 ARRAY_SIZE(ov5693_global_regs
), NULL
);
642 return dev_err_probe(ov5693
->dev
, ret
,
643 "global settings error\n");
645 ret
= ov5693_mode_configure(ov5693
);
647 return dev_err_probe(ov5693
->dev
, ret
,
648 "mode configure error\n");
650 ret
= ov5693_enable_streaming(ov5693
, false);
652 dev_err(ov5693
->dev
, "stop streaming error\n");
657 static void ov5693_sensor_powerdown(struct ov5693_device
*ov5693
)
659 gpiod_set_value_cansleep(ov5693
->reset
, 1);
660 gpiod_set_value_cansleep(ov5693
->powerdown
, 1);
662 regulator_bulk_disable(OV5693_NUM_SUPPLIES
, ov5693
->supplies
);
664 clk_disable_unprepare(ov5693
->xvclk
);
667 static int ov5693_sensor_powerup(struct ov5693_device
*ov5693
)
671 gpiod_set_value_cansleep(ov5693
->reset
, 1);
672 gpiod_set_value_cansleep(ov5693
->powerdown
, 1);
674 ret
= clk_prepare_enable(ov5693
->xvclk
);
676 dev_err(ov5693
->dev
, "Failed to enable clk\n");
680 ret
= regulator_bulk_enable(OV5693_NUM_SUPPLIES
, ov5693
->supplies
);
682 dev_err(ov5693
->dev
, "Failed to enable regulators\n");
686 gpiod_set_value_cansleep(ov5693
->powerdown
, 0);
687 gpiod_set_value_cansleep(ov5693
->reset
, 0);
689 usleep_range(5000, 7500);
694 ov5693_sensor_powerdown(ov5693
);
698 static int __maybe_unused
ov5693_sensor_suspend(struct device
*dev
)
700 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
701 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
703 ov5693_sensor_powerdown(ov5693
);
708 static int __maybe_unused
ov5693_sensor_resume(struct device
*dev
)
710 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
711 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
714 mutex_lock(&ov5693
->lock
);
716 ret
= ov5693_sensor_powerup(ov5693
);
720 ret
= ov5693_sensor_init(ov5693
);
722 dev_err(dev
, "ov5693 sensor init failure\n");
729 ov5693_sensor_powerdown(ov5693
);
731 mutex_unlock(&ov5693
->lock
);
735 static int ov5693_detect(struct ov5693_device
*ov5693
)
740 ret
= cci_read(ov5693
->regmap
, OV5693_REG_CHIP_ID
, &id
, NULL
);
744 if (id
!= OV5693_CHIP_ID
)
745 return dev_err_probe(ov5693
->dev
, -ENODEV
,
746 "sensor ID mismatch. Got 0x%04llx\n", id
);
751 /* V4L2 Framework callbacks */
753 static unsigned int __ov5693_calc_vts(u32 height
)
756 * We need to set a sensible default VTS for whatever format height we
757 * happen to be given from set_fmt(). This function just targets
758 * an even multiple of 30fps.
761 unsigned int tgt_fps
;
763 tgt_fps
= rounddown(OV5693_PIXEL_RATE
/ OV5693_FIXED_PPL
/ height
, 30);
765 return ALIGN_DOWN(OV5693_PIXEL_RATE
/ OV5693_FIXED_PPL
/ tgt_fps
, 2);
768 static struct v4l2_mbus_framefmt
*
769 __ov5693_get_pad_format(struct ov5693_device
*ov5693
,
770 struct v4l2_subdev_state
*state
,
771 unsigned int pad
, enum v4l2_subdev_format_whence which
)
774 case V4L2_SUBDEV_FORMAT_TRY
:
775 return v4l2_subdev_state_get_format(state
, pad
);
776 case V4L2_SUBDEV_FORMAT_ACTIVE
:
777 return &ov5693
->mode
.format
;
783 static struct v4l2_rect
*
784 __ov5693_get_pad_crop(struct ov5693_device
*ov5693
,
785 struct v4l2_subdev_state
*state
,
786 unsigned int pad
, enum v4l2_subdev_format_whence which
)
789 case V4L2_SUBDEV_FORMAT_TRY
:
790 return v4l2_subdev_state_get_crop(state
, pad
);
791 case V4L2_SUBDEV_FORMAT_ACTIVE
:
792 return &ov5693
->mode
.crop
;
798 static int ov5693_get_fmt(struct v4l2_subdev
*sd
,
799 struct v4l2_subdev_state
*state
,
800 struct v4l2_subdev_format
*format
)
802 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
804 format
->format
= ov5693
->mode
.format
;
809 static int ov5693_set_fmt(struct v4l2_subdev
*sd
,
810 struct v4l2_subdev_state
*state
,
811 struct v4l2_subdev_format
*format
)
813 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
814 const struct v4l2_rect
*crop
;
815 struct v4l2_mbus_framefmt
*fmt
;
816 unsigned int hratio
, vratio
;
817 unsigned int width
, height
;
821 crop
= __ov5693_get_pad_crop(ov5693
, state
, format
->pad
, format
->which
);
824 * Align to two to simplify the binning calculations below, and clamp
825 * the requested format at the crop rectangle
827 width
= clamp_t(unsigned int, ALIGN(format
->format
.width
, 2),
828 OV5693_MIN_CROP_WIDTH
, crop
->width
);
829 height
= clamp_t(unsigned int, ALIGN(format
->format
.height
, 2),
830 OV5693_MIN_CROP_HEIGHT
, crop
->height
);
833 * We can only support setting either the dimensions of the crop rect
834 * or those dimensions binned (separately) by a factor of two.
836 hratio
= clamp_t(unsigned int,
837 DIV_ROUND_CLOSEST(crop
->width
, width
), 1, 2);
838 vratio
= clamp_t(unsigned int,
839 DIV_ROUND_CLOSEST(crop
->height
, height
), 1, 2);
841 fmt
= __ov5693_get_pad_format(ov5693
, state
, format
->pad
,
844 fmt
->width
= crop
->width
/ hratio
;
845 fmt
->height
= crop
->height
/ vratio
;
846 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
848 format
->format
= *fmt
;
850 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
853 mutex_lock(&ov5693
->lock
);
855 ov5693
->mode
.binning_x
= hratio
> 1;
856 ov5693
->mode
.inc_x_odd
= hratio
> 1 ? 3 : 1;
857 ov5693
->mode
.binning_y
= vratio
> 1;
858 ov5693
->mode
.inc_y_odd
= vratio
> 1 ? 3 : 1;
860 ov5693
->mode
.vts
= __ov5693_calc_vts(fmt
->height
);
862 __v4l2_ctrl_modify_range(ov5693
->ctrls
.vblank
,
863 OV5693_TIMING_MIN_VTS
,
864 OV5693_TIMING_MAX_VTS
- fmt
->height
,
865 1, ov5693
->mode
.vts
- fmt
->height
);
866 __v4l2_ctrl_s_ctrl(ov5693
->ctrls
.vblank
,
867 ov5693
->mode
.vts
- fmt
->height
);
869 hblank
= OV5693_FIXED_PPL
- fmt
->width
;
870 __v4l2_ctrl_modify_range(ov5693
->ctrls
.hblank
, hblank
, hblank
, 1,
873 exposure_max
= ov5693
->mode
.vts
- OV5693_INTEGRATION_TIME_MARGIN
;
874 __v4l2_ctrl_modify_range(ov5693
->ctrls
.exposure
,
875 ov5693
->ctrls
.exposure
->minimum
, exposure_max
,
876 ov5693
->ctrls
.exposure
->step
,
877 min(ov5693
->ctrls
.exposure
->val
,
880 mutex_unlock(&ov5693
->lock
);
884 static int ov5693_get_selection(struct v4l2_subdev
*sd
,
885 struct v4l2_subdev_state
*state
,
886 struct v4l2_subdev_selection
*sel
)
888 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
890 switch (sel
->target
) {
891 case V4L2_SEL_TGT_CROP
:
892 mutex_lock(&ov5693
->lock
);
893 sel
->r
= *__ov5693_get_pad_crop(ov5693
, state
, sel
->pad
,
895 mutex_unlock(&ov5693
->lock
);
897 case V4L2_SEL_TGT_NATIVE_SIZE
:
900 sel
->r
.width
= OV5693_NATIVE_WIDTH
;
901 sel
->r
.height
= OV5693_NATIVE_HEIGHT
;
903 case V4L2_SEL_TGT_CROP_BOUNDS
:
904 case V4L2_SEL_TGT_CROP_DEFAULT
:
905 sel
->r
.top
= OV5693_ACTIVE_START_TOP
;
906 sel
->r
.left
= OV5693_ACTIVE_START_LEFT
;
907 sel
->r
.width
= OV5693_ACTIVE_WIDTH
;
908 sel
->r
.height
= OV5693_ACTIVE_HEIGHT
;
917 static int ov5693_set_selection(struct v4l2_subdev
*sd
,
918 struct v4l2_subdev_state
*state
,
919 struct v4l2_subdev_selection
*sel
)
921 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
922 struct v4l2_mbus_framefmt
*format
;
923 struct v4l2_rect
*__crop
;
924 struct v4l2_rect rect
;
926 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
930 * Clamp the boundaries of the crop rectangle to the size of the sensor
931 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
934 rect
.left
= clamp(ALIGN(sel
->r
.left
, 2), OV5693_NATIVE_START_LEFT
,
935 OV5693_NATIVE_WIDTH
);
936 rect
.top
= clamp(ALIGN(sel
->r
.top
, 2), OV5693_NATIVE_START_TOP
,
937 OV5693_NATIVE_HEIGHT
);
938 rect
.width
= clamp_t(unsigned int, ALIGN(sel
->r
.width
, 2),
939 OV5693_MIN_CROP_WIDTH
, OV5693_NATIVE_WIDTH
);
940 rect
.height
= clamp_t(unsigned int, ALIGN(sel
->r
.height
, 2),
941 OV5693_MIN_CROP_HEIGHT
, OV5693_NATIVE_HEIGHT
);
943 /* Make sure the crop rectangle isn't outside the bounds of the array */
944 rect
.width
= min_t(unsigned int, rect
.width
,
945 OV5693_NATIVE_WIDTH
- rect
.left
);
946 rect
.height
= min_t(unsigned int, rect
.height
,
947 OV5693_NATIVE_HEIGHT
- rect
.top
);
949 __crop
= __ov5693_get_pad_crop(ov5693
, state
, sel
->pad
, sel
->which
);
951 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
953 * Reset the output image size if the crop rectangle size has
956 format
= __ov5693_get_pad_format(ov5693
, state
, sel
->pad
,
958 format
->width
= rect
.width
;
959 format
->height
= rect
.height
;
968 static int ov5693_s_stream(struct v4l2_subdev
*sd
, int enable
)
970 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
974 ret
= pm_runtime_resume_and_get(ov5693
->dev
);
978 mutex_lock(&ov5693
->lock
);
979 ret
= __v4l2_ctrl_handler_setup(&ov5693
->ctrls
.handler
);
981 mutex_unlock(&ov5693
->lock
);
985 ret
= ov5693_enable_streaming(ov5693
, true);
986 mutex_unlock(&ov5693
->lock
);
988 mutex_lock(&ov5693
->lock
);
989 ret
= ov5693_enable_streaming(ov5693
, false);
990 mutex_unlock(&ov5693
->lock
);
996 pm_runtime_put(ov5693
->dev
);
1000 pm_runtime_put_noidle(ov5693
->dev
);
1004 static int ov5693_get_frame_interval(struct v4l2_subdev
*sd
,
1005 struct v4l2_subdev_state
*sd_state
,
1006 struct v4l2_subdev_frame_interval
*interval
)
1008 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
1009 unsigned int framesize
= OV5693_FIXED_PPL
* (ov5693
->mode
.format
.height
+
1010 ov5693
->ctrls
.vblank
->val
);
1011 unsigned int fps
= DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE
, framesize
);
1014 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1015 * subdev active state API.
1017 if (interval
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1020 interval
->interval
.numerator
= 1;
1021 interval
->interval
.denominator
= fps
;
1026 static int ov5693_enum_mbus_code(struct v4l2_subdev
*sd
,
1027 struct v4l2_subdev_state
*state
,
1028 struct v4l2_subdev_mbus_code_enum
*code
)
1030 /* Only a single mbus format is supported */
1031 if (code
->index
> 0)
1034 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1038 static int ov5693_enum_frame_size(struct v4l2_subdev
*sd
,
1039 struct v4l2_subdev_state
*state
,
1040 struct v4l2_subdev_frame_size_enum
*fse
)
1042 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
1043 struct v4l2_rect
*__crop
;
1045 if (fse
->index
> 1 || fse
->code
!= MEDIA_BUS_FMT_SBGGR10_1X10
)
1048 __crop
= __ov5693_get_pad_crop(ov5693
, state
, fse
->pad
, fse
->which
);
1052 fse
->min_width
= __crop
->width
/ (fse
->index
+ 1);
1053 fse
->min_height
= __crop
->height
/ (fse
->index
+ 1);
1054 fse
->max_width
= fse
->min_width
;
1055 fse
->max_height
= fse
->min_height
;
1060 static const struct v4l2_subdev_video_ops ov5693_video_ops
= {
1061 .s_stream
= ov5693_s_stream
,
1064 static const struct v4l2_subdev_pad_ops ov5693_pad_ops
= {
1065 .enum_mbus_code
= ov5693_enum_mbus_code
,
1066 .enum_frame_size
= ov5693_enum_frame_size
,
1067 .get_fmt
= ov5693_get_fmt
,
1068 .set_fmt
= ov5693_set_fmt
,
1069 .get_selection
= ov5693_get_selection
,
1070 .set_selection
= ov5693_set_selection
,
1071 .get_frame_interval
= ov5693_get_frame_interval
,
1074 static const struct v4l2_subdev_ops ov5693_ops
= {
1075 .video
= &ov5693_video_ops
,
1076 .pad
= &ov5693_pad_ops
,
1079 /* Sensor and Driver Configuration Functions */
1081 static int ov5693_init_controls(struct ov5693_device
*ov5693
)
1083 const struct v4l2_ctrl_ops
*ops
= &ov5693_ctrl_ops
;
1084 struct ov5693_v4l2_ctrls
*ctrls
= &ov5693
->ctrls
;
1085 struct v4l2_fwnode_device_properties props
;
1086 int vblank_max
, vblank_def
;
1091 ret
= v4l2_ctrl_handler_init(&ctrls
->handler
, 12);
1096 ctrls
->link_freq
= v4l2_ctrl_new_int_menu(&ctrls
->handler
,
1097 NULL
, V4L2_CID_LINK_FREQ
,
1098 0, 0, link_freq_menu_items
);
1099 if (ctrls
->link_freq
)
1100 ctrls
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1103 ctrls
->pixel_rate
= v4l2_ctrl_new_std(&ctrls
->handler
, NULL
,
1104 V4L2_CID_PIXEL_RATE
, 0,
1105 OV5693_PIXEL_RATE
, 1,
1109 exposure_max
= ov5693
->mode
.vts
- OV5693_INTEGRATION_TIME_MARGIN
;
1110 ctrls
->exposure
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1112 OV5693_EXPOSURE_MIN
, exposure_max
,
1113 OV5693_EXPOSURE_STEP
, exposure_max
);
1116 ctrls
->analogue_gain
= v4l2_ctrl_new_std(&ctrls
->handler
,
1117 ops
, V4L2_CID_ANALOGUE_GAIN
,
1123 ctrls
->digital_gain
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1124 V4L2_CID_DIGITAL_GAIN
,
1125 OV5693_DIGITAL_GAIN_MIN
,
1126 OV5693_DIGITAL_GAIN_MAX
,
1127 OV5693_DIGITAL_GAIN_STEP
,
1128 OV5693_DIGITAL_GAIN_DEF
);
1131 ctrls
->hflip
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1132 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1134 ctrls
->vflip
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1135 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1137 hblank
= OV5693_FIXED_PPL
- ov5693
->mode
.format
.width
;
1138 ctrls
->hblank
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1139 V4L2_CID_HBLANK
, hblank
,
1143 ctrls
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1145 vblank_max
= OV5693_TIMING_MAX_VTS
- ov5693
->mode
.format
.height
;
1146 vblank_def
= ov5693
->mode
.vts
- ov5693
->mode
.format
.height
;
1147 ctrls
->vblank
= v4l2_ctrl_new_std(&ctrls
->handler
, ops
,
1149 OV5693_TIMING_MIN_VTS
,
1150 vblank_max
, 1, vblank_def
);
1152 ctrls
->test_pattern
= v4l2_ctrl_new_std_menu_items(
1153 &ctrls
->handler
, ops
,
1154 V4L2_CID_TEST_PATTERN
,
1155 ARRAY_SIZE(ov5693_test_pattern_menu
) - 1,
1156 0, 0, ov5693_test_pattern_menu
);
1158 if (ctrls
->handler
.error
) {
1159 dev_err(ov5693
->dev
, "Error initialising v4l2 ctrls\n");
1160 ret
= ctrls
->handler
.error
;
1161 goto err_free_handler
;
1164 /* set properties from fwnode (e.g. rotation, orientation) */
1165 ret
= v4l2_fwnode_device_parse(ov5693
->dev
, &props
);
1167 goto err_free_handler
;
1169 ret
= v4l2_ctrl_new_fwnode_properties(&ctrls
->handler
, ops
,
1172 goto err_free_handler
;
1174 /* Use same lock for controls as for everything else. */
1175 ctrls
->handler
.lock
= &ov5693
->lock
;
1176 ov5693
->sd
.ctrl_handler
= &ctrls
->handler
;
1181 v4l2_ctrl_handler_free(&ctrls
->handler
);
1185 static int ov5693_configure_gpios(struct ov5693_device
*ov5693
)
1187 ov5693
->reset
= devm_gpiod_get_optional(ov5693
->dev
, "reset",
1189 if (IS_ERR(ov5693
->reset
)) {
1190 dev_err(ov5693
->dev
, "Error fetching reset GPIO\n");
1191 return PTR_ERR(ov5693
->reset
);
1194 ov5693
->powerdown
= devm_gpiod_get_optional(ov5693
->dev
, "powerdown",
1196 if (IS_ERR(ov5693
->powerdown
)) {
1197 dev_err(ov5693
->dev
, "Error fetching powerdown GPIO\n");
1198 return PTR_ERR(ov5693
->powerdown
);
1204 static int ov5693_get_regulators(struct ov5693_device
*ov5693
)
1208 for (i
= 0; i
< OV5693_NUM_SUPPLIES
; i
++)
1209 ov5693
->supplies
[i
].supply
= ov5693_supply_names
[i
];
1211 return devm_regulator_bulk_get(ov5693
->dev
, OV5693_NUM_SUPPLIES
,
1215 static int ov5693_check_hwcfg(struct ov5693_device
*ov5693
)
1217 struct fwnode_handle
*fwnode
= dev_fwnode(ov5693
->dev
);
1218 struct v4l2_fwnode_endpoint bus_cfg
= {
1219 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
1221 struct fwnode_handle
*endpoint
;
1225 endpoint
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1227 return -EPROBE_DEFER
; /* Could be provided by cio2-bridge */
1229 ret
= v4l2_fwnode_endpoint_alloc_parse(endpoint
, &bus_cfg
);
1230 fwnode_handle_put(endpoint
);
1234 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= 2) {
1235 dev_err(ov5693
->dev
, "only a 2-lane CSI2 config is supported");
1237 goto out_free_bus_cfg
;
1240 if (!bus_cfg
.nr_of_link_frequencies
) {
1241 dev_err(ov5693
->dev
, "no link frequencies defined\n");
1243 goto out_free_bus_cfg
;
1246 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++)
1247 if (bus_cfg
.link_frequencies
[i
] == OV5693_LINK_FREQ_419_2MHZ
)
1250 if (i
== bus_cfg
.nr_of_link_frequencies
) {
1251 dev_err(ov5693
->dev
, "supported link freq %ull not found\n",
1252 OV5693_LINK_FREQ_419_2MHZ
);
1254 goto out_free_bus_cfg
;
1258 v4l2_fwnode_endpoint_free(&bus_cfg
);
1263 static int ov5693_probe(struct i2c_client
*client
)
1265 struct ov5693_device
*ov5693
;
1269 ov5693
= devm_kzalloc(&client
->dev
, sizeof(*ov5693
), GFP_KERNEL
);
1273 ov5693
->dev
= &client
->dev
;
1275 ov5693
->regmap
= devm_cci_regmap_init_i2c(client
, 16);
1276 if (IS_ERR(ov5693
->regmap
))
1277 return PTR_ERR(ov5693
->regmap
);
1279 ret
= ov5693_check_hwcfg(ov5693
);
1283 mutex_init(&ov5693
->lock
);
1285 v4l2_i2c_subdev_init(&ov5693
->sd
, client
, &ov5693_ops
);
1287 ov5693
->xvclk
= devm_clk_get_optional(&client
->dev
, "xvclk");
1288 if (IS_ERR(ov5693
->xvclk
))
1289 return dev_err_probe(&client
->dev
, PTR_ERR(ov5693
->xvclk
),
1290 "failed to get xvclk: %ld\n",
1291 PTR_ERR(ov5693
->xvclk
));
1293 if (ov5693
->xvclk
) {
1294 xvclk_rate
= clk_get_rate(ov5693
->xvclk
);
1296 ret
= fwnode_property_read_u32(dev_fwnode(&client
->dev
),
1301 dev_err(&client
->dev
, "can't get clock frequency");
1306 if (xvclk_rate
!= OV5693_XVCLK_FREQ
)
1307 dev_warn(&client
->dev
, "Found clk freq %u, expected %u\n",
1308 xvclk_rate
, OV5693_XVCLK_FREQ
);
1310 ret
= ov5693_configure_gpios(ov5693
);
1314 ret
= ov5693_get_regulators(ov5693
);
1316 return dev_err_probe(&client
->dev
, ret
,
1317 "Error fetching regulators\n");
1319 ov5693
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1320 ov5693
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1321 ov5693
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1323 ov5693
->mode
.crop
= ov5693_default_crop
;
1324 ov5693
->mode
.format
= ov5693_default_fmt
;
1325 ov5693
->mode
.vts
= __ov5693_calc_vts(ov5693
->mode
.format
.height
);
1327 ret
= ov5693_init_controls(ov5693
);
1331 ret
= media_entity_pads_init(&ov5693
->sd
.entity
, 1, &ov5693
->pad
);
1333 goto err_ctrl_handler_free
;
1336 * We need the driver to work in the event that pm runtime is disable in
1337 * the kernel, so power up and verify the chip now. In the event that
1338 * runtime pm is disabled this will leave the chip on, so that streaming
1342 ret
= ov5693_sensor_powerup(ov5693
);
1344 goto err_media_entity_cleanup
;
1346 ret
= ov5693_detect(ov5693
);
1350 pm_runtime_set_active(&client
->dev
);
1351 pm_runtime_get_noresume(&client
->dev
);
1352 pm_runtime_enable(&client
->dev
);
1354 ret
= v4l2_async_register_subdev_sensor(&ov5693
->sd
);
1356 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1358 goto err_pm_runtime
;
1361 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
1362 pm_runtime_use_autosuspend(&client
->dev
);
1363 pm_runtime_put_autosuspend(&client
->dev
);
1368 pm_runtime_disable(&client
->dev
);
1369 pm_runtime_put_noidle(&client
->dev
);
1371 ov5693_sensor_powerdown(ov5693
);
1372 err_media_entity_cleanup
:
1373 media_entity_cleanup(&ov5693
->sd
.entity
);
1374 err_ctrl_handler_free
:
1375 v4l2_ctrl_handler_free(&ov5693
->ctrls
.handler
);
1380 static void ov5693_remove(struct i2c_client
*client
)
1382 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1383 struct ov5693_device
*ov5693
= to_ov5693_sensor(sd
);
1385 v4l2_async_unregister_subdev(sd
);
1386 media_entity_cleanup(&ov5693
->sd
.entity
);
1387 v4l2_ctrl_handler_free(&ov5693
->ctrls
.handler
);
1388 mutex_destroy(&ov5693
->lock
);
1391 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1392 * make sure to turn power off manually.
1394 pm_runtime_disable(&client
->dev
);
1395 if (!pm_runtime_status_suspended(&client
->dev
))
1396 ov5693_sensor_powerdown(ov5693
);
1397 pm_runtime_set_suspended(&client
->dev
);
1400 static const struct dev_pm_ops ov5693_pm_ops
= {
1401 SET_RUNTIME_PM_OPS(ov5693_sensor_suspend
, ov5693_sensor_resume
, NULL
)
1404 static const struct acpi_device_id ov5693_acpi_match
[] = {
1408 MODULE_DEVICE_TABLE(acpi
, ov5693_acpi_match
);
1410 static const struct of_device_id ov5693_of_match
[] = {
1411 { .compatible
= "ovti,ov5693", },
1414 MODULE_DEVICE_TABLE(of
, ov5693_of_match
);
1416 static struct i2c_driver ov5693_driver
= {
1419 .acpi_match_table
= ov5693_acpi_match
,
1420 .of_match_table
= ov5693_of_match
,
1421 .pm
= &ov5693_pm_ops
,
1423 .probe
= ov5693_probe
,
1424 .remove
= ov5693_remove
,
1426 module_i2c_driver(ov5693_driver
);
1428 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1429 MODULE_LICENSE("GPL");