exynos_drm_fbdev.c 11 KB

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