linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 00/11] Add support for mt2701 JPEG ENC support
@ 2020-03-03 12:34 Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 01/11] media: platform: Improve subscribe event flow for bug fixing Xia Jiang
                   ` (10 more replies)
  0 siblings, 11 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream

This patchset add support for mt2701 JPEG ENC support.  
                                                        
This is the compliance test result for jpeg dec and enc.

The JPEG dec log:                                           
------------------------------------------------------------
v4l2-compliance -d /dev/video0
v4l2-compliance SHA: af33cc5ef00177edbb45d466faf9061197767f2b, 32 bits

Compliance test for mtk-jpeg device /dev/video0:

Driver Info:
        Driver name      : mtk-jpeg
        Card type        : mtk-jpeg decoder
        Bus info         : platform:15004000.jpegdec
        Driver version   : 5.5.0
        Capabilities     : 0x84204000
                Video Memory-to-Memory Multiplanar
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps      : 0x04204000
                Video Memory-to-Memory Multiplanar
                Streaming
                Extended Pix Format
        Detected JPEG Decoder

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second /dev/video0 open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: OK
        test for unlimited opens: OK

Debug ioctls:
        test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
        test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
        test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
        test VIDIOC_ENUMAUDIO: OK (Not Supported)
        test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDIO: OK (Not Supported)
        Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
        test VIDIOC_G/S_MODULATOR: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_ENUMAUDOUT: OK (Not Supported)
        test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDOUT: OK (Not Supported)
        Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
        test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
        test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
        test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
        test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
        test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
        test VIDIOC_QUERYCTRL: OK
        test VIDIOC_G/S_CTRL: OK
        test VIDIOC_G/S/TRY_EXT_CTRLS: OK
        test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported)
        test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
        Standard Controls: 0 Private Controls: 0

Format ioctls:
        test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
        test VIDIOC_G/S_PARM: OK (Not Supported)
        test VIDIOC_G_FBUF: OK (Not Supported)
        test VIDIOC_G_FMT: OK
        test VIDIOC_TRY_FMT: OK
        test VIDIOC_S_FMT: OK
        test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
        test Cropping: OK (Not Supported)
        test Composing: OK
        test Scaling: OK

Codec ioctls:
        test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
        test VIDIOC_G_ENC_INDEX: OK (Not Supported)
        test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls:
        test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
        test VIDIOC_EXPBUF: OK
        test Requests: OK (Not Supported)

Total for mtk-jpeg device /dev/video0: 44, Succeeded: 44, Failed: 0, Warnings: 0
------------------------------------------------------------

The JPEG enc log:

------------------------------------------------------------
v4l2-compliance -d /dev/video1 
v4l2-compliance SHA: af33cc5ef00177edbb45d466faf9061197767f2b, 32 bits

Compliance test for mtk-jpeg device /dev/video1:

Driver Info:
        Driver name      : mtk-jpeg
        Card type        : mtk-jpeg encoder
        Bus info         : platform:1500a000.jpegenc
        Driver version   : 5.5.0
        Capabilities     : 0x84204000
                Video Memory-to-Memory Multiplanar
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps      : 0x04204000
                Video Memory-to-Memory Multiplanar
                Streaming
                Extended Pix Format
        Detected JPEG Encoder

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second /dev/video1 open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: OK
        test for unlimited opens: OK

Debug ioctls:
        test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
        test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
        test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
        test VIDIOC_ENUMAUDIO: OK (Not Supported)
        test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDIO: OK (Not Supported)
        Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
        test VIDIOC_G/S_MODULATOR: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_ENUMAUDOUT: OK (Not Supported)
        test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDOUT: OK (Not Supported)
        Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
        test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
        test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
        test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
        test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
        test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
        test VIDIOC_QUERYCTRL: OK
        test VIDIOC_G/S_CTRL: OK
        test VIDIOC_G/S/TRY_EXT_CTRLS: OK
        test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK
        test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
        Standard Controls: 4 Private Controls: 0

Format ioctls:
        test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
        test VIDIOC_G/S_PARM: OK (Not Supported)
        test VIDIOC_G_FBUF: OK (Not Supported)
        test VIDIOC_G_FMT: OK
        test VIDIOC_TRY_FMT: OK
        test VIDIOC_S_FMT: OK
        test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
        test Cropping: OK
        test Composing: OK (Not Supported)
        test Scaling: OK (Not Supported)

Codec ioctls:
        test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
        test VIDIOC_G_ENC_INDEX: OK (Not Supported)
        test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls:
        test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
        test VIDIOC_EXPBUF: OK
        test Requests: OK (Not Supported)

Total for mtk-jpeg device /dev/video1: 44, Succeeded: 44, Failed: 0, Warnings: 0
------------------------------------------------------------

Change compared to v6:
-split unrelate changes into its own patch for the fixing bug patch.
-reverse spin lock and unlock operation in device run function.

Xia Jiang (11):
  media: platform: Improve subscribe event flow for bug fixing
  media: platform: Improve queue set up flow for bug fixing
  media: platform: Improve s_selection flow for bug fixing
  media: platform: Use kernel native functions for improving code
    quality
  media: platform: Change case for improving code quality
  media: platform: Change MTK_JPEG_COMP_MAX macro definition location
  media: platform: Delete redundant code for improving code quality
  media: dt-bindings: Add jpeg enc device tree node document
  arm: dts: Add jpeg enc device tree node
  media: platform: Rename jpeg dec file name
  media: platform: Add jpeg dec/enc feature

 .../bindings/media/mediatek-jpeg-encoder.txt  |  37 +
 arch/arm/boot/dts/mt2701.dtsi                 |  13 +
 drivers/media/platform/mtk-jpeg/Makefile      |   5 +-
 .../media/platform/mtk-jpeg/mtk_jpeg_core.c   | 757 +++++++++++++-----
 .../media/platform/mtk-jpeg/mtk_jpeg_core.h   |  50 +-
 .../{mtk_jpeg_hw.c => mtk_jpeg_dec_hw.c}      |  10 +-
 .../{mtk_jpeg_hw.h => mtk_jpeg_dec_hw.h}      |  14 +-
 ...{mtk_jpeg_parse.c => mtk_jpeg_dec_parse.c} |   2 +-
 ...{mtk_jpeg_parse.h => mtk_jpeg_dec_parse.h} |   2 +-
 .../{mtk_jpeg_reg.h => mtk_jpeg_dec_reg.h}    |  19 +-
 .../media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c | 273 +++++++
 .../media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h |  86 ++
 .../platform/mtk-jpeg/mtk_jpeg_enc_reg.h      |  78 ++
 13 files changed, 1106 insertions(+), 240 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/media/mediatek-jpeg-encoder.txt
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_hw.c => mtk_jpeg_dec_hw.c} (98%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_hw.h => mtk_jpeg_dec_hw.h} (89%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_parse.c => mtk_jpeg_dec_parse.c} (98%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_parse.h => mtk_jpeg_dec_parse.h} (92%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_reg.h => mtk_jpeg_dec_reg.h} (77%)
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h

-- 
2.18.0


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v7 01/11] media: platform: Improve subscribe event flow for bug fixing
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 02/11] media: platform: Improve queue set up " Xia Jiang
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Let v4l2_ctrl_subscribe_event() do the job for other types except
V4L2_EVENT_SOURCE_CHANGE.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index ee802fc3bcdf..cde59275d49c 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -446,9 +446,9 @@ static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
 	switch (sub->type) {
 	case V4L2_EVENT_SOURCE_CHANGE:
 		return v4l2_src_change_event_subscribe(fh, sub);
-	default:
-		return -EINVAL;
 	}
+
+	return v4l2_ctrl_subscribe_event(fh, sub);
 }
 
 static int mtk_jpeg_g_selection(struct file *file, void *priv,
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 02/11] media: platform: Improve queue set up flow for bug fixing
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 01/11] media: platform: Improve subscribe event flow for bug fixing Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 03/11] media: platform: Improve s_selection " Xia Jiang
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Add checking created buffer size follow in mtk_jpeg_queue_setup().

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index cde59275d49c..da0dae4b0fc9 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -571,6 +571,13 @@ static int mtk_jpeg_queue_setup(struct vb2_queue *q,
 	if (!q_data)
 		return -EINVAL;
 
+	if (*num_planes) {
+		for (i = 0; i < *num_planes; i++)
+			if (sizes[i] < q_data->sizeimage[i])
+				return -EINVAL;
+		return 0;
+	}
+
 	*num_planes = q_data->fmt->colplanes;
 	for (i = 0; i < q_data->fmt->colplanes; i++) {
 		sizes[i] = q_data->sizeimage[i];
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 03/11] media: platform: Improve s_selection flow for bug fixing
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 01/11] media: platform: Improve subscribe event flow for bug fixing Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 02/11] media: platform: Improve queue set up " Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-11 11:02   ` Tomasz Figa
  2020-03-03 12:34 ` [PATCH v7 04/11] media: platform: Use kernel native functions for improving code quality Xia Jiang
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Get correct compose value in mtk_jpeg_s_selection function.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index da0dae4b0fc9..fb2c8d026580 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -492,8 +492,8 @@ static int mtk_jpeg_s_selection(struct file *file, void *priv,
 	case V4L2_SEL_TGT_COMPOSE:
 		s->r.left = 0;
 		s->r.top = 0;
-		s->r.width = ctx->out_q.w;
-		s->r.height = ctx->out_q.h;
+		ctx->out_q.w = s->r.width;
+		ctx->out_q.h = s->r.height;
 		break;
 	default:
 		return -EINVAL;
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 04/11] media: platform: Use kernel native functions for improving code quality
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (2 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 03/11] media: platform: Improve s_selection " Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 05/11] media: platform: Change case " Xia Jiang
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Use clamp() to replace mtk_jpeg_bound_align_image() and round() to
replace mtk_jpeg_align().

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 .../media/platform/mtk-jpeg/mtk_jpeg_core.c   | 41 +++++--------------
 .../media/platform/mtk-jpeg/mtk_jpeg_core.h   |  8 ++--
 drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c |  8 ++--
 drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h |  5 ---
 4 files changed, 19 insertions(+), 43 deletions(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index fb2c8d026580..4e9ff1f9732e 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -157,25 +157,6 @@ static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
 	return NULL;
 }
 
-static void mtk_jpeg_bound_align_image(u32 *w, unsigned int wmin,
-				       unsigned int wmax, unsigned int walign,
-				       u32 *h, unsigned int hmin,
-				       unsigned int hmax, unsigned int halign)
-{
-	int width, height, w_step, h_step;
-
-	width = *w;
-	height = *h;
-	w_step = 1 << walign;
-	h_step = 1 << halign;
-
-	v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
-	if (*w < width && (*w + w_step) <= wmax)
-		*w += w_step;
-	if (*h < height && (*h + h_step) <= hmax)
-		*h += h_step;
-}
-
 static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
 				       struct v4l2_format *f)
 {
@@ -218,25 +199,25 @@ static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 	if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
 
-		mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
-					   MTK_JPEG_MAX_WIDTH, 0,
-					   &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
-					   MTK_JPEG_MAX_HEIGHT, 0);
+		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
+				       MTK_JPEG_MAX_HEIGHT);
+		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
+				      MTK_JPEG_MAX_WIDTH);
 
 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
 		pfmt->bytesperline = 0;
 		/* Source size must be aligned to 128 */
-		pfmt->sizeimage = mtk_jpeg_align(pfmt->sizeimage, 128);
+		pfmt->sizeimage = round_up(pfmt->sizeimage, 128);
 		if (pfmt->sizeimage == 0)
 			pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
 		goto end;
 	}
 
 	/* type is MTK_JPEG_FMT_TYPE_CAPTURE */
-	mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
-				   MTK_JPEG_MAX_WIDTH, fmt->h_align,
-				   &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
-				   MTK_JPEG_MAX_HEIGHT, fmt->v_align);
+	pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align),
+			       MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT);
+	pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align),
+			      MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH);
 
 	for (i = 0; i < fmt->colplanes; i++) {
 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
@@ -771,8 +752,8 @@ static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
 {
 	bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
 	bs->end_addr = bs->str_addr +
-			 mtk_jpeg_align(vb2_get_plane_payload(src_buf, 0), 16);
-	bs->size = mtk_jpeg_align(vb2_plane_size(src_buf, 0), 128);
+		       round_up(vb2_get_plane_payload(src_buf, 0), 16);
+	bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
 }
 
 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
index 999bd1427809..28e9b30ad5c3 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
@@ -21,10 +21,10 @@
 #define MTK_JPEG_FMT_TYPE_OUTPUT	1
 #define MTK_JPEG_FMT_TYPE_CAPTURE	2
 
-#define MTK_JPEG_MIN_WIDTH	32
-#define MTK_JPEG_MIN_HEIGHT	32
-#define MTK_JPEG_MAX_WIDTH	8192
-#define MTK_JPEG_MAX_HEIGHT	8192
+#define MTK_JPEG_MIN_WIDTH	32U
+#define MTK_JPEG_MIN_HEIGHT	32U
+#define MTK_JPEG_MAX_WIDTH	8192U
+#define MTK_JPEG_MAX_HEIGHT	8192U
 
 #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
 
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c
index ddf0dfa78e20..68abcfd7494d 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c
@@ -153,10 +153,10 @@ static int mtk_jpeg_calc_dst_size(struct mtk_jpeg_dec_param *param)
 				param->sampling_w[i];
 		/* output format is 420/422 */
 		param->comp_w[i] = padding_w >> brz_w[i];
-		param->comp_w[i] = mtk_jpeg_align(param->comp_w[i],
-						  MTK_JPEG_DCTSIZE);
-		param->img_stride[i] = i ? mtk_jpeg_align(param->comp_w[i], 16)
-					: mtk_jpeg_align(param->comp_w[i], 32);
+		param->comp_w[i] = round_up(param->comp_w[i],
+					    MTK_JPEG_DCTSIZE);
+		param->img_stride[i] = i ? round_up(param->comp_w[i], 16)
+					: round_up(param->comp_w[i], 32);
 		ds_row_h[i] = (MTK_JPEG_DCTSIZE * param->sampling_h[i]);
 	}
 	param->dec_w = param->img_stride[0];
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h
index 9c6584eaad99..7b0687f8f4b6 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h
@@ -54,11 +54,6 @@ struct mtk_jpeg_dec_param {
 	u8 uv_brz_w;
 };
 
-static inline u32 mtk_jpeg_align(u32 val, u32 align)
-{
-	return (val + align - 1) & ~(align - 1);
-}
-
 struct mtk_jpeg_bs {
 	dma_addr_t	str_addr;
 	dma_addr_t	end_addr;
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 05/11] media: platform: Change case for improving code quality
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (3 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 04/11] media: platform: Use kernel native functions for improving code quality Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 06/11] media: platform: Change MTK_JPEG_COMP_MAX macro definition location Xia Jiang
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Change register offset hex numberals from upercase to lowercase.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
index 94db04e9cdb6..2945da842dfa 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
@@ -20,29 +20,29 @@
 #define BIT_INQST_MASK_ALLIRQ		0x37
 
 #define JPGDEC_REG_RESET		0x0090
-#define JPGDEC_REG_BRZ_FACTOR		0x00F8
-#define JPGDEC_REG_DU_NUM		0x00FC
+#define JPGDEC_REG_BRZ_FACTOR		0x00f8
+#define JPGDEC_REG_DU_NUM		0x00fc
 #define JPGDEC_REG_DEST_ADDR0_Y		0x0140
 #define JPGDEC_REG_DEST_ADDR0_U		0x0144
 #define JPGDEC_REG_DEST_ADDR0_V		0x0148
-#define JPGDEC_REG_DEST_ADDR1_Y		0x014C
+#define JPGDEC_REG_DEST_ADDR1_Y		0x014c
 #define JPGDEC_REG_DEST_ADDR1_U		0x0150
 #define JPGDEC_REG_DEST_ADDR1_V		0x0154
 #define JPGDEC_REG_STRIDE_Y		0x0158
-#define JPGDEC_REG_STRIDE_UV		0x015C
+#define JPGDEC_REG_STRIDE_UV		0x015c
 #define JPGDEC_REG_IMG_STRIDE_Y		0x0160
 #define JPGDEC_REG_IMG_STRIDE_UV	0x0164
-#define JPGDEC_REG_WDMA_CTRL		0x016C
+#define JPGDEC_REG_WDMA_CTRL		0x016c
 #define JPGDEC_REG_PAUSE_MCU_NUM	0x0170
-#define JPGDEC_REG_OPERATION_MODE	0x017C
+#define JPGDEC_REG_OPERATION_MODE	0x017c
 #define JPGDEC_REG_FILE_ADDR		0x0200
-#define JPGDEC_REG_COMP_ID		0x020C
+#define JPGDEC_REG_COMP_ID		0x020c
 #define JPGDEC_REG_TOTAL_MCU_NUM	0x0210
 #define JPGDEC_REG_COMP0_DATA_UNIT_NUM	0x0224
-#define JPGDEC_REG_DU_CTRL		0x023C
+#define JPGDEC_REG_DU_CTRL		0x023c
 #define JPGDEC_REG_TRIG			0x0240
 #define JPGDEC_REG_FILE_BRP		0x0248
-#define JPGDEC_REG_FILE_TOTAL_SIZE	0x024C
+#define JPGDEC_REG_FILE_TOTAL_SIZE	0x024c
 #define JPGDEC_REG_QT_ID		0x0270
 #define JPGDEC_REG_INTERRUPT_STATUS	0x0274
 #define JPGDEC_REG_STATUS		0x0278
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 06/11] media: platform: Change MTK_JPEG_COMP_MAX macro definition location
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (4 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 05/11] media: platform: Change case " Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 07/11] media: platform: Delete redundant code for improving code quality Xia Jiang
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Move MTK_JPEG_COMP_MAX definition to mtk_jpeg_core.h file, because it
is used by mtk_jpeg_core.c file.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h | 2 ++
 drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h  | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
index 28e9b30ad5c3..64a731261214 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
@@ -15,6 +15,8 @@
 
 #define MTK_JPEG_NAME		"mtk-jpeg"
 
+#define MTK_JPEG_COMP_MAX		3
+
 #define MTK_JPEG_FMT_FLAG_DEC_OUTPUT	BIT(0)
 #define MTK_JPEG_FMT_FLAG_DEC_CAPTURE	BIT(1)
 
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
index 2945da842dfa..21ec8f96797f 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
@@ -8,7 +8,6 @@
 #ifndef _MTK_JPEG_REG_H
 #define _MTK_JPEG_REG_H
 
-#define MTK_JPEG_COMP_MAX		3
 #define MTK_JPEG_BLOCK_MAX		10
 #define MTK_JPEG_DCTSIZE		8
 
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 07/11] media: platform: Delete redundant code for improving code quality
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (5 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 06/11] media: platform: Change MTK_JPEG_COMP_MAX macro definition location Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 08/11] media: dt-bindings: Add jpeg enc device tree node document Xia Jiang
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Delete unused member variables annotation.
Delete unused variable definition.
Delete redundant log print, because V4L2 debug logs already print it.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c | 17 +++--------------
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h |  5 +++--
 2 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index 4e9ff1f9732e..b76575e7d300 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -182,7 +182,6 @@ static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 				   struct mtk_jpeg_ctx *ctx, int q_type)
 {
 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 	int i;
 
 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
@@ -190,7 +189,7 @@ static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 
 	if (ctx->state != MTK_JPEG_INIT) {
 		mtk_jpeg_adjust_fmt_mplane(ctx, f);
-		goto end;
+		return 0;
 	}
 
 	pix_mp->num_planes = fmt->colplanes;
@@ -210,7 +209,7 @@ static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 		pfmt->sizeimage = round_up(pfmt->sizeimage, 128);
 		if (pfmt->sizeimage == 0)
 			pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
-		goto end;
+		return 0;
 	}
 
 	/* type is MTK_JPEG_FMT_TYPE_CAPTURE */
@@ -224,20 +223,10 @@ static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 		u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
 		u32 h = pix_mp->height * fmt->v_sample[i] / 4;
 
-		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
 		pfmt->bytesperline = stride;
 		pfmt->sizeimage = stride * h;
 	}
-end:
-	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "wxh:%ux%u\n",
-		 pix_mp->width, pix_mp->height);
-	for (i = 0; i < pix_mp->num_planes; i++) {
-		v4l2_dbg(2, debug, &jpeg->v4l2_dev,
-			 "plane[%d] bpl=%u, size=%u\n",
-			 i,
-			 pix_mp->plane_fmt[i].bytesperline,
-			 pix_mp->plane_fmt[i].sizeimage);
-	}
+
 	return 0;
 }
 
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
index 64a731261214..9bbd615b1067 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
@@ -30,6 +30,9 @@
 
 #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
 
+/**
+ * enum mtk_jpeg_ctx_state - contex state of jpeg
+ */
 enum mtk_jpeg_ctx_state {
 	MTK_JPEG_INIT = 0,
 	MTK_JPEG_RUNNING,
@@ -109,9 +112,7 @@ struct mtk_jpeg_q_data {
  * @out_q:		source (output) queue information
  * @cap_q:		destination (capture) queue queue information
  * @fh:			V4L2 file handle
- * @dec_param		parameters for HW decoding
  * @state:		state of the context
- * @header_valid:	set if header has been parsed and valid
  * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
  * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
  * @quantization: enum v4l2_quantization, colorspace quantization
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 08/11] media: dt-bindings: Add jpeg enc device tree node document
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (6 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 07/11] media: platform: Delete redundant code for improving code quality Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 09/11] arm: dts: Add jpeg enc device tree node Xia Jiang
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Add jpeg enc device tree node document

Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
v7: no changes

v6: no changes

v5: no changes

v4: no changes

v3: change compatible to SoC specific compatible

v2: no changes
---
 .../bindings/media/mediatek-jpeg-encoder.txt  | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/mediatek-jpeg-encoder.txt

diff --git a/Documentation/devicetree/bindings/media/mediatek-jpeg-encoder.txt b/Documentation/devicetree/bindings/media/mediatek-jpeg-encoder.txt
new file mode 100644
index 000000000000..fa8da699493b
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/mediatek-jpeg-encoder.txt
@@ -0,0 +1,37 @@
+* MediaTek JPEG Encoder
+
+MediaTek JPEG Encoder is the JPEG encode hardware present in MediaTek SoCs
+
+Required properties:
+- compatible : should be one of:
+               "mediatek,mt2701-jpgenc"
+               ...
+               followed by "mediatek,mtk-jpgenc"
+- reg : physical base address of the JPEG encoder registers and length of
+  memory mapped region.
+- interrupts : interrupt number to the interrupt controller.
+- clocks: device clocks, see
+  Documentation/devicetree/bindings/clock/clock-bindings.txt for details.
+- clock-names: must contain "jpgenc". It is the clock of JPEG encoder.
+- power-domains: a phandle to the power domain, see
+  Documentation/devicetree/bindings/power/power_domain.txt for details.
+- mediatek,larb: must contain the local arbiters in the current SoCs, see
+  Documentation/devicetree/bindings/memory-controllers/mediatek,smi-larb.txt
+  for details.
+- iommus: should point to the respective IOMMU block with master port as
+  argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.txt
+  for details.
+
+Example:
+	jpegenc: jpegenc@1500a000 {
+		compatible = "mediatek,mt2701-jpgenc",
+			     "mediatek,mtk-jpgenc";
+		reg = <0 0x1500a000 0 0x1000>;
+		interrupts = <GIC_SPI 141 IRQ_TYPE_LEVEL_LOW>;
+		clocks =  <&imgsys CLK_IMG_VENC>;
+		clock-names = "jpgenc";
+		power-domains = <&scpsys MT2701_POWER_DOMAIN_ISP>;
+		mediatek,larb = <&larb2>;
+		iommus = <&iommu MT2701_M4U_PORT_JPGENC_RDMA>,
+			 <&iommu MT2701_M4U_PORT_JPGENC_BSDMA>;
+	};
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 09/11] arm: dts: Add jpeg enc device tree node
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (7 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 08/11] media: dt-bindings: Add jpeg enc device tree node document Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 10/11] media: platform: Rename jpeg dec file name Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature Xia Jiang
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Add jpeg enc device tree node

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
v7: no changes

v6: no changes

v5: no changes

v4: no changes

v3: change compatible to SoC specific compatible

v2: no changes
---
 arch/arm/boot/dts/mt2701.dtsi | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/arm/boot/dts/mt2701.dtsi b/arch/arm/boot/dts/mt2701.dtsi
index 51e1305c6471..f2f92150b3fb 100644
--- a/arch/arm/boot/dts/mt2701.dtsi
+++ b/arch/arm/boot/dts/mt2701.dtsi
@@ -569,6 +569,19 @@
 			 <&iommu MT2701_M4U_PORT_JPGDEC_BSDMA>;
 	};
 
+	jpegenc: jpegenc@1500a000 {
+		compatible = "mediatek,mt2701-jpgenc",
+			     "mediatek,mtk-jpgenc";
+		reg = <0 0x1500a000 0 0x1000>;
+		interrupts = <GIC_SPI 141 IRQ_TYPE_LEVEL_LOW>;
+		clocks =  <&imgsys CLK_IMG_VENC>;
+		clock-names = "jpgenc";
+		power-domains = <&scpsys MT2701_POWER_DOMAIN_ISP>;
+		mediatek,larb = <&larb2>;
+		iommus = <&iommu MT2701_M4U_PORT_JPGENC_RDMA>,
+			 <&iommu MT2701_M4U_PORT_JPGENC_BSDMA>;
+	};
+
 	vdecsys: syscon@16000000 {
 		compatible = "mediatek,mt2701-vdecsys", "syscon";
 		reg = <0 0x16000000 0 0x1000>;
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 10/11] media: platform: Rename jpeg dec file name
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (8 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 09/11] arm: dts: Add jpeg enc device tree node Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-03 12:34 ` [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature Xia Jiang
  10 siblings, 0 replies; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Rename the files which are for decode feature. This is preparing
path since the jpeg enc patch will be added later.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
v7: no changes

v6: no changes

v5: no changes

v4: no changes

v3: no changes

v2: no changes
---
 drivers/media/platform/mtk-jpeg/Makefile                      | 2 +-
 drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c               | 4 ++--
 .../platform/mtk-jpeg/{mtk_jpeg_hw.c => mtk_jpeg_dec_hw.c}    | 2 +-
 .../platform/mtk-jpeg/{mtk_jpeg_hw.h => mtk_jpeg_dec_hw.h}    | 2 +-
 .../mtk-jpeg/{mtk_jpeg_parse.c => mtk_jpeg_dec_parse.c}       | 2 +-
 .../mtk-jpeg/{mtk_jpeg_parse.h => mtk_jpeg_dec_parse.h}       | 2 +-
 .../platform/mtk-jpeg/{mtk_jpeg_reg.h => mtk_jpeg_dec_reg.h}  | 0
 7 files changed, 7 insertions(+), 7 deletions(-)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_hw.c => mtk_jpeg_dec_hw.c} (99%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_hw.h => mtk_jpeg_dec_hw.h} (98%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_parse.c => mtk_jpeg_dec_parse.c} (98%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_parse.h => mtk_jpeg_dec_parse.h} (92%)
 rename drivers/media/platform/mtk-jpeg/{mtk_jpeg_reg.h => mtk_jpeg_dec_reg.h} (100%)

diff --git a/drivers/media/platform/mtk-jpeg/Makefile b/drivers/media/platform/mtk-jpeg/Makefile
index 92a4fc046bfe..48516dcf96e6 100644
--- a/drivers/media/platform/mtk-jpeg/Makefile
+++ b/drivers/media/platform/mtk-jpeg/Makefile
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
-mtk_jpeg-objs := mtk_jpeg_core.o mtk_jpeg_hw.o mtk_jpeg_parse.o
+mtk_jpeg-objs := mtk_jpeg_core.o mtk_jpeg_dec_hw.o mtk_jpeg_dec_parse.o
 obj-$(CONFIG_VIDEO_MEDIATEK_JPEG) += mtk_jpeg.o
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index b76575e7d300..ccdba87ef806 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -23,9 +23,9 @@
 #include <media/videobuf2-dma-contig.h>
 #include <soc/mediatek/smi.h>
 
-#include "mtk_jpeg_hw.h"
+#include "mtk_jpeg_dec_hw.h"
 #include "mtk_jpeg_core.h"
