summaryrefslogtreecommitdiff
path: root/tvapi/libtv/tvin/CTvin.cpp (plain)
blob: 1fe4fd09765d263665f55d4aa72677af4a6f36ce
1#define LOG_TAG "CTvin"
2
3#include "CTvin.h"
4#include <CTvLog.h>
5#include <stdio.h>
6#include <unistd.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/ioctl.h>
10#include <fcntl.h>
11#include <string.h>
12#include <errno.h>
13#include <dlfcn.h>
14#include <linux/fb.h>
15#include <stdlib.h>
16#include <cutils/properties.h>
17#include <cutils/log.h>
18#include <sys/mman.h>
19#include "../vpp/CPQdb.h"
20#include "CAv.h"
21#include "../tvsetting/CTvSetting.h"
22#include "../tvutils/tvutils.h"
23#include "../tvconfig/tvconfig.h"
24#include "CFbcCommunication.h"
25
26#define AFE_DEV_PATH "/dev/tvafe0"
27#define HDMIRX_KSV_PATH "/dev/hdmirx0"
28
29
30#define CC_SEL_VDIN_DEV (0)
31
32#define MODE_3D_DISABLE 0x00000000
33#define MODE_3D_ENABLE 0x00000001
34#define MODE_AUTO 0x00000002
35#define MODE_2D_TO_3D 0x00000004
36#define MODE_LR 0x00000008
37#define MODE_BT 0x00000010
38#define MODE_LR_SWITCH 0x00000020
39#define MODE_FIELD_DEPTH 0x00000040
40//#define MODE_3D_TO_2D_L 0x00000080 redefine
41//#define MODE_3D_TO_2D_R 0x00000100 redefine
42#define LR_FORMAT_INDICATOR 0x00000200
43#define BT_FORMAT_INDICATOR 0x00000400
44
45/* ADC calibration pattern & format define */
46/* default 100% 8 color-bar */
47//#define VGA_SOURCE_RGB444
48#define VGA_H_ACTIVE (1024)
49#define VGA_V_ACTIVE (768)
50#define COMP_H_ACTIVE (1280)
51#define COMP_V_ACTIVE (720)
52#define CVBS_H_ACTIVE (720)
53#define CVBS_V_ACTIVE (480)
54
55int CTvin::mSourceInputToPortMap[SOURCE_MAX];
56CTvin *CTvin::mInstance;
57
58CTvin *CTvin::getInstance()
59{
60 if (NULL == mInstance) mInstance = new CTvin();
61 return mInstance;
62}
63
64CTvin::CTvin()
65{
66 int i = 0;
67
68 m_vdin_dev_fd = -1;
69 afe_dev_fd = -1;
70
71 m_tvin_param.index = 0;
72 m_is_decoder_start = false;
73 gExistD2D3 = -1;
74 gVideoPath[0] = '\0';
75 m_pathid = TV_PATH_DECODER_3D_AMVIDEO;
76
77 for (i = 0; i < SOURCE_MAX; i++) {
78 mSourceInputToPortMap[i] = TVIN_PORT_NULL;
79 }
80
81 mSourceInputToPortMap[SOURCE_TV] = TVIN_PORT_CVBS3;
82 mSourceInputToPortMap[SOURCE_AV1] = TVIN_PORT_CVBS1;
83 mSourceInputToPortMap[SOURCE_AV2] = TVIN_PORT_CVBS2;
84 mSourceInputToPortMap[SOURCE_YPBPR1] = TVIN_PORT_COMP0;
85 mSourceInputToPortMap[SOURCE_YPBPR2] = TVIN_PORT_COMP1;
86 mSourceInputToPortMap[SOURCE_HDMI1] = TVIN_PORT_HDMI0;
87 mSourceInputToPortMap[SOURCE_HDMI2] = TVIN_PORT_HDMI2;
88 mSourceInputToPortMap[SOURCE_HDMI3] = TVIN_PORT_HDMI1;
89 mSourceInputToPortMap[SOURCE_VGA] = TVIN_PORT_VGA0;
90 mSourceInputToPortMap[SOURCE_MPEG] = TVIN_PORT_MPEG0;
91 mSourceInputToPortMap[SOURCE_DTV] = TVIN_PORT_DTV;
92 mSourceInputToPortMap[SOURCE_IPTV] = TVIN_PORT_BT656;
93}
94
95CTvin::~CTvin()
96{
97}
98
99int CTvin::OpenTvin()
100{
101 const char *config_value;
102 config_value = config_get_str ( CFG_SECTION_TV, "tvin.manual.set.tvpath", "null" );
103 strcpy ( config_tv_path, config_value );
104 memset ( config_default_path, 0x0, 64 );
105 config_value = config_get_str ( CFG_SECTION_TV, "tvin.manual.set.defaultpath", "null" );
106 strcpy ( config_default_path, config_value );
107 return 0;
108}
109
110int CTvin::IsFileExist ( const char *file_name )
111{
112 struct stat tmp_st;
113
114 if ( stat ( file_name, &tmp_st ) < 0 ) {
115 return 0;
116 }
117
118 return 1;
119}
120
121char *CTvin::DelSub ( char *str, char *sub )
122{
123 char *psrc = str, *pdest = str, *psub = NULL, *p = NULL;
124
125 if ( ( str == NULL ) || ( sub == NULL ) ) {
126 return NULL;
127 }
128
129 while ( *psrc ) {
130 p = psrc;
131 psub = sub;
132
133 while ( *p && *p == *psub ) {
134 p++;
135 psub++;
136 }
137
138 if ( *psub == 0 ) {
139 psrc = p;
140 } else {
141 *pdest++ = *psrc++;
142 }
143 }
144
145 *pdest = 0;
146
147 return str;
148}
149
150int CTvin::setMpeg2Vdin(int enable)
151{
152 /* let output data loop to vdin */
153 FILE *fp = fopen ( "/sys/module/di/parameters/mpeg2vdin_en", "w" );
154
155 if ( fp == NULL ) {
156 LOGW ( "Open /sys/module/di/parameters/mpeg2vdin_en error\n" );
157 return -1;
158 }
159 fprintf ( fp, "%d", enable );
160 fclose ( fp );
161 return 0;
162}
163
164char *CTvin::VDIN_CheckVideoPath ( const char *videopath )
165{
166 strncpy ( gVideoPath, videopath, sizeof ( gVideoPath ) );
167
168 if ( strstr ( videopath, "d2d3 " ) != NULL ) {
169 if ( gExistD2D3 == -1 ) {
170 gExistD2D3 = IsFileExist ( "/sys/class/d2d3/d2d3" );
171 }
172
173 if ( gExistD2D3 == 0 ) {
174 DelSub ( gVideoPath, (char *)"d2d3 " );
175 }
176 }
177
178 LOGW ( "%s, video path before check [%s]\n", CFG_SECTION_TV, videopath );
179 LOGW ( "%s, video path after check [%s]\n", CFG_SECTION_TV, gVideoPath );
180
181 return gVideoPath;
182}
183
184int CTvin::VDIN_AddPath ( const char *videopath )
185{
186 FILE *fp = NULL;
187 int ret = -1;
188 char *tmp_video_path = NULL;
189
190 tmp_video_path = VDIN_CheckVideoPath ( videopath );
191
192 fp = fopen ( "/sys/class/vfm/map", "w" );
193
194 if ( fp == NULL ) {
195 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
196 return -1;
197 }
198
199 ret = fprintf ( fp, "%s", tmp_video_path );
200
201 if ( ret < 0 ) {
202 LOGW ( "Add VideoPath error(%s)!\n", strerror ( errno ) );
203 }
204
205 fclose ( fp );
206 fp = NULL;
207
208 return ret;
209}
210
211int CTvin::VDIN_RmDefPath ( void )
212{
213 int fd = -1, ret;
214 char str[] = "rm default";
215
216
217 fd = open ( "/sys/class/vfm/map", O_RDWR );
218
219 if ( fd < 0 ) {
220 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
221 return -1;
222 }
223
224 ret = write ( fd, str, sizeof ( str ) );
225
226 if ( ret < 0 ) {
227 LOGW ( "Rm default path error(%s)!\n", strerror ( errno ) );
228 }
229
230 close ( fd );
231 fd = -1;
232
233 return ret;
234}
235
236int CTvin::VDIN_RmTvPath ( void )
237{
238 int fd, ret;
239 char str[] = "rm tvpath";
240
241
242 fd = open ( "/sys/class/vfm/map", O_RDWR );
243
244 if ( fd < 0 ) {
245 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
246 return -1;
247 }
248
249 ret = write ( fd, str, sizeof ( str ) );
250
251 if ( ret <= 0 ) {
252 LOGW ( "Rm tv path error(%s)!\n", strerror ( errno ) );
253 }
254
255 close ( fd );
256 fd = -1;
257 return ret;
258}
259
260int CTvin::VDIN_AddVideoPath ( int selPath )
261{
262 int ret = -1;
263
264 switch ( selPath ) {
265 case TV_PATH_VDIN_AMVIDEO:
266 ret = VDIN_AddPath ( "add tvpath vdin0 amvideo" );
267 break;
268
269 case TV_PATH_VDIN_DEINTERLACE_AMVIDEO:
270 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace amvideo" );
271 break;
272
273 case TV_PATH_VDIN_3D_AMVIDEO:
274 ret = VDIN_AddPath ( "add tvpath vdin0 ppmgr amvideo" );
275 break;
276
277 case TV_PATH_VDIN_NEW3D_AMVIDEO:
278 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace ppmgr d2d3 amvideo" );
279 break;
280
281 case TV_PATH_VDIN_NEW3D_WITHOUTPPMGR_AMVIDEO:
282 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace d2d3 amvideo" );
283 break;
284
285 case TV_PATH_DECODER_3D_AMVIDEO:
286 ret = VDIN_AddPath ( "add default decoder ppmgr deinterlace amvideo" );
287 break;
288
289 case TV_PATH_DECODER_AMVIDEO:
290 ret = VDIN_AddPath ( "add default decoder deinterlace amvideo" );
291 break;
292
293 case TV_PATH_VDIN_FREESCALE_AMVIDEO:
294 ret = VDIN_AddPath ( "add previewpath vdin0 freescale amvideo" );
295 break;
296
297 case TV_PATH_DECODER_NEW3D_AMVIDEO:
298 ret = VDIN_AddPath ( "add default decoder deinterlace ppmgr d2d3 amvideo" );
299 break;
300
301 case TV_PATH_DECODER_NEW3D_WITHOUTPPMGR_AMVIDEO:
302 ret = VDIN_AddPath ( "add default decoder deinterlace d2d3 amvideo" );
303 break;
304 }
305
306 return ret;
307}
308
309int CTvin::VDIN_RmPreviewPath ( void )
310{
311 int fd, ret;
312 char str[] = "rm previewpath";
313
314
315 fd = open ( "/sys/class/vfm/map", O_RDWR );
316
317 if ( fd < 0 ) {
318 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
319 return -1;
320 }
321
322 ret = write ( fd, str, sizeof ( str ) );
323
324 if ( ret < 0 ) {
325 LOGW ( "Rm tv path error(%s)!\n", strerror ( errno ) );
326 }
327
328 close ( fd );
329 fd = -1;
330
331 return ret;
332}
333
334int CTvin::VDIN_OpenModule()
335{
336 char file_name[64];
337 sprintf ( file_name, "/dev/vdin%d", CC_SEL_VDIN_DEV );
338 m_vdin_dev_fd = open ( file_name, O_RDWR );
339
340 if ( m_vdin_dev_fd < 0 ) {
341 LOGW ( "Open %s error(%s)!\n", file_name, strerror ( errno ) );
342 return -1;
343 }
344
345 memset ( &gTvinVDINParam, 0, sizeof ( gTvinVDINParam ) );
346 memset ( &gTvinVDINSignalInfo, 0, sizeof ( gTvinVDINSignalInfo ) );
347
348 LOGD ( "Open vdin module vdin_dev_fd = [%d]", m_vdin_dev_fd );
349
350 return m_vdin_dev_fd;
351}
352
353int CTvin::VDIN_GetVdinFd()
354{
355 return m_vdin_dev_fd;
356}
357
358int CTvin::VDIN_CloseModule()
359{
360 if ( m_vdin_dev_fd != -1 ) {
361 close ( m_vdin_dev_fd );
362 m_vdin_dev_fd = -1;
363 }
364
365 return 0;
366}
367
368int CTvin::VDIN_DeviceIOCtl ( int request, ... )
369{
370 int tmp_ret = -1;
371 va_list ap;
372 void *arg;
373
374 if ( m_vdin_dev_fd >= 0 ) {
375 va_start ( ap, request );
376 arg = va_arg ( ap, void * );
377 va_end ( ap );
378
379 tmp_ret = ioctl ( m_vdin_dev_fd, request, arg );
380 return tmp_ret;
381 }
382
383 return -1;
384}
385
386int CTvin::VDIN_OpenPort ( tvin_port_t port )
387{
388 int rt = -1;
389 struct tvin_parm_s vdinParam;
390 vdinParam.port = port;
391 vdinParam.index = 0;
392 rt = VDIN_DeviceIOCtl ( TVIN_IOC_OPEN, &vdinParam );
393
394 if ( rt < 0 ) {
395 LOGW ( "Vdin open port, error(%s)!", strerror ( errno ) );
396 }
397
398 return rt;
399}
400
401int CTvin::VDIN_ClosePort()
402{
403 int rt = -1;
404
405 rt = VDIN_DeviceIOCtl ( TVIN_IOC_CLOSE );
406
407 if ( rt < 0 ) {
408 LOGW ( "Vdin close port, error(%s)!", strerror ( errno ) );
409 }
410
411 return rt;
412}
413
414int CTvin::VDIN_StartDec ( const struct tvin_parm_s *vdinParam )
415{
416 int rt = -1;
417
418 if ( vdinParam == NULL ) {
419 return -1;
420 }
421
422 rt = VDIN_DeviceIOCtl ( TVIN_IOC_START_DEC, vdinParam );
423
424 LOGD ( "VDIN_StartDec:\n" );
425 LOGD ( "index = [%d]\n", vdinParam->index );
426 LOGD ( "port = [0x%x]\n", ( unsigned int ) vdinParam->port );
427 LOGD ( "format = [0x%x]\n", ( unsigned int ) ( vdinParam->info.fmt ) );
428
429 if ( rt < 0 ) {
430 LOGW ( "Vdin start decode, error(%s)!\n", strerror ( errno ) );
431 }
432
433 return rt;
434}
435
436int CTvin::VDIN_StopDec()
437{
438 int rt = -1;
439
440 rt = VDIN_DeviceIOCtl ( TVIN_IOC_STOP_DEC );
441
442 if ( rt < 0 ) {
443 LOGW ( "Vdin stop decode, error(%s)", strerror ( errno ) );
444 }
445
446 return rt;
447}
448
449int CTvin::VDIN_GetSignalInfo ( struct tvin_info_s *SignalInfo )
450{
451 int rt = -1;
452
453 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_SIG_INFO, SignalInfo );
454
455 if ( rt < 0 ) {
456 LOGW ( "Vdin get signal info, error(%s), ret = %d.\n", strerror ( errno ), rt );
457 //system("reboot");
458 }
459
460 return rt;
461}
462
463int CTvin::VDIN_SetVdinParam ( const struct tvin_parm_s *vdinParam )
464{
465 int rt = -1, i = 0;
466
467 rt = VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, vdinParam );
468
469 if ( rt < 0 ) {
470 LOGW ( "Vdin set signal param, error(%s)\n", strerror ( errno ) );
471 }
472
473 return rt;
474}
475
476int CTvin::VDIN_GetVdinParam ( const struct tvin_parm_s *vdinParam )
477{
478 int rt = -1, i = 0;
479
480 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, vdinParam );
481
482 if ( rt < 0 ) {
483 LOGW ( "Vdin get signal param, error(%s)\n", strerror ( errno ) );
484 }
485
486 return rt;
487}
488
489int CTvin::VDIN_OnoffVScaler ( int isOn )
490{
491 FILE *fp = NULL;
492
493 if ( isOn == 1 ) {
494 isOn = 1;
495 } else {
496 isOn = 0;
497 }
498
499 fp = fopen ( "/sys/class/video/vscaler", "w" );
500 if ( fp == NULL ) {
501 LOGW ( "Open /sys/class/video/vscaler error(%s)!\n", strerror ( errno ) );
502 return -1;
503 }
504
505 fprintf ( fp, "%d", ( int ) isOn );
506 fclose ( fp );
507 fp = NULL;
508
509 return 0;
510}
511
512int CTvin::VDIN_GetDisplayVFreq (void)
513{
514 int fd = -1;
515 char buf[32];
516
517 fd = open("/sys/class/display/mode", O_RDWR);
518 if (fd < 0) {
519 LOGW("Open /sys/class/display/mode error(%s)!\n", strerror(errno));
520 return -1;
521 }
522 memset(buf, 0, sizeof(buf));
523 read(fd, buf, sizeof(buf));
524 close(fd);
525 LOGD( "%s, VDIN_GetDisplayVFreq is %s\n", __FUNCTION__, buf);
526 if (strstr(buf, "50hz") != NULL) {
527 return 50;
528 } else if (strstr(buf, "60hz") != 0) {
529 return 60;
530 } else {
531 return 50;
532 }
533}
534
535int CTvin::VDIN_SetDisplayVFreq ( int freq, int display_resolution , bool isFbc)
536{
537 FILE *fp = NULL;
538 const char *config_value = NULL;
539 static int display_mode_type = -1;
540 CFbcCommunication *pFBC = NULL;
541 static int last_freq = 50;
542
543 last_freq = VDIN_GetDisplayVFreq();
544 if (last_freq == freq) {
545 LOGD ( "%s, same freq, last_freq = %d, freq = %d.", __FUNCTION__, last_freq, freq);
546 return 0;
547 } else {
548 LOGD ( "%s, from last_freq[%d[ to new_freq[%d].", __FUNCTION__, last_freq, freq);
549 last_freq = freq;
550 }
551
552 if (display_mode_type == -1) {
553 config_value = config_get_str ( CFG_SECTION_TV, "tvin.display.mode.type", "null" );
554 if (strcasecmp(config_value, "null") == 0 || strcasecmp(config_value, "hdmi_out") == 0) {
555 display_mode_type = 0;
556 } else if (strcasecmp(config_value, "lvds_out") == 0) {
557 display_mode_type = 1;
558 } else {
559 display_mode_type = 0;
560 }
561 }
562
563 fp = fopen ( "/sys/class/display/mode", "w" );
564
565 if ( fp == NULL ) {
566 LOGW ( "Open /sys/class/display/mode error(%s)!\n", strerror ( errno ) );
567 return -1;
568 }
569
570 if ( isFbc ) {
571 pFBC = GetSingletonFBC();
572 if (pFBC != NULL) {
573 pFBC->cfbc_Set_VMute (COMM_DEV_SERIAL, 2);
574 usleep ( 300000 );
575 }
576 }
577
578 switch ( display_resolution ) {
579 case VPP_DISPLAY_RESOLUTION_1366X768:
580 if ( freq == 50 ) {
581 fprintf ( fp, "%s", "768p50hz" );
582 } else {
583 fprintf ( fp, "%s", "768p60hz" );
584 }
585 break;
586 case VPP_DISPLAY_RESOLUTION_3840X2160:
587 if ( freq == 50 ) {
588 if (isFbc) {
589 fprintf ( fp, "%s", "2160p50hz420" );
590 } else {
591 fprintf ( fp, "%s", "2160p50hz" );
592 }
593 } else {
594 if (isFbc) {
595 fprintf ( fp, "%s", "2160p60hz420" );
596 } else {
597 fprintf ( fp, "%s", "2160p60hz" );
598 }
599 }
600 break;
601 case VPP_DISPLAY_RESOLUTION_1920X1080:
602 default:
603 if ( freq == 50 ) {
604 fprintf ( fp, "%s", "1080p50hz" );
605 } else {
606 fprintf ( fp, "%s", "1080p60hz" );
607 }
608 break;
609 }
610
611 fclose ( fp );
612 fp = NULL;
613
614 return 0;
615}
616
617void CTvin::Tvin_SetDepthOf2Dto3D ( int value )
618{
619 //value = -16~16
620 int tmp_value = DepthTable_2DTO3D[value + 16];
621 LOGD ( "%s, value: %d", __FUNCTION__, value );
622 VDIN_Set2D3DDepth ( value );
623}
624
625
626int CTvin::VDIN_Set2D3DDepth ( int count )
627{
628 FILE *fp = NULL;
629
630
631 fp = fopen ( "/sys/module/d2d3/parameters/depth", "w" );
632
633 if ( fp == NULL ) {
634 LOGW ( "Open /sys/module/d2d3/parameters/depth ERROR(%s)!!\n", strerror ( errno ) );
635 return -1;
636 }
637
638 if ( count >= -127 && count <= 127 ) {
639#ifdef LOGD_3D_FUNCTION
640 LOGD ( "set depth value (%d).\n", count );
641#endif
642 } else {
643 count = 8 * 12;
644 LOGE ( "set depth value ERROR!! set default depth.\n" );
645 }
646
647 fprintf ( fp, "%d", count );
648 fclose ( fp );
649 fp = NULL;
650
651 return 0;
652}
653
654int CTvin::VDIN_Set2Dto3D ( int on_off )
655{
656 struct tvin_parm_s VdinParam;
657 VDIN_GetVdinParam ( &VdinParam );
658 VdinParam.flag &= ( ~TVIN_PARM_FLAG_2D_TO_3D );
659 VdinParam.flag |= ( on_off ) ? ( TVIN_PARM_FLAG_2D_TO_3D ) : ( 0 );
660 return VDIN_SetVdinParam ( &VdinParam );
661}
662
663int CTvin::VDIN_Get_avg_luma(void)
664{
665 unsigned int lum_sum, pixel_sum, luma_avg;
666 struct tvin_parm_s vdinParam;
667
668 if ( 0 == VDIN_GetVdinParam( &vdinParam )) {
669 lum_sum = vdinParam.luma_sum;
670 pixel_sum = vdinParam.pixel_sum * 2;
671 if (pixel_sum != 0 && m_is_decoder_start) {
672 luma_avg = lum_sum / pixel_sum;
673 } else {
674 luma_avg = 116;
675 }
676 } else {
677 return -1;
678 }
679 LOGD ( "VDIN_get_avg_lut lum_sum =%d,pixel_sum=%d,lut_avg=%d\n", lum_sum, pixel_sum, luma_avg);
680 return luma_avg;
681}
682
683int CTvin::VDIN_GetHistgram ( int *hisgram )
684{
685 int i = 0;
686 struct tvin_parm_s vdinParam;
687
688 if ( NULL == hisgram ) {
689 return -1;
690 }
691
692 if ( 0 == VDIN_GetVdinParam ( &vdinParam ) ) {
693 for ( i = 0; i < CC_HIST_GRAM_BUF_SIZE; i++ ) {
694 hisgram[i] = ( int ) vdinParam.histgram[i];
695 }
696 } else {
697 return -1;
698 }
699
700 return 0;
701}
702
703int CTvin::VDIN_SetMVCViewMode ( int mode )
704{
705 FILE *fp = NULL;
706
707
708 fp = fopen ( "/sys/module/amvdec_h264mvc/parameters/view_mode", "w" );
709
710 if ( fp == NULL ) {
711 LOGW ( "Open /sys/module/amvdec_h264mvc/parameters/view_mode error(%s)!\n", strerror ( errno ) );
712 return -1;
713 }
714
715 fprintf ( fp, "%d", ( int ) mode );
716
717 fclose ( fp );
718 fp = NULL;
719
720 return 0;
721}
722
723int CTvin::VDIN_GetMVCViewMode ( void )
724{
725 FILE *fp = NULL;
726 int ret = 0;
727 int mode = 0;
728
729
730 fp = fopen ( "/sys/module/amvdec_h264mvc/parameters/view_mode", "w" );
731
732 if ( fp == NULL ) {
733 LOGW ( "Open /sys/module/amvdec_h264mvc/parameters/view_mode ERROR(%s)!!\n", strerror ( errno ) );
734 return -1;
735 }
736
737 ret = fread ( &mode, 1, 1, fp );
738 LOGD ( "fread /sys/module/amvdec_h264mvc/parameters/view_mode = [%d]", mode );
739
740 fclose ( fp );
741 fp = NULL;
742
743 return mode;
744}
745
746int CTvin::VDIN_SetDIBuffMgrMode ( int mgr_mode )
747{
748 FILE *fp = NULL;
749
750
751 fp = fopen ( "sys/module/di/parameters/buf_mgr_mode", "w" );
752
753 if ( fp == NULL ) {
754 LOGW ( "Open /sys/module/di/parameters/buf_mgr_mode error(%s)!\n", strerror ( errno ) );
755 return -1;
756 }
757
758 fprintf ( fp, "%d", mgr_mode );
759
760 fclose ( fp );
761 fp = NULL;
762
763 return 0;
764}
765
766int CTvin::VDIN_SetDICFG ( int cfg )
767{
768 FILE *fp = NULL;
769
770
771 fp = fopen ( "sys/class/deinterlace/di0/config", "w" );
772
773 if ( fp == NULL ) {
774 LOGW ( "Open /sys/class/deinterlace/di0/config error(%s)!\n", strerror ( errno ) );
775 return -1;
776 }
777
778 if ( 0 == cfg ) {
779 fprintf ( fp, "%s", "disable" );
780 } else {
781 fprintf ( fp, "%s", "enable" );
782 }
783
784 fclose ( fp );
785 fp = NULL;
786
787 return 0;
788}
789
790int CTvin::VDIN_SetDI3DDetc ( int enable )
791{
792 FILE *fp = NULL;
793
794
795 fp = fopen ( "/sys/module/di/parameters/det3d_en", "w" );
796
797 if ( fp == NULL ) {
798 LOGW ( "Open /sys/module/di/parameters/det3d_en error(%s)!\n", strerror ( errno ) );
799 return -1;
800 }
801
802 fprintf ( fp, "%d", enable );
803
804
805 fclose ( fp );
806 fp = NULL;
807
808 return 0;
809}
810
811int CTvin::VDIN_Get3DDetc ( void )
812{
813 int fd = -1;
814 int ret = -1;
815 char buf[10];
816
817 fd = open ( "/sys/module/di/parameters/det3d_en", O_RDWR );
818 if ( fd < 0 ) {
819 LOGW ( "Open /sys/module/di/parameters/det3d_en error(%s)!\n", strerror ( errno ) );
820 return -1;
821 }
822
823 ret = read ( fd, buf, sizeof ( buf ) );
824 close ( fd );
825 fd = -1;
826
827 if ( strcmp ( "enable", buf ) == 0 ) {
828 return 1;
829 } else {
830 return 0;
831 }
832}
833
834
835int CTvin::VDIN_GetVscalerStatus ( void )
836{
837 int fd = -1;
838 int ret = -1;
839 char buf[7];
840
841 fd = open ( "/sys/class/video/vscaler", O_RDWR );
842 if ( fd < 0 ) {
843 LOGW ( "Open /sys/class/video/vscaler error(%s)!\n", strerror ( errno ) );
844 return -1;
845 }
846
847 ret = read ( fd, buf, sizeof ( buf ) );
848 close ( fd );
849 fd = -1;
850
851 sscanf ( buf, "%d", &ret );
852
853 ret = ( ( ret & 0x40000 ) == 0 ) ? 1 : 0;
854 if ( ret == 1 ) {
855 sleep ( 1 );
856 }
857
858 return ret;
859}
860
861int CTvin::VDIN_TurnOnBlackBarDetect ( int isEnable )
862{
863 FILE *fp = NULL;
864
865 fp = fopen ( "/sys/module/tvin_vdin/parameters/black_bar_enable", "w" );
866 if ( fp == NULL ) {
867 LOGW ( "Open /sys/module/tvin_vdin/parameters/black_bar_enable error(%s)!\n", strerror ( errno ) );
868 return -1;
869 }
870
871 fprintf ( fp, "%d", isEnable );
872 fclose ( fp );
873 fp = NULL;
874
875 return 0;
876}
877
878int CTvin::VDIN_LoadHdcpKey ( unsigned char *hdcpkey_buff __unused)
879{
880 unsigned char testHdcp[368] = { 0x53, 0x4B, 0x59, 0x01, 0x00, 0x10, 0x0D, 0x15, 0x3A, 0x8E, 0x99, 0xEE, 0x2A, 0x55, 0x58, 0xEE, 0xED, 0x4B, 0xBE, 0x00, 0x74, 0xA9, 0x00, 0x10, 0x0A, 0x21, 0xE3,
881 0x30, 0x66, 0x34, 0xCE, 0x9C, 0xC7, 0x8B, 0x51, 0x27, 0xF9, 0x0B, 0xAD, 0x09, 0x5F, 0x4D, 0xC2, 0xCA, 0xA2, 0x13, 0x06, 0x18, 0x8D, 0x34, 0x82, 0x46, 0x2D, 0xC9, 0x4B, 0xB0, 0x1C, 0xDE,
882 0x3D, 0x49, 0x39, 0x58, 0xEF, 0x2B, 0x68, 0x39, 0x71, 0xC9, 0x4D, 0x25, 0xE9, 0x75, 0x4D, 0xAC, 0x62, 0xF5, 0xF5, 0x87, 0xA0, 0xB2, 0x4A, 0x60, 0xD3, 0xF1, 0x09, 0x3A, 0xB2, 0x3E, 0x19,
883 0x4F, 0x3B, 0x1B, 0x2F, 0x85, 0x14, 0x28, 0x44, 0xFC, 0x69, 0x6F, 0x50, 0x42, 0x81, 0xBF, 0x7C, 0x2B, 0x3A, 0x17, 0x2C, 0x15, 0xE4, 0x93, 0x77, 0x74, 0xE8, 0x1F, 0x1C, 0x38, 0x54, 0x49,
884 0x10, 0x64, 0x5B, 0x7D, 0x90, 0x3D, 0xA0, 0xE1, 0x8B, 0x67, 0x5C, 0x19, 0xE6, 0xCA, 0x9D, 0xE9, 0x68, 0x5A, 0xB5, 0x62, 0xDF, 0xA1, 0x28, 0xBC, 0x68, 0x82, 0x9A, 0x22, 0xC4, 0xDC, 0x48,
885 0x85, 0x0F, 0xF1, 0x3E, 0x05, 0xDD, 0x1B, 0x2D, 0xF5, 0x49, 0x3A, 0x15, 0x29, 0xE7, 0xB6, 0x0B, 0x2A, 0x40, 0xE3, 0xB0, 0x89, 0xD5, 0x75, 0x84, 0x2E, 0x76, 0xE7, 0xBC, 0x63, 0x67, 0xE3,
886 0x57, 0x67, 0x86, 0x81, 0xF4, 0xD7, 0xEA, 0x4D, 0x89, 0x8E, 0x37, 0x95, 0x59, 0x1C, 0x8A, 0xCD, 0x79, 0xF8, 0x4F, 0x82, 0xF2, 0x6C, 0x7E, 0x7F, 0x79, 0x8A, 0x6B, 0x90, 0xC0, 0xAF, 0x4C,
887 0x8D, 0x43, 0x47, 0x1F, 0x9A, 0xF1, 0xBB, 0x88, 0x64, 0x49, 0x14, 0x50, 0xD1, 0xC3, 0xDF, 0xA6, 0x87, 0xA0, 0x15, 0x98, 0x51, 0x81, 0xF5, 0x97, 0x55, 0x10, 0x4A, 0x99, 0x30, 0x54, 0xA4,
888 0xFC, 0xDA, 0x0E, 0xAC, 0x6A, 0xFA, 0x90, 0xEE, 0x12, 0x70, 0x69, 0x74, 0x63, 0x46, 0x63, 0xFB, 0xE6, 0x1F, 0x72, 0xEC, 0x43, 0x5D, 0x50, 0xFF, 0x03, 0x4F, 0x05, 0x33, 0x88, 0x36, 0x93,
889 0xE4, 0x72, 0xD5, 0xCC, 0x34, 0x52, 0x96, 0x15, 0xCE, 0xD0, 0x32, 0x52, 0x41, 0x4F, 0xBC, 0x2D, 0xDF, 0xC5, 0xD6, 0x7F, 0xD5, 0x74, 0xCE, 0x51, 0xDC, 0x10, 0x5E, 0xF7, 0xAA, 0x4A, 0x2D,
890 0x20, 0x9A, 0x17, 0xDD, 0x30, 0x89, 0x71, 0x82, 0x36, 0x50, 0x09, 0x1F, 0x7C, 0xF3, 0x12, 0xE9, 0x43, 0x10, 0x5F, 0x51, 0xBF, 0xB8, 0x45, 0xA8, 0x5A, 0x8D, 0x3F, 0x77, 0xE5, 0x96, 0x73,
891 0x68, 0xAB, 0x73, 0xE5, 0x4C, 0xFB, 0xE5, 0x98, 0xB9, 0xAE, 0x74, 0xEB, 0x51, 0xDB, 0x91, 0x07, 0x7B, 0x66, 0x02, 0x9B, 0x79, 0x03, 0xC5, 0x34, 0x1C, 0x58, 0x13, 0x31, 0xD2, 0x4A, 0xEC
892 };
893 int ret = -1;
894 int fd = -1;
895
896 fd = open ( "/sys/class/hdmirx/hdmirx0/edid", O_RDWR );
897 if ( fd < 0 ) {
898 LOGW ( "Open hdmi hdcp key error(%s)!!\n", strerror ( errno ) );
899 return -1;
900 }
901
902 ret = write ( fd, testHdcp, 368 );
903 if ( ret < 0 ) {
904 LOGD ( "Write hdmi hdcp key error(%s)!!\n", strerror ( errno ) );
905 }
906
907 close ( fd );
908 fd = -1;
909
910 return ret;
911}
912
913int CTvin::VDIN_KeepLastFrame ( int enable __unused)
914{
915 return 0;
916 /*
917 FILE *fp = NULL;
918 fp = fopen ( "/sys/module/amvideo/parameters/keep_old_frame", "w" );
919
920 if ( fp == NULL ) {
921 LOGW ( "Open /sys/module/amvideo/parameters/keep_old_frame error(%s)!\n", strerror ( errno ) );
922 return -1;
923 }
924
925 fprintf ( fp, "%d", enable );
926
927 fclose ( fp );
928 fp = NULL;
929
930 return 0;
931 */
932}
933
934int CTvin::VDIN_SetVideoFreeze ( int enable )
935{
936 FILE *fp = NULL;
937
938 fp = fopen ( "/sys/class/vdin/vdin0/attr", "w" );
939 if ( fp == NULL ) {
940 LOGW ( "Open /sys/class/vdin/vdin0/attr error(%s)!\n", strerror ( errno ) );
941 return -1;
942 }
943
944 if ( enable == 1 ) {
945 fprintf ( fp, "freeze" );
946 } else {
947 fprintf ( fp, "unfreeze" );
948 }
949
950 fclose ( fp );
951 fp = NULL;
952
953 return 0;
954}
955
956int CTvin::VDIN_SetDIBypasshd ( int enable )
957{
958 FILE *fp = NULL;
959
960 fp = fopen ( "/sys/module/di/parameters/bypass_hd", "w" );
961 if ( fp == NULL ) {
962 LOGW ( "Open /sys/module/di/parameters/bypass_hd error(%s)!\n", strerror ( errno ) );
963 return -1;
964 }
965
966 fprintf ( fp, "%d", enable );
967 fclose ( fp );
968 fp = NULL;
969
970 return 0;
971}
972
973int CTvin::VDIN_SetDIBypassAll ( int enable )
974{
975 FILE *fp = NULL;
976
977 fp = fopen ( "/sys/module/di/parameters/bypass_all", "w" );
978 if ( fp == NULL ) {
979 LOGW ( "Open /sys/module/di/parameters/bypass_all error(%s)!\n", strerror ( errno ) );
980 return -1;
981 }
982
983 fprintf ( fp, "%d", enable );
984 fclose ( fp );
985 fp = NULL;
986 return 0;
987}
988
989int CTvin::VDIN_SetDIBypass_Get_Buf_Threshold ( int enable )
990{
991 FILE *fp = NULL;
992
993 fp = fopen ( "/sys/module/di/parameters/bypass_get_buf_threshold", "w" );
994 if ( fp == NULL ) {
995 LOGW ( "Open /sys/module/di/parameters/bypass_get_buf_threshold error(%s)!\n", strerror ( errno ) );
996 return -1;
997 }
998
999 fprintf ( fp, "%d", enable );
1000 fclose ( fp );
1001 fp = NULL;
1002
1003 return 0;
1004
1005}
1006
1007int CTvin::VDIN_SetDIBypassProg ( int enable )
1008{
1009 FILE *fp = NULL;
1010
1011 fp = fopen ( "/sys/module/di/parameters/bypass_prog", "w" );
1012 if ( fp == NULL ) {
1013 LOGW ( "Open /sys/module/di/parameters/bypass_prog error(%s)!\n", strerror ( errno ) );
1014 return -1;
1015 }
1016
1017 fprintf ( fp, "%d", enable );
1018 fclose ( fp );
1019 fp = NULL;
1020
1021 return 0;
1022}
1023
1024int CTvin::VDIN_SetDIBypassDynamic ( int flag )
1025{
1026 FILE *fp = NULL;
1027
1028 fp = fopen ( "/sys/module/di/parameters/bypass_dynamic", "w" );
1029 if ( fp == NULL ) {
1030 LOGW ( "Open /sys/module/di/parameters/bypass_dynamic error(%s)!\n", strerror ( errno ) );
1031 return -1;
1032 }
1033
1034 fprintf ( fp, "%d", flag );
1035 fclose ( fp );
1036 fp = NULL;
1037
1038 return 0;
1039}
1040
1041int CTvin::VDIN_SetDIDet3DMode ( int value )
1042{
1043 FILE *fp = NULL;
1044
1045 fp = fopen ( "/sys/module/di/parameters/det3d_mode", "w" );
1046 if ( fp == NULL ) {
1047 LOGW ( "Open /sys/module/di/parameters/det3d_mode error(%s)!\n", strerror ( errno ) );
1048 return -1;
1049 }
1050
1051 fprintf ( fp, "%d", value );
1052 fclose ( fp );
1053 fp = NULL;
1054
1055 return 0;
1056}
1057
1058int CTvin::VDIN_SetDIBypass3D ( int enable )
1059{
1060 FILE *fp = NULL;
1061
1062 fp = fopen ( "/sys/module/di/parameters/bypass_3d", "w" );
1063
1064 if ( fp == NULL ) {
1065 LOGW ( "Open /sys/module/di/parameters/bypass_3d error(%s)!\n", strerror ( errno ) );
1066 return -1;
1067 }
1068
1069 fprintf ( fp, "%d", enable );
1070 fclose ( fp );
1071 fp = NULL;
1072
1073 return 0;
1074}
1075
1076int CTvin::VDIN_SetDIBypassPost ( int enable )
1077{
1078 FILE *fp = NULL;
1079
1080 fp = fopen ( "/sys/module/di/parameters/bypass_post", "w" );
1081 if ( fp == NULL ) {
1082 LOGW ( "Open /sys/module/di/parameters/bypass_post error(%s)!\n", strerror ( errno ) );
1083 return -1;
1084 }
1085
1086 fprintf ( fp, "%d", enable );
1087 fclose ( fp );
1088 fp = NULL;
1089
1090 return 0;
1091}
1092
1093int CTvin::set3D_FL_Frame(int value)
1094{
1095 FILE *fp = NULL;
1096 fp = fopen ( "/sys/module/amvideo/parameters/pause_one_3d_fl_frame", "w" );
1097 if ( fp == NULL ) {
1098 LOGW ( "Open /sys/module/amvideo/parameters/pause_one_3d_fl_frame error(%s)!\n", strerror ( errno ) );
1099 return -1;
1100 }
1101 fprintf ( fp, "%d", value );
1102 fclose ( fp );
1103 fp = NULL;
1104 return 0;
1105}
1106
1107int CTvin::setLatchFlag(int value)
1108{
1109 int fd = -1;
1110 int nodeVal = 0;
1111 char s[10];
1112 FILE *fp = NULL;
1113 memset(s, 0, sizeof(s));
1114 read(fd, s, sizeof(s));
1115 close(fd);
1116 nodeVal = atoi(s);
1117 fp = fopen ( "/sys/module/am_vecm/parameters/vecm_latch_flag", "w" );
1118 if ( fp == NULL ) {
1119 LOGW ( "/sys/module/am_vecm/parameters/vecm_latch_flag error(%s)!\n", strerror ( errno ) );
1120 return -1;
1121 }
1122 fprintf ( fp, "%d", value | nodeVal);
1123 fclose ( fp );
1124 fp = NULL;
1125 LOGD ( "read vecm_latch_flag = (%s)====(0x%x), then set vecm_latch_flag | 0x%x = 0x%x.\n", s, nodeVal, value, value | nodeVal);
1126 return 0;
1127}
1128
1129int CTvin::VDIN_SetDIProg_Proc_Config ( int value )
1130{
1131 FILE *fp = NULL;
1132
1133 fp = fopen ( "/sys/module/di/parameters/prog_proc_config", "w" );
1134 if ( fp == NULL ) {
1135 LOGW ( "Open /sys/module/di/parameters/prog_proc_config error(%s)!\n", strerror ( errno ) );
1136 return -1;
1137 }
1138
1139 fprintf ( fp, "%d", value );
1140 fclose ( fp );
1141 fp = NULL;
1142
1143 return 0;
1144}
1145
1146#if(1)
1147int CTvin::VDIN_SetDIInput2Pre ( int value )
1148{
1149 FILE *fp = NULL;
1150
1151 fp = fopen ( "/sys/module/di/parameters/input2pre", "w" );
1152 if ( fp == NULL ) {
1153 LOGW ( "Open /sys/module/di/parameters/input2pre error(%s)!\n", strerror ( errno ) );
1154 return -1;
1155 }
1156
1157 fprintf ( fp, "%d", value );
1158 fclose ( fp );
1159 fp = NULL;
1160
1161 return 0;
1162
1163}
1164#endif
1165int CTvin::VDIN_SetVdinFlag ( int flag )
1166{
1167 FILE *fp = NULL;
1168 int freq = 1200000;
1169
1170 fp = fopen ( "/sys/class/vdin/memp", "w" );
1171 if ( fp == NULL ) {
1172 LOGW ( "Open /sys/class/vdin/memp error(%s)!\n", strerror ( errno ) );
1173 return -1;
1174 }
1175
1176 fprintf ( fp, "%d", flag );
1177 fclose ( fp );
1178 fp = NULL;
1179
1180 return 0;
1181}
1182
1183int CTvin::VDIN_EnableRDMA ( int enable )
1184{
1185 FILE *fp = NULL;
1186 fp = fopen ( "/sys/module/rdma/parameters/enable", "w" );
1187
1188 if ( fp == NULL ) {
1189 LOGW ( "Open /sys/module/rdma/parameters/enable error(%s)!\n", strerror ( errno ) );
1190 return -1;
1191 }
1192
1193 fprintf ( fp, "%d", enable );
1194 fclose ( fp );
1195 fp = NULL;
1196 return 0;
1197}
1198
1199// AFE
1200int CTvin::AFE_OpenModule ( void )
1201{
1202 if ( afe_dev_fd < 0 ) {
1203 afe_dev_fd = open ( AFE_DEV_PATH, O_RDWR );
1204
1205 if ( afe_dev_fd < 0 ) {
1206 LOGW ( "Open tvafe module, error(%s).\n", strerror ( errno ) );
1207 return -1;
1208 }
1209 }
1210
1211 return afe_dev_fd;
1212}
1213
1214void CTvin::AFE_CloseModule ( void )
1215{
1216 if ( afe_dev_fd >= 0 ) {
1217 close ( afe_dev_fd );
1218 afe_dev_fd = -1;
1219 }
1220
1221 return;
1222}
1223
1224int CTvin::AFE_DeviceIOCtl ( int request, ... )
1225{
1226 int tmp_ret = -1;
1227 va_list ap;
1228 void *arg;
1229
1230 if ( afe_dev_fd >= 0 ) {
1231 va_start ( ap, request );
1232 arg = va_arg ( ap, void * );
1233 va_end ( ap );
1234
1235 tmp_ret = ioctl ( afe_dev_fd, request, arg );
1236
1237 return tmp_ret;
1238 }
1239
1240 return -1;
1241}
1242
1243int CTvin::AFE_GetDeviceFileHandle()
1244{
1245 return afe_dev_fd;
1246}
1247
1248int CTvin::AFE_SetVGAEdid ( const unsigned char *ediddata )
1249{
1250 int rt = -1;
1251 struct tvafe_vga_edid_s vgaEdid;
1252
1253#ifdef NO_IC_TEST
1254
1255 for ( int i = 0; i < 256; i++ ) {
1256 test_edid[i] = ediddata[i];
1257 }
1258
1259#endif
1260
1261 for ( int i = 0; i < 256; i++ ) {
1262 vgaEdid.value[i] = ediddata[i];
1263 }
1264
1265 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_EDID, &vgaEdid );
1266
1267 if ( rt < 0 ) {
1268 LOGW ( "AFE_SetVGAEdid, error(%s).!\n", strerror ( errno ) );
1269 }
1270
1271 return rt;
1272}
1273
1274int CTvin::AFE_GetVGAEdid ( unsigned char *ediddata )
1275{
1276 int rt = -1;
1277 struct tvafe_vga_edid_s vgaEdid;
1278
1279#ifdef NO_IC_TEST
1280
1281 for ( int i = 0; i < 256; i++ ) {
1282 ediddata[i] = test_edid[i];
1283 }
1284
1285 LOGD ( "AFE_GetVGAEdid:\n" );
1286 LOGD ( "===================================================\n" );
1287
1288 for ( int i = 0; i < 256; i++ ) {
1289 LOGD ( "vag edid[%d] = [0x%x].\n", i, ediddata[i] );
1290 }
1291
1292 LOGD ( "===================================================\n" );
1293#endif
1294
1295 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_VGA_EDID, &vgaEdid );
1296
1297 for ( int i = 0; i < 256; i++ ) {
1298 ediddata[i] = vgaEdid.value[i];
1299 }
1300
1301 if ( rt < 0 ) {
1302 LOGW ( "AFE_GetVGAEdid, error(%s)!\n", strerror ( errno ) );
1303 }
1304
1305 return rt;
1306}
1307
1308int CTvin::AFE_SetADCTimingAdjust ( const struct tvafe_vga_parm_s *timingadj )
1309{
1310 int rt = -1;
1311
1312 if ( timingadj == NULL ) {
1313 return rt;
1314 }
1315
1316 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, timingadj );
1317
1318 if ( rt < 0 ) {
1319 LOGW ( "AFE_SetADCTimingAdjust, error(%s)!\n", strerror ( errno ) );
1320 }
1321
1322 return rt;
1323}
1324
1325int CTvin::AFE_GetADCCurrentTimingAdjust ( struct tvafe_vga_parm_s *timingadj )
1326{
1327 int rt = -1;
1328
1329 if ( timingadj == NULL ) {
1330 return rt;
1331 }
1332
1333 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_VGA_PARM, timingadj );
1334
1335 if ( rt < 0 ) {
1336 LOGW ( "AFE_GetADCCurrentTimingAdjust, error(%s)!\n", strerror ( errno ) );
1337 return -1;
1338 }
1339
1340 return 0;
1341}
1342
1343int CTvin::AFE_VGAAutoAdjust ( struct tvafe_vga_parm_s *timingadj )
1344{
1345 enum tvafe_cmd_status_e CMDStatus = TVAFE_CMD_STATUS_PROCESSING;
1346 struct tvin_parm_s tvin_para;
1347 int rt = -1, i = 0;
1348
1349 if ( timingadj == NULL ) {
1350 return -1;
1351 }
1352
1353 for ( i = 0, CMDStatus == TVAFE_CMD_STATUS_PROCESSING; i < 50; i++ ) {
1354 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &CMDStatus );
1355
1356 if ( rt < 0 ) {
1357 LOGD ( "get afe CMD status, error(%s), fd(%d), return(%d).\n", strerror ( errno ), AFE_GetDeviceFileHandle(), rt );
1358 }
1359
1360 if ( ( CMDStatus == TVAFE_CMD_STATUS_IDLE ) || ( CMDStatus == TVAFE_CMD_STATUS_SUCCESSFUL ) ) {
1361 break;
1362 }
1363
1364 usleep ( 10 * 1000 );
1365 }
1366
1367 if ( ( CMDStatus == TVAFE_CMD_STATUS_PROCESSING ) || ( CMDStatus == TVAFE_CMD_STATUS_FAILED ) ) {
1368 return -1;
1369 }
1370
1371 for ( i = 0; i < 100; i++ ) {
1372 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, &tvin_para );
1373
1374 if ( tvin_para.info.status == TVIN_SIG_STATUS_STABLE ) {
1375 break;
1376 }
1377
1378 usleep ( 10 * 1000 );
1379 }
1380
1381 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_AUTO );
1382
1383 if ( rt < 0 ) {
1384 timingadj->clk_step = 0;
1385 timingadj->phase = 0;
1386 timingadj->hpos_step = 0;
1387 timingadj->vpos_step = 0;
1388 AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, timingadj );
1389 return rt;
1390 } else {
1391 ;//AFE_DeviceIOCtl(TVIN_IOC_G_AFE_VGA_PARM, timingadj);
1392 }
1393
1394 for ( i = 0; i < 10; i++ ) {
1395 sleep ( 1 );
1396
1397 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &CMDStatus );
1398
1399 if ( rt < 0 ) {
1400 return rt;
1401 } else {
1402 if ( CMDStatus == TVAFE_CMD_STATUS_SUCCESSFUL ) {
1403 usleep ( 100 * 1000 );
1404 AFE_GetADCCurrentTimingAdjust ( timingadj );
1405 LOGD ( "===================================================\n" );
1406 LOGW ( "AFE_VGAAutoAdjust, successfull!\n" );
1407 return 0;
1408 }
1409 }
1410 }
1411
1412 return -1;
1413}
1414
1415int CTvin::AFE_SetVGAAutoAjust ( void )
1416{
1417 int rt = -1;
1418 tvafe_vga_parm_t timingadj;
1419 tvafe_cmd_status_t Status;
1420 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &Status );
1421
1422 if ( ( Status == TVAFE_CMD_STATUS_IDLE ) || ( Status == TVAFE_CMD_STATUS_SUCCESSFUL ) ) {
1423 ;
1424 } else {
1425 LOGW ( "AFE_SetVGAAutoAjust, TVIN_IOC_G_AFE_CMD_STATUS failed!\n" );
1426 return -1;
1427 }
1428
1429 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_AUTO );
1430
1431 if ( rt < 0 ) {
1432 timingadj.clk_step = 0;
1433 timingadj.phase = 0;
1434 timingadj.hpos_step = 0;
1435 timingadj.vpos_step = 0;
1436 AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, &timingadj );
1437 return rt;
1438 }
1439
1440 return 0;
1441}
1442
1443int CTvin::AFE_GetVGAAutoAdjustCMDStatus ( tvafe_cmd_status_t *Status )
1444{
1445 int rt = -1;
1446
1447 if ( Status == NULL ) {
1448 return rt;
1449 }
1450
1451 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, Status );
1452
1453 if ( rt < 0 ) {
1454 LOGW ( "AFE_GetVGAAutoAdjustStatus, get status, error(%s) fd(%d) return(%d)\n", strerror ( errno ), AFE_GetDeviceFileHandle(), rt );
1455 return rt;
1456 }
1457
1458 return 0;
1459}
1460
1461int CTvin::AFE_GetAdcCal ( struct tvafe_adc_cal_s *adccalvalue )
1462{
1463 int rt = -1;
1464
1465 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_ADC_CAL, adccalvalue );
1466
1467 if ( rt < 0 ) {
1468 LOGW ( "AFE_GetADCGainOffset, error(%s)!\n", strerror ( errno ) );
1469 }
1470
1471 return rt;
1472}
1473
1474int CTvin::AFE_SetAdcCal ( struct tvafe_adc_cal_s *adccalvalue )
1475{
1476 int rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_ADC_CAL, adccalvalue );
1477
1478 if ( rt < 0 ) {
1479 LOGW ( "AFE_SetAdcCal, error(%s)!", strerror ( errno ) );
1480 }
1481
1482 return rt;
1483}
1484
1485int CTvin::AFE_GetAdcCompCal ( struct tvafe_adc_comp_cal_s *adccalvalue )
1486{
1487 int rt = -1;
1488
1489 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_ADC_COMP_CAL, adccalvalue );
1490
1491 if ( rt < 0 ) {
1492 LOGW ( "AFE_GetYPbPrADCGainOffset, error(%s)!\n", strerror ( errno ) );
1493 }
1494
1495 return rt;
1496}
1497
1498int CTvin::AFE_SetAdcCompCal ( struct tvafe_adc_comp_cal_s *adccalvalue )
1499{
1500 int rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_ADC_COMP_CAL, adccalvalue );
1501
1502 if ( rt < 0 ) {
1503 LOGW ( "AFE_SetYPbPrADCGainOffset, error(%s)!", strerror ( errno ) );
1504 }
1505
1506 return rt;
1507}
1508int CTvin::AFE_GetYPbPrWSSinfo ( struct tvafe_comp_wss_s *wssinfo )
1509{
1510 int rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_COMP_WSS, wssinfo );
1511
1512 if ( rt < 0 ) {
1513 LOGW ( "AFE_GetYPbPrWSSinfo, error(%s)!", strerror ( errno ) );
1514 }
1515
1516 return rt;
1517}
1518
1519#define RGB444 3
1520#define YCBCR422 2
1521#define YCBCR444 3
1522#define Y422_POS 0
1523#define CB422_POS 1
1524#define CR422_POS 3
1525#define Y444_POS 0
1526#define CB444_POS 1
1527#define CR444_POS 2
1528#define R444_POS 0
1529#define G444_POS 1
1530#define B444_POS 2
1531
1532//=========== VGA =====================
1533#define VGA_BUF_WID (VGA_H_ACTIVE)
1534
1535#ifdef PATTERN_7_COLOR_BAR
1536#define VGA_BAR_WID (VGA_H_ACTIVE/7)
1537#define VGA_H_CUT_WID (10)
1538#else
1539#define VGA_BAR_WID (VGA_H_ACTIVE/8)
1540#define VGA_H_CUT_WID (10)
1541#endif
1542
1543#define VGA_V_CUT_WID (40)
1544#define VGA_V_CAL_WID (200+VGA_V_CUT_WID)
1545
1546#define VGA_WHITE_HS (VGA_BAR_WID*0+VGA_H_CUT_WID)
1547#define VGA_WHITE_HE (VGA_BAR_WID*1-VGA_H_CUT_WID-1)
1548#define VGA_WHITE_VS (VGA_V_CUT_WID)
1549#define VGA_WHITE_VE (VGA_V_CAL_WID-1)
1550#define VGA_WHITE_SIZE ((VGA_WHITE_HE-VGA_WHITE_HS+1)*(VGA_WHITE_VE-VGA_WHITE_VS+1))
1551#ifdef PATTERN_7_COLOR_BAR
1552#define VGA_BLACK_HS (VGA_BAR_WID*6+VGA_H_CUT_WID)
1553#define VGA_BLACK_HE (VGA_BAR_WID*7-VGA_H_CUT_WID-1)
1554#define VGA_BLACK_VS (768-140)
1555#define VGA_BLACK_VE (768-40-1)
1556#define VGA_BLACK_SIZE ((VGA_BLACK_HE-VGA_BLACK_HS+1)*(VGA_BLACK_VE-VGA_BLACK_VS+1))
1557#else
1558#define VGA_BLACK_HS (VGA_BAR_WID*7+VGA_H_CUT_WID)
1559#define VGA_BLACK_HE (VGA_BAR_WID*8-VGA_H_CUT_WID-1)
1560#define VGA_BLACK_VS (VGA_V_CUT_WID)
1561#define VGA_BLACK_VE (VGA_V_CAL_WID-1)
1562#define VGA_BLACK_SIZE ((VGA_BLACK_HE-VGA_BLACK_HS+1)*(VGA_BLACK_VE-VGA_BLACK_VS+1))
1563#endif
1564
1565//=========== YPBPR =====================
1566#define COMP_BUF_WID (COMP_H_ACTIVE)
1567
1568#define COMP_BAR_WID (COMP_H_ACTIVE/8)
1569#define COMP_H_CUT_WID (20)
1570#define COMP_V_CUT_WID (100)
1571#define COMP_V_CAL_WID (200+COMP_V_CUT_WID)
1572
1573#define COMP_WHITE_HS (COMP_BAR_WID*0+COMP_H_CUT_WID)
1574#define COMP_WHITE_HE (COMP_BAR_WID*1-COMP_H_CUT_WID-1)
1575#define COMP_WHITE_VS (COMP_V_CUT_WID)
1576#define COMP_WHITE_VE (COMP_V_CAL_WID-1)
1577#define COMP_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1))
1578#define CB_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1)/2)
1579#define CR_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1)/2)
1580
1581#define COMP_YELLOW_HS (COMP_BAR_WID*1+COMP_H_CUT_WID)
1582#define COMP_YELLOW_HE (COMP_BAR_WID*2-COMP_H_CUT_WID-1)
1583#define COMP_YELLOW_VS (COMP_V_CUT_WID)
1584#define COMP_YELLOW_VE (COMP_V_CAL_WID-1)
1585#define COMP_YELLOW_SIZE ((COMP_YELLOW_HE-COMP_YELLOW_HS+1)*(COMP_YELLOW_VE-COMP_YELLOW_VS+1)/2)
1586
1587#define COMP_CYAN_HS (COMP_BAR_WID*2+COMP_H_CUT_WID)
1588#define COMP_CYAN_HE (COMP_BAR_WID*3-COMP_H_CUT_WID-1)
1589#define COMP_CYAN_VS (COMP_V_CUT_WID)
1590#define COMP_CYAN_VE (COMP_V_CAL_WID-1)
1591#define COMP_CYAN_SIZE ((COMP_CYAN_HE-COMP_CYAN_HS+1)*(COMP_CYAN_VE-COMP_CYAN_VS+1)/2)
1592
1593#define COMP_RED_HS (COMP_BAR_WID*5+COMP_H_CUT_WID)
1594#define COMP_RED_HE (COMP_BAR_WID*6-COMP_H_CUT_WID-1)
1595#define COMP_RED_VS (COMP_V_CUT_WID)
1596#define COMP_RED_VE (COMP_V_CAL_WID-1)
1597#define COMP_RED_SIZE ((COMP_RED_HE-COMP_RED_HS+1)*(COMP_RED_VE-COMP_RED_VS+1)/2)
1598
1599#define COMP_BLUE_HS (COMP_BAR_WID*6+COMP_H_CUT_WID)
1600#define COMP_BLUE_HE (COMP_BAR_WID*7-COMP_H_CUT_WID-1)
1601#define COMP_BLUE_VS (COMP_V_CUT_WID)
1602#define COMP_BLUE_VE (COMP_V_CAL_WID-1)
1603#define COMP_BLUE_SIZE ((COMP_BLUE_HE-COMP_BLUE_HS+1)*(COMP_BLUE_VE-COMP_BLUE_VS+1)/2)
1604
1605#define COMP_BLACK_HS (COMP_BAR_WID*7+COMP_H_CUT_WID)
1606#define COMP_BLACK_HE (COMP_BAR_WID*8-COMP_H_CUT_WID-1)
1607#define COMP_BLACK_VS (COMP_V_CUT_WID)
1608#define COMP_BLACK_VE (COMP_V_CAL_WID-1)
1609#define COMP_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1))
1610#define CB_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1)/2)
1611#define CR_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1)/2)
1612
1613//=========== CVBS =====================
1614#define CVBS_BUF_WID (CVBS_H_ACTIVE)
1615#define CVBS_BAR_WID (CVBS_H_ACTIVE/8)
1616#define CVBS_H_CUT_WID (20)
1617
1618#define CVBS_V_CUT_WID (40)
1619#define CVBS_V_CAL_WID (140+CVBS_V_CUT_WID)
1620
1621#define CVBS_WHITE_HS (CVBS_BAR_WID*0+CVBS_H_CUT_WID)
1622#define CVBS_WHITE_HE (CVBS_BAR_WID*1-CVBS_H_CUT_WID-1)
1623#define CVBS_WHITE_VS (CVBS_V_CUT_WID)
1624#define CVBS_WHITE_VE (CVBS_V_CAL_WID-1)
1625#define CVBS_WHITE_SIZE ((CVBS_WHITE_HE-CVBS_WHITE_HS+1)*(CVBS_WHITE_VE-CVBS_WHITE_VS+1))
1626
1627#define CVBS_BLACK_HS (CVBS_BAR_WID*7+CVBS_H_CUT_WID)
1628#define CVBS_BLACK_HE (CVBS_BAR_WID*8-CVBS_H_CUT_WID-1)
1629#define CVBS_BLACK_VS (CVBS_V_CUT_WID)
1630#define CVBS_BLACK_VE (CVBS_V_CAL_WID-1)
1631#define CVBS_BLACK_SIZE ((CVBS_BLACK_HE-CVBS_BLACK_HS+1)*(CVBS_BLACK_VE-CVBS_BLACK_VS+1))
1632
1633#define COMP_CAP_SIZE (COMP_H_ACTIVE*COMP_V_ACTIVE*YCBCR422)
1634#ifdef VGA_SOURCE_RGB444
1635#define VGA_CAP_SIZE (VGA_H_ACTIVE*VGA_V_ACTIVE*RGB444)
1636#else
1637#define VGA_CAP_SIZE (VGA_H_ACTIVE*VGA_V_ACTIVE*YCBCR444)
1638#endif
1639#define CVBS_CAP_SIZE (CVBS_H_ACTIVE*CVBS_V_ACTIVE)
1640
1641#define PRE_0 -16
1642#define PRE_1 -128
1643#define PRE_2 -128
1644#define COEF_00 1.164
1645#define COEF_01 0
1646#define COEF_02 1.793
1647#define COEF_10 1.164
1648#define COEF_11 -0.213
1649#define COEF_12 -0.534
1650#define COEF_20 1.164
1651#define COEF_21 2.115
1652#define COEF_22 0
1653#define POST_0 0
1654#define POST_1 0
1655#define POST_2 0
1656
1657unsigned int CTvin::data_limit ( float data )
1658{
1659 if ( data < 0 ) {
1660 return ( 0 );
1661 } else if ( data > 255 ) {
1662 return ( 255 );
1663 } else {
1664 return ( ( unsigned int ) data );
1665 }
1666}
1667
1668void CTvin::matrix_convert_yuv709_to_rgb ( unsigned int y, unsigned int u, unsigned int v, unsigned int *r, unsigned int *g, unsigned int *b )
1669{
1670 *r = data_limit ( ( ( float ) y + PRE_0 ) * COEF_00 + ( ( float ) u + PRE_1 ) * COEF_01 + ( ( float ) v + PRE_2 ) * COEF_02 + POST_0 + 0.5 );
1671 *g = data_limit ( ( ( float ) y + PRE_0 ) * COEF_10 + ( ( float ) u + PRE_1 ) * COEF_11 + ( ( float ) v + PRE_2 ) * COEF_12 + POST_1 + 0.5 );
1672 *b = data_limit ( ( ( float ) y + PRE_0 ) * COEF_20 + ( ( float ) u + PRE_1 ) * COEF_21 + ( ( float ) v + PRE_2 ) * COEF_22 + POST_2 + 0.5 );
1673}
1674
1675void CTvin::re_order ( unsigned int *a, unsigned int *b )
1676{
1677 unsigned int c = 0;
1678
1679 if ( *a > *b ) {
1680 c = *a;
1681 *a = *b;
1682 *b = c;
1683 }
1684}
1685
1686char *CTvin::get_cap_addr ( enum adc_cal_type_e calType )
1687{
1688 int n;
1689 char *dp;
1690
1691 for ( n = 0; n < 0x00ff; n++ ) {
1692 if ( VDIN_DeviceIOCtl ( TVIN_IOC_G_SIG_INFO, &gTvinAFESignalInfo ) < 0 ) {
1693 LOGW ( "get_cap_addr, get signal info, error(%s),fd(%d).\n", strerror ( errno ), m_vdin_dev_fd );
1694 return NULL;
1695 } else {
1696 if ( gTvinAFESignalInfo.status == TVIN_SIG_STATUS_STABLE ) {
1697 gTvinAFEParam.info.fmt = gTvinAFESignalInfo.fmt;
1698 break;
1699 }
1700 }
1701 }
1702
1703 if ( gTvinAFESignalInfo.status != TVIN_SIG_STATUS_STABLE ) {
1704 LOGD ( "get_cap_addr, signal isn't stable, out of calibration!\n" );
1705 return NULL;
1706 } else {
1707 if ( VDIN_DeviceIOCtl ( TVIN_IOC_STOP_DEC ) < 0 ) {
1708 LOGW ( "get_cap_addr, stop vdin, error (%s).\n", strerror ( errno ) );
1709 return NULL;
1710 }
1711
1712 usleep ( 1000 );
1713
1714 if ( calType == CAL_YPBPR ) {
1715 dp = ( char * ) mmap ( NULL, COMP_CAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_vdin_dev_fd, 0 );
1716 } else {
1717 dp = ( char * ) mmap ( NULL, VGA_CAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_vdin_dev_fd, 0 );
1718 }
1719
1720 if ( dp == NULL ) {
1721 LOGE ( "get_cap_addr, mmap failed!\n" );
1722 }
1723
1724 return dp;
1725 }
1726
1727 return NULL;
1728}
1729
1730inline unsigned char CTvin::get_mem_data ( char *dp, unsigned int addr )
1731{
1732 return ( * ( dp + ( addr ^ 7 ) ) );
1733}
1734
1735int CTvin::get_frame_average ( enum adc_cal_type_e calType, struct adc_cal_s *mem_data )
1736{
1737 unsigned int y = 0, cb = 0, cr = 0;
1738 unsigned int r = 0, g = 0, b = 0;
1739 unsigned long n;
1740 unsigned int i = 0, j = 0;
1741 char *dp = get_cap_addr ( calType );
1742
1743 if ( calType == CAL_YPBPR ) {
1744 for ( j = COMP_WHITE_VS; j <= COMP_WHITE_VE; j++ ) {
1745 for ( i = COMP_WHITE_HS; i <= COMP_WHITE_HE; i++ ) {
1746 mem_data->g_y_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 ) );
1747 }
1748 }
1749
1750 mem_data->g_y_max /= COMP_WHITE_SIZE;
1751
1752 for ( j = COMP_WHITE_VS; j <= COMP_WHITE_VE; j++ ) {
1753 for ( i = COMP_WHITE_HS; i <= COMP_WHITE_HE; ) {
1754 mem_data->cb_white += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1755 mem_data->cr_white += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1756 i = i + 2;
1757 }
1758 }
1759
1760 mem_data->cb_white /= CB_WHITE_SIZE;
1761 mem_data->cr_white /= CR_WHITE_SIZE;
1762
1763 for ( j = COMP_RED_VS; j <= COMP_RED_VE; j++ ) {
1764 for ( i = COMP_RED_HS; i <= COMP_RED_HE; ) {
1765 mem_data->rcr_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1766 i = i + 2;
1767 }
1768 }
1769
1770 mem_data->rcr_max /= COMP_RED_SIZE;
1771
1772 for ( j = COMP_BLUE_VS; j <= COMP_BLUE_VE; j++ ) {
1773 for ( i = COMP_BLUE_HS; i <= COMP_BLUE_HE; ) {
1774 mem_data->bcb_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1775 i = i + 2;
1776 }
1777 }
1778
1779 mem_data->bcb_max /= COMP_BLUE_SIZE;
1780
1781 for ( j = COMP_BLACK_VS; j <= COMP_BLACK_VE; j++ ) {
1782 for ( i = COMP_BLACK_HS; i <= COMP_BLACK_HE; i++ ) {
1783 mem_data->g_y_min += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 ) );
1784 }
1785 }
1786
1787 mem_data->g_y_min /= COMP_BLACK_SIZE;
1788
1789 for ( j = COMP_BLACK_VS; j <= COMP_BLACK_VE; j++ ) {
1790 for ( i = COMP_BLACK_HS; i <= COMP_BLACK_HE; ) {
1791 mem_data->cb_black += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1792 mem_data->cr_black += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1793 i = i + 2;
1794 }
1795 }
1796
1797 mem_data->cb_black /= CB_BLACK_SIZE;
1798 mem_data->cr_black /= CR_BLACK_SIZE;
1799
1800 /*
1801 for (j=COMP_BLACK_VS; j<=COMP_BLACK_VE; j++) {
1802 for (i=COMP_BLACK_HS; i<=COMP_BLACK_HE;) {
1803 //mem_data->cb_black += get_mem_data(dp, ((COMP_BUF_WID*j+i)*YCBCR422+CB422_POS));
1804 mem_data->cr_black += get_mem_data(dp, ((COMP_BUF_WID*j+i)*YCBCR422+CR422_POS));
1805 i = i+2;
1806 }
1807 }
1808 mem_data->cr_black /= CR_BLACK_SIZE;
1809 */
1810 for ( j = COMP_CYAN_VS; j <= COMP_CYAN_VE; j++ ) {
1811 for ( i = COMP_CYAN_HS; i <= COMP_CYAN_HE; ) {
1812 mem_data->rcr_min += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1813 i = i + 2;
1814 }
1815 }
1816
1817 mem_data->rcr_min /= COMP_CYAN_SIZE;
1818
1819 for ( j = COMP_YELLOW_VS; j <= COMP_YELLOW_VE; j++ ) {
1820 for ( i = COMP_YELLOW_HS; i <= COMP_YELLOW_HE; ) {
1821 mem_data->bcb_min += get_mem_data ( dp, ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS );
1822 i = i + 2;
1823 }
1824 }
1825
1826 mem_data->bcb_min /= COMP_YELLOW_SIZE;
1827
1828 } else if ( calType == CAL_VGA ) {
1829 for ( j = VGA_WHITE_VS; j <= VGA_WHITE_VE; j++ ) {
1830 for ( i = VGA_WHITE_HS; i <= VGA_WHITE_HE; i++ ) {
1831#ifdef VGA_SOURCE_RGB444
1832 r = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + R444_POS ) );
1833 g = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + G444_POS ) );
1834 b = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + B444_POS ) );
1835#else
1836 y = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + Y444_POS ) );
1837 cb = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CB444_POS ) );
1838 cr = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CR444_POS ) );
1839 matrix_convert_yuv709_to_rgb ( y, cb, cr, &r, &g, &b );
1840#endif
1841 mem_data->rcr_max = mem_data->rcr_max + r;
1842 mem_data->g_y_max = mem_data->g_y_max + g;
1843 mem_data->bcb_max = mem_data->bcb_max + b;
1844 }
1845 }
1846
1847 mem_data->rcr_max = mem_data->rcr_max / VGA_WHITE_SIZE;
1848 mem_data->g_y_max = mem_data->g_y_max / VGA_WHITE_SIZE;
1849 mem_data->bcb_max = mem_data->bcb_max / VGA_WHITE_SIZE;
1850
1851 for ( j = VGA_BLACK_VS; j <= VGA_BLACK_VE; j++ ) {
1852 for ( i = VGA_BLACK_HS; i <= VGA_BLACK_HE; i++ ) {
1853#ifdef VGA_SOURCE_RGB444
1854 r = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + R444_POS ) );
1855 g = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + G444_POS ) );
1856 b = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + B444_POS ) );
1857#else
1858 y = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + Y444_POS ) );
1859 cb = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CB444_POS ) );
1860 cr = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CR444_POS ) );
1861 matrix_convert_yuv709_to_rgb ( y, cb, cr, &r, &g, &b );
1862#endif
1863 mem_data->rcr_min = mem_data->rcr_min + r;
1864 mem_data->g_y_min = mem_data->g_y_min + g;
1865 mem_data->bcb_min = mem_data->bcb_min + b;
1866 }
1867 }
1868
1869 mem_data->rcr_min = mem_data->rcr_min / VGA_BLACK_SIZE;
1870 mem_data->g_y_min = mem_data->g_y_min / VGA_BLACK_SIZE;
1871 mem_data->bcb_min = mem_data->bcb_min / VGA_BLACK_SIZE;
1872
1873 } else { //CVBS
1874 for ( j = CVBS_WHITE_VS; j <= CVBS_WHITE_VE; j++ ) {
1875 for ( i = CVBS_WHITE_HS; i <= CVBS_WHITE_HE; i++ ) {
1876 mem_data->g_y_max += mem_data->g_y_max + get_mem_data ( dp, ( ( CVBS_BUF_WID * j + i ) * YCBCR422 ) );
1877 }
1878 }
1879
1880 mem_data->g_y_max /= COMP_WHITE_SIZE;
1881
1882 for ( j = CVBS_BLACK_VS; j <= CVBS_BLACK_VE; j++ ) {
1883 for ( i = CVBS_BLACK_HS; i <= CVBS_BLACK_HE; i++ ) {
1884 mem_data->g_y_min += mem_data->g_y_min + get_mem_data ( dp, ( ( CVBS_BUF_WID * j + i ) * YCBCR422 ) );
1885 }
1886 }
1887
1888 mem_data->g_y_min /= CVBS_BLACK_SIZE;
1889 }
1890
1891 if ( calType == CAL_YPBPR ) {
1892 munmap ( dp, COMP_CAP_SIZE );
1893 } else if ( calType == CAL_VGA ) {
1894 munmap ( dp, VGA_CAP_SIZE );
1895 } else {
1896 munmap ( dp, CVBS_CAP_SIZE );
1897 }
1898
1899 if ( VDIN_DeviceIOCtl ( TVIN_IOC_START_DEC, &gTvinAFEParam ) < 0 ) {
1900 LOGW ( "get_frame_average, get vdin signal info, error(%s),fd(%d).\n", strerror ( errno ), m_vdin_dev_fd );
1901 return 0;
1902 }
1903
1904 return 0;
1905}
1906
1907#define ADC_CAL_FRAME_QTY_ORDER 2 //NOTE: MUST >=2!!
1908struct adc_cal_s CTvin::get_n_frame_average ( enum adc_cal_type_e calType )
1909{
1910 struct adc_cal_s mem_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1911 unsigned int rcrmax[1 << ADC_CAL_FRAME_QTY_ORDER];
1912 unsigned int rcrmin[1 << ADC_CAL_FRAME_QTY_ORDER];
1913 unsigned int g_ymax[1 << ADC_CAL_FRAME_QTY_ORDER];
1914 unsigned int g_ymin[1 << ADC_CAL_FRAME_QTY_ORDER];
1915 unsigned int bcbmax[1 << ADC_CAL_FRAME_QTY_ORDER];
1916 unsigned int bcbmin[1 << ADC_CAL_FRAME_QTY_ORDER];
1917 unsigned int cbwhite[1 << ADC_CAL_FRAME_QTY_ORDER];
1918 unsigned int crwhite[1 << ADC_CAL_FRAME_QTY_ORDER];
1919 unsigned int cbblack[1 << ADC_CAL_FRAME_QTY_ORDER];
1920 unsigned int crblack[1 << ADC_CAL_FRAME_QTY_ORDER];
1921 unsigned int i = 0, j = 0;
1922
1923 for ( i = 0; i < ( 1 << ADC_CAL_FRAME_QTY_ORDER ); i++ ) {
1924 get_frame_average ( calType, &mem_data );
1925 rcrmax[i] = mem_data.rcr_max;
1926 rcrmin[i] = mem_data.rcr_min;
1927 g_ymax[i] = mem_data.g_y_max;
1928 g_ymin[i] = mem_data.g_y_min;
1929 bcbmax[i] = mem_data.bcb_max;
1930 bcbmin[i] = mem_data.bcb_min;
1931 cbwhite[i] = mem_data.cb_white;
1932 crwhite[i] = mem_data.cr_white;
1933 cbblack[i] = mem_data.cb_black;
1934 crblack[i] = mem_data.cr_black;
1935 }
1936
1937 for ( i = 0; i < ( 1 << ADC_CAL_FRAME_QTY_ORDER ) - 1; i++ ) {
1938 for ( j = 1; j < ( 1 << ADC_CAL_FRAME_QTY_ORDER ); j++ ) {
1939 re_order ( & ( rcrmax[i] ), & ( rcrmax[j] ) );
1940 re_order ( & ( rcrmin[i] ), & ( rcrmin[j] ) );
1941 re_order ( & ( g_ymax[i] ), & ( g_ymax[j] ) );
1942 re_order ( & ( g_ymin[i] ), & ( g_ymin[j] ) );
1943 re_order ( & ( bcbmax[i] ), & ( bcbmax[j] ) );
1944 re_order ( & ( bcbmin[i] ), & ( bcbmin[j] ) );
1945 re_order ( & ( cbwhite[i] ), & ( cbwhite[j] ) );
1946 re_order ( & ( crwhite[i] ), & ( crwhite[j] ) );
1947 re_order ( & ( cbblack[i] ), & ( cbblack[j] ) );
1948 re_order ( & ( crblack[i] ), & ( crblack[j] ) );
1949 }
1950 }
1951
1952 memset ( &mem_data, 0, sizeof ( mem_data ) );
1953
1954 for ( i = 0; i < ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 1 ) ); i++ ) { //(1<<(ADC_CAL_FRAME_QTY_ORDER-1))
1955 mem_data.rcr_max += rcrmax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1956 mem_data.rcr_min += rcrmin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1957 mem_data.g_y_max += g_ymax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1958 mem_data.g_y_min += g_ymin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1959 mem_data.bcb_max += bcbmax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1960 mem_data.bcb_min += bcbmin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1961 mem_data.cb_white += cbwhite[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1962 mem_data.cr_white += crwhite[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1963 mem_data.cb_black += cbblack[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1964 mem_data.cr_black += crblack[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1965 }
1966
1967
1968 mem_data.rcr_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1969 mem_data.rcr_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1970 mem_data.g_y_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1971 mem_data.g_y_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1972 mem_data.bcb_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1973 mem_data.bcb_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1974 mem_data.cb_white >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1975 mem_data.cr_white >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1976 mem_data.cb_black >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1977 mem_data.cr_black >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1978
1979 return mem_data;
1980}
1981
1982int CTvin::AFE_GetMemData ( int typeSel, struct adc_cal_s *mem_data )
1983{
1984 int rt = -1;
1985
1986 if ( m_vdin_dev_fd < 0 || mem_data == NULL ) {
1987 LOGW ( "AFE_GetMemData, didn't open vdin fd, return!\n" );
1988 return -1;
1989 }
1990
1991 memset ( &gTvinAFEParam, 0, sizeof ( gTvinAFEParam ) );
1992 memset ( &gTvinAFESignalInfo, 0, sizeof ( gTvinAFESignalInfo ) );
1993
1994 if ( VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, &gTvinAFEParam ) < 0 ) {
1995 LOGW ( "AFE_GetMemData, get vdin param, error(%s), fd(%d)!\n", strerror ( errno ), m_vdin_dev_fd );
1996 return -1;
1997 }
1998
1999 gTvinAFEParam.flag = gTvinAFEParam.flag | TVIN_PARM_FLAG_CAP;
2000
2001 if ( VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, &gTvinAFEParam ) < 0 ) {
2002 LOGW ( "AFE_GetMemData, set vdin param error(%s)!\n", strerror ( errno ) );
2003 return -1;
2004 }
2005
2006 if ( typeSel == 0 ) {
2007 get_frame_average ( CAL_YPBPR, mem_data );
2008 } else if ( typeSel == 1 ) {
2009 get_frame_average ( CAL_VGA, mem_data );
2010 } else {
2011 *mem_data = get_n_frame_average ( CAL_CVBS );
2012 }
2013
2014 gTvinAFEParam.flag &= 0x11111110;
2015
2016 if ( VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, &gTvinAFEParam ) < 0 ) {
2017 LOGW ( "AFE_GetMemData, set vdin param error(%s)\n", strerror ( errno ) );
2018 return -1;
2019 }
2020
2021 LOGD ( "AFE_GetMemData, MAX ======> :\n Y(White)->%d \n Cb(Blue)->%d \n Cr(Red)->%d\n", mem_data->g_y_max, mem_data->bcb_max, mem_data->rcr_max );
2022 LOGD ( "AFE_GetMemData, MIN ======>:\n Y(Black)->%d \n Cb(Yellow)->%d \n Cr(Cyan)->%d\n Cb(White) ->%d\n Cb(Black)->%d\n Cr(Black)->%d\n", mem_data->g_y_min, mem_data->bcb_min, mem_data->rcr_min,
2023 mem_data->cb_white, mem_data->cb_black, mem_data->cr_black );
2024 return 0;
2025}
2026
2027int CTvin::AFE_GetCVBSLockStatus ( enum tvafe_cvbs_video_e *cvbs_lock_status )
2028{
2029 int rt = -1;
2030
2031 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CVBS_LOCK, cvbs_lock_status );
2032
2033 if ( rt < 0 ) {
2034 LOGD ( "AFE_GetCVBSLockStatus, error: return(%d), error(%s)!\n", rt, strerror ( errno ) );
2035 } else {
2036 LOGD ( "AFE_GetCVBSLockStatus, value=%d.\n", *cvbs_lock_status );
2037 }
2038
2039 return *cvbs_lock_status;
2040}
2041
2042int CTvin::AFE_SetCVBSStd ( tvin_sig_fmt_t fmt )
2043{
2044 int rt = -1;
2045
2046 LOGD ( "AFE_SetCVBSStd, sig_fmt = %d\n", fmt );
2047 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_CVBS_STD, &fmt );
2048
2049 if ( rt < 0 ) {
2050 LOGD ( "AFE_SetCVBSStd, error: return(%d), error(%s)!\n", rt, strerror ( errno ) );
2051 }
2052
2053 return rt;
2054}
2055
2056int CTvin::TvinApi_SetStartDropFrameCn ( int count )
2057{
2058 int ret = -1;
2059 char set_str[4];
2060
2061 memset ( set_str, 0, 4 );
2062 sprintf ( set_str, "%d", count );
2063 return SetFileAttrValue ( "/sys/module/di/parameters/start_frame_drop_count", set_str );
2064}
2065
2066int CTvin::TvinApi_SetVdinHVScale ( int vdinx, int hscale, int vscale )
2067{
2068 int ret = -1;
2069 char set_str[32];
2070
2071 memset ( set_str, 0, 32 );
2072 sprintf ( set_str, "%s %d %d", "hvscaler", hscale, vscale );
2073
2074 if ( vdinx == 0 ) {
2075 ret = SetFileAttrValue ( "/sys/class/vdin/vdin0/attr", set_str );
2076 } else {
2077 ret = SetFileAttrValue ( "/sys/class/vdin/vdin1/attr", set_str );
2078 }
2079
2080 return ret;
2081}
2082int CTvin::TvinApi_SetCompPhase ( am_phase_t &am_phase )
2083{
2084 int ret = -1, fd = -1;
2085 unsigned int i = 0;
2086 int idx = 0, len = 0;
2087 char str1[200], str2[100];
2088
2089 LOGD ( "enter,TvinApi_SetCompPhase" );
2090
2091 fd = open ( "/sys/module/tvin_afe/parameters/comp_phase", O_RDWR );
2092 if ( fd < 0 ) {
2093 LOGW ( "Open vdin_comp_phase_op_mutex error(%s)!!\n", strerror ( errno ) );
2094 return -1;
2095 }
2096
2097 for ( i = 0; i < am_phase.length; i++ ) {
2098 sprintf ( &str1[idx], "%d,", am_phase.phase[i] );
2099 sprintf ( str2, "%d,", am_phase.phase[i] );
2100 int len = strlen ( str2 );
2101 idx = idx + len;
2102 }
2103
2104 LOGD ( "##########str1 = %s\n", str1 );
2105
2106 ret = write ( fd, str1, strlen ( str1 ) );
2107 if ( ret < 0 ) {
2108 LOGD ( "Write vdin_comp_phase_op_mutex error(%s)!!\n", strerror ( errno ) );
2109 }
2110
2111 LOGD ( "write ok!!!" );
2112 close ( fd );
2113 fd = -1;
2114
2115 return ret;
2116}
2117
2118tvin_trans_fmt CTvin::TvinApi_Get3DDectMode()
2119{
2120 int fd;
2121 int ret;
2122 char det_3d[10];
2123 int det_3dmode = 8;
2124 //LOGW("det_3dmode %d\n", det_3dmode);
2125
2126 fd = open ( "/sys/module/di/parameters/det3d_mode", O_RDWR );
2127 if ( fd < 0 ) {
2128 LOGW ( "/sys/module/di/parameters/det3d_mode error(%s)!!\n", strerror ( errno ) );
2129
2130 return TVIN_TFMT_3D_MAX;
2131 }
2132
2133 ret = read ( fd, det_3d, 10 );
2134 if ( ret < 0 ) {
2135 LOGW ( "/sys/module/di/parameters/det3d_mode error(%s)!!\n", strerror ( errno ) );
2136 }
2137
2138 det_3dmode = atoi ( det_3d );
2139 close ( fd );
2140 fd = -1;
2141
2142 return (tvin_trans_fmt)det_3dmode;
2143}
2144
2145int CTvin::TvinApi_SetCompPhaseEnable ( int enable )
2146{
2147 int ret = -1;
2148 if ( enable == 1 ) {
2149 ret = SetFileAttrValue ( "/sys/module/tvin_afe/parameters/enable_dphase", "Y" );
2150 LOGD ( "%s, enable TvinApi_SetCompPhase.", CFG_SECTION_TV );
2151 }
2152
2153 return ret;
2154}
2155
2156int CTvin::VDIN_GetPortConnect ( int port )
2157{
2158 int status = 0;
2159
2160 if ( VDIN_DeviceIOCtl ( TVIN_IOC_CALLMASTER_SET, &port ) < 0 ) {
2161 LOGW ( "TVIN_IOC_CALLMASTER_SET error(%s) port %d\n", strerror ( errno ), port );
2162 return 0;
2163 }
2164
2165 if ( VDIN_DeviceIOCtl ( TVIN_IOC_CALLMASTER_GET, &status ) < 0 ) {
2166 LOGW ( "TVIN_IOC_CALLMASTER_GET error(%s)\n", strerror ( errno ) );
2167 return 0;
2168 }
2169
2170 //LOGD("%s, port:%x,status:%d", CFG_SECTION_TV,port,status);
2171 return status;
2172}
2173
2174int CTvin::VDIN_OpenHDMIPinMuxOn ( bool flag )
2175{
2176 FILE *fp = NULL;
2177
2178 fp = fopen ( "/sys/class/hdmirx/hdmirx0/debug", "w" );
2179 if ( fp == NULL ) {
2180 LOGW ( "Open /sys/class/hdmirx/hdmirx0/debug(%s)!\n", strerror ( errno ) );
2181 return -1;
2182 }
2183
2184 if ( flag ) {
2185 fprintf ( fp, "%s", "pinmux_on" );
2186 } else {
2187 fprintf ( fp, "%s", "pinmux_off" );
2188
2189 }
2190
2191 fclose ( fp );
2192 fp = NULL;
2193 return 1;
2194}
2195
2196int CTvin::VDIN_GetHdmiHdcpKeyKsvInfo(struct _hdcp_ksv *msg)
2197{
2198 int m_ksv_dev_fd = -1;
2199
2200 if (msg == NULL) {
2201 LOGE("%s, msg is NULL\n", __FUNCTION__);
2202 }
2203
2204 //PrintMessage(__FUNCTION__, 0, msg);
2205
2206 m_ksv_dev_fd = open(HDMIRX_KSV_PATH, O_RDWR);
2207 if (m_ksv_dev_fd < 0) {
2208 LOGE("%s, Open file %s error: (%s)!\n", __FUNCTION__, HDMIRX_KSV_PATH, strerror ( errno ));
2209 return -1;
2210 }
2211 LOGD("# call ioctl with HDMI_IOC_HDCP_SENT_KSV #");
2212 ioctl(m_ksv_dev_fd, HDMI_IOC_HDCP_KSV, msg);
2213
2214 close(m_ksv_dev_fd);
2215 m_ksv_dev_fd = -1;
2216
2217 LOGD("msg->bksv0 is %x, msg->bksv1 is %x", msg->bksv0, msg->bksv1);
2218
2219 return 0;
2220}
2221
2222
2223int CTvin::get_hdmi_ksv_info(int source_input, int data_buf[])
2224{
2225 if (source_input != SOURCE_HDMI1 && source_input != SOURCE_HDMI2 && source_input != SOURCE_HDMI3) {
2226 return -1;
2227 }
2228
2229 struct _hdcp_ksv msg;
2230 int ret = -1;
2231 ret = VDIN_GetHdmiHdcpKeyKsvInfo(&msg);
2232 memset((void *)data_buf, 0, 2);
2233 data_buf[0] = msg.bksv0;
2234 data_buf[1] = msg.bksv1;
2235 return ret;
2236}
2237
2238
2239int CTvin::TVAFE_EnablePlugInDetect ( bool flag )
2240{
2241 FILE *fp = NULL;
2242 int status = 1;
2243
2244 fp = fopen ( "/sys/class/tvafe/tvafe0/debug", "w" );
2245 if ( fp == NULL ) {
2246 LOGW ( "Open /sys/class/tvafe/tvafe0/debug (%s)!\n", strerror ( errno ) );
2247 return -1;
2248 }
2249
2250 if ( flag ) {
2251 fprintf ( fp, "%s", "tvafe_enable" );
2252 } else {
2253 fprintf ( fp, "%s", "tvafe_down" );
2254
2255 }
2256
2257 fclose ( fp );
2258 fp = NULL;
2259 return status;
2260}
2261
2262int CTvin::TvinApi_GetHDMIAudioStatus ( void )
2263{
2264 int fd;
2265 int val = 0;
2266 char bcmd[16];
2267 fd = open ( "/sys/module/tvin_hdmirx/parameters/auds_rcv_sts", O_RDONLY );
2268
2269 if ( fd >= 0 ) {
2270 read ( fd, bcmd, sizeof ( bcmd ) );
2271 val = strtol ( bcmd, NULL, 10 );
2272 close ( fd );
2273 } else {
2274 LOGE ( "open /sys/module/tvin_hdmirx/parameters/auds_rcv_sts ERROR(%s)!!\n", strerror ( errno ) );
2275 return -1;
2276 }
2277
2278 return val;
2279}
2280
2281int CTvin::TvinApi_LoadPLLValues ( am_regs_t regs )
2282{
2283 int rt = AFE_DeviceIOCtl ( TVIN_IOC_LOAD_REG, &regs );
2284
2285 if ( rt < 0 ) {
2286 LOGE ( "TvinApi_LoadPLLValues, error(%s)!\n", strerror ( errno ) );
2287 }
2288
2289 return rt;
2290}
2291
2292int CTvin::TvinApi_LoadCVD2Values ( am_regs_t regs )
2293{
2294 int rt = AFE_DeviceIOCtl ( TVIN_IOC_LOAD_REG, &regs );
2295
2296 if ( rt < 0 ) {
2297 LOGE ( "TvinApi_LoadCVD2Values, error(%s)!\n", strerror ( errno ) );
2298 }
2299
2300 return rt;
2301}
2302
2303int CTvin::TvinApi_GetFbSize ( unsigned int *fb_width, unsigned int *fb_height )
2304{
2305 int fbfd = 0;
2306 struct fb_var_screeninfo vinfo;
2307 struct fb_fix_screeninfo finfo;
2308 int xres = 0, yres = 0, bits_per_pixel = 0;
2309
2310 fbfd = open ( "/dev/graphics/fb0", O_RDWR );
2311 if ( !fbfd ) {
2312 return -1;
2313 }
2314
2315 if ( ioctl ( fbfd, FBIOGET_FSCREENINFO, &finfo ) ) {
2316 goto fail_close_fb;
2317 }
2318
2319 if ( ioctl ( fbfd, FBIOGET_VSCREENINFO, &vinfo ) ) {
2320 goto fail_close_fb;
2321 }
2322
2323 *fb_width = vinfo.xres;
2324 *fb_height = vinfo.yres;
2325 return 1;
2326
2327fail_close_fb:
2328 close ( fbfd );;
2329 return -1;
2330}
2331
2332tv_source_input_type_t CTvin::Tvin_SourceInputToSourceInputType ( tv_source_input_t source_input )
2333{
2334 if (source_input == SOURCE_TV) {
2335 return SOURCE_TYPE_TV;
2336 } else if (source_input == SOURCE_AV1 || source_input == SOURCE_AV2) {
2337 return SOURCE_TYPE_AV;
2338 } else if (source_input == SOURCE_YPBPR1 || source_input == SOURCE_YPBPR2) {
2339 return SOURCE_TYPE_COMPONENT;
2340 } else if (source_input == SOURCE_VGA) {
2341 return SOURCE_TYPE_VGA;
2342 } else if (source_input == SOURCE_HDMI1 || source_input == SOURCE_HDMI2 || source_input == SOURCE_HDMI3) {
2343 return SOURCE_TYPE_HDMI;
2344 } else if (source_input == SOURCE_DTV) {
2345 return SOURCE_TYPE_DTV;
2346 } else if (source_input == SOURCE_IPTV) {
2347 return SOURCE_TYPE_IPTV;
2348 } else if (source_input == SOURCE_MPEG) {
2349 return SOURCE_TYPE_MPEG;
2350 }
2351
2352 return SOURCE_TYPE_MPEG;
2353}
2354
2355tv_source_input_type_t CTvin::Tvin_SourcePortToSourceInputType ( tvin_port_t source_port )
2356{
2357 tv_source_input_t source_input = Tvin_PortToSourceInput(source_port);
2358 return Tvin_SourceInputToSourceInputType(source_input);
2359}
2360
2361tvin_port_t CTvin::Tvin_GetSourcePortBySourceType ( tv_source_input_type_t source_type )
2362{
2363 tvin_port_t source_port;
2364
2365 switch ( source_type ) {
2366 case SOURCE_TYPE_TV:
2367 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_TV);
2368 break;
2369 case SOURCE_TYPE_AV:
2370 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_AV1);
2371 break;
2372 case SOURCE_TYPE_COMPONENT:
2373 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_YPBPR1);
2374 break;
2375 case SOURCE_TYPE_VGA:
2376 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_VGA);
2377 break;
2378 case SOURCE_TYPE_HDMI:
2379 source_port = TVIN_PORT_HDMI0;
2380 break;
2381 case SOURCE_TYPE_IPTV:
2382 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_IPTV);
2383 break;
2384 case SOURCE_TYPE_DTV:
2385 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_DTV);
2386 break;
2387 case SOURCE_TYPE_MPEG:
2388 default:
2389 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_MPEG);
2390 break;
2391 }
2392
2393 return source_port;
2394}
2395
2396tvin_port_t CTvin::Tvin_GetSourcePortBySourceInput ( tv_source_input_t source_input )
2397{
2398 tvin_port_t source_port = TVIN_PORT_NULL;
2399
2400 if ( source_input < SOURCE_TV || source_input >= SOURCE_MAX ) {
2401 source_port = TVIN_PORT_NULL;
2402 } else {
2403 source_port = ( tvin_port_t ) mSourceInputToPortMap[ ( int ) source_input];
2404 }
2405
2406 return source_port;
2407}
2408
2409tv_source_input_t CTvin::Tvin_PortToSourceInput ( tvin_port_t port )
2410{
2411 int i;
2412
2413 for ( i = SOURCE_TV; i < SOURCE_MAX; i++ ) {
2414 if ( mSourceInputToPortMap[i] == (int)port ) {
2415 break;
2416 }
2417 }
2418
2419 if ( i == SOURCE_MAX ) {
2420 return SOURCE_MAX;
2421 } else {
2422 return tv_source_input_t ( i );
2423 }
2424}
2425
2426unsigned int CTvin::Tvin_TransPortStringToValue(const char *port_str)
2427{
2428 if (strcasecmp(port_str, "TVIN_PORT_CVBS0") == 0) {
2429 return TVIN_PORT_CVBS0;
2430 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS1") == 0) {
2431 return TVIN_PORT_CVBS1;
2432 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS2") == 0) {
2433 return TVIN_PORT_CVBS2;
2434 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS3") == 0) {
2435 return TVIN_PORT_CVBS3;
2436 } else if (strcasecmp(port_str, "TVIN_PORT_COMP0") == 0) {
2437 return TVIN_PORT_COMP0;
2438 } else if (strcasecmp(port_str, "TVIN_PORT_COMP1") == 0) {
2439 return TVIN_PORT_COMP1;
2440 } else if (strcasecmp(port_str, "TVIN_PORT_VGA0") == 0) {
2441 return TVIN_PORT_VGA0;
2442 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI0") == 0) {
2443 return TVIN_PORT_HDMI0;
2444 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI1") == 0) {
2445 return TVIN_PORT_HDMI1;
2446 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI2") == 0) {
2447 return TVIN_PORT_HDMI2;
2448 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI3") == 0) {
2449 return TVIN_PORT_HDMI3;
2450 }
2451
2452 return TVIN_PORT_MPEG0;
2453}
2454
2455void CTvin::Tvin_LoadSourceInputToPortMap()
2456{
2457 const char *config_value = NULL;
2458
2459 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.atv", "TVIN_PORT_CVBS3");
2460 mSourceInputToPortMap[SOURCE_TV] = Tvin_TransPortStringToValue(config_value);
2461
2462 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.av1", "TVIN_PORT_CVBS1");
2463 mSourceInputToPortMap[SOURCE_AV1] = Tvin_TransPortStringToValue(config_value);
2464
2465 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.av2", "TVIN_PORT_CVBS2");
2466 mSourceInputToPortMap[SOURCE_AV2] = Tvin_TransPortStringToValue(config_value);
2467
2468 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.ypbpr1", "TVIN_PORT_COMP0");
2469 mSourceInputToPortMap[SOURCE_YPBPR1] = Tvin_TransPortStringToValue(config_value);
2470
2471 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.ypbpr2", "TVIN_PORT_COMP1");
2472 mSourceInputToPortMap[SOURCE_YPBPR2] = Tvin_TransPortStringToValue(config_value);
2473
2474 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi1", "TVIN_PORT_HDMI0");
2475 mSourceInputToPortMap[SOURCE_HDMI1] = Tvin_TransPortStringToValue(config_value);
2476
2477 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi2", "TVIN_PORT_HDMI2");
2478 mSourceInputToPortMap[SOURCE_HDMI2] = Tvin_TransPortStringToValue(config_value);
2479
2480 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi3", "TVIN_PORT_HDMI1");
2481 mSourceInputToPortMap[SOURCE_HDMI3] = Tvin_TransPortStringToValue(config_value);
2482
2483 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.vga", "TVIN_PORT_VGA0");
2484 mSourceInputToPortMap[SOURCE_VGA] = Tvin_TransPortStringToValue(config_value);
2485
2486 mSourceInputToPortMap[SOURCE_MPEG] = TVIN_PORT_MPEG0;
2487 mSourceInputToPortMap[SOURCE_DTV] = TVIN_PORT_DTV;
2488 mSourceInputToPortMap[SOURCE_IPTV] = TVIN_PORT_BT656;
2489}
2490
2491int CTvin::Tvin_GetSourcePortByCECPhysicalAddress(int physical_addr)
2492{
2493 if (physical_addr == 0x1000) {
2494 return TVIN_PORT_HDMI0;
2495 } else if (physical_addr == 0x2000) {
2496 return TVIN_PORT_HDMI1;
2497 } else if (physical_addr == 0x3000) {
2498 return TVIN_PORT_HDMI2;
2499 }
2500
2501 return TVIN_PORT_MAX;
2502}
2503
2504tv_audio_channel_t CTvin::Tvin_GetInputSourceAudioChannelIndex ( tv_source_input_t source_input )
2505{
2506 int aud_chan = TV_AUDIO_LINE_IN_0;
2507 const char *config_value = NULL;
2508
2509 if ( source_input == SOURCE_TV ) {
2510 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.atv", "2");
2511 } else if ( source_input == SOURCE_AV1 ) {
2512 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.av1", "1");
2513 } else if ( source_input == SOURCE_AV2 ) {
2514 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.av2", "3");
2515 } else if ( source_input == SOURCE_YPBPR1 ) {
2516 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.comp1", "0");
2517 } else if ( source_input == SOURCE_YPBPR2 ) {
2518 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.comp2", "0");
2519 } else if ( source_input == SOURCE_HDMI1 ) {
2520 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi1", "0");
2521 } else if ( source_input == SOURCE_HDMI2 ) {
2522 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi2", "0");
2523 } else if ( source_input == SOURCE_HDMI3 ) {
2524 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi3", "0");
2525 } else if ( source_input == SOURCE_VGA ) {
2526 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.vga", "0");
2527 } else if ( source_input == SOURCE_MPEG ) {
2528 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.mpeg", "0");
2529 }
2530
2531 if (config_value != NULL) {
2532 aud_chan = strtol (config_value, NULL, 10);
2533 }
2534
2535 return ( tv_audio_channel_t ) aud_chan;
2536}
2537
2538tv_audio_in_source_type_t CTvin::Tvin_GetAudioInSourceType ( tv_source_input_t source_input )
2539{
2540 const char *config_value = NULL;
2541
2542 if (source_input == SOURCE_TV) {
2543 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.insource.atv", "TV_AUDIO_IN_SOURCE_TYPE_LINEIN");
2544 if (strcasecmp(config_value, "TV_AUDIO_IN_SOURCE_TYPE_ATV") == 0) {
2545 return TV_AUDIO_IN_SOURCE_TYPE_ATV;
2546 }
2547 } else if (source_input == SOURCE_AV1 || source_input == SOURCE_AV2) {
2548 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2549 } else if (source_input == SOURCE_YPBPR1 || source_input == SOURCE_YPBPR2 || source_input == SOURCE_VGA) {
2550 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2551 } else if (source_input == SOURCE_HDMI1 || source_input == SOURCE_HDMI2 || source_input == SOURCE_HDMI3) {
2552 return TV_AUDIO_IN_SOURCE_TYPE_HDMI;
2553 }
2554
2555 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2556}
2557
2558int CTvin::isVgaFmtInHdmi ( tvin_sig_fmt_t fmt )
2559{
2560 if ( fmt == TVIN_SIG_FMT_HDMI_640X480P_60HZ
2561 || fmt == TVIN_SIG_FMT_HDMI_800X600_00HZ
2562 || fmt == TVIN_SIG_FMT_HDMI_1024X768_00HZ
2563 || fmt == TVIN_SIG_FMT_HDMI_720X400_00HZ
2564 || fmt == TVIN_SIG_FMT_HDMI_1280X768_00HZ
2565 || fmt == TVIN_SIG_FMT_HDMI_1280X800_00HZ
2566 || fmt == TVIN_SIG_FMT_HDMI_1280X960_00HZ
2567 || fmt == TVIN_SIG_FMT_HDMI_1280X1024_00HZ
2568 || fmt == TVIN_SIG_FMT_HDMI_1360X768_00HZ
2569 || fmt == TVIN_SIG_FMT_HDMI_1366X768_00HZ
2570 || fmt == TVIN_SIG_FMT_HDMI_1600X1200_00HZ
2571 || fmt == TVIN_SIG_FMT_HDMI_1920X1200_00HZ ) {
2572 LOGD ( "%s, HDMI source : VGA format.", CFG_SECTION_TV );
2573 return 1;
2574 }
2575
2576 return -1;
2577}
2578
2579int CTvin::isSDFmtInHdmi ( tvin_sig_fmt_t fmt )
2580{
2581 if ( fmt == TVIN_SIG_FMT_HDMI_640X480P_60HZ
2582 || fmt == TVIN_SIG_FMT_HDMI_720X480P_60HZ
2583 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_60HZ
2584 || fmt == TVIN_SIG_FMT_HDMI_1440X240P_60HZ
2585 || fmt == TVIN_SIG_FMT_HDMI_2880X480I_60HZ
2586 || fmt == TVIN_SIG_FMT_HDMI_2880X240P_60HZ
2587 || fmt == TVIN_SIG_FMT_HDMI_1440X480P_60HZ
2588 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ
2589 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2590 || fmt == TVIN_SIG_FMT_HDMI_1440X288P_50HZ
2591 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2592 || fmt == TVIN_SIG_FMT_HDMI_2880X288P_50HZ
2593 || fmt == TVIN_SIG_FMT_HDMI_1440X576P_50HZ
2594 || fmt == TVIN_SIG_FMT_HDMI_2880X480P_60HZ
2595 || fmt == TVIN_SIG_FMT_HDMI_2880X576P_60HZ
2596 || fmt == TVIN_SIG_FMT_HDMI_720X576P_100HZ
2597 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_100HZ
2598 || fmt == TVIN_SIG_FMT_HDMI_720X480P_120HZ
2599 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_120HZ
2600 || fmt == TVIN_SIG_FMT_HDMI_720X576P_200HZ
2601 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_200HZ
2602 || fmt == TVIN_SIG_FMT_HDMI_720X480P_240HZ
2603 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_240HZ
2604 || fmt == TVIN_SIG_FMT_HDMI_800X600_00HZ
2605 || fmt == TVIN_SIG_FMT_HDMI_720X400_00HZ ) {
2606 LOGD ( "%s, SD format.", CFG_SECTION_TV );
2607 return true;
2608 } else {
2609 LOGD ( "%s, HD format.", CFG_SECTION_TV );
2610 return false;
2611 }
2612}
2613
2614bool CTvin::Tvin_is50HzFrameRateFmt ( tvin_sig_fmt_t fmt )
2615{
2616 /** component **/
2617 if ( fmt == TVIN_SIG_FMT_COMP_576P_50HZ_D000
2618 || fmt == TVIN_SIG_FMT_COMP_576I_50HZ_D000
2619 || fmt == TVIN_SIG_FMT_COMP_720P_50HZ_D000
2620 || fmt == TVIN_SIG_FMT_COMP_1080P_50HZ_D000
2621 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_A
2622 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_B
2623 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_C
2624 || fmt == TVIN_SIG_FMT_COMP_1080P_24HZ_D000
2625 || fmt == TVIN_SIG_FMT_COMP_1080P_25HZ_D000
2626 /** hdmi **/
2627 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ
2628 || fmt == TVIN_SIG_FMT_HDMI_1280X720P_50HZ
2629 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_A
2630 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2631 || fmt == TVIN_SIG_FMT_HDMI_1440X288P_50HZ
2632 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2633 || fmt == TVIN_SIG_FMT_HDMI_2880X288P_50HZ
2634 || fmt == TVIN_SIG_FMT_HDMI_1440X576P_50HZ
2635 || fmt == TVIN_SIG_FMT_HDMI_1920X1080P_50HZ
2636 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_B
2637 || fmt == TVIN_SIG_FMT_HDMI_1280X720P_50HZ_FRAME_PACKING
2638 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_FRAME_PACKING
2639 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ_FRAME_PACKING
2640 /** cvbs **/
2641 || fmt == TVIN_SIG_FMT_CVBS_PAL_I
2642 || fmt == TVIN_SIG_FMT_CVBS_PAL_M
2643 || fmt == TVIN_SIG_FMT_CVBS_PAL_CN
2644 || fmt == TVIN_SIG_FMT_CVBS_SECAM ) {
2645 LOGD ( "%s, Frame rate == 50Hz.", CFG_SECTION_TV );
2646 return true;
2647 } else {
2648 LOGD ( "%s, Frame rate != 50Hz.", CFG_SECTION_TV );
2649 return false;
2650 }
2651}
2652
2653bool CTvin::Tvin_IsDeinterlaceFmt ( tvin_sig_fmt_t fmt )
2654{
2655 if ( fmt == TVIN_SIG_FMT_COMP_480I_59HZ_D940
2656 || fmt == TVIN_SIG_FMT_COMP_576I_50HZ_D000
2657 || fmt == TVIN_SIG_FMT_COMP_1080I_47HZ_D952
2658 || fmt == TVIN_SIG_FMT_COMP_1080I_48HZ_D000
2659 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_A
2660 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_B
2661 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_C
2662 || fmt == TVIN_SIG_FMT_COMP_1080I_60HZ_D000
2663 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_120HZ
2664 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_240HZ
2665 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_60HZ
2666 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_100HZ
2667 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_200HZ
2668 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2669 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_100HZ
2670 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_120HZ
2671 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_A
2672 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_B
2673 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_FRAME_PACKING
2674 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_60HZ
2675 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_60HZ_FRAME_PACKING
2676 || fmt == TVIN_SIG_FMT_HDMI_2880X480I_60HZ
2677 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2678 || fmt == TVIN_SIG_FMT_CVBS_NTSC_M
2679 || fmt == TVIN_SIG_FMT_CVBS_NTSC_443
2680 || fmt == TVIN_SIG_FMT_CVBS_PAL_60
2681 || fmt == TVIN_SIG_FMT_CVBS_PAL_CN
2682 || fmt == TVIN_SIG_FMT_CVBS_PAL_I
2683 || fmt == TVIN_SIG_FMT_CVBS_PAL_M
2684 || fmt == TVIN_SIG_FMT_CVBS_SECAM ) {
2685 LOGD ( "%s, Interlace format.", CFG_SECTION_TV );
2686 return true;
2687 } else {
2688 LOGD ( "%s, Progressive format.", CFG_SECTION_TV );
2689 return false;
2690 }
2691}
2692
2693int CTvin::Tvin_StartDecoder ( tvin_info_t &info )
2694{
2695 if ( m_is_decoder_start == false ) {
2696 m_tvin_param.info = info;
2697
2698 if ( VDIN_StartDec ( &m_tvin_param ) >= 0 ) {
2699 LOGD ( "StartDecoder succeed." );
2700 m_is_decoder_start = true;
2701 } else {
2702 LOGW ( "StartDecoder failed." );
2703 return -1;
2704 }
2705 } else {
2706 return -2;
2707 }
2708
2709 return 0;
2710}
2711
2712int CTvin::SwitchPort (tvin_port_t source_port )
2713{
2714 int ret = 0;
2715 LOGD ("%s, source_port = %x", __FUNCTION__, source_port);
2716 ret = Tvin_StopDecoder();
2717 if ( 0 == ret || 1 == ret ) {
2718 if ( 1 == ret ) { //decode not started
2719 //mVpp.Tvin_SetVideoScreenColorType ( TV_SIGNAL_BLUE_PATTERN );
2720 }
2721 VDIN_ClosePort();
2722 } else {
2723 LOGW ( "%s,stop decoder failed.", __FUNCTION__);
2724 return -1;
2725 }
2726 // Open Port
2727 if ( VDIN_OpenPort ( source_port ) < 0 ) {
2728 LOGD ( "%s, OpenPort failed, source_port =%x ", __FUNCTION__, source_port );
2729 }
2730 m_tvin_param.port = source_port;
2731 return 0;
2732}
2733
2734int CTvin::Tvin_StopDecoder()
2735{
2736 if ( m_is_decoder_start == true ) {
2737 if ( VDIN_StopDec() >= 0 ) {
2738 LOGD ( "StopDecoder ok!" );
2739 m_is_decoder_start = false;
2740 return 0;
2741 } else {
2742 LOGD ( "StopDecoder Failed!" );
2743 }
2744 } else {
2745 return 1;
2746 }
2747
2748 return -1;
2749}
2750
2751int CTvin::init_vdin ( void )
2752{
2753 VDIN_OpenModule();
2754 return 0;
2755}
2756
2757int CTvin::uninit_vdin ( void )
2758{
2759 VDIN_ClosePort();
2760 VDIN_CloseModule();
2761 return 0;
2762}
2763
2764int CTvin::Tvin_AddPath ( tvin_path_id_t pathid )
2765{
2766 int ret = -1;
2767 int i = 0, dly = 10;
2768 tv_path_type_t pathtype;
2769
2770 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2771 pathtype = TV_PATH_TYPE_TVIN;
2772 } else {
2773 pathtype = TV_PATH_TYPE_DEFAULT;
2774 }
2775
2776 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2777 if ( m_pathid == pathid ) {
2778 LOGW ( "%s, no need to add the same tvin path.\n", CFG_SECTION_TV );
2779 return 0;
2780 }
2781
2782 for ( i = 0; i < 50; i++ ) {
2783 ret = VDIN_RmTvPath();
2784
2785 if ( ret > 0 ) {
2786 LOGD ( "%s, remove tvin path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2787 break;
2788 } else {
2789 LOGW ( "%s, remove tvin path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2790 usleep ( dly * 1000 );
2791 }
2792 }
2793 } else {
2794 for ( i = 0; i < 50; i++ ) {
2795 ret = VDIN_RmDefPath();
2796
2797 if ( ret > 0 ) {
2798 LOGD ( "%s, remove default path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2799 break;
2800 } else {
2801 LOGW ( "%s, remove default path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2802 usleep ( dly * 1000 );
2803 }
2804 }
2805 }
2806
2807 for ( i = 0; i < 50; i++ ) {
2808 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2809 if ( strcmp ( config_tv_path, "null" ) == 0 ) {
2810 ret = VDIN_AddVideoPath ( pathid );
2811 } else {
2812 ret = VDIN_AddPath ( config_tv_path );
2813 }
2814 } else {
2815 if ( strcmp ( config_default_path, "null" ) == 0 ) {
2816 ret = VDIN_AddVideoPath ( pathid );
2817 } else {
2818 ret = VDIN_AddPath ( config_default_path );
2819 }
2820 }
2821
2822 if ( ret >= 0 ) {
2823 LOGD ( "%s, add pathid[%d] ok, %d ms gone.\n", CFG_SECTION_TV, pathid, i );
2824 break;
2825 } else {
2826 LOGW ( "%s, add pathid[%d] faild, %d ms gone.\n", CFG_SECTION_TV, pathid, i );
2827 usleep ( dly * 1000 );
2828 }
2829 }
2830
2831 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_MAX ) {
2832 m_pathid = pathid;
2833 }
2834
2835 return ret;
2836}
2837
2838
2839int CTvin::Tvin_RemovePath ( tv_path_type_t pathtype )
2840{
2841 int ret = -1;
2842 int i = 0, dly = 10;
2843
2844 for ( i = 0; i < 500; i++ ) {
2845 ret = Tvin_CheckPathActive ( pathtype, 0 );
2846
2847 if ( ret == TV_PATH_STATUS_INACTIVE ) {
2848 LOGD ( "%s, check path is inactive, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2849 break;
2850 } else if ( ret == TV_PATH_STATUS_INACTIVE || ret == TV_PATH_STATUS_ERROR ) {
2851 usleep ( dly * 1000 );
2852 } else {
2853 break;
2854 }
2855 }
2856
2857 if ( i == 500 ) {
2858 LOGE ( "%s, check path active faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2859 }
2860
2861 if ( pathtype == TV_PATH_TYPE_DEFAULT ) {
2862 for ( i = 0; i < 50; i++ ) {
2863 ret = VDIN_RmDefPath();
2864
2865 if ( ret > 0 ) {
2866 LOGD ( "%s, remove default path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2867 break;
2868 } else {
2869 LOGW ( "%s, remove default path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2870 usleep ( dly * 1000 );
2871 }
2872 }
2873 } else if ( pathtype == TV_PATH_TYPE_TVIN ) {
2874 for ( i = 0; i < 50; i++ ) {
2875 ret = VDIN_RmTvPath();
2876
2877 if ( ret > 0 ) {
2878 LOGD ( "%s, remove tvin path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2879 break;
2880 } else {
2881 LOGW ( "%s, remove tvin path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2882 usleep ( dly * 1000 );
2883 }
2884 }
2885
2886 m_pathid = TV_PATH_DECODER_3D_AMVIDEO;
2887 } else if ( pathtype == TV_PATH_TYPE_TVIN_PREVIEW ) {
2888 for ( i = 0; i < 50; i++ ) {
2889 ret = VDIN_RmPreviewPath();
2890
2891 if ( ret > 0 ) {
2892 LOGD ( "%s, remove preview path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2893 break;
2894 } else {
2895 LOGW ( "%s, remove preview path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2896 usleep ( dly * 1000 );
2897 }
2898 }
2899
2900 m_pathid = TV_PATH_DECODER_NEW3D_WITHOUTPPMGR_AMVIDEO;
2901 } else {
2902 ret = -1;
2903 }
2904
2905 return ret;
2906}
2907
2908
2909int CTvin::Tvin_CheckPathActive ( tv_path_type_t path_type, int isCheckD2D3 )
2910{
2911 FILE *f;
2912 char path[256];
2913 char decoder_str[20] = "default {";
2914 char tvin_str[20] = "tvpath {";
2915 char decoder_active_str[20] = "decoder(1)";
2916 char tvin_active_str[20] = "vdin0(1)";
2917 char di_active_str[20] = "deinterlace(1)";
2918 char d2d3_active_str[20] = "d2d3(1)";
2919 char decoder_inactive_str[20] = "decoder(0)";
2920 char tvin_inactive_str[20] = "vdin0(0)";
2921 char di_inactive_str[20] = "deinterlace(0)";
2922 char d2d3_inactive_str[20] = "d2d3(0)";
2923
2924 char *str_find = NULL;
2925 char *active_str = NULL;
2926 char *inactive_str = NULL;
2927 int mount_freq;
2928 int match;
2929 int is_active = TV_PATH_STATUS_INACTIVE;
2930
2931 memset ( path, 0, 255 );
2932
2933 f = fopen ( "/sys/class/vfm/map", "r" );
2934 if ( !f ) {
2935 LOGE ( "%s, can not open /sys/class/vfm/map!\n", CFG_SECTION_TV );
2936 return TV_PATH_STATUS_NO_DEV;
2937 }
2938
2939 while ( fgets ( path, 254, f ) ) {
2940 if ( path_type == TV_PATH_TYPE_DEFAULT ) {
2941 str_find = strstr ( path, decoder_str );
2942 active_str = decoder_active_str;
2943 inactive_str = decoder_inactive_str;
2944 } else if ( path_type == TV_PATH_TYPE_TVIN || path_type == TV_PATH_TYPE_TVIN_PREVIEW ) {
2945 str_find = strstr ( path, tvin_str );
2946 active_str = tvin_active_str;
2947 inactive_str = tvin_inactive_str;
2948 } else {
2949 LOGW ( "%s, there is no %d path_type.\n", CFG_SECTION_TV, path_type );
2950 break;
2951 }
2952
2953 if ( str_find ) {
2954 if ( isCheckD2D3 == 0 ) {
2955 if ( strstr ( str_find, active_str ) && strstr ( str_find, di_active_str ) ) {
2956 is_active = TV_PATH_STATUS_ACTIVE;
2957 //LOGD("%s, %s is active.\n", CFG_SECTION_TV, path);
2958 } else if ( strstr ( str_find, inactive_str )
2959 && ( strstr ( str_find, di_inactive_str ) || ( !strstr ( str_find, di_inactive_str ) ) )
2960 ) {
2961 is_active = TV_PATH_STATUS_INACTIVE;
2962 //LOGD("%s, %s is inactive.\n", CFG_SECTION_TV, path);
2963 } else {
2964 is_active = TV_PATH_STATUS_ERROR;
2965 LOGE ( "%s, %s is error!\n", CFG_SECTION_TV, path );
2966 }
2967
2968 break;
2969 } else {
2970 if ( strstr ( str_find, active_str ) && strstr ( str_find, di_active_str ) && strstr ( str_find, d2d3_active_str ) ) {
2971 is_active = TV_PATH_STATUS_ACTIVE;
2972 //LOGD("%s, %s is active.\n", CFG_SECTION_TV, path);
2973 } else if ( strstr ( str_find, inactive_str )
2974 && ( strstr ( str_find, di_inactive_str ) || ( !strstr ( str_find, di_inactive_str ) ) )
2975 && ( strstr ( str_find, d2d3_inactive_str ) || ( !strstr ( str_find, d2d3_inactive_str ) ) )
2976 ) {
2977 is_active = TV_PATH_STATUS_INACTIVE;
2978 //LOGD("%s, %s is inactive.\n", CFG_SECTION_TV, path);
2979 } else {
2980 is_active = TV_PATH_STATUS_ERROR;
2981 LOGE ( "%s, %s is error!\n", CFG_SECTION_TV, path );
2982 }
2983
2984 break;
2985 }
2986 }
2987 }
2988
2989 fclose ( f );
2990
2991 return is_active;
2992}
2993
2994
2995int CTvin::Tv_init_afe ( void )
2996{
2997 AFE_OpenModule();
2998 return 0;
2999}
3000
3001int CTvin::Tv_uninit_afe ( void )
3002{
3003 AFE_CloseModule();
3004 return 0;
3005}
3006
3007int CTvin::get_hdmi_sampling_rate()
3008{
3009 int fd;
3010 int val = 0;
3011 char bcmd[16];
3012 fd = open ( "/sys/module/tvin_hdmirx/parameters/audio_sample_rate", O_RDONLY );
3013 if ( fd >= 0 ) {
3014 read ( fd, bcmd, sizeof ( bcmd ) );
3015 val = strtol ( bcmd, NULL, 10 );
3016 close ( fd );
3017 }
3018
3019 return val;
3020}
3021
3022//**************************************************************************
3023CTvin::CTvinSigDetect::CTvinSigDetect ()
3024{
3025 mDetectState = STATE_STOPED;
3026 mpObserver = NULL;
3027 initSigState();
3028}
3029
3030CTvin::CTvinSigDetect::~CTvinSigDetect()
3031{
3032}
3033
3034int CTvin::CTvinSigDetect::startDetect(bool bPause)
3035{
3036 LOGD ( "startDetect()" );
3037
3038 if ( mDetectState == STATE_RUNNING || mDetectState == STATE_PAUSE ) {
3039 return mDetectState;
3040 }
3041
3042 m_request_pause_detect = bPause;
3043 initSigState();
3044 this->run();
3045 return mDetectState;
3046}
3047
3048int CTvin::CTvinSigDetect::initSigState()
3049{
3050 m_cur_sig_info.trans_fmt = TVIN_TFMT_2D;
3051 m_cur_sig_info.fmt = TVIN_SIG_FMT_NULL;
3052 m_cur_sig_info.status = TVIN_SIG_STATUS_NULL;
3053 m_cur_sig_info.reserved = 0;
3054 m_pre_sig_info = m_cur_sig_info;
3055 mKeepNosigTime = 0;
3056 m_is_nosig_checktimes_once_valid = false;
3057 mResumeLaterTime = 0;
3058 return 0;
3059}
3060
3061int CTvin::CTvinSigDetect::stopDetect()
3062{
3063 CMutex::Autolock _l ( mLock );
3064 LOGD ( "stopDetect()" );
3065 requestExit();
3066 return 0;
3067}
3068
3069int CTvin::CTvinSigDetect::pauseDetect()
3070{
3071 CMutex::Autolock _l ( mLock );
3072 LOGD ( "pauseDetect()" );
3073 m_request_pause_detect = true;
3074 return 0;
3075}
3076
3077int CTvin::CTvinSigDetect::requestAndWaitPauseDetect()
3078{
3079 CMutex::Autolock _l ( mLock );
3080 LOGD ( "requestAndWaitPauseDetect()" );
3081 m_request_pause_detect = true;
3082
3083 if ( mDetectState == STATE_RUNNING ) {
3084 mRequestPauseCondition.wait ( mLock );
3085 }
3086
3087 return 0;
3088}
3089
3090int CTvin::CTvinSigDetect::resumeDetect(int later)//ms
3091{
3092 CMutex::Autolock _l ( mLock );
3093 LOGD ( "resumeDetect()" );
3094 mResumeLaterTime = later;
3095 m_request_pause_detect = false;
3096 mDetectPauseCondition.signal();
3097 return 0;
3098}
3099
3100void CTvin::CTvinSigDetect::setVdinNoSigCheckKeepTimes(int times, bool isOnce)
3101{
3102 LOGD("setVdinNoSigCheckKeepTimes mKeepNosigTime = %d, times = %d", mKeepNosigTime, times);
3103 mKeepNosigTime = times;
3104 m_is_nosig_checktimes_once_valid = isOnce;
3105}
3106
3107int CTvin::CTvinSigDetect::Tv_TvinSigDetect ( int &sleeptime )
3108{
3109 CTvin::getInstance()->VDIN_GetSignalInfo ( &m_cur_sig_info ); //get info
3110 //set no sig check times
3111 static long long sNosigKeepTime = 0;
3112 //LOGD("stime=%d status=%d, fmt = %d sNosigKeepTime = %d, mKeepNosigTime = %d", sleeptime, m_cur_sig_info.status,m_cur_sig_info.fmt, sNosigKeepTime, mKeepNosigTime);
3113 if ( m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG || m_cur_sig_info.status == TVIN_SIG_STATUS_NULL ) {
3114 sNosigKeepTime += sleeptime;
3115 if ( sNosigKeepTime > mKeepNosigTime ) { //real no sig
3116 //cur is no sig
3117 if ( m_is_nosig_checktimes_once_valid ) { //just once change,is nosig, and default it
3118 m_is_nosig_checktimes_once_valid = false;
3119 mKeepNosigTime = 0;
3120 }
3121 } else {//not
3122 m_cur_sig_info.status = m_pre_sig_info.status;
3123 }
3124 } else {
3125 sNosigKeepTime = 0;
3126 if ( m_is_nosig_checktimes_once_valid ) { //just once change,not nosig,default is
3127 m_is_nosig_checktimes_once_valid = false;
3128 mKeepNosigTime = 0;
3129 }
3130 }
3131
3132 //if state change
3133 if ( m_cur_sig_info.status != m_pre_sig_info.status ) {
3134 sleeptime = 20;
3135
3136 if ( m_cur_sig_info.status == TVIN_SIG_STATUS_STABLE ) { // to stable
3137 mpObserver->onSigToStable();
3138 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_UNSTABLE ) { //stable to unstable
3139 //mVpp.Tvin_SetVideoScreenColorType ( TV_SIGNAL_BLACK_PATTERN );
3140 mpObserver->onSigStableToUnstable();
3141 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOTSUP ) {
3142 mpObserver->onSigStableToUnSupport();
3143 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3144 mpObserver->onSigStableToNoSig();
3145 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_UNSTABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOTSUP ) {
3146 mpObserver->onSigUnStableToUnSupport();
3147 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_UNSTABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3148 mpObserver->onSigUnStableToNoSig();
3149 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_NULL && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3150 mpObserver->onSigNullToNoSig();
3151 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_NOSIG && m_cur_sig_info.status == TVIN_SIG_STATUS_UNSTABLE ) {
3152 mpObserver->onSigNoSigToUnstable();
3153 }
3154 } else { //state not change
3155 sleeptime = 20;
3156
3157 switch ( m_cur_sig_info.status ) {
3158 case TVIN_SIG_STATUS_STABLE:
3159 mpObserver->onSigStillStable();
3160 if ( m_cur_sig_info.trans_fmt != m_pre_sig_info.trans_fmt ) {
3161 mpObserver->onStableTransFmtChange();
3162 }
3163 if (m_cur_sig_info.fmt != m_pre_sig_info.fmt) {
3164 mpObserver->onStableSigFmtChange();
3165 }
3166 break;
3167
3168 case TVIN_SIG_STATUS_NOTSUP:
3169 mpObserver->onSigStillNoSupport();
3170 break;
3171
3172 case TVIN_SIG_STATUS_UNSTABLE:
3173 mpObserver->onSigStillUnstable();
3174 break;
3175
3176 case TVIN_SIG_STATUS_NOSIG:
3177 mpObserver->onSigStillNosig();
3178 break;
3179
3180 case TVIN_SIG_STATUS_NULL:
3181 default:
3182 mpObserver->onSigStillNull();
3183 break;
3184 }
3185 }
3186
3187 m_pre_sig_info = m_cur_sig_info;//backup info
3188 return sleeptime;
3189}
3190
3191bool CTvin::CTvinSigDetect::threadLoop()
3192{
3193 //enter onStart()
3194 if ( mpObserver == NULL ) {
3195 return false;
3196 }
3197
3198 int sleeptime = 200;//ms
3199 mDetectState = STATE_RUNNING;
3200 mpObserver->onSigDetectEnter();
3201
3202 while ( !exitPending() ) { //requietexit() or requietexitWait() not call
3203 while ( m_request_pause_detect ) {
3204 mLock.lock();
3205 mRequestPauseCondition.broadcast();
3206 mDetectState = STATE_PAUSE;
3207 mDetectPauseCondition.wait ( mLock ); //first unlock,when return,lock again,so need,call unlock
3208 mDetectState = STATE_RUNNING;
3209 mLock.unlock();
3210 //
3211 while (!m_request_pause_detect && mResumeLaterTime > 0) {
3212 //LOGD("mResumeLaterTime = %d", mResumeLaterTime);
3213 usleep(10 * 1000);
3214 mResumeLaterTime -= 10;
3215 }
3216 }
3217
3218 mResumeLaterTime = 0;
3219 mpObserver->onSigDetectLoop();
3220 Tv_TvinSigDetect ( sleeptime );
3221 //可以优化
3222 if ( !m_request_pause_detect ) {
3223 usleep ( sleeptime * 1000 );
3224 }
3225 }
3226
3227 mDetectState = STATE_STOPED;
3228 mRequestPauseCondition.broadcast();
3229 //exit
3230 //return true, run again, return false,not run.
3231 return false;
3232}
3233
3234v4l2_std_id CTvin::CvbsFtmToV4l2ColorStd(tvin_sig_fmt_t fmt)
3235{
3236 v4l2_std_id v4l2_std;
3237 if (fmt == TVIN_SIG_FMT_CVBS_NTSC_M || fmt == TVIN_SIG_FMT_CVBS_NTSC_443) {
3238 v4l2_std = V4L2_COLOR_STD_NTSC;
3239 } else if (fmt >= TVIN_SIG_FMT_CVBS_PAL_I && fmt <= TVIN_SIG_FMT_CVBS_PAL_CN) {
3240 v4l2_std = V4L2_COLOR_STD_PAL;
3241 } else if (fmt == TVIN_SIG_FMT_CVBS_SECAM) {
3242 v4l2_std = V4L2_COLOR_STD_SECAM;
3243 } else {
3244 v4l2_std = V4L2_COLOR_STD_PAL;
3245 }
3246 return v4l2_std;
3247}
3248
3249int CTvin::CvbsFtmToColorStdEnum(tvin_sig_fmt_t fmt)
3250{
3251 v4l2_std_id v4l2_std;
3252 if (fmt == TVIN_SIG_FMT_CVBS_NTSC_M || fmt == TVIN_SIG_FMT_CVBS_NTSC_443) {
3253 v4l2_std = CC_ATV_VIDEO_STD_NTSC;
3254 } else if (fmt >= TVIN_SIG_FMT_CVBS_PAL_I && fmt <= TVIN_SIG_FMT_CVBS_PAL_CN) {
3255 v4l2_std = CC_ATV_VIDEO_STD_PAL;
3256 } else if (fmt == TVIN_SIG_FMT_CVBS_SECAM) {
3257 v4l2_std = CC_ATV_VIDEO_STD_SECAM;
3258 } else {
3259 v4l2_std = CC_ATV_VIDEO_STD_PAL;
3260 }
3261 return v4l2_std;
3262}
3263
3264int CTvin::GetITContent()
3265{
3266 FILE *fp = NULL;
3267 int value = 0;
3268 fp = fopen("/sys/module/tvin_hdmirx/parameters/it_content", "r");
3269 if (fp == NULL) {
3270 LOGE ( "Open /sys/module/tvin_hdmirx/parameters/it_content error(%s)!\n", strerror ( errno ) );
3271 return -1;
3272 }
3273 fscanf(fp, "%d", &value );
3274 fclose(fp);
3275 fp = NULL;
3276 return value;
3277}
3278//**************************************************************************
3279