All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] DRM driver for Faraday TVE200
@ 2017-08-13 15:11 ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: linux-arm-kernel

I finally got my act together to clean up my patches and post
this DRM driver.

It's an old hardware, but using all the new fancy CMA helpers
and integrating really nicely I think. Most important: it
JustWorks(TM) and i have a little penguin and interactive
console on my D-Link DIR-685 router (!). Well that is not
a serious usecase, but it kind of proves that it does all it
should. Vblank interrupts work like a charm and all.

It would not have been possible if I hadn't had Eric Anholts
excellent PL11x driver to use as role model.

Daniel, Eric: please help me out with the next steps in your
maintenance model, so I learn how to do this upstream work
right with the DRM community.

I posted the display driver separately since it is, well
a separate business. I guess Thierry collects these drivers,
else tell me.

Linus Walleij (4):
  drm/tve200: Add DT bindings
  drm/tve200: Add new driver for TVE200
  ARM: dts: Add TVE200 to the Gemini SoC DTSI
  ARM: dts: Add TVE/TVC and ILI9322 panel to DIR-685

 .../devicetree/bindings/display/faraday,tve200.txt |  41 +++
 Documentation/gpu/index.rst                        |   1 +
 Documentation/gpu/tve200.rst                       |   6 +
 MAINTAINERS                                        |   6 +
 arch/arm/boot/dts/gemini-dlink-dir-685.dts         |  77 ++++-
 arch/arm/boot/dts/gemini.dtsi                      |  19 ++
 drivers/gpu/drm/Kconfig                            |   2 +
 drivers/gpu/drm/Makefile                           |   1 +
 drivers/gpu/drm/tve200/Kconfig                     |  15 +
 drivers/gpu/drm/tve200/Makefile                    |   5 +
 drivers/gpu/drm/tve200/tve200_connector.c          | 126 ++++++++
 drivers/gpu/drm/tve200/tve200_display.c            | 346 +++++++++++++++++++++
 drivers/gpu/drm/tve200/tve200_drm.h                | 129 ++++++++
 drivers/gpu/drm/tve200/tve200_drv.c                | 277 +++++++++++++++++
 14 files changed, 1050 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt
 create mode 100644 Documentation/gpu/tve200.rst
 create mode 100644 drivers/gpu/drm/tve200/Kconfig
 create mode 100644 drivers/gpu/drm/tve200/Makefile
 create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
 create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c

-- 
2.13.4

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

* [PATCH 0/4] DRM driver for Faraday TVE200
@ 2017-08-13 15:11 ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: dri-devel, Eric Anholt, Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel

I finally got my act together to clean up my patches and post
this DRM driver.

It's an old hardware, but using all the new fancy CMA helpers
and integrating really nicely I think. Most important: it
JustWorks(TM) and i have a little penguin and interactive
console on my D-Link DIR-685 router (!). Well that is not
a serious usecase, but it kind of proves that it does all it
should. Vblank interrupts work like a charm and all.

It would not have been possible if I hadn't had Eric Anholts
excellent PL11x driver to use as role model.

Daniel, Eric: please help me out with the next steps in your
maintenance model, so I learn how to do this upstream work
right with the DRM community.

I posted the display driver separately since it is, well
a separate business. I guess Thierry collects these drivers,
else tell me.

Linus Walleij (4):
  drm/tve200: Add DT bindings
  drm/tve200: Add new driver for TVE200
  ARM: dts: Add TVE200 to the Gemini SoC DTSI
  ARM: dts: Add TVE/TVC and ILI9322 panel to DIR-685

 .../devicetree/bindings/display/faraday,tve200.txt |  41 +++
 Documentation/gpu/index.rst                        |   1 +
 Documentation/gpu/tve200.rst                       |   6 +
 MAINTAINERS                                        |   6 +
 arch/arm/boot/dts/gemini-dlink-dir-685.dts         |  77 ++++-
 arch/arm/boot/dts/gemini.dtsi                      |  19 ++
 drivers/gpu/drm/Kconfig                            |   2 +
 drivers/gpu/drm/Makefile                           |   1 +
 drivers/gpu/drm/tve200/Kconfig                     |  15 +
 drivers/gpu/drm/tve200/Makefile                    |   5 +
 drivers/gpu/drm/tve200/tve200_connector.c          | 126 ++++++++
 drivers/gpu/drm/tve200/tve200_display.c            | 346 +++++++++++++++++++++
 drivers/gpu/drm/tve200/tve200_drm.h                | 129 ++++++++
 drivers/gpu/drm/tve200/tve200_drv.c                | 277 +++++++++++++++++
 14 files changed, 1050 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt
 create mode 100644 Documentation/gpu/tve200.rst
 create mode 100644 drivers/gpu/drm/tve200/Kconfig
 create mode 100644 drivers/gpu/drm/tve200/Makefile
 create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
 create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c

-- 
2.13.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 1/4] drm/tve200: Add DT bindings
  2017-08-13 15:11 ` Linus Walleij
@ 2017-08-13 15:11     ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW, Eric Anholt,
	Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r, Linus Walleij,
	devicetree-u79uwXL29TY76Z2rM5mHXA

This adds device tree bindings for the Faraday TVE200 IP block.
This IP block is present in the Gemini ARM SoC and also in some
Grain Media GMxxxx SoCs.

Cc: devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Signed-off-by: Linus Walleij <linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
 .../devicetree/bindings/display/faraday,tve200.txt | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt

diff --git a/Documentation/devicetree/bindings/display/faraday,tve200.txt b/Documentation/devicetree/bindings/display/faraday,tve200.txt
new file mode 100644
index 000000000000..95d588fbb2f2
--- /dev/null
+++ b/Documentation/devicetree/bindings/display/faraday,tve200.txt
@@ -0,0 +1,41 @@
+* Faraday TV Encoder TVE200
+
+Required properties:
+
+- compatible: must be one of:
+	"faraday,tve200"
+	"cortina,gemini-tvc", "faraday,tve200"
+
+- reg: base address and size of the control registers block
+
+- interrupts: contains an interrupt specifier for the interrupt
+	line from the TVE200
+
+- clock-names: should contain "PCLK" for the clock line clocking the
+	silicon and "TVE" for the 27MHz clock to the video driver
+
+- clocks: contains phandle and clock specifier pairs for the entries
+	in the clock-names property. See
+	Documentation/devicetree/bindings/clock/clock-bindings.txt
+
+Optional properties:
+
+- resets: contains the reset line phandle for the block
+
+Required sub-nodes:
+
+- port: describes LCD panel signals, following the common binding
+	for video transmitter interfaces; see
+	Documentation/devicetree/bindings/media/video-interfaces.txt
+
+Example:
+
+tvc@6a000000 {
+	compatible = "faraday,tve200";
+	reg = <0x6a000000 0x1000>;
+	interrupts = <13 IRQ_TYPE_EDGE_RISING>;
+	resets = <&syscon GEMINI_RESET_TVC>;
+	clocks = <&syscon GEMINI_CLK_GATE_TVC>,
+		 <&syscon GEMINI_CLK_TVC>;
+	clock-names = "PCLK", "TVE";
+};
-- 
2.13.4

--
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] 24+ messages in thread

* [PATCH 1/4] drm/tve200: Add DT bindings
@ 2017-08-13 15:11     ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: linux-arm-kernel

This adds device tree bindings for the Faraday TVE200 IP block.
This IP block is present in the Gemini ARM SoC and also in some
Grain Media GMxxxx SoCs.

Cc: devicetree at vger.kernel.org
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 .../devicetree/bindings/display/faraday,tve200.txt | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt

diff --git a/Documentation/devicetree/bindings/display/faraday,tve200.txt b/Documentation/devicetree/bindings/display/faraday,tve200.txt
new file mode 100644
index 000000000000..95d588fbb2f2
--- /dev/null
+++ b/Documentation/devicetree/bindings/display/faraday,tve200.txt
@@ -0,0 +1,41 @@
+* Faraday TV Encoder TVE200
+
+Required properties:
+
+- compatible: must be one of:
+	"faraday,tve200"
+	"cortina,gemini-tvc", "faraday,tve200"
+
+- reg: base address and size of the control registers block
+
+- interrupts: contains an interrupt specifier for the interrupt
+	line from the TVE200
+
+- clock-names: should contain "PCLK" for the clock line clocking the
+	silicon and "TVE" for the 27MHz clock to the video driver
+
+- clocks: contains phandle and clock specifier pairs for the entries
+	in the clock-names property. See
+	Documentation/devicetree/bindings/clock/clock-bindings.txt
+
+Optional properties:
+
+- resets: contains the reset line phandle for the block
+
+Required sub-nodes:
+
+- port: describes LCD panel signals, following the common binding
+	for video transmitter interfaces; see
+	Documentation/devicetree/bindings/media/video-interfaces.txt
+
+Example:
+
+tvc at 6a000000 {
+	compatible = "faraday,tve200";
+	reg = <0x6a000000 0x1000>;
+	interrupts = <13 IRQ_TYPE_EDGE_RISING>;
+	resets = <&syscon GEMINI_RESET_TVC>;
+	clocks = <&syscon GEMINI_CLK_GATE_TVC>,
+		 <&syscon GEMINI_CLK_TVC>;
+	clock-names = "PCLK", "TVE";
+};
-- 
2.13.4

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-13 15:11 ` Linus Walleij
@ 2017-08-13 15:11   ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: linux-arm-kernel

This adds a new DRM driver for the Faraday Technology TVE200
block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
be found in the StorLink SL3516 (later Cortina Systems CS3516)
as well as the Grain Media GM8180.

I do not have definitive word from anyone at Faraday that this
IP block is theirs, but it bears the hallmark of their 3-digit
version code (200) and is used in two SoCs from completely
different companies. (Grain Media was fully owned by Faraday
until it was transferred to NovoTek this january, and
Faraday did lots of work on the StorLink SoCs.)

The D-Link DIR-685 uses this in connection with the Ilitek
ILI9322 panel driver that supports BT.656 input, while the
GM8180 apparently has been used with the Cirrus Logic CS4954
digital video encoder. The oldest user seems to be
something called Techwall 2835.

This driver is heavily inspired by Eric Anholt's PL111
driver and therefore I have mentioned all the ancestor authors
in the header file.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 Documentation/gpu/index.rst               |   1 +
 Documentation/gpu/tve200.rst              |   6 +
 MAINTAINERS                               |   6 +
 drivers/gpu/drm/Kconfig                   |   2 +
 drivers/gpu/drm/Makefile                  |   1 +
 drivers/gpu/drm/tve200/Kconfig            |  15 ++
 drivers/gpu/drm/tve200/Makefile           |   5 +
 drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
 drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
 drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
 11 files changed, 914 insertions(+)
 create mode 100644 Documentation/gpu/tve200.rst
 create mode 100644 drivers/gpu/drm/tve200/Kconfig
 create mode 100644 drivers/gpu/drm/tve200/Makefile
 create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
 create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c

diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
index 35d673bf9b56..c36586dad29d 100644
--- a/Documentation/gpu/index.rst
+++ b/Documentation/gpu/index.rst
@@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
    pl111
    tegra
    tinydrm
+   tve200
    vc4
    vga-switcheroo
    vgaarbiter
diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
new file mode 100644
index 000000000000..69b17b324e12
--- /dev/null
+++ b/Documentation/gpu/tve200.rst
@@ -0,0 +1,6 @@
+==================================
+ drm/tve200 Faraday TV Encoder 200
+==================================
+
+.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
+   :doc: Faraday TV Encoder 200
diff --git a/MAINTAINERS b/MAINTAINERS
index e87cba115ea4..c3d42d68253a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
 S:	Maintained
 F:	drivers/gpu/drm/bochs/
 
+DRM DRIVER FOR FARADAY TVE200 TV ENCODER
+M:	Linus Walleij <linus.walleij@linaro.org>
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+S:	Maintained
+F:	drivers/gpu/drm/tve200/
+
 DRM DRIVER FOR INTEL I810 VIDEO CARDS
 S:	Orphan / Obsolete
 F:	drivers/gpu/drm/i810/
diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
index 83cb2a88c204..c5e1a8409285 100644
--- a/drivers/gpu/drm/Kconfig
+++ b/drivers/gpu/drm/Kconfig
@@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
 
 source "drivers/gpu/drm/pl111/Kconfig"
 
+source "drivers/gpu/drm/tve200/Kconfig"
+
 # Keep legacy drivers last
 
 menuconfig DRM_LEGACY
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index 24a066e1841c..cc81813e2238 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
 obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
 obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
 obj-$(CONFIG_DRM_PL111) += pl111/
