2 * SoC-camera host driver for Renesas R-Car VIN unit
4 * Copyright (C) 2011-2013 Renesas Solutions Corp.
5 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Based on V4L2 Driver for SuperH Mobile CEU interface "sh_mobile_ceu_camera.c"
9 * Copyright (C) 2008 Magnus Damm
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_data/camera-rcar.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <media/soc_camera.h>
29 #include <media/soc_mediabus.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-mediabus.h>
34 #include <media/v4l2-subdev.h>
35 #include <media/videobuf2-dma-contig.h>
37 #include "soc_scale_crop.h"
39 #define DRV_NAME "rcar_vin"
41 /* Register offsets for R-Car VIN */
42 #define VNMC_REG 0x00 /* Video n Main Control Register */
43 #define VNMS_REG 0x04 /* Video n Module Status Register */
44 #define VNFC_REG 0x08 /* Video n Frame Capture Register */
45 #define VNSLPRC_REG 0x0C /* Video n Start Line Pre-Clip Register */
46 #define VNELPRC_REG 0x10 /* Video n End Line Pre-Clip Register */
47 #define VNSPPRC_REG 0x14 /* Video n Start Pixel Pre-Clip Register */
48 #define VNEPPRC_REG 0x18 /* Video n End Pixel Pre-Clip Register */
49 #define VNSLPOC_REG 0x1C /* Video n Start Line Post-Clip Register */
50 #define VNELPOC_REG 0x20 /* Video n End Line Post-Clip Register */
51 #define VNSPPOC_REG 0x24 /* Video n Start Pixel Post-Clip Register */
52 #define VNEPPOC_REG 0x28 /* Video n End Pixel Post-Clip Register */
53 #define VNIS_REG 0x2C /* Video n Image Stride Register */
54 #define VNMB_REG(m) (0x30 + ((m) << 2)) /* Video n Memory Base m Register */
55 #define VNIE_REG 0x40 /* Video n Interrupt Enable Register */
56 #define VNINTS_REG 0x44 /* Video n Interrupt Status Register */
57 #define VNSI_REG 0x48 /* Video n Scanline Interrupt Register */
58 #define VNMTC_REG 0x4C /* Video n Memory Transfer Control Register */
59 #define VNYS_REG 0x50 /* Video n Y Scale Register */
60 #define VNXS_REG 0x54 /* Video n X Scale Register */
61 #define VNDMR_REG 0x58 /* Video n Data Mode Register */
62 #define VNDMR2_REG 0x5C /* Video n Data Mode Register 2 */
63 #define VNUVAOF_REG 0x60 /* Video n UV Address Offset Register */
65 /* Register bit fields for R-Car VIN */
66 /* Video n Main Control Register bits */
67 #define VNMC_FOC (1 << 21)
68 #define VNMC_YCAL (1 << 19)
69 #define VNMC_INF_YUV8_BT656 (0 << 16)
70 #define VNMC_INF_YUV8_BT601 (1 << 16)
71 #define VNMC_INF_YUV10_BT656 (2 << 16)
72 #define VNMC_INF_YUV10_BT601 (3 << 16)
73 #define VNMC_INF_YUV16 (5 << 16)
74 #define VNMC_VUP (1 << 10)
75 #define VNMC_IM_ODD (0 << 3)
76 #define VNMC_IM_ODD_EVEN (1 << 3)
77 #define VNMC_IM_EVEN (2 << 3)
78 #define VNMC_IM_FULL (3 << 3)
79 #define VNMC_BPS (1 << 1)
80 #define VNMC_ME (1 << 0)
82 /* Video n Module Status Register bits */
83 #define VNMS_FBS_MASK (3 << 3)
84 #define VNMS_FBS_SHIFT 3
85 #define VNMS_AV (1 << 1)
86 #define VNMS_CA (1 << 0)
88 /* Video n Frame Capture Register bits */
89 #define VNFC_C_FRAME (1 << 1)
90 #define VNFC_S_FRAME (1 << 0)
92 /* Video n Interrupt Enable Register bits */
93 #define VNIE_FIE (1 << 4)
94 #define VNIE_EFE (1 << 1)
96 /* Video n Data Mode Register bits */
97 #define VNDMR_EXRGB (1 << 8)
98 #define VNDMR_BPSM (1 << 4)
99 #define VNDMR_DTMD_YCSEP (1 << 1)
100 #define VNDMR_DTMD_ARGB1555 (1 << 0)
102 /* Video n Data Mode Register 2 bits */
103 #define VNDMR2_VPS (1 << 30)
104 #define VNDMR2_HPS (1 << 29)
105 #define VNDMR2_FTEV (1 << 17)
107 #define VIN_MAX_WIDTH 2048
108 #define VIN_MAX_HEIGHT 2048
117 enum rcar_vin_state {
123 struct rcar_vin_priv {
127 /* State of the VIN module in capturing mode */
128 enum rcar_vin_state state;
129 struct rcar_vin_platform_data *pdata;
130 struct soc_camera_host ici;
131 struct list_head capture;
132 #define MAX_BUFFER_NUM 3
133 struct vb2_buffer *queue_buf[MAX_BUFFER_NUM];
134 struct vb2_alloc_ctx *alloc_ctx;
135 enum v4l2_field field;
136 unsigned int vb_count;
137 unsigned int nr_hw_slots;
138 bool request_to_stop;
139 struct completion capture_stop;
143 #define is_continuous_transfer(priv) (priv->vb_count > MAX_BUFFER_NUM)
145 struct rcar_vin_buffer {
146 struct vb2_buffer vb;
147 struct list_head list;
150 #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
151 struct rcar_vin_buffer, \
154 struct rcar_vin_cam {
155 /* VIN offsets within the camera output, before the VIN scaler */
156 unsigned int vin_left;
157 unsigned int vin_top;
158 /* Client output, as seen by the VIN */
162 * User window from S_CROP / G_CROP, produced by client cropping and
163 * scaling, VIN scaling and VIN cropping, mapped back onto the client
166 struct v4l2_rect subrect;
167 /* Camera cropping rectangle */
168 struct v4l2_rect rect;
169 const struct soc_mbus_pixelfmt *extra_fmt;
173 * .queue_setup() is called to check whether the driver can accept the requested
174 * number of buffers and to fill in plane sizes for the current frame format if
177 static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
178 const struct v4l2_format *fmt,
180 unsigned int *num_planes,
181 unsigned int sizes[], void *alloc_ctxs[])
183 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
184 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
185 struct rcar_vin_priv *priv = ici->priv;
188 const struct soc_camera_format_xlate *xlate;
189 unsigned int bytes_per_line;
192 xlate = soc_camera_xlate_by_fourcc(icd,
193 fmt->fmt.pix.pixelformat);
196 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
201 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
203 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
204 fmt->fmt.pix.height);
208 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
210 /* Called from VIDIOC_REQBUFS or in compatibility mode */
211 sizes[0] = icd->sizeimage;
214 alloc_ctxs[0] = priv->alloc_ctx;
216 if (!vq->num_buffers)
221 priv->vb_count = *count;
225 /* Number of hardware slots */
226 if (is_continuous_transfer(priv))
227 priv->nr_hw_slots = MAX_BUFFER_NUM;
229 priv->nr_hw_slots = 1;
231 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
236 static int rcar_vin_setup(struct rcar_vin_priv *priv)
238 struct soc_camera_device *icd = priv->ici.icd;
239 struct rcar_vin_cam *cam = icd->host_priv;
240 u32 vnmc, dmr, interrupts;
241 bool progressive = false, output_is_yuv = false;
243 switch (priv->field) {
247 case V4L2_FIELD_BOTTOM:
250 case V4L2_FIELD_INTERLACED:
251 case V4L2_FIELD_INTERLACED_TB:
254 case V4L2_FIELD_INTERLACED_BT:
255 vnmc = VNMC_IM_FULL | VNMC_FOC;
257 case V4L2_FIELD_NONE:
258 if (is_continuous_transfer(priv)) {
259 vnmc = VNMC_IM_ODD_EVEN;
270 /* input interface */
271 switch (icd->current_fmt->code) {
272 case V4L2_MBUS_FMT_YUYV8_1X16:
273 /* BT.601/BT.1358 16bit YCbCr422 */
274 vnmc |= VNMC_INF_YUV16;
276 case V4L2_MBUS_FMT_YUYV8_2X8:
277 /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
278 vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ?
279 VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
281 case V4L2_MBUS_FMT_YUYV10_2X10:
282 /* BT.656 10bit YCbCr422 or BT.601 10bit YCbCr422 */
283 vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ?
284 VNMC_INF_YUV10_BT656 : VNMC_INF_YUV10_BT601;
291 switch (icd->current_fmt->host_fmt->fourcc) {
292 case V4L2_PIX_FMT_NV16:
293 iowrite32(ALIGN(cam->width * cam->height, 0x80),
294 priv->base + VNUVAOF_REG);
295 dmr = VNDMR_DTMD_YCSEP;
296 output_is_yuv = true;
298 case V4L2_PIX_FMT_YUYV:
300 output_is_yuv = true;
302 case V4L2_PIX_FMT_UYVY:
304 output_is_yuv = true;
306 case V4L2_PIX_FMT_RGB555X:
307 dmr = VNDMR_DTMD_ARGB1555;
309 case V4L2_PIX_FMT_RGB565:
312 case V4L2_PIX_FMT_RGB32:
313 if (priv->chip == RCAR_GEN2 || priv->chip == RCAR_H1 ||
314 priv->chip == RCAR_E1) {
319 dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n",
320 icd->current_fmt->host_fmt->fourcc);
324 /* Always update on field change */
327 /* If input and output use the same colorspace, use bypass mode */
331 /* progressive or interlaced mode */
332 interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE;
335 iowrite32(interrupts, priv->base + VNINTS_REG);
336 /* enable interrupts */
337 iowrite32(interrupts, priv->base + VNIE_REG);
338 /* start capturing */
339 iowrite32(dmr, priv->base + VNDMR_REG);
340 iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG);
345 static void rcar_vin_capture(struct rcar_vin_priv *priv)
347 if (is_continuous_transfer(priv))
348 /* Continuous Frame Capture Mode */
349 iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG);
351 /* Single Frame Capture Mode */
352 iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG);
355 static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv)
357 priv->state = STOPPING;
359 /* set continuous & single transfer off */
360 iowrite32(0, priv->base + VNFC_REG);
361 /* disable capture (release DMA buffer), reset */
362 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
363 priv->base + VNMC_REG);
365 /* update the status if stopped already */
366 if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA))
367 priv->state = STOPPED;
370 static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv)
374 for (slot = 0; slot < priv->nr_hw_slots; slot++)
375 if (priv->queue_buf[slot] == NULL)
381 static int rcar_vin_hw_ready(struct rcar_vin_priv *priv)
383 /* Ensure all HW slots are filled */
384 return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0;
387 /* Moves a buffer from the queue to the HW slots */
388 static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv)
390 struct vb2_buffer *vb;
391 dma_addr_t phys_addr_top;
394 if (list_empty(&priv->capture))
397 /* Find a free HW slot */
398 slot = rcar_vin_get_free_hw_slot(priv);
402 vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb;
403 list_del_init(to_buf_list(vb));
404 priv->queue_buf[slot] = vb;
405 phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0);
406 iowrite32(phys_addr_top, priv->base + VNMB_REG(slot));
411 static void rcar_vin_videobuf_queue(struct vb2_buffer *vb)
413 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
414 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
415 struct rcar_vin_priv *priv = ici->priv;
418 size = icd->sizeimage;
420 if (vb2_plane_size(vb, 0) < size) {
421 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
422 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
426 vb2_set_plane_payload(vb, 0, size);
428 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
429 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
431 spin_lock_irq(&priv->lock);
433 list_add_tail(to_buf_list(vb), &priv->capture);
434 rcar_vin_fill_hw_slot(priv);
436 /* If we weren't running, and have enough buffers, start capturing! */
437 if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) {
438 if (rcar_vin_setup(priv)) {
440 list_del_init(to_buf_list(vb));
441 spin_unlock_irq(&priv->lock);
444 priv->request_to_stop = false;
445 init_completion(&priv->capture_stop);
446 priv->state = RUNNING;
447 rcar_vin_capture(priv);
450 spin_unlock_irq(&priv->lock);
455 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
458 static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
460 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
461 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
462 struct rcar_vin_priv *priv = ici->priv;
466 spin_lock_irq(&priv->lock);
468 /* Is the buffer in use by the VIN hardware? */
469 for (i = 0; i < MAX_BUFFER_NUM; i++) {
470 if (priv->queue_buf[i] == vb) {
477 while (priv->state != STOPPED) {
479 /* issue stop if running */
480 if (priv->state == RUNNING)
481 rcar_vin_request_capture_stop(priv);
483 /* wait until capturing has been stopped */
484 if (priv->state == STOPPING) {
485 priv->request_to_stop = true;
486 spin_unlock_irq(&priv->lock);
487 wait_for_completion(&priv->capture_stop);
488 spin_lock_irq(&priv->lock);
492 * Capturing has now stopped. The buffer we have been asked
493 * to release could be any of the current buffers in use, so
494 * release all buffers that are in use by HW
496 for (i = 0; i < MAX_BUFFER_NUM; i++) {
497 if (priv->queue_buf[i]) {
498 vb2_buffer_done(priv->queue_buf[i],
499 VB2_BUF_STATE_ERROR);
500 priv->queue_buf[i] = NULL;
504 list_del_init(to_buf_list(vb));
507 spin_unlock_irq(&priv->lock);
510 static int rcar_vin_videobuf_init(struct vb2_buffer *vb)
512 INIT_LIST_HEAD(to_buf_list(vb));
516 static int rcar_vin_stop_streaming(struct vb2_queue *vq)
518 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
519 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
520 struct rcar_vin_priv *priv = ici->priv;
521 struct list_head *buf_head, *tmp;
523 spin_lock_irq(&priv->lock);
524 list_for_each_safe(buf_head, tmp, &priv->capture)
525 list_del_init(buf_head);
526 spin_unlock_irq(&priv->lock);
531 static struct vb2_ops rcar_vin_vb2_ops = {
532 .queue_setup = rcar_vin_videobuf_setup,
533 .buf_init = rcar_vin_videobuf_init,
534 .buf_cleanup = rcar_vin_videobuf_release,
535 .buf_queue = rcar_vin_videobuf_queue,
536 .stop_streaming = rcar_vin_stop_streaming,
537 .wait_prepare = soc_camera_unlock,
538 .wait_finish = soc_camera_lock,
541 static irqreturn_t rcar_vin_irq(int irq, void *data)
543 struct rcar_vin_priv *priv = data;
545 bool can_run = false, hw_stopped;
547 unsigned int handled = 0;
549 spin_lock(&priv->lock);
551 int_status = ioread32(priv->base + VNINTS_REG);
555 iowrite32(int_status, priv->base + VNINTS_REG);
558 /* nothing to do if capture status is 'STOPPED' */
559 if (priv->state == STOPPED)
562 hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA);
564 if (!priv->request_to_stop) {
565 if (is_continuous_transfer(priv))
566 slot = (ioread32(priv->base + VNMS_REG) &
567 VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
571 priv->queue_buf[slot]->v4l2_buf.field = priv->field;
572 priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++;
573 do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp);
574 vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE);
575 priv->queue_buf[slot] = NULL;
577 if (priv->state != STOPPING)
578 can_run = rcar_vin_fill_hw_slot(priv);
580 if (hw_stopped || !can_run) {
581 priv->state = STOPPED;
582 } else if (is_continuous_transfer(priv) &&
583 list_empty(&priv->capture) &&
584 priv->state == RUNNING) {
586 * The continuous capturing requires an explicit stop
587 * operation when there is no buffer to be set into
588 * the VnMBm registers.
590 rcar_vin_request_capture_stop(priv);
592 rcar_vin_capture(priv);
595 } else if (hw_stopped) {
596 priv->state = STOPPED;
597 priv->request_to_stop = false;
598 complete(&priv->capture_stop);
602 spin_unlock(&priv->lock);
604 return IRQ_RETVAL(handled);
607 static int rcar_vin_add_device(struct soc_camera_device *icd)
609 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
610 struct rcar_vin_priv *priv = ici->priv;
613 for (i = 0; i < MAX_BUFFER_NUM; i++)
614 priv->queue_buf[i] = NULL;
616 pm_runtime_get_sync(ici->v4l2_dev.dev);
618 dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n",
624 static void rcar_vin_remove_device(struct soc_camera_device *icd)
626 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
627 struct rcar_vin_priv *priv = ici->priv;
628 struct vb2_buffer *vb;
631 /* disable capture, disable interrupts */
632 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
633 priv->base + VNMC_REG);
634 iowrite32(0, priv->base + VNIE_REG);
636 priv->state = STOPPED;
637 priv->request_to_stop = false;
639 /* make sure active buffer is cancelled */
640 spin_lock_irq(&priv->lock);
641 for (i = 0; i < MAX_BUFFER_NUM; i++) {
642 vb = priv->queue_buf[i];
644 list_del_init(to_buf_list(vb));
645 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
648 spin_unlock_irq(&priv->lock);
650 pm_runtime_put(ici->v4l2_dev.dev);
652 dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n",
656 /* Called with .host_lock held */
657 static int rcar_vin_clock_start(struct soc_camera_host *ici)
659 /* VIN does not have "mclk" */
663 /* Called with .host_lock held */
664 static void rcar_vin_clock_stop(struct soc_camera_host *ici)
666 /* VIN does not have "mclk" */
669 /* rect is guaranteed to not exceed the scaled camera rectangle */
670 static int rcar_vin_set_rect(struct soc_camera_device *icd)
672 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
673 struct rcar_vin_cam *cam = icd->host_priv;
674 struct rcar_vin_priv *priv = ici->priv;
675 unsigned int left_offset, top_offset;
676 unsigned char dsize = 0;
677 struct v4l2_rect *cam_subrect = &cam->subrect;
679 dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n",
680 icd->user_width, icd->user_height, cam->vin_left, cam->vin_top);
682 left_offset = cam->vin_left;
683 top_offset = cam->vin_top;
685 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 &&
686 priv->chip == RCAR_E1)
689 dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n",
690 cam->width, cam->height, cam->vin_left, cam->vin_top);
691 dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n",
692 cam_subrect->width, cam_subrect->height,
693 cam_subrect->left, cam_subrect->top);
695 /* Set Start/End Pixel/Line Pre-Clip */
696 iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG);
697 iowrite32((left_offset + cam->width - 1) << dsize,
698 priv->base + VNEPPRC_REG);
699 switch (priv->field) {
700 case V4L2_FIELD_INTERLACED:
701 case V4L2_FIELD_INTERLACED_TB:
702 case V4L2_FIELD_INTERLACED_BT:
703 iowrite32(top_offset / 2, priv->base + VNSLPRC_REG);
704 iowrite32((top_offset + cam->height) / 2 - 1,
705 priv->base + VNELPRC_REG);
708 iowrite32(top_offset, priv->base + VNSLPRC_REG);
709 iowrite32(top_offset + cam->height - 1,
710 priv->base + VNELPRC_REG);
714 /* Set Start/End Pixel/Line Post-Clip */
715 iowrite32(0, priv->base + VNSPPOC_REG);
716 iowrite32(0, priv->base + VNSLPOC_REG);
717 iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG);
718 switch (priv->field) {
719 case V4L2_FIELD_INTERLACED:
720 case V4L2_FIELD_INTERLACED_TB:
721 case V4L2_FIELD_INTERLACED_BT:
722 iowrite32(cam_subrect->height / 2 - 1,
723 priv->base + VNELPOC_REG);
726 iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG);
730 iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG);
735 static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc)
737 *vnmc = ioread32(priv->base + VNMC_REG);
739 iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG);
742 static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc)
744 unsigned long timeout = jiffies + 10 * HZ;
747 * Wait until the end of the current frame. It can take a long time,
748 * but if it has been aborted by a MRST1 reset, it should exit sooner.
750 while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) &&
751 time_before(jiffies, timeout))
754 if (time_after(jiffies, timeout)) {
755 dev_err(priv->ici.v4l2_dev.dev,
756 "Timeout waiting for frame end! Interface problem?\n");
760 iowrite32(vnmc, priv->base + VNMC_REG);
763 #define VIN_MBUS_FLAGS (V4L2_MBUS_MASTER | \
764 V4L2_MBUS_PCLK_SAMPLE_RISING | \
765 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
766 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
767 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
768 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
769 V4L2_MBUS_DATA_ACTIVE_HIGH)
771 static int rcar_vin_set_bus_param(struct soc_camera_device *icd)
773 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
774 struct rcar_vin_priv *priv = ici->priv;
775 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
776 struct v4l2_mbus_config cfg;
777 unsigned long common_flags;
782 capture_stop_preserve(priv, &vnmc);
784 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
786 common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
788 dev_warn(icd->parent,
789 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
790 cfg.flags, VIN_MBUS_FLAGS);
793 } else if (ret != -ENOIOCTLCMD) {
796 common_flags = VIN_MBUS_FLAGS;
799 /* Make choises, based on platform preferences */
800 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
801 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
802 if (priv->pdata->flags & RCAR_VIN_HSYNC_ACTIVE_LOW)
803 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
805 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
808 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
809 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
810 if (priv->pdata->flags & RCAR_VIN_VSYNC_ACTIVE_LOW)
811 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
813 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
816 cfg.flags = common_flags;
817 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
818 if (ret < 0 && ret != -ENOIOCTLCMD)
821 val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0;
822 if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
824 if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
826 iowrite32(val, priv->base + VNDMR2_REG);
828 ret = rcar_vin_set_rect(icd);
832 capture_restore(priv, vnmc);
837 static int rcar_vin_try_bus_param(struct soc_camera_device *icd,
838 unsigned char buswidth)
840 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
841 struct v4l2_mbus_config cfg;
844 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
845 if (ret == -ENOIOCTLCMD)
853 /* check is there common mbus flags */
854 ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
858 dev_warn(icd->parent,
859 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
860 cfg.flags, VIN_MBUS_FLAGS);
865 static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt)
867 return fmt->packing == SOC_MBUS_PACKING_NONE ||
868 (fmt->bits_per_sample > 8 &&
869 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
872 static const struct soc_mbus_pixelfmt rcar_vin_formats[] = {
874 .fourcc = V4L2_PIX_FMT_NV16,
876 .bits_per_sample = 8,
877 .packing = SOC_MBUS_PACKING_2X8_PADHI,
878 .order = SOC_MBUS_ORDER_LE,
879 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
882 .fourcc = V4L2_PIX_FMT_UYVY,
884 .bits_per_sample = 16,
885 .packing = SOC_MBUS_PACKING_NONE,
886 .order = SOC_MBUS_ORDER_LE,
887 .layout = SOC_MBUS_LAYOUT_PACKED,
890 .fourcc = V4L2_PIX_FMT_RGB565,
892 .bits_per_sample = 16,
893 .packing = SOC_MBUS_PACKING_NONE,
894 .order = SOC_MBUS_ORDER_LE,
895 .layout = SOC_MBUS_LAYOUT_PACKED,
898 .fourcc = V4L2_PIX_FMT_RGB555X,
900 .bits_per_sample = 16,
901 .packing = SOC_MBUS_PACKING_NONE,
902 .order = SOC_MBUS_ORDER_LE,
903 .layout = SOC_MBUS_LAYOUT_PACKED,
906 .fourcc = V4L2_PIX_FMT_RGB32,
908 .bits_per_sample = 32,
909 .packing = SOC_MBUS_PACKING_NONE,
910 .order = SOC_MBUS_ORDER_LE,
911 .layout = SOC_MBUS_LAYOUT_PACKED,
915 static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx,
916 struct soc_camera_format_xlate *xlate)
918 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
919 struct device *dev = icd->parent;
922 struct rcar_vin_cam *cam;
923 enum v4l2_mbus_pixelcode code;
924 const struct soc_mbus_pixelfmt *fmt;
926 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
930 fmt = soc_mbus_get_fmtdesc(code);
932 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
936 ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample);
940 if (!icd->host_priv) {
941 struct v4l2_mbus_framefmt mf;
942 struct v4l2_rect rect;
943 struct device *dev = icd->parent;
946 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
950 /* Cache current client geometry */
951 ret = soc_camera_client_g_rect(sd, &rect);
952 if (ret == -ENOIOCTLCMD) {
953 /* Sensor driver doesn't support cropping */
956 rect.width = mf.width;
957 rect.height = mf.height;
958 } else if (ret < 0) {
963 * If sensor proposes too large format then try smaller ones:
964 * 1280x960, 640x480, 320x240
966 for (shift = 0; shift < 3; shift++) {
967 if (mf.width <= VIN_MAX_WIDTH &&
968 mf.height <= VIN_MAX_HEIGHT)
971 mf.width = 1280 >> shift;
972 mf.height = 960 >> shift;
973 ret = v4l2_device_call_until_err(sd->v4l2_dev,
974 soc_camera_grp_id(icd),
983 "Failed to configure the client below %ux%x\n",
984 mf.width, mf.height);
988 dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height);
990 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
994 * We are called with current camera crop,
995 * initialise subrect with it
999 cam->width = mf.width;
1000 cam->height = mf.height;
1002 icd->host_priv = cam;
1004 cam = icd->host_priv;
1007 /* Beginning of a pass */
1009 cam->extra_fmt = NULL;
1012 case V4L2_MBUS_FMT_YUYV8_1X16:
1013 case V4L2_MBUS_FMT_YUYV8_2X8:
1014 case V4L2_MBUS_FMT_YUYV10_2X10:
1018 /* Add all our formats that can be generated by VIN */
1019 cam->extra_fmt = rcar_vin_formats;
1021 n = ARRAY_SIZE(rcar_vin_formats);
1023 for (k = 0; xlate && k < n; k++, xlate++) {
1024 xlate->host_fmt = &rcar_vin_formats[k];
1026 dev_dbg(dev, "Providing format %s using code %d\n",
1027 rcar_vin_formats[k].name, code);
1031 if (!rcar_vin_packing_supported(fmt))
1034 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1039 /* Generic pass-through */
1042 xlate->host_fmt = fmt;
1050 static void rcar_vin_put_formats(struct soc_camera_device *icd)
1052 kfree(icd->host_priv);
1053 icd->host_priv = NULL;
1056 static int rcar_vin_set_crop(struct soc_camera_device *icd,
1057 const struct v4l2_crop *a)
1059 struct v4l2_crop a_writable = *a;
1060 const struct v4l2_rect *rect = &a_writable.c;
1061 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1062 struct rcar_vin_priv *priv = ici->priv;
1063 struct v4l2_crop cam_crop;
1064 struct rcar_vin_cam *cam = icd->host_priv;
1065 struct v4l2_rect *cam_rect = &cam_crop.c;
1066 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1067 struct device *dev = icd->parent;
1068 struct v4l2_mbus_framefmt mf;
1072 dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1073 rect->left, rect->top);
1075 /* During camera cropping its output window can change too, stop VIN */
1076 capture_stop_preserve(priv, &vnmc);
1077 dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc);
1079 /* Apply iterative camera S_CROP for new input window. */
1080 ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
1081 &cam->rect, &cam->subrect);
1085 dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n",
1086 cam_rect->width, cam_rect->height,
1087 cam_rect->left, cam_rect->top);
1089 /* On success cam_crop contains current camera crop */
1091 /* Retrieve camera output window */
1092 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1096 if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT)
1099 /* Cache camera output window */
1100 cam->width = mf.width;
1101 cam->height = mf.height;
1103 icd->user_width = cam->width;
1104 icd->user_height = cam->height;
1106 cam->vin_left = rect->left & ~1;
1107 cam->vin_top = rect->top & ~1;
1109 /* Use VIN cropping to crop to the new window. */
1110 ret = rcar_vin_set_rect(icd);
1114 cam->subrect = *rect;
1116 dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n",
1117 icd->user_width, icd->user_height,
1118 cam->vin_left, cam->vin_top);
1120 /* Restore capture */
1121 for (i = 0; i < MAX_BUFFER_NUM; i++) {
1122 if (priv->queue_buf[i] && priv->state == STOPPED) {
1127 capture_restore(priv, vnmc);
1129 /* Even if only camera cropping succeeded */
1133 static int rcar_vin_get_crop(struct soc_camera_device *icd,
1134 struct v4l2_crop *a)
1136 struct rcar_vin_cam *cam = icd->host_priv;
1138 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1139 a->c = cam->subrect;
1144 /* Similar to set_crop multistage iterative algorithm */
1145 static int rcar_vin_set_fmt(struct soc_camera_device *icd,
1146 struct v4l2_format *f)
1148 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1149 struct rcar_vin_priv *priv = ici->priv;
1150 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1151 struct rcar_vin_cam *cam = icd->host_priv;
1152 struct v4l2_pix_format *pix = &f->fmt.pix;
1153 struct v4l2_mbus_framefmt mf;
1154 struct device *dev = icd->parent;
1155 __u32 pixfmt = pix->pixelformat;
1156 const struct soc_camera_format_xlate *xlate;
1157 unsigned int vin_sub_width = 0, vin_sub_height = 0;
1160 enum v4l2_field field;
1163 dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n",
1164 pixfmt, pix->width, pix->height);
1166 switch (pix->field) {
1168 pix->field = V4L2_FIELD_NONE;
1170 case V4L2_FIELD_NONE:
1171 case V4L2_FIELD_TOP:
1172 case V4L2_FIELD_BOTTOM:
1173 case V4L2_FIELD_INTERLACED_TB:
1174 case V4L2_FIELD_INTERLACED_BT:
1177 case V4L2_FIELD_INTERLACED:
1178 /* Query for standard if not explicitly mentioned _TB/_BT */
1179 ret = v4l2_subdev_call(sd, video, querystd, &std);
1181 std = V4L2_STD_625_50;
1183 field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB :
1184 V4L2_FIELD_INTERLACED_BT;
1188 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1190 dev_warn(dev, "Format %x not found\n", pixfmt);
1193 /* Calculate client output geometry */
1194 soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf,
1196 mf.field = pix->field;
1197 mf.colorspace = pix->colorspace;
1198 mf.code = xlate->code;
1201 case V4L2_PIX_FMT_RGB32:
1202 can_scale = priv->chip != RCAR_E1;
1204 case V4L2_PIX_FMT_UYVY:
1205 case V4L2_PIX_FMT_YUYV:
1206 case V4L2_PIX_FMT_RGB565:
1207 case V4L2_PIX_FMT_RGB555X:
1215 dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height);
1217 ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1218 &mf, &vin_sub_width, &vin_sub_height,
1221 /* Done with the camera. Now see if we can improve the result */
1222 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1223 ret, mf.width, mf.height, pix->width, pix->height);
1225 if (ret == -ENOIOCTLCMD)
1226 dev_dbg(dev, "Sensor doesn't support scaling\n");
1230 if (mf.code != xlate->code)
1233 /* Prepare VIN crop */
1234 cam->width = mf.width;
1235 cam->height = mf.height;
1237 /* Use VIN scaling to scale to the requested user window. */
1239 /* We cannot scale up */
1240 if (pix->width > vin_sub_width)
1241 vin_sub_width = pix->width;
1243 if (pix->height > vin_sub_height)
1244 vin_sub_height = pix->height;
1246 pix->colorspace = mf.colorspace;
1249 pix->width = vin_sub_width;
1250 pix->height = vin_sub_height;
1254 * We have calculated CFLCR, the actual configuration will be performed
1255 * in rcar_vin_set_bus_param()
1258 dev_dbg(dev, "W: %u : %u, H: %u : %u\n",
1259 vin_sub_width, pix->width, vin_sub_height, pix->height);
1261 icd->current_fmt = xlate;
1263 priv->field = field;
1268 static int rcar_vin_try_fmt(struct soc_camera_device *icd,
1269 struct v4l2_format *f)
1271 const struct soc_camera_format_xlate *xlate;
1272 struct v4l2_pix_format *pix = &f->fmt.pix;
1273 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1274 struct v4l2_mbus_framefmt mf;
1275 __u32 pixfmt = pix->pixelformat;
1279 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1281 xlate = icd->current_fmt;
1282 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1283 pixfmt, xlate->host_fmt->fourcc);
1284 pixfmt = xlate->host_fmt->fourcc;
1285 pix->pixelformat = pixfmt;
1286 pix->colorspace = icd->colorspace;
1289 /* FIXME: calculate using depth and bus width */
1290 v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1,
1291 &pix->height, 4, VIN_MAX_HEIGHT, 2, 0);
1294 height = pix->height;
1296 /* let soc-camera calculate these values */
1297 pix->bytesperline = 0;
1300 /* limit to sensor capabilities */
1301 mf.width = pix->width;
1302 mf.height = pix->height;
1303 mf.field = pix->field;
1304 mf.code = xlate->code;
1305 mf.colorspace = pix->colorspace;
1307 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1308 video, try_mbus_fmt, &mf);
1312 pix->width = mf.width;
1313 pix->height = mf.height;
1314 pix->field = mf.field;
1315 pix->colorspace = mf.colorspace;
1317 if (pixfmt == V4L2_PIX_FMT_NV16) {
1318 /* FIXME: check against rect_max after converting soc-camera */
1319 /* We can scale precisely, need a bigger image from camera */
1320 if (pix->width < width || pix->height < height) {
1322 * We presume, the sensor behaves sanely, i.e. if
1323 * requested a bigger rectangle, it will not return a
1326 mf.width = VIN_MAX_WIDTH;
1327 mf.height = VIN_MAX_HEIGHT;
1328 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1329 soc_camera_grp_id(icd),
1330 video, try_mbus_fmt,
1333 dev_err(icd->parent,
1334 "client try_fmt() = %d\n", ret);
1338 /* We will scale exactly */
1339 if (mf.width > width)
1341 if (mf.height > height)
1342 pix->height = height;
1348 static unsigned int rcar_vin_poll(struct file *file, poll_table *pt)
1350 struct soc_camera_device *icd = file->private_data;
1352 return vb2_poll(&icd->vb2_vidq, file, pt);
1355 static int rcar_vin_querycap(struct soc_camera_host *ici,
1356 struct v4l2_capability *cap)
1358 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
1359 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1363 static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
1364 struct soc_camera_device *icd)
1366 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1367 vq->io_modes = VB2_MMAP | VB2_USERPTR;
1369 vq->ops = &rcar_vin_vb2_ops;
1370 vq->mem_ops = &vb2_dma_contig_memops;
1371 vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
1372 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1374 return vb2_queue_init(vq);
1377 static struct soc_camera_host_ops rcar_vin_host_ops = {
1378 .owner = THIS_MODULE,
1379 .add = rcar_vin_add_device,
1380 .remove = rcar_vin_remove_device,
1381 .clock_start = rcar_vin_clock_start,
1382 .clock_stop = rcar_vin_clock_stop,
1383 .get_formats = rcar_vin_get_formats,
1384 .put_formats = rcar_vin_put_formats,
1385 .get_crop = rcar_vin_get_crop,
1386 .set_crop = rcar_vin_set_crop,
1387 .try_fmt = rcar_vin_try_fmt,
1388 .set_fmt = rcar_vin_set_fmt,
1389 .poll = rcar_vin_poll,
1390 .querycap = rcar_vin_querycap,
1391 .set_bus_param = rcar_vin_set_bus_param,
1392 .init_videobuf2 = rcar_vin_init_videobuf2,
1395 static struct platform_device_id rcar_vin_id_table[] = {
1396 { "r8a7791-vin", RCAR_GEN2 },
1397 { "r8a7790-vin", RCAR_GEN2 },
1398 { "r8a7779-vin", RCAR_H1 },
1399 { "r8a7778-vin", RCAR_M1 },
1400 { "uPD35004-vin", RCAR_E1 },
1403 MODULE_DEVICE_TABLE(platform, rcar_vin_id_table);
1405 static int rcar_vin_probe(struct platform_device *pdev)
1407 struct rcar_vin_priv *priv;
1408 struct resource *mem;
1409 struct rcar_vin_platform_data *pdata;
1412 pdata = pdev->dev.platform_data;
1413 if (!pdata || !pdata->flags) {
1414 dev_err(&pdev->dev, "platform data not set\n");
1418 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1422 irq = platform_get_irq(pdev, 0);
1426 priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv),
1431 priv->base = devm_ioremap_resource(&pdev->dev, mem);
1432 if (IS_ERR(priv->base))
1433 return PTR_ERR(priv->base);
1435 ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED,
1436 dev_name(&pdev->dev), priv);
1440 priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1441 if (IS_ERR(priv->alloc_ctx))
1442 return PTR_ERR(priv->alloc_ctx);
1444 priv->ici.priv = priv;
1445 priv->ici.v4l2_dev.dev = &pdev->dev;
1446 priv->ici.nr = pdev->id;
1447 priv->ici.drv_name = dev_name(&pdev->dev);
1448 priv->ici.ops = &rcar_vin_host_ops;
1450 priv->pdata = pdata;
1451 priv->chip = pdev->id_entry->driver_data;
1452 spin_lock_init(&priv->lock);
1453 INIT_LIST_HEAD(&priv->capture);
1455 priv->state = STOPPED;
1457 pm_suspend_ignore_children(&pdev->dev, true);
1458 pm_runtime_enable(&pdev->dev);
1460 ret = soc_camera_host_register(&priv->ici);
1467 pm_runtime_disable(&pdev->dev);
1468 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1473 static int rcar_vin_remove(struct platform_device *pdev)
1475 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1476 struct rcar_vin_priv *priv = container_of(soc_host,
1477 struct rcar_vin_priv, ici);
1479 soc_camera_host_unregister(soc_host);
1480 pm_runtime_disable(&pdev->dev);
1481 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1486 static struct platform_driver rcar_vin_driver = {
1487 .probe = rcar_vin_probe,
1488 .remove = rcar_vin_remove,
1491 .owner = THIS_MODULE,
1493 .id_table = rcar_vin_id_table,
1496 module_platform_driver(rcar_vin_driver);
1498 MODULE_LICENSE("GPL");
1499 MODULE_ALIAS("platform:rcar_vin");
1500 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");