rockchip_drm_fbdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drm.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_fb_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include "rockchip_drm_drv.h"
  19. #include "rockchip_drm_gem.h"
  20. #include "rockchip_drm_fb.h"
  21. #include "rockchip_drm_fbdev.h"
  22. #define PREFERRED_BPP 32
  23. #define to_drm_private(x) \
  24. container_of(x, struct rockchip_drm_private, fbdev_helper)
  25. static int rockchip_fbdev_mmap(struct fb_info *info,
  26. struct vm_area_struct *vma)
  27. {
  28. struct drm_fb_helper *helper = info->par;
  29. struct rockchip_drm_private *private = to_drm_private(helper);
  30. return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
  31. }
  32. static struct fb_ops rockchip_drm_fbdev_ops = {
  33. .owner = THIS_MODULE,
  34. DRM_FB_HELPER_DEFAULT_OPS,
  35. .fb_mmap = rockchip_fbdev_mmap,
  36. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  37. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  38. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  39. };
  40. static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
  41. struct drm_fb_helper_surface_size *sizes)
  42. {
  43. struct rockchip_drm_private *private = to_drm_private(helper);
  44. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  45. struct drm_device *dev = helper->dev;
  46. struct rockchip_gem_object *rk_obj;
  47. struct drm_framebuffer *fb;
  48. unsigned int bytes_per_pixel;
  49. unsigned long offset;
  50. struct fb_info *fbi;
  51. size_t size;
  52. int ret;
  53. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  54. mode_cmd.width = sizes->surface_width;
  55. mode_cmd.height = sizes->surface_height;
  56. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  57. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  58. sizes->surface_depth);
  59. size = mode_cmd.pitches[0] * mode_cmd.height;
  60. rk_obj = rockchip_gem_create_object(dev, size, true);
  61. if (IS_ERR(rk_obj))
  62. return -ENOMEM;
  63. private->fbdev_bo = &rk_obj->base;
  64. fbi = drm_fb_helper_alloc_fbi(helper);
  65. if (IS_ERR(fbi)) {
  66. DRM_DEV_ERROR(dev->dev, "Failed to create framebuffer info.\n");
  67. ret = PTR_ERR(fbi);
  68. goto out;
  69. }
  70. helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
  71. private->fbdev_bo);
  72. if (IS_ERR(helper->fb)) {
  73. DRM_DEV_ERROR(dev->dev,
  74. "Failed to allocate DRM framebuffer.\n");
  75. ret = PTR_ERR(helper->fb);
  76. goto out;
  77. }
  78. fbi->par = helper;
  79. fbi->flags = FBINFO_FLAG_DEFAULT;
  80. fbi->fbops = &rockchip_drm_fbdev_ops;
  81. fb = helper->fb;
  82. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  83. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  84. offset = fbi->var.xoffset * bytes_per_pixel;
  85. offset += fbi->var.yoffset * fb->pitches[0];
  86. dev->mode_config.fb_base = 0;
  87. fbi->screen_base = rk_obj->kvaddr + offset;
  88. fbi->screen_size = rk_obj->base.size;
  89. fbi->fix.smem_len = rk_obj->base.size;
  90. DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
  91. fb->width, fb->height, fb->format->depth,
  92. rk_obj->kvaddr,
  93. offset, size);
  94. fbi->skip_vt_switch = true;
  95. return 0;
  96. out:
  97. rockchip_gem_free_object(&rk_obj->base);
  98. return ret;
  99. }
  100. static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
  101. .fb_probe = rockchip_drm_fbdev_create,
  102. };
  103. int rockchip_drm_fbdev_init(struct drm_device *dev)
  104. {
  105. struct rockchip_drm_private *private = dev->dev_private;
  106. struct drm_fb_helper *helper;
  107. int ret;
  108. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  109. return -EINVAL;
  110. helper = &private->fbdev_helper;
  111. drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
  112. ret = drm_fb_helper_init(dev, helper, ROCKCHIP_MAX_CONNECTOR);
  113. if (ret < 0) {
  114. DRM_DEV_ERROR(dev->dev,
  115. "Failed to initialize drm fb helper - %d.\n",
  116. ret);
  117. return ret;
  118. }
  119. ret = drm_fb_helper_single_add_all_connectors(helper);
  120. if (ret < 0) {
  121. DRM_DEV_ERROR(dev->dev,
  122. "Failed to add connectors - %d.\n", ret);
  123. goto err_drm_fb_helper_fini;
  124. }
  125. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  126. if (ret < 0) {
  127. DRM_DEV_ERROR(dev->dev,
  128. "Failed to set initial hw config - %d.\n",
  129. ret);
  130. goto err_drm_fb_helper_fini;
  131. }
  132. return 0;
  133. err_drm_fb_helper_fini:
  134. drm_fb_helper_fini(helper);
  135. return ret;
  136. }
  137. void rockchip_drm_fbdev_fini(struct drm_device *dev)
  138. {
  139. struct rockchip_drm_private *private = dev->dev_private;
  140. struct drm_fb_helper *helper;
  141. helper = &private->fbdev_helper;
  142. drm_fb_helper_unregister_fbi(helper);
  143. if (helper->fb)
  144. drm_framebuffer_put(helper->fb);
  145. drm_fb_helper_fini(helper);
  146. }