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