dm: Add a dsi host uclass

Display Serial Interface (DSI) host can usefully be modelled
as their own uclass.
DSI defines a serial bus and a communication protocol
between the host and the device (panel, bridge).

Signed-off-by: Yannick Fertré <yannick.fertre@st.com>
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index 36f666e..cdaf616 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -686,6 +686,17 @@
 	  rather requires a SoC-specific glue driver to call it), it
 	  can not be enabled from the configuration menu.
 
+config VIDEO_DSI_HOST_SANDBOX
+	bool "Enable sandbox for dsi host"
+	depends on SANDBOX
+	select VIDEO_MIPI_DSI
+	help
+	  Enable support for sandbox dsi host device used for testing
+	  purposes.
+	  Display Serial Interface (DSI) defines a serial bus and
+	  a communication protocol between the host and the device
+	  (panel, bridge).
+
 config VIDEO_SIMPLE
 	bool "Simple display driver for preconfigured display"
 	help
diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index 7df9b0b..07aaca9 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -11,6 +11,7 @@
 obj-$(CONFIG_CONSOLE_TRUETYPE) += console_truetype.o fonts/
 obj-$(CONFIG_DISPLAY) += display-uclass.o
 obj-$(CONFIG_DM_VIDEO) += backlight-uclass.o
+obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi-host-uclass.o
 obj-$(CONFIG_DM_VIDEO) += panel-uclass.o simple_panel.o
 obj-$(CONFIG_DM_VIDEO) += video-uclass.o vidconsole-uclass.o
 obj-$(CONFIG_DM_VIDEO) += video_bmp.o
@@ -58,6 +59,7 @@
 obj-$(CONFIG_VIDEO_MX3) += mx3fb.o videomodes.o
 obj-$(CONFIG_VIDEO_MXS) += mxsfb.o videomodes.o
 obj-$(CONFIG_VIDEO_OMAP3) += omap3_dss.o
+obj-$(CONFIG_VIDEO_DSI_HOST_SANDBOX) += sandbox_dsi_host.o
 obj-$(CONFIG_VIDEO_SANDBOX_SDL) += sandbox_sdl.o
 obj-$(CONFIG_VIDEO_SIMPLE) += simplefb.o
 obj-$(CONFIG_VIDEO_TEGRA20) += tegra.o
diff --git a/drivers/video/dsi-host-uclass.c b/drivers/video/dsi-host-uclass.c
new file mode 100644
index 0000000..1db1f88
--- /dev/null
+++ b/drivers/video/dsi-host-uclass.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019 STMicroelectronics - All Rights Reserved
+ * Author(s): Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
+ *
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dsi_host.h>
+
+int dsi_host_init(struct udevice *dev,
+		  struct mipi_dsi_device *device,
+		  struct display_timing *timings,
+		  unsigned int max_data_lanes,
+		  const struct mipi_dsi_phy_ops *phy_ops)
+{
+	struct dsi_host_ops *ops = dsi_host_get_ops(dev);
+
+	if (!ops->init)
+		return -ENOSYS;
+
+	return ops->init(dev, device, timings, max_data_lanes, phy_ops);
+}
+
+int dsi_host_enable(struct udevice *dev)
+{
+	struct dsi_host_ops *ops = dsi_host_get_ops(dev);
+
+	if (!ops->enable)
+		return -ENOSYS;
+
+	return ops->enable(dev);
+}
+
+UCLASS_DRIVER(dsi_host) = {
+	.id		= UCLASS_DSI_HOST,
+	.name		= "dsi_host",
+};
diff --git a/drivers/video/sandbox_dsi_host.c b/drivers/video/sandbox_dsi_host.c
new file mode 100644
index 0000000..cd644ec
--- /dev/null
+++ b/drivers/video/sandbox_dsi_host.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
+/*
+ * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
+ */
+
+#include <common.h>
+#include <display.h>
+#include <dm.h>
+#include <dsi_host.h>
+
+/**
+ * struct sandbox_dsi_host_priv - private data for driver
+ * @device: DSI peripheral device
+ * @timing: Display timings
+ * @max_data_lanes: maximum number of data lines
+ * @phy_ops: set of function pointers for performing physical operations
+ */
+struct sandbox_dsi_host_priv {
+	struct mipi_dsi_device *device;
+	struct display_timing *timings;
+	unsigned int max_data_lanes;
+	const struct mipi_dsi_phy_ops *phy_ops;
+};
+
+static int sandbox_dsi_host_init(struct udevice *dev,
+				 struct mipi_dsi_device *device,
+				 struct display_timing *timings,
+				 unsigned int max_data_lanes,
+				 const struct mipi_dsi_phy_ops *phy_ops)
+{
+	struct sandbox_dsi_host_priv *priv = dev_get_priv(dev);
+
+	if (!device)
+		return -1;
+
+	if (!timings)
+		return -2;
+
+	if (max_data_lanes == 0)
+		return -3;
+
+	if (!phy_ops)
+		return -4;
+
+	if (!phy_ops->init || !phy_ops->get_lane_mbps ||
+	    !phy_ops->post_set_mode)
+		return -5;
+
+	priv->max_data_lanes = max_data_lanes;
+	priv->phy_ops = phy_ops;
+	priv->timings = timings;
+	priv->device = device;
+
+	return 0;
+}
+
+static int sandbox_dsi_host_enable(struct udevice *dev)
+{
+	struct sandbox_dsi_host_priv *priv = dev_get_priv(dev);
+	unsigned int lane_mbps;
+	int ret;
+
+	priv->phy_ops->init(priv->device);
+	ret = priv->phy_ops->get_lane_mbps(priv->device, priv->timings, 2,
+					   MIPI_DSI_FMT_RGB888, &lane_mbps);
+	if (ret)
+		return -1;
+
+	priv->phy_ops->post_set_mode(priv->device, MIPI_DSI_MODE_VIDEO);
+
+	return 0;
+}
+
+struct dsi_host_ops sandbox_dsi_host_ops = {
+	.init = sandbox_dsi_host_init,
+	.enable = sandbox_dsi_host_enable,
+};
+
+static const struct udevice_id sandbox_dsi_host_ids[] = {
+	{ .compatible = "sandbox,dsi-host"},
+	{ }
+};
+
+U_BOOT_DRIVER(sandbox_dsi_host) = {
+	.name		      = "sandbox-dsi-host",
+	.id		      = UCLASS_DSI_HOST,
+	.of_match	      = sandbox_dsi_host_ids,
+	.ops		      = &sandbox_dsi_host_ops,
+	.priv_auto_alloc_size = sizeof(struct sandbox_dsi_host_priv),
+};