rockchip_drm_fbdev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define PREFERRED_BPP 32
  22. #define to_drm_private(x) \
  23. container_of(x, struct rockchip_drm_private, fbdev_helper)
  24. static int rockchip_fbdev_mmap(struct fb_info *info,
  25. struct vm_area_struct *vma)
  26. {
  27. struct drm_fb_helper *helper = info->par;
  28. struct rockchip_drm_private *private = to_drm_private(helper);
  29. return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
  30. }
  31. static struct fb_ops rockchip_drm_fbdev_ops = {
  32. .owner = THIS_MODULE,
  33. .fb_mmap = rockchip_fbdev_mmap,
  34. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  35. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  36. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  37. .fb_check_var = drm_fb_helper_check_var,
  38. .fb_set_par = drm_fb_helper_set_par,
  39. .fb_blank = drm_fb_helper_blank,
  40. .fb_pan_display = drm_fb_helper_pan_display,
  41. .fb_setcmap = drm_fb_helper_setcmap,
  42. };
  43. static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
  44. struct drm_fb_helper_surface_size *sizes)
  45. {
  46. struct rockchip_drm_private *private = to_drm_private(helper);
  47. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  48. struct drm_device *dev = helper->dev;
  49. struct rockchip_gem_object *rk_obj;
  50. struct drm_framebuffer *fb;
  51. unsigned int bytes_per_pixel;
  52. unsigned long offset;
  53. struct fb_info *fbi;
  54. size_t size;
  55. int ret;
  56. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  57. mode_cmd.width = sizes->surface_width;
  58. mode_cmd.height = sizes->surface_height;
  59. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  60. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  61. sizes->surface_depth);
  62. size = mode_cmd.pitches[0] * mode_cmd.height;
  63. rk_obj = rockchip_gem_create_object(dev, size, true);
  64. if (IS_ERR(rk_obj))
  65. return -ENOMEM;
  66. private->fbdev_bo = &rk_obj->base;
  67. fbi = drm_fb_helper_alloc_fbi(helper);
  68. if (IS_ERR(fbi)) {
  69. dev_err(dev->dev, "Failed to create framebuffer info.\n");
  70. ret = PTR_ERR(fbi);
  71. goto err_rockchip_gem_free_object;
  72. }
  73. helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
  74. private->fbdev_bo);
  75. if (IS_ERR(helper->fb)) {
  76. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  77. ret = PTR_ERR(helper->fb);
  78. goto err_release_fbi;
  79. }
  80. fbi->par = helper;
  81. fbi->flags = FBINFO_FLAG_DEFAULT;
  82. fbi->fbops = &rockchip_drm_fbdev_ops;
  83. fb = helper->fb;
  84. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  85. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  86. offset = fbi->var.xoffset * bytes_per_pixel;
  87. offset += fbi->var.yoffset * fb->pitches[0];
  88. dev->mode_config.fb_base = 0;
  89. fbi->screen_base = rk_obj->kvaddr + offset;
  90. fbi->screen_size = rk_obj->base.size;
  91. fbi->fix.smem_len = rk_obj->base.size;
  92. DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%d\n",
  93. fb->width, fb->height, fb->depth, rk_obj->kvaddr,
  94. offset, size);
  95. fbi->skip_vt_switch = true;
  96. return 0;
  97. err_release_fbi:
  98. drm_fb_helper_release_fbi(helper);
  99. err_rockchip_gem_free_object:
  100. rockchip_gem_free_object(&rk_obj->base);
  101. return ret;
  102. }
  103. static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
  104. .fb_probe = rockchip_drm_fbdev_create,
  105. };
  106. int rockchip_drm_fbdev_init(struct drm_device *dev)
  107. {
  108. struct rockchip_drm_private *private = dev->dev_private;
  109. struct drm_fb_helper *helper;
  110. unsigned int num_crtc;
  111. int ret;
  112. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  113. return -EINVAL;
  114. num_crtc = dev->mode_config.num_crtc;
  115. helper = &private->fbdev_helper;
  116. drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
  117. ret = drm_fb_helper_init(dev, helper, num_crtc, ROCKCHIP_MAX_CONNECTOR);
  118. if (ret < 0) {
  119. dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n",
  120. ret);
  121. return ret;
  122. }
  123. ret = drm_fb_helper_single_add_all_connectors(helper);
  124. if (ret < 0) {
  125. dev_err(dev->dev, "Failed to add connectors - %d.\n", ret);
  126. goto err_drm_fb_helper_fini;
  127. }
  128. /* disable all the possible outputs/crtcs before entering KMS mode */
  129. drm_helper_disable_unused_functions(dev);
  130. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  131. if (ret < 0) {
  132. dev_err(dev->dev, "Failed to set initial hw config - %d.\n",
  133. ret);
  134. goto err_drm_fb_helper_fini;
  135. }
  136. return 0;
  137. err_drm_fb_helper_fini:
  138. drm_fb_helper_fini(helper);
  139. return ret;
  140. }
  141. void rockchip_drm_fbdev_fini(struct drm_device *dev)
  142. {
  143. struct rockchip_drm_private *private = dev->dev_private;
  144. struct drm_fb_helper *helper;
  145. helper = &private->fbdev_helper;
  146. drm_fb_helper_unregister_fbi(helper);
  147. drm_fb_helper_release_fbi(helper);
  148. if (helper->fb)
  149. drm_framebuffer_unreference(helper->fb);
  150. drm_fb_helper_fini(helper);
  151. }