summaryrefslogtreecommitdiff
path: root/drivers/media/platform/starfive/stf_vin.c
blob: 21fc51573f2c32a900ca3b315ac19c11c0649282 (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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
// SPDX-License-Identifier: GPL-2.0
/*
 * Driver for VIC Video In
 *
 * Copyright (C) starfivetech.Inc
 * Authors: Xing Tang <eric.tang@starfivetech.com>
 *
 */

#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/dmaengine.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_reserved_mem.h>
#include <linux/of_graph.h>
#include <linux/of_address.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/io.h>
#include <linux/dma-mapping.h>
#include <linux/uaccess.h>
#include <video/stf-vin.h>
#include "stf_isp.h"
#include "stf_csi.h"


static const struct reg_name mem_reg_name[] = {
	{"mipi0"},
	{"vclk"},
	{"vrst"},
	{"mipi1"},
	{"sctrl"},
	{"isp0"},
	{"isp1"},
	{"tclk"},
	{"trst"},
	{"iopad"}
};

static DEFINE_MUTEX(vin_mutex);



static inline u32 reg_read(void __iomem * base, u32 reg)
{
	return ioread32(base + reg);
}

static inline void reg_write(void __iomem * base, u32 reg, u32 val)
{
	iowrite32(val, base + reg);
}

static inline void reg_set(void __iomem * base, u32 reg, u32 mask)
{
	reg_write(base, reg, reg_read(base, reg) | mask);
}

static inline void reg_clear(void __iomem * base, u32 reg, u32 mask)
{
	reg_write(base, reg, reg_read(base, reg) & ~mask);
}

static void reg_set_highest_bit(void __iomem * base, u32 reg)
{
    u32 val;
	val = ioread32(base + reg);
	val &= ~(0x1 << 31);
	val |= (0x1 & 0x1) << 31;
	iowrite32(val, base + reg);
}

static void reg_clear_highest_bit(void __iomem * base, u32 reg)
{
    u32 val;
	val = ioread32(base + reg);
	val &= ~(0x1 << 31);
	val |= (0x0 & 0x1) << 31;
	iowrite32(val, base + reg);
}

static int vin_open(struct inode *inode, struct file *file)
{
	int ret = 0;
	struct stf_vin_dev *dev;

	mutex_lock(&vin_mutex);

	dev=container_of(inode->i_cdev, struct stf_vin_dev, vin_cdev);

	file->private_data = dev;
//out:
	mutex_unlock(&vin_mutex);
	return ret;
}

static ssize_t vin_read(struct file *file, char __user * buf,
			size_t count, loff_t * ppos)
{
	int ret;
	int data[2];
	struct stf_vin_dev *vin = file->private_data;
	if (vin->condition == false) {
		if (file->f_flags & O_NONBLOCK)
			return -EAGAIN;
		if (wait_event_interruptible(vin->wq, vin->condition != false))
			return -ERESTARTSYS;
	}
	data[0] = vin->odd;
	data[1] = vin->buf.size;

	mutex_lock(&vin_mutex);
	ret = copy_to_user(buf, data, count);
	if (ret != 0) {
		pr_err("Failed to copy data\n");
		return -EINVAL;
	}
	mutex_unlock(&vin_mutex);

	vin->condition = false;

	return count;
}

static int vin_release(struct inode *inode, struct file *file)
{
	return 0;
}

static const struct vm_operations_struct mmap_mem_ops = {
#ifdef CONFIG_HAVE_IOREMAP_PROT
	.access = generic_access_phys
#endif
};

static int vin_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct stf_vin_dev *vin = file->private_data;
	size_t size = vma->vm_end - vma->vm_start;

	vma->vm_ops = &mmap_mem_ops;

	/* Remap-pfn-range will mark the range VM_IO */
	if (remap_pfn_range(vma,
			    vma->vm_start,
			    vin->buf.paddr >> PAGE_SHIFT,
			    size, vma->vm_page_prot)) {
		return -EAGAIN;
	}

	return 0;
}

