All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver
@ 2017-02-07 15:02 Ramesh Shanmugasundaram
       [not found] ` <1486479757-32128-1-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
                   ` (7 more replies)
  0 siblings, 8 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

Hi Media, DT maintainers, All,

This patch set contains two drivers
 - Renesas R-Car Digital Radio Interface (DRIF) driver
 - Maxim's MAX2175 RF to Bits tuner driver

These patches were based on top of media-tree repo
commit: 47b037a0512d9f8675ec2693bed46c8ea6a884ab

These two drivers combined together expose a V4L2 SDR device that is compliant with the V4L2 framework [1]. Agreed review comments are incorporated in this series.

The rcar_drif device is modelled using "renesas,bonding" property. The discussion on this property is available here [2].

Change history:

v2 -> v3:
rcar_drif:
 - Reduced DRIF DT properties to expose tested I2S mode only (Hans - discussion on #v4l)
 - Fixed error path clean up of ctrl_hdl on rcar_drif

v1 -> v2:
 - SDR formats renamed as "planar" instead of sliced (Hans)
 - Documentation formatting correction (Laurent)

 rcar_drif:
 - DT model using "bonding" property
 - Addressed Laurent's coments on bindings - DT optional parameters rename & rework
 - Addressed Han's comments on driver
 - Addressed Geert's comments on DT

 max2175:
 - Avoided scaling using method proposed by Antti. Thanks
 - Bindings is a separate patch (Rob)
 - Addressed Rob's comment on bindings
 - Added Custom controls documentation (Laurent)

[1] v4l2-compliance report

root@salvator-x:~# v4l2-compliance -S /dev/swradio0
v4l2-compliance SHA   : 788b674f3827607c09c31be11c91638f816aa6ae

Driver Info:
        Driver name   : rcar_drif
        Card type     : R-Car DRIF
        Bus info      : platform:R-Car DRIF
        Driver version: 4.10.0
        Capabilities  : 0x85310000
                SDR Capture
                Tuner
                Read/Write
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x05310000
                SDR Capture
                Tuner
                Read/Write
                Streaming
                Extended Pix Format

Compliance test for device /dev/swradio0 (not using libv4l2):

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second sdr open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: OK
        test for unlimited opens: OK

Debug ioctls:
        test VIDIOC_DBG_G/S_REGISTER: OK
        test VIDIOC_LOG_STATUS: OK

Input ioctls:
        test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK
        test VIDIOC_G/S_FREQUENCY: OK
        test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
        test VIDIOC_ENUMAUDIO: OK (Not Supported)
        test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDIO: OK (Not Supported)
        Inputs: 0 Audio Inputs: 0 Tuners: 1

Output ioctls:
        test VIDIOC_G/S_MODULATOR: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK
        test VIDIOC_ENUMAUDOUT: OK (Not Supported)
        test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDOUT: OK (Not Supported)
        Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
        test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
        test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
        test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
        test VIDIOC_G/S_EDID: OK (Not Supported)

        Control ioctls:
                test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
                test VIDIOC_QUERYCTRL: OK
                test VIDIOC_G/S_CTRL: OK
                test VIDIOC_G/S/TRY_EXT_CTRLS: OK
                test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK
                test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
                Standard Controls: 5 Private Controls: 3

        Format ioctls:
                test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
                test VIDIOC_G/S_PARM: OK (Not Supported)
                test VIDIOC_G_FBUF: OK (Not Supported)
                test VIDIOC_G_FMT: OK
                test VIDIOC_TRY_FMT: OK
                test VIDIOC_S_FMT: OK
                test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
                test Cropping: OK (Not Supported)
                test Composing: OK (Not Supported)
                test Scaling: OK (Not Supported)

        Codec ioctls:
                test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
                test VIDIOC_G_ENC_INDEX: OK (Not Supported)
                test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

        Buffer ioctls:
                test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
                test VIDIOC_EXPBUF: OK (Not Supported)

Test input 0:


Total: 43, Succeeded: 43, Failed: 0, Warnings: 0
root@salvator-x:~#

[2] "bonding" DT property discussion (https://www.mail-archive.com/linux-renesas-soc@vger.kernel.org/msg09415.html)

Ramesh Shanmugasundaram (7):
  media: v4l2-ctrls: Reserve controls for MAX217X
  dt-bindings: media: Add MAX2175 binding description
  media: i2c: max2175: Add MAX2175 support
  media: Add new SDR formats PC16, PC18 & PC20
  doc_rst: media: New SDR formats PC16, PC18 & PC20
  dt-bindings: media: Add Renesas R-Car DRIF binding
  media: platform: rcar_drif: Add DRIF support

 .../devicetree/bindings/media/i2c/max2175.txt      |   61 +
 .../devicetree/bindings/media/renesas,drif.txt     |  186 +++
 .../devicetree/bindings/property-units.txt         |    1 +
 .../media/uapi/v4l/pixfmt-sdr-pcu16be.rst          |   55 +
 .../media/uapi/v4l/pixfmt-sdr-pcu18be.rst          |   55 +
 .../media/uapi/v4l/pixfmt-sdr-pcu20be.rst          |   54 +
 Documentation/media/uapi/v4l/sdr-formats.rst       |    3 +
 Documentation/media/v4l-drivers/index.rst          |    1 +
 Documentation/media/v4l-drivers/max2175.rst        |   60 +
 drivers/media/i2c/Kconfig                          |    4 +
 drivers/media/i2c/Makefile                         |    2 +
 drivers/media/i2c/max2175/Kconfig                  |    8 +
 drivers/media/i2c/max2175/Makefile                 |    4 +
 drivers/media/i2c/max2175/max2175.c                | 1438 ++++++++++++++++++
 drivers/media/i2c/max2175/max2175.h                |  108 ++
 drivers/media/platform/Kconfig                     |   25 +
 drivers/media/platform/Makefile                    |    1 +
 drivers/media/platform/rcar_drif.c                 | 1534 ++++++++++++++++++++
 drivers/media/v4l2-core/v4l2-ioctl.c               |    3 +
 include/uapi/linux/v4l2-controls.h                 |    5 +
 include/uapi/linux/videodev2.h                     |    3 +
 21 files changed, 3611 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/max2175.txt
 create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
 create mode 100644 Documentation/media/v4l-drivers/max2175.rst
 create mode 100644 drivers/media/i2c/max2175/Kconfig
 create mode 100644 drivers/media/i2c/max2175/Makefile
 create mode 100644 drivers/media/i2c/max2175/max2175.c
 create mode 100644 drivers/media/i2c/max2175/max2175.h
 create mode 100644 drivers/media/platform/rcar_drif.c

-- 
1.9.1

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH v3 1/7] media: v4l2-ctrls: Reserve controls for MAX217X
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
@ 2017-02-07 15:02     ` Ramesh Shanmugasundaram
  2017-02-07 15:02 ` [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description Ramesh Shanmugasundaram
                       ` (6 subsequent siblings)
  7 siblings, 0 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt-DgEjT+Ai2ygdnm+yROfE0A, mark.rutland-5wv7dgnIgG8,
	mchehab-DgEjT+Ai2ygdnm+yROfE0A, hverkuil-qWit8jRvyhVmR6Xm/wNWPw,
	sakari.ailus-VuQAYsv1563Yd54FQh9/CA, crope-X3B1VOXEql0
  Cc: chris.paterson2-zM6kxYcvzFBBDgjK7y7TUQ,
	laurent.pinchart-ryLnwIuWjnjg/C1BVhZhaw,
	geert+renesas-gXvu3+zWzMSzQB+pC5nmwQ,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-renesas-soc-u79uwXL29TY76Z2rM5mHXA,
	Ramesh Shanmugasundaram

Reserve controls for MAX217X RF to Bits tuner family. These hybrid
radio receiver chips are highly programmable and hence reserving 32
controls.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
---
 include/uapi/linux/v4l2-controls.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index 0d2e1e0..83b28b4 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -180,6 +180,11 @@ enum v4l2_colorfx {
  * We reserve 16 controls for this driver. */
 #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
 
+/* The base for the max217x driver controls.
+ * We reserve 32 controls for this driver
+ */
+#define V4L2_CID_USER_MAX217X_BASE		(V4L2_CID_USER_BASE + 0x1090)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 1/7] media: v4l2-ctrls: Reserve controls for MAX217X
@ 2017-02-07 15:02     ` Ramesh Shanmugasundaram
  0 siblings, 0 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

Reserve controls for MAX217X RF to Bits tuner family. These hybrid
radio receiver chips are highly programmable and hence reserving 32
controls.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 include/uapi/linux/v4l2-controls.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index 0d2e1e0..83b28b4 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -180,6 +180,11 @@ enum v4l2_colorfx {
  * We reserve 16 controls for this driver. */
 #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
 
+/* The base for the max217x driver controls.
+ * We reserve 32 controls for this driver
+ */
+#define V4L2_CID_USER_MAX217X_BASE		(V4L2_CID_USER_BASE + 0x1090)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
       [not found] ` <1486479757-32128-1-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
  2017-02-15 16:59   ` Rob Herring
  2017-04-11  9:42   ` Laurent Pinchart
  2017-02-07 15:02 ` [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support Ramesh Shanmugasundaram
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

Add device tree binding documentation for MAX2175 Rf to bits tuner
device.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 .../devicetree/bindings/media/i2c/max2175.txt      | 61 ++++++++++++++++++++++
 .../devicetree/bindings/property-units.txt         |  1 +
 2 files changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/max2175.txt

diff --git a/Documentation/devicetree/bindings/media/i2c/max2175.txt b/Documentation/devicetree/bindings/media/i2c/max2175.txt
new file mode 100644
index 0000000..f591ab4
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
@@ -0,0 +1,61 @@
+Maxim Integrated MAX2175 RF to Bits tuner
+-----------------------------------------
+
+The MAX2175 IC is an advanced analog/digital hybrid-radio receiver with
+RF to Bits® front-end designed for software-defined radio solutions.
+
+Required properties:
+--------------------
+- compatible: "maxim,max2175" for MAX2175 RF-to-bits tuner.
+- clocks: phandle to the fixed xtal clock.
+- clock-names: name of the fixed xtal clock.
+- port: child port node of a tuner that defines the local and remote
+  endpoints. The remote endpoint is assumed to be an SDR device
+  that is capable of receiving the digital samples from the tuner.
+
+Optional properties:
+--------------------
+- maxim,slave	      : phandle to the master tuner if it is a slave. This
+			is used to define two tuners in diversity mode
+			(1 master, 1 slave). By default each tuner is an
+			individual master.
+- maxim,refout-load-pF: load capacitance value (in pF) on reference
+			output drive level. The possible load values are
+			 0 (default - refout disabled)
+			10
+			20
+			30
+			40
+			60
+			70
+- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path is
+			selected for AM antenna input. By default this
+			filter path is not used.
+
+Example:
+--------
+
+Board specific DTS file
+
+/* Fixed XTAL clock node */
+maxim_xtal: clock {
+	compatible = "fixed-clock";
+	#clock-cells = <0>;
+	clock-frequency = <36864000>;
+};
+
+/* A tuner device instance under i2c bus */
+max2175_0: tuner@60 {
+	compatible = "maxim,max2175";
+	reg = <0x60>;
+	clocks = <&maxim_xtal>;
+	clock-names = "xtal";
+	maxim,refout-load-pF = <10>;
+
+	port {
+		max2175_0_ep: endpoint {
+			remote-endpoint = <&slave_rx_device>;
+		};
+	};
+
+};
diff --git a/Documentation/devicetree/bindings/property-units.txt b/Documentation/devicetree/bindings/property-units.txt
index 12278d7..f1f1c22 100644
--- a/Documentation/devicetree/bindings/property-units.txt
+++ b/Documentation/devicetree/bindings/property-units.txt
@@ -28,6 +28,7 @@ Electricity
 -ohms		: Ohms
 -micro-ohms	: micro Ohms
 -microvolt	: micro volts
+-pF		: pico farads
 
 Temperature
 ----------------------------------------
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
       [not found] ` <1486479757-32128-1-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
  2017-02-07 15:02 ` [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description Ramesh Shanmugasundaram
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
  2017-02-13 12:40   ` Hans Verkuil
  2017-02-07 15:02 ` [PATCH v3 4/7] media: Add new SDR formats PC16, PC18 & PC20 Ramesh Shanmugasundaram
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

This patch adds driver support for the MAX2175 chip. This is Maxim
Integrated's RF to Bits tuner front end chip designed for software-defined
radio solutions. This driver exposes the tuner as a sub-device instance
with standard and custom controls to configure the device.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 Documentation/media/v4l-drivers/index.rst   |    1 +
 Documentation/media/v4l-drivers/max2175.rst |   60 ++
 drivers/media/i2c/Kconfig                   |    4 +
 drivers/media/i2c/Makefile                  |    2 +
 drivers/media/i2c/max2175/Kconfig           |    8 +
 drivers/media/i2c/max2175/Makefile          |    4 +
 drivers/media/i2c/max2175/max2175.c         | 1438 +++++++++++++++++++++++++++
 drivers/media/i2c/max2175/max2175.h         |  108 ++
 8 files changed, 1625 insertions(+)
 create mode 100644 Documentation/media/v4l-drivers/max2175.rst
 create mode 100644 drivers/media/i2c/max2175/Kconfig
 create mode 100644 drivers/media/i2c/max2175/Makefile
 create mode 100644 drivers/media/i2c/max2175/max2175.c
 create mode 100644 drivers/media/i2c/max2175/max2175.h

diff --git a/Documentation/media/v4l-drivers/index.rst b/Documentation/media/v4l-drivers/index.rst
index a606d1c..d8cade5 100644
--- a/Documentation/media/v4l-drivers/index.rst
+++ b/Documentation/media/v4l-drivers/index.rst
@@ -42,6 +42,7 @@ For more details see the file COPYING in the source distribution of Linux.
 	davinci-vpbe
 	fimc
 	ivtv
+        max2175
 	meye
 	omap3isp
 	omap4_camera
diff --git a/Documentation/media/v4l-drivers/max2175.rst b/Documentation/media/v4l-drivers/max2175.rst
new file mode 100644
index 0000000..201af8f
--- /dev/null
+++ b/Documentation/media/v4l-drivers/max2175.rst
@@ -0,0 +1,60 @@
+Maxim Integrated MAX2175 RF to bits tuner driver
+================================================
+
+The MAX2175 driver implements the following driver-specific controls:
+
+``V4L2_CID_MAX2175_I2S_ENABLE``
+-------------------------------
+    Enable/Disable I2S output of the tuner.
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 4
+
+    * - ``(0)``
+      - I2S output is disabled.
+    * - ``(1)``
+      - I2S output is enabled.
+
+``V4L2_CID_MAX2175_HSLS``
+-------------------------
+    The high-side/low-side (HSLS) control of the tuner for a given band.
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 4
+
+    * - ``(0)``
+      - The LO frequency position is below the desired frequency.
+    * - ``(1)``
+      - The LO frequency position is above the desired frequency.
+
+``V4L2_CID_MAX2175_RX_MODE (menu)``
+-----------------------------------
+    The Rx mode controls a number of preset parameters of the tuner like sck
+    rate, sampling rate etc. These multiple settings are provided under one
+    single label called Rx mode in the datasheet. The list below shows the
+    supported modes with a brief description.
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 4
+
+    * - ``"Europe modes"``
+    * - ``"FM 1.2" (0)``
+      - This configures FM band with a sample rate of 0.512 million
+        samples/sec with a 10.24 MHz sck.
+    * - ``"DAB 1.2" (1)``
+      - This configures VHF band with a sample rate of 2.048 million
+        samples/sec with a 32.768 MHz sck.
+
+    * - ``"North America modes"``
+    * - ``"FM 1.0" (0)``
+      - This configures FM band with a sample rate of 0.7441875 million
+        samples/sec with a 14.88375 MHz sck.
+    * - ``"DAB 1.2" (1)``
+      - This configures FM band with a sample rate of 0.372 million
+        samples/sec with a 7.441875 MHz sck.
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index cee1dae..3fd1443 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -751,6 +751,10 @@ config VIDEO_SAA6752HS
 	  To compile this driver as a module, choose M here: the
 	  module will be called saa6752hs.
 
+comment "SDR tuner chips"
+
+source "drivers/media/i2c/max2175/Kconfig"
+
 comment "Miscellaneous helper chips"
 
 config VIDEO_THS7303
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index 5bc7bbe..d8a079e 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -7,6 +7,8 @@ obj-$(CONFIG_VIDEO_CX25840) += cx25840/
 obj-$(CONFIG_VIDEO_M5MOLS)	+= m5mols/
 obj-y				+= soc_camera/
 
+obj-$(CONFIG_SDR_MAX2175) 	+= max2175/
+
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_TVAUDIO) += tvaudio.o
 obj-$(CONFIG_VIDEO_TDA7432) += tda7432.o
diff --git a/drivers/media/i2c/max2175/Kconfig b/drivers/media/i2c/max2175/Kconfig
new file mode 100644
index 0000000..93a8f83
--- /dev/null
+++ b/drivers/media/i2c/max2175/Kconfig
@@ -0,0 +1,8 @@
+config SDR_MAX2175
+	tristate "Maxim 2175 RF to Bits tuner"
+	depends on VIDEO_V4L2 && MEDIA_SDR_SUPPORT && I2C
+	---help---
+	  Support for Maxim 2175 tuner
+
+	  To compile this driver as a module, choose M here; the
+	  module will be called max2175.
diff --git a/drivers/media/i2c/max2175/Makefile b/drivers/media/i2c/max2175/Makefile
new file mode 100644
index 0000000..9bb46ac
--- /dev/null
+++ b/drivers/media/i2c/max2175/Makefile
@@ -0,0 +1,4 @@
+#
+# Makefile for Maxim RF to Bits tuner device
+#
+obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/max2175/max2175.c b/drivers/media/i2c/max2175/max2175.c
new file mode 100644
index 0000000..e04c56c
--- /dev/null
+++ b/drivers/media/i2c/max2175/max2175.c
@@ -0,0 +1,1438 @@
+/*
+ * Maxim Integrated MAX2175 RF to Bits tuner driver
+ *
+ * This driver & most of the hard coded values are based on the reference
+ * application delivered by Maxim for this device.
+ *
+ * Copyright (C) 2016 Maxim Integrated Products
+ * Copyright (C) 2016 Renesas Electronics Corporation
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-of.h>
+
+#include "max2175.h"
+
+#define DRIVER_NAME "max2175"
+
+static unsigned int debug;
+module_param(debug, uint, 0644);
+MODULE_PARM_DESC(debug, "activate debug info");
+
+#define mxm_dbg(ctx, fmt, arg...) v4l2_dbg(1, debug, &ctx->sd, fmt, ## arg)
+#define mxm_err(ctx, fmt, arg...) dev_err(&ctx->client->dev, fmt, ## arg)
+
+/* Rx mode */
+struct max2175_rxmode {
+	enum max2175_band band;		/* Associated band */
+	u32 freq;			/* Default freq in Hz */
+	u8 i2s_word_size;		/* Bit value */
+};
+
+/* Register map to define preset values */
+struct max2175_reg_map {
+	u8 idx;				/* Register index */
+	u8 val;				/* Register value */
+};
+
+static const struct max2175_rxmode eu_rx_modes[] = {
+	/* EU modes */
+	[MAX2175_EU_FM_1_2] = { MAX2175_BAND_FM, 98256000, 1 },
+	[MAX2175_DAB_1_2]   = { MAX2175_BAND_VHF, 182640000, 0 },
+};
+
+static const struct max2175_rxmode na_rx_modes[] = {
+	/* NA modes */
+	[MAX2175_NA_FM_1_0] = { MAX2175_BAND_FM, 98255520, 1 },
+	[MAX2175_NA_FM_2_0] = { MAX2175_BAND_FM, 98255520, 6 },
+};
+
+/*
+ * Preset values:
+ * Based on Maxim MAX2175 Register Table revision: 130p10
+ */
+static const u8 full_fm_eu_1p0[] = {
+	0x15, 0x04, 0xb8, 0xe3, 0x35, 0x18, 0x7c, 0x00,
+	0x00, 0x7d, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
+	0x61, 0x61, 0x61, 0x61, 0x5a, 0x0f, 0x34, 0x1c,
+	0x14, 0x88, 0x33, 0x02, 0x00, 0x09, 0x00, 0x65,
+	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
+	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0x2f, 0x7e, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
+	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
+	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
+	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
+	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x40, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
+	0x00, 0x47, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
+	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
+	0x1b,
+};
+
+static const u8 full_fm_na_1p0[] = {
+	0x13, 0x08, 0x8d, 0xc0, 0x35, 0x18, 0x7d, 0x3f,
+	0x7d, 0x75, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
+	0x61, 0x61, 0x61, 0x61, 0x5c, 0x0f, 0x34, 0x1c,
+	0x14, 0x88, 0x33, 0x02, 0x00, 0x01, 0x00, 0x65,
+	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
+	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0xaf, 0x7e, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
+	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
+	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
+	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
+	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x40, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
+	0x00, 0x35, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
+	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
+	0x1b,
+};
+
+/* DAB1.2 settings */
+static const struct max2175_reg_map dab12_map[] = {
+	{ 0x01, 0x13 }, { 0x02, 0x0d }, { 0x03, 0x15 }, { 0x04, 0x55 },
+	{ 0x05, 0x0a }, { 0x06, 0xa0 }, { 0x07, 0x40 }, { 0x08, 0x00 },
+	{ 0x09, 0x00 }, { 0x0a, 0x7d }, { 0x0b, 0x4a }, { 0x0c, 0x28 },
+	{ 0x0e, 0x43 }, { 0x0f, 0xb5 }, { 0x10, 0x31 }, { 0x11, 0x9e },
+	{ 0x12, 0x68 }, { 0x13, 0x9e }, { 0x14, 0x68 }, { 0x15, 0x58 },
+	{ 0x16, 0x2f }, { 0x17, 0x3f }, { 0x18, 0x40 }, { 0x1a, 0x88 },
+	{ 0x1b, 0xaa }, { 0x1c, 0x9a }, { 0x1d, 0x00 }, { 0x1e, 0x00 },
+	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x00 }, { 0x26, 0x00 },
+	{ 0x27, 0x00 }, { 0x32, 0x08 }, { 0x33, 0xf8 }, { 0x36, 0x2d },
+	{ 0x37, 0x7e }, { 0x55, 0xaf }, { 0x56, 0x3f }, { 0x57, 0xf8 },
+	{ 0x58, 0x99 }, { 0x76, 0x00 }, { 0x77, 0x00 }, { 0x78, 0x02 },
+	{ 0x79, 0x40 }, { 0x82, 0x00 }, { 0x83, 0x00 }, { 0x85, 0x00 },
+	{ 0x86, 0x20 },
+};
+
+/* EU FM 1.2 settings */
+static const struct max2175_reg_map fmeu1p2_map[] = {
+	{ 0x01, 0x15 }, { 0x02, 0x04 }, { 0x03, 0xb8 }, { 0x04, 0xe3 },
+	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x00 },
+	{ 0x09, 0x00 }, { 0x0a, 0x73 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
+	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
+	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5a },
+	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
+	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
+	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
+	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0x2f },
+	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
+	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0x40 }, { 0x78, 0x00 },
+	{ 0x79, 0x00 }, { 0x82, 0x47 }, { 0x83, 0x00 }, { 0x85, 0x11 },
+	{ 0x86, 0x3f },
+};
+
+/* FM NA 1.0 settings */
+static const struct max2175_reg_map fmna1p0_map[] = {
+	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
+	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7d }, { 0x08, 0x3f },
+	{ 0x09, 0x7d }, { 0x0a, 0x75 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
+	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
+	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
+	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
+	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
+	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
+	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
+	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
+	{ 0x58, 0x9f }, { 0x76, 0xa6 }, { 0x77, 0x40 }, { 0x78, 0x00 },
+	{ 0x79, 0x00 }, { 0x82, 0x35 }, { 0x83, 0x00 }, { 0x85, 0x11 },
+	{ 0x86, 0x3f },
+};
+
+/* FM NA 2.0 settings */
+static const struct max2175_reg_map fmna2p0_map[] = {
+	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
+	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x54 },
+	{ 0x09, 0xa7 }, { 0x0a, 0x55 }, { 0x0b, 0x42 }, { 0x0c, 0x48 },
+	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
+	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
+	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
+	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
+	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
+	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
+	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
+	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0xc0 }, { 0x78, 0x00 },
+	{ 0x79, 0x00 }, { 0x82, 0x6b }, { 0x83, 0x00 }, { 0x85, 0x11 },
+	{ 0x86, 0x3f },
+};
+
+static const u16 ch_coeff_dab1[] = {
+	0x001c, 0x0007, 0xffcd, 0x0056, 0xffa4, 0x0033, 0x0027, 0xff61,
+	0x010e, 0xfec0, 0x0106, 0xffb8, 0xff1c, 0x023c, 0xfcb2, 0x039b,
+	0xfd4e, 0x0055, 0x036a, 0xf7de, 0x0d21, 0xee72, 0x1499, 0x6a51,
+};
+
+static const u16 ch_coeff_fmeu[] = {
+	0x0000, 0xffff, 0x0001, 0x0002, 0xfffa, 0xffff, 0x0015, 0xffec,
+	0xffde, 0x0054, 0xfff9, 0xff52, 0x00b8, 0x00a2, 0xfe0a, 0x00af,
+	0x02e3, 0xfc14, 0xfe89, 0x089d, 0xfa2e, 0xf30f, 0x25be, 0x4eb6,
+};
+
+static const u16 eq_coeff_fmeu1_ra02_m6db[] = {
+	0x0040, 0xffc6, 0xfffa, 0x002c, 0x000d, 0xff90, 0x0037, 0x006e,
+	0xffc0, 0xff5b, 0x006a, 0x00f0, 0xff57, 0xfe94, 0x0112, 0x0252,
+	0xfe0c, 0xfc6a, 0x0385, 0x0553, 0xfa49, 0xf789, 0x0b91, 0x1a10,
+};
+
+static const u16 ch_coeff_fmna[] = {
+	0x0001, 0x0003, 0xfffe, 0xfff4, 0x0000, 0x001f, 0x000c, 0xffbc,
+	0xffd3, 0x007d, 0x0075, 0xff33, 0xff01, 0x0131, 0x01ef, 0xfe60,
+	0xfc7a, 0x020e, 0x0656, 0xfd94, 0xf395, 0x02ab, 0x2857, 0x3d3f,
+};
+
+static const u16 eq_coeff_fmna1_ra02_m6db[] = {
+	0xfff1, 0xffe1, 0xffef, 0x000e, 0x0030, 0x002f, 0xfff6, 0xffa7,
+	0xff9d, 0x000a, 0x00a2, 0x00b5, 0xffea, 0xfed9, 0xfec5, 0x003d,
+	0x0217, 0x021b, 0xff5a, 0xfc2b, 0xfcbd, 0x02c4, 0x0ac3, 0x0e85,
+};
+
+static const u8 adc_presets[2][23] = {
+	{
+		0x83, 0x00, 0xcf, 0xb4, 0x0f, 0x2c, 0x0c, 0x49,
+		0x00, 0x00, 0x00, 0x8c,	0x02, 0x02, 0x00, 0x04,
+		0xec, 0x82, 0x4b, 0xcc, 0x01, 0x88, 0x0c,
+	},
+	{
+		0x83, 0x00, 0xcf, 0xb4,	0x0f, 0x2c, 0x0c, 0x49,
+		0x00, 0x00, 0x00, 0x8c,	0x02, 0x20, 0x33, 0x8c,
+		0x57, 0xd7, 0x59, 0xb7,	0x65, 0x0e, 0x0c,
+	},
+};
+
+/* Custom controls */
+#define V4L2_CID_MAX2175_I2S_ENABLE	(V4L2_CID_USER_MAX217X_BASE + 0x01)
+#define V4L2_CID_MAX2175_HSLS		(V4L2_CID_USER_MAX217X_BASE + 0x02)
+#define V4L2_CID_MAX2175_RX_MODE	(V4L2_CID_USER_MAX217X_BASE + 0x03)
+
+/* Tuner bands */
+static const struct v4l2_frequency_band eu_bands_rf = {
+	.tuner = 0,
+	.type = V4L2_TUNER_RF,
+	.index = 0,
+	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+	.rangelow   = 65000000,
+	.rangehigh  = 240000000,
+};
+
+static const struct v4l2_frequency_band na_bands_rf = {
+	.tuner = 0,
+	.type = V4L2_TUNER_RF,
+	.index = 0,
+	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+	.rangelow   = 65000000,
+	.rangehigh  = 108000000,
+};
+
+/* Regmap settings */
+static const struct regmap_range max2175_regmap_volatile_range[] = {
+	regmap_reg_range(0x30, 0x35),
+	regmap_reg_range(0x3a, 0x45),
+	regmap_reg_range(0x59, 0x5e),
+	regmap_reg_range(0x73, 0x75),
+};
+
+static const struct regmap_access_table max2175_volatile_regs = {
+	.yes_ranges = max2175_regmap_volatile_range,
+	.n_yes_ranges = ARRAY_SIZE(max2175_regmap_volatile_range),
+};
+
+static const struct reg_default max2175_reg_defaults[] = {
+	{ 0x00, 0x07},
+};
+
+static const struct regmap_config max2175_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = 0xff,
+	.reg_defaults = max2175_reg_defaults,
+	.num_reg_defaults = ARRAY_SIZE(max2175_reg_defaults),
+	.volatile_table = &max2175_volatile_regs,
+	.cache_type = REGCACHE_FLAT,
+};
+
+struct max2175 {
+	struct v4l2_subdev sd;		/* Sub-device */
+	struct i2c_client *client;	/* I2C client */
+
+	/* Controls */
+	struct v4l2_ctrl_handler ctrl_hdl;
+	struct v4l2_ctrl *lna_gain;	/* LNA gain value */
+	struct v4l2_ctrl *if_gain;	/* I/F gain value */
+	struct v4l2_ctrl *pll_lock;	/* PLL lock */
+	struct v4l2_ctrl *i2s_en;	/* I2S output enable */
+	struct v4l2_ctrl *hsls;		/* High-side/Low-side polarity */
+	struct v4l2_ctrl *rx_mode;	/* Receive mode */
+
+	/* Regmap */
+	struct regmap *regmap;
+
+	/* Cached configuration */
+	u32 freq;			/* Tuned freq In Hz */
+	const struct max2175_rxmode *rx_modes;		/* EU or NA modes */
+	const struct v4l2_frequency_band *bands_rf;	/* EU or NA bands */
+
+	/* Device settings */
+	unsigned long xtal_freq;	/* Ref Oscillator freq in Hz */
+	u32 decim_ratio;
+	bool master;			/* Master/Slave */
+	bool am_hiz;			/* AM Hi-Z filter usage */
+
+	/* ROM values */
+	u8 rom_bbf_bw_am;
+	u8 rom_bbf_bw_fm;
+	u8 rom_bbf_bw_dab;
+
+	/* Driver private variables */
+	bool mode_resolved;		/* Flag to sanity check settings */
+};
+
+static inline struct max2175 *max2175_from_sd(struct v4l2_subdev *sd)
+{
+	return container_of(sd, struct max2175, sd);
+}
+
+static inline struct max2175 *max2175_from_ctrl_hdl(struct v4l2_ctrl_handler *h)
+{
+	return container_of(h, struct max2175, ctrl_hdl);
+}
+
+/* Get bitval of a given val */
+static inline u8 max2175_get_bitval(u8 val, u8 msb, u8 lsb)
+{
+	return (val & GENMASK(msb, lsb)) >> lsb;
+}
+
+/* Read/Write bit(s) on top of regmap */
+static int max2175_read(struct max2175 *ctx, u8 idx, u8 *val)
+{
+	u32 regval = 0;
+	int ret = regmap_read(ctx->regmap, idx, &regval);
+
+	if (ret)
+		mxm_err(ctx, "read ret(%d): idx 0x%02x\n", ret, idx);
+
+	*val = regval;
+	return ret;
+}
+
+static int max2175_write(struct max2175 *ctx, u8 idx, u8 val)
+{
+	int ret = regmap_write(ctx->regmap, idx, val);
+
+	if (ret)
+		mxm_err(ctx, "write ret(%d): idx 0x%02x val 0x%02x\n",
+			ret, idx, val);
+	return ret;
+}
+
+static u8 max2175_read_bits(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb)
+{
+	u8 val;
+
+	if (max2175_read(ctx, idx, &val))
+		return 0;
+
+	return max2175_get_bitval(val, msb, lsb);
+}
+
+static int max2175_write_bits(struct max2175 *ctx, u8 idx,
+			     u8 msb, u8 lsb, u8 newval)
+{
+	int ret = regmap_update_bits(ctx->regmap, idx, GENMASK(msb, lsb),
+				     newval << lsb);
+
+	if (ret)
+		mxm_err(ctx, "wbits ret(%d): idx 0x%02x\n", ret, idx);
+
+	return ret;
+}
+
+static int max2175_write_bit(struct max2175 *ctx, u8 idx, u8 bit, u8 newval)
+{
+	return max2175_write_bits(ctx, idx, bit, bit, newval);
+}
+
+/* Checks expected pattern every msec until timeout */
+static int max2175_poll_timeout(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb,
+				u8 exp_bitval, u32 timeout_ms)
+{
+	unsigned int val;
+
+	return regmap_read_poll_timeout(ctx->regmap, idx, val,
+			(max2175_get_bitval(val, msb, lsb) == exp_bitval),
+			1000, timeout_ms * 1000);
+}
+
+static int max2175_poll_csm_ready(struct max2175 *ctx)
+{
+	int ret;
+
+	ret = max2175_poll_timeout(ctx, 69, 1, 1, 0, 50);
+	if (ret)
+		mxm_err(ctx, "csm not ready\n");
+
+	return ret;
+}
+
+#define MAX2175_IS_BAND_AM(ctx)		\
+	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_AM)
+
+#define MAX2175_IS_BAND_VHF(ctx)	\
+	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_VHF)
+
+#define MAX2175_IS_FM_MODE(ctx)		\
+	(max2175_read_bits(ctx, 12, 5, 4) == 0)
+
+#define MAX2175_IS_FMHD_MODE(ctx)	\
+	(max2175_read_bits(ctx, 12, 5, 4) == 1)
+
+#define MAX2175_IS_DAB_MODE(ctx)	\
+	(max2175_read_bits(ctx, 12, 5, 4) == 2)
+
+static int max2175_band_from_freq(u32 freq)
+{
+	if (freq >= 144000 && freq <= 26100000)
+		return MAX2175_BAND_AM;
+	else if (freq >= 65000000 && freq <= 108000000)
+		return MAX2175_BAND_FM;
+
+	return MAX2175_BAND_VHF;
+}
+
+static void max2175_i2s_enable(struct max2175 *ctx, bool enable)
+{
+	if (enable)
+		/* Stuff bits are zeroed */
+		max2175_write_bits(ctx, 104, 3, 0, 2);
+	else
+		/* Keep SCK alive */
+		max2175_write_bits(ctx, 104, 3, 0, 9);
+	mxm_dbg(ctx, "i2s %sabled\n", enable ? "en" : "dis");
+}
+
+static void max2175_set_filter_coeffs(struct max2175 *ctx, u8 m_sel,
+				      u8 bank, const u16 *coeffs)
+{
+	unsigned int i;
+	u8 coeff_addr, upper_address = 24;
+
+	mxm_dbg(ctx, "set_filter_coeffs: m_sel %d bank %d\n", m_sel, bank);
+	max2175_write_bits(ctx, 114, 5, 4, m_sel);
+
+	if (m_sel == 2)
+		upper_address = 12;
+
+	for (i = 0; i < upper_address; i++) {
+		coeff_addr = i + bank * 24;
+		max2175_write(ctx, 115, coeffs[i] >> 8);
+		max2175_write(ctx, 116, coeffs[i]);
+		max2175_write(ctx, 117, coeff_addr | 1 << 7);
+	}
+	max2175_write_bit(ctx, 117, 7, 0);
+}
+
+static void max2175_load_fmeu_1p2(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(fmeu1p2_map); i++)
+		max2175_write(ctx, fmeu1p2_map[i].idx, fmeu1p2_map[i].val);
+
+	ctx->decim_ratio = 36;
+
+	/* Load the Channel Filter Coefficients into channel filter bank #2 */
+	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmeu);
+	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
+				  eq_coeff_fmeu1_ra02_m6db);
+}
+
+static void max2175_load_dab_1p2(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(dab12_map); i++)
+		max2175_write(ctx, dab12_map[i].idx, dab12_map[i].val);
+
+	ctx->decim_ratio = 1;
+
+	/* Load the Channel Filter Coefficients into channel filter bank #2 */
+	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 2, ch_coeff_dab1);
+}
+
+static void max2175_load_fmna_1p0(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(fmna1p0_map); i++)
+		max2175_write(ctx, fmna1p0_map[i].idx, fmna1p0_map[i].val);
+}
+
+static void max2175_load_fmna_2p0(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(fmna2p0_map); i++)
+		max2175_write(ctx, fmna2p0_map[i].idx, fmna2p0_map[i].val);
+}
+
+static void max2175_set_bbfilter(struct max2175 *ctx)
+{
+	if (MAX2175_IS_BAND_AM(ctx)) {
+		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_am);
+		mxm_dbg(ctx, "set_bbfilter AM: rom %d\n", ctx->rom_bbf_bw_am);
+	} else if (MAX2175_IS_DAB_MODE(ctx)) {
+		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_dab);
+		mxm_dbg(ctx, "set_bbfilter DAB: rom %d\n", ctx->rom_bbf_bw_dab);
+	} else {
+		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_fm);
+		mxm_dbg(ctx, "set_bbfilter FM: rom %d\n", ctx->rom_bbf_bw_fm);
+	}
+}
+
+static bool max2175_set_csm_mode(struct max2175 *ctx,
+			  enum max2175_csm_mode new_mode)
+{
+	int ret = max2175_poll_csm_ready(ctx);
+
+	if (ret)
+		return ret;
+
+	max2175_write_bits(ctx, 0, 2, 0, new_mode);
+	mxm_dbg(ctx, "set csm new mode %d\n", new_mode);
+
+	/* Wait for a fixed settle down time depending on new mode */
+	switch (new_mode) {
+	case MAX2175_PRESET_TUNE:
+		usleep_range(51100, 51500);	/* 51.1ms */
+		break;
+	/*
+	 * Other mode switches need different sleep values depending on band &
+	 * mode
+	 */
+	default:
+		break;
+	}
+
+	return max2175_poll_csm_ready(ctx);
+}
+
+static int max2175_csm_action(struct max2175 *ctx,
+			      enum max2175_csm_mode action)
+{
+	int ret;
+
+	mxm_dbg(ctx, "csm_action: %d\n", action);
+
+	/* Other actions can be added in future when needed */
+	ret = max2175_set_csm_mode(ctx, MAX2175_LOAD_TO_BUFFER);
+	if (ret)
+		return ret;
+	return max2175_set_csm_mode(ctx, MAX2175_PRESET_TUNE);
+}
+
+static int max2175_set_lo_freq(struct max2175 *ctx, u32 lo_freq)
+{
+	u8 lo_mult, loband_bits = 0, vcodiv_bits = 0;
+	u32 int_desired, frac_desired;
+	enum max2175_band band;
+	int ret;
+
+	band = max2175_read_bits(ctx, 5, 1, 0);
+	switch (band) {
+	case MAX2175_BAND_AM:
+		lo_mult = 16;
+		break;
+	case MAX2175_BAND_FM:
+		if (lo_freq <= 74700000) {
+			lo_mult = 16;
+		} else if (lo_freq > 74700000 && lo_freq <= 110000000) {
+			loband_bits = 1;
+			lo_mult = 8;
+		} else {
+			loband_bits = 1;
+			vcodiv_bits = 3;
+			lo_mult = 8;
+		}
+		break;
+	case MAX2175_BAND_VHF:
+		if (lo_freq <= 210000000)
+			vcodiv_bits = 2;
+		else
+			vcodiv_bits = 1;
+
+		loband_bits = 2;
+		lo_mult = 4;
+		break;
+	default:
+		loband_bits = 3;
+		vcodiv_bits = 2;
+		lo_mult = 2;
+		break;
+	}
+
+	if (band == MAX2175_BAND_L)
+		lo_freq /= lo_mult;
+	else
+		lo_freq *= lo_mult;
+
+	int_desired = lo_freq / ctx->xtal_freq;
+	frac_desired = div_u64((u64)(lo_freq % ctx->xtal_freq) << 20,
+			       ctx->xtal_freq);
+
+	/* Check CSM is not busy */
+	ret = max2175_poll_csm_ready(ctx);
+	if (ret)
+		return ret;
+
+	mxm_dbg(ctx, "lo_mult %u int %u  frac %u\n",
+		lo_mult, int_desired, frac_desired);
+
+	/* Write the calculated values to the appropriate registers */
+	max2175_write(ctx, 1, int_desired);
+	max2175_write_bits(ctx, 2, 3, 0, (frac_desired >> 16) & 0xf);
+	max2175_write(ctx, 3, frac_desired >> 8);
+	max2175_write(ctx, 4, frac_desired);
+	max2175_write_bits(ctx, 5, 3, 2, loband_bits);
+	max2175_write_bits(ctx, 6, 7, 6, vcodiv_bits);
+	return ret;
+}
+
+/*
+ * Helper similar to DIV_ROUND_CLOSEST but an inline function that accepts s64
+ * dividend and s32 divisor
+ */
+static inline s64 max2175_round_closest(s64 dividend, s32 divisor)
+{
+	if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0))
+		return div_s64(dividend + divisor / 2, divisor);
+
+	return div_s64(dividend - divisor / 2, divisor);
+}
+
+static int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
+{
+	s32 clock_rate = ctx->xtal_freq / ctx->decim_ratio;
+	u32 nco_reg, abs_nco_freq = abs(nco_freq);
+	s64 nco_val_desired;
+	int ret;
+
+	if (abs_nco_freq < clock_rate / 2) {
+		nco_val_desired = 2 * nco_freq;
+	} else {
+		nco_val_desired = 2 * (clock_rate - abs_nco_freq);
+		if (nco_freq < 0)
+			nco_val_desired = -nco_val_desired;
+	}
+
+	nco_reg = max2175_round_closest(nco_val_desired << 20, clock_rate);
+
+	if (nco_freq < 0)
+		nco_reg += 0x200000;
+
+	/* Check CSM is not busy */
+	ret = max2175_poll_csm_ready(ctx);
+	if (ret)
+		return ret;
+
+	mxm_dbg(ctx, "freq %d desired %lld reg %u\n",
+		nco_freq, nco_val_desired, nco_reg);
+
+	/* Write the calculated values to the appropriate registers */
+	max2175_write_bits(ctx, 7, 4, 0, (nco_reg >> 16) & 0x1f);
+	max2175_write(ctx, 8, nco_reg >> 8);
+	max2175_write(ctx, 9, nco_reg);
+	return ret;
+}
+
+static int max2175_set_rf_freq_non_am_bands(struct max2175 *ctx, u64 freq,
+					    u32 lo_pos)
+{
+	s64 adj_freq, low_if_freq;
+	int ret;
+
+	mxm_dbg(ctx, "rf_freq: non AM bands\n");
+
+	if (MAX2175_IS_FM_MODE(ctx))
+		low_if_freq = 128000;
+	else if (MAX2175_IS_FMHD_MODE(ctx))
+		low_if_freq = 228000;
+	else
+		return max2175_set_lo_freq(ctx, freq);
+
+	if (MAX2175_IS_BAND_VHF(ctx) == (lo_pos == MAX2175_LO_ABOVE_DESIRED))
+		adj_freq = freq + low_if_freq;
+	else
+		adj_freq = freq - low_if_freq;
+
+	ret = max2175_set_lo_freq(ctx, adj_freq);
+	if (ret)
+		return ret;
+
+	return max2175_set_nco_freq(ctx, -low_if_freq);
+}
+
+static int max2175_set_rf_freq(struct max2175 *ctx, u64 freq, u32 lo_pos)
+{
+	int ret;
+
+	if (MAX2175_IS_BAND_AM(ctx))
+		ret = max2175_set_nco_freq(ctx, freq);
+	else
+		ret = max2175_set_rf_freq_non_am_bands(ctx, freq, lo_pos);
+
+	mxm_dbg(ctx, "set_rf_freq: ret %d freq %llu\n", ret, freq);
+	return ret;
+}
+
+static int max2175_tune_rf_freq(struct max2175 *ctx, u64 freq, u32 hsls)
+{
+	int ret;
+
+	ret = max2175_set_rf_freq(ctx, freq, hsls);
+	if (ret)
+		return ret;
+
+	ret = max2175_csm_action(ctx, MAX2175_BUFFER_PLUS_PRESET_TUNE);
+	if (ret)
+		return ret;
+
+	mxm_dbg(ctx, "tune_rf_freq: old %u new %llu\n", ctx->freq, freq);
+	ctx->freq = freq;
+	return ret;
+}
+
+static void max2175_set_hsls(struct max2175 *ctx, u32 lo_pos)
+{
+	mxm_dbg(ctx, "set_hsls: lo_pos %u\n", lo_pos);
+
+	if ((lo_pos == MAX2175_LO_BELOW_DESIRED) == MAX2175_IS_BAND_VHF(ctx))
+		max2175_write_bit(ctx, 5, 4, 1);
+	else
+		max2175_write_bit(ctx, 5, 4, 0);
+}
+
+static void max2175_set_eu_rx_mode(struct max2175 *ctx, u32 rx_mode)
+{
+	switch (rx_mode) {
+	case MAX2175_EU_FM_1_2:
+		max2175_load_fmeu_1p2(ctx);
+		break;
+
+	case MAX2175_DAB_1_2:
+		max2175_load_dab_1p2(ctx);
+		break;
+	}
+	/* Master is the default setting */
+	if (!ctx->master)
+		max2175_write_bit(ctx, 30, 7, 1);
+}
+
+static void max2175_set_na_rx_mode(struct max2175 *ctx, u32 rx_mode)
+{
+	switch (rx_mode) {
+	case MAX2175_NA_FM_1_0:
+		max2175_load_fmna_1p0(ctx);
+		break;
+	case MAX2175_NA_FM_2_0:
+		max2175_load_fmna_2p0(ctx);
+		break;
+	}
+	/* Master is the default setting */
+	if (!ctx->master)
+		max2175_write_bit(ctx, 30, 7, 1);
+
+	ctx->decim_ratio = 27;
+
+	/* Load the Channel Filter Coefficients into channel filter bank #2 */
+	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmna);
+	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
+				  eq_coeff_fmna1_ra02_m6db);
+}
+
+static int max2175_set_rx_mode(struct max2175 *ctx, u32 rx_mode)
+{
+	mxm_dbg(ctx, "set_rx_mode: %u am_hiz %u\n", rx_mode, ctx->am_hiz);
+	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
+		max2175_set_eu_rx_mode(ctx, rx_mode);
+	else
+		max2175_set_na_rx_mode(ctx, rx_mode);
+
+	if (ctx->am_hiz) {
+		mxm_dbg(ctx, "setting AM HiZ related config\n");
+		max2175_write_bit(ctx, 50, 5, 1);
+		max2175_write_bit(ctx, 90, 7, 1);
+		max2175_write_bits(ctx, 73, 1, 0, 2);
+		max2175_write_bits(ctx, 80, 5, 0, 33);
+	}
+
+	/* Load BB filter trim values saved in ROM */
+	max2175_set_bbfilter(ctx);
+
+	/* Set HSLS */
+	max2175_set_hsls(ctx, ctx->hsls->cur.val);
+
+	/* Use i2s enable settings */
+	max2175_i2s_enable(ctx, ctx->i2s_en->cur.val);
+
+	ctx->mode_resolved = true;
+	return 0;
+}
+
+static int max2175_rx_mode_from_freq(struct max2175 *ctx, u32 freq, u32 *mode)
+{
+	unsigned int i;
+	int band = max2175_band_from_freq(freq);
+
+	/* Pick the first match always */
+	for (i = 0; i <= ctx->rx_mode->maximum; i++) {
+		if (ctx->rx_modes[i].band == band) {
+			*mode = i;
+			mxm_dbg(ctx, "rx_mode_from_freq: freq %u mode %d\n",
+				freq, *mode);
+			return 0;
+		}
+	}
+	return -EINVAL;
+}
+
+static bool max2175_freq_rx_mode_valid(struct max2175 *ctx,
+					 u32 mode, u32 freq)
+{
+	int band = max2175_band_from_freq(freq);
+
+	return (ctx->rx_modes[mode].band == band);
+}
+
+static void max2175_load_adc_presets(struct max2175 *ctx)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < ARRAY_SIZE(adc_presets); i++)
+		for (j = 0; j < ARRAY_SIZE(adc_presets[0]); j++)
+			max2175_write(ctx, 146 + j + i * 55, adc_presets[i][j]);
+}
+
+static int max2175_init_power_manager(struct max2175 *ctx)
+{
+	int ret;
+
+	/* Execute on-chip power-up/calibration */
+	max2175_write_bit(ctx, 99, 2, 0);
+	usleep_range(1000, 1500);
+	max2175_write_bit(ctx, 99, 2, 1);
+
+	/* Wait for the power manager to finish. */
+	ret = max2175_poll_timeout(ctx, 69, 7, 7, 1, 50);
+	if (ret)
+		mxm_err(ctx, "init pm failed\n");
+	return ret;
+}
+
+static int max2175_recalibrate_adc(struct max2175 *ctx)
+{
+	int ret;
+
+	/* ADC Re-calibration */
+	max2175_write(ctx, 150, 0xff);
+	max2175_write(ctx, 205, 0xff);
+	max2175_write(ctx, 147, 0x20);
+	max2175_write(ctx, 147, 0x00);
+	max2175_write(ctx, 202, 0x20);
+	max2175_write(ctx, 202, 0x00);
+
+	ret = max2175_poll_timeout(ctx, 69, 4, 3, 3, 50);
+	if (ret)
+		mxm_err(ctx, "adc recalibration failed\n");
+	return ret;
+}
+
+static u8 max2175_read_rom(struct max2175 *ctx, u8 row)
+{
+	u8 data;
+
+	max2175_write_bit(ctx, 56, 4, 0);
+	max2175_write_bits(ctx, 56, 3, 0, row);
+
+	usleep_range(2000, 2500);
+	max2175_read(ctx, 58, &data);
+
+	max2175_write_bits(ctx, 56, 3, 0, 0);
+
+	mxm_dbg(ctx, "read_rom: row %d data 0x%02x\n", row, data);
+	return data;
+}
+
+static void max2175_load_from_rom(struct max2175 *ctx)
+{
+	u8 data = 0;
+
+	data = max2175_read_rom(ctx, 0);
+	ctx->rom_bbf_bw_am = data & 0x0f;
+	max2175_write_bits(ctx, 81, 3, 0, data >> 4);
+
+	data = max2175_read_rom(ctx, 1);
+	ctx->rom_bbf_bw_fm = data & 0x0f;
+	ctx->rom_bbf_bw_dab = data >> 4;
+
+	data = max2175_read_rom(ctx, 2);
+	max2175_write_bits(ctx, 82, 4, 0, data & 0x1f);
+	max2175_write_bits(ctx, 82, 7, 5, data >> 5);
+
+	data = max2175_read_rom(ctx, 3);
+	if (ctx->am_hiz) {
+		data &= 0x0f;
+		data |= max2175_read_rom(ctx, 7) & 0x40 >> 2;
+		if (!data)
+			data |= 2;
+	} else {
+		data = data & 0xf0 >> 4;
+		data |= max2175_read_rom(ctx, 7) & 0x80 >> 3;
+		if (!data)
+			data |= 30;
+	}
+	max2175_write_bits(ctx, 80, 5, 0, data + 31);
+
+	data = max2175_read_rom(ctx, 6);
+	max2175_write_bits(ctx, 81, 7, 6, data >> 6);
+}
+
+static void max2175_load_full_fm_eu_1p0(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(full_fm_eu_1p0); i++)
+		max2175_write(ctx, i + 1, full_fm_eu_1p0[i]);
+
+	usleep_range(5000, 5500);
+	ctx->decim_ratio = 36;
+}
+
+static void max2175_load_full_fm_na_1p0(struct max2175 *ctx)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(full_fm_na_1p0); i++)
+		max2175_write(ctx, i + 1, full_fm_na_1p0[i]);
+
+	usleep_range(5000, 5500);
+	ctx->decim_ratio = 27;
+}
+
+static int max2175_core_init(struct max2175 *ctx, u32 refout_bits)
+{
+	int ret;
+
+	/* MAX2175 uses 36.864MHz clock for EU & 40.154MHz for NA region */
+	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
+		max2175_load_full_fm_eu_1p0(ctx);
+	else
+		max2175_load_full_fm_na_1p0(ctx);
+
+	/* The default settings assume master */
+	if (!ctx->master)
+		max2175_write_bit(ctx, 30, 7, 1);
+
+	mxm_dbg(ctx, "refout_bits %u\n", refout_bits);
+
+	/* Set REFOUT */
+	max2175_write_bits(ctx, 56, 7, 5, refout_bits);
+
+	/* ADC Reset */
+	max2175_write_bit(ctx, 99, 1, 0);
+	usleep_range(1000, 1500);
+	max2175_write_bit(ctx, 99, 1, 1);
+
+	/* Load ADC preset values */
+	max2175_load_adc_presets(ctx);
+
+	/* Initialize the power management state machine */
+	ret = max2175_init_power_manager(ctx);
+	if (ret)
+		return ret;
+
+	/* Recalibrate ADC */
+	ret = max2175_recalibrate_adc(ctx);
+	if (ret)
+		return ret;
+
+	/* Load ROM values to appropriate registers */
+	max2175_load_from_rom(ctx);
+
+	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
+		/* Load FIR coefficients into bank 0 */
+		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
+					  ch_coeff_fmeu);
+		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
+					  eq_coeff_fmeu1_ra02_m6db);
+	} else {
+		/* Load FIR coefficients into bank 0 */
+		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
+					  ch_coeff_fmna);
+		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
+					  eq_coeff_fmna1_ra02_m6db);
+	}
+	mxm_dbg(ctx, "core initialized\n");
+	return 0;
+}
+
+static void max2175_s_ctrl_rx_mode(struct max2175 *ctx, u32 rx_mode)
+{
+	/* Load mode. Range check already done */
+	max2175_set_rx_mode(ctx, rx_mode);
+
+	mxm_dbg(ctx, "s_ctrl_rx_mode: %u curr freq %u\n", rx_mode, ctx->freq);
+
+	/* Check if current freq valid for mode & update */
+	if (max2175_freq_rx_mode_valid(ctx, rx_mode, ctx->freq))
+		max2175_tune_rf_freq(ctx, ctx->freq, ctx->hsls->cur.val);
+	else
+		/* Use default freq of mode if current freq is not valid */
+		max2175_tune_rf_freq(ctx, ctx->rx_modes[rx_mode].freq,
+				     ctx->hsls->cur.val);
+}
+
+static int max2175_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
+
+	mxm_dbg(ctx, "s_ctrl: id 0x%x, val %u\n", ctrl->id, ctrl->val);
+	switch (ctrl->id) {
+	case V4L2_CID_MAX2175_I2S_ENABLE:
+		max2175_i2s_enable(ctx, ctrl->val);
+		break;
+	case V4L2_CID_MAX2175_HSLS:
+		max2175_set_hsls(ctx, ctx->hsls->val);
+		break;
+	case V4L2_CID_MAX2175_RX_MODE:
+		max2175_s_ctrl_rx_mode(ctx, ctrl->val);
+		break;
+	}
+
+	return 0;
+}
+
+static u32 max2175_get_lna_gain(struct max2175 *ctx)
+{
+	enum max2175_band band = max2175_read_bits(ctx, 5, 1, 0);
+
+	switch (band) {
+	case MAX2175_BAND_AM:
+		return max2175_read_bits(ctx, 51, 3, 0);
+	case MAX2175_BAND_FM:
+		return max2175_read_bits(ctx, 50, 3, 0);
+	case MAX2175_BAND_VHF:
+		return max2175_read_bits(ctx, 52, 5, 0);
+	default:
+		return 0;
+	}
+}
+
+static int max2175_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
+
+	switch (ctrl->id) {
+	case V4L2_CID_RF_TUNER_LNA_GAIN:
+		ctrl->val = max2175_get_lna_gain(ctx);
+		break;
+	case V4L2_CID_RF_TUNER_IF_GAIN:
+		ctrl->val = max2175_read_bits(ctx, 49, 4, 0);
+		break;
+	case V4L2_CID_RF_TUNER_PLL_LOCK:
+		ctrl->val = (max2175_read_bits(ctx, 60, 7, 6) == 3);
+		break;
+	}
+	mxm_dbg(ctx, "g_volatile_ctrl: id 0x%x val %d\n", ctrl->id, ctrl->val);
+	return 0;
+};
+
+static int max2175_set_freq_and_mode(struct max2175 *ctx, u32 freq)
+{
+	u32 rx_mode;
+	int ret;
+
+	/* Get band from frequency */
+	ret = max2175_rx_mode_from_freq(ctx, freq, &rx_mode);
+	if (ret)
+		return ret;
+
+	mxm_dbg(ctx, "set_freq_and_mode: freq %u rx_mode %d\n", freq, rx_mode);
+
+	/* Load mode */
+	max2175_set_rx_mode(ctx, rx_mode);
+	ctx->rx_mode->cur.val = rx_mode;
+
+	/* Tune to the new freq given */
+	return max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
+}
+
+static int max2175_s_frequency(struct v4l2_subdev *sd,
+			       const struct v4l2_frequency *vf)
+{
+	struct max2175 *ctx = max2175_from_sd(sd);
+	u32 freq;
+	int ret = 0;
+
+	mxm_dbg(ctx, "s_freq: new %u curr %u, mode_resolved %d\n",
+		vf->frequency, ctx->freq, ctx->mode_resolved);
+
+	if (vf->tuner != 0)
+		return -EINVAL;
+
+	freq = clamp(vf->frequency, ctx->bands_rf->rangelow,
+		     ctx->bands_rf->rangehigh);
+
+	/* Check new freq valid for rx_mode if already resolved */
+	if (ctx->mode_resolved &&
+	    max2175_freq_rx_mode_valid(ctx, ctx->rx_mode->cur.val, freq))
+		ret = max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
+	else
+		/* Find default rx_mode for freq and tune to it */
+		ret = max2175_set_freq_and_mode(ctx, freq);
+
+	mxm_dbg(ctx, "s_freq: ret %d curr %u mode_resolved %d mode %u\n",
+		ret, ctx->freq, ctx->mode_resolved, ctx->rx_mode->cur.val);
+	return ret;
+}
+
+static int max2175_g_frequency(struct v4l2_subdev *sd,
+			       struct v4l2_frequency *vf)
+{
+	struct max2175 *ctx = max2175_from_sd(sd);
+	int ret = 0;
+
+	if (vf->tuner != 0)
+		return -EINVAL;
+
+	/* RF freq */
+	vf->type = V4L2_TUNER_RF;
+	vf->frequency = ctx->freq;
+	return ret;
+}
+
+static int max2175_enum_freq_bands(struct v4l2_subdev *sd,
+			    struct v4l2_frequency_band *band)
+{
+	struct max2175 *ctx = max2175_from_sd(sd);
+
+	if (band->tuner != 0 || band->index != 0)
+		return -EINVAL;
+
+	*band = *ctx->bands_rf;
+	return 0;
+}
+
+static int max2175_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
+{
+	struct max2175 *ctx = max2175_from_sd(sd);
+
+	if (vt->index > 0)
+		return -EINVAL;
+
+	strlcpy(vt->name, "RF", sizeof(vt->name));
+	vt->type = V4L2_TUNER_RF;
+	vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
+	vt->rangelow = ctx->bands_rf->rangelow;
+	vt->rangehigh = ctx->bands_rf->rangehigh;
+	return 0;
+}
+
+static int max2175_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
+{
+	/* Check tuner index is valid */
+	if (vt->index > 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct v4l2_subdev_tuner_ops max2175_tuner_ops = {
+	.s_frequency = max2175_s_frequency,
+	.g_frequency = max2175_g_frequency,
+	.enum_freq_bands = max2175_enum_freq_bands,
+	.g_tuner = max2175_g_tuner,
+	.s_tuner = max2175_s_tuner,
+};
+
+static const struct v4l2_subdev_ops max2175_ops = {
+	.tuner = &max2175_tuner_ops,
+};
+
+static const struct v4l2_ctrl_ops max2175_ctrl_ops = {
+	.s_ctrl = max2175_s_ctrl,
+	.g_volatile_ctrl = max2175_g_volatile_ctrl,
+};
+
+static const struct v4l2_ctrl_config max2175_i2s_en = {
+	.ops = &max2175_ctrl_ops,
+	.id = V4L2_CID_MAX2175_I2S_ENABLE,
+	.name = "I2S Enable",
+	.type = V4L2_CTRL_TYPE_BOOLEAN,
+	.min = 0,
+	.max = 1,
+	.step = 1,
+	.def = 1,
+};
+
+/*
+ * HSLS value control LO freq adjacent location configuration.
+ * Refer to Documentation/media/v4l-drivers/max2175 for more details.
+ */
+static const struct v4l2_ctrl_config max2175_hsls = {
+	.ops = &max2175_ctrl_ops,
+	.id = V4L2_CID_MAX2175_HSLS,
+	.name = "HSLS above/below desired",
+	.type = V4L2_CTRL_TYPE_INTEGER,
+	.min = 0,
+	.max = 1,
+	.step = 1,
+	.def = 1,
+};
+
+/*
+ * Rx modes below are a set of preset configurations that decides the tuner's
+ * sck and sample rate of transmission. They are separate for EU & NA regions.
+ * Refer to Documentation/media/v4l-drivers/max2175 for more details.
+ */
+static const char * const max2175_ctrl_eu_rx_modes[] = {
+	[MAX2175_EU_FM_1_2]	= "EU FM 1.2",
+	[MAX2175_DAB_1_2]	= "DAB 1.2",
+};
+
+static const char * const max2175_ctrl_na_rx_modes[] = {
+	[MAX2175_NA_FM_1_0]	= "NA FM 1.0",
+	[MAX2175_NA_FM_2_0]	= "NA FM 2.0",
+};
+
+static const struct v4l2_ctrl_config max2175_eu_rx_mode = {
+	.ops = &max2175_ctrl_ops,
+	.id = V4L2_CID_MAX2175_RX_MODE,
+	.name = "RX MODE",
+	.type = V4L2_CTRL_TYPE_MENU,
+	.max = ARRAY_SIZE(max2175_ctrl_eu_rx_modes) - 1,
+	.def = 0,
+	.qmenu = max2175_ctrl_eu_rx_modes,
+};
+
+static const struct v4l2_ctrl_config max2175_na_rx_mode = {
+	.ops = &max2175_ctrl_ops,
+	.id = V4L2_CID_MAX2175_RX_MODE,
+	.name = "RX MODE",
+	.type = V4L2_CTRL_TYPE_MENU,
+	.max = ARRAY_SIZE(max2175_ctrl_na_rx_modes) - 1,
+	.def = 0,
+	.qmenu = max2175_ctrl_na_rx_modes,
+};
+
+static int max2175_refout_load_to_bits(struct i2c_client *client, u32 load,
+				       u32 *bits)
+{
+	if (load >= 0 && load <= 40)
+		*bits = load / 10;
+	else if (load >= 60 && load <= 70)
+		*bits = load / 10 - 1;
+	else
+		return -EINVAL;
+
+	return 0;
+}
+
+static int max2175_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	bool master = true, am_hiz = false;
+	u32 refout_load, refout_bits = 0;	/* REFOUT disabled */
+	struct v4l2_ctrl_handler *hdl;
+	struct device_node *np;
+	struct v4l2_subdev *sd;
+	struct regmap *regmap;
+	struct max2175 *ctx;
+	struct clk *clk;
+	int ret;
+
+	/* Parse DT properties */
+	np = of_parse_phandle(client->dev.of_node, "slave", 0);
+	if (np)
+		master = false;			/* Slave tuner */
+
+	if (of_find_property(client->dev.of_node, "maxim,am-hiz", NULL))
+		am_hiz = true;
+
+	if (!of_property_read_u32(client->dev.of_node, "maxim,refout-load-pF",
+				  &refout_load)) {
+		ret = max2175_refout_load_to_bits(client, refout_load,
+						  &refout_bits);
+		if (ret) {
+			dev_err(&client->dev, "invalid refout_load %u\n",
+				refout_load);
+			return -EINVAL;
+		}
+	}
+
+	clk = devm_clk_get(&client->dev, "xtal");
+	if (IS_ERR(clk)) {
+		ret = PTR_ERR(clk);
+		dev_err(&client->dev, "cannot get xtal clock %d\n", ret);
+		return -ENODEV;
+	}
+
+	regmap = devm_regmap_init_i2c(client, &max2175_regmap_config);
+	if (IS_ERR(regmap)) {
+		ret = PTR_ERR(regmap);
+		dev_err(&client->dev, "regmap init failed %d\n", ret);
+		return -ENODEV;
+	}
+
+	/* Alloc tuner context */
+	ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
+	if (ctx == NULL)
+		return -ENOMEM;
+
+	sd = &ctx->sd;
+	ctx->master = master;
+	ctx->am_hiz = am_hiz;
+	ctx->mode_resolved = false;
+	ctx->regmap = regmap;
+	ctx->xtal_freq = clk_get_rate(clk);
+	dev_info(&client->dev, "xtal freq %luHz\n", ctx->xtal_freq);
+
+	v4l2_i2c_subdev_init(sd, client, &max2175_ops);
+	ctx->client = client;
+
+	sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+
+	/* Controls */
+	hdl = &ctx->ctrl_hdl;
+	ret = v4l2_ctrl_handler_init(hdl, 7);
+	if (ret) {
+		dev_err(&client->dev, "ctrl handler init failed\n");
+		goto err;
+	}
+
+	ctx->lna_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
+					  V4L2_CID_RF_TUNER_LNA_GAIN,
+					  0, 63, 1, 0);
+	ctx->lna_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
+				 V4L2_CTRL_FLAG_READ_ONLY);
+	ctx->if_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
+					 V4L2_CID_RF_TUNER_IF_GAIN,
+					 0, 31, 1, 0);
+	ctx->if_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
+				V4L2_CTRL_FLAG_READ_ONLY);
+	ctx->pll_lock = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
+					  V4L2_CID_RF_TUNER_PLL_LOCK,
+					  0, 1, 1, 0);
+	ctx->pll_lock->flags |= (V4L2_CTRL_FLAG_VOLATILE |
+				 V4L2_CTRL_FLAG_READ_ONLY);
+	ctx->i2s_en = v4l2_ctrl_new_custom(hdl, &max2175_i2s_en, NULL);
+	ctx->hsls = v4l2_ctrl_new_custom(hdl, &max2175_hsls, NULL);
+
+	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
+		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
+						    &max2175_eu_rx_mode, NULL);
+		ctx->rx_modes = eu_rx_modes;
+		ctx->bands_rf = &eu_bands_rf;
+	} else {
+		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
+						    &max2175_na_rx_mode, NULL);
+		ctx->rx_modes = na_rx_modes;
+		ctx->bands_rf = &na_bands_rf;
+	}
+	ctx->sd.ctrl_handler = &ctx->ctrl_hdl;
+
+	/* Set the defaults */
+	ctx->freq = ctx->bands_rf->rangelow;
+
+	/* Register subdev */
+	ret = v4l2_async_register_subdev(sd);
+	if (ret) {
+		dev_err(&client->dev, "register subdev failed\n");
+		goto err_reg;
+	}
+
+	/* Initialize device */
+	ret = max2175_core_init(ctx, refout_bits);
+	if (ret)
+		goto err_init;
+
+	ret = v4l2_ctrl_handler_setup(hdl);
+	if (ret)
+		goto err_init;
+
+	dev_info(&client->dev, "probed\n");
+	return 0;
+
+err_init:
+	v4l2_async_unregister_subdev(sd);
+err_reg:
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+err:
+	return ret;
+}
+
+static int max2175_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct max2175 *ctx = max2175_from_sd(sd);
+
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+	v4l2_async_unregister_subdev(sd);
+	dev_info(&client->dev, "removed\n");
+	return 0;
+}
+
+static const struct i2c_device_id max2175_id[] = {
+	{ DRIVER_NAME, 0},
+	{},
+};
+MODULE_DEVICE_TABLE(i2c, max2175_id);
+
+static const struct of_device_id max2175_of_ids[] = {
+	{ .compatible = "maxim, max2175", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, max2175_of_ids);
+
+static struct i2c_driver max2175_driver = {
+	.driver = {
+		.name	= DRIVER_NAME,
+		.of_match_table = max2175_of_ids,
+	},
+	.probe		= max2175_probe,
+	.remove		= max2175_remove,
+	.id_table	= max2175_id,
+};
+
+module_i2c_driver(max2175_driver);
+
+MODULE_DESCRIPTION("Maxim MAX2175 RF to Bits tuner driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
diff --git a/drivers/media/i2c/max2175/max2175.h b/drivers/media/i2c/max2175/max2175.h
new file mode 100644
index 0000000..3c70f49
--- /dev/null
+++ b/drivers/media/i2c/max2175/max2175.h
@@ -0,0 +1,108 @@
+/*
+ * Maxim Integrated MAX2175 RF to Bits tuner driver
+ *
+ * This driver & most of the hard coded values are based on the reference
+ * application delivered by Maxim for this device.
+ *
+ * Copyright (C) 2016 Maxim Integrated Products
+ * Copyright (C) 2016 Renesas Electronics Corporation
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __MAX2175_H__
+#define __MAX2175_H__
+
+#define MAX2175_EU_XTAL_FREQ	36864000	/* In Hz */
+#define MAX2175_NA_XTAL_FREQ	40186125	/* In Hz */
+
+enum max2175_region {
+	MAX2175_REGION_EU = 0,	/* Europe */
+	MAX2175_REGION_NA,	/* North America */
+};
+
+
+enum max2175_band {
+	MAX2175_BAND_AM = 0,
+	MAX2175_BAND_FM,
+	MAX2175_BAND_VHF,
+	MAX2175_BAND_L,
+};
+
+enum max2175_eu_mode {
+	/* EU modes */
+	MAX2175_EU_FM_1_2 = 0,
+	MAX2175_DAB_1_2,
+
+	/* Other possible modes to add in future
+	 * MAX2175_DAB_1_0,
+	 * MAX2175_DAB_1_3,
+	 * MAX2175_EU_FM_2_2,
+	 * MAX2175_EU_FMHD_4_0,
+	 * MAX2175_EU_AM_1_0,
+	 * MAX2175_EU_AM_2_2,
+	 */
+};
+
+enum max2175_na_mode {
+	/* NA modes */
+	MAX2175_NA_FM_1_0 = 0,
+	MAX2175_NA_FM_2_0,
+
+	/* Other possible modes to add in future
+	 * MAX2175_NA_FMHD_1_0,
+	 * MAX2175_NA_FMHD_1_2,
+	 * MAX2175_NA_AM_1_0,
+	 * MAX2175_NA_AM_1_2,
+	 */
+};
+
+/* Supported I2S modes */
+enum {
+	MAX2175_I2S_MODE0 = 0,
+	MAX2175_I2S_MODE1,
+	MAX2175_I2S_MODE2,
+	MAX2175_I2S_MODE3,
+	MAX2175_I2S_MODE4,
+};
+
+/* Coefficient table groups */
+enum {
+	MAX2175_CH_MSEL = 0,
+	MAX2175_EQ_MSEL,
+	MAX2175_AA_MSEL,
+};
+
+/* HSLS LO injection polarity */
+enum {
+	MAX2175_LO_BELOW_DESIRED = 0,
+	MAX2175_LO_ABOVE_DESIRED,
+};
+
+/* Channel FSM modes */
+enum max2175_csm_mode {
+	MAX2175_LOAD_TO_BUFFER = 0,
+	MAX2175_PRESET_TUNE,
+	MAX2175_SEARCH,
+	MAX2175_AF_UPDATE,
+	MAX2175_JUMP_FAST_TUNE,
+	MAX2175_CHECK,
+	MAX2175_LOAD_AND_SWAP,
+	MAX2175_END,
+	MAX2175_BUFFER_PLUS_PRESET_TUNE,
+	MAX2175_BUFFER_PLUS_SEARCH,
+	MAX2175_BUFFER_PLUS_AF_UPDATE,
+	MAX2175_BUFFER_PLUS_JUMP_FAST_TUNE,
+	MAX2175_BUFFER_PLUS_CHECK,
+	MAX2175_BUFFER_PLUS_LOAD_AND_SWAP,
+	MAX2175_NO_ACTION
+};
+
+#endif /* __MAX2175_H__ */
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 4/7] media: Add new SDR formats PC16, PC18 & PC20
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
                   ` (2 preceding siblings ...)
  2017-02-07 15:02 ` [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support Ramesh Shanmugasundaram
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
  2017-02-07 15:02 ` [PATCH v3 5/7] doc_rst: media: New " Ramesh Shanmugasundaram
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

This patch adds support for the three new SDR formats. These formats
were prefixed with "planar" indicating I & Q data are not interleaved
as in other formats. Here, I & Q data constitutes the top half and bottom
half of the received buffer.

V4L2_SDR_FMT_PCU16BE - 14-bit complex (I & Q) unsigned big-endian sample
inside 16-bit. V4L2 FourCC: PC16

V4L2_SDR_FMT_PCU18BE - 16-bit complex (I & Q) unsigned big-endian sample
inside 18-bit. V4L2 FourCC: PC18

V4L2_SDR_FMT_PCU20BE - 18-bit complex (I & Q) unsigned big-endian sample
inside 20-bit. V4L2 FourCC: PC20

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 drivers/media/v4l2-core/v4l2-ioctl.c | 3 +++
 include/uapi/linux/videodev2.h       | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index 0c3f238..fdf6c913 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -1213,6 +1213,9 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 	case V4L2_SDR_FMT_CS8:		descr = "Complex S8"; break;
 	case V4L2_SDR_FMT_CS14LE:	descr = "Complex S14LE"; break;
 	case V4L2_SDR_FMT_RU12LE:	descr = "Real U12LE"; break;
+	case V4L2_SDR_FMT_PCU16BE:	descr = "Planar Complex U16BE"; break;
+	case V4L2_SDR_FMT_PCU18BE:	descr = "Planar Complex U18BE"; break;
+	case V4L2_SDR_FMT_PCU20BE:	descr = "Planar Complex U20BE"; break;
 	case V4L2_TCH_FMT_DELTA_TD16:	descr = "16-bit signed deltas"; break;
 	case V4L2_TCH_FMT_DELTA_TD08:	descr = "8-bit signed deltas"; break;
 	case V4L2_TCH_FMT_TU16:		descr = "16-bit unsigned touch data"; break;
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index 46e8a2e3..26a31c8 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -669,6 +669,9 @@ struct v4l2_pix_format {
 #define V4L2_SDR_FMT_CS8          v4l2_fourcc('C', 'S', '0', '8') /* complex s8 */
 #define V4L2_SDR_FMT_CS14LE       v4l2_fourcc('C', 'S', '1', '4') /* complex s14le */
 #define V4L2_SDR_FMT_RU12LE       v4l2_fourcc('R', 'U', '1', '2') /* real u12le */
+#define V4L2_SDR_FMT_PCU16BE	  v4l2_fourcc('P', 'C', '1', '6') /* planar complex u16be */
+#define V4L2_SDR_FMT_PCU18BE	  v4l2_fourcc('P', 'C', '1', '8') /* planar complex u18be */
+#define V4L2_SDR_FMT_PCU20BE	  v4l2_fourcc('P', 'C', '2', '0') /* planar complex u20be */
 
 /* Touch formats - used for Touch devices */
 #define V4L2_TCH_FMT_DELTA_TD16	v4l2_fourcc('T', 'D', '1', '6') /* 16-bit signed deltas */
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 5/7] doc_rst: media: New SDR formats PC16, PC18 & PC20
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
                   ` (3 preceding siblings ...)
  2017-02-07 15:02 ` [PATCH v3 4/7] media: Add new SDR formats PC16, PC18 & PC20 Ramesh Shanmugasundaram
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
  2017-04-11 12:39   ` Laurent Pinchart
  2017-02-07 15:02 ` [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding Ramesh Shanmugasundaram
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

This patch adds documentation for the three new SDR formats

V4L2_SDR_FMT_PCU16BE
V4L2_SDR_FMT_PCU18BE
V4L2_SDR_FMT_PCU20BE

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 .../media/uapi/v4l/pixfmt-sdr-pcu16be.rst          | 55 ++++++++++++++++++++++
 .../media/uapi/v4l/pixfmt-sdr-pcu18be.rst          | 55 ++++++++++++++++++++++
 .../media/uapi/v4l/pixfmt-sdr-pcu20be.rst          | 54 +++++++++++++++++++++
 Documentation/media/uapi/v4l/sdr-formats.rst       |  3 ++
 4 files changed, 167 insertions(+)
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst

diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
new file mode 100644
index 0000000..2de1b1a
--- /dev/null
+++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
@@ -0,0 +1,55 @@
+.. -*- coding: utf-8; mode: rst -*-
+
+.. _V4L2-SDR-FMT-PCU16BE:
+
+******************************
+V4L2_SDR_FMT_PCU16BE ('PC16')
+******************************
+
+Planar complex unsigned 16-bit big endian IQ sample
+
+Description
+===========
+
+This format contains a sequence of complex number samples. Each complex
+number consist of two parts called In-phase and Quadrature (IQ). Both I
+and Q are represented as a 16 bit unsigned big endian number stored in
+32 bit space. The remaining unused bits within the 32 bit space will be
+padded with 0. I value starts first and Q value starts at an offset
+equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
+the 16 bits, bit 15:2 (14 bit) is data and bit 1:0 (2 bit) can be any
+value.
+
+**Byte Order.**
+Each cell is one byte.
+
+.. flat-table::
+    :header-rows:  1
+    :stub-columns: 0
+
+    * -  Offset:
+      -  Byte B0
+      -  Byte B1
+      -  Byte B2
+      -  Byte B3
+    * -  start + 0:
+      -  I'\ :sub:`0[13:6]`
+      -  I'\ :sub:`0[5:0]; B1[1:0]=pad`
+      -  pad
+      -  pad
+    * -  start + 4:
+      -  I'\ :sub:`1[13:6]`
+      -  I'\ :sub:`1[5:0]; B1[1:0]=pad`
+      -  pad
+      -  pad
+    * -  ...
+    * - start + offset:
+      -  Q'\ :sub:`0[13:6]`
+      -  Q'\ :sub:`0[5:0]; B1[1:0]=pad`
+      -  pad
+      -  pad
+    * - start + offset + 4:
+      -  Q'\ :sub:`1[13:6]`
+      -  Q'\ :sub:`1[5:0]; B1[1:0]=pad`
+      -  pad
+      -  pad
diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
new file mode 100644
index 0000000..da8b26b
--- /dev/null
+++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
@@ -0,0 +1,55 @@
+.. -*- coding: utf-8; mode: rst -*-
+
+.. _V4L2-SDR-FMT-PCU18BE:
+
+******************************
+V4L2_SDR_FMT_PCU18BE ('PC18')
+******************************
+
+Planar complex unsigned 18-bit big endian IQ sample
+
+Description
+===========
+
+This format contains a sequence of complex number samples. Each complex
+number consist of two parts called In-phase and Quadrature (IQ). Both I
+and Q are represented as a 18 bit unsigned big endian number stored in
+32 bit space. The remaining unused bits within the 32 bit space will be
+padded with 0. I value starts first and Q value starts at an offset
+equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
+the 18 bits, bit 17:2 (16 bit) is data and bit 1:0 (2 bit) can be any
+value.
+
+**Byte Order.**
+Each cell is one byte.
+
+.. flat-table::
+    :header-rows:  1
+    :stub-columns: 0
+
+    * -  Offset:
+      -  Byte B0
+      -  Byte B1
+      -  Byte B2
+      -  Byte B3
+    * -  start + 0:
+      -  I'\ :sub:`0[17:10]`
+      -  I'\ :sub:`0[9:2]`
+      -  I'\ :sub:`0[1:0]; B2[5:0]=pad`
+      -  pad
+    * -  start + 4:
+      -  I'\ :sub:`1[17:10]`
+      -  I'\ :sub:`1[9:2]`
+      -  I'\ :sub:`1[1:0]; B2[5:0]=pad`
+      -  pad
+    * -  ...
+    * - start + offset:
+      -  Q'\ :sub:`0[17:10]`
+      -  Q'\ :sub:`0[9:2]`
+      -  Q'\ :sub:`0[1:0]; B2[5:0]=pad`
+      -  pad
+    * - start + offset + 4:
+      -  Q'\ :sub:`1[17:10]`
+      -  Q'\ :sub:`1[9:2]`
+      -  Q'\ :sub:`1[1:0]; B2[5:0]=pad`
+      -  pad
diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
new file mode 100644
index 0000000..5499eed
--- /dev/null
+++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
@@ -0,0 +1,54 @@
+.. -*- coding: utf-8; mode: rst -*-
+.. _V4L2-SDR-FMT-PCU20BE:
+
+******************************
+V4L2_SDR_FMT_PCU20BE ('PC20')
+******************************
+
+Planar complex unsigned 20-bit big endian IQ sample
+
+Description
+===========
+
+This format contains a sequence of complex number samples. Each complex
+number consist of two parts called In-phase and Quadrature (IQ). Both I
+and Q are represented as a 20 bit unsigned big endian number stored in
+32 bit space. The remaining unused bits within the 32 bit space will be
+padded with 0. I value starts first and Q value starts at an offset
+equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
+the 20 bits, bit 19:2 (18 bit) is data and bit 1:0 (2 bit) can be any
+value.
+
+**Byte Order.**
+Each cell is one byte.
+
+.. flat-table::
+    :header-rows:  1
+    :stub-columns: 0
+
+    * -  Offset:
+      -  Byte B0
+      -  Byte B1
+      -  Byte B2
+      -  Byte B3
+    * -  start + 0:
+      -  I'\ :sub:`0[19:12]`
+      -  I'\ :sub:`0[11:4]`
+      -  I'\ :sub:`0[3:0]; B2[3:0]=pad`
+      -  pad
+    * -  start + 4:
+      -  I'\ :sub:`1[19:12]`
+      -  I'\ :sub:`1[11:4]`
+      -  I'\ :sub:`1[3:0]; B2[3:0]=pad`
+      -  pad
+    * -  ...
+    * - start + offset:
+      -  Q'\ :sub:`0[19:12]`
+      -  Q'\ :sub:`0[11:4]`
+      -  Q'\ :sub:`0[3:0]; B2[3:0]=pad`
+      -  pad
+    * - start + offset + 4:
+      -  Q'\ :sub:`1[19:12]`
+      -  Q'\ :sub:`1[11:4]`
+      -  Q'\ :sub:`1[3:0]; B2[3:0]=pad`
+      -  pad
diff --git a/Documentation/media/uapi/v4l/sdr-formats.rst b/Documentation/media/uapi/v4l/sdr-formats.rst
index f863c08..2037f5b 100644
--- a/Documentation/media/uapi/v4l/sdr-formats.rst
+++ b/Documentation/media/uapi/v4l/sdr-formats.rst
@@ -17,3 +17,6 @@ These formats are used for :ref:`SDR <sdr>` interface only.
     pixfmt-sdr-cs08
     pixfmt-sdr-cs14le
     pixfmt-sdr-ru12le
+    pixfmt-sdr-pcu16be
+    pixfmt-sdr-pcu18be
+    pixfmt-sdr-pcu20be
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
                   ` (4 preceding siblings ...)
  2017-02-07 15:02 ` [PATCH v3 5/7] doc_rst: media: New " Ramesh Shanmugasundaram
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
       [not found]   ` <1486479757-32128-7-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
  2017-04-11 22:35   ` Laurent Pinchart
  2017-02-07 15:02 ` [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support Ramesh Shanmugasundaram
  2017-02-13 12:46 ` [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Hans Verkuil
  7 siblings, 2 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

Add binding documentation for Renesas R-Car Digital Radio Interface
(DRIF) controller.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 .../devicetree/bindings/media/renesas,drif.txt     | 186 +++++++++++++++++++++
 1 file changed, 186 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt

diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt b/Documentation/devicetree/bindings/media/renesas,drif.txt
new file mode 100644
index 0000000..6315609
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
@@ -0,0 +1,186 @@
+Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
+------------------------------------------------------------
+
+R-Car Gen3 DRIF is a SPI like receive only slave device. A general
+representation of DRIF interfacing with a master device is shown below.
+
++---------------------+                +---------------------+
+|                     |-----SCK------->|CLK                  |
+|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
+|                     |-----SD0------->|D0                   |
+|                     |-----SD1------->|D1                   |
++---------------------+                +---------------------+
+
+As per the datasheet, each DRIF channel (drifn) is made up of two internal
+channels (drifn0 & drifn1). These two internal channels share the common
+CLK & SYNC. Each internal channel has its own dedicated resources like
+irq, dma channels, address space & clock. This internal split is not
+visible to the external master device.
+
+The device tree model represents each internal channel as a separate node.
+The internal channels sharing the CLK & SYNC are tied together by their
+phandles using a new property called "renesas,bonding". For the rest of
+the documentation, unless explicitly stated, the word channel implies an
+internal channel.
+
+When both internal channels are enabled they need to be managed together
+as one (i.e.) they cannot operate alone as independent devices. Out of the
+two, one of them needs to act as a primary device that accepts common
+properties of both the internal channels. This channel is identified by a
+new property called "renesas,primary-bond".
+
+To summarize,
+   - When both the internal channels that are bonded together are enabled,
+     the zeroth channel is selected as primary-bond. This channels accepts
+     properties common to all the members of the bond.
+   - When only one of the bonded channels need to be enabled, the property
+     "renesas,bonding" or "renesas,primary-bond" will have no effect. That
+     enabled channel can act alone as any other independent device.
+
+Required properties of an internal channel:
+-------------------------------------------
+- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of R8A7795 SoC.
+	      "renesas,rcar-gen3-drif" 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
+	      followed by the generic version.
+- reg: offset and length of that channel.
+- interrupts: associated with that channel.
+- clocks: phandle and clock specifier of that channel.
+- clock-names: clock input name string: "fck".
+- dmas: phandles to the DMA channels.
+- dma-names: names of the DMA channel: "rx".
+- renesas,bonding: phandle to the other channel.
+
+Optional properties of an internal channel:
+-------------------------------------------
+- power-domains: phandle to the respective power domain.
+
+Required properties of an internal channel when:
+	- It is the only enabled channel of the bond (or)
+	- If it acts as primary among enabled bonds
+--------------------------------------------------------
+- pinctrl-0: pin control group to be used for this channel.
+- pinctrl-names: must be "default".
+- renesas,primary-bond: empty property indicating the channel acts as primary
+			among the bonded channels.
+- port: child port node of a channel that defines the local and remote
+	endpoints. The remote endpoint is assumed to be a third party tuner
+	device endpoint.
+
+Optional endpoint property:
+---------------------------
+- renesas,sync-active  : Indicates sync signal polarity, 0/1 for low/high
+			 respectively. This property maps to SYNCAC bit in the
+			 hardware manual. The default is 1 (active high)
+
+Example
+--------
+
+SoC common dtsi file
+
+		drif00: rif@e6f40000 {
+			compatible = "renesas,r8a7795-drif",
+				     "renesas,rcar-gen3-drif";
+			reg = <0 0xe6f40000 0 0x64>;
+			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&cpg CPG_MOD 515>;
+			clock-names = "fck";
+			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
+			dma-names = "rx", "rx";
+			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
+			renesas,bonding = <&drif01>;
+			status = "disabled";
+		};
+
+		drif01: rif@e6f50000 {
+			compatible = "renesas,r8a7795-drif",
+				     "renesas,rcar-gen3-drif";
+			reg = <0 0xe6f50000 0 0x64>;
+			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&cpg CPG_MOD 514>;
+			clock-names = "fck";
+			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
+			dma-names = "rx", "rx";
+			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
+			renesas,bonding = <&drif00>;
+			status = "disabled";
+		};
+
+
+Board specific dts file
+
+(1) Both internal channels enabled:
+-----------------------------------
+
+When interfacing with a third party tuner device with two data pins as shown
+below.
+
++---------------------+                +---------------------+
+|                     |-----SCK------->|CLK                  |
+|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
+|                     |-----SD0------->|D0                   |
+|                     |-----SD1------->|D1                   |
++---------------------+                +---------------------+
+
+pfc {
+	...
+
+	drif0_pins: drif0 {
+		groups = "drif0_ctrl_a", "drif0_data0_a",
+				 "drif0_data1_a";
+		function = "drif0";
+	};
+	...
+}
+
+&drif00 {
+	pinctrl-0 = <&drif0_pins>;
+	pinctrl-names = "default";
+	renesas,primary-bond;
+	status = "okay";
+	port {
+		drif0_ep: endpoint {
+		     remote-endpoint = <&tuner_ep>;
+		};
+	};
+};
+
+&drif01 {
+	status = "okay";
+};
+
+(2) Internal channel 1 alone is enabled:
+----------------------------------------
+
+When interfacing with a third party tuner device with one data pin as shown
+below.
+
++---------------------+                +---------------------+
+|                     |-----SCK------->|CLK                  |
+|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
+|                     |                |D0 (unused)          |
+|                     |-----SD-------->|D1                   |
++---------------------+                +---------------------+
+
+pfc {
+	...
+
+	drif0_pins: drif0 {
+		groups = "drif0_ctrl_a", "drif0_data1_a";
+		function = "drif0";
+	};
+	...
+}
+
+&drif01 {
+	pinctrl-0 = <&drif0_pins>;
+	pinctrl-names = "default";
+	status = "okay";
+	port {
+		drif0_ep: endpoint {
+		     remote-endpoint = <&tuner_ep>;
+		     renesas,syncac-active = <0>;
+		};
+	};
+};
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
                   ` (5 preceding siblings ...)
  2017-02-07 15:02 ` [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding Ramesh Shanmugasundaram
@ 2017-02-07 15:02 ` Ramesh Shanmugasundaram
       [not found]   ` <1486479757-32128-8-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
  2017-02-13 12:46 ` [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Hans Verkuil
  7 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-07 15:02 UTC (permalink / raw)
  To: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc, Ramesh Shanmugasundaram

This patch adds Digital Radio Interface (DRIF) support to R-Car Gen3 SoCs.
The driver exposes each instance of DRIF as a V4L2 SDR device. A DRIF
device represents a channel and each channel can have one or two
sub-channels respectively depending on the target board.

DRIF supports only Rx functionality. It receives samples from a RF
frontend tuner chip it is interfaced with. The combination of DRIF and the
tuner device, which is registered as a sub-device, determines the receive
sample rate and format.

In order to be compliant as a V4L2 SDR device, DRIF needs to bind with
the tuner device, which can be provided by a third party vendor. DRIF acts
as a slave device and the tuner device acts as a master transmitting the
samples. The driver allows asynchronous binding of a tuner device that
is registered as a v4l2 sub-device. The driver can learn about the tuner
it is interfaced with based on port endpoint properties of the device in
device tree. The V4L2 SDR device inherits the controls exposed by the
tuner device.

The device can also be configured to use either one or both of the data
pins at runtime based on the master (tuner) configuration.

Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
---
 drivers/media/platform/Kconfig     |   25 +
 drivers/media/platform/Makefile    |    1 +
 drivers/media/platform/rcar_drif.c | 1534 ++++++++++++++++++++++++++++++++++++
 3 files changed, 1560 insertions(+)
 create mode 100644 drivers/media/platform/rcar_drif.c

diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig
index 0245af0..bf40568 100644
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -435,3 +435,28 @@ menuconfig DVB_PLATFORM_DRIVERS
 if DVB_PLATFORM_DRIVERS
 source "drivers/media/platform/sti/c8sectpfe/Kconfig"
 endif #DVB_PLATFORM_DRIVERS
+
+menuconfig SDR_PLATFORM_DRIVERS
+	bool "SDR platform devices"
+	depends on MEDIA_SDR_SUPPORT
+	default n
+	---help---
+	  Say Y here to enable support for platform-specific SDR Drivers.
+
+if SDR_PLATFORM_DRIVERS
+
+config VIDEO_RCAR_DRIF
+	tristate "Renesas Digitial Radio Interface (DRIF)"
+	depends on VIDEO_V4L2 && HAS_DMA
+	depends on ARCH_RENESAS
+	select VIDEOBUF2_VMALLOC
+	---help---
+	  Say Y if you want to enable R-Car Gen3 DRIF support. DRIF is Digital
+	  Radio Interface that interfaces with an RF front end chip. It is a
+	  receiver of digital data which uses DMA to transfer received data to
+	  a configured location for an application to use.
+
+	  To compile this driver as a module, choose M here; the module
+	  will be called rcar_drif.
+
+endif # SDR_PLATFORM_DRIVERS
diff --git a/drivers/media/platform/Makefile b/drivers/media/platform/Makefile
index 5b3cb27..1c2daaf 100644
--- a/drivers/media/platform/Makefile
+++ b/drivers/media/platform/Makefile
@@ -47,6 +47,7 @@ obj-$(CONFIG_VIDEO_SH_VOU)		+= sh_vou.o
 
 obj-$(CONFIG_SOC_CAMERA)		+= soc_camera/
 
+obj-$(CONFIG_VIDEO_RCAR_DRIF)		+= rcar_drif.o
 obj-$(CONFIG_VIDEO_RENESAS_FCP) 	+= rcar-fcp.o
 obj-$(CONFIG_VIDEO_RENESAS_FDP1)	+= rcar_fdp1.o
 obj-$(CONFIG_VIDEO_RENESAS_JPU) 	+= rcar_jpu.o
diff --git a/drivers/media/platform/rcar_drif.c b/drivers/media/platform/rcar_drif.c
new file mode 100644
index 0000000..88950e3
--- /dev/null
+++ b/drivers/media/platform/rcar_drif.c
@@ -0,0 +1,1534 @@
+/*
+ * R-Car Gen3 Digital Radio Interface (DRIF) driver
+ *
+ * Copyright (C) 2016 Renesas Electronics Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * The R-Car DRIF is a receive only MSIOF like controller with an
+ * external master device driving the SCK. It receives data into a FIFO,
+ * then this driver uses the SYS-DMAC engine to move the data from
+ * the device to memory.
+ *
+ * Each DRIF channel DRIFx (as per datasheet) contains two internal
+ * channels DRIFx0 & DRIFx1 within itself with each having its own resources
+ * like module clk, register set, irq and dma. These internal channels share
+ * common CLK & SYNC from master. The two data pins D0 & D1 shall be
+ * considered to represent the two internal channels. This internal split
+ * is not visible to the master device.
+ *
+ * Depending on the master device, a DRIF channel can use
+ *  (1) both internal channels (D0 & D1) to receive data in parallel (or)
+ *  (2) one internal channel (D0 or D1) to receive data
+ *
+ * The primary design goal of this controller is to act as Digitial Radio
+ * Interface that receives digital samples from a tuner device. Hence the
+ * driver exposes the device as a V4L2 SDR device. In order to qualify as
+ * a V4L2 SDR device, it should possess tuner interface as mandated by the
+ * framework. This driver expects a tuner driver (sub-device) to bind
+ * asynchronously with this device and the combined drivers shall expose
+ * a V4L2 compliant SDR device. The DRIF driver is independent of the
+ * tuner vendor.
+ *
+ * The DRIF h/w can support I2S mode and Frame start synchronization pulse mode.
+ * This driver is tested for I2S mode only because of the availability of
+ * suitable master devices. Hence, not all configurable options of DRIF h/w
+ * like lsb/msb first, syncdl, dtdl etc. are exposed via DT and I2S defaults
+ * are used. These can be exposed later if needed after testing.
+ */
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/dma-mapping.h>
+#include <linux/dmaengine.h>
+#include <linux/ioctl.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/of_graph.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/sched.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fh.h>
+#include <media/v4l2-ioctl.h>
+#include <media/videobuf2-v4l2.h>
+#include <media/videobuf2-vmalloc.h>
+
+/* DRIF register offsets */
+#define RCAR_DRIF_SITMDR1			0x00
+#define RCAR_DRIF_SITMDR2			0x04
+#define RCAR_DRIF_SITMDR3			0x08
+#define RCAR_DRIF_SIRMDR1			0x10
+#define RCAR_DRIF_SIRMDR2			0x14
+#define RCAR_DRIF_SIRMDR3			0x18
+#define RCAR_DRIF_SICTR				0x28
+#define RCAR_DRIF_SIFCTR			0x30
+#define RCAR_DRIF_SISTR				0x40
+#define RCAR_DRIF_SIIER				0x44
+#define RCAR_DRIF_SIRFDR			0x60
+
+#define RCAR_DRIF_RFOVF			BIT(3)	/* Receive FIFO overflow */
+#define RCAR_DRIF_RFUDF			BIT(4)	/* Receive FIFO underflow */
+#define RCAR_DRIF_RFSERR		BIT(5)	/* Receive frame sync error */
+#define RCAR_DRIF_REOF			BIT(7)	/* Frame reception end */
+#define RCAR_DRIF_RDREQ			BIT(12) /* Receive data xfer req */
+#define RCAR_DRIF_RFFUL			BIT(13)	/* Receive FIFO full */
+
+/* SIRMDR1 */
+#define RCAR_DRIF_SIRMDR1_SYNCMD_FRAME		(0 << 28)
+#define RCAR_DRIF_SIRMDR1_SYNCMD_LR		(3 << 28)
+
+#define RCAR_DRIF_SIRMDR1_SYNCAC_POL_HIGH	(0 << 25)
+#define RCAR_DRIF_SIRMDR1_SYNCAC_POL_LOW	(1 << 25)
+
+#define RCAR_DRIF_SIRMDR1_MSB_FIRST		(0 << 24)
+#define RCAR_DRIF_SIRMDR1_LSB_FIRST		(1 << 24)
+
+#define RCAR_DRIF_SIRMDR1_DTDL_0		(0 << 20)
+#define RCAR_DRIF_SIRMDR1_DTDL_1		(1 << 20)
+#define RCAR_DRIF_SIRMDR1_DTDL_2		(2 << 20)
+#define RCAR_DRIF_SIRMDR1_DTDL_0PT5		(5 << 20)
+#define RCAR_DRIF_SIRMDR1_DTDL_1PT5		(6 << 20)
+
+#define RCAR_DRIF_SIRMDR1_SYNCDL_0		(0 << 20)
+#define RCAR_DRIF_SIRMDR1_SYNCDL_1		(1 << 20)
+#define RCAR_DRIF_SIRMDR1_SYNCDL_2		(2 << 20)
+#define RCAR_DRIF_SIRMDR1_SYNCDL_3		(3 << 20)
+#define RCAR_DRIF_SIRMDR1_SYNCDL_0PT5		(5 << 20)
+#define RCAR_DRIF_SIRMDR1_SYNCDL_1PT5		(6 << 20)
+
+#define RCAR_DRIF_MDR_GRPCNT(n)			(((n) - 1) << 30)
+#define RCAR_DRIF_MDR_BITLEN(n)			(((n) - 1) << 24)
+#define RCAR_DRIF_MDR_WDCNT(n)			(((n) - 1) << 16)
+
+/* Hidden Transmit register that controls CLK & SYNC */
+#define RCAR_DRIF_SITMDR1_PCON			BIT(30)
+
+#define RCAR_DRIF_SICTR_RX_RISING_EDGE		BIT(26)
+#define RCAR_DRIF_SICTR_RX_EN			BIT(8)
+#define RCAR_DRIF_SICTR_RESET			BIT(0)
+
+/* Constants */
+#define RCAR_DRIF_MAX_NUM_HWBUFS		32
+#define RCAR_DRIF_MAX_DEVS			4
+#define RCAR_DRIF_DEFAULT_NUM_HWBUFS		16
+#define RCAR_DRIF_DEFAULT_HWBUF_SIZE		(4 * PAGE_SIZE)
+#define RCAR_DRIF_MAX_CHANNEL			2
+#define RCAR_SDR_BUFFER_SIZE			SZ_64K
+
+/* Internal buffer status flags */
+#define RCAR_DRIF_BUF_DONE			BIT(0)	/* DMA completed */
+#define RCAR_DRIF_BUF_OVERFLOW			BIT(1)	/* Overflow detected */
+
+#define to_rcar_drif_buf_pair(sdr, ch_num, idx)	(sdr->ch[!(ch_num)]->buf[idx])
+
+#define for_each_rcar_drif_channel(ch, ch_mask)			\
+	for_each_set_bit(ch, ch_mask, RCAR_DRIF_MAX_CHANNEL)
+
+static const unsigned int num_hwbufs = 32;
+
+/* Debug */
+static unsigned int debug;
+module_param(debug, uint, 0644);
+MODULE_PARM_DESC(debug, "activate debug info");
+
+#define rdrif_dbg(level, sdr, fmt, arg...)				\
+	v4l2_dbg(level, debug, &sdr->v4l2_dev, fmt, ## arg)
+
+#define rdrif_err(sdr, fmt, arg...)					\
+	dev_err(sdr->v4l2_dev.dev, fmt, ## arg)
+
+/* Stream formats */
+struct rcar_drif_format {
+	u32	pixelformat;
+	u32	buffersize;
+	u32	wdlen;
+	u32	num_ch;
+};
+
+/* Format descriptions for capture */
+static const struct rcar_drif_format formats[] = {
+	{
+		.pixelformat	= V4L2_SDR_FMT_PCU16BE,
+		.buffersize	= RCAR_SDR_BUFFER_SIZE,
+		.wdlen		= 16,
+		.num_ch	= 2,
+	},
+	{
+		.pixelformat	= V4L2_SDR_FMT_PCU18BE,
+		.buffersize	= RCAR_SDR_BUFFER_SIZE,
+		.wdlen		= 18,
+		.num_ch	= 2,
+	},
+	{
+		.pixelformat	= V4L2_SDR_FMT_PCU20BE,
+		.buffersize	= RCAR_SDR_BUFFER_SIZE,
+		.wdlen		= 20,
+		.num_ch	= 2,
+	},
+};
+
+static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
+
+/* Buffer for a received frame from one or both internal channels */
+struct rcar_drif_frame_buf {
+	/* Common v4l buffer stuff -- must be first */
+	struct vb2_v4l2_buffer vb;
+	struct list_head list;
+};
+
+struct rcar_drif_async_subdev {
+	struct v4l2_subdev *sd;
+	struct v4l2_async_subdev asd;
+};
+
+/* DMA buffer */
+struct rcar_drif_hwbuf {
+	void *addr;			/* CPU-side address */
+	unsigned int status;		/* Buffer status flags */
+};
+
+/* Internal channel */
+struct rcar_drif {
+	struct rcar_drif_sdr *sdr;	/* Group device */
+	struct platform_device *pdev;	/* Channel's pdev */
+	void __iomem *base;		/* Base register address */
+	resource_size_t start;		/* I/O resource offset */
+	struct dma_chan *dmach;		/* Reserved DMA channel */
+	struct clk *clkp;		/* Module clock */
+	struct rcar_drif_hwbuf *buf[RCAR_DRIF_MAX_NUM_HWBUFS]; /* H/W bufs */
+	dma_addr_t dma_handle;		/* Handle for all bufs */
+	unsigned int num;		/* Channel number */
+	bool acting_sdr;		/* Channel acting as SDR device */
+};
+
+/* DRIF V4L2 SDR */
+struct rcar_drif_sdr {
+	struct device *dev;		/* Platform device */
+	struct video_device *vdev;	/* V4L2 SDR device */
+	struct v4l2_device v4l2_dev;	/* V4L2 device */
+
+	/* Videobuf2 queue and queued buffers list */
+	struct vb2_queue vb_queue;
+	struct list_head queued_bufs;
+	spinlock_t queued_bufs_lock;	/* Protects queued_bufs */
+
+	struct mutex v4l2_mutex;	/* To serialize ioctls */
+	struct mutex vb_queue_mutex;	/* To serialize streaming ioctls */
+	struct v4l2_ctrl_handler ctrl_hdl;	/* SDR control handler */
+	struct v4l2_async_notifier notifier;	/* For subdev (tuner) */
+
+	/* Current V4L2 SDR format array index */
+	unsigned int fmt_idx;
+
+	/* Device tree SYNC properties */
+	u32 mdr1;
+
+	/* Internals */
+	struct rcar_drif *ch[RCAR_DRIF_MAX_CHANNEL]; /* DRIFx0,1 */
+	unsigned long hw_ch_mask;	/* Enabled channels per DT */
+	unsigned long cur_ch_mask;	/* Used channels for an SDR FMT */
+	u32 num_hw_ch;			/* Num of DT enabled channels */
+	u32 num_cur_ch;			/* Num of used channels */
+	u32 hwbuf_size;			/* Each DMA buffer size */
+	u32 produced;			/* Buffers produced by sdr dev */
+};
+
+/* Allocate buffer context */
+static int rcar_drif_alloc_bufctxt(struct rcar_drif_sdr *sdr)
+{
+	struct rcar_drif_hwbuf *bufctx;
+	unsigned int i, idx;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		bufctx = kcalloc(num_hwbufs, sizeof(*bufctx), GFP_KERNEL);
+		if (!bufctx)
+			return -ENOMEM;
+
+		for (idx = 0; idx < num_hwbufs; idx++)
+			sdr->ch[i]->buf[idx] = bufctx + idx;
+	}
+	return 0;
+}
+
+/* Release buffer context */
+static void rcar_drif_release_bufctxt(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		kfree(sdr->ch[i]->buf[0]);
+		sdr->ch[i]->buf[0] = NULL;
+	}
+}
+
+/* Release DMA channel */
+static void rcar_drif_release_dmachannel(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
+		if (sdr->ch[i]->dmach) {
+			dma_release_channel(sdr->ch[i]->dmach);
+			sdr->ch[i]->dmach = NULL;
+		}
+}
+
+/* Allocate DMA channel */
+static int rcar_drif_alloc_dmachannel(struct rcar_drif_sdr *sdr)
+{
+	struct dma_slave_config dma_cfg;
+	unsigned int i;
+	int ret = -ENODEV;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		struct rcar_drif *ch = sdr->ch[i];
+
+		ch->dmach = dma_request_slave_channel(&ch->pdev->dev, "rx");
+		if (!ch->dmach) {
+			rdrif_err(sdr, "ch%u: dma channel req failed\n", i);
+			goto dmach_error;
+		}
+
+		/* Configure slave */
+		memset(&dma_cfg, 0, sizeof(dma_cfg));
+		dma_cfg.src_addr = (phys_addr_t)(ch->start + RCAR_DRIF_SIRFDR);
+		dma_cfg.dst_addr = 0;
+		dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		ret = dmaengine_slave_config(ch->dmach, &dma_cfg);
+		if (ret) {
+			rdrif_err(sdr, "ch%u: dma slave config failed\n", i);
+			goto dmach_error;
+		}
+	}
+	return 0;
+
+dmach_error:
+	rcar_drif_release_dmachannel(sdr);
+	return ret;
+}
+
+/* Release queued vb2 buffers */
+static void rcar_drif_release_queued_bufs(struct rcar_drif_sdr *sdr,
+					  enum vb2_buffer_state state)
+{
+	struct rcar_drif_frame_buf *fbuf, *tmp;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
+	list_for_each_entry_safe(fbuf, tmp, &sdr->queued_bufs, list) {
+		list_del(&fbuf->list);
+		vb2_buffer_done(&fbuf->vb.vb2_buf, state);
+	}
+	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
+}
+
+/* Set MDR defaults */
+static inline void rcar_drif_set_mdr1(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	/* Set defaults for enabled internal channels */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		/* Refer MSIOF section in manual for this register setting */
+		writel(RCAR_DRIF_SITMDR1_PCON,
+		       sdr->ch[i]->base + RCAR_DRIF_SITMDR1);
+
+		/* Setup MDR1 value */
+		writel(sdr->mdr1, sdr->ch[i]->base + RCAR_DRIF_SIRMDR1);
+
+		rdrif_dbg(2, sdr, "ch%u: mdr1 = 0x%08x",
+			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR1));
+	}
+}
+
+/* Extract bitlen and wdcnt from given word length */
+static int rcar_drif_convert_wdlen(struct rcar_drif_sdr *sdr,
+				   u32 wdlen, u32 *bitlen, u32 *wdcnt)
+{
+	unsigned int i, nr_wds;
+
+	/* FIFO register size is 32 bits */
+	for (i = 0; i < 32; i++) {
+		nr_wds = wdlen % (32 - i);
+		if (nr_wds == 0) {
+			*bitlen = 32 - i;
+			*wdcnt = wdlen / *bitlen;
+			break;
+		}
+	}
+
+	/* Sanity check range */
+	if (i == 32 || !(*bitlen >= 8 && *bitlen <= 32) ||
+	    !(*wdcnt >= 1 && *wdcnt <= 64)) {
+		rdrif_err(sdr, "invalid wdlen %u configured\n", wdlen);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/* Set DRIF receive format */
+static int rcar_drif_set_format(struct rcar_drif_sdr *sdr)
+{
+	u32 bitlen, wdcnt, wdlen;
+	unsigned int i;
+	int ret = -EINVAL;
+
+	wdlen = formats[sdr->fmt_idx].wdlen;
+	rdrif_dbg(2, sdr, "setfmt: idx %u, wdlen %u, num_ch %u\n",
+		  sdr->fmt_idx, wdlen, formats[sdr->fmt_idx].num_ch);
+
+	/* Sanity check */
+	if (formats[sdr->fmt_idx].num_ch > sdr->num_cur_ch) {
+		rdrif_err(sdr, "fmt idx %u current ch %u mismatch\n",
+			  sdr->fmt_idx, sdr->num_cur_ch);
+		return ret;
+	}
+
+	/* Get bitlen & wdcnt from wdlen */
+	ret = rcar_drif_convert_wdlen(sdr, wdlen, &bitlen, &wdcnt);
+	if (ret)
+		return ret;
+
+	/* Setup group, bitlen & wdcnt */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		u32 mdr;
+
+		/* Two groups */
+		mdr = RCAR_DRIF_MDR_GRPCNT(2) | RCAR_DRIF_MDR_BITLEN(bitlen) |
+		       RCAR_DRIF_MDR_WDCNT(wdcnt);
+		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR2);
+
+		mdr = RCAR_DRIF_MDR_BITLEN(bitlen) | RCAR_DRIF_MDR_WDCNT(wdcnt);
+		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR3);
+
+		rdrif_dbg(2, sdr, "ch%u: new mdr[2,3] = 0x%08x, 0x%08x\n",
+			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR2),
+			  readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR3));
+	}
+	return ret;
+}
+
+/* Release DMA buffers */
+static void rcar_drif_release_buf(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		struct rcar_drif *ch = sdr->ch[i];
+
+		/* First entry contains the dma buf ptr */
+		if (ch->buf[0] && ch->buf[0]->addr) {
+			dma_free_coherent(&ch->pdev->dev,
+					  sdr->hwbuf_size * num_hwbufs,
+					  ch->buf[0]->addr, ch->dma_handle);
+			ch->buf[0]->addr = NULL;
+		}
+	}
+}
+
+/* Request DMA buffers */
+static int rcar_drif_request_buf(struct rcar_drif_sdr *sdr)
+{
+	int ret = -ENOMEM;
+	unsigned int i, j;
+	void *addr;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		struct rcar_drif *ch = sdr->ch[i];
+
+		/* Allocate DMA buffers */
+		addr = dma_alloc_coherent(&ch->pdev->dev,
+					  sdr->hwbuf_size * num_hwbufs,
+					  &ch->dma_handle, GFP_KERNEL);
+		if (!addr) {
+			rdrif_err(sdr,
+			"ch%u: dma alloc failed. num_hwbufs %u size %u\n",
+			i, num_hwbufs, sdr->hwbuf_size);
+			goto alloc_error;
+		}
+
+		/* Split the chunk and populate bufctxt */
+		for (j = 0; j < num_hwbufs; j++) {
+			ch->buf[j]->addr = addr + (j * sdr->hwbuf_size);
+			ch->buf[j]->status = 0;
+		}
+	}
+
+	return 0;
+
+alloc_error:
+	return ret;
+}
+
+/* Setup vb_queue minimum buffer requirements */
+static int rcar_drif_queue_setup(struct vb2_queue *vq,
+			unsigned int *num_buffers, unsigned int *num_planes,
+			unsigned int sizes[], struct device *alloc_devs[])
+{
+	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
+
+	/* Need at least 16 buffers */
+	if (vq->num_buffers + *num_buffers < 16)
+		*num_buffers = 16 - vq->num_buffers;
+
+	*num_planes = 1;
+	sizes[0] = PAGE_ALIGN(formats[sdr->fmt_idx].buffersize);
+
+	rdrif_dbg(2, sdr, "num_bufs %d sizes[0] %d\n", *num_buffers, sizes[0]);
+	return 0;
+}
+
+/* Enqueue buffer */
+static void rcar_drif_buf_queue(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vb->vb2_queue);
+	struct rcar_drif_frame_buf *fbuf =
+			container_of(vbuf, struct rcar_drif_frame_buf, vb);
+	unsigned long flags;
+
+	rdrif_dbg(2, sdr, "buf_queue idx %u\n", vb->index);
+	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
+	list_add_tail(&fbuf->list, &sdr->queued_bufs);
+	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
+}
+
+/* Get a frame buf from list */
+static struct rcar_drif_frame_buf *
+rcar_drif_get_fbuf(struct rcar_drif_sdr *sdr)
+{
+	struct rcar_drif_frame_buf *fbuf;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
+	fbuf = list_first_entry_or_null(&sdr->queued_bufs, struct
+					rcar_drif_frame_buf, list);
+	if (!fbuf) {
+		/*
+		 * App is late in enqueing buffers. Samples lost & there will
+		 * be a gap in sequence number when app recovers
+		 */
+		rdrif_dbg(1, sdr, "\napp late: prod %u\n", sdr->produced);
+		sdr->produced++; /* Increment the produced count anyway */
+		spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
+		return NULL;
+	}
+	list_del(&fbuf->list);
+	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
+
+	return fbuf;
+}
+
+static inline bool rcar_drif_buf_pairs_done(struct rcar_drif_hwbuf *buf1,
+					    struct rcar_drif_hwbuf *buf2)
+{
+	return (buf1->status & buf2->status & RCAR_DRIF_BUF_DONE);
+}
+
+/* Channel DMA complete */
+static void rcar_drif_channel_complete(struct rcar_drif *ch, u32 idx)
+{
+	u32 str;
+
+	ch->buf[idx]->status |= RCAR_DRIF_BUF_DONE;
+
+	/* Check for DRIF errors */
+	str = readl(ch->base + RCAR_DRIF_SISTR);
+	if (unlikely(str & RCAR_DRIF_RFOVF)) {
+		/* Writing the same clears it */
+		writel(str, ch->base + RCAR_DRIF_SISTR);
+
+		/* Overflow: some samples are lost */
+		ch->buf[idx]->status |= RCAR_DRIF_BUF_OVERFLOW;
+	}
+}
+
+/* Deliver buffer to user */
+static void rcar_drif_deliver_buf(struct rcar_drif *ch)
+{
+	struct rcar_drif_sdr *sdr = ch->sdr;
+	u32 idx = sdr->produced % num_hwbufs;
+	struct rcar_drif_frame_buf *fbuf;
+	bool overflow = false;
+
+	rcar_drif_channel_complete(ch, idx);
+
+	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL) {
+		struct rcar_drif_hwbuf *bufi, *bufq;
+
+		if (ch->num) {
+			bufi = to_rcar_drif_buf_pair(sdr, ch->num, idx);
+			bufq = ch->buf[idx];
+		} else {
+			bufi = ch->buf[idx];
+			bufq = to_rcar_drif_buf_pair(sdr, ch->num, idx);
+		}
+
+		/* Check if both DMA buffers are done */
+		if (!rcar_drif_buf_pairs_done(bufi, bufq))
+			return;
+
+		/* Clear buf done status */
+		bufi->status &= ~RCAR_DRIF_BUF_DONE;
+		bufq->status &= ~RCAR_DRIF_BUF_DONE;
+
+		/* Get fbuf */
+		fbuf = rcar_drif_get_fbuf(sdr);
+		if (!fbuf)
+			return;
+
+		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
+		       bufi->addr, sdr->hwbuf_size);
+		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0) + sdr->hwbuf_size,
+		       bufq->addr, sdr->hwbuf_size);
+
+		if ((bufi->status | bufq->status) & RCAR_DRIF_BUF_OVERFLOW) {
+			overflow = true;
+			/* Clear the flag in status */
+			bufi->status &= ~RCAR_DRIF_BUF_OVERFLOW;
+			bufq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
+		}
+	} else {
+		struct rcar_drif_hwbuf *bufiq;
+
+		/* Get fbuf */
+		fbuf = rcar_drif_get_fbuf(sdr);
+		if (!fbuf)
+			return;
+
+		bufiq = ch->buf[idx];
+
+		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
+		       bufiq->addr, sdr->hwbuf_size);
+
+		if (bufiq->status & RCAR_DRIF_BUF_OVERFLOW) {
+			overflow = true;
+			/* Clear the flag in status */
+			bufiq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
+		}
+	}
+
+	rdrif_dbg(2, sdr, "ch%u: prod %u\n", ch->num, sdr->produced);
+
+	fbuf->vb.field = V4L2_FIELD_NONE;
+	fbuf->vb.sequence = sdr->produced++;
+	fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
+	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
+			      formats[sdr->fmt_idx].buffersize);
+
+	/* Set error state on overflow */
+	if (overflow)
+		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+	else
+		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
+}
+
+/* DMA callback for each stage */
+static void rcar_drif_dma_complete(void *dma_async_param)
+{
+	struct rcar_drif *ch = dma_async_param;
+	struct rcar_drif_sdr *sdr = ch->sdr;
+
+	mutex_lock(&sdr->vb_queue_mutex);
+
+	/* DMA can be terminated while the callback was waiting on lock */
+	if (!vb2_is_streaming(&sdr->vb_queue))
+		goto stopped;
+
+	rcar_drif_deliver_buf(ch);
+stopped:
+	mutex_unlock(&sdr->vb_queue_mutex);
+}
+
+static int rcar_drif_qbuf(struct rcar_drif *ch)
+{
+	struct rcar_drif_sdr *sdr = ch->sdr;
+	dma_addr_t addr = ch->dma_handle;
+	struct dma_async_tx_descriptor *rxd;
+	dma_cookie_t cookie;
+	int ret = -EIO;
+
+	/* Setup cyclic DMA with given buffers */
+	rxd = dmaengine_prep_dma_cyclic(ch->dmach, addr,
+					sdr->hwbuf_size * num_hwbufs,
+					sdr->hwbuf_size, DMA_DEV_TO_MEM,
+					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	if (!rxd) {
+		rdrif_err(sdr, "ch%u: prep dma cyclic failed\n", ch->num);
+		return ret;
+	}
+
+	/* Submit descriptor */
+	rxd->callback = rcar_drif_dma_complete;
+	rxd->callback_param = ch;
+	cookie = dmaengine_submit(rxd);
+	if (dma_submit_error(cookie)) {
+		rdrif_err(sdr, "ch%u: dma submit failed\n", ch->num);
+		return ret;
+	}
+
+	dma_async_issue_pending(ch->dmach);
+	return 0;
+}
+
+/* Enable reception */
+static int rcar_drif_enable_rx(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+	u32 ctr;
+	int ret;
+
+	/*
+	 * When both internal channels are enabled, they can be synchronized
+	 * only by the master
+	 */
+
+	/* Enable receive */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
+		ctr |= (RCAR_DRIF_SICTR_RX_RISING_EDGE |
+			 RCAR_DRIF_SICTR_RX_EN);
+		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
+	}
+
+	/* Check receive enabled */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
+					 ctr, ctr & RCAR_DRIF_SICTR_RX_EN,
+					 2, 500000);
+		if (ret) {
+			rdrif_err(sdr, "ch%u: rx en failed. ctr 0x%08x\n",
+				  i, readl(sdr->ch[i]->base + RCAR_DRIF_SICTR));
+			break;
+		}
+	}
+	return ret;
+}
+
+/* Disable reception */
+static void rcar_drif_disable_rx(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+	u32 ctr;
+	int ret;
+
+	/* Disable receive */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
+		ctr &= ~RCAR_DRIF_SICTR_RX_EN;
+		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
+	}
+
+	/* Check receive disabled */
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
+					 ctr, !(ctr & RCAR_DRIF_SICTR_RX_EN),
+					 2, 500000);
+		if (ret)
+			dev_warn(&sdr->vdev->dev,
+			"ch%u: failed to disable rx. ctr 0x%08x\n",
+			i, readl(sdr->ch[i]->base + RCAR_DRIF_SICTR));
+	}
+}
+
+/* Start channel */
+static int rcar_drif_start_channel(struct rcar_drif *ch)
+{
+	struct rcar_drif_sdr *sdr = ch->sdr;
+	u32 ctr, str;
+	int ret;
+
+	/* Reset receive */
+	writel(RCAR_DRIF_SICTR_RESET, ch->base + RCAR_DRIF_SICTR);
+	ret = readl_poll_timeout(ch->base + RCAR_DRIF_SICTR,
+					 ctr, !(ctr & RCAR_DRIF_SICTR_RESET),
+					 2, 500000);
+	if (ret) {
+		rdrif_err(sdr, "ch%u: failed to reset rx. ctr 0x%08x\n",
+			  ch->num, readl(ch->base + RCAR_DRIF_SICTR));
+		return ret;
+	}
+
+	/* Queue buffers for DMA */
+	ret = rcar_drif_qbuf(ch);
+	if (ret)
+		return ret;
+
+	/* Clear status register flags */
+	str = RCAR_DRIF_RFFUL | RCAR_DRIF_REOF | RCAR_DRIF_RFSERR |
+		RCAR_DRIF_RFUDF | RCAR_DRIF_RFOVF;
+	writel(str, ch->base + RCAR_DRIF_SISTR);
+
+	/* Enable DMA receive interrupt */
+	writel(0x00009000, ch->base + RCAR_DRIF_SIIER);
+
+	return ret;
+}
+
+/* Start receive operation */
+static int rcar_drif_start(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+	int ret;
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ret = rcar_drif_start_channel(sdr->ch[i]);
+		if (ret)
+			goto start_error;
+	}
+
+	sdr->produced = 0;
+	ret = rcar_drif_enable_rx(sdr);
+start_error:
+	return ret;
+}
+
+/* Stop channel */
+static void rcar_drif_stop_channel(struct rcar_drif *ch)
+{
+	struct rcar_drif_sdr *sdr = ch->sdr;
+	int ret, retries = 3;
+
+	/* Disable DMA receive interrupt */
+	writel(0x00000000, ch->base + RCAR_DRIF_SIIER);
+
+	do {
+		/* Terminate all DMA transfers */
+		ret = dmaengine_terminate_sync(ch->dmach);
+		if (!ret)
+			break;
+		rdrif_dbg(2, sdr, "stop retry\n");
+	} while (--retries);
+
+	WARN_ON(!retries);
+}
+
+/* Stop receive operation */
+static void rcar_drif_stop(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	/* Disable Rx */
+	rcar_drif_disable_rx(sdr);
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
+		rcar_drif_stop_channel(sdr->ch[i]);
+}
+
+/* Start streaming */
+static int rcar_drif_start_streaming(struct vb2_queue *vq, unsigned int count)
+{
+	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
+	unsigned int i, j;
+	int ret;
+
+	mutex_lock(&sdr->v4l2_mutex);
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
+		ret = clk_prepare_enable(sdr->ch[i]->clkp);
+		if (ret)
+			goto start_error;
+	}
+
+	/* Set default MDRx settings */
+	rcar_drif_set_mdr1(sdr);
+
+	/* Set new format */
+	ret = rcar_drif_set_format(sdr);
+	if (ret)
+		goto start_error;
+
+	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL)
+		sdr->hwbuf_size =
+		formats[sdr->fmt_idx].buffersize / RCAR_DRIF_MAX_CHANNEL;
+	else
+		sdr->hwbuf_size = formats[sdr->fmt_idx].buffersize;
+
+	rdrif_dbg(1, sdr, "num_hwbufs %u, hwbuf_size %u\n",
+		num_hwbufs, sdr->hwbuf_size);
+
+	/* Alloc DMA channel */
+	ret = rcar_drif_alloc_dmachannel(sdr);
+	if (ret)
+		goto start_error;
+
+	/* Alloc buf context */
+	ret = rcar_drif_alloc_bufctxt(sdr);
+	if (ret)
+		goto start_error;
+
+	/* Request buffers */
+	ret = rcar_drif_request_buf(sdr);
+	if (ret)
+		goto start_error;
+
+	/* Start Rx */
+	ret = rcar_drif_start(sdr);
+	if (ret)
+		goto start_error;
+
+	mutex_unlock(&sdr->v4l2_mutex);
+	rdrif_dbg(1, sdr, "started\n");
+	return ret;
+
+start_error:
+	rcar_drif_release_queued_bufs(sdr, VB2_BUF_STATE_QUEUED);
+	rcar_drif_release_buf(sdr);
+	rcar_drif_release_bufctxt(sdr);
+	rcar_drif_release_dmachannel(sdr);
+	for (j = 0; j < i; j++)
+		clk_disable_unprepare(sdr->ch[j]->clkp);
+
+	mutex_unlock(&sdr->v4l2_mutex);
+	return ret;
+}
+
+/* Stop streaming */
+static void rcar_drif_stop_streaming(struct vb2_queue *vq)
+{
+	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
+	unsigned int i;
+
+	mutex_lock(&sdr->v4l2_mutex);
+
+	/* Stop hardware streaming */
+	rcar_drif_stop(sdr);
+
+	/* Return all queued buffers to vb2 */
+	rcar_drif_release_queued_bufs(sdr, VB2_BUF_STATE_ERROR);
+
+	/* Release buf & buf context */
+	rcar_drif_release_buf(sdr);
+	rcar_drif_release_bufctxt(sdr);
+
+	/* Release DMA channel resources */
+	rcar_drif_release_dmachannel(sdr);
+
+	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
+		clk_disable_unprepare(sdr->ch[i]->clkp);
+
+	mutex_unlock(&sdr->v4l2_mutex);
+	rdrif_dbg(1, sdr, "stopped: prod %u\n", sdr->produced);
+}
+
+/* Vb2 ops */
+static struct vb2_ops rcar_drif_vb2_ops = {
+	.queue_setup            = rcar_drif_queue_setup,
+	.buf_queue              = rcar_drif_buf_queue,
+	.start_streaming        = rcar_drif_start_streaming,
+	.stop_streaming         = rcar_drif_stop_streaming,
+	.wait_prepare		= vb2_ops_wait_prepare,
+	.wait_finish		= vb2_ops_wait_finish,
+};
+
+static int rcar_drif_querycap(struct file *file, void *fh,
+			      struct v4l2_capability *cap)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+
+	strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
+	strlcpy(cap->card, sdr->vdev->name, sizeof(cap->card));
+	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
+		 sdr->vdev->name);
+	return 0;
+}
+
+static int rcar_drif_set_default_format(struct rcar_drif_sdr *sdr)
+{
+	unsigned int i;
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		/* Matching fmt based on required channels is set as default */
+		if (sdr->num_hw_ch == formats[i].num_ch) {
+			sdr->fmt_idx = i;
+			sdr->cur_ch_mask = sdr->hw_ch_mask;
+			sdr->num_cur_ch = sdr->num_hw_ch;
+			dev_dbg(sdr->dev, "default fmt[%u]: mask %lu num %u\n",
+				i, sdr->cur_ch_mask, sdr->num_cur_ch);
+			return 0;
+		}
+	}
+	return -EINVAL;
+}
+
+static int rcar_drif_enum_fmt_sdr_cap(struct file *file, void *priv,
+				      struct v4l2_fmtdesc *f)
+{
+	if (f->index >= NUM_FORMATS)
+		return -EINVAL;
+
+	f->pixelformat = formats[f->index].pixelformat;
+	return 0;
+}
+
+static int rcar_drif_g_fmt_sdr_cap(struct file *file, void *priv,
+				   struct v4l2_format *f)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+
+	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
+	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
+	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
+	return 0;
+}
+
+static int rcar_drif_s_fmt_sdr_cap(struct file *file, void *priv,
+				   struct v4l2_format *f)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct vb2_queue *q = &sdr->vb_queue;
+	unsigned int i;
+
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
+			sdr->fmt_idx  = i;
+			f->fmt.sdr.buffersize = formats[i].buffersize;
+
+			/*
+			 * If a format demands one channel only out of two
+			 * enabled channels, pick the 0th channel.
+			 */
+			if (formats[i].num_ch < sdr->num_hw_ch) {
+				sdr->cur_ch_mask = BIT(0);
+				sdr->num_cur_ch = formats[i].num_ch;
+			} else {
+				sdr->cur_ch_mask = sdr->hw_ch_mask;
+				sdr->num_cur_ch = sdr->num_hw_ch;
+			}
+
+			rdrif_dbg(1, sdr, "cur: idx %u mask %lu num %u\n",
+				  i, sdr->cur_ch_mask, sdr->num_cur_ch);
+			return 0;
+		}
+	}
+
+	if (rcar_drif_set_default_format(sdr)) {
+		rdrif_err(sdr, "cannot set default format\n");
+		return -EINVAL;
+	}
+
+	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
+	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
+	return 0;
+}
+
+static int rcar_drif_try_fmt_sdr_cap(struct file *file, void *priv,
+				     struct v4l2_format *f)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	unsigned int i;
+
+	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
+			f->fmt.sdr.buffersize = formats[i].buffersize;
+			return 0;
+		}
+	}
+
+	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
+	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
+	return 0;
+}
+
+/* Tuner subdev ioctls */
+static int rcar_drif_enum_freq_bands(struct file *file, void *priv,
+				     struct v4l2_frequency_band *band)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct v4l2_subdev *sd;
+	int ret = 0;
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_subdev_call(sd, tuner, enum_freq_bands, band);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static int rcar_drif_g_frequency(struct file *file, void *priv,
+				 struct v4l2_frequency *f)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct v4l2_subdev *sd;
+	int ret = 0;
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_subdev_call(sd, tuner, g_frequency, f);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static int rcar_drif_s_frequency(struct file *file, void *priv,
+				 const struct v4l2_frequency *f)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct v4l2_subdev *sd;
+	int ret = 0;
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_subdev_call(sd, tuner, s_frequency, f);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static int rcar_drif_g_tuner(struct file *file, void *priv,
+			     struct v4l2_tuner *vt)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct v4l2_subdev *sd;
+	int ret = 0;
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_subdev_call(sd, tuner, g_tuner, vt);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static int rcar_drif_s_tuner(struct file *file, void *priv,
+			     const struct v4l2_tuner *vt)
+{
+	struct rcar_drif_sdr *sdr = video_drvdata(file);
+	struct v4l2_subdev *sd;
+	int ret = 0;
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_subdev_call(sd, tuner, s_tuner, vt);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static const struct v4l2_ioctl_ops rcar_drif_ioctl_ops = {
+	.vidioc_querycap          = rcar_drif_querycap,
+
+	.vidioc_enum_fmt_sdr_cap  = rcar_drif_enum_fmt_sdr_cap,
+	.vidioc_g_fmt_sdr_cap     = rcar_drif_g_fmt_sdr_cap,
+	.vidioc_s_fmt_sdr_cap     = rcar_drif_s_fmt_sdr_cap,
+	.vidioc_try_fmt_sdr_cap   = rcar_drif_try_fmt_sdr_cap,
+
+	.vidioc_reqbufs           = vb2_ioctl_reqbufs,
+	.vidioc_create_bufs       = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
+	.vidioc_querybuf          = vb2_ioctl_querybuf,
+	.vidioc_qbuf              = vb2_ioctl_qbuf,
+	.vidioc_dqbuf             = vb2_ioctl_dqbuf,
+
+	.vidioc_streamon          = vb2_ioctl_streamon,
+	.vidioc_streamoff         = vb2_ioctl_streamoff,
+
+	.vidioc_s_frequency       = rcar_drif_s_frequency,
+	.vidioc_g_frequency       = rcar_drif_g_frequency,
+	.vidioc_s_tuner		  = rcar_drif_s_tuner,
+	.vidioc_g_tuner		  = rcar_drif_g_tuner,
+	.vidioc_enum_freq_bands   = rcar_drif_enum_freq_bands,
+	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+	.vidioc_log_status        = v4l2_ctrl_log_status,
+};
+
+static const struct v4l2_file_operations rcar_drif_fops = {
+	.owner                    = THIS_MODULE,
+	.open                     = v4l2_fh_open,
+	.release                  = vb2_fop_release,
+	.read                     = vb2_fop_read,
+	.poll                     = vb2_fop_poll,
+	.mmap                     = vb2_fop_mmap,
+	.unlocked_ioctl           = video_ioctl2,
+};
+
+static int rcar_drif_notify_bound(struct v4l2_async_notifier *notifier,
+				   struct v4l2_subdev *subdev,
+				   struct v4l2_async_subdev *asd)
+{
+	struct rcar_drif_sdr *sdr =
+		container_of(notifier, struct rcar_drif_sdr, notifier);
+
+	/* Nothing to do at this point */
+	rdrif_dbg(2, sdr, "bound asd: %s\n", asd->match.of.node->name);
+	return 0;
+}
+
+/* Sub-device registered notification callback */
+static int rcar_drif_notify_complete(struct v4l2_async_notifier *notifier)
+{
+	struct rcar_drif_sdr *sdr =
+		container_of(notifier, struct rcar_drif_sdr, notifier);
+	struct v4l2_subdev *sd;
+	int ret;
+
+	sdr->v4l2_dev.ctrl_handler = &sdr->ctrl_hdl;
+
+	ret = v4l2_device_register_subdev_nodes(&sdr->v4l2_dev);
+	if (ret) {
+		rdrif_err(sdr, "failed register subdev nodes ret %d\n", ret);
+		return ret;
+	}
+
+	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
+		ret = v4l2_ctrl_add_handler(sdr->v4l2_dev.ctrl_handler,
+					    sd->ctrl_handler, NULL);
+		if (ret) {
+			rdrif_err(sdr, "failed ctrl add hdlr ret %d\n", ret);
+			return ret;
+		}
+	}
+	rdrif_dbg(2, sdr, "notify complete\n");
+	return 0;
+}
+
+/* Read endpoint properties */
+static void rcar_drif_get_ep_properties(struct rcar_drif_sdr *sdr,
+					struct device_node *np)
+{
+	u32 val;
+
+	/* Set the I2S defaults for SIRMDR1*/
+	sdr->mdr1 = RCAR_DRIF_SIRMDR1_SYNCMD_LR | RCAR_DRIF_SIRMDR1_MSB_FIRST |
+		RCAR_DRIF_SIRMDR1_DTDL_1 | RCAR_DRIF_SIRMDR1_SYNCDL_0;
+
+	/* Parse sync polarity from endpoint */
+	if (!of_property_read_u32(np, "renesas,sync-active", &val))
+		sdr->mdr1 |= val ? RCAR_DRIF_SIRMDR1_SYNCAC_POL_HIGH :
+			RCAR_DRIF_SIRMDR1_SYNCAC_POL_LOW;
+	else
+		sdr->mdr1 |= RCAR_DRIF_SIRMDR1_SYNCAC_POL_HIGH; /* default */
+
+	dev_dbg(sdr->dev, "mdr1 0x%08x\n", sdr->mdr1);
+}
+
+/* Parse sub-devs (tuner) to find a matching device */
+static int rcar_drif_parse_subdevs(struct rcar_drif_sdr *sdr,
+				   struct device *dev)
+{
+	struct v4l2_async_notifier *notifier = &sdr->notifier;
+	struct rcar_drif_async_subdev *rsd;
+	struct device_node *node;
+
+	notifier->subdevs = devm_kzalloc(dev, sizeof(*notifier->subdevs),
+					 GFP_KERNEL);
+	if (!notifier->subdevs)
+		return -ENOMEM;
+
+	node = of_graph_get_next_endpoint(dev->of_node, NULL);
+	if (!node)
+		return 0;
+
+	rsd = devm_kzalloc(dev, sizeof(*rsd), GFP_KERNEL);
+	if (!rsd) {
+		of_node_put(node);
+		return -ENOMEM;
+	}
+
+	notifier->subdevs[notifier->num_subdevs] = &rsd->asd;
+	rsd->asd.match.of.node = of_graph_get_remote_port_parent(node);
+	of_node_put(node);
+	if (!rsd->asd.match.of.node) {
+		dev_warn(dev, "bad remote port parent\n");
+		return -EINVAL;
+	}
+
+	rsd->asd.match_type = V4L2_ASYNC_MATCH_OF;
+	notifier->num_subdevs++;
+
+	/* Get the endpoint properties */
+	rcar_drif_get_ep_properties(sdr, node);
+	return 0;
+}
+
+/* Check if the given device is the primary bond */
+static bool rcar_drif_primary_bond(struct platform_device *pdev)
+{
+	if (of_find_property(pdev->dev.of_node, "renesas,primary-bond", NULL))
+		return true;
+
+	return false;
+}
+
+/* Get the bonded platform dev if enabled */
+static struct platform_device *rcar_drif_enabled_bond(struct platform_device *p)
+{
+	struct device_node *np;
+
+	np = of_parse_phandle(p->dev.of_node, "renesas,bonding", 0);
+	if (np && of_device_is_available(np))
+		return of_find_device_by_node(np);
+
+	return NULL;
+}
+
+/* Proble internal channel */
+static int rcar_drif_channel_probe(struct platform_device *pdev)
+{
+	struct rcar_drif *ch;
+	struct resource	*res;
+	void __iomem *base;
+	struct clk *clkp;
+	int ret;
+
+	/* Peripheral clock */
+	clkp = devm_clk_get(&pdev->dev, "fck");
+	if (IS_ERR(clkp)) {
+		ret = PTR_ERR(clkp);
+		dev_err(&pdev->dev, "clk get failed (%d)\n", ret);
+		return ret;
+	}
+
+	/* Register map */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(base)) {
+		ret = PTR_ERR(base);
+		dev_err(&pdev->dev, "ioremap failed (%d)\n", ret);
+		return ret;
+	}
+
+	/* Reserve memory for enabled channel */
+	ch = devm_kzalloc(&pdev->dev, sizeof(*ch), GFP_KERNEL);
+	if (!ch) {
+		ret = PTR_ERR(ch);
+		dev_err(&pdev->dev, "failed alloc channel\n");
+		return ret;
+	}
+	ch->pdev = pdev;
+	ch->clkp = clkp;
+	ch->base = base;
+	ch->start = res->start;
+	platform_set_drvdata(pdev, ch);
+	return 0;
+}
+
+static int rcar_drif_probe(struct platform_device *pdev)
+{
+	struct rcar_drif *ch, *b_ch = NULL;
+	struct platform_device *b_pdev;
+	struct rcar_drif_sdr *sdr;
+	int ret;
+
+	/* Probe internal channel */
+	ret = rcar_drif_channel_probe(pdev);
+	if (ret)
+		return ret;
+
+	/* Check if both channels of the bond are enabled */
+	b_pdev = rcar_drif_enabled_bond(pdev);
+	if (b_pdev) {
+		/* Check if current channel acting as primary-bond */
+		if (!rcar_drif_primary_bond(pdev)) {
+			dev_notice(&pdev->dev, "probed\n");
+			return 0;
+		}
+
+		/* Check if the other device is probed */
+		b_ch = platform_get_drvdata(b_pdev);
+		if (!b_ch) {
+			dev_info(&pdev->dev, "defer probe\n");
+			return -EPROBE_DEFER;
+		}
+		/* Set the other channel number */
+		b_ch->num = 1;
+	}
+
+	/* Channel acting as SDR instance */
+	ch = platform_get_drvdata(pdev);
+	ch->acting_sdr = true;
+
+	/* Reserve memory for SDR structure */
+	sdr = devm_kzalloc(&pdev->dev, sizeof(*sdr), GFP_KERNEL);
+	if (!sdr) {
+		ret = PTR_ERR(sdr);
+		dev_err(&pdev->dev, "failed alloc drif context\n");
+		return ret;
+	}
+	sdr->dev = &pdev->dev;
+	sdr->hw_ch_mask = BIT(ch->num);
+
+	/* Establish links between SDR and channel(s) */
+	ch->sdr = sdr;
+	sdr->ch[ch->num] = ch;
+	if (b_ch) {
+		sdr->ch[b_ch->num] = b_ch;
+		b_ch->sdr = sdr;
+		sdr->hw_ch_mask |= BIT(b_ch->num);
+	}
+	sdr->num_hw_ch = hweight_long(sdr->hw_ch_mask);
+
+	/* Validate any supported format for enabled channels */
+	ret = rcar_drif_set_default_format(sdr);
+	if (ret) {
+		dev_err(sdr->dev, "failed to set default format\n");
+		return ret;
+	}
+
+	/* Set defaults */
+	sdr->hwbuf_size = RCAR_DRIF_DEFAULT_HWBUF_SIZE;
+
+	mutex_init(&sdr->v4l2_mutex);
+	mutex_init(&sdr->vb_queue_mutex);
+	spin_lock_init(&sdr->queued_bufs_lock);
+	INIT_LIST_HEAD(&sdr->queued_bufs);
+
+	/* Init videobuf2 queue structure */
+	sdr->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
+	sdr->vb_queue.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
+	sdr->vb_queue.drv_priv = sdr;
+	sdr->vb_queue.buf_struct_size = sizeof(struct rcar_drif_frame_buf);
+	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
+	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
+	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+
+	/* Init videobuf2 queue */
+	ret = vb2_queue_init(&sdr->vb_queue);
+	if (ret) {
+		dev_err(sdr->dev, "could not initialize vb2 queue\n");
+		return ret;
+	}
+
+	/* Register the v4l2_device */
+	ret = v4l2_device_register(&pdev->dev, &sdr->v4l2_dev);
+	if (ret) {
+		dev_err(sdr->dev, "failed v4l2_device_register (%d)\n", ret);
+		return ret;
+	}
+
+	/*
+	 * Parse subdevs after v4l2_device_register because if the subdev
+	 * is already probed, bound and complete will be called immediately
+	 */
+	ret = rcar_drif_parse_subdevs(sdr, &pdev->dev);
+	if (ret)
+		goto err_unreg_v4l2;
+
+	sdr->notifier.bound = rcar_drif_notify_bound;
+	sdr->notifier.complete = rcar_drif_notify_complete;
+
+	v4l2_ctrl_handler_init(&sdr->ctrl_hdl, 10);
+
+	/* Register notifier */
+	ret = v4l2_async_notifier_register(&sdr->v4l2_dev, &sdr->notifier);
+	if (ret < 0) {
+		dev_err(sdr->dev, "notifier registration failed (%d)\n", ret);
+		goto err_free_ctrls;
+	}
+
+	/* Init video_device structure */
+	sdr->vdev = video_device_alloc();
+	if (!sdr->vdev) {
+		ret = -ENOMEM;
+		goto err_unreg_notif;
+	}
+	snprintf(sdr->vdev->name, sizeof(sdr->vdev->name), "R-Car DRIF");
+	sdr->vdev->fops = &rcar_drif_fops;
+	sdr->vdev->ioctl_ops = &rcar_drif_ioctl_ops;
+	sdr->vdev->release = video_device_release;
+	sdr->vdev->lock = &sdr->v4l2_mutex;
+	sdr->vdev->queue = &sdr->vb_queue;
+	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
+	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
+	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
+	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
+		V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
+	video_set_drvdata(sdr->vdev, sdr);
+
+	/* Register V4L2 SDR device */
+	ret = video_register_device(sdr->vdev, VFL_TYPE_SDR, -1);
+	if (ret) {
+		dev_err(sdr->dev, "failed video_register_device (%d)\n", ret);
+		goto err_unreg_notif;
+	}
+
+	dev_notice(sdr->dev, "probed\n");
+	return 0;
+
+err_unreg_notif:
+	video_device_release(sdr->vdev);
+	v4l2_async_notifier_unregister(&sdr->notifier);
+err_free_ctrls:
+	v4l2_ctrl_handler_free(&sdr->ctrl_hdl);
+err_unreg_v4l2:
+	v4l2_device_unregister(&sdr->v4l2_dev);
+	return ret;
+}
+
+static int rcar_drif_remove(struct platform_device *pdev)
+{
+	struct rcar_drif *ch = platform_get_drvdata(pdev);
+	struct rcar_drif_sdr *sdr = ch->sdr;
+
+	if (!ch->acting_sdr) {
+		/* Nothing to do */
+		dev_notice(&pdev->dev, "removed\n");
+		return 0;
+	}
+
+	/* SDR instance */
+	v4l2_ctrl_handler_free(sdr->vdev->ctrl_handler);
+	v4l2_async_notifier_unregister(&sdr->notifier);
+	v4l2_device_unregister(&sdr->v4l2_dev);
+	video_unregister_device(sdr->vdev);
+	dev_notice(&pdev->dev, "removed\n");
+	return 0;
+}
+
+static int __maybe_unused rcar_drif_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int __maybe_unused rcar_drif_resume(struct device *dev)
+{
+	return 0;
+}
+
+static SIMPLE_DEV_PM_OPS(rcar_drif_pm_ops, rcar_drif_suspend,
+			 rcar_drif_resume);
+
+static const struct of_device_id rcar_drif_of_table[] = {
+	{ .compatible = "renesas,rcar-gen3-drif" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, rcar_drif_of_table);
+
+#define RCAR_DRIF_DRV_NAME "rcar_drif"
+static struct platform_driver rcar_drif_driver = {
+	.driver = {
+		.name = RCAR_DRIF_DRV_NAME,
+		.of_match_table = of_match_ptr(rcar_drif_of_table),
+		.pm = &rcar_drif_pm_ops,
+		},
+	.probe = rcar_drif_probe,
+	.remove = rcar_drif_remove,
+};
+
+module_platform_driver(rcar_drif_driver);
+
+MODULE_DESCRIPTION("Renesas R-Car Gen3 DRIF driver");
+MODULE_ALIAS("platform:" RCAR_DRIF_DRV_NAME);
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support
  2017-02-07 15:02 ` [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support Ramesh Shanmugasundaram
@ 2017-02-13 12:40   ` Hans Verkuil
  0 siblings, 0 replies; 31+ messages in thread
From: Hans Verkuil @ 2017-02-13 12:40 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram, robh+dt, mark.rutland, mchehab,
	sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

Hi Ramesh,

A few little nitpicks:

On 02/07/2017 04:02 PM, Ramesh Shanmugasundaram wrote:
> This patch adds driver support for the MAX2175 chip. This is Maxim
> Integrated's RF to Bits tuner front end chip designed for software-defined
> radio solutions. This driver exposes the tuner as a sub-device instance
> with standard and custom controls to configure the device.
> 
> Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
> ---
>  Documentation/media/v4l-drivers/index.rst   |    1 +
>  Documentation/media/v4l-drivers/max2175.rst |   60 ++
>  drivers/media/i2c/Kconfig                   |    4 +
>  drivers/media/i2c/Makefile                  |    2 +
>  drivers/media/i2c/max2175/Kconfig           |    8 +
>  drivers/media/i2c/max2175/Makefile          |    4 +
>  drivers/media/i2c/max2175/max2175.c         | 1438 +++++++++++++++++++++++++++
>  drivers/media/i2c/max2175/max2175.h         |  108 ++
>  8 files changed, 1625 insertions(+)
>  create mode 100644 Documentation/media/v4l-drivers/max2175.rst
>  create mode 100644 drivers/media/i2c/max2175/Kconfig
>  create mode 100644 drivers/media/i2c/max2175/Makefile
>  create mode 100644 drivers/media/i2c/max2175/max2175.c
>  create mode 100644 drivers/media/i2c/max2175/max2175.h
> 

<snip>

> +
> +static const struct v4l2_ctrl_config max2175_i2s_en = {
> +	.ops = &max2175_ctrl_ops,
> +	.id = V4L2_CID_MAX2175_I2S_ENABLE,
> +	.name = "I2S Enable",
> +	.type = V4L2_CTRL_TYPE_BOOLEAN,
> +	.min = 0,
> +	.max = 1,
> +	.step = 1,
> +	.def = 1,
> +};
> +
> +/*
> + * HSLS value control LO freq adjacent location configuration.
> + * Refer to Documentation/media/v4l-drivers/max2175 for more details.
> + */
> +static const struct v4l2_ctrl_config max2175_hsls = {
> +	.ops = &max2175_ctrl_ops,
> +	.id = V4L2_CID_MAX2175_HSLS,
> +	.name = "HSLS above/below desired",

The convention is that the descriptions of controls follow the English 'title' rules
w.r.t. caps. See v4l2-ctrls.c.

This would become: "HSLS Above/Below Desired".

> +	.type = V4L2_CTRL_TYPE_INTEGER,

Shouldn't this be a boolean control?

> +	.min = 0,
> +	.max = 1,
> +	.step = 1,
> +	.def = 1,
> +};
> +
> +/*
> + * Rx modes below are a set of preset configurations that decides the tuner's
> + * sck and sample rate of transmission. They are separate for EU & NA regions.
> + * Refer to Documentation/media/v4l-drivers/max2175 for more details.
> + */
> +static const char * const max2175_ctrl_eu_rx_modes[] = {
> +	[MAX2175_EU_FM_1_2]	= "EU FM 1.2",
> +	[MAX2175_DAB_1_2]	= "DAB 1.2",
> +};
> +
> +static const char * const max2175_ctrl_na_rx_modes[] = {
> +	[MAX2175_NA_FM_1_0]	= "NA FM 1.0",
> +	[MAX2175_NA_FM_2_0]	= "NA FM 2.0",
> +};
> +
> +static const struct v4l2_ctrl_config max2175_eu_rx_mode = {
> +	.ops = &max2175_ctrl_ops,
> +	.id = V4L2_CID_MAX2175_RX_MODE,
> +	.name = "RX MODE",

MODE -> Mode.

> +	.type = V4L2_CTRL_TYPE_MENU,
> +	.max = ARRAY_SIZE(max2175_ctrl_eu_rx_modes) - 1,
> +	.def = 0,
> +	.qmenu = max2175_ctrl_eu_rx_modes,
> +};
> +
> +static const struct v4l2_ctrl_config max2175_na_rx_mode = {
> +	.ops = &max2175_ctrl_ops,
> +	.id = V4L2_CID_MAX2175_RX_MODE,
> +	.name = "RX MODE",

Ditto.

> +	.type = V4L2_CTRL_TYPE_MENU,
> +	.max = ARRAY_SIZE(max2175_ctrl_na_rx_modes) - 1,
> +	.def = 0,
> +	.qmenu = max2175_ctrl_na_rx_modes,
> +};
> +

Regards,

	Hans

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver
  2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
                   ` (6 preceding siblings ...)
  2017-02-07 15:02 ` [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support Ramesh Shanmugasundaram
@ 2017-02-13 12:46 ` Hans Verkuil
  2017-02-14 13:42   ` Laurent Pinchart
  7 siblings, 1 reply; 31+ messages in thread
From: Hans Verkuil @ 2017-02-13 12:46 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram, robh+dt, mark.rutland, mchehab,
	sakari.ailus, crope
  Cc: chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

On 02/07/2017 04:02 PM, Ramesh Shanmugasundaram wrote:
> Hi Media, DT maintainers, All,
> 
> This patch set contains two drivers
>  - Renesas R-Car Digital Radio Interface (DRIF) driver
>  - Maxim's MAX2175 RF to Bits tuner driver
> 
> These patches were based on top of media-tree repo
> commit: 47b037a0512d9f8675ec2693bed46c8ea6a884ab
> 
> These two drivers combined together expose a V4L2 SDR device that is compliant with the V4L2 framework [1]. Agreed review comments are incorporated in this series.
> 
> The rcar_drif device is modelled using "renesas,bonding" property. The discussion on this property is available here [2].

Other than the single comment I had it all looks good. Once I have Acks for the
bindings and from Laurent for the rcar part I can merge it.

Regards,

	Hans

> 
> Change history:
> 
> v2 -> v3:
> rcar_drif:
>  - Reduced DRIF DT properties to expose tested I2S mode only (Hans - discussion on #v4l)
>  - Fixed error path clean up of ctrl_hdl on rcar_drif
> 
> v1 -> v2:
>  - SDR formats renamed as "planar" instead of sliced (Hans)
>  - Documentation formatting correction (Laurent)
> 
>  rcar_drif:
>  - DT model using "bonding" property
>  - Addressed Laurent's coments on bindings - DT optional parameters rename & rework
>  - Addressed Han's comments on driver
>  - Addressed Geert's comments on DT
> 
>  max2175:
>  - Avoided scaling using method proposed by Antti. Thanks
>  - Bindings is a separate patch (Rob)
>  - Addressed Rob's comment on bindings
>  - Added Custom controls documentation (Laurent)
> 
> [1] v4l2-compliance report
> 
> root@salvator-x:~# v4l2-compliance -S /dev/swradio0
> v4l2-compliance SHA   : 788b674f3827607c09c31be11c91638f816aa6ae
> 
> Driver Info:
>         Driver name   : rcar_drif
>         Card type     : R-Car DRIF
>         Bus info      : platform:R-Car DRIF
>         Driver version: 4.10.0
>         Capabilities  : 0x85310000
>                 SDR Capture
>                 Tuner
>                 Read/Write
>                 Streaming
>                 Extended Pix Format
>                 Device Capabilities
>         Device Caps   : 0x05310000
>                 SDR Capture
>                 Tuner
>                 Read/Write
>                 Streaming
>                 Extended Pix Format
> 
> Compliance test for device /dev/swradio0 (not using libv4l2):
> 
> Required ioctls:
>         test VIDIOC_QUERYCAP: OK
> 
> Allow for multiple opens:
>         test second sdr open: OK
>         test VIDIOC_QUERYCAP: OK
>         test VIDIOC_G/S_PRIORITY: OK
>         test for unlimited opens: OK
> 
> Debug ioctls:
>         test VIDIOC_DBG_G/S_REGISTER: OK
>         test VIDIOC_LOG_STATUS: OK
> 
> Input ioctls:
>         test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK
>         test VIDIOC_G/S_FREQUENCY: OK
>         test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
>         test VIDIOC_ENUMAUDIO: OK (Not Supported)
>         test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
>         test VIDIOC_G/S_AUDIO: OK (Not Supported)
>         Inputs: 0 Audio Inputs: 0 Tuners: 1
> 
> Output ioctls:
>         test VIDIOC_G/S_MODULATOR: OK (Not Supported)
>         test VIDIOC_G/S_FREQUENCY: OK
>         test VIDIOC_ENUMAUDOUT: OK (Not Supported)
>         test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
>         test VIDIOC_G/S_AUDOUT: OK (Not Supported)
>         Outputs: 0 Audio Outputs: 0 Modulators: 0
> 
> Input/Output configuration ioctls:
>         test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
>         test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
>         test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
>         test VIDIOC_G/S_EDID: OK (Not Supported)
> 
>         Control ioctls:
>                 test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
>                 test VIDIOC_QUERYCTRL: OK
>                 test VIDIOC_G/S_CTRL: OK
>                 test VIDIOC_G/S/TRY_EXT_CTRLS: OK
>                 test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK
>                 test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
>                 Standard Controls: 5 Private Controls: 3
> 
>         Format ioctls:
>                 test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
>                 test VIDIOC_G/S_PARM: OK (Not Supported)
>                 test VIDIOC_G_FBUF: OK (Not Supported)
>                 test VIDIOC_G_FMT: OK
>                 test VIDIOC_TRY_FMT: OK
>                 test VIDIOC_S_FMT: OK
>                 test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
>                 test Cropping: OK (Not Supported)
>                 test Composing: OK (Not Supported)
>                 test Scaling: OK (Not Supported)
> 
>         Codec ioctls:
>                 test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
>                 test VIDIOC_G_ENC_INDEX: OK (Not Supported)
>                 test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
> 
>         Buffer ioctls:
>                 test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
>                 test VIDIOC_EXPBUF: OK (Not Supported)
> 
> Test input 0:
> 
> 
> Total: 43, Succeeded: 43, Failed: 0, Warnings: 0
> root@salvator-x:~#
> 
> [2] "bonding" DT property discussion (https://www.mail-archive.com/linux-renesas-soc@vger.kernel.org/msg09415.html)
> 
> Ramesh Shanmugasundaram (7):
>   media: v4l2-ctrls: Reserve controls for MAX217X
>   dt-bindings: media: Add MAX2175 binding description
>   media: i2c: max2175: Add MAX2175 support
>   media: Add new SDR formats PC16, PC18 & PC20
>   doc_rst: media: New SDR formats PC16, PC18 & PC20
>   dt-bindings: media: Add Renesas R-Car DRIF binding
>   media: platform: rcar_drif: Add DRIF support
> 
>  .../devicetree/bindings/media/i2c/max2175.txt      |   61 +
>  .../devicetree/bindings/media/renesas,drif.txt     |  186 +++
>  .../devicetree/bindings/property-units.txt         |    1 +
>  .../media/uapi/v4l/pixfmt-sdr-pcu16be.rst          |   55 +
>  .../media/uapi/v4l/pixfmt-sdr-pcu18be.rst          |   55 +
>  .../media/uapi/v4l/pixfmt-sdr-pcu20be.rst          |   54 +
>  Documentation/media/uapi/v4l/sdr-formats.rst       |    3 +
>  Documentation/media/v4l-drivers/index.rst          |    1 +
>  Documentation/media/v4l-drivers/max2175.rst        |   60 +
>  drivers/media/i2c/Kconfig                          |    4 +
>  drivers/media/i2c/Makefile                         |    2 +
>  drivers/media/i2c/max2175/Kconfig                  |    8 +
>  drivers/media/i2c/max2175/Makefile                 |    4 +
>  drivers/media/i2c/max2175/max2175.c                | 1438 ++++++++++++++++++
>  drivers/media/i2c/max2175/max2175.h                |  108 ++
>  drivers/media/platform/Kconfig                     |   25 +
>  drivers/media/platform/Makefile                    |    1 +
>  drivers/media/platform/rcar_drif.c                 | 1534 ++++++++++++++++++++
>  drivers/media/v4l2-core/v4l2-ioctl.c               |    3 +
>  include/uapi/linux/v4l2-controls.h                 |    5 +
>  include/uapi/linux/videodev2.h                     |    3 +
>  21 files changed, 3611 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/i2c/max2175.txt
>  create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
>  create mode 100644 Documentation/media/v4l-drivers/max2175.rst
>  create mode 100644 drivers/media/i2c/max2175/Kconfig
>  create mode 100644 drivers/media/i2c/max2175/Makefile
>  create mode 100644 drivers/media/i2c/max2175/max2175.c
>  create mode 100644 drivers/media/i2c/max2175/max2175.h
>  create mode 100644 drivers/media/platform/rcar_drif.c
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver
  2017-02-13 12:46 ` [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Hans Verkuil
@ 2017-02-14 13:42   ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-02-14 13:42 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Ramesh Shanmugasundaram, robh+dt, mark.rutland, mchehab,
	sakari.ailus, crope, chris.paterson2, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

Hi Hans,

On Monday 13 Feb 2017 13:46:08 Hans Verkuil wrote:
> On 02/07/2017 04:02 PM, Ramesh Shanmugasundaram wrote:
> > Hi Media, DT maintainers, All,
> > 
> > This patch set contains two drivers
> > 
> >  - Renesas R-Car Digital Radio Interface (DRIF) driver
> >  - Maxim's MAX2175 RF to Bits tuner driver
> > 
> > These patches were based on top of media-tree repo
> > commit: 47b037a0512d9f8675ec2693bed46c8ea6a884ab
> > 
> > These two drivers combined together expose a V4L2 SDR device that is
> > compliant with the V4L2 framework [1]. Agreed review comments are
> > incorporated in this series.
> > 
> > The rcar_drif device is modelled using "renesas,bonding" property. The
> > discussion on this property is available here [2].
> Other than the single comment I had it all looks good. Once I have Acks for
> the bindings and from Laurent for the rcar part I can merge it.

Just FYI, I won't have time to review this before March at the earliest.

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-02-07 15:02 ` [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description Ramesh Shanmugasundaram
@ 2017-02-15 16:59   ` Rob Herring
  2017-04-11  9:42   ` Laurent Pinchart
  1 sibling, 0 replies; 31+ messages in thread
From: Rob Herring @ 2017-02-15 16:59 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

On Tue, Feb 07, 2017 at 03:02:32PM +0000, Ramesh Shanmugasundaram wrote:
> Add device tree binding documentation for MAX2175 Rf to bits tuner
> device.
> 
> Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
> ---
>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 ++++++++++++++++++++++
>  .../devicetree/bindings/property-units.txt         |  1 +
>  2 files changed, 62 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/i2c/max2175.txt

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
  2017-02-07 15:02 ` [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding Ramesh Shanmugasundaram
@ 2017-02-15 17:09       ` Rob Herring
  2017-04-11 22:35   ` Laurent Pinchart
  1 sibling, 0 replies; 31+ messages in thread
From: Rob Herring @ 2017-02-15 17:09 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: mark.rutland-5wv7dgnIgG8, mchehab-DgEjT+Ai2ygdnm+yROfE0A,
	hverkuil-qWit8jRvyhVmR6Xm/wNWPw,
	sakari.ailus-VuQAYsv1563Yd54FQh9/CA, crope-X3B1VOXEql0,
	chris.paterson2-zM6kxYcvzFBBDgjK7y7TUQ,
	laurent.pinchart-ryLnwIuWjnjg/C1BVhZhaw,
	geert+renesas-gXvu3+zWzMSzQB+pC5nmwQ,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-renesas-soc-u79uwXL29TY76Z2rM5mHXA

On Tue, Feb 07, 2017 at 03:02:36PM +0000, Ramesh Shanmugasundaram wrote:
> Add binding documentation for Renesas R-Car Digital Radio Interface
> (DRIF) controller.
> 
> Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
> ---
>  .../devicetree/bindings/media/renesas,drif.txt     | 186 +++++++++++++++++++++
>  1 file changed, 186 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt
> 
> diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt b/Documentation/devicetree/bindings/media/renesas,drif.txt
> new file mode 100644
> index 0000000..6315609
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
> @@ -0,0 +1,186 @@
> +Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
> +------------------------------------------------------------
> +
> +R-Car Gen3 DRIF is a SPI like receive only slave device. A general
> +representation of DRIF interfacing with a master device is shown below.
> +
> ++---------------------+                +---------------------+
> +|                     |-----SCK------->|CLK                  |
> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> +|                     |-----SD0------->|D0                   |
> +|                     |-----SD1------->|D1                   |
> ++---------------------+                +---------------------+
> +
> +As per the datasheet, each DRIF channel (drifn) is made up of two internal
> +channels (drifn0 & drifn1). These two internal channels share the common
> +CLK & SYNC. Each internal channel has its own dedicated resources like
> +irq, dma channels, address space & clock. This internal split is not
> +visible to the external master device.
> +
> +The device tree model represents each internal channel as a separate node.
> +The internal channels sharing the CLK & SYNC are tied together by their
> +phandles using a new property called "renesas,bonding". For the rest of
> +the documentation, unless explicitly stated, the word channel implies an
> +internal channel.
> +
> +When both internal channels are enabled they need to be managed together
> +as one (i.e.) they cannot operate alone as independent devices. Out of the
> +two, one of them needs to act as a primary device that accepts common
> +properties of both the internal channels. This channel is identified by a
> +new property called "renesas,primary-bond".
> +
> +To summarize,
> +   - When both the internal channels that are bonded together are enabled,
> +     the zeroth channel is selected as primary-bond. This channels accepts
> +     properties common to all the members of the bond.
> +   - When only one of the bonded channels need to be enabled, the property
> +     "renesas,bonding" or "renesas,primary-bond" will have no effect. That
> +     enabled channel can act alone as any other independent device.
> +
> +Required properties of an internal channel:
> +-------------------------------------------
> +- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of R8A7795 SoC.
> +	      "renesas,rcar-gen3-drif" 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
> +	      followed by the generic version.
> +- reg: offset and length of that channel.
> +- interrupts: associated with that channel.
> +- clocks: phandle and clock specifier of that channel.
> +- clock-names: clock input name string: "fck".
> +- dmas: phandles to the DMA channels.
> +- dma-names: names of the DMA channel: "rx".
> +- renesas,bonding: phandle to the other channel.
> +
> +Optional properties of an internal channel:
> +-------------------------------------------
> +- power-domains: phandle to the respective power domain.
> +
> +Required properties of an internal channel when:
> +	- It is the only enabled channel of the bond (or)
> +	- If it acts as primary among enabled bonds
> +--------------------------------------------------------
> +- pinctrl-0: pin control group to be used for this channel.
> +- pinctrl-names: must be "default".
> +- renesas,primary-bond: empty property indicating the channel acts as primary
> +			among the bonded channels.
> +- port: child port node of a channel that defines the local and remote
> +	endpoints. The remote endpoint is assumed to be a third party tuner
> +	device endpoint.
> +
> +Optional endpoint property:
> +---------------------------
> +- renesas,sync-active  : Indicates sync signal polarity, 0/1 for low/high
> +			 respectively. This property maps to SYNCAC bit in the
> +			 hardware manual. The default is 1 (active high)

Why does this belong in the endpoint? I'd prefer to not have vendor 
specific properties in endpoints. Is this a property of the tuner or 
DRIF? 

> +
> +Example
> +--------
> +
> +SoC common dtsi file
> +
> +		drif00: rif@e6f40000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f40000 0 0x64>;
> +			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 515>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif01>;
> +			status = "disabled";

Don't put "status" in examples.

> +		};
> +
> +		drif01: rif@e6f50000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f50000 0 0x64>;
> +			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 514>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif00>;
> +			status = "disabled";
> +		};
> +
> +
> +Board specific dts file

Chip vs. board in not relevant to the binding doc. Please combine them 
here in your example.

Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
@ 2017-02-15 17:09       ` Rob Herring
  0 siblings, 0 replies; 31+ messages in thread
From: Rob Herring @ 2017-02-15 17:09 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

On Tue, Feb 07, 2017 at 03:02:36PM +0000, Ramesh Shanmugasundaram wrote:
> Add binding documentation for Renesas R-Car Digital Radio Interface
> (DRIF) controller.
> 
> Signed-off-by: Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>
> ---
>  .../devicetree/bindings/media/renesas,drif.txt     | 186 +++++++++++++++++++++
>  1 file changed, 186 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt
> 
> diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt b/Documentation/devicetree/bindings/media/renesas,drif.txt
> new file mode 100644
> index 0000000..6315609
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
> @@ -0,0 +1,186 @@
> +Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
> +------------------------------------------------------------
> +
> +R-Car Gen3 DRIF is a SPI like receive only slave device. A general
> +representation of DRIF interfacing with a master device is shown below.
> +
> ++---------------------+                +---------------------+
> +|                     |-----SCK------->|CLK                  |
> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> +|                     |-----SD0------->|D0                   |
> +|                     |-----SD1------->|D1                   |
> ++---------------------+                +---------------------+
> +
> +As per the datasheet, each DRIF channel (drifn) is made up of two internal
> +channels (drifn0 & drifn1). These two internal channels share the common
> +CLK & SYNC. Each internal channel has its own dedicated resources like
> +irq, dma channels, address space & clock. This internal split is not
> +visible to the external master device.
> +
> +The device tree model represents each internal channel as a separate node.
> +The internal channels sharing the CLK & SYNC are tied together by their
> +phandles using a new property called "renesas,bonding". For the rest of
> +the documentation, unless explicitly stated, the word channel implies an
> +internal channel.
> +
> +When both internal channels are enabled they need to be managed together
> +as one (i.e.) they cannot operate alone as independent devices. Out of the
> +two, one of them needs to act as a primary device that accepts common
> +properties of both the internal channels. This channel is identified by a
> +new property called "renesas,primary-bond".
> +
> +To summarize,
> +   - When both the internal channels that are bonded together are enabled,
> +     the zeroth channel is selected as primary-bond. This channels accepts
> +     properties common to all the members of the bond.
> +   - When only one of the bonded channels need to be enabled, the property
> +     "renesas,bonding" or "renesas,primary-bond" will have no effect. That
> +     enabled channel can act alone as any other independent device.
> +
> +Required properties of an internal channel:
> +-------------------------------------------
> +- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of R8A7795 SoC.
> +	      "renesas,rcar-gen3-drif" 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
> +	      followed by the generic version.
> +- reg: offset and length of that channel.
> +- interrupts: associated with that channel.
> +- clocks: phandle and clock specifier of that channel.
> +- clock-names: clock input name string: "fck".
> +- dmas: phandles to the DMA channels.
> +- dma-names: names of the DMA channel: "rx".
> +- renesas,bonding: phandle to the other channel.
> +
> +Optional properties of an internal channel:
> +-------------------------------------------
> +- power-domains: phandle to the respective power domain.
> +
> +Required properties of an internal channel when:
> +	- It is the only enabled channel of the bond (or)
> +	- If it acts as primary among enabled bonds
> +--------------------------------------------------------
> +- pinctrl-0: pin control group to be used for this channel.
> +- pinctrl-names: must be "default".
> +- renesas,primary-bond: empty property indicating the channel acts as primary
> +			among the bonded channels.
> +- port: child port node of a channel that defines the local and remote
> +	endpoints. The remote endpoint is assumed to be a third party tuner
> +	device endpoint.
> +
> +Optional endpoint property:
> +---------------------------
> +- renesas,sync-active  : Indicates sync signal polarity, 0/1 for low/high
> +			 respectively. This property maps to SYNCAC bit in the
> +			 hardware manual. The default is 1 (active high)

Why does this belong in the endpoint? I'd prefer to not have vendor 
specific properties in endpoints. Is this a property of the tuner or 
DRIF? 

> +
> +Example
> +--------
> +
> +SoC common dtsi file
> +
> +		drif00: rif@e6f40000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f40000 0 0x64>;
> +			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 515>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif01>;
> +			status = "disabled";

Don't put "status" in examples.

> +		};
> +
> +		drif01: rif@e6f50000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f50000 0 0x64>;
> +			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 514>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif00>;
> +			status = "disabled";
> +		};
> +
> +
> +Board specific dts file

Chip vs. board in not relevant to the binding doc. Please combine them 
here in your example.

Rob

^ permalink raw reply	[flat|nested] 31+ messages in thread

* RE: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
  2017-02-15 17:09       ` Rob Herring
  (?)
@ 2017-02-16 11:02       ` Ramesh Shanmugasundaram
  2017-04-11 22:41         ` Laurent Pinchart
  -1 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-02-16 11:02 UTC (permalink / raw)
  To: Rob Herring
  Cc: mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, laurent.pinchart, geert+renesas, linux-media,
	devicetree, linux-renesas-soc

Hi Rob,

Thank you for the review comments.

> Subject: Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF
> binding
> 
> On Tue, Feb 07, 2017 at 03:02:36PM +0000, Ramesh Shanmugasundaram wrote:
> > Add binding documentation for Renesas R-Car Digital Radio Interface
> > (DRIF) controller.
> >
> > Signed-off-by: Ramesh Shanmugasundaram
> > <ramesh.shanmugasundaram@bp.renesas.com>
> > ---
> >  .../devicetree/bindings/media/renesas,drif.txt     | 186
> +++++++++++++++++++++
> >  1 file changed, 186 insertions(+)
> >  create mode 100644
> > Documentation/devicetree/bindings/media/renesas,drif.txt
> >
> > diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt
> > b/Documentation/devicetree/bindings/media/renesas,drif.txt
> > new file mode 100644
> > index 0000000..6315609
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
> > @@ -0,0 +1,186 @@
> > +Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
> > +------------------------------------------------------------
> > +
> > +R-Car Gen3 DRIF is a SPI like receive only slave device. A general
> > +representation of DRIF interfacing with a master device is shown below.
> > +
> > ++---------------------+                +---------------------+
> > +|                     |-----SCK------->|CLK                  |
> > +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> > +|                     |-----SD0------->|D0                   |
> > +|                     |-----SD1------->|D1                   |
> > ++---------------------+                +---------------------+
> > +
> > +As per the datasheet, each DRIF channel (drifn) is made up of two
> > +internal channels (drifn0 & drifn1). These two internal channels
> > +share the common CLK & SYNC. Each internal channel has its own
> > +dedicated resources like irq, dma channels, address space & clock.
> > +This internal split is not visible to the external master device.
> > +
> > +The device tree model represents each internal channel as a separate
> node.
> > +The internal channels sharing the CLK & SYNC are tied together by
> > +their phandles using a new property called "renesas,bonding". For the
> > +rest of the documentation, unless explicitly stated, the word channel
> > +implies an internal channel.
> > +
> > +When both internal channels are enabled they need to be managed
> > +together as one (i.e.) they cannot operate alone as independent
> > +devices. Out of the two, one of them needs to act as a primary device
> > +that accepts common properties of both the internal channels. This
> > +channel is identified by a new property called "renesas,primary-bond".
> > +
> > +To summarize,
> > +   - When both the internal channels that are bonded together are
> enabled,
> > +     the zeroth channel is selected as primary-bond. This channels
> accepts
> > +     properties common to all the members of the bond.
> > +   - When only one of the bonded channels need to be enabled, the
> property
> > +     "renesas,bonding" or "renesas,primary-bond" will have no effect.
> That
> > +     enabled channel can act alone as any other independent device.
> > +
> > +Required properties of an internal channel:
> > +-------------------------------------------
> > +- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of
> R8A7795 SoC.
> > +	      "renesas,rcar-gen3-drif" 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
> > +	      followed by the generic version.
> > +- reg: offset and length of that channel.
> > +- interrupts: associated with that channel.
> > +- clocks: phandle and clock specifier of that channel.
> > +- clock-names: clock input name string: "fck".
> > +- dmas: phandles to the DMA channels.
> > +- dma-names: names of the DMA channel: "rx".
> > +- renesas,bonding: phandle to the other channel.
> > +
> > +Optional properties of an internal channel:
> > +-------------------------------------------
> > +- power-domains: phandle to the respective power domain.
> > +
> > +Required properties of an internal channel when:
> > +	- It is the only enabled channel of the bond (or)
> > +	- If it acts as primary among enabled bonds
> > +--------------------------------------------------------
> > +- pinctrl-0: pin control group to be used for this channel.
> > +- pinctrl-names: must be "default".
> > +- renesas,primary-bond: empty property indicating the channel acts as
> primary
> > +			among the bonded channels.
> > +- port: child port node of a channel that defines the local and remote
> > +	endpoints. The remote endpoint is assumed to be a third party tuner
> > +	device endpoint.
> > +
> > +Optional endpoint property:
> > +---------------------------
> > +- renesas,sync-active  : Indicates sync signal polarity, 0/1 for
> low/high
> > +			 respectively. This property maps to SYNCAC bit in the
> > +			 hardware manual. The default is 1 (active high)
> 
> Why does this belong in the endpoint? I'd prefer to not have vendor
> specific properties in endpoints. Is this a property of the tuner or DRIF?

This property is similar to the properties in Documentation/devicetree/bindings/media/video-interfaces.txt (e.g. hsync-active, vsync-active).Hence, Laurent & Hans suggested this to be defined as an endpoint property and try to standardize it.

I think I see your point. As endpoint properties can be defined on both endpoints, having a vendor specific property is a problem with a third party tuner. We could remove the vendor tag and make it  a generic property "sync-active", if you are OK with it? 

This property can be defined for both tuner and DRIF. However, it would mostly be a constant in the case of tuner because as per I2S spec, transmitter WS (sync) changes from high->low & low->high always. Only DRIF allows the option to latch when WS high->low or low->high - both cases.

In a traditional use case it is always WS high->low latching to get the first data. However, with DRIF & MAX2175 combo, our latest investigations reveal that latching when WS low->high provided better synchronization on all cases. There is no loss of data by doing this. Hence, it would be nice to retain this as a configurable property.

Please advice.


> 
> > +
> > +Example
> > +--------
> > +
> > +SoC common dtsi file
> > +
> > +		drif00: rif@e6f40000 {
> > +			compatible = "renesas,r8a7795-drif",
> > +				     "renesas,rcar-gen3-drif";
> > +			reg = <0 0xe6f40000 0 0x64>;
> > +			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
> > +			clocks = <&cpg CPG_MOD 515>;
> > +			clock-names = "fck";
> > +			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
> > +			dma-names = "rx", "rx";
> > +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> > +			renesas,bonding = <&drif01>;
> > +			status = "disabled";
> 
> Don't put "status" in examples.

OK. Will note this down for future patches. Will be corrected in the next version.

> 
> > +		};
> > +
> > +		drif01: rif@e6f50000 {
> > +			compatible = "renesas,r8a7795-drif",
> > +				     "renesas,rcar-gen3-drif";
> > +			reg = <0 0xe6f50000 0 0x64>;
> > +			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
> > +			clocks = <&cpg CPG_MOD 514>;
> > +			clock-names = "fck";
> > +			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
> > +			dma-names = "rx", "rx";
> > +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> > +			renesas,bonding = <&drif00>;
> > +			status = "disabled";
> > +		};
> > +
> > +
> > +Board specific dts file
> 
> Chip vs. board in not relevant to the binding doc. Please combine them
> here in your example.

Will do.

Thanks,
Ramesh

> 
> Rob

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 1/7] media: v4l2-ctrls: Reserve controls for MAX217X
  2017-02-07 15:02     ` Ramesh Shanmugasundaram
  (?)
@ 2017-04-11  8:28     ` Laurent Pinchart
  -1 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11  8:28 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:31 Ramesh Shanmugasundaram wrote:
> Reserve controls for MAX217X RF to Bits tuner family. These hybrid
> radio receiver chips are highly programmable and hence reserving 32
> controls.
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram@bp.renesas.com>

Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  include/uapi/linux/v4l2-controls.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/include/uapi/linux/v4l2-controls.h
> b/include/uapi/linux/v4l2-controls.h index 0d2e1e0..83b28b4 100644
> --- a/include/uapi/linux/v4l2-controls.h
> +++ b/include/uapi/linux/v4l2-controls.h
> @@ -180,6 +180,11 @@ enum v4l2_colorfx {
>   * We reserve 16 controls for this driver. */
>  #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
> 
> +/* The base for the max217x driver controls.
> + * We reserve 32 controls for this driver
> + */
> +#define V4L2_CID_USER_MAX217X_BASE		(V4L2_CID_USER_BASE + 0x1090)
> +
>  /* MPEG-class control IDs */
>  /* The MPEG controls are applicable to all codec controls
>   * and the 'MPEG' part of the define is historical */

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-02-07 15:02 ` [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description Ramesh Shanmugasundaram
  2017-02-15 16:59   ` Rob Herring
@ 2017-04-11  9:42   ` Laurent Pinchart
  2017-04-11  9:57     ` Ramesh Shanmugasundaram
  1 sibling, 1 reply; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11  9:42 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> Add device tree binding documentation for MAX2175 Rf to bits tuner
> device.
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram@bp.renesas.com> ---
>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 +++++++++++++++++++
>  .../devicetree/bindings/property-units.txt         |  1 +
>  2 files changed, 62 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/i2c/max2175.txt
> 
> diff --git a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file mode
> 100644
> index 0000000..f591ab4
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
> @@ -0,0 +1,61 @@
> +Maxim Integrated MAX2175 RF to Bits tuner
> +-----------------------------------------
> +
> +The MAX2175 IC is an advanced analog/digital hybrid-radio receiver with
> +RF to Bits® front-end designed for software-defined radio solutions.
> +
> +Required properties:
> +--------------------
> +- compatible: "maxim,max2175" for MAX2175 RF-to-bits tuner.
> +- clocks: phandle to the fixed xtal clock.
> +- clock-names: name of the fixed xtal clock.

I would mention that the name has to be "xtal". Maybe something like

- clock-names: name of the fixed xtal clock, shall be "xtal".

> +- port: child port node of a tuner that defines the local and remote
> +  endpoints. The remote endpoint is assumed to be an SDR device
> +  that is capable of receiving the digital samples from the tuner.

You should refer to the OF graphs bindings here. How about the following to 
document the port node ?

- port: child port node corresponding to the I2S output, in accordance with 
the video interface bindings defined in
Documentation/devicetree/bindings/media/video-interfaces.txt. The port node 
must contain at least one endpoint.

> +Optional properties:
> +--------------------
> +- maxim,slave	      : phandle to the master tuner if it is a slave. 
This
> +			is used to define two tuners in diversity mode
> +			(1 master, 1 slave). By default each tuner is an
> +			individual master.

It seems weird to me to name a property "slave" when it points to the master 
tuner. Shouldn't it be named "maxim,master" ?

> +- maxim,refout-load-pF: load capacitance value (in pF) on reference
> +			output drive level. The possible load values are
> +			 0 (default - refout disabled)
> +			10
> +			20
> +			30
> +			40
> +			60
> +			70
> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path 
is
> +			selected for AM antenna input. By default this
> +			filter path is not used.

Isn't this something that should be selected at runtime through a control ? Or 
does the hardware design dictate whether the filter has to be used or must not 
be used ?

> +Example:
> +--------
> +
> +Board specific DTS file
> +
> +/* Fixed XTAL clock node */
> +maxim_xtal: clock {
> +	compatible = "fixed-clock";
> +	#clock-cells = <0>;
> +	clock-frequency = <36864000>;
> +};
> +
> +/* A tuner device instance under i2c bus */
> +max2175_0: tuner@60 {
> +	compatible = "maxim,max2175";
> +	reg = <0x60>;
> +	clocks = <&maxim_xtal>;
> +	clock-names = "xtal";
> +	maxim,refout-load-pF = <10>;
> +
> +	port {
> +		max2175_0_ep: endpoint {
> +			remote-endpoint = <&slave_rx_device>;
> +		};
> +	};
> +
> +};
> diff --git a/Documentation/devicetree/bindings/property-units.txt
> b/Documentation/devicetree/bindings/property-units.txt index
> 12278d7..f1f1c22 100644
> --- a/Documentation/devicetree/bindings/property-units.txt
> +++ b/Documentation/devicetree/bindings/property-units.txt
> @@ -28,6 +28,7 @@ Electricity
>  -ohms		: Ohms
>  -micro-ohms	: micro Ohms
>  -microvolt	: micro volts
> +-pF		: pico farads
> 
>  Temperature
>  ----------------------------------------

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* RE: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-04-11  9:42   ` Laurent Pinchart
@ 2017-04-11  9:57     ` Ramesh Shanmugasundaram
       [not found]       ` <HK2PR06MB0545282102FBC0472D9831AEC3000-jAC1QCBx2F1Af0bSEaWBKm0DtJ1/0DrXvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
  0 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-04-11  9:57 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Laurent,

Thanks for the review comments.

> 
> On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> > Add device tree binding documentation for MAX2175 Rf to bits tuner
> > device.
> >
> > Signed-off-by: Ramesh Shanmugasundaram
> > <ramesh.shanmugasundaram@bp.renesas.com> ---
> >  .../devicetree/bindings/media/i2c/max2175.txt      | 61
> +++++++++++++++++++
> >  .../devicetree/bindings/property-units.txt         |  1 +
> >  2 files changed, 62 insertions(+)
> >  create mode 100644
> > Documentation/devicetree/bindings/media/i2c/max2175.txt
> >
> > diff --git a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> > b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> > mode
> > 100644
> > index 0000000..f591ab4
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
> > @@ -0,0 +1,61 @@
> > +Maxim Integrated MAX2175 RF to Bits tuner
> > +-----------------------------------------
> > +
> > +The MAX2175 IC is an advanced analog/digital hybrid-radio receiver
> > +with RF to Bits(r) front-end designed for software-defined radio
> solutions.
> > +
> > +Required properties:
> > +--------------------
> > +- compatible: "maxim,max2175" for MAX2175 RF-to-bits tuner.
> > +- clocks: phandle to the fixed xtal clock.
> > +- clock-names: name of the fixed xtal clock.
> 
> I would mention that the name has to be "xtal". Maybe something like
> 
> - clock-names: name of the fixed xtal clock, shall be "xtal".

Agreed.

> 
> > +- port: child port node of a tuner that defines the local and remote
> > +  endpoints. The remote endpoint is assumed to be an SDR device
> > +  that is capable of receiving the digital samples from the tuner.
> 
> You should refer to the OF graphs bindings here. How about the following
> to document the port node ?
> 
> - port: child port node corresponding to the I2S output, in accordance
> with the video interface bindings defined in
> Documentation/devicetree/bindings/media/video-interfaces.txt. The port
> node must contain at least one endpoint.

Agreed.

> 
> > +Optional properties:
> > +--------------------
> > +- maxim,slave	      : phandle to the master tuner if it is a slave.
> This
> > +			is used to define two tuners in diversity mode
> > +			(1 master, 1 slave). By default each tuner is an
> > +			individual master.
> 
> It seems weird to me to name a property "slave" when it points to the
> master tuner. Shouldn't it be named "maxim,master" ?

Agreed.

> 
> > +- maxim,refout-load-pF: load capacitance value (in pF) on reference
> > +			output drive level. The possible load values are
> > +			 0 (default - refout disabled)
> > +			10
> > +			20
> > +			30
> > +			40
> > +			60
> > +			70
> > +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path
> is
> > +			selected for AM antenna input. By default this
> > +			filter path is not used.
> 
> Isn't this something that should be selected at runtime through a control
> ? Or does the hardware design dictate whether the filter has to be used or
> must not be used ?

This is dictated by the h/w design and not selectable at run-time. 
I will update these changes in the next patchset.

Thanks,
Ramesh

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-04-11  9:57     ` Ramesh Shanmugasundaram
@ 2017-04-11 11:27           ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 11:27 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt-DgEjT+Ai2ygdnm+yROfE0A, mark.rutland-5wv7dgnIgG8,
	mchehab-DgEjT+Ai2ygdnm+yROfE0A, hverkuil-qWit8jRvyhVmR6Xm/wNWPw,
	sakari.ailus-VuQAYsv1563Yd54FQh9/CA, crope-X3B1VOXEql0,
	Chris Paterson, geert+renesas-gXvu3+zWzMSzQB+pC5nmwQ,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-renesas-soc-u79uwXL29TY76Z2rM5mHXA

Hi Ramesh,

On Tuesday 11 Apr 2017 09:57:45 Ramesh Shanmugasundaram wrote:
> > On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> >> Add device tree binding documentation for MAX2175 Rf to bits tuner
> >> device.
> >> 
> >> Signed-off-by: Ramesh Shanmugasundaram
> >> <ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org> ---
> >> 
> >>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 +++++++++++++++
> >>  .../devicetree/bindings/property-units.txt         |  1 +
> >>  2 files changed, 62 insertions(+)
> >>  create mode 100644
> >> 
> >> Documentation/devicetree/bindings/media/i2c/max2175.txt
> >> 
> >> diff --git a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> >> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> >> mode 100644
> >> index 0000000..f591ab4
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt

[snip]

> >> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path
> >> is
> >> +			selected for AM antenna input. By default this
> >> +			filter path is not used.
> > 
> > Isn't this something that should be selected at runtime through a control
> > ? Or does the hardware design dictate whether the filter has to be used or
> > must not be used ?
> 
> This is dictated by the h/w design and not selectable at run-time.
> I will update these changes in the next patchset.

In that case I'm fine with a property, but could we name it in such a way that 
it describes the hardware instead of instructing the software on how to 
configure the device ? For instance (and this is a made-up example as I don't 
know exactly how this works), if the AM Hi-Z filter is required when dealing 
with AM frequencies and forbidden when dealing with other frequency bands, and 
*if* boards have to be designed specifically for one frequency band (AM, FM, 
VHF, L, ...) without any way to accept different bands, then you could instead 
use

	maxim,frequency-band = "AM";

and enable the filter accordingly in the driver. This would be in my opinion a 
better system hardware description.

-- 
Regards,

Laurent Pinchart

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
@ 2017-04-11 11:27           ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 11:27 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

On Tuesday 11 Apr 2017 09:57:45 Ramesh Shanmugasundaram wrote:
> > On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> >> Add device tree binding documentation for MAX2175 Rf to bits tuner
> >> device.
> >> 
> >> Signed-off-by: Ramesh Shanmugasundaram
> >> <ramesh.shanmugasundaram@bp.renesas.com> ---
> >> 
> >>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 +++++++++++++++
> >>  .../devicetree/bindings/property-units.txt         |  1 +
> >>  2 files changed, 62 insertions(+)
> >>  create mode 100644
> >> 
> >> Documentation/devicetree/bindings/media/i2c/max2175.txt
> >> 
> >> diff --git a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> >> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> >> mode 100644
> >> index 0000000..f591ab4
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt

[snip]

> >> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path
> >> is
> >> +			selected for AM antenna input. By default this
> >> +			filter path is not used.
> > 
> > Isn't this something that should be selected at runtime through a control
> > ? Or does the hardware design dictate whether the filter has to be used or
> > must not be used ?
> 
> This is dictated by the h/w design and not selectable at run-time.
> I will update these changes in the next patchset.

In that case I'm fine with a property, but could we name it in such a way that 
it describes the hardware instead of instructing the software on how to 
configure the device ? For instance (and this is a made-up example as I don't 
know exactly how this works), if the AM Hi-Z filter is required when dealing 
with AM frequencies and forbidden when dealing with other frequency bands, and 
*if* boards have to be designed specifically for one frequency band (AM, FM, 
VHF, L, ...) without any way to accept different bands, then you could instead 
use

	maxim,frequency-band = "AM";

and enable the filter accordingly in the driver. This would be in my opinion a 
better system hardware description.

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* RE: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-04-11 11:27           ` Laurent Pinchart
  (?)
@ 2017-04-11 12:19           ` Ramesh Shanmugasundaram
       [not found]             ` <HK2PR06MB0545F9FBB1B27E91D80F906EC3000-jAC1QCBx2F1Af0bSEaWBKm0DtJ1/0DrXvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
  -1 siblings, 1 reply; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-04-11 12:19 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Laurent,

> On Tuesday 11 Apr 2017 09:57:45 Ramesh Shanmugasundaram wrote:
> > > On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> > >> Add device tree binding documentation for MAX2175 Rf to bits tuner
> > >> device.
> > >>
> > >> Signed-off-by: Ramesh Shanmugasundaram
> > >> <ramesh.shanmugasundaram@bp.renesas.com> ---
> > >>
> > >>  .../devicetree/bindings/media/i2c/max2175.txt      | 61
> +++++++++++++++
> > >>  .../devicetree/bindings/property-units.txt         |  1 +
> > >>  2 files changed, 62 insertions(+)
> > >>  create mode 100644
> > >>
> > >> Documentation/devicetree/bindings/media/i2c/max2175.txt
> > >>
> > >> diff --git
> > >> a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> > >> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> > >> mode 100644 index 0000000..f591ab4
> > >> --- /dev/null
> > >> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
> 
> [snip]
> 
> > >> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter
> path
> > >> is
> > >> +			selected for AM antenna input. By default this
> > >> +			filter path is not used.
> > >
> > > Isn't this something that should be selected at runtime through a
> > > control ? Or does the hardware design dictate whether the filter has
> > > to be used or must not be used ?
> >
> > This is dictated by the h/w design and not selectable at run-time.
> > I will update these changes in the next patchset.
> 
> In that case I'm fine with a property, but could we name it in such a way
> that it describes the hardware instead of instructing the software on how
> to configure the device ? For instance (and this is a made-up example as I
> don't know exactly how this works), if the AM Hi-Z filter is required when
> dealing with AM frequencies and forbidden when dealing with other
> frequency bands, and
> *if* boards have to be designed specifically for one frequency band (AM,
> FM, VHF, L, ...) without any way to accept different bands, then you could
> instead use
> 
> 	maxim,frequency-band = "AM";
> 
> and enable the filter accordingly in the driver. This would be in my
> opinion a better system hardware description.

I am not sure. The AM antenna input path has a default filter and AM Hi-Z filter. H/W dictates the path to be used for AM input only and this is fixed. The device can be configured to use different bands at runtime & not AM only. I could edit the description as below:

- maxim,am-hiz	      : empty property indicates AM Hi-Z filter path usage for AM antenna
			input as dictated by hardware design. By default this filter path is not used.

Is it any better? Do you still think the property name should be changed please?

Thanks,
Ramesh

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 5/7] doc_rst: media: New SDR formats PC16, PC18 & PC20
  2017-02-07 15:02 ` [PATCH v3 5/7] doc_rst: media: New " Ramesh Shanmugasundaram
@ 2017-04-11 12:39   ` Laurent Pinchart
  2017-04-18 17:13     ` Ramesh Shanmugasundaram
  0 siblings, 1 reply; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 12:39 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:35 Ramesh Shanmugasundaram wrote:
> This patch adds documentation for the three new SDR formats
> 
> V4L2_SDR_FMT_PCU16BE
> V4L2_SDR_FMT_PCU18BE
> V4L2_SDR_FMT_PCU20BE
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram@bp.renesas.com> ---
>  .../media/uapi/v4l/pixfmt-sdr-pcu16be.rst          | 55 +++++++++++++++++++
>  .../media/uapi/v4l/pixfmt-sdr-pcu18be.rst          | 55 +++++++++++++++++++
>  .../media/uapi/v4l/pixfmt-sdr-pcu20be.rst          | 54 +++++++++++++++++++
>  Documentation/media/uapi/v4l/sdr-formats.rst       |  3 ++
>  4 files changed, 167 insertions(+)
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
>  create mode 100644 Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> 
> diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
> b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst new file mode 100644
> index 0000000..2de1b1a
> --- /dev/null
> +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
> @@ -0,0 +1,55 @@
> +.. -*- coding: utf-8; mode: rst -*-
> +
> +.. _V4L2-SDR-FMT-PCU16BE:
> +
> +******************************
> +V4L2_SDR_FMT_PCU16BE ('PC16')
> +******************************
> +
> +Planar complex unsigned 16-bit big endian IQ sample
> +
> +Description
> +===========
> +
> +This format contains a sequence of complex number samples. Each complex
> +number consist of two parts called In-phase and Quadrature (IQ). Both I
> +and Q are represented as a 16 bit unsigned big endian number stored in
> +32 bit space. The remaining unused bits within the 32 bit space will be
> +padded with 0. I value starts first and Q value starts at an offset
> +equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
> +the 16 bits, bit 15:2 (14 bit) is data and bit 1:0 (2 bit) can be any
> +value.

This sounds very strange to me. Are the two lower bits always random ? What is 
that used for ?

> +**Byte Order.**
> +Each cell is one byte.
> +
> +.. flat-table::
> +    :header-rows:  1
> +    :stub-columns: 0
> +
> +    * -  Offset:
> +      -  Byte B0
> +      -  Byte B1
> +      -  Byte B2
> +      -  Byte B3
> +    * -  start + 0:
> +      -  I'\ :sub:`0[13:6]`
> +      -  I'\ :sub:`0[5:0]; B1[1:0]=pad`
> +      -  pad
> +      -  pad
> +    * -  start + 4:
> +      -  I'\ :sub:`1[13:6]`
> +      -  I'\ :sub:`1[5:0]; B1[1:0]=pad`
> +      -  pad
> +      -  pad
> +    * -  ...
> +    * - start + offset:
> +      -  Q'\ :sub:`0[13:6]`
> +      -  Q'\ :sub:`0[5:0]; B1[1:0]=pad`
> +      -  pad
> +      -  pad
> +    * - start + offset + 4:
> +      -  Q'\ :sub:`1[13:6]`
> +      -  Q'\ :sub:`1[5:0]; B1[1:0]=pad`
> +      -  pad
> +      -  pad
> diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
> b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst new file mode 100644
> index 0000000..da8b26b
> --- /dev/null
> +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
> @@ -0,0 +1,55 @@
> +.. -*- coding: utf-8; mode: rst -*-
> +
> +.. _V4L2-SDR-FMT-PCU18BE:
> +
> +******************************
> +V4L2_SDR_FMT_PCU18BE ('PC18')
> +******************************
> +
> +Planar complex unsigned 18-bit big endian IQ sample
> +
> +Description
> +===========
> +
> +This format contains a sequence of complex number samples. Each complex
> +number consist of two parts called In-phase and Quadrature (IQ). Both I
> +and Q are represented as a 18 bit unsigned big endian number stored in
> +32 bit space. The remaining unused bits within the 32 bit space will be
> +padded with 0. I value starts first and Q value starts at an offset
> +equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
> +the 18 bits, bit 17:2 (16 bit) is data and bit 1:0 (2 bit) can be any
> +value.
> +
> +**Byte Order.**
> +Each cell is one byte.
> +
> +.. flat-table::
> +    :header-rows:  1
> +    :stub-columns: 0
> +
> +    * -  Offset:
> +      -  Byte B0
> +      -  Byte B1
> +      -  Byte B2
> +      -  Byte B3
> +    * -  start + 0:
> +      -  I'\ :sub:`0[17:10]`
> +      -  I'\ :sub:`0[9:2]`
> +      -  I'\ :sub:`0[1:0]; B2[5:0]=pad`
> +      -  pad
> +    * -  start + 4:
> +      -  I'\ :sub:`1[17:10]`
> +      -  I'\ :sub:`1[9:2]`
> +      -  I'\ :sub:`1[1:0]; B2[5:0]=pad`
> +      -  pad
> +    * -  ...
> +    * - start + offset:
> +      -  Q'\ :sub:`0[17:10]`
> +      -  Q'\ :sub:`0[9:2]`
> +      -  Q'\ :sub:`0[1:0]; B2[5:0]=pad`
> +      -  pad
> +    * - start + offset + 4:
> +      -  Q'\ :sub:`1[17:10]`
> +      -  Q'\ :sub:`1[9:2]`
> +      -  Q'\ :sub:`1[1:0]; B2[5:0]=pad`
> +      -  pad
> diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst new file mode 100644
> index 0000000..5499eed
> --- /dev/null
> +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> @@ -0,0 +1,54 @@
> +.. -*- coding: utf-8; mode: rst -*-
> +.. _V4L2-SDR-FMT-PCU20BE:
> +
> +******************************
> +V4L2_SDR_FMT_PCU20BE ('PC20')
> +******************************
> +
> +Planar complex unsigned 20-bit big endian IQ sample
> +
> +Description
> +===========
> +
> +This format contains a sequence of complex number samples. Each complex
> +number consist of two parts called In-phase and Quadrature (IQ). Both I
> +and Q are represented as a 20 bit unsigned big endian number stored in
> +32 bit space. The remaining unused bits within the 32 bit space will be
> +padded with 0. I value starts first and Q value starts at an offset
> +equalling half of the buffer size (i.e.) offset = buffersize/2. Out of
> +the 20 bits, bit 19:2 (18 bit) is data and bit 1:0 (2 bit) can be any
> +value.
> +
> +**Byte Order.**
> +Each cell is one byte.
> +
> +.. flat-table::
> +    :header-rows:  1
> +    :stub-columns: 0
> +
> +    * -  Offset:
> +      -  Byte B0
> +      -  Byte B1
> +      -  Byte B2
> +      -  Byte B3
> +    * -  start + 0:
> +      -  I'\ :sub:`0[19:12]`
> +      -  I'\ :sub:`0[11:4]`
> +      -  I'\ :sub:`0[3:0]; B2[3:0]=pad`
> +      -  pad
> +    * -  start + 4:
> +      -  I'\ :sub:`1[19:12]`
> +      -  I'\ :sub:`1[11:4]`
> +      -  I'\ :sub:`1[3:0]; B2[3:0]=pad`
> +      -  pad
> +    * -  ...
> +    * - start + offset:
> +      -  Q'\ :sub:`0[19:12]`
> +      -  Q'\ :sub:`0[11:4]`
> +      -  Q'\ :sub:`0[3:0]; B2[3:0]=pad`
> +      -  pad
> +    * - start + offset + 4:
> +      -  Q'\ :sub:`1[19:12]`
> +      -  Q'\ :sub:`1[11:4]`
> +      -  Q'\ :sub:`1[3:0]; B2[3:0]=pad`
> +      -  pad
> diff --git a/Documentation/media/uapi/v4l/sdr-formats.rst
> b/Documentation/media/uapi/v4l/sdr-formats.rst index f863c08..2037f5b
> 100644
> --- a/Documentation/media/uapi/v4l/sdr-formats.rst
> +++ b/Documentation/media/uapi/v4l/sdr-formats.rst
> @@ -17,3 +17,6 @@ These formats are used for :ref:`SDR <sdr>` interface
> only. pixfmt-sdr-cs08
>      pixfmt-sdr-cs14le
>      pixfmt-sdr-ru12le
> +    pixfmt-sdr-pcu16be
> +    pixfmt-sdr-pcu18be
> +    pixfmt-sdr-pcu20be

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
  2017-04-11 12:19           ` Ramesh Shanmugasundaram
@ 2017-04-11 12:58                 ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 12:58 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt-DgEjT+Ai2ygdnm+yROfE0A, mark.rutland-5wv7dgnIgG8,
	mchehab-DgEjT+Ai2ygdnm+yROfE0A, hverkuil-qWit8jRvyhVmR6Xm/wNWPw,
	sakari.ailus-VuQAYsv1563Yd54FQh9/CA, crope-X3B1VOXEql0,
	Chris Paterson, geert+renesas-gXvu3+zWzMSzQB+pC5nmwQ,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-renesas-soc-u79uwXL29TY76Z2rM5mHXA

Hi Ramesh,

On Tuesday 11 Apr 2017 12:19:54 Ramesh Shanmugasundaram wrote:
> > On Tuesday 11 Apr 2017 09:57:45 Ramesh Shanmugasundaram wrote:
> >>> On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> >>>> Add device tree binding documentation for MAX2175 Rf to bits tuner
> >>>> device.
> >>>> 
> >>>> Signed-off-by: Ramesh Shanmugasundaram
> >>>> <ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
> >>>> ---
> >>>>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 +++++++++++++
> >>>>  .../devicetree/bindings/property-units.txt         |  1 +
> >>>>  2 files changed, 62 insertions(+)
> >>>>  create mode 100644
> >>>> 
> >>>> Documentation/devicetree/bindings/media/i2c/max2175.txt
> >>>> diff --git
> >>>> a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> >>>> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> >>>> mode 100644 index 0000000..f591ab4
> >>>> --- /dev/null
> >>>> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
> > 
> > [snip]
> > 
> >>>> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter
> >>>> path is
> >>>> +			selected for AM antenna input. By default this
> >>>> +			filter path is not used.
> >>> 
> >>> Isn't this something that should be selected at runtime through a
> >>> control ? Or does the hardware design dictate whether the filter has
> >>> to be used or must not be used ?
> >> 
> >> This is dictated by the h/w design and not selectable at run-time.
> >> I will update these changes in the next patchset.
> > 
> > In that case I'm fine with a property, but could we name it in such a way
> > that it describes the hardware instead of instructing the software on how
> > to configure the device ? For instance (and this is a made-up example as I
> > don't know exactly how this works), if the AM Hi-Z filter is required when
> > dealing with AM frequencies and forbidden when dealing with other
> > frequency bands, and *if* boards have to be designed specifically for one
> > frequency band (AM, FM, VHF, L, ...) without any way to accept different
> > bands, then you could instead use
> > 
> > 	maxim,frequency-band = "AM";
> > 
> > and enable the filter accordingly in the driver. This would be in my
> > opinion a better system hardware description.
> 
> I am not sure. The AM antenna input path has a default filter and AM Hi-Z
> filter. H/W dictates the path to be used for AM input only and this is
> fixed. The device can be configured to use different bands at runtime & not
> AM only. I could edit the description as below:
> 
> - maxim,am-hiz	      : empty property indicates AM Hi-Z filter path
> usage for AM antenna input as dictated by hardware design. By default this
> filter path is not used.
> 
> Is it any better? Do you still think the property name should be changed
> please?

I still think this should be renamed, but possibly because I don't understand 
all the details of this particular feature :-). The property, as named and 
documented above, describes a software features. It requests the driver to 
enable the AM Hi-Z filter. DT properties should instead describe the hardware. 
You should use a property that describes the hardware design, and use that to 
infer, in the driver, whether to enable or disable the filter.

-- 
Regards,

Laurent Pinchart

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description
@ 2017-04-11 12:58                 ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 12:58 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

On Tuesday 11 Apr 2017 12:19:54 Ramesh Shanmugasundaram wrote:
> > On Tuesday 11 Apr 2017 09:57:45 Ramesh Shanmugasundaram wrote:
> >>> On Tuesday 07 Feb 2017 15:02:32 Ramesh Shanmugasundaram wrote:
> >>>> Add device tree binding documentation for MAX2175 Rf to bits tuner
> >>>> device.
> >>>> 
> >>>> Signed-off-by: Ramesh Shanmugasundaram
> >>>> <ramesh.shanmugasundaram@bp.renesas.com>
> >>>> ---
> >>>>  .../devicetree/bindings/media/i2c/max2175.txt      | 61 +++++++++++++
> >>>>  .../devicetree/bindings/property-units.txt         |  1 +
> >>>>  2 files changed, 62 insertions(+)
> >>>>  create mode 100644
> >>>> 
> >>>> Documentation/devicetree/bindings/media/i2c/max2175.txt
> >>>> diff --git
> >>>> a/Documentation/devicetree/bindings/media/i2c/max2175.txt
> >>>> b/Documentation/devicetree/bindings/media/i2c/max2175.txt new file
> >>>> mode 100644 index 0000000..f591ab4
> >>>> --- /dev/null
> >>>> +++ b/Documentation/devicetree/bindings/media/i2c/max2175.txt
> > 
> > [snip]
> > 
> >>>> +- maxim,am-hiz	      : empty property indicates AM Hi-Z filter
> >>>> path is
> >>>> +			selected for AM antenna input. By default this
> >>>> +			filter path is not used.
> >>> 
> >>> Isn't this something that should be selected at runtime through a
> >>> control ? Or does the hardware design dictate whether the filter has
> >>> to be used or must not be used ?
> >> 
> >> This is dictated by the h/w design and not selectable at run-time.
> >> I will update these changes in the next patchset.
> > 
> > In that case I'm fine with a property, but could we name it in such a way
> > that it describes the hardware instead of instructing the software on how
> > to configure the device ? For instance (and this is a made-up example as I
> > don't know exactly how this works), if the AM Hi-Z filter is required when
> > dealing with AM frequencies and forbidden when dealing with other
> > frequency bands, and *if* boards have to be designed specifically for one
> > frequency band (AM, FM, VHF, L, ...) without any way to accept different
> > bands, then you could instead use
> > 
> > 	maxim,frequency-band = "AM";
> > 
> > and enable the filter accordingly in the driver. This would be in my
> > opinion a better system hardware description.
> 
> I am not sure. The AM antenna input path has a default filter and AM Hi-Z
> filter. H/W dictates the path to be used for AM input only and this is
> fixed. The device can be configured to use different bands at runtime & not
> AM only. I could edit the description as below:
> 
> - maxim,am-hiz	      : empty property indicates AM Hi-Z filter path
> usage for AM antenna input as dictated by hardware design. By default this
> filter path is not used.
> 
> Is it any better? Do you still think the property name should be changed
> please?

I still think this should be renamed, but possibly because I don't understand 
all the details of this particular feature :-). The property, as named and 
documented above, describes a software features. It requests the driver to 
enable the AM Hi-Z filter. DT properties should instead describe the hardware. 
You should use a property that describes the hardware design, and use that to 
infer, in the driver, whether to enable or disable the filter.

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support
  2017-02-07 15:02 ` [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support Ramesh Shanmugasundaram
@ 2017-04-11 22:24       ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 22:24 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt-DgEjT+Ai2ygdnm+yROfE0A, mark.rutland-5wv7dgnIgG8,
	mchehab-DgEjT+Ai2ygdnm+yROfE0A, hverkuil-qWit8jRvyhVmR6Xm/wNWPw,
	sakari.ailus-VuQAYsv1563Yd54FQh9/CA, crope-X3B1VOXEql0,
	chris.paterson2-zM6kxYcvzFBBDgjK7y7TUQ,
	geert+renesas-gXvu3+zWzMSzQB+pC5nmwQ,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-renesas-soc-u79uwXL29TY76Z2rM5mHXA

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:37 Ramesh Shanmugasundaram wrote:
> This patch adds Digital Radio Interface (DRIF) support to R-Car Gen3 SoCs.
> The driver exposes each instance of DRIF as a V4L2 SDR device. A DRIF
> device represents a channel and each channel can have one or two
> sub-channels respectively depending on the target board.
> 
> DRIF supports only Rx functionality. It receives samples from a RF
> frontend tuner chip it is interfaced with. The combination of DRIF and the
> tuner device, which is registered as a sub-device, determines the receive
> sample rate and format.
> 
> In order to be compliant as a V4L2 SDR device, DRIF needs to bind with
> the tuner device, which can be provided by a third party vendor. DRIF acts
> as a slave device and the tuner device acts as a master transmitting the
> samples. The driver allows asynchronous binding of a tuner device that
> is registered as a v4l2 sub-device. The driver can learn about the tuner
> it is interfaced with based on port endpoint properties of the device in
> device tree. The V4L2 SDR device inherits the controls exposed by the
> tuner device.
> 
> The device can also be configured to use either one or both of the data
> pins at runtime based on the master (tuner) configuration.
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
> ---
>  drivers/media/platform/Kconfig     |   25 +
>  drivers/media/platform/Makefile    |    1 +
>  drivers/media/platform/rcar_drif.c | 1534 +++++++++++++++++++++++++++++++++
>  3 files changed, 1560 insertions(+)
>  create mode 100644 drivers/media/platform/rcar_drif.c

[snip]

> diff --git a/drivers/media/platform/rcar_drif.c
> b/drivers/media/platform/rcar_drif.c new file mode 100644
> index 0000000..88950e3
> --- /dev/null
> +++ b/drivers/media/platform/rcar_drif.c
> @@ -0,0 +1,1534 @@

[snip]

> +/*
> + * The R-Car DRIF is a receive only MSIOF like controller with an
> + * external master device driving the SCK. It receives data into a FIFO,
> + * then this driver uses the SYS-DMAC engine to move the data from
> + * the device to memory.
> + *
> + * Each DRIF channel DRIFx (as per datasheet) contains two internal
> + * channels DRIFx0 & DRIFx1 within itself with each having its own
> resources
> + * like module clk, register set, irq and dma. These internal channels
> share
> + * common CLK & SYNC from master. The two data pins D0 & D1 shall be
> + * considered to represent the two internal channels. This internal split
> + * is not visible to the master device.
> + *
> + * Depending on the master device, a DRIF channel can use
> + *  (1) both internal channels (D0 & D1) to receive data in parallel (or)
> + *  (2) one internal channel (D0 or D1) to receive data
> + *
> + * The primary design goal of this controller is to act as Digitial Radio

s/Digitial/Digital/

> + * Interface that receives digital samples from a tuner device. Hence the
> + * driver exposes the device as a V4L2 SDR device. In order to qualify as
> + * a V4L2 SDR device, it should possess tuner interface as mandated by the
> + * framework. This driver expects a tuner driver (sub-device) to bind
> + * asynchronously with this device and the combined drivers shall expose
> + * a V4L2 compliant SDR device. The DRIF driver is independent of the
> + * tuner vendor.
> + *
> + * The DRIF h/w can support I2S mode and Frame start synchronization pulse
> mode.
> + * This driver is tested for I2S mode only because of the availability of
> + * suitable master devices. Hence, not all configurable options of DRIF h/w
> + * like lsb/msb first, syncdl, dtdl etc. are exposed via DT and I2S
> defaults
> + * are used. These can be exposed later if needed after testing.
> + */

[snip]

> +#define to_rcar_drif_buf_pair(sdr, ch_num,
> idx)	(sdr->ch[!(ch_num)]->buf[idx])

You should enclose both sdr and idx in parenthesis, as they can be 
expressions.

> +
> +#define for_each_rcar_drif_channel(ch, ch_mask)			\
> +	for_each_set_bit(ch, ch_mask, RCAR_DRIF_MAX_CHANNEL)
> +
> +static const unsigned int num_hwbufs = 32;

Is there a specific reason to make this a static const instead of a #define ?

> +/* Debug */
> +static unsigned int debug;
> +module_param(debug, uint, 0644);
> +MODULE_PARM_DESC(debug, "activate debug info");
> +
> +#define rdrif_dbg(level, sdr, fmt, arg...)				\
> +	v4l2_dbg(level, debug, &sdr->v4l2_dev, fmt, ## arg)
> +
> +#define rdrif_err(sdr, fmt, arg...)					\
> +	dev_err(sdr->v4l2_dev.dev, fmt, ## arg)
> +
> +/* Stream formats */
> +struct rcar_drif_format {
> +	u32	pixelformat;
> +	u32	buffersize;
> +	u32	wdlen;
> +	u32	num_ch;
> +};
> +
> +/* Format descriptions for capture */
> +static const struct rcar_drif_format formats[] = {
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU16BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 16,
> +		.num_ch	= 2,

How about aligning the = as in the other lines ?

num_ch is always set to 2. Should we remove it for now, and add it back later 
when we'll support single-channel formats ? I think we should avoid carrying 
dead code.

> +	},
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU18BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 18,
> +		.num_ch	= 2,
> +	},
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU20BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 20,
> +		.num_ch	= 2,
> +	},
> +};
> +
> +static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);

Same question here, can't this be a define ? I think I'd even avoid 
NUM_FORMATS completely and use ARRAY_SIZE(formats) directly in the code, to 
make the boundary check more explicit when iterating over the array.

> +
> +/* Buffer for a received frame from one or both internal channels */
> +struct rcar_drif_frame_buf {
> +	/* Common v4l buffer stuff -- must be first */
> +	struct vb2_v4l2_buffer vb;
> +	struct list_head list;
> +};
> +
> +struct rcar_drif_async_subdev {
> +	struct v4l2_subdev *sd;
> +	struct v4l2_async_subdev asd;
> +};
> +
> +/* DMA buffer */
> +struct rcar_drif_hwbuf {
> +	void *addr;			/* CPU-side address */
> +	unsigned int status;		/* Buffer status flags */
> +};
> +
> +/* Internal channel */
> +struct rcar_drif {
> +	struct rcar_drif_sdr *sdr;	/* Group device */
> +	struct platform_device *pdev;	/* Channel's pdev */
> +	void __iomem *base;		/* Base register address */
> +	resource_size_t start;		/* I/O resource offset */
> +	struct dma_chan *dmach;		/* Reserved DMA channel */
> +	struct clk *clkp;		/* Module clock */
> +	struct rcar_drif_hwbuf *buf[RCAR_DRIF_MAX_NUM_HWBUFS]; /* H/W bufs */
> +	dma_addr_t dma_handle;		/* Handle for all bufs */
> +	unsigned int num;		/* Channel number */
> +	bool acting_sdr;		/* Channel acting as SDR device */
> +};
> +
> +/* DRIF V4L2 SDR */
> +struct rcar_drif_sdr {
> +	struct device *dev;		/* Platform device */
> +	struct video_device *vdev;	/* V4L2 SDR device */
> +	struct v4l2_device v4l2_dev;	/* V4L2 device */
> +
> +	/* Videobuf2 queue and queued buffers list */
> +	struct vb2_queue vb_queue;
> +	struct list_head queued_bufs;
> +	spinlock_t queued_bufs_lock;	/* Protects queued_bufs */
> +
> +	struct mutex v4l2_mutex;	/* To serialize ioctls */
> +	struct mutex vb_queue_mutex;	/* To serialize streaming ioctls */
> +	struct v4l2_ctrl_handler ctrl_hdl;	/* SDR control handler */
> +	struct v4l2_async_notifier notifier;	/* For subdev (tuner) */
> +
> +	/* Current V4L2 SDR format array index */
> +	unsigned int fmt_idx;

Instead of storing the index I would store a pointer to the corresponding 
rcar_drif_format, looking up information about the current format will then be 
easier.

> +
> +	/* Device tree SYNC properties */
> +	u32 mdr1;
> +
> +	/* Internals */
> +	struct rcar_drif *ch[RCAR_DRIF_MAX_CHANNEL]; /* DRIFx0,1 */
> +	unsigned long hw_ch_mask;	/* Enabled channels per DT */
> +	unsigned long cur_ch_mask;	/* Used channels for an SDR FMT */
> +	u32 num_hw_ch;			/* Num of DT enabled channels */
> +	u32 num_cur_ch;			/* Num of used channels */
> +	u32 hwbuf_size;			/* Each DMA buffer size */
> +	u32 produced;			/* Buffers produced by sdr dev */
> +};
> +
> +/* Allocate buffer context */
> +static int rcar_drif_alloc_bufctxt(struct rcar_drif_sdr *sdr)
> +{
> +	struct rcar_drif_hwbuf *bufctx;
> +	unsigned int i, idx;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		bufctx = kcalloc(num_hwbufs, sizeof(*bufctx), GFP_KERNEL);

How about embedding the buffer contexts in the rcar_drif structure instead of 
just storing pointers there ? The rcar_drif_hwbuf structure is pretty small, 
it won't make a big difference, and will simplify the code.

> +		if (!bufctx)
> +			return -ENOMEM;
> +
> +		for (idx = 0; idx < num_hwbufs; idx++)
> +			sdr->ch[i]->buf[idx] = bufctx + idx;
> +	}
> +	return 0;
> +}

[snip]

> +/* Release DMA channel */
> +static void rcar_drif_release_dmachannel(struct rcar_drif_sdr *sdr)

I would name the function rcar_drif_release_dma_channels as it handles all 
channels. Same for rcar_drif_alloc_dma_channels.

> +{
> +	unsigned int i;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
> +		if (sdr->ch[i]->dmach) {
> +			dma_release_channel(sdr->ch[i]->dmach);
> +			sdr->ch[i]->dmach = NULL;
> +		}
> +}
> +
> +/* Allocate DMA channel */
> +static int rcar_drif_alloc_dmachannel(struct rcar_drif_sdr *sdr)
> +{
> +	struct dma_slave_config dma_cfg;
> +	unsigned int i;
> +	int ret = -ENODEV;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		ch->dmach = dma_request_slave_channel(&ch->pdev->dev, "rx");
> +		if (!ch->dmach) {
> +			rdrif_err(sdr, "ch%u: dma channel req failed\n", i);
> +			goto dmach_error;
> +		}
> +
> +		/* Configure slave */
> +		memset(&dma_cfg, 0, sizeof(dma_cfg));
> +		dma_cfg.src_addr = (phys_addr_t)(ch->start + 
RCAR_DRIF_SIRFDR);
> +		dma_cfg.dst_addr = 0;

This isn't needed as you memset the whole structure to 0.

> +		dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +		ret = dmaengine_slave_config(ch->dmach, &dma_cfg);
> +		if (ret) {
> +			rdrif_err(sdr, "ch%u: dma slave config failed\n", i);
> +			goto dmach_error;
> +		}
> +	}
> +	return 0;
> +
> +dmach_error:
> +	rcar_drif_release_dmachannel(sdr);
> +	return ret;
> +}

[snip]

> +/* Set MDR defaults */
> +static inline void rcar_drif_set_mdr1(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +
> +	/* Set defaults for enabled internal channels */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		/* Refer MSIOF section in manual for this register setting */
> +		writel(RCAR_DRIF_SITMDR1_PCON,
> +		       sdr->ch[i]->base + RCAR_DRIF_SITMDR1);

I would create a rcar_drif_write(struct rcar_drif *ch, u32 offset, u32 data) 
function, the code will become clearer. Same for the read operation.

> +		/* Setup MDR1 value */
> +		writel(sdr->mdr1, sdr->ch[i]->base + RCAR_DRIF_SIRMDR1);
> +
> +		rdrif_dbg(2, sdr, "ch%u: mdr1 = 0x%08x",
> +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR1));

Once you've debugged the driver I'm not sure those debugging statements are 
still needed.

> +	}
> +}
> +
> +/* Extract bitlen and wdcnt from given word length */
> +static int rcar_drif_convert_wdlen(struct rcar_drif_sdr *sdr,
> +				   u32 wdlen, u32 *bitlen, u32 *wdcnt)
> +{
> +	unsigned int i, nr_wds;
> +
> +	/* FIFO register size is 32 bits */
> +	for (i = 0; i < 32; i++) {
> +		nr_wds = wdlen % (32 - i);
> +		if (nr_wds == 0) {
> +			*bitlen = 32 - i;
> +			*wdcnt = wdlen / *bitlen;

Can't you store the bitlen and wdcnt values in the rcar_drif_format structure 
instead of recomputing them every time ?

> +			break;
> +		}
> +	}
> +
> +	/* Sanity check range */
> +	if (i == 32 || !(*bitlen >= 8 && *bitlen <= 32) ||
> +	    !(*wdcnt >= 1 && *wdcnt <= 64)) {
> +		rdrif_err(sdr, "invalid wdlen %u configured\n", wdlen);
> +		return -EINVAL;

You shouldn't have invalid wdlen values in the driver. I would remove this 
check as it makes error handling in the caller more complex.

> +	}
> +
> +	return 0;
> +}
> +
> +/* Set DRIF receive format */
> +static int rcar_drif_set_format(struct rcar_drif_sdr *sdr)
> +{
> +	u32 bitlen, wdcnt, wdlen;
> +	unsigned int i;
> +	int ret = -EINVAL;
> +
> +	wdlen = formats[sdr->fmt_idx].wdlen;
> +	rdrif_dbg(2, sdr, "setfmt: idx %u, wdlen %u, num_ch %u\n",
> +		  sdr->fmt_idx, wdlen, formats[sdr->fmt_idx].num_ch);
> +
> +	/* Sanity check */
> +	if (formats[sdr->fmt_idx].num_ch > sdr->num_cur_ch) {
> +		rdrif_err(sdr, "fmt idx %u current ch %u mismatch\n",
> +			  sdr->fmt_idx, sdr->num_cur_ch);
> +		return ret;

This should never happen, it should be caught at set format time.

> +	}
> +
> +	/* Get bitlen & wdcnt from wdlen */
> +	ret = rcar_drif_convert_wdlen(sdr, wdlen, &bitlen, &wdcnt);
> +	if (ret)
> +		return ret;
> +
> +	/* Setup group, bitlen & wdcnt */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		u32 mdr;
> +
> +		/* Two groups */
> +		mdr = RCAR_DRIF_MDR_GRPCNT(2) | RCAR_DRIF_MDR_BITLEN(bitlen) |
> +		       RCAR_DRIF_MDR_WDCNT(wdcnt);
> +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR2);
> +
> +		mdr = RCAR_DRIF_MDR_BITLEN(bitlen) | 
RCAR_DRIF_MDR_WDCNT(wdcnt);
> +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR3);
> +
> +		rdrif_dbg(2, sdr, "ch%u: new mdr[2,3] = 0x%08x, 0x%08x\n",
> +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR2),
> +			  readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR3));
> +	}
> +	return ret;
> +}
> +
> +/* Release DMA buffers */
> +static void rcar_drif_release_buf(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		/* First entry contains the dma buf ptr */
> +		if (ch->buf[0] && ch->buf[0]->addr) {
> +			dma_free_coherent(&ch->pdev->dev,
> +					  sdr->hwbuf_size * num_hwbufs,
> +					  ch->buf[0]->addr, ch->dma_handle);
> +			ch->buf[0]->addr = NULL;
> +		}
> +	}
> +}
> +
> +/* Request DMA buffers */
> +static int rcar_drif_request_buf(struct rcar_drif_sdr *sdr)
> +{
> +	int ret = -ENOMEM;
> +	unsigned int i, j;
> +	void *addr;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		/* Allocate DMA buffers */
> +		addr = dma_alloc_coherent(&ch->pdev->dev,
> +					  sdr->hwbuf_size * num_hwbufs,
> +					  &ch->dma_handle, GFP_KERNEL);
> +		if (!addr) {
> +			rdrif_err(sdr,
> +			"ch%u: dma alloc failed. num_hwbufs %u size %u\n",
> +			i, num_hwbufs, sdr->hwbuf_size);
> +			goto alloc_error;
> +		}
> +
> +		/* Split the chunk and populate bufctxt */
> +		for (j = 0; j < num_hwbufs; j++) {
> +			ch->buf[j]->addr = addr + (j * sdr->hwbuf_size);
> +			ch->buf[j]->status = 0;
> +		}
> +	}
> +
> +	return 0;
> +
> +alloc_error:
> +	return ret;
> +}
> +
> +/* Setup vb_queue minimum buffer requirements */
> +static int rcar_drif_queue_setup(struct vb2_queue *vq,
> +			unsigned int *num_buffers, unsigned int *num_planes,
> +			unsigned int sizes[], struct device *alloc_devs[])
> +{
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> +
> +	/* Need at least 16 buffers */
> +	if (vq->num_buffers + *num_buffers < 16)
> +		*num_buffers = 16 - vq->num_buffers;
> +
> +	*num_planes = 1;
> +	sizes[0] = PAGE_ALIGN(formats[sdr->fmt_idx].buffersize);
> +
> +	rdrif_dbg(2, sdr, "num_bufs %d sizes[0] %d\n", *num_buffers, 
sizes[0]);
> +	return 0;
> +}
> +
> +/* Enqueue buffer */
> +static void rcar_drif_buf_queue(struct vb2_buffer *vb)
> +{
> +	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vb->vb2_queue);
> +	struct rcar_drif_frame_buf *fbuf =
> +			container_of(vbuf, struct rcar_drif_frame_buf, vb);
> +	unsigned long flags;
> +
> +	rdrif_dbg(2, sdr, "buf_queue idx %u\n", vb->index);
> +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> +	list_add_tail(&fbuf->list, &sdr->queued_bufs);
> +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +}
> +
> +/* Get a frame buf from list */
> +static struct rcar_drif_frame_buf *
> +rcar_drif_get_fbuf(struct rcar_drif_sdr *sdr)
> +{
> +	struct rcar_drif_frame_buf *fbuf;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> +	fbuf = list_first_entry_or_null(&sdr->queued_bufs, struct
> +					rcar_drif_frame_buf, list);
> +	if (!fbuf) {
> +		/*
> +		 * App is late in enqueing buffers. Samples lost & there will
> +		 * be a gap in sequence number when app recovers
> +		 */
> +		rdrif_dbg(1, sdr, "\napp late: prod %u\n", sdr->produced);
> +		sdr->produced++; /* Increment the produced count anyway */
> +		spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +		return NULL;
> +	}
> +	list_del(&fbuf->list);
> +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +
> +	return fbuf;
> +}
> +
> +static inline bool rcar_drif_buf_pairs_done(struct rcar_drif_hwbuf *buf1,
> +					    struct rcar_drif_hwbuf *buf2)
> +{
> +	return (buf1->status & buf2->status & RCAR_DRIF_BUF_DONE);
> +}
> +
> +/* Channel DMA complete */
> +static void rcar_drif_channel_complete(struct rcar_drif *ch, u32 idx)
> +{
> +	u32 str;
> +
> +	ch->buf[idx]->status |= RCAR_DRIF_BUF_DONE;
> +
> +	/* Check for DRIF errors */
> +	str = readl(ch->base + RCAR_DRIF_SISTR);
> +	if (unlikely(str & RCAR_DRIF_RFOVF)) {
> +		/* Writing the same clears it */
> +		writel(str, ch->base + RCAR_DRIF_SISTR);
> +
> +		/* Overflow: some samples are lost */
> +		ch->buf[idx]->status |= RCAR_DRIF_BUF_OVERFLOW;
> +	}
> +}
> +
> +/* Deliver buffer to user */
> +static void rcar_drif_deliver_buf(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	u32 idx = sdr->produced % num_hwbufs;
> +	struct rcar_drif_frame_buf *fbuf;
> +	bool overflow = false;
> +
> +	rcar_drif_channel_complete(ch, idx);
> +
> +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL) {
> +		struct rcar_drif_hwbuf *bufi, *bufq;
> +
> +		if (ch->num) {
> +			bufi = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> +			bufq = ch->buf[idx];
> +		} else {
> +			bufi = ch->buf[idx];
> +			bufq = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> +		}
> +
> +		/* Check if both DMA buffers are done */
> +		if (!rcar_drif_buf_pairs_done(bufi, bufq))
> +			return;
> +
> +		/* Clear buf done status */
> +		bufi->status &= ~RCAR_DRIF_BUF_DONE;
> +		bufq->status &= ~RCAR_DRIF_BUF_DONE;
> +
> +		/* Get fbuf */
> +		fbuf = rcar_drif_get_fbuf(sdr);
> +		if (!fbuf)
> +			return;
> +
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> +		       bufi->addr, sdr->hwbuf_size);
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0) + sdr-
>hwbuf_size,
> +		       bufq->addr, sdr->hwbuf_size);

Ouch ! That's a high data rate memcpy that can be avoided. Why don't you DMA 
directly to the vb2 buffers ? You will need to use videobuf2-dma-contig 
instead of videobuf2-vmalloc, but apart from that there should be no issue.

> +		if ((bufi->status | bufq->status) & RCAR_DRIF_BUF_OVERFLOW) {
> +			overflow = true;
> +			/* Clear the flag in status */
> +			bufi->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +			bufq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +		}
> +	} else {
> +		struct rcar_drif_hwbuf *bufiq;
> +
> +		/* Get fbuf */
> +		fbuf = rcar_drif_get_fbuf(sdr);
> +		if (!fbuf)
> +			return;
> +
> +		bufiq = ch->buf[idx];
> +
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> +		       bufiq->addr, sdr->hwbuf_size);
> +
> +		if (bufiq->status & RCAR_DRIF_BUF_OVERFLOW) {
> +			overflow = true;
> +			/* Clear the flag in status */
> +			bufiq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +		}
> +	}
> +
> +	rdrif_dbg(2, sdr, "ch%u: prod %u\n", ch->num, sdr->produced);
> +
> +	fbuf->vb.field = V4L2_FIELD_NONE;
> +	fbuf->vb.sequence = sdr->produced++;
> +	fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
> +	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
> +			      formats[sdr->fmt_idx].buffersize);
> +
> +	/* Set error state on overflow */
> +	if (overflow)
> +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> +	else
> +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);

Maybe

	vb2_buffer_done(&fbuf->vb.vb2_buf,
			overflow ? VB2_BUF_STATE_ERROR: VB2_BUF_STATE_DONE);

> +}
> +
> +/* DMA callback for each stage */
> +static void rcar_drif_dma_complete(void *dma_async_param)
> +{
> +	struct rcar_drif *ch = dma_async_param;
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +
> +	mutex_lock(&sdr->vb_queue_mutex);

Isn't the complete callback potentially called in interrupt context ? I know 
the rcar-dmac driver uses a threaded interrupt handler for that, but is that a 
guarantee of the DMA engine API ?

> +
> +	/* DMA can be terminated while the callback was waiting on lock */
> +	if (!vb2_is_streaming(&sdr->vb_queue))

Can it ? The streaming flag is cleared after the stop_streaming operation is 
called, which will terminate all DMA transfers synchronously.

> +		goto stopped;
> +
> +	rcar_drif_deliver_buf(ch);
> +stopped:
> +	mutex_unlock(&sdr->vb_queue_mutex);
> +}
> +
> +static int rcar_drif_qbuf(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	dma_addr_t addr = ch->dma_handle;
> +	struct dma_async_tx_descriptor *rxd;
> +	dma_cookie_t cookie;
> +	int ret = -EIO;
> +
> +	/* Setup cyclic DMA with given buffers */
> +	rxd = dmaengine_prep_dma_cyclic(ch->dmach, addr,
> +					sdr->hwbuf_size * num_hwbufs,
> +					sdr->hwbuf_size, DMA_DEV_TO_MEM,
> +					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> +	if (!rxd) {
> +		rdrif_err(sdr, "ch%u: prep dma cyclic failed\n", ch->num);
> +		return ret;
> +	}
> +
> +	/* Submit descriptor */
> +	rxd->callback = rcar_drif_dma_complete;
> +	rxd->callback_param = ch;
> +	cookie = dmaengine_submit(rxd);
> +	if (dma_submit_error(cookie)) {
> +		rdrif_err(sdr, "ch%u: dma submit failed\n", ch->num);
> +		return ret;
> +	}
> +
> +	dma_async_issue_pending(ch->dmach);
> +	return 0;
> +}
> +
> +/* Enable reception */
> +static int rcar_drif_enable_rx(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	u32 ctr;
> +	int ret;
> +
> +	/*
> +	 * When both internal channels are enabled, they can be synchronized
> +	 * only by the master
> +	 */
> +
> +	/* Enable receive */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +		ctr |= (RCAR_DRIF_SICTR_RX_RISING_EDGE |
> +			 RCAR_DRIF_SICTR_RX_EN);
> +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +	}
> +
> +	/* Check receive enabled */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> +					 ctr, ctr & RCAR_DRIF_SICTR_RX_EN,
> +					 2, 500000);

A 2µs sleep for a 500ms total timeout seems very low to me, that will stress 
the CPU. Same comment for the other locations where you use 
readl_poll_timeout.

How long does the channel typically take to get enabled ?

> +		if (ret) {
> +			rdrif_err(sdr, "ch%u: rx en failed. ctr 0x%08x\n",
> +				  i, readl(sdr->ch[i]->base + 
RCAR_DRIF_SICTR));
> +			break;
> +		}
> +	}
> +	return ret;
> +}
> +
> +/* Disable reception */
> +static void rcar_drif_disable_rx(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	u32 ctr;
> +	int ret;
> +
> +	/* Disable receive */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +		ctr &= ~RCAR_DRIF_SICTR_RX_EN;
> +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +	}
> +
> +	/* Check receive disabled */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> +					 ctr, !(ctr & RCAR_DRIF_SICTR_RX_EN),
> +					 2, 500000);

How long does the channel typically take to get disabled ?

> +		if (ret)
> +			dev_warn(&sdr->vdev->dev,
> +			"ch%u: failed to disable rx. ctr 0x%08x\n",
> +			i, readl(sdr->ch[i]->base + RCAR_DRIF_SICTR));
> +	}
> +}
> +
> +/* Start channel */
> +static int rcar_drif_start_channel(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	u32 ctr, str;
> +	int ret;
> +
> +	/* Reset receive */
> +	writel(RCAR_DRIF_SICTR_RESET, ch->base + RCAR_DRIF_SICTR);
> +	ret = readl_poll_timeout(ch->base + RCAR_DRIF_SICTR,
> +					 ctr, !(ctr & RCAR_DRIF_SICTR_RESET),

The alignment is weird.

> +					 2, 500000);
> +	if (ret) {
> +		rdrif_err(sdr, "ch%u: failed to reset rx. ctr 0x%08x\n",
> +			  ch->num, readl(ch->base + RCAR_DRIF_SICTR));
> +		return ret;
> +	}
> +
> +	/* Queue buffers for DMA */
> +	ret = rcar_drif_qbuf(ch);
> +	if (ret)
> +		return ret;
> +
> +	/* Clear status register flags */
> +	str = RCAR_DRIF_RFFUL | RCAR_DRIF_REOF | RCAR_DRIF_RFSERR |
> +		RCAR_DRIF_RFUDF | RCAR_DRIF_RFOVF;
> +	writel(str, ch->base + RCAR_DRIF_SISTR);
> +
> +	/* Enable DMA receive interrupt */
> +	writel(0x00009000, ch->base + RCAR_DRIF_SIIER);
> +
> +	return ret;
> +}
> +
> +/* Start receive operation */
> +static int rcar_drif_start(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	int ret;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = rcar_drif_start_channel(sdr->ch[i]);
> +		if (ret)
> +			goto start_error;
> +	}
> +
> +	sdr->produced = 0;
> +	ret = rcar_drif_enable_rx(sdr);
> +start_error:

Don't you need to stop the channels that were successfully started if an error 
occurs ?

> +	return ret;
> +}
> +
> +/* Stop channel */
> +static void rcar_drif_stop_channel(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	int ret, retries = 3;
> +
> +	/* Disable DMA receive interrupt */
> +	writel(0x00000000, ch->base + RCAR_DRIF_SIIER);
> +
> +	do {
> +		/* Terminate all DMA transfers */
> +		ret = dmaengine_terminate_sync(ch->dmach);
> +		if (!ret)
> +			break;
> +		rdrif_dbg(2, sdr, "stop retry\n");
> +	} while (--retries);

Why do you need to retry the terminate operation, why does it fail ?

> +	WARN_ON(!retries);
> +}

[snip]

> +/* Start streaming */
> +static int rcar_drif_start_streaming(struct vb2_queue *vq, unsigned int
> count)
> +{
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> +	unsigned int i, j;
> +	int ret;
> +
> +	mutex_lock(&sdr->v4l2_mutex);

I'm surprised, aren't the start_streaming and stop_streaming operations called 
with the video device lock held already by the v4l2-ioctl layer ? I think they 
should be, if they're not there's probably a bug somewhere.

> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = clk_prepare_enable(sdr->ch[i]->clkp);
> +		if (ret)
> +			goto start_error;
> +	}
> +
> +	/* Set default MDRx settings */
> +	rcar_drif_set_mdr1(sdr);
> +
> +	/* Set new format */
> +	ret = rcar_drif_set_format(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL)
> +		sdr->hwbuf_size =
> +		formats[sdr->fmt_idx].buffersize / RCAR_DRIF_MAX_CHANNEL;
> +	else
> +		sdr->hwbuf_size = formats[sdr->fmt_idx].buffersize;
> +
> +	rdrif_dbg(1, sdr, "num_hwbufs %u, hwbuf_size %u\n",
> +		num_hwbufs, sdr->hwbuf_size);
> +
> +	/* Alloc DMA channel */
> +	ret = rcar_drif_alloc_dmachannel(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Alloc buf context */
> +	ret = rcar_drif_alloc_bufctxt(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Request buffers */
> +	ret = rcar_drif_request_buf(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Start Rx */
> +	ret = rcar_drif_start(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	mutex_unlock(&sdr->v4l2_mutex);
> +	rdrif_dbg(1, sdr, "started\n");
> +	return ret;
> +
> +start_error:

As there's a single error label I would call this "error". Up to you.

> +	rcar_drif_release_queued_bufs(sdr, VB2_BUF_STATE_QUEUED);
> +	rcar_drif_release_buf(sdr);
> +	rcar_drif_release_bufctxt(sdr);
> +	rcar_drif_release_dmachannel(sdr);
> +	for (j = 0; j < i; j++)
> +		clk_disable_unprepare(sdr->ch[j]->clkp);
> +
> +	mutex_unlock(&sdr->v4l2_mutex);
> +	return ret;
> +}

[snip]

> +/* Vb2 ops */
> +static struct vb2_ops rcar_drif_vb2_ops = {

You can make this static const.

> +	.queue_setup            = rcar_drif_queue_setup,
> +	.buf_queue              = rcar_drif_buf_queue,
> +	.start_streaming        = rcar_drif_start_streaming,
> +	.stop_streaming         = rcar_drif_stop_streaming,
> +	.wait_prepare		= vb2_ops_wait_prepare,
> +	.wait_finish		= vb2_ops_wait_finish,
> +};

[snip]

> +static int rcar_drif_g_fmt_sdr_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));

I believe the core ioctl handling code already does this for you. Same for the 
other ioctl handlers in 

> +	return 0;
> +}
> +
> +static int rcar_drif_s_fmt_sdr_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	struct vb2_queue *q = &sdr->vb_queue;
> +	unsigned int i;
> +
> +	if (vb2_is_busy(q))
> +		return -EBUSY;
> +
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {

The code would become more readable (at least in my opinion) if you just added 
a break here, and moved the code below after the loop. In case the requested 
format isn't found (i == NUM_FORMATS) you can then set i to 0 and proceed, 
that will select the first available format as a default.

> +			sdr->fmt_idx  = i;
> +			f->fmt.sdr.buffersize = formats[i].buffersize;
> +
> +			/*
> +			 * If a format demands one channel only out of two
> +			 * enabled channels, pick the 0th channel.
> +			 */
> +			if (formats[i].num_ch < sdr->num_hw_ch) {
> +				sdr->cur_ch_mask = BIT(0);
> +				sdr->num_cur_ch = formats[i].num_ch;
> +			} else {
> +				sdr->cur_ch_mask = sdr->hw_ch_mask;
> +				sdr->num_cur_ch = sdr->num_hw_ch;
> +			}
> +
> +			rdrif_dbg(1, sdr, "cur: idx %u mask %lu num %u\n",
> +				  i, sdr->cur_ch_mask, sdr->num_cur_ch);
> +			return 0;
> +		}
> +	}
> +
> +	if (rcar_drif_set_default_format(sdr)) {
> +		rdrif_err(sdr, "cannot set default format\n");
> +		return -EINVAL;
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> +	return 0;
> +}
> +
> +static int rcar_drif_try_fmt_sdr_cap(struct file *file, void *priv,
> +				     struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	unsigned int i;
> +
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
> +			f->fmt.sdr.buffersize = formats[i].buffersize;
> +			return 0;
> +		}
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;

The result of the TRY_FMT ioctl should not depend on the currently configured 
format. I would return a fixed format (for instance the first one in the 
formats array) in the default case.

> +	return 0;
> +}
> +
> +/* Tuner subdev ioctls */
> +static int rcar_drif_enum_freq_bands(struct file *file, void *priv,
> +				     struct v4l2_frequency_band *band)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	struct v4l2_subdev *sd;
> +	int ret = 0;
> +
> +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> +		ret = v4l2_subdev_call(sd, tuner, enum_freq_bands, band);

This won't work as-is when you'll have multiple subdevs. As the driver only 
supports a single connected subdev at the moment, I suggest storing a pointer 
to that subdev in the rcar_drif_sdr structure, and calling operations on that 
subdev explicitly instead of looping over all subdevs. The comment holds for 
all other ioctls.

> +		if (ret)
> +			break;
> +	}
> +	return ret;
> +}

[snip]

> +static int rcar_drif_notify_bound(struct v4l2_async_notifier *notifier,
> +				   struct v4l2_subdev *subdev,
> +				   struct v4l2_async_subdev *asd)
> +{
> +	struct rcar_drif_sdr *sdr =
> +		container_of(notifier, struct rcar_drif_sdr, notifier);
> +
> +	/* Nothing to do at this point */

If there's nothing to do you can just leave the bound callback unimplemented, 
it's optional.

> +	rdrif_dbg(2, sdr, "bound asd: %s\n", asd->match.of.node->name);
> +	return 0;
> +}
> +
> +/* Sub-device registered notification callback */
> +static int rcar_drif_notify_complete(struct v4l2_async_notifier *notifier)
> +{
> +	struct rcar_drif_sdr *sdr =
> +		container_of(notifier, struct rcar_drif_sdr, notifier);
> +	struct v4l2_subdev *sd;
> +	int ret;
> +
> +	sdr->v4l2_dev.ctrl_handler = &sdr->ctrl_hdl;
> +
> +	ret = v4l2_device_register_subdev_nodes(&sdr->v4l2_dev);
> +	if (ret) {
> +		rdrif_err(sdr, "failed register subdev nodes ret %d\n", ret);
> +		return ret;
> +	}

Do you need to expose subdev nodes to userspace ? Can't everything be handled 
from the V4L2 SDR node ?

> +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> +		ret = v4l2_ctrl_add_handler(sdr->v4l2_dev.ctrl_handler,
> +					    sd->ctrl_handler, NULL);

Shouldn't you undo this somewhere when unbinding the subdevs ?

> +		if (ret) {
> +			rdrif_err(sdr, "failed ctrl add hdlr ret %d\n", ret);
> +			return ret;
> +		}
> +	}
> +	rdrif_dbg(2, sdr, "notify complete\n");
> +	return 0;
> +}

[snip]

> +/* Parse sub-devs (tuner) to find a matching device */
> +static int rcar_drif_parse_subdevs(struct rcar_drif_sdr *sdr,
> +				   struct device *dev)
> +{
> +	struct v4l2_async_notifier *notifier = &sdr->notifier;
> +	struct rcar_drif_async_subdev *rsd;
> +	struct device_node *node;
> +
> +	notifier->subdevs = devm_kzalloc(dev, sizeof(*notifier->subdevs),
> +					 GFP_KERNEL);
> +	if (!notifier->subdevs)
> +		return -ENOMEM;
> +
> +	node = of_graph_get_next_endpoint(dev->of_node, NULL);
> +	if (!node)
> +		return 0;
> +
> +	rsd = devm_kzalloc(dev, sizeof(*rsd), GFP_KERNEL);
> +	if (!rsd) {
> +		of_node_put(node);

If you move the allocation above of_graph_get_next_endpoint() you won't have 
to call of_node_put() in the error path.

> +		return -ENOMEM;
> +	}
> +
> +	notifier->subdevs[notifier->num_subdevs] = &rsd->asd;
> +	rsd->asd.match.of.node = of_graph_get_remote_port_parent(node);

Aren't you missing an of_node_put() on the returned node ? Or does the async 
framework take care of that ?

> +	of_node_put(node);
> +	if (!rsd->asd.match.of.node) {
> +		dev_warn(dev, "bad remote port parent\n");
> +		return -EINVAL;
> +	}
> +
> +	rsd->asd.match_type = V4L2_ASYNC_MATCH_OF;
> +	notifier->num_subdevs++;
> +
> +	/* Get the endpoint properties */
> +	rcar_drif_get_ep_properties(sdr, node);
> +	return 0;
> +}
> +
> +/* Check if the given device is the primary bond */
> +static bool rcar_drif_primary_bond(struct platform_device *pdev)
> +{
> +	if (of_find_property(pdev->dev.of_node, "renesas,primary-bond", NULL))
> +		return true;
> +
> +	return false;

How about

	return of_property_read_bool(pdev->dev.of_node,
				     "renesas,primary-bond");

> +}
> +
> +/* Get the bonded platform dev if enabled */
> +static struct platform_device *rcar_drif_enabled_bond(struct
> platform_device *p)
> +{
> +	struct device_node *np;
> +
> +	np = of_parse_phandle(p->dev.of_node, "renesas,bonding", 0);

The function takes a reference to np, you need to call of_node_put() on it 
(only if the returned pointer isn't NULL).

> +	if (np && of_device_is_available(np))
> +		return of_find_device_by_node(np);

of_find_device_by_node() takes a reference to the returned device, you need to 
call device_put() on it when you don't need it anymore.


> +	return NULL;
> +}
> +
> +/* Proble internal channel */
> +static int rcar_drif_channel_probe(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch;
> +	struct resource	*res;
> +	void __iomem *base;
> +	struct clk *clkp;

Maybe s/clkp/clk/ ?

> +	int ret;
> +
> +	/* Peripheral clock */
> +	clkp = devm_clk_get(&pdev->dev, "fck");
> +	if (IS_ERR(clkp)) {
> +		ret = PTR_ERR(clkp);
> +		dev_err(&pdev->dev, "clk get failed (%d)\n", ret);
> +		return ret;
> +	}

Isn't the clock managed automatically by runtime PM ?

> +	/* Register map */
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	base = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(base)) {
> +		ret = PTR_ERR(base);
> +		dev_err(&pdev->dev, "ioremap failed (%d)\n", ret);
> +		return ret;

devm_ioremap_resource() already prints an error message, you can remove this 
one.

> +	}
> +
> +	/* Reserve memory for enabled channel */
> +	ch = devm_kzalloc(&pdev->dev, sizeof(*ch), GFP_KERNEL);
> +	if (!ch) {
> +		ret = PTR_ERR(ch);
> +		dev_err(&pdev->dev, "failed alloc channel\n");

Memory allocation failures already print error messages, you can remove this 
one.

> +		return ret;
> +	}
> +	ch->pdev = pdev;
> +	ch->clkp = clkp;
> +	ch->base = base;
> +	ch->start = res->start;

If you allocated the ch structure first you could set the fields directly 
without a need for local variables.

> +	platform_set_drvdata(pdev, ch);
> +	return 0;
> +}
> +
> +static int rcar_drif_probe(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch, *b_ch = NULL;
> +	struct platform_device *b_pdev;
> +	struct rcar_drif_sdr *sdr;
> +	int ret;
> +
> +	/* Probe internal channel */
> +	ret = rcar_drif_channel_probe(pdev);
> +	if (ret)
> +		return ret;

I would have done it the other way around, inlining the 
rcar_drif_channel_probe() function here as that's the common case, and moving 
the V4L2 SDR device initialization code to a different function.

> +	/* Check if both channels of the bond are enabled */
> +	b_pdev = rcar_drif_enabled_bond(pdev);
> +	if (b_pdev) {
> +		/* Check if current channel acting as primary-bond */
> +		if (!rcar_drif_primary_bond(pdev)) {
> +			dev_notice(&pdev->dev, "probed\n");
> +			return 0;
> +		}
> +
> +		/* Check if the other device is probed */
> +		b_ch = platform_get_drvdata(b_pdev);
> +		if (!b_ch) {
> +			dev_info(&pdev->dev, "defer probe\n");
> +			return -EPROBE_DEFER;
> +		}

Isn't this all very racy ? What if the other channel's device is removed while 
this one is probed ?

> +		/* Set the other channel number */
> +		b_ch->num = 1;

Reading data from the other channel's private structure is one thing, but 
writing it makes me shiver :-S Could we make it so that 0 is the slave and 1 
the master ? That way you would set ch->num = 1 instead of b_ch->num = 1, 
keeping all modifications to the private structure local to the device being 
probed.

> +	}
> +
> +	/* Channel acting as SDR instance */
> +	ch = platform_get_drvdata(pdev);
> +	ch->acting_sdr = true;
> +
> +	/* Reserve memory for SDR structure */
> +	sdr = devm_kzalloc(&pdev->dev, sizeof(*sdr), GFP_KERNEL);
> +	if (!sdr) {
> +		ret = PTR_ERR(sdr);
> +		dev_err(&pdev->dev, "failed alloc drif context\n");
> +		return ret;
> +	}
> +	sdr->dev = &pdev->dev;
> +	sdr->hw_ch_mask = BIT(ch->num);
> +
> +	/* Establish links between SDR and channel(s) */
> +	ch->sdr = sdr;
> +	sdr->ch[ch->num] = ch;
> +	if (b_ch) {
> +		sdr->ch[b_ch->num] = b_ch;
> +		b_ch->sdr = sdr;
> +		sdr->hw_ch_mask |= BIT(b_ch->num);
> +	}
> +	sdr->num_hw_ch = hweight_long(sdr->hw_ch_mask);
> +
> +	/* Validate any supported format for enabled channels */
> +	ret = rcar_drif_set_default_format(sdr);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed to set default format\n");
> +		return ret;
> +	}
> +
> +	/* Set defaults */
> +	sdr->hwbuf_size = RCAR_DRIF_DEFAULT_HWBUF_SIZE;
> +
> +	mutex_init(&sdr->v4l2_mutex);
> +	mutex_init(&sdr->vb_queue_mutex);
> +	spin_lock_init(&sdr->queued_bufs_lock);
> +	INIT_LIST_HEAD(&sdr->queued_bufs);
> +
> +	/* Init videobuf2 queue structure */
> +	sdr->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
> +	sdr->vb_queue.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
> +	sdr->vb_queue.drv_priv = sdr;
> +	sdr->vb_queue.buf_struct_size = sizeof(struct rcar_drif_frame_buf);
> +	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
> +	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
> +	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> +
> +	/* Init videobuf2 queue */
> +	ret = vb2_queue_init(&sdr->vb_queue);
> +	if (ret) {
> +		dev_err(sdr->dev, "could not initialize vb2 queue\n");
> +		return ret;
> +	}
> +
> +	/* Register the v4l2_device */
> +	ret = v4l2_device_register(&pdev->dev, &sdr->v4l2_dev);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed v4l2_device_register (%d)\n", ret);

Maybe "failed to register V4L2 device" to make it a real sentence ? :-)

> +		return ret;
> +	}
> +
> +	/*
> +	 * Parse subdevs after v4l2_device_register because if the subdev
> +	 * is already probed, bound and complete will be called immediately
> +	 */
> +	ret = rcar_drif_parse_subdevs(sdr, &pdev->dev);
> +	if (ret)
> +		goto err_unreg_v4l2;
> +
> +	sdr->notifier.bound = rcar_drif_notify_bound;
> +	sdr->notifier.complete = rcar_drif_notify_complete;
> +
> +	v4l2_ctrl_handler_init(&sdr->ctrl_hdl, 10);

Possibly a stupid question, why 10, if you don't create any control in this 
driver ?

> +	/* Register notifier */
> +	ret = v4l2_async_notifier_register(&sdr->v4l2_dev, &sdr->notifier);
> +	if (ret < 0) {
> +		dev_err(sdr->dev, "notifier registration failed (%d)\n", ret);
> +		goto err_free_ctrls;
> +	}
> +
> +	/* Init video_device structure */
> +	sdr->vdev = video_device_alloc();
> +	if (!sdr->vdev) {
> +		ret = -ENOMEM;
> +		goto err_unreg_notif;
> +	}
> +	snprintf(sdr->vdev->name, sizeof(sdr->vdev->name), "R-Car DRIF");
> +	sdr->vdev->fops = &rcar_drif_fops;
> +	sdr->vdev->ioctl_ops = &rcar_drif_ioctl_ops;
> +	sdr->vdev->release = video_device_release;
> +	sdr->vdev->lock = &sdr->v4l2_mutex;
> +	sdr->vdev->queue = &sdr->vb_queue;
> +	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
> +	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
> +	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
> +	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
> +		V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
> +	video_set_drvdata(sdr->vdev, sdr);
> +
> +	/* Register V4L2 SDR device */
> +	ret = video_register_device(sdr->vdev, VFL_TYPE_SDR, -1);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed video_register_device (%d)\n", ret);

Same here, "failed to register video device" ?

> +		goto err_unreg_notif;
> +	}
> +
> +	dev_notice(sdr->dev, "probed\n");

Do you think this message is really useful ? I believe it would just add a bit 
more noise to the kernel log, without any real use.

> +	return 0;
> +
> +err_unreg_notif:
> +	video_device_release(sdr->vdev);
> +	v4l2_async_notifier_unregister(&sdr->notifier);
> +err_free_ctrls:
> +	v4l2_ctrl_handler_free(&sdr->ctrl_hdl);
> +err_unreg_v4l2:
> +	v4l2_device_unregister(&sdr->v4l2_dev);
> +	return ret;
> +}
> +
> +static int rcar_drif_remove(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch = platform_get_drvdata(pdev);
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +
> +	if (!ch->acting_sdr) {

Isn't it possible to check the channel number instead and remove the 
acting_sdr field ?

> +		/* Nothing to do */
> +		dev_notice(&pdev->dev, "removed\n");
> +		return 0;
> +	}
> +
> +	/* SDR instance */
> +	v4l2_ctrl_handler_free(sdr->vdev->ctrl_handler);
> +	v4l2_async_notifier_unregister(&sdr->notifier);
> +	v4l2_device_unregister(&sdr->v4l2_dev);
> +	video_unregister_device(sdr->vdev);
> +	dev_notice(&pdev->dev, "removed\n");

Even more than the probed message, I think this one can go away.

> +	return 0;
> +}
> +
> +static int __maybe_unused rcar_drif_suspend(struct device *dev)
> +{
> +	return 0;

Maybe a /* FIXME: Implement suspend/resume support */ ?

> +}
> +
> +static int __maybe_unused rcar_drif_resume(struct device *dev)
> +{
> +	return 0;

Same here ?

> +}

[snip]

-- 
Regards,

Laurent Pinchart

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support
@ 2017-04-11 22:24       ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 22:24 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:37 Ramesh Shanmugasundaram wrote:
> This patch adds Digital Radio Interface (DRIF) support to R-Car Gen3 SoCs.
> The driver exposes each instance of DRIF as a V4L2 SDR device. A DRIF
> device represents a channel and each channel can have one or two
> sub-channels respectively depending on the target board.
> 
> DRIF supports only Rx functionality. It receives samples from a RF
> frontend tuner chip it is interfaced with. The combination of DRIF and the
> tuner device, which is registered as a sub-device, determines the receive
> sample rate and format.
> 
> In order to be compliant as a V4L2 SDR device, DRIF needs to bind with
> the tuner device, which can be provided by a third party vendor. DRIF acts
> as a slave device and the tuner device acts as a master transmitting the
> samples. The driver allows asynchronous binding of a tuner device that
> is registered as a v4l2 sub-device. The driver can learn about the tuner
> it is interfaced with based on port endpoint properties of the device in
> device tree. The V4L2 SDR device inherits the controls exposed by the
> tuner device.
> 
> The device can also be configured to use either one or both of the data
> pins at runtime based on the master (tuner) configuration.
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram@bp.renesas.com>
> ---
>  drivers/media/platform/Kconfig     |   25 +
>  drivers/media/platform/Makefile    |    1 +
>  drivers/media/platform/rcar_drif.c | 1534 +++++++++++++++++++++++++++++++++
>  3 files changed, 1560 insertions(+)
>  create mode 100644 drivers/media/platform/rcar_drif.c

[snip]

> diff --git a/drivers/media/platform/rcar_drif.c
> b/drivers/media/platform/rcar_drif.c new file mode 100644
> index 0000000..88950e3
> --- /dev/null
> +++ b/drivers/media/platform/rcar_drif.c
> @@ -0,0 +1,1534 @@

[snip]

> +/*
> + * The R-Car DRIF is a receive only MSIOF like controller with an
> + * external master device driving the SCK. It receives data into a FIFO,
> + * then this driver uses the SYS-DMAC engine to move the data from
> + * the device to memory.
> + *
> + * Each DRIF channel DRIFx (as per datasheet) contains two internal
> + * channels DRIFx0 & DRIFx1 within itself with each having its own
> resources
> + * like module clk, register set, irq and dma. These internal channels
> share
> + * common CLK & SYNC from master. The two data pins D0 & D1 shall be
> + * considered to represent the two internal channels. This internal split
> + * is not visible to the master device.
> + *
> + * Depending on the master device, a DRIF channel can use
> + *  (1) both internal channels (D0 & D1) to receive data in parallel (or)
> + *  (2) one internal channel (D0 or D1) to receive data
> + *
> + * The primary design goal of this controller is to act as Digitial Radio

s/Digitial/Digital/

> + * Interface that receives digital samples from a tuner device. Hence the
> + * driver exposes the device as a V4L2 SDR device. In order to qualify as
> + * a V4L2 SDR device, it should possess tuner interface as mandated by the
> + * framework. This driver expects a tuner driver (sub-device) to bind
> + * asynchronously with this device and the combined drivers shall expose
> + * a V4L2 compliant SDR device. The DRIF driver is independent of the
> + * tuner vendor.
> + *
> + * The DRIF h/w can support I2S mode and Frame start synchronization pulse
> mode.
> + * This driver is tested for I2S mode only because of the availability of
> + * suitable master devices. Hence, not all configurable options of DRIF h/w
> + * like lsb/msb first, syncdl, dtdl etc. are exposed via DT and I2S
> defaults
> + * are used. These can be exposed later if needed after testing.
> + */

[snip]

> +#define to_rcar_drif_buf_pair(sdr, ch_num,
> idx)	(sdr->ch[!(ch_num)]->buf[idx])

You should enclose both sdr and idx in parenthesis, as they can be 
expressions.

> +
> +#define for_each_rcar_drif_channel(ch, ch_mask)			\
> +	for_each_set_bit(ch, ch_mask, RCAR_DRIF_MAX_CHANNEL)
> +
> +static const unsigned int num_hwbufs = 32;

Is there a specific reason to make this a static const instead of a #define ?

> +/* Debug */
> +static unsigned int debug;
> +module_param(debug, uint, 0644);
> +MODULE_PARM_DESC(debug, "activate debug info");
> +
> +#define rdrif_dbg(level, sdr, fmt, arg...)				\
> +	v4l2_dbg(level, debug, &sdr->v4l2_dev, fmt, ## arg)
> +
> +#define rdrif_err(sdr, fmt, arg...)					\
> +	dev_err(sdr->v4l2_dev.dev, fmt, ## arg)
> +
> +/* Stream formats */
> +struct rcar_drif_format {
> +	u32	pixelformat;
> +	u32	buffersize;
> +	u32	wdlen;
> +	u32	num_ch;
> +};
> +
> +/* Format descriptions for capture */
> +static const struct rcar_drif_format formats[] = {
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU16BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 16,
> +		.num_ch	= 2,

How about aligning the = as in the other lines ?

num_ch is always set to 2. Should we remove it for now, and add it back later 
when we'll support single-channel formats ? I think we should avoid carrying 
dead code.

> +	},
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU18BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 18,
> +		.num_ch	= 2,
> +	},
> +	{
> +		.pixelformat	= V4L2_SDR_FMT_PCU20BE,
> +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> +		.wdlen		= 20,
> +		.num_ch	= 2,
> +	},
> +};
> +
> +static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);

Same question here, can't this be a define ? I think I'd even avoid 
NUM_FORMATS completely and use ARRAY_SIZE(formats) directly in the code, to 
make the boundary check more explicit when iterating over the array.

> +
> +/* Buffer for a received frame from one or both internal channels */
> +struct rcar_drif_frame_buf {
> +	/* Common v4l buffer stuff -- must be first */
> +	struct vb2_v4l2_buffer vb;
> +	struct list_head list;
> +};
> +
> +struct rcar_drif_async_subdev {
> +	struct v4l2_subdev *sd;
> +	struct v4l2_async_subdev asd;
> +};
> +
> +/* DMA buffer */
> +struct rcar_drif_hwbuf {
> +	void *addr;			/* CPU-side address */
> +	unsigned int status;		/* Buffer status flags */
> +};
> +
> +/* Internal channel */
> +struct rcar_drif {
> +	struct rcar_drif_sdr *sdr;	/* Group device */
> +	struct platform_device *pdev;	/* Channel's pdev */
> +	void __iomem *base;		/* Base register address */
> +	resource_size_t start;		/* I/O resource offset */
> +	struct dma_chan *dmach;		/* Reserved DMA channel */
> +	struct clk *clkp;		/* Module clock */
> +	struct rcar_drif_hwbuf *buf[RCAR_DRIF_MAX_NUM_HWBUFS]; /* H/W bufs */
> +	dma_addr_t dma_handle;		/* Handle for all bufs */
> +	unsigned int num;		/* Channel number */
> +	bool acting_sdr;		/* Channel acting as SDR device */
> +};
> +
> +/* DRIF V4L2 SDR */
> +struct rcar_drif_sdr {
> +	struct device *dev;		/* Platform device */
> +	struct video_device *vdev;	/* V4L2 SDR device */
> +	struct v4l2_device v4l2_dev;	/* V4L2 device */
> +
> +	/* Videobuf2 queue and queued buffers list */
> +	struct vb2_queue vb_queue;
> +	struct list_head queued_bufs;
> +	spinlock_t queued_bufs_lock;	/* Protects queued_bufs */
> +
> +	struct mutex v4l2_mutex;	/* To serialize ioctls */
> +	struct mutex vb_queue_mutex;	/* To serialize streaming ioctls */
> +	struct v4l2_ctrl_handler ctrl_hdl;	/* SDR control handler */
> +	struct v4l2_async_notifier notifier;	/* For subdev (tuner) */
> +
> +	/* Current V4L2 SDR format array index */
> +	unsigned int fmt_idx;

Instead of storing the index I would store a pointer to the corresponding 
rcar_drif_format, looking up information about the current format will then be 
easier.

> +
> +	/* Device tree SYNC properties */
> +	u32 mdr1;
> +
> +	/* Internals */
> +	struct rcar_drif *ch[RCAR_DRIF_MAX_CHANNEL]; /* DRIFx0,1 */
> +	unsigned long hw_ch_mask;	/* Enabled channels per DT */
> +	unsigned long cur_ch_mask;	/* Used channels for an SDR FMT */
> +	u32 num_hw_ch;			/* Num of DT enabled channels */
> +	u32 num_cur_ch;			/* Num of used channels */
> +	u32 hwbuf_size;			/* Each DMA buffer size */
> +	u32 produced;			/* Buffers produced by sdr dev */
> +};
> +
> +/* Allocate buffer context */
> +static int rcar_drif_alloc_bufctxt(struct rcar_drif_sdr *sdr)
> +{
> +	struct rcar_drif_hwbuf *bufctx;
> +	unsigned int i, idx;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		bufctx = kcalloc(num_hwbufs, sizeof(*bufctx), GFP_KERNEL);

How about embedding the buffer contexts in the rcar_drif structure instead of 
just storing pointers there ? The rcar_drif_hwbuf structure is pretty small, 
it won't make a big difference, and will simplify the code.

> +		if (!bufctx)
> +			return -ENOMEM;
> +
> +		for (idx = 0; idx < num_hwbufs; idx++)
> +			sdr->ch[i]->buf[idx] = bufctx + idx;
> +	}
> +	return 0;
> +}

[snip]

> +/* Release DMA channel */
> +static void rcar_drif_release_dmachannel(struct rcar_drif_sdr *sdr)

I would name the function rcar_drif_release_dma_channels as it handles all 
channels. Same for rcar_drif_alloc_dma_channels.

> +{
> +	unsigned int i;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
> +		if (sdr->ch[i]->dmach) {
> +			dma_release_channel(sdr->ch[i]->dmach);
> +			sdr->ch[i]->dmach = NULL;
> +		}
> +}
> +
> +/* Allocate DMA channel */
> +static int rcar_drif_alloc_dmachannel(struct rcar_drif_sdr *sdr)
> +{
> +	struct dma_slave_config dma_cfg;
> +	unsigned int i;
> +	int ret = -ENODEV;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		ch->dmach = dma_request_slave_channel(&ch->pdev->dev, "rx");
> +		if (!ch->dmach) {
> +			rdrif_err(sdr, "ch%u: dma channel req failed\n", i);
> +			goto dmach_error;
> +		}
> +
> +		/* Configure slave */
> +		memset(&dma_cfg, 0, sizeof(dma_cfg));
> +		dma_cfg.src_addr = (phys_addr_t)(ch->start + 
RCAR_DRIF_SIRFDR);
> +		dma_cfg.dst_addr = 0;

This isn't needed as you memset the whole structure to 0.

> +		dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +		ret = dmaengine_slave_config(ch->dmach, &dma_cfg);
> +		if (ret) {
> +			rdrif_err(sdr, "ch%u: dma slave config failed\n", i);
> +			goto dmach_error;
> +		}
> +	}
> +	return 0;
> +
> +dmach_error:
> +	rcar_drif_release_dmachannel(sdr);
> +	return ret;
> +}

[snip]

> +/* Set MDR defaults */
> +static inline void rcar_drif_set_mdr1(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +
> +	/* Set defaults for enabled internal channels */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		/* Refer MSIOF section in manual for this register setting */
> +		writel(RCAR_DRIF_SITMDR1_PCON,
> +		       sdr->ch[i]->base + RCAR_DRIF_SITMDR1);

I would create a rcar_drif_write(struct rcar_drif *ch, u32 offset, u32 data) 
function, the code will become clearer. Same for the read operation.

> +		/* Setup MDR1 value */
> +		writel(sdr->mdr1, sdr->ch[i]->base + RCAR_DRIF_SIRMDR1);
> +
> +		rdrif_dbg(2, sdr, "ch%u: mdr1 = 0x%08x",
> +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR1));

Once you've debugged the driver I'm not sure those debugging statements are 
still needed.

> +	}
> +}
> +
> +/* Extract bitlen and wdcnt from given word length */
> +static int rcar_drif_convert_wdlen(struct rcar_drif_sdr *sdr,
> +				   u32 wdlen, u32 *bitlen, u32 *wdcnt)
> +{
> +	unsigned int i, nr_wds;
> +
> +	/* FIFO register size is 32 bits */
> +	for (i = 0; i < 32; i++) {
> +		nr_wds = wdlen % (32 - i);
> +		if (nr_wds == 0) {
> +			*bitlen = 32 - i;
> +			*wdcnt = wdlen / *bitlen;

Can't you store the bitlen and wdcnt values in the rcar_drif_format structure 
instead of recomputing them every time ?

> +			break;
> +		}
> +	}
> +
> +	/* Sanity check range */
> +	if (i == 32 || !(*bitlen >= 8 && *bitlen <= 32) ||
> +	    !(*wdcnt >= 1 && *wdcnt <= 64)) {
> +		rdrif_err(sdr, "invalid wdlen %u configured\n", wdlen);
> +		return -EINVAL;

You shouldn't have invalid wdlen values in the driver. I would remove this 
check as it makes error handling in the caller more complex.

> +	}
> +
> +	return 0;
> +}
> +
> +/* Set DRIF receive format */
> +static int rcar_drif_set_format(struct rcar_drif_sdr *sdr)
> +{
> +	u32 bitlen, wdcnt, wdlen;
> +	unsigned int i;
> +	int ret = -EINVAL;
> +
> +	wdlen = formats[sdr->fmt_idx].wdlen;
> +	rdrif_dbg(2, sdr, "setfmt: idx %u, wdlen %u, num_ch %u\n",
> +		  sdr->fmt_idx, wdlen, formats[sdr->fmt_idx].num_ch);
> +
> +	/* Sanity check */
> +	if (formats[sdr->fmt_idx].num_ch > sdr->num_cur_ch) {
> +		rdrif_err(sdr, "fmt idx %u current ch %u mismatch\n",
> +			  sdr->fmt_idx, sdr->num_cur_ch);
> +		return ret;

This should never happen, it should be caught at set format time.

> +	}
> +
> +	/* Get bitlen & wdcnt from wdlen */
> +	ret = rcar_drif_convert_wdlen(sdr, wdlen, &bitlen, &wdcnt);
> +	if (ret)
> +		return ret;
> +
> +	/* Setup group, bitlen & wdcnt */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		u32 mdr;
> +
> +		/* Two groups */
> +		mdr = RCAR_DRIF_MDR_GRPCNT(2) | RCAR_DRIF_MDR_BITLEN(bitlen) |
> +		       RCAR_DRIF_MDR_WDCNT(wdcnt);
> +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR2);
> +
> +		mdr = RCAR_DRIF_MDR_BITLEN(bitlen) | 
RCAR_DRIF_MDR_WDCNT(wdcnt);
> +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR3);
> +
> +		rdrif_dbg(2, sdr, "ch%u: new mdr[2,3] = 0x%08x, 0x%08x\n",
> +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR2),
> +			  readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR3));
> +	}
> +	return ret;
> +}
> +
> +/* Release DMA buffers */
> +static void rcar_drif_release_buf(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		/* First entry contains the dma buf ptr */
> +		if (ch->buf[0] && ch->buf[0]->addr) {
> +			dma_free_coherent(&ch->pdev->dev,
> +					  sdr->hwbuf_size * num_hwbufs,
> +					  ch->buf[0]->addr, ch->dma_handle);
> +			ch->buf[0]->addr = NULL;
> +		}
> +	}
> +}
> +
> +/* Request DMA buffers */
> +static int rcar_drif_request_buf(struct rcar_drif_sdr *sdr)
> +{
> +	int ret = -ENOMEM;
> +	unsigned int i, j;
> +	void *addr;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		struct rcar_drif *ch = sdr->ch[i];
> +
> +		/* Allocate DMA buffers */
> +		addr = dma_alloc_coherent(&ch->pdev->dev,
> +					  sdr->hwbuf_size * num_hwbufs,
> +					  &ch->dma_handle, GFP_KERNEL);
> +		if (!addr) {
> +			rdrif_err(sdr,
> +			"ch%u: dma alloc failed. num_hwbufs %u size %u\n",
> +			i, num_hwbufs, sdr->hwbuf_size);
> +			goto alloc_error;
> +		}
> +
> +		/* Split the chunk and populate bufctxt */
> +		for (j = 0; j < num_hwbufs; j++) {
> +			ch->buf[j]->addr = addr + (j * sdr->hwbuf_size);
> +			ch->buf[j]->status = 0;
> +		}
> +	}
> +
> +	return 0;
> +
> +alloc_error:
> +	return ret;
> +}
> +
> +/* Setup vb_queue minimum buffer requirements */
> +static int rcar_drif_queue_setup(struct vb2_queue *vq,
> +			unsigned int *num_buffers, unsigned int *num_planes,
> +			unsigned int sizes[], struct device *alloc_devs[])
> +{
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> +
> +	/* Need at least 16 buffers */
> +	if (vq->num_buffers + *num_buffers < 16)
> +		*num_buffers = 16 - vq->num_buffers;
> +
> +	*num_planes = 1;
> +	sizes[0] = PAGE_ALIGN(formats[sdr->fmt_idx].buffersize);
> +
> +	rdrif_dbg(2, sdr, "num_bufs %d sizes[0] %d\n", *num_buffers, 
sizes[0]);
> +	return 0;
> +}
> +
> +/* Enqueue buffer */
> +static void rcar_drif_buf_queue(struct vb2_buffer *vb)
> +{
> +	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vb->vb2_queue);
> +	struct rcar_drif_frame_buf *fbuf =
> +			container_of(vbuf, struct rcar_drif_frame_buf, vb);
> +	unsigned long flags;
> +
> +	rdrif_dbg(2, sdr, "buf_queue idx %u\n", vb->index);
> +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> +	list_add_tail(&fbuf->list, &sdr->queued_bufs);
> +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +}
> +
> +/* Get a frame buf from list */
> +static struct rcar_drif_frame_buf *
> +rcar_drif_get_fbuf(struct rcar_drif_sdr *sdr)
> +{
> +	struct rcar_drif_frame_buf *fbuf;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> +	fbuf = list_first_entry_or_null(&sdr->queued_bufs, struct
> +					rcar_drif_frame_buf, list);
> +	if (!fbuf) {
> +		/*
> +		 * App is late in enqueing buffers. Samples lost & there will
> +		 * be a gap in sequence number when app recovers
> +		 */
> +		rdrif_dbg(1, sdr, "\napp late: prod %u\n", sdr->produced);
> +		sdr->produced++; /* Increment the produced count anyway */
> +		spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +		return NULL;
> +	}
> +	list_del(&fbuf->list);
> +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> +
> +	return fbuf;
> +}
> +
> +static inline bool rcar_drif_buf_pairs_done(struct rcar_drif_hwbuf *buf1,
> +					    struct rcar_drif_hwbuf *buf2)
> +{
> +	return (buf1->status & buf2->status & RCAR_DRIF_BUF_DONE);
> +}
> +
> +/* Channel DMA complete */
> +static void rcar_drif_channel_complete(struct rcar_drif *ch, u32 idx)
> +{
> +	u32 str;
> +
> +	ch->buf[idx]->status |= RCAR_DRIF_BUF_DONE;
> +
> +	/* Check for DRIF errors */
> +	str = readl(ch->base + RCAR_DRIF_SISTR);
> +	if (unlikely(str & RCAR_DRIF_RFOVF)) {
> +		/* Writing the same clears it */
> +		writel(str, ch->base + RCAR_DRIF_SISTR);
> +
> +		/* Overflow: some samples are lost */
> +		ch->buf[idx]->status |= RCAR_DRIF_BUF_OVERFLOW;
> +	}
> +}
> +
> +/* Deliver buffer to user */
> +static void rcar_drif_deliver_buf(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	u32 idx = sdr->produced % num_hwbufs;
> +	struct rcar_drif_frame_buf *fbuf;
> +	bool overflow = false;
> +
> +	rcar_drif_channel_complete(ch, idx);
> +
> +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL) {
> +		struct rcar_drif_hwbuf *bufi, *bufq;
> +
> +		if (ch->num) {
> +			bufi = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> +			bufq = ch->buf[idx];
> +		} else {
> +			bufi = ch->buf[idx];
> +			bufq = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> +		}
> +
> +		/* Check if both DMA buffers are done */
> +		if (!rcar_drif_buf_pairs_done(bufi, bufq))
> +			return;
> +
> +		/* Clear buf done status */
> +		bufi->status &= ~RCAR_DRIF_BUF_DONE;
> +		bufq->status &= ~RCAR_DRIF_BUF_DONE;
> +
> +		/* Get fbuf */
> +		fbuf = rcar_drif_get_fbuf(sdr);
> +		if (!fbuf)
> +			return;
> +
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> +		       bufi->addr, sdr->hwbuf_size);
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0) + sdr-
>hwbuf_size,
> +		       bufq->addr, sdr->hwbuf_size);

Ouch ! That's a high data rate memcpy that can be avoided. Why don't you DMA 
directly to the vb2 buffers ? You will need to use videobuf2-dma-contig 
instead of videobuf2-vmalloc, but apart from that there should be no issue.

> +		if ((bufi->status | bufq->status) & RCAR_DRIF_BUF_OVERFLOW) {
> +			overflow = true;
> +			/* Clear the flag in status */
> +			bufi->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +			bufq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +		}
> +	} else {
> +		struct rcar_drif_hwbuf *bufiq;
> +
> +		/* Get fbuf */
> +		fbuf = rcar_drif_get_fbuf(sdr);
> +		if (!fbuf)
> +			return;
> +
> +		bufiq = ch->buf[idx];
> +
> +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> +		       bufiq->addr, sdr->hwbuf_size);
> +
> +		if (bufiq->status & RCAR_DRIF_BUF_OVERFLOW) {
> +			overflow = true;
> +			/* Clear the flag in status */
> +			bufiq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> +		}
> +	}
> +
> +	rdrif_dbg(2, sdr, "ch%u: prod %u\n", ch->num, sdr->produced);
> +
> +	fbuf->vb.field = V4L2_FIELD_NONE;
> +	fbuf->vb.sequence = sdr->produced++;
> +	fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
> +	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
> +			      formats[sdr->fmt_idx].buffersize);
> +
> +	/* Set error state on overflow */
> +	if (overflow)
> +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> +	else
> +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);

Maybe

	vb2_buffer_done(&fbuf->vb.vb2_buf,
			overflow ? VB2_BUF_STATE_ERROR: VB2_BUF_STATE_DONE);

> +}
> +
> +/* DMA callback for each stage */
> +static void rcar_drif_dma_complete(void *dma_async_param)
> +{
> +	struct rcar_drif *ch = dma_async_param;
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +
> +	mutex_lock(&sdr->vb_queue_mutex);

Isn't the complete callback potentially called in interrupt context ? I know 
the rcar-dmac driver uses a threaded interrupt handler for that, but is that a 
guarantee of the DMA engine API ?

> +
> +	/* DMA can be terminated while the callback was waiting on lock */
> +	if (!vb2_is_streaming(&sdr->vb_queue))

Can it ? The streaming flag is cleared after the stop_streaming operation is 
called, which will terminate all DMA transfers synchronously.

> +		goto stopped;
> +
> +	rcar_drif_deliver_buf(ch);
> +stopped:
> +	mutex_unlock(&sdr->vb_queue_mutex);
> +}
> +
> +static int rcar_drif_qbuf(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	dma_addr_t addr = ch->dma_handle;
> +	struct dma_async_tx_descriptor *rxd;
> +	dma_cookie_t cookie;
> +	int ret = -EIO;
> +
> +	/* Setup cyclic DMA with given buffers */
> +	rxd = dmaengine_prep_dma_cyclic(ch->dmach, addr,
> +					sdr->hwbuf_size * num_hwbufs,
> +					sdr->hwbuf_size, DMA_DEV_TO_MEM,
> +					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> +	if (!rxd) {
> +		rdrif_err(sdr, "ch%u: prep dma cyclic failed\n", ch->num);
> +		return ret;
> +	}
> +
> +	/* Submit descriptor */
> +	rxd->callback = rcar_drif_dma_complete;
> +	rxd->callback_param = ch;
> +	cookie = dmaengine_submit(rxd);
> +	if (dma_submit_error(cookie)) {
> +		rdrif_err(sdr, "ch%u: dma submit failed\n", ch->num);
> +		return ret;
> +	}
> +
> +	dma_async_issue_pending(ch->dmach);
> +	return 0;
> +}
> +
> +/* Enable reception */
> +static int rcar_drif_enable_rx(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	u32 ctr;
> +	int ret;
> +
> +	/*
> +	 * When both internal channels are enabled, they can be synchronized
> +	 * only by the master
> +	 */
> +
> +	/* Enable receive */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +		ctr |= (RCAR_DRIF_SICTR_RX_RISING_EDGE |
> +			 RCAR_DRIF_SICTR_RX_EN);
> +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +	}
> +
> +	/* Check receive enabled */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> +					 ctr, ctr & RCAR_DRIF_SICTR_RX_EN,
> +					 2, 500000);

A 2µs sleep for a 500ms total timeout seems very low to me, that will stress 
the CPU. Same comment for the other locations where you use 
readl_poll_timeout.

How long does the channel typically take to get enabled ?

> +		if (ret) {
> +			rdrif_err(sdr, "ch%u: rx en failed. ctr 0x%08x\n",
> +				  i, readl(sdr->ch[i]->base + 
RCAR_DRIF_SICTR));
> +			break;
> +		}
> +	}
> +	return ret;
> +}
> +
> +/* Disable reception */
> +static void rcar_drif_disable_rx(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	u32 ctr;
> +	int ret;
> +
> +	/* Disable receive */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +		ctr &= ~RCAR_DRIF_SICTR_RX_EN;
> +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> +	}
> +
> +	/* Check receive disabled */
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> +					 ctr, !(ctr & RCAR_DRIF_SICTR_RX_EN),
> +					 2, 500000);

How long does the channel typically take to get disabled ?

> +		if (ret)
> +			dev_warn(&sdr->vdev->dev,
> +			"ch%u: failed to disable rx. ctr 0x%08x\n",
> +			i, readl(sdr->ch[i]->base + RCAR_DRIF_SICTR));
> +	}
> +}
> +
> +/* Start channel */
> +static int rcar_drif_start_channel(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	u32 ctr, str;
> +	int ret;
> +
> +	/* Reset receive */
> +	writel(RCAR_DRIF_SICTR_RESET, ch->base + RCAR_DRIF_SICTR);
> +	ret = readl_poll_timeout(ch->base + RCAR_DRIF_SICTR,
> +					 ctr, !(ctr & RCAR_DRIF_SICTR_RESET),

The alignment is weird.

> +					 2, 500000);
> +	if (ret) {
> +		rdrif_err(sdr, "ch%u: failed to reset rx. ctr 0x%08x\n",
> +			  ch->num, readl(ch->base + RCAR_DRIF_SICTR));
> +		return ret;
> +	}
> +
> +	/* Queue buffers for DMA */
> +	ret = rcar_drif_qbuf(ch);
> +	if (ret)
> +		return ret;
> +
> +	/* Clear status register flags */
> +	str = RCAR_DRIF_RFFUL | RCAR_DRIF_REOF | RCAR_DRIF_RFSERR |
> +		RCAR_DRIF_RFUDF | RCAR_DRIF_RFOVF;
> +	writel(str, ch->base + RCAR_DRIF_SISTR);
> +
> +	/* Enable DMA receive interrupt */
> +	writel(0x00009000, ch->base + RCAR_DRIF_SIIER);
> +
> +	return ret;
> +}
> +
> +/* Start receive operation */
> +static int rcar_drif_start(struct rcar_drif_sdr *sdr)
> +{
> +	unsigned int i;
> +	int ret;
> +
> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = rcar_drif_start_channel(sdr->ch[i]);
> +		if (ret)
> +			goto start_error;
> +	}
> +
> +	sdr->produced = 0;
> +	ret = rcar_drif_enable_rx(sdr);
> +start_error:

Don't you need to stop the channels that were successfully started if an error 
occurs ?

> +	return ret;
> +}
> +
> +/* Stop channel */
> +static void rcar_drif_stop_channel(struct rcar_drif *ch)
> +{
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +	int ret, retries = 3;
> +
> +	/* Disable DMA receive interrupt */
> +	writel(0x00000000, ch->base + RCAR_DRIF_SIIER);
> +
> +	do {
> +		/* Terminate all DMA transfers */
> +		ret = dmaengine_terminate_sync(ch->dmach);
> +		if (!ret)
> +			break;
> +		rdrif_dbg(2, sdr, "stop retry\n");
> +	} while (--retries);

Why do you need to retry the terminate operation, why does it fail ?

> +	WARN_ON(!retries);
> +}

[snip]

> +/* Start streaming */
> +static int rcar_drif_start_streaming(struct vb2_queue *vq, unsigned int
> count)
> +{
> +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> +	unsigned int i, j;
> +	int ret;
> +
> +	mutex_lock(&sdr->v4l2_mutex);

I'm surprised, aren't the start_streaming and stop_streaming operations called 
with the video device lock held already by the v4l2-ioctl layer ? I think they 
should be, if they're not there's probably a bug somewhere.

> +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> +		ret = clk_prepare_enable(sdr->ch[i]->clkp);
> +		if (ret)
> +			goto start_error;
> +	}
> +
> +	/* Set default MDRx settings */
> +	rcar_drif_set_mdr1(sdr);
> +
> +	/* Set new format */
> +	ret = rcar_drif_set_format(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL)
> +		sdr->hwbuf_size =
> +		formats[sdr->fmt_idx].buffersize / RCAR_DRIF_MAX_CHANNEL;
> +	else
> +		sdr->hwbuf_size = formats[sdr->fmt_idx].buffersize;
> +
> +	rdrif_dbg(1, sdr, "num_hwbufs %u, hwbuf_size %u\n",
> +		num_hwbufs, sdr->hwbuf_size);
> +
> +	/* Alloc DMA channel */
> +	ret = rcar_drif_alloc_dmachannel(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Alloc buf context */
> +	ret = rcar_drif_alloc_bufctxt(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Request buffers */
> +	ret = rcar_drif_request_buf(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	/* Start Rx */
> +	ret = rcar_drif_start(sdr);
> +	if (ret)
> +		goto start_error;
> +
> +	mutex_unlock(&sdr->v4l2_mutex);
> +	rdrif_dbg(1, sdr, "started\n");
> +	return ret;
> +
> +start_error:

As there's a single error label I would call this "error". Up to you.

> +	rcar_drif_release_queued_bufs(sdr, VB2_BUF_STATE_QUEUED);
> +	rcar_drif_release_buf(sdr);
> +	rcar_drif_release_bufctxt(sdr);
> +	rcar_drif_release_dmachannel(sdr);
> +	for (j = 0; j < i; j++)
> +		clk_disable_unprepare(sdr->ch[j]->clkp);
> +
> +	mutex_unlock(&sdr->v4l2_mutex);
> +	return ret;
> +}

[snip]

> +/* Vb2 ops */
> +static struct vb2_ops rcar_drif_vb2_ops = {

You can make this static const.

> +	.queue_setup            = rcar_drif_queue_setup,
> +	.buf_queue              = rcar_drif_buf_queue,
> +	.start_streaming        = rcar_drif_start_streaming,
> +	.stop_streaming         = rcar_drif_stop_streaming,
> +	.wait_prepare		= vb2_ops_wait_prepare,
> +	.wait_finish		= vb2_ops_wait_finish,
> +};

[snip]

> +static int rcar_drif_g_fmt_sdr_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));

I believe the core ioctl handling code already does this for you. Same for the 
other ioctl handlers in 

> +	return 0;
> +}
> +
> +static int rcar_drif_s_fmt_sdr_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	struct vb2_queue *q = &sdr->vb_queue;
> +	unsigned int i;
> +
> +	if (vb2_is_busy(q))
> +		return -EBUSY;
> +
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {

The code would become more readable (at least in my opinion) if you just added 
a break here, and moved the code below after the loop. In case the requested 
format isn't found (i == NUM_FORMATS) you can then set i to 0 and proceed, 
that will select the first available format as a default.

> +			sdr->fmt_idx  = i;
> +			f->fmt.sdr.buffersize = formats[i].buffersize;
> +
> +			/*
> +			 * If a format demands one channel only out of two
> +			 * enabled channels, pick the 0th channel.
> +			 */
> +			if (formats[i].num_ch < sdr->num_hw_ch) {
> +				sdr->cur_ch_mask = BIT(0);
> +				sdr->num_cur_ch = formats[i].num_ch;
> +			} else {
> +				sdr->cur_ch_mask = sdr->hw_ch_mask;
> +				sdr->num_cur_ch = sdr->num_hw_ch;
> +			}
> +
> +			rdrif_dbg(1, sdr, "cur: idx %u mask %lu num %u\n",
> +				  i, sdr->cur_ch_mask, sdr->num_cur_ch);
> +			return 0;
> +		}
> +	}
> +
> +	if (rcar_drif_set_default_format(sdr)) {
> +		rdrif_err(sdr, "cannot set default format\n");
> +		return -EINVAL;
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> +	return 0;
> +}
> +
> +static int rcar_drif_try_fmt_sdr_cap(struct file *file, void *priv,
> +				     struct v4l2_format *f)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	unsigned int i;
> +
> +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
> +			f->fmt.sdr.buffersize = formats[i].buffersize;
> +			return 0;
> +		}
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;

The result of the TRY_FMT ioctl should not depend on the currently configured 
format. I would return a fixed format (for instance the first one in the 
formats array) in the default case.

> +	return 0;
> +}
> +
> +/* Tuner subdev ioctls */
> +static int rcar_drif_enum_freq_bands(struct file *file, void *priv,
> +				     struct v4l2_frequency_band *band)
> +{
> +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> +	struct v4l2_subdev *sd;
> +	int ret = 0;
> +
> +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> +		ret = v4l2_subdev_call(sd, tuner, enum_freq_bands, band);

This won't work as-is when you'll have multiple subdevs. As the driver only 
supports a single connected subdev at the moment, I suggest storing a pointer 
to that subdev in the rcar_drif_sdr structure, and calling operations on that 
subdev explicitly instead of looping over all subdevs. The comment holds for 
all other ioctls.

> +		if (ret)
> +			break;
> +	}
> +	return ret;
> +}

[snip]

> +static int rcar_drif_notify_bound(struct v4l2_async_notifier *notifier,
> +				   struct v4l2_subdev *subdev,
> +				   struct v4l2_async_subdev *asd)
> +{
> +	struct rcar_drif_sdr *sdr =
> +		container_of(notifier, struct rcar_drif_sdr, notifier);
> +
> +	/* Nothing to do at this point */

If there's nothing to do you can just leave the bound callback unimplemented, 
it's optional.

> +	rdrif_dbg(2, sdr, "bound asd: %s\n", asd->match.of.node->name);
> +	return 0;
> +}
> +
> +/* Sub-device registered notification callback */
> +static int rcar_drif_notify_complete(struct v4l2_async_notifier *notifier)
> +{
> +	struct rcar_drif_sdr *sdr =
> +		container_of(notifier, struct rcar_drif_sdr, notifier);
> +	struct v4l2_subdev *sd;
> +	int ret;
> +
> +	sdr->v4l2_dev.ctrl_handler = &sdr->ctrl_hdl;
> +
> +	ret = v4l2_device_register_subdev_nodes(&sdr->v4l2_dev);
> +	if (ret) {
> +		rdrif_err(sdr, "failed register subdev nodes ret %d\n", ret);
> +		return ret;
> +	}

Do you need to expose subdev nodes to userspace ? Can't everything be handled 
from the V4L2 SDR node ?

> +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> +		ret = v4l2_ctrl_add_handler(sdr->v4l2_dev.ctrl_handler,
> +					    sd->ctrl_handler, NULL);

Shouldn't you undo this somewhere when unbinding the subdevs ?

> +		if (ret) {
> +			rdrif_err(sdr, "failed ctrl add hdlr ret %d\n", ret);
> +			return ret;
> +		}
> +	}
> +	rdrif_dbg(2, sdr, "notify complete\n");
> +	return 0;
> +}

[snip]

> +/* Parse sub-devs (tuner) to find a matching device */
> +static int rcar_drif_parse_subdevs(struct rcar_drif_sdr *sdr,
> +				   struct device *dev)
> +{
> +	struct v4l2_async_notifier *notifier = &sdr->notifier;
> +	struct rcar_drif_async_subdev *rsd;
> +	struct device_node *node;
> +
> +	notifier->subdevs = devm_kzalloc(dev, sizeof(*notifier->subdevs),
> +					 GFP_KERNEL);
> +	if (!notifier->subdevs)
> +		return -ENOMEM;
> +
> +	node = of_graph_get_next_endpoint(dev->of_node, NULL);
> +	if (!node)
> +		return 0;
> +
> +	rsd = devm_kzalloc(dev, sizeof(*rsd), GFP_KERNEL);
> +	if (!rsd) {
> +		of_node_put(node);

If you move the allocation above of_graph_get_next_endpoint() you won't have 
to call of_node_put() in the error path.

> +		return -ENOMEM;
> +	}
> +
> +	notifier->subdevs[notifier->num_subdevs] = &rsd->asd;
> +	rsd->asd.match.of.node = of_graph_get_remote_port_parent(node);

Aren't you missing an of_node_put() on the returned node ? Or does the async 
framework take care of that ?

> +	of_node_put(node);
> +	if (!rsd->asd.match.of.node) {
> +		dev_warn(dev, "bad remote port parent\n");
> +		return -EINVAL;
> +	}
> +
> +	rsd->asd.match_type = V4L2_ASYNC_MATCH_OF;
> +	notifier->num_subdevs++;
> +
> +	/* Get the endpoint properties */
> +	rcar_drif_get_ep_properties(sdr, node);
> +	return 0;
> +}
> +
> +/* Check if the given device is the primary bond */
> +static bool rcar_drif_primary_bond(struct platform_device *pdev)
> +{
> +	if (of_find_property(pdev->dev.of_node, "renesas,primary-bond", NULL))
> +		return true;
> +
> +	return false;

How about

	return of_property_read_bool(pdev->dev.of_node,
				     "renesas,primary-bond");

> +}
> +
> +/* Get the bonded platform dev if enabled */
> +static struct platform_device *rcar_drif_enabled_bond(struct
> platform_device *p)
> +{
> +	struct device_node *np;
> +
> +	np = of_parse_phandle(p->dev.of_node, "renesas,bonding", 0);

The function takes a reference to np, you need to call of_node_put() on it 
(only if the returned pointer isn't NULL).

> +	if (np && of_device_is_available(np))
> +		return of_find_device_by_node(np);

of_find_device_by_node() takes a reference to the returned device, you need to 
call device_put() on it when you don't need it anymore.


> +	return NULL;
> +}
> +
> +/* Proble internal channel */
> +static int rcar_drif_channel_probe(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch;
> +	struct resource	*res;
> +	void __iomem *base;
> +	struct clk *clkp;

Maybe s/clkp/clk/ ?

> +	int ret;
> +
> +	/* Peripheral clock */
> +	clkp = devm_clk_get(&pdev->dev, "fck");
> +	if (IS_ERR(clkp)) {
> +		ret = PTR_ERR(clkp);
> +		dev_err(&pdev->dev, "clk get failed (%d)\n", ret);
> +		return ret;
> +	}

Isn't the clock managed automatically by runtime PM ?

> +	/* Register map */
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	base = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(base)) {
> +		ret = PTR_ERR(base);
> +		dev_err(&pdev->dev, "ioremap failed (%d)\n", ret);
> +		return ret;

devm_ioremap_resource() already prints an error message, you can remove this 
one.

> +	}
> +
> +	/* Reserve memory for enabled channel */
> +	ch = devm_kzalloc(&pdev->dev, sizeof(*ch), GFP_KERNEL);
> +	if (!ch) {
> +		ret = PTR_ERR(ch);
> +		dev_err(&pdev->dev, "failed alloc channel\n");

Memory allocation failures already print error messages, you can remove this 
one.

> +		return ret;
> +	}
> +	ch->pdev = pdev;
> +	ch->clkp = clkp;
> +	ch->base = base;
> +	ch->start = res->start;

If you allocated the ch structure first you could set the fields directly 
without a need for local variables.

> +	platform_set_drvdata(pdev, ch);
> +	return 0;
> +}
> +
> +static int rcar_drif_probe(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch, *b_ch = NULL;
> +	struct platform_device *b_pdev;
> +	struct rcar_drif_sdr *sdr;
> +	int ret;
> +
> +	/* Probe internal channel */
> +	ret = rcar_drif_channel_probe(pdev);
> +	if (ret)
> +		return ret;

I would have done it the other way around, inlining the 
rcar_drif_channel_probe() function here as that's the common case, and moving 
the V4L2 SDR device initialization code to a different function.

> +	/* Check if both channels of the bond are enabled */
> +	b_pdev = rcar_drif_enabled_bond(pdev);
> +	if (b_pdev) {
> +		/* Check if current channel acting as primary-bond */
> +		if (!rcar_drif_primary_bond(pdev)) {
> +			dev_notice(&pdev->dev, "probed\n");
> +			return 0;
> +		}
> +
> +		/* Check if the other device is probed */
> +		b_ch = platform_get_drvdata(b_pdev);
> +		if (!b_ch) {
> +			dev_info(&pdev->dev, "defer probe\n");
> +			return -EPROBE_DEFER;
> +		}

Isn't this all very racy ? What if the other channel's device is removed while 
this one is probed ?

> +		/* Set the other channel number */
> +		b_ch->num = 1;

Reading data from the other channel's private structure is one thing, but 
writing it makes me shiver :-S Could we make it so that 0 is the slave and 1 
the master ? That way you would set ch->num = 1 instead of b_ch->num = 1, 
keeping all modifications to the private structure local to the device being 
probed.

> +	}
> +
> +	/* Channel acting as SDR instance */
> +	ch = platform_get_drvdata(pdev);
> +	ch->acting_sdr = true;
> +
> +	/* Reserve memory for SDR structure */
> +	sdr = devm_kzalloc(&pdev->dev, sizeof(*sdr), GFP_KERNEL);
> +	if (!sdr) {
> +		ret = PTR_ERR(sdr);
> +		dev_err(&pdev->dev, "failed alloc drif context\n");
> +		return ret;
> +	}
> +	sdr->dev = &pdev->dev;
> +	sdr->hw_ch_mask = BIT(ch->num);
> +
> +	/* Establish links between SDR and channel(s) */
> +	ch->sdr = sdr;
> +	sdr->ch[ch->num] = ch;
> +	if (b_ch) {
> +		sdr->ch[b_ch->num] = b_ch;
> +		b_ch->sdr = sdr;
> +		sdr->hw_ch_mask |= BIT(b_ch->num);
> +	}
> +	sdr->num_hw_ch = hweight_long(sdr->hw_ch_mask);
> +
> +	/* Validate any supported format for enabled channels */
> +	ret = rcar_drif_set_default_format(sdr);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed to set default format\n");
> +		return ret;
> +	}
> +
> +	/* Set defaults */
> +	sdr->hwbuf_size = RCAR_DRIF_DEFAULT_HWBUF_SIZE;
> +
> +	mutex_init(&sdr->v4l2_mutex);
> +	mutex_init(&sdr->vb_queue_mutex);
> +	spin_lock_init(&sdr->queued_bufs_lock);
> +	INIT_LIST_HEAD(&sdr->queued_bufs);
> +
> +	/* Init videobuf2 queue structure */
> +	sdr->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
> +	sdr->vb_queue.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
> +	sdr->vb_queue.drv_priv = sdr;
> +	sdr->vb_queue.buf_struct_size = sizeof(struct rcar_drif_frame_buf);
> +	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
> +	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
> +	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> +
> +	/* Init videobuf2 queue */
> +	ret = vb2_queue_init(&sdr->vb_queue);
> +	if (ret) {
> +		dev_err(sdr->dev, "could not initialize vb2 queue\n");
> +		return ret;
> +	}
> +
> +	/* Register the v4l2_device */
> +	ret = v4l2_device_register(&pdev->dev, &sdr->v4l2_dev);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed v4l2_device_register (%d)\n", ret);

Maybe "failed to register V4L2 device" to make it a real sentence ? :-)

> +		return ret;
> +	}
> +
> +	/*
> +	 * Parse subdevs after v4l2_device_register because if the subdev
> +	 * is already probed, bound and complete will be called immediately
> +	 */
> +	ret = rcar_drif_parse_subdevs(sdr, &pdev->dev);
> +	if (ret)
> +		goto err_unreg_v4l2;
> +
> +	sdr->notifier.bound = rcar_drif_notify_bound;
> +	sdr->notifier.complete = rcar_drif_notify_complete;
> +
> +	v4l2_ctrl_handler_init(&sdr->ctrl_hdl, 10);

Possibly a stupid question, why 10, if you don't create any control in this 
driver ?

> +	/* Register notifier */
> +	ret = v4l2_async_notifier_register(&sdr->v4l2_dev, &sdr->notifier);
> +	if (ret < 0) {
> +		dev_err(sdr->dev, "notifier registration failed (%d)\n", ret);
> +		goto err_free_ctrls;
> +	}
> +
> +	/* Init video_device structure */
> +	sdr->vdev = video_device_alloc();
> +	if (!sdr->vdev) {
> +		ret = -ENOMEM;
> +		goto err_unreg_notif;
> +	}
> +	snprintf(sdr->vdev->name, sizeof(sdr->vdev->name), "R-Car DRIF");
> +	sdr->vdev->fops = &rcar_drif_fops;
> +	sdr->vdev->ioctl_ops = &rcar_drif_ioctl_ops;
> +	sdr->vdev->release = video_device_release;
> +	sdr->vdev->lock = &sdr->v4l2_mutex;
> +	sdr->vdev->queue = &sdr->vb_queue;
> +	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
> +	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
> +	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
> +	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
> +		V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
> +	video_set_drvdata(sdr->vdev, sdr);
> +
> +	/* Register V4L2 SDR device */
> +	ret = video_register_device(sdr->vdev, VFL_TYPE_SDR, -1);
> +	if (ret) {
> +		dev_err(sdr->dev, "failed video_register_device (%d)\n", ret);

Same here, "failed to register video device" ?

> +		goto err_unreg_notif;
> +	}
> +
> +	dev_notice(sdr->dev, "probed\n");

Do you think this message is really useful ? I believe it would just add a bit 
more noise to the kernel log, without any real use.

> +	return 0;
> +
> +err_unreg_notif:
> +	video_device_release(sdr->vdev);
> +	v4l2_async_notifier_unregister(&sdr->notifier);
> +err_free_ctrls:
> +	v4l2_ctrl_handler_free(&sdr->ctrl_hdl);
> +err_unreg_v4l2:
> +	v4l2_device_unregister(&sdr->v4l2_dev);
> +	return ret;
> +}
> +
> +static int rcar_drif_remove(struct platform_device *pdev)
> +{
> +	struct rcar_drif *ch = platform_get_drvdata(pdev);
> +	struct rcar_drif_sdr *sdr = ch->sdr;
> +
> +	if (!ch->acting_sdr) {

Isn't it possible to check the channel number instead and remove the 
acting_sdr field ?

> +		/* Nothing to do */
> +		dev_notice(&pdev->dev, "removed\n");
> +		return 0;
> +	}
> +
> +	/* SDR instance */
> +	v4l2_ctrl_handler_free(sdr->vdev->ctrl_handler);
> +	v4l2_async_notifier_unregister(&sdr->notifier);
> +	v4l2_device_unregister(&sdr->v4l2_dev);
> +	video_unregister_device(sdr->vdev);
> +	dev_notice(&pdev->dev, "removed\n");

Even more than the probed message, I think this one can go away.

> +	return 0;
> +}
> +
> +static int __maybe_unused rcar_drif_suspend(struct device *dev)
> +{
> +	return 0;

Maybe a /* FIXME: Implement suspend/resume support */ ?

> +}
> +
> +static int __maybe_unused rcar_drif_resume(struct device *dev)
> +{
> +	return 0;

Same here ?

> +}

[snip]

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
  2017-02-07 15:02 ` [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding Ramesh Shanmugasundaram
       [not found]   ` <1486479757-32128-7-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
@ 2017-04-11 22:35   ` Laurent Pinchart
  1 sibling, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 22:35 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	chris.paterson2, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Ramesh,

Thank you for the patch.

On Tuesday 07 Feb 2017 15:02:36 Ramesh Shanmugasundaram wrote:
> Add binding documentation for Renesas R-Car Digital Radio Interface
> (DRIF) controller.
> 
> Signed-off-by: Ramesh Shanmugasundaram
> <ramesh.shanmugasundaram@bp.renesas.com>
> ---
>  .../devicetree/bindings/media/renesas,drif.txt     | 186 ++++++++++++++++++
>  1 file changed, 186 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/renesas,drif.txt
> 
> diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt
> b/Documentation/devicetree/bindings/media/renesas,drif.txt new file mode
> 100644
> index 0000000..6315609
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
> @@ -0,0 +1,186 @@
> +Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
> +------------------------------------------------------------
> +
> +R-Car Gen3 DRIF is a SPI like receive only slave device. A general
> +representation of DRIF interfacing with a master device is shown below.
> +
> ++---------------------+                +---------------------+
> +|                     |-----SCK------->|CLK                  |
> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> +|                     |-----SD0------->|D0                   |
> +|                     |-----SD1------->|D1                   |
> ++---------------------+                +---------------------+
> +
> +As per the datasheet, each DRIF channel (drifn) is made up of two internal
> +channels (drifn0 & drifn1). These two internal channels share the common
> +CLK & SYNC. Each internal channel has its own dedicated resources like
> +irq, dma channels, address space & clock. This internal split is not
> +visible to the external master device.
> +
> +The device tree model represents each internal channel as a separate node.
> +The internal channels sharing the CLK & SYNC are tied together by their
> +phandles using a new property called "renesas,bonding". For the rest of
> +the documentation, unless explicitly stated, the word channel implies an
> +internal channel.
> +
> +When both internal channels are enabled they need to be managed together
> +as one (i.e.) they cannot operate alone as independent devices. Out of the
> +two, one of them needs to act as a primary device that accepts common
> +properties of both the internal channels. This channel is identified by a
> +new property called "renesas,primary-bond".
> +
> +To summarize,
> +   - When both the internal channels that are bonded together are enabled,
> +     the zeroth channel is selected as primary-bond. This channels accepts
> +     properties common to all the members of the bond.
> +   - When only one of the bonded channels need to be enabled, the property
> +     "renesas,bonding" or "renesas,primary-bond" will have no effect. That
> +     enabled channel can act alone as any other independent device.
> +
> +Required properties of an internal channel:
> +-------------------------------------------
> +- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of
> R8A7795 SoC.
> +	      "renesas,rcar-gen3-drif" 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
> +	      followed by the generic version.
> +- reg: offset and length of that channel.
> +- interrupts: associated with that channel.
> +- clocks: phandle and clock specifier of that channel.
> +- clock-names: clock input name string: "fck".
> +- dmas: phandles to the DMA channels.
> +- dma-names: names of the DMA channel: "rx".
> +- renesas,bonding: phandle to the other channel.
> +
> +Optional properties of an internal channel:
> +-------------------------------------------
> +- power-domains: phandle to the respective power domain.
> +
> +Required properties of an internal channel when:
> +	- It is the only enabled channel of the bond (or)
> +	- If it acts as primary among enabled bonds
> +--------------------------------------------------------
> +- pinctrl-0: pin control group to be used for this channel.
> +- pinctrl-names: must be "default".
> +- renesas,primary-bond: empty property indicating the channel acts as
> primary
> +			among the bonded channels.
> +- port: child port node of a channel that defines the local and remote
> +	endpoints. The remote endpoint is assumed to be a third party tuner
> +	device endpoint.

You should refer to the OF graphs bindings here. How about the following to 
document the port node ?

- port: child port node corresponding to the data input, in accordance with 
the video interface bindings defined in
Documentation/devicetree/bindings/media/video-interfaces.txt. The port node 
must contain at least one endpoint.

> +Optional endpoint property:
> +---------------------------
> +- renesas,sync-active  : Indicates sync signal polarity, 0/1 for low/high
> +			 respectively. This property maps to SYNCAC bit in the
> +			 hardware manual. The default is 1 (active high)
> +
> +Example
> +--------
> +
> +SoC common dtsi file
> +
> +		drif00: rif@e6f40000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f40000 0 0x64>;
> +			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 515>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif01>;
> +			status = "disabled";
> +		};
> +
> +		drif01: rif@e6f50000 {
> +			compatible = "renesas,r8a7795-drif",
> +				     "renesas,rcar-gen3-drif";
> +			reg = <0 0xe6f50000 0 0x64>;
> +			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
> +			clocks = <&cpg CPG_MOD 514>;
> +			clock-names = "fck";
> +			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
> +			dma-names = "rx", "rx";
> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> +			renesas,bonding = <&drif00>;
> +			status = "disabled";
> +		};
> +
> +
> +Board specific dts file
> +
> +(1) Both internal channels enabled:
> +-----------------------------------
> +
> +When interfacing with a third party tuner device with two data pins as
> shown
> +below.
> +
> ++---------------------+                +---------------------+
> +|                     |-----SCK------->|CLK                  |
> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> +|                     |-----SD0------->|D0                   |
> +|                     |-----SD1------->|D1                   |
> ++---------------------+                +---------------------+
> +
> +pfc {
> +	...
> +
> +	drif0_pins: drif0 {
> +		groups = "drif0_ctrl_a", "drif0_data0_a",
> +				 "drif0_data1_a";
> +		function = "drif0";
> +	};
> +	...
> +}
> +
> +&drif00 {
> +	pinctrl-0 = <&drif0_pins>;
> +	pinctrl-names = "default";
> +	renesas,primary-bond;
> +	status = "okay";
> +	port {
> +		drif0_ep: endpoint {
> +		     remote-endpoint = <&tuner_ep>;
> +		};
> +	};
> +};
> +
> +&drif01 {
> +	status = "okay";
> +};
> +
> +(2) Internal channel 1 alone is enabled:
> +----------------------------------------
> +
> +When interfacing with a third party tuner device with one data pin as shown
> +below.
> +
> ++---------------------+                +---------------------+
> +|                     |-----SCK------->|CLK                  |
> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> +|                     |                |D0 (unused)          |
> +|                     |-----SD-------->|D1                   |
> ++---------------------+                +---------------------+
> +
> +pfc {
> +	...
> +
> +	drif0_pins: drif0 {
> +		groups = "drif0_ctrl_a", "drif0_data1_a";
> +		function = "drif0";
> +	};
> +	...
> +}
> +
> +&drif01 {
> +	pinctrl-0 = <&drif0_pins>;
> +	pinctrl-names = "default";
> +	status = "okay";
> +	port {
> +		drif0_ep: endpoint {
> +		     remote-endpoint = <&tuner_ep>;
> +		     renesas,syncac-active = <0>;
> +		};
> +	};
> +};

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding
  2017-02-16 11:02       ` Ramesh Shanmugasundaram
@ 2017-04-11 22:41         ` Laurent Pinchart
  0 siblings, 0 replies; 31+ messages in thread
From: Laurent Pinchart @ 2017-04-11 22:41 UTC (permalink / raw)
  To: Ramesh Shanmugasundaram
  Cc: Rob Herring, mark.rutland, mchehab, hverkuil, sakari.ailus,
	crope, Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hello,

On Thursday 16 Feb 2017 11:02:55 Ramesh Shanmugasundaram wrote:
> Hi Rob,
> 
> Thank you for the review comments.
> 
> > Subject: Re: [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF
> > binding
> > 
> > On Tue, Feb 07, 2017 at 03:02:36PM +0000, Ramesh Shanmugasundaram wrote:
> >> Add binding documentation for Renesas R-Car Digital Radio Interface
> >> (DRIF) controller.
> >> 
> >> Signed-off-by: Ramesh Shanmugasundaram
> >> <ramesh.shanmugasundaram@bp.renesas.com>
> >> ---
> >> 
> >>  .../devicetree/bindings/media/renesas,drif.txt     | 186 ++++++++++++++
> >>  1 file changed, 186 insertions(+)
> >>  create mode 100644
> >> 
> >> Documentation/devicetree/bindings/media/renesas,drif.txt
> >> 
> >> diff --git a/Documentation/devicetree/bindings/media/renesas,drif.txt
> >> b/Documentation/devicetree/bindings/media/renesas,drif.txt
> >> new file mode 100644
> >> index 0000000..6315609
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/media/renesas,drif.txt
> >> @@ -0,0 +1,186 @@
> >> +Renesas R-Car Gen3 Digital Radio Interface controller (DRIF)
> >> +------------------------------------------------------------
> >> +
> >> +R-Car Gen3 DRIF is a SPI like receive only slave device. A general
> >> +representation of DRIF interfacing with a master device is shown below.
> >> +
> >> ++---------------------+                +---------------------+
> >> +|                     |-----SCK------->|CLK                  |
> >> +|       Master        |-----SS-------->|SYNC  DRIFn (slave)  |
> >> +|                     |-----SD0------->|D0                   |
> >> +|                     |-----SD1------->|D1                   |
> >> ++---------------------+                +---------------------+
> >> +
> >> +As per the datasheet, each DRIF channel (drifn) is made up of two
> >> +internal channels (drifn0 & drifn1). These two internal channels
> >> +share the common CLK & SYNC. Each internal channel has its own
> >> +dedicated resources like irq, dma channels, address space & clock.
> >> +This internal split is not visible to the external master device.
> >> +
> >> +The device tree model represents each internal channel as a separate
> >> node.
> >> +The internal channels sharing the CLK & SYNC are tied together by
> >> +their phandles using a new property called "renesas,bonding". For the
> >> +rest of the documentation, unless explicitly stated, the word channel
> >> +implies an internal channel.
> >> +
> >> +When both internal channels are enabled they need to be managed
> >> +together as one (i.e.) they cannot operate alone as independent
> >> +devices. Out of the two, one of them needs to act as a primary device
> >> +that accepts common properties of both the internal channels. This
> >> +channel is identified by a new property called "renesas,primary-bond".
> >> +
> >> +To summarize,
> >> +   - When both the internal channels that are bonded together are
> >> enabled,
> >> +     the zeroth channel is selected as primary-bond. This channels
> >> accepts
> >> +     properties common to all the members of the bond.
> >> +   - When only one of the bonded channels need to be enabled, the
> >> property
> >> +     "renesas,bonding" or "renesas,primary-bond" will have no effect.
> >> That
> >> +     enabled channel can act alone as any other independent device.
> >> +
> >> +Required properties of an internal channel:
> >> +-------------------------------------------
> >> +- compatible: "renesas,r8a7795-drif" if DRIF controller is a part of
> >> R8A7795 SoC.
> >> +	      "renesas,rcar-gen3-drif" 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
> >> +	      followed by the generic version.
> >> +- reg: offset and length of that channel.
> >> +- interrupts: associated with that channel.
> >> +- clocks: phandle and clock specifier of that channel.
> >> +- clock-names: clock input name string: "fck".
> >> +- dmas: phandles to the DMA channels.
> >> +- dma-names: names of the DMA channel: "rx".
> >> +- renesas,bonding: phandle to the other channel.
> >> +
> >> +Optional properties of an internal channel:
> >> +-------------------------------------------
> >> +- power-domains: phandle to the respective power domain.
> >> +
> >> +Required properties of an internal channel when:
> >> +	- It is the only enabled channel of the bond (or)
> >> +	- If it acts as primary among enabled bonds
> >> +--------------------------------------------------------
> >> +- pinctrl-0: pin control group to be used for this channel.
> >> +- pinctrl-names: must be "default".
> >> +- renesas,primary-bond: empty property indicating the channel acts as
> >> primary
> >> +			among the bonded channels.
> >> +- port: child port node of a channel that defines the local and remote
> >> +	endpoints. The remote endpoint is assumed to be a third party tuner
> >> +	device endpoint.
> >> +
> >> +Optional endpoint property:
> >> +---------------------------
> >> +- renesas,sync-active  : Indicates sync signal polarity, 0/1 for
> >> low/high
> >> +			 respectively. This property maps to SYNCAC bit in the
> >> +			 hardware manual. The default is 1 (active high)
> > 
> > Why does this belong in the endpoint? I'd prefer to not have vendor
> > specific properties in endpoints. Is this a property of the tuner or DRIF?

In the general case, the sync signal polarity is a property of the tuner (and 
in some cases it could even be configurable on the tuner side), which could 
then be queried at runtime from the tuner by the DRIF driver. However, there 
could be logic on the board that would invert the polarity, so we need to 
specify it on the DRIF side as well. As the polarity can differ between 
different tuners, it makes sense to specify it in the endpoint, in case 
multiple tuners are connected (keeping in mind that only one of them can be 
used at a time). However, I we don't support connecting multiple tuners at 
this time, and I don't think we ever will, but I could be wrong there.

> This property is similar to the properties in
> Documentation/devicetree/bindings/media/video-interfaces.txt (e.g.
> hsync-active, vsync-active).Hence, Laurent & Hans suggested this to be
> defined as an endpoint property and try to standardize it.
> 
> I think I see your point. As endpoint properties can be defined on both
> endpoints, having a vendor specific property is a problem with a third
> party tuner. We could remove the vendor tag and make it  a generic property
> "sync-active", if you are OK with it?
>
> This property can be defined for both tuner and DRIF. However, it would
> mostly be a constant in the case of tuner because as per I2S spec,
> transmitter WS (sync) changes from high->low & low->high always. Only DRIF
> allows the option to latch when WS high->low or low->high - both cases.
> 
> In a traditional use case it is always WS high->low latching to get the
> first data. However, with DRIF & MAX2175 combo, our latest investigations
> reveal that latching when WS low->high provided better synchronization on
> all cases. There is no loss of data by doing this. Hence, it would be nice
> to retain this as a configurable property.
> 
> Please advice.
> 
> >> +
> >> +Example
> >> +--------
> >> +
> >> +SoC common dtsi file
> >> +
> >> +		drif00: rif@e6f40000 {
> >> +			compatible = "renesas,r8a7795-drif",
> >> +				     "renesas,rcar-gen3-drif";
> >> +			reg = <0 0xe6f40000 0 0x64>;
> >> +			interrupts = <GIC_SPI 12 IRQ_TYPE_LEVEL_HIGH>;
> >> +			clocks = <&cpg CPG_MOD 515>;
> >> +			clock-names = "fck";
> >> +			dmas = <&dmac1 0x20>, <&dmac2 0x20>;
> >> +			dma-names = "rx", "rx";
> >> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> >> +			renesas,bonding = <&drif01>;
> >> +			status = "disabled";
> > 
> > Don't put "status" in examples.
> 
> OK. Will note this down for future patches. Will be corrected in the next
> version.
>
> >> +		};
> >> +
> >> +		drif01: rif@e6f50000 {
> >> +			compatible = "renesas,r8a7795-drif",
> >> +				     "renesas,rcar-gen3-drif";
> >> +			reg = <0 0xe6f50000 0 0x64>;
> >> +			interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
> >> +			clocks = <&cpg CPG_MOD 514>;
> >> +			clock-names = "fck";
> >> +			dmas = <&dmac1 0x22>, <&dmac2 0x22>;
> >> +			dma-names = "rx", "rx";
> >> +			power-domains = <&sysc R8A7795_PD_ALWAYS_ON>;
> >> +			renesas,bonding = <&drif00>;
> >> +			status = "disabled";
> >> +		};
> >> +
> >> +
> >> +Board specific dts file
> > 
> > Chip vs. board in not relevant to the binding doc. Please combine them
> > here in your example.
> 
> Will do.

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 31+ messages in thread

* RE: [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support
  2017-04-11 22:24       ` Laurent Pinchart
  (?)
@ 2017-04-18 17:12       ` Ramesh Shanmugasundaram
  -1 siblings, 0 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-04-18 17:12 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Laurent,

Many thanks for your time & the review comments. I have agreed to most of the comments and a few need further discussion. Could you please take a look at those?

> On Tuesday 07 Feb 2017 15:02:37 Ramesh Shanmugasundaram wrote:
> > This patch adds Digital Radio Interface (DRIF) support to R-Car Gen3
> SoCs.
> > The driver exposes each instance of DRIF as a V4L2 SDR device. A DRIF
> > device represents a channel and each channel can have one or two
> > sub-channels respectively depending on the target board.
> >
> > DRIF supports only Rx functionality. It receives samples from a RF
> > frontend tuner chip it is interfaced with. The combination of DRIF and
> > the tuner device, which is registered as a sub-device, determines the
> > receive sample rate and format.
> >
> > In order to be compliant as a V4L2 SDR device, DRIF needs to bind with
> > the tuner device, which can be provided by a third party vendor. DRIF
> > acts as a slave device and the tuner device acts as a master
> > transmitting the samples. The driver allows asynchronous binding of a
> > tuner device that is registered as a v4l2 sub-device. The driver can
> > learn about the tuner it is interfaced with based on port endpoint
> > properties of the device in device tree. The V4L2 SDR device inherits
> > the controls exposed by the tuner device.
> >
> > The device can also be configured to use either one or both of the
> > data pins at runtime based on the master (tuner) configuration.
> >
> > Signed-off-by: Ramesh Shanmugasundaram
> > <ramesh.shanmugasundaram@bp.renesas.com>
> > ---
> >  drivers/media/platform/Kconfig     |   25 +
> >  drivers/media/platform/Makefile    |    1 +
> >  drivers/media/platform/rcar_drif.c | 1534
> > +++++++++++++++++++++++++++++++++
> >  3 files changed, 1560 insertions(+)
> >  create mode 100644 drivers/media/platform/rcar_drif.c
> 
> [snip]
> 
> > diff --git a/drivers/media/platform/rcar_drif.c
> > b/drivers/media/platform/rcar_drif.c new file mode 100644 index
> > 0000000..88950e3
> > --- /dev/null
> > +++ b/drivers/media/platform/rcar_drif.c
> > @@ -0,0 +1,1534 @@
> 
> [snip]
> 
> > +/*
> > + * The R-Car DRIF is a receive only MSIOF like controller with an
> > + * external master device driving the SCK. It receives data into a
> > +FIFO,
> > + * then this driver uses the SYS-DMAC engine to move the data from
> > + * the device to memory.
> > + *
> > + * Each DRIF channel DRIFx (as per datasheet) contains two internal
> > + * channels DRIFx0 & DRIFx1 within itself with each having its own
> > resources
> > + * like module clk, register set, irq and dma. These internal
> > + channels
> > share
> > + * common CLK & SYNC from master. The two data pins D0 & D1 shall be
> > + * considered to represent the two internal channels. This internal
> > + split
> > + * is not visible to the master device.
> > + *
> > + * Depending on the master device, a DRIF channel can use
> > + *  (1) both internal channels (D0 & D1) to receive data in parallel
> > + (or)
> > + *  (2) one internal channel (D0 or D1) to receive data
> > + *
> > + * The primary design goal of this controller is to act as Digitial
> > + Radio
> 
> s/Digitial/Digital/

Agreed

> 
> > + * Interface that receives digital samples from a tuner device. Hence
> > + the
> > + * driver exposes the device as a V4L2 SDR device. In order to
> > + qualify as
> > + * a V4L2 SDR device, it should possess tuner interface as mandated
> > + by the
> > + * framework. This driver expects a tuner driver (sub-device) to bind
> > + * asynchronously with this device and the combined drivers shall
> > + expose
> > + * a V4L2 compliant SDR device. The DRIF driver is independent of the
> > + * tuner vendor.
> > + *
> > + * The DRIF h/w can support I2S mode and Frame start synchronization
> > + pulse
> > mode.
> > + * This driver is tested for I2S mode only because of the
> > + availability of
> > + * suitable master devices. Hence, not all configurable options of
> > + DRIF h/w
> > + * like lsb/msb first, syncdl, dtdl etc. are exposed via DT and I2S
> > defaults
> > + * are used. These can be exposed later if needed after testing.
> > + */
> 
> [snip]
> 
> > +#define to_rcar_drif_buf_pair(sdr, ch_num,
> > idx)	(sdr->ch[!(ch_num)]->buf[idx])
> 
> You should enclose both sdr and idx in parenthesis, as they can be
> expressions.

Agreed.

> 
> > +
> > +#define for_each_rcar_drif_channel(ch, ch_mask)			\
> > +	for_each_set_bit(ch, ch_mask, RCAR_DRIF_MAX_CHANNEL)
> > +
> > +static const unsigned int num_hwbufs = 32;
> 
> Is there a specific reason to make this a static const instead of a
> #define ?

Just style only. The #define needs a RCAR_DRIF_ prefix and I used this value in few places. The #define makes the statements longer.

> 
> > +/* Debug */
> > +static unsigned int debug;
> > +module_param(debug, uint, 0644);
> > +MODULE_PARM_DESC(debug, "activate debug info");
> > +
> > +#define rdrif_dbg(level, sdr, fmt, arg...)				\
> > +	v4l2_dbg(level, debug, &sdr->v4l2_dev, fmt, ## arg)
> > +
> > +#define rdrif_err(sdr, fmt, arg...)					\
> > +	dev_err(sdr->v4l2_dev.dev, fmt, ## arg)
> > +
> > +/* Stream formats */
> > +struct rcar_drif_format {
> > +	u32	pixelformat;
> > +	u32	buffersize;
> > +	u32	wdlen;
> > +	u32	num_ch;
> > +};
> > +
> > +/* Format descriptions for capture */ static const struct
> > +rcar_drif_format formats[] = {
> > +	{
> > +		.pixelformat	= V4L2_SDR_FMT_PCU16BE,
> > +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> > +		.wdlen		= 16,
> > +		.num_ch	= 2,
> 
> How about aligning the = as in the other lines ?

Agreed

> 
> num_ch is always set to 2. Should we remove it for now, and add it back
> later when we'll support single-channel formats ? I think we should avoid
> carrying dead code.

Actually single channel support is tested internally. If single & dual channels are already supported any future change will be just adding the new SDR format only, which could be relatively trivial for this driver. To add new SDR formats today I need to enable appropriate format in the tuner code too, which could be done later on a need basis by us or others.

> 
> > +	},
> > +	{
> > +		.pixelformat	= V4L2_SDR_FMT_PCU18BE,
> > +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> > +		.wdlen		= 18,
> > +		.num_ch	= 2,
> > +	},
> > +	{
> > +		.pixelformat	= V4L2_SDR_FMT_PCU20BE,
> > +		.buffersize	= RCAR_SDR_BUFFER_SIZE,
> > +		.wdlen		= 20,
> > +		.num_ch	= 2,
> > +	},
> > +};
> > +
> > +static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
> 
> Same question here, can't this be a define ? I think I'd even avoid
> NUM_FORMATS completely and use ARRAY_SIZE(formats) directly in the code,
> to make the boundary check more explicit when iterating over the array.

Agreed.

> 
> > +
> > +/* Buffer for a received frame from one or both internal channels */
> > +struct rcar_drif_frame_buf {
> > +	/* Common v4l buffer stuff -- must be first */
> > +	struct vb2_v4l2_buffer vb;
> > +	struct list_head list;
> > +};
> > +
> > +struct rcar_drif_async_subdev {
> > +	struct v4l2_subdev *sd;
> > +	struct v4l2_async_subdev asd;
> > +};
> > +
> > +/* DMA buffer */
> > +struct rcar_drif_hwbuf {
> > +	void *addr;			/* CPU-side address */
> > +	unsigned int status;		/* Buffer status flags */
> > +};
> > +
> > +/* Internal channel */
> > +struct rcar_drif {
> > +	struct rcar_drif_sdr *sdr;	/* Group device */
> > +	struct platform_device *pdev;	/* Channel's pdev */
> > +	void __iomem *base;		/* Base register address */
> > +	resource_size_t start;		/* I/O resource offset */
> > +	struct dma_chan *dmach;		/* Reserved DMA channel */
> > +	struct clk *clkp;		/* Module clock */
> > +	struct rcar_drif_hwbuf *buf[RCAR_DRIF_MAX_NUM_HWBUFS]; /* H/W bufs
> */
> > +	dma_addr_t dma_handle;		/* Handle for all bufs */
> > +	unsigned int num;		/* Channel number */
> > +	bool acting_sdr;		/* Channel acting as SDR device */
> > +};
> > +
> > +/* DRIF V4L2 SDR */
> > +struct rcar_drif_sdr {
> > +	struct device *dev;		/* Platform device */
> > +	struct video_device *vdev;	/* V4L2 SDR device */
> > +	struct v4l2_device v4l2_dev;	/* V4L2 device */
> > +
> > +	/* Videobuf2 queue and queued buffers list */
> > +	struct vb2_queue vb_queue;
> > +	struct list_head queued_bufs;
> > +	spinlock_t queued_bufs_lock;	/* Protects queued_bufs */
> > +
> > +	struct mutex v4l2_mutex;	/* To serialize ioctls */
> > +	struct mutex vb_queue_mutex;	/* To serialize streaming ioctls */
> > +	struct v4l2_ctrl_handler ctrl_hdl;	/* SDR control handler */
> > +	struct v4l2_async_notifier notifier;	/* For subdev (tuner) */
> > +
> > +	/* Current V4L2 SDR format array index */
> > +	unsigned int fmt_idx;
> 
> Instead of storing the index I would store a pointer to the corresponding
> rcar_drif_format, looking up information about the current format will
> then be easier.
> 

Agreed

> > +
> > +	/* Device tree SYNC properties */
> > +	u32 mdr1;
> > +
> > +	/* Internals */
> > +	struct rcar_drif *ch[RCAR_DRIF_MAX_CHANNEL]; /* DRIFx0,1 */
> > +	unsigned long hw_ch_mask;	/* Enabled channels per DT */
> > +	unsigned long cur_ch_mask;	/* Used channels for an SDR FMT */
> > +	u32 num_hw_ch;			/* Num of DT enabled channels */
> > +	u32 num_cur_ch;			/* Num of used channels */
> > +	u32 hwbuf_size;			/* Each DMA buffer size */
> > +	u32 produced;			/* Buffers produced by sdr dev */
> > +};
> > +
> > +/* Allocate buffer context */
> > +static int rcar_drif_alloc_bufctxt(struct rcar_drif_sdr *sdr) {
> > +	struct rcar_drif_hwbuf *bufctx;
> > +	unsigned int i, idx;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		bufctx = kcalloc(num_hwbufs, sizeof(*bufctx), GFP_KERNEL);
> 
> How about embedding the buffer contexts in the rcar_drif structure instead
> of just storing pointers there ? The rcar_drif_hwbuf structure is pretty
> small, it won't make a big difference, and will simplify the code.

Agreed

> 
> > +		if (!bufctx)
> > +			return -ENOMEM;
> > +
> > +		for (idx = 0; idx < num_hwbufs; idx++)
> > +			sdr->ch[i]->buf[idx] = bufctx + idx;
> > +	}
> > +	return 0;
> > +}
> 
> [snip]
> 
> > +/* Release DMA channel */
> > +static void rcar_drif_release_dmachannel(struct rcar_drif_sdr *sdr)
> 
> I would name the function rcar_drif_release_dma_channels as it handles all
> channels. Same for rcar_drif_alloc_dma_channels.

Agreed.

> 
> > +{
> > +	unsigned int i;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask)
> > +		if (sdr->ch[i]->dmach) {
> > +			dma_release_channel(sdr->ch[i]->dmach);
> > +			sdr->ch[i]->dmach = NULL;
> > +		}
> > +}
> > +
> > +/* Allocate DMA channel */
> > +static int rcar_drif_alloc_dmachannel(struct rcar_drif_sdr *sdr) {
> > +	struct dma_slave_config dma_cfg;
> > +	unsigned int i;
> > +	int ret = -ENODEV;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		struct rcar_drif *ch = sdr->ch[i];
> > +
> > +		ch->dmach = dma_request_slave_channel(&ch->pdev->dev, "rx");
> > +		if (!ch->dmach) {
> > +			rdrif_err(sdr, "ch%u: dma channel req failed\n", i);
> > +			goto dmach_error;
> > +		}
> > +
> > +		/* Configure slave */
> > +		memset(&dma_cfg, 0, sizeof(dma_cfg));
> > +		dma_cfg.src_addr = (phys_addr_t)(ch->start +
> RCAR_DRIF_SIRFDR);
> > +		dma_cfg.dst_addr = 0;
> 
> This isn't needed as you memset the whole structure to 0.

Agreed

> 
> > +		dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> > +		ret = dmaengine_slave_config(ch->dmach, &dma_cfg);
> > +		if (ret) {
> > +			rdrif_err(sdr, "ch%u: dma slave config failed\n", i);
> > +			goto dmach_error;
> > +		}
> > +	}
> > +	return 0;
> > +
> > +dmach_error:
> > +	rcar_drif_release_dmachannel(sdr);
> > +	return ret;
> > +}
> 
> [snip]
> 
> > +/* Set MDR defaults */
> > +static inline void rcar_drif_set_mdr1(struct rcar_drif_sdr *sdr) {
> > +	unsigned int i;
> > +
> > +	/* Set defaults for enabled internal channels */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		/* Refer MSIOF section in manual for this register setting */
> > +		writel(RCAR_DRIF_SITMDR1_PCON,
> > +		       sdr->ch[i]->base + RCAR_DRIF_SITMDR1);
> 
> I would create a rcar_drif_write(struct rcar_drif *ch, u32 offset, u32
> data) function, the code will become clearer. Same for the read operation.

Agreed

> 
> > +		/* Setup MDR1 value */
> > +		writel(sdr->mdr1, sdr->ch[i]->base + RCAR_DRIF_SIRMDR1);
> > +
> > +		rdrif_dbg(2, sdr, "ch%u: mdr1 = 0x%08x",
> > +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR1));
> 
> Once you've debugged the driver I'm not sure those debugging statements
> are still needed.

I would like to keep this statement please. This single register print would clarify if a user selected DT options and this could be enabled at runtime on a deployed board.
 
> 
> > +	}
> > +}
> > +
> > +/* Extract bitlen and wdcnt from given word length */ static int
> > +rcar_drif_convert_wdlen(struct rcar_drif_sdr *sdr,
> > +				   u32 wdlen, u32 *bitlen, u32 *wdcnt) {
> > +	unsigned int i, nr_wds;
> > +
> > +	/* FIFO register size is 32 bits */
> > +	for (i = 0; i < 32; i++) {
> > +		nr_wds = wdlen % (32 - i);
> > +		if (nr_wds == 0) {
> > +			*bitlen = 32 - i;
> > +			*wdcnt = wdlen / *bitlen;
> 
> Can't you store the bitlen and wdcnt values in the rcar_drif_format
> structure instead of recomputing them every time ?

Agreed

> 
> > +			break;
> > +		}
> > +	}
> > +
> > +	/* Sanity check range */
> > +	if (i == 32 || !(*bitlen >= 8 && *bitlen <= 32) ||
> > +	    !(*wdcnt >= 1 && *wdcnt <= 64)) {
> > +		rdrif_err(sdr, "invalid wdlen %u configured\n", wdlen);
> > +		return -EINVAL;
> 
> You shouldn't have invalid wdlen values in the driver. I would remove this
> check as it makes error handling in the caller more complex.

Agreed

> 
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +/* Set DRIF receive format */
> > +static int rcar_drif_set_format(struct rcar_drif_sdr *sdr) {
> > +	u32 bitlen, wdcnt, wdlen;
> > +	unsigned int i;
> > +	int ret = -EINVAL;
> > +
> > +	wdlen = formats[sdr->fmt_idx].wdlen;
> > +	rdrif_dbg(2, sdr, "setfmt: idx %u, wdlen %u, num_ch %u\n",
> > +		  sdr->fmt_idx, wdlen, formats[sdr->fmt_idx].num_ch);
> > +
> > +	/* Sanity check */
> > +	if (formats[sdr->fmt_idx].num_ch > sdr->num_cur_ch) {
> > +		rdrif_err(sdr, "fmt idx %u current ch %u mismatch\n",
> > +			  sdr->fmt_idx, sdr->num_cur_ch);
> > +		return ret;
> 
> This should never happen, it should be caught at set format time.

But, this is the set format function?

> 
> > +	}
> > +
> > +	/* Get bitlen & wdcnt from wdlen */
> > +	ret = rcar_drif_convert_wdlen(sdr, wdlen, &bitlen, &wdcnt);
> > +	if (ret)
> > +		return ret;
> > +
> > +	/* Setup group, bitlen & wdcnt */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		u32 mdr;
> > +
> > +		/* Two groups */
> > +		mdr = RCAR_DRIF_MDR_GRPCNT(2) | RCAR_DRIF_MDR_BITLEN(bitlen) |
> > +		       RCAR_DRIF_MDR_WDCNT(wdcnt);
> > +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR2);
> > +
> > +		mdr = RCAR_DRIF_MDR_BITLEN(bitlen) |
> RCAR_DRIF_MDR_WDCNT(wdcnt);
> > +		writel(mdr, sdr->ch[i]->base + RCAR_DRIF_SIRMDR3);
> > +
> > +		rdrif_dbg(2, sdr, "ch%u: new mdr[2,3] = 0x%08x, 0x%08x\n",
> > +			  i, readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR2),
> > +			  readl(sdr->ch[i]->base + RCAR_DRIF_SIRMDR3));
> > +	}
> > +	return ret;
> > +}
> > +
> > +/* Release DMA buffers */
> > +static void rcar_drif_release_buf(struct rcar_drif_sdr *sdr) {
> > +	unsigned int i;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		struct rcar_drif *ch = sdr->ch[i];
> > +
> > +		/* First entry contains the dma buf ptr */
> > +		if (ch->buf[0] && ch->buf[0]->addr) {
> > +			dma_free_coherent(&ch->pdev->dev,
> > +					  sdr->hwbuf_size * num_hwbufs,
> > +					  ch->buf[0]->addr, ch->dma_handle);
> > +			ch->buf[0]->addr = NULL;
> > +		}
> > +	}
> > +}
> > +
> > +/* Request DMA buffers */
> > +static int rcar_drif_request_buf(struct rcar_drif_sdr *sdr) {
> > +	int ret = -ENOMEM;
> > +	unsigned int i, j;
> > +	void *addr;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		struct rcar_drif *ch = sdr->ch[i];
> > +
> > +		/* Allocate DMA buffers */
> > +		addr = dma_alloc_coherent(&ch->pdev->dev,
> > +					  sdr->hwbuf_size * num_hwbufs,
> > +					  &ch->dma_handle, GFP_KERNEL);
> > +		if (!addr) {
> > +			rdrif_err(sdr,
> > +			"ch%u: dma alloc failed. num_hwbufs %u size %u\n",
> > +			i, num_hwbufs, sdr->hwbuf_size);
> > +			goto alloc_error;
> > +		}
> > +
> > +		/* Split the chunk and populate bufctxt */
> > +		for (j = 0; j < num_hwbufs; j++) {
> > +			ch->buf[j]->addr = addr + (j * sdr->hwbuf_size);
> > +			ch->buf[j]->status = 0;
> > +		}
> > +	}
> > +
> > +	return 0;
> > +
> > +alloc_error:
> > +	return ret;
> > +}
> > +
> > +/* Setup vb_queue minimum buffer requirements */ static int
> > +rcar_drif_queue_setup(struct vb2_queue *vq,
> > +			unsigned int *num_buffers, unsigned int *num_planes,
> > +			unsigned int sizes[], struct device *alloc_devs[]) {
> > +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> > +
> > +	/* Need at least 16 buffers */
> > +	if (vq->num_buffers + *num_buffers < 16)
> > +		*num_buffers = 16 - vq->num_buffers;
> > +
> > +	*num_planes = 1;
> > +	sizes[0] = PAGE_ALIGN(formats[sdr->fmt_idx].buffersize);
> > +
> > +	rdrif_dbg(2, sdr, "num_bufs %d sizes[0] %d\n", *num_buffers,
> sizes[0]);
> > +	return 0;
> > +}
> > +
> > +/* Enqueue buffer */
> > +static void rcar_drif_buf_queue(struct vb2_buffer *vb) {
> > +	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
> > +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct rcar_drif_frame_buf *fbuf =
> > +			container_of(vbuf, struct rcar_drif_frame_buf, vb);
> > +	unsigned long flags;
> > +
> > +	rdrif_dbg(2, sdr, "buf_queue idx %u\n", vb->index);
> > +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> > +	list_add_tail(&fbuf->list, &sdr->queued_bufs);
> > +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags); }
> > +
> > +/* Get a frame buf from list */
> > +static struct rcar_drif_frame_buf *
> > +rcar_drif_get_fbuf(struct rcar_drif_sdr *sdr) {
> > +	struct rcar_drif_frame_buf *fbuf;
> > +	unsigned long flags;
> > +
> > +	spin_lock_irqsave(&sdr->queued_bufs_lock, flags);
> > +	fbuf = list_first_entry_or_null(&sdr->queued_bufs, struct
> > +					rcar_drif_frame_buf, list);
> > +	if (!fbuf) {
> > +		/*
> > +		 * App is late in enqueing buffers. Samples lost & there will
> > +		 * be a gap in sequence number when app recovers
> > +		 */
> > +		rdrif_dbg(1, sdr, "\napp late: prod %u\n", sdr->produced);
> > +		sdr->produced++; /* Increment the produced count anyway */
> > +		spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> > +		return NULL;
> > +	}
> > +	list_del(&fbuf->list);
> > +	spin_unlock_irqrestore(&sdr->queued_bufs_lock, flags);
> > +
> > +	return fbuf;
> > +}
> > +
> > +static inline bool rcar_drif_buf_pairs_done(struct rcar_drif_hwbuf
> *buf1,
> > +					    struct rcar_drif_hwbuf *buf2) {
> > +	return (buf1->status & buf2->status & RCAR_DRIF_BUF_DONE); }
> > +
> > +/* Channel DMA complete */
> > +static void rcar_drif_channel_complete(struct rcar_drif *ch, u32 idx)
> > +{
> > +	u32 str;
> > +
> > +	ch->buf[idx]->status |= RCAR_DRIF_BUF_DONE;
> > +
> > +	/* Check for DRIF errors */
> > +	str = readl(ch->base + RCAR_DRIF_SISTR);
> > +	if (unlikely(str & RCAR_DRIF_RFOVF)) {
> > +		/* Writing the same clears it */
> > +		writel(str, ch->base + RCAR_DRIF_SISTR);
> > +
> > +		/* Overflow: some samples are lost */
> > +		ch->buf[idx]->status |= RCAR_DRIF_BUF_OVERFLOW;
> > +	}
> > +}
> > +
> > +/* Deliver buffer to user */
> > +static void rcar_drif_deliver_buf(struct rcar_drif *ch) {
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +	u32 idx = sdr->produced % num_hwbufs;
> > +	struct rcar_drif_frame_buf *fbuf;
> > +	bool overflow = false;
> > +
> > +	rcar_drif_channel_complete(ch, idx);
> > +
> > +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL) {
> > +		struct rcar_drif_hwbuf *bufi, *bufq;
> > +
> > +		if (ch->num) {
> > +			bufi = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> > +			bufq = ch->buf[idx];
> > +		} else {
> > +			bufi = ch->buf[idx];
> > +			bufq = to_rcar_drif_buf_pair(sdr, ch->num, idx);
> > +		}
> > +
> > +		/* Check if both DMA buffers are done */
> > +		if (!rcar_drif_buf_pairs_done(bufi, bufq))
> > +			return;
> > +
> > +		/* Clear buf done status */
> > +		bufi->status &= ~RCAR_DRIF_BUF_DONE;
> > +		bufq->status &= ~RCAR_DRIF_BUF_DONE;
> > +
> > +		/* Get fbuf */
> > +		fbuf = rcar_drif_get_fbuf(sdr);
> > +		if (!fbuf)
> > +			return;
> > +
> > +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> > +		       bufi->addr, sdr->hwbuf_size);
> > +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0) + sdr-
> >hwbuf_size,
> > +		       bufq->addr, sdr->hwbuf_size);
> 
> Ouch ! That's a high data rate memcpy that can be avoided. Why don't you
> DMA directly to the vb2 buffers ? You will need to use videobuf2-dma-
> contig instead of videobuf2-vmalloc, but apart from that there should be
> no issue.

Yes. We thought about this issue and considered this approach as a trade-off to avoid DRIF overflow issue. Overflow happens DMAC fails to consume from DRIF FIFO on time (i.e.) when user app is busy processing samples and fails to queue buffers to DMAC. After an overflow, the device needs to reset (streaming stop/restart sequence). With cyclic DMA, we de-coupled the h/w and user buffers to avoid a costly device reset when user app is busy or not scheduled. Some samples will be lost but that is identified with the sequence numbers and the action/policy can be left to the user. Are you OK with this please?

> 
> > +		if ((bufi->status | bufq->status) & RCAR_DRIF_BUF_OVERFLOW) {
> > +			overflow = true;
> > +			/* Clear the flag in status */
> > +			bufi->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> > +			bufq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> > +		}
> > +	} else {
> > +		struct rcar_drif_hwbuf *bufiq;
> > +
> > +		/* Get fbuf */
> > +		fbuf = rcar_drif_get_fbuf(sdr);
> > +		if (!fbuf)
> > +			return;
> > +
> > +		bufiq = ch->buf[idx];
> > +
> > +		memcpy(vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0),
> > +		       bufiq->addr, sdr->hwbuf_size);
> > +
> > +		if (bufiq->status & RCAR_DRIF_BUF_OVERFLOW) {
> > +			overflow = true;
> > +			/* Clear the flag in status */
> > +			bufiq->status &= ~RCAR_DRIF_BUF_OVERFLOW;
> > +		}
> > +	}
> > +
> > +	rdrif_dbg(2, sdr, "ch%u: prod %u\n", ch->num, sdr->produced);
> > +
> > +	fbuf->vb.field = V4L2_FIELD_NONE;
> > +	fbuf->vb.sequence = sdr->produced++;
> > +	fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
> > +	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
> > +			      formats[sdr->fmt_idx].buffersize);
> > +
> > +	/* Set error state on overflow */
> > +	if (overflow)
> > +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> > +	else
> > +		vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
> 
> Maybe
> 
> 	vb2_buffer_done(&fbuf->vb.vb2_buf,
> 			overflow ? VB2_BUF_STATE_ERROR: VB2_BUF_STATE_DONE);

Agreed

> 
> > +}
> > +
> > +/* DMA callback for each stage */
> > +static void rcar_drif_dma_complete(void *dma_async_param) {
> > +	struct rcar_drif *ch = dma_async_param;
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +
> > +	mutex_lock(&sdr->vb_queue_mutex);
> 
> Isn't the complete callback potentially called in interrupt context ? I
> know the rcar-dmac driver uses a threaded interrupt handler for that, but
> is that a guarantee of the DMA engine API ?
> 

DMA engine API mentions the callback will be called in tasklet context. Hmm... I could convert that into spin_lock_irq() if that's OK.

> > +
> > +	/* DMA can be terminated while the callback was waiting on lock */
> > +	if (!vb2_is_streaming(&sdr->vb_queue))
> 
> Can it ? The streaming flag is cleared after the stop_streaming operation
> is called, which will terminate all DMA transfers synchronously.

rcar-dmac did not have device_synchronize support, when I started with this patch set. There is a comment in the terminal_all call

1224         /*      
1225          * FIXME: No new interrupt can occur now, but the IRQ thread might still
1226          * be running.
1227          */

Hence, I was a bit paranoid. 

> 
> > +		goto stopped;
> > +
> > +	rcar_drif_deliver_buf(ch);
> > +stopped:
> > +	mutex_unlock(&sdr->vb_queue_mutex);
> > +}
> > +
> > +static int rcar_drif_qbuf(struct rcar_drif *ch) {
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +	dma_addr_t addr = ch->dma_handle;
> > +	struct dma_async_tx_descriptor *rxd;
> > +	dma_cookie_t cookie;
> > +	int ret = -EIO;
> > +
> > +	/* Setup cyclic DMA with given buffers */
> > +	rxd = dmaengine_prep_dma_cyclic(ch->dmach, addr,
> > +					sdr->hwbuf_size * num_hwbufs,
> > +					sdr->hwbuf_size, DMA_DEV_TO_MEM,
> > +					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> > +	if (!rxd) {
> > +		rdrif_err(sdr, "ch%u: prep dma cyclic failed\n", ch->num);
> > +		return ret;
> > +	}
> > +
> > +	/* Submit descriptor */
> > +	rxd->callback = rcar_drif_dma_complete;
> > +	rxd->callback_param = ch;
> > +	cookie = dmaengine_submit(rxd);
> > +	if (dma_submit_error(cookie)) {
> > +		rdrif_err(sdr, "ch%u: dma submit failed\n", ch->num);
> > +		return ret;
> > +	}
> > +
> > +	dma_async_issue_pending(ch->dmach);
> > +	return 0;
> > +}
> > +
> > +/* Enable reception */
> > +static int rcar_drif_enable_rx(struct rcar_drif_sdr *sdr) {
> > +	unsigned int i;
> > +	u32 ctr;
> > +	int ret;
> > +
> > +	/*
> > +	 * When both internal channels are enabled, they can be synchronized
> > +	 * only by the master
> > +	 */
> > +
> > +	/* Enable receive */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> > +		ctr |= (RCAR_DRIF_SICTR_RX_RISING_EDGE |
> > +			 RCAR_DRIF_SICTR_RX_EN);
> > +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> > +	}
> > +
> > +	/* Check receive enabled */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> > +					 ctr, ctr & RCAR_DRIF_SICTR_RX_EN,
> > +					 2, 500000);
> 
> A 2µs sleep for a 500ms total timeout seems very low to me, that will
> stress the CPU. Same comment for the other locations where you use
> readl_poll_timeout.
> 
> How long does the channel typically take to get enabled ?

It takes ~6-7µs on my board. The manual did not specify the expected time and I used a worst case of 500ms as this is used in the on/off path only.

Would 7µs sleep time be acceptable instead of 2µs? 

> 
> > +		if (ret) {
> > +			rdrif_err(sdr, "ch%u: rx en failed. ctr 0x%08x\n",
> > +				  i, readl(sdr->ch[i]->base +
> RCAR_DRIF_SICTR));
> > +			break;
> > +		}
> > +	}
> > +	return ret;
> > +}
> > +
> > +/* Disable reception */
> > +static void rcar_drif_disable_rx(struct rcar_drif_sdr *sdr) {
> > +	unsigned int i;
> > +	u32 ctr;
> > +	int ret;
> > +
> > +	/* Disable receive */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ctr = readl(sdr->ch[i]->base + RCAR_DRIF_SICTR);
> > +		ctr &= ~RCAR_DRIF_SICTR_RX_EN;
> > +		writel(ctr, sdr->ch[i]->base + RCAR_DRIF_SICTR);
> > +	}
> > +
> > +	/* Check receive disabled */
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ret = readl_poll_timeout(sdr->ch[i]->base + RCAR_DRIF_SICTR,
> > +					 ctr, !(ctr & RCAR_DRIF_SICTR_RX_EN),
> > +					 2, 500000);
> 
> How long does the channel typically take to get disabled ?

Same as above comment.

> 
> > +		if (ret)
> > +			dev_warn(&sdr->vdev->dev,
> > +			"ch%u: failed to disable rx. ctr 0x%08x\n",
> > +			i, readl(sdr->ch[i]->base + RCAR_DRIF_SICTR));
> > +	}
> > +}
> > +
> > +/* Start channel */
> > +static int rcar_drif_start_channel(struct rcar_drif *ch) {
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +	u32 ctr, str;
> > +	int ret;
> > +
> > +	/* Reset receive */
> > +	writel(RCAR_DRIF_SICTR_RESET, ch->base + RCAR_DRIF_SICTR);
> > +	ret = readl_poll_timeout(ch->base + RCAR_DRIF_SICTR,
> > +					 ctr, !(ctr & RCAR_DRIF_SICTR_RESET),
> 
> The alignment is weird.

If I remember correctly, it is a checkpatch warning when I started with the patch set (Last year Oct timeframe).

> 
> > +					 2, 500000);
> > +	if (ret) {
> > +		rdrif_err(sdr, "ch%u: failed to reset rx. ctr 0x%08x\n",
> > +			  ch->num, readl(ch->base + RCAR_DRIF_SICTR));
> > +		return ret;
> > +	}
> > +
> > +	/* Queue buffers for DMA */
> > +	ret = rcar_drif_qbuf(ch);
> > +	if (ret)
> > +		return ret;
> > +
> > +	/* Clear status register flags */
> > +	str = RCAR_DRIF_RFFUL | RCAR_DRIF_REOF | RCAR_DRIF_RFSERR |
> > +		RCAR_DRIF_RFUDF | RCAR_DRIF_RFOVF;
> > +	writel(str, ch->base + RCAR_DRIF_SISTR);
> > +
> > +	/* Enable DMA receive interrupt */
> > +	writel(0x00009000, ch->base + RCAR_DRIF_SIIER);
> > +
> > +	return ret;
> > +}
> > +
> > +/* Start receive operation */
> > +static int rcar_drif_start(struct rcar_drif_sdr *sdr) {
> > +	unsigned int i;
> > +	int ret;
> > +
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ret = rcar_drif_start_channel(sdr->ch[i]);
> > +		if (ret)
> > +			goto start_error;
> > +	}
> > +
> > +	sdr->produced = 0;
> > +	ret = rcar_drif_enable_rx(sdr);
> > +start_error:
> 
> Don't you need to stop the channels that were successfully started if an
> error occurs ?

Thank you. I missed this :-(.

> 
> > +	return ret;
> > +}
> > +
> > +/* Stop channel */
> > +static void rcar_drif_stop_channel(struct rcar_drif *ch) {
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +	int ret, retries = 3;
> > +
> > +	/* Disable DMA receive interrupt */
> > +	writel(0x00000000, ch->base + RCAR_DRIF_SIIER);
> > +
> > +	do {
> > +		/* Terminate all DMA transfers */
> > +		ret = dmaengine_terminate_sync(ch->dmach);
> > +		if (!ret)
> > +			break;
> > +		rdrif_dbg(2, sdr, "stop retry\n");
> > +	} while (--retries);
> 
> Why do you need to retry the terminate operation, why does it fail ?

Yes, I think it can be removed. I cannot remember why I added retry here because rcar-dmac terminate_all always seem to return 0. Last year, there used to be a WARN_ON in rcar-dmac code when channel halt fails (i.e.) it is not guaranteed that all transfers are stopped even after calling dmaengine_terminate_sync(). With the character version of this driver, I used to have a test app to start/stream/stop/restart sequence & hit the WARN_ON in dmac code sometimes (https://www.spinics.net/lists/linux-renesas-soc/msg04840.html). I may have added this return value check assuming it is passed on but I cannot see that even in the old code.

So yes, I agree with your comment.

> 
> > +	WARN_ON(!retries);
> > +}
> 
> [snip]
> 
> > +/* Start streaming */
> > +static int rcar_drif_start_streaming(struct vb2_queue *vq, unsigned
> > +int
> > count)
> > +{
> > +	struct rcar_drif_sdr *sdr = vb2_get_drv_priv(vq);
> > +	unsigned int i, j;
> > +	int ret;
> > +
> > +	mutex_lock(&sdr->v4l2_mutex);
> 
> I'm surprised, aren't the start_streaming and stop_streaming operations
> called with the video device lock held already by the v4l2-ioctl layer ? I
> think they should be, if they're not there's probably a bug somewhere.

I did not see it. Please correct me if this is wrong

v4l_streamon
 vb2_ioctl_streamon
  vb2_streamon
   vb2_core_streamon
    vb2_start_streaming
     q->ops->start_streaming => rcar_drif_start_streaming

     
> 
> > +	for_each_rcar_drif_channel(i, &sdr->cur_ch_mask) {
> > +		ret = clk_prepare_enable(sdr->ch[i]->clkp);
> > +		if (ret)
> > +			goto start_error;
> > +	}
> > +
> > +	/* Set default MDRx settings */
> > +	rcar_drif_set_mdr1(sdr);
> > +
> > +	/* Set new format */
> > +	ret = rcar_drif_set_format(sdr);
> > +	if (ret)
> > +		goto start_error;
> > +
> > +	if (sdr->num_cur_ch == RCAR_DRIF_MAX_CHANNEL)
> > +		sdr->hwbuf_size =
> > +		formats[sdr->fmt_idx].buffersize / RCAR_DRIF_MAX_CHANNEL;
> > +	else
> > +		sdr->hwbuf_size = formats[sdr->fmt_idx].buffersize;
> > +
> > +	rdrif_dbg(1, sdr, "num_hwbufs %u, hwbuf_size %u\n",
> > +		num_hwbufs, sdr->hwbuf_size);
> > +
> > +	/* Alloc DMA channel */
> > +	ret = rcar_drif_alloc_dmachannel(sdr);
> > +	if (ret)
> > +		goto start_error;
> > +
> > +	/* Alloc buf context */
> > +	ret = rcar_drif_alloc_bufctxt(sdr);
> > +	if (ret)
> > +		goto start_error;
> > +
> > +	/* Request buffers */
> > +	ret = rcar_drif_request_buf(sdr);
> > +	if (ret)
> > +		goto start_error;
> > +
> > +	/* Start Rx */
> > +	ret = rcar_drif_start(sdr);
> > +	if (ret)
> > +		goto start_error;
> > +
> > +	mutex_unlock(&sdr->v4l2_mutex);
> > +	rdrif_dbg(1, sdr, "started\n");
> > +	return ret;
> > +
> > +start_error:
> 
> As there's a single error label I would call this "error". Up to you.

Agreed.

> 
> > +	rcar_drif_release_queued_bufs(sdr, VB2_BUF_STATE_QUEUED);
> > +	rcar_drif_release_buf(sdr);
> > +	rcar_drif_release_bufctxt(sdr);
> > +	rcar_drif_release_dmachannel(sdr);
> > +	for (j = 0; j < i; j++)
> > +		clk_disable_unprepare(sdr->ch[j]->clkp);
> > +
> > +	mutex_unlock(&sdr->v4l2_mutex);
> > +	return ret;
> > +}
> 
> [snip]
> 
> > +/* Vb2 ops */
> > +static struct vb2_ops rcar_drif_vb2_ops = {
> 
> You can make this static const.

Agreed

> 
> > +	.queue_setup            = rcar_drif_queue_setup,
> > +	.buf_queue              = rcar_drif_buf_queue,
> > +	.start_streaming        = rcar_drif_start_streaming,
> > +	.stop_streaming         = rcar_drif_stop_streaming,
> > +	.wait_prepare		= vb2_ops_wait_prepare,
> > +	.wait_finish		= vb2_ops_wait_finish,
> > +};
> 
> [snip]
> 
> > +static int rcar_drif_g_fmt_sdr_cap(struct file *file, void *priv,
> > +				   struct v4l2_format *f)
> > +{
> > +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> > +
> > +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> > +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> > +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> 
> I believe the core ioctl handling code already does this for you. Same for
> the other ioctl handlers in

Agreed

> 
> > +	return 0;
> > +}
> > +
> > +static int rcar_drif_s_fmt_sdr_cap(struct file *file, void *priv,
> > +				   struct v4l2_format *f)
> > +{
> > +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> > +	struct vb2_queue *q = &sdr->vb_queue;
> > +	unsigned int i;
> > +
> > +	if (vb2_is_busy(q))
> > +		return -EBUSY;
> > +
> > +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> > +	for (i = 0; i < NUM_FORMATS; i++) {
> > +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
> 
> The code would become more readable (at least in my opinion) if you just
> added a break here, and moved the code below after the loop. In case the
> requested format isn't found (i == NUM_FORMATS) you can then set i to 0
> and proceed, that will select the first available format as a default.

Agreed

> 
> > +			sdr->fmt_idx  = i;
> > +			f->fmt.sdr.buffersize = formats[i].buffersize;
> > +
> > +			/*
> > +			 * If a format demands one channel only out of two
> > +			 * enabled channels, pick the 0th channel.
> > +			 */
> > +			if (formats[i].num_ch < sdr->num_hw_ch) {
> > +				sdr->cur_ch_mask = BIT(0);
> > +				sdr->num_cur_ch = formats[i].num_ch;
> > +			} else {
> > +				sdr->cur_ch_mask = sdr->hw_ch_mask;
> > +				sdr->num_cur_ch = sdr->num_hw_ch;
> > +			}
> > +
> > +			rdrif_dbg(1, sdr, "cur: idx %u mask %lu num %u\n",
> > +				  i, sdr->cur_ch_mask, sdr->num_cur_ch);
> > +			return 0;
> > +		}
> > +	}
> > +
> > +	if (rcar_drif_set_default_format(sdr)) {
> > +		rdrif_err(sdr, "cannot set default format\n");
> > +		return -EINVAL;
> > +	}
> > +
> > +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> > +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> > +	return 0;
> > +}
> > +
> > +static int rcar_drif_try_fmt_sdr_cap(struct file *file, void *priv,
> > +				     struct v4l2_format *f)
> > +{
> > +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> > +	unsigned int i;
> > +
> > +	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
> > +	for (i = 0; i < NUM_FORMATS; i++) {
> > +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
> > +			f->fmt.sdr.buffersize = formats[i].buffersize;
> > +			return 0;
> > +		}
> > +	}
> > +
> > +	f->fmt.sdr.pixelformat = formats[sdr->fmt_idx].pixelformat;
> > +	f->fmt.sdr.buffersize = formats[sdr->fmt_idx].buffersize;
> 
> The result of the TRY_FMT ioctl should not depend on the currently
> configured format. I would return a fixed format (for instance the first
> one in the formats array) in the default case.

Agreed

> 
> > +	return 0;
> > +}
> > +
> > +/* Tuner subdev ioctls */
> > +static int rcar_drif_enum_freq_bands(struct file *file, void *priv,
> > +				     struct v4l2_frequency_band *band) {
> > +	struct rcar_drif_sdr *sdr = video_drvdata(file);
> > +	struct v4l2_subdev *sd;
> > +	int ret = 0;
> > +
> > +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> > +		ret = v4l2_subdev_call(sd, tuner, enum_freq_bands, band);
> 
> This won't work as-is when you'll have multiple subdevs. As the driver
> only supports a single connected subdev at the moment, I suggest storing a
> pointer to that subdev in the rcar_drif_sdr structure, and calling
> operations on that subdev explicitly instead of looping over all subdevs.
> The comment holds for all other ioctls.

Agreed.

> 
> > +		if (ret)
> > +			break;
> > +	}
> > +	return ret;
> > +}
> 
> [snip]
> 
> > +static int rcar_drif_notify_bound(struct v4l2_async_notifier *notifier,
> > +				   struct v4l2_subdev *subdev,
> > +				   struct v4l2_async_subdev *asd) {
> > +	struct rcar_drif_sdr *sdr =
> > +		container_of(notifier, struct rcar_drif_sdr, notifier);
> > +
> > +	/* Nothing to do at this point */
> 
> If there's nothing to do you can just leave the bound callback
> unimplemented, it's optional.

Agreed.

> 
> > +	rdrif_dbg(2, sdr, "bound asd: %s\n", asd->match.of.node->name);
> > +	return 0;
> > +}
> > +
> > +/* Sub-device registered notification callback */ static int
> > +rcar_drif_notify_complete(struct v4l2_async_notifier *notifier) {
> > +	struct rcar_drif_sdr *sdr =
> > +		container_of(notifier, struct rcar_drif_sdr, notifier);
> > +	struct v4l2_subdev *sd;
> > +	int ret;
> > +
> > +	sdr->v4l2_dev.ctrl_handler = &sdr->ctrl_hdl;
> > +
> > +	ret = v4l2_device_register_subdev_nodes(&sdr->v4l2_dev);
> > +	if (ret) {
> > +		rdrif_err(sdr, "failed register subdev nodes ret %d\n", ret);
> > +		return ret;
> > +	}
> 
> Do you need to expose subdev nodes to userspace ? Can't everything be
> handled from the V4L2 SDR node ?

As of today, everything can be handled from the V4L2 SDR node with current MAX2175 subdev. If the tuner driver is enhanced later, this would help.

> 
> > +	v4l2_device_for_each_subdev(sd, &sdr->v4l2_dev) {
> > +		ret = v4l2_ctrl_add_handler(sdr->v4l2_dev.ctrl_handler,
> > +					    sd->ctrl_handler, NULL);
> 
> Shouldn't you undo this somewhere when unbinding the subdevs ?



> 
> > +		if (ret) {
> > +			rdrif_err(sdr, "failed ctrl add hdlr ret %d\n", ret);
> > +			return ret;
> > +		}
> > +	}
> > +	rdrif_dbg(2, sdr, "notify complete\n");
> > +	return 0;
> > +}
> 
> [snip]
> 
> > +/* Parse sub-devs (tuner) to find a matching device */ static int
> > +rcar_drif_parse_subdevs(struct rcar_drif_sdr *sdr,
> > +				   struct device *dev)
> > +{
> > +	struct v4l2_async_notifier *notifier = &sdr->notifier;
> > +	struct rcar_drif_async_subdev *rsd;
> > +	struct device_node *node;
> > +
> > +	notifier->subdevs = devm_kzalloc(dev, sizeof(*notifier->subdevs),
> > +					 GFP_KERNEL);
> > +	if (!notifier->subdevs)
> > +		return -ENOMEM;
> > +
> > +	node = of_graph_get_next_endpoint(dev->of_node, NULL);
> > +	if (!node)
> > +		return 0;
> > +
> > +	rsd = devm_kzalloc(dev, sizeof(*rsd), GFP_KERNEL);
> > +	if (!rsd) {
> > +		of_node_put(node);
> 
> If you move the allocation above of_graph_get_next_endpoint() you won't
> have to call of_node_put() in the error path.

Agreed

> 
> > +		return -ENOMEM;
> > +	}
> > +
> > +	notifier->subdevs[notifier->num_subdevs] = &rsd->asd;
> > +	rsd->asd.match.of.node = of_graph_get_remote_port_parent(node);
> 
> Aren't you missing an of_node_put() on the returned node ? Or does the
> async framework take care of that ?

You are right. of_node_put() on the returned node is missing. I will add it.

> 
> > +	of_node_put(node);
> > +	if (!rsd->asd.match.of.node) {
> > +		dev_warn(dev, "bad remote port parent\n");
> > +		return -EINVAL;
> > +	}
> > +
> > +	rsd->asd.match_type = V4L2_ASYNC_MATCH_OF;
> > +	notifier->num_subdevs++;
> > +
> > +	/* Get the endpoint properties */
> > +	rcar_drif_get_ep_properties(sdr, node);
> > +	return 0;
> > +}
> > +
> > +/* Check if the given device is the primary bond */ static bool
> > +rcar_drif_primary_bond(struct platform_device *pdev) {
> > +	if (of_find_property(pdev->dev.of_node, "renesas,primary-bond",
> NULL))
> > +		return true;
> > +
> > +	return false;
> 
> How about
> 
> 	return of_property_read_bool(pdev->dev.of_node,
> 				     "renesas,primary-bond");

Shall I remove this function itself and just use the property in the "if" condition please? It's used in one place only. I tend to not touch the bindings name/type as we have some sort of agreement after ~4months time :-)
 
> 
> > +}
> > +
> > +/* Get the bonded platform dev if enabled */ static struct
> > +platform_device *rcar_drif_enabled_bond(struct
> > platform_device *p)
> > +{
> > +	struct device_node *np;
> > +
> > +	np = of_parse_phandle(p->dev.of_node, "renesas,bonding", 0);
> 
> The function takes a reference to np, you need to call of_node_put() on it
> (only if the returned pointer isn't NULL).

Agreed

> 
> > +	if (np && of_device_is_available(np))
> > +		return of_find_device_by_node(np);
> 
> of_find_device_by_node() takes a reference to the returned device, you
> need to call device_put() on it when you don't need it anymore.

Agreed & Thanks again.


> 
> 
> > +	return NULL;
> > +}
> > +
> > +/* Proble internal channel */
> > +static int rcar_drif_channel_probe(struct platform_device *pdev)
> > +{
> > +	struct rcar_drif *ch;
> > +	struct resource	*res;
> > +	void __iomem *base;
> > +	struct clk *clkp;
> 
> Maybe s/clkp/clk/ ?

Agreed

> 
> > +	int ret;
> > +
> > +	/* Peripheral clock */
> > +	clkp = devm_clk_get(&pdev->dev, "fck");
> > +	if (IS_ERR(clkp)) {
> > +		ret = PTR_ERR(clkp);
> > +		dev_err(&pdev->dev, "clk get failed (%d)\n", ret);
> > +		return ret;
> > +	}
> 
> Isn't the clock managed automatically by runtime PM ?

I think the driver need to support runtime PM in order to manage the clock. Otherwise it just gets adds the clk to genpd (_prepare) without enable/disable. I need to double check this.

> 
> > +	/* Register map */
> > +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > +	base = devm_ioremap_resource(&pdev->dev, res);
> > +	if (IS_ERR(base)) {
> > +		ret = PTR_ERR(base);
> > +		dev_err(&pdev->dev, "ioremap failed (%d)\n", ret);
> > +		return ret;
> 
> devm_ioremap_resource() already prints an error message, you can remove
> this
> one.

Agreed

> 
> > +	}
> > +
> > +	/* Reserve memory for enabled channel */
> > +	ch = devm_kzalloc(&pdev->dev, sizeof(*ch), GFP_KERNEL);
> > +	if (!ch) {
> > +		ret = PTR_ERR(ch);
> > +		dev_err(&pdev->dev, "failed alloc channel\n");
> 
> Memory allocation failures already print error messages, you can remove
> this
> one.

Agreed.

> 
> > +		return ret;
> > +	}
> > +	ch->pdev = pdev;
> > +	ch->clkp = clkp;
> > +	ch->base = base;
> > +	ch->start = res->start;
> 
> If you allocated the ch structure first you could set the fields directly
> without a need for local variables.

Agreed.

> 
> > +	platform_set_drvdata(pdev, ch);
> > +	return 0;
> > +}
> > +
> > +static int rcar_drif_probe(struct platform_device *pdev)
> > +{
> > +	struct rcar_drif *ch, *b_ch = NULL;
> > +	struct platform_device *b_pdev;
> > +	struct rcar_drif_sdr *sdr;
> > +	int ret;
> > +
> > +	/* Probe internal channel */
> > +	ret = rcar_drif_channel_probe(pdev);
> > +	if (ret)
> > +		return ret;
> 
> I would have done it the other way around, inlining the
> rcar_drif_channel_probe() function here as that's the common case, and
> moving
> the V4L2 SDR device initialization code to a different function.

Agreed

> 
> > +	/* Check if both channels of the bond are enabled */
> > +	b_pdev = rcar_drif_enabled_bond(pdev);
> > +	if (b_pdev) {
> > +		/* Check if current channel acting as primary-bond */
> > +		if (!rcar_drif_primary_bond(pdev)) {
> > +			dev_notice(&pdev->dev, "probed\n");
> > +			return 0;
> > +		}
> > +
> > +		/* Check if the other device is probed */
> > +		b_ch = platform_get_drvdata(b_pdev);
> > +		if (!b_ch) {
> > +			dev_info(&pdev->dev, "defer probe\n");
> > +			return -EPROBE_DEFER;
> > +		}
> 
> Isn't this all very racy ? What if the other channel's device is removed
> while
> this one is probed ?

OK. Will holding the device_lock(&b_pdev->dev) is sufficient?
> 
> > +		/* Set the other channel number */
> > +		b_ch->num = 1;
> 
> Reading data from the other channel's private structure is one thing, but
> writing it makes me shiver :-S Could we make it so that 0 is the slave and
> 1
> the master ? That way you would set ch->num = 1 instead of b_ch->num = 1,
> keeping all modifications to the private structure local to the device
> being
> probed.

Agreed.

> 
> > +	}
> > +
> > +	/* Channel acting as SDR instance */
> > +	ch = platform_get_drvdata(pdev);
> > +	ch->acting_sdr = true;
> > +
> > +	/* Reserve memory for SDR structure */
> > +	sdr = devm_kzalloc(&pdev->dev, sizeof(*sdr), GFP_KERNEL);
> > +	if (!sdr) {
> > +		ret = PTR_ERR(sdr);
> > +		dev_err(&pdev->dev, "failed alloc drif context\n");
> > +		return ret;
> > +	}
> > +	sdr->dev = &pdev->dev;
> > +	sdr->hw_ch_mask = BIT(ch->num);
> > +
> > +	/* Establish links between SDR and channel(s) */
> > +	ch->sdr = sdr;
> > +	sdr->ch[ch->num] = ch;
> > +	if (b_ch) {
> > +		sdr->ch[b_ch->num] = b_ch;
> > +		b_ch->sdr = sdr;
> > +		sdr->hw_ch_mask |= BIT(b_ch->num);
> > +	}
> > +	sdr->num_hw_ch = hweight_long(sdr->hw_ch_mask);
> > +
> > +	/* Validate any supported format for enabled channels */
> > +	ret = rcar_drif_set_default_format(sdr);
> > +	if (ret) {
> > +		dev_err(sdr->dev, "failed to set default format\n");
> > +		return ret;
> > +	}
> > +
> > +	/* Set defaults */
> > +	sdr->hwbuf_size = RCAR_DRIF_DEFAULT_HWBUF_SIZE;
> > +
> > +	mutex_init(&sdr->v4l2_mutex);
> > +	mutex_init(&sdr->vb_queue_mutex);
> > +	spin_lock_init(&sdr->queued_bufs_lock);
> > +	INIT_LIST_HEAD(&sdr->queued_bufs);
> > +
> > +	/* Init videobuf2 queue structure */
> > +	sdr->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
> > +	sdr->vb_queue.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
> > +	sdr->vb_queue.drv_priv = sdr;
> > +	sdr->vb_queue.buf_struct_size = sizeof(struct rcar_drif_frame_buf);
> > +	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
> > +	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
> > +	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> > +
> > +	/* Init videobuf2 queue */
> > +	ret = vb2_queue_init(&sdr->vb_queue);
> > +	if (ret) {
> > +		dev_err(sdr->dev, "could not initialize vb2 queue\n");
> > +		return ret;
> > +	}
> > +
> > +	/* Register the v4l2_device */
> > +	ret = v4l2_device_register(&pdev->dev, &sdr->v4l2_dev);
> > +	if (ret) {
> > +		dev_err(sdr->dev, "failed v4l2_device_register (%d)\n", ret);
> 
> Maybe "failed to register V4L2 device" to make it a real sentence ? :-)

Agreed

> 
> > +		return ret;
> > +	}
> > +
> > +	/*
> > +	 * Parse subdevs after v4l2_device_register because if the subdev
> > +	 * is already probed, bound and complete will be called immediately
> > +	 */
> > +	ret = rcar_drif_parse_subdevs(sdr, &pdev->dev);
> > +	if (ret)
> > +		goto err_unreg_v4l2;
> > +
> > +	sdr->notifier.bound = rcar_drif_notify_bound;
> > +	sdr->notifier.complete = rcar_drif_notify_complete;
> > +
> > +	v4l2_ctrl_handler_init(&sdr->ctrl_hdl, 10);
> 
> Possibly a stupid question, why 10, if you don't create any control in
> this
> driver ?

To accommodate the subdev controls.

> 
> > +	/* Register notifier */
> > +	ret = v4l2_async_notifier_register(&sdr->v4l2_dev, &sdr->notifier);
> > +	if (ret < 0) {
> > +		dev_err(sdr->dev, "notifier registration failed (%d)\n", ret);
> > +		goto err_free_ctrls;
> > +	}
> > +
> > +	/* Init video_device structure */
> > +	sdr->vdev = video_device_alloc();
> > +	if (!sdr->vdev) {
> > +		ret = -ENOMEM;
> > +		goto err_unreg_notif;
> > +	}
> > +	snprintf(sdr->vdev->name, sizeof(sdr->vdev->name), "R-Car DRIF");
> > +	sdr->vdev->fops = &rcar_drif_fops;
> > +	sdr->vdev->ioctl_ops = &rcar_drif_ioctl_ops;
> > +	sdr->vdev->release = video_device_release;
> > +	sdr->vdev->lock = &sdr->v4l2_mutex;
> > +	sdr->vdev->queue = &sdr->vb_queue;
> > +	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
> > +	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
> > +	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
> > +	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
> > +		V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
> > +	video_set_drvdata(sdr->vdev, sdr);
> > +
> > +	/* Register V4L2 SDR device */
> > +	ret = video_register_device(sdr->vdev, VFL_TYPE_SDR, -1);
> > +	if (ret) {
> > +		dev_err(sdr->dev, "failed video_register_device (%d)\n", ret);
> 
> Same here, "failed to register video device" ?

Agreed.

> 
> > +		goto err_unreg_notif;
> > +	}
> > +
> > +	dev_notice(sdr->dev, "probed\n");
> 
> Do you think this message is really useful ? I believe it would just add a
> bit
> more noise to the kernel log, without any real use.

OK. Will remove it.

> 
> > +	return 0;
> > +
> > +err_unreg_notif:
> > +	video_device_release(sdr->vdev);
> > +	v4l2_async_notifier_unregister(&sdr->notifier);
> > +err_free_ctrls:
> > +	v4l2_ctrl_handler_free(&sdr->ctrl_hdl);
> > +err_unreg_v4l2:
> > +	v4l2_device_unregister(&sdr->v4l2_dev);
> > +	return ret;
> > +}
> > +
> > +static int rcar_drif_remove(struct platform_device *pdev)
> > +{
> > +	struct rcar_drif *ch = platform_get_drvdata(pdev);
> > +	struct rcar_drif_sdr *sdr = ch->sdr;
> > +
> > +	if (!ch->acting_sdr) {
> 
> Isn't it possible to check the channel number instead and remove the
> acting_sdr field ?

Agreed.

> 
> > +		/* Nothing to do */
> > +		dev_notice(&pdev->dev, "removed\n");
> > +		return 0;
> > +	}
> > +
> > +	/* SDR instance */
> > +	v4l2_ctrl_handler_free(sdr->vdev->ctrl_handler);
> > +	v4l2_async_notifier_unregister(&sdr->notifier);
> > +	v4l2_device_unregister(&sdr->v4l2_dev);
> > +	video_unregister_device(sdr->vdev);
> > +	dev_notice(&pdev->dev, "removed\n");
> 
> Even more than the probed message, I think this one can go away.

Agreed.

> 
> > +	return 0;
> > +}
> > +
> > +static int __maybe_unused rcar_drif_suspend(struct device *dev)
> > +{
> > +	return 0;
> 
> Maybe a /* FIXME: Implement suspend/resume support */ ?

Agreed.

> 
> > +}
> > +
> > +static int __maybe_unused rcar_drif_resume(struct device *dev)
> > +{
> > +	return 0;
> 
> Same here ?

Agreed.

Thanks,
Ramesh

^ permalink raw reply	[flat|nested] 31+ messages in thread

* RE: [PATCH v3 5/7] doc_rst: media: New SDR formats PC16, PC18 & PC20
  2017-04-11 12:39   ` Laurent Pinchart
@ 2017-04-18 17:13     ` Ramesh Shanmugasundaram
  0 siblings, 0 replies; 31+ messages in thread
From: Ramesh Shanmugasundaram @ 2017-04-18 17:13 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: robh+dt, mark.rutland, mchehab, hverkuil, sakari.ailus, crope,
	Chris Paterson, geert+renesas, linux-media, devicetree,
	linux-renesas-soc

Hi Laurent,

Thanks for the review comments.

> On Tuesday 07 Feb 2017 15:02:35 Ramesh Shanmugasundaram wrote:
> > This patch adds documentation for the three new SDR formats
> >
> > V4L2_SDR_FMT_PCU16BE
> > V4L2_SDR_FMT_PCU18BE
> > V4L2_SDR_FMT_PCU20BE
> >
> > Signed-off-by: Ramesh Shanmugasundaram
> > <ramesh.shanmugasundaram@bp.renesas.com> ---
> >  .../media/uapi/v4l/pixfmt-sdr-pcu16be.rst          | 55
> +++++++++++++++++++
> >  .../media/uapi/v4l/pixfmt-sdr-pcu18be.rst          | 55
> +++++++++++++++++++
> >  .../media/uapi/v4l/pixfmt-sdr-pcu20be.rst          | 54
> +++++++++++++++++++
> >  Documentation/media/uapi/v4l/sdr-formats.rst       |  3 ++
> >  4 files changed, 167 insertions(+)
> >  create mode 100644
> > Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
> >  create mode 100644
> > Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
> >  create mode 100644
> > Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> >
> > diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
> > b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst new file mode
> > 100644 index 0000000..2de1b1a
> > --- /dev/null
> > +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu16be.rst
> > @@ -0,0 +1,55 @@
> > +.. -*- coding: utf-8; mode: rst -*-
> > +
> > +.. _V4L2-SDR-FMT-PCU16BE:
> > +
> > +******************************
> > +V4L2_SDR_FMT_PCU16BE ('PC16')
> > +******************************
> > +
> > +Planar complex unsigned 16-bit big endian IQ sample
> > +
> > +Description
> > +===========
> > +
> > +This format contains a sequence of complex number samples. Each
> > +complex number consist of two parts called In-phase and Quadrature
> > +(IQ). Both I and Q are represented as a 16 bit unsigned big endian
> > +number stored in
> > +32 bit space. The remaining unused bits within the 32 bit space will
> > +be padded with 0. I value starts first and Q value starts at an
> > +offset equalling half of the buffer size (i.e.) offset =
> > +buffersize/2. Out of the 16 bits, bit 15:2 (14 bit) is data and bit
> > +1:0 (2 bit) can be any value.
> 
> This sounds very strange to me. Are the two lower bits always random ?
> What is that used for ?

It could be zeros or it could be status bits in case of MAX2175 (if enabled). I mentioned any value because the user app does not have any assumptions on these bits value.
 
> 
> > +**Byte Order.**
> > +Each cell is one byte.
> > +
> > +.. flat-table::
> > +    :header-rows:  1
> > +    :stub-columns: 0
> > +
> > +    * -  Offset:
> > +      -  Byte B0
> > +      -  Byte B1
> > +      -  Byte B2
> > +      -  Byte B3
> > +    * -  start + 0:
> > +      -  I'\ :sub:`0[13:6]`
> > +      -  I'\ :sub:`0[5:0]; B1[1:0]=pad`
> > +      -  pad
> > +      -  pad
> > +    * -  start + 4:
> > +      -  I'\ :sub:`1[13:6]`
> > +      -  I'\ :sub:`1[5:0]; B1[1:0]=pad`
> > +      -  pad
> > +      -  pad
> > +    * -  ...
> > +    * - start + offset:
> > +      -  Q'\ :sub:`0[13:6]`
> > +      -  Q'\ :sub:`0[5:0]; B1[1:0]=pad`
> > +      -  pad
> > +      -  pad
> > +    * - start + offset + 4:
> > +      -  Q'\ :sub:`1[13:6]`
> > +      -  Q'\ :sub:`1[5:0]; B1[1:0]=pad`
> > +      -  pad
> > +      -  pad
> > diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
> > b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst new file mode
> > 100644 index 0000000..da8b26b
> > --- /dev/null
> > +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu18be.rst
> > @@ -0,0 +1,55 @@
> > +.. -*- coding: utf-8; mode: rst -*-
> > +
> > +.. _V4L2-SDR-FMT-PCU18BE:
> > +
> > +******************************
> > +V4L2_SDR_FMT_PCU18BE ('PC18')
> > +******************************
> > +
> > +Planar complex unsigned 18-bit big endian IQ sample
> > +
> > +Description
> > +===========
> > +
> > +This format contains a sequence of complex number samples. Each
> > +complex number consist of two parts called In-phase and Quadrature
> > +(IQ). Both I and Q are represented as a 18 bit unsigned big endian
> > +number stored in
> > +32 bit space. The remaining unused bits within the 32 bit space will
> > +be padded with 0. I value starts first and Q value starts at an
> > +offset equalling half of the buffer size (i.e.) offset =
> > +buffersize/2. Out of the 18 bits, bit 17:2 (16 bit) is data and bit
> > +1:0 (2 bit) can be any value.
> > +
> > +**Byte Order.**
> > +Each cell is one byte.
> > +
> > +.. flat-table::
> > +    :header-rows:  1
> > +    :stub-columns: 0
> > +
> > +    * -  Offset:
> > +      -  Byte B0
> > +      -  Byte B1
> > +      -  Byte B2
> > +      -  Byte B3
> > +    * -  start + 0:
> > +      -  I'\ :sub:`0[17:10]`
> > +      -  I'\ :sub:`0[9:2]`
> > +      -  I'\ :sub:`0[1:0]; B2[5:0]=pad`
> > +      -  pad
> > +    * -  start + 4:
> > +      -  I'\ :sub:`1[17:10]`
> > +      -  I'\ :sub:`1[9:2]`
> > +      -  I'\ :sub:`1[1:0]; B2[5:0]=pad`
> > +      -  pad
> > +    * -  ...
> > +    * - start + offset:
> > +      -  Q'\ :sub:`0[17:10]`
> > +      -  Q'\ :sub:`0[9:2]`
> > +      -  Q'\ :sub:`0[1:0]; B2[5:0]=pad`
> > +      -  pad
> > +    * - start + offset + 4:
> > +      -  Q'\ :sub:`1[17:10]`
> > +      -  Q'\ :sub:`1[9:2]`
> > +      -  Q'\ :sub:`1[1:0]; B2[5:0]=pad`
> > +      -  pad
> > diff --git a/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> > b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst new file mode
> > 100644 index 0000000..5499eed
> > --- /dev/null
> > +++ b/Documentation/media/uapi/v4l/pixfmt-sdr-pcu20be.rst
> > @@ -0,0 +1,54 @@
> > +.. -*- coding: utf-8; mode: rst -*-
> > +.. _V4L2-SDR-FMT-PCU20BE:
> > +
> > +******************************
> > +V4L2_SDR_FMT_PCU20BE ('PC20')
> > +******************************
> > +
> > +Planar complex unsigned 20-bit big endian IQ sample
> > +
> > +Description
> > +===========
> > +
> > +This format contains a sequence of complex number samples. Each
> > +complex number consist of two parts called In-phase and Quadrature
> > +(IQ). Both I and Q are represented as a 20 bit unsigned big endian
> > +number stored in
> > +32 bit space. The remaining unused bits within the 32 bit space will
> > +be padded with 0. I value starts first and Q value starts at an
> > +offset equalling half of the buffer size (i.e.) offset =
> > +buffersize/2. Out of the 20 bits, bit 19:2 (18 bit) is data and bit
> > +1:0 (2 bit) can be any value.
> > +
> > +**Byte Order.**
> > +Each cell is one byte.
> > +
> > +.. flat-table::
> > +    :header-rows:  1
> > +    :stub-columns: 0
> > +
> > +    * -  Offset:
> > +      -  Byte B0
> > +      -  Byte B1
> > +      -  Byte B2
> > +      -  Byte B3
> > +    * -  start + 0:
> > +      -  I'\ :sub:`0[19:12]`
> > +      -  I'\ :sub:`0[11:4]`
> > +      -  I'\ :sub:`0[3:0]; B2[3:0]=pad`
> > +      -  pad
> > +    * -  start + 4:
> > +      -  I'\ :sub:`1[19:12]`
> > +      -  I'\ :sub:`1[11:4]`
> > +      -  I'\ :sub:`1[3:0]; B2[3:0]=pad`
> > +      -  pad
> > +    * -  ...
> > +    * - start + offset:
> > +      -  Q'\ :sub:`0[19:12]`
> > +      -  Q'\ :sub:`0[11:4]`
> > +      -  Q'\ :sub:`0[3:0]; B2[3:0]=pad`
> > +      -  pad
> > +    * - start + offset + 4:
> > +      -  Q'\ :sub:`1[19:12]`
> > +      -  Q'\ :sub:`1[11:4]`
> > +      -  Q'\ :sub:`1[3:0]; B2[3:0]=pad`
> > +      -  pad
> > diff --git a/Documentation/media/uapi/v4l/sdr-formats.rst
> > b/Documentation/media/uapi/v4l/sdr-formats.rst index f863c08..2037f5b
> > 100644
> > --- a/Documentation/media/uapi/v4l/sdr-formats.rst
> > +++ b/Documentation/media/uapi/v4l/sdr-formats.rst
> > @@ -17,3 +17,6 @@ These formats are used for :ref:`SDR <sdr>`
> > interface only. pixfmt-sdr-cs08
> >      pixfmt-sdr-cs14le
> >      pixfmt-sdr-ru12le
> > +    pixfmt-sdr-pcu16be
> > +    pixfmt-sdr-pcu18be
> > +    pixfmt-sdr-pcu20be


Thanks,
Ramesh

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2017-04-18 17:13 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-07 15:02 [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Ramesh Shanmugasundaram
     [not found] ` <1486479757-32128-1-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
2017-02-07 15:02   ` [PATCH v3 1/7] media: v4l2-ctrls: Reserve controls for MAX217X Ramesh Shanmugasundaram
2017-02-07 15:02     ` Ramesh Shanmugasundaram
2017-04-11  8:28     ` Laurent Pinchart
2017-02-07 15:02 ` [PATCH v3 2/7] dt-bindings: media: Add MAX2175 binding description Ramesh Shanmugasundaram
2017-02-15 16:59   ` Rob Herring
2017-04-11  9:42   ` Laurent Pinchart
2017-04-11  9:57     ` Ramesh Shanmugasundaram
     [not found]       ` <HK2PR06MB0545282102FBC0472D9831AEC3000-jAC1QCBx2F1Af0bSEaWBKm0DtJ1/0DrXvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2017-04-11 11:27         ` Laurent Pinchart
2017-04-11 11:27           ` Laurent Pinchart
2017-04-11 12:19           ` Ramesh Shanmugasundaram
     [not found]             ` <HK2PR06MB0545F9FBB1B27E91D80F906EC3000-jAC1QCBx2F1Af0bSEaWBKm0DtJ1/0DrXvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2017-04-11 12:58               ` Laurent Pinchart
2017-04-11 12:58                 ` Laurent Pinchart
2017-02-07 15:02 ` [PATCH v3 3/7] media: i2c: max2175: Add MAX2175 support Ramesh Shanmugasundaram
2017-02-13 12:40   ` Hans Verkuil
2017-02-07 15:02 ` [PATCH v3 4/7] media: Add new SDR formats PC16, PC18 & PC20 Ramesh Shanmugasundaram
2017-02-07 15:02 ` [PATCH v3 5/7] doc_rst: media: New " Ramesh Shanmugasundaram
2017-04-11 12:39   ` Laurent Pinchart
2017-04-18 17:13     ` Ramesh Shanmugasundaram
2017-02-07 15:02 ` [PATCH v3 6/7] dt-bindings: media: Add Renesas R-Car DRIF binding Ramesh Shanmugasundaram
     [not found]   ` <1486479757-32128-7-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
2017-02-15 17:09     ` Rob Herring
2017-02-15 17:09       ` Rob Herring
2017-02-16 11:02       ` Ramesh Shanmugasundaram
2017-04-11 22:41         ` Laurent Pinchart
2017-04-11 22:35   ` Laurent Pinchart
2017-02-07 15:02 ` [PATCH v3 7/7] media: platform: rcar_drif: Add DRIF support Ramesh Shanmugasundaram
     [not found]   ` <1486479757-32128-8-git-send-email-ramesh.shanmugasundaram-kTT6dE0pTRh9uiUsa/gSgQ@public.gmane.org>
2017-04-11 22:24     ` Laurent Pinchart
2017-04-11 22:24       ` Laurent Pinchart
2017-04-18 17:12       ` Ramesh Shanmugasundaram
2017-02-13 12:46 ` [PATCH v3 0/7] Add V4L2 SDR (DRIF & MAX2175) driver Hans Verkuil
2017-02-14 13:42   ` Laurent Pinchart

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.