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