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