exynos_drm_fbdev.c 12 KB

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