blob: ce946b9c57a9f36e7c3d50c6e4c00c837565690a [file] [log] [blame]
Mark Yao2048e322014-08-22 18:36:26 +08001/*
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"
Baoyou Xie813cfc82016-09-25 15:38:28 +080023#include "rockchip_drm_fbdev.h"
Mark Yao2048e322014-08-22 18:36:26 +080024
25#define PREFERRED_BPP 32
26#define to_drm_private(x) \
27 container_of(x, struct rockchip_drm_private, fbdev_helper)
28
29static 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
38static struct fb_ops rockchip_drm_fbdev_ops = {
39 .owner = THIS_MODULE,
Stefan Christ72b6bcb2016-11-14 00:03:20 +010040 DRM_FB_HELPER_DEFAULT_OPS,
Mark Yao2048e322014-08-22 18:36:26 +080041 .fb_mmap = rockchip_fbdev_mmap,
Archit Tanejadf3b0312015-07-22 14:58:03 +053042 .fb_fillrect = drm_fb_helper_cfb_fillrect,
43 .fb_copyarea = drm_fb_helper_cfb_copyarea,
44 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Mark Yao2048e322014-08-22 18:36:26 +080045};
46
47static 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
Daniel Kurtzf76c83b2015-01-12 14:58:23 +080071 rk_obj = rockchip_gem_create_object(dev, size, true);
Mark Yao2048e322014-08-22 18:36:26 +080072 if (IS_ERR(rk_obj))
73 return -ENOMEM;
74
75 private->fbdev_bo = &rk_obj->base;
76
Archit Tanejadf3b0312015-07-22 14:58:03 +053077 fbi = drm_fb_helper_alloc_fbi(helper);
78 if (IS_ERR(fbi)) {
79 dev_err(dev->dev, "Failed to create framebuffer info.\n");
80 ret = PTR_ERR(fbi);
Daniel Vetterda7bdda2017-02-07 17:16:03 +010081 goto out;
Mark Yao2048e322014-08-22 18:36:26 +080082 }
83
84 helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
85 private->fbdev_bo);
86 if (IS_ERR(helper->fb)) {
87 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
88 ret = PTR_ERR(helper->fb);
Daniel Vetterda7bdda2017-02-07 17:16:03 +010089 goto out;
Mark Yao2048e322014-08-22 18:36:26 +080090 }
91
Mark Yao2048e322014-08-22 18:36:26 +080092 fbi->par = helper;
93 fbi->flags = FBINFO_FLAG_DEFAULT;
94 fbi->fbops = &rockchip_drm_fbdev_ops;
95
Mark Yao2048e322014-08-22 18:36:26 +080096 fb = helper->fb;
Ville Syrjäläb00c6002016-12-14 23:31:35 +020097 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
Rob Clarkd3c8ea32015-03-11 10:23:12 -040098 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
Mark Yao2048e322014-08-22 18:36:26 +080099
100 offset = fbi->var.xoffset * bytes_per_pixel;
101 offset += fbi->var.yoffset * fb->pitches[0];
102
103 dev->mode_config.fb_base = 0;
104 fbi->screen_base = rk_obj->kvaddr + offset;
105 fbi->screen_size = rk_obj->base.size;
106 fbi->fix.smem_len = rk_obj->base.size;
107
Brian Norris913bb402016-06-09 10:46:32 -0700108 DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200109 fb->width, fb->height, fb->format->depth,
110 rk_obj->kvaddr,
Mark Yao2048e322014-08-22 18:36:26 +0800111 offset, size);
Caesar Wangb340b3f2015-03-31 18:22:50 +0800112
113 fbi->skip_vt_switch = true;
114
Mark Yao2048e322014-08-22 18:36:26 +0800115 return 0;
116
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100117out:
Mark Yao2048e322014-08-22 18:36:26 +0800118 rockchip_gem_free_object(&rk_obj->base);
119 return ret;
120}
121
122static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
123 .fb_probe = rockchip_drm_fbdev_create,
124};
125
126int rockchip_drm_fbdev_init(struct drm_device *dev)
127{
128 struct rockchip_drm_private *private = dev->dev_private;
129 struct drm_fb_helper *helper;
Mark Yao2048e322014-08-22 18:36:26 +0800130 int ret;
131
132 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
133 return -EINVAL;
134
Mark Yao2048e322014-08-22 18:36:26 +0800135 helper = &private->fbdev_helper;
136
137 drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
138
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200139 ret = drm_fb_helper_init(dev, helper, ROCKCHIP_MAX_CONNECTOR);
Mark Yao2048e322014-08-22 18:36:26 +0800140 if (ret < 0) {
141 dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n",
142 ret);
143 return ret;
144 }
145
146 ret = drm_fb_helper_single_add_all_connectors(helper);
147 if (ret < 0) {
148 dev_err(dev->dev, "Failed to add connectors - %d.\n", ret);
149 goto err_drm_fb_helper_fini;
150 }
151
Mark Yao2048e322014-08-22 18:36:26 +0800152 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
153 if (ret < 0) {
154 dev_err(dev->dev, "Failed to set initial hw config - %d.\n",
155 ret);
156 goto err_drm_fb_helper_fini;
157 }
158
159 return 0;
160
161err_drm_fb_helper_fini:
162 drm_fb_helper_fini(helper);
163 return ret;
164}
165
166void rockchip_drm_fbdev_fini(struct drm_device *dev)
167{
168 struct rockchip_drm_private *private = dev->dev_private;
169 struct drm_fb_helper *helper;
170
171 helper = &private->fbdev_helper;
172
Archit Tanejadf3b0312015-07-22 14:58:03 +0530173 drm_fb_helper_unregister_fbi(helper);
Mark Yao2048e322014-08-22 18:36:26 +0800174
175 if (helper->fb)
176 drm_framebuffer_unreference(helper->fb);
177
178 drm_fb_helper_fini(helper);
179}