drv.c 6.0 KB

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