drv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * Authors: Philippe Cornu <philippe.cornu@st.com>
  6. * Yannick Fertre <yannick.fertre@st.com>
  7. * Fabien Dessenne <fabien.dessenne@st.com>
  8. * Mickael Reulier <mickael.reulier@st.com>
  9. */
  10. #include <linux/component.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/module.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/pm_runtime.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_fb_helper.h>
  20. #include <drm/drm_gem_cma_helper.h>
  21. #include <drm/drm_gem_framebuffer_helper.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_vblank.h>
  24. #include "ltdc.h"
  25. #define STM_MAX_FB_WIDTH 2048
  26. #define STM_MAX_FB_HEIGHT 2048 /* same as width to handle orientation */
  27. static const struct drm_mode_config_funcs drv_mode_config_funcs = {
  28. .fb_create = drm_gem_fb_create,
  29. .atomic_check = drm_atomic_helper_check,
  30. .atomic_commit = drm_atomic_helper_commit,
  31. };
  32. static int stm_gem_cma_dumb_create(struct drm_file *file,
  33. struct drm_device *dev,
  34. struct drm_mode_create_dumb *args)
  35. {
  36. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  37. /*
  38. * in order to optimize data transfer, pitch is aligned on
  39. * 128 bytes, height is aligned on 4 bytes
  40. */
  41. args->pitch = roundup(min_pitch, 128);
  42. args->height = roundup(args->height, 4);
  43. return drm_gem_cma_dumb_create_internal(file, dev, args);
  44. }
  45. DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
  46. static struct drm_driver drv_driver = {
  47. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  48. .name = "stm",
  49. .desc = "STMicroelectronics SoC DRM",
  50. .date = "20170330",
  51. .major = 1,
  52. .minor = 0,
  53. .patchlevel = 0,
  54. .fops = &drv_driver_fops,
  55. DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(stm_gem_cma_dumb_create),
  56. };
  57. static int drv_load(struct drm_device *ddev)
  58. {
  59. struct platform_device *pdev = to_platform_device(ddev->dev);
  60. struct ltdc_device *ldev;
  61. int ret;
  62. DRM_DEBUG("%s\n", __func__);
  63. ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL);
  64. if (!ldev)
  65. return -ENOMEM;
  66. ddev->dev_private = (void *)ldev;
  67. ret = drmm_mode_config_init(ddev);
  68. if (ret)
  69. return ret;
  70. /*
  71. * set max width and height as default value.
  72. * this value would be used to check framebuffer size limitation
  73. * at drm_mode_addfb().
  74. */
  75. ddev->mode_config.min_width = 0;
  76. ddev->mode_config.min_height = 0;
  77. ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
  78. ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
  79. ddev->mode_config.funcs = &drv_mode_config_funcs;
  80. ret = ltdc_load(ddev);
  81. if (ret)
  82. return ret;
  83. drm_mode_config_reset(ddev);
  84. drm_kms_helper_poll_init(ddev);
  85. platform_set_drvdata(pdev, ddev);
  86. return 0;
  87. }
  88. static void drv_unload(struct drm_device *ddev)
  89. {
  90. DRM_DEBUG("%s\n", __func__);
  91. drm_kms_helper_poll_fini(ddev);
  92. ltdc_unload(ddev);
  93. }
  94. static __maybe_unused int drv_suspend(struct device *dev)
  95. {
  96. struct drm_device *ddev = dev_get_drvdata(dev);
  97. struct ltdc_device *ldev = ddev->dev_private;
  98. struct drm_atomic_state *state;
  99. WARN_ON(ldev->suspend_state);
  100. state = drm_atomic_helper_suspend(ddev);
  101. if (IS_ERR(state))
  102. return PTR_ERR(state);
  103. ldev->suspend_state = state;
  104. pm_runtime_force_suspend(dev);
  105. return 0;
  106. }
  107. static __maybe_unused int drv_resume(struct device *dev)
  108. {
  109. struct drm_device *ddev = dev_get_drvdata(dev);
  110. struct ltdc_device *ldev = ddev->dev_private;
  111. int ret;
  112. if (WARN_ON(!ldev->suspend_state))
  113. return -ENOENT;
  114. pm_runtime_force_resume(dev);
  115. ret = drm_atomic_helper_resume(ddev, ldev->suspend_state);
  116. if (ret)
  117. pm_runtime_force_suspend(dev);
  118. ldev->suspend_state = NULL;
  119. return ret;
  120. }
  121. static __maybe_unused int drv_runtime_suspend(struct device *dev)
  122. {
  123. struct drm_device *ddev = dev_get_drvdata(dev);
  124. DRM_DEBUG_DRIVER("\n");
  125. ltdc_suspend(ddev);
  126. return 0;
  127. }
  128. static __maybe_unused int drv_runtime_resume(struct device *dev)
  129. {
  130. struct drm_device *ddev = dev_get_drvdata(dev);
  131. DRM_DEBUG_DRIVER("\n");
  132. return ltdc_resume(ddev);
  133. }
  134. static const struct dev_pm_ops drv_pm_ops = {
  135. SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
  136. SET_RUNTIME_PM_OPS(drv_runtime_suspend,
  137. drv_runtime_resume, NULL)
  138. };
  139. static int stm_drm_platform_probe(struct platform_device *pdev)
  140. {
  141. struct device *dev = &pdev->dev;
  142. struct drm_device *ddev;
  143. int ret;
  144. DRM_DEBUG("%s\n", __func__);
  145. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  146. ddev = drm_dev_alloc(&drv_driver, dev);
  147. if (IS_ERR(ddev))
  148. return PTR_ERR(ddev);
  149. ret = drv_load(ddev);
  150. if (ret)
  151. goto err_put;
  152. ret = drm_dev_register(ddev, 0);
  153. if (ret)
  154. goto err_put;
  155. drm_fbdev_generic_setup(ddev, 16);
  156. return 0;
  157. err_put:
  158. drm_dev_put(ddev);
  159. return ret;
  160. }
  161. static int stm_drm_platform_remove(struct platform_device *pdev)
  162. {
  163. struct drm_device *ddev = platform_get_drvdata(pdev);
  164. DRM_DEBUG("%s\n", __func__);
  165. drm_dev_unregister(ddev);
  166. drv_unload(ddev);
  167. drm_dev_put(ddev);
  168. return 0;
  169. }
  170. static const struct of_device_id drv_dt_ids[] = {
  171. { .compatible = "st,stm32-ltdc"},
  172. { /* end node */ },
  173. };
  174. MODULE_DEVICE_TABLE(of, drv_dt_ids);
  175. static struct platform_driver stm_drm_platform_driver = {
  176. .probe = stm_drm_platform_probe,
  177. .remove = stm_drm_platform_remove,
  178. .driver = {
  179. .name = "stm32-display",
  180. .of_match_table = drv_dt_ids,
  181. .pm = &drv_pm_ops,
  182. },
  183. };
  184. module_platform_driver(stm_drm_platform_driver);
  185. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  186. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  187. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  188. MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
  189. MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
  190. MODULE_LICENSE("GPL v2");