summaryrefslogtreecommitdiff
path: root/ANDROID_3.4.5/drivers/media/video/wmt_v4l2/wmt-vid.c
blob: 934f0ba9e7e5e331c091b2e7223309184b1007cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*++ 
 * linux/drivers/media/video/wmt_v4l2/wmt-vid.c
 * WonderMedia v4l video input device driver
 *
 * Copyright c 2010  WonderMedia  Technologies, Inc.
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 2 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * WonderMedia Technologies, Inc.
 * 4F, 533, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C
--*/

#include <linux/i2c.h>
#include <linux/slab.h> 
#include <linux/module.h>
#include <linux/videodev2.h>
#include "wmt-vid.h"

//#define VID_REG_TRACE
#ifdef VID_REG_TRACE
#define VID_REG_SET32(addr, val)  \
        PRINT("REG_SET:0x%x -> 0x%0x\n", addr, val);\
        REG32_VAL(addr) = (val)
#else
#define VID_REG_SET32(addr, val)      REG32_VAL(addr) = (val)
#endif

//#define VID_DEBUG    /* Flag to enable debug message */
//#define VID_DEBUG_DETAIL
//#define VID_TRACE
//#define VID_INFO

#ifdef VID_DEBUG
#define DBG_MSG(fmt, args...)      PRINT("{%s} " fmt, __FUNCTION__ , ## args)
#else
#define DBG_MSG(fmt, args...)
#endif

#ifdef VID_DEBUG_DETAIL
#define DBG_DETAIL(fmt, args...)   PRINT("{%s} " fmt, __FUNCTION__ , ## args)
#else
#define DBG_DETAIL(fmt, args...)
#endif

#ifdef VID_TRACE
  #define TRACE(fmt, args...)      PRINT("{%s}:  " fmt, __FUNCTION__ , ## args)
#else
  #define TRACE(fmt, args...) 
#endif

#ifdef VID_INFO
  #define DBG_INFO(fmt, args...)      PRINT("[INFO] {%s}:  " fmt, __FUNCTION__ , ## args)
#else
  #define DBG_INFO(fmt, args...) 
#endif

#define DBG_ERR(fmt, args...)      PRINT("*E* {%s} " fmt, __FUNCTION__ , ## args)

#define VID_INT_MODE        

static vid_fb_t *_cur_fb;
static vid_fb_t *_prev_fb;

static spinlock_t vid_lock;
static unsigned int vid_i2c_gpio_en = 0;

static swi2c_reg_t vid_i2c0_scl;
static swi2c_reg_t vid_i2c0_sda;
static swi2c_handle_t vid_swi2c;

static struct i2c_adapter *vid_i2c_adapter[2];
static struct i2c_client *vid_i2c_client[2];

static int vid_dev_tot_num;
static int vid_dev_num ;

#define WMT_VID_I2C_CHANNEL  2

static void vid_gpio_init(vid_mode mode)
{
	auto_pll_divisor(DEV_C24MOUT, CLK_ENABLE, 0, 0); 
	auto_pll_divisor(DEV_VID, CLK_ENABLE, 0, 0); 

	GPIO_CTRL_GP8_VDIN_BYTE_VAL = 0x0;
	PULL_EN_GP8_VDIN_BYTE_VAL = 0x0;

	GPIO_CTRL_GP9_VSYNC_BYTE_VAL  &= ~(BIT0|BIT1|BIT2);
	PULL_EN_GP9_VSYNC_BYTE_VAL &= ~(BIT0|BIT1|BIT2);

	GPIO_CTRL_GP17_I2C_BYTE_VAL &= ~BIT6;	//24Mhz on , not set to GPIO

	return;
}

void vid_set_ntsc_656(void)
{
	VID_REG_SET32( REG_BASE_VID+0x08, 	0x0 );	
	VID_REG_SET32( REG_BASE_VID+0x30, 0x000006b4);	// N_LN_LENGTH : 133m: d1716(6b4h) , 166m: d1715(6b3) 
}

void vid_set_pal_656(void)
{
	VID_REG_SET32( REG_BASE_VID+0x08, 0x0 );	
	VID_REG_SET32( REG_BASE_VID+0x34, 0x000006c0 );	// P_LN_LENGTH : 133m: d1728(6c0h) , 166m: d1727(6bf) 
}

