Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / platform / soc_camera / rcar_vin.c
1 /*
2  * SoC-camera host driver for Renesas R-Car VIN unit
3  *
4  * Copyright (C) 2011-2013 Renesas Solutions Corp.
5  * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
6  *
7  * Based on V4L2 Driver for SuperH Mobile CEU interface "sh_mobile_ceu_camera.c"
8  *
9  * Copyright (C) 2008 Magnus Damm
10  *
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.
15  */
16
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/of.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_data/camera-rcar.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29
30 #include <media/soc_camera.h>
31 #include <media/soc_mediabus.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-mediabus.h>
36 #include <media/v4l2-of.h>
37 #include <media/v4l2-subdev.h>
38 #include <media/videobuf2-dma-contig.h>
39
40 #include "soc_scale_crop.h"
41
42 #define DRV_NAME "rcar_vin"
43
44 /* Register offsets for R-Car VIN */
45 #define VNMC_REG        0x00    /* Video n Main Control Register */
46 #define VNMS_REG        0x04    /* Video n Module Status Register */
47 #define VNFC_REG        0x08    /* Video n Frame Capture Register */
48 #define VNSLPRC_REG     0x0C    /* Video n Start Line Pre-Clip Register */
49 #define VNELPRC_REG     0x10    /* Video n End Line Pre-Clip Register */
50 #define VNSPPRC_REG     0x14    /* Video n Start Pixel Pre-Clip Register */
51 #define VNEPPRC_REG     0x18    /* Video n End Pixel Pre-Clip Register */
52 #define VNSLPOC_REG     0x1C    /* Video n Start Line Post-Clip Register */
53 #define VNELPOC_REG     0x20    /* Video n End Line Post-Clip Register */
54 #define VNSPPOC_REG     0x24    /* Video n Start Pixel Post-Clip Register */
55 #define VNEPPOC_REG     0x28    /* Video n End Pixel Post-Clip Register */
56 #define VNIS_REG        0x2C    /* Video n Image Stride Register */
57 #define VNMB_REG(m)     (0x30 + ((m) << 2)) /* Video n Memory Base m Register */
58 #define VNIE_REG        0x40    /* Video n Interrupt Enable Register */
59 #define VNINTS_REG      0x44    /* Video n Interrupt Status Register */
60 #define VNSI_REG        0x48    /* Video n Scanline Interrupt Register */
61 #define VNMTC_REG       0x4C    /* Video n Memory Transfer Control Register */
62 #define VNYS_REG        0x50    /* Video n Y Scale Register */
63 #define VNXS_REG        0x54    /* Video n X Scale Register */
64 #define VNDMR_REG       0x58    /* Video n Data Mode Register */
65 #define VNDMR2_REG      0x5C    /* Video n Data Mode Register 2 */
66 #define VNUVAOF_REG     0x60    /* Video n UV Address Offset Register */
67
68 /* Register bit fields for R-Car VIN */
69 /* Video n Main Control Register bits */
70 #define VNMC_FOC                (1 << 21)
71 #define VNMC_YCAL               (1 << 19)
72 #define VNMC_INF_YUV8_BT656     (0 << 16)
73 #define VNMC_INF_YUV8_BT601     (1 << 16)
74 #define VNMC_INF_YUV10_BT656    (2 << 16)
75 #define VNMC_INF_YUV10_BT601    (3 << 16)
76 #define VNMC_INF_YUV16          (5 << 16)
77 #define VNMC_VUP                (1 << 10)
78 #define VNMC_IM_ODD             (0 << 3)
79 #define VNMC_IM_ODD_EVEN        (1 << 3)
80 #define VNMC_IM_EVEN            (2 << 3)
81 #define VNMC_IM_FULL            (3 << 3)
82 #define VNMC_BPS                (1 << 1)
83 #define VNMC_ME                 (1 << 0)
84
85 /* Video n Module Status Register bits */
86 #define VNMS_FBS_MASK           (3 << 3)
87 #define VNMS_FBS_SHIFT          3
88 #define VNMS_AV                 (1 << 1)
89 #define VNMS_CA                 (1 << 0)
90
91 /* Video n Frame Capture Register bits */
92 #define VNFC_C_FRAME            (1 << 1)
93 #define VNFC_S_FRAME            (1 << 0)
94
95 /* Video n Interrupt Enable Register bits */
96 #define VNIE_FIE                (1 << 4)
97 #define VNIE_EFE                (1 << 1)
98
99 /* Video n Data Mode Register bits */
100 #define VNDMR_EXRGB             (1 << 8)
101 #define VNDMR_BPSM              (1 << 4)
102 #define VNDMR_DTMD_YCSEP        (1 << 1)
103 #define VNDMR_DTMD_ARGB1555     (1 << 0)
104
105 /* Video n Data Mode Register 2 bits */
106 #define VNDMR2_VPS              (1 << 30)
107 #define VNDMR2_HPS              (1 << 29)
108 #define VNDMR2_FTEV             (1 << 17)
109
110 #define VIN_MAX_WIDTH           2048
111 #define VIN_MAX_HEIGHT          2048
112
113 enum chip_id {
114         RCAR_GEN2,
115         RCAR_H1,
116         RCAR_M1,
117         RCAR_E1,
118 };
119
120 enum rcar_vin_state {
121         STOPPED = 0,
122         RUNNING,
123         STOPPING,
124 };
125
126 struct rcar_vin_priv {
127         void __iomem                    *base;
128         spinlock_t                      lock;
129         int                             sequence;
130         /* State of the VIN module in capturing mode */
131         enum rcar_vin_state             state;
132         struct soc_camera_host          ici;
133         struct list_head                capture;
134 #define MAX_BUFFER_NUM                  3
135         struct vb2_buffer               *queue_buf[MAX_BUFFER_NUM];
136         struct vb2_alloc_ctx            *alloc_ctx;
137         enum v4l2_field                 field;
138         unsigned int                    pdata_flags;
139         unsigned int                    vb_count;
140         unsigned int                    nr_hw_slots;
141         bool                            request_to_stop;
142         struct completion               capture_stop;
143         enum chip_id                    chip;
144 };
145
146 #define is_continuous_transfer(priv)    (priv->vb_count > MAX_BUFFER_NUM)
147
148 struct rcar_vin_buffer {
149         struct vb2_buffer               vb;
150         struct list_head                list;
151 };
152
153 #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
154                                                        struct rcar_vin_buffer, \
155                                                        vb)->list)
156
157 struct rcar_vin_cam {
158         /* VIN offsets within the camera output, before the VIN scaler */
159         unsigned int                    vin_left;
160         unsigned int                    vin_top;
161         /* Client output, as seen by the VIN */
162         unsigned int                    width;
163         unsigned int                    height;
164         /*
165          * User window from S_CROP / G_CROP, produced by client cropping and
166          * scaling, VIN scaling and VIN cropping, mapped back onto the client
167          * input window
168          */
169         struct v4l2_rect                subrect;
170         /* Camera cropping rectangle */
171         struct v4l2_rect                rect;
172         const struct soc_mbus_pixelfmt  *extra_fmt;
173 };
174
175 /*
176  * .queue_setup() is called to check whether the driver can accept the requested
177  * number of buffers and to fill in plane sizes for the current frame format if
178  * required
179  */
180 static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
181                                    const struct v4l2_format *fmt,
182                                    unsigned int *count,
183                                    unsigned int *num_planes,
184                                    unsigned int sizes[], void *alloc_ctxs[])
185 {
186         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
187         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
188         struct rcar_vin_priv *priv = ici->priv;
189
190         if (fmt) {
191                 const struct soc_camera_format_xlate *xlate;
192                 unsigned int bytes_per_line;
193                 int ret;
194
195                 xlate = soc_camera_xlate_by_fourcc(icd,
196                                                    fmt->fmt.pix.pixelformat);
197                 if (!xlate)
198                         return -EINVAL;
199                 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
200                                               xlate->host_fmt);
201                 if (ret < 0)
202                         return ret;
203
204                 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
205
206                 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
207                                           fmt->fmt.pix.height);
208                 if (ret < 0)
209                         return ret;
210
211                 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
212         } else {
213                 /* Called from VIDIOC_REQBUFS or in compatibility mode */
214                 sizes[0] = icd->sizeimage;
215         }
216
217         alloc_ctxs[0] = priv->alloc_ctx;
218
219         if (!vq->num_buffers)
220                 priv->sequence = 0;
221
222         if (!*count)
223                 *count = 2;
224         priv->vb_count = *count;
225
226         *num_planes = 1;
227
228         /* Number of hardware slots */
229         if (is_continuous_transfer(priv))
230                 priv->nr_hw_slots = MAX_BUFFER_NUM;
231         else
232                 priv->nr_hw_slots = 1;
233
234         dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
235
236         return 0;
237 }
238
239 static int rcar_vin_setup(struct rcar_vin_priv *priv)
240 {
241         struct soc_camera_device *icd = priv->ici.icd;
242         struct rcar_vin_cam *cam = icd->host_priv;
243         u32 vnmc, dmr, interrupts;
244         bool progressive = false, output_is_yuv = false;
245
246         switch (priv->field) {
247         case V4L2_FIELD_TOP:
248                 vnmc = VNMC_IM_ODD;
249                 break;
250         case V4L2_FIELD_BOTTOM:
251                 vnmc = VNMC_IM_EVEN;
252                 break;
253         case V4L2_FIELD_INTERLACED:
254         case V4L2_FIELD_INTERLACED_TB:
255                 vnmc = VNMC_IM_FULL;
256                 break;
257         case V4L2_FIELD_INTERLACED_BT:
258                 vnmc = VNMC_IM_FULL | VNMC_FOC;
259                 break;
260         case V4L2_FIELD_NONE:
261                 if (is_continuous_transfer(priv)) {
262                         vnmc = VNMC_IM_ODD_EVEN;
263                         progressive = true;
264                 } else {
265                         vnmc = VNMC_IM_ODD;
266                 }
267                 break;
268         default:
269                 vnmc = VNMC_IM_ODD;
270                 break;
271         }
272
273         /* input interface */
274         switch (icd->current_fmt->code) {
275         case V4L2_MBUS_FMT_YUYV8_1X16:
276                 /* BT.601/BT.1358 16bit YCbCr422 */
277                 vnmc |= VNMC_INF_YUV16;
278                 break;
279         case V4L2_MBUS_FMT_YUYV8_2X8:
280                 /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
281                 vnmc |= priv->pdata_flags & RCAR_VIN_BT656 ?
282                         VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
283                 break;
284         case V4L2_MBUS_FMT_YUYV10_2X10:
285                 /* BT.656 10bit YCbCr422 or BT.601 10bit YCbCr422 */
286                 vnmc |= priv->pdata_flags & RCAR_VIN_BT656 ?
287                         VNMC_INF_YUV10_BT656 : VNMC_INF_YUV10_BT601;
288                 break;
289         default:
290                 break;
291         }
292
293         /* output format */
294         switch (icd->current_fmt->host_fmt->fourcc) {
295         case V4L2_PIX_FMT_NV16:
296                 iowrite32(ALIGN(cam->width * cam->height, 0x80),
297                           priv->base + VNUVAOF_REG);
298                 dmr = VNDMR_DTMD_YCSEP;
299                 output_is_yuv = true;
300                 break;
301         case V4L2_PIX_FMT_YUYV:
302                 dmr = VNDMR_BPSM;
303                 output_is_yuv = true;
304                 break;
305         case V4L2_PIX_FMT_UYVY:
306                 dmr = 0;
307                 output_is_yuv = true;
308                 break;
309         case V4L2_PIX_FMT_RGB555X:
310                 dmr = VNDMR_DTMD_ARGB1555;
311                 break;
312         case V4L2_PIX_FMT_RGB565:
313                 dmr = 0;
314                 break;
315         case V4L2_PIX_FMT_RGB32:
316                 if (priv->chip == RCAR_GEN2 || priv->chip == RCAR_H1 ||
317                     priv->chip == RCAR_E1) {
318                         dmr = VNDMR_EXRGB;
319                         break;
320                 }
321         default:
322                 dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n",
323                          icd->current_fmt->host_fmt->fourcc);
324                 return -EINVAL;
325         }
326
327         /* Always update on field change */
328         vnmc |= VNMC_VUP;
329
330         /* If input and output use the same colorspace, use bypass mode */
331         if (output_is_yuv)
332                 vnmc |= VNMC_BPS;
333
334         /* progressive or interlaced mode */
335         interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE;
336
337         /* ack interrupts */
338         iowrite32(interrupts, priv->base + VNINTS_REG);
339         /* enable interrupts */
340         iowrite32(interrupts, priv->base + VNIE_REG);
341         /* start capturing */
342         iowrite32(dmr, priv->base + VNDMR_REG);
343         iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG);
344
345         return 0;
346 }
347
348 static void rcar_vin_capture(struct rcar_vin_priv *priv)
349 {
350         if (is_continuous_transfer(priv))
351                 /* Continuous Frame Capture Mode */
352                 iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG);
353         else
354                 /* Single Frame Capture Mode */
355                 iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG);
356 }
357
358 static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv)
359 {
360         priv->state = STOPPING;
361
362         /* set continuous & single transfer off */
363         iowrite32(0, priv->base + VNFC_REG);
364         /* disable capture (release DMA buffer), reset */
365         iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
366                   priv->base + VNMC_REG);
367
368         /* update the status if stopped already */
369         if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA))
370                 priv->state = STOPPED;
371 }
372
373 static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv)
374 {
375         int slot;
376
377         for (slot = 0; slot < priv->nr_hw_slots; slot++)
378                 if (priv->queue_buf[slot] == NULL)
379                         return slot;
380
381         return -1;
382 }
383
384 static int rcar_vin_hw_ready(struct rcar_vin_priv *priv)
385 {
386         /* Ensure all HW slots are filled */
387         return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0;
388 }
389
390 /* Moves a buffer from the queue to the HW slots */
391 static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv)
392 {
393         struct vb2_buffer *vb;
394         dma_addr_t phys_addr_top;
395         int slot;
396
397         if (list_empty(&priv->capture))
398                 return 0;
399
400         /* Find a free HW slot */
401         slot = rcar_vin_get_free_hw_slot(priv);
402         if (slot < 0)
403                 return 0;
404
405         vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb;
406         list_del_init(to_buf_list(vb));
407         priv->queue_buf[slot] = vb;
408         phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0);
409         iowrite32(phys_addr_top, priv->base + VNMB_REG(slot));
410
411         return 1;
412 }
413
414 static void rcar_vin_videobuf_queue(struct vb2_buffer *vb)
415 {
416         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
417         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
418         struct rcar_vin_priv *priv = ici->priv;
419         unsigned long size;
420
421         size = icd->sizeimage;
422
423         if (vb2_plane_size(vb, 0) < size) {
424                 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
425                         vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
426                 goto error;
427         }
428
429         vb2_set_plane_payload(vb, 0, size);
430
431         dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
432                 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
433
434         spin_lock_irq(&priv->lock);
435
436         list_add_tail(to_buf_list(vb), &priv->capture);
437         rcar_vin_fill_hw_slot(priv);
438
439         /* If we weren't running, and have enough buffers, start capturing! */
440         if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) {
441                 if (rcar_vin_setup(priv)) {
442                         /* Submit error */
443                         list_del_init(to_buf_list(vb));
444                         spin_unlock_irq(&priv->lock);
445                         goto error;
446                 }
447                 priv->request_to_stop = false;
448                 init_completion(&priv->capture_stop);
449                 priv->state = RUNNING;
450                 rcar_vin_capture(priv);
451         }
452
453         spin_unlock_irq(&priv->lock);
454
455         return;
456
457 error:
458         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
459 }
460
461 static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
462 {
463         struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
464         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
465         struct rcar_vin_priv *priv = ici->priv;
466         unsigned int i;
467         int buf_in_use = 0;
468
469         spin_lock_irq(&priv->lock);
470
471         /* Is the buffer in use by the VIN hardware? */
472         for (i = 0; i < MAX_BUFFER_NUM; i++) {
473                 if (priv->queue_buf[i] == vb) {
474                         buf_in_use = 1;
475                         break;
476                 }
477         }
478
479         if (buf_in_use) {
480                 while (priv->state != STOPPED) {
481
482                         /* issue stop if running */
483                         if (priv->state == RUNNING)
484                                 rcar_vin_request_capture_stop(priv);
485
486                         /* wait until capturing has been stopped */
487                         if (priv->state == STOPPING) {
488                                 priv->request_to_stop = true;
489                                 spin_unlock_irq(&priv->lock);
490                                 wait_for_completion(&priv->capture_stop);
491                                 spin_lock_irq(&priv->lock);
492                         }
493                 }
494                 /*
495                  * Capturing has now stopped. The buffer we have been asked
496                  * to release could be any of the current buffers in use, so
497                  * release all buffers that are in use by HW
498                  */
499                 for (i = 0; i < MAX_BUFFER_NUM; i++) {
500                         if (priv->queue_buf[i]) {
501                                 vb2_buffer_done(priv->queue_buf[i],
502                                         VB2_BUF_STATE_ERROR);
503                                 priv->queue_buf[i] = NULL;
504                         }
505                 }
506         } else {
507                 list_del_init(to_buf_list(vb));
508         }
509
510         spin_unlock_irq(&priv->lock);
511 }
512
513 static int rcar_vin_videobuf_init(struct vb2_buffer *vb)
514 {
515         INIT_LIST_HEAD(to_buf_list(vb));
516         return 0;
517 }
518
519 static int rcar_vin_stop_streaming(struct vb2_queue *vq)
520 {
521         struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
522         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
523         struct rcar_vin_priv *priv = ici->priv;
524         struct list_head *buf_head, *tmp;
525
526         spin_lock_irq(&priv->lock);
527         list_for_each_safe(buf_head, tmp, &priv->capture)
528                 list_del_init(buf_head);
529         spin_unlock_irq(&priv->lock);
530
531         return 0;
532 }
533
534 static struct vb2_ops rcar_vin_vb2_ops = {
535         .queue_setup    = rcar_vin_videobuf_setup,
536         .buf_init       = rcar_vin_videobuf_init,
537         .buf_cleanup    = rcar_vin_videobuf_release,
538         .buf_queue      = rcar_vin_videobuf_queue,
539         .stop_streaming = rcar_vin_stop_streaming,
540         .wait_prepare   = soc_camera_unlock,
541         .wait_finish    = soc_camera_lock,
542 };
543
544 static irqreturn_t rcar_vin_irq(int irq, void *data)
545 {
546         struct rcar_vin_priv *priv = data;
547         u32 int_status;
548         bool can_run = false, hw_stopped;
549         int slot;
550         unsigned int handled = 0;
551
552         spin_lock(&priv->lock);
553
554         int_status = ioread32(priv->base + VNINTS_REG);
555         if (!int_status)
556                 goto done;
557         /* ack interrupts */
558         iowrite32(int_status, priv->base + VNINTS_REG);
559         handled = 1;
560
561         /* nothing to do if capture status is 'STOPPED' */
562         if (priv->state == STOPPED)
563                 goto done;
564
565         hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA);
566
567         if (!priv->request_to_stop) {
568                 if (is_continuous_transfer(priv))
569                         slot = (ioread32(priv->base + VNMS_REG) &
570                                 VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
571                 else
572                         slot = 0;
573
574                 priv->queue_buf[slot]->v4l2_buf.field = priv->field;
575                 priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++;
576                 do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp);
577                 vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE);
578                 priv->queue_buf[slot] = NULL;
579
580                 if (priv->state != STOPPING)
581                         can_run = rcar_vin_fill_hw_slot(priv);
582
583                 if (hw_stopped || !can_run) {
584                         priv->state = STOPPED;
585                 } else if (is_continuous_transfer(priv) &&
586                            list_empty(&priv->capture) &&
587                            priv->state == RUNNING) {
588                         /*
589                          * The continuous capturing requires an explicit stop
590                          * operation when there is no buffer to be set into
591                          * the VnMBm registers.
592                          */
593                         rcar_vin_request_capture_stop(priv);
594                 } else {
595                         rcar_vin_capture(priv);
596                 }
597
598         } else if (hw_stopped) {
599                 priv->state = STOPPED;
600                 priv->request_to_stop = false;
601                 complete(&priv->capture_stop);
602         }
603
604 done:
605         spin_unlock(&priv->lock);
606
607         return IRQ_RETVAL(handled);
608 }
609
610 static int rcar_vin_add_device(struct soc_camera_device *icd)
611 {
612         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
613         struct rcar_vin_priv *priv = ici->priv;
614         int i;
615
616         for (i = 0; i < MAX_BUFFER_NUM; i++)
617                 priv->queue_buf[i] = NULL;
618
619         pm_runtime_get_sync(ici->v4l2_dev.dev);
620
621         dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n",
622                 icd->devnum);
623
624         return 0;
625 }
626
627 static void rcar_vin_remove_device(struct soc_camera_device *icd)
628 {
629         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
630         struct rcar_vin_priv *priv = ici->priv;
631         struct vb2_buffer *vb;
632         int i;
633
634         /* disable capture, disable interrupts */
635         iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
636                   priv->base + VNMC_REG);
637         iowrite32(0, priv->base + VNIE_REG);
638
639         priv->state = STOPPED;
640         priv->request_to_stop = false;
641
642         /* make sure active buffer is cancelled */
643         spin_lock_irq(&priv->lock);
644         for (i = 0; i < MAX_BUFFER_NUM; i++) {
645                 vb = priv->queue_buf[i];
646                 if (vb) {
647                         list_del_init(to_buf_list(vb));
648                         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
649                 }
650         }
651         spin_unlock_irq(&priv->lock);
652
653         pm_runtime_put(ici->v4l2_dev.dev);
654
655         dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n",
656                 icd->devnum);
657 }
658
659 /* Called with .host_lock held */
660 static int rcar_vin_clock_start(struct soc_camera_host *ici)
661 {
662         /* VIN does not have "mclk" */
663         return 0;
664 }
665
666 /* Called with .host_lock held */
667 static void rcar_vin_clock_stop(struct soc_camera_host *ici)
668 {
669         /* VIN does not have "mclk" */
670 }
671
672 /* rect is guaranteed to not exceed the scaled camera rectangle */
673 static int rcar_vin_set_rect(struct soc_camera_device *icd)
674 {
675         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
676         struct rcar_vin_cam *cam = icd->host_priv;
677         struct rcar_vin_priv *priv = ici->priv;
678         unsigned int left_offset, top_offset;
679         unsigned char dsize = 0;
680         struct v4l2_rect *cam_subrect = &cam->subrect;
681
682         dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n",
683                 icd->user_width, icd->user_height, cam->vin_left, cam->vin_top);
684
685         left_offset = cam->vin_left;
686         top_offset = cam->vin_top;
687
688         if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 &&
689             priv->chip == RCAR_E1)
690                 dsize = 1;
691
692         dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n",
693                 cam->width, cam->height, cam->vin_left, cam->vin_top);
694         dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n",
695                 cam_subrect->width, cam_subrect->height,
696                 cam_subrect->left, cam_subrect->top);
697
698         /* Set Start/End Pixel/Line Pre-Clip */
699         iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG);
700         iowrite32((left_offset + cam->width - 1) << dsize,
701                   priv->base + VNEPPRC_REG);
702         switch (priv->field) {
703         case V4L2_FIELD_INTERLACED:
704         case V4L2_FIELD_INTERLACED_TB:
705         case V4L2_FIELD_INTERLACED_BT:
706                 iowrite32(top_offset / 2, priv->base + VNSLPRC_REG);
707                 iowrite32((top_offset + cam->height) / 2 - 1,
708                           priv->base + VNELPRC_REG);
709                 break;
710         default:
711                 iowrite32(top_offset, priv->base + VNSLPRC_REG);
712                 iowrite32(top_offset + cam->height - 1,
713                           priv->base + VNELPRC_REG);
714                 break;
715         }
716
717         /* Set Start/End Pixel/Line Post-Clip */
718         iowrite32(0, priv->base + VNSPPOC_REG);
719         iowrite32(0, priv->base + VNSLPOC_REG);
720         iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG);
721         switch (priv->field) {
722         case V4L2_FIELD_INTERLACED:
723         case V4L2_FIELD_INTERLACED_TB:
724         case V4L2_FIELD_INTERLACED_BT:
725                 iowrite32(cam_subrect->height / 2 - 1,
726                           priv->base + VNELPOC_REG);
727                 break;
728         default:
729                 iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG);
730                 break;
731         }
732
733         iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG);
734
735         return 0;
736 }
737
738 static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc)
739 {
740         *vnmc = ioread32(priv->base + VNMC_REG);
741         /* module disable */
742         iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG);
743 }
744
745 static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc)
746 {
747         unsigned long timeout = jiffies + 10 * HZ;
748
749         /*
750          * Wait until the end of the current frame. It can take a long time,
751          * but if it has been aborted by a MRST1 reset, it should exit sooner.
752          */
753         while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) &&
754                 time_before(jiffies, timeout))
755                 msleep(1);
756
757         if (time_after(jiffies, timeout)) {
758                 dev_err(priv->ici.v4l2_dev.dev,
759                         "Timeout waiting for frame end! Interface problem?\n");
760                 return;
761         }
762
763         iowrite32(vnmc, priv->base + VNMC_REG);
764 }
765
766 #define VIN_MBUS_FLAGS  (V4L2_MBUS_MASTER |             \
767                          V4L2_MBUS_PCLK_SAMPLE_RISING | \
768                          V4L2_MBUS_HSYNC_ACTIVE_HIGH |  \
769                          V4L2_MBUS_HSYNC_ACTIVE_LOW |   \
770                          V4L2_MBUS_VSYNC_ACTIVE_HIGH |  \
771                          V4L2_MBUS_VSYNC_ACTIVE_LOW |   \
772                          V4L2_MBUS_DATA_ACTIVE_HIGH)
773
774 static int rcar_vin_set_bus_param(struct soc_camera_device *icd)
775 {
776         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
777         struct rcar_vin_priv *priv = ici->priv;
778         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
779         struct v4l2_mbus_config cfg;
780         unsigned long common_flags;
781         u32 vnmc;
782         u32 val;
783         int ret;
784
785         capture_stop_preserve(priv, &vnmc);
786
787         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
788         if (!ret) {
789                 common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
790                 if (!common_flags) {
791                         dev_warn(icd->parent,
792                                  "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
793                                  cfg.flags, VIN_MBUS_FLAGS);
794                         return -EINVAL;
795                 }
796         } else if (ret != -ENOIOCTLCMD) {
797                 return ret;
798         } else {
799                 common_flags = VIN_MBUS_FLAGS;
800         }
801
802         /* Make choises, based on platform preferences */
803         if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
804             (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
805                 if (priv->pdata_flags & RCAR_VIN_HSYNC_ACTIVE_LOW)
806                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
807                 else
808                         common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
809         }
810
811         if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
812             (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
813                 if (priv->pdata_flags & RCAR_VIN_VSYNC_ACTIVE_LOW)
814                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
815                 else
816                         common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
817         }
818
819         cfg.flags = common_flags;
820         ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
821         if (ret < 0 && ret != -ENOIOCTLCMD)
822                 return ret;
823
824         val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0;
825         if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
826                 val |= VNDMR2_VPS;
827         if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
828                 val |= VNDMR2_HPS;
829         iowrite32(val, priv->base + VNDMR2_REG);
830
831         ret = rcar_vin_set_rect(icd);
832         if (ret < 0)
833                 return ret;
834
835         capture_restore(priv, vnmc);
836
837         return 0;
838 }
839
840 static int rcar_vin_try_bus_param(struct soc_camera_device *icd,
841                                   unsigned char buswidth)
842 {
843         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
844         struct v4l2_mbus_config cfg;
845         int ret;
846
847         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
848         if (ret == -ENOIOCTLCMD)
849                 return 0;
850         else if (ret)
851                 return ret;
852
853         if (buswidth > 24)
854                 return -EINVAL;
855
856         /* check is there common mbus flags */
857         ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
858         if (ret)
859                 return 0;
860
861         dev_warn(icd->parent,
862                 "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
863                  cfg.flags, VIN_MBUS_FLAGS);
864
865         return -EINVAL;
866 }
867
868 static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt)
869 {
870         return  fmt->packing == SOC_MBUS_PACKING_NONE ||
871                 (fmt->bits_per_sample > 8 &&
872                  fmt->packing == SOC_MBUS_PACKING_EXTEND16);
873 }
874
875 static const struct soc_mbus_pixelfmt rcar_vin_formats[] = {
876         {
877                 .fourcc                 = V4L2_PIX_FMT_NV16,
878                 .name                   = "NV16",
879                 .bits_per_sample        = 8,
880                 .packing                = SOC_MBUS_PACKING_2X8_PADHI,
881                 .order                  = SOC_MBUS_ORDER_LE,
882                 .layout                 = SOC_MBUS_LAYOUT_PLANAR_Y_C,
883         },
884         {
885                 .fourcc                 = V4L2_PIX_FMT_UYVY,
886                 .name                   = "UYVY",
887                 .bits_per_sample        = 16,
888                 .packing                = SOC_MBUS_PACKING_NONE,
889                 .order                  = SOC_MBUS_ORDER_LE,
890                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
891         },
892         {
893                 .fourcc                 = V4L2_PIX_FMT_RGB565,
894                 .name                   = "RGB565",
895                 .bits_per_sample        = 16,
896                 .packing                = SOC_MBUS_PACKING_NONE,
897                 .order                  = SOC_MBUS_ORDER_LE,
898                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
899         },
900         {
901                 .fourcc                 = V4L2_PIX_FMT_RGB555X,
902                 .name                   = "ARGB1555",
903                 .bits_per_sample        = 16,
904                 .packing                = SOC_MBUS_PACKING_NONE,
905                 .order                  = SOC_MBUS_ORDER_LE,
906                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
907         },
908         {
909                 .fourcc                 = V4L2_PIX_FMT_RGB32,
910                 .name                   = "RGB888",
911                 .bits_per_sample        = 32,
912                 .packing                = SOC_MBUS_PACKING_NONE,
913                 .order                  = SOC_MBUS_ORDER_LE,
914                 .layout                 = SOC_MBUS_LAYOUT_PACKED,
915         },
916 };
917
918 static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx,
919                                 struct soc_camera_format_xlate *xlate)
920 {
921         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
922         struct device *dev = icd->parent;
923         int ret, k, n;
924         int formats = 0;
925         struct rcar_vin_cam *cam;
926         enum v4l2_mbus_pixelcode code;
927         const struct soc_mbus_pixelfmt *fmt;
928
929         ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
930         if (ret < 0)
931                 return 0;
932
933         fmt = soc_mbus_get_fmtdesc(code);
934         if (!fmt) {
935                 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
936                 return 0;
937         }
938
939         ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample);
940         if (ret < 0)
941                 return 0;
942
943         if (!icd->host_priv) {
944                 struct v4l2_mbus_framefmt mf;
945                 struct v4l2_rect rect;
946                 struct device *dev = icd->parent;
947                 int shift;
948
949                 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
950                 if (ret < 0)
951                         return ret;
952
953                 /* Cache current client geometry */
954                 ret = soc_camera_client_g_rect(sd, &rect);
955                 if (ret == -ENOIOCTLCMD) {
956                         /* Sensor driver doesn't support cropping */
957                         rect.left = 0;
958                         rect.top = 0;
959                         rect.width = mf.width;
960                         rect.height = mf.height;
961                 } else if (ret < 0) {
962                         return ret;
963                 }
964
965                 /*
966                  * If sensor proposes too large format then try smaller ones:
967                  * 1280x960, 640x480, 320x240
968                  */
969                 for (shift = 0; shift < 3; shift++) {
970                         if (mf.width <= VIN_MAX_WIDTH &&
971                             mf.height <= VIN_MAX_HEIGHT)
972                                 break;
973
974                         mf.width = 1280 >> shift;
975                         mf.height = 960 >> shift;
976                         ret = v4l2_device_call_until_err(sd->v4l2_dev,
977                                                          soc_camera_grp_id(icd),
978                                                          video, s_mbus_fmt,
979                                                          &mf);
980                         if (ret < 0)
981                                 return ret;
982                 }
983
984                 if (shift == 3) {
985                         dev_err(dev,
986                                 "Failed to configure the client below %ux%x\n",
987                                 mf.width, mf.height);
988                         return -EIO;
989                 }
990
991                 dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height);
992
993                 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
994                 if (!cam)
995                         return -ENOMEM;
996                 /*
997                  * We are called with current camera crop,
998                  * initialise subrect with it
999                  */
1000                 cam->rect = rect;
1001                 cam->subrect = rect;
1002                 cam->width = mf.width;
1003                 cam->height = mf.height;
1004
1005                 icd->host_priv = cam;
1006         } else {
1007                 cam = icd->host_priv;
1008         }
1009
1010         /* Beginning of a pass */
1011         if (!idx)
1012                 cam->extra_fmt = NULL;
1013
1014         switch (code) {
1015         case V4L2_MBUS_FMT_YUYV8_1X16:
1016         case V4L2_MBUS_FMT_YUYV8_2X8:
1017         case V4L2_MBUS_FMT_YUYV10_2X10:
1018                 if (cam->extra_fmt)
1019                         break;
1020
1021                 /* Add all our formats that can be generated by VIN */
1022                 cam->extra_fmt = rcar_vin_formats;
1023
1024                 n = ARRAY_SIZE(rcar_vin_formats);
1025                 formats += n;
1026                 for (k = 0; xlate && k < n; k++, xlate++) {
1027                         xlate->host_fmt = &rcar_vin_formats[k];
1028                         xlate->code = code;
1029                         dev_dbg(dev, "Providing format %s using code %d\n",
1030                                 rcar_vin_formats[k].name, code);
1031                 }
1032                 break;
1033         default:
1034                 if (!rcar_vin_packing_supported(fmt))
1035                         return 0;
1036
1037                 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1038                         fmt->name);
1039                 break;
1040         }
1041
1042         /* Generic pass-through */
1043         formats++;
1044         if (xlate) {
1045                 xlate->host_fmt = fmt;
1046                 xlate->code = code;
1047                 xlate++;
1048         }
1049
1050         return formats;
1051 }
1052
1053 static void rcar_vin_put_formats(struct soc_camera_device *icd)
1054 {
1055         kfree(icd->host_priv);
1056         icd->host_priv = NULL;
1057 }
1058
1059 static int rcar_vin_set_crop(struct soc_camera_device *icd,
1060                              const struct v4l2_crop *a)
1061 {
1062         struct v4l2_crop a_writable = *a;
1063         const struct v4l2_rect *rect = &a_writable.c;
1064         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1065         struct rcar_vin_priv *priv = ici->priv;
1066         struct v4l2_crop cam_crop;
1067         struct rcar_vin_cam *cam = icd->host_priv;
1068         struct v4l2_rect *cam_rect = &cam_crop.c;
1069         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1070         struct device *dev = icd->parent;
1071         struct v4l2_mbus_framefmt mf;
1072         u32 vnmc;
1073         int ret, i;
1074
1075         dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1076                 rect->left, rect->top);
1077
1078         /* During camera cropping its output window can change too, stop VIN */
1079         capture_stop_preserve(priv, &vnmc);
1080         dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc);
1081
1082         /* Apply iterative camera S_CROP for new input window. */
1083         ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
1084                                        &cam->rect, &cam->subrect);
1085         if (ret < 0)
1086                 return ret;
1087
1088         dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n",
1089                 cam_rect->width, cam_rect->height,
1090                 cam_rect->left, cam_rect->top);
1091
1092         /* On success cam_crop contains current camera crop */
1093
1094         /* Retrieve camera output window */
1095         ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1096         if (ret < 0)
1097                 return ret;
1098
1099         if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT)
1100                 return -EINVAL;
1101
1102         /* Cache camera output window */
1103         cam->width = mf.width;
1104         cam->height = mf.height;
1105
1106         icd->user_width  = cam->width;
1107         icd->user_height = cam->height;
1108
1109         cam->vin_left = rect->left & ~1;
1110         cam->vin_top = rect->top & ~1;
1111
1112         /* Use VIN cropping to crop to the new window. */
1113         ret = rcar_vin_set_rect(icd);
1114         if (ret < 0)
1115                 return ret;
1116
1117         cam->subrect = *rect;
1118
1119         dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n",
1120                 icd->user_width, icd->user_height,
1121                 cam->vin_left, cam->vin_top);
1122
1123         /* Restore capture */
1124         for (i = 0; i < MAX_BUFFER_NUM; i++) {
1125                 if (priv->queue_buf[i] && priv->state == STOPPED) {
1126                         vnmc |= VNMC_ME;
1127                         break;
1128                 }
1129         }
1130         capture_restore(priv, vnmc);
1131
1132         /* Even if only camera cropping succeeded */
1133         return ret;
1134 }
1135
1136 static int rcar_vin_get_crop(struct soc_camera_device *icd,
1137                              struct v4l2_crop *a)
1138 {
1139         struct rcar_vin_cam *cam = icd->host_priv;
1140
1141         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1142         a->c = cam->subrect;
1143
1144         return 0;
1145 }
1146
1147 /* Similar to set_crop multistage iterative algorithm */
1148 static int rcar_vin_set_fmt(struct soc_camera_device *icd,
1149                             struct v4l2_format *f)
1150 {
1151         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1152         struct rcar_vin_priv *priv = ici->priv;
1153         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1154         struct rcar_vin_cam *cam = icd->host_priv;
1155         struct v4l2_pix_format *pix = &f->fmt.pix;
1156         struct v4l2_mbus_framefmt mf;
1157         struct device *dev = icd->parent;
1158         __u32 pixfmt = pix->pixelformat;
1159         const struct soc_camera_format_xlate *xlate;
1160         unsigned int vin_sub_width = 0, vin_sub_height = 0;
1161         int ret;
1162         bool can_scale;
1163         enum v4l2_field field;
1164         v4l2_std_id std;
1165
1166         dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n",
1167                 pixfmt, pix->width, pix->height);
1168
1169         switch (pix->field) {
1170         default:
1171                 pix->field = V4L2_FIELD_NONE;
1172                 /* fall-through */
1173         case V4L2_FIELD_NONE:
1174         case V4L2_FIELD_TOP:
1175         case V4L2_FIELD_BOTTOM:
1176         case V4L2_FIELD_INTERLACED_TB:
1177         case V4L2_FIELD_INTERLACED_BT:
1178                 field = pix->field;
1179                 break;
1180         case V4L2_FIELD_INTERLACED:
1181                 /* Query for standard if not explicitly mentioned _TB/_BT */
1182                 ret = v4l2_subdev_call(sd, video, querystd, &std);
1183                 if (ret < 0)
1184                         std = V4L2_STD_625_50;
1185
1186                 field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB :
1187                                                 V4L2_FIELD_INTERLACED_BT;
1188                 break;
1189         }
1190
1191         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1192         if (!xlate) {
1193                 dev_warn(dev, "Format %x not found\n", pixfmt);
1194                 return -EINVAL;
1195         }
1196         /* Calculate client output geometry */
1197         soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf,
1198                                       12);
1199         mf.field = pix->field;
1200         mf.colorspace = pix->colorspace;
1201         mf.code  = xlate->code;
1202
1203         switch (pixfmt) {
1204         case V4L2_PIX_FMT_RGB32:
1205                 can_scale = priv->chip != RCAR_E1;
1206                 break;
1207         case V4L2_PIX_FMT_UYVY:
1208         case V4L2_PIX_FMT_YUYV:
1209         case V4L2_PIX_FMT_RGB565:
1210         case V4L2_PIX_FMT_RGB555X:
1211                 can_scale = true;
1212                 break;
1213         default:
1214                 can_scale = false;
1215                 break;
1216         }
1217
1218         dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height);
1219
1220         ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1221                                       &mf, &vin_sub_width, &vin_sub_height,
1222                                       can_scale, 12);
1223
1224         /* Done with the camera. Now see if we can improve the result */
1225         dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1226                 ret, mf.width, mf.height, pix->width, pix->height);
1227
1228         if (ret == -ENOIOCTLCMD)
1229                 dev_dbg(dev, "Sensor doesn't support scaling\n");
1230         else if (ret < 0)
1231                 return ret;
1232
1233         if (mf.code != xlate->code)
1234                 return -EINVAL;
1235
1236         /* Prepare VIN crop */
1237         cam->width = mf.width;
1238         cam->height = mf.height;
1239
1240         /* Use VIN scaling to scale to the requested user window. */
1241
1242         /* We cannot scale up */
1243         if (pix->width > vin_sub_width)
1244                 vin_sub_width = pix->width;
1245
1246         if (pix->height > vin_sub_height)
1247                 vin_sub_height = pix->height;
1248
1249         pix->colorspace = mf.colorspace;
1250
1251         if (!can_scale) {
1252                 pix->width = vin_sub_width;
1253                 pix->height = vin_sub_height;
1254         }
1255
1256         /*
1257          * We have calculated CFLCR, the actual configuration will be performed
1258          * in rcar_vin_set_bus_param()
1259          */
1260
1261         dev_dbg(dev, "W: %u : %u, H: %u : %u\n",
1262                 vin_sub_width, pix->width, vin_sub_height, pix->height);
1263
1264         icd->current_fmt = xlate;
1265
1266         priv->field = field;
1267
1268         return 0;
1269 }
1270
1271 static int rcar_vin_try_fmt(struct soc_camera_device *icd,
1272                             struct v4l2_format *f)
1273 {
1274         const struct soc_camera_format_xlate *xlate;
1275         struct v4l2_pix_format *pix = &f->fmt.pix;
1276         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1277         struct v4l2_mbus_framefmt mf;
1278         __u32 pixfmt = pix->pixelformat;
1279         int width, height;
1280         int ret;
1281
1282         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1283         if (!xlate) {
1284                 xlate = icd->current_fmt;
1285                 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1286                         pixfmt, xlate->host_fmt->fourcc);
1287                 pixfmt = xlate->host_fmt->fourcc;
1288                 pix->pixelformat = pixfmt;
1289                 pix->colorspace = icd->colorspace;
1290         }
1291
1292         /* FIXME: calculate using depth and bus width */
1293         v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1,
1294                               &pix->height, 4, VIN_MAX_HEIGHT, 2, 0);
1295
1296         width = pix->width;
1297         height = pix->height;
1298
1299         /* let soc-camera calculate these values */
1300         pix->bytesperline = 0;
1301         pix->sizeimage = 0;
1302
1303         /* limit to sensor capabilities */
1304         mf.width = pix->width;
1305         mf.height = pix->height;
1306         mf.field = pix->field;
1307         mf.code = xlate->code;
1308         mf.colorspace = pix->colorspace;
1309
1310         ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1311                                          video, try_mbus_fmt, &mf);
1312         if (ret < 0)
1313                 return ret;
1314
1315         pix->width = mf.width;
1316         pix->height = mf.height;
1317         pix->field = mf.field;
1318         pix->colorspace = mf.colorspace;
1319
1320         if (pixfmt == V4L2_PIX_FMT_NV16) {
1321                 /* FIXME: check against rect_max after converting soc-camera */
1322                 /* We can scale precisely, need a bigger image from camera */
1323                 if (pix->width < width || pix->height < height) {
1324                         /*
1325                          * We presume, the sensor behaves sanely, i.e. if
1326                          * requested a bigger rectangle, it will not return a
1327                          * smaller one.
1328                          */
1329                         mf.width = VIN_MAX_WIDTH;
1330                         mf.height = VIN_MAX_HEIGHT;
1331                         ret = v4l2_device_call_until_err(sd->v4l2_dev,
1332                                                          soc_camera_grp_id(icd),
1333                                                          video, try_mbus_fmt,
1334                                                          &mf);
1335                         if (ret < 0) {
1336                                 dev_err(icd->parent,
1337                                         "client try_fmt() = %d\n", ret);
1338                                 return ret;
1339                         }
1340                 }
1341                 /* We will scale exactly */
1342                 if (mf.width > width)
1343                         pix->width = width;
1344                 if (mf.height > height)
1345                         pix->height = height;
1346         }
1347
1348         return ret;
1349 }
1350
1351 static unsigned int rcar_vin_poll(struct file *file, poll_table *pt)
1352 {
1353         struct soc_camera_device *icd = file->private_data;
1354
1355         return vb2_poll(&icd->vb2_vidq, file, pt);
1356 }
1357
1358 static int rcar_vin_querycap(struct soc_camera_host *ici,
1359                              struct v4l2_capability *cap)
1360 {
1361         strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
1362         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1363         return 0;
1364 }
1365
1366 static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
1367                                    struct soc_camera_device *icd)
1368 {
1369         vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1370         vq->io_modes = VB2_MMAP | VB2_USERPTR;
1371         vq->drv_priv = icd;
1372         vq->ops = &rcar_vin_vb2_ops;
1373         vq->mem_ops = &vb2_dma_contig_memops;
1374         vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
1375         vq->timestamp_type  = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1376
1377         return vb2_queue_init(vq);
1378 }
1379
1380 static struct soc_camera_host_ops rcar_vin_host_ops = {
1381         .owner          = THIS_MODULE,
1382         .add            = rcar_vin_add_device,
1383         .remove         = rcar_vin_remove_device,
1384         .clock_start    = rcar_vin_clock_start,
1385         .clock_stop     = rcar_vin_clock_stop,
1386         .get_formats    = rcar_vin_get_formats,
1387         .put_formats    = rcar_vin_put_formats,
1388         .get_crop       = rcar_vin_get_crop,
1389         .set_crop       = rcar_vin_set_crop,
1390         .try_fmt        = rcar_vin_try_fmt,
1391         .set_fmt        = rcar_vin_set_fmt,
1392         .poll           = rcar_vin_poll,
1393         .querycap       = rcar_vin_querycap,
1394         .set_bus_param  = rcar_vin_set_bus_param,
1395         .init_videobuf2 = rcar_vin_init_videobuf2,
1396 };
1397
1398 #ifdef CONFIG_OF
1399 static struct of_device_id rcar_vin_of_table[] = {
1400         { .compatible = "renesas,vin-r8a7791", .data = (void *)RCAR_GEN2 },
1401         { .compatible = "renesas,vin-r8a7790", .data = (void *)RCAR_GEN2 },
1402         { .compatible = "renesas,vin-r8a7779", .data = (void *)RCAR_H1 },
1403         { .compatible = "renesas,vin-r8a7778", .data = (void *)RCAR_M1 },
1404         { },
1405 };
1406 MODULE_DEVICE_TABLE(of, rcar_vin_of_table);
1407 #endif
1408
1409 static struct platform_device_id rcar_vin_id_table[] = {
1410         { "r8a7791-vin",  RCAR_GEN2 },
1411         { "r8a7790-vin",  RCAR_GEN2 },
1412         { "r8a7779-vin",  RCAR_H1 },
1413         { "r8a7778-vin",  RCAR_M1 },
1414         { "uPD35004-vin", RCAR_E1 },
1415         {},
1416 };
1417 MODULE_DEVICE_TABLE(platform, rcar_vin_id_table);
1418
1419 static int rcar_vin_probe(struct platform_device *pdev)
1420 {
1421         const struct of_device_id *match = NULL;
1422         struct rcar_vin_priv *priv;
1423         struct resource *mem;
1424         struct rcar_vin_platform_data *pdata;
1425         unsigned int pdata_flags;
1426         int irq, ret;
1427
1428         if (pdev->dev.of_node) {
1429                 struct v4l2_of_endpoint ep;
1430                 struct device_node *np;
1431
1432                 match = of_match_device(of_match_ptr(rcar_vin_of_table),
1433                                         &pdev->dev);
1434
1435                 np = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
1436                 if (!np) {
1437                         dev_err(&pdev->dev, "could not find endpoint\n");
1438                         return -EINVAL;
1439                 }
1440
1441                 ret = v4l2_of_parse_endpoint(np, &ep);
1442                 if (ret) {
1443                         dev_err(&pdev->dev, "could not parse endpoint\n");
1444                         return ret;
1445                 }
1446
1447                 if (ep.bus_type == V4L2_MBUS_BT656)
1448                         pdata_flags = RCAR_VIN_BT656;
1449                 else {
1450                         pdata_flags = 0;
1451                         if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1452                                 pdata_flags |= RCAR_VIN_HSYNC_ACTIVE_LOW;
1453                         if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1454                                 pdata_flags |= RCAR_VIN_VSYNC_ACTIVE_LOW;
1455                 }
1456
1457                 of_node_put(np);
1458
1459                 dev_dbg(&pdev->dev, "pdata_flags = %08x\n", pdata_flags);
1460         } else {
1461                 pdata = pdev->dev.platform_data;
1462                 if (!pdata || !pdata->flags) {
1463                         dev_err(&pdev->dev, "platform data not set\n");
1464                         return -EINVAL;
1465                 }
1466                 pdata_flags = pdata->flags;
1467         }
1468
1469         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1470         if (mem == NULL)
1471                 return -EINVAL;
1472
1473         irq = platform_get_irq(pdev, 0);
1474         if (irq <= 0)
1475                 return -EINVAL;
1476
1477         priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv),
1478                             GFP_KERNEL);
1479         if (!priv)
1480                 return -ENOMEM;
1481
1482         priv->base = devm_ioremap_resource(&pdev->dev, mem);
1483         if (IS_ERR(priv->base))
1484                 return PTR_ERR(priv->base);
1485
1486         ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED,
1487                                dev_name(&pdev->dev), priv);
1488         if (ret)
1489                 return ret;
1490
1491         priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1492         if (IS_ERR(priv->alloc_ctx))
1493                 return PTR_ERR(priv->alloc_ctx);
1494
1495         priv->ici.priv = priv;
1496         priv->ici.v4l2_dev.dev = &pdev->dev;
1497         priv->ici.drv_name = dev_name(&pdev->dev);
1498         priv->ici.ops = &rcar_vin_host_ops;
1499
1500         priv->pdata_flags = pdata_flags;
1501         if (!match) {
1502                 priv->ici.nr = pdev->id;
1503                 priv->chip = pdev->id_entry->driver_data;
1504         } else {
1505                 priv->ici.nr = of_alias_get_id(pdev->dev.of_node, "vin");
1506                 priv->chip = (enum chip_id)match->data;
1507         };
1508
1509         spin_lock_init(&priv->lock);
1510         INIT_LIST_HEAD(&priv->capture);
1511
1512         priv->state = STOPPED;
1513
1514         pm_suspend_ignore_children(&pdev->dev, true);
1515         pm_runtime_enable(&pdev->dev);
1516
1517         ret = soc_camera_host_register(&priv->ici);
1518         if (ret)
1519                 goto cleanup;
1520
1521         return 0;
1522
1523 cleanup:
1524         pm_runtime_disable(&pdev->dev);
1525         vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1526
1527         return ret;
1528 }
1529
1530 static int rcar_vin_remove(struct platform_device *pdev)
1531 {
1532         struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1533         struct rcar_vin_priv *priv = container_of(soc_host,
1534                                                   struct rcar_vin_priv, ici);
1535
1536         soc_camera_host_unregister(soc_host);
1537         pm_runtime_disable(&pdev->dev);
1538         vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1539
1540         return 0;
1541 }
1542
1543 static struct platform_driver rcar_vin_driver = {
1544         .probe          = rcar_vin_probe,
1545         .remove         = rcar_vin_remove,
1546         .driver         = {
1547                 .name           = DRV_NAME,
1548                 .owner          = THIS_MODULE,
1549                 .of_match_table = of_match_ptr(rcar_vin_of_table),
1550         },
1551         .id_table       = rcar_vin_id_table,
1552 };
1553
1554 module_platform_driver(rcar_vin_driver);
1555
1556 MODULE_LICENSE("GPL");
1557 MODULE_ALIAS("platform:rcar_vin");
1558 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");