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 const struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
175 .fb_create
= drm_fb_cma_create
,
176 .output_poll_changed
= imx_drm_output_poll_changed
,
177 .atomic_check
= drm_atomic_helper_check
,
178 .atomic_commit
= drm_atomic_helper_commit
,
181 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
183 struct drm_device
*dev
= state
->dev
;
184 struct drm_crtc
*crtc
;
185 struct drm_crtc_state
*crtc_state
;
186 struct drm_plane_state
*plane_state
;
187 struct drm_gem_cma_object
*cma_obj
;
189 unsigned shared_count
;
190 struct fence
**shared
;
194 /* Wait for fences. */
195 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
196 plane_state
= crtc
->primary
->state
;
197 if (plane_state
->fb
) {
198 cma_obj
= drm_fb_cma_get_gem_obj(plane_state
->fb
, 0);
199 if (cma_obj
->base
.dma_buf
) {
200 ret
= reservation_object_get_fences_rcu(
201 cma_obj
->base
.dma_buf
->resv
, &excl
,
202 &shared_count
, &shared
);
204 DRM_ERROR("failed to get fences "
208 fence_wait(excl
, false);
211 for (j
= 0; j
< shared_count
; i
++) {
212 fence_wait(shared
[j
], false);
213 fence_put(shared
[j
]);
219 drm_atomic_helper_commit_modeset_disables(dev
, state
);
221 drm_atomic_helper_commit_planes(dev
, state
, true);
223 drm_atomic_helper_commit_modeset_enables(dev
, state
);
225 drm_atomic_helper_commit_hw_done(state
);
227 drm_atomic_helper_wait_for_vblanks(dev
, state
);
229 drm_atomic_helper_cleanup_planes(dev
, state
);
232 static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
233 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
237 * Main DRM initialisation. This binds, initialises and registers
238 * with DRM the subcomponents of the driver.
240 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
242 struct imx_drm_device
*imxdrm
;
243 struct drm_connector
*connector
;
246 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
252 drm
->dev_private
= imxdrm
;
255 * enable drm irq mode.
256 * - with irq_enabled = true, we can use the vblank feature.
258 * P.S. note that we wouldn't use drm irq handler but
259 * just specific driver own one instead because
260 * drm framework supports only one irq handler and
261 * drivers can well take care of their interrupts
263 drm
->irq_enabled
= true;
266 * set max width and height as default value(4096x4096).
267 * this value would be used to check framebuffer size limitation
268 * at drm_mode_addfb().
270 drm
->mode_config
.min_width
= 64;
271 drm
->mode_config
.min_height
= 64;
272 drm
->mode_config
.max_width
= 4096;
273 drm
->mode_config
.max_height
= 4096;
274 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
275 drm
->mode_config
.helper_private
= &imx_drm_mode_config_helpers
;
277 drm_mode_config_init(drm
);
279 ret
= drm_vblank_init(drm
, MAX_CRTC
);
283 platform_set_drvdata(drm
->platformdev
, drm
);
285 /* Now try and bind all our sub-components */
286 ret
= component_bind_all(drm
->dev
, drm
);
291 * All components are now added, we can publish the connector sysfs
292 * entries to userspace. This will generate hotplug events and so
293 * userspace will expect to be able to access DRM at this point.
295 list_for_each_entry(connector
, &drm
->mode_config
.connector_list
, head
) {
296 ret
= drm_connector_register(connector
);
299 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
301 connector
->name
, ret
);
306 drm_mode_config_reset(drm
);
309 * All components are now initialised, so setup the fb helper.
310 * The fb helper takes copies of key hardware information, so the
311 * crtcs/connectors/encoders must not change after this point.
313 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
314 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
315 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
318 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
319 drm
->mode_config
.num_crtc
, MAX_CRTC
);
320 if (IS_ERR(imxdrm
->fbhelper
)) {
321 ret
= PTR_ERR(imxdrm
->fbhelper
);
322 imxdrm
->fbhelper
= NULL
;
327 drm_kms_helper_poll_init(drm
);
332 component_unbind_all(drm
->dev
, drm
);
334 drm_vblank_cleanup(drm
);
336 drm_mode_config_cleanup(drm
);
342 * imx_drm_add_crtc - add a new crtc
344 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
345 struct imx_drm_crtc
**new_crtc
, struct drm_plane
*primary_plane
,
346 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
347 struct device_node
*port
)
349 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
350 struct imx_drm_crtc
*imx_drm_crtc
;
353 * The vblank arrays are dimensioned by MAX_CRTC - we can't
354 * pass IDs greater than this to those functions.
356 if (imxdrm
->pipes
>= MAX_CRTC
)
359 if (imxdrm
->drm
->open_count
)
362 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
366 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
367 imx_drm_crtc
->crtc
= crtc
;
371 imxdrm
->crtc
[imxdrm
->pipes
++] = imx_drm_crtc
;
373 *new_crtc
= imx_drm_crtc
;
375 drm_crtc_helper_add(crtc
,
376 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
378 drm_crtc_init_with_planes(drm
, crtc
, primary_plane
, NULL
,
379 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
, NULL
);
383 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
386 * imx_drm_remove_crtc - remove a crtc
388 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
390 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
391 unsigned int pipe
= drm_crtc_index(imx_drm_crtc
->crtc
);
393 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
395 imxdrm
->crtc
[pipe
] = NULL
;
401 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
403 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
404 struct drm_encoder
*encoder
, struct device_node
*np
)
406 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
409 * If we failed to find the CRTC(s) which this encoder is
410 * supposed to be connected to, it's because the CRTC has
411 * not been registered yet. Defer probing, and hope that
412 * the required CRTC is added later.
415 return -EPROBE_DEFER
;
417 encoder
->possible_crtcs
= crtc_mask
;
419 /* FIXME: this is the mask of outputs which can clone this output. */
420 encoder
->possible_clones
= ~0;
424 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
426 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
430 static struct drm_driver imx_drm_driver
= {
431 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
433 .load
= imx_drm_driver_load
,
434 .unload
= imx_drm_driver_unload
,
435 .lastclose
= imx_drm_driver_lastclose
,
436 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
437 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
438 .dumb_create
= drm_gem_cma_dumb_create
,
439 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
440 .dumb_destroy
= drm_gem_dumb_destroy
,
442 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
443 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
444 .gem_prime_import
= drm_gem_prime_import
,
445 .gem_prime_export
= drm_gem_prime_export
,
446 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
447 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
448 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
449 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
450 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
451 .get_vblank_counter
= drm_vblank_no_hw_counter
,
452 .enable_vblank
= imx_drm_enable_vblank
,
453 .disable_vblank
= imx_drm_disable_vblank
,
454 .ioctls
= imx_drm_ioctls
,
455 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
456 .fops
= &imx_drm_driver_fops
,
458 .desc
= "i.MX DRM graphics",
465 static int compare_of(struct device
*dev
, void *data
)
467 struct device_node
*np
= data
;
469 /* Special case for DI, dev->of_node may not be set yet */
470 if (strcmp(dev
->driver
->name
, "imx-ipuv3-crtc") == 0) {
471 struct ipu_client_platformdata
*pdata
= dev
->platform_data
;
473 return pdata
->of_node
== np
;
476 /* Special case for LDB, one device for two channels */
477 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
478 np
= of_get_parent(np
);
482 return dev
->of_node
== np
;
485 static int imx_drm_bind(struct device
*dev
)
487 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
490 static void imx_drm_unbind(struct device
*dev
)
492 drm_put_dev(dev_get_drvdata(dev
));
495 static const struct component_master_ops imx_drm_ops
= {
496 .bind
= imx_drm_bind
,
497 .unbind
= imx_drm_unbind
,
500 static int imx_drm_platform_probe(struct platform_device
*pdev
)
502 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
505 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
510 static int imx_drm_platform_remove(struct platform_device
*pdev
)
512 component_master_del(&pdev
->dev
, &imx_drm_ops
);
516 #ifdef CONFIG_PM_SLEEP
517 static int imx_drm_suspend(struct device
*dev
)
519 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
520 struct imx_drm_device
*imxdrm
;
522 /* The drm_dev is NULL before .load hook is called */
526 drm_kms_helper_poll_disable(drm_dev
);
528 imxdrm
= drm_dev
->dev_private
;
529 imxdrm
->state
= drm_atomic_helper_suspend(drm_dev
);
530 if (IS_ERR(imxdrm
->state
)) {
531 drm_kms_helper_poll_enable(drm_dev
);
532 return PTR_ERR(imxdrm
->state
);
538 static int imx_drm_resume(struct device
*dev
)
540 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
541 struct imx_drm_device
*imx_drm
;
546 imx_drm
= drm_dev
->dev_private
;
547 drm_atomic_helper_resume(drm_dev
, imx_drm
->state
);
548 drm_kms_helper_poll_enable(drm_dev
);
554 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
556 static const struct of_device_id imx_drm_dt_ids
[] = {
557 { .compatible
= "fsl,imx-display-subsystem", },
560 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
562 static struct platform_driver imx_drm_pdrv
= {
563 .probe
= imx_drm_platform_probe
,
564 .remove
= imx_drm_platform_remove
,
567 .pm
= &imx_drm_pm_ops
,
568 .of_match_table
= imx_drm_dt_ids
,
571 module_platform_driver(imx_drm_pdrv
);
573 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
574 MODULE_DESCRIPTION("i.MX drm driver core");
575 MODULE_LICENSE("GPL");