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 bool tegra_fb_is_tiled(struct drm_framebuffer *framebuffer)
51 struct tegra_fb *fb = to_tegra_fb(framebuffer);
53 if (fb->planes[0]->flags & TEGRA_BO_TILED)
59 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
61 struct tegra_fb *fb = to_tegra_fb(framebuffer);
64 for (i = 0; i < fb->num_planes; i++) {
65 struct tegra_bo *bo = fb->planes[i];
68 drm_gem_object_unreference_unlocked(&bo->gem);
71 drm_framebuffer_cleanup(framebuffer);
76 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
77 struct drm_file *file, unsigned int *handle)
79 struct tegra_fb *fb = to_tegra_fb(framebuffer);
81 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
84 static struct drm_framebuffer_funcs tegra_fb_funcs = {
85 .destroy = tegra_fb_destroy,
86 .create_handle = tegra_fb_create_handle,
89 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct tegra_bo **planes,
92 unsigned int num_planes)
98 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
100 return ERR_PTR(-ENOMEM);
102 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
104 return ERR_PTR(-ENOMEM);
106 fb->num_planes = num_planes;
108 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
110 for (i = 0; i < fb->num_planes; i++)
111 fb->planes[i] = planes[i];
113 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
115 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
125 static struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
126 struct drm_file *file,
127 struct drm_mode_fb_cmd2 *cmd)
129 unsigned int hsub, vsub, i;
130 struct tegra_bo *planes[4];
131 struct drm_gem_object *gem;
135 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
136 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
138 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
139 unsigned int width = cmd->width / (i ? hsub : 1);
140 unsigned int height = cmd->height / (i ? vsub : 1);
141 unsigned int size, bpp;
143 gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
149 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
151 size = (height - 1) * cmd->pitches[i] +
152 width * bpp + cmd->offsets[i];
154 if (gem->size < size) {
159 planes[i] = to_tegra_bo(gem);
162 fb = tegra_fb_alloc(drm, cmd, planes, i);
172 drm_gem_object_unreference_unlocked(&planes[i]->gem);
177 #ifdef CONFIG_DRM_TEGRA_FBDEV
178 static struct fb_ops tegra_fb_ops = {
179 .owner = THIS_MODULE,
180 .fb_fillrect = sys_fillrect,
181 .fb_copyarea = sys_copyarea,
182 .fb_imageblit = sys_imageblit,
183 .fb_check_var = drm_fb_helper_check_var,
184 .fb_set_par = drm_fb_helper_set_par,
185 .fb_blank = drm_fb_helper_blank,
186 .fb_pan_display = drm_fb_helper_pan_display,
187 .fb_setcmap = drm_fb_helper_setcmap,
190 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
191 struct drm_fb_helper_surface_size *sizes)
193 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
194 struct drm_device *drm = helper->dev;
195 struct drm_mode_fb_cmd2 cmd = { 0 };
196 unsigned int bytes_per_pixel;
197 struct drm_framebuffer *fb;
198 unsigned long offset;
199 struct fb_info *info;
204 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
206 cmd.width = sizes->surface_width;
207 cmd.height = sizes->surface_height;
208 cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
209 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
210 sizes->surface_depth);
212 size = cmd.pitches[0] * cmd.height;
214 bo = tegra_bo_create(drm, size, 0);
218 info = framebuffer_alloc(0, drm->dev);
220 dev_err(drm->dev, "failed to allocate framebuffer info\n");
221 tegra_bo_free_object(&bo->gem);
225 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
226 if (IS_ERR(fbdev->fb)) {
227 dev_err(drm->dev, "failed to allocate DRM framebuffer\n");
228 err = PTR_ERR(fbdev->fb);
232 fb = &fbdev->fb->base;
234 helper->fbdev = info;
237 info->flags = FBINFO_FLAG_DEFAULT;
238 info->fbops = &tegra_fb_ops;
240 err = fb_alloc_cmap(&info->cmap, 256, 0);
242 dev_err(drm->dev, "failed to allocate color map: %d\n", err);
246 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
247 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
249 offset = info->var.xoffset * bytes_per_pixel +
250 info->var.yoffset * fb->pitches[0];
252 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
253 info->screen_base = (void __iomem *)bo->vaddr + offset;
254 info->screen_size = size;
255 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
256 info->fix.smem_len = size;
261 drm_framebuffer_unregister_private(fb);
262 tegra_fb_destroy(fb);
264 framebuffer_release(info);
268 static struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
269 .fb_probe = tegra_fbdev_probe,
272 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm,
273 unsigned int preferred_bpp,
274 unsigned int num_crtc,
275 unsigned int max_connectors)
277 struct drm_fb_helper *helper;
278 struct tegra_fbdev *fbdev;
281 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
283 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
284 return ERR_PTR(-ENOMEM);
287 fbdev->base.funcs = &tegra_fb_helper_funcs;
288 helper = &fbdev->base;
290 err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
292 dev_err(drm->dev, "failed to initialize DRM FB helper\n");
296 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
298 dev_err(drm->dev, "failed to add connectors\n");
302 drm_helper_disable_unused_functions(drm);
304 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
306 dev_err(drm->dev, "failed to set initial configuration\n");
313 drm_fb_helper_fini(&fbdev->base);
319 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
321 struct fb_info *info = fbdev->base.fbdev;
326 err = unregister_framebuffer(info);
328 DRM_DEBUG_KMS("failed to unregister framebuffer\n");
331 fb_dealloc_cmap(&info->cmap);
333 framebuffer_release(info);
337 drm_framebuffer_unregister_private(&fbdev->fb->base);
338 tegra_fb_destroy(&fbdev->fb->base);
341 drm_fb_helper_fini(&fbdev->base);
345 void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
348 drm_modeset_lock_all(fbdev->base.dev);
349 drm_fb_helper_restore_fbdev_mode(&fbdev->base);
350 drm_modeset_unlock_all(fbdev->base.dev);
354 static void tegra_fb_output_poll_changed(struct drm_device *drm)
356 struct tegra_drm *tegra = drm->dev_private;
359 drm_fb_helper_hotplug_event(&tegra->fbdev->base);
363 static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
364 .fb_create = tegra_fb_create,
365 #ifdef CONFIG_DRM_TEGRA_FBDEV
366 .output_poll_changed = tegra_fb_output_poll_changed,
370 int tegra_drm_fb_init(struct drm_device *drm)
372 #ifdef CONFIG_DRM_TEGRA_FBDEV
373 struct tegra_drm *tegra = drm->dev_private;
376 drm->mode_config.min_width = 0;
377 drm->mode_config.min_height = 0;
379 drm->mode_config.max_width = 4096;
380 drm->mode_config.max_height = 4096;
382 drm->mode_config.funcs = &tegra_drm_mode_funcs;
384 #ifdef CONFIG_DRM_TEGRA_FBDEV
385 tegra->fbdev = tegra_fbdev_create(drm, 32, drm->mode_config.num_crtc,
386 drm->mode_config.num_connector);
387 if (IS_ERR(tegra->fbdev))
388 return PTR_ERR(tegra->fbdev);
394 void tegra_drm_fb_exit(struct drm_device *drm)
396 #ifdef CONFIG_DRM_TEGRA_FBDEV
397 struct tegra_drm *tegra = drm->dev_private;
399 tegra_fbdev_free(tegra->fbdev);