1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Russell King 4 * Written from the i915 driver. 5 */ 6 7 #include <linux/errno.h> 8 #include <linux/fb.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 12 #include <drm/drm_crtc_helper.h> 13 #include <drm/drm_drv.h> 14 #include <drm/drm_fb_helper.h> 15 #include <drm/drm_fourcc.h> 16 17 #include "armada_crtc.h" 18 #include "armada_drm.h" 19 #include "armada_fb.h" 20 #include "armada_gem.h" 21 22 static void armada_fbdev_fb_destroy(struct fb_info *info) 23 { 24 struct drm_fb_helper *fbh = info->par; 25 26 drm_fb_helper_fini(fbh); 27 28 fbh->fb->funcs->destroy(fbh->fb); 29 30 drm_client_release(&fbh->client); 31 } 32 33 static const struct fb_ops armada_fb_ops = { 34 .owner = THIS_MODULE, 35 FB_DEFAULT_IOMEM_OPS, 36 DRM_FB_HELPER_DEFAULT_OPS, 37 .fb_destroy = armada_fbdev_fb_destroy, 38 }; 39 40 static const struct drm_fb_helper_funcs armada_fbdev_helper_funcs; 41 42 int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper *fbh, 43 struct drm_fb_helper_surface_size *sizes) 44 { 45 struct drm_device *dev = fbh->dev; 46 struct drm_mode_fb_cmd2 mode; 47 struct armada_framebuffer *dfb; 48 struct armada_gem_object *obj; 49 struct fb_info *info; 50 int size, ret; 51 void *ptr; 52 53 memset(&mode, 0, sizeof(mode)); 54 mode.width = sizes->surface_width; 55 mode.height = sizes->surface_height; 56 mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp); 57 mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 58 sizes->surface_depth); 59 60 size = mode.pitches[0] * mode.height; 61 obj = armada_gem_alloc_private_object(dev, size); 62 if (!obj) { 63 DRM_ERROR("failed to allocate fb memory\n"); 64 return -ENOMEM; 65 } 66 67 ret = armada_gem_linear_back(dev, obj); 68 if (ret) { 69 drm_gem_object_put(&obj->obj); 70 return ret; 71 } 72 73 ptr = armada_gem_map_object(dev, obj); 74 if (!ptr) { 75 drm_gem_object_put(&obj->obj); 76 return -ENOMEM; 77 } 78 79 dfb = armada_framebuffer_create(dev, 80 drm_get_format_info(dev, mode.pixel_format, 81 mode.modifier[0]), 82 &mode, obj); 83 84 /* 85 * A reference is now held by the framebuffer object if 86 * successful, otherwise this drops the ref for the error path. 87 */ 88 drm_gem_object_put(&obj->obj); 89 90 if (IS_ERR(dfb)) 91 return PTR_ERR(dfb); 92 93 info = drm_fb_helper_alloc_info(fbh); 94 if (IS_ERR(info)) { 95 ret = PTR_ERR(info); 96 goto err_fballoc; 97 } 98 99 info->fbops = &armada_fb_ops; 100 info->fix.smem_start = obj->phys_addr; 101 info->fix.smem_len = obj->obj.size; 102 info->screen_size = obj->obj.size; 103 info->screen_base = ptr; 104 fbh->funcs = &armada_fbdev_helper_funcs; 105 fbh->fb = &dfb->fb; 106 107 drm_fb_helper_fill_info(info, fbh, sizes); 108 109 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n", 110 dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8, 111 (unsigned long long)obj->phys_addr); 112 113 return 0; 114 115 err_fballoc: 116 dfb->fb.funcs->destroy(&dfb->fb); 117 return ret; 118 } 119