exynos_drm_fbdev.c 12 KB

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