rockchip_drm_fbdev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = cfb_fillrect,
  35. .fb_copyarea = cfb_copyarea,
  36. .fb_imageblit = 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 = framebuffer_alloc(0, dev->dev);
  68. if (!fbi) {
  69. dev_err(dev->dev, "Failed to allocate framebuffer info.\n");
  70. ret = -ENOMEM;
  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_framebuffer_release;
  79. }
  80. helper->fbdev = fbi;
  81. fbi->par = helper;
  82. fbi->flags = FBINFO_FLAG_DEFAULT;
  83. fbi->fbops = &rockchip_drm_fbdev_ops;
  84. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  85. if (ret) {
  86. dev_err(dev->dev, "Failed to allocate color map.\n");
  87. goto err_drm_framebuffer_unref;
  88. }
  89. fb = helper->fb;
  90. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  91. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  92. offset = fbi->var.xoffset * bytes_per_pixel;
  93. offset += fbi->var.yoffset * fb->pitches[0];
  94. dev->mode_config.fb_base = 0;
  95. fbi->screen_base = rk_obj->kvaddr + offset;
  96. fbi->screen_size = rk_obj->base.size;
  97. fbi->fix.smem_len = rk_obj->base.size;
  98. DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%d\n",
  99. fb->width, fb->height, fb->depth, rk_obj->kvaddr,
  100. offset, size);
  101. fbi->skip_vt_switch = true;
  102. return 0;
  103. err_drm_framebuffer_unref:
  104. drm_framebuffer_unreference(helper->fb);
  105. err_framebuffer_release:
  106. framebuffer_release(fbi);
  107. err_rockchip_gem_free_object:
  108. rockchip_gem_free_object(&rk_obj->base);
  109. return ret;
  110. }
  111. static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
  112. .fb_probe = rockchip_drm_fbdev_create,
  113. };
  114. int rockchip_drm_fbdev_init(struct drm_device *dev)
  115. {
  116. struct rockchip_drm_private *private = dev->dev_private;
  117. struct drm_fb_helper *helper;
  118. unsigned int num_crtc;
  119. int ret;
  120. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  121. return -EINVAL;
  122. num_crtc = dev->mode_config.num_crtc;
  123. helper = &private->fbdev_helper;
  124. drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
  125. ret = drm_fb_helper_init(dev, helper, num_crtc, ROCKCHIP_MAX_CONNECTOR);
  126. if (ret < 0) {
  127. dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n",
  128. ret);
  129. return ret;
  130. }
  131. ret = drm_fb_helper_single_add_all_connectors(helper);
  132. if (ret < 0) {
  133. dev_err(dev->dev, "Failed to add connectors - %d.\n", ret);
  134. goto err_drm_fb_helper_fini;
  135. }
  136. /* disable all the possible outputs/crtcs before entering KMS mode */
  137. drm_helper_disable_unused_functions(dev);
  138. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  139. if (ret < 0) {
  140. dev_err(dev->dev, "Failed to set initial hw config - %d.\n",
  141. ret);
  142. goto err_drm_fb_helper_fini;
  143. }
  144. return 0;
  145. err_drm_fb_helper_fini:
  146. drm_fb_helper_fini(helper);
  147. return ret;
  148. }
  149. void rockchip_drm_fbdev_fini(struct drm_device *dev)
  150. {
  151. struct rockchip_drm_private *private = dev->dev_private;
  152. struct drm_fb_helper *helper;
  153. helper = &private->fbdev_helper;
  154. if (helper->fbdev) {
  155. struct fb_info *info;
  156. int ret;
  157. info = helper->fbdev;
  158. ret = unregister_framebuffer(info);
  159. if (ret < 0)
  160. DRM_DEBUG_KMS("failed unregister_framebuffer() - %d\n",
  161. ret);
  162. if (info->cmap.len)
  163. fb_dealloc_cmap(&info->cmap);
  164. framebuffer_release(info);
  165. }
  166. if (helper->fb)
  167. drm_framebuffer_unreference(helper->fb);
  168. drm_fb_helper_fini(helper);
  169. }