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