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