1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright (C) 2014-2017 Mentor Graphics Inc.
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
29 /* min/typical/max system clock (xclk) frequencies */
30 #define OV5640_XCLK_MIN 6000000
31 #define OV5640_XCLK_MAX 54000000
33 #define OV5640_NATIVE_WIDTH 2624
34 #define OV5640_NATIVE_HEIGHT 1964
35 #define OV5640_PIXEL_ARRAY_TOP 14
36 #define OV5640_PIXEL_ARRAY_LEFT 16
37 #define OV5640_PIXEL_ARRAY_WIDTH 2592
38 #define OV5640_PIXEL_ARRAY_HEIGHT 1944
40 /* FIXME: not documented. */
41 #define OV5640_MIN_VBLANK 24
42 #define OV5640_MAX_VTS 3375
44 #define OV5640_DEFAULT_SLAVE_ID 0x3c
46 #define OV5640_LINK_RATE_MAX 490000000U
48 #define OV5640_REG_SYS_RESET02 0x3002
49 #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
50 #define OV5640_REG_SYS_CTRL0 0x3008
51 #define OV5640_REG_SYS_CTRL0_SW_PWDN 0x42
52 #define OV5640_REG_SYS_CTRL0_SW_PWUP 0x02
53 #define OV5640_REG_SYS_CTRL0_SW_RST 0x82
54 #define OV5640_REG_CHIP_ID 0x300a
55 #define OV5640_REG_IO_MIPI_CTRL00 0x300e
56 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
57 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
58 #define OV5640_REG_PAD_OUTPUT00 0x3019
59 #define OV5640_REG_SYSTEM_CONTROL1 0x302e
60 #define OV5640_REG_SC_PLL_CTRL0 0x3034
61 #define OV5640_REG_SC_PLL_CTRL1 0x3035
62 #define OV5640_REG_SC_PLL_CTRL2 0x3036
63 #define OV5640_REG_SC_PLL_CTRL3 0x3037
64 #define OV5640_REG_SLAVE_ID 0x3100
65 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
66 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
67 #define OV5640_REG_AWB_R_GAIN 0x3400
68 #define OV5640_REG_AWB_G_GAIN 0x3402
69 #define OV5640_REG_AWB_B_GAIN 0x3404
70 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
71 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
72 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
73 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
74 #define OV5640_REG_AEC_PK_MANUAL 0x3503
75 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
76 #define OV5640_REG_AEC_PK_VTS 0x350c
77 #define OV5640_REG_TIMING_HS 0x3800
78 #define OV5640_REG_TIMING_VS 0x3802
79 #define OV5640_REG_TIMING_HW 0x3804
80 #define OV5640_REG_TIMING_VH 0x3806
81 #define OV5640_REG_TIMING_DVPHO 0x3808
82 #define OV5640_REG_TIMING_DVPVO 0x380a
83 #define OV5640_REG_TIMING_HTS 0x380c
84 #define OV5640_REG_TIMING_VTS 0x380e
85 #define OV5640_REG_TIMING_HOFFS 0x3810
86 #define OV5640_REG_TIMING_VOFFS 0x3812
87 #define OV5640_REG_TIMING_TC_REG20 0x3820
88 #define OV5640_REG_TIMING_TC_REG21 0x3821
89 #define OV5640_REG_AEC_CTRL00 0x3a00
90 #define OV5640_REG_AEC_B50_STEP 0x3a08
91 #define OV5640_REG_AEC_B60_STEP 0x3a0a
92 #define OV5640_REG_AEC_CTRL0D 0x3a0d
93 #define OV5640_REG_AEC_CTRL0E 0x3a0e
94 #define OV5640_REG_AEC_CTRL0F 0x3a0f
95 #define OV5640_REG_AEC_CTRL10 0x3a10
96 #define OV5640_REG_AEC_CTRL11 0x3a11
97 #define OV5640_REG_AEC_CTRL1B 0x3a1b
98 #define OV5640_REG_AEC_CTRL1E 0x3a1e
99 #define OV5640_REG_AEC_CTRL1F 0x3a1f
100 #define OV5640_REG_HZ5060_CTRL00 0x3c00
101 #define OV5640_REG_HZ5060_CTRL01 0x3c01
102 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
103 #define OV5640_REG_FRAME_CTRL01 0x4202
104 #define OV5640_REG_FORMAT_CONTROL00 0x4300
105 #define OV5640_REG_VFIFO_HSIZE 0x4602
106 #define OV5640_REG_VFIFO_VSIZE 0x4604
107 #define OV5640_REG_JPG_MODE_SELECT 0x4713
108 #define OV5640_REG_CCIR656_CTRL00 0x4730
109 #define OV5640_REG_POLARITY_CTRL00 0x4740
110 #define OV5640_REG_MIPI_CTRL00 0x4800
111 #define OV5640_REG_DEBUG_MODE 0x4814
112 #define OV5640_REG_PCLK_PERIOD 0x4837
113 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
114 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
115 #define OV5640_REG_SDE_CTRL0 0x5580
116 #define OV5640_REG_SDE_CTRL1 0x5581
117 #define OV5640_REG_SDE_CTRL3 0x5583
118 #define OV5640_REG_SDE_CTRL4 0x5584
119 #define OV5640_REG_SDE_CTRL5 0x5585
120 #define OV5640_REG_AVG_READOUT 0x56a1
122 enum ov5640_mode_id {
123 OV5640_MODE_QQVGA_160_120 = 0,
124 OV5640_MODE_QCIF_176_144,
125 OV5640_MODE_QVGA_320_240,
126 OV5640_MODE_VGA_640_480,
127 OV5640_MODE_NTSC_720_480,
128 OV5640_MODE_PAL_720_576,
129 OV5640_MODE_XGA_1024_768,
130 OV5640_MODE_720P_1280_720,
131 OV5640_MODE_1080P_1920_1080,
132 OV5640_MODE_QSXGA_2592_1944,
136 enum ov5640_frame_rate {
140 OV5640_NUM_FRAMERATES,
143 enum ov5640_pixel_rate_id {
144 OV5640_PIXEL_RATE_168M,
145 OV5640_PIXEL_RATE_148M,
146 OV5640_PIXEL_RATE_124M,
147 OV5640_PIXEL_RATE_96M,
148 OV5640_PIXEL_RATE_48M,
149 OV5640_NUM_PIXEL_RATES,
153 * The chip manual suggests 24/48/96/192 MHz pixel clocks.
155 * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
156 * full resolution mode @15 FPS.
158 static const u32 ov5640_pixel_rates[] = {
159 [OV5640_PIXEL_RATE_168M] = 168000000,
160 [OV5640_PIXEL_RATE_148M] = 148000000,
161 [OV5640_PIXEL_RATE_124M] = 124000000,
162 [OV5640_PIXEL_RATE_96M] = 96000000,
163 [OV5640_PIXEL_RATE_48M] = 48000000,
167 * MIPI CSI-2 link frequencies.
169 * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
170 * data_lanes = (1, 2)
172 * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
174 static const s64 ov5640_csi2_link_freqs[] = {
175 992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
176 592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
177 384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
178 248000000, 192000000, 192000000, 192000000, 96000000,
181 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
182 #define OV5640_DEFAULT_LINK_FREQ 13
184 enum ov5640_format_mux {
185 OV5640_FMT_MUX_YUV422 = 0,
187 OV5640_FMT_MUX_DITHER,
188 OV5640_FMT_MUX_RAW_DPC,
189 OV5640_FMT_MUX_SNR_RAW,
190 OV5640_FMT_MUX_RAW_CIP,
193 struct ov5640_pixfmt {
198 enum ov5640_format_mux mux;
201 static const struct ov5640_pixfmt ov5640_dvp_formats[] = {
204 .code = MEDIA_BUS_FMT_JPEG_1X8,
205 .colorspace = V4L2_COLORSPACE_JPEG,
208 .mux = OV5640_FMT_MUX_YUV422,
211 .code = MEDIA_BUS_FMT_UYVY8_2X8,
212 .colorspace = V4L2_COLORSPACE_SRGB,
215 .mux = OV5640_FMT_MUX_YUV422,
218 .code = MEDIA_BUS_FMT_YUYV8_2X8,
219 .colorspace = V4L2_COLORSPACE_SRGB,
222 .mux = OV5640_FMT_MUX_YUV422,
224 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
225 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
226 .colorspace = V4L2_COLORSPACE_SRGB,
229 .mux = OV5640_FMT_MUX_RGB,
231 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
232 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
233 .colorspace = V4L2_COLORSPACE_SRGB,
236 .mux = OV5640_FMT_MUX_RGB,
238 /* Raw, BGBG... / GRGR... */
239 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
240 .colorspace = V4L2_COLORSPACE_SRGB,
243 .mux = OV5640_FMT_MUX_RAW_DPC,
245 /* Raw bayer, GBGB... / RGRG... */
246 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
247 .colorspace = V4L2_COLORSPACE_SRGB,
250 .mux = OV5640_FMT_MUX_RAW_DPC,
252 /* Raw bayer, GRGR... / BGBG... */
253 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
254 .colorspace = V4L2_COLORSPACE_SRGB,
257 .mux = OV5640_FMT_MUX_RAW_DPC,
259 /* Raw bayer, RGRG... / GBGB... */
260 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
261 .colorspace = V4L2_COLORSPACE_SRGB,
264 .mux = OV5640_FMT_MUX_RAW_DPC,
269 static const struct ov5640_pixfmt ov5640_csi2_formats[] = {
272 .code = MEDIA_BUS_FMT_JPEG_1X8,
273 .colorspace = V4L2_COLORSPACE_JPEG,
276 .mux = OV5640_FMT_MUX_YUV422,
279 .code = MEDIA_BUS_FMT_UYVY8_1X16,
280 .colorspace = V4L2_COLORSPACE_SRGB,
283 .mux = OV5640_FMT_MUX_YUV422,
286 .code = MEDIA_BUS_FMT_YUYV8_1X16,
287 .colorspace = V4L2_COLORSPACE_SRGB,
290 .mux = OV5640_FMT_MUX_YUV422,
292 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
293 .code = MEDIA_BUS_FMT_RGB565_1X16,
294 .colorspace = V4L2_COLORSPACE_SRGB,
297 .mux = OV5640_FMT_MUX_RGB,
300 .code = MEDIA_BUS_FMT_BGR888_1X24,
301 .colorspace = V4L2_COLORSPACE_SRGB,
304 .mux = OV5640_FMT_MUX_RGB,
306 /* Raw, BGBG... / GRGR... */
307 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
308 .colorspace = V4L2_COLORSPACE_SRGB,
311 .mux = OV5640_FMT_MUX_RAW_DPC,
313 /* Raw bayer, GBGB... / RGRG... */
314 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
315 .colorspace = V4L2_COLORSPACE_SRGB,
318 .mux = OV5640_FMT_MUX_RAW_DPC,
320 /* Raw bayer, GRGR... / BGBG... */
321 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
322 .colorspace = V4L2_COLORSPACE_SRGB,
325 .mux = OV5640_FMT_MUX_RAW_DPC,
327 /* Raw bayer, RGRG... / GBGB... */
328 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
329 .colorspace = V4L2_COLORSPACE_SRGB,
332 .mux = OV5640_FMT_MUX_RAW_DPC,
338 * FIXME: remove this when a subdev API becomes available
339 * to set the MIPI CSI-2 virtual channel.
341 static unsigned int virtual_channel;
342 module_param(virtual_channel, uint, 0444);
343 MODULE_PARM_DESC(virtual_channel,
344 "MIPI CSI-2 virtual channel (0..3), default 0");
346 static const int ov5640_framerates[] = {
347 [OV5640_15_FPS] = 15,
348 [OV5640_30_FPS] = 30,
349 [OV5640_60_FPS] = 60,
352 /* regulator supplies */
353 static const char * const ov5640_supply_name[] = {
354 "DOVDD", /* Digital I/O (1.8V) supply */
355 "AVDD", /* Analog (2.8V) supply */
356 "DVDD", /* Digital Core (1.5V) supply */
359 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
362 * Image size under 1280 * 960 are SUBSAMPLING
363 * Image size upper 1280 * 960 are SCALING
365 enum ov5640_downsize_mode {
377 struct ov5640_timings {
378 /* Analog crop rectangle. */
379 struct v4l2_rect analog_crop;
380 /* Visibile crop: from analog crop top-left corner. */
381 struct v4l2_rect crop;
382 /* Total pixels per line: width + fixed hblank. */
384 /* Default vertical blanking: frame height = height + vblank. */
388 struct ov5640_mode_info {
389 enum ov5640_mode_id id;
390 enum ov5640_downsize_mode dn_mode;
391 enum ov5640_pixel_rate_id pixel_rate;
396 struct ov5640_timings dvp_timings;
397 struct ov5640_timings csi2_timings;
399 const struct reg_value *reg_data;
402 /* Used by s_frame_interval only. */
407 struct ov5640_ctrls {
408 struct v4l2_ctrl_handler handler;
409 struct v4l2_ctrl *pixel_rate;
410 struct v4l2_ctrl *link_freq;
411 struct v4l2_ctrl *hblank;
412 struct v4l2_ctrl *vblank;
414 struct v4l2_ctrl *auto_exp;
415 struct v4l2_ctrl *exposure;
418 struct v4l2_ctrl *auto_wb;
419 struct v4l2_ctrl *blue_balance;
420 struct v4l2_ctrl *red_balance;
423 struct v4l2_ctrl *auto_gain;
424 struct v4l2_ctrl *gain;
426 struct v4l2_ctrl *brightness;
427 struct v4l2_ctrl *light_freq;
428 struct v4l2_ctrl *saturation;
429 struct v4l2_ctrl *contrast;
430 struct v4l2_ctrl *hue;
431 struct v4l2_ctrl *test_pattern;
432 struct v4l2_ctrl *hflip;
433 struct v4l2_ctrl *vflip;
437 struct i2c_client *i2c_client;
438 struct v4l2_subdev sd;
439 struct media_pad pad;
440 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
441 struct clk *xclk; /* system clock to OV5640 */
444 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
445 struct gpio_desc *reset_gpio;
446 struct gpio_desc *pwdn_gpio;
449 /* lock to protect all members below */
452 struct v4l2_mbus_framefmt fmt;
453 bool pending_fmt_change;
455 const struct ov5640_mode_info *current_mode;
456 const struct ov5640_mode_info *last_mode;
457 enum ov5640_frame_rate current_fr;
458 struct v4l2_fract frame_interval;
459 s64 current_link_freq;
461 struct ov5640_ctrls ctrls;
463 u32 prev_sysclk, prev_hts;
464 u32 ae_low, ae_high, ae_target;
466 bool pending_mode_change;
470 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
472 return container_of(sd, struct ov5640_dev, sd);
475 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
477 return &container_of(ctrl->handler, struct ov5640_dev,
481 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
483 return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
486 static inline const struct ov5640_pixfmt *
487 ov5640_formats(struct ov5640_dev *sensor)
489 return ov5640_is_csi2(sensor) ? ov5640_csi2_formats
490 : ov5640_dvp_formats;
493 static const struct ov5640_pixfmt *
494 ov5640_code_to_pixfmt(struct ov5640_dev *sensor, u32 code)
496 const struct ov5640_pixfmt *formats = ov5640_formats(sensor);
499 for (i = 0; formats[i].code; ++i) {
500 if (formats[i].code == code)
507 static u32 ov5640_code_to_bpp(struct ov5640_dev *sensor, u32 code)
509 const struct ov5640_pixfmt *format = ov5640_code_to_pixfmt(sensor,
516 * FIXME: all of these register tables are likely filled with
517 * entries that set the register to their power-on default values,
518 * and which are otherwise not touched by this driver. Those entries
519 * should be identified and removed to speed register load time
522 /* YUV422 UYVY VGA@30fps */
524 static const struct v4l2_mbus_framefmt ov5640_csi2_default_fmt = {
525 .code = MEDIA_BUS_FMT_UYVY8_1X16,
528 .colorspace = V4L2_COLORSPACE_SRGB,
529 .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
530 .quantization = V4L2_QUANTIZATION_FULL_RANGE,
531 .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
532 .field = V4L2_FIELD_NONE,
535 static const struct v4l2_mbus_framefmt ov5640_dvp_default_fmt = {
536 .code = MEDIA_BUS_FMT_UYVY8_2X8,
539 .colorspace = V4L2_COLORSPACE_SRGB,
540 .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
541 .quantization = V4L2_QUANTIZATION_FULL_RANGE,
542 .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
543 .field = V4L2_FIELD_NONE,
546 static const struct reg_value ov5640_init_setting[] = {
547 {0x3103, 0x11, 0, 0},
548 {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
549 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
550 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
551 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
552 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
553 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
554 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
555 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
556 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
557 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
558 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
559 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
560 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
561 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
562 {0x3815, 0x31, 0, 0},
563 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
564 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
565 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
566 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
567 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
568 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
569 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
570 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
571 {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
572 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
573 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
574 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
575 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
576 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
577 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
578 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
579 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
580 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
581 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
582 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
583 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
584 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
585 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
586 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
587 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
588 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
589 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
590 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
591 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
592 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
593 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
594 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
595 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
596 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
597 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
598 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
599 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
600 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
601 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
602 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
603 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
604 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
605 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
606 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
607 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
608 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
609 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
610 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
611 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
612 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
613 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
614 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
615 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
616 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
617 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
618 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
619 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
620 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
621 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
622 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
623 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
626 static const struct reg_value ov5640_setting_low_res[] = {
627 {0x3c07, 0x08, 0, 0},
628 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
629 {0x3814, 0x31, 0, 0},
630 {0x3815, 0x31, 0, 0},
631 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
632 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
633 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
634 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
635 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
636 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
637 {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
640 static const struct reg_value ov5640_setting_720P_1280_720[] = {
641 {0x3c07, 0x07, 0, 0},
642 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
643 {0x3814, 0x31, 0, 0},
644 {0x3815, 0x31, 0, 0},
645 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
646 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
647 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
648 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
649 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
650 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
651 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
652 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
655 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
656 {0x3c07, 0x08, 0, 0},
657 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
658 {0x3814, 0x11, 0, 0},
659 {0x3815, 0x11, 0, 0},
660 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
661 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
662 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
663 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
664 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
665 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
666 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
667 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
668 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
669 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
670 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
671 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
672 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
673 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
674 {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
675 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
676 {0x4005, 0x1a, 0, 0},
679 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
680 {0x3c07, 0x08, 0, 0},
681 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
682 {0x3814, 0x11, 0, 0},
683 {0x3815, 0x11, 0, 0},
684 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
685 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
686 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
687 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
688 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
689 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
690 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
691 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
694 static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
697 .id = OV5640_MODE_QQVGA_160_120,
698 .dn_mode = SUBSAMPLING,
699 .pixel_rate = OV5640_PIXEL_RATE_48M,
719 /* Feed the full valid pixel array to the ISP. */
721 .left = OV5640_PIXEL_ARRAY_LEFT,
722 .top = OV5640_PIXEL_ARRAY_TOP,
723 .width = OV5640_PIXEL_ARRAY_WIDTH,
724 .height = OV5640_PIXEL_ARRAY_HEIGHT,
726 /* Maintain a minimum processing margin. */
736 .reg_data = ov5640_setting_low_res,
737 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
738 .max_fps = OV5640_30_FPS,
739 .def_fps = OV5640_30_FPS
742 .id = OV5640_MODE_QCIF_176_144,
743 .dn_mode = SUBSAMPLING,
744 .pixel_rate = OV5640_PIXEL_RATE_48M,
764 /* Feed the full valid pixel array to the ISP. */
766 .left = OV5640_PIXEL_ARRAY_LEFT,
767 .top = OV5640_PIXEL_ARRAY_TOP,
768 .width = OV5640_PIXEL_ARRAY_WIDTH,
769 .height = OV5640_PIXEL_ARRAY_HEIGHT,
771 /* Maintain a minimum processing margin. */
781 .reg_data = ov5640_setting_low_res,
782 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
783 .max_fps = OV5640_30_FPS,
784 .def_fps = OV5640_30_FPS
787 .id = OV5640_MODE_QVGA_320_240,
788 .dn_mode = SUBSAMPLING,
791 .pixel_rate = OV5640_PIXEL_RATE_48M,
809 /* Feed the full valid pixel array to the ISP. */
811 .left = OV5640_PIXEL_ARRAY_LEFT,
812 .top = OV5640_PIXEL_ARRAY_TOP,
813 .width = OV5640_PIXEL_ARRAY_WIDTH,
814 .height = OV5640_PIXEL_ARRAY_HEIGHT,
816 /* Maintain a minimum processing margin. */
826 .reg_data = ov5640_setting_low_res,
827 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
828 .max_fps = OV5640_30_FPS,
829 .def_fps = OV5640_30_FPS
832 .id = OV5640_MODE_VGA_640_480,
833 .dn_mode = SUBSAMPLING,
834 .pixel_rate = OV5640_PIXEL_RATE_48M,
854 /* Feed the full valid pixel array to the ISP. */
856 .left = OV5640_PIXEL_ARRAY_LEFT,
857 .top = OV5640_PIXEL_ARRAY_TOP,
858 .width = OV5640_PIXEL_ARRAY_WIDTH,
859 .height = OV5640_PIXEL_ARRAY_HEIGHT,
861 /* Maintain a minimum processing margin. */
871 .reg_data = ov5640_setting_low_res,
872 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
873 .max_fps = OV5640_60_FPS,
874 .def_fps = OV5640_30_FPS
877 .id = OV5640_MODE_NTSC_720_480,
878 .dn_mode = SUBSAMPLING,
881 .pixel_rate = OV5640_PIXEL_RATE_96M,
899 /* Feed the full valid pixel array to the ISP. */
901 .left = OV5640_PIXEL_ARRAY_LEFT,
902 .top = OV5640_PIXEL_ARRAY_TOP,
903 .width = OV5640_PIXEL_ARRAY_WIDTH,
904 .height = OV5640_PIXEL_ARRAY_HEIGHT,
915 .reg_data = ov5640_setting_low_res,
916 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
917 .max_fps = OV5640_30_FPS,
918 .def_fps = OV5640_30_FPS
921 .id = OV5640_MODE_PAL_720_576,
922 .dn_mode = SUBSAMPLING,
925 .pixel_rate = OV5640_PIXEL_RATE_96M,
943 /* Feed the full valid pixel array to the ISP. */
945 .left = OV5640_PIXEL_ARRAY_LEFT,
946 .top = OV5640_PIXEL_ARRAY_TOP,
947 .width = OV5640_PIXEL_ARRAY_WIDTH,
948 .height = OV5640_PIXEL_ARRAY_HEIGHT,
959 .reg_data = ov5640_setting_low_res,
960 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
961 .max_fps = OV5640_30_FPS,
962 .def_fps = OV5640_30_FPS
965 .id = OV5640_MODE_XGA_1024_768,
966 .dn_mode = SUBSAMPLING,
967 .pixel_rate = OV5640_PIXEL_RATE_96M,
990 .width = OV5640_NATIVE_WIDTH,
991 .height = OV5640_PIXEL_ARRAY_HEIGHT,
1002 .reg_data = ov5640_setting_low_res,
1003 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
1004 .max_fps = OV5640_30_FPS,
1005 .def_fps = OV5640_30_FPS
1008 .id = OV5640_MODE_720P_1280_720,
1009 .dn_mode = SUBSAMPLING,
1010 .pixel_rate = OV5640_PIXEL_RATE_124M,
1045 .reg_data = ov5640_setting_720P_1280_720,
1046 .reg_data_size = ARRAY_SIZE(ov5640_setting_720P_1280_720),
1047 .max_fps = OV5640_30_FPS,
1048 .def_fps = OV5640_30_FPS
1051 .id = OV5640_MODE_1080P_1920_1080,
1053 .pixel_rate = OV5640_PIXEL_RATE_148M,
1073 /* Crop the full valid pixel array in the center. */
1080 /* Maintain a larger processing margins. */
1090 .reg_data = ov5640_setting_1080P_1920_1080,
1091 .reg_data_size = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
1092 .max_fps = OV5640_30_FPS,
1093 .def_fps = OV5640_30_FPS
1096 .id = OV5640_MODE_QSXGA_2592_1944,
1098 .pixel_rate = OV5640_PIXEL_RATE_168M,
1099 .width = OV5640_PIXEL_ARRAY_WIDTH,
1100 .height = OV5640_PIXEL_ARRAY_HEIGHT,
1118 /* Give more processing margin to full resolution. */
1122 .width = OV5640_NATIVE_WIDTH,
1134 .reg_data = ov5640_setting_QSXGA_2592_1944,
1135 .reg_data_size = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
1136 .max_fps = OV5640_15_FPS,
1137 .def_fps = OV5640_15_FPS
1141 static const struct ov5640_timings *
1142 ov5640_timings(const struct ov5640_dev *sensor,
1143 const struct ov5640_mode_info *mode)
1145 if (ov5640_is_csi2(sensor))
1146 return &mode->csi2_timings;
1148 return &mode->dvp_timings;
1151 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
1153 struct i2c_client *client = sensor->i2c_client;
1158 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
1161 buf[0] = OV5640_REG_SLAVE_ID >> 8;
1162 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
1163 buf[2] = client->addr << 1;
1165 msg.addr = OV5640_DEFAULT_SLAVE_ID;
1168 msg.len = sizeof(buf);
1170 ret = i2c_transfer(client->adapter, &msg, 1);
1172 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
1179 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
1181 struct i2c_client *client = sensor->i2c_client;
1187 buf[1] = reg & 0xff;
1190 msg.addr = client->addr;
1191 msg.flags = client->flags;
1193 msg.len = sizeof(buf);
1195 ret = i2c_transfer(client->adapter, &msg, 1);
1197 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1198 __func__, reg, val);
1205 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
1207 struct i2c_client *client = sensor->i2c_client;
1208 struct i2c_msg msg[2];
1213 buf[1] = reg & 0xff;
1215 msg[0].addr = client->addr;
1216 msg[0].flags = client->flags;
1218 msg[0].len = sizeof(buf);
1220 msg[1].addr = client->addr;
1221 msg[1].flags = client->flags | I2C_M_RD;
1225 ret = i2c_transfer(client->adapter, msg, 2);
1227 dev_err(&client->dev, "%s: error: reg=%x\n",
1236 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
1241 ret = ov5640_read_reg(sensor, reg, &hi);
1244 ret = ov5640_read_reg(sensor, reg + 1, &lo);
1248 *val = ((u16)hi << 8) | (u16)lo;
1252 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
1256 ret = ov5640_write_reg(sensor, reg, val >> 8);
1260 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
1263 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
1269 ret = ov5640_read_reg(sensor, reg, &readval);
1277 return ov5640_write_reg(sensor, reg, val);
1281 * After trying the various combinations, reading various
1282 * documentations spread around the net, and from the various
1283 * feedback, the clock tree is probably as follows:
1289 * +->| PLL1 | - reg 0x3036, for the multiplier
1290 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1291 * | +--------------+
1292 * +->| System Clock | - reg 0x3035, bits 4-7
1294 * | +--------------+
1295 * +->| MIPI Divider | - reg 0x3035, bits 0-3
1296 * | +-+------------+
1297 * | +----------------> MIPI SCLK
1299 * | +->| / 2 |-------> MIPI BIT CLK
1301 * | +--------------+
1302 * +->| PLL Root Div | - reg 0x3037, bit 4
1305 * +->| Bit Div | - reg 0x3034, bits 0-3
1308 * +->| SCLK Div | - reg 0x3108, bits 0-1
1310 * | +---------------> SCLK
1312 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1314 * | +---------------> SCLK 2X
1316 * +->| PCLK Div | - reg 0x3108, bits 4-5
1319 * +->| P_DIV | - reg 0x3035, bits 0-3
1321 * +------------> PCLK
1323 * There seems to be also constraints:
1324 * - the PLL pre-divider output rate should be in the 4-27MHz range
1325 * - the PLL multiplier output rate should be in the 500-1000MHz range
1326 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1330 * This is supposed to be ranging from 1 to 8, but the value is always
1331 * set to 3 in the vendor kernels.
1333 #define OV5640_PLL_PREDIV 3
1335 #define OV5640_PLL_MULT_MIN 4
1336 #define OV5640_PLL_MULT_MAX 252
1339 * This is supposed to be ranging from 1 to 16, but the value is
1340 * always set to either 1 or 2 in the vendor kernels.
1342 #define OV5640_SYSDIV_MIN 1
1343 #define OV5640_SYSDIV_MAX 16
1346 * This is supposed to be ranging from 1 to 2, but the value is always
1347 * set to 2 in the vendor kernels.
1349 #define OV5640_PLL_ROOT_DIV 2
1350 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
1353 * We only supports 8-bit formats at the moment
1355 #define OV5640_BIT_DIV 2
1356 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
1359 * This is supposed to be ranging from 1 to 8, but the value is always
1360 * set to 2 in the vendor kernels.
1362 #define OV5640_SCLK_ROOT_DIV 2
1365 * This is hardcoded so that the consistency is maintained between SCLK and
1368 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1371 * This is supposed to be ranging from 1 to 8, but the value is always
1372 * set to 1 in the vendor kernels.
1374 #define OV5640_PCLK_ROOT_DIV 1
1375 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
1377 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1378 u8 pll_prediv, u8 pll_mult,
1381 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1383 /* PLL1 output cannot exceed 1GHz. */
1384 if (sysclk / 1000000 > 1000)
1387 return sysclk / sysdiv;
1390 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1392 u8 *pll_prediv, u8 *pll_mult,
1395 unsigned long best = ~0;
1396 u8 best_sysdiv = 1, best_mult = 1;
1397 u8 _sysdiv, _pll_mult;
1399 for (_sysdiv = OV5640_SYSDIV_MIN;
1400 _sysdiv <= OV5640_SYSDIV_MAX;
1402 for (_pll_mult = OV5640_PLL_MULT_MIN;
1403 _pll_mult <= OV5640_PLL_MULT_MAX;
1405 unsigned long _rate;
1408 * The PLL multiplier cannot be odd if above
1411 if (_pll_mult > 127 && (_pll_mult % 2))
1414 _rate = ov5640_compute_sys_clk(sensor,
1416 _pll_mult, _sysdiv);
1419 * We have reached the maximum allowed PLL1 output,
1426 * Prefer rates above the expected clock rate than
1427 * below, even if that means being less precise.
1432 if (abs(rate - _rate) < abs(rate - best)) {
1434 best_sysdiv = _sysdiv;
1435 best_mult = _pll_mult;
1444 *sysdiv = best_sysdiv;
1445 *pll_prediv = OV5640_PLL_PREDIV;
1446 *pll_mult = best_mult;
1452 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1453 * for the MIPI CSI-2 output.
1455 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1457 u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1458 u8 prediv, mult, sysdiv;
1459 unsigned long link_freq;
1460 unsigned long sysclk;
1466 /* Use the link freq computed at ov5640_update_pixel_rate() time. */
1467 link_freq = sensor->current_link_freq;
1470 * - mipi_div - Additional divider for the MIPI lane clock.
1472 * Higher link frequencies would make sysclk > 1GHz.
1473 * Keep the sysclk low and do not divide in the MIPI domain.
1475 if (link_freq > OV5640_LINK_RATE_MAX)
1480 sysclk = link_freq * mipi_div;
1481 ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1484 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1486 * - root_div = 2 (fixed)
1487 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1488 * - pclk_div = 1 (fixed)
1489 * - p_div = (2 lanes ? mipi_div : 2 * mipi_div)
1491 * This results in the following MIPI_SCLK depending on the number
1494 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1495 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1497 root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1498 bit_div = OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1499 pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1503 * - YUV: PCLK >= 2 * SCLK
1504 * - RAW or JPEG: PCLK >= SCLK
1505 * - sclk2x_div = sclk_div / 2
1507 sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1508 sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1511 * Set the pixel clock period expressed in ns with 1-bit decimal
1514 * The register is very briefly documented. In the OV5645 datasheet it
1515 * is described as (2 * pclk period), and from testing it seems the
1516 * actual definition is 2 * 8-bit sample period.
1518 * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1520 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1521 sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1522 pclk_period = 2000000000UL / sample_rate;
1524 /* Program the clock tree registers. */
1525 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1529 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1530 (sysdiv << 4) | mipi_div);
1534 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1538 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1543 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1544 (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1548 return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1551 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1553 const struct ov5640_mode_info *mode = sensor->current_mode;
1554 const struct ov5640_timings *timings = &mode->dvp_timings;
1557 rate = timings->htot * (timings->crop.height + timings->vblank_def);
1558 rate *= ov5640_framerates[sensor->current_fr];
1563 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1565 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1566 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1568 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1569 OV5640_PCLK_ROOT_DIV;
1571 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1573 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1574 *bit_div = OV5640_BIT_DIV;
1575 *pclk_div = OV5640_PCLK_ROOT_DIV;
1577 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1580 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1582 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1586 rate = ov5640_calc_pixel_rate(sensor);
1587 rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code);
1588 rate /= sensor->ep.bus.parallel.bus_width;
1590 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1591 &bit_div, &pclk_div);
1596 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1602 * We need to set sysdiv according to the clock, and to clear
1605 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1610 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1615 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1616 0x1f, prediv | ((pll_rdiv - 1) << 4));
1620 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1621 (ilog2(pclk_div) << 4));
1624 /* set JPEG framing sizes */
1625 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1626 const struct ov5640_mode_info *mode)
1631 * compression mode 3 timing
1633 * Data is transmitted with programmable width (VFIFO_HSIZE).
1634 * No padding done. Last line may have less data. Varying
1635 * number of lines per frame, depending on amount of data.
1637 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1641 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
1645 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
1648 /* download ov5640 settings to sensor through i2c */
1649 static int ov5640_set_timings(struct ov5640_dev *sensor,
1650 const struct ov5640_mode_info *mode)
1652 const struct ov5640_timings *timings;
1653 const struct v4l2_rect *analog_crop;
1654 const struct v4l2_rect *crop;
1657 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1658 ret = ov5640_set_jpeg_timings(sensor, mode);
1663 timings = ov5640_timings(sensor, mode);
1664 analog_crop = &timings->analog_crop;
1665 crop = &timings->crop;
1667 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1672 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1677 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1678 analog_crop->left + analog_crop->width - 1);
1682 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1683 analog_crop->top + analog_crop->height - 1);
1687 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1691 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1695 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
1699 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
1703 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
1707 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1708 mode->height + timings->vblank_def);
1715 static void ov5640_load_regs(struct ov5640_dev *sensor,
1716 const struct reg_value *regs, unsigned int regnum)
1724 for (i = 0; i < regnum; ++i, ++regs) {
1725 delay_ms = regs->delay_ms;
1726 reg_addr = regs->reg_addr;
1730 /* remain in power down mode for DVP */
1731 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1732 val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1733 !ov5640_is_csi2(sensor))
1737 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1739 ret = ov5640_write_reg(sensor, reg_addr, val);
1744 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1748 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1750 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1751 BIT(0), on ? 0 : BIT(0));
1754 /* read exposure, in number of line periods */
1755 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1760 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1763 exp = ((int)temp & 0x0f) << 16;
1764 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1767 exp |= ((int)temp << 8);
1768 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1776 /* write exposure, given number of line periods */
1777 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1783 ret = ov5640_write_reg(sensor,
1784 OV5640_REG_AEC_PK_EXPOSURE_LO,
1788 ret = ov5640_write_reg(sensor,
1789 OV5640_REG_AEC_PK_EXPOSURE_MED,
1790 (exposure >> 8) & 0xff);
1793 return ov5640_write_reg(sensor,
1794 OV5640_REG_AEC_PK_EXPOSURE_HI,
1795 (exposure >> 16) & 0x0f);
1798 static int ov5640_get_gain(struct ov5640_dev *sensor)
1803 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1807 return gain & 0x3ff;
1810 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1812 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1816 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1818 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1819 BIT(1), on ? 0 : BIT(1));
1822 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1824 return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1825 OV5640_REG_SYS_CTRL0_SW_PWUP :
1826 OV5640_REG_SYS_CTRL0_SW_PWDN);
1829 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1834 * Enable/disable the MIPI interface
1836 * 0x300e = on ? 0x45 : 0x40
1838 * FIXME: the sensor manual (version 2.03) reports
1839 * [7:5] = 000 : 1 data lane mode
1840 * [7:5] = 001 : 2 data lanes mode
1841 * But this settings do not work, while the following ones
1842 * have been validated for 2 data lanes mode.
1844 * [7:5] = 010 : 2 data lanes mode
1845 * [4] = 0 : Power up MIPI HS Tx
1846 * [3] = 0 : Power up MIPI LS Rx
1847 * [2] = 1/0 : MIPI interface enable/disable
1848 * [1:0] = 01/00: FIXME: 'debug'
1850 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1855 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1859 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1861 /* calculate sysclk */
1862 u32 xvclk = sensor->xclk_freq / 10000;
1863 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1864 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1865 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1869 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1872 temp2 = temp1 & 0x0f;
1873 if (temp2 == 8 || temp2 == 10)
1874 bit_div2x = temp2 / 2;
1876 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1879 sysdiv = temp1 >> 4;
1883 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1888 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1891 prediv = temp1 & 0x0f;
1892 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1894 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1897 temp2 = temp1 & 0x03;
1898 sclk_rdiv = sclk_rdiv_map[temp2];
1900 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1903 VCO = xvclk * multiplier / prediv;
1905 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1910 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1912 /* read HTS from register settings */
1916 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1920 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1923 static int ov5640_get_hts(struct ov5640_dev *sensor)
1925 /* read HTS from register settings */
1929 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1935 static int ov5640_get_vts(struct ov5640_dev *sensor)
1940 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1946 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1948 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1951 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1953 /* get banding filter value */
1954 int ret, light_freq = 0;
1957 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1963 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1976 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1992 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1994 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1997 /* read preview PCLK */
1998 ret = ov5640_get_sysclk(sensor);
2003 sensor->prev_sysclk = ret;
2004 /* read preview HTS */
2005 ret = ov5640_get_hts(sensor);
2010 sensor->prev_hts = ret;
2012 /* read preview VTS */
2013 ret = ov5640_get_vts(sensor);
2018 /* calculate banding filter */
2020 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
2021 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
2026 max_band60 = (int)((prev_vts - 4) / band_step60);
2027 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
2032 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
2033 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
2038 max_band50 = (int)((prev_vts - 4) / band_step50);
2039 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
2042 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
2044 /* stable in high */
2045 u32 fast_high, fast_low;
2048 sensor->ae_low = target * 23 / 25; /* 0.92 */
2049 sensor->ae_high = target * 27 / 25; /* 1.08 */
2051 fast_high = sensor->ae_high << 1;
2052 if (fast_high > 255)
2055 fast_low = sensor->ae_low >> 1;
2057 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
2060 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
2063 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
2066 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
2069 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
2072 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
2075 static int ov5640_get_binning(struct ov5640_dev *sensor)
2080 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
2084 return temp & BIT(0);
2087 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
2093 * - [0]: Horizontal binning enable
2095 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2096 BIT(0), enable ? BIT(0) : 0);
2101 * - [0]: Undocumented, but hardcoded init sequences
2102 * are always setting REG21/REG20 bit 0 to same value...
2104 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2105 BIT(0), enable ? BIT(0) : 0);
2108 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
2110 struct i2c_client *client = sensor->i2c_client;
2111 u8 temp, channel = virtual_channel;
2115 dev_err(&client->dev,
2116 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
2121 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
2125 temp |= (channel << 6);
2126 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
2129 static const struct ov5640_mode_info *
2130 ov5640_find_mode(struct ov5640_dev *sensor, int width, int height, bool nearest)
2132 const struct ov5640_mode_info *mode;
2134 mode = v4l2_find_nearest_size(ov5640_mode_data,
2135 ARRAY_SIZE(ov5640_mode_data),
2136 width, height, width, height);
2140 (mode->width != width || mode->height != height)))
2147 * sensor changes between scaling and subsampling, go through
2148 * exposure calculation
2150 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
2151 const struct ov5640_mode_info *mode)
2153 u32 prev_shutter, prev_gain16;
2154 u32 cap_shutter, cap_gain16;
2155 u32 cap_sysclk, cap_hts, cap_vts;
2156 u32 light_freq, cap_bandfilt, cap_maxband;
2157 u32 cap_gain16_shutter;
2161 if (!mode->reg_data)
2164 /* read preview shutter */
2165 ret = ov5640_get_exposure(sensor);
2169 ret = ov5640_get_binning(sensor);
2172 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
2173 mode->id != OV5640_MODE_1080P_1920_1080)
2176 /* read preview gain */
2177 ret = ov5640_get_gain(sensor);
2183 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
2187 /* turn off night mode for capture */
2188 ret = ov5640_set_night_mode(sensor);
2192 /* Write capture setting */
2193 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2194 ret = ov5640_set_timings(sensor, mode);
2198 /* read capture VTS */
2199 ret = ov5640_get_vts(sensor);
2203 ret = ov5640_get_hts(sensor);
2210 ret = ov5640_get_sysclk(sensor);
2217 /* calculate capture banding filter */
2218 ret = ov5640_get_light_freq(sensor);
2223 if (light_freq == 60) {
2225 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
2228 cap_bandfilt = cap_sysclk * 100 / cap_hts;
2231 if (!sensor->prev_sysclk) {
2232 ret = ov5640_get_sysclk(sensor);
2237 sensor->prev_sysclk = ret;
2243 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
2245 /* calculate capture shutter/gain16 */
2246 if (average > sensor->ae_low && average < sensor->ae_high) {
2247 /* in stable range */
2248 cap_gain16_shutter =
2249 prev_gain16 * prev_shutter *
2250 cap_sysclk / sensor->prev_sysclk *
2251 sensor->prev_hts / cap_hts *
2252 sensor->ae_target / average;
2254 cap_gain16_shutter =
2255 prev_gain16 * prev_shutter *
2256 cap_sysclk / sensor->prev_sysclk *
2257 sensor->prev_hts / cap_hts;
2260 /* gain to shutter */
2261 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
2262 /* shutter < 1/100 */
2263 cap_shutter = cap_gain16_shutter / 16;
2264 if (cap_shutter < 1)
2267 cap_gain16 = cap_gain16_shutter / cap_shutter;
2268 if (cap_gain16 < 16)
2271 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2272 /* exposure reach max */
2273 cap_shutter = cap_bandfilt * cap_maxband;
2277 cap_gain16 = cap_gain16_shutter / cap_shutter;
2279 /* 1/100 < (cap_shutter = n/100) =< max */
2281 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2286 cap_gain16 = cap_gain16_shutter / cap_shutter;
2290 /* set capture gain */
2291 ret = ov5640_set_gain(sensor, cap_gain16);
2295 /* write capture shutter */
2296 if (cap_shutter > (cap_vts - 4)) {
2297 cap_vts = cap_shutter + 4;
2298 ret = ov5640_set_vts(sensor, cap_vts);
2304 return ov5640_set_exposure(sensor, cap_shutter);
2308 * if sensor changes inside scaling or subsampling
2309 * change mode directly
2311 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2312 const struct ov5640_mode_info *mode)
2314 if (!mode->reg_data)
2317 /* Write capture setting */
2318 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2319 return ov5640_set_timings(sensor, mode);
2322 static int ov5640_set_mode(struct ov5640_dev *sensor)
2324 const struct ov5640_mode_info *mode = sensor->current_mode;
2325 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2326 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2327 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2328 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2331 dn_mode = mode->dn_mode;
2332 orig_dn_mode = orig_mode->dn_mode;
2334 /* auto gain and exposure must be turned off when changing modes */
2336 ret = ov5640_set_autogain(sensor, false);
2342 ret = ov5640_set_autoexposure(sensor, false);
2344 goto restore_auto_gain;
2347 if (ov5640_is_csi2(sensor))
2348 ret = ov5640_set_mipi_pclk(sensor);
2350 ret = ov5640_set_dvp_pclk(sensor);
2354 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2355 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2357 * change between subsampling and scaling
2358 * go through exposure calculation
2360 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2363 * change inside subsampling or scaling
2364 * download firmware directly
2366 ret = ov5640_set_mode_direct(sensor, mode);
2369 goto restore_auto_exp_gain;
2371 /* restore auto gain and exposure */
2373 ov5640_set_autogain(sensor, true);
2375 ov5640_set_autoexposure(sensor, true);
2377 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2380 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2383 ret = ov5640_get_light_freq(sensor);
2386 ret = ov5640_set_bandingfilter(sensor);
2389 ret = ov5640_set_virtual_channel(sensor);
2393 sensor->pending_mode_change = false;
2394 sensor->last_mode = mode;
2398 restore_auto_exp_gain:
2400 ov5640_set_autoexposure(sensor, true);
2403 ov5640_set_autogain(sensor, true);
2408 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2409 struct v4l2_mbus_framefmt *format);
2411 /* restore the last set video mode after chip power-on */
2412 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2416 /* first load the initial register values */
2417 ov5640_load_regs(sensor, ov5640_init_setting,
2418 ARRAY_SIZE(ov5640_init_setting));
2420 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2421 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2422 ilog2(OV5640_SCLK_ROOT_DIV));
2426 /* now restore the last capture mode */
2427 ret = ov5640_set_mode(sensor);
2431 return ov5640_set_framefmt(sensor, &sensor->fmt);
2434 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2436 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2440 * From section 2.7 power up sequence:
2441 * t0 + t1 + t2 >= 5ms Delay from DOVDD stable to PWDN pull down
2442 * t3 >= 1ms Delay from PWDN pull down to RESETB pull up
2443 * t4 >= 20ms Delay from RESETB pull up to SCCB (i2c) stable
2445 * Some modules don't expose RESETB/PWDN pins directly, instead providing a
2446 * "PWUP" GPIO which is wired through appropriate delays and inverters to the
2449 * In such cases, this gpio should be mapped to pwdn_gpio in the driver, and we
2450 * should still toggle the pwdn_gpio below with the appropriate delays, while
2451 * the calls to reset_gpio will be ignored.
2453 static void ov5640_powerup_sequence(struct ov5640_dev *sensor)
2455 if (sensor->pwdn_gpio) {
2456 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2458 /* camera power cycle */
2459 ov5640_power(sensor, false);
2460 usleep_range(5000, 10000);
2461 ov5640_power(sensor, true);
2462 usleep_range(5000, 10000);
2464 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2465 usleep_range(1000, 2000);
2467 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2469 /* software reset */
2470 ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
2471 OV5640_REG_SYS_CTRL0_SW_RST);
2473 usleep_range(20000, 25000);
2476 * software standby: allows registers programming;
2477 * exit at restore_mode() for CSI, s_stream(1) for DVP
2479 ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
2480 OV5640_REG_SYS_CTRL0_SW_PWDN);
2483 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2485 struct i2c_client *client = sensor->i2c_client;
2488 ret = clk_prepare_enable(sensor->xclk);
2490 dev_err(&client->dev, "%s: failed to enable clock\n",
2495 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2498 dev_err(&client->dev, "%s: failed to enable regulators\n",
2503 ov5640_powerup_sequence(sensor);
2505 ret = ov5640_init_slave_id(sensor);
2512 ov5640_power(sensor, false);
2513 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2515 clk_disable_unprepare(sensor->xclk);
2519 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2521 ov5640_power(sensor, false);
2522 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2523 clk_disable_unprepare(sensor->xclk);
2526 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2531 /* Reset MIPI bus settings to their default values. */
2532 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2533 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2534 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2539 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2542 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
2543 * "ov5640_set_stream_mipi()")
2544 * [4] = 0 : Power up MIPI HS Tx
2545 * [3] = 0 : Power up MIPI LS Rx
2546 * [2] = 0 : MIPI interface disabled
2548 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2553 * Gate clock and set LP11 in 'no packets mode' (idle)
2556 * [5] = 1 : Gate clock when 'no packets'
2557 * [2] = 1 : MIPI bus in LP11 when 'no packets'
2559 ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2564 * Set data lanes and clock in LP11 when 'sleeping'
2567 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2568 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2569 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2571 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2575 /* Give lanes some time to coax into LP11 state. */
2576 usleep_range(500, 1000);
2581 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2583 unsigned int flags = sensor->ep.bus.parallel.flags;
2584 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2589 /* Reset settings to their default values. */
2590 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2591 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2592 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2593 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2594 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2599 * Note about parallel port configuration.
2601 * When configured in parallel mode, the OV5640 will
2602 * output 10 bits data on DVP data lines [9:0].
2603 * If only 8 bits data are wanted, the 8 bits data lines
2604 * of the camera interface must be physically connected
2605 * on the DVP data lines [9:2].
2607 * Control lines polarity can be configured through
2608 * devicetree endpoint control lines properties.
2609 * If no endpoint control lines properties are set,
2610 * polarity will be as below:
2611 * - VSYNC: active high
2612 * - HREF: active low
2613 * - PCLK: active low
2615 * VSYNC & HREF are not configured if BT656 bus mode is selected
2619 * BT656 embedded synchronization configuration
2622 * - [7]: SYNC code selection (0: auto generate sync code,
2623 * 1: sync code from regs 0x4732-0x4735)
2624 * - [6]: f value in CCIR656 SYNC code when fixed f value
2625 * - [5]: Fixed f value
2626 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200,
2627 * 01: data from regs 0x4736-0x4738, 10: always keep 0)
2628 * - [1]: Clip data disable
2629 * - [0]: CCIR656 mode enable
2631 * Default CCIR656 SAV/EAV mode with default codes
2632 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2633 * - CCIR656 mode enable
2634 * - auto generation of sync codes
2635 * - blank toggle data 1'h040/1'h200
2636 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2638 ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2639 bt656 ? 0x01 : 0x00);
2644 * configure parallel port control lines polarity
2647 * - [5]: PCLK polarity (0: active low, 1: active high)
2648 * - [1]: HREF polarity (0: active low, 1: active high)
2649 * - [0]: VSYNC polarity (mismatch here between
2650 * datasheet and hardware, 0 is active high
2651 * and 1 is active low...)
2654 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2655 polarities |= BIT(1);
2656 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2657 polarities |= BIT(0);
2659 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2660 polarities |= BIT(5);
2662 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2667 * powerdown MIPI TX/RX PHY & enable DVP
2670 * [4] = 1 : Power down MIPI HS Tx
2671 * [3] = 1 : Power down MIPI LS Rx
2672 * [2] = 0 : DVP enable (MIPI disable)
2674 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2679 * enable VSYNC/HREF/PCLK DVP control lines
2680 * & D[9:6] DVP data lines
2682 * PAD OUTPUT ENABLE 01
2683 * - 6: VSYNC output enable
2684 * - 5: HREF output enable
2685 * - 4: PCLK output enable
2686 * - [3:0]: D[9:6] output enable
2688 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2689 bt656 ? 0x1f : 0x7f);
2694 * enable D[5:0] DVP data lines
2696 * PAD OUTPUT ENABLE 02
2697 * - [7:2]: D[5:0] output enable
2699 return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2702 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2707 ret = ov5640_set_power_on(sensor);
2711 ret = ov5640_restore_mode(sensor);
2716 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2717 ret = ov5640_set_power_mipi(sensor, on);
2719 ret = ov5640_set_power_dvp(sensor, on);
2724 ov5640_set_power_off(sensor);
2729 ov5640_set_power_off(sensor);
2733 static int ov5640_sensor_suspend(struct device *dev)
2735 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2736 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2738 return ov5640_set_power(ov5640, false);
2741 static int ov5640_sensor_resume(struct device *dev)
2743 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2744 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2746 return ov5640_set_power(ov5640, true);
2749 /* --------------- Subdev Operations --------------- */
2751 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2752 struct v4l2_fract *fi,
2753 const struct ov5640_mode_info *mode_info)
2755 const struct ov5640_mode_info *mode = mode_info;
2756 enum ov5640_frame_rate rate = OV5640_15_FPS;
2757 int minfps, maxfps, best_fps, fps;
2760 minfps = ov5640_framerates[OV5640_15_FPS];
2761 maxfps = ov5640_framerates[mode->max_fps];
2763 if (fi->numerator == 0) {
2764 fi->denominator = maxfps;
2766 rate = mode->max_fps;
2770 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2774 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2775 int curr_fps = ov5640_framerates[i];
2777 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2778 best_fps = curr_fps;
2784 fi->denominator = best_fps;
2787 mode = ov5640_find_mode(sensor, mode->width, mode->height, false);
2788 return mode ? rate : -EINVAL;
2791 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2792 struct v4l2_subdev_state *sd_state,
2793 struct v4l2_subdev_format *format)
2795 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2796 struct v4l2_mbus_framefmt *fmt;
2798 if (format->pad != 0)
2801 mutex_lock(&sensor->lock);
2803 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2804 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2809 format->format = *fmt;
2811 mutex_unlock(&sensor->lock);
2816 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2817 struct v4l2_mbus_framefmt *fmt,
2818 enum ov5640_frame_rate fr,
2819 const struct ov5640_mode_info **new_mode)
2821 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2822 const struct ov5640_mode_info *mode;
2823 const struct ov5640_pixfmt *pixfmt;
2826 mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true);
2830 pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code);
2834 * Adjust mode according to bpp:
2835 * - 8bpp modes work for resolution >= 1280x720
2836 * - 24bpp modes work resolution < 1280x720
2838 if (bpp == 8 && mode->width < 1280)
2839 mode = &ov5640_mode_data[OV5640_MODE_720P_1280_720];
2840 else if (bpp == 24 && mode->width > 1024)
2841 mode = &ov5640_mode_data[OV5640_MODE_XGA_1024_768];
2843 fmt->width = mode->width;
2844 fmt->height = mode->height;
2849 fmt->code = pixfmt->code;
2850 fmt->colorspace = pixfmt->colorspace;
2851 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2852 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2853 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2858 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2860 const struct ov5640_mode_info *mode = sensor->current_mode;
2861 enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2862 struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2863 const struct ov5640_timings *timings;
2864 s32 exposure_val, exposure_max;
2865 unsigned int hblank;
2874 * Update the pixel rate control value.
2876 * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2878 if (!ov5640_is_csi2(sensor)) {
2879 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2880 ov5640_calc_pixel_rate(sensor));
2886 * The MIPI CSI-2 link frequency should comply with the CSI-2
2887 * specification and be lower than 1GHz.
2889 * Start from the suggested pixel_rate for the current mode and
2890 * progressively slow it down if it exceeds 1GHz.
2892 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2893 bpp = ov5640_code_to_bpp(sensor, fmt->code);
2895 pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2896 link_freq = pixel_rate * bpp / (2 * num_lanes);
2897 } while (link_freq >= 1000000000U &&
2898 ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2900 sensor->current_link_freq = link_freq;
2903 * Higher link rates require the clock tree to be programmed with
2904 * 'mipi_div' = 1; this has the effect of halving the actual output
2905 * pixel rate in the MIPI domain.
2907 * Adjust the pixel rate and link frequency control value to report it
2908 * correctly to userspace.
2910 if (link_freq > OV5640_LINK_RATE_MAX) {
2915 for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2916 if (ov5640_csi2_link_freqs[i] == link_freq)
2919 WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2921 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2922 __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2924 timings = ov5640_timings(sensor, mode);
2925 hblank = timings->htot - mode->width;
2926 __v4l2_ctrl_modify_range(sensor->ctrls.hblank,
2927 hblank, hblank, 1, hblank);
2929 vblank = timings->vblank_def;
2931 if (sensor->current_fr != mode->def_fps) {
2933 * Compute the vertical blanking according to the framerate
2934 * configured with s_frame_interval.
2936 int fie_num = sensor->frame_interval.numerator;
2937 int fie_denom = sensor->frame_interval.denominator;
2939 vblank = ((fie_num * pixel_rate / fie_denom) / timings->htot) -
2943 __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
2944 OV5640_MAX_VTS - mode->height, 1, vblank);
2945 __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank);
2947 exposure_max = timings->crop.height + vblank - 4;
2948 exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
2949 sensor->ctrls.exposure->minimum,
2952 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2953 sensor->ctrls.exposure->minimum,
2954 exposure_max, 1, exposure_val);
2959 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2960 struct v4l2_subdev_state *sd_state,
2961 struct v4l2_subdev_format *format)
2963 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2964 const struct ov5640_mode_info *new_mode;
2965 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2968 if (format->pad != 0)
2971 mutex_lock(&sensor->lock);
2973 if (sensor->streaming) {
2978 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2979 sensor->current_fr, &new_mode);
2983 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2984 *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2988 if (new_mode != sensor->current_mode) {
2989 sensor->current_fr = new_mode->def_fps;
2990 sensor->current_mode = new_mode;
2991 sensor->pending_mode_change = true;
2993 if (mbus_fmt->code != sensor->fmt.code)
2994 sensor->pending_fmt_change = true;
2996 /* update format even if code is unchanged, resolution might change */
2997 sensor->fmt = *mbus_fmt;
2999 ov5640_update_pixel_rate(sensor);
3002 mutex_unlock(&sensor->lock);
3006 static int ov5640_get_selection(struct v4l2_subdev *sd,
3007 struct v4l2_subdev_state *sd_state,
3008 struct v4l2_subdev_selection *sel)
3010 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3011 const struct ov5640_mode_info *mode = sensor->current_mode;
3012 const struct ov5640_timings *timings;
3014 switch (sel->target) {
3015 case V4L2_SEL_TGT_CROP: {
3016 mutex_lock(&sensor->lock);
3017 timings = ov5640_timings(sensor, mode);
3018 sel->r = timings->analog_crop;
3019 mutex_unlock(&sensor->lock);
3024 case V4L2_SEL_TGT_NATIVE_SIZE:
3025 case V4L2_SEL_TGT_CROP_BOUNDS:
3028 sel->r.width = OV5640_NATIVE_WIDTH;
3029 sel->r.height = OV5640_NATIVE_HEIGHT;
3033 case V4L2_SEL_TGT_CROP_DEFAULT:
3034 sel->r.top = OV5640_PIXEL_ARRAY_TOP;
3035 sel->r.left = OV5640_PIXEL_ARRAY_LEFT;
3036 sel->r.width = OV5640_PIXEL_ARRAY_WIDTH;
3037 sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT;
3045 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
3046 struct v4l2_mbus_framefmt *format)
3048 bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8;
3049 const struct ov5640_pixfmt *pixfmt;
3052 pixfmt = ov5640_code_to_pixfmt(sensor, format->code);
3054 /* FORMAT CONTROL00: YUV and RGB formatting */
3055 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00,
3060 /* FORMAT MUX CONTROL: ISP YUV or RGB */
3061 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
3068 * - [5]: JPEG enable
3070 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3071 BIT(5), is_jpeg ? BIT(5) : 0);
3077 * - [4]: Reset JFIFO
3078 * - [3]: Reset SFIFO
3081 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
3082 BIT(4) | BIT(3) | BIT(2),
3083 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
3089 * - [5]: Enable JPEG 2x clock
3090 * - [3]: Enable JPEG clock
3092 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
3094 is_jpeg ? (BIT(5) | BIT(3)) : 0);
3101 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
3106 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3110 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
3112 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
3118 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
3123 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3127 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
3130 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
3136 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
3141 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3145 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
3149 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
3152 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
3158 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
3162 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
3163 BIT(0), awb ? 0 : 1);
3168 u16 red = (u16)sensor->ctrls.red_balance->val;
3169 u16 blue = (u16)sensor->ctrls.blue_balance->val;
3171 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
3174 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
3180 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
3181 enum v4l2_exposure_auto_type auto_exposure)
3183 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3184 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
3187 if (ctrls->auto_exp->is_new) {
3188 ret = ov5640_set_autoexposure(sensor, auto_exp);
3193 if (!auto_exp && ctrls->exposure->is_new) {
3196 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
3200 ret = ov5640_get_vts(sensor);
3206 if (ctrls->exposure->val < max_exp)
3207 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
3213 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
3215 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3218 if (ctrls->auto_gain->is_new) {
3219 ret = ov5640_set_autogain(sensor, auto_gain);
3224 if (!auto_gain && ctrls->gain->is_new)
3225 ret = ov5640_set_gain(sensor, ctrls->gain->val);
3230 static const char * const test_pattern_menu[] = {
3233 "Color bars w/ rolling bar",
3235 "Color squares w/ rolling bar",
3238 #define OV5640_TEST_ENABLE BIT(7)
3239 #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
3240 #define OV5640_TEST_TRANSPARENT BIT(5)
3241 #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
3242 #define OV5640_TEST_BAR_STANDARD (0 << 2)
3243 #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
3244 #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
3245 #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
3246 #define OV5640_TEST_BAR (0 << 0)
3247 #define OV5640_TEST_RANDOM (1 << 0)
3248 #define OV5640_TEST_SQUARE (2 << 0)
3249 #define OV5640_TEST_BLACK (3 << 0)
3251 static const u8 test_pattern_val[] = {
3253 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
3255 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
3256 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
3257 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
3258 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
3261 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
3263 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
3264 test_pattern_val[value]);
3267 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
3271 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
3272 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
3277 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
3278 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
3282 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
3285 * If sensor is mounted upside down, mirror logic is inversed.
3287 * Sensor is a BSI (Back Side Illuminated) one,
3288 * so image captured is physically mirrored.
3289 * This is why mirror logic is inversed in
3290 * order to cancel this mirror effect.
3296 * - [1]: Sensor mirror
3298 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3300 (!(value ^ sensor->upside_down)) ?
3301 (BIT(2) | BIT(1)) : 0);
3304 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
3306 /* If sensor is mounted upside down, flip logic is inversed */
3311 * - [1]: Sensor vflip
3313 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
3315 (value ^ sensor->upside_down) ?
3316 (BIT(2) | BIT(1)) : 0);
3319 static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
3321 const struct ov5640_mode_info *mode = sensor->current_mode;
3323 /* Update the VTOT timing register value. */
3324 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
3325 mode->height + value);
3328 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3330 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3331 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3334 /* v4l2_ctrl_lock() locks our own mutex */
3336 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3340 case V4L2_CID_AUTOGAIN:
3341 val = ov5640_get_gain(sensor);
3344 sensor->ctrls.gain->val = val;
3346 case V4L2_CID_EXPOSURE_AUTO:
3347 val = ov5640_get_exposure(sensor);
3350 sensor->ctrls.exposure->val = val;
3354 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3355 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3360 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3362 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3363 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3364 const struct ov5640_mode_info *mode = sensor->current_mode;
3365 const struct ov5640_timings *timings;
3366 unsigned int exp_max;
3369 /* v4l2_ctrl_lock() locks our own mutex */
3372 case V4L2_CID_VBLANK:
3373 /* Update the exposure range to the newly programmed vblank. */
3374 timings = ov5640_timings(sensor, mode);
3375 exp_max = mode->height + ctrl->val - 4;
3376 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
3377 sensor->ctrls.exposure->minimum,
3378 exp_max, sensor->ctrls.exposure->step,
3379 timings->vblank_def);
3384 * If the device is not powered up by the host driver do
3385 * not apply any controls to H/W at this time. Instead
3386 * the controls will be restored at start streaming time.
3388 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3392 case V4L2_CID_AUTOGAIN:
3393 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3395 case V4L2_CID_EXPOSURE_AUTO:
3396 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3398 case V4L2_CID_AUTO_WHITE_BALANCE:
3399 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3402 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3404 case V4L2_CID_CONTRAST:
3405 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3407 case V4L2_CID_SATURATION:
3408 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3410 case V4L2_CID_TEST_PATTERN:
3411 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3413 case V4L2_CID_POWER_LINE_FREQUENCY:
3414 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3416 case V4L2_CID_HFLIP:
3417 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3419 case V4L2_CID_VFLIP:
3420 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3422 case V4L2_CID_VBLANK:
3423 ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
3430 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3431 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3436 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3437 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
3438 .s_ctrl = ov5640_s_ctrl,
3441 static int ov5640_init_controls(struct ov5640_dev *sensor)
3443 const struct ov5640_mode_info *mode = sensor->current_mode;
3444 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3445 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3446 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3447 struct v4l2_fwnode_device_properties props;
3448 const struct ov5640_timings *timings;
3449 unsigned int max_vblank;
3450 unsigned int hblank;
3453 v4l2_ctrl_handler_init(hdl, 32);
3455 /* we can use our own mutex for the ctrl lock */
3456 hdl->lock = &sensor->lock;
3458 /* Clock related controls */
3459 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3460 ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3461 ov5640_pixel_rates[0], 1,
3462 ov5640_pixel_rates[mode->pixel_rate]);
3464 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3466 ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3467 OV5640_DEFAULT_LINK_FREQ,
3468 ov5640_csi2_link_freqs);
3470 timings = ov5640_timings(sensor, mode);
3471 hblank = timings->htot - mode->width;
3472 ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
3475 max_vblank = OV5640_MAX_VTS - mode->height;
3476 ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
3477 OV5640_MIN_VBLANK, max_vblank,
3478 1, timings->vblank_def);
3480 /* Auto/manual white balance */
3481 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3482 V4L2_CID_AUTO_WHITE_BALANCE,
3484 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3486 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3488 /* Auto/manual exposure */
3489 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3490 V4L2_CID_EXPOSURE_AUTO,
3491 V4L2_EXPOSURE_MANUAL, 0,
3492 V4L2_EXPOSURE_AUTO);
3493 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3495 /* Auto/manual gain */
3496 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3498 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
3501 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3503 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3505 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3507 ctrls->test_pattern =
3508 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3509 ARRAY_SIZE(test_pattern_menu) - 1,
3510 0, 0, test_pattern_menu);
3511 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3513 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3517 v4l2_ctrl_new_std_menu(hdl, ops,
3518 V4L2_CID_POWER_LINE_FREQUENCY,
3519 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3520 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3527 ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props);
3531 if (props.rotation == 180)
3532 sensor->upside_down = true;
3534 ret = v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
3538 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3539 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3540 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3541 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3542 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3544 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3545 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3546 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3548 sensor->sd.ctrl_handler = hdl;
3552 v4l2_ctrl_handler_free(hdl);
3556 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3557 struct v4l2_subdev_state *sd_state,
3558 struct v4l2_subdev_frame_size_enum *fse)
3560 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3561 u32 bpp = ov5640_code_to_bpp(sensor, fse->code);
3562 unsigned int index = fse->index;
3569 /* Only low-resolution modes are supported for 24bpp formats. */
3570 if (bpp == 24 && index >= OV5640_MODE_720P_1280_720)
3573 /* FIXME: Low resolution modes don't work in 8bpp formats. */
3575 index += OV5640_MODE_720P_1280_720;
3577 if (index >= OV5640_NUM_MODES)
3580 fse->min_width = ov5640_mode_data[index].width;
3581 fse->max_width = fse->min_width;
3582 fse->min_height = ov5640_mode_data[index].height;
3583 fse->max_height = fse->min_height;
3588 static int ov5640_enum_frame_interval(
3589 struct v4l2_subdev *sd,
3590 struct v4l2_subdev_state *sd_state,
3591 struct v4l2_subdev_frame_interval_enum *fie)
3593 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3594 const struct ov5640_mode_info *mode;
3595 struct v4l2_fract tpf;
3600 if (fie->index >= OV5640_NUM_FRAMERATES)
3603 mode = ov5640_find_mode(sensor, fie->width, fie->height, false);
3608 tpf.denominator = ov5640_framerates[fie->index];
3610 ret = ov5640_try_frame_interval(sensor, &tpf, mode);
3614 fie->interval = tpf;
3618 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3619 struct v4l2_subdev_frame_interval *fi)
3621 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3623 mutex_lock(&sensor->lock);
3624 fi->interval = sensor->frame_interval;
3625 mutex_unlock(&sensor->lock);
3630 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3631 struct v4l2_subdev_frame_interval *fi)
3633 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3634 const struct ov5640_mode_info *mode;
3635 int frame_rate, ret = 0;
3640 mutex_lock(&sensor->lock);
3642 if (sensor->streaming) {
3647 mode = sensor->current_mode;
3649 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, mode);
3650 if (frame_rate < 0) {
3651 /* Always return a valid frame interval value */
3652 fi->interval = sensor->frame_interval;
3656 mode = ov5640_find_mode(sensor, mode->width, mode->height, true);
3662 if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) {
3667 if (mode != sensor->current_mode ||
3668 frame_rate != sensor->current_fr) {
3669 sensor->current_fr = frame_rate;
3670 sensor->frame_interval = fi->interval;
3671 sensor->current_mode = mode;
3672 sensor->pending_mode_change = true;
3674 ov5640_update_pixel_rate(sensor);
3677 mutex_unlock(&sensor->lock);
3681 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3682 struct v4l2_subdev_state *sd_state,
3683 struct v4l2_subdev_mbus_code_enum *code)
3685 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3686 const struct ov5640_pixfmt *formats;
3687 unsigned int num_formats;
3689 if (ov5640_is_csi2(sensor)) {
3690 formats = ov5640_csi2_formats;
3691 num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1;
3693 formats = ov5640_dvp_formats;
3694 num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1;
3697 if (code->index >= num_formats)
3700 code->code = formats[code->index].code;
3705 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3707 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3711 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
3715 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
3717 pm_runtime_put(&sensor->i2c_client->dev);
3722 mutex_lock(&sensor->lock);
3724 if (sensor->streaming == !enable) {
3725 if (enable && sensor->pending_mode_change) {
3726 ret = ov5640_set_mode(sensor);
3731 if (enable && sensor->pending_fmt_change) {
3732 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3735 sensor->pending_fmt_change = false;
3738 if (ov5640_is_csi2(sensor))
3739 ret = ov5640_set_stream_mipi(sensor, enable);
3741 ret = ov5640_set_stream_dvp(sensor, enable);
3744 sensor->streaming = enable;
3748 mutex_unlock(&sensor->lock);
3750 if (!enable || ret) {
3751 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3752 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3758 static int ov5640_init_cfg(struct v4l2_subdev *sd,
3759 struct v4l2_subdev_state *state)
3761 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3762 struct v4l2_mbus_framefmt *fmt =
3763 v4l2_subdev_get_try_format(sd, state, 0);
3764 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
3766 *fmt = ov5640_is_csi2(sensor) ? ov5640_csi2_default_fmt :
3767 ov5640_dvp_default_fmt;
3769 crop->left = OV5640_PIXEL_ARRAY_LEFT;
3770 crop->top = OV5640_PIXEL_ARRAY_TOP;
3771 crop->width = OV5640_PIXEL_ARRAY_WIDTH;
3772 crop->height = OV5640_PIXEL_ARRAY_HEIGHT;
3777 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3778 .log_status = v4l2_ctrl_subdev_log_status,
3779 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3780 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3783 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3784 .g_frame_interval = ov5640_g_frame_interval,
3785 .s_frame_interval = ov5640_s_frame_interval,
3786 .s_stream = ov5640_s_stream,
3789 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3790 .init_cfg = ov5640_init_cfg,
3791 .enum_mbus_code = ov5640_enum_mbus_code,
3792 .get_fmt = ov5640_get_fmt,
3793 .set_fmt = ov5640_set_fmt,
3794 .get_selection = ov5640_get_selection,
3795 .enum_frame_size = ov5640_enum_frame_size,
3796 .enum_frame_interval = ov5640_enum_frame_interval,
3799 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3800 .core = &ov5640_core_ops,
3801 .video = &ov5640_video_ops,
3802 .pad = &ov5640_pad_ops,
3805 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3809 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3810 sensor->supplies[i].supply = ov5640_supply_name[i];
3812 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3813 OV5640_NUM_SUPPLIES,
3817 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3819 struct i2c_client *client = sensor->i2c_client;
3823 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3825 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3830 if (chip_id != 0x5640) {
3831 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3839 static int ov5640_probe(struct i2c_client *client)
3841 struct device *dev = &client->dev;
3842 struct fwnode_handle *endpoint;
3843 struct ov5640_dev *sensor;
3846 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3850 sensor->i2c_client = client;
3853 * default init sequence initialize sensor to
3854 * YUV422 UYVY VGA@30fps
3856 sensor->frame_interval.numerator = 1;
3857 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3858 sensor->current_fr = OV5640_30_FPS;
3859 sensor->current_mode =
3860 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3861 sensor->last_mode = sensor->current_mode;
3862 sensor->current_link_freq =
3863 ov5640_csi2_link_freqs[OV5640_DEFAULT_LINK_FREQ];
3865 sensor->ae_target = 52;
3867 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3870 dev_err(dev, "endpoint node not found\n");
3874 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3875 fwnode_handle_put(endpoint);
3877 dev_err(dev, "Could not parse endpoint\n");
3881 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3882 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3883 sensor->ep.bus_type != V4L2_MBUS_BT656) {
3884 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3888 sensor->fmt = ov5640_is_csi2(sensor) ? ov5640_csi2_default_fmt :
3889 ov5640_dvp_default_fmt;
3891 /* get system clock (xclk) */
3892 sensor->xclk = devm_clk_get(dev, "xclk");
3893 if (IS_ERR(sensor->xclk)) {
3894 dev_err(dev, "failed to get xclk\n");
3895 return PTR_ERR(sensor->xclk);
3898 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3899 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3900 sensor->xclk_freq > OV5640_XCLK_MAX) {
3901 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3906 /* request optional power down pin */
3907 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3909 if (IS_ERR(sensor->pwdn_gpio))
3910 return PTR_ERR(sensor->pwdn_gpio);
3912 /* request optional reset pin */
3913 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3915 if (IS_ERR(sensor->reset_gpio))
3916 return PTR_ERR(sensor->reset_gpio);
3918 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3920 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3921 V4L2_SUBDEV_FL_HAS_EVENTS;
3922 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3923 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3924 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3928 ret = ov5640_get_regulators(sensor);
3930 goto entity_cleanup;
3932 mutex_init(&sensor->lock);
3934 ret = ov5640_init_controls(sensor);
3936 goto entity_cleanup;
3938 ret = ov5640_sensor_resume(dev);
3940 dev_err(dev, "failed to power on\n");
3941 goto entity_cleanup;
3944 pm_runtime_set_active(dev);
3945 pm_runtime_get_noresume(dev);
3946 pm_runtime_enable(dev);
3948 ret = ov5640_check_chip_id(sensor);
3950 goto err_pm_runtime;
3952 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3954 goto err_pm_runtime;
3956 pm_runtime_set_autosuspend_delay(dev, 1000);
3957 pm_runtime_use_autosuspend(dev);
3958 pm_runtime_mark_last_busy(dev);
3959 pm_runtime_put_autosuspend(dev);
3964 pm_runtime_put_noidle(dev);
3965 pm_runtime_disable(dev);
3966 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3967 ov5640_sensor_suspend(dev);
3969 media_entity_cleanup(&sensor->sd.entity);
3970 mutex_destroy(&sensor->lock);
3974 static void ov5640_remove(struct i2c_client *client)
3976 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3977 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3978 struct device *dev = &client->dev;
3980 pm_runtime_disable(dev);
3981 if (!pm_runtime_status_suspended(dev))
3982 ov5640_sensor_suspend(dev);
3983 pm_runtime_set_suspended(dev);
3985 v4l2_async_unregister_subdev(&sensor->sd);
3986 media_entity_cleanup(&sensor->sd.entity);
3987 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3988 mutex_destroy(&sensor->lock);
3991 static const struct dev_pm_ops ov5640_pm_ops = {
3992 SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
3995 static const struct i2c_device_id ov5640_id[] = {
3999 MODULE_DEVICE_TABLE(i2c, ov5640_id);
4001 static const struct of_device_id ov5640_dt_ids[] = {
4002 { .compatible = "ovti,ov5640" },
4005 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
4007 static struct i2c_driver ov5640_i2c_driver = {
4010 .of_match_table = ov5640_dt_ids,
4011 .pm = &ov5640_pm_ops,
4013 .id_table = ov5640_id,
4014 .probe_new = ov5640_probe,
4015 .remove = ov5640_remove,
4018 module_i2c_driver(ov5640_i2c_driver);
4020 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
4021 MODULE_LICENSE("GPL");