xref: /OK3568_Linux_fs/kernel/drivers/video/rockchip/vehicle/vehicle_ad_gc2145.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * vehicle sensor gc2145
4  *
5  * Copyright (C) 2020 Rockchip Electronics Co.Ltd
6  * Authors:
7  *      Zhiqin Wei <wzq@rock-chips.com>
8  *
9  */
10 
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/sysctl.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/proc_fs.h>
21 #include <linux/suspend.h>
22 #include <linux/delay.h>
23 #include <linux/io.h>
24 #include <linux/irq.h>
25 #include <linux/uaccess.h>
26 #include <linux/of_gpio.h>
27 #include <linux/of_irq.h>
28 #include "vehicle_cfg.h"
29 #include "vehicle_main.h"
30 #include "vehicle_ad.h"
31 #include "vehicle_ad_gc2145.h"
32 #include <media/v4l2-mediabus.h>
33 
34 enum {
35 	CVSTD_720P60 = 0,
36 	CVSTD_720P50,
37 	CVSTD_1080P30,
38 	CVSTD_1080P25,
39 	CVSTD_720P30,
40 	CVSTD_720P25,
41 	CVSTD_SVGAP30,
42 	CVSTD_SD,
43 	CVSTD_NTSC,
44 	CVSTD_PAL
45 };
46 
47 enum {
48 	FORCE_PAL_WIDTH = 960,
49 	FORCE_PAL_HEIGHT = 576,
50 	FORCE_NTSC_WIDTH = 960,
51 	FORCE_NTSC_HEIGHT = 480,
52 	FORCE_SVGA_WIDTH = 800,
53 	FORCE_SVGA_HEIGHT = 600,
54 	FORCE_CIF_OUTPUT_FORMAT = CIF_OUTPUT_FORMAT_422,
55 };
56 
57 enum {
58 	VIDEO_UNPLUG,
59 	VIDEO_IN,
60 	VIDEO_LOCKED,
61 	VIDEO_UNLOCK
62 };
63 #define FLAG_LOSS			(0x1 << 7)
64 #define FLAG_V_LOCKED			(0x1 << 6)
65 #define FLAG_H_LOCKED			(0x1 << 5)
66 #define FLAG_CARRIER_PLL_LOCKED		(0x1 << 4)
67 #define FLAG_VIDEO_DETECTED		(0x1 << 3)
68 #define FLAG_EQ_SD_DETECTED		(0x1 << 2)
69 #define FLAG_PROGRESSIVE		(0x1 << 1)
70 #define FLAG_NO_CARRIER			(0x1 << 0)
71 #define FLAG_LOCKED			(FLAG_V_LOCKED | FLAG_H_LOCKED)
72 
73 static struct vehicle_ad_dev *gc2145_g_addev;
74 static int cvstd_mode = CVSTD_SVGAP30;
75 static int cvstd_old = CVSTD_NTSC;
76 //static int cvstd_sd = CVSTD_NTSC;
77 static int cvstd_state = VIDEO_UNPLUG;
78 static int cvstd_old_state = VIDEO_UNLOCK;
79 
80 #define SENSOR_REGISTER_LEN	1	/* sensor register address bytes*/
81 #define SENSOR_VALUE_LEN	1	/* sensor register value bytes*/
82 
83 struct rk_sensor_reg {
84 	unsigned int reg;
85 	unsigned int val;
86 };
87 
88 #define SENSOR_CHANNEL_REG		0x41
89 
90 #define SEQCMD_END  0xFF000000
91 #define SensorEnd   {SEQCMD_END, 0x00}
92 
93 #define SENSOR_DG VEHICLE_DG
94 #define SENSOR_ID(_msb, _lsb)		((_msb) << 8 | (_lsb))
95 
96 /* Preview resolution setting*/
97 static struct rk_sensor_reg sensor_preview_data_svga_30hz[] = {
98 	{0xfe, 0xf0},
99 	{0xfe, 0xf0},
100 	{0xfe, 0xf0},
101 	{0xfc, 0x06},
102 	{0xf6, 0x00},
103 	{0xf7, 0x1d},
104 	{0xf8, 0x84},
105 	{0xfa, 0x00},
106 	{0xf9, 0xfe},
107 	{0xf2, 0x00},
108 	/*ISP reg*/
109 	{0xfe, 0x00},
110 	{0x03, 0x04},
111 	{0x04, 0xe2},
112 	{0x09, 0x00},
113 	{0x0a, 0x00},
114 	{0x0b, 0x00},
115 	{0x0c, 0x00},
116 	{0x0d, 0x04},
117 	{0x0e, 0xc0},
118 	{0x0f, 0x06},
119 	{0x10, 0x52},
120 	{0x12, 0x2e},
121 	{0x17, 0x14},
122 	{0x18, 0x22},
123 	{0x19, 0x0e},
124 	{0x1a, 0x01},
125 	{0x1b, 0x4b},
126 	{0x1c, 0x07},
127 	{0x1d, 0x10},
128 	{0x1e, 0x88},
129 	{0x1f, 0x78},
130 	{0x20, 0x03},
131 	{0x21, 0x40},
132 	{0x22, 0xa0},
133 	{0x24, 0x3f},
134 	{0x25, 0x01},
135 	{0x26, 0x10},
136 	{0x2d, 0x60},
137 	{0x30, 0x01},
138 	{0x31, 0x90},
139 	{0x33, 0x06},
140 	{0x34, 0x01},
141 	{0xfe, 0x00},
142 	{0x80, 0x7f},
143 	{0x81, 0x26},
144 	{0x82, 0xfa},
145 	{0x83, 0x00},
146 	{0x84, 0x00},
147 	{0x86, 0x02},
148 	{0x88, 0x03},
149 	{0x89, 0x03},
150 	{0x85, 0x08},
151 	{0x8a, 0x00},
152 	{0x8b, 0x00},
153 	{0xb0, 0x55},
154 	{0xc3, 0x00},
155 	{0xc4, 0x80},
156 	{0xc5, 0x90},
157 	{0xc6, 0x3b},
158 	{0xc7, 0x46},
159 	{0xec, 0x06},
160 	{0xed, 0x04},
161 	{0xee, 0x60},
162 	{0xef, 0x90},
163 	{0xb6, 0x01},
164 	{0x90, 0x01},
165 	{0x91, 0x00},
166 	{0x92, 0x00},
167 	{0x93, 0x00},
168 	{0x94, 0x00},
169 	{0x95, 0x04},
170 	{0x96, 0xb0},
171 	{0x97, 0x06},
172 	{0x98, 0x40},
173 	/*BLK*/
174 	{0xfe, 0x00},
175 	{0x40, 0x42},
176 	{0x41, 0x00},
177 	{0x43, 0x5b},
178 	{0x5e, 0x00},
179 	{0x5f, 0x00},
180 	{0x60, 0x00},
181 	{0x61, 0x00},
182 	{0x62, 0x00},
183 	{0x63, 0x00},
184 	{0x64, 0x00},
185 	{0x65, 0x00},
186 	{0x66, 0x20},
187 	{0x67, 0x20},
188 	{0x68, 0x20},
189 	{0x69, 0x20},
190 	{0x76, 0x00},
191 	{0x6a, 0x08},
192 	{0x6b, 0x08},
193 	{0x6c, 0x08},
194 	{0x6d, 0x08},
195 	{0x6e, 0x08},
196 	{0x6f, 0x08},
197 	{0x70, 0x08},
198 	{0x71, 0x08},
199 	{0x76, 0x00},
200 	{0x72, 0xf0},
201 	{0x7e, 0x3c},
202 	{0x7f, 0x00},
203 	{0xfe, 0x02},
204 	{0x48, 0x15},
205 	{0x49, 0x00},
206 	{0x4b, 0x0b},
207 	{0xfe, 0x00},
208 	/*AEC*/
209 	{0xfe, 0x01},
210 	{0x01, 0x04},
211 	{0x02, 0xc0},
212 	{0x03, 0x04},
213 	{0x04, 0x90},
214 	{0x05, 0x30},
215 	{0x06, 0x90},
216 	{0x07, 0x30},
217 	{0x08, 0x80},
218 	{0x09, 0x00},
219 	{0x0a, 0x82},
220 	{0x0b, 0x11},
221 	{0x0c, 0x10},
222 	{0x11, 0x10},
223 	{0x13, 0x7b},
224 	{0x17, 0x00},
225 	{0x1c, 0x11},
226 	{0x1e, 0x61},
227 	{0x1f, 0x35},
228 	{0x20, 0x40},
229 	{0x22, 0x40},
230 	{0x23, 0x20},
231 	{0xfe, 0x02},
232 	{0x0f, 0x04},
233 	{0xfe, 0x01},
234 	{0x12, 0x35},
235 	{0x15, 0xb0},
236 	{0x10, 0x31},
237 	{0x3e, 0x28},
238 	{0x3f, 0xb0},
239 	{0x40, 0x90},
240 	{0x41, 0x0f},
241 
242 	/*INTPEE*/
243 	{0xfe, 0x02},
244 	{0x90, 0x6c},
245 	{0x91, 0x03},
246 	{0x92, 0xcb},
247 	{0x94, 0x33},
248 	{0x95, 0x84},
249 	{0x97, 0x45},
250 	{0xa2, 0x11},
251 	{0xfe, 0x00},
252 	/*DNDD*/
253 	{0xfe, 0x02},
254 	{0x80, 0xc1},
255 	{0x81, 0x08},
256 	{0x82, 0x1f},
257 	{0x83, 0x10},
258 	{0x84, 0x0a},
259 	{0x86, 0xf0},
260 	{0x87, 0x50},
261 	{0x88, 0x15},
262 	{0x89, 0xb0},
263 	{0x8a, 0x30},
264 	{0x8b, 0x10},
265 	/*ASDE*/
266 	{0xfe, 0x01},
267 	{0x21, 0x04},
268 	{0xfe, 0x02},
269 	{0xa3, 0x50},
270 	{0xa4, 0x20},
271 	{0xa5, 0x40},
272 	{0xa6, 0x80},
273 	{0xab, 0x40},
274 	{0xae, 0x0c},
275 	{0xb3, 0x46},
276 	{0xb4, 0x64},
277 	{0xb6, 0x38},
278 	{0xb7, 0x01},
279 	{0xb9, 0x2b},
280 	{0x3c, 0x04},
281 	{0x3d, 0x15},
282 	{0x4b, 0x06},
283 	{0x4c, 0x20},
284 	{0xfe, 0x00},
285 	/*GAMMA*/
286 	/*gamma1*/
287 	{0xfe, 0x02},
288 	{0x10, 0x09},
289 	{0x11, 0x0d},
290 	{0x12, 0x13},
291 	{0x13, 0x19},
292 	{0x14, 0x27},
293 	{0x15, 0x37},
294 	{0x16, 0x45},
295 	{0x17, 0x53},
296 	{0x18, 0x69},
297 	{0x19, 0x7d},
298 	{0x1a, 0x8f},
299 	{0x1b, 0x9d},
300 	{0x1c, 0xa9},
301 	{0x1d, 0xbd},
302 	{0x1e, 0xcd},
303 	{0x1f, 0xd9},
304 	{0x20, 0xe3},
305 	{0x21, 0xea},
306 	{0x22, 0xef},
307 	{0x23, 0xf5},
308 	{0x24, 0xf9},
309 	{0x25, 0xff},
310 	{0xfe, 0x00},
311 	{0xc6, 0x20},
312 	{0xc7, 0x2b},
313 	/*gamma2*/
314 	{0xfe, 0x02},
315 	{0x26, 0x0f},
316 	{0x27, 0x14},
317 	{0x28, 0x19},
318 	{0x29, 0x1e},
319 	{0x2a, 0x27},
320 	{0x2b, 0x33},
321 	{0x2c, 0x3b},
322 	{0x2d, 0x45},
323 	{0x2e, 0x59},
324 	{0x2f, 0x69},
325 	{0x30, 0x7c},
326 	{0x31, 0x89},
327 	{0x32, 0x98},
328 	{0x33, 0xae},
329 	{0x34, 0xc0},
330 	{0x35, 0xcf},
331 	{0x36, 0xda},
332 	{0x37, 0xe2},
333 	{0x38, 0xe9},
334 	{0x39, 0xf3},
335 	{0x3a, 0xf9},
336 	{0x3b, 0xff},
337 	/*YCP*/
338 	{0xfe, 0x02},
339 	{0xd1, 0x40},
340 	{0xd2, 0x40},
341 	{0xd3, 0x48},
342 	{0xd6, 0xf0},
343 	{0xd7, 0x10},
344 	{0xd8, 0xda},
345 	{0xdd, 0x14},
346 	{0xde, 0x86},
347 	{0xed, 0x80},
348 	{0xee, 0x00},
349 	{0xef, 0x3f},
350 	{0xd8, 0xd8},
351 	/*abs*/
352 	{0xfe, 0x01},
353 	{0x9f, 0x40},
354 	/*LSC*/
355 	{0xfe, 0x01},
356 	{0xc2, 0x14},
357 	{0xc3, 0x0d},
358 	{0xc4, 0x0c},
359 	{0xc8, 0x15},
360 	{0xc9, 0x0d},
361 	{0xca, 0x0a},
362 	{0xbc, 0x24},
363 	{0xbd, 0x10},
364 	{0xbe, 0x0b},
365 	{0xb6, 0x25},
366 	{0xb7, 0x16},
367 	{0xb8, 0x15},
368 	{0xc5, 0x00},
369 	{0xc6, 0x00},
370 	{0xc7, 0x00},
371 	{0xcb, 0x00},
372 	{0xcc, 0x00},
373 	{0xcd, 0x00},
374 	{0xbf, 0x07},
375 	{0xc0, 0x00},
376 	{0xc1, 0x00},
377 	{0xb9, 0x00},
378 	{0xba, 0x00},
379 	{0xbb, 0x00},
380 	{0xaa, 0x01},
381 	{0xab, 0x01},
382 	{0xac, 0x00},
383 	{0xad, 0x05},
384 	{0xae, 0x06},
385 	{0xaf, 0x0e},
386 	{0xb0, 0x0b},
387 	{0xb1, 0x07},
388 	{0xb2, 0x06},
389 	{0xb3, 0x17},
390 	{0xb4, 0x0e},
391 	{0xb5, 0x0e},
392 	{0xd0, 0x09},
393 	{0xd1, 0x00},
394 	{0xd2, 0x00},
395 	{0xd6, 0x08},
396 	{0xd7, 0x00},
397 	{0xd8, 0x00},
398 	{0xd9, 0x00},
399 	{0xda, 0x00},
400 	{0xdb, 0x00},
401 	{0xd3, 0x0a},
402 	{0xd4, 0x00},
403 	{0xd5, 0x00},
404 	{0xa4, 0x00},
405 	{0xa5, 0x00},
406 	{0xa6, 0x77},
407 	{0xa7, 0x77},
408 	{0xa8, 0x77},
409 	{0xa9, 0x77},
410 	{0xa1, 0x80},
411 	{0xa2, 0x80},
412 
413 	{0xfe, 0x01},
414 	{0xdf, 0x0d},
415 	{0xdc, 0x25},
416 	{0xdd, 0x30},
417 	{0xe0, 0x77},
418 	{0xe1, 0x80},
419 	{0xe2, 0x77},
420 	{0xe3, 0x90},
421 	{0xe6, 0x90},
422 	{0xe7, 0xa0},
423 	{0xe8, 0x90},
424 	{0xe9, 0xa0},
425 	{0xfe, 0x00},
426 	/*AWB*/
427 	{0xfe, 0x01},
428 	{0x4f, 0x00},
429 	{0x4f, 0x00},
430 	{0x4b, 0x01},
431 	{0x4f, 0x00},
432 
433 	{0x4c, 0x01},
434 	{0x4d, 0x71},
435 	{0x4e, 0x01},
436 	{0x4c, 0x01},
437 	{0x4d, 0x91},
438 	{0x4e, 0x01},
439 	{0x4c, 0x01},
440 	{0x4d, 0x70},
441 	{0x4e, 0x01},
442 	{0x4c, 0x01},
443 	{0x4d, 0x90},
444 	{0x4e, 0x02},
445 	{0x4c, 0x01},
446 	{0x4d, 0xb0},
447 	{0x4e, 0x02},
448 	{0x4c, 0x01},
449 	{0x4d, 0x8f},
450 	{0x4e, 0x02},
451 	{0x4c, 0x01},
452 	{0x4d, 0x6f},
453 	{0x4e, 0x02},
454 	{0x4c, 0x01},
455 	{0x4d, 0xaf},
456 	{0x4e, 0x02},
457 	{0x4c, 0x01},
458 	{0x4d, 0xd0},
459 	{0x4e, 0x02},
460 	{0x4c, 0x01},
461 	{0x4d, 0xf0},
462 	{0x4e, 0x02},
463 	{0x4c, 0x01},
464 	{0x4d, 0xcf},
465 	{0x4e, 0x02},
466 	{0x4c, 0x01},
467 	{0x4d, 0xef},
468 	{0x4e, 0x02},
469 	{0x4c, 0x01},
470 	{0x4d, 0x6e},
471 	{0x4e, 0x03},
472 	{0x4c, 0x01},
473 	{0x4d, 0x8e},
474 	{0x4e, 0x03},
475 	{0x4c, 0x01},
476 	{0x4d, 0xae},
477 	{0x4e, 0x03},
478 	{0x4c, 0x01},
479 	{0x4d, 0xce},
480 	{0x4e, 0x03},
481 	{0x4c, 0x01},
482 	{0x4d, 0x4d},
483 	{0x4e, 0x03},
484 	{0x4c, 0x01},
485 	{0x4d, 0x6d},
486 	{0x4e, 0x03},
487 	{0x4c, 0x01},
488 	{0x4d, 0x8d},
489 	{0x4e, 0x03},
490 	{0x4c, 0x01},
491 	{0x4d, 0xad},
492 	{0x4e, 0x03},
493 	{0x4c, 0x01},
494 	{0x4d, 0xcd},
495 	{0x4e, 0x03},
496 	{0x4c, 0x01},
497 	{0x4d, 0x4c},
498 	{0x4e, 0x03},
499 	{0x4c, 0x01},
500 	{0x4d, 0x6c},
501 	{0x4e, 0x03},
502 	{0x4c, 0x01},
503 	{0x4d, 0x8c},
504 	{0x4e, 0x03},
505 	{0x4c, 0x01},
506 	{0x4d, 0xac},
507 	{0x4e, 0x03},
508 	{0x4c, 0x01},
509 	{0x4d, 0xcc},
510 	{0x4e, 0x03},
511 	{0x4c, 0x01},
512 	{0x4d, 0xcb},
513 	{0x4e, 0x03},
514 	{0x4c, 0x01},
515 	{0x4d, 0x4b},
516 	{0x4e, 0x03},
517 	{0x4c, 0x01},
518 	{0x4d, 0x6b},
519 	{0x4e, 0x03},
520 	{0x4c, 0x01},
521 	{0x4d, 0x8b},
522 	{0x4e, 0x03},
523 	{0x4c, 0x01},
524 	{0x4d, 0xab},
525 	{0x4e, 0x03},
526 	{0x4c, 0x01},
527 	{0x4d, 0x8a},
528 	{0x4e, 0x04},
529 	{0x4c, 0x01},
530 	{0x4d, 0xaa},
531 	{0x4e, 0x04},
532 	{0x4c, 0x01},
533 	{0x4d, 0xca},
534 	{0x4e, 0x04},
535 	{0x4c, 0x01},
536 	{0x4d, 0xca},
537 	{0x4e, 0x04},
538 	{0x4c, 0x01},
539 	{0x4d, 0xc9},
540 	{0x4e, 0x04},
541 	{0x4c, 0x01},
542 	{0x4d, 0x8a},
543 	{0x4e, 0x04},
544 	{0x4c, 0x01},
545 	{0x4d, 0x89},
546 	{0x4e, 0x04},
547 	{0x4c, 0x01},
548 	{0x4d, 0xa9},
549 	{0x4e, 0x04},
550 	{0x4c, 0x02},
551 	{0x4d, 0x0b},
552 	{0x4e, 0x05},
553 	{0x4c, 0x02},
554 	{0x4d, 0x0a},
555 	{0x4e, 0x05},
556 	{0x4c, 0x01},
557 	{0x4d, 0xeb},
558 	{0x4e, 0x05},
559 	{0x4c, 0x01},
560 	{0x4d, 0xea},
561 	{0x4e, 0x05},
562 	{0x4c, 0x02},
563 	{0x4d, 0x09},
564 	{0x4e, 0x05},
565 	{0x4c, 0x02},
566 	{0x4d, 0x29},
567 	{0x4e, 0x05},
568 	{0x4c, 0x02},
569 	{0x4d, 0x2a},
570 	{0x4e, 0x05},
571 	{0x4c, 0x02},
572 	{0x4d, 0x4a},
573 	{0x4e, 0x05},
574 	{0x4c, 0x02},
575 	{0x4d, 0x8a},
576 	{0x4e, 0x06},
577 	{0x4c, 0x02},
578 	{0x4d, 0x49},
579 	{0x4e, 0x06},
580 	{0x4c, 0x02},
581 	{0x4d, 0x69},
582 	{0x4e, 0x06},
583 	{0x4c, 0x02},
584 	{0x4d, 0x89},
585 	{0x4e, 0x06},
586 	{0x4c, 0x02},
587 	{0x4d, 0xa9},
588 	{0x4e, 0x06},
589 	{0x4c, 0x02},
590 	{0x4d, 0x48},
591 	{0x4e, 0x06},
592 	{0x4c, 0x02},
593 	{0x4d, 0x68},
594 	{0x4e, 0x06},
595 	{0x4c, 0x02},
596 	{0x4d, 0x69},
597 	{0x4e, 0x06},
598 	{0x4c, 0x02},
599 	{0x4d, 0xca},
600 	{0x4e, 0x07},
601 	{0x4c, 0x02},
602 	{0x4d, 0xc9},
603 	{0x4e, 0x07},
604 	{0x4c, 0x02},
605 	{0x4d, 0xe9},
606 	{0x4e, 0x07},
607 	{0x4c, 0x03},
608 	{0x4d, 0x09},
609 	{0x4e, 0x07},
610 	{0x4c, 0x02},
611 	{0x4d, 0xc8},
612 	{0x4e, 0x07},
613 	{0x4c, 0x02},
614 	{0x4d, 0xe8},
615 	{0x4e, 0x07},
616 	{0x4c, 0x02},
617 	{0x4d, 0xa7},
618 	{0x4e, 0x07},
619 	{0x4c, 0x02},
620 	{0x4d, 0xc7},
621 	{0x4e, 0x07},
622 	{0x4c, 0x02},
623 	{0x4d, 0xe7},
624 	{0x4e, 0x07},
625 	{0x4c, 0x03},
626 	{0x4d, 0x07},
627 	{0x4e, 0x07},
628 
629 	{0x4f, 0x01},
630 	{0x50, 0x80},
631 	{0x51, 0xa8},
632 	{0x52, 0x47},
633 	{0x53, 0x38},
634 	{0x54, 0xc7},
635 	{0x56, 0x0e},
636 	{0x58, 0x08},
637 	{0x5b, 0x00},
638 	{0x5c, 0x74},
639 	{0x5d, 0x8b},
640 	{0x61, 0xdb},
641 	{0x62, 0xb8},
642 	{0x63, 0x86},
643 	{0x64, 0xc0},
644 	{0x65, 0x04},
645 	{0x67, 0xa8},
646 	{0x68, 0xb0},
647 	{0x69, 0x00},
648 	{0x6a, 0xa8},
649 	{0x6b, 0xb0},
650 	{0x6c, 0xaf},
651 	{0x6d, 0x8b},
652 	{0x6e, 0x50},
653 	{0x6f, 0x18},
654 	{0x73, 0xf0},
655 	{0x70, 0x0d},
656 	{0x71, 0x60},
657 	{0x72, 0x80},
658 	{0x74, 0x01},
659 	{0x75, 0x01},
660 	{0x7f, 0x0c},
661 	{0x76, 0x70},
662 	{0x77, 0x58},
663 	{0x78, 0xa0},
664 	{0x79, 0x5e},
665 	{0x7a, 0x54},
666 	{0x7b, 0x58},
667 	{0xfe, 0x00},
668 	/*CC*/
669 	{0xfe, 0x02},
670 	{0xc0, 0x01},
671 	{0xc1, 0x44},
672 	{0xc2, 0xfd},
673 	{0xc3, 0x04},
674 	{0xc4, 0xF0},
675 	{0xc5, 0x48},
676 	{0xc6, 0xfd},
677 	{0xc7, 0x46},
678 	{0xc8, 0xfd},
679 	{0xc9, 0x02},
680 	{0xca, 0xe0},
681 	{0xcb, 0x45},
682 	{0xcc, 0xec},
683 	{0xcd, 0x48},
684 	{0xce, 0xf0},
685 	{0xcf, 0xf0},
686 	{0xe3, 0x0c},
687 	{0xe4, 0x4b},
688 	{0xe5, 0xe0},
689 	/*ABS*/
690 	{0xfe, 0x01},
691 	{0x9f, 0x40},
692 	{0xfe, 0x00},
693 	/*OUTPUT*/
694 	{0xfe, 0x00},
695 	{0xf2, 0x0f},
696 	/*dark sun*/
697 	{0xfe, 0x02},
698 	{0x40, 0xbf},
699 	{0x46, 0xcf},
700 	{0xfe, 0x00},
701 
702 	/*frame rate 50Hz*/
703 	{0xfe, 0x00},
704 	{0x05, 0x02},
705 	{0x06, 0x20},
706 	{0x07, 0x00},
707 	{0x08, 0x32},
708 	{0xfe, 0x01},
709 	{0x25, 0x00},
710 	{0x26, 0xfa},
711 
712 	{0x27, 0x04},
713 	{0x28, 0xe2},
714 	{0x29, 0x04},
715 	{0x2a, 0xe2},
716 	{0x2b, 0x04},
717 	{0x2c, 0xe2},
718 	{0x2d, 0x04},
719 	{0x2e, 0xe2},
720 	{0xfe, 0x00},
721 
722 	{0xfe, 0x00},
723 	{0xfd, 0x01},
724 	{0xfa, 0x00},
725 	/*crop window*/
726 	{0xfe, 0x00},
727 	{0x90, 0x01},
728 	{0x91, 0x00},
729 	{0x92, 0x00},
730 	{0x93, 0x00},
731 	{0x94, 0x00},
732 	{0x95, 0x02},
733 	{0x96, 0x58},
734 	{0x97, 0x03},
735 	{0x98, 0x20},
736 	{0x99, 0x11},
737 	{0x9a, 0x06},
738 	/*AWB*/
739 	{0xfe, 0x00},
740 	{0xec, 0x02},
741 	{0xed, 0x02},
742 	{0xee, 0x30},
743 	{0xef, 0x48},
744 	{0xfe, 0x02},
745 	{0x9d, 0x08},
746 	{0xfe, 0x01},
747 	{0x74, 0x00},
748 	/*AEC*/
749 	{0xfe, 0x01},
750 	{0x01, 0x04},
751 	{0x02, 0x60},
752 	{0x03, 0x02},
753 	{0x04, 0x48},
754 	{0x05, 0x18},
755 	{0x06, 0x50},
756 	{0x07, 0x10},
757 	{0x08, 0x38},
758 	{0x0a, 0x80},
759 	{0x21, 0x04},
760 	{0xfe, 0x00},
761 	{0x20, 0x03},
762 	{0xfe, 0x00},
763 
764 	{0xfe, 0x00},
765 	{0x05, 0x02},
766 	{0x06, 0x20},
767 	{0x07, 0x00},
768 	{0x08, 0x50},
769 	{0xfe, 0x01},
770 	{0x25, 0x00},
771 	{0x26, 0xfa},
772 
773 	{0x27, 0x04},
774 	{0x28, 0xe2},
775 	{0x29, 0x04},
776 	{0x2a, 0xe2},
777 	{0x2b, 0x04},
778 	{0x2c, 0xe2},
779 	{0x2d, 0x04},
780 	{0x2e, 0xe2},
781 
782 	{0xfe, 0x00},
783 	{0xb6, 0x01},
784 	{0xfd, 0x01},
785 	{0xfa, 0x00},
786 	{0x18, 0x22},
787 	/*crop window*/
788 	{0xfe, 0x00},
789 	{0x90, 0x01},
790 	{0x91, 0x00},
791 	{0x92, 0x00},
792 	{0x93, 0x00},
793 	{0x94, 0x00},
794 	{0x95, 0x02},
795 	{0x96, 0x58},
796 	{0x97, 0x03},
797 	{0x98, 0x20},
798 	{0x99, 0x11},
799 	{0x9a, 0x06},
800 	/*AWB*/
801 	{0xfe, 0x00},
802 	{0xec, 0x02},
803 	{0xed, 0x02},
804 	{0xee, 0x30},
805 	{0xef, 0x48},
806 	{0xfe, 0x02},
807 	{0x9d, 0x08},
808 	{0xfe, 0x01},
809 	{0x74, 0x00},
810 	/*AEC*/
811 	{0xfe, 0x01},
812 	{0x01, 0x04},
813 	{0x02, 0x60},
814 	{0x03, 0x02},
815 	{0x04, 0x48},
816 	{0x05, 0x18},
817 	{0x06, 0x50},
818 	{0x07, 0x10},
819 	{0x08, 0x38},
820 	{0x0a, 0x80},
821 	{0x21, 0x04},
822 	{0xfe, 0x00},
823 	{0x20, 0x03},
824 	{0xfe, 0x00},
825 	SensorEnd
826 };
827 
gc2145_reinit_parameter(struct vehicle_ad_dev * ad,unsigned char cvstd)828 static void gc2145_reinit_parameter(struct vehicle_ad_dev *ad, unsigned char cvstd)
829 {
830 	int i = 0;
831 
832 	switch (cvstd) {
833 	case CVSTD_PAL:
834 	case CVSTD_NTSC:
835 	case CVSTD_SVGAP30:
836 	default:
837 		ad->cfg.width = 800;
838 		ad->cfg.height = 600;
839 		ad->cfg.start_x = 0;
840 		ad->cfg.start_y = 0;
841 		ad->cfg.input_format = CIF_INPUT_FORMAT_YUV;
842 		ad->cfg.output_format = FORCE_CIF_OUTPUT_FORMAT;
843 		ad->cfg.field_order = 0;
844 		ad->cfg.yuv_order = 0;/*00 - UYVY*/
845 		ad->cfg.href = 0;
846 		ad->cfg.vsync = 0;
847 		ad->cfg.frame_rate = 30;
848 		ad->cfg.type = V4L2_MBUS_PARALLEL;
849 		ad->cfg.mbus_flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
850 					V4L2_MBUS_VSYNC_ACTIVE_LOW |
851 					V4L2_MBUS_PCLK_SAMPLE_RISING;
852 		break;
853 	}
854 
855 	/* fix crop info from dts config */
856 	for (i = 0; i < 4; i++) {
857 		if ((ad->defrects[i].width == ad->cfg.width) &&
858 		    (ad->defrects[i].height == ad->cfg.height)) {
859 			ad->cfg.start_x = ad->defrects[i].crop_x;
860 			ad->cfg.start_y = ad->defrects[i].crop_y;
861 			ad->cfg.width = ad->defrects[i].crop_width;
862 			ad->cfg.height = ad->defrects[i].crop_height;
863 		}
864 	}
865 
866 #ifdef CVBS_DOUBLE_FPS_MODE
867 #endif
868 	SENSOR_DG("%s,crop(%d,%d)", __func__, ad->cfg.start_x, ad->cfg.start_y);
869 }
870 
gc2145_reg_init(struct vehicle_ad_dev * ad,unsigned char cvstd)871 static void gc2145_reg_init(struct vehicle_ad_dev *ad, unsigned char cvstd)
872 {
873 	struct rk_sensor_reg *sensor;
874 	int i;
875 	unsigned char val[2];
876 
877 	switch (cvstd) {
878 	case CVSTD_SVGAP30:
879 		sensor = sensor_preview_data_svga_30hz;
880 		break;
881 	default:
882 		sensor = sensor_preview_data_svga_30hz;
883 		break;
884 	}
885 	i = 0;
886 	while ((sensor[i].reg != SEQCMD_END) && (sensor[i].reg != 0xFC000000)) {
887 		if (sensor[i].reg == SENSOR_CHANNEL_REG)
888 			sensor[i].val = ad->ad_chl;
889 
890 		val[0] = sensor[i].val;
891 		vehicle_generic_sensor_write(ad, sensor[i].reg, val);
892 		i++;
893 	}
894 }
895 
gc2145_channel_set(struct vehicle_ad_dev * ad,int channel)896 void gc2145_channel_set(struct vehicle_ad_dev *ad, int channel)
897 {
898 	unsigned int reg = 0x41;
899 	unsigned char val[0];
900 
901 	val[0] = channel;
902 	ad->ad_chl = channel;
903 
904 	vehicle_generic_sensor_write(ad, reg, val);
905 }
906 
gc2145_ad_get_cfg(struct vehicle_cfg ** cfg)907 int gc2145_ad_get_cfg(struct vehicle_cfg **cfg)
908 {
909 	if (!gc2145_g_addev)
910 		return -1;
911 
912 	switch (cvstd_state) {
913 	case VIDEO_UNPLUG:
914 		gc2145_g_addev->cfg.ad_ready = false;
915 		break;
916 	case VIDEO_LOCKED:
917 		gc2145_g_addev->cfg.ad_ready = true;
918 		break;
919 	case VIDEO_IN:
920 		gc2145_g_addev->cfg.ad_ready = false;
921 		break;
922 	}
923 
924 	gc2145_g_addev->cfg.ad_ready = true;
925 	*cfg = &gc2145_g_addev->cfg;
926 
927 	return 0;
928 }
929 
gc2145_ad_check_cif_error(struct vehicle_ad_dev * ad,int last_line)930 void gc2145_ad_check_cif_error(struct vehicle_ad_dev *ad, int last_line)
931 {
932 	SENSOR_DG("%s, last_line %d\n", __func__, last_line);
933 
934 	if (last_line < 1)
935 		return;
936 
937 	ad->cif_error_last_line = last_line;
938 	if (cvstd_mode == CVSTD_PAL) {
939 		if (last_line == FORCE_NTSC_HEIGHT) {
940 			if (ad->state_check_work.state_check_wq)
941 				queue_delayed_work(
942 					ad->state_check_work.state_check_wq,
943 					&ad->state_check_work.work,
944 					msecs_to_jiffies(0));
945 		}
946 	} else if (cvstd_mode == CVSTD_NTSC) {
947 		if (last_line == FORCE_PAL_HEIGHT) {
948 			if (ad->state_check_work.state_check_wq)
949 				queue_delayed_work(
950 					ad->state_check_work.state_check_wq,
951 					&ad->state_check_work.work,
952 					msecs_to_jiffies(0));
953 		}
954 	} else if (cvstd_mode == CVSTD_SVGAP30) {
955 		if (last_line == FORCE_SVGA_HEIGHT) {
956 			if (ad->state_check_work.state_check_wq)
957 				queue_delayed_work(
958 					ad->state_check_work.state_check_wq,
959 					&ad->state_check_work.work,
960 					msecs_to_jiffies(0));
961 		}
962 	}
963 }
964 
gc2145_check_id(struct vehicle_ad_dev * ad)965 int gc2145_check_id(struct vehicle_ad_dev *ad)
966 {
967 	int ret = 0;
968 	int pidh, pidl;
969 	unsigned short id;
970 
971 	pidh = vehicle_generic_sensor_read(ad, 0xf0);
972 	pidl = vehicle_generic_sensor_read(ad, 0xf1);
973 	if (pidh != 0x21 || pidl != 0x45) {
974 		SENSOR_DG("%s: expected 0x2145, detected 0x%02x 0x%02x\n",
975 		    ad->ad_name, pidh, pidl);
976 		ret = -EINVAL;
977 	} else {
978 		id = SENSOR_ID(pidh, pidl);
979 		SENSOR_DG("%s Found GC%04X sensor OK!\n", __func__, id);
980 	}
981 
982 	return ret;
983 }
984 
gc2145_check_cvstd(struct vehicle_ad_dev * ad,bool activate_check)985 static int gc2145_check_cvstd(struct vehicle_ad_dev *ad, bool activate_check)
986 {
987 	return 0;
988 }
989 
gc2145_stream(struct vehicle_ad_dev * ad,int enable)990 int gc2145_stream(struct vehicle_ad_dev *ad, int enable)
991 {
992 	char val;
993 
994 	SENSOR_DG("%s on(%d)\n", __func__, enable);
995 
996 	if (enable)
997 		val = 0x0f; //stream on
998 	else
999 		val = 0x00; //stream off
1000 	vehicle_generic_sensor_write(ad, 0xf2, &val);
1001 
1002 	return 0;
1003 }
1004 
power_on(struct vehicle_ad_dev * ad)1005 static void power_on(struct vehicle_ad_dev *ad)
1006 {
1007 	/* gpio_direction_output(ad->power, ad->pwr_active); */
1008 	SENSOR_DG("gpio: power(%d), powerdown(%d)", ad->power, ad->powerdown);
1009 	if (gpio_is_valid(ad->power)) {
1010 		gpio_request(ad->power, "ad_power");
1011 		gpio_direction_output(ad->power, ad->pwr_active);
1012 		/* gpio_set_value(ad->power, ad->pwr_active); */
1013 	}
1014 
1015 	if (gpio_is_valid(ad->powerdown)) {
1016 		gpio_request(ad->powerdown, "ad_powerdown");
1017 		gpio_direction_output(ad->powerdown, !ad->pwdn_active);
1018 		/* gpio_set_value(ad->powerdown, !ad->pwdn_active); */
1019 	}
1020 }
1021 
power_off(struct vehicle_ad_dev * ad)1022 static void power_off(struct vehicle_ad_dev *ad)
1023 {
1024 	if (gpio_is_valid(ad->power))
1025 		gpio_free(ad->power);
1026 	if (gpio_is_valid(ad->powerdown))
1027 		gpio_free(ad->powerdown);
1028 }
1029 
gc2145_check_state_work(struct work_struct * work)1030 static void gc2145_check_state_work(struct work_struct *work)
1031 {
1032 	struct vehicle_ad_dev *ad;
1033 	static bool is_first = true;
1034 
1035 	ad = gc2145_g_addev;
1036 
1037 	if (ad->cif_error_last_line > 0) {
1038 		gc2145_check_cvstd(ad, true);
1039 		ad->cif_error_last_line = 0;
1040 	} else {
1041 		gc2145_check_cvstd(ad, false);
1042 	}
1043 
1044 	if (is_first) {
1045 		SENSOR_DG("%s:cvstd_old(%d), cvstd_mode(%d)\n", __func__, cvstd_old, cvstd_mode);
1046 		is_first = false;
1047 	}
1048 
1049 	if (cvstd_old != cvstd_mode || cvstd_old_state != cvstd_state) {
1050 		SENSOR_DG("%s:ad sensor std mode change, cvstd_old(%d), cvstd_mode(%d)\n",
1051 				 __func__, cvstd_old, cvstd_mode);
1052 		cvstd_old = cvstd_mode;
1053 		cvstd_old_state = cvstd_state;
1054 		SENSOR_DG("ad signal change notify\n");
1055 		vehicle_ad_stat_change_notify();
1056 	}
1057 
1058 	queue_delayed_work(ad->state_check_work.state_check_wq,
1059 			   &ad->state_check_work.work, msecs_to_jiffies(100));
1060 }
1061 
gc2145_ad_deinit(void)1062 int gc2145_ad_deinit(void)
1063 {
1064 	struct vehicle_ad_dev *ad;
1065 
1066 	ad = gc2145_g_addev;
1067 
1068 	if (!ad)
1069 		return -1;
1070 
1071 	if (ad->state_check_work.state_check_wq) {
1072 		cancel_delayed_work_sync(&ad->state_check_work.work);
1073 		flush_delayed_work(&ad->state_check_work.work);
1074 		flush_workqueue(ad->state_check_work.state_check_wq);
1075 		destroy_workqueue(ad->state_check_work.state_check_wq);
1076 	}
1077 	if (ad->irq)
1078 		free_irq(ad->irq, ad);
1079 	power_off(ad);
1080 
1081 	return 0;
1082 }
1083 
get_ad_mode_from_fix_format(int fix_format)1084 static __maybe_unused int get_ad_mode_from_fix_format(int fix_format)
1085 {
1086 	int mode = -1;
1087 
1088 	switch (fix_format) {
1089 	case AD_FIX_FORMAT_PAL:
1090 	case AD_FIX_FORMAT_NTSC:
1091 	case AD_FIX_FORMAT_720P_50FPS:
1092 	case AD_FIX_FORMAT_720P_30FPS:
1093 	case AD_FIX_FORMAT_720P_25FPS:
1094 	default:
1095 		mode = CVSTD_SVGAP30;
1096 		break;
1097 	}
1098 
1099 	return mode;
1100 }
1101 
gc2145_ad_init(struct vehicle_ad_dev * ad)1102 int gc2145_ad_init(struct vehicle_ad_dev *ad)
1103 {
1104 	int val;
1105 	int i = 0;
1106 
1107 	gc2145_g_addev = ad;
1108 
1109 	/*  1. i2c init */
1110 	while (ad->adapter == NULL) {
1111 		ad->adapter = i2c_get_adapter(ad->i2c_chl);
1112 		usleep_range(10000, 12000);
1113 	}
1114 	if (ad->adapter == NULL)
1115 		return -ENODEV;
1116 
1117 	if (!i2c_check_functionality(ad->adapter, I2C_FUNC_I2C))
1118 		return -EIO;
1119 
1120 	/*  2. ad power on sequence */
1121 	power_on(ad);
1122 
1123 	while (++i < 5) {
1124 		usleep_range(1000, 1200);
1125 		val = vehicle_generic_sensor_read(ad, 0xf0);
1126 		if (val != 0xff)
1127 			break;
1128 		SENSOR_DG("gc2145_init i2c_reg_read fail\n");
1129 	}
1130 
1131 	/*  3 .init default format params */
1132 	gc2145_reg_init(ad, cvstd_mode);
1133 	gc2145_reinit_parameter(ad, cvstd_mode);
1134 	SENSOR_DG("%s after reinit init\n", __func__);
1135 
1136 	/*  5. create workqueue to detect signal change */
1137 	INIT_DELAYED_WORK(&ad->state_check_work.work, gc2145_check_state_work);
1138 	ad->state_check_work.state_check_wq =
1139 		create_singlethread_workqueue("vehicle-ad-gc2145");
1140 
1141 	/* gc2145_check_cvstd(ad, true); */
1142 
1143 	queue_delayed_work(ad->state_check_work.state_check_wq,
1144 			   &ad->state_check_work.work, msecs_to_jiffies(100));
1145 
1146 	return 0;
1147 }
1148 
1149 
1150