1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019-2022 Bootlin
4  * Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
5  */
6 
7 #include <linux/types.h>
8 
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_crtc_helper.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_gem_dma_helper.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
15 #include <drm/drm_mode_config.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_vblank.h>
20 
21 #include "logicvc_drm.h"
22 #include "logicvc_interface.h"
23 #include "logicvc_layer.h"
24 #include "logicvc_mode.h"
25 
26 static const struct drm_mode_config_funcs logicvc_mode_config_funcs = {
27 	.fb_create		= drm_gem_fb_create,
28 	.atomic_check		= drm_atomic_helper_check,
29 	.atomic_commit		= drm_atomic_helper_commit,
30 };
31 
32 int logicvc_mode_init(struct logicvc_drm *logicvc)
33 {
34 	struct drm_device *drm_dev = &logicvc->drm_dev;
35 	struct drm_mode_config *mode_config = &drm_dev->mode_config;
36 	struct logicvc_layer *layer_primary;
37 	uint32_t preferred_depth;
38 	int ret;
39 
40 	ret = drm_vblank_init(drm_dev, mode_config->num_crtc);
41 	if (ret) {
42 		drm_err(drm_dev, "Failed to initialize vblank\n");
43 		return ret;
44 	}
45 
46 	layer_primary = logicvc_layer_get_primary(logicvc);
47 	if (!layer_primary) {
48 		drm_err(drm_dev, "Failed to get primary layer\n");
49 		return -EINVAL;
50 	}
51 
52 	preferred_depth = layer_primary->formats->depth;
53 
54 	/* DRM counts alpha in depth, our driver doesn't. */
55 	if (layer_primary->formats->alpha)
56 		preferred_depth += 8;
57 
58 	mode_config->min_width = 64;
59 	mode_config->max_width = 2048;
60 	mode_config->min_height = 1;
61 	mode_config->max_height = 2048;
62 	mode_config->preferred_depth = preferred_depth;
63 	mode_config->funcs = &logicvc_mode_config_funcs;
64 
65 	drm_mode_config_reset(drm_dev);
66 
67 	drm_kms_helper_poll_init(drm_dev);
68 
69 	return 0;
70 }
71 
72 void logicvc_mode_fini(struct logicvc_drm *logicvc)
73 {
74 	struct drm_device *drm_dev = &logicvc->drm_dev;
75 
76 	drm_kms_helper_poll_fini(drm_dev);
77 }
78