+obj-$(CONFIG_DRM_TVE200) += tve200/
diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
new file mode 100644
index 000000000000..21d9841ddb88
--- /dev/null
+++ b/drivers/gpu/drm/tve200/Kconfig
@@ -0,0 +1,15 @@
+config DRM_TVE200
+	tristate "DRM Support for Faraday TV Encoder TVE200"
+	depends on DRM
+	depends on CMA
+	depends on ARM || COMPILE_TEST
+	depends on OF
+	select DRM_PANEL
+	select DRM_KMS_HELPER
+	select DRM_KMS_CMA_HELPER
+	select DRM_GEM_CMA_HELPER
+	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
+	help
+	  Choose this option for DRM support for the Faraday TV Encoder
+	  TVE200 Controller.
+	  If M is selected the module will be called tve200_drm.
diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
new file mode 100644
index 000000000000..a9dba54f7ee5
--- /dev/null
+++ b/drivers/gpu/drm/tve200/Makefile
@@ -0,0 +1,5 @@
+tve200_drm-y +=	tve200_connector.o \
+		tve200_display.o \
+		tve200_drv.o
+
+obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
new file mode 100644
index 000000000000..93e99156d375
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_connector.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+/**
+ * tve200_drm_connector.c
+ * Implementation of the connector functions for the Faraday TV Encoder
+ */
+#include <linux/version.h>
+#include <linux/shmem_fs.h>
+#include <linux/dma-buf.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_of.h>
+#include <drm/drm_panel.h>
+
+#include "tve200_drm.h"
+
+static void tve200_connector_destroy(struct drm_connector *connector)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	if (tve200con->panel)
+		drm_panel_detach(tve200con->panel);
+
+	drm_connector_unregister(connector);
+	drm_connector_cleanup(connector);
+}
+
+static enum drm_connector_status tve200_connector_detect(struct drm_connector
+							*connector, bool force)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	return (tve200con->panel ?
+		connector_status_connected :
+		connector_status_disconnected);
+}
+
+static int tve200_connector_helper_get_modes(struct drm_connector *connector)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	if (!tve200con->panel)
+		return 0;
+
+	return drm_panel_get_modes(tve200con->panel);
+}
+
+static const struct drm_connector_funcs connector_funcs = {
+	.fill_modes = drm_helper_probe_single_connector_modes,
+	.destroy = tve200_connector_destroy,
+	.detect = tve200_connector_detect,
+	.dpms = drm_atomic_helper_connector_dpms,
+	.reset = drm_atomic_helper_connector_reset,
+	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+};
+
+static const struct drm_connector_helper_funcs connector_helper_funcs = {
+	.get_modes = tve200_connector_helper_get_modes,
+};
+
+/*
+ * Walks the OF graph to find the panel node and then asks DRM to look
+ * up the panel.
+ */
+static struct drm_panel *tve200_get_panel(struct device *dev)
+{
+	struct device_node *endpoint, *panel_node;
+	struct device_node *np = dev->of_node;
+	struct drm_panel *panel;
+
+	endpoint = of_graph_get_next_endpoint(np, NULL);
+	if (!endpoint) {
+		dev_err(dev, "no endpoint to fetch panel\n");
+		return NULL;
+	}
+
+	/* Don't proceed if we have an endpoint but no panel_node tied to it */
+	panel_node = of_graph_get_remote_port_parent(endpoint);
+	of_node_put(endpoint);
+	if (!panel_node) {
+		dev_err(dev, "no valid panel node\n");
+		return NULL;
+	}
+
+	panel = of_drm_find_panel(panel_node);
+	of_node_put(panel_node);
+
+	return panel;
+}
+
+int tve200_connector_init(struct drm_device *dev)
+{
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+	struct tve200_drm_connector *tve200con = &priv->connector;
+	struct drm_connector *connector = &tve200con->connector;
+
+	drm_connector_init(dev, connector, &connector_funcs,
+			   DRM_MODE_CONNECTOR_DPI);
+	drm_connector_helper_add(connector, &connector_helper_funcs);
+
+	tve200con->panel = tve200_get_panel(dev->dev);
+	if (tve200con->panel)
+		drm_panel_attach(tve200con->panel, connector);
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
new file mode 100644
index 000000000000..027553aacb33
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_display.c
@@ -0,0 +1,346 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+#include <linux/clk.h>
+#include <linux/version.h>
+#include <linux/dma-buf.h>
+#include <linux/of_graph.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_panel.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_fb_cma_helper.h>
+
+#include "tve200_drm.h"
+
+irqreturn_t tve200_irq(int irq, void *data)
+{
+	struct tve200_drm_dev_private *priv = data;
+	u32 stat;
+	u32 val;
+
+	stat = readl(priv->regs + TVE200_INT_STAT);
+
+	if (!stat)
+		return IRQ_NONE;
+
+	/*
+	 * Vblank IRQ
+	 *
+	 * The hardware is a bit tilted: the line stays high after clearing
+	 * the vblank IRQ, fireing many more interrupts. We counter this
+	 * by toggling the IRQ back and forth from fireing at vblank and
+	 * fireing at start of active image, which works around the problem
+	 * since those occur strictly in sequence, and we get two IRQs for each
+	 * frame, one at start of Vblank (that we make call into the CRTC) and
+	 * another one at the start of the image (that we discard).
+	 */
+	if (stat & TVE200_INT_V_STATUS) {
+		val = readl(priv->regs + TVE200_CTRL);
+		/* We have an actual start of vsync */
+		if (!(val & TVE200_VSTSTYPE_BITS)) {
+			drm_crtc_handle_vblank(&priv->pipe.crtc);
+			/* Toggle trigger to start of active image */
+			val |= TVE200_VSTSTYPE_VAI;
+		} else {
+			/* Toggle trigger back to start of vsync */
+			val &= ~TVE200_VSTSTYPE_BITS;
+		}
+		writel(val, priv->regs + TVE200_CTRL);
+	} else
+		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
+
+	/* Clear the interrupt once done */
+	writel(stat, priv->regs + TVE200_INT_CLR);
+
+	return IRQ_HANDLED;
+}
+
+static int tve200_display_check(struct drm_simple_display_pipe *pipe,
+			       struct drm_plane_state *pstate,
+			       struct drm_crtc_state *cstate)
+{
+	const struct drm_display_mode *mode = &cstate->mode;
+	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
+	struct drm_framebuffer *fb = pstate->fb;
+	struct drm_connector *connector = pipe->connector;
+	struct drm_device *drm = connector->dev;
+
+	/*
+	 * We support these specific resolutions and nothing else.
+	 */
+	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
+	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
+	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
+	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
+	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
+		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
+			mode->hdisplay, mode->vdisplay);
+		return -EINVAL;
+	}
+
+	if (fb) {
+		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
+
+		/* FB base address must be dword aligned. */
+		if (offset & 3) {
+			dev_err(drm->dev, "FB not 32-bit aligned\n");
+			return -EINVAL;
+		}
+
+		/*
+		 * There's no pitch register, the mode's hdisplay
+		 * controls this.
+		 */
+		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
+			dev_err(drm->dev, "can't handle pitches\n");
+			return -EINVAL;
+		}
+
+		/*
+		 * We can't change the FB format in a flicker-free
+		 * manner (and only update it during CRTC enable).
+		 */
+		if (old_fb && old_fb->format != fb->format)
+			cstate->mode_changed = true;
+	}
+
+	return 0;
+}
+
+static void tve200_display_enable(struct drm_simple_display_pipe *pipe,
+				 struct drm_crtc_state *cstate)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_plane *plane = &pipe->plane;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	const struct drm_display_mode *mode = &cstate->mode;
+	struct drm_framebuffer *fb = plane->state->fb;
+	struct drm_connector *connector = &priv->connector.connector;
+	u32 format = fb->format->format;
+	u32 ctrl1 = 0;
+
+	clk_prepare_enable(priv->clk);
+
+	/* Function 1 */
+	ctrl1 |= TVE200_CTRL_CSMODE;
+	/* Interlace mode for CCIR656: parameterize? */
+	ctrl1 |= TVE200_CTRL_NONINTERLACE;
+	/* 32 words per burst */
+	ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
+	/* 16 retries */
+	ctrl1 |= TVE200_CTRL_RETRYCNT_16;
+	/* NTSC mode: parametrize? */
+	ctrl1 |= TVE200_CTRL_NTSC;
+
+	/* Vsync IRQ at start of Vsync at first */
+	ctrl1 |= TVE200_VSTSTYPE_VSYNC;
+
+	if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
+		ctrl1 |= TVE200_CTRL_TVCLKP;
+
+	if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
+	    (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
+		ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
+		dev_info(drm->dev, "CIF mode\n");
+	} else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
+		ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
+		dev_info(drm->dev, "VGA mode\n");
+	} else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
+		   (mode->hdisplay == 720 && mode->vdisplay == 576)) {
+		ctrl1 |= TVE200_CTRL_IPRESOL_D1;
+		dev_info(drm->dev, "D1 mode\n");
+	}
+
+	if (format & DRM_FORMAT_BIG_ENDIAN) {
+		ctrl1 |= TVE200_CTRL_BBBP;
+		format &= ~DRM_FORMAT_BIG_ENDIAN;
+	}
+
+	switch (format) {
+	case DRM_FORMAT_XRGB8888:
+		ctrl1 |= TVE200_IPDMOD_RGB888;
+		break;
+	case DRM_FORMAT_RGB565:
+		ctrl1 |= TVE200_IPDMOD_RGB565;
+		break;
+	case DRM_FORMAT_XRGB1555:
+		ctrl1 |= TVE200_IPDMOD_RGB555;
+		break;
+	case DRM_FORMAT_XBGR8888:
+		ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_BGR565:
+		ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_XBGR1555:
+		ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_YUYV:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
+		break;
+	case DRM_FORMAT_YVYU:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
+		break;
+	case DRM_FORMAT_UYVY:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
+		break;
+	case DRM_FORMAT_VYUY:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
+		break;
+	case DRM_FORMAT_YUV420:
+		ctrl1 |= TVE200_CTRL_YUV420;
+		ctrl1 |= TVE200_IPDMOD_YUV420;
+		break;
+	default:
+		dev_err(drm->dev, "Unknown FB format 0x%08x\n",
+			fb->format->format);
+		break;
+	}
+
+	ctrl1 |= TVE200_TVEEN;
+
+	drm_panel_prepare(priv->connector.panel);
+
+	/* Turn it on */
+	writel(ctrl1, priv->regs + TVE200_CTRL);
+
+	drm_panel_enable(priv->connector.panel);
+
+	drm_crtc_vblank_on(crtc);
+}
+
+void tve200_display_disable(struct drm_simple_display_pipe *pipe)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	drm_crtc_vblank_off(crtc);
+
+	drm_panel_disable(priv->connector.panel);
+
+	/* Disable and Power Down */
+	writel(0, priv->regs + TVE200_CTRL);
+
+	drm_panel_unprepare(priv->connector.panel);
+
+	clk_disable_unprepare(priv->clk);
+}
+
+static void tve200_display_update(struct drm_simple_display_pipe *pipe,
+				 struct drm_plane_state *old_pstate)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	struct drm_pending_vblank_event *event = crtc->state->event;
+	struct drm_plane *plane = &pipe->plane;
+	struct drm_plane_state *pstate = plane->state;
+	struct drm_framebuffer *fb = pstate->fb;
+
+	if (fb) {
+		/* For RGB, the Y component is used as base address */
+		writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
+		       priv->regs + TVE200_Y_FRAME_BASE_ADDR);
+
+		/* For three plane YUV we need two more addresses */
+		if (fb->format->format == DRM_FORMAT_YUV420) {
+			writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
+			       priv->regs + TVE200_U_FRAME_BASE_ADDR);
+			writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
+			       priv->regs + TVE200_V_FRAME_BASE_ADDR);
+		}
+	}
+
+	if (event) {
+		crtc->state->event = NULL;
+
+		spin_lock_irq(&crtc->dev->event_lock);
+		if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+		spin_unlock_irq(&crtc->dev->event_lock);
+	}
+}
+
+int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
+	return 0;
+}
+
+void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	writel(0, priv->regs + TVE200_INT_EN);
+}
+
+static int tve200_display_prepare_fb(struct drm_simple_display_pipe *pipe,
+				    struct drm_plane_state *plane_state)
+{
+	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
+}
+
+const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
+	.check = tve200_display_check,
+	.enable = tve200_display_enable,
+	.disable = tve200_display_disable,
+	.update = tve200_display_update,
+	.prepare_fb = tve200_display_prepare_fb,
+};
+
+int tve200_display_init(struct drm_device *drm)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	int ret;
+	static const u32 formats[] = {
+		DRM_FORMAT_XRGB8888,
+		DRM_FORMAT_XBGR8888,
+		DRM_FORMAT_RGB565,
+		DRM_FORMAT_BGR565,
+		DRM_FORMAT_XRGB1555,
+		DRM_FORMAT_XBGR1555,
+		/*
+		 * The controller actually supports any YCbCr ordering,
+		 * for packed YCbCr. This just lists the orderings that
+		 * DRM supports.
+		 */
+		DRM_FORMAT_YUYV,
+		DRM_FORMAT_YVYU,
+		DRM_FORMAT_UYVY,
+		DRM_FORMAT_VYUY,
+		/* This uses three planes */
+		DRM_FORMAT_YUV420,
+	};
+
+	ret = drm_simple_display_pipe_init(drm, &priv->pipe,
+					   &tve200_display_funcs,
+					   formats, ARRAY_SIZE(formats),
+					   &priv->connector.connector);
+	if (ret)
+		return ret;
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/tve200/tve200_drm.h b/drivers/gpu/drm/tve200/tve200_drm.h
new file mode 100644
index 000000000000..f00fc47a6bd1
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_drm.h
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+#ifndef _TVE200_DRM_H_
+#define _TVE200_DRM_H_
+
+/* Bits 2-31 are valid physical base addresses */
+#define TVE200_Y_FRAME_BASE_ADDR	0x00
+#define TVE200_U_FRAME_BASE_ADDR	0x04
+#define TVE200_V_FRAME_BASE_ADDR	0x08
+
+#define TVE200_INT_EN			0x0C
+#define TVE200_INT_CLR			0x10
+#define TVE200_INT_STAT			0x14
+#define TVE200_INT_BUS_ERR		BIT(7)
+#define TVE200_INT_V_STATUS		BIT(6) /* vertical blank */
+#define TVE200_INT_V_NEXT_FRAME		BIT(5)
+#define TVE200_INT_U_NEXT_FRAME		BIT(4)
+#define TVE200_INT_Y_NEXT_FRAME		BIT(3)
+#define TVE200_INT_V_FIFO_UNDERRUN	BIT(2)
+#define TVE200_INT_U_FIFO_UNDERRUN	BIT(1)
+#define TVE200_INT_Y_FIFO_UNDERRUN	BIT(0)
+#define TVE200_FIFO_UNDERRUNS		(TVE200_INT_V_FIFO_UNDERRUN | \
+					 TVE200_INT_U_FIFO_UNDERRUN | \
+					 TVE200_INT_Y_FIFO_UNDERRUN)
+
+#define TVE200_CTRL			0x18
+#define TVE200_CTRL_YUV420		BIT(31)
+#define TVE200_CTRL_CSMODE		BIT(30)
+#define TVE200_CTRL_NONINTERLACE	BIT(28) /* 0 = non-interlace CCIR656 */
+#define TVE200_CTRL_TVCLKP		BIT(27) /* Inverted clock phase */
+/* Bits 24..26 define the burst size after arbitration on the bus */
+#define TVE200_CTRL_BURST_4_WORDS	(0 << 24)
+#define TVE200_CTRL_BURST_8_WORDS	(1 << 24)
+#define TVE200_CTRL_BURST_16_WORDS	(2 << 24)
+#define TVE200_CTRL_BURST_32_WORDS	(3 << 24)
+#define TVE200_CTRL_BURST_64_WORDS	(4 << 24)
+#define TVE200_CTRL_BURST_128_WORDS	(5 << 24)
+#define TVE200_CTRL_BURST_256_WORDS	(6 << 24)
+#define TVE200_CTRL_BURST_0_WORDS	(7 << 24) /* ? */
+/*
+ * Bits 16..23 is the retry count*16 before issueing a new AHB transfer
+ * on the AHB bus.
+ */
+#define TVE200_CTRL_RETRYCNT_MASK	GENMASK(23, 16)
+#define TVE200_CTRL_RETRYCNT_16		(1 << 16)
+#define TVE200_CTRL_BBBP		BIT(15) /* 0 = little-endian */
+/* Bits 12..14 define the YCbCr ordering */
+#define TVE200_CTRL_YCBCRODR_CB0Y0CR0Y1	(0 << 12)
+#define TVE200_CTRL_YCBCRODR_Y0CB0Y1CR0	(1 << 12)
+#define TVE200_CTRL_YCBCRODR_CR0Y0CB0Y1	(2 << 12)
+#define TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0	(3 << 12)
+#define TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0	(4 << 12)
+#define TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0	(5 << 12)
+#define TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0	(6 << 12)
+#define TVE200_CTRL_YCBCRODR_Y0CR0Y1CB0	(7 << 12)
+/* Bits 10..11 define the input resolution (framebuffer size) */
+#define TVE200_CTRL_IPRESOL_CIF		(0 << 10)
+#define TVE200_CTRL_IPRESOL_VGA		(1 << 10)
+#define TVE200_CTRL_IPRESOL_D1		(2 << 10)
+#define TVE200_CTRL_NTSC		BIT(9) /* 0 = PAL, 1 = NTSC */
+#define TVE200_CTRL_INTERLACE		BIT(8) /* 1 = interlace, only for D1 */
+#define TVE200_IPDMOD_RGB555		(0 << 6) /* TVE200_CTRL_YUV420 = 0 */
+#define TVE200_IPDMOD_RGB565		(1 << 6)
+#define TVE200_IPDMOD_RGB888		(2 << 6)
+#define TVE200_IPDMOD_YUV420		(2 << 6) /* TVE200_CTRL_YUV420 = 1 */
+#define TVE200_IPDMOD_YUV422		(3 << 6)
+/* Bits 4 & 5 define when to fire the vblank IRQ */
+#define TVE200_VSTSTYPE_VSYNC		(0 << 4) /* start of vsync */
+#define TVE200_VSTSTYPE_VBP		(1 << 4) /* start of v back porch */
+#define TVE200_VSTSTYPE_VAI		(2 << 4) /* start of v active image */
+#define TVE200_VSTSTYPE_VFP		(3 << 4) /* start of v front porch */
+#define TVE200_VSTSTYPE_BITS		(BIT(4) | BIT(5))
+#define TVE200_BGR			BIT(1) /* 0 = RGB, 1 = BGR */
+#define TVE200_TVEEN			BIT(0) /* Enable TVE block */
+
+#define TVE200_CTRL_2			0x1c
+#define TVE200_CTRL_3			0x20
+
+#define TVE200_CTRL_4			0x24
+#define TVE200_CTRL_4_RESET		BIT(0) /* triggers reset of TVE200 */
+
+#include <drm/drm_gem.h>
+#include <drm/drm_simple_kms_helper.h>
+
+struct tve200_drm_connector {
+	struct drm_connector connector;
+	struct drm_panel *panel;
+};
+
+struct tve200_drm_dev_private {
+	struct drm_device *drm;
+
+	struct tve200_drm_connector connector;
+	struct drm_simple_display_pipe pipe;
+	struct drm_fbdev_cma *fbdev;
+
+	void *regs;
+	struct clk *pclk;
+	struct clk *clk;
+};
+
+#define to_tve200_connector(x) \
+	container_of(x, struct tve200_drm_connector, connector)
+
+int tve200_display_init(struct drm_device *dev);
+int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc);
+void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc);
+irqreturn_t tve200_irq(int irq, void *data);
+int tve200_connector_init(struct drm_device *dev);
+int tve200_encoder_init(struct drm_device *dev);
+int tve200_dumb_create(struct drm_file *file_priv,
+		      struct drm_device *dev,
+		      struct drm_mode_create_dumb *args);
+
+#endif /* _TVE200_DRM_H_ */
diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c
new file mode 100644
index 000000000000..9e34147e2617
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_drv.c
@@ -0,0 +1,277 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+/**
+ * DOC: Faraday TV Encoder TVE200 DRM Driver
+ *
+ * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
+ * Controller (TVC) and is found in the Gemini Chipset from Storlink
+ * Semiconductor (later Storm Semiconductor, later Cortina Systems)
+ * but also in the Grain Media GM8180 chipset. On the Gemini the module
+ * is connected to 8 data lines and a single clock line, comprising an
+ * 8-bit BT.656 interface.
+ *
+ * This is a very basic YUV display driver. The datasheet specifies that
+ * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
+ * the hallmark of any TV encoder supporting both PAL and NTSC.
+ *
+ * This driver exposes a standard KMS interface for this TV encoder.
+ */
+
+#include <linux/clk.h>
+#include <linux/dma-buf.h>
+#include <linux/irq.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/shmem_fs.h>
+#include <linux/slab.h>
+#include <linux/version.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_fb_cma_helper.h>
+#include <drm/drm_panel.h>
+
+#include "tve200_drm.h"
+
+#define DRIVER_DESC      "DRM module for Faraday TVE200"
+
+struct drm_mode_config_funcs mode_config_funcs = {
+	.fb_create = drm_fb_cma_create,
+	.atomic_check = drm_atomic_helper_check,
+	.atomic_commit = drm_atomic_helper_commit,
+};
+
+static int tve200_modeset_init(struct drm_device *dev)
+{
+	struct drm_mode_config *mode_config;
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+	int ret = 0;
+
+	drm_mode_config_init(dev);
+	mode_config = &dev->mode_config;
+	mode_config->funcs = &mode_config_funcs;
+	mode_config->min_width = 352;
+	mode_config->max_width = 720;
+	mode_config->min_height = 240;
+	mode_config->max_height = 576;
+
+	ret = tve200_connector_init(dev);
+	if (ret) {
+		dev_err(dev->dev, "Failed to create tve200_drm_connector\n");
+		goto out_config;
+	}
+
+	/*
+	 * Don't actually attach if we didn't find a drm_panel
+	 * attached to us.
+	 */
+	if (!priv->connector.panel) {
+		dev_info(dev->dev,
+			 "deferring due to lack of DRM panel device\n");
+		ret = -EPROBE_DEFER;
+		goto out_config;
+	}
+	dev_info(dev->dev, "attached to panel %s\n",
+		 dev_name(priv->connector.panel->dev));
+
+	ret = tve200_display_init(dev);
+	if (ret) {
+		dev_err(dev->dev, "failed to init display\n");
+		goto out_config;
+	}
+
+	ret = drm_vblank_init(dev, 1);
+	if (ret) {
+		dev_err(dev->dev, "failed to init vblank\n");
+		goto out_config;
+	}
+
+	drm_mode_config_reset(dev);
+
+	/*
+	 * Passing in 16 here will make the RGB656 mode the default
+	 * Passing in 32 will use XRGB8888 mode
+	 */
+	priv->fbdev = drm_fbdev_cma_init(dev, 16,
+					 dev->mode_config.num_connector);
+	drm_kms_helper_poll_init(dev);
+
+	goto finish;
+
+out_config:
+	drm_mode_config_cleanup(dev);
+finish:
+	return ret;
+}
+
+DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
+
+static void tve200_lastclose(struct drm_device *dev)
+{
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+
+	drm_fbdev_cma_restore_mode(priv->fbdev);
+}
+
+static struct drm_driver tve200_drm_driver = {
+	.driver_features =
+		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
+	.lastclose = tve200_lastclose,
+	.ioctls = NULL,
+	.fops = &drm_fops,
+	.name = "tve200",
+	.desc = DRIVER_DESC,
+	.date = "20170703",
+	.major = 1,
+	.minor = 0,
+	.patchlevel = 0,
+	.dumb_create = drm_gem_cma_dumb_create,
+	.dumb_destroy = drm_gem_dumb_destroy,
+	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
+	.gem_free_object = drm_gem_cma_free_object,
+	.gem_vm_ops = &drm_gem_cma_vm_ops,
+
+	.enable_vblank = tve200_enable_vblank,
+	.disable_vblank = tve200_disable_vblank,
+
+	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+	.gem_prime_import = drm_gem_prime_import,
+	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
+	.gem_prime_export = drm_gem_prime_export,
+	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
+};
+
+static int tve200_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tve200_drm_dev_private *priv;
+	struct drm_device *drm;
+	struct resource *res;
+	int irq;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	drm = drm_dev_alloc(&tve200_drm_driver, dev);
+	if (IS_ERR(drm))
+		return PTR_ERR(drm);
+	platform_set_drvdata(pdev, drm);
+	priv->drm = drm;
+	drm->dev_private = priv;
+
+	/* Clock the silicon so we can access the registers */
+	priv->pclk = devm_clk_get(dev, "PCLK");
+	if (IS_ERR(priv->pclk)) {
+		dev_err(dev, "unable to get PCLK\n");
+		ret = PTR_ERR(priv->pclk);
+		goto dev_unref;
+	}
+	ret = clk_prepare_enable(priv->pclk);
+	if (ret) {
+		dev_err(dev, "failed to enable PCLK\n");
+		goto dev_unref;
+	}
+
+	/* This clock is for the pixels (27MHz) */
+	priv->clk = devm_clk_get(dev, "TVE");
+	if (IS_ERR(priv->clk)) {
+		dev_err(dev, "unable to get TVE clock\n");
+		ret = PTR_ERR(priv->clk);
+		goto clk_disable;
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->regs = devm_ioremap_resource(dev, res);
+	if (!priv->regs) {
+		dev_err(dev, "%s failed mmio\n", __func__);
+		ret = -EINVAL;
+		goto dev_unref;
+	}
+
+	irq = platform_get_irq(pdev, 0);
+	if (!irq) {
+		ret = -EINVAL;
+		goto dev_unref;
+	}
+
+	/* turn off interrupts before requesting the irq */
+	writel(0, priv->regs + TVE200_INT_EN);
+
+	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
+	if (ret) {
+		dev_err(dev, "failed to request irq %d\n", ret);
+		return ret;
+	}
+
+	ret = tve200_modeset_init(drm);
+	if (ret)
+		goto dev_unref;
+
+	ret = drm_dev_register(drm, 0);
+	if (ret < 0)
+		goto dev_unref;
+
+	return 0;
+
+clk_disable:
+	clk_disable_unprepare(priv->pclk);
+dev_unref:
+	drm_dev_unref(drm);
+	return ret;
+}
+
+static int tve200_remove(struct platform_device *pdev)
+{
+	struct drm_device *drm = platform_get_drvdata(pdev);
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	drm_dev_unregister(drm);
+	if (priv->fbdev)
+		drm_fbdev_cma_fini(priv->fbdev);
+	drm_mode_config_cleanup(drm);
+	clk_disable_unprepare(priv->pclk);
+	drm_dev_unref(drm);
+
+	return 0;
+}
+
+static const struct of_device_id tve200_of_match[] = {
+	{
+		.compatible = "faraday,tve200",
+	},
+	{},
+};
+
+static struct platform_driver tve200_driver = {
+	.driver = {
+		.name           = "tve200",
+		.of_match_table = of_match_ptr(tve200_of_match),
+	},
+	.probe = tve200_probe,
+	.remove = tve200_remove,
+};
+module_platform_driver(tve200_driver);
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
+MODULE_LICENSE("GPL");
-- 
2.13.4

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-13 15:11   ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: dri-devel, Eric Anholt, Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel

This adds a new DRM driver for the Faraday Technology TVE200
block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
be found in the StorLink SL3516 (later Cortina Systems CS3516)
as well as the Grain Media GM8180.

I do not have definitive word from anyone at Faraday that this
IP block is theirs, but it bears the hallmark of their 3-digit
version code (200) and is used in two SoCs from completely
different companies. (Grain Media was fully owned by Faraday
until it was transferred to NovoTek this january, and
Faraday did lots of work on the StorLink SoCs.)

The D-Link DIR-685 uses this in connection with the Ilitek
ILI9322 panel driver that supports BT.656 input, while the
GM8180 apparently has been used with the Cirrus Logic CS4954
digital video encoder. The oldest user seems to be
something called Techwall 2835.

This driver is heavily inspired by Eric Anholt's PL111
driver and therefore I have mentioned all the ancestor authors
in the header file.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 Documentation/gpu/index.rst               |   1 +
 Documentation/gpu/tve200.rst              |   6 +
 MAINTAINERS                               |   6 +
 drivers/gpu/drm/Kconfig                   |   2 +
 drivers/gpu/drm/Makefile                  |   1 +
 drivers/gpu/drm/tve200/Kconfig            |  15 ++
 drivers/gpu/drm/tve200/Makefile           |   5 +
 drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
 drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
 drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
 11 files changed, 914 insertions(+)
 create mode 100644 Documentation/gpu/tve200.rst
 create mode 100644 drivers/gpu/drm/tve200/Kconfig
 create mode 100644 drivers/gpu/drm/tve200/Makefile
 create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
 create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
 create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c

diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
index 35d673bf9b56..c36586dad29d 100644
--- a/Documentation/gpu/index.rst
+++ b/Documentation/gpu/index.rst
@@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
    pl111
    tegra
    tinydrm
+   tve200
    vc4
    vga-switcheroo
    vgaarbiter
diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
new file mode 100644
index 000000000000..69b17b324e12
--- /dev/null
+++ b/Documentation/gpu/tve200.rst
@@ -0,0 +1,6 @@
+==================================
+ drm/tve200 Faraday TV Encoder 200
+==================================
+
+.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
+   :doc: Faraday TV Encoder 200
diff --git a/MAINTAINERS b/MAINTAINERS
index e87cba115ea4..c3d42d68253a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
 S:	Maintained
 F:	drivers/gpu/drm/bochs/
 
+DRM DRIVER FOR FARADAY TVE200 TV ENCODER
+M:	Linus Walleij <linus.walleij@linaro.org>
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+S:	Maintained
+F:	drivers/gpu/drm/tve200/
+
 DRM DRIVER FOR INTEL I810 VIDEO CARDS
 S:	Orphan / Obsolete
 F:	drivers/gpu/drm/i810/
diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
index 83cb2a88c204..c5e1a8409285 100644
--- a/drivers/gpu/drm/Kconfig
+++ b/drivers/gpu/drm/Kconfig
@@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
 
 source "drivers/gpu/drm/pl111/Kconfig"
 
+source "drivers/gpu/drm/tve200/Kconfig"
+
 # Keep legacy drivers last
 
 menuconfig DRM_LEGACY
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index 24a066e1841c..cc81813e2238 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
 obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
 obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
 obj-$(CONFIG_DRM_PL111) += pl111/
