1 // SPDX-License-Identifier: GPL-2.0+
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_edid.h>
6 #include <drm/drm_probe_helper.h>
8 static const struct drm_connector_funcs vkms_connector_funcs = {
9 .fill_modes = drm_helper_probe_single_connector_modes,
10 .destroy = drm_connector_cleanup,
11 .reset = drm_atomic_helper_connector_reset,
12 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
16 static const struct drm_encoder_funcs vkms_encoder_funcs = {
17 .destroy = drm_encoder_cleanup,
20 static int vkms_conn_get_modes(struct drm_connector *connector)
24 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
25 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
30 static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
31 .get_modes = vkms_conn_get_modes,
34 static int vkms_add_overlay_plane(struct vkms_device *vkmsdev, int index,
35 struct drm_crtc *crtc)
37 struct vkms_plane *overlay;
39 overlay = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_OVERLAY, index);
41 return PTR_ERR(overlay);
43 if (!overlay->base.possible_crtcs)
44 overlay->base.possible_crtcs = drm_crtc_mask(crtc);
49 int vkms_output_init(struct vkms_device *vkmsdev, int index)
51 struct vkms_output *output = &vkmsdev->output;
52 struct drm_device *dev = &vkmsdev->drm;
53 struct drm_connector *connector = &output->connector;
54 struct drm_encoder *encoder = &output->encoder;
55 struct drm_crtc *crtc = &output->crtc;
56 struct vkms_plane *primary, *cursor = NULL;
61 primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY, index);
63 return PTR_ERR(primary);
65 if (vkmsdev->config->overlay) {
66 for (n = 0; n < NUM_OVERLAY_PLANES; n++) {
67 ret = vkms_add_overlay_plane(vkmsdev, index, crtc);
73 if (vkmsdev->config->cursor) {
74 cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR, index);
76 return PTR_ERR(cursor);
79 ret = vkms_crtc_init(dev, crtc, &primary->base, &cursor->base);
83 ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
84 DRM_MODE_CONNECTOR_VIRTUAL);
86 DRM_ERROR("Failed to init connector\n");
90 drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
92 ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
93 DRM_MODE_ENCODER_VIRTUAL, NULL);
95 DRM_ERROR("Failed to init encoder\n");
98 encoder->possible_crtcs = 1;
100 ret = drm_connector_attach_encoder(connector, encoder);
102 DRM_ERROR("Failed to attach connector to encoder\n");
106 if (vkmsdev->config->writeback) {
107 writeback = vkms_enable_writeback_connector(vkmsdev);
109 DRM_ERROR("Failed to init writeback connector\n");
112 drm_mode_config_reset(dev);
117 drm_encoder_cleanup(encoder);
120 drm_connector_cleanup(connector);
123 drm_crtc_cleanup(crtc);