Linux Audio

Check our new training course

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