Merge branch 'develop' of 192.168.1.29:/home/rockchip/kernel into develop
[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
33 #include <mach/rk29_camera.h>
34 #include <mach/rk29_iomap.h>
35 #include <mach/iomux.h>
36
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-dev.h>
39 #include <media/videobuf-dma-contig.h>
40 #include <media/soc_camera.h>
41
42 // VIP Reg Offset
43 #define RK29_VIP_AHBR_CTRL                0x00
44 #define RK29_VIP_INT_MASK                 0x04
45 #define RK29_VIP_INT_STS                  0x08
46 #define RK29_VIP_STS                      0x0c
47 #define RK29_VIP_CTRL                     0x10
48 #define RK29_VIP_CAPTURE_F1SA_Y           0x14
49 #define RK29_VIP_CAPTURE_F1SA_UV          0x18
50 #define RK29_VIP_CAPTURE_F1SA_Cr          0x1c
51 #define RK29_VIP_CAPTURE_F2SA_Y           0x20
52 #define RK29_VIP_CAPTURE_F2SA_UV          0x24
53 #define RK29_VIP_CAPTURE_F2SA_Cr          0x28
54 #define RK29_VIP_FB_SR                    0x2c
55 #define RK29_VIP_FS                       0x30
56 #define RK29_VIP_VIPRESERVED              0x34
57 #define RK29_VIP_CROP                     0x38
58 #define RK29_VIP_CRM                      0x3c
59 #define RK29_VIP_RESET                    0x40
60 #define RK29_VIP_L_SFT                    0x44
61
62 //The key register bit descrition
63 // VIP_CTRL Reg
64 #define  DISABLE_CAPTURE              (0x00<<0)
65 #define  ENABLE_CAPTURE               (0x01<<0)
66 #define  HSY_HIGH_ACTIVE              (0x00<<1)
67 #define  HSY_LOW_ACTIVE               (0x01<<1)
68 #define  VIP_CCIR656                  (0x00<<2)
69 #define  VIP_SENSOR                   (0x01<<2)
70 #define  SENSOR_UYVY                  (0x00<<3)
71 #define  SENSOR_YUYV                  (0x01<<3)
72 #define  VIP_YUV                      (0x00<<4)
73 #define  VIP_RAW                      (0x01<<4)
74 #define  CON_OR_PIN                   (0x00<<5)
75 #define  ONEFRAME                     (0x01<<5)
76 #define  VIPREGYUV420                 (0x00<<6)
77 #define  VIPREGYUV422                 (0x01<<6)
78 #define  FIELD0_START                 (0x00<<7)
79 #define  FIELD1_START                 (0x01<<7)
80 #define  CONTINUOUS                   (0x00<<8)
81 #define  PING_PONG                    (0x01<<8)
82 #define  POSITIVE_EDGE                (0x00<<9)
83 #define  NEGATIVE_EDGE                (0x01<<9)
84 #define  VIPREGNTSC                   (0x00<<10)
85 #define  VIPREGPAL                    (0x01<<10)
86 #define  VIP_DATA_LITTLEEND           (0x00<<11)
87 #define  VIP_DATA_BIGEND              (0x01<<11)
88 #define  VSY_LOW_ACTIVE               (0x00<<12)
89 #define  VSY_HIGH_ACTIVE              (0x01<<12)
90 #define  VIP_RAWINPUT_BYPASS          (0x00<<13)
91 #define  VIP_RAWINPUT_POSITIVE_EDGE   (0x01<<13)
92 #define  VIP_RAWINPUT_NEGATIVE_EDGE   (0x02<<13)
93
94 // GRF_SOC_CON0 Reg
95 #define  GRF_SOC_CON0_Reg             0xbc
96 #define  VIP_AXIMASTER                (0x00<<0)
97 #define  VIP_AHBMASTER                (0x01<<2)
98
99 // GRF_OS_REG0
100 #define  GRF_OS_REG0                  0xd0
101 #define  VIP_ACLK_DIV_HCLK_1          (0x00<<0)
102 #define  VIP_ACLK_DIV_HCLK_2          (0x01<<0)
103
104
105 #define MIN(x,y)   ((x<y) ? x: y)
106 #define MAX(x,y)    ((x>y) ? x: y)
107 #define RK29_SENSOR_24MHZ      24           /* MHz */
108 #define RK29_SENSOR_48MHZ      48
109
110 #define write_vip_reg(addr, val)  __raw_writel(val, addr+(rk29_camdev_info_ptr->base))
111 #define read_vip_reg(addr) __raw_readl(addr+(rk29_camdev_info_ptr->base))
112 #define mask_vip_reg(addr, msk, val)    write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
113
114 #define write_grf_reg(addr, val)  __raw_writel(val, addr+RK29_GRF_BASE)
115 #define read_grf_reg(addr) __raw_readl(addr+RK29_GRF_BASE)
116 #define mask_grf_reg(addr, msk, val)    write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
117
118 //Configure Macro
119 #define CONFIG_RK29CAMERA_TR      1
120 #define CONFIG_RK29CAMERA_DEBUG   0
121 #if (CONFIG_RK29CAMERA_TR)
122         #define RK29CAMERA_TR(format, ...)      printk(format, ## __VA_ARGS__)
123         #if (CONFIG_RK29CAMERA_DEBUG)
124         #define RK29CAMERA_DG(format, ...)      printk(format, ## __VA_ARGS__)
125         #else
126         #define RK29CAMERA_DG(format, ...)
127         #endif
128 #else
129         #define RK29CAMERA_TR(format, ...)
130         #define RK29CAMERA_DG(format, ...)
131 #endif
132
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 1          /* ddl@rock-chips.com :  */
154
155
156
157 extern void videobuf_dma_contig_free(struct videobuf_queue *q, struct videobuf_buffer *buf);
158 extern dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
159 extern void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
160             struct videobuf_queue_ops *ops,
161             struct device *dev,
162             spinlock_t *irqlock,
163             enum v4l2_buf_type type,
164             enum v4l2_field field,
165             unsigned int msize,
166             void *priv);
167
168 /* buffer for one video frame */
169 struct rk29_buffer
170 {
171     /* common v4l buffer stuff -- must be first */
172     struct videobuf_buffer vb;
173     const struct soc_camera_data_format        *fmt;
174     int                 inwork;
175 };
176 enum rk29_camera_reg_state
177 {
178         Reg_Invalidate,
179         Reg_Validate
180 };
181
182 struct rk29_camera_reg
183 {
184         unsigned int VipCtrl;
185         unsigned int VipCrop;
186         unsigned int VipFs;
187         unsigned int VipIntMsk;
188         unsigned int VipCrm;
189         enum rk29_camera_reg_state Inval;
190 };
191 struct rk29_camera_dev
192 {
193     struct soc_camera_host      soc_host;
194     struct device               *dev;
195     /* RK2827x is only supposed to handle one camera on its Quick Capture
196      * interface. If anyone ever builds hardware to enable more than
197      * one camera, they will have to modify this driver too */
198     struct soc_camera_device *icd;
199     struct clk *clk;
200     void __iomem *base;
201         void __iomem *grf_base;
202     int frame_inval;           /* ddl@rock-chips.com : The first frames is invalidate  */
203     unsigned int irq;
204
205     struct rk29camera_platform_data *pdata;
206     struct resource             *res;
207
208     struct list_head    capture;
209
210     spinlock_t          lock;
211
212     struct videobuf_buffer      *active;
213         struct videobuf_queue *vb_vidq_ptr;
214         struct rk29_camera_reg reginfo_suspend;
215 };
216 static DEFINE_MUTEX(camera_lock);
217 static const char *rk29_cam_driver_description = "RK29_Camera";
218 static struct rk29_camera_dev *rk29_camdev_info_ptr;
219
220 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable);
221
222
223 /*
224  *  Videobuf operations
225  */
226 static int rk29_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
227                                unsigned int *size)
228 {
229     struct soc_camera_device *icd = vq->priv_data;
230     int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
231
232     dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
233
234     /* planar capture requires Y, U and V buffers to be page aligned */
235     *size = PAGE_ALIGN( icd->user_width * icd->user_height * bytes_per_pixel);                               /* Y pages UV pages, yuv422*/
236
237     RK29CAMERA_DG("\n%s..%d.. size = %d\n",__FUNCTION__,__LINE__, *size);
238
239     return 0;
240 }
241 static void rk29_videobuf_free(struct videobuf_queue *vq, struct rk29_buffer *buf)
242 {
243     struct soc_camera_device *icd = vq->priv_data;
244
245     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
246             &buf->vb, buf->vb.baddr, buf->vb.bsize);
247
248         /* ddl@rock-chips.com: buf_release called soc_camera_streamoff and soc_camera_close*/
249         if (buf->vb.state == VIDEOBUF_NEEDS_INIT)
250                 return;
251
252     if (in_interrupt())
253         BUG();
254
255     videobuf_dma_contig_free(vq, &buf->vb);
256     dev_dbg(&icd->dev, "%s freed\n", __func__);
257     buf->vb.state = VIDEOBUF_NEEDS_INIT;
258         return;
259 }
260 static int rk29_videobuf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field)
261 {
262     struct soc_camera_device *icd = vq->priv_data;
263     struct rk29_buffer *buf;
264     int ret;
265
266     buf = container_of(vb, struct rk29_buffer, vb);
267
268     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
269             vb, vb->baddr, vb->bsize);
270
271     //RK29CAMERA_TR("\n%s..%d..  \n",__FUNCTION__,__LINE__);
272
273     /* Added list head initialization on alloc */
274     WARN_ON(!list_empty(&vb->queue));
275
276     /* This can be useful if you want to see if we actually fill
277      * the buffer with something */
278     //memset((void *)vb->baddr, 0xaa, vb->bsize);
279
280     BUG_ON(NULL == icd->current_fmt);
281
282     if (buf->fmt    != icd->current_fmt ||
283             vb->width   != icd->user_width ||
284             vb->height  != icd->user_height ||
285              vb->field   != field) {
286         buf->fmt    = icd->current_fmt;
287         vb->width   = icd->user_width;
288         vb->height  = icd->user_height;
289         vb->field   = field;
290         vb->state   = VIDEOBUF_NEEDS_INIT;
291     }
292
293     vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3) ;          /* ddl@rock-chips.com : fmt->depth is coorect */
294     if (0 != vb->baddr && vb->bsize < vb->size) {
295         ret = -EINVAL;
296         goto out;
297     }
298
299     if (vb->state == VIDEOBUF_NEEDS_INIT) {
300         ret = videobuf_iolock(vq, vb, NULL);
301         if (ret) {
302             goto fail;
303         }
304         vb->state = VIDEOBUF_PREPARED;
305     }
306     //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
307     return 0;
308 fail:
309     rk29_videobuf_free(vq, buf);
310 out:
311     return ret;
312 }
313
314 static inline void rk29_videobuf_capture(struct videobuf_buffer *vb)
315 {
316     unsigned int size;
317
318     if (vb) {
319         size = vb->width * vb->height; /* Y pages UV pages, yuv422*/
320         write_vip_reg(RK29_VIP_CAPTURE_F1SA_Y, vb->boff);
321         write_vip_reg(RK29_VIP_CAPTURE_F1SA_UV, vb->boff + size);
322         write_vip_reg(RK29_VIP_CAPTURE_F2SA_Y, vb->boff);
323         write_vip_reg(RK29_VIP_CAPTURE_F2SA_UV, vb->boff + size);
324         write_vip_reg(RK29_VIP_FB_SR,  0x00000002);//frame1 has been ready to receive data,frame 2 is not used
325     }
326 }
327 /* Locking: Caller holds q->irqlock */
328 static void rk29_videobuf_queue(struct videobuf_queue *vq,
329                                 struct videobuf_buffer *vb)
330 {
331     struct soc_camera_device *icd = vq->priv_data;
332     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
333     struct rk29_camera_dev *pcdev = ici->priv;
334
335     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
336             vb, vb->baddr, vb->bsize);
337
338     vb->state = VIDEOBUF_QUEUED;
339
340         if (!list_empty(&pcdev->capture)) {
341                 list_add_tail(&vb->queue, &pcdev->capture);
342         } else {
343                 if (list_entry(pcdev->capture.next, struct videobuf_buffer, queue) != vb)
344                         list_add_tail(&vb->queue, &pcdev->capture);
345                 else
346                         BUG();    /* ddl@rock-chips.com : The same videobuffer queue again */
347         }
348
349     if (!pcdev->active) {
350         pcdev->active = vb;
351         rk29_videobuf_capture(vb);
352     }
353 }
354 static irqreturn_t rk29_camera_irq(int irq, void *data)
355 {
356     struct rk29_camera_dev *pcdev = data;
357     struct videobuf_buffer *vb;
358
359     read_vip_reg(RK29_VIP_INT_STS);    /* clear vip interrupte single  */
360
361     /* ddl@rock-chps.com : Current VIP is run in One Frame Mode, Frame 1 is validate */
362     if (read_vip_reg(RK29_VIP_FB_SR) & 0x01) {
363
364                 if (!pcdev->active)
365                         goto RK29_CAMERA_IRQ_END;
366
367         if (pcdev->frame_inval>0) {
368             pcdev->frame_inval--;
369             rk29_videobuf_capture(pcdev->active);
370             goto RK29_CAMERA_IRQ_END;
371         } else if (pcdev->frame_inval) {
372                 RK29CAMERA_TR("frame_inval : %0x",pcdev->frame_inval);
373             pcdev->frame_inval = 0;
374         }
375
376         vb = pcdev->active;
377                 /* ddl@rock-chips.com : this vb may be deleted from queue */
378                 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
379                 list_del_init(&vb->queue);
380                 }
381
382         pcdev->active = NULL;
383         if (!list_empty(&pcdev->capture)) {
384             pcdev->active = list_entry(pcdev->capture.next, struct videobuf_buffer, queue);
385                         if (pcdev->active) {
386                                 rk29_videobuf_capture(pcdev->active);
387                         }
388         }
389
390         if (pcdev->active == NULL) {
391                         RK29CAMERA_DG("%s video_buf queue is empty!\n",__FUNCTION__);
392         }
393
394                 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
395                 vb->state = VIDEOBUF_DONE;
396                 do_gettimeofday(&vb->ts);
397                 vb->field_count++;
398                 }
399
400         wake_up(&vb->done);
401     }
402
403 RK29_CAMERA_IRQ_END:
404     return IRQ_HANDLED;
405 }
406
407
408 static void rk29_videobuf_release(struct videobuf_queue *vq,
409                                   struct videobuf_buffer *vb)
410 {
411     struct rk29_buffer *buf = container_of(vb, struct rk29_buffer, vb);
412     struct soc_camera_device *icd = vq->priv_data;
413     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
414     struct rk29_camera_dev *pcdev = ici->priv;
415
416 #ifdef DEBUG
417     dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
418             vb, vb->baddr, vb->bsize);
419
420     switch (vb->state)
421     {
422         case VIDEOBUF_ACTIVE:
423             dev_dbg(&icd->dev, "%s (active)\n", __func__);
424             break;
425         case VIDEOBUF_QUEUED:
426             dev_dbg(&icd->dev, "%s (queued)\n", __func__);
427             break;
428         case VIDEOBUF_PREPARED:
429             dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
430             break;
431         default:
432             dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
433             break;
434     }
435 #endif
436         if (vb == pcdev->active) {
437                 RK29CAMERA_DG("%s Wait for this video buf(0x%x) write finished!\n ",__FUNCTION__,(unsigned int)vb);
438                 interruptible_sleep_on_timeout(&vb->done, 100);
439                 RK29CAMERA_DG("%s This video buf(0x%x) write finished, release now!!\n",__FUNCTION__,(unsigned int)vb);
440         }
441     rk29_videobuf_free(vq, buf);
442 }
443
444 static struct videobuf_queue_ops rk29_videobuf_ops =
445 {
446     .buf_setup      = rk29_videobuf_setup,
447     .buf_prepare    = rk29_videobuf_prepare,
448     .buf_queue      = rk29_videobuf_queue,
449     .buf_release    = rk29_videobuf_release,
450 };
451
452 static void rk29_camera_init_videobuf(struct videobuf_queue *q,
453                                       struct soc_camera_device *icd)
454 {
455     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
456     struct rk29_camera_dev *pcdev = ici->priv;
457
458     /* We must pass NULL as dev pointer, then all pci_* dma operations
459      * transform to normal dma_* ones. */
460     videobuf_queue_dma_contig_init(q,
461                                    &rk29_videobuf_ops,
462                                    ici->v4l2_dev.dev, &pcdev->lock,
463                                    V4L2_BUF_TYPE_VIDEO_CAPTURE,
464                                    V4L2_FIELD_NONE,
465                                    sizeof(struct rk29_buffer),
466                                    icd);
467         pcdev->vb_vidq_ptr = q;         /* ddl@rock-chips.com */
468 }
469 static int rk29_camera_activate(struct rk29_camera_dev *pcdev, struct soc_camera_device *icd)
470 {
471     unsigned long sensor_bus_flags = SOCAM_MCLK_24MHZ;
472     struct clk *parent;
473
474     RK29CAMERA_DG("\n%s..%d.. \n",__FUNCTION__,__LINE__);
475     if (!pcdev->clk || IS_ERR(pcdev->clk))
476         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk source\n");
477
478     //if (icd->ops->query_bus_param)                                                  /* ddl@rock-chips.com : Query Sensor's xclk */
479         //sensor_bus_flags = icd->ops->query_bus_param(icd);
480
481     if (sensor_bus_flags & SOCAM_MCLK_48MHZ) {
482         parent = clk_get(NULL, "clk48m");
483         if (!parent || IS_ERR(parent))
484              goto RK29_CAMERA_ACTIVE_ERR;
485     } else if (sensor_bus_flags & SOCAM_MCLK_27MHZ) {
486         parent = clk_get(NULL, "extclk");
487         if (!parent || IS_ERR(parent))
488              goto RK29_CAMERA_ACTIVE_ERR;
489     } else {
490         parent = clk_get(NULL, "xin24m");
491         if (!parent || IS_ERR(parent))
492              goto RK29_CAMERA_ACTIVE_ERR;
493     }
494
495     clk_set_parent(pcdev->clk, parent);
496
497     clk_enable(pcdev->clk);
498     rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_VIP_CLKOUT);
499     ndelay(10);
500
501         //write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)|VIP_AHBMASTER);  //VIP Config to AHB
502         write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)&(~VIP_AHBMASTER));  //VIP Config to AXI
503         write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)&(~VIP_ACLK_DIV_HCLK_2));   //aclk:hclk = 1:1
504         //write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)|VIP_ACLK_DIV_HCLK_2);   //aclk:hclk = 2:1
505
506     write_vip_reg(RK29_VIP_RESET, 0x76543210);  /* ddl@rock-chips.com : vip software reset */
507     udelay(10);
508
509     write_vip_reg(RK29_VIP_AHBR_CTRL, 0x07);   /* ddl@rock-chips.com : vip ahb burst 16 */
510     write_vip_reg(RK29_VIP_INT_MASK, 0x01);    //capture complete interrupt enable
511     write_vip_reg(RK29_VIP_CRM,  0x00000000);  //Y/CB/CR color modification
512
513     return 0;
514 RK29_CAMERA_ACTIVE_ERR:
515     return -ENODEV;
516 }
517
518 static void rk29_camera_deactivate(struct rk29_camera_dev *pcdev)
519 {
520     pcdev->active = NULL;
521
522     write_vip_reg(RK29_VIP_CTRL, 0);
523     read_vip_reg(RK29_VIP_INT_STS);             //clear vip interrupte single
524
525     rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_GPIO1B4);
526     clk_disable(pcdev->clk);
527
528     return;
529 }
530
531 /* The following two functions absolutely depend on the fact, that
532  * there can be only one camera on RK28 quick capture interface */
533 static int rk29_camera_add_device(struct soc_camera_device *icd)
534 {
535     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
536     struct rk29_camera_dev *pcdev = ici->priv;
537     struct device *control = to_soc_camera_control(icd);
538     struct v4l2_subdev *sd;
539     int ret;
540
541     mutex_lock(&camera_lock);
542
543     if (pcdev->icd) {
544         ret = -EBUSY;
545         goto ebusy;
546     }
547
548     dev_info(&icd->dev, "RK29 Camera driver attached to camera %d\n",
549              icd->devnum);
550
551         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
552     pcdev->active = NULL;
553     pcdev->icd = NULL;
554         pcdev->reginfo_suspend.Inval = Reg_Invalidate;
555         /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
556      * if app havn't dequeue all videobuf before close camera device;
557         */
558     INIT_LIST_HEAD(&pcdev->capture);
559
560     ret = rk29_camera_activate(pcdev,icd);
561     if (ret)
562         goto ebusy;
563
564     /* ddl@rock-chips.com : v4l2_subdev is not created when ici->ops->add called in soc_camera_probe  */
565     if (control) {
566         sd = dev_get_drvdata(control);
567         ret = v4l2_subdev_call(sd,core, init, 0);
568         if (ret)
569             goto ebusy;
570     }
571
572     pcdev->icd = icd;
573
574 ebusy:
575     mutex_unlock(&camera_lock);
576
577     return ret;
578 }
579 static void rk29_camera_remove_device(struct soc_camera_device *icd)
580 {
581     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
582     struct rk29_camera_dev *pcdev = ici->priv;
583
584     BUG_ON(icd != pcdev->icd);
585
586     dev_info(&icd->dev, "RK29 Camera driver detached from camera %d\n",
587              icd->devnum);
588
589         rk29_camera_deactivate(pcdev);
590
591         /* ddl@rock-chips.com: Call videobuf_mmap_free here for free the struct video_buffer which malloc in videobuf_alloc */
592         if (pcdev->vb_vidq_ptr) {
593                 videobuf_mmap_free(pcdev->vb_vidq_ptr);
594                 pcdev->vb_vidq_ptr = NULL;
595         }
596
597         pcdev->active = NULL;
598     pcdev->icd = NULL;
599         pcdev->reginfo_suspend.Inval = Reg_Invalidate;
600         /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
601      * if app havn't dequeue all videobuf before close camera device;
602         */
603     INIT_LIST_HEAD(&pcdev->capture);
604
605         return;
606 }
607
608 static int rk29_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
609 {
610     unsigned long bus_flags, camera_flags, common_flags;
611     unsigned int vip_ctrl_val = 0;
612     int ret = 0;
613
614     RK29CAMERA_DG("\n%s..%d..\n",__FUNCTION__,__LINE__);
615
616     bus_flags = RK29_CAM_BUS_PARAM;
617         if (icd->ops->query_bus_param)
618         camera_flags = icd->ops->query_bus_param(icd);
619         else
620                 camera_flags = 0;
621
622     common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
623     if (!common_flags) {
624         ret = -EINVAL;
625         goto RK29_CAMERA_SET_BUS_PARAM_END;
626     }
627
628     ret = icd->ops->set_bus_param(icd, common_flags);
629     if (ret < 0)
630         goto RK29_CAMERA_SET_BUS_PARAM_END;
631
632         if (common_flags & SOCAM_DATAWIDTH_8) {
633         icd->buswidth = 8;
634         } else if (common_flags & SOCAM_DATAWIDTH_10) {
635             icd->buswidth = 10;
636         }
637
638     vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
639     if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) {
640         vip_ctrl_val |= NEGATIVE_EDGE;
641     } else {
642                 vip_ctrl_val &= ~NEGATIVE_EDGE;
643     }
644     if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) {
645         vip_ctrl_val |= HSY_LOW_ACTIVE;
646     } else {
647                 vip_ctrl_val &= ~HSY_LOW_ACTIVE;
648     }
649     if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) {
650         vip_ctrl_val |= VSY_HIGH_ACTIVE;
651     } else {
652                 vip_ctrl_val &= ~VSY_HIGH_ACTIVE;
653     }
654
655     /* ddl@rock-chips.com : Don't enable capture here, enable in stream_on */
656     //vip_ctrl_val |= ENABLE_CAPTURE;
657
658     write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
659     RK29CAMERA_DG("\n%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),
660                 read_grf_reg(GRF_SOC_CON0_Reg)&VIP_AHBMASTER, read_grf_reg(GRF_OS_REG0)&VIP_ACLK_DIV_HCLK_2);
661
662 RK29_CAMERA_SET_BUS_PARAM_END:
663         if (ret)
664         RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
665     return ret;
666 }
667
668 static int rk29_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
669 {
670     unsigned long bus_flags, camera_flags;
671     int ret;
672
673     bus_flags = RK29_CAM_BUS_PARAM;
674         if (icd->ops->query_bus_param) {
675         camera_flags = icd->ops->query_bus_param(icd);
676         } else {
677                 camera_flags = 0;
678         }
679     ret = soc_camera_bus_param_compatible(camera_flags, bus_flags) ;
680
681     if (ret < 0)
682         dev_warn(icd->dev.parent,
683                          "Flags incompatible: camera %lx, host %lx\n",
684                          camera_flags, bus_flags);
685     return ret;
686 }
687 static const struct soc_camera_data_format rk29_camera_formats[] = {
688         {
689                 .name           = "Planar YUV420 12 bit",
690                 .depth          = 12,
691                 .fourcc         = V4L2_PIX_FMT_YUV420,
692                 .colorspace     = V4L2_COLORSPACE_JPEG,
693         },{
694                 .name           = "Planar YUV422 16 bit",
695                 .depth          = 16,
696                 .fourcc         = V4L2_PIX_FMT_YUV422P,
697                 .colorspace     = V4L2_COLORSPACE_JPEG,
698         },{
699                 .name           = "Raw Bayer RGB 10 bit",
700                 .depth          = 16,
701                 .fourcc         = V4L2_PIX_FMT_SGRBG10,
702                 .colorspace     = V4L2_COLORSPACE_SRGB,
703         }
704 };
705
706 static void rk29_camera_setup_format(struct soc_camera_device *icd, __u32 host_pixfmt, __u32 cam_pixfmt, struct v4l2_rect *rect)
707 {
708         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
709     struct rk29_camera_dev *pcdev = ici->priv;
710     unsigned int vip_fs = 0,vip_crop = 0;
711     unsigned int vip_ctrl_val = VIP_SENSOR|ONEFRAME|DISABLE_CAPTURE;
712
713     switch (host_pixfmt)
714     {
715         case V4L2_PIX_FMT_YUV422P:
716             vip_ctrl_val |= VIPREGYUV422;
717                         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
718             break;
719         case V4L2_PIX_FMT_YUV420:
720             vip_ctrl_val |= VIPREGYUV420;
721                         if (pcdev->frame_inval != RK29_CAM_FRAME_INVAL_INIT)
722                                 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
723             break;
724                 case V4L2_PIX_FMT_SGRBG10:
725                         vip_ctrl_val |= (VIP_RAW | VIP_SENSOR | VIP_DATA_LITTLEEND);
726                         pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
727                         break;
728         default:                                                                                /* ddl@rock-chips.com : vip output format is hold when pixfmt is invalidate */
729             vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & VIPREGYUV422);
730             break;
731     }
732
733     switch (cam_pixfmt)
734     {
735         case V4L2_PIX_FMT_UYVY:
736             vip_ctrl_val |= SENSOR_UYVY;
737             break;
738         case V4L2_PIX_FMT_YUYV:
739             vip_ctrl_val |= SENSOR_YUYV;
740             break;
741         default :
742             vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & SENSOR_YUYV);
743             break;
744     }
745
746     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 */
747
748     read_vip_reg(RK29_VIP_INT_STS);                     /* clear vip interrupte single  */
749
750     if (vip_ctrl_val & ONEFRAME)  {
751         vip_crop = ((rect->left<<16) + rect->top);
752         vip_fs  = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
753     } else if (vip_ctrl_val & PING_PONG) {
754         if (rect->left ||rect->top ) {
755                         RK29CAMERA_DG("\n %s..PingPang not support Crop \n",__FUNCTION__);
756                         BUG();
757         }
758                 vip_crop = 0;
759         vip_fs  = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
760     }
761
762     write_vip_reg(RK29_VIP_CROP, vip_crop);
763     write_vip_reg(RK29_VIP_FS, vip_fs);
764
765     write_vip_reg(RK29_VIP_FB_SR,  0x00000003);
766
767     RK29CAMERA_DG("\n%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));
768         return;
769 }
770
771 static int rk29_camera_get_formats(struct soc_camera_device *icd, int idx,
772                                   struct soc_camera_format_xlate *xlate)
773 {
774     struct device *dev = icd->dev.parent;
775     int formats = 0, buswidth, ret;
776
777     buswidth = 8;
778
779     ret = rk29_camera_try_bus_param(icd, buswidth);
780     if (ret < 0)
781         return 0;
782
783     switch (icd->formats[idx].fourcc) {
784         case V4L2_PIX_FMT_UYVY:
785         case V4L2_PIX_FMT_YUYV:
786             formats++;
787             if (xlate) {
788                 xlate->host_fmt = &rk29_camera_formats[0];
789                 xlate->cam_fmt = icd->formats + idx;
790                 xlate->buswidth = buswidth;
791                 xlate++;
792                 dev_dbg(dev, "Providing format %s using %s\n",
793                         rk29_camera_formats[0].name,
794                         icd->formats[idx].name);
795             }
796
797             formats++;
798             if (xlate) {
799                 xlate->host_fmt = &rk29_camera_formats[1];
800                 xlate->cam_fmt = icd->formats + idx;
801                 xlate->buswidth = buswidth;
802                 xlate++;
803                 dev_dbg(dev, "Providing format %s using %s\n",
804                         rk29_camera_formats[1].name,
805                         icd->formats[idx].name);
806             }
807                         break;
808                 case V4L2_PIX_FMT_SGRBG10:
809                         formats++;
810             if (xlate) {
811                 xlate->host_fmt = &rk29_camera_formats[2];
812                 xlate->cam_fmt = icd->formats + idx;
813                 xlate->buswidth = 10;
814                 xlate++;
815                 dev_dbg(dev, "Providing format %s using %s\n",
816                         rk29_camera_formats[2].name,
817                         icd->formats[idx].name);
818             }
819                         break;
820         default:
821             break;
822     }
823
824     return formats;
825 }
826
827 static void rk29_camera_put_formats(struct soc_camera_device *icd)
828 {
829         return;
830 }
831
832 static int rk29_camera_set_crop(struct soc_camera_device *icd,
833                                struct v4l2_crop *a)
834 {
835     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
836     struct v4l2_format f;
837     struct v4l2_pix_format *pix = &f.fmt.pix;
838     int ret;
839
840     f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
841
842     ret = v4l2_subdev_call(sd, video, g_fmt, &f);
843     if (ret < 0)
844         return ret;
845
846     if ((pix->width < (a->c.left + a->c.width)) || (pix->height < (a->c.top + a->c.height)))  {
847
848         pix->width = a->c.left + a->c.width;
849         pix->height = a->c.top + a->c.height;
850
851         v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
852             &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
853             icd->current_fmt->fourcc == V4L2_PIX_FMT_YUV422P ?4 : 0);
854
855         ret = v4l2_subdev_call(sd, video, s_fmt, &f);
856         if (ret < 0)
857             return ret;
858     }
859
860     rk29_camera_setup_format(icd, icd->current_fmt->fourcc, pix->pixelformat, &a->c);
861
862     icd->user_width = pix->width;
863     icd->user_height = pix->height;
864
865     return 0;
866 }
867
868 static int rk29_camera_set_fmt(struct soc_camera_device *icd,
869                               struct v4l2_format *f)
870 {
871     struct device *dev = icd->dev.parent;
872     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
873     const struct soc_camera_data_format *cam_fmt = NULL;
874     const struct soc_camera_format_xlate *xlate = NULL;
875     struct v4l2_pix_format *pix = &f->fmt.pix;
876     struct v4l2_format cam_f = *f;
877     struct v4l2_rect rect;
878     int ret;
879
880     RK29CAMERA_DG("\n%s..%d..  \n",__FUNCTION__,__LINE__);
881
882     xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
883     if (!xlate) {
884         dev_err(dev, "Format %x not found\n", pix->pixelformat);
885         ret = -EINVAL;
886         goto RK29_CAMERA_SET_FMT_END;
887     }
888
889     cam_fmt = xlate->cam_fmt;
890
891     cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
892     ret = v4l2_subdev_call(sd, video, s_fmt, &cam_f);
893     cam_f.fmt.pix.pixelformat = pix->pixelformat;
894     *pix = cam_f.fmt.pix;
895
896     icd->sense = NULL;
897
898     if (!ret) {
899         rect.left = 0;
900         rect.top = 0;
901         rect.width = pix->width;
902         rect.height = pix->height;
903
904         RK29CAMERA_DG("\n%s..%s..%s \n",__FUNCTION__,xlate->host_fmt->name, cam_fmt->name);
905         rk29_camera_setup_format(icd, pix->pixelformat, cam_fmt->fourcc, &rect);
906
907         icd->buswidth = xlate->buswidth;
908         icd->current_fmt = xlate->host_fmt;
909     }
910
911 RK29_CAMERA_SET_FMT_END:
912         if (ret)
913         RK29CAMERA_TR("\n%s..%d.. ret = %d  \n",__FUNCTION__,__LINE__, ret);
914     return ret;
915 }
916
917 static int rk29_camera_try_fmt(struct soc_camera_device *icd,
918                                    struct v4l2_format *f)
919 {
920     struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
921     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
922     const struct soc_camera_format_xlate *xlate;
923     struct v4l2_pix_format *pix = &f->fmt.pix;
924     __u32 pixfmt = pix->pixelformat;
925     enum v4l2_field field;
926     int ret;
927
928     RK29CAMERA_DG("\n%s..%d.. \n",__FUNCTION__,__LINE__);
929
930     xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
931     if (!xlate) {
932         dev_err(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
933         ret = -EINVAL;
934         goto RK29_CAMERA_TRY_FMT_END;
935     }
936    /* limit to rk29 hardware capabilities */
937     v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
938               &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
939               pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
940
941     pix->bytesperline = pix->width * DIV_ROUND_UP(xlate->host_fmt->depth, 8);
942     pix->sizeimage = pix->height * pix->bytesperline;
943
944     /* camera has to see its format, but the user the original one */
945     pix->pixelformat = xlate->cam_fmt->fourcc;
946     /* limit to sensor capabilities */
947     ret = v4l2_subdev_call(sd, video, try_fmt, f);
948     pix->pixelformat = pixfmt;
949
950     field = pix->field;
951
952     if (field == V4L2_FIELD_ANY) {
953         pix->field = V4L2_FIELD_NONE;
954     } else if (field != V4L2_FIELD_NONE) {
955         dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
956         ret = -EINVAL;
957         goto RK29_CAMERA_TRY_FMT_END;
958     }
959
960 RK29_CAMERA_TRY_FMT_END:
961         if (ret)
962         RK29CAMERA_TR("\n%s..%d.. ret = %d  \n",__FUNCTION__,__LINE__, ret);
963     return ret;
964 }
965
966 static int rk29_camera_reqbufs(struct soc_camera_file *icf,
967                                struct v4l2_requestbuffers *p)
968 {
969     int i;
970
971     /* This is for locking debugging only. I removed spinlocks and now I
972      * check whether .prepare is ever called on a linked buffer, or whether
973      * a dma IRQ can occur for an in-work or unlinked buffer. Until now
974      * it hadn't triggered */
975     for (i = 0; i < p->count; i++) {
976         struct rk29_buffer *buf = container_of(icf->vb_vidq.bufs[i],
977                                                            struct rk29_buffer, vb);
978         buf->inwork = 0;
979         INIT_LIST_HEAD(&buf->vb.queue);
980     }
981
982     return 0;
983 }
984
985 static unsigned int rk29_camera_poll(struct file *file, poll_table *pt)
986 {
987     struct soc_camera_file *icf = file->private_data;
988     struct rk29_buffer *buf;
989
990     buf = list_entry(icf->vb_vidq.stream.next, struct rk29_buffer,
991                      vb.stream);
992
993     poll_wait(file, &buf->vb.done, pt);
994
995     if (buf->vb.state == VIDEOBUF_DONE ||
996             buf->vb.state == VIDEOBUF_ERROR)
997         return POLLIN|POLLRDNORM;
998
999     return 0;
1000 }
1001
1002 static int rk29_camera_querycap(struct soc_camera_host *ici,
1003                                 struct v4l2_capability *cap)
1004 {
1005     /* cap->name is set by the firendly caller:-> */
1006     strlcpy(cap->card, rk29_cam_driver_description, sizeof(cap->card));
1007     cap->version = RK29_CAM_VERSION_CODE;
1008     cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1009
1010     return 0;
1011 }
1012
1013 static int rk29_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1014 {
1015     struct soc_camera_host *ici =
1016                     to_soc_camera_host(icd->dev.parent);
1017     struct rk29_camera_dev *pcdev = ici->priv;
1018     int ret = 0,tmp;
1019
1020         if ((pcdev->icd == icd) && (icd->ops->suspend)) {
1021                 rk29_camera_s_stream(icd, 0);
1022                 ret = icd->ops->suspend(icd, state);
1023
1024                 pcdev->reginfo_suspend.VipCtrl = read_vip_reg(RK29_VIP_CTRL);
1025                 pcdev->reginfo_suspend.VipCrop = read_vip_reg(RK29_VIP_CROP);
1026                 pcdev->reginfo_suspend.VipFs = read_vip_reg(RK29_VIP_FS);
1027                 pcdev->reginfo_suspend.VipIntMsk = read_vip_reg(RK29_VIP_INT_MASK);
1028                 pcdev->reginfo_suspend.VipCrm = read_vip_reg(RK29_VIP_CRM);
1029
1030                 tmp = pcdev->reginfo_suspend.VipFs>>16;         /* ddl@rock-chips.com */
1031                 tmp += pcdev->reginfo_suspend.VipCrop>>16;
1032                 pcdev->reginfo_suspend.VipFs = (pcdev->reginfo_suspend.VipFs & 0xffff) | (tmp<<16);
1033
1034                 pcdev->reginfo_suspend.Inval = Reg_Validate;
1035                 rk29_camera_deactivate(pcdev);
1036
1037                 RK29CAMERA_DG("%s Enter Success...\n", __FUNCTION__);
1038         } else {
1039                 RK29CAMERA_DG("%s icd has been deattach, don't need enter suspend\n", __FUNCTION__);
1040         }
1041
1042     return ret;
1043 }
1044
1045 static int rk29_camera_resume(struct soc_camera_device *icd)
1046 {
1047     struct soc_camera_host *ici =
1048                     to_soc_camera_host(icd->dev.parent);
1049     struct rk29_camera_dev *pcdev = ici->priv;
1050     int ret = 0;
1051
1052         if ((pcdev->icd == icd) && (icd->ops->resume)) {
1053                 if (pcdev->reginfo_suspend.Inval == Reg_Validate) {
1054                         rk29_camera_activate(pcdev, icd);
1055                         write_vip_reg(RK29_VIP_INT_MASK, pcdev->reginfo_suspend.VipIntMsk);
1056                         write_vip_reg(RK29_VIP_CRM, pcdev->reginfo_suspend.VipCrm);
1057                         write_vip_reg(RK29_VIP_CTRL, pcdev->reginfo_suspend.VipCtrl&~ENABLE_CAPTURE);
1058                         write_vip_reg(RK29_VIP_CROP, pcdev->reginfo_suspend.VipCrop);
1059                         write_vip_reg(RK29_VIP_FS, pcdev->reginfo_suspend.VipFs);
1060
1061                         rk29_videobuf_capture(pcdev->active);
1062                         rk29_camera_s_stream(icd, 1);
1063                 }
1064                 ret = icd->ops->resume(icd);
1065
1066                 RK29CAMERA_DG("%s Enter success\n",__FUNCTION__);
1067         } else {
1068                 RK29CAMERA_DG("%s icd has been deattach, don't need enter resume\n", __FUNCTION__);
1069         }
1070
1071     return ret;
1072 }
1073
1074 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable)
1075 {
1076         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1077     struct rk29_camera_dev *pcdev = ici->priv;
1078     int vip_ctrl_val;
1079
1080         WARN_ON(pcdev->icd != icd);
1081
1082         vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
1083         if (enable) {
1084                 vip_ctrl_val |= ENABLE_CAPTURE;
1085         } else {
1086         vip_ctrl_val &= ~ENABLE_CAPTURE;
1087         }
1088         write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
1089
1090         RK29CAMERA_DG("%s.. enable : %d\n", __FUNCTION__, enable);
1091         return 0;
1092 }
1093
1094 static struct soc_camera_host_ops rk29_soc_camera_host_ops =
1095 {
1096     .owner              = THIS_MODULE,
1097     .add                = rk29_camera_add_device,
1098     .remove             = rk29_camera_remove_device,
1099     .suspend    = rk29_camera_suspend,
1100     .resume             = rk29_camera_resume,
1101     .set_crop   = rk29_camera_set_crop,
1102     .get_formats        = rk29_camera_get_formats,
1103     .put_formats        = rk29_camera_put_formats,
1104     .set_fmt    = rk29_camera_set_fmt,
1105     .try_fmt    = rk29_camera_try_fmt,
1106     .init_videobuf      = rk29_camera_init_videobuf,
1107     .reqbufs    = rk29_camera_reqbufs,
1108     .poll               = rk29_camera_poll,
1109     .querycap   = rk29_camera_querycap,
1110     .set_bus_param      = rk29_camera_set_bus_param,
1111     .s_stream = rk29_camera_s_stream   /* ddl@rock-chips.com : Add stream control for host */
1112 };
1113 static int rk29_camera_probe(struct platform_device *pdev)
1114 {
1115     struct rk29_camera_dev *pcdev;
1116     struct resource *res;
1117     int irq;
1118     int err = 0;
1119
1120     RK29CAMERA_DG("\n%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1121     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1122     irq = platform_get_irq(pdev, 0);
1123     if (!res || irq < 0) {
1124         err = -ENODEV;
1125         goto exit;
1126     }
1127
1128     pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1129     if (!pcdev) {
1130         dev_err(&pdev->dev, "Could not allocate pcdev\n");
1131         err = -ENOMEM;
1132         goto exit_alloc;
1133     }
1134     rk29_camdev_info_ptr = pcdev;
1135
1136     /*config output clk*/
1137     pcdev->clk = clk_get(&pdev->dev, "vip");
1138     if (!pcdev->clk || IS_ERR(pcdev->clk))  {
1139         RK29CAMERA_TR(KERN_ERR "failed to get vip_clk source\n");
1140         err = -ENOENT;
1141         goto exit_eclkget;
1142     }
1143
1144     dev_set_drvdata(&pdev->dev, pcdev);
1145     pcdev->res = res;
1146
1147     pcdev->pdata = pdev->dev.platform_data;             /* ddl@rock-chips.com : Request IO in init function */
1148     if (pcdev->pdata && pcdev->pdata->io_init) {
1149         pcdev->pdata->io_init();
1150     }
1151
1152     INIT_LIST_HEAD(&pcdev->capture);
1153     spin_lock_init(&pcdev->lock);
1154
1155     /*
1156      * Request the regions.
1157      */
1158     if (!request_mem_region(res->start, res->end - res->start + 1,
1159                             RK29_CAM_DRV_NAME)) {
1160         err = -EBUSY;
1161         goto exit_reqmem;
1162     }
1163
1164     pcdev->base = ioremap(res->start, res->end - res->start + 1);
1165     if (pcdev->base == NULL) {
1166         dev_err(pcdev->dev, "ioremap() of registers failed\n");
1167         err = -ENXIO;
1168         goto exit_ioremap;
1169     }
1170
1171     pcdev->irq = irq;
1172     pcdev->dev = &pdev->dev;
1173
1174     /* config buffer address */
1175     /* request irq */
1176     err = request_irq(pcdev->irq, rk29_camera_irq, 0, RK29_CAM_DRV_NAME,
1177                       pcdev);
1178     if (err) {
1179         dev_err(pcdev->dev, "Camera interrupt register failed \n");
1180         goto exit_reqirq;
1181     }
1182
1183     pcdev->soc_host.drv_name    = RK29_CAM_DRV_NAME;
1184     pcdev->soc_host.ops         = &rk29_soc_camera_host_ops;
1185     pcdev->soc_host.priv                = pcdev;
1186     pcdev->soc_host.v4l2_dev.dev        = &pdev->dev;
1187     pcdev->soc_host.nr          = pdev->id;
1188
1189     err = soc_camera_host_register(&pcdev->soc_host);
1190     if (err)
1191         goto exit_free_irq;
1192
1193     RK29CAMERA_DG("\n%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1194     return 0;
1195
1196 exit_free_irq:
1197     free_irq(pcdev->irq, pcdev);
1198 exit_reqirq:
1199     iounmap(pcdev->base);
1200 exit_ioremap:
1201     release_mem_region(res->start, res->end - res->start + 1);
1202 exit_reqmem:
1203     clk_put(pcdev->clk);
1204 exit_eclkget:
1205     kfree(pcdev);
1206 exit_alloc:
1207     rk29_camdev_info_ptr = NULL;
1208 exit:
1209     return err;
1210 }
1211
1212 static int __devexit rk29_camera_remove(struct platform_device *pdev)
1213 {
1214     struct rk29_camera_dev *pcdev = platform_get_drvdata(pdev);
1215     struct resource *res;
1216
1217     free_irq(pcdev->irq, pcdev);
1218
1219     soc_camera_host_unregister(&pcdev->soc_host);
1220
1221     res = pcdev->res;
1222     release_mem_region(res->start, res->end - res->start + 1);
1223
1224     if (pcdev->pdata && pcdev->pdata->io_deinit) {         /* ddl@rock-chips.com : Free IO in deinit function */
1225         pcdev->pdata->io_deinit();
1226     }
1227
1228     kfree(pcdev);
1229     rk29_camdev_info_ptr = NULL;
1230     dev_info(&pdev->dev, "RK28 Camera driver unloaded\n");
1231
1232     return 0;
1233 }
1234
1235 static struct platform_driver rk29_camera_driver =
1236 {
1237     .driver     = {
1238         .name   = RK29_CAM_DRV_NAME,
1239     },
1240     .probe              = rk29_camera_probe,
1241     .remove             = __devexit_p(rk29_camera_remove),
1242 };
1243
1244
1245 static int __devinit rk29_camera_init(void)
1246 {
1247     RK29CAMERA_DG("\n%s..%s..%d  \n",__FUNCTION__,__FILE__,__LINE__);
1248     return platform_driver_register(&rk29_camera_driver);
1249 }
1250
1251 static void __exit rk29_camera_exit(void)
1252 {
1253     platform_driver_unregister(&rk29_camera_driver);
1254 }
1255
1256 device_initcall_sync(rk29_camera_init);
1257 module_exit(rk29_camera_exit);
1258
1259 MODULE_DESCRIPTION("RK29 Soc Camera Host driver");
1260 MODULE_AUTHOR("ddl <ddl@rock-chips>");
1261 MODULE_LICENSE("GPL");