static long int vin_ioctl(struct file *file, unsigned int cmd,
			  long unsigned int arg)
{
	return 0;
}

static const struct file_operations vin_fops = {
	.owner = THIS_MODULE,
	.open = vin_open,
	.read = vin_read,
	.release = vin_release,
	.unlocked_ioctl = vin_ioctl,
	.mmap = vin_mmap,
};

static int vin_get_mem_res(struct platform_device *pdev, struct stf_vin_dev *vin)
{
	struct device *dev = &pdev->dev;
	struct resource	*res;
	void __iomem *regs;
	char *name;
	int i;

	for (i = 0; i < sizeof(mem_reg_name)/sizeof(struct reg_name); i++) {
	    name = (char *)(& mem_reg_name[i]);
		res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
		regs = devm_ioremap_resource(dev, res);
		if (IS_ERR(regs))
			return PTR_ERR(regs);	

		if(!strcmp(name, "mipi0")) {
			vin->mipi0_base = regs;
		} else if (!strcmp(name, "vclk")) {
			vin->clkgen_base = regs;
		} else if (!strcmp(name, "vrst")) {
			vin->rstgen_base = regs;
		} else if (!strcmp(name, "mipi1")) {
			vin->mipi1_base = regs;
		} else if (!strcmp(name, "sctrl")) {
			vin->sysctrl_base = regs;
		} else if (!strcmp(name, "isp0")) {
			vin->isp_isp0_base = regs;
		} else if (!strcmp(name, "isp1")) {
			vin->isp_isp1_base = regs;
		} else if (!strcmp(name, "tclk")) {
			vin->vin_top_clkgen_base = regs;
		} else if (!strcmp(name, "trst")) {
			vin->vin_top_rstgen_base = regs;
		}else if (!strcmp(name, "iopad")) {
			vin->vin_top_iopad_base = regs;
		}else {
			dev_err(&pdev->dev, "Could not match resource name\n");
		}
	}

	return 0;
}

/*-------------------------------------------------------*/
/*
 * NOTE:
 * 	vic clk driver hasn't complete, using follow functions
 * 	to reset
 * TODO: vic clk driver
 *
 */
static int vin_clk_reset(struct stf_vin_dev *vin)
{
	//disable clk
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_SRC_CTRL);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0_AXI_CTRL);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0NOC_AXI_CTRL);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_ISPSLV_AXI_CTRL);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1_AXI_CTRL);
	reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1NOC_AXI_CTRL);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_AXI);
    reg_clear_highest_bit(vin->vin_top_clkgen_base,CLK_VINNOC_AXI);

	//enable clk
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_SRC_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0NOC_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISPSLV_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1_AXI_CTRL);
	reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1NOC_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_AXI);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VINNOC_AXI);
	return 0;
}

static int isp_rstgen_assert_reset(struct stf_vin_dev *vin)
{
	 u32 val;	 
	 u32 val_reg_reset_config = 0x19f807;
		 
	 val = ioread32(vin->rstgen_base + SOFTWARE_RESET_ASSERT0);
	 val |= val_reg_reset_config;
	 iowrite32(val, vin->rstgen_base + SOFTWARE_RESET_ASSERT0);
 
	 val = ioread32(vin->rstgen_base + SOFTWARE_RESET_ASSERT0);
	 val &= ~(val_reg_reset_config);
 
	 iowrite32(val, vin->rstgen_base + SOFTWARE_RESET_ASSERT0);
		 	
	return 0;
}

