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