exynos_drm_fbdev.c 11 KB

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