media: ov5640: Rework timings programming
[platform/kernel/linux-rpi.git] / drivers / media / i2c / ov5640.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4  * Copyright (C) 2014-2017 Mentor Graphics Inc.
5  */
6
7 #include <linux/clk.h>
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/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
27
28 /* min/typical/max system clock (xclk) frequencies */
29 #define OV5640_XCLK_MIN  6000000
30 #define OV5640_XCLK_MAX 54000000
31
32 #define OV5640_DEFAULT_SLAVE_ID 0x3c
33
34 #define OV5640_LINK_RATE_MAX            490000000U
35
36 #define OV5640_REG_SYS_RESET02          0x3002
37 #define OV5640_REG_SYS_CLOCK_ENABLE02   0x3006
38 #define OV5640_REG_SYS_CTRL0            0x3008
39 #define OV5640_REG_SYS_CTRL0_SW_PWDN    0x42
40 #define OV5640_REG_SYS_CTRL0_SW_PWUP    0x02
41 #define OV5640_REG_CHIP_ID              0x300a
42 #define OV5640_REG_IO_MIPI_CTRL00       0x300e
43 #define OV5640_REG_PAD_OUTPUT_ENABLE01  0x3017
44 #define OV5640_REG_PAD_OUTPUT_ENABLE02  0x3018
45 #define OV5640_REG_PAD_OUTPUT00         0x3019
46 #define OV5640_REG_SYSTEM_CONTROL1      0x302e
47 #define OV5640_REG_SC_PLL_CTRL0         0x3034
48 #define OV5640_REG_SC_PLL_CTRL1         0x3035
49 #define OV5640_REG_SC_PLL_CTRL2         0x3036
50 #define OV5640_REG_SC_PLL_CTRL3         0x3037
51 #define OV5640_REG_SLAVE_ID             0x3100
52 #define OV5640_REG_SCCB_SYS_CTRL1       0x3103
53 #define OV5640_REG_SYS_ROOT_DIVIDER     0x3108
54 #define OV5640_REG_AWB_R_GAIN           0x3400
55 #define OV5640_REG_AWB_G_GAIN           0x3402
56 #define OV5640_REG_AWB_B_GAIN           0x3404
57 #define OV5640_REG_AWB_MANUAL_CTRL      0x3406
58 #define OV5640_REG_AEC_PK_EXPOSURE_HI   0x3500
59 #define OV5640_REG_AEC_PK_EXPOSURE_MED  0x3501
60 #define OV5640_REG_AEC_PK_EXPOSURE_LO   0x3502
61 #define OV5640_REG_AEC_PK_MANUAL        0x3503
62 #define OV5640_REG_AEC_PK_REAL_GAIN     0x350a
63 #define OV5640_REG_AEC_PK_VTS           0x350c
64 #define OV5640_REG_TIMING_HS            0x3800
65 #define OV5640_REG_TIMING_VS            0x3802
66 #define OV5640_REG_TIMING_HW            0x3804
67 #define OV5640_REG_TIMING_VH            0x3806
68 #define OV5640_REG_TIMING_DVPHO         0x3808
69 #define OV5640_REG_TIMING_DVPVO         0x380a
70 #define OV5640_REG_TIMING_HTS           0x380c
71 #define OV5640_REG_TIMING_VTS           0x380e
72 #define OV5640_REG_TIMING_HOFFS         0x3810
73 #define OV5640_REG_TIMING_VOFFS         0x3812
74 #define OV5640_REG_TIMING_TC_REG20      0x3820
75 #define OV5640_REG_TIMING_TC_REG21      0x3821
76 #define OV5640_REG_AEC_CTRL00           0x3a00
77 #define OV5640_REG_AEC_B50_STEP         0x3a08
78 #define OV5640_REG_AEC_B60_STEP         0x3a0a
79 #define OV5640_REG_AEC_CTRL0D           0x3a0d
80 #define OV5640_REG_AEC_CTRL0E           0x3a0e
81 #define OV5640_REG_AEC_CTRL0F           0x3a0f
82 #define OV5640_REG_AEC_CTRL10           0x3a10
83 #define OV5640_REG_AEC_CTRL11           0x3a11
84 #define OV5640_REG_AEC_CTRL1B           0x3a1b
85 #define OV5640_REG_AEC_CTRL1E           0x3a1e
86 #define OV5640_REG_AEC_CTRL1F           0x3a1f
87 #define OV5640_REG_HZ5060_CTRL00        0x3c00
88 #define OV5640_REG_HZ5060_CTRL01        0x3c01
89 #define OV5640_REG_SIGMADELTA_CTRL0C    0x3c0c
90 #define OV5640_REG_FRAME_CTRL01         0x4202
91 #define OV5640_REG_FORMAT_CONTROL00     0x4300
92 #define OV5640_REG_VFIFO_HSIZE          0x4602
93 #define OV5640_REG_VFIFO_VSIZE          0x4604
94 #define OV5640_REG_JPG_MODE_SELECT      0x4713
95 #define OV5640_REG_CCIR656_CTRL00       0x4730
96 #define OV5640_REG_POLARITY_CTRL00      0x4740
97 #define OV5640_REG_MIPI_CTRL00          0x4800
98 #define OV5640_REG_DEBUG_MODE           0x4814
99 #define OV5640_REG_PCLK_PERIOD          0x4837
100 #define OV5640_REG_ISP_FORMAT_MUX_CTRL  0x501f
101 #define OV5640_REG_PRE_ISP_TEST_SET1    0x503d
102 #define OV5640_REG_SDE_CTRL0            0x5580
103 #define OV5640_REG_SDE_CTRL1            0x5581
104 #define OV5640_REG_SDE_CTRL3            0x5583
105 #define OV5640_REG_SDE_CTRL4            0x5584
106 #define OV5640_REG_SDE_CTRL5            0x5585
107 #define OV5640_REG_AVG_READOUT          0x56a1
108
109 enum ov5640_mode_id {
110         OV5640_MODE_QQVGA_160_120 = 0,
111         OV5640_MODE_QCIF_176_144,
112         OV5640_MODE_QVGA_320_240,
113         OV5640_MODE_VGA_640_480,
114         OV5640_MODE_NTSC_720_480,
115         OV5640_MODE_PAL_720_576,
116         OV5640_MODE_XGA_1024_768,
117         OV5640_MODE_720P_1280_720,
118         OV5640_MODE_1080P_1920_1080,
119         OV5640_MODE_QSXGA_2592_1944,
120         OV5640_NUM_MODES,
121 };
122
123 enum ov5640_frame_rate {
124         OV5640_15_FPS = 0,
125         OV5640_30_FPS,
126         OV5640_60_FPS,
127         OV5640_NUM_FRAMERATES,
128 };
129
130 enum ov5640_pixel_rate_id {
131         OV5640_PIXEL_RATE_168M,
132         OV5640_PIXEL_RATE_148M,
133         OV5640_PIXEL_RATE_124M,
134         OV5640_PIXEL_RATE_96M,
135         OV5640_PIXEL_RATE_48M,
136         OV5640_NUM_PIXEL_RATES,
137 };
138
139 /*
140  * The chip manual suggests 24/48/96/192 MHz pixel clocks.
141  *
142  * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
143  * full resolution mode @15 FPS.
144  */
145 static const u32 ov5640_pixel_rates[] = {
146         [OV5640_PIXEL_RATE_168M] = 168000000,
147         [OV5640_PIXEL_RATE_148M] = 148000000,
148         [OV5640_PIXEL_RATE_124M] = 124000000,
149         [OV5640_PIXEL_RATE_96M] = 96000000,
150         [OV5640_PIXEL_RATE_48M] = 48000000,
151 };
152
153 /*
154  * MIPI CSI-2 link frequencies.
155  *
156  * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
157  * data_lanes = (1, 2)
158  *
159  * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
160  */
161 static const s64 ov5640_csi2_link_freqs[] = {
162         992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
163         592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
164         384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
165         248000000, 192000000, 192000000, 192000000, 96000000,
166 };
167
168 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
169 #define OV5640_DEFAULT_LINK_FREQ        13
170
171 enum ov5640_format_mux {
172         OV5640_FMT_MUX_YUV422 = 0,
173         OV5640_FMT_MUX_RGB,
174         OV5640_FMT_MUX_DITHER,
175         OV5640_FMT_MUX_RAW_DPC,
176         OV5640_FMT_MUX_SNR_RAW,
177         OV5640_FMT_MUX_RAW_CIP,
178 };
179
180 static const struct ov5640_pixfmt {
181         u32 code;
182         u32 colorspace;
183         u8 bpp;
184 } ov5640_formats[] = {
185         {
186                 .code = MEDIA_BUS_FMT_JPEG_1X8,
187                 .colorspace = V4L2_COLORSPACE_JPEG,
188                 .bpp = 16,
189         }, {
190                 .code = MEDIA_BUS_FMT_UYVY8_2X8,
191                 .colorspace = V4L2_COLORSPACE_SRGB,
192                 .bpp = 16,
193         }, {
194                 .code = MEDIA_BUS_FMT_UYVY8_1X16,
195                 .colorspace = V4L2_COLORSPACE_SRGB,
196                 .bpp = 16,
197         }, {
198                 .code = MEDIA_BUS_FMT_YUYV8_2X8,
199                 .colorspace = V4L2_COLORSPACE_SRGB,
200                 .bpp = 16,
201         }, {
202                 .code = MEDIA_BUS_FMT_YUYV8_1X16,
203                 .colorspace = V4L2_COLORSPACE_SRGB,
204                 .bpp = 16,
205         }, {
206                 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
207                 .colorspace = V4L2_COLORSPACE_SRGB,
208                 .bpp = 16,
209         }, {
210                 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
211                 .colorspace = V4L2_COLORSPACE_SRGB,
212                 .bpp = 16,
213         }, {
214                 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
215                 .colorspace = V4L2_COLORSPACE_SRGB,
216                 .bpp = 8,
217         }, {
218                 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
219                 .colorspace = V4L2_COLORSPACE_SRGB,
220                 .bpp = 8
221         }, {
222                 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
223                 .colorspace = V4L2_COLORSPACE_SRGB,
224                 .bpp = 8,
225         }, {
226                 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
227                 .colorspace = V4L2_COLORSPACE_SRGB,
228                 .bpp = 8,
229         },
230 };
231
232 static u32 ov5640_code_to_bpp(u32 code)
233 {
234         unsigned int i;
235
236         for (i = 0; i < ARRAY_SIZE(ov5640_formats); ++i) {
237                 if (ov5640_formats[i].code == code)
238                         return ov5640_formats[i].bpp;
239         }
240
241         return 0;
242 }
243
244 /*
245  * FIXME: remove this when a subdev API becomes available
246  * to set the MIPI CSI-2 virtual channel.
247  */
248 static unsigned int virtual_channel;
249 module_param(virtual_channel, uint, 0444);
250 MODULE_PARM_DESC(virtual_channel,
251                  "MIPI CSI-2 virtual channel (0..3), default 0");
252
253 static const int ov5640_framerates[] = {
254         [OV5640_15_FPS] = 15,
255         [OV5640_30_FPS] = 30,
256         [OV5640_60_FPS] = 60,
257 };
258
259 /* regulator supplies */
260 static const char * const ov5640_supply_name[] = {
261         "DOVDD", /* Digital I/O (1.8V) supply */
262         "AVDD",  /* Analog (2.8V) supply */
263         "DVDD",  /* Digital Core (1.5V) supply */
264 };
265
266 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
267
268 /*
269  * Image size under 1280 * 960 are SUBSAMPLING
270  * Image size upper 1280 * 960 are SCALING
271  */
272 enum ov5640_downsize_mode {
273         SUBSAMPLING,
274         SCALING,
275 };
276
277 struct reg_value {
278         u16 reg_addr;
279         u8 val;
280         u8 mask;
281         u32 delay_ms;
282 };
283
284 struct ov5640_mode_info {
285         enum ov5640_mode_id id;
286         enum ov5640_downsize_mode dn_mode;
287         enum ov5640_pixel_rate_id pixel_rate;
288         /* Analog crop rectangle. */
289         struct v4l2_rect analog_crop;
290         /* Visibile crop: from analog crop top-left corner. */
291         struct v4l2_rect crop;
292         /* Total pixels per line: crop.width + fixed hblank. */
293         u32 htot;
294         /* Default vertical blanking: frame height = crop.height + vblank. */
295         u32 vblank_def;
296         const struct reg_value *reg_data;
297         u32 reg_data_size;
298         /* DVP only; ignored in MIPI mode. */
299         u32 max_fps;
300 };
301
302 struct ov5640_ctrls {
303         struct v4l2_ctrl_handler handler;
304         struct v4l2_ctrl *pixel_rate;
305         struct v4l2_ctrl *link_freq;
306         struct {
307                 struct v4l2_ctrl *auto_exp;
308                 struct v4l2_ctrl *exposure;
309         };
310         struct {
311                 struct v4l2_ctrl *auto_wb;
312                 struct v4l2_ctrl *blue_balance;
313                 struct v4l2_ctrl *red_balance;
314         };
315         struct {
316                 struct v4l2_ctrl *auto_gain;
317                 struct v4l2_ctrl *gain;
318         };
319         struct v4l2_ctrl *brightness;
320         struct v4l2_ctrl *light_freq;
321         struct v4l2_ctrl *saturation;
322         struct v4l2_ctrl *contrast;
323         struct v4l2_ctrl *hue;
324         struct v4l2_ctrl *test_pattern;
325         struct v4l2_ctrl *hflip;
326         struct v4l2_ctrl *vflip;
327 };
328
329 struct ov5640_dev {
330         struct i2c_client *i2c_client;
331         struct v4l2_subdev sd;
332         struct media_pad pad;
333         struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
334         struct clk *xclk; /* system clock to OV5640 */
335         u32 xclk_freq;
336
337         struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
338         struct gpio_desc *reset_gpio;
339         struct gpio_desc *pwdn_gpio;
340         bool   upside_down;
341
342         /* lock to protect all members below */
343         struct mutex lock;
344
345         int power_count;
346
347         struct v4l2_mbus_framefmt fmt;
348         bool pending_fmt_change;
349
350         const struct ov5640_mode_info *current_mode;
351         const struct ov5640_mode_info *last_mode;
352         enum ov5640_frame_rate current_fr;
353         struct v4l2_fract frame_interval;
354         s64 current_link_freq;
355
356         struct ov5640_ctrls ctrls;
357
358         u32 prev_sysclk, prev_hts;
359         u32 ae_low, ae_high, ae_target;
360
361         bool pending_mode_change;
362         bool streaming;
363 };
364
365 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
366 {
367         return container_of(sd, struct ov5640_dev, sd);
368 }
369
370 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
371 {
372         return &container_of(ctrl->handler, struct ov5640_dev,
373                              ctrls.handler)->sd;
374 }
375
376 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
377 {
378         return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
379 }
380
381 /*
382  * FIXME: all of these register tables are likely filled with
383  * entries that set the register to their power-on default values,
384  * and which are otherwise not touched by this driver. Those entries
385  * should be identified and removed to speed register load time
386  * over i2c.
387  */
388 /* YUV422 UYVY VGA@30fps */
389 static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
390         {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
391         {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
392         {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
393         {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
394         {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
395         {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
396         {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
397         {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
398         {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
399         {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
400         {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
401         {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
402         {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
403         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
404         {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
405         {0x3815, 0x31, 0, 0},
406         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
407         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
408         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
409         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
410         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
411         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
412         {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
413         {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
414         {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
415         {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
416         {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
417         {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
418         {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
419         {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
420         {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
421         {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
422         {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
423         {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
424         {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
425         {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
426         {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
427         {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
428         {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
429         {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
430         {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
431         {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
432         {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
433         {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
434         {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
435         {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
436         {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
437         {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
438         {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
439         {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
440         {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
441         {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
442         {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
443         {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
444         {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
445         {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
446         {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
447         {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
448         {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
449         {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
450         {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
451         {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
452         {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
453         {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
454         {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
455         {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
456         {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
457         {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
458         {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
459         {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
460         {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
461         {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
462         {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
463         {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
464         {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
465         {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
466         {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
467 };
468
469 static const struct reg_value ov5640_setting_VGA_640_480[] = {
470         {0x3c07, 0x08, 0, 0},
471         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
472         {0x3814, 0x31, 0, 0},
473         {0x3815, 0x31, 0, 0},
474         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
475         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
476         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
477         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
478         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
479         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
480         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
481         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
482 };
483
484 static const struct reg_value ov5640_setting_XGA_1024_768[] = {
485         {0x3c07, 0x08, 0, 0},
486         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
487         {0x3814, 0x31, 0, 0},
488         {0x3815, 0x31, 0, 0},
489         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
490         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
491         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
492         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
493         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
494         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
495         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
496         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
497 };
498
499 static const struct reg_value ov5640_setting_QVGA_320_240[] = {
500         {0x3c07, 0x08, 0, 0},
501         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
502         {0x3814, 0x31, 0, 0},
503         {0x3815, 0x31, 0, 0},
504         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
505         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
506         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
507         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
508         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
509         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
510         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
511         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
512 };
513
514 static const struct reg_value ov5640_setting_QQVGA_160_120[] = {
515         {0x3c07, 0x08, 0, 0},
516         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
517         {0x3814, 0x31, 0, 0},
518         {0x3815, 0x31, 0, 0},
519         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
520         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
521         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
522         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
523         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
524         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
525         {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
526 };
527
528 static const struct reg_value ov5640_setting_QCIF_176_144[] = {
529         {0x3c07, 0x08, 0, 0},
530         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
531         {0x3814, 0x31, 0, 0},
532         {0x3815, 0x31, 0, 0},
533         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
534         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
535         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
536         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
537         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
538         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
539         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
540         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
541 };
542
543 static const struct reg_value ov5640_setting_NTSC_720_480[] = {
544         {0x3c07, 0x08, 0, 0},
545         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
546         {0x3814, 0x31, 0, 0},
547         {0x3815, 0x31, 0, 0},
548         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
549         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
550         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
551         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
552         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
553         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
554         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
555         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
556 };
557
558 static const struct reg_value ov5640_setting_PAL_720_576[] = {
559         {0x3c07, 0x08, 0, 0},
560         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
561         {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},
569         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
570         {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
571 };
572
573 static const struct reg_value ov5640_setting_720P_1280_720[] = {
574         {0x3c07, 0x07, 0, 0},
575         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
576         {0x3814, 0x31, 0, 0},
577         {0x3815, 0x31, 0, 0},
578         {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
579         {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
580         {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
581         {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
582         {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
583         {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
584         {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
585         {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
586 };
587
588 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
589         {0x3c07, 0x08, 0, 0},
590         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
591         {0x3814, 0x11, 0, 0},
592         {0x3815, 0x11, 0, 0},
593         {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
594         {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
595         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
596         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
597         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
598         {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
599         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
600         {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
601         {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
602         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
603         {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
604         {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
605         {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
606         {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
607         {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
608         {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
609         {0x4005, 0x1a, 0, 0},
610 };
611
612 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
613         {0x3c07, 0x08, 0, 0},
614         {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
615         {0x3814, 0x11, 0, 0},
616         {0x3815, 0x11, 0, 0},
617         {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
618         {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
619         {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
620         {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
621         {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
622         {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
623         {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
624         {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
625 };
626
627 /* power-on sensor init reg table */
628 static const struct ov5640_mode_info ov5640_mode_init_data = {
629                 .id             = 0,
630                 .dn_mode        = SUBSAMPLING,
631                 .pixel_rate     = OV5640_PIXEL_RATE_96M,
632                 .analog_crop = {
633                         .left   = 0,
634                         .top    = 4,
635                         .width  = 2624,
636                         .height = 1944,
637                 },
638                 .crop = {
639                         .left   = 16,
640                         .top    = 6,
641                         .width  = 640,
642                         .height = 480,
643                 },
644                 .htot           = 1896,
645                 .vblank_def     = 504,
646                 .reg_data       = ov5640_init_setting_30fps_VGA,
647                 .reg_data_size  = ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
648                 .max_fps        = OV5640_30_FPS
649 };
650
651 static const struct ov5640_mode_info
652 ov5640_mode_data[OV5640_NUM_MODES] = {
653         {
654                 /* 160x120 */
655                 .id             = OV5640_MODE_QQVGA_160_120,
656                 .dn_mode        = SUBSAMPLING,
657                 .pixel_rate     = OV5640_PIXEL_RATE_48M,
658                 .analog_crop = {
659                         .left   = 0,
660                         .top    = 4,
661                         .width  = 2624,
662                         .height = 1944,
663                 },
664                 .crop = {
665                         .left   = 16,
666                         .top    = 6,
667                         .width  = 160,
668                         .height = 120,
669                 },
670                 .htot           = 1896,
671                 .vblank_def     = 864,
672                 .reg_data       = ov5640_setting_QQVGA_160_120,
673                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_QQVGA_160_120),
674                 .max_fps        = OV5640_30_FPS
675         }, {
676                 /* 176x144 */
677                 .id             = OV5640_MODE_QCIF_176_144,
678                 .dn_mode        = SUBSAMPLING,
679                 .pixel_rate     = OV5640_PIXEL_RATE_48M,
680                 .analog_crop = {
681                         .left   = 0,
682                         .top    = 4,
683                         .width  = 2624,
684                         .height = 1944,
685                 },
686                 .crop = {
687                         .left   = 16,
688                         .top    = 6,
689                         .width  = 176,
690                         .height = 144,
691                 },
692                 .htot           = 1896,
693                 .vblank_def     = 840,
694                 .reg_data       = ov5640_setting_QCIF_176_144,
695                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_QCIF_176_144),
696                 .max_fps        = OV5640_30_FPS
697         }, {
698                 /* 320x240 */
699                 .id             = OV5640_MODE_QVGA_320_240,
700                 .dn_mode        = SUBSAMPLING,
701                 .pixel_rate     = OV5640_PIXEL_RATE_48M,
702                 .analog_crop = {
703                         .left   = 0,
704                         .top    = 4,
705                         .width  = 2624,
706                         .height = 1944,
707                 },
708                 .crop = {
709                         .left   = 16,
710                         .top    = 6,
711                         .width  = 320,
712                         .height = 240,
713                 },
714                 .htot           = 1896,
715                 .vblank_def     = 744,
716                 .reg_data       = ov5640_setting_QVGA_320_240,
717                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_QVGA_320_240),
718                 .max_fps        = OV5640_30_FPS
719         }, {
720                 /* 640x480 */
721                 .id             = OV5640_MODE_VGA_640_480,
722                 .dn_mode        = SUBSAMPLING,
723                 .pixel_rate     = OV5640_PIXEL_RATE_48M,
724                 .analog_crop = {
725                         .left   = 0,
726                         .top    = 4,
727                         .width  = 2624,
728                         .height = 1944,
729                 },
730                 .crop = {
731                         .left   = 16,
732                         .top    = 6,
733                         .width  = 640,
734                         .height = 480,
735                 },
736                 .htot           = 1896,
737                 .vblank_def     = 600,
738                 .reg_data       = ov5640_setting_VGA_640_480,
739                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_VGA_640_480),
740                 .max_fps        = OV5640_60_FPS
741         }, {
742                 /* 720x480 */
743                 .id             = OV5640_MODE_NTSC_720_480,
744                 .dn_mode        = SUBSAMPLING,
745                 .pixel_rate     = OV5640_PIXEL_RATE_96M,
746                 .analog_crop = {
747                         .left   = 0,
748                         .top    = 4,
749                         .width  = 2624,
750                         .height = 1944,
751                 },
752                 .crop = {
753                         .left   = 16,
754                         .top    = 60,
755                         .width  = 720,
756                         .height = 480,
757                 },
758                 .htot           = 1896,
759                 .vblank_def     = 504,
760                 .reg_data       = ov5640_setting_NTSC_720_480,
761                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_NTSC_720_480),
762                 .max_fps        = OV5640_30_FPS
763         }, {
764                 /* 720x576 */
765                 .id             = OV5640_MODE_PAL_720_576,
766                 .dn_mode        = SUBSAMPLING,
767                 .pixel_rate     = OV5640_PIXEL_RATE_96M,
768                 .analog_crop = {
769                         .left   = 0,
770                         .top    = 4,
771                         .width  = 2624,
772                         .height = 1944,
773                 },
774                 .crop = {
775                         .left   = 56,
776                         .top    = 6,
777                         .width  = 720,
778                         .height = 576,
779                 },
780                 .htot           = 1896,
781                 .vblank_def     = 408,
782                 .reg_data       = ov5640_setting_PAL_720_576,
783                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_PAL_720_576),
784                 .max_fps        = OV5640_30_FPS
785         }, {
786                 /* 1024x768 */
787                 .id             = OV5640_MODE_XGA_1024_768,
788                 .dn_mode        = SUBSAMPLING,
789                 .pixel_rate     = OV5640_PIXEL_RATE_96M,
790                 .analog_crop = {
791                         .left   = 0,
792                         .top    = 4,
793                         .width  = 2624,
794                         .height = 1944,
795                 },
796                 .crop = {
797                         .left   = 16,
798                         .top    = 6,
799                         .width  = 1024,
800                         .height = 768,
801                 },
802                 .htot           = 1896,
803                 .vblank_def     = 312,
804                 .reg_data       = ov5640_setting_XGA_1024_768,
805                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_XGA_1024_768),
806                 .max_fps        = OV5640_30_FPS
807         }, {
808                 /* 1280x720 */
809                 .id             = OV5640_MODE_720P_1280_720,
810                 .dn_mode        = SUBSAMPLING,
811                 .pixel_rate     = OV5640_PIXEL_RATE_124M,
812                 .analog_crop = {
813                         .left   = 0,
814                         .top    = 250,
815                         .width  = 2624,
816                         .height = 1456,
817                 },
818                 .crop = {
819                         .left   = 16,
820                         .top    = 4,
821                         .width  = 1280,
822                         .height = 720,
823                 },
824                 .htot           = 1892,
825                 .vblank_def     = 20,
826                 .reg_data       = ov5640_setting_720P_1280_720,
827                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_720P_1280_720),
828                 .max_fps        = OV5640_30_FPS
829         }, {
830                 /* 1920x1080 */
831                 .id             = OV5640_MODE_1080P_1920_1080,
832                 .dn_mode        = SCALING,
833                 .pixel_rate     = OV5640_PIXEL_RATE_148M,
834                 .analog_crop = {
835                         .left   = 336,
836                         .top    = 434,
837                         .width  = 1952,
838                         .height = 1088,
839                 },
840                 .crop = {
841                         .left   = 16,
842                         .top    = 4,
843                         .width  = 1920,
844                         .height = 1080,
845                 },
846                 .htot           = 2500,
847                 .vblank_def     = 40,
848                 .reg_data       = ov5640_setting_1080P_1920_1080,
849                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
850                 .max_fps        = OV5640_30_FPS
851         }, {
852                 /* 2592x1944 */
853                 .id             = OV5640_MODE_QSXGA_2592_1944,
854                 .dn_mode        = SCALING,
855                 .pixel_rate     = OV5640_PIXEL_RATE_168M,
856                 .analog_crop = {
857                         .left   = 0,
858                         .top    = 0,
859                         .width  = 2624,
860                         .height = 1952,
861                 },
862                 .crop = {
863                         .left   = 16,
864                         .top    = 4,
865                         .width  = 2592,
866                         .height = 1944,
867                 },
868                 .htot           = 2844,
869                 .vblank_def     = 24,
870                 .reg_data       = ov5640_setting_QSXGA_2592_1944,
871                 .reg_data_size  = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
872                 .max_fps        = OV5640_15_FPS
873         },
874 };
875
876 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
877 {
878         struct i2c_client *client = sensor->i2c_client;
879         struct i2c_msg msg;
880         u8 buf[3];
881         int ret;
882
883         if (client->addr == OV5640_DEFAULT_SLAVE_ID)
884                 return 0;
885
886         buf[0] = OV5640_REG_SLAVE_ID >> 8;
887         buf[1] = OV5640_REG_SLAVE_ID & 0xff;
888         buf[2] = client->addr << 1;
889
890         msg.addr = OV5640_DEFAULT_SLAVE_ID;
891         msg.flags = 0;
892         msg.buf = buf;
893         msg.len = sizeof(buf);
894
895         ret = i2c_transfer(client->adapter, &msg, 1);
896         if (ret < 0) {
897                 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
898                 return ret;
899         }
900
901         return 0;
902 }
903
904 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
905 {
906         struct i2c_client *client = sensor->i2c_client;
907         struct i2c_msg msg;
908         u8 buf[3];
909         int ret;
910
911         buf[0] = reg >> 8;
912         buf[1] = reg & 0xff;
913         buf[2] = val;
914
915         msg.addr = client->addr;
916         msg.flags = client->flags;
917         msg.buf = buf;
918         msg.len = sizeof(buf);
919
920         ret = i2c_transfer(client->adapter, &msg, 1);
921         if (ret < 0) {
922                 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
923                         __func__, reg, val);
924                 return ret;
925         }
926
927         return 0;
928 }
929
930 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
931 {
932         struct i2c_client *client = sensor->i2c_client;
933         struct i2c_msg msg[2];
934         u8 buf[2];
935         int ret;
936
937         buf[0] = reg >> 8;
938         buf[1] = reg & 0xff;
939
940         msg[0].addr = client->addr;
941         msg[0].flags = client->flags;
942         msg[0].buf = buf;
943         msg[0].len = sizeof(buf);
944
945         msg[1].addr = client->addr;
946         msg[1].flags = client->flags | I2C_M_RD;
947         msg[1].buf = buf;
948         msg[1].len = 1;
949
950         ret = i2c_transfer(client->adapter, msg, 2);
951         if (ret < 0) {
952                 dev_err(&client->dev, "%s: error: reg=%x\n",
953                         __func__, reg);
954                 return ret;
955         }
956
957         *val = buf[0];
958         return 0;
959 }
960
961 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
962 {
963         u8 hi, lo;
964         int ret;
965
966         ret = ov5640_read_reg(sensor, reg, &hi);
967         if (ret)
968                 return ret;
969         ret = ov5640_read_reg(sensor, reg + 1, &lo);
970         if (ret)
971                 return ret;
972
973         *val = ((u16)hi << 8) | (u16)lo;
974         return 0;
975 }
976
977 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
978 {
979         int ret;
980
981         ret = ov5640_write_reg(sensor, reg, val >> 8);
982         if (ret)
983                 return ret;
984
985         return ov5640_write_reg(sensor, reg + 1, val & 0xff);
986 }
987
988 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
989                           u8 mask, u8 val)
990 {
991         u8 readval;
992         int ret;
993
994         ret = ov5640_read_reg(sensor, reg, &readval);
995         if (ret)
996                 return ret;
997
998         readval &= ~mask;
999         val &= mask;
1000         val |= readval;
1001
1002         return ov5640_write_reg(sensor, reg, val);
1003 }
1004
1005 /*
1006  * After trying the various combinations, reading various
1007  * documentations spread around the net, and from the various
1008  * feedback, the clock tree is probably as follows:
1009  *
1010  *   +--------------+
1011  *   |  Ext. Clock  |
1012  *   +-+------------+
1013  *     |  +----------+
1014  *     +->|   PLL1   | - reg 0x3036, for the multiplier
1015  *        +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1016  *          |  +--------------+
1017  *          +->| System Clock |  - reg 0x3035, bits 4-7
1018  *             +-+------------+
1019  *               |  +--------------+
1020  *               +->| MIPI Divider | - reg 0x3035, bits 0-3
1021  *               |  +-+------------+
1022  *               |    +----------------> MIPI SCLK
1023  *               |    +  +-----+
1024  *               |    +->| / 2 |-------> MIPI BIT CLK
1025  *               |       +-----+
1026  *               |  +--------------+
1027  *               +->| PLL Root Div | - reg 0x3037, bit 4
1028  *                  +-+------------+
1029  *                    |  +---------+
1030  *                    +->| Bit Div | - reg 0x3034, bits 0-3
1031  *                       +-+-------+
1032  *                         |  +-------------+
1033  *                         +->| SCLK Div    | - reg 0x3108, bits 0-1
1034  *                         |  +-+-----------+
1035  *                         |    +---------------> SCLK
1036  *                         |  +-------------+
1037  *                         +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1038  *                         |  +-+-----------+
1039  *                         |    +---------------> SCLK 2X
1040  *                         |  +-------------+
1041  *                         +->| PCLK Div    | - reg 0x3108, bits 4-5
1042  *                            ++------------+
1043  *                             +  +-----------+
1044  *                             +->|   P_DIV   | - reg 0x3035, bits 0-3
1045  *                                +-----+-----+
1046  *                                       +------------> PCLK
1047  *
1048  * There seems to be also constraints:
1049  *  - the PLL pre-divider output rate should be in the 4-27MHz range
1050  *  - the PLL multiplier output rate should be in the 500-1000MHz range
1051  *  - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1052  */
1053
1054 /*
1055  * This is supposed to be ranging from 1 to 8, but the value is always
1056  * set to 3 in the vendor kernels.
1057  */
1058 #define OV5640_PLL_PREDIV       3
1059
1060 #define OV5640_PLL_MULT_MIN     4
1061 #define OV5640_PLL_MULT_MAX     252
1062
1063 /*
1064  * This is supposed to be ranging from 1 to 16, but the value is
1065  * always set to either 1 or 2 in the vendor kernels.
1066  */
1067 #define OV5640_SYSDIV_MIN       1
1068 #define OV5640_SYSDIV_MAX       16
1069
1070 /*
1071  * This is supposed to be ranging from 1 to 2, but the value is always
1072  * set to 2 in the vendor kernels.
1073  */
1074 #define OV5640_PLL_ROOT_DIV                     2
1075 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2         BIT(4)
1076
1077 /*
1078  * We only supports 8-bit formats at the moment
1079  */
1080 #define OV5640_BIT_DIV                          2
1081 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT         0x08
1082
1083 /*
1084  * This is supposed to be ranging from 1 to 8, but the value is always
1085  * set to 2 in the vendor kernels.
1086  */
1087 #define OV5640_SCLK_ROOT_DIV    2
1088
1089 /*
1090  * This is hardcoded so that the consistency is maintained between SCLK and
1091  * SCLK 2x.
1092  */
1093 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1094
1095 /*
1096  * This is supposed to be ranging from 1 to 8, but the value is always
1097  * set to 1 in the vendor kernels.
1098  */
1099 #define OV5640_PCLK_ROOT_DIV                    1
1100 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS      0x00
1101
1102 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1103                                             u8 pll_prediv, u8 pll_mult,
1104                                             u8 sysdiv)
1105 {
1106         unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1107
1108         /* PLL1 output cannot exceed 1GHz. */
1109         if (sysclk / 1000000 > 1000)
1110                 return 0;
1111
1112         return sysclk / sysdiv;
1113 }
1114
1115 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1116                                          unsigned long rate,
1117                                          u8 *pll_prediv, u8 *pll_mult,
1118                                          u8 *sysdiv)
1119 {
1120         unsigned long best = ~0;
1121         u8 best_sysdiv = 1, best_mult = 1;
1122         u8 _sysdiv, _pll_mult;
1123
1124         for (_sysdiv = OV5640_SYSDIV_MIN;
1125              _sysdiv <= OV5640_SYSDIV_MAX;
1126              _sysdiv++) {
1127                 for (_pll_mult = OV5640_PLL_MULT_MIN;
1128                      _pll_mult <= OV5640_PLL_MULT_MAX;
1129                      _pll_mult++) {
1130                         unsigned long _rate;
1131
1132                         /*
1133                          * The PLL multiplier cannot be odd if above
1134                          * 127.
1135                          */
1136                         if (_pll_mult > 127 && (_pll_mult % 2))
1137                                 continue;
1138
1139                         _rate = ov5640_compute_sys_clk(sensor,
1140                                                        OV5640_PLL_PREDIV,
1141                                                        _pll_mult, _sysdiv);
1142
1143                         /*
1144                          * We have reached the maximum allowed PLL1 output,
1145                          * increase sysdiv.
1146                          */
1147                         if (!_rate)
1148                                 break;
1149
1150                         /*
1151                          * Prefer rates above the expected clock rate than
1152                          * below, even if that means being less precise.
1153                          */
1154                         if (_rate < rate)
1155                                 continue;
1156
1157                         if (abs(rate - _rate) < abs(rate - best)) {
1158                                 best = _rate;
1159                                 best_sysdiv = _sysdiv;
1160                                 best_mult = _pll_mult;
1161                         }
1162
1163                         if (_rate == rate)
1164                                 goto out;
1165                 }
1166         }
1167
1168 out:
1169         *sysdiv = best_sysdiv;
1170         *pll_prediv = OV5640_PLL_PREDIV;
1171         *pll_mult = best_mult;
1172
1173         return best;
1174 }
1175
1176 /*
1177  * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1178  *                          for the MIPI CSI-2 output.
1179  */
1180 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1181 {
1182         u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1183         u8 prediv, mult, sysdiv;
1184         unsigned long link_freq;
1185         unsigned long sysclk;
1186         u8 pclk_period;
1187         u32 sample_rate;
1188         u32 num_lanes;
1189         int ret;
1190
1191         /* Use the link freq computed at ov5640_update_pixel_rate() time. */
1192         link_freq = sensor->current_link_freq;
1193
1194         /*
1195          * - mipi_div - Additional divider for the MIPI lane clock.
1196          *
1197          * Higher link frequencies would make sysclk > 1GHz.
1198          * Keep the sysclk low and do not divide in the MIPI domain.
1199          */
1200         if (link_freq > OV5640_LINK_RATE_MAX)
1201                 mipi_div = 1;
1202         else
1203                 mipi_div = 2;
1204
1205         sysclk = link_freq * mipi_div;
1206         ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1207
1208         /*
1209          * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1210          *
1211          * - root_div = 2 (fixed)
1212          * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1213          * - pclk_div = 1 (fixed)
1214          * - p_div  = (2 lanes ? mipi_div : 2 * mipi_div)
1215          *
1216          * This results in the following MIPI_SCLK depending on the number
1217          * of lanes:
1218          *
1219          * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1220          * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1221          */
1222         root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1223         bit_div =  OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1224         pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1225
1226         /*
1227          * Scaler clock:
1228          * - YUV: PCLK >= 2 * SCLK
1229          * - RAW or JPEG: PCLK >= SCLK
1230          * - sclk2x_div = sclk_div / 2
1231          */
1232         sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1233         sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1234
1235         /*
1236          * Set the pixel clock period expressed in ns with 1-bit decimal
1237          * (0x01=0.5ns).
1238          *
1239          * The register is very briefly documented. In the OV5645 datasheet it
1240          * is described as (2 * pclk period), and from testing it seems the
1241          * actual definition is 2 * 8-bit sample period.
1242          *
1243          * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1244          */
1245         num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1246         sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1247         pclk_period = 2000000000UL / sample_rate;
1248
1249         /* Program the clock tree registers. */
1250         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1251         if (ret)
1252                 return ret;
1253
1254         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1255                              (sysdiv << 4) | mipi_div);
1256         if (ret)
1257                 return ret;
1258
1259         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1260         if (ret)
1261                 return ret;
1262
1263         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1264                              root_div | prediv);
1265         if (ret)
1266                 return ret;
1267
1268         ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1269                              (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1270         if (ret)
1271                 return ret;
1272
1273         return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1274 }
1275
1276 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1277 {
1278         const struct ov5640_mode_info *mode = sensor->current_mode;
1279         u32 rate;
1280
1281         rate = mode->htot * (mode->crop.height + mode->vblank_def);
1282         rate *= ov5640_framerates[sensor->current_fr];
1283
1284         return rate;
1285 }
1286
1287 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1288                                       unsigned long rate,
1289                                       u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1290                                       u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1291 {
1292         unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1293                                 OV5640_PCLK_ROOT_DIV;
1294
1295         _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1296                                     sysdiv);
1297         *pll_rdiv = OV5640_PLL_ROOT_DIV;
1298         *bit_div = OV5640_BIT_DIV;
1299         *pclk_div = OV5640_PCLK_ROOT_DIV;
1300
1301         return _rate / *pll_rdiv / *bit_div / *pclk_div;
1302 }
1303
1304 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1305 {
1306         u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1307         u32 rate;
1308         int ret;
1309
1310         rate = ov5640_calc_pixel_rate(sensor);
1311         rate *= ov5640_code_to_bpp(sensor->fmt.code);
1312         rate /= sensor->ep.bus.parallel.bus_width;
1313
1314         ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1315                          &bit_div, &pclk_div);
1316
1317         if (bit_div == 2)
1318                 bit_div = 8;
1319
1320         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1321                              0x0f, bit_div);
1322         if (ret)
1323                 return ret;
1324
1325         /*
1326          * We need to set sysdiv according to the clock, and to clear
1327          * the MIPI divider.
1328          */
1329         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1330                              0xff, sysdiv << 4);
1331         if (ret)
1332                 return ret;
1333
1334         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1335                              0xff, mult);
1336         if (ret)
1337                 return ret;
1338
1339         ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1340                              0x1f, prediv | ((pll_rdiv - 1) << 4));
1341         if (ret)
1342                 return ret;
1343
1344         return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1345                               (ilog2(pclk_div) << 4));
1346 }
1347
1348 /* set JPEG framing sizes */
1349 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1350                                    const struct ov5640_mode_info *mode)
1351 {
1352         int ret;
1353
1354         /*
1355          * compression mode 3 timing
1356          *
1357          * Data is transmitted with programmable width (VFIFO_HSIZE).
1358          * No padding done. Last line may have less data. Varying
1359          * number of lines per frame, depending on amount of data.
1360          */
1361         ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1362         if (ret < 0)
1363                 return ret;
1364
1365         ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE,
1366                                  mode->crop.width);
1367         if (ret < 0)
1368                 return ret;
1369
1370         return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE,
1371                                   mode->crop.height);
1372 }
1373
1374 /* download ov5640 settings to sensor through i2c */
1375 static int ov5640_set_timings(struct ov5640_dev *sensor,
1376                               const struct ov5640_mode_info *mode)
1377 {
1378         const struct v4l2_rect *analog_crop = &mode->analog_crop;
1379         const struct v4l2_rect *crop = &mode->crop;
1380         int ret;
1381
1382         if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1383                 ret = ov5640_set_jpeg_timings(sensor, mode);
1384                 if (ret < 0)
1385                         return ret;
1386         }
1387
1388         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1389                                  analog_crop->left);
1390         if (ret < 0)
1391                 return ret;
1392
1393         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1394                                  analog_crop->top);
1395         if (ret < 0)
1396                 return ret;
1397
1398         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1399                                  analog_crop->left + analog_crop->width - 1);
1400         if (ret < 0)
1401                 return ret;
1402
1403         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1404                                  analog_crop->top + analog_crop->height - 1);
1405         if (ret < 0)
1406                 return ret;
1407
1408         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1409         if (ret < 0)
1410                 return ret;
1411
1412         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1413         if (ret < 0)
1414                 return ret;
1415
1416         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, crop->width);
1417         if (ret < 0)
1418                 return ret;
1419
1420         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, crop->height);
1421         if (ret < 0)
1422                 return ret;
1423
1424         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1425         if (ret < 0)
1426                 return ret;
1427
1428         ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1429                                  crop->height + mode->vblank_def);
1430         if (ret < 0)
1431                 return ret;
1432
1433         return 0;
1434 }
1435
1436 static int ov5640_load_regs(struct ov5640_dev *sensor,
1437                             const struct ov5640_mode_info *mode)
1438 {
1439         const struct reg_value *regs = mode->reg_data;
1440         unsigned int i;
1441         u32 delay_ms;
1442         u16 reg_addr;
1443         u8 mask, val;
1444         int ret = 0;
1445
1446         for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1447                 delay_ms = regs->delay_ms;
1448                 reg_addr = regs->reg_addr;
1449                 val = regs->val;
1450                 mask = regs->mask;
1451
1452                 /* remain in power down mode for DVP */
1453                 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1454                     val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1455                     !ov5640_is_csi2(sensor))
1456                         continue;
1457
1458                 if (mask)
1459                         ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1460                 else
1461                         ret = ov5640_write_reg(sensor, reg_addr, val);
1462                 if (ret)
1463                         break;
1464
1465                 if (delay_ms)
1466                         usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1467         }
1468
1469         return ov5640_set_timings(sensor, mode);
1470 }
1471
1472 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1473 {
1474         return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1475                               BIT(0), on ? 0 : BIT(0));
1476 }
1477
1478 /* read exposure, in number of line periods */
1479 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1480 {
1481         int exp, ret;
1482         u8 temp;
1483
1484         ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1485         if (ret)
1486                 return ret;
1487         exp = ((int)temp & 0x0f) << 16;
1488         ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1489         if (ret)
1490                 return ret;
1491         exp |= ((int)temp << 8);
1492         ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1493         if (ret)
1494                 return ret;
1495         exp |= (int)temp;
1496
1497         return exp >> 4;
1498 }
1499
1500 /* write exposure, given number of line periods */
1501 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1502 {
1503         int ret;
1504
1505         exposure <<= 4;
1506
1507         ret = ov5640_write_reg(sensor,
1508                                OV5640_REG_AEC_PK_EXPOSURE_LO,
1509                                exposure & 0xff);
1510         if (ret)
1511                 return ret;
1512         ret = ov5640_write_reg(sensor,
1513                                OV5640_REG_AEC_PK_EXPOSURE_MED,
1514                                (exposure >> 8) & 0xff);
1515         if (ret)
1516                 return ret;
1517         return ov5640_write_reg(sensor,
1518                                 OV5640_REG_AEC_PK_EXPOSURE_HI,
1519                                 (exposure >> 16) & 0x0f);
1520 }
1521
1522 static int ov5640_get_gain(struct ov5640_dev *sensor)
1523 {
1524         u16 gain;
1525         int ret;
1526
1527         ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1528         if (ret)
1529                 return ret;
1530
1531         return gain & 0x3ff;
1532 }
1533
1534 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1535 {
1536         return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1537                                   (u16)gain & 0x3ff);
1538 }
1539
1540 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1541 {
1542         return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1543                               BIT(1), on ? 0 : BIT(1));
1544 }
1545
1546 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1547 {
1548         return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1549                                 OV5640_REG_SYS_CTRL0_SW_PWUP :
1550                                 OV5640_REG_SYS_CTRL0_SW_PWDN);
1551 }
1552
1553 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1554 {
1555         int ret;
1556
1557         /*
1558          * Enable/disable the MIPI interface
1559          *
1560          * 0x300e = on ? 0x45 : 0x40
1561          *
1562          * FIXME: the sensor manual (version 2.03) reports
1563          * [7:5] = 000  : 1 data lane mode
1564          * [7:5] = 001  : 2 data lanes mode
1565          * But this settings do not work, while the following ones
1566          * have been validated for 2 data lanes mode.
1567          *
1568          * [7:5] = 010  : 2 data lanes mode
1569          * [4] = 0      : Power up MIPI HS Tx
1570          * [3] = 0      : Power up MIPI LS Rx
1571          * [2] = 1/0    : MIPI interface enable/disable
1572          * [1:0] = 01/00: FIXME: 'debug'
1573          */
1574         ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1575                                on ? 0x45 : 0x40);
1576         if (ret)
1577                 return ret;
1578
1579         return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1580                                 on ? 0x00 : 0x0f);
1581 }
1582
1583 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1584 {
1585          /* calculate sysclk */
1586         u32 xvclk = sensor->xclk_freq / 10000;
1587         u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1588         u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1589         u32 bit_div2x = 1, sclk_rdiv, sysclk;
1590         u8 temp1, temp2;
1591         int ret;
1592
1593         ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1594         if (ret)
1595                 return ret;
1596         temp2 = temp1 & 0x0f;
1597         if (temp2 == 8 || temp2 == 10)
1598                 bit_div2x = temp2 / 2;
1599
1600         ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1601         if (ret)
1602                 return ret;
1603         sysdiv = temp1 >> 4;
1604         if (sysdiv == 0)
1605                 sysdiv = 16;
1606
1607         ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1608         if (ret)
1609                 return ret;
1610         multiplier = temp1;
1611
1612         ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1613         if (ret)
1614                 return ret;
1615         prediv = temp1 & 0x0f;
1616         pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1617
1618         ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1619         if (ret)
1620                 return ret;
1621         temp2 = temp1 & 0x03;
1622         sclk_rdiv = sclk_rdiv_map[temp2];
1623
1624         if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1625                 return -EINVAL;
1626
1627         VCO = xvclk * multiplier / prediv;
1628
1629         sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1630
1631         return sysclk;
1632 }
1633
1634 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1635 {
1636          /* read HTS from register settings */
1637         u8 mode;
1638         int ret;
1639
1640         ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1641         if (ret)
1642                 return ret;
1643         mode &= 0xfb;
1644         return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1645 }
1646
1647 static int ov5640_get_hts(struct ov5640_dev *sensor)
1648 {
1649         /* read HTS from register settings */
1650         u16 hts;
1651         int ret;
1652
1653         ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1654         if (ret)
1655                 return ret;
1656         return hts;
1657 }
1658
1659 static int ov5640_get_vts(struct ov5640_dev *sensor)
1660 {
1661         u16 vts;
1662         int ret;
1663
1664         ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1665         if (ret)
1666                 return ret;
1667         return vts;
1668 }
1669
1670 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1671 {
1672         return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1673 }
1674
1675 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1676 {
1677         /* get banding filter value */
1678         int ret, light_freq = 0;
1679         u8 temp, temp1;
1680
1681         ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1682         if (ret)
1683                 return ret;
1684
1685         if (temp & 0x80) {
1686                 /* manual */
1687                 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1688                                       &temp1);
1689                 if (ret)
1690                         return ret;
1691                 if (temp1 & 0x04) {
1692                         /* 50Hz */
1693                         light_freq = 50;
1694                 } else {
1695                         /* 60Hz */
1696                         light_freq = 60;
1697                 }
1698         } else {
1699                 /* auto */
1700                 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1701                                       &temp1);
1702                 if (ret)
1703                         return ret;
1704
1705                 if (temp1 & 0x01) {
1706                         /* 50Hz */
1707                         light_freq = 50;
1708                 } else {
1709                         /* 60Hz */
1710                 }
1711         }
1712
1713         return light_freq;
1714 }
1715
1716 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1717 {
1718         u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1719         int ret;
1720
1721         /* read preview PCLK */
1722         ret = ov5640_get_sysclk(sensor);
1723         if (ret < 0)
1724                 return ret;
1725         if (ret == 0)
1726                 return -EINVAL;
1727         sensor->prev_sysclk = ret;
1728         /* read preview HTS */
1729         ret = ov5640_get_hts(sensor);
1730         if (ret < 0)
1731                 return ret;
1732         if (ret == 0)
1733                 return -EINVAL;
1734         sensor->prev_hts = ret;
1735
1736         /* read preview VTS */
1737         ret = ov5640_get_vts(sensor);
1738         if (ret < 0)
1739                 return ret;
1740         prev_vts = ret;
1741
1742         /* calculate banding filter */
1743         /* 60Hz */
1744         band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1745         ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1746         if (ret)
1747                 return ret;
1748         if (!band_step60)
1749                 return -EINVAL;
1750         max_band60 = (int)((prev_vts - 4) / band_step60);
1751         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1752         if (ret)
1753                 return ret;
1754
1755         /* 50Hz */
1756         band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1757         ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1758         if (ret)
1759                 return ret;
1760         if (!band_step50)
1761                 return -EINVAL;
1762         max_band50 = (int)((prev_vts - 4) / band_step50);
1763         return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1764 }
1765
1766 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1767 {
1768         /* stable in high */
1769         u32 fast_high, fast_low;
1770         int ret;
1771
1772         sensor->ae_low = target * 23 / 25;      /* 0.92 */
1773         sensor->ae_high = target * 27 / 25;     /* 1.08 */
1774
1775         fast_high = sensor->ae_high << 1;
1776         if (fast_high > 255)
1777                 fast_high = 255;
1778
1779         fast_low = sensor->ae_low >> 1;
1780
1781         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1782         if (ret)
1783                 return ret;
1784         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1785         if (ret)
1786                 return ret;
1787         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1788         if (ret)
1789                 return ret;
1790         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1791         if (ret)
1792                 return ret;
1793         ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1794         if (ret)
1795                 return ret;
1796         return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1797 }
1798
1799 static int ov5640_get_binning(struct ov5640_dev *sensor)
1800 {
1801         u8 temp;
1802         int ret;
1803
1804         ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1805         if (ret)
1806                 return ret;
1807
1808         return temp & BIT(0);
1809 }
1810
1811 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1812 {
1813         int ret;
1814
1815         /*
1816          * TIMING TC REG21:
1817          * - [0]:       Horizontal binning enable
1818          */
1819         ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1820                              BIT(0), enable ? BIT(0) : 0);
1821         if (ret)
1822                 return ret;
1823         /*
1824          * TIMING TC REG20:
1825          * - [0]:       Undocumented, but hardcoded init sequences
1826          *              are always setting REG21/REG20 bit 0 to same value...
1827          */
1828         return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1829                               BIT(0), enable ? BIT(0) : 0);
1830 }
1831
1832 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1833 {
1834         struct i2c_client *client = sensor->i2c_client;
1835         u8 temp, channel = virtual_channel;
1836         int ret;
1837
1838         if (channel > 3) {
1839                 dev_err(&client->dev,
1840                         "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1841                         __func__, channel);
1842                 return -EINVAL;
1843         }
1844
1845         ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1846         if (ret)
1847                 return ret;
1848         temp &= ~(3 << 6);
1849         temp |= (channel << 6);
1850         return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1851 }
1852
1853 static const struct ov5640_mode_info *
1854 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1855                  int width, int height, bool nearest)
1856 {
1857         const struct ov5640_mode_info *mode;
1858
1859         mode = v4l2_find_nearest_size(ov5640_mode_data,
1860                                       ARRAY_SIZE(ov5640_mode_data),
1861                                       crop.width, crop.height, width, height);
1862
1863         if (!mode ||
1864             (!nearest &&
1865              (mode->crop.width != width || mode->crop.height != height)))
1866                 return NULL;
1867
1868         /* Check to see if the current mode exceeds the max frame rate */
1869         if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps])
1870                 return NULL;
1871
1872         return mode;
1873 }
1874
1875 /*
1876  * sensor changes between scaling and subsampling, go through
1877  * exposure calculation
1878  */
1879 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1880                                          const struct ov5640_mode_info *mode)
1881 {
1882         u32 prev_shutter, prev_gain16;
1883         u32 cap_shutter, cap_gain16;
1884         u32 cap_sysclk, cap_hts, cap_vts;
1885         u32 light_freq, cap_bandfilt, cap_maxband;
1886         u32 cap_gain16_shutter;
1887         u8 average;
1888         int ret;
1889
1890         if (!mode->reg_data)
1891                 return -EINVAL;
1892
1893         /* read preview shutter */
1894         ret = ov5640_get_exposure(sensor);
1895         if (ret < 0)
1896                 return ret;
1897         prev_shutter = ret;
1898         ret = ov5640_get_binning(sensor);
1899         if (ret < 0)
1900                 return ret;
1901         if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1902             mode->id != OV5640_MODE_1080P_1920_1080)
1903                 prev_shutter *= 2;
1904
1905         /* read preview gain */
1906         ret = ov5640_get_gain(sensor);
1907         if (ret < 0)
1908                 return ret;
1909         prev_gain16 = ret;
1910
1911         /* get average */
1912         ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1913         if (ret)
1914                 return ret;
1915
1916         /* turn off night mode for capture */
1917         ret = ov5640_set_night_mode(sensor);
1918         if (ret < 0)
1919                 return ret;
1920
1921         /* Write capture setting */
1922         ret = ov5640_load_regs(sensor, mode);
1923         if (ret < 0)
1924                 return ret;
1925
1926         /* read capture VTS */
1927         ret = ov5640_get_vts(sensor);
1928         if (ret < 0)
1929                 return ret;
1930         cap_vts = ret;
1931         ret = ov5640_get_hts(sensor);
1932         if (ret < 0)
1933                 return ret;
1934         if (ret == 0)
1935                 return -EINVAL;
1936         cap_hts = ret;
1937
1938         ret = ov5640_get_sysclk(sensor);
1939         if (ret < 0)
1940                 return ret;
1941         if (ret == 0)
1942                 return -EINVAL;
1943         cap_sysclk = ret;
1944
1945         /* calculate capture banding filter */
1946         ret = ov5640_get_light_freq(sensor);
1947         if (ret < 0)
1948                 return ret;
1949         light_freq = ret;
1950
1951         if (light_freq == 60) {
1952                 /* 60Hz */
1953                 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1954         } else {
1955                 /* 50Hz */
1956                 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1957         }
1958
1959         if (!sensor->prev_sysclk) {
1960                 ret = ov5640_get_sysclk(sensor);
1961                 if (ret < 0)
1962                         return ret;
1963                 if (ret == 0)
1964                         return -EINVAL;
1965                 sensor->prev_sysclk = ret;
1966         }
1967
1968         if (!cap_bandfilt)
1969                 return -EINVAL;
1970
1971         cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1972
1973         /* calculate capture shutter/gain16 */
1974         if (average > sensor->ae_low && average < sensor->ae_high) {
1975                 /* in stable range */
1976                 cap_gain16_shutter =
1977                         prev_gain16 * prev_shutter *
1978                         cap_sysclk / sensor->prev_sysclk *
1979                         sensor->prev_hts / cap_hts *
1980                         sensor->ae_target / average;
1981         } else {
1982                 cap_gain16_shutter =
1983                         prev_gain16 * prev_shutter *
1984                         cap_sysclk / sensor->prev_sysclk *
1985                         sensor->prev_hts / cap_hts;
1986         }
1987
1988         /* gain to shutter */
1989         if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1990                 /* shutter < 1/100 */
1991                 cap_shutter = cap_gain16_shutter / 16;
1992                 if (cap_shutter < 1)
1993                         cap_shutter = 1;
1994
1995                 cap_gain16 = cap_gain16_shutter / cap_shutter;
1996                 if (cap_gain16 < 16)
1997                         cap_gain16 = 16;
1998         } else {
1999                 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2000                         /* exposure reach max */
2001                         cap_shutter = cap_bandfilt * cap_maxband;
2002                         if (!cap_shutter)
2003                                 return -EINVAL;
2004
2005                         cap_gain16 = cap_gain16_shutter / cap_shutter;
2006                 } else {
2007                         /* 1/100 < (cap_shutter = n/100) =< max */
2008                         cap_shutter =
2009                                 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2010                                 * cap_bandfilt;
2011                         if (!cap_shutter)
2012                                 return -EINVAL;
2013
2014                         cap_gain16 = cap_gain16_shutter / cap_shutter;
2015                 }
2016         }
2017
2018         /* set capture gain */
2019         ret = ov5640_set_gain(sensor, cap_gain16);
2020         if (ret)
2021                 return ret;
2022
2023         /* write capture shutter */
2024         if (cap_shutter > (cap_vts - 4)) {
2025                 cap_vts = cap_shutter + 4;
2026                 ret = ov5640_set_vts(sensor, cap_vts);
2027                 if (ret < 0)
2028                         return ret;
2029         }
2030
2031         /* set exposure */
2032         return ov5640_set_exposure(sensor, cap_shutter);
2033 }
2034
2035 /*
2036  * if sensor changes inside scaling or subsampling
2037  * change mode directly
2038  */
2039 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2040                                   const struct ov5640_mode_info *mode)
2041 {
2042         if (!mode->reg_data)
2043                 return -EINVAL;
2044
2045         /* Write capture setting */
2046         return ov5640_load_regs(sensor, mode);
2047 }
2048
2049 static int ov5640_set_mode(struct ov5640_dev *sensor)
2050 {
2051         const struct ov5640_mode_info *mode = sensor->current_mode;
2052         const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2053         enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2054         bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2055         bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2056         int ret;
2057
2058         dn_mode = mode->dn_mode;
2059         orig_dn_mode = orig_mode->dn_mode;
2060
2061         /* auto gain and exposure must be turned off when changing modes */
2062         if (auto_gain) {
2063                 ret = ov5640_set_autogain(sensor, false);
2064                 if (ret)
2065                         return ret;
2066         }
2067
2068         if (auto_exp) {
2069                 ret = ov5640_set_autoexposure(sensor, false);
2070                 if (ret)
2071                         goto restore_auto_gain;
2072         }
2073
2074         if (ov5640_is_csi2(sensor))
2075                 ret = ov5640_set_mipi_pclk(sensor);
2076         else
2077                 ret = ov5640_set_dvp_pclk(sensor);
2078         if (ret < 0)
2079                 return 0;
2080
2081         if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2082             (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2083                 /*
2084                  * change between subsampling and scaling
2085                  * go through exposure calculation
2086                  */
2087                 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2088         } else {
2089                 /*
2090                  * change inside subsampling or scaling
2091                  * download firmware directly
2092                  */
2093                 ret = ov5640_set_mode_direct(sensor, mode);
2094         }
2095         if (ret < 0)
2096                 goto restore_auto_exp_gain;
2097
2098         /* restore auto gain and exposure */
2099         if (auto_gain)
2100                 ov5640_set_autogain(sensor, true);
2101         if (auto_exp)
2102                 ov5640_set_autoexposure(sensor, true);
2103
2104         ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2105         if (ret < 0)
2106                 return ret;
2107         ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2108         if (ret < 0)
2109                 return ret;
2110         ret = ov5640_get_light_freq(sensor);
2111         if (ret < 0)
2112                 return ret;
2113         ret = ov5640_set_bandingfilter(sensor);
2114         if (ret < 0)
2115                 return ret;
2116         ret = ov5640_set_virtual_channel(sensor);
2117         if (ret < 0)
2118                 return ret;
2119
2120         sensor->pending_mode_change = false;
2121         sensor->last_mode = mode;
2122
2123         return 0;
2124
2125 restore_auto_exp_gain:
2126         if (auto_exp)
2127                 ov5640_set_autoexposure(sensor, true);
2128 restore_auto_gain:
2129         if (auto_gain)
2130                 ov5640_set_autogain(sensor, true);
2131
2132         return ret;
2133 }
2134
2135 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2136                                struct v4l2_mbus_framefmt *format);
2137
2138 /* restore the last set video mode after chip power-on */
2139 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2140 {
2141         int ret;
2142
2143         /* first load the initial register values */
2144         ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
2145         if (ret < 0)
2146                 return ret;
2147         sensor->last_mode = &ov5640_mode_init_data;
2148
2149         ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2150                              (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2151                              ilog2(OV5640_SCLK_ROOT_DIV));
2152         if (ret)
2153                 return ret;
2154
2155         /* now restore the last capture mode */
2156         ret = ov5640_set_mode(sensor);
2157         if (ret < 0)
2158                 return ret;
2159
2160         return ov5640_set_framefmt(sensor, &sensor->fmt);
2161 }
2162
2163 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2164 {
2165         gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2166 }
2167
2168 static void ov5640_reset(struct ov5640_dev *sensor)
2169 {
2170         if (!sensor->reset_gpio)
2171                 return;
2172
2173         gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2174
2175         /* camera power cycle */
2176         ov5640_power(sensor, false);
2177         usleep_range(5000, 10000);
2178         ov5640_power(sensor, true);
2179         usleep_range(5000, 10000);
2180
2181         gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2182         usleep_range(1000, 2000);
2183
2184         gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2185         usleep_range(20000, 25000);
2186 }
2187
2188 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2189 {
2190         struct i2c_client *client = sensor->i2c_client;
2191         int ret;
2192
2193         ret = clk_prepare_enable(sensor->xclk);
2194         if (ret) {
2195                 dev_err(&client->dev, "%s: failed to enable clock\n",
2196                         __func__);
2197                 return ret;
2198         }
2199
2200         ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2201                                     sensor->supplies);
2202         if (ret) {
2203                 dev_err(&client->dev, "%s: failed to enable regulators\n",
2204                         __func__);
2205                 goto xclk_off;
2206         }
2207
2208         ov5640_reset(sensor);
2209         ov5640_power(sensor, true);
2210
2211         ret = ov5640_init_slave_id(sensor);
2212         if (ret)
2213                 goto power_off;
2214
2215         return 0;
2216
2217 power_off:
2218         ov5640_power(sensor, false);
2219         regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2220 xclk_off:
2221         clk_disable_unprepare(sensor->xclk);
2222         return ret;
2223 }
2224
2225 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2226 {
2227         ov5640_power(sensor, false);
2228         regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2229         clk_disable_unprepare(sensor->xclk);
2230 }
2231
2232 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2233 {
2234         int ret;
2235
2236         if (!on) {
2237                 /* Reset MIPI bus settings to their default values. */
2238                 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2239                 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2240                 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2241                 return 0;
2242         }
2243
2244         /*
2245          * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2246          *
2247          * 0x300e = 0x40
2248          * [7:5] = 010  : 2 data lanes mode (see FIXME note in
2249          *                "ov5640_set_stream_mipi()")
2250          * [4] = 0      : Power up MIPI HS Tx
2251          * [3] = 0      : Power up MIPI LS Rx
2252          * [2] = 0      : MIPI interface disabled
2253          */
2254         ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2255         if (ret)
2256                 return ret;
2257
2258         /*
2259          * Gate clock and set LP11 in 'no packets mode' (idle)
2260          *
2261          * 0x4800 = 0x24
2262          * [5] = 1      : Gate clock when 'no packets'
2263          * [2] = 1      : MIPI bus in LP11 when 'no packets'
2264          */
2265         ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2266         if (ret)
2267                 return ret;
2268
2269         /*
2270          * Set data lanes and clock in LP11 when 'sleeping'
2271          *
2272          * 0x3019 = 0x70
2273          * [6] = 1      : MIPI data lane 2 in LP11 when 'sleeping'
2274          * [5] = 1      : MIPI data lane 1 in LP11 when 'sleeping'
2275          * [4] = 1      : MIPI clock lane in LP11 when 'sleeping'
2276          */
2277         ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2278         if (ret)
2279                 return ret;
2280
2281         /* Give lanes some time to coax into LP11 state. */
2282         usleep_range(500, 1000);
2283
2284         return 0;
2285 }
2286
2287 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2288 {
2289         unsigned int flags = sensor->ep.bus.parallel.flags;
2290         bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2291         u8 polarities = 0;
2292         int ret;
2293
2294         if (!on) {
2295                 /* Reset settings to their default values. */
2296                 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2297                 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2298                 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2299                 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2300                 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2301                 return 0;
2302         }
2303
2304         /*
2305          * Note about parallel port configuration.
2306          *
2307          * When configured in parallel mode, the OV5640 will
2308          * output 10 bits data on DVP data lines [9:0].
2309          * If only 8 bits data are wanted, the 8 bits data lines
2310          * of the camera interface must be physically connected
2311          * on the DVP data lines [9:2].
2312          *
2313          * Control lines polarity can be configured through
2314          * devicetree endpoint control lines properties.
2315          * If no endpoint control lines properties are set,
2316          * polarity will be as below:
2317          * - VSYNC:     active high
2318          * - HREF:      active low
2319          * - PCLK:      active low
2320          *
2321          * VSYNC & HREF are not configured if BT656 bus mode is selected
2322          */
2323
2324         /*
2325          * BT656 embedded synchronization configuration
2326          *
2327          * CCIR656 CTRL00
2328          * - [7]:       SYNC code selection (0: auto generate sync code,
2329          *              1: sync code from regs 0x4732-0x4735)
2330          * - [6]:       f value in CCIR656 SYNC code when fixed f value
2331          * - [5]:       Fixed f value
2332          * - [4:3]:     Blank toggle data options (00: data=1'h040/1'h200,
2333          *              01: data from regs 0x4736-0x4738, 10: always keep 0)
2334          * - [1]:       Clip data disable
2335          * - [0]:       CCIR656 mode enable
2336          *
2337          * Default CCIR656 SAV/EAV mode with default codes
2338          * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2339          * - CCIR656 mode enable
2340          * - auto generation of sync codes
2341          * - blank toggle data 1'h040/1'h200
2342          * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2343          */
2344         ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2345                                bt656 ? 0x01 : 0x00);
2346         if (ret)
2347                 return ret;
2348
2349         /*
2350          * configure parallel port control lines polarity
2351          *
2352          * POLARITY CTRL0
2353          * - [5]:       PCLK polarity (0: active low, 1: active high)
2354          * - [1]:       HREF polarity (0: active low, 1: active high)
2355          * - [0]:       VSYNC polarity (mismatch here between
2356          *              datasheet and hardware, 0 is active high
2357          *              and 1 is active low...)
2358          */
2359         if (!bt656) {
2360                 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2361                         polarities |= BIT(1);
2362                 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2363                         polarities |= BIT(0);
2364         }
2365         if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2366                 polarities |= BIT(5);
2367
2368         ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2369         if (ret)
2370                 return ret;
2371
2372         /*
2373          * powerdown MIPI TX/RX PHY & enable DVP
2374          *
2375          * MIPI CONTROL 00
2376          * [4] = 1      : Power down MIPI HS Tx
2377          * [3] = 1      : Power down MIPI LS Rx
2378          * [2] = 0      : DVP enable (MIPI disable)
2379          */
2380         ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2381         if (ret)
2382                 return ret;
2383
2384         /*
2385          * enable VSYNC/HREF/PCLK DVP control lines
2386          * & D[9:6] DVP data lines
2387          *
2388          * PAD OUTPUT ENABLE 01
2389          * - 6:         VSYNC output enable
2390          * - 5:         HREF output enable
2391          * - 4:         PCLK output enable
2392          * - [3:0]:     D[9:6] output enable
2393          */
2394         ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2395                                bt656 ? 0x1f : 0x7f);
2396         if (ret)
2397                 return ret;
2398
2399         /*
2400          * enable D[5:0] DVP data lines
2401          *
2402          * PAD OUTPUT ENABLE 02
2403          * - [7:2]:     D[5:0] output enable
2404          */
2405         return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2406 }
2407
2408 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2409 {
2410         int ret = 0;
2411
2412         if (on) {
2413                 ret = ov5640_set_power_on(sensor);
2414                 if (ret)
2415                         return ret;
2416
2417                 ret = ov5640_restore_mode(sensor);
2418                 if (ret)
2419                         goto power_off;
2420         }
2421
2422         if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2423                 ret = ov5640_set_power_mipi(sensor, on);
2424         else
2425                 ret = ov5640_set_power_dvp(sensor, on);
2426         if (ret)
2427                 goto power_off;
2428
2429         if (!on)
2430                 ov5640_set_power_off(sensor);
2431
2432         return 0;
2433
2434 power_off:
2435         ov5640_set_power_off(sensor);
2436         return ret;
2437 }
2438
2439 /* --------------- Subdev Operations --------------- */
2440
2441 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2442 {
2443         struct ov5640_dev *sensor = to_ov5640_dev(sd);
2444         int ret = 0;
2445
2446         mutex_lock(&sensor->lock);
2447
2448         /*
2449          * If the power count is modified from 0 to != 0 or from != 0 to 0,
2450          * update the power state.
2451          */
2452         if (sensor->power_count == !on) {
2453                 ret = ov5640_set_power(sensor, !!on);
2454                 if (ret)
2455                         goto out;
2456         }
2457
2458         /* Update the power count. */
2459         sensor->power_count += on ? 1 : -1;
2460         WARN_ON(sensor->power_count < 0);
2461 out:
2462         mutex_unlock(&sensor->lock);
2463
2464         if (on && !ret && sensor->power_count == 1) {
2465                 /* restore controls */
2466                 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2467         }
2468
2469         return ret;
2470 }
2471
2472 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2473                                      struct v4l2_fract *fi,
2474                                      u32 width, u32 height)
2475 {
2476         const struct ov5640_mode_info *mode;
2477         enum ov5640_frame_rate rate = OV5640_15_FPS;
2478         int minfps, maxfps, best_fps, fps;
2479         int i;
2480
2481         minfps = ov5640_framerates[OV5640_15_FPS];
2482         maxfps = ov5640_framerates[OV5640_60_FPS];
2483
2484         if (fi->numerator == 0) {
2485                 fi->denominator = maxfps;
2486                 fi->numerator = 1;
2487                 rate = OV5640_60_FPS;
2488                 goto find_mode;
2489         }
2490
2491         fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2492                         minfps, maxfps);
2493
2494         best_fps = minfps;
2495         for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2496                 int curr_fps = ov5640_framerates[i];
2497
2498                 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2499                         best_fps = curr_fps;
2500                         rate = i;
2501                 }
2502         }
2503
2504         fi->numerator = 1;
2505         fi->denominator = best_fps;
2506
2507 find_mode:
2508         mode = ov5640_find_mode(sensor, rate, width, height, false);
2509         return mode ? rate : -EINVAL;
2510 }
2511
2512 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2513                           struct v4l2_subdev_state *sd_state,
2514                           struct v4l2_subdev_format *format)
2515 {
2516         struct ov5640_dev *sensor = to_ov5640_dev(sd);
2517         struct v4l2_mbus_framefmt *fmt;
2518
2519         if (format->pad != 0)
2520                 return -EINVAL;
2521
2522         mutex_lock(&sensor->lock);
2523
2524         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2525                 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2526                                                  format->pad);
2527         else
2528                 fmt = &sensor->fmt;
2529
2530         format->format = *fmt;
2531
2532         mutex_unlock(&sensor->lock);
2533
2534         return 0;
2535 }
2536
2537 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2538                                    struct v4l2_mbus_framefmt *fmt,
2539                                    enum ov5640_frame_rate fr,
2540                                    const struct ov5640_mode_info **new_mode)
2541 {
2542         struct ov5640_dev *sensor = to_ov5640_dev(sd);
2543         const struct ov5640_mode_info *mode;
2544         int i;
2545
2546         mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2547         if (!mode)
2548                 return -EINVAL;
2549         fmt->width = mode->crop.width;
2550         fmt->height = mode->crop.height;
2551
2552         if (new_mode)
2553                 *new_mode = mode;
2554
2555         for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2556                 if (ov5640_formats[i].code == fmt->code)
2557                         break;
2558         if (i >= ARRAY_SIZE(ov5640_formats))
2559                 i = 0;
2560
2561         fmt->code = ov5640_formats[i].code;
2562         fmt->colorspace = ov5640_formats[i].colorspace;
2563         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2564         fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2565         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2566
2567         return 0;
2568 }
2569
2570 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2571 {
2572         const struct ov5640_mode_info *mode = sensor->current_mode;
2573         enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2574         struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2575         unsigned int i = 0;
2576         u32 pixel_rate;
2577         s64 link_freq;
2578         u32 num_lanes;
2579         u32 bpp;
2580
2581         /*
2582          * Update the pixel rate control value.
2583          *
2584          * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2585          */
2586         if (!ov5640_is_csi2(sensor)) {
2587                 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2588                                          ov5640_calc_pixel_rate(sensor));
2589
2590                 return 0;
2591         }
2592
2593         /*
2594          * The MIPI CSI-2 link frequency should comply with the CSI-2
2595          * specification and be lower than 1GHz.
2596          *
2597          * Start from the suggested pixel_rate for the current mode and
2598          * progressively slow it down if it exceeds 1GHz.
2599          */
2600         num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2601         bpp = ov5640_code_to_bpp(fmt->code);
2602         do {
2603                 pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2604                 link_freq = pixel_rate * bpp / (2 * num_lanes);
2605         } while (link_freq >= 1000000000U &&
2606                  ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2607
2608         sensor->current_link_freq = link_freq;
2609
2610         /*
2611          * Higher link rates require the clock tree to be programmed with
2612          * 'mipi_div' = 1; this has the effect of halving the actual output
2613          * pixel rate in the MIPI domain.
2614          *
2615          * Adjust the pixel rate and link frequency control value to report it
2616          * correctly to userspace.
2617          */
2618         if (link_freq > OV5640_LINK_RATE_MAX) {
2619                 pixel_rate /= 2;
2620                 link_freq /= 2;
2621         }
2622
2623         for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2624                 if (ov5640_csi2_link_freqs[i] == link_freq)
2625                         break;
2626         }
2627         WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2628
2629         __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2630         __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2631
2632         return 0;
2633 }
2634
2635 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2636                           struct v4l2_subdev_state *sd_state,
2637                           struct v4l2_subdev_format *format)
2638 {
2639         struct ov5640_dev *sensor = to_ov5640_dev(sd);
2640         const struct ov5640_mode_info *new_mode;
2641         struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2642         int ret;
2643
2644         if (format->pad != 0)
2645                 return -EINVAL;
2646
2647         mutex_lock(&sensor->lock);
2648
2649         if (sensor->streaming) {
2650                 ret = -EBUSY;
2651                 goto out;
2652         }
2653
2654         ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2655                                       sensor->current_fr, &new_mode);
2656         if (ret)
2657                 goto out;
2658
2659         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2660                 *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2661                 goto out;
2662         }
2663
2664         if (new_mode != sensor->current_mode) {
2665                 sensor->current_mode = new_mode;
2666                 sensor->pending_mode_change = true;
2667         }
2668         if (mbus_fmt->code != sensor->fmt.code)
2669                 sensor->pending_fmt_change = true;
2670
2671         /* update format even if code is unchanged, resolution might change */
2672         sensor->fmt = *mbus_fmt;
2673
2674         ov5640_update_pixel_rate(sensor);
2675
2676 out:
2677         mutex_unlock(&sensor->lock);
2678         return ret;
2679 }
2680
2681 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2682                                struct v4l2_mbus_framefmt *format)
2683 {
2684         int ret = 0;
2685         bool is_jpeg = false;
2686         u8 fmt, mux;
2687
2688         switch (format->code) {
2689         case MEDIA_BUS_FMT_UYVY8_1X16:
2690         case MEDIA_BUS_FMT_UYVY8_2X8:
2691                 /* YUV422, UYVY */
2692                 fmt = 0x3f;
2693                 mux = OV5640_FMT_MUX_YUV422;
2694                 break;
2695         case MEDIA_BUS_FMT_YUYV8_1X16:
2696         case MEDIA_BUS_FMT_YUYV8_2X8:
2697                 /* YUV422, YUYV */
2698                 fmt = 0x30;
2699                 mux = OV5640_FMT_MUX_YUV422;
2700                 break;
2701         case MEDIA_BUS_FMT_RGB565_2X8_LE:
2702                 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2703                 fmt = 0x6F;
2704                 mux = OV5640_FMT_MUX_RGB;
2705                 break;
2706         case MEDIA_BUS_FMT_RGB565_2X8_BE:
2707                 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2708                 fmt = 0x61;
2709                 mux = OV5640_FMT_MUX_RGB;
2710                 break;
2711         case MEDIA_BUS_FMT_JPEG_1X8:
2712                 /* YUV422, YUYV */
2713                 fmt = 0x30;
2714                 mux = OV5640_FMT_MUX_YUV422;
2715                 is_jpeg = true;
2716                 break;
2717         case MEDIA_BUS_FMT_SBGGR8_1X8:
2718                 /* Raw, BGBG... / GRGR... */
2719                 fmt = 0x00;
2720                 mux = OV5640_FMT_MUX_RAW_DPC;
2721                 break;
2722         case MEDIA_BUS_FMT_SGBRG8_1X8:
2723                 /* Raw bayer, GBGB... / RGRG... */
2724                 fmt = 0x01;
2725                 mux = OV5640_FMT_MUX_RAW_DPC;
2726                 break;
2727         case MEDIA_BUS_FMT_SGRBG8_1X8:
2728                 /* Raw bayer, GRGR... / BGBG... */
2729                 fmt = 0x02;
2730                 mux = OV5640_FMT_MUX_RAW_DPC;
2731                 break;
2732         case MEDIA_BUS_FMT_SRGGB8_1X8:
2733                 /* Raw bayer, RGRG... / GBGB... */
2734                 fmt = 0x03;
2735                 mux = OV5640_FMT_MUX_RAW_DPC;
2736                 break;
2737         default:
2738                 return -EINVAL;
2739         }
2740
2741         /* FORMAT CONTROL00: YUV and RGB formatting */
2742         ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
2743         if (ret)
2744                 return ret;
2745
2746         /* FORMAT MUX CONTROL: ISP YUV or RGB */
2747         ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
2748         if (ret)
2749                 return ret;
2750
2751         /*
2752          * TIMING TC REG21:
2753          * - [5]:       JPEG enable
2754          */
2755         ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2756                              BIT(5), is_jpeg ? BIT(5) : 0);
2757         if (ret)
2758                 return ret;
2759
2760         /*
2761          * SYSTEM RESET02:
2762          * - [4]:       Reset JFIFO
2763          * - [3]:       Reset SFIFO
2764          * - [2]:       Reset JPEG
2765          */
2766         ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2767                              BIT(4) | BIT(3) | BIT(2),
2768                              is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2769         if (ret)
2770                 return ret;
2771
2772         /*
2773          * CLOCK ENABLE02:
2774          * - [5]:       Enable JPEG 2x clock
2775          * - [3]:       Enable JPEG clock
2776          */
2777         return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2778                               BIT(5) | BIT(3),
2779                               is_jpeg ? (BIT(5) | BIT(3)) : 0);
2780 }
2781
2782 /*
2783  * Sensor Controls.
2784  */
2785
2786 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2787 {
2788         int ret;
2789
2790         if (value) {
2791                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2792                                      BIT(0), BIT(0));
2793                 if (ret)
2794                         return ret;
2795                 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2796         } else {
2797                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2798         }
2799
2800         return ret;
2801 }
2802
2803 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2804 {
2805         int ret;
2806
2807         if (value) {
2808                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2809                                      BIT(2), BIT(2));
2810                 if (ret)
2811                         return ret;
2812                 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2813                                        value & 0xff);
2814         } else {
2815                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2816         }
2817
2818         return ret;
2819 }
2820
2821 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2822 {
2823         int ret;
2824
2825         if (value) {
2826                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2827                                      BIT(1), BIT(1));
2828                 if (ret)
2829                         return ret;
2830                 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2831                                        value & 0xff);
2832                 if (ret)
2833                         return ret;
2834                 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2835                                        value & 0xff);
2836         } else {
2837                 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2838         }
2839
2840         return ret;
2841 }
2842
2843 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2844 {
2845         int ret;
2846
2847         ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2848                              BIT(0), awb ? 0 : 1);
2849         if (ret)
2850                 return ret;
2851
2852         if (!awb) {
2853                 u16 red = (u16)sensor->ctrls.red_balance->val;
2854                 u16 blue = (u16)sensor->ctrls.blue_balance->val;
2855
2856                 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2857                 if (ret)
2858                         return ret;
2859                 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2860         }
2861
2862         return ret;
2863 }
2864
2865 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
2866                                     enum v4l2_exposure_auto_type auto_exposure)
2867 {
2868         struct ov5640_ctrls *ctrls = &sensor->ctrls;
2869         bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
2870         int ret = 0;
2871
2872         if (ctrls->auto_exp->is_new) {
2873                 ret = ov5640_set_autoexposure(sensor, auto_exp);
2874                 if (ret)
2875                         return ret;
2876         }
2877
2878         if (!auto_exp && ctrls->exposure->is_new) {
2879                 u16 max_exp;
2880
2881                 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2882                                         &max_exp);
2883                 if (ret)
2884                         return ret;
2885                 ret = ov5640_get_vts(sensor);
2886                 if (ret < 0)
2887                         return ret;
2888                 max_exp += ret;
2889                 ret = 0;
2890
2891                 if (ctrls->exposure->val < max_exp)
2892                         ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2893         }
2894
2895         return ret;
2896 }
2897
2898 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
2899 {
2900         struct ov5640_ctrls *ctrls = &sensor->ctrls;
2901         int ret = 0;
2902
2903         if (ctrls->auto_gain->is_new) {
2904                 ret = ov5640_set_autogain(sensor, auto_gain);
2905                 if (ret)
2906                         return ret;
2907         }
2908
2909         if (!auto_gain && ctrls->gain->is_new)
2910                 ret = ov5640_set_gain(sensor, ctrls->gain->val);
2911
2912         return ret;
2913 }
2914
2915 static const char * const test_pattern_menu[] = {
2916         "Disabled",
2917         "Color bars",
2918         "Color bars w/ rolling bar",
2919         "Color squares",
2920         "Color squares w/ rolling bar",
2921 };
2922
2923 #define OV5640_TEST_ENABLE              BIT(7)
2924 #define OV5640_TEST_ROLLING             BIT(6)  /* rolling horizontal bar */
2925 #define OV5640_TEST_TRANSPARENT         BIT(5)
2926 #define OV5640_TEST_SQUARE_BW           BIT(4)  /* black & white squares */
2927 #define OV5640_TEST_BAR_STANDARD        (0 << 2)
2928 #define OV5640_TEST_BAR_VERT_CHANGE_1   (1 << 2)
2929 #define OV5640_TEST_BAR_HOR_CHANGE      (2 << 2)
2930 #define OV5640_TEST_BAR_VERT_CHANGE_2   (3 << 2)
2931 #define OV5640_TEST_BAR                 (0 << 0)
2932 #define OV5640_TEST_RANDOM              (1 << 0)
2933 #define OV5640_TEST_SQUARE              (2 << 0)
2934 #define OV5640_TEST_BLACK               (3 << 0)
2935
2936 static const u8 test_pattern_val[] = {
2937         0,
2938         OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
2939                 OV5640_TEST_BAR,
2940         OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
2941                 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
2942         OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
2943         OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
2944 };
2945
2946 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2947 {
2948         return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2949                                 test_pattern_val[value]);
2950 }
2951
2952 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
2953 {
2954         int ret;
2955
2956         ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
2957                              (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
2958                              0 : BIT(7));
2959         if (ret)
2960                 return ret;
2961
2962         return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
2963                               (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
2964                               BIT(2) : 0);
2965 }
2966
2967 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2968 {
2969         /*
2970          * If sensor is mounted upside down, mirror logic is inversed.
2971          *
2972          * Sensor is a BSI (Back Side Illuminated) one,
2973          * so image captured is physically mirrored.
2974          * This is why mirror logic is inversed in
2975          * order to cancel this mirror effect.
2976          */
2977
2978         /*
2979          * TIMING TC REG21:
2980          * - [2]:       ISP mirror
2981          * - [1]:       Sensor mirror
2982          */
2983         return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2984                               BIT(2) | BIT(1),
2985                               (!(value ^ sensor->upside_down)) ?
2986                               (BIT(2) | BIT(1)) : 0);
2987 }
2988
2989 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2990 {
2991         /* If sensor is mounted upside down, flip logic is inversed */
2992
2993         /*
2994          * TIMING TC REG20:
2995          * - [2]:       ISP vflip
2996          * - [1]:       Sensor vflip
2997          */
2998         return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2999                               BIT(2) | BIT(1),
3000                               (value ^ sensor->upside_down) ?
3001                               (BIT(2) | BIT(1)) : 0);
3002 }
3003
3004 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3005 {
3006         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3007         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3008         int val;
3009
3010         /* v4l2_ctrl_lock() locks our own mutex */
3011
3012         switch (ctrl->id) {
3013         case V4L2_CID_AUTOGAIN:
3014                 val = ov5640_get_gain(sensor);
3015                 if (val < 0)
3016                         return val;
3017                 sensor->ctrls.gain->val = val;
3018                 break;
3019         case V4L2_CID_EXPOSURE_AUTO:
3020                 val = ov5640_get_exposure(sensor);
3021                 if (val < 0)
3022                         return val;
3023                 sensor->ctrls.exposure->val = val;
3024                 break;
3025         }
3026
3027         return 0;
3028 }
3029
3030 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3031 {
3032         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3033         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3034         int ret;
3035
3036         /* v4l2_ctrl_lock() locks our own mutex */
3037
3038         /*
3039          * If the device is not powered up by the host driver do
3040          * not apply any controls to H/W at this time. Instead
3041          * the controls will be restored right after power-up.
3042          */
3043         if (sensor->power_count == 0)
3044                 return 0;
3045
3046         switch (ctrl->id) {
3047         case V4L2_CID_AUTOGAIN:
3048                 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3049                 break;
3050         case V4L2_CID_EXPOSURE_AUTO:
3051                 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3052                 break;
3053         case V4L2_CID_AUTO_WHITE_BALANCE:
3054                 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3055                 break;
3056         case V4L2_CID_HUE:
3057                 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3058                 break;
3059         case V4L2_CID_CONTRAST:
3060                 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3061                 break;
3062         case V4L2_CID_SATURATION:
3063                 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3064                 break;
3065         case V4L2_CID_TEST_PATTERN:
3066                 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3067                 break;
3068         case V4L2_CID_POWER_LINE_FREQUENCY:
3069                 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3070                 break;
3071         case V4L2_CID_HFLIP:
3072                 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3073                 break;
3074         case V4L2_CID_VFLIP:
3075                 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3076                 break;
3077         default:
3078                 ret = -EINVAL;
3079                 break;
3080         }
3081
3082         return ret;
3083 }
3084
3085 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3086         .g_volatile_ctrl = ov5640_g_volatile_ctrl,
3087         .s_ctrl = ov5640_s_ctrl,
3088 };
3089
3090 static int ov5640_init_controls(struct ov5640_dev *sensor)
3091 {
3092         const struct ov5640_mode_info *mode = sensor->current_mode;
3093         const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3094         struct ov5640_ctrls *ctrls = &sensor->ctrls;
3095         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3096         int ret;
3097
3098         v4l2_ctrl_handler_init(hdl, 32);
3099
3100         /* we can use our own mutex for the ctrl lock */
3101         hdl->lock = &sensor->lock;
3102
3103         /* Clock related controls */
3104         ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3105                               ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3106                               ov5640_pixel_rates[0], 1,
3107                               ov5640_pixel_rates[mode->pixel_rate]);
3108
3109         ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3110                                         V4L2_CID_LINK_FREQ,
3111                                         ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3112                                         OV5640_DEFAULT_LINK_FREQ,
3113                                         ov5640_csi2_link_freqs);
3114
3115         /* Auto/manual white balance */
3116         ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3117                                            V4L2_CID_AUTO_WHITE_BALANCE,
3118                                            0, 1, 1, 1);
3119         ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3120                                                 0, 4095, 1, 0);
3121         ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3122                                                0, 4095, 1, 0);
3123         /* Auto/manual exposure */
3124         ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3125                                                  V4L2_CID_EXPOSURE_AUTO,
3126                                                  V4L2_EXPOSURE_MANUAL, 0,
3127                                                  V4L2_EXPOSURE_AUTO);
3128         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3129                                             0, 65535, 1, 0);
3130         /* Auto/manual gain */
3131         ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3132                                              0, 1, 1, 1);
3133         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
3134                                         0, 1023, 1, 0);
3135
3136         ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3137                                               0, 255, 1, 64);
3138         ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3139                                        0, 359, 1, 0);
3140         ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3141                                             0, 255, 1, 0);
3142         ctrls->test_pattern =
3143                 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3144                                              ARRAY_SIZE(test_pattern_menu) - 1,
3145                                              0, 0, test_pattern_menu);
3146         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3147                                          0, 1, 1, 0);
3148         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3149                                          0, 1, 1, 0);
3150
3151         ctrls->light_freq =
3152                 v4l2_ctrl_new_std_menu(hdl, ops,
3153                                        V4L2_CID_POWER_LINE_FREQUENCY,
3154                                        V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3155                                        V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3156
3157         if (hdl->error) {
3158                 ret = hdl->error;
3159                 goto free_ctrls;
3160         }
3161
3162         ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3163         ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3164         ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3165         ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3166
3167         v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3168         v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3169         v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3170
3171         sensor->sd.ctrl_handler = hdl;
3172         return 0;
3173
3174 free_ctrls:
3175         v4l2_ctrl_handler_free(hdl);
3176         return ret;
3177 }
3178
3179 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3180                                   struct v4l2_subdev_state *sd_state,
3181                                   struct v4l2_subdev_frame_size_enum *fse)
3182 {
3183         if (fse->pad != 0)
3184                 return -EINVAL;
3185         if (fse->index >= OV5640_NUM_MODES)
3186                 return -EINVAL;
3187
3188         fse->min_width = ov5640_mode_data[fse->index].crop.width;
3189         fse->max_width = fse->min_width;
3190         fse->min_height = ov5640_mode_data[fse->index].crop.height;
3191         fse->max_height = fse->min_height;
3192
3193         return 0;
3194 }
3195
3196 static int ov5640_enum_frame_interval(
3197         struct v4l2_subdev *sd,
3198         struct v4l2_subdev_state *sd_state,
3199         struct v4l2_subdev_frame_interval_enum *fie)
3200 {
3201         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3202         struct v4l2_fract tpf;
3203         int ret;
3204
3205         if (fie->pad != 0)
3206                 return -EINVAL;
3207         if (fie->index >= OV5640_NUM_FRAMERATES)
3208                 return -EINVAL;
3209
3210         tpf.numerator = 1;
3211         tpf.denominator = ov5640_framerates[fie->index];
3212
3213         ret = ov5640_try_frame_interval(sensor, &tpf,
3214                                         fie->width, fie->height);
3215         if (ret < 0)
3216                 return -EINVAL;
3217
3218         fie->interval = tpf;
3219         return 0;
3220 }
3221
3222 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3223                                    struct v4l2_subdev_frame_interval *fi)
3224 {
3225         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3226
3227         mutex_lock(&sensor->lock);
3228         fi->interval = sensor->frame_interval;
3229         mutex_unlock(&sensor->lock);
3230
3231         return 0;
3232 }
3233
3234 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3235                                    struct v4l2_subdev_frame_interval *fi)
3236 {
3237         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3238         const struct ov5640_mode_info *mode;
3239         int frame_rate, ret = 0;
3240
3241         if (fi->pad != 0)
3242                 return -EINVAL;
3243
3244         mutex_lock(&sensor->lock);
3245
3246         if (sensor->streaming) {
3247                 ret = -EBUSY;
3248                 goto out;
3249         }
3250
3251         mode = sensor->current_mode;
3252
3253         frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
3254                                                mode->crop.width,
3255                                                mode->crop.height);
3256         if (frame_rate < 0) {
3257                 /* Always return a valid frame interval value */
3258                 fi->interval = sensor->frame_interval;
3259                 goto out;
3260         }
3261
3262         mode = ov5640_find_mode(sensor, frame_rate, mode->crop.width,
3263                                 mode->crop.height, true);
3264         if (!mode) {
3265                 ret = -EINVAL;
3266                 goto out;
3267         }
3268
3269         if (mode != sensor->current_mode ||
3270             frame_rate != sensor->current_fr) {
3271                 sensor->current_fr = frame_rate;
3272                 sensor->frame_interval = fi->interval;
3273                 sensor->current_mode = mode;
3274                 sensor->pending_mode_change = true;
3275
3276                 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
3277                                          ov5640_calc_pixel_rate(sensor));
3278         }
3279 out:
3280         mutex_unlock(&sensor->lock);
3281         return ret;
3282 }
3283
3284 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3285                                  struct v4l2_subdev_state *sd_state,
3286                                  struct v4l2_subdev_mbus_code_enum *code)
3287 {
3288         if (code->pad != 0)
3289                 return -EINVAL;
3290         if (code->index >= ARRAY_SIZE(ov5640_formats))
3291                 return -EINVAL;
3292
3293         code->code = ov5640_formats[code->index].code;
3294         return 0;
3295 }
3296
3297 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3298 {
3299         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3300         int ret = 0;
3301
3302         mutex_lock(&sensor->lock);
3303
3304         if (sensor->streaming == !enable) {
3305                 if (enable && sensor->pending_mode_change) {
3306                         ret = ov5640_set_mode(sensor);
3307                         if (ret)
3308                                 goto out;
3309                 }
3310
3311                 if (enable && sensor->pending_fmt_change) {
3312                         ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3313                         if (ret)
3314                                 goto out;
3315                         sensor->pending_fmt_change = false;
3316                 }
3317
3318                 if (ov5640_is_csi2(sensor))
3319                         ret = ov5640_set_stream_mipi(sensor, enable);
3320                 else
3321                         ret = ov5640_set_stream_dvp(sensor, enable);
3322
3323                 if (!ret)
3324                         sensor->streaming = enable;
3325         }
3326 out:
3327         mutex_unlock(&sensor->lock);
3328         return ret;
3329 }
3330
3331 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3332         .s_power = ov5640_s_power,
3333         .log_status = v4l2_ctrl_subdev_log_status,
3334         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3335         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3336 };
3337
3338 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3339         .g_frame_interval = ov5640_g_frame_interval,
3340         .s_frame_interval = ov5640_s_frame_interval,
3341         .s_stream = ov5640_s_stream,
3342 };
3343
3344 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3345         .enum_mbus_code = ov5640_enum_mbus_code,
3346         .get_fmt = ov5640_get_fmt,
3347         .set_fmt = ov5640_set_fmt,
3348         .enum_frame_size = ov5640_enum_frame_size,
3349         .enum_frame_interval = ov5640_enum_frame_interval,
3350 };
3351
3352 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3353         .core = &ov5640_core_ops,
3354         .video = &ov5640_video_ops,
3355         .pad = &ov5640_pad_ops,
3356 };
3357
3358 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3359 {
3360         int i;
3361
3362         for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3363                 sensor->supplies[i].supply = ov5640_supply_name[i];
3364
3365         return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3366                                        OV5640_NUM_SUPPLIES,
3367                                        sensor->supplies);
3368 }
3369
3370 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3371 {
3372         struct i2c_client *client = sensor->i2c_client;
3373         int ret = 0;
3374         u16 chip_id;
3375
3376         ret = ov5640_set_power_on(sensor);
3377         if (ret)
3378                 return ret;
3379
3380         ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3381         if (ret) {
3382                 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3383                         __func__);
3384                 goto power_off;
3385         }
3386
3387         if (chip_id != 0x5640) {
3388                 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3389                         __func__, chip_id);
3390                 ret = -ENXIO;
3391         }
3392
3393 power_off:
3394         ov5640_set_power_off(sensor);
3395         return ret;
3396 }
3397
3398 static int ov5640_probe(struct i2c_client *client)
3399 {
3400         struct device *dev = &client->dev;
3401         struct fwnode_handle *endpoint;
3402         struct ov5640_dev *sensor;
3403         struct v4l2_mbus_framefmt *fmt;
3404         u32 rotation;
3405         int ret;
3406
3407         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3408         if (!sensor)
3409                 return -ENOMEM;
3410
3411         sensor->i2c_client = client;
3412
3413         /*
3414          * default init sequence initialize sensor to
3415          * YUV422 UYVY VGA@30fps
3416          */
3417         fmt = &sensor->fmt;
3418         fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
3419         fmt->colorspace = V4L2_COLORSPACE_SRGB;
3420         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
3421         fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
3422         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
3423         fmt->width = 640;
3424         fmt->height = 480;
3425         fmt->field = V4L2_FIELD_NONE;
3426         sensor->frame_interval.numerator = 1;
3427         sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3428         sensor->current_fr = OV5640_30_FPS;
3429         sensor->current_mode =
3430                 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3431         sensor->last_mode = sensor->current_mode;
3432         sensor->current_link_freq = OV5640_DEFAULT_LINK_FREQ;
3433
3434         sensor->ae_target = 52;
3435
3436         /* optional indication of physical rotation of sensor */
3437         ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3438                                        &rotation);
3439         if (!ret) {
3440                 switch (rotation) {
3441                 case 180:
3442                         sensor->upside_down = true;
3443                         fallthrough;
3444                 case 0:
3445                         break;
3446                 default:
3447                         dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3448                                  rotation);
3449                 }
3450         }
3451
3452         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3453                                                   NULL);
3454         if (!endpoint) {
3455                 dev_err(dev, "endpoint node not found\n");
3456                 return -EINVAL;
3457         }
3458
3459         ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3460         fwnode_handle_put(endpoint);
3461         if (ret) {
3462                 dev_err(dev, "Could not parse endpoint\n");
3463                 return ret;
3464         }
3465
3466         if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3467             sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3468             sensor->ep.bus_type != V4L2_MBUS_BT656) {
3469                 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3470                 return -EINVAL;
3471         }
3472
3473         /* get system clock (xclk) */
3474         sensor->xclk = devm_clk_get(dev, "xclk");
3475         if (IS_ERR(sensor->xclk)) {
3476                 dev_err(dev, "failed to get xclk\n");
3477                 return PTR_ERR(sensor->xclk);
3478         }
3479
3480         sensor->xclk_freq = clk_get_rate(sensor->xclk);
3481         if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3482             sensor->xclk_freq > OV5640_XCLK_MAX) {
3483                 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3484                         sensor->xclk_freq);
3485                 return -EINVAL;
3486         }
3487
3488         /* request optional power down pin */
3489         sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3490                                                     GPIOD_OUT_HIGH);
3491         if (IS_ERR(sensor->pwdn_gpio))
3492                 return PTR_ERR(sensor->pwdn_gpio);
3493
3494         /* request optional reset pin */
3495         sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3496                                                      GPIOD_OUT_HIGH);
3497         if (IS_ERR(sensor->reset_gpio))
3498                 return PTR_ERR(sensor->reset_gpio);
3499
3500         v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3501
3502         sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3503                             V4L2_SUBDEV_FL_HAS_EVENTS;
3504         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3505         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3506         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3507         if (ret)
3508                 return ret;
3509
3510         ret = ov5640_get_regulators(sensor);
3511         if (ret)
3512                 return ret;
3513
3514         mutex_init(&sensor->lock);
3515
3516         ret = ov5640_check_chip_id(sensor);
3517         if (ret)
3518                 goto entity_cleanup;
3519
3520         ret = ov5640_init_controls(sensor);
3521         if (ret)
3522                 goto entity_cleanup;
3523
3524         ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3525         if (ret)
3526                 goto free_ctrls;
3527
3528         return 0;
3529
3530 free_ctrls:
3531         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3532 entity_cleanup:
3533         media_entity_cleanup(&sensor->sd.entity);
3534         mutex_destroy(&sensor->lock);
3535         return ret;
3536 }
3537
3538 static int ov5640_remove(struct i2c_client *client)
3539 {
3540         struct v4l2_subdev *sd = i2c_get_clientdata(client);
3541         struct ov5640_dev *sensor = to_ov5640_dev(sd);
3542
3543         v4l2_async_unregister_subdev(&sensor->sd);
3544         media_entity_cleanup(&sensor->sd.entity);
3545         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3546         mutex_destroy(&sensor->lock);
3547
3548         return 0;
3549 }
3550
3551 static const struct i2c_device_id ov5640_id[] = {
3552         {"ov5640", 0},
3553         {},
3554 };
3555 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3556
3557 static const struct of_device_id ov5640_dt_ids[] = {
3558         { .compatible = "ovti,ov5640" },
3559         { /* sentinel */ }
3560 };
3561 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3562
3563 static struct i2c_driver ov5640_i2c_driver = {
3564         .driver = {
3565                 .name  = "ov5640",
3566                 .of_match_table = ov5640_dt_ids,
3567         },
3568         .id_table = ov5640_id,
3569         .probe_new = ov5640_probe,
3570         .remove   = ov5640_remove,
3571 };
3572
3573 module_i2c_driver(ov5640_i2c_driver);
3574
3575 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3576 MODULE_LICENSE("GPL");