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/platform_device.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_fb_cma_helper.h>
33 struct imx_drm_device
{
34 struct drm_device
*drm
;
35 struct imx_drm_crtc
*crtc
[MAX_CRTC
];
37 struct drm_fbdev_cma
*fbhelper
;
41 struct drm_crtc
*crtc
;
43 struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs
;
49 static int legacyfb_depth
= 16;
50 module_param(legacyfb_depth
, int, 0444);
52 int imx_drm_crtc_id(struct imx_drm_crtc
*crtc
)
56 EXPORT_SYMBOL_GPL(imx_drm_crtc_id
);
58 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
60 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
61 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
64 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
68 static int imx_drm_driver_unload(struct drm_device
*drm
)
70 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
71 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
74 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
77 component_unbind_all(drm
->dev
, drm
);
79 drm_vblank_cleanup(drm
);
80 drm_kms_helper_poll_fini(drm
);
81 drm_mode_config_cleanup(drm
);
86 struct imx_drm_crtc
*imx_drm_find_crtc(struct drm_crtc
*crtc
)
88 struct imx_drm_device
*imxdrm
= crtc
->dev
->dev_private
;
91 for (i
= 0; i
< MAX_CRTC
; i
++)
92 if (imxdrm
->crtc
[i
] && imxdrm
->crtc
[i
]->crtc
== crtc
)
93 return imxdrm
->crtc
[i
];
98 int imx_drm_panel_format_pins(struct drm_encoder
*encoder
,
99 u32 interface_pix_fmt
, int hsync_pin
, int vsync_pin
)
101 struct imx_drm_crtc_helper_funcs
*helper
;
102 struct imx_drm_crtc
*imx_crtc
;
104 imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
108 helper
= &imx_crtc
->imx_drm_helper_funcs
;
109 if (helper
->set_interface_pix_fmt
)
110 return helper
->set_interface_pix_fmt(encoder
->crtc
,
111 encoder
->encoder_type
, interface_pix_fmt
,
112 hsync_pin
, vsync_pin
);
115 EXPORT_SYMBOL_GPL(imx_drm_panel_format_pins
);
117 int imx_drm_panel_format(struct drm_encoder
*encoder
, u32 interface_pix_fmt
)
119 return imx_drm_panel_format_pins(encoder
, interface_pix_fmt
, 2, 3);
121 EXPORT_SYMBOL_GPL(imx_drm_panel_format
);
123 int imx_drm_crtc_vblank_get(struct imx_drm_crtc
*imx_drm_crtc
)
125 return drm_vblank_get(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
127 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get
);
129 void imx_drm_crtc_vblank_put(struct imx_drm_crtc
*imx_drm_crtc
)
131 drm_vblank_put(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
133 EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put
);
135 void imx_drm_handle_vblank(struct imx_drm_crtc
*imx_drm_crtc
)
137 drm_handle_vblank(imx_drm_crtc
->crtc
->dev
, imx_drm_crtc
->pipe
);
139 EXPORT_SYMBOL_GPL(imx_drm_handle_vblank
);
141 static int imx_drm_enable_vblank(struct drm_device
*drm
, int crtc
)
143 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
144 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[crtc
];
150 if (!imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank
)
153 ret
= imx_drm_crtc
->imx_drm_helper_funcs
.enable_vblank(
159 static void imx_drm_disable_vblank(struct drm_device
*drm
, int crtc
)
161 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
162 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[crtc
];
167 if (!imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank
)
170 imx_drm_crtc
->imx_drm_helper_funcs
.disable_vblank(imx_drm_crtc
->crtc
);
173 static void imx_drm_driver_preclose(struct drm_device
*drm
,
174 struct drm_file
*file
)
178 if (!file
->is_master
)
181 for (i
= 0; i
< MAX_CRTC
; i
++)
182 imx_drm_disable_vblank(drm
, i
);
185 static const struct file_operations imx_drm_driver_fops
= {
186 .owner
= THIS_MODULE
,
188 .release
= drm_release
,
189 .unlocked_ioctl
= drm_ioctl
,
190 .mmap
= drm_gem_cma_mmap
,
193 .llseek
= noop_llseek
,
196 int imx_drm_connector_mode_valid(struct drm_connector
*connector
,
197 struct drm_display_mode
*mode
)
201 EXPORT_SYMBOL(imx_drm_connector_mode_valid
);
203 void imx_drm_connector_destroy(struct drm_connector
*connector
)
205 drm_sysfs_connector_remove(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 struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
217 .fb_create
= drm_fb_cma_create
,
221 * Main DRM initialisation. This binds, initialises and registers
222 * with DRM the subcomponents of the driver.
224 static int imx_drm_driver_load(struct drm_device
*drm
, unsigned long flags
)
226 struct imx_drm_device
*imxdrm
;
227 struct drm_connector
*connector
;
230 imxdrm
= devm_kzalloc(drm
->dev
, sizeof(*imxdrm
), GFP_KERNEL
);
236 drm
->dev_private
= imxdrm
;
239 * enable drm irq mode.
240 * - with irq_enabled = true, we can use the vblank feature.
242 * P.S. note that we wouldn't use drm irq handler but
243 * just specific driver own one instead because
244 * drm framework supports only one irq handler and
245 * drivers can well take care of their interrupts
247 drm
->irq_enabled
= true;
250 * set max width and height as default value(4096x4096).
251 * this value would be used to check framebuffer size limitation
252 * at drm_mode_addfb().
254 drm
->mode_config
.min_width
= 64;
255 drm
->mode_config
.min_height
= 64;
256 drm
->mode_config
.max_width
= 4096;
257 drm
->mode_config
.max_height
= 4096;
258 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
260 drm_mode_config_init(drm
);
262 drm_kms_helper_poll_init(drm
);
264 ret
= drm_vblank_init(drm
, MAX_CRTC
);
269 * with vblank_disable_allowed = true, vblank interrupt will be
270 * disabled by drm timer once a current process gives up ownership
271 * of vblank event. (after drm_vblank_put function is called)
273 drm
->vblank_disable_allowed
= true;
275 platform_set_drvdata(drm
->platformdev
, drm
);
277 /* Now try and bind all our sub-components */
278 ret
= component_bind_all(drm
->dev
, drm
);
283 * All components are now added, we can publish the connector sysfs
284 * entries to userspace. This will generate hotplug events and so
285 * userspace will expect to be able to access DRM at this point.
287 list_for_each_entry(connector
, &drm
->mode_config
.connector_list
, head
) {
288 ret
= drm_sysfs_connector_add(connector
);
291 "[CONNECTOR:%d:%s] drm_sysfs_connector_add failed: %d\n",
293 drm_get_connector_name(connector
), ret
);
299 * All components are now initialised, so setup the fb helper.
300 * The fb helper takes copies of key hardware information, so the
301 * crtcs/connectors/encoders must not change after this point.
303 #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
304 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
305 dev_warn(drm
->dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
308 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
,
309 drm
->mode_config
.num_crtc
, MAX_CRTC
);
310 if (IS_ERR(imxdrm
->fbhelper
)) {
311 ret
= PTR_ERR(imxdrm
->fbhelper
);
312 imxdrm
->fbhelper
= NULL
;
319 component_unbind_all(drm
->dev
, drm
);
321 drm_vblank_cleanup(drm
);
323 drm_kms_helper_poll_fini(drm
);
324 drm_mode_config_cleanup(drm
);
330 * imx_drm_add_crtc - add a new crtc
332 * The return value if !NULL is a cookie for the caller to pass to
333 * imx_drm_remove_crtc later.
335 int imx_drm_add_crtc(struct drm_device
*drm
, struct drm_crtc
*crtc
,
336 struct imx_drm_crtc
**new_crtc
,
337 const struct imx_drm_crtc_helper_funcs
*imx_drm_helper_funcs
,
338 void *cookie
, int id
)
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
->cookie
= cookie
;
361 imx_drm_crtc
->id
= id
;
362 imx_drm_crtc
->mux_id
= imx_drm_crtc
->pipe
;
363 imx_drm_crtc
->crtc
= crtc
;
365 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = imx_drm_crtc
;
367 *new_crtc
= imx_drm_crtc
;
369 ret
= drm_mode_crtc_set_gamma_size(imx_drm_crtc
->crtc
, 256);
373 drm_crtc_helper_add(crtc
,
374 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_helper_funcs
);
376 drm_crtc_init(drm
, crtc
,
377 imx_drm_crtc
->imx_drm_helper_funcs
.crtc_funcs
);
382 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
386 EXPORT_SYMBOL_GPL(imx_drm_add_crtc
);
389 * imx_drm_remove_crtc - remove a crtc
391 int imx_drm_remove_crtc(struct imx_drm_crtc
*imx_drm_crtc
)
393 struct imx_drm_device
*imxdrm
= imx_drm_crtc
->crtc
->dev
->dev_private
;
395 drm_crtc_cleanup(imx_drm_crtc
->crtc
);
397 imxdrm
->crtc
[imx_drm_crtc
->pipe
] = NULL
;
403 EXPORT_SYMBOL_GPL(imx_drm_remove_crtc
);
406 * Find the DRM CRTC possible mask for the device node cookie/id.
408 * The encoder possible masks are defined by their position in the
409 * mode_config crtc_list. This means that CRTCs must not be added
410 * or removed once the DRM device has been fully initialised.
412 static uint32_t imx_drm_find_crtc_mask(struct imx_drm_device
*imxdrm
,
413 void *cookie
, int id
)
417 for (i
= 0; i
< MAX_CRTC
; i
++) {
418 struct imx_drm_crtc
*imx_drm_crtc
= imxdrm
->crtc
[i
];
419 if (imx_drm_crtc
&& imx_drm_crtc
->id
== id
&&
420 imx_drm_crtc
->cookie
== cookie
)
421 return drm_crtc_mask(imx_drm_crtc
->crtc
);
427 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
428 struct drm_encoder
*encoder
, struct device_node
*np
)
430 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
431 uint32_t crtc_mask
= 0;
434 for (i
= 0; !ret
; i
++) {
435 struct of_phandle_args args
;
439 ret
= of_parse_phandle_with_args(np
, "crtcs", "#crtc-cells", i
,
446 id
= args
.args_count
> 0 ? args
.args
[0] : 0;
447 mask
= imx_drm_find_crtc_mask(imxdrm
, args
.np
, id
);
448 of_node_put(args
.np
);
451 * If we failed to find the CRTC(s) which this encoder is
452 * supposed to be connected to, it's because the CRTC has
453 * not been registered yet. Defer probing, and hope that
454 * the required CRTC is added later.
457 return -EPROBE_DEFER
;
462 encoder
->possible_crtcs
= crtc_mask
;
464 /* FIXME: this is the mask of outputs which can clone this output. */
465 encoder
->possible_clones
= ~0;
469 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
471 int imx_drm_encoder_get_mux_id(struct drm_encoder
*encoder
)
473 struct imx_drm_crtc
*imx_crtc
= imx_drm_find_crtc(encoder
->crtc
);
475 return imx_crtc
? imx_crtc
->mux_id
: -EINVAL
;
477 EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id
);
479 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
483 static struct drm_driver imx_drm_driver
= {
484 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
485 .load
= imx_drm_driver_load
,
486 .unload
= imx_drm_driver_unload
,
487 .lastclose
= imx_drm_driver_lastclose
,
488 .preclose
= imx_drm_driver_preclose
,
489 .gem_free_object
= drm_gem_cma_free_object
,
490 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
491 .dumb_create
= drm_gem_cma_dumb_create
,
492 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
493 .dumb_destroy
= drm_gem_dumb_destroy
,
495 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
496 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
497 .gem_prime_import
= drm_gem_prime_import
,
498 .gem_prime_export
= drm_gem_prime_export
,
499 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
500 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
501 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
502 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
503 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
504 .get_vblank_counter
= drm_vblank_count
,
505 .enable_vblank
= imx_drm_enable_vblank
,
506 .disable_vblank
= imx_drm_disable_vblank
,
507 .ioctls
= imx_drm_ioctls
,
508 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
509 .fops
= &imx_drm_driver_fops
,
511 .desc
= "i.MX DRM graphics",
518 static int compare_parent_of(struct device
*dev
, void *data
)
520 struct of_phandle_args
*args
= data
;
521 return dev
->parent
&& dev
->parent
->of_node
== args
->np
;
524 static int compare_of(struct device
*dev
, void *data
)
526 return dev
->of_node
== data
;
529 static int imx_drm_add_components(struct device
*master
, struct master
*m
)
531 struct device_node
*np
= master
->of_node
;
536 struct of_phandle_args args
;
538 ret
= of_parse_phandle_with_fixed_args(np
, "crtcs", 1,
543 ret
= component_master_add_child(m
, compare_parent_of
, &args
);
544 of_node_put(args
.np
);
551 struct device_node
*node
;
553 node
= of_parse_phandle(np
, "connectors", i
);
557 ret
= component_master_add_child(m
, compare_of
, node
);
566 static int imx_drm_bind(struct device
*dev
)
568 return drm_platform_init(&imx_drm_driver
, to_platform_device(dev
));
571 static void imx_drm_unbind(struct device
*dev
)
573 drm_put_dev(dev_get_drvdata(dev
));
576 static const struct component_master_ops imx_drm_ops
= {
577 .add_components
= imx_drm_add_components
,
578 .bind
= imx_drm_bind
,
579 .unbind
= imx_drm_unbind
,
582 static int imx_drm_platform_probe(struct platform_device
*pdev
)
586 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
590 return component_master_add(&pdev
->dev
, &imx_drm_ops
);
593 static int imx_drm_platform_remove(struct platform_device
*pdev
)
595 component_master_del(&pdev
->dev
, &imx_drm_ops
);
599 static const struct of_device_id imx_drm_dt_ids
[] = {
600 { .compatible
= "fsl,imx-drm", },
603 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
605 static struct platform_driver imx_drm_pdrv
= {
606 .probe
= imx_drm_platform_probe
,
607 .remove
= imx_drm_platform_remove
,
609 .owner
= THIS_MODULE
,
611 .of_match_table
= imx_drm_dt_ids
,
614 module_platform_driver(imx_drm_pdrv
);
616 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
617 MODULE_DESCRIPTION("i.MX drm driver core");
618 MODULE_LICENSE("GPL");