xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/gc2145.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GC2145 CMOS Image Sensor driver
4  *
5  *
6  * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
7  *
8  * V0.0X01.0X01 add poweron function.
9  * V0.0X01.0X02 fix mclk issue when probe multiple camera.
10  * V0.0X01.0X03 fix gc2145 exposure issues.
11  * V0.0X01.0X04 add enum_frame_interval function.
12  * V0.0X01.0X05 reduce rkisp1: CIF_ISP_PIC_SIZE_ERROR 0x00000001.
13  * V0.0X01.0X06 add quick stream on/off
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/i2c.h>
24 #include <linux/kernel.h>
25 #include <linux/media.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/of_graph.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/videodev2.h>
33 #include <linux/version.h>
34 #include <linux/rk-camera-module.h>
35 #include <media/media-entity.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-event.h>
40 #include <media/v4l2-fwnode.h>
41 #include <media/v4l2-image-sizes.h>
42 #include <media/v4l2-mediabus.h>
43 #include <media/v4l2-subdev.h>
44 
45 #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x6)
46 #define DRIVER_NAME "gc2145"
47 #define GC2145_PIXEL_RATE		(120 * 1000 * 1000)
48 
49 #define GC2145_EXPOSURE_CONTROL
50 
51 /*
52  * GC2145 register definitions
53  */
54 #define REG_SC_CHIP_ID_H		0xf0
55 #define REG_SC_CHIP_ID_L		0xf1
56 
57 #define REG_NULL			0xFFFF	/* Array end token */
58 
59 #define SENSOR_ID(_msb, _lsb)		((_msb) << 8 | (_lsb))
60 #define GC2145_ID			0x2145
61 
62 struct sensor_register {
63 	u16 addr;
64 	u8 value;
65 };
66 
67 struct gc2145_framesize {
68 	u16 width;
69 	u16 height;
70 	struct v4l2_fract max_fps;
71 	u16 max_exp_lines;
72 	const struct sensor_register *regs;
73 };
74 
75 struct gc2145_pll_ctrl {
76 	u8 ctrl1;
77 	u8 ctrl2;
78 	u8 ctrl3;
79 };
80 
81 struct gc2145_pixfmt {
82 	u32 code;
83 	/* Output format Register Value (REG_FORMAT_CTRL00) */
84 	struct sensor_register *format_ctrl_regs;
85 };
86 
87 struct pll_ctrl_reg {
88 	unsigned int div;
89 	unsigned char reg;
90 };
91 
92 static const char * const gc2145_supply_names[] = {
93 	"dovdd",	/* Digital I/O power */
94 	"avdd",		/* Analog power */
95 	"dvdd",		/* Digital core power */
96 };
97 
98 #define GC2145_NUM_SUPPLIES ARRAY_SIZE(gc2145_supply_names)
99 
100 struct gc2145 {
101 	struct v4l2_subdev sd;
102 	struct media_pad pad;
103 	struct v4l2_mbus_framefmt format;
104 	unsigned int fps;
105 	unsigned int xvclk_frequency;
106 	struct clk *xvclk;
107 	struct gpio_desc *power_gpio;
108 	struct gpio_desc *pwdn_gpio;
109 	struct gpio_desc *reset_gpio;
110 	struct regulator_bulk_data supplies[GC2145_NUM_SUPPLIES];
111 	struct mutex lock;
112 	struct i2c_client *client;
113 	struct v4l2_ctrl_handler ctrls;
114 	struct v4l2_ctrl *link_frequency;
115 	struct v4l2_fwnode_endpoint bus_cfg;
116 	const struct gc2145_framesize *frame_size;
117 	const struct gc2145_framesize *framesize_cfg;
118 	unsigned int cfg_num;
119 	int streaming;
120 	bool			power_on;
121 	u32 module_index;
122 	const char *module_facing;
123 	const char *module_name;
124 	const char *len_name;
125 };
126 
127 static const struct sensor_register gc2145_dvp_init_regs[] = {
128 	{0xfe, 0xf0},
129 	{0xfe, 0xf0},
130 	{0xfe, 0xf0},
131 	{0xfc, 0x06},
132 	{0xf6, 0x00},
133 	{0xf7, 0x1d},
134 	{0xf8, 0x84},
135 	{0xfa, 0x00},
136 	{0xf9, 0xfe},
137 	{0xf2, 0x00},
138 	/*ISP reg*/
139 	{0xfe, 0x00},
140 	{0x03, 0x04},
141 	{0x04, 0xe2},
142 	{0x09, 0x00},
143 	{0x0a, 0x00},
144 	{0x0b, 0x00},
145 	{0x0c, 0x00},
146 	{0x0d, 0x04},
147 	{0x0e, 0xc0},
148 	{0x0f, 0x06},
149 	{0x10, 0x52},
150 	{0x12, 0x2e},
151 	{0x17, 0x14},
152 	{0x18, 0x22},
153 	{0x19, 0x0e},
154 	{0x1a, 0x01},
155 	{0x1b, 0x4b},
156 	{0x1c, 0x07},
157 	{0x1d, 0x10},
158 	{0x1e, 0x88},
159 	{0x1f, 0x78},
160 	{0x20, 0x03},
161 	{0x21, 0x40},
162 	{0x22, 0xa0},
163 	{0x24, 0x3f},
164 	{0x25, 0x01},
165 	{0x26, 0x10},
166 	{0x2d, 0x60},
167 	{0x30, 0x01},
168 	{0x31, 0x90},
169 	{0x33, 0x06},
170 	{0x34, 0x01},
171 	{0xfe, 0x00},
172 	{0x80, 0x7f},
173 	{0x81, 0x26},
174 	{0x82, 0xfa},
175 	{0x83, 0x00},
176 	{0x84, 0x00},
177 	{0x86, 0x02},
178 	{0x88, 0x03},
179 	{0x89, 0x03},
180 	{0x85, 0x08},
181 	{0x8a, 0x00},
182 	{0x8b, 0x00},
183 	{0xb0, 0x55},
184 	{0xc3, 0x00},
185 	{0xc4, 0x80},
186 	{0xc5, 0x90},
187 	{0xc6, 0x3b},
188 	{0xc7, 0x46},
189 	{0xec, 0x06},
190 	{0xed, 0x04},
191 	{0xee, 0x60},
192 	{0xef, 0x90},
193 	{0xb6, 0x01},
194 	{0x90, 0x01},
195 	{0x91, 0x00},
196 	{0x92, 0x00},
197 	{0x93, 0x00},
198 	{0x94, 0x00},
199 	{0x95, 0x04},
200 	{0x96, 0xb0},
201 	{0x97, 0x06},
202 	{0x98, 0x40},
203 	/*BLK*/
204 	{0xfe, 0x00},
205 	{0x40, 0x42},
206 	{0x41, 0x00},
207 	{0x43, 0x5b},
208 	{0x5e, 0x00},
209 	{0x5f, 0x00},
210 	{0x60, 0x00},
211 	{0x61, 0x00},
212 	{0x62, 0x00},
213 	{0x63, 0x00},
214 	{0x64, 0x00},
215 	{0x65, 0x00},
216 	{0x66, 0x20},
217 	{0x67, 0x20},
218 	{0x68, 0x20},
219 	{0x69, 0x20},
220 	{0x76, 0x00},
221 	{0x6a, 0x08},
222 	{0x6b, 0x08},
223 	{0x6c, 0x08},
224 	{0x6d, 0x08},
225 	{0x6e, 0x08},
226 	{0x6f, 0x08},
227 	{0x70, 0x08},
228 	{0x71, 0x08},
229 	{0x76, 0x00},
230 	{0x72, 0xf0},
231 	{0x7e, 0x3c},
232 	{0x7f, 0x00},
233 	{0xfe, 0x02},
234 	{0x48, 0x15},
235 	{0x49, 0x00},
236 	{0x4b, 0x0b},
237 	{0xfe, 0x00},
238 	/*AEC*/
239 	{0xfe, 0x01},
240 	{0x01, 0x04},
241 	{0x02, 0xc0},
242 	{0x03, 0x04},
243 	{0x04, 0x90},
244 	{0x05, 0x30},
245 	{0x06, 0x90},
246 	{0x07, 0x30},
247 	{0x08, 0x80},
248 	{0x09, 0x00},
249 	{0x0a, 0x82},
250 	{0x0b, 0x11},
251 	{0x0c, 0x10},
252 	{0x11, 0x10},
253 	{0x13, 0x7b},
254 	{0x17, 0x00},
255 	{0x1c, 0x11},
256 	{0x1e, 0x61},
257 	{0x1f, 0x35},
258 	{0x20, 0x40},
259 	{0x22, 0x40},
260 	{0x23, 0x20},
261 	{0xfe, 0x02},
262 	{0x0f, 0x04},
263 	{0xfe, 0x01},
264 	{0x12, 0x35},
265 	{0x15, 0xb0},
266 	{0x10, 0x31},
267 	{0x3e, 0x28},
268 	{0x3f, 0xb0},
269 	{0x40, 0x90},
270 	{0x41, 0x0f},
271 
272 	/*INTPEE*/
273 	{0xfe, 0x02},
274 	{0x90, 0x6c},
275 	{0x91, 0x03},
276 	{0x92, 0xcb},
277 	{0x94, 0x33},
278 	{0x95, 0x84},
279 	{0x97, 0x45},
280 	{0xa2, 0x11},
281 	{0xfe, 0x00},
282 	/*DNDD*/
283 	{0xfe, 0x02},
284 	{0x80, 0xc1},
285 	{0x81, 0x08},
286 	{0x82, 0x1f},
287 	{0x83, 0x10},
288 	{0x84, 0x0a},
289 	{0x86, 0xf0},
290 	{0x87, 0x50},
291 	{0x88, 0x15},
292 	{0x89, 0xb0},
293 	{0x8a, 0x30},
294 	{0x8b, 0x10},
295 	/*ASDE*/
296 	{0xfe, 0x01},
297 	{0x21, 0x04},
298 	{0xfe, 0x02},
299 	{0xa3, 0x50},
300 	{0xa4, 0x20},
301 	{0xa5, 0x40},
302 	{0xa6, 0x80},
303 	{0xab, 0x40},
304 	{0xae, 0x0c},
305 	{0xb3, 0x46},
306 	{0xb4, 0x64},
307 	{0xb6, 0x38},
308 	{0xb7, 0x01},
309 	{0xb9, 0x2b},
310 	{0x3c, 0x04},
311 	{0x3d, 0x15},
312 	{0x4b, 0x06},
313 	{0x4c, 0x20},
314 	{0xfe, 0x00},
315 	/*GAMMA*/
316 	/*gamma1*/
317 #if 1
318 	{0xfe, 0x02},
319 	{0x10, 0x09},
320 	{0x11, 0x0d},
321 	{0x12, 0x13},
322 	{0x13, 0x19},
323 	{0x14, 0x27},
324 	{0x15, 0x37},
325 	{0x16, 0x45},
326 	{0x17, 0x53},
327 	{0x18, 0x69},
328 	{0x19, 0x7d},
329 	{0x1a, 0x8f},
330 	{0x1b, 0x9d},
331 	{0x1c, 0xa9},
332 	{0x1d, 0xbd},
333 	{0x1e, 0xcd},
334 	{0x1f, 0xd9},
335 	{0x20, 0xe3},
336 	{0x21, 0xea},
337 	{0x22, 0xef},
338 	{0x23, 0xf5},
339 	{0x24, 0xf9},
340 	{0x25, 0xff},
341 #else
342 	{0xfe, 0x02},
343 	{0x10, 0x0a},
344 	{0x11, 0x12},
345 	{0x12, 0x19},
346 	{0x13, 0x1f},
347 	{0x14, 0x2c},
348 	{0x15, 0x38},
349 	{0x16, 0x42},
350 	{0x17, 0x4e},
351 	{0x18, 0x63},
352 	{0x19, 0x76},
353 	{0x1a, 0x87},
354 	{0x1b, 0x96},
355 	{0x1c, 0xa2},
356 	{0x1d, 0xb8},
357 	{0x1e, 0xcb},
358 	{0x1f, 0xd8},
359 	{0x20, 0xe2},
360 	{0x21, 0xe9},
361 	{0x22, 0xf0},
362 	{0x23, 0xf8},
363 	{0x24, 0xfd},
364 	{0x25, 0xff},
365 	{0xfe, 0x00},
366 #endif
367 	{0xfe, 0x00},
368 	{0xc6, 0x20},
369 	{0xc7, 0x2b},
370 	/*gamma2*/
371 #if 1
372 	{0xfe, 0x02},
373 	{0x26, 0x0f},
374 	{0x27, 0x14},
375 	{0x28, 0x19},
376 	{0x29, 0x1e},
377 	{0x2a, 0x27},
378 	{0x2b, 0x33},
379 	{0x2c, 0x3b},
380 	{0x2d, 0x45},
381 	{0x2e, 0x59},
382 	{0x2f, 0x69},
383 	{0x30, 0x7c},
384 	{0x31, 0x89},
385 	{0x32, 0x98},
386 	{0x33, 0xae},
387 	{0x34, 0xc0},
388 	{0x35, 0xcf},
389 	{0x36, 0xda},
390 	{0x37, 0xe2},
391 	{0x38, 0xe9},
392 	{0x39, 0xf3},
393 	{0x3a, 0xf9},
394 	{0x3b, 0xff},
395 #else
396 	/*Gamma outdoor*/
397 	{0xfe, 0x02},
398 	{0x26, 0x17},
399 	{0x27, 0x18},
400 	{0x28, 0x1c},
401 	{0x29, 0x20},
402 	{0x2a, 0x28},
403 	{0x2b, 0x34},
404 	{0x2c, 0x40},
405 	{0x2d, 0x49},
406 	{0x2e, 0x5b},
407 	{0x2f, 0x6d},
408 	{0x30, 0x7d},
409 	{0x31, 0x89},
410 	{0x32, 0x97},
411 	{0x33, 0xac},
412 	{0x34, 0xc0},
413 	{0x35, 0xcf},
414 	{0x36, 0xda},
415 	{0x37, 0xe5},
416 	{0x38, 0xec},
417 	{0x39, 0xf8},
418 	{0x3a, 0xfd},
419 	{0x3b, 0xff},
420 #endif
421 	/*YCP*/
422 	{0xfe, 0x02},
423 	{0xd1, 0x40},
424 	{0xd2, 0x40},
425 	{0xd3, 0x48},
426 	{0xd6, 0xf0},
427 	{0xd7, 0x10},
428 	{0xd8, 0xda},
429 	{0xdd, 0x14},
430 	{0xde, 0x86},
431 	{0xed, 0x80},
432 	{0xee, 0x00},
433 	{0xef, 0x3f},
434 	{0xd8, 0xd8},
435 	/*abs*/
436 	{0xfe, 0x01},
437 	{0x9f, 0x40},
438 	/*LSC*/
439 	{0xfe, 0x01},
440 	{0xc2, 0x14},
441 	{0xc3, 0x0d},
442 	{0xc4, 0x0c},
443 	{0xc8, 0x15},
444 	{0xc9, 0x0d},
445 	{0xca, 0x0a},
446 	{0xbc, 0x24},
447 	{0xbd, 0x10},
448 	{0xbe, 0x0b},
449 	{0xb6, 0x25},
450 	{0xb7, 0x16},
451 	{0xb8, 0x15},
452 	{0xc5, 0x00},
453 	{0xc6, 0x00},
454 	{0xc7, 0x00},
455 	{0xcb, 0x00},
456 	{0xcc, 0x00},
457 	{0xcd, 0x00},
458 	{0xbf, 0x07},
459 	{0xc0, 0x00},
460 	{0xc1, 0x00},
461 	{0xb9, 0x00},
462 	{0xba, 0x00},
463 	{0xbb, 0x00},
464 	{0xaa, 0x01},
465 	{0xab, 0x01},
466 	{0xac, 0x00},
467 	{0xad, 0x05},
468 	{0xae, 0x06},
469 	{0xaf, 0x0e},
470 	{0xb0, 0x0b},
471 	{0xb1, 0x07},
472 	{0xb2, 0x06},
473 	{0xb3, 0x17},
474 	{0xb4, 0x0e},
475 	{0xb5, 0x0e},
476 	{0xd0, 0x09},
477 	{0xd1, 0x00},
478 	{0xd2, 0x00},
479 	{0xd6, 0x08},
480 	{0xd7, 0x00},
481 	{0xd8, 0x00},
482 	{0xd9, 0x00},
483 	{0xda, 0x00},
484 	{0xdb, 0x00},
485 	{0xd3, 0x0a},
486 	{0xd4, 0x00},
487 	{0xd5, 0x00},
488 	{0xa4, 0x00},
489 	{0xa5, 0x00},
490 	{0xa6, 0x77},
491 	{0xa7, 0x77},
492 	{0xa8, 0x77},
493 	{0xa9, 0x77},
494 	{0xa1, 0x80},
495 	{0xa2, 0x80},
496 
497 	{0xfe, 0x01},
498 	{0xdf, 0x0d},
499 	{0xdc, 0x25},
500 	{0xdd, 0x30},
501 	{0xe0, 0x77},
502 	{0xe1, 0x80},
503 	{0xe2, 0x77},
504 	{0xe3, 0x90},
505 	{0xe6, 0x90},
506 	{0xe7, 0xa0},
507 	{0xe8, 0x90},
508 	{0xe9, 0xa0},
509 	{0xfe, 0x00},
510 	/*AWB*/
511 	{0xfe, 0x01},
512 	{0x4f, 0x00},
513 	{0x4f, 0x00},
514 	{0x4b, 0x01},
515 	{0x4f, 0x00},
516 
517 	{0x4c, 0x01},
518 	{0x4d, 0x71},
519 	{0x4e, 0x01},
520 	{0x4c, 0x01},
521 	{0x4d, 0x91},
522 	{0x4e, 0x01},
523 	{0x4c, 0x01},
524 	{0x4d, 0x70},
525 	{0x4e, 0x01},
526 	{0x4c, 0x01},
527 	{0x4d, 0x90},
528 	{0x4e, 0x02},
529 	{0x4c, 0x01},
530 	{0x4d, 0xb0},
531 	{0x4e, 0x02},
532 	{0x4c, 0x01},
533 	{0x4d, 0x8f},
534 	{0x4e, 0x02},
535 	{0x4c, 0x01},
536 	{0x4d, 0x6f},
537 	{0x4e, 0x02},
538 	{0x4c, 0x01},
539 	{0x4d, 0xaf},
540 	{0x4e, 0x02},
541 	{0x4c, 0x01},
542 	{0x4d, 0xd0},
543 	{0x4e, 0x02},
544 	{0x4c, 0x01},
545 	{0x4d, 0xf0},
546 	{0x4e, 0x02},
547 	{0x4c, 0x01},
548 	{0x4d, 0xcf},
549 	{0x4e, 0x02},
550 	{0x4c, 0x01},
551 	{0x4d, 0xef},
552 	{0x4e, 0x02},
553 	{0x4c, 0x01},
554 	{0x4d, 0x6e},
555 	{0x4e, 0x03},
556 	{0x4c, 0x01},
557 	{0x4d, 0x8e},
558 	{0x4e, 0x03},
559 	{0x4c, 0x01},
560 	{0x4d, 0xae},
561 	{0x4e, 0x03},
562 	{0x4c, 0x01},
563 	{0x4d, 0xce},
564 	{0x4e, 0x03},
565 	{0x4c, 0x01},
566 	{0x4d, 0x4d},
567 	{0x4e, 0x03},
568 	{0x4c, 0x01},
569 	{0x4d, 0x6d},
570 	{0x4e, 0x03},
571 	{0x4c, 0x01},
572 	{0x4d, 0x8d},
573 	{0x4e, 0x03},
574 	{0x4c, 0x01},
575 	{0x4d, 0xad},
576 	{0x4e, 0x03},
577 	{0x4c, 0x01},
578 	{0x4d, 0xcd},
579 	{0x4e, 0x03},
580 	{0x4c, 0x01},
581 	{0x4d, 0x4c},
582 	{0x4e, 0x03},
583 	{0x4c, 0x01},
584 	{0x4d, 0x6c},
585 	{0x4e, 0x03},
586 	{0x4c, 0x01},
587 	{0x4d, 0x8c},
588 	{0x4e, 0x03},
589 	{0x4c, 0x01},
590 	{0x4d, 0xac},
591 	{0x4e, 0x03},
592 	{0x4c, 0x01},
593 	{0x4d, 0xcc},
594 	{0x4e, 0x03},
595 	{0x4c, 0x01},
596 	{0x4d, 0xcb},
597 	{0x4e, 0x03},
598 	{0x4c, 0x01},
599 	{0x4d, 0x4b},
600 	{0x4e, 0x03},
601 	{0x4c, 0x01},
602 	{0x4d, 0x6b},
603 	{0x4e, 0x03},
604 	{0x4c, 0x01},
605 	{0x4d, 0x8b},
606 	{0x4e, 0x03},
607 	{0x4c, 0x01},
608 	{0x4d, 0xab},
609 	{0x4e, 0x03},
610 	{0x4c, 0x01},
611 	{0x4d, 0x8a},
612 	{0x4e, 0x04},
613 	{0x4c, 0x01},
614 	{0x4d, 0xaa},
615 	{0x4e, 0x04},
616 	{0x4c, 0x01},
617 	{0x4d, 0xca},
618 	{0x4e, 0x04},
619 	{0x4c, 0x01},
620 	{0x4d, 0xca},
621 	{0x4e, 0x04},
622 	{0x4c, 0x01},
623 	{0x4d, 0xc9},
624 	{0x4e, 0x04},
625 	{0x4c, 0x01},
626 	{0x4d, 0x8a},
627 	{0x4e, 0x04},
628 	{0x4c, 0x01},
629 	{0x4d, 0x89},
630 	{0x4e, 0x04},
631 	{0x4c, 0x01},
632 	{0x4d, 0xa9},
633 	{0x4e, 0x04},
634 	{0x4c, 0x02},
635 	{0x4d, 0x0b},
636 	{0x4e, 0x05},
637 	{0x4c, 0x02},
638 	{0x4d, 0x0a},
639 	{0x4e, 0x05},
640 	{0x4c, 0x01},
641 	{0x4d, 0xeb},
642 	{0x4e, 0x05},
643 	{0x4c, 0x01},
644 	{0x4d, 0xea},
645 	{0x4e, 0x05},
646 	{0x4c, 0x02},
647 	{0x4d, 0x09},
648 	{0x4e, 0x05},
649 	{0x4c, 0x02},
650 	{0x4d, 0x29},
651 	{0x4e, 0x05},
652 	{0x4c, 0x02},
653 	{0x4d, 0x2a},
654 	{0x4e, 0x05},
655 	{0x4c, 0x02},
656 	{0x4d, 0x4a},
657 	{0x4e, 0x05},
658 	{0x4c, 0x02},
659 	{0x4d, 0x8a},
660 	{0x4e, 0x06},
661 	{0x4c, 0x02},
662 	{0x4d, 0x49},
663 	{0x4e, 0x06},
664 	{0x4c, 0x02},
665 	{0x4d, 0x69},
666 	{0x4e, 0x06},
667 	{0x4c, 0x02},
668 	{0x4d, 0x89},
669 	{0x4e, 0x06},
670 	{0x4c, 0x02},
671 	{0x4d, 0xa9},
672 	{0x4e, 0x06},
673 	{0x4c, 0x02},
674 	{0x4d, 0x48},
675 	{0x4e, 0x06},
676 	{0x4c, 0x02},
677 	{0x4d, 0x68},
678 	{0x4e, 0x06},
679 	{0x4c, 0x02},
680 	{0x4d, 0x69},
681 	{0x4e, 0x06},
682 	{0x4c, 0x02},
683 	{0x4d, 0xca},
684 	{0x4e, 0x07},
685 	{0x4c, 0x02},
686 	{0x4d, 0xc9},
687 	{0x4e, 0x07},
688 	{0x4c, 0x02},
689 	{0x4d, 0xe9},
690 	{0x4e, 0x07},
691 	{0x4c, 0x03},
692 	{0x4d, 0x09},
693 	{0x4e, 0x07},
694 	{0x4c, 0x02},
695 	{0x4d, 0xc8},
696 	{0x4e, 0x07},
697 	{0x4c, 0x02},
698 	{0x4d, 0xe8},
699 	{0x4e, 0x07},
700 	{0x4c, 0x02},
701 	{0x4d, 0xa7},
702 	{0x4e, 0x07},
703 	{0x4c, 0x02},
704 	{0x4d, 0xc7},
705 	{0x4e, 0x07},
706 	{0x4c, 0x02},
707 	{0x4d, 0xe7},
708 	{0x4e, 0x07},
709 	{0x4c, 0x03},
710 	{0x4d, 0x07},
711 	{0x4e, 0x07},
712 
713 	{0x4f, 0x01},
714 	{0x50, 0x80},
715 	{0x51, 0xa8},
716 	{0x52, 0x47},
717 	{0x53, 0x38},
718 	{0x54, 0xc7},
719 	{0x56, 0x0e},
720 	{0x58, 0x08},
721 	{0x5b, 0x00},
722 	{0x5c, 0x74},
723 	{0x5d, 0x8b},
724 	{0x61, 0xdb},
725 	{0x62, 0xb8},
726 	{0x63, 0x86},
727 	{0x64, 0xc0},
728 	{0x65, 0x04},
729 	{0x67, 0xa8},
730 	{0x68, 0xb0},
731 	{0x69, 0x00},
732 	{0x6a, 0xa8},
733 	{0x6b, 0xb0},
734 	{0x6c, 0xaf},
735 	{0x6d, 0x8b},
736 	{0x6e, 0x50},
737 	{0x6f, 0x18},
738 	{0x73, 0xf0},
739 	{0x70, 0x0d},
740 	{0x71, 0x60},
741 	{0x72, 0x80},
742 	{0x74, 0x01},
743 	{0x75, 0x01},
744 	{0x7f, 0x0c},
745 	{0x76, 0x70},
746 	{0x77, 0x58},
747 	{0x78, 0xa0},
748 	{0x79, 0x5e},
749 	{0x7a, 0x54},
750 	{0x7b, 0x58},
751 	{0xfe, 0x00},
752 	/*CC*/
753 	{0xfe, 0x02},
754 	{0xc0, 0x01},
755 	{0xc1, 0x44},
756 	{0xc2, 0xfd},
757 	{0xc3, 0x04},
758 	{0xc4, 0xF0},
759 	{0xc5, 0x48},
760 	{0xc6, 0xfd},
761 	{0xc7, 0x46},
762 	{0xc8, 0xfd},
763 	{0xc9, 0x02},
764 	{0xca, 0xe0},
765 	{0xcb, 0x45},
766 	{0xcc, 0xec},
767 	{0xcd, 0x48},
768 	{0xce, 0xf0},
769 	{0xcf, 0xf0},
770 	{0xe3, 0x0c},
771 	{0xe4, 0x4b},
772 	{0xe5, 0xe0},
773 	/*ABS*/
774 	{0xfe, 0x01},
775 	{0x9f, 0x40},
776 	{0xfe, 0x00},
777 	/*OUTPUT*/
778 	{0xfe, 0x00},
779 	{0xf2, 0x0f},
780 	/*dark sun*/
781 	{0xfe, 0x02},
782 	{0x40, 0xbf},
783 	{0x46, 0xcf},
784 	{0xfe, 0x00},
785 
786 	/*frame rate 50Hz*/
787 	{0xfe, 0x00},
788 	{0x05, 0x02},
789 	{0x06, 0x20},
790 	{0x07, 0x00},
791 	{0x08, 0x32},
792 	{0xfe, 0x01},
793 	{0x25, 0x00},
794 	{0x26, 0xfa},
795 
796 	{0x27, 0x04},
797 	{0x28, 0xe2},
798 	{0x29, 0x04},
799 	{0x2a, 0xe2},
800 	{0x2b, 0x04},
801 	{0x2c, 0xe2},
802 	{0x2d, 0x04},
803 	{0x2e, 0xe2},
804 	{0xfe, 0x00},
805 
806 	{0xfe, 0x00},
807 	{0xfd, 0x01},
808 	{0xfa, 0x00},
809 	/*crop window*/
810 	{0xfe, 0x00},
811 	{0x90, 0x01},
812 	{0x91, 0x00},
813 	{0x92, 0x00},
814 	{0x93, 0x00},
815 	{0x94, 0x00},
816 	{0x95, 0x02},
817 	{0x96, 0x58},
818 	{0x97, 0x03},
819 	{0x98, 0x20},
820 	{0x99, 0x11},
821 	{0x9a, 0x06},
822 	/*AWB*/
823 	{0xfe, 0x00},
824 	{0xec, 0x02},
825 	{0xed, 0x02},
826 	{0xee, 0x30},
827 	{0xef, 0x48},
828 	{0xfe, 0x02},
829 	{0x9d, 0x08},
830 	{0xfe, 0x01},
831 	{0x74, 0x00},
832 	/*AEC*/
833 	{0xfe, 0x01},
834 	{0x01, 0x04},
835 	{0x02, 0x60},
836 	{0x03, 0x02},
837 	{0x04, 0x48},
838 	{0x05, 0x18},
839 	{0x06, 0x50},
840 	{0x07, 0x10},
841 	{0x08, 0x38},
842 	{0x0a, 0x80},
843 	{0x21, 0x04},
844 	{0xfe, 0x00},
845 	{0x20, 0x03},
846 	{0xfe, 0x00},
847 	{REG_NULL, 0x00},
848 };
849 
850 /* Senor full resolution setting */
851 static const struct sensor_register gc2145_dvp_full[] = {
852 	//SENSORDB("GC2145_Sensor_2M"},
853 	{0xfe, 0x00},
854 	{0x05, 0x02},
855 	{0x06, 0x20},
856 	{0x07, 0x00},
857 	{0x08, 0x50},
858 	{0xfe, 0x01},
859 	{0x25, 0x00},
860 	{0x26, 0xfa},
861 
862 	{0x27, 0x04},
863 	{0x28, 0xe2},
864 	{0x29, 0x04},
865 	{0x2a, 0xe2},
866 	{0x2b, 0x04},
867 	{0x2c, 0xe2},
868 	{0x2d, 0x04},
869 	{0x2e, 0xe2},
870 	{0xfe, 0x00},
871 	{0xfd, 0x00},
872 	{0xfa, 0x11},
873 	{0x18, 0x22},
874 	/*crop window*/
875 	{0xfe, 0x00},
876 	{0x90, 0x01},
877 	{0x91, 0x00},
878 	{0x92, 0x00},
879 	{0x93, 0x00},
880 	{0x94, 0x00},
881 	{0x95, 0x04},
882 	{0x96, 0xb0},
883 	{0x97, 0x06},
884 	{0x98, 0x40},
885 	{0x99, 0x11},
886 	{0x9a, 0x06},
887 	/*AWB*/
888 	{0xfe, 0x00},
889 	{0xec, 0x06},
890 	{0xed, 0x04},
891 	{0xee, 0x60},
892 	{0xef, 0x90},
893 	{0xfe, 0x01},
894 	{0x74, 0x01},
895 	/*AEC*/
896 	{0xfe, 0x01},
897 	{0x01, 0x04},
898 	{0x02, 0xc0},
899 	{0x03, 0x04},
900 	{0x04, 0x90},
901 	{0x05, 0x30},
902 	{0x06, 0x90},
903 	{0x07, 0x30},
904 	{0x08, 0x80},
905 	{0x0a, 0x82},
906 	{0xfe, 0x01},
907 	{0x21, 0x15}, //if 0xfa=11,then 0x21=15;
908 	{0xfe, 0x00}, //else if 0xfa=00,then 0x21=04
909 	{0x20, 0x15},
910 	{0xfe, 0x00},
911 
912 	{REG_NULL, 0x00},
913 };
914 
915 /* Preview resolution setting*/
916 static const struct sensor_register gc2145_dvp_svga_30fps[] = {
917 	/*frame rate 50Hz*/
918 	{0xfe, 0x00},
919 	{0x05, 0x02},
920 	{0x06, 0x20},
921 	{0x07, 0x00},
922 	{0x08, 0xb8},
923 	{0xfe, 0x01},
924 	{0x13, 0x7b},
925 	{0x18, 0x95},
926 	{0x25, 0x01},
927 	{0x26, 0xac},
928 
929 	{0x27, 0x05},
930 	{0x28, 0x04},
931 	{0x29, 0x05},
932 	{0x2a, 0x04},
933 	{0x2b, 0x05},
934 	{0x2c, 0x04},
935 	{0x2d, 0x05},
936 	{0x2e, 0x04},
937 
938 	{0x3c, 0x00},
939 	{0x3d, 0x15},
940 	{0xfe, 0x00},
941 	{0xfe, 0x00},
942 	{0xfa, 0x00},
943 	{0x18, 0x42},
944 	{0xfd, 0x03},
945 	{0xb6, 0x01},
946 	/* crop window */
947 	{0xfe, 0x00},
948 	{0x99, 0x11},
949 	{0x9a, 0x06},
950 	{0x9b, 0x00},
951 	{0x9c, 0x00},
952 	{0x9d, 0x00},
953 	{0x9e, 0x00},
954 	{0x9f, 0x00},
955 	{0xa0, 0x00},
956 	{0xa1, 0x00},
957 	{0xa2, 0x00},
958 	{0x90, 0x01},
959 	{0x91, 0x00},
960 	{0x92, 0x00},
961 	{0x93, 0x00},
962 	{0x94, 0x00},
963 	{0x95, 0x02},
964 	{0x96, 0x58},
965 	{0x97, 0x03},
966 	{0x98, 0x20},
967 	/* AWB */
968 	{0xfe, 0x00},
969 	{0xec, 0x01},
970 	{0xed, 0x02},
971 	{0xee, 0x30},
972 	{0xef, 0x48},
973 	{0xfe, 0x01},
974 	{0x74, 0x00},
975 	/* AEC */
976 	{0xfe, 0x01},
977 	{0x01, 0x04},
978 	{0x02, 0x60},
979 	{0x03, 0x02},
980 	{0x04, 0x48},
981 	{0x05, 0x18},
982 	{0x06, 0x4c},
983 	{0x07, 0x14},
984 	{0x08, 0x36},
985 	{0x0a, 0xc0},
986 	{0x21, 0x14},
987 	{0xfe, 0x00},
988 	{REG_NULL, 0x00},
989 };
990 
991 /* Preview resolution setting*/
992 static const struct sensor_register gc2145_dvp_svga_20fps[] = {
993 	//SENSORDB("GC2145_Sensor_SVGA"},
994 	{0xfe, 0x00},
995 	{0x05, 0x02},
996 	{0x06, 0x20},
997 	{0x07, 0x00},
998 	{0x08, 0x50},
999 	{0xfe, 0x01},
1000 	{0x25, 0x00},
1001 	{0x26, 0xfa},
1002 
1003 	{0x27, 0x04},
1004 	{0x28, 0xe2},
1005 	{0x29, 0x04},
1006 	{0x2a, 0xe2},
1007 	{0x2b, 0x04},
1008 	{0x2c, 0xe2},
1009 	{0x2d, 0x04},
1010 	{0x2e, 0xe2},
1011 
1012 	{0xfe, 0x00},
1013 	{0xb6, 0x01},
1014 	{0xfd, 0x01},
1015 	{0xfa, 0x00},
1016 	{0x18, 0x22},
1017 	/*crop window*/
1018 	{0xfe, 0x00},
1019 	{0x90, 0x01},
1020 	{0x91, 0x00},
1021 	{0x92, 0x00},
1022 	{0x93, 0x00},
1023 	{0x94, 0x00},
1024 	{0x95, 0x02},
1025 	{0x96, 0x58},
1026 	{0x97, 0x03},
1027 	{0x98, 0x20},
1028 	{0x99, 0x11},
1029 	{0x9a, 0x06},
1030 	/*AWB*/
1031 	{0xfe, 0x00},
1032 	{0xec, 0x02},
1033 	{0xed, 0x02},
1034 	{0xee, 0x30},
1035 	{0xef, 0x48},
1036 	{0xfe, 0x02},
1037 	{0x9d, 0x08},
1038 	{0xfe, 0x01},
1039 	{0x74, 0x00},
1040 	/*AEC*/
1041 	{0xfe, 0x01},
1042 	{0x01, 0x04},
1043 	{0x02, 0x60},
1044 	{0x03, 0x02},
1045 	{0x04, 0x48},
1046 	{0x05, 0x18},
1047 	{0x06, 0x50},
1048 	{0x07, 0x10},
1049 	{0x08, 0x38},
1050 	{0x0a, 0x80},
1051 	{0x21, 0x04},
1052 	{0xfe, 0x00},
1053 	{0x20, 0x03},
1054 	{0xfe, 0x00},
1055 	{REG_NULL, 0x00},
1056 };
1057 
1058 static const struct sensor_register gc2145_mipi_init_regs[] = {
1059 	{0xfe, 0xf0},
1060 	{0xfe, 0xf0},
1061 	{0xfe, 0xf0},
1062 	{0xfc, 0x06},
1063 	{0xf6, 0x00},
1064 	{0xf7, 0x1d},
1065 	{0xf8, 0x84},
1066 	{0xfa, 0x00},
1067 	{0xf9, 0x8e},
1068 	{0xf2, 0x00},
1069 	/*ISP reg*/
1070 	{0xfe, 0x00},
1071 	{0x03, 0x04},
1072 	{0x04, 0xe2},
1073 	{0x09, 0x00},
1074 	{0x0a, 0x00},
1075 	{0x0b, 0x00},
1076 	{0x0c, 0x00},
1077 	{0x0d, 0x04},
1078 	{0x0e, 0xc0},
1079 	{0x0f, 0x06},
1080 	{0x10, 0x52},
1081 	{0x12, 0x2e},
1082 	{0x17, 0x14},
1083 	{0x18, 0x22},
1084 	{0x19, 0x0e},
1085 	{0x1a, 0x01},
1086 	{0x1b, 0x4b},
1087 	{0x1c, 0x07},
1088 	{0x1d, 0x10},
1089 	{0x1e, 0x88},
1090 	{0x1f, 0x78},
1091 	{0x20, 0x03},
1092 	{0x21, 0x40},
1093 	{0x22, 0xa0},
1094 	{0x24, 0x16},
1095 	{0x25, 0x01},
1096 	{0x26, 0x10},
1097 	{0x2d, 0x60},
1098 	{0x30, 0x01},
1099 	{0x31, 0x90},
1100 	{0x33, 0x06},
1101 	{0x34, 0x01},
1102 	{0xfe, 0x00},
1103 	{0x80, 0x7f},
1104 	{0x81, 0x26},
1105 	{0x82, 0xfa},
1106 	{0x83, 0x00},
1107 	{0x84, 0x00},
1108 	{0x86, 0x02},
1109 	{0x88, 0x03},
1110 	{0x89, 0x03},
1111 	{0x85, 0x08},
1112 	{0x8a, 0x00},
1113 	{0x8b, 0x00},
1114 	{0xb0, 0x55},
1115 	{0xc3, 0x00},
1116 	{0xc4, 0x80},
1117 	{0xc5, 0x90},
1118 	{0xc6, 0x3b},
1119 	{0xc7, 0x46},
1120 	{0xec, 0x06},
1121 	{0xed, 0x04},
1122 	{0xee, 0x60},
1123 	{0xef, 0x90},
1124 	{0xb6, 0x01},
1125 	{0x90, 0x01},
1126 	{0x91, 0x00},
1127 	{0x92, 0x00},
1128 	{0x93, 0x00},
1129 	{0x94, 0x00},
1130 	{0x95, 0x04},
1131 	{0x96, 0xb0},
1132 	{0x97, 0x06},
1133 	{0x98, 0x40},
1134 	/*BLK*/
1135 	{0xfe, 0x00},
1136 	{0x40, 0x42},
1137 	{0x41, 0x00},
1138 	{0x43, 0x5b},
1139 	{0x5e, 0x00},
1140 	{0x5f, 0x00},
1141 	{0x60, 0x00},
1142 	{0x61, 0x00},
1143 	{0x62, 0x00},
1144 	{0x63, 0x00},
1145 	{0x64, 0x00},
1146 	{0x65, 0x00},
1147 	{0x66, 0x20},
1148 	{0x67, 0x20},
1149 	{0x68, 0x20},
1150 	{0x69, 0x20},
1151 	{0x76, 0x00},
1152 	{0x6a, 0x08},
1153 	{0x6b, 0x08},
1154 	{0x6c, 0x08},
1155 	{0x6d, 0x08},
1156 	{0x6e, 0x08},
1157 	{0x6f, 0x08},
1158 	{0x70, 0x08},
1159 	{0x71, 0x08},
1160 	{0x76, 0x00},
1161 	{0x72, 0xf0},
1162 	{0x7e, 0x3c},
1163 	{0x7f, 0x00},
1164 	{0xfe, 0x02},
1165 	{0x48, 0x15},
1166 	{0x49, 0x00},
1167 	{0x4b, 0x0b},
1168 	{0xfe, 0x00},
1169 	/*AEC*/
1170 	{0xfe, 0x01},
1171 	{0x01, 0x04},
1172 	{0x02, 0xc0},
1173 	{0x03, 0x04},
1174 	{0x04, 0x90},
1175 	{0x05, 0x30},
1176 	{0x06, 0x90},
1177 	{0x07, 0x30},
1178 	{0x08, 0x80},
1179 	{0x09, 0x00},
1180 	{0x0a, 0x82},
1181 	{0x0b, 0x11},
1182 	{0x0c, 0x10},
1183 	{0x11, 0x10},
1184 	{0x13, 0x7b},
1185 	{0x17, 0x00},
1186 	{0x1c, 0x11},
1187 	{0x1e, 0x61},
1188 	{0x1f, 0x35},
1189 	{0x20, 0x40},
1190 	{0x22, 0x40},
1191 	{0x23, 0x20},
1192 	{0xfe, 0x02},
1193 	{0x0f, 0x04},
1194 	{0xfe, 0x01},
1195 	{0x12, 0x35},
1196 	{0x15, 0xb0},
1197 	{0x10, 0x31},
1198 	{0x3e, 0x28},
1199 	{0x3f, 0xb0},
1200 	{0x40, 0x90},
1201 	{0x41, 0x0f},
1202 	/*INTPEE*/
1203 	{0xfe, 0x02},
1204 	{0x90, 0x6c},
1205 	{0x91, 0x03},
1206 	{0x92, 0xcb},
1207 	{0x94, 0x33},
1208 	{0x95, 0x84},
1209 	{0x97, 0x65},
1210 	{0xa2, 0x11},
1211 	{0xfe, 0x00},
1212 	/*DNDD*/
1213 	{0xfe, 0x02},
1214 	{0x80, 0xc1},
1215 	{0x81, 0x08},
1216 	{0x82, 0x05},
1217 	{0x83, 0x08},
1218 	{0x84, 0x0a},
1219 	{0x86, 0xf0},
1220 	{0x87, 0x50},
1221 	{0x88, 0x15},
1222 	{0x89, 0xb0},
1223 	{0x8a, 0x30},
1224 	{0x8b, 0x10},
1225 	/*ASDE*/
1226 	{0xfe, 0x01},
1227 	{0x21, 0x04},
1228 	{0xfe, 0x02},
1229 	{0xa3, 0x50},
1230 	{0xa4, 0x20},
1231 	{0xa5, 0x40},
1232 	{0xa6, 0x80},
1233 	{0xab, 0x40},
1234 	{0xae, 0x0c},
1235 	{0xb3, 0x46},
1236 	{0xb4, 0x64},
1237 	{0xb6, 0x38},
1238 	{0xb7, 0x01},
1239 	{0xb9, 0x2b},
1240 	{0x3c, 0x04},
1241 	{0x3d, 0x15},
1242 	{0x4b, 0x06},
1243 	{0x4c, 0x20},
1244 	{0xfe, 0x00},
1245 	/*GAMMA*/
1246 	/*gamma1*/
1247 	{0xfe, 0x02},
1248 	{0x10, 0x09},
1249 	{0x11, 0x0d},
1250 	{0x12, 0x13},
1251 	{0x13, 0x19},
1252 	{0x14, 0x27},
1253 	{0x15, 0x37},
1254 	{0x16, 0x45},
1255 	{0x17, 0x53},
1256 	{0x18, 0x69},
1257 	{0x19, 0x7d},
1258 	{0x1a, 0x8f},
1259 	{0x1b, 0x9d},
1260 	{0x1c, 0xa9},
1261 	{0x1d, 0xbd},
1262 	{0x1e, 0xcd},
1263 	{0x1f, 0xd9},
1264 	{0x20, 0xe3},
1265 	{0x21, 0xea},
1266 	{0x22, 0xef},
1267 	{0x23, 0xf5},
1268 	{0x24, 0xf9},
1269 	{0x25, 0xff},
1270 	{0xfe, 0x00},
1271 	{0xc6, 0x20},
1272 	{0xc7, 0x2b},
1273 	/*gamma2*/
1274 	{0xfe, 0x02},
1275 	{0x26, 0x0f},
1276 	{0x27, 0x14},
1277 	{0x28, 0x19},
1278 	{0x29, 0x1e},
1279 	{0x2a, 0x27},
1280 	{0x2b, 0x33},
1281 	{0x2c, 0x3b},
1282 	{0x2d, 0x45},
1283 	{0x2e, 0x59},
1284 	{0x2f, 0x69},
1285 	{0x30, 0x7c},
1286 	{0x31, 0x89},
1287 	{0x32, 0x98},
1288 	{0x33, 0xae},
1289 	{0x34, 0xc0},
1290 	{0x35, 0xcf},
1291 	{0x36, 0xda},
1292 	{0x37, 0xe2},
1293 	{0x38, 0xe9},
1294 	{0x39, 0xf3},
1295 	{0x3a, 0xf9},
1296 	{0x3b, 0xff},
1297 	/*YCP*/
1298 	{0xfe, 0x02},
1299 	{0xd1, 0x32},
1300 	{0xd2, 0x32},
1301 	{0xd3, 0x40},
1302 	{0xd6, 0xf0},
1303 	{0xd7, 0x10},
1304 	{0xd8, 0xda},
1305 	{0xdd, 0x14},
1306 	{0xde, 0x86},
1307 	{0xed, 0x80},
1308 	{0xee, 0x00},
1309 	{0xef, 0x3f},
1310 	{0xd8, 0xd8},
1311 	/*abs*/
1312 	{0xfe, 0x01},
1313 	{0x9f, 0x40},
1314 	/*LSC*/
1315 	{0xfe, 0x01},
1316 	{0xc2, 0x14},
1317 	{0xc3, 0x0d},
1318 	{0xc4, 0x0c},
1319 	{0xc8, 0x15},
1320 	{0xc9, 0x0d},
1321 	{0xca, 0x0a},
1322 	{0xbc, 0x24},
1323 	{0xbd, 0x10},
1324 	{0xbe, 0x0b},
1325 	{0xb6, 0x25},
1326 	{0xb7, 0x16},
1327 	{0xb8, 0x15},
1328 	{0xc5, 0x00},
1329 	{0xc6, 0x00},
1330 	{0xc7, 0x00},
1331 	{0xcb, 0x00},
1332 	{0xcc, 0x00},
1333 	{0xcd, 0x00},
1334 	{0xbf, 0x07},
1335 	{0xc0, 0x00},
1336 	{0xc1, 0x00},
1337 	{0xb9, 0x00},
1338 	{0xba, 0x00},
1339 	{0xbb, 0x00},
1340 	{0xaa, 0x01},
1341 	{0xab, 0x01},
1342 	{0xac, 0x00},
1343 	{0xad, 0x05},
1344 	{0xae, 0x06},
1345 	{0xaf, 0x0e},
1346 	{0xb0, 0x0b},
1347 	{0xb1, 0x07},
1348 	{0xb2, 0x06},
1349 	{0xb3, 0x17},
1350 	{0xb4, 0x0e},
1351 	{0xb5, 0x0e},
1352 	{0xd0, 0x09},
1353 	{0xd1, 0x00},
1354 	{0xd2, 0x00},
1355 	{0xd6, 0x08},
1356 	{0xd7, 0x00},
1357 	{0xd8, 0x00},
1358 	{0xd9, 0x00},
1359 	{0xda, 0x00},
1360 	{0xdb, 0x00},
1361 	{0xd3, 0x0a},
1362 	{0xd4, 0x00},
1363 	{0xd5, 0x00},
1364 	{0xa4, 0x00},
1365 	{0xa5, 0x00},
1366 	{0xa6, 0x77},
1367 	{0xa7, 0x77},
1368 	{0xa8, 0x77},
1369 	{0xa9, 0x77},
1370 	{0xa1, 0x80},
1371 	{0xa2, 0x80},
1372 
1373 	{0xfe, 0x01},
1374 	{0xdf, 0x0d},
1375 	{0xdc, 0x25},
1376 	{0xdd, 0x30},
1377 	{0xe0, 0x77},
1378 	{0xe1, 0x80},
1379 	{0xe2, 0x77},
1380 	{0xe3, 0x90},
1381 	{0xe6, 0x90},
1382 	{0xe7, 0xa0},
1383 	{0xe8, 0x90},
1384 	{0xe9, 0xa0},
1385 	{0xfe, 0x00},
1386 	/*AWB*/
1387 	{0xfe, 0x01},
1388 	{0x4f, 0x00},
1389 	{0x4f, 0x00},
1390 	{0x4b, 0x01},
1391 	{0x4f, 0x00},
1392 
1393 	{0x4c, 0x01},
1394 	{0x4d, 0x71},
1395 	{0x4e, 0x01},
1396 	{0x4c, 0x01},
1397 	{0x4d, 0x91},
1398 	{0x4e, 0x01},
1399 	{0x4c, 0x01},
1400 	{0x4d, 0x70},
1401 	{0x4e, 0x01},
1402 	{0x4c, 0x01},
1403 	{0x4d, 0x90},
1404 	{0x4e, 0x02},
1405 	{0x4c, 0x01},
1406 	{0x4d, 0xb0},
1407 	{0x4e, 0x02},
1408 	{0x4c, 0x01},
1409 	{0x4d, 0x8f},
1410 	{0x4e, 0x02},
1411 	{0x4c, 0x01},
1412 	{0x4d, 0x6f},
1413 	{0x4e, 0x02},
1414 	{0x4c, 0x01},
1415 	{0x4d, 0xaf},
1416 	{0x4e, 0x02},
1417 	{0x4c, 0x01},
1418 	{0x4d, 0xd0},
1419 	{0x4e, 0x02},
1420 	{0x4c, 0x01},
1421 	{0x4d, 0xf0},
1422 	{0x4e, 0x02},
1423 	{0x4c, 0x01},
1424 	{0x4d, 0xcf},
1425 	{0x4e, 0x02},
1426 	{0x4c, 0x01},
1427 	{0x4d, 0xef},
1428 	{0x4e, 0x02},
1429 	{0x4c, 0x01},
1430 	{0x4d, 0x6e},
1431 	{0x4e, 0x03},
1432 	{0x4c, 0x01},
1433 	{0x4d, 0x8e},
1434 	{0x4e, 0x03},
1435 	{0x4c, 0x01},
1436 	{0x4d, 0xae},
1437 	{0x4e, 0x03},
1438 	{0x4c, 0x01},
1439 	{0x4d, 0xce},
1440 	{0x4e, 0x03},
1441 	{0x4c, 0x01},
1442 	{0x4d, 0x4d},
1443 	{0x4e, 0x03},
1444 	{0x4c, 0x01},
1445 	{0x4d, 0x6d},
1446 	{0x4e, 0x03},
1447 	{0x4c, 0x01},
1448 	{0x4d, 0x8d},
1449 	{0x4e, 0x03},
1450 	{0x4c, 0x01},
1451 	{0x4d, 0xad},
1452 	{0x4e, 0x03},
1453 	{0x4c, 0x01},
1454 	{0x4d, 0xcd},
1455 	{0x4e, 0x03},
1456 	{0x4c, 0x01},
1457 	{0x4d, 0x4c},
1458 	{0x4e, 0x03},
1459 	{0x4c, 0x01},
1460 	{0x4d, 0x6c},
1461 	{0x4e, 0x03},
1462 	{0x4c, 0x01},
1463 	{0x4d, 0x8c},
1464 	{0x4e, 0x03},
1465 	{0x4c, 0x01},
1466 	{0x4d, 0xac},
1467 	{0x4e, 0x03},
1468 	{0x4c, 0x01},
1469 	{0x4d, 0xcc},
1470 	{0x4e, 0x03},
1471 	{0x4c, 0x01},
1472 	{0x4d, 0xcb},
1473 	{0x4e, 0x03},
1474 	{0x4c, 0x01},
1475 	{0x4d, 0x4b},
1476 	{0x4e, 0x03},
1477 	{0x4c, 0x01},
1478 	{0x4d, 0x6b},
1479 	{0x4e, 0x03},
1480 	{0x4c, 0x01},
1481 	{0x4d, 0x8b},
1482 	{0x4e, 0x03},
1483 	{0x4c, 0x01},
1484 	{0x4d, 0xab},
1485 	{0x4e, 0x03},
1486 	{0x4c, 0x01},
1487 	{0x4d, 0x8a},
1488 	{0x4e, 0x04},
1489 	{0x4c, 0x01},
1490 	{0x4d, 0xaa},
1491 	{0x4e, 0x04},
1492 	{0x4c, 0x01},
1493 	{0x4d, 0xca},
1494 	{0x4e, 0x04},
1495 	{0x4c, 0x01},
1496 	{0x4d, 0xca},
1497 	{0x4e, 0x04},
1498 	{0x4c, 0x01},
1499 	{0x4d, 0xc9},
1500 	{0x4e, 0x04},
1501 	{0x4c, 0x01},
1502 	{0x4d, 0x8a},
1503 	{0x4e, 0x04},
1504 	{0x4c, 0x01},
1505 	{0x4d, 0x89},
1506 	{0x4e, 0x04},
1507 	{0x4c, 0x01},
1508 	{0x4d, 0xa9},
1509 	{0x4e, 0x04},
1510 	{0x4c, 0x02},
1511 	{0x4d, 0x0b},
1512 	{0x4e, 0x05},
1513 	{0x4c, 0x02},
1514 	{0x4d, 0x0a},
1515 	{0x4e, 0x05},
1516 	{0x4c, 0x01},
1517 	{0x4d, 0xeb},
1518 	{0x4e, 0x05},
1519 	{0x4c, 0x01},
1520 	{0x4d, 0xea},
1521 	{0x4e, 0x05},
1522 	{0x4c, 0x02},
1523 	{0x4d, 0x09},
1524 	{0x4e, 0x05},
1525 	{0x4c, 0x02},
1526 	{0x4d, 0x29},
1527 	{0x4e, 0x05},
1528 	{0x4c, 0x02},
1529 	{0x4d, 0x2a},
1530 	{0x4e, 0x05},
1531 	{0x4c, 0x02},
1532 	{0x4d, 0x4a},
1533 	{0x4e, 0x05},
1534 	{0x4c, 0x02},
1535 	{0x4d, 0x8a},
1536 	{0x4e, 0x06},
1537 	{0x4c, 0x02},
1538 	{0x4d, 0x49},
1539 	{0x4e, 0x06},
1540 	{0x4c, 0x02},
1541 	{0x4d, 0x69},
1542 	{0x4e, 0x06},
1543 	{0x4c, 0x02},
1544 	{0x4d, 0x89},
1545 	{0x4e, 0x06},
1546 	{0x4c, 0x02},
1547 	{0x4d, 0xa9},
1548 	{0x4e, 0x06},
1549 	{0x4c, 0x02},
1550 	{0x4d, 0x48},
1551 	{0x4e, 0x06},
1552 	{0x4c, 0x02},
1553 	{0x4d, 0x68},
1554 	{0x4e, 0x06},
1555 	{0x4c, 0x02},
1556 	{0x4d, 0x69},
1557 	{0x4e, 0x06},
1558 	{0x4c, 0x02},
1559 	{0x4d, 0xca},
1560 	{0x4e, 0x07},
1561 	{0x4c, 0x02},
1562 	{0x4d, 0xc9},
1563 	{0x4e, 0x07},
1564 	{0x4c, 0x02},
1565 	{0x4d, 0xe9},
1566 	{0x4e, 0x07},
1567 	{0x4c, 0x03},
1568 	{0x4d, 0x09},
1569 	{0x4e, 0x07},
1570 	{0x4c, 0x02},
1571 	{0x4d, 0xc8},
1572 	{0x4e, 0x07},
1573 	{0x4c, 0x02},
1574 	{0x4d, 0xe8},
1575 	{0x4e, 0x07},
1576 	{0x4c, 0x02},
1577 	{0x4d, 0xa7},
1578 	{0x4e, 0x07},
1579 	{0x4c, 0x02},
1580 	{0x4d, 0xc7},
1581 	{0x4e, 0x07},
1582 	{0x4c, 0x02},
1583 	{0x4d, 0xe7},
1584 	{0x4e, 0x07},
1585 	{0x4c, 0x03},
1586 	{0x4d, 0x07},
1587 	{0x4e, 0x07},
1588 
1589 	{0x4f, 0x01},
1590 	{0x50, 0x80},
1591 	{0x51, 0xa8},
1592 	{0x52, 0x47},
1593 	{0x53, 0x38},
1594 	{0x54, 0xc7},
1595 	{0x56, 0x0e},
1596 	{0x58, 0x08},
1597 	{0x5b, 0x00},
1598 	{0x5c, 0x74},
1599 	{0x5d, 0x8b},
1600 	{0x61, 0xdb},
1601 	{0x62, 0xb8},
1602 	{0x63, 0x86},
1603 	{0x64, 0xc0},
1604 	{0x65, 0x04},
1605 	{0x67, 0xa8},
1606 	{0x68, 0xb0},
1607 	{0x69, 0x00},
1608 	{0x6a, 0xa8},
1609 	{0x6b, 0xb0},
1610 	{0x6c, 0xaf},
1611 	{0x6d, 0x8b},
1612 	{0x6e, 0x50},
1613 	{0x6f, 0x18},
1614 	{0x73, 0xf0},
1615 	{0x70, 0x0d},
1616 	{0x71, 0x60},
1617 	{0x72, 0x80},
1618 	{0x74, 0x01},
1619 	{0x75, 0x01},
1620 	{0x7f, 0x0c},
1621 	{0x76, 0x70},
1622 	{0x77, 0x58},
1623 	{0x78, 0xa0},
1624 	{0x79, 0x5e},
1625 	{0x7a, 0x54},
1626 	{0x7b, 0x58},
1627 	{0xfe, 0x00},
1628 	/*CC*/
1629 	{0xfe, 0x02},
1630 	{0xc0, 0x01},
1631 	{0xc1, 0x44},
1632 	{0xc2, 0xfd},
1633 	{0xc3, 0x04},
1634 	{0xc4, 0xF0},
1635 	{0xc5, 0x48},
1636 	{0xc6, 0xfd},
1637 	{0xc7, 0x46},
1638 	{0xc8, 0xfd},
1639 	{0xc9, 0x02},
1640 	{0xca, 0xe0},
1641 	{0xcb, 0x45},
1642 	{0xcc, 0xec},
1643 	{0xcd, 0x48},
1644 	{0xce, 0xf0},
1645 	{0xcf, 0xf0},
1646 	{0xe3, 0x0c},
1647 	{0xe4, 0x4b},
1648 	{0xe5, 0xe0},
1649 	/*ABS*/
1650 	{0xfe, 0x01},
1651 	{0x9f, 0x40},
1652 	{0xfe, 0x00},
1653 	/*OUTPUT*/
1654 	{0xfe, 0x00},
1655 	{0xf2, 0x00},
1656 
1657 	/*frame rate 50Hz*/
1658 	{0xfe, 0x00},
1659 	{0x05, 0x01},
1660 	{0x06, 0x56},
1661 	{0x07, 0x00},
1662 	{0x08, 0x32},
1663 	{0xfe, 0x01},
1664 	{0x25, 0x00},
1665 	{0x26, 0xfa},
1666 
1667 	{0x27, 0x04},
1668 	{0x28, 0xe2},
1669 	{0x29, 0x04},
1670 	{0x2a, 0xe2},
1671 	{0x2b, 0x04},
1672 	{0x2c, 0xe2},
1673 	{0x2d, 0x04},
1674 	{0x2e, 0xe2},
1675 	{0xfe, 0x00},
1676 
1677 	{0xfe, 0x02},
1678 	{0x40, 0xbf},
1679 	{0x46, 0xcf},
1680 	{0xfe, 0x00},
1681 
1682 	{0xfe, 0x03},
1683 	{0x02, 0x22},
1684 	{0x03, 0x10},
1685 	{0x04, 0x10},
1686 	{0x05, 0x00},
1687 	{0x06, 0x88},
1688 	{0x01, 0x83},
1689 	{0x10, 0x84},
1690 	{0x11, 0x1e},
1691 	{0x12, 0x80},
1692 	{0x13, 0x0c},
1693 	{0x15, 0x10},
1694 	{0x17, 0xf0},
1695 	{0x21, 0x10},
1696 	{0x22, 0x04},
1697 	{0x23, 0x10},
1698 	{0x24, 0x10},
1699 	{0x25, 0x10},
1700 	{0x26, 0x05},
1701 	{0x29, 0x03},
1702 	{0x2a, 0x0a},
1703 	{0x2b, 0x06},
1704 	{0xfe, 0x00},
1705 	{REG_NULL, 0x00},
1706 };
1707 
1708 static const struct sensor_register gc2145_mipi_full[] = {
1709 	/*SENSORDB("GC2145_Sensor_2M")*/
1710 	{0xfe, 0x00},
1711 	{0x05, 0x02},
1712 	{0x06, 0x20},
1713 	{0x07, 0x00},
1714 	{0x08, 0x50},
1715 	{0xfe, 0x01},
1716 	{0x25, 0x00},
1717 	{0x26, 0xfa},
1718 
1719 	{0x27, 0x04},
1720 	{0x28, 0xe2},
1721 	{0x29, 0x04},
1722 	{0x2a, 0xe2},
1723 	{0x2b, 0x04},
1724 	{0x2c, 0xe2},
1725 	{0x2d, 0x04},
1726 	{0x2e, 0xe2},
1727 	{0xfe, 0x00},
1728 	{0xfd, 0x00},
1729 	{0xfa, 0x11},
1730 	{0x18, 0x22},
1731 	/*crop window*/
1732 	{0xfe, 0x00},
1733 	{0x90, 0x01},
1734 	{0x91, 0x00},
1735 	{0x92, 0x00},
1736 	{0x93, 0x00},
1737 	{0x94, 0x00},
1738 	{0x95, 0x04},
1739 	{0x96, 0xb0},
1740 	{0x97, 0x06},
1741 	{0x98, 0x40},
1742 	{0x99, 0x11},
1743 	{0x9a, 0x06},
1744 	/*AWB*/
1745 	{0xfe, 0x00},
1746 	{0xec, 0x06},
1747 	{0xed, 0x04},
1748 	{0xee, 0x60},
1749 	{0xef, 0x90},
1750 	{0xfe, 0x01},
1751 	{0x74, 0x01},
1752 
1753 	{0xfe, 0x00},
1754 	{0x7e, 0x3c},
1755 	{0x7f, 0x00},
1756 	{0xfe, 0x01},
1757 	{0xa0, 0x03},
1758 
1759 	/*AEC*/
1760 	{0xfe, 0x01},
1761 	{0x01, 0x04},
1762 	{0x02, 0xc0},
1763 	{0x03, 0x04},
1764 	{0x04, 0x90},
1765 	{0x05, 0x30},
1766 	{0x06, 0x90},
1767 	{0x07, 0x30},
1768 	{0x08, 0x80},
1769 	{0x0a, 0x82},
1770 	{0x1b, 0x01},
1771 	{0xfe, 0x00},
1772 	{0xfe, 0x01},
1773 	{0x21, 0x15},
1774 	{0xfe, 0x00},
1775 	{0x20, 0x15},
1776 	{0xfe, 0x03},
1777 	{0x12, 0x80},
1778 	{0x13, 0x0c},
1779 	{0x04, 0x01},
1780 	{0x05, 0x00},
1781 	{0xfe, 0x00},
1782 	{REG_NULL, 0x00},
1783 };
1784 
1785 static const struct sensor_register gc2145_mipi_svga_20fps[] = {
1786 	/*frame rate 50Hz*/
1787 	{0xfe, 0x00},
1788 	{0x05, 0x02},
1789 	{0x06, 0x20},
1790 	{0x07, 0x00},
1791 	{0x08, 0x50},
1792 	{0xfe, 0x01},
1793 	{0x25, 0x00},
1794 	{0x26, 0xfa},
1795 
1796 	{0x27, 0x04},
1797 	{0x28, 0xe2},
1798 	{0x29, 0x04},
1799 	{0x2a, 0xe2},
1800 	{0x2b, 0x04},
1801 	{0x2c, 0xe2},
1802 	{0x2d, 0x04},
1803 	{0x2e, 0xe2},
1804 
1805 	{0xfe, 0x00},
1806 	{0xb6, 0x01},
1807 	{0xfd, 0x01},
1808 	{0xfa, 0x00},
1809 	{0x18, 0x22},
1810 	/*crop window*/
1811 	{0xfe, 0x00},
1812 	{0x90, 0x01},
1813 	{0x91, 0x00},
1814 	{0x92, 0x00},
1815 	{0x93, 0x00},
1816 	{0x94, 0x00},
1817 	{0x95, 0x02},
1818 	{0x96, 0x58},
1819 	{0x97, 0x03},
1820 	{0x98, 0x20},
1821 	{0x99, 0x11},
1822 	{0x9a, 0x06},
1823 	/*AWB*/
1824 	{0xfe, 0x00},
1825 	{0xec, 0x02},
1826 	{0xed, 0x02},
1827 	{0xee, 0x30},
1828 	{0xef, 0x48},
1829 	{0xfe, 0x02},
1830 	{0x9d, 0x08},
1831 	{0xfe, 0x01},
1832 	{0x74, 0x00},
1833 	{0xfe, 0x00},
1834 	{0x7e, 0x3c},
1835 	{0x7f, 0x00},
1836 	{0xfe, 0x01},
1837 	{0xa0, 0x03},
1838 
1839 	/*AEC*/
1840 	{0xfe, 0x01},
1841 	{0x01, 0x04},
1842 	{0x02, 0x60},
1843 	{0x03, 0x02},
1844 	{0x04, 0x48},
1845 	{0x05, 0x18},
1846 	{0x06, 0x50},
1847 	{0x07, 0x10},
1848 	{0x08, 0x38},
1849 	{0x0a, 0x80},
1850 	//{0x1b, 0x04},
1851 	{0x21, 0x04},
1852 	{0xfe, 0x00},
1853 	{0x20, 0x03},
1854 	{0xfe, 0x03},
1855 	{0x12, 0x40},
1856 	{0x13, 0x06},
1857 	{0x04, 0x01},
1858 	{0x05, 0x00},
1859 	{0xfe, 0x00},
1860 	{REG_NULL, 0x00},
1861 };
1862 
1863 static const struct sensor_register gc2145_mipi_svga_30fps[] = {
1864 	/*frame rate 50Hz*/
1865 	{0xfe, 0x00},
1866 	{0x05, 0x02},
1867 	{0x06, 0x20},
1868 	{0x07, 0x00},
1869 	{0x08, 0xb8},
1870 	{0xfe, 0x01},
1871 	{0x25, 0x01},
1872 	{0x26, 0xac},
1873 
1874 	{0x27, 0x05},//4e2 pad
1875 	{0x28, 0x04},
1876 	{0x29, 0x05},//6d6 pad
1877 	{0x2a, 0x04},
1878 	{0x2b, 0x05},//7d0 pad
1879 	{0x2c, 0x04},
1880 	{0x2d, 0x05},
1881 	{0x2e, 0x04},
1882 	{0xfe, 0x00},
1883 
1884 	{0xfe, 0x00},
1885 	{0xfd, 0x01},
1886 	{0xfa, 0x00},
1887 	{0x18, 0x62},
1888 	{0xfd, 0x03},
1889 	/*crop window*/
1890 	{0xfe, 0x00},
1891 	{0x90, 0x01},
1892 	{0x91, 0x00},
1893 	{0x92, 0x00},
1894 	{0x93, 0x00},
1895 	{0x94, 0x00},
1896 	{0x95, 0x02},
1897 	{0x96, 0x58},
1898 	{0x97, 0x03},
1899 	{0x98, 0x20},
1900 	{0x99, 0x11},
1901 	{0x9a, 0x06},
1902 	/*AWB*/
1903 	{0xfe, 0x00},
1904 	{0xec, 0x02},
1905 	{0xed, 0x02},
1906 	{0xee, 0x30},
1907 	{0xef, 0x48},
1908 	{0xfe, 0x02},
1909 	{0x9d, 0x08},
1910 	{0xfe, 0x01},
1911 	{0x74, 0x00},
1912 
1913 	{0xfe, 0x00},
1914 	{0x7e, 0x00},
1915 	{0x7f, 0x60},
1916 	{0xfe, 0x01},
1917 	{0xa0, 0x0b},
1918 	/*AEC*/
1919 	{0xfe, 0x01},
1920 	{0x01, 0x04},
1921 	{0x02, 0x60},
1922 	{0x03, 0x02},
1923 	{0x04, 0x48},
1924 	{0x05, 0x18},
1925 	{0x06, 0x50},
1926 	{0x07, 0x10},
1927 	{0x08, 0x38},
1928 	{0x0a, 0xc0},
1929 	{0x1b, 0x04},
1930 	{0x21, 0x04},
1931 	{0xfe, 0x00},
1932 	{0x20, 0x03},
1933 	{0xfe, 0x03},
1934 	{0x12, 0x40},
1935 	{0x13, 0x06},
1936 	{0x04, 0x01},
1937 	{0x05, 0x00},
1938 	{0xfe, 0x00},
1939 	{REG_NULL, 0x00},
1940 };
1941 
1942 static const struct gc2145_framesize gc2145_dvp_framesizes[] = {
1943 	{ /* SVGA */
1944 		.width		= 800,
1945 		.height		= 600,
1946 		.max_fps = {
1947 			.numerator = 10000,
1948 			.denominator = 160000,
1949 		},
1950 		.regs		= gc2145_dvp_svga_20fps,
1951 	}, { /* SVGA */
1952 		.width		= 800,
1953 		.height		= 600,
1954 		.max_fps = {
1955 			.numerator = 10000,
1956 			.denominator = 300000,
1957 		},
1958 		.regs		= gc2145_dvp_svga_30fps,
1959 	}, { /* FULL */
1960 		.width		= 1600,
1961 		.height		= 1200,
1962 		.max_fps = {
1963 			.numerator = 10000,
1964 			.denominator = 160000,
1965 		},
1966 		.regs		= gc2145_dvp_full,
1967 	}
1968 };
1969 
1970 static const struct gc2145_framesize gc2145_mipi_framesizes[] = {
1971 	{ /* SVGA */
1972 		.width		= 800,
1973 		.height		= 600,
1974 		.max_fps = {
1975 			.numerator = 10000,
1976 			.denominator = 160000,
1977 		},
1978 		.regs		= gc2145_mipi_svga_20fps,
1979 	}, { /* SVGA */
1980 		.width		= 800,
1981 		.height		= 600,
1982 		.max_fps = {
1983 			.numerator = 10000,
1984 			.denominator = 300000,
1985 		},
1986 		.regs		= gc2145_mipi_svga_30fps,
1987 	}, { /* FULL */
1988 		.width		= 1600,
1989 		.height		= 1200,
1990 		.max_fps = {
1991 			.numerator = 10000,
1992 			.denominator = 160000,
1993 		},
1994 		.regs		= gc2145_mipi_full,
1995 	}
1996 };
1997 
1998 static const s64 link_freq_menu_items[] = {
1999 	240000000
2000 };
2001 
2002 static const struct gc2145_pixfmt gc2145_formats[] = {
2003 	{
2004 		.code = MEDIA_BUS_FMT_UYVY8_2X8,
2005 	}
2006 };
2007 
to_gc2145(struct v4l2_subdev * sd)2008 static inline struct gc2145 *to_gc2145(struct v4l2_subdev *sd)
2009 {
2010 	return container_of(sd, struct gc2145, sd);
2011 }
2012 
2013 /* sensor register write */
gc2145_write(struct i2c_client * client,u8 reg,u8 val)2014 static int gc2145_write(struct i2c_client *client, u8 reg, u8 val)
2015 {
2016 	struct i2c_msg msg;
2017 	u8 buf[2];
2018 	int ret;
2019 
2020 	dev_dbg(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val);
2021 
2022 	buf[0] = reg & 0xFF;
2023 	buf[1] = val;
2024 
2025 	msg.addr = client->addr;
2026 	msg.flags = client->flags;
2027 	msg.buf = buf;
2028 	msg.len = sizeof(buf);
2029 
2030 	ret = i2c_transfer(client->adapter, &msg, 1);
2031 	if (ret >= 0)
2032 		return 0;
2033 
2034 	dev_err(&client->dev,
2035 		"gc2145 write reg(0x%x val:0x%x) failed !\n", reg, val);
2036 
2037 	return ret;
2038 }
2039 
2040 /* sensor register read */
gc2145_read(struct i2c_client * client,u8 reg,u8 * val)2041 static int gc2145_read(struct i2c_client *client, u8 reg, u8 *val)
2042 {
2043 	struct i2c_msg msg[2];
2044 	u8 buf[1];
2045 	int ret;
2046 
2047 	buf[0] = reg & 0xFF;
2048 
2049 	msg[0].addr = client->addr;
2050 	msg[0].flags = client->flags;
2051 	msg[0].buf = buf;
2052 	msg[0].len = sizeof(buf);
2053 
2054 	msg[1].addr = client->addr;
2055 	msg[1].flags = client->flags | I2C_M_RD;
2056 	msg[1].buf = buf;
2057 	msg[1].len = 1;
2058 
2059 	ret = i2c_transfer(client->adapter, msg, 2);
2060 	if (ret >= 0) {
2061 		*val = buf[0];
2062 		return 0;
2063 	}
2064 
2065 	dev_err(&client->dev,
2066 		"gc2145 read reg:0x%x failed !\n", reg);
2067 
2068 	return ret;
2069 }
2070 
gc2145_write_array(struct i2c_client * client,const struct sensor_register * regs)2071 static int gc2145_write_array(struct i2c_client *client,
2072 			      const struct sensor_register *regs)
2073 {
2074 	int i, ret = 0;
2075 
2076 	i = 0;
2077 	while (regs[i].addr != REG_NULL) {
2078 		ret = gc2145_write(client, regs[i].addr, regs[i].value);
2079 		if (ret) {
2080 			dev_err(&client->dev, "%s failed !\n", __func__);
2081 			break;
2082 		}
2083 
2084 		i++;
2085 	}
2086 
2087 	return ret;
2088 }
2089 
gc2145_get_default_format(struct gc2145 * gc2145,struct v4l2_mbus_framefmt * format)2090 static void gc2145_get_default_format(struct gc2145 *gc2145,
2091 				      struct v4l2_mbus_framefmt *format)
2092 {
2093 	format->width = gc2145->framesize_cfg[0].width;
2094 	format->height = gc2145->framesize_cfg[0].height;
2095 	format->colorspace = V4L2_COLORSPACE_SRGB;
2096 	format->code = gc2145_formats[0].code;
2097 	format->field = V4L2_FIELD_NONE;
2098 }
2099 
gc2145_set_streaming(struct gc2145 * gc2145,int on)2100 static void gc2145_set_streaming(struct gc2145 *gc2145, int on)
2101 {
2102 	struct i2c_client *client = gc2145->client;
2103 	int ret = 0;
2104 	u8 val;
2105 
2106 	dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
2107 
2108 	if (gc2145->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
2109 		val = on ? 0x94 : 0x84;
2110 		ret = gc2145_write(client, 0xfe, 0x03);
2111 		ret |= gc2145_write(client, 0x10, val);
2112 	} else {
2113 		val = on ? 0x0f : 0;
2114 		ret = gc2145_write(client, 0xf2, val);
2115 	}
2116 	if (ret)
2117 		dev_err(&client->dev, "gc2145 soft standby failed\n");
2118 }
2119 
2120 /*
2121  * V4L2 subdev video and pad level operations
2122  */
2123 
gc2145_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)2124 static int gc2145_enum_mbus_code(struct v4l2_subdev *sd,
2125 				 struct v4l2_subdev_pad_config *cfg,
2126 				 struct v4l2_subdev_mbus_code_enum *code)
2127 {
2128 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2129 
2130 	dev_dbg(&client->dev, "%s:\n", __func__);
2131 
2132 	if (code->index >= ARRAY_SIZE(gc2145_formats))
2133 		return -EINVAL;
2134 
2135 	code->code = gc2145_formats[code->index].code;
2136 
2137 	return 0;
2138 }
2139 
gc2145_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)2140 static int gc2145_enum_frame_sizes(struct v4l2_subdev *sd,
2141 				   struct v4l2_subdev_pad_config *cfg,
2142 				   struct v4l2_subdev_frame_size_enum *fse)
2143 {
2144 	struct gc2145 *gc2145 = to_gc2145(sd);
2145 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2146 	int i = ARRAY_SIZE(gc2145_formats);
2147 
2148 	dev_dbg(&client->dev, "%s:\n", __func__);
2149 
2150 	if (fse->index >= gc2145->cfg_num)
2151 		return -EINVAL;
2152 
2153 	while (--i)
2154 		if (fse->code == gc2145_formats[i].code)
2155 			break;
2156 
2157 	fse->code = gc2145_formats[i].code;
2158 
2159 	fse->min_width  = gc2145->framesize_cfg[fse->index].width;
2160 	fse->max_width  = fse->min_width;
2161 	fse->max_height = gc2145->framesize_cfg[fse->index].height;
2162 	fse->min_height = fse->max_height;
2163 
2164 	return 0;
2165 }
2166 
gc2145_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)2167 static int gc2145_get_fmt(struct v4l2_subdev *sd,
2168 			  struct v4l2_subdev_pad_config *cfg,
2169 			  struct v4l2_subdev_format *fmt)
2170 {
2171 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2172 	struct gc2145 *gc2145 = to_gc2145(sd);
2173 
2174 	dev_dbg(&client->dev, "%s enter\n", __func__);
2175 
2176 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2177 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
2178 		struct v4l2_mbus_framefmt *mf;
2179 
2180 		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
2181 		mutex_lock(&gc2145->lock);
2182 		fmt->format = *mf;
2183 		mutex_unlock(&gc2145->lock);
2184 		return 0;
2185 #else
2186 	return -ENOTTY;
2187 #endif
2188 	}
2189 
2190 	mutex_lock(&gc2145->lock);
2191 	fmt->format = gc2145->format;
2192 	mutex_unlock(&gc2145->lock);
2193 
2194 	dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
2195 		gc2145->format.code, gc2145->format.width,
2196 		gc2145->format.height);
2197 
2198 	return 0;
2199 }
2200 
__gc2145_try_frame_size_fps(struct gc2145 * gc2145,struct v4l2_mbus_framefmt * mf,const struct gc2145_framesize ** size,unsigned int fps)2201 static void __gc2145_try_frame_size_fps(struct gc2145 *gc2145,
2202 					struct v4l2_mbus_framefmt *mf,
2203 					const struct gc2145_framesize **size,
2204 					unsigned int fps)
2205 {
2206 	const struct gc2145_framesize *fsize = &gc2145->framesize_cfg[0];
2207 	const struct gc2145_framesize *match = NULL;
2208 	unsigned int i = gc2145->cfg_num;
2209 	unsigned int min_err = UINT_MAX;
2210 
2211 	while (i--) {
2212 		unsigned int err = abs(fsize->width - mf->width)
2213 				+ abs(fsize->height - mf->height);
2214 		if (err < min_err && fsize->regs[0].addr) {
2215 			min_err = err;
2216 			match = fsize;
2217 		}
2218 		fsize++;
2219 	}
2220 
2221 	if (!match) {
2222 		match = &gc2145->framesize_cfg[0];
2223 	} else {
2224 		fsize = &gc2145->framesize_cfg[0];
2225 		for (i = 0; i < gc2145->cfg_num; i++) {
2226 			if (fsize->width == match->width &&
2227 			    fsize->height == match->height &&
2228 			    fps >= DIV_ROUND_CLOSEST(fsize->max_fps.denominator,
2229 				fsize->max_fps.numerator))
2230 				match = fsize;
2231 
2232 			fsize++;
2233 		}
2234 	}
2235 
2236 	mf->width  = match->width;
2237 	mf->height = match->height;
2238 
2239 	if (size)
2240 		*size = match;
2241 }
2242 
2243 #ifdef GC2145_EXPOSURE_CONTROL
2244 /*
2245  * the function is called before sensor register setting in VIDIOC_S_FMT
2246  */
2247 /* Row times = Hb + Sh_delay + win_width + 4*/
2248 
gc2145_aec_ctrl(struct v4l2_subdev * sd,struct v4l2_mbus_framefmt * mf)2249 static int gc2145_aec_ctrl(struct v4l2_subdev *sd,
2250 			  struct v4l2_mbus_framefmt *mf)
2251 {
2252 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2253 	int ret = 0;
2254 	u8 value;
2255 	static unsigned int capture_fps = 10, capture_lines = 1266;
2256 	static unsigned int preview_fps = 20, preview_lines = 1266;
2257 	static unsigned int lines_10ms = 1;
2258 	static unsigned int shutter_h = 0x04, shutter_l = 0xe2;
2259 	static unsigned int cap = 0, shutter = 0x04e2;
2260 
2261 	dev_info(&client->dev, "%s enter\n", __func__);
2262 	if ((mf->width == 800 && mf->height == 600) && cap == 1) {
2263 		cap = 0;
2264 		ret = gc2145_write(client, 0xfe, 0x00);
2265 		ret |= gc2145_write(client, 0xb6, 0x00);
2266 		ret |= gc2145_write(client, 0x03, shutter_h);
2267 		ret |= gc2145_write(client, 0x04, shutter_l);
2268 		ret |= gc2145_write(client, 0x82, 0xfa);
2269 		ret |= gc2145_write(client, 0xb6, 0x01);
2270 		if (ret)
2271 			dev_err(&client->dev, "gc2145 reconfig failed!\n");
2272 	}
2273 	if (mf->width == 1600 && mf->height == 1200) {
2274 		cap = 1;
2275 		ret = gc2145_write(client, 0xfe, 0x00);
2276 		ret |= gc2145_write(client, 0xb6, 0x00);
2277 		ret |= gc2145_write(client, 0x82, 0xf8);
2278 
2279 		/*shutter calculate*/
2280 		ret |= gc2145_read(client, 0x03, &value);
2281 		shutter_h = value;
2282 		shutter = (value << 8);
2283 		ret |= gc2145_read(client, 0x04, &value);
2284 		shutter_l = value;
2285 		shutter |= (value & 0xff);
2286 		dev_info(&client->dev, "%s(%d) 800x600 shutter read(0x%04x)!\n",
2287 					__func__, __LINE__, shutter);
2288 		shutter = shutter * capture_lines / preview_lines;
2289 		shutter = shutter * capture_fps / preview_fps;
2290 		lines_10ms = capture_fps * capture_lines / 100;
2291 		if (shutter > lines_10ms) {
2292 			shutter = shutter + lines_10ms / 2;
2293 			shutter /= lines_10ms;
2294 			shutter *= lines_10ms;
2295 		}
2296 		if (shutter < 1)
2297 			shutter = 0x276;
2298 		dev_info(&client->dev, "%s(%d)lines_10ms(%d),cal(0x%08x)!\n",
2299 			  __func__, __LINE__, lines_10ms, shutter);
2300 
2301 		ret |= gc2145_write(client, 0x03, ((shutter >> 8) & 0x1f));
2302 		ret |= gc2145_write(client, 0x04, (shutter & 0xff));
2303 		if (ret)
2304 			dev_err(&client->dev, "full exp reconfig failed!\n");
2305 	}
2306 	return ret;
2307 }
2308 #endif
2309 
gc2145_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)2310 static int gc2145_set_fmt(struct v4l2_subdev *sd,
2311 			  struct v4l2_subdev_pad_config *cfg,
2312 			  struct v4l2_subdev_format *fmt)
2313 {
2314 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2315 	int index = ARRAY_SIZE(gc2145_formats);
2316 	struct v4l2_mbus_framefmt *mf = &fmt->format;
2317 	const struct gc2145_framesize *size = NULL;
2318 	struct gc2145 *gc2145 = to_gc2145(sd);
2319 
2320 	dev_info(&client->dev, "%s enter\n", __func__);
2321 
2322 	__gc2145_try_frame_size_fps(gc2145, mf, &size, gc2145->fps);
2323 
2324 	while (--index >= 0)
2325 		if (gc2145_formats[index].code == mf->code)
2326 			break;
2327 
2328 	if (index < 0)
2329 		return -EINVAL;
2330 
2331 	mf->colorspace = V4L2_COLORSPACE_SRGB;
2332 	mf->code = gc2145_formats[index].code;
2333 	mf->field = V4L2_FIELD_NONE;
2334 
2335 	mutex_lock(&gc2145->lock);
2336 
2337 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2338 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
2339 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
2340 		*mf = fmt->format;
2341 #else
2342 		return -ENOTTY;
2343 #endif
2344 	} else {
2345 		if (gc2145->streaming) {
2346 			mutex_unlock(&gc2145->lock);
2347 			return -EBUSY;
2348 		}
2349 
2350 		gc2145->frame_size = size;
2351 		gc2145->format = fmt->format;
2352 
2353 	}
2354 
2355 #ifdef GC2145_EXPOSURE_CONTROL
2356 	if (gc2145->power_on)
2357 		gc2145_aec_ctrl(sd, mf);
2358 #endif
2359 	mutex_unlock(&gc2145->lock);
2360 	return 0;
2361 }
2362 
2363 static int gc2145_init(struct v4l2_subdev *sd, u32 val);
gc2145_s_stream(struct v4l2_subdev * sd,int on)2364 static int gc2145_s_stream(struct v4l2_subdev *sd, int on)
2365 {
2366 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2367 	struct gc2145 *gc2145 = to_gc2145(sd);
2368 	int ret = 0;
2369 	unsigned int fps;
2370 	unsigned int delay_us;
2371 
2372 	fps = DIV_ROUND_CLOSEST(gc2145->frame_size->max_fps.denominator,
2373 					gc2145->frame_size->max_fps.numerator);
2374 
2375 	dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
2376 				gc2145->frame_size->width,
2377 				gc2145->frame_size->height,
2378 		DIV_ROUND_CLOSEST(gc2145->frame_size->max_fps.denominator,
2379 				  gc2145->frame_size->max_fps.numerator));
2380 
2381 	mutex_lock(&gc2145->lock);
2382 
2383 	on = !!on;
2384 
2385 	if (gc2145->streaming == on)
2386 		goto unlock;
2387 
2388 	if (!on) {
2389 		/* Stop Streaming Sequence */
2390 		gc2145_set_streaming(gc2145, on);
2391 		gc2145->streaming = on;
2392 		/* delay to enable oneframe complete */
2393 		delay_us = 1000 * 1000 / fps;
2394 		usleep_range(delay_us, delay_us+10);
2395 		dev_info(&client->dev, "%s: on: %d, sleep(%dus)\n",
2396 				__func__, on, delay_us);
2397 		goto unlock;
2398 	}
2399 	if (ret)
2400 		dev_err(&client->dev, "init error\n");
2401 
2402 	ret = gc2145_write_array(client, gc2145->frame_size->regs);
2403 	if (ret)
2404 		goto unlock;
2405 
2406 	gc2145_set_streaming(gc2145, on);
2407 	gc2145->streaming = on;
2408 
2409 unlock:
2410 	mutex_unlock(&gc2145->lock);
2411 	return ret;
2412 }
2413 
gc2145_set_test_pattern(struct gc2145 * gc2145,int value)2414 static int gc2145_set_test_pattern(struct gc2145 *gc2145, int value)
2415 {
2416 	return 0;
2417 }
2418 
gc2145_s_ctrl(struct v4l2_ctrl * ctrl)2419 static int gc2145_s_ctrl(struct v4l2_ctrl *ctrl)
2420 {
2421 	struct gc2145 *gc2145 =
2422 			container_of(ctrl->handler, struct gc2145, ctrls);
2423 
2424 	switch (ctrl->id) {
2425 	case V4L2_CID_TEST_PATTERN:
2426 		return gc2145_set_test_pattern(gc2145, ctrl->val);
2427 	}
2428 
2429 	return 0;
2430 }
2431 
2432 static const struct v4l2_ctrl_ops gc2145_ctrl_ops = {
2433 	.s_ctrl = gc2145_s_ctrl,
2434 };
2435 
2436 static const char * const gc2145_test_pattern_menu[] = {
2437 	"Disabled",
2438 	"Vertical Color Bars",
2439 };
2440 
2441 /* -----------------------------------------------------------------------------
2442  * V4L2 subdev internal operations
2443  */
2444 
2445 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
gc2145_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)2446 static int gc2145_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2447 {
2448 	struct gc2145 *gc2145 = to_gc2145(sd);
2449 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2450 	struct v4l2_mbus_framefmt *format =
2451 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
2452 
2453 	dev_dbg(&client->dev, "%s:\n", __func__);
2454 
2455 	gc2145_get_default_format(gc2145, format);
2456 
2457 	return 0;
2458 }
2459 #endif
2460 
gc2145_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)2461 static int gc2145_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
2462 				struct v4l2_mbus_config *config)
2463 {
2464 	struct gc2145 *gc2145 = to_gc2145(sd);
2465 
2466 	if (gc2145->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
2467 		config->type = V4L2_MBUS_CSI2_DPHY;
2468 		config->flags = V4L2_MBUS_CSI2_1_LANE |
2469 						V4L2_MBUS_CSI2_CHANNEL_0 |
2470 						V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
2471 	} else {
2472 		config->type = V4L2_MBUS_PARALLEL;
2473 		config->flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2474 				V4L2_MBUS_VSYNC_ACTIVE_LOW |
2475 				V4L2_MBUS_PCLK_SAMPLE_RISING;
2476 	}
2477 
2478 	return 0;
2479 }
2480 
gc2145_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)2481 static int gc2145_g_frame_interval(struct v4l2_subdev *sd,
2482 				   struct v4l2_subdev_frame_interval *fi)
2483 {
2484 	struct gc2145 *gc2145 = to_gc2145(sd);
2485 
2486 	fi->interval = gc2145->frame_size->max_fps;
2487 
2488 	return 0;
2489 }
2490 
gc2145_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)2491 static int gc2145_s_frame_interval(struct v4l2_subdev *sd,
2492 				   struct v4l2_subdev_frame_interval *fi)
2493 {
2494 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2495 	struct gc2145 *gc2145 = to_gc2145(sd);
2496 	const struct gc2145_framesize *size = NULL;
2497 	struct v4l2_mbus_framefmt mf;
2498 	unsigned int fps;
2499 	int ret = 0;
2500 
2501 	dev_dbg(&client->dev, "Setting %d/%d frame interval\n",
2502 		fi->interval.numerator, fi->interval.denominator);
2503 
2504 	mutex_lock(&gc2145->lock);
2505 
2506 	if (gc2145->format.width == 1600)
2507 		goto unlock;
2508 
2509 	fps = DIV_ROUND_CLOSEST(fi->interval.denominator,
2510 				fi->interval.numerator);
2511 	mf = gc2145->format;
2512 	__gc2145_try_frame_size_fps(gc2145, &mf, &size, fps);
2513 
2514 	if (gc2145->frame_size != size) {
2515 		dev_info(&client->dev, "%s match wxh@FPS is %dx%d@%d\n",
2516 			__func__, size->width, size->height,
2517 			DIV_ROUND_CLOSEST(size->max_fps.denominator,
2518 				size->max_fps.numerator));
2519 		ret |= gc2145_write_array(client, size->regs);
2520 		if (ret)
2521 			goto unlock;
2522 		gc2145->frame_size = size;
2523 		gc2145->fps = fps;
2524 	}
2525 unlock:
2526 	mutex_unlock(&gc2145->lock);
2527 
2528 	return ret;
2529 }
2530 
gc2145_get_module_inf(struct gc2145 * gc2145,struct rkmodule_inf * inf)2531 static void gc2145_get_module_inf(struct gc2145 *gc2145,
2532 				  struct rkmodule_inf *inf)
2533 {
2534 	memset(inf, 0, sizeof(*inf));
2535 	strlcpy(inf->base.sensor, DRIVER_NAME, sizeof(inf->base.sensor));
2536 	strlcpy(inf->base.module, gc2145->module_name,
2537 		sizeof(inf->base.module));
2538 	strlcpy(inf->base.lens, gc2145->len_name, sizeof(inf->base.lens));
2539 }
2540 
gc2145_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)2541 static long gc2145_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2542 {
2543 	struct gc2145 *gc2145 = to_gc2145(sd);
2544 	long ret = 0;
2545 	u32 stream = 0;
2546 
2547 	switch (cmd) {
2548 	case RKMODULE_GET_MODULE_INFO:
2549 		gc2145_get_module_inf(gc2145, (struct rkmodule_inf *)arg);
2550 		break;
2551 	case RKMODULE_SET_QUICK_STREAM:
2552 
2553 		stream = *((u32 *)arg);
2554 
2555 		gc2145_set_streaming(gc2145, !!stream);
2556 		break;
2557 	default:
2558 		ret = -ENOIOCTLCMD;
2559 		break;
2560 	}
2561 
2562 	return ret;
2563 }
2564 
2565 #ifdef CONFIG_COMPAT
gc2145_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)2566 static long gc2145_compat_ioctl32(struct v4l2_subdev *sd,
2567 				  unsigned int cmd, unsigned long arg)
2568 {
2569 	void __user *up = compat_ptr(arg);
2570 	struct rkmodule_inf *inf;
2571 	struct rkmodule_awb_cfg *cfg;
2572 	long ret;
2573 	u32 stream = 0;
2574 
2575 	switch (cmd) {
2576 	case RKMODULE_GET_MODULE_INFO:
2577 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
2578 		if (!inf) {
2579 			ret = -ENOMEM;
2580 			return ret;
2581 		}
2582 
2583 		ret = gc2145_ioctl(sd, cmd, inf);
2584 		if (!ret) {
2585 			ret = copy_to_user(up, inf, sizeof(*inf));
2586 			if (ret)
2587 				ret = -EFAULT;
2588 		}
2589 		kfree(inf);
2590 		break;
2591 	case RKMODULE_AWB_CFG:
2592 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2593 		if (!cfg) {
2594 			ret = -ENOMEM;
2595 			return ret;
2596 		}
2597 
2598 		ret = copy_from_user(cfg, up, sizeof(*cfg));
2599 		if (!ret)
2600 			ret = gc2145_ioctl(sd, cmd, cfg);
2601 		else
2602 			ret = -EFAULT;
2603 		kfree(cfg);
2604 		break;
2605 	case RKMODULE_SET_QUICK_STREAM:
2606 		ret = copy_from_user(&stream, up, sizeof(u32));
2607 		if (!ret)
2608 			ret = gc2145_ioctl(sd, cmd, &stream);
2609 		else
2610 			ret = -EFAULT;
2611 		break;
2612 	default:
2613 		ret = -ENOIOCTLCMD;
2614 		break;
2615 	}
2616 
2617 	return ret;
2618 }
2619 #endif
2620 
gc2145_init(struct v4l2_subdev * sd,u32 val)2621 static int gc2145_init(struct v4l2_subdev *sd, u32 val)
2622 {
2623 	int ret;
2624 	struct gc2145 *gc2145 = to_gc2145(sd);
2625 	struct i2c_client *client = gc2145->client;
2626 
2627 	dev_info(&client->dev, "%s(%d)\n", __func__, __LINE__);
2628 
2629 	if (gc2145->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY)
2630 		ret = gc2145_write_array(client, gc2145_mipi_init_regs);
2631 	else
2632 		ret = gc2145_write_array(client, gc2145_dvp_init_regs);
2633 
2634 	return ret;
2635 }
2636 
gc2145_power(struct v4l2_subdev * sd,int on)2637 static int gc2145_power(struct v4l2_subdev *sd, int on)
2638 {
2639 	int ret;
2640 	struct gc2145 *gc2145 = to_gc2145(sd);
2641 	struct i2c_client *client = gc2145->client;
2642 
2643 	dev_info(&client->dev, "%s(%d) on(%d)\n", __func__, __LINE__, on);
2644 	if (on) {
2645 		if (!IS_ERR(gc2145->pwdn_gpio)) {
2646 			gpiod_set_value_cansleep(gc2145->pwdn_gpio, 0);
2647 			usleep_range(2000, 5000);
2648 		}
2649 		ret = gc2145_init(sd, 0);
2650 		usleep_range(10000, 20000);
2651 		if (ret)
2652 			dev_err(&client->dev, "init error\n");
2653 		gc2145->power_on = true;
2654 	} else {
2655 		if (!IS_ERR(gc2145->pwdn_gpio)) {
2656 			gpiod_set_value_cansleep(gc2145->pwdn_gpio, 1);
2657 			usleep_range(2000, 5000);
2658 		}
2659 		gc2145->power_on = false;
2660 	}
2661 	return 0;
2662 }
2663 
gc2145_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)2664 static int gc2145_enum_frame_interval(struct v4l2_subdev *sd,
2665 				       struct v4l2_subdev_pad_config *cfg,
2666 				       struct v4l2_subdev_frame_interval_enum *fie)
2667 {
2668 	struct gc2145 *gc2145 = to_gc2145(sd);
2669 
2670 	if (fie->index >= gc2145->cfg_num)
2671 		return -EINVAL;
2672 
2673 	fie->code = MEDIA_BUS_FMT_UYVY8_2X8;
2674 	fie->width = gc2145->framesize_cfg[fie->index].width;
2675 	fie->height = gc2145->framesize_cfg[fie->index].height;
2676 	fie->interval = gc2145->framesize_cfg[fie->index].max_fps;
2677 	return 0;
2678 }
2679 
2680 static const struct v4l2_subdev_core_ops gc2145_subdev_core_ops = {
2681 	.log_status = v4l2_ctrl_subdev_log_status,
2682 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2683 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
2684 	.ioctl = gc2145_ioctl,
2685 #ifdef CONFIG_COMPAT
2686 	.compat_ioctl32 = gc2145_compat_ioctl32,
2687 #endif
2688 	.s_power = gc2145_power,
2689 };
2690 
2691 static const struct v4l2_subdev_video_ops gc2145_subdev_video_ops = {
2692 	.s_stream = gc2145_s_stream,
2693 	.g_frame_interval = gc2145_g_frame_interval,
2694 	.s_frame_interval = gc2145_s_frame_interval,
2695 };
2696 
2697 static const struct v4l2_subdev_pad_ops gc2145_subdev_pad_ops = {
2698 	.enum_mbus_code = gc2145_enum_mbus_code,
2699 	.enum_frame_size = gc2145_enum_frame_sizes,
2700 	.enum_frame_interval = gc2145_enum_frame_interval,
2701 	.get_fmt = gc2145_get_fmt,
2702 	.set_fmt = gc2145_set_fmt,
2703 	.get_mbus_config = gc2145_g_mbus_config,
2704 };
2705 
2706 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
2707 static const struct v4l2_subdev_ops gc2145_subdev_ops = {
2708 	.core  = &gc2145_subdev_core_ops,
2709 	.video = &gc2145_subdev_video_ops,
2710 	.pad   = &gc2145_subdev_pad_ops,
2711 };
2712 
2713 static const struct v4l2_subdev_internal_ops gc2145_subdev_internal_ops = {
2714 	.open = gc2145_open,
2715 };
2716 #endif
2717 
gc2145_detect(struct gc2145 * gc2145)2718 static int gc2145_detect(struct gc2145 *gc2145)
2719 {
2720 	struct i2c_client *client = gc2145->client;
2721 	u8 pid, ver;
2722 	int ret;
2723 
2724 	dev_dbg(&client->dev, "%s:\n", __func__);
2725 
2726 	/* Check sensor revision */
2727 	ret = gc2145_read(client, REG_SC_CHIP_ID_H, &pid);
2728 	if (!ret)
2729 		ret = gc2145_read(client, REG_SC_CHIP_ID_L, &ver);
2730 
2731 	if (!ret) {
2732 		unsigned short id;
2733 
2734 		id = SENSOR_ID(pid, ver);
2735 		if (id != GC2145_ID) {
2736 			ret = -1;
2737 			dev_err(&client->dev,
2738 				"Sensor detection failed (%04X, %d)\n",
2739 				id, ret);
2740 		} else {
2741 			dev_info(&client->dev, "Found GC%04X sensor\n", id);
2742 			if (!IS_ERR(gc2145->pwdn_gpio))
2743 				gpiod_set_value_cansleep(gc2145->pwdn_gpio, 1);
2744 		}
2745 	}
2746 
2747 	return ret;
2748 }
2749 
__gc2145_power_on(struct gc2145 * gc2145)2750 static int __gc2145_power_on(struct gc2145 *gc2145)
2751 {
2752 	int ret;
2753 	struct device *dev = &gc2145->client->dev;
2754 
2755 	dev_info(dev, "%s(%d)\n", __func__, __LINE__);
2756 	if (!IS_ERR(gc2145->power_gpio)) {
2757 		gpiod_set_value_cansleep(gc2145->power_gpio, 1);
2758 		usleep_range(2000, 5000);
2759 	}
2760 
2761 	if (!IS_ERR(gc2145->reset_gpio)) {
2762 		gpiod_set_value_cansleep(gc2145->reset_gpio, 0);
2763 		usleep_range(2000, 5000);
2764 		gpiod_set_value_cansleep(gc2145->reset_gpio, 1);
2765 		usleep_range(2000, 5000);
2766 	}
2767 
2768 	if (!IS_ERR(gc2145->xvclk)) {
2769 		ret = clk_set_rate(gc2145->xvclk, 24000000);
2770 		if (ret < 0)
2771 			dev_info(dev, "Failed to set xvclk rate (24MHz)\n");
2772 	}
2773 
2774 	if (!IS_ERR(gc2145->pwdn_gpio)) {
2775 		gpiod_set_value_cansleep(gc2145->pwdn_gpio, 1);
2776 		usleep_range(2000, 5000);
2777 	}
2778 
2779 	if (!IS_ERR(gc2145->supplies)) {
2780 		ret = regulator_bulk_enable(GC2145_NUM_SUPPLIES,
2781 			gc2145->supplies);
2782 		if (ret < 0)
2783 			dev_info(dev, "Failed to enable regulators\n");
2784 
2785 		usleep_range(20000, 50000);
2786 	}
2787 
2788 	if (!IS_ERR(gc2145->pwdn_gpio)) {
2789 		gpiod_set_value_cansleep(gc2145->pwdn_gpio, 0);
2790 		usleep_range(2000, 5000);
2791 	}
2792 
2793 	if (!IS_ERR(gc2145->reset_gpio)) {
2794 		gpiod_set_value_cansleep(gc2145->reset_gpio, 0);
2795 		usleep_range(2000, 5000);
2796 	}
2797 
2798 	if (!IS_ERR(gc2145->xvclk)) {
2799 		ret = clk_prepare_enable(gc2145->xvclk);
2800 		if (ret < 0)
2801 			dev_info(dev, "Failed to enable xvclk\n");
2802 	}
2803 	usleep_range(7000, 10000);
2804 	gc2145->power_on = true;
2805 	return 0;
2806 }
2807 
__gc2145_power_off(struct gc2145 * gc2145)2808 static void __gc2145_power_off(struct gc2145 *gc2145)
2809 {
2810 	dev_info(&gc2145->client->dev, "%s(%d)\n", __func__, __LINE__);
2811 	if (!IS_ERR(gc2145->xvclk))
2812 		clk_disable_unprepare(gc2145->xvclk);
2813 	if (!IS_ERR(gc2145->supplies))
2814 		regulator_bulk_disable(GC2145_NUM_SUPPLIES, gc2145->supplies);
2815 	if (!IS_ERR(gc2145->pwdn_gpio))
2816 		gpiod_set_value_cansleep(gc2145->pwdn_gpio, 1);
2817 	if (!IS_ERR(gc2145->reset_gpio))
2818 		gpiod_set_value_cansleep(gc2145->reset_gpio, 0);
2819 	if (!IS_ERR(gc2145->power_gpio))
2820 		gpiod_set_value_cansleep(gc2145->power_gpio, 0);
2821 	gc2145->power_on = false;
2822 }
2823 
gc2145_configure_regulators(struct gc2145 * gc2145)2824 static int gc2145_configure_regulators(struct gc2145 *gc2145)
2825 {
2826 	unsigned int i;
2827 
2828 	for (i = 0; i < GC2145_NUM_SUPPLIES; i++)
2829 		gc2145->supplies[i].supply = gc2145_supply_names[i];
2830 
2831 	return devm_regulator_bulk_get(&gc2145->client->dev,
2832 				       GC2145_NUM_SUPPLIES,
2833 				       gc2145->supplies);
2834 }
2835 
gc2145_parse_of(struct gc2145 * gc2145)2836 static int gc2145_parse_of(struct gc2145 *gc2145)
2837 {
2838 	struct device *dev = &gc2145->client->dev;
2839 	struct device_node *endpoint;
2840 	int ret;
2841 
2842 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
2843 	if (!endpoint) {
2844 		dev_err(dev, "Failed to get endpoint\n");
2845 		return -EINVAL;
2846 	}
2847 
2848 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
2849 		&gc2145->bus_cfg);
2850 	if (ret) {
2851 		dev_err(dev, "Failed to parse endpoint\n");
2852 		of_node_put(endpoint);
2853 		return ret;
2854 	}
2855 	if (gc2145->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
2856 		gc2145->framesize_cfg = gc2145_mipi_framesizes;
2857 		gc2145->cfg_num = ARRAY_SIZE(gc2145_mipi_framesizes);
2858 	} else {
2859 		gc2145->framesize_cfg = gc2145_dvp_framesizes;
2860 		gc2145->cfg_num = ARRAY_SIZE(gc2145_dvp_framesizes);
2861 	}
2862 
2863 	gc2145->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
2864 	if (IS_ERR(gc2145->power_gpio))
2865 		dev_info(dev, "Failed to get power-gpios, maybe no use\n");
2866 	gc2145->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
2867 	if (IS_ERR(gc2145->pwdn_gpio))
2868 		dev_info(dev, "Failed to get pwdn-gpios, maybe no use\n");
2869 	gc2145->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
2870 	if (IS_ERR(gc2145->reset_gpio))
2871 		dev_info(dev, "Failed to get reset-gpios, maybe no use\n");
2872 
2873 	ret = gc2145_configure_regulators(gc2145);
2874 	if (ret)
2875 		dev_info(dev, "Failed to get power regulators\n");
2876 
2877 	return ret;
2878 }
2879 
gc2145_probe(struct i2c_client * client,const struct i2c_device_id * id)2880 static int gc2145_probe(struct i2c_client *client,
2881 			const struct i2c_device_id *id)
2882 {
2883 	struct device *dev = &client->dev;
2884 	struct device_node *node = dev->of_node;
2885 	struct v4l2_subdev *sd;
2886 	struct gc2145 *gc2145;
2887 	char facing[2];
2888 	int ret;
2889 
2890 	dev_info(dev, "driver version: %02x.%02x.%02x",
2891 		DRIVER_VERSION >> 16,
2892 		(DRIVER_VERSION & 0xff00) >> 8,
2893 		DRIVER_VERSION & 0x00ff);
2894 
2895 	gc2145 = devm_kzalloc(&client->dev, sizeof(*gc2145), GFP_KERNEL);
2896 	if (!gc2145)
2897 		return -ENOMEM;
2898 
2899 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
2900 				   &gc2145->module_index);
2901 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
2902 				       &gc2145->module_facing);
2903 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
2904 				       &gc2145->module_name);
2905 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
2906 				       &gc2145->len_name);
2907 	if (ret) {
2908 		dev_err(dev, "could not get module information!\n");
2909 		return -EINVAL;
2910 	}
2911 
2912 	gc2145->client = client;
2913 	gc2145->xvclk = devm_clk_get(&client->dev, "xvclk");
2914 	if (IS_ERR(gc2145->xvclk)) {
2915 		dev_err(&client->dev, "Failed to get xvclk\n");
2916 		return -EINVAL;
2917 	}
2918 
2919 	ret = gc2145_parse_of(gc2145);
2920 	if (ret != 0)
2921 		return -EINVAL;
2922 
2923 	v4l2_ctrl_handler_init(&gc2145->ctrls, 3);
2924 	gc2145->link_frequency =
2925 			v4l2_ctrl_new_std(&gc2145->ctrls, &gc2145_ctrl_ops,
2926 					  V4L2_CID_PIXEL_RATE, 0,
2927 					  GC2145_PIXEL_RATE, 1,
2928 					  GC2145_PIXEL_RATE);
2929 
2930 	v4l2_ctrl_new_int_menu(&gc2145->ctrls, NULL, V4L2_CID_LINK_FREQ,
2931 			       0, 0, link_freq_menu_items);
2932 	v4l2_ctrl_new_std_menu_items(&gc2145->ctrls, &gc2145_ctrl_ops,
2933 				     V4L2_CID_TEST_PATTERN,
2934 				     ARRAY_SIZE(gc2145_test_pattern_menu) - 1,
2935 				     0, 0, gc2145_test_pattern_menu);
2936 	gc2145->sd.ctrl_handler = &gc2145->ctrls;
2937 
2938 	if (gc2145->ctrls.error) {
2939 		dev_err(&client->dev, "%s: control initialization error %d\n",
2940 			__func__, gc2145->ctrls.error);
2941 		return  gc2145->ctrls.error;
2942 	}
2943 
2944 	sd = &gc2145->sd;
2945 	client->flags |= I2C_CLIENT_SCCB;
2946 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
2947 	v4l2_i2c_subdev_init(sd, client, &gc2145_subdev_ops);
2948 
2949 	sd->internal_ops = &gc2145_subdev_internal_ops;
2950 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2951 		     V4L2_SUBDEV_FL_HAS_EVENTS;
2952 #endif
2953 
2954 #if defined(CONFIG_MEDIA_CONTROLLER)
2955 	gc2145->pad.flags = MEDIA_PAD_FL_SOURCE;
2956 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
2957 	ret = media_entity_pads_init(&sd->entity, 1, &gc2145->pad);
2958 	if (ret < 0) {
2959 		v4l2_ctrl_handler_free(&gc2145->ctrls);
2960 		return ret;
2961 	}
2962 #endif
2963 
2964 	mutex_init(&gc2145->lock);
2965 
2966 	gc2145_get_default_format(gc2145, &gc2145->format);
2967 	gc2145->frame_size = &gc2145->framesize_cfg[0];
2968 	gc2145->format.width = gc2145->framesize_cfg[0].width;
2969 	gc2145->format.height = gc2145->framesize_cfg[0].height;
2970 	gc2145->fps = DIV_ROUND_CLOSEST(gc2145->framesize_cfg[0].max_fps.denominator,
2971 			gc2145->framesize_cfg[0].max_fps.numerator);
2972 
2973 	__gc2145_power_on(gc2145);
2974 	gc2145->xvclk_frequency = clk_get_rate(gc2145->xvclk);
2975 	if (gc2145->xvclk_frequency < 6000000 ||
2976 	    gc2145->xvclk_frequency > 27000000)
2977 		goto error;
2978 
2979 	ret = gc2145_detect(gc2145);
2980 	if (ret < 0) {
2981 		dev_info(&client->dev, "Check id  failed:\n"
2982 			  "check following information:\n"
2983 			  "Power/PowerDown/Reset/Mclk/I2cBus !!\n");
2984 		goto error;
2985 	}
2986 
2987 	memset(facing, 0, sizeof(facing));
2988 	if (strcmp(gc2145->module_facing, "back") == 0)
2989 		facing[0] = 'b';
2990 	else
2991 		facing[0] = 'f';
2992 
2993 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
2994 		 gc2145->module_index, facing,
2995 		 DRIVER_NAME, dev_name(sd->dev));
2996 	ret = v4l2_async_register_subdev_sensor_common(sd);
2997 	if (ret)
2998 		goto error;
2999 
3000 	dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
3001 	gc2145->power_on = false;
3002 	return 0;
3003 
3004 error:
3005 	v4l2_ctrl_handler_free(&gc2145->ctrls);
3006 #if defined(CONFIG_MEDIA_CONTROLLER)
3007 	media_entity_cleanup(&sd->entity);
3008 #endif
3009 	mutex_destroy(&gc2145->lock);
3010 	__gc2145_power_off(gc2145);
3011 	return ret;
3012 }
3013 
gc2145_remove(struct i2c_client * client)3014 static int gc2145_remove(struct i2c_client *client)
3015 {
3016 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
3017 	struct gc2145 *gc2145 = to_gc2145(sd);
3018 
3019 	v4l2_ctrl_handler_free(&gc2145->ctrls);
3020 	v4l2_async_unregister_subdev(sd);
3021 #if defined(CONFIG_MEDIA_CONTROLLER)
3022 	media_entity_cleanup(&sd->entity);
3023 #endif
3024 	mutex_destroy(&gc2145->lock);
3025 
3026 	__gc2145_power_off(gc2145);
3027 
3028 	return 0;
3029 }
3030 
3031 static const struct i2c_device_id gc2145_id[] = {
3032 	{ "gc2145", 0 },
3033 	{ /* sentinel */ },
3034 };
3035 MODULE_DEVICE_TABLE(i2c, gc2145_id);
3036 
3037 #if IS_ENABLED(CONFIG_OF)
3038 static const struct of_device_id gc2145_of_match[] = {
3039 	{ .compatible = "galaxycore,gc2145", },
3040 	{ /* sentinel */ },
3041 };
3042 MODULE_DEVICE_TABLE(of, gc2145_of_match);
3043 #endif
3044 
3045 static struct i2c_driver gc2145_i2c_driver = {
3046 	.driver = {
3047 		.name	= DRIVER_NAME,
3048 		.of_match_table = of_match_ptr(gc2145_of_match),
3049 	},
3050 	.probe		= gc2145_probe,
3051 	.remove		= gc2145_remove,
3052 	.id_table	= gc2145_id,
3053 };
3054 
sensor_mod_init(void)3055 static int __init sensor_mod_init(void)
3056 {
3057 	return i2c_add_driver(&gc2145_i2c_driver);
3058 }
3059 
sensor_mod_exit(void)3060 static void __exit sensor_mod_exit(void)
3061 {
3062 	i2c_del_driver(&gc2145_i2c_driver);
3063 }
3064 
3065 device_initcall_sync(sensor_mod_init);
3066 module_exit(sensor_mod_exit);
3067 
3068 MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
3069 MODULE_DESCRIPTION("GC2145 CMOS Image Sensor driver");
3070 MODULE_LICENSE("GPL v2");
3071