static int vin_rstgen_assert_reset(struct stf_vin_dev *vin)
{
	u32 val;
	/*
	 *      Software_RESET_assert1 (0x11840004)
	 *      ------------------------------------
	 *      bit[15]         rstn_vin_src
	 *      bit[16]         rstn_ispslv_axi
	 *      bit[17]         rstn_vin_axi
	 *      bit[18]         rstn_vinnoc_axi
	 *      bit[19]         rstn_isp0_axi
	 *      bit[20]         rstn_isp0noc_axi
	 *      bit[21]         rstn_isp1_axi
	 *      bit[22]         rstn_isp1noc_axi
	 *
	 */
	u32 val_reg_reset_config = 0x7f8000;

	val = ioread32(vin->vin_top_rstgen_base + SOFTWARE_RESET_ASSERT1);
	val |= val_reg_reset_config;
	iowrite32(val, vin->vin_top_rstgen_base + SOFTWARE_RESET_ASSERT1);

	val = ioread32(vin->vin_top_rstgen_base + SOFTWARE_RESET_ASSERT1);
	val &= ~(val_reg_reset_config);

	iowrite32(val, vin->vin_top_rstgen_base + SOFTWARE_RESET_ASSERT1);

	return 0;
}

/*
 * vin_get_pixel_size:
 *
 * size = (width * height * 4)/(cnfg_axiwr_pix_ct / 2)
 */
static int vin_get_axiwr_pixel_size(struct stf_vin_dev *vin)
{
	u32 value;
	int cnfg_axiwr_pix_ct;

	value = reg_read(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL) & 0x3;
	if (value == 0)
		cnfg_axiwr_pix_ct = 2;
	else if (value == 1)
		cnfg_axiwr_pix_ct = 4;
	else if (value == 2)
		cnfg_axiwr_pix_ct = 8;
	else
		return 0;

	return (vin->frame.height * vin->frame.width * 4) / (cnfg_axiwr_pix_ct /
							     2);
}

static int vin_update_buf_size(struct stf_vin_dev *vin)
{
	u32 size;
	if (vin->format.format == SRC_DVP_SENSOR_VIN_OV5640) //ov5640 out rgb565
		size = vin->frame.height*vin->frame.width*2;
	else
		size = vin_get_axiwr_pixel_size(vin); //out raw data

	if (size == 0) {
		dev_err(vin->dev, "size is 0");
		return -EINVAL;
	}

	vin->buf.size = size;

	return 0;
}


static void dvp_io_pad_func_shared_config(struct stf_vin_dev *vin)
{
	/*
	 * pin: 49 ~ 57
	 * offset: 0x144 ~ 0x164
	 * SCFG_funcshare_pad_ctrl
	 */
	u32 val_scfg_funcshare_config = 0x800080;
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG81);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG82);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG83);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG84);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG85);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG86);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG87);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG88);
	iowrite32(val_scfg_funcshare_config, vin->vin_top_iopad_base + IOPAD_REG89);
}

static int vin_rstgen_clkgen(struct stf_vin_dev *vin)
{
	u32 val_vin_axi_wr_ctrl_reg = 0x02000000;

	/* rst disable */
	reg_write(vin->rstgen_base, SOFTWARE_RESET_ASSERT0, 0xFFFFFFFF);

	/* rst enable */
	reg_write(vin->rstgen_base, SOFTWARE_RESET_ASSERT0, 0x0);

	switch (vin->format.format) {
	case SRC_DVP_SENSOR_VIN_OV5640:
	case SRC_DVP_SENSOR_VIN:
		reg_write(vin->clkgen_base, CLK_VIN_AXI_WR_CTRL, val_vin_axi_wr_ctrl_reg);
		break;

	case SRC_COLORBAR_VIN_ISP:
	case SRC_DVP_SENSOR_VIN_ISP:
		isp_clk_set(vin);
		break;

	case SRC_CSI2RX_VIN_ISP:
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int get_vin_axiwr_pix_ct(struct stf_vin_dev *vin)
{
	u32 value;
	int cnfg_axiwr_pix_ct = 0;

	value = reg_read(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL) & 0x3;
	if (value == 0)
		cnfg_axiwr_pix_ct = 2;
	else if (value == 1)
		cnfg_axiwr_pix_ct = 4;
	else if (value == 2)
		cnfg_axiwr_pix_ct = 8;
	else
		return 0;

	return cnfg_axiwr_pix_ct;
}

static void set_vin_wr_pix_total(struct stf_vin_dev *vin)
{
	int val, pix_ct;
	pix_ct = get_vin_axiwr_pix_ct(vin);
	val = (vin->frame.width / pix_ct) - 1;
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_WR_PIX_TOTAL, val);
}



static void dvp_vin_ddr_addr_config(struct stf_vin_dev *vin)
{
	u32 val_vin_axi_ctrl_reg = 0x00000003;
	dev_dbg(vin->dev, "%d: addr: 0x%lx, size: 0x%lx\n", __LINE__,
		(long)vin->buf.paddr, (long)(vin->buf.paddr + vin->buf.size));
	/* set the start address of vin and enable */
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_WR_START_ADDR, (long)vin->buf.paddr);
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_END_ADDR, (long)(vin->buf.paddr + vin->buf.size));
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_AXI_CTRL, val_vin_axi_ctrl_reg);
}

