drivers: display_dummy: rework to obtain configuration from devicetree

Add bindings for dummy display controller.
Rework dummy display controller driver to obtain
configuration from devicetree. Remove unnecessary casts,
add multi-instance support.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
diff --git a/drivers/display/display_dummy.c b/drivers/display/display_dummy.c
index 5c83315..371bf77 100644
--- a/drivers/display/display_dummy.c
+++ b/drivers/display/display_dummy.c
@@ -1,19 +1,25 @@
 /*
  * Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
+ * Copyright (c) 2021 Nordic Semiconductor
  *
  * SPDX-License-Identifier: Apache-2.0
  */
 
-#include <string.h>
+#define DT_DRV_COMPAT zephyr_dummy_dc
 
+#include <string.h>
 #include <drivers/display.h>
+#include <device.h>
+
+struct dummy_display_config {
+	uint16_t height;
+	uint16_t width;
+};
 
 struct dummy_display_data {
 	enum display_pixel_format current_pixel_format;
 };
 
-static struct dummy_display_data dummy_display_data;
-
 static int dummy_display_init(const struct device *dev)
 {
 	struct dummy_display_data *disp_data =
@@ -29,19 +35,21 @@
 			       const struct display_buffer_descriptor *desc,
 			       const void *buf)
 {
+	const struct dummy_display_config *config = dev->config;
+
 	__ASSERT(desc->width <= desc->pitch, "Pitch is smaller then width");
-	__ASSERT(desc->pitch <= CONFIG_DUMMY_DISPLAY_X_RES,
+	__ASSERT(desc->pitch <= config->width,
 		"Pitch in descriptor is larger than screen size");
-	__ASSERT(desc->height <= CONFIG_DUMMY_DISPLAY_Y_RES,
+	__ASSERT(desc->height <= config->height,
 		"Height in descriptor is larger than screen size");
-	__ASSERT(x + desc->pitch <= CONFIG_DUMMY_DISPLAY_X_RES,
+	__ASSERT(x + desc->pitch <= config->width,
 		 "Writing outside screen boundaries in horizontal direction");
-	__ASSERT(y + desc->height <= CONFIG_DUMMY_DISPLAY_Y_RES,
+	__ASSERT(y + desc->height <= config->height,
 		 "Writing outside screen boundaries in vertical direction");
 
 	if (desc->width > desc->pitch ||
-	    x + desc->pitch > CONFIG_DUMMY_DISPLAY_X_RES ||
-	    y + desc->height > CONFIG_DUMMY_DISPLAY_Y_RES) {
+	    x + desc->pitch > config->width ||
+	    y + desc->height > config->height) {
 		return -EINVAL;
 	}
 
@@ -86,12 +94,12 @@
 static void dummy_display_get_capabilities(const struct device *dev,
 		struct display_capabilities *capabilities)
 {
-	struct dummy_display_data *disp_data =
-		(struct dummy_display_data *)dev->data;
+	const struct dummy_display_config *config = dev->config;
+	struct dummy_display_data *disp_data = dev->data;
 
 	memset(capabilities, 0, sizeof(struct display_capabilities));
-	capabilities->x_resolution = CONFIG_DUMMY_DISPLAY_X_RES;
-	capabilities->y_resolution = CONFIG_DUMMY_DISPLAY_Y_RES;
+	capabilities->x_resolution = config->width;
+	capabilities->y_resolution = config->height;
 	capabilities->supported_pixel_formats = PIXEL_FORMAT_ARGB_8888 |
 		PIXEL_FORMAT_RGB_888 |
 		PIXEL_FORMAT_MONO01 |
@@ -104,8 +112,7 @@
 static int dummy_display_set_pixel_format(const struct device *dev,
 		const enum display_pixel_format pixel_format)
 {
-	struct dummy_display_data *disp_data =
-		(struct dummy_display_data *)dev->data;
+	struct dummy_display_data *disp_data = dev->data;
 
 	disp_data->current_pixel_format = pixel_format;
 	return 0;
@@ -123,8 +130,19 @@
 	.set_pixel_format = dummy_display_set_pixel_format,
 };
 
-DEVICE_DEFINE(dummy_display, CONFIG_DUMMY_DISPLAY_DEV_NAME,
-		    &dummy_display_init, NULL,
-		    &dummy_display_data, NULL,
-		    APPLICATION, CONFIG_DISPLAY_INIT_PRIORITY,
-		    &dummy_display_api);
+#define DISPLAY_DUMMY_DEFINE(n)						\
+	static const struct dummy_display_config dd_config_##n = {	\
+		.height = DT_INST_PROP(n, height),			\
+		.width = DT_INST_PROP(n, width),			\
+	};								\
+									\
+	static struct dummy_display_data dd_data_##n;			\
+									\
+	DEVICE_DT_INST_DEFINE(n, &dummy_display_init, NULL,		\
+			      &dd_data_##n,				\
+			      &dd_config_##n,				\
+			      APPLICATION,				\
+			      CONFIG_DISPLAY_INIT_PRIORITY,		\
+			      &dummy_display_api);			\
+
+DT_INST_FOREACH_STATUS_OKAY(DISPLAY_DUMMY_DEFINE)
diff --git a/dts/bindings/display/zephyr,dummy-dc.yaml b/dts/bindings/display/zephyr,dummy-dc.yaml
new file mode 100644
index 0000000..fde1444
--- /dev/null
+++ b/dts/bindings/display/zephyr,dummy-dc.yaml
@@ -0,0 +1,8 @@
+# Copyright (c) 2021 Nordic Semiconductor ASA
+# SPDX-License-Identifier: Apache-2.0
+
+description: Dummy display controller
+
+compatible: "zephyr,dummy-dc"
+
+include: display-controller.yaml
diff --git a/samples/drivers/display/dummy_dc.overlay b/samples/drivers/display/dummy_dc.overlay
new file mode 100644
index 0000000..7b143f8
--- /dev/null
+++ b/samples/drivers/display/dummy_dc.overlay
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/ {
+	chosen {
+		zephyr,display = &dummy_dc;
+	};
+
+	dummy_dc: dummy_dc {
+		compatible = "zephyr,dummy-dc";
+		label = "DISPLAY";
+		height = <240>;
+		width = <320>;
+	};
+};
diff --git a/samples/drivers/display/sample.yaml b/samples/drivers/display/sample.yaml
index 94ea697..95c6888 100644
--- a/samples/drivers/display/sample.yaml
+++ b/samples/drivers/display/sample.yaml
@@ -56,6 +56,7 @@
     tags: display
   sample.display.dummy:
     platform_allow: native_posix
+    extra_args: DTC_OVERLAY_FILE="dummy_dc.overlay"
     extra_configs:
         - CONFIG_DUMMY_DISPLAY=y
         - CONFIG_SDL_DISPLAY=n
diff --git a/samples/subsys/display/lvgl/dummy_dc.overlay b/samples/subsys/display/lvgl/dummy_dc.overlay
new file mode 100644
index 0000000..7b143f8
--- /dev/null
+++ b/samples/subsys/display/lvgl/dummy_dc.overlay
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/ {
+	chosen {
+		zephyr,display = &dummy_dc;
+	};
+
+	dummy_dc: dummy_dc {
+		compatible = "zephyr,dummy-dc";
+		label = "DISPLAY";
+		height = <240>;
+		width = <320>;
+	};
+};
diff --git a/samples/subsys/display/lvgl/sample.yaml b/samples/subsys/display/lvgl/sample.yaml
index a159d5a..e3eb080 100644
--- a/samples/subsys/display/lvgl/sample.yaml
+++ b/samples/subsys/display/lvgl/sample.yaml
@@ -40,6 +40,7 @@
   sample.display.dummy:
     build_only: true
     platform_allow: native_posix
+    extra_args: DTC_OVERLAY_FILE="dummy_dc.overlay"
     extra_configs:
       - CONFIG_DUMMY_DISPLAY=y
       - CONFIG_DUMMY_DISPLAY_DEV_NAME="DISPLAY"
diff --git a/tests/lib/gui/lvgl/app.overlay b/tests/lib/gui/lvgl/app.overlay
new file mode 100644
index 0000000..7b143f8
--- /dev/null
+++ b/tests/lib/gui/lvgl/app.overlay
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/ {
+	chosen {
+		zephyr,display = &dummy_dc;
+	};
+
+	dummy_dc: dummy_dc {
+		compatible = "zephyr,dummy-dc";
+		label = "DISPLAY";
+		height = <240>;
+		width = <320>;
+	};
+};