+obj-$(CONFIG_DRM_TVE200) += tve200/
diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
new file mode 100644
index 000000000000..21d9841ddb88
--- /dev/null
+++ b/drivers/gpu/drm/tve200/Kconfig
@@ -0,0 +1,15 @@
+config DRM_TVE200
+	tristate "DRM Support for Faraday TV Encoder TVE200"
+	depends on DRM
+	depends on CMA
+	depends on ARM || COMPILE_TEST
+	depends on OF
+	select DRM_PANEL
+	select DRM_KMS_HELPER
+	select DRM_KMS_CMA_HELPER
+	select DRM_GEM_CMA_HELPER
+	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
+	help
+	  Choose this option for DRM support for the Faraday TV Encoder
+	  TVE200 Controller.
+	  If M is selected the module will be called tve200_drm.
diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
new file mode 100644
index 000000000000..a9dba54f7ee5
--- /dev/null
+++ b/drivers/gpu/drm/tve200/Makefile
@@ -0,0 +1,5 @@
+tve200_drm-y +=	tve200_connector.o \
+		tve200_display.o \
+		tve200_drv.o
+
+obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
new file mode 100644
index 000000000000..93e99156d375
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_connector.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+/**
+ * tve200_drm_connector.c
+ * Implementation of the connector functions for the Faraday TV Encoder
+ */
+#include <linux/version.h>
+#include <linux/shmem_fs.h>
+#include <linux/dma-buf.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_of.h>
+#include <drm/drm_panel.h>
+
+#include "tve200_drm.h"
+
+static void tve200_connector_destroy(struct drm_connector *connector)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	if (tve200con->panel)
+		drm_panel_detach(tve200con->panel);
+
+	drm_connector_unregister(connector);
+	drm_connector_cleanup(connector);
+}
+
+static enum drm_connector_status tve200_connector_detect(struct drm_connector
+							*connector, bool force)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	return (tve200con->panel ?
+		connector_status_connected :
+		connector_status_disconnected);
+}
+
+static int tve200_connector_helper_get_modes(struct drm_connector *connector)
+{
+	struct tve200_drm_connector *tve200con =
+		to_tve200_connector(connector);
+
+	if (!tve200con->panel)
+		return 0;
+
+	return drm_panel_get_modes(tve200con->panel);
+}
+
+static const struct drm_connector_funcs connector_funcs = {
+	.fill_modes = drm_helper_probe_single_connector_modes,
+	.destroy = tve200_connector_destroy,
+	.detect = tve200_connector_detect,
+	.dpms = drm_atomic_helper_connector_dpms,
+	.reset = drm_atomic_helper_connector_reset,
+	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+};
+
+static const struct drm_connector_helper_funcs connector_helper_funcs = {
+	.get_modes = tve200_connector_helper_get_modes,
+};
+
+/*
+ * Walks the OF graph to find the panel node and then asks DRM to look
+ * up the panel.
+ */
+static struct drm_panel *tve200_get_panel(struct device *dev)
+{
+	struct device_node *endpoint, *panel_node;
+	struct device_node *np = dev->of_node;
+	struct drm_panel *panel;
+
+	endpoint = of_graph_get_next_endpoint(np, NULL);
+	if (!endpoint) {
+		dev_err(dev, "no endpoint to fetch panel\n");
+		return NULL;
+	}
+
+	/* Don't proceed if we have an endpoint but no panel_node tied to it */
+	panel_node = of_graph_get_remote_port_parent(endpoint);
+	of_node_put(endpoint);
+	if (!panel_node) {
+		dev_err(dev, "no valid panel node\n");
+		return NULL;
+	}
+
+	panel = of_drm_find_panel(panel_node);
+	of_node_put(panel_node);
+
+	return panel;
+}
+
+int tve200_connector_init(struct drm_device *dev)
+{
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+	struct tve200_drm_connector *tve200con = &priv->connector;
+	struct drm_connector *connector = &tve200con->connector;
+
+	drm_connector_init(dev, connector, &connector_funcs,
+			   DRM_MODE_CONNECTOR_DPI);
+	drm_connector_helper_add(connector, &connector_helper_funcs);
+
+	tve200con->panel = tve200_get_panel(dev->dev);
+	if (tve200con->panel)
+		drm_panel_attach(tve200con->panel, connector);
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
new file mode 100644
index 000000000000..027553aacb33
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_display.c
@@ -0,0 +1,346 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+#include <linux/clk.h>
+#include <linux/version.h>
+#include <linux/dma-buf.h>
+#include <linux/of_graph.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_panel.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_fb_cma_helper.h>
+
+#include "tve200_drm.h"
+
+irqreturn_t tve200_irq(int irq, void *data)
+{
+	struct tve200_drm_dev_private *priv = data;
+	u32 stat;
+	u32 val;
+
+	stat = readl(priv->regs + TVE200_INT_STAT);
+
+	if (!stat)
+		return IRQ_NONE;
+
+	/*
+	 * Vblank IRQ
+	 *
+	 * The hardware is a bit tilted: the line stays high after clearing
+	 * the vblank IRQ, fireing many more interrupts. We counter this
+	 * by toggling the IRQ back and forth from fireing at vblank and
+	 * fireing at start of active image, which works around the problem
+	 * since those occur strictly in sequence, and we get two IRQs for each
+	 * frame, one at start of Vblank (that we make call into the CRTC) and
+	 * another one at the start of the image (that we discard).
+	 */
+	if (stat & TVE200_INT_V_STATUS) {
+		val = readl(priv->regs + TVE200_CTRL);
+		/* We have an actual start of vsync */
+		if (!(val & TVE200_VSTSTYPE_BITS)) {
+			drm_crtc_handle_vblank(&priv->pipe.crtc);
+			/* Toggle trigger to start of active image */
+			val |= TVE200_VSTSTYPE_VAI;
+		} else {
+			/* Toggle trigger back to start of vsync */
+			val &= ~TVE200_VSTSTYPE_BITS;
+		}
+		writel(val, priv->regs + TVE200_CTRL);
+	} else
+		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
+
+	/* Clear the interrupt once done */
+	writel(stat, priv->regs + TVE200_INT_CLR);
+
+	return IRQ_HANDLED;
+}
+
+static int tve200_display_check(struct drm_simple_display_pipe *pipe,
+			       struct drm_plane_state *pstate,
+			       struct drm_crtc_state *cstate)
+{
+	const struct drm_display_mode *mode = &cstate->mode;
+	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
+	struct drm_framebuffer *fb = pstate->fb;
+	struct drm_connector *connector = pipe->connector;
+	struct drm_device *drm = connector->dev;
+
+	/*
+	 * We support these specific resolutions and nothing else.
+	 */
+	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
+	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
+	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
+	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
+	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
+		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
+			mode->hdisplay, mode->vdisplay);
+		return -EINVAL;
+	}
+
+	if (fb) {
+		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
+
+		/* FB base address must be dword aligned. */
+		if (offset & 3) {
+			dev_err(drm->dev, "FB not 32-bit aligned\n");
+			return -EINVAL;
+		}
+
+		/*
+		 * There's no pitch register, the mode's hdisplay
+		 * controls this.
+		 */
+		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
+			dev_err(drm->dev, "can't handle pitches\n");
+			return -EINVAL;
+		}
+
+		/*
+		 * We can't change the FB format in a flicker-free
+		 * manner (and only update it during CRTC enable).
+		 */
+		if (old_fb && old_fb->format != fb->format)
+			cstate->mode_changed = true;
+	}
+
+	return 0;
+}
+
+static void tve200_display_enable(struct drm_simple_display_pipe *pipe,
+				 struct drm_crtc_state *cstate)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_plane *plane = &pipe->plane;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	const struct drm_display_mode *mode = &cstate->mode;
+	struct drm_framebuffer *fb = plane->state->fb;
+	struct drm_connector *connector = &priv->connector.connector;
+	u32 format = fb->format->format;
+	u32 ctrl1 = 0;
+
+	clk_prepare_enable(priv->clk);
+
+	/* Function 1 */
+	ctrl1 |= TVE200_CTRL_CSMODE;
+	/* Interlace mode for CCIR656: parameterize? */
+	ctrl1 |= TVE200_CTRL_NONINTERLACE;
+	/* 32 words per burst */
+	ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
+	/* 16 retries */
+	ctrl1 |= TVE200_CTRL_RETRYCNT_16;
+	/* NTSC mode: parametrize? */
+	ctrl1 |= TVE200_CTRL_NTSC;
+
+	/* Vsync IRQ at start of Vsync at first */
+	ctrl1 |= TVE200_VSTSTYPE_VSYNC;
+
+	if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
+		ctrl1 |= TVE200_CTRL_TVCLKP;
+
+	if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
+	    (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
+		ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
+		dev_info(drm->dev, "CIF mode\n");
+	} else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
+		ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
+		dev_info(drm->dev, "VGA mode\n");
+	} else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
+		   (mode->hdisplay == 720 && mode->vdisplay == 576)) {
+		ctrl1 |= TVE200_CTRL_IPRESOL_D1;
+		dev_info(drm->dev, "D1 mode\n");
+	}
+
+	if (format & DRM_FORMAT_BIG_ENDIAN) {
+		ctrl1 |= TVE200_CTRL_BBBP;
+		format &= ~DRM_FORMAT_BIG_ENDIAN;
+	}
+
+	switch (format) {
+	case DRM_FORMAT_XRGB8888:
+		ctrl1 |= TVE200_IPDMOD_RGB888;
+		break;
+	case DRM_FORMAT_RGB565:
+		ctrl1 |= TVE200_IPDMOD_RGB565;
+		break;
+	case DRM_FORMAT_XRGB1555:
+		ctrl1 |= TVE200_IPDMOD_RGB555;
+		break;
+	case DRM_FORMAT_XBGR8888:
+		ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_BGR565:
+		ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_XBGR1555:
+		ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
+		break;
+	case DRM_FORMAT_YUYV:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
+		break;
+	case DRM_FORMAT_YVYU:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
+		break;
+	case DRM_FORMAT_UYVY:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
+		break;
+	case DRM_FORMAT_VYUY:
+		ctrl1 |= TVE200_IPDMOD_YUV422;
+		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
+		break;
+	case DRM_FORMAT_YUV420:
+		ctrl1 |= TVE200_CTRL_YUV420;
+		ctrl1 |= TVE200_IPDMOD_YUV420;
+		break;
+	default:
+		dev_err(drm->dev, "Unknown FB format 0x%08x\n",
+			fb->format->format);
+		break;
+	}
+
+	ctrl1 |= TVE200_TVEEN;
+
+	drm_panel_prepare(priv->connector.panel);
+
+	/* Turn it on */
+	writel(ctrl1, priv->regs + TVE200_CTRL);
+
+	drm_panel_enable(priv->connector.panel);
+
+	drm_crtc_vblank_on(crtc);
+}
+
+void tve200_display_disable(struct drm_simple_display_pipe *pipe)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	drm_crtc_vblank_off(crtc);
+
+	drm_panel_disable(priv->connector.panel);
+
+	/* Disable and Power Down */
+	writel(0, priv->regs + TVE200_CTRL);
+
+	drm_panel_unprepare(priv->connector.panel);
+
+	clk_disable_unprepare(priv->clk);
+}
+
+static void tve200_display_update(struct drm_simple_display_pipe *pipe,
+				 struct drm_plane_state *old_pstate)
+{
+	struct drm_crtc *crtc = &pipe->crtc;
+	struct drm_device *drm = crtc->dev;
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	struct drm_pending_vblank_event *event = crtc->state->event;
+	struct drm_plane *plane = &pipe->plane;
+	struct drm_plane_state *pstate = plane->state;
+	struct drm_framebuffer *fb = pstate->fb;
+
+	if (fb) {
+		/* For RGB, the Y component is used as base address */
+		writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
+		       priv->regs + TVE200_Y_FRAME_BASE_ADDR);
+
+		/* For three plane YUV we need two more addresses */
+		if (fb->format->format == DRM_FORMAT_YUV420) {
+			writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
+			       priv->regs + TVE200_U_FRAME_BASE_ADDR);
+			writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
+			       priv->regs + TVE200_V_FRAME_BASE_ADDR);
+		}
+	}
+
+	if (event) {
+		crtc->state->event = NULL;
+
+		spin_lock_irq(&crtc->dev->event_lock);
+		if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+		spin_unlock_irq(&crtc->dev->event_lock);
+	}
+}
+
+int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
+	return 0;
+}
+
+void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	writel(0, priv->regs + TVE200_INT_EN);
+}
+
+static int tve200_display_prepare_fb(struct drm_simple_display_pipe *pipe,
+				    struct drm_plane_state *plane_state)
+{
+	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
+}
+
+const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
+	.check = tve200_display_check,
+	.enable = tve200_display_enable,
+	.disable = tve200_display_disable,
+	.update = tve200_display_update,
+	.prepare_fb = tve200_display_prepare_fb,
+};
+
+int tve200_display_init(struct drm_device *drm)
+{
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+	int ret;
+	static const u32 formats[] = {
+		DRM_FORMAT_XRGB8888,
+		DRM_FORMAT_XBGR8888,
+		DRM_FORMAT_RGB565,
+		DRM_FORMAT_BGR565,
+		DRM_FORMAT_XRGB1555,
+		DRM_FORMAT_XBGR1555,
+		/*
+		 * The controller actually supports any YCbCr ordering,
+		 * for packed YCbCr. This just lists the orderings that
+		 * DRM supports.
+		 */
+		DRM_FORMAT_YUYV,
+		DRM_FORMAT_YVYU,
+		DRM_FORMAT_UYVY,
+		DRM_FORMAT_VYUY,
+		/* This uses three planes */
+		DRM_FORMAT_YUV420,
+	};
+
+	ret = drm_simple_display_pipe_init(drm, &priv->pipe,
+					   &tve200_display_funcs,
+					   formats, ARRAY_SIZE(formats),
+					   &priv->connector.connector);
+	if (ret)
+		return ret;
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/tve200/tve200_drm.h b/drivers/gpu/drm/tve200/tve200_drm.h
new file mode 100644
index 000000000000..f00fc47a6bd1
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_drm.h
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+#ifndef _TVE200_DRM_H_
+#define _TVE200_DRM_H_
+
+/* Bits 2-31 are valid physical base addresses */
+#define TVE200_Y_FRAME_BASE_ADDR	0x00
+#define TVE200_U_FRAME_BASE_ADDR	0x04
+#define TVE200_V_FRAME_BASE_ADDR	0x08
+
+#define TVE200_INT_EN			0x0C
+#define TVE200_INT_CLR			0x10
+#define TVE200_INT_STAT			0x14
+#define TVE200_INT_BUS_ERR		BIT(7)
+#define TVE200_INT_V_STATUS		BIT(6) /* vertical blank */
+#define TVE200_INT_V_NEXT_FRAME		BIT(5)
+#define TVE200_INT_U_NEXT_FRAME		BIT(4)
+#define TVE200_INT_Y_NEXT_FRAME		BIT(3)
+#define TVE200_INT_V_FIFO_UNDERRUN	BIT(2)
+#define TVE200_INT_U_FIFO_UNDERRUN	BIT(1)
+#define TVE200_INT_Y_FIFO_UNDERRUN	BIT(0)
+#define TVE200_FIFO_UNDERRUNS		(TVE200_INT_V_FIFO_UNDERRUN | \
+					 TVE200_INT_U_FIFO_UNDERRUN | \
+					 TVE200_INT_Y_FIFO_UNDERRUN)
+
+#define TVE200_CTRL			0x18
+#define TVE200_CTRL_YUV420		BIT(31)
+#define TVE200_CTRL_CSMODE		BIT(30)
+#define TVE200_CTRL_NONINTERLACE	BIT(28) /* 0 = non-interlace CCIR656 */
+#define TVE200_CTRL_TVCLKP		BIT(27) /* Inverted clock phase */
+/* Bits 24..26 define the burst size after arbitration on the bus */
+#define TVE200_CTRL_BURST_4_WORDS	(0 << 24)
+#define TVE200_CTRL_BURST_8_WORDS	(1 << 24)
+#define TVE200_CTRL_BURST_16_WORDS	(2 << 24)
+#define TVE200_CTRL_BURST_32_WORDS	(3 << 24)
+#define TVE200_CTRL_BURST_64_WORDS	(4 << 24)
+#define TVE200_CTRL_BURST_128_WORDS	(5 << 24)
+#define TVE200_CTRL_BURST_256_WORDS	(6 << 24)
+#define TVE200_CTRL_BURST_0_WORDS	(7 << 24) /* ? */
+/*
+ * Bits 16..23 is the retry count*16 before issueing a new AHB transfer
+ * on the AHB bus.
+ */
+#define TVE200_CTRL_RETRYCNT_MASK	GENMASK(23, 16)
+#define TVE200_CTRL_RETRYCNT_16		(1 << 16)
+#define TVE200_CTRL_BBBP		BIT(15) /* 0 = little-endian */
+/* Bits 12..14 define the YCbCr ordering */
+#define TVE200_CTRL_YCBCRODR_CB0Y0CR0Y1	(0 << 12)
+#define TVE200_CTRL_YCBCRODR_Y0CB0Y1CR0	(1 << 12)
+#define TVE200_CTRL_YCBCRODR_CR0Y0CB0Y1	(2 << 12)
+#define TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0	(3 << 12)
+#define TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0	(4 << 12)
+#define TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0	(5 << 12)
+#define TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0	(6 << 12)
+#define TVE200_CTRL_YCBCRODR_Y0CR0Y1CB0	(7 << 12)
+/* Bits 10..11 define the input resolution (framebuffer size) */
+#define TVE200_CTRL_IPRESOL_CIF		(0 << 10)
+#define TVE200_CTRL_IPRESOL_VGA		(1 << 10)
+#define TVE200_CTRL_IPRESOL_D1		(2 << 10)
+#define TVE200_CTRL_NTSC		BIT(9) /* 0 = PAL, 1 = NTSC */
+#define TVE200_CTRL_INTERLACE		BIT(8) /* 1 = interlace, only for D1 */
+#define TVE200_IPDMOD_RGB555		(0 << 6) /* TVE200_CTRL_YUV420 = 0 */
+#define TVE200_IPDMOD_RGB565		(1 << 6)
+#define TVE200_IPDMOD_RGB888		(2 << 6)
+#define TVE200_IPDMOD_YUV420		(2 << 6) /* TVE200_CTRL_YUV420 = 1 */
+#define TVE200_IPDMOD_YUV422		(3 << 6)
+/* Bits 4 & 5 define when to fire the vblank IRQ */
+#define TVE200_VSTSTYPE_VSYNC		(0 << 4) /* start of vsync */
+#define TVE200_VSTSTYPE_VBP		(1 << 4) /* start of v back porch */
+#define TVE200_VSTSTYPE_VAI		(2 << 4) /* start of v active image */
+#define TVE200_VSTSTYPE_VFP		(3 << 4) /* start of v front porch */
+#define TVE200_VSTSTYPE_BITS		(BIT(4) | BIT(5))
+#define TVE200_BGR			BIT(1) /* 0 = RGB, 1 = BGR */
+#define TVE200_TVEEN			BIT(0) /* Enable TVE block */
+
+#define TVE200_CTRL_2			0x1c
+#define TVE200_CTRL_3			0x20
+
+#define TVE200_CTRL_4			0x24
+#define TVE200_CTRL_4_RESET		BIT(0) /* triggers reset of TVE200 */
+
+#include <drm/drm_gem.h>
+#include <drm/drm_simple_kms_helper.h>
+
+struct tve200_drm_connector {
+	struct drm_connector connector;
+	struct drm_panel *panel;
+};
+
+struct tve200_drm_dev_private {
+	struct drm_device *drm;
+
+	struct tve200_drm_connector connector;
+	struct drm_simple_display_pipe pipe;
+	struct drm_fbdev_cma *fbdev;
+
+	void *regs;
+	struct clk *pclk;
+	struct clk *clk;
+};
+
+#define to_tve200_connector(x) \
+	container_of(x, struct tve200_drm_connector, connector)
+
+int tve200_display_init(struct drm_device *dev);
+int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc);
+void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc);
+irqreturn_t tve200_irq(int irq, void *data);
+int tve200_connector_init(struct drm_device *dev);
+int tve200_encoder_init(struct drm_device *dev);
+int tve200_dumb_create(struct drm_file *file_priv,
+		      struct drm_device *dev,
+		      struct drm_mode_create_dumb *args);
+
+#endif /* _TVE200_DRM_H_ */
diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c
new file mode 100644
index 000000000000..9e34147e2617
--- /dev/null
+++ b/drivers/gpu/drm/tve200/tve200_drv.c
@@ -0,0 +1,277 @@
+/*
+ * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
+ * Parts of this file were based on sources as follows:
+ *
+ * Copyright (C) 2006-2008 Intel Corporation
+ * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
+ * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (C) 2011 Texas Instruments
+ * Copyright (C) 2017 Eric Anholt
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms of
+ * such GNU licence.
+ */
+
+/**
+ * DOC: Faraday TV Encoder TVE200 DRM Driver
+ *
+ * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
+ * Controller (TVC) and is found in the Gemini Chipset from Storlink
+ * Semiconductor (later Storm Semiconductor, later Cortina Systems)
+ * but also in the Grain Media GM8180 chipset. On the Gemini the module
+ * is connected to 8 data lines and a single clock line, comprising an
+ * 8-bit BT.656 interface.
+ *
+ * This is a very basic YUV display driver. The datasheet specifies that
+ * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
+ * the hallmark of any TV encoder supporting both PAL and NTSC.
+ *
+ * This driver exposes a standard KMS interface for this TV encoder.
+ */
+
+#include <linux/clk.h>
+#include <linux/dma-buf.h>
+#include <linux/irq.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/shmem_fs.h>
+#include <linux/slab.h>
+#include <linux/version.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_fb_cma_helper.h>
+#include <drm/drm_panel.h>
+
+#include "tve200_drm.h"
+
+#define DRIVER_DESC      "DRM module for Faraday TVE200"
+
+struct drm_mode_config_funcs mode_config_funcs = {
+	.fb_create = drm_fb_cma_create,
+	.atomic_check = drm_atomic_helper_check,
+	.atomic_commit = drm_atomic_helper_commit,
+};
+
+static int tve200_modeset_init(struct drm_device *dev)
+{
+	struct drm_mode_config *mode_config;
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+	int ret = 0;
+
+	drm_mode_config_init(dev);
+	mode_config = &dev->mode_config;
+	mode_config->funcs = &mode_config_funcs;
+	mode_config->min_width = 352;
+	mode_config->max_width = 720;
+	mode_config->min_height = 240;
+	mode_config->max_height = 576;
+
+	ret = tve200_connector_init(dev);
+	if (ret) {
+		dev_err(dev->dev, "Failed to create tve200_drm_connector\n");
+		goto out_config;
+	}
+
+	/*
+	 * Don't actually attach if we didn't find a drm_panel
+	 * attached to us.
+	 */
+	if (!priv->connector.panel) {
+		dev_info(dev->dev,
+			 "deferring due to lack of DRM panel device\n");
+		ret = -EPROBE_DEFER;
+		goto out_config;
+	}
+	dev_info(dev->dev, "attached to panel %s\n",
+		 dev_name(priv->connector.panel->dev));
+
+	ret = tve200_display_init(dev);
+	if (ret) {
+		dev_err(dev->dev, "failed to init display\n");
+		goto out_config;
+	}
+
+	ret = drm_vblank_init(dev, 1);
+	if (ret) {
+		dev_err(dev->dev, "failed to init vblank\n");
+		goto out_config;
+	}
+
+	drm_mode_config_reset(dev);
+
+	/*
+	 * Passing in 16 here will make the RGB656 mode the default
+	 * Passing in 32 will use XRGB8888 mode
+	 */
+	priv->fbdev = drm_fbdev_cma_init(dev, 16,
+					 dev->mode_config.num_connector);
+	drm_kms_helper_poll_init(dev);
+
+	goto finish;
+
+out_config:
+	drm_mode_config_cleanup(dev);
+finish:
+	return ret;
+}
+
+DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
+
+static void tve200_lastclose(struct drm_device *dev)
+{
+	struct tve200_drm_dev_private *priv = dev->dev_private;
+
+	drm_fbdev_cma_restore_mode(priv->fbdev);
+}
+
+static struct drm_driver tve200_drm_driver = {
+	.driver_features =
+		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
+	.lastclose = tve200_lastclose,
+	.ioctls = NULL,
+	.fops = &drm_fops,
+	.name = "tve200",
+	.desc = DRIVER_DESC,
+	.date = "20170703",
+	.major = 1,
+	.minor = 0,
+	.patchlevel = 0,
+	.dumb_create = drm_gem_cma_dumb_create,
+	.dumb_destroy = drm_gem_dumb_destroy,
+	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
+	.gem_free_object = drm_gem_cma_free_object,
+	.gem_vm_ops = &drm_gem_cma_vm_ops,
+
+	.enable_vblank = tve200_enable_vblank,
+	.disable_vblank = tve200_disable_vblank,
+
+	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+	.gem_prime_import = drm_gem_prime_import,
+	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
+	.gem_prime_export = drm_gem_prime_export,
+	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
+};
+
+static int tve200_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tve200_drm_dev_private *priv;
+	struct drm_device *drm;
+	struct resource *res;
+	int irq;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	drm = drm_dev_alloc(&tve200_drm_driver, dev);
+	if (IS_ERR(drm))
+		return PTR_ERR(drm);
+	platform_set_drvdata(pdev, drm);
+	priv->drm = drm;
+	drm->dev_private = priv;
+
+	/* Clock the silicon so we can access the registers */
+	priv->pclk = devm_clk_get(dev, "PCLK");
+	if (IS_ERR(priv->pclk)) {
+		dev_err(dev, "unable to get PCLK\n");
+		ret = PTR_ERR(priv->pclk);
+		goto dev_unref;
+	}
+	ret = clk_prepare_enable(priv->pclk);
+	if (ret) {
+		dev_err(dev, "failed to enable PCLK\n");
+		goto dev_unref;
+	}
+
+	/* This clock is for the pixels (27MHz) */
+	priv->clk = devm_clk_get(dev, "TVE");
+	if (IS_ERR(priv->clk)) {
+		dev_err(dev, "unable to get TVE clock\n");
+		ret = PTR_ERR(priv->clk);
+		goto clk_disable;
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->regs = devm_ioremap_resource(dev, res);
+	if (!priv->regs) {
+		dev_err(dev, "%s failed mmio\n", __func__);
+		ret = -EINVAL;
+		goto dev_unref;
+	}
+
+	irq = platform_get_irq(pdev, 0);
+	if (!irq) {
+		ret = -EINVAL;
+		goto dev_unref;
+	}
+
+	/* turn off interrupts before requesting the irq */
+	writel(0, priv->regs + TVE200_INT_EN);
+
+	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
+	if (ret) {
+		dev_err(dev, "failed to request irq %d\n", ret);
+		return ret;
+	}
+
+	ret = tve200_modeset_init(drm);
+	if (ret)
+		goto dev_unref;
+
+	ret = drm_dev_register(drm, 0);
+	if (ret < 0)
+		goto dev_unref;
+
+	return 0;
+
+clk_disable:
+	clk_disable_unprepare(priv->pclk);
+dev_unref:
+	drm_dev_unref(drm);
+	return ret;
+}
+
+static int tve200_remove(struct platform_device *pdev)
+{
+	struct drm_device *drm = platform_get_drvdata(pdev);
+	struct tve200_drm_dev_private *priv = drm->dev_private;
+
+	drm_dev_unregister(drm);
+	if (priv->fbdev)
+		drm_fbdev_cma_fini(priv->fbdev);
+	drm_mode_config_cleanup(drm);
+	clk_disable_unprepare(priv->pclk);
+	drm_dev_unref(drm);
+
+	return 0;
+}
+
+static const struct of_device_id tve200_of_match[] = {
+	{
+		.compatible = "faraday,tve200",
+	},
+	{},
+};
+
+static struct platform_driver tve200_driver = {
+	.driver = {
+		.name           = "tve200",
+		.of_match_table = of_match_ptr(tve200_of_match),
+	},
+	.probe = tve200_probe,
+	.remove = tve200_remove,
+};
+module_platform_driver(tve200_driver);
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
+MODULE_LICENSE("GPL");
-- 
2.13.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 3/4] ARM: dts: Add TVE200 to the Gemini SoC DTSI
  2017-08-13 15:11 ` Linus Walleij
@ 2017-08-13 15:11   ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: linux-arm-kernel

The Faraday TVE200 is present in the Gemini SoC, sometimes
under the name "TVC". Add it to the SoC DTSI file along with
its resources.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
DRM developers: I will merge this patch through the ARM SoC
tree. It is only included in this series for completion.
---
 arch/arm/boot/dts/gemini.dtsi | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/arch/arm/boot/dts/gemini.dtsi b/arch/arm/boot/dts/gemini.dtsi
index c68e8d430234..f4957545e558 100644
--- a/arch/arm/boot/dts/gemini.dtsi
+++ b/arch/arm/boot/dts/gemini.dtsi
@@ -141,6 +141,12 @@
 						groups = "idegrp";
 					};
 				};
+				tvc_default_pins: pinctrl-tvc {
+					mux {
+						function = "tvc";
+						groups = "tvcgrp";
+					};
+				};
 			};
 		};
 
@@ -346,5 +352,18 @@
 			memcpy-bus-width = <32>;
 			#dma-cells = <2>;
 		};
+
+		tvc at 6a000000 {
+			compatible = "cortina,gemini-tvc", "faraday,tve200";
+			reg = <0x6a000000 0x1000>;
+			interrupts = <13 IRQ_TYPE_EDGE_RISING>;
+			resets = <&syscon GEMINI_RESET_TVC>;
+			clocks = <&syscon GEMINI_CLK_GATE_TVC>,
+				 <&syscon GEMINI_CLK_TVC>;
+			clock-names = "PCLK", "TVE";
+			pinctrl-names = "default";
+			pinctrl-0 = <&tvc_default_pins>;
+			status = "disabled";
+		};
 	};
 };
-- 
2.13.4

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

* [PATCH 3/4] ARM: dts: Add TVE200 to the Gemini SoC DTSI
@ 2017-08-13 15:11   ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: dri-devel, Eric Anholt, Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel

The Faraday TVE200 is present in the Gemini SoC, sometimes
under the name "TVC". Add it to the SoC DTSI file along with
its resources.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
DRM developers: I will merge this patch through the ARM SoC
tree. It is only included in this series for completion.
---
 arch/arm/boot/dts/gemini.dtsi | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/arch/arm/boot/dts/gemini.dtsi b/arch/arm/boot/dts/gemini.dtsi
index c68e8d430234..f4957545e558 100644
--- a/arch/arm/boot/dts/gemini.dtsi
+++ b/arch/arm/boot/dts/gemini.dtsi
@@ -141,6 +141,12 @@
 						groups = "idegrp";
 					};
 				};
+				tvc_default_pins: pinctrl-tvc {
+					mux {
+						function = "tvc";
+						groups = "tvcgrp";
+					};
+				};
 			};
 		};
 
@@ -346,5 +352,18 @@
 			memcpy-bus-width = <32>;
 			#dma-cells = <2>;
 		};
+
+		tvc@6a000000 {
+			compatible = "cortina,gemini-tvc", "faraday,tve200";
+			reg = <0x6a000000 0x1000>;
+			interrupts = <13 IRQ_TYPE_EDGE_RISING>;
+			resets = <&syscon GEMINI_RESET_TVC>;
+			clocks = <&syscon GEMINI_CLK_GATE_TVC>,
+				 <&syscon GEMINI_CLK_TVC>;
+			clock-names = "PCLK", "TVE";
+			pinctrl-names = "default";
+			pinctrl-0 = <&tvc_default_pins>;
+			status = "disabled";
+		};
 	};
 };
-- 
2.13.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 4/4] ARM: dts: Add TVE/TVC and ILI9322 panel to DIR-685
  2017-08-13 15:11 ` Linus Walleij
