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>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_fb_helper.h>
25 #include <drm/drm_crtc_helper.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_fb_cma_helper.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_of.h>
30 #include <video/imx-ipu-v3.h>
36 struct imx_drm_component
{
37 struct device_node
*of_node
;
38 struct list_head list
;
41 struct imx_drm_device
{
42 struct drm_device
*drm
;
44 struct drm_fbdev_cma
*fbhelper
;
45 struct drm_atomic_state
*state
;
48 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
49 static int legacyfb_depth
= 16;
50 module_param(legacyfb_depth
, int, 0444);
53 static void imx_drm_driver_lastclose(struct drm_device
*drm
)
55 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
57 drm_fbdev_cma_restore_mode(imxdrm
->fbhelper
);
60 static const struct file_operations imx_drm_driver_fops
= {
63 .release
= drm_release
,
64 .unlocked_ioctl
= drm_ioctl
,
65 .mmap
= drm_gem_cma_mmap
,
68 .llseek
= noop_llseek
,
71 void imx_drm_connector_destroy(struct drm_connector
*connector
)
73 drm_connector_unregister(connector
);
74 drm_connector_cleanup(connector
);
76 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy
);
78 void imx_drm_encoder_destroy(struct drm_encoder
*encoder
)
80 drm_encoder_cleanup(encoder
);
82 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy
);
84 static void imx_drm_output_poll_changed(struct drm_device
*drm
)
86 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
88 drm_fbdev_cma_hotplug_event(imxdrm
->fbhelper
);
91 static int imx_drm_atomic_check(struct drm_device
*dev
,
92 struct drm_atomic_state
*state
)
96 ret
= drm_atomic_helper_check_modeset(dev
, state
);
100 ret
= drm_atomic_helper_check_planes(dev
, state
);
105 * Check modeset again in case crtc_state->mode_changed is
106 * updated in plane's ->atomic_check callback.
108 ret
= drm_atomic_helper_check_modeset(dev
, state
);
115 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs
= {
116 .fb_create
= drm_fb_cma_create
,
117 .output_poll_changed
= imx_drm_output_poll_changed
,
118 .atomic_check
= imx_drm_atomic_check
,
119 .atomic_commit
= drm_atomic_helper_commit
,
122 static void imx_drm_atomic_commit_tail(struct drm_atomic_state
*state
)
124 struct drm_device
*dev
= state
->dev
;
126 drm_atomic_helper_commit_modeset_disables(dev
, state
);
128 drm_atomic_helper_commit_planes(dev
, state
,
129 DRM_PLANE_COMMIT_ACTIVE_ONLY
|
130 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET
);
132 drm_atomic_helper_commit_modeset_enables(dev
, state
);
134 drm_atomic_helper_commit_hw_done(state
);
136 drm_atomic_helper_wait_for_vblanks(dev
, state
);
138 drm_atomic_helper_cleanup_planes(dev
, state
);
141 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers
= {
142 .atomic_commit_tail
= imx_drm_atomic_commit_tail
,
146 int imx_drm_encoder_parse_of(struct drm_device
*drm
,
147 struct drm_encoder
*encoder
, struct device_node
*np
)
149 uint32_t crtc_mask
= drm_of_find_possible_crtcs(drm
, np
);
152 * If we failed to find the CRTC(s) which this encoder is
153 * supposed to be connected to, it's because the CRTC has
154 * not been registered yet. Defer probing, and hope that
155 * the required CRTC is added later.
158 return -EPROBE_DEFER
;
160 encoder
->possible_crtcs
= crtc_mask
;
162 /* FIXME: this is the mask of outputs which can clone this output. */
163 encoder
->possible_clones
= ~0;
167 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of
);
169 static const struct drm_ioctl_desc imx_drm_ioctls
[] = {
173 static struct drm_driver imx_drm_driver
= {
174 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
176 .lastclose
= imx_drm_driver_lastclose
,
177 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
178 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
179 .dumb_create
= drm_gem_cma_dumb_create
,
180 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
181 .dumb_destroy
= drm_gem_dumb_destroy
,
183 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
184 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
185 .gem_prime_import
= drm_gem_prime_import
,
186 .gem_prime_export
= drm_gem_prime_export
,
187 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
188 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
189 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
190 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
191 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
192 .ioctls
= imx_drm_ioctls
,
193 .num_ioctls
= ARRAY_SIZE(imx_drm_ioctls
),
194 .fops
= &imx_drm_driver_fops
,
196 .desc
= "i.MX DRM graphics",
203 static int compare_of(struct device
*dev
, void *data
)
205 struct device_node
*np
= data
;
207 /* Special case for DI, dev->of_node may not be set yet */
208 if (strcmp(dev
->driver
->name
, "imx-ipuv3-crtc") == 0) {
209 struct ipu_client_platformdata
*pdata
= dev
->platform_data
;
211 return pdata
->of_node
== np
;
214 /* Special case for LDB, one device for two channels */
215 if (of_node_cmp(np
->name
, "lvds-channel") == 0) {
216 np
= of_get_parent(np
);
220 return dev
->of_node
== np
;
223 static int imx_drm_bind(struct device
*dev
)
225 struct drm_device
*drm
;
226 struct imx_drm_device
*imxdrm
;
229 drm
= drm_dev_alloc(&imx_drm_driver
, dev
);
233 imxdrm
= devm_kzalloc(dev
, sizeof(*imxdrm
), GFP_KERNEL
);
240 drm
->dev_private
= imxdrm
;
243 * enable drm irq mode.
244 * - with irq_enabled = true, we can use the vblank feature.
246 * P.S. note that we wouldn't use drm irq handler but
247 * just specific driver own one instead because
248 * drm framework supports only one irq handler and
249 * drivers can well take care of their interrupts
251 drm
->irq_enabled
= true;
254 * set max width and height as default value(4096x4096).
255 * this value would be used to check framebuffer size limitation
256 * at drm_mode_addfb().
258 drm
->mode_config
.min_width
= 1;
259 drm
->mode_config
.min_height
= 1;
260 drm
->mode_config
.max_width
= 4096;
261 drm
->mode_config
.max_height
= 4096;
262 drm
->mode_config
.funcs
= &imx_drm_mode_config_funcs
;
263 drm
->mode_config
.helper_private
= &imx_drm_mode_config_helpers
;
265 drm_mode_config_init(drm
);
267 ret
= drm_vblank_init(drm
, MAX_CRTC
);
271 dev_set_drvdata(dev
, drm
);
273 /* Now try and bind all our sub-components */
274 ret
= component_bind_all(dev
, drm
);
278 drm_mode_config_reset(drm
);
281 * All components are now initialised, so setup the fb helper.
282 * The fb helper takes copies of key hardware information, so the
283 * crtcs/connectors/encoders must not change after this point.
285 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
286 if (legacyfb_depth
!= 16 && legacyfb_depth
!= 32) {
287 dev_warn(dev
, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
290 imxdrm
->fbhelper
= drm_fbdev_cma_init(drm
, legacyfb_depth
, MAX_CRTC
);
291 if (IS_ERR(imxdrm
->fbhelper
)) {
292 ret
= PTR_ERR(imxdrm
->fbhelper
);
293 imxdrm
->fbhelper
= NULL
;
298 drm_kms_helper_poll_init(drm
);
300 ret
= drm_dev_register(drm
, 0);
307 drm_kms_helper_poll_fini(drm
);
308 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
309 if (imxdrm
->fbhelper
)
310 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
313 component_unbind_all(drm
->dev
, drm
);
315 drm_vblank_cleanup(drm
);
317 drm_mode_config_cleanup(drm
);
324 static void imx_drm_unbind(struct device
*dev
)
326 struct drm_device
*drm
= dev_get_drvdata(dev
);
327 struct imx_drm_device
*imxdrm
= drm
->dev_private
;
329 drm_dev_unregister(drm
);
331 drm_kms_helper_poll_fini(drm
);
333 if (imxdrm
->fbhelper
)
334 drm_fbdev_cma_fini(imxdrm
->fbhelper
);
336 drm_mode_config_cleanup(drm
);
338 component_unbind_all(drm
->dev
, drm
);
339 dev_set_drvdata(dev
, NULL
);
344 static const struct component_master_ops imx_drm_ops
= {
345 .bind
= imx_drm_bind
,
346 .unbind
= imx_drm_unbind
,
349 static int imx_drm_platform_probe(struct platform_device
*pdev
)
351 int ret
= drm_of_component_probe(&pdev
->dev
, compare_of
, &imx_drm_ops
);
354 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
359 static int imx_drm_platform_remove(struct platform_device
*pdev
)
361 component_master_del(&pdev
->dev
, &imx_drm_ops
);
365 #ifdef CONFIG_PM_SLEEP
366 static int imx_drm_suspend(struct device
*dev
)
368 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
369 struct imx_drm_device
*imxdrm
;
371 /* The drm_dev is NULL before .load hook is called */
375 drm_kms_helper_poll_disable(drm_dev
);
377 imxdrm
= drm_dev
->dev_private
;
378 imxdrm
->state
= drm_atomic_helper_suspend(drm_dev
);
379 if (IS_ERR(imxdrm
->state
)) {
380 drm_kms_helper_poll_enable(drm_dev
);
381 return PTR_ERR(imxdrm
->state
);
387 static int imx_drm_resume(struct device
*dev
)
389 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
390 struct imx_drm_device
*imx_drm
;
395 imx_drm
= drm_dev
->dev_private
;
396 drm_atomic_helper_resume(drm_dev
, imx_drm
->state
);
397 drm_kms_helper_poll_enable(drm_dev
);
403 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops
, imx_drm_suspend
, imx_drm_resume
);
405 static const struct of_device_id imx_drm_dt_ids
[] = {
406 { .compatible
= "fsl,imx-display-subsystem", },
409 MODULE_DEVICE_TABLE(of
, imx_drm_dt_ids
);
411 static struct platform_driver imx_drm_pdrv
= {
412 .probe
= imx_drm_platform_probe
,
413 .remove
= imx_drm_platform_remove
,
416 .pm
= &imx_drm_pm_ops
,
417 .of_match_table
= imx_drm_dt_ids
,
420 module_platform_driver(imx_drm_pdrv
);
422 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
423 MODULE_DESCRIPTION("i.MX drm driver core");
424 MODULE_LICENSE("GPL");