]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/ast/ast_fb.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[karo-tx-linux.git] / drivers / gpu / drm / ast / ast_fb.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18  * USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  * The above copyright notice and this permission notice (including the
21  * next paragraph) shall be included in all copies or substantial portions
22  * of the Software.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/mm.h>
33 #include <linux/tty.h>
34 #include <linux/sysrq.h>
35 #include <linux/delay.h>
36 #include <linux/fb.h>
37 #include <linux/init.h>
38
39
40 #include <drm/drmP.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_crtc_helper.h>
44 #include "ast_drv.h"
45
46 static void ast_dirty_update(struct ast_fbdev *afbdev,
47                              int x, int y, int width, int height)
48 {
49         int i;
50         struct drm_gem_object *obj;
51         struct ast_bo *bo;
52         int src_offset, dst_offset;
53         int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8;
54         int ret;
55         bool unmap = false;
56
57         obj = afbdev->afb.obj;
58         bo = gem_to_ast_bo(obj);
59
60         ret = ast_bo_reserve(bo, true);
61         if (ret) {
62                 DRM_ERROR("failed to reserve fb bo\n");
63                 return;
64         }
65
66         if (!bo->kmap.virtual) {
67                 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
68                 if (ret) {
69                         DRM_ERROR("failed to kmap fb updates\n");
70                         ast_bo_unreserve(bo);
71                         return;
72                 }
73                 unmap = true;
74         }
75         for (i = y; i < y + height; i++) {
76                 /* assume equal stride for now */
77                 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
78                 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
79
80         }
81         if (unmap)
82                 ttm_bo_kunmap(&bo->kmap);
83
84         ast_bo_unreserve(bo);
85 }
86
87 static void ast_fillrect(struct fb_info *info,
88                          const struct fb_fillrect *rect)
89 {
90         struct ast_fbdev *afbdev = info->par;
91         sys_fillrect(info, rect);
92         ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
93                          rect->height);
94 }
95
96 static void ast_copyarea(struct fb_info *info,
97                          const struct fb_copyarea *area)
98 {
99         struct ast_fbdev *afbdev = info->par;
100         sys_copyarea(info, area);
101         ast_dirty_update(afbdev, area->dx, area->dy, area->width,
102                          area->height);
103 }
104
105 static void ast_imageblit(struct fb_info *info,
106                           const struct fb_image *image)
107 {
108         struct ast_fbdev *afbdev = info->par;
109         sys_imageblit(info, image);
110         ast_dirty_update(afbdev, image->dx, image->dy, image->width,
111                          image->height);
112 }
113
114 static struct fb_ops astfb_ops = {
115         .owner = THIS_MODULE,
116         .fb_check_var = drm_fb_helper_check_var,
117         .fb_set_par = drm_fb_helper_set_par,
118         .fb_fillrect = ast_fillrect,
119         .fb_copyarea = ast_copyarea,
120         .fb_imageblit = ast_imageblit,
121         .fb_pan_display = drm_fb_helper_pan_display,
122         .fb_blank = drm_fb_helper_blank,
123         .fb_setcmap = drm_fb_helper_setcmap,
124         .fb_debug_enter = drm_fb_helper_debug_enter,
125         .fb_debug_leave = drm_fb_helper_debug_leave,
126 };
127
128 static int astfb_create_object(struct ast_fbdev *afbdev,
129                                struct drm_mode_fb_cmd2 *mode_cmd,
130                                struct drm_gem_object **gobj_p)
131 {
132         struct drm_device *dev = afbdev->helper.dev;
133         u32 bpp, depth;
134         u32 size;
135         struct drm_gem_object *gobj;
136
137         int ret = 0;
138         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
139
140         size = mode_cmd->pitches[0] * mode_cmd->height;
141         ret = ast_gem_create(dev, size, true, &gobj);
142         if (ret)
143                 return ret;
144
145         *gobj_p = gobj;
146         return ret;
147 }
148
149 static int astfb_create(struct drm_fb_helper *helper,
150                         struct drm_fb_helper_surface_size *sizes)
151 {
152         struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
153         struct drm_device *dev = afbdev->helper.dev;
154         struct drm_mode_fb_cmd2 mode_cmd;
155         struct drm_framebuffer *fb;
156         struct fb_info *info;
157         int size, ret;
158         struct device *device = &dev->pdev->dev;
159         void *sysram;
160         struct drm_gem_object *gobj = NULL;
161         struct ast_bo *bo = NULL;
162         mode_cmd.width = sizes->surface_width;
163         mode_cmd.height = sizes->surface_height;
164         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
165
166         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
167                                                           sizes->surface_depth);
168
169         size = mode_cmd.pitches[0] * mode_cmd.height;
170
171         ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
172         if (ret) {
173                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
174                 return ret;
175         }
176         bo = gem_to_ast_bo(gobj);
177
178         sysram = vmalloc(size);
179         if (!sysram)
180                 return -ENOMEM;
181
182         info = framebuffer_alloc(0, device);
183         if (!info) {
184                 ret = -ENOMEM;
185                 goto out;
186         }
187         info->par = afbdev;
188
189         ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
190         if (ret)
191                 goto out;
192
193         afbdev->sysram = sysram;
194         afbdev->size = size;
195
196         fb = &afbdev->afb.base;
197         afbdev->helper.fb = fb;
198         afbdev->helper.fbdev = info;
199
200         strcpy(info->fix.id, "astdrmfb");
201
202         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
203         info->fbops = &astfb_ops;
204
205         ret = fb_alloc_cmap(&info->cmap, 256, 0);
206         if (ret) {
207                 ret = -ENOMEM;
208                 goto out;
209         }
210
211         info->apertures = alloc_apertures(1);
212         if (!info->apertures) {
213                 ret = -ENOMEM;
214                 goto out;
215         }
216         info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
217         info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
218
219         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
220         drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
221
222         info->screen_base = sysram;
223         info->screen_size = size;
224
225         info->pixmap.flags = FB_PIXMAP_SYSTEM;
226
227         DRM_DEBUG_KMS("allocated %dx%d\n",
228                       fb->width, fb->height);
229
230         return 0;
231 out:
232         return ret;
233 }
234
235 static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
236                                u16 blue, int regno)
237 {
238         struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
239         ast_crtc->lut_r[regno] = red >> 8;
240         ast_crtc->lut_g[regno] = green >> 8;
241         ast_crtc->lut_b[regno] = blue >> 8;
242 }
243
244 static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
245                                u16 *blue, int regno)
246 {
247         struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
248         *red = ast_crtc->lut_r[regno] << 8;
249         *green = ast_crtc->lut_g[regno] << 8;
250         *blue = ast_crtc->lut_b[regno] << 8;
251 }
252
253 static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
254         .gamma_set = ast_fb_gamma_set,
255         .gamma_get = ast_fb_gamma_get,
256         .fb_probe = astfb_create,
257 };
258
259 static void ast_fbdev_destroy(struct drm_device *dev,
260                               struct ast_fbdev *afbdev)
261 {
262         struct fb_info *info;
263         struct ast_framebuffer *afb = &afbdev->afb;
264         if (afbdev->helper.fbdev) {
265                 info = afbdev->helper.fbdev;
266                 unregister_framebuffer(info);
267                 if (info->cmap.len)
268                         fb_dealloc_cmap(&info->cmap);
269                 framebuffer_release(info);
270         }
271
272         if (afb->obj) {
273                 drm_gem_object_unreference_unlocked(afb->obj);
274                 afb->obj = NULL;
275         }
276         drm_fb_helper_fini(&afbdev->helper);
277
278         vfree(afbdev->sysram);
279         drm_framebuffer_unregister_private(&afb->base);
280         drm_framebuffer_cleanup(&afb->base);
281 }
282
283 int ast_fbdev_init(struct drm_device *dev)
284 {
285         struct ast_private *ast = dev->dev_private;
286         struct ast_fbdev *afbdev;
287         int ret;
288
289         afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
290         if (!afbdev)
291                 return -ENOMEM;
292
293         ast->fbdev = afbdev;
294         afbdev->helper.funcs = &ast_fb_helper_funcs;
295         ret = drm_fb_helper_init(dev, &afbdev->helper,
296                                  1, 1);
297         if (ret) {
298                 kfree(afbdev);
299                 return ret;
300         }
301
302         drm_fb_helper_single_add_all_connectors(&afbdev->helper);
303
304         /* disable all the possible outputs/crtcs before entering KMS mode */
305         drm_helper_disable_unused_functions(dev);
306
307         drm_fb_helper_initial_config(&afbdev->helper, 32);
308         return 0;
309 }
310
311 void ast_fbdev_fini(struct drm_device *dev)
312 {
313         struct ast_private *ast = dev->dev_private;
314
315         if (!ast->fbdev)
316                 return;
317
318         ast_fbdev_destroy(dev, ast->fbdev);
319         kfree(ast->fbdev);
320         ast->fbdev = NULL;
321 }
322
323 void ast_fbdev_set_suspend(struct drm_device *dev, int state)
324 {
325         struct ast_private *ast = dev->dev_private;
326
327         if (!ast->fbdev)
328                 return;
329
330         fb_set_suspend(ast->fbdev->helper.fbdev, state);
331 }