1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012-2013 Avionic Design GmbH
4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
6 * Based on the KMS/FB DMA helpers
7 * Copyright (C) 2012 Analog Devices Inc.
10 #include <linux/console.h>
12 #include <linux/vmalloc.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_modeset_helper.h>
25 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
27 struct drm_fb_helper *helper = info->par;
31 bo = tegra_fb_get_plane(helper->fb, 0);
33 err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
37 return __tegra_gem_mmap(&bo->gem, vma);
40 static void tegra_fbdev_fb_destroy(struct fb_info *info)
42 struct drm_fb_helper *helper = info->par;
43 struct drm_framebuffer *fb = helper->fb;
44 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
46 drm_fb_helper_fini(helper);
48 /* Undo the special mapping we made in fbdev probe. */
53 drm_framebuffer_remove(fb);
55 drm_client_release(&helper->client);
56 drm_fb_helper_unprepare(helper);
60 static const struct fb_ops tegra_fb_ops = {
62 __FB_DEFAULT_DMAMEM_OPS_RDWR,
63 DRM_FB_HELPER_DEFAULT_OPS,
64 __FB_DEFAULT_DMAMEM_OPS_DRAW,
65 .fb_mmap = tegra_fb_mmap,
66 .fb_destroy = tegra_fbdev_fb_destroy,
69 static const struct drm_fb_helper_funcs tegra_fbdev_helper_funcs = {
72 int tegra_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
73 struct drm_fb_helper_surface_size *sizes)
75 struct tegra_drm *tegra = helper->dev->dev_private;
76 struct drm_device *drm = helper->dev;
77 struct drm_mode_fb_cmd2 cmd = { 0 };
78 unsigned int bytes_per_pixel;
79 struct drm_framebuffer *fb;
86 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
88 cmd.width = sizes->surface_width;
89 cmd.height = sizes->surface_height;
90 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
93 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
94 sizes->surface_depth);
96 size = cmd.pitches[0] * cmd.height;
98 bo = tegra_bo_create(drm, size, 0);
102 info = drm_fb_helper_alloc_info(helper);
104 dev_err(drm->dev, "failed to allocate framebuffer info\n");
105 drm_gem_object_put(&bo->gem);
106 return PTR_ERR(info);
109 fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
112 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
114 drm_gem_object_put(&bo->gem);
118 helper->funcs = &tegra_fbdev_helper_funcs;
122 info->fbops = &tegra_fb_ops;
124 drm_fb_helper_fill_info(info, helper, sizes);
126 offset = info->var.xoffset * bytes_per_pixel +
127 info->var.yoffset * fb->pitches[0];
130 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
131 pgprot_writecombine(PAGE_KERNEL));
133 dev_err(drm->dev, "failed to vmap() framebuffer\n");
139 info->flags |= FBINFO_VIRTFB;
140 info->screen_buffer = bo->vaddr + offset;
141 info->screen_size = size;
142 info->fix.smem_start = (unsigned long)(bo->iova + offset);
143 info->fix.smem_len = size;
148 drm_framebuffer_remove(fb);