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