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