exynos_drm_fbdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "drmP.h"
  29. #include "drm_crtc.h"
  30. #include "drm_fb_helper.h"
  31. #include "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 int exynos_drm_fbdev_set_par(struct fb_info *info)
  44. {
  45. struct fb_var_screeninfo *var = &info->var;
  46. switch (var->bits_per_pixel) {
  47. case 32:
  48. case 24:
  49. case 18:
  50. case 16:
  51. case 12:
  52. info->fix.visual = FB_VISUAL_TRUECOLOR;
  53. break;
  54. case 1:
  55. info->fix.visual = FB_VISUAL_MONO01;
  56. break;
  57. default:
  58. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  59. break;
  60. }
  61. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  62. return drm_fb_helper_set_par(info);
  63. }
  64. static struct fb_ops exynos_drm_fb_ops = {
  65. .owner = THIS_MODULE,
  66. .fb_fillrect = cfb_fillrect,
  67. .fb_copyarea = cfb_copyarea,
  68. .fb_imageblit = cfb_imageblit,
  69. .fb_check_var = drm_fb_helper_check_var,
  70. .fb_set_par = exynos_drm_fbdev_set_par,
  71. .fb_blank = drm_fb_helper_blank,
  72. .fb_pan_display = drm_fb_helper_pan_display,
  73. .fb_setcmap = drm_fb_helper_setcmap,
  74. };
  75. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  76. struct drm_framebuffer *fb)
  77. {
  78. struct fb_info *fbi = helper->fbdev;
  79. struct drm_device *dev = helper->dev;
  80. struct exynos_drm_gem_buf *buffer;
  81. unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
  82. unsigned long offset;
  83. DRM_DEBUG_KMS("%s\n", __FILE__);
  84. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  85. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  86. /* RGB formats use only one buffer */
  87. buffer = exynos_drm_fb_buffer(fb, 0);
  88. if (!buffer) {
  89. DRM_LOG_KMS("buffer is null.\n");
  90. return -EFAULT;
  91. }
  92. offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
  93. offset += fbi->var.yoffset * fb->pitches[0];
  94. dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
  95. fbi->screen_base = buffer->kvaddr + offset;
  96. fbi->fix.smem_start = (unsigned long)(buffer->dma_addr + offset);
  97. fbi->screen_size = size;
  98. fbi->fix.smem_len = size;
  99. return 0;
  100. }
  101. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  102. struct drm_fb_helper_surface_size *sizes)
  103. {
  104. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  105. struct exynos_drm_gem_obj *exynos_gem_obj;
  106. struct drm_device *dev = helper->dev;
  107. struct fb_info *fbi;
  108. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  109. struct platform_device *pdev = dev->platformdev;
  110. unsigned long size;
  111. int ret;
  112. DRM_DEBUG_KMS("%s\n", __FILE__);
  113. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  114. sizes->surface_width, sizes->surface_height,
  115. sizes->surface_bpp);
  116. mode_cmd.width = sizes->surface_width;
  117. mode_cmd.height = sizes->surface_height;
  118. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  119. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  120. sizes->surface_depth);
  121. mutex_lock(&dev->struct_mutex);
  122. fbi = framebuffer_alloc(0, &pdev->dev);
  123. if (!fbi) {
  124. DRM_ERROR("failed to allocate fb info.\n");
  125. ret = -ENOMEM;
  126. goto out;
  127. }
  128. size = mode_cmd.pitches[0] * mode_cmd.height;
  129. exynos_gem_obj = exynos_drm_gem_create(dev, size);
  130. if (IS_ERR(exynos_gem_obj)) {
  131. ret = PTR_ERR(exynos_gem_obj);
  132. goto out;
  133. }
  134. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  135. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  136. &exynos_gem_obj->base);
  137. if (IS_ERR_OR_NULL(helper->fb)) {
  138. DRM_ERROR("failed to create drm framebuffer.\n");
  139. ret = PTR_ERR(helper->fb);
  140. goto out;
  141. }
  142. helper->fbdev = fbi;
  143. fbi->par = helper;
  144. fbi->flags = FBINFO_FLAG_DEFAULT;
  145. fbi->fbops = &exynos_drm_fb_ops;
  146. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  147. if (ret) {
  148. DRM_ERROR("failed to allocate cmap.\n");
  149. goto out;
  150. }
  151. ret = exynos_drm_fbdev_update(helper, helper->fb);
  152. if (ret < 0) {
  153. fb_dealloc_cmap(&fbi->cmap);
  154. goto out;
  155. }
  156. /*
  157. * if failed, all resources allocated above would be released by
  158. * drm_mode_config_cleanup() when drm_load() had been called prior
  159. * to any specific driver such as fimd or hdmi driver.
  160. */
  161. out:
  162. mutex_unlock(&dev->struct_mutex);
  163. return ret;
  164. }
  165. static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  166. struct drm_fb_helper_surface_size *sizes)
  167. {
  168. int ret = 0;
  169. DRM_DEBUG_KMS("%s\n", __FILE__);
  170. /*
  171. * with !helper->fb, it means that this funcion is called first time
  172. * and after that, the helper->fb would be used as clone mode.
  173. */
  174. if (!helper->fb) {
  175. ret = exynos_drm_fbdev_create(helper, sizes);
  176. if (ret < 0) {
  177. DRM_ERROR("failed to create fbdev.\n");
  178. return ret;
  179. }
  180. /*
  181. * fb_helper expects a value more than 1 if succeed
  182. * because register_framebuffer() should be called.
  183. */
  184. ret = 1;
  185. }
  186. return ret;
  187. }
  188. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  189. .fb_probe = exynos_drm_fbdev_probe,
  190. };
  191. int exynos_drm_fbdev_init(struct drm_device *dev)
  192. {
  193. struct exynos_drm_fbdev *fbdev;
  194. struct exynos_drm_private *private = dev->dev_private;
  195. struct drm_fb_helper *helper;
  196. unsigned int num_crtc;
  197. int ret;
  198. DRM_DEBUG_KMS("%s\n", __FILE__);
  199. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  200. return 0;
  201. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  202. if (!fbdev) {
  203. DRM_ERROR("failed to allocate drm fbdev.\n");
  204. return -ENOMEM;
  205. }
  206. private->fb_helper = helper = &fbdev->drm_fb_helper;
  207. helper->funcs = &exynos_drm_fb_helper_funcs;
  208. num_crtc = dev->mode_config.num_crtc;
  209. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  210. if (ret < 0) {
  211. DRM_ERROR("failed to initialize drm fb helper.\n");
  212. goto err_init;
  213. }
  214. ret = drm_fb_helper_single_add_all_connectors(helper);
  215. if (ret < 0) {
  216. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  217. goto err_setup;
  218. }
  219. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  220. if (ret < 0) {
  221. DRM_ERROR("failed to set up hw configuration.\n");
  222. goto err_setup;
  223. }
  224. return 0;
  225. err_setup:
  226. drm_fb_helper_fini(helper);
  227. err_init:
  228. private->fb_helper = NULL;
  229. kfree(fbdev);
  230. return ret;
  231. }
  232. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  233. struct drm_fb_helper *fb_helper)
  234. {
  235. struct drm_framebuffer *fb;
  236. /* release drm framebuffer and real buffer */
  237. if (fb_helper->fb && fb_helper->fb->funcs) {
  238. fb = fb_helper->fb;
  239. if (fb && fb->funcs->destroy)
  240. fb->funcs->destroy(fb);
  241. }
  242. /* release linux framebuffer */
  243. if (fb_helper->fbdev) {
  244. struct fb_info *info;
  245. int ret;
  246. info = fb_helper->fbdev;
  247. ret = unregister_framebuffer(info);
  248. if (ret < 0)
  249. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  250. if (info->cmap.len)
  251. fb_dealloc_cmap(&info->cmap);
  252. framebuffer_release(info);
  253. }
  254. drm_fb_helper_fini(fb_helper);
  255. }
  256. void exynos_drm_fbdev_fini(struct drm_device *dev)
  257. {
  258. struct exynos_drm_private *private = dev->dev_private;
  259. struct exynos_drm_fbdev *fbdev;
  260. if (!private || !private->fb_helper)
  261. return;
  262. fbdev = to_exynos_fbdev(private->fb_helper);
  263. if (fbdev->exynos_gem_obj)
  264. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  265. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  266. kfree(fbdev);
  267. private->fb_helper = NULL;
  268. }
  269. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  270. {
  271. struct exynos_drm_private *private = dev->dev_private;
  272. if (!private || !private->fb_helper)
  273. return;
  274. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  275. }
  276. int exynos_drm_fbdev_reinit(struct drm_device *dev)
  277. {
  278. struct exynos_drm_private *private = dev->dev_private;
  279. struct drm_fb_helper *fb_helper;
  280. int ret;
  281. if (!private)
  282. return -EINVAL;
  283. /*
  284. * if all sub drivers were unloaded then num_connector is 0
  285. * so at this time, the framebuffers also should be destroyed.
  286. */
  287. if (!dev->mode_config.num_connector) {
  288. exynos_drm_fbdev_fini(dev);
  289. return 0;
  290. }
  291. fb_helper = private->fb_helper;
  292. if (fb_helper) {
  293. struct list_head temp_list;
  294. INIT_LIST_HEAD(&temp_list);
  295. /*
  296. * fb_helper is reintialized but kernel fb is reused
  297. * so kernel_fb_list need to be backuped and restored
  298. */
  299. if (!list_empty(&fb_helper->kernel_fb_list))
  300. list_replace_init(&fb_helper->kernel_fb_list,
  301. &temp_list);
  302. drm_fb_helper_fini(fb_helper);
  303. ret = drm_fb_helper_init(dev, fb_helper,
  304. dev->mode_config.num_crtc, MAX_CONNECTOR);
  305. if (ret < 0) {
  306. DRM_ERROR("failed to initialize drm fb helper\n");
  307. return ret;
  308. }
  309. if (!list_empty(&temp_list))
  310. list_replace(&temp_list, &fb_helper->kernel_fb_list);
  311. ret = drm_fb_helper_single_add_all_connectors(fb_helper);
  312. if (ret < 0) {
  313. DRM_ERROR("failed to add fb helper to connectors\n");
  314. goto err;
  315. }
  316. ret = drm_fb_helper_initial_config(fb_helper, PREFERRED_BPP);
  317. if (ret < 0) {
  318. DRM_ERROR("failed to set up hw configuration.\n");
  319. goto err;
  320. }
  321. } else {
  322. /*
  323. * if drm_load() failed whem drm load() was called prior
  324. * to specific drivers, fb_helper must be NULL and so
  325. * this fuction should be called again to re-initialize and
  326. * re-configure the fb helper. it means that this function
  327. * has been called by the specific drivers.
  328. */
  329. ret = exynos_drm_fbdev_init(dev);
  330. }
  331. return ret;
  332. err:
  333. /*
  334. * if drm_load() failed when drm load() was called prior
  335. * to specific drivers, the fb_helper must be NULL and so check it.
  336. */
  337. if (fb_helper)
  338. drm_fb_helper_fini(fb_helper);
  339. return ret;
  340. }
  341. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  342. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  343. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  344. MODULE_DESCRIPTION("Samsung SoC DRM FBDEV Driver");
  345. MODULE_LICENSE("GPL");