static int stf_vin_config_set(struct stf_vin_dev *vin)
{
	u32  val_vin_rd_pix_total_reg,val_vin_rd_vblank_reg,val_vin_rd_vend_reg;
	u32  val_vin_rd_hblank_reg,val_vin_rd_hend_reg,val_vin_rw_ctrl_reg;
	u32  val_vin_src_channel_reg,val_vin_axi_ctrl_reg,val_vin_rw_start_addr_reg;
	u32  val_vin_rd_end_addr_reg,val_vin_wr_pix_reg,reset_val;
//	int ret;
	csi2rx_dphy_cfg_t dphy_rx_cfg = {
			.dlane_nb		= vin->csi_fmt.lane,
			.dlane_map		= {vin->csi_fmt.dlane_swap[0],	  vin->csi_fmt.dlane_swap[1],	 vin->csi_fmt.dlane_swap[2],	vin->csi_fmt.dlane_swap[3]},
			.dlane_pn_swap	= {vin->csi_fmt.dlane_pn_swap[0], vin->csi_fmt.dlane_pn_swap[1], vin->csi_fmt.dlane_pn_swap[2], vin->csi_fmt.dlane_pn_swap[3]},
			.dlane_en		= {vin->csi_fmt.lane>0,   vin->csi_fmt.lane>1,   vin->csi_fmt.lane>2, 	vin->csi_fmt.lane>3},
			.clane_nb		= 1,
			.clane_map		= {vin->csi_fmt.clane_swap, 	5},
			.clane_pn_swap	= {vin->csi_fmt.clane_pn_swap,	0},
	};
	csi2rx_cfg_t csi2rx_cfg = {
				.lane_nb	= vin->csi_fmt.lane,//lane count
				.dlane_map	= {1,2,3,4},
				.dt 		= vin->csi_fmt.dt,
				.hsize		= vin->csi_fmt.w,
				.vsize		= vin->csi_fmt.h,
	};

	switch (vin->format.format) {
	case SRC_COLORBAR_VIN_ISP:
		/*vin */
		val_vin_rd_pix_total_reg = 0x000003BF;
		val_vin_rd_vblank_reg = 0x0000002D;
		val_vin_rd_vend_reg = 0x00000464;
		val_vin_rd_hblank_reg = 0x00000117;
		val_vin_rd_hend_reg = 0x00000897;
		val_vin_rw_ctrl_reg = 0x00010300;
		val_vin_src_channel_reg = 0x00000088;
		val_vin_axi_ctrl_reg = 0x00000004;

		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_PIX_TOTAL, val_vin_rd_pix_total_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_VBLANK, val_vin_rd_vblank_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_VEND, val_vin_rd_vend_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_HBLANK, val_vin_rd_hblank_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_HEND, val_vin_rd_hend_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL, val_vin_rw_ctrl_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_SRC_CHAN_SEL, val_vin_src_channel_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_AXI_CTRL, val_vin_axi_ctrl_reg);

		if(vin->isp0||vin->isp1)
			isp_ddr_config(vin);
		break;

	case SRC_DVP_SENSOR_VIN:
		dvp_io_pad_func_shared_config(vin);
		/*
		 * NOTE:
		 * 0x38: [13:12]
		 * 00: pix_data[7:0]
		 * 01: pix_data[9:2]
		 */
	    val_vin_rw_ctrl_reg = 0x00010301;
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL, val_vin_rw_ctrl_reg);

		set_vin_wr_pix_total(vin);
	break;

	case SRC_DVP_SENSOR_VIN_OV5640:
		dvp_io_pad_func_shared_config(vin);
	    val_vin_rw_start_addr_reg = vin->buf.paddr;
		val_vin_rd_end_addr_reg = vin->buf.paddr+vin->frame.width*vin->frame.height*2;
		val_vin_wr_pix_reg = 0x000001df;
		val_vin_rw_ctrl_reg = 0x00001202;
		val_vin_axi_ctrl_reg = 0x00000003;
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_WR_START_ADDR, val_vin_rw_start_addr_reg);	// First frame address
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RD_END_ADDR, val_vin_rd_end_addr_reg);	//4bytes 0x887e9000 2bytes 0x883f4800, 1byte 0x881FA400
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_WR_PIX_TOTAL, val_vin_wr_pix_reg);	// 0x000003bf        0x000001df        0x000000ef
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL, val_vin_rw_ctrl_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_AXI_CTRL,  val_vin_axi_ctrl_reg);
	break;

	case SRC_DVP_SENSOR_VIN_ISP:
		dvp_io_pad_func_shared_config(vin);
	    val_vin_rw_ctrl_reg = 0x00011300;
	    val_vin_src_channel_reg = 0x00001100;
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_RW_CTRL, val_vin_rw_ctrl_reg);
		reg_write(vin->sysctrl_base, SYSCTRL_VIN_SRC_CHAN_SEL, val_vin_src_channel_reg);
		if(vin->isp0||vin->isp1)
			isp_ddr_config(vin);
	break;

	case SRC_CSI2RX_VIN_ISP:
		isp_config(vin, 0);	
	    csi2rx_dphy_config(vin,&dphy_rx_cfg);
		csi2rx_config(vin,0, &csi2rx_cfg);
	    reset_val = reg_read(vin->rstgen_base, 0x00);
		reset_val &= ~(1<<17);
		reg_write(vin->rstgen_base, 0x00, reset_val);
	break;

	default:
		pr_err("unknown format\n");
		return -EINVAL;
	}

	return 0;
}


