xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/nvp6158_drv/nvp6158_dev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /********************************************************************************
3 *
4 *  Copyright (C) 2017 	NEXTCHIP Inc. All rights reserved.
5 *  Copyright (c) 2021 	Rockchip Electronics Co. Ltd.All rights reserved.
6 *  Module		: install driver main
7 *  Description	: driver main
8 *  Author		:
9 *  Date         :
10 *  Version		: Version 2.0
11 *
12 ********************************************************************************
13 *  History      :
14 *
15 *
16 ********************************************************************************/
17 #include <linux/kernel.h>
18 #include <linux/version.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/mm.h>
24 #include <linux/proc_fs.h>
25 
26 #ifndef CONFIG_HISI_SNAPSHOT_BOOT
27 #include <linux/miscdevice.h>
28 #endif
29 
30 #include <linux/fs.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/uaccess.h>
34 #include <asm/io.h>
35 //#include <asm/system.h>
36 #include <linux/interrupt.h>
37 #include <linux/ioport.h>
38 #include <linux/string.h>
39 #include <linux/list.h>
40 #include <asm/delay.h>
41 #include <linux/timer.h>
42 #include <linux/delay.h>
43 #include <linux/proc_fs.h>
44 #include <linux/poll.h>
45 #include <asm/bitops.h>
46 #include <linux/uaccess.h>
47 #include <asm/irq.h>
48 #include <linux/moduleparam.h>
49 #include <linux/ioport.h>
50 #include <linux/interrupt.h>
51 #include <linux/semaphore.h>
52 #include <linux/kthread.h>
53 
54 #include <linux/i2c.h>
55 #include <linux/i2c-dev.h>
56 
57 //#include "gpio_i2c.h"
58 #include "nvp6158_video.h"
59 #include "nvp6158_coax_protocol.h"
60 #include "nvp6158_motion.h"
61 #include "nvp6158_common.h"
62 #include "nvp6158_audio.h"
63 #include "nvp6158_video_auto_detect.h"
64 //#include "acp_firmup.h"
65 #include "nvp6158_video_eq.h"
66 #include "nvp6158_drv.h"
67 
68 unsigned int nvp6158_mode = PAL;  //0:ntsc, 1: pal
69 unsigned int nvp6158_kthread = 0;
70 
71 module_param(nvp6158_mode, uint, S_IRUGO);
72 
73 module_param(nvp6158_kthread, uint, S_IRUGO);
74 #ifdef CONFIG_HISI_SNAPSHOT_BOOT
75 static struct himedia_device s_stNvp6158Device;
76 #endif
77 
78 #ifdef CONFIG_HISI_SNAPSHOT_BOOT
nvp6158_freeze(struct himedia_device * pdev)79 static int nvp6158_freeze(struct himedia_device* pdev)
80 {
81     printk(KERN_ALERT "%s  %d\n", __FUNCTION__, __LINE__);
82     return 0;
83 }
84 
nvp6158_restore(struct himedia_device * pdev)85 static int nvp6158_restore(struct himedia_device* pdev)
86 {
87     printk(KERN_ALERT "%s  %d\n", __FUNCTION__, __LINE__);
88     return 0;
89 }
90 #endif
91 
92 static const struct file_operations nvp6158_fops = {
93 	.owner      = THIS_MODULE,
94     	.unlocked_ioctl	= nvp6158_native_ioctl,
95 	.open       = nvp6158_open,
96 	.release    = nvp6158_close,
97 };
98 
99 #ifdef CONFIG_HISI_SNAPSHOT_BOOT
100 struct himedia_ops stNvp6158DrvOps =
101 {
102     .pm_freeze = nvp6158_freeze,
103     .pm_restore  = nvp6158_restore
104 };
105 #endif
106 
107 /*******************************************************************************
108 *	Description		: kernel thread for EQ (now, not used)
109 *	Argurments		: void
110 *	Return value	: 0
111 *	Modify			:
112 *	warning			:
113 *******************************************************************************/
114 #ifdef STREAM_ON_DEFLAULT
nvp6158_kernel_thread(void * data)115 static int nvp6158_kernel_thread(void *data)
116 {
117 	//int ch;
118 	//nvp6158_input_videofmt videofmt;
119 	NVP6158_INFORMATION_S s_nvp6158_vfmts;
120 	video_equalizer_info_s s_eq_info;
121 	unsigned char prefmt=0, curfmt=0, chvloss=0;
122 	unsigned char ch = 0;
123 
124 	memset(&s_nvp6158_vfmts, 0, sizeof(NVP6158_INFORMATION_S));
125 
126 	while(!kthread_should_stop()) {
127 		#if 1  //standard rutine of a process
128 		down(&nvp6158_lock);
129 		ch = ch % (nvp6158_cnt*4);
130 		nvp6158_getvideoloss();
131 		if(nvp6158_chip_id[0]==NVP6158C_R0_ID || nvp6158_chip_id[0]==NVP6158_R0_ID) {
132 			nvp6158_video_fmt_det(ch, &s_nvp6158_vfmts);
133 			curfmt = s_nvp6158_vfmts.curvideofmt[ch];
134 			prefmt = s_nvp6158_vfmts.prevideofmt[ch];
135 			chvloss = s_nvp6158_vfmts.curvideoloss[ch];
136 			//printk(">>>>>>%s CH[%d] chvloss = %d curfmt = %x prefmt = %x\n", __func__, ch, chvloss, curfmt, prefmt);
137 
138 			if(chvloss == 0x00) {
139 				if(nvp6158_ch_mode_status[ch] != prefmt) {
140 					nvp6158_set_chnmode(ch, curfmt);
141 					nvp6158_set_portmode(0, ch%4, NVP6158_OUTMODE_1MUX_FHD, ch%4);
142 					s_eq_info.Ch = ch%4;
143 					s_eq_info.devnum = ch/4;
144 					s_eq_info.FmtDef = curfmt;
145 					nvp6158_get_eq_dist(&s_eq_info);
146 					s_nvp6158_vfmts.prevideofmt[ch] = curfmt;
147 					printk(">>>>>>%s CH[%d] s_eq_info.distance = %d\n",
148 						__func__, ch, s_eq_info.distance);
149 					nvp6158_set_equalizer(&s_eq_info);
150 
151 				}
152 			} else {
153 				if(nvp6158_ch_mode_status[ch] != NC_VIVO_CH_FORMATDEF_UNKNOWN) {
154 					nvp6158_set_chnmode(ch, NC_VIVO_CH_FORMATDEF_UNKNOWN);
155 					nvp6158_set_portmode(0, ch%4, NVP6158_OUTMODE_1MUX_FHD, ch%4);
156 				}
157 			}
158 		} else {
159 			nvp6168_video_fmt_det(ch, &s_nvp6158_vfmts);
160 			curfmt = s_nvp6158_vfmts.curvideofmt[ch];
161 			prefmt = s_nvp6158_vfmts.prevideofmt[ch];
162 			chvloss = s_nvp6158_vfmts.curvideoloss[ch];
163 			//printk(">>>>>>%s CH[%d] chvloss = %d curfmt = %x prefmt = %x nvp6158_ch_mode_status[%d]=%x\n", __func__, ch, chvloss, curfmt, prefmt, ch, nvp6158_ch_mode_status[ch]);
164 
165 			if(chvloss == 0x00) {
166 				if(nvp6158_ch_mode_status[ch] != prefmt) {
167 					nvp6168_set_chnmode(ch, curfmt);
168 					nvp6158_set_portmode(0, ch%4, NVP6158_OUTMODE_1MUX_FHD, ch%4);
169 					s_eq_info.Ch = ch%4;
170 					s_eq_info.devnum = ch/4;
171 					s_eq_info.FmtDef = curfmt;
172 					nvp6158_get_eq_dist(&s_eq_info);
173 					s_nvp6158_vfmts.prevideofmt[ch] = curfmt;
174 					printk(">>>>>>%s CH[%d] s_eq_info.distance = %d\n", __func__, ch, s_eq_info.distance);
175 					nvp6168_set_equalizer(&s_eq_info);
176 				}
177 			} else {
178 				if(nvp6158_ch_mode_status[ch] != NC_VIVO_CH_FORMATDEF_UNKNOWN) {
179 					nvp6168_set_chnmode(ch, NC_VIVO_CH_FORMATDEF_UNKNOWN);
180 					nvp6158_set_portmode(0, ch%4, NVP6158_OUTMODE_1MUX_FHD, ch%4);
181 				}
182 			}
183 		}
184 		ch ++;
185 		up(&nvp6158_lock);
186 		#endif
187 		schedule_timeout_interruptible(msecs_to_jiffies(200));
188 		//printk("nvp6158_kernel_thread running\n");
189 	}
190 
191 	return 0;
192 }
193 #endif
194 
195 /*******************************************************************************
196 *	Description		: It is called when "insmod nvp61XX_ex.ko" command run
197 *	Argurments		: void
198 *	Return value	: -1(could not register nvp61XX device), 0(success)
199 *	Modify			:
200 *	warning			:
201 *******************************************************************************/
202 struct proc_dir_entry *nvp6158_dir;
203 #define NVP6158_PROC_ENTRY "nvp6158"
204 
205 #ifndef CONFIG_VIDEO_NVP6158
206 
207 #ifdef STREAM_ON_DEFLAULT
208 static struct task_struct *nvp6158_kt = NULL;
209 #endif
210 
211 #ifndef CONFIG_HISI_SNAPSHOT_BOOT
212 static struct miscdevice nvp6158_dev = {
213 	.minor		= MISC_DYNAMIC_MINOR,
214 	.name		= "nc_vdec",
215 	.fops  		= &nvp6158_fops,
216 };
217 #endif
218 
nvp6158_module_init(void)219 static int __init nvp6158_module_init(void)
220 {
221 	int ret = 0;
222 	//char entry[20];
223 #ifdef CONFIG_HISI_SNAPSHOT_BOOT
224 	snprintf(s_stNvp6158Device.devfs_name, sizeof(s_stNvp6158Device.devfs_name), DEV_NAME);
225 	s_stNvp6158Device.minor  = HIMEDIA_DYNAMIC_MINOR;
226 	s_stNvp6158Device.fops   = &nvp6158_fops;
227 	s_stNvp6158Device.drvops = &stNvp6158DrvOps;
228 	s_stNvp6158Device.owner  = THIS_MODULE;
229 
230 	ret = himedia_register(&s_stNvp6158Device);
231 	if (ret) {
232 		printk(0, "could not register nvp6158_dev device");
233 		return -1;
234  	}
235 #else
236 	ret = misc_register(&nvp6158_dev);
237    	if (ret) {
238 		printk("ERROR: could not register nvp6158_dev devices:%#x \n",ret);
239         	return -1;
240 	}
241 #endif
242 
243 #ifdef STREAM_ON_DEFLAULT
244 	//printk("NVP6158/68(C) ex Driver %s COMPILE TIME[%s %s]\n", NVP6158_DRIVER_VER, __DATE__,__TIME__);
245 	nvp6158_init(I2C_1);
246 	if (ret) {
247 		printk(KERN_ERR "ERROR: jaguar1 init failed\n");
248 		return -1;
249 	}
250 	down(&nvp6158_lock);
251 	nvp6158_video_decoder_init();
252 	/* initialize Audio
253 	 * recmaster, pbmaster, ch_num, samplerate, bits */
254 	if(nvp6158_chip_id[0] == NVP6158C_R0_ID || nvp6158_chip_id[0] == NVP6158_R0_ID)
255 		nvp6158_audio_init(1, 0, 16, 0, 0);
256 	else
257 		nvp6168_audio_init(1, 0, 16, 0, 0);
258 	//VDO_1/2 enable, VCLK_x ebable
259 	gpio_i2c_write(0x60, 0xFF, 0x01);
260 	gpio_i2c_write(0x60, 0xCA, 0x66);
261 	up(&nvp6158_lock);
262 
263 	/* create kernel thread for EQ, But Now not used. */
264 	if(nvp6158_kthread == 1) {
265 		nvp6158_kt = kthread_create(nvp6158_kernel_thread, NULL, "nvp6158_kt");
266 	    	if(!IS_ERR(nvp6158_kt))
267 	       		wake_up_process(nvp6158_kt);
268 	    	else {
269 	        	printk("create nvp6158 watchdog thread failed!!\n");
270 	        	nvp6158_kt = 0;
271 	        	return 0;
272 	    	}
273 	}
274 #endif
275 	return 0;
276 }
277 
278 /*******************************************************************************
279 *	Description		: It is called when "rmmod nvp61XX_ex.ko" command run
280 *	Argurments		: void
281 *	Return value	: void
282 *	Modify			:
283 *	warning			:
284 *******************************************************************************/
nvp6158_module_exit(void)285 static void __exit nvp6158_module_exit(void)
286 {
287 #ifdef STREAM_ON_DEFLAULT
288 	if(nvp6158_kt)
289 		kthread_stop(nvp6158_kt);
290 #endif
291 #ifdef CONFIG_HISI_SNAPSHOT_BOOT
292 	himedia_unregister(&s_stNvp6158Device);
293 #else
294 	misc_deregister(&nvp6158_dev);
295 #endif
296 	nvp6158_i2c_client_exit();
297 	//printk("NVP6158(C) ex Driver %s COMPILE TIME[%s %s] removed\n", NVP6158_DRIVER_VER, __DATE__,__TIME__);
298 }
299 
300 module_init(nvp6158_module_init);
301 module_exit(nvp6158_module_exit);
302 #endif
303 
304 MODULE_LICENSE("GPL");
305 
306 /*******************************************************************************
307 *	End of file
308 *******************************************************************************/
309 
310