rockchip_drm_fbdev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  4. * Author:Mark Yao <mark.yao@rock-chips.com>
  5. */
  6. #include <drm/drm.h>
  7. #include <drm/drm_fb_helper.h>
  8. #include <drm/drm_fourcc.h>
  9. #include <drm/drm_probe_helper.h>
  10. #include "rockchip_drm_drv.h"
  11. #include "rockchip_drm_gem.h"
  12. #include "rockchip_drm_fb.h"
  13. #include "rockchip_drm_fbdev.h"
  14. #define PREFERRED_BPP 32
  15. #define to_drm_private(x) \
  16. container_of(x, struct rockchip_drm_private, fbdev_helper)
  17. static int rockchip_fbdev_mmap(struct fb_info *info,
  18. struct vm_area_struct *vma)
  19. {
  20. struct drm_fb_helper *helper = info->par;
  21. struct rockchip_drm_private *private = to_drm_private(helper);
  22. return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
  23. }
  24. static const struct fb_ops rockchip_drm_fbdev_ops = {
  25. .owner = THIS_MODULE,
  26. DRM_FB_HELPER_DEFAULT_OPS,
  27. .fb_mmap = rockchip_fbdev_mmap,
  28. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  29. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  30. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  31. };
  32. static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
  33. struct drm_fb_helper_surface_size *sizes)
  34. {
  35. struct rockchip_drm_private *private = to_drm_private(helper);
  36. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  37. struct drm_device *dev = helper->dev;
  38. struct rockchip_gem_object *rk_obj;
  39. struct drm_framebuffer *fb;
  40. unsigned int bytes_per_pixel;
  41. unsigned long offset;
  42. struct fb_info *fbi;
  43. size_t size;
  44. int ret;
  45. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  46. mode_cmd.width = sizes->surface_width;
  47. mode_cmd.height = sizes->surface_height;
  48. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  49. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  50. sizes->surface_depth);
  51. size = mode_cmd.pitches[0] * mode_cmd.height;
  52. rk_obj = rockchip_gem_create_object(dev, size, true);
  53. if (IS_ERR(rk_obj))
  54. return -ENOMEM;
  55. private->fbdev_bo = &rk_obj->base;
  56. fbi = drm_fb_helper_alloc_fbi(helper);
  57. if (IS_ERR(fbi)) {
  58. DRM_DEV_ERROR(dev->dev, "Failed to create framebuffer info.\n");
  59. ret = PTR_ERR(fbi);
  60. goto out;
  61. }
  62. helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
  63. private->fbdev_bo);
  64. if (IS_ERR(helper->fb)) {
  65. DRM_DEV_ERROR(dev->dev,
  66. "Failed to allocate DRM framebuffer.\n");
  67. ret = PTR_ERR(helper->fb);
  68. goto out;
  69. }
  70. fbi->fbops = &rockchip_drm_fbdev_ops;
  71. fb = helper->fb;
  72. drm_fb_helper_fill_info(fbi, helper, sizes);
  73. offset = fbi->var.xoffset * bytes_per_pixel;
  74. offset += fbi->var.yoffset * fb->pitches[0];
  75. dev->mode_config.fb_base = 0;
  76. fbi->screen_base = rk_obj->kvaddr + offset;
  77. fbi->screen_size = rk_obj->base.size;
  78. fbi->fix.smem_len = rk_obj->base.size;
  79. DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
  80. fb->width, fb->height, fb->format->depth,
  81. rk_obj->kvaddr,
  82. offset, size);
  83. return 0;
  84. out:
  85. rockchip_gem_free_object(&rk_obj->base);
  86. return ret;
  87. }
  88. static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
  89. .fb_probe = rockchip_drm_fbdev_create,
  90. };
  91. int rockchip_drm_fbdev_init(struct drm_device *dev)
  92. {
  93. struct rockchip_drm_private *private = dev->dev_private;
  94. struct drm_fb_helper *helper;
  95. int ret;
  96. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  97. return -EINVAL;
  98. helper = &private->fbdev_helper;
  99. drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
  100. ret = drm_fb_helper_init(dev, helper);
  101. if (ret < 0) {
  102. DRM_DEV_ERROR(dev->dev,
  103. "Failed to initialize drm fb helper - %d.\n",
  104. ret);
  105. return ret;
  106. }
  107. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  108. if (ret < 0) {
  109. DRM_DEV_ERROR(dev->dev,
  110. "Failed to set initial hw config - %d.\n",
  111. ret);
  112. goto err_drm_fb_helper_fini;
  113. }
  114. return 0;
  115. err_drm_fb_helper_fini:
  116. drm_fb_helper_fini(helper);
  117. return ret;
  118. }
  119. void rockchip_drm_fbdev_fini(struct drm_device *dev)
  120. {
  121. struct rockchip_drm_private *private = dev->dev_private;
  122. struct drm_fb_helper *helper;
  123. helper = &private->fbdev_helper;
  124. drm_fb_helper_unregister_fbi(helper);
  125. if (helper->fb)
  126. drm_framebuffer_put(helper->fb);
  127. drm_fb_helper_fini(helper);
  128. }