static void vin_free_buf(struct stf_vin_dev *vin)
{
	struct vin_buf *buf = &vin->buf;

	if (buf->vaddr) {
		buf->vaddr = NULL;
		buf->size = 0;
	}
}

static void vin_intr_clear(void __iomem * sysctrl_base)
{
	reg_write(sysctrl_base, SYSCTRL_VIN_INTP_CTRL, 0x1);
	reg_write(sysctrl_base, SYSCTRL_VIN_INTP_CTRL, 0x0);
}

static irqreturn_t vin_wr_irq_handler(int irq, void *priv)
{
	static struct vin_params params;
	static int wtimes = 0;
	struct stf_vin_dev *vin = priv;

	vin_intr_clear(vin->sysctrl_base);

	wtimes = wtimes % 2;
	if (wtimes == 0)
		params.paddr = (void *)vin->buf.paddr;
	else
		params.paddr = (void *)(vin->buf.paddr + vin->buf.size);

	params.size = vin->buf.size;

	vin_notifier_call(1, &params);

	vin->condition = true;
	vin->odd = wtimes;
	wake_up_interruptible(&vin->wq);
	wtimes++;

	return IRQ_HANDLED;
}

void vin_isp0_intr_clear(struct stf_vin_dev *vin)
{
	u32 value;
	value = reg_read(vin->isp_isp0_base, 0xA00);
	reg_write(vin->isp_isp0_base, 0xA00, value);
}

