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>
19 #include <linux/module.h>
20 #include <linux/of_graph.h>
21 #include <linux/platform_device.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
34 struct imx_drm_component
{
35 struct device_node
*of_node
;
36 struct list_head list
;
39 struct imx_drm_device
{
40 struct drm_device
*drm
;
41 struct imx_drm_crtc
*crtc
[MAX_CRTC
];
43 struct drm_fbdev_cma
*fbhelper
;
47 struct drm_crtc
*crtc
;
49 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs
;
50 struct device_node
*port
;
53 static int legacyfb_depth
= 16;
54 module_param(legacyfb_depth
, int, 0444);
56 int imx_drm_crtc_id(struct imx_drm_crtc
*crtc
)
60 EXPORT_SYMBOL_GPL(imx_drm_crtc_id
);
62 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
64 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
65 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
68 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
72 static int imx_drm_driver_unload(struct drm_device
*drm
)
74 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
75 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
78 drm_kms_helper_poll_fini(drm
);
80 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
82 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
85 component_unbind_all(drm
->dev
, drm
);
87 drm_vblank_cleanup(drm
);
88 drm_mode_config_cleanup(drm
);
93 static struct imx_drm_crtc
*imx_drm_find_crtc(struct drm_crtc
*crtc
)
95 struct imx_drm_device
*imxdrm
= crtc
->dev
->dev_private
;
98 for (i
= 0; i
< MAX_CRTC
; i
++)
99 if (imxdrm
->crtc
[i
] && imxdrm
->crtc
[i
]->crtc
== crtc
)
100 return imxdrm
->crtc
[i
];
105 int imx_drm_panel_format_pins(struct drm_encoder
*encoder
,
106 u32 interface_pix_fmt
, int hsync_pin
, int vsync_pin
)
108 struct imx_drm_crtc_helper_funcs
*helper
;
109 struct imx_drm_crtc
*imx_crtc
;
111 imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
115 helper
= &imx_crtc
->imx_drm_helper_funcs
;
116 if (helper
->set_interface_pix_fmt
)
117 return helper
->set_interface_pix_fmt(encoder
->crtc
,
118 encoder
->encoder_type
, interface_pix_fmt
,
119 hsync_pin
, vsync_pin
);
122 EXPORT_SYMBOL_GPL(imx_drm_panel_format_pins
);
124 int imx_drm_panel_format(struct drm_encoder
*encoder
, u32 interface_pix_fmt
)
126 return imx_drm_panel_format_pins(encoder
, interface_pix_fmt
, 2, 3);
128 EXPORT_SYMBOL_GPL(imx_drm_panel_format
);
130 int imx_drm_crtc_vblank_get(struct imx_drm_crtc
*imx_drm_crtc
)
132 return drm_vblank_get(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
134 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get
);
136 void imx_drm_crtc_vblank_put(struct imx_drm_crtc
*imx_drm_crtc
)
138 drm_vblank_put(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
140 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put
);
142 void imx_drm_handle_vblank(struct imx_drm_crtc
*imx_drm_crtc
)
144 drm_handle_vblank(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
146 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank
);
148 static int imx_drm_enable_vblank(struct drm_device
*drm
, int crtc
)
150 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
151 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[crtc
];
157 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
160 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
166 static void imx_drm_disable_vblank(struct drm_device
*drm
, int crtc
)
168 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
169 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[crtc
];
174 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
177 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
180 static void imx_drm_driver_preclose(struct drm_device
*drm
,
181 struct drm_file
*file
)
185 if (!file
->is_master
)
188 for (i
= 0; i
< MAX_CRTC
; i
++)
189 imx_drm_disable_vblank(drm
, i
);
192 static const struct file_operations imx_drm_driver_fops
= {
193 .owner
= THIS_MODULE
,
195 .release
= drm_release
,
196 .unlocked_ioctl
= drm_ioctl
,
197 .mmap
= drm_gem_cma_mmap
,
200 .llseek
= noop_llseek
,
203 void imx_drm_connector_destroy(struct drm_connector
*connector
)
205 drm_connector_unregister(connector
);
206 drm_connector_cleanup(connector
);
208 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
210 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
212 drm_encoder_cleanup(encoder
);
214 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
216 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
218 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
219 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
221 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
225 static struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
226 .fb_create
= drm_fb_cma_create
,
227 .output_poll_changed
= imx_drm_output_poll_changed
,
231 * Main DRM initialisation. This binds, initialises and registers
232 * with DRM the subcomponents of the driver.
234 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
236 struct imx_drm_device
*imxdrm
;
237 struct drm_connector
*connector
;
240 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
246 drm
->dev_private
= imxdrm
;
249 * enable drm irq mode.
250 * - with irq_enabled = true, we can use the vblank feature.
252 * P.S. note that we wouldn't use drm irq handler but
253 * just specific driver own one instead because
254 * drm framework supports only one irq handler and
255 * drivers can well take care of their interrupts
257 drm
->irq_enabled
= true;
260 * set max width and height as default value(4096x4096).
261 * this value would be used to check framebuffer size limitation
262 * at drm_mode_addfb().
264 drm
->mode_config
.min_width
= 64;
265 drm
->mode_config
.min_height
= 64;
266 drm
->mode_config
.max_width
= 4096;
267 drm
->mode_config
.max_height
= 4096;
268 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
270 drm_mode_config_init(drm
);
272 ret
= drm_vblank_init(drm
, MAX_CRTC
);
277 * with vblank_disable_allowed = true, vblank interrupt will be
278 * disabled by drm timer once a current process gives up ownership
279 * of vblank event. (after drm_vblank_put function is called)
281 drm
->vblank_disable_allowed
= true;
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
);
307 * All components are now initialised, so setup the fb helper.
308 * The fb helper takes copies of key hardware information, so the
309 * crtcs/connectors/encoders must not change after this point.
311 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
312 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
313 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
316 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
317 drm
->mode_config
.num_crtc
, MAX_CRTC
);
318 if (IS_ERR(imxdrm
->fbhelper
)) {
319 ret
= PTR_ERR(imxdrm
->fbhelper
);
320 imxdrm
->fbhelper
= NULL
;
325 drm_kms_helper_poll_init(drm
);
330 component_unbind_all(drm
->dev
, drm
);
332 drm_vblank_cleanup(drm
);
334 drm_mode_config_cleanup(drm
);
340 * imx_drm_add_crtc - add a new crtc
342 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
343 struct imx_drm_crtc
**new_crtc
,
344 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
345 struct device_node
*port
)
347 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
348 struct imx_drm_crtc
*imx_drm_crtc
;
352 * The vblank arrays are dimensioned by MAX_CRTC - we can't
353 * pass IDs greater than this to those functions.
355 if (imxdrm
->pipes
>= MAX_CRTC
)
358 if (imxdrm
->drm
->open_count
)
361 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
365 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
366 imx_drm_crtc
->pipe
= imxdrm
->pipes
++;
367 imx_drm_crtc
->port
= port
;
368 imx_drm_crtc
->crtc
= crtc
;
370 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = imx_drm_crtc
;
372 *new_crtc
= imx_drm_crtc
;
374 ret
= drm_mode_crtc_set_gamma_size(imx_drm_crtc
->crtc
, 256);
378 drm_crtc_helper_add(crtc
,
379 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
381 drm_crtc_init(drm
, crtc
,
382 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
);
387 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
391 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
394 * imx_drm_remove_crtc - remove a crtc
396 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
398 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
400 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
402 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
408 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
411 * Find the DRM CRTC possible mask for the connected endpoint.
413 * The encoder possible masks are defined by their position in the
414 * mode_config crtc_list. This means that CRTCs must not be added
415 * or removed once the DRM device has been fully initialised.
417 static uint32_t imx_drm_find_crtc_mask(struct imx_drm_device
*imxdrm
,
418 struct device_node
*endpoint
)
420 struct device_node
*port
;
423 port
= of_graph_get_remote_port(endpoint
);
428 for (i
= 0; i
< MAX_CRTC
; i
++) {
429 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[i
];
430 if (imx_drm_crtc
&& imx_drm_crtc
->port
== port
)
431 return drm_crtc_mask(imx_drm_crtc
->crtc
);
437 static struct device_node
*imx_drm_of_get_next_endpoint(
438 const struct device_node
*parent
, struct device_node
*prev
)
440 struct device_node
*node
= of_graph_get_next_endpoint(parent
, prev
);
445 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
446 struct drm_encoder
*encoder
, struct device_node
*np
)
448 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
449 struct device_node
*ep
= NULL
;
450 uint32_t crtc_mask
= 0;
456 ep
= imx_drm_of_get_next_endpoint(np
, ep
);
460 mask
= imx_drm_find_crtc_mask(imxdrm
, ep
);
463 * If we failed to find the CRTC(s) which this encoder is
464 * supposed to be connected to, it's because the CRTC has
465 * not been registered yet. Defer probing, and hope that
466 * the required CRTC is added later.
469 return -EPROBE_DEFER
;
479 encoder
->possible_crtcs
= crtc_mask
;
481 /* FIXME: this is the mask of outputs which can clone this output. */
482 encoder
->possible_clones
= ~0;
486 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
489 * @node: device tree node containing encoder input ports
490 * @encoder: drm_encoder
492 int imx_drm_encoder_get_mux_id(struct device_node
*node
,
493 struct drm_encoder
*encoder
)
495 struct imx_drm_crtc
*imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
496 struct device_node
*ep
= NULL
;
497 struct of_endpoint endpoint
;
498 struct device_node
*port
;
501 if (!node
|| !imx_crtc
)
505 ep
= imx_drm_of_get_next_endpoint(node
, ep
);
509 port
= of_graph_get_remote_port(ep
);
511 if (port
== imx_crtc
->port
) {
512 ret
= of_graph_parse_endpoint(ep
, &endpoint
);
513 return ret
? ret
: endpoint
.port
;
519 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id
);
521 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
525 static struct drm_driver imx_drm_driver
= {
526 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
527 .load
= imx_drm_driver_load
,
528 .unload
= imx_drm_driver_unload
,
529 .lastclose
= imx_drm_driver_lastclose
,
530 .preclose
= imx_drm_driver_preclose
,
531 .gem_free_object
= drm_gem_cma_free_object
,
532 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
533 .dumb_create
= drm_gem_cma_dumb_create
,
534 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
535 .dumb_destroy
= drm_gem_dumb_destroy
,
537 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
538 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
539 .gem_prime_import
= drm_gem_prime_import
,
540 .gem_prime_export
= drm_gem_prime_export
,
541 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
542 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
543 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
544 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
545 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
546 .get_vblank_counter
= drm_vblank_count
,
547 .enable_vblank
= imx_drm_enable_vblank
,
548 .disable_vblank
= imx_drm_disable_vblank
,
549 .ioctls
= imx_drm_ioctls
,
550 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
551 .fops
= &imx_drm_driver_fops
,
553 .desc
= "i.MX DRM graphics",
560 static int compare_of(struct device
*dev
, void *data
)
562 struct device_node
*np
= data
;
564 /* Special case for LDB, one device for two channels */
565 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
566 np
= of_get_parent(np
);
570 return dev
->of_node
== np
;
573 static int imx_drm_bind(struct device
*dev
)
575 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
578 static void imx_drm_unbind(struct device
*dev
)
580 drm_put_dev(dev_get_drvdata(dev
));
583 static const struct component_master_ops imx_drm_ops
= {
584 .bind
= imx_drm_bind
,
585 .unbind
= imx_drm_unbind
,
588 static int imx_drm_platform_probe(struct platform_device
*pdev
)
590 struct device_node
*ep
, *port
, *remote
;
591 struct component_match
*match
= NULL
;
596 * Bind the IPU display interface ports first, so that
597 * imx_drm_encoder_parse_of called from encoder .bind callbacks
601 port
= of_parse_phandle(pdev
->dev
.of_node
, "ports", i
);
605 component_match_add(&pdev
->dev
, &match
, compare_of
, port
);
609 dev_err(&pdev
->dev
, "missing 'ports' property\n");
613 /* Then bind all encoders */
615 port
= of_parse_phandle(pdev
->dev
.of_node
, "ports", i
);
619 for_each_child_of_node(port
, ep
) {
620 remote
= of_graph_get_remote_port_parent(ep
);
621 if (!remote
|| !of_device_is_available(remote
)) {
624 } else if (!of_device_is_available(remote
->parent
)) {
625 dev_warn(&pdev
->dev
, "parent device of %s is not available\n",
631 component_match_add(&pdev
->dev
, &match
, compare_of
, remote
);
637 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
641 return component_master_add_with_match(&pdev
->dev
, &imx_drm_ops
, match
);
644 static int imx_drm_platform_remove(struct platform_device
*pdev
)
646 component_master_del(&pdev
->dev
, &imx_drm_ops
);
650 static const struct of_device_id imx_drm_dt_ids
[] = {
651 { .compatible
= "fsl,imx-display-subsystem", },
654 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
656 static struct platform_driver imx_drm_pdrv
= {
657 .probe
= imx_drm_platform_probe
,
658 .remove
= imx_drm_platform_remove
,
660 .owner
= THIS_MODULE
,
662 .of_match_table
= imx_drm_dt_ids
,
665 module_platform_driver(imx_drm_pdrv
);
667 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
668 MODULE_DESCRIPTION("i.MX drm driver core");
669 MODULE_LICENSE("GPL");