]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/rockchip/rockchip_drm_fbdev.c
drm/rockchip: Use drm_fb_helper_fill_info
[linux.git] / drivers / gpu / drm / rockchip / rockchip_drm_fbdev.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_probe_helper.h>
19
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_gem.h"
22 #include "rockchip_drm_fb.h"
23 #include "rockchip_drm_fbdev.h"
24
25 #define PREFERRED_BPP           32
26 #define to_drm_private(x) \
27                 container_of(x, struct rockchip_drm_private, fbdev_helper)
28
29 static int rockchip_fbdev_mmap(struct fb_info *info,
30                                struct vm_area_struct *vma)
31 {
32         struct drm_fb_helper *helper = info->par;
33         struct rockchip_drm_private *private = to_drm_private(helper);
34
35         return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
36 }
37
38 static struct fb_ops rockchip_drm_fbdev_ops = {
39         .owner          = THIS_MODULE,
40         DRM_FB_HELPER_DEFAULT_OPS,
41         .fb_mmap        = rockchip_fbdev_mmap,
42         .fb_fillrect    = drm_fb_helper_cfb_fillrect,
43         .fb_copyarea    = drm_fb_helper_cfb_copyarea,
44         .fb_imageblit   = drm_fb_helper_cfb_imageblit,
45 };
46
47 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
48                                      struct drm_fb_helper_surface_size *sizes)
49 {
50         struct rockchip_drm_private *private = to_drm_private(helper);
51         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
52         struct drm_device *dev = helper->dev;
53         struct rockchip_gem_object *rk_obj;
54         struct drm_framebuffer *fb;
55         unsigned int bytes_per_pixel;
56         unsigned long offset;
57         struct fb_info *fbi;
58         size_t size;
59         int ret;
60
61         bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
62
63         mode_cmd.width = sizes->surface_width;
64         mode_cmd.height = sizes->surface_height;
65         mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
66         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
67                 sizes->surface_depth);
68
69         size = mode_cmd.pitches[0] * mode_cmd.height;
70
71         rk_obj = rockchip_gem_create_object(dev, size, true);
72         if (IS_ERR(rk_obj))
73                 return -ENOMEM;
74
75         private->fbdev_bo = &rk_obj->base;
76
77         fbi = drm_fb_helper_alloc_fbi(helper);
78         if (IS_ERR(fbi)) {
79                 DRM_DEV_ERROR(dev->dev, "Failed to create framebuffer info.\n");
80                 ret = PTR_ERR(fbi);
81                 goto out;
82         }
83
84         helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
85                                                    private->fbdev_bo);
86         if (IS_ERR(helper->fb)) {
87                 DRM_DEV_ERROR(dev->dev,
88                               "Failed to allocate DRM framebuffer.\n");
89                 ret = PTR_ERR(helper->fb);
90                 goto out;
91         }
92
93         fbi->fbops = &rockchip_drm_fbdev_ops;
94
95         fb = helper->fb;
96         drm_fb_helper_fill_info(fbi, helper, sizes);
97
98         offset = fbi->var.xoffset * bytes_per_pixel;
99         offset += fbi->var.yoffset * fb->pitches[0];
100
101         dev->mode_config.fb_base = 0;
102         fbi->screen_base = rk_obj->kvaddr + offset;
103         fbi->screen_size = rk_obj->base.size;
104         fbi->fix.smem_len = rk_obj->base.size;
105
106         DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
107                       fb->width, fb->height, fb->format->depth,
108                       rk_obj->kvaddr,
109                       offset, size);
110
111         return 0;
112
113 out:
114         rockchip_gem_free_object(&rk_obj->base);
115         return ret;
116 }
117
118 static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
119         .fb_probe = rockchip_drm_fbdev_create,
120 };
121
122 int rockchip_drm_fbdev_init(struct drm_device *dev)
123 {
124         struct rockchip_drm_private *private = dev->dev_private;
125         struct drm_fb_helper *helper;
126         int ret;
127
128         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
129                 return -EINVAL;
130
131         helper = &private->fbdev_helper;
132
133         drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
134
135         ret = drm_fb_helper_init(dev, helper, ROCKCHIP_MAX_CONNECTOR);
136         if (ret < 0) {
137                 DRM_DEV_ERROR(dev->dev,
138                               "Failed to initialize drm fb helper - %d.\n",
139                               ret);
140                 return ret;
141         }
142
143         ret = drm_fb_helper_single_add_all_connectors(helper);
144         if (ret < 0) {
145                 DRM_DEV_ERROR(dev->dev,
146                               "Failed to add connectors - %d.\n", ret);
147                 goto err_drm_fb_helper_fini;
148         }
149
150         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
151         if (ret < 0) {
152                 DRM_DEV_ERROR(dev->dev,
153                               "Failed to set initial hw config - %d.\n",
154                               ret);
155                 goto err_drm_fb_helper_fini;
156         }
157
158         return 0;
159
160 err_drm_fb_helper_fini:
161         drm_fb_helper_fini(helper);
162         return ret;
163 }
164
165 void rockchip_drm_fbdev_fini(struct drm_device *dev)
166 {
167         struct rockchip_drm_private *private = dev->dev_private;
168         struct drm_fb_helper *helper;
169
170         helper = &private->fbdev_helper;
171
172         drm_fb_helper_unregister_fbi(helper);
173
174         if (helper->fb)
175                 drm_framebuffer_put(helper->fb);
176
177         drm_fb_helper_fini(helper);
178 }