v4l2: modify v4l2 compatible name
[platform/kernel/linux-starfive.git] / drivers / media / platform / starfive / v4l2_driver / ov4689_mipi.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  * Copyright (C) 2021 StarFive Technology Co., Ltd.
6  */
7
8 #include <linux/clk.h>
9 #include <linux/clk-provider.h>
10 #include <linux/clkdev.h>
11 #include <linux/ctype.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
28 #include "stfcamss.h"
29
30
31 #define OV4689_LANES    2
32
33 #define OV4689_LINK_FREQ_500MHZ         500000000LL
34
35 /* min/typical/max system clock (xclk) frequencies */
36 #define OV4689_XCLK_MIN  6000000
37 #define OV4689_XCLK_MAX 54000000
38
39 #define OV4689_CHIP_ID  (0x4688)
40
41 #define OV4689_CHIP_ID_HIGH_BYTE        0x300a   // max should be 0x46
42 #define OV4689_CHIP_ID_LOW_BYTE         0x300b   // max should be 0x88
43 #define OV4689_REG_CHIP_ID              0x300a
44
45 #define OV4689_REG_H_OUTPUT_SIZE        0x3808
46 #define OV4689_REG_V_OUTPUT_SIZE        0x380a
47 #define OV4689_REG_TIMING_HTS           0x380c
48 #define OV4689_REG_TIMING_VTS           0x380e
49
50 #define OV4689_REG_EXPOSURE_HI          0x3500
51 #define OV4689_REG_EXPOSURE_MED         0x3501
52 #define OV4689_REG_EXPOSURE_LO          0x3502
53 #define OV4689_REG_GAIN_H               0x3507
54 #define OV4689_REG_GAIN_M               0x3508
55 #define OV4689_REG_GAIN_L               0x3509
56 #define OV4689_REG_TEST_PATTERN         0x5040
57 #define OV4689_REG_TIMING_TC_REG20      0x3820
58 #define OV4689_REG_TIMING_TC_REG21      0x3821
59
60 #define OV4689_REG_AWB_R_GAIN           0x500C
61 #define OV4689_REG_AWB_B_GAIN           0x5010
62
63
64 enum ov4689_mode_id {
65         //OV4689_MODE_720P_1280_720 = 0,
66         OV4689_MODE_1080P_1920_1080 = 0,
67         //OV4689_MODE_4M_2688_1520,
68         OV4689_NUM_MODES,
69 };
70
71 enum ov4689_frame_rate {
72         OV4689_15_FPS = 0,
73         OV4689_30_FPS,
74         OV4689_45_FPS,
75         OV4689_60_FPS,
76         OV4689_90_FPS,
77         OV4689_120_FPS,
78         OV4689_150_FPS,
79         OV4689_180_FPS,
80         OV4689_330_FPS,
81         OV4689_NUM_FRAMERATES,
82 };
83
84 enum ov4689_format_mux {
85         OV4689_FMT_MUX_RAW,
86 };
87
88 static const int ov4689_framerates[] = {
89         [OV4689_15_FPS] = 15,
90         [OV4689_30_FPS] = 30,
91         [OV4689_45_FPS] = 45,
92         [OV4689_60_FPS] = 60,
93         [OV4689_90_FPS] = 90,
94         [OV4689_120_FPS] = 120,
95         [OV4689_150_FPS] = 150,
96         [OV4689_180_FPS] = 180,
97         [OV4689_330_FPS] = 330,
98 };
99
100 /* regulator supplies */
101 static const char * const ov4689_supply_name[] = {
102         "DOVDD", /* Digital I/O (1.8V) supply */
103         "AVDD",  /* Analog (2.8V) supply */
104         "DVDD",  /* Digital Core (1.5V) supply */
105 };
106
107 #define OV4689_NUM_SUPPLIES ARRAY_SIZE(ov4689_supply_name)
108
109 /*
110  * Image size under 1280 * 960 are SUBSAMPLING
111  * Image size upper 1280 * 960 are SCALING
112  */
113 enum ov4689_downsize_mode {
114         SUBSAMPLING,
115         SCALING,
116 };
117
118 struct reg_value {
119         u16 reg_addr;
120         u8 val;
121         u8 mask;
122         u32 delay_ms;
123 };
124
125 struct ov4689_mode_info {
126         enum ov4689_mode_id id;
127         enum ov4689_downsize_mode dn_mode;
128         u32 hact;
129         u32 htot;
130         u32 vact;
131         u32 vtot;
132         const struct reg_value *reg_data;
133         u32 reg_data_size;
134         u32 max_fps;
135 };
136
137 struct ov4689_ctrls {
138         struct v4l2_ctrl_handler handler;
139         struct v4l2_ctrl *pixel_rate;
140         struct {
141                 struct v4l2_ctrl *exposure;
142         };
143         struct {
144                 struct v4l2_ctrl *auto_wb;
145                 struct v4l2_ctrl *blue_balance;
146                 struct v4l2_ctrl *red_balance;
147         };
148         struct {
149                 struct v4l2_ctrl *anal_gain;
150         };
151         struct v4l2_ctrl *brightness;
152         struct v4l2_ctrl *light_freq;
153         struct v4l2_ctrl *link_freq;
154         struct v4l2_ctrl *saturation;
155         struct v4l2_ctrl *contrast;
156         struct v4l2_ctrl *hue;
157         struct v4l2_ctrl *test_pattern;
158         struct v4l2_ctrl *hflip;
159         struct v4l2_ctrl *vflip;
160 };
161
162 struct ov4689_dev {
163         struct i2c_client *i2c_client;
164         struct v4l2_subdev sd;
165         struct media_pad pad;
166         struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
167         struct clk *xclk; /* system clock to OV4689 */
168         u32 xclk_freq;
169
170         struct regulator_bulk_data supplies[OV4689_NUM_SUPPLIES];
171         struct gpio_desc *reset_gpio;
172         struct gpio_desc *pwdn_gpio;
173         bool   upside_down;
174
175         /* lock to protect all members below */
176         struct mutex lock;
177
178         int power_count;
179
180         struct v4l2_mbus_framefmt fmt;
181
182         const struct ov4689_mode_info *current_mode;
183         const struct ov4689_mode_info *last_mode;
184         enum ov4689_frame_rate current_fr;
185         struct v4l2_fract frame_interval;
186
187         struct ov4689_ctrls ctrls;
188
189         u32 prev_sysclk, prev_hts;
190         u32 ae_low, ae_high, ae_target;
191
192         bool pending_mode_change;
193         int streaming;
194 };
195
196 static inline struct ov4689_dev *to_ov4689_dev(struct v4l2_subdev *sd)
197 {
198         return container_of(sd, struct ov4689_dev, sd);
199 }
200
201 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
202 {
203         return &container_of(ctrl->handler, struct ov4689_dev,
204                         ctrls.handler)->sd;
205 }
206
207 /* ov4689 initial register */
208 static const struct reg_value ov4689_init_setting_30fps_1080P[] = {
209 /* ov4689_1080p_30fps_4d */
210         {0x0103, 0x01, 0, 0},
211         {0x3638, 0x00, 0, 0},
212         {0x0300, 0x02, 0, 0},
213         {0x0302, 0x32, 0, 0},
214         {0x0303, 0x00, 0, 0},
215         {0x0304, 0x03, 0, 0},
216         {0x030b, 0x00, 0, 0},
217         {0x030d, 0x1e, 0, 0},
218         {0x030e, 0x04, 0, 0},
219         {0x030f, 0x01, 0, 0},
220         {0x0312, 0x01, 0, 0},
221         {0x031e, 0x00, 0, 0},
222         {0x3000, 0x20, 0, 0},
223         {0x3002, 0x00, 0, 0},
224         {0x3018, 0x72, 0, 0},
225         {0x3020, 0x93, 0, 0},
226         {0x3021, 0x03, 0, 0},
227         {0x3022, 0x01, 0, 0},
228         {0x3031, 0x0a, 0, 0},
229         {0x3305, 0xf1, 0, 0},
230         {0x3307, 0x04, 0, 0},
231         {0x3309, 0x29, 0, 0},
232         {0x3500, 0x00, 0, 0},
233         {0x3501, 0x4c, 0, 0},
234         {0x3502, 0x00, 0, 0},
235         {0x3503, 0x04, 0, 0},
236         {0x3504, 0x00, 0, 0},
237         {0x3505, 0x00, 0, 0},
238         {0x3506, 0x00, 0, 0},
239         {0x3507, 0x00, 0, 0},
240         {0x3508, 0x00, 0, 0},
241         {0x3509, 0x80, 0, 0},
242         {0x350a, 0x00, 0, 0},
243         {0x350b, 0x00, 0, 0},
244         {0x350c, 0x00, 0, 0},
245         {0x350d, 0x00, 0, 0},
246         {0x350e, 0x00, 0, 0},
247         {0x350f, 0x80, 0, 0},
248         {0x3510, 0x00, 0, 0},
249         {0x3511, 0x00, 0, 0},
250         {0x3512, 0x00, 0, 0},
251         {0x3513, 0x00, 0, 0},
252         {0x3514, 0x00, 0, 0},
253         {0x3515, 0x80, 0, 0},
254         {0x3516, 0x00, 0, 0},
255         {0x3517, 0x00, 0, 0},
256         {0x3518, 0x00, 0, 0},
257         {0x3519, 0x00, 0, 0},
258         {0x351a, 0x00, 0, 0},
259         {0x351b, 0x80, 0, 0},
260         {0x351c, 0x00, 0, 0},
261         {0x351d, 0x00, 0, 0},
262         {0x351e, 0x00, 0, 0},
263         {0x351f, 0x00, 0, 0},
264         {0x3520, 0x00, 0, 0},
265         {0x3521, 0x80, 0, 0},
266         {0x3522, 0x08, 0, 0},
267         {0x3524, 0x08, 0, 0},
268         {0x3526, 0x08, 0, 0},
269         {0x3528, 0x08, 0, 0},
270         {0x352a, 0x08, 0, 0},
271         {0x3602, 0x00, 0, 0},
272         {0x3603, 0x40, 0, 0},
273         {0x3604, 0x02, 0, 0},
274         {0x3605, 0x00, 0, 0},
275         {0x3606, 0x00, 0, 0},
276         {0x3607, 0x00, 0, 0},
277         {0x3609, 0x12, 0, 0},
278         {0x360a, 0x40, 0, 0},
279         {0x360c, 0x08, 0, 0},
280         {0x360f, 0xe5, 0, 0},
281         {0x3608, 0x8f, 0, 0},
282         {0x3611, 0x00, 0, 0},
283         {0x3613, 0xf7, 0, 0},
284         {0x3616, 0x58, 0, 0},
285         {0x3619, 0x99, 0, 0},
286         {0x361b, 0x60, 0, 0},
287         {0x361c, 0x7a, 0, 0},
288         {0x361e, 0x79, 0, 0},
289         {0x361f, 0x02, 0, 0},
290         {0x3632, 0x00, 0, 0},
291         {0x3633, 0x10, 0, 0},
292         {0x3634, 0x10, 0, 0},
293         {0x3635, 0x10, 0, 0},
294         {0x3636, 0x15, 0, 0},
295         {0x3646, 0x86, 0, 0},
296         {0x364a, 0x0b, 0, 0},
297         {0x3700, 0x17, 0, 0},
298         {0x3701, 0x22, 0, 0},
299         {0x3703, 0x10, 0, 0},
300         {0x370a, 0x37, 0, 0},
301         {0x3705, 0x00, 0, 0},
302         {0x3706, 0x63, 0, 0},
303         {0x3709, 0x3c, 0, 0},
304         {0x370b, 0x01, 0, 0},
305         {0x370c, 0x30, 0, 0},
306         {0x3710, 0x24, 0, 0},
307         {0x3711, 0x0c, 0, 0},
308         {0x3716, 0x00, 0, 0},
309         {0x3720, 0x28, 0, 0},
310         {0x3729, 0x7b, 0, 0},
311         {0x372a, 0x84, 0, 0},
312         {0x372b, 0xbd, 0, 0},
313         {0x372c, 0xbc, 0, 0},
314         {0x372e, 0x52, 0, 0},
315         {0x373c, 0x0e, 0, 0},
316         {0x373e, 0x33, 0, 0},
317         {0x3743, 0x10, 0, 0},
318         {0x3744, 0x88, 0, 0},
319         {0x3745, 0xc0, 0, 0},
320         {0x374a, 0x43, 0, 0},
321         {0x374c, 0x00, 0, 0},
322         {0x374e, 0x23, 0, 0},
323         {0x3751, 0x7b, 0, 0},
324         {0x3752, 0x84, 0, 0},
325         {0x3753, 0xbd, 0, 0},
326         {0x3754, 0xbc, 0, 0},
327         {0x3756, 0x52, 0, 0},
328         {0x375c, 0x00, 0, 0},
329         {0x3760, 0x00, 0, 0},
330         {0x3761, 0x00, 0, 0},
331         {0x3762, 0x00, 0, 0},
332         {0x3763, 0x00, 0, 0},
333         {0x3764, 0x00, 0, 0},
334         {0x3767, 0x04, 0, 0},
335         {0x3768, 0x04, 0, 0},
336         {0x3769, 0x08, 0, 0},
337         {0x376a, 0x08, 0, 0},
338         {0x376b, 0x20, 0, 0},
339         {0x376c, 0x00, 0, 0},
340         {0x376d, 0x00, 0, 0},
341         {0x376e, 0x00, 0, 0},
342         {0x3773, 0x00, 0, 0},
343         {0x3774, 0x51, 0, 0},
344         {0x3776, 0xbd, 0, 0},
345         {0x3777, 0xbd, 0, 0},
346         {0x3781, 0x18, 0, 0},
347         {0x3783, 0x25, 0, 0},
348         {0x3798, 0x1b, 0, 0},
349         {0x3800, 0x01, 0, 0},
350         {0x3801, 0x88, 0, 0},
351         {0x3802, 0x00, 0, 0},
352         {0x3803, 0xe0, 0, 0},
353         {0x3804, 0x09, 0, 0},
354         {0x3805, 0x17, 0, 0},
355         {0x3806, 0x05, 0, 0},
356         {0x3807, 0x1f, 0, 0},
357         {0x3808, 0x07, 0, 0},
358         {0x3809, 0x80, 0, 0},
359         {0x380a, 0x04, 0, 0},
360         {0x380b, 0x38, 0, 0},
361         {0x380c, 0x0d, 0, 0},
362         {0x380d, 0x70, 0, 0},
363         {0x380e, 0x04, 0, 0},
364         {0x380f, 0x8A, 0, 0},
365         {0x3810, 0x00, 0, 0},
366         {0x3811, 0x08, 0, 0},
367         {0x3812, 0x00, 0, 0},
368         {0x3813, 0x04, 0, 0},
369         {0x3814, 0x01, 0, 0},
370         {0x3815, 0x01, 0, 0},
371         {0x3819, 0x01, 0, 0},
372         {0x3820, 0x06, 0, 0},
373         {0x3821, 0x00, 0, 0},
374         {0x3829, 0x00, 0, 0},
375         {0x382a, 0x01, 0, 0},
376         {0x382b, 0x01, 0, 0},
377         {0x382d, 0x7f, 0, 0},
378         {0x3830, 0x04, 0, 0},
379         {0x3836, 0x01, 0, 0},
380         {0x3837, 0x00, 0, 0},
381         {0x3841, 0x02, 0, 0},
382         {0x3846, 0x08, 0, 0},
383         {0x3847, 0x07, 0, 0},
384         {0x3d85, 0x36, 0, 0},
385         {0x3d8c, 0x71, 0, 0},
386         {0x3d8d, 0xcb, 0, 0},
387         {0x3f0a, 0x00, 0, 0},
388         {0x4000, 0xf1, 0, 0},
389         {0x4001, 0x40, 0, 0},
390         {0x4002, 0x04, 0, 0},
391         {0x4003, 0x14, 0, 0},
392         {0x400e, 0x00, 0, 0},
393         {0x4011, 0x00, 0, 0},
394         {0x401a, 0x00, 0, 0},
395         {0x401b, 0x00, 0, 0},
396         {0x401c, 0x00, 0, 0},
397         {0x401d, 0x00, 0, 0},
398         {0x401f, 0x00, 0, 0},
399         {0x4020, 0x00, 0, 0},
400         {0x4021, 0x10, 0, 0},
401         {0x4022, 0x06, 0, 0},
402         {0x4023, 0x13, 0, 0},
403         {0x4024, 0x07, 0, 0},
404         {0x4025, 0x40, 0, 0},
405         {0x4026, 0x07, 0, 0},
406         {0x4027, 0x50, 0, 0},
407         {0x4028, 0x00, 0, 0},
408         {0x4029, 0x02, 0, 0},
409         {0x402a, 0x06, 0, 0},
410         {0x402b, 0x04, 0, 0},
411         {0x402c, 0x02, 0, 0},
412         {0x402d, 0x02, 0, 0},
413         {0x402e, 0x0e, 0, 0},
414         {0x402f, 0x04, 0, 0},
415         {0x4302, 0xff, 0, 0},
416         {0x4303, 0xff, 0, 0},
417         {0x4304, 0x00, 0, 0},
418         {0x4305, 0x00, 0, 0},
419         {0x4306, 0x00, 0, 0},
420         {0x4308, 0x02, 0, 0},
421         {0x4500, 0x6c, 0, 0},
422         {0x4501, 0xc4, 0, 0},
423         {0x4502, 0x40, 0, 0},
424         {0x4503, 0x01, 0, 0},
425         {0x4601, 0x77, 0, 0},
426         {0x4800, 0x04, 0, 0},
427         {0x4813, 0x08, 0, 0},
428         {0x481f, 0x40, 0, 0},
429         {0x4829, 0x78, 0, 0},
430         {0x4837, 0x10, 0, 0},
431         {0x4b00, 0x2a, 0, 0},
432         {0x4b0d, 0x00, 0, 0},
433         {0x4d00, 0x04, 0, 0},
434         {0x4d01, 0x42, 0, 0},
435         {0x4d02, 0xd1, 0, 0},
436         {0x4d03, 0x93, 0, 0},
437         {0x4d04, 0xf5, 0, 0},
438         {0x4d05, 0xc1, 0, 0},
439         {0x5000, 0xf3, 0, 0},
440         {0x5001, 0x11, 0, 0},
441         {0x5004, 0x00, 0, 0},
442         {0x500a, 0x00, 0, 0},
443         {0x500b, 0x00, 0, 0},
444         {0x5032, 0x00, 0, 0},
445         {0x5040, 0x00, 0, 0},
446         {0x5050, 0x0c, 0, 0},
447         {0x5500, 0x00, 0, 0},
448         {0x5501, 0x10, 0, 0},
449         {0x5502, 0x01, 0, 0},
450         {0x5503, 0x0f, 0, 0},
451         {0x8000, 0x00, 0, 0},
452         {0x8001, 0x00, 0, 0},
453         {0x8002, 0x00, 0, 0},
454         {0x8003, 0x00, 0, 0},
455         {0x8004, 0x00, 0, 0},
456         {0x8005, 0x00, 0, 0},
457         {0x8006, 0x00, 0, 0},
458         {0x8007, 0x00, 0, 0},
459         {0x8008, 0x00, 0, 0},
460         {0x3638, 0x00, 0, 0},
461 };
462
463 static const struct reg_value ov4689_setting_VGA_640_480[] = {
464         //@@ RES_640x480_2x_Bin_330fps_816Mbps
465         //OV4689_AM01B_640x480_24M_2lane_816Mbps_330fps_20140210.txt
466         {0x0103, 0x01, 0, 0},
467         {0x3638, 0x00, 0, 0},
468         {0x0300, 0x00, 0, 0}, // 00
469         {0x0302, 0x22, 0, 0}, // 816Mbps 5a ; 64 ; 5a ; 78  ; 78 ; 2a
470         {0x0303, 0x00, 0, 0}, // 03 ; 01 ; 02 ;
471         {0x0304, 0x03, 0, 0},
472         {0x030b, 0x00, 0, 0},
473         {0x030d, 0x1e, 0, 0},
474         {0x030e, 0x04, 0, 0},
475         {0x030f, 0x01, 0, 0},
476         {0x0312, 0x01, 0, 0},
477         {0x031e, 0x00, 0, 0},
478         {0x3000, 0x20, 0, 0},
479         {0x3002, 0x00, 0, 0},
480         {0x3018, 0x32, 0, 0}, // 32/72 2lane/4lane
481         {0x3019, 0x0c, 0, 0}, // 0c/00 2lane/4lane
482         {0x3020, 0x93, 0, 0},
483         {0x3021, 0x03, 0, 0},
484         {0x3022, 0x01, 0, 0},
485         {0x3031, 0x0a, 0, 0},
486         {0x303f, 0x0c, 0, 0},
487         {0x3305, 0xf1, 0, 0},
488         {0x3307, 0x04, 0, 0},
489         {0x3309, 0x29, 0, 0},
490         {0x3500, 0x00, 0, 0},
491         {0x3501, 0x4c, 0, 0},
492         {0x3502, 0x00, 0, 0},
493         {0x3503, 0x04, 0, 0},
494         {0x3504, 0x00, 0, 0},
495         {0x3505, 0x00, 0, 0},
496         {0x3506, 0x00, 0, 0},
497         {0x3507, 0x00, 0, 0},
498         {0x3508, 0x00, 0, 0},
499         {0x3509, 0x80, 0, 0}, // 8X
500         {0x350a, 0x00, 0, 0},
501         {0x350b, 0x00, 0, 0},
502         {0x350c, 0x00, 0, 0},
503         {0x350d, 0x00, 0, 0},
504         {0x350e, 0x00, 0, 0},
505         {0x350f, 0x80, 0, 0},
506         {0x3510, 0x00, 0, 0},
507         {0x3511, 0x00, 0, 0},
508         {0x3512, 0x00, 0, 0},
509         {0x3513, 0x00, 0, 0},
510         {0x3514, 0x00, 0, 0},
511         {0x3515, 0x80, 0, 0},
512         {0x3516, 0x00, 0, 0},
513         {0x3517, 0x00, 0, 0},
514         {0x3518, 0x00, 0, 0},
515         {0x3519, 0x00, 0, 0},
516         {0x351a, 0x00, 0, 0},
517         {0x351b, 0x80, 0, 0},
518         {0x351c, 0x00, 0, 0},
519         {0x351d, 0x00, 0, 0},
520         {0x351e, 0x00, 0, 0},
521         {0x351f, 0x00, 0, 0},
522         {0x3520, 0x00, 0, 0},
523         {0x3521, 0x80, 0, 0},
524         {0x3522, 0x08, 0, 0},
525         {0x3524, 0x08, 0, 0},
526         {0x3526, 0x08, 0, 0},
527         {0x3528, 0x08, 0, 0},
528         {0x352a, 0x08, 0, 0},
529         {0x3602, 0x00, 0, 0},
530         {0x3603, 0x40, 0, 0},
531         {0x3604, 0x02, 0, 0},
532         {0x3605, 0x00, 0, 0},
533         {0x3606, 0x00, 0, 0},
534         {0x3607, 0x00, 0, 0},
535         {0x3609, 0x12, 0, 0},
536         {0x360a, 0x40, 0, 0},
537         {0x360c, 0x08, 0, 0},
538         {0x360f, 0xe5, 0, 0},
539         {0x3608, 0x8f, 0, 0},
540         {0x3611, 0x00, 0, 0},
541         {0x3613, 0xf7, 0, 0},
542         {0x3616, 0x58, 0, 0},
543         {0x3619, 0x99, 0, 0},
544         {0x361b, 0x60, 0, 0},
545         {0x361c, 0x7a, 0, 0},
546         {0x361e, 0x79, 0, 0},
547         {0x361f, 0x02, 0, 0},
548         {0x3632, 0x05, 0, 0},
549         {0x3633, 0x10, 0, 0},
550         {0x3634, 0x10, 0, 0},
551         {0x3635, 0x10, 0, 0},
552         {0x3636, 0x15, 0, 0},
553         {0x3646, 0x86, 0, 0},
554         {0x364a, 0x0b, 0, 0},
555         {0x3700, 0x17, 0, 0},
556         {0x3701, 0x22, 0, 0},
557         {0x3703, 0x10, 0, 0},
558         {0x370a, 0x37, 0, 0},
559         {0x3705, 0x00, 0, 0},
560         {0x3706, 0x63, 0, 0},
561         {0x3709, 0x3c, 0, 0},
562         {0x370b, 0x01, 0, 0},
563         {0x370c, 0x30, 0, 0},
564         {0x3710, 0x24, 0, 0},
565         {0x3711, 0x0c, 0, 0},
566         {0x3716, 0x00, 0, 0},
567         {0x3720, 0x28, 0, 0},
568         {0x3729, 0x7b, 0, 0},
569         {0x372a, 0x84, 0, 0},
570         {0x372b, 0xbd, 0, 0},
571         {0x372c, 0xbc, 0, 0},
572         {0x372e, 0x52, 0, 0},
573         {0x373c, 0x0e, 0, 0},
574         {0x373e, 0x33, 0, 0},
575         {0x3743, 0x10, 0, 0},
576         {0x3744, 0x88, 0, 0},
577         {0x3745, 0xc0, 0, 0},
578         {0x374a, 0x43, 0, 0},
579         {0x374c, 0x00, 0, 0},
580         {0x374e, 0x23, 0, 0},
581         {0x3751, 0x7b, 0, 0},
582         {0x3752, 0x84, 0, 0},
583         {0x3753, 0xbd, 0, 0},
584         {0x3754, 0xbc, 0, 0},
585         {0x3756, 0x52, 0, 0},
586         {0x375c, 0x00, 0, 0},
587         {0x3760, 0x00, 0, 0},
588         {0x3761, 0x00, 0, 0},
589         {0x3762, 0x00, 0, 0},
590         {0x3763, 0x00, 0, 0},
591         {0x3764, 0x00, 0, 0},
592         {0x3767, 0x04, 0, 0},
593         {0x3768, 0x04, 0, 0},
594         {0x3769, 0x08, 0, 0},
595         {0x376a, 0x08, 0, 0},
596         {0x376b, 0x40, 0, 0},
597         {0x376c, 0x00, 0, 0},
598         {0x376d, 0x00, 0, 0},
599         {0x376e, 0x00, 0, 0},
600         {0x3773, 0x00, 0, 0},
601         {0x3774, 0x51, 0, 0},
602         {0x3776, 0xbd, 0, 0},
603         {0x3777, 0xbd, 0, 0},
604         {0x3781, 0x18, 0, 0},
605         {0x3783, 0x25, 0, 0},
606         {0x3798, 0x1b, 0, 0},
607         {0x3800, 0x00, 0, 0},
608         {0x3801, 0x48, 0, 0},
609         {0x3802, 0x00, 0, 0},
610         {0x3803, 0x2C, 0, 0},
611         {0x3804, 0x0a, 0, 0},
612         {0x3805, 0x57, 0, 0},
613         {0x3806, 0x05, 0, 0},
614         {0x3807, 0xD3, 0, 0},
615         {0x3808, 0x02, 0, 0},
616         {0x3809, 0x80, 0, 0},
617         {0x380a, 0x01, 0, 0},
618         {0x380b, 0xe0, 0, 0},
619
620         {0x380c, 0x02, 0, 0}, // 0a ; 03
621         {0x380d, 0x04, 0, 0}, // 1c ; 5C
622
623         {0x380e, 0x03, 0, 0},
624         {0x380f, 0x05, 0, 0},
625         {0x3810, 0x00, 0, 0},
626         {0x3811, 0x04, 0, 0},
627         {0x3812, 0x00, 0, 0},
628         {0x3813, 0x02, 0, 0},
629         {0x3814, 0x03, 0, 0},
630         {0x3815, 0x01, 0, 0},
631         {0x3819, 0x01, 0, 0},
632         {0x3820, 0x06, 0, 0},
633         {0x3821, 0x00, 0, 0},
634         {0x3829, 0x00, 0, 0},
635         {0x382a, 0x03, 0, 0},
636         {0x382b, 0x01, 0, 0},
637         {0x382d, 0x7f, 0, 0},
638         {0x3830, 0x08, 0, 0},
639         {0x3836, 0x02, 0, 0},
640         {0x3837, 0x00, 0, 0},
641         {0x3841, 0x02, 0, 0},
642         {0x3846, 0x08, 0, 0},
643         {0x3847, 0x07, 0, 0},
644         {0x3d85, 0x36, 0, 0},
645         {0x3d8c, 0x71, 0, 0},
646         {0x3d8d, 0xcb, 0, 0},
647         {0x3f0a, 0x00, 0, 0},
648         {0x4000, 0x71, 0, 0},
649         {0x4001, 0x50, 0, 0},
650         {0x4002, 0x04, 0, 0},
651         {0x4003, 0x14, 0, 0},
652         {0x400e, 0x00, 0, 0},
653         {0x4011, 0x00, 0, 0},
654         {0x401a, 0x00, 0, 0},
655         {0x401b, 0x00, 0, 0},
656         {0x401c, 0x00, 0, 0},
657         {0x401d, 0x00, 0, 0},
658         {0x401f, 0x00, 0, 0},
659         {0x4020, 0x00, 0, 0},
660         {0x4021, 0x10, 0, 0},
661         {0x4022, 0x03, 0, 0},
662         {0x4023, 0x93, 0, 0},
663         {0x4024, 0x04, 0, 0},
664         {0x4025, 0xC0, 0, 0},
665         {0x4026, 0x04, 0, 0},
666         {0x4027, 0xD0, 0, 0},
667         {0x4028, 0x00, 0, 0},
668         {0x4029, 0x02, 0, 0},
669         {0x402a, 0x06, 0, 0},
670         {0x402b, 0x04, 0, 0},
671         {0x402c, 0x02, 0, 0},
672         {0x402d, 0x02, 0, 0},
673         {0x402e, 0x0e, 0, 0},
674         {0x402f, 0x04, 0, 0},
675         {0x4302, 0xff, 0, 0},
676         {0x4303, 0xff, 0, 0},
677         {0x4304, 0x00, 0, 0},
678         {0x4305, 0x00, 0, 0},
679         {0x4306, 0x00, 0, 0},
680         {0x4308, 0x02, 0, 0},
681         {0x4500, 0x6c, 0, 0},
682         {0x4501, 0xc4, 0, 0},
683         {0x4502, 0x44, 0, 0},
684         {0x4503, 0x01, 0, 0},
685         {0x4600, 0x00, 0, 0},
686         {0x4601, 0x4F, 0, 0},
687         {0x4800, 0x04, 0, 0},
688         {0x4813, 0x08, 0, 0},
689         {0x481f, 0x40, 0, 0},
690         {0x4829, 0x78, 0, 0},
691         {0x4837, 0x10, 0, 0}, // 20 ; 10
692         {0x4b00, 0x2a, 0, 0},
693         {0x4b0d, 0x00, 0, 0},
694         {0x4d00, 0x04, 0, 0},
695         {0x4d01, 0x42, 0, 0},
696         {0x4d02, 0xd1, 0, 0},
697         {0x4d03, 0x93, 0, 0},
698         {0x4d04, 0xf5, 0, 0},
699         {0x4d05, 0xc1, 0, 0},
700         {0x5000, 0xf3, 0, 0},
701         {0x5001, 0x11, 0, 0},
702         {0x5004, 0x00, 0, 0},
703         {0x500a, 0x00, 0, 0},
704         {0x500b, 0x00, 0, 0},
705         {0x5032, 0x00, 0, 0},
706         {0x5040, 0x00, 0, 0},
707         {0x5050, 0x3c, 0, 0},
708         {0x5500, 0x00, 0, 0},
709         {0x5501, 0x10, 0, 0},
710         {0x5502, 0x01, 0, 0},
711         {0x5503, 0x0f, 0, 0},
712         {0x8000, 0x00, 0, 0},
713         {0x8001, 0x00, 0, 0},
714         {0x8002, 0x00, 0, 0},
715         {0x8003, 0x00, 0, 0},
716         {0x8004, 0x00, 0, 0},
717         {0x8005, 0x00, 0, 0},
718         {0x8006, 0x00, 0, 0},
719         {0x8007, 0x00, 0, 0},
720         {0x8008, 0x00, 0, 0},
721         {0x3638, 0x00, 0, 0},
722 };
723
724 static const struct reg_value ov4689_setting_720P_1280_720[] = {
725         //@@ RES_1280x720_2x_Bin_150fps_816Mbps
726         //OV4689_AM01B_1280x720_24M_2lane_816Mbps_150fps_20140210.txt
727         {0x0103, 0x01, 0, 0},
728         {0x3638, 0x00, 0, 0},
729         {0x0300, 0x00, 0, 0}, // 00
730         {0x0302, 0x22, 0, 0}, // 816Mbps 5a ; 64 ; 5a ; 78  ; 78 ; 2a
731         {0x0303, 0x00, 0, 0}, // 03 ; 01 ; 02 ;
732         {0x0304, 0x03, 0, 0},
733         {0x030b, 0x00, 0, 0},
734         {0x030d, 0x1e, 0, 0},
735         {0x030e, 0x04, 0, 0},
736         {0x030f, 0x01, 0, 0},
737         {0x0312, 0x01, 0, 0},
738         {0x031e, 0x00, 0, 0},
739         {0x3000, 0x20, 0, 0},
740         {0x3002, 0x00, 0, 0},
741         {0x3018, 0x32, 0, 0}, // 32/72 2lane/4lane
742         {0x3019, 0x0c, 0, 0}, // 0c/00 2lane/4lane
743         {0x3020, 0x93, 0, 0},
744         {0x3021, 0x03, 0, 0},
745         {0x3022, 0x01, 0, 0},
746         {0x3031, 0x0a, 0, 0},
747         {0x303f, 0x0c, 0, 0},
748         {0x3305, 0xf1, 0, 0},
749         {0x3307, 0x04, 0, 0},
750         {0x3309, 0x29, 0, 0},
751         {0x3500, 0x00, 0, 0},
752         {0x3501, 0x30, 0, 0},
753         {0x3502, 0x00, 0, 0},
754         {0x3503, 0x04, 0, 0},
755         {0x3504, 0x00, 0, 0},
756         {0x3505, 0x00, 0, 0},
757         {0x3506, 0x00, 0, 0},
758         {0x3507, 0x00, 0, 0},
759         {0x3508, 0x07, 0, 0},
760         {0x3509, 0x78, 0, 0}, // 8X
761         {0x350a, 0x00, 0, 0},
762         {0x350b, 0x00, 0, 0},
763         {0x350c, 0x00, 0, 0},
764         {0x350d, 0x00, 0, 0},
765         {0x350e, 0x00, 0, 0},
766         {0x350f, 0x80, 0, 0},
767         {0x3510, 0x00, 0, 0},
768         {0x3511, 0x00, 0, 0},
769         {0x3512, 0x00, 0, 0},
770         {0x3513, 0x00, 0, 0},
771         {0x3514, 0x00, 0, 0},
772         {0x3515, 0x80, 0, 0},
773         {0x3516, 0x00, 0, 0},
774         {0x3517, 0x00, 0, 0},
775         {0x3518, 0x00, 0, 0},
776         {0x3519, 0x00, 0, 0},
777         {0x351a, 0x00, 0, 0},
778         {0x351b, 0x80, 0, 0},
779         {0x351c, 0x00, 0, 0},
780         {0x351d, 0x00, 0, 0},
781         {0x351e, 0x00, 0, 0},
782         {0x351f, 0x00, 0, 0},
783         {0x3520, 0x00, 0, 0},
784         {0x3521, 0x80, 0, 0},
785         {0x3522, 0x08, 0, 0},
786         {0x3524, 0x08, 0, 0},
787         {0x3526, 0x08, 0, 0},
788         {0x3528, 0x08, 0, 0},
789         {0x352a, 0x08, 0, 0},
790         {0x3602, 0x00, 0, 0},
791         {0x3603, 0x40, 0, 0},
792         {0x3604, 0x02, 0, 0},
793         {0x3605, 0x00, 0, 0},
794         {0x3606, 0x00, 0, 0},
795         {0x3607, 0x00, 0, 0},
796         {0x3609, 0x12, 0, 0},
797         {0x360a, 0x40, 0, 0},
798         {0x360c, 0x08, 0, 0},
799         {0x360f, 0xe5, 0, 0},
800         {0x3608, 0x8f, 0, 0},
801         {0x3611, 0x00, 0, 0},
802         {0x3613, 0xf7, 0, 0},
803         {0x3616, 0x58, 0, 0},
804         {0x3619, 0x99, 0, 0},
805         {0x361b, 0x60, 0, 0},
806         {0x361c, 0x7a, 0, 0},
807         {0x361e, 0x79, 0, 0},
808         {0x361f, 0x02, 0, 0},
809         {0x3632, 0x05, 0, 0},
810         {0x3633, 0x10, 0, 0},
811         {0x3634, 0x10, 0, 0},
812         {0x3635, 0x10, 0, 0},
813         {0x3636, 0x15, 0, 0},
814         {0x3646, 0x86, 0, 0},
815         {0x364a, 0x0b, 0, 0},
816         {0x3700, 0x17, 0, 0},
817         {0x3701, 0x22, 0, 0},
818         {0x3703, 0x10, 0, 0},
819         {0x370a, 0x37, 0, 0},
820         {0x3705, 0x00, 0, 0},
821         {0x3706, 0x63, 0, 0},
822         {0x3709, 0x3c, 0, 0},
823         {0x370b, 0x01, 0, 0},
824         {0x370c, 0x30, 0, 0},
825         {0x3710, 0x24, 0, 0},
826         {0x3711, 0x0c, 0, 0},
827         {0x3716, 0x00, 0, 0},
828         {0x3720, 0x28, 0, 0},
829         {0x3729, 0x7b, 0, 0},
830         {0x372a, 0x84, 0, 0},
831         {0x372b, 0xbd, 0, 0},
832         {0x372c, 0xbc, 0, 0},
833         {0x372e, 0x52, 0, 0},
834         {0x373c, 0x0e, 0, 0},
835         {0x373e, 0x33, 0, 0},
836         {0x3743, 0x10, 0, 0},
837         {0x3744, 0x88, 0, 0},
838         {0x3745, 0xc0, 0, 0},
839         {0x374a, 0x43, 0, 0},
840         {0x374c, 0x00, 0, 0},
841         {0x374e, 0x23, 0, 0},
842         {0x3751, 0x7b, 0, 0},
843         {0x3752, 0x84, 0, 0},
844         {0x3753, 0xbd, 0, 0},
845         {0x3754, 0xbc, 0, 0},
846         {0x3756, 0x52, 0, 0},
847         {0x375c, 0x00, 0, 0},
848         {0x3760, 0x00, 0, 0},
849         {0x3761, 0x00, 0, 0},
850         {0x3762, 0x00, 0, 0},
851         {0x3763, 0x00, 0, 0},
852         {0x3764, 0x00, 0, 0},
853         {0x3767, 0x04, 0, 0},
854         {0x3768, 0x04, 0, 0},
855         {0x3769, 0x08, 0, 0},
856         {0x376a, 0x08, 0, 0},
857         {0x376b, 0x40, 0, 0},
858         {0x376c, 0x00, 0, 0},
859         {0x376d, 0x00, 0, 0},
860         {0x376e, 0x00, 0, 0},
861         {0x3773, 0x00, 0, 0},
862         {0x3774, 0x51, 0, 0},
863         {0x3776, 0xbd, 0, 0},
864         {0x3777, 0xbd, 0, 0},
865         {0x3781, 0x18, 0, 0},
866         {0x3783, 0x25, 0, 0},
867         {0x3798, 0x1b, 0, 0},
868         {0x3800, 0x00, 0, 0},
869         {0x3801, 0x48, 0, 0},
870         {0x3802, 0x00, 0, 0},
871         {0x3803, 0x2C, 0, 0},
872         {0x3804, 0x0a, 0, 0},
873         {0x3805, 0x57, 0, 0},
874         {0x3806, 0x05, 0, 0},
875         {0x3807, 0xD3, 0, 0},
876         {0x3808, 0x05, 0, 0},
877         {0x3809, 0x00, 0, 0},
878         {0x380a, 0x02, 0, 0},
879         {0x380b, 0xD0, 0, 0},
880 #ifndef UNUSED_CODE
881         {0x380c, 0x04, 0, 0}, // 0a ; 03
882         {0x380d, 0x08, 0, 0}, // 1c ; 5C
883 #else
884         {0x380c, 0x05, 0, 0}, // 120fps
885         {0x380d, 0x0A, 0, 0},
886 #endif
887         {0x380e, 0x03, 0, 0},
888         {0x380f, 0x05, 0, 0},
889         {0x3810, 0x00, 0, 0},
890         {0x3811, 0x04, 0, 0},
891         {0x3812, 0x00, 0, 0},
892         {0x3813, 0x02, 0, 0},
893         {0x3814, 0x03, 0, 0},
894         {0x3815, 0x01, 0, 0},
895         {0x3819, 0x01, 0, 0},
896         {0x3820, 0x06, 0, 0},
897         {0x3821, 0x00, 0, 0},
898         {0x3829, 0x00, 0, 0},
899         {0x382a, 0x03, 0, 0},
900         {0x382b, 0x01, 0, 0},
901         {0x382d, 0x7f, 0, 0},
902         {0x3830, 0x08, 0, 0},
903         {0x3836, 0x02, 0, 0},
904         {0x3837, 0x00, 0, 0},
905         {0x3841, 0x02, 0, 0},
906         {0x3846, 0x08, 0, 0},
907         {0x3847, 0x07, 0, 0},
908         {0x3d85, 0x36, 0, 0},
909         {0x3d8c, 0x71, 0, 0},
910         {0x3d8d, 0xcb, 0, 0},
911         {0x3f0a, 0x00, 0, 0},
912         {0x4000, 0x71, 0, 0},
913         {0x4001, 0x50, 0, 0},
914         {0x4002, 0x04, 0, 0},
915         {0x4003, 0x14, 0, 0},
916         {0x400e, 0x00, 0, 0},
917         {0x4011, 0x00, 0, 0},
918         {0x401a, 0x00, 0, 0},
919         {0x401b, 0x00, 0, 0},
920         {0x401c, 0x00, 0, 0},
921         {0x401d, 0x00, 0, 0},
922         {0x401f, 0x00, 0, 0},
923         {0x4020, 0x00, 0, 0},
924         {0x4021, 0x10, 0, 0},
925         {0x4022, 0x03, 0, 0},
926         {0x4023, 0x93, 0, 0},
927         {0x4024, 0x04, 0, 0},
928         {0x4025, 0xC0, 0, 0},
929         {0x4026, 0x04, 0, 0},
930         {0x4027, 0xD0, 0, 0},
931         {0x4028, 0x00, 0, 0},
932         {0x4029, 0x02, 0, 0},
933         {0x402a, 0x06, 0, 0},
934         {0x402b, 0x04, 0, 0},
935         {0x402c, 0x02, 0, 0},
936         {0x402d, 0x02, 0, 0},
937         {0x402e, 0x0e, 0, 0},
938         {0x402f, 0x04, 0, 0},
939         {0x4302, 0xff, 0, 0},
940         {0x4303, 0xff, 0, 0},
941         {0x4304, 0x00, 0, 0},
942         {0x4305, 0x00, 0, 0},
943         {0x4306, 0x00, 0, 0},
944         {0x4308, 0x02, 0, 0},
945         {0x4500, 0x6c, 0, 0},
946         {0x4501, 0xc4, 0, 0},
947         {0x4502, 0x44, 0, 0},
948         {0x4503, 0x01, 0, 0},
949         {0x4600, 0x00, 0, 0},
950         {0x4601, 0x4F, 0, 0},
951         {0x4800, 0x04, 0, 0},
952         {0x4813, 0x08, 0, 0},
953         {0x481f, 0x40, 0, 0},
954         {0x4829, 0x78, 0, 0},
955         {0x4837, 0x10, 0, 0}, // 20 ; 10
956         {0x4b00, 0x2a, 0, 0},
957         {0x4b0d, 0x00, 0, 0},
958         {0x4d00, 0x04, 0, 0},
959         {0x4d01, 0x42, 0, 0},
960         {0x4d02, 0xd1, 0, 0},
961         {0x4d03, 0x93, 0, 0},
962         {0x4d04, 0xf5, 0, 0},
963         {0x4d05, 0xc1, 0, 0},
964         {0x5000, 0xf3, 0, 0},
965         {0x5001, 0x11, 0, 0},
966         {0x5004, 0x00, 0, 0},
967         {0x500a, 0x00, 0, 0},
968         {0x500b, 0x00, 0, 0},
969         {0x5032, 0x00, 0, 0},
970         {0x5040, 0x00, 0, 0},
971         {0x5050, 0x3c, 0, 0},
972         {0x5500, 0x00, 0, 0},
973         {0x5501, 0x10, 0, 0},
974         {0x5502, 0x01, 0, 0},
975         {0x5503, 0x0f, 0, 0},
976         {0x8000, 0x00, 0, 0},
977         {0x8001, 0x00, 0, 0},
978         {0x8002, 0x00, 0, 0},
979         {0x8003, 0x00, 0, 0},
980         {0x8004, 0x00, 0, 0},
981         {0x8005, 0x00, 0, 0},
982         {0x8006, 0x00, 0, 0},
983         {0x8007, 0x00, 0, 0},
984         {0x8008, 0x00, 0, 0},
985         {0x3638, 0x00, 0, 0},
986 };
987
988 static const struct reg_value ov4689_setting_1080P_1920_1080[] = {
989         //@@ RES_1920x1080_60fps_816Mbps 2lanes
990         {0x0103, 0x01, 0, 0},
991         {0x3638, 0x00, 0, 0},
992         {0x0300, 0x00, 0, 0},  // clk
993         {0x0302, 0x22, 0, 0},
994         {0x0303, 0x00, 0, 0},
995         {0x0304, 0x03, 0, 0},
996         {0x030b, 0x00, 0, 0},
997         {0x030d, 0x1e, 0, 0},
998         {0x030e, 0x04, 0, 0},
999         {0x030f, 0x01, 0, 0},
1000         {0x0312, 0x01, 0, 0},
1001         {0x031e, 0x00, 0, 0},
1002         {0x3000, 0x20, 0, 0},
1003         {0x3002, 0x00, 0, 0},
1004         {0x3018, 0x32, 0, 0},
1005         {0x3019, 0x0c, 0, 0},
1006         {0x3020, 0x93, 0, 0},
1007         {0x3021, 0x03, 0, 0},
1008         {0x3022, 0x01, 0, 0},
1009         {0x3031, 0x0a, 0, 0},
1010         {0x303f, 0x0c, 0, 0},
1011         {0x3305, 0xf1, 0, 0},
1012         {0x3307, 0x04, 0, 0},
1013         {0x3309, 0x29, 0, 0},
1014         {0x3500, 0x00, 0, 0},  // AEC
1015         {0x3501, 0x4c, 0, 0},
1016         {0x3502, 0x00, 0, 0},
1017         {0x3503, 0x04, 0, 0},
1018         {0x3504, 0x00, 0, 0},
1019         {0x3505, 0x00, 0, 0},
1020         {0x3506, 0x00, 0, 0},
1021         {0x3507, 0x00, 0, 0},
1022         {0x3508, 0x00, 0, 0},
1023         {0x3509, 0x80, 0, 0},
1024         {0x350a, 0x00, 0, 0},
1025         {0x350b, 0x00, 0, 0},
1026         {0x350c, 0x00, 0, 0},
1027         {0x350d, 0x00, 0, 0},
1028         {0x350e, 0x00, 0, 0},
1029         {0x350f, 0x80, 0, 0},
1030         {0x3510, 0x00, 0, 0},
1031         {0x3511, 0x00, 0, 0},
1032         {0x3512, 0x00, 0, 0},
1033         {0x3513, 0x00, 0, 0},
1034         {0x3514, 0x00, 0, 0},
1035         {0x3515, 0x80, 0, 0},
1036         {0x3516, 0x00, 0, 0},
1037         {0x3517, 0x00, 0, 0},
1038         {0x3518, 0x00, 0, 0},
1039         {0x3519, 0x00, 0, 0},
1040         {0x351a, 0x00, 0, 0},
1041         {0x351b, 0x80, 0, 0},
1042         {0x351c, 0x00, 0, 0},
1043         {0x351d, 0x00, 0, 0},
1044         {0x351e, 0x00, 0, 0},
1045         {0x351f, 0x00, 0, 0},
1046         {0x3520, 0x00, 0, 0},
1047         {0x3521, 0x80, 0, 0},
1048         {0x3522, 0x08, 0, 0},
1049         {0x3524, 0x08, 0, 0},
1050         {0x3526, 0x08, 0, 0},
1051         {0x3528, 0x08, 0, 0},
1052         {0x352a, 0x08, 0, 0},
1053         {0x3602, 0x00, 0, 0},
1054         {0x3603, 0x40, 0, 0},
1055         {0x3604, 0x02, 0, 0},
1056         {0x3605, 0x00, 0, 0},
1057         {0x3606, 0x00, 0, 0},
1058         {0x3607, 0x00, 0, 0},
1059         {0x3609, 0x12, 0, 0},
1060         {0x360a, 0x40, 0, 0},
1061         {0x360c, 0x08, 0, 0},
1062         {0x360f, 0xe5, 0, 0},
1063         {0x3608, 0x8f, 0, 0},
1064         {0x3611, 0x00, 0, 0},
1065         {0x3613, 0xf7, 0, 0},
1066         {0x3616, 0x58, 0, 0},
1067         {0x3619, 0x99, 0, 0},
1068         {0x361b, 0x60, 0, 0},
1069         {0x361c, 0x7a, 0, 0},
1070         {0x361e, 0x79, 0, 0},
1071         {0x361f, 0x02, 0, 0},
1072         {0x3632, 0x00, 0, 0},
1073         {0x3633, 0x10, 0, 0},
1074         {0x3634, 0x10, 0, 0},
1075         {0x3635, 0x10, 0, 0},
1076         {0x3636, 0x15, 0, 0},
1077         {0x3646, 0x86, 0, 0},
1078         {0x364a, 0x0b, 0, 0},
1079         {0x3700, 0x17, 0, 0},
1080         {0x3701, 0x22, 0, 0},
1081         {0x3703, 0x10, 0, 0},
1082         {0x370a, 0x37, 0, 0},
1083         {0x3705, 0x00, 0, 0},
1084         {0x3706, 0x63, 0, 0},
1085         {0x3709, 0x3c, 0, 0},
1086         {0x370b, 0x01, 0, 0},
1087         {0x370c, 0x30, 0, 0},
1088         {0x3710, 0x24, 0, 0},
1089         {0x3711, 0x0c, 0, 0},
1090         {0x3716, 0x00, 0, 0},
1091         {0x3720, 0x28, 0, 0},
1092         {0x3729, 0x7b, 0, 0},
1093         {0x372a, 0x84, 0, 0},
1094         {0x372b, 0xbd, 0, 0},
1095         {0x372c, 0xbc, 0, 0},
1096         {0x372e, 0x52, 0, 0},
1097         {0x373c, 0x0e, 0, 0},
1098         {0x373e, 0x33, 0, 0},
1099         {0x3743, 0x10, 0, 0},
1100         {0x3744, 0x88, 0, 0},
1101         {0x3745, 0xc0, 0, 0},
1102         {0x374a, 0x43, 0, 0},
1103         {0x374c, 0x00, 0, 0},
1104         {0x374e, 0x23, 0, 0},
1105         {0x3751, 0x7b, 0, 0},
1106         {0x3752, 0x84, 0, 0},
1107         {0x3753, 0xbd, 0, 0},
1108         {0x3754, 0xbc, 0, 0},
1109         {0x3756, 0x52, 0, 0},
1110         {0x375c, 0x00, 0, 0},
1111         {0x3760, 0x00, 0, 0},
1112         {0x3761, 0x00, 0, 0},
1113         {0x3762, 0x00, 0, 0},
1114         {0x3763, 0x00, 0, 0},
1115         {0x3764, 0x00, 0, 0},
1116         {0x3767, 0x04, 0, 0},
1117         {0x3768, 0x04, 0, 0},
1118         {0x3769, 0x08, 0, 0},
1119         {0x376a, 0x08, 0, 0},
1120         {0x376b, 0x20, 0, 0},
1121         {0x376c, 0x00, 0, 0},
1122         {0x376d, 0x00, 0, 0},
1123         {0x376e, 0x00, 0, 0},
1124         {0x3773, 0x00, 0, 0},
1125         {0x3774, 0x51, 0, 0},
1126         {0x3776, 0xbd, 0, 0},
1127         {0x3777, 0xbd, 0, 0},
1128         {0x3781, 0x18, 0, 0},
1129         {0x3783, 0x25, 0, 0},
1130         {0x3798, 0x1b, 0, 0},
1131         {0x3800, 0x01, 0, 0},   // timings
1132         {0x3801, 0x88, 0, 0},
1133         {0x3802, 0x00, 0, 0},
1134         {0x3803, 0xe0, 0, 0},
1135         {0x3804, 0x09, 0, 0},
1136         {0x3805, 0x17, 0, 0},
1137         {0x3806, 0x05, 0, 0},
1138         {0x3807, 0x1f, 0, 0},
1139         {0x3808, 0x07, 0, 0},
1140         {0x3809, 0x80, 0, 0},
1141         {0x380a, 0x04, 0, 0},
1142         {0x380b, 0x38, 0, 0},
1143         {0x380c, 0x06, 0, 0},
1144         {0x380d, 0xe0, 0, 0},
1145         {0x380e, 0x04, 0, 0},
1146         {0x380f, 0x70, 0, 0},
1147         {0x3810, 0x00, 0, 0},
1148         {0x3811, 0x08, 0, 0},
1149         {0x3812, 0x00, 0, 0},
1150         {0x3813, 0x04, 0, 0},
1151         {0x3814, 0x01, 0, 0},
1152         {0x3815, 0x01, 0, 0},
1153         {0x3819, 0x01, 0, 0},
1154         {0x3820, 0x06, 0, 0},
1155         {0x3821, 0x00, 0, 0},
1156         {0x3829, 0x00, 0, 0},
1157         {0x382a, 0x01, 0, 0},
1158         {0x382b, 0x01, 0, 0},
1159         {0x382d, 0x7f, 0, 0},
1160         {0x3830, 0x04, 0, 0},
1161         {0x3836, 0x01, 0, 0},
1162         {0x3837, 0x00, 0, 0},
1163         {0x3841, 0x02, 0, 0},
1164         {0x3846, 0x08, 0, 0},
1165         {0x3847, 0x07, 0, 0},
1166         {0x3d85, 0x36, 0, 0},
1167         {0x3d8c, 0x71, 0, 0},
1168         {0x3d8d, 0xcb, 0, 0},
1169         {0x3f0a, 0x00, 0, 0},
1170         {0x4000, 0xf1, 0, 0},
1171         {0x4001, 0x40, 0, 0},
1172         {0x4002, 0x04, 0, 0},
1173         {0x4003, 0x14, 0, 0},
1174         {0x400e, 0x00, 0, 0},
1175         {0x4011, 0x00, 0, 0},
1176         {0x401a, 0x00, 0, 0},
1177         {0x401b, 0x00, 0, 0},
1178         {0x401c, 0x00, 0, 0},
1179         {0x401d, 0x00, 0, 0},
1180         {0x401f, 0x00, 0, 0},
1181         {0x4020, 0x00, 0, 0},
1182         {0x4021, 0x10, 0, 0},
1183         {0x4022, 0x06, 0, 0},
1184         {0x4023, 0x13, 0, 0},
1185         {0x4024, 0x07, 0, 0},
1186         {0x4025, 0x40, 0, 0},
1187         {0x4026, 0x07, 0, 0},
1188         {0x4027, 0x50, 0, 0},
1189         {0x4028, 0x00, 0, 0},
1190         {0x4029, 0x02, 0, 0},
1191         {0x402a, 0x06, 0, 0},
1192         {0x402b, 0x04, 0, 0},
1193         {0x402c, 0x02, 0, 0},
1194         {0x402d, 0x02, 0, 0},
1195         {0x402e, 0x0e, 0, 0},
1196         {0x402f, 0x04, 0, 0},
1197         {0x4302, 0xff, 0, 0},
1198         {0x4303, 0xff, 0, 0},
1199         {0x4304, 0x00, 0, 0},
1200         {0x4305, 0x00, 0, 0},
1201         {0x4306, 0x00, 0, 0},
1202         {0x4308, 0x02, 0, 0},
1203         {0x4500, 0x6c, 0, 0},
1204         {0x4501, 0xc4, 0, 0},
1205         {0x4502, 0x40, 0, 0},
1206         {0x4503, 0x01, 0, 0},
1207         {0x4601, 0x77, 0, 0},
1208         {0x4800, 0x04, 0, 0},
1209         {0x4813, 0x08, 0, 0},
1210         {0x481f, 0x40, 0, 0},
1211         {0x4829, 0x78, 0, 0},
1212         {0x4837, 0x10, 0, 0},
1213         {0x4b00, 0x2a, 0, 0},
1214         {0x4b0d, 0x00, 0, 0},
1215         {0x4d00, 0x04, 0, 0},
1216         {0x4d01, 0x42, 0, 0},
1217         {0x4d02, 0xd1, 0, 0},
1218         {0x4d03, 0x93, 0, 0},
1219         {0x4d04, 0xf5, 0, 0},
1220         {0x4d05, 0xc1, 0, 0},
1221         {0x5000, 0xf3, 0, 0},
1222         {0x5001, 0x11, 0, 0},
1223         {0x5004, 0x00, 0, 0},
1224         {0x500a, 0x00, 0, 0},
1225         {0x500b, 0x00, 0, 0},
1226         {0x5032, 0x00, 0, 0},
1227         {0x5040, 0x00, 0, 0},
1228         {0x5050, 0x0c, 0, 0},
1229         {0x5500, 0x00, 0, 0},
1230         {0x5501, 0x10, 0, 0},
1231         {0x5502, 0x01, 0, 0},
1232         {0x5503, 0x0f, 0, 0},
1233         {0x8000, 0x00, 0, 0},
1234         {0x8001, 0x00, 0, 0},
1235         {0x8002, 0x00, 0, 0},
1236         {0x8003, 0x00, 0, 0},
1237         {0x8004, 0x00, 0, 0},
1238         {0x8005, 0x00, 0, 0},
1239         {0x8006, 0x00, 0, 0},
1240         {0x8007, 0x00, 0, 0},
1241         {0x8008, 0x00, 0, 0},
1242         {0x3638, 0x00, 0, 0},
1243 };
1244
1245 static const struct reg_value ov4689_setting_4M_2688_1520[] = {
1246         //@@ 0 10 RES_2688x1520_default(60fps)
1247         //102 2630 960
1248         {0x0103, 0x01, 0, 0},
1249         {0x3638, 0x00, 0, 0},
1250         {0x0300, 0x00, 0, 0},
1251         {0x0302, 0x22, 0, 0}, // 2a ;1008Mbps,23 ;; 840Mbps
1252         {0x0304, 0x03, 0, 0},
1253         {0x030b, 0x00, 0, 0},
1254         {0x030d, 0x1e, 0, 0},
1255         {0x030e, 0x04, 0, 0},
1256         {0x030f, 0x01, 0, 0},
1257         {0x0312, 0x01, 0, 0},
1258         {0x031e, 0x00, 0, 0},
1259         {0x3000, 0x20, 0, 0},
1260         {0x3002, 0x00, 0, 0},
1261         {0x3018, 0x32, 0, 0},
1262         {0x3019, 0x0C, 0, 0},
1263         {0x3020, 0x93, 0, 0},
1264         {0x3021, 0x03, 0, 0},
1265         {0x3022, 0x01, 0, 0},
1266         {0x3031, 0x0a, 0, 0},
1267         {0x303f, 0x0c, 0, 0},
1268         {0x3305, 0xf1, 0, 0},
1269         {0x3307, 0x04, 0, 0},
1270         {0x3309, 0x29, 0, 0},
1271         {0x3500, 0x00, 0, 0},
1272         {0x3501, 0x60, 0, 0},
1273         {0x3502, 0x00, 0, 0},
1274         {0x3503, 0x04, 0, 0},
1275         {0x3504, 0x00, 0, 0},
1276         {0x3505, 0x00, 0, 0},
1277         {0x3506, 0x00, 0, 0},
1278         {0x3507, 0x00, 0, 0},
1279         {0x3508, 0x00, 0, 0},
1280         {0x3509, 0x80, 0, 0},
1281         {0x350a, 0x00, 0, 0},
1282         {0x350b, 0x00, 0, 0},
1283         {0x350c, 0x00, 0, 0},
1284         {0x350d, 0x00, 0, 0},
1285         {0x350e, 0x00, 0, 0},
1286         {0x350f, 0x80, 0, 0},
1287         {0x3510, 0x00, 0, 0},
1288         {0x3511, 0x00, 0, 0},
1289         {0x3512, 0x00, 0, 0},
1290         {0x3513, 0x00, 0, 0},
1291         {0x3514, 0x00, 0, 0},
1292         {0x3515, 0x80, 0, 0},
1293         {0x3516, 0x00, 0, 0},
1294         {0x3517, 0x00, 0, 0},
1295         {0x3518, 0x00, 0, 0},
1296         {0x3519, 0x00, 0, 0},
1297         {0x351a, 0x00, 0, 0},
1298         {0x351b, 0x80, 0, 0},
1299         {0x351c, 0x00, 0, 0},
1300         {0x351d, 0x00, 0, 0},
1301         {0x351e, 0x00, 0, 0},
1302         {0x351f, 0x00, 0, 0},
1303         {0x3520, 0x00, 0, 0},
1304         {0x3521, 0x80, 0, 0},
1305         {0x3522, 0x08, 0, 0},
1306         {0x3524, 0x08, 0, 0},
1307         {0x3526, 0x08, 0, 0},
1308         {0x3528, 0x08, 0, 0},
1309         {0x352a, 0x08, 0, 0},
1310         {0x3602, 0x00, 0, 0},
1311         {0x3603, 0x40, 0, 0},
1312         {0x3604, 0x02, 0, 0},
1313         {0x3605, 0x00, 0, 0},
1314         {0x3606, 0x00, 0, 0},
1315         {0x3607, 0x00, 0, 0},
1316         {0x3609, 0x12, 0, 0},
1317         {0x360a, 0x40, 0, 0},
1318         {0x360c, 0x08, 0, 0},
1319         {0x360f, 0xe5, 0, 0},
1320         {0x3608, 0x8f, 0, 0},
1321         {0x3611, 0x00, 0, 0},
1322         {0x3613, 0xf7, 0, 0},
1323         {0x3616, 0x58, 0, 0},
1324         {0x3619, 0x99, 0, 0},
1325         {0x361b, 0x60, 0, 0},
1326         {0x361c, 0x7a, 0, 0},
1327         {0x361e, 0x79, 0, 0},
1328         {0x361f, 0x02, 0, 0},
1329         {0x3632, 0x00, 0, 0},
1330         {0x3633, 0x10, 0, 0},
1331         {0x3634, 0x10, 0, 0},
1332         {0x3635, 0x10, 0, 0},
1333         {0x3636, 0x15, 0, 0},
1334         {0x3646, 0x86, 0, 0},
1335         {0x364a, 0x0b, 0, 0},
1336         {0x3700, 0x17, 0, 0},
1337         {0x3701, 0x22, 0, 0},
1338         {0x3703, 0x10, 0, 0},
1339         {0x370a, 0x37, 0, 0},
1340         {0x3705, 0x00, 0, 0},
1341         {0x3706, 0x63, 0, 0},
1342         {0x3709, 0x3c, 0, 0},
1343         {0x370b, 0x01, 0, 0},
1344         {0x370c, 0x30, 0, 0},
1345         {0x3710, 0x24, 0, 0},
1346         {0x3711, 0x0c, 0, 0},
1347         {0x3716, 0x00, 0, 0},
1348         {0x3720, 0x28, 0, 0},
1349         {0x3729, 0x7b, 0, 0},
1350         {0x372a, 0x84, 0, 0},
1351         {0x372b, 0xbd, 0, 0},
1352         {0x372c, 0xbc, 0, 0},
1353         {0x372e, 0x52, 0, 0},
1354         {0x373c, 0x0e, 0, 0},
1355         {0x373e, 0x33, 0, 0},
1356         {0x3743, 0x10, 0, 0},
1357         {0x3744, 0x88, 0, 0},
1358         {0x3745, 0xc0, 0, 0},
1359         {0x374a, 0x43, 0, 0},
1360         {0x374c, 0x00, 0, 0},
1361         {0x374e, 0x23, 0, 0},
1362         {0x3751, 0x7b, 0, 0},
1363         {0x3752, 0x84, 0, 0},
1364         {0x3753, 0xbd, 0, 0},
1365         {0x3754, 0xbc, 0, 0},
1366         {0x3756, 0x52, 0, 0},
1367         {0x375c, 0x00, 0, 0},
1368         {0x3760, 0x00, 0, 0},
1369         {0x3761, 0x00, 0, 0},
1370         {0x3762, 0x00, 0, 0},
1371         {0x3763, 0x00, 0, 0},
1372         {0x3764, 0x00, 0, 0},
1373         {0x3767, 0x04, 0, 0},
1374         {0x3768, 0x04, 0, 0},
1375         {0x3769, 0x08, 0, 0},
1376         {0x376a, 0x08, 0, 0},
1377         {0x376b, 0x20, 0, 0},
1378         {0x376c, 0x00, 0, 0},
1379         {0x376d, 0x00, 0, 0},
1380         {0x376e, 0x00, 0, 0},
1381         {0x3773, 0x00, 0, 0},
1382         {0x3774, 0x51, 0, 0},
1383         {0x3776, 0xbd, 0, 0},
1384         {0x3777, 0xbd, 0, 0},
1385         {0x3781, 0x18, 0, 0},
1386         {0x3783, 0x25, 0, 0},
1387         {0x3798, 0x1b, 0, 0},
1388         {0x3800, 0x00, 0, 0},
1389         {0x3801, 0x08, 0, 0},
1390         {0x3802, 0x00, 0, 0},
1391         {0x3803, 0x04, 0, 0},
1392         {0x3804, 0x0a, 0, 0},
1393         {0x3805, 0x97, 0, 0},
1394         {0x3806, 0x05, 0, 0},
1395         {0x3807, 0xfb, 0, 0},
1396         {0x3808, 0x0a, 0, 0},
1397         {0x3809, 0x80, 0, 0},
1398         {0x380a, 0x05, 0, 0},
1399         {0x380b, 0xf0, 0, 0},
1400         {0x380c, 0x03, 0, 0},
1401         {0x380d, 0x5c, 0, 0},
1402         {0x380e, 0x06, 0, 0},
1403         {0x380f, 0x12, 0, 0},
1404         {0x3810, 0x00, 0, 0},
1405         {0x3811, 0x08, 0, 0},
1406         {0x3812, 0x00, 0, 0},
1407         {0x3813, 0x04, 0, 0},
1408         {0x3814, 0x01, 0, 0},
1409         {0x3815, 0x01, 0, 0},
1410         {0x3819, 0x01, 0, 0},
1411         {0x3820, 0x00, 0, 0},
1412         {0x3821, 0x06, 0, 0},
1413         {0x3829, 0x00, 0, 0},
1414         {0x382a, 0x01, 0, 0},
1415         {0x382b, 0x01, 0, 0},
1416         {0x382d, 0x7f, 0, 0},
1417         {0x3830, 0x04, 0, 0},
1418         {0x3836, 0x01, 0, 0},
1419         {0x3837, 0x00, 0, 0},
1420         {0x3841, 0x02, 0, 0},
1421         {0x3846, 0x08, 0, 0},
1422         {0x3847, 0x07, 0, 0},
1423         {0x3d85, 0x36, 0, 0},
1424         {0x3d8c, 0x71, 0, 0},
1425         {0x3d8d, 0xcb, 0, 0},
1426         {0x3f0a, 0x00, 0, 0},
1427         {0x4000, 0x71, 0, 0},
1428         {0x4001, 0x40, 0, 0},
1429         {0x4002, 0x04, 0, 0},
1430         {0x4003, 0x14, 0, 0},
1431         {0x400e, 0x00, 0, 0},
1432         {0x4011, 0x00, 0, 0},
1433         {0x401a, 0x00, 0, 0},
1434         {0x401b, 0x00, 0, 0},
1435         {0x401c, 0x00, 0, 0},
1436         {0x401d, 0x00, 0, 0},
1437         {0x401f, 0x00, 0, 0},
1438         {0x4020, 0x00, 0, 0},
1439         {0x4021, 0x10, 0, 0},
1440         {0x4022, 0x07, 0, 0},
1441         {0x4023, 0xcf, 0, 0},
1442         {0x4024, 0x09, 0, 0},
1443         {0x4025, 0x60, 0, 0},
1444         {0x4026, 0x09, 0, 0},
1445         {0x4027, 0x6f, 0, 0},
1446         {0x4028, 0x00, 0, 0},
1447         {0x4029, 0x02, 0, 0},
1448         {0x402a, 0x06, 0, 0},
1449         {0x402b, 0x04, 0, 0},
1450         {0x402c, 0x02, 0, 0},
1451         {0x402d, 0x02, 0, 0},
1452         {0x402e, 0x0e, 0, 0},
1453         {0x402f, 0x04, 0, 0},
1454         {0x4302, 0xff, 0, 0},
1455         {0x4303, 0xff, 0, 0},
1456         {0x4304, 0x00, 0, 0},
1457         {0x4305, 0x00, 0, 0},
1458         {0x4306, 0x00, 0, 0},
1459         {0x4308, 0x02, 0, 0},
1460         {0x4500, 0x6c, 0, 0},
1461         {0x4501, 0xc4, 0, 0},
1462         {0x4502, 0x40, 0, 0},
1463         {0x4503, 0x01, 0, 0},
1464         {0x4601, 0x04, 0, 0},
1465         {0x4800, 0x04, 0, 0},
1466         {0x4813, 0x08, 0, 0},
1467         {0x481f, 0x40, 0, 0},
1468         {0x4829, 0x78, 0, 0},
1469         {0x4837, 0x14, 0, 0}, // 10
1470         {0x4b00, 0x2a, 0, 0},
1471         {0x4b0d, 0x00, 0, 0},
1472         {0x4d00, 0x04, 0, 0},
1473         {0x4d01, 0x42, 0, 0},
1474         {0x4d02, 0xd1, 0, 0},
1475         {0x4d03, 0x93, 0, 0},
1476         {0x4d04, 0xf5, 0, 0},
1477         {0x4d05, 0xc1, 0, 0},
1478         {0x5000, 0xf3, 0, 0},
1479         {0x5001, 0x11, 0, 0},
1480         {0x5004, 0x00, 0, 0},
1481         {0x500a, 0x00, 0, 0},
1482         {0x500b, 0x00, 0, 0},
1483         {0x5032, 0x00, 0, 0},
1484         {0x5040, 0x00, 0, 0},
1485         {0x5050, 0x0c, 0, 0},
1486         {0x5500, 0x00, 0, 0},
1487         {0x5501, 0x10, 0, 0},
1488         {0x5502, 0x01, 0, 0},
1489         {0x5503, 0x0f, 0, 0},
1490         {0x8000, 0x00, 0, 0},
1491         {0x8001, 0x00, 0, 0},
1492         {0x8002, 0x00, 0, 0},
1493         {0x8003, 0x00, 0, 0},
1494         {0x8004, 0x00, 0, 0},
1495         {0x8005, 0x00, 0, 0},
1496         {0x8006, 0x00, 0, 0},
1497         {0x8007, 0x00, 0, 0},
1498         {0x8008, 0x00, 0, 0},
1499         {0x3638, 0x00, 0, 0},
1500 //      {0x0100, 0x01, 0, 0},
1501
1502 //      {0x0100, 0x00, 0, 0},
1503         {0x380c, 0x0A, 0, 0}, // 05
1504         {0x380d, 0x0A, 0, 0}, // 10
1505         {0x380e, 0x06, 0, 0},
1506         {0x380f, 0x12, 0, 0},
1507 //      {0x0100, 0x01, 0, 0},
1508         {0x3105, 0x31, 0, 0},
1509         {0x301a, 0xf9, 0, 0},
1510         {0x3508, 0x07, 0, 0},
1511         {0x484b, 0x05, 0, 0},
1512         {0x4805, 0x03, 0, 0},
1513         {0x3601, 0x01, 0, 0},
1514         {0x3745, 0xc0, 0, 0},
1515         {0x3798, 0x1b, 0, 0},
1516 //      {0x0100, 0x01, 0, 0},
1517         {0xffff, 0x0a, 0, 0},
1518         {0x3105, 0x11, 0, 0},
1519         {0x301a, 0xf1, 0, 0},
1520         {0x4805, 0x00, 0, 0},
1521         {0x301a, 0xf0, 0, 0},
1522         {0x3208, 0x00, 0, 0},
1523         {0x302a, 0x00, 0, 0},
1524         {0x302a, 0x00, 0, 0},
1525         {0x302a, 0x00, 0, 0},
1526         {0x302a, 0x00, 0, 0},
1527         {0x302a, 0x00, 0, 0},
1528         {0x3601, 0x00, 0, 0},
1529         {0x3638, 0x00, 0, 0},
1530         {0x3208, 0x10, 0, 0},
1531         {0x3208, 0xa0, 0, 0},
1532 };
1533
1534 /* power-on sensor init reg table */
1535 static const struct ov4689_mode_info ov4689_mode_init_data = {
1536
1537 };
1538
1539 static const struct ov4689_mode_info
1540 ov4689_mode_data[OV4689_NUM_MODES] = {
1541         // {OV4689_MODE_720P_1280_720, SUBSAMPLING,
1542         //  1280, 0x408, 720, 0x305,
1543         //  ov4689_setting_720P_1280_720,
1544         //  ARRAY_SIZE(ov4689_setting_720P_1280_720),
1545         //  OV4689_150_FPS},
1546         // {OV4689_MODE_1080P_1920_1080, SCALING,
1547         //  1920, 0x6e0, 1080, 0x470,
1548         //  ov4689_setting_1080P_1920_1080,
1549         //  ARRAY_SIZE(ov4689_setting_1080P_1920_1080),
1550         //  OV4689_60_FPS},
1551         // {OV4689_MODE_4M_2688_1520, SCALING,
1552         //  2688, 0xa0a, 1520, 0x612,
1553         //  ov4689_setting_4M_2688_1520,
1554         //  ARRAY_SIZE(ov4689_setting_4M_2688_1520),
1555         //  OV4689_60_FPS},
1556
1557         {OV4689_MODE_1080P_1920_1080, SCALING,
1558          1920, 0x6e0, 1080, 0x470,
1559          ov4689_init_setting_30fps_1080P,
1560          ARRAY_SIZE(ov4689_init_setting_30fps_1080P),
1561          OV4689_60_FPS},
1562 };
1563
1564 static int ov4689_write_reg(struct ov4689_dev *sensor, u16 reg, u8 val)
1565 {
1566         struct i2c_client *client = sensor->i2c_client;
1567         struct i2c_msg msg;
1568         u8 buf[3];
1569         int ret;
1570
1571         buf[0] = reg >> 8;
1572         buf[1] = reg & 0xff;
1573         buf[2] = val;
1574
1575         msg.addr = client->addr;
1576         msg.flags = client->flags;
1577         msg.buf = buf;
1578         msg.len = sizeof(buf);
1579
1580         ret = i2c_transfer(client->adapter, &msg, 1);
1581         if (ret < 0) {
1582                 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1583                         __func__, reg, val);
1584                 return ret;
1585         }
1586
1587         return 0;
1588 }
1589
1590 static int ov4689_read_reg(struct ov4689_dev *sensor, u16 reg, u8 *val)
1591 {
1592         struct i2c_client *client = sensor->i2c_client;
1593         struct i2c_msg msg[2];
1594         u8 buf[2];
1595         int ret;
1596
1597         buf[0] = reg >> 8;
1598         buf[1] = reg & 0xff;
1599
1600         msg[0].addr = client->addr;
1601         msg[0].flags = client->flags;
1602         msg[0].buf = buf;
1603         msg[0].len = sizeof(buf);
1604
1605         msg[1].addr = client->addr;
1606         msg[1].flags = client->flags | I2C_M_RD;
1607         msg[1].buf = buf;
1608         msg[1].len = 1;
1609
1610         ret = i2c_transfer(client->adapter, msg, 2);
1611         if (ret < 0) {
1612                 dev_err(&client->dev, "%s: error: reg=%x\n",
1613                         __func__, reg);
1614                 return ret;
1615         }
1616
1617         *val = buf[0];
1618         return 0;
1619 }
1620
1621 static int ov4689_read_reg16(struct ov4689_dev *sensor, u16 reg, u16 *val)
1622 {
1623         u8 hi, lo;
1624         int ret;
1625
1626         ret = ov4689_read_reg(sensor, reg, &hi);
1627         if (ret)
1628                 return ret;
1629         ret = ov4689_read_reg(sensor, reg + 1, &lo);
1630         if (ret)
1631                 return ret;
1632
1633         *val = ((u16)hi << 8) | (u16)lo;
1634         return 0;
1635 }
1636
1637 static int ov4689_write_reg16(struct ov4689_dev *sensor, u16 reg, u16 val)
1638 {
1639         int ret;
1640
1641         ret = ov4689_write_reg(sensor, reg, val >> 8);
1642         if (ret)
1643                 return ret;
1644
1645         return ov4689_write_reg(sensor, reg + 1, val & 0xff);
1646 }
1647
1648 static int ov4689_mod_reg(struct ov4689_dev *sensor, u16 reg,
1649                         u8 mask, u8 val)
1650 {
1651         u8 readval;
1652         int ret;
1653
1654         ret = ov4689_read_reg(sensor, reg, &readval);
1655         if (ret)
1656                 return ret;
1657
1658         readval &= ~mask;
1659         val &= mask;
1660         val |= readval;
1661
1662         return ov4689_write_reg(sensor, reg, val);
1663 }
1664
1665 static int ov4689_set_timings(struct ov4689_dev *sensor,
1666                         const struct ov4689_mode_info *mode)
1667 {
1668         return 0;
1669 }
1670
1671 static int ov4689_load_regs(struct ov4689_dev *sensor,
1672                         const struct ov4689_mode_info *mode)
1673 {
1674         const struct reg_value *regs = mode->reg_data;
1675         unsigned int i;
1676         u32 delay_ms;
1677         u16 reg_addr;
1678         u8 mask, val;
1679         int ret = 0;
1680
1681         st_info(ST_SENSOR, "%s, mode = 0x%x\n", __func__, mode->id);
1682         for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1683                 delay_ms = regs->delay_ms;
1684                 reg_addr = regs->reg_addr;
1685                 val = regs->val;
1686                 mask = regs->mask;
1687
1688                 if (mask)
1689                         ret = ov4689_mod_reg(sensor, reg_addr, mask, val);
1690                 else
1691                         ret = ov4689_write_reg(sensor, reg_addr, val);
1692                 if (ret)
1693                         break;
1694
1695                 if (delay_ms)
1696                         usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1697         }
1698
1699         return ov4689_set_timings(sensor, mode);
1700 }
1701
1702 #ifdef UNUSED_CODE
1703 static int ov4689_get_exposure(struct ov4689_dev *sensor)
1704 {
1705         int exp, ret;
1706         u8 temp;
1707
1708         ret = ov4689_read_reg(sensor, OV4689_REG_EXPOSURE_HI, &temp);
1709         if (ret)
1710                 return ret;
1711         exp = ((int)temp & 0x0f) << 16;
1712         ret = ov4689_read_reg(sensor, OV4689_REG_EXPOSURE_MED, &temp);
1713         if (ret)
1714                 return ret;
1715         exp |= ((int)temp << 8);
1716         ret = ov4689_read_reg(sensor, OV4689_REG_EXPOSURE_LO, &temp);
1717         if (ret)
1718                 return ret;
1719         exp |= (int)temp;
1720
1721         return exp >> 4;
1722 }
1723 #endif
1724
1725 static int ov4689_set_exposure(struct ov4689_dev *sensor, u32 exposure)
1726 {
1727         int ret;
1728
1729         st_info(ST_SENSOR, "%s, exposure = 0x%x\n", __func__, exposure);
1730         exposure <<= 4;
1731
1732         ret = ov4689_write_reg(sensor,
1733                         OV4689_REG_EXPOSURE_LO,
1734                         exposure & 0xff);
1735         if (ret)
1736                 return ret;
1737         ret = ov4689_write_reg(sensor,
1738                         OV4689_REG_EXPOSURE_MED,
1739                         (exposure >> 8) & 0xff);
1740         if (ret)
1741                 return ret;
1742         return ov4689_write_reg(sensor,
1743                                 OV4689_REG_EXPOSURE_HI,
1744                                 (exposure >> 16) & 0x0f);
1745 }
1746
1747 static int ov4689_get_gain(struct ov4689_dev *sensor)
1748 {
1749         u32 gain = 0;
1750         u8 val;
1751
1752         ov4689_read_reg(sensor, OV4689_REG_GAIN_H, &val);
1753         gain = (val & 0x3) << 16;
1754         ov4689_read_reg(sensor, OV4689_REG_GAIN_M, &val);
1755         gain |= val << 8;
1756         ov4689_read_reg(sensor, OV4689_REG_GAIN_L, &val);
1757         gain |= val;
1758
1759         return gain;
1760 }
1761
1762 static int ov4689_set_gain(struct ov4689_dev *sensor, int gain)
1763 {
1764         ov4689_write_reg(sensor, OV4689_REG_GAIN_H,
1765                                 (gain >> 16) & 0x3);
1766         ov4689_write_reg(sensor, OV4689_REG_GAIN_M,
1767                                 (gain >> 8) & 0xff);
1768         ov4689_write_reg(sensor, OV4689_REG_GAIN_L,
1769                                 gain & 0xff);
1770         return 0;
1771 }
1772
1773 static int ov4689_set_stream_mipi(struct ov4689_dev *sensor, bool on)
1774 {
1775         return 0;
1776 }
1777
1778 #ifdef UNUSED_CODE
1779 static int ov4689_get_sysclk(struct ov4689_dev *sensor)
1780 {
1781         return 0;
1782 }
1783
1784 static int ov4689_set_night_mode(struct ov4689_dev *sensor)
1785 {
1786         return 0;
1787 }
1788
1789 static int ov4689_get_hts(struct ov4689_dev *sensor)
1790 {
1791         /* read HTS from register settings */
1792         u16 hts;
1793         int ret;
1794
1795         ret = ov4689_read_reg16(sensor, OV4689_REG_TIMING_HTS, &hts);
1796         if (ret)
1797                 return ret;
1798         return hts;
1799 }
1800 #endif
1801
1802 static int ov4689_get_vts(struct ov4689_dev *sensor)
1803 {
1804         u16 vts;
1805         int ret;
1806
1807         ret = ov4689_read_reg16(sensor, OV4689_REG_TIMING_VTS, &vts);
1808         if (ret)
1809                 return ret;
1810         return vts;
1811 }
1812
1813 #ifdef UNUSED_CODE
1814 static int ov4689_set_vts(struct ov4689_dev *sensor, int vts)
1815 {
1816         return ov4689_write_reg16(sensor, OV4689_REG_TIMING_VTS, vts);
1817 }
1818
1819 static int ov4689_get_light_freq(struct ov4689_dev *sensor)
1820 {
1821         return 0;
1822 }
1823
1824 static int ov4689_set_bandingfilter(struct ov4689_dev *sensor)
1825 {
1826         return 0;
1827 }
1828
1829 static int ov4689_set_ae_target(struct ov4689_dev *sensor, int target)
1830 {
1831         return 0;
1832 }
1833
1834 static int ov4689_get_binning(struct ov4689_dev *sensor)
1835 {
1836         return 0;
1837 }
1838
1839 static int ov4689_set_binning(struct ov4689_dev *sensor, bool enable)
1840 {
1841         return 0;
1842 }
1843 #endif
1844
1845 static const struct ov4689_mode_info *
1846 ov4689_find_mode(struct ov4689_dev *sensor, enum ov4689_frame_rate fr,
1847                 int width, int height, bool nearest)
1848 {
1849         const struct ov4689_mode_info *mode;
1850
1851         mode = v4l2_find_nearest_size(ov4689_mode_data,
1852                                 ARRAY_SIZE(ov4689_mode_data),
1853                                 hact, vact,
1854                                 width, height);
1855
1856         if (!mode ||
1857                 (!nearest && (mode->hact != width || mode->vact != height)))
1858                 return NULL;
1859
1860         /* Check to see if the current mode exceeds the max frame rate */
1861         if (ov4689_framerates[fr] > ov4689_framerates[mode->max_fps])
1862                 return NULL;
1863
1864         return mode;
1865 }
1866
1867 static u64 ov4689_calc_pixel_rate(struct ov4689_dev *sensor)
1868 {
1869         u64 rate;
1870
1871         rate = sensor->current_mode->vact * sensor->current_mode->hact;
1872         rate *= ov4689_framerates[sensor->current_fr];
1873
1874         return rate;
1875 }
1876
1877 /*
1878  * After trying the various combinations, reading various
1879  * documentations spread around the net, and from the various
1880  * feedback, the clock tree is probably as follows:
1881  *
1882  *   +--------------+
1883  *   |  Ext. Clock  |
1884  *   +-+------------+
1885  *     |  +----------+
1886  *     +->|   PLL1   | - reg 0x030a, bit0 for the pre-dividerp
1887  *        +-+--------+ - reg 0x0300, bits 0-2 for the pre-divider
1888  *        +-+--------+ - reg 0x0301~0x0302, for the multiplier
1889  *          |  +--------------+
1890  *          +->| MIPI Divider |  - reg 0x0303, bits 0-3 for the pre-divider
1891  *               | +---------> MIPI PHY CLK
1892  *               |    +-----+
1893  *               | +->| PLL1_DIV_MIPI | - reg 0x0304, bits 0-1 for the divider
1894  *                 |    +----------------> PCLK
1895  *               |    +-----+
1896  *
1897  *   +--------------+
1898  *   |  Ext. Clock  |
1899  *   +-+------------+
1900  *     |  +----------+
1901  *     +->|   PLL2  | - reg 0x0311, bit0 for the pre-dividerp
1902  *        +-+--------+ - reg 0x030b, bits 0-2 for the pre-divider
1903  *        +-+--------+ - reg 0x030c~0x030d, for the multiplier
1904  *          |  +--------------+
1905  *          +->| SCLK Divider |  - reg 0x030F, bits 0-3 for the pre-divider
1906  *               +-+--------+    - reg 0x030E, bits 0-2 for the divider
1907  *               |    +---------> SCLK
1908  *
1909  *          |       +-----+
1910  *          +->| DAC Divider | - reg 0x0312, bits 0-3 for the divider
1911  *                    |    +----------------> DACCLK
1912  **
1913  */
1914
1915 /*
1916  * ov4689_set_mipi_pclk() - Calculate the clock tree configuration values
1917  *                      for the MIPI CSI-2 output.
1918  *
1919  * @rate: The requested bandwidth per lane in bytes per second.
1920  *      'Bandwidth Per Lane' is calculated as:
1921  *      bpl = HTOT * VTOT * FPS * bpp / num_lanes;
1922  *
1923  * This function use the requested bandwidth to calculate:
1924  *
1925  * - mipi_pclk   = bpl / 2; ( / 2 is for CSI-2 DDR)
1926  * - mipi_phy_clk   = mipi_pclk * PLL1_DIV_MIPI;
1927  *
1928  * with these fixed parameters:
1929  *      PLL1_PREDIVP    = 1;
1930  *      PLL1_PREDIV     = 1; (MIPI_BIT_MODE == 8 ? 2 : 2,5);
1931  *      PLL1_DIVM       = 1;
1932  *      PLL1_DIV_MIPI   = 4;
1933  *
1934  * FIXME: this have been tested with 10-bit raw and 2 lanes setup only.
1935  * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
1936  * above formula for setups with 1 lane or image formats with different bpp.
1937  *
1938  * FIXME: this deviates from the sensor manual documentation which is quite
1939  * thin on the MIPI clock tree generation part.
1940  */
1941
1942 #define PLL1_PREDIVP         1     // bypass
1943 #define PLL1_PREDIV          1     // bypass
1944 #define PLL1_DIVM            1  // bypass
1945 #define PLL1_DIV_MIPI        3  // div
1946 #define PLL1_DIV_MIPI_BASE   1  // div
1947
1948 #define PLL1_DIVSP    1   // no use
1949 #define PLL1_DIVS     1   // no use
1950
1951 #define PLL2_PREDIVP       0
1952 #define PLL2_PREDIV        0
1953 #define PLL2_DIVSP       1
1954 #define PLL2_DIVS        4
1955 #define PLL2_DIVDAC      1
1956
1957 #define OV4689_PLL1_PREDIVP         0x030a   // bits[0]
1958 #define OV4689_PLL1_PREDIV          0x0300   // bits[2:0]
1959 #define OV4689_PLL1_MULTIPLIER      0x0301   // bits[9:8]  0x0302 bits[7:0]
1960 #define OV4689_PLL1_DIVM            0x0303   // bits[3:0]
1961 #define OV4689_PLL1_DIV_MIPI        0x0304   // bits[1:0]
1962
1963 #define OV4689_PLL1_DIVSP           0x0305   //bits[1:0]
1964 #define OV4689_PLL1_DIVS            0x0306   // bits[0]
1965
1966 #define OV4689_PLL2_PREDIVP         0x0311   // bits[0]
1967 #define OV4689_PLL2_PREDIV          0x030b   // bits[2:0]
1968 #define OV4689_PLL2_MULTIPLIER      0x030c   // bits[9:8]   0x030d bits[7:0]
1969 #define OV4689_PLL2_DIVSP           0x030f  // bits[3:0]
1970 #define OV4689_PLL2_DIVS            0x030e  // bits[2:0]
1971 #define OV4689_PLL2_DIVDAC          0x0312  // bits[3:0]
1972
1973 #define OV4689_TIMING_HTS           0x380c
1974
1975 static int ov4689_set_mipi_pclk(struct ov4689_dev *sensor,
1976                                 unsigned long rate)
1977 {
1978         const struct ov4689_mode_info *mode = sensor->current_mode;
1979         //const struct ov4689_mode_info *orig_mode = sensor->last_mode;
1980         u8 val;
1981         int ret = 0;
1982         int fps = ov4689_framerates[sensor->current_fr];
1983         u16 htot, val16;
1984
1985         htot = mode->htot * ov4689_framerates[mode->max_fps] / fps;
1986
1987         ret = ov4689_write_reg16(sensor, OV4689_TIMING_HTS, htot);
1988
1989         ret = ov4689_read_reg(sensor, OV4689_TIMING_HTS, &val);
1990         val16 = val << 8;
1991         ret = ov4689_read_reg(sensor, OV4689_TIMING_HTS + 1, &val);
1992         val16 |= val;
1993
1994         st_info(ST_SENSOR, "fps = %d, max_fps = %d\n", fps, mode->max_fps);
1995         st_info(ST_SENSOR, "mode->htot = 0x%x, htot = 0x%x\n", mode->htot,
1996                         htot);
1997         st_info(ST_SENSOR, "reg: 0x%x = 0x%x\n", OV4689_TIMING_HTS, val16);
1998
1999         return 0;
2000 }
2001
2002 /*
2003  * if sensor changes inside scaling or subsampling
2004  * change mode directly
2005  */
2006 static int ov4689_set_mode_direct(struct ov4689_dev *sensor,
2007                                 const struct ov4689_mode_info *mode)
2008 {
2009         if (!mode->reg_data)
2010                 return -EINVAL;
2011
2012         /* Write capture setting */
2013         return ov4689_load_regs(sensor, mode);
2014 }
2015
2016 static int ov4689_set_mode(struct ov4689_dev *sensor)
2017 {
2018         const struct ov4689_mode_info *mode = sensor->current_mode;
2019         //const struct ov4689_mode_info *orig_mode = sensor->last_mode;
2020
2021         int ret = 0;
2022
2023         ret = ov4689_set_mode_direct(sensor, mode);
2024         if (ret < 0)
2025                 return ret;
2026
2027         /*
2028          * we support have 10 bits raw RGB(mipi)
2029          */
2030         if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2031                 ret = ov4689_set_mipi_pclk(sensor, 0);
2032
2033         if (ret < 0)
2034                 return 0;
2035
2036         sensor->pending_mode_change = false;
2037         sensor->last_mode = mode;
2038         return 0;
2039 }
2040
2041 /* restore the last set video mode after chip power-on */
2042 static int ov4689_restore_mode(struct ov4689_dev *sensor)
2043 {
2044         int ret;
2045
2046         /* first load the initial register values */
2047         ret = ov4689_load_regs(sensor, &ov4689_mode_init_data);
2048         if (ret < 0)
2049                 return ret;
2050         sensor->last_mode = &ov4689_mode_init_data;
2051
2052         /* now restore the last capture mode */
2053         ret = ov4689_set_mode(sensor);
2054         if (ret < 0)
2055                 return ret;
2056
2057         return ret;
2058 }
2059
2060 static void ov4689_power(struct ov4689_dev *sensor, bool enable)
2061 {
2062         if (!sensor->pwdn_gpio)
2063                 return;
2064         gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2065 }
2066
2067 static void ov4689_reset(struct ov4689_dev *sensor)
2068 {
2069         if (!sensor->reset_gpio)
2070                 return;
2071
2072         gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2073
2074         usleep_range(5000, 25000);
2075
2076         gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2077         usleep_range(1000, 2000);
2078 }
2079
2080 static int ov4689_set_power_on(struct ov4689_dev *sensor)
2081 {
2082         struct i2c_client *client = sensor->i2c_client;
2083         int ret;
2084
2085         ret = clk_prepare_enable(sensor->xclk);
2086         if (ret) {
2087                 dev_err(&client->dev, "%s: failed to enable clock\n",
2088                         __func__);
2089                 return ret;
2090         }
2091
2092         ret = regulator_bulk_enable(OV4689_NUM_SUPPLIES,
2093                                 sensor->supplies);
2094         if (ret) {
2095                 dev_err(&client->dev, "%s: failed to enable regulators\n",
2096                         __func__);
2097                 goto xclk_off;
2098         }
2099
2100         ov4689_reset(sensor);
2101         ov4689_power(sensor, true);
2102
2103         return 0;
2104
2105 xclk_off:
2106         clk_disable_unprepare(sensor->xclk);
2107         return ret;
2108 }
2109
2110 static void ov4689_set_power_off(struct ov4689_dev *sensor)
2111 {
2112         ov4689_power(sensor, false);
2113         regulator_bulk_disable(OV4689_NUM_SUPPLIES, sensor->supplies);
2114         clk_disable_unprepare(sensor->xclk);
2115 }
2116
2117 static int ov4689_set_power_mipi(struct ov4689_dev *sensor, bool on)
2118 {
2119         return 0;
2120 }
2121
2122 static int ov4689_set_power(struct ov4689_dev *sensor, bool on)
2123 {
2124         int ret = 0;
2125         u16 chip_id;
2126
2127         if (on) {
2128                 ret = ov4689_set_power_on(sensor);
2129                 if (ret)
2130                         return ret;
2131
2132                 ret = ov4689_read_reg16(sensor, OV4689_REG_CHIP_ID, &chip_id);
2133                 if (ret) {
2134                         dev_err(&sensor->i2c_client->dev, "%s: failed to read chip identifier\n",
2135                                 __func__);
2136                         ret = -ENODEV;
2137                         goto power_off;
2138                 }
2139
2140                 if (chip_id != OV4689_CHIP_ID) {
2141                         dev_err(&sensor->i2c_client->dev,
2142                                         "%s: wrong chip identifier, expected 0x%x, got 0x%x\n",
2143                                         __func__, OV4689_CHIP_ID, chip_id);
2144                         ret = -ENXIO;
2145                         goto power_off;
2146                 }
2147                 dev_err(&sensor->i2c_client->dev, "%s: chip identifier, got 0x%x\n",
2148                         __func__, chip_id);
2149
2150                 ret = ov4689_restore_mode(sensor);
2151                 if (ret)
2152                         goto power_off;
2153         }
2154
2155         if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2156                 ret = ov4689_set_power_mipi(sensor, on);
2157         if (ret)
2158                 goto power_off;
2159
2160         if (!on)
2161                 ov4689_set_power_off(sensor);
2162
2163         return 0;
2164
2165 power_off:
2166         ov4689_set_power_off(sensor);
2167         return ret;
2168 }
2169
2170 static int ov4689_s_power(struct v4l2_subdev *sd, int on)
2171 {
2172         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2173         int ret = 0;
2174
2175         mutex_lock(&sensor->lock);
2176
2177         /*
2178          * If the power count is modified from 0 to != 0 or from != 0 to 0,
2179          * update the power state.
2180          */
2181         if (sensor->power_count == !on) {
2182                 ret = ov4689_set_power(sensor, !!on);
2183                 if (ret)
2184                         goto out;
2185         }
2186
2187         /* Update the power count. */
2188         sensor->power_count += on ? 1 : -1;
2189         WARN_ON(sensor->power_count < 0);
2190 out:
2191         mutex_unlock(&sensor->lock);
2192
2193         if (on && !ret && sensor->power_count == 1) {
2194                 /* restore controls */
2195                 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2196         }
2197
2198         return ret;
2199 }
2200
2201 static int ov4689_try_frame_interval(struct ov4689_dev *sensor,
2202                                 struct v4l2_fract *fi,
2203                                 u32 width, u32 height)
2204 {
2205         const struct ov4689_mode_info *mode;
2206         enum ov4689_frame_rate rate = OV4689_15_FPS;
2207         int minfps, maxfps, best_fps, fps;
2208         int i;
2209
2210         minfps = ov4689_framerates[OV4689_15_FPS];
2211         maxfps = ov4689_framerates[OV4689_NUM_FRAMERATES - 1];
2212
2213         if (fi->numerator == 0) {
2214                 fi->denominator = maxfps;
2215                 fi->numerator = 1;
2216                 rate = OV4689_60_FPS;
2217                 goto find_mode;
2218         }
2219
2220         fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2221                         minfps, maxfps);
2222
2223         best_fps = minfps;
2224         for (i = 0; i < ARRAY_SIZE(ov4689_framerates); i++) {
2225                 int curr_fps = ov4689_framerates[i];
2226
2227                 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2228                         best_fps = curr_fps;
2229                         rate = i;
2230                 }
2231         }
2232         st_info(ST_SENSOR, "best_fps = %d, fps = %d\n", best_fps, fps);
2233
2234         fi->numerator = 1;
2235         fi->denominator = best_fps;
2236
2237 find_mode:
2238         mode = ov4689_find_mode(sensor, rate, width, height, false);
2239         return mode ? rate : -EINVAL;
2240 }
2241
2242 static int ov4689_enum_mbus_code(struct v4l2_subdev *sd,
2243                                 struct v4l2_subdev_state *state,
2244                                 struct v4l2_subdev_mbus_code_enum *code)
2245 {
2246         if (code->pad != 0)
2247                 return -EINVAL;
2248
2249         if (code->index)
2250                 return -EINVAL;
2251
2252         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
2253         return 0;
2254 }
2255
2256 static int ov4689_get_fmt(struct v4l2_subdev *sd,
2257                         struct v4l2_subdev_state *state,
2258                         struct v4l2_subdev_format *format)
2259 {
2260         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2261         struct v4l2_mbus_framefmt *fmt;
2262
2263         if (format->pad != 0)
2264                 return -EINVAL;
2265
2266         mutex_lock(&sensor->lock);
2267
2268         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2269                 fmt = v4l2_subdev_get_try_format(&sensor->sd, state,
2270                                                 format->pad);
2271         else
2272                 fmt = &sensor->fmt;
2273
2274         format->format = *fmt;
2275
2276         mutex_unlock(&sensor->lock);
2277
2278         return 0;
2279 }
2280
2281 static int ov4689_try_fmt_internal(struct v4l2_subdev *sd,
2282                                 struct v4l2_mbus_framefmt *fmt,
2283                                 enum ov4689_frame_rate fr,
2284                                 const struct ov4689_mode_info **new_mode)
2285 {
2286         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2287         const struct ov4689_mode_info *mode;
2288
2289         mode = ov4689_find_mode(sensor, fr, fmt->width, fmt->height, true);
2290         if (!mode)
2291                 return -EINVAL;
2292         fmt->width = mode->hact;
2293         fmt->height = mode->vact;
2294
2295         if (new_mode)
2296                 *new_mode = mode;
2297
2298         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
2299
2300         return 0;
2301 }
2302
2303 static int ov4689_set_fmt(struct v4l2_subdev *sd,
2304                         struct v4l2_subdev_state *state,
2305                         struct v4l2_subdev_format *format)
2306 {
2307         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2308         const struct ov4689_mode_info *new_mode;
2309         struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2310         struct v4l2_mbus_framefmt *fmt;
2311         int ret;
2312
2313         if (format->pad != 0)
2314                 return -EINVAL;
2315
2316         mutex_lock(&sensor->lock);
2317
2318         if (sensor->streaming) {
2319                 ret = -EBUSY;
2320                 goto out;
2321         }
2322
2323         ret = ov4689_try_fmt_internal(sd, mbus_fmt, 0, &new_mode);
2324         if (ret)
2325                 goto out;
2326
2327         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2328                 fmt = v4l2_subdev_get_try_format(sd, state, 0);
2329         else
2330                 fmt = &sensor->fmt;
2331
2332         *fmt = *mbus_fmt;
2333
2334         if (new_mode != sensor->current_mode) {
2335                 sensor->current_mode = new_mode;
2336                 sensor->pending_mode_change = true;
2337         }
2338         if (new_mode->max_fps < sensor->current_fr) {
2339                 sensor->current_fr = new_mode->max_fps;
2340                 sensor->frame_interval.numerator = 1;
2341                 sensor->frame_interval.denominator =
2342                         ov4689_framerates[sensor->current_fr];
2343                 sensor->current_mode = new_mode;
2344                 sensor->pending_mode_change = true;
2345         }
2346
2347         __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2348                                 ov4689_calc_pixel_rate(sensor));
2349 out:
2350         mutex_unlock(&sensor->lock);
2351         return ret;
2352 }
2353
2354 /*
2355  * Sensor Controls.
2356  */
2357
2358 static int ov4689_set_ctrl_hue(struct ov4689_dev *sensor, int value)
2359 {
2360         int ret = 0;
2361
2362         return ret;
2363 }
2364
2365 static int ov4689_set_ctrl_contrast(struct ov4689_dev *sensor, int value)
2366 {
2367         int ret = 0;
2368
2369         return ret;
2370 }
2371
2372 static int ov4689_set_ctrl_saturation(struct ov4689_dev *sensor, int value)
2373 {
2374         int ret  = 0;
2375
2376         return ret;
2377 }
2378
2379 static int ov4689_set_ctrl_white_balance(struct ov4689_dev *sensor, int awb)
2380 {
2381         struct ov4689_ctrls *ctrls = &sensor->ctrls;
2382         int ret = 0;
2383
2384         if (!awb && (ctrls->red_balance->is_new
2385                         || ctrls->blue_balance->is_new)) {
2386                 u16 red = (u16)ctrls->red_balance->val;
2387                 u16 blue = (u16)ctrls->blue_balance->val;
2388
2389                 st_info(ST_SENSOR, "red = 0x%x, blue = 0x%x\n", red, blue);
2390                 ret = ov4689_write_reg16(sensor, OV4689_REG_AWB_R_GAIN, red);
2391                 if (ret)
2392                         return ret;
2393                 ret = ov4689_write_reg16(sensor, OV4689_REG_AWB_B_GAIN, blue);
2394         }
2395         return ret;
2396 }
2397
2398 static int ov4689_set_ctrl_exposure(struct ov4689_dev *sensor,
2399                                 enum v4l2_exposure_auto_type auto_exposure)
2400 {
2401         struct ov4689_ctrls *ctrls = &sensor->ctrls;
2402         bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
2403         int ret = 0;
2404
2405         if (!auto_exp && ctrls->exposure->is_new) {
2406                 u16 max_exp = 0;
2407
2408                 ret = ov4689_read_reg16(sensor, OV4689_REG_V_OUTPUT_SIZE,
2409                                         &max_exp);
2410
2411                 ret = ov4689_get_vts(sensor);
2412                 if (ret < 0)
2413                         return ret;
2414                 max_exp += ret;
2415                 ret = 0;
2416
2417                 st_info(ST_SENSOR, "%s, max_exp = 0x%x\n", __func__, max_exp);
2418                 if (ctrls->exposure->val < max_exp)
2419                         ret = ov4689_set_exposure(sensor, ctrls->exposure->val);
2420         }
2421
2422         return ret;
2423 }
2424
2425 static const s64 link_freq_menu_items[] = {
2426         OV4689_LINK_FREQ_500MHZ
2427 };
2428
2429 static const char * const test_pattern_menu[] = {
2430         "Disabled",
2431         "Color bars",
2432         "Color bars w/ rolling bar",
2433         "Color squares",
2434         "Color squares w/ rolling bar",
2435 };
2436
2437 #define OV4689_TEST_ENABLE              BIT(7)
2438 #define OV4689_TEST_ROLLING             BIT(6)  /* rolling horizontal bar */
2439 #define OV4689_TEST_TRANSPARENT         BIT(5)
2440 #define OV4689_TEST_SQUARE_BW           BIT(4)  /* black & white squares */
2441 #define OV4689_TEST_BAR_STANDARD        (0 << 2)
2442 #define OV4689_TEST_BAR_DARKER_1        (1 << 2)
2443 #define OV4689_TEST_BAR_DARKER_2        (2 << 2)
2444 #define OV4689_TEST_BAR_DARKER_3        (3 << 2)
2445 #define OV4689_TEST_BAR                 (0 << 0)
2446 #define OV4689_TEST_RANDOM              (1 << 0)
2447 #define OV4689_TEST_SQUARE              (2 << 0)
2448 #define OV4689_TEST_BLACK               (3 << 0)
2449
2450 static const u8 test_pattern_val[] = {
2451         0,
2452         OV4689_TEST_ENABLE | OV4689_TEST_BAR_STANDARD |
2453                 OV4689_TEST_BAR,
2454         OV4689_TEST_ENABLE | OV4689_TEST_ROLLING |
2455                 OV4689_TEST_BAR_DARKER_1 | OV4689_TEST_BAR,
2456         OV4689_TEST_ENABLE | OV4689_TEST_SQUARE,
2457         OV4689_TEST_ENABLE | OV4689_TEST_ROLLING | OV4689_TEST_SQUARE,
2458 };
2459
2460 static int ov4689_set_ctrl_test_pattern(struct ov4689_dev *sensor, int value)
2461 {
2462         return ov4689_write_reg(sensor, OV4689_REG_TEST_PATTERN,
2463                         test_pattern_val[value]);
2464 }
2465
2466 static int ov4689_set_ctrl_light_freq(struct ov4689_dev *sensor, int value)
2467 {
2468         return 0;
2469 }
2470
2471 static int ov4689_set_ctrl_hflip(struct ov4689_dev *sensor, int value)
2472 {
2473         /*
2474          * TIMING TC REG21:
2475          * - [2]:       Digital mirror
2476          * - [1]:       Array mirror
2477          */
2478         return ov4689_mod_reg(sensor, OV4689_REG_TIMING_TC_REG21,
2479                         BIT(2) | BIT(1),
2480                         (!(value ^ sensor->upside_down)) ?
2481                         (BIT(2) | BIT(1)) : 0);
2482 }
2483
2484 static int ov4689_set_ctrl_vflip(struct ov4689_dev *sensor, int value)
2485 {
2486         /*
2487          * TIMING TC REG20:
2488          * - [2]:       Digital vflip
2489          * - [1]:       Array vflip
2490          */
2491         return ov4689_mod_reg(sensor, OV4689_REG_TIMING_TC_REG20,
2492                                 BIT(2) | BIT(1),
2493                                 (value ^ sensor->upside_down) ?
2494                                 (BIT(2) | BIT(1)) : 0);
2495 }
2496
2497 static int ov4689_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2498 {
2499         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2500         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2501         int val;
2502
2503         /* v4l2_ctrl_lock() locks our own mutex */
2504
2505         switch (ctrl->id) {
2506         case V4L2_CID_ANALOGUE_GAIN:
2507                 val = ov4689_get_gain(sensor);
2508                 break;
2509         }
2510
2511         return 0;
2512 }
2513
2514 static int ov4689_s_ctrl(struct v4l2_ctrl *ctrl)
2515 {
2516         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2517         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2518         int ret;
2519
2520         /* v4l2_ctrl_lock() locks our own mutex */
2521
2522         /*
2523          * If the device is not powered up by the host driver do
2524          * not apply any controls to H/W at this time. Instead
2525          * the controls will be restored right after power-up.
2526          */
2527         if (sensor->power_count == 0)
2528                 return 0;
2529
2530         switch (ctrl->id) {
2531         case V4L2_CID_ANALOGUE_GAIN:
2532                 ret = ov4689_set_gain(sensor, ctrl->val);
2533                 break;
2534         case V4L2_CID_EXPOSURE:
2535                 ret = ov4689_set_ctrl_exposure(sensor, V4L2_EXPOSURE_MANUAL);
2536                 break;
2537         case V4L2_CID_AUTO_WHITE_BALANCE:
2538                 ret = ov4689_set_ctrl_white_balance(sensor, ctrl->val);
2539                 break;
2540         case V4L2_CID_HUE:
2541                 ret = ov4689_set_ctrl_hue(sensor, ctrl->val);
2542                 break;
2543         case V4L2_CID_CONTRAST:
2544                 ret = ov4689_set_ctrl_contrast(sensor, ctrl->val);
2545                 break;
2546         case V4L2_CID_SATURATION:
2547                 ret = ov4689_set_ctrl_saturation(sensor, ctrl->val);
2548                 break;
2549         case V4L2_CID_TEST_PATTERN:
2550                 ret = ov4689_set_ctrl_test_pattern(sensor, ctrl->val);
2551                 break;
2552         case V4L2_CID_POWER_LINE_FREQUENCY:
2553                 ret = ov4689_set_ctrl_light_freq(sensor, ctrl->val);
2554                 break;
2555         case V4L2_CID_HFLIP:
2556                 ret = ov4689_set_ctrl_hflip(sensor, ctrl->val);
2557                 break;
2558         case V4L2_CID_VFLIP:
2559                 ret = ov4689_set_ctrl_vflip(sensor, ctrl->val);
2560                 break;
2561         default:
2562                 ret = -EINVAL;
2563                 break;
2564         }
2565
2566         return ret;
2567 }
2568
2569 static const struct v4l2_ctrl_ops ov4689_ctrl_ops = {
2570         .g_volatile_ctrl = ov4689_g_volatile_ctrl,
2571         .s_ctrl = ov4689_s_ctrl,
2572 };
2573
2574 static int ov4689_init_controls(struct ov4689_dev *sensor)
2575 {
2576         const struct v4l2_ctrl_ops *ops = &ov4689_ctrl_ops;
2577         struct ov4689_ctrls *ctrls = &sensor->ctrls;
2578         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2579         int ret;
2580
2581         v4l2_ctrl_handler_init(hdl, 32);
2582
2583         /* we can use our own mutex for the ctrl lock */
2584         hdl->lock = &sensor->lock;
2585
2586         /* Clock related controls */
2587         ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
2588                                         0, INT_MAX, 1,
2589                                         ov4689_calc_pixel_rate(sensor));
2590
2591         /* Auto/manual white balance */
2592         ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2593                                         V4L2_CID_AUTO_WHITE_BALANCE,
2594                                         0, 1, 1, 0);
2595         ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2596                                                 0, 4095, 1, 1024);
2597         ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2598                                                 0, 4095, 1, 1024);
2599
2600         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2601                                         4, 0xfff8, 1, 0x4c00);
2602         ctrls->anal_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
2603                                         0x10, 0xfff8, 1, 0x0080);
2604         ctrls->test_pattern =
2605                 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2606                                         ARRAY_SIZE(test_pattern_menu) - 1,
2607                                         0, 0, test_pattern_menu);
2608         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2609                                         0, 1, 1, 0);
2610         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2611                                         0, 1, 1, 0);
2612         ctrls->light_freq =
2613                 v4l2_ctrl_new_std_menu(hdl, ops,
2614                                         V4L2_CID_POWER_LINE_FREQUENCY,
2615                                         V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
2616                                         V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
2617         ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops, V4L2_CID_LINK_FREQ,
2618                                         0, 0, link_freq_menu_items);
2619         if (hdl->error) {
2620                 ret = hdl->error;
2621                 goto free_ctrls;
2622         }
2623
2624         ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2625         ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2626         // ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2627         // ctrls->anal_gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2628
2629         v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2630
2631         sensor->sd.ctrl_handler = hdl;
2632         return 0;
2633
2634 free_ctrls:
2635         v4l2_ctrl_handler_free(hdl);
2636         return ret;
2637 }
2638
2639 static int ov4689_enum_frame_size(struct v4l2_subdev *sd,
2640                                 struct v4l2_subdev_state *state,
2641                                 struct v4l2_subdev_frame_size_enum *fse)
2642 {
2643         if (fse->pad != 0)
2644                 return -EINVAL;
2645         if (fse->index >= OV4689_NUM_MODES)
2646                 return -EINVAL;
2647
2648         fse->min_width =
2649                 ov4689_mode_data[fse->index].hact;
2650         fse->max_width = fse->min_width;
2651         fse->min_height =
2652                 ov4689_mode_data[fse->index].vact;
2653         fse->max_height = fse->min_height;
2654
2655         return 0;
2656 }
2657
2658 static int ov4689_enum_frame_interval(
2659         struct v4l2_subdev *sd,
2660         struct v4l2_subdev_state *state,
2661         struct v4l2_subdev_frame_interval_enum *fie)
2662 {
2663         //struct ov4689_dev *sensor = to_ov4689_dev(sd);
2664         struct v4l2_fract tpf;
2665         int i = 0;
2666
2667         if (fie->pad != 0)
2668                 return -EINVAL;
2669         if (fie->index >= OV4689_NUM_FRAMERATES)
2670                 return -EINVAL;
2671
2672         tpf.numerator = 1;
2673         tpf.denominator = ov4689_framerates[fie->index];
2674
2675         // ret = ov4689_try_frame_interval(sensor, &tpf,
2676         //              fie->width, fie->height);
2677         // if (ret < 0)
2678         //      return -EINVAL;
2679
2680         pr_debug("fie->width = %d, fie->height = %d\n", fie->width, fie->height);
2681         for (i = 0; i < OV4689_NUM_MODES; i++) {
2682                 if (fie->width == ov4689_mode_data[i].hact &&
2683                         fie->height == ov4689_mode_data[i].vact)
2684                         break;
2685         }
2686         if (i == OV4689_NUM_MODES)
2687                 return -ENOTTY;
2688
2689         fie->interval = tpf;
2690         fie->width = ov4689_mode_data[i].hact;
2691         fie->height = ov4689_mode_data[i].vact;
2692
2693         return 0;
2694 }
2695
2696 static int ov4689_g_frame_interval(struct v4l2_subdev *sd,
2697                                 struct v4l2_subdev_frame_interval *fi)
2698 {
2699         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2700
2701         mutex_lock(&sensor->lock);
2702         fi->interval = sensor->frame_interval;
2703         mutex_unlock(&sensor->lock);
2704
2705         return 0;
2706 }
2707
2708 static int ov4689_s_frame_interval(struct v4l2_subdev *sd,
2709                                 struct v4l2_subdev_frame_interval *fi)
2710 {
2711         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2712         const struct ov4689_mode_info *mode;
2713         int frame_rate, ret = 0;
2714
2715         if (fi->pad != 0)
2716                 return -EINVAL;
2717
2718         mutex_lock(&sensor->lock);
2719
2720         if (sensor->streaming) {
2721                 ret = -EBUSY;
2722                 goto out;
2723         }
2724
2725         mode = sensor->current_mode;
2726
2727         frame_rate = ov4689_try_frame_interval(sensor, &fi->interval,
2728                                         mode->hact, mode->vact);
2729         if (frame_rate < 0) {
2730                 /* Always return a valid frame interval value */
2731                 fi->interval = sensor->frame_interval;
2732                 goto out;
2733         }
2734
2735         mode = ov4689_find_mode(sensor, frame_rate, mode->hact,
2736                                 mode->vact, true);
2737         if (!mode) {
2738                 ret = -EINVAL;
2739                 goto out;
2740         }
2741
2742         if (mode != sensor->current_mode ||
2743                 frame_rate != sensor->current_fr) {
2744                 sensor->current_fr = frame_rate;
2745                 sensor->frame_interval = fi->interval;
2746                 sensor->current_mode = mode;
2747                 sensor->pending_mode_change = true;
2748
2749                 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2750                                         ov4689_calc_pixel_rate(sensor));
2751         }
2752 out:
2753         mutex_unlock(&sensor->lock);
2754         return ret;
2755 }
2756
2757 static int ov4689_stream_start(struct ov4689_dev *sensor, int enable)
2758 {
2759         return ov4689_write_reg(sensor, 0x100, enable);
2760 }
2761
2762 static int ov4689_s_stream(struct v4l2_subdev *sd, int enable)
2763 {
2764         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2765         int ret = 0;
2766
2767         mutex_lock(&sensor->lock);
2768
2769         if (sensor->streaming == !enable) {
2770                 if (enable && sensor->pending_mode_change) {
2771                         ret = ov4689_set_mode(sensor);
2772                         if (ret)
2773                                 goto out;
2774                 }
2775
2776                 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2777                         ret = ov4689_set_stream_mipi(sensor, enable);
2778
2779                 ret = ov4689_stream_start(sensor, enable);
2780
2781                 if (ret)
2782                         goto out;
2783         }
2784         sensor->streaming += enable ? 1 : -1;
2785         WARN_ON(sensor->streaming < 0);
2786 out:
2787         mutex_unlock(&sensor->lock);
2788         return ret;
2789 }
2790
2791 static const struct v4l2_subdev_core_ops ov4689_core_ops = {
2792         .s_power = ov4689_s_power,
2793         .log_status = v4l2_ctrl_subdev_log_status,
2794         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2795         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2796 };
2797
2798 static const struct v4l2_subdev_video_ops ov4689_video_ops = {
2799         .g_frame_interval = ov4689_g_frame_interval,
2800         .s_frame_interval = ov4689_s_frame_interval,
2801         .s_stream = ov4689_s_stream,
2802 };
2803
2804 static const struct v4l2_subdev_pad_ops ov4689_pad_ops = {
2805         .enum_mbus_code = ov4689_enum_mbus_code,
2806         .get_fmt = ov4689_get_fmt,
2807         .set_fmt = ov4689_set_fmt,
2808         .enum_frame_size = ov4689_enum_frame_size,
2809         .enum_frame_interval = ov4689_enum_frame_interval,
2810 };
2811
2812 static const struct v4l2_subdev_ops ov4689_subdev_ops = {
2813         .core = &ov4689_core_ops,
2814         .video = &ov4689_video_ops,
2815         .pad = &ov4689_pad_ops,
2816 };
2817
2818 static int ov4689_get_regulators(struct ov4689_dev *sensor)
2819 {
2820         int i;
2821
2822         for (i = 0; i < OV4689_NUM_SUPPLIES; i++)
2823                 sensor->supplies[i].supply = ov4689_supply_name[i];
2824
2825         return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2826                                         OV4689_NUM_SUPPLIES,
2827                                         sensor->supplies);
2828 }
2829
2830 static int ov4689_check_chip_id(struct ov4689_dev *sensor)
2831 {
2832         struct i2c_client *client = sensor->i2c_client;
2833         int ret = 0;
2834         u16 chip_id;
2835
2836         ret = ov4689_set_power_on(sensor);
2837         if (ret)
2838                 return ret;
2839
2840         ret = ov4689_read_reg16(sensor, OV4689_REG_CHIP_ID, &chip_id);
2841         if (ret) {
2842                 dev_err(&client->dev, "%s: failed to read chip identifier\n",
2843                         __func__);
2844                 goto power_off;
2845         }
2846
2847         if (chip_id != OV4689_CHIP_ID) {
2848                 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x%x,  got 0x%x\n",
2849                         __func__, OV4689_CHIP_ID, chip_id);
2850                 ret = -ENXIO;
2851         }
2852         dev_err(&client->dev, "%s: chip identifier, got 0x%x\n",
2853                 __func__, chip_id);
2854
2855 power_off:
2856         ov4689_set_power_off(sensor);
2857         return ret;
2858 }
2859
2860 static int ov4689_probe(struct i2c_client *client)
2861 {
2862         struct device *dev = &client->dev;
2863         struct fwnode_handle *endpoint;
2864         struct ov4689_dev *sensor;
2865         struct v4l2_mbus_framefmt *fmt;
2866         u32 rotation;
2867         int ret;
2868
2869         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2870         if (!sensor)
2871                 return -ENOMEM;
2872
2873         sensor->i2c_client = client;
2874
2875         fmt = &sensor->fmt;
2876         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
2877         fmt->colorspace = V4L2_COLORSPACE_SRGB;
2878         fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2879         fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2880         fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2881         fmt->width = 1920;
2882         fmt->height = 1080;
2883         fmt->field = V4L2_FIELD_NONE;
2884         sensor->frame_interval.numerator = 1;
2885         sensor->frame_interval.denominator = ov4689_framerates[OV4689_30_FPS];
2886         sensor->current_fr = OV4689_30_FPS;
2887         sensor->current_mode =
2888                 &ov4689_mode_data[OV4689_MODE_1080P_1920_1080];
2889         sensor->last_mode = sensor->current_mode;
2890
2891         sensor->ae_target = 52;
2892
2893         /* optional indication of physical rotation of sensor */
2894         ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
2895                                         &rotation);
2896         if (!ret) {
2897                 switch (rotation) {
2898                 case 180:
2899                         sensor->upside_down = true;
2900                         fallthrough;
2901                 case 0:
2902                         break;
2903                 default:
2904                         dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
2905                                 rotation);
2906                 }
2907         }
2908
2909         endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2910                                                 NULL);
2911         if (!endpoint) {
2912                 dev_err(dev, "endpoint node not found\n");
2913                 return -EINVAL;
2914         }
2915
2916         ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2917         fwnode_handle_put(endpoint);
2918         if (ret) {
2919                 dev_err(dev, "Could not parse endpoint\n");
2920                 return ret;
2921         }
2922
2923         if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
2924                 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
2925                 sensor->ep.bus_type != V4L2_MBUS_BT656) {
2926                 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
2927                 return -EINVAL;
2928         }
2929
2930         /* get system clock (xclk) */
2931         sensor->xclk = devm_clk_get(dev, "xclk");
2932         if (IS_ERR(sensor->xclk)) {
2933                 dev_err(dev, "failed to get xclk\n");
2934                 return PTR_ERR(sensor->xclk);
2935         }
2936
2937         sensor->xclk_freq = clk_get_rate(sensor->xclk);
2938         if (sensor->xclk_freq < OV4689_XCLK_MIN ||
2939                 sensor->xclk_freq > OV4689_XCLK_MAX) {
2940                 dev_err(dev, "xclk frequency out of range: %d Hz\n",
2941                         sensor->xclk_freq);
2942                 return -EINVAL;
2943         }
2944
2945         /* request optional power down pin */
2946         sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2947                                                 GPIOD_OUT_HIGH);
2948         if (IS_ERR(sensor->pwdn_gpio))
2949                 return PTR_ERR(sensor->pwdn_gpio);
2950
2951         /* request optional reset pin */
2952         sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2953                                                 GPIOD_OUT_HIGH);
2954         if (IS_ERR(sensor->reset_gpio))
2955                 return PTR_ERR(sensor->reset_gpio);
2956
2957         v4l2_i2c_subdev_init(&sensor->sd, client, &ov4689_subdev_ops);
2958
2959         sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2960                         V4L2_SUBDEV_FL_HAS_EVENTS;
2961         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
2962         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2963         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
2964         if (ret)
2965                 return ret;
2966
2967         ret = ov4689_get_regulators(sensor);
2968         if (ret)
2969                 return ret;
2970
2971         mutex_init(&sensor->lock);
2972
2973         ret = ov4689_check_chip_id(sensor);
2974         if (ret)
2975                 goto entity_cleanup;
2976
2977         ret = ov4689_init_controls(sensor);
2978         if (ret)
2979                 goto entity_cleanup;
2980
2981         ret = v4l2_async_register_subdev_sensor(&sensor->sd);
2982         if (ret)
2983                 goto free_ctrls;
2984
2985         return 0;
2986
2987 free_ctrls:
2988         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2989 entity_cleanup:
2990         media_entity_cleanup(&sensor->sd.entity);
2991         mutex_destroy(&sensor->lock);
2992         return ret;
2993 }
2994
2995 static int ov4689_remove(struct i2c_client *client)
2996 {
2997         struct v4l2_subdev *sd = i2c_get_clientdata(client);
2998         struct ov4689_dev *sensor = to_ov4689_dev(sd);
2999
3000         v4l2_async_unregister_subdev(&sensor->sd);
3001         media_entity_cleanup(&sensor->sd.entity);
3002         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3003         mutex_destroy(&sensor->lock);
3004
3005         return 0;
3006 }
3007
3008 static const struct i2c_device_id ov4689_id[] = {
3009         { "ov4689", 0 },
3010         {},
3011 };
3012 MODULE_DEVICE_TABLE(i2c, ov4689_id);
3013
3014 static const struct of_device_id ov4689_dt_ids[] = {
3015         { .compatible = "ovti,ov4689" },
3016         { /* sentinel */ }
3017 };
3018 MODULE_DEVICE_TABLE(of, ov4689_dt_ids);
3019
3020 static struct i2c_driver ov4689_i2c_driver = {
3021         .driver = {
3022                 .name  = "ov4689",
3023                 .of_match_table = ov4689_dt_ids,
3024         },
3025         .id_table = ov4689_id,
3026         .probe_new = ov4689_probe,
3027         .remove   = ov4689_remove,
3028 };
3029
3030 module_i2c_driver(ov4689_i2c_driver);
3031
3032 MODULE_DESCRIPTION("OV4689 MIPI Camera Subdev Driver");
3033 MODULE_LICENSE("GPL");