exynos_drm_fbdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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)(buffer->dma_addr + offset);
  78. fbi->screen_size = size;
  79. fbi->fix.smem_len = size;
  80. return 0;
  81. }
  82. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  83. struct drm_fb_helper_surface_size *sizes)
  84. {
  85. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  86. struct exynos_drm_gem_obj *exynos_gem_obj;
  87. struct drm_device *dev = helper->dev;
  88. struct fb_info *fbi;
  89. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  90. struct platform_device *pdev = dev->platformdev;
  91. unsigned long size;
  92. int ret;
  93. DRM_DEBUG_KMS("%s\n", __FILE__);
  94. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  95. sizes->surface_width, sizes->surface_height,
  96. sizes->surface_bpp);
  97. mode_cmd.width = sizes->surface_width;
  98. mode_cmd.height = sizes->surface_height;
  99. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  100. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  101. sizes->surface_depth);
  102. mutex_lock(&dev->struct_mutex);
  103. fbi = framebuffer_alloc(0, &pdev->dev);
  104. if (!fbi) {
  105. DRM_ERROR("failed to allocate fb info.\n");
  106. ret = -ENOMEM;
  107. goto out;
  108. }
  109. size = mode_cmd.pitches[0] * mode_cmd.height;
  110. /* 0 means to allocate physically continuous memory */
  111. exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
  112. if (IS_ERR(exynos_gem_obj)) {
  113. ret = PTR_ERR(exynos_gem_obj);
  114. goto out;
  115. }
  116. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  117. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  118. &exynos_gem_obj->base);
  119. if (IS_ERR_OR_NULL(helper->fb)) {
  120. DRM_ERROR("failed to create drm framebuffer.\n");
  121. ret = PTR_ERR(helper->fb);
  122. goto out;
  123. }
  124. helper->fbdev = fbi;
  125. fbi->par = helper;
  126. fbi->flags = FBINFO_FLAG_DEFAULT;
  127. fbi->fbops = &exynos_drm_fb_ops;
  128. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  129. if (ret) {
  130. DRM_ERROR("failed to allocate cmap.\n");
  131. goto out;
  132. }
  133. ret = exynos_drm_fbdev_update(helper, helper->fb);
  134. if (ret < 0) {
  135. fb_dealloc_cmap(&fbi->cmap);
  136. goto out;
  137. }
  138. /*
  139. * if failed, all resources allocated above would be released by
  140. * drm_mode_config_cleanup() when drm_load() had been called prior
  141. * to any specific driver such as fimd or hdmi driver.
  142. */
  143. out:
  144. mutex_unlock(&dev->struct_mutex);
  145. return ret;
  146. }
  147. static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  148. struct drm_fb_helper_surface_size *sizes)
  149. {
  150. int ret = 0;
  151. DRM_DEBUG_KMS("%s\n", __FILE__);
  152. /*
  153. * with !helper->fb, it means that this funcion is called first time
  154. * and after that, the helper->fb would be used as clone mode.
  155. */
  156. if (!helper->fb) {
  157. ret = exynos_drm_fbdev_create(helper, sizes);
  158. if (ret < 0) {
  159. DRM_ERROR("failed to create fbdev.\n");
  160. return ret;
  161. }
  162. /*
  163. * fb_helper expects a value more than 1 if succeed
  164. * because register_framebuffer() should be called.
  165. */
  166. ret = 1;
  167. }
  168. return ret;
  169. }
  170. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  171. .fb_probe = exynos_drm_fbdev_probe,
  172. };
  173. int exynos_drm_fbdev_init(struct drm_device *dev)
  174. {
  175. struct exynos_drm_fbdev *fbdev;
  176. struct exynos_drm_private *private = dev->dev_private;
  177. struct drm_fb_helper *helper;
  178. unsigned int num_crtc;
  179. int ret;
  180. DRM_DEBUG_KMS("%s\n", __FILE__);
  181. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  182. return 0;
  183. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  184. if (!fbdev) {
  185. DRM_ERROR("failed to allocate drm fbdev.\n");
  186. return -ENOMEM;
  187. }
  188. private->fb_helper = helper = &fbdev->drm_fb_helper;
  189. helper->funcs = &exynos_drm_fb_helper_funcs;
  190. num_crtc = dev->mode_config.num_crtc;
  191. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  192. if (ret < 0) {
  193. DRM_ERROR("failed to initialize drm fb helper.\n");
  194. goto err_init;
  195. }
  196. ret = drm_fb_helper_single_add_all_connectors(helper);
  197. if (ret < 0) {
  198. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  199. goto err_setup;
  200. }
  201. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  202. if (ret < 0) {
  203. DRM_ERROR("failed to set up hw configuration.\n");
  204. goto err_setup;
  205. }
  206. return 0;
  207. err_setup:
  208. drm_fb_helper_fini(helper);
  209. err_init:
  210. private->fb_helper = NULL;
  211. kfree(fbdev);
  212. return ret;
  213. }
  214. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  215. struct drm_fb_helper *fb_helper)
  216. {
  217. struct drm_framebuffer *fb;
  218. /* release drm framebuffer and real buffer */
  219. if (fb_helper->fb && fb_helper->fb->funcs) {
  220. fb = fb_helper->fb;
  221. if (fb)
  222. drm_framebuffer_remove(fb);
  223. }
  224. /* release linux framebuffer */
  225. if (fb_helper->fbdev) {
  226. struct fb_info *info;
  227. int ret;
  228. info = fb_helper->fbdev;
  229. ret = unregister_framebuffer(info);
  230. if (ret < 0)
  231. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  232. if (info->cmap.len)
  233. fb_dealloc_cmap(&info->cmap);
  234. framebuffer_release(info);
  235. }
  236. drm_fb_helper_fini(fb_helper);
  237. }
  238. void exynos_drm_fbdev_fini(struct drm_device *dev)
  239. {
  240. struct exynos_drm_private *private = dev->dev_private;
  241. struct exynos_drm_fbdev *fbdev;
  242. if (!private || !private->fb_helper)
  243. return;
  244. fbdev = to_exynos_fbdev(private->fb_helper);
  245. if (fbdev->exynos_gem_obj)
  246. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  247. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  248. kfree(fbdev);
  249. private->fb_helper = NULL;
  250. }
  251. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  252. {
  253. struct exynos_drm_private *private = dev->dev_private;
  254. if (!private || !private->fb_helper)
  255. return;
  256. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  257. }