drv.c 5.7 KB

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