1 /*
2  * Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  * Copyright (c) 2021 Nordic Semiconductor
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define DT_DRV_COMPAT zephyr_dummy_dc
9 
10 #include <errno.h>
11 #include <string.h>
12 
13 #include <zephyr/drivers/display.h>
14 #include <zephyr/device.h>
15 
16 struct dummy_display_config {
17 	uint16_t height;
18 	uint16_t width;
19 };
20 
21 struct dummy_display_data {
22 	enum display_pixel_format current_pixel_format;
23 };
24 
dummy_display_init(const struct device * dev)25 static int dummy_display_init(const struct device *dev)
26 {
27 	struct dummy_display_data *disp_data = dev->data;
28 
29 	disp_data->current_pixel_format = PIXEL_FORMAT_ARGB_8888;
30 
31 	return 0;
32 }
33 
dummy_display_write(const struct device * dev,const uint16_t x,const uint16_t y,const struct display_buffer_descriptor * desc,const void * buf)34 static int dummy_display_write(const struct device *dev, const uint16_t x,
35 			       const uint16_t y,
36 			       const struct display_buffer_descriptor *desc,
37 			       const void *buf)
38 {
39 	const struct dummy_display_config *config = dev->config;
40 
41 	__ASSERT(desc->width <= desc->pitch, "Pitch is smaller than width");
42 	__ASSERT(desc->pitch <= config->width,
43 		"Pitch in descriptor is larger than screen size");
44 	__ASSERT(desc->height <= config->height,
45 		"Height in descriptor is larger than screen size");
46 	__ASSERT(x + desc->pitch <= config->width,
47 		 "Writing outside screen boundaries in horizontal direction");
48 	__ASSERT(y + desc->height <= config->height,
49 		 "Writing outside screen boundaries in vertical direction");
50 
51 	if (desc->width > desc->pitch ||
52 	    x + desc->pitch > config->width ||
53 	    y + desc->height > config->height) {
54 		return -EINVAL;
55 	}
56 
57 	return 0;
58 }
59 
dummy_display_blanking_off(const struct device * dev)60 static int dummy_display_blanking_off(const struct device *dev)
61 {
62 	return 0;
63 }
64 
dummy_display_blanking_on(const struct device * dev)65 static int dummy_display_blanking_on(const struct device *dev)
66 {
67 	return 0;
68 }
69 
dummy_display_set_brightness(const struct device * dev,const uint8_t brightness)70 static int dummy_display_set_brightness(const struct device *dev,
71 					const uint8_t brightness)
72 {
73 	return 0;
74 }
75 
dummy_display_set_contrast(const struct device * dev,const uint8_t contrast)76 static int dummy_display_set_contrast(const struct device *dev,
77 				      const uint8_t contrast)
78 {
79 	return 0;
80 }
81 
dummy_display_get_capabilities(const struct device * dev,struct display_capabilities * capabilities)82 static void dummy_display_get_capabilities(const struct device *dev,
83 		struct display_capabilities *capabilities)
84 {
85 	const struct dummy_display_config *config = dev->config;
86 	struct dummy_display_data *disp_data = dev->data;
87 
88 	memset(capabilities, 0, sizeof(struct display_capabilities));
89 	capabilities->x_resolution = config->width;
90 	capabilities->y_resolution = config->height;
91 	capabilities->supported_pixel_formats = PIXEL_FORMAT_ARGB_8888 |
92 		PIXEL_FORMAT_RGB_888 |
93 		PIXEL_FORMAT_MONO01 |
94 		PIXEL_FORMAT_MONO10;
95 	capabilities->current_pixel_format = disp_data->current_pixel_format;
96 	capabilities->screen_info = SCREEN_INFO_MONO_VTILED |
97 		SCREEN_INFO_MONO_MSB_FIRST;
98 }
99 
dummy_display_set_pixel_format(const struct device * dev,const enum display_pixel_format pixel_format)100 static int dummy_display_set_pixel_format(const struct device *dev,
101 		const enum display_pixel_format pixel_format)
102 {
103 	struct dummy_display_data *disp_data = dev->data;
104 
105 	disp_data->current_pixel_format = pixel_format;
106 	return 0;
107 }
108 
109 static DEVICE_API(display, dummy_display_api) = {
110 	.blanking_on = dummy_display_blanking_on,
111 	.blanking_off = dummy_display_blanking_off,
112 	.write = dummy_display_write,
113 	.set_brightness = dummy_display_set_brightness,
114 	.set_contrast = dummy_display_set_contrast,
115 	.get_capabilities = dummy_display_get_capabilities,
116 	.set_pixel_format = dummy_display_set_pixel_format,
117 };
118 
119 #define DISPLAY_DUMMY_DEFINE(n)						\
120 	static const struct dummy_display_config dd_config_##n = {	\
121 		.height = DT_INST_PROP(n, height),			\
122 		.width = DT_INST_PROP(n, width),			\
123 	};								\
124 									\
125 	static struct dummy_display_data dd_data_##n;			\
126 									\
127 	DEVICE_DT_INST_DEFINE(n, &dummy_display_init, NULL,		\
128 			      &dd_data_##n,				\
129 			      &dd_config_##n,				\
130 			      POST_KERNEL,				\
131 			      CONFIG_DISPLAY_INIT_PRIORITY,		\
132 			      &dummy_display_api);			\
133 
134 DT_INST_FOREACH_STATUS_OKAY(DISPLAY_DUMMY_DEFINE)
135