1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* exynos_drm_fbdev.c 3 * 4 * Copyright (c) 2011 Samsung Electronics Co., Ltd. 5 * Authors: 6 * Inki Dae <inki.dae@samsung.com> 7 * Joonyoung Shim <jy0922.shim@samsung.com> 8 * Seung-Woo Kim <sw0312.kim@samsung.com> 9 */ 10 11 #include <linux/fb.h> 12 13 #include <drm/drm_crtc_helper.h> 14 #include <drm/drm_drv.h> 15 #include <drm/drm_fb_helper.h> 16 #include <drm/drm_framebuffer.h> 17 #include <drm/drm_gem_framebuffer_helper.h> 18 #include <drm/drm_prime.h> 19 #include <drm/exynos_drm.h> 20 21 #include "exynos_drm_drv.h" 22 #include "exynos_drm_fb.h" 23 #include "exynos_drm_fbdev.h" 24 25 #define MAX_CONNECTOR 4 26 27 static int exynos_drm_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 28 { 29 struct drm_fb_helper *helper = info->par; 30 struct drm_gem_object *obj = drm_gem_fb_get_obj(helper->fb, 0); 31 32 return drm_gem_prime_mmap(obj, vma); 33 } 34 35 static void exynos_drm_fb_destroy(struct fb_info *info) 36 { 37 struct drm_fb_helper *fb_helper = info->par; 38 struct drm_framebuffer *fb = fb_helper->fb; 39 40 drm_fb_helper_fini(fb_helper); 41 42 drm_framebuffer_remove(fb); 43 44 drm_client_release(&fb_helper->client); 45 } 46 47 static const struct fb_ops exynos_drm_fb_ops = { 48 .owner = THIS_MODULE, 49 __FB_DEFAULT_DMAMEM_OPS_RDWR, 50 DRM_FB_HELPER_DEFAULT_OPS, 51 __FB_DEFAULT_DMAMEM_OPS_DRAW, 52 .fb_mmap = exynos_drm_fb_mmap, 53 .fb_destroy = exynos_drm_fb_destroy, 54 }; 55 56 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper, 57 struct drm_fb_helper_surface_size *sizes, 58 struct exynos_drm_gem *exynos_gem) 59 { 60 struct fb_info *fbi; 61 struct drm_framebuffer *fb = helper->fb; 62 unsigned int size = fb->width * fb->height * fb->format->cpp[0]; 63 unsigned long offset; 64 65 fbi = drm_fb_helper_alloc_info(helper); 66 if (IS_ERR(fbi)) { 67 DRM_DEV_ERROR(to_dma_dev(helper->dev), 68 "failed to allocate fb info.\n"); 69 return PTR_ERR(fbi); 70 } 71 72 fbi->fbops = &exynos_drm_fb_ops; 73 74 drm_fb_helper_fill_info(fbi, helper, sizes); 75 76 offset = fbi->var.xoffset * fb->format->cpp[0]; 77 offset += fbi->var.yoffset * fb->pitches[0]; 78 79 fbi->flags |= FBINFO_VIRTFB; 80 fbi->screen_buffer = exynos_gem->kvaddr + offset; 81 fbi->screen_size = size; 82 fbi->fix.smem_len = size; 83 84 return 0; 85 } 86 87 static const struct drm_fb_helper_funcs exynos_drm_fbdev_helper_funcs = { 88 }; 89 90 int exynos_drm_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper, 91 struct drm_fb_helper_surface_size *sizes) 92 { 93 struct exynos_drm_gem *exynos_gem; 94 struct drm_device *dev = helper->dev; 95 struct drm_mode_fb_cmd2 mode_cmd = { 0 }; 96 unsigned long size; 97 int ret; 98 99 DRM_DEV_DEBUG_KMS(dev->dev, 100 "surface width(%d), height(%d) and bpp(%d\n", 101 sizes->surface_width, sizes->surface_height, 102 sizes->surface_bpp); 103 104 mode_cmd.width = sizes->surface_width; 105 mode_cmd.height = sizes->surface_height; 106 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3); 107 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 108 sizes->surface_depth); 109 110 size = mode_cmd.pitches[0] * mode_cmd.height; 111 112 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_WC, size, true); 113 if (IS_ERR(exynos_gem)) 114 return PTR_ERR(exynos_gem); 115 116 helper->fb = 117 exynos_drm_framebuffer_init(dev, 118 drm_get_format_info(dev, mode_cmd.pixel_format, 119 mode_cmd.modifier[0]), 120 &mode_cmd, &exynos_gem, 1); 121 if (IS_ERR(helper->fb)) { 122 DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n"); 123 ret = PTR_ERR(helper->fb); 124 goto err_destroy_gem; 125 } 126 helper->funcs = &exynos_drm_fbdev_helper_funcs; 127 128 ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem); 129 if (ret < 0) 130 goto err_destroy_framebuffer; 131 132 return 0; 133 134 err_destroy_framebuffer: 135 drm_framebuffer_cleanup(helper->fb); 136 helper->fb = NULL; 137 err_destroy_gem: 138 exynos_drm_gem_destroy(exynos_gem); 139 return ret; 140 } 141