1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * DRM core format related functions
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
7*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
8*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
9*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
10*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
11*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
12*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
13*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
14*4882a593Smuzhiyun * is" without express or implied warranty.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22*4882a593Smuzhiyun * OF THIS SOFTWARE.
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include <linux/bug.h>
26*4882a593Smuzhiyun #include <linux/ctype.h>
27*4882a593Smuzhiyun #include <linux/export.h>
28*4882a593Smuzhiyun #include <linux/kernel.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <drm/drm_device.h>
31*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
32*4882a593Smuzhiyun
printable_char(int c)33*4882a593Smuzhiyun static char printable_char(int c)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun return isascii(c) && isprint(c) ? c : '?';
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /**
39*4882a593Smuzhiyun * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
40*4882a593Smuzhiyun * @bpp: bits per pixels
41*4882a593Smuzhiyun * @depth: bit depth per pixel
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
44*4882a593Smuzhiyun * Useful in fbdev emulation code, since that deals in those values.
45*4882a593Smuzhiyun */
drm_mode_legacy_fb_format(uint32_t bpp,uint32_t depth)46*4882a593Smuzhiyun uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun uint32_t fmt = DRM_FORMAT_INVALID;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun switch (bpp) {
51*4882a593Smuzhiyun case 8:
52*4882a593Smuzhiyun if (depth == 8)
53*4882a593Smuzhiyun fmt = DRM_FORMAT_C8;
54*4882a593Smuzhiyun break;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun case 16:
57*4882a593Smuzhiyun switch (depth) {
58*4882a593Smuzhiyun case 15:
59*4882a593Smuzhiyun fmt = DRM_FORMAT_XRGB1555;
60*4882a593Smuzhiyun break;
61*4882a593Smuzhiyun case 16:
62*4882a593Smuzhiyun fmt = DRM_FORMAT_RGB565;
63*4882a593Smuzhiyun break;
64*4882a593Smuzhiyun default:
65*4882a593Smuzhiyun break;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun break;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun case 24:
70*4882a593Smuzhiyun if (depth == 24)
71*4882a593Smuzhiyun fmt = DRM_FORMAT_RGB888;
72*4882a593Smuzhiyun break;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun case 32:
75*4882a593Smuzhiyun switch (depth) {
76*4882a593Smuzhiyun case 24:
77*4882a593Smuzhiyun fmt = DRM_FORMAT_XRGB8888;
78*4882a593Smuzhiyun break;
79*4882a593Smuzhiyun case 30:
80*4882a593Smuzhiyun fmt = DRM_FORMAT_XRGB2101010;
81*4882a593Smuzhiyun break;
82*4882a593Smuzhiyun case 32:
83*4882a593Smuzhiyun fmt = DRM_FORMAT_ARGB8888;
84*4882a593Smuzhiyun break;
85*4882a593Smuzhiyun default:
86*4882a593Smuzhiyun break;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun default:
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return fmt;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_legacy_fb_format);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * drm_driver_legacy_fb_format - compute drm fourcc code from legacy description
100*4882a593Smuzhiyun * @dev: DRM device
101*4882a593Smuzhiyun * @bpp: bits per pixels
102*4882a593Smuzhiyun * @depth: bit depth per pixel
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
105*4882a593Smuzhiyun * Unlike drm_mode_legacy_fb_format() this looks at the drivers mode_config,
106*4882a593Smuzhiyun * and depending on the &drm_mode_config.quirk_addfb_prefer_host_byte_order flag
107*4882a593Smuzhiyun * it returns little endian byte order or host byte order framebuffer formats.
108*4882a593Smuzhiyun */
drm_driver_legacy_fb_format(struct drm_device * dev,uint32_t bpp,uint32_t depth)109*4882a593Smuzhiyun uint32_t drm_driver_legacy_fb_format(struct drm_device *dev,
110*4882a593Smuzhiyun uint32_t bpp, uint32_t depth)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun uint32_t fmt = drm_mode_legacy_fb_format(bpp, depth);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (dev->mode_config.quirk_addfb_prefer_host_byte_order) {
115*4882a593Smuzhiyun if (fmt == DRM_FORMAT_XRGB8888)
116*4882a593Smuzhiyun fmt = DRM_FORMAT_HOST_XRGB8888;
117*4882a593Smuzhiyun if (fmt == DRM_FORMAT_ARGB8888)
118*4882a593Smuzhiyun fmt = DRM_FORMAT_HOST_ARGB8888;
119*4882a593Smuzhiyun if (fmt == DRM_FORMAT_RGB565)
120*4882a593Smuzhiyun fmt = DRM_FORMAT_HOST_RGB565;
121*4882a593Smuzhiyun if (fmt == DRM_FORMAT_XRGB1555)
122*4882a593Smuzhiyun fmt = DRM_FORMAT_HOST_XRGB1555;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (dev->mode_config.quirk_addfb_prefer_xbgr_30bpp &&
126*4882a593Smuzhiyun fmt == DRM_FORMAT_XRGB2101010)
127*4882a593Smuzhiyun fmt = DRM_FORMAT_XBGR2101010;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun return fmt;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun EXPORT_SYMBOL(drm_driver_legacy_fb_format);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun * drm_get_format_name - fill a string with a drm fourcc format's name
135*4882a593Smuzhiyun * @format: format to compute name of
136*4882a593Smuzhiyun * @buf: caller-supplied buffer
137*4882a593Smuzhiyun */
drm_get_format_name(uint32_t format,struct drm_format_name_buf * buf)138*4882a593Smuzhiyun const char *drm_get_format_name(uint32_t format, struct drm_format_name_buf *buf)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun snprintf(buf->str, sizeof(buf->str),
141*4882a593Smuzhiyun "%c%c%c%c %s-endian (0x%08x)",
142*4882a593Smuzhiyun printable_char(format & 0xff),
143*4882a593Smuzhiyun printable_char((format >> 8) & 0xff),
144*4882a593Smuzhiyun printable_char((format >> 16) & 0xff),
145*4882a593Smuzhiyun printable_char((format >> 24) & 0x7f),
146*4882a593Smuzhiyun format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
147*4882a593Smuzhiyun format);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return buf->str;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun EXPORT_SYMBOL(drm_get_format_name);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * Internal function to query information for a given format. See
155*4882a593Smuzhiyun * drm_format_info() for the public API.
156*4882a593Smuzhiyun */
__drm_format_info(u32 format)157*4882a593Smuzhiyun const struct drm_format_info *__drm_format_info(u32 format)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun static const struct drm_format_info formats[] = {
160*4882a593Smuzhiyun { .format = DRM_FORMAT_C8, .depth = 8, .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
161*4882a593Smuzhiyun { .format = DRM_FORMAT_RGB332, .depth = 8, .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
162*4882a593Smuzhiyun { .format = DRM_FORMAT_BGR233, .depth = 8, .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
163*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
164*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
165*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBX4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
166*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRX4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
167*4882a593Smuzhiyun { .format = DRM_FORMAT_ARGB4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
168*4882a593Smuzhiyun { .format = DRM_FORMAT_ABGR4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
169*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBA4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
170*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRA4444, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
171*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB1555, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
172*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR1555, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
173*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBX5551, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
174*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRX5551, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
175*4882a593Smuzhiyun { .format = DRM_FORMAT_ARGB1555, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
176*4882a593Smuzhiyun { .format = DRM_FORMAT_ABGR1555, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
177*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBA5551, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
178*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRA5551, .depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
179*4882a593Smuzhiyun { .format = DRM_FORMAT_RGB565, .depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
180*4882a593Smuzhiyun { .format = DRM_FORMAT_BGR565, .depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
181*4882a593Smuzhiyun { .format = DRM_FORMAT_RGB888, .depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
182*4882a593Smuzhiyun { .format = DRM_FORMAT_BGR888, .depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
183*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
184*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
185*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBX8888, .depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
186*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRX8888, .depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
187*4882a593Smuzhiyun { .format = DRM_FORMAT_RGB565_A8, .depth = 24, .num_planes = 2, .cpp = { 2, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
188*4882a593Smuzhiyun { .format = DRM_FORMAT_BGR565_A8, .depth = 24, .num_planes = 2, .cpp = { 2, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
189*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
190*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
191*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBX1010102, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
192*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRX1010102, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
193*4882a593Smuzhiyun { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
194*4882a593Smuzhiyun { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
195*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBA1010102, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
196*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRA1010102, .depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
197*4882a593Smuzhiyun { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
198*4882a593Smuzhiyun { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
199*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBA8888, .depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
200*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRA8888, .depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
201*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB16161616F, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1 },
202*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR16161616F, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1 },
203*4882a593Smuzhiyun { .format = DRM_FORMAT_ARGB16161616F, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
204*4882a593Smuzhiyun { .format = DRM_FORMAT_ABGR16161616F, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
205*4882a593Smuzhiyun { .format = DRM_FORMAT_RGB888_A8, .depth = 32, .num_planes = 2, .cpp = { 3, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
206*4882a593Smuzhiyun { .format = DRM_FORMAT_BGR888_A8, .depth = 32, .num_planes = 2, .cpp = { 3, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
207*4882a593Smuzhiyun { .format = DRM_FORMAT_XRGB8888_A8, .depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
208*4882a593Smuzhiyun { .format = DRM_FORMAT_XBGR8888_A8, .depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
209*4882a593Smuzhiyun { .format = DRM_FORMAT_RGBX8888_A8, .depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
210*4882a593Smuzhiyun { .format = DRM_FORMAT_BGRX8888_A8, .depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true },
211*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV410, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4, .is_yuv = true },
212*4882a593Smuzhiyun { .format = DRM_FORMAT_YVU410, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4, .is_yuv = true },
213*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV411, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1, .is_yuv = true },
214*4882a593Smuzhiyun { .format = DRM_FORMAT_YVU411, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1, .is_yuv = true },
215*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV420, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2, .is_yuv = true },
216*4882a593Smuzhiyun { .format = DRM_FORMAT_YVU420, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2, .is_yuv = true },
217*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV422, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1, .is_yuv = true },
218*4882a593Smuzhiyun { .format = DRM_FORMAT_YVU422, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1, .is_yuv = true },
219*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV444, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1, .is_yuv = true },
220*4882a593Smuzhiyun { .format = DRM_FORMAT_YVU444, .depth = 0, .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1, .is_yuv = true },
221*4882a593Smuzhiyun { .format = DRM_FORMAT_NV12, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2, .is_yuv = true },
222*4882a593Smuzhiyun { .format = DRM_FORMAT_NV21, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2, .is_yuv = true },
223*4882a593Smuzhiyun { .format = DRM_FORMAT_NV16, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
224*4882a593Smuzhiyun { .format = DRM_FORMAT_NV61, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
225*4882a593Smuzhiyun { .format = DRM_FORMAT_NV24, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
226*4882a593Smuzhiyun { .format = DRM_FORMAT_NV42, .depth = 0, .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
227*4882a593Smuzhiyun { .format = DRM_FORMAT_YUYV, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
228*4882a593Smuzhiyun { .format = DRM_FORMAT_YVYU, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
229*4882a593Smuzhiyun { .format = DRM_FORMAT_UYVY, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
230*4882a593Smuzhiyun { .format = DRM_FORMAT_VYUY, .depth = 0, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
231*4882a593Smuzhiyun { .format = DRM_FORMAT_XYUV8888, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
232*4882a593Smuzhiyun { .format = DRM_FORMAT_VUY888, .depth = 0, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
233*4882a593Smuzhiyun { .format = DRM_FORMAT_AYUV, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true, .is_yuv = true },
234*4882a593Smuzhiyun { .format = DRM_FORMAT_Y210, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
235*4882a593Smuzhiyun { .format = DRM_FORMAT_Y212, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
236*4882a593Smuzhiyun { .format = DRM_FORMAT_Y216, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 2, .vsub = 1, .is_yuv = true },
237*4882a593Smuzhiyun { .format = DRM_FORMAT_Y410, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true, .is_yuv = true },
238*4882a593Smuzhiyun { .format = DRM_FORMAT_Y412, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true, .is_yuv = true },
239*4882a593Smuzhiyun { .format = DRM_FORMAT_Y416, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .has_alpha = true, .is_yuv = true },
240*4882a593Smuzhiyun { .format = DRM_FORMAT_XVYU2101010, .depth = 0, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
241*4882a593Smuzhiyun { .format = DRM_FORMAT_XVYU12_16161616, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
242*4882a593Smuzhiyun { .format = DRM_FORMAT_XVYU16161616, .depth = 0, .num_planes = 1, .cpp = { 8, 0, 0 }, .hsub = 1, .vsub = 1, .is_yuv = true },
243*4882a593Smuzhiyun { .format = DRM_FORMAT_Y0L0, .depth = 0, .num_planes = 1,
244*4882a593Smuzhiyun .char_per_block = { 8, 0, 0 }, .block_w = { 2, 0, 0 }, .block_h = { 2, 0, 0 },
245*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .has_alpha = true, .is_yuv = true },
246*4882a593Smuzhiyun { .format = DRM_FORMAT_X0L0, .depth = 0, .num_planes = 1,
247*4882a593Smuzhiyun .char_per_block = { 8, 0, 0 }, .block_w = { 2, 0, 0 }, .block_h = { 2, 0, 0 },
248*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .is_yuv = true },
249*4882a593Smuzhiyun { .format = DRM_FORMAT_Y0L2, .depth = 0, .num_planes = 1,
250*4882a593Smuzhiyun .char_per_block = { 8, 0, 0 }, .block_w = { 2, 0, 0 }, .block_h = { 2, 0, 0 },
251*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .has_alpha = true, .is_yuv = true },
252*4882a593Smuzhiyun { .format = DRM_FORMAT_X0L2, .depth = 0, .num_planes = 1,
253*4882a593Smuzhiyun .char_per_block = { 8, 0, 0 }, .block_w = { 2, 0, 0 }, .block_h = { 2, 0, 0 },
254*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .is_yuv = true },
255*4882a593Smuzhiyun { .format = DRM_FORMAT_P010, .depth = 0, .num_planes = 2,
256*4882a593Smuzhiyun .char_per_block = { 2, 4, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
257*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .is_yuv = true},
258*4882a593Smuzhiyun { .format = DRM_FORMAT_P012, .depth = 0, .num_planes = 2,
259*4882a593Smuzhiyun .char_per_block = { 2, 4, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
260*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .is_yuv = true},
261*4882a593Smuzhiyun { .format = DRM_FORMAT_P016, .depth = 0, .num_planes = 2,
262*4882a593Smuzhiyun .char_per_block = { 2, 4, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
263*4882a593Smuzhiyun .hsub = 2, .vsub = 2, .is_yuv = true},
264*4882a593Smuzhiyun { .format = DRM_FORMAT_P210, .depth = 0,
265*4882a593Smuzhiyun .num_planes = 2, .char_per_block = { 2, 4, 0 },
266*4882a593Smuzhiyun .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 }, .hsub = 2,
267*4882a593Smuzhiyun .vsub = 1, .is_yuv = true },
268*4882a593Smuzhiyun { .format = DRM_FORMAT_VUY101010, .depth = 0,
269*4882a593Smuzhiyun .num_planes = 1, .cpp = { 0, 0, 0 }, .hsub = 1, .vsub = 1,
270*4882a593Smuzhiyun .is_yuv = true },
271*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV420_8BIT, .depth = 0,
272*4882a593Smuzhiyun .num_planes = 1, .cpp = { 0, 0, 0 }, .hsub = 2, .vsub = 2,
273*4882a593Smuzhiyun .is_yuv = true },
274*4882a593Smuzhiyun { .format = DRM_FORMAT_YUV420_10BIT, .depth = 0,
275*4882a593Smuzhiyun .num_planes = 1, .cpp = { 0, 0, 0 }, .hsub = 2, .vsub = 2,
276*4882a593Smuzhiyun .is_yuv = true },
277*4882a593Smuzhiyun { .format = DRM_FORMAT_NV15, .depth = 0,
278*4882a593Smuzhiyun .num_planes = 2, .char_per_block = { 5, 5, 0 },
279*4882a593Smuzhiyun .block_w = { 4, 2, 0 }, .block_h = { 1, 1, 0 }, .hsub = 2,
280*4882a593Smuzhiyun .vsub = 2, .is_yuv = true },
281*4882a593Smuzhiyun #ifdef CONFIG_NO_GKI
282*4882a593Smuzhiyun { .format = DRM_FORMAT_NV20, .depth = 0,
283*4882a593Smuzhiyun .num_planes = 2, .char_per_block = { 5, 5, 0 },
284*4882a593Smuzhiyun .block_w = { 4, 2, 0 }, .block_h = { 1, 1, 0 }, .hsub = 2,
285*4882a593Smuzhiyun .vsub = 1, .is_yuv = true },
286*4882a593Smuzhiyun { .format = DRM_FORMAT_NV30, .depth = 0,
287*4882a593Smuzhiyun .num_planes = 2, .char_per_block = { 5, 5, 0 },
288*4882a593Smuzhiyun .block_w = { 4, 2, 0 }, .block_h = { 1, 1, 0 }, .hsub = 1,
289*4882a593Smuzhiyun .vsub = 1, .is_yuv = true },
290*4882a593Smuzhiyun #endif
291*4882a593Smuzhiyun { .format = DRM_FORMAT_Q410, .depth = 0,
292*4882a593Smuzhiyun .num_planes = 3, .char_per_block = { 2, 2, 2 },
293*4882a593Smuzhiyun .block_w = { 1, 1, 1 }, .block_h = { 1, 1, 1 }, .hsub = 0,
294*4882a593Smuzhiyun .vsub = 0, .is_yuv = true },
295*4882a593Smuzhiyun { .format = DRM_FORMAT_Q401, .depth = 0,
296*4882a593Smuzhiyun .num_planes = 3, .char_per_block = { 2, 2, 2 },
297*4882a593Smuzhiyun .block_w = { 1, 1, 1 }, .block_h = { 1, 1, 1 }, .hsub = 0,
298*4882a593Smuzhiyun .vsub = 0, .is_yuv = true },
299*4882a593Smuzhiyun };
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun unsigned int i;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(formats); ++i) {
304*4882a593Smuzhiyun if (formats[i].format == format)
305*4882a593Smuzhiyun return &formats[i];
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun return NULL;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun * drm_format_info - query information for a given format
313*4882a593Smuzhiyun * @format: pixel format (DRM_FORMAT_*)
314*4882a593Smuzhiyun *
315*4882a593Smuzhiyun * The caller should only pass a supported pixel format to this function.
316*4882a593Smuzhiyun * Unsupported pixel formats will generate a warning in the kernel log.
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * Returns:
319*4882a593Smuzhiyun * The instance of struct drm_format_info that describes the pixel format, or
320*4882a593Smuzhiyun * NULL if the format is unsupported.
321*4882a593Smuzhiyun */
drm_format_info(u32 format)322*4882a593Smuzhiyun const struct drm_format_info *drm_format_info(u32 format)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun const struct drm_format_info *info;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun info = __drm_format_info(format);
327*4882a593Smuzhiyun WARN_ON(!info);
328*4882a593Smuzhiyun return info;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun EXPORT_SYMBOL(drm_format_info);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /**
333*4882a593Smuzhiyun * drm_get_format_info - query information for a given framebuffer configuration
334*4882a593Smuzhiyun * @dev: DRM device
335*4882a593Smuzhiyun * @mode_cmd: metadata from the userspace fb creation request
336*4882a593Smuzhiyun *
337*4882a593Smuzhiyun * Returns:
338*4882a593Smuzhiyun * The instance of struct drm_format_info that describes the pixel format, or
339*4882a593Smuzhiyun * NULL if the format is unsupported.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun const struct drm_format_info *
drm_get_format_info(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd)342*4882a593Smuzhiyun drm_get_format_info(struct drm_device *dev,
343*4882a593Smuzhiyun const struct drm_mode_fb_cmd2 *mode_cmd)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun const struct drm_format_info *info = NULL;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun if (dev->mode_config.funcs->get_format_info)
348*4882a593Smuzhiyun info = dev->mode_config.funcs->get_format_info(mode_cmd);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (!info)
351*4882a593Smuzhiyun info = drm_format_info(mode_cmd->pixel_format);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun return info;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun EXPORT_SYMBOL(drm_get_format_info);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /**
358*4882a593Smuzhiyun * drm_format_info_block_width - width in pixels of block.
359*4882a593Smuzhiyun * @info: pixel format info
360*4882a593Smuzhiyun * @plane: plane index
361*4882a593Smuzhiyun *
362*4882a593Smuzhiyun * Returns:
363*4882a593Smuzhiyun * The width in pixels of a block, depending on the plane index.
364*4882a593Smuzhiyun */
drm_format_info_block_width(const struct drm_format_info * info,int plane)365*4882a593Smuzhiyun unsigned int drm_format_info_block_width(const struct drm_format_info *info,
366*4882a593Smuzhiyun int plane)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun if (!info || plane < 0 || plane >= info->num_planes)
369*4882a593Smuzhiyun return 0;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (!info->block_w[plane])
372*4882a593Smuzhiyun return 1;
373*4882a593Smuzhiyun return info->block_w[plane];
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun EXPORT_SYMBOL(drm_format_info_block_width);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /**
378*4882a593Smuzhiyun * drm_format_info_block_height - height in pixels of a block
379*4882a593Smuzhiyun * @info: pixel format info
380*4882a593Smuzhiyun * @plane: plane index
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun * Returns:
383*4882a593Smuzhiyun * The height in pixels of a block, depending on the plane index.
384*4882a593Smuzhiyun */
drm_format_info_block_height(const struct drm_format_info * info,int plane)385*4882a593Smuzhiyun unsigned int drm_format_info_block_height(const struct drm_format_info *info,
386*4882a593Smuzhiyun int plane)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun if (!info || plane < 0 || plane >= info->num_planes)
389*4882a593Smuzhiyun return 0;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (!info->block_h[plane])
392*4882a593Smuzhiyun return 1;
393*4882a593Smuzhiyun return info->block_h[plane];
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun EXPORT_SYMBOL(drm_format_info_block_height);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun * drm_format_info_min_pitch - computes the minimum required pitch in bytes
399*4882a593Smuzhiyun * @info: pixel format info
400*4882a593Smuzhiyun * @plane: plane index
401*4882a593Smuzhiyun * @buffer_width: buffer width in pixels
402*4882a593Smuzhiyun *
403*4882a593Smuzhiyun * Returns:
404*4882a593Smuzhiyun * The minimum required pitch in bytes for a buffer by taking into consideration
405*4882a593Smuzhiyun * the pixel format information and the buffer width.
406*4882a593Smuzhiyun */
drm_format_info_min_pitch(const struct drm_format_info * info,int plane,unsigned int buffer_width)407*4882a593Smuzhiyun uint64_t drm_format_info_min_pitch(const struct drm_format_info *info,
408*4882a593Smuzhiyun int plane, unsigned int buffer_width)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun if (!info || plane < 0 || plane >= info->num_planes)
411*4882a593Smuzhiyun return 0;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun return DIV_ROUND_UP_ULL((u64)buffer_width * info->char_per_block[plane],
414*4882a593Smuzhiyun drm_format_info_block_width(info, plane) *
415*4882a593Smuzhiyun drm_format_info_block_height(info, plane));
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun EXPORT_SYMBOL(drm_format_info_min_pitch);
418