@ 2017-08-13 15:11   ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: linux-arm-kernel

This adds the TVE200/TVC TV-encoder and the Ilitek ILI9322 panel
to the DIR-685 device tree.

This brings graphics to this funky router and it is possible to
even run a console on its tiny screen.

Incidentally this requires us to disable the access to the
parallel (NOR) flash, as the communication pins to the panel
are shared with the flash memory.

To access the flash, a separate kernel with the panel disabled
and the flash enabled should be booted. The pin control selecting
whether to use the lines cannot be altered at runtime due to
hardware constraints.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
DRM developers: I will merge this patch through the ARM SoC
tree. It is only included in this series for completion.
---
 arch/arm/boot/dts/gemini-dlink-dir-685.dts | 77 +++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/gemini-dlink-dir-685.dts b/arch/arm/boot/dts/gemini-dlink-dir-685.dts
index 094a29624b8d..6c8d3d839ae6 100644
--- a/arch/arm/boot/dts/gemini-dlink-dir-685.dts
+++ b/arch/arm/boot/dts/gemini-dlink-dir-685.dts
@@ -46,6 +46,59 @@
 		};
 	};
 
+	vdisp: regulator {
+		compatible = "regulator-fixed";
+		regulator-name = "display-power";
+		regulator-min-microvolt = <3600000>;
+		regulator-max-microvolt = <3600000>;
+		/* Collides with LCD E */
+		gpio = <&gpio0 16 GPIO_ACTIVE_HIGH>;
+		enable-active-high;
+	};
+
+	spi {
+		compatible = "spi-gpio";
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		/* Collides with IDE pins, that's cool (we do not use them) */
+		gpio-sck = <&gpio1 5 GPIO_ACTIVE_HIGH>;
+		gpio-miso = <&gpio1 8 GPIO_ACTIVE_HIGH>;
+		gpio-mosi = <&gpio1 7 GPIO_ACTIVE_HIGH>;
+		/* Collides with pflash CE1, not so cool */
+		cs-gpios = <&gpio0 20 GPIO_ACTIVE_HIGH>;
+		num-chipselects = <1>;
+
+		panel: display at 0 {
+			compatible = "ilitek,ili9322";
+			reg = <0>;
+			/* 50 ns min period = 20 MHz */
+			spi-max-frequency = <20000000>;
+			spi-cpol; /* Clock active low */
+
+			/* Panel LM918A01-1A SY-B4-091116-E0199 */
+			width-mm = <65>;
+			height-mm = <50>;
+			ilitek,entry-mode = <11>;
+			ilitek,vreg1out-microvolt = <4600>;
+			ilitek,vcom-high-percent = <91>;
+			ilitek,vcom-amplitude-percent = <114>;
+			ilitek,gamma-correction-neg = <0xa>, <0x5>, <0x7>,
+				<0x7>, <0x7>, <0x5>, <0x1>, <0x6>;
+			ilitek,gamma-correction-pos = <0x7>, <0x7>, <0x3>,
+				<0x2>, <0x3>, <0x5>, <0x7>, <0x2>;
+			vcc-supply = <&vdisp>;
+			iovcc-supply = <&vdisp>;
+			vci-supply = <&vdisp>;
+
+			port {
+				panel_in: endpoint {
+					remote-endpoint = <&display_out>;
+				};
+			};
+		};
+	};
+
 	leds {
 		compatible = "gpio-leds";
 		led at 7 {
@@ -114,7 +167,16 @@
 
 	soc {
 		flash at 30000000 {
-			status = "okay";
+			/*
+			 * Flash access is by default disabled, because it
+			 * collides with the Chip Enable signal for the display
+			 * panel, that reuse the parallel flash Chip Select 1
+			 * (CS1). Enabling flash makes graphics stop working.
+			 *
+			 * We might be able to hack around this by letting
+			 * GPIO poke around in the flash controller registers.
+			 */
+			/* status = "okay"; */
 			/* 32MB of flash */
 			reg = <0x30000000 0x02000000>;
 
@@ -241,5 +303,18 @@
 		ata at 63000000 {
 			status = "okay";
 		};
+
+		tvc at 6a000000 {
+			status = "okay";
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			port at 0 {
+				reg = <0>;
+				display_out: endpoint {
+					remote-endpoint = <&panel_in>;
+				};
+			};
+		};
 	};
 };
-- 
2.13.4

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

* [PATCH 4/4] ARM: dts: Add TVE/TVC and ILI9322 panel to DIR-685
@ 2017-08-13 15:11   ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-13 15:11 UTC (permalink / raw)
  To: dri-devel, Eric Anholt, Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel

This adds the TVE200/TVC TV-encoder and the Ilitek ILI9322 panel
to the DIR-685 device tree.

This brings graphics to this funky router and it is possible to
even run a console on its tiny screen.

Incidentally this requires us to disable the access to the
parallel (NOR) flash, as the communication pins to the panel
are shared with the flash memory.

To access the flash, a separate kernel with the panel disabled
and the flash enabled should be booted. The pin control selecting
whether to use the lines cannot be altered at runtime due to
hardware constraints.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
DRM developers: I will merge this patch through the ARM SoC
tree. It is only included in this series for completion.
---
 arch/arm/boot/dts/gemini-dlink-dir-685.dts | 77 +++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/gemini-dlink-dir-685.dts b/arch/arm/boot/dts/gemini-dlink-dir-685.dts
index 094a29624b8d..6c8d3d839ae6 100644
--- a/arch/arm/boot/dts/gemini-dlink-dir-685.dts
+++ b/arch/arm/boot/dts/gemini-dlink-dir-685.dts
@@ -46,6 +46,59 @@
 		};
 	};
 
+	vdisp: regulator {
+		compatible = "regulator-fixed";
+		regulator-name = "display-power";
+		regulator-min-microvolt = <3600000>;
+		regulator-max-microvolt = <3600000>;
+		/* Collides with LCD E */
+		gpio = <&gpio0 16 GPIO_ACTIVE_HIGH>;
+		enable-active-high;
+	};
+
+	spi {
+		compatible = "spi-gpio";
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		/* Collides with IDE pins, that's cool (we do not use them) */
+		gpio-sck = <&gpio1 5 GPIO_ACTIVE_HIGH>;
+		gpio-miso = <&gpio1 8 GPIO_ACTIVE_HIGH>;
+		gpio-mosi = <&gpio1 7 GPIO_ACTIVE_HIGH>;
+		/* Collides with pflash CE1, not so cool */
+		cs-gpios = <&gpio0 20 GPIO_ACTIVE_HIGH>;
+		num-chipselects = <1>;
+
+		panel: display@0 {
+			compatible = "ilitek,ili9322";
+			reg = <0>;
+			/* 50 ns min period = 20 MHz */
+			spi-max-frequency = <20000000>;
+			spi-cpol; /* Clock active low */
+
+			/* Panel LM918A01-1A SY-B4-091116-E0199 */
+			width-mm = <65>;
+			height-mm = <50>;
+			ilitek,entry-mode = <11>;
+			ilitek,vreg1out-microvolt = <4600>;
+			ilitek,vcom-high-percent = <91>;
+			ilitek,vcom-amplitude-percent = <114>;
+			ilitek,gamma-correction-neg = <0xa>, <0x5>, <0x7>,
+				<0x7>, <0x7>, <0x5>, <0x1>, <0x6>;
+			ilitek,gamma-correction-pos = <0x7>, <0x7>, <0x3>,
+				<0x2>, <0x3>, <0x5>, <0x7>, <0x2>;
+			vcc-supply = <&vdisp>;
+			iovcc-supply = <&vdisp>;
+			vci-supply = <&vdisp>;
+
+			port {
+				panel_in: endpoint {
+					remote-endpoint = <&display_out>;
+				};
+			};
+		};
+	};
+
 	leds {
 		compatible = "gpio-leds";
 		led@7 {
@@ -114,7 +167,16 @@
 
 	soc {
 		flash@30000000 {
-			status = "okay";
+			/*
+			 * Flash access is by default disabled, because it
+			 * collides with the Chip Enable signal for the display
+			 * panel, that reuse the parallel flash Chip Select 1
+			 * (CS1). Enabling flash makes graphics stop working.
+			 *
+			 * We might be able to hack around this by letting
+			 * GPIO poke around in the flash controller registers.
+			 */
+			/* status = "okay"; */
 			/* 32MB of flash */
 			reg = <0x30000000 0x02000000>;
 
@@ -241,5 +303,18 @@
 		ata@63000000 {
 			status = "okay";
 		};
+
+		tvc@6a000000 {
+			status = "okay";
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			port@0 {
+				reg = <0>;
+				display_out: endpoint {
+					remote-endpoint = <&panel_in>;
+				};
+			};
+		};
 	};
 };
-- 
2.13.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-13 15:11   ` Linus Walleij
@ 2017-08-14 14:20     ` Daniel Vetter
  -1 siblings, 0 replies; 24+ messages in thread
From: Daniel Vetter @ 2017-08-14 14:20 UTC (permalink / raw)
  To: linux-arm-kernel

On Sun, Aug 13, 2017 at 05:11:30PM +0200, Linus Walleij wrote:
> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
> 
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
> 
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
> 
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.
> 
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  Documentation/gpu/index.rst               |   1 +
>  Documentation/gpu/tve200.rst              |   6 +
>  MAINTAINERS                               |   6 +
>  drivers/gpu/drm/Kconfig                   |   2 +
>  drivers/gpu/drm/Makefile                  |   1 +
>  drivers/gpu/drm/tve200/Kconfig            |  15 ++
>  drivers/gpu/drm/tve200/Makefile           |   5 +
>  drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
>  drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
>  drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
>  drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
>  11 files changed, 914 insertions(+)
>  create mode 100644 Documentation/gpu/tve200.rst
>  create mode 100644 drivers/gpu/drm/tve200/Kconfig
>  create mode 100644 drivers/gpu/drm/tve200/Makefile
>  create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c
> 
> diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
> index 35d673bf9b56..c36586dad29d 100644
> --- a/Documentation/gpu/index.rst
> +++ b/Documentation/gpu/index.rst
> @@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
>     pl111
>     tegra
>     tinydrm
> +   tve200
>     vc4
>     vga-switcheroo
>     vgaarbiter
> diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
> new file mode 100644
> index 000000000000..69b17b324e12
> --- /dev/null
> +++ b/Documentation/gpu/tve200.rst
> @@ -0,0 +1,6 @@
> +==================================
> + drm/tve200 Faraday TV Encoder 200
> +==================================
> +
> +.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
> +   :doc: Faraday TV Encoder 200
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e87cba115ea4..c3d42d68253a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
>  S:	Maintained
>  F:	drivers/gpu/drm/bochs/
>  
> +DRM DRIVER FOR FARADAY TVE200 TV ENCODER
> +M:	Linus Walleij <linus.walleij@linaro.org>
> +T:	git git://anongit.freedesktop.org/drm/drm-misc
> +S:	Maintained
> +F:	drivers/gpu/drm/tve200/
> +
>  DRM DRIVER FOR INTEL I810 VIDEO CARDS
>  S:	Orphan / Obsolete
>  F:	drivers/gpu/drm/i810/
> diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
> index 83cb2a88c204..c5e1a8409285 100644
> --- a/drivers/gpu/drm/Kconfig
> +++ b/drivers/gpu/drm/Kconfig
> @@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
>  
>  source "drivers/gpu/drm/pl111/Kconfig"
>  
> +source "drivers/gpu/drm/tve200/Kconfig"
> +
>  # Keep legacy drivers last
>  
>  menuconfig DRM_LEGACY
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index 24a066e1841c..cc81813e2238 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
>  obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
>  obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
>  obj-$(CONFIG_DRM_PL111) += pl111/
> +obj-$(CONFIG_DRM_TVE200) += tve200/
> diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
> new file mode 100644
> index 000000000000..21d9841ddb88
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Kconfig
> @@ -0,0 +1,15 @@
> +config DRM_TVE200
> +	tristate "DRM Support for Faraday TV Encoder TVE200"
> +	depends on DRM
> +	depends on CMA
> +	depends on ARM || COMPILE_TEST
> +	depends on OF
> +	select DRM_PANEL
> +	select DRM_KMS_HELPER
> +	select DRM_KMS_CMA_HELPER
> +	select DRM_GEM_CMA_HELPER
> +	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
> +	help
> +	  Choose this option for DRM support for the Faraday TV Encoder
> +	  TVE200 Controller.
> +	  If M is selected the module will be called tve200_drm.
> diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
> new file mode 100644
> index 000000000000..a9dba54f7ee5
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Makefile
> @@ -0,0 +1,5 @@
> +tve200_drm-y +=	tve200_connector.o \
> +		tve200_display.o \
> +		tve200_drv.o
> +
> +obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
> diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
> new file mode 100644
> index 000000000000..93e99156d375
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_connector.c
> @@ -0,0 +1,126 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * tve200_drm_connector.c
> + * Implementation of the connector functions for the Faraday TV Encoder
> + */
> +#include <linux/version.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/dma-buf.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_of.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +static void tve200_connector_destroy(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (tve200con->panel)
> +		drm_panel_detach(tve200con->panel);
> +
> +	drm_connector_unregister(connector);
> +	drm_connector_cleanup(connector);
> +}
> +
> +static enum drm_connector_status tve200_connector_detect(struct drm_connector
> +							*connector, bool force)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	return (tve200con->panel ?
> +		connector_status_connected :
> +		connector_status_disconnected);
> +}
> +
> +static int tve200_connector_helper_get_modes(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (!tve200con->panel)
> +		return 0;
> +
> +	return drm_panel_get_modes(tve200con->panel);
> +}
> +
> +static const struct drm_connector_funcs connector_funcs = {
> +	.fill_modes = drm_helper_probe_single_connector_modes,
> +	.destroy = tve200_connector_destroy,
> +	.detect = tve200_connector_detect,
> +	.dpms = drm_atomic_helper_connector_dpms,
> +	.reset = drm_atomic_helper_connector_reset,
> +	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
> +	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
> +};
> +
> +static const struct drm_connector_helper_funcs connector_helper_funcs = {
> +	.get_modes = tve200_connector_helper_get_modes,
> +};

The new "wrap panel as a bridge endpoint" helper we have in
drm/bridge/panel.c is meant to remove all the need for the above
boilerplate. Even comes with devm_ built-in :-)

Please check it out.

You're using the simple helpers, but I think they should fully mesh with
the bridge stuff.

Otherwise looks all neat and tidy.

Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>

Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
happily throw commit rights at every driver submission (and honestly
expect that, since it helps tremendously with balance maintainer loads for
the oddball trivial patch).

Thanks, Daniel