void wmt_vid_set_common_mode(vid_tvsys_e tvsys)	
{
	TRACE("wmt_vid_set_common_mode() \n");

	if (tvsys == VID_NTSC)
		vid_set_ntsc_656();
	else
		vid_set_pal_656();

	VID_REG_SET32( REG_BASE_VID+0x04, 0x100     );	// TMODE[0],REG_UPDATE[8]

	if (tvsys==VID_NTSC) {
		VID_REG_SET32( REG_BASE_VID+0x14, 0x10a0004 );		// VSBB[25:16]:VSBT[9:0]; 139/1=PAL  10A/4=NTSC
		VID_REG_SET32( REG_BASE_VID+0x1c, 0x1370018 );		// PAL_TACTEND[25:16], PAL_TACTBEG[9:0]
		VID_REG_SET32( REG_BASE_VID+0x20, 0x26f0151 );		// PAL_BACTEND[25:16], PAL_BACTBEG[9:0]

		VID_REG_SET32( REG_BASE_VID+0x68, 720        );	// WIDTH[7:0];
		VID_REG_SET32( REG_BASE_VID+0x6C, 768        );	// LN_WIDTH[7:0]
		VID_REG_SET32( REG_BASE_VID+0x70, 480        );	// VID_HEIGHT[9:0]
	} else {
		VID_REG_SET32( REG_BASE_VID+0x14, 0x1390001 );		// VSBB[25:16]:VSBT[9:0]; 139/1=PAL  10A/4=NTSC
		VID_REG_SET32( REG_BASE_VID+0x1c, 0x1360017 );		// PAL_TACTEND[25:16], PAL_TACTBEG[9:0]
		VID_REG_SET32( REG_BASE_VID+0x20, 0x26f0150 );		// PAL_BACTEND[25:16], PAL_BACTBEG[9:0]

		VID_REG_SET32( REG_BASE_VID+0x68, 720        );	// WIDTH[7:0];
		VID_REG_SET32( REG_BASE_VID+0x6C, 768        );	// LN_WIDTH[7:0]
		VID_REG_SET32( REG_BASE_VID+0x70, 576        );	// VID_HEIGHT[9:0]
	}

	VID_REG_SET32( REG_BASE_VID+0x10, 0x20001   );		// HSB[9:0];HSW[9:0];HSP[28]
	VID_REG_SET32( REG_BASE_VID+0x18, 0x3f0002  );		// HVDLY @[25:16];VSW @[9:0];VSP[28]
	VID_REG_SET32( REG_BASE_VID+0x24, 0x1060017 );		// NTSC_TACTEND[25:16],NTSC_TACTBEG[9:0]
	VID_REG_SET32( REG_BASE_VID+0x28, 0x20d011e );		// NTSC_BACTEND[25:16],NTSC_BACTBEG[9:0]

	VID_REG_SET32( REG_BASE_VID+0x78, 0 	     );	// REG_OUTPUT444_EN,( 0:422 format, 1:444 format )
	VID_REG_SET32( REG_BASE_VID+0x7C, 0 	     );	// REG_HSCALE_MODE(0:bypass , 1: horizontal scale 1/2)
	VID_REG_SET32( REG_BASE_VID+0x74, 0x1 	      );	// VID Memory write Enable
	VID_REG_SET32( REG_BASE_VID+0xfc, 0x1 	      );	// BIT SWAP
	//VID_REG_SET32( REG_BASE_VID+0x00, 0x1 	     );	// VID Enable
}


int wmt_vid_i2c_xfer(struct i2c_msg *msg, unsigned int num, int bus_id)
{
	int  ret = 1;
	int i = 0;

	if (num > 1) {
		for (i = 0; i < num - 1; ++i)
			msg[i].flags |= I2C_M_NOSTART;
	}
	ret = i2c_transfer(vid_i2c_adapter[vid_dev_num], msg, num);

	if (ret <= 0) {
		printk("[%s] fail ret %d \n", __func__, ret);
		return ret;
	}

	return ret;
}

