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(0, 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, 1)
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 videobuf_queue *vb_vidq_ptr;
244 struct rk29_camera_reg reginfo_suspend;
245 struct workqueue_struct *camera_wq;
246 struct rk29_camera_work *camera_work;
247 unsigned int camera_work_count;
248 struct hrtimer fps_timer;
249 struct work_struct camera_reinit_work;
251 rk29_camera_sensor_cb_s icd_cb;
253 static DEFINE_MUTEX(camera_lock);
254 static const char *rk29_cam_driver_description = "RK29_Camera";
255 static struct rk29_camera_dev *rk29_camdev_info_ptr;
257 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable);
261 * Videobuf operations
263 static int rk29_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
266 struct soc_camera_device *icd = vq->priv_data;
267 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
268 struct rk29_camera_dev *pcdev = ici->priv;
269 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
271 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
273 /* planar capture requires Y, U and V buffers to be page aligned */
275 *size = PAGE_ALIGN(icd->user_width* icd->user_height * bytes_per_pixel); /* Y pages UV pages, yuv422*/
276 pcdev->vipmem_bsize = PAGE_ALIGN(pcdev->host_width * pcdev->host_height * bytes_per_pixel);
278 *size = PAGE_ALIGN((icd->user_width* icd->user_height * icd->current_fmt->depth + 7)>>3); /* Y pages UV pages, yuv422*/
279 pcdev->vipmem_bsize = PAGE_ALIGN((pcdev->host_width * pcdev->host_height * icd->current_fmt->depth + 7)>>3);
282 if (CAM_WORKQUEUE_IS_EN()) {
283 if (CAM_IPPWORK_IS_EN()) {
284 if (*count > pcdev->vipmem_size/pcdev->vipmem_bsize) { /* Buffers must be limited, when this resolution is genered by IPP */
285 *count = pcdev->vipmem_size/pcdev->vipmem_bsize;
288 if ((pcdev->camera_work_count != *count) && pcdev->camera_work) {
289 kfree(pcdev->camera_work);
290 pcdev->camera_work = NULL;
291 pcdev->camera_work_count = 0;
294 if (pcdev->camera_work == NULL) {
295 pcdev->camera_work = kmalloc(sizeof(struct rk29_camera_work)*(*count), GFP_KERNEL);
296 if (pcdev->camera_work == NULL) {
297 RK29CAMERA_TR("\n %s kmalloc fail\n", __FUNCTION__);
300 pcdev->camera_work_count = *count;
304 RK29CAMERA_DG("%s..%d.. videobuf size:%d, vipmem_buf size:%d \n",__FUNCTION__,__LINE__, *size,pcdev->vipmem_bsize);
308 static void rk29_videobuf_free(struct videobuf_queue *vq, struct rk29_buffer *buf)
310 struct soc_camera_device *icd = vq->priv_data;
312 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
313 &buf->vb, buf->vb.baddr, buf->vb.bsize);
315 /* ddl@rock-chips.com: buf_release called soc_camera_streamoff and soc_camera_close*/
316 if (buf->vb.state == VIDEOBUF_NEEDS_INIT)
322 videobuf_dma_contig_free(vq, &buf->vb);
323 dev_dbg(&icd->dev, "%s freed\n", __func__);
324 buf->vb.state = VIDEOBUF_NEEDS_INIT;
327 static int rk29_videobuf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field)
329 struct soc_camera_device *icd = vq->priv_data;
330 struct rk29_buffer *buf;
333 buf = container_of(vb, struct rk29_buffer, vb);
335 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
336 vb, vb->baddr, vb->bsize);
338 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
340 /* Added list head initialization on alloc */
341 WARN_ON(!list_empty(&vb->queue));
343 /* This can be useful if you want to see if we actually fill
344 * the buffer with something */
345 //memset((void *)vb->baddr, 0xaa, vb->bsize);
347 BUG_ON(NULL == icd->current_fmt);
349 if (buf->fmt != icd->current_fmt ||
350 vb->width != icd->user_width ||
351 vb->height != icd->user_height ||
352 vb->field != field) {
353 buf->fmt = icd->current_fmt;
354 vb->width = icd->user_width;
355 vb->height = icd->user_height;
357 vb->state = VIDEOBUF_NEEDS_INIT;
360 vb->size = (((vb->width * vb->height *buf->fmt->depth) + 7) >> 3) ; /* ddl@rock-chips.com : fmt->depth is coorect */
361 if (0 != vb->baddr && vb->bsize < vb->size) {
366 if (vb->state == VIDEOBUF_NEEDS_INIT) {
367 ret = videobuf_iolock(vq, vb, NULL);
371 vb->state = VIDEOBUF_PREPARED;
373 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
376 rk29_videobuf_free(vq, buf);
381 static inline void rk29_videobuf_capture(struct videobuf_buffer *vb)
383 unsigned int y_addr,uv_addr;
384 struct rk29_camera_dev *pcdev = rk29_camdev_info_ptr;
387 if (CAM_IPPWORK_IS_EN()) {
388 y_addr = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
389 uv_addr = y_addr + pcdev->host_width*pcdev->host_height;
391 if (y_addr > (pcdev->vipmem_phybase + pcdev->vipmem_size - pcdev->vipmem_bsize)) {
392 RK29CAMERA_TR("vipmem for IPP is overflow! %dx%d -> %dx%d vb_index:%d\n",pcdev->host_width,pcdev->host_height,
393 pcdev->icd->user_width,pcdev->icd->user_height, vb->i);
398 uv_addr = y_addr + vb->width * vb->height;
400 write_vip_reg(RK29_VIP_CAPTURE_F1SA_Y, y_addr);
401 write_vip_reg(RK29_VIP_CAPTURE_F1SA_UV, uv_addr);
402 write_vip_reg(RK29_VIP_CAPTURE_F2SA_Y, y_addr);
403 write_vip_reg(RK29_VIP_CAPTURE_F2SA_UV, uv_addr);
404 write_vip_reg(RK29_VIP_FB_SR, 0x00000002);//frame1 has been ready to receive data,frame 2 is not used
407 /* Locking: Caller holds q->irqlock */
408 static void rk29_videobuf_queue(struct videobuf_queue *vq,
409 struct videobuf_buffer *vb)
411 struct soc_camera_device *icd = vq->priv_data;
412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
413 struct rk29_camera_dev *pcdev = ici->priv;
415 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
416 vb, vb->baddr, vb->bsize);
418 vb->state = VIDEOBUF_QUEUED;
420 if (list_empty(&pcdev->capture)) {
421 list_add_tail(&vb->queue, &pcdev->capture);
423 if (list_entry(pcdev->capture.next, struct videobuf_buffer, queue) != vb)
424 list_add_tail(&vb->queue, &pcdev->capture);
426 BUG(); /* ddl@rock-chips.com : The same videobuffer queue again */
429 if (!pcdev->active) {
431 rk29_videobuf_capture(vb);
434 static int rk29_pixfmt2ippfmt(unsigned int pixfmt, int *ippfmt)
438 case V4L2_PIX_FMT_YUV422P:
440 *ippfmt = IPP_Y_CBCR_H2V1;
443 case V4L2_PIX_FMT_YUV420:
445 *ippfmt = IPP_Y_CBCR_H2V2;
449 goto rk29_pixfmt2ippfmt_err;
453 rk29_pixfmt2ippfmt_err:
456 static void rk29_camera_capture_process(struct work_struct *work)
458 struct rk29_camera_work *camera_work = container_of(work, struct rk29_camera_work, work);
459 struct videobuf_buffer *vb = camera_work->vb;
460 struct rk29_camera_dev *pcdev = camera_work->pcdev;
461 struct rk29_ipp_req ipp_req;
462 unsigned long int flags;
464 if (CAM_IPPWORK_IS_EN()) {
465 ipp_req.src0.YrgbMst = pcdev->vipmem_phybase + vb->i*pcdev->vipmem_bsize;
466 ipp_req.src0.CbrMst= ipp_req.src0.YrgbMst + pcdev->host_width*pcdev->host_height;
467 ipp_req.src0.w = pcdev->host_width;
468 ipp_req.src0.h = pcdev->host_height;
469 rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.src0.fmt);
471 ipp_req.dst0.YrgbMst = vb->boff;
472 ipp_req.dst0.CbrMst= vb->boff+vb->width * vb->height;
473 ipp_req.dst0.w = pcdev->icd->user_width;
474 ipp_req.dst0.h = pcdev->icd->user_height;
475 rk29_pixfmt2ippfmt(pcdev->pixfmt, &ipp_req.dst0.fmt);
477 ipp_req.src_vir_w = ipp_req.src0.w;
478 ipp_req.dst_vir_w = ipp_req.dst0.w;
479 ipp_req.timeout = 100;
480 ipp_req.flag = IPP_ROT_0;
482 if (ipp_do_blit(&ipp_req)) {
483 spin_lock_irqsave(&pcdev->lock, flags);
484 vb->state = VIDEOBUF_ERROR;
485 spin_unlock_irqrestore(&pcdev->lock, flags);
486 RK29CAMERA_TR("Capture image(vb->i:0x%x) which IPP operated is error!\n", vb->i);
487 RK29CAMERA_TR("ipp_req.src0.YrgbMst:0x%x ipp_req.src0.CbrMst:0x%x \n", ipp_req.src0.YrgbMst,ipp_req.src0.CbrMst);
488 RK29CAMERA_TR("ipp_req.src0.w:0x%x ipp_req.src0.h:0x%x \n",ipp_req.src0.w,ipp_req.src0.h);
489 RK29CAMERA_TR("ipp_req.src0.fmt:0x%x\n",ipp_req.src0.fmt);
490 RK29CAMERA_TR("ipp_req.dst0.YrgbMst:0x%x ipp_req.dst0.CbrMst:0x%x \n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
491 RK29CAMERA_TR("ipp_req.dst0.w:0x%x ipp_req.dst0.h:0x%x \n",ipp_req.dst0.w ,ipp_req.dst0.h);
492 RK29CAMERA_TR("ipp_req.dst0.fmt:0x%x\n",ipp_req.dst0.fmt);
493 RK29CAMERA_TR("ipp_req.src_vir_w:0x%x ipp_req.dst_vir_w :0x%x\n",ipp_req.src_vir_w ,ipp_req.dst_vir_w);
494 RK29CAMERA_TR("ipp_req.timeout:0x%x ipp_req.flag :0x%x\n",ipp_req.timeout,ipp_req.flag);
498 if (pcdev->icd_cb.sensor_cb)
499 (pcdev->icd_cb.sensor_cb)(vb);
501 wake_up(&(camera_work->vb->done));
503 static irqreturn_t rk29_camera_irq(int irq, void *data)
505 struct rk29_camera_dev *pcdev = data;
506 struct videobuf_buffer *vb;
507 struct rk29_camera_work *wk;
509 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
510 /* ddl@rock-chps.com : Current VIP is run in One Frame Mode, Frame 1 is validate */
511 if (read_vip_reg(RK29_VIP_FB_SR) & 0x01) {
514 goto RK29_CAMERA_IRQ_END;
516 if (pcdev->frame_inval>0) {
517 pcdev->frame_inval--;
518 rk29_videobuf_capture(pcdev->active);
519 goto RK29_CAMERA_IRQ_END;
520 } else if (pcdev->frame_inval) {
521 RK29CAMERA_TR("frame_inval : %0x",pcdev->frame_inval);
522 pcdev->frame_inval = 0;
526 /* ddl@rock-chips.com : this vb may be deleted from queue */
527 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
528 list_del_init(&vb->queue);
531 pcdev->active = NULL;
532 if (!list_empty(&pcdev->capture)) {
533 pcdev->active = list_entry(pcdev->capture.next, struct videobuf_buffer, queue);
535 rk29_videobuf_capture(pcdev->active);
539 if (pcdev->active == NULL) {
540 RK29CAMERA_DG("%s video_buf queue is empty!\n",__FUNCTION__);
543 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
544 vb->state = VIDEOBUF_DONE;
545 do_gettimeofday(&vb->ts);
549 if (CAM_WORKQUEUE_IS_EN()) {
550 wk = pcdev->camera_work + vb->i;
551 INIT_WORK(&(wk->work), rk29_camera_capture_process);
554 queue_work(pcdev->camera_wq, &(wk->work));
565 static void rk29_videobuf_release(struct videobuf_queue *vq,
566 struct videobuf_buffer *vb)
568 struct rk29_buffer *buf = container_of(vb, struct rk29_buffer, vb);
569 struct soc_camera_device *icd = vq->priv_data;
570 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
571 struct rk29_camera_dev *pcdev = ici->priv;
574 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
575 vb, vb->baddr, vb->bsize);
579 case VIDEOBUF_ACTIVE:
580 dev_dbg(&icd->dev, "%s (active)\n", __func__);
582 case VIDEOBUF_QUEUED:
583 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
585 case VIDEOBUF_PREPARED:
586 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
589 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
593 if (vb == pcdev->active) {
594 RK29CAMERA_DG("%s Wait for this video buf(0x%x) write finished!\n ",__FUNCTION__,(unsigned int)vb);
595 interruptible_sleep_on_timeout(&vb->done, 100);
596 RK29CAMERA_DG("%s This video buf(0x%x) write finished, release now!!\n",__FUNCTION__,(unsigned int)vb);
598 rk29_videobuf_free(vq, buf);
601 static struct videobuf_queue_ops rk29_videobuf_ops =
603 .buf_setup = rk29_videobuf_setup,
604 .buf_prepare = rk29_videobuf_prepare,
605 .buf_queue = rk29_videobuf_queue,
606 .buf_release = rk29_videobuf_release,
609 static void rk29_camera_init_videobuf(struct videobuf_queue *q,
610 struct soc_camera_device *icd)
612 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
613 struct rk29_camera_dev *pcdev = ici->priv;
615 /* We must pass NULL as dev pointer, then all pci_* dma operations
616 * transform to normal dma_* ones. */
617 videobuf_queue_dma_contig_init(q,
619 ici->v4l2_dev.dev, &pcdev->lock,
620 V4L2_BUF_TYPE_VIDEO_CAPTURE,
622 sizeof(struct rk29_buffer),
624 pcdev->vb_vidq_ptr = q; /* ddl@rock-chips.com */
626 static int rk29_camera_activate(struct rk29_camera_dev *pcdev, struct soc_camera_device *icd)
628 unsigned long sensor_bus_flags = SOCAM_MCLK_24MHZ;
631 RK29CAMERA_DG("%s..%d.. \n",__FUNCTION__,__LINE__);
632 if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix || !pcdev->hclk_cpu_display ||
633 !pcdev->vip_slave || !pcdev->vip_out || !pcdev->vip_input || !pcdev->vip_bus || !pcdev->pd_display ||
634 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) || IS_ERR(pcdev->hclk_cpu_display) || IS_ERR(pcdev->pd_display) ||
635 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip_out) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus)) {
637 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
638 goto RK29_CAMERA_ACTIVE_ERR;
641 if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
642 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix)) {
644 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
645 goto RK29_CAMERA_ACTIVE_ERR;
648 clk_enable(pcdev->pd_display);
650 clk_enable(pcdev->aclk_ddr_lcdc);
651 clk_enable(pcdev->aclk_disp_matrix);
653 clk_enable(pcdev->hclk_cpu_display);
654 clk_enable(pcdev->vip_slave);
656 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
657 clk_enable(pcdev->hclk_disp_matrix);
658 clk_enable(pcdev->vip_matrix);
661 clk_enable(pcdev->vip_input);
662 clk_enable(pcdev->vip_bus);
664 //if (icd->ops->query_bus_param) /* ddl@rock-chips.com : Query Sensor's xclk */
665 //sensor_bus_flags = icd->ops->query_bus_param(icd);
667 if (sensor_bus_flags & SOCAM_MCLK_48MHZ) {
668 parent = clk_get(NULL, "clk48m");
669 if (!parent || IS_ERR(parent))
670 goto RK29_CAMERA_ACTIVE_ERR;
671 } else if (sensor_bus_flags & SOCAM_MCLK_27MHZ) {
672 parent = clk_get(NULL, "extclk");
673 if (!parent || IS_ERR(parent))
674 goto RK29_CAMERA_ACTIVE_ERR;
676 parent = clk_get(NULL, "xin24m");
677 if (!parent || IS_ERR(parent))
678 goto RK29_CAMERA_ACTIVE_ERR;
681 clk_set_parent(pcdev->vip_out, parent);
683 clk_enable(pcdev->vip_out);
684 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_VIP_CLKOUT);
687 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
688 write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)|VIP_AHBMASTER); //VIP Config to AHB
689 write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)&(~VIP_ACLK_DIV_HCLK_2)); //aclk:hclk = 1:1
691 write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)&(~VIP_AHBMASTER)); //VIP Config to AXI
692 write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)|VIP_ACLK_DIV_HCLK_2); //aclk:hclk = 2:1
696 write_vip_reg(RK29_VIP_RESET, 0x76543210); /* ddl@rock-chips.com : vip software reset */
699 write_vip_reg(RK29_VIP_AHBR_CTRL, 0x07); /* ddl@rock-chips.com : vip ahb burst 16 */
700 write_vip_reg(RK29_VIP_INT_MASK, 0x01); //capture complete interrupt enable
701 write_vip_reg(RK29_VIP_CRM, 0x00000000); //Y/CB/CR color modification
704 RK29_CAMERA_ACTIVE_ERR:
708 static void rk29_camera_deactivate(struct rk29_camera_dev *pcdev)
710 //pcdev->active = NULL;
712 write_vip_reg(RK29_VIP_CTRL, 0);
713 read_vip_reg(RK29_VIP_INT_STS); //clear vip interrupte single
715 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_GPIO1B4);
716 clk_disable(pcdev->vip_out);
718 clk_disable(pcdev->vip_input);
719 clk_disable(pcdev->vip_bus);
721 #if (CONFIG_RK29_CAM_WORK_BUS==RK29_CAM_AHB)
722 clk_disable(pcdev->hclk_disp_matrix);
723 clk_disable(pcdev->vip_matrix);
726 clk_disable(pcdev->hclk_cpu_display);
727 clk_disable(pcdev->vip_slave);
729 clk_disable(pcdev->aclk_ddr_lcdc);
730 clk_disable(pcdev->aclk_disp_matrix);
732 clk_disable(pcdev->pd_display);
736 /* The following two functions absolutely depend on the fact, that
737 * there can be only one camera on RK28 quick capture interface */
738 static int rk29_camera_add_device(struct soc_camera_device *icd)
740 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
741 struct rk29_camera_dev *pcdev = ici->priv;
742 struct device *control = to_soc_camera_control(icd);
743 struct v4l2_subdev *sd;
746 mutex_lock(&camera_lock);
753 dev_info(&icd->dev, "RK29 Camera driver attached to camera%d(%s)\n",
754 icd->devnum,dev_name(icd->pdev));
756 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
757 pcdev->active = NULL;
759 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
760 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
761 * if app havn't dequeue all videobuf before close camera device;
763 INIT_LIST_HEAD(&pcdev->capture);
765 ret = rk29_camera_activate(pcdev,icd);
769 /* ddl@rock-chips.com : v4l2_subdev is not created when ici->ops->add called in soc_camera_probe */
771 sd = dev_get_drvdata(control);
772 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_IOREQUEST,(void*)pcdev->pdata);
773 ret = v4l2_subdev_call(sd,core, init, 0);
776 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_CB_REGISTER,(void*)(&pcdev->icd_cb));
782 mutex_unlock(&camera_lock);
786 static void rk29_camera_remove_device(struct soc_camera_device *icd)
788 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
789 struct rk29_camera_dev *pcdev = ici->priv;
790 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
792 mutex_lock(&camera_lock);
793 BUG_ON(icd != pcdev->icd);
795 dev_info(&icd->dev, "RK29 Camera driver detached from camera%d(%s)\n",
796 icd->devnum,dev_name(icd->pdev));
798 /* ddl@rock-chips.com: Application will call VIDIOC_STREAMOFF before close device, but
799 stream may be turn on again before close device, if suspend and resume happened. */
800 if (read_vip_reg(RK29_VIP_CTRL) & ENABLE_CAPTURE) {
801 rk29_camera_s_stream(icd,0);
804 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_DEACTIVATE,NULL);
805 rk29_camera_deactivate(pcdev);
807 /* ddl@rock-chips.com: Call videobuf_mmap_free here for free the struct video_buffer which malloc in videobuf_alloc */
809 if (pcdev->vb_vidq_ptr) {
810 videobuf_mmap_free(pcdev->vb_vidq_ptr);
811 pcdev->vb_vidq_ptr = NULL;
814 pcdev->vb_vidq_ptr = NULL;
817 if (pcdev->camera_work) {
818 kfree(pcdev->camera_work);
819 pcdev->camera_work = NULL;
820 pcdev->camera_work_count = 0;
823 pcdev->active = NULL;
825 pcdev->icd_cb.sensor_cb = NULL;
826 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
827 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
828 * if app havn't dequeue all videobuf before close camera device;
830 INIT_LIST_HEAD(&pcdev->capture);
832 mutex_unlock(&camera_lock);
833 RK29CAMERA_DG("%s exit\n",__FUNCTION__);
838 static int rk29_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
840 unsigned long bus_flags, camera_flags, common_flags;
841 unsigned int vip_ctrl_val = 0;
844 RK29CAMERA_DG("%s..%d..\n",__FUNCTION__,__LINE__);
846 bus_flags = RK29_CAM_BUS_PARAM;
847 if (icd->ops->query_bus_param)
848 camera_flags = icd->ops->query_bus_param(icd);
852 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
855 goto RK29_CAMERA_SET_BUS_PARAM_END;
858 ret = icd->ops->set_bus_param(icd, common_flags);
860 goto RK29_CAMERA_SET_BUS_PARAM_END;
862 if (common_flags & SOCAM_DATAWIDTH_8) {
864 } else if (common_flags & SOCAM_DATAWIDTH_10) {
868 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
869 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) {
870 vip_ctrl_val |= NEGATIVE_EDGE;
872 vip_ctrl_val &= ~NEGATIVE_EDGE;
874 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) {
875 vip_ctrl_val |= HSY_LOW_ACTIVE;
877 vip_ctrl_val &= ~HSY_LOW_ACTIVE;
879 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) {
880 vip_ctrl_val |= VSY_HIGH_ACTIVE;
882 vip_ctrl_val &= ~VSY_HIGH_ACTIVE;
885 /* ddl@rock-chips.com : Don't enable capture here, enable in stream_on */
886 //vip_ctrl_val |= ENABLE_CAPTURE;
888 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
889 RK29CAMERA_DG("%s..ctrl:0x%x CtrReg=%x AXI_AHB:0x%x aclk_hclk:0x%x \n",__FUNCTION__,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL),
890 read_grf_reg(GRF_SOC_CON0_Reg)&VIP_AHBMASTER, read_grf_reg(GRF_OS_REG0)&VIP_ACLK_DIV_HCLK_2);
892 RK29_CAMERA_SET_BUS_PARAM_END:
894 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
898 static int rk29_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
900 unsigned long bus_flags, camera_flags;
903 bus_flags = RK29_CAM_BUS_PARAM;
904 if (icd->ops->query_bus_param) {
905 camera_flags = icd->ops->query_bus_param(icd);
909 ret = soc_camera_bus_param_compatible(camera_flags, bus_flags) ;
912 dev_warn(icd->dev.parent,
913 "Flags incompatible: camera %lx, host %lx\n",
914 camera_flags, bus_flags);
917 static const struct soc_camera_data_format rk29_camera_formats[] = {
919 .name = "Planar YUV420 12 bit",
921 .fourcc = V4L2_PIX_FMT_YUV420,
922 .colorspace = V4L2_COLORSPACE_JPEG,
924 .name = "Planar YUV422 16 bit",
926 .fourcc = V4L2_PIX_FMT_YUV422P,
927 .colorspace = V4L2_COLORSPACE_JPEG,
929 .name = "Raw Bayer RGB 10 bit",
931 .fourcc = V4L2_PIX_FMT_SGRBG10,
932 .colorspace = V4L2_COLORSPACE_SRGB,
936 static void rk29_camera_setup_format(struct soc_camera_device *icd, __u32 host_pixfmt, __u32 cam_pixfmt, struct v4l2_rect *rect)
938 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
939 struct rk29_camera_dev *pcdev = ici->priv;
940 unsigned int vip_fs = 0,vip_crop = 0;
941 unsigned int vip_ctrl_val = VIP_SENSOR|ONEFRAME|DISABLE_CAPTURE;
945 case V4L2_PIX_FMT_YUV422P:
946 vip_ctrl_val |= VIPREGYUV422;
947 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
948 pcdev->pixfmt = host_pixfmt;
950 case V4L2_PIX_FMT_YUV420:
951 vip_ctrl_val |= VIPREGYUV420;
952 if (pcdev->frame_inval != RK29_CAM_FRAME_INVAL_INIT)
953 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
954 pcdev->pixfmt = host_pixfmt;
956 case V4L2_PIX_FMT_SGRBG10:
957 vip_ctrl_val |= (VIP_RAW | VIP_SENSOR | VIP_DATA_LITTLEEND);
958 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
959 pcdev->pixfmt = host_pixfmt;
961 default: /* ddl@rock-chips.com : vip output format is hold when pixfmt is invalidate */
962 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & VIPREGYUV422);
968 case V4L2_PIX_FMT_UYVY:
969 vip_ctrl_val |= SENSOR_UYVY;
971 case V4L2_PIX_FMT_YUYV:
972 vip_ctrl_val |= SENSOR_YUYV;
975 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & SENSOR_YUYV);
979 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val); /* ddl@rock-chips.com: VIP capture mode and capture format must be set before FS register set */
981 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
983 if (vip_ctrl_val & ONEFRAME) {
984 vip_crop = ((rect->left<<16) + rect->top);
985 vip_fs = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
986 } else if (vip_ctrl_val & PING_PONG) {
990 write_vip_reg(RK29_VIP_CROP, vip_crop);
991 write_vip_reg(RK29_VIP_FS, vip_fs);
993 write_vip_reg(RK29_VIP_FB_SR, 0x00000003);
995 pcdev->host_width = rect->width;
996 pcdev->host_height = rect->height;
998 RK29CAMERA_DG("%s.. crop:0x%x fs:0x%x ctrl:0x%x CtrlReg:0x%x\n",__FUNCTION__,vip_crop,vip_fs,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL));
1002 static int rk29_camera_get_formats(struct soc_camera_device *icd, int idx,
1003 struct soc_camera_format_xlate *xlate)
1005 struct device *dev = icd->dev.parent;
1006 int formats = 0, buswidth, ret;
1010 ret = rk29_camera_try_bus_param(icd, buswidth);
1014 switch (icd->formats[idx].fourcc) {
1015 case V4L2_PIX_FMT_UYVY:
1016 case V4L2_PIX_FMT_YUYV:
1019 xlate->host_fmt = &rk29_camera_formats[0];
1020 xlate->cam_fmt = icd->formats + idx;
1021 xlate->buswidth = buswidth;
1023 dev_dbg(dev, "Providing format %s using %s\n",
1024 rk29_camera_formats[0].name,
1025 icd->formats[idx].name);
1030 xlate->host_fmt = &rk29_camera_formats[1];
1031 xlate->cam_fmt = icd->formats + idx;
1032 xlate->buswidth = buswidth;
1034 dev_dbg(dev, "Providing format %s using %s\n",
1035 rk29_camera_formats[1].name,
1036 icd->formats[idx].name);
1039 case V4L2_PIX_FMT_SGRBG10:
1042 xlate->host_fmt = &rk29_camera_formats[2];
1043 xlate->cam_fmt = icd->formats + idx;
1044 xlate->buswidth = 10;
1046 dev_dbg(dev, "Providing format %s using %s\n",
1047 rk29_camera_formats[2].name,
1048 icd->formats[idx].name);
1058 static void rk29_camera_put_formats(struct soc_camera_device *icd)
1063 static int rk29_camera_set_crop(struct soc_camera_device *icd,
1064 struct v4l2_crop *a)
1066 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1067 struct v4l2_format f;
1068 struct v4l2_pix_format *pix = &f.fmt.pix;
1071 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1073 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1077 if ((pix->width < (a->c.left + a->c.width)) || (pix->height < (a->c.top + a->c.height))) {
1079 pix->width = a->c.left + a->c.width;
1080 pix->height = a->c.top + a->c.height;
1082 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1083 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1084 icd->current_fmt->fourcc == V4L2_PIX_FMT_YUV422P ?4 : 0);
1086 ret = v4l2_subdev_call(sd, video, s_fmt, &f);
1091 rk29_camera_setup_format(icd, icd->current_fmt->fourcc, pix->pixelformat, &a->c);
1093 icd->user_width = pix->width;
1094 icd->user_height = pix->height;
1099 static int rk29_camera_set_fmt(struct soc_camera_device *icd,
1100 struct v4l2_format *f)
1102 struct device *dev = icd->dev.parent;
1103 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1104 const struct soc_camera_data_format *cam_fmt = NULL;
1105 const struct soc_camera_format_xlate *xlate = NULL;
1106 struct soc_camera_host *ici =to_soc_camera_host(icd->dev.parent);
1107 struct rk29_camera_dev *pcdev = ici->priv;
1108 struct v4l2_pix_format *pix = &f->fmt.pix;
1109 struct v4l2_format cam_f = *f;
1110 struct v4l2_rect rect;
1111 int ret,usr_w,usr_h;
1114 usr_h = pix->height;
1115 RK29CAMERA_DG("%s enter width:%d height:%d\n",__FUNCTION__,usr_w,usr_h);
1117 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1119 dev_err(dev, "Format %x not found\n", pix->pixelformat);
1121 goto RK29_CAMERA_SET_FMT_END;
1124 cam_fmt = xlate->cam_fmt;
1126 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1127 ret = v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1128 cam_f.fmt.pix.pixelformat = pix->pixelformat;
1129 *pix = cam_f.fmt.pix;
1130 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1131 if ((pix->width != usr_w) || (pix->height != usr_h)) {
1133 pix->height = usr_h;
1141 rect.width = cam_f.fmt.pix.width;
1142 rect.height = cam_f.fmt.pix.height;
1144 RK29CAMERA_DG("%s..%s..%s icd width:%d host width:%d \n",__FUNCTION__,xlate->host_fmt->name, cam_fmt->name,
1145 rect.width, pix->width);
1146 rk29_camera_setup_format(icd, pix->pixelformat, cam_fmt->fourcc, &rect);
1147 icd->buswidth = xlate->buswidth;
1148 icd->current_fmt = xlate->host_fmt;
1150 if (CAM_IPPWORK_IS_EN()) {
1151 BUG_ON(pcdev->vipmem_phybase == 0);
1155 RK29_CAMERA_SET_FMT_END:
1157 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
1160 static bool rk29_camera_fmt_capturechk(struct v4l2_format *f)
1164 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1166 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1168 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1170 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1172 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1177 RK29CAMERA_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1180 static int rk29_camera_try_fmt(struct soc_camera_device *icd,
1181 struct v4l2_format *f)
1183 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1184 struct rk29_camera_dev *pcdev = ici->priv;
1185 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1186 const struct soc_camera_format_xlate *xlate;
1187 struct v4l2_pix_format *pix = &f->fmt.pix;
1188 __u32 pixfmt = pix->pixelformat;
1189 enum v4l2_field field;
1190 int ret,usr_w,usr_h;
1191 bool is_capture = rk29_camera_fmt_capturechk(f);
1192 bool vipmem_is_overflow = false;
1195 usr_h = pix->height;
1196 RK29CAMERA_DG("%s enter width:%d height:%d\n",__FUNCTION__,usr_w,usr_h);
1198 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1200 dev_err(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
1202 goto RK29_CAMERA_TRY_FMT_END;
1204 /* limit to rk29 hardware capabilities */
1205 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
1206 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
1207 pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
1209 pix->bytesperline = pix->width * DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1210 pix->sizeimage = pix->height * pix->bytesperline;
1212 /* camera has to see its format, but the user the original one */
1213 pix->pixelformat = xlate->cam_fmt->fourcc;
1214 /* limit to sensor capabilities */
1215 ret = v4l2_subdev_call(sd, video, try_fmt, f);
1216 pix->pixelformat = pixfmt;
1217 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1218 if ((pix->width > usr_w) && (pix->height > usr_h)) {
1220 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3) > pcdev->vipmem_size);
1222 /* Assume preview buffer minimum is 4 */
1223 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3)*4 > pcdev->vipmem_size);
1225 if (vipmem_is_overflow == false) {
1227 pix->height = usr_h;
1229 RK29CAMERA_TR("vipmem for IPP is overflow, This resolution(%dx%d -> %dx%d) is invalidate!\n",pix->width,pix->height,usr_w,usr_h);
1231 } else if ((pix->width < usr_w) && (pix->height < usr_h)) {
1232 if (((usr_w>>1) < pix->width) && ((usr_h>>1) < pix->height)) {
1234 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3) > pcdev->vipmem_size);
1236 vipmem_is_overflow = (PAGE_ALIGN((pix->width*pix->height*icd->current_fmt->depth+7)>>3)*4 > pcdev->vipmem_size);
1238 if (vipmem_is_overflow == false) {
1240 pix->height = usr_h;
1242 RK29CAMERA_TR("vipmem for IPP is overflow, This resolution(%dx%d -> %dx%d) is invalidate!\n",pix->width,pix->height,usr_w,usr_h);
1245 RK29CAMERA_TR("The aspect ratio(%dx%d/%dx%d) is bigger than 2 !\n",pix->width,pix->height,usr_w,usr_h);
1252 if (field == V4L2_FIELD_ANY) {
1253 pix->field = V4L2_FIELD_NONE;
1254 } else if (field != V4L2_FIELD_NONE) {
1255 dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
1257 goto RK29_CAMERA_TRY_FMT_END;
1260 RK29_CAMERA_TRY_FMT_END:
1262 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
1266 static int rk29_camera_reqbufs(struct soc_camera_file *icf,
1267 struct v4l2_requestbuffers *p)
1271 /* This is for locking debugging only. I removed spinlocks and now I
1272 * check whether .prepare is ever called on a linked buffer, or whether
1273 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1274 * it hadn't triggered */
1275 for (i = 0; i < p->count; i++) {
1276 struct rk29_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1277 struct rk29_buffer, vb);
1279 INIT_LIST_HEAD(&buf->vb.queue);
1285 static unsigned int rk29_camera_poll(struct file *file, poll_table *pt)
1287 struct soc_camera_file *icf = file->private_data;
1288 struct rk29_buffer *buf;
1290 buf = list_entry(icf->vb_vidq.stream.next, struct rk29_buffer,
1293 poll_wait(file, &buf->vb.done, pt);
1295 if (buf->vb.state == VIDEOBUF_DONE ||
1296 buf->vb.state == VIDEOBUF_ERROR)
1297 return POLLIN|POLLRDNORM;
1302 static int rk29_camera_querycap(struct soc_camera_host *ici,
1303 struct v4l2_capability *cap)
1305 /* cap->name is set by the firendly caller:-> */
1306 strlcpy(cap->card, rk29_cam_driver_description, sizeof(cap->card));
1307 cap->version = RK29_CAM_VERSION_CODE;
1308 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1313 static int rk29_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1315 struct soc_camera_host *ici =
1316 to_soc_camera_host(icd->dev.parent);
1317 struct rk29_camera_dev *pcdev = ici->priv;
1318 struct v4l2_subdev *sd;
1321 mutex_lock(&camera_lock);
1322 if ((pcdev->icd == icd) && (icd->ops->suspend)) {
1323 rk29_camera_s_stream(icd, 0);
1324 sd = soc_camera_to_subdev(icd);
1325 v4l2_subdev_call(sd, video, s_stream, 0);
1326 ret = icd->ops->suspend(icd, state);
1328 pcdev->reginfo_suspend.VipCtrl = read_vip_reg(RK29_VIP_CTRL);
1329 pcdev->reginfo_suspend.VipCrop = read_vip_reg(RK29_VIP_CROP);
1330 pcdev->reginfo_suspend.VipFs = read_vip_reg(RK29_VIP_FS);
1331 pcdev->reginfo_suspend.VipIntMsk = read_vip_reg(RK29_VIP_INT_MASK);
1332 pcdev->reginfo_suspend.VipCrm = read_vip_reg(RK29_VIP_CRM);
1334 tmp = pcdev->reginfo_suspend.VipFs>>16; /* ddl@rock-chips.com */
1335 tmp += pcdev->reginfo_suspend.VipCrop>>16;
1336 pcdev->reginfo_suspend.VipFs = (pcdev->reginfo_suspend.VipFs & 0xffff) | (tmp<<16);
1338 pcdev->reginfo_suspend.Inval = Reg_Validate;
1339 rk29_camera_deactivate(pcdev);
1341 RK29CAMERA_DG("%s Enter Success...\n", __FUNCTION__);
1343 RK29CAMERA_DG("%s icd has been deattach, don't need enter suspend\n", __FUNCTION__);
1345 mutex_unlock(&camera_lock);
1349 static int rk29_camera_resume(struct soc_camera_device *icd)
1351 struct soc_camera_host *ici =
1352 to_soc_camera_host(icd->dev.parent);
1353 struct rk29_camera_dev *pcdev = ici->priv;
1354 struct v4l2_subdev *sd;
1357 mutex_lock(&camera_lock);
1358 if ((pcdev->icd == icd) && (icd->ops->resume)) {
1359 if (pcdev->reginfo_suspend.Inval == Reg_Validate) {
1360 rk29_camera_activate(pcdev, icd);
1361 write_vip_reg(RK29_VIP_INT_MASK, pcdev->reginfo_suspend.VipIntMsk);
1362 write_vip_reg(RK29_VIP_CRM, pcdev->reginfo_suspend.VipCrm);
1363 write_vip_reg(RK29_VIP_CTRL, pcdev->reginfo_suspend.VipCtrl&~ENABLE_CAPTURE);
1364 write_vip_reg(RK29_VIP_CROP, pcdev->reginfo_suspend.VipCrop);
1365 write_vip_reg(RK29_VIP_FS, pcdev->reginfo_suspend.VipFs);
1367 rk29_videobuf_capture(pcdev->active);
1368 rk29_camera_s_stream(icd, 1);
1369 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
1371 RK29CAMERA_TR("Resume fail, vip register recored is invalidate!!\n");
1372 goto rk29_camera_resume_end;
1375 ret = icd->ops->resume(icd);
1376 sd = soc_camera_to_subdev(icd);
1377 v4l2_subdev_call(sd, video, s_stream, 1);
1379 RK29CAMERA_DG("%s Enter success\n",__FUNCTION__);
1381 RK29CAMERA_DG("%s icd has been deattach, don't need enter resume\n", __FUNCTION__);
1384 rk29_camera_resume_end:
1385 mutex_unlock(&camera_lock);
1389 static void rk29_camera_reinit_work(struct work_struct *work)
1391 struct device *control;
1392 struct v4l2_subdev *sd;
1393 struct v4l2_format cam_f;
1394 const struct soc_camera_format_xlate *xlate;
1397 write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)&(~ENABLE_CAPTURE)));
1399 control = to_soc_camera_control(rk29_camdev_info_ptr->icd);
1400 sd = dev_get_drvdata(control);
1401 ret = v4l2_subdev_call(sd,core, init, 1);
1403 cam_f.fmt.pix.width = rk29_camdev_info_ptr->icd->user_width;
1404 cam_f.fmt.pix.height = rk29_camdev_info_ptr->icd->user_height;
1405 xlate = soc_camera_xlate_by_fourcc(rk29_camdev_info_ptr->icd, rk29_camdev_info_ptr->icd->current_fmt->fourcc);
1406 cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
1407 ret |= v4l2_subdev_call(sd, video, s_fmt, &cam_f);
1409 write_vip_reg(RK29_VIP_CTRL, (read_vip_reg(RK29_VIP_CTRL)|ENABLE_CAPTURE));
1411 RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor now! ret:0x%x\n",ret);
1413 static enum hrtimer_restart rk29_camera_fps_func(struct hrtimer *timer)
1415 RK29CAMERA_DG("rk29_camera_fps_func fps:0x%x\n",rk29_camdev_info_ptr->fps);
1416 if (rk29_camdev_info_ptr->fps < 3) {
1417 RK29CAMERA_TR("Camera host haven't recevie data from sensor,Reinit sensor delay!\n");
1418 INIT_WORK(&rk29_camdev_info_ptr->camera_reinit_work, rk29_camera_reinit_work);
1419 queue_work(rk29_camdev_info_ptr->camera_wq,&(rk29_camdev_info_ptr->camera_reinit_work));
1422 return HRTIMER_NORESTART;
1424 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable)
1426 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1427 struct rk29_camera_dev *pcdev = ici->priv;
1431 WARN_ON(pcdev->icd != icd);
1433 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
1436 hrtimer_cancel(&pcdev->fps_timer);
1437 hrtimer_start(&pcdev->fps_timer,ktime_set(2, 0),HRTIMER_MODE_REL);
1438 vip_ctrl_val |= ENABLE_CAPTURE;
1440 vip_ctrl_val &= ~ENABLE_CAPTURE;
1441 ret = hrtimer_cancel(&pcdev->fps_timer);
1442 ret |= flush_work(&rk29_camdev_info_ptr->camera_reinit_work);
1443 RK29CAMERA_DG("STREAM_OFF cancel timer and flush work:0x%x \n", ret);
1445 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
1447 RK29CAMERA_DG("%s.. enable : 0x%x \n", __FUNCTION__, enable);
1451 static struct soc_camera_host_ops rk29_soc_camera_host_ops =
1453 .owner = THIS_MODULE,
1454 .add = rk29_camera_add_device,
1455 .remove = rk29_camera_remove_device,
1456 .suspend = rk29_camera_suspend,
1457 .resume = rk29_camera_resume,
1458 .set_crop = rk29_camera_set_crop,
1459 .get_formats = rk29_camera_get_formats,
1460 .put_formats = rk29_camera_put_formats,
1461 .set_fmt = rk29_camera_set_fmt,
1462 .try_fmt = rk29_camera_try_fmt,
1463 .init_videobuf = rk29_camera_init_videobuf,
1464 .reqbufs = rk29_camera_reqbufs,
1465 .poll = rk29_camera_poll,
1466 .querycap = rk29_camera_querycap,
1467 .set_bus_param = rk29_camera_set_bus_param,
1468 .s_stream = rk29_camera_s_stream /* ddl@rock-chips.com : Add stream control for host */
1470 static int rk29_camera_probe(struct platform_device *pdev)
1472 struct rk29_camera_dev *pcdev;
1473 struct resource *res;
1477 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1478 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1479 irq = platform_get_irq(pdev, 0);
1480 if (!res || irq < 0) {
1485 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1487 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1491 rk29_camdev_info_ptr = pcdev;
1493 /*config output clk*/
1494 pcdev->aclk_ddr_lcdc = clk_get(&pdev->dev, "aclk_ddr_lcdc");
1495 pcdev->aclk_disp_matrix = clk_get(&pdev->dev, "aclk_disp_matrix");
1497 pcdev->hclk_cpu_display = clk_get(&pdev->dev, "hclk_cpu_display");
1498 pcdev->vip_slave = clk_get(&pdev->dev, "vip_slave");
1499 pcdev->vip_out = clk_get(&pdev->dev,"vip_out");
1500 pcdev->vip_input = clk_get(&pdev->dev,"vip_input");
1501 pcdev->vip_bus = clk_get(&pdev->dev, "vip_bus");
1503 pcdev->hclk_disp_matrix = clk_get(&pdev->dev,"hclk_disp_matrix");
1504 pcdev->vip_matrix = clk_get(&pdev->dev,"vip_matrix");
1506 pcdev->pd_display = clk_get(&pdev->dev,"pd_display");
1508 if (!pcdev->aclk_ddr_lcdc || !pcdev->aclk_disp_matrix || !pcdev->hclk_cpu_display ||
1509 !pcdev->vip_slave || !pcdev->vip_out || !pcdev->vip_input || !pcdev->vip_bus || !pcdev->pd_display ||
1510 IS_ERR(pcdev->aclk_ddr_lcdc) || IS_ERR(pcdev->aclk_disp_matrix) || IS_ERR(pcdev->hclk_cpu_display) || IS_ERR(pcdev->pd_display) ||
1511 IS_ERR(pcdev->vip_slave) || IS_ERR(pcdev->vip_out) || IS_ERR(pcdev->vip_input) || IS_ERR(pcdev->vip_bus)) {
1513 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(axi) source\n");
1518 if (!pcdev->hclk_disp_matrix || !pcdev->vip_matrix ||
1519 IS_ERR(pcdev->hclk_disp_matrix) || IS_ERR(pcdev->vip_matrix)) {
1521 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk(ahb) source\n");
1526 dev_set_drvdata(&pdev->dev, pcdev);
1529 pcdev->pdata = pdev->dev.platform_data; /* ddl@rock-chips.com : Request IO in init function */
1530 if (pcdev->pdata && pcdev->pdata->io_init) {
1531 pcdev->pdata->io_init();
1533 #ifdef CONFIG_VIDEO_RK29_WORK_IPP
1534 if (pcdev->pdata && (strcmp(pcdev->pdata->meminfo.name,"camera_ipp_mem")==0)) {
1535 pcdev->vipmem_phybase = pcdev->pdata->meminfo.start;
1536 pcdev->vipmem_size = pcdev->pdata->meminfo.size;
1537 RK29CAMERA_DG("\n%s Memory(start:0x%x size:0x%x) for IPP obtain \n",__FUNCTION__, pcdev->pdata->meminfo.start,pcdev->pdata->meminfo.size);
1539 RK29CAMERA_TR("\n%s Memory for IPP have not obtain! IPP Function is fail\n",__FUNCTION__);
1540 pcdev->vipmem_phybase = 0;
1541 pcdev->vipmem_size = 0;
1544 INIT_LIST_HEAD(&pcdev->capture);
1545 spin_lock_init(&pcdev->lock);
1548 * Request the regions.
1550 if (!request_mem_region(res->start, res->end - res->start + 1,
1551 RK29_CAM_DRV_NAME)) {
1556 pcdev->base = ioremap(res->start, res->end - res->start + 1);
1557 if (pcdev->base == NULL) {
1558 dev_err(pcdev->dev, "ioremap() of registers failed\n");
1564 pcdev->dev = &pdev->dev;
1566 /* config buffer address */
1568 err = request_irq(pcdev->irq, rk29_camera_irq, 0, RK29_CAM_DRV_NAME,
1571 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1575 pcdev->camera_wq = create_workqueue("camera wq");
1576 if (pcdev->camera_wq == NULL)
1578 INIT_WORK(&pcdev->camera_reinit_work, rk29_camera_reinit_work);
1580 pcdev->soc_host.drv_name = RK29_CAM_DRV_NAME;
1581 pcdev->soc_host.ops = &rk29_soc_camera_host_ops;
1582 pcdev->soc_host.priv = pcdev;
1583 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1584 pcdev->soc_host.nr = pdev->id;
1586 err = soc_camera_host_register(&pcdev->soc_host);
1590 hrtimer_init(&pcdev->fps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1591 pcdev->fps_timer.function = rk29_camera_fps_func;
1592 pcdev->icd_cb.sensor_cb = NULL;
1594 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1598 free_irq(pcdev->irq, pcdev);
1599 if (pcdev->camera_wq) {
1600 destroy_workqueue(pcdev->camera_wq);
1601 pcdev->camera_wq = NULL;
1604 iounmap(pcdev->base);
1606 release_mem_region(res->start, res->end - res->start + 1);
1608 if (pcdev->aclk_ddr_lcdc) {
1609 clk_put(pcdev->aclk_ddr_lcdc);
1610 pcdev->aclk_ddr_lcdc = NULL;
1612 if (pcdev->aclk_disp_matrix) {
1613 clk_put(pcdev->aclk_disp_matrix);
1614 pcdev->aclk_disp_matrix = NULL;
1616 if (pcdev->hclk_cpu_display) {
1617 clk_put(pcdev->hclk_cpu_display);
1618 pcdev->hclk_cpu_display = NULL;
1620 if (pcdev->vip_slave) {
1621 clk_put(pcdev->vip_slave);
1622 pcdev->vip_slave = NULL;
1624 if (pcdev->vip_out) {
1625 clk_put(pcdev->vip_out);
1626 pcdev->vip_out = NULL;
1628 if (pcdev->vip_input) {
1629 clk_put(pcdev->vip_input);
1630 pcdev->vip_input = NULL;
1632 if (pcdev->vip_bus) {
1633 clk_put(pcdev->vip_bus);
1634 pcdev->vip_bus = NULL;
1636 if (pcdev->hclk_disp_matrix) {
1637 clk_put(pcdev->hclk_disp_matrix);
1638 pcdev->hclk_disp_matrix = NULL;
1640 if (pcdev->vip_matrix) {
1641 clk_put(pcdev->vip_matrix);
1642 pcdev->vip_matrix = NULL;
1646 rk29_camdev_info_ptr = NULL;
1651 static int __devexit rk29_camera_remove(struct platform_device *pdev)
1653 struct rk29_camera_dev *pcdev = platform_get_drvdata(pdev);
1654 struct resource *res;
1656 free_irq(pcdev->irq, pcdev);
1658 if (pcdev->camera_wq) {
1659 destroy_workqueue(pcdev->camera_wq);
1660 pcdev->camera_wq = NULL;
1663 soc_camera_host_unregister(&pcdev->soc_host);
1666 release_mem_region(res->start, res->end - res->start + 1);
1668 if (pcdev->pdata && pcdev->pdata->io_deinit) { /* ddl@rock-chips.com : Free IO in deinit function */
1669 pcdev->pdata->io_deinit(0);
1670 pcdev->pdata->io_deinit(1);
1674 rk29_camdev_info_ptr = NULL;
1675 dev_info(&pdev->dev, "RK28 Camera driver unloaded\n");
1680 static struct platform_driver rk29_camera_driver =
1683 .name = RK29_CAM_DRV_NAME,
1685 .probe = rk29_camera_probe,
1686 .remove = __devexit_p(rk29_camera_remove),
1690 static int __devinit rk29_camera_init(void)
1692 RK29CAMERA_DG("%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1693 return platform_driver_register(&rk29_camera_driver);
1696 static void __exit rk29_camera_exit(void)
1698 platform_driver_unregister(&rk29_camera_driver);
1701 device_initcall_sync(rk29_camera_init);
1702 module_exit(rk29_camera_exit);
1704 MODULE_DESCRIPTION("RK29 Soc Camera Host driver");
1705 MODULE_AUTHOR("ddl <ddl@rock-chips>");
1706 MODULE_LICENSE("GPL");