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 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
53 static int legacyfb_depth
= 16;
54 module_param(legacyfb_depth
, int, 0444);
57 int imx_drm_crtc_id(struct imx_drm_crtc
*crtc
)
61 EXPORT_SYMBOL_GPL(imx_drm_crtc_id
);
63 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
65 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
67 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
70 static int imx_drm_driver_unload(struct drm_device
*drm
)
72 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
74 drm_kms_helper_poll_fini(drm
);
77 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
79 component_unbind_all(drm
->dev
, drm
);
81 drm_vblank_cleanup(drm
);
82 drm_mode_config_cleanup(drm
);
84 platform_set_drvdata(drm
->platformdev
, NULL
);
89 static struct imx_drm_crtc
*imx_drm_find_crtc(struct drm_crtc
*crtc
)
91 struct imx_drm_device
*imxdrm
= crtc
->dev
->dev_private
;
94 for (i
= 0; i
< MAX_CRTC
; i
++)
95 if (imxdrm
->crtc
[i
] && imxdrm
->crtc
[i
]->crtc
== crtc
)
96 return imxdrm
->crtc
[i
];
101 int imx_drm_set_bus_format_pins(struct drm_encoder
*encoder
, u32 bus_format
,
102 int hsync_pin
, int vsync_pin
)
104 struct imx_drm_crtc_helper_funcs
*helper
;
105 struct imx_drm_crtc
*imx_crtc
;
107 imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
111 helper
= &imx_crtc
->imx_drm_helper_funcs
;
112 if (helper
->set_interface_pix_fmt
)
113 return helper
->set_interface_pix_fmt(encoder
->crtc
,
114 bus_format
, hsync_pin
, vsync_pin
);
117 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins
);
119 int imx_drm_set_bus_format(struct drm_encoder
*encoder
, u32 bus_format
)
121 return imx_drm_set_bus_format_pins(encoder
, bus_format
, 2, 3);
123 EXPORT_SYMBOL_GPL(imx_drm_set_bus_format
);
125 int imx_drm_crtc_vblank_get(struct imx_drm_crtc
*imx_drm_crtc
)
127 return drm_vblank_get(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
129 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get
);
131 void imx_drm_crtc_vblank_put(struct imx_drm_crtc
*imx_drm_crtc
)
133 drm_vblank_put(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
135 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put
);
137 void imx_drm_handle_vblank(struct imx_drm_crtc
*imx_drm_crtc
)
139 drm_handle_vblank(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
141 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank
);
143 static int imx_drm_enable_vblank(struct drm_device
*drm
, unsigned int pipe
)
145 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
146 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
152 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
155 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
161 static void imx_drm_disable_vblank(struct drm_device
*drm
, unsigned int pipe
)
163 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
164 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[pipe
];
169 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
172 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
175 static void imx_drm_driver_preclose(struct drm_device
*drm
,
176 struct drm_file
*file
)
180 if (!file
->is_master
)
183 for (i
= 0; i
< MAX_CRTC
; i
++)
184 imx_drm_disable_vblank(drm
, i
);
187 static const struct file_operations imx_drm_driver_fops
= {
188 .owner
= THIS_MODULE
,
190 .release
= drm_release
,
191 .unlocked_ioctl
= drm_ioctl
,
192 .mmap
= drm_gem_cma_mmap
,
195 .llseek
= noop_llseek
,
198 void imx_drm_connector_destroy(struct drm_connector
*connector
)
200 drm_connector_unregister(connector
);
201 drm_connector_cleanup(connector
);
203 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
205 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
207 drm_encoder_cleanup(encoder
);
209 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
211 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
213 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
215 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
218 static struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
219 .fb_create
= drm_fb_cma_create
,
220 .output_poll_changed
= imx_drm_output_poll_changed
,
224 * Main DRM initialisation. This binds, initialises and registers
225 * with DRM the subcomponents of the driver.
227 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
229 struct imx_drm_device
*imxdrm
;
230 struct drm_connector
*connector
;
233 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
239 drm
->dev_private
= imxdrm
;
242 * enable drm irq mode.
243 * - with irq_enabled = true, we can use the vblank feature.
245 * P.S. note that we wouldn't use drm irq handler but
246 * just specific driver own one instead because
247 * drm framework supports only one irq handler and
248 * drivers can well take care of their interrupts
250 drm
->irq_enabled
= true;
253 * set max width and height as default value(4096x4096).
254 * this value would be used to check framebuffer size limitation
255 * at drm_mode_addfb().
257 drm
->mode_config
.min_width
= 64;
258 drm
->mode_config
.min_height
= 64;
259 drm
->mode_config
.max_width
= 4096;
260 drm
->mode_config
.max_height
= 4096;
261 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
263 drm_mode_config_init(drm
);
265 ret
= drm_vblank_init(drm
, MAX_CRTC
);
270 * with vblank_disable_allowed = true, vblank interrupt will be
271 * disabled by drm timer once a current process gives up ownership
272 * of vblank event. (after drm_vblank_put function is called)
274 drm
->vblank_disable_allowed
= true;
276 platform_set_drvdata(drm
->platformdev
, drm
);
278 /* Now try and bind all our sub-components */
279 ret
= component_bind_all(drm
->dev
, drm
);
284 * All components are now added, we can publish the connector sysfs
285 * entries to userspace. This will generate hotplug events and so
286 * userspace will expect to be able to access DRM at this point.
288 list_for_each_entry(connector
, &drm
->mode_config
.connector_list
, head
) {
289 ret
= drm_connector_register(connector
);
292 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
294 connector
->name
, ret
);
300 * All components are now initialised, so setup the fb helper.
301 * The fb helper takes copies of key hardware information, so the
302 * crtcs/connectors/encoders must not change after this point.
304 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
305 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
306 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
309 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
310 drm
->mode_config
.num_crtc
, MAX_CRTC
);
311 if (IS_ERR(imxdrm
->fbhelper
)) {
312 ret
= PTR_ERR(imxdrm
->fbhelper
);
313 imxdrm
->fbhelper
= NULL
;
318 drm_kms_helper_poll_init(drm
);
323 component_unbind_all(drm
->dev
, drm
);
325 drm_vblank_cleanup(drm
);
327 drm_mode_config_cleanup(drm
);
333 * imx_drm_add_crtc - add a new crtc
335 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
336 struct imx_drm_crtc
**new_crtc
, struct drm_plane
*primary_plane
,
337 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
338 struct device_node
*port
)
340 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
341 struct imx_drm_crtc
*imx_drm_crtc
;
345 * The vblank arrays are dimensioned by MAX_CRTC - we can't
346 * pass IDs greater than this to those functions.
348 if (imxdrm
->pipes
>= MAX_CRTC
)
351 if (imxdrm
->drm
->open_count
)
354 imx_drm_crtc
= kzalloc(sizeof(*imx_drm_crtc
), GFP_KERNEL
);
358 imx_drm_crtc
->imx_drm_helper_funcs
= *imx_drm_helper_funcs
;
359 imx_drm_crtc
->pipe
= imxdrm
->pipes
++;
360 imx_drm_crtc
->crtc
= crtc
;
364 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = imx_drm_crtc
;
366 *new_crtc
= imx_drm_crtc
;
368 ret
= drm_mode_crtc_set_gamma_size(imx_drm_crtc
->crtc
, 256);
372 drm_crtc_helper_add(crtc
,
373 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
375 drm_crtc_init_with_planes(drm
, crtc
, primary_plane
, NULL
,
376 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
);
381 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
385 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
388 * imx_drm_remove_crtc - remove a crtc
390 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
392 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
394 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
396 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
402 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
404 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
405 struct drm_encoder
*encoder
, struct device_node
*np
)
407 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
410 * If we failed to find the CRTC(s) which this encoder is
411 * supposed to be connected to, it's because the CRTC has
412 * not been registered yet. Defer probing, and hope that
413 * the required CRTC is added later.
416 return -EPROBE_DEFER
;
418 encoder
->possible_crtcs
= crtc_mask
;
420 /* FIXME: this is the mask of outputs which can clone this output. */
421 encoder
->possible_clones
= ~0;
425 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
428 * @node: device tree node containing encoder input ports
429 * @encoder: drm_encoder
431 int imx_drm_encoder_get_mux_id(struct device_node
*node
,
432 struct drm_encoder
*encoder
)
434 struct imx_drm_crtc
*imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
435 struct device_node
*ep
;
436 struct of_endpoint endpoint
;
437 struct device_node
*port
;
440 if (!node
|| !imx_crtc
)
443 for_each_endpoint_of_node(node
, ep
) {
444 port
= of_graph_get_remote_port(ep
);
446 if (port
== imx_crtc
->crtc
->port
) {
447 ret
= of_graph_parse_endpoint(ep
, &endpoint
);
449 return ret
? ret
: endpoint
.port
;
455 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id
);
457 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
461 static struct drm_driver imx_drm_driver
= {
462 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
463 .load
= imx_drm_driver_load
,
464 .unload
= imx_drm_driver_unload
,
465 .lastclose
= imx_drm_driver_lastclose
,
466 .preclose
= imx_drm_driver_preclose
,
467 .set_busid
= drm_platform_set_busid
,
468 .gem_free_object
= drm_gem_cma_free_object
,
469 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
470 .dumb_create
= drm_gem_cma_dumb_create
,
471 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
472 .dumb_destroy
= drm_gem_dumb_destroy
,
474 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
475 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
476 .gem_prime_import
= drm_gem_prime_import
,
477 .gem_prime_export
= drm_gem_prime_export
,
478 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
479 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
480 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
481 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
482 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
483 .get_vblank_counter
= drm_vblank_no_hw_counter
,
484 .enable_vblank
= imx_drm_enable_vblank
,
485 .disable_vblank
= imx_drm_disable_vblank
,
486 .ioctls
= imx_drm_ioctls
,
487 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
488 .fops
= &imx_drm_driver_fops
,
490 .desc
= "i.MX DRM graphics",
497 static int compare_of(struct device
*dev
, void *data
)
499 struct device_node
*np
= data
;
501 /* Special case for LDB, one device for two channels */
502 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
503 np
= of_get_parent(np
);
507 return dev
->of_node
== np
;
510 static int imx_drm_bind(struct device
*dev
)
512 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
515 static void imx_drm_unbind(struct device
*dev
)
517 drm_put_dev(dev_get_drvdata(dev
));
520 static const struct component_master_ops imx_drm_ops
= {
521 .bind
= imx_drm_bind
,
522 .unbind
= imx_drm_unbind
,
525 static int imx_drm_platform_probe(struct platform_device
*pdev
)
527 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
530 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
535 static int imx_drm_platform_remove(struct platform_device
*pdev
)
537 component_master_del(&pdev
->dev
, &imx_drm_ops
);
541 #ifdef CONFIG_PM_SLEEP
542 static int imx_drm_suspend(struct device
*dev
)
544 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
546 /* The drm_dev is NULL before .load hook is called */
550 drm_kms_helper_poll_disable(drm_dev
);
555 static int imx_drm_resume(struct device
*dev
)
557 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
562 drm_helper_resume_force_mode(drm_dev
);
563 drm_kms_helper_poll_enable(drm_dev
);
569 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
571 static const struct of_device_id imx_drm_dt_ids
[] = {
572 { .compatible
= "fsl,imx-display-subsystem", },
575 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
577 static struct platform_driver imx_drm_pdrv
= {
578 .probe
= imx_drm_platform_probe
,
579 .remove
= imx_drm_platform_remove
,
582 .pm
= &imx_drm_pm_ops
,
583 .of_match_table
= imx_drm_dt_ids
,
586 module_platform_driver(imx_drm_pdrv
);
588 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
589 MODULE_DESCRIPTION("i.MX drm driver core");
590 MODULE_LICENSE("GPL");