int write_array_2_i2c(int slave_addr, char *array_addr, int array_size)
{
	int i=0;
	
	for (i = 0; i < array_size; i += 2) {
		wmt_vid_i2c_write(slave_addr, array_addr[i], array_addr[i+1]);
	}

	return 0;
}

int wmt_vid_i2c_write_page(int chipId ,unsigned int index,char *pdata,int len)
{
    struct i2c_msg msg[1];
    unsigned char buf[4];
    int ret;
    
    buf[0] = index;
    memcpy(&buf[1],pdata,len);
    msg[0].addr = chipId;
    msg[0].flags = 0 ;
    msg[0].flags &= ~(I2C_M_RD);
    msg[0].len = len;
    msg[0].buf = buf;

    ret = wmt_vid_i2c_xfer(msg,1,1);
    
    return ret;
}

int wmt_vid_i2c_read_page(int chipId ,unsigned int index,char *pdata,int len) 
{
    struct i2c_msg msg[2];
    unsigned char buf[4];
    
    memset(buf,0x55,len+1);
    buf[0] = index;

    msg[0].addr = chipId;
    msg[0].flags = 0 ;
    msg[0].flags &= ~(I2C_M_RD);
    msg[0].len = 1;
    msg[0].buf = buf;

    msg[1].addr = chipId;
    msg[1].flags = 0 ;
    msg[1].flags |= (I2C_M_RD);
    msg[1].len = len;
    msg[1].buf = buf;

    wmt_vid_i2c_xfer(msg,2,1);

    memcpy(pdata,buf,len);

    return 0;
}

int wmt_vid_i2c_read(int chipId ,unsigned int index) 
{
	char retval;

	if (vid_i2c_gpio_en) {
		wmt_swi2c_read(&vid_swi2c, chipId*2, index, &retval, 1);
	} else {
		wmt_vid_i2c_read_page( chipId ,index,&retval,1) ;   
	}

	return retval;
}

int wmt_vid_i2c_write(int chipId ,unsigned int index,char data)
{
	if (vid_i2c_gpio_en) {
		wmt_swi2c_write(&vid_swi2c, chipId*2,  index, &data, 2);
	} else {
		wmt_vid_i2c_write_page(chipId ,index, &data, 2);
	}

	return 0;
}

int wmt_vid_i2c_write16addr(int chipId ,unsigned int index,unsigned int data)
{
	int ret=0;
	struct i2c_msg msg[1];
	unsigned char buf[4];

	if (vid_i2c_gpio_en) {
		DBG_ERR("wmt_vid_i2c_write16addr() Not support GPIO mode now");
		return -1;
	}

	buf[0]=((index>>8) & 0xff);
	buf[1]=(index & 0xff);
	buf[2]=(data & 0xff);

	msg[0].addr = chipId;
	msg[0].flags = 0;
	msg[0].flags &= ~(I2C_M_RD);
	msg[0].len = 3;
	msg[0].buf = buf;
	ret = wmt_vid_i2c_xfer(msg,1,1);

	return ret;
}

int wmt_vid_i2c_write16data(int chipId ,unsigned int index,unsigned int data)
{
	int ret=0;
	struct i2c_msg msg[1];
	unsigned char buf[4];

	if (vid_i2c_gpio_en) {
		DBG_ERR("wmt_vid_i2c_write16data() Not support GPIO mode now");
		return -1;
	}

	buf[0]=((index>>8) & 0xff);
	buf[1]=(index & 0xff);
	buf[2]=((data>>8) & 0xff);
	buf[3]=(data & 0xff);

	msg[0].addr = chipId;
	msg[0].flags = 0;
	msg[0].flags &= ~(I2C_M_RD);
	msg[0].len = 4;
	msg[0].buf = buf;
	ret = wmt_vid_i2c_xfer(msg,1,1);

	return ret;
}

