2 * Copyright (C) 2012-2013 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
5 * Based on the KMS/FB CMA helpers
6 * Copyright (C) 2012 Analog Device Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
16 static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
18 return container_of(fb, struct tegra_fb, base);
21 #ifdef CONFIG_DRM_TEGRA_FBDEV
22 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
24 return container_of(helper, struct tegra_fbdev, base);
28 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
31 struct tegra_fb *fb = to_tegra_fb(framebuffer);
33 if (index >= drm_format_num_planes(framebuffer->pixel_format))
36 return fb->planes[index];
39 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
41 struct tegra_fb *fb = to_tegra_fb(framebuffer);
43 if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
49 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
50 struct tegra_bo_tiling *tiling)
52 struct tegra_fb *fb = to_tegra_fb(framebuffer);
54 /* TODO: handle YUV formats? */
55 *tiling = fb->planes[0]->tiling;
60 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
62 struct tegra_fb *fb = to_tegra_fb(framebuffer);
65 for (i = 0; i < fb->num_planes; i++) {
66 struct tegra_bo *bo = fb->planes[i];
69 if (bo->pages && bo->vaddr)
72 drm_gem_object_unreference_unlocked(&bo->gem);
76 drm_framebuffer_cleanup(framebuffer);
81 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
82 struct drm_file *file, unsigned int *handle)
84 struct tegra_fb *fb = to_tegra_fb(framebuffer);
86 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
89 static struct drm_framebuffer_funcs tegra_fb_funcs = {
90 .destroy = tegra_fb_destroy,
91 .create_handle = tegra_fb_create_handle,
94 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct tegra_bo **planes,
97 unsigned int num_planes)
103 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
105 return ERR_PTR(-ENOMEM);
107 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
110 return ERR_PTR(-ENOMEM);
113 fb->num_planes = num_planes;
115 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
117 for (i = 0; i < fb->num_planes; i++)
118 fb->planes[i] = planes[i];
120 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
122 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
132 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
133 struct drm_file *file,
134 struct drm_mode_fb_cmd2 *cmd)
136 unsigned int hsub, vsub, i;
137 struct tegra_bo *planes[4];
138 struct drm_gem_object *gem;
142 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
143 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
145 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
146 unsigned int width = cmd->width / (i ? hsub : 1);
147 unsigned int height = cmd->height / (i ? vsub : 1);
148 unsigned int size, bpp;
150 gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
156 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
158 size = (height - 1) * cmd->pitches[i] +
159 width * bpp + cmd->offsets[i];
161 if (gem->size < size) {
166 planes[i] = to_tegra_bo(gem);
169 fb = tegra_fb_alloc(drm, cmd, planes, i);
179 drm_gem_object_unreference_unlocked(&planes[i]->gem);
184 #ifdef CONFIG_DRM_TEGRA_FBDEV
185 static struct fb_ops tegra_fb_ops = {
186 .owner = THIS_MODULE,
187 .fb_fillrect = sys_fillrect,
188 .fb_copyarea = sys_copyarea,
189 .fb_imageblit = sys_imageblit,
190 .fb_check_var = drm_fb_helper_check_var,
191 .fb_set_par = drm_fb_helper_set_par,
192 .fb_blank = drm_fb_helper_blank,
193 .fb_pan_display = drm_fb_helper_pan_display,
194 .fb_setcmap = drm_fb_helper_setcmap,
197 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
198 struct drm_fb_helper_surface_size *sizes)
200 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
201 struct tegra_drm *tegra = helper->dev->dev_private;
202 struct drm_device *drm = helper->dev;
203 struct drm_mode_fb_cmd2 cmd = { 0 };
204 unsigned int bytes_per_pixel;
205 struct drm_framebuffer *fb;
206 unsigned long offset;
207 struct fb_info *info;
212 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
214 cmd.width = sizes->surface_width;
215 cmd.height = sizes->surface_height;
216 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
218 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
219 sizes->surface_depth);
221 size = cmd.pitches[0] * cmd.height;
223 bo = tegra_bo_create(drm, size, 0);
227 info = framebuffer_alloc(0, drm->dev);
229 dev_err(drm->dev, "failed to allocate framebuffer info\n");
230 drm_gem_object_unreference_unlocked(&bo->gem);
234 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
235 if (IS_ERR(fbdev->fb)) {
236 err = PTR_ERR(fbdev->fb);
237 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
239 drm_gem_object_unreference_unlocked(&bo->gem);
243 fb = &fbdev->fb->base;
245 helper->fbdev = info;
248 info->flags = FBINFO_FLAG_DEFAULT;
249 info->fbops = &tegra_fb_ops;
251 err = fb_alloc_cmap(&info->cmap, 256, 0);
253 dev_err(drm->dev, "failed to allocate color map: %d\n", err);
257 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
258 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
260 offset = info->var.xoffset * bytes_per_pixel +
261 info->var.yoffset * fb->pitches[0];
264 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
265 pgprot_writecombine(PAGE_KERNEL));
267 dev_err(drm->dev, "failed to vmap() framebuffer\n");
273 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
274 info->screen_base = (void __iomem *)bo->vaddr + offset;
275 info->screen_size = size;
276 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
277 info->fix.smem_len = size;
282 drm_framebuffer_unregister_private(fb);
283 tegra_fb_destroy(fb);
285 framebuffer_release(info);
289 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
290 .fb_probe = tegra_fbdev_probe,
293 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
295 struct tegra_fbdev *fbdev;
297 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
299 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
300 return ERR_PTR(-ENOMEM);
303 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
308 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
313 static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
314 unsigned int preferred_bpp,
315 unsigned int num_crtc,
316 unsigned int max_connectors)
318 struct drm_device *drm = fbdev->base.dev;
321 err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
323 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
328 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
330 dev_err(drm->dev, "failed to add connectors: %d\n", err);
334 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
336 dev_err(drm->dev, "failed to set initial configuration: %d\n",
344 drm_fb_helper_fini(&fbdev->base);
348 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
350 struct fb_info *info = fbdev->base.fbdev;
355 err = unregister_framebuffer(info);
357 DRM_DEBUG_KMS("failed to unregister framebuffer\n");
360 fb_dealloc_cmap(&info->cmap);
362 framebuffer_release(info);
366 drm_framebuffer_unregister_private(&fbdev->fb->base);
367 drm_framebuffer_remove(&fbdev->fb->base);
370 drm_fb_helper_fini(&fbdev->base);
371 tegra_fbdev_free(fbdev);
374 void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
377 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
380 void tegra_fb_output_poll_changed(struct drm_device *drm)
382 struct tegra_drm *tegra = drm->dev_private;
385 drm_fb_helper_hotplug_event(&tegra->fbdev->base);
389 int tegra_drm_fb_prepare(struct drm_device *drm)
391 #ifdef CONFIG_DRM_TEGRA_FBDEV
392 struct tegra_drm *tegra = drm->dev_private;
394 tegra->fbdev = tegra_fbdev_create(drm);
395 if (IS_ERR(tegra->fbdev))
396 return PTR_ERR(tegra->fbdev);
402 void tegra_drm_fb_free(struct drm_device *drm)
404 #ifdef CONFIG_DRM_TEGRA_FBDEV
405 struct tegra_drm *tegra = drm->dev_private;
407 tegra_fbdev_free(tegra->fbdev);
411 int tegra_drm_fb_init(struct drm_device *drm)
413 #ifdef CONFIG_DRM_TEGRA_FBDEV
414 struct tegra_drm *tegra = drm->dev_private;
417 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
418 drm->mode_config.num_connector);
426 void tegra_drm_fb_exit(struct drm_device *drm)
428 #ifdef CONFIG_DRM_TEGRA_FBDEV
429 struct tegra_drm *tegra = drm->dev_private;
431 tegra_fbdev_exit(tegra->fbdev);