> +
> +/*
> + * Walks the OF graph to find the panel node and then asks DRM to look
> + * up the panel.
> + */
> +static struct drm_panel *tve200_get_panel(struct device *dev)
> +{
> +	struct device_node *endpoint, *panel_node;
> +	struct device_node *np = dev->of_node;
> +	struct drm_panel *panel;
> +
> +	endpoint = of_graph_get_next_endpoint(np, NULL);
> +	if (!endpoint) {
> +		dev_err(dev, "no endpoint to fetch panel\n");
> +		return NULL;
> +	}
> +
> +	/* Don't proceed if we have an endpoint but no panel_node tied to it */
> +	panel_node = of_graph_get_remote_port_parent(endpoint);
> +	of_node_put(endpoint);
> +	if (!panel_node) {
> +		dev_err(dev, "no valid panel node\n");
> +		return NULL;
> +	}
> +
> +	panel = of_drm_find_panel(panel_node);
> +	of_node_put(panel_node);
> +
> +	return panel;
> +}
> +
> +int tve200_connector_init(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	struct tve200_drm_connector *tve200con = &priv->connector;
> +	struct drm_connector *connector = &tve200con->connector;
> +
> +	drm_connector_init(dev, connector, &connector_funcs,
> +			   DRM_MODE_CONNECTOR_DPI);
> +	drm_connector_helper_add(connector, &connector_helper_funcs);
> +
> +	tve200con->panel = tve200_get_panel(dev->dev);
> +	if (tve200con->panel)
> +		drm_panel_attach(tve200con->panel, connector);
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
> new file mode 100644
> index 000000000000..027553aacb33
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_display.c
> @@ -0,0 +1,346 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +#include <linux/clk.h>
> +#include <linux/version.h>
> +#include <linux/dma-buf.h>
> +#include <linux/of_graph.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_panel.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +
> +#include "tve200_drm.h"
> +
> +irqreturn_t tve200_irq(int irq, void *data)
> +{
> +	struct tve200_drm_dev_private *priv = data;
> +	u32 stat;
> +	u32 val;
> +
> +	stat = readl(priv->regs + TVE200_INT_STAT);
> +
> +	if (!stat)
> +		return IRQ_NONE;
> +
> +	/*
> +	 * Vblank IRQ
> +	 *
> +	 * The hardware is a bit tilted: the line stays high after clearing
> +	 * the vblank IRQ, fireing many more interrupts. We counter this
> +	 * by toggling the IRQ back and forth from fireing at vblank and
> +	 * fireing at start of active image, which works around the problem
> +	 * since those occur strictly in sequence, and we get two IRQs for each
> +	 * frame, one at start of Vblank (that we make call into the CRTC) and
> +	 * another one at the start of the image (that we discard).
> +	 */
> +	if (stat & TVE200_INT_V_STATUS) {
> +		val = readl(priv->regs + TVE200_CTRL);
> +		/* We have an actual start of vsync */
> +		if (!(val & TVE200_VSTSTYPE_BITS)) {
> +			drm_crtc_handle_vblank(&priv->pipe.crtc);
> +			/* Toggle trigger to start of active image */
> +			val |= TVE200_VSTSTYPE_VAI;
> +		} else {
> +			/* Toggle trigger back to start of vsync */
> +			val &= ~TVE200_VSTSTYPE_BITS;
> +		}
> +		writel(val, priv->regs + TVE200_CTRL);
> +	} else
> +		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
> +
> +	/* Clear the interrupt once done */
> +	writel(stat, priv->regs + TVE200_INT_CLR);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int tve200_display_check(struct drm_simple_display_pipe *pipe,
> +			       struct drm_plane_state *pstate,
> +			       struct drm_crtc_state *cstate)
> +{
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
> +	struct drm_framebuffer *fb = pstate->fb;
> +	struct drm_connector *connector = pipe->connector;
> +	struct drm_device *drm = connector->dev;
> +
> +	/*
> +	 * We support these specific resolutions and nothing else.
> +	 */
> +	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
> +	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
> +	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
> +		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
> +			mode->hdisplay, mode->vdisplay);
> +		return -EINVAL;
> +	}
> +
> +	if (fb) {
> +		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
> +
> +		/* FB base address must be dword aligned. */
> +		if (offset & 3) {
> +			dev_err(drm->dev, "FB not 32-bit aligned\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * There's no pitch register, the mode's hdisplay
> +		 * controls this.
> +		 */
> +		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
> +			dev_err(drm->dev, "can't handle pitches\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * We can't change the FB format in a flicker-free
> +		 * manner (and only update it during CRTC enable).
> +		 */
> +		if (old_fb && old_fb->format != fb->format)
> +			cstate->mode_changed = true;
> +	}
> +
> +	return 0;
> +}
> +
> +static void tve200_display_enable(struct drm_simple_display_pipe *pipe,
> +				 struct drm_crtc_state *cstate)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_plane *plane = &pipe->plane;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *fb = plane->state->fb;
> +	struct drm_connector *connector = &priv->connector.connector;
> +	u32 format = fb->format->format;
> +	u32 ctrl1 = 0;
> +
> +	clk_prepare_enable(priv->clk);
> +
> +	/* Function 1 */
> +	ctrl1 |= TVE200_CTRL_CSMODE;
> +	/* Interlace mode for CCIR656: parameterize? */
> +	ctrl1 |= TVE200_CTRL_NONINTERLACE;
> +	/* 32 words per burst */
> +	ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
> +	/* 16 retries */
> +	ctrl1 |= TVE200_CTRL_RETRYCNT_16;
> +	/* NTSC mode: parametrize? */
> +	ctrl1 |= TVE200_CTRL_NTSC;
> +
> +	/* Vsync IRQ at start of Vsync at first */
> +	ctrl1 |= TVE200_VSTSTYPE_VSYNC;
> +
> +	if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
> +		ctrl1 |= TVE200_CTRL_TVCLKP;
> +
> +	if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
> +	    (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
> +		ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
> +		dev_info(drm->dev, "CIF mode\n");
> +	} else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
> +		ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
> +		dev_info(drm->dev, "VGA mode\n");
> +	} else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
> +		   (mode->hdisplay == 720 && mode->vdisplay == 576)) {
> +		ctrl1 |= TVE200_CTRL_IPRESOL_D1;
> +		dev_info(drm->dev, "D1 mode\n");
> +	}
> +
> +	if (format & DRM_FORMAT_BIG_ENDIAN) {
> +		ctrl1 |= TVE200_CTRL_BBBP;
> +		format &= ~DRM_FORMAT_BIG_ENDIAN;
> +	}
> +
> +	switch (format) {
> +	case DRM_FORMAT_XRGB8888:
> +		ctrl1 |= TVE200_IPDMOD_RGB888;
> +		break;
> +	case DRM_FORMAT_RGB565:
> +		ctrl1 |= TVE200_IPDMOD_RGB565;
> +		break;
> +	case DRM_FORMAT_XRGB1555:
> +		ctrl1 |= TVE200_IPDMOD_RGB555;
> +		break;
> +	case DRM_FORMAT_XBGR8888:
> +		ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_BGR565:
> +		ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_XBGR1555:
> +		ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_YUYV:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
> +		break;
> +	case DRM_FORMAT_YVYU:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
> +		break;
> +	case DRM_FORMAT_UYVY:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
> +		break;
> +	case DRM_FORMAT_VYUY:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
> +		break;
> +	case DRM_FORMAT_YUV420:
> +		ctrl1 |= TVE200_CTRL_YUV420;
> +		ctrl1 |= TVE200_IPDMOD_YUV420;
> +		break;
> +	default:
> +		dev_err(drm->dev, "Unknown FB format 0x%08x\n",
> +			fb->format->format);
> +		break;
> +	}
> +
> +	ctrl1 |= TVE200_TVEEN;
> +
> +	drm_panel_prepare(priv->connector.panel);
> +
> +	/* Turn it on */
> +	writel(ctrl1, priv->regs + TVE200_CTRL);
> +
> +	drm_panel_enable(priv->connector.panel);
> +
> +	drm_crtc_vblank_on(crtc);
> +}
> +
> +void tve200_display_disable(struct drm_simple_display_pipe *pipe)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_crtc_vblank_off(crtc);
> +
> +	drm_panel_disable(priv->connector.panel);
> +
> +	/* Disable and Power Down */
> +	writel(0, priv->regs + TVE200_CTRL);
> +
> +	drm_panel_unprepare(priv->connector.panel);
> +
> +	clk_disable_unprepare(priv->clk);
> +}
> +
> +static void tve200_display_update(struct drm_simple_display_pipe *pipe,
> +				 struct drm_plane_state *old_pstate)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	struct drm_pending_vblank_event *event = crtc->state->event;
> +	struct drm_plane *plane = &pipe->plane;
> +	struct drm_plane_state *pstate = plane->state;
> +	struct drm_framebuffer *fb = pstate->fb;
> +
> +	if (fb) {
> +		/* For RGB, the Y component is used as base address */
> +		writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
> +		       priv->regs + TVE200_Y_FRAME_BASE_ADDR);
> +
> +		/* For three plane YUV we need two more addresses */
> +		if (fb->format->format == DRM_FORMAT_YUV420) {
> +			writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
> +			       priv->regs + TVE200_U_FRAME_BASE_ADDR);
> +			writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
> +			       priv->regs + TVE200_V_FRAME_BASE_ADDR);
> +		}
> +	}
> +
> +	if (event) {
> +		crtc->state->event = NULL;
> +
> +		spin_lock_irq(&crtc->dev->event_lock);
> +		if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
> +			drm_crtc_arm_vblank_event(crtc, event);
> +		else
> +			drm_crtc_send_vblank_event(crtc, event);
> +		spin_unlock_irq(&crtc->dev->event_lock);
> +	}
> +}
> +
> +int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
> +	return 0;
> +}
> +
> +void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	writel(0, priv->regs + TVE200_INT_EN);
> +}
> +
> +static int tve200_display_prepare_fb(struct drm_simple_display_pipe *pipe,
> +				    struct drm_plane_state *plane_state)
> +{
> +	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
> +}
> +
> +const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
> +	.check = tve200_display_check,
> +	.enable = tve200_display_enable,
> +	.disable = tve200_display_disable,
> +	.update = tve200_display_update,
> +	.prepare_fb = tve200_display_prepare_fb,
> +};
> +
> +int tve200_display_init(struct drm_device *drm)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	int ret;
> +	static const u32 formats[] = {
> +		DRM_FORMAT_XRGB8888,
> +		DRM_FORMAT_XBGR8888,
> +		DRM_FORMAT_RGB565,
> +		DRM_FORMAT_BGR565,
> +		DRM_FORMAT_XRGB1555,
> +		DRM_FORMAT_XBGR1555,
> +		/*
> +		 * The controller actually supports any YCbCr ordering,
> +		 * for packed YCbCr. This just lists the orderings that
> +		 * DRM supports.
> +		 */
> +		DRM_FORMAT_YUYV,
> +		DRM_FORMAT_YVYU,
> +		DRM_FORMAT_UYVY,
> +		DRM_FORMAT_VYUY,
> +		/* This uses three planes */
> +		DRM_FORMAT_YUV420,
> +	};
> +
> +	ret = drm_simple_display_pipe_init(drm, &priv->pipe,
> +					   &tve200_display_funcs,
> +					   formats, ARRAY_SIZE(formats),
> +					   &priv->connector.connector);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_drm.h b/drivers/gpu/drm/tve200/tve200_drm.h
> new file mode 100644
> index 000000000000..f00fc47a6bd1
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_drm.h
> @@ -0,0 +1,129 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +#ifndef _TVE200_DRM_H_
> +#define _TVE200_DRM_H_
> +
> +/* Bits 2-31 are valid physical base addresses */
> +#define TVE200_Y_FRAME_BASE_ADDR	0x00
> +#define TVE200_U_FRAME_BASE_ADDR	0x04
> +#define TVE200_V_FRAME_BASE_ADDR	0x08
> +
> +#define TVE200_INT_EN			0x0C
> +#define TVE200_INT_CLR			0x10
> +#define TVE200_INT_STAT			0x14
> +#define TVE200_INT_BUS_ERR		BIT(7)
> +#define TVE200_INT_V_STATUS		BIT(6) /* vertical blank */
> +#define TVE200_INT_V_NEXT_FRAME		BIT(5)
> +#define TVE200_INT_U_NEXT_FRAME		BIT(4)
> +#define TVE200_INT_Y_NEXT_FRAME		BIT(3)
> +#define TVE200_INT_V_FIFO_UNDERRUN	BIT(2)
> +#define TVE200_INT_U_FIFO_UNDERRUN	BIT(1)
> +#define TVE200_INT_Y_FIFO_UNDERRUN	BIT(0)
> +#define TVE200_FIFO_UNDERRUNS		(TVE200_INT_V_FIFO_UNDERRUN | \
> +					 TVE200_INT_U_FIFO_UNDERRUN | \
> +					 TVE200_INT_Y_FIFO_UNDERRUN)
> +
> +#define TVE200_CTRL			0x18
> +#define TVE200_CTRL_YUV420		BIT(31)
> +#define TVE200_CTRL_CSMODE		BIT(30)
> +#define TVE200_CTRL_NONINTERLACE	BIT(28) /* 0 = non-interlace CCIR656 */
> +#define TVE200_CTRL_TVCLKP		BIT(27) /* Inverted clock phase */
> +/* Bits 24..26 define the burst size after arbitration on the bus */
> +#define TVE200_CTRL_BURST_4_WORDS	(0 << 24)
> +#define TVE200_CTRL_BURST_8_WORDS	(1 << 24)
> +#define TVE200_CTRL_BURST_16_WORDS	(2 << 24)
> +#define TVE200_CTRL_BURST_32_WORDS	(3 << 24)
> +#define TVE200_CTRL_BURST_64_WORDS	(4 << 24)
> +#define TVE200_CTRL_BURST_128_WORDS	(5 << 24)
> +#define TVE200_CTRL_BURST_256_WORDS	(6 << 24)
> +#define TVE200_CTRL_BURST_0_WORDS	(7 << 24) /* ? */
> +/*
> + * Bits 16..23 is the retry count*16 before issueing a new AHB transfer
> + * on the AHB bus.
> + */
> +#define TVE200_CTRL_RETRYCNT_MASK	GENMASK(23, 16)
> +#define TVE200_CTRL_RETRYCNT_16		(1 << 16)
> +#define TVE200_CTRL_BBBP		BIT(15) /* 0 = little-endian */
> +/* Bits 12..14 define the YCbCr ordering */
> +#define TVE200_CTRL_YCBCRODR_CB0Y0CR0Y1	(0 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y0CB0Y1CR0	(1 << 12)
> +#define TVE200_CTRL_YCBCRODR_CR0Y0CB0Y1	(2 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0	(3 << 12)
> +#define TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0	(4 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0	(5 << 12)
> +#define TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0	(6 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y0CR0Y1CB0	(7 << 12)
> +/* Bits 10..11 define the input resolution (framebuffer size) */
> +#define TVE200_CTRL_IPRESOL_CIF		(0 << 10)
> +#define TVE200_CTRL_IPRESOL_VGA		(1 << 10)
> +#define TVE200_CTRL_IPRESOL_D1		(2 << 10)
> +#define TVE200_CTRL_NTSC		BIT(9) /* 0 = PAL, 1 = NTSC */
> +#define TVE200_CTRL_INTERLACE		BIT(8) /* 1 = interlace, only for D1 */
> +#define TVE200_IPDMOD_RGB555		(0 << 6) /* TVE200_CTRL_YUV420 = 0 */
> +#define TVE200_IPDMOD_RGB565		(1 << 6)
> +#define TVE200_IPDMOD_RGB888		(2 << 6)
> +#define TVE200_IPDMOD_YUV420		(2 << 6) /* TVE200_CTRL_YUV420 = 1 */
> +#define TVE200_IPDMOD_YUV422		(3 << 6)
> +/* Bits 4 & 5 define when to fire the vblank IRQ */
> +#define TVE200_VSTSTYPE_VSYNC		(0 << 4) /* start of vsync */
> +#define TVE200_VSTSTYPE_VBP		(1 << 4) /* start of v back porch */
> +#define TVE200_VSTSTYPE_VAI		(2 << 4) /* start of v active image */
> +#define TVE200_VSTSTYPE_VFP		(3 << 4) /* start of v front porch */
> +#define TVE200_VSTSTYPE_BITS		(BIT(4) | BIT(5))
> +#define TVE200_BGR			BIT(1) /* 0 = RGB, 1 = BGR */
> +#define TVE200_TVEEN			BIT(0) /* Enable TVE block */
> +
> +#define TVE200_CTRL_2			0x1c
> +#define TVE200_CTRL_3			0x20
> +
> +#define TVE200_CTRL_4			0x24
> +#define TVE200_CTRL_4_RESET		BIT(0) /* triggers reset of TVE200 */
> +
> +#include <drm/drm_gem.h>
> +#include <drm/drm_simple_kms_helper.h>
> +
> +struct tve200_drm_connector {
> +	struct drm_connector connector;
> +	struct drm_panel *panel;
> +};
> +
> +struct tve200_drm_dev_private {
> +	struct drm_device *drm;
> +
> +	struct tve200_drm_connector connector;
> +	struct drm_simple_display_pipe pipe;
> +	struct drm_fbdev_cma *fbdev;
> +
> +	void *regs;
> +	struct clk *pclk;
> +	struct clk *clk;
> +};
> +
> +#define to_tve200_connector(x) \
> +	container_of(x, struct tve200_drm_connector, connector)
> +
> +int tve200_display_init(struct drm_device *dev);
> +int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc);
> +void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc);
> +irqreturn_t tve200_irq(int irq, void *data);
> +int tve200_connector_init(struct drm_device *dev);
> +int tve200_encoder_init(struct drm_device *dev);
> +int tve200_dumb_create(struct drm_file *file_priv,
> +		      struct drm_device *dev,
> +		      struct drm_mode_create_dumb *args);
> +
> +#endif /* _TVE200_DRM_H_ */
> diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c
> new file mode 100644
> index 000000000000..9e34147e2617
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_drv.c
> @@ -0,0 +1,277 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * DOC: Faraday TV Encoder TVE200 DRM Driver
> + *
> + * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
> + * Controller (TVC) and is found in the Gemini Chipset from Storlink
> + * Semiconductor (later Storm Semiconductor, later Cortina Systems)
> + * but also in the Grain Media GM8180 chipset. On the Gemini the module
> + * is connected to 8 data lines and a single clock line, comprising an
> + * 8-bit BT.656 interface.
> + *
> + * This is a very basic YUV display driver. The datasheet specifies that
> + * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
> + * the hallmark of any TV encoder supporting both PAL and NTSC.
> + *
> + * This driver exposes a standard KMS interface for this TV encoder.
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/dma-buf.h>
> +#include <linux/irq.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/slab.h>
> +#include <linux/version.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +#define DRIVER_DESC      "DRM module for Faraday TVE200"
> +
> +struct drm_mode_config_funcs mode_config_funcs = {
> +	.fb_create = drm_fb_cma_create,
> +	.atomic_check = drm_atomic_helper_check,
> +	.atomic_commit = drm_atomic_helper_commit,
> +};
> +
> +static int tve200_modeset_init(struct drm_device *dev)
> +{
> +	struct drm_mode_config *mode_config;
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	int ret = 0;
> +
> +	drm_mode_config_init(dev);
> +	mode_config = &dev->mode_config;
> +	mode_config->funcs = &mode_config_funcs;
> +	mode_config->min_width = 352;
> +	mode_config->max_width = 720;
> +	mode_config->min_height = 240;
> +	mode_config->max_height = 576;
> +
> +	ret = tve200_connector_init(dev);
> +	if (ret) {
> +		dev_err(dev->dev, "Failed to create tve200_drm_connector\n");
> +		goto out_config;
> +	}
> +
> +	/*
> +	 * Don't actually attach if we didn't find a drm_panel
> +	 * attached to us.
> +	 */
> +	if (!priv->connector.panel) {
> +		dev_info(dev->dev,
> +			 "deferring due to lack of DRM panel device\n");
> +		ret = -EPROBE_DEFER;
> +		goto out_config;
> +	}
> +	dev_info(dev->dev, "attached to panel %s\n",
> +		 dev_name(priv->connector.panel->dev));
> +
> +	ret = tve200_display_init(dev);
> +	if (ret) {
> +		dev_err(dev->dev, "failed to init display\n");
> +		goto out_config;
> +	}
> +
> +	ret = drm_vblank_init(dev, 1);
> +	if (ret) {
> +		dev_err(dev->dev, "failed to init vblank\n");
> +		goto out_config;
> +	}
> +
> +	drm_mode_config_reset(dev);
> +
> +	/*
> +	 * Passing in 16 here will make the RGB656 mode the default
> +	 * Passing in 32 will use XRGB8888 mode
> +	 */
> +	priv->fbdev = drm_fbdev_cma_init(dev, 16,
> +					 dev->mode_config.num_connector);
> +	drm_kms_helper_poll_init(dev);
> +
> +	goto finish;
> +
> +out_config:
> +	drm_mode_config_cleanup(dev);
> +finish:
> +	return ret;
> +}
> +
> +DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
> +
> +static void tve200_lastclose(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +
> +	drm_fbdev_cma_restore_mode(priv->fbdev);
> +}
> +
> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = tve200_modeset_init(drm);
> +	if (ret)
> +		goto dev_unref;
> +
> +	ret = drm_dev_register(drm, 0);
> +	if (ret < 0)
> +		goto dev_unref;
> +
> +	return 0;
> +
> +clk_disable:
> +	clk_disable_unprepare(priv->pclk);
> +dev_unref:
> +	drm_dev_unref(drm);
> +	return ret;
> +}
> +
> +static int tve200_remove(struct platform_device *pdev)
> +{
> +	struct drm_device *drm = platform_get_drvdata(pdev);
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_dev_unregister(drm);
> +	if (priv->fbdev)
> +		drm_fbdev_cma_fini(priv->fbdev);
> +	drm_mode_config_cleanup(drm);
> +	clk_disable_unprepare(priv->pclk);
> +	drm_dev_unref(drm);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id tve200_of_match[] = {
> +	{
> +		.compatible = "faraday,tve200",
> +	},
> +	{},
> +};
> +
> +static struct platform_driver tve200_driver = {
> +	.driver = {
> +		.name           = "tve200",
> +		.of_match_table = of_match_ptr(tve200_of_match),
> +	},
> +	.probe = tve200_probe,
> +	.remove = tve200_remove,
> +};
> +module_platform_driver(tve200_driver);
> +
> +MODULE_DESCRIPTION(DRIVER_DESC);
> +MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
> +MODULE_LICENSE("GPL");
> -- 
> 2.13.4
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-14 14:20     ` Daniel Vetter
  0 siblings, 0 replies; 24+ messages in thread
From: Daniel Vetter @ 2017-08-14 14:20 UTC (permalink / raw)
  To: Linus Walleij; +Cc: dri-devel, Daniel Vetter, linux-arm-kernel

On Sun, Aug 13, 2017 at 05:11:30PM +0200, Linus Walleij wrote:
> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
> 
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
> 
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
> 
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.
> 
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  Documentation/gpu/index.rst               |   1 +
>  Documentation/gpu/tve200.rst              |   6 +
>  MAINTAINERS                               |   6 +
>  drivers/gpu/drm/Kconfig                   |   2 +
>  drivers/gpu/drm/Makefile                  |   1 +
>  drivers/gpu/drm/tve200/Kconfig            |  15 ++
>  drivers/gpu/drm/tve200/Makefile           |   5 +
>  drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
>  drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
>  drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
>  drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
>  11 files changed, 914 insertions(+)
>  create mode 100644 Documentation/gpu/tve200.rst
>  create mode 100644 drivers/gpu/drm/tve200/Kconfig
>  create mode 100644 drivers/gpu/drm/tve200/Makefile
>  create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c
> 
> diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
> index 35d673bf9b56..c36586dad29d 100644
> --- a/Documentation/gpu/index.rst
> +++ b/Documentation/gpu/index.rst
> @@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
>     pl111
>     tegra
>     tinydrm
> +   tve200
>     vc4
>     vga-switcheroo
>     vgaarbiter
> diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
> new file mode 100644
> index 000000000000..69b17b324e12
> --- /dev/null
> +++ b/Documentation/gpu/tve200.rst
> @@ -0,0 +1,6 @@
> +==================================
> + drm/tve200 Faraday TV Encoder 200
> +==================================
> +
> +.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
> +   :doc: Faraday TV Encoder 200
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e87cba115ea4..c3d42d68253a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
>  S:	Maintained
>  F:	drivers/gpu/drm/bochs/
>  
> +DRM DRIVER FOR FARADAY TVE200 TV ENCODER
> +M:	Linus Walleij <linus.walleij@linaro.org>
> +T:	git git://anongit.freedesktop.org/drm/drm-misc
> +S:	Maintained
> +F:	drivers/gpu/drm/tve200/
> +
>  DRM DRIVER FOR INTEL I810 VIDEO CARDS
>  S:	Orphan / Obsolete
>  F:	drivers/gpu/drm/i810/
> diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
> index 83cb2a88c204..c5e1a8409285 100644
> --- a/drivers/gpu/drm/Kconfig
> +++ b/drivers/gpu/drm/Kconfig
> @@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
>  
>  source "drivers/gpu/drm/pl111/Kconfig"
>  
> +source "drivers/gpu/drm/tve200/Kconfig"
> +
>  # Keep legacy drivers last
>  
>  menuconfig DRM_LEGACY
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index 24a066e1841c..cc81813e2238 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
>  obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
>  obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
>  obj-$(CONFIG_DRM_PL111) += pl111/
> +obj-$(CONFIG_DRM_TVE200) += tve200/
> diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
> new file mode 100644
> index 000000000000..21d9841ddb88
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Kconfig
> @@ -0,0 +1,15 @@
> +config DRM_TVE200
> +	tristate "DRM Support for Faraday TV Encoder TVE200"
> +	depends on DRM
> +	depends on CMA
> +	depends on ARM || COMPILE_TEST
> +	depends on OF
> +	select DRM_PANEL
> +	select DRM_KMS_HELPER
> +	select DRM_KMS_CMA_HELPER
> +	select DRM_GEM_CMA_HELPER
> +	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
> +	help
> +	  Choose this option for DRM support for the Faraday TV Encoder
> +	  TVE200 Controller.
> +	  If M is selected the module will be called tve200_drm.
> diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
> new file mode 100644
> index 000000000000..a9dba54f7ee5
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Makefile
> @@ -0,0 +1,5 @@
> +tve200_drm-y +=	tve200_connector.o \
> +		tve200_display.o \
> +		tve200_drv.o
> +
> +obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
> diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
> new file mode 100644
> index 000000000000..93e99156d375
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_connector.c
> @@ -0,0 +1,126 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * tve200_drm_connector.c
> + * Implementation of the connector functions for the Faraday TV Encoder
> + */
> +#include <linux/version.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/dma-buf.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_of.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +static void tve200_connector_destroy(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (tve200con->panel)
> +		drm_panel_detach(tve200con->panel);
> +
> +	drm_connector_unregister(connector);
> +	drm_connector_cleanup(connector);
> +}
> +
> +static enum drm_connector_status tve200_connector_detect(struct drm_connector
> +							*connector, bool force)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	return (tve200con->panel ?
> +		connector_status_connected :
> +		connector_status_disconnected);
> +}
> +
> +static int tve200_connector_helper_get_modes(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (!tve200con->panel)
> +		return 0;
> +
> +	return drm_panel_get_modes(tve200con->panel);
> +}
> +
> +static const struct drm_connector_funcs connector_funcs = {
> +	.fill_modes = drm_helper_probe_single_connector_modes,
> +	.destroy = tve200_connector_destroy,
> +	.detect = tve200_connector_detect,
> +	.dpms = drm_atomic_helper_connector_dpms,
> +	.reset = drm_atomic_helper_connector_reset,
> +	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
> +	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
> +};
> +
> +static const struct drm_connector_helper_funcs connector_helper_funcs = {
> +	.get_modes = tve200_connector_helper_get_modes,
> +};

The new "wrap panel as a bridge endpoint" helper we have in
drm/bridge/panel.c is meant to remove all the need for the above
boilerplate. Even comes with devm_ built-in :-)

Please check it out.

You're using the simple helpers, but I think they should fully mesh with
the bridge stuff.

Otherwise looks all neat and tidy.

Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>

Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
happily throw commit rights at every driver submission (and honestly
expect that, since it helps tremendously with balance maintainer loads for
the oddball trivial patch).

Thanks, Daniel

> +
> +/*
> + * Walks the OF graph to find the panel node and then asks DRM to look
> + * up the panel.
> + */
> +static struct drm_panel *tve200_get_panel(struct device *dev)
> +{
> +	struct device_node *endpoint, *panel_node;
> +	struct device_node *np = dev->of_node;
> +	struct drm_panel *panel;
> +
> +	endpoint = of_graph_get_next_endpoint(np, NULL);
> +	if (!endpoint) {
> +		dev_err(dev, "no endpoint to fetch panel\n");
> +		return NULL;
> +	}
> +
> +	/* Don't proceed if we have an endpoint but no panel_node tied to it */
> +	panel_node = of_graph_get_remote_port_parent(endpoint);
> +	of_node_put(endpoint);
> +	if (!panel_node) {
> +		dev_err(dev, "no valid panel node\n");
> +		return NULL;
> +	}
> +
> +	panel = of_drm_find_panel(panel_node);
> +	of_node_put(panel_node);
> +
> +	return panel;
> +}
> +
> +int tve200_connector_init(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	struct tve200_drm_connector *tve200con = &priv->connector;
> +	struct drm_connector *connector = &tve200con->connector;
> +
> +	drm_connector_init(dev, connector, &connector_funcs,
> +			   DRM_MODE_CONNECTOR_DPI);
> +	drm_connector_helper_add(connector, &connector_helper_funcs);
> +
> +	tve200con->panel = tve200_get_panel(dev->dev);
> +	if (tve200con->panel)
> +		drm_panel_attach(tve200con->panel, connector);
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
> new file mode 100644
> index 000000000000..027553aacb33
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_display.c
> @@ -0,0 +1,346 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +#include <linux/clk.h>
> +#include <linux/version.h>
> +#include <linux/dma-buf.h>
> +#include <linux/of_graph.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_panel.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +
> +#include "tve200_drm.h"
> +
> +irqreturn_t tve200_irq(int irq, void *data)
> +{
> +	struct tve200_drm_dev_private *priv = data;
> +	u32 stat;
> +	u32 val;
> +
> +	stat = readl(priv->regs + TVE200_INT_STAT);
> +
> +	if (!stat)
> +		return IRQ_NONE;
> +
> +	/*
> +	 * Vblank IRQ
> +	 *
> +	 * The hardware is a bit tilted: the line stays high after clearing
> +	 * the vblank IRQ, fireing many more interrupts. We counter this
> +	 * by toggling the IRQ back and forth from fireing at vblank and
> +	 * fireing at start of active image, which works around the problem
> +	 * since those occur strictly in sequence, and we get two IRQs for each
> +	 * frame, one at start of Vblank (that we make call into the CRTC) and
> +	 * another one at the start of the image (that we discard).
> +	 */
> +	if (stat & TVE200_INT_V_STATUS) {
> +		val = readl(priv->regs + TVE200_CTRL);
> +		/* We have an actual start of vsync */
> +		if (!(val & TVE200_VSTSTYPE_BITS)) {
> +			drm_crtc_handle_vblank(&priv->pipe.crtc);
> +			/* Toggle trigger to start of active image */
> +			val |= TVE200_VSTSTYPE_VAI;
> +		} else {
> +			/* Toggle trigger back to start of vsync */
> +			val &= ~TVE200_VSTSTYPE_BITS;
> +		}
> +		writel(val, priv->regs + TVE200_CTRL);
> +	} else
> +		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
> +
> +	/* Clear the interrupt once done */
> +	writel(stat, priv->regs + TVE200_INT_CLR);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int tve200_display_check(struct drm_simple_display_pipe *pipe,
> +			       struct drm_plane_state *pstate,
> +			       struct drm_crtc_state *cstate)
> +{
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
> +	struct drm_framebuffer *fb = pstate->fb;
> +	struct drm_connector *connector = pipe->connector;
> +	struct drm_device *drm = connector->dev;
> +
> +	/*
> +	 * We support these specific resolutions and nothing else.
> +	 */
> +	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
> +	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
> +	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
> +		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
> +			mode->hdisplay, mode->vdisplay);
> +		return -EINVAL;
> +	}
> +
> +	if (fb) {
> +		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
> +
> +		/* FB base address must be dword aligned. */
> +		if (offset & 3) {
> +			dev_err(drm->dev, "FB not 32-bit aligned\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * There's no pitch register, the mode's hdisplay
> +		 * controls this.
> +		 */
> +		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
> +			dev_err(drm->dev, "can't handle pitches\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * We can't change the FB format in a flicker-free
> +		 * manner (and only update it during CRTC enable).
> +		 */
> +		if (old_fb && old_fb->format != fb->format)
> +			cstate->mode_changed = true;
> +	}
> +
> +	return 0;
> +}
> +
> +static void tve200_display_enable(struct drm_simple_display_pipe *pipe,
> +				 struct drm_crtc_state *cstate)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_plane *plane = &pipe->plane;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *fb = plane->state->fb;
> +	struct drm_connector *connector = &priv->connector.connector;
> +	u32 format = fb->format->format;
> +	u32 ctrl1 = 0;
> +
> +	clk_prepare_enable(priv->clk);
> +
> +	/* Function 1 */
> +	ctrl1 |= TVE200_CTRL_CSMODE;
> +	/* Interlace mode for CCIR656: parameterize? */
> +	ctrl1 |= TVE200_CTRL_NONINTERLACE;
> +	/* 32 words per burst */
> +	ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
> +	/* 16 retries */
> +	ctrl1 |= TVE200_CTRL_RETRYCNT_16;
> +	/* NTSC mode: parametrize? */
> +	ctrl1 |= TVE200_CTRL_NTSC;
> +
> +	/* Vsync IRQ at start of Vsync at first */
> +	ctrl1 |= TVE200_VSTSTYPE_VSYNC;
> +
> +	if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
> +		ctrl1 |= TVE200_CTRL_TVCLKP;
> +
> +	if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
> +	    (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
> +		ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
> +		dev_info(drm->dev, "CIF mode\n");
> +	} else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
> +		ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
> +		dev_info(drm->dev, "VGA mode\n");
> +	} else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
> +		   (mode->hdisplay == 720 && mode->vdisplay == 576)) {
> +		ctrl1 |= TVE200_CTRL_IPRESOL_D1;
> +		dev_info(drm->dev, "D1 mode\n");
> +	}
> +
> +	if (format & DRM_FORMAT_BIG_ENDIAN) {
> +		ctrl1 |= TVE200_CTRL_BBBP;
> +		format &= ~DRM_FORMAT_BIG_ENDIAN;
> +	}
> +
> +	switch (format) {
> +	case DRM_FORMAT_XRGB8888:
> +		ctrl1 |= TVE200_IPDMOD_RGB888;
> +		break;
> +	case DRM_FORMAT_RGB565:
> +		ctrl1 |= TVE200_IPDMOD_RGB565;
> +		break;
> +	case DRM_FORMAT_XRGB1555:
> +		ctrl1 |= TVE200_IPDMOD_RGB555;
> +		break;
> +	case DRM_FORMAT_XBGR8888:
> +		ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_BGR565:
> +		ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_XBGR1555:
> +		ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
> +		break;
> +	case DRM_FORMAT_YUYV:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
> +		break;
> +	case DRM_FORMAT_YVYU:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
> +		break;
> +	case DRM_FORMAT_UYVY:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
> +		break;
> +	case DRM_FORMAT_VYUY:
> +		ctrl1 |= TVE200_IPDMOD_YUV422;
> +		ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
> +		break;
> +	case DRM_FORMAT_YUV420:
> +		ctrl1 |= TVE200_CTRL_YUV420;
> +		ctrl1 |= TVE200_IPDMOD_YUV420;
> +		break;
> +	default:
> +		dev_err(drm->dev, "Unknown FB format 0x%08x\n",
> +			fb->format->format);
> +		break;
> +	}
> +
> +	ctrl1 |= TVE200_TVEEN;
> +
> +	drm_panel_prepare(priv->connector.panel);
> +
> +	/* Turn it on */
> +	writel(ctrl1, priv->regs + TVE200_CTRL);
> +
> +	drm_panel_enable(priv->connector.panel);
> +
> +	drm_crtc_vblank_on(crtc);
> +}
> +
> +void tve200_display_disable(struct drm_simple_display_pipe *pipe)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_crtc_vblank_off(crtc);
> +
> +	drm_panel_disable(priv->connector.panel);
> +
> +	/* Disable and Power Down */
> +	writel(0, priv->regs + TVE200_CTRL);
> +
> +	drm_panel_unprepare(priv->connector.panel);
> +
> +	clk_disable_unprepare(priv->clk);
> +}
> +
> +static void tve200_display_update(struct drm_simple_display_pipe *pipe,
> +				 struct drm_plane_state *old_pstate)
> +{
> +	struct drm_crtc *crtc = &pipe->crtc;
> +	struct drm_device *drm = crtc->dev;
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	struct drm_pending_vblank_event *event = crtc->state->event;
> +	struct drm_plane *plane = &pipe->plane;
> +	struct drm_plane_state *pstate = plane->state;
> +	struct drm_framebuffer *fb = pstate->fb;
> +
> +	if (fb) {
> +		/* For RGB, the Y component is used as base address */
> +		writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
> +		       priv->regs + TVE200_Y_FRAME_BASE_ADDR);
> +
> +		/* For three plane YUV we need two more addresses */
> +		if (fb->format->format == DRM_FORMAT_YUV420) {
> +			writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
> +			       priv->regs + TVE200_U_FRAME_BASE_ADDR);
> +			writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
> +			       priv->regs + TVE200_V_FRAME_BASE_ADDR);
> +		}
> +	}
> +
> +	if (event) {
> +		crtc->state->event = NULL;
> +
> +		spin_lock_irq(&crtc->dev->event_lock);
> +		if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
> +			drm_crtc_arm_vblank_event(crtc, event);
> +		else
> +			drm_crtc_send_vblank_event(crtc, event);
> +		spin_unlock_irq(&crtc->dev->event_lock);
> +	}
> +}
> +
> +int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
> +	return 0;
> +}
> +
> +void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	writel(0, priv->regs + TVE200_INT_EN);
> +}
> +
> +static int tve200_display_prepare_fb(struct drm_simple_display_pipe *pipe,
> +				    struct drm_plane_state *plane_state)
> +{
> +	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
> +}
> +
> +const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
> +	.check = tve200_display_check,
> +	.enable = tve200_display_enable,
> +	.disable = tve200_display_disable,
> +	.update = tve200_display_update,
> +	.prepare_fb = tve200_display_prepare_fb,
> +};
> +
> +int tve200_display_init(struct drm_device *drm)
> +{
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +	int ret;
> +	static const u32 formats[] = {
> +		DRM_FORMAT_XRGB8888,
> +		DRM_FORMAT_XBGR8888,
> +		DRM_FORMAT_RGB565,
> +		DRM_FORMAT_BGR565,
> +		DRM_FORMAT_XRGB1555,
> +		DRM_FORMAT_XBGR1555,
> +		/*
> +		 * The controller actually supports any YCbCr ordering,
> +		 * for packed YCbCr. This just lists the orderings that
> +		 * DRM supports.
> +		 */
> +		DRM_FORMAT_YUYV,
> +		DRM_FORMAT_YVYU,
> +		DRM_FORMAT_UYVY,
> +		DRM_FORMAT_VYUY,
> +		/* This uses three planes */
> +		DRM_FORMAT_YUV420,
> +	};
> +
> +	ret = drm_simple_display_pipe_init(drm, &priv->pipe,
> +					   &tve200_display_funcs,
> +					   formats, ARRAY_SIZE(formats),
> +					   &priv->connector.connector);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_drm.h b/drivers/gpu/drm/tve200/tve200_drm.h
> new file mode 100644
> index 000000000000..f00fc47a6bd1
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_drm.h
> @@ -0,0 +1,129 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +#ifndef _TVE200_DRM_H_
> +#define _TVE200_DRM_H_
> +
> +/* Bits 2-31 are valid physical base addresses */
> +#define TVE200_Y_FRAME_BASE_ADDR	0x00
> +#define TVE200_U_FRAME_BASE_ADDR	0x04
> +#define TVE200_V_FRAME_BASE_ADDR	0x08
> +
> +#define TVE200_INT_EN			0x0C
> +#define TVE200_INT_CLR			0x10
> +#define TVE200_INT_STAT			0x14
> +#define TVE200_INT_BUS_ERR		BIT(7)
> +#define TVE200_INT_V_STATUS		BIT(6) /* vertical blank */
> +#define TVE200_INT_V_NEXT_FRAME		BIT(5)
> +#define TVE200_INT_U_NEXT_FRAME		BIT(4)
> +#define TVE200_INT_Y_NEXT_FRAME		BIT(3)
> +#define TVE200_INT_V_FIFO_UNDERRUN	BIT(2)
> +#define TVE200_INT_U_FIFO_UNDERRUN	BIT(1)
> +#define TVE200_INT_Y_FIFO_UNDERRUN	BIT(0)
> +#define TVE200_FIFO_UNDERRUNS		(TVE200_INT_V_FIFO_UNDERRUN | \
> +					 TVE200_INT_U_FIFO_UNDERRUN | \
> +					 TVE200_INT_Y_FIFO_UNDERRUN)
> +
> +#define TVE200_CTRL			0x18
> +#define TVE200_CTRL_YUV420		BIT(31)
> +#define TVE200_CTRL_CSMODE		BIT(30)
> +#define TVE200_CTRL_NONINTERLACE	BIT(28) /* 0 = non-interlace CCIR656 */
> +#define TVE200_CTRL_TVCLKP		BIT(27) /* Inverted clock phase */
> +/* Bits 24..26 define the burst size after arbitration on the bus */
> +#define TVE200_CTRL_BURST_4_WORDS	(0 << 24)
> +#define TVE200_CTRL_BURST_8_WORDS	(1 << 24)
> +#define TVE200_CTRL_BURST_16_WORDS	(2 << 24)
> +#define TVE200_CTRL_BURST_32_WORDS	(3 << 24)
> +#define TVE200_CTRL_BURST_64_WORDS	(4 << 24)
> +#define TVE200_CTRL_BURST_128_WORDS	(5 << 24)
> +#define TVE200_CTRL_BURST_256_WORDS	(6 << 24)
> +#define TVE200_CTRL_BURST_0_WORDS	(7 << 24) /* ? */
> +/*
> + * Bits 16..23 is the retry count*16 before issueing a new AHB transfer
> + * on the AHB bus.
> + */
> +#define TVE200_CTRL_RETRYCNT_MASK	GENMASK(23, 16)
> +#define TVE200_CTRL_RETRYCNT_16		(1 << 16)
> +#define TVE200_CTRL_BBBP		BIT(15) /* 0 = little-endian */
> +/* Bits 12..14 define the YCbCr ordering */
> +#define TVE200_CTRL_YCBCRODR_CB0Y0CR0Y1	(0 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y0CB0Y1CR0	(1 << 12)
> +#define TVE200_CTRL_YCBCRODR_CR0Y0CB0Y1	(2 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0	(3 << 12)
> +#define TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0	(4 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0	(5 << 12)
> +#define TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0	(6 << 12)
> +#define TVE200_CTRL_YCBCRODR_Y0CR0Y1CB0	(7 << 12)
> +/* Bits 10..11 define the input resolution (framebuffer size) */
> +#define TVE200_CTRL_IPRESOL_CIF		(0 << 10)
> +#define TVE200_CTRL_IPRESOL_VGA		(1 << 10)
> +#define TVE200_CTRL_IPRESOL_D1		(2 << 10)
> +#define TVE200_CTRL_NTSC		BIT(9) /* 0 = PAL, 1 = NTSC */
> +#define TVE200_CTRL_INTERLACE		BIT(8) /* 1 = interlace, only for D1 */
> +#define TVE200_IPDMOD_RGB555		(0 << 6) /* TVE200_CTRL_YUV420 = 0 */
> +#define TVE200_IPDMOD_RGB565		(1 << 6)
> +#define TVE200_IPDMOD_RGB888		(2 << 6)
> +#define TVE200_IPDMOD_YUV420		(2 << 6) /* TVE200_CTRL_YUV420 = 1 */
> +#define TVE200_IPDMOD_YUV422		(3 << 6)
> +/* Bits 4 & 5 define when to fire the vblank IRQ */
> +#define TVE200_VSTSTYPE_VSYNC		(0 << 4) /* start of vsync */
> +#define TVE200_VSTSTYPE_VBP		(1 << 4) /* start of v back porch */
> +#define TVE200_VSTSTYPE_VAI		(2 << 4) /* start of v active image */
> +#define TVE200_VSTSTYPE_VFP		(3 << 4) /* start of v front porch */
> +#define TVE200_VSTSTYPE_BITS		(BIT(4) | BIT(5))
> +#define TVE200_BGR			BIT(1) /* 0 = RGB, 1 = BGR */
> +#define TVE200_TVEEN			BIT(0) /* Enable TVE block */
> +
> +#define TVE200_CTRL_2			0x1c
> +#define TVE200_CTRL_3			0x20
> +
> +#define TVE200_CTRL_4			0x24
> +#define TVE200_CTRL_4_RESET		BIT(0) /* triggers reset of TVE200 */
> +
> +#include <drm/drm_gem.h>
> +#include <drm/drm_simple_kms_helper.h>
> +
> +struct tve200_drm_connector {
> +	struct drm_connector connector;
> +	struct drm_panel *panel;
> +};
> +
> +struct tve200_drm_dev_private {
> +	struct drm_device *drm;
> +
> +	struct tve200_drm_connector connector;
> +	struct drm_simple_display_pipe pipe;
> +	struct drm_fbdev_cma *fbdev;
> +
> +	void *regs;
> +	struct clk *pclk;
> +	struct clk *clk;
> +};
> +
> +#define to_tve200_connector(x) \
> +	container_of(x, struct tve200_drm_connector, connector)
> +
> +int tve200_display_init(struct drm_device *dev);
> +int tve200_enable_vblank(struct drm_device *drm, unsigned int crtc);
> +void tve200_disable_vblank(struct drm_device *drm, unsigned int crtc);
> +irqreturn_t tve200_irq(int irq, void *data);
> +int tve200_connector_init(struct drm_device *dev);
> +int tve200_encoder_init(struct drm_device *dev);
> +int tve200_dumb_create(struct drm_file *file_priv,
> +		      struct drm_device *dev,
> +		      struct drm_mode_create_dumb *args);
> +
> +#endif /* _TVE200_DRM_H_ */
> diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c
> new file mode 100644
> index 000000000000..9e34147e2617
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_drv.c
> @@ -0,0 +1,277 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * DOC: Faraday TV Encoder TVE200 DRM Driver
> + *
> + * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
> + * Controller (TVC) and is found in the Gemini Chipset from Storlink
> + * Semiconductor (later Storm Semiconductor, later Cortina Systems)
> + * but also in the Grain Media GM8180 chipset. On the Gemini the module
> + * is connected to 8 data lines and a single clock line, comprising an
> + * 8-bit BT.656 interface.
> + *
> + * This is a very basic YUV display driver. The datasheet specifies that
> + * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
> + * the hallmark of any TV encoder supporting both PAL and NTSC.
> + *
> + * This driver exposes a standard KMS interface for this TV encoder.
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/dma-buf.h>
> +#include <linux/irq.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/slab.h>
> +#include <linux/version.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +#define DRIVER_DESC      "DRM module for Faraday TVE200"
> +
> +struct drm_mode_config_funcs mode_config_funcs = {
> +	.fb_create = drm_fb_cma_create,
> +	.atomic_check = drm_atomic_helper_check,
> +	.atomic_commit = drm_atomic_helper_commit,
> +};
> +
> +static int tve200_modeset_init(struct drm_device *dev)
> +{
> +	struct drm_mode_config *mode_config;
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	int ret = 0;
> +
> +	drm_mode_config_init(dev);
> +	mode_config = &dev->mode_config;
> +	mode_config->funcs = &mode_config_funcs;
> +	mode_config->min_width = 352;
> +	mode_config->max_width = 720;
> +	mode_config->min_height = 240;
> +	mode_config->max_height = 576;
> +
> +	ret = tve200_connector_init(dev);
> +	if (ret) {
> +		dev_err(dev->dev, "Failed to create tve200_drm_connector\n");
> +		goto out_config;
> +	}
> +
> +	/*
> +	 * Don't actually attach if we didn't find a drm_panel
> +	 * attached to us.
> +	 */
> +	if (!priv->connector.panel) {
> +		dev_info(dev->dev,
> +			 "deferring due to lack of DRM panel device\n");
> +		ret = -EPROBE_DEFER;
> +		goto out_config;
> +	}
> +	dev_info(dev->dev, "attached to panel %s\n",
> +		 dev_name(priv->connector.panel->dev));
> +
> +	ret = tve200_display_init(dev);
> +	if (ret) {
> +		dev_err(dev->dev, "failed to init display\n");
> +		goto out_config;
> +	}
> +
> +	ret = drm_vblank_init(dev, 1);
> +	if (ret) {
> +		dev_err(dev->dev, "failed to init vblank\n");
> +		goto out_config;
> +	}
> +
> +	drm_mode_config_reset(dev);
> +
> +	/*
> +	 * Passing in 16 here will make the RGB656 mode the default
> +	 * Passing in 32 will use XRGB8888 mode
> +	 */
> +	priv->fbdev = drm_fbdev_cma_init(dev, 16,
> +					 dev->mode_config.num_connector);
> +	drm_kms_helper_poll_init(dev);
> +
> +	goto finish;
> +
> +out_config:
> +	drm_mode_config_cleanup(dev);
> +finish:
> +	return ret;
> +}
> +
> +DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
> +
> +static void tve200_lastclose(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +
> +	drm_fbdev_cma_restore_mode(priv->fbdev);
> +}
> +
> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = tve200_modeset_init(drm);
> +	if (ret)
> +		goto dev_unref;
> +
> +	ret = drm_dev_register(drm, 0);
> +	if (ret < 0)
> +		goto dev_unref;
> +
> +	return 0;
> +
> +clk_disable:
> +	clk_disable_unprepare(priv->pclk);
> +dev_unref:
> +	drm_dev_unref(drm);
> +	return ret;
> +}
> +
> +static int tve200_remove(struct platform_device *pdev)
> +{
> +	struct drm_device *drm = platform_get_drvdata(pdev);
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_dev_unregister(drm);
> +	if (priv->fbdev)
> +		drm_fbdev_cma_fini(priv->fbdev);
> +	drm_mode_config_cleanup(drm);
> +	clk_disable_unprepare(priv->pclk);
> +	drm_dev_unref(drm);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id tve200_of_match[] = {
> +	{
> +		.compatible = "faraday,tve200",
> +	},
> +	{},
> +};
> +
> +static struct platform_driver tve200_driver = {
> +	.driver = {
> +		.name           = "tve200",
> +		.of_match_table = of_match_ptr(tve200_of_match),
> +	},
> +	.probe = tve200_probe,
> +	.remove = tve200_remove,
> +};
> +module_platform_driver(tve200_driver);
> +
> +MODULE_DESCRIPTION(DRIVER_DESC);
> +MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
> +MODULE_LICENSE("GPL");
> -- 
> 2.13.4
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-13 15:11   ` Linus Walleij
@ 2017-08-14 18:56     ` Eric Anholt
  -1 siblings, 0 replies; 24+ messages in thread
From: Eric Anholt @ 2017-08-14 18:56 UTC (permalink / raw)
  To: linux-arm-kernel

Linus Walleij <linus.walleij@linaro.org> writes:

> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
>
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
>
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
>
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.

This looks great!  I've got just a couple of little things I noticed,
but with the init error path bugs fixed it's:

Reviewed-by: Eric Anholt <eric@anholt.net>

I also recommend checking out panel-bridge for deleting a bunch of the
code, and joining us in the drm-misc committer group :)

> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  Documentation/gpu/index.rst               |   1 +
>  Documentation/gpu/tve200.rst              |   6 +
>  MAINTAINERS                               |   6 +
>  drivers/gpu/drm/Kconfig                   |   2 +
>  drivers/gpu/drm/Makefile                  |   1 +
>  drivers/gpu/drm/tve200/Kconfig            |  15 ++
>  drivers/gpu/drm/tve200/Makefile           |   5 +
>  drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
>  drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
>  drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
>  drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
>  11 files changed, 914 insertions(+)
>  create mode 100644 Documentation/gpu/tve200.rst
>  create mode 100644 drivers/gpu/drm/tve200/Kconfig
>  create mode 100644 drivers/gpu/drm/tve200/Makefile
>  create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c
>
> diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
> index 35d673bf9b56..c36586dad29d 100644
> --- a/Documentation/gpu/index.rst
> +++ b/Documentation/gpu/index.rst
> @@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
>     pl111
>     tegra
>     tinydrm
> +   tve200
>     vc4
>     vga-switcheroo
>     vgaarbiter
> diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
> new file mode 100644
> index 000000000000..69b17b324e12
> --- /dev/null
> +++ b/Documentation/gpu/tve200.rst
> @@ -0,0 +1,6 @@
> +==================================
> + drm/tve200 Faraday TV Encoder 200
> +==================================
> +
> +.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
> +   :doc: Faraday TV Encoder 200
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e87cba115ea4..c3d42d68253a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
>  S:	Maintained
>  F:	drivers/gpu/drm/bochs/
>  
> +DRM DRIVER FOR FARADAY TVE200 TV ENCODER
> +M:	Linus Walleij <linus.walleij@linaro.org>
> +T:	git git://anongit.freedesktop.org/drm/drm-misc
> +S:	Maintained
> +F:	drivers/gpu/drm/tve200/
> +
>  DRM DRIVER FOR INTEL I810 VIDEO CARDS
>  S:	Orphan / Obsolete
>  F:	drivers/gpu/drm/i810/
> diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
> index 83cb2a88c204..c5e1a8409285 100644
> --- a/drivers/gpu/drm/Kconfig
> +++ b/drivers/gpu/drm/Kconfig
> @@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
>  
>  source "drivers/gpu/drm/pl111/Kconfig"
>  
> +source "drivers/gpu/drm/tve200/Kconfig"
> +
>  # Keep legacy drivers last
>  
>  menuconfig DRM_LEGACY
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index 24a066e1841c..cc81813e2238 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
>  obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
>  obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
>  obj-$(CONFIG_DRM_PL111) += pl111/
> +obj-$(CONFIG_DRM_TVE200) += tve200/
> diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
> new file mode 100644
> index 000000000000..21d9841ddb88
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Kconfig
> @@ -0,0 +1,15 @@
> +config DRM_TVE200
> +	tristate "DRM Support for Faraday TV Encoder TVE200"
> +	depends on DRM
> +	depends on CMA
> +	depends on ARM || COMPILE_TEST
> +	depends on OF
> +	select DRM_PANEL
> +	select DRM_KMS_HELPER
> +	select DRM_KMS_CMA_HELPER
> +	select DRM_GEM_CMA_HELPER
> +	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
> +	help
> +	  Choose this option for DRM support for the Faraday TV Encoder
> +	  TVE200 Controller.
> +	  If M is selected the module will be called tve200_drm.
> diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
> new file mode 100644
> index 000000000000..a9dba54f7ee5
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Makefile
> @@ -0,0 +1,5 @@
> +tve200_drm-y +=	tve200_connector.o \
> +		tve200_display.o \
> +		tve200_drv.o
> +
> +obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
> diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
> new file mode 100644
> index 000000000000..93e99156d375
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_connector.c
> @@ -0,0 +1,126 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * tve200_drm_connector.c
> + * Implementation of the connector functions for the Faraday TV Encoder
> + */
> +#include <linux/version.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/dma-buf.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_of.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +static void tve200_connector_destroy(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (tve200con->panel)
> +		drm_panel_detach(tve200con->panel);
> +
> +	drm_connector_unregister(connector);
> +	drm_connector_cleanup(connector);
> +}
> +
> +static enum drm_connector_status tve200_connector_detect(struct drm_connector
> +							*connector, bool force)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	return (tve200con->panel ?
> +		connector_status_connected :
> +		connector_status_disconnected);
> +}
> +
> +static int tve200_connector_helper_get_modes(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (!tve200con->panel)
> +		return 0;
> +
> +	return drm_panel_get_modes(tve200con->panel);
> +}
> +
> +static const struct drm_connector_funcs connector_funcs = {
> +	.fill_modes = drm_helper_probe_single_connector_modes,
> +	.destroy = tve200_connector_destroy,
> +	.detect = tve200_connector_detect,
> +	.dpms = drm_atomic_helper_connector_dpms,
> +	.reset = drm_atomic_helper_connector_reset,
> +	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
> +	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
> +};
> +
> +static const struct drm_connector_helper_funcs connector_helper_funcs = {
> +	.get_modes = tve200_connector_helper_get_modes,
> +};
> +
> +/*
> + * Walks the OF graph to find the panel node and then asks DRM to look
> + * up the panel.
> + */
> +static struct drm_panel *tve200_get_panel(struct device *dev)
> +{
> +	struct device_node *endpoint, *panel_node;
> +	struct device_node *np = dev->of_node;
> +	struct drm_panel *panel;
> +
> +	endpoint = of_graph_get_next_endpoint(np, NULL);
> +	if (!endpoint) {
> +		dev_err(dev, "no endpoint to fetch panel\n");
> +		return NULL;
> +	}
> +
> +	/* Don't proceed if we have an endpoint but no panel_node tied to it */
> +	panel_node = of_graph_get_remote_port_parent(endpoint);
> +	of_node_put(endpoint);
> +	if (!panel_node) {
> +		dev_err(dev, "no valid panel node\n");
> +		return NULL;
> +	}
> +
> +	panel = of_drm_find_panel(panel_node);
> +	of_node_put(panel_node);
> +
> +	return panel;
> +}
> +
> +int tve200_connector_init(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	struct tve200_drm_connector *tve200con = &priv->connector;
> +	struct drm_connector *connector = &tve200con->connector;
> +
> +	drm_connector_init(dev, connector, &connector_funcs,
> +			   DRM_MODE_CONNECTOR_DPI);
> +	drm_connector_helper_add(connector, &connector_helper_funcs);
> +
> +	tve200con->panel = tve200_get_panel(dev->dev);
> +	if (tve200con->panel)
> +		drm_panel_attach(tve200con->panel, connector);
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
> new file mode 100644
> index 000000000000..027553aacb33
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_display.c
> @@ -0,0 +1,346 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +#include <linux/clk.h>
> +#include <linux/version.h>
> +#include <linux/dma-buf.h>
> +#include <linux/of_graph.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_panel.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +
> +#include "tve200_drm.h"
> +
> +irqreturn_t tve200_irq(int irq, void *data)
> +{
> +	struct tve200_drm_dev_private *priv = data;
> +	u32 stat;
> +	u32 val;
> +
> +	stat = readl(priv->regs + TVE200_INT_STAT);
> +
> +	if (!stat)
> +		return IRQ_NONE;
> +
> +	/*
> +	 * Vblank IRQ
> +	 *
> +	 * The hardware is a bit tilted: the line stays high after clearing
> +	 * the vblank IRQ, fireing many more interrupts. We counter this
> +	 * by toggling the IRQ back and forth from fireing at vblank and
> +	 * fireing at start of active image, which works around the problem
> +	 * since those occur strictly in sequence, and we get two IRQs for each
> +	 * frame, one at start of Vblank (that we make call into the CRTC) and
> +	 * another one at the start of the image (that we discard).
> +	 */

spelling nit: firing

Seems like a pretty good solution, though it makes me wonder how they
intended the HW to be used.

> +	if (stat & TVE200_INT_V_STATUS) {
> +		val = readl(priv->regs + TVE200_CTRL);
> +		/* We have an actual start of vsync */
> +		if (!(val & TVE200_VSTSTYPE_BITS)) {
> +			drm_crtc_handle_vblank(&priv->pipe.crtc);
> +			/* Toggle trigger to start of active image */
> +			val |= TVE200_VSTSTYPE_VAI;
> +		} else {
> +			/* Toggle trigger back to start of vsync */
> +			val &= ~TVE200_VSTSTYPE_BITS;
> +		}
> +		writel(val, priv->regs + TVE200_CTRL);
> +	} else
> +		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
> +
> +	/* Clear the interrupt once done */
> +	writel(stat, priv->regs + TVE200_INT_CLR);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int tve200_display_check(struct drm_simple_display_pipe *pipe,
> +			       struct drm_plane_state *pstate,
> +			       struct drm_crtc_state *cstate)
> +{
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
> +	struct drm_framebuffer *fb = pstate->fb;
> +	struct drm_connector *connector = pipe->connector;
> +	struct drm_device *drm = connector->dev;
> +
> +	/*
> +	 * We support these specific resolutions and nothing else.
> +	 */
> +	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
> +	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
> +	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
> +		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
> +			mode->hdisplay, mode->vdisplay);
> +		return -EINVAL;
> +	}
> +
> +	if (fb) {
> +		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
> +
> +		/* FB base address must be dword aligned. */
> +		if (offset & 3) {
> +			dev_err(drm->dev, "FB not 32-bit aligned\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * There's no pitch register, the mode's hdisplay
> +		 * controls this.
> +		 */
> +		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
> +			dev_err(drm->dev, "can't handle pitches\n");
> +			return -EINVAL;
> +		}

I learned recently that one of the general guidelines we have is "don't
let userspace trigger dmesg errors."  I think these dev_err()s would be
better as DRM_DEBUG_KMS() (which you can enable at boot or runtime using
the drm.debug module parameter)

> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,

I think you also want CMA's helpers for .gem_prime_vmap, vunmap, mmap.
I think there are igt tests for this, but it's something you won't
notice in normal usage.

> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;

Don't this and the following gotos want to be goto clk_disable, instead?

> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;

goto instead of return here, as well?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170814/3f831343/attachment-0001.sig>

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-14 18:56     ` Eric Anholt
  0 siblings, 0 replies; 24+ messages in thread
From: Eric Anholt @ 2017-08-14 18:56 UTC (permalink / raw)
  To: Linus Walleij, dri-devel, Daniel Vetter, Jani Nikula, Sean Paul
  Cc: linux-arm-kernel


[-- Attachment #1.1: Type: text/plain, Size: 17755 bytes --]

Linus Walleij <linus.walleij@linaro.org> writes:

> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
>
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
>
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
>
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.

This looks great!  I've got just a couple of little things I noticed,
but with the init error path bugs fixed it's:

Reviewed-by: Eric Anholt <eric@anholt.net>

I also recommend checking out panel-bridge for deleting a bunch of the
code, and joining us in the drm-misc committer group :)

> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  Documentation/gpu/index.rst               |   1 +
>  Documentation/gpu/tve200.rst              |   6 +
>  MAINTAINERS                               |   6 +
>  drivers/gpu/drm/Kconfig                   |   2 +
>  drivers/gpu/drm/Makefile                  |   1 +
>  drivers/gpu/drm/tve200/Kconfig            |  15 ++
>  drivers/gpu/drm/tve200/Makefile           |   5 +
>  drivers/gpu/drm/tve200/tve200_connector.c | 126 +++++++++++
>  drivers/gpu/drm/tve200/tve200_display.c   | 346 ++++++++++++++++++++++++++++++
>  drivers/gpu/drm/tve200/tve200_drm.h       | 129 +++++++++++
>  drivers/gpu/drm/tve200/tve200_drv.c       | 277 ++++++++++++++++++++++++
>  11 files changed, 914 insertions(+)
>  create mode 100644 Documentation/gpu/tve200.rst
>  create mode 100644 drivers/gpu/drm/tve200/Kconfig
>  create mode 100644 drivers/gpu/drm/tve200/Makefile
>  create mode 100644 drivers/gpu/drm/tve200/tve200_connector.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_display.c
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drm.h
>  create mode 100644 drivers/gpu/drm/tve200/tve200_drv.c
>
> diff --git a/Documentation/gpu/index.rst b/Documentation/gpu/index.rst
> index 35d673bf9b56..c36586dad29d 100644
> --- a/Documentation/gpu/index.rst
> +++ b/Documentation/gpu/index.rst
> @@ -15,6 +15,7 @@ Linux GPU Driver Developer's Guide
>     pl111
>     tegra
>     tinydrm
> +   tve200
>     vc4
>     vga-switcheroo
>     vgaarbiter
> diff --git a/Documentation/gpu/tve200.rst b/Documentation/gpu/tve200.rst
> new file mode 100644
> index 000000000000..69b17b324e12
> --- /dev/null
> +++ b/Documentation/gpu/tve200.rst
> @@ -0,0 +1,6 @@
> +==================================
> + drm/tve200 Faraday TV Encoder 200
> +==================================
> +
> +.. kernel-doc:: drivers/gpu/drm/tve200/tve200_drv.c
> +   :doc: Faraday TV Encoder 200
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e87cba115ea4..c3d42d68253a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4305,6 +4305,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
>  S:	Maintained
>  F:	drivers/gpu/drm/bochs/
>  
> +DRM DRIVER FOR FARADAY TVE200 TV ENCODER
> +M:	Linus Walleij <linus.walleij@linaro.org>
> +T:	git git://anongit.freedesktop.org/drm/drm-misc
> +S:	Maintained
> +F:	drivers/gpu/drm/tve200/
> +
>  DRM DRIVER FOR INTEL I810 VIDEO CARDS
>  S:	Orphan / Obsolete
>  F:	drivers/gpu/drm/i810/
> diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
> index 83cb2a88c204..c5e1a8409285 100644
> --- a/drivers/gpu/drm/Kconfig
> +++ b/drivers/gpu/drm/Kconfig
> @@ -278,6 +278,8 @@ source "drivers/gpu/drm/tinydrm/Kconfig"
>  
>  source "drivers/gpu/drm/pl111/Kconfig"
>  
> +source "drivers/gpu/drm/tve200/Kconfig"
> +
>  # Keep legacy drivers last
>  
>  menuconfig DRM_LEGACY
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index 24a066e1841c..cc81813e2238 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -100,3 +100,4 @@ obj-$(CONFIG_DRM_ZTE)	+= zte/
>  obj-$(CONFIG_DRM_MXSFB)	+= mxsfb/
>  obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
>  obj-$(CONFIG_DRM_PL111) += pl111/
> +obj-$(CONFIG_DRM_TVE200) += tve200/
> diff --git a/drivers/gpu/drm/tve200/Kconfig b/drivers/gpu/drm/tve200/Kconfig
> new file mode 100644
> index 000000000000..21d9841ddb88
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Kconfig
> @@ -0,0 +1,15 @@
> +config DRM_TVE200
> +	tristate "DRM Support for Faraday TV Encoder TVE200"
> +	depends on DRM
> +	depends on CMA
> +	depends on ARM || COMPILE_TEST
> +	depends on OF
> +	select DRM_PANEL
> +	select DRM_KMS_HELPER
> +	select DRM_KMS_CMA_HELPER
> +	select DRM_GEM_CMA_HELPER
> +	select VT_HW_CONSOLE_BINDING if FRAMEBUFFER_CONSOLE
> +	help
> +	  Choose this option for DRM support for the Faraday TV Encoder
> +	  TVE200 Controller.
> +	  If M is selected the module will be called tve200_drm.
> diff --git a/drivers/gpu/drm/tve200/Makefile b/drivers/gpu/drm/tve200/Makefile
> new file mode 100644
> index 000000000000..a9dba54f7ee5
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/Makefile
> @@ -0,0 +1,5 @@
> +tve200_drm-y +=	tve200_connector.o \
> +		tve200_display.o \
> +		tve200_drv.o
> +
> +obj-$(CONFIG_DRM_TVE200) += tve200_drm.o
> diff --git a/drivers/gpu/drm/tve200/tve200_connector.c b/drivers/gpu/drm/tve200/tve200_connector.c
> new file mode 100644
> index 000000000000..93e99156d375
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_connector.c
> @@ -0,0 +1,126 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +
> +/**
> + * tve200_drm_connector.c
> + * Implementation of the connector functions for the Faraday TV Encoder
> + */
> +#include <linux/version.h>
> +#include <linux/shmem_fs.h>
> +#include <linux/dma-buf.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_atomic_helper.h>
> +#include <drm/drm_crtc_helper.h>
> +#include <drm/drm_of.h>
> +#include <drm/drm_panel.h>
> +
> +#include "tve200_drm.h"
> +
> +static void tve200_connector_destroy(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (tve200con->panel)
> +		drm_panel_detach(tve200con->panel);
> +
> +	drm_connector_unregister(connector);
> +	drm_connector_cleanup(connector);
> +}
> +
> +static enum drm_connector_status tve200_connector_detect(struct drm_connector
> +							*connector, bool force)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	return (tve200con->panel ?
> +		connector_status_connected :
> +		connector_status_disconnected);
> +}
> +
> +static int tve200_connector_helper_get_modes(struct drm_connector *connector)
> +{
> +	struct tve200_drm_connector *tve200con =
> +		to_tve200_connector(connector);
> +
> +	if (!tve200con->panel)
> +		return 0;
> +
> +	return drm_panel_get_modes(tve200con->panel);
> +}
> +
> +static const struct drm_connector_funcs connector_funcs = {
> +	.fill_modes = drm_helper_probe_single_connector_modes,
> +	.destroy = tve200_connector_destroy,
> +	.detect = tve200_connector_detect,
> +	.dpms = drm_atomic_helper_connector_dpms,
> +	.reset = drm_atomic_helper_connector_reset,
> +	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
> +	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
> +};
> +
> +static const struct drm_connector_helper_funcs connector_helper_funcs = {
> +	.get_modes = tve200_connector_helper_get_modes,
> +};
> +
> +/*
> + * Walks the OF graph to find the panel node and then asks DRM to look
> + * up the panel.
> + */
> +static struct drm_panel *tve200_get_panel(struct device *dev)
> +{
> +	struct device_node *endpoint, *panel_node;
> +	struct device_node *np = dev->of_node;
> +	struct drm_panel *panel;
> +
> +	endpoint = of_graph_get_next_endpoint(np, NULL);
> +	if (!endpoint) {
> +		dev_err(dev, "no endpoint to fetch panel\n");
> +		return NULL;
> +	}
> +
> +	/* Don't proceed if we have an endpoint but no panel_node tied to it */
> +	panel_node = of_graph_get_remote_port_parent(endpoint);
> +	of_node_put(endpoint);
> +	if (!panel_node) {
> +		dev_err(dev, "no valid panel node\n");
> +		return NULL;
> +	}
> +
> +	panel = of_drm_find_panel(panel_node);
> +	of_node_put(panel_node);
> +
> +	return panel;
> +}
> +
> +int tve200_connector_init(struct drm_device *dev)
> +{
> +	struct tve200_drm_dev_private *priv = dev->dev_private;
> +	struct tve200_drm_connector *tve200con = &priv->connector;
> +	struct drm_connector *connector = &tve200con->connector;
> +
> +	drm_connector_init(dev, connector, &connector_funcs,
> +			   DRM_MODE_CONNECTOR_DPI);
> +	drm_connector_helper_add(connector, &connector_helper_funcs);
> +
> +	tve200con->panel = tve200_get_panel(dev->dev);
> +	if (tve200con->panel)
> +		drm_panel_attach(tve200con->panel, connector);
> +
> +	return 0;
> +}
> diff --git a/drivers/gpu/drm/tve200/tve200_display.c b/drivers/gpu/drm/tve200/tve200_display.c
> new file mode 100644
> index 000000000000..027553aacb33
> --- /dev/null
> +++ b/drivers/gpu/drm/tve200/tve200_display.c
> @@ -0,0 +1,346 @@
> +/*
> + * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
> + * Parts of this file were based on sources as follows:
> + *
> + * Copyright (C) 2006-2008 Intel Corporation
> + * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
> + * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
> + * Copyright (C) 2011 Texas Instruments
> + * Copyright (C) 2017 Eric Anholt
> + *
> + * This program is free software and is provided to you under the terms of the
> + * GNU General Public License version 2 as published by the Free Software
> + * Foundation, and any use by you of this program is subject to the terms of
> + * such GNU licence.
> + */
> +#include <linux/clk.h>
> +#include <linux/version.h>
> +#include <linux/dma-buf.h>
> +#include <linux/of_graph.h>
> +
> +#include <drm/drmP.h>
> +#include <drm/drm_panel.h>
> +#include <drm/drm_gem_cma_helper.h>
> +#include <drm/drm_fb_cma_helper.h>
> +
> +#include "tve200_drm.h"
> +
> +irqreturn_t tve200_irq(int irq, void *data)
> +{
> +	struct tve200_drm_dev_private *priv = data;
> +	u32 stat;
> +	u32 val;
> +
> +	stat = readl(priv->regs + TVE200_INT_STAT);
> +
> +	if (!stat)
> +		return IRQ_NONE;
> +
> +	/*
> +	 * Vblank IRQ
> +	 *
> +	 * The hardware is a bit tilted: the line stays high after clearing
> +	 * the vblank IRQ, fireing many more interrupts. We counter this
> +	 * by toggling the IRQ back and forth from fireing at vblank and
> +	 * fireing at start of active image, which works around the problem
> +	 * since those occur strictly in sequence, and we get two IRQs for each
> +	 * frame, one at start of Vblank (that we make call into the CRTC) and
> +	 * another one at the start of the image (that we discard).
> +	 */

spelling nit: firing

Seems like a pretty good solution, though it makes me wonder how they
intended the HW to be used.

> +	if (stat & TVE200_INT_V_STATUS) {
> +		val = readl(priv->regs + TVE200_CTRL);
> +		/* We have an actual start of vsync */
> +		if (!(val & TVE200_VSTSTYPE_BITS)) {
> +			drm_crtc_handle_vblank(&priv->pipe.crtc);
> +			/* Toggle trigger to start of active image */
> +			val |= TVE200_VSTSTYPE_VAI;
> +		} else {
> +			/* Toggle trigger back to start of vsync */
> +			val &= ~TVE200_VSTSTYPE_BITS;
> +		}
> +		writel(val, priv->regs + TVE200_CTRL);
> +	} else
> +		dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
> +
> +	/* Clear the interrupt once done */
> +	writel(stat, priv->regs + TVE200_INT_CLR);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int tve200_display_check(struct drm_simple_display_pipe *pipe,
> +			       struct drm_plane_state *pstate,
> +			       struct drm_crtc_state *cstate)
> +{
> +	const struct drm_display_mode *mode = &cstate->mode;
> +	struct drm_framebuffer *old_fb = pipe->plane.state->fb;
> +	struct drm_framebuffer *fb = pstate->fb;
> +	struct drm_connector *connector = pipe->connector;
> +	struct drm_device *drm = connector->dev;
> +
> +	/*
> +	 * We support these specific resolutions and nothing else.
> +	 */
> +	if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
> +	    !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
> +	    !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
> +	    !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
> +		dev_err(drm->dev, "unsupported display mode (%u x %u)\n",
> +			mode->hdisplay, mode->vdisplay);
> +		return -EINVAL;
> +	}
> +
> +	if (fb) {
> +		u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
> +
> +		/* FB base address must be dword aligned. */
> +		if (offset & 3) {
> +			dev_err(drm->dev, "FB not 32-bit aligned\n");
> +			return -EINVAL;
> +		}
> +
> +		/*
> +		 * There's no pitch register, the mode's hdisplay
> +		 * controls this.
> +		 */
> +		if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
> +			dev_err(drm->dev, "can't handle pitches\n");
> +			return -EINVAL;
> +		}

I learned recently that one of the general guidelines we have is "don't
let userspace trigger dmesg errors."  I think these dev_err()s would be
better as DRM_DEBUG_KMS() (which you can enable at boot or runtime using
the drm.debug module parameter)

> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,

I think you also want CMA's helpers for .gem_prime_vmap, vunmap, mmap.
I think there are igt tests for this, but it's something you won't
notice in normal usage.

> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;

Don't this and the following gotos want to be goto clk_disable, instead?

> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;

goto instead of return here, as well?

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-14 14:20     ` Daniel Vetter
@ 2017-08-15 20:02       ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-15 20:02 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 14, 2017 at 4:20 PM, Daniel Vetter <daniel@ffwll.ch> wrote:

> Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
> happily throw commit rights at every driver submission (and honestly
> expect that, since it helps tremendously with balance maintainer loads for
> the oddball trivial patch).

Yeah I'm game for it :)

I need to reiterate the patch addressing yours and Eric's comments first.

I *really* want to experience your development workflow first hand after
seeing your talks, so it'll be exciting. Just tell me how to proceed with
this and/or point me to the right docs and I'll get going with it.

Yours,
Linus Walleij

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-15 20:02       ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-15 20:02 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: open list:DRM PANEL DRIVERS, Daniel Vetter, linux-arm-kernel

On Mon, Aug 14, 2017 at 4:20 PM, Daniel Vetter <daniel@ffwll.ch> wrote:

> Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
> happily throw commit rights at every driver submission (and honestly
> expect that, since it helps tremendously with balance maintainer loads for
> the oddball trivial patch).

Yeah I'm game for it :)

I need to reiterate the patch addressing yours and Eric's comments first.

I *really* want to experience your development workflow first hand after
seeing your talks, so it'll be exciting. Just tell me how to proceed with
this and/or point me to the right docs and I'll get going with it.

Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-15 20:02       ` Linus Walleij
@ 2017-08-15 20:08         ` Daniel Vetter
  -1 siblings, 0 replies; 24+ messages in thread
From: Daniel Vetter @ 2017-08-15 20:08 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Aug 15, 2017 at 10:02 PM, Linus Walleij
<linus.walleij@linaro.org> wrote:
> On Mon, Aug 14, 2017 at 4:20 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
>
>> Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
>> happily throw commit rights at every driver submission (and honestly
>> expect that, since it helps tremendously with balance maintainer loads for
>> the oddball trivial patch).
>
> Yeah I'm game for it :)
>
> I need to reiterate the patch addressing yours and Eric's comments first.
>
> I *really* want to experience your development workflow first hand after
> seeing your talks, so it'll be exciting. Just tell me how to proceed with
> this and/or point me to the right docs and I'll get going with it.

You need an fd.o account with acess to drm-misc.

https://www.freedesktop.org/wiki/AccountRequests/

Meanwhile enjoy the docs:

https://01.org/linuxgraphics/gfx-docs/maintainer-tools/drm-misc.html
https://01.org/linuxgraphics/gfx-docs/maintainer-tools/dim.html

(we have patches in flight to make it into a neat sphinx-style book
like the kerneldocs now are)

Then grab dim (our script)
https://cgit.freedesktop.org/drm-intel/tree/dim?h=maintainer-tools

Adjust dimrc to your taste and run dim setup (that will also grab a
checkout of all the maintainer-tools so that it autoupdates). There's
a bunch of howtos all around, and once fully installed there's also
bash completion and stuff like that.

If you hit a snag, best way to get help is over #dri-devel on
freenode. That's also where we tend to organize/coordinate review and
stuff. Also if anything is uncool with the tools pls pipe up, we
constantly try to make them better.

Cheers, Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-15 20:08         ` Daniel Vetter
  0 siblings, 0 replies; 24+ messages in thread
From: Daniel Vetter @ 2017-08-15 20:08 UTC (permalink / raw)
  To: Linus Walleij
  Cc: open list:DRM PANEL DRIVERS, Daniel Vetter, linux-arm-kernel

On Tue, Aug 15, 2017 at 10:02 PM, Linus Walleij
<linus.walleij@linaro.org> wrote:
> On Mon, Aug 14, 2017 at 4:20 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
>
>> Wrt merging/maintaining: Want to include it in the drm-misc pile? We'll
>> happily throw commit rights at every driver submission (and honestly
>> expect that, since it helps tremendously with balance maintainer loads for
>> the oddball trivial patch).
>
> Yeah I'm game for it :)
>
> I need to reiterate the patch addressing yours and Eric's comments first.
>
> I *really* want to experience your development workflow first hand after
> seeing your talks, so it'll be exciting. Just tell me how to proceed with
> this and/or point me to the right docs and I'll get going with it.

You need an fd.o account with acess to drm-misc.

https://www.freedesktop.org/wiki/AccountRequests/

Meanwhile enjoy the docs:

https://01.org/linuxgraphics/gfx-docs/maintainer-tools/drm-misc.html
https://01.org/linuxgraphics/gfx-docs/maintainer-tools/dim.html

(we have patches in flight to make it into a neat sphinx-style book
like the kerneldocs now are)

Then grab dim (our script)
https://cgit.freedesktop.org/drm-intel/tree/dim?h=maintainer-tools

Adjust dimrc to your taste and run dim setup (that will also grab a
checkout of all the maintainer-tools so that it autoupdates). There's
a bunch of howtos all around, and once fully installed there's also
bash completion and stuff like that.

If you hit a snag, best way to get help is over #dri-devel on
freenode. That's also where we tend to organize/coordinate review and
stuff. Also if anything is uncool with the tools pls pipe up, we
constantly try to make them better.

Cheers, Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-13 15:11   ` Linus Walleij
@ 2017-08-15 21:49     ` Noralf Trønnes
  -1 siblings, 0 replies; 24+ messages in thread
From: Noralf Trønnes @ 2017-08-15 21:49 UTC (permalink / raw)
  To: linux-arm-kernel


Den 13.08.2017 17.11, skrev Linus Walleij:
> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
>
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
>
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
>
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.
>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---

[...]

> diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c

[...]

> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,

The dumb_destroy and dumb_map_offset callbacks have defaults now that
fits the cma helper drivers so you don't need to set them. And
drm_gem_cma_dumb_map_offset() is gone as soon as I get and ack on the
remaining cleanup patches.

Noralf.

> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = tve200_modeset_init(drm);
> +	if (ret)
> +		goto dev_unref;
> +
> +	ret = drm_dev_register(drm, 0);
> +	if (ret < 0)
> +		goto dev_unref;
> +
> +	return 0;
> +
> +clk_disable:
> +	clk_disable_unprepare(priv->pclk);
> +dev_unref:
> +	drm_dev_unref(drm);
> +	return ret;
> +}
> +
> +static int tve200_remove(struct platform_device *pdev)
> +{
> +	struct drm_device *drm = platform_get_drvdata(pdev);
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_dev_unregister(drm);
> +	if (priv->fbdev)
> +		drm_fbdev_cma_fini(priv->fbdev);
> +	drm_mode_config_cleanup(drm);
> +	clk_disable_unprepare(priv->pclk);
> +	drm_dev_unref(drm);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id tve200_of_match[] = {
> +	{
> +		.compatible = "faraday,tve200",
> +	},
> +	{},
> +};
> +
> +static struct platform_driver tve200_driver = {
> +	.driver = {
> +		.name           = "tve200",
> +		.of_match_table = of_match_ptr(tve200_of_match),
> +	},
> +	.probe = tve200_probe,
> +	.remove = tve200_remove,
> +};
> +module_platform_driver(tve200_driver);
> +
> +MODULE_DESCRIPTION(DRIVER_DESC);
> +MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
> +MODULE_LICENSE("GPL");

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-15 21:49     ` Noralf Trønnes
  0 siblings, 0 replies; 24+ messages in thread
From: Noralf Trønnes @ 2017-08-15 21:49 UTC (permalink / raw)
  To: Linus Walleij, dri-devel, Eric Anholt, Daniel Vetter,
	Jani Nikula, Sean Paul
  Cc: linux-arm-kernel


Den 13.08.2017 17.11, skrev Linus Walleij:
> This adds a new DRM driver for the Faraday Technology TVE200
> block. This "TV Encoder" encodes a ITU-T BT.656 stream and can
> be found in the StorLink SL3516 (later Cortina Systems CS3516)
> as well as the Grain Media GM8180.
>
> I do not have definitive word from anyone at Faraday that this
> IP block is theirs, but it bears the hallmark of their 3-digit
> version code (200) and is used in two SoCs from completely
> different companies. (Grain Media was fully owned by Faraday
> until it was transferred to NovoTek this january, and
> Faraday did lots of work on the StorLink SoCs.)
>
> The D-Link DIR-685 uses this in connection with the Ilitek
> ILI9322 panel driver that supports BT.656 input, while the
> GM8180 apparently has been used with the Cirrus Logic CS4954
> digital video encoder. The oldest user seems to be
> something called Techwall 2835.
>
> This driver is heavily inspired by Eric Anholt's PL111
> driver and therefore I have mentioned all the ancestor authors
> in the header file.
>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---

[...]

> diff --git a/drivers/gpu/drm/tve200/tve200_drv.c b/drivers/gpu/drm/tve200/tve200_drv.c

[...]

> +static struct drm_driver tve200_drm_driver = {
> +	.driver_features =
> +		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
> +	.lastclose = tve200_lastclose,
> +	.ioctls = NULL,
> +	.fops = &drm_fops,
> +	.name = "tve200",
> +	.desc = DRIVER_DESC,
> +	.date = "20170703",
> +	.major = 1,
> +	.minor = 0,
> +	.patchlevel = 0,
> +	.dumb_create = drm_gem_cma_dumb_create,
> +	.dumb_destroy = drm_gem_dumb_destroy,
> +	.dumb_map_offset = drm_gem_cma_dumb_map_offset,

The dumb_destroy and dumb_map_offset callbacks have defaults now that
fits the cma helper drivers so you don't need to set them. And
drm_gem_cma_dumb_map_offset() is gone as soon as I get and ack on the
remaining cleanup patches.

Noralf.

> +	.gem_free_object = drm_gem_cma_free_object,
> +	.gem_vm_ops = &drm_gem_cma_vm_ops,
> +
> +	.enable_vblank = tve200_enable_vblank,
> +	.disable_vblank = tve200_disable_vblank,
> +
> +	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
> +	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> +	.gem_prime_import = drm_gem_prime_import,
> +	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
> +	.gem_prime_export = drm_gem_prime_export,
> +	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
> +};
> +
> +static int tve200_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct tve200_drm_dev_private *priv;
> +	struct drm_device *drm;
> +	struct resource *res;
> +	int irq;
> +	int ret;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	drm = drm_dev_alloc(&tve200_drm_driver, dev);
> +	if (IS_ERR(drm))
> +		return PTR_ERR(drm);
> +	platform_set_drvdata(pdev, drm);
> +	priv->drm = drm;
> +	drm->dev_private = priv;
> +
> +	/* Clock the silicon so we can access the registers */
> +	priv->pclk = devm_clk_get(dev, "PCLK");
> +	if (IS_ERR(priv->pclk)) {
> +		dev_err(dev, "unable to get PCLK\n");
> +		ret = PTR_ERR(priv->pclk);
> +		goto dev_unref;
> +	}
> +	ret = clk_prepare_enable(priv->pclk);
> +	if (ret) {
> +		dev_err(dev, "failed to enable PCLK\n");
> +		goto dev_unref;
> +	}
> +
> +	/* This clock is for the pixels (27MHz) */
> +	priv->clk = devm_clk_get(dev, "TVE");
> +	if (IS_ERR(priv->clk)) {
> +		dev_err(dev, "unable to get TVE clock\n");
> +		ret = PTR_ERR(priv->clk);
> +		goto clk_disable;
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	priv->regs = devm_ioremap_resource(dev, res);
> +	if (!priv->regs) {
> +		dev_err(dev, "%s failed mmio\n", __func__);
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	irq = platform_get_irq(pdev, 0);
> +	if (!irq) {
> +		ret = -EINVAL;
> +		goto dev_unref;
> +	}
> +
> +	/* turn off interrupts before requesting the irq */
> +	writel(0, priv->regs + TVE200_INT_EN);
> +
> +	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
> +	if (ret) {
> +		dev_err(dev, "failed to request irq %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = tve200_modeset_init(drm);
> +	if (ret)
> +		goto dev_unref;
> +
> +	ret = drm_dev_register(drm, 0);
> +	if (ret < 0)
> +		goto dev_unref;
> +
> +	return 0;
> +
> +clk_disable:
> +	clk_disable_unprepare(priv->pclk);
> +dev_unref:
> +	drm_dev_unref(drm);
> +	return ret;
> +}
> +
> +static int tve200_remove(struct platform_device *pdev)
> +{
> +	struct drm_device *drm = platform_get_drvdata(pdev);
> +	struct tve200_drm_dev_private *priv = drm->dev_private;
> +
> +	drm_dev_unregister(drm);
> +	if (priv->fbdev)
> +		drm_fbdev_cma_fini(priv->fbdev);
> +	drm_mode_config_cleanup(drm);
> +	clk_disable_unprepare(priv->pclk);
> +	drm_dev_unref(drm);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id tve200_of_match[] = {
> +	{
> +		.compatible = "faraday,tve200",
> +	},
> +	{},
> +};
> +
> +static struct platform_driver tve200_driver = {
> +	.driver = {
> +		.name           = "tve200",
> +		.of_match_table = of_match_ptr(tve200_of_match),
> +	},
> +	.probe = tve200_probe,
> +	.remove = tve200_remove,
> +};
> +module_platform_driver(tve200_driver);
> +
> +MODULE_DESCRIPTION(DRIVER_DESC);
> +MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
> +MODULE_LICENSE("GPL");

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH 1/4] drm/tve200: Add DT bindings
  2017-08-13 15:11     ` Linus Walleij
@ 2017-08-17 20:46       ` Rob Herring
  -1 siblings, 0 replies; 24+ messages in thread
From: Rob Herring @ 2017-08-17 20:46 UTC (permalink / raw)
  To: Linus Walleij; +Cc: devicetree, dri-devel, Daniel Vetter, linux-arm-kernel

On Sun, Aug 13, 2017 at 05:11:29PM +0200, Linus Walleij wrote:
> This adds device tree bindings for the Faraday TVE200 IP block.
> This IP block is present in the Gemini ARM SoC and also in some
> Grain Media GMxxxx SoCs.
> 
> Cc: devicetree@vger.kernel.org
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  .../devicetree/bindings/display/faraday,tve200.txt | 41 ++++++++++++++++++++++
>  1 file changed, 41 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt
> 
> diff --git a/Documentation/devicetree/bindings/display/faraday,tve200.txt b/Documentation/devicetree/bindings/display/faraday,tve200.txt
> new file mode 100644
> index 000000000000..95d588fbb2f2
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/display/faraday,tve200.txt
> @@ -0,0 +1,41 @@
> +* Faraday TV Encoder TVE200
> +
> +Required properties:
> +
> +- compatible: must be one of:
> +	"faraday,tve200"
> +	"cortina,gemini-tvc", "faraday,tve200"
> +
> +- reg: base address and size of the control registers block
> +
> +- interrupts: contains an interrupt specifier for the interrupt
> +	line from the TVE200
> +
> +- clock-names: should contain "PCLK" for the clock line clocking the
> +	silicon and "TVE" for the 27MHz clock to the video driver
> +
> +- clocks: contains phandle and clock specifier pairs for the entries
> +	in the clock-names property. See
> +	Documentation/devicetree/bindings/clock/clock-bindings.txt
> +
> +Optional properties:
> +
> +- resets: contains the reset line phandle for the block
> +
> +Required sub-nodes:
> +
> +- port: describes LCD panel signals, following the common binding
> +	for video transmitter interfaces; see
> +	Documentation/devicetree/bindings/media/video-interfaces.txt

Need to explicitly list ports and endpoints.

Also, needs to be in the example as it is required.

> +
> +Example:
> +
> +tvc@6a000000 {

display-controller@...

> +	compatible = "faraday,tve200";
> +	reg = <0x6a000000 0x1000>;
> +	interrupts = <13 IRQ_TYPE_EDGE_RISING>;
> +	resets = <&syscon GEMINI_RESET_TVC>;
> +	clocks = <&syscon GEMINI_CLK_GATE_TVC>,
> +		 <&syscon GEMINI_CLK_TVC>;
> +	clock-names = "PCLK", "TVE";
> +};
> -- 
> 2.13.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe devicetree" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* [PATCH 1/4] drm/tve200: Add DT bindings
@ 2017-08-17 20:46       ` Rob Herring
  0 siblings, 0 replies; 24+ messages in thread
From: Rob Herring @ 2017-08-17 20:46 UTC (permalink / raw)
  To: linux-arm-kernel

On Sun, Aug 13, 2017 at 05:11:29PM +0200, Linus Walleij wrote:
> This adds device tree bindings for the Faraday TVE200 IP block.
> This IP block is present in the Gemini ARM SoC and also in some
> Grain Media GMxxxx SoCs.
> 
> Cc: devicetree at vger.kernel.org
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  .../devicetree/bindings/display/faraday,tve200.txt | 41 ++++++++++++++++++++++
>  1 file changed, 41 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/display/faraday,tve200.txt
> 
> diff --git a/Documentation/devicetree/bindings/display/faraday,tve200.txt b/Documentation/devicetree/bindings/display/faraday,tve200.txt
> new file mode 100644
> index 000000000000..95d588fbb2f2
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/display/faraday,tve200.txt
> @@ -0,0 +1,41 @@
> +* Faraday TV Encoder TVE200
> +
> +Required properties:
> +
> +- compatible: must be one of:
> +	"faraday,tve200"
> +	"cortina,gemini-tvc", "faraday,tve200"
> +
> +- reg: base address and size of the control registers block
> +
> +- interrupts: contains an interrupt specifier for the interrupt
> +	line from the TVE200
> +
> +- clock-names: should contain "PCLK" for the clock line clocking the
> +	silicon and "TVE" for the 27MHz clock to the video driver
> +
> +- clocks: contains phandle and clock specifier pairs for the entries
> +	in the clock-names property. See
> +	Documentation/devicetree/bindings/clock/clock-bindings.txt
> +
> +Optional properties:
> +
> +- resets: contains the reset line phandle for the block
> +
> +Required sub-nodes:
> +
> +- port: describes LCD panel signals, following the common binding
> +	for video transmitter interfaces; see
> +	Documentation/devicetree/bindings/media/video-interfaces.txt

Need to explicitly list ports and endpoints.

Also, needs to be in the example as it is required.

> +
> +Example:
> +
> +tvc at 6a000000 {

display-controller at ...

> +	compatible = "faraday,tve200";
> +	reg = <0x6a000000 0x1000>;
> +	interrupts = <13 IRQ_TYPE_EDGE_RISING>;
> +	resets = <&syscon GEMINI_RESET_TVC>;
> +	clocks = <&syscon GEMINI_CLK_GATE_TVC>,
> +		 <&syscon GEMINI_CLK_TVC>;
> +	clock-names = "PCLK", "TVE";
> +};
> -- 
> 2.13.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe devicetree" in
> the body of a message to majordomo at vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 2/4] drm/tve200: Add new driver for TVE200
  2017-08-14 18:56     ` Eric Anholt
@ 2017-08-20 10:04       ` Linus Walleij
  -1 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-20 10:04 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 14, 2017 at 8:56 PM, Eric Anholt <eric@anholt.net> wrote:

