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_fb_helper.h>
14 #include <drm/drm_gem_dma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_mode_config.h>
17 #include <drm/drm_panel.h>
18 #include <drm/drm_print.h>
19 #include <drm/drm_probe_helper.h>
20 #include <drm/drm_vblank.h>
21 
22 #include "logicvc_drm.h"
23 #include "logicvc_interface.h"
24 #include "logicvc_layer.h"
25 #include "logicvc_mode.h"
26 
27 static const struct drm_mode_config_funcs logicvc_mode_config_funcs = {
28 	.fb_create		= drm_gem_fb_create,
29 	.output_poll_changed	= drm_fb_helper_output_poll_changed,
30 	.atomic_check		= drm_atomic_helper_check,
31 	.atomic_commit		= drm_atomic_helper_commit,
32 };
33 
34 int logicvc_mode_init(struct logicvc_drm *logicvc)
35 {
36 	struct drm_device *drm_dev = &logicvc->drm_dev;
37 	struct drm_mode_config *mode_config = &drm_dev->mode_config;
38 	struct logicvc_layer *layer_primary;
39 	uint32_t preferred_depth;
40 	int ret;
41 
42 	ret = drm_vblank_init(drm_dev, mode_config->num_crtc);
43 	if (ret) {
44 		drm_err(drm_dev, "Failed to initialize vblank\n");
45 		return ret;
46 	}
47 
48 	layer_primary = logicvc_layer_get_primary(logicvc);
49 	if (!layer_primary) {
50 		drm_err(drm_dev, "Failed to get primary layer\n");
51 		return -EINVAL;
52 	}
53 
54 	preferred_depth = layer_primary->formats->depth;
55 
56 	/* DRM counts alpha in depth, our driver doesn't. */
57 	if (layer_primary->formats->alpha)
58 		preferred_depth += 8;
59 
60 	mode_config->min_width = 64;
61 	mode_config->max_width = 2048;
62 	mode_config->min_height = 1;
63 	mode_config->max_height = 2048;
64 	mode_config->preferred_depth = preferred_depth;
65 	mode_config->funcs = &logicvc_mode_config_funcs;
66 
67 	drm_mode_config_reset(drm_dev);
68 
69 	drm_kms_helper_poll_init(drm_dev);
70 
71 	return 0;
72 }
73 
74 void logicvc_mode_fini(struct logicvc_drm *logicvc)
75 {
76 	struct drm_device *drm_dev = &logicvc->drm_dev;
77 
78 	drm_kms_helper_poll_fini(drm_dev);
79 }
80