From 78c4a5fdcb33d30dfedd931e85522fee95559017 Mon Sep 17 00:00:00 2001
From: Naushir Patuck <naush@raspberrypi.com>
Date: Mon, 4 Aug 2025 09:10:54 +0100
Subject: [PATCH] drivers: media: pisp_be: Add minimal alinment to the format
 structure

Add a new minimal alignment field to the format structure. This minimal
alignment will be used if a stride has been provided by userland. If no
stride has been provided by userland (bytesperline == 0), the optimal
alignemnt will be used in the stride calculation.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
 .../platform/raspberrypi/pisp_be/pisp_be.c    |  9 +++-
 .../raspberrypi/pisp_be/pisp_be_formats.h     | 51 +++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)

--- a/drivers/media/platform/raspberrypi/pisp_be/pisp_be.c
+++ b/drivers/media/platform/raspberrypi/pisp_be/pisp_be.c
@@ -1074,9 +1074,16 @@ static void pispbe_set_plane_params(stru
 	for (unsigned int i = 0; i < nplanes; i++) {
 		struct v4l2_plane_pix_format *p = &f->fmt.pix_mp.plane_fmt[i];
 		unsigned int bpl, plane_size;
+		/*
+		 * If a stride has been provided, ensure it meets the minimal
+		 * alignment constraints. If not provided, use an optimal stride
+		 * alignment for efficiency.
+		 */
+		const unsigned int align =
+			p->bytesperline ? fmt->min_align : fmt->opt_align;
 
 		bpl = (f->fmt.pix_mp.width * fmt->bit_depth) >> 3;
-		bpl = ALIGN(max(p->bytesperline, bpl), fmt->opt_align);
+		bpl = ALIGN(max(p->bytesperline, bpl), align);
 
 		plane_size = bpl * f->fmt.pix_mp.height *
 		      (nplanes > 1 ? fmt->plane_factor[i] : total_plane_factor);
--- a/drivers/media/platform/raspberrypi/pisp_be/pisp_be_formats.h
+++ b/drivers/media/platform/raspberrypi/pisp_be/pisp_be_formats.h
@@ -17,6 +17,7 @@
 struct pisp_be_format {
 	unsigned int fourcc;
 	unsigned int opt_align;
+	unsigned int min_align;
 	unsigned int bit_depth;
 	/* 0P3 factor for plane sizing */
 	unsigned int plane_factor[PISPBE_MAX_PLANES];
@@ -59,6 +60,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_YUV420,
 		/* 128 alignment to ensure U/V planes are 64 byte aligned. */
 		.opt_align	    = 128,
+		.min_align	    = 32,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.25), P3(0.25) },
 		.num_planes	    = 1,
@@ -69,6 +71,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_YVU420,
 		/* 128 alignment to ensure U/V planes are 64 byte aligned. */
 		.opt_align	    = 128,
+		.min_align	    = 32,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.25), P3(0.25) },
 		.num_planes	    = 1,
@@ -78,6 +81,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_NV12,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5) },
 		.num_planes	    = 1,
@@ -87,6 +91,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_NV21,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5) },
 		.num_planes	    = 1,
@@ -96,6 +101,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YUYV,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 16,
 		.plane_factor	    = { P3(1) },
 		.num_planes	    = 1,
@@ -105,6 +111,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_UYVY,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 16,
 		.plane_factor	    = { P3(1) },
 		.num_planes	    = 1,
@@ -114,6 +121,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YVYU,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 16,
 		.plane_factor	    = { P3(1) },
 		.num_planes	    = 1,
@@ -123,6 +131,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_VYUY,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 16,
 		.plane_factor	    = { P3(1) },
 		.num_planes	    = 1,
@@ -133,6 +142,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_YUV422P,
 		/* 128 alignment to ensure U/V planes are 64 byte aligned. */
 		.opt_align	    = 128,
+		.min_align	    = 32,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5), P3(0.5) },
 		.num_planes	    = 1,
