Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 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 | |
| 24 | #define PREFERRED_BPP 32 |
| 25 | #define to_drm_private(x) \ |
| 26 | container_of(x, struct rockchip_drm_private, fbdev_helper) |
| 27 | |
| 28 | static int rockchip_fbdev_mmap(struct fb_info *info, |
| 29 | struct vm_area_struct *vma) |
| 30 | { |
| 31 | struct drm_fb_helper *helper = info->par; |
| 32 | struct rockchip_drm_private *private = to_drm_private(helper); |
| 33 | |
| 34 | return rockchip_gem_mmap_buf(private->fbdev_bo, vma); |
| 35 | } |
| 36 | |
| 37 | static struct fb_ops rockchip_drm_fbdev_ops = { |
| 38 | .owner = THIS_MODULE, |
| 39 | .fb_mmap = rockchip_fbdev_mmap, |
Archit Taneja | df3b031 | 2015-07-22 14:58:03 +0530 | [diff] [blame] | 40 | .fb_fillrect = drm_fb_helper_cfb_fillrect, |
| 41 | .fb_copyarea = drm_fb_helper_cfb_copyarea, |
| 42 | .fb_imageblit = drm_fb_helper_cfb_imageblit, |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 43 | .fb_check_var = drm_fb_helper_check_var, |
| 44 | .fb_set_par = drm_fb_helper_set_par, |
| 45 | .fb_blank = drm_fb_helper_blank, |
| 46 | .fb_pan_display = drm_fb_helper_pan_display, |
| 47 | .fb_setcmap = drm_fb_helper_setcmap, |
| 48 | }; |
| 49 | |
| 50 | static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper, |
| 51 | struct drm_fb_helper_surface_size *sizes) |
| 52 | { |
| 53 | struct rockchip_drm_private *private = to_drm_private(helper); |
| 54 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
| 55 | struct drm_device *dev = helper->dev; |
| 56 | struct rockchip_gem_object *rk_obj; |
| 57 | struct drm_framebuffer *fb; |
| 58 | unsigned int bytes_per_pixel; |
| 59 | unsigned long offset; |
| 60 | struct fb_info *fbi; |
| 61 | size_t size; |
| 62 | int ret; |
| 63 | |
| 64 | bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); |
| 65 | |
| 66 | mode_cmd.width = sizes->surface_width; |
| 67 | mode_cmd.height = sizes->surface_height; |
| 68 | mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel; |
| 69 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 70 | sizes->surface_depth); |
| 71 | |
| 72 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 73 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 74 | rk_obj = rockchip_gem_create_object(dev, size, true); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 75 | if (IS_ERR(rk_obj)) |
| 76 | return -ENOMEM; |
| 77 | |
| 78 | private->fbdev_bo = &rk_obj->base; |
| 79 | |
Archit Taneja | df3b031 | 2015-07-22 14:58:03 +0530 | [diff] [blame] | 80 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 81 | if (IS_ERR(fbi)) { |
| 82 | dev_err(dev->dev, "Failed to create framebuffer info.\n"); |
| 83 | ret = PTR_ERR(fbi); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 84 | goto err_rockchip_gem_free_object; |
| 85 | } |
| 86 | |
| 87 | helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd, |
| 88 | private->fbdev_bo); |
| 89 | if (IS_ERR(helper->fb)) { |
| 90 | dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n"); |
| 91 | ret = PTR_ERR(helper->fb); |
Archit Taneja | df3b031 | 2015-07-22 14:58:03 +0530 | [diff] [blame] | 92 | goto err_release_fbi; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 95 | fbi->par = helper; |
| 96 | fbi->flags = FBINFO_FLAG_DEFAULT; |
| 97 | fbi->fbops = &rockchip_drm_fbdev_ops; |
| 98 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 99 | fb = helper->fb; |
| 100 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
Rob Clark | d3c8ea3 | 2015-03-11 10:23:12 -0400 | [diff] [blame] | 101 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 102 | |
| 103 | offset = fbi->var.xoffset * bytes_per_pixel; |
| 104 | offset += fbi->var.yoffset * fb->pitches[0]; |
| 105 | |
| 106 | dev->mode_config.fb_base = 0; |
| 107 | fbi->screen_base = rk_obj->kvaddr + offset; |
| 108 | fbi->screen_size = rk_obj->base.size; |
| 109 | fbi->fix.smem_len = rk_obj->base.size; |
| 110 | |
| 111 | DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%d\n", |
| 112 | fb->width, fb->height, fb->depth, rk_obj->kvaddr, |
| 113 | offset, size); |
Caesar Wang | b340b3f | 2015-03-31 18:22:50 +0800 | [diff] [blame] | 114 | |
| 115 | fbi->skip_vt_switch = true; |
| 116 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 117 | return 0; |
| 118 | |
Archit Taneja | df3b031 | 2015-07-22 14:58:03 +0530 | [diff] [blame] | 119 | err_release_fbi: |
| 120 | drm_fb_helper_release_fbi(helper); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 121 | err_rockchip_gem_free_object: |
| 122 | rockchip_gem_free_object(&rk_obj->base); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = { |
| 127 | .fb_probe = rockchip_drm_fbdev_create, |
| 128 | }; |
| 129 | |
| 130 | int rockchip_drm_fbdev_init(struct drm_device *dev) |
| 131 | { |
| 132 | struct rockchip_drm_private *private = dev->dev_private; |
| 133 | struct drm_fb_helper *helper; |
| 134 | unsigned int num_crtc; |
| 135 | int ret; |
| 136 | |
| 137 | if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | num_crtc = dev->mode_config.num_crtc; |
| 141 | |
| 142 | helper = &private->fbdev_helper; |
| 143 | |
| 144 | drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs); |
| 145 | |
| 146 | ret = drm_fb_helper_init(dev, helper, num_crtc, ROCKCHIP_MAX_CONNECTOR); |
| 147 | if (ret < 0) { |
| 148 | dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n", |
| 149 | ret); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | ret = drm_fb_helper_single_add_all_connectors(helper); |
| 154 | if (ret < 0) { |
| 155 | dev_err(dev->dev, "Failed to add connectors - %d.\n", ret); |
| 156 | goto err_drm_fb_helper_fini; |
| 157 | } |
| 158 | |
| 159 | /* disable all the possible outputs/crtcs before entering KMS mode */ |
| 160 | drm_helper_disable_unused_functions(dev); |
| 161 | |
| 162 | ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP); |
| 163 | if (ret < 0) { |
| 164 | dev_err(dev->dev, "Failed to set initial hw config - %d.\n", |
| 165 | ret); |
| 166 | goto err_drm_fb_helper_fini; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | err_drm_fb_helper_fini: |
| 172 | drm_fb_helper_fini(helper); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | void rockchip_drm_fbdev_fini(struct drm_device *dev) |
| 177 | { |
| 178 | struct rockchip_drm_private *private = dev->dev_private; |
| 179 | struct drm_fb_helper *helper; |
| 180 | |
| 181 | helper = &private->fbdev_helper; |
| 182 | |
Archit Taneja | df3b031 | 2015-07-22 14:58:03 +0530 | [diff] [blame] | 183 | drm_fb_helper_unregister_fbi(helper); |
| 184 | drm_fb_helper_release_fbi(helper); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 185 | |
| 186 | if (helper->fb) |
| 187 | drm_framebuffer_unreference(helper->fb); |
| 188 | |
| 189 | drm_fb_helper_fini(helper); |
| 190 | } |