1854502faSRodrigo Siqueira // SPDX-License-Identifier: GPL-2.0 2854502faSRodrigo Siqueira /* 3854502faSRodrigo Siqueira * This program is free software; you can redistribute it and/or modify 4854502faSRodrigo Siqueira * it under the terms of the GNU General Public License as published by 5854502faSRodrigo Siqueira * the Free Software Foundation; either version 2 of the License, or 6854502faSRodrigo Siqueira * (at your option) any later version. 7854502faSRodrigo Siqueira */ 8854502faSRodrigo Siqueira 9854502faSRodrigo Siqueira #include "vkms_drv.h" 10854502faSRodrigo Siqueira #include <drm/drm_crtc_helper.h> 11d1648930SRodrigo Siqueira #include <drm/drm_atomic_helper.h> 12854502faSRodrigo Siqueira 13854502faSRodrigo Siqueira static void vkms_connector_destroy(struct drm_connector *connector) 14854502faSRodrigo Siqueira { 15854502faSRodrigo Siqueira drm_connector_unregister(connector); 16854502faSRodrigo Siqueira drm_connector_cleanup(connector); 17854502faSRodrigo Siqueira } 18854502faSRodrigo Siqueira 19854502faSRodrigo Siqueira static const struct drm_connector_funcs vkms_connector_funcs = { 20854502faSRodrigo Siqueira .fill_modes = drm_helper_probe_single_connector_modes, 21854502faSRodrigo Siqueira .destroy = vkms_connector_destroy, 22d1648930SRodrigo Siqueira .reset = drm_atomic_helper_connector_reset, 23d1648930SRodrigo Siqueira .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 24d1648930SRodrigo Siqueira .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 25854502faSRodrigo Siqueira }; 26854502faSRodrigo Siqueira 27854502faSRodrigo Siqueira static const struct drm_encoder_funcs vkms_encoder_funcs = { 28854502faSRodrigo Siqueira .destroy = drm_encoder_cleanup, 29854502faSRodrigo Siqueira }; 30854502faSRodrigo Siqueira 31d1648930SRodrigo Siqueira static int vkms_conn_get_modes(struct drm_connector *connector) 32d1648930SRodrigo Siqueira { 33d1648930SRodrigo Siqueira int count; 34d1648930SRodrigo Siqueira 35d1648930SRodrigo Siqueira count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX); 36d1648930SRodrigo Siqueira drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF); 37d1648930SRodrigo Siqueira 38d1648930SRodrigo Siqueira return count; 39d1648930SRodrigo Siqueira } 40d1648930SRodrigo Siqueira 41d1648930SRodrigo Siqueira static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = { 42d1648930SRodrigo Siqueira .get_modes = vkms_conn_get_modes, 43d1648930SRodrigo Siqueira }; 44d1648930SRodrigo Siqueira 45854502faSRodrigo Siqueira int vkms_output_init(struct vkms_device *vkmsdev) 46854502faSRodrigo Siqueira { 47854502faSRodrigo Siqueira struct vkms_output *output = &vkmsdev->output; 48854502faSRodrigo Siqueira struct drm_device *dev = &vkmsdev->drm; 49854502faSRodrigo Siqueira struct drm_connector *connector = &output->connector; 50854502faSRodrigo Siqueira struct drm_encoder *encoder = &output->encoder; 51854502faSRodrigo Siqueira struct drm_crtc *crtc = &output->crtc; 52c27d931dSHaneen Mohammed struct drm_plane *primary, *cursor = NULL; 53854502faSRodrigo Siqueira int ret; 54854502faSRodrigo Siqueira 55c27d931dSHaneen Mohammed primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY); 56854502faSRodrigo Siqueira if (IS_ERR(primary)) 57854502faSRodrigo Siqueira return PTR_ERR(primary); 58854502faSRodrigo Siqueira 59*b8789ea7SHaneen Mohammed if (enable_cursor) { 60c27d931dSHaneen Mohammed cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR); 61c27d931dSHaneen Mohammed if (IS_ERR(cursor)) { 62c27d931dSHaneen Mohammed ret = PTR_ERR(cursor); 63c27d931dSHaneen Mohammed goto err_cursor; 64c27d931dSHaneen Mohammed } 65*b8789ea7SHaneen Mohammed } 66c27d931dSHaneen Mohammed 67c27d931dSHaneen Mohammed ret = vkms_crtc_init(dev, crtc, primary, cursor); 68854502faSRodrigo Siqueira if (ret) 69854502faSRodrigo Siqueira goto err_crtc; 70854502faSRodrigo Siqueira 71854502faSRodrigo Siqueira ret = drm_connector_init(dev, connector, &vkms_connector_funcs, 72854502faSRodrigo Siqueira DRM_MODE_CONNECTOR_VIRTUAL); 73854502faSRodrigo Siqueira if (ret) { 74854502faSRodrigo Siqueira DRM_ERROR("Failed to init connector\n"); 75854502faSRodrigo Siqueira goto err_connector; 76854502faSRodrigo Siqueira } 77854502faSRodrigo Siqueira 78d1648930SRodrigo Siqueira drm_connector_helper_add(connector, &vkms_conn_helper_funcs); 79d1648930SRodrigo Siqueira 80854502faSRodrigo Siqueira ret = drm_connector_register(connector); 81854502faSRodrigo Siqueira if (ret) { 82854502faSRodrigo Siqueira DRM_ERROR("Failed to register connector\n"); 83854502faSRodrigo Siqueira goto err_connector_register; 84854502faSRodrigo Siqueira } 85854502faSRodrigo Siqueira 86854502faSRodrigo Siqueira ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs, 87854502faSRodrigo Siqueira DRM_MODE_ENCODER_VIRTUAL, NULL); 88854502faSRodrigo Siqueira if (ret) { 89854502faSRodrigo Siqueira DRM_ERROR("Failed to init encoder\n"); 90854502faSRodrigo Siqueira goto err_encoder; 91854502faSRodrigo Siqueira } 92854502faSRodrigo Siqueira encoder->possible_crtcs = 1; 93854502faSRodrigo Siqueira 94cde4c44dSDaniel Vetter ret = drm_connector_attach_encoder(connector, encoder); 95854502faSRodrigo Siqueira if (ret) { 96854502faSRodrigo Siqueira DRM_ERROR("Failed to attach connector to encoder\n"); 97854502faSRodrigo Siqueira goto err_attach; 98854502faSRodrigo Siqueira } 99854502faSRodrigo Siqueira 100854502faSRodrigo Siqueira drm_mode_config_reset(dev); 101854502faSRodrigo Siqueira 102854502faSRodrigo Siqueira return 0; 103854502faSRodrigo Siqueira 104854502faSRodrigo Siqueira err_attach: 105854502faSRodrigo Siqueira drm_encoder_cleanup(encoder); 106854502faSRodrigo Siqueira 107854502faSRodrigo Siqueira err_encoder: 108854502faSRodrigo Siqueira drm_connector_unregister(connector); 109854502faSRodrigo Siqueira 110854502faSRodrigo Siqueira err_connector_register: 111854502faSRodrigo Siqueira drm_connector_cleanup(connector); 112854502faSRodrigo Siqueira 113854502faSRodrigo Siqueira err_connector: 114854502faSRodrigo Siqueira drm_crtc_cleanup(crtc); 115854502faSRodrigo Siqueira 116854502faSRodrigo Siqueira err_crtc: 117*b8789ea7SHaneen Mohammed if (enable_cursor) 118c27d931dSHaneen Mohammed drm_plane_cleanup(cursor); 119c27d931dSHaneen Mohammed 120c27d931dSHaneen Mohammed err_cursor: 121854502faSRodrigo Siqueira drm_plane_cleanup(primary); 122c27d931dSHaneen Mohammed 123854502faSRodrigo Siqueira return ret; 124854502faSRodrigo Siqueira } 125