xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_output.c (revision e907bf3c)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include "vkms_drv.h"
4 #include <drm/drm_crtc_helper.h>
5 #include <drm/drm_atomic_helper.h>
6 
7 static void vkms_connector_destroy(struct drm_connector *connector)
8 {
9 	drm_connector_unregister(connector);
10 	drm_connector_cleanup(connector);
11 }
12 
13 static const struct drm_connector_funcs vkms_connector_funcs = {
14 	.fill_modes = drm_helper_probe_single_connector_modes,
15 	.destroy = vkms_connector_destroy,
16 	.reset = drm_atomic_helper_connector_reset,
17 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
18 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
19 };
20 
21 static const struct drm_encoder_funcs vkms_encoder_funcs = {
22 	.destroy = drm_encoder_cleanup,
23 };
24 
25 static int vkms_conn_get_modes(struct drm_connector *connector)
26 {
27 	int count;
28 
29 	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
30 	drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
31 
32 	return count;
33 }
34 
35 static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
36 	.get_modes    = vkms_conn_get_modes,
37 };
38 
39 int vkms_output_init(struct vkms_device *vkmsdev)
40 {
41 	struct vkms_output *output = &vkmsdev->output;
42 	struct drm_device *dev = &vkmsdev->drm;
43 	struct drm_connector *connector = &output->connector;
44 	struct drm_encoder *encoder = &output->encoder;
45 	struct drm_crtc *crtc = &output->crtc;
46 	struct drm_plane *primary, *cursor = NULL;
47 	int ret;
48 
49 	primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY);
50 	if (IS_ERR(primary))
51 		return PTR_ERR(primary);
52 
53 	if (enable_cursor) {
54 		cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR);
55 		if (IS_ERR(cursor)) {
56 			ret = PTR_ERR(cursor);
57 			goto err_cursor;
58 		}
59 	}
60 
61 	ret = vkms_crtc_init(dev, crtc, primary, cursor);
62 	if (ret)
63 		goto err_crtc;
64 
65 	ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
66 				 DRM_MODE_CONNECTOR_VIRTUAL);
67 	if (ret) {
68 		DRM_ERROR("Failed to init connector\n");
69 		goto err_connector;
70 	}
71 
72 	drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
73 
74 	ret = drm_connector_register(connector);
75 	if (ret) {
76 		DRM_ERROR("Failed to register connector\n");
77 		goto err_connector_register;
78 	}
79 
80 	ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
81 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
82 	if (ret) {
83 		DRM_ERROR("Failed to init encoder\n");
84 		goto err_encoder;
85 	}
86 	encoder->possible_crtcs = 1;
87 
88 	ret = drm_connector_attach_encoder(connector, encoder);
89 	if (ret) {
90 		DRM_ERROR("Failed to attach connector to encoder\n");
91 		goto err_attach;
92 	}
93 
94 	drm_mode_config_reset(dev);
95 
96 	return 0;
97 
98 err_attach:
99 	drm_encoder_cleanup(encoder);
100 
101 err_encoder:
102 	drm_connector_unregister(connector);
103 
104 err_connector_register:
105 	drm_connector_cleanup(connector);
106 
107 err_connector:
108 	drm_crtc_cleanup(crtc);
109 
110 err_crtc:
111 	if (enable_cursor)
112 		drm_plane_cleanup(cursor);
113 
114 err_cursor:
115 	drm_plane_cleanup(primary);
116 
117 	return ret;
118 }
119