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>
27 #include <drm/drm_plane_helper.h>
28 #include <drm/drm_of.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
;
52 static int legacyfb_depth
= 16;
53 module_param(legacyfb_depth
, int, 0444);
55 int imx_drm_crtc_id(struct imx_drm_crtc
*crtc
)
59 EXPORT_SYMBOL_GPL(imx_drm_crtc_id
);
61 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
63 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
64 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
66 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
70 static int imx_drm_driver_unload(struct drm_device
*drm
)
72 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
73 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
76 drm_kms_helper_poll_fini(drm
);
78 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
80 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 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_set_bus_format_pins(struct drm_encoder
*encoder
, u32 bus_format
,
106 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 bus_format
, hsync_pin
, vsync_pin
);
121 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins
);
123 int imx_drm_set_bus_format(struct drm_encoder
*encoder
, u32 bus_format
)
125 return imx_drm_set_bus_format_pins(encoder
, bus_format
, 2, 3);
127 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format
);
129 int imx_drm_crtc_vblank_get(struct imx_drm_crtc
*imx_drm_crtc
)
131 return drm_vblank_get(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
133 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get
);
135 void imx_drm_crtc_vblank_put(struct imx_drm_crtc
*imx_drm_crtc
)
137 drm_vblank_put(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
139 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put
);
141 void imx_drm_handle_vblank(struct imx_drm_crtc
*imx_drm_crtc
)
143 drm_handle_vblank(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
145 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank
);
147 static int imx_drm_enable_vblank(struct drm_device
*drm
, unsigned int pipe
)
149 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
150 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
156 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
159 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
165 static void imx_drm_disable_vblank(struct drm_device
*drm
, unsigned int pipe
)
167 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
168 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
173 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
176 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
179 static void imx_drm_driver_preclose(struct drm_device
*drm
,
180 struct drm_file
*file
)
184 if (!file
->is_master
)
187 for (i
= 0; i
< MAX_CRTC
; i
++)
188 imx_drm_disable_vblank(drm
, i
);
191 static const struct file_operations imx_drm_driver_fops
= {
192 .owner
= THIS_MODULE
,
194 .release
= drm_release
,
195 .unlocked_ioctl
= drm_ioctl
,
196 .mmap
= drm_gem_cma_mmap
,
199 .llseek
= noop_llseek
,
202 void imx_drm_connector_destroy(struct drm_connector
*connector
)
204 drm_connector_unregister(connector
);
205 drm_connector_cleanup(connector
);
207 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
209 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
211 drm_encoder_cleanup(encoder
);
213 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
215 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
217 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
218 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
220 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
224 static struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
225 .fb_create
= drm_fb_cma_create
,
226 .output_poll_changed
= imx_drm_output_poll_changed
,
230 * Main DRM initialisation. This binds, initialises and registers
231 * with DRM the subcomponents of the driver.
233 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
235 struct imx_drm_device
*imxdrm
;
236 struct drm_connector
*connector
;
239 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
245 drm
->dev_private
= imxdrm
;
248 * enable drm irq mode.
249 * - with irq_enabled = true, we can use the vblank feature.
251 * P.S. note that we wouldn't use drm irq handler but
252 * just specific driver own one instead because
253 * drm framework supports only one irq handler and
254 * drivers can well take care of their interrupts
256 drm
->irq_enabled
= true;
259 * set max width and height as default value(4096x4096).
260 * this value would be used to check framebuffer size limitation
261 * at drm_mode_addfb().
263 drm
->mode_config
.min_width
= 64;
264 drm
->mode_config
.min_height
= 64;
265 drm
->mode_config
.max_width
= 4096;
266 drm
->mode_config
.max_height
= 4096;
267 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
269 drm_mode_config_init(drm
);
271 ret
= drm_vblank_init(drm
, MAX_CRTC
);
276 * with vblank_disable_allowed = true, vblank interrupt will be
277 * disabled by drm timer once a current process gives up ownership
278 * of vblank event. (after drm_vblank_put function is called)
280 drm
->vblank_disable_allowed
= true;
282 platform_set_drvdata(drm
->platformdev
, drm
);
284 /* Now try and bind all our sub-components */
285 ret
= component_bind_all(drm
->dev
, drm
);
290 * All components are now added, we can publish the connector sysfs
291 * entries to userspace. This will generate hotplug events and so
292 * userspace will expect to be able to access DRM at this point.
294 list_for_each_entry(connector
, &drm
->mode_config
.connector_list
, head
) {
295 ret
= drm_connector_register(connector
);
298 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
300 connector
->name
, ret
);
306 * All components are now initialised, so setup the fb helper.
307 * The fb helper takes copies of key hardware information, so the
308 * crtcs/connectors/encoders must not change after this point.
310 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
311 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
312 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
315 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
316 drm
->mode_config
.num_crtc
, MAX_CRTC
);
317 if (IS_ERR(imxdrm
->fbhelper
)) {
318 ret
= PTR_ERR(imxdrm
->fbhelper
);
319 imxdrm
->fbhelper
= NULL
;
324 drm_kms_helper_poll_init(drm
);
329 component_unbind_all(drm
->dev
, drm
);
331 drm_vblank_cleanup(drm
);
333 drm_mode_config_cleanup(drm
);
339 * imx_drm_add_crtc - add a new crtc
341 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
342 struct imx_drm_crtc
**new_crtc
, struct drm_plane
*primary_plane
,
343 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
344 struct device_node
*port
)
346 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
347 struct imx_drm_crtc
*imx_drm_crtc
;
351 * The vblank arrays are dimensioned by MAX_CRTC - we can't
352 * pass IDs greater than this to those functions.
354 if (imxdrm
->pipes
>= MAX_CRTC
)
357 if (imxdrm
->drm
->open_count
)
360 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
364 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
365 imx_drm_crtc
->pipe
= imxdrm
->pipes
++;
366 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_with_planes(drm
, crtc
, primary_plane
, NULL
,
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
);
410 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
411 struct drm_encoder
*encoder
, struct device_node
*np
)
413 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
416 * If we failed to find the CRTC(s) which this encoder is
417 * supposed to be connected to, it's because the CRTC has
418 * not been registered yet. Defer probing, and hope that
419 * the required CRTC is added later.
422 return -EPROBE_DEFER
;
424 encoder
->possible_crtcs
= crtc_mask
;
426 /* FIXME: this is the mask of outputs which can clone this output. */
427 encoder
->possible_clones
= ~0;
431 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
434 * @node: device tree node containing encoder input ports
435 * @encoder: drm_encoder
437 int imx_drm_encoder_get_mux_id(struct device_node
*node
,
438 struct drm_encoder
*encoder
)
440 struct imx_drm_crtc
*imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
441 struct device_node
*ep
;
442 struct of_endpoint endpoint
;
443 struct device_node
*port
;
446 if (!node
|| !imx_crtc
)
449 for_each_endpoint_of_node(node
, ep
) {
450 port
= of_graph_get_remote_port(ep
);
452 if (port
== imx_crtc
->crtc
->port
) {
453 ret
= of_graph_parse_endpoint(ep
, &endpoint
);
455 return ret
? ret
: endpoint
.port
;
461 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id
);
463 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
467 static struct drm_driver imx_drm_driver
= {
468 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
469 .load
= imx_drm_driver_load
,
470 .unload
= imx_drm_driver_unload
,
471 .lastclose
= imx_drm_driver_lastclose
,
472 .preclose
= imx_drm_driver_preclose
,
473 .set_busid
= drm_platform_set_busid
,
474 .gem_free_object
= drm_gem_cma_free_object
,
475 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
476 .dumb_create
= drm_gem_cma_dumb_create
,
477 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
478 .dumb_destroy
= drm_gem_dumb_destroy
,
480 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
481 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
482 .gem_prime_import
= drm_gem_prime_import
,
483 .gem_prime_export
= drm_gem_prime_export
,
484 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
485 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
486 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
487 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
488 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
489 .get_vblank_counter
= drm_vblank_no_hw_counter
,
490 .enable_vblank
= imx_drm_enable_vblank
,
491 .disable_vblank
= imx_drm_disable_vblank
,
492 .ioctls
= imx_drm_ioctls
,
493 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
494 .fops
= &imx_drm_driver_fops
,
496 .desc
= "i.MX DRM graphics",
503 static int compare_of(struct device
*dev
, void *data
)
505 struct device_node
*np
= data
;
507 /* Special case for LDB, one device for two channels */
508 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
509 np
= of_get_parent(np
);
513 return dev
->of_node
== np
;
516 static int imx_drm_bind(struct device
*dev
)
518 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
521 static void imx_drm_unbind(struct device
*dev
)
523 drm_put_dev(dev_get_drvdata(dev
));
526 static const struct component_master_ops imx_drm_ops
= {
527 .bind
= imx_drm_bind
,
528 .unbind
= imx_drm_unbind
,
531 static int imx_drm_platform_probe(struct platform_device
*pdev
)
533 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
536 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
541 static int imx_drm_platform_remove(struct platform_device
*pdev
)
543 component_master_del(&pdev
->dev
, &imx_drm_ops
);
547 #ifdef CONFIG_PM_SLEEP
548 static int imx_drm_suspend(struct device
*dev
)
550 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
552 /* The drm_dev is NULL before .load hook is called */
556 drm_kms_helper_poll_disable(drm_dev
);
561 static int imx_drm_resume(struct device
*dev
)
563 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
568 drm_helper_resume_force_mode(drm_dev
);
569 drm_kms_helper_poll_enable(drm_dev
);
575 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
577 static const struct of_device_id imx_drm_dt_ids
[] = {
578 { .compatible
= "fsl,imx-display-subsystem", },
581 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
583 static struct platform_driver imx_drm_pdrv
= {
584 .probe
= imx_drm_platform_probe
,
585 .remove
= imx_drm_platform_remove
,
588 .pm
= &imx_drm_pm_ops
,
589 .of_match_table
= imx_drm_dt_ids
,
592 module_platform_driver(imx_drm_pdrv
);
594 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
595 MODULE_DESCRIPTION("i.MX drm driver core");
596 MODULE_LICENSE("GPL");