-#include "mtk_jpeg_parse.h"
+#include "mtk_jpeg_dec_parse.h"
 
 static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
 	{
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.c
similarity index 99%
rename from drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c
rename to drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.c
index 68abcfd7494d..afbbfd5d02bc 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.c
@@ -9,7 +9,7 @@
 #include <linux/kernel.h>
 #include <media/videobuf2-core.h>
 
-#include "mtk_jpeg_hw.h"
+#include "mtk_jpeg_dec_hw.h"
 
 #define MTK_JPEG_DUNUM_MASK(val)	(((val) - 1) & 0x3)
 
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
similarity index 98%
rename from drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h
rename to drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
index 7b0687f8f4b6..1cc37dbfc8e7 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_hw.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
@@ -11,7 +11,7 @@
 #include <media/videobuf2-core.h>
 
 #include "mtk_jpeg_core.h"
-#include "mtk_jpeg_reg.h"
+#include "mtk_jpeg_dec_reg.h"
 
 enum {
 	MTK_JPEG_DEC_RESULT_EOF_DONE		= 0,
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.c
similarity index 98%
rename from drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.c
rename to drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.c
index f862d38f3af7..b95c45791c29 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.c
@@ -8,7 +8,7 @@
 #include <linux/kernel.h>
 #include <linux/videodev2.h>
 
-#include "mtk_jpeg_parse.h"
+#include "mtk_jpeg_dec_parse.h"
 
 #define TEM	0x01
 #define SOF0	0xc0
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.h
similarity index 92%
rename from drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.h
rename to drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.h
index 0a48eeabaff2..2918f15811f8 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_parse.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_parse.h
@@ -8,7 +8,7 @@
 #ifndef _MTK_JPEG_PARSE_H
 #define _MTK_JPEG_PARSE_H
 
-#include "mtk_jpeg_hw.h"
+#include "mtk_jpeg_dec_hw.h"
 
 bool mtk_jpeg_parse(struct mtk_jpeg_dec_param *param, u8 *src_addr_va,
 		    u32 src_size);
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_reg.h
similarity index 100%
rename from drivers/media/platform/mtk-jpeg/mtk_jpeg_reg.h
rename to drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_reg.h
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
                   ` (9 preceding siblings ...)
  2020-03-03 12:34 ` [PATCH v7 10/11] media: platform: Rename jpeg dec file name Xia Jiang
@ 2020-03-03 12:34 ` Xia Jiang
  2020-03-06 11:23   ` Tomasz Figa
  10 siblings, 1 reply; 18+ messages in thread
From: Xia Jiang @ 2020-03-03 12:34 UTC (permalink / raw)
  To: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang
  Cc: linux-media, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, Marek Szyprowski, Tomasz Figa, srv_heupstream,
	Xia Jiang

Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
decode and encode have great similarities with function operation.

Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
---
v7: reverse spin lock and unlock operation in device run function for
    multi-instance.

v6: add space to arounding '+'.
    alignment 'struct mtk_jpeg_fmt *fmt' match open parenthesis.
    change 'mtk_jpeg_enc_set_encFormat' to 'mtk_jpeg_enc_set_enc_format'.
    make 'mtk_jpeg_ctrls_setup' to static prototype.
    delete unused variables 'jpeg'/'align_h'/'align_w'/'flags'.
    initialize 'yuv_format'/'enc_quality' variables.
    
v5: support crop for encoder and compose for decoder in s_selection and
    g_selection function.
    use clamp() to replace mtk_jpeg_bound_align_image() and round_up()
    to replace mtk_jpeg_align().
    delete jpeg_enc_param/mtk_jpeg_enc_param structure and
    mtk_jpeg_set_param(), program the registers directly based on
    the original V4L2 values.
    move macro definition about hw to mtk_jpeg_enc_reg.h.
    delete unnecessary V4L2 logs in driver.
    cancel spin lock and unlock operation in deviec run function.
    change jpeg enc register offset hex numberals from upercase to lowercase.

v4: split mtk_jpeg_try_fmt_mplane() to two functions, one for encoder,                                                      
    one for decoder.                                                          
    split mtk_jpeg_set_default_params() to two functions, one for                                                          
    encoder, one for decoder.                                                          
    add cropping support for encoder in g/s_selection ioctls.                                                          
    change exif mode support by using V4L2_JPEG_ACTIVE_MARKER_APP1.                                                         
    change MTK_JPEG_MAX_WIDTH/MTK_JPEG_MAX_HEIGH from 8192 to 65535 by                                                      
    specification.                                                          
    move width shifting operation behind aligning operation in                                                          
    mtk_jpeg_try_enc_fmt_mplane() for bug fix.                                                          
    fix user abuseing data_offset issue for DMABUF in                                                          
    mtk_jpeg_set_enc_src().                                                          
    fix kbuild warings: change MTK_JPEG_MIN_HEIGHT/MTK_JPEG_MAX_HEIGHT                                                      
                        and MTK_JPEG_MIN_WIDTH/MTK_JPEG_MAX_WIDTH from                                                      
                        'int' type to 'unsigned int' type.                                                          
                        fix msleadingly indented of 'else'.                                                                                                              
v3: delete Change-Id.                                                          
    only test once handler->error after the last v4l2_ctrl_new_std().                                                       
    seperate changes of v4l2-ctrls.c and v4l2-controls.h to new patch.                                                      
v2: fix compliance test fail, check created buffer size in driver. 
---
 drivers/media/platform/mtk-jpeg/Makefile      |   5 +-
 .../media/platform/mtk-jpeg/mtk_jpeg_core.c   | 684 ++++++++++++++----
 .../media/platform/mtk-jpeg/mtk_jpeg_core.h   |  39 +-
 .../media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h |   7 +-
 .../media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c | 273 +++++++
 .../media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h |  86 +++
 .../platform/mtk-jpeg/mtk_jpeg_enc_reg.h      |  78 ++
 7 files changed, 1007 insertions(+), 165 deletions(-)
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
 create mode 100644 drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h

diff --git a/drivers/media/platform/mtk-jpeg/Makefile b/drivers/media/platform/mtk-jpeg/Makefile
index 48516dcf96e6..76c33aad0f3f 100644
--- a/drivers/media/platform/mtk-jpeg/Makefile
+++ b/drivers/media/platform/mtk-jpeg/Makefile
@@ -1,3 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
-mtk_jpeg-objs := mtk_jpeg_core.o mtk_jpeg_dec_hw.o mtk_jpeg_dec_parse.o
+mtk_jpeg-objs := mtk_jpeg_core.o \
+		 mtk_jpeg_dec_hw.o \
+		 mtk_jpeg_dec_parse.o \
+		 mtk_jpeg_enc_hw.o
 obj-$(CONFIG_VIDEO_MEDIATEK_JPEG) += mtk_jpeg.o
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
index ccdba87ef806..fd9202fa6793 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
@@ -3,6 +3,7 @@
  * Copyright (c) 2016 MediaTek Inc.
  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
  *         Rick Chang <rick.chang@mediatek.com>
+ *         Xia Jiang <xia.jiang@mediatek.com>
  */
 
 #include <linux/clk.h>
@@ -23,6 +24,7 @@
 #include <media/videobuf2-dma-contig.h>
 #include <soc/mediatek/smi.h>
 
+#include "mtk_jpeg_enc_hw.h"
 #include "mtk_jpeg_dec_hw.h"
 #include "mtk_jpeg_core.h"
 #include "mtk_jpeg_dec_parse.h"
@@ -31,7 +33,8 @@ static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
 	{
 		.fourcc		= V4L2_PIX_FMT_JPEG,
 		.colplanes	= 1,
-		.flags		= MTK_JPEG_FMT_FLAG_DEC_OUTPUT,
+		.flags		= MTK_JPEG_FMT_FLAG_DEC_OUTPUT |
+				  MTK_JPEG_FMT_FLAG_ENC_CAPTURE,
 	},
 	{
 		.fourcc		= V4L2_PIX_FMT_YUV420M,
@@ -51,6 +54,42 @@ static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
 		.v_align	= 3,
 		.flags		= MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
 	},
+	{
+		.fourcc		= V4L2_PIX_FMT_NV12M,
+		.h_sample	= {4, 2, 2},
+		.v_sample	= {4, 2, 2},
+		.colplanes	= 2,
+		.h_align	= 4,
+		.v_align	= 4,
+		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
+	},
+	{
+		.fourcc		= V4L2_PIX_FMT_NV21M,
+		.h_sample	= {4, 2, 2},
+		.v_sample	= {4, 2, 2},
+		.colplanes	= 2,
+		.h_align	= 4,
+		.v_align	= 4,
+		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
+	},
+	{
+		.fourcc		= V4L2_PIX_FMT_YUYV,
+		.h_sample	= {4, 2, 2},
+		.v_sample	= {4, 4, 4},
+		.colplanes	= 1,
+		.h_align	= 4,
+		.v_align	= 3,
+		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
+	},
+	{
+		.fourcc		= V4L2_PIX_FMT_YVYU,
+		.h_sample	= {4, 2, 2},
+		.v_sample	= {4, 4, 4},
+		.colplanes	= 1,
+		.h_align	= 4,
+		.v_align	= 3,
+		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
+	},
 };
 
 #define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
@@ -70,6 +109,11 @@ struct mtk_jpeg_src_buf {
 static int debug;
 module_param(debug, int, 0644);
 
+static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
+{
+	return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
+}
+
 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
 {
 	return container_of(fh, struct mtk_jpeg_ctx, fh);
@@ -86,14 +130,69 @@ static int mtk_jpeg_querycap(struct file *file, void *priv,
 {
 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
 
-	strscpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
-	strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
+	strscpy(cap->driver, MTK_JPEG_NAME, sizeof(cap->driver));
+	if (jpeg->mode ==  MTK_JPEG_ENC)
+		strscpy(cap->card, MTK_JPEG_NAME " encoder", sizeof(cap->card));
+	else
+		strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
 		 dev_name(jpeg->dev));
 
 	return 0;
 }
 
+static int vidioc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
+	int ret = 0;
+
+	switch (ctrl->id) {
+	case V4L2_CID_JPEG_RESTART_INTERVAL:
+		ctx->restart_interval = ctrl->val;
+		break;
+	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
+		ctx->enc_quality = ctrl->val;
+		break;
+	case V4L2_CID_JPEG_ACTIVE_MARKER:
+		ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
+		break;
+	}
+
+	return ret;
+}
+
+static const struct v4l2_ctrl_ops mtk_jpeg_ctrl_ops = {
+	.s_ctrl = vidioc_jpeg_s_ctrl,
+};
+
+static int mtk_jpeg_ctrls_setup(struct mtk_jpeg_ctx *ctx)
+{
+	const struct v4l2_ctrl_ops *ops = &mtk_jpeg_ctrl_ops;
+	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
+	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
+
+	v4l2_ctrl_handler_init(handler, 3);
+
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL,
+				  0, 100, 1, 0);
+		v4l2_ctrl_new_std(handler, ops,
+				  V4L2_CID_JPEG_COMPRESSION_QUALITY, 48, 100, 1,
+				  90);
+		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
+				  V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
+
+		if (handler->error) {
+			v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+			return handler->error;
+		}
+	}
+
+	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
+
+	return 0;
+}
+
 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
 			     struct v4l2_fmtdesc *f, u32 type)
 {
@@ -118,19 +217,27 @@ static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
 				     struct v4l2_fmtdesc *f)
 {
+	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
+
 	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
+				 ctx->jpeg->mode == MTK_JPEG_ENC ?
+				 MTK_JPEG_FMT_FLAG_ENC_CAPTURE :
 				 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
 }
 
 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
 				     struct v4l2_fmtdesc *f)
 {
+	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
+
 	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
+				 ctx->jpeg->mode == MTK_JPEG_ENC ?
+				 MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
 				 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
 }
 
-static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
-						   enum v4l2_buf_type type)
+static struct mtk_jpeg_q_data *
+mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx, enum v4l2_buf_type type)
 {
 	if (V4L2_TYPE_IS_OUTPUT(type))
 		return &ctx->out_q;
@@ -143,9 +250,14 @@ static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
 {
 	unsigned int k, fmt_flag;
 
-	fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
-		   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
-		   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
+	if (ctx->jpeg->mode ==  MTK_JPEG_ENC)
+		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
+			   MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
+			   MTK_JPEG_FMT_FLAG_ENC_CAPTURE;
+	else
+		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
+			   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
+			   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
 
 	for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
 		struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
@@ -177,9 +289,73 @@ static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
 	}
 }
 
-static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
-				   struct mtk_jpeg_fmt *fmt,
-				   struct mtk_jpeg_ctx *ctx, int q_type)
+static int mtk_jpeg_try_enc_fmt_mplane(struct v4l2_format *f,
+				       struct mtk_jpeg_fmt *fmt,
+				       struct mtk_jpeg_ctx *ctx, int q_type)
+{
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	int i;
+
+	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
+	pix_mp->field = V4L2_FIELD_NONE;
+
+	if (ctx->state != MTK_JPEG_INIT) {
+		mtk_jpeg_adjust_fmt_mplane(ctx, f);
+		return 0;
+	}
+
+	pix_mp->num_planes = fmt->colplanes;
+	pix_mp->pixelformat = fmt->fourcc;
+
+	if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
+		if (pix_mp->num_planes == 1U) {
+			pix_mp->height = clamp(round_up(pix_mp->height, 8),
+					       MTK_JPEG_MIN_HEIGHT,
+					       MTK_JPEG_MAX_HEIGHT);
+			pix_mp->width = clamp(round_up(pix_mp->width, 32),
+					      MTK_JPEG_MIN_WIDTH,
+					      MTK_JPEG_MAX_WIDTH);
+			pix_mp->plane_fmt[0].bytesperline = pix_mp->width * 2;
+			pix_mp->plane_fmt[0].sizeimage =
+				pix_mp->width * pix_mp->height * 2;
+		}
+		if (pix_mp->num_planes == 2U) {
+			pix_mp->height = clamp(round_up(pix_mp->height, 16),
+					       MTK_JPEG_MIN_HEIGHT,
+					       MTK_JPEG_MAX_HEIGHT);
+			pix_mp->width = clamp(round_up(pix_mp->width, 16),
+					      MTK_JPEG_MIN_WIDTH,
+					      MTK_JPEG_MAX_WIDTH);
+			for (i = 0; i < pix_mp->num_planes; i++) {
+				pix_mp->plane_fmt[i].bytesperline =
+					pix_mp->width;
+				pix_mp->plane_fmt[i].sizeimage =
+					pix_mp->width * pix_mp->height /
+					(i + 1);
+			}
+		}
+	} else {
+		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
+				       MTK_JPEG_MAX_HEIGHT);
+		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
+				      MTK_JPEG_MAX_WIDTH);
+
+		if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
+			pix_mp->plane_fmt[0].bytesperline = 0;
+			pix_mp->plane_fmt[0].sizeimage =
+				round_up(pix_mp->plane_fmt[0].sizeimage, 128);
+			if (pix_mp->plane_fmt[0].sizeimage == 0)
+				pix_mp->plane_fmt[0].sizeimage =
+					MTK_JPEG_DEFAULT_SIZEIMAGE;
+		}
+	}
+
+	return 0;
+}
+
+static int mtk_jpeg_try_dec_fmt_mplane(struct v4l2_format *f,
+				       struct mtk_jpeg_fmt *fmt,
+				       struct mtk_jpeg_ctx *ctx, int q_type)
 {
 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 	int i;
@@ -286,6 +462,7 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
 {
 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 	struct mtk_jpeg_fmt *fmt;
+	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 
 	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
 				   MTK_JPEG_FMT_TYPE_CAPTURE);
@@ -299,7 +476,12 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
 		 (fmt->fourcc >> 16 & 0xff),
 		 (fmt->fourcc >> 24 & 0xff));
 
-	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
+	if (jpeg->mode == MTK_JPEG_ENC)
+		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
+						   MTK_JPEG_FMT_TYPE_CAPTURE);
+	else
+		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
+						   MTK_JPEG_FMT_TYPE_CAPTURE);
 }
 
 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
@@ -307,6 +489,7 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
 {
 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 	struct mtk_jpeg_fmt *fmt;
+	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 
 	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
 				   MTK_JPEG_FMT_TYPE_OUTPUT);
@@ -320,7 +503,12 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
 		 (fmt->fourcc >> 16 & 0xff),
 		 (fmt->fourcc >> 24 & 0xff));
 
-	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
+	if (jpeg->mode == MTK_JPEG_ENC)
+		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
+						   MTK_JPEG_FMT_TYPE_OUTPUT);
+	else
+		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
+						   MTK_JPEG_FMT_TYPE_OUTPUT);
 }
 
 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
@@ -425,28 +613,48 @@ static int mtk_jpeg_g_selection(struct file *file, void *priv,
 				struct v4l2_selection *s)
 {
 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
+	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 
-	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-		return -EINVAL;
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
+			return -EINVAL;
 
-	switch (s->target) {
-	case V4L2_SEL_TGT_COMPOSE:
-	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
-		s->r.width = ctx->out_q.w;
-		s->r.height = ctx->out_q.h;
-		s->r.left = 0;
-		s->r.top = 0;
-		break;
-	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
-	case V4L2_SEL_TGT_COMPOSE_PADDED:
-		s->r.width = ctx->cap_q.w;
-		s->r.height = ctx->cap_q.h;
-		s->r.left = 0;
-		s->r.top = 0;
-		break;
-	default:
-		return -EINVAL;
+		switch (s->target) {
+		case V4L2_SEL_TGT_CROP:
+		case V4L2_SEL_TGT_CROP_BOUNDS:
+		case V4L2_SEL_TGT_CROP_DEFAULT:
+			s->r.width = ctx->out_q.w;
+			s->r.height = ctx->out_q.h;
+			s->r.left = 0;
+			s->r.top = 0;
+			break;
+		default:
+			return -EINVAL;
+		}
+	} else {
+		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+			return -EINVAL;
+
+		switch (s->target) {
+		case V4L2_SEL_TGT_COMPOSE:
+		case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+			s->r.width = ctx->out_q.w;
+			s->r.height = ctx->out_q.h;
+			s->r.left = 0;
+			s->r.top = 0;
+			break;
+		case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+		case V4L2_SEL_TGT_COMPOSE_PADDED:
+			s->r.width = ctx->cap_q.w;
+			s->r.height = ctx->cap_q.h;
+			s->r.left = 0;
+			s->r.top = 0;
+			break;
+		default:
+			return -EINVAL;
+		}
 	}
+
 	return 0;
 }
 
@@ -454,20 +662,38 @@ static int mtk_jpeg_s_selection(struct file *file, void *priv,
 				struct v4l2_selection *s)
 {
 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
+	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 
-	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-		return -EINVAL;
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
+			return -EINVAL;
 
-	switch (s->target) {
-	case V4L2_SEL_TGT_COMPOSE:
-		s->r.left = 0;
-		s->r.top = 0;
-		ctx->out_q.w = s->r.width;
-		ctx->out_q.h = s->r.height;
-		break;
-	default:
-		return -EINVAL;
+		switch (s->target) {
+		case V4L2_SEL_TGT_CROP:
+			s->r.left = 0;
+			s->r.top = 0;
+			ctx->out_q.w = s->r.width;
+			ctx->out_q.h = s->r.height;
+			break;
+		default:
+			return -EINVAL;
+		}
+	} else {
+		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+			return -EINVAL;
+
+		switch (s->target) {
+		case V4L2_SEL_TGT_COMPOSE:
+			s->r.left = 0;
+			s->r.top = 0;
+			ctx->out_q.w = s->r.width;
+			ctx->out_q.h = s->r.height;
+			break;
+		default:
+			return -EINVAL;
+		}
 	}
+
 	return 0;
 }
 
@@ -643,29 +869,42 @@ static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
 		goto end;
 
 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
-	param = &jpeg_src_buf->dec_param;
-	memset(param, 0, sizeof(*param));
-
-	if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
-		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
-		goto end;
-	}
-	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
-				      vb2_get_plane_payload(vb, 0));
-	if (!header_valid) {
-		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
-		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
-		return;
-	}
-
-	if (ctx->state == MTK_JPEG_INIT) {
-		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
-			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
+			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos");
+			goto end;
+		}
+		if (ctx->state == MTK_JPEG_INIT)
+			ctx->state = MTK_JPEG_RUNNING;
+	} else {
+		param = &jpeg_src_buf->dec_param;
+		memset(param, 0, sizeof(*param));
+
+		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
+			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
+			goto end;
+		}
+		header_valid = mtk_jpeg_parse(param,
+					      (u8 *)vb2_plane_vaddr(vb, 0),
+					      vb2_get_plane_payload(vb, 0));
+		if (!header_valid) {
+			v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
+			vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
+			return;
+		}
 
-		mtk_jpeg_queue_src_chg_event(ctx);
-		mtk_jpeg_set_queue_data(ctx, param);
-		ctx->state = vb2_is_streaming(dst_vq) ?
-				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
+		if (ctx->state == MTK_JPEG_INIT) {
+			struct vb2_queue *dst_vq;
+
+			dst_vq = v4l2_m2m_get_vq
+					(ctx->fh.m2m_ctx,
+					 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+			mtk_jpeg_queue_src_chg_event(ctx);
+			mtk_jpeg_set_queue_data(ctx, param);
+			ctx->state = vb2_is_streaming(dst_vq) ?
+					MTK_JPEG_SOURCE_CHANGE :
+					MTK_JPEG_RUNNING;
+		}
 	}
 end:
 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
@@ -708,16 +947,16 @@ static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
 	 * subsampling. Update capture queue when the stream is off.
 	 */
 	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
-	    !V4L2_TYPE_IS_OUTPUT(q->type)) {
+	    !V4L2_TYPE_IS_OUTPUT(q->type) &&
+	    ctx->jpeg->mode == MTK_JPEG_DEC) {
 		struct mtk_jpeg_src_buf *src_buf;
 
 		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
 		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
 		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
 		ctx->state = MTK_JPEG_RUNNING;
-	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
+	} else if (V4L2_TYPE_IS_OUTPUT(q->type))
 		ctx->state = MTK_JPEG_INIT;
-	}
 
 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
@@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
 	return 0;
 }
 
+static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
+				 struct vb2_buffer *dst_buf,
+				 struct mtk_jpeg_enc_bs *bs)
+{
+	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
+	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;
+	bs->dma_addr_offsetmask = bs->dma_addr & JPEG_ENC_DST_ADDR_OFFSET_MASK;
+	bs->size = round_up(vb2_plane_size(dst_buf, 0), 128);
+
+	mtk_jpeg_enc_set_dst_addr(base, bs->dma_addr, bs->size,
+				  bs->dma_addr_offset,
+				  bs->dma_addr_offsetmask);
+}
+
+static void mtk_jpeg_set_enc_src(struct mtk_jpeg_ctx *ctx, void __iomem *base,
+				 struct vb2_buffer *src_buf,
+				 struct mtk_jpeg_enc_fb *fb)
+{
+	int i;
+
+	mtk_jpeg_enc_set_img_size(base, ctx->out_q.w, ctx->out_q.h);
+	mtk_jpeg_enc_set_enc_format(base, ctx->out_q.fmt->fourcc);
+	mtk_jpeg_enc_set_blk_num(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
+				 ctx->out_q.h);
+	mtk_jpeg_enc_set_stride(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
+				ctx->out_q.h, ctx->out_q.bytesperline[0]);
+
+	for (i = 0; i < src_buf->num_planes; i++) {
+		fb->fb_addr[i].dma_addr =
+			vb2_dma_contig_plane_dma_addr(src_buf, i) +
+			src_buf->planes[i].data_offset;
+		fb->fb_addr[i].size = vb2_plane_size(src_buf, i) -
+				      src_buf->planes[i].data_offset;
+	}
+
+	mtk_jpeg_enc_set_src_addr(base, fb->fb_addr[0].dma_addr,
+				  fb->fb_addr[1].dma_addr);
+}
+
 static void mtk_jpeg_device_run(void *priv)
 {
 	struct mtk_jpeg_ctx *ctx = priv;
@@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
 	struct mtk_jpeg_src_buf *jpeg_src_buf;
 	struct mtk_jpeg_bs bs;
 	struct mtk_jpeg_fb fb;
+	struct mtk_jpeg_enc_bs enc_bs;
+	struct mtk_jpeg_enc_fb enc_fb;
 	int i;
 
 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
@@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
 		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
 			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
 		buf_state = VB2_BUF_STATE_DONE;
-		goto dec_end;
+		goto device_run_end;
 	}
 
-	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
-		mtk_jpeg_queue_src_chg_event(ctx);
-		ctx->state = MTK_JPEG_SOURCE_CHANGE;
-		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
-		return;
-	}
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		spin_lock_irqsave(&jpeg->hw_lock, flags);
+		mtk_jpeg_enc_reset(jpeg->reg_base);
+
+		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
+				     &enc_bs);
+		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
+				     &enc_fb);
+		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
+					  ctx->enc_quality,
+					  ctx->restart_interval);
+
+		mtk_jpeg_enc_start(jpeg->reg_base);
+	} else {
+		if (mtk_jpeg_check_resolution_change
+			(ctx, &jpeg_src_buf->dec_param)) {
+			mtk_jpeg_queue_src_chg_event(ctx);
+			ctx->state = MTK_JPEG_SOURCE_CHANGE;
+			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
+			return;
+		}
 
-	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
-	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
-		goto dec_end;
+		mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
+		if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param,
+					 &dst_buf->vb2_buf, &fb))
+			goto device_run_end;
 
-	spin_lock_irqsave(&jpeg->hw_lock, flags);
-	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
-	mtk_jpeg_dec_set_config(jpeg->dec_reg_base,
-				&jpeg_src_buf->dec_param, &bs, &fb);
+		spin_lock_irqsave(&jpeg->hw_lock, flags);
+		mtk_jpeg_dec_reset(jpeg->reg_base);
+		mtk_jpeg_dec_set_config(jpeg->reg_base,
+					&jpeg_src_buf->dec_param, &bs, &fb);
 
-	mtk_jpeg_dec_start(jpeg->dec_reg_base);
+		mtk_jpeg_dec_start(jpeg->reg_base);
+	}
 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
 	return;
 
-dec_end:
+device_run_end:
 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 	v4l2_m2m_buf_done(src_buf, buf_state);
@@ -875,30 +1172,30 @@ static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
 	ret = mtk_smi_larb_get(jpeg->larb);
 	if (ret)
 		dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
-	clk_prepare_enable(jpeg->clk_jdec_smi);
-	clk_prepare_enable(jpeg->clk_jdec);
+	if (jpeg->mode == MTK_JPEG_DEC)
+		clk_prepare_enable(jpeg->clk_jpeg_smi);
+	clk_prepare_enable(jpeg->clk_jpeg);
 }
 
 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
 {
-	clk_disable_unprepare(jpeg->clk_jdec);
-	clk_disable_unprepare(jpeg->clk_jdec_smi);
+	clk_disable_unprepare(jpeg->clk_jpeg);
+	if (jpeg->mode == MTK_JPEG_DEC)
+		clk_disable_unprepare(jpeg->clk_jpeg_smi);
 	mtk_smi_larb_put(jpeg->larb);
 }
 
-static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
+static irqreturn_t mtk_jpeg_irq(int irq, void *priv)
 {
 	struct mtk_jpeg_dev *jpeg = priv;
 	struct mtk_jpeg_ctx *ctx;
 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
 	struct mtk_jpeg_src_buf *jpeg_src_buf;
 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
-	u32	dec_irq_ret;
-	u32 dec_ret;
+	u32 irq_ret;
+	u32 ret, result_size;
 	int i;
 
-	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
-	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
 	if (!ctx) {
 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
@@ -909,32 +1206,89 @@ static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
 
-	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
-		mtk_jpeg_dec_reset(jpeg->dec_reg_base);
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		ret = mtk_jpeg_enc_get_int_status(jpeg->reg_base);
+		irq_ret = mtk_jpeg_enc_enum_result(jpeg->reg_base, ret);
 
-	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
-		dev_err(jpeg->dev, "decode failed\n");
-		goto dec_end;
-	}
+		if (irq_ret >= MTK_JPEG_ENC_RESULT_STALL)
+			mtk_jpeg_enc_reset(jpeg->reg_base);
+
+		if (irq_ret != MTK_JPEG_ENC_RESULT_DONE) {
+			dev_err(jpeg->dev, "encode failed\n");
+			goto irq_end;
+		}
+
+		result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
+		vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
+				      result_size);
+	} else {
+		ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
+		irq_ret = mtk_jpeg_dec_enum_result(ret);
+
+		if (irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
+			mtk_jpeg_dec_reset(jpeg->reg_base);
 
-	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
-		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
-				      jpeg_src_buf->dec_param.comp_size[i]);
+		if (irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
+			dev_err(jpeg->dev, "decode failed\n");
+			goto irq_end;
+		}
+
+		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
+			vb2_set_plane_payload
+				(&dst_buf->vb2_buf, i,
+				 jpeg_src_buf->dec_param.comp_size[i]);
+	}
 
 	buf_state = VB2_BUF_STATE_DONE;
 
-dec_end:
+irq_end:
 	v4l2_m2m_buf_done(src_buf, buf_state);
 	v4l2_m2m_buf_done(dst_buf, buf_state);
 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
 	return IRQ_HANDLED;
 }
 
-static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
+static void mtk_jpeg_set_enc_default_params(struct mtk_jpeg_ctx *ctx)
+{
+	struct mtk_jpeg_q_data *q = &ctx->out_q;
+
+	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
+
+	ctx->colorspace = V4L2_COLORSPACE_JPEG,
+	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
+	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
+
+	q->w = MTK_JPEG_MIN_WIDTH;
+	q->h = MTK_JPEG_MIN_HEIGHT;
+
+	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
+				      MTK_JPEG_FMT_TYPE_OUTPUT);
+
+	q->w = clamp(round_up(q->w, 32), MTK_JPEG_MIN_WIDTH,
+		     MTK_JPEG_MAX_WIDTH);
+	q->h = clamp(round_up(q->h, 8), MTK_JPEG_MIN_HEIGHT,
+		     MTK_JPEG_MAX_HEIGHT);
+
+	q->sizeimage[0] = q->w * q->h * 2;
+	q->bytesperline[0] = q->w * 2;
+
+	q = &ctx->cap_q;
+	q->w = MTK_JPEG_MIN_WIDTH;
+	q->h = MTK_JPEG_MIN_HEIGHT;
+	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
+				      MTK_JPEG_FMT_TYPE_CAPTURE);
+	q->bytesperline[0] = 0;
+	q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
+}
+
+static void mtk_jpeg_set_dec_default_params(struct mtk_jpeg_ctx *ctx)
 {
 	struct mtk_jpeg_q_data *q = &ctx->out_q;
 	int i;
 
+	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
+
 	ctx->colorspace = V4L2_COLORSPACE_JPEG,
 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
@@ -990,7 +1344,18 @@ static int mtk_jpeg_open(struct file *file)
 		goto error;
 	}
 
-	mtk_jpeg_set_default_params(ctx);
+	ret = mtk_jpeg_ctrls_setup(ctx);
+	if (ret) {
+		v4l2_err(&jpeg->v4l2_dev, "Failed to setup controls() (%d)\n",
+			 ret);
+		goto error;
+	}
+
+	if (jpeg->mode == MTK_JPEG_ENC)
+		mtk_jpeg_set_enc_default_params(ctx);
+	else
+		mtk_jpeg_set_dec_default_params(ctx);
+
 	mutex_unlock(&jpeg->lock);
 	return 0;
 
@@ -1010,6 +1375,7 @@ static int mtk_jpeg_release(struct file *file)
 
 	mutex_lock(&jpeg->lock);
 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
 	v4l2_fh_del(&ctx->fh);
 	v4l2_fh_exit(&ctx->fh);
 	kfree(ctx);
@@ -1043,19 +1409,24 @@ static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
 
 	jpeg->larb = &pdev->dev;
 
-	jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
-	if (IS_ERR(jpeg->clk_jdec))
-		return PTR_ERR(jpeg->clk_jdec);
+	if (jpeg->mode == MTK_JPEG_ENC) {
+		jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgenc");
+		return PTR_ERR_OR_ZERO(jpeg->clk_jpeg);
+	}
+
+	jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgdec");
+	if (IS_ERR(jpeg->clk_jpeg))
+		return PTR_ERR(jpeg->clk_jpeg);
 
-	jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
-	return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
+	jpeg->clk_jpeg_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
+	return PTR_ERR_OR_ZERO(jpeg->clk_jpeg_smi);
 }
 
 static int mtk_jpeg_probe(struct platform_device *pdev)
 {
 	struct mtk_jpeg_dev *jpeg;
 	struct resource *res;
-	int dec_irq;
+	int jpeg_irq;
 	int ret;
 
 	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
@@ -1065,28 +1436,26 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
 	mutex_init(&jpeg->lock);
 	spin_lock_init(&jpeg->hw_lock);
 	jpeg->dev = &pdev->dev;
+	jpeg->mode = (enum mtk_jpeg_mode)of_device_get_match_data(jpeg->dev);
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
-	if (IS_ERR(jpeg->dec_reg_base)) {
-		ret = PTR_ERR(jpeg->dec_reg_base);
+	jpeg->reg_base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(jpeg->reg_base)) {
+		ret = PTR_ERR(jpeg->reg_base);
 		return ret;
 	}
 
-	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-	dec_irq = platform_get_irq(pdev, 0);
-	if (!res || dec_irq < 0) {
-		dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
-		ret = -EINVAL;
-		return ret;
+	jpeg_irq = platform_get_irq(pdev, 0);
+	if (jpeg_irq < 0) {
+		dev_err(&pdev->dev, "Failed to get jpeg_irq %d.\n", jpeg_irq);
+		return jpeg_irq;
 	}
 
-	ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
+	ret = devm_request_irq(&pdev->dev, jpeg_irq, mtk_jpeg_irq, 0,
 			       pdev->name, jpeg);
 	if (ret) {
-		dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
-			dec_irq, ret);
-		ret = -EINVAL;
+		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
+			jpeg_irq, ret);
 		goto err_req_irq;
 	}
 
@@ -1110,33 +1479,35 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
 		goto err_m2m_init;
 	}
 
-	jpeg->dec_vdev = video_device_alloc();
-	if (!jpeg->dec_vdev) {
+	jpeg->vfd_jpeg = video_device_alloc();
+	if (!jpeg->vfd_jpeg) {
 		ret = -ENOMEM;
-		goto err_dec_vdev_alloc;
+		goto err_vfd_jpeg_alloc;
 	}
-	snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
-		 "%s-dec", MTK_JPEG_NAME);
-	jpeg->dec_vdev->fops = &mtk_jpeg_fops;
-	jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
-	jpeg->dec_vdev->minor = -1;
-	jpeg->dec_vdev->release = video_device_release;
-	jpeg->dec_vdev->lock = &jpeg->lock;
-	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
-	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
-	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
+	snprintf(jpeg->vfd_jpeg->name, sizeof(jpeg->vfd_jpeg->name),
+		 "%s-%s", MTK_JPEG_NAME,
+		 jpeg->mode == MTK_JPEG_ENC ? "enc" : "dec");
+	jpeg->vfd_jpeg->fops = &mtk_jpeg_fops;
+	jpeg->vfd_jpeg->ioctl_ops = &mtk_jpeg_ioctl_ops;
+	jpeg->vfd_jpeg->minor = -1;
+	jpeg->vfd_jpeg->release = video_device_release;
+	jpeg->vfd_jpeg->lock = &jpeg->lock;
+	jpeg->vfd_jpeg->v4l2_dev = &jpeg->v4l2_dev;
+	jpeg->vfd_jpeg->vfl_dir = VFL_DIR_M2M;
+	jpeg->vfd_jpeg->device_caps = V4L2_CAP_STREAMING |
 				      V4L2_CAP_VIDEO_M2M_MPLANE;
 
-	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
+	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);
 	if (ret) {
 		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
-		goto err_dec_vdev_register;
+		goto err_vfd_jpeg_register;
 	}
 
-	video_set_drvdata(jpeg->dec_vdev, jpeg);
+	video_set_drvdata(jpeg->vfd_jpeg, jpeg);
 	v4l2_info(&jpeg->v4l2_dev,
-		  "decoder device registered as /dev/video%d (%d,%d)\n",
-		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
+		  "jpeg device %d registered as /dev/video%d (%d,%d)\n",
+		  jpeg->mode, jpeg->vfd_jpeg->num, VIDEO_MAJOR,
+		  jpeg->vfd_jpeg->minor);
 
 	platform_set_drvdata(pdev, jpeg);
 
@@ -1144,10 +1515,10 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
 
 	return 0;
 
-err_dec_vdev_register:
-	video_device_release(jpeg->dec_vdev);
+err_vfd_jpeg_register:
+	video_device_release(jpeg->vfd_jpeg);
 
-err_dec_vdev_alloc:
+err_vfd_jpeg_alloc:
 	v4l2_m2m_release(jpeg->m2m_dev);
 
 err_m2m_init:
@@ -1167,8 +1538,8 @@ static int mtk_jpeg_remove(struct platform_device *pdev)
 	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
 
 	pm_runtime_disable(&pdev->dev);
-	video_unregister_device(jpeg->dec_vdev);
-	video_device_release(jpeg->dec_vdev);
+	video_unregister_device(jpeg->vfd_jpeg);
+	video_device_release(jpeg->vfd_jpeg);
 	v4l2_m2m_release(jpeg->m2m_dev);
 	v4l2_device_unregister(&jpeg->v4l2_dev);
 