static irqreturn_t vin_isp0_irq_handler(int irq, void *priv)
{
	static struct vin_params params;
	static int wtimes = 0;
	struct stf_vin_dev *vin = priv;

	/*clear interrupt */
	vin_isp0_intr_clear(vin);

	wtimes = wtimes % 2;
	if (wtimes == 0) {
		params.paddr = (void *)vin->buf.paddr;
		reg_write(vin->isp_isp0_base, ISP_REG_Y_PLANE_START_ADDR,  vin->buf.paddr);	// Unscaled Output Image Y Plane Start Address Register
		reg_write(vin->isp_isp0_base, ISP_REG_UV_PLANE_START_ADDR, vin->buf.paddr+(vin->frame.width*vin->frame.height));	// Unscaled Output Image UV Plane Start Address Register
		reg_write(vin->isp_isp0_base, ISP_REG_STRIDE, vin->frame.width);	// Unscaled Output Image Stride Register
	} else {
		params.paddr = (void *)vin->buf.paddr;
		reg_write(vin->isp_isp0_base, ISP_REG_Y_PLANE_START_ADDR,  vin->buf.paddr+vin->buf.size);	// Unscaled Output Image Y Plane Start Address Register
		reg_write(vin->isp_isp0_base, ISP_REG_UV_PLANE_START_ADDR, vin->buf.paddr+vin->buf.size+(vin->frame.width*vin->frame.height));	// Unscaled Output Image UV Plane Start Address Register
		reg_write(vin->isp_isp0_base, ISP_REG_STRIDE, vin->frame.width);	// Unscaled Output Image Stride Register
	}

	params.size = vin->buf.size;

	vin->condition = true;
	vin->odd = wtimes;
	wake_up_interruptible(&vin->wq);

	vin_notifier_call(1, &params);

	wtimes++;
	return IRQ_HANDLED;
}

static void vin_irq_disable(struct stf_vin_dev *vin)
{
	unsigned int mask_value = 0, value = 0;

	/* mask and clear vin interrupt */
	mask_value = (0x1 << 4) | (0x1 << 20);

	value = 0x1 | (0x1 << 16) | mask_value;
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_INTP_CTRL, value);

	value = mask_value;
	reg_write(vin->sysctrl_base, SYSCTRL_VIN_INTP_CTRL, value);
}

static void vin_irq_enable(struct stf_vin_dev *vin)
{
	unsigned int value = 0;

	value = ~((0x1 << 4) | (0x1 << 20));

	reg_write(vin->sysctrl_base, SYSCTRL_VIN_INTP_CTRL, value);
}

static int vin_sys_init(struct stf_vin_dev *vin)
{
	u32 val;

	val = ioread32(vin->vin_top_clkgen_base + 0x124)>>24;
	val &= 0x1;
	if (val != 0) {
        val = ioread32(vin->vin_top_clkgen_base + 0x124)>>24;
	    val &= ~(0x1<<24); 
	    val |= (0x0&0x1)<<24; 
		iowrite32(val, vin->vin_top_clkgen_base + 0x124);
    } else {
        printk("nne bus clk src is already clk_cpu_axi\n");
    }
	//enable clk
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_SRC_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP0NOC_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISPSLV_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1_AXI_CTRL);
	reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_ISP1NOC_AXI_CTRL);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VIN_AXI);
    reg_set_highest_bit(vin->vin_top_clkgen_base,CLK_VINNOC_AXI);

    vin_rstgen_assert_reset(vin);

	reg_set_highest_bit(vin->clkgen_base,CLK_ISP0_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_ISP0_2X_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_ISP0_MIPI_CTRL);
	
	reg_set_highest_bit(vin->clkgen_base,CLK_ISP1_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_ISP1_2X_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_ISP1_MIPI_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_MIPI_RX0_SYS0_CTRL);
	reg_set_highest_bit(vin->clkgen_base,CLK_MIPI_RX1_SYS1_CTRL);

    isp_rstgen_assert_reset(vin);

    // hold vin resets for sub modules before csi2rx controller get configed
    reg_write(vin->rstgen_base, SOFTWARE_RESET_ASSERT0, 0xffffffff);
    mdelay(10);

    // clear reset for all vin submodules except dphy-rx (follow lunhai's advice)
    reg_write(vin->rstgen_base, SOFTWARE_RESET_ASSERT0, 1<<17);
    return 0;
}