> I also recommend checking out panel-bridge for deleting a bunch of the
> code,

Daniel said the same. I tried to look into this but the drivers currently
using it (like Atmel HLCDC) are so different using custom encoders and
what not that I can't really see clearly how to apply it in this case.

If you patch the PL111 driver to use the bridge helper I promise I will
follow up and patch this driver the same way though!

Yours,
Linus Walleij

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

* Re: [PATCH 2/4] drm/tve200: Add new driver for TVE200
@ 2017-08-20 10:04       ` Linus Walleij
  0 siblings, 0 replies; 24+ messages in thread
From: Linus Walleij @ 2017-08-20 10:04 UTC (permalink / raw)
  To: Eric Anholt; +Cc: linux-arm-kernel, Daniel Vetter, open list:DRM PANEL DRIVERS

On Mon, Aug 14, 2017 at 8:56 PM, Eric Anholt <eric@anholt.net> wrote:

> I also recommend checking out panel-bridge for deleting a bunch of the
> code,

Daniel said the same. I tried to look into this but the drivers currently
using it (like Atmel HLCDC) are so different using custom encoders and
what not that I can't really see clearly how to apply it in this case.

If you patch the PL111 driver to use the bridge helper I promise I will
follow up and patch this driver the same way though!

Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

end of thread, other threads:[~2017-08-20 10:04 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-13 15:11 [PATCH 0/4] DRM driver for Faraday TVE200 Linus Walleij
2017-08-13 15:11 ` Linus Walleij
     [not found] ` <20170813151132.24736-1-linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2017-08-13 15:11   ` [PATCH 1/4] drm/tve200: Add DT bindings Linus Walleij
