2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "drm_fb_helper.h"
24 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
27 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
30 struct drm_fb_helper base
;
31 struct drm_framebuffer
*fb
;
32 struct drm_gem_object
*bo
;
35 static struct fb_ops msm_fb_ops
= {
38 /* Note: to properly handle manual update displays, we wrap the
39 * basic fbdev ops which write to the framebuffer
41 .fb_read
= fb_sys_read
,
42 .fb_write
= fb_sys_write
,
43 .fb_fillrect
= sys_fillrect
,
44 .fb_copyarea
= sys_copyarea
,
45 .fb_imageblit
= sys_imageblit
,
47 .fb_check_var
= drm_fb_helper_check_var
,
48 .fb_set_par
= drm_fb_helper_set_par
,
49 .fb_pan_display
= drm_fb_helper_pan_display
,
50 .fb_blank
= drm_fb_helper_blank
,
51 .fb_setcmap
= drm_fb_helper_setcmap
,
54 static int msm_fbdev_create(struct drm_fb_helper
*helper
,
55 struct drm_fb_helper_surface_size
*sizes
)
57 struct msm_fbdev
*fbdev
= to_msm_fbdev(helper
);
58 struct drm_device
*dev
= helper
->dev
;
59 struct drm_framebuffer
*fb
= NULL
;
60 struct fb_info
*fbi
= NULL
;
61 struct drm_mode_fb_cmd2 mode_cmd
= {0};
65 sizes
->surface_bpp
= 32;
66 sizes
->surface_depth
= 24;
68 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes
->surface_width
,
69 sizes
->surface_height
, sizes
->surface_bpp
,
70 sizes
->fb_width
, sizes
->fb_height
);
72 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
73 sizes
->surface_depth
);
75 mode_cmd
.width
= sizes
->surface_width
;
76 mode_cmd
.height
= sizes
->surface_height
;
78 mode_cmd
.pitches
[0] = align_pitch(
79 mode_cmd
.width
, sizes
->surface_bpp
);
81 /* allocate backing bo */
82 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
83 DBG("allocating %d bytes for fb %d", size
, dev
->primary
->index
);
84 mutex_lock(&dev
->struct_mutex
);
85 fbdev
->bo
= msm_gem_new(dev
, size
, MSM_BO_SCANOUT
| MSM_BO_WC
);
86 mutex_unlock(&dev
->struct_mutex
);
87 if (IS_ERR(fbdev
->bo
)) {
88 ret
= PTR_ERR(fbdev
->bo
);
90 dev_err(dev
->dev
, "failed to allocate buffer object: %d\n", ret
);
94 fb
= msm_framebuffer_init(dev
, &mode_cmd
, &fbdev
->bo
);
96 dev_err(dev
->dev
, "failed to allocate fb\n");
97 /* note: if fb creation failed, we can't rely on fb destroy
100 drm_gem_object_unreference(fbdev
->bo
);
105 mutex_lock(&dev
->struct_mutex
);
107 /* TODO implement our own fb_mmap so we don't need this: */
108 msm_gem_get_iova_locked(fbdev
->bo
, 0, &paddr
);
110 fbi
= framebuffer_alloc(0, dev
->dev
);
112 dev_err(dev
->dev
, "failed to allocate fb info\n");
117 DBG("fbi=%p, dev=%p", fbi
, dev
);
124 fbi
->flags
= FBINFO_DEFAULT
;
125 fbi
->fbops
= &msm_fb_ops
;
127 strcpy(fbi
->fix
.id
, "msm");
129 ret
= fb_alloc_cmap(&fbi
->cmap
, 256, 0);
135 drm_fb_helper_fill_fix(fbi
, fb
->pitches
[0], fb
->depth
);
136 drm_fb_helper_fill_var(fbi
, helper
, sizes
->fb_width
, sizes
->fb_height
);
138 dev
->mode_config
.fb_base
= paddr
;
140 fbi
->screen_base
= msm_gem_vaddr_locked(fbdev
->bo
);
141 fbi
->screen_size
= fbdev
->bo
->size
;
142 fbi
->fix
.smem_start
= paddr
;
143 fbi
->fix
.smem_len
= fbdev
->bo
->size
;
145 DBG("par=%p, %dx%d", fbi
->par
, fbi
->var
.xres
, fbi
->var
.yres
);
146 DBG("allocated %dx%d fb", fbdev
->fb
->width
, fbdev
->fb
->height
);
148 mutex_unlock(&dev
->struct_mutex
);
153 mutex_unlock(&dev
->struct_mutex
);
158 framebuffer_release(fbi
);
160 drm_framebuffer_unregister_private(fb
);
161 drm_framebuffer_remove(fb
);
168 static void msm_crtc_fb_gamma_set(struct drm_crtc
*crtc
,
169 u16 red
, u16 green
, u16 blue
, int regno
)
171 DBG("fbdev: set gamma");
174 static void msm_crtc_fb_gamma_get(struct drm_crtc
*crtc
,
175 u16
*red
, u16
*green
, u16
*blue
, int regno
)
177 DBG("fbdev: get gamma");
180 static struct drm_fb_helper_funcs msm_fb_helper_funcs
= {
181 .gamma_set
= msm_crtc_fb_gamma_set
,
182 .gamma_get
= msm_crtc_fb_gamma_get
,
183 .fb_probe
= msm_fbdev_create
,
186 /* initialize fbdev helper */
187 struct drm_fb_helper
*msm_fbdev_init(struct drm_device
*dev
)
189 struct msm_drm_private
*priv
= dev
->dev_private
;
190 struct msm_fbdev
*fbdev
= NULL
;
191 struct drm_fb_helper
*helper
;
194 fbdev
= kzalloc(sizeof(*fbdev
), GFP_KERNEL
);
198 helper
= &fbdev
->base
;
200 helper
->funcs
= &msm_fb_helper_funcs
;
202 ret
= drm_fb_helper_init(dev
, helper
,
203 priv
->num_crtcs
, priv
->num_connectors
);
205 dev_err(dev
->dev
, "could not init fbdev: ret=%d\n", ret
);
209 drm_fb_helper_single_add_all_connectors(helper
);
211 /* disable all the possible outputs/crtcs before entering KMS mode */
212 drm_helper_disable_unused_functions(dev
);
214 drm_fb_helper_initial_config(helper
, 32);
216 priv
->fbdev
= helper
;
225 void msm_fbdev_free(struct drm_device
*dev
)
227 struct msm_drm_private
*priv
= dev
->dev_private
;
228 struct drm_fb_helper
*helper
= priv
->fbdev
;
229 struct msm_fbdev
*fbdev
;
236 /* only cleanup framebuffer if it is present */
238 unregister_framebuffer(fbi
);
239 framebuffer_release(fbi
);
242 drm_fb_helper_fini(helper
);
244 fbdev
= to_msm_fbdev(priv
->fbdev
);
246 /* this will free the backing object */
248 drm_framebuffer_unregister_private(fbdev
->fb
);
249 drm_framebuffer_remove(fbdev
->fb
);