exynos_drm_fbdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* exynos_drm_fbdev.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/drm_crtc.h>
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "exynos_drm_drv.h"
  33. #include "exynos_drm_fb.h"
  34. #include "exynos_drm_gem.h"
  35. #define MAX_CONNECTOR 4
  36. #define PREFERRED_BPP 32
  37. #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
  38. drm_fb_helper)
  39. struct exynos_drm_fbdev {
  40. struct drm_fb_helper drm_fb_helper;
  41. struct exynos_drm_gem_obj *exynos_gem_obj;
  42. };
  43. static struct fb_ops exynos_drm_fb_ops = {
  44. .owner = THIS_MODULE,
  45. .fb_fillrect = cfb_fillrect,
  46. .fb_copyarea = cfb_copyarea,
  47. .fb_imageblit = cfb_imageblit,
  48. .fb_check_var = drm_fb_helper_check_var,
  49. .fb_set_par = drm_fb_helper_set_par,
  50. .fb_blank = drm_fb_helper_blank,
  51. .fb_pan_display = drm_fb_helper_pan_display,
  52. .fb_setcmap = drm_fb_helper_setcmap,
  53. };
  54. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  55. struct drm_framebuffer *fb)
  56. {
  57. struct fb_info *fbi = helper->fbdev;
  58. struct drm_device *dev = helper->dev;
  59. struct exynos_drm_gem_buf *buffer;
  60. unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
  61. unsigned long offset;
  62. DRM_DEBUG_KMS("%s\n", __FILE__);
  63. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  64. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  65. /* RGB formats use only one buffer */
  66. buffer = exynos_drm_fb_buffer(fb, 0);
  67. if (!buffer) {
  68. DRM_LOG_KMS("buffer is null.\n");
  69. return -EFAULT;
  70. }
  71. /* buffer count to framebuffer always is 1 at booting time. */
  72. exynos_drm_fb_set_buf_cnt(fb, 1);
  73. offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
  74. offset += fbi->var.yoffset * fb->pitches[0];
  75. dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
  76. fbi->screen_base = buffer->kvaddr + offset;
  77. fbi->fix.smem_start = (unsigned long)(page_to_phys(buffer->pages[0]) +
  78. offset);
  79. fbi->screen_size = size;
  80. fbi->fix.smem_len = size;
  81. return 0;
  82. }
  83. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  84. struct drm_fb_helper_surface_size *sizes)
  85. {
  86. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  87. struct exynos_drm_gem_obj *exynos_gem_obj;
  88. struct drm_device *dev = helper->dev;
  89. struct fb_info *fbi;
  90. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  91. struct platform_device *pdev = dev->platformdev;
  92. unsigned long size;
  93. int ret;
  94. DRM_DEBUG_KMS("%s\n", __FILE__);
  95. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  96. sizes->surface_width, sizes->surface_height,
  97. sizes->surface_bpp);
  98. mode_cmd.width = sizes->surface_width;
  99. mode_cmd.height = sizes->surface_height;
  100. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  101. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  102. sizes->surface_depth);
  103. mutex_lock(&dev->struct_mutex);
  104. fbi = framebuffer_alloc(0, &pdev->dev);
  105. if (!fbi) {
  106. DRM_ERROR("failed to allocate fb info.\n");
  107. ret = -ENOMEM;
  108. goto out;
  109. }
  110. size = mode_cmd.pitches[0] * mode_cmd.height;
  111. /* 0 means to allocate physically continuous memory */
  112. exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
  113. if (IS_ERR(exynos_gem_obj)) {
  114. ret = PTR_ERR(exynos_gem_obj);
  115. goto out;
  116. }
  117. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  118. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  119. &exynos_gem_obj->base);
  120. if (IS_ERR_OR_NULL(helper->fb)) {
  121. DRM_ERROR("failed to create drm framebuffer.\n");
  122. ret = PTR_ERR(helper->fb);
  123. goto out;
  124. }
  125. helper->fbdev = fbi;
  126. fbi->par = helper;
  127. fbi->flags = FBINFO_FLAG_DEFAULT;
  128. fbi->fbops = &exynos_drm_fb_ops;
  129. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  130. if (ret) {
  131. DRM_ERROR("failed to allocate cmap.\n");
  132. goto out;
  133. }
  134. ret = exynos_drm_fbdev_update(helper, helper->fb);
  135. if (ret < 0) {
  136. fb_dealloc_cmap(&fbi->cmap);
  137. goto out;
  138. }
  139. /*
  140. * if failed, all resources allocated above would be released by
  141. * drm_mode_config_cleanup() when drm_load() had been called prior
  142. * to any specific driver such as fimd or hdmi driver.
  143. */
  144. out:
  145. mutex_unlock(&dev->struct_mutex);
  146. return ret;
  147. }
  148. static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  149. struct drm_fb_helper_surface_size *sizes)
  150. {
  151. int ret = 0;
  152. DRM_DEBUG_KMS("%s\n", __FILE__);
  153. /*
  154. * with !helper->fb, it means that this funcion is called first time
  155. * and after that, the helper->fb would be used as clone mode.
  156. */
  157. if (!helper->fb) {
  158. ret = exynos_drm_fbdev_create(helper, sizes);
  159. if (ret < 0) {
  160. DRM_ERROR("failed to create fbdev.\n");
  161. return ret;
  162. }
  163. /*
  164. * fb_helper expects a value more than 1 if succeed
  165. * because register_framebuffer() should be called.
  166. */
  167. ret = 1;
  168. }
  169. return ret;
  170. }
  171. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  172. .fb_probe = exynos_drm_fbdev_probe,
  173. };
  174. int exynos_drm_fbdev_init(struct drm_device *dev)
  175. {
  176. struct exynos_drm_fbdev *fbdev;
  177. struct exynos_drm_private *private = dev->dev_private;
  178. struct drm_fb_helper *helper;
  179. unsigned int num_crtc;
  180. int ret;
  181. DRM_DEBUG_KMS("%s\n", __FILE__);
  182. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  183. return 0;
  184. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  185. if (!fbdev) {
  186. DRM_ERROR("failed to allocate drm fbdev.\n");
  187. return -ENOMEM;
  188. }
  189. private->fb_helper = helper = &fbdev->drm_fb_helper;
  190. helper->funcs = &exynos_drm_fb_helper_funcs;
  191. num_crtc = dev->mode_config.num_crtc;
  192. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  193. if (ret < 0) {
  194. DRM_ERROR("failed to initialize drm fb helper.\n");
  195. goto err_init;
  196. }
  197. ret = drm_fb_helper_single_add_all_connectors(helper);
  198. if (ret < 0) {
  199. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  200. goto err_setup;
  201. }
  202. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  203. if (ret < 0) {
  204. DRM_ERROR("failed to set up hw configuration.\n");
  205. goto err_setup;
  206. }
  207. return 0;
  208. err_setup:
  209. drm_fb_helper_fini(helper);
  210. err_init:
  211. private->fb_helper = NULL;
  212. kfree(fbdev);
  213. return ret;
  214. }
  215. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  216. struct drm_fb_helper *fb_helper)
  217. {
  218. struct drm_framebuffer *fb;
  219. /* release drm framebuffer and real buffer */
  220. if (fb_helper->fb && fb_helper->fb->funcs) {
  221. fb = fb_helper->fb;
  222. if (fb)
  223. drm_framebuffer_remove(fb);
  224. }
  225. /* release linux framebuffer */
  226. if (fb_helper->fbdev) {
  227. struct fb_info *info;
  228. int ret;
  229. info = fb_helper->fbdev;
  230. ret = unregister_framebuffer(info);
  231. if (ret < 0)
  232. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  233. if (info->cmap.len)
  234. fb_dealloc_cmap(&info->cmap);
  235. framebuffer_release(info);
  236. }
  237. drm_fb_helper_fini(fb_helper);
  238. }
  239. void exynos_drm_fbdev_fini(struct drm_device *dev)
  240. {
  241. struct exynos_drm_private *private = dev->dev_private;
  242. struct exynos_drm_fbdev *fbdev;
  243. if (!private || !private->fb_helper)
  244. return;
  245. fbdev = to_exynos_fbdev(private->fb_helper);
  246. if (fbdev->exynos_gem_obj)
  247. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  248. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  249. kfree(fbdev);
  250. private->fb_helper = NULL;
  251. }
  252. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  253. {
  254. struct exynos_drm_private *private = dev->dev_private;
  255. if (!private || !private->fb_helper)
  256. return;
  257. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  258. }