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>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/platform_data/camera-rcar.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/videodev2.h>
27 #include <media/soc_camera.h>
28 #include <media/soc_mediabus.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-dev.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-mediabus.h>
33 #include <media/v4l2-subdev.h>
34 #include <media/videobuf2-dma-contig.h>
36 #include "soc_scale_crop.h"
38 #define DRV_NAME "rcar_vin"
40 /* Register offsets for R-Car VIN */
41 #define VNMC_REG 0x00 /* Video n Main Control Register */
42 #define VNMS_REG 0x04 /* Video n Module Status Register */
43 #define VNFC_REG 0x08 /* Video n Frame Capture Register */
44 #define VNSLPRC_REG 0x0C /* Video n Start Line Pre-Clip Register */
45 #define VNELPRC_REG 0x10 /* Video n End Line Pre-Clip Register */
46 #define VNSPPRC_REG 0x14 /* Video n Start Pixel Pre-Clip Register */
47 #define VNEPPRC_REG 0x18 /* Video n End Pixel Pre-Clip Register */
48 #define VNSLPOC_REG 0x1C /* Video n Start Line Post-Clip Register */
49 #define VNELPOC_REG 0x20 /* Video n End Line Post-Clip Register */
50 #define VNSPPOC_REG 0x24 /* Video n Start Pixel Post-Clip Register */
51 #define VNEPPOC_REG 0x28 /* Video n End Pixel Post-Clip Register */
52 #define VNIS_REG 0x2C /* Video n Image Stride Register */
53 #define VNMB_REG(m) (0x30 + ((m) << 2)) /* Video n Memory Base m Register */
54 #define VNIE_REG 0x40 /* Video n Interrupt Enable Register */
55 #define VNINTS_REG 0x44 /* Video n Interrupt Status Register */
56 #define VNSI_REG 0x48 /* Video n Scanline Interrupt Register */
57 #define VNMTC_REG 0x4C /* Video n Memory Transfer Control Register */
58 #define VNYS_REG 0x50 /* Video n Y Scale Register */
59 #define VNXS_REG 0x54 /* Video n X Scale Register */
60 #define VNDMR_REG 0x58 /* Video n Data Mode Register */
61 #define VNDMR2_REG 0x5C /* Video n Data Mode Register 2 */
62 #define VNUVAOF_REG 0x60 /* Video n UV Address Offset Register */
64 /* Register bit fields for R-Car VIN */
65 /* Video n Main Control Register bits */
66 #define VNMC_FOC (1 << 21)
67 #define VNMC_YCAL (1 << 19)
68 #define VNMC_INF_YUV8_BT656 (0 << 16)
69 #define VNMC_INF_YUV8_BT601 (1 << 16)
70 #define VNMC_INF_YUV16 (5 << 16)
71 #define VNMC_VUP (1 << 10)
72 #define VNMC_IM_ODD (0 << 3)
73 #define VNMC_IM_ODD_EVEN (1 << 3)
74 #define VNMC_IM_EVEN (2 << 3)
75 #define VNMC_IM_FULL (3 << 3)
76 #define VNMC_BPS (1 << 1)
77 #define VNMC_ME (1 << 0)
79 /* Video n Module Status Register bits */
80 #define VNMS_FBS_MASK (3 << 3)
81 #define VNMS_FBS_SHIFT 3
82 #define VNMS_AV (1 << 1)
83 #define VNMS_CA (1 << 0)
85 /* Video n Frame Capture Register bits */
86 #define VNFC_C_FRAME (1 << 1)
87 #define VNFC_S_FRAME (1 << 0)
89 /* Video n Interrupt Enable Register bits */
90 #define VNIE_FIE (1 << 4)
91 #define VNIE_EFE (1 << 1)
93 /* Video n Data Mode Register bits */
94 #define VNDMR_EXRGB (1 << 8)
95 #define VNDMR_BPSM (1 << 4)
96 #define VNDMR_DTMD_YCSEP (1 << 1)
97 #define VNDMR_DTMD_ARGB1555 (1 << 0)
99 /* Video n Data Mode Register 2 bits */
100 #define VNDMR2_VPS (1 << 30)
101 #define VNDMR2_HPS (1 << 29)
102 #define VNDMR2_FTEV (1 << 17)
104 #define VIN_MAX_WIDTH 2048
105 #define VIN_MAX_HEIGHT 2048
114 enum rcar_vin_state {
120 struct rcar_vin_priv {
124 /* State of the VIN module in capturing mode */
125 enum rcar_vin_state state;
126 struct rcar_vin_platform_data *pdata;
127 struct soc_camera_host ici;
128 struct list_head capture;
129 #define MAX_BUFFER_NUM 3
130 struct vb2_buffer *queue_buf[MAX_BUFFER_NUM];
131 struct vb2_alloc_ctx *alloc_ctx;
132 enum v4l2_field field;
133 unsigned int vb_count;
134 unsigned int nr_hw_slots;
135 bool request_to_stop;
136 struct completion capture_stop;
140 #define is_continuous_transfer(priv) (priv->vb_count > MAX_BUFFER_NUM)
142 struct rcar_vin_buffer {
143 struct vb2_buffer vb;
144 struct list_head list;
147 #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
148 struct rcar_vin_buffer, \
151 struct rcar_vin_cam {
152 /* VIN offsets within the camera output, before the VIN scaler */
153 unsigned int vin_left;
154 unsigned int vin_top;
155 /* Client output, as seen by the VIN */
159 * User window from S_CROP / G_CROP, produced by client cropping and
160 * scaling, VIN scaling and VIN cropping, mapped back onto the client
163 struct v4l2_rect subrect;
164 /* Camera cropping rectangle */
165 struct v4l2_rect rect;
166 const struct soc_mbus_pixelfmt *extra_fmt;
170 * .queue_setup() is called to check whether the driver can accept the requested
171 * number of buffers and to fill in plane sizes for the current frame format if
174 static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
175 const struct v4l2_format *fmt,
177 unsigned int *num_planes,
178 unsigned int sizes[], void *alloc_ctxs[])
180 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
181 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
182 struct rcar_vin_priv *priv = ici->priv;
185 const struct soc_camera_format_xlate *xlate;
186 unsigned int bytes_per_line;
189 xlate = soc_camera_xlate_by_fourcc(icd,
190 fmt->fmt.pix.pixelformat);
193 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
198 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
200 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
201 fmt->fmt.pix.height);
205 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
207 /* Called from VIDIOC_REQBUFS or in compatibility mode */
208 sizes[0] = icd->sizeimage;
211 alloc_ctxs[0] = priv->alloc_ctx;
213 if (!vq->num_buffers)
218 priv->vb_count = *count;
222 /* Number of hardware slots */
223 if (is_continuous_transfer(priv))
224 priv->nr_hw_slots = MAX_BUFFER_NUM;
226 priv->nr_hw_slots = 1;
228 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
233 static int rcar_vin_setup(struct rcar_vin_priv *priv)
235 struct soc_camera_device *icd = priv->ici.icd;
236 struct rcar_vin_cam *cam = icd->host_priv;
237 u32 vnmc, dmr, interrupts;
238 bool progressive = false, output_is_yuv = false;
240 switch (priv->field) {
244 case V4L2_FIELD_BOTTOM:
247 case V4L2_FIELD_INTERLACED:
248 case V4L2_FIELD_INTERLACED_TB:
251 case V4L2_FIELD_INTERLACED_BT:
252 vnmc = VNMC_IM_FULL | VNMC_FOC;
254 case V4L2_FIELD_NONE:
255 if (is_continuous_transfer(priv)) {
256 vnmc = VNMC_IM_ODD_EVEN;
267 /* input interface */
268 switch (icd->current_fmt->code) {
269 case V4L2_MBUS_FMT_YUYV8_1X16:
270 /* BT.601/BT.1358 16bit YCbCr422 */
271 vnmc |= VNMC_INF_YUV16;
273 case V4L2_MBUS_FMT_YUYV8_2X8:
274 /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
275 vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ?
276 VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
282 switch (icd->current_fmt->host_fmt->fourcc) {
283 case V4L2_PIX_FMT_NV16:
284 iowrite32(ALIGN(cam->width * cam->height, 0x80),
285 priv->base + VNUVAOF_REG);
286 dmr = VNDMR_DTMD_YCSEP;
287 output_is_yuv = true;
289 case V4L2_PIX_FMT_YUYV:
291 output_is_yuv = true;
293 case V4L2_PIX_FMT_UYVY:
295 output_is_yuv = true;
297 case V4L2_PIX_FMT_RGB555X:
298 dmr = VNDMR_DTMD_ARGB1555;
300 case V4L2_PIX_FMT_RGB565:
303 case V4L2_PIX_FMT_RGB32:
304 if (priv->chip == RCAR_H2 || priv->chip == RCAR_H1 ||
305 priv->chip == RCAR_E1) {
310 dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n",
311 icd->current_fmt->host_fmt->fourcc);
315 /* Always update on field change */
318 /* If input and output use the same colorspace, use bypass mode */
322 /* progressive or interlaced mode */
323 interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE;
326 iowrite32(interrupts, priv->base + VNINTS_REG);
327 /* enable interrupts */
328 iowrite32(interrupts, priv->base + VNIE_REG);
329 /* start capturing */
330 iowrite32(dmr, priv->base + VNDMR_REG);
331 iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG);
336 static void rcar_vin_capture(struct rcar_vin_priv *priv)
338 if (is_continuous_transfer(priv))
339 /* Continuous Frame Capture Mode */
340 iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG);
342 /* Single Frame Capture Mode */
343 iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG);
346 static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv)
348 priv->state = STOPPING;
350 /* set continuous & single transfer off */
351 iowrite32(0, priv->base + VNFC_REG);
352 /* disable capture (release DMA buffer), reset */
353 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
354 priv->base + VNMC_REG);
356 /* update the status if stopped already */
357 if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA))
358 priv->state = STOPPED;
361 static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv)
365 for (slot = 0; slot < priv->nr_hw_slots; slot++)
366 if (priv->queue_buf[slot] == NULL)
372 static int rcar_vin_hw_ready(struct rcar_vin_priv *priv)
374 /* Ensure all HW slots are filled */
375 return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0;
378 /* Moves a buffer from the queue to the HW slots */
379 static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv)
381 struct vb2_buffer *vb;
382 dma_addr_t phys_addr_top;
385 if (list_empty(&priv->capture))
388 /* Find a free HW slot */
389 slot = rcar_vin_get_free_hw_slot(priv);
393 vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb;
394 list_del_init(to_buf_list(vb));
395 priv->queue_buf[slot] = vb;
396 phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0);
397 iowrite32(phys_addr_top, priv->base + VNMB_REG(slot));
402 static void rcar_vin_videobuf_queue(struct vb2_buffer *vb)
404 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
405 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
406 struct rcar_vin_priv *priv = ici->priv;
409 size = icd->sizeimage;
411 if (vb2_plane_size(vb, 0) < size) {
412 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
413 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
417 vb2_set_plane_payload(vb, 0, size);
419 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
420 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
422 spin_lock_irq(&priv->lock);
424 list_add_tail(to_buf_list(vb), &priv->capture);
425 rcar_vin_fill_hw_slot(priv);
427 /* If we weren't running, and have enough buffers, start capturing! */
428 if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) {
429 if (rcar_vin_setup(priv)) {
431 list_del_init(to_buf_list(vb));
432 spin_unlock_irq(&priv->lock);
435 priv->request_to_stop = false;
436 init_completion(&priv->capture_stop);
437 priv->state = RUNNING;
438 rcar_vin_capture(priv);
441 spin_unlock_irq(&priv->lock);
446 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
449 static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
451 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
452 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
453 struct rcar_vin_priv *priv = ici->priv;
457 spin_lock_irq(&priv->lock);
459 /* Is the buffer in use by the VIN hardware? */
460 for (i = 0; i < MAX_BUFFER_NUM; i++) {
461 if (priv->queue_buf[i] == vb) {
468 while (priv->state != STOPPED) {
470 /* issue stop if running */
471 if (priv->state == RUNNING)
472 rcar_vin_request_capture_stop(priv);
474 /* wait until capturing has been stopped */
475 if (priv->state == STOPPING) {
476 priv->request_to_stop = true;
477 spin_unlock_irq(&priv->lock);
478 wait_for_completion(&priv->capture_stop);
479 spin_lock_irq(&priv->lock);
483 * Capturing has now stopped. The buffer we have been asked
484 * to release could be any of the current buffers in use, so
485 * release all buffers that are in use by HW
487 for (i = 0; i < MAX_BUFFER_NUM; i++) {
488 if (priv->queue_buf[i]) {
489 vb2_buffer_done(priv->queue_buf[i],
490 VB2_BUF_STATE_ERROR);
491 priv->queue_buf[i] = NULL;
495 list_del_init(to_buf_list(vb));
498 spin_unlock_irq(&priv->lock);
501 static int rcar_vin_videobuf_init(struct vb2_buffer *vb)
503 INIT_LIST_HEAD(to_buf_list(vb));
507 static int rcar_vin_stop_streaming(struct vb2_queue *vq)
509 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
510 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
511 struct rcar_vin_priv *priv = ici->priv;
512 struct list_head *buf_head, *tmp;
514 spin_lock_irq(&priv->lock);
515 list_for_each_safe(buf_head, tmp, &priv->capture)
516 list_del_init(buf_head);
517 spin_unlock_irq(&priv->lock);
522 static struct vb2_ops rcar_vin_vb2_ops = {
523 .queue_setup = rcar_vin_videobuf_setup,
524 .buf_init = rcar_vin_videobuf_init,
525 .buf_cleanup = rcar_vin_videobuf_release,
526 .buf_queue = rcar_vin_videobuf_queue,
527 .stop_streaming = rcar_vin_stop_streaming,
528 .wait_prepare = soc_camera_unlock,
529 .wait_finish = soc_camera_lock,
532 static irqreturn_t rcar_vin_irq(int irq, void *data)
534 struct rcar_vin_priv *priv = data;
536 bool can_run = false, hw_stopped;
538 unsigned int handled = 0;
540 spin_lock(&priv->lock);
542 int_status = ioread32(priv->base + VNINTS_REG);
546 iowrite32(int_status, priv->base + VNINTS_REG);
549 /* nothing to do if capture status is 'STOPPED' */
550 if (priv->state == STOPPED)
553 hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA);
555 if (!priv->request_to_stop) {
556 if (is_continuous_transfer(priv))
557 slot = (ioread32(priv->base + VNMS_REG) &
558 VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
562 priv->queue_buf[slot]->v4l2_buf.field = priv->field;
563 priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++;
564 do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp);
565 vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE);
566 priv->queue_buf[slot] = NULL;
568 if (priv->state != STOPPING)
569 can_run = rcar_vin_fill_hw_slot(priv);
571 if (hw_stopped || !can_run) {
572 priv->state = STOPPED;
573 } else if (is_continuous_transfer(priv) &&
574 list_empty(&priv->capture) &&
575 priv->state == RUNNING) {
577 * The continuous capturing requires an explicit stop
578 * operation when there is no buffer to be set into
579 * the VnMBm registers.
581 rcar_vin_request_capture_stop(priv);
583 rcar_vin_capture(priv);
586 } else if (hw_stopped) {
587 priv->state = STOPPED;
588 priv->request_to_stop = false;
589 complete(&priv->capture_stop);
593 spin_unlock(&priv->lock);
595 return IRQ_RETVAL(handled);
598 static int rcar_vin_add_device(struct soc_camera_device *icd)
600 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
601 struct rcar_vin_priv *priv = ici->priv;
604 for (i = 0; i < MAX_BUFFER_NUM; i++)
605 priv->queue_buf[i] = NULL;
607 pm_runtime_get_sync(ici->v4l2_dev.dev);
609 dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n",
615 static void rcar_vin_remove_device(struct soc_camera_device *icd)
617 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
618 struct rcar_vin_priv *priv = ici->priv;
619 struct vb2_buffer *vb;
622 /* disable capture, disable interrupts */
623 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
624 priv->base + VNMC_REG);
625 iowrite32(0, priv->base + VNIE_REG);
627 priv->state = STOPPED;
628 priv->request_to_stop = false;
630 /* make sure active buffer is cancelled */
631 spin_lock_irq(&priv->lock);
632 for (i = 0; i < MAX_BUFFER_NUM; i++) {
633 vb = priv->queue_buf[i];
635 list_del_init(to_buf_list(vb));
636 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
639 spin_unlock_irq(&priv->lock);
641 pm_runtime_put(ici->v4l2_dev.dev);
643 dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n",
647 /* Called with .host_lock held */
648 static int rcar_vin_clock_start(struct soc_camera_host *ici)
650 /* VIN does not have "mclk" */
654 /* Called with .host_lock held */
655 static void rcar_vin_clock_stop(struct soc_camera_host *ici)
657 /* VIN does not have "mclk" */
660 /* rect is guaranteed to not exceed the scaled camera rectangle */
661 static int rcar_vin_set_rect(struct soc_camera_device *icd)
663 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
664 struct rcar_vin_cam *cam = icd->host_priv;
665 struct rcar_vin_priv *priv = ici->priv;
666 unsigned int left_offset, top_offset;
667 unsigned char dsize = 0;
668 struct v4l2_rect *cam_subrect = &cam->subrect;
670 dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n",
671 icd->user_width, icd->user_height, cam->vin_left, cam->vin_top);
673 left_offset = cam->vin_left;
674 top_offset = cam->vin_top;
676 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 &&
677 priv->chip == RCAR_E1)
680 dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n",
681 cam->width, cam->height, cam->vin_left, cam->vin_top);
682 dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n",
683 cam_subrect->width, cam_subrect->height,
684 cam_subrect->left, cam_subrect->top);
686 /* Set Start/End Pixel/Line Pre-Clip */
687 iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG);
688 iowrite32((left_offset + cam->width - 1) << dsize,
689 priv->base + VNEPPRC_REG);
690 switch (priv->field) {
691 case V4L2_FIELD_INTERLACED:
692 case V4L2_FIELD_INTERLACED_TB:
693 case V4L2_FIELD_INTERLACED_BT:
694 iowrite32(top_offset / 2, priv->base + VNSLPRC_REG);
695 iowrite32((top_offset + cam->height) / 2 - 1,
696 priv->base + VNELPRC_REG);
699 iowrite32(top_offset, priv->base + VNSLPRC_REG);
700 iowrite32(top_offset + cam->height - 1,
701 priv->base + VNELPRC_REG);
705 /* Set Start/End Pixel/Line Post-Clip */
706 iowrite32(0, priv->base + VNSPPOC_REG);
707 iowrite32(0, priv->base + VNSLPOC_REG);
708 iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG);
709 switch (priv->field) {
710 case V4L2_FIELD_INTERLACED:
711 case V4L2_FIELD_INTERLACED_TB:
712 case V4L2_FIELD_INTERLACED_BT:
713 iowrite32(cam_subrect->height / 2 - 1,
714 priv->base + VNELPOC_REG);
717 iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG);
721 iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG);
726 static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc)
728 *vnmc = ioread32(priv->base + VNMC_REG);
730 iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG);
733 static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc)
735 unsigned long timeout = jiffies + 10 * HZ;
738 * Wait until the end of the current frame. It can take a long time,
739 * but if it has been aborted by a MRST1 reset, it should exit sooner.
741 while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) &&
742 time_before(jiffies, timeout))
745 if (time_after(jiffies, timeout)) {
746 dev_err(priv->ici.v4l2_dev.dev,
747 "Timeout waiting for frame end! Interface problem?\n");
751 iowrite32(vnmc, priv->base + VNMC_REG);
754 #define VIN_MBUS_FLAGS (V4L2_MBUS_MASTER | \
755 V4L2_MBUS_PCLK_SAMPLE_RISING | \
756 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
757 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
758 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
759 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
760 V4L2_MBUS_DATA_ACTIVE_HIGH)
762 static int rcar_vin_set_bus_param(struct soc_camera_device *icd)
764 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
765 struct rcar_vin_priv *priv = ici->priv;
766 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
767 struct v4l2_mbus_config cfg;
768 unsigned long common_flags;
773 capture_stop_preserve(priv, &vnmc);
775 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
777 common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
779 dev_warn(icd->parent,
780 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
781 cfg.flags, VIN_MBUS_FLAGS);
784 } else if (ret != -ENOIOCTLCMD) {
787 common_flags = VIN_MBUS_FLAGS;
790 /* Make choises, based on platform preferences */
791 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
792 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
793 if (priv->pdata->flags & RCAR_VIN_HSYNC_ACTIVE_LOW)
794 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
796 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
799 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
800 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
801 if (priv->pdata->flags & RCAR_VIN_VSYNC_ACTIVE_LOW)
802 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
804 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
807 cfg.flags = common_flags;
808 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
809 if (ret < 0 && ret != -ENOIOCTLCMD)
812 val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0;
813 if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
815 if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
817 iowrite32(val, priv->base + VNDMR2_REG);
819 ret = rcar_vin_set_rect(icd);
823 capture_restore(priv, vnmc);
828 static int rcar_vin_try_bus_param(struct soc_camera_device *icd,
829 unsigned char buswidth)
831 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
832 struct v4l2_mbus_config cfg;
835 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
836 if (ret == -ENOIOCTLCMD)
844 /* check is there common mbus flags */
845 ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
849 dev_warn(icd->parent,
850 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
851 cfg.flags, VIN_MBUS_FLAGS);
856 static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt)
858 return fmt->packing == SOC_MBUS_PACKING_NONE ||
859 (fmt->bits_per_sample > 8 &&
860 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
863 static const struct soc_mbus_pixelfmt rcar_vin_formats[] = {
865 .fourcc = V4L2_PIX_FMT_NV16,
867 .bits_per_sample = 8,
868 .packing = SOC_MBUS_PACKING_2X8_PADHI,
869 .order = SOC_MBUS_ORDER_LE,
870 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
873 .fourcc = V4L2_PIX_FMT_UYVY,
875 .bits_per_sample = 16,
876 .packing = SOC_MBUS_PACKING_NONE,
877 .order = SOC_MBUS_ORDER_LE,
878 .layout = SOC_MBUS_LAYOUT_PACKED,
881 .fourcc = V4L2_PIX_FMT_RGB565,
883 .bits_per_sample = 16,
884 .packing = SOC_MBUS_PACKING_NONE,
885 .order = SOC_MBUS_ORDER_LE,
886 .layout = SOC_MBUS_LAYOUT_PACKED,
889 .fourcc = V4L2_PIX_FMT_RGB555X,
891 .bits_per_sample = 16,
892 .packing = SOC_MBUS_PACKING_NONE,
893 .order = SOC_MBUS_ORDER_LE,
894 .layout = SOC_MBUS_LAYOUT_PACKED,
897 .fourcc = V4L2_PIX_FMT_RGB32,
899 .bits_per_sample = 32,
900 .packing = SOC_MBUS_PACKING_NONE,
901 .order = SOC_MBUS_ORDER_LE,
902 .layout = SOC_MBUS_LAYOUT_PACKED,
906 static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx,
907 struct soc_camera_format_xlate *xlate)
909 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
910 struct device *dev = icd->parent;
913 struct rcar_vin_cam *cam;
914 enum v4l2_mbus_pixelcode code;
915 const struct soc_mbus_pixelfmt *fmt;
917 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
921 fmt = soc_mbus_get_fmtdesc(code);
923 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
927 ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample);
931 if (!icd->host_priv) {
932 struct v4l2_mbus_framefmt mf;
933 struct v4l2_rect rect;
934 struct device *dev = icd->parent;
937 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
941 /* Cache current client geometry */
942 ret = soc_camera_client_g_rect(sd, &rect);
943 if (ret == -ENOIOCTLCMD) {
944 /* Sensor driver doesn't support cropping */
947 rect.width = mf.width;
948 rect.height = mf.height;
949 } else if (ret < 0) {
954 * If sensor proposes too large format then try smaller ones:
955 * 1280x960, 640x480, 320x240
957 for (shift = 0; shift < 3; shift++) {
958 if (mf.width <= VIN_MAX_WIDTH &&
959 mf.height <= VIN_MAX_HEIGHT)
962 mf.width = 1280 >> shift;
963 mf.height = 960 >> shift;
964 ret = v4l2_device_call_until_err(sd->v4l2_dev,
965 soc_camera_grp_id(icd),
974 "Failed to configure the client below %ux%x\n",
975 mf.width, mf.height);
979 dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height);
981 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
985 * We are called with current camera crop,
986 * initialise subrect with it
990 cam->width = mf.width;
991 cam->height = mf.height;
993 icd->host_priv = cam;
995 cam = icd->host_priv;
998 /* Beginning of a pass */
1000 cam->extra_fmt = NULL;
1003 case V4L2_MBUS_FMT_YUYV8_1X16:
1004 case V4L2_MBUS_FMT_YUYV8_2X8:
1008 /* Add all our formats that can be generated by VIN */
1009 cam->extra_fmt = rcar_vin_formats;
1011 n = ARRAY_SIZE(rcar_vin_formats);
1013 for (k = 0; xlate && k < n; k++, xlate++) {
1014 xlate->host_fmt = &rcar_vin_formats[k];
1016 dev_dbg(dev, "Providing format %s using code %d\n",
1017 rcar_vin_formats[k].name, code);
1021 if (!rcar_vin_packing_supported(fmt))
1024 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1029 /* Generic pass-through */
1032 xlate->host_fmt = fmt;
1040 static void rcar_vin_put_formats(struct soc_camera_device *icd)
1042 kfree(icd->host_priv);
1043 icd->host_priv = NULL;
1046 static int rcar_vin_set_crop(struct soc_camera_device *icd,
1047 const struct v4l2_crop *a)
1049 struct v4l2_crop a_writable = *a;
1050 const struct v4l2_rect *rect = &a_writable.c;
1051 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1052 struct rcar_vin_priv *priv = ici->priv;
1053 struct v4l2_crop cam_crop;
1054 struct rcar_vin_cam *cam = icd->host_priv;
1055 struct v4l2_rect *cam_rect = &cam_crop.c;
1056 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1057 struct device *dev = icd->parent;
1058 struct v4l2_mbus_framefmt mf;
1062 dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1063 rect->left, rect->top);
1065 /* During camera cropping its output window can change too, stop VIN */
1066 capture_stop_preserve(priv, &vnmc);
1067 dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc);
1069 /* Apply iterative camera S_CROP for new input window. */
1070 ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
1071 &cam->rect, &cam->subrect);
1075 dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n",
1076 cam_rect->width, cam_rect->height,
1077 cam_rect->left, cam_rect->top);
1079 /* On success cam_crop contains current camera crop */
1081 /* Retrieve camera output window */
1082 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1086 if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT)
1089 /* Cache camera output window */
1090 cam->width = mf.width;
1091 cam->height = mf.height;
1093 icd->user_width = cam->width;
1094 icd->user_height = cam->height;
1096 cam->vin_left = rect->left & ~1;
1097 cam->vin_top = rect->top & ~1;
1099 /* Use VIN cropping to crop to the new window. */
1100 ret = rcar_vin_set_rect(icd);
1104 cam->subrect = *rect;
1106 dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n",
1107 icd->user_width, icd->user_height,
1108 cam->vin_left, cam->vin_top);
1110 /* Restore capture */
1111 for (i = 0; i < MAX_BUFFER_NUM; i++) {
1112 if (priv->queue_buf[i] && priv->state == STOPPED) {
1117 capture_restore(priv, vnmc);
1119 /* Even if only camera cropping succeeded */
1123 static int rcar_vin_get_crop(struct soc_camera_device *icd,
1124 struct v4l2_crop *a)
1126 struct rcar_vin_cam *cam = icd->host_priv;
1128 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1129 a->c = cam->subrect;
1134 /* Similar to set_crop multistage iterative algorithm */
1135 static int rcar_vin_set_fmt(struct soc_camera_device *icd,
1136 struct v4l2_format *f)
1138 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1139 struct rcar_vin_priv *priv = ici->priv;
1140 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1141 struct rcar_vin_cam *cam = icd->host_priv;
1142 struct v4l2_pix_format *pix = &f->fmt.pix;
1143 struct v4l2_mbus_framefmt mf;
1144 struct device *dev = icd->parent;
1145 __u32 pixfmt = pix->pixelformat;
1146 const struct soc_camera_format_xlate *xlate;
1147 unsigned int vin_sub_width = 0, vin_sub_height = 0;
1150 enum v4l2_field field;
1153 dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n",
1154 pixfmt, pix->width, pix->height);
1156 switch (pix->field) {
1158 pix->field = V4L2_FIELD_NONE;
1160 case V4L2_FIELD_NONE:
1161 case V4L2_FIELD_TOP:
1162 case V4L2_FIELD_BOTTOM:
1163 case V4L2_FIELD_INTERLACED_TB:
1164 case V4L2_FIELD_INTERLACED_BT:
1167 case V4L2_FIELD_INTERLACED:
1168 /* Query for standard if not explicitly mentioned _TB/_BT */
1169 ret = v4l2_subdev_call(sd, video, querystd, &std);
1171 std = V4L2_STD_625_50;
1173 field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB :
1174 V4L2_FIELD_INTERLACED_BT;
1178 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1180 dev_warn(dev, "Format %x not found\n", pixfmt);
1183 /* Calculate client output geometry */
1184 soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf,
1186 mf.field = pix->field;
1187 mf.colorspace = pix->colorspace;
1188 mf.code = xlate->code;
1191 case V4L2_PIX_FMT_RGB32:
1192 can_scale = priv->chip != RCAR_E1;
1194 case V4L2_PIX_FMT_UYVY:
1195 case V4L2_PIX_FMT_YUYV:
1196 case V4L2_PIX_FMT_RGB565:
1197 case V4L2_PIX_FMT_RGB555X:
1205 dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height);
1207 ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1208 &mf, &vin_sub_width, &vin_sub_height,
1211 /* Done with the camera. Now see if we can improve the result */
1212 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1213 ret, mf.width, mf.height, pix->width, pix->height);
1215 if (ret == -ENOIOCTLCMD)
1216 dev_dbg(dev, "Sensor doesn't support scaling\n");
1220 if (mf.code != xlate->code)
1223 /* Prepare VIN crop */
1224 cam->width = mf.width;
1225 cam->height = mf.height;
1227 /* Use VIN scaling to scale to the requested user window. */
1229 /* We cannot scale up */
1230 if (pix->width > vin_sub_width)
1231 vin_sub_width = pix->width;
1233 if (pix->height > vin_sub_height)
1234 vin_sub_height = pix->height;
1236 pix->colorspace = mf.colorspace;
1239 pix->width = vin_sub_width;
1240 pix->height = vin_sub_height;
1244 * We have calculated CFLCR, the actual configuration will be performed
1245 * in rcar_vin_set_bus_param()
1248 dev_dbg(dev, "W: %u : %u, H: %u : %u\n",
1249 vin_sub_width, pix->width, vin_sub_height, pix->height);
1251 icd->current_fmt = xlate;
1253 priv->field = field;
1258 static int rcar_vin_try_fmt(struct soc_camera_device *icd,
1259 struct v4l2_format *f)
1261 const struct soc_camera_format_xlate *xlate;
1262 struct v4l2_pix_format *pix = &f->fmt.pix;
1263 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1264 struct v4l2_mbus_framefmt mf;
1265 __u32 pixfmt = pix->pixelformat;
1269 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1271 xlate = icd->current_fmt;
1272 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1273 pixfmt, xlate->host_fmt->fourcc);
1274 pixfmt = xlate->host_fmt->fourcc;
1275 pix->pixelformat = pixfmt;
1276 pix->colorspace = icd->colorspace;
1279 /* FIXME: calculate using depth and bus width */
1280 v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1,
1281 &pix->height, 4, VIN_MAX_HEIGHT, 2, 0);
1284 height = pix->height;
1286 /* let soc-camera calculate these values */
1287 pix->bytesperline = 0;
1290 /* limit to sensor capabilities */
1291 mf.width = pix->width;
1292 mf.height = pix->height;
1293 mf.field = pix->field;
1294 mf.code = xlate->code;
1295 mf.colorspace = pix->colorspace;
1297 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1298 video, try_mbus_fmt, &mf);
1302 pix->width = mf.width;
1303 pix->height = mf.height;
1304 pix->field = mf.field;
1305 pix->colorspace = mf.colorspace;
1307 if (pixfmt == V4L2_PIX_FMT_NV16) {
1308 /* FIXME: check against rect_max after converting soc-camera */
1309 /* We can scale precisely, need a bigger image from camera */
1310 if (pix->width < width || pix->height < height) {
1312 * We presume, the sensor behaves sanely, i.e. if
1313 * requested a bigger rectangle, it will not return a
1316 mf.width = VIN_MAX_WIDTH;
1317 mf.height = VIN_MAX_HEIGHT;
1318 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1319 soc_camera_grp_id(icd),
1320 video, try_mbus_fmt,
1323 dev_err(icd->parent,
1324 "client try_fmt() = %d\n", ret);
1328 /* We will scale exactly */
1329 if (mf.width > width)
1331 if (mf.height > height)
1332 pix->height = height;
1338 static unsigned int rcar_vin_poll(struct file *file, poll_table *pt)
1340 struct soc_camera_device *icd = file->private_data;
1342 return vb2_poll(&icd->vb2_vidq, file, pt);
1345 static int rcar_vin_querycap(struct soc_camera_host *ici,
1346 struct v4l2_capability *cap)
1348 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
1349 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1353 static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
1354 struct soc_camera_device *icd)
1356 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1357 vq->io_modes = VB2_MMAP | VB2_USERPTR;
1359 vq->ops = &rcar_vin_vb2_ops;
1360 vq->mem_ops = &vb2_dma_contig_memops;
1361 vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
1362 vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1364 return vb2_queue_init(vq);
1367 static struct soc_camera_host_ops rcar_vin_host_ops = {
1368 .owner = THIS_MODULE,
1369 .add = rcar_vin_add_device,
1370 .remove = rcar_vin_remove_device,
1371 .clock_start = rcar_vin_clock_start,
1372 .clock_stop = rcar_vin_clock_stop,
1373 .get_formats = rcar_vin_get_formats,
1374 .put_formats = rcar_vin_put_formats,
1375 .get_crop = rcar_vin_get_crop,
1376 .set_crop = rcar_vin_set_crop,
1377 .try_fmt = rcar_vin_try_fmt,
1378 .set_fmt = rcar_vin_set_fmt,
1379 .poll = rcar_vin_poll,
1380 .querycap = rcar_vin_querycap,
1381 .set_bus_param = rcar_vin_set_bus_param,
1382 .init_videobuf2 = rcar_vin_init_videobuf2,
1385 static struct platform_device_id rcar_vin_id_table[] = {
1386 { "r8a7790-vin", RCAR_H2 },
1387 { "r8a7779-vin", RCAR_H1 },
1388 { "r8a7778-vin", RCAR_M1 },
1389 { "uPD35004-vin", RCAR_E1 },
1392 MODULE_DEVICE_TABLE(platform, rcar_vin_id_table);
1394 static int rcar_vin_probe(struct platform_device *pdev)
1396 struct rcar_vin_priv *priv;
1397 struct resource *mem;
1398 struct rcar_vin_platform_data *pdata;
1401 pdata = pdev->dev.platform_data;
1402 if (!pdata || !pdata->flags) {
1403 dev_err(&pdev->dev, "platform data not set\n");
1407 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1411 irq = platform_get_irq(pdev, 0);
1415 priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv),
1420 priv->base = devm_ioremap_resource(&pdev->dev, mem);
1421 if (IS_ERR(priv->base))
1422 return PTR_ERR(priv->base);
1424 ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED,
1425 dev_name(&pdev->dev), priv);
1429 priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1430 if (IS_ERR(priv->alloc_ctx))
1431 return PTR_ERR(priv->alloc_ctx);
1433 priv->ici.priv = priv;
1434 priv->ici.v4l2_dev.dev = &pdev->dev;
1435 priv->ici.nr = pdev->id;
1436 priv->ici.drv_name = dev_name(&pdev->dev);
1437 priv->ici.ops = &rcar_vin_host_ops;
1439 priv->pdata = pdata;
1440 priv->chip = pdev->id_entry->driver_data;
1441 spin_lock_init(&priv->lock);
1442 INIT_LIST_HEAD(&priv->capture);
1444 priv->state = STOPPED;
1446 pm_suspend_ignore_children(&pdev->dev, true);
1447 pm_runtime_enable(&pdev->dev);
1449 ret = soc_camera_host_register(&priv->ici);
1456 pm_runtime_disable(&pdev->dev);
1457 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1462 static int rcar_vin_remove(struct platform_device *pdev)
1464 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1465 struct rcar_vin_priv *priv = container_of(soc_host,
1466 struct rcar_vin_priv, ici);
1468 soc_camera_host_unregister(soc_host);
1469 pm_runtime_disable(&pdev->dev);
1470 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1475 static struct platform_driver rcar_vin_driver = {
1476 .probe = rcar_vin_probe,
1477 .remove = rcar_vin_remove,
1480 .owner = THIS_MODULE,
1482 .id_table = rcar_vin_id_table,
1485 module_platform_driver(rcar_vin_driver);
1487 MODULE_LICENSE("GPL");
1488 MODULE_ALIAS("platform:rcar_vin");
1489 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");