Re: [PATCH] adv7604: add CEC support for adv7611/adv7612

2018-10-15 Thread Niklas Söderlund
Hi Hans,

Thanks for your patch.

On 2018-10-12 13:30:02 +0200, Hans Verkuil wrote:
> The CEC IP is very similar between the three HDMI receivers, but
> not identical. Add support for all three variants.
> 
> Tested with an adv7604 and an adv7612.
> 
> Signed-off-by: Hans Verkuil 

This fixes CEC on my Koelsch board using the adv7604.

Reviewed-by: Niklas Söderlund 

Side note do you know of a way to simulate a cycling of the physical HDMI 
cable? My current test-case for CEC is:

v4l2-ctl -d $(grep -l "adv7612" /sys/class/video4linux/*/name | sed 
's#.*video4linux\(.*\)/name#/dev\1#g') --set-edid=type=hdmi
cec-ctl -d 0 --playback
cec-ctl -d 1 --tv
# Here I need to attach or if it already is connected disconnect and 
# reconnect the HDMI cable
cec-ctl -d 0 -S
cec-ctl -d 1 -S

If that step could be done in software I can add this test to my 
automatic test scripts which would be nice.

> ---
>  drivers/media/i2c/adv7604.c | 63 +++--
>  1 file changed, 53 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
> index 9eb7c70a7712..88786276dbe4 100644
> --- a/drivers/media/i2c/adv7604.c
> +++ b/drivers/media/i2c/adv7604.c
> @@ -114,6 +114,11 @@ struct adv76xx_chip_info {
>   unsigned int fmt_change_digital_mask;
>   unsigned int cp_csc;
> 
> + unsigned int cec_irq_status;
> + unsigned int cec_rx_enable;
> + unsigned int cec_rx_enable_mask;
> + bool cec_irq_swap;
> +
>   const struct adv76xx_format_info *formats;
>   unsigned int nformats;
> 
> @@ -2003,10 +2008,11 @@ static void adv76xx_cec_tx_raw_status(struct 
> v4l2_subdev *sd, u8 tx_raw_status)
>  static void adv76xx_cec_isr(struct v4l2_subdev *sd, bool *handled)
>  {
>   struct adv76xx_state *state = to_state(sd);
> + const struct adv76xx_chip_info *info = state->info;
>   u8 cec_irq;
> 
>   /* cec controller */
> - cec_irq = io_read(sd, 0x4d) & 0x0f;
> + cec_irq = io_read(sd, info->cec_irq_status) & 0x0f;
>   if (!cec_irq)
>   return;
> 
> @@ -2024,15 +2030,21 @@ static void adv76xx_cec_isr(struct v4l2_subdev *sd, 
> bool *handled)
> 
>   for (i = 0; i < msg.len; i++)
>   msg.msg[i] = cec_read(sd, i + 0x15);
> - cec_write(sd, 0x26, 0x01); /* re-enable rx */
> + cec_write(sd, info->cec_rx_enable,
> +   info->cec_rx_enable_mask); /* re-enable rx */
>   cec_received_msg(state->cec_adap, );
>   }
>   }
> 
> - /* note: the bit order is swapped between 0x4d and 0x4e */
> - cec_irq = ((cec_irq & 0x08) >> 3) | ((cec_irq & 0x04) >> 1) |
> -   ((cec_irq & 0x02) << 1) | ((cec_irq & 0x01) << 3);
> - io_write(sd, 0x4e, cec_irq);
> + if (info->cec_irq_swap) {
> + /*
> +  * Note: the bit order is swapped between 0x4d and 0x4e
> +  * on adv7604
> +  */
> + cec_irq = ((cec_irq & 0x08) >> 3) | ((cec_irq & 0x04) >> 1) |
> +   ((cec_irq & 0x02) << 1) | ((cec_irq & 0x01) << 3);
> + }
> + io_write(sd, info->cec_irq_status + 1, cec_irq);
> 
>   if (handled)
>   *handled = true;
> @@ -2041,6 +2053,7 @@ static void adv76xx_cec_isr(struct v4l2_subdev *sd, 
> bool *handled)
>  static int adv76xx_cec_adap_enable(struct cec_adapter *adap, bool enable)
>  {
>   struct adv76xx_state *state = cec_get_drvdata(adap);
> + const struct adv76xx_chip_info *info = state->info;
>   struct v4l2_subdev *sd = >sd;
> 
>   if (!state->cec_enabled_adap && enable) {
> @@ -2052,11 +2065,11 @@ static int adv76xx_cec_adap_enable(struct cec_adapter 
> *adap, bool enable)
>   /* tx: arbitration lost */
>   /* tx: retry timeout */
>   /* rx: ready */
> - io_write_clr_set(sd, 0x50, 0x0f, 0x0f);
> - cec_write(sd, 0x26, 0x01);/* enable rx */
> + io_write_clr_set(sd, info->cec_irq_status + 3, 0x0f, 0x0f);
> + cec_write(sd, info->cec_rx_enable, info->cec_rx_enable_mask);
>   } else if (state->cec_enabled_adap && !enable) {
>   /* disable cec interrupts */
> - io_write_clr_set(sd, 0x50, 0x0f, 0x00);
> + io_write_clr_set(sd, info->cec_irq_status + 3, 0x0f, 0x00);
>   /* disable address mask 1-3 */
>   cec_write_clr_set(sd, 0x27, 0x70, 0x00);
>

Re: [RFC PATCH 00/11] Convert last remaining g/s_crop/cropcap drivers

2018-10-05 Thread Niklas Söderlund
Hi Hans,

I like this series, nice work!

On 2018-10-05 09:49:00 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> This patch series converts the last remaining drivers that use g/s_crop and
> cropcap to g/s_selection.
> 
> The first two patches do some minor code cleanup.
> 
> The third patch adds a new video_device flag to indicate that the driver
> inverts the normal usage of g/s_crop/cropcap. This applies to the old
> Samsung drivers that predate the Selection API and that abused the existing
> crop API.
> 
> The next three patches do some code cleanup and prepare drivers for the
> removal of g/s_crop and ensure that cropcap only returns the pixelaspect.
> 
> The next three patches convert the remaining Samsung drivers and set the
> QUIRK flag for all three.
> 
> The final two patches remove vidioc_g/s_crop and rename vidioc_cropcap
> to vidioc_g_pixelaspect.
> 
> I would really appreciate it if someone from Samsung can test these
> three drivers or at the very least review the code.
> 
> Niklas, this series supersedes your 'v4l2-ioctl: fix CROPCAP type handling'
> patch. Sorry about that :-)

No worries, I'm happy my tests run without errors again :-) If 
appropriate fell free to add for the v4l2 and rcar-vin portions:

Tested-by: Niklas Söderlund 

> 
> Regards,
> 
>   Hans
> 
> Hans Verkuil (11):
>   v4l2-ioctl: don't use CROP/COMPOSE_ACTIVE
>   v4l2-common.h: put backwards compat defines under #ifndef __KERNEL__
>   v4l2-ioctl: add QUIRK_INVERTED_CROP
>   davinci/vpbe: drop unused g_cropcap
>   cropcap/g_selection split
>   exynos-gsc: replace v4l2_crop by v4l2_selection
>   s5p_mfc_dec.c: convert g_crop to g_selection
>   exynos4-is: convert g/s_crop to g/s_selection
>   s5p-g2d: convert g/s_crop to g/s_selection
>   v4l2-ioctl: remove unused vidioc_g/s_crop
>   vidioc_cropcap -> vidioc_g_pixelaspect
> 
>  drivers/media/pci/bt8xx/bttv-driver.c |  12 +-
>  drivers/media/pci/cobalt/cobalt-v4l2.c|  48 +--
>  drivers/media/pci/cx18/cx18-ioctl.c   |  13 +-
>  drivers/media/pci/cx23885/cx23885-video.c |  40 --
>  drivers/media/pci/ivtv/ivtv-ioctl.c   |  17 +--
>  drivers/media/pci/saa7134/saa7134-video.c |  21 ++-
>  drivers/media/platform/am437x/am437x-vpfe.c   |  31 ++---
>  drivers/media/platform/davinci/vpbe.c |  23 
>  drivers/media/platform/davinci/vpbe_display.c |  10 +-
>  drivers/media/platform/davinci/vpfe_capture.c |  12 +-
>  drivers/media/platform/exynos-gsc/gsc-core.c  |  57 +++-
>  drivers/media/platform/exynos-gsc/gsc-core.h  |   3 +-
>  drivers/media/platform/exynos-gsc/gsc-m2m.c   |  23 ++--
>  drivers/media/platform/exynos4-is/fimc-core.h |   6 +-
>  drivers/media/platform/exynos4-is/fimc-m2m.c  | 130 ++
>  drivers/media/platform/rcar-vin/rcar-v4l2.c   |  10 +-
>  drivers/media/platform/s5p-g2d/g2d.c  | 102 +-
>  drivers/media/platform/s5p-mfc/s5p_mfc.c  |   1 +
>  drivers/media/platform/s5p-mfc/s5p_mfc_dec.c  |  49 ---
>  drivers/media/platform/vivid/vivid-core.c |   9 +-
>  drivers/media/platform/vivid/vivid-vid-cap.c  |  18 ++-
>  drivers/media/platform/vivid/vivid-vid-cap.h  |   2 +-
>  drivers/media/platform/vivid/vivid-vid-out.c  |  18 ++-
>  drivers/media/platform/vivid/vivid-vid-out.h  |   2 +-
>  drivers/media/usb/au0828/au0828-video.c   |  38 +++--
>  drivers/media/usb/cpia2/cpia2_v4l.c   |  31 +++--
>  drivers/media/usb/cx231xx/cx231xx-417.c   |  41 --
>  drivers/media/usb/cx231xx/cx231xx-video.c |  41 --
>  drivers/media/usb/pvrusb2/pvrusb2-v4l2.c  |  13 +-
>  drivers/media/v4l2-core/v4l2-dev.c|   8 +-
>  drivers/media/v4l2-core/v4l2-ioctl.c  |  44 --
>  include/media/davinci/vpbe.h  |   4 -
>  include/media/v4l2-dev.h      |  13 +-
>  include/media/v4l2-ioctl.h|  16 +--
>  include/uapi/linux/v4l2-common.h  |  28 ++--
>  35 files changed, 537 insertions(+), 397 deletions(-)
> 
> -- 
> 2.18.0
> 

-- 
Regards,
Niklas Söderlund


Re: [RFC PATCH 11/11] vidioc_cropcap -> vidioc_g_pixelaspect

2018-10-05 Thread Niklas Söderlund
Hi Hans,

Thanks for your patch.

On 2018-10-05 09:49:11 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> Now vidioc_cropcap is only used to return the pixelaspect, so
> rename it accordingly.
> 
> Signed-off-by: Hans Verkuil 

For the v4l2 and rcar-vin changes:

Reviewed-by: Niklas Söderlund 

> ---
>  drivers/media/pci/bt8xx/bttv-driver.c | 12 +--
>  drivers/media/pci/cobalt/cobalt-v4l2.c| 10 +
>  drivers/media/pci/cx18/cx18-ioctl.c   | 13 ++--
>  drivers/media/pci/cx23885/cx23885-video.c | 12 +--
>  drivers/media/pci/ivtv/ivtv-ioctl.c   | 17 ---
>  drivers/media/pci/saa7134/saa7134-video.c | 21 +--
>  drivers/media/platform/am437x/am437x-vpfe.c   | 13 ++--
>  drivers/media/platform/davinci/vpbe_display.c | 10 -
>  drivers/media/platform/davinci/vpfe_capture.c | 12 +--
>  drivers/media/platform/rcar-vin/rcar-v4l2.c   | 10 -
>  drivers/media/platform/vivid/vivid-core.c |  9 
>  drivers/media/platform/vivid/vivid-vid-cap.c  | 18 +++-
>  drivers/media/platform/vivid/vivid-vid-cap.h  |  2 +-
>  drivers/media/platform/vivid/vivid-vid-out.c  | 18 +++-
>  drivers/media/platform/vivid/vivid-vid-out.h  |  2 +-
>  drivers/media/usb/au0828/au0828-video.c   | 12 +--
>  drivers/media/usb/cx231xx/cx231xx-417.c   | 12 +--
>  drivers/media/usb/cx231xx/cx231xx-video.c | 12 +--
>  drivers/media/usb/pvrusb2/pvrusb2-v4l2.c  | 13 +++-
>  drivers/media/v4l2-core/v4l2-dev.c|  6 +++---
>  drivers/media/v4l2-core/v4l2-ioctl.c  | 15 +++--
>  include/media/v4l2-ioctl.h|  8 +++
>  22 files changed, 131 insertions(+), 126 deletions(-)
> 
> diff --git a/drivers/media/pci/bt8xx/bttv-driver.c 
> b/drivers/media/pci/bt8xx/bttv-driver.c
> index b2cfcbb0008e..52cac1d3f577 100644
> --- a/drivers/media/pci/bt8xx/bttv-driver.c
> +++ b/drivers/media/pci/bt8xx/bttv-driver.c
> @@ -2792,19 +2792,17 @@ static int bttv_g_tuner(struct file *file, void *priv,
>   return 0;
>  }
>  
> -static int bttv_cropcap(struct file *file, void *priv,
> - struct v4l2_cropcap *cap)
> +static int bttv_g_pixelaspect(struct file *file, void *priv,
> +   int type, struct v4l2_fract *f)
>  {
>   struct bttv_fh *fh = priv;
>   struct bttv *btv = fh->btv;
>  
> - if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
> - cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
> + if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>   return -EINVAL;
>  
>   /* defrect and bounds are set via g_selection */
> - cap->pixelaspect = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
> -
> + *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
>   return 0;
>  }
>  
> @@ -3162,7 +3160,7 @@ static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
>   .vidioc_g_fmt_vbi_cap   = bttv_g_fmt_vbi_cap,
>   .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
>   .vidioc_s_fmt_vbi_cap   = bttv_s_fmt_vbi_cap,
> - .vidioc_cropcap = bttv_cropcap,
> + .vidioc_g_pixelaspect   = bttv_g_pixelaspect,
>   .vidioc_reqbufs = bttv_reqbufs,
>   .vidioc_querybuf= bttv_querybuf,
>   .vidioc_qbuf= bttv_qbuf,
> diff --git a/drivers/media/pci/cobalt/cobalt-v4l2.c 
> b/drivers/media/pci/cobalt/cobalt-v4l2.c
> index 4a0205aae4b4..c088de551081 100644
> --- a/drivers/media/pci/cobalt/cobalt-v4l2.c
> +++ b/drivers/media/pci/cobalt/cobalt-v4l2.c
> @@ -1077,20 +1077,22 @@ static int cobalt_g_parm(struct file *file, void *fh, 
> struct v4l2_streamparm *a)
>   return 0;
>  }
>  
> -static int cobalt_cropcap(struct file *file, void *fh, struct v4l2_cropcap 
> *cc)
> +static int cobalt_g_pixelaspect(struct file *file, void *fh,
> + int type, struct v4l2_fract *f)
>  {
>   struct cobalt_stream *s = video_drvdata(file);
>   struct v4l2_dv_timings timings;
>   int err = 0;
>  
> - if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> + if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>   return -EINVAL;
> +
>   if (s->input == 1)
>   timings = cea1080p60;
>   else
>   err = v4l2_subdev_call(s->sd, video, g_dv_timings, );
>   if (!err)
> - cc->pixelaspect = v4l2_dv_timings_aspect_ratio();
> + *f = v4l2_dv_timings_aspect_ratio();
>   return err;
>  }
>  
> @@ -1132,7 +1134,7 @@ static const st

Re: [RFC PATCH 10/11] v4l2-ioctl: remove unused vidioc_g/s_crop

2018-10-05 Thread Niklas Söderlund
Hi Hans,

Thanks for your work.

On 2018-10-05 09:49:10 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> Now that all drivers have dropped vidioc_g/s_crop we can remove
> support for them in the V4L2 core.
> 
> Signed-off-by: Hans Verkuil 

If the quirk patch hurt my head this makes me smile!

Reviewed-by: Niklas Söderlund 

> ---
>  drivers/media/v4l2-core/v4l2-dev.c   | 4 ++--
>  drivers/media/v4l2-core/v4l2-ioctl.c | 4 
>  include/media/v4l2-ioctl.h   | 8 
>  3 files changed, 2 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-dev.c 
> b/drivers/media/v4l2-core/v4l2-dev.c
> index 69e775930fc4..d81141d51faa 100644
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -621,9 +621,9 @@ static void determine_valid_ioctls(struct video_device 
> *vdev)
>   SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, 
> vidioc_try_decoder_cmd);
>   SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, 
> vidioc_enum_framesizes);
>   SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, 
> vidioc_enum_frameintervals);
> - if (ops->vidioc_g_crop || ops->vidioc_g_selection)
> + if (ops->vidioc_g_selection)
>   set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
> - if (ops->vidioc_s_crop || ops->vidioc_s_selection)
> + if (ops->vidioc_s_selection)
>   set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
>   SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
>   SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c 
> b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 63a92285de39..a59954d351a2 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -2207,8 +2207,6 @@ static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
>   };
>   int ret;
>  
> - if (ops->vidioc_g_crop)
> - return ops->vidioc_g_crop(file, fh, p);
>   /* simulate capture crop using selection api */
>  
>   /* crop means compose for output devices */
> @@ -2239,8 +2237,6 @@ static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
>   .r = p->c,
>   };
>  
> - if (ops->vidioc_s_crop)
> - return ops->vidioc_s_crop(file, fh, p);
>   /* simulate capture crop using selection api */
>  
>   /* crop means compose for output devices */
> diff --git a/include/media/v4l2-ioctl.h b/include/media/v4l2-ioctl.h
> index 5848d92c30da..85fdd3f4b8ad 100644
> --- a/include/media/v4l2-ioctl.h
> +++ b/include/media/v4l2-ioctl.h
> @@ -222,10 +222,6 @@ struct v4l2_fh;
>   *   :ref:`VIDIOC_S_MODULATOR ` ioctl
>   * @vidioc_cropcap: pointer to the function that implements
>   *   :ref:`VIDIOC_CROPCAP ` ioctl
> - * @vidioc_g_crop: pointer to the function that implements
> - *   :ref:`VIDIOC_G_CROP ` ioctl
> - * @vidioc_s_crop: pointer to the function that implements
> - *   :ref:`VIDIOC_S_CROP ` ioctl
>   * @vidioc_g_selection: pointer to the function that implements
>   *   :ref:`VIDIOC_G_SELECTION ` ioctl
>   * @vidioc_s_selection: pointer to the function that implements
> @@ -493,10 +489,6 @@ struct v4l2_ioctl_ops {
>   /* Crop ioctls */
>   int (*vidioc_cropcap)(struct file *file, void *fh,
> struct v4l2_cropcap *a);
> - int (*vidioc_g_crop)(struct file *file, void *fh,
> -  struct v4l2_crop *a);
> - int (*vidioc_s_crop)(struct file *file, void *fh,
> -          const struct v4l2_crop *a);
>   int (*vidioc_g_selection)(struct file *file, void *fh,
> struct v4l2_selection *s);
>   int (*vidioc_s_selection)(struct file *file, void *fh,
> -- 
> 2.18.0
> 

-- 
Regards,
Niklas Söderlund


Re: [RFC PATCH 03/11] v4l2-ioctl: add QUIRK_INVERTED_CROP

2018-10-05 Thread Niklas Söderlund
Hi Hans,

Thanks for your patch.

On 2018-10-05 09:49:03 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> Some old Samsung drivers use the legacy crop API incorrectly:
> the crop and compose targets are swapped. Normally VIDIOC_G_CROP
> will return the CROP rectangle of a CAPTURE stream and the COMPOSE
> rectangle of an OUTPUT stream.
> 
> The Samsung drivers do the opposite. Note that these drivers predate
> the selection API.
> 
> If this 'QUIRK' flag is set, then the v4l2-ioctl core will swap
> the CROP and COMPOSE targets as well.
> 
> That way backwards compatibility is ensured and we can convert the
> Samsung drivers to the selection API.
> 
> Signed-off-by: Hans Verkuil 

I understand the need for this quirk but it make my head hurt :-)

Reviewed-by: Niklas Söderlund 

> ---
>  drivers/media/v4l2-core/v4l2-ioctl.c | 17 -
>  include/media/v4l2-dev.h | 13 +++--
>  2 files changed, 27 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c 
> b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 9c2370e4d05c..63a92285de39 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -2200,6 +2200,7 @@ static int v4l_s_selection(const struct v4l2_ioctl_ops 
> *ops,
>  static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
>   struct file *file, void *fh, void *arg)
>  {
> + struct video_device *vfd = video_devdata(file);
>   struct v4l2_crop *p = arg;
>   struct v4l2_selection s = {
>   .type = p->type,
> @@ -2216,6 +2217,10 @@ static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
>   else
>   s.target = V4L2_SEL_TGT_CROP;
>  
> + if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, >flags))
> + s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
> + V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
> +
>   ret = v4l_g_selection(ops, file, fh, );
>  
>   /* copying results to old structure on success */
> @@ -2227,6 +2232,7 @@ static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
>  static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
>   struct file *file, void *fh, void *arg)
>  {
> + struct video_device *vfd = video_devdata(file);
>   struct v4l2_crop *p = arg;
>   struct v4l2_selection s = {
>   .type = p->type,
> @@ -2243,12 +2249,17 @@ static int v4l_s_crop(const struct v4l2_ioctl_ops 
> *ops,
>   else
>   s.target = V4L2_SEL_TGT_CROP;
>  
> + if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, >flags))
> + s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
> + V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
> +
>   return v4l_s_selection(ops, file, fh, );
>  }
>  
>  static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
>   struct file *file, void *fh, void *arg)
>  {
> + struct video_device *vfd = video_devdata(file);
>   struct v4l2_cropcap *p = arg;
>   struct v4l2_selection s = { .type = p->type };
>   int ret = 0;
> @@ -2285,13 +2296,17 @@ static int v4l_cropcap(const struct v4l2_ioctl_ops 
> *ops,
>   else
>   s.target = V4L2_SEL_TGT_CROP_BOUNDS;
>  
> + if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, >flags))
> + s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
> + V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
> +
>   ret = v4l_g_selection(ops, file, fh, );
>   if (ret)
>   return ret;
>   p->bounds = s.r;
>  
>   /* obtaining defrect */
> - if (V4L2_TYPE_IS_OUTPUT(p->type))
> + if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
>   s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
>   else
>   s.target = V4L2_SEL_TGT_CROP_DEFAULT;
> diff --git a/include/media/v4l2-dev.h b/include/media/v4l2-dev.h
> index 456ac13eca1d..48531e57cc5a 100644
> --- a/include/media/v4l2-dev.h
> +++ b/include/media/v4l2-dev.h
> @@ -74,10 +74,19 @@ struct v4l2_ctrl_handler;
>   *   indicates that file->private_data points to  v4l2_fh.
>   *   This flag is set by the core when v4l2_fh_init() is called.
>   *   All new drivers should use it.
> + * @V4L2_FL_QUIRK_INVERTED_CROP:
> + *   some old M2M drivers use g/s_crop/cropcap incorrectly: crop and
> + *   compose are swapped. If this flag is set, then the selection
> + *   targets are swapped in the g/s_crop/cropcap functions in v4l2-ioctl.c.
> + *   This allows those drivers to correctly implement the selection API,
> + *   but the old crop API will still work as expec

Re: [RFC PATCH 02/11] v4l2-common.h: put backwards compat defines under #ifndef __KERNEL__

2018-10-05 Thread Niklas Söderlund
Hi Hans,

Thanks for your patch.

On 2018-10-05 09:49:02 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> This ensures that they won't be used in kernel code.
> 
> Signed-off-by: Hans Verkuil 

Reviewed-by: Niklas Söderlund 

> ---
>  include/uapi/linux/v4l2-common.h | 28 +++-
>  1 file changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/include/uapi/linux/v4l2-common.h 
> b/include/uapi/linux/v4l2-common.h
> index 4f7b892377cd..7d21c1634b4d 100644
> --- a/include/uapi/linux/v4l2-common.h
> +++ b/include/uapi/linux/v4l2-common.h
> @@ -79,24 +79,11 @@
>  /* Current composing area plus all padding pixels */
>  #define V4L2_SEL_TGT_COMPOSE_PADDED  0x0103
>  
> -/* Backward compatibility target definitions --- to be removed. */
> -#define V4L2_SEL_TGT_CROP_ACTIVE V4L2_SEL_TGT_CROP
> -#define V4L2_SEL_TGT_COMPOSE_ACTIVE  V4L2_SEL_TGT_COMPOSE
> -#define V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL  V4L2_SEL_TGT_CROP
> -#define V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL V4L2_SEL_TGT_COMPOSE
> -#define V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS  V4L2_SEL_TGT_CROP_BOUNDS
> -#define V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS V4L2_SEL_TGT_COMPOSE_BOUNDS
> -
>  /* Selection flags */
>  #define V4L2_SEL_FLAG_GE (1 << 0)
>  #define V4L2_SEL_FLAG_LE (1 << 1)
>  #define V4L2_SEL_FLAG_KEEP_CONFIG(1 << 2)
>  
> -/* Backward compatibility flag definitions --- to be removed. */
> -#define V4L2_SUBDEV_SEL_FLAG_SIZE_GE V4L2_SEL_FLAG_GE
> -#define V4L2_SUBDEV_SEL_FLAG_SIZE_LE V4L2_SEL_FLAG_LE
> -#define V4L2_SUBDEV_SEL_FLAG_KEEP_CONFIG V4L2_SEL_FLAG_KEEP_CONFIG
> -
>  struct v4l2_edid {
>   __u32 pad;
>   __u32 start_block;
> @@ -105,4 +92,19 @@ struct v4l2_edid {
>   __u8  *edid;
>  };
>  
> +#ifndef __KERNEL__
> +/* Backward compatibility target definitions --- to be removed. */
> +#define V4L2_SEL_TGT_CROP_ACTIVE V4L2_SEL_TGT_CROP
> +#define V4L2_SEL_TGT_COMPOSE_ACTIVE  V4L2_SEL_TGT_COMPOSE
> +#define V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL  V4L2_SEL_TGT_CROP
> +#define V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL V4L2_SEL_TGT_COMPOSE
> +#define V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS  V4L2_SEL_TGT_CROP_BOUNDS
> +#define V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS V4L2_SEL_TGT_COMPOSE_BOUNDS
> +
> +/* Backward compatibility flag definitions --- to be removed. */
> +#define V4L2_SUBDEV_SEL_FLAG_SIZE_GE V4L2_SEL_FLAG_GE
> +#define V4L2_SUBDEV_SEL_FLAG_SIZE_LE V4L2_SEL_FLAG_LE
> +#define V4L2_SUBDEV_SEL_FLAG_KEEP_CONFIG V4L2_SEL_FLAG_KEEP_CONFIG
> +#endif
> +
>  #endif /* __V4L2_COMMON__ */
> -- 
> 2.18.0
> 

-- 
Regards,
Niklas Söderlund


Re: [RFC PATCH 01/11] v4l2-ioctl: don't use CROP/COMPOSE_ACTIVE

2018-10-05 Thread Niklas Söderlund
Hi Hans,

Thanks for your work.

On 2018-10-05 09:49:01 +0200, Hans Verkuil wrote:
> From: Hans Verkuil 
> 
> Drop the deprecated _ACTIVE part.
> 
> Signed-off-by: Hans Verkuil 

Reviewed-by: Niklas Söderlund 

> ---
>  drivers/media/v4l2-core/v4l2-ioctl.c | 8 
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c 
> b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 7de041bae84f..9c2370e4d05c 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -2212,9 +2212,9 @@ static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
>  
>   /* crop means compose for output devices */
>   if (V4L2_TYPE_IS_OUTPUT(p->type))
> - s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
> + s.target = V4L2_SEL_TGT_COMPOSE;
>   else
> - s.target = V4L2_SEL_TGT_CROP_ACTIVE;
> + s.target = V4L2_SEL_TGT_CROP;
>  
>   ret = v4l_g_selection(ops, file, fh, );
>  
> @@ -2239,9 +2239,9 @@ static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
>  
>   /* crop means compose for output devices */
>   if (V4L2_TYPE_IS_OUTPUT(p->type))
> - s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
> + s.target = V4L2_SEL_TGT_COMPOSE;
>   else
> - s.target = V4L2_SEL_TGT_CROP_ACTIVE;
> + s.target = V4L2_SEL_TGT_CROP;
>  
>   return v4l_s_selection(ops, file, fh, );
>  }
> -- 
> 2.18.0
> 

-- 
Regards,
Niklas Söderlund


[PATCH v2 3/3] rcar-vin: declare which VINs can use a Up Down Scaler (UDS)

2018-10-04 Thread Niklas Söderlund
Add information about which VINs on which SoC have access to a UDS
scaler.

Signed-off-by: Niklas Söderlund 
---
 drivers/media/platform/rcar-vin/rcar-core.c | 18 ++
 1 file changed, 18 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 01e418c2d4c6792e..337ae8bbe1e0b14c 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -919,12 +919,21 @@ static const struct rvin_group_route 
rcar_info_r8a7795_routes[] = {
{ /* Sentinel */ }
 };
 
