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