@@ -1179,7 +1550,11 @@ static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
 {
 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
 
-	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
+	if (jpeg->mode == MTK_JPEG_ENC)
+		mtk_jpeg_enc_reset(jpeg->reg_base);
+	else
+		mtk_jpeg_dec_reset(jpeg->reg_base);
+
 	mtk_jpeg_clk_off(jpeg);
 
 	return 0;
@@ -1190,7 +1565,10 @@ static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
 
 	mtk_jpeg_clk_on(jpeg);
-	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
+	if (jpeg->mode == MTK_JPEG_ENC)
+		mtk_jpeg_enc_reset(jpeg->reg_base);
+	else
+		mtk_jpeg_dec_reset(jpeg->reg_base);
 
 	return 0;
 }
@@ -1226,11 +1604,15 @@ static const struct dev_pm_ops mtk_jpeg_pm_ops = {
 static const struct of_device_id mtk_jpeg_match[] = {
 	{
 		.compatible = "mediatek,mt8173-jpgdec",
-		.data       = NULL,
+		.data       = (void *)MTK_JPEG_DEC,
 	},
 	{
 		.compatible = "mediatek,mt2701-jpgdec",
-		.data       = NULL,
+		.data       = (void *)MTK_JPEG_DEC,
+	},
+	{
+		.compatible = "mediatek,mtk-jpgenc",
+		.data       = (void *)MTK_JPEG_ENC,
 	},
 	{},
 };
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
index 9bbd615b1067..02a387d8be5d 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
@@ -3,6 +3,7 @@
  * Copyright (c) 2016 MediaTek Inc.
  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
  *         Rick Chang <rick.chang@mediatek.com>
+ *         Xia Jiang <xia.jiang@mediatek.com>
  */
 
 #ifndef _MTK_JPEG_CORE_H
@@ -19,16 +20,19 @@
 
 #define MTK_JPEG_FMT_FLAG_DEC_OUTPUT	BIT(0)
 #define MTK_JPEG_FMT_FLAG_DEC_CAPTURE	BIT(1)
+#define MTK_JPEG_FMT_FLAG_ENC_OUTPUT	BIT(2)
+#define MTK_JPEG_FMT_FLAG_ENC_CAPTURE	BIT(3)
 
 #define MTK_JPEG_FMT_TYPE_OUTPUT	1
 #define MTK_JPEG_FMT_TYPE_CAPTURE	2
 
 #define MTK_JPEG_MIN_WIDTH	32U
 #define MTK_JPEG_MIN_HEIGHT	32U
-#define MTK_JPEG_MAX_WIDTH	8192U
-#define MTK_JPEG_MAX_HEIGHT	8192U
+#define MTK_JPEG_MAX_WIDTH	65535U
+#define MTK_JPEG_MAX_HEIGHT	65535U
 
 #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
+#define MTK_JPEG_DEFAULT_EXIF_SIZE	(64 * 1024)
 
 /**
  * enum mtk_jpeg_ctx_state - contex state of jpeg
@@ -39,6 +43,14 @@ enum mtk_jpeg_ctx_state {
 	MTK_JPEG_SOURCE_CHANGE,
 };
 
+/**
+ * enum mtk_jpeg_mode - mode of jpeg
+ */
+enum mtk_jpeg_mode {
+	MTK_JPEG_ENC,
+	MTK_JPEG_DEC,
+};
+
 /**
  * struct mt_jpeg - JPEG IP abstraction
  * @lock:		the mutex protecting this structure
@@ -48,11 +60,12 @@ enum mtk_jpeg_ctx_state {
  * @v4l2_dev:		v4l2 device for mem2mem mode
  * @m2m_dev:		v4l2 mem2mem device data
  * @alloc_ctx:		videobuf2 memory allocator's context
- * @dec_vdev:		video device node for decoder mem2mem mode
- * @dec_reg_base:	JPEG registers mapping
- * @clk_jdec:		JPEG hw working clock
- * @clk_jdec_smi:	JPEG SMI bus clock
+ * @vfd_jpeg:		video device node for jpeg mem2mem mode
+ * @reg_base:		JPEG registers mapping
+ * @clk_jpeg:		JPEG hw working clock
+ * @clk_jpeg_smi:	JPEG SMI bus clock
  * @larb:		SMI device
+ * @mode:		compression (encode) operation or decompression (decode)
  */
 struct mtk_jpeg_dev {
 	struct mutex		lock;
@@ -62,11 +75,12 @@ struct mtk_jpeg_dev {
 	struct v4l2_device	v4l2_dev;
 	struct v4l2_m2m_dev	*m2m_dev;
 	void			*alloc_ctx;
-	struct video_device	*dec_vdev;
-	void __iomem		*dec_reg_base;
-	struct clk		*clk_jdec;
-	struct clk		*clk_jdec_smi;
+	struct video_device	*vfd_jpeg;
+	void __iomem		*reg_base;
+	struct clk		*clk_jpeg;
+	struct clk		*clk_jpeg_smi;
 	struct device		*larb;
+	enum mtk_jpeg_mode	mode;
 };
 
 /**
@@ -113,6 +127,7 @@ struct mtk_jpeg_q_data {
  * @cap_q:		destination (capture) queue queue information
  * @fh:			V4L2 file handle
  * @state:		state of the context
+ * @ctrl_hdl:		controls handler
  * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
  * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
  * @quantization: enum v4l2_quantization, colorspace quantization
@@ -124,6 +139,10 @@ struct mtk_jpeg_ctx {
 	struct mtk_jpeg_q_data		cap_q;
 	struct v4l2_fh			fh;
 	enum mtk_jpeg_ctx_state		state;
+	u8				enable_exif;
+	u8				enc_quality;
+	u8				restart_interval;
+	struct v4l2_ctrl_handler	ctrl_hdl;
 
 	enum v4l2_colorspace colorspace;
 	enum v4l2_ycbcr_encoding ycbcr_enc;
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
index 1cc37dbfc8e7..ce263db5f30a 100644
--- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
@@ -3,10 +3,11 @@
  * Copyright (c) 2016 MediaTek Inc.
  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
  *         Rick Chang <rick.chang@mediatek.com>
+ *         Xia Jiang <xia.jiang@mediatek.com>
  */
 
-#ifndef _MTK_JPEG_HW_H
-#define _MTK_JPEG_HW_H
+#ifndef _MTK_JPEG_DEC_HW_H
+#define _MTK_JPEG_DEC_HW_H
 
 #include <media/videobuf2-core.h>
 
@@ -75,4 +76,4 @@ void mtk_jpeg_dec_set_config(void __iomem *base,
 void mtk_jpeg_dec_reset(void __iomem *dec_reg_base);
 void mtk_jpeg_dec_start(void __iomem *dec_reg_base);
 
-#endif /* _MTK_JPEG_HW_H */
+#endif /* _MTK_JPEG_DEC_HW_H */
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
new file mode 100644
index 000000000000..da3e7a83a80a
--- /dev/null
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
@@ -0,0 +1,273 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2019 MediaTek Inc.
+ * Author: Xia Jiang <xia.jiang@mediatek.com>
+ *
+ */
+
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <media/videobuf2-core.h>
+
+#include "mtk_jpeg_enc_hw.h"
+
+static struct mtk_jpeg_enc_qlt mtk_jpeg_enc_quality[] = {
+	{
+		.quality_param		= 97,
+		.hardware_value	= JPEG_ENC_QUALITY_Q97,
+	},
+	{
+		.quality_param		= 95,
+		.hardware_value	= JPEG_ENC_QUALITY_Q95,
+	},
+	{
+		.quality_param		= 92,
+		.hardware_value	= JPEG_ENC_QUALITY_Q92,
+	},
+	{
+		.quality_param		= 90,
+		.hardware_value	= JPEG_ENC_QUALITY_Q90,
+	},
+	{
+		.quality_param		= 87,
+		.hardware_value	= JPEG_ENC_QUALITY_Q87,
+	},
+	{
+		.quality_param		= 84,
+		.hardware_value	= JPEG_ENC_QUALITY_Q84,
+	},
+	{
+		.quality_param		= 82,
+		.hardware_value	= JPEG_ENC_QUALITY_Q82,
+	},
+	{
+		.quality_param		= 80,
+		.hardware_value	= JPEG_ENC_QUALITY_Q80,
+	},
+	{
+		.quality_param		= 74,
+		.hardware_value	= JPEG_ENC_QUALITY_Q74,
+	},
+	{
+		.quality_param		= 68,
+		.hardware_value	= JPEG_ENC_QUALITY_Q68,
+	},
+	{
+		.quality_param		= 64,
+		.hardware_value	= JPEG_ENC_QUALITY_Q64,
+	},
+	{
+		.quality_param		= 60,
+		.hardware_value	= JPEG_ENC_QUALITY_Q60,
+	},
+	{
+		.quality_param		= 48,
+		.hardware_value	= JPEG_ENC_QUALITY_Q48,
+	},
+	{
+		.quality_param		= 39,
+		.hardware_value	= JPEG_ENC_QUALITY_Q39,
+	},
+	{
+		.quality_param		= 34,
+		.hardware_value	= JPEG_ENC_QUALITY_Q34,
+	},
+};
+
+#define MTK_JPEG_ENC_NUM_QUALITY ARRAY_SIZE(mtk_jpeg_enc_quality)
+
+void mtk_jpeg_enc_reset(void __iomem *base)
+{
+	writel(0x00, base + JPEG_ENC_RSTB);
+	writel(JPEG_ENC_RESET_BIT, base + JPEG_ENC_RSTB);
+	writel(0x00, base + JPEG_ENC_CODEC_SEL);
+}
+
+u32 mtk_jpeg_enc_get_int_status(void __iomem *base)
+{
+	u32 ret;
+
+	ret = readl(base + JPEG_ENC_INT_STS) &
+		    JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
+	if (ret)
+		writel(0, base + JPEG_ENC_INT_STS);
+
+	return ret;
+}
+
+u32 mtk_jpeg_enc_get_file_size(void __iomem *base)
+{
+	return readl(base + JPEG_ENC_DMA_ADDR0) -
+	       readl(base + JPEG_ENC_DST_ADDR0);
+}
+
+u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status)
+{
+	if (irq_status & JPEG_ENC_INT_STATUS_DONE)
+		return MTK_JPEG_ENC_RESULT_DONE;
+	else if (irq_status & JPEG_ENC_INT_STATUS_STALL)
+		return MTK_JPEG_ENC_RESULT_STALL;
+	else if (irq_status & JPEG_ENC_INT_STATUS_VCODEC_IRQ)
+		return MTK_JPEG_ENC_RESULT_VCODEC_IRQ;
+	return MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN;
+}
+
+void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height)
+{
+	u32 value;
+
+	value = JPEG_ENC_WIDTH_HEIGHT(width, height);
+	writel(value, base + JPEG_ENC_IMG_SIZE);
+}
+
+void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format)
+{
+	u32 yuv_format, value;
+
+	yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
+	switch (enc_format) {
+	case V4L2_PIX_FMT_YUYV:
+		yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
+		break;
+	case V4L2_PIX_FMT_YVYU:
+		yuv_format = JPEG_ENC_YUV_FORMAT_YVYU;
+		break;
+	case V4L2_PIX_FMT_NV12M:
+		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;
+		break;
+	case V4L2_PIX_FMT_NV21M:
+		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;
+		break;
+	default:
+		break;
+	}
+
+	value = readl(base + JPEG_ENC_CTRL);
+	value &= ~JPEG_ENC_CTRL_YUV_FORMAT_MASK;
+	value |= JPEG_ENC_FORMAT(yuv_format);
+	writel(value, base + JPEG_ENC_CTRL);
+}
+
+void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
+			      u32 height)
+{
+	u32 blk_num;
+	u32 is_420;
+	u32 padding_width;
+	u32 padding_height;
+
+	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
+		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;
+	padding_width = round_up(width, 16);
+	padding_height = round_up(height, is_420 ? 16 : 8);
+
+	blk_num = (padding_width >> 4) * (padding_height >> (is_420 ? 4 : 3)) *
+		  (is_420 ? 6 : 4) - 1;
+
+	writel(blk_num, base + JPEG_ENC_BLK_NUM);
+}
+
+void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
+			     u32 height, u32 bytesperline)
+{
+	u32 width_even;
+	u32 is_420;
+	u32 img_stride;
+	u32 mem_stride;
+
+	width_even = round_up(width, 2);
+	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
+		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;
+	if (!is_420)
+		width_even = width_even << 1;
+	img_stride = round_up(width_even, (is_420 ? 16 : 32));
+	mem_stride = is_420 ? bytesperline : round_up(width_even, 32);
+
+	writel(img_stride, base + JPEG_ENC_IMG_STRIDE);
+	writel(mem_stride, base + JPEG_ENC_STRIDE);
+}
+
+void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
+			       u32 src_addr_c)
+{
+	writel(src_addr, base + JPEG_ENC_SRC_LUMA_ADDR);
+	writel(src_addr_c, base + JPEG_ENC_SRC_CHROMA_ADDR);
+}
+
+void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
+			       u32 stall_size, u32 init_offset,
+			       u32 offset_mask)
+{
+	writel(JPEG_ENC_INIT_OFFSET(init_offset), base + JPEG_ENC_OFFSET_ADDR);
+	writel(JPEG_ENC_OFFSET_MASK(offset_mask),
+	       base + JPEG_ENC_BYTE_OFFSET_MASK);
+	writel(JPEG_ENC_DST_ADDR(dst_addr), base + JPEG_ENC_DST_ADDR0);
+	writel(JPEG_ENC_STALL_ADDR(dst_addr, stall_size),
+	       base + JPEG_ENC_STALL_ADDR0);
+}
+
+static void mtk_jpeg_enc_set_quality(void __iomem *base, u32 quality)
+{
+	u32 value;
+	u32 i, enc_quality;
+
+	enc_quality = mtk_jpeg_enc_quality[0].hardware_value;
+	for (i = 0; i < MTK_JPEG_ENC_NUM_QUALITY; i++) {
+		if (quality >= mtk_jpeg_enc_quality[i].quality_param) {
+			enc_quality = mtk_jpeg_enc_quality[i].hardware_value;
+			break;
+		}
+	}
+
+	value = readl(base + JPEG_ENC_QUALITY);
+	value = JPEG_ENC_SET_QUALITY(value, enc_quality);
+	writel(value, base + JPEG_ENC_QUALITY);
+}
+
+static void mtk_jpeg_enc_set_restart_interval(void __iomem *base,
+					      u32 restart_interval)
+{
+	u32 value;
+
+	value = readl(base + JPEG_ENC_CTRL);
+	if (restart_interval)
+		value |= JPEG_ENC_CTRL_RESTART_EN_BIT;
+	else
+		value &= ~JPEG_ENC_CTRL_RESTART_EN_BIT;
+	writel(value, base + JPEG_ENC_CTRL);
+	writel(restart_interval, base + JPEG_ENC_RST_MCU_NUM);
+}
+
+static void mtk_jpeg_enc_set_encode_mode(void __iomem *base, u32 exif_en)
+{
+	u32 value;
+
+	value = readl(base + JPEG_ENC_CTRL);
+	value &= ~JPEG_ENC_CTRL_FILE_FORMAT_BIT;
+	writel(value, base + JPEG_ENC_CTRL);
+
+	if (exif_en) {
+		value = readl(base + JPEG_ENC_CTRL);
+		value |= JPEG_ENC_EN_JFIF_EXIF;
+		writel(value, base + JPEG_ENC_CTRL);
+	}
+}
+
+void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
+			       u32 restart_interval)
+{
+	mtk_jpeg_enc_set_quality(base, quality);
+
+	mtk_jpeg_enc_set_restart_interval(base, restart_interval);
+
+	mtk_jpeg_enc_set_encode_mode(base, exif_en);
+}
+
+void mtk_jpeg_enc_start(void __iomem *base)
+{
+	u32 value;
+
+	value = readl(base + JPEG_ENC_CTRL);
+	value |= JPEG_ENC_CTRL_INT_EN_BIT | JPEG_ENC_CTRL_ENABLE_BIT;
+	writel(value, base + JPEG_ENC_CTRL);
+}
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
new file mode 100644
index 000000000000..d0d7d218a0c2
--- /dev/null
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2019 MediaTek Inc.
+ * Author: Xia Jiang <xia.jiang@mediatek.com>
+ *
+ */
+
+#ifndef _MTK_JPEG_ENC_HW_H
+#define _MTK_JPEG_ENC_HW_H
+
+#include <media/videobuf2-core.h>
+
+#include "mtk_jpeg_core.h"
+#include "mtk_jpeg_enc_reg.h"
+
+enum {
+	MTK_JPEG_ENC_RESULT_DONE		= 0,
+	MTK_JPEG_ENC_RESULT_STALL,
+	MTK_JPEG_ENC_RESULT_VCODEC_IRQ,
+	MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN
+};
+
+/**
+ * struct mtk_jpeg_enc_qlt - JPEG encoder quality data
+ * @quality_param:	quality value
+ * @hardware_value:hardware value of quality
+ */
+struct mtk_jpeg_enc_qlt {
+	u8	quality_param;
+	u8	hardware_value;
+};
+
+/**
+ * struct mt_jpeg_enc_bs - JPEG encoder bitstream  buffer
+ * @dma_addr:			JPEG encoder destination address
+ * @size:			JPEG encoder bistream size
+ * @dma_addr_offset:		JPEG encoder offset address
+ * @dma_addr_offsetmask:	JPEG encoder destination address offset mask
+ */
+struct mtk_jpeg_enc_bs {
+	dma_addr_t	dma_addr;
+	size_t		size;
+	u32			dma_addr_offset;
+	u32			dma_addr_offsetmask;
+};
+
+/**
+ * struct mtk_jpeg_mem - JPEG memory
+ * @dma_addr:		memory address
+ * @size:		memory size
+ */
+struct mtk_jpeg_mem {
+	dma_addr_t	dma_addr;
+	size_t		size;
+};
+
+/**
+ * struct mtk_jpeg_enc_fb - JPEG encoder frame buffer
+ * @fb_addr:		frmae buffer memory information
+ * @num_planes:		number of planes
+ */
+struct mtk_jpeg_enc_fb {
+	struct mtk_jpeg_mem	fb_addr[MTK_JPEG_COMP_MAX];
+	u32			num_planes;
+};
+
+void mtk_jpeg_enc_reset(void __iomem *base);
+u32 mtk_jpeg_enc_get_int_status(void __iomem *base);
+u32 mtk_jpeg_enc_get_file_size(void __iomem *base);
+u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status);
+void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height);
+void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format);
+void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
+			      u32 height);
+void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
+			     u32 height, u32 bytesperline);
+void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
+			       u32 src_addr_c);
+void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
+			       u32 stall_size, u32 init_offset,
+			       u32 offset_mask);
+void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
+			       u32 restart_interval);
+void mtk_jpeg_enc_start(void __iomem *enc_reg_base);
+
+#endif /* _MTK_JPEG_ENC_HW_H */
diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
new file mode 100644
index 000000000000..cec3631addc5
--- /dev/null
+++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2019 MediaTek Inc.
+ * Author: Xia Jiang <xia.jiang@mediatek.com>
+ *
+ */
+#ifndef _MTK_JPEG_ENC_REG_H
+#define _MTK_JPEG_ENC_REG_H
+
+#define JPEG_ENC_INT_STATUS_DONE			BIT(0)
+#define JPEG_ENC_INT_STATUS_STALL			BIT(1)
+#define JPEG_ENC_INT_STATUS_VCODEC_IRQ		BIT(4)
+#define JPEG_ENC_INT_STATUS_MASK_ALLIRQ		0x13
+
+#define JPEG_ENC_DST_ADDR_OFFSET_MASK GENMASK(3, 0)
+
+#define JPEG_ENC_CTRL_YUV_FORMAT_MASK 0x18
+#define JPEG_ENC_CTRL_RESTART_EN_BIT            BIT(10)
+#define JPEG_ENC_CTRL_FILE_FORMAT_BIT           BIT(5)
+#define JPEG_ENC_EN_JFIF_EXIF                   BIT(5)
+#define JPEG_ENC_CTRL_INT_EN_BIT                BIT(2)
+#define JPEG_ENC_CTRL_ENABLE_BIT                BIT(0)
+#define JPEG_ENC_RESET_BIT                      BIT(0)
+
+#define JPEG_ENC_YUV_FORMAT_YUYV 0
+#define JPEG_ENC_YUV_FORMAT_YVYU 1
+#define JPEG_ENC_YUV_FORMAT_NV12 2
+#define JEPG_ENC_YUV_FORMAT_NV21 3
+
+#define JPEG_ENC_QUALITY_Q60 0x0
+#define JPEG_ENC_QUALITY_Q80 0x1
+#define JPEG_ENC_QUALITY_Q90 0x2
+#define JPEG_ENC_QUALITY_Q95 0x3
+#define JPEG_ENC_QUALITY_Q39 0x4
+#define JPEG_ENC_QUALITY_Q68 0x5
+#define JPEG_ENC_QUALITY_Q84 0x6
+#define JPEG_ENC_QUALITY_Q92 0x7
+#define JPEG_ENC_QUALITY_Q48 0x8
+#define JPEG_ENC_QUALITY_Q74 0xa
+#define JPEG_ENC_QUALITY_Q87 0xb
+#define JPEG_ENC_QUALITY_Q34 0xc
+#define JPEG_ENC_QUALITY_Q64 0xe
+#define JPEG_ENC_QUALITY_Q82 0xf
+#define JPEG_ENC_QUALITY_Q97 0x10
+
+#define JPEG_ENC_RSTB				0x100
+#define JPEG_ENC_CTRL				0x104
+#define JPEG_ENC_QUALITY			0x108
+#define JPEG_ENC_BLK_NUM			0x10C
+#define JPEG_ENC_BLK_CNT			0x110
+#define JPEG_ENC_INT_STS			0x11c
+#define JPEG_ENC_DST_ADDR0			0x120
+#define JPEG_ENC_DMA_ADDR0			0x124
+#define JPEG_ENC_STALL_ADDR0		0x128
+#define JPEG_ENC_OFFSET_ADDR		0x138
+#define JPEG_ENC_RST_MCU_NUM		0x150
+#define JPEG_ENC_IMG_SIZE			0x154
+#define JPEG_ENC_DEBUG_INFO0		0x160
+#define JPEG_ENC_DEBUG_INFO1		0x164
+#define JPEG_ENC_TOTAL_CYCLE		0x168
+#define JPEG_ENC_BYTE_OFFSET_MASK	0x16c
+#define JPEG_ENC_SRC_LUMA_ADDR		0x170
+#define JPEG_ENC_SRC_CHROMA_ADDR	0x174
+#define JPEG_ENC_STRIDE				0x178
+#define JPEG_ENC_IMG_STRIDE			0x17c
+#define JPEG_ENC_DCM_CTRL			0x300
+#define JPEG_ENC_CODEC_SEL			0x314
+#define JPEG_ENC_ULTRA_THRES		0x318
+
+#define JPEG_ENC_FORMAT(x)		(((x) & 3) << 3)
+#define JPEG_ENC_WIDTH_HEIGHT(w, h)	(((w) << 16) | (h))
+#define JPEG_ENC_INIT_OFFSET(x)		((x) & (~0xf))
+#define JPEG_ENC_OFFSET_MASK(x)		((x) & 0xf)
+#define JPEG_ENC_DST_ADDR(x)		((x) & (~0xf))
+#define JPEG_ENC_STALL_ADDR(x, y)		(((x) + (y)) & (~0xf))
+#define JPEG_ENC_SET_QUALITY(x, y)	(((x) & 0xffff0000) | (y))
+
+#endif /* _MTK_JPEG_ENC_REG_H */
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-03-03 12:34 ` [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature Xia Jiang
@ 2020-03-06 11:23   ` Tomasz Figa
  2020-04-16  4:03     ` Xia Jiang
  0 siblings, 1 reply; 18+ messages in thread
From: Tomasz Figa @ 2020-03-06 11:23 UTC (permalink / raw)
  To: Xia Jiang
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, linux-media, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, Marek Szyprowski,
	srv_heupstream

Hi Xia,

On Tue, Mar 03, 2020 at 08:34:46PM +0800, Xia Jiang wrote:
> Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
> decode and encode have great similarities with function operation.

Thank you for the patch. Please see my comments inline.

[snip]

> @@ -51,6 +54,42 @@ static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
>  		.v_align	= 3,
>  		.flags		= MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
>  	},
> +	{
> +		.fourcc		= V4L2_PIX_FMT_NV12M,
> +		.h_sample	= {4, 2, 2},
> +		.v_sample	= {4, 2, 2},
> +		.colplanes	= 2,
> +		.h_align	= 4,
> +		.v_align	= 4,
> +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> +	},
> +	{
> +		.fourcc		= V4L2_PIX_FMT_NV21M,
> +		.h_sample	= {4, 2, 2},
> +		.v_sample	= {4, 2, 2},
> +		.colplanes	= 2,
> +		.h_align	= 4,
> +		.v_align	= 4,
> +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> +	},
> +	{
> +		.fourcc		= V4L2_PIX_FMT_YUYV,
> +		.h_sample	= {4, 2, 2},
> +		.v_sample	= {4, 4, 4},
> +		.colplanes	= 1,
> +		.h_align	= 4,
> +		.v_align	= 3,
> +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> +	},
> +	{
> +		.fourcc		= V4L2_PIX_FMT_YVYU,
> +		.h_sample	= {4, 2, 2},
> +		.v_sample	= {4, 4, 4},
> +		.colplanes	= 1,
> +		.h_align	= 4,
> +		.v_align	= 3,
> +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> +	},
>  };

Since the encoder and decoder don't seem to share almost any format,
perhaps it would make sense to just have separate format arrays for both?

>  
>  #define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
> @@ -70,6 +109,11 @@ struct mtk_jpeg_src_buf {
>  static int debug;
>  module_param(debug, int, 0644);
>  
> +static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
> +{
> +	return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
> +}
> +
>  static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
>  {
>  	return container_of(fh, struct mtk_jpeg_ctx, fh);
> @@ -86,14 +130,69 @@ static int mtk_jpeg_querycap(struct file *file, void *priv,
>  {
>  	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
>  
> -	strscpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
> -	strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
> +	strscpy(cap->driver, MTK_JPEG_NAME, sizeof(cap->driver));
> +	if (jpeg->mode ==  MTK_JPEG_ENC)
> +		strscpy(cap->card, MTK_JPEG_NAME " encoder", sizeof(cap->card));
> +	else
> +		strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
>  	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
>  		 dev_name(jpeg->dev));
>  
>  	return 0;
>  }
>  
> +static int vidioc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +	struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
> +	int ret = 0;
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_JPEG_RESTART_INTERVAL:
> +		ctx->restart_interval = ctrl->val;
> +		break;
> +	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
> +		ctx->enc_quality = ctrl->val;
> +		break;
> +	case V4L2_CID_JPEG_ACTIVE_MARKER:
> +		ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
> +		break;
> +	}
> +
> +	return ret;

Wouldn't it be enough to just return 0 directly and remove ret?

> +}
> +
> +static const struct v4l2_ctrl_ops mtk_jpeg_ctrl_ops = {
> +	.s_ctrl = vidioc_jpeg_s_ctrl,
> +};
> +
> +static int mtk_jpeg_ctrls_setup(struct mtk_jpeg_ctx *ctx)
> +{
> +	const struct v4l2_ctrl_ops *ops = &mtk_jpeg_ctrl_ops;
> +	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> +
> +	v4l2_ctrl_handler_init(handler, 3);
> +
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL,
> +				  0, 100, 1, 0);
> +		v4l2_ctrl_new_std(handler, ops,
> +				  V4L2_CID_JPEG_COMPRESSION_QUALITY, 48, 100, 1,
> +				  90);
> +		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
> +				  V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
> +
> +		if (handler->error) {
> +			v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> +			return handler->error;
> +		}
> +	}
> +
> +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> +
> +	return 0;
> +}
> +
>  static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
>  			     struct v4l2_fmtdesc *f, u32 type)
>  {
> @@ -118,19 +217,27 @@ static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
>  static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
>  				     struct v4l2_fmtdesc *f)
>  {
> +	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> +
>  	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
> +				 ctx->jpeg->mode == MTK_JPEG_ENC ?
> +				 MTK_JPEG_FMT_FLAG_ENC_CAPTURE :
>  				 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);

We wouldn't need this special casing if we separate the format arrays.

>  }
>  
>  static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
>  				     struct v4l2_fmtdesc *f)
>  {
> +	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> +
>  	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
> +				 ctx->jpeg->mode == MTK_JPEG_ENC ?
> +				 MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
>  				 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);

Ditto.

>  }
>  
> -static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
> -						   enum v4l2_buf_type type)
> +static struct mtk_jpeg_q_data *
> +mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx, enum v4l2_buf_type type)
>  {
>  	if (V4L2_TYPE_IS_OUTPUT(type))
>  		return &ctx->out_q;
> @@ -143,9 +250,14 @@ static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
>  {
>  	unsigned int k, fmt_flag;
>  
> -	fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> -		   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
> -		   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
> +	if (ctx->jpeg->mode ==  MTK_JPEG_ENC)
> +		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> +			   MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
> +			   MTK_JPEG_FMT_FLAG_ENC_CAPTURE;
> +	else
> +		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> +			   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
> +			   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;

Ditto.

>  
>  	for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
>  		struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
> @@ -177,9 +289,73 @@ static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
>  	}
>  }
>  
> -static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
> -				   struct mtk_jpeg_fmt *fmt,
> -				   struct mtk_jpeg_ctx *ctx, int q_type)
> +static int mtk_jpeg_try_enc_fmt_mplane(struct v4l2_format *f,
> +				       struct mtk_jpeg_fmt *fmt,
> +				       struct mtk_jpeg_ctx *ctx, int q_type)
> +{
> +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> +	int i;
> +
> +	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));

Zeroing the reserved fields is handled by the core.

> +	pix_mp->field = V4L2_FIELD_NONE;
> +
> +	if (ctx->state != MTK_JPEG_INIT) {

Is this really the right condition? As per V4L2 semantics, the format can't
be changed when the queue has buffers allocated (vb2_is_busy()). Also,
CAPTURE queue of the encoder doesn't have anything that could be changed,
as the pixelformat is always JPEG and the rest determined by the OUTPUT
queue, so perhaps it should have all the G_/TRY_/S_FMT ioctls implemented
like G_FMT?

> +		mtk_jpeg_adjust_fmt_mplane(ctx, f);

The name of thi function sounds like it adjusts something, but it
effectively just gets the current format. Why not just call the
implementation of G_FMT directly?

> +		return 0;
> +	}
> +
> +	pix_mp->num_planes = fmt->colplanes;
> +	pix_mp->pixelformat = fmt->fourcc;
> +
> +	if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {

Why the custom queue type macro? Could we use standard definitions instead?
I.e. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE.

> +		if (pix_mp->num_planes == 1U) {

nit: It is unusual to add the U prefix in comparisions like this.

> +			pix_mp->height = clamp(round_up(pix_mp->height, 8),
> +					       MTK_JPEG_MIN_HEIGHT,
> +					       MTK_JPEG_MAX_HEIGHT);
> +			pix_mp->width = clamp(round_up(pix_mp->width, 32),
> +					      MTK_JPEG_MIN_WIDTH,
> +					      MTK_JPEG_MAX_WIDTH);
> +			pix_mp->plane_fmt[0].bytesperline = pix_mp->width * 2;
> +			pix_mp->plane_fmt[0].sizeimage =
> +				pix_mp->width * pix_mp->height * 2;
> +		}
> +		if (pix_mp->num_planes == 2U) {
> +			pix_mp->height = clamp(round_up(pix_mp->height, 16),
> +					       MTK_JPEG_MIN_HEIGHT,
> +					       MTK_JPEG_MAX_HEIGHT);
> +			pix_mp->width = clamp(round_up(pix_mp->width, 16),
> +					      MTK_JPEG_MIN_WIDTH,
> +					      MTK_JPEG_MAX_WIDTH);
> +			for (i = 0; i < pix_mp->num_planes; i++) {
> +				pix_mp->plane_fmt[i].bytesperline =
> +					pix_mp->width;
> +				pix_mp->plane_fmt[i].sizeimage =
> +					pix_mp->width * pix_mp->height /
> +					(i + 1);
> +			}
> +		}
> +	} else {
> +		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
> +				       MTK_JPEG_MAX_HEIGHT);
> +		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
> +				      MTK_JPEG_MAX_WIDTH);
> +
> +		if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
> +			pix_mp->plane_fmt[0].bytesperline = 0;
> +			pix_mp->plane_fmt[0].sizeimage =
> +				round_up(pix_mp->plane_fmt[0].sizeimage, 128);
> +			if (pix_mp->plane_fmt[0].sizeimage == 0)
> +				pix_mp->plane_fmt[0].sizeimage =
> +					MTK_JPEG_DEFAULT_SIZEIMAGE;
> +		}
> +	}
> +
> +	return 0;
> +}

It doesn't look like the function above does anything specific to the
encoder. It just does generic format adjustment.

Could we just add various format parameters to the mtk_jpeg_fmt struct and
have a generic function that is shared by both encoder and decoder? That
would also let us remove the conditional code for queue type, because the
operations below actually depend on the format, not the queue.

> +
> +static int mtk_jpeg_try_dec_fmt_mplane(struct v4l2_format *f,
> +				       struct mtk_jpeg_fmt *fmt,
> +				       struct mtk_jpeg_ctx *ctx, int q_type)
>  {
>  	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
>  	int i;
> @@ -286,6 +462,7 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
>  {
>  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
>  	struct mtk_jpeg_fmt *fmt;
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
>  
>  	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
>  				   MTK_JPEG_FMT_TYPE_CAPTURE);
> @@ -299,7 +476,12 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
>  		 (fmt->fourcc >> 16 & 0xff),
>  		 (fmt->fourcc >> 24 & 0xff));
>  
> -	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
> +	if (jpeg->mode == MTK_JPEG_ENC)
> +		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
> +						   MTK_JPEG_FMT_TYPE_CAPTURE);
> +	else
> +		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
> +						   MTK_JPEG_FMT_TYPE_CAPTURE);

With a generic format adjustment function we wouldn't need this special
casing.

>  }
>  
>  static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
> @@ -307,6 +489,7 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
>  {
>  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
>  	struct mtk_jpeg_fmt *fmt;
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
>  
>  	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
>  				   MTK_JPEG_FMT_TYPE_OUTPUT);
> @@ -320,7 +503,12 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
>  		 (fmt->fourcc >> 16 & 0xff),
>  		 (fmt->fourcc >> 24 & 0xff));
>  
> -	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
> +	if (jpeg->mode == MTK_JPEG_ENC)
> +		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
> +						   MTK_JPEG_FMT_TYPE_OUTPUT);
> +	else
> +		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
> +						   MTK_JPEG_FMT_TYPE_OUTPUT);

Ditto.

>  }
>  
>  static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
> @@ -425,28 +613,48 @@ static int mtk_jpeg_g_selection(struct file *file, void *priv,
>  				struct v4l2_selection *s)
>  {
>  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
>  
> -	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> -		return -EINVAL;
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
> +			return -EINVAL;

Instead of repeating the if/else in every callback, could we instead
provide separate ops structs for encoder and decoder so they can have
separate callbacks for those that need to differ significantly?

>  
> -	switch (s->target) {
> -	case V4L2_SEL_TGT_COMPOSE:
> -	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
> -		s->r.width = ctx->out_q.w;
> -		s->r.height = ctx->out_q.h;
> -		s->r.left = 0;
> -		s->r.top = 0;
> -		break;
> -	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
> -	case V4L2_SEL_TGT_COMPOSE_PADDED:
> -		s->r.width = ctx->cap_q.w;
> -		s->r.height = ctx->cap_q.h;
> -		s->r.left = 0;
> -		s->r.top = 0;
> -		break;
> -	default:
> -		return -EINVAL;
> +		switch (s->target) {
> +		case V4L2_SEL_TGT_CROP:
> +		case V4L2_SEL_TGT_CROP_BOUNDS:
> +		case V4L2_SEL_TGT_CROP_DEFAULT:
> +			s->r.width = ctx->out_q.w;
> +			s->r.height = ctx->out_q.h;
> +			s->r.left = 0;
> +			s->r.top = 0;
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +	} else {
> +		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +			return -EINVAL;
> +
> +		switch (s->target) {
> +		case V4L2_SEL_TGT_COMPOSE:
> +		case V4L2_SEL_TGT_COMPOSE_DEFAULT:
> +			s->r.width = ctx->out_q.w;
> +			s->r.height = ctx->out_q.h;
> +			s->r.left = 0;
> +			s->r.top = 0;
> +			break;
> +		case V4L2_SEL_TGT_COMPOSE_BOUNDS:
> +		case V4L2_SEL_TGT_COMPOSE_PADDED:
> +			s->r.width = ctx->cap_q.w;
> +			s->r.height = ctx->cap_q.h;
> +			s->r.left = 0;
> +			s->r.top = 0;
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
>  	}
> +
>  	return 0;
>  }
>  
> @@ -454,20 +662,38 @@ static int mtk_jpeg_s_selection(struct file *file, void *priv,
>  				struct v4l2_selection *s)
>  {
>  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
>  
> -	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> -		return -EINVAL;
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
> +			return -EINVAL;

Ditto.

>  
> -	switch (s->target) {
> -	case V4L2_SEL_TGT_COMPOSE:
> -		s->r.left = 0;
> -		s->r.top = 0;
> -		ctx->out_q.w = s->r.width;
> -		ctx->out_q.h = s->r.height;
> -		break;
> -	default:
> -		return -EINVAL;
> +		switch (s->target) {
> +		case V4L2_SEL_TGT_CROP:
> +			s->r.left = 0;
> +			s->r.top = 0;
> +			ctx->out_q.w = s->r.width;
> +			ctx->out_q.h = s->r.height;

What happens if the userspace provides a value bigger than current format?

> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +	} else {
> +		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +			return -EINVAL;
> +
> +		switch (s->target) {
> +		case V4L2_SEL_TGT_COMPOSE:
> +			s->r.left = 0;
> +			s->r.top = 0;
> +			ctx->out_q.w = s->r.width;
> +			ctx->out_q.h = s->r.height;

This is about existing code, but perhaps needs to be fixed. Shouldn't the
decoder compose rectangle be read-only, as it means the visible size of the
decoded JPEG as per the image metadata?

> +			break;
> +		default:
> +			return -EINVAL;
> +		}
>  	}
> +
>  	return 0;
>  }
>  
> @@ -643,29 +869,42 @@ static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
>  		goto end;
>  
>  	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
> -	param = &jpeg_src_buf->dec_param;
> -	memset(param, 0, sizeof(*param));
> -
> -	if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> -		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
> -		goto end;
> -	}
> -	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
> -				      vb2_get_plane_payload(vb, 0));
> -	if (!header_valid) {
> -		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
> -		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> -		return;
> -	}
> -
> -	if (ctx->state == MTK_JPEG_INIT) {
> -		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
> -			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> +			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos");
> +			goto end;
> +		}
> +		if (ctx->state == MTK_JPEG_INIT)
> +			ctx->state = MTK_JPEG_RUNNING;

Why does the state change here? Queuing buffers is a repetitive operation.
What's special about the first buffer?

Perhaps the state should change when we start streaming on both queues?

(On a side note, I don't see a need for ctx->state in case of the encoder.
The information provided by vb2 by the means of vb2_is_busy() or
vb2_is_streaming() should be enough.)

> +	} else {
> +		param = &jpeg_src_buf->dec_param;
> +		memset(param, 0, sizeof(*param));
> +
> +		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> +			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
> +			goto end;
> +		}
> +		header_valid = mtk_jpeg_parse(param,
> +					      (u8 *)vb2_plane_vaddr(vb, 0),
> +					      vb2_get_plane_payload(vb, 0));
> +		if (!header_valid) {
> +			v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
> +			vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> +			return;
> +		}
>  
> -		mtk_jpeg_queue_src_chg_event(ctx);
> -		mtk_jpeg_set_queue_data(ctx, param);
> -		ctx->state = vb2_is_streaming(dst_vq) ?
> -				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
> +		if (ctx->state == MTK_JPEG_INIT) {
> +			struct vb2_queue *dst_vq;
> +
> +			dst_vq = v4l2_m2m_get_vq
> +					(ctx->fh.m2m_ctx,
> +					 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +			mtk_jpeg_queue_src_chg_event(ctx);
> +			mtk_jpeg_set_queue_data(ctx, param);
> +			ctx->state = vb2_is_streaming(dst_vq) ?
> +					MTK_JPEG_SOURCE_CHANGE :
> +					MTK_JPEG_RUNNING;
> +		}
>  	}
>  end:
>  	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));

It sounds like we would also benefit from just having separate callbacks
for encoder an decoder here.

The code isn't included in this patch, but start_streaming implementation
has a problem that I want to point out:

	static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
	{
		struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
		struct vb2_v4l2_buffer *vb;
		int ret = 0;
	
		ret = pm_runtime_get_sync(ctx->jpeg->dev);
		if (ret < 0)
			goto err;

This is a memory to memory device, so it doesn't need to keep running all
the time when streaming is enabled. One would normally call
pm_runtime_get_sync() before starting a frame and then pm_runtime_put()
after completing it. This would save power for the time between processing
two frames.

		return 0;
	err:
		while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
			v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED);
		return ret;
	}

> @@ -708,16 +947,16 @@ static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
>  	 * subsampling. Update capture queue when the stream is off.
>  	 */
>  	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
> -	    !V4L2_TYPE_IS_OUTPUT(q->type)) {
> +	    !V4L2_TYPE_IS_OUTPUT(q->type) &&
> +	    ctx->jpeg->mode == MTK_JPEG_DEC) {
>  		struct mtk_jpeg_src_buf *src_buf;
>  
>  		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
>  		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
>  		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
>  		ctx->state = MTK_JPEG_RUNNING;
> -	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> +	} else if (V4L2_TYPE_IS_OUTPUT(q->type))
>  		ctx->state = MTK_JPEG_INIT;
> -	}

Wouldn't it be possible that at this point the encoder is actually in
process of encoding a frame? Wouldn't this code race with the interrupt
handler?

>  
>  	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
>  		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
> @@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
>  	return 0;
>  }
>  
> +static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> +				 struct vb2_buffer *dst_buf,
> +				 struct mtk_jpeg_enc_bs *bs)
> +{
> +	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> +	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;

Could you explain what is the meaning of the dma_addr_offset and where the
default EXIF size comes from? Also, how is the encoder output affected by
the enable_exif flag?

> +	bs->dma_addr_offsetmask = bs->dma_addr & JPEG_ENC_DST_ADDR_OFFSET_MASK;
> +	bs->size = round_up(vb2_plane_size(dst_buf, 0), 128);

We can't round up the size that comes from vb2, because it could lead to a
buffer overflow. We need to ensure that the hardware doesn't write more
than the value returned by vb2_plane_size().

> +
> +	mtk_jpeg_enc_set_dst_addr(base, bs->dma_addr, bs->size,
> +				  bs->dma_addr_offset,
> +				  bs->dma_addr_offsetmask);
> +}
> +
> +static void mtk_jpeg_set_enc_src(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> +				 struct vb2_buffer *src_buf,
> +				 struct mtk_jpeg_enc_fb *fb)
> +{
> +	int i;
> +
> +	mtk_jpeg_enc_set_img_size(base, ctx->out_q.w, ctx->out_q.h);
> +	mtk_jpeg_enc_set_enc_format(base, ctx->out_q.fmt->fourcc);
> +	mtk_jpeg_enc_set_blk_num(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
> +				 ctx->out_q.h);
> +	mtk_jpeg_enc_set_stride(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
> +				ctx->out_q.h, ctx->out_q.bytesperline[0]);
> +
> +	for (i = 0; i < src_buf->num_planes; i++) {
> +		fb->fb_addr[i].dma_addr =
> +			vb2_dma_contig_plane_dma_addr(src_buf, i) +
> +			src_buf->planes[i].data_offset;
> +		fb->fb_addr[i].size = vb2_plane_size(src_buf, i) -
> +				      src_buf->planes[i].data_offset;
> +	}
> +
> +	mtk_jpeg_enc_set_src_addr(base, fb->fb_addr[0].dma_addr,
> +				  fb->fb_addr[1].dma_addr);

What would be the second dma_addr in case of a format with 1 plane?

> +}
> +
>  static void mtk_jpeg_device_run(void *priv)
>  {
>  	struct mtk_jpeg_ctx *ctx = priv;
> @@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
>  	struct mtk_jpeg_src_buf *jpeg_src_buf;
>  	struct mtk_jpeg_bs bs;
>  	struct mtk_jpeg_fb fb;
> +	struct mtk_jpeg_enc_bs enc_bs;
> +	struct mtk_jpeg_enc_fb enc_fb;
>  	int i;
>  
>  	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> @@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
>  		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
>  			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
>  		buf_state = VB2_BUF_STATE_DONE;

About existing code, but we may want to explain this.
What is this last frame handling above for?

> -		goto dec_end;
> +		goto device_run_end;
>  	}
>  
> -	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
> -		mtk_jpeg_queue_src_chg_event(ctx);
> -		ctx->state = MTK_JPEG_SOURCE_CHANGE;
> -		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> -		return;
> -	}
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> +		mtk_jpeg_enc_reset(jpeg->reg_base);

Why do we need to reset every frame?

> +
> +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
> +				     &enc_bs);
> +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
> +				     &enc_fb);
> +		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
> +					  ctx->enc_quality,
> +					  ctx->restart_interval);
> +
> +		mtk_jpeg_enc_start(jpeg->reg_base);
> +	} else {
> +		if (mtk_jpeg_check_resolution_change
> +			(ctx, &jpeg_src_buf->dec_param)) {
> +			mtk_jpeg_queue_src_chg_event(ctx);
> +			ctx->state = MTK_JPEG_SOURCE_CHANGE;
> +			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);

This is a bit strange. Resolution change should be signaled when the
hardware attempted to decode a frame and detected a different resolution
than current. It shouldn't be necessary for the userspace to queue a pair
of buffers to signal it, as with the current code.

> +			return;
> +		}
>  
> -	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
> -	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
> -		goto dec_end;
> +		mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
> +		if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param,
> +					 &dst_buf->vb2_buf, &fb))
> +			goto device_run_end;
>  
> -	spin_lock_irqsave(&jpeg->hw_lock, flags);
> -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> -	mtk_jpeg_dec_set_config(jpeg->dec_reg_base,
> -				&jpeg_src_buf->dec_param, &bs, &fb);
> +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> +		mtk_jpeg_dec_reset(jpeg->reg_base);

Same here, why do we need to reset the decoder every frame? Shouldn't it be
enough to reset it in runtime resume callback?

> +		mtk_jpeg_dec_set_config(jpeg->reg_base,
> +					&jpeg_src_buf->dec_param, &bs, &fb);
>  
> -	mtk_jpeg_dec_start(jpeg->dec_reg_base);
> +		mtk_jpeg_dec_start(jpeg->reg_base);
> +	}
>  	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
>  	return;
>  
> -dec_end:
> +device_run_end:
>  	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
>  	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
>  	v4l2_m2m_buf_done(src_buf, buf_state);
> @@ -875,30 +1172,30 @@ static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
>  	ret = mtk_smi_larb_get(jpeg->larb);
>  	if (ret)
>  		dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
> -	clk_prepare_enable(jpeg->clk_jdec_smi);
> -	clk_prepare_enable(jpeg->clk_jdec);
> +	if (jpeg->mode == MTK_JPEG_DEC)
> +		clk_prepare_enable(jpeg->clk_jpeg_smi);

Could the clk_bulk_* API be used instead? It would eliminate the need for
this special casing.

> +	clk_prepare_enable(jpeg->clk_jpeg);
>  }
>  
>  static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
>  {
> -	clk_disable_unprepare(jpeg->clk_jdec);
> -	clk_disable_unprepare(jpeg->clk_jdec_smi);
> +	clk_disable_unprepare(jpeg->clk_jpeg);
> +	if (jpeg->mode == MTK_JPEG_DEC)
> +		clk_disable_unprepare(jpeg->clk_jpeg_smi);

Ditto.

>  	mtk_smi_larb_put(jpeg->larb);
>  }
>  
> -static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
> +static irqreturn_t mtk_jpeg_irq(int irq, void *priv)
>  {
>  	struct mtk_jpeg_dev *jpeg = priv;
>  	struct mtk_jpeg_ctx *ctx;
>  	struct vb2_v4l2_buffer *src_buf, *dst_buf;
>  	struct mtk_jpeg_src_buf *jpeg_src_buf;
>  	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
> -	u32	dec_irq_ret;
> -	u32 dec_ret;
> +	u32 irq_ret;
> +	u32 ret, result_size;
>  	int i;
>  
> -	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
> -	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
>  	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
>  	if (!ctx) {
>  		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
> @@ -909,32 +1206,89 @@ static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
>  	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
>  	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
>  
> -	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
> -		mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> +	if (jpeg->mode == MTK_JPEG_ENC) {

As the hardware blocks are completely different, we definitely want to have
separate interrupt handlers for them, rather than this special casing.

> +		ret = mtk_jpeg_enc_get_int_status(jpeg->reg_base);
> +		irq_ret = mtk_jpeg_enc_enum_result(jpeg->reg_base, ret);
>  
> -	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
> -		dev_err(jpeg->dev, "decode failed\n");
> -		goto dec_end;
> -	}
> +		if (irq_ret >= MTK_JPEG_ENC_RESULT_STALL)
> +			mtk_jpeg_enc_reset(jpeg->reg_base);
> +
> +		if (irq_ret != MTK_JPEG_ENC_RESULT_DONE) {
> +			dev_err(jpeg->dev, "encode failed\n");
> +			goto irq_end;
> +		}
> +
> +		result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
> +		vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
> +				      result_size);
> +	} else {
> +		ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
> +		irq_ret = mtk_jpeg_dec_enum_result(ret);
> +
> +		if (irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
> +			mtk_jpeg_dec_reset(jpeg->reg_base);
>  
> -	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> -		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
> -				      jpeg_src_buf->dec_param.comp_size[i]);
> +		if (irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
> +			dev_err(jpeg->dev, "decode failed\n");
> +			goto irq_end;
> +		}
> +
> +		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> +			vb2_set_plane_payload
> +				(&dst_buf->vb2_buf, i,
> +				 jpeg_src_buf->dec_param.comp_size[i]);
> +	}
>  
>  	buf_state = VB2_BUF_STATE_DONE;
>  
> -dec_end:
> +irq_end:
>  	v4l2_m2m_buf_done(src_buf, buf_state);
>  	v4l2_m2m_buf_done(dst_buf, buf_state);
>  	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
>  	return IRQ_HANDLED;
>  }
>  
> -static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
> +static void mtk_jpeg_set_enc_default_params(struct mtk_jpeg_ctx *ctx)
> +{
> +	struct mtk_jpeg_q_data *q = &ctx->out_q;
> +
> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;

Perhaps this could be just moved to the caller, rather than duplicating for
encoder and decoder?

> +
> +	ctx->colorspace = V4L2_COLORSPACE_JPEG,
> +	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
> +	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
> +	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
> +
> +	q->w = MTK_JPEG_MIN_WIDTH;
> +	q->h = MTK_JPEG_MIN_HEIGHT;
> +
> +	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
> +				      MTK_JPEG_FMT_TYPE_OUTPUT);
> +
> +	q->w = clamp(round_up(q->w, 32), MTK_JPEG_MIN_WIDTH,
> +		     MTK_JPEG_MAX_WIDTH);
> +	q->h = clamp(round_up(q->h, 8), MTK_JPEG_MIN_HEIGHT,
> +		     MTK_JPEG_MAX_HEIGHT);

Is this clamp() really needed? The code explicitly sets correct values
few lines above.

> +
> +	q->sizeimage[0] = q->w * q->h * 2;
> +	q->bytesperline[0] = q->w * 2;

In general this kind of hardcoded computation is really error prone, e.g.
if the default pixelformat changes for some reason. Could the same logic as
for TRY_/S_FMT be called from here to compute these values?

> +
> +	q = &ctx->cap_q;
> +	q->w = MTK_JPEG_MIN_WIDTH;
> +	q->h = MTK_JPEG_MIN_HEIGHT;
> +	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
> +				      MTK_JPEG_FMT_TYPE_CAPTURE);
> +	q->bytesperline[0] = 0;
> +	q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
> +}
> +
> +static void mtk_jpeg_set_dec_default_params(struct mtk_jpeg_ctx *ctx)
>  {
>  	struct mtk_jpeg_q_data *q = &ctx->out_q;
>  	int i;
>  
> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> +
>  	ctx->colorspace = V4L2_COLORSPACE_JPEG,
>  	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
>  	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
> @@ -990,7 +1344,18 @@ static int mtk_jpeg_open(struct file *file)
>  		goto error;
>  	}
>  
> -	mtk_jpeg_set_default_params(ctx);
> +	ret = mtk_jpeg_ctrls_setup(ctx);
> +	if (ret) {
> +		v4l2_err(&jpeg->v4l2_dev, "Failed to setup controls() (%d)\n",

nit: Is the "()" after "controls" necessary?

> +			 ret);
> +		goto error;
> +	}
> +
> +	if (jpeg->mode == MTK_JPEG_ENC)
> +		mtk_jpeg_set_enc_default_params(ctx);
> +	else
> +		mtk_jpeg_set_dec_default_params(ctx);
> +
>  	mutex_unlock(&jpeg->lock);
>  	return 0;
>  
> @@ -1010,6 +1375,7 @@ static int mtk_jpeg_release(struct file *file)
>  
>  	mutex_lock(&jpeg->lock);
>  	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
> +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
>  	v4l2_fh_del(&ctx->fh);
>  	v4l2_fh_exit(&ctx->fh);
>  	kfree(ctx);
> @@ -1043,19 +1409,24 @@ static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
>  
>  	jpeg->larb = &pdev->dev;
>  
> -	jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
> -	if (IS_ERR(jpeg->clk_jdec))
> -		return PTR_ERR(jpeg->clk_jdec);
> +	if (jpeg->mode == MTK_JPEG_ENC) {
> +		jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgenc");
> +		return PTR_ERR_OR_ZERO(jpeg->clk_jpeg);
> +	}
> +
> +	jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgdec");
> +	if (IS_ERR(jpeg->clk_jpeg))
> +		return PTR_ERR(jpeg->clk_jpeg);
>  
> -	jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
> -	return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
> +	jpeg->clk_jpeg_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
> +	return PTR_ERR_OR_ZERO(jpeg->clk_jpeg_smi);

I'd suggest putting the clock names in a match_data struct, which could be
pointed to by the .data field of of_device_id. Then devm_clk_bulk_get()
could be used generically, without the need to special case between encoder
and decoder.

>  }
>  
>  static int mtk_jpeg_probe(struct platform_device *pdev)
>  {
>  	struct mtk_jpeg_dev *jpeg;
>  	struct resource *res;
> -	int dec_irq;
> +	int jpeg_irq;
>  	int ret;
>  
>  	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
> @@ -1065,28 +1436,26 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>  	mutex_init(&jpeg->lock);
>  	spin_lock_init(&jpeg->hw_lock);
>  	jpeg->dev = &pdev->dev;
> +	jpeg->mode = (enum mtk_jpeg_mode)of_device_get_match_data(jpeg->dev);

As the match data is expected to be a pointer, it shouldn't be casted to a
non-pointer value. Please define a struct with necessary hardware-specific
data and have the .data field of of_device_id point to it.

>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> -	jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
> -	if (IS_ERR(jpeg->dec_reg_base)) {
> -		ret = PTR_ERR(jpeg->dec_reg_base);
> +	jpeg->reg_base = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(jpeg->reg_base)) {
> +		ret = PTR_ERR(jpeg->reg_base);
>  		return ret;
>  	}
>  
> -	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> -	dec_irq = platform_get_irq(pdev, 0);
> -	if (!res || dec_irq < 0) {
> -		dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
> -		ret = -EINVAL;

This is a bug fix, so plese do it in a separate patch, earlier in the
series.

> -		return ret;
> +	jpeg_irq = platform_get_irq(pdev, 0);
> +	if (jpeg_irq < 0) {
> +		dev_err(&pdev->dev, "Failed to get jpeg_irq %d.\n", jpeg_irq);
> +		return jpeg_irq;
>  	}
>  
> -	ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
> +	ret = devm_request_irq(&pdev->dev, jpeg_irq, mtk_jpeg_irq, 0,
>  			       pdev->name, jpeg);
>  	if (ret) {
> -		dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
> -			dec_irq, ret);
> -		ret = -EINVAL;

Ditto. (Could be in one patch as the similar fix above.)

> +		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
> +			jpeg_irq, ret);
>  		goto err_req_irq;
>  	}
>  
> @@ -1110,33 +1479,35 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>  		goto err_m2m_init;
>  	}
>  
> -	jpeg->dec_vdev = video_device_alloc();
> -	if (!jpeg->dec_vdev) {
> +	jpeg->vfd_jpeg = video_device_alloc();

Why "vfd"? "vdev" sounds more like video_device.

Also, the "jpeg" suffix doesn't seem to be needed, as the parent struct is
typically called "jpeg".

> +	if (!jpeg->vfd_jpeg) {
>  		ret = -ENOMEM;
> -		goto err_dec_vdev_alloc;
> +		goto err_vfd_jpeg_alloc;
>  	}
> -	snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
> -		 "%s-dec", MTK_JPEG_NAME);
> -	jpeg->dec_vdev->fops = &mtk_jpeg_fops;
> -	jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
> -	jpeg->dec_vdev->minor = -1;
> -	jpeg->dec_vdev->release = video_device_release;
> -	jpeg->dec_vdev->lock = &jpeg->lock;
> -	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
> -	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
> -	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
> +	snprintf(jpeg->vfd_jpeg->name, sizeof(jpeg->vfd_jpeg->name),
> +		 "%s-%s", MTK_JPEG_NAME,
> +		 jpeg->mode == MTK_JPEG_ENC ? "enc" : "dec");
> +	jpeg->vfd_jpeg->fops = &mtk_jpeg_fops;
> +	jpeg->vfd_jpeg->ioctl_ops = &mtk_jpeg_ioctl_ops;
> +	jpeg->vfd_jpeg->minor = -1;
> +	jpeg->vfd_jpeg->release = video_device_release;
> +	jpeg->vfd_jpeg->lock = &jpeg->lock;
> +	jpeg->vfd_jpeg->v4l2_dev = &jpeg->v4l2_dev;
> +	jpeg->vfd_jpeg->vfl_dir = VFL_DIR_M2M;
> +	jpeg->vfd_jpeg->device_caps = V4L2_CAP_STREAMING |
>  				      V4L2_CAP_VIDEO_M2M_MPLANE;
>  
> -	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
> +	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);

FYI the type changed to VFL_TYPE_VIDEO recently.

>  	if (ret) {
>  		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
> -		goto err_dec_vdev_register;
> +		goto err_vfd_jpeg_register;
>  	}
>  
> -	video_set_drvdata(jpeg->dec_vdev, jpeg);
> +	video_set_drvdata(jpeg->vfd_jpeg, jpeg);
>  	v4l2_info(&jpeg->v4l2_dev,
> -		  "decoder device registered as /dev/video%d (%d,%d)\n",
> -		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
> +		  "jpeg device %d registered as /dev/video%d (%d,%d)\n",

Here it would be actually useful to special case the encoder and decoder,
because it would be easier for the user to know which device is which.

> +		  jpeg->mode, jpeg->vfd_jpeg->num, VIDEO_MAJOR,
> +		  jpeg->vfd_jpeg->minor);
>  
>  	platform_set_drvdata(pdev, jpeg);
>  
> @@ -1144,10 +1515,10 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>  
>  	return 0;
>  
> -err_dec_vdev_register:
> -	video_device_release(jpeg->dec_vdev);
> +err_vfd_jpeg_register:
> +	video_device_release(jpeg->vfd_jpeg);
>  
> -err_dec_vdev_alloc:
> +err_vfd_jpeg_alloc:
>  	v4l2_m2m_release(jpeg->m2m_dev);
>  
>  err_m2m_init:
> @@ -1167,8 +1538,8 @@ static int mtk_jpeg_remove(struct platform_device *pdev)
>  	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
>  
>  	pm_runtime_disable(&pdev->dev);
> -	video_unregister_device(jpeg->dec_vdev);
> -	video_device_release(jpeg->dec_vdev);
> +	video_unregister_device(jpeg->vfd_jpeg);
> +	video_device_release(jpeg->vfd_jpeg);
>  	v4l2_m2m_release(jpeg->m2m_dev);
>  	v4l2_device_unregister(&jpeg->v4l2_dev);
>  
> @@ -1179,7 +1550,11 @@ static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
>  {
>  	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
>  
> -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> +	if (jpeg->mode == MTK_JPEG_ENC)
> +		mtk_jpeg_enc_reset(jpeg->reg_base);
> +	else
> +		mtk_jpeg_dec_reset(jpeg->reg_base);

Is the reset here really necessary? We're going to cut down the power to
the IP block some time after the function returns.

> +
>  	mtk_jpeg_clk_off(jpeg);
>  
>  	return 0;
> @@ -1190,7 +1565,10 @@ static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
>  	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
>  
>  	mtk_jpeg_clk_on(jpeg);
> -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> +	if (jpeg->mode == MTK_JPEG_ENC)
> +		mtk_jpeg_enc_reset(jpeg->reg_base);
> +	else
> +		mtk_jpeg_dec_reset(jpeg->reg_base);
>  
>  	return 0;
>  }

The code is not visible in this patch, but the implementation of the system
PM ops doesn't look correct.

	static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
	{
		int ret;
	
		if (pm_runtime_suspended(dev))
			return 0;
	
		ret = mtk_jpeg_pm_suspend(dev);

The hardware may be currently processing a frame. Wouldn't this cause
issues? Also, wouldn't this race with the interrupt handler?

Normally one would prevent the driver from scheduling next frames here,
wait for the driver to finish processing the current frame and only then
suspend.

		return ret;
	}
	
	static __maybe_unused int mtk_jpeg_resume(struct device *dev)
	{
		int ret;
	
		if (pm_runtime_suspended(dev))
			return 0;
	
		ret = mtk_jpeg_pm_resume(dev);

Here one normally needs to unblock the driver from scheduling next frames.
	
		return ret;
	}

> @@ -1226,11 +1604,15 @@ static const struct dev_pm_ops mtk_jpeg_pm_ops = {
>  static const struct of_device_id mtk_jpeg_match[] = {
>  	{
>  		.compatible = "mediatek,mt8173-jpgdec",
> -		.data       = NULL,
> +		.data       = (void *)MTK_JPEG_DEC,
>  	},
>  	{
>  		.compatible = "mediatek,mt2701-jpgdec",
> -		.data       = NULL,
> +		.data       = (void *)MTK_JPEG_DEC,
> +	},
> +	{
> +		.compatible = "mediatek,mtk-jpgenc",
> +		.data       = (void *)MTK_JPEG_ENC,
>  	},
>  	{},
>  };
> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> index 9bbd615b1067..02a387d8be5d 100644
> --- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> @@ -3,6 +3,7 @@
>   * Copyright (c) 2016 MediaTek Inc.
>   * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
>   *         Rick Chang <rick.chang@mediatek.com>
> + *         Xia Jiang <xia.jiang@mediatek.com>
>   */
>  
>  #ifndef _MTK_JPEG_CORE_H
> @@ -19,16 +20,19 @@
>  
>  #define MTK_JPEG_FMT_FLAG_DEC_OUTPUT	BIT(0)
>  #define MTK_JPEG_FMT_FLAG_DEC_CAPTURE	BIT(1)
> +#define MTK_JPEG_FMT_FLAG_ENC_OUTPUT	BIT(2)
> +#define MTK_JPEG_FMT_FLAG_ENC_CAPTURE	BIT(3)
>  
>  #define MTK_JPEG_FMT_TYPE_OUTPUT	1
>  #define MTK_JPEG_FMT_TYPE_CAPTURE	2
>  
>  #define MTK_JPEG_MIN_WIDTH	32U
>  #define MTK_JPEG_MIN_HEIGHT	32U
> -#define MTK_JPEG_MAX_WIDTH	8192U
> -#define MTK_JPEG_MAX_HEIGHT	8192U
> +#define MTK_JPEG_MAX_WIDTH	65535U
> +#define MTK_JPEG_MAX_HEIGHT	65535U

Is this correct even for the decoder?

>  
>  #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
> +#define MTK_JPEG_DEFAULT_EXIF_SIZE	(64 * 1024)
>  
>  /**
>   * enum mtk_jpeg_ctx_state - contex state of jpeg
> @@ -39,6 +43,14 @@ enum mtk_jpeg_ctx_state {
>  	MTK_JPEG_SOURCE_CHANGE,
>  };
>  
> +/**
> + * enum mtk_jpeg_mode - mode of jpeg
> + */
> +enum mtk_jpeg_mode {
> +	MTK_JPEG_ENC,
> +	MTK_JPEG_DEC,
> +};
> +

Perhaps bool is_encoder insted of an enum could simplify the code a bit?

>  /**
>   * struct mt_jpeg - JPEG IP abstraction
>   * @lock:		the mutex protecting this structure
> @@ -48,11 +60,12 @@ enum mtk_jpeg_ctx_state {
>   * @v4l2_dev:		v4l2 device for mem2mem mode
>   * @m2m_dev:		v4l2 mem2mem device data
>   * @alloc_ctx:		videobuf2 memory allocator's context
> - * @dec_vdev:		video device node for decoder mem2mem mode
> - * @dec_reg_base:	JPEG registers mapping
> - * @clk_jdec:		JPEG hw working clock
> - * @clk_jdec_smi:	JPEG SMI bus clock
> + * @vfd_jpeg:		video device node for jpeg mem2mem mode
> + * @reg_base:		JPEG registers mapping
> + * @clk_jpeg:		JPEG hw working clock
> + * @clk_jpeg_smi:	JPEG SMI bus clock
>   * @larb:		SMI device
> + * @mode:		compression (encode) operation or decompression (decode)
>   */
>  struct mtk_jpeg_dev {
>  	struct mutex		lock;
> @@ -62,11 +75,12 @@ struct mtk_jpeg_dev {
>  	struct v4l2_device	v4l2_dev;
>  	struct v4l2_m2m_dev	*m2m_dev;
>  	void			*alloc_ctx;
> -	struct video_device	*dec_vdev;
> -	void __iomem		*dec_reg_base;
> -	struct clk		*clk_jdec;
> -	struct clk		*clk_jdec_smi;
> +	struct video_device	*vfd_jpeg;
> +	void __iomem		*reg_base;
> +	struct clk		*clk_jpeg;
> +	struct clk		*clk_jpeg_smi;
>  	struct device		*larb;
> +	enum mtk_jpeg_mode	mode;
>  };
>  
>  /**
> @@ -113,6 +127,7 @@ struct mtk_jpeg_q_data {
>   * @cap_q:		destination (capture) queue queue information
>   * @fh:			V4L2 file handle
>   * @state:		state of the context
> + * @ctrl_hdl:		controls handler
>   * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
>   * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
>   * @quantization: enum v4l2_quantization, colorspace quantization
> @@ -124,6 +139,10 @@ struct mtk_jpeg_ctx {
>  	struct mtk_jpeg_q_data		cap_q;
>  	struct v4l2_fh			fh;
>  	enum mtk_jpeg_ctx_state		state;
> +	u8				enable_exif;

bool?

> +	u8				enc_quality;
> +	u8				restart_interval;
> +	struct v4l2_ctrl_handler	ctrl_hdl;
>  
>  	enum v4l2_colorspace colorspace;
>  	enum v4l2_ycbcr_encoding ycbcr_enc;
> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> index 1cc37dbfc8e7..ce263db5f30a 100644
> --- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> @@ -3,10 +3,11 @@
>   * Copyright (c) 2016 MediaTek Inc.
>   * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
>   *         Rick Chang <rick.chang@mediatek.com>
> + *         Xia Jiang <xia.jiang@mediatek.com>
>   */
>  
> -#ifndef _MTK_JPEG_HW_H
> -#define _MTK_JPEG_HW_H
> +#ifndef _MTK_JPEG_DEC_HW_H
> +#define _MTK_JPEG_DEC_HW_H
>  
>  #include <media/videobuf2-core.h>
>  
> @@ -75,4 +76,4 @@ void mtk_jpeg_dec_set_config(void __iomem *base,
>  void mtk_jpeg_dec_reset(void __iomem *dec_reg_base);
>  void mtk_jpeg_dec_start(void __iomem *dec_reg_base);
>  
> -#endif /* _MTK_JPEG_HW_H */
> +#endif /* _MTK_JPEG_DEC_HW_H */
> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
> new file mode 100644
> index 000000000000..da3e7a83a80a
> --- /dev/null
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
> @@ -0,0 +1,273 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) 2019 MediaTek Inc.
> + * Author: Xia Jiang <xia.jiang@mediatek.com>
> + *
> + */
> +
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <media/videobuf2-core.h>
> +
> +#include "mtk_jpeg_enc_hw.h"
> +
> +static struct mtk_jpeg_enc_qlt mtk_jpeg_enc_quality[] = {

static const?

> +	{
> +		.quality_param		= 97,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q97,
> +	},
> +	{
> +		.quality_param		= 95,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q95,
> +	},
> +	{
> +		.quality_param		= 92,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q92,
> +	},
> +	{
> +		.quality_param		= 90,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q90,
> +	},
> +	{
> +		.quality_param		= 87,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q87,
> +	},
> +	{
> +		.quality_param		= 84,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q84,
> +	},
> +	{
> +		.quality_param		= 82,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q82,
> +	},
> +	{
> +		.quality_param		= 80,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q80,
> +	},
> +	{
> +		.quality_param		= 74,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q74,
> +	},
> +	{
> +		.quality_param		= 68,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q68,
> +	},
> +	{
> +		.quality_param		= 64,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q64,
> +	},
> +	{
> +		.quality_param		= 60,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q60,
> +	},
> +	{
> +		.quality_param		= 48,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q48,
> +	},
> +	{
> +		.quality_param		= 39,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q39,
> +	},
> +	{
> +		.quality_param		= 34,
> +		.hardware_value	= JPEG_ENC_QUALITY_Q34,
> +	},
> +};

nit: Perhaps we could condense this a bit? E.g.

[...] = {
	{ .quality = XX, .hw = YY }.
	// ...
};

> +
> +#define MTK_JPEG_ENC_NUM_QUALITY ARRAY_SIZE(mtk_jpeg_enc_quality)

I think just using ARRAY_SIZE() directly would make the code clearer (and
it's similar length to the macro).

> +
> +void mtk_jpeg_enc_reset(void __iomem *base)
> +{
> +	writel(0x00, base + JPEG_ENC_RSTB);
> +	writel(JPEG_ENC_RESET_BIT, base + JPEG_ENC_RSTB);
> +	writel(0x00, base + JPEG_ENC_CODEC_SEL);
> +}
> +
> +u32 mtk_jpeg_enc_get_int_status(void __iomem *base)
> +{
> +	u32 ret;
> +
> +	ret = readl(base + JPEG_ENC_INT_STS) &
> +		    JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
> +	if (ret)
> +		writel(0, base + JPEG_ENC_INT_STS);
> +
> +	return ret;
> +}

Does it make sense to have a function for what is essentially just 2 lines?
Also, the name is misleading, as the function not only gets but also
clears.

> +
> +u32 mtk_jpeg_enc_get_file_size(void __iomem *base)
> +{
> +	return readl(base + JPEG_ENC_DMA_ADDR0) -
> +	       readl(base + JPEG_ENC_DST_ADDR0);
> +}

This is called once from the interrupt handler, which is hardware-specific
anyway, so one would normally just read the registers directly from there.

> +
> +u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status)
> +{
> +	if (irq_status & JPEG_ENC_INT_STATUS_DONE)
> +		return MTK_JPEG_ENC_RESULT_DONE;
> +	else if (irq_status & JPEG_ENC_INT_STATUS_STALL)
> +		return MTK_JPEG_ENC_RESULT_STALL;
> +	else if (irq_status & JPEG_ENC_INT_STATUS_VCODEC_IRQ)
> +		return MTK_JPEG_ENC_RESULT_VCODEC_IRQ;
> +	return MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN;
> +}

I see this function being called only once in the interrupt handler.
Wouldn't it be more readable if the interrupt masks were just checked
directly there?

> +
> +void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height)
> +{
> +	u32 value;
> +
> +	value = JPEG_ENC_WIDTH_HEIGHT(width, height);
> +	writel(value, base + JPEG_ENC_IMG_SIZE);
> +}
> +
> +void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format)
> +{
> +	u32 yuv_format, value;
> +
> +	yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
> +	switch (enc_format) {
> +	case V4L2_PIX_FMT_YUYV:
> +		yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
> +		break;
> +	case V4L2_PIX_FMT_YVYU:
> +		yuv_format = JPEG_ENC_YUV_FORMAT_YVYU;
> +		break;
> +	case V4L2_PIX_FMT_NV12M:
> +		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;
> +		break;
> +	case V4L2_PIX_FMT_NV21M:
> +		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;

Instead of the explicit switch here, we could just have the hardware format
contained in the mtk_jpeg_fmt struct.

> +		break;
> +	default:
> +		break;
> +	}
> +
> +	value = readl(base + JPEG_ENC_CTRL);
> +	value &= ~JPEG_ENC_CTRL_YUV_FORMAT_MASK;
> +	value |= JPEG_ENC_FORMAT(yuv_format);
> +	writel(value, base + JPEG_ENC_CTRL);
> +}
> +
> +void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
> +			      u32 height)
> +{
> +	u32 blk_num;
> +	u32 is_420;
> +	u32 padding_width;
> +	u32 padding_height;
> +
> +	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
> +		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;
> +	padding_width = round_up(width, 16);
> +	padding_height = round_up(height, is_420 ? 16 : 8);
> +
> +	blk_num = (padding_width >> 4) * (padding_height >> (is_420 ? 4 : 3)) *
> +		  (is_420 ? 6 : 4) - 1;

The whole code above could be rewritten as below:

if (enc_format == V4L2_PIX_FMT_NV12M
    || enc_format == V4L2_PIX_FMT_NV21M)
	blk_num = DIV_ROUND_UP(width, 16) * DIV_ROUND_UP(height, 16) * 6 - 1;
else
	blk_num = DIV_ROUND_UP(width, 16) * DIV_ROUND_UP(height, 8) * 4 - 1;

However the magic numbers deserve some explanation, especially
multiplication by 6 or 4 and subtraction of 1.

> +
> +	writel(blk_num, base + JPEG_ENC_BLK_NUM);
> +}
> +
> +void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
> +			     u32 height, u32 bytesperline)
> +{
> +	u32 width_even;
> +	u32 is_420;

Perhaps should be bool?

> +	u32 img_stride;
> +	u32 mem_stride;
> +
> +	width_even = round_up(width, 2);
> +	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
> +		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;

No need for the explicit 1 : 0.

> +	if (!is_420)
> +		width_even = width_even << 1;
> +	img_stride = round_up(width_even, (is_420 ? 16 : 32));
> +	mem_stride = is_420 ? bytesperline : round_up(width_even, 32);

Wouldn't it make the code more readable if it was rewritten like below?

if (enc_format == V4L2_PIX_FMT_NV12M
    || enc_format == V4L2_PIX_FMT_NV21M) {
        /* 4:2:0 */
	img_stride = round_up(width, 16);
	mem_stride = bytesperline;
} else {
	/* 4:2:2 */
	img_stride = round_up(width * 2, 32);
	mem_stride = img_stride;
}

> +
> +	writel(img_stride, base + JPEG_ENC_IMG_STRIDE);
> +	writel(mem_stride, base + JPEG_ENC_STRIDE);

Does the hardware assume that Y and CbCr planes have the same strides?

What is the difference between img_stride and mem_stride?

> +}
> +
> +void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
> +			       u32 src_addr_c)
> +{
> +	writel(src_addr, base + JPEG_ENC_SRC_LUMA_ADDR);
> +	writel(src_addr_c, base + JPEG_ENC_SRC_CHROMA_ADDR);
> +}
> +
> +void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
> +			       u32 stall_size, u32 init_offset,
> +			       u32 offset_mask)
> +{
> +	writel(JPEG_ENC_INIT_OFFSET(init_offset), base + JPEG_ENC_OFFSET_ADDR);
> +	writel(JPEG_ENC_OFFSET_MASK(offset_mask),
> +	       base + JPEG_ENC_BYTE_OFFSET_MASK);
> +	writel(JPEG_ENC_DST_ADDR(dst_addr), base + JPEG_ENC_DST_ADDR0);
> +	writel(JPEG_ENC_STALL_ADDR(dst_addr, stall_size),
> +	       base + JPEG_ENC_STALL_ADDR0);
> +}
> +
> +static void mtk_jpeg_enc_set_quality(void __iomem *base, u32 quality)
> +{
> +	u32 value;
> +	u32 i, enc_quality;
> +
> +	enc_quality = mtk_jpeg_enc_quality[0].hardware_value;

For values less than 34, shouldn't we use the lowest quality, not the
highest?

> +	for (i = 0; i < MTK_JPEG_ENC_NUM_QUALITY; i++) {

Please use ARRAY_SIZE() instead of the macro to make sure there is no
accidental out of bounds access.

> +		if (quality >= mtk_jpeg_enc_quality[i].quality_param) {
> +			enc_quality = mtk_jpeg_enc_quality[i].hardware_value;
> +			break;
> +		}
> +	}
> +
> +	value = readl(base + JPEG_ENC_QUALITY);

Do we need to read the register?

> +	value = JPEG_ENC_SET_QUALITY(value, enc_quality);
> +	writel(value, base + JPEG_ENC_QUALITY);
> +}
> +
> +static void mtk_jpeg_enc_set_restart_interval(void __iomem *base,
> +					      u32 restart_interval)
> +{
> +	u32 value;
> +
> +	value = readl(base + JPEG_ENC_CTRL);
> +	if (restart_interval)
> +		value |= JPEG_ENC_CTRL_RESTART_EN_BIT;
> +	else
> +		value &= ~JPEG_ENC_CTRL_RESTART_EN_BIT;
> +	writel(value, base + JPEG_ENC_CTRL);
> +	writel(restart_interval, base + JPEG_ENC_RST_MCU_NUM);
> +}

Wouldn't it make more sense to just do all the register settings in one
function, with comments? Splitting it like here introduces superfluous
register reads and writes - we could just read JPEG_ENC_CTRL once, update
all the fields in the local variable and write it once if done in one
function.

A typical approach is to have one function that receives the necessary V4L2
context and then extracts necessary parameters directly from there and
programs the registers. That reduces the level of abstraction and makes the
code easier to follow for anyone familiar with V4L2.

> +
> +static void mtk_jpeg_enc_set_encode_mode(void __iomem *base, u32 exif_en)
> +{
> +	u32 value;
> +
> +	value = readl(base + JPEG_ENC_CTRL);
> +	value &= ~JPEG_ENC_CTRL_FILE_FORMAT_BIT;
> +	writel(value, base + JPEG_ENC_CTRL);
> +
> +	if (exif_en) {
> +		value = readl(base + JPEG_ENC_CTRL);

Do we need to read the register second time? Could we just read it once,
take care of both fields and write once?

> +		value |= JPEG_ENC_EN_JFIF_EXIF;
> +		writel(value, base + JPEG_ENC_CTRL);
> +	}
> +}
> +
> +void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
> +			       u32 restart_interval)
> +{
> +	mtk_jpeg_enc_set_quality(base, quality);
> +
> +	mtk_jpeg_enc_set_restart_interval(base, restart_interval);
> +
> +	mtk_jpeg_enc_set_encode_mode(base, exif_en);
> +}
> +
> +void mtk_jpeg_enc_start(void __iomem *base)
> +{
> +	u32 value;
> +
> +	value = readl(base + JPEG_ENC_CTRL);
> +	value |= JPEG_ENC_CTRL_INT_EN_BIT | JPEG_ENC_CTRL_ENABLE_BIT;
> +	writel(value, base + JPEG_ENC_CTRL);
> +}
> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
> new file mode 100644
> index 000000000000..d0d7d218a0c2
> --- /dev/null
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
> @@ -0,0 +1,86 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Copyright (c) 2019 MediaTek Inc.
> + * Author: Xia Jiang <xia.jiang@mediatek.com>
> + *
> + */
> +
> +#ifndef _MTK_JPEG_ENC_HW_H
> +#define _MTK_JPEG_ENC_HW_H
> +
> +#include <media/videobuf2-core.h>
> +
> +#include "mtk_jpeg_core.h"
> +#include "mtk_jpeg_enc_reg.h"
> +
> +enum {
> +	MTK_JPEG_ENC_RESULT_DONE		= 0,

nit: No need for explicit value assignment.

> +	MTK_JPEG_ENC_RESULT_STALL,
> +	MTK_JPEG_ENC_RESULT_VCODEC_IRQ,
> +	MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN
> +};
> +
> +/**
> + * struct mtk_jpeg_enc_qlt - JPEG encoder quality data
> + * @quality_param:	quality value
> + * @hardware_value:hardware value of quality
> + */
> +struct mtk_jpeg_enc_qlt {
> +	u8	quality_param;
> +	u8	hardware_value;
> +};
> +
> +/**
> + * struct mt_jpeg_enc_bs - JPEG encoder bitstream  buffer
> + * @dma_addr:			JPEG encoder destination address
> + * @size:			JPEG encoder bistream size
> + * @dma_addr_offset:		JPEG encoder offset address
> + * @dma_addr_offsetmask:	JPEG encoder destination address offset mask
> + */
> +struct mtk_jpeg_enc_bs {
> +	dma_addr_t	dma_addr;
> +	size_t		size;
> +	u32			dma_addr_offset;
> +	u32			dma_addr_offsetmask;

Something seems to be wrong with the indentation here. In general it's
advisable to avoid using tabs between types and field names and just have a
single space, because changing some types or adding new fields could break
the indentation if tabs are used.

> +};
> +
> +/**
> + * struct mtk_jpeg_mem - JPEG memory
> + * @dma_addr:		memory address
> + * @size:		memory size
> + */
> +struct mtk_jpeg_mem {
> +	dma_addr_t	dma_addr;
> +	size_t		size;
> +};

Do we need this intermediate struct? Perhaps we could just get these
directly from vb2?

> +
> +/**
> + * struct mtk_jpeg_enc_fb - JPEG encoder frame buffer
> + * @fb_addr:		frmae buffer memory information
> + * @num_planes:		number of planes
> + */
> +struct mtk_jpeg_enc_fb {
> +	struct mtk_jpeg_mem	fb_addr[MTK_JPEG_COMP_MAX];

Since it's just a small struct, I'd just suggest using VIDEO_MAX_PLANES.

> +	u32			num_planes;
> +};

Or actually just dropping this struct at all. We should be able to just
directly get these parameters from vb2 whenever needed.

> +
> +void mtk_jpeg_enc_reset(void __iomem *base);
> +u32 mtk_jpeg_enc_get_int_status(void __iomem *base);
> +u32 mtk_jpeg_enc_get_file_size(void __iomem *base);
> +u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status);
> +void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height);
> +void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format);
> +void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
> +			      u32 height);
> +void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
> +			     u32 height, u32 bytesperline);
> +void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
> +			       u32 src_addr_c);
> +void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
> +			       u32 stall_size, u32 init_offset,
> +			       u32 offset_mask);
> +void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
> +			       u32 restart_interval);
> +void mtk_jpeg_enc_start(void __iomem *enc_reg_base);
> +
> +#endif /* _MTK_JPEG_ENC_HW_H */
> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
> new file mode 100644
> index 000000000000..cec3631addc5
> --- /dev/null
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
> @@ -0,0 +1,78 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Copyright (c) 2019 MediaTek Inc.
> + * Author: Xia Jiang <xia.jiang@mediatek.com>
> + *
> + */
> +#ifndef _MTK_JPEG_ENC_REG_H
> +#define _MTK_JPEG_ENC_REG_H
> +
> +#define JPEG_ENC_INT_STATUS_DONE			BIT(0)
> +#define JPEG_ENC_INT_STATUS_STALL			BIT(1)
> +#define JPEG_ENC_INT_STATUS_VCODEC_IRQ		BIT(4)
> +#define JPEG_ENC_INT_STATUS_MASK_ALLIRQ		0x13
> +
> +#define JPEG_ENC_DST_ADDR_OFFSET_MASK GENMASK(3, 0)
> +
> +#define JPEG_ENC_CTRL_YUV_FORMAT_MASK 0x18
> +#define JPEG_ENC_CTRL_RESTART_EN_BIT            BIT(10)
> +#define JPEG_ENC_CTRL_FILE_FORMAT_BIT           BIT(5)
> +#define JPEG_ENC_EN_JFIF_EXIF                   BIT(5)
> +#define JPEG_ENC_CTRL_INT_EN_BIT                BIT(2)
> +#define JPEG_ENC_CTRL_ENABLE_BIT                BIT(0)
> +#define JPEG_ENC_RESET_BIT                      BIT(0)
> +
> +#define JPEG_ENC_YUV_FORMAT_YUYV 0
> +#define JPEG_ENC_YUV_FORMAT_YVYU 1
> +#define JPEG_ENC_YUV_FORMAT_NV12 2
> +#define JEPG_ENC_YUV_FORMAT_NV21 3
> +
> +#define JPEG_ENC_QUALITY_Q60 0x0
> +#define JPEG_ENC_QUALITY_Q80 0x1
> +#define JPEG_ENC_QUALITY_Q90 0x2
> +#define JPEG_ENC_QUALITY_Q95 0x3
> +#define JPEG_ENC_QUALITY_Q39 0x4
> +#define JPEG_ENC_QUALITY_Q68 0x5
> +#define JPEG_ENC_QUALITY_Q84 0x6
> +#define JPEG_ENC_QUALITY_Q92 0x7
> +#define JPEG_ENC_QUALITY_Q48 0x8
> +#define JPEG_ENC_QUALITY_Q74 0xa
> +#define JPEG_ENC_QUALITY_Q87 0xb
> +#define JPEG_ENC_QUALITY_Q34 0xc
> +#define JPEG_ENC_QUALITY_Q64 0xe
> +#define JPEG_ENC_QUALITY_Q82 0xf
> +#define JPEG_ENC_QUALITY_Q97 0x10
> +
> +#define JPEG_ENC_RSTB				0x100
> +#define JPEG_ENC_CTRL				0x104
> +#define JPEG_ENC_QUALITY			0x108
> +#define JPEG_ENC_BLK_NUM			0x10C
> +#define JPEG_ENC_BLK_CNT			0x110
> +#define JPEG_ENC_INT_STS			0x11c
> +#define JPEG_ENC_DST_ADDR0			0x120
> +#define JPEG_ENC_DMA_ADDR0			0x124
> +#define JPEG_ENC_STALL_ADDR0		0x128
> +#define JPEG_ENC_OFFSET_ADDR		0x138
> +#define JPEG_ENC_RST_MCU_NUM		0x150
> +#define JPEG_ENC_IMG_SIZE			0x154
> +#define JPEG_ENC_DEBUG_INFO0		0x160
> +#define JPEG_ENC_DEBUG_INFO1		0x164
> +#define JPEG_ENC_TOTAL_CYCLE		0x168
> +#define JPEG_ENC_BYTE_OFFSET_MASK	0x16c
> +#define JPEG_ENC_SRC_LUMA_ADDR		0x170
> +#define JPEG_ENC_SRC_CHROMA_ADDR	0x174
> +#define JPEG_ENC_STRIDE				0x178
> +#define JPEG_ENC_IMG_STRIDE			0x17c
> +#define JPEG_ENC_DCM_CTRL			0x300
> +#define JPEG_ENC_CODEC_SEL			0x314
> +#define JPEG_ENC_ULTRA_THRES		0x318

nit: Please make sure all the macro values are aligned nicely, using tabs. 

> +
> +#define JPEG_ENC_FORMAT(x)		(((x) & 3) << 3)
> +#define JPEG_ENC_WIDTH_HEIGHT(w, h)	(((w) << 16) | (h))
> +#define JPEG_ENC_INIT_OFFSET(x)		((x) & (~0xf))
> +#define JPEG_ENC_OFFSET_MASK(x)		((x) & 0xf)
> +#define JPEG_ENC_DST_ADDR(x)		((x) & (~0xf))
> +#define JPEG_ENC_STALL_ADDR(x, y)		(((x) + (y)) & (~0xf))

For the offset and address calculation, please just open code in the
function that writes the registers, as they're not specific hardware
register layouts, but just simple calculations.

> +#define JPEG_ENC_SET_QUALITY(x, y)	(((x) & 0xffff0000) | (y))

That's an unusual pattern. Normally macros just generate the values for the
target bitfield. It's responsibility of the caller to OR it with the
rest of the register. That way it's clear what the code does from reading
it.

Best regards,
Tomasz


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 03/11] media: platform: Improve s_selection flow for bug fixing
  2020-03-03 12:34 ` [PATCH v7 03/11] media: platform: Improve s_selection " Xia Jiang
