exynos_drm_fbdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 bool
  166. exynos_drm_fbdev_is_samefb(struct drm_framebuffer *fb,
  167. struct drm_fb_helper_surface_size *sizes)
  168. {
  169. if (fb->width != sizes->surface_width)
  170. return false;
  171. if (fb->height != sizes->surface_height)
  172. return false;
  173. if (fb->bits_per_pixel != sizes->surface_bpp)
  174. return false;
  175. if (fb->depth != sizes->surface_depth)
  176. return false;
  177. return true;
  178. }
  179. static int exynos_drm_fbdev_recreate(struct drm_fb_helper *helper,
  180. struct drm_fb_helper_surface_size *sizes)
  181. {
  182. struct drm_device *dev = helper->dev;
  183. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  184. struct exynos_drm_gem_obj *exynos_gem_obj;
  185. struct drm_framebuffer *fb = helper->fb;
  186. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  187. unsigned long size;
  188. DRM_DEBUG_KMS("%s\n", __FILE__);
  189. if (exynos_drm_fbdev_is_samefb(fb, sizes))
  190. return 0;
  191. mode_cmd.width = sizes->surface_width;
  192. mode_cmd.height = sizes->surface_height;
  193. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  194. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  195. sizes->surface_depth);
  196. if (exynos_fbdev->exynos_gem_obj)
  197. exynos_drm_gem_destroy(exynos_fbdev->exynos_gem_obj);
  198. if (fb->funcs->destroy)
  199. fb->funcs->destroy(fb);
  200. size = mode_cmd.pitches[0] * mode_cmd.height;
  201. exynos_gem_obj = exynos_drm_gem_create(dev, size);
  202. if (IS_ERR(exynos_gem_obj))
  203. return PTR_ERR(exynos_gem_obj);
  204. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  205. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  206. &exynos_gem_obj->base);
  207. if (IS_ERR_OR_NULL(helper->fb)) {
  208. DRM_ERROR("failed to create drm framebuffer.\n");
  209. return PTR_ERR(helper->fb);
  210. }
  211. return exynos_drm_fbdev_update(helper, helper->fb);
  212. }
  213. static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  214. struct drm_fb_helper_surface_size *sizes)
  215. {
  216. int ret = 0;
  217. DRM_DEBUG_KMS("%s\n", __FILE__);
  218. if (!helper->fb) {
  219. ret = exynos_drm_fbdev_create(helper, sizes);
  220. if (ret < 0) {
  221. DRM_ERROR("failed to create fbdev.\n");
  222. return ret;
  223. }
  224. /*
  225. * fb_helper expects a value more than 1 if succeed
  226. * because register_framebuffer() should be called.
  227. */
  228. ret = 1;
  229. } else {
  230. ret = exynos_drm_fbdev_recreate(helper, sizes);
  231. if (ret < 0) {
  232. DRM_ERROR("failed to reconfigure fbdev\n");
  233. return ret;
  234. }
  235. }
  236. return ret;
  237. }
  238. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  239. .fb_probe = exynos_drm_fbdev_probe,
  240. };
  241. int exynos_drm_fbdev_init(struct drm_device *dev)
  242. {
  243. struct exynos_drm_fbdev *fbdev;
  244. struct exynos_drm_private *private = dev->dev_private;
  245. struct drm_fb_helper *helper;
  246. unsigned int num_crtc;
  247. int ret;
  248. DRM_DEBUG_KMS("%s\n", __FILE__);
  249. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  250. return 0;
  251. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  252. if (!fbdev) {
  253. DRM_ERROR("failed to allocate drm fbdev.\n");
  254. return -ENOMEM;
  255. }
  256. private->fb_helper = helper = &fbdev->drm_fb_helper;
  257. helper->funcs = &exynos_drm_fb_helper_funcs;
  258. num_crtc = dev->mode_config.num_crtc;
  259. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  260. if (ret < 0) {
  261. DRM_ERROR("failed to initialize drm fb helper.\n");
  262. goto err_init;
  263. }
  264. ret = drm_fb_helper_single_add_all_connectors(helper);
  265. if (ret < 0) {
  266. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  267. goto err_setup;
  268. }
  269. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  270. if (ret < 0) {
  271. DRM_ERROR("failed to set up hw configuration.\n");
  272. goto err_setup;
  273. }
  274. return 0;
  275. err_setup:
  276. drm_fb_helper_fini(helper);
  277. err_init:
  278. private->fb_helper = NULL;
  279. kfree(fbdev);
  280. return ret;
  281. }
  282. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  283. struct drm_fb_helper *fb_helper)
  284. {
  285. struct drm_framebuffer *fb;
  286. /* release drm framebuffer and real buffer */
  287. if (fb_helper->fb && fb_helper->fb->funcs) {
  288. fb = fb_helper->fb;
  289. if (fb && fb->funcs->destroy)
  290. fb->funcs->destroy(fb);
  291. }
  292. /* release linux framebuffer */
  293. if (fb_helper->fbdev) {
  294. struct fb_info *info;
  295. int ret;
  296. info = fb_helper->fbdev;
  297. ret = unregister_framebuffer(info);
  298. if (ret < 0)
  299. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  300. if (info->cmap.len)
  301. fb_dealloc_cmap(&info->cmap);
  302. framebuffer_release(info);
  303. }
  304. drm_fb_helper_fini(fb_helper);
  305. }
  306. void exynos_drm_fbdev_fini(struct drm_device *dev)
  307. {
  308. struct exynos_drm_private *private = dev->dev_private;
  309. struct exynos_drm_fbdev *fbdev;
  310. if (!private || !private->fb_helper)
  311. return;
  312. fbdev = to_exynos_fbdev(private->fb_helper);
  313. if (fbdev->exynos_gem_obj)
  314. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  315. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  316. kfree(fbdev);
  317. private->fb_helper = NULL;
  318. }
  319. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  320. {
  321. struct exynos_drm_private *private = dev->dev_private;
  322. if (!private || !private->fb_helper)
  323. return;
  324. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  325. }
  326. int exynos_drm_fbdev_reinit(struct drm_device *dev)
  327. {
  328. struct exynos_drm_private *private = dev->dev_private;
  329. struct drm_fb_helper *fb_helper;
  330. int ret;
  331. if (!private)
  332. return -EINVAL;
  333. /*
  334. * if all sub drivers were unloaded then num_connector is 0
  335. * so at this time, the framebuffers also should be destroyed.
  336. */
  337. if (!dev->mode_config.num_connector) {
  338. exynos_drm_fbdev_fini(dev);
  339. return 0;
  340. }
  341. fb_helper = private->fb_helper;
  342. if (fb_helper) {
  343. struct list_head temp_list;
  344. INIT_LIST_HEAD(&temp_list);
  345. /*
  346. * fb_helper is reintialized but kernel fb is reused
  347. * so kernel_fb_list need to be backuped and restored
  348. */
  349. if (!list_empty(&fb_helper->kernel_fb_list))
  350. list_replace_init(&fb_helper->kernel_fb_list,
  351. &temp_list);
  352. drm_fb_helper_fini(fb_helper);
  353. ret = drm_fb_helper_init(dev, fb_helper,
  354. dev->mode_config.num_crtc, MAX_CONNECTOR);
  355. if (ret < 0) {
  356. DRM_ERROR("failed to initialize drm fb helper\n");
  357. return ret;
  358. }
  359. if (!list_empty(&temp_list))
  360. list_replace(&temp_list, &fb_helper->kernel_fb_list);
  361. ret = drm_fb_helper_single_add_all_connectors(fb_helper);
  362. if (ret < 0) {
  363. DRM_ERROR("failed to add fb helper to connectors\n");
  364. goto err;
  365. }
  366. ret = drm_fb_helper_initial_config(fb_helper, PREFERRED_BPP);
  367. if (ret < 0) {
  368. DRM_ERROR("failed to set up hw configuration.\n");
  369. goto err;
  370. }
  371. } else {
  372. /*
  373. * if drm_load() failed whem drm load() was called prior
  374. * to specific drivers, fb_helper must be NULL and so
  375. * this fuction should be called again to re-initialize and
  376. * re-configure the fb helper. it means that this function
  377. * has been called by the specific drivers.
  378. */
  379. ret = exynos_drm_fbdev_init(dev);
  380. }
  381. return ret;
  382. err:
  383. /*
  384. * if drm_load() failed when drm load() was called prior
  385. * to specific drivers, the fb_helper must be NULL and so check it.
  386. */
  387. if (fb_helper)
  388. drm_fb_helper_fini(fb_helper);
  389. return ret;
  390. }
  391. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  392. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  393. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  394. MODULE_DESCRIPTION("Samsung SoC DRM FBDEV Driver");
  395. MODULE_LICENSE("GPL");