x86, cpufeature: If we disable CLFLUSH, we should disable CLFLUSHOPT
[linux/fpc-iii.git] / drivers / media / platform / soc_camera / rcar_vin.c
blob3b1c05a72d00cf16ab7a5f070aa09da12b35d00a
1 /*
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/io.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_YUV16 (5 << 16)
72 #define VNMC_VUP (1 << 10)
73 #define VNMC_IM_ODD (0 << 3)
74 #define VNMC_IM_ODD_EVEN (1 << 3)
75 #define VNMC_IM_EVEN (2 << 3)
76 #define VNMC_IM_FULL (3 << 3)
77 #define VNMC_BPS (1 << 1)
78 #define VNMC_ME (1 << 0)
80 /* Video n Module Status Register bits */
81 #define VNMS_FBS_MASK (3 << 3)
82 #define VNMS_FBS_SHIFT 3
83 #define VNMS_AV (1 << 1)
84 #define VNMS_CA (1 << 0)
86 /* Video n Frame Capture Register bits */
87 #define VNFC_C_FRAME (1 << 1)
88 #define VNFC_S_FRAME (1 << 0)
90 /* Video n Interrupt Enable Register bits */
91 #define VNIE_FIE (1 << 4)
92 #define VNIE_EFE (1 << 1)
94 /* Video n Data Mode Register bits */
95 #define VNDMR_EXRGB (1 << 8)
96 #define VNDMR_BPSM (1 << 4)
97 #define VNDMR_DTMD_YCSEP (1 << 1)
98 #define VNDMR_DTMD_ARGB1555 (1 << 0)
100 /* Video n Data Mode Register 2 bits */
101 #define VNDMR2_VPS (1 << 30)
102 #define VNDMR2_HPS (1 << 29)
103 #define VNDMR2_FTEV (1 << 17)
105 #define VIN_MAX_WIDTH 2048
106 #define VIN_MAX_HEIGHT 2048
108 enum chip_id {
109 RCAR_GEN2,
110 RCAR_H1,
111 RCAR_M1,
112 RCAR_E1,
115 enum rcar_vin_state {
116 STOPPED = 0,
117 RUNNING,
118 STOPPING,
121 struct rcar_vin_priv {
122 void __iomem *base;
123 spinlock_t lock;
124 int sequence;
125 /* State of the VIN module in capturing mode */
126 enum rcar_vin_state state;
127 struct rcar_vin_platform_data *pdata;
128 struct soc_camera_host ici;
129 struct list_head capture;
130 #define MAX_BUFFER_NUM 3
131 struct vb2_buffer *queue_buf[MAX_BUFFER_NUM];
132 struct vb2_alloc_ctx *alloc_ctx;
133 enum v4l2_field field;
134 unsigned int vb_count;
135 unsigned int nr_hw_slots;
136 bool request_to_stop;
137 struct completion capture_stop;
138 enum chip_id chip;
141 #define is_continuous_transfer(priv) (priv->vb_count > MAX_BUFFER_NUM)
143 struct rcar_vin_buffer {
144 struct vb2_buffer vb;
145 struct list_head list;
148 #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
149 struct rcar_vin_buffer, \
150 vb)->list)
152 struct rcar_vin_cam {
153 /* VIN offsets within the camera output, before the VIN scaler */
154 unsigned int vin_left;
155 unsigned int vin_top;
156 /* Client output, as seen by the VIN */
157 unsigned int width;
158 unsigned int height;
160 * User window from S_CROP / G_CROP, produced by client cropping and
161 * scaling, VIN scaling and VIN cropping, mapped back onto the client
162 * input window
164 struct v4l2_rect subrect;
165 /* Camera cropping rectangle */
166 struct v4l2_rect rect;
167 const struct soc_mbus_pixelfmt *extra_fmt;
171 * .queue_setup() is called to check whether the driver can accept the requested
172 * number of buffers and to fill in plane sizes for the current frame format if
173 * required
175 static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
176 const struct v4l2_format *fmt,
177 unsigned int *count,
178 unsigned int *num_planes,
179 unsigned int sizes[], void *alloc_ctxs[])
181 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
182 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
183 struct rcar_vin_priv *priv = ici->priv;
185 if (fmt) {
186 const struct soc_camera_format_xlate *xlate;
187 unsigned int bytes_per_line;
188 int ret;
190 xlate = soc_camera_xlate_by_fourcc(icd,
191 fmt->fmt.pix.pixelformat);
192 if (!xlate)
193 return -EINVAL;
194 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
195 xlate->host_fmt);
196 if (ret < 0)
197 return ret;
199 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
201 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
202 fmt->fmt.pix.height);
203 if (ret < 0)
204 return ret;
206 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
207 } else {
208 /* Called from VIDIOC_REQBUFS or in compatibility mode */
209 sizes[0] = icd->sizeimage;
212 alloc_ctxs[0] = priv->alloc_ctx;
214 if (!vq->num_buffers)
215 priv->sequence = 0;
217 if (!*count)
218 *count = 2;
219 priv->vb_count = *count;
221 *num_planes = 1;
223 /* Number of hardware slots */
224 if (is_continuous_transfer(priv))
225 priv->nr_hw_slots = MAX_BUFFER_NUM;
226 else
227 priv->nr_hw_slots = 1;
229 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
231 return 0;
234 static int rcar_vin_setup(struct rcar_vin_priv *priv)
236 struct soc_camera_device *icd = priv->ici.icd;
237 struct rcar_vin_cam *cam = icd->host_priv;
238 u32 vnmc, dmr, interrupts;
239 bool progressive = false, output_is_yuv = false;
241 switch (priv->field) {
242 case V4L2_FIELD_TOP:
243 vnmc = VNMC_IM_ODD;
244 break;
245 case V4L2_FIELD_BOTTOM:
246 vnmc = VNMC_IM_EVEN;
247 break;
248 case V4L2_FIELD_INTERLACED:
249 case V4L2_FIELD_INTERLACED_TB:
250 vnmc = VNMC_IM_FULL;
251 break;
252 case V4L2_FIELD_INTERLACED_BT:
253 vnmc = VNMC_IM_FULL | VNMC_FOC;
254 break;
255 case V4L2_FIELD_NONE:
256 if (is_continuous_transfer(priv)) {
257 vnmc = VNMC_IM_ODD_EVEN;
258 progressive = true;
259 } else {
260 vnmc = VNMC_IM_ODD;
262 break;
263 default:
264 vnmc = VNMC_IM_ODD;
265 break;
268 /* input interface */
269 switch (icd->current_fmt->code) {
270 case V4L2_MBUS_FMT_YUYV8_1X16:
271 /* BT.601/BT.1358 16bit YCbCr422 */
272 vnmc |= VNMC_INF_YUV16;
273 break;
274 case V4L2_MBUS_FMT_YUYV8_2X8:
275 /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
276 vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ?
277 VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
278 default:
279 break;
282 /* output format */
283 switch (icd->current_fmt->host_fmt->fourcc) {
284 case V4L2_PIX_FMT_NV16:
285 iowrite32(ALIGN(cam->width * cam->height, 0x80),
286 priv->base + VNUVAOF_REG);
287 dmr = VNDMR_DTMD_YCSEP;
288 output_is_yuv = true;
289 break;
290 case V4L2_PIX_FMT_YUYV:
291 dmr = VNDMR_BPSM;
292 output_is_yuv = true;
293 break;
294 case V4L2_PIX_FMT_UYVY:
295 dmr = 0;
296 output_is_yuv = true;
297 break;
298 case V4L2_PIX_FMT_RGB555X:
299 dmr = VNDMR_DTMD_ARGB1555;
300 break;
301 case V4L2_PIX_FMT_RGB565:
302 dmr = 0;
303 break;
304 case V4L2_PIX_FMT_RGB32:
305 if (priv->chip == RCAR_GEN2 || priv->chip == RCAR_H1 ||
306 priv->chip == RCAR_E1) {
307 dmr = VNDMR_EXRGB;
308 break;
310 default:
311 dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n",
312 icd->current_fmt->host_fmt->fourcc);
313 return -EINVAL;
316 /* Always update on field change */
317 vnmc |= VNMC_VUP;
319 /* If input and output use the same colorspace, use bypass mode */
320 if (output_is_yuv)
321 vnmc |= VNMC_BPS;
323 /* progressive or interlaced mode */
324 interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE;
326 /* ack interrupts */
327 iowrite32(interrupts, priv->base + VNINTS_REG);
328 /* enable interrupts */
329 iowrite32(interrupts, priv->base + VNIE_REG);
330 /* start capturing */
331 iowrite32(dmr, priv->base + VNDMR_REG);
332 iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG);
334 return 0;
337 static void rcar_vin_capture(struct rcar_vin_priv *priv)
339 if (is_continuous_transfer(priv))
340 /* Continuous Frame Capture Mode */
341 iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG);
342 else
343 /* Single Frame Capture Mode */
344 iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG);
347 static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv)
349 priv->state = STOPPING;
351 /* set continuous & single transfer off */
352 iowrite32(0, priv->base + VNFC_REG);
353 /* disable capture (release DMA buffer), reset */
354 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
355 priv->base + VNMC_REG);
357 /* update the status if stopped already */
358 if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA))
359 priv->state = STOPPED;
362 static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv)
364 int slot;
366 for (slot = 0; slot < priv->nr_hw_slots; slot++)
367 if (priv->queue_buf[slot] == NULL)
368 return slot;
370 return -1;
373 static int rcar_vin_hw_ready(struct rcar_vin_priv *priv)
375 /* Ensure all HW slots are filled */
376 return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0;
379 /* Moves a buffer from the queue to the HW slots */
380 static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv)
382 struct vb2_buffer *vb;
383 dma_addr_t phys_addr_top;
384 int slot;
386 if (list_empty(&priv->capture))
387 return 0;
389 /* Find a free HW slot */
390 slot = rcar_vin_get_free_hw_slot(priv);
391 if (slot < 0)
392 return 0;
394 vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb;
395 list_del_init(to_buf_list(vb));
396 priv->queue_buf[slot] = vb;
397 phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0);
398 iowrite32(phys_addr_top, priv->base + VNMB_REG(slot));
400 return 1;
403 static void rcar_vin_videobuf_queue(struct vb2_buffer *vb)
405 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
406 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
407 struct rcar_vin_priv *priv = ici->priv;
408 unsigned long size;
410 size = icd->sizeimage;
412 if (vb2_plane_size(vb, 0) < size) {
413 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
414 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
415 goto error;
418 vb2_set_plane_payload(vb, 0, size);
420 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
421 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
423 spin_lock_irq(&priv->lock);
425 list_add_tail(to_buf_list(vb), &priv->capture);
426 rcar_vin_fill_hw_slot(priv);
428 /* If we weren't running, and have enough buffers, start capturing! */
429 if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) {
430 if (rcar_vin_setup(priv)) {
431 /* Submit error */
432 list_del_init(to_buf_list(vb));
433 spin_unlock_irq(&priv->lock);
434 goto error;
436 priv->request_to_stop = false;
437 init_completion(&priv->capture_stop);
438 priv->state = RUNNING;
439 rcar_vin_capture(priv);
442 spin_unlock_irq(&priv->lock);
444 return;
446 error:
447 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
450 static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
452 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
453 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
454 struct rcar_vin_priv *priv = ici->priv;
455 unsigned int i;
456 int buf_in_use = 0;
458 spin_lock_irq(&priv->lock);
460 /* Is the buffer in use by the VIN hardware? */
461 for (i = 0; i < MAX_BUFFER_NUM; i++) {
462 if (priv->queue_buf[i] == vb) {
463 buf_in_use = 1;
464 break;
468 if (buf_in_use) {
469 while (priv->state != STOPPED) {
471 /* issue stop if running */
472 if (priv->state == RUNNING)
473 rcar_vin_request_capture_stop(priv);
475 /* wait until capturing has been stopped */
476 if (priv->state == STOPPING) {
477 priv->request_to_stop = true;
478 spin_unlock_irq(&priv->lock);
479 wait_for_completion(&priv->capture_stop);
480 spin_lock_irq(&priv->lock);
484 * Capturing has now stopped. The buffer we have been asked
485 * to release could be any of the current buffers in use, so
486 * release all buffers that are in use by HW
488 for (i = 0; i < MAX_BUFFER_NUM; i++) {
489 if (priv->queue_buf[i]) {
490 vb2_buffer_done(priv->queue_buf[i],
491 VB2_BUF_STATE_ERROR);
492 priv->queue_buf[i] = NULL;
495 } else {
496 list_del_init(to_buf_list(vb));
499 spin_unlock_irq(&priv->lock);
502 static int rcar_vin_videobuf_init(struct vb2_buffer *vb)
504 INIT_LIST_HEAD(to_buf_list(vb));
505 return 0;
508 static int rcar_vin_stop_streaming(struct vb2_queue *vq)
510 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
511 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
512 struct rcar_vin_priv *priv = ici->priv;
513 struct list_head *buf_head, *tmp;
515 spin_lock_irq(&priv->lock);
516 list_for_each_safe(buf_head, tmp, &priv->capture)
517 list_del_init(buf_head);
518 spin_unlock_irq(&priv->lock);
520 return 0;
523 static struct vb2_ops rcar_vin_vb2_ops = {
524 .queue_setup = rcar_vin_videobuf_setup,
525 .buf_init = rcar_vin_videobuf_init,
526 .buf_cleanup = rcar_vin_videobuf_release,
527 .buf_queue = rcar_vin_videobuf_queue,
528 .stop_streaming = rcar_vin_stop_streaming,
529 .wait_prepare = soc_camera_unlock,
530 .wait_finish = soc_camera_lock,
533 static irqreturn_t rcar_vin_irq(int irq, void *data)
535 struct rcar_vin_priv *priv = data;
536 u32 int_status;
537 bool can_run = false, hw_stopped;
538 int slot;
539 unsigned int handled = 0;
541 spin_lock(&priv->lock);
543 int_status = ioread32(priv->base + VNINTS_REG);
544 if (!int_status)
545 goto done;
546 /* ack interrupts */
547 iowrite32(int_status, priv->base + VNINTS_REG);
548 handled = 1;
550 /* nothing to do if capture status is 'STOPPED' */
551 if (priv->state == STOPPED)
552 goto done;
554 hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA);
556 if (!priv->request_to_stop) {
557 if (is_continuous_transfer(priv))
558 slot = (ioread32(priv->base + VNMS_REG) &
559 VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
560 else
561 slot = 0;
563 priv->queue_buf[slot]->v4l2_buf.field = priv->field;
564 priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++;
565 do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp);
566 vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE);
567 priv->queue_buf[slot] = NULL;
569 if (priv->state != STOPPING)
570 can_run = rcar_vin_fill_hw_slot(priv);
572 if (hw_stopped || !can_run) {
573 priv->state = STOPPED;
574 } else if (is_continuous_transfer(priv) &&
575 list_empty(&priv->capture) &&
576 priv->state == RUNNING) {
578 * The continuous capturing requires an explicit stop
579 * operation when there is no buffer to be set into
580 * the VnMBm registers.
582 rcar_vin_request_capture_stop(priv);
583 } else {
584 rcar_vin_capture(priv);
587 } else if (hw_stopped) {
588 priv->state = STOPPED;
589 priv->request_to_stop = false;
590 complete(&priv->capture_stop);
593 done:
594 spin_unlock(&priv->lock);
596 return IRQ_RETVAL(handled);
599 static int rcar_vin_add_device(struct soc_camera_device *icd)
601 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
602 struct rcar_vin_priv *priv = ici->priv;
603 int i;
605 for (i = 0; i < MAX_BUFFER_NUM; i++)
606 priv->queue_buf[i] = NULL;
608 pm_runtime_get_sync(ici->v4l2_dev.dev);
610 dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n",
611 icd->devnum);
613 return 0;
616 static void rcar_vin_remove_device(struct soc_camera_device *icd)
618 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
619 struct rcar_vin_priv *priv = ici->priv;
620 struct vb2_buffer *vb;
621 int i;
623 /* disable capture, disable interrupts */
624 iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
625 priv->base + VNMC_REG);
626 iowrite32(0, priv->base + VNIE_REG);
628 priv->state = STOPPED;
629 priv->request_to_stop = false;
631 /* make sure active buffer is cancelled */
632 spin_lock_irq(&priv->lock);
633 for (i = 0; i < MAX_BUFFER_NUM; i++) {
634 vb = priv->queue_buf[i];
635 if (vb) {
636 list_del_init(to_buf_list(vb));
637 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
640 spin_unlock_irq(&priv->lock);
642 pm_runtime_put(ici->v4l2_dev.dev);
644 dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n",
645 icd->devnum);
648 /* Called with .host_lock held */
649 static int rcar_vin_clock_start(struct soc_camera_host *ici)
651 /* VIN does not have "mclk" */
652 return 0;
655 /* Called with .host_lock held */
656 static void rcar_vin_clock_stop(struct soc_camera_host *ici)
658 /* VIN does not have "mclk" */
661 /* rect is guaranteed to not exceed the scaled camera rectangle */
662 static int rcar_vin_set_rect(struct soc_camera_device *icd)
664 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
665 struct rcar_vin_cam *cam = icd->host_priv;
666 struct rcar_vin_priv *priv = ici->priv;
667 unsigned int left_offset, top_offset;
668 unsigned char dsize = 0;
669 struct v4l2_rect *cam_subrect = &cam->subrect;
671 dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n",
672 icd->user_width, icd->user_height, cam->vin_left, cam->vin_top);
674 left_offset = cam->vin_left;
675 top_offset = cam->vin_top;
677 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 &&
678 priv->chip == RCAR_E1)
679 dsize = 1;
681 dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n",
682 cam->width, cam->height, cam->vin_left, cam->vin_top);
683 dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n",
684 cam_subrect->width, cam_subrect->height,
685 cam_subrect->left, cam_subrect->top);
687 /* Set Start/End Pixel/Line Pre-Clip */
688 iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG);
689 iowrite32((left_offset + cam->width - 1) << dsize,
690 priv->base + VNEPPRC_REG);
691 switch (priv->field) {
692 case V4L2_FIELD_INTERLACED:
693 case V4L2_FIELD_INTERLACED_TB:
694 case V4L2_FIELD_INTERLACED_BT:
695 iowrite32(top_offset / 2, priv->base + VNSLPRC_REG);
696 iowrite32((top_offset + cam->height) / 2 - 1,
697 priv->base + VNELPRC_REG);
698 break;
699 default:
700 iowrite32(top_offset, priv->base + VNSLPRC_REG);
701 iowrite32(top_offset + cam->height - 1,
702 priv->base + VNELPRC_REG);
703 break;
706 /* Set Start/End Pixel/Line Post-Clip */
707 iowrite32(0, priv->base + VNSPPOC_REG);
708 iowrite32(0, priv->base + VNSLPOC_REG);
709 iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG);
710 switch (priv->field) {
711 case V4L2_FIELD_INTERLACED:
712 case V4L2_FIELD_INTERLACED_TB:
713 case V4L2_FIELD_INTERLACED_BT:
714 iowrite32(cam_subrect->height / 2 - 1,
715 priv->base + VNELPOC_REG);
716 break;
717 default:
718 iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG);
719 break;
722 iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG);
724 return 0;
727 static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc)
729 *vnmc = ioread32(priv->base + VNMC_REG);
730 /* module disable */
731 iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG);
734 static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc)
736 unsigned long timeout = jiffies + 10 * HZ;
739 * Wait until the end of the current frame. It can take a long time,
740 * but if it has been aborted by a MRST1 reset, it should exit sooner.
742 while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) &&
743 time_before(jiffies, timeout))
744 msleep(1);
746 if (time_after(jiffies, timeout)) {
747 dev_err(priv->ici.v4l2_dev.dev,
748 "Timeout waiting for frame end! Interface problem?\n");
749 return;
752 iowrite32(vnmc, priv->base + VNMC_REG);
755 #define VIN_MBUS_FLAGS (V4L2_MBUS_MASTER | \
756 V4L2_MBUS_PCLK_SAMPLE_RISING | \
757 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
758 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
759 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
760 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
761 V4L2_MBUS_DATA_ACTIVE_HIGH)
763 static int rcar_vin_set_bus_param(struct soc_camera_device *icd)
765 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
766 struct rcar_vin_priv *priv = ici->priv;
767 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
768 struct v4l2_mbus_config cfg;
769 unsigned long common_flags;
770 u32 vnmc;
771 u32 val;
772 int ret;
774 capture_stop_preserve(priv, &vnmc);
776 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
777 if (!ret) {
778 common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
779 if (!common_flags) {
780 dev_warn(icd->parent,
781 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
782 cfg.flags, VIN_MBUS_FLAGS);
783 return -EINVAL;
785 } else if (ret != -ENOIOCTLCMD) {
786 return ret;
787 } else {
788 common_flags = VIN_MBUS_FLAGS;
791 /* Make choises, based on platform preferences */
792 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
793 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
794 if (priv->pdata->flags & RCAR_VIN_HSYNC_ACTIVE_LOW)
795 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
796 else
797 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
800 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
801 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
802 if (priv->pdata->flags & RCAR_VIN_VSYNC_ACTIVE_LOW)
803 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
804 else
805 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
808 cfg.flags = common_flags;
809 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
810 if (ret < 0 && ret != -ENOIOCTLCMD)
811 return ret;
813 val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0;
814 if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
815 val |= VNDMR2_VPS;
816 if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
817 val |= VNDMR2_HPS;
818 iowrite32(val, priv->base + VNDMR2_REG);
820 ret = rcar_vin_set_rect(icd);
821 if (ret < 0)
822 return ret;
824 capture_restore(priv, vnmc);
826 return 0;
829 static int rcar_vin_try_bus_param(struct soc_camera_device *icd,
830 unsigned char buswidth)
832 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
833 struct v4l2_mbus_config cfg;
834 int ret;
836 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
837 if (ret == -ENOIOCTLCMD)
838 return 0;
839 else if (ret)
840 return ret;
842 if (buswidth > 24)
843 return -EINVAL;
845 /* check is there common mbus flags */
846 ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
847 if (ret)
848 return 0;
850 dev_warn(icd->parent,
851 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
852 cfg.flags, VIN_MBUS_FLAGS);
854 return -EINVAL;
857 static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt)
859 return fmt->packing == SOC_MBUS_PACKING_NONE ||
860 (fmt->bits_per_sample > 8 &&
861 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
864 static const struct soc_mbus_pixelfmt rcar_vin_formats[] = {
866 .fourcc = V4L2_PIX_FMT_NV16,
867 .name = "NV16",
868 .bits_per_sample = 8,
869 .packing = SOC_MBUS_PACKING_2X8_PADHI,
870 .order = SOC_MBUS_ORDER_LE,
871 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
874 .fourcc = V4L2_PIX_FMT_UYVY,
875 .name = "UYVY",
876 .bits_per_sample = 16,
877 .packing = SOC_MBUS_PACKING_NONE,
878 .order = SOC_MBUS_ORDER_LE,
879 .layout = SOC_MBUS_LAYOUT_PACKED,
882 .fourcc = V4L2_PIX_FMT_RGB565,
883 .name = "RGB565",
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_RGB555X,
891 .name = "ARGB1555",
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_RGB32,
899 .name = "RGB888",
900 .bits_per_sample = 32,
901 .packing = SOC_MBUS_PACKING_NONE,
902 .order = SOC_MBUS_ORDER_LE,
903 .layout = SOC_MBUS_LAYOUT_PACKED,
907 static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx,
908 struct soc_camera_format_xlate *xlate)
910 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
911 struct device *dev = icd->parent;
912 int ret, k, n;
913 int formats = 0;
914 struct rcar_vin_cam *cam;
915 enum v4l2_mbus_pixelcode code;
916 const struct soc_mbus_pixelfmt *fmt;
918 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
919 if (ret < 0)
920 return 0;
922 fmt = soc_mbus_get_fmtdesc(code);
923 if (!fmt) {
924 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
925 return 0;
928 ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample);
929 if (ret < 0)
930 return 0;
932 if (!icd->host_priv) {
933 struct v4l2_mbus_framefmt mf;
934 struct v4l2_rect rect;
935 struct device *dev = icd->parent;
936 int shift;
938 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
939 if (ret < 0)
940 return ret;
942 /* Cache current client geometry */
943 ret = soc_camera_client_g_rect(sd, &rect);
944 if (ret == -ENOIOCTLCMD) {
945 /* Sensor driver doesn't support cropping */
946 rect.left = 0;
947 rect.top = 0;
948 rect.width = mf.width;
949 rect.height = mf.height;
950 } else if (ret < 0) {
951 return ret;
955 * If sensor proposes too large format then try smaller ones:
956 * 1280x960, 640x480, 320x240
958 for (shift = 0; shift < 3; shift++) {
959 if (mf.width <= VIN_MAX_WIDTH &&
960 mf.height <= VIN_MAX_HEIGHT)
961 break;
963 mf.width = 1280 >> shift;
964 mf.height = 960 >> shift;
965 ret = v4l2_device_call_until_err(sd->v4l2_dev,
966 soc_camera_grp_id(icd),
967 video, s_mbus_fmt,
968 &mf);
969 if (ret < 0)
970 return ret;
973 if (shift == 3) {
974 dev_err(dev,
975 "Failed to configure the client below %ux%x\n",
976 mf.width, mf.height);
977 return -EIO;
980 dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height);
982 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
983 if (!cam)
984 return -ENOMEM;
986 * We are called with current camera crop,
987 * initialise subrect with it
989 cam->rect = rect;
990 cam->subrect = rect;
991 cam->width = mf.width;
992 cam->height = mf.height;
994 icd->host_priv = cam;
995 } else {
996 cam = icd->host_priv;
999 /* Beginning of a pass */
1000 if (!idx)
1001 cam->extra_fmt = NULL;
1003 switch (code) {
1004 case V4L2_MBUS_FMT_YUYV8_1X16:
1005 case V4L2_MBUS_FMT_YUYV8_2X8:
1006 if (cam->extra_fmt)
1007 break;
1009 /* Add all our formats that can be generated by VIN */
1010 cam->extra_fmt = rcar_vin_formats;
1012 n = ARRAY_SIZE(rcar_vin_formats);
1013 formats += n;
1014 for (k = 0; xlate && k < n; k++, xlate++) {
1015 xlate->host_fmt = &rcar_vin_formats[k];
1016 xlate->code = code;
1017 dev_dbg(dev, "Providing format %s using code %d\n",
1018 rcar_vin_formats[k].name, code);
1020 break;
1021 default:
1022 if (!rcar_vin_packing_supported(fmt))
1023 return 0;
1025 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1026 fmt->name);
1027 break;
1030 /* Generic pass-through */
1031 formats++;
1032 if (xlate) {
1033 xlate->host_fmt = fmt;
1034 xlate->code = code;
1035 xlate++;
1038 return formats;
1041 static void rcar_vin_put_formats(struct soc_camera_device *icd)
1043 kfree(icd->host_priv);
1044 icd->host_priv = NULL;
1047 static int rcar_vin_set_crop(struct soc_camera_device *icd,
1048 const struct v4l2_crop *a)
1050 struct v4l2_crop a_writable = *a;
1051 const struct v4l2_rect *rect = &a_writable.c;
1052 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1053 struct rcar_vin_priv *priv = ici->priv;
1054 struct v4l2_crop cam_crop;
1055 struct rcar_vin_cam *cam = icd->host_priv;
1056 struct v4l2_rect *cam_rect = &cam_crop.c;
1057 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1058 struct device *dev = icd->parent;
1059 struct v4l2_mbus_framefmt mf;
1060 u32 vnmc;
1061 int ret, i;
1063 dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1064 rect->left, rect->top);
1066 /* During camera cropping its output window can change too, stop VIN */
1067 capture_stop_preserve(priv, &vnmc);
1068 dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc);
1070 /* Apply iterative camera S_CROP for new input window. */
1071 ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
1072 &cam->rect, &cam->subrect);
1073 if (ret < 0)
1074 return ret;
1076 dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n",
1077 cam_rect->width, cam_rect->height,
1078 cam_rect->left, cam_rect->top);
1080 /* On success cam_crop contains current camera crop */
1082 /* Retrieve camera output window */
1083 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1084 if (ret < 0)
1085 return ret;
1087 if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT)
1088 return -EINVAL;
1090 /* Cache camera output window */
1091 cam->width = mf.width;
1092 cam->height = mf.height;
1094 icd->user_width = cam->width;
1095 icd->user_height = cam->height;
1097 cam->vin_left = rect->left & ~1;
1098 cam->vin_top = rect->top & ~1;
1100 /* Use VIN cropping to crop to the new window. */
1101 ret = rcar_vin_set_rect(icd);
1102 if (ret < 0)
1103 return ret;
1105 cam->subrect = *rect;
1107 dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n",
1108 icd->user_width, icd->user_height,
1109 cam->vin_left, cam->vin_top);
1111 /* Restore capture */
1112 for (i = 0; i < MAX_BUFFER_NUM; i++) {
1113 if (priv->queue_buf[i] && priv->state == STOPPED) {
1114 vnmc |= VNMC_ME;
1115 break;
1118 capture_restore(priv, vnmc);
1120 /* Even if only camera cropping succeeded */
1121 return ret;
1124 static int rcar_vin_get_crop(struct soc_camera_device *icd,
1125 struct v4l2_crop *a)
1127 struct rcar_vin_cam *cam = icd->host_priv;
1129 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1130 a->c = cam->subrect;
1132 return 0;
1135 /* Similar to set_crop multistage iterative algorithm */
1136 static int rcar_vin_set_fmt(struct soc_camera_device *icd,
1137 struct v4l2_format *f)
1139 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1140 struct rcar_vin_priv *priv = ici->priv;
1141 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1142 struct rcar_vin_cam *cam = icd->host_priv;
1143 struct v4l2_pix_format *pix = &f->fmt.pix;
1144 struct v4l2_mbus_framefmt mf;
1145 struct device *dev = icd->parent;
1146 __u32 pixfmt = pix->pixelformat;
1147 const struct soc_camera_format_xlate *xlate;
1148 unsigned int vin_sub_width = 0, vin_sub_height = 0;
1149 int ret;
1150 bool can_scale;
1151 enum v4l2_field field;
1152 v4l2_std_id std;
1154 dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n",
1155 pixfmt, pix->width, pix->height);
1157 switch (pix->field) {
1158 default:
1159 pix->field = V4L2_FIELD_NONE;
1160 /* fall-through */
1161 case V4L2_FIELD_NONE:
1162 case V4L2_FIELD_TOP:
1163 case V4L2_FIELD_BOTTOM:
1164 case V4L2_FIELD_INTERLACED_TB:
1165 case V4L2_FIELD_INTERLACED_BT:
1166 field = pix->field;
1167 break;
1168 case V4L2_FIELD_INTERLACED:
1169 /* Query for standard if not explicitly mentioned _TB/_BT */
1170 ret = v4l2_subdev_call(sd, video, querystd, &std);
1171 if (ret < 0)
1172 std = V4L2_STD_625_50;
1174 field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB :
1175 V4L2_FIELD_INTERLACED_BT;
1176 break;
1179 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1180 if (!xlate) {
1181 dev_warn(dev, "Format %x not found\n", pixfmt);
1182 return -EINVAL;
1184 /* Calculate client output geometry */
1185 soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf,
1186 12);
1187 mf.field = pix->field;
1188 mf.colorspace = pix->colorspace;
1189 mf.code = xlate->code;
1191 switch (pixfmt) {
1192 case V4L2_PIX_FMT_RGB32:
1193 can_scale = priv->chip != RCAR_E1;
1194 break;
1195 case V4L2_PIX_FMT_UYVY:
1196 case V4L2_PIX_FMT_YUYV:
1197 case V4L2_PIX_FMT_RGB565:
1198 case V4L2_PIX_FMT_RGB555X:
1199 can_scale = true;
1200 break;
1201 default:
1202 can_scale = false;
1203 break;
1206 dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height);
1208 ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1209 &mf, &vin_sub_width, &vin_sub_height,
1210 can_scale, 12);
1212 /* Done with the camera. Now see if we can improve the result */
1213 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1214 ret, mf.width, mf.height, pix->width, pix->height);
1216 if (ret == -ENOIOCTLCMD)
1217 dev_dbg(dev, "Sensor doesn't support scaling\n");
1218 else if (ret < 0)
1219 return ret;
1221 if (mf.code != xlate->code)
1222 return -EINVAL;
1224 /* Prepare VIN crop */
1225 cam->width = mf.width;
1226 cam->height = mf.height;
1228 /* Use VIN scaling to scale to the requested user window. */
1230 /* We cannot scale up */
1231 if (pix->width > vin_sub_width)
1232 vin_sub_width = pix->width;
1234 if (pix->height > vin_sub_height)
1235 vin_sub_height = pix->height;
1237 pix->colorspace = mf.colorspace;
1239 if (!can_scale) {
1240 pix->width = vin_sub_width;
1241 pix->height = vin_sub_height;
1245 * We have calculated CFLCR, the actual configuration will be performed
1246 * in rcar_vin_set_bus_param()
1249 dev_dbg(dev, "W: %u : %u, H: %u : %u\n",
1250 vin_sub_width, pix->width, vin_sub_height, pix->height);
1252 icd->current_fmt = xlate;
1254 priv->field = field;
1256 return 0;
1259 static int rcar_vin_try_fmt(struct soc_camera_device *icd,
1260 struct v4l2_format *f)
1262 const struct soc_camera_format_xlate *xlate;
1263 struct v4l2_pix_format *pix = &f->fmt.pix;
1264 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1265 struct v4l2_mbus_framefmt mf;
1266 __u32 pixfmt = pix->pixelformat;
1267 int width, height;
1268 int ret;
1270 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1271 if (!xlate) {
1272 xlate = icd->current_fmt;
1273 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1274 pixfmt, xlate->host_fmt->fourcc);
1275 pixfmt = xlate->host_fmt->fourcc;
1276 pix->pixelformat = pixfmt;
1277 pix->colorspace = icd->colorspace;
1280 /* FIXME: calculate using depth and bus width */
1281 v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1,
1282 &pix->height, 4, VIN_MAX_HEIGHT, 2, 0);
1284 width = pix->width;
1285 height = pix->height;
1287 /* let soc-camera calculate these values */
1288 pix->bytesperline = 0;
1289 pix->sizeimage = 0;
1291 /* limit to sensor capabilities */
1292 mf.width = pix->width;
1293 mf.height = pix->height;
1294 mf.field = pix->field;
1295 mf.code = xlate->code;
1296 mf.colorspace = pix->colorspace;
1298 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1299 video, try_mbus_fmt, &mf);
1300 if (ret < 0)
1301 return ret;
1303 pix->width = mf.width;
1304 pix->height = mf.height;
1305 pix->field = mf.field;
1306 pix->colorspace = mf.colorspace;
1308 if (pixfmt == V4L2_PIX_FMT_NV16) {
1309 /* FIXME: check against rect_max after converting soc-camera */
1310 /* We can scale precisely, need a bigger image from camera */
1311 if (pix->width < width || pix->height < height) {
1313 * We presume, the sensor behaves sanely, i.e. if
1314 * requested a bigger rectangle, it will not return a
1315 * smaller one.
1317 mf.width = VIN_MAX_WIDTH;
1318 mf.height = VIN_MAX_HEIGHT;
1319 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1320 soc_camera_grp_id(icd),
1321 video, try_mbus_fmt,
1322 &mf);
1323 if (ret < 0) {
1324 dev_err(icd->parent,
1325 "client try_fmt() = %d\n", ret);
1326 return ret;
1329 /* We will scale exactly */
1330 if (mf.width > width)
1331 pix->width = width;
1332 if (mf.height > height)
1333 pix->height = height;
1336 return ret;
1339 static unsigned int rcar_vin_poll(struct file *file, poll_table *pt)
1341 struct soc_camera_device *icd = file->private_data;
1343 return vb2_poll(&icd->vb2_vidq, file, pt);
1346 static int rcar_vin_querycap(struct soc_camera_host *ici,
1347 struct v4l2_capability *cap)
1349 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
1350 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1351 return 0;
1354 static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
1355 struct soc_camera_device *icd)
1357 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1358 vq->io_modes = VB2_MMAP | VB2_USERPTR;
1359 vq->drv_priv = icd;
1360 vq->ops = &rcar_vin_vb2_ops;
1361 vq->mem_ops = &vb2_dma_contig_memops;
1362 vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
1363 vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1365 return vb2_queue_init(vq);
1368 static struct soc_camera_host_ops rcar_vin_host_ops = {
1369 .owner = THIS_MODULE,
1370 .add = rcar_vin_add_device,
1371 .remove = rcar_vin_remove_device,
1372 .clock_start = rcar_vin_clock_start,
1373 .clock_stop = rcar_vin_clock_stop,
1374 .get_formats = rcar_vin_get_formats,
1375 .put_formats = rcar_vin_put_formats,
1376 .get_crop = rcar_vin_get_crop,
1377 .set_crop = rcar_vin_set_crop,
1378 .try_fmt = rcar_vin_try_fmt,
1379 .set_fmt = rcar_vin_set_fmt,
1380 .poll = rcar_vin_poll,
1381 .querycap = rcar_vin_querycap,
1382 .set_bus_param = rcar_vin_set_bus_param,
1383 .init_videobuf2 = rcar_vin_init_videobuf2,
1386 static struct platform_device_id rcar_vin_id_table[] = {
1387 { "r8a7791-vin", RCAR_GEN2 },
1388 { "r8a7790-vin", RCAR_GEN2 },
1389 { "r8a7779-vin", RCAR_H1 },
1390 { "r8a7778-vin", RCAR_M1 },
1391 { "uPD35004-vin", RCAR_E1 },
1394 MODULE_DEVICE_TABLE(platform, rcar_vin_id_table);
1396 static int rcar_vin_probe(struct platform_device *pdev)
1398 struct rcar_vin_priv *priv;
1399 struct resource *mem;
1400 struct rcar_vin_platform_data *pdata;
1401 int irq, ret;
1403 pdata = pdev->dev.platform_data;
1404 if (!pdata || !pdata->flags) {
1405 dev_err(&pdev->dev, "platform data not set\n");
1406 return -EINVAL;
1409 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1410 if (mem == NULL)
1411 return -EINVAL;
1413 irq = platform_get_irq(pdev, 0);
1414 if (irq <= 0)
1415 return -EINVAL;
1417 priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv),
1418 GFP_KERNEL);
1419 if (!priv)
1420 return -ENOMEM;
1422 priv->base = devm_ioremap_resource(&pdev->dev, mem);
1423 if (IS_ERR(priv->base))
1424 return PTR_ERR(priv->base);
1426 ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED,
1427 dev_name(&pdev->dev), priv);
1428 if (ret)
1429 return ret;
1431 priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1432 if (IS_ERR(priv->alloc_ctx))
1433 return PTR_ERR(priv->alloc_ctx);
1435 priv->ici.priv = priv;
1436 priv->ici.v4l2_dev.dev = &pdev->dev;
1437 priv->ici.nr = pdev->id;
1438 priv->ici.drv_name = dev_name(&pdev->dev);
1439 priv->ici.ops = &rcar_vin_host_ops;
1441 priv->pdata = pdata;
1442 priv->chip = pdev->id_entry->driver_data;
1443 spin_lock_init(&priv->lock);
1444 INIT_LIST_HEAD(&priv->capture);
1446 priv->state = STOPPED;
1448 pm_suspend_ignore_children(&pdev->dev, true);
1449 pm_runtime_enable(&pdev->dev);
1451 ret = soc_camera_host_register(&priv->ici);
1452 if (ret)
1453 goto cleanup;
1455 return 0;
1457 cleanup:
1458 pm_runtime_disable(&pdev->dev);
1459 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1461 return ret;
1464 static int rcar_vin_remove(struct platform_device *pdev)
1466 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1467 struct rcar_vin_priv *priv = container_of(soc_host,
1468 struct rcar_vin_priv, ici);
1470 soc_camera_host_unregister(soc_host);
1471 pm_runtime_disable(&pdev->dev);
1472 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1474 return 0;
1477 static struct platform_driver rcar_vin_driver = {
1478 .probe = rcar_vin_probe,
1479 .remove = rcar_vin_remove,
1480 .driver = {
1481 .name = DRV_NAME,
1482 .owner = THIS_MODULE,
1484 .id_table = rcar_vin_id_table,
1487 module_platform_driver(rcar_vin_driver);
1489 MODULE_LICENSE("GPL");
1490 MODULE_ALIAS("platform:rcar_vin");
1491 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");