@ 2020-03-11 11:02   ` Tomasz Figa
  0 siblings, 0 replies; 18+ messages in thread
From: Tomasz Figa @ 2020-03-11 11:02 UTC (permalink / raw)
  To: Xia Jiang
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, Linux Media Mailing List,
	linux-devicetree, Linux Kernel Mailing List,
	list@263.net:IOMMU DRIVERS
	<iommu@lists.linux-foundation.org>,
	Joerg Roedel <joro@8bytes.org>,,
	moderated list:ARM/Mediatek SoC support, Marek Szyprowski,
	srv_heupstream

Hi Xia,

On Tue, Mar 3, 2020 at 9:35 PM Xia Jiang <xia.jiang@mediatek.com> wrote:
>
> Get correct compose value in mtk_jpeg_s_selection function.

It's a good practice to describe why the current code is wrong and new is good.

>
> Signed-off-by: Xia Jiang <xia.jiang@mediatek.com>
> ---
>  drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>

Thanks for the patch. Please see my comment inline.

> diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
> index da0dae4b0fc9..fb2c8d026580 100644
> --- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
> @@ -492,8 +492,8 @@ static int mtk_jpeg_s_selection(struct file *file, void *priv,
>         case V4L2_SEL_TGT_COMPOSE:
>                 s->r.left = 0;
>                 s->r.top = 0;
> -               s->r.width = ctx->out_q.w;
> -               s->r.height = ctx->out_q.h;
> +               ctx->out_q.w = s->r.width;
> +               ctx->out_q.h = s->r.height;
>                 break;
>         default:
>                 return -EINVAL;

The decoder compose target means the visible rectangle of the decoded
image, which comes from the stream metadata. It's not something that
can be set by the userspace.

Best regards,
Tomasz

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-03-06 11:23   ` Tomasz Figa
@ 2020-04-16  4:03     ` Xia Jiang
  2020-05-01 17:37       ` Tomasz Figa
  0 siblings, 1 reply; 18+ messages in thread