int wmt_vid_i2c_read16addr(int chipId ,unsigned int index)
{
	int ret=0;
	struct i2c_msg msg[2];
	unsigned char buf[2];
	unsigned int val;

	if (vid_i2c_gpio_en) {
		DBG_ERR("wmt_vid_i2c_read16addr() Not support GPIO mode now");
		return -1;
	}

	buf[0]=((index>>8) & 0xff);
	buf[1]=(index & 0xff);

	msg[0].addr = chipId;
	msg[0].flags = 0;
	msg[0].flags &= ~(I2C_M_RD);
	msg[0].len = 2;
	msg[0].buf = buf;

	msg[1].addr = chipId;
	msg[1].flags = 0;
	msg[1].flags |= (I2C_M_RD);
	msg[1].len = 1;
	msg[1].buf = buf;

	ret = wmt_vid_i2c_xfer(msg,2,1);

	val=buf[0];
	return val;
}

int wmt_vid_i2c_read16data(int chipId ,unsigned int index)
{
	int ret=0;
	struct i2c_msg msg[2];
	unsigned char buf[2];
	unsigned int val;

	if (vid_i2c_gpio_en) {
		DBG_ERR("wmt_vid_i2c_read16data() Not support GPIO mode now");
		return -1;
	}

	buf[0]=((index>>8) & 0xff);
	buf[1]=(index & 0xff);

	msg[0].addr = chipId;
	msg[0].flags = 0;
	msg[0].flags &= ~(I2C_M_RD);
	msg[0].len = 2;
	msg[0].buf = buf;

	msg[1].addr = chipId;
	msg[1].flags = 0;
	msg[1].flags |= (I2C_M_RD);
	msg[1].len = 2;
	msg[1].buf = buf;

	ret = wmt_vid_i2c_xfer(msg,2,1);

	//printk("wmt_vid_i2c_read16data(%x,%x)\n",buf[0],buf[1]);

	val = ((buf[0]&0xff)<<8) | (buf[1]&0xff);

	return val;
}

//---dev_0---------------------------------------------

struct wmt_vid_i2c_priv_0 {
	unsigned int var;
};

static int __devinit wmt_vid_i2c_probe_0(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	struct wmt_vid_i2c_priv *wmt_vid_i2c;
	int ret=0;

	printk("wmt_vid_i2c_probe_0() \n");
	if (!i2c_check_functionality(vid_i2c_client[0]->adapter, I2C_FUNC_I2C)) {
		printk(KERN_ERR "wmt_vid_i2c_probe: need I2C_FUNC_I2C\n");
		return -ENODEV;
	}

	wmt_vid_i2c = kzalloc(sizeof(struct wmt_vid_i2c_priv_0), GFP_KERNEL);
	if (wmt_vid_i2c == NULL) {
		printk("wmt_vid_i2c_probe kzalloc fail");
		return -ENOMEM;
	}
	i2c_set_clientdata(i2c, wmt_vid_i2c);

	return ret;
}

static int  wmt_vid_i2c_remove_0(struct i2c_client *client)
{
	kfree(i2c_get_clientdata(vid_i2c_client[0]));
	return 0;
}

