2 * V4L2 Driver for RK28 camera host
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/errno.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.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>
43 module_param(debug, int, S_IRUGO|S_IWUSR);
45 #define dprintk(level, fmt, arg...) do { \
47 printk(KERN_WARNING"rk29xx_camera: " fmt , ## arg); } while (0)
49 #define RK29CAMERA_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
50 #define RK29CAMERA_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
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
72 //The key register bit descrition
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)
105 #define GRF_SOC_CON0_Reg 0xbc
106 #define VIP_AXIMASTER (0x00<<0)
107 #define VIP_AHBMASTER (0x01<<2)
110 #define GRF_OS_REG0 0xd0
111 #define VIP_ACLK_DIV_HCLK_1 (0x00<<0)
112 #define VIP_ACLK_DIV_HCLK_2 (0x01<<0)
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
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)))
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)))
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))
133 #define RK29_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 2)
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)
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 : */
155 #define RK29_CAM_AXI 0
156 #define RK29_CAM_AHB 1
157 #define CONFIG_RK29_CAM_WORK_BUS RK29_CAM_AXI
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,
165 enum v4l2_buf_type type,
166 enum v4l2_field field,
170 /* buffer for one video frame */
173 /* common v4l buffer stuff -- must be first */
174 struct videobuf_buffer vb;
175 const struct soc_camera_data_format *fmt;
178 enum rk29_camera_reg_state
184 struct rk29_camera_reg
186 unsigned int VipCtrl;
187 unsigned int VipCrop;
189 unsigned int VipIntMsk;
191 enum rk29_camera_reg_state Inval;
193 struct rk29_camera_work
195 struct videobuf_buffer *vb;
196 struct rk29_camera_dev *pcdev;
197 struct work_struct work;
199 struct rk29_camera_dev
201 struct soc_camera_host soc_host;
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;
208 struct clk *aclk_ddr_lcdc;
209 struct clk *aclk_disp_matrix;
211 struct clk *hclk_cpu_display;
212 struct clk *vip_slave;
215 struct clk *vip_input;
218 struct clk *hclk_disp_matrix;
219 struct clk *vip_matrix;
221 struct clk *pd_display;
224 void __iomem *grf_base;
225 int frame_inval; /* ddl@rock-chips.com : The first frames is invalidate */
229 unsigned int vipmem_phybase;
230 unsigned int vipmem_size;
231 unsigned int vipmem_bsize;
235 struct rk29camera_platform_data *pdata;
236 struct resource *res;
238 struct list_head capture;
242 struct videobuf_buffer *active;
243 struct rk29_camera_reg reginfo_suspend;
244 struct workqueue_struct *camera_wq;
245 struct rk29_camera_work *camera_work;
246 unsigned int camera_work_count;
247 struct hrtimer fps_timer;
248 struct work_struct camera_reinit_work;
250 rk29_camera_sensor_cb_s icd_cb;
252 static DEFINE_MUTEX(camera_lock);
253 static const char *rk29_cam_driver_description = "RK29_Camera";
254 static struct rk29_camera_dev *rk29_camdev_info_ptr;
256 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable);
260 * Videobuf operations
262 static int rk29_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
265 struct soc_camera_device *icd = vq->priv_data;
266 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
267 struct rk29_camera_dev *pcdev = ici->priv;
269 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
271 /* planar capture requires Y, U and V buffers to be page aligned */
273 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
274 *size = PAGE_ALIGN(icd->user_width* icd->user_height * bytes_per_pixel); /* Y pages UV pages, yuv422*/
275 pcdev->vipmem_bsize = PAGE_ALIGN(pcdev->host_width * pcdev->host_height * bytes_per_pixel);
277 *size = PAGE_ALIGN((icd->user_width* icd->user_height * icd->current_fmt->depth + 7)>>3); /* Y pages UV pages, yuv422*/
278 pcdev->vipmem_bsize = PAGE_ALIGN((pcdev->host_width * pcdev->host_height * icd->current_fmt->depth + 7)>>3);
281 if (CAM_WORKQUEUE_IS_EN()) {
282 if (CAM_IPPWORK_IS_EN()) {
283 if (*count > pcdev->vipmem_size/pcdev->vipmem_bsize) { /* Buffers must be limited, when this resolution is genered by IPP */
284 *count = pcdev->vipmem_size/pcdev->vipmem_bsize;
287 if ((pcdev->camera_work_count != *count) && pcdev->camera_work) {
288 kfree(pcdev->camera_work);
289 pcdev->camera_work = NULL;
290 pcdev->camera_work_count = 0;
293 if (pcdev->camera_work == NULL) {
294 pcdev->camera_work = kmalloc(sizeof(struct rk29_camera_work)*(*count), GFP_KERNEL);
295 if (pcdev->camera_work == NULL) {
296 RK29CAMERA_TR("\n %s kmalloc fail\n", __FUNCTION__);
299 pcdev->camera_work_count = *count;
303 RK29CAMERA_DG("%s..%d.. videobuf size:%d, vipmem_buf size:%d \n",__FUNCTION__,__LINE__, *size,pcdev->vipmem_bsize);
307 static void rk29_videobuf_free(struct videobuf_queue *vq, struct rk29_buffer *buf)
309 struct soc_camera_device *icd = vq->priv_data;
311 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
312 &buf->vb, buf->vb.baddr, buf->vb.bsize);
314 /* ddl@rock-chips.com: buf_release called soc_camera_streamoff and soc_camera_close*/
315 if (buf->vb.state == VIDEOBUF_NEEDS_INIT)
321 videobuf_dma_contig_free(vq, &buf->vb);
322 dev_dbg(&icd->dev, "%s freed\n", __func__);
323 buf->vb.state = VIDEOBUF_NEEDS_INIT;
326 static int rk29_videobuf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field)
328 struct soc_camera_device *icd = vq->priv_data;
329 struct rk29_buffer *buf;
332 buf = container_of(vb, struct rk29_buffer, vb);
334 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
335 vb, vb->baddr, vb->bsize);
337 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
339 /* Added list head initialization on alloc */
340 WARN_ON(!list_empty(&vb->queue));
342 /* This can be useful if you want to see if we actually fill
343 * the buffer with something */
344 //memset((void *)vb->baddr, 0xaa, vb->bsize);
346 BUG_ON(NULL == icd->current_fmt);
348 if (buf->fmt != icd->current_fmt ||
349 vb->width != icd->user_width ||
350 vb->height != icd->user_height ||
351 vb->field != field) {
352 buf->fmt = icd->current_fmt;
353 vb->width = icd->user_width;
354 vb->height = icd->user_height;
356 vb->state = VIDEOBUF_NEEDS_INIT;
359 vb->size = (((vb->width * vb->height *buf->fmt->depth) + 7) >> 3) ; /* ddl@rock-chips.com : fmt->depth is coorect */
360 if (0 != vb->baddr && vb->bsize < vb->size) {
365 if (vb->state == VIDEOBUF_NEEDS_INIT) {
366 ret = videobuf_iolock(vq, vb, NULL);
370 vb->state = VIDEOBUF_PREPARED;
372 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
375 rk29_videobuf_free(vq, buf);
380 static inline void rk29_videobuf_capture(struct videobuf_buffer *vb)
382 unsigned int y_addr,uv_addr;
383 struct rk29_camera_dev *pcdev = rk29_camdev_info_ptr;
386 if (CAM_IPPWORK_IS_EN()) {
387 y_addr = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
388 uv_addr = y_addr + pcdev->host_width*pcdev->host_height;
390 if (y_addr > (pcdev->vipmem_phybase + pcdev->vipmem_size - pcdev->vipmem_bsize)) {
391 RK29CAMERA_TR("vipmem for IPP is overflow! %dx%d -> %dx%d vb_index:%d\n",pcdev->host_width,pcdev->host_height,
392 pcdev->icd->user_width,pcdev->icd->user_height, vb->i);
397 uv_addr = y_addr + vb->width * vb->height;
399 write_vip_reg(RK29_VIP_CAPTURE_F1SA_Y, y_addr);
400 write_vip_reg(RK29_VIP_CAPTURE_F1SA_UV, uv_addr);
401 write_vip_reg(RK29_VIP_CAPTURE_F2SA_Y, y_addr);
402 write_vip_reg(RK29_VIP_CAPTURE_F2SA_UV, uv_addr);
403 write_vip_reg(RK29_VIP_FB_SR, 0x00000002);//frame1 has been ready to receive data,frame 2 is not used
406 /* Locking: Caller holds q->irqlock */
407 static void rk29_videobuf_queue(struct videobuf_queue *vq,
408 struct videobuf_buffer *vb)
410 struct soc_camera_device *icd = vq->priv_data;
411 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
412 struct rk29_camera_dev *pcdev = ici->priv;
414 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
415 vb, vb->baddr, vb->bsize);
417 vb->state = VIDEOBUF_QUEUED;
419 if (list_empty(&pcdev->capture)) {
420 list_add_tail(&vb->queue, &pcdev->capture);
422 if (list_entry(pcdev->capture.next, struct videobuf_buffer, queue) != vb)
423 list_add_tail(&vb->queue, &pcdev->capture);
425 BUG(); /* ddl@rock-chips.com : The same videobuffer queue again */
428 if (!pcdev->active) {
430 rk29_videobuf_capture(vb);
433 static int rk29_pixfmt2ippfmt(unsigned int pixfmt, int *ippfmt)
437 case V4L2_PIX_FMT_NV16:
438 case V4L2_PIX_FMT_YUV422P:
440 *ippfmt = IPP_Y_CBCR_H2V1;
443 case V4L2_PIX_FMT_NV12:
444 case V4L2_PIX_FMT_YUV420:
446 *ippfmt = IPP_Y_CBCR_H2V2;
450 goto rk29_pixfmt2ippfmt_err;
454 rk29_pixfmt2ippfmt_err:
457 static void rk29_camera_capture_process(struct work_struct *work)
459 struct rk29_camera_work *camera_work = container_of(work, struct rk29_camera_work, work);
460 struct videobuf_buffer *vb = camera_work->vb;
461 struct rk29_camera_dev *pcdev = camera_work->pcdev;
462 struct rk29_ipp_req ipp_req;
463 unsigned long int flags;
465 if (CAM_IPPWORK_IS_EN()) {
466 ipp_req.src0.YrgbMst = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
467 ipp_req.src0.CbrMst= ipp_req.src0.YrgbMst + pcdev->host_width*pcdev->host_height;
468 ipp_req.src0.w = pcdev->host_width;
469 ipp_req.src0.h = pcdev->host_height;
470 rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.src0.fmt);
472 ipp_req.dst0.YrgbMst = vb->boff;
473 ipp_req.dst0.CbrMst= vb->boff+vb->width * vb->height;
474 ipp_req.dst0.w = pcdev->icd->user_width;
475 ipp_req.dst0.h = pcdev->icd->user_height;
476 rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.dst0.fmt);
478 ipp_req.src_vir_w = ipp_req.src0.w;
479 ipp_req.dst_vir_w = ipp_req.dst0.w;
480 ipp_req.timeout = 100;
481 ipp_req.flag = IPP_ROT_0;
483 //if (ipp_do_blit(&ipp_req)) {
484 if (ipp_blit_sync(&ipp_req)) {
485 spin_lock_irqsave(&pcdev->lock, flags);
486 vb->state = VIDEOBUF_ERROR;
487 spin_unlock_irqrestore(&pcdev->lock, flags);
488 RK29CAMERA_TR("Capture image(vb->i:0x%x) which IPP operated is error!\n", vb->i);
489 RK29CAMERA_TR("ipp_req.src0.YrgbMst:0x%x ipp_req.src0.CbrMst:0x%x \n", ipp_req.src0.YrgbMst,ipp_req.src0.CbrMst);
490 RK29CAMERA_TR("ipp_req.src0.w:0x%x ipp_req.src0.h:0x%x \n",ipp_req.src0.w,ipp_req.src0.h);
491 RK29CAMERA_TR("ipp_req.src0.fmt:0x%x\n",ipp_req.src0.fmt);
492 RK29CAMERA_TR("ipp_req.dst0.YrgbMst:0x%x ipp_req.dst0.CbrMst:0x%x \n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
493 RK29CAMERA_TR("ipp_req.dst0.w:0x%x ipp_req.dst0.h:0x%x \n",ipp_req.dst0.w ,ipp_req.dst0.h);
494 RK29CAMERA_TR("ipp_req.dst0.fmt:0x%x\n",ipp_req.dst0.fmt);
495 RK29CAMERA_TR("ipp_req.src_vir_w:0x%x ipp_req.dst_vir_w :0x%x\n",ipp_req.src_vir_w ,ipp_req.dst_vir_w);
496 RK29CAMERA_TR("ipp_req.timeout:0x%x ipp_req.flag :0x%x\n",ipp_req.timeout,ipp_req.flag);
500 if (pcdev->icd_cb.sensor_cb)
501 (pcdev->icd_cb.sensor_cb)(vb);
503 wake_up(&(camera_work->vb->done));
505 static irqreturn_t rk29_camera_irq(int irq, void *data)
507 struct rk29_camera_dev *pcdev = data;
508 struct videobuf_buffer *vb;
509 struct rk29_camera_work *wk;
511 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
512 /* ddl@rock-chps.com : Current VIP is run in One Frame Mode, Frame 1 is validate */
513 if (read_vip_reg(RK29_VIP_FB_SR) & 0x01) {
516 goto RK29_CAMERA_IRQ_END;
518 if (pcdev->frame_inval>0) {
519 pcdev->frame_inval--;
520 rk29_videobuf_capture(pcdev->active);
521 goto RK29_CAMERA_IRQ_END;
522 } else if (pcdev->frame_inval) {
523 RK29CAMERA_TR("frame_inval : %0x",pcdev->frame_inval);
524 pcdev->frame_inval = 0;
528 /* ddl@rock-chips.com : this vb may be deleted from queue */
529 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
530 list_del_init(&vb->queue);
533 pcdev->active = NULL;
534 if (!list_empty(&pcdev->capture)) {
535 pcdev->active = list_entry(pcdev->capture.next, struct videobuf_buffer, queue);
537 rk29_videobuf_capture(pcdev->active);
541 if (pcdev->active == NULL) {
542 RK29CAMERA_DG("%s video_buf queue is empty!\n",__FUNCTION__);
545 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
546 vb->state = VIDEOBUF_DONE;
547 do_gettimeofday(&vb->ts);
551 if (CAM_WORKQUEUE_IS_EN()) {
552 wk = pcdev->camera_work + vb->i;
553 INIT_WORK(&(wk->work), rk29_camera_capture_process);
556 queue_work(pcdev->camera_wq, &(wk->work));
567 static void rk29_videobuf_release(struct videobuf_queue *vq,
568 struct videobuf_buffer *vb)
570 struct rk29_buffer *buf = container_of(vb, struct rk29_buffer, vb);
571 struct soc_camera_device *icd = vq->priv_data;
572 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
573 struct rk29_camera_dev *pcdev = ici->priv;
576 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
577 vb, vb->baddr, vb->bsize);
581 case VIDEOBUF_ACTIVE:
582 dev_dbg(&icd->dev, "%s (active)\n", __func__);
584 case VIDEOBUF_QUEUED:
585 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
587 case VIDEOBUF_PREPARED:
588 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
591 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
595 if (vb == pcdev->active) {
596 RK29CAMERA_DG("%s Wait for this video buf(0x%x) write finished!\n ",__FUNCTION__,(unsigned int)vb);
597 interruptible_sleep_on_timeout(&vb->done, 100);
598 RK29CAMERA_DG("%s This video buf(0x%x) write finished, release now!!\n",__FUNCTION__,(unsigned int)vb);
600 rk29_videobuf_free(vq, buf);
603 static struct videobuf_queue_ops rk29_videobuf_ops =
605 .buf_setup = rk29_videobuf_setup,
606 .buf_prepare = rk29_videobuf_prepare,
607 .buf_queue = rk29_videobuf_queue,
608 .buf_release = rk29_videobuf_release,
611 static void rk29_camera_init_videobuf(struct videobuf_queue *q,
612 struct soc_camera_device *icd)
614 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
615 struct rk29_camera_dev *pcdev = ici->priv;
617 /* We must pass NULL as dev pointer, then all pci_* dma operations
618 * transform to normal dma_* ones. */
619 videobuf_queue_dma_contig_init(q,
621 ici->v4l2_dev.dev, &pcdev->lock,
622 V4L2_BUF_TYPE_VIDEO_CAPTURE,
624 sizeof(struct rk29_buffer),
627 static int rk29_camera_activate(struct rk29_camera_dev *pcdev, struct soc_camera_device *icd)
629 unsigned long sensor_bus_flags = SOCAM_MCLK_24MHZ;
632 RK29CAMERA_DG("%s..%d.. \n",__FUNCTION__,__LINE__);
633 if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix || !pcdev->hclk_cpu_display ||
634 !pcdev->vip_slave || !pcdev->vip_out || !pcdev->vip_input || !pcdev->vip_bus || !pcdev->pd_display ||
635 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) || IS_ERR(pcdev->hclk_cpu_display) || IS_ERR(pcdev->pd_display) ||
636 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip_out) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus)) {
638 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
639 goto RK29_CAMERA_ACTIVE_ERR;
642 if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
643 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix)) {
645 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
646 goto RK29_CAMERA_ACTIVE_ERR;
649 clk_enable(pcdev->pd_display);
651 clk_enable(pcdev->aclk_ddr_lcdc);
652 clk_enable(pcdev->aclk_disp_matrix);
654 clk_enable(pcdev->hclk_cpu_display);
655 clk_enable(pcdev->vip_slave);
657 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
658 clk_enable(pcdev->hclk_disp_matrix);
659 clk_enable(pcdev->vip_matrix);
662 clk_enable(pcdev->vip_input);
663 clk_enable(pcdev->vip_bus);
665 //if (icd->ops->query_bus_param) /* ddl@rock-chips.com : Query Sensor's xclk */
666 //sensor_bus_flags = icd->ops->query_bus_param(icd);
668 if (sensor_bus_flags & SOCAM_MCLK_48MHZ) {
669 parent = clk_get(NULL, "clk48m");
670 if (!parent || IS_ERR(parent))
671 goto RK29_CAMERA_ACTIVE_ERR;
672 } else if (sensor_bus_flags & SOCAM_MCLK_27MHZ) {
673 parent = clk_get(NULL, "extclk");
674 if (!parent || IS_ERR(parent))
675 goto RK29_CAMERA_ACTIVE_ERR;
677 parent = clk_get(NULL, "xin24m");
678 if (!parent || IS_ERR(parent))
679 goto RK29_CAMERA_ACTIVE_ERR;
682 clk_set_parent(pcdev->vip_out, parent);
684 clk_enable(pcdev->vip_out);
685 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_VIP_CLKOUT);
688 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
689 write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)|VIP_AHBMASTER); //VIP Config to AHB
690 write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)&(~VIP_ACLK_DIV_HCLK_2)); //aclk:hclk = 1:1
692 write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)&(~VIP_AHBMASTER)); //VIP Config to AXI
693 write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)|VIP_ACLK_DIV_HCLK_2); //aclk:hclk = 2:1
697 write_vip_reg(RK29_VIP_RESET, 0x76543210); /* ddl@rock-chips.com : vip software reset */
700 write_vip_reg(RK29_VIP_AHBR_CTRL, 0x07); /* ddl@rock-chips.com : vip ahb burst 16 */
701 write_vip_reg(RK29_VIP_INT_MASK, 0x01); //capture complete interrupt enable
702 write_vip_reg(RK29_VIP_CRM, 0x00000000); //Y/CB/CR color modification
705 RK29_CAMERA_ACTIVE_ERR:
709 static void rk29_camera_deactivate(struct rk29_camera_dev *pcdev)
711 //pcdev->active = NULL;
713 write_vip_reg(RK29_VIP_CTRL, 0);
714 read_vip_reg(RK29_VIP_INT_STS); //clear vip interrupte single
716 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_GPIO1B4);
717 clk_disable(pcdev->vip_out);
719 clk_disable(pcdev->vip_input);
720 clk_disable(pcdev->vip_bus);
722 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
723 clk_disable(pcdev->hclk_disp_matrix);
724 clk_disable(pcdev->vip_matrix);
727 clk_disable(pcdev->hclk_cpu_display);
728 clk_disable(pcdev->vip_slave);
730 clk_disable(pcdev->aclk_ddr_lcdc);
731 clk_disable(pcdev->aclk_disp_matrix);
733 clk_disable(pcdev->pd_display);
737 /* The following two functions absolutely depend on the fact, that
738 * there can be only one camera on RK28 quick capture interface */
739 static int rk29_camera_add_device(struct soc_camera_device *icd)
741 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
742 struct rk29_camera_dev *pcdev = ici->priv;
743 struct device *control = to_soc_camera_control(icd);
744 struct v4l2_subdev *sd;
747 mutex_lock(&camera_lock);
754 dev_info(&icd->dev, "RK29 Camera driver attached to camera%d(%s)\n",
755 icd->devnum,dev_name(icd->pdev));
757 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
758 pcdev->active = NULL;
760 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
761 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
762 * if app havn't dequeue all videobuf before close camera device;
764 INIT_LIST_HEAD(&pcdev->capture);
766 ret = rk29_camera_activate(pcdev,icd);
770 /* ddl@rock-chips.com : v4l2_subdev is not created when ici->ops->add called in soc_camera_probe */
772 sd = dev_get_drvdata(control);
773 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_IOREQUEST,(void*)pcdev->pdata);
775 ret = v4l2_subdev_call(sd,core, init, 0);
779 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_CB_REGISTER,(void*)(&pcdev->icd_cb));
785 mutex_unlock(&camera_lock);
789 static void rk29_camera_remove_device(struct soc_camera_device *icd)
791 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
792 struct rk29_camera_dev *pcdev = ici->priv;
793 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
795 mutex_lock(&camera_lock);
796 BUG_ON(icd != pcdev->icd);
798 dev_info(&icd->dev, "RK29 Camera driver detached from camera%d(%s)\n",
799 icd->devnum,dev_name(icd->pdev));
801 /* ddl@rock-chips.com: Application will call VIDIOC_STREAMOFF before close device, but
802 stream may be turn on again before close device, if suspend and resume happened. */
803 if (read_vip_reg(RK29_VIP_CTRL) & ENABLE_CAPTURE) {
804 rk29_camera_s_stream(icd,0);
807 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_DEACTIVATE,NULL);
808 rk29_camera_deactivate(pcdev);
810 if (pcdev->camera_work) {
811 kfree(pcdev->camera_work);
812 pcdev->camera_work = NULL;
813 pcdev->camera_work_count = 0;
816 pcdev->active = NULL;
818 pcdev->icd_cb.sensor_cb = NULL;
819 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
820 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
821 * if app havn't dequeue all videobuf before close camera device;
823 INIT_LIST_HEAD(&pcdev->capture);
825 mutex_unlock(&camera_lock);
826 RK29CAMERA_DG("%s exit\n",__FUNCTION__);
831 static int rk29_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
833 unsigned long bus_flags, camera_flags, common_flags;
834 unsigned int vip_ctrl_val = 0;
837 RK29CAMERA_DG("%s..%d..\n",__FUNCTION__,__LINE__);
839 bus_flags = RK29_CAM_BUS_PARAM;
840 if (icd->ops->query_bus_param)
841 camera_flags = icd->ops->query_bus_param(icd);
845 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
848 goto RK29_CAMERA_SET_BUS_PARAM_END;
851 ret = icd->ops->set_bus_param(icd, common_flags);
853 goto RK29_CAMERA_SET_BUS_PARAM_END;
855 if (common_flags & SOCAM_DATAWIDTH_8) {
857 } else if (common_flags & SOCAM_DATAWIDTH_10) {
861 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
862 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) {
863 vip_ctrl_val |= NEGATIVE_EDGE;
865 vip_ctrl_val &= ~NEGATIVE_EDGE;
867 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) {
868 vip_ctrl_val |= HSY_LOW_ACTIVE;
870 vip_ctrl_val &= ~HSY_LOW_ACTIVE;
872 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) {
873 vip_ctrl_val |= VSY_HIGH_ACTIVE;
875 vip_ctrl_val &= ~VSY_HIGH_ACTIVE;
878 /* ddl@rock-chips.com : Don't enable capture here, enable in stream_on */
879 //vip_ctrl_val |= ENABLE_CAPTURE;
881 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
882 RK29CAMERA_DG("%s..ctrl:0x%x CtrReg=%x AXI_AHB:0x%x aclk_hclk:0x%x \n",__FUNCTION__,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL),
883 read_grf_reg(GRF_SOC_CON0_Reg)&VIP_AHBMASTER, read_grf_reg(GRF_OS_REG0)&VIP_ACLK_DIV_HCLK_2);
885 RK29_CAMERA_SET_BUS_PARAM_END:
887 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
891 static int rk29_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
893 unsigned long bus_flags, camera_flags;
896 bus_flags = RK29_CAM_BUS_PARAM;
897 if (icd->ops->query_bus_param) {
898 camera_flags = icd->ops->query_bus_param(icd);
902 ret = soc_camera_bus_param_compatible(camera_flags, bus_flags) ;
905 dev_warn(icd->dev.parent,
906 "Flags incompatible: camera %lx, host %lx\n",
907 camera_flags, bus_flags);
910 static const struct soc_camera_data_format rk29_camera_formats[] = {
912 .name = "YUV420 NV12",
914 .fourcc = V4L2_PIX_FMT_NV12,
915 .colorspace = V4L2_COLORSPACE_JPEG,
917 .name = "YUV422 NV16",
919 .fourcc = V4L2_PIX_FMT_NV16,
920 .colorspace = V4L2_COLORSPACE_JPEG,
922 .name = "NV12(v0.0.1)", /* ddl@rock-chips.com: 0.0.1 driver */
924 .fourcc = V4L2_PIX_FMT_YUV420,
925 .colorspace = V4L2_COLORSPACE_JPEG,
927 .name = "NV16(v0.0.1)",
929 .fourcc = V4L2_PIX_FMT_YUV422P,
930 .colorspace = V4L2_COLORSPACE_JPEG,
932 .name = "Raw Bayer RGB 10 bit",
934 .fourcc = V4L2_PIX_FMT_SGRBG10,
935 .colorspace = V4L2_COLORSPACE_SRGB,
939 static void rk29_camera_setup_format(struct soc_camera_device *icd, __u32 host_pixfmt, __u32 cam_pixfmt, struct v4l2_rect *rect)
941 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
942 struct rk29_camera_dev *pcdev = ici->priv;
943 unsigned int vip_fs = 0,vip_crop = 0;
944 unsigned int vip_ctrl_val = VIP_SENSOR|ONEFRAME|DISABLE_CAPTURE;
948 case V4L2_PIX_FMT_NV16:
949 case V4L2_PIX_FMT_YUV422P: /* ddl@rock-chips.com: V4L2_PIX_FMT_YUV422P is V4L2_PIX_FMT_NV16 actually in 0.0.1 driver */
950 vip_ctrl_val |= VIPREGYUV422;
951 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
952 pcdev->pixfmt = host_pixfmt;
954 case V4L2_PIX_FMT_NV12:
955 case V4L2_PIX_FMT_YUV420: /* ddl@rock-chips.com: V4L2_PIX_FMT_YUV420 is V4L2_PIX_FMT_NV12 actually in 0.0.1 driver */
956 vip_ctrl_val |= VIPREGYUV420;
957 if (pcdev->frame_inval != RK29_CAM_FRAME_INVAL_INIT)
958 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
959 pcdev->pixfmt = host_pixfmt;
961 case V4L2_PIX_FMT_SGRBG10:
962 vip_ctrl_val |= (VIP_RAW | VIP_SENSOR | VIP_DATA_LITTLEEND);
963 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
964 pcdev->pixfmt = host_pixfmt;
966 default: /* ddl@rock-chips.com : vip output format is hold when pixfmt is invalidate */
967 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & VIPREGYUV422);
973 case V4L2_PIX_FMT_UYVY:
974 vip_ctrl_val |= SENSOR_UYVY;
976 case V4L2_PIX_FMT_YUYV:
977 vip_ctrl_val |= SENSOR_YUYV;
980 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & SENSOR_YUYV);
984 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val); /* ddl@rock-chips.com: VIP capture mode and capture format must be set before FS register set */
986 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
988 if (vip_ctrl_val & ONEFRAME) {
989 vip_crop = ((rect->left<<16) + rect->top);
990 vip_fs = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
991 } else if (vip_ctrl_val & PING_PONG) {
995 write_vip_reg(RK29_VIP_CROP, vip_crop);
996 write_vip_reg(RK29_VIP_FS, vip_fs);
998 write_vip_reg(RK29_VIP_FB_SR, 0x00000003);
1000 pcdev->host_width = rect->width;
1001 pcdev->host_height = rect->height;
1003 RK29CAMERA_DG("%s.. crop:0x%x fs:0x%x ctrl:0x%x CtrlReg:0x%x\n",__FUNCTION__,vip_crop,vip_fs,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL));
1007 static int rk29_camera_get_formats(struct soc_camera_device *icd, int idx,
1008 struct soc_camera_format_xlate *xlate)
1010 struct device *dev = icd->dev.parent;
1011 int formats = 0, buswidth, ret;
1015 ret = rk29_camera_try_bus_param(icd, buswidth);
1019 switch (icd->formats[idx].fourcc) {
1020 case V4L2_PIX_FMT_UYVY:
1021 case V4L2_PIX_FMT_YUYV:
1024 xlate->host_fmt = &rk29_camera_formats[0];
1025 xlate->cam_fmt = icd->formats + idx;
1026 xlate->buswidth = buswidth;
1028 dev_dbg(dev, "Providing format %s using %s\n",
1029 rk29_camera_formats[0].name,
1030 icd->formats[idx].name);
1035 xlate->host_fmt = &rk29_camera_formats[1];
1036 xlate->cam_fmt = icd->formats + idx;
1037 xlate->buswidth = buswidth;
1039 dev_dbg(dev, "Providing format %s using %s\n",
1040 rk29_camera_formats[1].name,
1041 icd->formats[idx].name);
1046 xlate->host_fmt = &rk29_camera_formats[2];
1047 xlate->cam_fmt = icd->formats + idx;
1048 xlate->buswidth = buswidth;
1050 dev_dbg(dev, "Providing format %s using %s\n",
1051 rk29_camera_formats[2].name,
1052 icd->formats[idx].name);
1057 xlate->host_fmt = &rk29_camera_formats[3];
1058 xlate->cam_fmt = icd->formats + idx;
1059 xlate->buswidth = buswidth;
1061 dev_dbg(dev, "Providing format %s using %s\n",
1062 rk29_camera_formats[3].name,
1063 icd->formats[idx].name);
1066 case V4L2_PIX_FMT_SGRBG10:
1069 xlate->host_fmt = &rk29_camera_formats[4];
1070 xlate->cam_fmt = icd->formats + idx;
1071 xlate->buswidth = 10;
1073 dev_dbg(dev, "Providing format %s using %s\n",
1074 rk29_camera_formats[2].name,
1075 icd->formats[idx].name);
1085 static void rk29_camera_put_formats(struct soc_camera_device *icd)
1090 static int rk29_camera_set_crop(struct soc_camera_device *icd,
1091 struct v4l2_crop *a)
1093 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1094 struct v4l2_format f;
1095 struct v4l2_pix_format *pix = &f.fmt.pix;
1098 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1100 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1104 if ((pix->width < (a->c.left + a->c.width)) || (pix->height < (a->c.top + a->c.height))) {
1106 pix->width = a->c.left + a->c.width;
1107 pix->height = a->c.top + a->c.height;
1109 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1110 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1111 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV16 ?4 : 0);
1113 ret = v4l2_subdev_call(sd, video, s_fmt, &f);
1118 rk29_camera_setup_format(icd, icd->current_fmt->fourcc, pix->pixelformat, &a->c);
1120 icd->user_width = pix->width;
1121 icd->user_height = pix->height;
1126 static int rk29_camera_set_fmt(struct soc_camera_device *icd,
1127 struct v4l2_format *f)
1129 struct device *dev = icd->dev.parent;
1130 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1131 const struct soc_camera_data_format *cam_fmt = NULL;
1132 const struct soc_camera_format_xlate *xlate = NULL;
1133 struct soc_camera_host *ici =to_soc_camera_host(icd->dev.parent);
1134 struct rk29_camera_dev *pcdev = ici->priv;
1135 struct v4l2_pix_format *pix = &f->fmt.pix;
1136 struct v4l2_format cam_f = *f;
1137 struct v4l2_rect rect;
1138 int ret,usr_w,usr_h;
1142 usr_h = pix->height;
1143 RK29CAMERA_DG("%s enter width:%d height:%d\n",__FUNCTION__,usr_w,usr_h);
1145 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1147 dev_err(dev, "Format %x not found\n", pix->pixelformat);
1149 goto RK29_CAMERA_SET_FMT_END;
1152 cam_fmt = xlate->cam_fmt;
1153 /* ddl@rock-chips.com: sensor init code transmit in here after open */
1154 if (pcdev->icd_init == 0) {
1155 v4l2_subdev_call(sd,core, init, 0);
1156 pcdev->icd_init = 1;
1159 stream_on = read_vip_reg(RK29_VIP_CTRL);
1160 if (stream_on & ENABLE_CAPTURE)
1161 write_vip_reg(RK29_VIP_CTRL, (stream_on & (~ENABLE_CAPTURE)));
1162 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1163 ret = v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1164 cam_f.fmt.pix.pixelformat = pix->pixelformat;
1165 *pix = cam_f.fmt.pix;
1166 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1167 if ((pix->width != usr_w) || (pix->height != usr_h)) {
1168 if (unlikely((pix->width <16) || (pix->width > 8190) || (pix->height < 16) || (pix->height > 8190))) {
1169 RK29CAMERA_TR("Senor and IPP both invalid source resolution(%dx%d)\n",pix->width,pix->height);
1171 goto RK29_CAMERA_SET_FMT_END;
1173 if (unlikely((usr_w <16) || (usr_w > 2047) || (usr_h < 16) || (usr_h > 2047))) {
1174 RK29CAMERA_TR("Senor and IPP both invalid destination resolution(%dx%d)\n",usr_w,usr_h);
1176 goto RK29_CAMERA_SET_FMT_END;
1179 pix->height = usr_h;
1187 rect.width = cam_f.fmt.pix.width;
1188 rect.height = cam_f.fmt.pix.height;
1190 RK29CAMERA_DG("%s..%s..%s icd width:%d host width:%d \n",__FUNCTION__,xlate->host_fmt->name, cam_fmt->name,
1191 rect.width, pix->width);
1192 rk29_camera_setup_format(icd, pix->pixelformat, cam_fmt->fourcc, &rect);
1193 icd->buswidth = xlate->buswidth;
1194 icd->current_fmt = xlate->host_fmt;
1196 if (CAM_IPPWORK_IS_EN()) {
1197 BUG_ON(pcdev->vipmem_phybase == 0);
1201 RK29_CAMERA_SET_FMT_END:
1202 if (stream_on & ENABLE_CAPTURE)
1203 write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL) | ENABLE_CAPTURE));
1205 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
1208 static bool rk29_camera_fmt_capturechk(struct v4l2_format *f)
1212 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1214 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1216 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1218 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1220 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1225 RK29CAMERA_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1228 static int rk29_camera_try_fmt(struct soc_camera_device *icd,
1229 struct v4l2_format *f)
1231 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1232 struct rk29_camera_dev *pcdev = ici->priv;
1233 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1234 const struct soc_camera_format_xlate *xlate;
1235 struct v4l2_pix_format *pix = &f->fmt.pix;
1236 __u32 pixfmt = pix->pixelformat;
1237 enum v4l2_field field;
1238 int ret,usr_w,usr_h,i;
1239 bool is_capture = rk29_camera_fmt_capturechk(f);
1240 bool vipmem_is_overflow = false;
1243 usr_h = pix->height;
1244 RK29CAMERA_DG("%s enter width:%d height:%d\n",__FUNCTION__,usr_w,usr_h);
1246 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1248 dev_err(ici->v4l2_dev.dev, "Format (%c%c%c%c) not found\n", pixfmt & 0xFF, (pixfmt >> 8) & 0xFF,
1249 (pixfmt >> 16) & 0xFF, (pixfmt >> 24) & 0xFF);
1251 RK29CAMERA_TR("%s(version:%c%c%c) support format:\n",rk29_cam_driver_description,(RK29_CAM_VERSION_CODE&0xff0000)>>16,
1252 (RK29_CAM_VERSION_CODE&0xff00)>>8,(RK29_CAM_VERSION_CODE&0xff));
1253 for (i = 0; i < icd->num_user_formats; i++)
1254 RK29CAMERA_TR("(%c%c%c%c)-%s\n",
1255 icd->user_formats[i].host_fmt->fourcc & 0xFF, (icd->user_formats[i].host_fmt->fourcc >> 8) & 0xFF,
1256 (icd->user_formats[i].host_fmt->fourcc >> 16) & 0xFF, (icd->user_formats[i].host_fmt->fourcc >> 24) & 0xFF,
1257 icd->user_formats[i].host_fmt->name);
1258 goto RK29_CAMERA_TRY_FMT_END;
1260 /* limit to rk29 hardware capabilities */
1261 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1262 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1263 pixfmt == V4L2_PIX_FMT_NV16 ? 4 : 0);
1265 pix->bytesperline = pix->width * DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1266 pix->sizeimage = pix->height * pix->bytesperline;
1268 /* camera has to see its format, but the user the original one */
1269 pix->pixelformat = xlate->cam_fmt->fourcc;
1270 /* limit to sensor capabilities */
1271 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1272 pix->pixelformat = pixfmt;
1273 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1274 if ((pix->width > usr_w) && (pix->height > usr_h)) {
1276 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3) > pcdev->vipmem_size);
1278 /* Assume preview buffer minimum is 4 */
1279 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3)*4 > pcdev->vipmem_size);
1281 if (vipmem_is_overflow == false) {
1283 pix->height = usr_h;
1285 RK29CAMERA_TR("vipmem for IPP is overflow, This resolution(%dx%d -> %dx%d) is invalidate!\n",pix->width,pix->height,usr_w,usr_h);
1287 } else if ((pix->width < usr_w) && (pix->height < usr_h)) {
1288 if (((usr_w>>1) < pix->width) && ((usr_h>>1) < pix->height)) {
1290 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3) > pcdev->vipmem_size);
1292 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3)*4 > pcdev->vipmem_size);
1294 if (vipmem_is_overflow == false) {
1296 pix->height = usr_h;
1298 RK29CAMERA_TR("vipmem for IPP is overflow, This resolution(%dx%d -> %dx%d) is invalidate!\n",pix->width,pix->height,usr_w,usr_h);
1301 RK29CAMERA_TR("The aspect ratio(%dx%d/%dx%d) is bigger than 2 !\n",pix->width,pix->height,usr_w,usr_h);
1308 if (field == V4L2_FIELD_ANY) {
1309 pix->field = V4L2_FIELD_NONE;
1310 } else if (field != V4L2_FIELD_NONE) {
1311 dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
1313 goto RK29_CAMERA_TRY_FMT_END;
1316 RK29_CAMERA_TRY_FMT_END:
1318 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
1322 static int rk29_camera_reqbufs(struct soc_camera_file *icf,
1323 struct v4l2_requestbuffers *p)
1327 /* This is for locking debugging only. I removed spinlocks and now I
1328 * check whether .prepare is ever called on a linked buffer, or whether
1329 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1330 * it hadn't triggered */
1331 for (i = 0; i < p->count; i++) {
1332 struct rk29_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1333 struct rk29_buffer, vb);
1335 INIT_LIST_HEAD(&buf->vb.queue);
1341 static unsigned int rk29_camera_poll(struct file *file, poll_table *pt)
1343 struct soc_camera_file *icf = file->private_data;
1344 struct rk29_buffer *buf;
1346 buf = list_entry(icf->vb_vidq.stream.next, struct rk29_buffer,
1349 poll_wait(file, &buf->vb.done, pt);
1351 if (buf->vb.state == VIDEOBUF_DONE ||
1352 buf->vb.state == VIDEOBUF_ERROR)
1353 return POLLIN|POLLRDNORM;
1358 static int rk29_camera_querycap(struct soc_camera_host *ici,
1359 struct v4l2_capability *cap)
1361 /* cap->name is set by the firendly caller:-> */
1362 strlcpy(cap->card, rk29_cam_driver_description, sizeof(cap->card));
1363 cap->version = RK29_CAM_VERSION_CODE;
1364 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1369 static int rk29_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1371 struct soc_camera_host *ici =
1372 to_soc_camera_host(icd->dev.parent);
1373 struct rk29_camera_dev *pcdev = ici->priv;
1374 struct v4l2_subdev *sd;
1377 mutex_lock(&camera_lock);
1378 if ((pcdev->icd == icd) && (icd->ops->suspend)) {
1379 rk29_camera_s_stream(icd, 0);
1380 sd = soc_camera_to_subdev(icd);
1381 v4l2_subdev_call(sd, video, s_stream, 0);
1382 ret = icd->ops->suspend(icd, state);
1384 pcdev->reginfo_suspend.VipCtrl = read_vip_reg(RK29_VIP_CTRL);
1385 pcdev->reginfo_suspend.VipCrop = read_vip_reg(RK29_VIP_CROP);
1386 pcdev->reginfo_suspend.VipFs = read_vip_reg(RK29_VIP_FS);
1387 pcdev->reginfo_suspend.VipIntMsk = read_vip_reg(RK29_VIP_INT_MASK);
1388 pcdev->reginfo_suspend.VipCrm = read_vip_reg(RK29_VIP_CRM);
1390 tmp = pcdev->reginfo_suspend.VipFs>>16; /* ddl@rock-chips.com */
1391 tmp += pcdev->reginfo_suspend.VipCrop>>16;
1392 pcdev->reginfo_suspend.VipFs = (pcdev->reginfo_suspend.VipFs & 0xffff) | (tmp<<16);
1394 pcdev->reginfo_suspend.Inval = Reg_Validate;
1395 rk29_camera_deactivate(pcdev);
1397 RK29CAMERA_DG("%s Enter Success...\n", __FUNCTION__);
1399 RK29CAMERA_DG("%s icd has been deattach, don't need enter suspend\n", __FUNCTION__);
1401 mutex_unlock(&camera_lock);
1405 static int rk29_camera_resume(struct soc_camera_device *icd)
1407 struct soc_camera_host *ici =
1408 to_soc_camera_host(icd->dev.parent);
1409 struct rk29_camera_dev *pcdev = ici->priv;
1410 struct v4l2_subdev *sd;
1413 mutex_lock(&camera_lock);
1414 if ((pcdev->icd == icd) && (icd->ops->resume)) {
1415 if (pcdev->reginfo_suspend.Inval == Reg_Validate) {
1416 rk29_camera_activate(pcdev, icd);
1417 write_vip_reg(RK29_VIP_INT_MASK, pcdev->reginfo_suspend.VipIntMsk);
1418 write_vip_reg(RK29_VIP_CRM, pcdev->reginfo_suspend.VipCrm);
1419 write_vip_reg(RK29_VIP_CTRL, pcdev->reginfo_suspend.VipCtrl&~ENABLE_CAPTURE);
1420 write_vip_reg(RK29_VIP_CROP, pcdev->reginfo_suspend.VipCrop);
1421 write_vip_reg(RK29_VIP_FS, pcdev->reginfo_suspend.VipFs);
1423 rk29_videobuf_capture(pcdev->active);
1424 rk29_camera_s_stream(icd, 1);
1425 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
1427 RK29CAMERA_TR("Resume fail, vip register recored is invalidate!!\n");
1428 goto rk29_camera_resume_end;
1431 ret = icd->ops->resume(icd);
1432 sd = soc_camera_to_subdev(icd);
1433 v4l2_subdev_call(sd, video, s_stream, 1);
1435 RK29CAMERA_DG("%s Enter success\n",__FUNCTION__);
1437 RK29CAMERA_DG("%s icd has been deattach, don't need enter resume\n", __FUNCTION__);
1440 rk29_camera_resume_end:
1441 mutex_unlock(&camera_lock);
1445 static void rk29_camera_reinit_work(struct work_struct *work)
1447 struct device *control;
1448 struct v4l2_subdev *sd;
1449 struct v4l2_format cam_f;
1450 const struct soc_camera_format_xlate *xlate;
1453 write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)&(~ENABLE_CAPTURE)));
1455 control = to_soc_camera_control(rk29_camdev_info_ptr->icd);
1456 sd = dev_get_drvdata(control);
1457 ret = v4l2_subdev_call(sd,core, init, 1);
1459 cam_f.fmt.pix.width = rk29_camdev_info_ptr->icd->user_width;
1460 cam_f.fmt.pix.height = rk29_camdev_info_ptr->icd->user_height;
1461 xlate = soc_camera_xlate_by_fourcc(rk29_camdev_info_ptr->icd, rk29_camdev_info_ptr->icd->current_fmt->fourcc);
1462 cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
1463 ret |= v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1465 write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)|ENABLE_CAPTURE));
1467 RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor now! ret:0x%x\n",ret);
1469 static enum hrtimer_restart rk29_camera_fps_func(struct hrtimer *timer)
1471 RK29CAMERA_DG("rk29_camera_fps_func fps:0x%x\n",rk29_camdev_info_ptr->fps);
1472 if (rk29_camdev_info_ptr->fps < 3) {
1473 RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor delay!\n");
1474 INIT_WORK(&rk29_camdev_info_ptr->camera_reinit_work, rk29_camera_reinit_work);
1475 queue_work(rk29_camdev_info_ptr->camera_wq,&(rk29_camdev_info_ptr->camera_reinit_work));
1478 return HRTIMER_NORESTART;
1480 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable)
1482 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1483 struct rk29_camera_dev *pcdev = ici->priv;
1487 WARN_ON(pcdev->icd != icd);
1489 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
1492 hrtimer_cancel(&pcdev->fps_timer);
1493 hrtimer_start(&pcdev->fps_timer,ktime_set(2, 0),HRTIMER_MODE_REL);
1494 vip_ctrl_val |= ENABLE_CAPTURE;
1496 vip_ctrl_val &= ~ENABLE_CAPTURE;
1497 ret = hrtimer_cancel(&pcdev->fps_timer);
1498 ret |= flush_work(&rk29_camdev_info_ptr->camera_reinit_work);
1499 RK29CAMERA_DG("STREAM_OFF cancel timer and flush work:0x%x \n", ret);
1501 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
1503 RK29CAMERA_DG("%s.. enable : 0x%x \n", __FUNCTION__, enable);
1507 static struct soc_camera_host_ops rk29_soc_camera_host_ops =
1509 .owner = THIS_MODULE,
1510 .add = rk29_camera_add_device,
1511 .remove = rk29_camera_remove_device,
1512 .suspend = rk29_camera_suspend,
1513 .resume = rk29_camera_resume,
1514 .set_crop = rk29_camera_set_crop,
1515 .get_formats = rk29_camera_get_formats,
1516 .put_formats = rk29_camera_put_formats,
1517 .set_fmt = rk29_camera_set_fmt,
1518 .try_fmt = rk29_camera_try_fmt,
1519 .init_videobuf = rk29_camera_init_videobuf,
1520 .reqbufs = rk29_camera_reqbufs,
1521 .poll = rk29_camera_poll,
1522 .querycap = rk29_camera_querycap,
1523 .set_bus_param = rk29_camera_set_bus_param,
1524 .s_stream = rk29_camera_s_stream /* ddl@rock-chips.com : Add stream control for host */
1526 static int rk29_camera_probe(struct platform_device *pdev)
1528 struct rk29_camera_dev *pcdev;
1529 struct resource *res;
1533 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1534 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1535 irq = platform_get_irq(pdev, 0);
1536 if (!res || irq < 0) {
1541 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1543 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1547 rk29_camdev_info_ptr = pcdev;
1549 /*config output clk*/
1550 pcdev->aclk_ddr_lcdc = clk_get(&pdev->dev, "aclk_ddr_lcdc");
1551 pcdev->aclk_disp_matrix = clk_get(&pdev->dev, "aclk_disp_matrix");
1553 pcdev->hclk_cpu_display = clk_get(&pdev->dev, "hclk_cpu_display");
1554 pcdev->vip_slave = clk_get(&pdev->dev, "vip_slave");
1555 pcdev->vip_out = clk_get(&pdev->dev,"vip_out");
1556 pcdev->vip_input = clk_get(&pdev->dev,"vip_input");
1557 pcdev->vip_bus = clk_get(&pdev->dev, "vip_bus");
1559 pcdev->hclk_disp_matrix = clk_get(&pdev->dev,"hclk_disp_matrix");
1560 pcdev->vip_matrix = clk_get(&pdev->dev,"vip_matrix");
1562 pcdev->pd_display = clk_get(&pdev->dev,"pd_display");
1564 if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix || !pcdev->hclk_cpu_display ||
1565 !pcdev->vip_slave || !pcdev->vip_out || !pcdev->vip_input || !pcdev->vip_bus || !pcdev->pd_display ||
1566 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) || IS_ERR(pcdev->hclk_cpu_display) || IS_ERR(pcdev->pd_display) ||
1567 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip_out) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus)) {
1569 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
1574 if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
1575 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix)) {
1577 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
1582 dev_set_drvdata(&pdev->dev, pcdev);
1585 pcdev->pdata = pdev->dev.platform_data; /* ddl@rock-chips.com : Request IO in init function */
1586 if (pcdev->pdata && pcdev->pdata->io_init) {
1587 pcdev->pdata->io_init();
1589 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1590 if (pcdev->pdata && (strcmp(pcdev->pdata->meminfo.name,"camera_ipp_mem")==0)) {
1591 pcdev->vipmem_phybase = pcdev->pdata->meminfo.start;
1592 pcdev->vipmem_size = pcdev->pdata->meminfo.size;
1593 RK29CAMERA_DG("\n%s Memory(start:0x%x size:0x%x) for IPP obtain \n",__FUNCTION__, pcdev->pdata->meminfo.start,pcdev->pdata->meminfo.size);
1595 RK29CAMERA_TR("\n%s Memory for IPP have not obtain! IPP Function is fail\n",__FUNCTION__);
1596 pcdev->vipmem_phybase = 0;
1597 pcdev->vipmem_size = 0;
1600 INIT_LIST_HEAD(&pcdev->capture);
1601 spin_lock_init(&pcdev->lock);
1604 * Request the regions.
1606 if (!request_mem_region(res->start, res->end - res->start + 1,
1607 RK29_CAM_DRV_NAME)) {
1612 pcdev->base = ioremap(res->start, res->end - res->start + 1);
1613 if (pcdev->base == NULL) {
1614 dev_err(pcdev->dev, "ioremap() of registers failed\n");
1620 pcdev->dev = &pdev->dev;
1622 /* config buffer address */
1624 err = request_irq(pcdev->irq, rk29_camera_irq, 0, RK29_CAM_DRV_NAME,
1627 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1631 pcdev->camera_wq = create_workqueue("camera wq");
1632 if (pcdev->camera_wq == NULL)
1634 INIT_WORK(&pcdev->camera_reinit_work, rk29_camera_reinit_work);
1636 pcdev->soc_host.drv_name = RK29_CAM_DRV_NAME;
1637 pcdev->soc_host.ops = &rk29_soc_camera_host_ops;
1638 pcdev->soc_host.priv = pcdev;
1639 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1640 pcdev->soc_host.nr = pdev->id;
1642 err = soc_camera_host_register(&pcdev->soc_host);
1646 hrtimer_init(&pcdev->fps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1647 pcdev->fps_timer.function = rk29_camera_fps_func;
1648 pcdev->icd_cb.sensor_cb = NULL;
1650 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1654 free_irq(pcdev->irq, pcdev);
1655 if (pcdev->camera_wq) {
1656 destroy_workqueue(pcdev->camera_wq);
1657 pcdev->camera_wq = NULL;
1660 iounmap(pcdev->base);
1662 release_mem_region(res->start, res->end - res->start + 1);
1664 if (pcdev->aclk_ddr_lcdc) {
1665 clk_put(pcdev->aclk_ddr_lcdc);
1666 pcdev->aclk_ddr_lcdc = NULL;
1668 if (pcdev->aclk_disp_matrix) {
1669 clk_put(pcdev->aclk_disp_matrix);
1670 pcdev->aclk_disp_matrix = NULL;
1672 if (pcdev->hclk_cpu_display) {
1673 clk_put(pcdev->hclk_cpu_display);
1674 pcdev->hclk_cpu_display = NULL;
1676 if (pcdev->vip_slave) {
1677 clk_put(pcdev->vip_slave);
1678 pcdev->vip_slave = NULL;
1680 if (pcdev->vip_out) {
1681 clk_put(pcdev->vip_out);
1682 pcdev->vip_out = NULL;
1684 if (pcdev->vip_input) {
1685 clk_put(pcdev->vip_input);
1686 pcdev->vip_input = NULL;
1688 if (pcdev->vip_bus) {
1689 clk_put(pcdev->vip_bus);
1690 pcdev->vip_bus = NULL;
1692 if (pcdev->hclk_disp_matrix) {
1693 clk_put(pcdev->hclk_disp_matrix);
1694 pcdev->hclk_disp_matrix = NULL;
1696 if (pcdev->vip_matrix) {
1697 clk_put(pcdev->vip_matrix);
1698 pcdev->vip_matrix = NULL;
1702 rk29_camdev_info_ptr = NULL;
1707 static int __devexit rk29_camera_remove(struct platform_device *pdev)
1709 struct rk29_camera_dev *pcdev = platform_get_drvdata(pdev);
1710 struct resource *res;
1712 free_irq(pcdev->irq, pcdev);
1714 if (pcdev->camera_wq) {
1715 destroy_workqueue(pcdev->camera_wq);
1716 pcdev->camera_wq = NULL;
1719 soc_camera_host_unregister(&pcdev->soc_host);
1722 release_mem_region(res->start, res->end - res->start + 1);
1724 if (pcdev->pdata && pcdev->pdata->io_deinit) { /* ddl@rock-chips.com : Free IO in deinit function */
1725 pcdev->pdata->io_deinit(0);
1726 pcdev->pdata->io_deinit(1);
1730 rk29_camdev_info_ptr = NULL;
1731 dev_info(&pdev->dev, "RK28 Camera driver unloaded\n");
1736 static struct platform_driver rk29_camera_driver =
1739 .name = RK29_CAM_DRV_NAME,
1741 .probe = rk29_camera_probe,
1742 .remove = __devexit_p(rk29_camera_remove),
1746 static int __devinit rk29_camera_init(void)
1748 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1749 return platform_driver_register(&rk29_camera_driver);
1752 static void __exit rk29_camera_exit(void)
1754 platform_driver_unregister(&rk29_camera_driver);
1757 device_initcall_sync(rk29_camera_init);
1758 module_exit(rk29_camera_exit);
1760 MODULE_DESCRIPTION("RK29 Soc Camera Host driver");
1761 MODULE_AUTHOR("ddl <ddl@rock-chips>");
1762 MODULE_LICENSE("GPL");