xref: /dflybsd-src/sys/dev/drm/drm_fourcc.c (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot  * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
31dedbd3bSFrançois Tigeot  *
41dedbd3bSFrançois Tigeot  * DRM core format related functions
51dedbd3bSFrançois Tigeot  *
61dedbd3bSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
71dedbd3bSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
81dedbd3bSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
91dedbd3bSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
101dedbd3bSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
111dedbd3bSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
121dedbd3bSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
131dedbd3bSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
141dedbd3bSFrançois Tigeot  * is" without express or implied warranty.
151dedbd3bSFrançois Tigeot  *
161dedbd3bSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
171dedbd3bSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
181dedbd3bSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
191dedbd3bSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
201dedbd3bSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
211dedbd3bSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
221dedbd3bSFrançois Tigeot  * OF THIS SOFTWARE.
231dedbd3bSFrançois Tigeot  */
241dedbd3bSFrançois Tigeot 
251dedbd3bSFrançois Tigeot #include <linux/bug.h>
261dedbd3bSFrançois Tigeot #include <linux/ctype.h>
271dedbd3bSFrançois Tigeot #include <linux/export.h>
281dedbd3bSFrançois Tigeot #include <linux/kernel.h>
291dedbd3bSFrançois Tigeot 
301dedbd3bSFrançois Tigeot #include <drm/drmP.h>
311dedbd3bSFrançois Tigeot #include <drm/drm_fourcc.h>
321dedbd3bSFrançois Tigeot 
printable_char(int c)331dedbd3bSFrançois Tigeot static char printable_char(int c)
341dedbd3bSFrançois Tigeot {
351dedbd3bSFrançois Tigeot 	return isascii(c) && isprint(c) ? c : '?';
361dedbd3bSFrançois Tigeot }
371dedbd3bSFrançois Tigeot 
381dedbd3bSFrançois Tigeot /**
391dedbd3bSFrançois Tigeot  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
401dedbd3bSFrançois Tigeot  * @bpp: bits per pixels
411dedbd3bSFrançois Tigeot  * @depth: bit depth per pixel
421dedbd3bSFrançois Tigeot  *
431dedbd3bSFrançois Tigeot  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
441dedbd3bSFrançois Tigeot  * Useful in fbdev emulation code, since that deals in those values.
451dedbd3bSFrançois Tigeot  */
drm_mode_legacy_fb_format(uint32_t bpp,uint32_t depth)461dedbd3bSFrançois Tigeot uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
471dedbd3bSFrançois Tigeot {
481dedbd3bSFrançois Tigeot 	uint32_t fmt;
491dedbd3bSFrançois Tigeot 
501dedbd3bSFrançois Tigeot 	switch (bpp) {
511dedbd3bSFrançois Tigeot 	case 8:
521dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_C8;
531dedbd3bSFrançois Tigeot 		break;
541dedbd3bSFrançois Tigeot 	case 16:
551dedbd3bSFrançois Tigeot 		if (depth == 15)
561dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB1555;
571dedbd3bSFrançois Tigeot 		else
581dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_RGB565;
591dedbd3bSFrançois Tigeot 		break;
601dedbd3bSFrançois Tigeot 	case 24:
611dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_RGB888;
621dedbd3bSFrançois Tigeot 		break;
631dedbd3bSFrançois Tigeot 	case 32:
641dedbd3bSFrançois Tigeot 		if (depth == 24)
651dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB8888;
661dedbd3bSFrançois Tigeot 		else if (depth == 30)
671dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB2101010;
681dedbd3bSFrançois Tigeot 		else
691dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_ARGB8888;
701dedbd3bSFrançois Tigeot 		break;
711dedbd3bSFrançois Tigeot 	default:
721dedbd3bSFrançois Tigeot 		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
731dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_XRGB8888;
741dedbd3bSFrançois Tigeot 		break;
751dedbd3bSFrançois Tigeot 	}
761dedbd3bSFrançois Tigeot 
771dedbd3bSFrançois Tigeot 	return fmt;
781dedbd3bSFrançois Tigeot }
791dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_mode_legacy_fb_format);
801dedbd3bSFrançois Tigeot 
811dedbd3bSFrançois Tigeot /**
824be47400SFrançois Tigeot  * drm_get_format_name - fill a string with a drm fourcc format's name
831dedbd3bSFrançois Tigeot  * @format: format to compute name of
844be47400SFrançois Tigeot  * @buf: caller-supplied buffer
851dedbd3bSFrançois Tigeot  */
drm_get_format_name(uint32_t format,struct drm_format_name_buf * buf)864be47400SFrançois Tigeot const char *drm_get_format_name(uint32_t format, struct drm_format_name_buf *buf)
871dedbd3bSFrançois Tigeot {
884be47400SFrançois Tigeot 	snprintf(buf->str, sizeof(buf->str),
891dedbd3bSFrançois Tigeot 		 "%c%c%c%c %s-endian (0x%08x)",
901dedbd3bSFrançois Tigeot 		 printable_char(format & 0xff),
911dedbd3bSFrançois Tigeot 		 printable_char((format >> 8) & 0xff),
921dedbd3bSFrançois Tigeot 		 printable_char((format >> 16) & 0xff),
931dedbd3bSFrançois Tigeot 		 printable_char((format >> 24) & 0x7f),
941dedbd3bSFrançois Tigeot 		 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
951dedbd3bSFrançois Tigeot 		 format);
961dedbd3bSFrançois Tigeot 
974be47400SFrançois Tigeot 	return buf->str;
981dedbd3bSFrançois Tigeot }
991dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_get_format_name);
1001dedbd3bSFrançois Tigeot 
1014be47400SFrançois Tigeot /*
1024be47400SFrançois Tigeot  * Internal function to query information for a given format. See
1034be47400SFrançois Tigeot  * drm_format_info() for the public API.
1041dedbd3bSFrançois Tigeot  */
__drm_format_info(u32 format)1054be47400SFrançois Tigeot const struct drm_format_info *__drm_format_info(u32 format)
1061dedbd3bSFrançois Tigeot {
1074be47400SFrançois Tigeot 	static const struct drm_format_info formats[] = {
1084be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_C8,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
1094be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB332,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
1104be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR233,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
1114be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1124be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1134be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1144be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1154be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1164be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1174be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1184be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1194be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1204be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1214be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1224be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1234be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1244be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1254be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1264be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1274be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB565,		.depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1284be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR565,		.depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
1294be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB888,		.depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
1304be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR888,		.depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
1314be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1324be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1334be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1344be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
135*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_RGB565_A8,	.depth = 24, .num_planes = 2, .cpp = { 2, 1, 0 }, .hsub = 1, .vsub = 1 },
136*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_BGR565_A8,	.depth = 24, .num_planes = 2, .cpp = { 2, 1, 0 }, .hsub = 1, .vsub = 1 },
1374be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1384be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1394be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1404be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1414be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1424be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1434be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1444be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1454be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1464be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1474be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1484be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
149*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_RGB888_A8,	.depth = 32, .num_planes = 2, .cpp = { 3, 1, 0 }, .hsub = 1, .vsub = 1 },
150*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_BGR888_A8,	.depth = 32, .num_planes = 2, .cpp = { 3, 1, 0 }, .hsub = 1, .vsub = 1 },
151*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB8888_A8,	.depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1 },
152*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR8888_A8,	.depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1 },
153*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX8888_A8,	.depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1 },
154*a85cb24fSFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX8888_A8,	.depth = 32, .num_planes = 2, .cpp = { 4, 1, 0 }, .hsub = 1, .vsub = 1 },
1554be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV410,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4 },
1564be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU410,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4 },
1574be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV411,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1 },
1584be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU411,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1 },
1594be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV420,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2 },
1604be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU420,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2 },
1614be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV422,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1 },
1624be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU422,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1 },
1634be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV444,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1 },
1644be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU444,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1 },
1654be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV12,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2 },
1664be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV21,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2 },
1674be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV16,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1 },
1684be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV61,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1 },
1694be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV24,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1 },
1704be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV42,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1 },
1714be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUYV,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
1724be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVYU,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
1734be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_UYVY,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
1744be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_VYUY,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
1754be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_AYUV,		.depth = 0,  .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
1764be47400SFrançois Tigeot 	};
1771dedbd3bSFrançois Tigeot 
1784be47400SFrançois Tigeot 	unsigned int i;
1794be47400SFrançois Tigeot 
1804be47400SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
1814be47400SFrançois Tigeot 		if (formats[i].format == format)
1824be47400SFrançois Tigeot 			return &formats[i];
1831dedbd3bSFrançois Tigeot 	}
1844be47400SFrançois Tigeot 
1854be47400SFrançois Tigeot 	return NULL;
1861dedbd3bSFrançois Tigeot }
1874be47400SFrançois Tigeot 
1884be47400SFrançois Tigeot /**
1894be47400SFrançois Tigeot  * drm_format_info - query information for a given format
1904be47400SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
1914be47400SFrançois Tigeot  *
1924be47400SFrançois Tigeot  * The caller should only pass a supported pixel format to this function.
1934be47400SFrançois Tigeot  * Unsupported pixel formats will generate a warning in the kernel log.
1944be47400SFrançois Tigeot  *
1954be47400SFrançois Tigeot  * Returns:
1964be47400SFrançois Tigeot  * The instance of struct drm_format_info that describes the pixel format, or
1974be47400SFrançois Tigeot  * NULL if the format is unsupported.
1984be47400SFrançois Tigeot  */
drm_format_info(u32 format)1994be47400SFrançois Tigeot const struct drm_format_info *drm_format_info(u32 format)
2004be47400SFrançois Tigeot {
2014be47400SFrançois Tigeot 	const struct drm_format_info *info;
2024be47400SFrançois Tigeot 
2034be47400SFrançois Tigeot 	info = __drm_format_info(format);
2044be47400SFrançois Tigeot 	WARN_ON(!info);
2054be47400SFrançois Tigeot 	return info;
2064be47400SFrançois Tigeot }
2074be47400SFrançois Tigeot EXPORT_SYMBOL(drm_format_info);
2081dedbd3bSFrançois Tigeot 
2091dedbd3bSFrançois Tigeot /**
210*a85cb24fSFrançois Tigeot  * drm_get_format_info - query information for a given framebuffer configuration
211*a85cb24fSFrançois Tigeot  * @dev: DRM device
212*a85cb24fSFrançois Tigeot  * @mode_cmd: metadata from the userspace fb creation request
213*a85cb24fSFrançois Tigeot  *
214*a85cb24fSFrançois Tigeot  * Returns:
215*a85cb24fSFrançois Tigeot  * The instance of struct drm_format_info that describes the pixel format, or
216*a85cb24fSFrançois Tigeot  * NULL if the format is unsupported.
217*a85cb24fSFrançois Tigeot  */
218*a85cb24fSFrançois Tigeot const struct drm_format_info *
drm_get_format_info(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd)219*a85cb24fSFrançois Tigeot drm_get_format_info(struct drm_device *dev,
220*a85cb24fSFrançois Tigeot 		    const struct drm_mode_fb_cmd2 *mode_cmd)
221*a85cb24fSFrançois Tigeot {
222*a85cb24fSFrançois Tigeot 	const struct drm_format_info *info = NULL;
223*a85cb24fSFrançois Tigeot 
224*a85cb24fSFrançois Tigeot 	if (dev->mode_config.funcs->get_format_info)
225*a85cb24fSFrançois Tigeot 		info = dev->mode_config.funcs->get_format_info(mode_cmd);
226*a85cb24fSFrançois Tigeot 
227*a85cb24fSFrançois Tigeot 	if (!info)
228*a85cb24fSFrançois Tigeot 		info = drm_format_info(mode_cmd->pixel_format);
229*a85cb24fSFrançois Tigeot 
230*a85cb24fSFrançois Tigeot 	return info;
231*a85cb24fSFrançois Tigeot }
232*a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_get_format_info);
233*a85cb24fSFrançois Tigeot 
234*a85cb24fSFrançois Tigeot /**
2351dedbd3bSFrançois Tigeot  * drm_format_num_planes - get the number of planes for format
2361dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2371dedbd3bSFrançois Tigeot  *
2381dedbd3bSFrançois Tigeot  * Returns:
2391dedbd3bSFrançois Tigeot  * The number of planes used by the specified pixel format.
2401dedbd3bSFrançois Tigeot  */
drm_format_num_planes(uint32_t format)2411dedbd3bSFrançois Tigeot int drm_format_num_planes(uint32_t format)
2421dedbd3bSFrançois Tigeot {
2434be47400SFrançois Tigeot 	const struct drm_format_info *info;
2444be47400SFrançois Tigeot 
2454be47400SFrançois Tigeot 	info = drm_format_info(format);
2464be47400SFrançois Tigeot 	return info ? info->num_planes : 1;
2471dedbd3bSFrançois Tigeot }
2481dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_num_planes);
2491dedbd3bSFrançois Tigeot 
2501dedbd3bSFrançois Tigeot /**
2511dedbd3bSFrançois Tigeot  * drm_format_plane_cpp - determine the bytes per pixel value
2521dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2531dedbd3bSFrançois Tigeot  * @plane: plane index
2541dedbd3bSFrançois Tigeot  *
2551dedbd3bSFrançois Tigeot  * Returns:
2561dedbd3bSFrançois Tigeot  * The bytes per pixel value for the specified plane.
2571dedbd3bSFrançois Tigeot  */
drm_format_plane_cpp(uint32_t format,int plane)2581dedbd3bSFrançois Tigeot int drm_format_plane_cpp(uint32_t format, int plane)
2591dedbd3bSFrançois Tigeot {
2604be47400SFrançois Tigeot 	const struct drm_format_info *info;
2611dedbd3bSFrançois Tigeot 
2624be47400SFrançois Tigeot 	info = drm_format_info(format);
2634be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
2641dedbd3bSFrançois Tigeot 		return 0;
2651dedbd3bSFrançois Tigeot 
2664be47400SFrançois Tigeot 	return info->cpp[plane];
2671dedbd3bSFrançois Tigeot }
2681dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_cpp);
2691dedbd3bSFrançois Tigeot 
2701dedbd3bSFrançois Tigeot /**
2711dedbd3bSFrançois Tigeot  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
2721dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2731dedbd3bSFrançois Tigeot  *
2741dedbd3bSFrançois Tigeot  * Returns:
2751dedbd3bSFrançois Tigeot  * The horizontal chroma subsampling factor for the
2761dedbd3bSFrançois Tigeot  * specified pixel format.
2771dedbd3bSFrançois Tigeot  */
drm_format_horz_chroma_subsampling(uint32_t format)2781dedbd3bSFrançois Tigeot int drm_format_horz_chroma_subsampling(uint32_t format)
2791dedbd3bSFrançois Tigeot {
2804be47400SFrançois Tigeot 	const struct drm_format_info *info;
2814be47400SFrançois Tigeot 
2824be47400SFrançois Tigeot 	info = drm_format_info(format);
2834be47400SFrançois Tigeot 	return info ? info->hsub : 1;
2841dedbd3bSFrançois Tigeot }
2851dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
2861dedbd3bSFrançois Tigeot 
2871dedbd3bSFrançois Tigeot /**
2881dedbd3bSFrançois Tigeot  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
2891dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2901dedbd3bSFrançois Tigeot  *
2911dedbd3bSFrançois Tigeot  * Returns:
2921dedbd3bSFrançois Tigeot  * The vertical chroma subsampling factor for the
2931dedbd3bSFrançois Tigeot  * specified pixel format.
2941dedbd3bSFrançois Tigeot  */
drm_format_vert_chroma_subsampling(uint32_t format)2951dedbd3bSFrançois Tigeot int drm_format_vert_chroma_subsampling(uint32_t format)
2961dedbd3bSFrançois Tigeot {
2974be47400SFrançois Tigeot 	const struct drm_format_info *info;
2984be47400SFrançois Tigeot 
2994be47400SFrançois Tigeot 	info = drm_format_info(format);
3004be47400SFrançois Tigeot 	return info ? info->vsub : 1;
3011dedbd3bSFrançois Tigeot }
3021dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3031dedbd3bSFrançois Tigeot 
3041dedbd3bSFrançois Tigeot /**
3051dedbd3bSFrançois Tigeot  * drm_format_plane_width - width of the plane given the first plane
3061dedbd3bSFrançois Tigeot  * @width: width of the first plane
3071dedbd3bSFrançois Tigeot  * @format: pixel format
3081dedbd3bSFrançois Tigeot  * @plane: plane index
3091dedbd3bSFrançois Tigeot  *
3101dedbd3bSFrançois Tigeot  * Returns:
3111dedbd3bSFrançois Tigeot  * The width of @plane, given that the width of the first plane is @width.
3121dedbd3bSFrançois Tigeot  */
drm_format_plane_width(int width,uint32_t format,int plane)3131dedbd3bSFrançois Tigeot int drm_format_plane_width(int width, uint32_t format, int plane)
3141dedbd3bSFrançois Tigeot {
3154be47400SFrançois Tigeot 	const struct drm_format_info *info;
3164be47400SFrançois Tigeot 
3174be47400SFrançois Tigeot 	info = drm_format_info(format);
3184be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
3191dedbd3bSFrançois Tigeot 		return 0;
3201dedbd3bSFrançois Tigeot 
3211dedbd3bSFrançois Tigeot 	if (plane == 0)
3221dedbd3bSFrançois Tigeot 		return width;
3231dedbd3bSFrançois Tigeot 
3244be47400SFrançois Tigeot 	return width / info->hsub;
3251dedbd3bSFrançois Tigeot }
3261dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_width);
3271dedbd3bSFrançois Tigeot 
3281dedbd3bSFrançois Tigeot /**
3291dedbd3bSFrançois Tigeot  * drm_format_plane_height - height of the plane given the first plane
3301dedbd3bSFrançois Tigeot  * @height: height of the first plane
3311dedbd3bSFrançois Tigeot  * @format: pixel format
3321dedbd3bSFrançois Tigeot  * @plane: plane index
3331dedbd3bSFrançois Tigeot  *
3341dedbd3bSFrançois Tigeot  * Returns:
3351dedbd3bSFrançois Tigeot  * The height of @plane, given that the height of the first plane is @height.
3361dedbd3bSFrançois Tigeot  */
drm_format_plane_height(int height,uint32_t format,int plane)3371dedbd3bSFrançois Tigeot int drm_format_plane_height(int height, uint32_t format, int plane)
3381dedbd3bSFrançois Tigeot {
3394be47400SFrançois Tigeot 	const struct drm_format_info *info;
3404be47400SFrançois Tigeot 
3414be47400SFrançois Tigeot 	info = drm_format_info(format);
3424be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
3431dedbd3bSFrançois Tigeot 		return 0;
3441dedbd3bSFrançois Tigeot 
3451dedbd3bSFrançois Tigeot 	if (plane == 0)
3461dedbd3bSFrançois Tigeot 		return height;
3471dedbd3bSFrançois Tigeot 
3484be47400SFrançois Tigeot 	return height / info->vsub;
3491dedbd3bSFrançois Tigeot }
3501dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_height);
351