+static const struct rvin_group_scaler rcar_info_h3_m3w_m3n_scalers[] = {
+   { .vin = 0, .companion = 1 },
+   { .vin = 1, .companion = 0 },
+   { .vin = 4, .companion = 5 },
+   { .vin = 5, .companion = 4 },
+   { /* Sentinel */ }
+};
+
 static const struct rvin_info rcar_info_r8a7795 = {
.model = RCAR_GEN3,
.use_mc = true,
.max_width = 4096,
.max_height = 4096,
.routes = rcar_info_r8a7795_routes,
+   .scalers = rcar_info_h3_m3w_m3n_scalers,
 };
 
 static const struct rvin_group_route rcar_info_r8a7795es1_routes[] = {
@@ -979,6 +988,7 @@ static const struct rvin_info rcar_info_r8a7795es1 = {
.max_width = 4096,
.max_height = 4096,
.routes = rcar_info_r8a7795es1_routes,
+   .scalers = rcar_info_h3_m3w_m3n_scalers,
 };
 
 static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
@@ -1019,6 +1029,7 @@ static const struct rvin_info rcar_info_r8a7796 = {
.max_width = 4096,
.max_height = 4096,
.routes = rcar_info_r8a7796_routes,
+   .scalers = rcar_info_h3_m3w_m3n_scalers,
 };
 
 static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
@@ -1063,6 +1074,7 @@ static const struct rvin_info rcar_info_r8a77965 = {
.max_width = 4096,
.max_height = 4096,
.routes = rcar_info_r8a77965_routes,
+   .scalers = rcar_info_h3_m3w_m3n_scalers,
 };
 
 static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
@@ -1088,12 +1100,18 @@ static const struct rvin_group_route 
rcar_info_r8a77995_routes[] = {
{ /* Sentinel */ }
 };
 
+static const struct rvin_group_scaler rcar_info_r8a77995_scalers[] = {
+   { .vin = 4, .companion = -1 },
+   { /* Sentinel */ }
+};
+
 static const struct rvin_info rcar_info_r8a77995 = {
.model = RCAR_GEN3,
.use_mc = true,
.max_width = 4096,
.max_height = 4096,
.routes = rcar_info_r8a77995_routes,
+   .scalers = rcar_info_r8a77995_scalers,
 };
 
 static const struct of_device_id rvin_of_id_table[] = {
-- 
2.19.0



Re: [PATCH 3/5] media: dt-bindings: media: rcar_vin: Add r8a774a1 support

2018-09-17 Thread Niklas Söderlund
Hi Biju,

Thanks for your patch.

On 2018-09-10 15:31:16 +0100, Biju Das wrote:
> Document RZ/G2M (R8A774A1) SoC bindings.
> 
> The RZ/G2M SoC is similar to R-Car M3-W (R8A7796).
> 
> Signed-off-by: Biju Das 
> Reviewed-by: Fabrizio Castro 

Acked-by: Niklas Söderlund 

> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index 2f42005..8c81689 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -12,6 +12,7 @@ on Gen3 platforms to a CSI-2 receiver.
>   - compatible: Must be one or more of the following
> - "renesas,vin-r8a7743" for the R8A7743 device
> - "renesas,vin-r8a7745" for the R8A7745 device
> +   - "renesas,vin-r8a774a1" for the R8A774A1 device
> - "renesas,vin-r8a7778" for the R8A7778 device
> - "renesas,vin-r8a7779" for the R8A7779 device
> - "renesas,vin-r8a7790" for the R8A7790 device
> @@ -58,9 +59,9 @@ The per-board settings Gen2 platforms:
>  - data-enable-active: polarity of CLKENB signal, see [1] for
>description. Default is active high.
>  
> -The per-board settings Gen3 platforms:
> +The per-board settings Gen3 and RZ/G2 platforms:
>  
> -Gen3 platforms can support both a single connected parallel input source
> +Gen3 and RZ/G2 platforms can support both a single connected parallel input 
> source
>  from external SoC pins (port@0) and/or multiple parallel input sources
>  from local SoC CSI-2 receivers (port@1) depending on SoC.
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


[PATCH v2] media: rcar-vin: enable support for r8a77965

2018-05-28 Thread Niklas Söderlund
Add the SoC specific information for Renesas r8a77965.

Signed-off-by: Niklas Söderlund 
Reviewed-by: Laurent Pinchart 

---

* Changes since v1
- Fixed typo in variable name,
  s/_rcar_info_r8a77965_routes/rcar_info_r8a77965_routes/.
- Collected Laurents tag.
---
 drivers/media/platform/rcar-vin/rcar-core.c | 48 +
 1 file changed, 48 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index d3072e166a1ca24f..e6a010ee4ba1f671 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -974,6 +974,50 @@ static const struct rvin_info rcar_info_r8a7796 = {
.routes = rcar_info_r8a7796_routes,
 };
 
+static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
+   { /* Sentinel */ }
+};
+
+static const struct rvin_info rcar_info_r8a77965 = {
+   .model = RCAR_GEN3,
+   .use_mc = true,
+   .max_width = 4096,
+   .max_height = 4096,
+   .routes = rcar_info_r8a77965_routes,
+};
+
 static const struct rvin_group_route _rcar_info_r8a77970_routes[] = {
{ .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
{ .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
@@ -1030,6 +1074,10 @@ static const struct of_device_id rvin_of_id_table[] = {
.compatible = "renesas,vin-r8a7796",
.data = _info_r8a7796,
},
+   {
+   .compatible = "renesas,vin-r8a77965",
+   .data = _info_r8a77965,
+   },
{
.compatible = "renesas,vin-r8a77970",
.data = _info_r8a77970,
-- 
2.17.0



[PATCH v2] rcar-vin: sync which hardware buffer to start capture from

2018-05-28 Thread Niklas Söderlund
When starting the VIN capture procedure we are not guaranteed that the
first buffer written to is VnMB1 to which we assigned the first buffer
queued. This is problematic for two reasons. Buffers might not be
dequeued in the same order they where queued for capture. Future
features planed for the VIN driver is support for outputting frames in
SEQ_TB/BT format and to do that it's important that capture starts from
the first buffer slot, VnMB1.

We are guaranteed that capturing always happens in sequence (VnMB1 ->
VnMB2 -> VnMB3 -> VnMB1). So drop up to two frames when starting
capturing so that the driver always returns buffers in the same order
they are queued and prepare for SEQ_TB/BT output.

Signed-off-by: Niklas Söderlund 

---

* Changes since v1
- Fix spelling in commit message pointed out by Sergei.
  s/writing/written/ and s/outputing/outputting/.
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 16 +++-
 drivers/media/platform/rcar-vin/rcar-vin.h |  2 ++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index ac07f99e3516a620..cfe5d7a9d44ee0e1 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -856,7 +856,7 @@ static int rvin_capture_start(struct rvin_dev *vin)
/* Continuous Frame Capture Mode */
rvin_write(vin, VNFC_C_FRAME, VNFC_REG);
 
-   vin->state = RUNNING;
+   vin->state = STARTING;
 
return 0;
 }
@@ -910,6 +910,20 @@ static irqreturn_t rvin_irq(int irq, void *data)
vnms = rvin_read(vin, VNMS_REG);
slot = (vnms & VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
 
+   /*
+* To hand buffers back in a known order to userspace start
+* to capture first from slot 0.
+*/
+   if (vin->state == STARTING) {
+   if (slot != 0) {
+   vin_dbg(vin, "Starting sync slot: %d\n", slot);
+   goto done;
+   }
+
+   vin_dbg(vin, "Capture start synced!\n");
+   vin->state = RUNNING;
+   }
+
/* Capture frame */
if (vin->queue_buf[slot]) {
vin->queue_buf[slot]->field = vin->format.field;
diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
b/drivers/media/platform/rcar-vin/rcar-vin.h
index c2aef789b491ab31..ff747e22d8cfb643 100644
--- a/drivers/media/platform/rcar-vin/rcar-vin.h
+++ b/drivers/media/platform/rcar-vin/rcar-vin.h
@@ -53,11 +53,13 @@ enum rvin_csi_id {
 
 /**
  * STOPPED  - No operation in progress
+ * STARTING - Capture starting up
  * RUNNING  - Operation in progress have buffers
  * STOPPING - Stopping operation
  */
 enum rvin_dma_state {
STOPPED = 0,
+   STARTING,
RUNNING,
STOPPING,
 };
-- 
2.17.0



[PATCH v2] dt-bindings: media: rcar_vin: fix style for ports and endpoints

2018-05-28 Thread Niklas Söderlund
The style for referring to ports and endpoint are wrong. Refer to them
using lowercase and a unit address, port@x and endpoint@x.

Signed-off-by: Niklas Söderlund 
Reported-by: Geert Uytterhoeven 
Reviewed-by: Rob Herring 

---

* Changes since v1.
- Fixed spelling reported by Sergei, s/then/than/.
- Collected Rob's tag.
---
 .../devicetree/bindings/media/rcar_vin.txt| 20 +--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
b/Documentation/devicetree/bindings/media/rcar_vin.txt
index c2c57dcf73f4851b..f8b50f8a3a0bd5d3 100644
--- a/Documentation/devicetree/bindings/media/rcar_vin.txt
+++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
@@ -45,23 +45,23 @@ The per-board settings Gen2 platforms:
 The per-board settings Gen3 platforms:
 
 Gen3 platforms can support both a single connected parallel input source
-from external SoC pins (port0) and/or multiple parallel input sources
-from local SoC CSI-2 receivers (port1) depending on SoC.
+from external SoC pins (port@0) and/or multiple parallel input sources
+from local SoC CSI-2 receivers (port@1) depending on SoC.
 
 - renesas,id - ID number of the VIN, VINx in the documentation.
 - ports
-- port 0 - sub-node describing a single endpoint connected to the VIN
+- port@0 - sub-node describing a single endpoint connected to the VIN
   from external SoC pins described in video-interfaces.txt[1].
-  Describing more then one endpoint in port 0 is invalid. Only VIN
-  instances that are connected to external pins should have port 0.
-- port 1 - sub-nodes describing one or more endpoints connected to
+  Describing more than one endpoint in port@0 is invalid. Only VIN
+  instances that are connected to external pins should have port@0.
+- port@1 - sub-nodes describing one or more endpoints connected to
   the VIN from local SoC CSI-2 receivers. The endpoint numbers must
   use the following schema.
 
-- Endpoint 0 - sub-node describing the endpoint connected to CSI20
-- Endpoint 1 - sub-node describing the endpoint connected to CSI21
-- Endpoint 2 - sub-node describing the endpoint connected to CSI40
-- Endpoint 3 - sub-node describing the endpoint connected to CSI41
+- endpoint@0 - sub-node describing the endpoint connected to CSI20
+- endpoint@1 - sub-node describing the endpoint connected to CSI21
+- endpoint@2 - sub-node describing the endpoint connected to CSI40
+- endpoint@3 - sub-node describing the endpoint connected to CSI41
 
 Device node example for Gen2 platforms
 --
-- 
2.17.0



Re: [ANN] Meeting to discuss improvements to support MC-based cameras on generic apps

2018-05-28 Thread Niklas Söderlund
Hi Mauro,

On 2018-05-28 10:43:51 -0300, Mauro Carvalho Chehab wrote:
> Em Thu, 17 May 2018 16:07:08 -0300
> Mauro Carvalho Chehab <mchehab+sams...@kernel.org> escreveu:
> 
> > Hi all,
> > 
> > The goal of this e-mail is to schedule a meeting in order to discuss
> > improvements at the media subsystem in order to support complex camera
> > hardware by usual apps.
> > 
> > The main focus here is to allow supporting devices with MC-based
> > hardware connected to a camera.
> > 
> > In short, my proposal is to meet with the interested parties on solving
> > this issue during the Open Source Summit in Japan, e. g. between
> > June, 19-22, in Tokyo.
> 
> Let's schedule the meeting to happen in Tokyo, Japan at June, 19.

I will be in Tokyo at that time and would be happy to join.

> 
> Location yet to be defined, but it will either be together with
> OSS Japan or at Google. I'll confirm the address tomorrow.
> 
> Regards,
> Mauro
> 
> 
> 
> Thanks,
> Mauro

-- 
Regards,
Niklas Söderlund


Re: [PATCH v4 5/9] media: rcar-vin: Parse parallel input on Gen3

2018-05-28 Thread Niklas Söderlund
Hi Jacopo,

On 2018-05-25 13:50:08 +0200, Jacopo Mondi wrote:
> Hi Niklas,
>I might have another question before sending v5.
> 
> On Fri, May 25, 2018 at 12:29:44AM +0200, Niklas Söderlund wrote:
> > Hi Jacopo,
> >
> > Thanks for your work.
> >
> > I really like what you did with this patch in v4.
> >
> > On 2018-05-25 00:02:15 +0200, Jacopo Mondi wrote:
> > > The rcar-vin driver so far had a mutually exclusive code path for
> > > handling parallel and CSI-2 video input subdevices, with only the CSI-2
> > > use case supporting media-controller. As we add support for parallel
> > > inputs to Gen3 media-controller compliant code path now parse both port@0
> > > and port@1, handling the media-controller use case in the parallel
> > > bound/unbind notifier operations.
> > >
> > > Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> > >
> > > ---
> > > v3 -> v4:
> > > - Change the mc/parallel initialization order. Initialize mc first, then
> > >   parallel
> > > - As a consequence no need to delay parallel notifiers registration, the
> > >   media controller is set up already when parallel input got parsed,
> > >   this greatly simplify the group notifier complete callback.
> > > ---
> > >  drivers/media/platform/rcar-vin/rcar-core.c | 56 
> > > ++---
> > >  1 file changed, 35 insertions(+), 21 deletions(-)
> > >
> > > diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> > > b/drivers/media/platform/rcar-vin/rcar-core.c
> > > index a799684..29619c2 100644
> > > --- a/drivers/media/platform/rcar-vin/rcar-core.c
> > > +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> > > @@ -399,6 +399,11 @@ static int rvin_parallel_subdevice_attach(struct 
> > > rvin_dev *vin,
> > >   ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
> > >   vin->parallel->sink_pad = ret < 0 ? 0 : ret;
> > >
> > > + if (vin->info->use_mc) {
> > > + vin->parallel->subdev = subdev;
> > > + return 0;
> > > + }
> > > +
> > >   /* Find compatible subdevices mbus format */
> > >   vin->mbus_code = 0;
> > >   code.index = 0;
> > > @@ -460,10 +465,12 @@ static int rvin_parallel_subdevice_attach(struct 
> > > rvin_dev *vin,
> > >  static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
> > >  {
> > >   rvin_v4l2_unregister(vin);
> > > - v4l2_ctrl_handler_free(>ctrl_handler);
> > > -
> > > - vin->vdev.ctrl_handler = NULL;
> > >   vin->parallel->subdev = NULL;
> > > +
> > > + if (!vin->info->use_mc) {
> > > + v4l2_ctrl_handler_free(>ctrl_handler);
> > > + vin->vdev.ctrl_handler = NULL;
> > > + }
> > >  }
> > >
> > >  static int rvin_parallel_notify_complete(struct v4l2_async_notifier 
> > > *notifier)
> > > @@ -552,18 +559,18 @@ static int rvin_parallel_parse_v4l2(struct device 
> > > *dev,
> > >   return 0;
> > >  }
> > >
> > > -static int rvin_parallel_graph_init(struct rvin_dev *vin)
> > > +static int rvin_parallel_init(struct rvin_dev *vin)
> > >  {
> > >   int ret;
> > >
> > > - ret = v4l2_async_notifier_parse_fwnode_endpoints(
> > > - vin->dev, >notifier,
> > > - sizeof(struct rvin_parallel_entity), rvin_parallel_parse_v4l2);
> > > + ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
> > > + vin->dev, >notifier, sizeof(struct rvin_parallel_entity),
> > > + 0, rvin_parallel_parse_v4l2);
> > >   if (ret)
> > >   return ret;
> > >
> > >   if (!vin->parallel)
> > > - return -ENODEV;
> > > + return -ENOTCONN;
> >
> > I think you still should return -ENODEV here if !vin->info->use_mc to
> > preserve Gen2 which runs without media controller behavior. How about:
> >
> > return vin->info->use_mc ? -ENOTCONN : -ENODEV;
> >
> > >
> > >   vin_dbg(vin, "Found parallel subdevice %pOF\n",
> > >   to_of_node(vin->parallel->asd.match.fwnode));
> > > @@ -784,14 +791,8 @@ static int rvin_mc_init(struct rvin_dev *vin)
> > >  {
> > >   int ret;
> > >
> > > - vin->pad.flags = MEDIA_PAD_FL_SINK;
> > > - ret = media_entity_pads_init(>vdev.entity, 1

Re: [PATCH v4 5/9] media: rcar-vin: Parse parallel input on Gen3

2018-05-24 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

I really like what you did with this patch in v4.

On 2018-05-25 00:02:15 +0200, Jacopo Mondi wrote:
> The rcar-vin driver so far had a mutually exclusive code path for
> handling parallel and CSI-2 video input subdevices, with only the CSI-2
> use case supporting media-controller. As we add support for parallel
> inputs to Gen3 media-controller compliant code path now parse both port@0
> and port@1, handling the media-controller use case in the parallel
> bound/unbind notifier operations.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> 
> ---
> v3 -> v4:
> - Change the mc/parallel initialization order. Initialize mc first, then
>   parallel
> - As a consequence no need to delay parallel notifiers registration, the
>   media controller is set up already when parallel input got parsed,
>   this greatly simplify the group notifier complete callback.
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 56 
> ++---
>  1 file changed, 35 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index a799684..29619c2 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -399,6 +399,11 @@ static int rvin_parallel_subdevice_attach(struct 
> rvin_dev *vin,
>   ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
>   vin->parallel->sink_pad = ret < 0 ? 0 : ret;
>  
> + if (vin->info->use_mc) {
> + vin->parallel->subdev = subdev;
> + return 0;
> + }
> +
>   /* Find compatible subdevices mbus format */
>   vin->mbus_code = 0;
>   code.index = 0;
> @@ -460,10 +465,12 @@ static int rvin_parallel_subdevice_attach(struct 
> rvin_dev *vin,
>  static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
>  {
>   rvin_v4l2_unregister(vin);
> - v4l2_ctrl_handler_free(>ctrl_handler);
> -
> - vin->vdev.ctrl_handler = NULL;
>   vin->parallel->subdev = NULL;
> +
> + if (!vin->info->use_mc) {
> + v4l2_ctrl_handler_free(>ctrl_handler);
> + vin->vdev.ctrl_handler = NULL;
> + }
>  }
>  
>  static int rvin_parallel_notify_complete(struct v4l2_async_notifier 
> *notifier)
> @@ -552,18 +559,18 @@ static int rvin_parallel_parse_v4l2(struct device *dev,
>   return 0;
>  }
>  
> -static int rvin_parallel_graph_init(struct rvin_dev *vin)
> +static int rvin_parallel_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - ret = v4l2_async_notifier_parse_fwnode_endpoints(
> - vin->dev, >notifier,
> - sizeof(struct rvin_parallel_entity), rvin_parallel_parse_v4l2);
> + ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
> + vin->dev, >notifier, sizeof(struct rvin_parallel_entity),
> + 0, rvin_parallel_parse_v4l2);
>   if (ret)
>   return ret;
>  
>   if (!vin->parallel)
> - return -ENODEV;
> + return -ENOTCONN;

I think you still should return -ENODEV here if !vin->info->use_mc to 
preserve Gen2 which runs without media controller behavior. How about:

return vin->info->use_mc ? -ENOTCONN : -ENODEV;

>  
>   vin_dbg(vin, "Found parallel subdevice %pOF\n",
>   to_of_node(vin->parallel->asd.match.fwnode));
> @@ -784,14 +791,8 @@ static int rvin_mc_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - vin->pad.flags = MEDIA_PAD_FL_SINK;
> - ret = media_entity_pads_init(>vdev.entity, 1, >pad);
> - if (ret)
> - return ret;
> -
> - ret = rvin_group_get(vin);
> - if (ret)
> - return ret;
> + if (!vin->info->use_mc)
> + return 0;
>  
>   ret = rvin_mc_parse_of_graph(vin);
>   if (ret)
> @@ -1074,11 +1075,24 @@ static int rcar_vin_probe(struct platform_device 
> *pdev)
>   return ret;
>  
>   platform_set_drvdata(pdev, vin);
> - if (vin->info->use_mc)
> - ret = rvin_mc_init(vin);
> - else
> - ret = rvin_parallel_graph_init(vin);
> - if (ret < 0)
> +
> + if (vin->info->use_mc) {
> + vin->pad.flags = MEDIA_PAD_FL_SINK;
> + ret = media_entity_pads_init(>vdev.entity, 1, >pad);
> + if (ret)
> + return ret;
> +
> + ret = rvin_group_get(vin);
> + if (ret)
> +         return ret;
> + }

I don't see why you need to move the media pad creation out of 
rvin_mc_init(). With the reorder of the rvin_mc_init() 
rvin_parallel_init() I would keep this in rvin_mc_init().

> +
> + ret = rvin_mc_init(vin);
> + if (ret)
> + return ret;
> +
> + ret = rvin_parallel_init(vin);
> + if (ret < 0 && ret != -ENOTCONN)
>   goto error;
>  
>   pm_suspend_ignore_children(>dev, true);
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 8/9] media: rcar-vin: Rename _rcar_info to rcar_info

2018-05-24 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-18 16:40:44 +0200, Jacopo Mondi wrote:
> Remove trailing underscore to align all rcar_group_route structure
> declarations.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

With Sergei's comment addressed:

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index dcebb42..b740f41 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -1060,7 +1060,7 @@ static const struct rvin_info rcar_info_r8a7796 = {
>   .routes = rcar_info_r8a7796_routes,
>  };
>  
> -static const struct rvin_group_route _rcar_info_r8a77970_routes[] = {
> +static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
>   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
>   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
>   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
> @@ -1076,7 +1076,7 @@ static const struct rvin_info rcar_info_r8a77970 = {
>   .use_mc = true,
>   .max_width = 4096,
>   .max_height = 4096,
> - .routes = _rcar_info_r8a77970_routes,
> + .routes = rcar_info_r8a77970_routes,
>  };
>  
>  static const struct of_device_id rvin_of_id_table[] = {
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 7/9] media: rcar-vin: Handle parallel subdev in link_notify

2018-05-24 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-18 16:40:43 +0200, Jacopo Mondi wrote:
> Handle parallel subdevices in link_notify callback. If the notified link
> involves a parallel subdevice, do not change routing of the VIN-CSI-2
> devices and mark the VIN instance as using a parallel input. If the
> CSI-2 link setup succeeds instead, mark the VIN instance as using CSI-2.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 36 
> +++--
>  1 file changed, 34 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index d13bbcf..dcebb42 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -168,10 +168,37 @@ static int rvin_group_link_notify(struct media_link 
> *link, u32 flags,
>   }
>  
>   /* Add the new link to the existing mask and check if it works. */
> - csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);
>   channel = rvin_group_csi_pad_to_channel(link->source->index);

I think you should move the channel = ...; bellow the new if statement 
the same way you do with mask_new.

> - mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
> + csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);

Missing new line.

With these two nits fixed:

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> + if (csi_id == -ENODEV) {
> + struct v4l2_subdev *sd;
> + unsigned int i;
> +
> + /*
> +  * Make sure the source entity subdevice is registered as
> +  * a parallel input of one of the enabled VINs if it is not
> +  * one of the CSI-2 subdevices.
> +  *
> +  * No hardware configuration required for parallel inputs,
> +  * we can return here.
> +  */
> + sd = media_entity_to_v4l2_subdev(link->source->entity);
> + for (i = 0; i < RCAR_VIN_NUM; i++) {
> + if (group->vin[i] && group->vin[i]->parallel &&
> + group->vin[i]->parallel->subdev == sd) {
> + group->vin[i]->is_csi = false;
> + ret = 0;
> + goto out;
> + }
> + }
> +
> + vin_err(vin, "Subdevice %s not registered to any VIN\n",
> + link->source->entity->name);
> + ret = -ENODEV;
> + goto out;
> + }
>  
> + mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
>   vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new);
>  
>   if (!mask_new) {
> @@ -181,6 +208,11 @@ static int rvin_group_link_notify(struct media_link 
> *link, u32 flags,
>  
>   /* New valid CHSEL found, set the new value. */
>   ret = rvin_set_channel_routing(group->vin[master_id], __ffs(mask_new));
> + if (ret)
> + goto out;
> +
> + vin->is_csi = true;
> +
>  out:
>   mutex_unlock(>lock);
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 6/9] media: rcar-vin: Link parallel input media entities

2018-05-24 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-18 16:40:42 +0200, Jacopo Mondi wrote:
> When running with media-controller link the parallel input
> media entities with the VIN entities at 'complete' callback time.
> 
> To create media links the v4l2_device should be registered first.
> Check if the device is already registered, to avoid double registrations.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 26 --
>  1 file changed, 24 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index 745e8ee..d13bbcf 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -478,6 +478,8 @@ static void rvin_parallel_subdevice_detach(struct 
> rvin_dev *vin)
>  static int rvin_parallel_notify_complete(struct v4l2_async_notifier 
> *notifier)
>  {
>   struct rvin_dev *vin = notifier_to_vin(notifier);
> + struct media_entity *source;
> + struct media_entity *sink;
>   int ret;
>  
>   ret = v4l2_device_register_subdev_nodes(>v4l2_dev);
> @@ -486,7 +488,26 @@ static int rvin_parallel_notify_complete(struct 
> v4l2_async_notifier *notifier)
>   return ret;
>   }
>  
> - return rvin_v4l2_register(vin);
> + if (!video_is_registered(>vdev)) {
> + ret = rvin_v4l2_register(vin);
> + if (ret < 0)
> + return ret;
> + }
> +
> + if (!vin->info->use_mc)
> + return 0;
> +
> + /* If we're running with media-controller, link the subdevs. */
> + source = >parallel->subdev->entity;
> + sink = >vdev.entity;
> +
> + ret = media_create_pad_link(source, vin->parallel->source_pad,
> + sink, vin->parallel->sink_pad, 0);
> + if (ret)
> + vin_err(vin, "Error adding link from %s to %s: %d\n",
> + source->name, sink->name, ret);
> +
> + return ret;
>  }
>  
>  static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
> @@ -611,7 +632,8 @@ static int rvin_group_notify_complete(struct 
> v4l2_async_notifier *notifier)
>  
>   /* Register all video nodes for the group. */
>   for (i = 0; i < RCAR_VIN_NUM; i++) {
> - if (vin->group->vin[i]) {
> + if (vin->group->vin[i] &&
> + !video_is_registered(>group->vin[i]->vdev)) {
>   ret = rvin_v4l2_register(vin->group->vin[i]);
>   if (ret)
>   return ret;
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 5/9] media: rcar-vin: Parse parallel input on Gen3

2018-05-24 Thread Niklas Söderlund
subdevice %pOF\n",
>   to_of_node(vin->parallel->asd.match.fwnode));
>  
> + /* If we use media-controller, notifier registration is post-poned. */
> + if (vin->info->use_mc)
> + return 0;

Is this really needed? Se comment about CSI-2 and parallel ordering in 
rcar_vin_probe() bellow.

> +
>   vin->notifier.ops = _parallel_notify_ops;
>   ret = v4l2_async_notifier_register(>v4l2_dev, >notifier);
>   if (ret < 0) {
> @@ -766,6 +781,30 @@ static int rvin_mc_parse_of_graph(struct rvin_dev *vin)
>  
>   mutex_unlock(>group->lock);
>  
> + /*
> +  * Go and register all notifiers for parallel subdevs, and
> +  * the group notifier for CSI-2 subdevs, if any.
> +  */
> + for (i = 0; i < RCAR_VIN_NUM; i++) {
> + struct rvin_dev *ivin = vin->group->vin[i];
> + struct v4l2_async_notifier *inotifier;
> +
> + if (!ivin)
> + continue;
> +
> + inotifier = >notifier;
> + if (!inotifier->num_subdevs)
> + continue;
> +
> + inotifier->ops = _parallel_notify_ops;
> + ret = v4l2_async_notifier_register(>v4l2_dev, inotifier);
> + if (ret < 0) {
> + vin_err(ivin,
> + "Notifier registration failed: %d\n", ret);
> + goto error_unregister_notifiers;
> + }
> + }
> +
>   if (!vin->group->notifier.num_subdevs)
>   return 0;
>  
> @@ -773,25 +812,29 @@ static int rvin_mc_parse_of_graph(struct rvin_dev *vin)
>   ret = v4l2_async_notifier_register(>v4l2_dev,
>  >group->notifier);
>   if (ret < 0) {
> - vin_err(vin, "Notifier registration failed\n");
> + vin_err(vin, "Notifier registration failed: %d\n", ret);

I won't object to this, if you think the return value is useful for 
user-space please keep it. But break it out to it's own patch :-)

>   return ret;
>   }
>  
>   return 0;
> +
> +error_unregister_notifiers:
> + for (i = 0; i < RCAR_VIN_NUM; i++) {
> + if (!vin->group->vin[i])
> + continue;
> +
> + v4l2_async_notifier_unregister(>group->vin[i]->notifier);
> + }
> +
> + return ret;
>  }
>  
>  static int rvin_mc_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - vin->pad.flags = MEDIA_PAD_FL_SINK;
> - ret = media_entity_pads_init(>vdev.entity, 1, >pad);
> - if (ret)
> - return ret;
> -
> - ret = rvin_group_get(vin);
> - if (ret)
> - return ret;
> + if (!vin->info->use_mc)
> + return 0;
>  
>   ret = rvin_mc_parse_of_graph(vin);
>   if (ret)
> @@ -1074,13 +1117,26 @@ static int rcar_vin_probe(struct platform_device 
> *pdev)
>   return ret;
>  
>   platform_set_drvdata(pdev, vin);
> - if (vin->info->use_mc)
> - ret = rvin_mc_init(vin);
> - else
> - ret = rvin_parallel_graph_init(vin);
> - if (ret < 0)
> +
> +     if (vin->info->use_mc) {
> + vin->pad.flags = MEDIA_PAD_FL_SINK;
> + ret = media_entity_pads_init(>vdev.entity, 1, >pad);
> + if (ret)
> + return ret;
> +
> + ret = rvin_group_get(vin);
> + if (ret)
> + return ret;
> + }
> +
> + ret = rvin_parallel_init(vin);
> + if (ret < 0 && ret != -ENOTCONN)
>   goto error;
>  
> + ret = rvin_mc_init(vin);
> + if (ret)
> + return ret;
> +

I think that if you first call rvin_mc_init() and then 
rvin_parallel_init() you can remove the delay of registering the VIN 
local notifier as the media device will have then been created and there 
is no reason to delay the notifier registration. And in the process this 
patch becomes much simpler (I think).

>   pm_suspend_ignore_children(>dev, true);
>   pm_runtime_enable(>dev);
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 4/9] media: rcar-vin: Cache the mbus configuration flags

2018-05-24 Thread Niklas Söderlund
 @@ -659,8 +659,12 @@ static int rvin_setup(struct rvin_dev *vin)
>   break;
>   case MEDIA_BUS_FMT_UYVY8_2X8:
>   /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
> - vnmc |= vin->mbus_cfg.type == V4L2_MBUS_BT656 ?
> - VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
> + if (!vin->is_csi &&
> + vin->parallel->mbus_type == V4L2_MBUS_BT656)
> + vnmc |= VNMC_INF_YUV8_BT656;
> + else
> + vnmc |= VNMC_INF_YUV8_BT601;
> +
>   input_is_yuv = true;
>   break;
>   case MEDIA_BUS_FMT_RGB888_1X24:
> @@ -668,8 +672,12 @@ static int rvin_setup(struct rvin_dev *vin)
>   break;
>   case MEDIA_BUS_FMT_UYVY10_2X10:
>   /* BT.656 10bit YCbCr422 or BT.601 10bit YCbCr422 */
> - vnmc |= vin->mbus_cfg.type == V4L2_MBUS_BT656 ?
> - VNMC_INF_YUV10_BT656 : VNMC_INF_YUV10_BT601;
> + if (!vin->is_csi &&
> + vin->parallel->mbus_type == V4L2_MBUS_BT656)
> + vnmc |= VNMC_INF_YUV10_BT656;
> + else
> + vnmc |= VNMC_INF_YUV10_BT601;
> +
>   input_is_yuv = true;
>   break;
>   default:
> @@ -683,11 +691,13 @@ static int rvin_setup(struct rvin_dev *vin)
>   dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
>  
>   /* Hsync Signal Polarity Select */
> - if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
> + if (!vin->is_csi &&
> + !(vin->parallel->mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
>   dmr2 |= VNDMR2_HPS;
>  
>   /* Vsync Signal Polarity Select */
> - if (!(vin->mbus_cfg.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
> + if (!vin->is_csi &&
> + !(vin->parallel->mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
>   dmr2 |= VNDMR2_VPS;

Would it not make sens to have nested if-statements here?

if (!vin->is_csi) {
if (!(vin->parallel->mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
...

...
}

>  
>   /*
> @@ -734,7 +744,7 @@ static int rvin_setup(struct rvin_dev *vin)
>  
>   if (vin->info->model == RCAR_GEN3) {
>   /* Select between CSI-2 and parallel input */
> - if (vin->mbus_cfg.type == V4L2_MBUS_CSI2)
> + if (vin->is_csi)
>   vnmc &= ~VNMC_DPINE;
>   else
>   vnmc |= VNMC_DPINE;
> diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
> b/drivers/media/platform/rcar-vin/rcar-vin.h
> index 7d0ffe08..c5f7fd1 100644
> --- a/drivers/media/platform/rcar-vin/rcar-vin.h
> +++ b/drivers/media/platform/rcar-vin/rcar-vin.h
> @@ -73,16 +73,22 @@ struct rvin_video_format {
>  };
>  
>  /**
> - * struct rvin_graph_entity - Video endpoint from async framework
> + * struct rvin_parallel_entity - Parallel video input endpoint descriptor
>   * @asd: sub-device descriptor for async framework
>   * @subdev:  subdevice matched using async framework
> + * @mbus_type:   media bus type
> + * @mbus_flags:  media bus configuration flags
>   * @source_pad:  source pad of remote subdevice
>   * @sink_pad:sink pad of remote subdevice
> + *
>   */
> -struct rvin_graph_entity {
> +struct rvin_parallel_entity {
>   struct v4l2_async_subdev asd;
>   struct v4l2_subdev *subdev;
>  
> + enum v4l2_mbus_type mbus_type;
> + unsigned int mbus_flags;
> +
>   unsigned int source_pad;
>   unsigned int sink_pad;
>  };
> @@ -146,7 +152,8 @@ struct rvin_info {
>   * @v4l2_dev:V4L2 device
>   * @ctrl_handler:V4L2 control handler
>   * @notifier:V4L2 asynchronous subdevs notifier
> - * @parallel:entity in the DT for local parallel subdevice
> + *
> + * @parallel:parallel input subdevice descriptor
>   *
>   * @group:   Gen3 CSI group
>   * @id:  Gen3 group id for this VIN
> @@ -164,7 +171,8 @@ struct rvin_info {
>   * @sequence:V4L2 buffers sequence number
>   * @state:   keeps track of operation state
>   *
> - * @mbus_cfg:media bus configuration from DT
> + * @is_csi:  flag to mark the VIN as using a CSI-2 subdevice
> + *
>   * @mbus_code:   media bus format code
>   * @format:  active V4L2 pixel format
>   *
> @@ -182,7 +190,8 @@ struct rvin_dev {
>   struct v4l2_device v4l2_dev;
>   struct v4l2_ctrl_handler ctrl_handler;
>   struct v4l2_async_notifier notifier;
> - struct rvin_graph_entity *parallel;
> +
> + struct rvin_parallel_entity *parallel;
>  
>   struct rvin_group *group;
>   unsigned int id;
> @@ -199,7 +208,8 @@ struct rvin_dev {
>   unsigned int sequence;
>   enum rvin_dma_state state;
>  
> - struct v4l2_mbus_config mbus_cfg;
> + bool is_csi;
> +
>   u32 mbus_code;
>   struct v4l2_pix_format format;
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 3/9] media: rcar-vin: Create a group notifier

2018-05-24 Thread Niklas Söderlund
quot;Notifier registration failed\n");
>   return ret;
> @@ -1112,15 +1106,10 @@ static int rcar_vin_remove(struct platform_device 
> *pdev)
>   v4l2_async_notifier_unregister(>notifier);
>   v4l2_async_notifier_cleanup(>notifier);

If the vin being removed is the one which v4l2_dev is used to register 
the group you should unregister the group notifier here. You got this 
for 'free' with the above code before this change so it's easy to miss 
:-)

>  
> - if (vin->info->use_mc) {
> - mutex_lock(>group->lock);
> - if (vin->group->notifier == >notifier)
> - vin->group->notifier = NULL;
> - mutex_unlock(>group->lock);
> + if (vin->info->use_mc)
>   rvin_group_put(vin);
> - } else {
> + else
>   v4l2_ctrl_handler_free(>ctrl_handler);
> - }
>  
>   rvin_dma_unregister(vin);
>  
> diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
> b/drivers/media/platform/rcar-vin/rcar-vin.h
> index 755ac3c..7d0ffe08 100644
> --- a/drivers/media/platform/rcar-vin/rcar-vin.h
> +++ b/drivers/media/platform/rcar-vin/rcar-vin.h
> @@ -209,6 +209,8 @@ struct rvin_dev {
>   v4l2_std_id std;
>  };
>  
> +#define v4l2_dev_to_vin(d)   container_of(d, struct rvin_dev, v4l2_dev)

I think you should either define this in rcar-core.c (see 
notifier_to_vin()) or use the container_of() directly to limit it's 
scope.

Over all I'm not happy about my initial usage these defines in the 
driver, I was young and dumb :-) I won't object if you wish to keep it 
but then please move it to the .c file.

> +
>  #define vin_to_source(vin)   ((vin)->parallel->subdev)

This in particular I hate and at some point I hope to remove it or move 
it to rcar-v4l2.c. :-) But that is a task for later and not related to 
your patch-set.

>  
>  /* Debug */
> @@ -225,8 +227,7 @@ struct rvin_dev {
>   *
>   * @lock:protects the count, notifier, vin and csi members
>   * @count:   number of enabled VIN instances found in DT
> - * @notifier:pointer to the notifier of a VIN which handles 
> the
> - *   groups async sub-devices.
> + * @notifier:group notifier for CSI-2 async subdevices
>   * @vin: VIN instances which are part of the group
>   * @csi: array of pairs of fwnode and subdev pointers
>   *   to all CSI-2 subdevices.
> @@ -238,7 +239,7 @@ struct rvin_group {
>  
>   struct mutex lock;
>   unsigned int count;
> - struct v4l2_async_notifier *notifier;
> + struct v4l2_async_notifier notifier;
>   struct rvin_dev *vin[RCAR_VIN_NUM];
>  
>   struct {
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 2/9] media: rcar-vin: Remove two empty lines

2018-05-23 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-18 16:40:38 +0200, Jacopo Mondi wrote:
> Remove un-necessary empty lines.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 2 --
>  1 file changed, 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index 6b80f98..1aadd90 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -707,11 +707,9 @@ static int rvin_mc_parse_of_endpoint(struct device *dev,
>   return -EINVAL;
>  
>   if (!of_device_is_available(to_of_node(asd->match.fwnode))) {
> -
>   vin_dbg(vin, "OF device %pOF disabled, ignoring\n",
>   to_of_node(asd->match.fwnode));
>   return -ENOTCONN;
> -
>   }
>  
>   if (vin->group->csi[vep->base.id].fwnode) {
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v3 1/9] media: rcar-vin: Rename 'digital' to 'parallel'

2018-05-23 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-18 16:40:37 +0200, Jacopo Mondi wrote:
> As the term 'digital' is used all over the rcar-vin code in place of
> 'parallel', rename all the occurrencies.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 72 
> ++---
>  drivers/media/platform/rcar-vin/rcar-dma.c  |  4 +-
>  drivers/media/platform/rcar-vin/rcar-v4l2.c | 12 ++---
>  drivers/media/platform/rcar-vin/rcar-vin.h  |  6 +--
>  4 files changed, 47 insertions(+), 47 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index d3072e1..6b80f98 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -376,12 +376,12 @@ static int rvin_find_pad(struct v4l2_subdev *sd, int 
> direction)
>  }
>  
>  /* 
> -
> - * Digital async notifier
> + * Parallel async notifier
>   */
>  
>  /* The vin lock should be held when calling the subdevice attach and detach 
> */
> -static int rvin_digital_subdevice_attach(struct rvin_dev *vin,
> -  struct v4l2_subdev *subdev)
> +static int rvin_parallel_subdevice_attach(struct rvin_dev *vin,
> +   struct v4l2_subdev *subdev)
>  {
>   struct v4l2_subdev_mbus_code_enum code = {
>   .which = V4L2_SUBDEV_FORMAT_ACTIVE,
> @@ -392,15 +392,15 @@ static int rvin_digital_subdevice_attach(struct 
> rvin_dev *vin,
>   ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
>   if (ret < 0)
>   return ret;
> - vin->digital->source_pad = ret;
> + vin->parallel->source_pad = ret;
>  
>   ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
> - vin->digital->sink_pad = ret < 0 ? 0 : ret;
> + vin->parallel->sink_pad = ret < 0 ? 0 : ret;
>  
>   /* Find compatible subdevices mbus format */
>   vin->mbus_code = 0;
>   code.index = 0;
> - code.pad = vin->digital->source_pad;
> + code.pad = vin->parallel->source_pad;
>   while (!vin->mbus_code &&
>  !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, )) {
>   code.index++;
> @@ -450,21 +450,21 @@ static int rvin_digital_subdevice_attach(struct 
> rvin_dev *vin,
>  
>   vin->vdev.ctrl_handler = >ctrl_handler;
>  
> - vin->digital->subdev = subdev;
> + vin->parallel->subdev = subdev;
>  
>   return 0;
>  }
>  
> -static void rvin_digital_subdevice_detach(struct rvin_dev *vin)
> +static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
>  {
>   rvin_v4l2_unregister(vin);
>   v4l2_ctrl_handler_free(>ctrl_handler);
>  
>   vin->vdev.ctrl_handler = NULL;
> - vin->digital->subdev = NULL;
> + vin->parallel->subdev = NULL;
>  }
>  
> -static int rvin_digital_notify_complete(struct v4l2_async_notifier *notifier)
> +static int rvin_parallel_notify_complete(struct v4l2_async_notifier 
> *notifier)
>  {
>   struct rvin_dev *vin = notifier_to_vin(notifier);
>   int ret;
> @@ -478,28 +478,28 @@ static int rvin_digital_notify_complete(struct 
> v4l2_async_notifier *notifier)
>   return rvin_v4l2_register(vin);
>  }
>  
> -static void rvin_digital_notify_unbind(struct v4l2_async_notifier *notifier,
> -struct v4l2_subdev *subdev,
> -struct v4l2_async_subdev *asd)
> +static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
> +     struct v4l2_subdev *subdev,
> + struct v4l2_async_subdev *asd)

When I run my indentation script this indentation changes from spaces to 
all tabs. If possible I would like to keep that as I usually run it on 
these files before submitting any patches, but it's not a big deal.

Whit this fixed, thanks for clearing this up!

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

>  {
>   struct rvin_dev *vin = notifier_to_vin(notifier);
>  
> - vin_dbg(vin, "unbind digital subdev %s\n", subdev->name);
> + vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
>  
>   mutex_lock(>lock);
> - rvin_digital_subdevice_detach(vin);
> + rvin_parallel_subdevice_detach(vin);
>   mutex_unlock(>lock);
>  }
>  
> -static int rvin_digital_notify_bound(struct v4l2_async_notifier *no

Re: [PATCH v2 3/4] media: rcar-vin: Handle CLOCKENB pin polarity

2018-05-22 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-21 19:27:42 +0200, Jacopo Mondi wrote:
> Handle CLOCKENB pin polarity, or use HSYNC in its place if polarity is
> not specified and we're running on parallel data bus with explicit
> synchronism signals.
> 
> While at there, simplify the media bus handling flags logic, inspecting
> flags only if the system is running on parallel media bus type and ignore
> flags when on CSI-2. Also change comments style to remove un-necessary
> camel case and add a full stop at the end of sentences.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-dma.c | 34 
> ++
>  1 file changed, 25 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
> b/drivers/media/platform/rcar-vin/rcar-dma.c
> index 17f291f..ffd3d62 100644
> --- a/drivers/media/platform/rcar-vin/rcar-dma.c
> +++ b/drivers/media/platform/rcar-vin/rcar-dma.c
> @@ -123,6 +123,8 @@
>  /* Video n Data Mode Register 2 bits */
>  #define VNDMR2_VPS   (1 << 30)
>  #define VNDMR2_HPS   (1 << 29)
> +#define VNDMR2_CES   (1 << 28)
> +#define VNDMR2_CHS   (1 << 23)
>  #define VNDMR2_FTEV  (1 << 17)
>  #define VNDMR2_VLV(n)((n & 0xf) << 12)
>  
> @@ -684,21 +686,35 @@ static int rvin_setup(struct rvin_dev *vin)
>   break;
>   }
>  
> - /* Enable VSYNC Field Toogle mode after one VSYNC input */
> + /* Enable VSYNC field toggle mode after one VSYNC input. */
>   if (vin->info->model == RCAR_GEN3)
>   dmr2 = VNDMR2_FTEV;
>   else
>   dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
>  
> - /* Hsync Signal Polarity Select */
> - if (!vin->is_csi &&
> - !(vin->parallel->mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
> - dmr2 |= VNDMR2_HPS;
> + /* Synchronism signal polarities: only for parallel data bus. */
> + if (!vin->is_csi) {
> + /* Hsync signal polarity select. */
> + if (!(vin->parallel->mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
> + dmr2 |= VNDMR2_HPS;
>  
> - /* Vsync Signal Polarity Select */
> - if (!vin->is_csi &&
> - !(vin->parallel->mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
> - dmr2 |= VNDMR2_VPS;
> + /* Vsync signal polarity select. */
> + if (!(vin->parallel->mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
> + dmr2 |= VNDMR2_VPS;
> +
> + /*
> +  * Data enable signal polarity select.
> +  * Use HSYNC as data-enable if not specified and running
> +  * with explicit synchronizations; otherwise default 'high'
> +  * is selected.
> +  */
> + if (vin->parallel->mbus_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
> + dmr2 |= VNDMR2_CES;
> + else if (!(vin->parallel->mbus_flags &
> +  V4L2_MBUS_DATA_ACTIVE_HIGH) &&
> +  vin->parallel->mbus_type == V4L2_MBUS_PARALLEL)
> + dmr2 |= VNDMR2_CHS;

As I stated in v1, I think this expression is more complex then is relly
needed here. Would not something like this be much more readable?

/* Clock Enable signal polarity select. */
if (vin->parallel->mbus_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
dmr2 |= VNDMR2_CES;

/* Use HSYNC as clock enable if VIn_CLKENB is not available. */
if (!(vin->parallel->mbus_flags & (V4L2_MBUS_DATA_ACTIVE_LOW | 
V4L2_MBUS_DATA_ACTIVE_HIGH)))
dmr2 |= VNDMR2_CHS;


> + }
>  
>   /*
>* Output format
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 2/4] dt-bindings: media: rcar-vin: Document data-active

2018-05-22 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-21 19:27:41 +0200, Jacopo Mondi wrote:
> Document 'data-active' property in R-Car VIN device tree bindings.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> 
> v1 -> v2:
> - HSYNC is used in place of data enable signal only when running with
>   explicit synchronizations.
> - The property is no more mandatory when running with embedded
>   synchronizations, and default is selected.
> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 6 ++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index dab3118..2c144b4 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -64,6 +64,12 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>   If both HSYNC and VSYNC polarities are not specified, embedded
>   synchronization is selected.
>  
> +- data-active: data enable signal line polarity (CLKENB pin).
> +  0/1 for LOW/HIGH respectively. If not specified and running with
> +   embedded synchronization, the default is active high. If not
> +   specified and running with explicit synchronization, HSYNC is used
> +   as data enable signal.

This indentation looks funny :-)

If you check the rest of the rcar_vin.txt file only spaces are used for
indentation.

> +
>  - port 1 - sub-nodes describing one or more endpoints connected to
>the VIN from local SoC CSI-2 receivers. The endpoint numbers must
>use the following schema.
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 1/4] dt-bindings: media: rcar-vin: Describe optional ep properties

2018-05-22 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-21 19:27:40 +0200, Jacopo Mondi wrote:
> Describe the optional properties for endpoint nodes of port@0
> and port@1 of the R-Car VIN driver device tree bindings documentation.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 13 -
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index 5c6f2a7..dab3118 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -54,6 +54,16 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>from external SoC pins described in video-interfaces.txt[1].
>Describing more then one endpoint in port 0 is invalid. Only VIN
>instances that are connected to external pins should have port 0.
> +
> +  - Optional properties for endpoint nodes of port@0:
> +- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH
> +   respectively. Default is active high.
> +- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH
> +   respectively. Default is active high.
> +
> + If both HSYNC and VSYNC polarities are not specified, embedded
> + synchronization is selected.
> +
>  - port 1 - sub-nodes describing one or more endpoints connected to
>the VIN from local SoC CSI-2 receivers. The endpoint numbers must
>use the following schema.
> @@ -63,6 +73,8 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>  - Endpoint 2 - sub-node describing the endpoint connected to CSI40
>  - Endpoint 3 - sub-node describing the endpoint connected to CSI41
>  
> +  Endpoint nodes of port@1 do not support any optional endpoint property.
> +
>  Device node example for Gen2 platforms
>  --
>  
> @@ -113,7 +125,6 @@ Board setup example for Gen2 platforms (vin1 composite 
> video input)
>  
>  vin1ep0: endpoint {
>  remote-endpoint = <>;
> -bus-width = <8>;

Until we have figured out if we should document or remove the bus-width 
parameter maybe move this to 4/4 ?


>  };
>  };
>  };
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


[PATCH v2 2/2] v4l: Add support for STD ioctls on subdev nodes

2018-05-17 Thread Niklas Söderlund
There is no way to control the standard of subdevices which are part of
a media device. The ioctls which exists all target video devices
explicitly and the idea is that the video device should talk to the
subdevice. For subdevices part of a media graph this is not possible and
the standard must be controlled on the subdev device directly.

Add four new ioctls to be able to directly interact with subdevices and
control the video standard; VIDIOC_SUBDEV_ENUMSTD, VIDIOC_SUBDEV_G_STD,
VIDIOC_SUBDEV_S_STD and VIDIOC_SUBDEV_QUERYSTD.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

---

* Changes since v1
- Added VIDIOC_SUBDEV_ENUMSTD.
---
 .../media/uapi/v4l/vidioc-enumstd.rst | 11 ++
 Documentation/media/uapi/v4l/vidioc-g-std.rst | 14 
 .../media/uapi/v4l/vidioc-querystd.rst| 11 ++
 drivers/media/v4l2-core/v4l2-subdev.c | 22 +++
 include/uapi/linux/v4l2-subdev.h  |  4 
 5 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/Documentation/media/uapi/v4l/vidioc-enumstd.rst 
b/Documentation/media/uapi/v4l/vidioc-enumstd.rst
index b7fda29f46a139a0..2644a62acd4b6822 100644
--- a/Documentation/media/uapi/v4l/vidioc-enumstd.rst
+++ b/Documentation/media/uapi/v4l/vidioc-enumstd.rst
@@ -2,14 +2,14 @@
 
 .. _VIDIOC_ENUMSTD:
 
-
-ioctl VIDIOC_ENUMSTD
-
+***
+ioctl VIDIOC_ENUMSTD, VIDIOC_SUBDEV_ENUMSTD
+***
 
 Name
 
 
-VIDIOC_ENUMSTD - Enumerate supported video standards
+VIDIOC_ENUMSTD - VIDIOC_SUBDEV_ENUMSTD - Enumerate supported video standards
 
 
 Synopsis
@@ -18,6 +18,9 @@ Synopsis
 .. c:function:: int ioctl( int fd, VIDIOC_ENUMSTD, struct v4l2_standard *argp )
 :name: VIDIOC_ENUMSTD
 
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_ENUMSTD, struct v4l2_standard 
*argp )
+:name: VIDIOC_SUBDEV_ENUMSTD
+
 
 Arguments
 =
diff --git a/Documentation/media/uapi/v4l/vidioc-g-std.rst 
b/Documentation/media/uapi/v4l/vidioc-g-std.rst
index 90791ab51a5371b8..8d94f0404df270db 100644
--- a/Documentation/media/uapi/v4l/vidioc-g-std.rst
+++ b/Documentation/media/uapi/v4l/vidioc-g-std.rst
@@ -2,14 +2,14 @@
 
 .. _VIDIOC_G_STD:
 
-
-ioctl VIDIOC_G_STD, VIDIOC_S_STD
-
+**
+ioctl VIDIOC_G_STD, VIDIOC_S_STD, VIDIOC_SUBDEV_G_STD, VIDIOC_SUBDEV_S_STD
+**
 
 Name
 
 
-VIDIOC_G_STD - VIDIOC_S_STD - Query or select the video standard of the 
current input
+VIDIOC_G_STD - VIDIOC_S_STD - VIDIOC_SUBDEV_G_STD - VIDIOC_SUBDEV_S_STD - 
Query or select the video standard of the current input
 
 
 Synopsis
@@ -21,6 +21,12 @@ Synopsis
 .. c:function:: int ioctl( int fd, VIDIOC_S_STD, const v4l2_std_id *argp )
 :name: VIDIOC_S_STD
 
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_G_STD, v4l2_std_id *argp )
+:name: VIDIOC_SUBDEV_G_STD
+
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_S_STD, const v4l2_std_id 
*argp )
+:name: VIDIOC_SUBDEV_S_STD
+
 
 Arguments
 =
diff --git a/Documentation/media/uapi/v4l/vidioc-querystd.rst 
b/Documentation/media/uapi/v4l/vidioc-querystd.rst
index cf40bca19b9f8665..a8385cc7481869dd 100644
--- a/Documentation/media/uapi/v4l/vidioc-querystd.rst
+++ b/Documentation/media/uapi/v4l/vidioc-querystd.rst
@@ -2,14 +2,14 @@
 
 .. _VIDIOC_QUERYSTD:
 
-*
-ioctl VIDIOC_QUERYSTD
-*
+*
+ioctl VIDIOC_QUERYSTD, VIDIOC_SUBDEV_QUERYSTD
+*
 
 Name
 
 
-VIDIOC_QUERYSTD - Sense the video standard received by the current input
+VIDIOC_QUERYSTD - VIDIOC_SUBDEV_QUERYSTD - Sense the video standard received 
by the current input
 
 
 Synopsis
@@ -18,6 +18,9 @@ Synopsis
 .. c:function:: int ioctl( int fd, VIDIOC_QUERYSTD, v4l2_std_id *argp )
 :name: VIDIOC_QUERYSTD
 
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_QUERYSTD, v4l2_std_id *argp )
+:name: VIDIOC_SUBDEV_QUERYSTD
+
 
 Arguments
 =
diff --git a/drivers/media/v4l2-core/v4l2-subdev.c 
b/drivers/media/v4l2-core/v4l2-subdev.c
index f9eed938d3480b74..27a2c633f2323f5f 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -494,6 +494,28 @@ static long subdev_do_ioctl(struct file *file, unsigned 
int cmd, void *arg)
 
case VIDIOC_SUBDEV_S_DV_TIMINGS:
return v4l2_subdev_call(sd, video, s_dv_timings, arg);
+
+   case VIDIOC_SUBDEV_G_STD:
+   return v4l2_subdev_call(sd, video, g_std, arg);
+
+   case VIDIOC_SUBDEV_S_STD: {
+   v4l2_std_id *std = arg;
+
+   return v4l2_subdev_call(sd, video, s_std, *std);
+   }
+
+   case VIDIOC_SUBDEV_E

[PATCH v2 1/2] v4l2-ioctl: create helper to fill in v4l2_standard for ENUMSTD

2018-05-17 Thread Niklas Söderlund
Prepare for adding a new IOCTL VIDIOC_SUBDEV_ENUMSTD which would
enumerate the standards for a subdevice by breaking out the code which
could be shared between the video and subdevice versions of this IOCTL.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/v4l2-core/v4l2-ioctl.c | 66 
 include/media/v4l2-ioctl.h   | 11 +
 2 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c 
b/drivers/media/v4l2-core/v4l2-ioctl.c
index a40dbec271f1d9fe..1b5893373358ad5e 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -125,6 +125,42 @@ int v4l2_video_std_construct(struct v4l2_standard *vs,
 }
 EXPORT_SYMBOL(v4l2_video_std_construct);
 
+/* Fill in the fields of a v4l2_standard structure according to the
+ * 'id' and 'vs->index' parameters. Returns negative on error. */
+int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
+{
+   v4l2_std_id curr_id = 0;
+   unsigned int index = vs->index, i, j = 0;
+   const char *descr = "";
+
+   /* Return -ENODATA if the id for the current input
+  or output is 0, meaning that it doesn't support this API. */
+   if (id == 0)
+   return -ENODATA;
+
+   /* Return norm array in a canonical way */
+   for (i = 0; i <= index && id; i++) {
+   /* last std value in the standards array is 0, so this
+  while always ends there since (id & 0) == 0. */
+   while ((id & standards[j].std) != standards[j].std)
+   j++;
+   curr_id = standards[j].std;
+   descr = standards[j].descr;
+   j++;
+   if (curr_id == 0)
+   break;
+   if (curr_id != V4L2_STD_PAL &&
+   curr_id != V4L2_STD_SECAM &&
+   curr_id != V4L2_STD_NTSC)
+   id &= ~curr_id;
+   }
+   if (i <= index)
+   return -EINVAL;
+
+   v4l2_video_std_construct(vs, curr_id, descr);
+   return 0;
+}
+
 /* - */
 /* some arrays for pretty-printing debug messages of enum types  */
 
@@ -1753,36 +1789,8 @@ static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
 {
struct video_device *vfd = video_devdata(file);
struct v4l2_standard *p = arg;
-   v4l2_std_id id = vfd->tvnorms, curr_id = 0;
-   unsigned int index = p->index, i, j = 0;
-   const char *descr = "";
-
-   /* Return -ENODATA if the tvnorms for the current input
-  or output is 0, meaning that it doesn't support this API. */
-   if (id == 0)
-   return -ENODATA;
 
-   /* Return norm array in a canonical way */
-   for (i = 0; i <= index && id; i++) {
-   /* last std value in the standards array is 0, so this
-  while always ends there since (id & 0) == 0. */
-   while ((id & standards[j].std) != standards[j].std)
-   j++;
-   curr_id = standards[j].std;
-   descr = standards[j].descr;
-   j++;
-   if (curr_id == 0)
-   break;
-   if (curr_id != V4L2_STD_PAL &&
-   curr_id != V4L2_STD_SECAM &&
-   curr_id != V4L2_STD_NTSC)
-   id &= ~curr_id;
-   }
-   if (i <= index)
-   return -EINVAL;
-
-   v4l2_video_std_construct(p, curr_id, descr);
-   return 0;
+   return v4l_video_std_enumstd(p, vfd->tvnorms);
 }
 
 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
diff --git a/include/media/v4l2-ioctl.h b/include/media/v4l2-ioctl.h
index a7b3f7c75d628b06..fa0463a1b19dc880 100644
--- a/include/media/v4l2-ioctl.h
+++ b/include/media/v4l2-ioctl.h
@@ -642,6 +642,17 @@ void v4l2_video_std_frame_period(int id, struct v4l2_fract 
*frameperiod);
 int v4l2_video_std_construct(struct v4l2_standard *vs,
int id, const char *name);
 
+/**
+ * v4l_video_std_enumstd - Ancillary routine that fills in the fields of
+ * a _standard structure according to the @id and @vs->index
+ * parameters.
+ *
+ * @vs: struct _standard pointer to be filled.
+ * @id: analog TV sdandard ID.
+ *
+ */
+int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id);
+
 /**
  * v4l_printk_ioctl - Ancillary routine that prints the ioctl in a
  * human-readable format.
-- 
2.17.0



[PATCH v2 0/2] v4l: Add support for STD ioctls on subdev nodes

2018-05-17 Thread Niklas Söderlund
Hi Hans,

This series enables the video standards to be controlled directly on the 
subdev device node. This is needed as there is no way to control the 
standard of a subdevice if it's part of a media controller centric setup 
as oppose to a video centric one.

I have tested this on Renesas Gen3 Salvator-XS M3-N using the AFE
subdevice from the adv748x driver together with the R-Car VIN and CSI-2
pipeline. And verified ENUMSTD still works for video device centric 
devices on Renesas Gen2 Koelsch board.

I wrote a prototype patch for v4l2-ctl which adds four new options
(--get-subdev-standard, --get-subdev-standard, --set-subdev-standard and
--get-subdev-detected-standard) to ease testing which I plan to submit
after some cleanup if this patch receives positive feedback.

If you or anyone else is interested in testing this patch the v4l2-utils
prototype patches are available at

git://git.ragnatech.se/v4l-utils#subdev-std

* Changes since v1
- Add VIDIOC_SUBDEV_ENUMSTD.

Niklas Söderlund (2):
  v4l2-ioctl: create helper to fill in v4l2_standard for ENUMSTD
  v4l: Add support for STD ioctls on subdev nodes

 .../media/uapi/v4l/vidioc-enumstd.rst | 11 ++--
 Documentation/media/uapi/v4l/vidioc-g-std.rst | 14 ++--
 .../media/uapi/v4l/vidioc-querystd.rst| 11 ++--
 drivers/media/v4l2-core/v4l2-ioctl.c  | 66 +++
 drivers/media/v4l2-core/v4l2-subdev.c | 22 +++
 include/media/v4l2-ioctl.h| 11 
 include/uapi/linux/v4l2-subdev.h  |  4 ++
 7 files changed, 98 insertions(+), 41 deletions(-)

-- 
2.17.0



Re: [PATCH] v4l: Add support for STD ioctls on subdev nodes

2018-05-17 Thread Niklas Söderlund
Hi Hans,

Thanks for your feedback.

On 2018-05-17 10:04:35 +0200, Hans Verkuil wrote:
> On 17/05/18 04:00, Niklas Söderlund wrote:
> 
> Missing commit log.

I checked other commits doing the same thing, they had no commit log and 
that was enough reason for me to be lazy. Will fix in v2. And thanks for 
forcing me to improve :-)

> 
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > ---
> >  Documentation/media/uapi/v4l/vidioc-g-std.rst| 14 ++
> >  Documentation/media/uapi/v4l/vidioc-querystd.rst | 11 +++
> 
> What about ENUMSTD?

I did check about the possibility to add support for ENUMSTD. When 
digging around I got the feeling ENUMSTD was a videodev specific ioctl.  
As you bring it up here I assume that is not the case, I will see if I 
can add it for v2.

> 
> Regards,
> 
>   Hans
> 
> >  drivers/media/v4l2-core/v4l2-subdev.c| 12 
> >  include/uapi/linux/v4l2-subdev.h |  3 +++
> >  4 files changed, 32 insertions(+), 8 deletions(-)
> > 
> > ---
> > 
> > Hi Hans,
> > 
> > I have tested this on Renesas Gen3 Salvator-XS M3-N using the AFE 
> > subdevice from the adv748x driver together with the R-Car VIN and CSI-2 
> > pipeline.  
> > 
> > I wrote a prototype patch for v4l2-ctl which adds three new options 
> > (--get-subdev-standard, --set-subdev-standard and 
> > --get-subdev-detected-standard) to ease testing which I plan to submit 
> > after some cleanup if this patch receives positive feedback.
> > 
> > If you or anyone else is interested in testing this patch the v4l2-utils 
> > prototype patches are available at
> > 
> > git://git.ragnatech.se/v4l-utils#subdev-std
> > 
> > Regards,
> > // Niklas
> > 
> > diff --git a/Documentation/media/uapi/v4l/vidioc-g-std.rst 
> > b/Documentation/media/uapi/v4l/vidioc-g-std.rst
> > index 90791ab51a5371b8..8d94f0404df270db 100644
> > --- a/Documentation/media/uapi/v4l/vidioc-g-std.rst
> > +++ b/Documentation/media/uapi/v4l/vidioc-g-std.rst
> > @@ -2,14 +2,14 @@
> >  
> >  .. _VIDIOC_G_STD:
> >  
> > -
> > -ioctl VIDIOC_G_STD, VIDIOC_S_STD
> > -
> > +**
> > +ioctl VIDIOC_G_STD, VIDIOC_S_STD, VIDIOC_SUBDEV_G_STD, VIDIOC_SUBDEV_S_STD
> > +**
> >  
> >  Name
> >  
> >  
> > -VIDIOC_G_STD - VIDIOC_S_STD - Query or select the video standard of the 
> > current input
> > +VIDIOC_G_STD - VIDIOC_S_STD - VIDIOC_SUBDEV_G_STD - VIDIOC_SUBDEV_S_STD - 
> > Query or select the video standard of the current input
> >  
> >  
> >  Synopsis
> > @@ -21,6 +21,12 @@ Synopsis
> >  .. c:function:: int ioctl( int fd, VIDIOC_S_STD, const v4l2_std_id *argp )
> >  :name: VIDIOC_S_STD
> >  
> > +.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_G_STD, v4l2_std_id *argp )
> > +:name: VIDIOC_SUBDEV_G_STD
> > +
> > +.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_S_STD, const v4l2_std_id 
> > *argp )
> > +:name: VIDIOC_SUBDEV_S_STD
> > +
> >  
> >  Arguments
> >  =
> > diff --git a/Documentation/media/uapi/v4l/vidioc-querystd.rst 
> > b/Documentation/media/uapi/v4l/vidioc-querystd.rst
> > index cf40bca19b9f8665..a8385cc7481869dd 100644
> > --- a/Documentation/media/uapi/v4l/vidioc-querystd.rst
> > +++ b/Documentation/media/uapi/v4l/vidioc-querystd.rst
> > @@ -2,14 +2,14 @@
> >  
> >  .. _VIDIOC_QUERYSTD:
> >  
> > -*
> > -ioctl VIDIOC_QUERYSTD
> > -*
> > +*
> > +ioctl VIDIOC_QUERYSTD, VIDIOC_SUBDEV_QUERYSTD
> > +*
> >  
> >  Name
> >  
> >  
> > -VIDIOC_QUERYSTD - Sense the video standard received by the current input
> > +VIDIOC_QUERYSTD - VIDIOC_SUBDEV_QUERYSTD - Sense the video standard 
> > received by the current input
> >  
> >  
> >  Synopsis
> > @@ -18,6 +18,9 @@ Synopsis
> >  .. c:function:: int ioctl( int fd, VIDIOC_QUERYSTD, v4l2_std_id *argp )
> >  :name: VIDIOC_QUERYSTD
> >  
> > +.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_QUERYSTD, v4l2_std_id 
> > *argp )
> > +:name: VIDIOC_SUBDEV_QUERYSTD
> > +
> >  
> >  Arguments
> >  =
> > diff --

Re: [PATCH v2 1/4] media: rcar-vin: Parse digital input in mc-path

2018-05-17 Thread Niklas Söderlund
Hi Jacopo,

On 2018-05-17 12:13:06 +0200, Jacopo Mondi wrote:
> Hi Niklas,
>thanks for review.
> 
> On Wed, May 16, 2018 at 10:32:49PM +0200, Niklas Söderlund wrote:
> > Hi Jacopo,
> >
> > Thanks for your work!
> >
> > First let me apologies for the use of the keyword 'digital' in the
> > driver it should have been parallel... Someday we should remedy this.
> >
> > If you touch any parts of the code where such a transition make sens I
> > would not complain about the intermixed use of digital/parallel. Once
> > your work is done we could follow up with a cleanup patch to complete
> > the transition. Or if you rather stick with digital here I'm fine with
> > that too.
> 
> I would go with a major s/digital/parallel/ after this has been
> merged, if that' fine with you.

I'm totally fine whit that.

> >
> > On 2018-05-16 14:16:53 +0200, Jacopo Mondi wrote:
> > > Add support for digital input subdevices to Gen-3 rcar-vin.
> > > The Gen-3, media-controller compliant, version has so far only accepted
> > > CSI-2 input subdevices. Remove assumptions on the supported bus_type and
> > > accepted number of subdevices, and allow digital input connections on 
> > > port@0.
> > >
> > > Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> > > ---
> > >  drivers/media/platform/rcar-vin/rcar-core.c | 99 
> > > +++--
> > >  drivers/media/platform/rcar-vin/rcar-vin.h  | 15 +
> > >  2 files changed, 93 insertions(+), 21 deletions(-)
> > >
> > > diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> > > b/drivers/media/platform/rcar-vin/rcar-core.c
> > > index d3072e1..0ea21ab 100644
> > > --- a/drivers/media/platform/rcar-vin/rcar-core.c
> > > +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> > > @@ -562,7 +562,7 @@ static int rvin_digital_graph_init(struct rvin_dev 
> > > *vin)
> > >   return ret;
> > >
> > >   if (!vin->digital)
> > > - return -ENODEV;
> > > + return -ENOTCONN;
> > >
> > >   vin_dbg(vin, "Found digital subdevice %pOF\n",
> > >   to_of_node(vin->digital->asd.match.fwnode));
> > > @@ -703,15 +703,13 @@ static int rvin_mc_parse_of_endpoint(struct device 
> > > *dev,
> > >  {
> > >   struct rvin_dev *vin = dev_get_drvdata(dev);
> > >
> > > - if (vep->base.port != 1 || vep->base.id >= RVIN_CSI_MAX)
> > > + if (vep->base.port != RVIN_PORT_CSI2 || vep->base.id >= RVIN_CSI_MAX)
> >
> > I don't like this RVIN_PORT_CSI2. It makes the code harder to read as I
> > have have to go and lookup which port RVIN_PORT_CSI2 represents. I would
> 
> Why do you have to go and look? It's an enum, it abstracts away the numerical
> value it represents with a human readable string. If you want to check
> which number it represent you can got and look at the enum definition,
> once. While reading the code, the most important part is "this is the CSI-2
> port" or "this is port 1"? You wrote the driver and for you there is
> no ambiguity there, I understand.
> 
> > rater just keep vep->base.port != 1 as I think it's much clearer whats
> > going on. And it's not as we will move the CSI-2 input to a different
> > port as it's described in the bindings.
> 
> That's one more reason to have an enum for that.
> 
> Anyway, that's pure bikeshedding, I like discussing these things
> too but I'm surely not making an argument for this. If you don't like
> the enum I'll remove that.

I'm sorry, I don't like the enum :-(

> 
> >
> > >   return -EINVAL;
> > >
> > >   if (!of_device_is_available(to_of_node(asd->match.fwnode))) {
> > > -
> > >   vin_dbg(vin, "OF device %pOF disabled, ignoring\n",
> > >   to_of_node(asd->match.fwnode));
> > >   return -ENOTCONN;
> > > -
> > >   }
> > >
> > >   if (vin->group->csi[vep->base.id].fwnode) {
> > > @@ -720,6 +718,8 @@ static int rvin_mc_parse_of_endpoint(struct device 
> > > *dev,
> > >   return -ENOTCONN;
> > >   }
> > >
> > > + vin->mbus_cfg.type = V4L2_MBUS_CSI2;
> > > + vin->mbus_cfg.flags = 0;
> >
> > I like this move of mbus_cfg handling! Makes the two cases more aligned
> > which are good. Unfortunately I fear it needs more work :-(
> >
> > With this series addition of parallel input

[PATCH] v4l: Add support for STD ioctls on subdev nodes

2018-05-16 Thread Niklas Söderlund
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 Documentation/media/uapi/v4l/vidioc-g-std.rst| 14 ++
 Documentation/media/uapi/v4l/vidioc-querystd.rst | 11 +++
 drivers/media/v4l2-core/v4l2-subdev.c| 12 
 include/uapi/linux/v4l2-subdev.h |  3 +++
 4 files changed, 32 insertions(+), 8 deletions(-)

---

Hi Hans,

I have tested this on Renesas Gen3 Salvator-XS M3-N using the AFE 
subdevice from the adv748x driver together with the R-Car VIN and CSI-2 
pipeline.  

I wrote a prototype patch for v4l2-ctl which adds three new options 
(--get-subdev-standard, --set-subdev-standard and 
--get-subdev-detected-standard) to ease testing which I plan to submit 
after some cleanup if this patch receives positive feedback.

If you or anyone else is interested in testing this patch the v4l2-utils 
prototype patches are available at

git://git.ragnatech.se/v4l-utils#subdev-std

Regards,
// Niklas

diff --git a/Documentation/media/uapi/v4l/vidioc-g-std.rst 
b/Documentation/media/uapi/v4l/vidioc-g-std.rst
index 90791ab51a5371b8..8d94f0404df270db 100644
--- a/Documentation/media/uapi/v4l/vidioc-g-std.rst
+++ b/Documentation/media/uapi/v4l/vidioc-g-std.rst
@@ -2,14 +2,14 @@
 
 .. _VIDIOC_G_STD:
 
-
-ioctl VIDIOC_G_STD, VIDIOC_S_STD
-
+**
+ioctl VIDIOC_G_STD, VIDIOC_S_STD, VIDIOC_SUBDEV_G_STD, VIDIOC_SUBDEV_S_STD
+**
 
 Name
 
 
-VIDIOC_G_STD - VIDIOC_S_STD - Query or select the video standard of the 
current input
+VIDIOC_G_STD - VIDIOC_S_STD - VIDIOC_SUBDEV_G_STD - VIDIOC_SUBDEV_S_STD - 
Query or select the video standard of the current input
 
 
 Synopsis
@@ -21,6 +21,12 @@ Synopsis
 .. c:function:: int ioctl( int fd, VIDIOC_S_STD, const v4l2_std_id *argp )
 :name: VIDIOC_S_STD
 
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_G_STD, v4l2_std_id *argp )
+:name: VIDIOC_SUBDEV_G_STD
+
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_S_STD, const v4l2_std_id 
*argp )
+:name: VIDIOC_SUBDEV_S_STD
+
 
 Arguments
 =
diff --git a/Documentation/media/uapi/v4l/vidioc-querystd.rst 
b/Documentation/media/uapi/v4l/vidioc-querystd.rst
index cf40bca19b9f8665..a8385cc7481869dd 100644
--- a/Documentation/media/uapi/v4l/vidioc-querystd.rst
+++ b/Documentation/media/uapi/v4l/vidioc-querystd.rst
@@ -2,14 +2,14 @@
 
 .. _VIDIOC_QUERYSTD:
 
-*
-ioctl VIDIOC_QUERYSTD
-*
+*
+ioctl VIDIOC_QUERYSTD, VIDIOC_SUBDEV_QUERYSTD
+*
 
 Name
 
 
-VIDIOC_QUERYSTD - Sense the video standard received by the current input
+VIDIOC_QUERYSTD - VIDIOC_SUBDEV_QUERYSTD - Sense the video standard received 
by the current input
 
 
 Synopsis
@@ -18,6 +18,9 @@ Synopsis
 .. c:function:: int ioctl( int fd, VIDIOC_QUERYSTD, v4l2_std_id *argp )
 :name: VIDIOC_QUERYSTD
 
+.. c:function:: int ioctl( int fd, VIDIOC_SUBDEV_QUERYSTD, v4l2_std_id *argp )
+:name: VIDIOC_SUBDEV_QUERYSTD
+
 
 Arguments
 =
diff --git a/drivers/media/v4l2-core/v4l2-subdev.c 
b/drivers/media/v4l2-core/v4l2-subdev.c
index f9eed938d3480b74..a156b1812e923721 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -494,6 +494,18 @@ static long subdev_do_ioctl(struct file *file, unsigned 
int cmd, void *arg)
 
case VIDIOC_SUBDEV_S_DV_TIMINGS:
return v4l2_subdev_call(sd, video, s_dv_timings, arg);
+
+   case VIDIOC_SUBDEV_G_STD:
+   return v4l2_subdev_call(sd, video, g_std, arg);
+
+   case VIDIOC_SUBDEV_S_STD: {
+   v4l2_std_id *std = arg;
+
+   return v4l2_subdev_call(sd, video, s_std, *std);
+   }
+
+   case VIDIOC_SUBDEV_QUERYSTD:
+   return v4l2_subdev_call(sd, video, querystd, arg);
 #endif
default:
return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
diff --git a/include/uapi/linux/v4l2-subdev.h b/include/uapi/linux/v4l2-subdev.h
index c95a53e6743cb040..133696a1f324ffdc 100644
--- a/include/uapi/linux/v4l2-subdev.h
+++ b/include/uapi/linux/v4l2-subdev.h
@@ -170,8 +170,11 @@ struct v4l2_subdev_selection {
 #define VIDIOC_SUBDEV_G_SELECTION  _IOWR('V', 61, struct 
v4l2_subdev_selection)
 #define VIDIOC_SUBDEV_S_SELECTION  _IOWR('V', 62, struct 
v4l2_subdev_selection)
 /* The following ioctls are identical to the ioctls in videodev2.h */
+#define VIDIOC_SUBDEV_G_STD_IOR('V', 23, v4l2_std_id)
+#define VIDIOC_SUBDEV_S_STD_IOW('V', 24, v4l2_std_id)
 #define VIDIOC_SUBDEV_G_EDID   _IOWR('V', 40, struct v4l2_edid)
 #define VIDIOC_SUBDEV_S_EDID   _IOWR('V', 41, struct v4l2_edid)
+#

[PATCH] dt-bindings: media: rcar_vin: fix style for ports and endpoints

2018-05-16 Thread Niklas Söderlund
The style for referring to ports and endpoint are wrong. Refer to them
using lowercase and a unit address, port@x and endpoint@x.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reported-by: Geert Uytterhoeven <ge...@linux-m68k.org>
---
 .../devicetree/bindings/media/rcar_vin.txt| 20 +--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
b/Documentation/devicetree/bindings/media/rcar_vin.txt
index c2c57dcf73f4851b..a574b9c037c05a3c 100644
--- a/Documentation/devicetree/bindings/media/rcar_vin.txt
+++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
@@ -45,23 +45,23 @@ The per-board settings Gen2 platforms:
 The per-board settings Gen3 platforms:
 
 Gen3 platforms can support both a single connected parallel input source
-from external SoC pins (port0) and/or multiple parallel input sources
-from local SoC CSI-2 receivers (port1) depending on SoC.
+from external SoC pins (port@0) and/or multiple parallel input sources
+from local SoC CSI-2 receivers (port@1) depending on SoC.
 
 - renesas,id - ID number of the VIN, VINx in the documentation.
 - ports
-- port 0 - sub-node describing a single endpoint connected to the VIN
+- port@0 - sub-node describing a single endpoint connected to the VIN
   from external SoC pins described in video-interfaces.txt[1].
-  Describing more then one endpoint in port 0 is invalid. Only VIN
-  instances that are connected to external pins should have port 0.
-- port 1 - sub-nodes describing one or more endpoints connected to
+  Describing more then one endpoint in port@0 is invalid. Only VIN
+  instances that are connected to external pins should have port@0.
+- port@1 - sub-nodes describing one or more endpoints connected to
   the VIN from local SoC CSI-2 receivers. The endpoint numbers must
   use the following schema.
 
-- Endpoint 0 - sub-node describing the endpoint connected to CSI20
-- Endpoint 1 - sub-node describing the endpoint connected to CSI21
-- Endpoint 2 - sub-node describing the endpoint connected to CSI40
-- Endpoint 3 - sub-node describing the endpoint connected to CSI41
+- endpoint@0 - sub-node describing the endpoint connected to CSI20
+- endpoint@1 - sub-node describing the endpoint connected to CSI21
+- endpoint@2 - sub-node describing the endpoint connected to CSI40
+- endpoint@3 - sub-node describing the endpoint connected to CSI41
 
 Device node example for Gen2 platforms
 --
-- 
2.17.0



[PATCH] rcar-vin: sync which hardware buffer to start capture from

2018-05-16 Thread Niklas Söderlund
When starting the VIN capture procedure we are not guaranteed that the
first buffer writing to is VnMB1 to which we assigned the first buffer
queued. This is problematic for two reasons. Buffers might not be
dequeued in the same order they where queued for capture. Future
features planed for the VIN driver is support for outputing frames in
SEQ_TB/BT format and to do that it's important that capture starts from
the first buffer slot, VnMB1.

We are guaranteed that capturing always happens in sequence (VnMB1 ->
VnMB2 -> VnMB3 -> VnMB1). So drop up to two frames when starting
capturing so that the driver always returns buffers in the same order
they are queued and prepare for SEQ_TB/BT output.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 16 +++-
 drivers/media/platform/rcar-vin/rcar-vin.h |  2 ++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index ac07f99e3516a620..cfe5d7a9d44ee0e1 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -856,7 +856,7 @@ static int rvin_capture_start(struct rvin_dev *vin)
/* Continuous Frame Capture Mode */
rvin_write(vin, VNFC_C_FRAME, VNFC_REG);
 
-   vin->state = RUNNING;
+   vin->state = STARTING;
 
return 0;
 }
@@ -910,6 +910,20 @@ static irqreturn_t rvin_irq(int irq, void *data)
vnms = rvin_read(vin, VNMS_REG);
slot = (vnms & VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
 
+   /*
+* To hand buffers back in a known order to userspace start
+* to capture first from slot 0.
+*/
+   if (vin->state == STARTING) {
+   if (slot != 0) {
+   vin_dbg(vin, "Starting sync slot: %d\n", slot);
+   goto done;
+   }
+
+   vin_dbg(vin, "Capture start synced!\n");
+   vin->state = RUNNING;
+   }
+
/* Capture frame */
if (vin->queue_buf[slot]) {
vin->queue_buf[slot]->field = vin->format.field;
diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
b/drivers/media/platform/rcar-vin/rcar-vin.h
index c2aef789b491ab31..ff747e22d8cfb643 100644
--- a/drivers/media/platform/rcar-vin/rcar-vin.h
+++ b/drivers/media/platform/rcar-vin/rcar-vin.h
@@ -53,11 +53,13 @@ enum rvin_csi_id {
 
 /**
  * STOPPED  - No operation in progress
+ * STARTING - Capture starting up
  * RUNNING  - Operation in progress have buffers
  * STOPPING - Stopping operation
  */
 enum rvin_dma_state {
STOPPED = 0,
+   STARTING,
RUNNING,
STOPPING,
 };
-- 
2.17.0



[PATCH] rcar-csi2: set default format if a unsupported one is requested

2018-05-16 Thread Niklas Söderlund
Instead of failing the set_fmt() if a unsupported format is requested
set a default one and return the changed format to the user.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reported-by: Sakari Ailus <sakari.ai...@iki.fi>
---
 drivers/media/platform/rcar-vin/rcar-csi2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
b/drivers/media/platform/rcar-vin/rcar-csi2.c
index e64f07fe184e7675..daef72d410a3425d 100644
--- a/drivers/media/platform/rcar-vin/rcar-csi2.c
+++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
@@ -613,7 +613,7 @@ static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
struct v4l2_mbus_framefmt *framefmt;
 
if (!rcsi2_code_to_fmt(format->format.code))
-   return -EINVAL;
+   format->format.code = rcar_csi2_formats[0].code;
 
if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
priv->mf = format->format;
-- 
2.17.0



Re: [PATCH 6/6] ARM: dts: rcar-gen2: Add 'data-active' property

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-16 18:32:32 +0200, Jacopo Mondi wrote:
> The 'data-active' property needs to be specified when using embedded
> synchronization. Add it to the Gen-2 boards using composite video input.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  arch/arm/boot/dts/r8a7790-lager.dts   | 1 +
>  arch/arm/boot/dts/r8a7791-koelsch.dts | 1 +
>  arch/arm/boot/dts/r8a7791-porter.dts  | 1 +
>  arch/arm/boot/dts/r8a7793-gose.dts| 1 +
>  arch/arm/boot/dts/r8a7794-alt.dts | 1 +
>  arch/arm/boot/dts/r8a7794-silk.dts| 1 +
>  6 files changed, 6 insertions(+)
> 
> diff --git a/arch/arm/boot/dts/r8a7790-lager.dts 
> b/arch/arm/boot/dts/r8a7790-lager.dts
> index b56b309..48fcb44 100644
> --- a/arch/arm/boot/dts/r8a7790-lager.dts
> +++ b/arch/arm/boot/dts/r8a7790-lager.dts
> @@ -893,6 +893,7 @@
> 
>   vin1ep0: endpoint {
>   remote-endpoint = <>;
> + data-active = <1>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7791-koelsch.dts 
> b/arch/arm/boot/dts/r8a7791-koelsch.dts
> index 9967666..fa0b25f 100644
> --- a/arch/arm/boot/dts/r8a7791-koelsch.dts
> +++ b/arch/arm/boot/dts/r8a7791-koelsch.dts
> @@ -868,6 +868,7 @@
> 
>   vin1ep: endpoint {
>   remote-endpoint = <>;
> + data-active = <1>;

Depending on how we interpret the data-active property this can be good 
or bad. But if we interpret it as the polarity of the VIn_CLKENB pin 
this is not good as this is not connected for the adv7180 on Koelsch.

I have not checked all the Gen2 schematics as I'm still not sure how to 
interpret the property.


>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7791-porter.dts 
> b/arch/arm/boot/dts/r8a7791-porter.dts
> index 055a7f1..96b9605 100644
> --- a/arch/arm/boot/dts/r8a7791-porter.dts
> +++ b/arch/arm/boot/dts/r8a7791-porter.dts
> @@ -391,6 +391,7 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> + data-active = <1>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7793-gose.dts 
> b/arch/arm/boot/dts/r8a7793-gose.dts
> index 9d3fba2..80b4fa8 100644
> --- a/arch/arm/boot/dts/r8a7793-gose.dts
> +++ b/arch/arm/boot/dts/r8a7793-gose.dts
> @@ -779,6 +779,7 @@
> 
>   vin1ep: endpoint {
>   remote-endpoint = <_out>;
> + data-active = <1>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7794-alt.dts 
> b/arch/arm/boot/dts/r8a7794-alt.dts
> index 4bbb9cc..00df605d 100644
> --- a/arch/arm/boot/dts/r8a7794-alt.dts
> +++ b/arch/arm/boot/dts/r8a7794-alt.dts
> @@ -380,6 +380,7 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> + data-active = <1>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7794-silk.dts 
> b/arch/arm/boot/dts/r8a7794-silk.dts
> index c0c5d31..ed17376 100644
> --- a/arch/arm/boot/dts/r8a7794-silk.dts
> +++ b/arch/arm/boot/dts/r8a7794-silk.dts
> @@ -480,6 +480,7 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> + data-active = <1>;
>   };
>   };
>  };
> --
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 5/6] ARM: dts: rcar-gen2: Remove unused VIN properties

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-16 18:32:31 +0200, Jacopo Mondi wrote:
> The 'bus-width' and 'pclk-sample' properties are not parsed by the VIN
> driver and only confuse users. Remove them in all Gen2 SoC that used
> them.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  arch/arm/boot/dts/r8a7790-lager.dts   | 3 ---
>  arch/arm/boot/dts/r8a7791-koelsch.dts | 3 ---
>  arch/arm/boot/dts/r8a7791-porter.dts  | 1 -
>  arch/arm/boot/dts/r8a7793-gose.dts| 3 ---
>  arch/arm/boot/dts/r8a7794-alt.dts | 1 -
>  arch/arm/boot/dts/r8a7794-silk.dts| 1 -
>  6 files changed, 12 deletions(-)
> 
> diff --git a/arch/arm/boot/dts/r8a7790-lager.dts 
> b/arch/arm/boot/dts/r8a7790-lager.dts
> index 063fdb6..b56b309 100644
> --- a/arch/arm/boot/dts/r8a7790-lager.dts
> +++ b/arch/arm/boot/dts/r8a7790-lager.dts
> @@ -873,10 +873,8 @@
>   port {
>   vin0ep2: endpoint {
>   remote-endpoint = <_out>;
> - bus-width = <24>;

I can't really make up my mind if this is a good thing or not. Device 
tree describes the hardware and not what the drivers make use of. And 
the fact is that this bus is 24 bits wide. So I'm not sure we should 
remove these properties. But I would love to hear what others think 
about this.

>   hsync-active = <0>;
>   vsync-active = <0>;
> - pclk-sample = <1>;
>   data-active = <1>;
>   };
>   };
> @@ -895,7 +893,6 @@
> 
>   vin1ep0: endpoint {
>   remote-endpoint = <>;
> - bus-width = <8>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7791-koelsch.dts 
> b/arch/arm/boot/dts/r8a7791-koelsch.dts
> index f40321a..9967666 100644
> --- a/arch/arm/boot/dts/r8a7791-koelsch.dts
> +++ b/arch/arm/boot/dts/r8a7791-koelsch.dts
> @@ -849,10 +849,8 @@
> 
>   vin0ep2: endpoint {
>   remote-endpoint = <_out>;
> - bus-width = <24>;
>   hsync-active = <0>;
>   vsync-active = <0>;
> - pclk-sample = <1>;
>   data-active = <1>;
>   };
>   };
> @@ -870,7 +868,6 @@
> 
>   vin1ep: endpoint {
>   remote-endpoint = <>;
> - bus-width = <8>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7791-porter.dts 
> b/arch/arm/boot/dts/r8a7791-porter.dts
> index c14e6fe..055a7f1 100644
> --- a/arch/arm/boot/dts/r8a7791-porter.dts
> +++ b/arch/arm/boot/dts/r8a7791-porter.dts
> @@ -391,7 +391,6 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> - bus-width = <8>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7793-gose.dts 
> b/arch/arm/boot/dts/r8a7793-gose.dts
> index 9ed6961..9d3fba2 100644
> --- a/arch/arm/boot/dts/r8a7793-gose.dts
> +++ b/arch/arm/boot/dts/r8a7793-gose.dts
> @@ -759,10 +759,8 @@
> 
>   vin0ep2: endpoint {
>   remote-endpoint = <_out>;
> - bus-width = <24>;
>   hsync-active = <0>;
>   vsync-active = <0>;
> - pclk-sample = <1>;
>   data-active = <1>;
>   };
>   };
> @@ -781,7 +779,6 @@
> 
>   vin1ep: endpoint {
>   remote-endpoint = <_out>;
> - bus-width = <8>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7794-alt.dts 
> b/arch/arm/boot/dts/r8a7794-alt.dts
> index 26a8834..4bbb9cc 100644
> --- a/arch/arm/boot/dts/r8a7794-alt.dts
> +++ b/arch/arm/boot/dts/r8a7794-alt.dts
> @@ -380,7 +380,6 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> -     bus-width = <8>;
>   };
>   };
>  };
> diff --git a/arch/arm/boot/dts/r8a7794-silk.dts 
> b/arch/arm/boot/dts/r8a7794-silk.dts
> index 351cb3b..c0c5d31 100644
> --- a/arch/arm/boot/dts/r8a7794-silk.dts
> +++ b/arch/arm/boot/dts/r8a7794-silk.dts
> @@ -480,7 +480,6 @@
> 
>   vin0ep: endpoint {
>   remote-endpoint = <>;
> - bus-width = <8>;
>   };
>   };
>  };
> --
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 4/6] media: rcar-vin: Handle CLOCKENB pin polarity

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

I'm happy that you dig into this as it clearly needs doing!

On 2018-05-16 18:32:30 +0200, Jacopo Mondi wrote:
> Handle CLOCKENB pin polarity, or use HSYNC in its place if polarity is
> not specified.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-dma.c | 11 +++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
> b/drivers/media/platform/rcar-vin/rcar-dma.c
> index ac07f99..7a84eae 100644
> --- a/drivers/media/platform/rcar-vin/rcar-dma.c
> +++ b/drivers/media/platform/rcar-vin/rcar-dma.c
> @@ -123,6 +123,8 @@
>  /* Video n Data Mode Register 2 bits */
>  #define VNDMR2_VPS   (1 << 30)
>  #define VNDMR2_HPS   (1 << 29)
> +#define VNDMR2_CES   (1 << 28)
> +#define VNDMR2_CHS   (1 << 23)
>  #define VNDMR2_FTEV  (1 << 17)
>  #define VNDMR2_VLV(n)((n & 0xf) << 12)
>  
> @@ -691,6 +693,15 @@ static int rvin_setup(struct rvin_dev *vin)
>   dmr2 |= VNDMR2_VPS;
>  
>   /*
> +  * Clock-enable active level select.
> +  * Use HSYNC as enable if not specified
> +  */
> + if (vin->mbus_cfg.flags & V4L2_MBUS_DATA_ACTIVE_LOW)
> + dmr2 |= VNDMR2_CES;
> + else if (!(vin->mbus_cfg.flags & V4L2_MBUS_DATA_ACTIVE_HIGH))
> + dmr2 |= VNDMR2_CHS;

After studying the datasheet for a while I'm getting more and more 
convinced this should be (with context leftout in this patch context) 
something like this:

/* Hsync Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
dmr2 |= VNDMR2_HPS;

/* Vsync Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
dmr2 |= VNDMR2_VPS;

/* Clock Enable Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_DATA_ACTIVE_LOW))
dmr2 |= VNDMR2_CES;

/* Use HSYNC as clock enable if VIn_CLKENB is not available. */
if (!(vin->mbus_cfg.flags & (V4L2_MBUS_DATA_ACTIVE_LOW | 
V4L2_MBUS_DATA_ACTIVE_HIGH)))
    dmr2 |= VNDMR2_CHS;

Or am I misunderstanding something?

> +
> + /*
>* Output format
>*/
>   switch (vin->format.pixelformat) {
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 3/6] media: rcar-vin: Handle data-active property

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-16 18:32:29 +0200, Jacopo Mondi wrote:
> The data-active property has to be specified when running with embedded
> synchronization. The VIN peripheral can use HSYNC in place of CLOCKENB
> when the CLOCKENB pin is not connected, this requires explicit
> synchronization to be in use.

Is this really the intent of the data-active property? I read the 
video-interfaces.txt document as such as if no hsync-active, 
vsync-active and data-active we should use the embedded synchronization 
else set the polarity for the requested pins. What am I not 
understanding here?

> 
> Now that the driver supports 'data-active' property, it makes not sense
> to zero the mbus configuration flags when running with implicit synch
> (V4L2_MBUS_BT656).
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 10 --
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index d3072e1..075d08f 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -531,15 +531,21 @@ static int rvin_digital_parse_v4l2(struct device *dev,
>   return -ENOTCONN;
>  
>   vin->mbus_cfg.type = vep->bus_type;
> + vin->mbus_cfg.flags = vep->bus.parallel.flags;
>  
>   switch (vin->mbus_cfg.type) {
>   case V4L2_MBUS_PARALLEL:
>   vin_dbg(vin, "Found PARALLEL media bus\n");
> - vin->mbus_cfg.flags = vep->bus.parallel.flags;
>   break;
>   case V4L2_MBUS_BT656:
>   vin_dbg(vin, "Found BT656 media bus\n");
> - vin->mbus_cfg.flags = 0;
> +
> + if (!(vin->mbus_cfg.flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
> + !(vin->mbus_cfg.flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
> + vin_err(vin,
> + "Missing data enable signal polarity 
> property\n");

I fear this can't be an error as that would break backward comp ability 
with existing dtb's.

> + return -EINVAL;
> + }
>   break;
>   default:
>   vin_err(vin, "Unknown media bus type\n");
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 2/6] dt-bindings: media: rcar-vin: Document data-active

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-16 18:32:28 +0200, Jacopo Mondi wrote:
> Document 'data-active' property in R-Car VIN device tree bindings.
> The property is optional when running with explicit synchronization
> (eg. BT.601) but mandatory when embedded synchronization is in use (eg.
> BT.656) as specified by the hardware manual.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 5 +
>  1 file changed, 5 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index c53ce4e..17eac8a 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -63,6 +63,11 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>   If both HSYNC and VSYNC polarities are not specified, embedded
>   synchronization is selected.
> 
> +- data-active: active state of data enable signal (CLOCKENB pin).

I'm not sure what you mean by active state here. video-interfaces.txt 
defines data-active as 'similar to HSYNC and VSYNC, specifies data line 
polarity' so I assume this is the polarity of the CLOCKENB pin?

> +  0/1 for LOW/HIGH respectively. If not specified, use HSYNC as
> +  data enable signal. When using embedded synchronization this
> +  property is mandatory.

I'm confused, why is this mandatory if we have no embedded sync (that is 
hsync-active and vsync-active not defined)? I can't find any reference 
to this in the Gen2 datasheet but I'm sure I'm just missing it :-)

> +
>  - port 1 - sub-nodes describing one or more endpoints connected to
>the VIN from local SoC CSI-2 receivers. The endpoint numbers must
>use the following schema.
> --
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 1/6] dt-bindings: media: rcar-vin: Describe optional ep properties

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-16 18:32:27 +0200, Jacopo Mondi wrote:
> Describe the optional endpoint properties for endpoint nodes of port@0
> and port@1 of the R-Car VIN driver device tree bindings documentation.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 13 -
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index a19517e1..c53ce4e 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -53,6 +53,16 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>from external SoC pins described in video-interfaces.txt[1].
>Describing more then one endpoint in port 0 is invalid. Only VIN
>instances that are connected to external pins should have port 0.
> +
> +  - Optional properties for endpoint nodes of port@0:
> +- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH
> +   respectively. Default is active high.
> +- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH
> +   respectively. Default is active high.
> +
> + If both HSYNC and VSYNC polarities are not specified, embedded
> + synchronization is selected.
> +
>  - port 1 - sub-nodes describing one or more endpoints connected to
>the VIN from local SoC CSI-2 receivers. The endpoint numbers must
>use the following schema.
> @@ -62,6 +72,8 @@ from local SoC CSI-2 receivers (port1) depending on SoC.
>  - Endpoint 2 - sub-node describing the endpoint connected to CSI40
>  - Endpoint 3 - sub-node describing the endpoint connected to CSI41
> 
> +  Endpoint nodes of port@1 do not support any optional endpoint property.
> +
>  Device node example for Gen2 platforms
>  --
> 
> @@ -112,7 +124,6 @@ Board setup example for Gen2 platforms (vin1 composite 
> video input)
> 
>  vin1ep0: endpoint {
>  remote-endpoint = <>;
> -bus-width = <8>;
>  };
>  };
>  };
> --
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 0/6] media: rcar-vin: Brush endpoint properties

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

On 2018-05-16 18:32:26 +0200, Jacopo Mondi wrote:
> Hello,
>this series touches the bindings and the driver handling endpoint
> properties for digital subdevices of the R-Car VIN driver.
> 
> The first patch simply documents what are the endpoint properties supported
> at the moment, then the second one extends them with 'data-active'.
> 
> As the VIN hardware allows to use HSYNC as data enable signal when the CLCKENB
> pin is left unconnected, the 'data-active' property presence determinates
> if HSYNC has to be used or not as data enable signal. As a consequence, when
> running with embedded synchronism, and there is not HSYNC signal, it becomes
> mandatory to specify 'data-active' polarity in DTS.
> 
> To address this, all Gen-2 boards featuring a composite video input and
> running with embedded synchronization, now need that property to be specified
> in DTS. Before adding it, remove un-used properties as 'pclk-sample' and
> 'bus-width' from the Gen-2 bindings, as they are not parsed by the VIN driver
> and only confuse users.
> 
> Niklas, as you already know I don't have any Gen2 board. Could you give this
> a spin on Koelsch if you like the series?

I tested this on my Koelsch and capture is still working :-)

> 
> Thanks
>j
> 
> Jacopo Mondi (6):
>   dt-bindings: media: rcar-vin: Describe optional ep properties
>   dt-bindings: media: rcar-vin: Document data-active
>   media: rcar-vin: Handle data-active property
>   media: rcar-vin: Handle CLOCKENB pin polarity
>   ARM: dts: rcar-gen2: Remove unused VIN properties
>   ARM: dts: rcar-gen2: Add 'data-active' property
> 
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 18 +-
>  arch/arm/boot/dts/r8a7790-lager.dts  |  4 +---
>  arch/arm/boot/dts/r8a7791-koelsch.dts|  4 +---
>  arch/arm/boot/dts/r8a7791-porter.dts |  2 +-
>  arch/arm/boot/dts/r8a7793-gose.dts   |  4 +---
>  arch/arm/boot/dts/r8a7794-alt.dts|  2 +-
>  arch/arm/boot/dts/r8a7794-silk.dts   |  2 +-
>  drivers/media/platform/rcar-vin/rcar-core.c  | 10 --
>  drivers/media/platform/rcar-vin/rcar-dma.c   | 11 +++
>  9 files changed, 42 insertions(+), 15 deletions(-)
> 
> --
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 4/4] media: rcar-vin: Add support for R-Car R8A77995 SoC

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

On 2018-05-16 14:16:56 +0200, Jacopo Mondi wrote:
> Add R-Car R8A77995 SoC to the rcar-vin supported ones.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> Reviewed-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 16 
>  1 file changed, 16 insertions(+)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index ea74c55..fba8610 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -1104,6 +1104,10 @@ static const struct rvin_group_route 
> _rcar_info_r8a77970_routes[] = {
>   { /* Sentinel */ }
>  };
>  
> +static const struct rvin_group_route _rcar_info_r8a77995_routes[] = {

Nitpick and I see that the error is not yours but present in other but 
not all route declarations. But the intention is not to have a leading _ 
here.  A patch to clean that up for _rcar_info_r8a77965_routes[] and 
_rcar_info_r8a77970_routes[] would be welcome, or I can do it if you are 
over loaded.

> + { /* Sentinel */ }
> +};
> +
>  static const struct rvin_info rcar_info_r8a77970 = {
>   .model = RCAR_GEN3,
>   .use_mc = true,
> @@ -1112,6 +1116,14 @@ static const struct rvin_info rcar_info_r8a77970 = {
>   .routes = _rcar_info_r8a77970_routes,
>  };
>  
> +static const struct rvin_info rcar_info_r8a77995 = {
> + .model = RCAR_GEN3,
> + .use_mc = true,
> + .max_width = 4096,
> + .max_height = 4096,
> + .routes = _rcar_info_r8a77995_routes,
> +};
> +
>  static const struct of_device_id rvin_of_id_table[] = {
>   {
>   .compatible = "renesas,vin-r8a7778",
> @@ -1153,6 +1165,10 @@ static const struct of_device_id rvin_of_id_table[] = {
>   .compatible = "renesas,vin-r8a77970",
>   .data = _info_r8a77970,
>   },
> + {
> + .compatible = "renesas,vin-r8a77995",
> + .data = _info_r8a77995,
> + },
>   { /* Sentinel */ },
>  };
>  MODULE_DEVICE_TABLE(of, rvin_of_id_table);
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 3/4] media: rcar-vin: Handle digital subdev in link_notify

2018-05-16 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch,

On 2018-05-16 14:16:55 +0200, Jacopo Mondi wrote:
> Handle digital subdevices in link_notify callback. If the notified link
> involves a digital subdevice, do not change routing of the VIN-CSI-2
> devices.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 30 
> +++--
>  1 file changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index 1003c8c..ea74c55 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -168,10 +168,36 @@ static int rvin_group_link_notify(struct media_link 
> *link, u32 flags,
>   }
>  
>   /* Add the new link to the existing mask and check if it works. */
> - csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);
>   channel = rvin_group_csi_pad_to_channel(link->source->index);
> - mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
> + csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);
> + if (csi_id == -ENODEV) {
> + struct v4l2_subdev *sd;
> + unsigned int i;
> +
> + /*
> +  * Make sure the source entity subdevice is registered as
> +  * a digital input of one of the enabled VINs if it is not
> +  * one of the CSI-2 subdevices.
> +  *
> +  * No hardware configuration required for digital inputs,
> +  * we can return here.
> +  */
> + sd = media_entity_to_v4l2_subdev(link->source->entity);
> + for (i = 0; i < RCAR_VIN_NUM; i++) {
> + if (group->vin[i] && group->vin[i]->digital &&
> + group->vin[i]->digital->subdev == sd) {
> + ret = 0;
> + goto out;
> + }
> + }
> +
> + vin_err(vin, "Subdevice %s not registered to any VIN\n",
> + link->source->entity->name);
> + ret = -ENODEV;
> + goto out;
> + }

I like this patch, you made it so simple. I feared this would be the 
ugly part when adding parallel support to Gen3. All that is missing is 
handling of vin->mbus_cfg or how you think we best handle that for the 
different input buses.

>  
> + mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
>   vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new);
>  
>   if (!mask_new) {
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 2/4] media: rcar-vin: Handle mc in digital notifier ops

2018-05-16 Thread Niklas Söderlund
gt;lock);
> - ret = rvin_digital_subdevice_attach(vin, subdev);
> - mutex_unlock(>lock);
> - if (ret)
> +
> + /* Find source and sink pad of remote subdevice */
> + ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
> + if (ret < 0) {
> + mutex_unlock(>lock);
>   return ret;
> + }
> + vin->digital->source_pad = ret;
> +
> + ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
> + vin->digital->sink_pad = ret < 0 ? 0 : ret;
> +
> + vin->digital->subdev = subdev;
> +
> + if (!vin->info->use_mc) {
> + ret = rvin_digital_subdevice_attach(vin, subdev);
> + if (ret) {
> + mutex_unlock(>lock);
> + return ret;
> + }
> + }
> +
> + mutex_unlock(>lock);
>  
>   v4l2_set_subdev_hostdata(subdev, vin);
>  
> @@ -555,9 +582,10 @@ static int rvin_digital_graph_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - ret = v4l2_async_notifier_parse_fwnode_endpoints(
> + ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
>   vin->dev, >notifier,
> - sizeof(struct rvin_graph_entity), rvin_digital_parse_v4l2);
> + sizeof(struct rvin_graph_entity), RVIN_PORT_DIGITAL,
> + rvin_digital_parse_v4l2);
>   if (ret)
>   return ret;
>  
> @@ -567,6 +595,13 @@ static int rvin_digital_graph_init(struct rvin_dev *vin)
>   vin_dbg(vin, "Found digital subdevice %pOF\n",
>   to_of_node(vin->digital->asd.match.fwnode));
>  
> + /*
> +  * If we run with media-controller, notifiers will be registered
> +  * later, once all VINs have probed.
> +  */
> +     if (vin->info->use_mc)
> + return 0;
> +
>   vin->notifier.ops = _digital_notify_ops;
>   ret = v4l2_async_notifier_register(>v4l2_dev, >notifier);
>   if (ret < 0) {
> @@ -596,7 +631,8 @@ static int rvin_group_notify_complete(struct 
> v4l2_async_notifier *notifier)
>  
>   /* Register all video nodes for the group. */
>   for (i = 0; i < RCAR_VIN_NUM; i++) {
> - if (vin->group->vin[i]) {
> + if (vin->group->vin[i] &&
> + !video_is_registered(>group->vin[i]->vdev)) {
>   ret = rvin_v4l2_register(vin->group->vin[i]);
>   if (ret)
>   return ret;
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2 1/4] media: rcar-vin: Parse digital input in mc-path

2018-05-16 Thread Niklas Söderlund
s a bit to optimised.

for (i = 0; i < RCAR_VIN_NUM; i++)

With the same checks as bellow would work just as good with the checks 
you have bellow. v4l2_async_notifier_unregister() checks if it's called 
with a notifier that have not been registered and does the right thing.

> + struct v4l2_async_notifier *notifier;
> +
> + if (!vin->group->vin[i - 1])
> + continue;
> +
> + notifier = >group->vin[i - 1]->notifier;
> + if (!notifier->num_subdevs)
> + continue;
> +
> + v4l2_async_notifier_unregister(notifier);
> + }
> +
> + return ret;
>  }
>  
>  static int rvin_mc_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - /* All our sources are CSI-2 */
> - vin->mbus_cfg.type = V4L2_MBUS_CSI2;
> - vin->mbus_cfg.flags = 0;
> -
>   vin->pad.flags = MEDIA_PAD_FL_SINK;
>   ret = media_entity_pads_init(>vdev.entity, 1, >pad);
>   if (ret)
> diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
> b/drivers/media/platform/rcar-vin/rcar-vin.h
> index c2aef78..836751e 100644
> --- a/drivers/media/platform/rcar-vin/rcar-vin.h
> +++ b/drivers/media/platform/rcar-vin/rcar-vin.h
> @@ -52,6 +52,19 @@ enum rvin_csi_id {
>  };
>  
>  /**
> + * enum rvin_port_id
> + *
> + * List the available VIN port functions.
> + *
> + * RVIN_PORT_DIGITAL - Input port for digital video connection
> + * RVIN_PORT_CSI2- Input port for CSI-2 video connection
> + */
> +enum rvin_port_id {
> + RVIN_PORT_DIGITAL,
> + RVIN_PORT_CSI2
> +};
> +
> +/**
>   * STOPPED  - No operation in progress
>   * RUNNING  - Operation in progress have buffers
>   * STOPPING - Stopping operation
> @@ -225,6 +238,7 @@ struct rvin_dev {
>   *
>   * @lock:protects the count, notifier, vin and csi members
>   * @count:   number of enabled VIN instances found in DT
> + * @v4l2_dev:pointer to the group v4l2 device

I pray there is a away to avoid adding this here, it feels awkward :-(

>   * @notifier:pointer to the notifier of a VIN which handles 
> the
>   *   groups async sub-devices.
>   * @vin: VIN instances which are part of the group
> @@ -238,6 +252,7 @@ struct rvin_group {
>  
>   struct mutex lock;
>   unsigned int count;
> + struct v4l2_device *v4l2_dev;
>   struct v4l2_async_notifier *notifier;
>   struct rvin_dev *vin[RCAR_VIN_NUM];
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


[PATCH v16 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-05-14 Thread Niklas Söderlund
A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
connected between the video sources and the video grabbers (VIN).

Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Maxime Ripard <maxime.rip...@bootlin.com>

---

* Changes since v15
- Merge struct phtw_mbps and struct phypll_hsfreqrange into a new struct
  which maps a mpbs value to a register value, struct rcsi2_mbps_reg.
- Reduced number of loops and delay when waiting for LP-11 and
  confirmation of PHTW write as suggested by Laurent.
- Dropped dev_dbg() printouts of the requested link speed.
- Fix small issues in comments.
- Remove unneeded () in for-loop condition in rcsi2_phtw_write_array().
- Remove __refdata from declaration of 'static struct platform_driver
  rcar_csi2_pdrv'.
- Update MODULE_DESCRIPTION to 'Renesas R-Car MIPI CSI-2 receiver
  driver'.
- Fixed two erroneous values in hsfreqrange_h3_v3h_m3n[]. Thanks Jacopo
  for spotting this!
- Max link speed for V3M and E3 are 1.125Gbps remove settings above that
  limit in phtw_mbps_v3m_e3. This also changed in datasheet v1.0.
- Add review tags from Laurent and Maxime.

* Changes since v14
- Data sheet update changed init sequence for PHY forcing a restructure
  of the driver. The restructure was so big I felt compel to drop all
  review tags :-(
- The change was that the Renesas H3 procedure was aligned with other
  SoC in the Gen3 family procedure. I had kept the rework as separate
  patches and was planing to post once original driver with H3 and M3-W
  support where merged. As review tags are dropped I chosen to squash
  those patches into 2/2.
- Add support for Gen3 V3M.
- Add support for Gen3 M3-N.
- Set PHTC_TESTCLR when stopping the PHY.
- Revert back to the v12 and earlier phypll calculation as it turns out
  it was correct after all.

* Changes since v13
- Change return rcar_csi2_formats + i to return _csi2_formats[i].
- Add define for PHCLM_STOPSTATECKL.
- Update spelling in comments.
- Update calculation in rcar_csi2_calc_phypll() according to
  https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
  before v14 did not take into account that 2 bits per sample is
  transmitted.
- Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
  statement to set correct number of lanes to enable.
- Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
  style of rest of file.
- Switch to %u instead of 0x%x when printing bus type.
- Switch to %u instead of %d for priv->lanes which is unsigned.
- Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
  rcar_csi2_formats[].
- Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
- Set INTSTATE after PL-11 is confirmed to match flow chart in
  datasheet.
- Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
- Add Maxime's and laurent's tags.
---
 drivers/media/platform/rcar-vin/Kconfig |   12 +
 drivers/media/platform/rcar-vin/Makefile|1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c | 1084 +++
 3 files changed, 1097 insertions(+)
 create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c

diff --git a/drivers/media/platform/rcar-vin/Kconfig 
b/drivers/media/platform/rcar-vin/Kconfig
index 8fa7ee468c63afb9..d5835da6d4100d87 100644
--- a/drivers/media/platform/rcar-vin/Kconfig
+++ b/drivers/media/platform/rcar-vin/Kconfig
@@ -1,3 +1,15 @@
+config VIDEO_RCAR_CSI2
+   tristate "R-Car MIPI CSI-2 Receiver"
+   depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF
+   depends on ARCH_RENESAS || COMPILE_TEST
+   select V4L2_FWNODE
+   help
+ Support for Renesas R-Car MIPI CSI-2 receiver.
+ Supports R-Car Gen3 SoCs.
+
+ To compile this driver as a module, choose M here: the
+ module will be called rcar-csi2.
+
 config VIDEO_RCAR_VIN
tristate "R-Car Video Input (VIN) Driver"
depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF && HAS_DMA && 
MEDIA_CONTROLLER
diff --git a/drivers/media/platform/rcar-vin/Makefile 
b/drivers/media/platform/rcar-vin/Makefile
index 48c5632c21dc060b..5ab803d3e7c1aa57 100644
--- a/drivers/media/platform/rcar-vin/Makefile
+++ b/drivers/media/platform/rcar-vin/Makefile
@@ -1,3 +1,4 @@
 rcar-vin-objs = rcar-core.o rcar-dma.o rcar-v4l2.o
 
+obj-$(CONFIG_VIDEO_RCAR_CSI2) += rcar-csi2.o
 obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin.o
diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
b/drivers/media/platform/rcar-vin/rcar-csi2.c
new file mode 100644
index ..e64f07fe184e7675
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
@@ -0,0 +1,1084 @@
+// SPDX-License-Identifier: GPL-2.0

[PATCH v16 1/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation

2018-05-14 Thread Niklas Söderlund
Documentation for Renesas R-Car MIPI CSI-2 receiver. The CSI-2 receivers
are located between the video sources (CSI-2 transmitters) and the video
grabbers (VIN) on Gen3 of Renesas R-Car SoC.

Each CSI-2 device is connected to more than one VIN device which
simultaneously can receive video from the same CSI-2 device. Each VIN
device can also be connected to more than one CSI-2 device. The routing
of which links are used is controlled by the VIN devices. There are only
a few possible routes which are set by hardware limitations, which are
different for each SoC in the Gen3 family.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Acked-by: Rob Herring <r...@kernel.org>
Acked-by: Sakari Ailus <sakari.ai...@linux.intel.com>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

---

* Changes since v14.
- Added compatible string for R8A77965 and R8A77970.
- s/Port 0/port@0/
- s/Port 1/port@1/
- s/Endpoint 0/endpoint@0/

* Changes since v13
- Add Laurent's tag.
---
 .../bindings/media/renesas,rcar-csi2.txt  | 101 ++
 MAINTAINERS   |   1 +
 2 files changed, 102 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt

diff --git a/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt 
b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
new file mode 100644
index ..2d385b65b275bc58
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
@@ -0,0 +1,101 @@
+Renesas R-Car MIPI CSI-2
+
+
+The R-Car CSI-2 receiver device provides MIPI CSI-2 capabilities for the
+Renesas R-Car family of devices. It is used in conjunction with the
+R-Car VIN module, which provides the video capture capabilities.
+
+Mandatory properties
+
+ - compatible: Must be one or more of the following
+   - "renesas,r8a7795-csi2" for the R8A7795 device.
+   - "renesas,r8a7796-csi2" for the R8A7796 device.
+   - "renesas,r8a77965-csi2" for the R8A77965 device.
+   - "renesas,r8a77970-csi2" for the R8A77970 device.
+
+ - reg: the register base and size for the device registers
+ - interrupts: the interrupt for the device
+ - clocks: reference to the parent clock
+
+The device node shall contain two 'port' child nodes according to the
+bindings defined in Documentation/devicetree/bindings/media/
+video-interfaces.txt. port@0 shall connect to the CSI-2 source. port@1
+shall connect to all the R-Car VIN modules that have a hardware
+connection to the CSI-2 receiver.
+
+- port@0- Video source (mandatory)
+   - endpoint@0 - sub-node describing the endpoint that is the video source
+
+- port@1 - VIN instances (optional)
+   - One endpoint sub-node for every R-Car VIN instance which is connected
+ to the R-Car CSI-2 receiver.
+
+Example:
+
+   csi20: csi2@fea8 {
+   compatible = "renesas,r8a7796-csi2";
+   reg = <0 0xfea8 0 0x1>;
+   interrupts = <0 184 IRQ_TYPE_LEVEL_HIGH>;
+   clocks = < CPG_MOD 714>;
+   power-domains = < R8A7796_PD_ALWAYS_ON>;
+   resets = < 714>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@0 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <0>;
+
+   csi20_in: endpoint@0 {
+   reg = <0>;
+   clock-lanes = <0>;
+   data-lanes = <1>;
+   remote-endpoint = <_txb>;
+   };
+   };
+
+   port@1 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <1>;
+
+   csi20vin0: endpoint@0 {
+   reg = <0>;
+   remote-endpoint = <>;
+   };
+   csi20vin1: endpoint@1 {
+   reg = <1>;
+   remote-endpoint = <>;
+   };
+   csi20vin2: endpoint@2 {
+   reg = <2>;
+   remote-endpoint = <>;
+   };
+   csi20vi

[PATCH v16 0/2] rcar-csi2: add Renesas R-Car MIPI CSI-2

2018-05-14 Thread Niklas Söderlund
e pm_runtime_get_sync() and pm_runtime_put() to
  rcar_csi2_s_stream() and remove rcar_csi2_s_power().
- Add validation of pixel code to rcar_csi2_set_pad_format().
- Remove static rcar_csi2_notify_unbind() as it only printed a debug
  message.

* Changes since v9
- Add reset property to device tree example
- Use BIT(x) instead of (1 << x)
- Use u16 in struct phypll_hsfreqrange to pack struct better.
- Use unsigned int type for loop variable in rcar_csi2_code_to_fmt
- Move fields inside struct struct rcar_csi2_info and struct rcar_csi2
  to pack struct's tighter.
- Use %u instead of %d when printing __u32.
- Don't check return of platform_get_resource(), let
  devm_ioremap_resource() handle it.
- Store quirk workaround for r8a7795 ES1.0 in the data field of struct
  soc_device_attribute.

Changes since v8:
- Updated bindings documentation, thanks Rob!
- Make use of the now in media-tree sub-notifier V4L2 API
- Add delay when resetting the IP to allow for a proper reset
- Fix bug in s_stream error path where the usage count was off if an
  error was hit.
- Add support for H3 ES2.0

Changes since v7:
- Rebase on top of the latest incremental async patches.
- Fix comments on DT documentation.
- Use v4l2_ctrl_g_ctrl_int64() instead of v4l2_g_ext_ctrls().
- Handle try formats in .set_fmt() and .get_fmt().
- Don't call v4l2_device_register_subdev_nodes() as this is not needed
  with the complete() callbacks synchronized.
- Fix line over 80 chars.
- Fix varies spelling mistakes.

Changes since v6:
- Rebased on top of Sakaris fwnode patches.
- Changed of RCAR_CSI2_PAD_MAX to NR_OF_RCAR_CSI2_PAD.
- Remove assumption about unknown media bus type, thanks Sakari for
  pointing this out.
- Created table for supported format information instead of scattering
  this information around the driver, thanks Sakari!
- Small newline fixes and reduce some indentation levels

Changes since v5:
- Make use of the incremental async subnotifer and helper to map DT
  endpoint to media pad number. This moves functionality which
  previously in the Gen3 patches for R-Car VIN driver to this R-Car
  CSI-2 driver. This is done in preparation to support the ADV7482
  driver in development by Kieran which will register more then one
  subdevice and the CSI-2 driver needs to cope wit this. Further more it
  prepares the driver for another use-case where more then one subdevice
  is present upstream for the CSI-2.
- Small cleanups.
- Add explicit include for linux/io.h, thanks Kieran.

Changes since v4:
- Match SoC part numbers and drop trailing space in documentation,
  thanks Geert for pointing this out.
- Clarify that the driver is a CSI-2 receiver by supervised
  s/interface/receiver/, thanks Laurent.
- Add entries in Kconfig and Makefile alphabetically instead of append.
- Rename struct rcar_csi2 member swap to lane_swap.
- Remove macros to wrap calls to dev_{dbg,info,warn,err}.
- Add wrappers for ioread32 and iowrite32.
- Remove unused interrupt handler, but keep checking in probe that there
  are a interrupt define in DT.
- Rework how to wait for LP-11 state, thanks Laurent for the great idea!
- Remove unneeded delay in rcar_csi2_reset()
- Remove check for duplicated lane id:s from DT parsing. Broken out to a
  separate patch adding this check directly to v4l2_of_parse_endpoint().
- Fixed rcar_csi2_start() to ask it's source subdevice for information
  about pixel rate and frame format. With this change having
  {set,get}_fmt operations became redundant, it was only used for
  figuring out this out so dropped them.
- Tabulated frequency settings map.
- Dropped V4L2_SUBDEV_FL_HAS_DEVNODE it should never have been set.
- Switched from MEDIA_ENT_F_ATV_DECODER to
  MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER as entity function. I can't
  find a more suitable function, and what the hardware do is to fetch
  video from an external chip and passes it on to a another SoC internal
  IP it's sort of a formatter.
- Break out DT documentation and code in two patches.

Changes since v3:
- Update DT binding documentation with input from Geert Uytterhoeven,
  thanks

Changes since v2:
- Added media control pads as this is needed by the new rcar-vin driver.
- Update DT bindings after review comments and to add r8a7796 support.
- Add get_fmt handler.
- Fix media bus format error s/YUYV8/UYVY8/

Changes since v1:
- Drop dependency on a pad aware s_stream operation.
- Use the DT bindings format "renesas,-", thanks Geert
  for pointing this out.

Niklas Söderlund (2):
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

 .../bindings/media/renesas,rcar-csi2.txt  |  101 ++
 MAINTAINERS   |1 +
 drivers/media/platform/rcar-vin/Kconfig   |   12 +
 drivers/media/platform/rcar-vin/Makefile  |1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c   | 1084 +
 5 files changed, 1199 insertions(+)
 create mode 100644 
Documentation/devicetree

Re: [PATCH v15 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-05-14 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your feedback.

On 2018-05-14 15:14:48 +0200, Jacopo Mondi wrote:
> Hi Niklas,
>thanks for the patch
> 
> On Sun, May 13, 2018 at 09:19:17PM +0200, Niklas Söderlund wrote:
> > A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
> > supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
> > connected between the video sources and the video grabbers (VIN).
> >
> > Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.
> >
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> >
> > ---
> >
> > * Changes since v14
> > - Data sheet update changed init sequence for PHY forcing a restructure
> >   of the driver. The restructure was so big I felt compel to drop all
> >   review tags :-(
> > - The change was that the Renesas H3 procedure was aligned with other
> >   SoC in the Gen3 family procedure. I had kept the rework as separate
> >   patches and was planing to post once original driver with H3 and M3-W
> >   support where merged. As review tags are dropped I chosen to squash
> >   those patches into 2/2.
> > - Add support for Gen3 V3M.
> > - Add support for Gen3 M3-N.
> > - Set PHTC_TESTCLR when stopping the PHY.
> > - Revert back to the v12 and earlier phypll calculation as it turns out
> >   it was correct after all.
> >
> > * Changes since v13
> > - Change return rcar_csi2_formats + i to return _csi2_formats[i].
> > - Add define for PHCLM_STOPSTATECKL.
> > - Update spelling in comments.
> > - Update calculation in rcar_csi2_calc_phypll() according to
> >   https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
> >   before v14 did not take into account that 2 bits per sample is
> >   transmitted.
> > - Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
> >   statement to set correct number of lanes to enable.
> > - Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
> >   style of rest of file.
> > - Switch to %u instead of 0x%x when printing bus type.
> > - Switch to %u instead of %d for priv->lanes which is unsigned.
> > - Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
> >   rcar_csi2_formats[].
> > - Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
> > - Set INTSTATE after PL-11 is confirmed to match flow chart in
> >   datasheet.
> > - Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
> > - Add Maxime's and laurent's tags.
> > ---
> >  drivers/media/platform/rcar-vin/Kconfig |   12 +
> >  drivers/media/platform/rcar-vin/Makefile|1 +
> >  drivers/media/platform/rcar-vin/rcar-csi2.c | 1101 +++
> >  3 files changed, 1114 insertions(+)
> >  create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c
> >
> > diff --git a/drivers/media/platform/rcar-vin/Kconfig 
> > b/drivers/media/platform/rcar-vin/Kconfig
> > index 8fa7ee468c63afb9..d5835da6d4100d87 100644
> > --- a/drivers/media/platform/rcar-vin/Kconfig
> > +++ b/drivers/media/platform/rcar-vin/Kconfig
> > @@ -1,3 +1,15 @@
> > +config VIDEO_RCAR_CSI2
> > +   tristate "R-Car MIPI CSI-2 Receiver"
> > +   depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF
> > +   depends on ARCH_RENESAS || COMPILE_TEST
> > +   select V4L2_FWNODE
> > +   help
> > + Support for Renesas R-Car MIPI CSI-2 receiver.
> > + Supports R-Car Gen3 SoCs.
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called rcar-csi2.
> > +
> >  config VIDEO_RCAR_VIN
> > tristate "R-Car Video Input (VIN) Driver"
> > depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF && HAS_DMA && 
> > MEDIA_CONTROLLER
> > diff --git a/drivers/media/platform/rcar-vin/Makefile 
> > b/drivers/media/platform/rcar-vin/Makefile
> > index 48c5632c21dc060b..5ab803d3e7c1aa57 100644
> > --- a/drivers/media/platform/rcar-vin/Makefile
> > +++ b/drivers/media/platform/rcar-vin/Makefile
> > @@ -1,3 +1,4 @@
> >  rcar-vin-objs = rcar-core.o rcar-dma.o rcar-v4l2.o
> >
> > +obj-$(CONFIG_VIDEO_RCAR_CSI2) += rcar-csi2.o
> >  obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin.o
> > diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
> > b/drivers/media/platform/rcar-vin/rcar-csi2.c
> > new file mode 100644
> > index ..b19374f1516464dc
> > --- /dev/null
> > +++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
> &g

[PATCH v15 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-05-13 Thread Niklas Söderlund
A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
connected between the video sources and the video grabbers (VIN).

Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

---

* Changes since v14
- Data sheet update changed init sequence for PHY forcing a restructure
  of the driver. The restructure was so big I felt compel to drop all
  review tags :-(
- The change was that the Renesas H3 procedure was aligned with other
  SoC in the Gen3 family procedure. I had kept the rework as separate
  patches and was planing to post once original driver with H3 and M3-W
  support where merged. As review tags are dropped I chosen to squash
  those patches into 2/2.
- Add support for Gen3 V3M.
- Add support for Gen3 M3-N.
- Set PHTC_TESTCLR when stopping the PHY.
- Revert back to the v12 and earlier phypll calculation as it turns out
  it was correct after all.

* Changes since v13
- Change return rcar_csi2_formats + i to return _csi2_formats[i].
- Add define for PHCLM_STOPSTATECKL.
- Update spelling in comments.
- Update calculation in rcar_csi2_calc_phypll() according to
  https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
  before v14 did not take into account that 2 bits per sample is
  transmitted.
- Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
  statement to set correct number of lanes to enable.
- Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
  style of rest of file.
- Switch to %u instead of 0x%x when printing bus type.
- Switch to %u instead of %d for priv->lanes which is unsigned.
- Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
  rcar_csi2_formats[].
- Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
- Set INTSTATE after PL-11 is confirmed to match flow chart in
  datasheet.
- Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
- Add Maxime's and laurent's tags.
---
 drivers/media/platform/rcar-vin/Kconfig |   12 +
 drivers/media/platform/rcar-vin/Makefile|1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c | 1101 +++
 3 files changed, 1114 insertions(+)
 create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c

diff --git a/drivers/media/platform/rcar-vin/Kconfig 
b/drivers/media/platform/rcar-vin/Kconfig
index 8fa7ee468c63afb9..d5835da6d4100d87 100644
--- a/drivers/media/platform/rcar-vin/Kconfig
+++ b/drivers/media/platform/rcar-vin/Kconfig
@@ -1,3 +1,15 @@
+config VIDEO_RCAR_CSI2
+   tristate "R-Car MIPI CSI-2 Receiver"
+   depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF
+   depends on ARCH_RENESAS || COMPILE_TEST
+   select V4L2_FWNODE
+   help
+ Support for Renesas R-Car MIPI CSI-2 receiver.
+ Supports R-Car Gen3 SoCs.
+
+ To compile this driver as a module, choose M here: the
+ module will be called rcar-csi2.
+
 config VIDEO_RCAR_VIN
tristate "R-Car Video Input (VIN) Driver"
depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF && HAS_DMA && 
MEDIA_CONTROLLER
diff --git a/drivers/media/platform/rcar-vin/Makefile 
b/drivers/media/platform/rcar-vin/Makefile
index 48c5632c21dc060b..5ab803d3e7c1aa57 100644
--- a/drivers/media/platform/rcar-vin/Makefile
+++ b/drivers/media/platform/rcar-vin/Makefile
@@ -1,3 +1,4 @@
 rcar-vin-objs = rcar-core.o rcar-dma.o rcar-v4l2.o
 
+obj-$(CONFIG_VIDEO_RCAR_CSI2) += rcar-csi2.o
 obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin.o
diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
b/drivers/media/platform/rcar-vin/rcar-csi2.c
new file mode 100644
index ..b19374f1516464dc
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
@@ -0,0 +1,1101 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Renesas R-Car MIPI CSI-2 Receiver
+ *
+ * Copyright (C) 2018 Renesas Electronics Corp.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct rcar_csi2;
+
+/* Register offsets and bits */
+
+/* Control Timing Select */
+#define TREF_REG   0x00
+#define TREF_TREF  BIT(0)
+
+/* Software Reset */
+#define SRST_REG   0x04
+#define SRST_SRST  BIT(0)
+
+/* PHY Operation Control */
+#define PHYCNT_REG 0x08
+#define PHYCNT_SHUTDOWNZ   BIT(17)
+#define PHYCNT_RSTZBIT(16)
+#define PHYCNT_ENABLECLK   BIT(4)
+#define PHYCNT_ENABLE_3BIT(3)
+#define PHYCNT_ENABLE_2BIT(2)
+#define PHYCNT_ENABLE_1BIT(1)
+#define PHYCNT_ENABLE_0BIT(0)
+

[PATCH v15 1/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation

2018-05-13 Thread Niklas Söderlund
Documentation for Renesas R-Car MIPI CSI-2 receiver. The CSI-2 receivers
are located between the video sources (CSI-2 transmitters) and the video
grabbers (VIN) on Gen3 of Renesas R-Car SoC.

Each CSI-2 device is connected to more than one VIN device which
simultaneously can receive video from the same CSI-2 device. Each VIN
device can also be connected to more than one CSI-2 device. The routing
of which links are used is controlled by the VIN devices. There are only
a few possible routes which are set by hardware limitations, which are
different for each SoC in the Gen3 family.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Acked-by: Rob Herring <r...@kernel.org>
Acked-by: Sakari Ailus <sakari.ai...@linux.intel.com>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>

---

* Changes since v14.
- Added compatible string for R8A77965 and R8A77970.
- s/Port 0/port@0/
- s/Port 1/port@1/
- s/Endpoint 0/endpoint@0/

* Changes since v13
- Add Laurent's tag.
---
 .../bindings/media/renesas,rcar-csi2.txt  | 101 ++
 MAINTAINERS   |   1 +
 2 files changed, 102 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt

diff --git a/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt 
b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
new file mode 100644
index ..2d385b65b275bc58
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
@@ -0,0 +1,101 @@
+Renesas R-Car MIPI CSI-2
+
+
+The R-Car CSI-2 receiver device provides MIPI CSI-2 capabilities for the
+Renesas R-Car family of devices. It is used in conjunction with the
+R-Car VIN module, which provides the video capture capabilities.
+
+Mandatory properties
+
+ - compatible: Must be one or more of the following
+   - "renesas,r8a7795-csi2" for the R8A7795 device.
+   - "renesas,r8a7796-csi2" for the R8A7796 device.
+   - "renesas,r8a77965-csi2" for the R8A77965 device.
+   - "renesas,r8a77970-csi2" for the R8A77970 device.
+
+ - reg: the register base and size for the device registers
+ - interrupts: the interrupt for the device
+ - clocks: reference to the parent clock
+
+The device node shall contain two 'port' child nodes according to the
+bindings defined in Documentation/devicetree/bindings/media/
+video-interfaces.txt. port@0 shall connect to the CSI-2 source. port@1
+shall connect to all the R-Car VIN modules that have a hardware
+connection to the CSI-2 receiver.
+
+- port@0- Video source (mandatory)
+   - endpoint@0 - sub-node describing the endpoint that is the video source
+
+- port@1 - VIN instances (optional)
+   - One endpoint sub-node for every R-Car VIN instance which is connected
+ to the R-Car CSI-2 receiver.
+
+Example:
+
+   csi20: csi2@fea8 {
+   compatible = "renesas,r8a7796-csi2";
+   reg = <0 0xfea8 0 0x1>;
+   interrupts = <0 184 IRQ_TYPE_LEVEL_HIGH>;
+   clocks = < CPG_MOD 714>;
+   power-domains = < R8A7796_PD_ALWAYS_ON>;
+   resets = < 714>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@0 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <0>;
+
+   csi20_in: endpoint@0 {
+   reg = <0>;
+   clock-lanes = <0>;
+   data-lanes = <1>;
+   remote-endpoint = <_txb>;
+   };
+   };
+
+   port@1 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <1>;
+
+   csi20vin0: endpoint@0 {
+   reg = <0>;
+   remote-endpoint = <>;
+   };
+   csi20vin1: endpoint@1 {
+   reg = <1>;
+   remote-endpoint = <>;
+   };
+   csi20vin2: endpoint@2 {
+   reg = <2>;
+   remote-endpoint = <>;
+   };
+   csi20vin3: endpoint@3 {
+   

[PATCH v15 0/2] rcar-csi2: add Renesas R-Car MIPI CSI-2

2018-05-13 Thread Niklas Söderlund
sub-notifier V4L2 API
- Add delay when resetting the IP to allow for a proper reset
- Fix bug in s_stream error path where the usage count was off if an
  error was hit.
- Add support for H3 ES2.0

Changes since v7:
- Rebase on top of the latest incremental async patches.
- Fix comments on DT documentation.
- Use v4l2_ctrl_g_ctrl_int64() instead of v4l2_g_ext_ctrls().
- Handle try formats in .set_fmt() and .get_fmt().
- Don't call v4l2_device_register_subdev_nodes() as this is not needed
  with the complete() callbacks synchronized.
- Fix line over 80 chars.
- Fix varies spelling mistakes.

Changes since v6:
- Rebased on top of Sakaris fwnode patches.
- Changed of RCAR_CSI2_PAD_MAX to NR_OF_RCAR_CSI2_PAD.
- Remove assumption about unknown media bus type, thanks Sakari for
  pointing this out.
- Created table for supported format information instead of scattering
  this information around the driver, thanks Sakari!
- Small newline fixes and reduce some indentation levels

Changes since v5:
- Make use of the incremental async subnotifer and helper to map DT
  endpoint to media pad number. This moves functionality which
  previously in the Gen3 patches for R-Car VIN driver to this R-Car
  CSI-2 driver. This is done in preparation to support the ADV7482
  driver in development by Kieran which will register more then one
  subdevice and the CSI-2 driver needs to cope wit this. Further more it
  prepares the driver for another use-case where more then one subdevice
  is present upstream for the CSI-2.
- Small cleanups.
- Add explicit include for linux/io.h, thanks Kieran.

Changes since v4:
- Match SoC part numbers and drop trailing space in documentation,
  thanks Geert for pointing this out.
- Clarify that the driver is a CSI-2 receiver by supervised
  s/interface/receiver/, thanks Laurent.
- Add entries in Kconfig and Makefile alphabetically instead of append.
- Rename struct rcar_csi2 member swap to lane_swap.
- Remove macros to wrap calls to dev_{dbg,info,warn,err}.
- Add wrappers for ioread32 and iowrite32.
- Remove unused interrupt handler, but keep checking in probe that there
  are a interrupt define in DT.
- Rework how to wait for LP-11 state, thanks Laurent for the great idea!
- Remove unneeded delay in rcar_csi2_reset()
- Remove check for duplicated lane id:s from DT parsing. Broken out to a
  separate patch adding this check directly to v4l2_of_parse_endpoint().
- Fixed rcar_csi2_start() to ask it's source subdevice for information
  about pixel rate and frame format. With this change having
  {set,get}_fmt operations became redundant, it was only used for
  figuring out this out so dropped them.
- Tabulated frequency settings map.
- Dropped V4L2_SUBDEV_FL_HAS_DEVNODE it should never have been set.
- Switched from MEDIA_ENT_F_ATV_DECODER to
  MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER as entity function. I can't
  find a more suitable function, and what the hardware do is to fetch
  video from an external chip and passes it on to a another SoC internal
  IP it's sort of a formatter.
- Break out DT documentation and code in two patches.

Changes since v3:
- Update DT binding documentation with input from Geert Uytterhoeven,
  thanks

Changes since v2:
- Added media control pads as this is needed by the new rcar-vin driver.
- Update DT bindings after review comments and to add r8a7796 support.
- Add get_fmt handler.
- Fix media bus format error s/YUYV8/UYVY8/

Changes since v1:
- Drop dependency on a pad aware s_stream operation.
- Use the DT bindings format "renesas,-", thanks Geert
  for pointing this out.

Niklas Söderlund (2):
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

 .../bindings/media/renesas,rcar-csi2.txt  |  101 ++
 MAINTAINERS   |1 +
 drivers/media/platform/rcar-vin/Kconfig   |   12 +
 drivers/media/platform/rcar-vin/Makefile  |1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c   | 1101 +
 5 files changed, 1216 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
 create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c

-- 
2.17.0



Re: [PATCH v14 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-05-13 Thread Niklas Söderlund
Hi Laurent,

On 2018-04-28 19:01:37 +0300, Laurent Pinchart wrote:
> Hi Niklas,
> 
> On Friday, 27 April 2018 02:28:32 EEST Niklas Söderlund wrote:
> > On 2018-04-27 00:30:25 +0300, Laurent Pinchart wrote:
> > > On Thursday, 26 April 2018 23:21:21 EEST Niklas Söderlund wrote:
> > >> A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
> > >> supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
> > >> connected between the video sources and the video grabbers (VIN).
> > >> 
> > >> Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.
> > >> 
> > >> Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > >> Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
> > >> Reviewed-by: Maxime Ripard <maxime.rip...@bootlin.com>
> > >> Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
> > >> 
> > >> ---
> > >> 
> > >> * Changes since v13
> > >> - Change return rcar_csi2_formats + i to return _csi2_formats[i].
> > >> - Add define for PHCLM_STOPSTATECKL.
> > >> - Update spelling in comments.
> > >> - Update calculation in rcar_csi2_calc_phypll() according to
> > >>   https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
> > >>   before v14 did not take into account that 2 bits per sample is
> > >>   transmitted.
> > > 
> > > Just one small comment about this, please see below.
> > > 
> > >> - Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
> > >>   statement to set correct number of lanes to enable.
> > >> - Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
> > >>   style of rest of file.
> > >> - Switch to %u instead of 0x%x when printing bus type.
> > >> - Switch to %u instead of %d for priv->lanes which is unsigned.
> > >> - Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
> > >>   rcar_csi2_formats[].
> > >> - Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
> > >> - Set INTSTATE after PL-11 is confirmed to match flow chart in
> > >>   datasheet.
> > >> - Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
> > >> - Add Maxime's and laurent's tags.
> > >> ---
> > >> 
> > >>  drivers/media/platform/rcar-vin/Kconfig |  12 +
> > >>  drivers/media/platform/rcar-vin/Makefile|   1 +
> > >>  drivers/media/platform/rcar-vin/rcar-csi2.c | 883 
> > >>  3 files changed, 896 insertions(+)
> > >>  create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c
> > > 
> > > [snip]
> > > 
> > >> diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c
> > >> b/drivers/media/platform/rcar-vin/rcar-csi2.c new file mode 100644
> > >> index ..49b29d5680f9d80b
> > >> --- /dev/null
> > > > +++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
> > > 
> > > [snip]
> > > 
> > >> +static int rcar_csi2_calc_phypll(struct rcar_csi2 *priv, unsigned int
> > >> bpp,
> > >> + u32 *phypll)
> > >> +{
> > >> +const struct phypll_hsfreqrange *hsfreq;
> > >> +struct v4l2_subdev *source;
> > >> +struct v4l2_ctrl *ctrl;
> > >> +u64 mbps;
> > >> +
> > >> +if (!priv->remote)
> > >> +return -ENODEV;
> > >> +
> > >> +source = priv->remote;
> > >> +
> > >> +/* Read the pixel rate control from remote */
> > >> +ctrl = v4l2_ctrl_find(source->ctrl_handler, 
> > >> V4L2_CID_PIXEL_RATE);
> > >> +if (!ctrl) {
> > >> +dev_err(priv->dev, "no pixel rate control in subdev 
> > >> %s\n",
> > >> +source->name);
> > >> +return -EINVAL;
> > >> +}
> > >> +
> > >> +/*
> > >> + * Calculate the phypll in mbps (from v4l2 documentation)
> > >> + * link_freq = (pixel_rate * bits_per_sample) / (2 * 
> > >> nr_of_lanes)
> > >> + */
> > >> +mbps = v4l2_ctrl_g_ctrl_int64(ctrl) * bpp;
> > &

[PATCH] media: rcar-vin: enable support for r8a77965

2018-05-13 Thread Niklas Söderlund
Add the SoC specific information for Renesas r8a77965.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 48 +
 1 file changed, 48 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index d3072e166a1ca24f..f7bfd05accbfde67 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -974,6 +974,50 @@ static const struct rvin_info rcar_info_r8a7796 = {
.routes = rcar_info_r8a7796_routes,
 };
 
+static const struct rvin_group_route _rcar_info_r8a77965_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
+   { /* Sentinel */ }
+};
+
+static const struct rvin_info rcar_info_r8a77965 = {
+   .model = RCAR_GEN3,
+   .use_mc = true,
+   .max_width = 4096,
+   .max_height = 4096,
+   .routes = _rcar_info_r8a77965_routes,
+};
+
 static const struct rvin_group_route _rcar_info_r8a77970_routes[] = {
{ .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
{ .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
@@ -1030,6 +1074,10 @@ static const struct of_device_id rvin_of_id_table[] = {
.compatible = "renesas,vin-r8a7796",
.data = _info_r8a7796,
},
+   {
+   .compatible = "renesas,vin-r8a77965",
+   .data = _info_r8a77965,
+   },
{
.compatible = "renesas,vin-r8a77970",
.data = _info_r8a77970,
-- 
2.17.0



[PATCH] media: dt-bindings: media: rcar_vin: add support for r8a77965

2018-05-13 Thread Niklas Söderlund
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 Documentation/devicetree/bindings/media/rcar_vin.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
b/Documentation/devicetree/bindings/media/rcar_vin.txt
index a19517e1c669eb35..c2c57dcf73f4851b 100644
--- a/Documentation/devicetree/bindings/media/rcar_vin.txt
+++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
@@ -21,6 +21,7 @@ on Gen3 platforms to a CSI-2 receiver.
- "renesas,vin-r8a7794" for the R8A7794 device
- "renesas,vin-r8a7795" for the R8A7795 device
- "renesas,vin-r8a7796" for the R8A7796 device
+   - "renesas,vin-r8a77965" for the R8A77965 device
- "renesas,vin-r8a77970" for the R8A77970 device
- "renesas,rcar-gen2-vin" for a generic R-Car Gen2 or RZ/G1 compatible
  device.
-- 
2.17.0



Re: [PATCH] media: rcar-vin: Drop unnecessary register properties from example vin port

2018-05-13 Thread Niklas Söderlund
Hi Simon,

Thanks for your patch.

On 2018-05-09 20:45:58 +0200, Simon Horman wrote:
> The example vin port node does not have an address and thus does not
> need address-cells or address size-properties.
> 
> Signed-off-by: Simon Horman <horms+rene...@verge.net.au>

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  Documentation/devicetree/bindings/media/rcar_vin.txt | 3 ---
>  1 file changed, 3 deletions(-)
> 
> diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
> b/Documentation/devicetree/bindings/media/rcar_vin.txt
> index a19517e1c669..2a0c59e97f40 100644
> --- a/Documentation/devicetree/bindings/media/rcar_vin.txt
> +++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
> @@ -107,9 +107,6 @@ Board setup example for Gen2 platforms (vin1 composite 
> video input)
>  status = "okay";
>  
>  port {
> -#address-cells = <1>;
> -#size-cells = <0>;
> -
>  vin1ep0: endpoint {
>  remote-endpoint = <>;
>  bus-width = <8>;
> -- 
> 2.11.0
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v2] media: i2c: adv748x: Fix pixel rate values

2018-05-12 Thread Niklas Söderlund
Hi Kieran,

Thanks for your feedback.

On 2018-05-12 09:04:18 +0100, Kieran Bingham wrote:
> Hi Niklas, Laurent,
> 
> Thanks for the updated patch and detailed investigation to get this right.
> 
> On 11/05/18 15:04, Niklas Söderlund wrote:
> > From: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>
> > 
> > The pixel rate, as reported by the V4L2_CID_PIXEL_RATE control, must
> > include both horizontal and vertical blanking. Both the AFE and HDMI
> > receiver program it incorrectly:
> > 
> > - The HDMI receiver goes to the trouble of removing blanking to compute
> > the rate of active pixels. This is easy to fix by removing the
> > computation and returning the incoming pixel clock rate directly.
> > 
> > - The AFE performs similar calculation, while it should simply return
> > the fixed pixel rate for analog sources, mandated by the ADV748x to be
> > 14.3180180 MHz.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>
> > [Niklas: Update AFE fixed pixel rate]
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> 
> Reviewed-by: Kieran Bingham <kieran.bingham+rene...@ideasonboard.com>

Thanks,

> 
> Does this still require changes to the CSI2 driver to be synchronised?
> (or does it not matter as the CSI2 isn't upstream yet)...

Yes and no :-)

All but v14 of the R-Car CSI-2 patches use the method to calculate the 
link speed which works with this patch. v15 I'm hoping to post early 
next week will go back to the v13 way of interpret the PIXEL_RATE so all 
is well. But for the latest renesas-drivers release the CSI-2 v14 is 
included so if you work on that base this patch will be troublesome for 
CVBS capture.

> 
> --
> Kieran
> 
> 
> > ---
> > 
> > * Changes since v1
> > - Update AFE fixed pixel rate.
> > ---
> >  drivers/media/i2c/adv748x/adv748x-afe.c  | 12 ++--
> >  drivers/media/i2c/adv748x/adv748x-hdmi.c |  8 +---
> >  2 files changed, 7 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/media/i2c/adv748x/adv748x-afe.c 
> > b/drivers/media/i2c/adv748x/adv748x-afe.c
> > index 61514bae7e5ceb42..edd25e895e5dec3c 100644
> > --- a/drivers/media/i2c/adv748x/adv748x-afe.c
> > +++ b/drivers/media/i2c/adv748x/adv748x-afe.c
> > @@ -321,17 +321,17 @@ static const struct v4l2_subdev_video_ops 
> > adv748x_afe_video_ops = {
> >  static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
> >  {
> > struct v4l2_subdev *tx;
> > -   unsigned int width, height, fps;
> >  
> > tx = adv748x_get_remote_sd(>pads[ADV748X_AFE_SOURCE]);
> > if (!tx)
> > return -ENOLINK;
> >  
> > -   width = 720;
> > -   height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
> > -   fps = afe->curr_norm & V4L2_STD_525_60 ? 30 : 25;
> > -
> > -   return adv748x_csi2_set_pixelrate(tx, width * height * fps);
> > +   /*
> > +* The ADV748x ADC sampling frequency is twice the externally supplied
> > +* clock whose frequency is required to be 28.63636 MHz. It oversamples
> > +* with a factor of 4 resulting in a pixel rate of 14.3180180 MHz.
> > +*/
> > +   return adv748x_csi2_set_pixelrate(tx, 14318180);
> >  }
> >  
> >  static int adv748x_afe_enum_mbus_code(struct v4l2_subdev *sd,
> > diff --git a/drivers/media/i2c/adv748x/adv748x-hdmi.c 
> > b/drivers/media/i2c/adv748x/adv748x-hdmi.c
> > index 10d229a4f08868f7..aecc2a84dfecbec8 100644
> > --- a/drivers/media/i2c/adv748x/adv748x-hdmi.c
> > +++ b/drivers/media/i2c/adv748x/adv748x-hdmi.c
> > @@ -402,8 +402,6 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
> > adv748x_hdmi *hdmi)
> >  {
> > struct v4l2_subdev *tx;
> > struct v4l2_dv_timings timings;
> > -   struct v4l2_bt_timings *bt = 
> > -   unsigned int fps;
> >  
> > tx = adv748x_get_remote_sd(>pads[ADV748X_HDMI_SOURCE]);
> > if (!tx)
> > @@ -411,11 +409,7 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
> > adv748x_hdmi *hdmi)
> >  
> > adv748x_hdmi_query_dv_timings(>sd, );
> >  
> > -   fps = DIV_ROUND_CLOSEST_ULL(bt->pixelclock,
> > -   V4L2_DV_BT_FRAME_WIDTH(bt) *
> > -   V4L2_DV_BT_FRAME_HEIGHT(bt));
> > -
> > -   return adv748x_csi2_set_pixelrate(tx, bt->width * bt->height * fps);
> > +   return adv748x_csi2_set_pixelrate(tx, timings.bt.pixelclock);
> >  }
> >  
> >  static int adv748x_hdmi_enum_mbus_code(struct v4l2_subdev *sd,
> > 
> 




-- 
Regards,
Niklas Söderlund


[PATCH v2 1/2] Revert "media: rcar-vin: enable field toggle after a set number of lines for Gen3"

2018-05-11 Thread Niklas Söderlund
The offending commit was an attempt to fix the issue of writing outside
the capture buffer for VIN Gen3. Unfortunately it only fixed the symptom
of the problem to such a degree I could no longer reproduce it. Revert
the offending commit before a proper fix can be added in a follow-up
patch.

This reverts commit 015060cb7795eac34454696cc9c9f8b76926a401.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 20 +---
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index b41ba9a4a2b3ac90..ac07f99e3516a620 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -124,9 +124,7 @@
 #define VNDMR2_VPS (1 << 30)
 #define VNDMR2_HPS (1 << 29)
 #define VNDMR2_FTEV(1 << 17)
-#define VNDMR2_FTEH(1 << 16)
 #define VNDMR2_VLV(n)  ((n & 0xf) << 12)
-#define VNDMR2_HLV(n)  ((n) & 0xfff)
 
 /* Video n CSI2 Interface Mode Register (Gen3) */
 #define VNCSI_IFMD_DES1(1 << 26)
@@ -614,9 +612,8 @@ void rvin_crop_scale_comp(struct rvin_dev *vin)
 
 static int rvin_setup(struct rvin_dev *vin)
 {
-   u32 vnmc, dmr, dmr2, interrupts, lines;
+   u32 vnmc, dmr, dmr2, interrupts;
bool progressive = false, output_is_yuv = false, input_is_yuv = false;
-   bool halfsize = false;
 
switch (vin->format.field) {
case V4L2_FIELD_TOP:
@@ -631,15 +628,12 @@ static int rvin_setup(struct rvin_dev *vin)
/* Use BT if video standard can be read and is 60 Hz format */
if (!vin->info->use_mc && vin->std & V4L2_STD_525_60)
vnmc = VNMC_IM_FULL | VNMC_FOC;
-   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_TB:
vnmc = VNMC_IM_FULL;
-   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_BT:
vnmc = VNMC_IM_FULL | VNMC_FOC;
-   halfsize = true;
break;
case V4L2_FIELD_NONE:
vnmc = VNMC_IM_ODD_EVEN;
@@ -682,15 +676,11 @@ static int rvin_setup(struct rvin_dev *vin)
break;
}
 
-   if (vin->info->model == RCAR_GEN3) {
-   /* Enable HSYNC Field Toggle mode after height HSYNC inputs. */
-   lines = vin->format.height / (halfsize ? 2 : 1);
-   dmr2 = VNDMR2_FTEH | VNDMR2_HLV(lines);
-   vin_dbg(vin, "Field Toogle after %u lines\n", lines);
-   } else {
-   /* Enable VSYNC Field Toogle mode after one VSYNC input. */
+   /* Enable VSYNC Field Toogle mode after one VSYNC input */
+   if (vin->info->model == RCAR_GEN3)
+   dmr2 = VNDMR2_FTEV;
+   else
dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
-   }
 
/* Hsync Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
-- 
2.17.0



[PATCH v2 2/2] rcar-vin: fix crop and compose handling for Gen3

2018-05-11 Thread Niklas Söderlund
When refactoring the Gen3 enablement series crop and compose handling
where broken. This went unnoticed but can result in writing out side the
capture buffer. Fix this by restoring the crop and compose to reflect
the format dimensions as we have not yet enabled the scaler for Gen3.

Fixes: 5e7c623632fcf8f5 ("media: rcar-vin: use different v4l2 operations in 
media controller mode")
Reported-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-v4l2.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-v4l2.c 
b/drivers/media/platform/rcar-vin/rcar-v4l2.c
index 2fb8587116f25a4f..e78fba84d59028ef 100644
--- a/drivers/media/platform/rcar-vin/rcar-v4l2.c
+++ b/drivers/media/platform/rcar-vin/rcar-v4l2.c
@@ -702,6 +702,12 @@ static int rvin_mc_s_fmt_vid_cap(struct file *file, void 
*priv,
 
vin->format = f->fmt.pix;
 
+   vin->crop.top = 0;
+   vin->crop.left = 0;
+   vin->crop.width = vin->format.width;
+   vin->crop.height = vin->format.height;
+   vin->compose = vin->crop;
+
return 0;
 }
 
-- 
2.17.0



[PATCH v2 0/2] Fix potential buffer overrun root cause

2018-05-11 Thread Niklas Söderlund
Hi,

Commit 015060cb7795eac3 ("media: rcar-vin: enable field toggle after a
set number of lines for Gen3") was an attempt to fix the issue of
writing outside the capture buffer for VIN Gen3. Unfortunately it only
fixed a symptom of a problem to such a degree I could no longer
reproduce it.

Jacopo on the other hand working on a different setup still ran into the
issue. And he even figured out the root cause of the issue. When I
submitted the original VIN Gen3 support I had when addressing a review
comment missed to keep the crop and compose dimensions in sync with the
requested format resulting in the DMA engine not properly stopping
before writing outside the buffer.

This series reverts the incorrect fix in 1/2 and applies a correct one
in 2/2. I think this should be picked up for v4.18.

* Changes since v1
- Add commit message to 1/2.

Niklas Söderlund (2):
  Revert "media: rcar-vin: enable field toggle after a set number of
lines for Gen3"
  rcar-vin: fix crop and compose handling for Gen3

 drivers/media/platform/rcar-vin/rcar-dma.c  | 20 +---
 drivers/media/platform/rcar-vin/rcar-v4l2.c |  6 ++
 2 files changed, 11 insertions(+), 15 deletions(-)

-- 
2.17.0



[PATCH 2/2] rcar-vin: fix crop and compose handling for Gen3

2018-05-11 Thread Niklas Söderlund
When refactoring the Gen3 enablement series crop and compose handling
where broken. This went unnoticed but can result in writing out side the
capture buffer. Fix this by restoring the crop and compose to reflect
the format dimensions as we have not yet enabled the scaler for Gen3.

Fixes: 5e7c623632fcf8f5 ("media: rcar-vin: use different v4l2 operations in 
media controller mode")
Reported-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-v4l2.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-v4l2.c 
b/drivers/media/platform/rcar-vin/rcar-v4l2.c
index 2fb8587116f25a4f..e78fba84d59028ef 100644
--- a/drivers/media/platform/rcar-vin/rcar-v4l2.c
+++ b/drivers/media/platform/rcar-vin/rcar-v4l2.c
@@ -702,6 +702,12 @@ static int rvin_mc_s_fmt_vid_cap(struct file *file, void 
*priv,
 
vin->format = f->fmt.pix;
 
+   vin->crop.top = 0;
+   vin->crop.left = 0;
+   vin->crop.width = vin->format.width;
+   vin->crop.height = vin->format.height;
+   vin->compose = vin->crop;
+
return 0;
 }
 
-- 
2.17.0



[PATCH 1/2] Revert "media: rcar-vin: enable field toggle after a set number of lines for Gen3"

2018-05-11 Thread Niklas Söderlund
This reverts commit 015060cb7795eac34454696cc9c9f8b76926a401.
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 20 +---
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index b41ba9a4a2b3ac90..ac07f99e3516a620 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -124,9 +124,7 @@
 #define VNDMR2_VPS (1 << 30)
 #define VNDMR2_HPS (1 << 29)
 #define VNDMR2_FTEV(1 << 17)
-#define VNDMR2_FTEH(1 << 16)
 #define VNDMR2_VLV(n)  ((n & 0xf) << 12)
-#define VNDMR2_HLV(n)  ((n) & 0xfff)
 
 /* Video n CSI2 Interface Mode Register (Gen3) */
 #define VNCSI_IFMD_DES1(1 << 26)
@@ -614,9 +612,8 @@ void rvin_crop_scale_comp(struct rvin_dev *vin)
 
 static int rvin_setup(struct rvin_dev *vin)
 {
-   u32 vnmc, dmr, dmr2, interrupts, lines;
+   u32 vnmc, dmr, dmr2, interrupts;
bool progressive = false, output_is_yuv = false, input_is_yuv = false;
-   bool halfsize = false;
 
switch (vin->format.field) {
case V4L2_FIELD_TOP:
@@ -631,15 +628,12 @@ static int rvin_setup(struct rvin_dev *vin)
/* Use BT if video standard can be read and is 60 Hz format */
if (!vin->info->use_mc && vin->std & V4L2_STD_525_60)
vnmc = VNMC_IM_FULL | VNMC_FOC;
-   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_TB:
vnmc = VNMC_IM_FULL;
-   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_BT:
vnmc = VNMC_IM_FULL | VNMC_FOC;
-   halfsize = true;
break;
case V4L2_FIELD_NONE:
vnmc = VNMC_IM_ODD_EVEN;
@@ -682,15 +676,11 @@ static int rvin_setup(struct rvin_dev *vin)
break;
}
 
-   if (vin->info->model == RCAR_GEN3) {
-   /* Enable HSYNC Field Toggle mode after height HSYNC inputs. */
-   lines = vin->format.height / (halfsize ? 2 : 1);
-   dmr2 = VNDMR2_FTEH | VNDMR2_HLV(lines);
-   vin_dbg(vin, "Field Toogle after %u lines\n", lines);
-   } else {
-   /* Enable VSYNC Field Toogle mode after one VSYNC input. */
+   /* Enable VSYNC Field Toogle mode after one VSYNC input */
+   if (vin->info->model == RCAR_GEN3)
+   dmr2 = VNDMR2_FTEV;
+   else
dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
-   }
 
/* Hsync Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
-- 
2.17.0



[PATCH 0/2] rcar-vin: Fix potential buffer overrun root cause

2018-05-11 Thread Niklas Söderlund
Hi,

Commit 015060cb7795eac3 ("media: rcar-vin: enable field toggle after a 
set number of lines for Gen3") was an attempt to fix the issue of 
writing outside the capture buffer for VIN Gen3. Unfortunately it only 
fixed a symptom of a problem to such a degree I could no longer 
reproduce it.

Jacopo on the other hand working on a different setup still ran into the 
issue. And he even figured out the root cause of the issue. When I 
submitted the original VIN Gen3 support I had when addressing a review 
comment missed to keep the crop and compose dimensions in sync with the 
requested format resulting in the DMA engine not properly stopping 
before writing outside the buffer.

This series reverts the incorrect fix in 1/2 and applies a correct one 
in 2/2. I think this should be picked up for v4.18.

Niklas Söderlund (2):
  Revert "media: rcar-vin: enable field toggle after a set number of
lines for Gen3"
  rcar-vin: fix crop and compose handling for Gen3

 drivers/media/platform/rcar-vin/rcar-dma.c  | 20 +---
 drivers/media/platform/rcar-vin/rcar-v4l2.c |  6 ++
 2 files changed, 11 insertions(+), 15 deletions(-)

-- 
2.17.0



[PATCH v2] media: i2c: adv748x: Fix pixel rate values

2018-05-11 Thread Niklas Söderlund
From: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>

The pixel rate, as reported by the V4L2_CID_PIXEL_RATE control, must
include both horizontal and vertical blanking. Both the AFE and HDMI
receiver program it incorrectly:

- The HDMI receiver goes to the trouble of removing blanking to compute
the rate of active pixels. This is easy to fix by removing the
computation and returning the incoming pixel clock rate directly.

- The AFE performs similar calculation, while it should simply return
the fixed pixel rate for analog sources, mandated by the ADV748x to be
14.3180180 MHz.

Signed-off-by: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>
[Niklas: Update AFE fixed pixel rate]
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

---

* Changes since v1
- Update AFE fixed pixel rate.
---
 drivers/media/i2c/adv748x/adv748x-afe.c  | 12 ++--
 drivers/media/i2c/adv748x/adv748x-hdmi.c |  8 +---
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/drivers/media/i2c/adv748x/adv748x-afe.c 
b/drivers/media/i2c/adv748x/adv748x-afe.c
index 61514bae7e5ceb42..edd25e895e5dec3c 100644
--- a/drivers/media/i2c/adv748x/adv748x-afe.c
+++ b/drivers/media/i2c/adv748x/adv748x-afe.c
@@ -321,17 +321,17 @@ static const struct v4l2_subdev_video_ops 
adv748x_afe_video_ops = {
 static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
 {
struct v4l2_subdev *tx;
-   unsigned int width, height, fps;
 
tx = adv748x_get_remote_sd(>pads[ADV748X_AFE_SOURCE]);
if (!tx)
return -ENOLINK;
 
-   width = 720;
-   height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
-   fps = afe->curr_norm & V4L2_STD_525_60 ? 30 : 25;
-
-   return adv748x_csi2_set_pixelrate(tx, width * height * fps);
+   /*
+* The ADV748x ADC sampling frequency is twice the externally supplied
+* clock whose frequency is required to be 28.63636 MHz. It oversamples
+* with a factor of 4 resulting in a pixel rate of 14.3180180 MHz.
+*/
+   return adv748x_csi2_set_pixelrate(tx, 14318180);
 }
 
 static int adv748x_afe_enum_mbus_code(struct v4l2_subdev *sd,
diff --git a/drivers/media/i2c/adv748x/adv748x-hdmi.c 
b/drivers/media/i2c/adv748x/adv748x-hdmi.c
index 10d229a4f08868f7..aecc2a84dfecbec8 100644
--- a/drivers/media/i2c/adv748x/adv748x-hdmi.c
+++ b/drivers/media/i2c/adv748x/adv748x-hdmi.c
@@ -402,8 +402,6 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
adv748x_hdmi *hdmi)
 {
struct v4l2_subdev *tx;
struct v4l2_dv_timings timings;
-   struct v4l2_bt_timings *bt = 
-   unsigned int fps;
 
tx = adv748x_get_remote_sd(>pads[ADV748X_HDMI_SOURCE]);
if (!tx)
@@ -411,11 +409,7 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
adv748x_hdmi *hdmi)
 
adv748x_hdmi_query_dv_timings(>sd, );
 
-   fps = DIV_ROUND_CLOSEST_ULL(bt->pixelclock,
-   V4L2_DV_BT_FRAME_WIDTH(bt) *
-   V4L2_DV_BT_FRAME_HEIGHT(bt));
-
-   return adv748x_csi2_set_pixelrate(tx, bt->width * bt->height * fps);
+   return adv748x_csi2_set_pixelrate(tx, timings.bt.pixelclock);
 }
 
 static int adv748x_hdmi_enum_mbus_code(struct v4l2_subdev *sd,
-- 
2.17.0



Re: [PATCH 4/5] media: rcar-vin: Do not use crop if not configured

2018-05-11 Thread Niklas Söderlund
Hi again,

On 2018-05-11 13:10:37 +0200, Niklas Söderlund wrote:
> Hi Jacopo,
> 
> Thanks for your work.
> 
> On 2018-05-11 11:59:40 +0200, Jacopo Mondi wrote:
> > The crop_scale routine uses the crop rectangle memebers to configure the
> > VIN clipping rectangle. When crop is not configured all its fields are
> > 0s, and setting the clipping rectangle vertical and horizontal extensions
> > to (0 - 1) causes the registers to be written with an incorrect
> > 0x value.
> 
> This is an interesting find and a clear bug in my code. But I can't see 
> how crop ever could be 0. When s_fmt is called it always resets the crop 
> and compose width's to the requested format size.
> 
> I'm curious how you found this bug, I tried to reproduce it but could 
> not. 

My bad I was looking at the wrong thing, yes I can reproduce this on 
CSI-2 capture as well. Really nice find!

> This is indeed something we should fix! But I think the proper fix is 
>not allowing crop to be 0 and not treating the symptom in 
>rvin_crop_scale_comp().
> 
> > 
> > Fix this by using the actual format width and height when no crop
> > rectangle has been programmed.
> > 
> > Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> > ---
> >  drivers/media/platform/rcar-vin/rcar-dma.c | 15 +--
> >  1 file changed, 9 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
> > b/drivers/media/platform/rcar-vin/rcar-dma.c
> > index b41ba9a..ea7a120 100644
> > --- a/drivers/media/platform/rcar-vin/rcar-dma.c
> > +++ b/drivers/media/platform/rcar-vin/rcar-dma.c
> > @@ -579,22 +579,25 @@ static void rvin_crop_scale_comp_gen2(struct rvin_dev 
> > *vin)
> >  
> >  void rvin_crop_scale_comp(struct rvin_dev *vin)
> >  {
> > -   /* Set Start/End Pixel/Line Pre-Clip */
> > +   u32 width = vin->crop.width ? vin->crop.left + vin->crop.width :
> > + vin->format.width;
> > +   u32 height = vin->crop.height ? vin->crop.top + vin->crop.height :
> > +   vin->format.height;
> > +
> > +   /* Set Start/End Pixel/Line Pre-Clip if crop is configured. */
> > rvin_write(vin, vin->crop.left, VNSPPRC_REG);
> > -   rvin_write(vin, vin->crop.left + vin->crop.width - 1, VNEPPRC_REG);
> > +   rvin_write(vin, width - 1, VNEPPRC_REG);
> >  
> > switch (vin->format.field) {
> > case V4L2_FIELD_INTERLACED:
> > case V4L2_FIELD_INTERLACED_TB:
> > case V4L2_FIELD_INTERLACED_BT:
> > rvin_write(vin, vin->crop.top / 2, VNSLPRC_REG);
> > -   rvin_write(vin, (vin->crop.top + vin->crop.height) / 2 - 1,
> > -  VNELPRC_REG);
> > +   rvin_write(vin, height / 2 - 1, VNELPRC_REG);
> > break;
> > default:
> > rvin_write(vin, vin->crop.top, VNSLPRC_REG);
> > -   rvin_write(vin, vin->crop.top + vin->crop.height - 1,
> > -  VNELPRC_REG);
> > +   rvin_write(vin, height - 1, VNELPRC_REG);
> > break;
> > }
> >  
> > -- 
> > 2.7.4
> > 
> 
> -- 
> Regards,
> Niklas Söderlund

-- 
Regards,
Niklas Söderlund


Re: [PATCH 4/5] media: rcar-vin: Do not use crop if not configured

2018-05-11 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your work.

On 2018-05-11 11:59:40 +0200, Jacopo Mondi wrote:
> The crop_scale routine uses the crop rectangle memebers to configure the
> VIN clipping rectangle. When crop is not configured all its fields are
> 0s, and setting the clipping rectangle vertical and horizontal extensions
> to (0 - 1) causes the registers to be written with an incorrect
> 0x value.

This is an interesting find and a clear bug in my code. But I can't see 
how crop ever could be 0. When s_fmt is called it always resets the crop 
and compose width's to the requested format size.

I'm curious how you found this bug, I tried to reproduce it but could 
not. This is indeed something we should fix! But I think the proper fix 
is not allowing crop to be 0 and not treating the symptom in 
rvin_crop_scale_comp().

> 
> Fix this by using the actual format width and height when no crop
> rectangle has been programmed.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>
> ---
>  drivers/media/platform/rcar-vin/rcar-dma.c | 15 +--
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
> b/drivers/media/platform/rcar-vin/rcar-dma.c
> index b41ba9a..ea7a120 100644
> --- a/drivers/media/platform/rcar-vin/rcar-dma.c
> +++ b/drivers/media/platform/rcar-vin/rcar-dma.c
> @@ -579,22 +579,25 @@ static void rvin_crop_scale_comp_gen2(struct rvin_dev 
> *vin)
>  
>  void rvin_crop_scale_comp(struct rvin_dev *vin)
>  {
> - /* Set Start/End Pixel/Line Pre-Clip */
> + u32 width = vin->crop.width ? vin->crop.left + vin->crop.width :
> +   vin->format.width;
> + u32 height = vin->crop.height ? vin->crop.top + vin->crop.height :
> + vin->format.height;
> +
> + /* Set Start/End Pixel/Line Pre-Clip if crop is configured. */
>   rvin_write(vin, vin->crop.left, VNSPPRC_REG);
> - rvin_write(vin, vin->crop.left + vin->crop.width - 1, VNEPPRC_REG);
> + rvin_write(vin, width - 1, VNEPPRC_REG);
>  
>   switch (vin->format.field) {
>   case V4L2_FIELD_INTERLACED:
>   case V4L2_FIELD_INTERLACED_TB:
>   case V4L2_FIELD_INTERLACED_BT:
>   rvin_write(vin, vin->crop.top / 2, VNSLPRC_REG);
> - rvin_write(vin, (vin->crop.top + vin->crop.height) / 2 - 1,
> -VNELPRC_REG);
> + rvin_write(vin, height / 2 - 1, VNELPRC_REG);
>   break;
>   default:
>   rvin_write(vin, vin->crop.top, VNSLPRC_REG);
> - rvin_write(vin, vin->crop.top + vin->crop.height - 1,
> -VNELPRC_REG);
> + rvin_write(vin, height - 1, VNELPRC_REG);
>   break;
>   }
>  
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 2/5] media: rcar-vin: Add digital input subdevice parsing

2018-05-11 Thread Niklas Söderlund
> + goto error_unlock_return;
> + }
> +
>   ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
>   vin->group->vin[i]->dev, vin->group->notifier,
> - sizeof(struct v4l2_async_subdev), 1,
> + asd_struct_size, ep.port,
>   rvin_mc_parse_of_endpoint);
> - if (ret) {
> - mutex_unlock(>group->lock);
> - return ret;
> - }
> + if (ret)
> + goto error_unlock_return;
>   }
>  
>   mutex_unlock(>group->lock);
> @@ -785,16 +898,17 @@ static int rvin_mc_parse_of_graph(struct rvin_dev *vin)
>   }
>  
>   return 0;
> +
> +error_unlock_return:
> + mutex_unlock(>group->lock);
> +
> + return ret;
>  }
>  
>  static int rvin_mc_init(struct rvin_dev *vin)
>  {
>   int ret;
>  
> - /* All our sources are CSI-2 */
> - vin->mbus_cfg.type = V4L2_MBUS_CSI2;
> - vin->mbus_cfg.flags = 0;
> -
>   vin->pad.flags = MEDIA_PAD_FL_SINK;
>   ret = media_entity_pads_init(>vdev.entity, 1, >pad);
>   if (ret)
> diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h 
> b/drivers/media/platform/rcar-vin/rcar-vin.h
> index c2aef78..a63f3c6 100644
> --- a/drivers/media/platform/rcar-vin/rcar-vin.h
> +++ b/drivers/media/platform/rcar-vin/rcar-vin.h
> @@ -52,6 +52,19 @@ enum rvin_csi_id {
>  };
>  
>  /**
> + * enum rvin_port_id
> + *
> + * List the available VIN port functions.
> + *
> + * RVIN_PORT_DIGITAL - Input port for digital video connection
> + * RVIN_PORT_CSI2- Input port for CSI-2 video connection
> + */
> +enum rvin_port_id {
> + RVIN_PORT_DIGITAL,
> + RVIN_PORT_CSI2
> +};

This enum is never used in the patch-set is it used later by a different 
patch-set or a left over from refactoring?

> +
> +/**
>   * STOPPED  - No operation in progress
>   * RUNNING  - Operation in progress have buffers
>   * STOPPING - Stopping operation
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH 1/5] media: rcar-vin: Add support for R-Car R8A77995 SoC

2018-05-11 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your patch.

On 2018-05-11 11:59:37 +0200, Jacopo Mondi wrote:
> Add R-Car R8A77995 SoC to the rcar-vin supported ones.
> 
> Signed-off-by: Jacopo Mondi <jacopo+rene...@jmondi.org>

I would move this to be the last patch in the series as a indication 
that capture on R8A77995 is now ready to be used. But for the change 
itself.

Reviewed-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 16 
>  1 file changed, 16 insertions(+)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index d3072e1..e547ef7 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -985,6 +985,10 @@ static const struct rvin_group_route 
> _rcar_info_r8a77970_routes[] = {
>   { /* Sentinel */ }
>  };
>  
> +static const struct rvin_group_route _rcar_info_r8a77995_routes[] = {
> + { /* Sentinel */ }
> +};
> +
>  static const struct rvin_info rcar_info_r8a77970 = {
>   .model = RCAR_GEN3,
>   .use_mc = true,
> @@ -993,6 +997,14 @@ static const struct rvin_info rcar_info_r8a77970 = {
>   .routes = _rcar_info_r8a77970_routes,
>  };
>  
> +static const struct rvin_info rcar_info_r8a77995 = {
> + .model = RCAR_GEN3,
> + .use_mc = true,
> + .max_width = 4096,
> + .max_height = 4096,
> + .routes = _rcar_info_r8a77995_routes,
> +};
> +
>  static const struct of_device_id rvin_of_id_table[] = {
>   {
>   .compatible = "renesas,vin-r8a7778",
> @@ -1034,6 +1046,10 @@ static const struct of_device_id rvin_of_id_table[] = {
>   .compatible = "renesas,vin-r8a77970",
>   .data = _info_r8a77970,
>   },
> + {
> + .compatible = "renesas,vin-r8a77995",
> + .data = _info_r8a77995,
> + },
>   { /* Sentinel */ },
>  };
>  MODULE_DEVICE_TABLE(of, rvin_of_id_table);
> -- 
> 2.7.4
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH] media: i2c: adv748x: Fix pixel rate values

2018-05-05 Thread Niklas Söderlund
Hi Laurent,

On 2018-05-05 12:48:58 +0300, Laurent Pinchart wrote:
> Hi Niklas,
> 
> On Saturday, 5 May 2018 01:58:10 EEST Niklas Söderlund wrote:
> > On 2018-04-25 01:36:42 +0200, Niklas Söderlund wrote:
> > > On 2018-04-21 15:44:44 +0300, Laurent Pinchart wrote:
> > >> The pixel rate, as reported by the V4L2_CID_PIXEL_RATE control, must
> > >> include both horizontal and vertical blanking. Both the AFE and HDMI
> > >> receiver program it incorrectly:
> > >> 
> > >> - The HDMI receiver goes to the trouble of removing blanking to compute
> > >> the rate of active pixels. This is easy to fix by removing the
> > >> computation and returning the incoming pixel clock rate directly.
> > >> 
> > >> - The AFE performs similar calculation, while it should simply return
> > >> the fixed pixel rate for analog sources, mandated by the ADV748x to be
> > >> 28.63636 MHz.
> > >> 
> > >> Signed-off-by: Laurent Pinchart
> > >> <laurent.pinchart+rene...@ideasonboard.com>
> > > 
> > > Reviewed-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > 
> > I'm afraid I would like to revoke this review tag, please see bellow.
> > 
> > > This patch uncovered a calculation error in rcar-csi2 which compensated
> > > for the removing of the blanking in the adv748x, thanks for that! Good
> > > thing that it's not merged yet, will include the fix in the next version
> > > of the CSI-2 driver.
> > > 
> > >> ---
> > >> 
> > >>  drivers/media/i2c/adv748x/adv748x-afe.c  | 11 +--
> > >>  drivers/media/i2c/adv748x/adv748x-hdmi.c |  8 +---
> > >>  2 files changed, 6 insertions(+), 13 deletions(-)
> > >> 
> > >> diff --git a/drivers/media/i2c/adv748x/adv748x-afe.c
> > >> b/drivers/media/i2c/adv748x/adv748x-afe.c index
> > >> 61514bae7e5c..3e18d5ae813b 100644
> > >> --- a/drivers/media/i2c/adv748x/adv748x-afe.c
> > >> +++ b/drivers/media/i2c/adv748x/adv748x-afe.c
> > >> @@ -321,17 +321,16 @@ static const struct v4l2_subdev_video_ops
> > >> adv748x_afe_video_ops = {> > 
> > >>  static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
> > >>  {
> > >>  struct v4l2_subdev *tx;
> > >> -unsigned int width, height, fps;
> > >> 
> > >>  tx = adv748x_get_remote_sd(>pads[ADV748X_AFE_SOURCE]);
> > >>  if (!tx)
> > >>  return -ENOLINK;
> > >> 
> > >> -width = 720;
> > >> -height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
> > >> -fps = afe->curr_norm & V4L2_STD_525_60 ? 30 : 25;
> > >> -
> > >> -return adv748x_csi2_set_pixelrate(tx, width * height * fps);
> > >> +/*
> > >> + * The ADV748x samples analog video signals using an externally
> > >> supplied
> > >> + * clock whose frequency is required to be 28.63636 MHz.
> > >> + */
> > >> +return adv748x_csi2_set_pixelrate(tx, 28636360);
> > 
> > I believe this is wrong. The sampling rate of the AFE is 28.63636 MHz
> > but the pixelrate output on the CSI-TXB might not be right?
> 
> There are only two ways that would allow the pixel rate output of the CSI-TXB 
> to be different than the sampling frequency. The SD core or TXB would need to 
> either resample or buffer the signal. Resampling would change the resolution 
> so that's out of question. Buffering would require a memory buffer large 
> enough to hold one line of data, which would be quite expensive in terms of 
> silicon, and is thus unlikely.

I'm the first to admit I have much to learn here :-) I tried reading up 
and with my limited understanding it seems possible that different 
components are sampled at different rates, and normal pixel clock for 
PAL and NTSC which kept popping up in my research was:

Digital video is a sampled form of analog video. The most common 
sampling schemes in use today are:

  Pixel Clock   HorizHorizVert
   Rate TotalActive
NTSC square pixel  12.27 MHz780  640  525
NTSC CCIR-601  13.5  MHz858  720  525
NTSC 4FSc  14.32 MHz910  768  525
PAL  square pixel  14.75 MHz944  768  625
PAL  CCIR-601  13.5  MHz864  720  625
PAL  4FSc  17.72 MHz   1135  948

Re: [PATCH] media: i2c: adv748x: Fix pixel rate values

2018-05-04 Thread Niklas Söderlund
Hi Laurent,

On 2018-04-25 01:36:42 +0200, Niklas Söderlund wrote:
> Hi Laurent,
> 
> Thanks for your patch.
> 
> On 2018-04-21 15:44:44 +0300, Laurent Pinchart wrote:
> > The pixel rate, as reported by the V4L2_CID_PIXEL_RATE control, must
> > include both horizontal and vertical blanking. Both the AFE and HDMI
> > receiver program it incorrectly:
> > 
> > - The HDMI receiver goes to the trouble of removing blanking to compute
> > the rate of active pixels. This is easy to fix by removing the
> > computation and returning the incoming pixel clock rate directly.
> > 
> > - The AFE performs similar calculation, while it should simply return
> > the fixed pixel rate for analog sources, mandated by the ADV748x to be
> > 28.63636 MHz.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>
> 
> Reviewed-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

I'm afraid I would like to revoke this review tag, please see bellow.

> 
> This patch uncovered a calculation error in rcar-csi2 which compensated 
> for the removing of the blanking in the adv748x, thanks for that! Good 
> thing that it's not merged yet, will include the fix in the next version 
> of the CSI-2 driver.
> 
> > ---
> >  drivers/media/i2c/adv748x/adv748x-afe.c  | 11 +--
> >  drivers/media/i2c/adv748x/adv748x-hdmi.c |  8 +---
> >  2 files changed, 6 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/media/i2c/adv748x/adv748x-afe.c 
> > b/drivers/media/i2c/adv748x/adv748x-afe.c
> > index 61514bae7e5c..3e18d5ae813b 100644
> > --- a/drivers/media/i2c/adv748x/adv748x-afe.c
> > +++ b/drivers/media/i2c/adv748x/adv748x-afe.c
> > @@ -321,17 +321,16 @@ static const struct v4l2_subdev_video_ops 
> > adv748x_afe_video_ops = {
> >  static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
> >  {
> > struct v4l2_subdev *tx;
> > -   unsigned int width, height, fps;
> >  
> > tx = adv748x_get_remote_sd(>pads[ADV748X_AFE_SOURCE]);
> > if (!tx)
> > return -ENOLINK;
> >  
> > -   width = 720;
> > -   height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
> > -   fps = afe->curr_norm & V4L2_STD_525_60 ? 30 : 25;
> > -
> > -   return adv748x_csi2_set_pixelrate(tx, width * height * fps);
> > +   /*
> > +* The ADV748x samples analog video signals using an externally supplied
> > +* clock whose frequency is required to be 28.63636 MHz.
> > +*/
> > +   return adv748x_csi2_set_pixelrate(tx, 28636360);

I believe this is wrong. The sampling rate of the AFE is 28.63636 MHz 
but the pixelrate output on the CSI-TXB might not be right? The adv7482 
is a complex and badly documented thing. But it's internal plumbing 
shows that the CVBS signal sampled by the AFE is passed to the SD core 
and then to the TXB CSI-2 transmitter.  

Reading the documentation we have such registers as LTA[1:0] which 
controls the delay between the chroma and luma samples. I do believe the 
adv748x is running with the default of AUTO_PDC_EN which indicates that 
the timings are automatically controlled by the adv748x hardware. And 
there are other registers in the SD core which to me looks like it can 
be configured to make use of the samples so that it won't correlate 1:1 
with the pixel rate.

Looking at the CSI-TXA and CSI-TXB cores which are particularly badly 
documented and the driver contains a lot of undocumented register 
writes. One that is documented and I find interesting in this context is 
EN_AUTOCALC_DPHY_PARAMS which is set 'yes' for the adv748x driver. This 
leads me to believe that the pixel rate output on the CSI-2 bus is not 
correlated with the AFE sampling clock. There are lots of holes in the 
documentation here but some stand out, the undocumented hole around 0xda 
which contains the few documented bits in that area, MIPI_PLL_LOCK_FLAG, 
MIPI_PLL_CLK_DET and MIPI_PLL_EN. Maybe the true link_freq or pixel_rate 
could be read from a undocumented register in that darkness :-)

The real reason I started to dig into this is that after you corrected 
my assumption about how to setup the R-Car CSI-2 receiver link freq this 
change breaks capture of CVBS for me. Looking at how I on your 
suggestion calculate link speed.

link_freq = (pixel_rate * bits_per_sample) / (2 * nr_of_lanes)

pixel_rate = 28636360 (from the adv748x V4L2_CID_PIXEL_RATE)
bits_per_sample = 16 (adv748x reports MEDIA_BUS_FMT_UYVY8_2X8)
nr_of_lanes = 1 (Using TXB of the adv748x)

Gives a link_freq of 229Mhz or as the R-Car CSI-2 deals with in Mbps 
~460Mbps as CSI-2 is DDR. If I try to capture CVBS using such a high 
link speed capture fail. I tried using the lowest link speed 

Re: [bug report] media: rcar-vin: add group allocator functions

2018-05-03 Thread Niklas Söderlund
Hi Dan,

Thanks for your report. A proposed fix to this is available [1], 
awaiting inclusion in the media-tree :-)

1. https://patchwork.linuxtv.org/patch/49025/

On 2018-05-03 15:36:30 +0300, Dan Carpenter wrote:
> Hello Niklas Söderlund,
> 
> The patch 3bb4c3bc85bf: "media: rcar-vin: add group allocator
> functions" from Apr 14, 2018, leads to the following static checker
> warning:
> 
>   drivers/media/platform/rcar-vin/rcar-core.c:346 rvin_group_put()
>   error: potential NULL dereference 'vin->group'.
> 
> drivers/media/platform/rcar-vin/rcar-core.c
>339  static void rvin_group_put(struct rvin_dev *vin)
>340  {
>341  mutex_lock(>group->lock);
>342  
>343  vin->group = NULL;
> ^
> Set to NULL.
> 
>344  vin->v4l2_dev.mdev = NULL;
>345  
>346  if (WARN_ON(vin->group->vin[vin->id] != vin))
> 
>347  goto out;
>348  
>349  vin->group->vin[vin->id] = NULL;
> 
>350  out:
>351  mutex_unlock(>group->lock);
>   
>352  
>353  kref_put(>group->refcount, rvin_group_release);
>       
> 
> There are a bunch of NULL dereferences here...
> 
>354  }
> 
> regards,
> dan carpenter

-- 
Regards,
Niklas Söderlund


Re: [PATCH v14 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-28 Thread Niklas Söderlund
Hi Jacopo,

Thanks for your feedback.

On 2018-04-28 13:28:27 +0200, Jacopo Mondi wrote:
> Hi Niklas,
>apart from a small comment, as my comments on v13 have been
>clarified
> 
> Reviewed-by: Jacopo Mondi <jac...@jmondi.org>

Thanks!

[snip]

> > diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
> > b/drivers/media/platform/rcar-vin/rcar-csi2.c
> > new file mode 100644
> > index ..49b29d5680f9d80b
> > --- /dev/null
> > +++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
> > @@ -0,0 +1,883 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Driver for Renesas R-Car MIPI CSI-2 Receiver
> > + *
> > + * Copyright (C) 2018 Renesas Electronics Corp.
> > + */

[snip]

> > +MODULE_AUTHOR("Niklas Söderlund <niklas.soderl...@ragnatech.se>");
> > +MODULE_DESCRIPTION("Renesas R-Car MIPI CSI-2 receiver");
> > +MODULE_LICENSE("GPL");
> 
> This doesn't match the SPDX header that reports GPL-2.0

I'm now officially more confused then normal :-) I really tried to get 
this right and the combination I use here

// SPDX-License-Identifier: GPL-2.0
MODULE_LICENSE("GPL");

Seems to be used all over the kernel, did some digging on the master 
branch of the media tree from a few days ago and found 265 files with 
this combination using this script:

count=0
for f in $(git grep -l "SPDX-License-Identifier: GPL-2.0$"); do
if grep -q 'MODULE_LICENSE("GPL")' $f; then
echo $f
grep SPDX-License-Identifier $f
grep MODULE_LICENSE $f;
count=$(($count + 1))
fi
done
echo "Count: $count"

I'm happy to post a new version of this series to make this right but 
I'm afraid that I at this point know what right is. My intention is to 
replace a licence text found in an old Renesas BSP which this work is 
loosely based on:

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.

So it's quiet clear it's GPL-2.0 and not GPL-2.0+ and AFIK what I have done 
here is correct, please tell me why I'm wrong and how I can correct it :-)

-- 
Regards,
Niklas Söderlund


Re: [PATCH] v4l: vsp1: Fix vsp1_regs.h license header

2018-04-28 Thread Niklas Söderlund
Hi Laurent,

Thanks for your patch.

On 2018-04-28 00:46:47 +0300, Laurent Pinchart wrote:
> All source files of the vsp1 driver are licensed under the GPLv2+ except
> for vsp1_regs.h which is licensed under GPLv2. This is caused by a bad
> copy that dates back from the initial version of the driver. Fix
> it.
> 
> Cc: Nobuhiro Iwamatsu <nobuhiro.iwamatsu...@renesas.com>
> Cc: Kieran Bingham <kieran.bingham+rene...@ideasonboard.com>
> Cc: Sergei Shtylyov <sergei.shtyl...@cogentembedded.com>
> Cc: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> Cc: Wolfram Sang <wsa+rene...@sang-engineering.com>
> Signed-off-by: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>

Acked-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

-- 
Regards,
Niklas Söderlund


Re: [PATCH v14 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-26 Thread Niklas Söderlund
Hi Laurent,

Thanks for your feedback.

On 2018-04-27 00:30:25 +0300, Laurent Pinchart wrote:
> Hi Niklas,
> 
> Thank you for the patch.
> 
> On Thursday, 26 April 2018 23:21:21 EEST Niklas Söderlund wrote:
> > A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
> > supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
> > connected between the video sources and the video grabbers (VIN).
> > 
> > Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.
> > 
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
> > Reviewed-by: Maxime Ripard <maxime.rip...@bootlin.com>
> > Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
> > 
> > ---
> > 
> > * Changes since v13
> > - Change return rcar_csi2_formats + i to return _csi2_formats[i].
> > - Add define for PHCLM_STOPSTATECKL.
> > - Update spelling in comments.
> > - Update calculation in rcar_csi2_calc_phypll() according to
> >   https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
> >   before v14 did not take into account that 2 bits per sample is
> >   transmitted.
> 
> Just one small comment about this, please see below.
> 
> > - Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
> >   statement to set correct number of lanes to enable.
> > - Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
> >   style of rest of file.
> > - Switch to %u instead of 0x%x when printing bus type.
> > - Switch to %u instead of %d for priv->lanes which is unsigned.
> > - Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
> >   rcar_csi2_formats[].
> > - Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
> > - Set INTSTATE after PL-11 is confirmed to match flow chart in
> >   datasheet.
> > - Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
> > - Add Maxime's and laurent's tags.
> > ---
> >  drivers/media/platform/rcar-vin/Kconfig |  12 +
> >  drivers/media/platform/rcar-vin/Makefile|   1 +
> >  drivers/media/platform/rcar-vin/rcar-csi2.c | 883 
> >  3 files changed, 896 insertions(+)
> >  create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c
> 
> [snip]
> 
> 
> > diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c
> > b/drivers/media/platform/rcar-vin/rcar-csi2.c new file mode 100644
> > index ..49b29d5680f9d80b
> > --- /dev/null
> > +++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
> 
> [snip]
> 
> > +static int rcar_csi2_calc_phypll(struct rcar_csi2 *priv, unsigned int bpp,
> > +u32 *phypll)
> > +{
> > +   const struct phypll_hsfreqrange *hsfreq;
> > +   struct v4l2_subdev *source;
> > +   struct v4l2_ctrl *ctrl;
> > +   u64 mbps;
> > +
> > +   if (!priv->remote)
> > +   return -ENODEV;
> > +
> > +   source = priv->remote;
> > +
> > +   /* Read the pixel rate control from remote */
> > +   ctrl = v4l2_ctrl_find(source->ctrl_handler, V4L2_CID_PIXEL_RATE);
> > +   if (!ctrl) {
> > +   dev_err(priv->dev, "no pixel rate control in subdev %s\n",
> > +   source->name);
> > +   return -EINVAL;
> > +   }
> > +
> > +   /*
> > +* Calculate the phypll in mbps (from v4l2 documentation)
> > +* link_freq = (pixel_rate * bits_per_sample) / (2 * nr_of_lanes)
> > +*/
> > +   mbps = v4l2_ctrl_g_ctrl_int64(ctrl) * bpp;
> > +   do_div(mbps, priv->lanes * 200);
> 
> pixel rate * bits per sample will give you the overall bit rate, which you 
> then divide by the number of lanes to get the bitrate per lane, and then by 2 
> as D-PHY is a DDR PHY and transmits 2 bits per clock cycle. You then end up 
> with the link frequency, which is thus expressed in MHz, not in Mbps. I would 
> thus name the mbps variable freq, and rename the phypll_hsfreqrange mbps 
> field 
> to freq (maybe with a small comment right after the field to tell the value 
> is 
> expressed in MHz).

I agree that freq would be a better name for what it represents. Never 
the less I prefer to stick with mbps as that is whats used in the 
datasheet. See Table '25.9 HSFREQRANGE Bit Set Values'.

With this in mind if you still feel strongly about renaming it I could 
do so. But at lest for me it feels more useful to easily be able to map 
the variable to the datasheet tables :-)

> 
> > +   for (hsfreq = priv->info->hsfreqrange; hsfreq->mbps != 0; hsfreq++)
> > +   if (hsfreq->mbps >= mbps)
> > +   break;
> > +
> > +   if (!hsfreq->mbps) {
> > +   dev_err(priv->dev, "Unsupported PHY speed (%llu Mbps)", mbps);
> > +   return -ERANGE;
> > +   }
> > +
> > +   dev_dbg(priv->dev, "PHY HSFREQRANGE requested %llu got %u Mbps\n", mbps,
> > +   hsfreq->mbps);
> > +
> > +   *phypll = PHYPLL_HSFREQRANGE(hsfreq->reg);
> > +
> > +   return 0;
> > +}
> 
> [snip]
> 
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> 
> 

-- 
Regards,
Niklas Söderlund


[PATCH v14 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-26 Thread Niklas Söderlund
A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
connected between the video sources and the video grabbers (VIN).

Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
Reviewed-by: Maxime Ripard <maxime.rip...@bootlin.com>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>

---

* Changes since v13
- Change return rcar_csi2_formats + i to return _csi2_formats[i].
- Add define for PHCLM_STOPSTATECKL.
- Update spelling in comments.
- Update calculation in rcar_csi2_calc_phypll() according to
  https://linuxtv.org/downloads/v4l-dvb-apis/kapi/csi2.html. The one
  before v14 did not take into account that 2 bits per sample is
  transmitted.
- Use Geert's suggestion of (1 << priv->lanes) - 1 instead of switch
  statement to set correct number of lanes to enable.
- Change hex constants in hsfreqrange_m3w_h3es1[] to lower case to match
  style of rest of file.
- Switch to %u instead of 0x%x when printing bus type.
- Switch to %u instead of %d for priv->lanes which is unsigned.
- Add MEDIA_BUS_FMT_YUYV8_1X16 to the list of supported formats in
  rcar_csi2_formats[].
- Fixed bps for MEDIA_BUS_FMT_YUYV10_2X10 to 20 and not 16.
- Set INTSTATE after PL-11 is confirmed to match flow chart in
  datasheet.
- Change priv->notifier.subdevs == NULL to !priv->notifier.subdevs.
- Add Maxime's and laurent's tags.
---
 drivers/media/platform/rcar-vin/Kconfig |  12 +
 drivers/media/platform/rcar-vin/Makefile|   1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c | 883 
 3 files changed, 896 insertions(+)
 create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c

diff --git a/drivers/media/platform/rcar-vin/Kconfig 
b/drivers/media/platform/rcar-vin/Kconfig
index 8fa7ee468c63afb9..3dfeb91f8f186528 100644
--- a/drivers/media/platform/rcar-vin/Kconfig
+++ b/drivers/media/platform/rcar-vin/Kconfig
@@ -1,3 +1,15 @@
+config VIDEO_RCAR_CSI2
+   tristate "R-Car MIPI CSI-2 Receiver"
+   depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF
+   depends on ARCH_RENESAS || COMPILE_TEST
+   select V4L2_FWNODE
+   ---help---
+ Support for Renesas R-Car MIPI CSI-2 receiver.
+ Supports R-Car Gen3 SoCs.
+
+ To compile this driver as a module, choose M here: the
+ module will be called rcar-csi2.
+
 config VIDEO_RCAR_VIN
tristate "R-Car Video Input (VIN) Driver"
depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF && HAS_DMA && 
MEDIA_CONTROLLER
diff --git a/drivers/media/platform/rcar-vin/Makefile 
b/drivers/media/platform/rcar-vin/Makefile
index 48c5632c21dc060b..5ab803d3e7c1aa57 100644
--- a/drivers/media/platform/rcar-vin/Makefile
+++ b/drivers/media/platform/rcar-vin/Makefile
@@ -1,3 +1,4 @@
 rcar-vin-objs = rcar-core.o rcar-dma.o rcar-v4l2.o
 
+obj-$(CONFIG_VIDEO_RCAR_CSI2) += rcar-csi2.o
 obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin.o
diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c 
b/drivers/media/platform/rcar-vin/rcar-csi2.c
new file mode 100644
index ..49b29d5680f9d80b
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Renesas R-Car MIPI CSI-2 Receiver
+ *
+ * Copyright (C) 2018 Renesas Electronics Corp.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Register offsets and bits */
+
+/* Control Timing Select */
+#define TREF_REG   0x00
+#define TREF_TREF  BIT(0)
+
+/* Software Reset */
+#define SRST_REG   0x04
+#define SRST_SRST  BIT(0)
+
+/* PHY Operation Control */
+#define PHYCNT_REG 0x08
+#define PHYCNT_SHUTDOWNZ   BIT(17)
+#define PHYCNT_RSTZBIT(16)
+#define PHYCNT_ENABLECLK   BIT(4)
+#define PHYCNT_ENABLE_3BIT(3)
+#define PHYCNT_ENABLE_2BIT(2)
+#define PHYCNT_ENABLE_1BIT(1)
+#define PHYCNT_ENABLE_0BIT(0)
+
+/* Checksum Control */
+#define CHKSUM_REG 0x0c
+#define CHKSUM_ECC_EN  BIT(1)
+#define CHKSUM_CRC_EN  BIT(0)
+
+/*
+ * Channel Data Type Select
+ * VCDT[0-15]:  Channel 1 VCDT[16-31]:  Channel 2
+ * VCDT2[0-15]: Channel 3 VCDT2[16-31]: Channel 4
+ */
+#define VCDT_REG   0x10
+#define VCDT2_REG  0x14
+#define VCDT_VCDTN_EN  BIT(15)
+#define VCDT_SEL_VC(n) (((n) & 0x3) << 8)
+#define VC

[PATCH v14 0/2] rcar-csi2: add Renesas R-Car MIPI CSI-2

2018-04-26 Thread Niklas Söderlund
 table for supported format information instead of scattering
  this information around the driver, thanks Sakari!
- Small newline fixes and reduce some indentation levels

Changes since v5:
- Make use of the incremental async subnotifer and helper to map DT
  endpoint to media pad number. This moves functionality which
  previously in the Gen3 patches for R-Car VIN driver to this R-Car
  CSI-2 driver. This is done in preparation to support the ADV7482
  driver in development by Kieran which will register more then one
  subdevice and the CSI-2 driver needs to cope wit this. Further more it
  prepares the driver for another use-case where more then one subdevice
  is present upstream for the CSI-2.
- Small cleanups.
- Add explicit include for linux/io.h, thanks Kieran.

Changes since v4:
- Match SoC part numbers and drop trailing space in documentation,
  thanks Geert for pointing this out.
- Clarify that the driver is a CSI-2 receiver by supervised
  s/interface/receiver/, thanks Laurent.
- Add entries in Kconfig and Makefile alphabetically instead of append.
- Rename struct rcar_csi2 member swap to lane_swap.
- Remove macros to wrap calls to dev_{dbg,info,warn,err}.
- Add wrappers for ioread32 and iowrite32.
- Remove unused interrupt handler, but keep checking in probe that there
  are a interrupt define in DT.
- Rework how to wait for LP-11 state, thanks Laurent for the great idea!
- Remove unneeded delay in rcar_csi2_reset()
- Remove check for duplicated lane id:s from DT parsing. Broken out to a
  separate patch adding this check directly to v4l2_of_parse_endpoint().
- Fixed rcar_csi2_start() to ask it's source subdevice for information
  about pixel rate and frame format. With this change having
  {set,get}_fmt operations became redundant, it was only used for
  figuring out this out so dropped them.
- Tabulated frequency settings map.
- Dropped V4L2_SUBDEV_FL_HAS_DEVNODE it should never have been set.
- Switched from MEDIA_ENT_F_ATV_DECODER to
  MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER as entity function. I can't
  find a more suitable function, and what the hardware do is to fetch
  video from an external chip and passes it on to a another SoC internal
  IP it's sort of a formatter.
- Break out DT documentation and code in two patches.

Changes since v3:
- Update DT binding documentation with input from Geert Uytterhoeven,
  thanks

Changes since v2:
- Added media control pads as this is needed by the new rcar-vin driver.
- Update DT bindings after review comments and to add r8a7796 support.
- Add get_fmt handler.
- Fix media bus format error s/YUYV8/UYVY8/

Changes since v1:
- Drop dependency on a pad aware s_stream operation.
- Use the DT bindings format "renesas,-", thanks Geert
  for pointing this out.

Niklas Söderlund (2):
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation
  rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

 .../bindings/media/renesas,rcar-csi2.txt  |  99 ++
 MAINTAINERS   |   1 +
 drivers/media/platform/rcar-vin/Kconfig   |  12 +
 drivers/media/platform/rcar-vin/Makefile  |   1 +
 drivers/media/platform/rcar-vin/rcar-csi2.c   | 883 ++
 5 files changed, 996 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
 create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c

-- 
2.17.0



[PATCH v14 1/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver documentation

2018-04-26 Thread Niklas Söderlund
Documentation for Renesas R-Car MIPI CSI-2 receiver. The CSI-2 receivers
are located between the video sources (CSI-2 transmitters) and the video
grabbers (VIN) on Gen3 of Renesas R-Car SoC.

Each CSI-2 device is connected to more than one VIN device which
simultaneously can receive video from the same CSI-2 device. Each VIN
device can also be connected to more than one CSI-2 device. The routing
of which links are used is controlled by the VIN devices. There are only
a few possible routes which are set by hardware limitations, which are
different for each SoC in the Gen3 family.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Acked-by: Rob Herring <r...@kernel.org>
Acked-by: Sakari Ailus <sakari.ai...@linux.intel.com>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>

---

* Changes since v13
- Add Laurent's tag.
---
 .../bindings/media/renesas,rcar-csi2.txt  | 99 +++
 MAINTAINERS   |  1 +
 2 files changed, 100 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt

diff --git a/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt 
b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
new file mode 100644
index ..6f71f997dc48eee9
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/renesas,rcar-csi2.txt
@@ -0,0 +1,99 @@
+Renesas R-Car MIPI CSI-2
+
+
+The R-Car CSI-2 receiver device provides MIPI CSI-2 capabilities for the
+Renesas R-Car family of devices. It is used in conjunction with the
+R-Car VIN module, which provides the video capture capabilities.
+
+Mandatory properties
+
+ - compatible: Must be one or more of the following
+   - "renesas,r8a7795-csi2" for the R8A7795 device.
+   - "renesas,r8a7796-csi2" for the R8A7796 device.
+
+ - reg: the register base and size for the device registers
+ - interrupts: the interrupt for the device
+ - clocks: reference to the parent clock
+
+The device node shall contain two 'port' child nodes according to the
+bindings defined in Documentation/devicetree/bindings/media/
+video-interfaces.txt. Port 0 shall connect to the CSI-2 source. Port 1
+shall connect to all the R-Car VIN modules that have a hardware
+connection to the CSI-2 receiver.
+
+- Port 0 - Video source (mandatory)
+   - Endpoint 0 - sub-node describing the endpoint that is the video source
+
+- Port 1 - VIN instances (optional)
+   - One endpoint sub-node for every R-Car VIN instance which is connected
+ to the R-Car CSI-2 receiver.
+
+Example:
+
+   csi20: csi2@fea8 {
+   compatible = "renesas,r8a7796-csi2";
+   reg = <0 0xfea8 0 0x1>;
+   interrupts = <0 184 IRQ_TYPE_LEVEL_HIGH>;
+   clocks = < CPG_MOD 714>;
+   power-domains = < R8A7796_PD_ALWAYS_ON>;
+   resets = < 714>;
+
+   ports {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   port@0 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <0>;
+
+   csi20_in: endpoint@0 {
+   reg = <0>;
+   clock-lanes = <0>;
+   data-lanes = <1>;
+   remote-endpoint = <_txb>;
+   };
+   };
+
+   port@1 {
+   #address-cells = <1>;
+   #size-cells = <0>;
+
+   reg = <1>;
+
+   csi20vin0: endpoint@0 {
+   reg = <0>;
+   remote-endpoint = <>;
+   };
+   csi20vin1: endpoint@1 {
+   reg = <1>;
+   remote-endpoint = <>;
+   };
+   csi20vin2: endpoint@2 {
+   reg = <2>;
+   remote-endpoint = <>;
+   };
+   csi20vin3: endpoint@3 {
+   reg = <3>;
+   remote-endpoint = <>;
+   };
+   csi20vin4: endpoint@4 {
+   reg = <4>;
+   remote-en

Re: [PATCH] rcar-vin: fix null pointer dereference in rvin_group_get()

2018-04-26 Thread Niklas Söderlund
Hi Geert,

Thanks for your feedback.

On 2018-04-25 09:25:56 +0200, Geert Uytterhoeven wrote:
> On Wed, Apr 25, 2018 at 1:45 AM, Niklas Söderlund
> <niklas.soderlund+rene...@ragnatech.se> wrote:
> > Store the group pointer before disassociating the VIN from the group.
> 
> s/get/put/ in one-line summary?

Yes, silly copy paste error, must have copied function name from the @@ 
context line and not from the diff itself. Thanks for noticing.

Will send a v2 after I have checked with Simon that the is happy with 
the change itself.

> 
> > Fixes: 3bb4c3bc85bf77a7 ("media: rcar-vin: add group allocator functions")
> > Reported-by: Colin Ian King <colin.k...@canonical.com>
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > ---
> >  drivers/media/platform/rcar-vin/rcar-core.c | 12 +++-
> >  1 file changed, 7 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> > b/drivers/media/platform/rcar-vin/rcar-core.c
> > index 7bc2774a11232362..d3072e166a1ca24f 100644
> > --- a/drivers/media/platform/rcar-vin/rcar-core.c
> > +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> > @@ -338,19 +338,21 @@ static int rvin_group_get(struct rvin_dev *vin)
> >
> >  static void rvin_group_put(struct rvin_dev *vin)
> >  {
> > -   mutex_lock(>group->lock);
> > +   struct rvin_group *group = vin->group;
> 
> Gr{oetje,eeting}s,
> 
> Geert
> 
> -- 
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- 
> ge...@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like 
> that.
> -- Linus Torvalds

-- 
Regards,
Niklas Söderlund


Re: [PATCH] rcar-vin: fix null pointer dereference in rvin_group_get()

2018-04-26 Thread Niklas Söderlund
Hi Simon,

Thanks for your feedback.

On 2018-04-25 09:18:51 +0200, Simon Horman wrote:
> On Wed, Apr 25, 2018 at 01:45:06AM +0200, Niklas Söderlund wrote:
> > Store the group pointer before disassociating the VIN from the group.
> > 
> > Fixes: 3bb4c3bc85bf77a7 ("media: rcar-vin: add group allocator functions")
> > Reported-by: Colin Ian King <colin.k...@canonical.com>
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > ---
> >  drivers/media/platform/rcar-vin/rcar-core.c | 12 +++-
> >  1 file changed, 7 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> > b/drivers/media/platform/rcar-vin/rcar-core.c
> > index 7bc2774a11232362..d3072e166a1ca24f 100644
> > --- a/drivers/media/platform/rcar-vin/rcar-core.c
> > +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> > @@ -338,19 +338,21 @@ static int rvin_group_get(struct rvin_dev *vin)
> >  
> >  static void rvin_group_put(struct rvin_dev *vin)
> >  {
> > -   mutex_lock(>group->lock);
> > +   struct rvin_group *group = vin->group;
> > +
> > +   mutex_lock(>lock);
> 
> Hi Niklas, its not clear to me why moving the lock is safe.
> Could you explain the locking scheme a little?

The lock here protects the members of the group struct and not any of 
the members of the vin struct. The intent of the rvin_group_put() 
function is:

1. Disassociate the vin struct from the group struct. This is done by 
   removing the pointer to the vin from the group->vin array and 
   removing the pointer from vin->group to the group struct. Here the 
   lock is needed to protect access to the group->vin array.

2. Decrease the refcount of the struct group and if we are the last one 
   out release the group.

The problem with the original code is that I first disassociate group 
from the vin 'vin->group = NULL' but still use the pointer stored in the 
vin struct when I try to disassociate the vin from the group 
'vin->group->vin[vin->id]'.

AFIK can tell the locking here is fine, the problem was that I pulled 
the rug from under my own feet in how I access the lock in order to not 
having to declare a variable to store the pointer in ;-)

Do this explanation help put you at ease?

> 
> >  
> > vin->group = NULL;
> > vin->v4l2_dev.mdev = NULL;
> >  
> > -   if (WARN_ON(vin->group->vin[vin->id] != vin))
> > +   if (WARN_ON(group->vin[vin->id] != vin))
> > goto out;
> >  
> > -   vin->group->vin[vin->id] = NULL;
> > +   group->vin[vin->id] = NULL;
> >  out:
> > -   mutex_unlock(>group->lock);
> > +   mutex_unlock(>lock);
> >  
> > -   kref_put(>group->refcount, rvin_group_release);
> > +   kref_put(>refcount, rvin_group_release);
> >  }
> >  
> >  /* 
> > -
> > -- 
> > 2.17.0
> > 

-- 
Regards,
Niklas Söderlund


[PATCH] rcar-vin: enable field toggle after a set number of lines for Gen3

2018-04-24 Thread Niklas Söderlund
The VIN Gen3 hardware don't have Line Post-Clip capabilities as VIN Gen2
hardware have. To protect against writing outside the capture window
enable field toggle after a set number of lines have been captured.

Capturing outside the allocated capture buffer where observed on R-Car
Gen3 Salvator-XS H3 from the CVBS input if the standard is
misconfigured. That is if a PAL source is connected to the system but
the adv748x standard is set to NTSC. In this case the format reported by
the adv748x is 720x480 and that is what is used for the media pipeline.
The PAL source generates frames in the format of 720x576 and the field
is not toggled until the VSYNC is detected and at that time data have
already been written outside the allocated capture buffer.

With this change the capture in the situation described above results in
garbage frames but that is far better then writing outside the capture
buffer.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 20 +++-
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index ac07f99e3516a620..b41ba9a4a2b3ac90 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -124,7 +124,9 @@
 #define VNDMR2_VPS (1 << 30)
 #define VNDMR2_HPS (1 << 29)
 #define VNDMR2_FTEV(1 << 17)
+#define VNDMR2_FTEH(1 << 16)
 #define VNDMR2_VLV(n)  ((n & 0xf) << 12)
+#define VNDMR2_HLV(n)  ((n) & 0xfff)
 
 /* Video n CSI2 Interface Mode Register (Gen3) */
 #define VNCSI_IFMD_DES1(1 << 26)
@@ -612,8 +614,9 @@ void rvin_crop_scale_comp(struct rvin_dev *vin)
 
 static int rvin_setup(struct rvin_dev *vin)
 {
-   u32 vnmc, dmr, dmr2, interrupts;
+   u32 vnmc, dmr, dmr2, interrupts, lines;
bool progressive = false, output_is_yuv = false, input_is_yuv = false;
+   bool halfsize = false;
 
switch (vin->format.field) {
case V4L2_FIELD_TOP:
@@ -628,12 +631,15 @@ static int rvin_setup(struct rvin_dev *vin)
/* Use BT if video standard can be read and is 60 Hz format */
if (!vin->info->use_mc && vin->std & V4L2_STD_525_60)
vnmc = VNMC_IM_FULL | VNMC_FOC;
+   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_TB:
vnmc = VNMC_IM_FULL;
+   halfsize = true;
break;
case V4L2_FIELD_INTERLACED_BT:
vnmc = VNMC_IM_FULL | VNMC_FOC;
+   halfsize = true;
break;
case V4L2_FIELD_NONE:
vnmc = VNMC_IM_ODD_EVEN;
@@ -676,11 +682,15 @@ static int rvin_setup(struct rvin_dev *vin)
break;
}
 
-   /* Enable VSYNC Field Toogle mode after one VSYNC input */
-   if (vin->info->model == RCAR_GEN3)
-   dmr2 = VNDMR2_FTEV;
-   else
+   if (vin->info->model == RCAR_GEN3) {
+   /* Enable HSYNC Field Toggle mode after height HSYNC inputs. */
+   lines = vin->format.height / (halfsize ? 2 : 1);
+   dmr2 = VNDMR2_FTEH | VNDMR2_HLV(lines);
+   vin_dbg(vin, "Field Toogle after %u lines\n", lines);
+   } else {
+   /* Enable VSYNC Field Toogle mode after one VSYNC input. */
dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
+   }
 
/* Hsync Signal Polarity Select */
if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
-- 
2.17.0



[PATCH] rcar-vin: add support for MEDIA_BUS_FMT_UYVY8_1X16

2018-04-24 Thread Niklas Söderlund
By setting VNMC_YCAL rcar-vin can support input video in
MEDIA_BUS_FMT_UYVY8_1X16 format.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 1 +
 drivers/media/platform/rcar-vin/rcar-dma.c  | 5 +
 2 files changed, 6 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 55b745ac86a5884d..7bc2774a11232362 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -404,6 +404,7 @@ static int rvin_digital_subdevice_attach(struct rvin_dev 
*vin,
code.index++;
switch (code.code) {
case MEDIA_BUS_FMT_YUYV8_1X16:
+   case MEDIA_BUS_FMT_UYVY8_1X16:
case MEDIA_BUS_FMT_UYVY8_2X8:
case MEDIA_BUS_FMT_UYVY10_2X10:
case MEDIA_BUS_FMT_RGB888_1X24:
diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index 4a3a195e7f59047c..ac07f99e3516a620 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -653,6 +653,10 @@ static int rvin_setup(struct rvin_dev *vin)
vnmc |= VNMC_INF_YUV16;
input_is_yuv = true;
break;
+   case MEDIA_BUS_FMT_UYVY8_1X16:
+   vnmc |= VNMC_INF_YUV16 | VNMC_YCAL;
+   input_is_yuv = true;
+   break;
case MEDIA_BUS_FMT_UYVY8_2X8:
/* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
vnmc |= vin->mbus_cfg.type == V4L2_MBUS_BT656 ?
@@ -1009,6 +1013,7 @@ static int rvin_mc_validate_format(struct rvin_dev *vin, 
struct v4l2_subdev *sd,
 
switch (fmt.format.code) {
case MEDIA_BUS_FMT_YUYV8_1X16:
+   case MEDIA_BUS_FMT_UYVY8_1X16:
case MEDIA_BUS_FMT_UYVY8_2X8:
case MEDIA_BUS_FMT_UYVY10_2X10:
case MEDIA_BUS_FMT_RGB888_1X24:
-- 
2.17.0



[PATCH] rcar-vin: fix null pointer dereference in rvin_group_get()

2018-04-24 Thread Niklas Söderlund
Store the group pointer before disassociating the VIN from the group.

Fixes: 3bb4c3bc85bf77a7 ("media: rcar-vin: add group allocator functions")
Reported-by: Colin Ian King <colin.k...@canonical.com>
Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 7bc2774a11232362..d3072e166a1ca24f 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -338,19 +338,21 @@ static int rvin_group_get(struct rvin_dev *vin)
 
 static void rvin_group_put(struct rvin_dev *vin)
 {
-   mutex_lock(>group->lock);
+   struct rvin_group *group = vin->group;
+
+   mutex_lock(>lock);
 
vin->group = NULL;
vin->v4l2_dev.mdev = NULL;
 
-   if (WARN_ON(vin->group->vin[vin->id] != vin))
+   if (WARN_ON(group->vin[vin->id] != vin))
goto out;
 
-   vin->group->vin[vin->id] = NULL;
+   group->vin[vin->id] = NULL;
 out:
-   mutex_unlock(>group->lock);
+   mutex_unlock(>lock);
 
-   kref_put(>group->refcount, rvin_group_release);
+   kref_put(>refcount, rvin_group_release);
 }
 
 /* 
-
-- 
2.17.0



[PATCH] rcar-vin: remove generic gen3 compatible string

2018-04-24 Thread Niklas Söderlund
The compatible string "renesas,rcar-gen3-vin" was added before the
Gen3 driver code was added but it's not possible to use. Each SoC in the
Gen3 series require SoC specific knowledge in the driver to function.
Remove it before it is added to any device tree descriptions.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
---
 Documentation/devicetree/bindings/media/rcar_vin.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/media/rcar_vin.txt 
b/Documentation/devicetree/bindings/media/rcar_vin.txt
index ba31431d4b1fbdbb..a19517e1c669eb35 100644
--- a/Documentation/devicetree/bindings/media/rcar_vin.txt
+++ b/Documentation/devicetree/bindings/media/rcar_vin.txt
@@ -24,7 +24,6 @@ on Gen3 platforms to a CSI-2 receiver.
- "renesas,vin-r8a77970" for the R8A77970 device
- "renesas,rcar-gen2-vin" for a generic R-Car Gen2 or RZ/G1 compatible
  device.
-   - "renesas,rcar-gen3-vin" for a generic R-Car Gen3 compatible device.
 
When compatible with the generic version nodes must list the
SoC-specific version corresponding to the platform first
-- 
2.17.0



Re: [PATCH] media: i2c: adv748x: Fix pixel rate values

2018-04-24 Thread Niklas Söderlund
Hi Laurent,

Thanks for your patch.

On 2018-04-21 15:44:44 +0300, Laurent Pinchart wrote:
> The pixel rate, as reported by the V4L2_CID_PIXEL_RATE control, must
> include both horizontal and vertical blanking. Both the AFE and HDMI
> receiver program it incorrectly:
> 
> - The HDMI receiver goes to the trouble of removing blanking to compute
> the rate of active pixels. This is easy to fix by removing the
> computation and returning the incoming pixel clock rate directly.
> 
> - The AFE performs similar calculation, while it should simply return
> the fixed pixel rate for analog sources, mandated by the ADV748x to be
> 28.63636 MHz.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart+rene...@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>

This patch uncovered a calculation error in rcar-csi2 which compensated 
for the removing of the blanking in the adv748x, thanks for that! Good 
thing that it's not merged yet, will include the fix in the next version 
of the CSI-2 driver.

> ---
>  drivers/media/i2c/adv748x/adv748x-afe.c  | 11 +--
>  drivers/media/i2c/adv748x/adv748x-hdmi.c |  8 +---
>  2 files changed, 6 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/media/i2c/adv748x/adv748x-afe.c 
> b/drivers/media/i2c/adv748x/adv748x-afe.c
> index 61514bae7e5c..3e18d5ae813b 100644
> --- a/drivers/media/i2c/adv748x/adv748x-afe.c
> +++ b/drivers/media/i2c/adv748x/adv748x-afe.c
> @@ -321,17 +321,16 @@ static const struct v4l2_subdev_video_ops 
> adv748x_afe_video_ops = {
>  static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
>  {
>   struct v4l2_subdev *tx;
> - unsigned int width, height, fps;
>  
>   tx = adv748x_get_remote_sd(>pads[ADV748X_AFE_SOURCE]);
>   if (!tx)
>   return -ENOLINK;
>  
> - width = 720;
> - height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
> - fps = afe->curr_norm & V4L2_STD_525_60 ? 30 : 25;
> -
> - return adv748x_csi2_set_pixelrate(tx, width * height * fps);
> + /*
> +  * The ADV748x samples analog video signals using an externally supplied
> +  * clock whose frequency is required to be 28.63636 MHz.
> +  */
> + return adv748x_csi2_set_pixelrate(tx, 28636360);
>  }
>  
>  static int adv748x_afe_enum_mbus_code(struct v4l2_subdev *sd,
> diff --git a/drivers/media/i2c/adv748x/adv748x-hdmi.c 
> b/drivers/media/i2c/adv748x/adv748x-hdmi.c
> index 10d229a4f088..aecc2a84dfec 100644
> --- a/drivers/media/i2c/adv748x/adv748x-hdmi.c
> +++ b/drivers/media/i2c/adv748x/adv748x-hdmi.c
> @@ -402,8 +402,6 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
> adv748x_hdmi *hdmi)
>  {
>   struct v4l2_subdev *tx;
>   struct v4l2_dv_timings timings;
> - struct v4l2_bt_timings *bt = 
> - unsigned int fps;
>  
>   tx = adv748x_get_remote_sd(>pads[ADV748X_HDMI_SOURCE]);
>   if (!tx)
> @@ -411,11 +409,7 @@ static int adv748x_hdmi_propagate_pixelrate(struct 
> adv748x_hdmi *hdmi)
>  
>   adv748x_hdmi_query_dv_timings(>sd, );
>  
> - fps = DIV_ROUND_CLOSEST_ULL(bt->pixelclock,
> - V4L2_DV_BT_FRAME_WIDTH(bt) *
> - V4L2_DV_BT_FRAME_HEIGHT(bt));
> -
> - return adv748x_csi2_set_pixelrate(tx, bt->width * bt->height * fps);
> + return adv748x_csi2_set_pixelrate(tx, timings.bt.pixelclock);
>  }
>  
>  static int adv748x_hdmi_enum_mbus_code(struct v4l2_subdev *sd,
> -- 
> Regards,
> 
> Laurent Pinchart
> 

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-16 Thread Niklas Söderlund
Hi Jacopo,

On 2018-04-16 01:16:35 +0200, Niklas Söderlund wrote:

[snip]

> > > +
> > > + /* Set frequency range if we have it */
> > > + if (priv->info->csi0clkfreqrange)
> > > + rcar_csi2_write(priv, CSI0CLKFCPR_REG,
> > > + CSI0CLKFREQRANGE(priv->info->csi0clkfreqrange));
> > > +
> > > + rcar_csi2_write(priv, PHYCNT_REG, phycnt);
> > > + rcar_csi2_write(priv, LINKCNT_REG, LINKCNT_MONITOR_EN |
> > > + LINKCNT_REG_MONI_PACT_EN | LINKCNT_ICLK_NONSTOP);
> > > + rcar_csi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ);
> > > + rcar_csi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ |
> > > + PHYCNT_RSTZ);
> > 
> > Nit: from tables 25.[17-20] it seems to me you do not have to re-issue
> > PHYCNT_SHUTDOWNZ when writing PHYCNT_RSTZ to PHYCNT_REG.
> 
> You are correct, I miss read ' Here, the ENABLE_0 to ENABLE_3 and
> ENABLECLK values set above should be retained' as all previous PHYCNT 
> bits should be retained not just the ones explicitly listed. I will give 
> this a test and if it still works I will remove it for the next version.

This change breaks capture and LP-11 is never detected. So I will 
continue to retain the PHYCNT_SHUTDOWNZ here.

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
> > +
> > +   return rcar_csi2_wait_phy_start(priv);
> > +}
> > +
> > +static void rcar_csi2_stop(struct rcar_csi2 *priv)
> > +{
> > +   rcar_csi2_write(priv, PHYCNT_REG, 0);
> > +
> > +   rcar_csi2_reset(priv);
> > +}
> > +
> > +static int rcar_csi2_s_stream(struct v4l2_subdev *sd, int enable)
> > +{
> > +   struct rcar_csi2 *priv = sd_to_csi2(sd);
> > +   struct v4l2_subdev *nextsd;
> > +   int ret = 0;
> > +
> > +   mutex_lock(>lock);
> > +
> > +   if (!priv->remote) {
> > +   ret = -ENODEV;
> > +   goto out;
> > +   }
> 
> Can this happen?

Sure, the video source is unbound and as you note bellow the unbind 
callback sets priv->remote = NULL. Or even simpler the driver for the 
video source is not loaded at all so the rcar-csi2 drivers bound 
callback is never called.

> 
> The 'bind' callback sets priv->remote and it gets assigned back to
> NULL only on 'unbind'. Wouldn't it be better to remove the link in the
> media graph and let the system return an EPIPE before calling this?

That would be very complex as some driver in the pipeline (VIN?) would 
have to parse the full graph in DT and somehow with subdevice support 
helpers to traverse the DT port to MC pad mapping using helpers that do 
not exist yet (and are hard to use if the remote subdevice driver is not 
loaded) to build a full view of the pipeline and return -EPIPE if not 
all entities are present in the media graph :-)

[snip]

> > +static int rcar_csi2_parse_v4l2(struct rcar_csi2 *priv,
> > +   struct v4l2_fwnode_endpoint *vep)
> > +{
> > +   unsigned int i;
> > +
> > +   /* Only port 0 endpoint 0 is valid */
> > +   if (vep->base.port || vep->base.id)
> > +   return -ENOTCONN;
> > +
> > +   if (vep->bus_type != V4L2_MBUS_CSI2) {
> > +   dev_err(priv->dev, "Unsupported bus: 0x%x\n", vep->bus_type);
> > +   return -EINVAL;
> > +   }
> > +
> > +   priv->lanes = vep->bus.mipi_csi2.num_data_lanes;
> > +   if (priv->lanes != 1 && priv->lanes != 2 && priv->lanes != 4) {
> 
> Is this an HW limitation? Like the 'data_lanes' comment, if it is,
> shouldn't you mention in bindings that the accepted lane numbers is
> limited to the [1,2,4] values.

Yes, see chapter 25.4.8 Lane Setting.

[snip]

> 
> No serious issues though. So when fixed/clarified feel free to append my
> Reviewed-by tag, if relevant at all.

Thanks! I feel I managed to address all of your review comments but 
would still like your confirmation for me to add your tag. Thanks again 
for the review!

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Laurent,

Thanks for your feedback.

I have addressed all your comment's but one for the next version. Please 
indicate if you are fine with this and I can still keep your review tag 
:-)

On 2018-04-04 18:15:16 +0300, Laurent Pinchart wrote:

[snip]

> > +static int rcar_csi2_start(struct rcar_csi2 *priv)
> > +{
> > +   const struct rcar_csi2_format *format;
> > +   u32 phycnt, phypll, vcdt = 0, vcdt2 = 0;
> > +   unsigned int i;
> > +   int ret;
> > +
> > +   dev_dbg(priv->dev, "Input size (%ux%u%c)\n",
> > +   priv->mf.width, priv->mf.height,
> > +   priv->mf.field == V4L2_FIELD_NONE ? 'p' : 'i');
> > +
> > +   /* Code is validated in set_fmt */
> > +   format = rcar_csi2_code_to_fmt(priv->mf.code);
> 
> You could store the format pointer iin the rcar_csi2 structure to avoid 
> looking it up here.

I could do that, but then I would duplicate the storage of the code 
between the two cached values priv->mf and priv->. I could find that acceptable but if you don't strongly 
disagree I would prefer to keep the current way of looking it up here 
:-)

[snip]

> > +static int rcar_csi2_probe_resources(struct rcar_csi2 *priv,
> > +struct platform_device *pdev)
> > +{
> > +   struct resource *res;
> > +   int irq;
> > +
> > +   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > +   priv->base = devm_ioremap_resource(>dev, res);
> > +   if (IS_ERR(priv->base))
> > +   return PTR_ERR(priv->base);
> > +
> > +   irq = platform_get_irq(pdev, 0);
> > +   if (irq < 0)
> > +   return irq;
> 
> You don't seem to use the IRQ. Is this meant to catch invalid DT that don't 
> specify an IRQ, to make sure we'll always have one available when we'll need 
> to later ?

Yes, as you deducted this is currently only to catch invalid DT. In the 
DT documentation I list it as a mandatory property. I think there might 
be potential use-case to at some point add interrupt support of for the 
some of the error interrupts which can be enabled, specially now when we 
seen similar patches for VIN floating around.

> > +
> > +   return 0;
> > +
> > +error:
> > +   v4l2_async_notifier_unregister(>notifier);
> > +   v4l2_async_notifier_cleanup(>notifier);
> > +
> > +   return ret;
> > +}
> 
> [snip]
> 
> With these small issues fixed and Kieran's and Maxime's comments addressed as 
> you see fit,
> 
> Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>

Thanks, I will hold of adding it until you indicate if you are OK with 
the one comment I'm not fully agreeing with you on.

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Sakari,

Thanks for your feedback.

On 2018-04-04 23:13:57 +0300, Sakari Ailus wrote:

[snip]

> > > + pm_runtime_enable(>dev);
> > 
> > Is CONFIG_PM mandatory on Renesas SoCs? If not, you end up with the
> > device uninitialised at probe, and pm_runtime_get_sync will not
> > initialise it either if CONFIG_PM is not enabled. I guess you could
> > call your runtime_resume function unconditionally, and mark the device
> > as active in runtime_pm using pm_runtime_set_active.
> 
> There doesn't seem to be any runtime_resume function. Was there supposed
> to be one?

No there is not suppose to be one.

> 
> Assuming runtime PM would actually do something here, you might add
> pm_runtime_idle() to power the device off after probing.
> 
> I guess pm_runtime_set_active() should precede pm_runtime_enable().

The CSI-2 is in the always on power domain so the calls to 
pm_runtime_get_sync() and pm_runtime_put() are there in the s_stream() 
callback to enable and disable the module clock. I'm no expert on PM but 
in my testing the pm_ calls in this driver seems to be correct.

1. In probe I call pm_runtime_enable(). And rudimentary tests shows the 
   clock is off (but I might miss something) as I wish it to be until 
   stream on time.
2. In s_stream() I call pm_runtime_get_sync() before writing any 
   register when starting a stream. And likewise I call pm_runtime_put() 
   when stopping and I no longer need to write to a register.
3. In remove() I call pm_runtime_disable().

Am I missing something obvious here?

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Geert and Laurent,

Thanks for the feedback.

On 2018-04-05 11:26:45 +0300, Laurent Pinchart wrote:

[snip]

> > Alternatively, you could check for a valid number of lanes, and use
> > knowledge about the internal lane bits:
> > 
> > phycnt = PHYCNT_ENABLECLK;
> > phycnt |= (1 << priv->lanes) - 1;
> 
> If Niklas is fine with that, I like it too.

I'm fine what that thanks for the suggestion Geert!

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Maxime,

Thanks for your feedback.

On 2018-03-29 13:30:39 +0200, Maxime Ripard wrote:
> Hi Niklas,
> 
> On Tue, Feb 13, 2018 at 12:01:32AM +0100, Niklas Söderlund wrote:
> > +   switch (priv->lanes) {
> > +   case 1:
> > +   phycnt = PHYCNT_ENABLECLK | PHYCNT_ENABLE_0;
> > +   break;
> > +   case 2:
> > +   phycnt = PHYCNT_ENABLECLK | PHYCNT_ENABLE_1 | PHYCNT_ENABLE_0;
> > +   break;
> > +   case 4:
> > +   phycnt = PHYCNT_ENABLECLK | PHYCNT_ENABLE_3 | PHYCNT_ENABLE_2 |
> > +   PHYCNT_ENABLE_1 | PHYCNT_ENABLE_0;
> > +   break;
> > +   default:
> > +   return -EINVAL;
> > +   }
> 
> I guess you could have a simpler construct here using this:
> 
> phycnt = PHYCNT_ENABLECLK;
> 
> switch (priv->lanes) {
> case 4:
>   phycnt |= PHYCNT_ENABLE_3 | PHYCNT_ENABLE_2;
> case 2:
>   phycnt |= PHYCNT_ENABLE_1;
> case 1:
>   phycnt |= PHYCNT_ENABLE_0;
>   break;
> 
> default:
>   return -EINVAL;
> }
> 
> But that's really up to you.

Thanks for the suggestion and sparking of the discussion, I think I will 
go with Geert at.al approach of:

phycnt = PHYCNT_ENABLECLK;
phycnt |= (1 << priv->lanes) - 1;

> 
> > +static int rcar_csi2_probe(struct platform_device *pdev)
> > +{
> > +   const struct soc_device_attribute *attr;
> > +   struct rcar_csi2 *priv;
> > +   unsigned int i;
> > +   int ret;
> > +
> > +   priv = devm_kzalloc(>dev, sizeof(*priv), GFP_KERNEL);
> > +   if (!priv)
> > +   return -ENOMEM;
> > +
> > +   priv->info = of_device_get_match_data(>dev);
> > +
> > +   /* r8a7795 ES1.x behaves different then ES2.0+ but no own compat */
> > +   attr = soc_device_match(r8a7795es1);
> > +   if (attr)
> > +   priv->info = attr->data;
> > +
> > +   priv->dev = >dev;
> > +
> > +   mutex_init(>lock);
> > +   priv->stream_count = 0;
> > +
> > +   ret = rcar_csi2_probe_resources(priv, pdev);
> > +   if (ret) {
> > +   dev_err(priv->dev, "Failed to get resources\n");
> > +   return ret;
> > +   }
> > +
> > +   platform_set_drvdata(pdev, priv);
> > +
> > +   ret = rcar_csi2_parse_dt(priv);
> > +   if (ret)
> > +   return ret;
> > +
> > +   priv->subdev.owner = THIS_MODULE;
> > +   priv->subdev.dev = >dev;
> > +   v4l2_subdev_init(>subdev, _csi2_subdev_ops);
> > +   v4l2_set_subdevdata(>subdev, >dev);
> > +   snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s %s",
> > +KBUILD_MODNAME, dev_name(>dev));
> > +   priv->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
> > +
> > +   priv->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
> > +   priv->subdev.entity.ops = _csi2_entity_ops;
> > +
> > +   priv->pads[RCAR_CSI2_SINK].flags = MEDIA_PAD_FL_SINK;
> > +   for (i = RCAR_CSI2_SOURCE_VC0; i < NR_OF_RCAR_CSI2_PAD; i++)
> > +   priv->pads[i].flags = MEDIA_PAD_FL_SOURCE;
> > +
> > +   ret = media_entity_pads_init(>subdev.entity, NR_OF_RCAR_CSI2_PAD,
> > +priv->pads);
> > +   if (ret)
> > +   goto error;
> > +
> > +   pm_runtime_enable(>dev);
> 
> Is CONFIG_PM mandatory on Renesas SoCs? If not, you end up with the
> device uninitialised at probe, and pm_runtime_get_sync will not
> initialise it either if CONFIG_PM is not enabled. I guess you could
> call your runtime_resume function unconditionally, and mark the device
> as active in runtime_pm using pm_runtime_set_active.

Yes CONFIG_PM is selected by ARCH_RENESAS. Thanks for letting me know 
about this in any case I was not aware of this behavior in the case 
CONFIG_PM where not enabled.

> 
> Looks good otherwise, once fixed (and if relevant):
> Reviewed-by: Maxime Ripard <maxime.rip...@bootlin.com>

Thanks!

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Laurent,

Thanks for your feedback.

On 2018-04-04 18:25:23 +0300, Laurent Pinchart wrote:

[snip]

> > > diff --git a/drivers/media/platform/rcar-vin/rcar-csi2.c
> > > b/drivers/media/platform/rcar-vin/rcar-csi2.c new file mode 100644
> > > index ..c0c2a763151bc928
> > > --- /dev/null
> > > +++ b/drivers/media/platform/rcar-vin/rcar-csi2.c
> > > @@ -0,0 +1,884 @@
> > > +// SPDX-License-Identifier: GPL-2.0+
> 
> Do you intend making it GPL-2.0+ or did you mean GPL-2.0 ?

Wops I intended to make it GPL-2.0 thanks for catching this.

-- 
Regards,
Niklas Söderlund


Re: [PATCH v13 2/2] rcar-csi2: add Renesas R-Car MIPI CSI-2 receiver driver

2018-04-15 Thread Niklas Söderlund
Hi Kieran,

Thanks for your feedback.

On 2018-03-13 23:23:49 +0100, Kieran Bingham wrote:
> Hi Niklas
> 
> Thank you for this patch ...
> I know it has been a lot of work getting this and the VIN together!
> 
> On 13/02/18 00:01, Niklas Söderlund wrote:
> > A V4L2 driver for Renesas R-Car MIPI CSI-2 receiver. The driver
> > supports the R-Car Gen3 SoCs where separate CSI-2 hardware blocks are
> > connected between the video sources and the video grabbers (VIN).
> > 
> > Driver is based on a prototype by Koji Matsuoka in the Renesas BSP.
> > 
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
> > Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
> 
> I don't think there's actually anything major in the below - so with any
> comments addressed, or specifically ignored you can add my:
> 
> Reviewed-by: Kieran Bingham <kieran.bingham+rene...@ideasonboard.com>

Thanks, see bellow for answers to your questions. I have removed the 
questions Laurent already have provided a reply for, thanks for doing 
that!

> 
> tag if you like.
> 
> 
> > ---
> >  drivers/media/platform/rcar-vin/Kconfig |  12 +
> >  drivers/media/platform/rcar-vin/Makefile|   1 +
> >  drivers/media/platform/rcar-vin/rcar-csi2.c | 884 
> > 
> >  3 files changed, 897 insertions(+)
> >  create mode 100644 drivers/media/platform/rcar-vin/rcar-csi2.c
> > 
> > diff --git a/drivers/media/platform/rcar-vin/Kconfig 
> > b/drivers/media/platform/rcar-vin/Kconfig
> > index af4c98b44d2e22cb..6875f30c1ae42631 100644
> > --- a/drivers/media/platform/rcar-vin/Kconfig
> > +++ b/drivers/media/platform/rcar-vin/Kconfig
> > @@ -1,3 +1,15 @@
> > +config VIDEO_RCAR_CSI2
> > +   tristate "R-Car MIPI CSI-2 Receiver"
> > +   depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF
> 
> I think I recall recently seeing that depending upon OF is redundant and not
> necessary (though I think that's minor in this instance)

I can't seem to find anything to indicate this, but as you state if 
there is such a transition ongoing we can delete this later IMHO.

$ find . -name Kconfig -exec grep OF {} \; | grep "depends on" | wc -l
622

But I'm happy to be proven wrong and remove it now as well.

[snip]

> > +static const struct rcar_csi2_format 
> > *rcar_csi2_code_to_fmt(unsigned int code)
> > +{
> > +   unsigned int i;
> > +
> > +   for (i = 0; i < ARRAY_SIZE(rcar_csi2_formats); i++)
> > +   if (rcar_csi2_formats[i].code == code)
> > +   return rcar_csi2_formats + i;
> 
> I would have written this as:
>   return _csi2_formats[i];  but I think your way is valid too :)

This seems to be the popular opinion among reviewers so I will adopt 
this style for the next version :-)


> 
> I have a nice 'for_each_entity_in_array' macro I keep meaning to post which
> would be nice here too - but hey - for another day.

Looking forward to it.

[snip]

> > +static int rcar_csi2_wait_phy_start(struct rcar_csi2 *priv)
> > +{
> > +   int timeout;
> > +
> > +   /* Wait for the clock and data lanes to enter LP-11 state. */
> > +   for (timeout = 100; timeout > 0; timeout--) {
> > +   const u32 lane_mask = (1 << priv->lanes) - 1;
> > +
> > +   if ((rcar_csi2_read(priv, PHCLM_REG) & 1) == 1 &&
> 
> The '1' is not very clear here.. Could this be:
> 
>   if ((rcar_csi2_read(priv, PHCLM_REG) & PHCLM_STOPSTATE) &&

Great catch I have clearly missed this define. I will call it 
PHCLM_STOPSTATECLK since that it's what the latest datasheet I have 
calls it. Thanks for pointing this out.

[snip]

> > +static int rcar_csi2_s_stream(struct v4l2_subdev *sd, int enable)
> > +{
> > +   struct rcar_csi2 *priv = sd_to_csi2(sd);
> > +   struct v4l2_subdev *nextsd;
> > +   int ret = 0;
> > +
> > +   mutex_lock(>lock);
> > +
> > +   if (!priv->remote) {
> > +   ret = -ENODEV;
> > +   goto out;
> > +   }
> > +
> > +   nextsd = priv->remote;
> > +
> > +   if (enable && priv->stream_count == 0) {
> > +   pm_runtime_get_sync(priv->dev);
> > +
> > +   ret = rcar_csi2_start(priv);
> > +   if (ret) {
> > +   pm_runtime_put(priv->dev);
> > +   goto out;
> > +   }
> > +
> > +   ret = v4l2_subdev_call(nextsd, video, s_stream, 1);
> 
> Would it be nicer to pass 'enable' through instead of the '1'? (of course
> enable==1 h

[PATCH v14 31/33] rcar-vin: enable support for r8a7795

2018-04-14 Thread Niklas Söderlund
Add the SoC specific information for Renesas r8a7795 ES1.x and ES2.0.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/platform/rcar-vin/Kconfig |   2 +-
 drivers/media/platform/rcar-vin/rcar-core.c | 120 
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/rcar-vin/Kconfig 
b/drivers/media/platform/rcar-vin/Kconfig
index af4c98b44d2e22cb..8fa7ee468c63afb9 100644
--- a/drivers/media/platform/rcar-vin/Kconfig
+++ b/drivers/media/platform/rcar-vin/Kconfig
@@ -6,7 +6,7 @@ config VIDEO_RCAR_VIN
select V4L2_FWNODE
---help---
  Support for Renesas R-Car Video Input (VIN) driver.
- Supports R-Car Gen2 SoCs.
+ Supports R-Car Gen2 and Gen3 SoCs.
 
  To compile this driver as a module, choose M here: the
  module will be called rcar-vin.
diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 0cc76d73115e9277..81119ae4402d0990 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -21,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -832,6 +833,104 @@ static const struct rvin_info rcar_info_gen2 = {
.max_height = 2048,
 };
 
+static const struct rvin_group_route rcar_info_r8a7795_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
+   { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
+   { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
+   { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
+   { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
+   { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
+   { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
+   { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
+   { /* Sentinel */ }
+};
+
+static const struct rvin_info rcar_info_r8a7795 = {
+   .model = RCAR_GEN3,
+   .use_mc = true,
+   .max_width = 4096,
+   .max_height = 4096,
+   .routes = rcar_info_r8a7795_routes,
+};
+
+static const struct rvin_group_route rcar_info_r8a7795es1_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI21, .channel = 0, .vin = 0, .mask = BIT(2) | BIT(5) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
+   { .csi = RVIN_CSI21, .channel = 0, .vin = 1, .mask = BIT(1) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
+   { .csi = RVIN_CSI21, .channel = 1, .vin = 1, .mask = BIT(5) },
+   { .csi = RVIN_CSI21, .channel = 0, .vin = 2, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin 

[PATCH v14 32/33] rcar-vin: enable support for r8a7796

2018-04-14 Thread Niklas Söderlund
Add the SoC specific information for Renesas r8a7796.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 44 +
 1 file changed, 44 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 81119ae4402d0990..81c82793b1312aae 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -931,6 +931,46 @@ static const struct rvin_info rcar_info_r8a7795es1 = {
.routes = rcar_info_r8a7795es1_routes,
 };
 
+static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
+   { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
+   { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
+   { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
+   { /* Sentinel */ }
+};
+
+static const struct rvin_info rcar_info_r8a7796 = {
+   .model = RCAR_GEN3,
+   .use_mc = true,
+   .max_width = 4096,
+   .max_height = 4096,
+   .routes = rcar_info_r8a7796_routes,
+};
+
 static const struct of_device_id rvin_of_id_table[] = {
{
.compatible = "renesas,vin-r8a7778",
@@ -964,6 +1004,10 @@ static const struct of_device_id rvin_of_id_table[] = {
.compatible = "renesas,vin-r8a7795",
.data = _info_r8a7795,
},
+   {
+   .compatible = "renesas,vin-r8a7796",
+   .data = _info_r8a7796,
+   },
{ /* Sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
-- 
2.16.2



[PATCH v14 33/33] rcar-vin: enable support for r8a77970

2018-04-14 Thread Niklas Söderlund
Add the SoC specific information for Renesas r8a77970.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 81c82793b1312aae..55b745ac86a5884d 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -971,6 +971,25 @@ static const struct rvin_info rcar_info_r8a7796 = {
.routes = rcar_info_r8a7796_routes,
 };
 
+static const struct rvin_group_route _rcar_info_r8a77970_routes[] = {
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
+   { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
+   { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
+   { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
+   { /* Sentinel */ }
+};
+
+static const struct rvin_info rcar_info_r8a77970 = {
+   .model = RCAR_GEN3,
+   .use_mc = true,
+   .max_width = 4096,
+   .max_height = 4096,
+   .routes = _rcar_info_r8a77970_routes,
+};
+
 static const struct of_device_id rvin_of_id_table[] = {
{
.compatible = "renesas,vin-r8a7778",
@@ -1008,6 +1027,10 @@ static const struct of_device_id rvin_of_id_table[] = {
.compatible = "renesas,vin-r8a7796",
.data = _info_r8a7796,
},
+   {
+   .compatible = "renesas,vin-r8a77970",
+   .data = _info_r8a77970,
+   },
{ /* Sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
-- 
2.16.2



[PATCH v14 29/33] rcar-vin: add link notify for Gen3

2018-04-14 Thread Niklas Söderlund
Add the ability to process media device link change requests. Link
enabling is a bit complicated on Gen3, whether or not it's possible to
enable a link depends on what other links already are enabled. On Gen3
the 8 VINs are split into two subgroup's (VIN0-3 and VIN4-7) and from a
routing perspective these two groups are independent of each other.
Each subgroup's routing is controlled by the subgroup VIN master
instance (VIN0 and VIN4).

There are a limited number of possible route setups available for each
subgroup and the configuration of each setup is dictated by the
hardware. On H3 for example there are 6 possible route setups for each
subgroup to choose from.

This leads to the media device link notification code being rather large
since it will find the best routing configuration to try and accommodate
as many links as possible. When it's not possible to enable a new link
due to hardware constrains the link_notifier callback will return
-EMLINK.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>

---

* Changes since v12
- Add review tag from Laurent.

* Changes since v11
- Fixed spelling
- Updated comment to clarify the intent that no link can be enabled if
any video node is open.
- Use container_of() instead of a loop to find struct vin_dev from the
video device.
---
 drivers/media/platform/rcar-vin/rcar-core.c | 147 
 1 file changed, 147 insertions(+)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 99f6301a778046df..0cc76d73115e9277 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -24,6 +24,7 @@
 
 #include 
 #include 
+#include 
 
 #include "rcar-vin.h"
 
@@ -44,6 +45,151 @@
  */
 #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
 
+/* 
-
+ * Media Controller link notification
+ */
+
+/* group lock should be held when calling this function. */
+static int rvin_group_entity_to_csi_id(struct rvin_group *group,
+  struct media_entity *entity)
+{
+   struct v4l2_subdev *sd;
+   unsigned int i;
+
+   sd = media_entity_to_v4l2_subdev(entity);
+
+   for (i = 0; i < RVIN_CSI_MAX; i++)
+   if (group->csi[i].subdev == sd)
+   return i;
+
+   return -ENODEV;
+}
+
+static unsigned int rvin_group_get_mask(struct rvin_dev *vin,
+   enum rvin_csi_id csi_id,
+   unsigned char channel)
+{
+   const struct rvin_group_route *route;
+   unsigned int mask = 0;
+
+   for (route = vin->info->routes; route->mask; route++) {
+   if (route->vin == vin->id &&
+   route->csi == csi_id &&
+   route->channel == channel) {
+   vin_dbg(vin,
+   "Adding route: vin: %d csi: %d channel: %d\n",
+   route->vin, route->csi, route->channel);
+   mask |= route->mask;
+   }
+   }
+
+   return mask;
+}
+
+/*
+ * Link setup for the links between a VIN and a CSI-2 receiver is a bit
+ * complex. The reason for this is that the register controlling routing
+ * is not present in each VIN instance. There are special VINs which
+ * control routing for themselves and other VINs. There are not many
+ * different possible links combinations that can be enabled at the same
+ * time, therefor all already enabled links which are controlled by a
+ * master VIN need to be taken into account when making the decision
+ * if a new link can be enabled or not.
+ *
+ * 1. Find out which VIN the link the user tries to enable is connected to.
+ * 2. Lookup which master VIN controls the links for this VIN.
+ * 3. Start with a bitmask with all bits set.
+ * 4. For each previously enabled link from the master VIN bitwise AND its
+ *route mask (see documentation for mask in struct rvin_group_route)
+ *with the bitmask.
+ * 5. Bitwise AND the mask for the link the user tries to enable to the 
bitmask.
+ * 6. If the bitmask is not empty at this point the new link can be enabled
+ *while keeping all previous links enabled. Update the CHSEL value of the
+ *master VIN and inform the user that the link could be enabled.
+ *
+ * Please note that no link can be enabled if any VIN in the group is
+ * currently open.
+ */
+static int rvin_group_link_notify(struct media_link *link, u32 flags,
+ unsigned int notification)
+{
+   struct rvin_group *group = container_of(link->graph_obj.mdev,
+

[PATCH v14 30/33] rcar-vin: extend {start,stop}_streaming to work with media controller

2018-04-14 Thread Niklas Söderlund
The procedure to start or stop streaming using the non-MC single
subdevice and the MC graph and multiple subdevices are quite different.
Create a new function to abstract which method is used based on which
mode the driver is running in and add logic to start the MC graph.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>

---

* Changes since v12
- Rebase to latest media-tree/master changed a 'return ret' to a 'goto
  out' in rvin_start_streaming().
---
 drivers/media/platform/rcar-vin/rcar-dma.c | 135 +++--
 1 file changed, 127 insertions(+), 8 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c 
b/drivers/media/platform/rcar-vin/rcar-dma.c
index d70a689d9dd3713f..4a3a195e7f59047c 100644
--- a/drivers/media/platform/rcar-vin/rcar-dma.c
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -996,10 +996,126 @@ static void rvin_buffer_queue(struct vb2_buffer *vb)
spin_unlock_irqrestore(>qlock, flags);
 }
 
+static int rvin_mc_validate_format(struct rvin_dev *vin, struct v4l2_subdev 
*sd,
+  struct media_pad *pad)
+{
+   struct v4l2_subdev_format fmt = {
+   .which = V4L2_SUBDEV_FORMAT_ACTIVE,
+   };
+
+   fmt.pad = pad->index;
+   if (v4l2_subdev_call(sd, pad, get_fmt, NULL, ))
+   return -EPIPE;
+
+   switch (fmt.format.code) {
+   case MEDIA_BUS_FMT_YUYV8_1X16:
+   case MEDIA_BUS_FMT_UYVY8_2X8:
+   case MEDIA_BUS_FMT_UYVY10_2X10:
+   case MEDIA_BUS_FMT_RGB888_1X24:
+   vin->mbus_code = fmt.format.code;
+   break;
+   default:
+   return -EPIPE;
+   }
+
+   switch (fmt.format.field) {
+   case V4L2_FIELD_TOP:
+   case V4L2_FIELD_BOTTOM:
+   case V4L2_FIELD_NONE:
+   case V4L2_FIELD_INTERLACED_TB:
+   case V4L2_FIELD_INTERLACED_BT:
+   case V4L2_FIELD_INTERLACED:
+   case V4L2_FIELD_SEQ_TB:
+   case V4L2_FIELD_SEQ_BT:
+   /* Supported natively */
+   break;
+   case V4L2_FIELD_ALTERNATE:
+   switch (vin->format.field) {
+   case V4L2_FIELD_TOP:
+   case V4L2_FIELD_BOTTOM:
+   case V4L2_FIELD_NONE:
+   break;
+   case V4L2_FIELD_INTERLACED_TB:
+   case V4L2_FIELD_INTERLACED_BT:
+   case V4L2_FIELD_INTERLACED:
+   case V4L2_FIELD_SEQ_TB:
+   case V4L2_FIELD_SEQ_BT:
+   /* Use VIN hardware to combine the two fields */
+   fmt.format.height *= 2;
+   break;
+   default:
+   return -EPIPE;
+   }
+   break;
+   default:
+   return -EPIPE;
+   }
+
+   if (fmt.format.width != vin->format.width ||
+   fmt.format.height != vin->format.height ||
+   fmt.format.code != vin->mbus_code)
+   return -EPIPE;
+
+   return 0;
+}
+
+static int rvin_set_stream(struct rvin_dev *vin, int on)
+{
+   struct media_pipeline *pipe;
+   struct media_device *mdev;
+   struct v4l2_subdev *sd;
+   struct media_pad *pad;
+   int ret;
+
+   /* No media controller used, simply pass operation to subdevice. */
+   if (!vin->info->use_mc) {
+   ret = v4l2_subdev_call(vin->digital->subdev, video, s_stream,
+  on);
+
+   return ret == -ENOIOCTLCMD ? 0 : ret;
+   }
+
+   pad = media_entity_remote_pad(>pad);
+   if (!pad)
+   return -EPIPE;
+
+   sd = media_entity_to_v4l2_subdev(pad->entity);
+
+   if (!on) {
+   media_pipeline_stop(>vdev.entity);
+   return v4l2_subdev_call(sd, video, s_stream, 0);
+   }
+
+   ret = rvin_mc_validate_format(vin, sd, pad);
+   if (ret)
+   return ret;
+
+   /*
+* The graph lock needs to be taken to protect concurrent
+* starts of multiple VIN instances as they might share
+* a common subdevice down the line and then should use
+* the same pipe.
+*/
+   mdev = vin->vdev.entity.graph_obj.mdev;
+   mutex_lock(>graph_mutex);
+   pipe = sd->entity.pipe ? sd->entity.pipe : >vdev.pipe;
+   ret = __media_pipeline_start(>vdev.entity, pipe);
+   mutex_unlock(>graph_mutex);
+   if (ret)
+   return ret;
+
+   ret = v4l2_subdev_call(sd, video, s_stream, 1);
+   if (ret == -ENOIOCTLCMD)
+   ret = 0;
+   if (ret)
+   media_pipeline_stop(>vdev.entity);
+
+   return ret;
+}
+
 static int rvin_start_streaming(struct vb2_queue *vq, unsigned int count)
 {
struct rvin_dev *vin

[PATCH v14 28/33] rcar-vin: parse Gen3 OF and setup media graph

2018-04-14 Thread Niklas Söderlund
The parsing and registering CSI-2 subdevices with the v4l2 async
framework is a collaborative effort shared between the VIN instances
which are part of the group. When the last VIN in the group is probed it
asks all other VINs to parse its share of OF and record the async
subdevices it finds in the notifier belonging to the last probed VIN.

Once all CSI-2 subdevices in this notifier are bound proceed to register
all VIN video devices of the group and crate media device links between
all CSI-2 and VIN entities according to the SoC specific routing
configuration.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 246 +++-
 drivers/media/platform/rcar-vin/rcar-vin.h  |  12 +-
 2 files changed, 254 insertions(+), 4 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index 38eaa246e4c0a7b5..99f6301a778046df 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -27,6 +27,23 @@
 
 #include "rcar-vin.h"
 
+/*
+ * The companion CSI-2 receiver driver (rcar-csi2) is known
+ * and we know it has one source pad (pad 0) and four sink
+ * pads (pad 1-4). So to translate a pad on the remote
+ * CSI-2 receiver to/from the VIN internal channel number simply
+ * subtract/add one from the pad/channel number.
+ */
+#define rvin_group_csi_pad_to_channel(pad) ((pad) - 1)
+#define rvin_group_csi_channel_to_pad(channel) ((channel) + 1)
+
+/*
+ * Not all VINs are created equal, master VINs control the
+ * routing for other VIN's. We can figure out which VIN is
+ * master by looking at a VINs id.
+ */
+#define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
+
 /* 
-
  * Gen3 CSI2 Group Allocator
  */
@@ -409,6 +426,216 @@ static int rvin_digital_graph_init(struct rvin_dev *vin)
return 0;
 }
 
+/* 
-
+ * Group async notifier
+ */
+
+static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
+{
+   struct rvin_dev *vin = notifier_to_vin(notifier);
+   const struct rvin_group_route *route;
+   unsigned int i;
+   int ret;
+
+   ret = v4l2_device_register_subdev_nodes(>v4l2_dev);
+   if (ret) {
+   vin_err(vin, "Failed to register subdev nodes\n");
+   return ret;
+   }
+
+   /* Register all video nodes for the group. */
+   for (i = 0; i < RCAR_VIN_NUM; i++) {
+   if (vin->group->vin[i]) {
+   ret = rvin_v4l2_register(vin->group->vin[i]);
+   if (ret)
+   return ret;
+   }
+   }
+
+   /* Create all media device links between VINs and CSI-2's. */
+   mutex_lock(>group->lock);
+   for (route = vin->info->routes; route->mask; route++) {
+   struct media_pad *source_pad, *sink_pad;
+   struct media_entity *source, *sink;
+   unsigned int source_idx;
+
+   /* Check that VIN is part of the group. */
+   if (!vin->group->vin[route->vin])
+   continue;
+
+   /* Check that VIN' master is part of the group. */
+   if (!vin->group->vin[rvin_group_id_to_master(route->vin)])
+   continue;
+
+   /* Check that CSI-2 is part of the group. */
+   if (!vin->group->csi[route->csi].subdev)
+   continue;
+
+   source = >group->csi[route->csi].subdev->entity;
+   source_idx = rvin_group_csi_channel_to_pad(route->channel);
+   source_pad = >pads[source_idx];
+
+   sink = >group->vin[route->vin]->vdev.entity;
+   sink_pad = >pads[0];
+
+   /* Skip if link already exists. */
+   if (media_entity_find_link(source_pad, sink_pad))
+   continue;
+
+   ret = media_create_pad_link(source, source_idx, sink, 0, 0);
+   if (ret) {
+   vin_err(vin, "Error adding link from %s to %s\n",
+   source->name, sink->name);
+   break;
+   }
+   }
+   mutex_unlock(>group->lock);
+
+   return ret;
+}
+
+static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
+struct v4l2_subdev *subdev,
+struct v4l2_async_subdev *asd)
+{
+   struct rvin_dev *vin = notifier_to_vin(notifier);
+   

[PATCH v14 25/33] rcar-vin: add group allocator functions

2018-04-14 Thread Niklas Söderlund
In media controller mode all VIN instances needs to be part of the same
media graph. There is also a need for each VIN instance to know about
and in some cases be able to communicate with other VIN instances.

Add an allocator framework where the first VIN instance to be probed
creates a shared data structure and registers a media device.
Consecutive VINs insert themself into the global group.

Signed-off-by: Niklas Söderlund <niklas.soderlund+rene...@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinch...@ideasonboard.com>
Reviewed-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/platform/rcar-vin/rcar-core.c | 174 +++-
 drivers/media/platform/rcar-vin/rcar-vin.h  |  31 +
 2 files changed, 203 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index e0a33d372c500ccd..38eaa246e4c0a7b5 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -20,12 +20,174 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
 
 #include "rcar-vin.h"
 
+/* 
-
+ * Gen3 CSI2 Group Allocator
+ */
+
+/* FIXME:  This should if we find a system that supports more
+ * than one group for the whole system be replaced with a linked
+ * list of groups. And eventually all of this should be replaced
+ * with a global device allocator API.
+ *
+ * But for now this works as on all supported systems there will
+ * be only one group for all instances.
+ */
+
+static DEFINE_MUTEX(rvin_group_lock);
+static struct rvin_group *rvin_group_data;
+
+static void rvin_group_cleanup(struct rvin_group *group)
+{
+   media_device_unregister(>mdev);
+   media_device_cleanup(>mdev);
+   mutex_destroy(>lock);
+}
+
+static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin)
+{
+   struct media_device *mdev = >mdev;
+   const struct of_device_id *match;
+   struct device_node *np;
+   int ret;
+
+   mutex_init(>lock);
+
+   /* Count number of VINs in the system */
+   group->count = 0;
+   for_each_matching_node(np, vin->dev->driver->of_match_table)
+   if (of_device_is_available(np))
+   group->count++;
+
+   vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
+
+   mdev->dev = vin->dev;
+
+   match = of_match_node(vin->dev->driver->of_match_table,
+ vin->dev->of_node);
+
+   strlcpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
+   strlcpy(mdev->model, match->compatible, sizeof(mdev->model));
+   snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
+dev_name(mdev->dev));
+
+   media_device_init(mdev);
+
+   ret = media_device_register(>mdev);
+   if (ret)
+   rvin_group_cleanup(group);
+
+   return ret;
+}
+
+static void rvin_group_release(struct kref *kref)
+{
+   struct rvin_group *group =
+   container_of(kref, struct rvin_group, refcount);
+
+   mutex_lock(_group_lock);
+
+   rvin_group_data = NULL;
+
+   rvin_group_cleanup(group);
+
+   kfree(group);
+
+   mutex_unlock(_group_lock);
+}
+
+static int rvin_group_get(struct rvin_dev *vin)
+{
+   struct rvin_group *group;
+   u32 id;
+   int ret;
+
+   /* Make sure VIN id is present and sane */
+   ret = of_property_read_u32(vin->dev->of_node, "renesas,id", );
+   if (ret) {
+   vin_err(vin, "%pOF: No renesas,id property found\n",
+   vin->dev->of_node);
+   return -EINVAL;
+   }
+
+   if (id >= RCAR_VIN_NUM) {
+   vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
+   vin->dev->of_node, id);
+   return -EINVAL;
+   }
+
+   /* Join or create a VIN group */
+   mutex_lock(_group_lock);
+   if (rvin_group_data) {
+   group = rvin_group_data;
+   kref_get(>refcount);
+   } else {
+   group = kzalloc(sizeof(*group), GFP_KERNEL);
+   if (!group) {
+   ret = -ENOMEM;
+   goto err_group;
+   }
+
+   ret = rvin_group_init(group, vin);
+   if (ret) {
+   kfree(group);
+   vin_err(vin, "Failed to initialize group\n");
+   goto err_group;
+   }
+
+   kref_init(>refcount);
+
+   rvin_group_data = group;
+   }
+   mutex_unlock(_group_lock);
+
+   /* Add VIN to group */
+   mutex_lock(>lock);
+
+   if (group->vin[id]) {

  1   2   3   4   5   6   7   8   9   10   >