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>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/reservation.h>
24 #include <drm/drm_atomic.h>
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_fb_helper.h>
27 #include <drm/drm_crtc_helper.h>
28 #include <drm/drm_gem_cma_helper.h>
29 #include <drm/drm_fb_cma_helper.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_of.h>
32 #include <video/imx-ipu-v3.h>
38 struct imx_drm_component
{
39 struct device_node
*of_node
;
40 struct list_head list
;
43 struct imx_drm_device
{
44 struct drm_device
*drm
;
45 struct imx_drm_crtc
*crtc
[MAX_CRTC
];
47 struct drm_fbdev_cma
*fbhelper
;
48 struct drm_atomic_state
*state
;
52 struct drm_crtc
*crtc
;
53 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs
;
56 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
57 static int legacyfb_depth
= 16;
58 module_param(legacyfb_depth
, int, 0444);
61 unsigned int imx_drm_crtc_id(struct imx_drm_crtc
*crtc
)
63 return drm_crtc_index(crtc
->crtc
);
65 EXPORT_SYMBOL_GPL(imx_drm_crtc_id
);
67 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
69 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
71 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
74 static int imx_drm_driver_unload(struct drm_device
*drm
)
76 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
78 drm_kms_helper_poll_fini(drm
);
81 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
83 component_unbind_all(drm
->dev
, drm
);
85 drm_vblank_cleanup(drm
);
86 drm_mode_config_cleanup(drm
);
88 platform_set_drvdata(drm
->platformdev
, NULL
);
93 int imx_drm_crtc_vblank_get(struct imx_drm_crtc
*imx_drm_crtc
)
95 return drm_crtc_vblank_get(imx_drm_crtc
->crtc
);
97 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get
);
99 void imx_drm_crtc_vblank_put(struct imx_drm_crtc
*imx_drm_crtc
)
101 drm_crtc_vblank_put(imx_drm_crtc
->crtc
);
103 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put
);
105 void imx_drm_handle_vblank(struct imx_drm_crtc
*imx_drm_crtc
)
107 drm_crtc_handle_vblank(imx_drm_crtc
->crtc
);
109 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank
);
111 static int imx_drm_enable_vblank(struct drm_device
*drm
, unsigned int pipe
)
113 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
114 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
120 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
123 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
129 static void imx_drm_disable_vblank(struct drm_device
*drm
, unsigned int pipe
)
131 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
132 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
137 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
140 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
143 static const struct file_operations imx_drm_driver_fops
= {
144 .owner
= THIS_MODULE
,
146 .release
= drm_release
,
147 .unlocked_ioctl
= drm_ioctl
,
148 .mmap
= drm_gem_cma_mmap
,
151 .llseek
= noop_llseek
,
154 void imx_drm_connector_destroy(struct drm_connector
*connector
)
156 drm_connector_unregister(connector
);
157 drm_connector_cleanup(connector
);
159 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
161 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
163 drm_encoder_cleanup(encoder
);
165 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
167 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
169 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
171 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
174 static int imx_drm_atomic_check(struct drm_device
*dev
,
175 struct drm_atomic_state
*state
)
179 ret
= drm_atomic_helper_check_modeset(dev
, state
);
183 ret
= drm_atomic_helper_check_planes(dev
, state
);
188 * Check modeset again in case crtc_state->mode_changed is
189 * updated in plane's ->atomic_check callback.
191 ret
= drm_atomic_helper_check_modeset(dev
, state
);
198 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
199 .fb_create
= drm_fb_cma_create
,
200 .output_poll_changed
= imx_drm_output_poll_changed
,
201 .atomic_check
= imx_drm_atomic_check
,
202 .atomic_commit
= drm_atomic_helper_commit
,
205 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
207 struct drm_device
*dev
= state
->dev
;
208 struct drm_crtc
*crtc
;
209 struct drm_crtc_state
*crtc_state
;
210 struct drm_plane_state
*plane_state
;
211 struct drm_gem_cma_object
*cma_obj
;
213 unsigned shared_count
;
214 struct fence
**shared
;
218 /* Wait for fences. */
219 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
220 plane_state
= crtc
->primary
->state
;
221 if (plane_state
->fb
) {
222 cma_obj
= drm_fb_cma_get_gem_obj(plane_state
->fb
, 0);
223 if (cma_obj
->base
.dma_buf
) {
224 ret
= reservation_object_get_fences_rcu(
225 cma_obj
->base
.dma_buf
->resv
, &excl
,
226 &shared_count
, &shared
);
228 DRM_ERROR("failed to get fences "
232 fence_wait(excl
, false);
235 for (j
= 0; j
< shared_count
; i
++) {
236 fence_wait(shared
[j
], false);
237 fence_put(shared
[j
]);
243 drm_atomic_helper_commit_modeset_disables(dev
, state
);
245 drm_atomic_helper_commit_planes(dev
, state
, true);
247 drm_atomic_helper_commit_modeset_enables(dev
, state
);
249 drm_atomic_helper_commit_hw_done(state
);
251 drm_atomic_helper_wait_for_vblanks(dev
, state
);
253 drm_atomic_helper_cleanup_planes(dev
, state
);
256 static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
257 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
261 * Main DRM initialisation. This binds, initialises and registers
262 * with DRM the subcomponents of the driver.
264 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
266 struct imx_drm_device
*imxdrm
;
267 struct drm_connector
*connector
;
270 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
276 drm
->dev_private
= imxdrm
;
279 * enable drm irq mode.
280 * - with irq_enabled = true, we can use the vblank feature.
282 * P.S. note that we wouldn't use drm irq handler but
283 * just specific driver own one instead because
284 * drm framework supports only one irq handler and
285 * drivers can well take care of their interrupts
287 drm
->irq_enabled
= true;
290 * set max width and height as default value(4096x4096).
291 * this value would be used to check framebuffer size limitation
292 * at drm_mode_addfb().
294 drm
->mode_config
.min_width
= 64;
295 drm
->mode_config
.min_height
= 64;
296 drm
->mode_config
.max_width
= 4096;
297 drm
->mode_config
.max_height
= 4096;
298 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
299 drm
->mode_config
.helper_private
= &imx_drm_mode_config_helpers
;
301 drm_mode_config_init(drm
);
303 ret
= drm_vblank_init(drm
, MAX_CRTC
);
307 platform_set_drvdata(drm
->platformdev
, drm
);
309 /* Now try and bind all our sub-components */
310 ret
= component_bind_all(drm
->dev
, drm
);
315 * All components are now added, we can publish the connector sysfs
316 * entries to userspace. This will generate hotplug events and so
317 * userspace will expect to be able to access DRM at this point.
319 list_for_each_entry(connector
, &drm
->mode_config
.connector_list
, head
) {
320 ret
= drm_connector_register(connector
);
323 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
325 connector
->name
, ret
);
330 drm_mode_config_reset(drm
);
333 * All components are now initialised, so setup the fb helper.
334 * The fb helper takes copies of key hardware information, so the
335 * crtcs/connectors/encoders must not change after this point.
337 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
338 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
339 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
342 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
343 drm
->mode_config
.num_crtc
, MAX_CRTC
);
344 if (IS_ERR(imxdrm
->fbhelper
)) {
345 ret
= PTR_ERR(imxdrm
->fbhelper
);
346 imxdrm
->fbhelper
= NULL
;
351 drm_kms_helper_poll_init(drm
);
356 component_unbind_all(drm
->dev
, drm
);
358 drm_vblank_cleanup(drm
);
360 drm_mode_config_cleanup(drm
);
366 * imx_drm_add_crtc - add a new crtc
368 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
369 struct imx_drm_crtc
**new_crtc
, struct drm_plane
*primary_plane
,
370 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
371 struct device_node
*port
)
373 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
374 struct imx_drm_crtc
*imx_drm_crtc
;
377 * The vblank arrays are dimensioned by MAX_CRTC - we can't
378 * pass IDs greater than this to those functions.
380 if (imxdrm
->pipes
>= MAX_CRTC
)
383 if (imxdrm
->drm
->open_count
)
386 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
390 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
391 imx_drm_crtc
->crtc
= crtc
;
395 imxdrm
->crtc
[imxdrm
->pipes
++] = imx_drm_crtc
;
397 *new_crtc
= imx_drm_crtc
;
399 drm_crtc_helper_add(crtc
,
400 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
402 drm_crtc_init_with_planes(drm
, crtc
, primary_plane
, NULL
,
403 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
, NULL
);
407 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
410 * imx_drm_remove_crtc - remove a crtc
412 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
414 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
415 unsigned int pipe
= drm_crtc_index(imx_drm_crtc
->crtc
);
417 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
419 imxdrm
->crtc
[pipe
] = NULL
;
425 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
427 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
428 struct drm_encoder
*encoder
, struct device_node
*np
)
430 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
433 * If we failed to find the CRTC(s) which this encoder is
434 * supposed to be connected to, it's because the CRTC has
435 * not been registered yet. Defer probing, and hope that
436 * the required CRTC is added later.
439 return -EPROBE_DEFER
;
441 encoder
->possible_crtcs
= crtc_mask
;
443 /* FIXME: this is the mask of outputs which can clone this output. */
444 encoder
->possible_clones
= ~0;
448 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
450 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
454 static struct drm_driver imx_drm_driver
= {
455 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
457 .load
= imx_drm_driver_load
,
458 .unload
= imx_drm_driver_unload
,
459 .lastclose
= imx_drm_driver_lastclose
,
460 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
461 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
462 .dumb_create
= drm_gem_cma_dumb_create
,
463 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
464 .dumb_destroy
= drm_gem_dumb_destroy
,
466 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
467 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
468 .gem_prime_import
= drm_gem_prime_import
,
469 .gem_prime_export
= drm_gem_prime_export
,
470 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
471 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
472 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
473 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
474 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
475 .get_vblank_counter
= drm_vblank_no_hw_counter
,
476 .enable_vblank
= imx_drm_enable_vblank
,
477 .disable_vblank
= imx_drm_disable_vblank
,
478 .ioctls
= imx_drm_ioctls
,
479 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
480 .fops
= &imx_drm_driver_fops
,
482 .desc
= "i.MX DRM graphics",
489 static int compare_of(struct device
*dev
, void *data
)
491 struct device_node
*np
= data
;
493 /* Special case for DI, dev->of_node may not be set yet */
494 if (strcmp(dev
->driver
->name
, "imx-ipuv3-crtc") == 0) {
495 struct ipu_client_platformdata
*pdata
= dev
->platform_data
;
497 return pdata
->of_node
== np
;
500 /* Special case for LDB, one device for two channels */
501 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
502 np
= of_get_parent(np
);
506 return dev
->of_node
== np
;
509 static int imx_drm_bind(struct device
*dev
)
511 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
514 static void imx_drm_unbind(struct device
*dev
)
516 drm_put_dev(dev_get_drvdata(dev
));
519 static const struct component_master_ops imx_drm_ops
= {
520 .bind
= imx_drm_bind
,
521 .unbind
= imx_drm_unbind
,
524 static int imx_drm_platform_probe(struct platform_device
*pdev
)
526 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
529 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
534 static int imx_drm_platform_remove(struct platform_device
*pdev
)
536 component_master_del(&pdev
->dev
, &imx_drm_ops
);
540 #ifdef CONFIG_PM_SLEEP
541 static int imx_drm_suspend(struct device
*dev
)
543 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
544 struct imx_drm_device
*imxdrm
;
546 /* The drm_dev is NULL before .load hook is called */
550 drm_kms_helper_poll_disable(drm_dev
);
552 imxdrm
= drm_dev
->dev_private
;
553 imxdrm
->state
= drm_atomic_helper_suspend(drm_dev
);
554 if (IS_ERR(imxdrm
->state
)) {
555 drm_kms_helper_poll_enable(drm_dev
);
556 return PTR_ERR(imxdrm
->state
);
562 static int imx_drm_resume(struct device
*dev
)
564 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
565 struct imx_drm_device
*imx_drm
;
570 imx_drm
= drm_dev
->dev_private
;
571 drm_atomic_helper_resume(drm_dev
, imx_drm
->state
);
572 drm_kms_helper_poll_enable(drm_dev
);
578 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
580 static const struct of_device_id imx_drm_dt_ids
[] = {
581 { .compatible
= "fsl,imx-display-subsystem", },
584 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
586 static struct platform_driver imx_drm_pdrv
= {
587 .probe
= imx_drm_platform_probe
,
588 .remove
= imx_drm_platform_remove
,
591 .pm
= &imx_drm_pm_ops
,
592 .of_match_table
= imx_drm_dt_ids
,
595 module_platform_driver(imx_drm_pdrv
);
597 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
598 MODULE_DESCRIPTION("i.MX drm driver core");
599 MODULE_LICENSE("GPL");