2 * Freescale i.MX drm driver
4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
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.
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>
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_fb_cma_helper.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_of.h>
30 #include <video/imx-ipu-v3.h>
33 #include "ipuv3-plane.h"
37 struct imx_drm_device
{
38 struct drm_device
*drm
;
40 struct drm_fbdev_cma
*fbhelper
;
41 struct drm_atomic_state
*state
;
44 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
45 static int legacyfb_depth
= 16;
46 module_param(legacyfb_depth
, int, 0444);
49 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
51 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
53 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
56 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops
);
58 void imx_drm_connector_destroy(struct drm_connector
*connector
)
60 drm_connector_unregister(connector
);
61 drm_connector_cleanup(connector
);
63 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
65 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
67 drm_encoder_cleanup(encoder
);
69 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
71 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
73 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
75 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
78 static int imx_drm_atomic_check(struct drm_device
*dev
,
79 struct drm_atomic_state
*state
)
83 ret
= drm_atomic_helper_check_modeset(dev
, state
);
87 ret
= drm_atomic_helper_check_planes(dev
, state
);
92 * Check modeset again in case crtc_state->mode_changed is
93 * updated in plane's ->atomic_check callback.
95 ret
= drm_atomic_helper_check_modeset(dev
, state
);
99 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
100 ret
= ipu_planes_assign_pre(dev
, state
);
107 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
108 .fb_create
= drm_fb_cma_create
,
109 .output_poll_changed
= imx_drm_output_poll_changed
,
110 .atomic_check
= imx_drm_atomic_check
,
111 .atomic_commit
= drm_atomic_helper_commit
,
114 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
116 struct drm_device
*dev
= state
->dev
;
117 struct drm_plane
*plane
;
118 struct drm_plane_state
*old_plane_state
;
119 bool plane_disabling
= false;
122 drm_atomic_helper_commit_modeset_disables(dev
, state
);
124 drm_atomic_helper_commit_planes(dev
, state
,
125 DRM_PLANE_COMMIT_ACTIVE_ONLY
|
126 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET
);
128 drm_atomic_helper_commit_modeset_enables(dev
, state
);
130 for_each_plane_in_state(state
, plane
, old_plane_state
, i
) {
131 if (drm_atomic_plane_disabling(old_plane_state
, plane
->state
))
132 plane_disabling
= true;
135 if (plane_disabling
) {
136 drm_atomic_helper_wait_for_vblanks(dev
, state
);
138 for_each_plane_in_state(state
, plane
, old_plane_state
, i
)
139 ipu_plane_disable_deferred(plane
);
143 drm_atomic_helper_commit_hw_done(state
);
146 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
147 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
151 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
152 struct drm_encoder
*encoder
, struct device_node
*np
)
154 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
157 * If we failed to find the CRTC(s) which this encoder is
158 * supposed to be connected to, it's because the CRTC has
159 * not been registered yet. Defer probing, and hope that
160 * the required CRTC is added later.
163 return -EPROBE_DEFER
;
165 encoder
->possible_crtcs
= crtc_mask
;
167 /* FIXME: this is the mask of outputs which can clone this output. */
168 encoder
->possible_clones
= ~0;
172 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
174 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
178 static struct drm_driver imx_drm_driver
= {
179 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
181 .lastclose
= imx_drm_driver_lastclose
,
182 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
183 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
184 .dumb_create
= drm_gem_cma_dumb_create
,
185 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
186 .dumb_destroy
= drm_gem_dumb_destroy
,
188 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
189 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
190 .gem_prime_import
= drm_gem_prime_import
,
191 .gem_prime_export
= drm_gem_prime_export
,
192 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
193 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
194 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
195 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
196 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
197 .ioctls
= imx_drm_ioctls
,
198 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
199 .fops
= &imx_drm_driver_fops
,
201 .desc
= "i.MX DRM graphics",
208 static int compare_of(struct device
*dev
, void *data
)
210 struct device_node
*np
= data
;
212 /* Special case for DI, dev->of_node may not be set yet */
213 if (strcmp(dev
->driver
->name
, "imx-ipuv3-crtc") == 0) {
214 struct ipu_client_platformdata
*pdata
= dev
->platform_data
;
216 return pdata
->of_node
== np
;
219 /* Special case for LDB, one device for two channels */
220 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
221 np
= of_get_parent(np
);
225 return dev
->of_node
== np
;
228 static int imx_drm_bind(struct device
*dev
)
230 struct drm_device
*drm
;
231 struct imx_drm_device
*imxdrm
;
234 drm
= drm_dev_alloc(&imx_drm_driver
, dev
);
238 imxdrm
= devm_kzalloc(dev
, sizeof(*imxdrm
), GFP_KERNEL
);
245 drm
->dev_private
= imxdrm
;
248 * enable drm irq mode.
249 * - with irq_enabled = true, we can use the vblank feature.
251 * P.S. note that we wouldn't use drm irq handler but
252 * just specific driver own one instead because
253 * drm framework supports only one irq handler and
254 * drivers can well take care of their interrupts
256 drm
->irq_enabled
= true;
259 * set max width and height as default value(4096x4096).
260 * this value would be used to check framebuffer size limitation
261 * at drm_mode_addfb().
263 drm
->mode_config
.min_width
= 1;
264 drm
->mode_config
.min_height
= 1;
265 drm
->mode_config
.max_width
= 4096;
266 drm
->mode_config
.max_height
= 4096;
267 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
268 drm
->mode_config
.helper_private
= &imx_drm_mode_config_helpers
;
270 drm_mode_config_init(drm
);
272 ret
= drm_vblank_init(drm
, MAX_CRTC
);
276 dev_set_drvdata(dev
, drm
);
278 /* Now try and bind all our sub-components */
279 ret
= component_bind_all(dev
, drm
);
283 drm_mode_config_reset(drm
);
286 * All components are now initialised, so setup the fb helper.
287 * The fb helper takes copies of key hardware information, so the
288 * crtcs/connectors/encoders must not change after this point.
290 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
291 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
292 dev_warn(dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
295 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
, MAX_CRTC
);
296 if (IS_ERR(imxdrm
->fbhelper
)) {
297 ret
= PTR_ERR(imxdrm
->fbhelper
);
298 imxdrm
->fbhelper
= NULL
;
303 drm_kms_helper_poll_init(drm
);
305 ret
= drm_dev_register(drm
, 0);
312 drm_kms_helper_poll_fini(drm
);
313 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
314 if (imxdrm
->fbhelper
)
315 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
318 component_unbind_all(drm
->dev
, drm
);
320 drm_mode_config_cleanup(drm
);
327 static void imx_drm_unbind(struct device
*dev
)
329 struct drm_device
*drm
= dev_get_drvdata(dev
);
330 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
332 drm_dev_unregister(drm
);
334 drm_kms_helper_poll_fini(drm
);
336 if (imxdrm
->fbhelper
)
337 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
339 drm_mode_config_cleanup(drm
);
341 component_unbind_all(drm
->dev
, drm
);
342 dev_set_drvdata(dev
, NULL
);
347 static const struct component_master_ops imx_drm_ops
= {
348 .bind
= imx_drm_bind
,
349 .unbind
= imx_drm_unbind
,
352 static int imx_drm_platform_probe(struct platform_device
*pdev
)
354 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
357 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
362 static int imx_drm_platform_remove(struct platform_device
*pdev
)
364 component_master_del(&pdev
->dev
, &imx_drm_ops
);
368 #ifdef CONFIG_PM_SLEEP
369 static int imx_drm_suspend(struct device
*dev
)
371 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
372 struct imx_drm_device
*imxdrm
;
374 /* The drm_dev is NULL before .load hook is called */
378 drm_kms_helper_poll_disable(drm_dev
);
380 imxdrm
= drm_dev
->dev_private
;
381 imxdrm
->state
= drm_atomic_helper_suspend(drm_dev
);
382 if (IS_ERR(imxdrm
->state
)) {
383 drm_kms_helper_poll_enable(drm_dev
);
384 return PTR_ERR(imxdrm
->state
);
390 static int imx_drm_resume(struct device
*dev
)
392 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
393 struct imx_drm_device
*imx_drm
;
398 imx_drm
= drm_dev
->dev_private
;
399 drm_atomic_helper_resume(drm_dev
, imx_drm
->state
);
400 drm_kms_helper_poll_enable(drm_dev
);
406 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
408 static const struct of_device_id imx_drm_dt_ids
[] = {
409 { .compatible
= "fsl,imx-display-subsystem", },
412 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
414 static struct platform_driver imx_drm_pdrv
= {
415 .probe
= imx_drm_platform_probe
,
416 .remove
= imx_drm_platform_remove
,
419 .pm
= &imx_drm_pm_ops
,
420 .of_match_table
= imx_drm_dt_ids
,
424 static struct platform_driver
* const drivers
[] = {
429 static int __init
imx_drm_init(void)
431 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
433 module_init(imx_drm_init
);
435 static void __exit
imx_drm_exit(void)
437 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
439 module_exit(imx_drm_exit
);
441 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
442 MODULE_DESCRIPTION("i.MX drm driver core");
443 MODULE_LICENSE("GPL");