HID: sensor-hub: Add quirk for Lenovo Yoga 900 with ITE Chips
[cascardo/linux.git] / drivers / gpu / drm / msm / msm_fbdev.c
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
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.
8  *
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
12  * more details.
13  *
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/>.
16  */
17
18 #include "msm_drv.h"
19
20 #include "drm_crtc.h"
21 #include "drm_fb_helper.h"
22 #include "msm_gem.h"
23
24 extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
25                                         struct vm_area_struct *vma);
26 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
27
28 /*
29  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
30  */
31
32 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
33
34 struct msm_fbdev {
35         struct drm_fb_helper base;
36         struct drm_framebuffer *fb;
37         struct drm_gem_object *bo;
38 };
39
40 static struct fb_ops msm_fb_ops = {
41         .owner = THIS_MODULE,
42
43         /* Note: to properly handle manual update displays, we wrap the
44          * basic fbdev ops which write to the framebuffer
45          */
46         .fb_read = drm_fb_helper_sys_read,
47         .fb_write = drm_fb_helper_sys_write,
48         .fb_fillrect = drm_fb_helper_sys_fillrect,
49         .fb_copyarea = drm_fb_helper_sys_copyarea,
50         .fb_imageblit = drm_fb_helper_sys_imageblit,
51         .fb_mmap = msm_fbdev_mmap,
52
53         .fb_check_var = drm_fb_helper_check_var,
54         .fb_set_par = drm_fb_helper_set_par,
55         .fb_pan_display = drm_fb_helper_pan_display,
56         .fb_blank = drm_fb_helper_blank,
57         .fb_setcmap = drm_fb_helper_setcmap,
58 };
59
60 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
61 {
62         struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
63         struct msm_fbdev *fbdev = to_msm_fbdev(helper);
64         struct drm_gem_object *drm_obj = fbdev->bo;
65         struct drm_device *dev = helper->dev;
66         int ret = 0;
67
68         if (drm_device_is_unplugged(dev))
69                 return -ENODEV;
70
71         mutex_lock(&dev->struct_mutex);
72
73         ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
74
75         mutex_unlock(&dev->struct_mutex);
76
77         if (ret) {
78                 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
79                 return ret;
80         }
81
82         return msm_gem_mmap_obj(drm_obj, vma);
83 }
84
85 static int msm_fbdev_create(struct drm_fb_helper *helper,
86                 struct drm_fb_helper_surface_size *sizes)
87 {
88         struct msm_fbdev *fbdev = to_msm_fbdev(helper);
89         struct drm_device *dev = helper->dev;
90         struct drm_framebuffer *fb = NULL;
91         struct fb_info *fbi = NULL;
92         struct drm_mode_fb_cmd2 mode_cmd = {0};
93         uint32_t paddr;
94         int ret, size;
95
96         DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
97                         sizes->surface_height, sizes->surface_bpp,
98                         sizes->fb_width, sizes->fb_height);
99
100         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
101                         sizes->surface_depth);
102
103         mode_cmd.width = sizes->surface_width;
104         mode_cmd.height = sizes->surface_height;
105
106         mode_cmd.pitches[0] = align_pitch(
107                         mode_cmd.width, sizes->surface_bpp);
108
109         /* allocate backing bo */
110         size = mode_cmd.pitches[0] * mode_cmd.height;
111         DBG("allocating %d bytes for fb %d", size, dev->primary->index);
112         mutex_lock(&dev->struct_mutex);
113         fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT |
114                         MSM_BO_WC | MSM_BO_STOLEN);
115         mutex_unlock(&dev->struct_mutex);
116         if (IS_ERR(fbdev->bo)) {
117                 ret = PTR_ERR(fbdev->bo);
118                 fbdev->bo = NULL;
119                 dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
120                 goto fail;
121         }
122
123         fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
124         if (IS_ERR(fb)) {
125                 dev_err(dev->dev, "failed to allocate fb\n");
126                 /* note: if fb creation failed, we can't rely on fb destroy
127                  * to unref the bo:
128                  */
129                 drm_gem_object_unreference(fbdev->bo);
130                 ret = PTR_ERR(fb);
131                 goto fail;
132         }
133
134         mutex_lock(&dev->struct_mutex);
135
136         /*
137          * NOTE: if we can be guaranteed to be able to map buffer
138          * in panic (ie. lock-safe, etc) we could avoid pinning the
139          * buffer now:
140          */
141         ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
142         if (ret) {
143                 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
144                 goto fail_unlock;
145         }
146
147         fbi = drm_fb_helper_alloc_fbi(helper);
148         if (IS_ERR(fbi)) {
149                 dev_err(dev->dev, "failed to allocate fb info\n");
150                 ret = PTR_ERR(fbi);
151                 goto fail_unlock;
152         }
153
154         DBG("fbi=%p, dev=%p", fbi, dev);
155
156         fbdev->fb = fb;
157         helper->fb = fb;
158
159         fbi->par = helper;
160         fbi->flags = FBINFO_DEFAULT;
161         fbi->fbops = &msm_fb_ops;
162
163         strcpy(fbi->fix.id, "msm");
164
165         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
166         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
167
168         dev->mode_config.fb_base = paddr;
169
170         fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
171         fbi->screen_size = fbdev->bo->size;
172         fbi->fix.smem_start = paddr;
173         fbi->fix.smem_len = fbdev->bo->size;
174
175         DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
176         DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
177
178         mutex_unlock(&dev->struct_mutex);
179
180         return 0;
181
182 fail_unlock:
183         mutex_unlock(&dev->struct_mutex);
184 fail:
185
186         if (ret) {
187                 if (fb) {
188                         drm_framebuffer_unregister_private(fb);
189                         drm_framebuffer_remove(fb);
190                 }
191         }
192
193         return ret;
194 }
195
196 static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
197                 u16 red, u16 green, u16 blue, int regno)
198 {
199         DBG("fbdev: set gamma");
200 }
201
202 static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
203                 u16 *red, u16 *green, u16 *blue, int regno)
204 {
205         DBG("fbdev: get gamma");
206 }
207
208 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
209         .gamma_set = msm_crtc_fb_gamma_set,
210         .gamma_get = msm_crtc_fb_gamma_get,
211         .fb_probe = msm_fbdev_create,
212 };
213
214 /* initialize fbdev helper */
215 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
216 {
217         struct msm_drm_private *priv = dev->dev_private;
218         struct msm_fbdev *fbdev = NULL;
219         struct drm_fb_helper *helper;
220         int ret;
221
222         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
223         if (!fbdev)
224                 goto fail;
225
226         helper = &fbdev->base;
227
228         drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
229
230         ret = drm_fb_helper_init(dev, helper,
231                         priv->num_crtcs, priv->num_connectors);
232         if (ret) {
233                 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
234                 goto fail;
235         }
236
237         ret = drm_fb_helper_single_add_all_connectors(helper);
238         if (ret)
239                 goto fini;
240
241         ret = drm_fb_helper_initial_config(helper, 32);
242         if (ret)
243                 goto fini;
244
245         priv->fbdev = helper;
246
247         return helper;
248
249 fini:
250         drm_fb_helper_fini(helper);
251 fail:
252         kfree(fbdev);
253         return NULL;
254 }
255
256 void msm_fbdev_free(struct drm_device *dev)
257 {
258         struct msm_drm_private *priv = dev->dev_private;
259         struct drm_fb_helper *helper = priv->fbdev;
260         struct msm_fbdev *fbdev;
261
262         DBG();
263
264         drm_fb_helper_unregister_fbi(helper);
265         drm_fb_helper_release_fbi(helper);
266
267         drm_fb_helper_fini(helper);
268
269         fbdev = to_msm_fbdev(priv->fbdev);
270
271         /* this will free the backing object */
272         if (fbdev->fb) {
273                 drm_framebuffer_unregister_private(fbdev->fb);
274                 drm_framebuffer_remove(fbdev->fb);
275         }
276
277         kfree(fbdev);
278
279         priv->fbdev = NULL;
280 }