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