Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Freescale i.MX drm driver
0004  *
0005  * Copyright (C) 2011 Sascha Hauer, Pengutronix
0006  */
0007 
0008 #include <linux/component.h>
0009 #include <linux/device.h>
0010 #include <linux/dma-buf.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 
0014 #include <video/imx-ipu-v3.h>
0015 
0016 #include <drm/drm_atomic.h>
0017 #include <drm/drm_atomic_helper.h>
0018 #include <drm/drm_drv.h>
0019 #include <drm/drm_fb_cma_helper.h>
0020 #include <drm/drm_fb_helper.h>
0021 #include <drm/drm_gem_cma_helper.h>
0022 #include <drm/drm_gem_framebuffer_helper.h>
0023 #include <drm/drm_managed.h>
0024 #include <drm/drm_of.h>
0025 #include <drm/drm_plane_helper.h>
0026 #include <drm/drm_probe_helper.h>
0027 #include <drm/drm_vblank.h>
0028 
0029 #include "imx-drm.h"
0030 #include "ipuv3-plane.h"
0031 
0032 #define MAX_CRTC    4
0033 
0034 static int legacyfb_depth = 16;
0035 module_param(legacyfb_depth, int, 0444);
0036 
0037 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
0038 
0039 void imx_drm_connector_destroy(struct drm_connector *connector)
0040 {
0041     drm_connector_unregister(connector);
0042     drm_connector_cleanup(connector);
0043 }
0044 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
0045 
0046 static int imx_drm_atomic_check(struct drm_device *dev,
0047                 struct drm_atomic_state *state)
0048 {
0049     int ret;
0050 
0051     ret = drm_atomic_helper_check(dev, state);
0052     if (ret)
0053         return ret;
0054 
0055     /*
0056      * Check modeset again in case crtc_state->mode_changed is
0057      * updated in plane's ->atomic_check callback.
0058      */
0059     ret = drm_atomic_helper_check_modeset(dev, state);
0060     if (ret)
0061         return ret;
0062 
0063     /* Assign PRG/PRE channels and check if all constrains are satisfied. */
0064     ret = ipu_planes_assign_pre(dev, state);
0065     if (ret)
0066         return ret;
0067 
0068     return ret;
0069 }
0070 
0071 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
0072     .fb_create = drm_gem_fb_create,
0073     .atomic_check = imx_drm_atomic_check,
0074     .atomic_commit = drm_atomic_helper_commit,
0075 };
0076 
0077 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
0078 {
0079     struct drm_device *dev = state->dev;
0080     struct drm_plane *plane;
0081     struct drm_plane_state *old_plane_state, *new_plane_state;
0082     bool plane_disabling = false;
0083     int i;
0084 
0085     drm_atomic_helper_commit_modeset_disables(dev, state);
0086 
0087     drm_atomic_helper_commit_planes(dev, state,
0088                 DRM_PLANE_COMMIT_ACTIVE_ONLY |
0089                 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
0090 
0091     drm_atomic_helper_commit_modeset_enables(dev, state);
0092 
0093     for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
0094         if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
0095             plane_disabling = true;
0096     }
0097 
0098     /*
0099      * The flip done wait is only strictly required by imx-drm if a deferred
0100      * plane disable is in-flight. As the core requires blocking commits
0101      * to wait for the flip it is done here unconditionally. This keeps the
0102      * workitem around a bit longer than required for the majority of
0103      * non-blocking commits, but we accept that for the sake of simplicity.
0104      */
0105     drm_atomic_helper_wait_for_flip_done(dev, state);
0106 
0107     if (plane_disabling) {
0108         for_each_old_plane_in_state(state, plane, old_plane_state, i)
0109             ipu_plane_disable_deferred(plane);
0110 
0111     }
0112 
0113     drm_atomic_helper_commit_hw_done(state);
0114 }
0115 
0116 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
0117     .atomic_commit_tail = imx_drm_atomic_commit_tail,
0118 };
0119 
0120 
0121 int imx_drm_encoder_parse_of(struct drm_device *drm,
0122     struct drm_encoder *encoder, struct device_node *np)
0123 {
0124     uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
0125 
0126     /*
0127      * If we failed to find the CRTC(s) which this encoder is
0128      * supposed to be connected to, it's because the CRTC has
0129      * not been registered yet.  Defer probing, and hope that
0130      * the required CRTC is added later.
0131      */
0132     if (crtc_mask == 0)
0133         return -EPROBE_DEFER;
0134 
0135     encoder->possible_crtcs = crtc_mask;
0136 
0137     /* FIXME: cloning support not clear, disable it all for now */
0138     encoder->possible_clones = 0;
0139 
0140     return 0;
0141 }
0142 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
0143 
0144 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
0145     /* none so far */
0146 };
0147 
0148 static int imx_drm_dumb_create(struct drm_file *file_priv,
0149                    struct drm_device *drm,
0150                    struct drm_mode_create_dumb *args)
0151 {
0152     u32 width = args->width;
0153     int ret;
0154 
0155     args->width = ALIGN(width, 8);
0156 
0157     ret = drm_gem_cma_dumb_create(file_priv, drm, args);
0158     if (ret)
0159         return ret;
0160 
0161     args->width = width;
0162     return ret;
0163 }
0164 
0165 static const struct drm_driver imx_drm_driver = {
0166     .driver_features    = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
0167     DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create),
0168     .ioctls         = imx_drm_ioctls,
0169     .num_ioctls     = ARRAY_SIZE(imx_drm_ioctls),
0170     .fops           = &imx_drm_driver_fops,
0171     .name           = "imx-drm",
0172     .desc           = "i.MX DRM graphics",
0173     .date           = "20120507",
0174     .major          = 1,
0175     .minor          = 0,
0176     .patchlevel     = 0,
0177 };
0178 
0179 static int compare_of(struct device *dev, void *data)
0180 {
0181     struct device_node *np = data;
0182 
0183     /* Special case for DI, dev->of_node may not be set yet */
0184     if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
0185         struct ipu_client_platformdata *pdata = dev->platform_data;
0186 
0187         return pdata->of_node == np;
0188     }
0189 
0190     /* Special case for LDB, one device for two channels */
0191     if (of_node_name_eq(np, "lvds-channel")) {
0192         np = of_get_parent(np);
0193         of_node_put(np);
0194     }
0195 
0196     return dev->of_node == np;
0197 }
0198 
0199 static int imx_drm_bind(struct device *dev)
0200 {
0201     struct drm_device *drm;
0202     int ret;
0203 
0204     drm = drm_dev_alloc(&imx_drm_driver, dev);
0205     if (IS_ERR(drm))
0206         return PTR_ERR(drm);
0207 
0208     /*
0209      * set max width and height as default value(4096x4096).
0210      * this value would be used to check framebuffer size limitation
0211      * at drm_mode_addfb().
0212      */
0213     drm->mode_config.min_width = 1;
0214     drm->mode_config.min_height = 1;
0215     drm->mode_config.max_width = 4096;
0216     drm->mode_config.max_height = 4096;
0217     drm->mode_config.funcs = &imx_drm_mode_config_funcs;
0218     drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
0219     drm->mode_config.normalize_zpos = true;
0220 
0221     ret = drmm_mode_config_init(drm);
0222     if (ret)
0223         goto err_kms;
0224 
0225     ret = drm_vblank_init(drm, MAX_CRTC);
0226     if (ret)
0227         goto err_kms;
0228 
0229     dev_set_drvdata(dev, drm);
0230 
0231     /* Now try and bind all our sub-components */
0232     ret = component_bind_all(dev, drm);
0233     if (ret)
0234         goto err_kms;
0235 
0236     drm_mode_config_reset(drm);
0237 
0238     /*
0239      * All components are now initialised, so setup the fb helper.
0240      * The fb helper takes copies of key hardware information, so the
0241      * crtcs/connectors/encoders must not change after this point.
0242      */
0243     if (legacyfb_depth != 16 && legacyfb_depth != 32) {
0244         dev_warn(dev, "Invalid legacyfb_depth.  Defaulting to 16bpp\n");
0245         legacyfb_depth = 16;
0246     }
0247 
0248     drm_kms_helper_poll_init(drm);
0249 
0250     ret = drm_dev_register(drm, 0);
0251     if (ret)
0252         goto err_poll_fini;
0253 
0254     drm_fbdev_generic_setup(drm, legacyfb_depth);
0255 
0256     return 0;
0257 
0258 err_poll_fini:
0259     drm_kms_helper_poll_fini(drm);
0260     component_unbind_all(drm->dev, drm);
0261 err_kms:
0262     drm_dev_put(drm);
0263 
0264     return ret;
0265 }
0266 
0267 static void imx_drm_unbind(struct device *dev)
0268 {
0269     struct drm_device *drm = dev_get_drvdata(dev);
0270 
0271     drm_dev_unregister(drm);
0272 
0273     drm_kms_helper_poll_fini(drm);
0274 
0275     component_unbind_all(drm->dev, drm);
0276 
0277     drm_dev_put(drm);
0278 
0279     dev_set_drvdata(dev, NULL);
0280 }
0281 
0282 static const struct component_master_ops imx_drm_ops = {
0283     .bind = imx_drm_bind,
0284     .unbind = imx_drm_unbind,
0285 };
0286 
0287 static int imx_drm_platform_probe(struct platform_device *pdev)
0288 {
0289     int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
0290 
0291     if (!ret)
0292         ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
0293 
0294     return ret;
0295 }
0296 
0297 static int imx_drm_platform_remove(struct platform_device *pdev)
0298 {
0299     component_master_del(&pdev->dev, &imx_drm_ops);
0300     return 0;
0301 }
0302 
0303 #ifdef CONFIG_PM_SLEEP
0304 static int imx_drm_suspend(struct device *dev)
0305 {
0306     struct drm_device *drm_dev = dev_get_drvdata(dev);
0307 
0308     return drm_mode_config_helper_suspend(drm_dev);
0309 }
0310 
0311 static int imx_drm_resume(struct device *dev)
0312 {
0313     struct drm_device *drm_dev = dev_get_drvdata(dev);
0314 
0315     return drm_mode_config_helper_resume(drm_dev);
0316 }
0317 #endif
0318 
0319 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
0320 
0321 static const struct of_device_id imx_drm_dt_ids[] = {
0322     { .compatible = "fsl,imx-display-subsystem", },
0323     { /* sentinel */ },
0324 };
0325 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
0326 
0327 static struct platform_driver imx_drm_pdrv = {
0328     .probe      = imx_drm_platform_probe,
0329     .remove     = imx_drm_platform_remove,
0330     .driver     = {
0331         .name   = "imx-drm",
0332         .pm = &imx_drm_pm_ops,
0333         .of_match_table = imx_drm_dt_ids,
0334     },
0335 };
0336 
0337 static struct platform_driver * const drivers[] = {
0338     &imx_drm_pdrv,
0339     &ipu_drm_driver,
0340 };
0341 
0342 static int __init imx_drm_init(void)
0343 {
0344     if (drm_firmware_drivers_only())
0345         return -ENODEV;
0346 
0347     return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
0348 }
0349 module_init(imx_drm_init);
0350 
0351 static void __exit imx_drm_exit(void)
0352 {
0353     platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
0354 }
0355 module_exit(imx_drm_exit);
0356 
0357 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
0358 MODULE_DESCRIPTION("i.MX drm driver core");
0359 MODULE_LICENSE("GPL");