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