1
2
3
4
5
6
7
8#include <linux/component.h>
9#include <linux/device.h>
10#include <linux/dma-buf.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13
14#include <video/imx-ipu-v3.h>
15
16#include <drm/drm_atomic.h>
17#include <drm/drm_atomic_helper.h>
18#include <drm/drm_drv.h>
19#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_fb_helper.h>
21#include <drm/drm_gem_cma_helper.h>
22#include <drm/drm_gem_framebuffer_helper.h>
23#include <drm/drm_managed.h>
24#include <drm/drm_of.h>
25#include <drm/drm_plane_helper.h>
26#include <drm/drm_probe_helper.h>
27#include <drm/drm_vblank.h>
28
29#include "imx-drm.h"
30#include "ipuv3-plane.h"
31
32#define MAX_CRTC 4
33
34static int legacyfb_depth = 16;
35module_param(legacyfb_depth, int, 0444);
36
37DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
38
39void imx_drm_connector_destroy(struct drm_connector *connector)
40{
41 drm_connector_unregister(connector);
42 drm_connector_cleanup(connector);
43}
44EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
45
46static int imx_drm_atomic_check(struct drm_device *dev,
47 struct drm_atomic_state *state)
48{
49 int ret;
50
51 ret = drm_atomic_helper_check(dev, state);
52 if (ret)
53 return ret;
54
55
56
57
58
59 ret = drm_atomic_helper_check_modeset(dev, state);
60 if (ret)
61 return ret;
62
63
64 ret = ipu_planes_assign_pre(dev, state);
65 if (ret)
66 return ret;
67
68 return ret;
69}
70
71static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
72 .fb_create = drm_gem_fb_create,
73 .atomic_check = imx_drm_atomic_check,
74 .atomic_commit = drm_atomic_helper_commit,
75};
76
77static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
78{
79 struct drm_device *dev = state->dev;
80 struct drm_plane *plane;
81 struct drm_plane_state *old_plane_state, *new_plane_state;
82 bool plane_disabling = false;
83 int i;
84
85 drm_atomic_helper_commit_modeset_disables(dev, state);
86
87 drm_atomic_helper_commit_planes(dev, state,
88 DRM_PLANE_COMMIT_ACTIVE_ONLY |
89 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
90
91 drm_atomic_helper_commit_modeset_enables(dev, state);
92
93 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
94 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
95 plane_disabling = true;
96 }
97
98
99
100
101
102
103
104
105 drm_atomic_helper_wait_for_flip_done(dev, state);
106
107 if (plane_disabling) {
108 for_each_old_plane_in_state(state, plane, old_plane_state, i)
109 ipu_plane_disable_deferred(plane);
110
111 }
112
113 drm_atomic_helper_commit_hw_done(state);
114}
115
116static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
117 .atomic_commit_tail = imx_drm_atomic_commit_tail,
118};
119
120
121int imx_drm_encoder_parse_of(struct drm_device *drm,
122 struct drm_encoder *encoder, struct device_node *np)
123{
124 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
125
126
127
128
129
130
131
132 if (crtc_mask == 0)
133 return -EPROBE_DEFER;
134
135 encoder->possible_crtcs = crtc_mask;
136
137
138 encoder->possible_clones = 0;
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
143
144static const struct drm_ioctl_desc imx_drm_ioctls[] = {
145
146};
147
148static int imx_drm_dumb_create(struct drm_file *file_priv,
149 struct drm_device *drm,
150 struct drm_mode_create_dumb *args)
151{
152 u32 width = args->width;
153 int ret;
154
155 args->width = ALIGN(width, 8);
156
157 ret = drm_gem_cma_dumb_create(file_priv, drm, args);
158 if (ret)
159 return ret;
160
161 args->width = width;
162 return ret;
163}
164
165static const struct drm_driver imx_drm_driver = {
166 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
167 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create),
168 .ioctls = imx_drm_ioctls,
169 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
170 .fops = &imx_drm_driver_fops,
171 .name = "imx-drm",
172 .desc = "i.MX DRM graphics",
173 .date = "20120507",
174 .major = 1,
175 .minor = 0,
176 .patchlevel = 0,
177};
178
179static int compare_of(struct device *dev, void *data)
180{
181 struct device_node *np = data;
182
183
184 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
185 struct ipu_client_platformdata *pdata = dev->platform_data;
186
187 return pdata->of_node == np;
188 }
189
190
191 if (of_node_name_eq(np, "lvds-channel")) {
192 np = of_get_parent(np);
193 of_node_put(np);
194 }
195
196 return dev->of_node == np;
197}
198
199static int imx_drm_bind(struct device *dev)
200{
201 struct drm_device *drm;
202 int ret;
203
204 drm = drm_dev_alloc(&imx_drm_driver, dev);
205 if (IS_ERR(drm))
206 return PTR_ERR(drm);
207
208
209
210
211
212
213 drm->mode_config.min_width = 1;
214 drm->mode_config.min_height = 1;
215 drm->mode_config.max_width = 4096;
216 drm->mode_config.max_height = 4096;
217 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
218 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
219 drm->mode_config.normalize_zpos = true;
220
221 ret = drmm_mode_config_init(drm);
222 if (ret)
223 goto err_kms;
224
225 ret = drm_vblank_init(drm, MAX_CRTC);
226 if (ret)
227 goto err_kms;
228
229 dev_set_drvdata(dev, drm);
230
231
232 ret = component_bind_all(dev, drm);
233 if (ret)
234 goto err_kms;
235
236 drm_mode_config_reset(drm);
237
238
239
240
241
242
243 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
244 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
245 legacyfb_depth = 16;
246 }
247
248 drm_kms_helper_poll_init(drm);
249
250 ret = drm_dev_register(drm, 0);
251 if (ret)
252 goto err_poll_fini;
253
254 drm_fbdev_generic_setup(drm, legacyfb_depth);
255
256 return 0;
257
258err_poll_fini:
259 drm_kms_helper_poll_fini(drm);
260 component_unbind_all(drm->dev, drm);
261err_kms:
262 drm_dev_put(drm);
263
264 return ret;
265}
266
267static void imx_drm_unbind(struct device *dev)
268{
269 struct drm_device *drm = dev_get_drvdata(dev);
270
271 drm_dev_unregister(drm);
272
273 drm_kms_helper_poll_fini(drm);
274
275 component_unbind_all(drm->dev, drm);
276
277 drm_dev_put(drm);
278
279 dev_set_drvdata(dev, NULL);
280}
281
282static const struct component_master_ops imx_drm_ops = {
283 .bind = imx_drm_bind,
284 .unbind = imx_drm_unbind,
285};
286
287static int imx_drm_platform_probe(struct platform_device *pdev)
288{
289 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
290
291 if (!ret)
292 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
293
294 return ret;
295}
296
297static int imx_drm_platform_remove(struct platform_device *pdev)
298{
299 component_master_del(&pdev->dev, &imx_drm_ops);
300 return 0;
301}
302
303#ifdef CONFIG_PM_SLEEP
304static int imx_drm_suspend(struct device *dev)
305{
306 struct drm_device *drm_dev = dev_get_drvdata(dev);
307
308 return drm_mode_config_helper_suspend(drm_dev);
309}
310
311static int imx_drm_resume(struct device *dev)
312{
313 struct drm_device *drm_dev = dev_get_drvdata(dev);
314
315 return drm_mode_config_helper_resume(drm_dev);
316}
317#endif
318
319static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
320
321static const struct of_device_id imx_drm_dt_ids[] = {
322 { .compatible = "fsl,imx-display-subsystem", },
323 { },
324};
325MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
326
327static struct platform_driver imx_drm_pdrv = {
328 .probe = imx_drm_platform_probe,
329 .remove = imx_drm_platform_remove,
330 .driver = {
331 .name = "imx-drm",
332 .pm = &imx_drm_pm_ops,
333 .of_match_table = imx_drm_dt_ids,
334 },
335};
336
337static struct platform_driver * const drivers[] = {
338 &imx_drm_pdrv,
339 &ipu_drm_driver,
340};
341
342static int __init imx_drm_init(void)
343{
344 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
345}
346module_init(imx_drm_init);
347
348static void __exit imx_drm_exit(void)
349{
350 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
351}
352module_exit(imx_drm_exit);
353
354MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
355MODULE_DESCRIPTION("i.MX drm driver core");
356MODULE_LICENSE("GPL");
357