static int vin_parse_dt(struct device *dev, struct stf_vin_dev *vin) 
{
	int ret=0;
	struct device_node *np = dev->of_node;

	if(!np)
		return -EINVAL;

	if (of_property_read_u32(np, "format", &vin->format.format)) {
		dev_err(dev,"Missing format property in the DT.\n");
		ret = -EINVAL;
	}
	dev_info(dev,"vin->format.format = %d ,in the DT.\n",vin->format.format);
	
	if (of_property_read_u32(np, "frame-width", &vin->frame.width)) {
		dev_err(dev,"Missing frame-width property in the DT.\n");
		ret = -EINVAL;
	}

	if (of_property_read_u32(np, "frame-height", &vin->frame.height)) {
		dev_err(dev,"Missing frame.height property in the DT.\n");
		ret = -EINVAL;
	}

	vin->isp0 = of_property_read_bool(np, "isp0_enable");
	vin->isp1 = of_property_read_bool(np, "isp1_enable");

	of_property_read_u8_array(np, "csi-dlane-swaps", vin->csi_fmt.dlane_swap, 4);

	of_property_read_u8_array(np, "csi-dlane-pn-swaps", vin->csi_fmt.dlane_pn_swap, 4);

	of_property_read_u8(np, "csi-clane-swap", &vin->csi_fmt.clane_swap);

	of_property_read_u8(np, "csi-clane-pn-swap", &vin->csi_fmt.clane_pn_swap);

	of_property_read_u32(np, "csi-mipiID", &vin->csi_fmt.mipi_id);
	
	of_property_read_u32(np, "csi-width", &vin->csi_fmt.w); 
	
	of_property_read_u32(np, "csi-height", &vin->csi_fmt.h); 
	
	of_property_read_u32(np, "csi-dt", &vin->csi_fmt.dt);
	
	of_property_read_u32(np, "csi-lane", &vin->csi_fmt.lane);	

	return ret;
}

static int stf_vin_clk_init(struct stf_vin_dev *vin)
{
	int ret=0;
	switch (vin->format.format) {
	case SRC_COLORBAR_VIN_ISP:
	case SRC_DVP_SENSOR_VIN:
	case SRC_DVP_SENSOR_VIN_OV5640:
	case SRC_DVP_SENSOR_VIN_ISP:
		 ret =  vin_clk_reset(vin);
	     ret |= vin_rstgen_assert_reset(vin);
	     ret |= vin_rstgen_clkgen(vin);
	break;

	case SRC_CSI2RX_VIN_ISP:
		 ret =vin_sys_init(vin);
	break;

	default:
		pr_err("unknown format\n");
		return -EINVAL;
	}
	
	return ret;
}

