Merge branch 'drm-next-4.9' of git://people.freedesktop.org/~agd5f/linux into drm...
[cascardo/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_crtc_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         .fb_mmap        = rockchip_fbdev_mmap,
41         .fb_fillrect    = drm_fb_helper_cfb_fillrect,
42         .fb_copyarea    = drm_fb_helper_cfb_copyarea,
43         .fb_imageblit   = drm_fb_helper_cfb_imageblit,
44         .fb_check_var   = drm_fb_helper_check_var,
45         .fb_set_par     = drm_fb_helper_set_par,
46         .fb_blank       = drm_fb_helper_blank,
47         .fb_pan_display = drm_fb_helper_pan_display,
48         .fb_setcmap     = drm_fb_helper_setcmap,
49 };
50
51 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
52                                      struct drm_fb_helper_surface_size *sizes)
53 {
54         struct rockchip_drm_private *private = to_drm_private(helper);
55         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
56         struct drm_device *dev = helper->dev;
57         struct rockchip_gem_object *rk_obj;
58         struct drm_framebuffer *fb;
59         unsigned int bytes_per_pixel;
60         unsigned long offset;
61         struct fb_info *fbi;
62         size_t size;
63         int ret;
64
65         bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
66
67         mode_cmd.width = sizes->surface_width;
68         mode_cmd.height = sizes->surface_height;
69         mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
70         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
71                 sizes->surface_depth);
72
73         size = mode_cmd.pitches[0] * mode_cmd.height;
74
75         rk_obj = rockchip_gem_create_object(dev, size, true);
76         if (IS_ERR(rk_obj))
77                 return -ENOMEM;
78
79         private->fbdev_bo = &rk_obj->base;
80
81         fbi = drm_fb_helper_alloc_fbi(helper);
82         if (IS_ERR(fbi)) {
83                 dev_err(dev->dev, "Failed to create framebuffer info.\n");
84                 ret = PTR_ERR(fbi);
85                 goto err_rockchip_gem_free_object;
86         }
87
88         helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
89                                                    private->fbdev_bo);
90         if (IS_ERR(helper->fb)) {
91                 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
92                 ret = PTR_ERR(helper->fb);
93                 goto err_release_fbi;
94         }
95
96         fbi->par = helper;
97         fbi->flags = FBINFO_FLAG_DEFAULT;
98         fbi->fbops = &rockchip_drm_fbdev_ops;
99
100         fb = helper->fb;
101         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
102         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
103
104         offset = fbi->var.xoffset * bytes_per_pixel;
105         offset += fbi->var.yoffset * fb->pitches[0];
106
107         dev->mode_config.fb_base = 0;
108         fbi->screen_base = rk_obj->kvaddr + offset;
109         fbi->screen_size = rk_obj->base.size;
110         fbi->fix.smem_len = rk_obj->base.size;
111
112         DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
113                       fb->width, fb->height, fb->depth, rk_obj->kvaddr,
114                       offset, size);
115
116         fbi->skip_vt_switch = true;
117
118         return 0;
119
120 err_release_fbi:
121         drm_fb_helper_release_fbi(helper);
122 err_rockchip_gem_free_object:
123         rockchip_gem_free_object(&rk_obj->base);
124         return ret;
125 }
126
127 static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
128         .fb_probe = rockchip_drm_fbdev_create,
129 };
130
131 int rockchip_drm_fbdev_init(struct drm_device *dev)
132 {
133         struct rockchip_drm_private *private = dev->dev_private;
134         struct drm_fb_helper *helper;
135         unsigned int num_crtc;
136         int ret;
137
138         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
139                 return -EINVAL;
140
141         num_crtc = dev->mode_config.num_crtc;
142
143         helper = &private->fbdev_helper;
144
145         drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
146
147         ret = drm_fb_helper_init(dev, helper, num_crtc, ROCKCHIP_MAX_CONNECTOR);
148         if (ret < 0) {
149                 dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n",
150                         ret);
151                 return ret;
152         }
153
154         ret = drm_fb_helper_single_add_all_connectors(helper);
155         if (ret < 0) {
156                 dev_err(dev->dev, "Failed to add connectors - %d.\n", ret);
157                 goto err_drm_fb_helper_fini;
158         }
159
160         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
161         if (ret < 0) {
162                 dev_err(dev->dev, "Failed to set initial hw config - %d.\n",
163                         ret);
164                 goto err_drm_fb_helper_fini;
165         }
166
167         return 0;
168
169 err_drm_fb_helper_fini:
170         drm_fb_helper_fini(helper);
171         return ret;
172 }
173
174 void rockchip_drm_fbdev_fini(struct drm_device *dev)
175 {
176         struct rockchip_drm_private *private = dev->dev_private;
177         struct drm_fb_helper *helper;
178
179         helper = &private->fbdev_helper;
180
181         drm_fb_helper_unregister_fbi(helper);
182         drm_fb_helper_release_fbi(helper);
183
184         if (helper->fb)
185                 drm_framebuffer_unreference(helper->fb);
186
187         drm_fb_helper_fini(helper);
188 }