@@ -143,6 +153,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YUV420M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.25), P3(0.25) },
 		.num_planes	    = 3,
@@ -152,6 +163,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_NV12M,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5) },
 		.num_planes	    = 2,
@@ -161,6 +173,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_NV21M,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5) },
 		.num_planes	    = 2,
@@ -170,6 +183,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YVU420M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.25), P3(0.25) },
 		.num_planes	    = 3,
@@ -179,6 +193,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YUV422M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5), P3(0.5) },
 		.num_planes	    = 3,
@@ -188,6 +203,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YVU422M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(0.5), P3(0.5) },
 		.num_planes	    = 3,
@@ -197,6 +213,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YUV444M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(1), P3(1) },
 		.num_planes	    = 3,
@@ -206,6 +223,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_YVU444M,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 8,
 		.plane_factor	    = { P3(1), P3(1), P3(1) },
 		.num_planes	    = 3,
@@ -216,6 +234,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_RGB24,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 24,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -225,6 +244,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_BGR24,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.bit_depth	    = 24,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -234,6 +254,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_XBGR32,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 32,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -243,6 +264,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_RGBX32,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 32,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -252,6 +274,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_RGB48,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 48,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -261,6 +284,7 @@ static const struct pisp_be_format suppo
 	{
 		.fourcc		    = V4L2_PIX_FMT_BGR48,
 		.opt_align	    = 64,
+		.min_align	    = 16,
 		.bit_depth	    = 48,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
@@ -272,6 +296,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SRGGB8,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -281,6 +306,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SBGGR8,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -290,6 +316,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGRBG8,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -299,6 +326,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGBRG8,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -309,6 +337,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SRGGB16,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -318,6 +347,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SBGGR16,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -327,6 +357,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGRBG16,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -336,6 +367,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGBRG16,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -347,6 +379,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SRGGB10,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -356,6 +389,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SBGGR10,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -365,6 +399,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGRBG10,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -374,6 +409,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGBRG10,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -384,6 +420,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SRGGB12,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -393,6 +430,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SBGGR12,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -402,6 +440,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGRBG12,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -411,6 +450,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGBRG12,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -421,6 +461,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SRGGB14,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -430,6 +471,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SBGGR14,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -439,6 +481,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGRBG14,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -448,6 +491,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_SGBRG14,
 		.bit_depth	    = 16,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -458,6 +502,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_PISP_COMP1_BGGR,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -467,6 +512,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_PISP_COMP1_RGGB,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -476,6 +522,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_PISP_COMP1_GRBG,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -485,6 +532,7 @@ static const struct pisp_be_format suppo
 		.fourcc		    = V4L2_PIX_FMT_PISP_COMP1_GBRG,
 		.bit_depth	    = 8,
 		.opt_align	    = 32,
+		.min_align	    = 16,
 		.plane_factor	    = { P3(1.0) },
 		.num_planes	    = 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -495,6 +543,7 @@ static const struct pisp_be_format suppo
 		.fourcc		= V4L2_PIX_FMT_GREY,
 		.bit_depth	= 8,
 		.opt_align	= 32,
+		.min_align	= 16,
 		.num_planes	= 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
 		.colorspace_default = V4L2_COLORSPACE_RAW,
@@ -503,6 +552,7 @@ static const struct pisp_be_format suppo
 		.fourcc		= V4L2_PIX_FMT_Y16,
 		.bit_depth	= 16,
 		.opt_align	= 32,
+		.min_align	= 16,
 		.plane_factor	= { P3(1.0) },
 		.num_planes	= 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
@@ -512,6 +562,7 @@ static const struct pisp_be_format suppo
 		.fourcc		= V4L2_PIX_FMT_PISP_COMP1_MONO,
 		.bit_depth	= 8,
 		.opt_align	= 32,
+		.min_align	= 16,
 		.plane_factor	= { P3(1.0) },
 		.num_planes	= 1,
 		.colorspace_mask    = V4L2_COLORSPACE_MASK_RAW,
