[PATCH v3 02/31] media: v4l2-common: Add helper function v4l_get_required_align_by_bpp()

Frank Li posted 31 patches 5 months, 3 weeks ago
[PATCH v3 02/31] media: v4l2-common: Add helper function v4l_get_required_align_by_bpp()
Posted by Frank Li 5 months, 3 weeks ago
Add helper v4l_get_required_align_by_bpp() to help get width alignment
requirement. Basic replace below logic and enhance to any 2^[0..31] in
drivers/media/platform/nxp/imx-mipi-csis.c

mipi_csis_set_fmt(
{
        ...

        switch (csis_fmt->width % 8) {
        case 0:
                align = 0;
                break;
        case 4:
                align = 1;
                break;
        case 2:
        case 6:
                align = 2;
                break;
        default:
                /* 1, 3, 5, 7 */
                align = 3;
                break;
        }
	...
}

Signed-off-by: Frank Li <Frank.Li@nxp.com>
---
The same patch will be see at https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-2-4dfca4ed2f87@nxp.com/
dw csi2 patch also this
---
 include/media/v4l2-common.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 39dd0c78d70f7b935c2e10f9767646d2cedd3079..0a9da5e8daaddf26903d9ff4bed08b283dcd38b2 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -704,4 +704,34 @@ static inline bool v4l2_is_quant_valid(__u8 quantization)
 	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
 }
 
+/**
+ * v4l_get_required_align_by_bpp - get align requirement for
+ *	v4l_bound_align_image(). (bpp * width) % (1 << align) have to be 0.
+ *	given number bpp, get width's alignment requirement. For example,
+ *	if align is 3, means require bpp * width must be multiples of 8.
+ *	    bpp     return  width's requirememnt
+ *	    0       0       none
+ *	    1       3       8 (need 3 zero bits)
+ *	    2       2       4
+ *	    3       3       8
+ *	    4       1       2
+ *	    5       3       8
+ *	    6       2       4
+ *	    7       3       8
+ * @bpp: input bpp
+ * @align: expected alignment, 2^(align).
+ *
+ * return: required alignment.
+ */
+static inline u32 v4l_get_required_align_by_bpp(u32 bpp, u32 align)
+{
+	int pos;
+
+	if (bpp == 0)
+		return 0;
+
+	pos = ffs(bpp) - 1;
+	return pos > align ? 0 : align - pos;
+}
+
 #endif /* V4L2_COMMON_H_ */

-- 
2.34.1
Re: [PATCH v3 02/31] media: v4l2-common: Add helper function v4l_get_required_align_by_bpp()
Posted by Laurent Pinchart 3 months, 2 weeks ago
On Thu, Aug 21, 2025 at 04:15:37PM -0400, Frank Li wrote:
> Add helper v4l_get_required_align_by_bpp() to help get width alignment
> requirement. Basic replace below logic and enhance to any 2^[0..31] in
> drivers/media/platform/nxp/imx-mipi-csis.c
> 
> mipi_csis_set_fmt(
> {
>         ...
> 
>         switch (csis_fmt->width % 8) {
>         case 0:
>                 align = 0;
>                 break;
>         case 4:
>                 align = 1;
>                 break;
>         case 2:
>         case 6:
>                 align = 2;
>                 break;
>         default:
>                 /* 1, 3, 5, 7 */
>                 align = 3;
>                 break;
>         }
> 	...
> }
> 
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
> The same patch will be see at https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-2-4dfca4ed2f87@nxp.com/
> dw csi2 patch also this
> ---
>  include/media/v4l2-common.h | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> index 39dd0c78d70f7b935c2e10f9767646d2cedd3079..0a9da5e8daaddf26903d9ff4bed08b283dcd38b2 100644
> --- a/include/media/v4l2-common.h
> +++ b/include/media/v4l2-common.h
> @@ -704,4 +704,34 @@ static inline bool v4l2_is_quant_valid(__u8 quantization)
>  	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
>  }
>  
> +/**
> + * v4l_get_required_align_by_bpp - get align requirement for
> + *	v4l_bound_align_image(). (bpp * width) % (1 << align) have to be 0.
> + *	given number bpp, get width's alignment requirement. For example,
> + *	if align is 3, means require bpp * width must be multiples of 8.
> + *	    bpp     return  width's requirememnt
> + *	    0       0       none
> + *	    1       3       8 (need 3 zero bits)
> + *	    2       2       4
> + *	    3       3       8
> + *	    4       1       2
> + *	    5       3       8
> + *	    6       2       4
> + *	    7       3       8

This is supposed to be a brief one-line description. The rest of the
documentation goes after the argument.

Also, have checked the formatting of the generated documentation ?

> + * @bpp: input bpp

I have no idea if this is a bits per pixel or bytes per pixel value. I'm
actually not sure what the function is even supposed to do. It feels
it's really a ad-hoc helper, I would like to see it being used in
multiple drivers to see if it makes sense.

> + * @align: expected alignment, 2^(align).
> + *
> + * return: required alignment.
> + */
> +static inline u32 v4l_get_required_align_by_bpp(u32 bpp, u32 align)
> +{
> +	int pos;
> +
> +	if (bpp == 0)
> +		return 0;
> +
> +	pos = ffs(bpp) - 1;
> +	return pos > align ? 0 : align - pos;
> +}
> +
>  #endif /* V4L2_COMMON_H_ */
> 

