xref: /linux/drivers/gpu/drm/imx/dcss/dcss-kms.c (revision e77a8005748547fb1f10645097f13ccdd804d7e5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 NXP.
4  */
5 
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_bridge.h>
9 #include <drm/drm_bridge_connector.h>
10 #include <drm/drm_client_setup.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_fbdev_dma.h>
13 #include <drm/drm_gem_dma_helper.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_vblank.h>
18 
19 #include "dcss-dev.h"
20 #include "dcss-kms.h"
21 
22 DEFINE_DRM_GEM_DMA_FOPS(dcss_cma_fops);
23 
24 static const struct drm_mode_config_funcs dcss_drm_mode_config_funcs = {
25 	.fb_create = drm_gem_fb_create,
26 	.atomic_check = drm_atomic_helper_check,
27 	.atomic_commit = drm_atomic_helper_commit,
28 };
29 
30 static const struct drm_driver dcss_kms_driver = {
31 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
32 	DRM_GEM_DMA_DRIVER_OPS,
33 	DRM_FBDEV_DMA_DRIVER_OPS,
34 	.fops			= &dcss_cma_fops,
35 	.name			= "imx-dcss",
36 	.desc			= "i.MX8MQ Display Subsystem",
37 	.date			= "20190917",
38 	.major			= 1,
39 	.minor			= 0,
40 	.patchlevel		= 0,
41 };
42 
43 static const struct drm_mode_config_helper_funcs dcss_mode_config_helpers = {
44 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
45 };
46 
47 static void dcss_kms_mode_config_init(struct dcss_kms_dev *kms)
48 {
49 	struct drm_mode_config *config = &kms->base.mode_config;
50 
51 	drm_mode_config_init(&kms->base);
52 
53 	config->min_width = 1;
54 	config->min_height = 1;
55 	config->max_width = 4096;
56 	config->max_height = 4096;
57 	config->normalize_zpos = true;
58 
59 	config->funcs = &dcss_drm_mode_config_funcs;
60 	config->helper_private = &dcss_mode_config_helpers;
61 }
62 
63 static const struct drm_encoder_funcs dcss_kms_simple_encoder_funcs = {
64 	.destroy = drm_encoder_cleanup,
65 };
66 
67 static int dcss_kms_bridge_connector_init(struct dcss_kms_dev *kms)
68 {
69 	struct drm_device *ddev = &kms->base;
70 	struct drm_encoder *encoder = &kms->encoder;
71 	struct drm_crtc *crtc = (struct drm_crtc *)&kms->crtc;
72 	struct drm_panel *panel;
73 	struct drm_bridge *bridge;
74 	int ret;
75 
76 	ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0,
77 					  &panel, &bridge);
78 	if (ret)
79 		return ret;
80 
81 	if (!bridge) {
82 		dev_err(ddev->dev, "No bridge found %d.\n", ret);
83 		return -ENODEV;
84 	}
85 
86 	encoder->possible_crtcs = drm_crtc_mask(crtc);
87 
88 	ret = drm_encoder_init(&kms->base, encoder,
89 			       &dcss_kms_simple_encoder_funcs,
90 			       DRM_MODE_ENCODER_NONE, NULL);
91 	if (ret) {
92 		dev_err(ddev->dev, "Failed initializing encoder %d.\n", ret);
93 		return ret;
94 	}
95 
96 	ret = drm_bridge_attach(encoder, bridge, NULL,
97 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
98 	if (ret < 0)
99 		return ret;
100 
101 	kms->connector = drm_bridge_connector_init(ddev, encoder);
102 	if (IS_ERR(kms->connector)) {
103 		dev_err(ddev->dev, "Unable to create bridge connector.\n");
104 		return PTR_ERR(kms->connector);
105 	}
106 
107 	drm_connector_attach_encoder(kms->connector, encoder);
108 
109 	return 0;
110 }
111 
112 struct dcss_kms_dev *dcss_kms_attach(struct dcss_dev *dcss)
113 {
114 	struct dcss_kms_dev *kms;
115 	struct drm_device *drm;
116 	struct dcss_crtc *crtc;
117 	int ret;
118 
119 	kms = devm_drm_dev_alloc(dcss->dev, &dcss_kms_driver,
120 				 struct dcss_kms_dev, base);
121 	if (IS_ERR(kms))
122 		return kms;
123 
124 	drm = &kms->base;
125 	crtc = &kms->crtc;
126 
127 	drm->dev_private = dcss;
128 
129 	dcss_kms_mode_config_init(kms);
130 
131 	ret = drm_vblank_init(drm, 1);
132 	if (ret)
133 		goto cleanup_mode_config;
134 
135 	ret = dcss_kms_bridge_connector_init(kms);
136 	if (ret)
137 		goto cleanup_mode_config;
138 
139 	ret = dcss_crtc_init(crtc, drm);
140 	if (ret)
141 		goto cleanup_mode_config;
142 
143 	drm_mode_config_reset(drm);
144 
145 	drm_kms_helper_poll_init(drm);
146 
147 	ret = drm_dev_register(drm, 0);
148 	if (ret)
149 		goto cleanup_crtc;
150 
151 	drm_client_setup(drm, NULL);
152 
153 	return kms;
154 
155 cleanup_crtc:
156 	drm_kms_helper_poll_fini(drm);
157 	dcss_crtc_deinit(crtc, drm);
158 
159 cleanup_mode_config:
160 	drm_mode_config_cleanup(drm);
161 	drm->dev_private = NULL;
162 
163 	return ERR_PTR(ret);
164 }
165 
166 void dcss_kms_detach(struct dcss_kms_dev *kms)
167 {
168 	struct drm_device *drm = &kms->base;
169 
170 	drm_dev_unregister(drm);
171 	drm_kms_helper_poll_fini(drm);
172 	drm_atomic_helper_shutdown(drm);
173 	drm_crtc_vblank_off(&kms->crtc.base);
174 	drm_mode_config_cleanup(drm);
175 	dcss_crtc_deinit(&kms->crtc, drm);
176 	drm->dev_private = NULL;
177 }
178 
179 void dcss_kms_shutdown(struct dcss_kms_dev *kms)
180 {
181 	struct drm_device *drm = &kms->base;
182 
183 	drm_atomic_helper_shutdown(drm);
184 }
185