Merge tag 'v4.4-rc4'
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / rk_camsys / camsys_marvin.c
1 #include "camsys_marvin.h"
2 #include "camsys_soc_priv.h"
3 #include "camsys_gpio.h"
4
5 #include <linux/rockchip/common.h> 
6 #include <dt-bindings/clock/rk_system_status.h>
7 #include <linux/rockchip_ion.h>
8 #include <linux/file.h>
9
10 extern int rockchip_set_system_status(unsigned long status);
11 extern int rockchip_clear_system_status(unsigned long status);
12
13 static const char miscdev_name[] = CAMSYS_MARVIN_DEVNAME;
14
15
16 static int camsys_mrv_iomux_cb(camsys_extdev_t *extdev,void *ptr)
17 {
18     struct pinctrl      *pinctrl;
19     struct pinctrl_state    *state;
20     int retval = 0;
21     char state_str[20] = {0};
22     camsys_dev_t *camsys_dev = (camsys_dev_t*)ptr;
23     struct device *dev = &(extdev->pdev->dev);
24     camsys_soc_priv_t *soc;
25
26     // DVP IO Config
27     
28     if (extdev->phy.type == CamSys_Phy_Cif) {
29
30         switch (extdev->phy.info.cif.fmt)
31         {
32             case CamSys_Fmt_Raw_8b:
33             case CamSys_Fmt_Yuv420_8b:
34             case CamSys_Fmt_Yuv422_8b:
35             {
36                 if (extdev->phy.info.cif.cifio == CamSys_SensorBit0_CifBit0) {
37                     strcpy(state_str,"isp_dvp8bit0");
38                 } else if (extdev->phy.info.cif.cifio == CamSys_SensorBit0_CifBit2) {
39                     strcpy(state_str,"isp_dvp8bit2");
40                 } else if (extdev->phy.info.cif.cifio == CamSys_SensorBit0_CifBit4) {
41                     strcpy(state_str,"isp_dvp8bit4");
42                 }else {
43                     camsys_err("extdev->phy.info.cif.cifio: 0x%x is invalidate!", extdev->phy.info.cif.cifio);
44                     goto fail;
45                 }
46
47                 break;
48             }
49
50             case CamSys_Fmt_Raw_10b:
51             {
52                 strcpy(state_str,"isp_dvp10bit");
53                 break;
54             }
55
56             case CamSys_Fmt_Raw_12b:
57             {
58                 strcpy(state_str,"isp_dvp12bit");
59                 break;
60             }
61
62             default:
63             {
64                 camsys_err("extdev->phy.info.cif.fmt: 0x%x is invalidate!",extdev->phy.info.cif.fmt);
65                 goto fail;
66             }
67         }        
68     } else {
69         if (extdev->dev_cfg & CAMSYS_DEVCFG_FLASHLIGHT) {
70             if (extdev->dev_cfg & CAMSYS_DEVCFG_PREFLASHLIGHT) {
71                 strcpy(state_str,"isp_mipi_fl_prefl");
72             } else {
73                 strcpy(state_str,"isp_mipi_fl");
74             }
75             {
76                 //mux triggerout as gpio
77             //get gpio index
78                 int flash_trigger_io ;
79                 enum of_gpio_flags flags;
80                 flash_trigger_io = of_get_named_gpio_flags(camsys_dev->pdev->dev.of_node, "rockchip,gpios", 0, &flags);
81                 if(gpio_is_valid(flash_trigger_io)){
82                     flash_trigger_io = of_get_named_gpio_flags(camsys_dev->pdev->dev.of_node, "rockchip,gpios", 0, &flags);
83                     gpio_request(flash_trigger_io,"camsys_gpio");
84                     gpio_direction_output(flash_trigger_io, (~(extdev->fl.fl.active) & 0x1));
85                 }
86             }                   
87         } else {
88             strcpy(state_str,"default");
89         }
90     }
91
92     camsys_trace(1,"marvin pinctrl select: %s", state_str);
93     
94     pinctrl = devm_pinctrl_get(dev);
95     if (IS_ERR(pinctrl)) {
96         camsys_err("devm_pinctrl_get failed!");
97         goto fail;
98     }
99     state = pinctrl_lookup_state(pinctrl,
100                          state_str);
101     if (IS_ERR(state)){
102         camsys_err("pinctrl_lookup_state failed!");
103         goto fail;
104     }
105
106     if (!IS_ERR(state)) {
107         retval = pinctrl_select_state(pinctrl, state);
108         if (retval){
109             camsys_err("pinctrl_select_state failed!");
110             goto fail;
111         }
112     }
113
114     if (camsys_dev->soc) {
115         soc = (camsys_soc_priv_t*)camsys_dev->soc;
116         if (soc->soc_cfg) {
117             (soc->soc_cfg)(Cif_IoDomain_Cfg,(void*)&extdev->dovdd.min_uv);
118             (soc->soc_cfg)(Clk_DriverStrength_Cfg,(void*)&extdev->clk.driver_strength);
119         } else {
120             camsys_err("camsys_dev->soc->soc_cfg is NULL!");
121         }
122     } else {
123         camsys_err("camsys_dev->soc is NULL!");
124     }
125     
126     return 0;
127 fail:
128     return -1;
129 }
130
131 static int camsys_mrv_flash_trigger_cb(void *ptr,int mode,unsigned int on)
132 {
133     camsys_dev_t *camsys_dev = (camsys_dev_t*)ptr;
134     struct device *dev = &(camsys_dev->pdev->dev);
135     int flash_trigger_io ;
136     struct pinctrl      *pinctrl;
137     struct pinctrl_state    *state;
138     char state_str[20] = {0};
139     int retval = 0;
140     enum of_gpio_flags flags;
141     camsys_extdev_t *extdev = NULL;
142         
143     if(!on){
144         strcpy(state_str,"isp_flash_as_gpio");
145         pinctrl = devm_pinctrl_get(dev);
146         if (IS_ERR(pinctrl)) {
147             camsys_err("devm_pinctrl_get failed!");
148         }
149         state = pinctrl_lookup_state(pinctrl,
150                              state_str);
151         if (IS_ERR(state)){
152             camsys_err("pinctrl_lookup_state failed!");
153         }
154
155         if (!IS_ERR(state)) {
156             retval = pinctrl_select_state(pinctrl, state);
157             if (retval){
158                 camsys_err("pinctrl_select_state failed!");
159             }
160
161         }
162
163         //get gpio index
164         flash_trigger_io = of_get_named_gpio_flags(camsys_dev->pdev->dev.of_node, "rockchip,gpios", 0, &flags);
165         if(gpio_is_valid(flash_trigger_io)){
166             flash_trigger_io = of_get_named_gpio_flags(camsys_dev->pdev->dev.of_node, "rockchip,gpios", 0, &flags);
167             gpio_request(flash_trigger_io,"camsys_gpio");
168             //get flash io active pol
169             if (!list_empty(&camsys_dev->extdevs.list)) {
170                 list_for_each_entry(extdev, &camsys_dev->extdevs.list, list) {
171                     if (extdev->dev_cfg & CAMSYS_DEVCFG_FLASHLIGHT) {
172                         gpio_direction_output(flash_trigger_io, (~(extdev->fl.fl.active) & 0x1));
173                     }
174                 }
175             }    
176         }
177     }else{
178         strcpy(state_str,"isp_flash_as_trigger_out");
179         pinctrl = devm_pinctrl_get(dev);
180         if (IS_ERR(pinctrl)) {
181             camsys_err("devm_pinctrl_get failed!");
182         }
183         state = pinctrl_lookup_state(pinctrl,
184                              state_str);
185         if (IS_ERR(state)){
186             camsys_err("pinctrl_lookup_state failed!");
187         }
188
189         if (!IS_ERR(state)) {
190             retval = pinctrl_select_state(pinctrl, state);
191             if (retval){
192                 camsys_err("pinctrl_select_state failed!");
193             }
194
195         }
196     }
197     return retval;
198 }
199 static struct device *rockchip_get_sysmmu_device_by_compatible(const char *compt)
200 {
201         struct device_node *dn = NULL;
202         struct platform_device *pd = NULL;
203         struct device *ret = NULL ;
204
205         dn = of_find_compatible_node(NULL,NULL,compt);
206         if(!dn)
207         {
208                 printk("can't find device node %s \r\n",compt);
209                 return NULL;
210         }
211         
212         pd = of_find_device_by_node(dn);
213         if(!pd)
214         {       
215                 printk("can't find platform device in device node %s \r\n",compt);
216                 return  NULL;
217         }
218         ret = &pd->dev;
219         
220         return ret;
221
222 }
223 #ifdef CONFIG_IOMMU_API
224 static inline void platform_set_sysmmu(struct device *iommu, struct device *dev)
225 {
226         dev->archdata.iommu = iommu;
227 }
228 #else
229 static inline void platform_set_sysmmu(struct device *iommu, struct device *dev)
230 {
231 }
232 #endif
233
234
235 static int camsys_mrv_iommu_cb(void *ptr,camsys_sysctrl_t *devctl)
236 {
237     struct device *iommu_dev = NULL,*dev = NULL;
238     struct file *file = NULL;
239     struct ion_client *client = NULL;
240     struct ion_handle *handle = NULL;
241     camsys_iommu_t *iommu = NULL;
242     int ret = 0,iommu_enabled = 0;
243     camsys_dev_t * camsys_dev = (camsys_dev_t *)ptr;
244
245     of_property_read_u32(camsys_dev->pdev->dev.of_node, "rockchip,isp,iommu_enable", &iommu_enabled);
246     if(iommu_enabled != 1){
247         camsys_err("isp iommu have not been enabled!\n");
248         ret = -1;
249         goto iommu_end;
250     }
251     iommu_dev = rockchip_get_sysmmu_device_by_compatible(ISP_IOMMU_COMPATIBLE_NAME);
252     if(!iommu_dev){
253         camsys_err("get iommu device erro!\n");
254         ret = -1;
255         goto iommu_end;
256     }
257     dev = &(camsys_dev->pdev->dev);
258     iommu = (camsys_iommu_t *)(devctl->rev);
259     file = fget(iommu->client_fd);
260     if(!file){
261         camsys_err("get client_fd file erro!\n");
262         ret = -1;
263         goto iommu_end;
264     }
265     
266     client = file->private_data;
267     
268     if(!client){
269         camsys_err("get ion_client erro!\n");
270         ret = -1;
271         goto iommu_end;
272     }
273     
274     fput(file);
275
276     handle = ion_import_dma_buf(client,iommu->map_fd);
277
278     camsys_trace(1,"map fd %d ,client fd %d\n",iommu->map_fd,iommu->client_fd);
279     if(!handle){
280         camsys_err("get ion_handle erro!\n");
281         ret = -1;
282         goto iommu_end;
283     }
284     if(devctl->on){
285         platform_set_sysmmu(iommu_dev,dev);
286         ret = rockchip_iovmm_activate(dev);
287         
288         ret = ion_map_iommu(dev,client,handle,&(iommu->linear_addr),&(iommu->len));
289         
290     }else{
291         ion_unmap_iommu(dev,client,handle);
292         platform_set_sysmmu(iommu_dev,dev);
293         rockchip_iovmm_deactivate(dev);
294     }
295 iommu_end:
296     return ret;
297 }
298 static int camsys_mrv_reset_cb(void *ptr,unsigned int on)
299 {
300     camsys_dev_t *camsys_dev = (camsys_dev_t*)ptr;
301     camsys_soc_priv_t *soc;
302
303
304     if (camsys_dev->soc) {
305         soc = (camsys_soc_priv_t*)camsys_dev->soc;
306         if (soc->soc_cfg) {
307             (soc->soc_cfg)(Isp_SoftRst,(void*)(unsigned long)on);
308         } else {
309             camsys_err("camsys_dev->soc->soc_cfg is NULL!");
310         }
311     } else {
312         camsys_err("camsys_dev->soc is NULL!");
313     }
314     
315     return 0;
316 }
317
318 static int camsys_mrv_clkin_cb(void *ptr, unsigned int on)
319 {
320     camsys_dev_t *camsys_dev = (camsys_dev_t*)ptr;
321     camsys_mrv_clk_t *clk = (camsys_mrv_clk_t*)camsys_dev->clk;
322     unsigned long isp_clk;
323         
324     if (on && !clk->in_on) {
325                 rockchip_set_system_status(SYS_STATUS_ISP);
326
327                 if (on == 1) {
328                     isp_clk = 210000000;           
329                 } else {
330                     isp_clk = 420000000;            
331                 }
332
333                 clk_set_rate(clk->isp,isp_clk);
334         clk_set_rate(clk->isp_jpe, isp_clk);
335
336                 clk_prepare_enable(clk->pd_isp);
337         clk_prepare_enable(clk->aclk_isp);
338         clk_prepare_enable(clk->hclk_isp);
339         clk_prepare_enable(clk->isp);
340         clk_prepare_enable(clk->isp_jpe);
341         clk_prepare_enable(clk->pclkin_isp); 
342                 if(CHIP_TYPE == 3368){
343
344                         clk_prepare_enable(clk->cif_clk_out);
345                         clk_prepare_enable(clk->pclk_dphyrx);
346                         clk_prepare_enable(clk->clk_vio0_noc);
347                 }else{
348                         clk_prepare_enable(clk->clk_mipi_24m);          
349                 }
350         clk->in_on = true;
351
352         camsys_trace(1, "%s clock(f: %ld Hz) in turn on",dev_name(camsys_dev->miscdev.this_device),isp_clk);
353         camsys_mrv_reset_cb(ptr,1);
354         udelay(100);
355         camsys_mrv_reset_cb(ptr,0);
356         
357     } else if (!on && clk->in_on) {
358
359         clk_disable_unprepare(clk->aclk_isp);
360         clk_disable_unprepare(clk->hclk_isp);
361         clk_disable_unprepare(clk->isp);
362         clk_disable_unprepare(clk->isp_jpe);
363         clk_disable_unprepare(clk->pclkin_isp);
364                 if(CHIP_TYPE == 3368){
365                 clk_disable_unprepare(clk->cif_clk_out);
366                         clk_disable_unprepare(clk->pclk_dphyrx);
367                         clk_disable_unprepare(clk->clk_vio0_noc);
368
369                 }else{
370                 clk_disable_unprepare(clk->clk_mipi_24m); 
371                 }
372                 clk_disable_unprepare(clk->pd_isp);
373
374                 rockchip_clear_system_status(SYS_STATUS_ISP);
375         clk->in_on = false;
376         camsys_trace(1, "%s clock in turn off",dev_name(camsys_dev->miscdev.this_device));
377     }
378     
379     return 0;
380 }
381 static int camsys_mrv_clkout_cb(void *ptr, unsigned int on,unsigned int inclk)
382 {
383     camsys_dev_t *camsys_dev = (camsys_dev_t*)ptr;
384     camsys_mrv_clk_t *clk = (camsys_mrv_clk_t*)camsys_dev->clk;
385     
386     mutex_lock(&clk->lock);
387     if (on && (clk->out_on != on)) {  
388
389         clk_set_rate(clk->cif_clk_out,inclk);
390         clk_prepare_enable(clk->cif_clk_out);
391         
392                 clk->out_on = on;
393         camsys_trace(1, "%s clock out(rate: %dHz) turn on",dev_name(camsys_dev->miscdev.this_device),
394                     inclk);
395     } else if (!on && clk->out_on) {
396         if(!IS_ERR_OR_NULL(clk->cif_clk_pll)) {
397             clk_set_parent(clk->cif_clk_out, clk->cif_clk_pll);
398         } else {
399             camsys_warn("%s clock out may be not off!", dev_name(camsys_dev->miscdev.this_device));
400         }
401
402         clk_disable_unprepare( clk->cif_clk_out);
403
404         clk->out_on = 0;
405
406         camsys_trace(1, "%s clock out turn off",dev_name(camsys_dev->miscdev.this_device));
407     }
408     mutex_unlock(&clk->lock);    
409
410     return 0;
411 }
412 static irqreturn_t camsys_mrv_irq(int irq, void *data)
413 {
414     camsys_dev_t *camsys_dev = (camsys_dev_t*)data;
415     camsys_irqstas_t *irqsta;
416     camsys_irqpool_t *irqpool;
417     unsigned int isp_mis,mipi_mis,mi_mis,*mis,jpg_mis,jpg_err_mis;
418         
419         unsigned int mi_ris,mi_imis;
420
421     isp_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_ISP_MIS));
422     mipi_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MIPI_MIS));
423     jpg_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_JPG_MIS));
424     jpg_err_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_JPG_ERR_MIS));
425         mi_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_MIS));
426 #if 1   
427         mi_ris =  __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_RIS));
428         mi_imis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_IMIS));
429         while((mi_ris & mi_imis) != mi_mis){
430                 camsys_trace(2,"mi_mis status erro,mi_mis 0x%x,mi_ris 0x%x,imis 0x%x\n",mi_mis,mi_ris,mi_imis);
431                 mi_mis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_MIS));
432                 mi_ris =  __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_RIS));
433         mi_imis = __raw_readl((void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_IMIS));
434         }
435
436 #endif
437
438     __raw_writel(isp_mis, (void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_ISP_ICR)); 
439     __raw_writel(mipi_mis, (void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MIPI_ICR)); 
440     __raw_writel(jpg_mis, (void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_JPG_ICR));
441     __raw_writel(jpg_err_mis, (void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_JPG_ERR_ICR));
442         __raw_writel(mi_mis, (void volatile *)(camsys_dev->devmems.registermem->vir_base + MRV_MI_ICR)); 
443
444     spin_lock(&camsys_dev->irq.lock);
445     if (!list_empty(&camsys_dev->irq.irq_pool)) {
446         list_for_each_entry(irqpool, &camsys_dev->irq.irq_pool, list) {
447             if (irqpool->pid != 0) {
448                 switch(irqpool->mis)
449                 {
450                     case MRV_ISP_MIS:
451                     {
452                         mis = &isp_mis;
453                         break;
454                     }
455
456                     case MRV_MIPI_MIS:
457                     {
458                         mis = &mipi_mis;
459                         break;
460                     }
461                     case MRV_MI_MIS:
462                     {
463                         mis = &mi_mis;
464                         break;
465                     }
466
467                     case MRV_JPG_MIS:
468                     {
469                         mis = &jpg_mis;
470                         break;
471                     }
472
473                     case MRV_JPG_ERR_MIS:
474                     {
475                         mis = &jpg_err_mis;
476                         break;
477                     }
478
479                     default:     
480                     {
481                         camsys_trace(2,"Thread(pid:%d) irqpool mis(%d) is invalidate",irqpool->pid,irqpool->mis);
482                         goto end;
483                     }
484                 }
485
486                 if (*mis != 0) {
487                     spin_lock(&irqpool->lock);
488                     if (!list_empty(&irqpool->deactive)) {
489                         irqsta = list_first_entry(&irqpool->deactive, camsys_irqstas_t, list);
490                         irqsta->sta.mis = *mis;                                                 
491                         list_del_init(&irqsta->list);            
492                         list_add_tail(&irqsta->list,&irqpool->active);                        
493                         wake_up(&irqpool->done);
494                     }
495                     spin_unlock(&irqpool->lock);
496                 }
497             }
498         }
499     }
500 end:    
501     spin_unlock(&camsys_dev->irq.lock);
502
503     return IRQ_HANDLED;
504 }
505 static int camsys_mrv_remove_cb(struct platform_device *pdev)
506 {
507     camsys_dev_t *camsys_dev = platform_get_drvdata(pdev);
508     camsys_mrv_clk_t *mrv_clk=NULL;
509
510     if (camsys_dev->clk != NULL) {
511
512         mrv_clk = (camsys_mrv_clk_t*)camsys_dev->clk;
513         if (mrv_clk->out_on)
514             camsys_mrv_clkout_cb(mrv_clk,0,0);
515         if (mrv_clk->in_on)
516             camsys_mrv_clkin_cb(mrv_clk,0);
517     
518         if (!IS_ERR_OR_NULL(mrv_clk->pd_isp)) {
519                         devm_clk_put(&pdev->dev,mrv_clk->pd_isp);
520         }
521         if (!IS_ERR_OR_NULL(mrv_clk->aclk_isp)) {
522             devm_clk_put(&pdev->dev,mrv_clk->aclk_isp);
523         }
524         if (!IS_ERR_OR_NULL(mrv_clk->hclk_isp)) {
525             devm_clk_put(&pdev->dev,mrv_clk->hclk_isp);
526         }
527         if (!IS_ERR_OR_NULL(mrv_clk->isp)) {
528             devm_clk_put(&pdev->dev,mrv_clk->isp);
529         }
530         if (!IS_ERR_OR_NULL(mrv_clk->isp_jpe)) {
531             devm_clk_put(&pdev->dev,mrv_clk->isp_jpe);
532         }
533         if (!IS_ERR_OR_NULL(mrv_clk->pclkin_isp)) {
534             devm_clk_put(&pdev->dev,mrv_clk->pclkin_isp);
535         }
536         if (!IS_ERR_OR_NULL(mrv_clk->cif_clk_out)) {
537             devm_clk_put(&pdev->dev,mrv_clk->cif_clk_out);
538         }
539         if (!IS_ERR_OR_NULL(mrv_clk->clk_vio0_noc)) {
540             devm_clk_put(&pdev->dev,mrv_clk->clk_vio0_noc);
541         }
542
543         kfree(mrv_clk);
544         mrv_clk = NULL;
545     }
546
547     return 0;
548 }
549 int camsys_mrv_probe_cb(struct platform_device *pdev, camsys_dev_t *camsys_dev)
550 {
551     int err = 0;   
552     camsys_mrv_clk_t *mrv_clk=NULL;
553     
554         err = request_irq(camsys_dev->irq.irq_id, camsys_mrv_irq, IRQF_SHARED, CAMSYS_MARVIN_IRQNAME,camsys_dev);
555     if (err) {
556         camsys_err("request irq for %s failed",CAMSYS_MARVIN_IRQNAME);
557         goto end;
558     }
559
560     //Clk and Iomux init
561     mrv_clk = kzalloc(sizeof(camsys_mrv_clk_t),GFP_KERNEL);
562     if (mrv_clk == NULL) {
563         camsys_err("Allocate camsys_mrv_clk_t failed!");
564         err = -EINVAL;
565         goto clk_failed;
566     }
567     if(CHIP_TYPE == 3368){
568             mrv_clk->pd_isp = devm_clk_get(&pdev->dev, "pd_isp");
569             mrv_clk->aclk_isp = devm_clk_get(&pdev->dev, "aclk_isp");
570             mrv_clk->hclk_isp = devm_clk_get(&pdev->dev, "hclk_isp");
571             mrv_clk->isp = devm_clk_get(&pdev->dev, "clk_isp");
572             mrv_clk->isp_jpe = devm_clk_get(&pdev->dev, "clk_isp_jpe");
573             mrv_clk->pclkin_isp = devm_clk_get(&pdev->dev, "pclkin_isp");
574             mrv_clk->cif_clk_out = devm_clk_get(&pdev->dev, "clk_cif_out");
575             mrv_clk->cif_clk_pll = devm_clk_get(&pdev->dev, "clk_cif_pll");
576             mrv_clk->pclk_dphyrx = devm_clk_get(&pdev->dev, "pclk_dphyrx");    
577             mrv_clk->clk_vio0_noc = devm_clk_get(&pdev->dev, "clk_vio0_noc");
578
579                 if (IS_ERR_OR_NULL(mrv_clk->aclk_isp) || IS_ERR_OR_NULL(mrv_clk->hclk_isp) ||
580                 IS_ERR_OR_NULL(mrv_clk->isp) || IS_ERR_OR_NULL(mrv_clk->isp_jpe) || IS_ERR_OR_NULL(mrv_clk->pclkin_isp) || 
581                 IS_ERR_OR_NULL(mrv_clk->cif_clk_out) || IS_ERR_OR_NULL(mrv_clk->pclk_dphyrx)||
582                 IS_ERR_OR_NULL(mrv_clk->pd_isp) || IS_ERR_OR_NULL(mrv_clk->clk_vio0_noc)) {
583                 camsys_err("Get %s clock resouce failed!\n",miscdev_name);
584                 err = -EINVAL;
585                 goto clk_failed;
586             }
587     }else{
588             mrv_clk->pd_isp = devm_clk_get(&pdev->dev, "pd_isp");
589             mrv_clk->aclk_isp = devm_clk_get(&pdev->dev, "aclk_isp");
590             mrv_clk->hclk_isp = devm_clk_get(&pdev->dev, "hclk_isp");
591             mrv_clk->isp = devm_clk_get(&pdev->dev, "clk_isp");
592             mrv_clk->isp_jpe = devm_clk_get(&pdev->dev, "clk_isp_jpe");
593             mrv_clk->pclkin_isp = devm_clk_get(&pdev->dev, "pclkin_isp");
594             mrv_clk->cif_clk_out = devm_clk_get(&pdev->dev, "clk_cif_out");
595             mrv_clk->cif_clk_pll = devm_clk_get(&pdev->dev, "clk_cif_pll");
596             mrv_clk->clk_mipi_24m = devm_clk_get(&pdev->dev,"clk_mipi_24m"); 
597             
598                 if (IS_ERR_OR_NULL(mrv_clk->pd_isp) || IS_ERR_OR_NULL(mrv_clk->aclk_isp) || IS_ERR_OR_NULL(mrv_clk->hclk_isp) ||
599                 IS_ERR_OR_NULL(mrv_clk->isp) || IS_ERR_OR_NULL(mrv_clk->isp_jpe) || IS_ERR_OR_NULL(mrv_clk->pclkin_isp) || 
600                 IS_ERR_OR_NULL(mrv_clk->cif_clk_out) || IS_ERR_OR_NULL(mrv_clk->clk_mipi_24m)) {
601                 camsys_err("Get %s clock resouce failed!\n",miscdev_name);
602                 err = -EINVAL;
603                 goto clk_failed;
604             }
605         }
606     clk_set_rate(mrv_clk->isp,210000000);
607     clk_set_rate(mrv_clk->isp_jpe, 210000000);
608     
609     mutex_init(&mrv_clk->lock);
610     
611     mrv_clk->in_on = false;
612     mrv_clk->out_on = 0;
613         
614     camsys_dev->clk = (void*)mrv_clk;
615     camsys_dev->clkin_cb = camsys_mrv_clkin_cb;
616     camsys_dev->clkout_cb = camsys_mrv_clkout_cb;
617     camsys_dev->reset_cb = camsys_mrv_reset_cb;
618     camsys_dev->iomux = camsys_mrv_iomux_cb;
619     camsys_dev->flash_trigger_cb = camsys_mrv_flash_trigger_cb;
620     camsys_dev->iommu_cb = camsys_mrv_iommu_cb;
621     
622     camsys_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
623     camsys_dev->miscdev.name = miscdev_name;
624     camsys_dev->miscdev.nodename = miscdev_name;
625     camsys_dev->miscdev.fops = &camsys_fops;
626
627     err = misc_register(&camsys_dev->miscdev);
628     if (err < 0) {
629         camsys_err("misc register %s failed!",miscdev_name);
630         goto misc_register_failed;
631     }   
632
633     //Variable init
634     camsys_dev->dev_id = CAMSYS_DEVID_MARVIN;
635     camsys_dev->platform_remove = camsys_mrv_remove_cb;
636    
637          
638     return 0;
639 misc_register_failed:
640     if (!IS_ERR_OR_NULL(camsys_dev->miscdev.this_device)) {
641         misc_deregister(&camsys_dev->miscdev);
642     }
643
644 clk_failed:
645     if (mrv_clk != NULL) {
646         if (!IS_ERR_OR_NULL(mrv_clk->pd_isp)) {
647             clk_put(mrv_clk->pd_isp);
648         }
649         if (!IS_ERR_OR_NULL(mrv_clk->aclk_isp)) {
650             clk_put(mrv_clk->aclk_isp);
651         }
652         if (!IS_ERR_OR_NULL(mrv_clk->hclk_isp)) {
653             clk_put(mrv_clk->hclk_isp);
654         }
655         if (!IS_ERR_OR_NULL(mrv_clk->isp)) {
656             clk_put(mrv_clk->isp);
657         }
658         if (!IS_ERR_OR_NULL(mrv_clk->isp_jpe)) {
659             clk_put(mrv_clk->isp_jpe);
660         }
661         if (!IS_ERR_OR_NULL(mrv_clk->pclkin_isp)) {
662             clk_put(mrv_clk->pclkin_isp);
663         }
664         if (!IS_ERR_OR_NULL(mrv_clk->cif_clk_out)) {
665             clk_put(mrv_clk->cif_clk_out);
666         }
667                 if(CHIP_TYPE == 3368){
668                 if (!IS_ERR_OR_NULL(mrv_clk->pclk_dphyrx)) {
669                     clk_put(mrv_clk->pclk_dphyrx);
670                 }
671                 if (!IS_ERR_OR_NULL(mrv_clk->clk_vio0_noc)) {
672                         clk_put(mrv_clk->clk_vio0_noc);
673                 }
674                 }
675
676         kfree(mrv_clk);
677         mrv_clk = NULL;
678     }
679     
680 end:
681     return err;
682 }
683 EXPORT_SYMBOL_GPL(camsys_mrv_probe_cb);
684