]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/imx/imx-drm-core.c
bdefaa1635eb609dda6efb96147f8a043c711914
[linux.git] / drivers / gpu / drm / imx / imx-drm-core.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX drm driver
4  *
5  * Copyright (C) 2011 Sascha Hauer, Pengutronix
6  */
7 #include <linux/component.h>
8 #include <linux/device.h>
9 #include <linux/dma-buf.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <drm/drmP.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_cma_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_gem_cma_helper.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_plane_helper.h>
21 #include <drm/drm_probe_helper.h>
22 #include <video/imx-ipu-v3.h>
23
24 #include "imx-drm.h"
25 #include "ipuv3-plane.h"
26
27 #define MAX_CRTC        4
28
29 static int legacyfb_depth = 16;
30 module_param(legacyfb_depth, int, 0444);
31
32 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
33
34 void imx_drm_connector_destroy(struct drm_connector *connector)
35 {
36         drm_connector_unregister(connector);
37         drm_connector_cleanup(connector);
38 }
39 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
40
41 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
42 {
43         drm_encoder_cleanup(encoder);
44 }
45 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
46
47 static int imx_drm_atomic_check(struct drm_device *dev,
48                                 struct drm_atomic_state *state)
49 {
50         int ret;
51
52         ret = drm_atomic_helper_check(dev, state);
53         if (ret)
54                 return ret;
55
56         /*
57          * Check modeset again in case crtc_state->mode_changed is
58          * updated in plane's ->atomic_check callback.
59          */
60         ret = drm_atomic_helper_check_modeset(dev, state);
61         if (ret)
62                 return ret;
63
64         /* Assign PRG/PRE channels and check if all constrains are satisfied. */
65         ret = ipu_planes_assign_pre(dev, state);
66         if (ret)
67                 return ret;
68
69         return ret;
70 }
71
72 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
73         .fb_create = drm_gem_fb_create,
74         .atomic_check = imx_drm_atomic_check,
75         .atomic_commit = drm_atomic_helper_commit,
76 };
77
78 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
79 {
80         struct drm_device *dev = state->dev;
81         struct drm_plane *plane;
82         struct drm_plane_state *old_plane_state, *new_plane_state;
83         bool plane_disabling = false;
84         int i;
85
86         drm_atomic_helper_commit_modeset_disables(dev, state);
87
88         drm_atomic_helper_commit_planes(dev, state,
89                                 DRM_PLANE_COMMIT_ACTIVE_ONLY |
90                                 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
91
92         drm_atomic_helper_commit_modeset_enables(dev, state);
93
94         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
95                 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
96                         plane_disabling = true;
97         }
98
99         /*
100          * The flip done wait is only strictly required by imx-drm if a deferred
101          * plane disable is in-flight. As the core requires blocking commits
102          * to wait for the flip it is done here unconditionally. This keeps the
103          * workitem around a bit longer than required for the majority of
104          * non-blocking commits, but we accept that for the sake of simplicity.
105          */
106         drm_atomic_helper_wait_for_flip_done(dev, state);
107
108         if (plane_disabling) {
109                 for_each_old_plane_in_state(state, plane, old_plane_state, i)
110                         ipu_plane_disable_deferred(plane);
111
112         }
113
114         drm_atomic_helper_commit_hw_done(state);
115 }
116
117 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
118         .atomic_commit_tail = imx_drm_atomic_commit_tail,
119 };
120
121
122 int imx_drm_encoder_parse_of(struct drm_device *drm,
123         struct drm_encoder *encoder, struct device_node *np)
124 {
125         uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
126
127         /*
128          * If we failed to find the CRTC(s) which this encoder is
129          * supposed to be connected to, it's because the CRTC has
130          * not been registered yet.  Defer probing, and hope that
131          * the required CRTC is added later.
132          */
133         if (crtc_mask == 0)
134                 return -EPROBE_DEFER;
135
136         encoder->possible_crtcs = crtc_mask;
137
138         /* FIXME: this is the mask of outputs which can clone this output. */
139         encoder->possible_clones = ~0;
140
141         return 0;
142 }
143 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
144
145 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
146         /* none so far */
147 };
148
149 static struct drm_driver imx_drm_driver = {
150         .driver_features        = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
151         .gem_free_object_unlocked = drm_gem_cma_free_object,
152         .gem_vm_ops             = &drm_gem_cma_vm_ops,
153         .dumb_create            = drm_gem_cma_dumb_create,
154
155         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
156         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
157         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
158         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
159         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
160         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
161         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
162         .ioctls                 = imx_drm_ioctls,
163         .num_ioctls             = ARRAY_SIZE(imx_drm_ioctls),
164         .fops                   = &imx_drm_driver_fops,
165         .name                   = "imx-drm",
166         .desc                   = "i.MX DRM graphics",
167         .date                   = "20120507",
168         .major                  = 1,
169         .minor                  = 0,
170         .patchlevel             = 0,
171 };
172
173 static int compare_of(struct device *dev, void *data)
174 {
175         struct device_node *np = data;
176
177         /* Special case for DI, dev->of_node may not be set yet */
178         if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
179                 struct ipu_client_platformdata *pdata = dev->platform_data;
180
181                 return pdata->of_node == np;
182         }
183
184         /* Special case for LDB, one device for two channels */
185         if (of_node_name_eq(np, "lvds-channel")) {
186                 np = of_get_parent(np);
187                 of_node_put(np);
188         }
189
190         return dev->of_node == np;
191 }
192
193 static int imx_drm_bind(struct device *dev)
194 {
195         struct drm_device *drm;
196         int ret;
197
198         drm = drm_dev_alloc(&imx_drm_driver, dev);
199         if (IS_ERR(drm))
200                 return PTR_ERR(drm);
201
202         /*
203          * enable drm irq mode.
204          * - with irq_enabled = true, we can use the vblank feature.
205          *
206          * P.S. note that we wouldn't use drm irq handler but
207          *      just specific driver own one instead because
208          *      drm framework supports only one irq handler and
209          *      drivers can well take care of their interrupts
210          */
211         drm->irq_enabled = true;
212
213         /*
214          * set max width and height as default value(4096x4096).
215          * this value would be used to check framebuffer size limitation
216          * at drm_mode_addfb().
217          */
218         drm->mode_config.min_width = 1;
219         drm->mode_config.min_height = 1;
220         drm->mode_config.max_width = 4096;
221         drm->mode_config.max_height = 4096;
222         drm->mode_config.funcs = &imx_drm_mode_config_funcs;
223         drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
224         drm->mode_config.allow_fb_modifiers = true;
225         drm->mode_config.normalize_zpos = true;
226
227         drm_mode_config_init(drm);
228
229         ret = drm_vblank_init(drm, MAX_CRTC);
230         if (ret)
231                 goto err_kms;
232
233         dev_set_drvdata(dev, drm);
234
235         /* Now try and bind all our sub-components */
236         ret = component_bind_all(dev, drm);
237         if (ret)
238                 goto err_kms;
239
240         drm_mode_config_reset(drm);
241
242         /*
243          * All components are now initialised, so setup the fb helper.
244          * The fb helper takes copies of key hardware information, so the
245          * crtcs/connectors/encoders must not change after this point.
246          */
247         if (legacyfb_depth != 16 && legacyfb_depth != 32) {
248                 dev_warn(dev, "Invalid legacyfb_depth.  Defaulting to 16bpp\n");
249                 legacyfb_depth = 16;
250         }
251
252         drm_kms_helper_poll_init(drm);
253
254         ret = drm_dev_register(drm, 0);
255         if (ret)
256                 goto err_poll_fini;
257
258         drm_fbdev_generic_setup(drm, legacyfb_depth);
259
260         return 0;
261
262 err_poll_fini:
263         drm_kms_helper_poll_fini(drm);
264         component_unbind_all(drm->dev, drm);
265 err_kms:
266         drm_mode_config_cleanup(drm);
267         drm_dev_put(drm);
268
269         return ret;
270 }
271
272 static void imx_drm_unbind(struct device *dev)
273 {
274         struct drm_device *drm = dev_get_drvdata(dev);
275
276         drm_dev_unregister(drm);
277
278         drm_kms_helper_poll_fini(drm);
279
280         drm_mode_config_cleanup(drm);
281
282         component_unbind_all(drm->dev, drm);
283         dev_set_drvdata(dev, NULL);
284
285         drm_dev_put(drm);
286 }
287
288 static const struct component_master_ops imx_drm_ops = {
289         .bind = imx_drm_bind,
290         .unbind = imx_drm_unbind,
291 };
292
293 static int imx_drm_platform_probe(struct platform_device *pdev)
294 {
295         int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
296
297         if (!ret)
298                 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
299
300         return ret;
301 }
302
303 static int imx_drm_platform_remove(struct platform_device *pdev)
304 {
305         component_master_del(&pdev->dev, &imx_drm_ops);
306         return 0;
307 }
308
309 #ifdef CONFIG_PM_SLEEP
310 static int imx_drm_suspend(struct device *dev)
311 {
312         struct drm_device *drm_dev = dev_get_drvdata(dev);
313
314         return drm_mode_config_helper_suspend(drm_dev);
315 }
316
317 static int imx_drm_resume(struct device *dev)
318 {
319         struct drm_device *drm_dev = dev_get_drvdata(dev);
320
321         return drm_mode_config_helper_resume(drm_dev);
322 }
323 #endif
324
325 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
326
327 static const struct of_device_id imx_drm_dt_ids[] = {
328         { .compatible = "fsl,imx-display-subsystem", },
329         { /* sentinel */ },
330 };
331 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
332
333 static struct platform_driver imx_drm_pdrv = {
334         .probe          = imx_drm_platform_probe,
335         .remove         = imx_drm_platform_remove,
336         .driver         = {
337                 .name   = "imx-drm",
338                 .pm     = &imx_drm_pm_ops,
339                 .of_match_table = imx_drm_dt_ids,
340         },
341 };
342
343 static struct platform_driver * const drivers[] = {
344         &imx_drm_pdrv,
345         &ipu_drm_driver,
346 };
347
348 static int __init imx_drm_init(void)
349 {
350         return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
351 }
352 module_init(imx_drm_init);
353
354 static void __exit imx_drm_exit(void)
355 {
356         platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
357 }
358 module_exit(imx_drm_exit);
359
360 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
361 MODULE_DESCRIPTION("i.MX drm driver core");
362 MODULE_LICENSE("GPL");