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_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>
34 #include "ipuv3-plane.h"
38 struct imx_drm_device
{
39 struct drm_device
*drm
;
41 struct drm_fbdev_cma
*fbhelper
;
42 struct drm_atomic_state
*state
;
45 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
46 static int legacyfb_depth
= 16;
47 module_param(legacyfb_depth
, int, 0444);
50 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
52 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
54 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
57 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops
);
59 void imx_drm_connector_destroy(struct drm_connector
*connector
)
61 drm_connector_unregister(connector
);
62 drm_connector_cleanup(connector
);
64 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
66 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
68 drm_encoder_cleanup(encoder
);
70 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
72 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
74 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
76 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
79 static int imx_drm_atomic_check(struct drm_device
*dev
,
80 struct drm_atomic_state
*state
)
84 ret
= drm_atomic_helper_check_modeset(dev
, state
);
88 ret
= drm_atomic_helper_check_planes(dev
, state
);
93 * Check modeset again in case crtc_state->mode_changed is
94 * updated in plane's ->atomic_check callback.
96 ret
= drm_atomic_helper_check_modeset(dev
, state
);
100 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
101 ret
= ipu_planes_assign_pre(dev
, state
);
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
,
115 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
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;
123 drm_atomic_helper_commit_modeset_disables(dev
, state
);
125 drm_atomic_helper_commit_planes(dev
, state
,
126 DRM_PLANE_COMMIT_ACTIVE_ONLY
|
127 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET
);
129 drm_atomic_helper_commit_modeset_enables(dev
, state
);
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;
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.
143 drm_atomic_helper_wait_for_flip_done(dev
, state
);
145 if (plane_disabling
) {
146 for_each_old_plane_in_state(state
, plane
, old_plane_state
, i
)
147 ipu_plane_disable_deferred(plane
);
151 drm_atomic_helper_commit_hw_done(state
);
154 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
155 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
159 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
160 struct drm_encoder
*encoder
, struct device_node
*np
)
162 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
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.
171 return -EPROBE_DEFER
;
173 encoder
->possible_crtcs
= crtc_mask
;
175 /* FIXME: this is the mask of outputs which can clone this output. */
176 encoder
->possible_clones
= ~0;
180 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
182 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
186 static struct drm_driver imx_drm_driver
= {
187 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
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
,
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
,
207 .desc
= "i.MX DRM graphics",
214 static int compare_of(struct device
*dev
, void *data
)
216 struct device_node
*np
= data
;
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
;
222 return pdata
->of_node
== np
;
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
);
231 return dev
->of_node
== np
;
234 static int imx_drm_bind(struct device
*dev
)
236 struct drm_device
*drm
;
237 struct imx_drm_device
*imxdrm
;
240 drm
= drm_dev_alloc(&imx_drm_driver
, dev
);
244 imxdrm
= devm_kzalloc(dev
, sizeof(*imxdrm
), GFP_KERNEL
);
251 drm
->dev_private
= imxdrm
;
254 * enable drm irq mode.
255 * - with irq_enabled = true, we can use the vblank feature.
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
262 drm
->irq_enabled
= true;
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().
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
;
276 drm_mode_config_init(drm
);
278 ret
= drm_vblank_init(drm
, MAX_CRTC
);
282 dev_set_drvdata(dev
, drm
);
284 /* Now try and bind all our sub-components */
285 ret
= component_bind_all(dev
, drm
);
289 drm_mode_config_reset(drm
);
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.
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");
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
;
309 drm_kms_helper_poll_init(drm
);
311 ret
= drm_dev_register(drm
, 0);
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
);
324 component_unbind_all(drm
->dev
, drm
);
326 drm_mode_config_cleanup(drm
);
333 static void imx_drm_unbind(struct device
*dev
)
335 struct drm_device
*drm
= dev_get_drvdata(dev
);
336 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
338 drm_dev_unregister(drm
);
340 drm_kms_helper_poll_fini(drm
);
342 if (imxdrm
->fbhelper
)
343 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
345 drm_mode_config_cleanup(drm
);
347 component_unbind_all(drm
->dev
, drm
);
348 dev_set_drvdata(dev
, NULL
);
353 static const struct component_master_ops imx_drm_ops
= {
354 .bind
= imx_drm_bind
,
355 .unbind
= imx_drm_unbind
,
358 static int imx_drm_platform_probe(struct platform_device
*pdev
)
360 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
363 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
368 static int imx_drm_platform_remove(struct platform_device
*pdev
)
370 component_master_del(&pdev
->dev
, &imx_drm_ops
);
374 #ifdef CONFIG_PM_SLEEP
375 static int imx_drm_suspend(struct device
*dev
)
377 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
378 struct imx_drm_device
*imxdrm
;
380 /* The drm_dev is NULL before .load hook is called */
384 drm_kms_helper_poll_disable(drm_dev
);
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
);
396 static int imx_drm_resume(struct device
*dev
)
398 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
399 struct imx_drm_device
*imx_drm
;
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
);
412 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
414 static const struct of_device_id imx_drm_dt_ids
[] = {
415 { .compatible
= "fsl,imx-display-subsystem", },
418 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
420 static struct platform_driver imx_drm_pdrv
= {
421 .probe
= imx_drm_platform_probe
,
422 .remove
= imx_drm_platform_remove
,
425 .pm
= &imx_drm_pm_ops
,
426 .of_match_table
= imx_drm_dt_ids
,
430 static struct platform_driver
* const drivers
[] = {
435 static int __init
imx_drm_init(void)
437 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
439 module_init(imx_drm_init
);
441 static void __exit
imx_drm_exit(void)
443 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
445 module_exit(imx_drm_exit
);
447 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
448 MODULE_DESCRIPTION("i.MX drm driver core");
449 MODULE_LICENSE("GPL");