From: Xia Jiang @ 2020-04-16  4:03 UTC (permalink / raw)
  To: Tomasz Figa
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, linux-media, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, Marek Szyprowski,
	srv_heupstream

On Fri, 2020-03-06 at 20:23 +0900, Tomasz Figa wrote:
> Hi Xia,
> 
> On Tue, Mar 03, 2020 at 08:34:46PM +0800, Xia Jiang wrote:
> > Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
> > decode and encode have great similarities with function operation.
> 
> Thank you for the patch. Please see my comments inline.

Dear Tomasz,

Thank you for your reply. I have followed your advice and submited v8
version patch.

Please check my reply below.
> [snip]
> 
> > @@ -51,6 +54,42 @@ static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
> >  		.v_align	= 3,
> >  		.flags		= MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
> >  	},
> > +	{
> > +		.fourcc		= V4L2_PIX_FMT_NV12M,
> > +		.h_sample	= {4, 2, 2},
> > +		.v_sample	= {4, 2, 2},
> > +		.colplanes	= 2,
> > +		.h_align	= 4,
> > +		.v_align	= 4,
> > +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> > +	},
> > +	{
> > +		.fourcc		= V4L2_PIX_FMT_NV21M,
> > +		.h_sample	= {4, 2, 2},
> > +		.v_sample	= {4, 2, 2},
> > +		.colplanes	= 2,
> > +		.h_align	= 4,
> > +		.v_align	= 4,
> > +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> > +	},
> > +	{
> > +		.fourcc		= V4L2_PIX_FMT_YUYV,
> > +		.h_sample	= {4, 2, 2},
> > +		.v_sample	= {4, 4, 4},
> > +		.colplanes	= 1,
> > +		.h_align	= 4,
> > +		.v_align	= 3,
> > +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> > +	},
> > +	{
> > +		.fourcc		= V4L2_PIX_FMT_YVYU,
> > +		.h_sample	= {4, 2, 2},
> > +		.v_sample	= {4, 4, 4},
> > +		.colplanes	= 1,
> > +		.h_align	= 4,
> > +		.v_align	= 3,
> > +		.flags		= MTK_JPEG_FMT_FLAG_ENC_OUTPUT,
> > +	},
> >  };
> 
> Since the encoder and decoder don't seem to share almost any format,
> perhaps it would make sense to just have separate format arrays for both?
done.
> 
> >  
> >  #define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
> > @@ -70,6 +109,11 @@ struct mtk_jpeg_src_buf {
> >  static int debug;
> >  module_param(debug, int, 0644);
> >  
> > +static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
> > +{
> > +	return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
> > +}
> > +
> >  static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
> >  {
> >  	return container_of(fh, struct mtk_jpeg_ctx, fh);
> > @@ -86,14 +130,69 @@ static int mtk_jpeg_querycap(struct file *file, void *priv,
> >  {
> >  	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
> >  
> > -	strscpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
> > -	strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
> > +	strscpy(cap->driver, MTK_JPEG_NAME, sizeof(cap->driver));
> > +	if (jpeg->mode ==  MTK_JPEG_ENC)
> > +		strscpy(cap->card, MTK_JPEG_NAME " encoder", sizeof(cap->card));
> > +	else
> > +		strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
> >  	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
> >  		 dev_name(jpeg->dev));
> >  
> >  	return 0;
> >  }
> >  
> > +static int vidioc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > +	struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
> > +	int ret = 0;
> > +
> > +	switch (ctrl->id) {
> > +	case V4L2_CID_JPEG_RESTART_INTERVAL:
> > +		ctx->restart_interval = ctrl->val;
> > +		break;
> > +	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
> > +		ctx->enc_quality = ctrl->val;
> > +		break;
> > +	case V4L2_CID_JPEG_ACTIVE_MARKER:
> > +		ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
> > +		break;
> > +	}
> > +
> > +	return ret;
> 
> Wouldn't it be enough to just return 0 directly and remove ret?
yes,done.
> 
> > +}
> > +
> > +static const struct v4l2_ctrl_ops mtk_jpeg_ctrl_ops = {
> > +	.s_ctrl = vidioc_jpeg_s_ctrl,
> > +};
> > +
> > +static int mtk_jpeg_ctrls_setup(struct mtk_jpeg_ctx *ctx)
> > +{
> > +	const struct v4l2_ctrl_ops *ops = &mtk_jpeg_ctrl_ops;
> > +	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
> > +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> > +
> > +	v4l2_ctrl_handler_init(handler, 3);
> > +
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL,
> > +				  0, 100, 1, 0);
> > +		v4l2_ctrl_new_std(handler, ops,
> > +				  V4L2_CID_JPEG_COMPRESSION_QUALITY, 48, 100, 1,
> > +				  90);
> > +		v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
> > +				  V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
> > +
> > +		if (handler->error) {
> > +			v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> > +			return handler->error;
> > +		}
> > +	}
> > +
> > +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> > +
> > +	return 0;
> > +}
> > +
> >  static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
> >  			     struct v4l2_fmtdesc *f, u32 type)
> >  {
> > @@ -118,19 +217,27 @@ static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
> >  static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
> >  				     struct v4l2_fmtdesc *f)
> >  {
> > +	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> > +
> >  	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
> > +				 ctx->jpeg->mode == MTK_JPEG_ENC ?
> > +				 MTK_JPEG_FMT_FLAG_ENC_CAPTURE :
> >  				 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
> 
> We wouldn't need this special casing if we separate the format arrays.
done
> 
> >  }
> >  
> >  static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
> >  				     struct v4l2_fmtdesc *f)
> >  {
> > +	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> > +
> >  	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
> > +				 ctx->jpeg->mode == MTK_JPEG_ENC ?
> > +				 MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
> >  				 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
> 
> Ditto.
done.
> 
> >  }
> >  
> > -static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
> > -						   enum v4l2_buf_type type)
> > +static struct mtk_jpeg_q_data *
> > +mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx, enum v4l2_buf_type type)
> >  {
> >  	if (V4L2_TYPE_IS_OUTPUT(type))
> >  		return &ctx->out_q;
> > @@ -143,9 +250,14 @@ static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
> >  {
> >  	unsigned int k, fmt_flag;
> >  
> > -	fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> > -		   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
> > -		   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
> > +	if (ctx->jpeg->mode ==  MTK_JPEG_ENC)
> > +		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> > +			   MTK_JPEG_FMT_FLAG_ENC_OUTPUT :
> > +			   MTK_JPEG_FMT_FLAG_ENC_CAPTURE;
> > +	else
> > +		fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
> > +			   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
> > +			   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
> 
> Ditto.
done.
> 
> >  
> >  	for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
> >  		struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
> > @@ -177,9 +289,73 @@ static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
> >  	}
> >  }
> >  
> > -static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
> > -				   struct mtk_jpeg_fmt *fmt,
> > -				   struct mtk_jpeg_ctx *ctx, int q_type)
> > +static int mtk_jpeg_try_enc_fmt_mplane(struct v4l2_format *f,
> > +				       struct mtk_jpeg_fmt *fmt,
> > +				       struct mtk_jpeg_ctx *ctx, int q_type)
> > +{
> > +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> > +	int i;
> > +
> > +	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
> 
> Zeroing the reserved fields is handled by the core.
removed it.
> 
> > +	pix_mp->field = V4L2_FIELD_NONE;
> > +
> > +	if (ctx->state != MTK_JPEG_INIT) {
> 
> Is this really the right condition? As per V4L2 semantics, the format can't
> be changed when the queue has buffers allocated (vb2_is_busy()). Also,
> CAPTURE queue of the encoder doesn't have anything that could be changed,
> as the pixelformat is always JPEG and the rest determined by the OUTPUT
> queue, so perhaps it should have all the G_/TRY_/S_FMT ioctls implemented
> like G_FMT?
I have canceld the state for jpeg encoder.

> > +		mtk_jpeg_adjust_fmt_mplane(ctx, f);
> 
> The name of thi function sounds like it adjusts something, but it
> effectively just gets the current format. Why not just call the
> implementation of G_FMT directly?
yes, use G_FMT for jpeg decoder and deleted this function.
> 
> > +		return 0;
> > +	}
> > +
> > +	pix_mp->num_planes = fmt->colplanes;
> > +	pix_mp->pixelformat = fmt->fourcc;
> > +
> > +	if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
> 
> Why the custom queue type macro? Could we use standard definitions instead?
> I.e. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE.
done.
> 
> > +		if (pix_mp->num_planes == 1U) {
> 
> nit: It is unusual to add the U prefix in comparisions like this.
> 
> > +			pix_mp->height = clamp(round_up(pix_mp->height, 8),
> > +					       MTK_JPEG_MIN_HEIGHT,
> > +					       MTK_JPEG_MAX_HEIGHT);
> > +			pix_mp->width = clamp(round_up(pix_mp->width, 32),
> > +					      MTK_JPEG_MIN_WIDTH,
> > +					      MTK_JPEG_MAX_WIDTH);
> > +			pix_mp->plane_fmt[0].bytesperline = pix_mp->width * 2;
> > +			pix_mp->plane_fmt[0].sizeimage =
> > +				pix_mp->width * pix_mp->height * 2;
> > +		}
> > +		if (pix_mp->num_planes == 2U) {
> > +			pix_mp->height = clamp(round_up(pix_mp->height, 16),
> > +					       MTK_JPEG_MIN_HEIGHT,
> > +					       MTK_JPEG_MAX_HEIGHT);
> > +			pix_mp->width = clamp(round_up(pix_mp->width, 16),
> > +					      MTK_JPEG_MIN_WIDTH,
> > +					      MTK_JPEG_MAX_WIDTH);
> > +			for (i = 0; i < pix_mp->num_planes; i++) {
> > +				pix_mp->plane_fmt[i].bytesperline =
> > +					pix_mp->width;
> > +				pix_mp->plane_fmt[i].sizeimage =
> > +					pix_mp->width * pix_mp->height /
> > +					(i + 1);
> > +			}
> > +		}
> > +	} else {
> > +		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
> > +				       MTK_JPEG_MAX_HEIGHT);
> > +		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
> > +				      MTK_JPEG_MAX_WIDTH);
> > +
> > +		if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
> > +			pix_mp->plane_fmt[0].bytesperline = 0;
> > +			pix_mp->plane_fmt[0].sizeimage =
> > +				round_up(pix_mp->plane_fmt[0].sizeimage, 128);
> > +			if (pix_mp->plane_fmt[0].sizeimage == 0)
> > +				pix_mp->plane_fmt[0].sizeimage =
> > +					MTK_JPEG_DEFAULT_SIZEIMAGE;
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> 
> It doesn't look like the function above does anything specific to the
> encoder. It just does generic format adjustment.
> 
> Could we just add various format parameters to the mtk_jpeg_fmt struct and
> have a generic function that is shared by both encoder and decoder? That
> would also let us remove the conditional code for queue type, because the
> operations below actually depend on the format, not the queue.
done.
> 
> > +
> > +static int mtk_jpeg_try_dec_fmt_mplane(struct v4l2_format *f,
> > +				       struct mtk_jpeg_fmt *fmt,
> > +				       struct mtk_jpeg_ctx *ctx, int q_type)
> >  {
> >  	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> >  	int i;
> > @@ -286,6 +462,7 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> >  {
> >  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> >  	struct mtk_jpeg_fmt *fmt;
> > +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> >  
> >  	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
> >  				   MTK_JPEG_FMT_TYPE_CAPTURE);
> > @@ -299,7 +476,12 @@ static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> >  		 (fmt->fourcc >> 16 & 0xff),
> >  		 (fmt->fourcc >> 24 & 0xff));
> >  
> > -	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
> > +	if (jpeg->mode == MTK_JPEG_ENC)
> > +		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
> > +						   MTK_JPEG_FMT_TYPE_CAPTURE);
> > +	else
> > +		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
> > +						   MTK_JPEG_FMT_TYPE_CAPTURE);
> 
> With a generic format adjustment function we wouldn't need this special
> casing.
done.
> 
> >  }
> >  
> >  static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
> > @@ -307,6 +489,7 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
> >  {
> >  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> >  	struct mtk_jpeg_fmt *fmt;
> > +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> >  
> >  	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
> >  				   MTK_JPEG_FMT_TYPE_OUTPUT);
> > @@ -320,7 +503,12 @@ static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
> >  		 (fmt->fourcc >> 16 & 0xff),
> >  		 (fmt->fourcc >> 24 & 0xff));
> >  
> > -	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
> > +	if (jpeg->mode == MTK_JPEG_ENC)
> > +		return mtk_jpeg_try_enc_fmt_mplane(f, fmt, ctx,
> > +						   MTK_JPEG_FMT_TYPE_OUTPUT);
> > +	else
> > +		return mtk_jpeg_try_dec_fmt_mplane(f, fmt, ctx,
> > +						   MTK_JPEG_FMT_TYPE_OUTPUT);
> 
> Ditto.
done.
> 
> >  }
> >  
> >  static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
> > @@ -425,28 +613,48 @@ static int mtk_jpeg_g_selection(struct file *file, void *priv,
> >  				struct v4l2_selection *s)
> >  {
> >  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> > +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> >  
> > -	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> > -		return -EINVAL;
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
> > +			return -EINVAL;
> 
> Instead of repeating the if/else in every callback, could we instead
> provide separate ops structs for encoder and decoder so they can have
> separate callbacks for those that need to differ significantly?
done.
> 
> >  
> > -	switch (s->target) {
> > -	case V4L2_SEL_TGT_COMPOSE:
> > -	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
> > -		s->r.width = ctx->out_q.w;
> > -		s->r.height = ctx->out_q.h;
> > -		s->r.left = 0;
> > -		s->r.top = 0;
> > -		break;
> > -	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
> > -	case V4L2_SEL_TGT_COMPOSE_PADDED:
> > -		s->r.width = ctx->cap_q.w;
> > -		s->r.height = ctx->cap_q.h;
> > -		s->r.left = 0;
> > -		s->r.top = 0;
> > -		break;
> > -	default:
> > -		return -EINVAL;
> > +		switch (s->target) {
> > +		case V4L2_SEL_TGT_CROP:
> > +		case V4L2_SEL_TGT_CROP_BOUNDS:
> > +		case V4L2_SEL_TGT_CROP_DEFAULT:
> > +			s->r.width = ctx->out_q.w;
> > +			s->r.height = ctx->out_q.h;
> > +			s->r.left = 0;
> > +			s->r.top = 0;
> > +			break;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	} else {
> > +		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> > +			return -EINVAL;
> > +
> > +		switch (s->target) {
> > +		case V4L2_SEL_TGT_COMPOSE:
> > +		case V4L2_SEL_TGT_COMPOSE_DEFAULT:
> > +			s->r.width = ctx->out_q.w;
> > +			s->r.height = ctx->out_q.h;
> > +			s->r.left = 0;
> > +			s->r.top = 0;
> > +			break;
> > +		case V4L2_SEL_TGT_COMPOSE_BOUNDS:
> > +		case V4L2_SEL_TGT_COMPOSE_PADDED:
> > +			s->r.width = ctx->cap_q.w;
> > +			s->r.height = ctx->cap_q.h;
> > +			s->r.left = 0;
> > +			s->r.top = 0;
> > +			break;
> > +		default:
> > +			return -EINVAL;
> > +		}
> >  	}
> > +
> >  	return 0;
> >  }
> >  
> > @@ -454,20 +662,38 @@ static int mtk_jpeg_s_selection(struct file *file, void *priv,
> >  				struct v4l2_selection *s)
> >  {
> >  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
> > +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> >  
> > -	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> > -		return -EINVAL;
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
> > +			return -EINVAL;
> 
> Ditto.
done.
> 
> >  
> > -	switch (s->target) {
> > -	case V4L2_SEL_TGT_COMPOSE:
> > -		s->r.left = 0;
> > -		s->r.top = 0;
> > -		ctx->out_q.w = s->r.width;
> > -		ctx->out_q.h = s->r.height;
> > -		break;
> > -	default:
> > -		return -EINVAL;
> > +		switch (s->target) {
> > +		case V4L2_SEL_TGT_CROP:
> > +			s->r.left = 0;
> > +			s->r.top = 0;
> > +			ctx->out_q.w = s->r.width;
> > +			ctx->out_q.h = s->r.height;
> 
> What happens if the userspace provides a value bigger than current format?
we need get the min value of userspace value and current value,changed
it like this:
ctx->out_q.w = min(s->r.width, ctx->out_q.w);
ctx->out_q.h = min(s->r.height,ctx->out_q.h);
> 
> > +			break;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	} else {
> > +		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> > +			return -EINVAL;
> > +
> > +		switch (s->target) {
> > +		case V4L2_SEL_TGT_COMPOSE:
> > +			s->r.left = 0;
> > +			s->r.top = 0;
> > +			ctx->out_q.w = s->r.width;
> > +			ctx->out_q.h = s->r.height;
> 
> This is about existing code, but perhaps needs to be fixed. Shouldn't the
> decoder compose rectangle be read-only, as it means the visible size of the
> decoded JPEG as per the image metadata?
yes,I misunderstood it.The orignal code is right:
s->r.width = ctx->out_q.w;
s->r.height = ctx->out_q.h;
> 
> > +			break;
> > +		default:
> > +			return -EINVAL;
> > +		}
> >  	}
> > +
> >  	return 0;
> >  }
> >  
> > @@ -643,29 +869,42 @@ static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
> >  		goto end;
> >  
> >  	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
> > -	param = &jpeg_src_buf->dec_param;
> > -	memset(param, 0, sizeof(*param));
> > -
> > -	if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> > -		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
> > -		goto end;
> > -	}
> > -	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
> > -				      vb2_get_plane_payload(vb, 0));
> > -	if (!header_valid) {
> > -		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
> > -		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> > -		return;
> > -	}
> > -
> > -	if (ctx->state == MTK_JPEG_INIT) {
> > -		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
> > -			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> > +			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos");
> > +			goto end;
> > +		}
> > +		if (ctx->state == MTK_JPEG_INIT)
> > +			ctx->state = MTK_JPEG_RUNNING;
> 
> Why does the state change here? Queuing buffers is a repetitive operation.
> What's special about the first buffer?
> 
> Perhaps the state should change when we start streaming on both queues?
> 
> (On a side note, I don't see a need for ctx->state in case of the encoder.
> The information provided by vb2 by the means of vb2_is_busy() or
> vb2_is_streaming() should be enough.)
deleted the ctx->state in case of the encoder.
> 
> > +	} else {
> > +		param = &jpeg_src_buf->dec_param;
> > +		memset(param, 0, sizeof(*param));
> > +
> > +		if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
> > +			v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
> > +			goto end;
> > +		}
> > +		header_valid = mtk_jpeg_parse(param,
> > +					      (u8 *)vb2_plane_vaddr(vb, 0),
> > +					      vb2_get_plane_payload(vb, 0));
> > +		if (!header_valid) {
> > +			v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
> > +			vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> > +			return;
> > +		}
> >  
> > -		mtk_jpeg_queue_src_chg_event(ctx);
> > -		mtk_jpeg_set_queue_data(ctx, param);
> > -		ctx->state = vb2_is_streaming(dst_vq) ?
> > -				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
> > +		if (ctx->state == MTK_JPEG_INIT) {
> > +			struct vb2_queue *dst_vq;
> > +
> > +			dst_vq = v4l2_m2m_get_vq
> > +					(ctx->fh.m2m_ctx,
> > +					 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +			mtk_jpeg_queue_src_chg_event(ctx);
> > +			mtk_jpeg_set_queue_data(ctx, param);
> > +			ctx->state = vb2_is_streaming(dst_vq) ?
> > +					MTK_JPEG_SOURCE_CHANGE :
> > +					MTK_JPEG_RUNNING;
> > +		}
> >  	}
> >  end:
> >  	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
> 
> It sounds like we would also benefit from just having separate callbacks
> for encoder an decoder here.
done.
> 
> The code isn't included in this patch, but start_streaming implementation
> has a problem that I want to point out:
> 
> 	static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
> 	{
> 		struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
> 		struct vb2_v4l2_buffer *vb;
> 		int ret = 0;
> 	
> 		ret = pm_runtime_get_sync(ctx->jpeg->dev);
> 		if (ret < 0)
> 			goto err;
> 
> This is a memory to memory device, so it doesn't need to keep running all
> the time when streaming is enabled. One would normally call
> pm_runtime_get_sync() before starting a frame and then pm_runtime_put()
> after completing it. This would save power for the time between processing
> two frames.
I have removed pm_runtime_get_sync() to device_run() before starting a
frame and pm_runtime_put() to irq handler function after completing it.
> 
> 		return 0;
> 	err:
> 		while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
> 			v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED);
> 		return ret;
> 	}
> 
> > @@ -708,16 +947,16 @@ static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
> >  	 * subsampling. Update capture queue when the stream is off.
> >  	 */
> >  	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
> > -	    !V4L2_TYPE_IS_OUTPUT(q->type)) {
> > +	    !V4L2_TYPE_IS_OUTPUT(q->type) &&
> > +	    ctx->jpeg->mode == MTK_JPEG_DEC) {
> >  		struct mtk_jpeg_src_buf *src_buf;
> >  
> >  		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> >  		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
> >  		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
> >  		ctx->state = MTK_JPEG_RUNNING;
> > -	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
> > +	} else if (V4L2_TYPE_IS_OUTPUT(q->type))
> >  		ctx->state = MTK_JPEG_INIT;
> > -	}
> 
> Wouldn't it be possible that at this point the encoder is actually in
> process of encoding a frame? Wouldn't this code race with the interrupt
> handler?
have removed ctx->state of jpeg encoder.
> 
> >  
> >  	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
> >  		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
> > @@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
> >  	return 0;
> >  }
> >  
> > +static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> > +				 struct vb2_buffer *dst_buf,
> > +				 struct mtk_jpeg_enc_bs *bs)
> > +{
> > +	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > +	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;
> 
> Could you explain what is the meaning of the dma_addr_offset and where the
> default EXIF size comes from? Also, how is the encoder output affected by
> the enable_exif flag?
If enabled the exif mode, the real output will be filled at the locaiton
of dst_addr+ dma_addr_offset(exif size).The dma_addr_offset will be
filled by the application.
The default exif size is setted as constant value 64k according to the
spec.(Exif metadata are restricted in size to 64kB in JPEG images
because according to the specification this information must be
contained within a signed JPEG APP1 segment)
> 
> > +	bs->dma_addr_offsetmask = bs->dma_addr & JPEG_ENC_DST_ADDR_OFFSET_MASK;
> > +	bs->size = round_up(vb2_plane_size(dst_buf, 0), 128);
> 
> We can't round up the size that comes from vb2, because it could lead to a
> buffer overflow. We need to ensure that the hardware doesn't write more
> than the value returned by vb2_plane_size().
done.
> 
> > +
> > +	mtk_jpeg_enc_set_dst_addr(base, bs->dma_addr, bs->size,
> > +				  bs->dma_addr_offset,
> > +				  bs->dma_addr_offsetmask);
> > +}
> > +
> > +static void mtk_jpeg_set_enc_src(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> > +				 struct vb2_buffer *src_buf,
> > +				 struct mtk_jpeg_enc_fb *fb)
> > +{
> > +	int i;
> > +
> > +	mtk_jpeg_enc_set_img_size(base, ctx->out_q.w, ctx->out_q.h);
> > +	mtk_jpeg_enc_set_enc_format(base, ctx->out_q.fmt->fourcc);
> > +	mtk_jpeg_enc_set_blk_num(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
> > +				 ctx->out_q.h);
> > +	mtk_jpeg_enc_set_stride(base, ctx->out_q.fmt->fourcc, ctx->out_q.w,
> > +				ctx->out_q.h, ctx->out_q.bytesperline[0]);
> > +
> > +	for (i = 0; i < src_buf->num_planes; i++) {
> > +		fb->fb_addr[i].dma_addr =
> > +			vb2_dma_contig_plane_dma_addr(src_buf, i) +
> > +			src_buf->planes[i].data_offset;
> > +		fb->fb_addr[i].size = vb2_plane_size(src_buf, i) -
> > +				      src_buf->planes[i].data_offset;
> > +	}
> > +
> > +	mtk_jpeg_enc_set_src_addr(base, fb->fb_addr[0].dma_addr,
> > +				  fb->fb_addr[1].dma_addr);
> 
> What would be the second dma_addr in case of a format with 1 plane?
have changed it according to the num_planes:when it is 1, we just set
the first dma_addr,when it is 2, set both address.
> 
> > +}
> > +
> >  static void mtk_jpeg_device_run(void *priv)
> >  {
> >  	struct mtk_jpeg_ctx *ctx = priv;
> > @@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
> >  	struct mtk_jpeg_src_buf *jpeg_src_buf;
> >  	struct mtk_jpeg_bs bs;
> >  	struct mtk_jpeg_fb fb;
> > +	struct mtk_jpeg_enc_bs enc_bs;
> > +	struct mtk_jpeg_enc_fb enc_fb;
> >  	int i;
> >  
> >  	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> > @@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
> >  		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> >  			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
> >  		buf_state = VB2_BUF_STATE_DONE;
> 
> About existing code, but we may want to explain this.
> What is this last frame handling above for?
if the user gives us a empty buffer(means it is the last frame),the
driver will not encode and done the buffer to the user.

> > -		goto dec_end;
> > +		goto device_run_end;
> >  	}
> >  
> > -	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
> > -		mtk_jpeg_queue_src_chg_event(ctx);
> > -		ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > -		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > -		return;
> > -	}
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> 
> Why do we need to reset every frame?
We do this operation is to ensure that all registers are cleared.
It's safer from the hardware point of view.
> 
> > +
> > +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
> > +				     &enc_bs);
> > +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
> > +				     &enc_fb);
> > +		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
> > +					  ctx->enc_quality,
> > +					  ctx->restart_interval);
> > +
> > +		mtk_jpeg_enc_start(jpeg->reg_base);
> > +	} else {
> > +		if (mtk_jpeg_check_resolution_change
> > +			(ctx, &jpeg_src_buf->dec_param)) {
> > +			mtk_jpeg_queue_src_chg_event(ctx);
> > +			ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > +			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> 
> This is a bit strange. Resolution change should be signaled when the
> hardware attempted to decode a frame and detected a different resolution
> than current. It shouldn't be necessary for the userspace to queue a pair
> of buffers to signal it, as with the current code.
If the the resolution is bigger than current, the current buffer will
not be enough for the changed resolution.Shouldn't it tell the userspace
to queue new buffer and stream on again?
> 
> > +			return;
> > +		}
> >  
> > -	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
> > -	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
> > -		goto dec_end;
> > +		mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
> > +		if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param,
> > +					 &dst_buf->vb2_buf, &fb))
> > +			goto device_run_end;
> >  
> > -	spin_lock_irqsave(&jpeg->hw_lock, flags);
> > -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> > -	mtk_jpeg_dec_set_config(jpeg->dec_reg_base,
> > -				&jpeg_src_buf->dec_param, &bs, &fb);
> > +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> > +		mtk_jpeg_dec_reset(jpeg->reg_base);
> 
> Same here, why do we need to reset the decoder every frame? Shouldn't it be
> enough to reset it in runtime resume callback?
Ditto.
> 
> > +		mtk_jpeg_dec_set_config(jpeg->reg_base,
> > +					&jpeg_src_buf->dec_param, &bs, &fb);
> >  
> > -	mtk_jpeg_dec_start(jpeg->dec_reg_base);
> > +		mtk_jpeg_dec_start(jpeg->reg_base);
> > +	}
> >  	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
> >  	return;
> >  
> > -dec_end:
> > +device_run_end:
> >  	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
> >  	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
> >  	v4l2_m2m_buf_done(src_buf, buf_state);
> > @@ -875,30 +1172,30 @@ static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
> >  	ret = mtk_smi_larb_get(jpeg->larb);
> >  	if (ret)
> >  		dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
> > -	clk_prepare_enable(jpeg->clk_jdec_smi);
> > -	clk_prepare_enable(jpeg->clk_jdec);
> > +	if (jpeg->mode == MTK_JPEG_DEC)
> > +		clk_prepare_enable(jpeg->clk_jpeg_smi);
> 
> Could the clk_bulk_* API be used instead? It would eliminate the need for
> this special casing.
done.
> 
> > +	clk_prepare_enable(jpeg->clk_jpeg);
> >  }
> >  
> >  static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
> >  {
> > -	clk_disable_unprepare(jpeg->clk_jdec);
> > -	clk_disable_unprepare(jpeg->clk_jdec_smi);
> > +	clk_disable_unprepare(jpeg->clk_jpeg);
> > +	if (jpeg->mode == MTK_JPEG_DEC)
> > +		clk_disable_unprepare(jpeg->clk_jpeg_smi);
> 
> Ditto.
done.
> 
> >  	mtk_smi_larb_put(jpeg->larb);
> >  }
> >  
> > -static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
> > +static irqreturn_t mtk_jpeg_irq(int irq, void *priv)
> >  {
> >  	struct mtk_jpeg_dev *jpeg = priv;
> >  	struct mtk_jpeg_ctx *ctx;
> >  	struct vb2_v4l2_buffer *src_buf, *dst_buf;
> >  	struct mtk_jpeg_src_buf *jpeg_src_buf;
> >  	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
> > -	u32	dec_irq_ret;
> > -	u32 dec_ret;
> > +	u32 irq_ret;
> > +	u32 ret, result_size;
> >  	int i;
> >  
> > -	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
> > -	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
> >  	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
> >  	if (!ctx) {
> >  		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
> > @@ -909,32 +1206,89 @@ static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
> >  	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
> >  	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
> >  
> > -	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
> > -		mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> 
> As the hardware blocks are completely different, we definitely want to have
> separate interrupt handlers for them, rather than this special casing.
done.
> 
> > +		ret = mtk_jpeg_enc_get_int_status(jpeg->reg_base);
> > +		irq_ret = mtk_jpeg_enc_enum_result(jpeg->reg_base, ret);
> >  
> > -	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
> > -		dev_err(jpeg->dev, "decode failed\n");
> > -		goto dec_end;
> > -	}
> > +		if (irq_ret >= MTK_JPEG_ENC_RESULT_STALL)
> > +			mtk_jpeg_enc_reset(jpeg->reg_base);
> > +
> > +		if (irq_ret != MTK_JPEG_ENC_RESULT_DONE) {
> > +			dev_err(jpeg->dev, "encode failed\n");
> > +			goto irq_end;
> > +		}
> > +
> > +		result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
> > +		vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
> > +				      result_size);
> > +	} else {
> > +		ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
> > +		irq_ret = mtk_jpeg_dec_enum_result(ret);
> > +
> > +		if (irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
> > +			mtk_jpeg_dec_reset(jpeg->reg_base);
> >  
> > -	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> > -		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
> > -				      jpeg_src_buf->dec_param.comp_size[i]);
> > +		if (irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
> > +			dev_err(jpeg->dev, "decode failed\n");
> > +			goto irq_end;
> > +		}
> > +
> > +		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> > +			vb2_set_plane_payload
> > +				(&dst_buf->vb2_buf, i,
> > +				 jpeg_src_buf->dec_param.comp_size[i]);
> > +	}
> >  
> >  	buf_state = VB2_BUF_STATE_DONE;
> >  
> > -dec_end:
> > +irq_end:
> >  	v4l2_m2m_buf_done(src_buf, buf_state);
> >  	v4l2_m2m_buf_done(dst_buf, buf_state);
> >  	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> >  	return IRQ_HANDLED;
> >  }
> >  
> > -static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
> > +static void mtk_jpeg_set_enc_default_params(struct mtk_jpeg_ctx *ctx)
> > +{
> > +	struct mtk_jpeg_q_data *q = &ctx->out_q;
> > +
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> 
> Perhaps this could be just moved to the caller, rather than duplicating for
> encoder and decoder?
> 
> > +
> > +	ctx->colorspace = V4L2_COLORSPACE_JPEG,
> > +	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
> > +	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
> > +	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
> > +
> > +	q->w = MTK_JPEG_MIN_WIDTH;
> > +	q->h = MTK_JPEG_MIN_HEIGHT;
> > +
> > +	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
> > +				      MTK_JPEG_FMT_TYPE_OUTPUT);
> > +
> > +	q->w = clamp(round_up(q->w, 32), MTK_JPEG_MIN_WIDTH,
> > +		     MTK_JPEG_MAX_WIDTH);
> > +	q->h = clamp(round_up(q->h, 8), MTK_JPEG_MIN_HEIGHT,
> > +		     MTK_JPEG_MAX_HEIGHT);
> 
> Is this clamp() really needed? The code explicitly sets correct values
> few lines above.
> 
> > +
> > +	q->sizeimage[0] = q->w * q->h * 2;
> > +	q->bytesperline[0] = q->w * 2;
> 
> In general this kind of hardcoded computation is really error prone, e.g.
> if the default pixelformat changes for some reason. Could the same logic as
> for TRY_/S_FMT be called from here to compute these values?
done.
> > +
> > +	q = &ctx->cap_q;
> > +	q->w = MTK_JPEG_MIN_WIDTH;
> > +	q->h = MTK_JPEG_MIN_HEIGHT;
> > +	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
> > +				      MTK_JPEG_FMT_TYPE_CAPTURE);
> > +	q->bytesperline[0] = 0;
> > +	q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
> > +}
> > +
> > +static void mtk_jpeg_set_dec_default_params(struct mtk_jpeg_ctx *ctx)
> >  {
> >  	struct mtk_jpeg_q_data *q = &ctx->out_q;
> >  	int i;
> >  
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +
> >  	ctx->colorspace = V4L2_COLORSPACE_JPEG,
> >  	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
> >  	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
> > @@ -990,7 +1344,18 @@ static int mtk_jpeg_open(struct file *file)
> >  		goto error;
> >  	}
> >  
> > -	mtk_jpeg_set_default_params(ctx);
> > +	ret = mtk_jpeg_ctrls_setup(ctx);
> > +	if (ret) {
> > +		v4l2_err(&jpeg->v4l2_dev, "Failed to setup controls() (%d)\n",
> 
> nit: Is the "()" after "controls" necessary?
yes,changed.
> 
> > +			 ret);
> > +		goto error;
> > +	}
> > +
> > +	if (jpeg->mode == MTK_JPEG_ENC)
> > +		mtk_jpeg_set_enc_default_params(ctx);
> > +	else
> > +		mtk_jpeg_set_dec_default_params(ctx);
> > +
> >  	mutex_unlock(&jpeg->lock);
> >  	return 0;
> >  
> > @@ -1010,6 +1375,7 @@ static int mtk_jpeg_release(struct file *file)
> >  
> >  	mutex_lock(&jpeg->lock);
> >  	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
> > +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> >  	v4l2_fh_del(&ctx->fh);
> >  	v4l2_fh_exit(&ctx->fh);
> >  	kfree(ctx);
> > @@ -1043,19 +1409,24 @@ static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
> >  
> >  	jpeg->larb = &pdev->dev;
> >  
> > -	jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
> > -	if (IS_ERR(jpeg->clk_jdec))
> > -		return PTR_ERR(jpeg->clk_jdec);
> > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > +		jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgenc");
> > +		return PTR_ERR_OR_ZERO(jpeg->clk_jpeg);
> > +	}
> > +
> > +	jpeg->clk_jpeg = devm_clk_get(jpeg->dev, "jpgdec");
> > +	if (IS_ERR(jpeg->clk_jpeg))
> > +		return PTR_ERR(jpeg->clk_jpeg);
> >  
> > -	jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
> > -	return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
> > +	jpeg->clk_jpeg_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
> > +	return PTR_ERR_OR_ZERO(jpeg->clk_jpeg_smi);
> 
> I'd suggest putting the clock names in a match_data struct, which could be
> pointed to by the .data field of of_device_id. Then devm_clk_bulk_get()
> could be used generically, without the need to special case between encoder
> and decoder.
done.
> 
> >  }
> >  
> >  static int mtk_jpeg_probe(struct platform_device *pdev)
> >  {
> >  	struct mtk_jpeg_dev *jpeg;
> >  	struct resource *res;
> > -	int dec_irq;
> > +	int jpeg_irq;
> >  	int ret;
> >  
> >  	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
> > @@ -1065,28 +1436,26 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
> >  	mutex_init(&jpeg->lock);
> >  	spin_lock_init(&jpeg->hw_lock);
> >  	jpeg->dev = &pdev->dev;
> > +	jpeg->mode = (enum mtk_jpeg_mode)of_device_get_match_data(jpeg->dev);
> 
> As the match data is expected to be a pointer, it shouldn't be casted to a
> non-pointer value. Please define a struct with necessary hardware-specific
> data and have the .data field of of_device_id point to it.
done.
> 
> >  
> >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > -	jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
> > -	if (IS_ERR(jpeg->dec_reg_base)) {
> > -		ret = PTR_ERR(jpeg->dec_reg_base);
> > +	jpeg->reg_base = devm_ioremap_resource(&pdev->dev, res);
> > +	if (IS_ERR(jpeg->reg_base)) {
> > +		ret = PTR_ERR(jpeg->reg_base);
> >  		return ret;
> >  	}
> >  
> > -	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> > -	dec_irq = platform_get_irq(pdev, 0);
> > -	if (!res || dec_irq < 0) {
> > -		dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
> > -		ret = -EINVAL;
> 
> This is a bug fix, so plese do it in a separate patch, earlier in the
> series.
done.
> 
> > -		return ret;
> > +	jpeg_irq = platform_get_irq(pdev, 0);
> > +	if (jpeg_irq < 0) {
> > +		dev_err(&pdev->dev, "Failed to get jpeg_irq %d.\n", jpeg_irq);
> > +		return jpeg_irq;
> >  	}
> >  
> > -	ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
> > +	ret = devm_request_irq(&pdev->dev, jpeg_irq, mtk_jpeg_irq, 0,
> >  			       pdev->name, jpeg);
> >  	if (ret) {
> > -		dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
> > -			dec_irq, ret);
> > -		ret = -EINVAL;
> 
> Ditto. (Could be in one patch as the similar fix above.)
done.
> 
> > +		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
> > +			jpeg_irq, ret);
> >  		goto err_req_irq;
> >  	}
> >  
> > @@ -1110,33 +1479,35 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
> >  		goto err_m2m_init;
> >  	}
> >  
> > -	jpeg->dec_vdev = video_device_alloc();
> > -	if (!jpeg->dec_vdev) {
> > +	jpeg->vfd_jpeg = video_device_alloc();
> 
> Why "vfd"? "vdev" sounds more like video_device.
done.
> 
> Also, the "jpeg" suffix doesn't seem to be needed, as the parent struct is
> typically called "jpeg".
done.
> 
> > +	if (!jpeg->vfd_jpeg) {
> >  		ret = -ENOMEM;
> > -		goto err_dec_vdev_alloc;
> > +		goto err_vfd_jpeg_alloc;
> >  	}
> > -	snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
> > -		 "%s-dec", MTK_JPEG_NAME);
> > -	jpeg->dec_vdev->fops = &mtk_jpeg_fops;
> > -	jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
> > -	jpeg->dec_vdev->minor = -1;
> > -	jpeg->dec_vdev->release = video_device_release;
> > -	jpeg->dec_vdev->lock = &jpeg->lock;
> > -	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
> > -	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
> > -	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
> > +	snprintf(jpeg->vfd_jpeg->name, sizeof(jpeg->vfd_jpeg->name),
> > +		 "%s-%s", MTK_JPEG_NAME,
> > +		 jpeg->mode == MTK_JPEG_ENC ? "enc" : "dec");
> > +	jpeg->vfd_jpeg->fops = &mtk_jpeg_fops;
> > +	jpeg->vfd_jpeg->ioctl_ops = &mtk_jpeg_ioctl_ops;
> > +	jpeg->vfd_jpeg->minor = -1;
> > +	jpeg->vfd_jpeg->release = video_device_release;
> > +	jpeg->vfd_jpeg->lock = &jpeg->lock;
> > +	jpeg->vfd_jpeg->v4l2_dev = &jpeg->v4l2_dev;
> > +	jpeg->vfd_jpeg->vfl_dir = VFL_DIR_M2M;
> > +	jpeg->vfd_jpeg->device_caps = V4L2_CAP_STREAMING |
> >  				      V4L2_CAP_VIDEO_M2M_MPLANE;
> >  
> > -	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
> > +	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);
> 
> FYI the type changed to VFL_TYPE_VIDEO recently.
I changed VFL_TYPE_GRABBER to VFL_TYPE_VIDEO,but builded fail.
> >  	if (ret) {
> >  		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
> > -		goto err_dec_vdev_register;
> > +		goto err_vfd_jpeg_register;
> >  	}
> >  
> > -	video_set_drvdata(jpeg->dec_vdev, jpeg);
> > +	video_set_drvdata(jpeg->vfd_jpeg, jpeg);
> >  	v4l2_info(&jpeg->v4l2_dev,
> > -		  "decoder device registered as /dev/video%d (%d,%d)\n",
> > -		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
> > +		  "jpeg device %d registered as /dev/video%d (%d,%d)\n",
> 
> Here it would be actually useful to special case the encoder and decoder,
> because it would be easier for the user to know which device is which.
> 
> > +		  jpeg->mode, jpeg->vfd_jpeg->num, VIDEO_MAJOR,
> > +		  jpeg->vfd_jpeg->minor);
> >  
> >  	platform_set_drvdata(pdev, jpeg);
> >  
> > @@ -1144,10 +1515,10 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
> >  
> >  	return 0;
> >  
> > -err_dec_vdev_register:
> > -	video_device_release(jpeg->dec_vdev);
> > +err_vfd_jpeg_register:
> > +	video_device_release(jpeg->vfd_jpeg);
> >  
> > -err_dec_vdev_alloc:
> > +err_vfd_jpeg_alloc:
> >  	v4l2_m2m_release(jpeg->m2m_dev);
> >  
> >  err_m2m_init:
> > @@ -1167,8 +1538,8 @@ static int mtk_jpeg_remove(struct platform_device *pdev)
> >  	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
> >  
> >  	pm_runtime_disable(&pdev->dev);
> > -	video_unregister_device(jpeg->dec_vdev);
> > -	video_device_release(jpeg->dec_vdev);
> > +	video_unregister_device(jpeg->vfd_jpeg);
> > +	video_device_release(jpeg->vfd_jpeg);
> >  	v4l2_m2m_release(jpeg->m2m_dev);
> >  	v4l2_device_unregister(&jpeg->v4l2_dev);
> >  
> > @@ -1179,7 +1550,11 @@ static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
> >  {
> >  	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
> >  
> > -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> > +	if (jpeg->mode == MTK_JPEG_ENC)
> > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> > +	else
> > +		mtk_jpeg_dec_reset(jpeg->reg_base);
> 
> Is the reset here really necessary? We're going to cut down the power to
> the IP block some time after the function returns.
I have deleted it .
> 
> > +
> >  	mtk_jpeg_clk_off(jpeg);
> >  
> >  	return 0;
> > @@ -1190,7 +1565,10 @@ static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
> >  	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
> >  
> >  	mtk_jpeg_clk_on(jpeg);
> > -	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
> > +	if (jpeg->mode == MTK_JPEG_ENC)
> > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> > +	else
> > +		mtk_jpeg_dec_reset(jpeg->reg_base);
> >  
> >  	return 0;
> >  }
> 
> The code is not visible in this patch, but the implementation of the system
> PM ops doesn't look correct.
> 
> 	static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
> 	{
> 		int ret;
> 	
> 		if (pm_runtime_suspended(dev))
> 			return 0;
> 	
> 		ret = mtk_jpeg_pm_suspend(dev);
> 
> The hardware may be currently processing a frame. Wouldn't this cause
> issues? Also, wouldn't this race with the interrupt handler?
Yes.I have added lock mechanisms in this function and irq handler
function to avoid this.
> 
> Normally one would prevent the driver from scheduling next frames here,
> wait for the driver to finish processing the current frame and only then
> suspend.
> 
> 		return ret;
> 	}
> 	
> 	static __maybe_unused int mtk_jpeg_resume(struct device *dev)
> 	{
> 		int ret;
> 	
> 		if (pm_runtime_suspended(dev))
> 			return 0;
> 	
> 		ret = mtk_jpeg_pm_resume(dev);
> 
> Here one normally needs to unblock the driver from scheduling next frames.
> 	
> 		return ret;
> 	}
> 
> > @@ -1226,11 +1604,15 @@ static const struct dev_pm_ops mtk_jpeg_pm_ops = {
> >  static const struct of_device_id mtk_jpeg_match[] = {
> >  	{
> >  		.compatible = "mediatek,mt8173-jpgdec",
> > -		.data       = NULL,
> > +		.data       = (void *)MTK_JPEG_DEC,
> >  	},
> >  	{
> >  		.compatible = "mediatek,mt2701-jpgdec",
> > -		.data       = NULL,
> > +		.data       = (void *)MTK_JPEG_DEC,
> > +	},
> > +	{
> > +		.compatible = "mediatek,mtk-jpgenc",
> > +		.data       = (void *)MTK_JPEG_ENC,
> >  	},
> >  	{},
> >  };
> > diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> > index 9bbd615b1067..02a387d8be5d 100644
> > --- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> > +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.h
> > @@ -3,6 +3,7 @@
> >   * Copyright (c) 2016 MediaTek Inc.
> >   * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
> >   *         Rick Chang <rick.chang@mediatek.com>
> > + *         Xia Jiang <xia.jiang@mediatek.com>
> >   */
> >  
> >  #ifndef _MTK_JPEG_CORE_H
> > @@ -19,16 +20,19 @@
> >  
> >  #define MTK_JPEG_FMT_FLAG_DEC_OUTPUT	BIT(0)
> >  #define MTK_JPEG_FMT_FLAG_DEC_CAPTURE	BIT(1)
> > +#define MTK_JPEG_FMT_FLAG_ENC_OUTPUT	BIT(2)
> > +#define MTK_JPEG_FMT_FLAG_ENC_CAPTURE	BIT(3)
> >  
> >  #define MTK_JPEG_FMT_TYPE_OUTPUT	1
> >  #define MTK_JPEG_FMT_TYPE_CAPTURE	2
> >  
> >  #define MTK_JPEG_MIN_WIDTH	32U
> >  #define MTK_JPEG_MIN_HEIGHT	32U
> > -#define MTK_JPEG_MAX_WIDTH	8192U
> > -#define MTK_JPEG_MAX_HEIGHT	8192U
> > +#define MTK_JPEG_MAX_WIDTH	65535U
> > +#define MTK_JPEG_MAX_HEIGHT	65535U
> 
> Is this correct even for the decoder?
yes.
> 
> >  
> >  #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
> > +#define MTK_JPEG_DEFAULT_EXIF_SIZE	(64 * 1024)
> >  
> >  /**
> >   * enum mtk_jpeg_ctx_state - contex state of jpeg
> > @@ -39,6 +43,14 @@ enum mtk_jpeg_ctx_state {
> >  	MTK_JPEG_SOURCE_CHANGE,
> >  };
> >  
> > +/**
> > + * enum mtk_jpeg_mode - mode of jpeg
> > + */
> > +enum mtk_jpeg_mode {
> > +	MTK_JPEG_ENC,
> > +	MTK_JPEG_DEC,
> > +};
> > +
> 
> Perhaps bool is_encoder insted of an enum could simplify the code a bit?
I have deleted this.
> 
> >  /**
> >   * struct mt_jpeg - JPEG IP abstraction
> >   * @lock:		the mutex protecting this structure
> > @@ -48,11 +60,12 @@ enum mtk_jpeg_ctx_state {
> >   * @v4l2_dev:		v4l2 device for mem2mem mode
> >   * @m2m_dev:		v4l2 mem2mem device data
> >   * @alloc_ctx:		videobuf2 memory allocator's context
> > - * @dec_vdev:		video device node for decoder mem2mem mode
> > - * @dec_reg_base:	JPEG registers mapping
> > - * @clk_jdec:		JPEG hw working clock
> > - * @clk_jdec_smi:	JPEG SMI bus clock
> > + * @vfd_jpeg:		video device node for jpeg mem2mem mode
> > + * @reg_base:		JPEG registers mapping
> > + * @clk_jpeg:		JPEG hw working clock
> > + * @clk_jpeg_smi:	JPEG SMI bus clock
> >   * @larb:		SMI device
> > + * @mode:		compression (encode) operation or decompression (decode)
> >   */
> >  struct mtk_jpeg_dev {
> >  	struct mutex		lock;
> > @@ -62,11 +75,12 @@ struct mtk_jpeg_dev {
> >  	struct v4l2_device	v4l2_dev;
> >  	struct v4l2_m2m_dev	*m2m_dev;
> >  	void			*alloc_ctx;
> > -	struct video_device	*dec_vdev;
> > -	void __iomem		*dec_reg_base;
> > -	struct clk		*clk_jdec;
> > -	struct clk		*clk_jdec_smi;
> > +	struct video_device	*vfd_jpeg;
> > +	void __iomem		*reg_base;
> > +	struct clk		*clk_jpeg;
> > +	struct clk		*clk_jpeg_smi;
> >  	struct device		*larb;
> > +	enum mtk_jpeg_mode	mode;
> >  };
> >  
> >  /**
> > @@ -113,6 +127,7 @@ struct mtk_jpeg_q_data {
> >   * @cap_q:		destination (capture) queue queue information
> >   * @fh:			V4L2 file handle
> >   * @state:		state of the context
> > + * @ctrl_hdl:		controls handler
> >   * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
> >   * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
> >   * @quantization: enum v4l2_quantization, colorspace quantization
> > @@ -124,6 +139,10 @@ struct mtk_jpeg_ctx {
> >  	struct mtk_jpeg_q_data		cap_q;
> >  	struct v4l2_fh			fh;
> >  	enum mtk_jpeg_ctx_state		state;
> > +	u8				enable_exif;
> 
> bool?
yes.
> 
> > +	u8				enc_quality;
> > +	u8				restart_interval;
> > +	struct v4l2_ctrl_handler	ctrl_hdl;
> >  
> >  	enum v4l2_colorspace colorspace;
> >  	enum v4l2_ycbcr_encoding ycbcr_enc;
> > diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> > index 1cc37dbfc8e7..ce263db5f30a 100644
> > --- a/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> > +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_dec_hw.h
> > @@ -3,10 +3,11 @@
> >   * Copyright (c) 2016 MediaTek Inc.
> >   * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
> >   *         Rick Chang <rick.chang@mediatek.com>
> > + *         Xia Jiang <xia.jiang@mediatek.com>
> >   */
> >  
> > -#ifndef _MTK_JPEG_HW_H
> > -#define _MTK_JPEG_HW_H
> > +#ifndef _MTK_JPEG_DEC_HW_H
> > +#define _MTK_JPEG_DEC_HW_H
> >  
> >  #include <media/videobuf2-core.h>
> >  
> > @@ -75,4 +76,4 @@ void mtk_jpeg_dec_set_config(void __iomem *base,
> >  void mtk_jpeg_dec_reset(void __iomem *dec_reg_base);
> >  void mtk_jpeg_dec_start(void __iomem *dec_reg_base);
> >  
> > -#endif /* _MTK_JPEG_HW_H */
> > +#endif /* _MTK_JPEG_DEC_HW_H */
> > diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
> > new file mode 100644
> > index 000000000000..da3e7a83a80a
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.c
> > @@ -0,0 +1,273 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (c) 2019 MediaTek Inc.
> > + * Author: Xia Jiang <xia.jiang@mediatek.com>
> > + *
> > + */
> > +
> > +#include <linux/io.h>
> > +#include <linux/kernel.h>
> > +#include <media/videobuf2-core.h>
> > +
> > +#include "mtk_jpeg_enc_hw.h"
> > +
> > +static struct mtk_jpeg_enc_qlt mtk_jpeg_enc_quality[] = {
> 
> static const?
done.
> 
> > +	{
> > +		.quality_param		= 97,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q97,
> > +	},
> > +	{
> > +		.quality_param		= 95,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q95,
> > +	},
> > +	{
> > +		.quality_param		= 92,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q92,
> > +	},
> > +	{
> > +		.quality_param		= 90,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q90,
> > +	},
> > +	{
> > +		.quality_param		= 87,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q87,
> > +	},
> > +	{
> > +		.quality_param		= 84,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q84,
> > +	},
> > +	{
> > +		.quality_param		= 82,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q82,
> > +	},
> > +	{
> > +		.quality_param		= 80,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q80,
> > +	},
> > +	{
> > +		.quality_param		= 74,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q74,
> > +	},
> > +	{
> > +		.quality_param		= 68,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q68,
> > +	},
> > +	{
> > +		.quality_param		= 64,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q64,
> > +	},
> > +	{
> > +		.quality_param		= 60,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q60,
> > +	},
> > +	{
> > +		.quality_param		= 48,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q48,
> > +	},
> > +	{
> > +		.quality_param		= 39,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q39,
> > +	},
> > +	{
> > +		.quality_param		= 34,
> > +		.hardware_value	= JPEG_ENC_QUALITY_Q34,
> > +	},
> > +};
> 
> nit: Perhaps we could condense this a bit? E.g.
> 
> [...] = {
> 	{ .quality = XX, .hw = YY }.
> 	// ...
> };
done.
> 
> > +
> > +#define MTK_JPEG_ENC_NUM_QUALITY ARRAY_SIZE(mtk_jpeg_enc_quality)
> 
> I think just using ARRAY_SIZE() directly would make the code clearer (and
> it's similar length to the macro).
done.
> 
> > +
> > +void mtk_jpeg_enc_reset(void __iomem *base)
> > +{
> > +	writel(0x00, base + JPEG_ENC_RSTB);
> > +	writel(JPEG_ENC_RESET_BIT, base + JPEG_ENC_RSTB);
> > +	writel(0x00, base + JPEG_ENC_CODEC_SEL);
> > +}
> > +
> > +u32 mtk_jpeg_enc_get_int_status(void __iomem *base)
> > +{
> > +	u32 ret;
> > +
> > +	ret = readl(base + JPEG_ENC_INT_STS) &
> > +		    JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
> > +	if (ret)
> > +		writel(0, base + JPEG_ENC_INT_STS);
> > +
> > +	return ret;
> > +}
> 
> Does it make sense to have a function for what is essentially just 2 lines?
> Also, the name is misleading, as the function not only gets but also
> clears.
Make all hw register setting in mtk_jpeg_enc_hw.c is one part of current
architecture.
I have changed the function name to
mtk_jpeg_enc_get_and_clear_int_status.
> 
> > +
> > +u32 mtk_jpeg_enc_get_file_size(void __iomem *base)
> > +{
> > +	return readl(base + JPEG_ENC_DMA_ADDR0) -
> > +	       readl(base + JPEG_ENC_DST_ADDR0);
> > +}
> 
> This is called once from the interrupt handler, which is hardware-specific
> anyway, so one would normally just read the registers directly from there.
Make all hw register setting in mtk_jpeg_enc_hw.c is one part of current
architecture.
> 
> > +
> > +u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status)
> > +{
> > +	if (irq_status & JPEG_ENC_INT_STATUS_DONE)
> > +		return MTK_JPEG_ENC_RESULT_DONE;
> > +	else if (irq_status & JPEG_ENC_INT_STATUS_STALL)
> > +		return MTK_JPEG_ENC_RESULT_STALL;
> > +	else if (irq_status & JPEG_ENC_INT_STATUS_VCODEC_IRQ)
> > +		return MTK_JPEG_ENC_RESULT_VCODEC_IRQ;
> > +	return MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN;
> > +}
> 
> I see this function being called only once in the interrupt handler.
> Wouldn't it be more readable if the interrupt masks were just checked
> directly there?
Ditto.
> 
> > +
> > +void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height)
> > +{
> > +	u32 value;
> > +
> > +	value = JPEG_ENC_WIDTH_HEIGHT(width, height);
> > +	writel(value, base + JPEG_ENC_IMG_SIZE);
> > +}
> > +
> > +void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format)
> > +{
> > +	u32 yuv_format, value;
> > +
> > +	yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
> > +	switch (enc_format) {
> > +	case V4L2_PIX_FMT_YUYV:
> > +		yuv_format = JPEG_ENC_YUV_FORMAT_YUYV;
> > +		break;
> > +	case V4L2_PIX_FMT_YVYU:
> > +		yuv_format = JPEG_ENC_YUV_FORMAT_YVYU;
> > +		break;
> > +	case V4L2_PIX_FMT_NV12M:
> > +		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;
> > +		break;
> > +	case V4L2_PIX_FMT_NV21M:
> > +		yuv_format = JPEG_ENC_YUV_FORMAT_NV12;
> 
> Instead of the explicit switch here, we could just have the hardware format
> contained in the mtk_jpeg_fmt struct.
done.
> 
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +
> > +	value = readl(base + JPEG_ENC_CTRL);
> > +	value &= ~JPEG_ENC_CTRL_YUV_FORMAT_MASK;
> > +	value |= JPEG_ENC_FORMAT(yuv_format);
> > +	writel(value, base + JPEG_ENC_CTRL);
> > +}
> > +
> > +void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
> > +			      u32 height)
> > +{
> > +	u32 blk_num;
> > +	u32 is_420;
> > +	u32 padding_width;
> > +	u32 padding_height;
> > +
> > +	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
> > +		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;
> > +	padding_width = round_up(width, 16);
> > +	padding_height = round_up(height, is_420 ? 16 : 8);
> > +
> > +	blk_num = (padding_width >> 4) * (padding_height >> (is_420 ? 4 : 3)) *
> > +		  (is_420 ? 6 : 4) - 1;
> 
> The whole code above could be rewritten as below:
> 
> if (enc_format == V4L2_PIX_FMT_NV12M
>     || enc_format == V4L2_PIX_FMT_NV21M)
> 	blk_num = DIV_ROUND_UP(width, 16) * DIV_ROUND_UP(height, 16) * 6 - 1;
> else
> 	blk_num = DIV_ROUND_UP(width, 16) * DIV_ROUND_UP(height, 8) * 4 - 1;
> 
> However the magic numbers deserve some explanation, especially
> multiplication by 6 or 4 and subtraction of 1.
done.
> 
> > +
> > +	writel(blk_num, base + JPEG_ENC_BLK_NUM);
> > +}
> > +
> > +void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
> > +			     u32 height, u32 bytesperline)
> > +{
> > +	u32 width_even;
> > +	u32 is_420;
> 
> Perhaps should be bool?
yes.
> 
> > +	u32 img_stride;
> > +	u32 mem_stride;
> > +
> > +	width_even = round_up(width, 2);
> > +	is_420 = (enc_format == V4L2_PIX_FMT_NV12M ||
> > +		  enc_format == V4L2_PIX_FMT_NV21M) ? 1 : 0;
> 
> No need for the explicit 1 : 0.
> 
> > +	if (!is_420)
> > +		width_even = width_even << 1;
> > +	img_stride = round_up(width_even, (is_420 ? 16 : 32));
> > +	mem_stride = is_420 ? bytesperline : round_up(width_even, 32);
> 
> Wouldn't it make the code more readable if it was rewritten like below?
> 
> if (enc_format == V4L2_PIX_FMT_NV12M
>     || enc_format == V4L2_PIX_FMT_NV21M) {
>         /* 4:2:0 */
> 	img_stride = round_up(width, 16);
> 	mem_stride = bytesperline;
> } else {
> 	/* 4:2:2 */
> 	img_stride = round_up(width * 2, 32);
> 	mem_stride = img_stride;
> }
> 
done.
> > +
> > +	writel(img_stride, base + JPEG_ENC_IMG_STRIDE);
> > +	writel(mem_stride, base + JPEG_ENC_STRIDE);
> 
> Does the hardware assume that Y and CbCr planes have the same strides?
yes,we just set image stride and memory stride to hardware.
> 
> What is the difference between img_stride and mem_stride?
The image stride(picture real width stride ) is 32-byte aligned for
yuv420, 16-byte aligned for yuv420.
The memory stride must be 128-byte aligned.
In general, memory stride is bigger than image stride.
> 
> > +}
> > +
> > +void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
> > +			       u32 src_addr_c)
> > +{
> > +	writel(src_addr, base + JPEG_ENC_SRC_LUMA_ADDR);
> > +	writel(src_addr_c, base + JPEG_ENC_SRC_CHROMA_ADDR);
> > +}
> > +
> > +void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
> > +			       u32 stall_size, u32 init_offset,
> > +			       u32 offset_mask)
> > +{
> > +	writel(JPEG_ENC_INIT_OFFSET(init_offset), base + JPEG_ENC_OFFSET_ADDR);
> > +	writel(JPEG_ENC_OFFSET_MASK(offset_mask),
> > +	       base + JPEG_ENC_BYTE_OFFSET_MASK);
> > +	writel(JPEG_ENC_DST_ADDR(dst_addr), base + JPEG_ENC_DST_ADDR0);
> > +	writel(JPEG_ENC_STALL_ADDR(dst_addr, stall_size),
> > +	       base + JPEG_ENC_STALL_ADDR0);
> > +}
> > +
> > +static void mtk_jpeg_enc_set_quality(void __iomem *base, u32 quality)
> > +{
> > +	u32 value;
> > +	u32 i, enc_quality;
> > +
> > +	enc_quality = mtk_jpeg_enc_quality[0].hardware_value;
> 
> For values less than 34, shouldn't we use the lowest quality, not the
> highest?
Yes,I have fixed the bug.
> 
> > +	for (i = 0; i < MTK_JPEG_ENC_NUM_QUALITY; i++) {
> 
> Please use ARRAY_SIZE() instead of the macro to make sure there is no
> accidental out of bounds access.
done.
> 
> > +		if (quality >= mtk_jpeg_enc_quality[i].quality_param) {
> > +			enc_quality = mtk_jpeg_enc_quality[i].hardware_value;
> > +			break;
> > +		}
> > +	}
> > +
> > +	value = readl(base + JPEG_ENC_QUALITY);
> 
> Do we need to read the register?
> 
> > +	value = JPEG_ENC_SET_QUALITY(value, enc_quality);
> > +	writel(value, base + JPEG_ENC_QUALITY);
> > +}
> > +
> > +static void mtk_jpeg_enc_set_restart_interval(void __iomem *base,
> > +					      u32 restart_interval)
> > +{
> > +	u32 value;
> > +
> > +	value = readl(base + JPEG_ENC_CTRL);
> > +	if (restart_interval)
> > +		value |= JPEG_ENC_CTRL_RESTART_EN_BIT;
> > +	else
> > +		value &= ~JPEG_ENC_CTRL_RESTART_EN_BIT;
> > +	writel(value, base + JPEG_ENC_CTRL);
> > +	writel(restart_interval, base + JPEG_ENC_RST_MCU_NUM);
> > +}
> 
> Wouldn't it make more sense to just do all the register settings in one
> function, with comments? Splitting it like here introduces superfluous
> register reads and writes - we could just read JPEG_ENC_CTRL once, update
> all the fields in the local variable and write it once if done in one
> function.
done.
> 
> A typical approach is to have one function that receives the necessary V4L2
> context and then extracts necessary parameters directly from there and
> programs the registers. That reduces the level of abstraction and makes the
> code easier to follow for anyone familiar with V4L2.
> 
> > +
> > +static void mtk_jpeg_enc_set_encode_mode(void __iomem *base, u32 exif_en)
> > +{
> > +	u32 value;
> > +
> > +	value = readl(base + JPEG_ENC_CTRL);
> > +	value &= ~JPEG_ENC_CTRL_FILE_FORMAT_BIT;
> > +	writel(value, base + JPEG_ENC_CTRL);
> > +
> > +	if (exif_en) {
> > +		value = readl(base + JPEG_ENC_CTRL);
> 
> Do we need to read the register second time? Could we just read it once,
> take care of both fields and write once?
done.I have maked all JPEG_ENC_CTRL settings in one function and just
read once.
> 
> > +		value |= JPEG_ENC_EN_JFIF_EXIF;
> > +		writel(value, base + JPEG_ENC_CTRL);
> > +	}
> > +}
> > +
> > +void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
> > +			       u32 restart_interval)
> > +{
> > +	mtk_jpeg_enc_set_quality(base, quality);
> > +
> > +	mtk_jpeg_enc_set_restart_interval(base, restart_interval);
> > +
> > +	mtk_jpeg_enc_set_encode_mode(base, exif_en);
> > +}
> > +
> > +void mtk_jpeg_enc_start(void __iomem *base)
> > +{
> > +	u32 value;
> > +
> > +	value = readl(base + JPEG_ENC_CTRL);
> > +	value |= JPEG_ENC_CTRL_INT_EN_BIT | JPEG_ENC_CTRL_ENABLE_BIT;
> > +	writel(value, base + JPEG_ENC_CTRL);
> > +}
> > diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
> > new file mode 100644
> > index 000000000000..d0d7d218a0c2
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_hw.h
> > @@ -0,0 +1,86 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +/*
> > + * Copyright (c) 2019 MediaTek Inc.
> > + * Author: Xia Jiang <xia.jiang@mediatek.com>
> > + *
> > + */
> > +
> > +#ifndef _MTK_JPEG_ENC_HW_H
> > +#define _MTK_JPEG_ENC_HW_H
> > +
> > +#include <media/videobuf2-core.h>
> > +
> > +#include "mtk_jpeg_core.h"
> > +#include "mtk_jpeg_enc_reg.h"
> > +
> > +enum {
> > +	MTK_JPEG_ENC_RESULT_DONE		= 0,
> 
> nit: No need for explicit value assignment.
> 
> > +	MTK_JPEG_ENC_RESULT_STALL,
> > +	MTK_JPEG_ENC_RESULT_VCODEC_IRQ,
> > +	MTK_JPEG_ENC_RESULT_ERROR_UNKNOWN
> > +};
> > +
> > +/**
> > + * struct mtk_jpeg_enc_qlt - JPEG encoder quality data
> > + * @quality_param:	quality value
> > + * @hardware_value:hardware value of quality
> > + */
> > +struct mtk_jpeg_enc_qlt {
> > +	u8	quality_param;
> > +	u8	hardware_value;
> > +};
> > +
> > +/**
> > + * struct mt_jpeg_enc_bs - JPEG encoder bitstream  buffer
> > + * @dma_addr:			JPEG encoder destination address
> > + * @size:			JPEG encoder bistream size
> > + * @dma_addr_offset:		JPEG encoder offset address
> > + * @dma_addr_offsetmask:	JPEG encoder destination address offset mask
> > + */
> > +struct mtk_jpeg_enc_bs {
> > +	dma_addr_t	dma_addr;
> > +	size_t		size;
> > +	u32			dma_addr_offset;
> > +	u32			dma_addr_offsetmask;
> 
> Something seems to be wrong with the indentation here. In general it's
> advisable to avoid using tabs between types and field names and just have a
> single space, because changing some types or adding new fields could break
> the indentation if tabs are used.
> 
> > +};
> > +
> > +/**
> > + * struct mtk_jpeg_mem - JPEG memory
> > + * @dma_addr:		memory address
> > + * @size:		memory size
> > + */
> > +struct mtk_jpeg_mem {
> > +	dma_addr_t	dma_addr;
> > +	size_t		size;
> > +};
> 
> Do we need this intermediate struct? Perhaps we could just get these
> directly from vb2?
yes,I have deleted it.
> 
> > +
> > +/**
> > + * struct mtk_jpeg_enc_fb - JPEG encoder frame buffer
> > + * @fb_addr:		frmae buffer memory information
> > + * @num_planes:		number of planes
> > + */
> > +struct mtk_jpeg_enc_fb {
> > +	struct mtk_jpeg_mem	fb_addr[MTK_JPEG_COMP_MAX];
> 
> Since it's just a small struct, I'd just suggest using VIDEO_MAX_PLANES.
Done
> 
> > +	u32			num_planes;
> > +};
> 
> Or actually just dropping this struct at all. We should be able to just
> directly get these parameters from vb2 whenever needed.
> 
> > +
> > +void mtk_jpeg_enc_reset(void __iomem *base);
> > +u32 mtk_jpeg_enc_get_int_status(void __iomem *base);
> > +u32 mtk_jpeg_enc_get_file_size(void __iomem *base);
> > +u32 mtk_jpeg_enc_enum_result(void __iomem *base, u32 irq_status);
> > +void mtk_jpeg_enc_set_img_size(void __iomem *base, u32 width, u32 height);
> > +void mtk_jpeg_enc_set_enc_format(void __iomem *base, u32 enc_format);
> > +void mtk_jpeg_enc_set_blk_num(void __iomem *base, u32 enc_format, u32 width,
> > +			      u32 height);
> > +void mtk_jpeg_enc_set_stride(void __iomem *base, u32 enc_format, u32 width,
> > +			     u32 height, u32 bytesperline);
> > +void mtk_jpeg_enc_set_src_addr(void __iomem *base, u32 src_addr,
> > +			       u32 src_addr_c);
> > +void mtk_jpeg_enc_set_dst_addr(void __iomem *base, u32 dst_addr,
> > +			       u32 stall_size, u32 init_offset,
> > +			       u32 offset_mask);
> > +void mtk_jpeg_enc_set_ctrl_cfg(void __iomem *base, u32 exif_en, u32 quality,
> > +			       u32 restart_interval);
> > +void mtk_jpeg_enc_start(void __iomem *enc_reg_base);
> > +
> > +#endif /* _MTK_JPEG_ENC_HW_H */
> > diff --git a/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
> > new file mode 100644
> > index 000000000000..cec3631addc5
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-jpeg/mtk_jpeg_enc_reg.h
> > @@ -0,0 +1,78 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +/*
> > + * Copyright (c) 2019 MediaTek Inc.
> > + * Author: Xia Jiang <xia.jiang@mediatek.com>
> > + *
> > + */
> > +#ifndef _MTK_JPEG_ENC_REG_H
> > +#define _MTK_JPEG_ENC_REG_H
> > +
> > +#define JPEG_ENC_INT_STATUS_DONE			BIT(0)
> > +#define JPEG_ENC_INT_STATUS_STALL			BIT(1)
> > +#define JPEG_ENC_INT_STATUS_VCODEC_IRQ		BIT(4)
> > +#define JPEG_ENC_INT_STATUS_MASK_ALLIRQ		0x13
> > +
> > +#define JPEG_ENC_DST_ADDR_OFFSET_MASK GENMASK(3, 0)
> > +
> > +#define JPEG_ENC_CTRL_YUV_FORMAT_MASK 0x18
> > +#define JPEG_ENC_CTRL_RESTART_EN_BIT            BIT(10)
> > +#define JPEG_ENC_CTRL_FILE_FORMAT_BIT           BIT(5)
> > +#define JPEG_ENC_EN_JFIF_EXIF                   BIT(5)
> > +#define JPEG_ENC_CTRL_INT_EN_BIT                BIT(2)
> > +#define JPEG_ENC_CTRL_ENABLE_BIT                BIT(0)
> > +#define JPEG_ENC_RESET_BIT                      BIT(0)
> > +
> > +#define JPEG_ENC_YUV_FORMAT_YUYV 0
> > +#define JPEG_ENC_YUV_FORMAT_YVYU 1
> > +#define JPEG_ENC_YUV_FORMAT_NV12 2
> > +#define JEPG_ENC_YUV_FORMAT_NV21 3
> > +
> > +#define JPEG_ENC_QUALITY_Q60 0x0
> > +#define JPEG_ENC_QUALITY_Q80 0x1
> > +#define JPEG_ENC_QUALITY_Q90 0x2
> > +#define JPEG_ENC_QUALITY_Q95 0x3
> > +#define JPEG_ENC_QUALITY_Q39 0x4
> > +#define JPEG_ENC_QUALITY_Q68 0x5
> > +#define JPEG_ENC_QUALITY_Q84 0x6
> > +#define JPEG_ENC_QUALITY_Q92 0x7
> > +#define JPEG_ENC_QUALITY_Q48 0x8
> > +#define JPEG_ENC_QUALITY_Q74 0xa
> > +#define JPEG_ENC_QUALITY_Q87 0xb
> > +#define JPEG_ENC_QUALITY_Q34 0xc
> > +#define JPEG_ENC_QUALITY_Q64 0xe
> > +#define JPEG_ENC_QUALITY_Q82 0xf
> > +#define JPEG_ENC_QUALITY_Q97 0x10
> > +
> > +#define JPEG_ENC_RSTB				0x100
> > +#define JPEG_ENC_CTRL				0x104
> > +#define JPEG_ENC_QUALITY			0x108
> > +#define JPEG_ENC_BLK_NUM			0x10C
> > +#define JPEG_ENC_BLK_CNT			0x110
> > +#define JPEG_ENC_INT_STS			0x11c
> > +#define JPEG_ENC_DST_ADDR0			0x120
> > +#define JPEG_ENC_DMA_ADDR0			0x124
> > +#define JPEG_ENC_STALL_ADDR0		0x128
> > +#define JPEG_ENC_OFFSET_ADDR		0x138
> > +#define JPEG_ENC_RST_MCU_NUM		0x150
> > +#define JPEG_ENC_IMG_SIZE			0x154
> > +#define JPEG_ENC_DEBUG_INFO0		0x160
> > +#define JPEG_ENC_DEBUG_INFO1		0x164
> > +#define JPEG_ENC_TOTAL_CYCLE		0x168
> > +#define JPEG_ENC_BYTE_OFFSET_MASK	0x16c
> > +#define JPEG_ENC_SRC_LUMA_ADDR		0x170
> > +#define JPEG_ENC_SRC_CHROMA_ADDR	0x174
> > +#define JPEG_ENC_STRIDE				0x178
> > +#define JPEG_ENC_IMG_STRIDE			0x17c
> > +#define JPEG_ENC_DCM_CTRL			0x300
> > +#define JPEG_ENC_CODEC_SEL			0x314
> > +#define JPEG_ENC_ULTRA_THRES		0x318
> 
> nit: Please make sure all the macro values are aligned nicely, using tabs. 
done.
> 
> > +
> > +#define JPEG_ENC_FORMAT(x)		(((x) & 3) << 3)
> > +#define JPEG_ENC_WIDTH_HEIGHT(w, h)	(((w) << 16) | (h))
> > +#define JPEG_ENC_INIT_OFFSET(x)		((x) & (~0xf))
> > +#define JPEG_ENC_OFFSET_MASK(x)		((x) & 0xf)
> > +#define JPEG_ENC_DST_ADDR(x)		((x) & (~0xf))
> > +#define JPEG_ENC_STALL_ADDR(x, y)		(((x) + (y)) & (~0xf))
> 
> For the offset and address calculation, please just open code in the
> function that writes the registers, as they're not specific hardware
> register layouts, but just simple calculations.
done.
> 
> > +#define JPEG_ENC_SET_QUALITY(x, y)	(((x) & 0xffff0000) | (y))
> 
> That's an unusual pattern. Normally macros just generate the values for the
> target bitfield. It's responsibility of the caller to OR it with the
> rest of the register. That way it's clear what the code does from reading
> it.
done.
> 
> Best regards,
> Tomasz
> 


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-04-16  4:03     ` Xia Jiang
@ 2020-05-01 17:37       ` Tomasz Figa
  2020-05-09 10:28         ` Xia Jiang
  0 siblings, 1 reply; 18+ messages in thread
From: Tomasz Figa @ 2020-05-01 17:37 UTC (permalink / raw)
  To: Xia Jiang
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, linux-media, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, Marek Szyprowski,
	srv_heupstream

Hi Xia,

On Thu, Apr 16, 2020 at 12:03:15PM +0800, Xia Jiang wrote:
> On Fri, 2020-03-06 at 20:23 +0900, Tomasz Figa wrote:
> > Hi Xia,
> > 
> > On Tue, Mar 03, 2020 at 08:34:46PM +0800, Xia Jiang wrote:
> > > Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
> > > decode and encode have great similarities with function operation.
> > 
> > Thank you for the patch. Please see my comments inline.
> 
> Dear Tomasz,
> 
> Thank you for your reply. I have followed your advice and submited v8
> version patch.
> 
> Please check my reply below.
[snip]
> > 
> > >  
> > > -	switch (s->target) {
> > > -	case V4L2_SEL_TGT_COMPOSE:
> > > -		s->r.left = 0;
> > > -		s->r.top = 0;
> > > -		ctx->out_q.w = s->r.width;
> > > -		ctx->out_q.h = s->r.height;
> > > -		break;
> > > -	default:
> > > -		return -EINVAL;
> > > +		switch (s->target) {
> > > +		case V4L2_SEL_TGT_CROP:
> > > +			s->r.left = 0;
> > > +			s->r.top = 0;
> > > +			ctx->out_q.w = s->r.width;
> > > +			ctx->out_q.h = s->r.height;
> > 
> > What happens if the userspace provides a value bigger than current format?
> we need get the min value of userspace value and current value,changed
> it like this:
> ctx->out_q.w = min(s->r.width, ctx->out_q.w);
> ctx->out_q.h = min(s->r.height,ctx->out_q.h);

Since ctx->out_q is modified by this function, wouldn't that cause
problems if S_SELECTION was called two times, first with a smaller
rectangle and then with a bigger one? We should store the active crop
and format separately and use the latter for min().

[snip]
> > >  
> > >  	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
> > >  		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
> > > @@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
> > >  	return 0;
> > >  }
> > >  
> > > +static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> > > +				 struct vb2_buffer *dst_buf,
> > > +				 struct mtk_jpeg_enc_bs *bs)
> > > +{
> > > +	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > > +	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;
> > 
> > Could you explain what is the meaning of the dma_addr_offset and where the
> > default EXIF size comes from? Also, how is the encoder output affected by
> > the enable_exif flag?
> If enabled the exif mode, the real output will be filled at the locaiton
> of dst_addr+ dma_addr_offset(exif size).The dma_addr_offset will be
> filled by the application.
> The default exif size is setted as constant value 64k according to the
> spec.(Exif metadata are restricted in size to 64kB in JPEG images
> because according to the specification this information must be
> contained within a signed JPEG APP1 segment)

Okay, thanks. Then it sounds like MTK_JPEG_MAX_EXIF_SIZE could be a more
appropriate name.

[snip]
> > > +}
> > > +
> > >  static void mtk_jpeg_device_run(void *priv)
> > >  {
> > >  	struct mtk_jpeg_ctx *ctx = priv;
> > > @@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
> > >  	struct mtk_jpeg_src_buf *jpeg_src_buf;
> > >  	struct mtk_jpeg_bs bs;
> > >  	struct mtk_jpeg_fb fb;
> > > +	struct mtk_jpeg_enc_bs enc_bs;
> > > +	struct mtk_jpeg_enc_fb enc_fb;
> > >  	int i;
> > >  
> > >  	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> > > @@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
> > >  		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> > >  			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
> > >  		buf_state = VB2_BUF_STATE_DONE;
> > 
> > About existing code, but we may want to explain this.
> > What is this last frame handling above for?
> if the user gives us a empty buffer(means it is the last frame),the
> driver will not encode and done the buffer to the user.
>

An empty buffer is not a valid way of signaling a last frame in V4L2. In
general, I'm not sure there is such a thing in JPEG, because all frames
are separate from each other and we always expect 1 input buffer and 1
output buffer for one frame. We might want to remove the special
handling in a follow up patch.

> > > -		goto dec_end;
> > > +		goto device_run_end;
> > >  	}
> > >  
> > > -	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
> > > -		mtk_jpeg_queue_src_chg_event(ctx);
> > > -		ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > -		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > > -		return;
> > > -	}
> > > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > > +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> > > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> > 
> > Why do we need to reset every frame?
> We do this operation is to ensure that all registers are cleared.
> It's safer from the hardware point of view.

Wouldn't this only waste power? If we reset the hardware after powering
up, the only registers that could change would be changed by the driver
itself. The driver should program all registers properly when starting
next frame anyway, so such a reset shouldn't be necessary.

> > 
> > > +
> > > +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
> > > +				     &enc_bs);
> > > +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
> > > +				     &enc_fb);
> > > +		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
> > > +					  ctx->enc_quality,
> > > +					  ctx->restart_interval);
> > > +
> > > +		mtk_jpeg_enc_start(jpeg->reg_base);
> > > +	} else {
> > > +		if (mtk_jpeg_check_resolution_change
> > > +			(ctx, &jpeg_src_buf->dec_param)) {
> > > +			mtk_jpeg_queue_src_chg_event(ctx);
> > > +			ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > +			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > 
> > This is a bit strange. Resolution change should be signaled when the
> > hardware attempted to decode a frame and detected a different resolution
> > than current. It shouldn't be necessary for the userspace to queue a pair
> > of buffers to signal it, as with the current code.
> If the the resolution is bigger than current, the current buffer will
> not be enough for the changed resolution.Shouldn't it tell the userspace
> to queue new buffer and stream on again?

The V4L2 decode flow is as follows:
 - application configures and starts only the OUTPUT queue,
 - application queues an OUTPUT buffer with a frame worth of bitstream,
 - decoder parses the bitstream headers, detects CAPTURE format and
   signals the source change event,
 - application reads CAPTURE format and configures and starts the
   CAPTURE queue,
 - application queues a CAPTURE buffer,
 - decoder decodes the image to the queued buffer.

In case of subsequent (dynamic) resolution change:
 - application queues an OUTPUT buffer and a CAPTURE buffer,
 - decoder parses the bitstream, notices resolution change, updates
   CAPTURE format and signals the source change event, refusing to
   continue the decoding until the application acknowledges it,
 - application either reallocates its CAPTURE buffers or confirms that
   the existing buffers are fine and acknowledges resolution change,
 - decoding continues.

For more details, please check the interface specification:
https://www.kernel.org/doc/html/latest/media/uapi/v4l/dev-decoder.html

[snip]
> > > -	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
> > > +	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);
> > 
> > FYI the type changed to VFL_TYPE_VIDEO recently.
> I changed VFL_TYPE_GRABBER to VFL_TYPE_VIDEO,but builded fail.

What kernel version are you building with?

> > >  	if (ret) {
> > >  		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
> > > -		goto err_dec_vdev_register;
> > > +		goto err_vfd_jpeg_register;
> > >  	}
> > >  
> > > -	video_set_drvdata(jpeg->dec_vdev, jpeg);
> > > +	video_set_drvdata(jpeg->vfd_jpeg, jpeg);
> > >  	v4l2_info(&jpeg->v4l2_dev,
> > > -		  "decoder device registered as /dev/video%d (%d,%d)\n",
> > > -		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
> > > +		  "jpeg device %d registered as /dev/video%d (%d,%d)\n",
> > 
> > Here it would be actually useful to special case the encoder and decoder,
> > because it would be easier for the user to know which device is which.
> > 

Just making sure this wasn't overlooked.

[snip]
> > > +
> > > +void mtk_jpeg_enc_reset(void __iomem *base)
> > > +{
> > > +	writel(0x00, base + JPEG_ENC_RSTB);
> > > +	writel(JPEG_ENC_RESET_BIT, base + JPEG_ENC_RSTB);
> > > +	writel(0x00, base + JPEG_ENC_CODEC_SEL);
> > > +}
> > > +
> > > +u32 mtk_jpeg_enc_get_int_status(void __iomem *base)
> > > +{
> > > +	u32 ret;
> > > +
> > > +	ret = readl(base + JPEG_ENC_INT_STS) &
> > > +		    JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
> > > +	if (ret)
> > > +		writel(0, base + JPEG_ENC_INT_STS);
> > > +
> > > +	return ret;
> > > +}
> > 
> > Does it make sense to have a function for what is essentially just 2 lines?
> > Also, the name is misleading, as the function not only gets but also
> > clears.
> Make all hw register setting in mtk_jpeg_enc_hw.c is one part of current
> architecture.
> I have changed the function name to
> mtk_jpeg_enc_get_and_clear_int_status.

As I mentioned before, this driver needs a big clean up and that's why I
suggested starting over with a new one for the JPEG encoder part. Since
we decided to extend this one in the end, would you be able to improve
this aspect as well? Thanks.

Best regards,
Tomasz

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-05-01 17:37       ` Tomasz Figa
@ 2020-05-09 10:28         ` Xia Jiang
  2020-05-20 19:40           ` Tomasz Figa
  0 siblings, 1 reply; 18+ messages in thread
