xref: /linux/drivers/gpu/drm/vkms/vkms_output.c (revision 2df7af93fdadb9ba8226fe443fae15ecdefda2a6)
17fd56e02SRodrigo Siqueira // SPDX-License-Identifier: GPL-2.0+
2854502faSRodrigo Siqueira 
3854502faSRodrigo Siqueira #include "vkms_drv.h"
4d1648930SRodrigo Siqueira #include <drm/drm_atomic_helper.h>
5fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
6d1956157SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
7854502faSRodrigo Siqueira 
8854502faSRodrigo Siqueira static void vkms_connector_destroy(struct drm_connector *connector)
9854502faSRodrigo Siqueira {
10854502faSRodrigo Siqueira 	drm_connector_cleanup(connector);
11854502faSRodrigo Siqueira }
12854502faSRodrigo Siqueira 
13854502faSRodrigo Siqueira static const struct drm_connector_funcs vkms_connector_funcs = {
14854502faSRodrigo Siqueira 	.fill_modes = drm_helper_probe_single_connector_modes,
15854502faSRodrigo Siqueira 	.destroy = vkms_connector_destroy,
16d1648930SRodrigo Siqueira 	.reset = drm_atomic_helper_connector_reset,
17d1648930SRodrigo Siqueira 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
18d1648930SRodrigo Siqueira 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
19854502faSRodrigo Siqueira };
20854502faSRodrigo Siqueira 
21d1648930SRodrigo Siqueira static int vkms_conn_get_modes(struct drm_connector *connector)
22d1648930SRodrigo Siqueira {
23d1648930SRodrigo Siqueira 	int count;
24d1648930SRodrigo Siqueira 
25d1648930SRodrigo Siqueira 	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
26d1648930SRodrigo Siqueira 	drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
27d1648930SRodrigo Siqueira 
28d1648930SRodrigo Siqueira 	return count;
29d1648930SRodrigo Siqueira }
30d1648930SRodrigo Siqueira 
31d1648930SRodrigo Siqueira static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
32d1648930SRodrigo Siqueira 	.get_modes    = vkms_conn_get_modes,
33d1648930SRodrigo Siqueira };
34d1648930SRodrigo Siqueira 
35e9d85f73SRodrigo Siqueira int vkms_output_init(struct vkms_device *vkmsdev, int index)
36854502faSRodrigo Siqueira {
37854502faSRodrigo Siqueira 	struct vkms_output *output = &vkmsdev->output;
38854502faSRodrigo Siqueira 	struct drm_device *dev = &vkmsdev->drm;
39854502faSRodrigo Siqueira 	struct drm_connector *connector = &output->connector;
40854502faSRodrigo Siqueira 	struct drm_encoder *encoder = &output->encoder;
41854502faSRodrigo Siqueira 	struct drm_crtc *crtc = &output->crtc;
42c27d931dSHaneen Mohammed 	struct drm_plane *primary, *cursor = NULL;
43854502faSRodrigo Siqueira 	int ret;
44854502faSRodrigo Siqueira 
45e9d85f73SRodrigo Siqueira 	primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY, index);
46854502faSRodrigo Siqueira 	if (IS_ERR(primary))
47854502faSRodrigo Siqueira 		return PTR_ERR(primary);
48854502faSRodrigo Siqueira 
49*2df7af93SSumera Priyadarsini 	if (vkmsdev->config->cursor) {
50e9d85f73SRodrigo Siqueira 		cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR, index);
51c27d931dSHaneen Mohammed 		if (IS_ERR(cursor)) {
52c27d931dSHaneen Mohammed 			ret = PTR_ERR(cursor);
53c27d931dSHaneen Mohammed 			goto err_cursor;
54c27d931dSHaneen Mohammed 		}
55b8789ea7SHaneen Mohammed 	}
56c27d931dSHaneen Mohammed 
57c27d931dSHaneen Mohammed 	ret = vkms_crtc_init(dev, crtc, primary, cursor);
58854502faSRodrigo Siqueira 	if (ret)
59854502faSRodrigo Siqueira 		goto err_crtc;
60854502faSRodrigo Siqueira 
61854502faSRodrigo Siqueira 	ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
62854502faSRodrigo Siqueira 				 DRM_MODE_CONNECTOR_VIRTUAL);
63854502faSRodrigo Siqueira 	if (ret) {
64854502faSRodrigo Siqueira 		DRM_ERROR("Failed to init connector\n");
65854502faSRodrigo Siqueira 		goto err_connector;
66854502faSRodrigo Siqueira 	}
67854502faSRodrigo Siqueira 
68d1648930SRodrigo Siqueira 	drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
69d1648930SRodrigo Siqueira 
70d1956157SThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
71854502faSRodrigo Siqueira 	if (ret) {
72854502faSRodrigo Siqueira 		DRM_ERROR("Failed to init encoder\n");
73854502faSRodrigo Siqueira 		goto err_encoder;
74854502faSRodrigo Siqueira 	}
75854502faSRodrigo Siqueira 	encoder->possible_crtcs = 1;
76854502faSRodrigo Siqueira 
77cde4c44dSDaniel Vetter 	ret = drm_connector_attach_encoder(connector, encoder);
78854502faSRodrigo Siqueira 	if (ret) {
79854502faSRodrigo Siqueira 		DRM_ERROR("Failed to attach connector to encoder\n");
80854502faSRodrigo Siqueira 		goto err_attach;
81854502faSRodrigo Siqueira 	}
82854502faSRodrigo Siqueira 
83dbd9d80cSRodrigo Siqueira 	ret = vkms_enable_writeback_connector(vkmsdev);
84dbd9d80cSRodrigo Siqueira 	if (ret)
85dbd9d80cSRodrigo Siqueira 		DRM_ERROR("Failed to init writeback connector\n");
86dbd9d80cSRodrigo Siqueira 
87854502faSRodrigo Siqueira 	drm_mode_config_reset(dev);
88854502faSRodrigo Siqueira 
89854502faSRodrigo Siqueira 	return 0;
90854502faSRodrigo Siqueira 
91854502faSRodrigo Siqueira err_attach:
92854502faSRodrigo Siqueira 	drm_encoder_cleanup(encoder);
93854502faSRodrigo Siqueira 
94854502faSRodrigo Siqueira err_encoder:
95854502faSRodrigo Siqueira 	drm_connector_cleanup(connector);
96854502faSRodrigo Siqueira 
97854502faSRodrigo Siqueira err_connector:
98854502faSRodrigo Siqueira 	drm_crtc_cleanup(crtc);
99854502faSRodrigo Siqueira 
100854502faSRodrigo Siqueira err_crtc:
101*2df7af93SSumera Priyadarsini 	if (vkmsdev->config->cursor)
102c27d931dSHaneen Mohammed 		drm_plane_cleanup(cursor);
103c27d931dSHaneen Mohammed 
104c27d931dSHaneen Mohammed err_cursor:
105854502faSRodrigo Siqueira 	drm_plane_cleanup(primary);
106c27d931dSHaneen Mohammed 
107854502faSRodrigo Siqueira 	return ret;
108854502faSRodrigo Siqueira }
109