Loading...
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
22static 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 drm_fb_helper_unprepare(fbh);
32 kfree(fbh);
33}
34
35static const struct fb_ops armada_fb_ops = {
36 .owner = THIS_MODULE,
37 FB_DEFAULT_IOMEM_OPS,
38 DRM_FB_HELPER_DEFAULT_OPS,
39 .fb_destroy = armada_fbdev_fb_destroy,
40};
41
42static const struct drm_fb_helper_funcs armada_fbdev_helper_funcs;
43
44int armada_fbdev_driver_fbdev_probe(struct drm_fb_helper *fbh,
45 struct drm_fb_helper_surface_size *sizes)
46{
47 struct drm_device *dev = fbh->dev;
48 struct drm_mode_fb_cmd2 mode;
49 struct armada_framebuffer *dfb;
50 struct armada_gem_object *obj;
51 struct fb_info *info;
52 int size, ret;
53 void *ptr;
54
55 memset(&mode, 0, sizeof(mode));
56 mode.width = sizes->surface_width;
57 mode.height = sizes->surface_height;
58 mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
59 mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
60 sizes->surface_depth);
61
62 size = mode.pitches[0] * mode.height;
63 obj = armada_gem_alloc_private_object(dev, size);
64 if (!obj) {
65 DRM_ERROR("failed to allocate fb memory\n");
66 return -ENOMEM;
67 }
68
69 ret = armada_gem_linear_back(dev, obj);
70 if (ret) {
71 drm_gem_object_put(&obj->obj);
72 return ret;
73 }
74
75 ptr = armada_gem_map_object(dev, obj);
76 if (!ptr) {
77 drm_gem_object_put(&obj->obj);
78 return -ENOMEM;
79 }
80
81 dfb = armada_framebuffer_create(dev, &mode, obj);
82
83 /*
84 * A reference is now held by the framebuffer object if
85 * successful, otherwise this drops the ref for the error path.
86 */
87 drm_gem_object_put(&obj->obj);
88
89 if (IS_ERR(dfb))
90 return PTR_ERR(dfb);
91
92 info = drm_fb_helper_alloc_info(fbh);
93 if (IS_ERR(info)) {
94 ret = PTR_ERR(info);
95 goto err_fballoc;
96 }
97
98 info->fbops = &armada_fb_ops;
99 info->fix.smem_start = obj->phys_addr;
100 info->fix.smem_len = obj->obj.size;
101 info->screen_size = obj->obj.size;
102 info->screen_base = ptr;
103 fbh->funcs = &armada_fbdev_helper_funcs;
104 fbh->fb = &dfb->fb;
105
106 drm_fb_helper_fill_info(info, fbh, sizes);
107
108 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
109 dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
110 (unsigned long long)obj->phys_addr);
111
112 return 0;
113
114 err_fballoc:
115 dfb->fb.funcs->destroy(&dfb->fb);
116 return ret;
117}
1/*
2 * Copyright (C) 2012 Russell King
3 * Written from the i915 driver.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/errno.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12
13#include <drm/drm_fb_helper.h>
14#include "armada_crtc.h"
15#include "armada_drm.h"
16#include "armada_fb.h"
17#include "armada_gem.h"
18
19static /*const*/ struct fb_ops armada_fb_ops = {
20 .owner = THIS_MODULE,
21 DRM_FB_HELPER_DEFAULT_OPS,
22 .fb_fillrect = drm_fb_helper_cfb_fillrect,
23 .fb_copyarea = drm_fb_helper_cfb_copyarea,
24 .fb_imageblit = drm_fb_helper_cfb_imageblit,
25};
26
27static int armada_fb_create(struct drm_fb_helper *fbh,
28 struct drm_fb_helper_surface_size *sizes)
29{
30 struct drm_device *dev = fbh->dev;
31 struct drm_mode_fb_cmd2 mode;
32 struct armada_framebuffer *dfb;
33 struct armada_gem_object *obj;
34 struct fb_info *info;
35 int size, ret;
36 void *ptr;
37
38 memset(&mode, 0, sizeof(mode));
39 mode.width = sizes->surface_width;
40 mode.height = sizes->surface_height;
41 mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
42 mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
43 sizes->surface_depth);
44
45 size = mode.pitches[0] * mode.height;
46 obj = armada_gem_alloc_private_object(dev, size);
47 if (!obj) {
48 DRM_ERROR("failed to allocate fb memory\n");
49 return -ENOMEM;
50 }
51
52 ret = armada_gem_linear_back(dev, obj);
53 if (ret) {
54 drm_gem_object_put_unlocked(&obj->obj);
55 return ret;
56 }
57
58 ptr = armada_gem_map_object(dev, obj);
59 if (!ptr) {
60 drm_gem_object_put_unlocked(&obj->obj);
61 return -ENOMEM;
62 }
63
64 dfb = armada_framebuffer_create(dev, &mode, obj);
65
66 /*
67 * A reference is now held by the framebuffer object if
68 * successful, otherwise this drops the ref for the error path.
69 */
70 drm_gem_object_put_unlocked(&obj->obj);
71
72 if (IS_ERR(dfb))
73 return PTR_ERR(dfb);
74
75 info = drm_fb_helper_alloc_fbi(fbh);
76 if (IS_ERR(info)) {
77 ret = PTR_ERR(info);
78 goto err_fballoc;
79 }
80
81 strlcpy(info->fix.id, "armada-drmfb", sizeof(info->fix.id));
82 info->par = fbh;
83 info->fbops = &armada_fb_ops;
84 info->fix.smem_start = obj->phys_addr;
85 info->fix.smem_len = obj->obj.size;
86 info->screen_size = obj->obj.size;
87 info->screen_base = ptr;
88 fbh->fb = &dfb->fb;
89
90 drm_fb_helper_fill_fix(info, dfb->fb.pitches[0],
91 dfb->fb.format->depth);
92 drm_fb_helper_fill_var(info, fbh, sizes->fb_width, sizes->fb_height);
93
94 DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
95 dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
96 (unsigned long long)obj->phys_addr);
97
98 return 0;
99
100 err_fballoc:
101 dfb->fb.funcs->destroy(&dfb->fb);
102 return ret;
103}
104
105static int armada_fb_probe(struct drm_fb_helper *fbh,
106 struct drm_fb_helper_surface_size *sizes)
107{
108 int ret = 0;
109
110 if (!fbh->fb) {
111 ret = armada_fb_create(fbh, sizes);
112 if (ret == 0)
113 ret = 1;
114 }
115 return ret;
116}
117
118static const struct drm_fb_helper_funcs armada_fb_helper_funcs = {
119 .fb_probe = armada_fb_probe,
120};
121
122int armada_fbdev_init(struct drm_device *dev)
123{
124 struct armada_private *priv = dev->dev_private;
125 struct drm_fb_helper *fbh;
126 int ret;
127
128 fbh = devm_kzalloc(dev->dev, sizeof(*fbh), GFP_KERNEL);
129 if (!fbh)
130 return -ENOMEM;
131
132 priv->fbdev = fbh;
133
134 drm_fb_helper_prepare(dev, fbh, &armada_fb_helper_funcs);
135
136 ret = drm_fb_helper_init(dev, fbh, 1);
137 if (ret) {
138 DRM_ERROR("failed to initialize drm fb helper\n");
139 goto err_fb_helper;
140 }
141
142 ret = drm_fb_helper_single_add_all_connectors(fbh);
143 if (ret) {
144 DRM_ERROR("failed to add fb connectors\n");
145 goto err_fb_setup;
146 }
147
148 ret = drm_fb_helper_initial_config(fbh, 32);
149 if (ret) {
150 DRM_ERROR("failed to set initial config\n");
151 goto err_fb_setup;
152 }
153
154 return 0;
155 err_fb_setup:
156 drm_fb_helper_fini(fbh);
157 err_fb_helper:
158 priv->fbdev = NULL;
159 return ret;
160}
161
162void armada_fbdev_fini(struct drm_device *dev)
163{
164 struct armada_private *priv = dev->dev_private;
165 struct drm_fb_helper *fbh = priv->fbdev;
166
167 if (fbh) {
168 drm_fb_helper_unregister_fbi(fbh);
169
170 drm_fb_helper_fini(fbh);
171
172 if (fbh->fb)
173 fbh->fb->funcs->destroy(fbh->fb);
174
175 priv->fbdev = NULL;
176 }
177}