-- 
Regards,

Laurent Pinchart
Re: [PATCH v3 02/31] media: v4l2-common: Add helper function v4l_get_required_align_by_bpp()
Posted by Frank Li 3 months, 2 weeks ago
On Mon, Oct 27, 2025 at 03:19:27AM +0200, Laurent Pinchart wrote:
> On Thu, Aug 21, 2025 at 04:15:37PM -0400, Frank Li wrote:
> > Add helper v4l_get_required_align_by_bpp() to help get width alignment
> > requirement. Basic replace below logic and enhance to any 2^[0..31] in
> > drivers/media/platform/nxp/imx-mipi-csis.c
> >
> > mipi_csis_set_fmt(
> > {
> >         ...
> >
> >         switch (csis_fmt->width % 8) {
> >         case 0:
> >                 align = 0;
> >                 break;
> >         case 4:
> >                 align = 1;
> >                 break;
> >         case 2:
> >         case 6:
> >                 align = 2;
> >                 break;
> >         default:
> >                 /* 1, 3, 5, 7 */
> >                 align = 3;
> >                 break;
> >         }
> > 	...
> > }
> >
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > ---
> > The same patch will be see at https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-2-4dfca4ed2f87@nxp.com/
> > dw csi2 patch also this
> > ---
> >  include/media/v4l2-common.h | 30 ++++++++++++++++++++++++++++++
> >  1 file changed, 30 insertions(+)
> >
> > diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> > index 39dd0c78d70f7b935c2e10f9767646d2cedd3079..0a9da5e8daaddf26903d9ff4bed08b283dcd38b2 100644
> > --- a/include/media/v4l2-common.h
> > +++ b/include/media/v4l2-common.h
> > @@ -704,4 +704,34 @@ static inline bool v4l2_is_quant_valid(__u8 quantization)
> >  	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
> >  }
> >
> > +/**
> > + * v4l_get_required_align_by_bpp - get align requirement for
> > + *	v4l_bound_align_image(). (bpp * width) % (1 << align) have to be 0.
> > + *	given number bpp, get width's alignment requirement. For example,
> > + *	if align is 3, means require bpp * width must be multiples of 8.
> > + *	    bpp     return  width's requirememnt
> > + *	    0       0       none
> > + *	    1       3       8 (need 3 zero bits)
> > + *	    2       2       4
> > + *	    3       3       8
> > + *	    4       1       2
> > + *	    5       3       8
> > + *	    6       2       4
> > + *	    7       3       8
>
> This is supposed to be a brief one-line description. The rest of the
> documentation goes after the argument.
>
> Also, have checked the formatting of the generated documentation ?
>
> > + * @bpp: input bpp
>
> I have no idea if this is a bits per pixel or bytes per pixel value. I'm
> actually not sure what the function is even supposed to do. It feels
> it's really a ad-hoc helper, I would like to see it being used in
> multiple drivers to see if it makes sense.

drivers/media/platform/nxp/imx-mipi-csis.c
imx_cpi_set_fmt() in
https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-3-4dfca4ed2f87@nxp.com/

I am not sure if userspace to pass down a old width to driver, like 1033.

Frank

>
> > + * @align: expected alignment, 2^(align).
> > + *
> > + * return: required alignment.
> > + */
> > +static inline u32 v4l_get_required_align_by_bpp(u32 bpp, u32 align)
> > +{
> > +	int pos;
> > +
> > +	if (bpp == 0)
> > +		return 0;
> > +
> > +	pos = ffs(bpp) - 1;
> > +	return pos > align ? 0 : align - pos;
> > +}
> > +
> >  #endif /* V4L2_COMMON_H_ */
> >
>
> --
> Regards,
>
> Laurent Pinchart
Re: [PATCH v3 02/31] media: v4l2-common: Add helper function v4l_get_required_align_by_bpp()
Posted by Frank Li 2 months, 3 weeks ago
On Mon, Oct 27, 2025 at 01:10:23PM -0400, Frank Li wrote:
> On Mon, Oct 27, 2025 at 03:19:27AM +0200, Laurent Pinchart wrote:
> > On Thu, Aug 21, 2025 at 04:15:37PM -0400, Frank Li wrote:
> > > Add helper v4l_get_required_align_by_bpp() to help get width alignment
> > > requirement. Basic replace below logic and enhance to any 2^[0..31] in
> > > drivers/media/platform/nxp/imx-mipi-csis.c
> > >
> > > mipi_csis_set_fmt(
> > > {
> > >         ...
> > >
> > >         switch (csis_fmt->width % 8) {
> > >         case 0:
> > >                 align = 0;
> > >                 break;
> > >         case 4:
> > >                 align = 1;
> > >                 break;
> > >         case 2:
> > >         case 6:
> > >                 align = 2;
> > >                 break;
> > >         default:
> > >                 /* 1, 3, 5, 7 */
> > >                 align = 3;
> > >                 break;
> > >         }
> > > 	...
> > > }
> > >
> > > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > > ---
> > > The same patch will be see at https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-2-4dfca4ed2f87@nxp.com/
> > > dw csi2 patch also this
> > > ---
> > >  include/media/v4l2-common.h | 30 ++++++++++++++++++++++++++++++
> > >  1 file changed, 30 insertions(+)
> > >
> > > diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> > > index 39dd0c78d70f7b935c2e10f9767646d2cedd3079..0a9da5e8daaddf26903d9ff4bed08b283dcd38b2 100644
> > > --- a/include/media/v4l2-common.h
> > > +++ b/include/media/v4l2-common.h
> > > @@ -704,4 +704,34 @@ static inline bool v4l2_is_quant_valid(__u8 quantization)
> > >  	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
> > >  }
> > >
> > > +/**
> > > + * v4l_get_required_align_by_bpp - get align requirement for
> > > + *	v4l_bound_align_image(). (bpp * width) % (1 << align) have to be 0.
> > > + *	given number bpp, get width's alignment requirement. For example,
> > > + *	if align is 3, means require bpp * width must be multiples of 8.
> > > + *	    bpp     return  width's requirememnt
> > > + *	    0       0       none
> > > + *	    1       3       8 (need 3 zero bits)
> > > + *	    2       2       4
> > > + *	    3       3       8
> > > + *	    4       1       2
> > > + *	    5       3       8
> > > + *	    6       2       4
> > > + *	    7       3       8
> >
> > This is supposed to be a brief one-line description. The rest of the
> > documentation goes after the argument.
> >
> > Also, have checked the formatting of the generated documentation ?
> >
> > > + * @bpp: input bpp
> >
> > I have no idea if this is a bits per pixel or bytes per pixel value. I'm
> > actually not sure what the function is even supposed to do. It feels
> > it's really a ad-hoc helper, I would like to see it being used in
> > multiple drivers to see if it makes sense.
>
> drivers/media/platform/nxp/imx-mipi-csis.c
> imx_cpi_set_fmt() in
> https://lore.kernel.org/imx/20250729-imx8qxp_pcam-v4-3-4dfca4ed2f87@nxp.com/
>
> I am not sure if userspace to pass down a old width to driver, like 1033.

Do you agree this change, I can post this patch + imx-mipi-csis.c as
sperate patches?

dwc csi2 need split more small serise to reduce review efforts.

Frank

>
> Frank
>
> >
> > > + * @align: expected alignment, 2^(align).
> > > + *
> > > + * return: required alignment.
> > > + */
> > > +static inline u32 v4l_get_required_align_by_bpp(u32 bpp, u32 align)
> > > +{
> > > +	int pos;
> > > +
> > > +	if (bpp == 0)
> > > +		return 0;
> > > +
> > > +	pos = ffs(bpp) - 1;
> > > +	return pos > align ? 0 : align - pos;
> > > +}
> > > +
> > >  #endif /* V4L2_COMMON_H_ */
> > >
> >
> > --
> > Regards,
> >
> > Laurent Pinchart