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>
33 #include <mach/rk29_camera.h>
34 #include <mach/rk29_iomap.h>
35 #include <mach/iomux.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-dev.h>
39 #include <media/videobuf-dma-contig.h>
40 #include <media/soc_camera.h>
43 #define RK29_VIP_AHBR_CTRL 0x00
44 #define RK29_VIP_INT_MASK 0x04
45 #define RK29_VIP_INT_STS 0x08
46 #define RK29_VIP_STS 0x0c
47 #define RK29_VIP_CTRL 0x10
48 #define RK29_VIP_CAPTURE_F1SA_Y 0x14
49 #define RK29_VIP_CAPTURE_F1SA_UV 0x18
50 #define RK29_VIP_CAPTURE_F1SA_Cr 0x1c
51 #define RK29_VIP_CAPTURE_F2SA_Y 0x20
52 #define RK29_VIP_CAPTURE_F2SA_UV 0x24
53 #define RK29_VIP_CAPTURE_F2SA_Cr 0x28
54 #define RK29_VIP_FB_SR 0x2c
55 #define RK29_VIP_FS 0x30
56 #define RK29_VIP_VIPRESERVED 0x34
57 #define RK29_VIP_CROP 0x38
58 #define RK29_VIP_CRM 0x3c
59 #define RK29_VIP_RESET 0x40
60 #define RK29_VIP_L_SFT 0x44
62 //The key register bit descrition
64 #define DISABLE_CAPTURE (0x00<<0)
65 #define ENABLE_CAPTURE (0x01<<0)
66 #define HSY_HIGH_ACTIVE (0x00<<1)
67 #define HSY_LOW_ACTIVE (0x01<<1)
68 #define VIP_CCIR656 (0x00<<2)
69 #define VIP_SENSOR (0x01<<2)
70 #define SENSOR_UYVY (0x00<<3)
71 #define SENSOR_YUYV (0x01<<3)
72 #define VIP_YUV (0x00<<4)
73 #define VIP_RAW (0x01<<4)
74 #define CON_OR_PIN (0x00<<5)
75 #define ONEFRAME (0x01<<5)
76 #define VIPREGYUV420 (0x00<<6)
77 #define VIPREGYUV422 (0x01<<6)
78 #define FIELD0_START (0x00<<7)
79 #define FIELD1_START (0x01<<7)
80 #define CONTINUOUS (0x00<<8)
81 #define PING_PONG (0x01<<8)
82 #define POSITIVE_EDGE (0x00<<9)
83 #define NEGATIVE_EDGE (0x01<<9)
84 #define VIPREGNTSC (0x00<<10)
85 #define VIPREGPAL (0x01<<10)
86 #define VIP_DATA_LITTLEEND (0x00<<11)
87 #define VIP_DATA_BIGEND (0x01<<11)
88 #define VSY_LOW_ACTIVE (0x00<<12)
89 #define VSY_HIGH_ACTIVE (0x01<<12)
90 #define VIP_RAWINPUT_BYPASS (0x00<<13)
91 #define VIP_RAWINPUT_POSITIVE_EDGE (0x01<<13)
92 #define VIP_RAWINPUT_NEGATIVE_EDGE (0x02<<13)
95 #define GRF_SOC_CON0_Reg 0xbc
96 #define VIP_AXIMASTER (0x00<<0)
97 #define VIP_AHBMASTER (0x01<<2)
100 #define GRF_OS_REG0 0xd0
101 #define VIP_ACLK_DIV_HCLK_1 (0x00<<0)
102 #define VIP_ACLK_DIV_HCLK_2 (0x01<<0)
105 #define MIN(x,y) ((x<y) ? x: y)
106 #define MAX(x,y) ((x>y) ? x: y)
107 #define RK29_SENSOR_24MHZ 24 /* MHz */
108 #define RK29_SENSOR_48MHZ 48
110 #define write_vip_reg(addr, val) __raw_writel(val, addr+(rk29_camdev_info_ptr->base))
111 #define read_vip_reg(addr) __raw_readl(addr+(rk29_camdev_info_ptr->base))
112 #define mask_vip_reg(addr, msk, val) write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
114 #define write_grf_reg(addr, val) __raw_writel(val, addr+RK29_GRF_BASE)
115 #define read_grf_reg(addr) __raw_readl(addr+RK29_GRF_BASE)
116 #define mask_grf_reg(addr, msk, val) write_vip_reg(addr, (val)|((~(msk))&read_vip_reg(addr)))
119 #define CONFIG_RK29CAMERA_TR 1
120 #define CONFIG_RK29CAMERA_DEBUG 1
121 #if (CONFIG_RK29CAMERA_TR)
122 #define RK29CAMERA_TR(format, ...) printk(format, ## __VA_ARGS__)
123 #if (CONFIG_RK29CAMERA_DEBUG)
124 #define RK29CAMERA_DG(format, ...) printk(format, ## __VA_ARGS__)
126 #define RK29CAMERA_DG(format, ...)
129 #define RK29CAMERA_TR(format, ...)
130 #define RK29CAMERA_DG(format, ...)
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 1 /* ddl@rock-chips.com : */
157 extern void videobuf_dma_contig_free(struct videobuf_queue *q, struct videobuf_buffer *buf);
158 extern dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
159 extern void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
160 struct videobuf_queue_ops *ops,
163 enum v4l2_buf_type type,
164 enum v4l2_field field,
168 /* buffer for one video frame */
171 /* common v4l buffer stuff -- must be first */
172 struct videobuf_buffer vb;
173 const struct soc_camera_data_format *fmt;
176 enum rk29_camera_reg_state
182 struct rk29_camera_reg
184 unsigned int VipCtrl;
185 unsigned int VipCrop;
187 unsigned int VipIntMsk;
189 enum rk29_camera_reg_state Inval;
191 struct rk29_camera_dev
193 struct soc_camera_host soc_host;
195 /* RK2827x is only supposed to handle one camera on its Quick Capture
196 * interface. If anyone ever builds hardware to enable more than
197 * one camera, they will have to modify this driver too */
198 struct soc_camera_device *icd;
201 void __iomem *grf_base;
202 int frame_inval; /* ddl@rock-chips.com : The first frames is invalidate */
205 struct rk29camera_platform_data *pdata;
206 struct resource *res;
208 struct list_head capture;
212 struct videobuf_buffer *active;
213 struct videobuf_queue *vb_vidq_ptr;
214 struct rk29_camera_reg reginfo_suspend;
216 static DEFINE_MUTEX(camera_lock);
217 static const char *rk29_cam_driver_description = "RK29_Camera";
218 static struct rk29_camera_dev *rk29_camdev_info_ptr;
220 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable);
224 * Videobuf operations
226 static int rk29_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
229 struct soc_camera_device *icd = vq->priv_data;
230 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
232 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
234 /* planar capture requires Y, U and V buffers to be page aligned */
235 *size = PAGE_ALIGN( icd->user_width * icd->user_height * bytes_per_pixel); /* Y pages UV pages, yuv422*/
237 RK29CAMERA_DG("\n%s..%d.. size = %d\n",__FUNCTION__,__LINE__, *size);
241 static void rk29_videobuf_free(struct videobuf_queue *vq, struct rk29_buffer *buf)
243 struct soc_camera_device *icd = vq->priv_data;
245 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
246 &buf->vb, buf->vb.baddr, buf->vb.bsize);
248 /* ddl@rock-chips.com: buf_release called soc_camera_streamoff and soc_camera_close*/
249 if (buf->vb.state == VIDEOBUF_NEEDS_INIT)
255 videobuf_dma_contig_free(vq, &buf->vb);
256 dev_dbg(&icd->dev, "%s freed\n", __func__);
257 buf->vb.state = VIDEOBUF_NEEDS_INIT;
260 static int rk29_videobuf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field)
262 struct soc_camera_device *icd = vq->priv_data;
263 struct rk29_buffer *buf;
266 buf = container_of(vb, struct rk29_buffer, vb);
268 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
269 vb, vb->baddr, vb->bsize);
271 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
273 /* Added list head initialization on alloc */
274 WARN_ON(!list_empty(&vb->queue));
276 /* This can be useful if you want to see if we actually fill
277 * the buffer with something */
278 //memset((void *)vb->baddr, 0xaa, vb->bsize);
280 BUG_ON(NULL == icd->current_fmt);
282 if (buf->fmt != icd->current_fmt ||
283 vb->width != icd->user_width ||
284 vb->height != icd->user_height ||
285 vb->field != field) {
286 buf->fmt = icd->current_fmt;
287 vb->width = icd->user_width;
288 vb->height = icd->user_height;
290 vb->state = VIDEOBUF_NEEDS_INIT;
293 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3) ; /* ddl@rock-chips.com : fmt->depth is coorect */
294 if (0 != vb->baddr && vb->bsize < vb->size) {
299 if (vb->state == VIDEOBUF_NEEDS_INIT) {
300 ret = videobuf_iolock(vq, vb, NULL);
304 vb->state = VIDEOBUF_PREPARED;
306 //RK29CAMERA_TR("\n%s..%d.. \n",__FUNCTION__,__LINE__);
309 rk29_videobuf_free(vq, buf);
314 static inline void rk29_videobuf_capture(struct videobuf_buffer *vb)
319 size = vb->width * vb->height; /* Y pages UV pages, yuv422*/
320 write_vip_reg(RK29_VIP_CAPTURE_F1SA_Y, vb->boff);
321 write_vip_reg(RK29_VIP_CAPTURE_F1SA_UV, vb->boff + size);
322 write_vip_reg(RK29_VIP_CAPTURE_F2SA_Y, vb->boff);
323 write_vip_reg(RK29_VIP_CAPTURE_F2SA_UV, vb->boff + size);
324 write_vip_reg(RK29_VIP_FB_SR, 0x00000002);//frame1 has been ready to receive data,frame 2 is not used
327 /* Locking: Caller holds q->irqlock */
328 static void rk29_videobuf_queue(struct videobuf_queue *vq,
329 struct videobuf_buffer *vb)
331 struct soc_camera_device *icd = vq->priv_data;
332 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
333 struct rk29_camera_dev *pcdev = ici->priv;
335 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
336 vb, vb->baddr, vb->bsize);
338 vb->state = VIDEOBUF_QUEUED;
340 if (!list_empty(&pcdev->capture)) {
341 list_add_tail(&vb->queue, &pcdev->capture);
343 if (list_entry(pcdev->capture.next, struct videobuf_buffer, queue) != vb)
344 list_add_tail(&vb->queue, &pcdev->capture);
346 BUG(); /* ddl@rock-chips.com : The same videobuffer queue again */
349 if (!pcdev->active) {
351 rk29_videobuf_capture(vb);
354 static irqreturn_t rk29_camera_irq(int irq, void *data)
356 struct rk29_camera_dev *pcdev = data;
357 struct videobuf_buffer *vb;
359 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
361 /* ddl@rock-chps.com : Current VIP is run in One Frame Mode, Frame 1 is validate */
362 if (read_vip_reg(RK29_VIP_FB_SR) & 0x01) {
365 goto RK29_CAMERA_IRQ_END;
367 if (pcdev->frame_inval>0) {
368 pcdev->frame_inval--;
369 rk29_videobuf_capture(pcdev->active);
370 goto RK29_CAMERA_IRQ_END;
371 } else if (pcdev->frame_inval) {
372 RK29CAMERA_TR("frame_inval : %0x",pcdev->frame_inval);
373 pcdev->frame_inval = 0;
377 /* ddl@rock-chips.com : this vb may be deleted from queue */
378 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
379 list_del_init(&vb->queue);
382 pcdev->active = NULL;
383 if (!list_empty(&pcdev->capture)) {
384 pcdev->active = list_entry(pcdev->capture.next, struct videobuf_buffer, queue);
386 rk29_videobuf_capture(pcdev->active);
390 if (pcdev->active == NULL) {
391 RK29CAMERA_DG("%s video_buf queue is empty!\n",__FUNCTION__);
394 if ((vb->state == VIDEOBUF_QUEUED) || (vb->state == VIDEOBUF_ACTIVE)) {
395 vb->state = VIDEOBUF_DONE;
396 do_gettimeofday(&vb->ts);
408 static void rk29_videobuf_release(struct videobuf_queue *vq,
409 struct videobuf_buffer *vb)
411 struct rk29_buffer *buf = container_of(vb, struct rk29_buffer, vb);
412 struct soc_camera_device *icd = vq->priv_data;
413 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
414 struct rk29_camera_dev *pcdev = ici->priv;
417 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
418 vb, vb->baddr, vb->bsize);
422 case VIDEOBUF_ACTIVE:
423 dev_dbg(&icd->dev, "%s (active)\n", __func__);
425 case VIDEOBUF_QUEUED:
426 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
428 case VIDEOBUF_PREPARED:
429 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
432 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
436 if (vb == pcdev->active) {
437 RK29CAMERA_DG("%s Wait for this video buf(0x%x) write finished!\n ",__FUNCTION__,(unsigned int)vb);
438 interruptible_sleep_on_timeout(&vb->done, 100);
439 RK29CAMERA_DG("%s This video buf(0x%x) write finished, release now!!\n",__FUNCTION__,(unsigned int)vb);
441 rk29_videobuf_free(vq, buf);
444 static struct videobuf_queue_ops rk29_videobuf_ops =
446 .buf_setup = rk29_videobuf_setup,
447 .buf_prepare = rk29_videobuf_prepare,
448 .buf_queue = rk29_videobuf_queue,
449 .buf_release = rk29_videobuf_release,
452 static void rk29_camera_init_videobuf(struct videobuf_queue *q,
453 struct soc_camera_device *icd)
455 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
456 struct rk29_camera_dev *pcdev = ici->priv;
458 /* We must pass NULL as dev pointer, then all pci_* dma operations
459 * transform to normal dma_* ones. */
460 videobuf_queue_dma_contig_init(q,
462 ici->v4l2_dev.dev, &pcdev->lock,
463 V4L2_BUF_TYPE_VIDEO_CAPTURE,
465 sizeof(struct rk29_buffer),
467 pcdev->vb_vidq_ptr = q; /* ddl@rock-chips.com */
469 static int rk29_camera_activate(struct rk29_camera_dev *pcdev, struct soc_camera_device *icd)
471 unsigned long sensor_bus_flags = SOCAM_MCLK_24MHZ;
474 RK29CAMERA_DG("\n%s..%d.. \n",__FUNCTION__,__LINE__);
475 if (!pcdev->clk || IS_ERR(pcdev->clk))
476 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk source\n");
478 //if (icd->ops->query_bus_param) /* ddl@rock-chips.com : Query Sensor's xclk */
479 //sensor_bus_flags = icd->ops->query_bus_param(icd);
481 if (sensor_bus_flags & SOCAM_MCLK_48MHZ) {
482 parent = clk_get(NULL, "clk48m");
483 if (!parent || IS_ERR(parent))
484 goto RK29_CAMERA_ACTIVE_ERR;
485 } else if (sensor_bus_flags & SOCAM_MCLK_27MHZ) {
486 parent = clk_get(NULL, "extclk");
487 if (!parent || IS_ERR(parent))
488 goto RK29_CAMERA_ACTIVE_ERR;
490 parent = clk_get(NULL, "xin24m");
491 if (!parent || IS_ERR(parent))
492 goto RK29_CAMERA_ACTIVE_ERR;
495 clk_set_parent(pcdev->clk, parent);
497 clk_enable(pcdev->clk);
498 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_VIP_CLKOUT);
501 //write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)|VIP_AHBMASTER); //VIP Config to AHB
502 write_grf_reg(GRF_SOC_CON0_Reg, read_grf_reg(GRF_SOC_CON0_Reg)&(~VIP_AHBMASTER)); //VIP Config to AXI
503 //write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)&(~VIP_ACLK_DIV_HCLK_2)); //aclk:hclk = 1:1
504 write_grf_reg(GRF_OS_REG0, read_grf_reg(GRF_OS_REG0)|VIP_ACLK_DIV_HCLK_2); //aclk:hclk = 2:1
506 write_vip_reg(RK29_VIP_RESET, 0x76543210); /* ddl@rock-chips.com : vip software reset */
509 write_vip_reg(RK29_VIP_AHBR_CTRL, 0x07); /* ddl@rock-chips.com : vip ahb burst 16 */
510 write_vip_reg(RK29_VIP_INT_MASK, 0x01); //capture complete interrupt enable
511 write_vip_reg(RK29_VIP_CRM, 0x00000000); //Y/CB/CR color modification
514 RK29_CAMERA_ACTIVE_ERR:
518 static void rk29_camera_deactivate(struct rk29_camera_dev *pcdev)
520 pcdev->active = NULL;
522 write_vip_reg(RK29_VIP_CTRL, 0);
523 read_vip_reg(RK29_VIP_INT_STS); //clear vip interrupte single
525 rk29_mux_api_set(GPIO1B4_VIPCLKOUT_NAME, GPIO1L_GPIO1B4);
526 clk_disable(pcdev->clk);
531 /* The following two functions absolutely depend on the fact, that
532 * there can be only one camera on RK28 quick capture interface */
533 static int rk29_camera_add_device(struct soc_camera_device *icd)
535 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
536 struct rk29_camera_dev *pcdev = ici->priv;
537 struct device *control = to_soc_camera_control(icd);
538 struct v4l2_subdev *sd;
541 mutex_lock(&camera_lock);
548 dev_info(&icd->dev, "RK29 Camera driver attached to camera %d\n",
551 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
552 pcdev->active = NULL;
554 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
555 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
556 * if app havn't dequeue all videobuf before close camera device;
558 INIT_LIST_HEAD(&pcdev->capture);
560 ret = rk29_camera_activate(pcdev,icd);
564 /* ddl@rock-chips.com : v4l2_subdev is not created when ici->ops->add called in soc_camera_probe */
566 sd = dev_get_drvdata(control);
567 ret = v4l2_subdev_call(sd,core, init, 0);
575 mutex_unlock(&camera_lock);
579 static void rk29_camera_remove_device(struct soc_camera_device *icd)
581 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
582 struct rk29_camera_dev *pcdev = ici->priv;
583 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
585 BUG_ON(icd != pcdev->icd);
587 dev_info(&icd->dev, "RK29 Camera driver detached from camera %d\n",
590 v4l2_subdev_call(sd, core, ioctl, RK29_CAM_SUBDEV_DEACTIVATE,NULL);
591 rk29_camera_deactivate(pcdev);
593 /* ddl@rock-chips.com: Call videobuf_mmap_free here for free the struct video_buffer which malloc in videobuf_alloc */
594 if (pcdev->vb_vidq_ptr) {
595 videobuf_mmap_free(pcdev->vb_vidq_ptr);
596 pcdev->vb_vidq_ptr = NULL;
599 pcdev->active = NULL;
601 pcdev->reginfo_suspend.Inval = Reg_Invalidate;
602 /* ddl@rock-chips.com: capture list must be reset, because this list may be not empty,
603 * if app havn't dequeue all videobuf before close camera device;
605 INIT_LIST_HEAD(&pcdev->capture);
610 static int rk29_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
612 unsigned long bus_flags, camera_flags, common_flags;
613 unsigned int vip_ctrl_val = 0;
616 RK29CAMERA_DG("\n%s..%d..\n",__FUNCTION__,__LINE__);
618 bus_flags = RK29_CAM_BUS_PARAM;
619 if (icd->ops->query_bus_param)
620 camera_flags = icd->ops->query_bus_param(icd);
624 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
627 goto RK29_CAMERA_SET_BUS_PARAM_END;
630 ret = icd->ops->set_bus_param(icd, common_flags);
632 goto RK29_CAMERA_SET_BUS_PARAM_END;
634 if (common_flags & SOCAM_DATAWIDTH_8) {
636 } else if (common_flags & SOCAM_DATAWIDTH_10) {
640 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
641 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) {
642 vip_ctrl_val |= NEGATIVE_EDGE;
644 vip_ctrl_val &= ~NEGATIVE_EDGE;
646 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) {
647 vip_ctrl_val |= HSY_LOW_ACTIVE;
649 vip_ctrl_val &= ~HSY_LOW_ACTIVE;
651 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH) {
652 vip_ctrl_val |= VSY_HIGH_ACTIVE;
654 vip_ctrl_val &= ~VSY_HIGH_ACTIVE;
657 /* ddl@rock-chips.com : Don't enable capture here, enable in stream_on */
658 //vip_ctrl_val |= ENABLE_CAPTURE;
660 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
661 RK29CAMERA_DG("\n%s..ctrl:0x%x CtrReg=%x AXI_AHB:0x%x aclk_hclk:0x%x \n",__FUNCTION__,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL),
662 read_grf_reg(GRF_SOC_CON0_Reg)&VIP_AHBMASTER, read_grf_reg(GRF_OS_REG0)&VIP_ACLK_DIV_HCLK_2);
664 RK29_CAMERA_SET_BUS_PARAM_END:
666 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
670 static int rk29_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
672 unsigned long bus_flags, camera_flags;
675 bus_flags = RK29_CAM_BUS_PARAM;
676 if (icd->ops->query_bus_param) {
677 camera_flags = icd->ops->query_bus_param(icd);
681 ret = soc_camera_bus_param_compatible(camera_flags, bus_flags) ;
684 dev_warn(icd->dev.parent,
685 "Flags incompatible: camera %lx, host %lx\n",
686 camera_flags, bus_flags);
689 static const struct soc_camera_data_format rk29_camera_formats[] = {
691 .name = "Planar YUV420 12 bit",
693 .fourcc = V4L2_PIX_FMT_YUV420,
694 .colorspace = V4L2_COLORSPACE_JPEG,
696 .name = "Planar YUV422 16 bit",
698 .fourcc = V4L2_PIX_FMT_YUV422P,
699 .colorspace = V4L2_COLORSPACE_JPEG,
701 .name = "Raw Bayer RGB 10 bit",
703 .fourcc = V4L2_PIX_FMT_SGRBG10,
704 .colorspace = V4L2_COLORSPACE_SRGB,
708 static void rk29_camera_setup_format(struct soc_camera_device *icd, __u32 host_pixfmt, __u32 cam_pixfmt, struct v4l2_rect *rect)
710 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
711 struct rk29_camera_dev *pcdev = ici->priv;
712 unsigned int vip_fs = 0,vip_crop = 0;
713 unsigned int vip_ctrl_val = VIP_SENSOR|ONEFRAME|DISABLE_CAPTURE;
717 case V4L2_PIX_FMT_YUV422P:
718 vip_ctrl_val |= VIPREGYUV422;
719 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
721 case V4L2_PIX_FMT_YUV420:
722 vip_ctrl_val |= VIPREGYUV420;
723 if (pcdev->frame_inval != RK29_CAM_FRAME_INVAL_INIT)
724 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_INIT;
726 case V4L2_PIX_FMT_SGRBG10:
727 vip_ctrl_val |= (VIP_RAW | VIP_SENSOR | VIP_DATA_LITTLEEND);
728 pcdev->frame_inval = RK29_CAM_FRAME_INVAL_DC;
730 default: /* ddl@rock-chips.com : vip output format is hold when pixfmt is invalidate */
731 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & VIPREGYUV422);
737 case V4L2_PIX_FMT_UYVY:
738 vip_ctrl_val |= SENSOR_UYVY;
740 case V4L2_PIX_FMT_YUYV:
741 vip_ctrl_val |= SENSOR_YUYV;
744 vip_ctrl_val |= (read_vip_reg(RK29_VIP_CTRL) & SENSOR_YUYV);
748 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 */
750 read_vip_reg(RK29_VIP_INT_STS); /* clear vip interrupte single */
752 if (vip_ctrl_val & ONEFRAME) {
753 vip_crop = ((rect->left<<16) + rect->top);
754 vip_fs = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
755 } else if (vip_ctrl_val & PING_PONG) {
756 if (rect->left ||rect->top ) {
757 RK29CAMERA_DG("\n %s..PingPang not support Crop \n",__FUNCTION__);
761 vip_fs = (((rect->width + rect->left)<<16) + (rect->height+rect->top));
764 write_vip_reg(RK29_VIP_CROP, vip_crop);
765 write_vip_reg(RK29_VIP_FS, vip_fs);
767 write_vip_reg(RK29_VIP_FB_SR, 0x00000003);
769 RK29CAMERA_DG("\n%s.. crop:0x%x fs:0x%x ctrl:0x%x CtrlReg:0x%x\n",__FUNCTION__,vip_crop,vip_fs,vip_ctrl_val,read_vip_reg(RK29_VIP_CTRL));
773 static int rk29_camera_get_formats(struct soc_camera_device *icd, int idx,
774 struct soc_camera_format_xlate *xlate)
776 struct device *dev = icd->dev.parent;
777 int formats = 0, buswidth, ret;
781 ret = rk29_camera_try_bus_param(icd, buswidth);
785 switch (icd->formats[idx].fourcc) {
786 case V4L2_PIX_FMT_UYVY:
787 case V4L2_PIX_FMT_YUYV:
790 xlate->host_fmt = &rk29_camera_formats[0];
791 xlate->cam_fmt = icd->formats + idx;
792 xlate->buswidth = buswidth;
794 dev_dbg(dev, "Providing format %s using %s\n",
795 rk29_camera_formats[0].name,
796 icd->formats[idx].name);
801 xlate->host_fmt = &rk29_camera_formats[1];
802 xlate->cam_fmt = icd->formats + idx;
803 xlate->buswidth = buswidth;
805 dev_dbg(dev, "Providing format %s using %s\n",
806 rk29_camera_formats[1].name,
807 icd->formats[idx].name);
810 case V4L2_PIX_FMT_SGRBG10:
813 xlate->host_fmt = &rk29_camera_formats[2];
814 xlate->cam_fmt = icd->formats + idx;
815 xlate->buswidth = 10;
817 dev_dbg(dev, "Providing format %s using %s\n",
818 rk29_camera_formats[2].name,
819 icd->formats[idx].name);
829 static void rk29_camera_put_formats(struct soc_camera_device *icd)
834 static int rk29_camera_set_crop(struct soc_camera_device *icd,
837 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
838 struct v4l2_format f;
839 struct v4l2_pix_format *pix = &f.fmt.pix;
842 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
848 if ((pix->width < (a->c.left + a->c.width)) || (pix->height < (a->c.top + a->c.height))) {
850 pix->width = a->c.left + a->c.width;
851 pix->height = a->c.top + a->c.height;
853 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
854 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
855 icd->current_fmt->fourcc == V4L2_PIX_FMT_YUV422P ?4 : 0);
857 ret = v4l2_subdev_call(sd, video, s_fmt, &f);
862 rk29_camera_setup_format(icd, icd->current_fmt->fourcc, pix->pixelformat, &a->c);
864 icd->user_width = pix->width;
865 icd->user_height = pix->height;
870 static int rk29_camera_set_fmt(struct soc_camera_device *icd,
871 struct v4l2_format *f)
873 struct device *dev = icd->dev.parent;
874 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
875 const struct soc_camera_data_format *cam_fmt = NULL;
876 const struct soc_camera_format_xlate *xlate = NULL;
877 struct v4l2_pix_format *pix = &f->fmt.pix;
878 struct v4l2_format cam_f = *f;
879 struct v4l2_rect rect;
882 RK29CAMERA_DG("\n%s..%d.. \n",__FUNCTION__,__LINE__);
884 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
886 dev_err(dev, "Format %x not found\n", pix->pixelformat);
888 goto RK29_CAMERA_SET_FMT_END;
891 cam_fmt = xlate->cam_fmt;
893 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
894 ret = v4l2_subdev_call(sd, video, s_fmt, &cam_f);
895 cam_f.fmt.pix.pixelformat = pix->pixelformat;
896 *pix = cam_f.fmt.pix;
903 rect.width = pix->width;
904 rect.height = pix->height;
906 RK29CAMERA_DG("\n%s..%s..%s \n",__FUNCTION__,xlate->host_fmt->name, cam_fmt->name);
907 rk29_camera_setup_format(icd, pix->pixelformat, cam_fmt->fourcc, &rect);
909 icd->buswidth = xlate->buswidth;
910 icd->current_fmt = xlate->host_fmt;
913 RK29_CAMERA_SET_FMT_END:
915 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
919 static int rk29_camera_try_fmt(struct soc_camera_device *icd,
920 struct v4l2_format *f)
922 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
923 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
924 const struct soc_camera_format_xlate *xlate;
925 struct v4l2_pix_format *pix = &f->fmt.pix;
926 __u32 pixfmt = pix->pixelformat;
927 enum v4l2_field field;
930 RK29CAMERA_DG("\n%s..%d.. \n",__FUNCTION__,__LINE__);
932 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
934 dev_err(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
936 goto RK29_CAMERA_TRY_FMT_END;
938 /* limit to rk29 hardware capabilities */
939 v4l_bound_align_image(&pix->width, RK29_CAM_W_MIN, RK29_CAM_W_MAX, 1,
940 &pix->height, RK29_CAM_H_MIN, RK29_CAM_H_MAX, 0,
941 pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
943 pix->bytesperline = pix->width * DIV_ROUND_UP(xlate->host_fmt->depth, 8);
944 pix->sizeimage = pix->height * pix->bytesperline;
946 /* camera has to see its format, but the user the original one */
947 pix->pixelformat = xlate->cam_fmt->fourcc;
948 /* limit to sensor capabilities */
949 ret = v4l2_subdev_call(sd, video, try_fmt, f);
950 pix->pixelformat = pixfmt;
954 if (field == V4L2_FIELD_ANY) {
955 pix->field = V4L2_FIELD_NONE;
956 } else if (field != V4L2_FIELD_NONE) {
957 dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
959 goto RK29_CAMERA_TRY_FMT_END;
962 RK29_CAMERA_TRY_FMT_END:
964 RK29CAMERA_TR("\n%s..%d.. ret = %d \n",__FUNCTION__,__LINE__, ret);
968 static int rk29_camera_reqbufs(struct soc_camera_file *icf,
969 struct v4l2_requestbuffers *p)
973 /* This is for locking debugging only. I removed spinlocks and now I
974 * check whether .prepare is ever called on a linked buffer, or whether
975 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
976 * it hadn't triggered */
977 for (i = 0; i < p->count; i++) {
978 struct rk29_buffer *buf = container_of(icf->vb_vidq.bufs[i],
979 struct rk29_buffer, vb);
981 INIT_LIST_HEAD(&buf->vb.queue);
987 static unsigned int rk29_camera_poll(struct file *file, poll_table *pt)
989 struct soc_camera_file *icf = file->private_data;
990 struct rk29_buffer *buf;
992 buf = list_entry(icf->vb_vidq.stream.next, struct rk29_buffer,
995 poll_wait(file, &buf->vb.done, pt);
997 if (buf->vb.state == VIDEOBUF_DONE ||
998 buf->vb.state == VIDEOBUF_ERROR)
999 return POLLIN|POLLRDNORM;
1004 static int rk29_camera_querycap(struct soc_camera_host *ici,
1005 struct v4l2_capability *cap)
1007 /* cap->name is set by the firendly caller:-> */
1008 strlcpy(cap->card, rk29_cam_driver_description, sizeof(cap->card));
1009 cap->version = RK29_CAM_VERSION_CODE;
1010 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1015 static int rk29_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1017 struct soc_camera_host *ici =
1018 to_soc_camera_host(icd->dev.parent);
1019 struct rk29_camera_dev *pcdev = ici->priv;
1022 if ((pcdev->icd == icd) && (icd->ops->suspend)) {
1023 rk29_camera_s_stream(icd, 0);
1024 ret = icd->ops->suspend(icd, state);
1026 pcdev->reginfo_suspend.VipCtrl = read_vip_reg(RK29_VIP_CTRL);
1027 pcdev->reginfo_suspend.VipCrop = read_vip_reg(RK29_VIP_CROP);
1028 pcdev->reginfo_suspend.VipFs = read_vip_reg(RK29_VIP_FS);
1029 pcdev->reginfo_suspend.VipIntMsk = read_vip_reg(RK29_VIP_INT_MASK);
1030 pcdev->reginfo_suspend.VipCrm = read_vip_reg(RK29_VIP_CRM);
1032 tmp = pcdev->reginfo_suspend.VipFs>>16; /* ddl@rock-chips.com */
1033 tmp += pcdev->reginfo_suspend.VipCrop>>16;
1034 pcdev->reginfo_suspend.VipFs = (pcdev->reginfo_suspend.VipFs & 0xffff) | (tmp<<16);
1036 pcdev->reginfo_suspend.Inval = Reg_Validate;
1037 rk29_camera_deactivate(pcdev);
1039 RK29CAMERA_DG("%s Enter Success...\n", __FUNCTION__);
1041 RK29CAMERA_DG("%s icd has been deattach, don't need enter suspend\n", __FUNCTION__);
1047 static int rk29_camera_resume(struct soc_camera_device *icd)
1049 struct soc_camera_host *ici =
1050 to_soc_camera_host(icd->dev.parent);
1051 struct rk29_camera_dev *pcdev = ici->priv;
1054 if ((pcdev->icd == icd) && (icd->ops->resume)) {
1055 if (pcdev->reginfo_suspend.Inval == Reg_Validate) {
1056 rk29_camera_activate(pcdev, icd);
1057 write_vip_reg(RK29_VIP_INT_MASK, pcdev->reginfo_suspend.VipIntMsk);
1058 write_vip_reg(RK29_VIP_CRM, pcdev->reginfo_suspend.VipCrm);
1059 write_vip_reg(RK29_VIP_CTRL, pcdev->reginfo_suspend.VipCtrl&~ENABLE_CAPTURE);
1060 write_vip_reg(RK29_VIP_CROP, pcdev->reginfo_suspend.VipCrop);
1061 write_vip_reg(RK29_VIP_FS, pcdev->reginfo_suspend.VipFs);
1063 rk29_videobuf_capture(pcdev->active);
1064 rk29_camera_s_stream(icd, 1);
1066 ret = icd->ops->resume(icd);
1068 RK29CAMERA_DG("%s Enter success\n",__FUNCTION__);
1070 RK29CAMERA_DG("%s icd has been deattach, don't need enter resume\n", __FUNCTION__);
1076 static int rk29_camera_s_stream(struct soc_camera_device *icd, int enable)
1078 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1079 struct rk29_camera_dev *pcdev = ici->priv;
1082 WARN_ON(pcdev->icd != icd);
1084 vip_ctrl_val = read_vip_reg(RK29_VIP_CTRL);
1086 vip_ctrl_val |= ENABLE_CAPTURE;
1088 vip_ctrl_val &= ~ENABLE_CAPTURE;
1090 write_vip_reg(RK29_VIP_CTRL, vip_ctrl_val);
1092 RK29CAMERA_DG("%s.. enable : %d\n", __FUNCTION__, enable);
1096 static struct soc_camera_host_ops rk29_soc_camera_host_ops =
1098 .owner = THIS_MODULE,
1099 .add = rk29_camera_add_device,
1100 .remove = rk29_camera_remove_device,
1101 .suspend = rk29_camera_suspend,
1102 .resume = rk29_camera_resume,
1103 .set_crop = rk29_camera_set_crop,
1104 .get_formats = rk29_camera_get_formats,
1105 .put_formats = rk29_camera_put_formats,
1106 .set_fmt = rk29_camera_set_fmt,
1107 .try_fmt = rk29_camera_try_fmt,
1108 .init_videobuf = rk29_camera_init_videobuf,
1109 .reqbufs = rk29_camera_reqbufs,
1110 .poll = rk29_camera_poll,
1111 .querycap = rk29_camera_querycap,
1112 .set_bus_param = rk29_camera_set_bus_param,
1113 .s_stream = rk29_camera_s_stream /* ddl@rock-chips.com : Add stream control for host */
1115 static int rk29_camera_probe(struct platform_device *pdev)
1117 struct rk29_camera_dev *pcdev;
1118 struct resource *res;
1122 RK29CAMERA_DG("\n%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1124 irq = platform_get_irq(pdev, 0);
1125 if (!res || irq < 0) {
1130 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1132 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1136 rk29_camdev_info_ptr = pcdev;
1138 /*config output clk*/
1139 pcdev->clk = clk_get(&pdev->dev, "vip");
1140 if (!pcdev->clk || IS_ERR(pcdev->clk)) {
1141 RK29CAMERA_TR(KERN_ERR "failed to get vip_clk source\n");
1146 dev_set_drvdata(&pdev->dev, pcdev);
1149 pcdev->pdata = pdev->dev.platform_data; /* ddl@rock-chips.com : Request IO in init function */
1150 if (pcdev->pdata && pcdev->pdata->io_init) {
1151 pcdev->pdata->io_init();
1154 INIT_LIST_HEAD(&pcdev->capture);
1155 spin_lock_init(&pcdev->lock);
1158 * Request the regions.
1160 if (!request_mem_region(res->start, res->end - res->start + 1,
1161 RK29_CAM_DRV_NAME)) {
1166 pcdev->base = ioremap(res->start, res->end - res->start + 1);
1167 if (pcdev->base == NULL) {
1168 dev_err(pcdev->dev, "ioremap() of registers failed\n");
1174 pcdev->dev = &pdev->dev;
1176 /* config buffer address */
1178 err = request_irq(pcdev->irq, rk29_camera_irq, 0, RK29_CAM_DRV_NAME,
1181 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1185 pcdev->soc_host.drv_name = RK29_CAM_DRV_NAME;
1186 pcdev->soc_host.ops = &rk29_soc_camera_host_ops;
1187 pcdev->soc_host.priv = pcdev;
1188 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1189 pcdev->soc_host.nr = pdev->id;
1191 err = soc_camera_host_register(&pcdev->soc_host);
1195 RK29CAMERA_DG("\n%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1199 free_irq(pcdev->irq, pcdev);
1201 iounmap(pcdev->base);
1203 release_mem_region(res->start, res->end - res->start + 1);
1205 clk_put(pcdev->clk);
1209 rk29_camdev_info_ptr = NULL;
1214 static int __devexit rk29_camera_remove(struct platform_device *pdev)
1216 struct rk29_camera_dev *pcdev = platform_get_drvdata(pdev);
1217 struct resource *res;
1219 free_irq(pcdev->irq, pcdev);
1221 soc_camera_host_unregister(&pcdev->soc_host);
1224 release_mem_region(res->start, res->end - res->start + 1);
1226 if (pcdev->pdata && pcdev->pdata->io_deinit) { /* ddl@rock-chips.com : Free IO in deinit function */
1227 pcdev->pdata->io_deinit();
1231 rk29_camdev_info_ptr = NULL;
1232 dev_info(&pdev->dev, "RK28 Camera driver unloaded\n");
1237 static struct platform_driver rk29_camera_driver =
1240 .name = RK29_CAM_DRV_NAME,
1242 .probe = rk29_camera_probe,
1243 .remove = __devexit_p(rk29_camera_remove),
1247 static int __devinit rk29_camera_init(void)
1249 RK29CAMERA_DG("\n%s..%s..%d \n",__FUNCTION__,__FILE__,__LINE__);
1250 return platform_driver_register(&rk29_camera_driver);
1253 static void __exit rk29_camera_exit(void)
1255 platform_driver_unregister(&rk29_camera_driver);
1258 device_initcall_sync(rk29_camera_init);
1259 module_exit(rk29_camera_exit);
1261 MODULE_DESCRIPTION("RK29 Soc Camera Host driver");
1262 MODULE_AUTHOR("ddl <ddl@rock-chips>");
1263 MODULE_LICENSE("GPL");