ASoC: sam9x5_wm8731: Drop 'ASoC' prefix from error messages
[linux-2.6-block.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         if (plane_disabling) {
137                 drm_atomic_helper_wait_for_vblanks(dev, state);
138
139                 for_each_old_plane_in_state(state, plane, old_plane_state, i)
140                         ipu_plane_disable_deferred(plane);
141
142         }
143
144         drm_atomic_helper_commit_hw_done(state);
145 }
146
147 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
148         .atomic_commit_tail = imx_drm_atomic_commit_tail,
149 };
150
151
152 int imx_drm_encoder_parse_of(struct drm_device *drm,
153         struct drm_encoder *encoder, struct device_node *np)
154 {
155         uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
156
157         /*
158          * If we failed to find the CRTC(s) which this encoder is
159          * supposed to be connected to, it's because the CRTC has
160          * not been registered yet.  Defer probing, and hope that
161          * the required CRTC is added later.
162          */
163         if (crtc_mask == 0)
164                 return -EPROBE_DEFER;
165
166         encoder->possible_crtcs = crtc_mask;
167
168         /* FIXME: this is the mask of outputs which can clone this output. */
169         encoder->possible_clones = ~0;
170
171         return 0;
172 }
173 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
174
175 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
176         /* none so far */
177 };
178
179 static struct drm_driver imx_drm_driver = {
180         .driver_features        = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
181                                   DRIVER_ATOMIC,
182         .lastclose              = imx_drm_driver_lastclose,
183         .gem_free_object_unlocked = drm_gem_cma_free_object,
184         .gem_vm_ops             = &drm_gem_cma_vm_ops,
185         .dumb_create            = drm_gem_cma_dumb_create,
186
187         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
188         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
189         .gem_prime_import       = drm_gem_prime_import,
190         .gem_prime_export       = drm_gem_prime_export,
191         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
192         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
193         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
194         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
195         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
196         .ioctls                 = imx_drm_ioctls,
197         .num_ioctls             = ARRAY_SIZE(imx_drm_ioctls),
198         .fops                   = &imx_drm_driver_fops,
199         .name                   = "imx-drm",
200         .desc                   = "i.MX DRM graphics",
201         .date                   = "20120507",
202         .major                  = 1,
203         .minor                  = 0,
204         .patchlevel             = 0,
205 };
206
207 static int compare_of(struct device *dev, void *data)
208 {
209         struct device_node *np = data;
210
211         /* Special case for DI, dev->of_node may not be set yet */
212         if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
213                 struct ipu_client_platformdata *pdata = dev->platform_data;
214
215                 return pdata->of_node == np;
216         }
217
218         /* Special case for LDB, one device for two channels */
219         if (of_node_cmp(np->name, "lvds-channel") == 0) {
220                 np = of_get_parent(np);
221                 of_node_put(np);
222         }
223
224         return dev->of_node == np;
225 }
226
227 static int imx_drm_bind(struct device *dev)
228 {
229         struct drm_device *drm;
230         struct imx_drm_device *imxdrm;
231         int ret;
232
233         drm = drm_dev_alloc(&imx_drm_driver, dev);
234         if (IS_ERR(drm))
235                 return PTR_ERR(drm);
236
237         imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
238         if (!imxdrm) {
239                 ret = -ENOMEM;
240                 goto err_unref;
241         }
242
243         imxdrm->drm = drm;
244         drm->dev_private = imxdrm;
245
246         /*
247          * enable drm irq mode.
248          * - with irq_enabled = true, we can use the vblank feature.
249          *
250          * P.S. note that we wouldn't use drm irq handler but
251          *      just specific driver own one instead because
252          *      drm framework supports only one irq handler and
253          *      drivers can well take care of their interrupts
254          */
255         drm->irq_enabled = true;
256
257         /*
258          * set max width and height as default value(4096x4096).
259          * this value would be used to check framebuffer size limitation
260          * at drm_mode_addfb().
261          */
262         drm->mode_config.min_width = 1;
263         drm->mode_config.min_height = 1;
264         drm->mode_config.max_width = 4096;
265         drm->mode_config.max_height = 4096;
266         drm->mode_config.funcs = &imx_drm_mode_config_funcs;
267         drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
268
269         drm_mode_config_init(drm);
270
271         ret = drm_vblank_init(drm, MAX_CRTC);
272         if (ret)
273                 goto err_kms;
274
275         dev_set_drvdata(dev, drm);
276
277         /* Now try and bind all our sub-components */
278         ret = component_bind_all(dev, drm);
279         if (ret)
280                 goto err_kms;
281
282         drm_mode_config_reset(drm);
283
284         /*
285          * All components are now initialised, so setup the fb helper.
286          * The fb helper takes copies of key hardware information, so the
287          * crtcs/connectors/encoders must not change after this point.
288          */
289 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
290         if (legacyfb_depth != 16 && legacyfb_depth != 32) {
291                 dev_warn(dev, "Invalid legacyfb_depth.  Defaulting to 16bpp\n");
292                 legacyfb_depth = 16;
293         }
294         imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, MAX_CRTC);
295         if (IS_ERR(imxdrm->fbhelper)) {
296                 ret = PTR_ERR(imxdrm->fbhelper);
297                 imxdrm->fbhelper = NULL;
298                 goto err_unbind;
299         }
300 #endif
301
302         drm_kms_helper_poll_init(drm);
303
304         ret = drm_dev_register(drm, 0);
305         if (ret)
306                 goto err_fbhelper;
307
308         return 0;
309
310 err_fbhelper:
311         drm_kms_helper_poll_fini(drm);
312 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
313         if (imxdrm->fbhelper)
314                 drm_fbdev_cma_fini(imxdrm->fbhelper);
315 err_unbind:
316 #endif
317         component_unbind_all(drm->dev, drm);
318 err_kms:
319         drm_mode_config_cleanup(drm);
320 err_unref:
321         drm_dev_unref(drm);
322
323         return ret;
324 }
325
326 static void imx_drm_unbind(struct device *dev)
327 {
328         struct drm_device *drm = dev_get_drvdata(dev);
329         struct imx_drm_device *imxdrm = drm->dev_private;
330
331         drm_dev_unregister(drm);
332
333         drm_kms_helper_poll_fini(drm);
334
335         if (imxdrm->fbhelper)
336                 drm_fbdev_cma_fini(imxdrm->fbhelper);
337
338         drm_mode_config_cleanup(drm);
339
340         component_unbind_all(drm->dev, drm);
341         dev_set_drvdata(dev, NULL);
342
343         drm_dev_unref(drm);
344 }
345
346 static const struct component_master_ops imx_drm_ops = {
347         .bind = imx_drm_bind,
348         .unbind = imx_drm_unbind,
349 };
350
351 static int imx_drm_platform_probe(struct platform_device *pdev)
352 {
353         int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
354
355         if (!ret)
356                 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
357
358         return ret;
359 }
360
361 static int imx_drm_platform_remove(struct platform_device *pdev)
362 {
363         component_master_del(&pdev->dev, &imx_drm_ops);
364         return 0;
365 }
366
367 #ifdef CONFIG_PM_SLEEP
368 static int imx_drm_suspend(struct device *dev)
369 {
370         struct drm_device *drm_dev = dev_get_drvdata(dev);
371         struct imx_drm_device *imxdrm;
372
373         /* The drm_dev is NULL before .load hook is called */
374         if (drm_dev == NULL)
375                 return 0;
376
377         drm_kms_helper_poll_disable(drm_dev);
378
379         imxdrm = drm_dev->dev_private;
380         imxdrm->state = drm_atomic_helper_suspend(drm_dev);
381         if (IS_ERR(imxdrm->state)) {
382                 drm_kms_helper_poll_enable(drm_dev);
383                 return PTR_ERR(imxdrm->state);
384         }
385
386         return 0;
387 }
388
389 static int imx_drm_resume(struct device *dev)
390 {
391         struct drm_device *drm_dev = dev_get_drvdata(dev);
392         struct imx_drm_device *imx_drm;
393
394         if (drm_dev == NULL)
395                 return 0;
396
397         imx_drm = drm_dev->dev_private;
398         drm_atomic_helper_resume(drm_dev, imx_drm->state);
399         drm_kms_helper_poll_enable(drm_dev);
400
401         return 0;
402 }
403 #endif
404
405 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
406
407 static const struct of_device_id imx_drm_dt_ids[] = {
408         { .compatible = "fsl,imx-display-subsystem", },
409         { /* sentinel */ },
410 };
411 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
412
413 static struct platform_driver imx_drm_pdrv = {
414         .probe          = imx_drm_platform_probe,
415         .remove         = imx_drm_platform_remove,
416         .driver         = {
417                 .name   = "imx-drm",
418                 .pm     = &imx_drm_pm_ops,
419                 .of_match_table = imx_drm_dt_ids,
420         },
421 };
422
423 static struct platform_driver * const drivers[] = {
424         &imx_drm_pdrv,
425         &ipu_drm_driver,
426 };
427
428 static int __init imx_drm_init(void)
429 {
430         return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
431 }
432 module_init(imx_drm_init);
433
434 static void __exit imx_drm_exit(void)
435 {
436         platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
437 }
438 module_exit(imx_drm_exit);
439
440 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
441 MODULE_DESCRIPTION("i.MX drm driver core");
442 MODULE_LICENSE("GPL");