xref: /linux/drivers/gpu/drm/vkms/vkms_output.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
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;
53 	int ret;
54 
55 	primary = vkms_plane_init(vkmsdev);
56 	if (IS_ERR(primary))
57 		return PTR_ERR(primary);
58 
59 	ret = vkms_crtc_init(dev, crtc, primary, NULL);
60 	if (ret)
61 		goto err_crtc;
62 
63 	ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
64 				 DRM_MODE_CONNECTOR_VIRTUAL);
65 	if (ret) {
66 		DRM_ERROR("Failed to init connector\n");
67 		goto err_connector;
68 	}
69 
70 	drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
71 
72 	ret = drm_connector_register(connector);
73 	if (ret) {
74 		DRM_ERROR("Failed to register connector\n");
75 		goto err_connector_register;
76 	}
77 
78 	ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
79 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
80 	if (ret) {
81 		DRM_ERROR("Failed to init encoder\n");
82 		goto err_encoder;
83 	}
84 	encoder->possible_crtcs = 1;
85 
86 	ret = drm_connector_attach_encoder(connector, encoder);
87 	if (ret) {
88 		DRM_ERROR("Failed to attach connector to encoder\n");
89 		goto err_attach;
90 	}
91 
92 	drm_mode_config_reset(dev);
93 
94 	return 0;
95 
96 err_attach:
97 	drm_encoder_cleanup(encoder);
98 
99 err_encoder:
100 	drm_connector_unregister(connector);
101 
102 err_connector_register:
103 	drm_connector_cleanup(connector);
104 
105 err_connector:
106 	drm_crtc_cleanup(crtc);
107 
108 err_crtc:
109 	drm_plane_cleanup(primary);
110 	return ret;
111 }
112