camera: fix camera resume may be fail
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / rk29_camera_oneframe.c
1 /*
2  * V4L2 Driver for RK28 camera host
3  *
4  * Copyright (C) 2006, Sascha Hauer, Pengutronix
5  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/io.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/errno.h>
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/moduleparam.h>
25 #include <linux/time.h>
26 #include <linux/clk.h>
27 #include <linux/version.h>
28 #include <linux/device.h>
29 #include <linux/platform_device.h>
30 #include <linux/mutex.h>
31 #include <linux/videodev2.h>
32 #include <mach/rk29_camera.h>
33 #include <mach/rk29_iomap.h>
34 #include <mach/iomux.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-dev.h>
37 #include <media/videobuf-dma-contig.h>
38 #include <media/soc_camera.h>
39 #include <mach/rk29-ipp.h>
40
41
42 static int debug;
43 module_param(debug, int, S_IRUGO|S_IWUSR);
44
45 #define dprintk(level, fmt, arg...) do {                        \
46         if (debug >= level)                                     \
47         printk(KERN_WARNING"rk29xx_camera: " fmt , ## arg); } while (0)
48
49 #define RK29CAMERA_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
50 #define RK29CAMERA_DG(format, ...) dprintk(0, format, ## __VA_ARGS__)
51
52 // VIP Reg Offset
53 #define RK29_VIP_AHBR_CTRL                0x00
54 #define RK29_VIP_INT_MASK                 0x04
55 #define RK29_VIP_INT_STS                  0x08
56 #define RK29_VIP_STS                      0x0c
57 #define RK29_VIP_CTRL                     0x10
58 #define RK29_VIP_CAPTURE_F1SA_Y           0x14
59 #define RK29_VIP_CAPTURE_F1SA_UV          0x18
60 #define RK29_VIP_CAPTURE_F1SA_Cr          0x1c
61 #define RK29_VIP_CAPTURE_F2SA_Y           0x20
62 #define RK29_VIP_CAPTURE_F2SA_UV          0x24
63 #define RK29_VIP_CAPTURE_F2SA_Cr          0x28
64 #define RK29_VIP_FB_SR                    0x2c
65 #define RK29_VIP_FS                       0x30
66 #define RK29_VIP_VIPRESERVED              0x34
67 #define RK29_VIP_CROP                     0x38
68 #define RK29_VIP_CRM                      0x3c
69 #define RK29_VIP_RESET                    0x40
70 #define RK29_VIP_L_SFT                    0x44
71
72 //The key register bit descrition
73 // VIP_CTRL Reg
74 #define  DISABLE_CAPTURE              (0x00<<0)
75 #define  ENABLE_CAPTURE               (0x01<<0)
76 #define  HSY_HIGH_ACTIVE              (0x00<<1)
77 #define  HSY_LOW_ACTIVE               (0x01<<1)
78 #define  VIP_CCIR656                  (0x00<<2)
79 #define  VIP_SENSOR                   (0x01<<2)
80 #define  SENSOR_UYVY                  (0x00<<3)
81 #define  SENSOR_YUYV                  (0x01<<3)
82 #define  VIP_YUV                      (0x00<<4)
83 #define  VIP_RAW                      (0x01<<4)
84 #define  CON_OR_PIN                   (0x00<<5)
85 #define  ONEFRAME                     (0x01<<5)
86 #define  VIPREGYUV420                 (0x00<<6)
87 #define  VIPREGYUV422                 (0x01<<6)
88 #define  FIELD0_START                 (0x00<<7)
89 #define  FIELD1_START                 (0x01<<7)
90 #define  CONTINUOUS                   (0x00<<8)
91 #define  PING_PONG                    (0x01<<8)
92 #define  POSITIVE_EDGE                (0x00<<9)
93 #define  NEGATIVE_EDGE                (0x01<<9)
94 #define  VIPREGNTSC                   (0x00<<10)
95 #define  VIPREGPAL                    (0x01<<10)
96 #define  VIP_DATA_LITTLEEND           (0x00<<11)
97 #define  VIP_DATA_BIGEND              (0x01<<11)
98 #define  VSY_LOW_ACTIVE               (0x00<<12)
99 #define  VSY_HIGH_ACTIVE              (0x01<<12)
100 #define  VIP_RAWINPUT_BYPASS          (0x00<<13)
101 #define  VIP_RAWINPUT_POSITIVE_EDGE   (0x01<<13)
102 #define  VIP_RAWINPUT_NEGATIVE_EDGE   (0x02<<13)
103
104 // GRF_SOC_CON0 Reg
105 #define  GRF_SOC_CON0_Reg             0xbc
106 #define  VIP_AXIMASTER                (0x00<<0)
107 #define  VIP_AHBMASTER                (0x01<<2)
108
109 // GRF_OS_REG0
110 #define  GRF_OS_REG0                  0xd0
111 #define  VIP_ACLK_DIV_HCLK_1          (0x00<<0)
112 #define  VIP_ACLK_DIV_HCLK_2          (0x01<<0)
113
114
115 #define MIN(x,y)   ((x<y) ? x: y)
116 #define MAX(x,y)    ((x>y) ? x: y)
117 #define RK29_SENSOR_24MHZ      24           /* MHz */
118 #define RK29_SENSOR_48MHZ      48
119
120 #define write_vip_reg(addr, val)  __raw_writel(val, addr+(rk29_camdev_info_ptr->base))
121 #define read_vip_reg(addr) __raw_readl(addr+(rk29_camdev_info_ptr->base))
122 #define mask_vip_reg(addr, msk, val)    write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
123
124 #define write_grf_reg(addr, val)  __raw_writel(val, addr+RK29_GRF_BASE)
125 #define read_grf_reg(addr) __raw_readl(addr+RK29_GRF_BASE)
126 #define mask_grf_reg(addr, msk, val)    write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
127
128 //Configure Macro
129 #define RK29_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 1)
130
131 /* limit to rk29 hardware capabilities */
132 #define RK29_CAM_BUS_PARAM   (SOCAM_MASTER |\
133                 SOCAM_HSYNC_ACTIVE_HIGH |\
134                 SOCAM_HSYNC_ACTIVE_LOW |\
135                 SOCAM_VSYNC_ACTIVE_HIGH |\
136                 SOCAM_VSYNC_ACTIVE_LOW |\
137                 SOCAM_PCLK_SAMPLE_RISING |\
138                 SOCAM_PCLK_SAMPLE_FALLING|\
139                 SOCAM_DATA_ACTIVE_HIGH |\
140                 SOCAM_DATA_ACTIVE_LOW|\
141                 SOCAM_DATAWIDTH_8|SOCAM_DATAWIDTH_10|\
142                 SOCAM_MCLK_24MHZ |SOCAM_MCLK_48MHZ)
143
144 #define RK29_CAM_W_MIN        48
145 #define RK29_CAM_H_MIN        32
146 #define RK29_CAM_W_MAX        3856            /* ddl@rock-chips.com : 10M Pixel */
147 #define RK29_CAM_H_MAX        2764
148 #define RK29_CAM_FRAME_INVAL_INIT 3
149 #define RK29_CAM_FRAME_INVAL_DC 3          /* ddl@rock-chips.com :  */
150
151 #define RK29_CAM_AXI   0
152 #define RK29_CAM_AHB   1
153 #define CONFIG_RK29_CAM_WORK_BUS    RK29_CAM_AXI
154
155 extern void videobuf_dma_contig_free(struct videobuf_queue *q, struct videobuf_buffer *buf);
156 extern dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
157 extern void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
158             struct videobuf_queue_ops *ops,
159             struct device *dev,
160             spinlock_t *irqlock,
161             enum v4l2_buf_type type,
162             enum v4l2_field field,
163             unsigned int msize,
164             void *priv);
165
166 /* buffer for one video frame */
167 struct rk29_buffer
168 {
169     /* common v4l buffer stuff -- must be first */
170     struct videobuf_buffer vb;
171     const struct soc_camera_data_format        *fmt;
172     int                 inwork;
173 };
174 enum rk29_camera_reg_state
175 {
176         Reg_Invalidate,
177         Reg_Validate
178 };
179
180 struct rk29_camera_reg
181 {
182         unsigned int VipCtrl;
183         unsigned int VipCrop;
184         unsigned int VipFs;
185         unsigned int VipIntMsk;
186         unsigned int VipCrm;
187         enum rk29_camera_reg_state Inval;
188 };
189 struct rk29_camera_work
190 {
191         struct videobuf_buffer *vb;
192         struct rk29_camera_dev *pcdev;
193         struct work_struct work;
194 };
195 struct rk29_camera_dev
196 {
197     struct soc_camera_host      soc_host;
198     struct device               *dev;
199     /* RK2827x is only supposed to handle one camera on its Quick Capture
200      * interface. If anyone ever builds hardware to enable more than
201      * one camera, they will have to modify this driver too */
202     struct soc_camera_device *icd;
203
204         struct clk *aclk_ddr_lcdc;
205         struct clk *aclk_disp_matrix;
206
207         struct clk *hclk_cpu_display;
208         struct clk *vip_slave;
209
210     struct clk *vip;
211         struct clk *vip_input;
212         struct clk *vip_bus;
213
214         struct clk *hclk_disp_matrix;
215         struct clk *vip_matrix;
216
217     void __iomem *base;
218         void __iomem *grf_base;
219     int frame_inval;           /* ddl@rock-chips.com : The first frames is invalidate  */
220     unsigned int irq;
221         unsigned int fps;
222         unsigned int pixfmt;
223         unsigned int vipmem_phybase;
224         unsigned int vipmem_size;
225         unsigned int vipmem_bsize;
226         int host_width;
227         int host_height;
228
229     struct rk29camera_platform_data *pdata;
230     struct resource             *res;
231
232     struct list_head    capture;
233
234     spinlock_t          lock;
235
236     struct videobuf_buffer      *active;
237         struct videobuf_queue *vb_vidq_ptr;
238         struct rk29_camera_reg reginfo_suspend;
239         struct workqueue_struct *camera_wq;
240         struct rk29_camera_work *camera_work;
241         unsigned int camera_work_count;
242         struct hrtimer fps_timer;
243         struct work_struct camera_reinit_work;
244 };
245 static DEFINE_MUTEX(camera_lock);
246 static const char *rk29_cam_driver_description = "RK29_Camera";
247 static struct rk29_camera_dev *rk29_camdev_info_ptr;
248
249 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable);
250
251
252 /*
253  *  Videobuf operations
254  */
255 static int rk29_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
256                                unsigned int *size)
257 {
258     struct soc_camera_device *icd = vq->priv_data;
259         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
260     struct rk29_camera_dev *pcdev = ici->priv;
261     int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
262
263     dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
264
265         /* planar capture requires Y, U and V buffers to be page aligned */
266     *size = PAGE_ALIGN(icd->user_width* icd->user_height * bytes_per_pixel);                               /* Y pages UV pages, yuv422*/
267         pcdev->vipmem_bsize = PAGE_ALIGN(pcdev->host_width * pcdev->host_height * bytes_per_pixel);
268
269         if ((pcdev->host_width != pcdev->icd->user_width) || (pcdev->host_height != pcdev->icd->user_height)) {
270
271                 if (*count > pcdev->vipmem_size/pcdev->vipmem_bsize) {    /* Buffers must be limited, when this resolution is genered by IPP */
272                         *count = pcdev->vipmem_size/pcdev->vipmem_bsize;
273                 }
274
275                 if ((pcdev->camera_work_count != *count) && pcdev->camera_work) {
276                         kfree(pcdev->camera_work);
277                         pcdev->camera_work = NULL;
278                         pcdev->camera_work_count = 0;
279                 }
280
281                 if (pcdev->camera_work == NULL) {
282                         pcdev->camera_work = kmalloc(sizeof(struct rk29_camera_work)*(*count), GFP_KERNEL);
283                         if (pcdev->camera_work == NULL) {
284                                 RK29CAMERA_TR("\n %s kmalloc fail\n", __FUNCTION__);
285                                 BUG();
286                         }
287                         pcdev->camera_work_count = *count;
288                 }
289         }
290
291     RK29CAMERA_DG("%s..%d.. videobuf size:%d, vipmem_buf size:%d \n",__FUNCTION__,__LINE__, *size,pcdev->vipmem_bsize);
292
293     return 0;
294 }
295 static void rk29_videobuf_free(struct videobuf_queue *vq, struct rk29_buffer *buf)
296 {
297     struct soc_camera_device *icd = vq->priv_data;
298
299     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
300             &buf->vb, buf->vb.baddr, buf->vb.bsize);
301
302         /* ddl@rock-chips.com: buf_release called soc_camera_streamoff and soc_camera_close*/
303         if (buf->vb.state == VIDEOBUF_NEEDS_INIT)
304                 return;
305
306     if (in_interrupt())
307         BUG();
308
309     videobuf_dma_contig_free(vq, &buf->vb);
310     dev_dbg(&icd->dev, "%s freed\n", __func__);
311     buf->vb.state = VIDEOBUF_NEEDS_INIT;
312         return;
313 }
314 static int rk29_videobuf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field)
315 {
316     struct soc_camera_device *icd = vq->priv_data;
317     struct rk29_buffer *buf;
318     int ret;
319
320     buf = container_of(vb, struct rk29_buffer, vb);
321
322     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
323             vb, vb->baddr, vb->bsize);
324
325     //RK29CAMERA_TR("\n%s..%d..  \n",__FUNCTION__,__LINE__);
326
327     /* Added list head initialization on alloc */
328     WARN_ON(!list_empty(&vb->queue));
329
330     /* This can be useful if you want to see if we actually fill
331      * the buffer with something */
332     //memset((void *)vb->baddr, 0xaa, vb->bsize);
333
334     BUG_ON(NULL == icd->current_fmt);
335
336     if (buf->fmt    != icd->current_fmt ||
337             vb->width   != icd->user_width ||
338             vb->height  != icd->user_height ||
339              vb->field   != field) {
340         buf->fmt    = icd->current_fmt;
341         vb->width   = icd->user_width;
342         vb->height  = icd->user_height;
343         vb->field   = field;
344         vb->state   = VIDEOBUF_NEEDS_INIT;
345     }
346
347     vb->size = (((vb->width * vb->height *buf->fmt->depth) + 7) >> 3) ;          /* ddl@rock-chips.com : fmt->depth is coorect */
348     if (0 != vb->baddr && vb->bsize < vb->size) {
349         ret = -EINVAL;
350         goto out;
351     }
352
353     if (vb->state == VIDEOBUF_NEEDS_INIT) {
354         ret = videobuf_iolock(vq, vb, NULL);
355         if (ret) {
356             goto fail;
357         }
358         vb->state = VIDEOBUF_PREPARED;
359     }
360     //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
361     return 0;
362 fail:
363     rk29_videobuf_free(vq, buf);
364 out:
365     return ret;
366 }
367
368 static inline void rk29_videobuf_capture(struct videobuf_buffer *vb)
369 {
370         unsigned int y_addr,uv_addr;
371         struct rk29_camera_dev *pcdev = rk29_camdev_info_ptr;
372
373     if (vb) {
374                 if ((pcdev->host_width != pcdev->icd->user_width) || (pcdev->host_height != pcdev->icd->user_height)) {
375                         y_addr = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
376                         uv_addr = y_addr + pcdev->host_width*pcdev->host_height;
377
378                         if (y_addr > (pcdev->vipmem_phybase + pcdev->vipmem_size - pcdev->vipmem_bsize)) {
379                                 RK29CAMERA_TR("vipmem for IPP is overflow! %dx%d -> %dx%d vb_index:%d\n",pcdev->host_width,pcdev->host_height,
380                                                   pcdev->icd->user_width,pcdev->icd->user_height, vb->i);
381                                 BUG();
382                         }
383                 } else {
384                         y_addr = vb->boff;
385                         uv_addr = y_addr + vb->width * vb->height;
386                 }
387         write_vip_reg(RK29_VIP_CAPTURE_F1SA_Y, y_addr);
388         write_vip_reg(RK29_VIP_CAPTURE_F1SA_UV, uv_addr);
389         write_vip_reg(RK29_VIP_CAPTURE_F2SA_Y, y_addr);
390         write_vip_reg(RK29_VIP_CAPTURE_F2SA_UV, uv_addr);
391         write_vip_reg(RK29_VIP_FB_SR,  0x00000002);//frame1 has been ready to receive data,frame 2 is not used
392     }
393 }
394 /* Locking: Caller holds q->irqlock */
395 static void rk29_videobuf_queue(struct videobuf_queue *vq,
396                                 struct videobuf_buffer *vb)
397 {
398     struct soc_camera_device *icd = vq->priv_data;
399     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
400     struct rk29_camera_dev *pcdev = ici->priv;
401
402     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
403             vb, vb->baddr, vb->bsize);
404
405     vb->state = VIDEOBUF_QUEUED;
406
407         if (list_empty(&pcdev->capture)) {
408                 list_add_tail(&vb->queue, &pcdev->capture);
409         } else {
410                 if (list_entry(pcdev->capture.next, struct videobuf_buffer, queue) != vb)
411                         list_add_tail(&vb->queue, &pcdev->capture);
412                 else
413                         BUG();    /* ddl@rock-chips.com : The same videobuffer queue again */
414         }
415
416     if (!pcdev->active) {
417         pcdev->active = vb;
418         rk29_videobuf_capture(vb);
419     }
420 }
421 static int rk29_pixfmt2ippfmt(unsigned int pixfmt, int *ippfmt)
422 {
423         switch (pixfmt)
424         {
425                 case V4L2_PIX_FMT_YUV422P:
426                 {
427                         *ippfmt = IPP_Y_CBCR_H2V1;
428                         break;
429                 }
430                 case V4L2_PIX_FMT_YUV420:
431                 {
432                         *ippfmt = IPP_Y_CBCR_H2V2;
433                         break;
434                 }
435                 default:
436                         goto rk29_pixfmt2ippfmt_err;
437         }
438
439         return 0;
440 rk29_pixfmt2ippfmt_err:
441         return -1;
442 }
443 static void rk29_camera_capture_process(struct work_struct *work)
444 {
445         struct rk29_camera_work *camera_work = container_of(work, struct rk29_camera_work, work);
446         struct videobuf_buffer *vb = camera_work->vb;
447         struct rk29_camera_dev *pcdev = camera_work->pcdev;
448         struct rk29_ipp_req ipp_req;
449         unsigned long int flags;
450
451         ipp_req.src0.YrgbMst = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
452         ipp_req.src0.CbrMst= ipp_req.src0.YrgbMst + pcdev->host_width*pcdev->host_height;
453         ipp_req.src0.w = pcdev->host_width;
454         ipp_req.src0.h = pcdev->host_height;
455         rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.src0.fmt);
456
457         ipp_req.dst0.YrgbMst = vb->boff;
458         ipp_req.dst0.CbrMst= vb->boff+vb->width * vb->height;
459         ipp_req.dst0.w = pcdev->icd->user_width;
460         ipp_req.dst0.h = pcdev->icd->user_height;
461         rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.dst0.fmt);
462
463         ipp_req.src_vir_w = ipp_req.src0.w;
464         ipp_req.dst_vir_w = ipp_req.dst0.w;
465         ipp_req.timeout = 100;
466         ipp_req.flag = IPP_ROT_0;
467
468         if (ipp_do_blit(&ipp_req)) {
469                 spin_lock_irqsave(&pcdev->lock, flags);
470                 vb->state = VIDEOBUF_ERROR;
471                 spin_unlock_irqrestore(&pcdev->lock, flags);
472                 RK29CAMERA_TR("Capture image(vb->i:0x%x) which IPP operated is error!\n", vb->i);
473                 RK29CAMERA_TR("ipp_req.src0.YrgbMst:0x%x ipp_req.src0.CbrMst:0x%x \n", ipp_req.src0.YrgbMst,ipp_req.src0.CbrMst);
474                 RK29CAMERA_TR("ipp_req.src0.w:0x%x ipp_req.src0.h:0x%x \n",ipp_req.src0.w,ipp_req.src0.h);
475                 RK29CAMERA_TR("ipp_req.src0.fmt:0x%x\n",ipp_req.src0.fmt);
476                 RK29CAMERA_TR("ipp_req.dst0.YrgbMst:0x%x ipp_req.dst0.CbrMst:0x%x \n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
477                 RK29CAMERA_TR("ipp_req.dst0.w:0x%x ipp_req.dst0.h:0x%x \n",ipp_req.dst0.w ,ipp_req.dst0.h);
478                 RK29CAMERA_TR("ipp_req.dst0.fmt:0x%x\n",ipp_req.dst0.fmt);
479                 RK29CAMERA_TR("ipp_req.src_vir_w:0x%x ipp_req.dst_vir_w :0x%x\n",ipp_req.src_vir_w ,ipp_req.dst_vir_w);
480             RK29CAMERA_TR("ipp_req.timeout:0x%x ipp_req.flag :0x%x\n",ipp_req.timeout,ipp_req.flag);
481         }
482
483         wake_up(&(camera_work->vb->done));
484 }
485 static irqreturn_t rk29_camera_irq(int irq, void *data)
486 {
487     struct rk29_camera_dev *pcdev = data;
488     struct videobuf_buffer *vb;
489         struct rk29_camera_work *wk;
490
491     read_vip_reg(RK29_VIP_INT_STS);    /* clear vip interrupte single  */
492     /* ddl@rock-chps.com : Current VIP is run in One Frame Mode, Frame 1 is validate */
493     if (read_vip_reg(RK29_VIP_FB_SR) & 0x01) {
494                 pcdev->fps++;
495                 if (!pcdev->active)
496                         goto RK29_CAMERA_IRQ_END;
497
498         if (pcdev->frame_inval>0) {
499             pcdev->frame_inval--;
500             rk29_videobuf_capture(pcdev->active);
501             goto RK29_CAMERA_IRQ_END;
502         } else if (pcdev->frame_inval) {
503                 RK29CAMERA_TR("frame_inval : %0x",pcdev->frame_inval);
504             pcdev->frame_inval = 0;
505         }
506
507         vb = pcdev->active;
508                 /* ddl@rock-chips.com : this vb may be deleted from queue */
509                 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
510                 list_del_init(&vb->queue);
511                 }
512
513         pcdev->active = NULL;
514         if (!list_empty(&pcdev->capture)) {
515             pcdev->active = list_entry(pcdev->capture.next, struct videobuf_buffer, queue);
516                         if (pcdev->active) {
517                                 rk29_videobuf_capture(pcdev->active);
518                         }
519         }
520
521         if (pcdev->active == NULL) {
522                         RK29CAMERA_DG("%s video_buf queue is empty!\n",__FUNCTION__);
523         }
524
525                 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
526                 vb->state = VIDEOBUF_DONE;
527                 do_gettimeofday(&vb->ts);
528                 vb->field_count++;
529                 }
530
531                 if ((pcdev->host_width != pcdev->icd->user_width) || (pcdev->host_height != pcdev->icd->user_height)) {
532                         wk = pcdev->camera_work + vb->i;
533                         INIT_WORK(&(wk->work), rk29_camera_capture_process);
534                         wk->vb = vb;
535                         wk->pcdev = pcdev;
536                         queue_work(pcdev->camera_wq, &(wk->work));
537                 } else {
538                         wake_up(&vb->done);
539                 }
540     }
541
542 RK29_CAMERA_IRQ_END:
543     return IRQ_HANDLED;
544 }
545
546
547 static void rk29_videobuf_release(struct videobuf_queue *vq,
548                                   struct videobuf_buffer *vb)
549 {
550     struct rk29_buffer *buf = container_of(vb, struct rk29_buffer, vb);
551     struct soc_camera_device *icd = vq->priv_data;
552     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
553     struct rk29_camera_dev *pcdev = ici->priv;
554
555 #ifdef DEBUG
556     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
557             vb, vb->baddr, vb->bsize);
558
559     switch (vb->state)
560     {
561         case VIDEOBUF_ACTIVE:
562             dev_dbg(&icd->dev, "%s (active)\n", __func__);
563             break;
564         case VIDEOBUF_QUEUED:
565             dev_dbg(&icd->dev, "%s (queued)\n", __func__);
566             break;
567         case VIDEOBUF_PREPARED:
568             dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
569             break;
570         default:
571             dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
572             break;
573     }
574 #endif
575         if (vb == pcdev->active) {
576                 RK29CAMERA_DG("%s Wait for this video buf(0x%x) write finished!\n ",__FUNCTION__,(unsigned int)vb);
577                 interruptible_sleep_on_timeout(&vb->done, 100);
578                 RK29CAMERA_DG("%s This video buf(0x%x) write finished, release now!!\n",__FUNCTION__,(unsigned int)vb);
579         }
580     rk29_videobuf_free(vq, buf);
581 }
582
583 static struct videobuf_queue_ops rk29_videobuf_ops =
584 {
585     .buf_setup      = rk29_videobuf_setup,
586     .buf_prepare    = rk29_videobuf_prepare,
587     .buf_queue      = rk29_videobuf_queue,
588     .buf_release    = rk29_videobuf_release,
589 };
590
591 static void rk29_camera_init_videobuf(struct videobuf_queue *q,
592                                       struct soc_camera_device *icd)
593 {
594     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
595     struct rk29_camera_dev *pcdev = ici->priv;
596
597     /* We must pass NULL as dev pointer, then all pci_* dma operations
598      * transform to normal dma_* ones. */
599     videobuf_queue_dma_contig_init(q,
600                                    &rk29_videobuf_ops,
601                                    ici->v4l2_dev.dev, &pcdev->lock,
602                                    V4L2_BUF_TYPE_VIDEO_CAPTURE,
603                                    V4L2_FIELD_NONE,
604                                    sizeof(struct rk29_buffer),
605                                    icd);
606         pcdev->vb_vidq_ptr = q;         /* ddl@rock-chips.com */
607 }
608 static int rk29_camera_activate(struct rk29_camera_dev *pcdev, struct soc_camera_device *icd)
609 {
610     unsigned long sensor_bus_flags = SOCAM_MCLK_24MHZ;
611     struct clk *parent;
612
613     RK29CAMERA_DG("%s..%d.. \n",__FUNCTION__,__LINE__);
614     if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix ||  !pcdev->hclk_cpu_display ||
615                 !pcdev->vip_slave || !pcdev->vip || !pcdev->vip_input || !pcdev->vip_bus ||
616                 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) ||  IS_ERR(pcdev->hclk_cpu_display) ||
617                 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus))  {
618
619         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
620         goto RK29_CAMERA_ACTIVE_ERR;
621     }
622
623         if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
624                 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix))  {
625
626         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
627         goto RK29_CAMERA_ACTIVE_ERR;
628     }
629
630         clk_enable(pcdev->aclk_ddr_lcdc);
631         clk_enable(pcdev->aclk_disp_matrix);
632
633         clk_enable(pcdev->hclk_cpu_display);
634         clk_enable(pcdev->vip_slave);
635
636 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
637         clk_enable(pcdev->hclk_disp_matrix);
638         clk_enable(pcdev->vip_matrix);
639 #endif
640
641         clk_enable(pcdev->vip_input);
642         clk_enable(pcdev->vip_bus);
643
644     //if (icd->ops->query_bus_param)                                                  /* ddl@rock-chips.com : Query Sensor's xclk */
645         //sensor_bus_flags = icd->ops->query_bus_param(icd);
646
647     if (sensor_bus_flags & SOCAM_MCLK_48MHZ) {
648         parent = clk_get(NULL, "clk48m");
649         if (!parent || IS_ERR(parent))
650              goto RK29_CAMERA_ACTIVE_ERR;
651     } else if (sensor_bus_flags & SOCAM_MCLK_27MHZ) {
652         parent = clk_get(NULL, "extclk");
653         if (!parent || IS_ERR(parent))
654              goto RK29_CAMERA_ACTIVE_ERR;
655     } else {
656         parent = clk_get(NULL, "xin24m");
657         if (!parent || IS_ERR(parent))
658              goto RK29_CAMERA_ACTIVE_ERR;
659     }
660
661     clk_set_parent(pcdev->vip, parent);
662
663     clk_enable(pcdev->vip);
664     rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_VIP_CLKOUT);
665     ndelay(10);
666
667 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
668         write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)|VIP_AHBMASTER);  //VIP Config to AHB
669         write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)&(~VIP_ACLK_DIV_HCLK_2));   //aclk:hclk = 1:1
670 #else
671         write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)&(~VIP_AHBMASTER));  //VIP Config to AXI
672     write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)|VIP_ACLK_DIV_HCLK_2);   //aclk:hclk = 2:1
673 #endif
674         ndelay(10);
675
676     write_vip_reg(RK29_VIP_RESET, 0x76543210);  /* ddl@rock-chips.com : vip software reset */
677     udelay(10);
678
679     write_vip_reg(RK29_VIP_AHBR_CTRL, 0x07);   /* ddl@rock-chips.com : vip ahb burst 16 */
680     write_vip_reg(RK29_VIP_INT_MASK, 0x01);    //capture complete interrupt enable
681     write_vip_reg(RK29_VIP_CRM,  0x00000000);  //Y/CB/CR color modification
682
683     return 0;
684 RK29_CAMERA_ACTIVE_ERR:
685     return -ENODEV;
686 }
687
688 static void rk29_camera_deactivate(struct rk29_camera_dev *pcdev)
689 {
690     //pcdev->active = NULL;
691
692     write_vip_reg(RK29_VIP_CTRL, 0);
693     read_vip_reg(RK29_VIP_INT_STS);             //clear vip interrupte single
694
695     rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_GPIO1B4);
696     clk_disable(pcdev->vip);
697
698         clk_disable(pcdev->vip_input);
699         clk_disable(pcdev->vip_bus);
700
701 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
702         clk_disable(pcdev->hclk_disp_matrix);
703         clk_disable(pcdev->vip_matrix);
704 #endif
705
706         clk_disable(pcdev->hclk_cpu_display);
707         clk_disable(pcdev->vip_slave);
708
709         clk_disable(pcdev->aclk_ddr_lcdc);
710         clk_disable(pcdev->aclk_disp_matrix);
711     return;
712 }
713
714 /* The following two functions absolutely depend on the fact, that
715  * there can be only one camera on RK28 quick capture interface */
716 static int rk29_camera_add_device(struct soc_camera_device *icd)
717 {
718     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
719     struct rk29_camera_dev *pcdev = ici->priv;
720     struct device *control = to_soc_camera_control(icd);
721     struct v4l2_subdev *sd;
722     int ret;
723
724     mutex_lock(&camera_lock);
725
726     if (pcdev->icd) {
727         ret = -EBUSY;
728         goto ebusy;
729     }
730
731     dev_info(&icd->dev, "RK29 Camera driver attached to camera%d(%s)\n",
732              icd->devnum,dev_name(icd->pdev));
733
734         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
735     pcdev->active = NULL;
736     pcdev->icd = NULL;
737         pcdev->reginfo_suspend.Inval = Reg_Invalidate;
738         /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
739      * if app havn't dequeue all videobuf before close camera device;
740         */
741     INIT_LIST_HEAD(&pcdev->capture);
742
743     ret = rk29_camera_activate(pcdev,icd);
744     if (ret)
745         goto ebusy;
746
747     /* ddl@rock-chips.com : v4l2_subdev is not created when ici->ops->add called in soc_camera_probe  */
748     if (control) {
749         sd = dev_get_drvdata(control);
750                 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_IOREQUEST,(void*)pcdev->pdata);
751         ret = v4l2_subdev_call(sd,core, init, 0);
752         if (ret)
753             goto ebusy;
754     }
755
756     pcdev->icd = icd;
757
758 ebusy:
759     mutex_unlock(&camera_lock);
760
761     return ret;
762 }
763 static void rk29_camera_remove_device(struct soc_camera_device *icd)
764 {
765     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
766     struct rk29_camera_dev *pcdev = ici->priv;
767         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
768
769         mutex_lock(&camera_lock);
770     BUG_ON(icd != pcdev->icd);
771
772     dev_info(&icd->dev, "RK29 Camera driver detached from camera%d(%s)\n",
773              icd->devnum,dev_name(icd->pdev));
774
775         /* ddl@rock-chips.com: Application will call VIDIOC_STREAMOFF before close device, but
776            stream may be turn on again before close device, if suspend and resume happened. */
777         if (read_vip_reg(RK29_VIP_CTRL) & ENABLE_CAPTURE) {
778                 rk29_camera_s_stream(icd,0);
779         }
780
781     v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_DEACTIVATE,NULL);
782         rk29_camera_deactivate(pcdev);
783
784         /* ddl@rock-chips.com: Call videobuf_mmap_free here for free the struct video_buffer which malloc in videobuf_alloc */
785         #if 0
786         if (pcdev->vb_vidq_ptr) {
787                 videobuf_mmap_free(pcdev->vb_vidq_ptr);
788                 pcdev->vb_vidq_ptr = NULL;
789         }
790         #else
791         pcdev->vb_vidq_ptr = NULL;
792         #endif
793
794         if (pcdev->camera_work) {
795                 kfree(pcdev->camera_work);
796                 pcdev->camera_work = NULL;
797                 pcdev->camera_work_count = 0;
798         }
799
800         pcdev->active = NULL;
801     pcdev->icd = NULL;
802         pcdev->reginfo_suspend.Inval = Reg_Invalidate;
803         /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
804      * if app havn't dequeue all videobuf before close camera device;
805         */
806     INIT_LIST_HEAD(&pcdev->capture);
807
808         mutex_unlock(&camera_lock);
809         RK29CAMERA_DG("%s exit\n",__FUNCTION__);
810
811         return;
812 }
813
814 static int rk29_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
815 {
816     unsigned long bus_flags, camera_flags, common_flags;
817     unsigned int vip_ctrl_val = 0;
818     int ret = 0;
819
820     RK29CAMERA_DG("%s..%d..\n",__FUNCTION__,__LINE__);
821
822     bus_flags = RK29_CAM_BUS_PARAM;
823         if (icd->ops->query_bus_param)
824         camera_flags = icd->ops->query_bus_param(icd);
825         else
826                 camera_flags = 0;
827
828     common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
829     if (!common_flags) {
830         ret = -EINVAL;
831         goto RK29_CAMERA_SET_BUS_PARAM_END;
832     }
833
834     ret = icd->ops->set_bus_param(icd, common_flags);
835     if (ret < 0)
836         goto RK29_CAMERA_SET_BUS_PARAM_END;
837
838         if (common_flags & SOCAM_DATAWIDTH_8) {
839         icd->buswidth = 8;
840         } else if (common_flags & SOCAM_DATAWIDTH_10) {
841             icd->buswidth = 10;
842         }
843
844     vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
845     if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) {
846         vip_ctrl_val |= NEGATIVE_EDGE;
847     } else {
848                 vip_ctrl_val &= ~NEGATIVE_EDGE;
849     }
850     if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) {
851         vip_ctrl_val |= HSY_LOW_ACTIVE;
852     } else {
853                 vip_ctrl_val &= ~HSY_LOW_ACTIVE;
854     }
855     if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) {
856         vip_ctrl_val |= VSY_HIGH_ACTIVE;
857     } else {
858                 vip_ctrl_val &= ~VSY_HIGH_ACTIVE;
859     }
860
861     /* ddl@rock-chips.com : Don't enable capture here, enable in stream_on */
862     //vip_ctrl_val |= ENABLE_CAPTURE;
863
864     write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
865     RK29CAMERA_DG("%s..ctrl:0x%x CtrReg=%x AXI_AHB:0x%x aclk_hclk:0x%x \n",__FUNCTION__,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL),
866                 read_grf_reg(GRF_SOC_CON0_Reg)&VIP_AHBMASTER, read_grf_reg(GRF_OS_REG0)&VIP_ACLK_DIV_HCLK_2);
867
868 RK29_CAMERA_SET_BUS_PARAM_END:
869         if (ret)
870         RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
871     return ret;
872 }
873
874 static int rk29_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
875 {
876     unsigned long bus_flags, camera_flags;
877     int ret;
878
879     bus_flags = RK29_CAM_BUS_PARAM;
880         if (icd->ops->query_bus_param) {
881         camera_flags = icd->ops->query_bus_param(icd);
882         } else {
883                 camera_flags = 0;
884         }
885     ret = soc_camera_bus_param_compatible(camera_flags, bus_flags) ;
886
887     if (ret < 0)
888         dev_warn(icd->dev.parent,
889                          "Flags incompatible: camera %lx, host %lx\n",
890                          camera_flags, bus_flags);
891     return ret;
892 }
893 static const struct soc_camera_data_format rk29_camera_formats[] = {
894         {
895                 .name           = "Planar YUV420 12 bit",
896                 .depth          = 12,
897                 .fourcc         = V4L2_PIX_FMT_YUV420,
898                 .colorspace     = V4L2_COLORSPACE_JPEG,
899         },{
900                 .name           = "Planar YUV422 16 bit",
901                 .depth          = 16,
902                 .fourcc         = V4L2_PIX_FMT_YUV422P,
903                 .colorspace     = V4L2_COLORSPACE_JPEG,
904         },{
905                 .name           = "Raw Bayer RGB 10 bit",
906                 .depth          = 16,
907                 .fourcc         = V4L2_PIX_FMT_SGRBG10,
908                 .colorspace     = V4L2_COLORSPACE_SRGB,
909         }
910 };
911
912 static void rk29_camera_setup_format(struct soc_camera_device *icd, __u32 host_pixfmt, __u32 cam_pixfmt, struct v4l2_rect *rect)
913 {
914         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
915     struct rk29_camera_dev *pcdev = ici->priv;
916     unsigned int vip_fs = 0,vip_crop = 0;
917     unsigned int vip_ctrl_val = VIP_SENSOR|ONEFRAME|DISABLE_CAPTURE;
918
919     switch (host_pixfmt)
920     {
921         case V4L2_PIX_FMT_YUV422P:
922             vip_ctrl_val |= VIPREGYUV422;
923                         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
924                         pcdev->pixfmt = host_pixfmt;
925             break;
926         case V4L2_PIX_FMT_YUV420:
927             vip_ctrl_val |= VIPREGYUV420;
928                         if (pcdev->frame_inval != RK29_CAM_FRAME_INVAL_INIT)
929                                 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
930                         pcdev->pixfmt = host_pixfmt;
931             break;
932                 case V4L2_PIX_FMT_SGRBG10:
933                         vip_ctrl_val |= (VIP_RAW | VIP_SENSOR | VIP_DATA_LITTLEEND);
934                         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
935                         pcdev->pixfmt = host_pixfmt;
936                         break;
937         default:                                                                                /* ddl@rock-chips.com : vip output format is hold when pixfmt is invalidate */
938             vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & VIPREGYUV422);
939             break;
940     }
941
942     switch (cam_pixfmt)
943     {
944         case V4L2_PIX_FMT_UYVY:
945             vip_ctrl_val |= SENSOR_UYVY;
946             break;
947         case V4L2_PIX_FMT_YUYV:
948             vip_ctrl_val |= SENSOR_YUYV;
949             break;
950         default :
951             vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & SENSOR_YUYV);
952             break;
953     }
954
955     write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);         /* ddl@rock-chips.com: VIP capture mode and capture format must be set before FS register set */
956
957     read_vip_reg(RK29_VIP_INT_STS);                     /* clear vip interrupte single  */
958
959     if (vip_ctrl_val & ONEFRAME)  {
960         vip_crop = ((rect->left<<16) + rect->top);
961         vip_fs  = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
962     } else if (vip_ctrl_val & PING_PONG) {
963         BUG();
964     }
965
966     write_vip_reg(RK29_VIP_CROP, vip_crop);
967     write_vip_reg(RK29_VIP_FS, vip_fs);
968
969     write_vip_reg(RK29_VIP_FB_SR,  0x00000003);
970
971         pcdev->host_width = rect->width;
972         pcdev->host_height = rect->height;
973
974     RK29CAMERA_DG("%s.. crop:0x%x fs:0x%x ctrl:0x%x CtrlReg:0x%x\n",__FUNCTION__,vip_crop,vip_fs,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL));
975         return;
976 }
977
978 static int rk29_camera_get_formats(struct soc_camera_device *icd, int idx,
979                                   struct soc_camera_format_xlate *xlate)
980 {
981     struct device *dev = icd->dev.parent;
982     int formats = 0, buswidth, ret;
983
984     buswidth = 8;
985
986     ret = rk29_camera_try_bus_param(icd, buswidth);
987     if (ret < 0)
988         return 0;
989
990     switch (icd->formats[idx].fourcc) {
991         case V4L2_PIX_FMT_UYVY:
992         case V4L2_PIX_FMT_YUYV:
993             formats++;
994             if (xlate) {
995                 xlate->host_fmt = &rk29_camera_formats[0];
996                 xlate->cam_fmt = icd->formats + idx;
997                 xlate->buswidth = buswidth;
998                 xlate++;
999                 dev_dbg(dev, "Providing format %s using %s\n",
1000                         rk29_camera_formats[0].name,
1001                         icd->formats[idx].name);
1002             }
1003
1004             formats++;
1005             if (xlate) {
1006                 xlate->host_fmt = &rk29_camera_formats[1];
1007                 xlate->cam_fmt = icd->formats + idx;
1008                 xlate->buswidth = buswidth;
1009                 xlate++;
1010                 dev_dbg(dev, "Providing format %s using %s\n",
1011                         rk29_camera_formats[1].name,
1012                         icd->formats[idx].name);
1013             }
1014                         break;
1015                 case V4L2_PIX_FMT_SGRBG10:
1016                         formats++;
1017             if (xlate) {
1018                 xlate->host_fmt = &rk29_camera_formats[2];
1019                 xlate->cam_fmt = icd->formats + idx;
1020                 xlate->buswidth = 10;
1021                 xlate++;
1022                 dev_dbg(dev, "Providing format %s using %s\n",
1023                         rk29_camera_formats[2].name,
1024                         icd->formats[idx].name);
1025             }
1026                         break;
1027         default:
1028             break;
1029     }
1030
1031     return formats;
1032 }
1033
1034 static void rk29_camera_put_formats(struct soc_camera_device *icd)
1035 {
1036         return;
1037 }
1038
1039 static int rk29_camera_set_crop(struct soc_camera_device *icd,
1040                                struct v4l2_crop *a)
1041 {
1042     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1043     struct v4l2_format f;
1044     struct v4l2_pix_format *pix = &f.fmt.pix;
1045     int ret;
1046
1047     f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1048
1049     ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1050     if (ret < 0)
1051         return ret;
1052
1053     if ((pix->width < (a->c.left + a->c.width)) || (pix->height < (a->c.top + a->c.height)))  {
1054
1055         pix->width = a->c.left + a->c.width;
1056         pix->height = a->c.top + a->c.height;
1057
1058         v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1059             &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1060             icd->current_fmt->fourcc == V4L2_PIX_FMT_YUV422P ?4 : 0);
1061
1062         ret = v4l2_subdev_call(sd, video, s_fmt, &f);
1063         if (ret < 0)
1064             return ret;
1065     }
1066
1067     rk29_camera_setup_format(icd, icd->current_fmt->fourcc, pix->pixelformat, &a->c);
1068
1069     icd->user_width = pix->width;
1070     icd->user_height = pix->height;
1071
1072     return 0;
1073 }
1074
1075 static int rk29_camera_set_fmt(struct soc_camera_device *icd,
1076                               struct v4l2_format *f)
1077 {
1078     struct device *dev = icd->dev.parent;
1079     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1080     const struct soc_camera_data_format *cam_fmt = NULL;
1081     const struct soc_camera_format_xlate *xlate = NULL;
1082         struct soc_camera_host *ici =to_soc_camera_host(icd->dev.parent);
1083     struct rk29_camera_dev *pcdev = ici->priv;
1084     struct v4l2_pix_format *pix = &f->fmt.pix;
1085     struct v4l2_format cam_f = *f;
1086     struct v4l2_rect rect;
1087     int ret,usr_w,usr_h;
1088
1089         usr_w = pix->width;
1090         usr_h = pix->height;
1091     RK29CAMERA_DG("%s enter width:%d  height:%d\n",__FUNCTION__,usr_w,usr_h);
1092
1093     xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1094     if (!xlate) {
1095         dev_err(dev, "Format %x not found\n", pix->pixelformat);
1096         ret = -EINVAL;
1097         goto RK29_CAMERA_SET_FMT_END;
1098     }
1099
1100     cam_fmt = xlate->cam_fmt;
1101
1102     cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1103     ret = v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1104     cam_f.fmt.pix.pixelformat = pix->pixelformat;
1105     *pix = cam_f.fmt.pix;
1106         #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1107         if ((pix->width != usr_w) || (pix->height != usr_h)) {
1108                 pix->width = usr_w;
1109                 pix->height = usr_h;
1110         }
1111         #endif
1112     icd->sense = NULL;
1113
1114     if (!ret) {
1115         rect.left = 0;
1116         rect.top = 0;
1117         rect.width = cam_f.fmt.pix.width;
1118         rect.height = cam_f.fmt.pix.height;
1119
1120         RK29CAMERA_DG("%s..%s..%s icd width:%d  host width:%d \n",__FUNCTION__,xlate->host_fmt->name, cam_fmt->name,
1121                                    rect.width, pix->width);
1122         rk29_camera_setup_format(icd, pix->pixelformat, cam_fmt->fourcc, &rect);
1123         icd->buswidth = xlate->buswidth;
1124         icd->current_fmt = xlate->host_fmt;
1125
1126                 if (((pcdev->host_width != pcdev->icd->user_width) || (pcdev->host_height != pcdev->icd->user_height))) {
1127                         BUG_ON(pcdev->vipmem_phybase == 0);
1128                 }
1129     }
1130
1131 RK29_CAMERA_SET_FMT_END:
1132         if (ret)
1133         RK29CAMERA_TR("\n%s..%d.. ret = %d  \n",__FUNCTION__,__LINE__, ret);
1134     return ret;
1135 }
1136
1137 static int rk29_camera_try_fmt(struct soc_camera_device *icd,
1138                                    struct v4l2_format *f)
1139 {
1140     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1141     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1142     const struct soc_camera_format_xlate *xlate;
1143     struct v4l2_pix_format *pix = &f->fmt.pix;
1144     __u32 pixfmt = pix->pixelformat;
1145     enum v4l2_field field;
1146     int ret,usr_w,usr_h;
1147
1148         usr_w = pix->width;
1149         usr_h = pix->height;
1150         RK29CAMERA_DG("%s enter width:%d  height:%d\n",__FUNCTION__,usr_w,usr_h);
1151
1152     xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1153     if (!xlate) {
1154         dev_err(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
1155         ret = -EINVAL;
1156         goto RK29_CAMERA_TRY_FMT_END;
1157     }
1158    /* limit to rk29 hardware capabilities */
1159     v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1160               &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1161               pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
1162
1163     pix->bytesperline = pix->width * DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1164     pix->sizeimage = pix->height * pix->bytesperline;
1165
1166     /* camera has to see its format, but the user the original one */
1167     pix->pixelformat = xlate->cam_fmt->fourcc;
1168     /* limit to sensor capabilities */
1169     ret = v4l2_subdev_call(sd, video, try_fmt, f);
1170     pix->pixelformat = pixfmt;
1171         #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1172         if ((pix->width > usr_w) && (pix->height > usr_h)) {
1173                 pix->width = usr_w;
1174                 pix->height = usr_h;
1175         } else if ((pix->width < usr_w) && (pix->height < usr_h)) {
1176                 if (((usr_w>>1) < pix->width) && ((usr_h>>1) < pix->height)) {
1177                         pix->width = usr_w;
1178                         pix->height = usr_h;
1179                 }
1180         }
1181         #endif
1182
1183     field = pix->field;
1184
1185     if (field == V4L2_FIELD_ANY) {
1186         pix->field = V4L2_FIELD_NONE;
1187     } else if (field != V4L2_FIELD_NONE) {
1188         dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
1189         ret = -EINVAL;
1190         goto RK29_CAMERA_TRY_FMT_END;
1191     }
1192
1193 RK29_CAMERA_TRY_FMT_END:
1194         if (ret)
1195         RK29CAMERA_TR("\n%s..%d.. ret = %d  \n",__FUNCTION__,__LINE__, ret);
1196     return ret;
1197 }
1198
1199 static int rk29_camera_reqbufs(struct soc_camera_file *icf,
1200                                struct v4l2_requestbuffers *p)
1201 {
1202     int i;
1203
1204     /* This is for locking debugging only. I removed spinlocks and now I
1205      * check whether .prepare is ever called on a linked buffer, or whether
1206      * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1207      * it hadn't triggered */
1208     for (i = 0; i < p->count; i++) {
1209         struct rk29_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1210                                                            struct rk29_buffer, vb);
1211         buf->inwork = 0;
1212         INIT_LIST_HEAD(&buf->vb.queue);
1213     }
1214
1215     return 0;
1216 }
1217
1218 static unsigned int rk29_camera_poll(struct file *file, poll_table *pt)
1219 {
1220     struct soc_camera_file *icf = file->private_data;
1221     struct rk29_buffer *buf;
1222
1223     buf = list_entry(icf->vb_vidq.stream.next, struct rk29_buffer,
1224                      vb.stream);
1225
1226     poll_wait(file, &buf->vb.done, pt);
1227
1228     if (buf->vb.state == VIDEOBUF_DONE ||
1229             buf->vb.state == VIDEOBUF_ERROR)
1230         return POLLIN|POLLRDNORM;
1231
1232     return 0;
1233 }
1234
1235 static int rk29_camera_querycap(struct soc_camera_host *ici,
1236                                 struct v4l2_capability *cap)
1237 {
1238     /* cap->name is set by the firendly caller:-> */
1239     strlcpy(cap->card, rk29_cam_driver_description, sizeof(cap->card));
1240     cap->version = RK29_CAM_VERSION_CODE;
1241     cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1242
1243     return 0;
1244 }
1245
1246 static int rk29_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1247 {
1248     struct soc_camera_host *ici =
1249                     to_soc_camera_host(icd->dev.parent);
1250     struct rk29_camera_dev *pcdev = ici->priv;
1251         struct v4l2_subdev *sd;
1252     int ret = 0,tmp;
1253
1254         mutex_lock(&camera_lock);
1255         if ((pcdev->icd == icd) && (icd->ops->suspend)) {
1256                 rk29_camera_s_stream(icd, 0);
1257                 sd = soc_camera_to_subdev(icd);
1258                 v4l2_subdev_call(sd, video, s_stream, 0);
1259                 ret = icd->ops->suspend(icd, state);
1260
1261                 pcdev->reginfo_suspend.VipCtrl = read_vip_reg(RK29_VIP_CTRL);
1262                 pcdev->reginfo_suspend.VipCrop = read_vip_reg(RK29_VIP_CROP);
1263                 pcdev->reginfo_suspend.VipFs = read_vip_reg(RK29_VIP_FS);
1264                 pcdev->reginfo_suspend.VipIntMsk = read_vip_reg(RK29_VIP_INT_MASK);
1265                 pcdev->reginfo_suspend.VipCrm = read_vip_reg(RK29_VIP_CRM);
1266
1267                 tmp = pcdev->reginfo_suspend.VipFs>>16;         /* ddl@rock-chips.com */
1268                 tmp += pcdev->reginfo_suspend.VipCrop>>16;
1269                 pcdev->reginfo_suspend.VipFs = (pcdev->reginfo_suspend.VipFs & 0xffff) | (tmp<<16);
1270
1271                 pcdev->reginfo_suspend.Inval = Reg_Validate;
1272                 rk29_camera_deactivate(pcdev);
1273
1274                 RK29CAMERA_DG("%s Enter Success...\n", __FUNCTION__);
1275         } else {
1276                 RK29CAMERA_DG("%s icd has been deattach, don't need enter suspend\n", __FUNCTION__);
1277         }
1278         mutex_unlock(&camera_lock);
1279     return ret;
1280 }
1281
1282 static int rk29_camera_resume(struct soc_camera_device *icd)
1283 {
1284     struct soc_camera_host *ici =
1285                     to_soc_camera_host(icd->dev.parent);
1286     struct rk29_camera_dev *pcdev = ici->priv;
1287         struct v4l2_subdev *sd;
1288     int ret = 0;
1289
1290         mutex_lock(&camera_lock);
1291         if ((pcdev->icd == icd) && (icd->ops->resume)) {
1292                 if (pcdev->reginfo_suspend.Inval == Reg_Validate) {
1293                         rk29_camera_activate(pcdev, icd);
1294                         write_vip_reg(RK29_VIP_INT_MASK, pcdev->reginfo_suspend.VipIntMsk);
1295                         write_vip_reg(RK29_VIP_CRM, pcdev->reginfo_suspend.VipCrm);
1296                         write_vip_reg(RK29_VIP_CTRL, pcdev->reginfo_suspend.VipCtrl&~ENABLE_CAPTURE);
1297                         write_vip_reg(RK29_VIP_CROP, pcdev->reginfo_suspend.VipCrop);
1298                         write_vip_reg(RK29_VIP_FS, pcdev->reginfo_suspend.VipFs);
1299
1300                         rk29_videobuf_capture(pcdev->active);
1301                         rk29_camera_s_stream(icd, 1);
1302                         pcdev->reginfo_suspend.Inval = Reg_Invalidate;
1303                 } else {
1304                         RK29CAMERA_TR("Resume fail, vip register recored is invalidate!!\n");
1305                         goto rk29_camera_resume_end;
1306                 }
1307
1308                 ret = icd->ops->resume(icd);
1309                 sd = soc_camera_to_subdev(icd);
1310                 v4l2_subdev_call(sd, video, s_stream, 1);
1311
1312                 RK29CAMERA_DG("%s Enter success\n",__FUNCTION__);
1313         } else {
1314                 RK29CAMERA_DG("%s icd has been deattach, don't need enter resume\n", __FUNCTION__);
1315         }
1316
1317 rk29_camera_resume_end:
1318         mutex_unlock(&camera_lock);
1319     return ret;
1320 }
1321
1322 static void rk29_camera_reinit_work(struct work_struct *work)
1323 {
1324         struct device *control;
1325     struct v4l2_subdev *sd;
1326         struct v4l2_format cam_f;
1327         const struct soc_camera_format_xlate *xlate;
1328         int ret;
1329
1330         write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)&(~ENABLE_CAPTURE)));
1331
1332         control = to_soc_camera_control(rk29_camdev_info_ptr->icd);
1333         sd = dev_get_drvdata(control);
1334         ret = v4l2_subdev_call(sd,core, init, 1);
1335
1336         cam_f.fmt.pix.width = rk29_camdev_info_ptr->icd->user_width;
1337         cam_f.fmt.pix.height = rk29_camdev_info_ptr->icd->user_height;
1338         xlate = soc_camera_xlate_by_fourcc(rk29_camdev_info_ptr->icd, rk29_camdev_info_ptr->icd->current_fmt->fourcc);
1339         cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
1340         ret |= v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1341
1342         write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)|ENABLE_CAPTURE));
1343
1344         RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor now! ret:0x%x\n",ret);
1345 }
1346 static enum hrtimer_restart rk29_camera_fps_func(struct hrtimer *timer)
1347 {
1348         RK29CAMERA_DG("rk29_camera_fps_func fps:0x%x\n",rk29_camdev_info_ptr->fps);
1349         if (rk29_camdev_info_ptr->fps < 3) {
1350                 RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor delay!\n");
1351                 INIT_WORK(&rk29_camdev_info_ptr->camera_reinit_work, rk29_camera_reinit_work);
1352                 queue_work(rk29_camdev_info_ptr->camera_wq,&(rk29_camdev_info_ptr->camera_reinit_work));
1353         }
1354
1355         return HRTIMER_NORESTART;
1356 }
1357 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable)
1358 {
1359         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1360     struct rk29_camera_dev *pcdev = ici->priv;
1361     int vip_ctrl_val;
1362         int ret;
1363
1364         WARN_ON(pcdev->icd != icd);
1365
1366         vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
1367         if (enable) {
1368                 pcdev->fps = 0;
1369                 hrtimer_cancel(&pcdev->fps_timer);
1370                 hrtimer_start(&pcdev->fps_timer,ktime_set(2, 0),HRTIMER_MODE_REL);
1371                 vip_ctrl_val |= ENABLE_CAPTURE;
1372         } else {
1373         vip_ctrl_val &= ~ENABLE_CAPTURE;
1374                 ret = hrtimer_cancel(&pcdev->fps_timer);
1375                 ret |= flush_work(&rk29_camdev_info_ptr->camera_reinit_work);
1376                 RK29CAMERA_DG("STREAM_OFF cancel timer and flush work:0x%x \n", ret);
1377         }
1378         write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
1379
1380         RK29CAMERA_DG("%s.. enable : 0x%x \n", __FUNCTION__, enable);
1381         return 0;
1382 }
1383
1384 static struct soc_camera_host_ops rk29_soc_camera_host_ops =
1385 {
1386     .owner              = THIS_MODULE,
1387     .add                = rk29_camera_add_device,
1388     .remove             = rk29_camera_remove_device,
1389     .suspend    = rk29_camera_suspend,
1390     .resume             = rk29_camera_resume,
1391     .set_crop   = rk29_camera_set_crop,
1392     .get_formats        = rk29_camera_get_formats,
1393     .put_formats        = rk29_camera_put_formats,
1394     .set_fmt    = rk29_camera_set_fmt,
1395     .try_fmt    = rk29_camera_try_fmt,
1396     .init_videobuf      = rk29_camera_init_videobuf,
1397     .reqbufs    = rk29_camera_reqbufs,
1398     .poll               = rk29_camera_poll,
1399     .querycap   = rk29_camera_querycap,
1400     .set_bus_param      = rk29_camera_set_bus_param,
1401     .s_stream = rk29_camera_s_stream   /* ddl@rock-chips.com : Add stream control for host */
1402 };
1403 static int rk29_camera_probe(struct platform_device *pdev)
1404 {
1405     struct rk29_camera_dev *pcdev;
1406     struct resource *res;
1407     int irq;
1408     int err = 0;
1409
1410     RK29CAMERA_DG("%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1411     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1412     irq = platform_get_irq(pdev, 0);
1413     if (!res || irq < 0) {
1414         err = -ENODEV;
1415         goto exit;
1416     }
1417
1418     pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1419     if (!pcdev) {
1420         dev_err(&pdev->dev, "Could not allocate pcdev\n");
1421         err = -ENOMEM;
1422         goto exit_alloc;
1423     }
1424     rk29_camdev_info_ptr = pcdev;
1425
1426     /*config output clk*/
1427         pcdev->aclk_ddr_lcdc = clk_get(&pdev->dev, "aclk_ddr_lcdc");
1428         pcdev->aclk_disp_matrix = clk_get(&pdev->dev, "aclk_disp_matrix");
1429
1430         pcdev->hclk_cpu_display = clk_get(&pdev->dev, "hclk_cpu_display");
1431         pcdev->vip_slave = clk_get(&pdev->dev, "vip_slave");
1432         pcdev->vip = clk_get(&pdev->dev,"vip");
1433         pcdev->vip_input = clk_get(&pdev->dev,"vip_input");
1434         pcdev->vip_bus = clk_get(&pdev->dev, "vip_bus");
1435
1436         pcdev->hclk_disp_matrix = clk_get(&pdev->dev,"hclk_disp_matrix");
1437         pcdev->vip_matrix = clk_get(&pdev->dev,"vip_matrix");
1438
1439     if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix ||  !pcdev->hclk_cpu_display ||
1440                 !pcdev->vip_slave || !pcdev->vip || !pcdev->vip_input || !pcdev->vip_bus ||
1441                 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) ||  IS_ERR(pcdev->hclk_cpu_display) ||
1442                 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus))  {
1443
1444         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
1445         err = -ENOENT;
1446         goto exit_reqmem;
1447     }
1448
1449         if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
1450                 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix))  {
1451
1452         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
1453         err = -ENOENT;
1454         goto exit_reqmem;
1455     }
1456
1457     dev_set_drvdata(&pdev->dev, pcdev);
1458     pcdev->res = res;
1459
1460     pcdev->pdata = pdev->dev.platform_data;             /* ddl@rock-chips.com : Request IO in init function */
1461     if (pcdev->pdata && pcdev->pdata->io_init) {
1462         pcdev->pdata->io_init();
1463     }
1464         #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1465         if (pcdev->pdata && (strcmp(pcdev->pdata->meminfo.name,"camera_ipp_mem")==0)) {
1466                 pcdev->vipmem_phybase = pcdev->pdata->meminfo.start;
1467                 pcdev->vipmem_size = pcdev->pdata->meminfo.size;
1468                 RK29CAMERA_DG("\n%s Memory(start:0x%x size:0x%x) for IPP obtain \n",__FUNCTION__, pcdev->pdata->meminfo.start,pcdev->pdata->meminfo.size);
1469         } else {
1470                 RK29CAMERA_TR("\n%s Memory for IPP have not obtain! IPP Function is fail\n",__FUNCTION__);
1471                 pcdev->vipmem_phybase = 0;
1472                 pcdev->vipmem_size = 0;
1473         }
1474         #endif
1475     INIT_LIST_HEAD(&pcdev->capture);
1476     spin_lock_init(&pcdev->lock);
1477
1478     /*
1479      * Request the regions.
1480      */
1481     if (!request_mem_region(res->start, res->end - res->start + 1,
1482                             RK29_CAM_DRV_NAME)) {
1483         err = -EBUSY;
1484         goto exit_reqmem;
1485     }
1486
1487     pcdev->base = ioremap(res->start, res->end - res->start + 1);
1488     if (pcdev->base == NULL) {
1489         dev_err(pcdev->dev, "ioremap() of registers failed\n");
1490         err = -ENXIO;
1491         goto exit_ioremap;
1492     }
1493
1494     pcdev->irq = irq;
1495     pcdev->dev = &pdev->dev;
1496
1497     /* config buffer address */
1498     /* request irq */
1499     err = request_irq(pcdev->irq, rk29_camera_irq, 0, RK29_CAM_DRV_NAME,
1500                       pcdev);
1501     if (err) {
1502         dev_err(pcdev->dev, "Camera interrupt register failed \n");
1503         goto exit_reqirq;
1504     }
1505
1506         pcdev->camera_wq = create_workqueue("camera wq");
1507         if (pcdev->camera_wq == NULL)
1508                 goto exit_free_irq;
1509         INIT_WORK(&pcdev->camera_reinit_work, rk29_camera_reinit_work);
1510
1511     pcdev->soc_host.drv_name    = RK29_CAM_DRV_NAME;
1512     pcdev->soc_host.ops         = &rk29_soc_camera_host_ops;
1513     pcdev->soc_host.priv                = pcdev;
1514     pcdev->soc_host.v4l2_dev.dev        = &pdev->dev;
1515     pcdev->soc_host.nr          = pdev->id;
1516
1517     err = soc_camera_host_register(&pcdev->soc_host);
1518     if (err)
1519         goto exit_free_irq;
1520
1521         hrtimer_init(&pcdev->fps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1522         pcdev->fps_timer.function = rk29_camera_fps_func;
1523
1524     RK29CAMERA_DG("%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1525     return 0;
1526
1527 exit_free_irq:
1528     free_irq(pcdev->irq, pcdev);
1529         if (pcdev->camera_wq) {
1530                 destroy_workqueue(pcdev->camera_wq);
1531                 pcdev->camera_wq = NULL;
1532         }
1533 exit_reqirq:
1534     iounmap(pcdev->base);
1535 exit_ioremap:
1536     release_mem_region(res->start, res->end - res->start + 1);
1537 exit_reqmem:
1538     if (pcdev->aclk_ddr_lcdc) {
1539                 clk_put(pcdev->aclk_ddr_lcdc);
1540                 pcdev->aclk_ddr_lcdc = NULL;
1541     }
1542         if (pcdev->aclk_disp_matrix) {
1543                 clk_put(pcdev->aclk_disp_matrix);
1544                 pcdev->aclk_disp_matrix = NULL;
1545     }
1546         if (pcdev->hclk_cpu_display) {
1547                 clk_put(pcdev->hclk_cpu_display);
1548                 pcdev->hclk_cpu_display = NULL;
1549     }
1550         if (pcdev->vip_slave) {
1551                 clk_put(pcdev->vip_slave);
1552                 pcdev->vip_slave = NULL;
1553     }
1554         if (pcdev->vip) {
1555                 clk_put(pcdev->vip);
1556                 pcdev->vip = NULL;
1557     }
1558         if (pcdev->vip_input) {
1559                 clk_put(pcdev->vip_input);
1560                 pcdev->vip_input = NULL;
1561     }
1562         if (pcdev->vip_bus) {
1563                 clk_put(pcdev->vip_bus);
1564                 pcdev->vip_bus = NULL;
1565     }
1566     if (pcdev->hclk_disp_matrix) {
1567                 clk_put(pcdev->hclk_disp_matrix);
1568                 pcdev->hclk_disp_matrix = NULL;
1569     }
1570         if (pcdev->vip_matrix) {
1571                 clk_put(pcdev->vip_matrix);
1572                 pcdev->vip_matrix = NULL;
1573     }
1574     kfree(pcdev);
1575 exit_alloc:
1576     rk29_camdev_info_ptr = NULL;
1577 exit:
1578     return err;
1579 }
1580
1581 static int __devexit rk29_camera_remove(struct platform_device *pdev)
1582 {
1583     struct rk29_camera_dev *pcdev = platform_get_drvdata(pdev);
1584     struct resource *res;
1585
1586     free_irq(pcdev->irq, pcdev);
1587
1588         if (pcdev->camera_wq) {
1589                 destroy_workqueue(pcdev->camera_wq);
1590                 pcdev->camera_wq = NULL;
1591         }
1592
1593     soc_camera_host_unregister(&pcdev->soc_host);
1594
1595     res = pcdev->res;
1596     release_mem_region(res->start, res->end - res->start + 1);
1597
1598     if (pcdev->pdata && pcdev->pdata->io_deinit) {         /* ddl@rock-chips.com : Free IO in deinit function */
1599         pcdev->pdata->io_deinit(0);
1600                 pcdev->pdata->io_deinit(1);
1601     }
1602
1603     kfree(pcdev);
1604     rk29_camdev_info_ptr = NULL;
1605     dev_info(&pdev->dev, "RK28 Camera driver unloaded\n");
1606
1607     return 0;
1608 }
1609
1610 static struct platform_driver rk29_camera_driver =
1611 {
1612     .driver     = {
1613         .name   = RK29_CAM_DRV_NAME,
1614     },
1615     .probe              = rk29_camera_probe,
1616     .remove             = __devexit_p(rk29_camera_remove),
1617 };
1618
1619
1620 static int __devinit rk29_camera_init(void)
1621 {
1622     RK29CAMERA_DG("%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1623     return platform_driver_register(&rk29_camera_driver);
1624 }
1625
1626 static void __exit rk29_camera_exit(void)
1627 {
1628     platform_driver_unregister(&rk29_camera_driver);
1629 }
1630
1631 device_initcall_sync(rk29_camera_init);
1632 module_exit(rk29_camera_exit);
1633
1634 MODULE_DESCRIPTION("RK29 Soc Camera Host driver");
1635 MODULE_AUTHOR("ddl <ddl@rock-chips>");
1636 MODULE_LICENSE("GPL");