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>
21 #include <linux/reservation.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <drm/drm_gem_cma_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>
37 struct imx_drm_component
{
38 struct device_node
*of_node
;
39 struct list_head list
;
42 struct imx_drm_device
{
43 struct drm_device
*drm
;
44 struct imx_drm_crtc
*crtc
[MAX_CRTC
];
46 struct drm_fbdev_cma
*fbhelper
;
47 struct drm_atomic_state
*state
;
51 struct drm_crtc
*crtc
;
52 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs
;
55 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
56 static int legacyfb_depth
= 16;
57 module_param(legacyfb_depth
, int, 0444);
60 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
62 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
64 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
67 static int imx_drm_enable_vblank(struct drm_device
*drm
, unsigned int pipe
)
69 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
70 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
76 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
79 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
85 static void imx_drm_disable_vblank(struct drm_device
*drm
, unsigned int pipe
)
87 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
88 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
93 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
96 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
99 static const struct file_operations imx_drm_driver_fops
= {
100 .owner
= THIS_MODULE
,
102 .release
= drm_release
,
103 .unlocked_ioctl
= drm_ioctl
,
104 .mmap
= drm_gem_cma_mmap
,
107 .llseek
= noop_llseek
,
110 void imx_drm_connector_destroy(struct drm_connector
*connector
)
112 drm_connector_unregister(connector
);
113 drm_connector_cleanup(connector
);
115 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
117 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
119 drm_encoder_cleanup(encoder
);
121 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
123 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
125 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
127 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
130 static int imx_drm_atomic_check(struct drm_device
*dev
,
131 struct drm_atomic_state
*state
)
135 ret
= drm_atomic_helper_check_modeset(dev
, state
);
139 ret
= drm_atomic_helper_check_planes(dev
, state
);
144 * Check modeset again in case crtc_state->mode_changed is
145 * updated in plane's ->atomic_check callback.
147 ret
= drm_atomic_helper_check_modeset(dev
, state
);
154 static int imx_drm_atomic_commit(struct drm_device
*dev
,
155 struct drm_atomic_state
*state
,
158 struct drm_plane_state
*plane_state
;
159 struct drm_plane
*plane
;
160 struct dma_buf
*dma_buf
;
164 * If the plane fb has an dma-buf attached, fish out the exclusive
165 * fence for the atomic helper to wait on.
167 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
168 if ((plane
->state
->fb
!= plane_state
->fb
) && plane_state
->fb
) {
169 dma_buf
= drm_fb_cma_get_gem_obj(plane_state
->fb
,
174 reservation_object_get_excl_rcu(dma_buf
->resv
);
178 return drm_atomic_helper_commit(dev
, state
, nonblock
);
181 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
182 .fb_create
= drm_fb_cma_create
,
183 .output_poll_changed
= imx_drm_output_poll_changed
,
184 .atomic_check
= imx_drm_atomic_check
,
185 .atomic_commit
= imx_drm_atomic_commit
,
188 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
190 struct drm_device
*dev
= state
->dev
;
192 drm_atomic_helper_commit_modeset_disables(dev
, state
);
194 drm_atomic_helper_commit_planes(dev
, state
,
195 DRM_PLANE_COMMIT_ACTIVE_ONLY
|
196 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET
);
198 drm_atomic_helper_commit_modeset_enables(dev
, state
);
200 drm_atomic_helper_commit_hw_done(state
);
202 drm_atomic_helper_wait_for_vblanks(dev
, state
);
204 drm_atomic_helper_cleanup_planes(dev
, state
);
207 static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
208 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
212 * imx_drm_add_crtc - add a new crtc
214 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
215 struct imx_drm_crtc
**new_crtc
, struct drm_plane
*primary_plane
,
216 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
217 struct device_node
*port
)
219 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
220 struct imx_drm_crtc
*imx_drm_crtc
;
223 * The vblank arrays are dimensioned by MAX_CRTC - we can't
224 * pass IDs greater than this to those functions.
226 if (imxdrm
->pipes
>= MAX_CRTC
)
229 if (imxdrm
->drm
->open_count
)
232 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
236 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
237 imx_drm_crtc
->crtc
= crtc
;
241 imxdrm
->crtc
[imxdrm
->pipes
++] = imx_drm_crtc
;
243 *new_crtc
= imx_drm_crtc
;
245 drm_crtc_helper_add(crtc
,
246 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
248 drm_crtc_init_with_planes(drm
, crtc
, primary_plane
, NULL
,
249 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
, NULL
);
253 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
256 * imx_drm_remove_crtc - remove a crtc
258 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
260 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
261 unsigned int pipe
= drm_crtc_index(imx_drm_crtc
->crtc
);
263 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
265 imxdrm
->crtc
[pipe
] = NULL
;
271 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
273 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
274 struct drm_encoder
*encoder
, struct device_node
*np
)
276 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
279 * If we failed to find the CRTC(s) which this encoder is
280 * supposed to be connected to, it's because the CRTC has
281 * not been registered yet. Defer probing, and hope that
282 * the required CRTC is added later.
285 return -EPROBE_DEFER
;
287 encoder
->possible_crtcs
= crtc_mask
;
289 /* FIXME: this is the mask of outputs which can clone this output. */
290 encoder
->possible_clones
= ~0;
294 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
296 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
300 static struct drm_driver imx_drm_driver
= {
301 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
303 .lastclose
= imx_drm_driver_lastclose
,
304 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
305 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
306 .dumb_create
= drm_gem_cma_dumb_create
,
307 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
308 .dumb_destroy
= drm_gem_dumb_destroy
,
310 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
311 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
312 .gem_prime_import
= drm_gem_prime_import
,
313 .gem_prime_export
= drm_gem_prime_export
,
314 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
315 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
316 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
317 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
318 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
319 .get_vblank_counter
= drm_vblank_no_hw_counter
,
320 .enable_vblank
= imx_drm_enable_vblank
,
321 .disable_vblank
= imx_drm_disable_vblank
,
322 .ioctls
= imx_drm_ioctls
,
323 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
324 .fops
= &imx_drm_driver_fops
,
326 .desc
= "i.MX DRM graphics",
333 static int compare_of(struct device
*dev
, void *data
)
335 struct device_node
*np
= data
;
337 /* Special case for DI, dev->of_node may not be set yet */
338 if (strcmp(dev
->driver
->name
, "imx-ipuv3-crtc") == 0) {
339 struct ipu_client_platformdata
*pdata
= dev
->platform_data
;
341 return pdata
->of_node
== np
;
344 /* Special case for LDB, one device for two channels */
345 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
346 np
= of_get_parent(np
);
350 return dev
->of_node
== np
;
353 static int imx_drm_bind(struct device
*dev
)
355 struct drm_device
*drm
;
356 struct imx_drm_device
*imxdrm
;
359 drm
= drm_dev_alloc(&imx_drm_driver
, dev
);
363 imxdrm
= devm_kzalloc(dev
, sizeof(*imxdrm
), GFP_KERNEL
);
370 drm
->dev_private
= imxdrm
;
373 * enable drm irq mode.
374 * - with irq_enabled = true, we can use the vblank feature.
376 * P.S. note that we wouldn't use drm irq handler but
377 * just specific driver own one instead because
378 * drm framework supports only one irq handler and
379 * drivers can well take care of their interrupts
381 drm
->irq_enabled
= true;
384 * set max width and height as default value(4096x4096).
385 * this value would be used to check framebuffer size limitation
386 * at drm_mode_addfb().
388 drm
->mode_config
.min_width
= 64;
389 drm
->mode_config
.min_height
= 64;
390 drm
->mode_config
.max_width
= 4096;
391 drm
->mode_config
.max_height
= 4096;
392 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
393 drm
->mode_config
.helper_private
= &imx_drm_mode_config_helpers
;
395 drm_mode_config_init(drm
);
397 ret
= drm_vblank_init(drm
, MAX_CRTC
);
401 dev_set_drvdata(dev
, drm
);
403 /* Now try and bind all our sub-components */
404 ret
= component_bind_all(dev
, drm
);
408 drm_mode_config_reset(drm
);
411 * All components are now initialised, so setup the fb helper.
412 * The fb helper takes copies of key hardware information, so the
413 * crtcs/connectors/encoders must not change after this point.
415 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
416 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
417 dev_warn(dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
420 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
421 drm
->mode_config
.num_crtc
, MAX_CRTC
);
422 if (IS_ERR(imxdrm
->fbhelper
)) {
423 ret
= PTR_ERR(imxdrm
->fbhelper
);
424 imxdrm
->fbhelper
= NULL
;
429 drm_kms_helper_poll_init(drm
);
431 ret
= drm_dev_register(drm
, 0);
438 drm_kms_helper_poll_fini(drm
);
439 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
440 if (imxdrm
->fbhelper
)
441 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
444 component_unbind_all(drm
->dev
, drm
);
446 drm_vblank_cleanup(drm
);
448 drm_mode_config_cleanup(drm
);
455 static void imx_drm_unbind(struct device
*dev
)
457 struct drm_device
*drm
= dev_get_drvdata(dev
);
458 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
460 drm_dev_unregister(drm
);
462 drm_kms_helper_poll_fini(drm
);
464 if (imxdrm
->fbhelper
)
465 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
467 drm_mode_config_cleanup(drm
);
469 component_unbind_all(drm
->dev
, drm
);
470 dev_set_drvdata(dev
, NULL
);
475 static const struct component_master_ops imx_drm_ops
= {
476 .bind
= imx_drm_bind
,
477 .unbind
= imx_drm_unbind
,
480 static int imx_drm_platform_probe(struct platform_device
*pdev
)
482 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
485 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
490 static int imx_drm_platform_remove(struct platform_device
*pdev
)
492 component_master_del(&pdev
->dev
, &imx_drm_ops
);
496 #ifdef CONFIG_PM_SLEEP
497 static int imx_drm_suspend(struct device
*dev
)
499 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
500 struct imx_drm_device
*imxdrm
;
502 /* The drm_dev is NULL before .load hook is called */
506 drm_kms_helper_poll_disable(drm_dev
);
508 imxdrm
= drm_dev
->dev_private
;
509 imxdrm
->state
= drm_atomic_helper_suspend(drm_dev
);
510 if (IS_ERR(imxdrm
->state
)) {
511 drm_kms_helper_poll_enable(drm_dev
);
512 return PTR_ERR(imxdrm
->state
);
518 static int imx_drm_resume(struct device
*dev
)
520 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
521 struct imx_drm_device
*imx_drm
;
526 imx_drm
= drm_dev
->dev_private
;
527 drm_atomic_helper_resume(drm_dev
, imx_drm
->state
);
528 drm_kms_helper_poll_enable(drm_dev
);
534 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
536 static const struct of_device_id imx_drm_dt_ids
[] = {
537 { .compatible
= "fsl,imx-display-subsystem", },
540 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
542 static struct platform_driver imx_drm_pdrv
= {
543 .probe
= imx_drm_platform_probe
,
544 .remove
= imx_drm_platform_remove
,
547 .pm
= &imx_drm_pm_ops
,
548 .of_match_table
= imx_drm_dt_ids
,
551 module_platform_driver(imx_drm_pdrv
);
553 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
554 MODULE_DESCRIPTION("i.MX drm driver core");
555 MODULE_LICENSE("GPL");