2017-08-13 15:11     ` Linus Walleij
2017-08-17 20:46     ` Rob Herring
2017-08-17 20:46       ` Rob Herring
2017-08-13 15:11 ` [PATCH 2/4] drm/tve200: Add new driver for TVE200 Linus Walleij
2017-08-13 15:11   ` Linus Walleij
2017-08-14 14:20   ` Daniel Vetter
2017-08-14 14:20     ` Daniel Vetter
2017-08-15 20:02     ` Linus Walleij
2017-08-15 20:02       ` Linus Walleij
2017-08-15 20:08       ` Daniel Vetter
2017-08-15 20:08         ` Daniel Vetter
2017-08-14 18:56   ` Eric Anholt
2017-08-14 18:56     ` Eric Anholt
2017-08-20 10:04     ` Linus Walleij
2017-08-20 10:04       ` Linus Walleij
2017-08-15 21:49   ` Noralf Trønnes
2017-08-15 21:49     ` Noralf Trønnes
2017-08-13 15:11 ` [PATCH 3/4] ARM: dts: Add TVE200 to the Gemini SoC DTSI Linus Walleij
2017-08-13 15:11   ` Linus Walleij
2017-08-13 15:11 ` [PATCH 4/4] ARM: dts: Add TVE/TVC and ILI9322 panel to DIR-685 Linus Walleij
2017-08-13 15:11   ` Linus Walleij

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.