Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) STMicroelectronics SA 2017
0004  *
0005  * Authors: Philippe Cornu <philippe.cornu@st.com>
0006  *          Yannick Fertre <yannick.fertre@st.com>
0007  *          Fabien Dessenne <fabien.dessenne@st.com>
0008  *          Mickael Reulier <mickael.reulier@st.com>
0009  */
0010 
0011 #include <linux/component.h>
0012 #include <linux/dma-mapping.h>
0013 #include <linux/module.h>
0014 #include <linux/of_platform.h>
0015 #include <linux/pm_runtime.h>
0016 
0017 #include <drm/drm_aperture.h>
0018 #include <drm/drm_atomic.h>
0019 #include <drm/drm_atomic_helper.h>
0020 #include <drm/drm_drv.h>
0021 #include <drm/drm_fb_cma_helper.h>
0022 #include <drm/drm_fb_helper.h>
0023 #include <drm/drm_gem_cma_helper.h>
0024 #include <drm/drm_gem_framebuffer_helper.h>
0025 #include <drm/drm_module.h>
0026 #include <drm/drm_probe_helper.h>
0027 #include <drm/drm_vblank.h>
0028 
0029 #include "ltdc.h"
0030 
0031 #define STM_MAX_FB_WIDTH    2048
0032 #define STM_MAX_FB_HEIGHT   2048 /* same as width to handle orientation */
0033 
0034 static const struct drm_mode_config_funcs drv_mode_config_funcs = {
0035     .fb_create = drm_gem_fb_create,
0036     .atomic_check = drm_atomic_helper_check,
0037     .atomic_commit = drm_atomic_helper_commit,
0038 };
0039 
0040 static int stm_gem_cma_dumb_create(struct drm_file *file,
0041                    struct drm_device *dev,
0042                    struct drm_mode_create_dumb *args)
0043 {
0044     unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
0045 
0046     /*
0047      * in order to optimize data transfer, pitch is aligned on
0048      * 128 bytes, height is aligned on 4 bytes
0049      */
0050     args->pitch = roundup(min_pitch, 128);
0051     args->height = roundup(args->height, 4);
0052 
0053     return drm_gem_cma_dumb_create_internal(file, dev, args);
0054 }
0055 
0056 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
0057 
0058 static const struct drm_driver drv_driver = {
0059     .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
0060     .name = "stm",
0061     .desc = "STMicroelectronics SoC DRM",
0062     .date = "20170330",
0063     .major = 1,
0064     .minor = 0,
0065     .patchlevel = 0,
0066     .fops = &drv_driver_fops,
0067     DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(stm_gem_cma_dumb_create),
0068 };
0069 
0070 static int drv_load(struct drm_device *ddev)
0071 {
0072     struct platform_device *pdev = to_platform_device(ddev->dev);
0073     struct ltdc_device *ldev;
0074     int ret;
0075 
0076     DRM_DEBUG("%s\n", __func__);
0077 
0078     ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL);
0079     if (!ldev)
0080         return -ENOMEM;
0081 
0082     ddev->dev_private = (void *)ldev;
0083 
0084     ret = drmm_mode_config_init(ddev);
0085     if (ret)
0086         return ret;
0087 
0088     /*
0089      * set max width and height as default value.
0090      * this value would be used to check framebuffer size limitation
0091      * at drm_mode_addfb().
0092      */
0093     ddev->mode_config.min_width = 0;
0094     ddev->mode_config.min_height = 0;
0095     ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
0096     ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
0097     ddev->mode_config.funcs = &drv_mode_config_funcs;
0098     ddev->mode_config.normalize_zpos = true;
0099 
0100     ret = ltdc_load(ddev);
0101     if (ret)
0102         return ret;
0103 
0104     drm_mode_config_reset(ddev);
0105     drm_kms_helper_poll_init(ddev);
0106 
0107     platform_set_drvdata(pdev, ddev);
0108 
0109     return 0;
0110 }
0111 
0112 static void drv_unload(struct drm_device *ddev)
0113 {
0114     DRM_DEBUG("%s\n", __func__);
0115 
0116     drm_kms_helper_poll_fini(ddev);
0117     ltdc_unload(ddev);
0118 }
0119 
0120 static __maybe_unused int drv_suspend(struct device *dev)
0121 {
0122     struct drm_device *ddev = dev_get_drvdata(dev);
0123     struct ltdc_device *ldev = ddev->dev_private;
0124     struct drm_atomic_state *state;
0125 
0126     WARN_ON(ldev->suspend_state);
0127 
0128     state = drm_atomic_helper_suspend(ddev);
0129     if (IS_ERR(state))
0130         return PTR_ERR(state);
0131 
0132     ldev->suspend_state = state;
0133     pm_runtime_force_suspend(dev);
0134 
0135     return 0;
0136 }
0137 
0138 static __maybe_unused int drv_resume(struct device *dev)
0139 {
0140     struct drm_device *ddev = dev_get_drvdata(dev);
0141     struct ltdc_device *ldev = ddev->dev_private;
0142     int ret;
0143 
0144     if (WARN_ON(!ldev->suspend_state))
0145         return -ENOENT;
0146 
0147     pm_runtime_force_resume(dev);
0148     ret = drm_atomic_helper_resume(ddev, ldev->suspend_state);
0149     if (ret)
0150         pm_runtime_force_suspend(dev);
0151 
0152     ldev->suspend_state = NULL;
0153 
0154     return ret;
0155 }
0156 
0157 static __maybe_unused int drv_runtime_suspend(struct device *dev)
0158 {
0159     struct drm_device *ddev = dev_get_drvdata(dev);
0160 
0161     DRM_DEBUG_DRIVER("\n");
0162     ltdc_suspend(ddev);
0163 
0164     return 0;
0165 }
0166 
0167 static __maybe_unused int drv_runtime_resume(struct device *dev)
0168 {
0169     struct drm_device *ddev = dev_get_drvdata(dev);
0170 
0171     DRM_DEBUG_DRIVER("\n");
0172     return ltdc_resume(ddev);
0173 }
0174 
0175 static const struct dev_pm_ops drv_pm_ops = {
0176     SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
0177     SET_RUNTIME_PM_OPS(drv_runtime_suspend,
0178                drv_runtime_resume, NULL)
0179 };
0180 
0181 static int stm_drm_platform_probe(struct platform_device *pdev)
0182 {
0183     struct device *dev = &pdev->dev;
0184     struct drm_device *ddev;
0185     int ret;
0186 
0187     DRM_DEBUG("%s\n", __func__);
0188 
0189     ret = drm_aperture_remove_framebuffers(false, &drv_driver);
0190     if (ret)
0191         return ret;
0192 
0193     dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
0194 
0195     ddev = drm_dev_alloc(&drv_driver, dev);
0196     if (IS_ERR(ddev))
0197         return PTR_ERR(ddev);
0198 
0199     ret = drv_load(ddev);
0200     if (ret)
0201         goto err_put;
0202 
0203     ret = drm_dev_register(ddev, 0);
0204     if (ret)
0205         goto err_put;
0206 
0207     drm_fbdev_generic_setup(ddev, 16);
0208 
0209     return 0;
0210 
0211 err_put:
0212     drm_dev_put(ddev);
0213 
0214     return ret;
0215 }
0216 
0217 static int stm_drm_platform_remove(struct platform_device *pdev)
0218 {
0219     struct drm_device *ddev = platform_get_drvdata(pdev);
0220 
0221     DRM_DEBUG("%s\n", __func__);
0222 
0223     drm_dev_unregister(ddev);
0224     drv_unload(ddev);
0225     drm_dev_put(ddev);
0226 
0227     return 0;
0228 }
0229 
0230 static const struct of_device_id drv_dt_ids[] = {
0231     { .compatible = "st,stm32-ltdc"},
0232     { /* end node */ },
0233 };
0234 MODULE_DEVICE_TABLE(of, drv_dt_ids);
0235 
0236 static struct platform_driver stm_drm_platform_driver = {
0237     .probe = stm_drm_platform_probe,
0238     .remove = stm_drm_platform_remove,
0239     .driver = {
0240         .name = "stm32-display",
0241         .of_match_table = drv_dt_ids,
0242         .pm = &drv_pm_ops,
0243     },
0244 };
0245 
0246 drm_module_platform_driver(stm_drm_platform_driver);
0247 
0248 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
0249 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
0250 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
0251 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
0252 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
0253 MODULE_LICENSE("GPL v2");