Home | History | Annotate | Line # | Download | only in xen
      1 /*	$NetBSD: xen_drm_front_conn.c,v 1.2 2021/12/18 23:45:45 riastradh Exp $	*/
      2 
      3 // SPDX-License-Identifier: GPL-2.0 OR MIT
      4 
      5 /*
      6  *  Xen para-virtual DRM device
      7  *
      8  * Copyright (C) 2016-2018 EPAM Systems Inc.
      9  *
     10  * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko (at) epam.com>
     11  */
     12 
     13 #include <sys/cdefs.h>
     14 __KERNEL_RCSID(0, "$NetBSD: xen_drm_front_conn.c,v 1.2 2021/12/18 23:45:45 riastradh Exp $");
     15 
     16 #include <drm/drm_atomic_helper.h>
     17 #include <drm/drm_drv.h>
     18 #include <drm/drm_probe_helper.h>
     19 
     20 #include <video/videomode.h>
     21 
     22 #include "xen_drm_front.h"
     23 #include "xen_drm_front_conn.h"
     24 #include "xen_drm_front_kms.h"
     25 
     26 static struct xen_drm_front_drm_pipeline *
     27 to_xen_drm_pipeline(struct drm_connector *connector)
     28 {
     29 	return container_of(connector, struct xen_drm_front_drm_pipeline, conn);
     30 }
     31 
     32 static const u32 plane_formats[] = {
     33 	DRM_FORMAT_RGB565,
     34 	DRM_FORMAT_RGB888,
     35 	DRM_FORMAT_XRGB8888,
     36 	DRM_FORMAT_ARGB8888,
     37 	DRM_FORMAT_XRGB4444,
     38 	DRM_FORMAT_ARGB4444,
     39 	DRM_FORMAT_XRGB1555,
     40 	DRM_FORMAT_ARGB1555,
     41 };
     42 
     43 const u32 *xen_drm_front_conn_get_formats(int *format_count)
     44 {
     45 	*format_count = ARRAY_SIZE(plane_formats);
     46 	return plane_formats;
     47 }
     48 
     49 static int connector_detect(struct drm_connector *connector,
     50 			    struct drm_modeset_acquire_ctx *ctx,
     51 			    bool force)
     52 {
     53 	struct xen_drm_front_drm_pipeline *pipeline =
     54 			to_xen_drm_pipeline(connector);
     55 
     56 	if (drm_dev_is_unplugged(connector->dev))
     57 		pipeline->conn_connected = false;
     58 
     59 	return pipeline->conn_connected ? connector_status_connected :
     60 			connector_status_disconnected;
     61 }
     62 
     63 #define XEN_DRM_CRTC_VREFRESH_HZ	60
     64 
     65 static int connector_get_modes(struct drm_connector *connector)
     66 {
     67 	struct xen_drm_front_drm_pipeline *pipeline =
     68 			to_xen_drm_pipeline(connector);
     69 	struct drm_display_mode *mode;
     70 	struct videomode videomode;
     71 	int width, height;
     72 
     73 	mode = drm_mode_create(connector->dev);
     74 	if (!mode)
     75 		return 0;
     76 
     77 	memset(&videomode, 0, sizeof(videomode));
     78 	videomode.hactive = pipeline->width;
     79 	videomode.vactive = pipeline->height;
     80 	width = videomode.hactive + videomode.hfront_porch +
     81 			videomode.hback_porch + videomode.hsync_len;
     82 	height = videomode.vactive + videomode.vfront_porch +
     83 			videomode.vback_porch + videomode.vsync_len;
     84 	videomode.pixelclock = width * height * XEN_DRM_CRTC_VREFRESH_HZ;
     85 	mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
     86 
     87 	drm_display_mode_from_videomode(&videomode, mode);
     88 	drm_mode_probed_add(connector, mode);
     89 	return 1;
     90 }
     91 
     92 static const struct drm_connector_helper_funcs connector_helper_funcs = {
     93 	.get_modes = connector_get_modes,
     94 	.detect_ctx = connector_detect,
     95 };
     96 
     97 static const struct drm_connector_funcs connector_funcs = {
     98 	.fill_modes = drm_helper_probe_single_connector_modes,
     99 	.destroy = drm_connector_cleanup,
    100 	.reset = drm_atomic_helper_connector_reset,
    101 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
    102 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
    103 };
    104 
    105 int xen_drm_front_conn_init(struct xen_drm_front_drm_info *drm_info,
    106 			    struct drm_connector *connector)
    107 {
    108 	struct xen_drm_front_drm_pipeline *pipeline =
    109 			to_xen_drm_pipeline(connector);
    110 
    111 	drm_connector_helper_add(connector, &connector_helper_funcs);
    112 
    113 	pipeline->conn_connected = true;
    114 
    115 	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
    116 			DRM_CONNECTOR_POLL_DISCONNECT;
    117 
    118 	return drm_connector_init(drm_info->drm_dev, connector,
    119 				  &connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
    120 }
    121