static int stf_vin_probe(struct platform_device *pdev)
{
	struct stf_vin_dev *vin;
	int irq;
	dev_t devid;
	struct class *vin_cls;
	int major = 0;
	int ret = 0;
	struct device_node *node;
	struct resource res_mem;

	dev_info(&pdev->dev, "vin probe enter!\n");

	vin = devm_kzalloc(&pdev->dev, sizeof(struct stf_vin_dev), GFP_KERNEL);
	if (!vin)
		return -ENOMEM;

	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		dev_err(&pdev->dev, "Could not get irq\n");
		return -ENODEV;
	}

	vin->isp0_irq = platform_get_irq(pdev, 1);
	if (vin->isp0_irq <= 0) {
		dev_err(&pdev->dev, "Could not get irq\n");
		return -ENODEV;
	}

	ret = vin_get_mem_res(pdev,vin);
	if (ret) {
			dev_err(&pdev->dev, "Could not map registers\n");
			goto out;
	}

	spin_lock_init(&vin->irqlock);

	vin->dev = &pdev->dev;
	vin->irq = irq;

	vin_irq_disable(vin);

	ret = devm_request_irq(&pdev->dev, vin->irq, vin_wr_irq_handler, 0,
			       "vin_axiwr_irq", vin);
	if (ret) {
		dev_err(&pdev->dev, "failed to request irq\n");
		goto out;
	}

	ret = devm_request_irq(&pdev->dev, vin->isp0_irq, vin_isp0_irq_handler, 0,
			       "vin_isp0_irq", vin);
	if (ret) {
		dev_err(&pdev->dev, "failed to request irq\n");
		goto out;
	}

	node = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
	if (node) {
		of_address_to_resource(node, 0, &res_mem);
		vin->buf.paddr = res_mem.start;
	} else {
		dev_err(&pdev->dev, "Could not get reserved memory\n");
		return -ENOMEM;
	}

	vin->buf.vaddr = devm_ioremap_resource(&pdev->dev, &res_mem);
	memset(vin->buf.vaddr, 0, RESERVED_MEM_SIZE);

	ret = vin_parse_dt(&pdev->dev, vin);
	if (ret)
		goto out;

	vin->condition = false;
	init_waitqueue_head(&vin->wq);

	spin_lock_init(&vin->irqlock);
	platform_set_drvdata(pdev, vin);

	/* Reset device */
	ret = stf_vin_clk_init(vin);
	if (ret) {
		dev_err(&pdev->dev, "Failed to reset device \n");
		goto out;
	}

	/* set the sysctl config */
	ret = stf_vin_config_set(vin);
	if (ret) {
		dev_err(&pdev->dev, "Failed to config device\n");
		goto out;
	}
	
	ret = vin_update_buf_size(vin);
	if (ret)
		goto out;

	vin_irq_enable(vin);
	if (vin->format.format == SRC_DVP_SENSOR_VIN)
		dvp_vin_ddr_addr_config(vin);

	usleep_range(3000, 5000);

	ret = alloc_chrdev_region(&devid, 0, 1, "vin");

	major = MAJOR(devid);
	if (major < 0) {
		dev_err(&pdev->dev, "Failed register chrdev\n");
		ret = major;
	}
	vin->major = major;

	cdev_init(&vin->vin_cdev, &vin_fops);
	cdev_add(&vin->vin_cdev, devid, 1);

	vin_cls = class_create(THIS_MODULE, "vin");
	device_create(vin_cls, NULL, MKDEV(major, 0), NULL, "vin");

	return 0;
out:
	return ret;
}

static int stf_vin_remove(struct platform_device *pdev)
{
	struct stf_vin_dev *vin = platform_get_drvdata(pdev);
	vin_free_buf(vin);

	cdev_del(&vin->vin_cdev);
	unregister_chrdev_region(MKDEV(vin->major, 0), 1);
	dev_info(&pdev->dev, "remove done\n");

	return 0;
}

static const struct of_device_id stf_vin_of_match[] = {
	{.compatible = "starfive,stf-vin"},
	{ /* end node */ },
};

MODULE_DEVICE_TABLE(of, stf_vin_of_match);

static struct platform_driver stf_vin_driver = {
	.probe = stf_vin_probe,
	.remove = stf_vin_remove,
	.driver = {
		   .name = DRV_NAME,
		   .of_match_table = of_match_ptr(stf_vin_of_match),
		   },
};

#if defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_SC2235) || defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_OV5640) || \
    defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_OV4689) || defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_IMX219)
static int __init stf_vin_init(void)
{
	return platform_driver_register(&stf_vin_driver);
}
#endif

static void __exit stf_vin_cleanup(void)
{
	platform_driver_unregister(&stf_vin_driver);
}

#if defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_SC2235) || defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_OV5640)
fs_initcall(stf_vin_init);
#elif defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_OV4689) || defined(CONFIG_VIDEO_STARFIVE_VIN_SENSOR_IMX219)
subsys_initcall(stf_vin_init);
#endif

module_exit(stf_vin_cleanup); 

MODULE_AUTHOR("StarFive Technology Co., Ltd.");
MODULE_DESCRIPTION("Starfive VIC video in driver");
MODULE_LICENSE("GPL");