static const struct i2c_device_id wmt_vid_i2c_id_0[] = {
	{ "wmt_vid_i2c_0", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, wmt_vid_i2c_id_0);

static struct i2c_driver wmt_vid_i2c_driver_0 = {
	.probe = wmt_vid_i2c_probe_0,
	.remove = wmt_vid_i2c_remove_0,
	.id_table = wmt_vid_i2c_id_0,
	.driver = {
		.name = "wmt_vid_i2c_0",
	},
};

static struct i2c_board_info wmt_vid_i2c_board_info_0 = {
	.type          = "wmt_vid_i2c_0",
	.flags         = 0x00,
	.platform_data = NULL,
	.archdata      = NULL,
	.irq           = -1,
};

//---dev_1---------------------------------------------
struct wmt_vid_i2c_priv_1 {
	unsigned int var;
};

static int __devinit wmt_vid_i2c_probe_1(struct i2c_client *i2c,
					 const struct i2c_device_id *id)
{
	struct wmt_vid_i2c_priv *wmt_vid_i2c;
	int ret=0;

	printk("wmt_vid_i2c_probe_1() \n");

	if (!i2c_check_functionality(vid_i2c_client[1]->adapter, I2C_FUNC_I2C)) {
		printk(KERN_ERR "wmt_vid_i2c_probe: need I2C_FUNC_I2C\n");
		return -ENODEV;
	}

	wmt_vid_i2c = kzalloc(sizeof(struct wmt_vid_i2c_priv_1), GFP_KERNEL);
	if (wmt_vid_i2c == NULL) {
		printk("wmt_vid_i2c_probe kzalloc fail");
		return -ENOMEM;
	}

	i2c_set_clientdata(i2c, wmt_vid_i2c);

	return ret;
}

static int  wmt_vid_i2c_remove_1(struct i2c_client *client)
{
	kfree(i2c_get_clientdata(vid_i2c_client[1]));
	return 0;
}

static const struct i2c_device_id wmt_vid_i2c_id_1[] = {
	{ "wmt_vid_i2c_1", 0},
	{ }
};
MODULE_DEVICE_TABLE(i2c, wmt_vid_i2c_id_1);

static struct i2c_driver wmt_vid_i2c_driver_1 = {
	.probe = wmt_vid_i2c_probe_1,
	.remove = wmt_vid_i2c_remove_1,
	.id_table = wmt_vid_i2c_id_1,
	.driver = { .name = "wmt_vid_i2c_1", },
};

static struct i2c_board_info wmt_vid_i2c_board_info_1 = {
	.type          = "wmt_vid_i2c_1",
	.flags         = 0x00,
	.platform_data = NULL,
	.archdata      = NULL,
	.irq           = -1,
};

//---------------------------------------------------------
int wmt_vid_i2c_set_same_dev(void)
{
	vid_dev_tot_num = 2;
	vid_i2c_adapter[1] = vid_i2c_adapter[0];
	vid_i2c_client[1] = vid_i2c_client[0];

	return 0;
}

int wmt_vid_i2c_init(int dev_num, short dev_id)
{
	struct i2c_board_info *wmt_vid_i2c_bi ;
	struct i2c_adapter *adapter;
	struct i2c_client *client;

	int ret =0;

	printk("wmt_vid_i2c_init() dev_num %d  dev_id 0x%02x \n",dev_num,dev_id);

	if (dev_num == 0)
		wmt_vid_i2c_bi = &wmt_vid_i2c_board_info_0;
	else
		wmt_vid_i2c_bi = &wmt_vid_i2c_board_info_1;

	adapter = i2c_get_adapter(WMT_VID_I2C_CHANNEL);
	if (adapter == NULL) {
		printk("can not get i2c adapter, client address error");
		return -ENODEV;
	}

	wmt_vid_i2c_bi->addr = dev_id;

	client = i2c_new_device(adapter, wmt_vid_i2c_bi);
	if ( client == NULL) {
		printk("allocate i2c client failed");
		return -ENOMEM;
	}

	vid_dev_tot_num = dev_num+1;
	vid_i2c_client[dev_num] = client;
	vid_i2c_adapter[dev_num] = adapter;

	i2c_put_adapter(adapter);
	if (dev_num == 0)
		ret = i2c_add_driver(&wmt_vid_i2c_driver_0);
	else
		ret = i2c_add_driver(&wmt_vid_i2c_driver_1);

	if (ret) {
		printk("i2c_add_driver fail");
	}	

	return ret;
}

int wmt_vid_i2c_release(void)
{
	int i;

	for (i = 0; i < vid_dev_tot_num; i++) {
		i2c_unregister_device(vid_i2c_client[i]);
		if (i == 0) {
			wmt_vid_i2c_remove_0(vid_i2c_client[i]);
			i2c_del_driver(&wmt_vid_i2c_driver_0);
		} else {
			wmt_vid_i2c_remove_1(vid_i2c_client[i]);
			i2c_del_driver(&wmt_vid_i2c_driver_1);
		}
	}

	return 0;
}

int wmt_vid_set_mode(int width, int height, unsigned int pix_format)
{
	TRACE("Enter\n");

	VID_REG_SET32(REG_VID_WIDTH, width); /* VID output width */
	VID_REG_SET32(REG_VID_LINE_WIDTH, width);
	VID_REG_SET32(REG_VID_HEIGHT, height);    /* VID output height */

	if (pix_format == V4L2_PIX_FMT_YUYV) // YUV422
	{
		DBG_INFO("V4L2_PIX_FMT_YUYV \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x0);    //CMOS Sensor Input mode, set to YUV
		VID_REG_SET32( REG_BASE_VID+0x98, 0x0);    //CMOS RGB Output Mode, only RGB565 need set to '1'
		VID_REG_SET32( REG_BASE_VID+0x78, 0x0);    //REG_OUTPUT444_EN, set to YUV 422
	}
	else if (pix_format == V4L2_PIX_FMT_YUV444) {
		DBG_INFO("V4L2_PIX_FMT_YUYV \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x0);    //CMOS Sensor Input mode, set to YUV
		VID_REG_SET32( REG_BASE_VID+0x98, 0x0);    //CMOS RGB Output Mode, only RGB565 need set to '1'
		VID_REG_SET32( REG_BASE_VID+0x78, 0x1);    //REG_OUTPUT444_EN, set to YUV 444
	}
	else if (pix_format == V4L2_PIX_FMT_RGB565) {
		DBG_INFO("V4L2_PIX_FMT_RGB565 \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x1);    //CMOS Sensor Input mode, Set to RGB
		VID_REG_SET32( REG_BASE_VID+0x98, 0x1);    //CMOS RGB Output Mode, only RGB565 need set to '1'
	}
	else if (pix_format == V4L2_PIX_FMT_RGB32) {
		DBG_INFO("V4L2_PIX_FMT_RGB888 \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x1);    //CMOS Sensor Input mode, Set to RGB
		VID_REG_SET32( REG_BASE_VID+0x98, 0x0);    //CMOS RGB Output Mode , only RGB565 need set to '1'
	}
	else if (pix_format == V4L2_PIX_FMT_NV12) {
		DBG_INFO("V4L2_PIX_FMT_NV12 \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x0);    //CMOS Sensor Input mode, set to YUV
		VID_REG_SET32( REG_BASE_VID+0x98, 0x0);    //CMOS RGB Output Mode, only RGB565 need set to '1'
		VID_REG_SET32( REG_BASE_VID+0x78, 0x10);
		VID_REG_SET32( REG_BASE_VID+0x74, 0x01);
	}
	else if (pix_format == V4L2_PIX_FMT_NV21) {
		DBG_INFO("V4L2_PIX_FMT_NV21 \n");
		VID_REG_SET32( REG_BASE_VID+0x90, 0x0);    //CMOS Sensor Input mode, set to YUV
		VID_REG_SET32( REG_BASE_VID+0x98, 0x0);    //CMOS RGB Output Mode, only RGB565 need set to '1'
		VID_REG_SET32( REG_BASE_VID+0x78, 0x10);
		VID_REG_SET32( REG_BASE_VID+0x74, 0x11);
	}
	else {
		pix_format = V4L2_PIX_FMT_YUYV;
		DBG_INFO("V4L2_PIX_unknow , set to V4L2_PIX_FMT_YUYV \n");
	}

	TRACE("Leave\n");

	return 0;
}

int wmt_vid_set_cur_fb(vid_fb_t *fb)
{
	unsigned long flags =0;

	spin_lock_irqsave(&vid_lock, flags);

	_prev_fb = _cur_fb;
	_cur_fb  = fb;

	fb->is_busy = 1;
	fb->done    = 0;
	wmt_vid_set_addr(fb->y_addr, fb->c_addr);		    

	spin_unlock_irqrestore(&vid_lock, flags);

	return 0;
}

vid_fb_t *wmt_vid_get_cur_fb(void)
{
	unsigned long flags =0;
	vid_fb_t *fb;    

	spin_lock_irqsave(&vid_lock, flags);
	fb = _cur_fb;
	spin_unlock_irqrestore(&vid_lock, flags);

	return fb;
}

int wmt_vid_set_addr(unsigned int y_addr, unsigned int c_addr)
{
	unsigned int  cur_y_addr;   // temp
	unsigned int  cur_c_addr;   // temp

	TRACE("Enter\n");

	cur_y_addr = REG32_VAL(REG_VID_Y0_SA);
	cur_c_addr = REG32_VAL(REG_VID_C0_SA);

	if ((y_addr != cur_y_addr) || (c_addr != cur_c_addr)) {
		VID_REG_SET32( REG_VID_Y0_SA, y_addr); /* VID Y FB address */
		VID_REG_SET32( REG_VID_C0_SA, c_addr); /* VID C FB address */
	}

	TRACE("Leave\n");
	return 0;
}

int wmt_vid_open(vid_mode mode, cmos_uboot_env_t *uboot_env)
{
	int value, int_ctrl;
	TRACE("Enter\n");

	//vid_i2c_gpio_en = uboot_env->i2c_gpio_en;
	DBG_INFO(" vid_i2c_gpio_en 0x%08x \n",vid_i2c_gpio_en);
	if ((vid_i2c_gpio_en) && (uboot_env != NULL))
	{
		memset(&vid_i2c0_scl, 0, sizeof(vid_i2c0_scl));
		memset(&vid_i2c0_sda, 0, sizeof(vid_i2c0_sda));

		vid_i2c0_scl.bit_mask = 1 << uboot_env->i2c_gpio_scl_binum;
		vid_i2c0_scl.pull_en_bit_mask = 1 << uboot_env->reg_i2c_gpio_scl_gpio_pe_bitnum;

		vid_i2c0_scl.data_in = uboot_env->reg_i2c_gpio_scl_gpio_in ;
		vid_i2c0_scl.gpio_en = uboot_env->reg_i2c_gpio_scl_gpio_en ;
		vid_i2c0_scl.out_en = uboot_env->reg_i2c_gpio_scl_gpio_od ;
		vid_i2c0_scl.data_out = uboot_env->reg_i2c_gpio_scl_gpio_oc ;
		vid_i2c0_scl.pull_en = uboot_env->reg_i2c_gpio_scl_gpio_pe ;


		vid_i2c0_sda.bit_mask =  1 << uboot_env->i2c_gpio_sda_binum;
		vid_i2c0_sda.pull_en_bit_mask = 1 << uboot_env->reg_i2c_gpio_sda_gpio_pe_bitnum;

		vid_i2c0_sda.data_in = uboot_env->reg_i2c_gpio_sda_gpio_in ;
		vid_i2c0_sda.gpio_en = uboot_env->reg_i2c_gpio_sda_gpio_en ;
		vid_i2c0_sda.out_en = uboot_env->reg_i2c_gpio_sda_gpio_od ;
		vid_i2c0_sda.data_out = uboot_env->reg_i2c_gpio_sda_gpio_oc ;
		vid_i2c0_sda.pull_en = uboot_env->reg_i2c_gpio_sda_gpio_pe ;

		if (vid_i2c0_scl.data_in & 0x1) {
			vid_i2c0_scl.bit_mask <<= 8;
			vid_i2c0_scl.pull_en_bit_mask <<= 8;
			vid_i2c0_scl.data_in -= 1;
			vid_i2c0_scl.gpio_en -= 1;
			vid_i2c0_scl.out_en -= 1;
			vid_i2c0_scl.data_out -= 1;
			vid_i2c0_scl.pull_en -= 1;

		}
		if (vid_i2c0_sda.data_in & 0x1) {
			vid_i2c0_sda.bit_mask <<= 8;
			vid_i2c0_sda.pull_en_bit_mask <<= 8;
			vid_i2c0_sda.data_in -= 1;
			vid_i2c0_sda.gpio_en -= 1;
			vid_i2c0_sda.out_en -= 1;
			vid_i2c0_sda.data_out -= 1;
			vid_i2c0_sda.pull_en -= 1;
		}

		vid_swi2c.scl_reg = &vid_i2c0_scl;
		vid_swi2c.sda_reg = &vid_i2c0_sda;

		DBG_INFO("SCL 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x \n",
			 vid_i2c0_scl.bit_mask, vid_i2c0_scl.pull_en_bit_mask,
			 vid_i2c0_scl.data_in, vid_i2c0_scl.gpio_en,
			 vid_i2c0_scl.out_en, vid_i2c0_scl.data_out,
			 vid_i2c0_scl.pull_en);
		DBG_INFO("SDA 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x \n",
			 vid_i2c0_sda.bit_mask, vid_i2c0_sda.pull_en_bit_mask,
			 vid_i2c0_sda.data_in, vid_i2c0_sda.gpio_en,
			 vid_i2c0_sda.out_en, vid_i2c0_sda.data_out,
			 vid_i2c0_sda.pull_en);
	}

	/*--------------------------------------------------------------------------
	  Step 1: Init GPIO for CMOS or TVDEC mode
	  --------------------------------------------------------------------------*/
	vid_gpio_init(mode);

	/*--------------------------------------------------------------------------
	  Step 2: Init CMOS or TVDEC module
	  --------------------------------------------------------------------------*/
	value = REG32_VAL(REG_VID_TVDEC_CTRL);
	VID_REG_SET32( REG_VID_MEMIF_EN, 0x1 );
	VID_REG_SET32( REG_VID_OUTPUT_FORMAT, 0x0 );  // 0: 422   1: 444

	int_ctrl = 0x00;
	if (mode == VID_MODE_CMOS) {
		VID_REG_SET32( REG_VID_TVDEC_CTRL, (value & 0xFFFFFFE)); /* disable TV decoder */
		VID_REG_SET32( REG_VID_CMOS_PIXEL_SWAP, 0x2);    /* 0x2 for YUYV */
#ifdef VID_INT_MODE
		int_ctrl = 0x0808;
#endif
		VID_REG_SET32( REG_VID_INT_CTRL, int_ctrl );

	}
	else {
		VID_REG_SET32( REG_VID_TVDEC_CTRL, (value | 0x1) );	/* enable TV decoder */
#ifdef VID_INT_MODE
		int_ctrl = 0x0404;
#endif
		VID_REG_SET32( REG_VID_INT_CTRL, int_ctrl );
		VID_REG_SET32( REG_VID_CMOS_EN, 0x0);	/* disable CMOS */
		wmt_vid_set_common_mode(VID_NTSC);

	}

	_cur_fb  = 0;
	_prev_fb = 0;

	spin_lock_init(&vid_lock);

	TRACE("Leave\n");

	return 0;
}

int wmt_vid_close(vid_mode mode)
{
	TRACE("Enter\n");

	auto_pll_divisor(DEV_VID, CLK_DISABLE, 0, 0); 
	auto_pll_divisor(DEV_C24MOUT, CLK_DISABLE, 0, 0); 

	GPIO_CTRL_GP17_I2C_BYTE_VAL |= BIT6;//24Mhz off , set to GPIO

	if(mode == VID_MODE_CMOS) {
		VID_REG_SET32( REG_VID_CMOS_EN, 0x0);	/* disable CMOS */
	}
	else {
		int value = REG32_VAL(REG_VID_TVDEC_CTRL);
		VID_REG_SET32( REG_VID_TVDEC_CTRL, (value & 0xFFFFFFE)); /* disable TV decoder */
	}
	TRACE("Leave\n");

	return 0;
}

EXPORT_SYMBOL(wmt_vid_set_mode);
EXPORT_SYMBOL(wmt_vid_i2c_write);
EXPORT_SYMBOL(wmt_vid_i2c_read);
EXPORT_SYMBOL(wmt_vid_i2c_write16addr);
EXPORT_SYMBOL(wmt_vid_i2c_read16addr);
EXPORT_SYMBOL(wmt_vid_i2c_write16data);
EXPORT_SYMBOL(wmt_vid_i2c_read16data);
EXPORT_SYMBOL(wmt_vid_set_cur_fb);
EXPORT_SYMBOL(wmt_vid_get_cur_fb);
EXPORT_SYMBOL(wmt_vid_set_addr);
EXPORT_SYMBOL(wmt_vid_close);
EXPORT_SYMBOL(wmt_vid_open);
EXPORT_SYMBOL(wmt_vid_set_common_mode);
EXPORT_SYMBOL(wmt_vid_i2c_init);
EXPORT_SYMBOL(wmt_vid_i2c_release);

MODULE_AUTHOR("WonderMedia SW Team Max Chen");
MODULE_DESCRIPTION("wmt-vid  driver");
MODULE_LICENSE("GPL");