The function that lookup or enumerate formats based on a media bus code or a pixel format share the same backend. The backend code has become too complex due to the need to support both types of formats. Decouple the two categories of functions by inlining the backend code and simplifying it. Signed-off-by: Laurent Pinchart <laurent.pinchart@xxxxxxxxxxxxxxxx> --- drivers/staging/media/imx/imx-media-utils.c | 116 +++++++++++--------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/drivers/staging/media/imx/imx-media-utils.c b/drivers/staging/media/imx/imx-media-utils.c index c22e222866e8..ff2202e95a48 100644 --- a/drivers/staging/media/imx/imx-media-utils.c +++ b/drivers/staging/media/imx/imx-media-utils.c @@ -193,10 +193,8 @@ static const struct imx_media_pixfmt pixel_formats[] = { }, }; -static const -struct imx_media_pixfmt *find_format(u32 fourcc, - u32 code, - enum codespace_sel cs_sel) +const struct imx_media_pixfmt * +imx_media_find_pixel_format(u32 fourcc, enum codespace_sel cs_sel) { bool allow_bayer = cs_sel & CS_SEL_BAYER; unsigned int i; @@ -206,33 +204,53 @@ struct imx_media_pixfmt *find_format(u32 fourcc, for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { const struct imx_media_pixfmt *fmt = &pixel_formats[i]; enum codespace_sel fmt_cs_sel; - unsigned int j; fmt_cs_sel = fmt->cs == IPUV3_COLORSPACE_YUV ? CS_SEL_YUV : CS_SEL_RGB; - if ((cs_sel != CS_SEL_ANY && fmt_cs_sel != cs_sel) || - (!fourcc && !fmt->codes) || - (!allow_bayer && fmt->bayer)) + if (!(cs_sel & fmt_cs_sel) || (!allow_bayer && fmt->bayer)) continue; - if (fourcc && fmt->fourcc == fourcc) + if (fmt->fourcc == fourcc) return fmt; + } + + return NULL; +} +EXPORT_SYMBOL_GPL(imx_media_find_pixel_format); + +int imx_media_enum_pixel_formats(u32 *fourcc, u32 index, + enum codespace_sel cs_sel) +{ + bool allow_bayer = cs_sel & CS_SEL_BAYER; + unsigned int i; + + cs_sel &= CS_SEL_YUV | CS_SEL_RGB; + + for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { + const struct imx_media_pixfmt *fmt = &pixel_formats[i]; + enum codespace_sel fmt_cs_sel; + + fmt_cs_sel = fmt->cs == IPUV3_COLORSPACE_YUV + ? CS_SEL_YUV : CS_SEL_RGB; - if (!code) + if (!(cs_sel & fmt_cs_sel) || (!allow_bayer && fmt->bayer)) continue; - for (j = 0; fmt->codes[j]; j++) { - if (code == fmt->codes[j]) - return fmt; + if (index == 0) { + *fourcc = fmt->fourcc; + return 0; } + + index--; } - return NULL; + return -EINVAL; } +EXPORT_SYMBOL_GPL(imx_media_enum_pixel_formats); -static int enum_formats(u32 *fourcc, u32 *code, u32 index, - enum codespace_sel cs_sel) +const struct imx_media_pixfmt * +imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel) { bool allow_bayer = cs_sel & CS_SEL_BAYER; unsigned int i; @@ -244,23 +262,45 @@ static int enum_formats(u32 *fourcc, u32 *code, u32 index, enum codespace_sel fmt_cs_sel; unsigned int j; + if (!fmt->codes) + continue; + fmt_cs_sel = fmt->cs == IPUV3_COLORSPACE_YUV ? CS_SEL_YUV : CS_SEL_RGB; - if ((cs_sel != CS_SEL_ANY && fmt_cs_sel != cs_sel) || - (!fourcc && !fmt->codes) || - (!allow_bayer && fmt->bayer)) + if (!(cs_sel & fmt_cs_sel) || (!allow_bayer && fmt->bayer)) continue; - if (fourcc && index == 0) { - *fourcc = fmt->fourcc; - return 0; + for (j = 0; fmt->codes[j]; j++) { + if (code == fmt->codes[j]) + return fmt; } + } - if (!code) { - index--; + return NULL; +} +EXPORT_SYMBOL_GPL(imx_media_find_mbus_format); + +int imx_media_enum_mbus_formats(u32 *code, u32 index, enum codespace_sel cs_sel) +{ + bool allow_bayer = cs_sel & CS_SEL_BAYER; + unsigned int i; + + cs_sel &= CS_SEL_YUV | CS_SEL_RGB; + + for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { + const struct imx_media_pixfmt *fmt = &pixel_formats[i]; + enum codespace_sel fmt_cs_sel; + unsigned int j; + + if (!fmt->codes) + continue; + + fmt_cs_sel = fmt->cs == IPUV3_COLORSPACE_YUV + ? CS_SEL_YUV : CS_SEL_RGB; + + if (!(cs_sel & fmt_cs_sel) || (!allow_bayer && fmt->bayer)) continue; - } for (j = 0; fmt->codes[j]; j++) { if (index == 0) { @@ -274,32 +314,6 @@ static int enum_formats(u32 *fourcc, u32 *code, u32 index, return -EINVAL; } - -const struct imx_media_pixfmt * -imx_media_find_pixel_format(u32 fourcc, enum codespace_sel cs_sel) -{ - return find_format(fourcc, 0, cs_sel); -} -EXPORT_SYMBOL_GPL(imx_media_find_pixel_format); - -int imx_media_enum_pixel_formats(u32 *fourcc, u32 index, - enum codespace_sel cs_sel) -{ - return enum_formats(fourcc, NULL, index, cs_sel); -} -EXPORT_SYMBOL_GPL(imx_media_enum_pixel_formats); - -const struct imx_media_pixfmt * -imx_media_find_mbus_format(u32 code, enum codespace_sel cs_sel) -{ - return find_format(0, code, cs_sel); -} -EXPORT_SYMBOL_GPL(imx_media_find_mbus_format); - -int imx_media_enum_mbus_formats(u32 *code, u32 index, enum codespace_sel cs_sel) -{ - return enum_formats(NULL, code, index, cs_sel); -} EXPORT_SYMBOL_GPL(imx_media_enum_mbus_formats); /* ----------------------------------------------------------------------------- -- Regards, Laurent Pinchart