From: Xia Jiang @ 2020-05-09 10:28 UTC (permalink / raw)
  To: Tomasz Figa
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, linux-media, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, Marek Szyprowski,
	srv_heupstream

On Fri, 2020-05-01 at 17:37 +0000, Tomasz Figa wrote:
> Hi Xia,
> 
> On Thu, Apr 16, 2020 at 12:03:15PM +0800, Xia Jiang wrote:
> > On Fri, 2020-03-06 at 20:23 +0900, Tomasz Figa wrote:
> > > Hi Xia,
> > > 
> > > On Tue, Mar 03, 2020 at 08:34:46PM +0800, Xia Jiang wrote:
> > > > Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
> > > > decode and encode have great similarities with function operation.
> > > 
> > > Thank you for the patch. Please see my comments inline.
> > 
> > Dear Tomasz,
> > 
> > Thank you for your reply. I have followed your advice and submited v8
> > version patch.
> > 
> > Please check my reply below.
Dear Tomasz,
I have some confuse about your advice, please check my reply below.
> [snip]
> > > 
> > > >  
> > > > -	switch (s->target) {
> > > > -	case V4L2_SEL_TGT_COMPOSE:
> > > > -		s->r.left = 0;
> > > > -		s->r.top = 0;
> > > > -		ctx->out_q.w = s->r.width;
> > > > -		ctx->out_q.h = s->r.height;
> > > > -		break;
> > > > -	default:
> > > > -		return -EINVAL;
> > > > +		switch (s->target) {
> > > > +		case V4L2_SEL_TGT_CROP:
> > > > +			s->r.left = 0;
> > > > +			s->r.top = 0;
> > > > +			ctx->out_q.w = s->r.width;
> > > > +			ctx->out_q.h = s->r.height;
> > > 
> > > What happens if the userspace provides a value bigger than current format?
> > we need get the min value of userspace value and current value,changed
> > it like this:
> > ctx->out_q.w = min(s->r.width, ctx->out_q.w);
> > ctx->out_q.h = min(s->r.height,ctx->out_q.h);
> 
> Since ctx->out_q is modified by this function, wouldn't that cause
> problems if S_SELECTION was called two times, first with a smaller
> rectangle and then with a bigger one? We should store the active crop
> and format separately and use the latter for min().
Add a member variable(struct v4l2_rect) in out_q structure for storing
the active crop, like this:
s->r.width =  min(s->r.width, ctx->out_q.w);
s->r.height = min(s->r.height,ctx->out_q.h);
ctx->out_q.rect.width = s->r.width;
ctx->out_q.rect.height =  s->r.height;
Is that ok?
> 
> [snip]
> > > >  
> > > >  	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
> > > >  		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
> > > > @@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
> > > >  	return 0;
> > > >  }
> > > >  
> > > > +static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> > > > +				 struct vb2_buffer *dst_buf,
> > > > +				 struct mtk_jpeg_enc_bs *bs)
> > > > +{
> > > > +	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > > > +	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;
> > > 
> > > Could you explain what is the meaning of the dma_addr_offset and where the
> > > default EXIF size comes from? Also, how is the encoder output affected by
> > > the enable_exif flag?
> > If enabled the exif mode, the real output will be filled at the locaiton
> > of dst_addr+ dma_addr_offset(exif size).The dma_addr_offset will be
> > filled by the application.
> > The default exif size is setted as constant value 64k according to the
> > spec.(Exif metadata are restricted in size to 64kB in JPEG images
> > because according to the specification this information must be
> > contained within a signed JPEG APP1 segment)
> 
> Okay, thanks. Then it sounds like MTK_JPEG_MAX_EXIF_SIZE could be a more
> appropriate name.
> 
> [snip]
> > > > +}
> > > > +
> > > >  static void mtk_jpeg_device_run(void *priv)
> > > >  {
> > > >  	struct mtk_jpeg_ctx *ctx = priv;
> > > > @@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
> > > >  	struct mtk_jpeg_src_buf *jpeg_src_buf;
> > > >  	struct mtk_jpeg_bs bs;
> > > >  	struct mtk_jpeg_fb fb;
> > > > +	struct mtk_jpeg_enc_bs enc_bs;
> > > > +	struct mtk_jpeg_enc_fb enc_fb;
> > > >  	int i;
> > > >  
> > > >  	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> > > > @@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
> > > >  		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> > > >  			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
> > > >  		buf_state = VB2_BUF_STATE_DONE;
> > > 
> > > About existing code, but we may want to explain this.
> > > What is this last frame handling above for?
> > if the user gives us a empty buffer(means it is the last frame),the
> > driver will not encode and done the buffer to the user.
> >
> 
> An empty buffer is not a valid way of signaling a last frame in V4L2. In
> general, I'm not sure there is such a thing in JPEG, because all frames
> are separate from each other and we always expect 1 input buffer and 1
> output buffer for one frame. We might want to remove the special
> handling in a follow up patch.
How does application to end jpeg operation in motion jpeg if we remove
this? I tryed to end with the condition that the input number equals
output number in UT, and is ok.
> 
> > > > -		goto dec_end;
> > > > +		goto device_run_end;
> > > >  	}
> > > >  
> > > > -	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
> > > > -		mtk_jpeg_queue_src_chg_event(ctx);
> > > > -		ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > > -		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > > > -		return;
> > > > -	}
> > > > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > > > +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> > > > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> > > 
> > > Why do we need to reset every frame?
> > We do this operation is to ensure that all registers are cleared.
> > It's safer from the hardware point of view.
> 
> Wouldn't this only waste power? If we reset the hardware after powering
> up, the only registers that could change would be changed by the driver
> itself. The driver should program all registers properly when starting
> next frame anyway, so such a reset shouldn't be necessary.
I confirmed with hardware designer again that we need to reset every
frame. If we do not do like this, unexpected mistakes may occur.
> 
> > > 
> > > > +
> > > > +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
> > > > +				     &enc_bs);
> > > > +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
> > > > +				     &enc_fb);
> > > > +		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
> > > > +					  ctx->enc_quality,
> > > > +					  ctx->restart_interval);
> > > > +
> > > > +		mtk_jpeg_enc_start(jpeg->reg_base);
> > > > +	} else {
> > > > +		if (mtk_jpeg_check_resolution_change
> > > > +			(ctx, &jpeg_src_buf->dec_param)) {
> > > > +			mtk_jpeg_queue_src_chg_event(ctx);
> > > > +			ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > > +			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > > 
> > > This is a bit strange. Resolution change should be signaled when the
> > > hardware attempted to decode a frame and detected a different resolution
> > > than current. It shouldn't be necessary for the userspace to queue a pair
> > > of buffers to signal it, as with the current code.
> > If the the resolution is bigger than current, the current buffer will
> > not be enough for the changed resolution.Shouldn't it tell the userspace
> > to queue new buffer and stream on again?
> 
> The V4L2 decode flow is as follows:
>  - application configures and starts only the OUTPUT queue,
>  - application queues an OUTPUT buffer with a frame worth of bitstream,
>  - decoder parses the bitstream headers, detects CAPTURE format and
>    signals the source change event,
>  - application reads CAPTURE format and configures and starts the
>    CAPTURE queue,
>  - application queues a CAPTURE buffer,
>  - decoder decodes the image to the queued buffer.
> 
> In case of subsequent (dynamic) resolution change:
>  - application queues an OUTPUT buffer and a CAPTURE buffer,
>  - decoder parses the bitstream, notices resolution change, updates
>    CAPTURE format and signals the source change event, refusing to
>    continue the decoding until the application acknowledges it,
>  - application either reallocates its CAPTURE buffers or confirms that
>    the existing buffers are fine and acknowledges resolution change,
>  - decoding continues.
> 
> For more details, please check the interface specification:
> https://www.kernel.org/doc/html/latest/media/uapi/v4l/dev-decoder.html
> 
I tryed to move this operation from device_run() to
mtk_jpeg_dec_buf_queue(),but have a problem in motion jpeg.For example,I
queued three buffers continuously,the third buffer has resolution
change(bigger than the second buffer),but the capture buffer used in
device run didn't changed.
How do we handle this case?
> [snip]
> > > > -	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
> > > > +	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);
> > > 
> > > FYI the type changed to VFL_TYPE_VIDEO recently.
> > I changed VFL_TYPE_GRABBER to VFL_TYPE_VIDEO,but builded fail.
> 
> What kernel version are you building with?
I build it with the latest kernel 5.7,but builed fail again.
> 
> > > >  	if (ret) {
> > > >  		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
> > > > -		goto err_dec_vdev_register;
> > > > +		goto err_vfd_jpeg_register;
> > > >  	}
> > > >  
> > > > -	video_set_drvdata(jpeg->dec_vdev, jpeg);
> > > > +	video_set_drvdata(jpeg->vfd_jpeg, jpeg);
> > > >  	v4l2_info(&jpeg->v4l2_dev,
> > > > -		  "decoder device registered as /dev/video%d (%d,%d)\n",
> > > > -		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
> > > > +		  "jpeg device %d registered as /dev/video%d (%d,%d)\n",
> > > 
> > > Here it would be actually useful to special case the encoder and decoder,
> > > because it would be easier for the user to know which device is which.
> > > 
> 
> Just making sure this wasn't overlooked.
> 
> [snip]
> > > > +
> > > > +void mtk_jpeg_enc_reset(void __iomem *base)
> > > > +{
> > > > +	writel(0x00, base + JPEG_ENC_RSTB);
> > > > +	writel(JPEG_ENC_RESET_BIT, base + JPEG_ENC_RSTB);
> > > > +	writel(0x00, base + JPEG_ENC_CODEC_SEL);
> > > > +}
> > > > +
> > > > +u32 mtk_jpeg_enc_get_int_status(void __iomem *base)
> > > > +{
> > > > +	u32 ret;
> > > > +
> > > > +	ret = readl(base + JPEG_ENC_INT_STS) &
> > > > +		    JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
> > > > +	if (ret)
> > > > +		writel(0, base + JPEG_ENC_INT_STS);
> > > > +
> > > > +	return ret;
> > > > +}
> > > 
> > > Does it make sense to have a function for what is essentially just 2 lines?
> > > Also, the name is misleading, as the function not only gets but also
> > > clears.
> > Make all hw register setting in mtk_jpeg_enc_hw.c is one part of current
> > architecture.
> > I have changed the function name to
> > mtk_jpeg_enc_get_and_clear_int_status.
> 
> As I mentioned before, this driver needs a big clean up and that's why I
> suggested starting over with a new one for the JPEG encoder part. Since
> we decided to extend this one in the end, would you be able to improve
> this aspect as well? Thanks.
> 
> Best regards,
> Tomasz


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature
  2020-05-09 10:28         ` Xia Jiang
@ 2020-05-20 19:40           ` Tomasz Figa
  0 siblings, 0 replies; 18+ messages in thread
From: Tomasz Figa @ 2020-05-20 19:40 UTC (permalink / raw)
  To: Xia Jiang
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Rob Herring,
	Matthias Brugger, Rick Chang, linux-media, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, Marek Szyprowski,
	srv_heupstream

Hi Xia,

On Sat, May 09, 2020 at 06:28:15PM +0800, Xia Jiang wrote:
> On Fri, 2020-05-01 at 17:37 +0000, Tomasz Figa wrote:
> > Hi Xia,
> > 
> > On Thu, Apr 16, 2020 at 12:03:15PM +0800, Xia Jiang wrote:
> > > On Fri, 2020-03-06 at 20:23 +0900, Tomasz Figa wrote:
> > > > Hi Xia,
> > > > 
> > > > On Tue, Mar 03, 2020 at 08:34:46PM +0800, Xia Jiang wrote:
> > > > > Add mtk jpeg encode v4l2 driver based on jpeg decode, because that jpeg
> > > > > decode and encode have great similarities with function operation.
> > > > 
> > > > Thank you for the patch. Please see my comments inline.
> > > 
> > > Dear Tomasz,
> > > 
> > > Thank you for your reply. I have followed your advice and submited v8
> > > version patch.
> > > 
> > > Please check my reply below.
> Dear Tomasz,
> I have some confuse about your advice, please check my reply below.

Sorry for the late reply again. Please see my reply inline.

> > [snip]
> > > > 
> > > > >  
> > > > > -	switch (s->target) {
> > > > > -	case V4L2_SEL_TGT_COMPOSE:
> > > > > -		s->r.left = 0;
> > > > > -		s->r.top = 0;
> > > > > -		ctx->out_q.w = s->r.width;
> > > > > -		ctx->out_q.h = s->r.height;
> > > > > -		break;
> > > > > -	default:
> > > > > -		return -EINVAL;
> > > > > +		switch (s->target) {
> > > > > +		case V4L2_SEL_TGT_CROP:
> > > > > +			s->r.left = 0;
> > > > > +			s->r.top = 0;
> > > > > +			ctx->out_q.w = s->r.width;
> > > > > +			ctx->out_q.h = s->r.height;
> > > > 
> > > > What happens if the userspace provides a value bigger than current format?
> > > we need get the min value of userspace value and current value,changed
> > > it like this:
> > > ctx->out_q.w = min(s->r.width, ctx->out_q.w);
> > > ctx->out_q.h = min(s->r.height,ctx->out_q.h);
> > 
> > Since ctx->out_q is modified by this function, wouldn't that cause
> > problems if S_SELECTION was called two times, first with a smaller
> > rectangle and then with a bigger one? We should store the active crop
> > and format separately and use the latter for min().
> Add a member variable(struct v4l2_rect) in out_q structure for storing
> the active crop, like this:
> s->r.width =  min(s->r.width, ctx->out_q.w);
> s->r.height = min(s->r.height,ctx->out_q.h);
> ctx->out_q.rect.width = s->r.width;
> ctx->out_q.rect.height =  s->r.height;
> Is that ok?

Yes. I'd call it crop_rect and it can be simplified further into:

ct->out_q.crop_rect = s->r;

> > 
> > [snip]
> > > > >  
> > > > >  	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
> > > > >  		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
> > > > > @@ -772,6 +1011,45 @@ static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
> > > > >  	return 0;
> > > > >  }
> > > > >  
> > > > > +static void mtk_jpeg_set_enc_dst(struct mtk_jpeg_ctx *ctx, void __iomem *base,
> > > > > +				 struct vb2_buffer *dst_buf,
> > > > > +				 struct mtk_jpeg_enc_bs *bs)
> > > > > +{
> > > > > +	bs->dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > > > > +	bs->dma_addr_offset = ctx->enable_exif ? MTK_JPEG_DEFAULT_EXIF_SIZE : 0;
> > > > 
> > > > Could you explain what is the meaning of the dma_addr_offset and where the
> > > > default EXIF size comes from? Also, how is the encoder output affected by
> > > > the enable_exif flag?
> > > If enabled the exif mode, the real output will be filled at the locaiton
> > > of dst_addr+ dma_addr_offset(exif size).The dma_addr_offset will be
> > > filled by the application.
> > > The default exif size is setted as constant value 64k according to the
> > > spec.(Exif metadata are restricted in size to 64kB in JPEG images
> > > because according to the specification this information must be
> > > contained within a signed JPEG APP1 segment)
> > 
> > Okay, thanks. Then it sounds like MTK_JPEG_MAX_EXIF_SIZE could be a more
> > appropriate name.
> > 
> > [snip]
> > > > > +}
> > > > > +
> > > > >  static void mtk_jpeg_device_run(void *priv)
> > > > >  {
> > > > >  	struct mtk_jpeg_ctx *ctx = priv;
> > > > > @@ -782,6 +1060,8 @@ static void mtk_jpeg_device_run(void *priv)
> > > > >  	struct mtk_jpeg_src_buf *jpeg_src_buf;
> > > > >  	struct mtk_jpeg_bs bs;
> > > > >  	struct mtk_jpeg_fb fb;
> > > > > +	struct mtk_jpeg_enc_bs enc_bs;
> > > > > +	struct mtk_jpeg_enc_fb enc_fb;
> > > > >  	int i;
> > > > >  
> > > > >  	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> > > > > @@ -792,30 +1072,47 @@ static void mtk_jpeg_device_run(void *priv)
> > > > >  		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
> > > > >  			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
> > > > >  		buf_state = VB2_BUF_STATE_DONE;
> > > > 
> > > > About existing code, but we may want to explain this.
> > > > What is this last frame handling above for?
> > > if the user gives us a empty buffer(means it is the last frame),the
> > > driver will not encode and done the buffer to the user.
> > >
> > 
> > An empty buffer is not a valid way of signaling a last frame in V4L2. In
> > general, I'm not sure there is such a thing in JPEG, because all frames
> > are separate from each other and we always expect 1 input buffer and 1
> > output buffer for one frame. We might want to remove the special
> > handling in a follow up patch.
> How does application to end jpeg operation in motion jpeg if we remove
> this? I tryed to end with the condition that the input number equals
> output number in UT, and is ok.

That's correct. The operation ends when the number of CAPTURE buffers
dequeued is the same as the number of OUTPUT buffers queued.

> > 
> > > > > -		goto dec_end;
> > > > > +		goto device_run_end;
> > > > >  	}
> > > > >  
> > > > > -	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
> > > > > -		mtk_jpeg_queue_src_chg_event(ctx);
> > > > > -		ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > > > -		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > > > > -		return;
> > > > > -	}
> > > > > +	if (jpeg->mode == MTK_JPEG_ENC) {
> > > > > +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> > > > > +		mtk_jpeg_enc_reset(jpeg->reg_base);
> > > > 
> > > > Why do we need to reset every frame?
> > > We do this operation is to ensure that all registers are cleared.
> > > It's safer from the hardware point of view.
> > 
> > Wouldn't this only waste power? If we reset the hardware after powering
> > up, the only registers that could change would be changed by the driver
> > itself. The driver should program all registers properly when starting
> > next frame anyway, so such a reset shouldn't be necessary.
> I confirmed with hardware designer again that we need to reset every
> frame. If we do not do like this, unexpected mistakes may occur.

Okay, thanks for double checking. Please add a comment to the code that it
is a hardware requirement.

> > 
> > > > 
> > > > > +
> > > > > +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf,
> > > > > +				     &enc_bs);
> > > > > +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf,
> > > > > +				     &enc_fb);
> > > > > +		mtk_jpeg_enc_set_ctrl_cfg(jpeg->reg_base, ctx->enable_exif,
> > > > > +					  ctx->enc_quality,
> > > > > +					  ctx->restart_interval);
> > > > > +
> > > > > +		mtk_jpeg_enc_start(jpeg->reg_base);
> > > > > +	} else {
> > > > > +		if (mtk_jpeg_check_resolution_change
> > > > > +			(ctx, &jpeg_src_buf->dec_param)) {
> > > > > +			mtk_jpeg_queue_src_chg_event(ctx);
> > > > > +			ctx->state = MTK_JPEG_SOURCE_CHANGE;
> > > > > +			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> > > > 
> > > > This is a bit strange. Resolution change should be signaled when the
> > > > hardware attempted to decode a frame and detected a different resolution
> > > > than current. It shouldn't be necessary for the userspace to queue a pair
> > > > of buffers to signal it, as with the current code.
> > > If the the resolution is bigger than current, the current buffer will
> > > not be enough for the changed resolution.Shouldn't it tell the userspace
> > > to queue new buffer and stream on again?
> > 
> > The V4L2 decode flow is as follows:
> >  - application configures and starts only the OUTPUT queue,
> >  - application queues an OUTPUT buffer with a frame worth of bitstream,
> >  - decoder parses the bitstream headers, detects CAPTURE format and
> >    signals the source change event,
> >  - application reads CAPTURE format and configures and starts the
> >    CAPTURE queue,
> >  - application queues a CAPTURE buffer,
> >  - decoder decodes the image to the queued buffer.
> > 
> > In case of subsequent (dynamic) resolution change:
> >  - application queues an OUTPUT buffer and a CAPTURE buffer,
> >  - decoder parses the bitstream, notices resolution change, updates
> >    CAPTURE format and signals the source change event, refusing to
> >    continue the decoding until the application acknowledges it,
> >  - application either reallocates its CAPTURE buffers or confirms that
> >    the existing buffers are fine and acknowledges resolution change,
> >  - decoding continues.
> > 
> > For more details, please check the interface specification:
> > https://www.kernel.org/doc/html/latest/media/uapi/v4l/dev-decoder.html
> > 
> I tryed to move this operation from device_run() to
> mtk_jpeg_dec_buf_queue(),but have a problem in motion jpeg.For example,I
> queued three buffers continuously,the third buffer has resolution
> change(bigger than the second buffer),but the capture buffer used in
> device run didn't changed.
> How do we handle this case?

Sorry, I think I misread the driver code. It looks like there is a code
that parses the JPEG header from the source buffer called from
mtk_jpeg_dec_buf_queue() and that is the moment the driver detects the new
resolution. Then it only signals the event once all the previously queued
frames have been decoded, i.e. when the first new resolution frame gets to
device_run(). I think the current code should be fine then. Sorry for
confusion again!

> > [snip]
> > > > > -	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
> > > > > +	ret = video_register_device(jpeg->vfd_jpeg, VFL_TYPE_GRABBER, -1);
> > > > 
> > > > FYI the type changed to VFL_TYPE_VIDEO recently.
> > > I changed VFL_TYPE_GRABBER to VFL_TYPE_VIDEO,but builded fail.
> > 
> > What kernel version are you building with?
> I build it with the latest kernel 5.7,but builed fail again.

That's strange. There is no VFL_TYPE_GRABBER in 5.7 anymore:
https://elixir.bootlin.com/linux/v5.7-rc6/source/include/media/v4l2-dev.h#L24

Best regards,
Tomasz


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2020-05-20 19:40 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-03 12:34 [PATCH v7 00/11] Add support for mt2701 JPEG ENC support Xia Jiang
2020-03-03 12:34 ` [PATCH v7 01/11] media: platform: Improve subscribe event flow for bug fixing Xia Jiang
2020-03-03 12:34 ` [PATCH v7 02/11] media: platform: Improve queue set up " Xia Jiang
2020-03-03 12:34 ` [PATCH v7 03/11] media: platform: Improve s_selection " Xia Jiang
2020-03-11 11:02   ` Tomasz Figa
2020-03-03 12:34 ` [PATCH v7 04/11] media: platform: Use kernel native functions for improving code quality Xia Jiang
2020-03-03 12:34 ` [PATCH v7 05/11] media: platform: Change case " Xia Jiang
2020-03-03 12:34 ` [PATCH v7 06/11] media: platform: Change MTK_JPEG_COMP_MAX macro definition location Xia Jiang
2020-03-03 12:34 ` [PATCH v7 07/11] media: platform: Delete redundant code for improving code quality Xia Jiang
2020-03-03 12:34 ` [PATCH v7 08/11] media: dt-bindings: Add jpeg enc device tree node document Xia Jiang
2020-03-03 12:34 ` [PATCH v7 09/11] arm: dts: Add jpeg enc device tree node Xia Jiang
2020-03-03 12:34 ` [PATCH v7 10/11] media: platform: Rename jpeg dec file name Xia Jiang
2020-03-03 12:34 ` [PATCH v7 11/11] media: platform: Add jpeg dec/enc feature Xia Jiang
2020-03-06 11:23   ` Tomasz Figa
2020-04-16  4:03     ` Xia Jiang
2020-05-01 17:37       ` Tomasz Figa
2020-05-09 10:28         ` Xia Jiang
2020-05-20 19:40           ` Tomasz Figa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).