All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/7] Add MT8173 Video Decoder Driver
@ 2016-04-13 12:01 ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

==============
 Introduction
==============

The purpose of this series is to add the driver for video codec hw embedded in the Mediatek's MT8173 SoCs.
Mediatek Video Codec is able to handle video decoding of in a range of formats.

This patch series add Mediatek block format V4L2_PIX_FMT_MT21, the decoder driver will decoded bitstream to
V4L2_PIX_FMT_MT21 format.

This patch series rely on MTK VPU driver in patch series "Add MT8173 Video Encoder Driver and VPU Driver"[1]
and patch "CHROMIUM: v4l: Add V4L2_PIX_FMT_VP9 definition"[2] for VP9 support.
Mediatek Video Decoder driver rely on VPU driver to load, communicate with VPU.

Internally the driver uses videobuf2 framework and MTK IOMMU and MTK SMI both have been merged in v4.6-rc1.

[1]https://patchwork.linuxtv.org/patch/33734/
[2]https://chromium-review.googlesource.com/#/c/245241/

==================
 Device interface
==================

In principle the driver bases on v4l2 memory-to-memory framework:
it provides a single video node and each opened file handle gets its own private context with separate
buffer queues. Each context consist of 2 buffer queues: OUTPUT (for source buffers, i.e. bitstream)
and CAPTURE (for destination buffers, i.e. decoded video frames).
OUTPUT and CAPTURE buffer could be MMAP or DMABUF memory type.
VIDIOC_G_CTRL and VIDIOC_G_EXT_CTRLS return V4L2_CID_MIN_BUFFERS_FOR_CAPTURE only when dirver in MTK_STATE_HEADER
state, or it will return EAGAIN.
Driver do not support subscribe event for control 'User Controls' for now.
And it default support export DMABUF for other display drivers.

v4l2-compliance test output:
localhost Encode # ./v4l2-compliance -d /dev/video0
Driver Info:
        Driver name   : mtk-vcodec-dec
        Card type     : platform:mt8173
        Bus info      : platform:mt8173
        Driver version: 4.4.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

Compliance test for device /dev/video0 (not using libv4l2):

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second video open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: 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_QUERYCTRL/MENU: OK
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(357): g_ctrl returned an error (11)
                test VIDIOC_G/S_CTRL: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(579): g_ext_ctrls returned an error (11)
                test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(721): subscribe event for control 'User Controls' failed
                test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
                test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
                Standard Controls: 2 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)
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-formats.cpp(405): expected EINVAL, but got 11 when getting format for buftype 9
                test VIDIOC_G_FMT: FAIL
                test VIDIOC_TRY_FMT: OK (Not Supported)
                test VIDIOC_S_FMT: OK (Not Supported)
                test VIDIOC_G_SLICED_VBI_CAP: 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
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-buffers.cpp(500): q.has_expbuf(node)
                test VIDIOC_EXPBUF: FAIL


Total: 38, Succeeded: 33, Failed: 5, Warnings: 0


Daniel Kurtz (1):
  [media]: v4l: add Mediatek MT21 video block format

Tiffany Lin (6):
  dt-bindings: Add a binding for Mediatek Video Decoder
  [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
  arm64: dts: mediatek: Add Video Encoder for MT8173

 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 +-
 arch/arm64/boot/dts/mediatek/mt8173.dtsi           |   38 +
 drivers/media/platform/mtk-vcodec/Makefile         |   15 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 +++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 +++++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 +++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  123 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |  100 ++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +
 include/uapi/drm/drm_fourcc.h                      |    1 +
 include/uapi/linux/videodev2.h                     |    2 +
 20 files changed, 5160 insertions(+), 25 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

-- 
1.7.9.5

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

* [PATCH 0/7] Add MT8173 Video Decoder Driver
@ 2016-04-13 12:01 ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w,
	Tiffany.lin-NuS5LvNUpcJWk0Htik3J/w, Tiffany Lin

==============
 Introduction
==============

The purpose of this series is to add the driver for video codec hw embedded in the Mediatek's MT8173 SoCs.
Mediatek Video Codec is able to handle video decoding of in a range of formats.

This patch series add Mediatek block format V4L2_PIX_FMT_MT21, the decoder driver will decoded bitstream to
V4L2_PIX_FMT_MT21 format.

This patch series rely on MTK VPU driver in patch series "Add MT8173 Video Encoder Driver and VPU Driver"[1]
and patch "CHROMIUM: v4l: Add V4L2_PIX_FMT_VP9 definition"[2] for VP9 support.
Mediatek Video Decoder driver rely on VPU driver to load, communicate with VPU.

Internally the driver uses videobuf2 framework and MTK IOMMU and MTK SMI both have been merged in v4.6-rc1.

[1]https://patchwork.linuxtv.org/patch/33734/
[2]https://chromium-review.googlesource.com/#/c/245241/

==================
 Device interface
==================

In principle the driver bases on v4l2 memory-to-memory framework:
it provides a single video node and each opened file handle gets its own private context with separate
buffer queues. Each context consist of 2 buffer queues: OUTPUT (for source buffers, i.e. bitstream)
and CAPTURE (for destination buffers, i.e. decoded video frames).
OUTPUT and CAPTURE buffer could be MMAP or DMABUF memory type.
VIDIOC_G_CTRL and VIDIOC_G_EXT_CTRLS return V4L2_CID_MIN_BUFFERS_FOR_CAPTURE only when dirver in MTK_STATE_HEADER
state, or it will return EAGAIN.
Driver do not support subscribe event for control 'User Controls' for now.
And it default support export DMABUF for other display drivers.

v4l2-compliance test output:
localhost Encode # ./v4l2-compliance -d /dev/video0
Driver Info:
        Driver name   : mtk-vcodec-dec
        Card type     : platform:mt8173
        Bus info      : platform:mt8173
        Driver version: 4.4.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

Compliance test for device /dev/video0 (not using libv4l2):

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second video open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: 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_QUERYCTRL/MENU: OK
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(357): g_ctrl returned an error (11)
                test VIDIOC_G/S_CTRL: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(579): g_ext_ctrls returned an error (11)
                test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(721): subscribe event for control 'User Controls' failed
                test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
                test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
                Standard Controls: 2 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)
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-formats.cpp(405): expected EINVAL, but got 11 when getting format for buftype 9
                test VIDIOC_G_FMT: FAIL
                test VIDIOC_TRY_FMT: OK (Not Supported)
                test VIDIOC_S_FMT: OK (Not Supported)
                test VIDIOC_G_SLICED_VBI_CAP: 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
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-buffers.cpp(500): q.has_expbuf(node)
                test VIDIOC_EXPBUF: FAIL


Total: 38, Succeeded: 33, Failed: 5, Warnings: 0


Daniel Kurtz (1):
  [media]: v4l: add Mediatek MT21 video block format

Tiffany Lin (6):
  dt-bindings: Add a binding for Mediatek Video Decoder
  [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
  arm64: dts: mediatek: Add Video Encoder for MT8173

 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 +-
 arch/arm64/boot/dts/mediatek/mt8173.dtsi           |   38 +
 drivers/media/platform/mtk-vcodec/Makefile         |   15 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 +++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 +++++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 +++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  123 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |  100 ++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +
 include/uapi/drm/drm_fourcc.h                      |    1 +
 include/uapi/linux/videodev2.h                     |    2 +
 20 files changed, 5160 insertions(+), 25 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 0/7] Add MT8173 Video Decoder Driver
@ 2016-04-13 12:01 ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

==============
 Introduction
==============

The purpose of this series is to add the driver for video codec hw embedded in the Mediatek's MT8173 SoCs.
Mediatek Video Codec is able to handle video decoding of in a range of formats.

This patch series add Mediatek block format V4L2_PIX_FMT_MT21, the decoder driver will decoded bitstream to
V4L2_PIX_FMT_MT21 format.

This patch series rely on MTK VPU driver in patch series "Add MT8173 Video Encoder Driver and VPU Driver"[1]
and patch "CHROMIUM: v4l: Add V4L2_PIX_FMT_VP9 definition"[2] for VP9 support.
Mediatek Video Decoder driver rely on VPU driver to load, communicate with VPU.

Internally the driver uses videobuf2 framework and MTK IOMMU and MTK SMI both have been merged in v4.6-rc1.

[1]https://patchwork.linuxtv.org/patch/33734/
[2]https://chromium-review.googlesource.com/#/c/245241/

==================
 Device interface
==================

In principle the driver bases on v4l2 memory-to-memory framework:
it provides a single video node and each opened file handle gets its own private context with separate
buffer queues. Each context consist of 2 buffer queues: OUTPUT (for source buffers, i.e. bitstream)
and CAPTURE (for destination buffers, i.e. decoded video frames).
OUTPUT and CAPTURE buffer could be MMAP or DMABUF memory type.
VIDIOC_G_CTRL and VIDIOC_G_EXT_CTRLS return V4L2_CID_MIN_BUFFERS_FOR_CAPTURE only when dirver in MTK_STATE_HEADER
state, or it will return EAGAIN.
Driver do not support subscribe event for control 'User Controls' for now.
And it default support export DMABUF for other display drivers.

v4l2-compliance test output:
localhost Encode # ./v4l2-compliance -d /dev/video0
Driver Info:
        Driver name   : mtk-vcodec-dec
        Card type     : platform:mt8173
        Bus info      : platform:mt8173
        Driver version: 4.4.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

Compliance test for device /dev/video0 (not using libv4l2):

Required ioctls:
        test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
        test second video open: OK
        test VIDIOC_QUERYCAP: OK
        test VIDIOC_G/S_PRIORITY: 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_QUERYCTRL/MENU: OK
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(357): g_ctrl returned an error (11)
                test VIDIOC_G/S_CTRL: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(579): g_ext_ctrls returned an error (11)
                test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-controls.cpp(721): subscribe event for control 'User Controls' failed
                test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
                test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
                Standard Controls: 2 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)
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-formats.cpp(405): expected EINVAL, but got 11 when getting format for buftype 9
                test VIDIOC_G_FMT: FAIL
                test VIDIOC_TRY_FMT: OK (Not Supported)
                test VIDIOC_S_FMT: OK (Not Supported)
                test VIDIOC_G_SLICED_VBI_CAP: 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
                fail: ../../../v4l-utils-1.6.0/utils/v4l2-compliance/v4l2-test-buffers.cpp(500): q.has_expbuf(node)
                test VIDIOC_EXPBUF: FAIL


Total: 38, Succeeded: 33, Failed: 5, Warnings: 0


Daniel Kurtz (1):
  [media]: v4l: add Mediatek MT21 video block format

Tiffany Lin (6):
  dt-bindings: Add a binding for Mediatek Video Decoder
  [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
  [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
  arm64: dts: mediatek: Add Video Encoder for MT8173

 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 +-
 arch/arm64/boot/dts/mediatek/mt8173.dtsi           |   38 +
 drivers/media/platform/mtk-vcodec/Makefile         |   15 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 +++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 +++++++++
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 +++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  123 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |  100 ++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +
 include/uapi/drm/drm_fourcc.h                      |    1 +
 include/uapi/linux/videodev2.h                     |    2 +
 20 files changed, 5160 insertions(+), 25 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

-- 
1.7.9.5

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

* [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-13 12:01   ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

From: Daniel Kurtz <djkurtz@chromium.org>

Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.

Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
V4L2_PIX_FMT_NV12 notation.

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 include/uapi/drm/drm_fourcc.h  |    1 +
 include/uapi/linux/videodev2.h |    2 ++
 2 files changed, 3 insertions(+)

diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
index 0b69a77..a193905 100644
--- a/include/uapi/drm/drm_fourcc.h
+++ b/include/uapi/drm/drm_fourcc.h
@@ -116,6 +116,7 @@
 #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
 #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
 
+#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
 /*
  * 3 plane YCbCr
  * index 0: Y plane, [7:0] Y
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index d0acd26..e9e3276 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -527,6 +527,8 @@ struct v4l2_pix_format {
 #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
 #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
 
+#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
+
 /* two planes -- one Y, one Cr + Cb interleaved  */
 #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
 #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */
-- 
1.7.9.5

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

* [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-13 12:01   ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w,
	Tiffany.lin-NuS5LvNUpcJWk0Htik3J/w, Tiffany Lin

From: Daniel Kurtz <djkurtz-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>

Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.

Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
V4L2_PIX_FMT_NV12 notation.

Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 include/uapi/drm/drm_fourcc.h  |    1 +
 include/uapi/linux/videodev2.h |    2 ++
 2 files changed, 3 insertions(+)

diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
index 0b69a77..a193905 100644
--- a/include/uapi/drm/drm_fourcc.h
+++ b/include/uapi/drm/drm_fourcc.h
@@ -116,6 +116,7 @@
 #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
 #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
 
+#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
 /*
  * 3 plane YCbCr
  * index 0: Y plane, [7:0] Y
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index d0acd26..e9e3276 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -527,6 +527,8 @@ struct v4l2_pix_format {
 #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
 #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
 
+#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
+
 /* two planes -- one Y, one Cr + Cb interleaved  */
 #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
 #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-13 12:01   ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

From: Daniel Kurtz <djkurtz@chromium.org>

Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.

Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
V4L2_PIX_FMT_NV12 notation.

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 include/uapi/drm/drm_fourcc.h  |    1 +
 include/uapi/linux/videodev2.h |    2 ++
 2 files changed, 3 insertions(+)

diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
index 0b69a77..a193905 100644
--- a/include/uapi/drm/drm_fourcc.h
+++ b/include/uapi/drm/drm_fourcc.h
@@ -116,6 +116,7 @@
 #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
 #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
 
+#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
 /*
  * 3 plane YCbCr
  * index 0: Y plane, [7:0] Y
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index d0acd26..e9e3276 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -527,6 +527,8 @@ struct v4l2_pix_format {
 #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
 #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
 
+#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
+
 /* two planes -- one Y, one Cr + Cb interleaved  */
 #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
 #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */
-- 
1.7.9.5

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

* [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
  2016-04-13 12:01   ` Tiffany Lin
  (?)
@ 2016-04-13 12:01     ` Tiffany Lin
  -1 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add a DT binding documentation of Video Decoder for the
MT8173 SoC from Mediatek.

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
index 59a47a5..9aa8848 100644
--- a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
+++ b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
@@ -1,25 +1,67 @@
 Mediatek Video Codec
 
 Mediatek Video Codec is the video codec hw present in Mediatek SoCs which
-supports high resolution encoding functionalities.
+supports high resolution encoding and decoding functionalities.
 
 Required properties:
 - compatible : "mediatek,mt8173-vcodec-enc" for encoder
+  "mediatek,mt8173-vcodec-dec" for decoder.
 - reg : Physical base address of the video codec registers and length of
   memory mapped region.
 - interrupts : interrupt number to the cpu.
 - mediatek,larb : must contain the local arbiters in the current Socs.
 - clocks : list of clock specifiers, corresponding to entries in
   the clock-names property.
-- clock-names: encoder must contain "venc_sel_src", "venc_sel",
-- "venc_lt_sel_src", "venc_lt_sel".
+- clock-names: encoder must contain "venc_sel_src", "venc_sel",,
+  "venc_lt_sel_src", "venc_lt_sel", decoder must contain "vcodecpll",
+  "univpll_d2", "clk_cci400_sel", "vdec_sel", "vdecpll".
 - iommus : should point to the respective IOMMU block with master port as
   argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.txt
   for details.
 - mediatek,vpu : the node of video processor unit
 
+
 Example:
-vcodec_enc: vcodec@0x18002000 {
+
+vcodec_dec: vcodec@16000000 {
+    compatible = "mediatek,mt8173-vcodec-dec";
+    reg = <0 0x16000000 0 0x100>,   /*VDEC_SYS*/
+          <0 0x16020000 0 0x1000>,  /*VDEC_MISC*/
+          <0 0x16021000 0 0x800>,   /*VDEC_LD*/
+          <0 0x16021800 0 0x800>,   /*VDEC_TOP*/
+          <0 0x16022000 0 0x1000>,  /*VDEC_CM*/
+          <0 0x16023000 0 0x1000>,  /*VDEC_AD*/
+          <0 0x16024000 0 0x1000>,  /*VDEC_AV*/
+          <0 0x16025000 0 0x1000>,  /*VDEC_PP*/
+          <0 0x16026800 0 0x800>,   /*VP8_VD*/
+          <0 0x16027000 0 0x800>,   /*VP6_VD*/
+          <0 0x16027800 0 0x800>,   /*VP8_VL*/
+          <0 0x16028400 0 0x400>;   /*VP9_VD*/
+    interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+    mediatek,larb = <&larb1>;
+    iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+    mediatek,vpu = <&vpu>;
+    power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+    clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+             <&topckgen CLK_TOP_UNIVPLL_D2>,
+             <&topckgen CLK_TOP_CCI400_SEL>,  
+             <&topckgen CLK_TOP_VDEC_SEL>,
+             <&topckgen CLK_TOP_VCODECPLL>;
+    clock-names = "vcodecpll",
+                  "univpll_d2",
+                  "clk_cci400_sel",   
+                  "vdec_sel",  
+                  "vdecpll";
+  };
+
+  vcodec_enc: vcodec@0x18002000 {
     compatible = "mediatek,mt8173-vcodec-enc";
     reg = <0 0x18002000 0 0x1000>,    /*VENC_SYS*/
           <0 0x19002000 0 0x1000>;    /*VENC_LT_SYS*/
-- 
1.7.9.5

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

* [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
@ 2016-04-13 12:01     ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add a DT binding documentation of Video Decoder for the
MT8173 SoC from Mediatek.

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
index 59a47a5..9aa8848 100644
--- a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
+++ b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
@@ -1,25 +1,67 @@
 Mediatek Video Codec
 
 Mediatek Video Codec is the video codec hw present in Mediatek SoCs which
-supports high resolution encoding functionalities.
+supports high resolution encoding and decoding functionalities.
 
 Required properties:
 - compatible : "mediatek,mt8173-vcodec-enc" for encoder
+  "mediatek,mt8173-vcodec-dec" for decoder.
 - reg : Physical base address of the video codec registers and length of
   memory mapped region.
 - interrupts : interrupt number to the cpu.
 - mediatek,larb : must contain the local arbiters in the current Socs.
 - clocks : list of clock specifiers, corresponding to entries in
   the clock-names property.
-- clock-names: encoder must contain "venc_sel_src", "venc_sel",
-- "venc_lt_sel_src", "venc_lt_sel".
+- clock-names: encoder must contain "venc_sel_src", "venc_sel",,
+  "venc_lt_sel_src", "venc_lt_sel", decoder must contain "vcodecpll",
+  "univpll_d2", "clk_cci400_sel", "vdec_sel", "vdecpll".
 - iommus : should point to the respective IOMMU block with master port as
   argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.txt
   for details.
 - mediatek,vpu : the node of video processor unit
 
+
 Example:
-vcodec_enc: vcodec@0x18002000 {
+
+vcodec_dec: vcodec@16000000 {
+    compatible = "mediatek,mt8173-vcodec-dec";
+    reg = <0 0x16000000 0 0x100>,   /*VDEC_SYS*/
+          <0 0x16020000 0 0x1000>,  /*VDEC_MISC*/
+          <0 0x16021000 0 0x800>,   /*VDEC_LD*/
+          <0 0x16021800 0 0x800>,   /*VDEC_TOP*/
+          <0 0x16022000 0 0x1000>,  /*VDEC_CM*/
+          <0 0x16023000 0 0x1000>,  /*VDEC_AD*/
+          <0 0x16024000 0 0x1000>,  /*VDEC_AV*/
+          <0 0x16025000 0 0x1000>,  /*VDEC_PP*/
+          <0 0x16026800 0 0x800>,   /*VP8_VD*/
+          <0 0x16027000 0 0x800>,   /*VP6_VD*/
+          <0 0x16027800 0 0x800>,   /*VP8_VL*/
+          <0 0x16028400 0 0x400>;   /*VP9_VD*/
+    interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+    mediatek,larb = <&larb1>;
+    iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+    mediatek,vpu = <&vpu>;
+    power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+    clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+             <&topckgen CLK_TOP_UNIVPLL_D2>,
+             <&topckgen CLK_TOP_CCI400_SEL>,  
+             <&topckgen CLK_TOP_VDEC_SEL>,
+             <&topckgen CLK_TOP_VCODECPLL>;
+    clock-names = "vcodecpll",
+                  "univpll_d2",
+                  "clk_cci400_sel",   
+                  "vdec_sel",  
+                  "vdecpll";
+  };
+
+  vcodec_enc: vcodec@0x18002000 {
     compatible = "mediatek,mt8173-vcodec-enc";
     reg = <0 0x18002000 0 0x1000>,    /*VENC_SYS*/
           <0 0x19002000 0 0x1000>;    /*VENC_LT_SYS*/
-- 
1.7.9.5

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

* [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
@ 2016-04-13 12:01     ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add a DT binding documentation of Video Decoder for the
MT8173 SoC from Mediatek.

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
index 59a47a5..9aa8848 100644
--- a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
+++ b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
@@ -1,25 +1,67 @@
 Mediatek Video Codec
 
 Mediatek Video Codec is the video codec hw present in Mediatek SoCs which
-supports high resolution encoding functionalities.
+supports high resolution encoding and decoding functionalities.
 
 Required properties:
 - compatible : "mediatek,mt8173-vcodec-enc" for encoder
+  "mediatek,mt8173-vcodec-dec" for decoder.
 - reg : Physical base address of the video codec registers and length of
   memory mapped region.
 - interrupts : interrupt number to the cpu.
 - mediatek,larb : must contain the local arbiters in the current Socs.
 - clocks : list of clock specifiers, corresponding to entries in
   the clock-names property.
-- clock-names: encoder must contain "venc_sel_src", "venc_sel",
-- "venc_lt_sel_src", "venc_lt_sel".
+- clock-names: encoder must contain "venc_sel_src", "venc_sel",,
+  "venc_lt_sel_src", "venc_lt_sel", decoder must contain "vcodecpll",
+  "univpll_d2", "clk_cci400_sel", "vdec_sel", "vdecpll".
 - iommus : should point to the respective IOMMU block with master port as
   argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.txt
   for details.
 - mediatek,vpu : the node of video processor unit
 
+
 Example:
-vcodec_enc: vcodec at 0x18002000 {
+
+vcodec_dec: vcodec at 16000000 {
+    compatible = "mediatek,mt8173-vcodec-dec";
+    reg = <0 0x16000000 0 0x100>,   /*VDEC_SYS*/
+          <0 0x16020000 0 0x1000>,  /*VDEC_MISC*/
+          <0 0x16021000 0 0x800>,   /*VDEC_LD*/
+          <0 0x16021800 0 0x800>,   /*VDEC_TOP*/
+          <0 0x16022000 0 0x1000>,  /*VDEC_CM*/
+          <0 0x16023000 0 0x1000>,  /*VDEC_AD*/
+          <0 0x16024000 0 0x1000>,  /*VDEC_AV*/
+          <0 0x16025000 0 0x1000>,  /*VDEC_PP*/
+          <0 0x16026800 0 0x800>,   /*VP8_VD*/
+          <0 0x16027000 0 0x800>,   /*VP6_VD*/
+          <0 0x16027800 0 0x800>,   /*VP8_VL*/
+          <0 0x16028400 0 0x400>;   /*VP9_VD*/
+    interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+    mediatek,larb = <&larb1>;
+    iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+             <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+    mediatek,vpu = <&vpu>;
+    power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+    clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+             <&topckgen CLK_TOP_UNIVPLL_D2>,
+             <&topckgen CLK_TOP_CCI400_SEL>,  
+             <&topckgen CLK_TOP_VDEC_SEL>,
+             <&topckgen CLK_TOP_VCODECPLL>;
+    clock-names = "vcodecpll",
+                  "univpll_d2",
+                  "clk_cci400_sel",   
+                  "vdec_sel",  
+                  "vdecpll";
+  };
+
+  vcodec_enc: vcodec at 0x18002000 {
     compatible = "mediatek,mt8173-vcodec-enc";
     reg = <0 0x18002000 0 0x1000>,    /*VENC_SYS*/
           <0 0x19002000 0 0x1000>;    /*VENC_LT_SYS*/
-- 
1.7.9.5

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  2016-04-13 12:01     ` Tiffany Lin
  (?)
@ 2016-04-13 12:01       ` Tiffany Lin
  -1 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add v4l2 layer decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
 11 files changed, 2596 insertions(+), 20 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index dc5cb00..4c8ed2f 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,7 +1,13 @@
 
 
-obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
-
+obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
+				       mtk-vcodec-enc.o \
+				       mtk-vcodec-common.o
+
+mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+		vdec_drv_if.o \
+		mtk_vcodec_dec.o \
+		mtk_vcodec_dec_pm.o \
 
 
 mtk-vcodec-enc-y := venc/venc_vp8_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
new file mode 100644
index 0000000..0499413
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
@@ -0,0 +1,1429 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vcodec_dec_pm.h"
+
+static struct mtk_video_fmt mtk_video_formats[] = {
+	{
+		.fourcc = V4L2_PIX_FMT_H264,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP8,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_MT21,
+		.type = MTK_FMT_FRAME,
+		.num_planes = 2,
+	},
+};
+#define OUT_FMT_IDX	0
+#define CAP_FMT_IDX	3
+
+#define VCODEC_CAPABILITY_4K_DISABLED	0x10
+#define VCODEC_DEC_4K_CODED_WIDTH	4096U
+#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
+
+#define MTK_VDEC_MIN_W	64U
+#define MTK_VDEC_MIN_H	64U
+#define MTK_VDEC_MAX_W	2048U
+#define MTK_VDEC_MAX_H	1088U
+#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
+#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
+
+static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
+	{
+		.fourcc	= V4L2_PIX_FMT_H264,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc	= V4L2_PIX_FMT_VP8,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+};
+
+#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
+#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
+
+
+const struct vb2_ops mtk_vdec_vb2_ops;
+
+static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+	unsigned int k;
+
+	for (k = 0; k < NUM_FORMATS; k++) {
+		fmt = &mtk_video_formats[k];
+		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
+			return fmt;
+	}
+
+	return NULL;
+}
+
+static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
+					      enum v4l2_buf_type type)
+{
+	if (V4L2_TYPE_IS_OUTPUT(type))
+		return &ctx->q_data[MTK_Q_DATA_SRC];
+
+	return &ctx->q_data[MTK_Q_DATA_DST];
+}
+
+static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vdec_fb *disp_frame_buffer;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_DISP_FRAME_BUFFER,
+				   &disp_frame_buffer)) {
+		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
+			     ctx->idx);
+		return NULL;
+	}
+
+	if (disp_frame_buffer == NULL) {
+		mtk_v4l2_debug(3, "No display frame buffer");
+		return NULL;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = disp_frame_buffer;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
+							ctx->picinfo.y_bs_sz);
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
+							ctx->picinfo.c_bs_sz);
+
+		dstbuf->ready_to_display = true;
+		dstbuf->nonrealdisplay = false;
+		mtk_v4l2_debug(2,
+			       "[%d]status=%x queue idx=%d to done_list %d",
+			       ctx->idx, frame_buffer->status,
+			       dstbuf->vb.vb2_buf.index,
+			       dstbuf->queued_in_vb2);
+
+		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
+		ctx->decoded_frame_cnt++;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	void *tmp_frame_addr;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_FREE_FRAME_BUFFER,
+				   &tmp_frame_addr)) {
+		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
+		return NULL;
+	}
+	if (tmp_frame_addr == NULL) {
+		mtk_v4l2_debug(3, " No free frame buffer");
+		return NULL;
+	}
+
+	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
+			 ctx->idx, tmp_frame_addr);
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = tmp_frame_addr;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		if ((dstbuf->queued_in_vb2) &&
+		    (dstbuf->queued_in_v4l2) &&
+		    (frame_buffer->status == FB_ST_FREE)) {
+			mtk_v4l2_debug(2,
+				"[%d]status=%x queue idx=%d to rdy_queue %d",
+				 ctx->idx, frame_buffer->status,
+				 dstbuf->vb.vb2_buf.index,
+				 dstbuf->queued_in_vb2);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+		} else if ((dstbuf->queued_in_vb2 == false) &&
+			   (dstbuf->queued_in_v4l2 == true)) {
+			mtk_v4l2_debug(2,
+					 "[%d]status=%x queue idx=%d to rdy_queue",
+					 ctx->idx, frame_buffer->status,
+					 dstbuf->vb.vb2_buf.index);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+			dstbuf->queued_in_vb2 = true;
+		} else {
+			mtk_v4l2_debug(2,
+					"[%d]status=%x err queue idx=%d %d %d",
+					ctx->idx, frame_buffer->status,
+					dstbuf->vb.vb2_buf.index,
+					dstbuf->queued_in_vb2,
+					dstbuf->queued_in_v4l2);
+		}
+		dstbuf->used = false;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_display_buffer(ctx);
+	} while (framptr);
+}
+
+static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_free_buffer(ctx);
+	} while (framptr);
+}
+
+static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
+{
+	static const struct v4l2_event ev_src_ch = {
+		.type = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes =
+		V4L2_EVENT_SRC_CH_RESOLUTION,
+	};
+
+	mtk_v4l2_debug(1, "[%d]", ctx->idx);
+	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
+
+	return 0;
+}
+
+static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
+{
+	bool res_chg;
+	int ret = 0;
+
+	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
+	if (ret)
+		mtk_v4l2_err("DecodeFinal failed");
+
+	clean_display_buffer(ctx);
+	clean_free_buffer(ctx);
+}
+
+static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
+{
+	int dpbsize;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_PIC_INFO,
+				   &ctx->last_decoded_picinfo)) {
+		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+				ctx->idx);
+		return false;
+	}
+
+	if (ctx->last_decoded_picinfo.pic_w == 0 ||
+		ctx->last_decoded_picinfo.pic_h == 0 ||
+		ctx->last_decoded_picinfo.buf_w == 0 ||
+		ctx->last_decoded_picinfo.buf_h == 0)
+		return false;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
+
+		mtk_v4l2_debug(1,
+				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
+				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
+				 ctx->last_decoded_picinfo.pic_h,
+				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				 ctx->last_decoded_picinfo.buf_w,
+				 ctx->last_decoded_picinfo.buf_h);
+
+		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+		if (dpbsize == 0)
+			dpbsize = 1;
+		ctx->dpb_count = dpbsize;
+		return true;
+	}
+
+	return false;
+}
+
+void mtk_vdec_worker(struct work_struct *work)
+{
+	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
+				    decode_work);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	struct vb2_buffer *src_buf, *dst_buf;
+	struct mtk_vcodec_mem buf;
+	struct vdec_fb *pfb;
+	bool res_chg = false;
+	int ret;
+	struct mtk_video_buf *dst_buf_info, *src_buf_info;
+	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
+
+	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+	if (src_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
+		return;
+	}
+
+	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
+	if (dst_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
+		return;
+	}
+
+	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
+	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
+
+	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
+	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
+
+	buf.va = vb2_plane_vaddr(src_buf, 0);
+	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
+	buf.size = (unsigned int)src_buf->planes[0].bytesused;
+	if (!buf.va) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
+			       ctx->idx, src_buf->index);
+		return;
+	}
+
+	pfb = &dst_buf_info->frame_buffer;
+	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
+	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
+	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
+
+	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
+	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
+	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
+	pfb->status = 0;
+	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
+	mtk_v4l2_debug(3,
+			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
+			 ctx->idx, buf.va,
+			 (u64)buf.dma_addr,
+			 buf.size, src_buf);
+
+	mtk_v4l2_debug(3,
+			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
+			dst_buf->index, pfb,
+			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
+			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
+
+	if (src_buf_info->lastframe == true) {
+		/* update src buf status */
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		src_buf_info->lastframe = false;
+		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
+
+		/* update dst buf status */
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		dst_buf_info->used = false;
+
+		clean_display_buffer(ctx);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
+		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
+		clean_free_buffer(ctx);
+	} else {
+
+		dst_buf_info->vb.timestamp
+					= src_buf_info->vb.timestamp;
+		dst_buf_info->vb.timecode
+					= src_buf_info->vb.timecode;
+		mutex_lock(&dst_buf_info->lock);
+		dst_buf_info->used = true;
+		mutex_unlock(&dst_buf_info->lock);
+		src_buf_info->used = true;
+
+		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
+
+		if (ret) {
+			mtk_v4l2_err(
+				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
+				ctx->idx,
+				src_buf->index,
+				src_buf_info->lastframe,
+				buf.size,
+				src_buf_info->vb.timestamp.tv_sec,
+				src_buf_info->vb.timestamp.tv_usec,
+				dst_buf->index,
+				ret, res_chg);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+					VB2_BUF_STATE_ERROR);
+		} else if (res_chg == false) {
+			/* we only return src buffer with VB2_BUF_STATE_DONE
+			  * when decode success without resolution change
+			  */
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+							VB2_BUF_STATE_DONE);
+		}
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		clean_display_buffer(ctx);
+		clean_free_buffer(ctx);
+
+		if ((ret == 0) && (res_chg == true)) {
+			mtk_vdec_pic_info_update(ctx);
+			/*
+			  * On encountering a resolution change in the stream.
+			  * The driver must first process and decode all
+			  * remaining buffers from before the resolution change
+			  * point, so call flush decode here
+			  */
+			mtk_vdec_flush_decoder(ctx);
+			/*
+			  * After all buffers containing decoded frames from
+			  * before the resolution change point ready to be
+			  * dequeued on the CAPTURE queue, the driver sends a
+			  * V4L2_EVENT_SOURCE_CHANGE event for source change
+			  * type V4L2_EVENT_SRC_CH_RESOLUTION
+			  */
+			mtk_vdec_queue_res_chg_event(ctx);
+		}
+	}
+
+	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+
+}
+
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_unlock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_lock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	int ret = 0;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	src_vq->drv_priv	= ctx;
+	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	src_vq->ops		= &mtk_vdec_vb2_ops;
+	src_vq->mem_ops		= &vb2_dma_contig_memops;
+	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	src_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(src_vq);
+	if (ret) {
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
+		return ret;
+	}
+	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	dst_vq->drv_priv	= ctx;
+	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	dst_vq->ops		= &mtk_vdec_vb2_ops;
+	dst_vq->mem_ops		= &vb2_dma_contig_memops;
+	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	dst_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(dst_vq);
+	if (ret) {
+		vb2_queue_release(src_vq);
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
+{
+	vdec_if_deinit(ctx);
+	ctx->state = MTK_STATE_FREE;
+}
+
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
+{
+	struct mtk_q_data *q_data;
+
+	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
+	ctx->fh.m2m_ctx = ctx->m2m_ctx;
+	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
+	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
+
+	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
+	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
+
+
+	q_data = &ctx->q_data[MTK_Q_DATA_DST];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->coded_width = DFT_CFG_WIDTH;
+	q_data->coded_height = DFT_CFG_HEIGHT;
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+
+	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
+
+	v4l_bound_align_image(&q_data->coded_width,
+					MTK_VDEC_MIN_W,
+					MTK_VDEC_MAX_W, 4,
+					&q_data->coded_height,
+					MTK_VDEC_MIN_H,
+					MTK_VDEC_MAX_H, 5, 6);
+
+	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
+	q_data->bytesperline[0] = q_data->coded_width;
+	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
+	q_data->bytesperline[1] = q_data->coded_width;
+
+}
+
+static int vidioc_vdec_streamon(struct file *file, void *priv,
+				enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+
+	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_streamoff(struct file *file, void *priv,
+				 enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_reqbufs(struct file *file, void *priv,
+			       struct v4l2_requestbuffers *reqbufs)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	int ret;
+
+	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
+			 reqbufs->type, reqbufs->count);
+	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
+
+	return ret;
+}
+
+static int vidioc_vdec_querybuf(struct file *file, void *priv,
+				struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
+			 buf->type, buf->index);
+	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_qbuf(struct file *file, void *priv,
+			    struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct vb2_queue *vq;
+	struct vb2_buffer *vb;
+	struct mtk_video_buf *mtkbuf;
+	struct vb2_v4l2_buffer	*vb2_v4l2;
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
+	vb = vq->bufs[buf->index];
+	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+
+	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    (buf->m.planes[0].bytesused == 0)) {
+		mtkbuf->lastframe = true;
+		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
+			 ctx->idx, buf->type, buf->index,
+			 mtkbuf->lastframe, buf->bytesused,
+			 buf->m.planes[0].bytesused, buf->length,
+			 vb);
+	}
+
+	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_dqbuf(struct file *file, void *priv,
+			     struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_expbuf(struct file *file, void *priv,
+			      struct v4l2_exportbuffer *eb)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
+}
+
+static int vidioc_vdec_querycap(struct file *file, void *priv,
+				struct v4l2_capability *cap)
+{
+	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
+	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
+	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
+	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
+	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+
+	return 0;
+}
+
+static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_event_unsubscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	return 0;
+}
+
+static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
+		mtk_v4l2_err("sizeimage of output format must be given");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vidioc_vdec_g_crop(struct file *file, void *priv,
+			 struct v4l2_crop *cr)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state < MTK_STATE_HEADER)
+		return -EINVAL;
+
+	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
+
+		if (vdec_if_get_param(ctx,
+					   GET_PARAM_CROP_INFO,
+					   cr)) {
+			mtk_v4l2_debug(2,
+					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
+					 ctx->idx);
+			cr->c.left = 0;
+			cr->c.top = 0;
+			cr->c.width = ctx->picinfo.buf_w;
+			cr->c.height = ctx->picinfo.buf_h;
+		}
+		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
+				 cr->c.left, cr->c.top, cr->c.width,
+				 cr->c.height);
+	} else {
+		cr->c.left = 0;
+		cr->c.top = 0;
+		cr->c.width = ctx->picinfo.pic_w;
+		cr->c.height = ctx->picinfo.pic_h;
+		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
+				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
+				 ctx->picinfo.buf_h);
+	}
+	return 0;
+}
+
+static int vidioc_vdec_s_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+	int ret;
+	struct mtk_video_fmt *fmt;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	fmt = mtk_vdec_find_format(f);
+	if (fmt == NULL) {
+		mtk_v4l2_err("mtk_venc_find_format fail");
+		return ret;
+	}
+
+	pix_mp = &f->fmt.pix_mp;
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
+		mtk_v4l2_err("out_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
+		mtk_v4l2_err("cap_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq)
+		return -EINVAL;
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+	if (!q_data)
+		return -EINVAL;
+
+	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		q_data->fmt = fmt;
+		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+		pix_mp->width = 0;
+		pix_mp->height = 0;
+
+		if (ctx->state == MTK_STATE_FREE) {
+			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
+			if (ret) {
+				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
+					       ctx->idx, ret);
+				return -EINVAL;
+			}
+			ctx->state = MTK_STATE_INIT;
+		}
+	} else {
+		q_data->fmt = fmt;
+	}
+
+	return 0;
+}
+
+static int vidioc_enum_framesizes(struct file *file, void *priv,
+				struct v4l2_frmsizeenum *fsize)
+{
+	int i = 0;
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (fsize->index != 0)
+		return -EINVAL;
+
+	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
+		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
+			continue;
+
+		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
+		if (!(ctx->dev->dec_capability &
+				VCODEC_CAPABILITY_4K_DISABLED)) {
+			mtk_v4l2_debug(1, "4K is enabled");
+			fsize->stepwise.max_width =
+					VCODEC_DEC_4K_CODED_WIDTH;
+			fsize->stepwise.max_height =
+					VCODEC_DEC_4K_CODED_HEIGHT;
+		}
+		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
+					ctx->dev->dec_capability,
+					fsize->stepwise.min_width,
+					fsize->stepwise.max_width,
+					fsize->stepwise.step_width,
+					fsize->stepwise.min_height,
+					fsize->stepwise.max_height,
+					fsize->stepwise.step_height);
+		return 0;
+	}
+
+	return -EINVAL;
+
+}
+
+static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
+{
+	struct mtk_video_fmt *fmt;
+	int i, j = 0;
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
+			continue;
+		if (!output_queue &&
+			(mtk_video_formats[i].type != MTK_FMT_FRAME))
+			continue;
+
+		if (j == f->index)
+			break;
+		++j;
+	}
+
+	if (i == NUM_FORMATS)
+		return -EINVAL;
+
+	fmt = &mtk_video_formats[i];
+	f->pixelformat = fmt->fourcc;
+	f->flags = 0;
+	memset(f->reserved, 0, sizeof(f->reserved));
+
+	if (mtk_video_formats[i].type != MTK_FMT_DEC)
+		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
+
+	return 0;
+}
+
+static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, false);
+}
+
+static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, true);
+}
+
+static int vidioc_vdec_g_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq) {
+		mtk_v4l2_err("no vb2 queue for type=%d",
+					   f->type);
+		return -EINVAL;
+	}
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    (ctx->state >= MTK_STATE_HEADER)) {
+		/* previous decode might have resolution change that make
+		 * picinfo changed update context'x picinfo here to make sure
+		 * we know what report to user space
+		 */
+		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
+			sizeof(struct vdec_pic_info));
+		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
+					ctx->picinfo.y_len_sz;
+		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
+					ctx->picinfo.c_len_sz;
+		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
+		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
+		q_data->coded_width = ctx->picinfo.buf_w;
+		q_data->coded_height = ctx->picinfo.buf_h;
+
+		/*
+		 * Width and height are set to the dimensions
+		 * of the movie, the buffer is bigger and
+		 * further processing stages should crop to this
+		 * rectangle.
+		 */
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+
+		/*
+		 * Set pixelformat to the format in which mt vcodec
+		 * outputs the decoded frame
+		 */
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		/*
+		 * This is run on OUTPUT
+		 * The buffer contains compressed image
+		 * so width and height have no meaning.
+		 * Assign value here to pass v4l2-compliance test
+		 */
+		pix_mp->width = q_data->visible_width;
+		pix_mp->height = q_data->visible_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+	} else {
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
+				ctx->idx, ctx->state, f->type);
+		return -EAGAIN;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
+				   const void *parg,
+				   unsigned int *nbuffers,
+				   unsigned int *nplanes,
+				   unsigned int sizes[], void *alloc_ctxs[])
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
+	struct mtk_q_data *q_data;
+
+	q_data = mtk_vdec_get_q_data(ctx, vq->type);
+
+	switch (vq->type) {
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+		*nplanes = 1;
+
+		sizes[0] = q_data->sizeimage[0];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		break;
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+		if (ctx->state < MTK_STATE_HEADER)
+			return -EINVAL;
+
+		/*
+		 * Output plane count is 2 - one for Y and one for CbCr
+		 */
+		*nplanes = 2;
+
+		mtk_v4l2_debug(1,
+				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
+				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
+
+		sizes[0] = q_data->sizeimage[0];
+		sizes[1] = q_data->sizeimage[1];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		alloc_ctxs[1] = ctx->dev->alloc_ctx;
+		break;
+	default:
+		break;
+	}
+
+	mtk_v4l2_debug(1,
+			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
+			ctx->idx, vq->type, *nplanes, *nbuffers,
+			sizes[0], sizes[1]);
+
+	return 0;
+}
+
+static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct mtk_q_data *q_data;
+	int i;
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
+			 ctx->idx, vb->vb2_queue->type, vb->index);
+
+	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
+
+	for (i = 0; i < q_data->fmt->num_planes; i++) {
+		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
+			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
+				       i, vb2_plane_size(vb, i),
+				       q_data->sizeimage[i]);
+		}
+	}
+
+	return 0;
+}
+
+static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
+			ctx->idx, vb->vb2_queue->type,
+			vb->index, vb);
+	/*
+	 * check if this buffer is ready to be used after decode
+	 */
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		struct vb2_buffer *src_buf;
+		struct mtk_vcodec_mem buf;
+		bool res_chg = false;
+		int ret = 0;
+		int dpbsize = 1;
+
+		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
+
+		if (ctx->state == MTK_STATE_INIT) {
+			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+			if (!src_buf) {
+				mtk_v4l2_err("No src buffer");
+				return;
+			}
+
+			buf.va	= vb2_plane_vaddr(src_buf, 0);
+			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
+							src_buf,
+							0);
+			buf.size = (size_t)src_buf->planes[0].bytesused;
+			mtk_v4l2_debug(2,
+					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
+					ctx->idx,
+					src_buf->index,
+					buf.va, (u64)buf.dma_addr, buf.size);
+
+			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
+			if (ret) {
+				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+						VB2_BUF_STATE_DONE);
+				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
+						ctx->idx,
+						src_buf->index,
+						buf.size, ret);
+				return;
+			}
+
+			if (vdec_if_get_param(ctx,
+						   GET_PARAM_PIC_INFO,
+						   &ctx->picinfo)) {
+				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+						ctx->idx);
+				return;
+			}
+
+			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
+				sizeof(struct vdec_pic_info));
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+						ctx->picinfo.y_bs_sz +
+						ctx->picinfo.y_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
+							ctx->picinfo.buf_w;
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
+						ctx->picinfo.c_bs_sz +
+						ctx->picinfo.c_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
+							ctx->picinfo.buf_w;
+			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
+				ctx->idx,
+				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
+				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
+
+			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+			if (dpbsize == 0) {
+				mtk_v4l2_debug(2,
+						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
+						ctx->idx, ret);
+				dpbsize = 1;
+			}
+			ctx->dpb_count = dpbsize;
+			ctx->state = MTK_STATE_HEADER;
+			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
+							ctx->dpb_count);
+		} else {
+			mtk_v4l2_debug(1, "[%d] already init driver %d",
+					 ctx->idx, ctx->state);
+		}
+
+	} else {
+		mutex_lock(&buf->lock);
+		if (buf->used == false) {
+			v4l2_m2m_buf_queue(ctx->m2m_ctx,
+					to_vb2_v4l2_buffer(vb));
+			buf->queued_in_vb2 = true;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		} else {
+			buf->queued_in_vb2 = false;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		}
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
+{
+	if (vb->vb2_queue->type ==
+		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+		mutex_lock(&buf->lock);
+		buf->queued_in_v4l2 = false;
+		buf->queued_in_vb2 = false;
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		buf->used = false;
+		buf->ready_to_display = false;
+		buf->nonrealdisplay = false;
+		buf->queued_in_v4l2 = false;
+		mutex_init(&buf->lock);
+	} else {
+		buf->lastframe = false;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	if (ctx->state == MTK_STATE_FLUSH)
+		ctx->state = MTK_STATE_HEADER;
+
+	return 0;
+}
+
+static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
+{
+	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
+			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
+
+	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		while (src_buf) {
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+					VB2_BUF_STATE_ERROR);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		}
+	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		int i;
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		if (ctx->state >= MTK_STATE_HEADER)
+			mtk_vdec_flush_decoder(ctx);
+
+		ctx->state = MTK_STATE_FLUSH;
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		while (dst_buf) {
+			vb2_set_plane_payload(dst_buf, 0, 0);
+			vb2_set_plane_payload(dst_buf, 1, 0);
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
+				VB2_BUF_STATE_ERROR);
+			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		}
+
+		for (i = 0; i < q->num_buffers; ++i) {
+			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
+				vb2_v4l2 = container_of(q->bufs[i],
+					struct vb2_v4l2_buffer, vb2_buf);
+				buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
+						ctx->idx, i, q->type,
+						buf->queued_in_vb2,
+						buf->queued_in_v4l2,
+						(int)q->bufs[i]->state);
+				v4l2_m2m_buf_done(vb2_v4l2,
+						VB2_BUF_STATE_ERROR);
+			}
+		}
+	}
+}
+
+static void m2mops_vdec_device_run(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	struct mtk_vcodec_dev *dev = ctx->dev;
+
+	queue_work(dev->decode_workqueue, &ctx->decode_work);
+}
+
+static int m2mops_vdec_job_ready(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (ctx->state == MTK_STATE_ABORT)
+		return 0;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
+		return 0;
+
+	if (ctx->state != MTK_STATE_HEADER)
+		return 0;
+
+	return 1;
+}
+
+static void m2mops_vdec_job_abort(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+
+	ctx->state = MTK_STATE_ABORT;
+}
+
+static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
+
+	switch (ctrl->id) {
+	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
+		if (ctx->state >= MTK_STATE_HEADER) {
+			ctrl->val = ctx->dpb_count;
+		} else {
+			mtk_v4l2_err("Seqinfo not ready");
+			ctrl->val = 1;
+			return -EAGAIN;
+		}
+		break;
+	default:
+		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
+	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
+};
+
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
+{
+	struct v4l2_ctrl *ctrl;
+
+	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
+
+	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
+					  &mtk_vcodec_dec_ctrl_ops,
+					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
+					  1, 32, 1, 1);
+	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
+
+	if (ctx->ctrl_hdl.error) {
+		mtk_v4l2_err("Adding control failed %d",
+					ctx->ctrl_hdl.error);
+		return ctx->ctrl_hdl.error;
+	}
+
+	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
+	return 0;
+}
+
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
+{
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+}
+
+static void m2mops_vdec_lock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_lock(&ctx->dev->dev_mutex);
+}
+
+static void m2mops_vdec_unlock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_unlock(&ctx->dev->dev_mutex);
+}
+
+const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
+	.device_run	= m2mops_vdec_device_run,
+	.job_ready	= m2mops_vdec_job_ready,
+	.job_abort	= m2mops_vdec_job_abort,
+	.lock	= m2mops_vdec_lock,
+	.unlock	= m2mops_vdec_unlock,
+};
+
+const struct vb2_ops mtk_vdec_vb2_ops = {
+	.queue_setup	= vb2ops_vdec_queue_setup,
+	.buf_prepare	= vb2ops_vdec_buf_prepare,
+	.buf_queue	= vb2ops_vdec_buf_queue,
+	.wait_prepare	= vb2_ops_wait_prepare,
+	.wait_finish	= vb2_ops_wait_finish,
+	.buf_init	= vb2ops_vdec_buf_init,
+	.buf_finish	= vb2ops_vdec_buf_finish,
+	.start_streaming	= vb2ops_vdec_start_streaming,
+	.stop_streaming	= vb2ops_vdec_stop_streaming,
+};
+
+const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
+	.vidioc_streamon	= vidioc_vdec_streamon,
+	.vidioc_streamoff	= vidioc_vdec_streamoff,
+
+	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
+	.vidioc_querybuf	= vidioc_vdec_querybuf,
+	.vidioc_qbuf	= vidioc_vdec_qbuf,
+	.vidioc_expbuf	= vidioc_vdec_expbuf,
+	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
+
+	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
+	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
+
+	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
+	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
+
+	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
+	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
+	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
+
+	.vidioc_querycap	= vidioc_vdec_querycap,
+	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
+	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
+	.vidioc_g_crop	= vidioc_vdec_g_crop,
+};
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
new file mode 100644
index 0000000..cb5cb42
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
@@ -0,0 +1,81 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_H_
+#define _MTK_VCODEC_DEC_H_
+
+#include <media/videobuf2-core.h>
+#include <media/videobuf2-v4l2.h>
+
+/**
+ * struct vdec_fb  - decoder frame buffer
+ * @base_y	: Y plane memory info
+ * @base_c	: C plane memory info
+ * @status      : frame buffer status (vdec_fb_status)
+ */
+struct vdec_fb {
+	struct mtk_vcodec_mem	base_y;
+	struct mtk_vcodec_mem	base_c;
+	unsigned int	status;
+};
+
+/**
+ * struct mtk_video_buf - Private data related to each VB2 buffer.
+ * @b:			VB2 buffer
+ * @list:			link list
+ * @used:		Output buffer contain decoded frame data
+ * @ready_to_display:	Output buffer not display yet
+ * @nonrealdisplay:	Output buffer is not display frame
+ * @queued_in_vb2:	Output buffer is queue in vb2
+ * @queued_in_v4l2:	Output buffer is in v4l2
+ * @lastframe:		Intput buffer is last buffer - EOS
+ * @frame_buffer:		Decode status of output buffer
+ * @lock:			V4L2 and decode thread should get mutex
+ *			before r/w info in mtk_video_buf
+ */
+struct mtk_video_buf {
+	struct vb2_v4l2_buffer	vb;
+	struct list_head	list;
+
+	bool	used;
+	bool	ready_to_display;
+	bool	nonrealdisplay;
+	bool	queued_in_vb2;
+	bool	queued_in_v4l2;
+	bool	lastframe;
+	struct vdec_fb	frame_buffer;
+	struct mutex	lock;
+};
+
+extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
+extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
+
+
+/*
+ * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
+ * get/release lock before/after access decoder hw.
+ * mtk_vdec_lock get decoder hw lock and set curr_ctx
+ * to ctx instance that get lock
+ */
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq);
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
+
+#endif /* _MTK_VCODEC_DEC_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
new file mode 100644
index 0000000..34e3217
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
@@ -0,0 +1,469 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vpu.h"
+
+module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
+module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
+
+#define VDEC_HW_ACTIVE	0x10
+#define VDEC_IRQ_CFG	0x11
+#define VDEC_IRQ_CLR	0x10
+#define VDEC_IRQ_CFG_REG	0xa4
+
+
+/* Wake up context wait_queue */
+static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
+{
+	ctx->int_cond = 1;
+	ctx->int_type = reason;
+	wake_up_interruptible(&ctx->queue);
+}
+
+static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	unsigned long flags;
+	struct mtk_vcodec_ctx *ctx;
+	unsigned int cg_status = 0;
+	unsigned int dec_done_status = 0;
+
+	spin_lock_irqsave(&dev->irqlock, flags);
+	ctx = dev->curr_ctx;
+	spin_unlock_irqrestore(&dev->irqlock, flags);
+
+	cg_status = readl(dev->reg_base[0] + (0));
+	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
+		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
+			       cg_status);
+		return IRQ_HANDLED;
+	}
+
+	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	ctx->irq_status = dec_done_status;
+	if ((dec_done_status & (0x1 << 16)) != 0x10000)
+		return IRQ_HANDLED;
+
+	/* clear interrupt */
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+
+	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
+
+	mtk_v4l2_debug(3,
+			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
+			ctx->idx, dec_done_status);
+
+
+	return IRQ_HANDLED;
+}
+
+static int fops_vcodec_open(struct file *file)
+{
+	struct video_device *vfd = video_devdata(file);
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = NULL;
+	int ret = 0;
+
+	if (dev->instance_mask == ~0UL) {
+		mtk_v4l2_err("Too many open contexts");
+		ret = -EBUSY;
+		goto err_alloc;
+	}
+
+	mutex_lock(&dev->dev_mutex);
+
+	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	ctx->idx = ffz(dev->instance_mask);
+	v4l2_fh_init(&ctx->fh, video_devdata(file));
+	file->private_data = &ctx->fh;
+	v4l2_fh_add(&ctx->fh);
+	INIT_LIST_HEAD(&ctx->list);
+	ctx->dev = dev;
+	init_waitqueue_head(&ctx->queue);
+
+	if (vfd == dev->vfd_dec) {
+		ctx->type = MTK_INST_DECODER;
+		ret = mtk_vdec_ctrls_setup(ctx);
+		if (ret) {
+			mtk_v4l2_err("Failed to setup mt vcodec controls");
+			goto err_ctrls_setup;
+		}
+		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
+						 &m2mctx_vdec_queue_init);
+		if (IS_ERR(ctx->m2m_ctx)) {
+			ret = PTR_ERR(ctx->m2m_ctx);
+			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
+				       ret);
+			goto err_ctx_init;
+		}
+		mtk_vcodec_dec_set_default_params(ctx);
+	} else {
+		mtk_v4l2_err("Invalid vfd !");
+		ret = -ENOENT;
+		goto err_ctx_init;
+	}
+
+	if (v4l2_fh_is_singular(&ctx->fh)) {
+		mtk_vcodec_dec_pw_on(&dev->pm);
+		ret = vpu_load_firmware(dev->vpu_plat_dev);
+		if (ret < 0) {
+			mtk_v4l2_err("vpu_load_firmware failed!");
+			goto err_load_fw;
+		}
+
+		dev->dec_capability =
+			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
+		mtk_v4l2_debug(0, "decoder capability %x",
+			       dev->dec_capability);
+	}
+
+	set_bit(ctx->idx, &dev->instance_mask);
+	dev->num_instances++;
+	list_add(&ctx->list, &dev->ctx_list);
+	mutex_unlock(&dev->dev_mutex);
+	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
+					ctx->idx);
+
+	return ret;
+
+	/* Deinit when failure occurred */
+err_load_fw:
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+err_ctx_init:
+	mtk_vdec_ctrls_free(ctx);
+err_ctrls_setup:
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+err_alloc:
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_release(struct file *file)
+{
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
+
+	mutex_lock(&dev->dev_mutex);
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+	mtk_vcodec_vdec_release(ctx);
+	mtk_vdec_ctrls_free(ctx);
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	list_del_init(&ctx->list);
+	dev->num_instances--;
+	if (dev->num_instances == 0)
+		mtk_vcodec_dec_pw_off(&dev->pm);
+	clear_bit(ctx->idx, &dev->instance_mask);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+	mutex_unlock(&dev->dev_mutex);
+	return 0;
+}
+
+static unsigned int fops_vcodec_poll(struct file *file,
+				     struct poll_table_struct *wait)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	int ret;
+
+	mutex_lock(&dev->dev_mutex);
+	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
+}
+
+static const struct v4l2_file_operations mtk_vcodec_fops = {
+	.owner				= THIS_MODULE,
+	.open				= fops_vcodec_open,
+	.release			= fops_vcodec_release,
+	.poll				= fops_vcodec_poll,
+	.unlocked_ioctl			= video_ioctl2,
+	.mmap				= fops_vcodec_mmap,
+};
+
+static void mtk_vcodec_dec_reset_handler(void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	struct mtk_vcodec_ctx *ctx;
+
+	mtk_v4l2_debug(0, "Watchdog timeout!!");
+
+	mutex_lock(&dev->dev_mutex);
+	list_for_each_entry(ctx, &dev->ctx_list, list) {
+		ctx->state = MTK_STATE_ABORT;
+		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
+					   ctx->idx);
+	}
+	mutex_unlock(&dev->dev_mutex);
+}
+
+static int mtk_vcodec_probe(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev;
+	struct video_device *vfd_dec;
+	struct resource *res;
+	int i, ret;
+	int reg_base_num;
+
+	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&dev->ctx_list);
+	dev->plat_dev = pdev;
+	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
+	if (dev->vpu_plat_dev == NULL) {
+		mtk_v4l2_err("[VPU] vpu device in not ready");
+		return -EPROBE_DEFER;
+	}
+
+	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
+				dev, VPU_RST_DEC);
+
+	ret = mtk_vcodec_init_dec_pm(dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
+		return ret;
+	}
+
+	reg_base_num = NUM_MAX_VDEC_REG_BASE;
+	for (i = 0; i < reg_base_num; i++) {
+		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
+		if (res == NULL) {
+			dev_err(&pdev->dev, "get memory resource failed.");
+			ret = -ENXIO;
+			goto err_res;
+		}
+		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
+		if (IS_ERR(dev->reg_base[i])) {
+			dev_err(&pdev->dev,
+				"devm_ioremap_resource %d failed.", i);
+			ret = PTR_ERR(dev->reg_base);
+			goto err_res;
+		}
+		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "failed to get irq resource");
+		ret = -ENOENT;
+		goto err_res;
+	}
+
+	dev->dec_irq = platform_get_irq(pdev, 0);
+	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
+			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
+			dev->dec_irq,
+			ret);
+		ret = -EINVAL;
+		goto err_res;
+	}
+
+	disable_irq(dev->dec_irq);
+	mutex_init(&dev->dev_mutex);
+	mutex_init(&dev->dec_mutex);
+	spin_lock_init(&dev->irqlock);
+
+	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
+		 "[MTK_V4L2]");
+
+	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
+	if (ret) {
+		mtk_v4l2_err("v4l2_device_register err=%d", ret);
+		return ret;
+	}
+
+	init_waitqueue_head(&dev->queue);
+
+	vfd_dec = video_device_alloc();
+	if (!vfd_dec) {
+		mtk_v4l2_err("Failed to allocate video device");
+		ret = -ENOMEM;
+		goto err_dec_alloc;
+	}
+	vfd_dec->fops		= &mtk_vcodec_fops;
+	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
+	vfd_dec->release	= video_device_release;
+	vfd_dec->lock		= &dev->dev_mutex;
+	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
+	vfd_dec->vfl_dir	= VFL_DIR_M2M;
+
+	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
+		 MTK_VCODEC_DEC_NAME);
+	video_set_drvdata(vfd_dec, dev);
+	dev->vfd_dec = vfd_dec;
+	platform_set_drvdata(pdev, dev);
+
+	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
+	if (IS_ERR(dev->alloc_ctx)) {
+		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
+		ret = PTR_ERR(dev->alloc_ctx);
+		goto err_vb2_ctx_init;
+	}
+
+	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
+	if (IS_ERR(dev->m2m_dev_dec)) {
+		mtk_v4l2_err("Failed to init mem2mem dec device");
+		ret = PTR_ERR(dev->m2m_dev_dec);
+		goto err_dec_mem_init;
+	}
+
+	dev->decode_workqueue =
+			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
+			WQ_MEM_RECLAIM | WQ_FREEZABLE);
+	if (!dev->decode_workqueue) {
+		mtk_v4l2_err("Failed to create decode workqueue");
+		ret = -EINVAL;
+		goto err_event_workq;
+	}
+
+	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
+	if (ret) {
+		mtk_v4l2_err("Failed to register video device");
+		goto err_dec_reg;
+	}
+	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
+			 vfd_dec->num);
+
+	return 0;
+
+err_dec_reg:
+	destroy_workqueue(dev->decode_workqueue);
+err_event_workq:
+	v4l2_m2m_release(dev->m2m_dev_dec);
+err_dec_mem_init:
+	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+err_vb2_ctx_init:
+	video_unregister_device(vfd_dec);
+err_dec_alloc:
+	v4l2_device_unregister(&dev->v4l2_dev);
+err_res:
+	mtk_vcodec_release_dec_pm(dev);
+	return ret;
+}
+
+static const struct of_device_id mtk_vcodec_match[] = {
+	{.compatible = "mediatek,mt8173-vcodec-dec",},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
+
+static int mtk_vcodec_dec_remove(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug_enter();
+	flush_workqueue(dev->decode_workqueue);
+	destroy_workqueue(dev->decode_workqueue);
+	if (dev->m2m_dev_dec)
+		v4l2_m2m_release(dev->m2m_dev_dec);
+	if (dev->alloc_ctx)
+		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+
+	if (dev->vfd_dec)
+		video_unregister_device(dev->vfd_dec);
+
+	v4l2_device_unregister(&dev->v4l2_dev);
+	mtk_vcodec_release_dec_pm(dev);
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
+	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
+			   mtk_vcodec_vdec_pm_runtime_resume,
+			   NULL)
+};
+
+static struct platform_driver mtk_vcodec_dec_driver = {
+	.probe	= mtk_vcodec_probe,
+	.remove	= mtk_vcodec_dec_remove,
+	.driver	= {
+		.name	= MTK_VCODEC_DEC_NAME,
+		.owner	= THIS_MODULE,
+		.of_match_table = mtk_vcodec_match,
+		.pm = &mtk_vcodec_vdec_pm_ops,
+	},
+};
+
+module_platform_driver(mtk_vcodec_dec_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
new file mode 100644
index 0000000..aa1701f
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
@@ -0,0 +1,153 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/clk.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <soc/mediatek/smi.h>
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
+{
+	struct device_node *node;
+	struct platform_device *pdev;
+	struct device *dev;
+	struct mtk_vcodec_pm *pm;
+	int ret = 0;
+
+	pdev = mtkdev->plat_dev;
+	pm = &mtkdev->pm;
+	pm->mtkdev = mtkdev;
+	dev = &pdev->dev;
+	node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
+	if (!node)
+		return -1;
+
+	pdev = of_find_device_by_node(node);
+	if (WARN_ON(!pdev)) {
+		of_node_put(node);
+		return -1;
+	}
+	pm->larbvdec = &pdev->dev;
+
+	pdev = mtkdev->plat_dev;
+	pm_runtime_enable(&pdev->dev);
+	pm->dev = &pdev->dev;
+
+	pm->vcodecpll = devm_clk_get(&pdev->dev, "vcodecpll");
+	if (pm->vcodecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vcodecpll fail");
+		ret = -1;
+	}
+
+	pm->univpll_d2 = devm_clk_get(&pdev->dev, "univpll_d2");
+	if (pm->univpll_d2 == NULL) {
+		mtk_v4l2_err("devm_clk_get univpll_d2 fail");
+		ret = -1;
+	}
+
+	pm->clk_cci400_sel = devm_clk_get(&pdev->dev, "clk_cci400_sel");
+	if (pm->clk_cci400_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get clk_cci400_sel fail");
+		ret = -1;
+	}
+
+	pm->vdec_sel = devm_clk_get(&pdev->dev, "vdec_sel");
+	if (pm->vdec_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get vdec_sel fail");
+		ret = -1;
+	}
+
+	pm->vdecpll = devm_clk_get(&pdev->dev, "vdecpll");
+	if (pm->vdecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vdecpll fail");
+		ret = -1;
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
+{
+	pm_runtime_disable(dev->pm.dev);
+}
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_get_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_get_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_put_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_put_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	clk_set_rate(pm->vcodecpll, 1482 * 1000000);
+
+	ret = clk_prepare_enable(pm->vcodecpll);
+	if (ret)
+		mtk_v4l2_err("vcodecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("univpll_d2 fail %d", ret);
+
+	ret = clk_prepare_enable(pm->clk_cci400_sel);
+	if (ret)
+		mtk_v4l2_err("clk_cci400_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->clk_cci400_sel, pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("vdecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdec_sel);
+	if (ret)
+		mtk_v4l2_err("vdec_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->vdec_sel, pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = mtk_smi_larb_get(pm->larbvdec);
+	if (ret)
+		mtk_v4l2_err("mtk_smi_larb_get larbvdec fail %d", ret);
+
+}
+
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm)
+{
+	mtk_smi_larb_put(pm->larbvdec);
+	clk_disable_unprepare(pm->vdec_sel);
+	clk_disable_unprepare(pm->vdecpll);
+}
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
new file mode 100644
index 0000000..28de4c1
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
@@ -0,0 +1,28 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_PM_H_
+#define _MTK_VCODEC_DEC_PM_H_
+
+#include "mtk_vcodec_drv.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm);
+
+#endif /* _MTK_VCODEC_DEC_PM_H_ */
\ No newline at end of file
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
index 55cf462..e3e3d66 100644
--- a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
@@ -26,10 +26,10 @@
 #include "mtk_vcodec_util.h"
 
 #define MTK_VCODEC_DRV_NAME	"mtk_vcodec_drv"
+#define MTK_VCODEC_DEC_NAME	"mtk-vcodec-dec" 
 #define MTK_VCODEC_ENC_NAME	"mtk-vcodec-enc"
 #define MTK_PLATFORM_STR	"platform:mt8173"
 
-
 #define MTK_VCODEC_MAX_PLANES	3
 #define MTK_V4L2_BENCHMARK	0
 #define WAIT_INTR_TIMEOUT_MS	1000
@@ -145,18 +145,18 @@ struct mtk_q_data {
 /**
  * struct mtk_enc_params - General encoding parameters
  * @bitrate: target bitrate in bits per second
- * @num_b_frame:	number of b frames between p-frame
- * @rc_frame:	frame based rate control
+ * @num_b_frame: number of b frames between p-frame
+ * @rc_frame: frame based rate control
  * @rc_mb: macroblock based rate control
  * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
- *				  with the first frame
+ *		  with the first frame
  * @intra_period: I frame period
- * @gop_size:	group of picture size, it's used as the intra frame period
- * @framerate_num: frame rate numerator. ex: framerate_num=30 and framerate_denom=1
- *				   menas FPS is 30
- * @framerate_denom:	frame rate denominator. ex: framerate_num=30 and
- *				       framerate_denom=1 menas FPS is 30
- * @h264_max_qp:	Max value for H.264 quantization parameter
+ * @gop_size: group of picture size, it's used as the intra frame period
+ * @framerate_num: frame rate numerator. ex: framerate_num=30 and
+ *		   framerate_denom=1 menas FPS is 30
+ * @framerate_denom: frame rate denominator. ex: framerate_num=30
+ *		     and framerate_denom=1 menas FPS is 30
+ * @h264_max_qp: Max value for H.264 quantization parameter
  * @h264_profile: V4L2 defined H.264 profile
  * @h264_level: V4L2 defined H.264 level
  * @force_intra: force/insert intra frame
@@ -197,33 +197,61 @@ struct mtk_vcodec_pm {
 	struct mtk_vcodec_dev	*mtkdev;
 };
 
+/**
+ * struct vdec_pic_info  - picture size information
+ * @pic_w: picture width
+ * @pic_h: picture height
+ * @buf_w: picture buffer width (16 aligned up from pic_w)
+ * @buf_h: picture buffer heiht (32 aligned up from pic_h)
+ * @y_bs_sz: Y bitstream size
+ * @c_bs_sz: CbCr bitstream size
+ * @y_len_sz: Y length size
+ * @c_len_sz: CbCr length size
+ * E.g. suppose picture size is 176x144,
+ *      buffer size will be aligned to 176x160.
+ */
+struct vdec_pic_info {
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int y_bs_sz;
+	unsigned int c_bs_sz;
+	unsigned int y_len_sz;
+	unsigned int c_len_sz;
+};
 
 /**
  * struct mtk_vcodec_ctx - Context (instance) private data.
  *
  * @type: type of the instance - decoder or encoder
- * @dev:	pointer to the mtk_vcodec_dev of the device
+ * @dev: pointer to the mtk_vcodec_dev of the device
  * @list: link to ctx_list of mtk_vcodec_dev
  * @fh: struct v4l2_fh
  * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
  * @q_data: store information of input and output queue
- *		    of the context
+ *		of the context
  * @idx: index of the context that this structure describes
  * @state: state of the context
  * @param_change: indicate encode parameter type
  * @enc_params: encoding parameters
- * @enc_if: hoooked encoder driver interface
+ * @dec_if: hooked decoder driver interface
+ * @enc_if: hooked encoder driver interface
  * @drv_handle: driver handle for specific decode/encode instance
  *
+ * @picinfo: store picture info after header parsing
+ * @dpb_count: store dpb count after header parsing
  * @int_cond: variable used by the waitqueue
  * @int_type: type of the last interrupt
  * @queue: waitqueue that can be used to wait for this context to
- *		   finish
+ *	finish
  * @irq_status: irq status
  *
  * @ctrl_hdl: handler for v4l2 framework
  *
+ * @decode_work: worker for the decoding
  * @encode_work: worker for the encoding
+ * @last_decoded_picinfo: pic information get from latest decode
  */
 struct mtk_vcodec_ctx {
 	enum mtk_instance_type type;
@@ -238,17 +266,24 @@ struct mtk_vcodec_ctx {
 	enum mtk_encode_param param_change;
 	struct mtk_enc_params enc_params;
 
+	struct vdec_common_if *dec_if;
 	struct venc_common_if *enc_if;
 	unsigned long drv_handle;
 
+	struct vdec_pic_info picinfo;
+	int dpb_count;
+
 	int int_cond;
 	int int_type;
 	wait_queue_head_t queue;
 	unsigned int irq_status;
 
 	struct v4l2_ctrl_handler ctrl_hdl;
+	struct work_struct decode_work;
 	struct work_struct encode_work;
+	struct vdec_pic_info last_decoded_picinfo;
 
+	int decoded_frame_cnt;
 #if MTK_V4L2_BENCHMARK
 	unsigned int total_enc_dec_cnt;
 	unsigned int total_enc_dec_time;
@@ -289,36 +324,61 @@ struct mtk_vcodec_ctx {
  *
  * @encode_workqueue: encode work queue
  *
+ * @v4l2_dev: V4L2 device to register video devices for.
+ * @vfd_dec: Video device for decoder
+ * @vfd_enc: Video device for encoder
+ *
+ * @m2m_dev_dec: m2m device for decoder
+ * @m2m_dev_enc: m2m device for encoder
+ * @plat_dev: platform device
+ * @vpu_plat_dev: mtk vpu platform device
+ * @alloc_ctx:VB2 allocator context
+ *	      (for allocations without kernel mapping).
+ * @ctx_list: list of struct mtk_vcodec_ctx
+ * @irqlock: protect data access by irq handler and work thread
+ * @curr_ctx: The context that is waiting for codec hardware
+ * @reg_base: Mapped address of MTK Vcodec registers.
+ *
+ * @instance_mask: used to mark which index id are used
+ * @num_instances: counter of active MTK Vcodec instances
+ *
+ * @decode_workqueue: decode work queue
+ * @encode_workqueue: encode work queue
  * @int_cond: used to identify interrupt condition happen
  * @int_type: used to identify what kind of interrupt condition happen
  * @dev_mutex: video_device lock
  * @queue: waitqueue for waiting for completion of device commands
  *
+ * @dec_irq: decoder irq resource
  * @enc_irq: h264 encoder irq resource
- * @enc_lt_irq: vp8 encoder irq resource
+ * @enc_lt_irq: vp8 encoder lt irq resource
  *
- * @enc_mutex: encoder hardware lock.
+ * @dec_mutex: decoder hardware lock
+ * @enc_mutex: encoder hardware lock
  *
  * @pm: power management control
  * @dec_capability: used to identify decode capability, ex: 4k
- * @enc_capability:	 used to identify encode capability
+ * @enc_capability: used to identify encode capability
  */
 struct mtk_vcodec_dev {
 	struct v4l2_device	v4l2_dev;
+	struct video_device	*vfd_dec;
 	struct video_device	*vfd_enc;
 
+	struct v4l2_m2m_dev	*m2m_dev_dec;
 	struct v4l2_m2m_dev	*m2m_dev_enc;
 	struct platform_device	*plat_dev;
 	struct platform_device	*vpu_plat_dev;
 	struct vb2_alloc_ctx	*alloc_ctx;
 	struct list_head	ctx_list;
 	spinlock_t	irqlock;
-	struct mtk_vcodec_ctx *curr_ctx;
+	struct mtk_vcodec_ctx	*curr_ctx;
 	void __iomem	*reg_base[NUM_MAX_VCODEC_REG_BASE];
 
 	unsigned long	instance_mask;
 	int	num_instances;
 
+	struct workqueue_struct *decode_workqueue;
 	struct workqueue_struct *encode_workqueue;
 
 	int	int_cond;
@@ -326,9 +386,11 @@ struct mtk_vcodec_dev {
 	struct mutex	dev_mutex;
 	wait_queue_head_t	queue;
 
+	int	dec_irq;
 	int	enc_irq;
 	int	enc_lt_irq;
 
+	struct mutex	dec_mutex;
 	struct mutex	enc_mutex;
 
 	struct mtk_vcodec_pm	pm;
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_base.h b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
new file mode 100644
index 0000000..73ea1fe
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_BASE_
+#define _VDEC_DRV_BASE_
+
+#include "mtk_vcodec_drv.h"
+
+#include "vdec_drv_if.h"
+
+struct vdec_common_if {
+	/**
+	 * (*init)() - initialize decode driver
+	 * @ctx     : [in] mtk v4l2 context
+	 * @h_vdec  : [out] driver handle
+	 */
+	int (*init)(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec);
+
+	/**
+	 * (*decode)() - trigger decode
+	 * @h_vdec  : [in] driver handle
+	 * @bs      : [in] input bitstream
+	 * @fb      : [in] frame buffer to store decoded frame
+	 * @res_chg : [out] resolution change happen
+	 */
+	int (*decode)(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		      struct vdec_fb *fb, bool *res_chg);
+
+	/**
+	 * (*get_param)() - get driver's parameter
+	 * @h_vdec : [in] driver handle
+	 * @type   : [in] input parameter type
+	 * @out    : [out] buffer to store query result
+	 */
+	int (*get_param)(unsigned long h_vdec, enum vdec_get_param_type type,
+			 void *out);
+
+	/**
+	 * (*deinit)() - deinitialize driver.
+	 * @h_vdec : [in] driver handle to be deinit
+	 */
+	int (*deinit)(unsigned long h_vdec);
+};
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
new file mode 100644
index 0000000..1d12719
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *         Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+
+#include "vdec_drv_base.h"
+#include "vdec_drv_if.h"
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
+{
+	int ret = 0;
+
+	switch (fourcc) {
+	case V4L2_PIX_FMT_H264:
+	case V4L2_PIX_FMT_VP8:
+	case V4L2_PIX_FMT_VP9:
+	default:
+		return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->init(ctx, &ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	unsigned long flags;
+	int ret = 0;
+
+	if (bs) {
+		if ((bs->dma_addr & 63) != 0)
+			return -EINVAL;
+	}
+
+	if (fb) {
+		if (((fb->base_y.dma_addr & 511) != 0) ||
+		    ((fb->base_c.dma_addr & 511) != 0))
+			return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = ctx;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	enable_irq(ctx->dev->dec_irq);
+	ret = ctx->dec_if->decode(ctx->drv_handle, bs, fb, res_chg);
+	disable_irq(ctx->dev->dec_irq);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = NULL;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out)
+{
+	int ret = 0;
+
+	mtk_vdec_lock(ctx);
+	ret = ctx->dec_if->get_param(ctx->drv_handle, type, out);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx)
+{
+	int ret = 0;
+
+	if (ctx->drv_handle == 0)
+		return 0;
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->deinit(ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	ctx->drv_handle = 0;
+	return ret;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.h b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
new file mode 100644
index 0000000..9717446
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *		   Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_IF_H_
+#define _VDEC_DRV_IF_H_
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_util.h"
+
+/**
+ * struct vdec_fb_status  - decoder frame buffer status
+ * @FB_ST_NORMAL	: initial state
+ * @FB_ST_DISPLAY	: frmae buffer is ready to be displayed
+ * @FB_ST_FREE		: frame buffer is not used by decoder any more
+ */
+enum vdec_fb_status {
+	FB_ST_NORMAL		= 0,
+	FB_ST_DISPLAY		= (1 << 0),
+	FB_ST_FREE		= (1 << 1)
+};
+
+enum vdec_get_param_type {
+	GET_PARAM_DISP_FRAME_BUFFER,
+	GET_PARAM_FREE_FRAME_BUFFER,
+	GET_PARAM_PIC_INFO,
+	GET_PARAM_CROP_INFO,
+	GET_PARAM_DPB_SIZE
+};
+
+/**
+ * struct vdec_fb_node  - decoder frame buffer node
+ * @list	: list to hold this node
+ * @fb		: point to frame buffer (vdec_fb)
+ */
+struct vdec_fb_node {
+	struct list_head list;
+	void *fb;
+};
+
+/**
+ * For the same vdec_handle, these functions below are not thread safe.
+ * For different vdec_handle, these functions can be called at the same time.
+ */
+
+/**
+ * vdec_if_init() - initialize decode driver
+ * @ctx      : [in] v4l2 context
+ * @fourcc   : [in] video format fourcc, V4L2_PIX_FMT_H264/VP8/VP9..
+ */
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc);
+
+/**
+ * vdec_if_deinit() - deinitialize decode driver
+ * @ctx : [in] v4l2 context
+ *
+ */
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx);
+
+/**
+ * vdec_if_decode() - trigger decode
+ * @handle  : [in] video decode handle
+ * @bs      : [in] input bitstream
+ * @fb      : [in] frame buffer to store decoded frame
+ * @res_chg : [out] resolution change happen
+ *
+ * while EOF flush decode, need to set input bitstream as NULL
+ */
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg);
+
+/**
+ * vdec_if_get_param() - get driver's parameter
+ * @handle : [in] video decode handle
+ * @type   : [in] input parameter type
+ * @out    : [out] buffer to store query result
+ */
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out);
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
new file mode 100644
index 0000000..937192c
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_IPI_MSG_H_
+#define _VDEC_IPI_MSG_H_
+
+/**
+ * enum vdec_ipi_msgid - message id between AP and VPU
+ * @AP_IPIMSG_XXX:		AP to VPU cmd message id
+ * @VPU_IPIMSG_XXX_DONE:	VPU ack AP cmd message id
+ */
+enum vdec_ipi_msgid {
+	AP_IPIMSG_DEC_INIT = 0xA000,
+	AP_IPIMSG_DEC_START = 0xA001,
+	AP_IPIMSG_DEC_END = 0xA002,
+	AP_IPIMSG_DEC_DEINIT = 0xA003,
+	AP_IPIMSG_DEC_RESET = 0xA004,
+
+	VPU_IPIMSG_DEC_INIT_ACK = 0xB000,
+	VPU_IPIMSG_DEC_START_ACK = 0xB001,
+	VPU_IPIMSG_DEC_END_ACK = 0xB002,
+	VPU_IPIMSG_DEC_DEINIT_ACK = 0xB003,
+	VPU_IPIMSG_DEC_RESET_ACK = 0xB004,
+};
+
+/**
+ * struct vdec_ap_ipi_cmd - generic AP to VPU ipi command format
+ * @msg_id		: vdec_ipi_msgid
+ * @vpu_inst_addr	: VPU decoder instance address
+ */
+struct vdec_ap_ipi_cmd {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+};
+
+/**
+ * struct vdec_vpu_ipi_ack - generic VPU to AP ipi command format
+ * @msg_id      : vdec_ipi_msgid
+ * @status      : VPU exeuction result
+ * @vdec_inst   : AP video decoder instance address
+ */
+struct vdec_vpu_ipi_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_ap_ipi_init - for AP_IPIMSG_DEC_INIT
+ * @msg_id        : AP_IPIMSG_DEC_INIT
+ * @reserved      : Reserved field
+ * @vdec_inst     : AP video decoder instance address
+ */
+struct vdec_ap_ipi_init {
+	uint32_t msg_id;
+	uint32_t reserved;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
+ * @status        : VPU exeuction result
+ * @vdec_inst     : AP video decoder instance address
+ * @vpu_inst_addr : VPU decoder instance address
+ */
+struct vdec_vpu_ipi_init_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+	uint32_t vpu_inst_addr;
+};
+
+#endif
-- 
1.7.9.5

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-13 12:01       ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add v4l2 layer decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
 11 files changed, 2596 insertions(+), 20 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index dc5cb00..4c8ed2f 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,7 +1,13 @@
 
 
-obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
-
+obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
+				       mtk-vcodec-enc.o \
+				       mtk-vcodec-common.o
+
+mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+		vdec_drv_if.o \
+		mtk_vcodec_dec.o \
+		mtk_vcodec_dec_pm.o \
 
 
 mtk-vcodec-enc-y := venc/venc_vp8_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
new file mode 100644
index 0000000..0499413
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
@@ -0,0 +1,1429 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vcodec_dec_pm.h"
+
+static struct mtk_video_fmt mtk_video_formats[] = {
+	{
+		.fourcc = V4L2_PIX_FMT_H264,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP8,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_MT21,
+		.type = MTK_FMT_FRAME,
+		.num_planes = 2,
+	},
+};
+#define OUT_FMT_IDX	0
+#define CAP_FMT_IDX	3
+
+#define VCODEC_CAPABILITY_4K_DISABLED	0x10
+#define VCODEC_DEC_4K_CODED_WIDTH	4096U
+#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
+
+#define MTK_VDEC_MIN_W	64U
+#define MTK_VDEC_MIN_H	64U
+#define MTK_VDEC_MAX_W	2048U
+#define MTK_VDEC_MAX_H	1088U
+#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
+#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
+
+static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
+	{
+		.fourcc	= V4L2_PIX_FMT_H264,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc	= V4L2_PIX_FMT_VP8,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+};
+
+#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
+#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
+
+
+const struct vb2_ops mtk_vdec_vb2_ops;
+
+static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+	unsigned int k;
+
+	for (k = 0; k < NUM_FORMATS; k++) {
+		fmt = &mtk_video_formats[k];
+		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
+			return fmt;
+	}
+
+	return NULL;
+}
+
+static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
+					      enum v4l2_buf_type type)
+{
+	if (V4L2_TYPE_IS_OUTPUT(type))
+		return &ctx->q_data[MTK_Q_DATA_SRC];
+
+	return &ctx->q_data[MTK_Q_DATA_DST];
+}
+
+static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vdec_fb *disp_frame_buffer;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_DISP_FRAME_BUFFER,
+				   &disp_frame_buffer)) {
+		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
+			     ctx->idx);
+		return NULL;
+	}
+
+	if (disp_frame_buffer == NULL) {
+		mtk_v4l2_debug(3, "No display frame buffer");
+		return NULL;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = disp_frame_buffer;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
+							ctx->picinfo.y_bs_sz);
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
+							ctx->picinfo.c_bs_sz);
+
+		dstbuf->ready_to_display = true;
+		dstbuf->nonrealdisplay = false;
+		mtk_v4l2_debug(2,
+			       "[%d]status=%x queue idx=%d to done_list %d",
+			       ctx->idx, frame_buffer->status,
+			       dstbuf->vb.vb2_buf.index,
+			       dstbuf->queued_in_vb2);
+
+		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
+		ctx->decoded_frame_cnt++;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	void *tmp_frame_addr;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_FREE_FRAME_BUFFER,
+				   &tmp_frame_addr)) {
+		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
+		return NULL;
+	}
+	if (tmp_frame_addr == NULL) {
+		mtk_v4l2_debug(3, " No free frame buffer");
+		return NULL;
+	}
+
+	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
+			 ctx->idx, tmp_frame_addr);
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = tmp_frame_addr;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		if ((dstbuf->queued_in_vb2) &&
+		    (dstbuf->queued_in_v4l2) &&
+		    (frame_buffer->status == FB_ST_FREE)) {
+			mtk_v4l2_debug(2,
+				"[%d]status=%x queue idx=%d to rdy_queue %d",
+				 ctx->idx, frame_buffer->status,
+				 dstbuf->vb.vb2_buf.index,
+				 dstbuf->queued_in_vb2);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+		} else if ((dstbuf->queued_in_vb2 == false) &&
+			   (dstbuf->queued_in_v4l2 == true)) {
+			mtk_v4l2_debug(2,
+					 "[%d]status=%x queue idx=%d to rdy_queue",
+					 ctx->idx, frame_buffer->status,
+					 dstbuf->vb.vb2_buf.index);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+			dstbuf->queued_in_vb2 = true;
+		} else {
+			mtk_v4l2_debug(2,
+					"[%d]status=%x err queue idx=%d %d %d",
+					ctx->idx, frame_buffer->status,
+					dstbuf->vb.vb2_buf.index,
+					dstbuf->queued_in_vb2,
+					dstbuf->queued_in_v4l2);
+		}
+		dstbuf->used = false;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_display_buffer(ctx);
+	} while (framptr);
+}
+
+static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_free_buffer(ctx);
+	} while (framptr);
+}
+
+static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
+{
+	static const struct v4l2_event ev_src_ch = {
+		.type = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes =
+		V4L2_EVENT_SRC_CH_RESOLUTION,
+	};
+
+	mtk_v4l2_debug(1, "[%d]", ctx->idx);
+	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
+
+	return 0;
+}
+
+static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
+{
+	bool res_chg;
+	int ret = 0;
+
+	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
+	if (ret)
+		mtk_v4l2_err("DecodeFinal failed");
+
+	clean_display_buffer(ctx);
+	clean_free_buffer(ctx);
+}
+
+static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
+{
+	int dpbsize;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_PIC_INFO,
+				   &ctx->last_decoded_picinfo)) {
+		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+				ctx->idx);
+		return false;
+	}
+
+	if (ctx->last_decoded_picinfo.pic_w == 0 ||
+		ctx->last_decoded_picinfo.pic_h == 0 ||
+		ctx->last_decoded_picinfo.buf_w == 0 ||
+		ctx->last_decoded_picinfo.buf_h == 0)
+		return false;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
+
+		mtk_v4l2_debug(1,
+				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
+				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
+				 ctx->last_decoded_picinfo.pic_h,
+				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				 ctx->last_decoded_picinfo.buf_w,
+				 ctx->last_decoded_picinfo.buf_h);
+
+		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+		if (dpbsize == 0)
+			dpbsize = 1;
+		ctx->dpb_count = dpbsize;
+		return true;
+	}
+
+	return false;
+}
+
+void mtk_vdec_worker(struct work_struct *work)
+{
+	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
+				    decode_work);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	struct vb2_buffer *src_buf, *dst_buf;
+	struct mtk_vcodec_mem buf;
+	struct vdec_fb *pfb;
+	bool res_chg = false;
+	int ret;
+	struct mtk_video_buf *dst_buf_info, *src_buf_info;
+	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
+
+	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+	if (src_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
+		return;
+	}
+
+	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
+	if (dst_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
+		return;
+	}
+
+	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
+	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
+
+	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
+	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
+
+	buf.va = vb2_plane_vaddr(src_buf, 0);
+	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
+	buf.size = (unsigned int)src_buf->planes[0].bytesused;
+	if (!buf.va) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
+			       ctx->idx, src_buf->index);
+		return;
+	}
+
+	pfb = &dst_buf_info->frame_buffer;
+	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
+	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
+	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
+
+	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
+	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
+	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
+	pfb->status = 0;
+	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
+	mtk_v4l2_debug(3,
+			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
+			 ctx->idx, buf.va,
+			 (u64)buf.dma_addr,
+			 buf.size, src_buf);
+
+	mtk_v4l2_debug(3,
+			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
+			dst_buf->index, pfb,
+			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
+			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
+
+	if (src_buf_info->lastframe == true) {
+		/* update src buf status */
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		src_buf_info->lastframe = false;
+		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
+
+		/* update dst buf status */
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		dst_buf_info->used = false;
+
+		clean_display_buffer(ctx);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
+		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
+		clean_free_buffer(ctx);
+	} else {
+
+		dst_buf_info->vb.timestamp
+					= src_buf_info->vb.timestamp;
+		dst_buf_info->vb.timecode
+					= src_buf_info->vb.timecode;
+		mutex_lock(&dst_buf_info->lock);
+		dst_buf_info->used = true;
+		mutex_unlock(&dst_buf_info->lock);
+		src_buf_info->used = true;
+
+		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
+
+		if (ret) {
+			mtk_v4l2_err(
+				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
+				ctx->idx,
+				src_buf->index,
+				src_buf_info->lastframe,
+				buf.size,
+				src_buf_info->vb.timestamp.tv_sec,
+				src_buf_info->vb.timestamp.tv_usec,
+				dst_buf->index,
+				ret, res_chg);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+					VB2_BUF_STATE_ERROR);
+		} else if (res_chg == false) {
+			/* we only return src buffer with VB2_BUF_STATE_DONE
+			  * when decode success without resolution change
+			  */
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+							VB2_BUF_STATE_DONE);
+		}
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		clean_display_buffer(ctx);
+		clean_free_buffer(ctx);
+
+		if ((ret == 0) && (res_chg == true)) {
+			mtk_vdec_pic_info_update(ctx);
+			/*
+			  * On encountering a resolution change in the stream.
+			  * The driver must first process and decode all
+			  * remaining buffers from before the resolution change
+			  * point, so call flush decode here
+			  */
+			mtk_vdec_flush_decoder(ctx);
+			/*
+			  * After all buffers containing decoded frames from
+			  * before the resolution change point ready to be
+			  * dequeued on the CAPTURE queue, the driver sends a
+			  * V4L2_EVENT_SOURCE_CHANGE event for source change
+			  * type V4L2_EVENT_SRC_CH_RESOLUTION
+			  */
+			mtk_vdec_queue_res_chg_event(ctx);
+		}
+	}
+
+	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+
+}
+
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_unlock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_lock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	int ret = 0;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	src_vq->drv_priv	= ctx;
+	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	src_vq->ops		= &mtk_vdec_vb2_ops;
+	src_vq->mem_ops		= &vb2_dma_contig_memops;
+	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	src_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(src_vq);
+	if (ret) {
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
+		return ret;
+	}
+	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	dst_vq->drv_priv	= ctx;
+	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	dst_vq->ops		= &mtk_vdec_vb2_ops;
+	dst_vq->mem_ops		= &vb2_dma_contig_memops;
+	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	dst_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(dst_vq);
+	if (ret) {
+		vb2_queue_release(src_vq);
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
+{
+	vdec_if_deinit(ctx);
+	ctx->state = MTK_STATE_FREE;
+}
+
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
+{
+	struct mtk_q_data *q_data;
+
+	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
+	ctx->fh.m2m_ctx = ctx->m2m_ctx;
+	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
+	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
+
+	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
+	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
+
+
+	q_data = &ctx->q_data[MTK_Q_DATA_DST];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->coded_width = DFT_CFG_WIDTH;
+	q_data->coded_height = DFT_CFG_HEIGHT;
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+
+	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
+
+	v4l_bound_align_image(&q_data->coded_width,
+					MTK_VDEC_MIN_W,
+					MTK_VDEC_MAX_W, 4,
+					&q_data->coded_height,
+					MTK_VDEC_MIN_H,
+					MTK_VDEC_MAX_H, 5, 6);
+
+	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
+	q_data->bytesperline[0] = q_data->coded_width;
+	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
+	q_data->bytesperline[1] = q_data->coded_width;
+
+}
+
+static int vidioc_vdec_streamon(struct file *file, void *priv,
+				enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+
+	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_streamoff(struct file *file, void *priv,
+				 enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_reqbufs(struct file *file, void *priv,
+			       struct v4l2_requestbuffers *reqbufs)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	int ret;
+
+	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
+			 reqbufs->type, reqbufs->count);
+	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
+
+	return ret;
+}
+
+static int vidioc_vdec_querybuf(struct file *file, void *priv,
+				struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
+			 buf->type, buf->index);
+	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_qbuf(struct file *file, void *priv,
+			    struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct vb2_queue *vq;
+	struct vb2_buffer *vb;
+	struct mtk_video_buf *mtkbuf;
+	struct vb2_v4l2_buffer	*vb2_v4l2;
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
+	vb = vq->bufs[buf->index];
+	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+
+	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    (buf->m.planes[0].bytesused == 0)) {
+		mtkbuf->lastframe = true;
+		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
+			 ctx->idx, buf->type, buf->index,
+			 mtkbuf->lastframe, buf->bytesused,
+			 buf->m.planes[0].bytesused, buf->length,
+			 vb);
+	}
+
+	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_dqbuf(struct file *file, void *priv,
+			     struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_expbuf(struct file *file, void *priv,
+			      struct v4l2_exportbuffer *eb)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
+}
+
+static int vidioc_vdec_querycap(struct file *file, void *priv,
+				struct v4l2_capability *cap)
+{
+	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
+	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
+	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
+	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
+	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+
+	return 0;
+}
+
+static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_event_unsubscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	return 0;
+}
+
+static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
+		mtk_v4l2_err("sizeimage of output format must be given");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vidioc_vdec_g_crop(struct file *file, void *priv,
+			 struct v4l2_crop *cr)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state < MTK_STATE_HEADER)
+		return -EINVAL;
+
+	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
+
+		if (vdec_if_get_param(ctx,
+					   GET_PARAM_CROP_INFO,
+					   cr)) {
+			mtk_v4l2_debug(2,
+					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
+					 ctx->idx);
+			cr->c.left = 0;
+			cr->c.top = 0;
+			cr->c.width = ctx->picinfo.buf_w;
+			cr->c.height = ctx->picinfo.buf_h;
+		}
+		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
+				 cr->c.left, cr->c.top, cr->c.width,
+				 cr->c.height);
+	} else {
+		cr->c.left = 0;
+		cr->c.top = 0;
+		cr->c.width = ctx->picinfo.pic_w;
+		cr->c.height = ctx->picinfo.pic_h;
+		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
+				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
+				 ctx->picinfo.buf_h);
+	}
+	return 0;
+}
+
+static int vidioc_vdec_s_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+	int ret;
+	struct mtk_video_fmt *fmt;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	fmt = mtk_vdec_find_format(f);
+	if (fmt == NULL) {
+		mtk_v4l2_err("mtk_venc_find_format fail");
+		return ret;
+	}
+
+	pix_mp = &f->fmt.pix_mp;
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
+		mtk_v4l2_err("out_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
+		mtk_v4l2_err("cap_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq)
+		return -EINVAL;
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+	if (!q_data)
+		return -EINVAL;
+
+	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		q_data->fmt = fmt;
+		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+		pix_mp->width = 0;
+		pix_mp->height = 0;
+
+		if (ctx->state == MTK_STATE_FREE) {
+			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
+			if (ret) {
+				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
+					       ctx->idx, ret);
+				return -EINVAL;
+			}
+			ctx->state = MTK_STATE_INIT;
+		}
+	} else {
+		q_data->fmt = fmt;
+	}
+
+	return 0;
+}
+
+static int vidioc_enum_framesizes(struct file *file, void *priv,
+				struct v4l2_frmsizeenum *fsize)
+{
+	int i = 0;
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (fsize->index != 0)
+		return -EINVAL;
+
+	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
+		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
+			continue;
+
+		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
+		if (!(ctx->dev->dec_capability &
+				VCODEC_CAPABILITY_4K_DISABLED)) {
+			mtk_v4l2_debug(1, "4K is enabled");
+			fsize->stepwise.max_width =
+					VCODEC_DEC_4K_CODED_WIDTH;
+			fsize->stepwise.max_height =
+					VCODEC_DEC_4K_CODED_HEIGHT;
+		}
+		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
+					ctx->dev->dec_capability,
+					fsize->stepwise.min_width,
+					fsize->stepwise.max_width,
+					fsize->stepwise.step_width,
+					fsize->stepwise.min_height,
+					fsize->stepwise.max_height,
+					fsize->stepwise.step_height);
+		return 0;
+	}
+
+	return -EINVAL;
+
+}
+
+static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
+{
+	struct mtk_video_fmt *fmt;
+	int i, j = 0;
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
+			continue;
+		if (!output_queue &&
+			(mtk_video_formats[i].type != MTK_FMT_FRAME))
+			continue;
+
+		if (j == f->index)
+			break;
+		++j;
+	}
+
+	if (i == NUM_FORMATS)
+		return -EINVAL;
+
+	fmt = &mtk_video_formats[i];
+	f->pixelformat = fmt->fourcc;
+	f->flags = 0;
+	memset(f->reserved, 0, sizeof(f->reserved));
+
+	if (mtk_video_formats[i].type != MTK_FMT_DEC)
+		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
+
+	return 0;
+}
+
+static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, false);
+}
+
+static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, true);
+}
+
+static int vidioc_vdec_g_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq) {
+		mtk_v4l2_err("no vb2 queue for type=%d",
+					   f->type);
+		return -EINVAL;
+	}
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    (ctx->state >= MTK_STATE_HEADER)) {
+		/* previous decode might have resolution change that make
+		 * picinfo changed update context'x picinfo here to make sure
+		 * we know what report to user space
+		 */
+		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
+			sizeof(struct vdec_pic_info));
+		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
+					ctx->picinfo.y_len_sz;
+		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
+					ctx->picinfo.c_len_sz;
+		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
+		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
+		q_data->coded_width = ctx->picinfo.buf_w;
+		q_data->coded_height = ctx->picinfo.buf_h;
+
+		/*
+		 * Width and height are set to the dimensions
+		 * of the movie, the buffer is bigger and
+		 * further processing stages should crop to this
+		 * rectangle.
+		 */
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+
+		/*
+		 * Set pixelformat to the format in which mt vcodec
+		 * outputs the decoded frame
+		 */
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		/*
+		 * This is run on OUTPUT
+		 * The buffer contains compressed image
+		 * so width and height have no meaning.
+		 * Assign value here to pass v4l2-compliance test
+		 */
+		pix_mp->width = q_data->visible_width;
+		pix_mp->height = q_data->visible_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+	} else {
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
+				ctx->idx, ctx->state, f->type);
+		return -EAGAIN;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
+				   const void *parg,
+				   unsigned int *nbuffers,
+				   unsigned int *nplanes,
+				   unsigned int sizes[], void *alloc_ctxs[])
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
+	struct mtk_q_data *q_data;
+
+	q_data = mtk_vdec_get_q_data(ctx, vq->type);
+
+	switch (vq->type) {
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+		*nplanes = 1;
+
+		sizes[0] = q_data->sizeimage[0];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		break;
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+		if (ctx->state < MTK_STATE_HEADER)
+			return -EINVAL;
+
+		/*
+		 * Output plane count is 2 - one for Y and one for CbCr
+		 */
+		*nplanes = 2;
+
+		mtk_v4l2_debug(1,
+				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
+				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
+
+		sizes[0] = q_data->sizeimage[0];
+		sizes[1] = q_data->sizeimage[1];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		alloc_ctxs[1] = ctx->dev->alloc_ctx;
+		break;
+	default:
+		break;
+	}
+
+	mtk_v4l2_debug(1,
+			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
+			ctx->idx, vq->type, *nplanes, *nbuffers,
+			sizes[0], sizes[1]);
+
+	return 0;
+}
+
+static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct mtk_q_data *q_data;
+	int i;
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
+			 ctx->idx, vb->vb2_queue->type, vb->index);
+
+	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
+
+	for (i = 0; i < q_data->fmt->num_planes; i++) {
+		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
+			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
+				       i, vb2_plane_size(vb, i),
+				       q_data->sizeimage[i]);
+		}
+	}
+
+	return 0;
+}
+
+static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
+			ctx->idx, vb->vb2_queue->type,
+			vb->index, vb);
+	/*
+	 * check if this buffer is ready to be used after decode
+	 */
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		struct vb2_buffer *src_buf;
+		struct mtk_vcodec_mem buf;
+		bool res_chg = false;
+		int ret = 0;
+		int dpbsize = 1;
+
+		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
+
+		if (ctx->state == MTK_STATE_INIT) {
+			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+			if (!src_buf) {
+				mtk_v4l2_err("No src buffer");
+				return;
+			}
+
+			buf.va	= vb2_plane_vaddr(src_buf, 0);
+			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
+							src_buf,
+							0);
+			buf.size = (size_t)src_buf->planes[0].bytesused;
+			mtk_v4l2_debug(2,
+					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
+					ctx->idx,
+					src_buf->index,
+					buf.va, (u64)buf.dma_addr, buf.size);
+
+			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
+			if (ret) {
+				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+						VB2_BUF_STATE_DONE);
+				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
+						ctx->idx,
+						src_buf->index,
+						buf.size, ret);
+				return;
+			}
+
+			if (vdec_if_get_param(ctx,
+						   GET_PARAM_PIC_INFO,
+						   &ctx->picinfo)) {
+				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+						ctx->idx);
+				return;
+			}
+
+			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
+				sizeof(struct vdec_pic_info));
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+						ctx->picinfo.y_bs_sz +
+						ctx->picinfo.y_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
+							ctx->picinfo.buf_w;
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
+						ctx->picinfo.c_bs_sz +
+						ctx->picinfo.c_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
+							ctx->picinfo.buf_w;
+			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
+				ctx->idx,
+				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
+				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
+
+			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+			if (dpbsize == 0) {
+				mtk_v4l2_debug(2,
+						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
+						ctx->idx, ret);
+				dpbsize = 1;
+			}
+			ctx->dpb_count = dpbsize;
+			ctx->state = MTK_STATE_HEADER;
+			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
+							ctx->dpb_count);
+		} else {
+			mtk_v4l2_debug(1, "[%d] already init driver %d",
+					 ctx->idx, ctx->state);
+		}
+
+	} else {
+		mutex_lock(&buf->lock);
+		if (buf->used == false) {
+			v4l2_m2m_buf_queue(ctx->m2m_ctx,
+					to_vb2_v4l2_buffer(vb));
+			buf->queued_in_vb2 = true;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		} else {
+			buf->queued_in_vb2 = false;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		}
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
+{
+	if (vb->vb2_queue->type ==
+		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+		mutex_lock(&buf->lock);
+		buf->queued_in_v4l2 = false;
+		buf->queued_in_vb2 = false;
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		buf->used = false;
+		buf->ready_to_display = false;
+		buf->nonrealdisplay = false;
+		buf->queued_in_v4l2 = false;
+		mutex_init(&buf->lock);
+	} else {
+		buf->lastframe = false;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	if (ctx->state == MTK_STATE_FLUSH)
+		ctx->state = MTK_STATE_HEADER;
+
+	return 0;
+}
+
+static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
+{
+	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
+			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
+
+	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		while (src_buf) {
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+					VB2_BUF_STATE_ERROR);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		}
+	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		int i;
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		if (ctx->state >= MTK_STATE_HEADER)
+			mtk_vdec_flush_decoder(ctx);
+
+		ctx->state = MTK_STATE_FLUSH;
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		while (dst_buf) {
+			vb2_set_plane_payload(dst_buf, 0, 0);
+			vb2_set_plane_payload(dst_buf, 1, 0);
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
+				VB2_BUF_STATE_ERROR);
+			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		}
+
+		for (i = 0; i < q->num_buffers; ++i) {
+			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
+				vb2_v4l2 = container_of(q->bufs[i],
+					struct vb2_v4l2_buffer, vb2_buf);
+				buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
+						ctx->idx, i, q->type,
+						buf->queued_in_vb2,
+						buf->queued_in_v4l2,
+						(int)q->bufs[i]->state);
+				v4l2_m2m_buf_done(vb2_v4l2,
+						VB2_BUF_STATE_ERROR);
+			}
+		}
+	}
+}
+
+static void m2mops_vdec_device_run(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	struct mtk_vcodec_dev *dev = ctx->dev;
+
+	queue_work(dev->decode_workqueue, &ctx->decode_work);
+}
+
+static int m2mops_vdec_job_ready(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (ctx->state == MTK_STATE_ABORT)
+		return 0;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
+		return 0;
+
+	if (ctx->state != MTK_STATE_HEADER)
+		return 0;
+
+	return 1;
+}
+
+static void m2mops_vdec_job_abort(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+
+	ctx->state = MTK_STATE_ABORT;
+}
+
+static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
+
+	switch (ctrl->id) {
+	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
+		if (ctx->state >= MTK_STATE_HEADER) {
+			ctrl->val = ctx->dpb_count;
+		} else {
+			mtk_v4l2_err("Seqinfo not ready");
+			ctrl->val = 1;
+			return -EAGAIN;
+		}
+		break;
+	default:
+		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
+	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
+};
+
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
+{
+	struct v4l2_ctrl *ctrl;
+
+	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
+
+	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
+					  &mtk_vcodec_dec_ctrl_ops,
+					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
+					  1, 32, 1, 1);
+	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
+
+	if (ctx->ctrl_hdl.error) {
+		mtk_v4l2_err("Adding control failed %d",
+					ctx->ctrl_hdl.error);
+		return ctx->ctrl_hdl.error;
+	}
+
+	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
+	return 0;
+}
+
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
+{
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+}
+
+static void m2mops_vdec_lock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_lock(&ctx->dev->dev_mutex);
+}
+
+static void m2mops_vdec_unlock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_unlock(&ctx->dev->dev_mutex);
+}
+
+const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
+	.device_run	= m2mops_vdec_device_run,
+	.job_ready	= m2mops_vdec_job_ready,
+	.job_abort	= m2mops_vdec_job_abort,
+	.lock	= m2mops_vdec_lock,
+	.unlock	= m2mops_vdec_unlock,
+};
+
+const struct vb2_ops mtk_vdec_vb2_ops = {
+	.queue_setup	= vb2ops_vdec_queue_setup,
+	.buf_prepare	= vb2ops_vdec_buf_prepare,
+	.buf_queue	= vb2ops_vdec_buf_queue,
+	.wait_prepare	= vb2_ops_wait_prepare,
+	.wait_finish	= vb2_ops_wait_finish,
+	.buf_init	= vb2ops_vdec_buf_init,
+	.buf_finish	= vb2ops_vdec_buf_finish,
+	.start_streaming	= vb2ops_vdec_start_streaming,
+	.stop_streaming	= vb2ops_vdec_stop_streaming,
+};
+
+const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
+	.vidioc_streamon	= vidioc_vdec_streamon,
+	.vidioc_streamoff	= vidioc_vdec_streamoff,
+
+	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
+	.vidioc_querybuf	= vidioc_vdec_querybuf,
+	.vidioc_qbuf	= vidioc_vdec_qbuf,
+	.vidioc_expbuf	= vidioc_vdec_expbuf,
+	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
+
+	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
+	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
+
+	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
+	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
+
+	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
+	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
+	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
+
+	.vidioc_querycap	= vidioc_vdec_querycap,
+	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
+	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
+	.vidioc_g_crop	= vidioc_vdec_g_crop,
+};
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
new file mode 100644
index 0000000..cb5cb42
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
@@ -0,0 +1,81 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_H_
+#define _MTK_VCODEC_DEC_H_
+
+#include <media/videobuf2-core.h>
+#include <media/videobuf2-v4l2.h>
+
+/**
+ * struct vdec_fb  - decoder frame buffer
+ * @base_y	: Y plane memory info
+ * @base_c	: C plane memory info
+ * @status      : frame buffer status (vdec_fb_status)
+ */
+struct vdec_fb {
+	struct mtk_vcodec_mem	base_y;
+	struct mtk_vcodec_mem	base_c;
+	unsigned int	status;
+};
+
+/**
+ * struct mtk_video_buf - Private data related to each VB2 buffer.
+ * @b:			VB2 buffer
+ * @list:			link list
+ * @used:		Output buffer contain decoded frame data
+ * @ready_to_display:	Output buffer not display yet
+ * @nonrealdisplay:	Output buffer is not display frame
+ * @queued_in_vb2:	Output buffer is queue in vb2
+ * @queued_in_v4l2:	Output buffer is in v4l2
+ * @lastframe:		Intput buffer is last buffer - EOS
+ * @frame_buffer:		Decode status of output buffer
+ * @lock:			V4L2 and decode thread should get mutex
+ *			before r/w info in mtk_video_buf
+ */
+struct mtk_video_buf {
+	struct vb2_v4l2_buffer	vb;
+	struct list_head	list;
+
+	bool	used;
+	bool	ready_to_display;
+	bool	nonrealdisplay;
+	bool	queued_in_vb2;
+	bool	queued_in_v4l2;
+	bool	lastframe;
+	struct vdec_fb	frame_buffer;
+	struct mutex	lock;
+};
+
+extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
+extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
+
+
+/*
+ * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
+ * get/release lock before/after access decoder hw.
+ * mtk_vdec_lock get decoder hw lock and set curr_ctx
+ * to ctx instance that get lock
+ */
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq);
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
+
+#endif /* _MTK_VCODEC_DEC_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
new file mode 100644
index 0000000..34e3217
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
@@ -0,0 +1,469 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vpu.h"
+
+module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
+module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
+
+#define VDEC_HW_ACTIVE	0x10
+#define VDEC_IRQ_CFG	0x11
+#define VDEC_IRQ_CLR	0x10
+#define VDEC_IRQ_CFG_REG	0xa4
+
+
+/* Wake up context wait_queue */
+static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
+{
+	ctx->int_cond = 1;
+	ctx->int_type = reason;
+	wake_up_interruptible(&ctx->queue);
+}
+
+static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	unsigned long flags;
+	struct mtk_vcodec_ctx *ctx;
+	unsigned int cg_status = 0;
+	unsigned int dec_done_status = 0;
+
+	spin_lock_irqsave(&dev->irqlock, flags);
+	ctx = dev->curr_ctx;
+	spin_unlock_irqrestore(&dev->irqlock, flags);
+
+	cg_status = readl(dev->reg_base[0] + (0));
+	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
+		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
+			       cg_status);
+		return IRQ_HANDLED;
+	}
+
+	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	ctx->irq_status = dec_done_status;
+	if ((dec_done_status & (0x1 << 16)) != 0x10000)
+		return IRQ_HANDLED;
+
+	/* clear interrupt */
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+
+	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
+
+	mtk_v4l2_debug(3,
+			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
+			ctx->idx, dec_done_status);
+
+
+	return IRQ_HANDLED;
+}
+
+static int fops_vcodec_open(struct file *file)
+{
+	struct video_device *vfd = video_devdata(file);
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = NULL;
+	int ret = 0;
+
+	if (dev->instance_mask == ~0UL) {
+		mtk_v4l2_err("Too many open contexts");
+		ret = -EBUSY;
+		goto err_alloc;
+	}
+
+	mutex_lock(&dev->dev_mutex);
+
+	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	ctx->idx = ffz(dev->instance_mask);
+	v4l2_fh_init(&ctx->fh, video_devdata(file));
+	file->private_data = &ctx->fh;
+	v4l2_fh_add(&ctx->fh);
+	INIT_LIST_HEAD(&ctx->list);
+	ctx->dev = dev;
+	init_waitqueue_head(&ctx->queue);
+
+	if (vfd == dev->vfd_dec) {
+		ctx->type = MTK_INST_DECODER;
+		ret = mtk_vdec_ctrls_setup(ctx);
+		if (ret) {
+			mtk_v4l2_err("Failed to setup mt vcodec controls");
+			goto err_ctrls_setup;
+		}
+		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
+						 &m2mctx_vdec_queue_init);
+		if (IS_ERR(ctx->m2m_ctx)) {
+			ret = PTR_ERR(ctx->m2m_ctx);
+			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
+				       ret);
+			goto err_ctx_init;
+		}
+		mtk_vcodec_dec_set_default_params(ctx);
+	} else {
+		mtk_v4l2_err("Invalid vfd !");
+		ret = -ENOENT;
+		goto err_ctx_init;
+	}
+
+	if (v4l2_fh_is_singular(&ctx->fh)) {
+		mtk_vcodec_dec_pw_on(&dev->pm);
+		ret = vpu_load_firmware(dev->vpu_plat_dev);
+		if (ret < 0) {
+			mtk_v4l2_err("vpu_load_firmware failed!");
+			goto err_load_fw;
+		}
+
+		dev->dec_capability =
+			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
+		mtk_v4l2_debug(0, "decoder capability %x",
+			       dev->dec_capability);
+	}
+
+	set_bit(ctx->idx, &dev->instance_mask);
+	dev->num_instances++;
+	list_add(&ctx->list, &dev->ctx_list);
+	mutex_unlock(&dev->dev_mutex);
+	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
+					ctx->idx);
+
+	return ret;
+
+	/* Deinit when failure occurred */
+err_load_fw:
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+err_ctx_init:
+	mtk_vdec_ctrls_free(ctx);
+err_ctrls_setup:
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+err_alloc:
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_release(struct file *file)
+{
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
+
+	mutex_lock(&dev->dev_mutex);
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+	mtk_vcodec_vdec_release(ctx);
+	mtk_vdec_ctrls_free(ctx);
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	list_del_init(&ctx->list);
+	dev->num_instances--;
+	if (dev->num_instances == 0)
+		mtk_vcodec_dec_pw_off(&dev->pm);
+	clear_bit(ctx->idx, &dev->instance_mask);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+	mutex_unlock(&dev->dev_mutex);
+	return 0;
+}
+
+static unsigned int fops_vcodec_poll(struct file *file,
+				     struct poll_table_struct *wait)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	int ret;
+
+	mutex_lock(&dev->dev_mutex);
+	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
+}
+
+static const struct v4l2_file_operations mtk_vcodec_fops = {
+	.owner				= THIS_MODULE,
+	.open				= fops_vcodec_open,
+	.release			= fops_vcodec_release,
+	.poll				= fops_vcodec_poll,
+	.unlocked_ioctl			= video_ioctl2,
+	.mmap				= fops_vcodec_mmap,
+};
+
+static void mtk_vcodec_dec_reset_handler(void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	struct mtk_vcodec_ctx *ctx;
+
+	mtk_v4l2_debug(0, "Watchdog timeout!!");
+
+	mutex_lock(&dev->dev_mutex);
+	list_for_each_entry(ctx, &dev->ctx_list, list) {
+		ctx->state = MTK_STATE_ABORT;
+		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
+					   ctx->idx);
+	}
+	mutex_unlock(&dev->dev_mutex);
+}
+
+static int mtk_vcodec_probe(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev;
+	struct video_device *vfd_dec;
+	struct resource *res;
+	int i, ret;
+	int reg_base_num;
+
+	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&dev->ctx_list);
+	dev->plat_dev = pdev;
+	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
+	if (dev->vpu_plat_dev == NULL) {
+		mtk_v4l2_err("[VPU] vpu device in not ready");
+		return -EPROBE_DEFER;
+	}
+
+	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
+				dev, VPU_RST_DEC);
+
+	ret = mtk_vcodec_init_dec_pm(dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
+		return ret;
+	}
+
+	reg_base_num = NUM_MAX_VDEC_REG_BASE;
+	for (i = 0; i < reg_base_num; i++) {
+		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
+		if (res == NULL) {
+			dev_err(&pdev->dev, "get memory resource failed.");
+			ret = -ENXIO;
+			goto err_res;
+		}
+		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
+		if (IS_ERR(dev->reg_base[i])) {
+			dev_err(&pdev->dev,
+				"devm_ioremap_resource %d failed.", i);
+			ret = PTR_ERR(dev->reg_base);
+			goto err_res;
+		}
+		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "failed to get irq resource");
+		ret = -ENOENT;
+		goto err_res;
+	}
+
+	dev->dec_irq = platform_get_irq(pdev, 0);
+	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
+			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
+			dev->dec_irq,
+			ret);
+		ret = -EINVAL;
+		goto err_res;
+	}
+
+	disable_irq(dev->dec_irq);
+	mutex_init(&dev->dev_mutex);
+	mutex_init(&dev->dec_mutex);
+	spin_lock_init(&dev->irqlock);
+
+	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
+		 "[MTK_V4L2]");
+
+	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
+	if (ret) {
+		mtk_v4l2_err("v4l2_device_register err=%d", ret);
+		return ret;
+	}
+
+	init_waitqueue_head(&dev->queue);
+
+	vfd_dec = video_device_alloc();
+	if (!vfd_dec) {
+		mtk_v4l2_err("Failed to allocate video device");
+		ret = -ENOMEM;
+		goto err_dec_alloc;
+	}
+	vfd_dec->fops		= &mtk_vcodec_fops;
+	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
+	vfd_dec->release	= video_device_release;
+	vfd_dec->lock		= &dev->dev_mutex;
+	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
+	vfd_dec->vfl_dir	= VFL_DIR_M2M;
+
+	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
+		 MTK_VCODEC_DEC_NAME);
+	video_set_drvdata(vfd_dec, dev);
+	dev->vfd_dec = vfd_dec;
+	platform_set_drvdata(pdev, dev);
+
+	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
+	if (IS_ERR(dev->alloc_ctx)) {
+		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
+		ret = PTR_ERR(dev->alloc_ctx);
+		goto err_vb2_ctx_init;
+	}
+
+	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
+	if (IS_ERR(dev->m2m_dev_dec)) {
+		mtk_v4l2_err("Failed to init mem2mem dec device");
+		ret = PTR_ERR(dev->m2m_dev_dec);
+		goto err_dec_mem_init;
+	}
+
+	dev->decode_workqueue =
+			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
+			WQ_MEM_RECLAIM | WQ_FREEZABLE);
+	if (!dev->decode_workqueue) {
+		mtk_v4l2_err("Failed to create decode workqueue");
+		ret = -EINVAL;
+		goto err_event_workq;
+	}
+
+	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
+	if (ret) {
+		mtk_v4l2_err("Failed to register video device");
+		goto err_dec_reg;
+	}
+	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
+			 vfd_dec->num);
+
+	return 0;
+
+err_dec_reg:
+	destroy_workqueue(dev->decode_workqueue);
+err_event_workq:
+	v4l2_m2m_release(dev->m2m_dev_dec);
+err_dec_mem_init:
+	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+err_vb2_ctx_init:
+	video_unregister_device(vfd_dec);
+err_dec_alloc:
+	v4l2_device_unregister(&dev->v4l2_dev);
+err_res:
+	mtk_vcodec_release_dec_pm(dev);
+	return ret;
+}
+
+static const struct of_device_id mtk_vcodec_match[] = {
+	{.compatible = "mediatek,mt8173-vcodec-dec",},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
+
+static int mtk_vcodec_dec_remove(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug_enter();
+	flush_workqueue(dev->decode_workqueue);
+	destroy_workqueue(dev->decode_workqueue);
+	if (dev->m2m_dev_dec)
+		v4l2_m2m_release(dev->m2m_dev_dec);
+	if (dev->alloc_ctx)
+		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+
+	if (dev->vfd_dec)
+		video_unregister_device(dev->vfd_dec);
+
+	v4l2_device_unregister(&dev->v4l2_dev);
+	mtk_vcodec_release_dec_pm(dev);
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
+	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
+			   mtk_vcodec_vdec_pm_runtime_resume,
+			   NULL)
+};
+
+static struct platform_driver mtk_vcodec_dec_driver = {
+	.probe	= mtk_vcodec_probe,
+	.remove	= mtk_vcodec_dec_remove,
+	.driver	= {
+		.name	= MTK_VCODEC_DEC_NAME,
+		.owner	= THIS_MODULE,
+		.of_match_table = mtk_vcodec_match,
+		.pm = &mtk_vcodec_vdec_pm_ops,
+	},
+};
+
+module_platform_driver(mtk_vcodec_dec_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
new file mode 100644
index 0000000..aa1701f
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
@@ -0,0 +1,153 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/clk.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <soc/mediatek/smi.h>
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
+{
+	struct device_node *node;
+	struct platform_device *pdev;
+	struct device *dev;
+	struct mtk_vcodec_pm *pm;
+	int ret = 0;
+
+	pdev = mtkdev->plat_dev;
+	pm = &mtkdev->pm;
+	pm->mtkdev = mtkdev;
+	dev = &pdev->dev;
+	node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
+	if (!node)
+		return -1;
+
+	pdev = of_find_device_by_node(node);
+	if (WARN_ON(!pdev)) {
+		of_node_put(node);
+		return -1;
+	}
+	pm->larbvdec = &pdev->dev;
+
+	pdev = mtkdev->plat_dev;
+	pm_runtime_enable(&pdev->dev);
+	pm->dev = &pdev->dev;
+
+	pm->vcodecpll = devm_clk_get(&pdev->dev, "vcodecpll");
+	if (pm->vcodecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vcodecpll fail");
+		ret = -1;
+	}
+
+	pm->univpll_d2 = devm_clk_get(&pdev->dev, "univpll_d2");
+	if (pm->univpll_d2 == NULL) {
+		mtk_v4l2_err("devm_clk_get univpll_d2 fail");
+		ret = -1;
+	}
+
+	pm->clk_cci400_sel = devm_clk_get(&pdev->dev, "clk_cci400_sel");
+	if (pm->clk_cci400_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get clk_cci400_sel fail");
+		ret = -1;
+	}
+
+	pm->vdec_sel = devm_clk_get(&pdev->dev, "vdec_sel");
+	if (pm->vdec_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get vdec_sel fail");
+		ret = -1;
+	}
+
+	pm->vdecpll = devm_clk_get(&pdev->dev, "vdecpll");
+	if (pm->vdecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vdecpll fail");
+		ret = -1;
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
+{
+	pm_runtime_disable(dev->pm.dev);
+}
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_get_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_get_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_put_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_put_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	clk_set_rate(pm->vcodecpll, 1482 * 1000000);
+
+	ret = clk_prepare_enable(pm->vcodecpll);
+	if (ret)
+		mtk_v4l2_err("vcodecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("univpll_d2 fail %d", ret);
+
+	ret = clk_prepare_enable(pm->clk_cci400_sel);
+	if (ret)
+		mtk_v4l2_err("clk_cci400_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->clk_cci400_sel, pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("vdecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdec_sel);
+	if (ret)
+		mtk_v4l2_err("vdec_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->vdec_sel, pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = mtk_smi_larb_get(pm->larbvdec);
+	if (ret)
+		mtk_v4l2_err("mtk_smi_larb_get larbvdec fail %d", ret);
+
+}
+
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm)
+{
+	mtk_smi_larb_put(pm->larbvdec);
+	clk_disable_unprepare(pm->vdec_sel);
+	clk_disable_unprepare(pm->vdecpll);
+}
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
new file mode 100644
index 0000000..28de4c1
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
@@ -0,0 +1,28 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_PM_H_
+#define _MTK_VCODEC_DEC_PM_H_
+
+#include "mtk_vcodec_drv.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm);
+
+#endif /* _MTK_VCODEC_DEC_PM_H_ */
\ No newline at end of file
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
index 55cf462..e3e3d66 100644
--- a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
@@ -26,10 +26,10 @@
 #include "mtk_vcodec_util.h"
 
 #define MTK_VCODEC_DRV_NAME	"mtk_vcodec_drv"
+#define MTK_VCODEC_DEC_NAME	"mtk-vcodec-dec" 
 #define MTK_VCODEC_ENC_NAME	"mtk-vcodec-enc"
 #define MTK_PLATFORM_STR	"platform:mt8173"
 
-
 #define MTK_VCODEC_MAX_PLANES	3
 #define MTK_V4L2_BENCHMARK	0
 #define WAIT_INTR_TIMEOUT_MS	1000
@@ -145,18 +145,18 @@ struct mtk_q_data {
 /**
  * struct mtk_enc_params - General encoding parameters
  * @bitrate: target bitrate in bits per second
- * @num_b_frame:	number of b frames between p-frame
- * @rc_frame:	frame based rate control
+ * @num_b_frame: number of b frames between p-frame
+ * @rc_frame: frame based rate control
  * @rc_mb: macroblock based rate control
  * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
- *				  with the first frame
+ *		  with the first frame
  * @intra_period: I frame period
- * @gop_size:	group of picture size, it's used as the intra frame period
- * @framerate_num: frame rate numerator. ex: framerate_num=30 and framerate_denom=1
- *				   menas FPS is 30
- * @framerate_denom:	frame rate denominator. ex: framerate_num=30 and
- *				       framerate_denom=1 menas FPS is 30
- * @h264_max_qp:	Max value for H.264 quantization parameter
+ * @gop_size: group of picture size, it's used as the intra frame period
+ * @framerate_num: frame rate numerator. ex: framerate_num=30 and
+ *		   framerate_denom=1 menas FPS is 30
+ * @framerate_denom: frame rate denominator. ex: framerate_num=30
+ *		     and framerate_denom=1 menas FPS is 30
+ * @h264_max_qp: Max value for H.264 quantization parameter
  * @h264_profile: V4L2 defined H.264 profile
  * @h264_level: V4L2 defined H.264 level
  * @force_intra: force/insert intra frame
@@ -197,33 +197,61 @@ struct mtk_vcodec_pm {
 	struct mtk_vcodec_dev	*mtkdev;
 };
 
+/**
+ * struct vdec_pic_info  - picture size information
+ * @pic_w: picture width
+ * @pic_h: picture height
+ * @buf_w: picture buffer width (16 aligned up from pic_w)
+ * @buf_h: picture buffer heiht (32 aligned up from pic_h)
+ * @y_bs_sz: Y bitstream size
+ * @c_bs_sz: CbCr bitstream size
+ * @y_len_sz: Y length size
+ * @c_len_sz: CbCr length size
+ * E.g. suppose picture size is 176x144,
+ *      buffer size will be aligned to 176x160.
+ */
+struct vdec_pic_info {
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int y_bs_sz;
+	unsigned int c_bs_sz;
+	unsigned int y_len_sz;
+	unsigned int c_len_sz;
+};
 
 /**
  * struct mtk_vcodec_ctx - Context (instance) private data.
  *
  * @type: type of the instance - decoder or encoder
- * @dev:	pointer to the mtk_vcodec_dev of the device
+ * @dev: pointer to the mtk_vcodec_dev of the device
  * @list: link to ctx_list of mtk_vcodec_dev
  * @fh: struct v4l2_fh
  * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
  * @q_data: store information of input and output queue
- *		    of the context
+ *		of the context
  * @idx: index of the context that this structure describes
  * @state: state of the context
  * @param_change: indicate encode parameter type
  * @enc_params: encoding parameters
- * @enc_if: hoooked encoder driver interface
+ * @dec_if: hooked decoder driver interface
+ * @enc_if: hooked encoder driver interface
  * @drv_handle: driver handle for specific decode/encode instance
  *
+ * @picinfo: store picture info after header parsing
+ * @dpb_count: store dpb count after header parsing
  * @int_cond: variable used by the waitqueue
  * @int_type: type of the last interrupt
  * @queue: waitqueue that can be used to wait for this context to
- *		   finish
+ *	finish
  * @irq_status: irq status
  *
  * @ctrl_hdl: handler for v4l2 framework
  *
+ * @decode_work: worker for the decoding
  * @encode_work: worker for the encoding
+ * @last_decoded_picinfo: pic information get from latest decode
  */
 struct mtk_vcodec_ctx {
 	enum mtk_instance_type type;
@@ -238,17 +266,24 @@ struct mtk_vcodec_ctx {
 	enum mtk_encode_param param_change;
 	struct mtk_enc_params enc_params;
 
+	struct vdec_common_if *dec_if;
 	struct venc_common_if *enc_if;
 	unsigned long drv_handle;
 
+	struct vdec_pic_info picinfo;
+	int dpb_count;
+
 	int int_cond;
 	int int_type;
 	wait_queue_head_t queue;
 	unsigned int irq_status;
 
 	struct v4l2_ctrl_handler ctrl_hdl;
+	struct work_struct decode_work;
 	struct work_struct encode_work;
+	struct vdec_pic_info last_decoded_picinfo;
 
+	int decoded_frame_cnt;
 #if MTK_V4L2_BENCHMARK
 	unsigned int total_enc_dec_cnt;
 	unsigned int total_enc_dec_time;
@@ -289,36 +324,61 @@ struct mtk_vcodec_ctx {
  *
  * @encode_workqueue: encode work queue
  *
+ * @v4l2_dev: V4L2 device to register video devices for.
+ * @vfd_dec: Video device for decoder
+ * @vfd_enc: Video device for encoder
+ *
+ * @m2m_dev_dec: m2m device for decoder
+ * @m2m_dev_enc: m2m device for encoder
+ * @plat_dev: platform device
+ * @vpu_plat_dev: mtk vpu platform device
+ * @alloc_ctx:VB2 allocator context
+ *	      (for allocations without kernel mapping).
+ * @ctx_list: list of struct mtk_vcodec_ctx
+ * @irqlock: protect data access by irq handler and work thread
+ * @curr_ctx: The context that is waiting for codec hardware
+ * @reg_base: Mapped address of MTK Vcodec registers.
+ *
+ * @instance_mask: used to mark which index id are used
+ * @num_instances: counter of active MTK Vcodec instances
+ *
+ * @decode_workqueue: decode work queue
+ * @encode_workqueue: encode work queue
  * @int_cond: used to identify interrupt condition happen
  * @int_type: used to identify what kind of interrupt condition happen
  * @dev_mutex: video_device lock
  * @queue: waitqueue for waiting for completion of device commands
  *
+ * @dec_irq: decoder irq resource
  * @enc_irq: h264 encoder irq resource
- * @enc_lt_irq: vp8 encoder irq resource
+ * @enc_lt_irq: vp8 encoder lt irq resource
  *
- * @enc_mutex: encoder hardware lock.
+ * @dec_mutex: decoder hardware lock
+ * @enc_mutex: encoder hardware lock
  *
  * @pm: power management control
  * @dec_capability: used to identify decode capability, ex: 4k
- * @enc_capability:	 used to identify encode capability
+ * @enc_capability: used to identify encode capability
  */
 struct mtk_vcodec_dev {
 	struct v4l2_device	v4l2_dev;
+	struct video_device	*vfd_dec;
 	struct video_device	*vfd_enc;
 
+	struct v4l2_m2m_dev	*m2m_dev_dec;
 	struct v4l2_m2m_dev	*m2m_dev_enc;
 	struct platform_device	*plat_dev;
 	struct platform_device	*vpu_plat_dev;
 	struct vb2_alloc_ctx	*alloc_ctx;
 	struct list_head	ctx_list;
 	spinlock_t	irqlock;
-	struct mtk_vcodec_ctx *curr_ctx;
+	struct mtk_vcodec_ctx	*curr_ctx;
 	void __iomem	*reg_base[NUM_MAX_VCODEC_REG_BASE];
 
 	unsigned long	instance_mask;
 	int	num_instances;
 
+	struct workqueue_struct *decode_workqueue;
 	struct workqueue_struct *encode_workqueue;
 
 	int	int_cond;
@@ -326,9 +386,11 @@ struct mtk_vcodec_dev {
 	struct mutex	dev_mutex;
 	wait_queue_head_t	queue;
 
+	int	dec_irq;
 	int	enc_irq;
 	int	enc_lt_irq;
 
+	struct mutex	dec_mutex;
 	struct mutex	enc_mutex;
 
 	struct mtk_vcodec_pm	pm;
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_base.h b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
new file mode 100644
index 0000000..73ea1fe
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_BASE_
+#define _VDEC_DRV_BASE_
+
+#include "mtk_vcodec_drv.h"
+
+#include "vdec_drv_if.h"
+
+struct vdec_common_if {
+	/**
+	 * (*init)() - initialize decode driver
+	 * @ctx     : [in] mtk v4l2 context
+	 * @h_vdec  : [out] driver handle
+	 */
+	int (*init)(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec);
+
+	/**
+	 * (*decode)() - trigger decode
+	 * @h_vdec  : [in] driver handle
+	 * @bs      : [in] input bitstream
+	 * @fb      : [in] frame buffer to store decoded frame
+	 * @res_chg : [out] resolution change happen
+	 */
+	int (*decode)(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		      struct vdec_fb *fb, bool *res_chg);
+
+	/**
+	 * (*get_param)() - get driver's parameter
+	 * @h_vdec : [in] driver handle
+	 * @type   : [in] input parameter type
+	 * @out    : [out] buffer to store query result
+	 */
+	int (*get_param)(unsigned long h_vdec, enum vdec_get_param_type type,
+			 void *out);
+
+	/**
+	 * (*deinit)() - deinitialize driver.
+	 * @h_vdec : [in] driver handle to be deinit
+	 */
+	int (*deinit)(unsigned long h_vdec);
+};
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
new file mode 100644
index 0000000..1d12719
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *         Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+
+#include "vdec_drv_base.h"
+#include "vdec_drv_if.h"
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
+{
+	int ret = 0;
+
+	switch (fourcc) {
+	case V4L2_PIX_FMT_H264:
+	case V4L2_PIX_FMT_VP8:
+	case V4L2_PIX_FMT_VP9:
+	default:
+		return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->init(ctx, &ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	unsigned long flags;
+	int ret = 0;
+
+	if (bs) {
+		if ((bs->dma_addr & 63) != 0)
+			return -EINVAL;
+	}
+
+	if (fb) {
+		if (((fb->base_y.dma_addr & 511) != 0) ||
+		    ((fb->base_c.dma_addr & 511) != 0))
+			return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = ctx;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	enable_irq(ctx->dev->dec_irq);
+	ret = ctx->dec_if->decode(ctx->drv_handle, bs, fb, res_chg);
+	disable_irq(ctx->dev->dec_irq);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = NULL;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out)
+{
+	int ret = 0;
+
+	mtk_vdec_lock(ctx);
+	ret = ctx->dec_if->get_param(ctx->drv_handle, type, out);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx)
+{
+	int ret = 0;
+
+	if (ctx->drv_handle == 0)
+		return 0;
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->deinit(ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	ctx->drv_handle = 0;
+	return ret;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.h b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
new file mode 100644
index 0000000..9717446
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *		   Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_IF_H_
+#define _VDEC_DRV_IF_H_
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_util.h"
+
+/**
+ * struct vdec_fb_status  - decoder frame buffer status
+ * @FB_ST_NORMAL	: initial state
+ * @FB_ST_DISPLAY	: frmae buffer is ready to be displayed
+ * @FB_ST_FREE		: frame buffer is not used by decoder any more
+ */
+enum vdec_fb_status {
+	FB_ST_NORMAL		= 0,
+	FB_ST_DISPLAY		= (1 << 0),
+	FB_ST_FREE		= (1 << 1)
+};
+
+enum vdec_get_param_type {
+	GET_PARAM_DISP_FRAME_BUFFER,
+	GET_PARAM_FREE_FRAME_BUFFER,
+	GET_PARAM_PIC_INFO,
+	GET_PARAM_CROP_INFO,
+	GET_PARAM_DPB_SIZE
+};
+
+/**
+ * struct vdec_fb_node  - decoder frame buffer node
+ * @list	: list to hold this node
+ * @fb		: point to frame buffer (vdec_fb)
+ */
+struct vdec_fb_node {
+	struct list_head list;
+	void *fb;
+};
+
+/**
+ * For the same vdec_handle, these functions below are not thread safe.
+ * For different vdec_handle, these functions can be called at the same time.
+ */
+
+/**
+ * vdec_if_init() - initialize decode driver
+ * @ctx      : [in] v4l2 context
+ * @fourcc   : [in] video format fourcc, V4L2_PIX_FMT_H264/VP8/VP9..
+ */
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc);
+
+/**
+ * vdec_if_deinit() - deinitialize decode driver
+ * @ctx : [in] v4l2 context
+ *
+ */
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx);
+
+/**
+ * vdec_if_decode() - trigger decode
+ * @handle  : [in] video decode handle
+ * @bs      : [in] input bitstream
+ * @fb      : [in] frame buffer to store decoded frame
+ * @res_chg : [out] resolution change happen
+ *
+ * while EOF flush decode, need to set input bitstream as NULL
+ */
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg);
+
+/**
+ * vdec_if_get_param() - get driver's parameter
+ * @handle : [in] video decode handle
+ * @type   : [in] input parameter type
+ * @out    : [out] buffer to store query result
+ */
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out);
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
new file mode 100644
index 0000000..937192c
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_IPI_MSG_H_
+#define _VDEC_IPI_MSG_H_
+
+/**
+ * enum vdec_ipi_msgid - message id between AP and VPU
+ * @AP_IPIMSG_XXX:		AP to VPU cmd message id
+ * @VPU_IPIMSG_XXX_DONE:	VPU ack AP cmd message id
+ */
+enum vdec_ipi_msgid {
+	AP_IPIMSG_DEC_INIT = 0xA000,
+	AP_IPIMSG_DEC_START = 0xA001,
+	AP_IPIMSG_DEC_END = 0xA002,
+	AP_IPIMSG_DEC_DEINIT = 0xA003,
+	AP_IPIMSG_DEC_RESET = 0xA004,
+
+	VPU_IPIMSG_DEC_INIT_ACK = 0xB000,
+	VPU_IPIMSG_DEC_START_ACK = 0xB001,
+	VPU_IPIMSG_DEC_END_ACK = 0xB002,
+	VPU_IPIMSG_DEC_DEINIT_ACK = 0xB003,
+	VPU_IPIMSG_DEC_RESET_ACK = 0xB004,
+};
+
+/**
+ * struct vdec_ap_ipi_cmd - generic AP to VPU ipi command format
+ * @msg_id		: vdec_ipi_msgid
+ * @vpu_inst_addr	: VPU decoder instance address
+ */
+struct vdec_ap_ipi_cmd {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+};
+
+/**
+ * struct vdec_vpu_ipi_ack - generic VPU to AP ipi command format
+ * @msg_id      : vdec_ipi_msgid
+ * @status      : VPU exeuction result
+ * @vdec_inst   : AP video decoder instance address
+ */
+struct vdec_vpu_ipi_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_ap_ipi_init - for AP_IPIMSG_DEC_INIT
+ * @msg_id        : AP_IPIMSG_DEC_INIT
+ * @reserved      : Reserved field
+ * @vdec_inst     : AP video decoder instance address
+ */
+struct vdec_ap_ipi_init {
+	uint32_t msg_id;
+	uint32_t reserved;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
+ * @status        : VPU exeuction result
+ * @vdec_inst     : AP video decoder instance address
+ * @vpu_inst_addr : VPU decoder instance address
+ */
+struct vdec_vpu_ipi_init_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+	uint32_t vpu_inst_addr;
+};
+
+#endif
-- 
1.7.9.5

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-13 12:01       ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add v4l2 layer decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
 .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
 drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
 drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
 11 files changed, 2596 insertions(+), 20 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
 create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index dc5cb00..4c8ed2f 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,7 +1,13 @@
 
 
-obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
-
+obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
+				       mtk-vcodec-enc.o \
+				       mtk-vcodec-common.o
+
+mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+		vdec_drv_if.o \
+		mtk_vcodec_dec.o \
+		mtk_vcodec_dec_pm.o \
 
 
 mtk-vcodec-enc-y := venc/venc_vp8_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
new file mode 100644
index 0000000..0499413
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
@@ -0,0 +1,1429 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vcodec_dec_pm.h"
+
+static struct mtk_video_fmt mtk_video_formats[] = {
+	{
+		.fourcc = V4L2_PIX_FMT_H264,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP8,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.type = MTK_FMT_DEC,
+		.num_planes	= 1,
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_MT21,
+		.type = MTK_FMT_FRAME,
+		.num_planes = 2,
+	},
+};
+#define OUT_FMT_IDX	0
+#define CAP_FMT_IDX	3
+
+#define VCODEC_CAPABILITY_4K_DISABLED	0x10
+#define VCODEC_DEC_4K_CODED_WIDTH	4096U
+#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
+
+#define MTK_VDEC_MIN_W	64U
+#define MTK_VDEC_MIN_H	64U
+#define MTK_VDEC_MAX_W	2048U
+#define MTK_VDEC_MAX_H	1088U
+#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
+#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
+
+static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
+	{
+		.fourcc	= V4L2_PIX_FMT_H264,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc	= V4L2_PIX_FMT_VP8,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+	{
+		.fourcc = V4L2_PIX_FMT_VP9,
+		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
+					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
+	},
+};
+
+#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
+#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
+
+
+const struct vb2_ops mtk_vdec_vb2_ops;
+
+static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+	unsigned int k;
+
+	for (k = 0; k < NUM_FORMATS; k++) {
+		fmt = &mtk_video_formats[k];
+		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
+			return fmt;
+	}
+
+	return NULL;
+}
+
+static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
+					      enum v4l2_buf_type type)
+{
+	if (V4L2_TYPE_IS_OUTPUT(type))
+		return &ctx->q_data[MTK_Q_DATA_SRC];
+
+	return &ctx->q_data[MTK_Q_DATA_DST];
+}
+
+static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vdec_fb *disp_frame_buffer;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_DISP_FRAME_BUFFER,
+				   &disp_frame_buffer)) {
+		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
+			     ctx->idx);
+		return NULL;
+	}
+
+	if (disp_frame_buffer == NULL) {
+		mtk_v4l2_debug(3, "No display frame buffer");
+		return NULL;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = disp_frame_buffer;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
+							ctx->picinfo.y_bs_sz);
+		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
+							ctx->picinfo.c_bs_sz);
+
+		dstbuf->ready_to_display = true;
+		dstbuf->nonrealdisplay = false;
+		mtk_v4l2_debug(2,
+			       "[%d]status=%x queue idx=%d to done_list %d",
+			       ctx->idx, frame_buffer->status,
+			       dstbuf->vb.vb2_buf.index,
+			       dstbuf->queued_in_vb2);
+
+		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
+		ctx->decoded_frame_cnt++;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	void *tmp_frame_addr;
+	struct vb2_queue *vq;
+	struct mtk_video_buf *dstbuf;
+	struct vdec_fb *frame_buffer;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_FREE_FRAME_BUFFER,
+				   &tmp_frame_addr)) {
+		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
+		return NULL;
+	}
+	if (tmp_frame_addr == NULL) {
+		mtk_v4l2_debug(3, " No free frame buffer");
+		return NULL;
+	}
+
+	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
+			 ctx->idx, tmp_frame_addr);
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (!vq) {
+		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
+		return NULL;
+	}
+
+	frame_buffer = tmp_frame_addr;
+	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
+
+	mutex_lock(&dstbuf->lock);
+	if (dstbuf->used) {
+		if ((dstbuf->queued_in_vb2) &&
+		    (dstbuf->queued_in_v4l2) &&
+		    (frame_buffer->status == FB_ST_FREE)) {
+			mtk_v4l2_debug(2,
+				"[%d]status=%x queue idx=%d to rdy_queue %d",
+				 ctx->idx, frame_buffer->status,
+				 dstbuf->vb.vb2_buf.index,
+				 dstbuf->queued_in_vb2);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+		} else if ((dstbuf->queued_in_vb2 == false) &&
+			   (dstbuf->queued_in_v4l2 == true)) {
+			mtk_v4l2_debug(2,
+					 "[%d]status=%x queue idx=%d to rdy_queue",
+					 ctx->idx, frame_buffer->status,
+					 dstbuf->vb.vb2_buf.index);
+			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
+			dstbuf->queued_in_vb2 = true;
+		} else {
+			mtk_v4l2_debug(2,
+					"[%d]status=%x err queue idx=%d %d %d",
+					ctx->idx, frame_buffer->status,
+					dstbuf->vb.vb2_buf.index,
+					dstbuf->queued_in_vb2,
+					dstbuf->queued_in_v4l2);
+		}
+		dstbuf->used = false;
+	}
+	mutex_unlock(&dstbuf->lock);
+	return &dstbuf->vb.vb2_buf;
+}
+
+static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_display_buffer(ctx);
+	} while (framptr);
+}
+
+static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
+{
+	struct vb2_buffer *framptr;
+
+	do {
+		framptr = get_free_buffer(ctx);
+	} while (framptr);
+}
+
+static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
+{
+	static const struct v4l2_event ev_src_ch = {
+		.type = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes =
+		V4L2_EVENT_SRC_CH_RESOLUTION,
+	};
+
+	mtk_v4l2_debug(1, "[%d]", ctx->idx);
+	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
+
+	return 0;
+}
+
+static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
+{
+	bool res_chg;
+	int ret = 0;
+
+	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
+	if (ret)
+		mtk_v4l2_err("DecodeFinal failed");
+
+	clean_display_buffer(ctx);
+	clean_free_buffer(ctx);
+}
+
+static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
+{
+	int dpbsize;
+
+	if (vdec_if_get_param(ctx,
+				   GET_PARAM_PIC_INFO,
+				   &ctx->last_decoded_picinfo)) {
+		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+				ctx->idx);
+		return false;
+	}
+
+	if (ctx->last_decoded_picinfo.pic_w == 0 ||
+		ctx->last_decoded_picinfo.pic_h == 0 ||
+		ctx->last_decoded_picinfo.buf_w == 0 ||
+		ctx->last_decoded_picinfo.buf_h == 0)
+		return false;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
+
+		mtk_v4l2_debug(1,
+				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
+				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
+				 ctx->last_decoded_picinfo.pic_h,
+				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				 ctx->last_decoded_picinfo.buf_w,
+				 ctx->last_decoded_picinfo.buf_h);
+
+		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+		if (dpbsize == 0)
+			dpbsize = 1;
+		ctx->dpb_count = dpbsize;
+		return true;
+	}
+
+	return false;
+}
+
+void mtk_vdec_worker(struct work_struct *work)
+{
+	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
+				    decode_work);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	struct vb2_buffer *src_buf, *dst_buf;
+	struct mtk_vcodec_mem buf;
+	struct vdec_fb *pfb;
+	bool res_chg = false;
+	int ret;
+	struct mtk_video_buf *dst_buf_info, *src_buf_info;
+	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
+
+	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+	if (src_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
+		return;
+	}
+
+	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
+	if (dst_buf == NULL) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
+		return;
+	}
+
+	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
+	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
+
+	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
+	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
+
+	buf.va = vb2_plane_vaddr(src_buf, 0);
+	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
+	buf.size = (unsigned int)src_buf->planes[0].bytesused;
+	if (!buf.va) {
+		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
+			       ctx->idx, src_buf->index);
+		return;
+	}
+
+	pfb = &dst_buf_info->frame_buffer;
+	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
+	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
+	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
+
+	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
+	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
+	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
+	pfb->status = 0;
+	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
+	mtk_v4l2_debug(3,
+			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
+			 ctx->idx, buf.va,
+			 (u64)buf.dma_addr,
+			 buf.size, src_buf);
+
+	mtk_v4l2_debug(3,
+			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
+			dst_buf->index, pfb,
+			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
+			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
+
+	if (src_buf_info->lastframe == true) {
+		/* update src buf status */
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		src_buf_info->lastframe = false;
+		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
+
+		/* update dst buf status */
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		dst_buf_info->used = false;
+
+		clean_display_buffer(ctx);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
+		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
+		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
+		clean_free_buffer(ctx);
+	} else {
+
+		dst_buf_info->vb.timestamp
+					= src_buf_info->vb.timestamp;
+		dst_buf_info->vb.timecode
+					= src_buf_info->vb.timecode;
+		mutex_lock(&dst_buf_info->lock);
+		dst_buf_info->used = true;
+		mutex_unlock(&dst_buf_info->lock);
+		src_buf_info->used = true;
+
+		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
+
+		if (ret) {
+			mtk_v4l2_err(
+				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
+				ctx->idx,
+				src_buf->index,
+				src_buf_info->lastframe,
+				buf.size,
+				src_buf_info->vb.timestamp.tv_sec,
+				src_buf_info->vb.timestamp.tv_usec,
+				dst_buf->index,
+				ret, res_chg);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+					VB2_BUF_STATE_ERROR);
+		} else if (res_chg == false) {
+			/* we only return src buffer with VB2_BUF_STATE_DONE
+			  * when decode success without resolution change
+			  */
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+			v4l2_m2m_buf_done(&src_buf_info->vb,
+							VB2_BUF_STATE_DONE);
+		}
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		clean_display_buffer(ctx);
+		clean_free_buffer(ctx);
+
+		if ((ret == 0) && (res_chg == true)) {
+			mtk_vdec_pic_info_update(ctx);
+			/*
+			  * On encountering a resolution change in the stream.
+			  * The driver must first process and decode all
+			  * remaining buffers from before the resolution change
+			  * point, so call flush decode here
+			  */
+			mtk_vdec_flush_decoder(ctx);
+			/*
+			  * After all buffers containing decoded frames from
+			  * before the resolution change point ready to be
+			  * dequeued on the CAPTURE queue, the driver sends a
+			  * V4L2_EVENT_SOURCE_CHANGE event for source change
+			  * type V4L2_EVENT_SRC_CH_RESOLUTION
+			  */
+			mtk_vdec_queue_res_chg_event(ctx);
+		}
+	}
+
+	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+
+}
+
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_unlock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
+{
+	mutex_lock(&ctx->dev->dec_mutex);
+	return 0;
+}
+
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	int ret = 0;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	src_vq->drv_priv	= ctx;
+	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	src_vq->ops		= &mtk_vdec_vb2_ops;
+	src_vq->mem_ops		= &vb2_dma_contig_memops;
+	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	src_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(src_vq);
+	if (ret) {
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
+		return ret;
+	}
+	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
+	dst_vq->drv_priv	= ctx;
+	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
+	dst_vq->ops		= &mtk_vdec_vb2_ops;
+	dst_vq->mem_ops		= &vb2_dma_contig_memops;
+	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	dst_vq->lock = &ctx->dev->dev_mutex;
+
+	ret = vb2_queue_init(dst_vq);
+	if (ret) {
+		vb2_queue_release(src_vq);
+		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
+{
+	vdec_if_deinit(ctx);
+	ctx->state = MTK_STATE_FREE;
+}
+
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
+{
+	struct mtk_q_data *q_data;
+
+	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
+	ctx->fh.m2m_ctx = ctx->m2m_ctx;
+	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
+	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
+
+	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
+	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
+
+
+	q_data = &ctx->q_data[MTK_Q_DATA_DST];
+	memset(q_data, 0, sizeof(struct mtk_q_data));
+	q_data->visible_width = DFT_CFG_WIDTH;
+	q_data->visible_height = DFT_CFG_HEIGHT;
+	q_data->coded_width = DFT_CFG_WIDTH;
+	q_data->coded_height = DFT_CFG_HEIGHT;
+	q_data->colorspace = V4L2_COLORSPACE_REC709;
+	q_data->field = V4L2_FIELD_NONE;
+
+	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
+
+	v4l_bound_align_image(&q_data->coded_width,
+					MTK_VDEC_MIN_W,
+					MTK_VDEC_MAX_W, 4,
+					&q_data->coded_height,
+					MTK_VDEC_MIN_H,
+					MTK_VDEC_MAX_H, 5, 6);
+
+	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
+	q_data->bytesperline[0] = q_data->coded_width;
+	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
+	q_data->bytesperline[1] = q_data->coded_width;
+
+}
+
+static int vidioc_vdec_streamon(struct file *file, void *priv,
+				enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+
+	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_streamoff(struct file *file, void *priv,
+				 enum v4l2_buf_type type)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
+	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
+}
+
+static int vidioc_vdec_reqbufs(struct file *file, void *priv,
+			       struct v4l2_requestbuffers *reqbufs)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	int ret;
+
+	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
+			 reqbufs->type, reqbufs->count);
+	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
+
+	return ret;
+}
+
+static int vidioc_vdec_querybuf(struct file *file, void *priv,
+				struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
+			 buf->type, buf->index);
+	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_qbuf(struct file *file, void *priv,
+			    struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct vb2_queue *vq;
+	struct vb2_buffer *vb;
+	struct mtk_video_buf *mtkbuf;
+	struct vb2_v4l2_buffer	*vb2_v4l2;
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
+	vb = vq->bufs[buf->index];
+	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+
+	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    (buf->m.planes[0].bytesused == 0)) {
+		mtkbuf->lastframe = true;
+		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
+			 ctx->idx, buf->type, buf->index,
+			 mtkbuf->lastframe, buf->bytesused,
+			 buf->m.planes[0].bytesused, buf->length,
+			 vb);
+	}
+
+	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_dqbuf(struct file *file, void *priv,
+			     struct v4l2_buffer *buf)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state == MTK_STATE_ABORT) {
+		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
+					ctx->idx);
+		return -EIO;
+	}
+
+	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
+}
+
+static int vidioc_vdec_expbuf(struct file *file, void *priv,
+			      struct v4l2_exportbuffer *eb)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
+}
+
+static int vidioc_vdec_querycap(struct file *file, void *priv,
+				struct v4l2_capability *cap)
+{
+	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
+	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
+	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
+	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
+	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+
+	return 0;
+}
+
+static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_event_unsubscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_EOS:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	return 0;
+}
+
+static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
+				struct v4l2_format *f)
+{
+	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
+	struct mtk_video_fmt *fmt;
+
+	fmt = mtk_vdec_find_format(f);
+	if (!fmt) {
+		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
+		fmt = mtk_vdec_find_format(f);
+	}
+
+	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
+		mtk_v4l2_err("sizeimage of output format must be given");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vidioc_vdec_g_crop(struct file *file, void *priv,
+			 struct v4l2_crop *cr)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (ctx->state < MTK_STATE_HEADER)
+		return -EINVAL;
+
+	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
+	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
+
+		if (vdec_if_get_param(ctx,
+					   GET_PARAM_CROP_INFO,
+					   cr)) {
+			mtk_v4l2_debug(2,
+					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
+					 ctx->idx);
+			cr->c.left = 0;
+			cr->c.top = 0;
+			cr->c.width = ctx->picinfo.buf_w;
+			cr->c.height = ctx->picinfo.buf_h;
+		}
+		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
+				 cr->c.left, cr->c.top, cr->c.width,
+				 cr->c.height);
+	} else {
+		cr->c.left = 0;
+		cr->c.top = 0;
+		cr->c.width = ctx->picinfo.pic_w;
+		cr->c.height = ctx->picinfo.pic_h;
+		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
+				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
+				 ctx->picinfo.buf_h);
+	}
+	return 0;
+}
+
+static int vidioc_vdec_s_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+	int ret;
+	struct mtk_video_fmt *fmt;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	fmt = mtk_vdec_find_format(f);
+	if (fmt == NULL) {
+		mtk_v4l2_err("mtk_venc_find_format fail");
+		return ret;
+	}
+
+	pix_mp = &f->fmt.pix_mp;
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
+		mtk_v4l2_err("out_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
+		mtk_v4l2_err("cap_q_ctx buffers already requested");
+		ret = -EBUSY;
+	}
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq)
+		return -EINVAL;
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+	if (!q_data)
+		return -EINVAL;
+
+	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		q_data->fmt = fmt;
+		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+		pix_mp->width = 0;
+		pix_mp->height = 0;
+
+		if (ctx->state == MTK_STATE_FREE) {
+			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
+			if (ret) {
+				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
+					       ctx->idx, ret);
+				return -EINVAL;
+			}
+			ctx->state = MTK_STATE_INIT;
+		}
+	} else {
+		q_data->fmt = fmt;
+	}
+
+	return 0;
+}
+
+static int vidioc_enum_framesizes(struct file *file, void *priv,
+				struct v4l2_frmsizeenum *fsize)
+{
+	int i = 0;
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+
+	if (fsize->index != 0)
+		return -EINVAL;
+
+	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
+		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
+			continue;
+
+		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
+		if (!(ctx->dev->dec_capability &
+				VCODEC_CAPABILITY_4K_DISABLED)) {
+			mtk_v4l2_debug(1, "4K is enabled");
+			fsize->stepwise.max_width =
+					VCODEC_DEC_4K_CODED_WIDTH;
+			fsize->stepwise.max_height =
+					VCODEC_DEC_4K_CODED_HEIGHT;
+		}
+		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
+					ctx->dev->dec_capability,
+					fsize->stepwise.min_width,
+					fsize->stepwise.max_width,
+					fsize->stepwise.step_width,
+					fsize->stepwise.min_height,
+					fsize->stepwise.max_height,
+					fsize->stepwise.step_height);
+		return 0;
+	}
+
+	return -EINVAL;
+
+}
+
+static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
+{
+	struct mtk_video_fmt *fmt;
+	int i, j = 0;
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
+			continue;
+		if (!output_queue &&
+			(mtk_video_formats[i].type != MTK_FMT_FRAME))
+			continue;
+
+		if (j == f->index)
+			break;
+		++j;
+	}
+
+	if (i == NUM_FORMATS)
+		return -EINVAL;
+
+	fmt = &mtk_video_formats[i];
+	f->pixelformat = fmt->fourcc;
+	f->flags = 0;
+	memset(f->reserved, 0, sizeof(f->reserved));
+
+	if (mtk_video_formats[i].type != MTK_FMT_DEC)
+		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
+
+	return 0;
+}
+
+static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, false);
+}
+
+static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
+					       struct v4l2_fmtdesc *f)
+{
+	return vidioc_enum_fmt(f, true);
+}
+
+static int vidioc_vdec_g_fmt(struct file *file, void *priv,
+			     struct v4l2_format *f)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	struct vb2_queue *vq;
+	struct mtk_q_data *q_data;
+
+	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+	if (!vq) {
+		mtk_v4l2_err("no vb2 queue for type=%d",
+					   f->type);
+		return -EINVAL;
+	}
+
+	q_data = mtk_vdec_get_q_data(ctx, f->type);
+
+	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
+	    (ctx->state >= MTK_STATE_HEADER)) {
+		/* previous decode might have resolution change that make
+		 * picinfo changed update context'x picinfo here to make sure
+		 * we know what report to user space
+		 */
+		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
+			sizeof(struct vdec_pic_info));
+		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
+					ctx->picinfo.y_len_sz;
+		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
+					ctx->picinfo.c_len_sz;
+		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
+		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
+		q_data->coded_width = ctx->picinfo.buf_w;
+		q_data->coded_height = ctx->picinfo.buf_h;
+
+		/*
+		 * Width and height are set to the dimensions
+		 * of the movie, the buffer is bigger and
+		 * further processing stages should crop to this
+		 * rectangle.
+		 */
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+
+		/*
+		 * Set pixelformat to the format in which mt vcodec
+		 * outputs the decoded frame
+		 */
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		/*
+		 * This is run on OUTPUT
+		 * The buffer contains compressed image
+		 * so width and height have no meaning.
+		 * Assign value here to pass v4l2-compliance test
+		 */
+		pix_mp->width = q_data->visible_width;
+		pix_mp->height = q_data->visible_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+	} else {
+		pix_mp->width = q_data->coded_width;
+		pix_mp->height = q_data->coded_height;
+		pix_mp->field = V4L2_FIELD_NONE;
+		pix_mp->num_planes = q_data->fmt->num_planes;
+		pix_mp->pixelformat = q_data->fmt->fourcc;
+		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
+		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
+		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
+		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
+
+		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
+				ctx->idx, ctx->state, f->type);
+		return -EAGAIN;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
+				   const void *parg,
+				   unsigned int *nbuffers,
+				   unsigned int *nplanes,
+				   unsigned int sizes[], void *alloc_ctxs[])
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
+	struct mtk_q_data *q_data;
+
+	q_data = mtk_vdec_get_q_data(ctx, vq->type);
+
+	switch (vq->type) {
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+		*nplanes = 1;
+
+		sizes[0] = q_data->sizeimage[0];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		break;
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+		if (ctx->state < MTK_STATE_HEADER)
+			return -EINVAL;
+
+		/*
+		 * Output plane count is 2 - one for Y and one for CbCr
+		 */
+		*nplanes = 2;
+
+		mtk_v4l2_debug(1,
+				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
+				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
+
+		sizes[0] = q_data->sizeimage[0];
+		sizes[1] = q_data->sizeimage[1];
+		alloc_ctxs[0] = ctx->dev->alloc_ctx;
+		alloc_ctxs[1] = ctx->dev->alloc_ctx;
+		break;
+	default:
+		break;
+	}
+
+	mtk_v4l2_debug(1,
+			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
+			ctx->idx, vq->type, *nplanes, *nbuffers,
+			sizes[0], sizes[1]);
+
+	return 0;
+}
+
+static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct mtk_q_data *q_data;
+	int i;
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
+			 ctx->idx, vb->vb2_queue->type, vb->index);
+
+	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
+
+	for (i = 0; i < q_data->fmt->num_planes; i++) {
+		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
+			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
+				       i, vb2_plane_size(vb, i),
+				       q_data->sizeimage[i]);
+		}
+	}
+
+	return 0;
+}
+
+static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
+			ctx->idx, vb->vb2_queue->type,
+			vb->index, vb);
+	/*
+	 * check if this buffer is ready to be used after decode
+	 */
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		struct vb2_buffer *src_buf;
+		struct mtk_vcodec_mem buf;
+		bool res_chg = false;
+		int ret = 0;
+		int dpbsize = 1;
+
+		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
+
+		if (ctx->state == MTK_STATE_INIT) {
+			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+			if (!src_buf) {
+				mtk_v4l2_err("No src buffer");
+				return;
+			}
+
+			buf.va	= vb2_plane_vaddr(src_buf, 0);
+			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
+							src_buf,
+							0);
+			buf.size = (size_t)src_buf->planes[0].bytesused;
+			mtk_v4l2_debug(2,
+					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
+					ctx->idx,
+					src_buf->index,
+					buf.va, (u64)buf.dma_addr, buf.size);
+
+			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
+			if (ret) {
+				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+						VB2_BUF_STATE_DONE);
+				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
+						ctx->idx,
+						src_buf->index,
+						buf.size, ret);
+				return;
+			}
+
+			if (vdec_if_get_param(ctx,
+						   GET_PARAM_PIC_INFO,
+						   &ctx->picinfo)) {
+				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
+						ctx->idx);
+				return;
+			}
+
+			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
+				sizeof(struct vdec_pic_info));
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
+						ctx->picinfo.y_bs_sz +
+						ctx->picinfo.y_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
+							ctx->picinfo.buf_w;
+			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
+						ctx->picinfo.c_bs_sz +
+						ctx->picinfo.c_len_sz;
+			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
+							ctx->picinfo.buf_w;
+			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
+				ctx->idx,
+				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
+				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
+				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
+
+			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
+			if (dpbsize == 0) {
+				mtk_v4l2_debug(2,
+						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
+						ctx->idx, ret);
+				dpbsize = 1;
+			}
+			ctx->dpb_count = dpbsize;
+			ctx->state = MTK_STATE_HEADER;
+			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
+							ctx->dpb_count);
+		} else {
+			mtk_v4l2_debug(1, "[%d] already init driver %d",
+					 ctx->idx, ctx->state);
+		}
+
+	} else {
+		mutex_lock(&buf->lock);
+		if (buf->used == false) {
+			v4l2_m2m_buf_queue(ctx->m2m_ctx,
+					to_vb2_v4l2_buffer(vb));
+			buf->queued_in_vb2 = true;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		} else {
+			buf->queued_in_vb2 = false;
+			buf->queued_in_v4l2 = true;
+			buf->ready_to_display = false;
+		}
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
+{
+	if (vb->vb2_queue->type ==
+		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
+		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
+		mutex_lock(&buf->lock);
+		buf->queued_in_v4l2 = false;
+		buf->queued_in_vb2 = false;
+		mutex_unlock(&buf->lock);
+	}
+}
+
+static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
+					struct vb2_v4l2_buffer, vb2_buf);
+	struct mtk_video_buf *buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		buf->used = false;
+		buf->ready_to_display = false;
+		buf->nonrealdisplay = false;
+		buf->queued_in_v4l2 = false;
+		mutex_init(&buf->lock);
+	} else {
+		buf->lastframe = false;
+	}
+
+	return 0;
+}
+
+static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	if (ctx->state == MTK_STATE_FLUSH)
+		ctx->state = MTK_STATE_HEADER;
+
+	return 0;
+}
+
+static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
+{
+	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
+	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
+
+	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
+			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
+
+	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		while (src_buf) {
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
+					VB2_BUF_STATE_ERROR);
+			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+		}
+	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		int i;
+		struct vb2_v4l2_buffer *vb2_v4l2;
+		struct mtk_video_buf *buf;
+
+		if (ctx->state >= MTK_STATE_HEADER)
+			mtk_vdec_flush_decoder(ctx);
+
+		ctx->state = MTK_STATE_FLUSH;
+
+		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		while (dst_buf) {
+			vb2_set_plane_payload(dst_buf, 0, 0);
+			vb2_set_plane_payload(dst_buf, 1, 0);
+			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
+				VB2_BUF_STATE_ERROR);
+			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+		}
+
+		for (i = 0; i < q->num_buffers; ++i) {
+			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
+				vb2_v4l2 = container_of(q->bufs[i],
+					struct vb2_v4l2_buffer, vb2_buf);
+				buf = container_of(vb2_v4l2,
+					struct mtk_video_buf, vb);
+
+				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
+						ctx->idx, i, q->type,
+						buf->queued_in_vb2,
+						buf->queued_in_v4l2,
+						(int)q->bufs[i]->state);
+				v4l2_m2m_buf_done(vb2_v4l2,
+						VB2_BUF_STATE_ERROR);
+			}
+		}
+	}
+}
+
+static void m2mops_vdec_device_run(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+	struct mtk_vcodec_dev *dev = ctx->dev;
+
+	queue_work(dev->decode_workqueue, &ctx->decode_work);
+}
+
+static int m2mops_vdec_job_ready(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+
+	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
+		return 0;
+
+	if (ctx->state == MTK_STATE_ABORT)
+		return 0;
+
+	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
+	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
+		return 0;
+
+	if (ctx->state != MTK_STATE_HEADER)
+		return 0;
+
+	return 1;
+}
+
+static void m2mops_vdec_job_abort(void *priv)
+{
+	struct mtk_vcodec_ctx *ctx = priv;
+
+	ctx->state = MTK_STATE_ABORT;
+}
+
+static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
+
+	switch (ctrl->id) {
+	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
+		if (ctx->state >= MTK_STATE_HEADER) {
+			ctrl->val = ctx->dpb_count;
+		} else {
+			mtk_v4l2_err("Seqinfo not ready");
+			ctrl->val = 1;
+			return -EAGAIN;
+		}
+		break;
+	default:
+		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
+	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
+};
+
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
+{
+	struct v4l2_ctrl *ctrl;
+
+	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
+
+	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
+					  &mtk_vcodec_dec_ctrl_ops,
+					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
+					  1, 32, 1, 1);
+	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
+
+	if (ctx->ctrl_hdl.error) {
+		mtk_v4l2_err("Adding control failed %d",
+					ctx->ctrl_hdl.error);
+		return ctx->ctrl_hdl.error;
+	}
+
+	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
+	return 0;
+}
+
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
+{
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+}
+
+static void m2mops_vdec_lock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_lock(&ctx->dev->dev_mutex);
+}
+
+static void m2mops_vdec_unlock(void *m2m_priv)
+{
+	struct mtk_vcodec_ctx *ctx = m2m_priv;
+
+	mtk_v4l2_debug(3, "[%d]", ctx->idx);
+	mutex_unlock(&ctx->dev->dev_mutex);
+}
+
+const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
+	.device_run	= m2mops_vdec_device_run,
+	.job_ready	= m2mops_vdec_job_ready,
+	.job_abort	= m2mops_vdec_job_abort,
+	.lock	= m2mops_vdec_lock,
+	.unlock	= m2mops_vdec_unlock,
+};
+
+const struct vb2_ops mtk_vdec_vb2_ops = {
+	.queue_setup	= vb2ops_vdec_queue_setup,
+	.buf_prepare	= vb2ops_vdec_buf_prepare,
+	.buf_queue	= vb2ops_vdec_buf_queue,
+	.wait_prepare	= vb2_ops_wait_prepare,
+	.wait_finish	= vb2_ops_wait_finish,
+	.buf_init	= vb2ops_vdec_buf_init,
+	.buf_finish	= vb2ops_vdec_buf_finish,
+	.start_streaming	= vb2ops_vdec_start_streaming,
+	.stop_streaming	= vb2ops_vdec_stop_streaming,
+};
+
+const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
+	.vidioc_streamon	= vidioc_vdec_streamon,
+	.vidioc_streamoff	= vidioc_vdec_streamoff,
+
+	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
+	.vidioc_querybuf	= vidioc_vdec_querybuf,
+	.vidioc_qbuf	= vidioc_vdec_qbuf,
+	.vidioc_expbuf	= vidioc_vdec_expbuf,
+	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
+
+	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
+	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
+
+	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
+	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
+	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
+
+	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
+	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
+	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
+
+	.vidioc_querycap	= vidioc_vdec_querycap,
+	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
+	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
+	.vidioc_g_crop	= vidioc_vdec_g_crop,
+};
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
new file mode 100644
index 0000000..cb5cb42
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
@@ -0,0 +1,81 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_H_
+#define _MTK_VCODEC_DEC_H_
+
+#include <media/videobuf2-core.h>
+#include <media/videobuf2-v4l2.h>
+
+/**
+ * struct vdec_fb  - decoder frame buffer
+ * @base_y	: Y plane memory info
+ * @base_c	: C plane memory info
+ * @status      : frame buffer status (vdec_fb_status)
+ */
+struct vdec_fb {
+	struct mtk_vcodec_mem	base_y;
+	struct mtk_vcodec_mem	base_c;
+	unsigned int	status;
+};
+
+/**
+ * struct mtk_video_buf - Private data related to each VB2 buffer.
+ * @b:			VB2 buffer
+ * @list:			link list
+ * @used:		Output buffer contain decoded frame data
+ * @ready_to_display:	Output buffer not display yet
+ * @nonrealdisplay:	Output buffer is not display frame
+ * @queued_in_vb2:	Output buffer is queue in vb2
+ * @queued_in_v4l2:	Output buffer is in v4l2
+ * @lastframe:		Intput buffer is last buffer - EOS
+ * @frame_buffer:		Decode status of output buffer
+ * @lock:			V4L2 and decode thread should get mutex
+ *			before r/w info in mtk_video_buf
+ */
+struct mtk_video_buf {
+	struct vb2_v4l2_buffer	vb;
+	struct list_head	list;
+
+	bool	used;
+	bool	ready_to_display;
+	bool	nonrealdisplay;
+	bool	queued_in_vb2;
+	bool	queued_in_v4l2;
+	bool	lastframe;
+	struct vdec_fb	frame_buffer;
+	struct mutex	lock;
+};
+
+extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
+extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
+
+
+/*
+ * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
+ * get/release lock before/after access decoder hw.
+ * mtk_vdec_lock get decoder hw lock and set curr_ctx
+ * to ctx instance that get lock
+ */
+int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
+int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
+			   struct vb2_queue *dst_vq);
+void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
+void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
+int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
+void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
+
+#endif /* _MTK_VCODEC_DEC_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
new file mode 100644
index 0000000..34e3217
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
@@ -0,0 +1,469 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: PC Chen <pc.chen@mediatek.com>
+*         Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-dma-contig.h>
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_intr.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_drv_if.h"
+#include "mtk_vpu.h"
+
+module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
+module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
+
+#define VDEC_HW_ACTIVE	0x10
+#define VDEC_IRQ_CFG	0x11
+#define VDEC_IRQ_CLR	0x10
+#define VDEC_IRQ_CFG_REG	0xa4
+
+
+/* Wake up context wait_queue */
+static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
+{
+	ctx->int_cond = 1;
+	ctx->int_type = reason;
+	wake_up_interruptible(&ctx->queue);
+}
+
+static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	unsigned long flags;
+	struct mtk_vcodec_ctx *ctx;
+	unsigned int cg_status = 0;
+	unsigned int dec_done_status = 0;
+
+	spin_lock_irqsave(&dev->irqlock, flags);
+	ctx = dev->curr_ctx;
+	spin_unlock_irqrestore(&dev->irqlock, flags);
+
+	cg_status = readl(dev->reg_base[0] + (0));
+	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
+		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
+			       cg_status);
+		return IRQ_HANDLED;
+	}
+
+	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	ctx->irq_status = dec_done_status;
+	if ((dec_done_status & (0x1 << 16)) != 0x10000)
+		return IRQ_HANDLED;
+
+	/* clear interrupt */
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
+	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
+
+	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
+
+	mtk_v4l2_debug(3,
+			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
+			ctx->idx, dec_done_status);
+
+
+	return IRQ_HANDLED;
+}
+
+static int fops_vcodec_open(struct file *file)
+{
+	struct video_device *vfd = video_devdata(file);
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = NULL;
+	int ret = 0;
+
+	if (dev->instance_mask == ~0UL) {
+		mtk_v4l2_err("Too many open contexts");
+		ret = -EBUSY;
+		goto err_alloc;
+	}
+
+	mutex_lock(&dev->dev_mutex);
+
+	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	ctx->idx = ffz(dev->instance_mask);
+	v4l2_fh_init(&ctx->fh, video_devdata(file));
+	file->private_data = &ctx->fh;
+	v4l2_fh_add(&ctx->fh);
+	INIT_LIST_HEAD(&ctx->list);
+	ctx->dev = dev;
+	init_waitqueue_head(&ctx->queue);
+
+	if (vfd == dev->vfd_dec) {
+		ctx->type = MTK_INST_DECODER;
+		ret = mtk_vdec_ctrls_setup(ctx);
+		if (ret) {
+			mtk_v4l2_err("Failed to setup mt vcodec controls");
+			goto err_ctrls_setup;
+		}
+		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
+						 &m2mctx_vdec_queue_init);
+		if (IS_ERR(ctx->m2m_ctx)) {
+			ret = PTR_ERR(ctx->m2m_ctx);
+			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
+				       ret);
+			goto err_ctx_init;
+		}
+		mtk_vcodec_dec_set_default_params(ctx);
+	} else {
+		mtk_v4l2_err("Invalid vfd !");
+		ret = -ENOENT;
+		goto err_ctx_init;
+	}
+
+	if (v4l2_fh_is_singular(&ctx->fh)) {
+		mtk_vcodec_dec_pw_on(&dev->pm);
+		ret = vpu_load_firmware(dev->vpu_plat_dev);
+		if (ret < 0) {
+			mtk_v4l2_err("vpu_load_firmware failed!");
+			goto err_load_fw;
+		}
+
+		dev->dec_capability =
+			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
+		mtk_v4l2_debug(0, "decoder capability %x",
+			       dev->dec_capability);
+	}
+
+	set_bit(ctx->idx, &dev->instance_mask);
+	dev->num_instances++;
+	list_add(&ctx->list, &dev->ctx_list);
+	mutex_unlock(&dev->dev_mutex);
+	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
+					ctx->idx);
+
+	return ret;
+
+	/* Deinit when failure occurred */
+err_load_fw:
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+err_ctx_init:
+	mtk_vdec_ctrls_free(ctx);
+err_ctrls_setup:
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+err_alloc:
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_release(struct file *file)
+{
+	struct mtk_vcodec_dev *dev = video_drvdata(file);
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
+
+	mutex_lock(&dev->dev_mutex);
+	v4l2_m2m_ctx_release(ctx->m2m_ctx);
+	mtk_vcodec_vdec_release(ctx);
+	mtk_vdec_ctrls_free(ctx);
+	v4l2_fh_del(&ctx->fh);
+	v4l2_fh_exit(&ctx->fh);
+	list_del_init(&ctx->list);
+	dev->num_instances--;
+	if (dev->num_instances == 0)
+		mtk_vcodec_dec_pw_off(&dev->pm);
+	clear_bit(ctx->idx, &dev->instance_mask);
+	devm_kfree(&dev->plat_dev->dev, ctx);
+	mutex_unlock(&dev->dev_mutex);
+	return 0;
+}
+
+static unsigned int fops_vcodec_poll(struct file *file,
+				     struct poll_table_struct *wait)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+	struct mtk_vcodec_dev *dev = ctx->dev;
+	int ret;
+
+	mutex_lock(&dev->dev_mutex);
+	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
+	mutex_unlock(&dev->dev_mutex);
+
+	return ret;
+}
+
+static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
+
+	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
+}
+
+static const struct v4l2_file_operations mtk_vcodec_fops = {
+	.owner				= THIS_MODULE,
+	.open				= fops_vcodec_open,
+	.release			= fops_vcodec_release,
+	.poll				= fops_vcodec_poll,
+	.unlocked_ioctl			= video_ioctl2,
+	.mmap				= fops_vcodec_mmap,
+};
+
+static void mtk_vcodec_dec_reset_handler(void *priv)
+{
+	struct mtk_vcodec_dev *dev = priv;
+	struct mtk_vcodec_ctx *ctx;
+
+	mtk_v4l2_debug(0, "Watchdog timeout!!");
+
+	mutex_lock(&dev->dev_mutex);
+	list_for_each_entry(ctx, &dev->ctx_list, list) {
+		ctx->state = MTK_STATE_ABORT;
+		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
+					   ctx->idx);
+	}
+	mutex_unlock(&dev->dev_mutex);
+}
+
+static int mtk_vcodec_probe(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev;
+	struct video_device *vfd_dec;
+	struct resource *res;
+	int i, ret;
+	int reg_base_num;
+
+	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&dev->ctx_list);
+	dev->plat_dev = pdev;
+	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
+	if (dev->vpu_plat_dev == NULL) {
+		mtk_v4l2_err("[VPU] vpu device in not ready");
+		return -EPROBE_DEFER;
+	}
+
+	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
+				dev, VPU_RST_DEC);
+
+	ret = mtk_vcodec_init_dec_pm(dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
+		return ret;
+	}
+
+	reg_base_num = NUM_MAX_VDEC_REG_BASE;
+	for (i = 0; i < reg_base_num; i++) {
+		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
+		if (res == NULL) {
+			dev_err(&pdev->dev, "get memory resource failed.");
+			ret = -ENXIO;
+			goto err_res;
+		}
+		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
+		if (IS_ERR(dev->reg_base[i])) {
+			dev_err(&pdev->dev,
+				"devm_ioremap_resource %d failed.", i);
+			ret = PTR_ERR(dev->reg_base);
+			goto err_res;
+		}
+		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "failed to get irq resource");
+		ret = -ENOENT;
+		goto err_res;
+	}
+
+	dev->dec_irq = platform_get_irq(pdev, 0);
+	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
+			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
+			dev->dec_irq,
+			ret);
+		ret = -EINVAL;
+		goto err_res;
+	}
+
+	disable_irq(dev->dec_irq);
+	mutex_init(&dev->dev_mutex);
+	mutex_init(&dev->dec_mutex);
+	spin_lock_init(&dev->irqlock);
+
+	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
+		 "[MTK_V4L2]");
+
+	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
+	if (ret) {
+		mtk_v4l2_err("v4l2_device_register err=%d", ret);
+		return ret;
+	}
+
+	init_waitqueue_head(&dev->queue);
+
+	vfd_dec = video_device_alloc();
+	if (!vfd_dec) {
+		mtk_v4l2_err("Failed to allocate video device");
+		ret = -ENOMEM;
+		goto err_dec_alloc;
+	}
+	vfd_dec->fops		= &mtk_vcodec_fops;
+	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
+	vfd_dec->release	= video_device_release;
+	vfd_dec->lock		= &dev->dev_mutex;
+	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
+	vfd_dec->vfl_dir	= VFL_DIR_M2M;
+
+	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
+		 MTK_VCODEC_DEC_NAME);
+	video_set_drvdata(vfd_dec, dev);
+	dev->vfd_dec = vfd_dec;
+	platform_set_drvdata(pdev, dev);
+
+	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
+	if (IS_ERR(dev->alloc_ctx)) {
+		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
+		ret = PTR_ERR(dev->alloc_ctx);
+		goto err_vb2_ctx_init;
+	}
+
+	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
+	if (IS_ERR(dev->m2m_dev_dec)) {
+		mtk_v4l2_err("Failed to init mem2mem dec device");
+		ret = PTR_ERR(dev->m2m_dev_dec);
+		goto err_dec_mem_init;
+	}
+
+	dev->decode_workqueue =
+			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
+			WQ_MEM_RECLAIM | WQ_FREEZABLE);
+	if (!dev->decode_workqueue) {
+		mtk_v4l2_err("Failed to create decode workqueue");
+		ret = -EINVAL;
+		goto err_event_workq;
+	}
+
+	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
+	if (ret) {
+		mtk_v4l2_err("Failed to register video device");
+		goto err_dec_reg;
+	}
+	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
+			 vfd_dec->num);
+
+	return 0;
+
+err_dec_reg:
+	destroy_workqueue(dev->decode_workqueue);
+err_event_workq:
+	v4l2_m2m_release(dev->m2m_dev_dec);
+err_dec_mem_init:
+	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+err_vb2_ctx_init:
+	video_unregister_device(vfd_dec);
+err_dec_alloc:
+	v4l2_device_unregister(&dev->v4l2_dev);
+err_res:
+	mtk_vcodec_release_dec_pm(dev);
+	return ret;
+}
+
+static const struct of_device_id mtk_vcodec_match[] = {
+	{.compatible = "mediatek,mt8173-vcodec-dec",},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
+
+static int mtk_vcodec_dec_remove(struct platform_device *pdev)
+{
+	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug_enter();
+	flush_workqueue(dev->decode_workqueue);
+	destroy_workqueue(dev->decode_workqueue);
+	if (dev->m2m_dev_dec)
+		v4l2_m2m_release(dev->m2m_dev_dec);
+	if (dev->alloc_ctx)
+		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
+
+	if (dev->vfd_dec)
+		video_unregister_device(dev->vfd_dec);
+
+	v4l2_device_unregister(&dev->v4l2_dev);
+	mtk_vcodec_release_dec_pm(dev);
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
+
+	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
+
+	if (m_dev->num_instances == 0)
+		return 0;
+
+	return 0;
+}
+
+static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
+	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
+			   mtk_vcodec_vdec_pm_runtime_resume,
+			   NULL)
+};
+
+static struct platform_driver mtk_vcodec_dec_driver = {
+	.probe	= mtk_vcodec_probe,
+	.remove	= mtk_vcodec_dec_remove,
+	.driver	= {
+		.name	= MTK_VCODEC_DEC_NAME,
+		.owner	= THIS_MODULE,
+		.of_match_table = mtk_vcodec_match,
+		.pm = &mtk_vcodec_vdec_pm_ops,
+	},
+};
+
+module_platform_driver(mtk_vcodec_dec_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
new file mode 100644
index 0000000..aa1701f
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
@@ -0,0 +1,153 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#include <linux/clk.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <soc/mediatek/smi.h>
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
+{
+	struct device_node *node;
+	struct platform_device *pdev;
+	struct device *dev;
+	struct mtk_vcodec_pm *pm;
+	int ret = 0;
+
+	pdev = mtkdev->plat_dev;
+	pm = &mtkdev->pm;
+	pm->mtkdev = mtkdev;
+	dev = &pdev->dev;
+	node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
+	if (!node)
+		return -1;
+
+	pdev = of_find_device_by_node(node);
+	if (WARN_ON(!pdev)) {
+		of_node_put(node);
+		return -1;
+	}
+	pm->larbvdec = &pdev->dev;
+
+	pdev = mtkdev->plat_dev;
+	pm_runtime_enable(&pdev->dev);
+	pm->dev = &pdev->dev;
+
+	pm->vcodecpll = devm_clk_get(&pdev->dev, "vcodecpll");
+	if (pm->vcodecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vcodecpll fail");
+		ret = -1;
+	}
+
+	pm->univpll_d2 = devm_clk_get(&pdev->dev, "univpll_d2");
+	if (pm->univpll_d2 == NULL) {
+		mtk_v4l2_err("devm_clk_get univpll_d2 fail");
+		ret = -1;
+	}
+
+	pm->clk_cci400_sel = devm_clk_get(&pdev->dev, "clk_cci400_sel");
+	if (pm->clk_cci400_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get clk_cci400_sel fail");
+		ret = -1;
+	}
+
+	pm->vdec_sel = devm_clk_get(&pdev->dev, "vdec_sel");
+	if (pm->vdec_sel == NULL) {
+		mtk_v4l2_err("devm_clk_get vdec_sel fail");
+		ret = -1;
+	}
+
+	pm->vdecpll = devm_clk_get(&pdev->dev, "vdecpll");
+	if (pm->vdecpll == NULL) {
+		mtk_v4l2_err("devm_clk_get vdecpll fail");
+		ret = -1;
+	}
+
+	return ret;
+}
+
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
+{
+	pm_runtime_disable(dev->pm.dev);
+}
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_get_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_get_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	ret = pm_runtime_put_sync(pm->dev);
+	if (ret)
+		mtk_v4l2_err("pm_runtime_put_sync fail %d", ret);
+}
+
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm)
+{
+	int ret;
+
+	clk_set_rate(pm->vcodecpll, 1482 * 1000000);
+
+	ret = clk_prepare_enable(pm->vcodecpll);
+	if (ret)
+		mtk_v4l2_err("vcodecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("univpll_d2 fail %d", ret);
+
+	ret = clk_prepare_enable(pm->clk_cci400_sel);
+	if (ret)
+		mtk_v4l2_err("clk_cci400_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->clk_cci400_sel, pm->univpll_d2);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("vdecpll fail %d", ret);
+
+	ret = clk_prepare_enable(pm->vdec_sel);
+	if (ret)
+		mtk_v4l2_err("vdec_sel fail %d", ret);
+
+	ret = clk_set_parent(pm->vdec_sel, pm->vdecpll);
+	if (ret)
+		mtk_v4l2_err("clk_set_parent fail %d", ret);
+
+	ret = mtk_smi_larb_get(pm->larbvdec);
+	if (ret)
+		mtk_v4l2_err("mtk_smi_larb_get larbvdec fail %d", ret);
+
+}
+
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm)
+{
+	mtk_smi_larb_put(pm->larbvdec);
+	clk_disable_unprepare(pm->vdec_sel);
+	clk_disable_unprepare(pm->vdecpll);
+}
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
new file mode 100644
index 0000000..28de4c1
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
@@ -0,0 +1,28 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Tiffany Lin <tiffany.lin@mediatek.com>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*/
+
+#ifndef _MTK_VCODEC_DEC_PM_H_
+#define _MTK_VCODEC_DEC_PM_H_
+
+#include "mtk_vcodec_drv.h"
+
+int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *dev);
+void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev);
+
+void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm);
+void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm);
+
+#endif /* _MTK_VCODEC_DEC_PM_H_ */
\ No newline at end of file
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
index 55cf462..e3e3d66 100644
--- a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
@@ -26,10 +26,10 @@
 #include "mtk_vcodec_util.h"
 
 #define MTK_VCODEC_DRV_NAME	"mtk_vcodec_drv"
+#define MTK_VCODEC_DEC_NAME	"mtk-vcodec-dec" 
 #define MTK_VCODEC_ENC_NAME	"mtk-vcodec-enc"
 #define MTK_PLATFORM_STR	"platform:mt8173"
 
-
 #define MTK_VCODEC_MAX_PLANES	3
 #define MTK_V4L2_BENCHMARK	0
 #define WAIT_INTR_TIMEOUT_MS	1000
@@ -145,18 +145,18 @@ struct mtk_q_data {
 /**
  * struct mtk_enc_params - General encoding parameters
  * @bitrate: target bitrate in bits per second
- * @num_b_frame:	number of b frames between p-frame
- * @rc_frame:	frame based rate control
+ * @num_b_frame: number of b frames between p-frame
+ * @rc_frame: frame based rate control
  * @rc_mb: macroblock based rate control
  * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
- *				  with the first frame
+ *		  with the first frame
  * @intra_period: I frame period
- * @gop_size:	group of picture size, it's used as the intra frame period
- * @framerate_num: frame rate numerator. ex: framerate_num=30 and framerate_denom=1
- *				   menas FPS is 30
- * @framerate_denom:	frame rate denominator. ex: framerate_num=30 and
- *				       framerate_denom=1 menas FPS is 30
- * @h264_max_qp:	Max value for H.264 quantization parameter
+ * @gop_size: group of picture size, it's used as the intra frame period
+ * @framerate_num: frame rate numerator. ex: framerate_num=30 and
+ *		   framerate_denom=1 menas FPS is 30
+ * @framerate_denom: frame rate denominator. ex: framerate_num=30
+ *		     and framerate_denom=1 menas FPS is 30
+ * @h264_max_qp: Max value for H.264 quantization parameter
  * @h264_profile: V4L2 defined H.264 profile
  * @h264_level: V4L2 defined H.264 level
  * @force_intra: force/insert intra frame
@@ -197,33 +197,61 @@ struct mtk_vcodec_pm {
 	struct mtk_vcodec_dev	*mtkdev;
 };
 
+/**
+ * struct vdec_pic_info  - picture size information
+ * @pic_w: picture width
+ * @pic_h: picture height
+ * @buf_w: picture buffer width (16 aligned up from pic_w)
+ * @buf_h: picture buffer heiht (32 aligned up from pic_h)
+ * @y_bs_sz: Y bitstream size
+ * @c_bs_sz: CbCr bitstream size
+ * @y_len_sz: Y length size
+ * @c_len_sz: CbCr length size
+ * E.g. suppose picture size is 176x144,
+ *      buffer size will be aligned to 176x160.
+ */
+struct vdec_pic_info {
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int y_bs_sz;
+	unsigned int c_bs_sz;
+	unsigned int y_len_sz;
+	unsigned int c_len_sz;
+};
 
 /**
  * struct mtk_vcodec_ctx - Context (instance) private data.
  *
  * @type: type of the instance - decoder or encoder
- * @dev:	pointer to the mtk_vcodec_dev of the device
+ * @dev: pointer to the mtk_vcodec_dev of the device
  * @list: link to ctx_list of mtk_vcodec_dev
  * @fh: struct v4l2_fh
  * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
  * @q_data: store information of input and output queue
- *		    of the context
+ *		of the context
  * @idx: index of the context that this structure describes
  * @state: state of the context
  * @param_change: indicate encode parameter type
  * @enc_params: encoding parameters
- * @enc_if: hoooked encoder driver interface
+ * @dec_if: hooked decoder driver interface
+ * @enc_if: hooked encoder driver interface
  * @drv_handle: driver handle for specific decode/encode instance
  *
+ * @picinfo: store picture info after header parsing
+ * @dpb_count: store dpb count after header parsing
  * @int_cond: variable used by the waitqueue
  * @int_type: type of the last interrupt
  * @queue: waitqueue that can be used to wait for this context to
- *		   finish
+ *	finish
  * @irq_status: irq status
  *
  * @ctrl_hdl: handler for v4l2 framework
  *
+ * @decode_work: worker for the decoding
  * @encode_work: worker for the encoding
+ * @last_decoded_picinfo: pic information get from latest decode
  */
 struct mtk_vcodec_ctx {
 	enum mtk_instance_type type;
@@ -238,17 +266,24 @@ struct mtk_vcodec_ctx {
 	enum mtk_encode_param param_change;
 	struct mtk_enc_params enc_params;
 
+	struct vdec_common_if *dec_if;
 	struct venc_common_if *enc_if;
 	unsigned long drv_handle;
 
+	struct vdec_pic_info picinfo;
+	int dpb_count;
+
 	int int_cond;
 	int int_type;
 	wait_queue_head_t queue;
 	unsigned int irq_status;
 
 	struct v4l2_ctrl_handler ctrl_hdl;
+	struct work_struct decode_work;
 	struct work_struct encode_work;
+	struct vdec_pic_info last_decoded_picinfo;
 
+	int decoded_frame_cnt;
 #if MTK_V4L2_BENCHMARK
 	unsigned int total_enc_dec_cnt;
 	unsigned int total_enc_dec_time;
@@ -289,36 +324,61 @@ struct mtk_vcodec_ctx {
  *
  * @encode_workqueue: encode work queue
  *
+ * @v4l2_dev: V4L2 device to register video devices for.
+ * @vfd_dec: Video device for decoder
+ * @vfd_enc: Video device for encoder
+ *
+ * @m2m_dev_dec: m2m device for decoder
+ * @m2m_dev_enc: m2m device for encoder
+ * @plat_dev: platform device
+ * @vpu_plat_dev: mtk vpu platform device
+ * @alloc_ctx:VB2 allocator context
+ *	      (for allocations without kernel mapping).
+ * @ctx_list: list of struct mtk_vcodec_ctx
+ * @irqlock: protect data access by irq handler and work thread
+ * @curr_ctx: The context that is waiting for codec hardware
+ * @reg_base: Mapped address of MTK Vcodec registers.
+ *
+ * @instance_mask: used to mark which index id are used
+ * @num_instances: counter of active MTK Vcodec instances
+ *
+ * @decode_workqueue: decode work queue
+ * @encode_workqueue: encode work queue
  * @int_cond: used to identify interrupt condition happen
  * @int_type: used to identify what kind of interrupt condition happen
  * @dev_mutex: video_device lock
  * @queue: waitqueue for waiting for completion of device commands
  *
+ * @dec_irq: decoder irq resource
  * @enc_irq: h264 encoder irq resource
- * @enc_lt_irq: vp8 encoder irq resource
+ * @enc_lt_irq: vp8 encoder lt irq resource
  *
- * @enc_mutex: encoder hardware lock.
+ * @dec_mutex: decoder hardware lock
+ * @enc_mutex: encoder hardware lock
  *
  * @pm: power management control
  * @dec_capability: used to identify decode capability, ex: 4k
- * @enc_capability:	 used to identify encode capability
+ * @enc_capability: used to identify encode capability
  */
 struct mtk_vcodec_dev {
 	struct v4l2_device	v4l2_dev;
+	struct video_device	*vfd_dec;
 	struct video_device	*vfd_enc;
 
+	struct v4l2_m2m_dev	*m2m_dev_dec;
 	struct v4l2_m2m_dev	*m2m_dev_enc;
 	struct platform_device	*plat_dev;
 	struct platform_device	*vpu_plat_dev;
 	struct vb2_alloc_ctx	*alloc_ctx;
 	struct list_head	ctx_list;
 	spinlock_t	irqlock;
-	struct mtk_vcodec_ctx *curr_ctx;
+	struct mtk_vcodec_ctx	*curr_ctx;
 	void __iomem	*reg_base[NUM_MAX_VCODEC_REG_BASE];
 
 	unsigned long	instance_mask;
 	int	num_instances;
 
+	struct workqueue_struct *decode_workqueue;
 	struct workqueue_struct *encode_workqueue;
 
 	int	int_cond;
@@ -326,9 +386,11 @@ struct mtk_vcodec_dev {
 	struct mutex	dev_mutex;
 	wait_queue_head_t	queue;
 
+	int	dec_irq;
 	int	enc_irq;
 	int	enc_lt_irq;
 
+	struct mutex	dec_mutex;
 	struct mutex	enc_mutex;
 
 	struct mtk_vcodec_pm	pm;
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_base.h b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
new file mode 100644
index 0000000..73ea1fe
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_base.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_BASE_
+#define _VDEC_DRV_BASE_
+
+#include "mtk_vcodec_drv.h"
+
+#include "vdec_drv_if.h"
+
+struct vdec_common_if {
+	/**
+	 * (*init)() - initialize decode driver
+	 * @ctx     : [in] mtk v4l2 context
+	 * @h_vdec  : [out] driver handle
+	 */
+	int (*init)(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec);
+
+	/**
+	 * (*decode)() - trigger decode
+	 * @h_vdec  : [in] driver handle
+	 * @bs      : [in] input bitstream
+	 * @fb      : [in] frame buffer to store decoded frame
+	 * @res_chg : [out] resolution change happen
+	 */
+	int (*decode)(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		      struct vdec_fb *fb, bool *res_chg);
+
+	/**
+	 * (*get_param)() - get driver's parameter
+	 * @h_vdec : [in] driver handle
+	 * @type   : [in] input parameter type
+	 * @out    : [out] buffer to store query result
+	 */
+	int (*get_param)(unsigned long h_vdec, enum vdec_get_param_type type,
+			 void *out);
+
+	/**
+	 * (*deinit)() - deinitialize driver.
+	 * @h_vdec : [in] driver handle to be deinit
+	 */
+	int (*deinit)(unsigned long h_vdec);
+};
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
new file mode 100644
index 0000000..1d12719
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *         Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+
+#include "vdec_drv_base.h"
+#include "vdec_drv_if.h"
+
+#include "mtk_vcodec_dec_pm.h"
+#include "mtk_vcodec_util.h"
+#include "mtk_vpu.h"
+
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
+{
+	int ret = 0;
+
+	switch (fourcc) {
+	case V4L2_PIX_FMT_H264:
+	case V4L2_PIX_FMT_VP8:
+	case V4L2_PIX_FMT_VP9:
+	default:
+		return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->init(ctx, &ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	unsigned long flags;
+	int ret = 0;
+
+	if (bs) {
+		if ((bs->dma_addr & 63) != 0)
+			return -EINVAL;
+	}
+
+	if (fb) {
+		if (((fb->base_y.dma_addr & 511) != 0) ||
+		    ((fb->base_c.dma_addr & 511) != 0))
+			return -EINVAL;
+	}
+
+	mtk_vdec_lock(ctx);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = ctx;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	enable_irq(ctx->dev->dec_irq);
+	ret = ctx->dec_if->decode(ctx->drv_handle, bs, fb, res_chg);
+	disable_irq(ctx->dev->dec_irq);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+
+	spin_lock_irqsave(&ctx->dev->irqlock, flags);
+	ctx->dev->curr_ctx = NULL;
+	spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
+
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out)
+{
+	int ret = 0;
+
+	mtk_vdec_lock(ctx);
+	ret = ctx->dec_if->get_param(ctx->drv_handle, type, out);
+	mtk_vdec_unlock(ctx);
+
+	return ret;
+}
+
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx)
+{
+	int ret = 0;
+
+	if (ctx->drv_handle == 0)
+		return 0;
+
+	mtk_vdec_lock(ctx);
+	mtk_vcodec_dec_clock_on(&ctx->dev->pm);
+	ret = ctx->dec_if->deinit(ctx->drv_handle);
+	mtk_vcodec_dec_clock_off(&ctx->dev->pm);
+	mtk_vdec_unlock(ctx);
+
+	ctx->drv_handle = 0;
+	return ret;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.h b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
new file mode 100644
index 0000000..9717446
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *		   Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_DRV_IF_H_
+#define _VDEC_DRV_IF_H_
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_dec.h"
+#include "mtk_vcodec_util.h"
+
+/**
+ * struct vdec_fb_status  - decoder frame buffer status
+ * @FB_ST_NORMAL	: initial state
+ * @FB_ST_DISPLAY	: frmae buffer is ready to be displayed
+ * @FB_ST_FREE		: frame buffer is not used by decoder any more
+ */
+enum vdec_fb_status {
+	FB_ST_NORMAL		= 0,
+	FB_ST_DISPLAY		= (1 << 0),
+	FB_ST_FREE		= (1 << 1)
+};
+
+enum vdec_get_param_type {
+	GET_PARAM_DISP_FRAME_BUFFER,
+	GET_PARAM_FREE_FRAME_BUFFER,
+	GET_PARAM_PIC_INFO,
+	GET_PARAM_CROP_INFO,
+	GET_PARAM_DPB_SIZE
+};
+
+/**
+ * struct vdec_fb_node  - decoder frame buffer node
+ * @list	: list to hold this node
+ * @fb		: point to frame buffer (vdec_fb)
+ */
+struct vdec_fb_node {
+	struct list_head list;
+	void *fb;
+};
+
+/**
+ * For the same vdec_handle, these functions below are not thread safe.
+ * For different vdec_handle, these functions can be called at the same time.
+ */
+
+/**
+ * vdec_if_init() - initialize decode driver
+ * @ctx      : [in] v4l2 context
+ * @fourcc   : [in] video format fourcc, V4L2_PIX_FMT_H264/VP8/VP9..
+ */
+int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc);
+
+/**
+ * vdec_if_deinit() - deinitialize decode driver
+ * @ctx : [in] v4l2 context
+ *
+ */
+int vdec_if_deinit(struct mtk_vcodec_ctx *ctx);
+
+/**
+ * vdec_if_decode() - trigger decode
+ * @handle  : [in] video decode handle
+ * @bs      : [in] input bitstream
+ * @fb      : [in] frame buffer to store decoded frame
+ * @res_chg : [out] resolution change happen
+ *
+ * while EOF flush decode, need to set input bitstream as NULL
+ */
+int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg);
+
+/**
+ * vdec_if_get_param() - get driver's parameter
+ * @handle : [in] video decode handle
+ * @type   : [in] input parameter type
+ * @out    : [out] buffer to store query result
+ */
+int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
+		      void *out);
+
+#endif
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
new file mode 100644
index 0000000..937192c
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_IPI_MSG_H_
+#define _VDEC_IPI_MSG_H_
+
+/**
+ * enum vdec_ipi_msgid - message id between AP and VPU
+ * @AP_IPIMSG_XXX:		AP to VPU cmd message id
+ * @VPU_IPIMSG_XXX_DONE:	VPU ack AP cmd message id
+ */
+enum vdec_ipi_msgid {
+	AP_IPIMSG_DEC_INIT = 0xA000,
+	AP_IPIMSG_DEC_START = 0xA001,
+	AP_IPIMSG_DEC_END = 0xA002,
+	AP_IPIMSG_DEC_DEINIT = 0xA003,
+	AP_IPIMSG_DEC_RESET = 0xA004,
+
+	VPU_IPIMSG_DEC_INIT_ACK = 0xB000,
+	VPU_IPIMSG_DEC_START_ACK = 0xB001,
+	VPU_IPIMSG_DEC_END_ACK = 0xB002,
+	VPU_IPIMSG_DEC_DEINIT_ACK = 0xB003,
+	VPU_IPIMSG_DEC_RESET_ACK = 0xB004,
+};
+
+/**
+ * struct vdec_ap_ipi_cmd - generic AP to VPU ipi command format
+ * @msg_id		: vdec_ipi_msgid
+ * @vpu_inst_addr	: VPU decoder instance address
+ */
+struct vdec_ap_ipi_cmd {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+};
+
+/**
+ * struct vdec_vpu_ipi_ack - generic VPU to AP ipi command format
+ * @msg_id      : vdec_ipi_msgid
+ * @status      : VPU exeuction result
+ * @vdec_inst   : AP video decoder instance address
+ */
+struct vdec_vpu_ipi_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_ap_ipi_init - for AP_IPIMSG_DEC_INIT
+ * @msg_id        : AP_IPIMSG_DEC_INIT
+ * @reserved      : Reserved field
+ * @vdec_inst     : AP video decoder instance address
+ */
+struct vdec_ap_ipi_init {
+	uint32_t msg_id;
+	uint32_t reserved;
+	uint64_t vdec_inst;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
+ * @status        : VPU exeuction result
+ * @vdec_inst     : AP video decoder instance address
+ * @vpu_inst_addr : VPU decoder instance address
+ */
+struct vdec_vpu_ipi_init_ack {
+	uint32_t msg_id;
+	int32_t status;
+	uint64_t vdec_inst;
+	uint32_t vpu_inst_addr;
+};
+
+#endif
-- 
1.7.9.5

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

* [PATCH 4/7] [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
  2016-04-13 12:01       ` Tiffany Lin
  (?)
@ 2016-04-13 12:01         ` Tiffany Lin
  -1 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin, PC Chen

Add h264 driver for MT8173

Signed-off-by: PC Chen <pc.chen@mediatek.com>
Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    5 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    4 +
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   28 +-
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++++++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +++
 6 files changed, 739 insertions(+), 9 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 4c8ed2f..58243ed 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,11 +1,12 @@
 
-
 obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-enc.o \
 				       mtk-vcodec-common.o
 
-mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
+		vdec_vpu_if.o \
 		mtk_vcodec_dec.o \
 		mtk_vcodec_dec_pm.o \
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
new file mode 100644
index 0000000..9762bbf
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
@@ -0,0 +1,487 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define NON_IDR_SLICE				0x01
+#define IDR_SLICE				0x05
+#define H264_PPS				0x08
+
+#define BUF_PREDICTION_SZ			(32 * 1024)
+#define BUF_PP_SZ				(30 * 4096)
+#define BUF_LD_SZ				(15 * 4096)
+
+#define MB_UNIT_SZ				16
+#define HW_MB_STORE_SZ				64
+
+#define H264_MAX_FB_NUM				17
+#define HDR_PARSING_BUF_SZ			1024
+#define NAL_TYPE(value)				((value) & 0x1F)
+
+/**
+ * struct h264_fb - h264 decode frame buffer information
+ * @vdec_fb_va  : virtual address of struct vdec_fb
+ * @y_fb_dma    : dma address of Y frame buffer
+ * @c_fb_dma    : dma address of C frame buffer
+ * @poc         : picture order count of frame buffer
+ * @reserved    : for 8 bytes alignment
+ */
+struct h264_fb {
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	int32_t poc;
+	uint32_t reserved;
+};
+
+/**
+ * struct h264_ring_fb_list - ring frame buffer list
+ * @fb_list   : frame buffer arrary
+ * @read_idx  : read index
+ * @write_idx : write index
+ * @count     : buffer count in list
+ */
+struct h264_ring_fb_list {
+	struct h264_fb fb_list[H264_MAX_FB_NUM];
+	unsigned int read_idx;
+	unsigned int write_idx;
+	unsigned int count;
+	unsigned int reserved;
+};
+
+/**
+ * struct vdec_h264_dec_info - decode information
+ * @dpb_sz		: decoding picture buffer size
+ * @resolution_changed  : resoltion change happen
+ * @realloc_mv_buf	: flag to notify driver to re-allocate mv buffer
+ * @reserved		: for 8 bytes alignment
+ * @bs_dma		: Input bit-stream buffer dma address
+ * @y_fb_dma		: Y frame buffer dma address
+ * @c_fb_dma		: C frame buffer dma address
+ * @vdec_fb_va		: VDEC frame buffer struct virtual address
+ */
+struct vdec_h264_dec_info {
+	uint32_t dpb_sz;
+	uint32_t resolution_changed;
+	uint32_t realloc_mv_buf;
+	uint32_t reserved;
+	uint64_t bs_dma;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	uint64_t vdec_fb_va;
+};
+
+/**
+ * struct vdec_h264_vsi - shared memory for decode information exchange
+ *                        between VPU and Host.
+ *                        The memory is allocated by VPU and mapping to Host
+ *                        in vpu_dec_init()
+ * @hdr_buf     : Header parsing buffer
+ * @ppl_buf_dma : HW working buffer ppl dma address
+ * @mv_buf_dma  : HW working buffer mv dma address
+ * @list_free   : free frame buffer ring list
+ * @list_disp   : display frame buffer ring list
+ * @dec		: decode information
+ * @pic		: picture information
+ * @crop        : crop information
+ */
+struct vdec_h264_vsi {
+	unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
+	uint64_t ppl_buf_dma;
+	uint64_t mv_buf_dma[H264_MAX_FB_NUM];
+	struct h264_ring_fb_list list_free;
+	struct h264_ring_fb_list list_disp;
+	struct vdec_h264_dec_info dec;
+	struct vdec_pic_info pic;
+	struct v4l2_rect crop;
+};
+
+/**
+ * struct vdec_h264_inst - h264 decoder instance
+ * @num_nalu : how many nalus be decoded
+ * @ctx      : point to mtk_vcodec_ctx
+ * @ppl_buf  : HW working buffer for ppl
+ * @mv_buf   : HW working buffer for mv
+ * @vpu      : VPU instance
+ * @vsi      : VPU shared information
+ */
+struct vdec_h264_inst {
+	unsigned int num_nalu;
+	struct mtk_vcodec_ctx *ctx;
+	struct mtk_vcodec_mem ppl_buf;
+	struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
+	struct vdec_vpu_inst vpu;
+	struct vdec_h264_vsi *vsi;
+};
+
+static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
+{
+	return HW_MB_STORE_SZ * (width/MB_UNIT_SZ) * (height/MB_UNIT_SZ);
+}
+
+static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
+{
+	int i;
+	int err;
+	struct mtk_vcodec_mem *mem = NULL;
+	unsigned int buf_sz;
+
+	buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+		mem->size = buf_sz;
+		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+		if (err) {
+			mtk_vcodec_err(inst, "failed to allocate mv buf");
+			return -ENOMEM;
+		}
+		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
+	}
+
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_h264_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem = NULL;
+
+	mtk_vcodec_debug_enter(inst);
+
+	inst->vsi->ppl_buf_dma = 0;
+	mem = &inst->ppl_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		inst->vsi->mv_buf_dma[i] = 0;
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+	}
+}
+
+static int allocate_working_buf(struct vdec_h264_inst *inst)
+{
+	int err = 0;
+
+	inst->ppl_buf.size = BUF_PREDICTION_SZ + BUF_PP_SZ + BUF_LD_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, &inst->ppl_buf);
+	if (err) {
+		mtk_vcodec_err(inst, "failed to allocate ppl buf");
+		return -ENOMEM;
+	}
+
+	inst->vsi->ppl_buf_dma = inst->ppl_buf.dma_addr;
+	return 0;
+}
+
+static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
+{
+	struct h264_ring_fb_list *list;
+
+	if (fb) {
+		list = &inst->vsi->list_free;
+		if (list->count == H264_MAX_FB_NUM) {
+			mtk_vcodec_err(inst, "[FB] put fb free_list full");
+			return;
+		}
+
+		mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
+				 fb->base_y.va, (u64)fb->base_y.dma_addr);
+
+		list->fb_list[list->write_idx].vdec_fb_va = (u64)fb;
+		list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
+				  0 : list->write_idx + 1;
+		list->count++;
+	}
+}
+
+static void get_pic_info(struct vdec_h264_inst *inst,
+			 struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left	= inst->vsi->crop.left;
+	cr->c.top	= inst->vsi->crop.top;
+	cr->c.width	= inst->vsi->crop.width;
+	cr->c.height	= inst->vsi->crop.height;
+	mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
+{
+	*dpb_sz = inst->vsi->dec.dpb_sz;
+	mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
+}
+
+static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_h264_inst *inst = NULL;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_H264;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
+	err = allocate_working_buf(inst);
+	if (err)
+		goto error_free_inst;
+
+	mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_inst:
+	kfree(inst);
+
+	return err;
+}
+
+static int vdec_h264_deinit(unsigned long h_vdec)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+
+	kfree(inst);
+	return 0;
+}
+
+static int find_start_code(unsigned char *data, unsigned int data_sz)
+{
+	if (data_sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
+		return 3;
+
+	if (data_sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
+	    data[3] == 1)
+		return 4;
+
+	return -1;
+}
+
+static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			    struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	int idx = 0;
+	int err = 0;
+	unsigned int nal_start;
+	unsigned int nal_type;
+	unsigned char *buf;
+	unsigned int buf_sz;
+	unsigned int data[2];
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	vdec_fb_va = (u64)fb;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
+			 inst->num_nalu, y_fb_dma, c_fb_dma, fb);
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL)
+		return vpu_dec_reset(vpu);
+
+	buf = (unsigned char *)bs->va;
+	buf_sz = bs->size;
+	idx = find_start_code(buf, buf_sz);
+	if (idx < 0)
+		goto err_free_fb_out;
+
+	nal_start = buf[idx];
+	nal_type = NAL_TYPE(buf[idx]);
+	mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
+			 nal_type);
+
+	if (nal_type == H264_PPS) {
+		if (buf_sz > HDR_PARSING_BUF_SZ) {
+			err = -EILSEQ;
+			goto err_free_fb_out;
+		}
+		buf_sz -= idx;
+		memcpy(inst->vsi->hdr_buf, buf + idx, buf_sz);
+	}
+
+	inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
+	inst->vsi->dec.y_fb_dma = y_fb_dma;
+	inst->vsi->dec.c_fb_dma = c_fb_dma;
+	inst->vsi->dec.vdec_fb_va = vdec_fb_va;
+
+	data[0] = buf_sz;
+	data[1] = nal_start;
+	err = vpu_dec_start(vpu, data, 2);
+	if (err)
+		goto err_free_fb_out;
+
+	*res_chg = inst->vsi->dec.resolution_changed;
+	if (*res_chg) {
+		struct vdec_pic_info pic;
+
+		mtk_vcodec_debug(inst, "- resolution changed -");
+		get_pic_info(inst, &pic);
+
+		if (inst->vsi->dec.realloc_mv_buf) {
+			err = alloc_mv_buf(inst, &pic);
+			if (err)
+				goto err_free_fb_out;
+		}
+	}
+
+	if (nal_type == NON_IDR_SLICE || nal_type == IDR_SLICE) {
+		/* wait decoder done interrupt */
+		err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						   MTK_INST_IRQ_RECEIVED,
+						   WAIT_INTR_TIMEOUT_MS);
+		if (err)
+			goto err_free_fb_out;
+
+		vpu_dec_end(vpu);
+	}
+
+	mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
+			 nal_type);
+	inst->num_nalu++;
+	return 0;
+
+err_free_fb_out:
+	put_fb_to_free(inst, fb);
+	mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
+	return err;
+}
+
+static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
+			     struct h264_ring_fb_list *list,
+			     bool disp_list, struct vdec_fb **out_fb)
+{
+	unsigned long vdec_fb_va;
+	struct vdec_fb *fb;
+
+	if (list->count == 0) {
+		mtk_vcodec_debug(inst, "[FB] there is no %s fb",
+				 disp_list ? "disp" : "free");
+		*out_fb = NULL;
+		return;
+	}
+
+	vdec_fb_va = (unsigned long)list->fb_list[list->read_idx].vdec_fb_va;
+	fb = (struct vdec_fb *)vdec_fb_va;
+	if (disp_list)
+		fb->status |= FB_ST_DISPLAY;
+	else
+		fb->status |= FB_ST_FREE;
+
+	*out_fb = fb;
+	mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
+			 disp_list ? "disp" : "free",
+			 fb->status, list->fb_list[list->read_idx].poc,
+			 list->fb_list[list->read_idx].vdec_fb_va);
+
+	list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
+			 0 : list->read_idx + 1;
+	list->count--;
+}
+
+static int vdec_h264_get_param(unsigned long h_vdec,
+			       enum vdec_get_param_type type, void *out)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		get_dpb_size(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_h264_if = {
+	vdec_h264_init,
+	vdec_h264_decode,
+	vdec_h264_get_param,
+	vdec_h264_deinit,
+};
+
+struct vdec_common_if *get_h264_dec_comm_if(void)
+{
+	return &vdec_h264_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 1d12719..576920c 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -24,12 +24,16 @@
 #include "mtk_vcodec_util.h"
 #include "mtk_vpu.h"
 
+struct vdec_common_if *get_h264_dec_comm_if(void);
+
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
 	int ret = 0;
 
 	switch (fourcc) {
 	case V4L2_PIX_FMT_H264:
+		ctx->dec_if = get_h264_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP8:
 	case V4L2_PIX_FMT_VP9:
 	default:
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
index 937192c..131928b 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -54,7 +54,7 @@ struct vdec_ap_ipi_cmd {
 struct vdec_vpu_ipi_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
@@ -66,20 +66,34 @@ struct vdec_vpu_ipi_ack {
 struct vdec_ap_ipi_init {
 	uint32_t msg_id;
 	uint32_t reserved;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
- * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
- * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
- * @status        : VPU exeuction result
- * @vdec_inst     : AP video decoder instance address
+ * struct vdec_ap_ipi_dec_start - for AP_IPIMSG_DEC_START
+ * @msg_id        : AP_IPIMSG_DEC_START
  * @vpu_inst_addr : VPU decoder instance address
+ * @data          : Header info
+ * @reserved      : Reserved field
+ */
+struct vdec_ap_ipi_dec_start {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+	uint32_t data[3];
+	uint32_t reserved;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id		: VPU_IPIMSG_DEC_INIT_ACK
+ * @status		: VPU exeuction result
+ * @ap_inst_addr	: AP vcodec_vpu_inst instance address
+ * @vpu_inst_addr	: VPU decoder instance address
  */
 struct vdec_vpu_ipi_init_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 	uint32_t vpu_inst_addr;
 };
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
new file mode 100644
index 0000000..33b96db
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_ipi_msg.h"
+#include "vdec_vpu_if.h"
+
+static void handle_init_ack_msg(struct vdec_vpu_ipi_init_ack *msg)
+{
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
+
+	/* mapping VPU address to kernel virtual address */
+	vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
+	vpu->inst_addr = msg->vpu_inst_addr;
+
+	mtk_vcodec_debug(vpu, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
+}
+
+/*
+ * This function runs in interrupt context and it means there's a IPI MSG
+ * from VPU.
+ */
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
+{
+	struct vdec_vpu_ipi_ack *msg = data;
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg->msg_id);
+
+	vpu->failure = msg->status;
+
+	if (msg->status == 0) {
+		switch (msg->msg_id) {
+		case VPU_IPIMSG_DEC_INIT_ACK:
+			handle_init_ack_msg(data);
+			break;
+
+		case VPU_IPIMSG_DEC_START_ACK:
+		case VPU_IPIMSG_DEC_END_ACK:
+		case VPU_IPIMSG_DEC_DEINIT_ACK:
+		case VPU_IPIMSG_DEC_RESET_ACK:
+			break;
+
+		default:
+			mtk_vcodec_err(vpu, "invalid msg=%X", msg->msg_id);
+			break;
+		}
+	}
+
+	mtk_vcodec_debug(vpu, "- id=%X", msg->msg_id);
+	vpu->signaled = 1;
+}
+
+static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
+{
+	int err;
+
+	mtk_vcodec_debug(vpu, "id=%X", *(unsigned int *)msg);
+
+	vpu->failure = 0;
+	vpu->signaled = 0;
+
+	err = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
+	if (err) {
+		mtk_vcodec_err(vpu, "vpu_ipi_send fail status=%d", err);
+		return err;
+	}
+
+	return vpu->failure;
+}
+
+static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
+{
+	struct vdec_ap_ipi_cmd msg;
+	int err = 0;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = msg_id;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
+	return err;
+}
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu)
+{
+	struct vdec_ap_ipi_init msg;
+	int err;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	init_waitqueue_head(&vpu->wq);
+	vpu->signaled = 0;
+	vpu->failure = 0;
+
+	err = vpu_ipi_register(vpu->dev, vpu->id, vpu->handler, NULL, NULL);
+	if (err != 0) {
+		mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
+		return err;
+	}
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_INIT;
+	msg.ap_inst_addr = (unsigned long)vpu;
+
+	mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length)
+{
+	struct vdec_ap_ipi_dec_start msg;
+	int i;
+	int err = 0;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_START;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	for (i = 0; i < length; i++)
+		msg.data[i] = data[i];
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_end(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
+}
+
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
+}
+
+int vpu_dec_reset(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
new file mode 100644
index 0000000..c16cd75
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_VPU_IF_H_
+#define _VDEC_VPU_IF_H_
+
+#include "mtk_vpu.h"
+
+/**
+ * struct vdec_vpu_inst - VPU instance for video codec
+ * @dev	        : platform device of VPU
+ * @inst_addr	: VPU decoder instance addr
+ * @signaled    : 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure     : VPU execution result status
+ * @wq          : Wait queue to wait VPU message ack
+ */
+struct vdec_vpu_inst {
+	enum ipi_id id;
+	void *vsi;
+	int failure;
+	unsigned int inst_addr;
+	unsigned int signaled;
+	struct mtk_vcodec_ctx *ctx;
+	struct platform_device *dev;
+	wait_queue_head_t wq;
+	ipi_handler_t handler;
+};
+
+/*
+ * Note these functions are not thread-safe for the same decoder instance.
+ * the reason is |signaled|. In vdec_vpu_wait_ack,
+ * wait_event_interruptible_timeout waits |signaled| to be 1.
+ * Suppose wait_event_interruptible_timeout returns and the execution has not
+ * reached line 127. If another thread calls vpu_dec_end,
+ * |signaled| will be 1 and wait_event_interruptible_timeout will return
+ *  immediately. We enusure thread-safe to add mtk_vdec_lock()/unlock() in
+ * vdec_drv_if.c
+ *
+ */
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu);
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length);
+int vpu_dec_end(struct vdec_vpu_inst *vpu);
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu);
+int vpu_dec_reset(struct vdec_vpu_inst *vpu);
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv);
+
+#endif
-- 
1.7.9.5

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

* [PATCH 4/7] [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
@ 2016-04-13 12:01         ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: devicetree, Tiffany Lin, linux-kernel, linux-mediatek,
	PoChun.Lin, Yingjoe Chen, Eddie Huang, PC Chen, linux-arm-kernel,
	linux-media

Add h264 driver for MT8173

Signed-off-by: PC Chen <pc.chen@mediatek.com>
Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    5 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    4 +
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   28 +-
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++++++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +++
 6 files changed, 739 insertions(+), 9 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 4c8ed2f..58243ed 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,11 +1,12 @@
 
-
 obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-enc.o \
 				       mtk-vcodec-common.o
 
-mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
+		vdec_vpu_if.o \
 		mtk_vcodec_dec.o \
 		mtk_vcodec_dec_pm.o \
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
new file mode 100644
index 0000000..9762bbf
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
@@ -0,0 +1,487 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define NON_IDR_SLICE				0x01
+#define IDR_SLICE				0x05
+#define H264_PPS				0x08
+
+#define BUF_PREDICTION_SZ			(32 * 1024)
+#define BUF_PP_SZ				(30 * 4096)
+#define BUF_LD_SZ				(15 * 4096)
+
+#define MB_UNIT_SZ				16
+#define HW_MB_STORE_SZ				64
+
+#define H264_MAX_FB_NUM				17
+#define HDR_PARSING_BUF_SZ			1024
+#define NAL_TYPE(value)				((value) & 0x1F)
+
+/**
+ * struct h264_fb - h264 decode frame buffer information
+ * @vdec_fb_va  : virtual address of struct vdec_fb
+ * @y_fb_dma    : dma address of Y frame buffer
+ * @c_fb_dma    : dma address of C frame buffer
+ * @poc         : picture order count of frame buffer
+ * @reserved    : for 8 bytes alignment
+ */
+struct h264_fb {
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	int32_t poc;
+	uint32_t reserved;
+};
+
+/**
+ * struct h264_ring_fb_list - ring frame buffer list
+ * @fb_list   : frame buffer arrary
+ * @read_idx  : read index
+ * @write_idx : write index
+ * @count     : buffer count in list
+ */
+struct h264_ring_fb_list {
+	struct h264_fb fb_list[H264_MAX_FB_NUM];
+	unsigned int read_idx;
+	unsigned int write_idx;
+	unsigned int count;
+	unsigned int reserved;
+};
+
+/**
+ * struct vdec_h264_dec_info - decode information
+ * @dpb_sz		: decoding picture buffer size
+ * @resolution_changed  : resoltion change happen
+ * @realloc_mv_buf	: flag to notify driver to re-allocate mv buffer
+ * @reserved		: for 8 bytes alignment
+ * @bs_dma		: Input bit-stream buffer dma address
+ * @y_fb_dma		: Y frame buffer dma address
+ * @c_fb_dma		: C frame buffer dma address
+ * @vdec_fb_va		: VDEC frame buffer struct virtual address
+ */
+struct vdec_h264_dec_info {
+	uint32_t dpb_sz;
+	uint32_t resolution_changed;
+	uint32_t realloc_mv_buf;
+	uint32_t reserved;
+	uint64_t bs_dma;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	uint64_t vdec_fb_va;
+};
+
+/**
+ * struct vdec_h264_vsi - shared memory for decode information exchange
+ *                        between VPU and Host.
+ *                        The memory is allocated by VPU and mapping to Host
+ *                        in vpu_dec_init()
+ * @hdr_buf     : Header parsing buffer
+ * @ppl_buf_dma : HW working buffer ppl dma address
+ * @mv_buf_dma  : HW working buffer mv dma address
+ * @list_free   : free frame buffer ring list
+ * @list_disp   : display frame buffer ring list
+ * @dec		: decode information
+ * @pic		: picture information
+ * @crop        : crop information
+ */
+struct vdec_h264_vsi {
+	unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
+	uint64_t ppl_buf_dma;
+	uint64_t mv_buf_dma[H264_MAX_FB_NUM];
+	struct h264_ring_fb_list list_free;
+	struct h264_ring_fb_list list_disp;
+	struct vdec_h264_dec_info dec;
+	struct vdec_pic_info pic;
+	struct v4l2_rect crop;
+};
+
+/**
+ * struct vdec_h264_inst - h264 decoder instance
+ * @num_nalu : how many nalus be decoded
+ * @ctx      : point to mtk_vcodec_ctx
+ * @ppl_buf  : HW working buffer for ppl
+ * @mv_buf   : HW working buffer for mv
+ * @vpu      : VPU instance
+ * @vsi      : VPU shared information
+ */
+struct vdec_h264_inst {
+	unsigned int num_nalu;
+	struct mtk_vcodec_ctx *ctx;
+	struct mtk_vcodec_mem ppl_buf;
+	struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
+	struct vdec_vpu_inst vpu;
+	struct vdec_h264_vsi *vsi;
+};
+
+static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
+{
+	return HW_MB_STORE_SZ * (width/MB_UNIT_SZ) * (height/MB_UNIT_SZ);
+}
+
+static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
+{
+	int i;
+	int err;
+	struct mtk_vcodec_mem *mem = NULL;
+	unsigned int buf_sz;
+
+	buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+		mem->size = buf_sz;
+		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+		if (err) {
+			mtk_vcodec_err(inst, "failed to allocate mv buf");
+			return -ENOMEM;
+		}
+		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
+	}
+
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_h264_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem = NULL;
+
+	mtk_vcodec_debug_enter(inst);
+
+	inst->vsi->ppl_buf_dma = 0;
+	mem = &inst->ppl_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		inst->vsi->mv_buf_dma[i] = 0;
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+	}
+}
+
+static int allocate_working_buf(struct vdec_h264_inst *inst)
+{
+	int err = 0;
+
+	inst->ppl_buf.size = BUF_PREDICTION_SZ + BUF_PP_SZ + BUF_LD_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, &inst->ppl_buf);
+	if (err) {
+		mtk_vcodec_err(inst, "failed to allocate ppl buf");
+		return -ENOMEM;
+	}
+
+	inst->vsi->ppl_buf_dma = inst->ppl_buf.dma_addr;
+	return 0;
+}
+
+static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
+{
+	struct h264_ring_fb_list *list;
+
+	if (fb) {
+		list = &inst->vsi->list_free;
+		if (list->count == H264_MAX_FB_NUM) {
+			mtk_vcodec_err(inst, "[FB] put fb free_list full");
+			return;
+		}
+
+		mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
+				 fb->base_y.va, (u64)fb->base_y.dma_addr);
+
+		list->fb_list[list->write_idx].vdec_fb_va = (u64)fb;
+		list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
+				  0 : list->write_idx + 1;
+		list->count++;
+	}
+}
+
+static void get_pic_info(struct vdec_h264_inst *inst,
+			 struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left	= inst->vsi->crop.left;
+	cr->c.top	= inst->vsi->crop.top;
+	cr->c.width	= inst->vsi->crop.width;
+	cr->c.height	= inst->vsi->crop.height;
+	mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
+{
+	*dpb_sz = inst->vsi->dec.dpb_sz;
+	mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
+}
+
+static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_h264_inst *inst = NULL;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_H264;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
+	err = allocate_working_buf(inst);
+	if (err)
+		goto error_free_inst;
+
+	mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_inst:
+	kfree(inst);
+
+	return err;
+}
+
+static int vdec_h264_deinit(unsigned long h_vdec)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+
+	kfree(inst);
+	return 0;
+}
+
+static int find_start_code(unsigned char *data, unsigned int data_sz)
+{
+	if (data_sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
+		return 3;
+
+	if (data_sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
+	    data[3] == 1)
+		return 4;
+
+	return -1;
+}
+
+static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			    struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	int idx = 0;
+	int err = 0;
+	unsigned int nal_start;
+	unsigned int nal_type;
+	unsigned char *buf;
+	unsigned int buf_sz;
+	unsigned int data[2];
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	vdec_fb_va = (u64)fb;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
+			 inst->num_nalu, y_fb_dma, c_fb_dma, fb);
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL)
+		return vpu_dec_reset(vpu);
+
+	buf = (unsigned char *)bs->va;
+	buf_sz = bs->size;
+	idx = find_start_code(buf, buf_sz);
+	if (idx < 0)
+		goto err_free_fb_out;
+
+	nal_start = buf[idx];
+	nal_type = NAL_TYPE(buf[idx]);
+	mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
+			 nal_type);
+
+	if (nal_type == H264_PPS) {
+		if (buf_sz > HDR_PARSING_BUF_SZ) {
+			err = -EILSEQ;
+			goto err_free_fb_out;
+		}
+		buf_sz -= idx;
+		memcpy(inst->vsi->hdr_buf, buf + idx, buf_sz);
+	}
+
+	inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
+	inst->vsi->dec.y_fb_dma = y_fb_dma;
+	inst->vsi->dec.c_fb_dma = c_fb_dma;
+	inst->vsi->dec.vdec_fb_va = vdec_fb_va;
+
+	data[0] = buf_sz;
+	data[1] = nal_start;
+	err = vpu_dec_start(vpu, data, 2);
+	if (err)
+		goto err_free_fb_out;
+
+	*res_chg = inst->vsi->dec.resolution_changed;
+	if (*res_chg) {
+		struct vdec_pic_info pic;
+
+		mtk_vcodec_debug(inst, "- resolution changed -");
+		get_pic_info(inst, &pic);
+
+		if (inst->vsi->dec.realloc_mv_buf) {
+			err = alloc_mv_buf(inst, &pic);
+			if (err)
+				goto err_free_fb_out;
+		}
+	}
+
+	if (nal_type == NON_IDR_SLICE || nal_type == IDR_SLICE) {
+		/* wait decoder done interrupt */
+		err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						   MTK_INST_IRQ_RECEIVED,
+						   WAIT_INTR_TIMEOUT_MS);
+		if (err)
+			goto err_free_fb_out;
+
+		vpu_dec_end(vpu);
+	}
+
+	mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
+			 nal_type);
+	inst->num_nalu++;
+	return 0;
+
+err_free_fb_out:
+	put_fb_to_free(inst, fb);
+	mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
+	return err;
+}
+
+static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
+			     struct h264_ring_fb_list *list,
+			     bool disp_list, struct vdec_fb **out_fb)
+{
+	unsigned long vdec_fb_va;
+	struct vdec_fb *fb;
+
+	if (list->count == 0) {
+		mtk_vcodec_debug(inst, "[FB] there is no %s fb",
+				 disp_list ? "disp" : "free");
+		*out_fb = NULL;
+		return;
+	}
+
+	vdec_fb_va = (unsigned long)list->fb_list[list->read_idx].vdec_fb_va;
+	fb = (struct vdec_fb *)vdec_fb_va;
+	if (disp_list)
+		fb->status |= FB_ST_DISPLAY;
+	else
+		fb->status |= FB_ST_FREE;
+
+	*out_fb = fb;
+	mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
+			 disp_list ? "disp" : "free",
+			 fb->status, list->fb_list[list->read_idx].poc,
+			 list->fb_list[list->read_idx].vdec_fb_va);
+
+	list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
+			 0 : list->read_idx + 1;
+	list->count--;
+}
+
+static int vdec_h264_get_param(unsigned long h_vdec,
+			       enum vdec_get_param_type type, void *out)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		get_dpb_size(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_h264_if = {
+	vdec_h264_init,
+	vdec_h264_decode,
+	vdec_h264_get_param,
+	vdec_h264_deinit,
+};
+
+struct vdec_common_if *get_h264_dec_comm_if(void)
+{
+	return &vdec_h264_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 1d12719..576920c 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -24,12 +24,16 @@
 #include "mtk_vcodec_util.h"
 #include "mtk_vpu.h"
 
+struct vdec_common_if *get_h264_dec_comm_if(void);
+
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
 	int ret = 0;
 
 	switch (fourcc) {
 	case V4L2_PIX_FMT_H264:
+		ctx->dec_if = get_h264_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP8:
 	case V4L2_PIX_FMT_VP9:
 	default:
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
index 937192c..131928b 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -54,7 +54,7 @@ struct vdec_ap_ipi_cmd {
 struct vdec_vpu_ipi_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
@@ -66,20 +66,34 @@ struct vdec_vpu_ipi_ack {
 struct vdec_ap_ipi_init {
 	uint32_t msg_id;
 	uint32_t reserved;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
- * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
- * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
- * @status        : VPU exeuction result
- * @vdec_inst     : AP video decoder instance address
+ * struct vdec_ap_ipi_dec_start - for AP_IPIMSG_DEC_START
+ * @msg_id        : AP_IPIMSG_DEC_START
  * @vpu_inst_addr : VPU decoder instance address
+ * @data          : Header info
+ * @reserved      : Reserved field
+ */
+struct vdec_ap_ipi_dec_start {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+	uint32_t data[3];
+	uint32_t reserved;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id		: VPU_IPIMSG_DEC_INIT_ACK
+ * @status		: VPU exeuction result
+ * @ap_inst_addr	: AP vcodec_vpu_inst instance address
+ * @vpu_inst_addr	: VPU decoder instance address
  */
 struct vdec_vpu_ipi_init_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 	uint32_t vpu_inst_addr;
 };
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
new file mode 100644
index 0000000..33b96db
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_ipi_msg.h"
+#include "vdec_vpu_if.h"
+
+static void handle_init_ack_msg(struct vdec_vpu_ipi_init_ack *msg)
+{
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
+
+	/* mapping VPU address to kernel virtual address */
+	vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
+	vpu->inst_addr = msg->vpu_inst_addr;
+
+	mtk_vcodec_debug(vpu, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
+}
+
+/*
+ * This function runs in interrupt context and it means there's a IPI MSG
+ * from VPU.
+ */
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
+{
+	struct vdec_vpu_ipi_ack *msg = data;
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg->msg_id);
+
+	vpu->failure = msg->status;
+
+	if (msg->status == 0) {
+		switch (msg->msg_id) {
+		case VPU_IPIMSG_DEC_INIT_ACK:
+			handle_init_ack_msg(data);
+			break;
+
+		case VPU_IPIMSG_DEC_START_ACK:
+		case VPU_IPIMSG_DEC_END_ACK:
+		case VPU_IPIMSG_DEC_DEINIT_ACK:
+		case VPU_IPIMSG_DEC_RESET_ACK:
+			break;
+
+		default:
+			mtk_vcodec_err(vpu, "invalid msg=%X", msg->msg_id);
+			break;
+		}
+	}
+
+	mtk_vcodec_debug(vpu, "- id=%X", msg->msg_id);
+	vpu->signaled = 1;
+}
+
+static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
+{
+	int err;
+
+	mtk_vcodec_debug(vpu, "id=%X", *(unsigned int *)msg);
+
+	vpu->failure = 0;
+	vpu->signaled = 0;
+
+	err = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
+	if (err) {
+		mtk_vcodec_err(vpu, "vpu_ipi_send fail status=%d", err);
+		return err;
+	}
+
+	return vpu->failure;
+}
+
+static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
+{
+	struct vdec_ap_ipi_cmd msg;
+	int err = 0;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = msg_id;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
+	return err;
+}
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu)
+{
+	struct vdec_ap_ipi_init msg;
+	int err;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	init_waitqueue_head(&vpu->wq);
+	vpu->signaled = 0;
+	vpu->failure = 0;
+
+	err = vpu_ipi_register(vpu->dev, vpu->id, vpu->handler, NULL, NULL);
+	if (err != 0) {
+		mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
+		return err;
+	}
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_INIT;
+	msg.ap_inst_addr = (unsigned long)vpu;
+
+	mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length)
+{
+	struct vdec_ap_ipi_dec_start msg;
+	int i;
+	int err = 0;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_START;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	for (i = 0; i < length; i++)
+		msg.data[i] = data[i];
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_end(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
+}
+
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
+}
+
+int vpu_dec_reset(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
new file mode 100644
index 0000000..c16cd75
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_VPU_IF_H_
+#define _VDEC_VPU_IF_H_
+
+#include "mtk_vpu.h"
+
+/**
+ * struct vdec_vpu_inst - VPU instance for video codec
+ * @dev	        : platform device of VPU
+ * @inst_addr	: VPU decoder instance addr
+ * @signaled    : 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure     : VPU execution result status
+ * @wq          : Wait queue to wait VPU message ack
+ */
+struct vdec_vpu_inst {
+	enum ipi_id id;
+	void *vsi;
+	int failure;
+	unsigned int inst_addr;
+	unsigned int signaled;
+	struct mtk_vcodec_ctx *ctx;
+	struct platform_device *dev;
+	wait_queue_head_t wq;
+	ipi_handler_t handler;
+};
+
+/*
+ * Note these functions are not thread-safe for the same decoder instance.
+ * the reason is |signaled|. In vdec_vpu_wait_ack,
+ * wait_event_interruptible_timeout waits |signaled| to be 1.
+ * Suppose wait_event_interruptible_timeout returns and the execution has not
+ * reached line 127. If another thread calls vpu_dec_end,
+ * |signaled| will be 1 and wait_event_interruptible_timeout will return
+ *  immediately. We enusure thread-safe to add mtk_vdec_lock()/unlock() in
+ * vdec_drv_if.c
+ *
+ */
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu);
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length);
+int vpu_dec_end(struct vdec_vpu_inst *vpu);
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu);
+int vpu_dec_reset(struct vdec_vpu_inst *vpu);
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv);
+
+#endif
-- 
1.7.9.5

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

* [PATCH 4/7] [media] vcodec: mediatek: Add Mediatek H264 Video Decoder Driver
@ 2016-04-13 12:01         ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add h264 driver for MT8173

Signed-off-by: PC Chen <pc.chen@mediatek.com>
Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    5 +-
 .../media/platform/mtk-vcodec/vdec/vdec_h264_if.c  |  487 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    4 +
 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   28 +-
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c    |  164 +++++++
 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h    |   60 +++
 6 files changed, 739 insertions(+), 9 deletions(-)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec_vpu_if.h

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 4c8ed2f..58243ed 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -1,11 +1,12 @@
 
-
 obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-enc.o \
 				       mtk-vcodec-common.o
 
-mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
+mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
+		vdec_vpu_if.o \
 		mtk_vcodec_dec.o \
 		mtk_vcodec_dec_pm.o \
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
new file mode 100644
index 0000000..9762bbf
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_h264_if.c
@@ -0,0 +1,487 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define NON_IDR_SLICE				0x01
+#define IDR_SLICE				0x05
+#define H264_PPS				0x08
+
+#define BUF_PREDICTION_SZ			(32 * 1024)
+#define BUF_PP_SZ				(30 * 4096)
+#define BUF_LD_SZ				(15 * 4096)
+
+#define MB_UNIT_SZ				16
+#define HW_MB_STORE_SZ				64
+
+#define H264_MAX_FB_NUM				17
+#define HDR_PARSING_BUF_SZ			1024
+#define NAL_TYPE(value)				((value) & 0x1F)
+
+/**
+ * struct h264_fb - h264 decode frame buffer information
+ * @vdec_fb_va  : virtual address of struct vdec_fb
+ * @y_fb_dma    : dma address of Y frame buffer
+ * @c_fb_dma    : dma address of C frame buffer
+ * @poc         : picture order count of frame buffer
+ * @reserved    : for 8 bytes alignment
+ */
+struct h264_fb {
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	int32_t poc;
+	uint32_t reserved;
+};
+
+/**
+ * struct h264_ring_fb_list - ring frame buffer list
+ * @fb_list   : frame buffer arrary
+ * @read_idx  : read index
+ * @write_idx : write index
+ * @count     : buffer count in list
+ */
+struct h264_ring_fb_list {
+	struct h264_fb fb_list[H264_MAX_FB_NUM];
+	unsigned int read_idx;
+	unsigned int write_idx;
+	unsigned int count;
+	unsigned int reserved;
+};
+
+/**
+ * struct vdec_h264_dec_info - decode information
+ * @dpb_sz		: decoding picture buffer size
+ * @resolution_changed  : resoltion change happen
+ * @realloc_mv_buf	: flag to notify driver to re-allocate mv buffer
+ * @reserved		: for 8 bytes alignment
+ * @bs_dma		: Input bit-stream buffer dma address
+ * @y_fb_dma		: Y frame buffer dma address
+ * @c_fb_dma		: C frame buffer dma address
+ * @vdec_fb_va		: VDEC frame buffer struct virtual address
+ */
+struct vdec_h264_dec_info {
+	uint32_t dpb_sz;
+	uint32_t resolution_changed;
+	uint32_t realloc_mv_buf;
+	uint32_t reserved;
+	uint64_t bs_dma;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+	uint64_t vdec_fb_va;
+};
+
+/**
+ * struct vdec_h264_vsi - shared memory for decode information exchange
+ *                        between VPU and Host.
+ *                        The memory is allocated by VPU and mapping to Host
+ *                        in vpu_dec_init()
+ * @hdr_buf     : Header parsing buffer
+ * @ppl_buf_dma : HW working buffer ppl dma address
+ * @mv_buf_dma  : HW working buffer mv dma address
+ * @list_free   : free frame buffer ring list
+ * @list_disp   : display frame buffer ring list
+ * @dec		: decode information
+ * @pic		: picture information
+ * @crop        : crop information
+ */
+struct vdec_h264_vsi {
+	unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
+	uint64_t ppl_buf_dma;
+	uint64_t mv_buf_dma[H264_MAX_FB_NUM];
+	struct h264_ring_fb_list list_free;
+	struct h264_ring_fb_list list_disp;
+	struct vdec_h264_dec_info dec;
+	struct vdec_pic_info pic;
+	struct v4l2_rect crop;
+};
+
+/**
+ * struct vdec_h264_inst - h264 decoder instance
+ * @num_nalu : how many nalus be decoded
+ * @ctx      : point to mtk_vcodec_ctx
+ * @ppl_buf  : HW working buffer for ppl
+ * @mv_buf   : HW working buffer for mv
+ * @vpu      : VPU instance
+ * @vsi      : VPU shared information
+ */
+struct vdec_h264_inst {
+	unsigned int num_nalu;
+	struct mtk_vcodec_ctx *ctx;
+	struct mtk_vcodec_mem ppl_buf;
+	struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
+	struct vdec_vpu_inst vpu;
+	struct vdec_h264_vsi *vsi;
+};
+
+static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
+{
+	return HW_MB_STORE_SZ * (width/MB_UNIT_SZ) * (height/MB_UNIT_SZ);
+}
+
+static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
+{
+	int i;
+	int err;
+	struct mtk_vcodec_mem *mem = NULL;
+	unsigned int buf_sz;
+
+	buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+		mem->size = buf_sz;
+		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+		if (err) {
+			mtk_vcodec_err(inst, "failed to allocate mv buf");
+			return -ENOMEM;
+		}
+		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
+	}
+
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_h264_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem = NULL;
+
+	mtk_vcodec_debug_enter(inst);
+
+	inst->vsi->ppl_buf_dma = 0;
+	mem = &inst->ppl_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	for (i = 0; i < H264_MAX_FB_NUM; i++) {
+		inst->vsi->mv_buf_dma[i] = 0;
+		mem = &inst->mv_buf[i];
+		if (mem->va)
+			mtk_vcodec_mem_free(inst->ctx, mem);
+	}
+}
+
+static int allocate_working_buf(struct vdec_h264_inst *inst)
+{
+	int err = 0;
+
+	inst->ppl_buf.size = BUF_PREDICTION_SZ + BUF_PP_SZ + BUF_LD_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, &inst->ppl_buf);
+	if (err) {
+		mtk_vcodec_err(inst, "failed to allocate ppl buf");
+		return -ENOMEM;
+	}
+
+	inst->vsi->ppl_buf_dma = inst->ppl_buf.dma_addr;
+	return 0;
+}
+
+static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
+{
+	struct h264_ring_fb_list *list;
+
+	if (fb) {
+		list = &inst->vsi->list_free;
+		if (list->count == H264_MAX_FB_NUM) {
+			mtk_vcodec_err(inst, "[FB] put fb free_list full");
+			return;
+		}
+
+		mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
+				 fb->base_y.va, (u64)fb->base_y.dma_addr);
+
+		list->fb_list[list->write_idx].vdec_fb_va = (u64)fb;
+		list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
+				  0 : list->write_idx + 1;
+		list->count++;
+	}
+}
+
+static void get_pic_info(struct vdec_h264_inst *inst,
+			 struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left	= inst->vsi->crop.left;
+	cr->c.top	= inst->vsi->crop.top;
+	cr->c.width	= inst->vsi->crop.width;
+	cr->c.height	= inst->vsi->crop.height;
+	mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
+{
+	*dpb_sz = inst->vsi->dec.dpb_sz;
+	mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
+}
+
+static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_h264_inst *inst = NULL;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_H264;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
+	err = allocate_working_buf(inst);
+	if (err)
+		goto error_free_inst;
+
+	mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_inst:
+	kfree(inst);
+
+	return err;
+}
+
+static int vdec_h264_deinit(unsigned long h_vdec)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+
+	kfree(inst);
+	return 0;
+}
+
+static int find_start_code(unsigned char *data, unsigned int data_sz)
+{
+	if (data_sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
+		return 3;
+
+	if (data_sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
+	    data[3] == 1)
+		return 4;
+
+	return -1;
+}
+
+static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			    struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	int idx = 0;
+	int err = 0;
+	unsigned int nal_start;
+	unsigned int nal_type;
+	unsigned char *buf;
+	unsigned int buf_sz;
+	unsigned int data[2];
+	uint64_t vdec_fb_va;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	vdec_fb_va = (u64)fb;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
+			 inst->num_nalu, y_fb_dma, c_fb_dma, fb);
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL)
+		return vpu_dec_reset(vpu);
+
+	buf = (unsigned char *)bs->va;
+	buf_sz = bs->size;
+	idx = find_start_code(buf, buf_sz);
+	if (idx < 0)
+		goto err_free_fb_out;
+
+	nal_start = buf[idx];
+	nal_type = NAL_TYPE(buf[idx]);
+	mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
+			 nal_type);
+
+	if (nal_type == H264_PPS) {
+		if (buf_sz > HDR_PARSING_BUF_SZ) {
+			err = -EILSEQ;
+			goto err_free_fb_out;
+		}
+		buf_sz -= idx;
+		memcpy(inst->vsi->hdr_buf, buf + idx, buf_sz);
+	}
+
+	inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
+	inst->vsi->dec.y_fb_dma = y_fb_dma;
+	inst->vsi->dec.c_fb_dma = c_fb_dma;
+	inst->vsi->dec.vdec_fb_va = vdec_fb_va;
+
+	data[0] = buf_sz;
+	data[1] = nal_start;
+	err = vpu_dec_start(vpu, data, 2);
+	if (err)
+		goto err_free_fb_out;
+
+	*res_chg = inst->vsi->dec.resolution_changed;
+	if (*res_chg) {
+		struct vdec_pic_info pic;
+
+		mtk_vcodec_debug(inst, "- resolution changed -");
+		get_pic_info(inst, &pic);
+
+		if (inst->vsi->dec.realloc_mv_buf) {
+			err = alloc_mv_buf(inst, &pic);
+			if (err)
+				goto err_free_fb_out;
+		}
+	}
+
+	if (nal_type == NON_IDR_SLICE || nal_type == IDR_SLICE) {
+		/* wait decoder done interrupt */
+		err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						   MTK_INST_IRQ_RECEIVED,
+						   WAIT_INTR_TIMEOUT_MS);
+		if (err)
+			goto err_free_fb_out;
+
+		vpu_dec_end(vpu);
+	}
+
+	mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
+			 nal_type);
+	inst->num_nalu++;
+	return 0;
+
+err_free_fb_out:
+	put_fb_to_free(inst, fb);
+	mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
+	return err;
+}
+
+static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
+			     struct h264_ring_fb_list *list,
+			     bool disp_list, struct vdec_fb **out_fb)
+{
+	unsigned long vdec_fb_va;
+	struct vdec_fb *fb;
+
+	if (list->count == 0) {
+		mtk_vcodec_debug(inst, "[FB] there is no %s fb",
+				 disp_list ? "disp" : "free");
+		*out_fb = NULL;
+		return;
+	}
+
+	vdec_fb_va = (unsigned long)list->fb_list[list->read_idx].vdec_fb_va;
+	fb = (struct vdec_fb *)vdec_fb_va;
+	if (disp_list)
+		fb->status |= FB_ST_DISPLAY;
+	else
+		fb->status |= FB_ST_FREE;
+
+	*out_fb = fb;
+	mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
+			 disp_list ? "disp" : "free",
+			 fb->status, list->fb_list[list->read_idx].poc,
+			 list->fb_list[list->read_idx].vdec_fb_va);
+
+	list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
+			 0 : list->read_idx + 1;
+	list->count--;
+}
+
+static int vdec_h264_get_param(unsigned long h_vdec,
+			       enum vdec_get_param_type type, void *out)
+{
+	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		get_dpb_size(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_h264_if = {
+	vdec_h264_init,
+	vdec_h264_decode,
+	vdec_h264_get_param,
+	vdec_h264_deinit,
+};
+
+struct vdec_common_if *get_h264_dec_comm_if(void)
+{
+	return &vdec_h264_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 1d12719..576920c 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -24,12 +24,16 @@
 #include "mtk_vcodec_util.h"
 #include "mtk_vpu.h"
 
+struct vdec_common_if *get_h264_dec_comm_if(void);
+
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
 	int ret = 0;
 
 	switch (fourcc) {
 	case V4L2_PIX_FMT_H264:
+		ctx->dec_if = get_h264_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP8:
 	case V4L2_PIX_FMT_VP9:
 	default:
diff --git a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
index 937192c..131928b 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
+++ b/drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
@@ -54,7 +54,7 @@ struct vdec_ap_ipi_cmd {
 struct vdec_vpu_ipi_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
@@ -66,20 +66,34 @@ struct vdec_vpu_ipi_ack {
 struct vdec_ap_ipi_init {
 	uint32_t msg_id;
 	uint32_t reserved;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 };
 
 /**
- * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
- * @msg_id        : VPU_IPIMSG_DEC_INIT_ACK
- * @status        : VPU exeuction result
- * @vdec_inst     : AP video decoder instance address
+ * struct vdec_ap_ipi_dec_start - for AP_IPIMSG_DEC_START
+ * @msg_id        : AP_IPIMSG_DEC_START
  * @vpu_inst_addr : VPU decoder instance address
+ * @data          : Header info
+ * @reserved      : Reserved field
+ */
+struct vdec_ap_ipi_dec_start {
+	uint32_t msg_id;
+	uint32_t vpu_inst_addr;
+	uint32_t data[3];
+	uint32_t reserved;
+};
+
+/**
+ * struct vdec_vpu_ipi_init_ack - for VPU_IPIMSG_DEC_INIT_ACK
+ * @msg_id		: VPU_IPIMSG_DEC_INIT_ACK
+ * @status		: VPU exeuction result
+ * @ap_inst_addr	: AP vcodec_vpu_inst instance address
+ * @vpu_inst_addr	: VPU decoder instance address
  */
 struct vdec_vpu_ipi_init_ack {
 	uint32_t msg_id;
 	int32_t status;
-	uint64_t vdec_inst;
+	uint64_t ap_inst_addr;
 	uint32_t vpu_inst_addr;
 };
 
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
new file mode 100644
index 0000000..33b96db
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_drv.h"
+#include "mtk_vcodec_util.h"
+#include "vdec_ipi_msg.h"
+#include "vdec_vpu_if.h"
+
+static void handle_init_ack_msg(struct vdec_vpu_ipi_init_ack *msg)
+{
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
+
+	/* mapping VPU address to kernel virtual address */
+	vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
+	vpu->inst_addr = msg->vpu_inst_addr;
+
+	mtk_vcodec_debug(vpu, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
+}
+
+/*
+ * This function runs in interrupt context and it means there's a IPI MSG
+ * from VPU.
+ */
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
+{
+	struct vdec_vpu_ipi_ack *msg = data;
+	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)msg->ap_inst_addr;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg->msg_id);
+
+	vpu->failure = msg->status;
+
+	if (msg->status == 0) {
+		switch (msg->msg_id) {
+		case VPU_IPIMSG_DEC_INIT_ACK:
+			handle_init_ack_msg(data);
+			break;
+
+		case VPU_IPIMSG_DEC_START_ACK:
+		case VPU_IPIMSG_DEC_END_ACK:
+		case VPU_IPIMSG_DEC_DEINIT_ACK:
+		case VPU_IPIMSG_DEC_RESET_ACK:
+			break;
+
+		default:
+			mtk_vcodec_err(vpu, "invalid msg=%X", msg->msg_id);
+			break;
+		}
+	}
+
+	mtk_vcodec_debug(vpu, "- id=%X", msg->msg_id);
+	vpu->signaled = 1;
+}
+
+static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
+{
+	int err;
+
+	mtk_vcodec_debug(vpu, "id=%X", *(unsigned int *)msg);
+
+	vpu->failure = 0;
+	vpu->signaled = 0;
+
+	err = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
+	if (err) {
+		mtk_vcodec_err(vpu, "vpu_ipi_send fail status=%d", err);
+		return err;
+	}
+
+	return vpu->failure;
+}
+
+static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
+{
+	struct vdec_ap_ipi_cmd msg;
+	int err = 0;
+
+	mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = msg_id;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
+	return err;
+}
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu)
+{
+	struct vdec_ap_ipi_init msg;
+	int err;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	init_waitqueue_head(&vpu->wq);
+	vpu->signaled = 0;
+	vpu->failure = 0;
+
+	err = vpu_ipi_register(vpu->dev, vpu->id, vpu->handler, NULL, NULL);
+	if (err != 0) {
+		mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
+		return err;
+	}
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_INIT;
+	msg.ap_inst_addr = (unsigned long)vpu;
+
+	mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length)
+{
+	struct vdec_ap_ipi_dec_start msg;
+	int i;
+	int err = 0;
+
+	mtk_vcodec_debug_enter(vpu);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_id = AP_IPIMSG_DEC_START;
+	msg.vpu_inst_addr = vpu->inst_addr;
+
+	for (i = 0; i < length; i++)
+		msg.data[i] = data[i];
+
+	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
+	mtk_vcodec_debug(vpu, "- ret=%d", err);
+	return err;
+}
+
+int vpu_dec_end(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
+}
+
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
+}
+
+int vpu_dec_reset(struct vdec_vpu_inst *vpu)
+{
+	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
new file mode 100644
index 0000000..c16cd75
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec_vpu_if.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _VDEC_VPU_IF_H_
+#define _VDEC_VPU_IF_H_
+
+#include "mtk_vpu.h"
+
+/**
+ * struct vdec_vpu_inst - VPU instance for video codec
+ * @dev	        : platform device of VPU
+ * @inst_addr	: VPU decoder instance addr
+ * @signaled    : 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure     : VPU execution result status
+ * @wq          : Wait queue to wait VPU message ack
+ */
+struct vdec_vpu_inst {
+	enum ipi_id id;
+	void *vsi;
+	int failure;
+	unsigned int inst_addr;
+	unsigned int signaled;
+	struct mtk_vcodec_ctx *ctx;
+	struct platform_device *dev;
+	wait_queue_head_t wq;
+	ipi_handler_t handler;
+};
+
+/*
+ * Note these functions are not thread-safe for the same decoder instance.
+ * the reason is |signaled|. In vdec_vpu_wait_ack,
+ * wait_event_interruptible_timeout waits |signaled| to be 1.
+ * Suppose wait_event_interruptible_timeout returns and the execution has not
+ * reached line 127. If another thread calls vpu_dec_end,
+ * |signaled| will be 1 and wait_event_interruptible_timeout will return
+ *  immediately. We enusure thread-safe to add mtk_vdec_lock()/unlock() in
+ * vdec_drv_if.c
+ *
+ */
+
+int vpu_dec_init(struct vdec_vpu_inst *vpu);
+int vpu_dec_start(struct vdec_vpu_inst *vpu, unsigned int *data,
+		  unsigned int length);
+int vpu_dec_end(struct vdec_vpu_inst *vpu);
+int vpu_dec_deinit(struct vdec_vpu_inst *vpu);
+int vpu_dec_reset(struct vdec_vpu_inst *vpu);
+void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv);
+
+#endif
-- 
1.7.9.5

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

* [PATCH 5/7] [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
@ 2016-04-13 12:01           ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin, PC Chen

Add vp8 decoder driver for MT8173

Signed-off-by: PC Chen <pc.chen@mediatek.com>
Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 634 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 58243ed..7743c81 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-common.o
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		vdec/vdec_vp8_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
new file mode 100644
index 0000000..9b6ef9d
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
@@ -0,0 +1,630 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
+ *	   PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define VP8_VP_WRAP_SZ				(45 * 4096)
+#define VP8_SEGID_DRAM_ADDR			0x3c
+#define VP8_HW_DATA_ADDR			0x93C
+#define VP8_HW_DATA_VALUE			0x940
+#define VP8_BSASET			        0x100
+#define VP8_BSDSET				0x104
+#define VP8_DEC_DATA_PROCESSING_LOOP		96
+
+#define VP8_RW_CKEN_SET				0x0
+#define VP8_RW_DCM_CON				0x18
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_MISC_SYS_SEL			0x84
+#define VP8_RW_MISC_SPEC_CON			0xC8
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_VP8_CTRL				0xA4
+#define VP8_RW_MISC_DCM_CON			0xEC
+#define VP8_RW_MISC_SRST			0xF4
+#define VP8_RW_MISC_FUNC_CON			0xCC
+
+#define VP8_MAX_FRM_BUFF_NUM				5
+#define VP8_HW_DATA_SZ					272
+#define VP8_DEC_DATA_SZ					300
+
+/**
+ * struct vdec_vp8_dec_info - decode misc information
+ * @vp_wrapper_dma    : wrapper buffer dma
+ * @prev_y_dma        : previous decoded frame buffer Y plane address
+ * @cur_y_fb_dma      : current plane Y frame buffer dma
+ * @cur_c_fb_dma      : current plane C frame buffer dma
+ * @bs_dma	      : bitstream dma
+ * @bs_sz	      : current plane C frame buffer dma
+ * @resolution_changed: resolution change flag
+ * @show_frame	      : display this frame or not
+ * @wait_key_frame    : wait key frame coming
+ */
+struct vdec_vp8_dec_info {
+	uint64_t vp_wrapper_dma;
+	uint64_t prev_y_dma;
+	uint64_t cur_y_fb_dma;
+	uint64_t cur_c_fb_dma;
+	uint64_t bs_dma;
+	uint32_t bs_sz;
+	uint32_t resolution_changed;
+	uint32_t show_frame;
+	uint32_t wait_key_frame;
+};
+
+/**
+ * struct vdec_vp8_vsi - VPU shared information
+ * @dec			: decoding information
+ * @pic			: picture information
+ * @dec_data		: decode data
+ * @segid_wrapper_work	: seg id wrapper buffer
+ * @load_data		: flag to indicate reload decode data
+ */
+struct vdec_vp8_vsi {
+	struct vdec_vp8_dec_info dec;
+	struct vdec_pic_info pic;
+	unsigned int dec_data[VP8_DEC_DATA_SZ];
+	unsigned int segid_wrapper_work[VP8_HW_DATA_SZ][4];
+	unsigned int load_data;
+};
+
+/**
+ * struct vdec_vp8_hw_reg_base - HW register base
+ * @sys		: base address for sys
+ * @misc	: base address for misc
+ * @ld		: base address for ld
+ * @top		: base address for top
+ * @cm		: base address for cm
+ * @hwd		: base address for hwd
+ * @hwb		: base address for hwb
+*/
+struct vdec_vp8_hw_reg_base {
+	void __iomem *sys;
+	void __iomem *misc;
+	void __iomem *ld;
+	void __iomem *top;
+	void __iomem *cm;
+	void __iomem *hwd;
+	void __iomem *hwb;
+};
+
+/**
+ * struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
+ * @wq_hd	: Wait queue to wait VPU message ack
+ * @signaled	: 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure	: VPU execution result status
+ * @inst_addr	: VPU decoder instance addr
+ */
+struct vdec_vp8_vpu_inst {
+	wait_queue_head_t wq_hd;
+	int signaled;
+	int failure;
+	unsigned int inst_addr;
+};
+
+/* frame buffer (fb) list
+ * [dec_fb_list]   - decode fb are initialized to 0 and populated in
+ * [dec_use_list]  - fb is set after decode and is moved to this list
+ * [dec_free_list] - fb is not needed for reference will be moved from
+ *		     [dec_use_list] to [dec_free_list] and
+ *		     once user remove fb from [dec_free_list],
+ *		     it is circulated back to [dec_fb_list]
+ * [disp_fb_list]  - display fb are initialized to 0 and populated in
+ * [disp_rdy_list] - fb is set after decode and is moved to this list
+ *                   once user remove fb from [disp_rdy_list] it is
+ *                   circulated back to [disp_fb_list]
+ */
+
+/**
+ * struct vdec_vp8_inst - VP8 decoder instance
+ * @cur_fb		: current frame buffer
+ * @dec_fb		: decode frame buffer node
+ * @disp_fb		: display frame buffer node
+ * @dec_fb_list		: list to store decode frame buffer
+ * @dec_use_list	: list to store frame buffer in use
+ * @dec_free_list	: list to store free frame buffer
+ * @disp_fb_list	: list to store display frame buffer
+ * @disp_rdy_list	: list to store display ready frame buffer
+ * @vp_wrapper_buf	: decoder working buffer
+ * @reg_base		: hw register base address
+ * @frm_cnt		: decode frame count
+ * @ctx			: V4L2 context
+ * @dev			: platform device
+ * @vpu			: VPU instance for decoder
+ * @vsi			: VPU share information
+ */
+struct vdec_vp8_inst {
+	struct vdec_fb *cur_fb;
+	struct vdec_fb_node dec_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct vdec_fb_node disp_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct list_head dec_fb_list;
+	struct list_head dec_use_list;
+	struct list_head dec_free_list;
+	struct list_head disp_fb_list;
+	struct list_head disp_rdy_list;
+	struct mtk_vcodec_mem vp_wrapper_buf;
+	struct vdec_vp8_hw_reg_base reg_base;
+	unsigned int frm_cnt;
+	struct mtk_vcodec_ctx *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp8_vsi *vsi;
+};
+
+static void get_hw_reg_base(struct vdec_vp8_inst *inst)
+{
+	inst->reg_base.top = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_TOP);
+	inst->reg_base.cm = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_CM);
+	inst->reg_base.hwd = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWD);
+	inst->reg_base.sys = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_SYS);
+	inst->reg_base.misc = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_MISC);
+	inst->reg_base.ld = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_LD);
+	inst->reg_base.hwb = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWB);
+}
+
+static void write_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = (1 << 16) + ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = inst->vsi->segid_wrapper_work[i][j];
+			writel(val, cm + VP8_HW_DATA_VALUE);
+		}
+	}
+}
+
+static void read_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = readl(cm + VP8_HW_DATA_VALUE);
+			inst->vsi->segid_wrapper_work[i][j] = val;
+		}
+	}
+}
+
+static void enable_hw_rw_dec_data(struct vdec_vp8_inst *inst)
+{
+	unsigned int reg_val = 0;
+	void __iomem *sys = inst->reg_base.sys;
+	void __iomem *misc = inst->reg_base.misc;
+	void __iomem *ld = inst->reg_base.ld;
+	void __iomem *hwb = inst->reg_base.hwb;
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	writel(0x1, sys + VP8_RW_CKEN_SET);
+	writel(0x101, ld + VP8_WO_VLD_SRST);
+	writel(0x101, hwb + VP8_WO_VLD_SRST);
+
+	writel(1, sys);
+	reg_val = readl(misc + VP8_RW_MISC_SRST);
+	writel((reg_val & 0xFFFFFFFE), misc + VP8_RW_MISC_SRST);
+
+	writel(0x1, misc + VP8_RW_MISC_SYS_SEL);
+	writel(0x17F, misc + VP8_RW_MISC_SPEC_CON);
+	writel(0x71201100, misc + VP8_RW_MISC_FUNC_CON);
+	writel(0x0, ld + VP8_WO_VLD_SRST);
+	writel(0x0, hwb + VP8_WO_VLD_SRST);
+	writel(0x1, sys + VP8_RW_DCM_CON);
+	writel(0x1, misc + VP8_RW_MISC_DCM_CON);
+	writel(0x1, hwd + VP8_RW_VP8_CTRL);
+}
+
+static void store_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int addr = 0, val = 0;
+	void __iomem *hwd = inst->reg_base.hwd;
+	unsigned int *p = &inst->vsi->dec_data[2];
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		for (j = 0; j < 3 ; j++) {
+			val = *p++;
+			writel(val, hwd + VP8_BSDSET);
+		}
+		addr += 4;
+	}
+}
+
+static void load_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i;
+	unsigned int addr = 0;
+	unsigned int *p = &inst->vsi->dec_data[2];
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET) & 0xFFFFFF;
+		addr += 4;
+	}
+}
+
+static void get_pic_info(struct vdec_vp8_inst *inst, struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static int vp8_dec_finish(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node;
+	uint64_t prev_y_dma = inst->vsi->dec.prev_y_dma;
+
+	mtk_vcodec_debug(inst, "prev fb base dma=%llx", prev_y_dma);
+
+	/* put last decode ok frame to dec_free_list */
+	if (0 != prev_y_dma) {
+		list_for_each_entry(node, &inst->dec_use_list, list) {
+			struct vdec_fb *fb = (struct vdec_fb *)node->fb;
+
+			if (prev_y_dma == (unsigned long)fb->base_y.dma_addr) {
+				list_move_tail(&node->list,
+					       &inst->dec_free_list);
+				break;
+			}
+		}
+	}
+
+	/* dec_fb_list -> dec_use_list */
+	node = list_first_entry(&inst->dec_fb_list, struct vdec_fb_node, list);
+	node->fb = inst->cur_fb;
+	list_move_tail(&node->list, &inst->dec_use_list);
+
+	/* disp_fb_list -> disp_rdy_list */
+	if (inst->vsi->dec.show_frame) {
+		node = list_first_entry(&inst->disp_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = inst->cur_fb;
+		list_move_tail(&node->list, &inst->disp_rdy_list);
+	}
+
+	return 0;
+}
+
+static void move_fb_list_use_to_free(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->dec_use_list, list)
+		list_move_tail(&node->list, &inst->dec_free_list);
+}
+
+static void init_list(struct vdec_vp8_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->dec_fb_list);
+	INIT_LIST_HEAD(&inst->dec_use_list);
+	INIT_LIST_HEAD(&inst->dec_free_list);
+	INIT_LIST_HEAD(&inst->disp_fb_list);
+	INIT_LIST_HEAD(&inst->disp_rdy_list);
+
+	for (i = 0; i < VP8_MAX_FRM_BUFF_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list, &inst->dec_fb_list);
+
+		INIT_LIST_HEAD(&inst->disp_fb[i].list);
+		inst->disp_fb[i].fb = NULL;
+		list_add_tail(&inst->disp_fb[i].list, &inst->disp_fb_list);
+	}
+}
+
+static void add_fb_to_free_list(struct vdec_vp8_inst *inst, void *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry(&inst->dec_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->dec_free_list);
+	}
+}
+
+static int alloc_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	int err;
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	mem->size = VP8_VP_WRAP_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (err) {
+		mtk_vcodec_err(inst, "Cannot allocate vp wrapper buffer");
+		return -ENOMEM;
+	}
+
+	inst->vsi->dec.vp_wrapper_dma = (u64)mem->dma_addr;
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	inst->vsi->dec.vp_wrapper_dma = 0;
+}
+
+static int vdec_vp8_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp8_inst *inst;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP8;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_vp8 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_vp8_vsi *)inst->vpu.vsi;
+	init_list(inst);
+	err = alloc_all_working_buf(inst);
+	if (err)
+		goto error_free_buf;
+
+	get_hw_reg_base(inst);
+	mtk_vcodec_debug(inst, "VP8 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_buf:
+	free_all_working_buf(inst);
+error_free_inst:
+	kfree(inst);
+	return err;
+}
+
+static int vdec_vp8_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			   struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+	struct vdec_vp8_dec_info *dec = &inst->vsi->dec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	unsigned char *bs_va;
+	unsigned int data;
+	int err = 0;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
+			 inst->frm_cnt, y_fb_dma, c_fb_dma, fb);
+
+	inst->cur_fb = fb;
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL) {
+		move_fb_list_use_to_free(inst);
+		return vpu_dec_reset(vpu);
+	}
+
+	bs_va = (unsigned char *)bs->va;
+	data = (*(bs_va + 9) << 24) | (*(bs_va + 8) << 16) |
+	       (*(bs_va + 7) << 8) | *(bs_va + 6);
+
+	dec->bs_dma = (unsigned long)bs->dma_addr;
+	dec->bs_sz = bs->size;
+	dec->cur_y_fb_dma = y_fb_dma;
+	dec->cur_c_fb_dma = c_fb_dma;
+
+	mtk_vcodec_debug(inst, "\n + FRAME[%d] +\n", inst->frm_cnt);
+
+	write_hw_data(inst);
+	enable_hw_rw_dec_data(inst);
+	store_dec_data(inst);
+
+	err = vpu_dec_start(vpu, &data, 1);
+	if (err) {
+		add_fb_to_free_list(inst, fb);
+		if (dec->wait_key_frame) {
+			mtk_vcodec_debug(inst, "wait key frame !");
+			return 0;
+		}
+
+		goto error;
+	}
+
+	if (dec->resolution_changed) {
+		mtk_vcodec_debug(inst, "- resolution_changed -");
+		*res_chg = true;
+		add_fb_to_free_list(inst, fb);
+		return 0;
+	}
+
+	/* wait decoder done interrupt */
+	mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
+				     WAIT_INTR_TIMEOUT_MS);
+
+	if (inst->vsi->load_data)
+		load_dec_data(inst);
+
+	vp8_dec_finish(inst);
+	read_hw_data(inst);
+
+	err = vpu_dec_end(vpu);
+	if (err)
+		goto error;
+
+	mtk_vcodec_debug(inst, "\n - FRAME[%d] - show=%d\n", inst->frm_cnt,
+			 dec->show_frame);
+	inst->frm_cnt++;
+	*res_chg = false;
+	return 0;
+
+error:
+	mtk_vcodec_err(inst, "\n - FRAME[%d] - err=%d\n", inst->frm_cnt, err);
+	return err;
+}
+
+static void get_disp_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->disp_rdy_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->disp_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_free_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->dec_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->dec_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_crop_info(struct vdec_vp8_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->vsi->pic.pic_w;
+	cr->c.height = inst->vsi->pic.pic_h;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp8_get_param(unsigned long h_vdec,
+			      enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 4;
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vdec_vp8_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+	kfree(inst);
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_vp8_if = {
+	vdec_vp8_init,
+	vdec_vp8_decode,
+	vdec_vp8_get_param,
+	vdec_vp8_deinit,
+};
+
+struct vdec_common_if *get_vp8_dec_comm_if(void)
+{
+	return &vdec_vp8_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 576920c..6e2f065 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -25,6 +25,7 @@
 #include "mtk_vpu.h"
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
+struct vdec_common_if *get_vp8_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -35,6 +36,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_h264_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP8:
+		ctx->dec_if = get_vp8_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP9:
 	default:
 		return -EINVAL;
-- 
1.7.9.5

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

* [PATCH 5/7] [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
@ 2016-04-13 12:01           ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak
  Cc: devicetree-u79uwXL29TY76Z2rM5mHXA, Tiffany Lin,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w, Yingjoe Chen, Eddie Huang,
	PC Chen, linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA

Add vp8 decoder driver for MT8173

Signed-off-by: PC Chen <pc.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 634 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 58243ed..7743c81 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-common.o
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		vdec/vdec_vp8_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
new file mode 100644
index 0000000..9b6ef9d
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
@@ -0,0 +1,630 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Jungchang Tsao <jungchang.tsao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
+ *	   PC Chen <pc.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define VP8_VP_WRAP_SZ				(45 * 4096)
+#define VP8_SEGID_DRAM_ADDR			0x3c
+#define VP8_HW_DATA_ADDR			0x93C
+#define VP8_HW_DATA_VALUE			0x940
+#define VP8_BSASET			        0x100
+#define VP8_BSDSET				0x104
+#define VP8_DEC_DATA_PROCESSING_LOOP		96
+
+#define VP8_RW_CKEN_SET				0x0
+#define VP8_RW_DCM_CON				0x18
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_MISC_SYS_SEL			0x84
+#define VP8_RW_MISC_SPEC_CON			0xC8
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_VP8_CTRL				0xA4
+#define VP8_RW_MISC_DCM_CON			0xEC
+#define VP8_RW_MISC_SRST			0xF4
+#define VP8_RW_MISC_FUNC_CON			0xCC
+
+#define VP8_MAX_FRM_BUFF_NUM				5
+#define VP8_HW_DATA_SZ					272
+#define VP8_DEC_DATA_SZ					300
+
+/**
+ * struct vdec_vp8_dec_info - decode misc information
+ * @vp_wrapper_dma    : wrapper buffer dma
+ * @prev_y_dma        : previous decoded frame buffer Y plane address
+ * @cur_y_fb_dma      : current plane Y frame buffer dma
+ * @cur_c_fb_dma      : current plane C frame buffer dma
+ * @bs_dma	      : bitstream dma
+ * @bs_sz	      : current plane C frame buffer dma
+ * @resolution_changed: resolution change flag
+ * @show_frame	      : display this frame or not
+ * @wait_key_frame    : wait key frame coming
+ */
+struct vdec_vp8_dec_info {
+	uint64_t vp_wrapper_dma;
+	uint64_t prev_y_dma;
+	uint64_t cur_y_fb_dma;
+	uint64_t cur_c_fb_dma;
+	uint64_t bs_dma;
+	uint32_t bs_sz;
+	uint32_t resolution_changed;
+	uint32_t show_frame;
+	uint32_t wait_key_frame;
+};
+
+/**
+ * struct vdec_vp8_vsi - VPU shared information
+ * @dec			: decoding information
+ * @pic			: picture information
+ * @dec_data		: decode data
+ * @segid_wrapper_work	: seg id wrapper buffer
+ * @load_data		: flag to indicate reload decode data
+ */
+struct vdec_vp8_vsi {
+	struct vdec_vp8_dec_info dec;
+	struct vdec_pic_info pic;
+	unsigned int dec_data[VP8_DEC_DATA_SZ];
+	unsigned int segid_wrapper_work[VP8_HW_DATA_SZ][4];
+	unsigned int load_data;
+};
+
+/**
+ * struct vdec_vp8_hw_reg_base - HW register base
+ * @sys		: base address for sys
+ * @misc	: base address for misc
+ * @ld		: base address for ld
+ * @top		: base address for top
+ * @cm		: base address for cm
+ * @hwd		: base address for hwd
+ * @hwb		: base address for hwb
+*/
+struct vdec_vp8_hw_reg_base {
+	void __iomem *sys;
+	void __iomem *misc;
+	void __iomem *ld;
+	void __iomem *top;
+	void __iomem *cm;
+	void __iomem *hwd;
+	void __iomem *hwb;
+};
+
+/**
+ * struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
+ * @wq_hd	: Wait queue to wait VPU message ack
+ * @signaled	: 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure	: VPU execution result status
+ * @inst_addr	: VPU decoder instance addr
+ */
+struct vdec_vp8_vpu_inst {
+	wait_queue_head_t wq_hd;
+	int signaled;
+	int failure;
+	unsigned int inst_addr;
+};
+
+/* frame buffer (fb) list
+ * [dec_fb_list]   - decode fb are initialized to 0 and populated in
+ * [dec_use_list]  - fb is set after decode and is moved to this list
+ * [dec_free_list] - fb is not needed for reference will be moved from
+ *		     [dec_use_list] to [dec_free_list] and
+ *		     once user remove fb from [dec_free_list],
+ *		     it is circulated back to [dec_fb_list]
+ * [disp_fb_list]  - display fb are initialized to 0 and populated in
+ * [disp_rdy_list] - fb is set after decode and is moved to this list
+ *                   once user remove fb from [disp_rdy_list] it is
+ *                   circulated back to [disp_fb_list]
+ */
+
+/**
+ * struct vdec_vp8_inst - VP8 decoder instance
+ * @cur_fb		: current frame buffer
+ * @dec_fb		: decode frame buffer node
+ * @disp_fb		: display frame buffer node
+ * @dec_fb_list		: list to store decode frame buffer
+ * @dec_use_list	: list to store frame buffer in use
+ * @dec_free_list	: list to store free frame buffer
+ * @disp_fb_list	: list to store display frame buffer
+ * @disp_rdy_list	: list to store display ready frame buffer
+ * @vp_wrapper_buf	: decoder working buffer
+ * @reg_base		: hw register base address
+ * @frm_cnt		: decode frame count
+ * @ctx			: V4L2 context
+ * @dev			: platform device
+ * @vpu			: VPU instance for decoder
+ * @vsi			: VPU share information
+ */
+struct vdec_vp8_inst {
+	struct vdec_fb *cur_fb;
+	struct vdec_fb_node dec_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct vdec_fb_node disp_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct list_head dec_fb_list;
+	struct list_head dec_use_list;
+	struct list_head dec_free_list;
+	struct list_head disp_fb_list;
+	struct list_head disp_rdy_list;
+	struct mtk_vcodec_mem vp_wrapper_buf;
+	struct vdec_vp8_hw_reg_base reg_base;
+	unsigned int frm_cnt;
+	struct mtk_vcodec_ctx *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp8_vsi *vsi;
+};
+
+static void get_hw_reg_base(struct vdec_vp8_inst *inst)
+{
+	inst->reg_base.top = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_TOP);
+	inst->reg_base.cm = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_CM);
+	inst->reg_base.hwd = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWD);
+	inst->reg_base.sys = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_SYS);
+	inst->reg_base.misc = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_MISC);
+	inst->reg_base.ld = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_LD);
+	inst->reg_base.hwb = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWB);
+}
+
+static void write_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = (1 << 16) + ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = inst->vsi->segid_wrapper_work[i][j];
+			writel(val, cm + VP8_HW_DATA_VALUE);
+		}
+	}
+}
+
+static void read_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = readl(cm + VP8_HW_DATA_VALUE);
+			inst->vsi->segid_wrapper_work[i][j] = val;
+		}
+	}
+}
+
+static void enable_hw_rw_dec_data(struct vdec_vp8_inst *inst)
+{
+	unsigned int reg_val = 0;
+	void __iomem *sys = inst->reg_base.sys;
+	void __iomem *misc = inst->reg_base.misc;
+	void __iomem *ld = inst->reg_base.ld;
+	void __iomem *hwb = inst->reg_base.hwb;
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	writel(0x1, sys + VP8_RW_CKEN_SET);
+	writel(0x101, ld + VP8_WO_VLD_SRST);
+	writel(0x101, hwb + VP8_WO_VLD_SRST);
+
+	writel(1, sys);
+	reg_val = readl(misc + VP8_RW_MISC_SRST);
+	writel((reg_val & 0xFFFFFFFE), misc + VP8_RW_MISC_SRST);
+
+	writel(0x1, misc + VP8_RW_MISC_SYS_SEL);
+	writel(0x17F, misc + VP8_RW_MISC_SPEC_CON);
+	writel(0x71201100, misc + VP8_RW_MISC_FUNC_CON);
+	writel(0x0, ld + VP8_WO_VLD_SRST);
+	writel(0x0, hwb + VP8_WO_VLD_SRST);
+	writel(0x1, sys + VP8_RW_DCM_CON);
+	writel(0x1, misc + VP8_RW_MISC_DCM_CON);
+	writel(0x1, hwd + VP8_RW_VP8_CTRL);
+}
+
+static void store_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int addr = 0, val = 0;
+	void __iomem *hwd = inst->reg_base.hwd;
+	unsigned int *p = &inst->vsi->dec_data[2];
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		for (j = 0; j < 3 ; j++) {
+			val = *p++;
+			writel(val, hwd + VP8_BSDSET);
+		}
+		addr += 4;
+	}
+}
+
+static void load_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i;
+	unsigned int addr = 0;
+	unsigned int *p = &inst->vsi->dec_data[2];
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET) & 0xFFFFFF;
+		addr += 4;
+	}
+}
+
+static void get_pic_info(struct vdec_vp8_inst *inst, struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static int vp8_dec_finish(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node;
+	uint64_t prev_y_dma = inst->vsi->dec.prev_y_dma;
+
+	mtk_vcodec_debug(inst, "prev fb base dma=%llx", prev_y_dma);
+
+	/* put last decode ok frame to dec_free_list */
+	if (0 != prev_y_dma) {
+		list_for_each_entry(node, &inst->dec_use_list, list) {
+			struct vdec_fb *fb = (struct vdec_fb *)node->fb;
+
+			if (prev_y_dma == (unsigned long)fb->base_y.dma_addr) {
+				list_move_tail(&node->list,
+					       &inst->dec_free_list);
+				break;
+			}
+		}
+	}
+
+	/* dec_fb_list -> dec_use_list */
+	node = list_first_entry(&inst->dec_fb_list, struct vdec_fb_node, list);
+	node->fb = inst->cur_fb;
+	list_move_tail(&node->list, &inst->dec_use_list);
+
+	/* disp_fb_list -> disp_rdy_list */
+	if (inst->vsi->dec.show_frame) {
+		node = list_first_entry(&inst->disp_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = inst->cur_fb;
+		list_move_tail(&node->list, &inst->disp_rdy_list);
+	}
+
+	return 0;
+}
+
+static void move_fb_list_use_to_free(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->dec_use_list, list)
+		list_move_tail(&node->list, &inst->dec_free_list);
+}
+
+static void init_list(struct vdec_vp8_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->dec_fb_list);
+	INIT_LIST_HEAD(&inst->dec_use_list);
+	INIT_LIST_HEAD(&inst->dec_free_list);
+	INIT_LIST_HEAD(&inst->disp_fb_list);
+	INIT_LIST_HEAD(&inst->disp_rdy_list);
+
+	for (i = 0; i < VP8_MAX_FRM_BUFF_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list, &inst->dec_fb_list);
+
+		INIT_LIST_HEAD(&inst->disp_fb[i].list);
+		inst->disp_fb[i].fb = NULL;
+		list_add_tail(&inst->disp_fb[i].list, &inst->disp_fb_list);
+	}
+}
+
+static void add_fb_to_free_list(struct vdec_vp8_inst *inst, void *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry(&inst->dec_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->dec_free_list);
+	}
+}
+
+static int alloc_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	int err;
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	mem->size = VP8_VP_WRAP_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (err) {
+		mtk_vcodec_err(inst, "Cannot allocate vp wrapper buffer");
+		return -ENOMEM;
+	}
+
+	inst->vsi->dec.vp_wrapper_dma = (u64)mem->dma_addr;
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	inst->vsi->dec.vp_wrapper_dma = 0;
+}
+
+static int vdec_vp8_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp8_inst *inst;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP8;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_vp8 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_vp8_vsi *)inst->vpu.vsi;
+	init_list(inst);
+	err = alloc_all_working_buf(inst);
+	if (err)
+		goto error_free_buf;
+
+	get_hw_reg_base(inst);
+	mtk_vcodec_debug(inst, "VP8 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_buf:
+	free_all_working_buf(inst);
+error_free_inst:
+	kfree(inst);
+	return err;
+}
+
+static int vdec_vp8_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			   struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+	struct vdec_vp8_dec_info *dec = &inst->vsi->dec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	unsigned char *bs_va;
+	unsigned int data;
+	int err = 0;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
+			 inst->frm_cnt, y_fb_dma, c_fb_dma, fb);
+
+	inst->cur_fb = fb;
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL) {
+		move_fb_list_use_to_free(inst);
+		return vpu_dec_reset(vpu);
+	}
+
+	bs_va = (unsigned char *)bs->va;
+	data = (*(bs_va + 9) << 24) | (*(bs_va + 8) << 16) |
+	       (*(bs_va + 7) << 8) | *(bs_va + 6);
+
+	dec->bs_dma = (unsigned long)bs->dma_addr;
+	dec->bs_sz = bs->size;
+	dec->cur_y_fb_dma = y_fb_dma;
+	dec->cur_c_fb_dma = c_fb_dma;
+
+	mtk_vcodec_debug(inst, "\n + FRAME[%d] +\n", inst->frm_cnt);
+
+	write_hw_data(inst);
+	enable_hw_rw_dec_data(inst);
+	store_dec_data(inst);
+
+	err = vpu_dec_start(vpu, &data, 1);
+	if (err) {
+		add_fb_to_free_list(inst, fb);
+		if (dec->wait_key_frame) {
+			mtk_vcodec_debug(inst, "wait key frame !");
+			return 0;
+		}
+
+		goto error;
+	}
+
+	if (dec->resolution_changed) {
+		mtk_vcodec_debug(inst, "- resolution_changed -");
+		*res_chg = true;
+		add_fb_to_free_list(inst, fb);
+		return 0;
+	}
+
+	/* wait decoder done interrupt */
+	mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
+				     WAIT_INTR_TIMEOUT_MS);
+
+	if (inst->vsi->load_data)
+		load_dec_data(inst);
+
+	vp8_dec_finish(inst);
+	read_hw_data(inst);
+
+	err = vpu_dec_end(vpu);
+	if (err)
+		goto error;
+
+	mtk_vcodec_debug(inst, "\n - FRAME[%d] - show=%d\n", inst->frm_cnt,
+			 dec->show_frame);
+	inst->frm_cnt++;
+	*res_chg = false;
+	return 0;
+
+error:
+	mtk_vcodec_err(inst, "\n - FRAME[%d] - err=%d\n", inst->frm_cnt, err);
+	return err;
+}
+
+static void get_disp_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->disp_rdy_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->disp_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_free_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->dec_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->dec_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_crop_info(struct vdec_vp8_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->vsi->pic.pic_w;
+	cr->c.height = inst->vsi->pic.pic_h;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp8_get_param(unsigned long h_vdec,
+			      enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 4;
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vdec_vp8_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+	kfree(inst);
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_vp8_if = {
+	vdec_vp8_init,
+	vdec_vp8_decode,
+	vdec_vp8_get_param,
+	vdec_vp8_deinit,
+};
+
+struct vdec_common_if *get_vp8_dec_comm_if(void)
+{
+	return &vdec_vp8_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 576920c..6e2f065 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -25,6 +25,7 @@
 #include "mtk_vpu.h"
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
+struct vdec_common_if *get_vp8_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -35,6 +36,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_h264_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP8:
+		ctx->dec_if = get_vp8_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP9:
 	default:
 		return -EINVAL;
-- 
1.7.9.5

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

* [PATCH 5/7] [media] vcodec: mediatek: Add Mediatek VP8 Video Decoder Driver
@ 2016-04-13 12:01           ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add vp8 decoder driver for MT8173

Signed-off-by: PC Chen <pc.chen@mediatek.com>
Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp8_if.c   |  630 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 634 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 58243ed..7743c81 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 				       mtk-vcodec-common.o
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
+		vdec/vdec_vp8_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
new file mode 100644
index 0000000..9b6ef9d
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp8_if.c
@@ -0,0 +1,630 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
+ *	   PC Chen <pc.chen@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_vpu_if.h"
+#include "vdec_drv_base.h"
+
+#define VP8_VP_WRAP_SZ				(45 * 4096)
+#define VP8_SEGID_DRAM_ADDR			0x3c
+#define VP8_HW_DATA_ADDR			0x93C
+#define VP8_HW_DATA_VALUE			0x940
+#define VP8_BSASET			        0x100
+#define VP8_BSDSET				0x104
+#define VP8_DEC_DATA_PROCESSING_LOOP		96
+
+#define VP8_RW_CKEN_SET				0x0
+#define VP8_RW_DCM_CON				0x18
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_MISC_SYS_SEL			0x84
+#define VP8_RW_MISC_SPEC_CON			0xC8
+#define VP8_WO_VLD_SRST				0x108
+#define VP8_RW_VP8_CTRL				0xA4
+#define VP8_RW_MISC_DCM_CON			0xEC
+#define VP8_RW_MISC_SRST			0xF4
+#define VP8_RW_MISC_FUNC_CON			0xCC
+
+#define VP8_MAX_FRM_BUFF_NUM				5
+#define VP8_HW_DATA_SZ					272
+#define VP8_DEC_DATA_SZ					300
+
+/**
+ * struct vdec_vp8_dec_info - decode misc information
+ * @vp_wrapper_dma    : wrapper buffer dma
+ * @prev_y_dma        : previous decoded frame buffer Y plane address
+ * @cur_y_fb_dma      : current plane Y frame buffer dma
+ * @cur_c_fb_dma      : current plane C frame buffer dma
+ * @bs_dma	      : bitstream dma
+ * @bs_sz	      : current plane C frame buffer dma
+ * @resolution_changed: resolution change flag
+ * @show_frame	      : display this frame or not
+ * @wait_key_frame    : wait key frame coming
+ */
+struct vdec_vp8_dec_info {
+	uint64_t vp_wrapper_dma;
+	uint64_t prev_y_dma;
+	uint64_t cur_y_fb_dma;
+	uint64_t cur_c_fb_dma;
+	uint64_t bs_dma;
+	uint32_t bs_sz;
+	uint32_t resolution_changed;
+	uint32_t show_frame;
+	uint32_t wait_key_frame;
+};
+
+/**
+ * struct vdec_vp8_vsi - VPU shared information
+ * @dec			: decoding information
+ * @pic			: picture information
+ * @dec_data		: decode data
+ * @segid_wrapper_work	: seg id wrapper buffer
+ * @load_data		: flag to indicate reload decode data
+ */
+struct vdec_vp8_vsi {
+	struct vdec_vp8_dec_info dec;
+	struct vdec_pic_info pic;
+	unsigned int dec_data[VP8_DEC_DATA_SZ];
+	unsigned int segid_wrapper_work[VP8_HW_DATA_SZ][4];
+	unsigned int load_data;
+};
+
+/**
+ * struct vdec_vp8_hw_reg_base - HW register base
+ * @sys		: base address for sys
+ * @misc	: base address for misc
+ * @ld		: base address for ld
+ * @top		: base address for top
+ * @cm		: base address for cm
+ * @hwd		: base address for hwd
+ * @hwb		: base address for hwb
+*/
+struct vdec_vp8_hw_reg_base {
+	void __iomem *sys;
+	void __iomem *misc;
+	void __iomem *ld;
+	void __iomem *top;
+	void __iomem *cm;
+	void __iomem *hwd;
+	void __iomem *hwb;
+};
+
+/**
+ * struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
+ * @wq_hd	: Wait queue to wait VPU message ack
+ * @signaled	: 1 - Host has received ack message from VPU, 0 - not recevie
+ * @failure	: VPU execution result status
+ * @inst_addr	: VPU decoder instance addr
+ */
+struct vdec_vp8_vpu_inst {
+	wait_queue_head_t wq_hd;
+	int signaled;
+	int failure;
+	unsigned int inst_addr;
+};
+
+/* frame buffer (fb) list
+ * [dec_fb_list]   - decode fb are initialized to 0 and populated in
+ * [dec_use_list]  - fb is set after decode and is moved to this list
+ * [dec_free_list] - fb is not needed for reference will be moved from
+ *		     [dec_use_list] to [dec_free_list] and
+ *		     once user remove fb from [dec_free_list],
+ *		     it is circulated back to [dec_fb_list]
+ * [disp_fb_list]  - display fb are initialized to 0 and populated in
+ * [disp_rdy_list] - fb is set after decode and is moved to this list
+ *                   once user remove fb from [disp_rdy_list] it is
+ *                   circulated back to [disp_fb_list]
+ */
+
+/**
+ * struct vdec_vp8_inst - VP8 decoder instance
+ * @cur_fb		: current frame buffer
+ * @dec_fb		: decode frame buffer node
+ * @disp_fb		: display frame buffer node
+ * @dec_fb_list		: list to store decode frame buffer
+ * @dec_use_list	: list to store frame buffer in use
+ * @dec_free_list	: list to store free frame buffer
+ * @disp_fb_list	: list to store display frame buffer
+ * @disp_rdy_list	: list to store display ready frame buffer
+ * @vp_wrapper_buf	: decoder working buffer
+ * @reg_base		: hw register base address
+ * @frm_cnt		: decode frame count
+ * @ctx			: V4L2 context
+ * @dev			: platform device
+ * @vpu			: VPU instance for decoder
+ * @vsi			: VPU share information
+ */
+struct vdec_vp8_inst {
+	struct vdec_fb *cur_fb;
+	struct vdec_fb_node dec_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct vdec_fb_node disp_fb[VP8_MAX_FRM_BUFF_NUM];
+	struct list_head dec_fb_list;
+	struct list_head dec_use_list;
+	struct list_head dec_free_list;
+	struct list_head disp_fb_list;
+	struct list_head disp_rdy_list;
+	struct mtk_vcodec_mem vp_wrapper_buf;
+	struct vdec_vp8_hw_reg_base reg_base;
+	unsigned int frm_cnt;
+	struct mtk_vcodec_ctx *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp8_vsi *vsi;
+};
+
+static void get_hw_reg_base(struct vdec_vp8_inst *inst)
+{
+	inst->reg_base.top = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_TOP);
+	inst->reg_base.cm = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_CM);
+	inst->reg_base.hwd = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWD);
+	inst->reg_base.sys = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_SYS);
+	inst->reg_base.misc = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_MISC);
+	inst->reg_base.ld = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_LD);
+	inst->reg_base.hwb = mtk_vcodec_get_reg_addr(inst->ctx, VDEC_HWB);
+}
+
+static void write_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = (1 << 16) + ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = inst->vsi->segid_wrapper_work[i][j];
+			writel(val, cm + VP8_HW_DATA_VALUE);
+		}
+	}
+}
+
+static void read_hw_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int seg_id_addr;
+	unsigned int val;
+	void __iomem *cm = inst->reg_base.cm;
+
+	seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
+
+	for (i = 0; i < VP8_HW_DATA_SZ; i++) {
+		for (j = 3; j >= 0; j--) {
+			val = ((seg_id_addr + i) << 2) + j;
+			writel(val, cm + VP8_HW_DATA_ADDR);
+
+			val = readl(cm + VP8_HW_DATA_VALUE);
+			inst->vsi->segid_wrapper_work[i][j] = val;
+		}
+	}
+}
+
+static void enable_hw_rw_dec_data(struct vdec_vp8_inst *inst)
+{
+	unsigned int reg_val = 0;
+	void __iomem *sys = inst->reg_base.sys;
+	void __iomem *misc = inst->reg_base.misc;
+	void __iomem *ld = inst->reg_base.ld;
+	void __iomem *hwb = inst->reg_base.hwb;
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	writel(0x1, sys + VP8_RW_CKEN_SET);
+	writel(0x101, ld + VP8_WO_VLD_SRST);
+	writel(0x101, hwb + VP8_WO_VLD_SRST);
+
+	writel(1, sys);
+	reg_val = readl(misc + VP8_RW_MISC_SRST);
+	writel((reg_val & 0xFFFFFFFE), misc + VP8_RW_MISC_SRST);
+
+	writel(0x1, misc + VP8_RW_MISC_SYS_SEL);
+	writel(0x17F, misc + VP8_RW_MISC_SPEC_CON);
+	writel(0x71201100, misc + VP8_RW_MISC_FUNC_CON);
+	writel(0x0, ld + VP8_WO_VLD_SRST);
+	writel(0x0, hwb + VP8_WO_VLD_SRST);
+	writel(0x1, sys + VP8_RW_DCM_CON);
+	writel(0x1, misc + VP8_RW_MISC_DCM_CON);
+	writel(0x1, hwd + VP8_RW_VP8_CTRL);
+}
+
+static void store_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i, j;
+	unsigned int addr = 0, val = 0;
+	void __iomem *hwd = inst->reg_base.hwd;
+	unsigned int *p = &inst->vsi->dec_data[2];
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		for (j = 0; j < 3 ; j++) {
+			val = *p++;
+			writel(val, hwd + VP8_BSDSET);
+		}
+		addr += 4;
+	}
+}
+
+static void load_dec_data(struct vdec_vp8_inst *inst)
+{
+	int i;
+	unsigned int addr = 0;
+	unsigned int *p = &inst->vsi->dec_data[2];
+	void __iomem *hwd = inst->reg_base.hwd;
+
+	for (i = 0; i < VP8_DEC_DATA_PROCESSING_LOOP; i++) {
+		writel(addr, hwd + VP8_BSASET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET);
+		*p++ = readl(hwd + VP8_BSDSET) & 0xFFFFFF;
+		addr += 4;
+	}
+}
+
+static void get_pic_info(struct vdec_vp8_inst *inst, struct vdec_pic_info *pic)
+{
+	pic->pic_w = inst->vsi->pic.pic_w;
+	pic->pic_h = inst->vsi->pic.pic_h;
+	pic->buf_w = inst->vsi->pic.buf_w;
+	pic->buf_h = inst->vsi->pic.buf_h;
+	pic->y_bs_sz = inst->vsi->pic.y_bs_sz;
+	pic->c_bs_sz = inst->vsi->pic.c_bs_sz;
+	pic->y_len_sz = inst->vsi->pic.y_len_sz;
+	pic->c_len_sz = inst->vsi->pic.c_len_sz;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+			 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+			 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static int vp8_dec_finish(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node;
+	uint64_t prev_y_dma = inst->vsi->dec.prev_y_dma;
+
+	mtk_vcodec_debug(inst, "prev fb base dma=%llx", prev_y_dma);
+
+	/* put last decode ok frame to dec_free_list */
+	if (0 != prev_y_dma) {
+		list_for_each_entry(node, &inst->dec_use_list, list) {
+			struct vdec_fb *fb = (struct vdec_fb *)node->fb;
+
+			if (prev_y_dma == (unsigned long)fb->base_y.dma_addr) {
+				list_move_tail(&node->list,
+					       &inst->dec_free_list);
+				break;
+			}
+		}
+	}
+
+	/* dec_fb_list -> dec_use_list */
+	node = list_first_entry(&inst->dec_fb_list, struct vdec_fb_node, list);
+	node->fb = inst->cur_fb;
+	list_move_tail(&node->list, &inst->dec_use_list);
+
+	/* disp_fb_list -> disp_rdy_list */
+	if (inst->vsi->dec.show_frame) {
+		node = list_first_entry(&inst->disp_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = inst->cur_fb;
+		list_move_tail(&node->list, &inst->disp_rdy_list);
+	}
+
+	return 0;
+}
+
+static void move_fb_list_use_to_free(struct vdec_vp8_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->dec_use_list, list)
+		list_move_tail(&node->list, &inst->dec_free_list);
+}
+
+static void init_list(struct vdec_vp8_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->dec_fb_list);
+	INIT_LIST_HEAD(&inst->dec_use_list);
+	INIT_LIST_HEAD(&inst->dec_free_list);
+	INIT_LIST_HEAD(&inst->disp_fb_list);
+	INIT_LIST_HEAD(&inst->disp_rdy_list);
+
+	for (i = 0; i < VP8_MAX_FRM_BUFF_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list, &inst->dec_fb_list);
+
+		INIT_LIST_HEAD(&inst->disp_fb[i].list);
+		inst->disp_fb[i].fb = NULL;
+		list_add_tail(&inst->disp_fb[i].list, &inst->disp_fb_list);
+	}
+}
+
+static void add_fb_to_free_list(struct vdec_vp8_inst *inst, void *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry(&inst->dec_fb_list,
+					struct vdec_fb_node, list);
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->dec_free_list);
+	}
+}
+
+static int alloc_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	int err;
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	mem->size = VP8_VP_WRAP_SZ;
+	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (err) {
+		mtk_vcodec_err(inst, "Cannot allocate vp wrapper buffer");
+		return -ENOMEM;
+	}
+
+	inst->vsi->dec.vp_wrapper_dma = (u64)mem->dma_addr;
+	return 0;
+}
+
+static void free_all_working_buf(struct vdec_vp8_inst *inst)
+{
+	struct mtk_vcodec_mem *mem = &inst->vp_wrapper_buf;
+
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	inst->vsi->dec.vp_wrapper_dma = 0;
+}
+
+static int vdec_vp8_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp8_inst *inst;
+	int err;
+
+	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP8;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	err = vpu_dec_init(&inst->vpu);
+	if (err) {
+		mtk_vcodec_err(inst, "vdec_vp8 init err=%d", err);
+		goto error_free_inst;
+	}
+
+	inst->vsi = (struct vdec_vp8_vsi *)inst->vpu.vsi;
+	init_list(inst);
+	err = alloc_all_working_buf(inst);
+	if (err)
+		goto error_free_buf;
+
+	get_hw_reg_base(inst);
+	mtk_vcodec_debug(inst, "VP8 Instance >> %p", inst);
+
+	*h_vdec = (unsigned long)inst;
+	return 0;
+
+error_free_buf:
+	free_all_working_buf(inst);
+error_free_inst:
+	kfree(inst);
+	return err;
+}
+
+static int vdec_vp8_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+			   struct vdec_fb *fb, bool *res_chg)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+	struct vdec_vp8_dec_info *dec = &inst->vsi->dec;
+	struct vdec_vpu_inst *vpu = &inst->vpu;
+	unsigned char *bs_va;
+	unsigned int data;
+	int err = 0;
+	uint64_t y_fb_dma;
+	uint64_t c_fb_dma;
+
+	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
+	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
+
+	mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
+			 inst->frm_cnt, y_fb_dma, c_fb_dma, fb);
+
+	inst->cur_fb = fb;
+
+	/* bs NULL means flush decoder */
+	if (bs == NULL) {
+		move_fb_list_use_to_free(inst);
+		return vpu_dec_reset(vpu);
+	}
+
+	bs_va = (unsigned char *)bs->va;
+	data = (*(bs_va + 9) << 24) | (*(bs_va + 8) << 16) |
+	       (*(bs_va + 7) << 8) | *(bs_va + 6);
+
+	dec->bs_dma = (unsigned long)bs->dma_addr;
+	dec->bs_sz = bs->size;
+	dec->cur_y_fb_dma = y_fb_dma;
+	dec->cur_c_fb_dma = c_fb_dma;
+
+	mtk_vcodec_debug(inst, "\n + FRAME[%d] +\n", inst->frm_cnt);
+
+	write_hw_data(inst);
+	enable_hw_rw_dec_data(inst);
+	store_dec_data(inst);
+
+	err = vpu_dec_start(vpu, &data, 1);
+	if (err) {
+		add_fb_to_free_list(inst, fb);
+		if (dec->wait_key_frame) {
+			mtk_vcodec_debug(inst, "wait key frame !");
+			return 0;
+		}
+
+		goto error;
+	}
+
+	if (dec->resolution_changed) {
+		mtk_vcodec_debug(inst, "- resolution_changed -");
+		*res_chg = true;
+		add_fb_to_free_list(inst, fb);
+		return 0;
+	}
+
+	/* wait decoder done interrupt */
+	mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
+				     WAIT_INTR_TIMEOUT_MS);
+
+	if (inst->vsi->load_data)
+		load_dec_data(inst);
+
+	vp8_dec_finish(inst);
+	read_hw_data(inst);
+
+	err = vpu_dec_end(vpu);
+	if (err)
+		goto error;
+
+	mtk_vcodec_debug(inst, "\n - FRAME[%d] - show=%d\n", inst->frm_cnt,
+			 dec->show_frame);
+	inst->frm_cnt++;
+	*res_chg = false;
+	return 0;
+
+error:
+	mtk_vcodec_err(inst, "\n - FRAME[%d] - err=%d\n", inst->frm_cnt, err);
+	return err;
+}
+
+static void get_disp_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->disp_rdy_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->disp_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_free_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb;
+
+	node = list_first_entry_or_null(&inst->dec_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->dec_fb_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static void get_crop_info(struct vdec_vp8_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->vsi->pic.pic_w;
+	cr->c.height = inst->vsi->pic.pic_h;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp8_get_param(unsigned long h_vdec,
+			      enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 4;
+		break;
+
+	default:
+		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int vdec_vp8_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
+
+	mtk_vcodec_debug_enter(inst);
+
+	vpu_dec_deinit(&inst->vpu);
+	free_all_working_buf(inst);
+	kfree(inst);
+
+	return 0;
+}
+
+static struct vdec_common_if vdec_vp8_if = {
+	vdec_vp8_init,
+	vdec_vp8_decode,
+	vdec_vp8_get_param,
+	vdec_vp8_deinit,
+};
+
+struct vdec_common_if *get_vp8_dec_comm_if(void)
+{
+	return &vdec_vp8_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 576920c..6e2f065 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -25,6 +25,7 @@
 #include "mtk_vpu.h"
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
+struct vdec_common_if *get_vp8_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -35,6 +36,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_h264_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP8:
+		ctx->dec_if = get_vp8_dec_comm_if();
+		break;
 	case V4L2_PIX_FMT_VP9:
 	default:
 		return -EINVAL;
-- 
1.7.9.5

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

* [PATCH 6/7] [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
  2016-04-13 12:01           ` Tiffany Lin
  (?)
@ 2016-04-13 12:01             ` Tiffany Lin
  -1 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add vp9 decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 1112 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 7743c81..852d969 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
 		vdec/vdec_vp8_if.o \
+		vdec/vdec_vp9_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
new file mode 100644
index 0000000..6d0fb68
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
@@ -0,0 +1,1108 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
+ *             Kai-Sean Yang <kai-sean.yang@mediatek.com>
+ *		  Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/syscalls.h>
+#include <linux/delay.h>
+#include <linux/time.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_drv_base.h"
+#include "vdec_vpu_if.h"
+
+#define VP9_SUPER_FRAME_BS_SZ 64
+
+#define REFS_PER_FRAME 3
+#define REF_FRAMES_LOG2 3
+#define REF_FRAMES (1 << REF_FRAMES_LOG2)
+#define VP9_MAX_FRM_BUFF_NUM (9 + 0)
+#define VP9_MAX_FRM_BUFF_NODE_NUM (9 + 9)
+
+
+struct vdec_vp9_frm_hdr {
+	unsigned int width;
+	unsigned int height;
+	unsigned char show_frame;
+	unsigned char resolution_changed;
+};
+
+struct vdec_vp9_work_buf {
+	unsigned int frmbuf_width;
+	unsigned int frmbuf_height;
+	struct mtk_vcodec_mem seg_id_buf;
+	struct mtk_vcodec_mem tile_buf;
+	struct mtk_vcodec_mem count_tbl_buf;
+	struct mtk_vcodec_mem prob_tbl_buf;
+	struct mtk_vcodec_mem mv_buf;
+	struct vdec_fb sf_ref_buf[VP9_MAX_FRM_BUFF_NUM-1];
+};
+
+struct vp9_input_ctx {
+	unsigned long v_fifo_sa;
+	unsigned long v_fifo_ea;
+	unsigned long p_fifo_sa;
+	unsigned long p_fifo_ea;
+	unsigned long v_frm_sa;
+	unsigned long v_frm_ea;
+	unsigned long p_frm_sa;
+	unsigned long p_frm_end;
+	unsigned int frm_sz;
+	unsigned int uncompress_sz;
+};
+
+struct vp9_dram_buf {
+	unsigned long va;
+	unsigned long pa;
+	unsigned int sz;
+	unsigned int vpua;
+};
+
+struct vp9_fb_info {
+	struct vdec_fb *fb;
+	struct vp9_dram_buf y_buf;
+	struct vp9_dram_buf c_buf;
+	struct vp9_dram_buf ufo_len_y;
+	struct vp9_dram_buf ufo_len_c;
+	unsigned int y_width;
+	unsigned int y_height;
+	unsigned int y_crop_width;
+	unsigned int y_crop_height;
+
+	unsigned int c_width;
+	unsigned int c_height;
+	unsigned int c_crop_width;
+	unsigned int c_crop_height;
+
+	unsigned int frm_num;
+};
+
+struct vp9_ref_cnt_buf {
+	struct vp9_fb_info buf;
+	unsigned int ref_cnt;
+};
+
+struct vp9_scale_factors {
+	int x_scale_fp;
+	int y_scale_fp;
+	int x_step_q4;
+	int y_step_q4;
+	unsigned int ref_scaling_en;
+};
+
+struct vp9_ref_buf {
+	struct vp9_fb_info *buf;
+	struct vp9_scale_factors scale_factors;
+	unsigned int idx;
+};
+
+struct vp9_sf_ref_fb {
+	struct vdec_fb fb;
+	int used;
+	int idx;
+};
+
+
+/*
+ * struct vdec_vp9_vsi - shared buffer between host and VPU driver
+ */
+struct vdec_vp9_vsi {
+	unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
+	struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUFF_NUM-1];
+	int sf_next_ref_fb_idx;
+	unsigned int sf_frm_cnt;
+	unsigned int sf_frm_offset[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_sz[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_idx;
+	unsigned int sf_init;
+	struct vdec_fb fb;
+	struct mtk_vcodec_mem bs;
+	struct vdec_fb cur_fb;
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int buf_sz_y_bs;
+	unsigned int buf_sz_c_bs;
+	unsigned int buf_len_sz_y;
+	unsigned int buf_len_sz_c;
+	unsigned int profile;
+	unsigned int show_frm;
+	unsigned int show_exist;
+	unsigned int frm_to_show;
+	unsigned int refresh_frm_flags;
+	unsigned int resolution_changed;
+
+	struct vp9_input_ctx input_ctx;
+	struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUFF_NUM];
+	int ref_frm_map[REF_FRAMES];
+	unsigned int new_fb_idx;
+	unsigned int frm_num;
+	struct vp9_dram_buf seg_id_buf;
+	struct vp9_dram_buf tile_buf;
+	struct vp9_dram_buf count_tbl_buf;
+	struct vp9_dram_buf prob_tbl_buf;
+	struct vp9_dram_buf mv_buf;
+	struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
+
+};
+
+struct vdec_vp9_inst {
+	struct vdec_vp9_work_buf work_buf;
+	struct vdec_vp9_frm_hdr frm_hdr;
+	struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUFF_NODE_NUM];
+	struct list_head available_fb_node_list;
+	struct list_head fb_use_list;
+	struct list_head fb_free_list;
+	struct list_head fb_disp_list;
+	struct vdec_fb *cur_fb;
+	unsigned int frm_cnt;
+	unsigned int total_frm_cnt;
+	void *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp9_vsi *vsi;
+	unsigned int show_reg;
+	struct file *log;
+	struct mtk_vcodec_mem mem;
+};
+
+static int vp9_setup_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vpu.vsi;
+
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	if ((vsi->mv_buf.va == 0) || (vsi->mv_buf.pa == 0) ||
+		(vsi->mv_buf.sz == 0))
+		return -EINVAL;
+
+	mtk_vcodec_debug(inst, "VP9_MV_BUF_Addr: 0x%lX (0x%lX)",
+		     vsi->mv_buf.va, vsi->mv_buf.pa);
+	return 0;
+}
+
+static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (fb == &vsi->sf_ref_fb[i].fb)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1)
+		return false;
+
+	return true;
+}
+
+static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
+					*inst, void *addr)
+{
+	struct vdec_fb *fb;
+	struct vdec_fb_node *node;
+
+	list_for_each_entry(node, &inst->fb_use_list, list) {
+		fb = (struct vdec_fb *)node->fb;
+		if (fb->base_y.va == addr) {
+			list_move_tail(&node->list,
+				       &inst->available_fb_node_list);
+			break;
+		}
+	}
+
+	return fb;
+}
+
+static bool vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+		if (node) {
+			node->fb = fb;
+			list_move_tail(&node->list, &inst->fb_free_list);
+		} else
+			mtk_vcodec_debug(inst, "No free fb node");
+	}
+
+	return true;
+}
+
+static bool vp9_free_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	struct vp9_sf_ref_fb *sf_ref_fb =
+		container_of(fb, struct vp9_sf_ref_fb, fb);
+
+	sf_ref_fb->used = 0;
+
+	return true;
+}
+
+static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
+			   int new_idx)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int ref_idx = *idx;
+
+	if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
+		vsi->frm_bufs[ref_idx].ref_cnt--;
+
+		if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
+			if (!vp9_is_sf_ref_fb(inst,
+					      vsi->frm_bufs[ref_idx].buf.fb)) {
+				struct vdec_fb *fb;
+
+				fb = vp9_rm_from_fb_use_list(inst,
+				     vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
+				vp9_add_to_fb_free_list(inst, fb);
+			} else
+				vp9_free_sf_ref_fb(
+					inst, vsi->frm_bufs[ref_idx].buf.fb);
+		}
+	}
+
+	*idx = new_idx;
+	vsi->frm_bufs[new_idx].ref_cnt++;
+}
+
+static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va) {
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_y);
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_c);
+			vsi->sf_ref_fb[i].used = 0;
+		}
+	}
+}
+
+static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vdec_fb *fb_dst_ptr, *fb_src_ptr;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va &&
+		    vsi->sf_ref_fb[i].used == 0) {
+			return i;
+		}
+	}
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va == NULL)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1) {
+		mtk_vcodec_err(inst, "List Full");
+		return -1;
+	}
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_y;
+	mem->size = vsi->buf_sz_y_bs +
+		    vsi->buf_len_sz_y;
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return -1;
+	}
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf y_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height);
+
+	fb_dst_ptr = &vsi->sf_ref_fb[i].fb;
+	fb_src_ptr = &inst->work_buf.sf_ref_buf[i];
+	fb_dst_ptr->base_y.va = fb_src_ptr->base_y.va;
+	fb_dst_ptr->base_y.dma_addr = fb_src_ptr->base_y.dma_addr;
+	fb_dst_ptr->base_y.size = fb_src_ptr->base_y.size;
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_c;
+	mem->size = vsi->buf_sz_c_bs +
+		    vsi->buf_len_sz_c;
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf c_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf c_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height / 2);
+
+	fb_dst_ptr->base_c.va = fb_src_ptr->base_c.va;
+	fb_dst_ptr->base_c.dma_addr = fb_src_ptr->base_c.dma_addr;
+	fb_dst_ptr->base_c.size = fb_src_ptr->base_c.size;
+
+	vsi->sf_ref_fb[i].used = 0;
+	vsi->sf_ref_fb[i].idx = i;
+
+	return i;
+}
+
+bool vp9_realloc_work_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int result;
+	struct mtk_vcodec_mem *mem;
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
+		     inst->frm_hdr.resolution_changed,
+		     inst->frm_hdr.width,
+		     inst->frm_hdr.height,
+		     inst->work_buf.frmbuf_width,
+		     inst->work_buf.frmbuf_height);
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	/* Free First */
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+	/* Alloc Later */
+	mem->size = ((inst->work_buf.frmbuf_width / 64) *
+		    (inst->work_buf.frmbuf_height / 64) + 2) * 36 * 16;
+
+	result = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot allocate mv_buf");
+		return false;
+	}
+	/* Set the va again */
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	vp9_free_all_sf_ref_fb(inst);
+	vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+	result = vp9_setup_buf(inst);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot vp9_setup_buf");
+		return false;
+	}
+
+	inst->frm_hdr.resolution_changed = true;
+
+	return true;
+}
+
+static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_disp_list);
+	} else {
+		mtk_vcodec_debug(inst, "List Full");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vp9_fb_info *frm_to_show;
+	int ref_index = 0, mask;
+
+	for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
+		if (mask & 1)
+			vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
+				       vsi->new_fb_idx);
+		++ref_index;
+	}
+
+	frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
+	vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
+
+	if (frm_to_show->fb != inst->cur_fb) {
+		if ((frm_to_show->fb != NULL) &&
+			(inst->cur_fb->base_y.size >=
+				frm_to_show->fb->base_y.size)) {
+			memcpy((void *)inst->cur_fb->base_y.va,
+				(void *)frm_to_show->fb->base_y.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height);
+			memcpy((void *)inst->cur_fb->base_c.va,
+				(void *)frm_to_show->fb->base_c.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height / 2);
+		} else {
+			mtk_vcodec_debug(inst,
+				"inst->cur_fb->base_y.size=%lx, frm_to_show->fb.base_y.size=%lx",
+				inst->cur_fb->base_y.size,
+				frm_to_show->fb->base_y.size);
+		}
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, inst->cur_fb);
+		}
+	} else {
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
+		}
+	}
+
+	if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
+		if (!vp9_is_sf_ref_fb(
+			inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
+			struct vdec_fb *fb;
+
+			fb = vp9_rm_from_fb_use_list(inst,
+			     vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
+
+			vp9_add_to_fb_free_list(inst, fb);
+		} else
+			vp9_free_sf_ref_fb(
+				inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
+	}
+
+	if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
+		vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+}
+
+static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_ctx *ctx = inst->ctx;
+	unsigned int irq_status;
+
+	mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						MTK_INST_IRQ_RECEIVED,
+						WAIT_INTR_TIMEOUT_MS);
+
+	irq_status = ctx->irq_status;
+	mtk_vcodec_debug(inst, "isr return %x", irq_status);
+
+	if (irq_status & 0x10000)
+		return true;
+	else
+		return false;
+}
+
+struct vdec_vp9_inst *vp9_alloc_inst(void *ctx)
+{
+	int result;
+	struct mtk_vcodec_mem mem;
+	struct vdec_vp9_inst *inst;
+
+	mem.size = sizeof(struct vdec_vp9_inst) * 2;
+	result = mtk_vcodec_mem_alloc(ctx, &mem);
+	if (result)
+		return NULL;
+
+	inst = mem.va;
+	inst->mem = mem;
+
+	return inst;
+}
+
+void vp9_free_handle(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_mem mem;
+
+	mem = inst->mem;
+	if (mem.va)
+		mtk_vcodec_mem_free(inst->ctx, &mem);
+}
+
+bool vp9_init_proc(struct vdec_vp9_inst *inst,
+		   struct vdec_pic_info *pic_info)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	pic_info->pic_w = vsi->pic_w;
+	pic_info->pic_h = vsi->pic_h;
+	pic_info->buf_w = vsi->buf_w;
+	pic_info->buf_h = vsi->buf_h;
+
+	mtk_vcodec_debug(inst,
+			"(PicW,PicH,BufW,BufH) = (%d,%d,%d,%d) profile=%d",
+			pic_info->pic_w, pic_info->pic_h,
+			pic_info->buf_w, pic_info->buf_h, vsi->profile);
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	/* ----> HW limitation */
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	if (vsi->profile > 0) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support profile(%d) > 0",
+			     vsi->profile);
+		return false;
+	}
+	if ((inst->work_buf.frmbuf_width > 4096) ||
+	    (inst->work_buf.frmbuf_height > 2304)) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support (W,H) = (%d,%d)",
+			     inst->work_buf.frmbuf_width,
+			     inst->work_buf.frmbuf_height);
+		return false;
+	}
+	/* <---- HW limitation */
+
+	return true;
+}
+
+bool vp9_check_proc(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	bool ret = false;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vsi->show_exist) {
+		vp9_swap_frm_bufs(inst);
+		mtk_vcodec_debug(inst, "Decode Ok @%d (show_exist)",
+				 vsi->frm_num);
+		vsi->frm_num++;
+		return true;
+	}
+
+	ret = vp9_wait_dec_end(inst);
+	if (!ret) {
+		mtk_vcodec_err(inst, "Decode NG, Decode Timeout @[%d]",
+			       vsi->frm_num);
+		return false;
+	}
+
+	if (vpu_dec_end(&inst->vpu)) {
+		mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
+		return false;
+	}
+
+	vp9_swap_frm_bufs(inst);
+	mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
+		     inst->frm_hdr.width, inst->frm_hdr.height);
+
+	vsi->frm_num++;
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
+		return true;
+
+	return false;
+}
+
+struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_disp_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+
+	return fb;
+}
+
+bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
+			    struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_use_list);
+	} else {
+		mtk_vcodec_debug(inst, "No free fb node");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+struct vdec_fb *vp9_rm_from_fb_free_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+
+	mtk_vcodec_debug_leave(inst);
+
+	return fb;
+}
+
+bool vp9_fb_use_list_to_fb_free_list(struct vdec_vp9_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
+		list_move_tail(&node->list, &inst->fb_free_list);
+
+	mtk_vcodec_debug_leave(inst);
+	return true;
+}
+
+void vp9_reset(struct vdec_vp9_inst *inst)
+{
+	vp9_fb_use_list_to_fb_free_list(inst);
+
+	vp9_free_all_sf_ref_fb(inst);
+	inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+
+	if (vpu_dec_reset(&inst->vpu))
+		mtk_vcodec_debug(inst, "vp9_dec_vpu_reset failed");
+
+	if (vp9_setup_buf(inst))
+		mtk_vcodec_debug(inst, "vp9_setup_buf failed");
+}
+
+static void init_list(struct vdec_vp9_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->available_fb_node_list);
+	INIT_LIST_HEAD(&inst->fb_use_list);
+	INIT_LIST_HEAD(&inst->fb_free_list);
+	INIT_LIST_HEAD(&inst->fb_disp_list);
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NODE_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list,
+			      &inst->available_fb_node_list);
+	}
+}
+
+static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
+{
+
+	pic->y_bs_sz = inst->vsi->buf_sz_y_bs;
+	pic->c_bs_sz = inst->vsi->buf_sz_c_bs;
+	pic->y_len_sz = inst->vsi->buf_len_sz_y;
+	pic->c_len_sz = inst->vsi->buf_len_sz_c;
+
+	pic->pic_w = inst->frm_hdr.width;
+	pic->pic_h = inst->frm_hdr.height;
+	pic->buf_w = inst->work_buf.frmbuf_width;
+	pic->buf_h = inst->work_buf.frmbuf_height;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+		 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+		 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	mtk_vcodec_debug_enter(inst);
+
+	*out_fb = vp9_rm_from_fb_disp_list(inst);
+	if (*out_fb)
+		(*out_fb)->status |= FB_ST_DISPLAY;
+}
+
+static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static int vdec_vp9_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct mtk_vcodec_mem *mem;
+	int ret = 0;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vpu_dec_deinit(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vpu_dec_deinit");
+		ret = -EINVAL;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	vp9_free_all_sf_ref_fb(inst);
+	vp9_free_handle(inst);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp9_inst *inst;
+
+	inst = vp9_alloc_inst(ctx);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->frm_cnt = 0;
+	inst->total_frm_cnt = 0;
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP9;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	if (vpu_dec_init(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vp9_dec_vpu_init - %d",
+						inst->vpu.inst_addr);
+		goto err_deinit_inst;
+	}
+
+	inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
+	init_list(inst);
+
+	(*h_vdec) = (unsigned long)inst;
+	return 0;
+
+err_deinit_inst:
+	vp9_free_handle(inst);
+
+	return -EINVAL;
+}
+
+static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	int ret = 0;
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct vdec_vp9_frm_hdr *frm_hdr = &inst->frm_hdr;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	unsigned int data[3];
+	int i;
+
+	mtk_vcodec_debug_enter(inst);
+
+	*res_chg = false;
+
+	if ((bs == NULL) && (fb == NULL)) {
+		mtk_vcodec_debug(inst, "[EOS]");
+		vp9_reset(inst);
+		return ret;
+	}
+
+	if (bs != NULL)
+		mtk_vcodec_debug(inst, "Input BS Size = %ld", bs->size);
+
+	memcpy((void *)inst + sizeof(*inst), (void *)inst,
+	       sizeof(*inst));
+
+	while (1) {
+		struct vdec_fb *cur_fb;
+
+		frm_hdr->resolution_changed = false;
+
+		data[0] = *((unsigned int *)bs->va);
+		data[1] = *((unsigned int *)(bs->va + 4));
+		data[2] = *((unsigned int *)(bs->va + 8));
+
+		vsi->bs = *bs;
+
+		if (fb)
+			vsi->fb = *fb;
+
+		if (!vsi->sf_init) {
+			unsigned int sf_bs_sz;
+			unsigned int sf_bs_off;
+			unsigned char *sf_bs_src;
+			unsigned char *sf_bs_dst;
+
+			sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
+				VP9_SUPER_FRAME_BS_SZ : bs->size;
+			sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
+			sf_bs_src = bs->va + bs->size - sf_bs_sz;
+			sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
+			memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
+		} else {
+			if ((vsi->sf_frm_cnt > 0) &&
+				(vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
+				unsigned int idx = vsi->sf_frm_idx;
+
+				memcpy((void *)vsi->input_ctx.v_frm_sa,
+					   (void *)(vsi->input_ctx.v_frm_sa +
+					   vsi->sf_frm_offset[idx]),
+					   vsi->sf_frm_sz[idx]);
+			}
+		}
+		ret = vpu_dec_start(&inst->vpu, data, 3);
+		if (ret) {
+			mtk_vcodec_err(inst, "vpu_dec_start failed");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		if (vsi->resolution_changed) {
+			if (!vp9_realloc_work_buf(inst)) {
+				ret = -EINVAL;
+				goto DECODE_ERROR;
+			}
+		}
+
+		if (vsi->sf_frm_cnt > 0) {
+			cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
+
+			if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
+				inst->cur_fb = cur_fb;
+			else
+				inst->cur_fb = fb;
+		} else {
+			inst->cur_fb = fb;
+		}
+
+		vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
+			vp9_add_to_fb_use_list(inst, inst->cur_fb);
+
+		mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
+
+		/* the same as VP9_SKIP_FRAME */
+		inst->frm_hdr.show_frame = vsi->show_frm;
+
+		if (vsi->show_exist)
+			mtk_vcodec_debug(inst,
+				"drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+
+		if (vsi->show_exist && (vsi->frm_to_show <
+					VP9_MAX_FRM_BUFF_NUM)) {
+			mtk_vcodec_debug(inst,
+				"Skip Decode drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+			vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
+							vsi->frm_to_show);
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		/* VPU assign the buffer pointer in its address space,
+		  * reassign here
+		  */
+		for (i = 0; i < REFS_PER_FRAME; i++) {
+			unsigned int idx = vsi->frm_refs[i].idx;
+
+			vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
+		}
+
+		if (frm_hdr->resolution_changed) {
+			unsigned int width = inst->frm_hdr.width;
+			unsigned int height = inst->frm_hdr.height;
+			unsigned int frmbuf_width =
+				inst->work_buf.frmbuf_width;
+			unsigned int frmbuf_height =
+				inst->work_buf.frmbuf_height;
+			struct mtk_vcodec_mem tmp_buf =	inst->work_buf.mv_buf;
+			struct vp9_dram_buf tmp_buf2 = vsi->mv_buf;
+			struct vdec_fb tmp_buf3 =
+				inst->work_buf.sf_ref_buf[0];
+
+			memcpy((void *)inst, (void *)inst + sizeof(*inst),
+			       sizeof(*inst));
+
+			inst->frm_hdr.width = width;
+			inst->frm_hdr.height = height;
+			inst->work_buf.frmbuf_width = frmbuf_width;
+			inst->work_buf.frmbuf_height = frmbuf_height;
+			inst->work_buf.mv_buf = tmp_buf;
+			inst->vsi->mv_buf = tmp_buf2;
+			inst->work_buf.sf_ref_buf[0] = tmp_buf3;
+
+			*res_chg = true;
+			mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
+			vp9_add_to_fb_free_list(inst, fb);
+			ret = 0;
+			goto DECODE_ERROR;
+		}
+
+		if (vp9_check_proc(inst) != true) {
+			mtk_vcodec_err(inst, "vp9_check_proc");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		inst->total_frm_cnt++;
+		if (vp9_is_last_sub_frm(inst))
+			break;
+
+		/* for resolution change backup */
+		memcpy((void *)inst + sizeof(*inst), (void *)inst,
+		       sizeof(*inst));
+	}
+	inst->frm_cnt++;
+
+	mtk_vcodec_debug_leave(inst);
+
+DECODE_ERROR:
+	if (ret < 0)
+		vp9_add_to_fb_free_list(inst, fb);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->frm_hdr.width;
+	cr->c.height = inst->frm_hdr.height;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp9_get_param(unsigned long h_vdec,
+			enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	int ret = 0;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 9;
+		break;
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+	default:
+		mtk_vcodec_err(inst, "not support type %d", type);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+static struct vdec_common_if vdec_vp9_if = {
+	vdec_vp9_init,
+	vdec_vp9_decode,
+	vdec_vp9_get_param,
+	vdec_vp9_deinit,
+};
+
+struct vdec_common_if *get_vp9_dec_comm_if(void)
+{
+	return &vdec_vp9_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 6e2f065..f5781d7 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -26,6 +26,7 @@
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
 struct vdec_common_if *get_vp8_dec_comm_if(void);
+struct vdec_common_if *get_vp9_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -39,6 +40,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_vp8_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP9:
+		ctx->dec_if = get_vp9_dec_comm_if();
+		break;
 	default:
 		return -EINVAL;
 	}
-- 
1.7.9.5

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

* [PATCH 6/7] [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
@ 2016-04-13 12:01             ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add vp9 decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 1112 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 7743c81..852d969 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
 		vdec/vdec_vp8_if.o \
+		vdec/vdec_vp9_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
new file mode 100644
index 0000000..6d0fb68
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
@@ -0,0 +1,1108 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
+ *             Kai-Sean Yang <kai-sean.yang@mediatek.com>
+ *		  Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/syscalls.h>
+#include <linux/delay.h>
+#include <linux/time.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_drv_base.h"
+#include "vdec_vpu_if.h"
+
+#define VP9_SUPER_FRAME_BS_SZ 64
+
+#define REFS_PER_FRAME 3
+#define REF_FRAMES_LOG2 3
+#define REF_FRAMES (1 << REF_FRAMES_LOG2)
+#define VP9_MAX_FRM_BUFF_NUM (9 + 0)
+#define VP9_MAX_FRM_BUFF_NODE_NUM (9 + 9)
+
+
+struct vdec_vp9_frm_hdr {
+	unsigned int width;
+	unsigned int height;
+	unsigned char show_frame;
+	unsigned char resolution_changed;
+};
+
+struct vdec_vp9_work_buf {
+	unsigned int frmbuf_width;
+	unsigned int frmbuf_height;
+	struct mtk_vcodec_mem seg_id_buf;
+	struct mtk_vcodec_mem tile_buf;
+	struct mtk_vcodec_mem count_tbl_buf;
+	struct mtk_vcodec_mem prob_tbl_buf;
+	struct mtk_vcodec_mem mv_buf;
+	struct vdec_fb sf_ref_buf[VP9_MAX_FRM_BUFF_NUM-1];
+};
+
+struct vp9_input_ctx {
+	unsigned long v_fifo_sa;
+	unsigned long v_fifo_ea;
+	unsigned long p_fifo_sa;
+	unsigned long p_fifo_ea;
+	unsigned long v_frm_sa;
+	unsigned long v_frm_ea;
+	unsigned long p_frm_sa;
+	unsigned long p_frm_end;
+	unsigned int frm_sz;
+	unsigned int uncompress_sz;
+};
+
+struct vp9_dram_buf {
+	unsigned long va;
+	unsigned long pa;
+	unsigned int sz;
+	unsigned int vpua;
+};
+
+struct vp9_fb_info {
+	struct vdec_fb *fb;
+	struct vp9_dram_buf y_buf;
+	struct vp9_dram_buf c_buf;
+	struct vp9_dram_buf ufo_len_y;
+	struct vp9_dram_buf ufo_len_c;
+	unsigned int y_width;
+	unsigned int y_height;
+	unsigned int y_crop_width;
+	unsigned int y_crop_height;
+
+	unsigned int c_width;
+	unsigned int c_height;
+	unsigned int c_crop_width;
+	unsigned int c_crop_height;
+
+	unsigned int frm_num;
+};
+
+struct vp9_ref_cnt_buf {
+	struct vp9_fb_info buf;
+	unsigned int ref_cnt;
+};
+
+struct vp9_scale_factors {
+	int x_scale_fp;
+	int y_scale_fp;
+	int x_step_q4;
+	int y_step_q4;
+	unsigned int ref_scaling_en;
+};
+
+struct vp9_ref_buf {
+	struct vp9_fb_info *buf;
+	struct vp9_scale_factors scale_factors;
+	unsigned int idx;
+};
+
+struct vp9_sf_ref_fb {
+	struct vdec_fb fb;
+	int used;
+	int idx;
+};
+
+
+/*
+ * struct vdec_vp9_vsi - shared buffer between host and VPU driver
+ */
+struct vdec_vp9_vsi {
+	unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
+	struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUFF_NUM-1];
+	int sf_next_ref_fb_idx;
+	unsigned int sf_frm_cnt;
+	unsigned int sf_frm_offset[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_sz[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_idx;
+	unsigned int sf_init;
+	struct vdec_fb fb;
+	struct mtk_vcodec_mem bs;
+	struct vdec_fb cur_fb;
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int buf_sz_y_bs;
+	unsigned int buf_sz_c_bs;
+	unsigned int buf_len_sz_y;
+	unsigned int buf_len_sz_c;
+	unsigned int profile;
+	unsigned int show_frm;
+	unsigned int show_exist;
+	unsigned int frm_to_show;
+	unsigned int refresh_frm_flags;
+	unsigned int resolution_changed;
+
+	struct vp9_input_ctx input_ctx;
+	struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUFF_NUM];
+	int ref_frm_map[REF_FRAMES];
+	unsigned int new_fb_idx;
+	unsigned int frm_num;
+	struct vp9_dram_buf seg_id_buf;
+	struct vp9_dram_buf tile_buf;
+	struct vp9_dram_buf count_tbl_buf;
+	struct vp9_dram_buf prob_tbl_buf;
+	struct vp9_dram_buf mv_buf;
+	struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
+
+};
+
+struct vdec_vp9_inst {
+	struct vdec_vp9_work_buf work_buf;
+	struct vdec_vp9_frm_hdr frm_hdr;
+	struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUFF_NODE_NUM];
+	struct list_head available_fb_node_list;
+	struct list_head fb_use_list;
+	struct list_head fb_free_list;
+	struct list_head fb_disp_list;
+	struct vdec_fb *cur_fb;
+	unsigned int frm_cnt;
+	unsigned int total_frm_cnt;
+	void *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp9_vsi *vsi;
+	unsigned int show_reg;
+	struct file *log;
+	struct mtk_vcodec_mem mem;
+};
+
+static int vp9_setup_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vpu.vsi;
+
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	if ((vsi->mv_buf.va == 0) || (vsi->mv_buf.pa == 0) ||
+		(vsi->mv_buf.sz == 0))
+		return -EINVAL;
+
+	mtk_vcodec_debug(inst, "VP9_MV_BUF_Addr: 0x%lX (0x%lX)",
+		     vsi->mv_buf.va, vsi->mv_buf.pa);
+	return 0;
+}
+
+static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (fb == &vsi->sf_ref_fb[i].fb)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1)
+		return false;
+
+	return true;
+}
+
+static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
+					*inst, void *addr)
+{
+	struct vdec_fb *fb;
+	struct vdec_fb_node *node;
+
+	list_for_each_entry(node, &inst->fb_use_list, list) {
+		fb = (struct vdec_fb *)node->fb;
+		if (fb->base_y.va == addr) {
+			list_move_tail(&node->list,
+				       &inst->available_fb_node_list);
+			break;
+		}
+	}
+
+	return fb;
+}
+
+static bool vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+		if (node) {
+			node->fb = fb;
+			list_move_tail(&node->list, &inst->fb_free_list);
+		} else
+			mtk_vcodec_debug(inst, "No free fb node");
+	}
+
+	return true;
+}
+
+static bool vp9_free_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	struct vp9_sf_ref_fb *sf_ref_fb =
+		container_of(fb, struct vp9_sf_ref_fb, fb);
+
+	sf_ref_fb->used = 0;
+
+	return true;
+}
+
+static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
+			   int new_idx)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int ref_idx = *idx;
+
+	if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
+		vsi->frm_bufs[ref_idx].ref_cnt--;
+
+		if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
+			if (!vp9_is_sf_ref_fb(inst,
+					      vsi->frm_bufs[ref_idx].buf.fb)) {
+				struct vdec_fb *fb;
+
+				fb = vp9_rm_from_fb_use_list(inst,
+				     vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
+				vp9_add_to_fb_free_list(inst, fb);
+			} else
+				vp9_free_sf_ref_fb(
+					inst, vsi->frm_bufs[ref_idx].buf.fb);
+		}
+	}
+
+	*idx = new_idx;
+	vsi->frm_bufs[new_idx].ref_cnt++;
+}
+
+static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va) {
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_y);
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_c);
+			vsi->sf_ref_fb[i].used = 0;
+		}
+	}
+}
+
+static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vdec_fb *fb_dst_ptr, *fb_src_ptr;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va &&
+		    vsi->sf_ref_fb[i].used == 0) {
+			return i;
+		}
+	}
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va == NULL)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1) {
+		mtk_vcodec_err(inst, "List Full");
+		return -1;
+	}
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_y;
+	mem->size = vsi->buf_sz_y_bs +
+		    vsi->buf_len_sz_y;
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return -1;
+	}
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf y_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height);
+
+	fb_dst_ptr = &vsi->sf_ref_fb[i].fb;
+	fb_src_ptr = &inst->work_buf.sf_ref_buf[i];
+	fb_dst_ptr->base_y.va = fb_src_ptr->base_y.va;
+	fb_dst_ptr->base_y.dma_addr = fb_src_ptr->base_y.dma_addr;
+	fb_dst_ptr->base_y.size = fb_src_ptr->base_y.size;
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_c;
+	mem->size = vsi->buf_sz_c_bs +
+		    vsi->buf_len_sz_c;
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf c_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf c_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height / 2);
+
+	fb_dst_ptr->base_c.va = fb_src_ptr->base_c.va;
+	fb_dst_ptr->base_c.dma_addr = fb_src_ptr->base_c.dma_addr;
+	fb_dst_ptr->base_c.size = fb_src_ptr->base_c.size;
+
+	vsi->sf_ref_fb[i].used = 0;
+	vsi->sf_ref_fb[i].idx = i;
+
+	return i;
+}
+
+bool vp9_realloc_work_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int result;
+	struct mtk_vcodec_mem *mem;
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
+		     inst->frm_hdr.resolution_changed,
+		     inst->frm_hdr.width,
+		     inst->frm_hdr.height,
+		     inst->work_buf.frmbuf_width,
+		     inst->work_buf.frmbuf_height);
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	/* Free First */
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+	/* Alloc Later */
+	mem->size = ((inst->work_buf.frmbuf_width / 64) *
+		    (inst->work_buf.frmbuf_height / 64) + 2) * 36 * 16;
+
+	result = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot allocate mv_buf");
+		return false;
+	}
+	/* Set the va again */
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	vp9_free_all_sf_ref_fb(inst);
+	vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+	result = vp9_setup_buf(inst);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot vp9_setup_buf");
+		return false;
+	}
+
+	inst->frm_hdr.resolution_changed = true;
+
+	return true;
+}
+
+static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_disp_list);
+	} else {
+		mtk_vcodec_debug(inst, "List Full");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vp9_fb_info *frm_to_show;
+	int ref_index = 0, mask;
+
+	for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
+		if (mask & 1)
+			vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
+				       vsi->new_fb_idx);
+		++ref_index;
+	}
+
+	frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
+	vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
+
+	if (frm_to_show->fb != inst->cur_fb) {
+		if ((frm_to_show->fb != NULL) &&
+			(inst->cur_fb->base_y.size >=
+				frm_to_show->fb->base_y.size)) {
+			memcpy((void *)inst->cur_fb->base_y.va,
+				(void *)frm_to_show->fb->base_y.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height);
+			memcpy((void *)inst->cur_fb->base_c.va,
+				(void *)frm_to_show->fb->base_c.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height / 2);
+		} else {
+			mtk_vcodec_debug(inst,
+				"inst->cur_fb->base_y.size=%lx, frm_to_show->fb.base_y.size=%lx",
+				inst->cur_fb->base_y.size,
+				frm_to_show->fb->base_y.size);
+		}
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, inst->cur_fb);
+		}
+	} else {
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
+		}
+	}
+
+	if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
+		if (!vp9_is_sf_ref_fb(
+			inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
+			struct vdec_fb *fb;
+
+			fb = vp9_rm_from_fb_use_list(inst,
+			     vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
+
+			vp9_add_to_fb_free_list(inst, fb);
+		} else
+			vp9_free_sf_ref_fb(
+				inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
+	}
+
+	if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
+		vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+}
+
+static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_ctx *ctx = inst->ctx;
+	unsigned int irq_status;
+
+	mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						MTK_INST_IRQ_RECEIVED,
+						WAIT_INTR_TIMEOUT_MS);
+
+	irq_status = ctx->irq_status;
+	mtk_vcodec_debug(inst, "isr return %x", irq_status);
+
+	if (irq_status & 0x10000)
+		return true;
+	else
+		return false;
+}
+
+struct vdec_vp9_inst *vp9_alloc_inst(void *ctx)
+{
+	int result;
+	struct mtk_vcodec_mem mem;
+	struct vdec_vp9_inst *inst;
+
+	mem.size = sizeof(struct vdec_vp9_inst) * 2;
+	result = mtk_vcodec_mem_alloc(ctx, &mem);
+	if (result)
+		return NULL;
+
+	inst = mem.va;
+	inst->mem = mem;
+
+	return inst;
+}
+
+void vp9_free_handle(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_mem mem;
+
+	mem = inst->mem;
+	if (mem.va)
+		mtk_vcodec_mem_free(inst->ctx, &mem);
+}
+
+bool vp9_init_proc(struct vdec_vp9_inst *inst,
+		   struct vdec_pic_info *pic_info)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	pic_info->pic_w = vsi->pic_w;
+	pic_info->pic_h = vsi->pic_h;
+	pic_info->buf_w = vsi->buf_w;
+	pic_info->buf_h = vsi->buf_h;
+
+	mtk_vcodec_debug(inst,
+			"(PicW,PicH,BufW,BufH) = (%d,%d,%d,%d) profile=%d",
+			pic_info->pic_w, pic_info->pic_h,
+			pic_info->buf_w, pic_info->buf_h, vsi->profile);
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	/* ----> HW limitation */
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	if (vsi->profile > 0) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support profile(%d) > 0",
+			     vsi->profile);
+		return false;
+	}
+	if ((inst->work_buf.frmbuf_width > 4096) ||
+	    (inst->work_buf.frmbuf_height > 2304)) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support (W,H) = (%d,%d)",
+			     inst->work_buf.frmbuf_width,
+			     inst->work_buf.frmbuf_height);
+		return false;
+	}
+	/* <---- HW limitation */
+
+	return true;
+}
+
+bool vp9_check_proc(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	bool ret = false;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vsi->show_exist) {
+		vp9_swap_frm_bufs(inst);
+		mtk_vcodec_debug(inst, "Decode Ok @%d (show_exist)",
+				 vsi->frm_num);
+		vsi->frm_num++;
+		return true;
+	}
+
+	ret = vp9_wait_dec_end(inst);
+	if (!ret) {
+		mtk_vcodec_err(inst, "Decode NG, Decode Timeout @[%d]",
+			       vsi->frm_num);
+		return false;
+	}
+
+	if (vpu_dec_end(&inst->vpu)) {
+		mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
+		return false;
+	}
+
+	vp9_swap_frm_bufs(inst);
+	mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
+		     inst->frm_hdr.width, inst->frm_hdr.height);
+
+	vsi->frm_num++;
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
+		return true;
+
+	return false;
+}
+
+struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_disp_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+
+	return fb;
+}
+
+bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
+			    struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_use_list);
+	} else {
+		mtk_vcodec_debug(inst, "No free fb node");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+struct vdec_fb *vp9_rm_from_fb_free_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+
+	mtk_vcodec_debug_leave(inst);
+
+	return fb;
+}
+
+bool vp9_fb_use_list_to_fb_free_list(struct vdec_vp9_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
+		list_move_tail(&node->list, &inst->fb_free_list);
+
+	mtk_vcodec_debug_leave(inst);
+	return true;
+}
+
+void vp9_reset(struct vdec_vp9_inst *inst)
+{
+	vp9_fb_use_list_to_fb_free_list(inst);
+
+	vp9_free_all_sf_ref_fb(inst);
+	inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+
+	if (vpu_dec_reset(&inst->vpu))
+		mtk_vcodec_debug(inst, "vp9_dec_vpu_reset failed");
+
+	if (vp9_setup_buf(inst))
+		mtk_vcodec_debug(inst, "vp9_setup_buf failed");
+}
+
+static void init_list(struct vdec_vp9_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->available_fb_node_list);
+	INIT_LIST_HEAD(&inst->fb_use_list);
+	INIT_LIST_HEAD(&inst->fb_free_list);
+	INIT_LIST_HEAD(&inst->fb_disp_list);
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NODE_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list,
+			      &inst->available_fb_node_list);
+	}
+}
+
+static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
+{
+
+	pic->y_bs_sz = inst->vsi->buf_sz_y_bs;
+	pic->c_bs_sz = inst->vsi->buf_sz_c_bs;
+	pic->y_len_sz = inst->vsi->buf_len_sz_y;
+	pic->c_len_sz = inst->vsi->buf_len_sz_c;
+
+	pic->pic_w = inst->frm_hdr.width;
+	pic->pic_h = inst->frm_hdr.height;
+	pic->buf_w = inst->work_buf.frmbuf_width;
+	pic->buf_h = inst->work_buf.frmbuf_height;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+		 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+		 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	mtk_vcodec_debug_enter(inst);
+
+	*out_fb = vp9_rm_from_fb_disp_list(inst);
+	if (*out_fb)
+		(*out_fb)->status |= FB_ST_DISPLAY;
+}
+
+static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static int vdec_vp9_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct mtk_vcodec_mem *mem;
+	int ret = 0;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vpu_dec_deinit(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vpu_dec_deinit");
+		ret = -EINVAL;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	vp9_free_all_sf_ref_fb(inst);
+	vp9_free_handle(inst);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp9_inst *inst;
+
+	inst = vp9_alloc_inst(ctx);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->frm_cnt = 0;
+	inst->total_frm_cnt = 0;
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP9;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	if (vpu_dec_init(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vp9_dec_vpu_init - %d",
+						inst->vpu.inst_addr);
+		goto err_deinit_inst;
+	}
+
+	inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
+	init_list(inst);
+
+	(*h_vdec) = (unsigned long)inst;
+	return 0;
+
+err_deinit_inst:
+	vp9_free_handle(inst);
+
+	return -EINVAL;
+}
+
+static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	int ret = 0;
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct vdec_vp9_frm_hdr *frm_hdr = &inst->frm_hdr;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	unsigned int data[3];
+	int i;
+
+	mtk_vcodec_debug_enter(inst);
+
+	*res_chg = false;
+
+	if ((bs == NULL) && (fb == NULL)) {
+		mtk_vcodec_debug(inst, "[EOS]");
+		vp9_reset(inst);
+		return ret;
+	}
+
+	if (bs != NULL)
+		mtk_vcodec_debug(inst, "Input BS Size = %ld", bs->size);
+
+	memcpy((void *)inst + sizeof(*inst), (void *)inst,
+	       sizeof(*inst));
+
+	while (1) {
+		struct vdec_fb *cur_fb;
+
+		frm_hdr->resolution_changed = false;
+
+		data[0] = *((unsigned int *)bs->va);
+		data[1] = *((unsigned int *)(bs->va + 4));
+		data[2] = *((unsigned int *)(bs->va + 8));
+
+		vsi->bs = *bs;
+
+		if (fb)
+			vsi->fb = *fb;
+
+		if (!vsi->sf_init) {
+			unsigned int sf_bs_sz;
+			unsigned int sf_bs_off;
+			unsigned char *sf_bs_src;
+			unsigned char *sf_bs_dst;
+
+			sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
+				VP9_SUPER_FRAME_BS_SZ : bs->size;
+			sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
+			sf_bs_src = bs->va + bs->size - sf_bs_sz;
+			sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
+			memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
+		} else {
+			if ((vsi->sf_frm_cnt > 0) &&
+				(vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
+				unsigned int idx = vsi->sf_frm_idx;
+
+				memcpy((void *)vsi->input_ctx.v_frm_sa,
+					   (void *)(vsi->input_ctx.v_frm_sa +
+					   vsi->sf_frm_offset[idx]),
+					   vsi->sf_frm_sz[idx]);
+			}
+		}
+		ret = vpu_dec_start(&inst->vpu, data, 3);
+		if (ret) {
+			mtk_vcodec_err(inst, "vpu_dec_start failed");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		if (vsi->resolution_changed) {
+			if (!vp9_realloc_work_buf(inst)) {
+				ret = -EINVAL;
+				goto DECODE_ERROR;
+			}
+		}
+
+		if (vsi->sf_frm_cnt > 0) {
+			cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
+
+			if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
+				inst->cur_fb = cur_fb;
+			else
+				inst->cur_fb = fb;
+		} else {
+			inst->cur_fb = fb;
+		}
+
+		vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
+			vp9_add_to_fb_use_list(inst, inst->cur_fb);
+
+		mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
+
+		/* the same as VP9_SKIP_FRAME */
+		inst->frm_hdr.show_frame = vsi->show_frm;
+
+		if (vsi->show_exist)
+			mtk_vcodec_debug(inst,
+				"drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+
+		if (vsi->show_exist && (vsi->frm_to_show <
+					VP9_MAX_FRM_BUFF_NUM)) {
+			mtk_vcodec_debug(inst,
+				"Skip Decode drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+			vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
+							vsi->frm_to_show);
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		/* VPU assign the buffer pointer in its address space,
+		  * reassign here
+		  */
+		for (i = 0; i < REFS_PER_FRAME; i++) {
+			unsigned int idx = vsi->frm_refs[i].idx;
+
+			vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
+		}
+
+		if (frm_hdr->resolution_changed) {
+			unsigned int width = inst->frm_hdr.width;
+			unsigned int height = inst->frm_hdr.height;
+			unsigned int frmbuf_width =
+				inst->work_buf.frmbuf_width;
+			unsigned int frmbuf_height =
+				inst->work_buf.frmbuf_height;
+			struct mtk_vcodec_mem tmp_buf =	inst->work_buf.mv_buf;
+			struct vp9_dram_buf tmp_buf2 = vsi->mv_buf;
+			struct vdec_fb tmp_buf3 =
+				inst->work_buf.sf_ref_buf[0];
+
+			memcpy((void *)inst, (void *)inst + sizeof(*inst),
+			       sizeof(*inst));
+
+			inst->frm_hdr.width = width;
+			inst->frm_hdr.height = height;
+			inst->work_buf.frmbuf_width = frmbuf_width;
+			inst->work_buf.frmbuf_height = frmbuf_height;
+			inst->work_buf.mv_buf = tmp_buf;
+			inst->vsi->mv_buf = tmp_buf2;
+			inst->work_buf.sf_ref_buf[0] = tmp_buf3;
+
+			*res_chg = true;
+			mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
+			vp9_add_to_fb_free_list(inst, fb);
+			ret = 0;
+			goto DECODE_ERROR;
+		}
+
+		if (vp9_check_proc(inst) != true) {
+			mtk_vcodec_err(inst, "vp9_check_proc");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		inst->total_frm_cnt++;
+		if (vp9_is_last_sub_frm(inst))
+			break;
+
+		/* for resolution change backup */
+		memcpy((void *)inst + sizeof(*inst), (void *)inst,
+		       sizeof(*inst));
+	}
+	inst->frm_cnt++;
+
+	mtk_vcodec_debug_leave(inst);
+
+DECODE_ERROR:
+	if (ret < 0)
+		vp9_add_to_fb_free_list(inst, fb);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->frm_hdr.width;
+	cr->c.height = inst->frm_hdr.height;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp9_get_param(unsigned long h_vdec,
+			enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	int ret = 0;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 9;
+		break;
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+	default:
+		mtk_vcodec_err(inst, "not support type %d", type);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+static struct vdec_common_if vdec_vp9_if = {
+	vdec_vp9_init,
+	vdec_vp9_decode,
+	vdec_vp9_get_param,
+	vdec_vp9_deinit,
+};
+
+struct vdec_common_if *get_vp9_dec_comm_if(void)
+{
+	return &vdec_vp9_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 6e2f065..f5781d7 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -26,6 +26,7 @@
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
 struct vdec_common_if *get_vp8_dec_comm_if(void);
+struct vdec_common_if *get_vp9_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -39,6 +40,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_vp8_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP9:
+		ctx->dec_if = get_vp9_dec_comm_if();
+		break;
 	default:
 		return -EINVAL;
 	}
-- 
1.7.9.5

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

* [PATCH 6/7] [media] vcodec: mediatek: Add Mediatek VP9 Video Decoder Driver
@ 2016-04-13 12:01             ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add vp9 decoder driver for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 drivers/media/platform/mtk-vcodec/Makefile         |    1 +
 .../media/platform/mtk-vcodec/vdec/vdec_vp9_if.c   | 1108 ++++++++++++++++++++
 drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |    3 +
 3 files changed, 1112 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c

diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
index 7743c81..852d969 100644
--- a/drivers/media/platform/mtk-vcodec/Makefile
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
 
 mtk-vcodec-dec-y := vdec/vdec_h264_if.o \
 		vdec/vdec_vp8_if.o \
+		vdec/vdec_vp9_if.o \
 		mtk_vcodec_dec_drv.o \
 		vdec_drv_if.o \
 		vdec_vpu_if.o \
diff --git a/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
new file mode 100644
index 0000000..6d0fb68
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/vdec/vdec_vp9_if.c
@@ -0,0 +1,1108 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
+ *             Kai-Sean Yang <kai-sean.yang@mediatek.com>
+ *		  Tiffany Lin <tiffany.lin@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/syscalls.h>
+#include <linux/delay.h>
+#include <linux/time.h>
+
+#include "mtk_vcodec_intr.h"
+#include "vdec_drv_base.h"
+#include "vdec_vpu_if.h"
+
+#define VP9_SUPER_FRAME_BS_SZ 64
+
+#define REFS_PER_FRAME 3
+#define REF_FRAMES_LOG2 3
+#define REF_FRAMES (1 << REF_FRAMES_LOG2)
+#define VP9_MAX_FRM_BUFF_NUM (9 + 0)
+#define VP9_MAX_FRM_BUFF_NODE_NUM (9 + 9)
+
+
+struct vdec_vp9_frm_hdr {
+	unsigned int width;
+	unsigned int height;
+	unsigned char show_frame;
+	unsigned char resolution_changed;
+};
+
+struct vdec_vp9_work_buf {
+	unsigned int frmbuf_width;
+	unsigned int frmbuf_height;
+	struct mtk_vcodec_mem seg_id_buf;
+	struct mtk_vcodec_mem tile_buf;
+	struct mtk_vcodec_mem count_tbl_buf;
+	struct mtk_vcodec_mem prob_tbl_buf;
+	struct mtk_vcodec_mem mv_buf;
+	struct vdec_fb sf_ref_buf[VP9_MAX_FRM_BUFF_NUM-1];
+};
+
+struct vp9_input_ctx {
+	unsigned long v_fifo_sa;
+	unsigned long v_fifo_ea;
+	unsigned long p_fifo_sa;
+	unsigned long p_fifo_ea;
+	unsigned long v_frm_sa;
+	unsigned long v_frm_ea;
+	unsigned long p_frm_sa;
+	unsigned long p_frm_end;
+	unsigned int frm_sz;
+	unsigned int uncompress_sz;
+};
+
+struct vp9_dram_buf {
+	unsigned long va;
+	unsigned long pa;
+	unsigned int sz;
+	unsigned int vpua;
+};
+
+struct vp9_fb_info {
+	struct vdec_fb *fb;
+	struct vp9_dram_buf y_buf;
+	struct vp9_dram_buf c_buf;
+	struct vp9_dram_buf ufo_len_y;
+	struct vp9_dram_buf ufo_len_c;
+	unsigned int y_width;
+	unsigned int y_height;
+	unsigned int y_crop_width;
+	unsigned int y_crop_height;
+
+	unsigned int c_width;
+	unsigned int c_height;
+	unsigned int c_crop_width;
+	unsigned int c_crop_height;
+
+	unsigned int frm_num;
+};
+
+struct vp9_ref_cnt_buf {
+	struct vp9_fb_info buf;
+	unsigned int ref_cnt;
+};
+
+struct vp9_scale_factors {
+	int x_scale_fp;
+	int y_scale_fp;
+	int x_step_q4;
+	int y_step_q4;
+	unsigned int ref_scaling_en;
+};
+
+struct vp9_ref_buf {
+	struct vp9_fb_info *buf;
+	struct vp9_scale_factors scale_factors;
+	unsigned int idx;
+};
+
+struct vp9_sf_ref_fb {
+	struct vdec_fb fb;
+	int used;
+	int idx;
+};
+
+
+/*
+ * struct vdec_vp9_vsi - shared buffer between host and VPU driver
+ */
+struct vdec_vp9_vsi {
+	unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
+	struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUFF_NUM-1];
+	int sf_next_ref_fb_idx;
+	unsigned int sf_frm_cnt;
+	unsigned int sf_frm_offset[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_sz[VP9_MAX_FRM_BUFF_NUM-1];
+	unsigned int sf_frm_idx;
+	unsigned int sf_init;
+	struct vdec_fb fb;
+	struct mtk_vcodec_mem bs;
+	struct vdec_fb cur_fb;
+	unsigned int pic_w;
+	unsigned int pic_h;
+	unsigned int buf_w;
+	unsigned int buf_h;
+	unsigned int buf_sz_y_bs;
+	unsigned int buf_sz_c_bs;
+	unsigned int buf_len_sz_y;
+	unsigned int buf_len_sz_c;
+	unsigned int profile;
+	unsigned int show_frm;
+	unsigned int show_exist;
+	unsigned int frm_to_show;
+	unsigned int refresh_frm_flags;
+	unsigned int resolution_changed;
+
+	struct vp9_input_ctx input_ctx;
+	struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUFF_NUM];
+	int ref_frm_map[REF_FRAMES];
+	unsigned int new_fb_idx;
+	unsigned int frm_num;
+	struct vp9_dram_buf seg_id_buf;
+	struct vp9_dram_buf tile_buf;
+	struct vp9_dram_buf count_tbl_buf;
+	struct vp9_dram_buf prob_tbl_buf;
+	struct vp9_dram_buf mv_buf;
+	struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
+
+};
+
+struct vdec_vp9_inst {
+	struct vdec_vp9_work_buf work_buf;
+	struct vdec_vp9_frm_hdr frm_hdr;
+	struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUFF_NODE_NUM];
+	struct list_head available_fb_node_list;
+	struct list_head fb_use_list;
+	struct list_head fb_free_list;
+	struct list_head fb_disp_list;
+	struct vdec_fb *cur_fb;
+	unsigned int frm_cnt;
+	unsigned int total_frm_cnt;
+	void *ctx;
+	struct vdec_vpu_inst vpu;
+	struct vdec_vp9_vsi *vsi;
+	unsigned int show_reg;
+	struct file *log;
+	struct mtk_vcodec_mem mem;
+};
+
+static int vp9_setup_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vpu.vsi;
+
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	if ((vsi->mv_buf.va == 0) || (vsi->mv_buf.pa == 0) ||
+		(vsi->mv_buf.sz == 0))
+		return -EINVAL;
+
+	mtk_vcodec_debug(inst, "VP9_MV_BUF_Addr: 0x%lX (0x%lX)",
+		     vsi->mv_buf.va, vsi->mv_buf.pa);
+	return 0;
+}
+
+static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (fb == &vsi->sf_ref_fb[i].fb)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1)
+		return false;
+
+	return true;
+}
+
+static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
+					*inst, void *addr)
+{
+	struct vdec_fb *fb;
+	struct vdec_fb_node *node;
+
+	list_for_each_entry(node, &inst->fb_use_list, list) {
+		fb = (struct vdec_fb *)node->fb;
+		if (fb->base_y.va == addr) {
+			list_move_tail(&node->list,
+				       &inst->available_fb_node_list);
+			break;
+		}
+	}
+
+	return fb;
+}
+
+static bool vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (fb) {
+		node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+		if (node) {
+			node->fb = fb;
+			list_move_tail(&node->list, &inst->fb_free_list);
+		} else
+			mtk_vcodec_debug(inst, "No free fb node");
+	}
+
+	return true;
+}
+
+static bool vp9_free_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
+{
+	struct vp9_sf_ref_fb *sf_ref_fb =
+		container_of(fb, struct vp9_sf_ref_fb, fb);
+
+	sf_ref_fb->used = 0;
+
+	return true;
+}
+
+static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
+			   int new_idx)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int ref_idx = *idx;
+
+	if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
+		vsi->frm_bufs[ref_idx].ref_cnt--;
+
+		if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
+			if (!vp9_is_sf_ref_fb(inst,
+					      vsi->frm_bufs[ref_idx].buf.fb)) {
+				struct vdec_fb *fb;
+
+				fb = vp9_rm_from_fb_use_list(inst,
+				     vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
+				vp9_add_to_fb_free_list(inst, fb);
+			} else
+				vp9_free_sf_ref_fb(
+					inst, vsi->frm_bufs[ref_idx].buf.fb);
+		}
+	}
+
+	*idx = new_idx;
+	vsi->frm_bufs[new_idx].ref_cnt++;
+}
+
+static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va) {
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_y);
+			mtk_vcodec_mem_free(inst->ctx,
+				    &inst->work_buf.sf_ref_buf[i].base_c);
+			vsi->sf_ref_fb[i].used = 0;
+		}
+	}
+}
+
+static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
+{
+	int i;
+	struct mtk_vcodec_mem *mem;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vdec_fb *fb_dst_ptr, *fb_src_ptr;
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va &&
+		    vsi->sf_ref_fb[i].used == 0) {
+			return i;
+		}
+	}
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NUM - 1; i++) {
+		if (inst->work_buf.sf_ref_buf[i].base_y.va == NULL)
+			break;
+	}
+
+	if (i == VP9_MAX_FRM_BUFF_NUM - 1) {
+		mtk_vcodec_err(inst, "List Full");
+		return -1;
+	}
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_y;
+	mem->size = vsi->buf_sz_y_bs +
+		    vsi->buf_len_sz_y;
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return -1;
+	}
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf y_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height);
+
+	fb_dst_ptr = &vsi->sf_ref_fb[i].fb;
+	fb_src_ptr = &inst->work_buf.sf_ref_buf[i];
+	fb_dst_ptr->base_y.va = fb_src_ptr->base_y.va;
+	fb_dst_ptr->base_y.dma_addr = fb_src_ptr->base_y.dma_addr;
+	fb_dst_ptr->base_y.size = fb_src_ptr->base_y.size;
+
+	mem = &inst->work_buf.sf_ref_buf[i].base_c;
+	mem->size = vsi->buf_sz_c_bs +
+		    vsi->buf_len_sz_c;
+
+	if (mtk_vcodec_mem_alloc(inst->ctx, mem)) {
+		mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf c_buf");
+		return -1;
+	}
+
+	mtk_vcodec_debug(inst, "allocate sf_ref_buf c_buf = 0x%lx, %d",
+			mem->size,
+			inst->work_buf.frmbuf_width *
+			inst->work_buf.frmbuf_height / 2);
+
+	fb_dst_ptr->base_c.va = fb_src_ptr->base_c.va;
+	fb_dst_ptr->base_c.dma_addr = fb_src_ptr->base_c.dma_addr;
+	fb_dst_ptr->base_c.size = fb_src_ptr->base_c.size;
+
+	vsi->sf_ref_fb[i].used = 0;
+	vsi->sf_ref_fb[i].idx = i;
+
+	return i;
+}
+
+bool vp9_realloc_work_buf(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	int result;
+	struct mtk_vcodec_mem *mem;
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
+		     inst->frm_hdr.resolution_changed,
+		     inst->frm_hdr.width,
+		     inst->frm_hdr.height,
+		     inst->work_buf.frmbuf_width,
+		     inst->work_buf.frmbuf_height);
+
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	/* Free First */
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+	/* Alloc Later */
+	mem->size = ((inst->work_buf.frmbuf_width / 64) *
+		    (inst->work_buf.frmbuf_height / 64) + 2) * 36 * 16;
+
+	result = mtk_vcodec_mem_alloc(inst->ctx, mem);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot allocate mv_buf");
+		return false;
+	}
+	/* Set the va again */
+	vsi->mv_buf.va = (unsigned long)inst->work_buf.mv_buf.va;
+	vsi->mv_buf.pa = (unsigned long)inst->work_buf.mv_buf.dma_addr;
+	vsi->mv_buf.sz = (unsigned long)inst->work_buf.mv_buf.size;
+
+	vp9_free_all_sf_ref_fb(inst);
+	vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+	result = vp9_setup_buf(inst);
+	if (result) {
+		mtk_vcodec_err(inst, "Cannot vp9_setup_buf");
+		return false;
+	}
+
+	inst->frm_hdr.resolution_changed = true;
+
+	return true;
+}
+
+static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
+			     struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_disp_list);
+	} else {
+		mtk_vcodec_debug(inst, "List Full");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	struct vp9_fb_info *frm_to_show;
+	int ref_index = 0, mask;
+
+	for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
+		if (mask & 1)
+			vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
+				       vsi->new_fb_idx);
+		++ref_index;
+	}
+
+	frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
+	vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
+
+	if (frm_to_show->fb != inst->cur_fb) {
+		if ((frm_to_show->fb != NULL) &&
+			(inst->cur_fb->base_y.size >=
+				frm_to_show->fb->base_y.size)) {
+			memcpy((void *)inst->cur_fb->base_y.va,
+				(void *)frm_to_show->fb->base_y.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height);
+			memcpy((void *)inst->cur_fb->base_c.va,
+				(void *)frm_to_show->fb->base_c.va,
+				inst->work_buf.frmbuf_width *
+				inst->work_buf.frmbuf_height / 2);
+		} else {
+			mtk_vcodec_debug(inst,
+				"inst->cur_fb->base_y.size=%lx, frm_to_show->fb.base_y.size=%lx",
+				inst->cur_fb->base_y.size,
+				frm_to_show->fb->base_y.size);
+		}
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, inst->cur_fb);
+		}
+	} else {
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
+			if (inst->frm_hdr.show_frame)
+				vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
+		}
+	}
+
+	if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
+		if (!vp9_is_sf_ref_fb(
+			inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
+			struct vdec_fb *fb;
+
+			fb = vp9_rm_from_fb_use_list(inst,
+			     vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
+
+			vp9_add_to_fb_free_list(inst, fb);
+		} else
+			vp9_free_sf_ref_fb(
+				inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
+	}
+
+	if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
+		vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+}
+
+static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_ctx *ctx = inst->ctx;
+	unsigned int irq_status;
+
+	mtk_vcodec_wait_for_done_ctx(inst->ctx,
+						MTK_INST_IRQ_RECEIVED,
+						WAIT_INTR_TIMEOUT_MS);
+
+	irq_status = ctx->irq_status;
+	mtk_vcodec_debug(inst, "isr return %x", irq_status);
+
+	if (irq_status & 0x10000)
+		return true;
+	else
+		return false;
+}
+
+struct vdec_vp9_inst *vp9_alloc_inst(void *ctx)
+{
+	int result;
+	struct mtk_vcodec_mem mem;
+	struct vdec_vp9_inst *inst;
+
+	mem.size = sizeof(struct vdec_vp9_inst) * 2;
+	result = mtk_vcodec_mem_alloc(ctx, &mem);
+	if (result)
+		return NULL;
+
+	inst = mem.va;
+	inst->mem = mem;
+
+	return inst;
+}
+
+void vp9_free_handle(struct vdec_vp9_inst *inst)
+{
+	struct mtk_vcodec_mem mem;
+
+	mem = inst->mem;
+	if (mem.va)
+		mtk_vcodec_mem_free(inst->ctx, &mem);
+}
+
+bool vp9_init_proc(struct vdec_vp9_inst *inst,
+		   struct vdec_pic_info *pic_info)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	pic_info->pic_w = vsi->pic_w;
+	pic_info->pic_h = vsi->pic_h;
+	pic_info->buf_w = vsi->buf_w;
+	pic_info->buf_h = vsi->buf_h;
+
+	mtk_vcodec_debug(inst,
+			"(PicW,PicH,BufW,BufH) = (%d,%d,%d,%d) profile=%d",
+			pic_info->pic_w, pic_info->pic_h,
+			pic_info->buf_w, pic_info->buf_h, vsi->profile);
+
+	inst->frm_hdr.width = vsi->pic_w;
+	inst->frm_hdr.height = vsi->pic_h;
+	inst->work_buf.frmbuf_width = vsi->buf_w;
+	inst->work_buf.frmbuf_height = vsi->buf_h;
+
+	/* ----> HW limitation */
+	if ((inst->frm_hdr.width > 4096) ||
+		(inst->frm_hdr.height > 2304)) {
+		mtk_vcodec_err(inst, "Invalid w/h %d/%d",
+			inst->frm_hdr.width,
+			inst->frm_hdr.height);
+		return false;
+	}
+
+	if (vsi->profile > 0) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support profile(%d) > 0",
+			     vsi->profile);
+		return false;
+	}
+	if ((inst->work_buf.frmbuf_width > 4096) ||
+	    (inst->work_buf.frmbuf_height > 2304)) {
+		mtk_vcodec_err(inst, "vp9_dec DO NOT support (W,H) = (%d,%d)",
+			     inst->work_buf.frmbuf_width,
+			     inst->work_buf.frmbuf_height);
+		return false;
+	}
+	/* <---- HW limitation */
+
+	return true;
+}
+
+bool vp9_check_proc(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	bool ret = false;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vsi->show_exist) {
+		vp9_swap_frm_bufs(inst);
+		mtk_vcodec_debug(inst, "Decode Ok @%d (show_exist)",
+				 vsi->frm_num);
+		vsi->frm_num++;
+		return true;
+	}
+
+	ret = vp9_wait_dec_end(inst);
+	if (!ret) {
+		mtk_vcodec_err(inst, "Decode NG, Decode Timeout @[%d]",
+			       vsi->frm_num);
+		return false;
+	}
+
+	if (vpu_dec_end(&inst->vpu)) {
+		mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
+		return false;
+	}
+
+	vp9_swap_frm_bufs(inst);
+	mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
+		     inst->frm_hdr.width, inst->frm_hdr.height);
+
+	vsi->frm_num++;
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
+{
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+
+	if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
+		return true;
+
+	return false;
+}
+
+struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_disp_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_DISPLAY;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no disp fb");
+
+	return fb;
+}
+
+bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
+			    struct vdec_fb *fb)
+{
+	struct vdec_fb_node *node;
+
+	if (!fb)
+		return false;
+
+	node = list_first_entry_or_null(&inst->available_fb_node_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		node->fb = fb;
+		list_move_tail(&node->list, &inst->fb_use_list);
+	} else {
+		mtk_vcodec_debug(inst, "No free fb node");
+		return false;
+	}
+
+	mtk_vcodec_debug_leave(inst);
+
+	return true;
+}
+
+struct vdec_fb *vp9_rm_from_fb_free_list(struct vdec_vp9_inst
+		*inst)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+
+	mtk_vcodec_debug_leave(inst);
+
+	return fb;
+}
+
+bool vp9_fb_use_list_to_fb_free_list(struct vdec_vp9_inst *inst)
+{
+	struct vdec_fb_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
+		list_move_tail(&node->list, &inst->fb_free_list);
+
+	mtk_vcodec_debug_leave(inst);
+	return true;
+}
+
+void vp9_reset(struct vdec_vp9_inst *inst)
+{
+	vp9_fb_use_list_to_fb_free_list(inst);
+
+	vp9_free_all_sf_ref_fb(inst);
+	inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
+
+	if (vpu_dec_reset(&inst->vpu))
+		mtk_vcodec_debug(inst, "vp9_dec_vpu_reset failed");
+
+	if (vp9_setup_buf(inst))
+		mtk_vcodec_debug(inst, "vp9_setup_buf failed");
+}
+
+static void init_list(struct vdec_vp9_inst *inst)
+{
+	int i;
+
+	INIT_LIST_HEAD(&inst->available_fb_node_list);
+	INIT_LIST_HEAD(&inst->fb_use_list);
+	INIT_LIST_HEAD(&inst->fb_free_list);
+	INIT_LIST_HEAD(&inst->fb_disp_list);
+
+	for (i = 0; i < VP9_MAX_FRM_BUFF_NODE_NUM; i++) {
+		INIT_LIST_HEAD(&inst->dec_fb[i].list);
+		inst->dec_fb[i].fb = NULL;
+		list_add_tail(&inst->dec_fb[i].list,
+			      &inst->available_fb_node_list);
+	}
+}
+
+static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
+{
+
+	pic->y_bs_sz = inst->vsi->buf_sz_y_bs;
+	pic->c_bs_sz = inst->vsi->buf_sz_c_bs;
+	pic->y_len_sz = inst->vsi->buf_len_sz_y;
+	pic->c_len_sz = inst->vsi->buf_len_sz_c;
+
+	pic->pic_w = inst->frm_hdr.width;
+	pic->pic_h = inst->frm_hdr.height;
+	pic->buf_w = inst->work_buf.frmbuf_width;
+	pic->buf_h = inst->work_buf.frmbuf_height;
+
+	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
+		 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
+	mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
+		 pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
+}
+
+static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	mtk_vcodec_debug_enter(inst);
+
+	*out_fb = vp9_rm_from_fb_disp_list(inst);
+	if (*out_fb)
+		(*out_fb)->status |= FB_ST_DISPLAY;
+}
+
+static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
+{
+	struct vdec_fb_node *node;
+	struct vdec_fb *fb = NULL;
+
+	node = list_first_entry_or_null(&inst->fb_free_list,
+					struct vdec_fb_node, list);
+	if (node) {
+		list_move_tail(&node->list, &inst->available_fb_node_list);
+		fb = (struct vdec_fb *)node->fb;
+		fb->status |= FB_ST_FREE;
+		mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
+				 node->fb, fb->status);
+	} else {
+		fb = NULL;
+		mtk_vcodec_debug(inst, "[FB] there is no free fb");
+	}
+
+	*out_fb = fb;
+}
+
+static int vdec_vp9_deinit(unsigned long h_vdec)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct mtk_vcodec_mem *mem;
+	int ret = 0;
+
+	mtk_vcodec_debug_enter(inst);
+
+	if (vpu_dec_deinit(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vpu_dec_deinit");
+		ret = -EINVAL;
+	}
+
+	mem = &inst->work_buf.mv_buf;
+	if (mem->va)
+		mtk_vcodec_mem_free(inst->ctx, mem);
+
+	vp9_free_all_sf_ref_fb(inst);
+	vp9_free_handle(inst);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
+{
+	struct vdec_vp9_inst *inst;
+
+	inst = vp9_alloc_inst(ctx);
+	if (!inst)
+		return -ENOMEM;
+
+	inst->frm_cnt = 0;
+	inst->total_frm_cnt = 0;
+	inst->ctx = ctx;
+
+	inst->vpu.id = IPI_VDEC_VP9;
+	inst->vpu.dev = ctx->dev->vpu_plat_dev;
+	inst->vpu.ctx = ctx;
+	inst->vpu.handler = vpu_dec_ipi_handler;
+
+	if (vpu_dec_init(&inst->vpu)) {
+		mtk_vcodec_err(inst, "[E]vp9_dec_vpu_init - %d",
+						inst->vpu.inst_addr);
+		goto err_deinit_inst;
+	}
+
+	inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
+	init_list(inst);
+
+	(*h_vdec) = (unsigned long)inst;
+	return 0;
+
+err_deinit_inst:
+	vp9_free_handle(inst);
+
+	return -EINVAL;
+}
+
+static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
+		   struct vdec_fb *fb, bool *res_chg)
+{
+	int ret = 0;
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	struct vdec_vp9_frm_hdr *frm_hdr = &inst->frm_hdr;
+	struct vdec_vp9_vsi *vsi = inst->vsi;
+	unsigned int data[3];
+	int i;
+
+	mtk_vcodec_debug_enter(inst);
+
+	*res_chg = false;
+
+	if ((bs == NULL) && (fb == NULL)) {
+		mtk_vcodec_debug(inst, "[EOS]");
+		vp9_reset(inst);
+		return ret;
+	}
+
+	if (bs != NULL)
+		mtk_vcodec_debug(inst, "Input BS Size = %ld", bs->size);
+
+	memcpy((void *)inst + sizeof(*inst), (void *)inst,
+	       sizeof(*inst));
+
+	while (1) {
+		struct vdec_fb *cur_fb;
+
+		frm_hdr->resolution_changed = false;
+
+		data[0] = *((unsigned int *)bs->va);
+		data[1] = *((unsigned int *)(bs->va + 4));
+		data[2] = *((unsigned int *)(bs->va + 8));
+
+		vsi->bs = *bs;
+
+		if (fb)
+			vsi->fb = *fb;
+
+		if (!vsi->sf_init) {
+			unsigned int sf_bs_sz;
+			unsigned int sf_bs_off;
+			unsigned char *sf_bs_src;
+			unsigned char *sf_bs_dst;
+
+			sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
+				VP9_SUPER_FRAME_BS_SZ : bs->size;
+			sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
+			sf_bs_src = bs->va + bs->size - sf_bs_sz;
+			sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
+			memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
+		} else {
+			if ((vsi->sf_frm_cnt > 0) &&
+				(vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
+				unsigned int idx = vsi->sf_frm_idx;
+
+				memcpy((void *)vsi->input_ctx.v_frm_sa,
+					   (void *)(vsi->input_ctx.v_frm_sa +
+					   vsi->sf_frm_offset[idx]),
+					   vsi->sf_frm_sz[idx]);
+			}
+		}
+		ret = vpu_dec_start(&inst->vpu, data, 3);
+		if (ret) {
+			mtk_vcodec_err(inst, "vpu_dec_start failed");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		if (vsi->resolution_changed) {
+			if (!vp9_realloc_work_buf(inst)) {
+				ret = -EINVAL;
+				goto DECODE_ERROR;
+			}
+		}
+
+		if (vsi->sf_frm_cnt > 0) {
+			cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
+
+			if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
+				inst->cur_fb = cur_fb;
+			else
+				inst->cur_fb = fb;
+		} else {
+			inst->cur_fb = fb;
+		}
+
+		vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
+		if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
+			vp9_add_to_fb_use_list(inst, inst->cur_fb);
+
+		mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
+
+		/* the same as VP9_SKIP_FRAME */
+		inst->frm_hdr.show_frame = vsi->show_frm;
+
+		if (vsi->show_exist)
+			mtk_vcodec_debug(inst,
+				"drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+
+		if (vsi->show_exist && (vsi->frm_to_show <
+					VP9_MAX_FRM_BUFF_NUM)) {
+			mtk_vcodec_debug(inst,
+				"Skip Decode drv->new_fb_idx=%d, drv->frm_to_show=%d",
+				vsi->new_fb_idx, vsi->frm_to_show);
+			vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
+							vsi->frm_to_show);
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		/* VPU assign the buffer pointer in its address space,
+		  * reassign here
+		  */
+		for (i = 0; i < REFS_PER_FRAME; i++) {
+			unsigned int idx = vsi->frm_refs[i].idx;
+
+			vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
+		}
+
+		if (frm_hdr->resolution_changed) {
+			unsigned int width = inst->frm_hdr.width;
+			unsigned int height = inst->frm_hdr.height;
+			unsigned int frmbuf_width =
+				inst->work_buf.frmbuf_width;
+			unsigned int frmbuf_height =
+				inst->work_buf.frmbuf_height;
+			struct mtk_vcodec_mem tmp_buf =	inst->work_buf.mv_buf;
+			struct vp9_dram_buf tmp_buf2 = vsi->mv_buf;
+			struct vdec_fb tmp_buf3 =
+				inst->work_buf.sf_ref_buf[0];
+
+			memcpy((void *)inst, (void *)inst + sizeof(*inst),
+			       sizeof(*inst));
+
+			inst->frm_hdr.width = width;
+			inst->frm_hdr.height = height;
+			inst->work_buf.frmbuf_width = frmbuf_width;
+			inst->work_buf.frmbuf_height = frmbuf_height;
+			inst->work_buf.mv_buf = tmp_buf;
+			inst->vsi->mv_buf = tmp_buf2;
+			inst->work_buf.sf_ref_buf[0] = tmp_buf3;
+
+			*res_chg = true;
+			mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
+			vp9_add_to_fb_free_list(inst, fb);
+			ret = 0;
+			goto DECODE_ERROR;
+		}
+
+		if (vp9_check_proc(inst) != true) {
+			mtk_vcodec_err(inst, "vp9_check_proc");
+			ret = -EINVAL;
+			goto DECODE_ERROR;
+		}
+
+		inst->total_frm_cnt++;
+		if (vp9_is_last_sub_frm(inst))
+			break;
+
+		/* for resolution change backup */
+		memcpy((void *)inst + sizeof(*inst), (void *)inst,
+		       sizeof(*inst));
+	}
+	inst->frm_cnt++;
+
+	mtk_vcodec_debug_leave(inst);
+
+DECODE_ERROR:
+	if (ret < 0)
+		vp9_add_to_fb_free_list(inst, fb);
+
+	mtk_vcodec_debug_leave(inst);
+
+	return ret;
+}
+
+static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_crop *cr)
+{
+	cr->c.left = 0;
+	cr->c.top = 0;
+	cr->c.width = inst->frm_hdr.width;
+	cr->c.height = inst->frm_hdr.height;
+	mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
+			 cr->c.left, cr->c.top, cr->c.width, cr->c.height);
+}
+
+static int vdec_vp9_get_param(unsigned long h_vdec,
+			enum vdec_get_param_type type, void *out)
+{
+	struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
+	int ret = 0;
+
+	switch (type) {
+	case GET_PARAM_DISP_FRAME_BUFFER:
+		get_disp_fb(inst, out);
+		break;
+	case GET_PARAM_FREE_FRAME_BUFFER:
+		get_free_fb(inst, out);
+		break;
+	case GET_PARAM_PIC_INFO:
+		get_pic_info(inst, out);
+		break;
+	case GET_PARAM_DPB_SIZE:
+		*((unsigned int *)out) = 9;
+		break;
+	case GET_PARAM_CROP_INFO:
+		get_crop_info(inst, out);
+		break;
+	default:
+		mtk_vcodec_err(inst, "not support type %d", type);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+static struct vdec_common_if vdec_vp9_if = {
+	vdec_vp9_init,
+	vdec_vp9_decode,
+	vdec_vp9_get_param,
+	vdec_vp9_deinit,
+};
+
+struct vdec_common_if *get_vp9_dec_comm_if(void)
+{
+	return &vdec_vp9_if;
+}
diff --git a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
index 6e2f065..f5781d7 100644
--- a/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
+++ b/drivers/media/platform/mtk-vcodec/vdec_drv_if.c
@@ -26,6 +26,7 @@
 
 struct vdec_common_if *get_h264_dec_comm_if(void);
 struct vdec_common_if *get_vp8_dec_comm_if(void);
+struct vdec_common_if *get_vp9_dec_comm_if(void);
 
 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 {
@@ -39,6 +40,8 @@ int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
 		ctx->dec_if = get_vp8_dec_comm_if();
 		break;
 	case V4L2_PIX_FMT_VP9:
+		ctx->dec_if = get_vp9_dec_comm_if();
+		break;
 	default:
 		return -EINVAL;
 	}
-- 
1.7.9.5

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

* [PATCH 7/7] arm64: dts: mediatek: Add Video Encoder for MT8173
@ 2016-04-13 12:01               ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin,
	Tiffany.lin, Tiffany Lin

Add video decoder node for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 arch/arm64/boot/dts/mediatek/mt8173.dtsi |   38 ++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/arm64/boot/dts/mediatek/mt8173.dtsi b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
index 26aeffe..32b555e 100644
--- a/arch/arm64/boot/dts/mediatek/mt8173.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
@@ -1124,6 +1124,44 @@
 			#clock-cells = <1>;
 		};
 
+		vcodec_dec: vcodec@16000000 {
+			compatible = "mediatek,mt8173-vcodec-dec";
+			reg = <0 0x16000000 0 0x100>,	/* VDEC_SYS */
+			      <0 0x16020000 0 0x1000>,	/* VDEC_MISC */
+			      <0 0x16021000 0 0x800>,	/* VDEC_LD */
+			      <0 0x16021800 0 0x800>,	/* VDEC_TOP */
+			      <0 0x16022000 0 0x1000>,	/* VDEC_CM */
+			      <0 0x16023000 0 0x1000>,	/* VDEC_AD */
+			      <0 0x16024000 0 0x1000>,	/* VDEC_AV */
+			      <0 0x16025000 0 0x1000>,	/* VDEC_PP */
+			      <0 0x16026800 0 0x800>,	/* VDEC_HWD */
+			      <0 0x16027000 0 0x800>,	/* VDEC_HWQ */
+			      <0 0x16027800 0 0x800>,	/* VDEC_HWB */
+			      <0 0x16028400 0 0x400>;	/* VDEC_HWG */
+			interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+			mediatek,larb = <&larb1>;
+			iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+			mediatek,vpu = <&vpu>;
+			power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+			clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+				 <&topckgen CLK_TOP_UNIVPLL_D2>,
+				 <&topckgen CLK_TOP_CCI400_SEL>,
+				 <&topckgen CLK_TOP_VDEC_SEL>,
+				 <&topckgen CLK_TOP_VCODECPLL>;
+			clock-names = "vcodecpll",
+				      "univpll_d2",
+				      "clk_cci400_sel",
+				      "vdec_sel",
+				      "vdecpll";
+		};
+
 		larb1: larb@16010000 {
 			compatible = "mediatek,mt8173-smi-larb";
 			reg = <0 0x16010000 0 0x1000>;
-- 
1.7.9.5

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

* [PATCH 7/7] arm64: dts: mediatek: Add Video Encoder for MT8173
@ 2016-04-13 12:01               ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w,
	Tiffany.lin-NuS5LvNUpcJWk0Htik3J/w, Tiffany Lin

Add video decoder node for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 arch/arm64/boot/dts/mediatek/mt8173.dtsi |   38 ++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/arm64/boot/dts/mediatek/mt8173.dtsi b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
index 26aeffe..32b555e 100644
--- a/arch/arm64/boot/dts/mediatek/mt8173.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
@@ -1124,6 +1124,44 @@
 			#clock-cells = <1>;
 		};
 
+		vcodec_dec: vcodec@16000000 {
+			compatible = "mediatek,mt8173-vcodec-dec";
+			reg = <0 0x16000000 0 0x100>,	/* VDEC_SYS */
+			      <0 0x16020000 0 0x1000>,	/* VDEC_MISC */
+			      <0 0x16021000 0 0x800>,	/* VDEC_LD */
+			      <0 0x16021800 0 0x800>,	/* VDEC_TOP */
+			      <0 0x16022000 0 0x1000>,	/* VDEC_CM */
+			      <0 0x16023000 0 0x1000>,	/* VDEC_AD */
+			      <0 0x16024000 0 0x1000>,	/* VDEC_AV */
+			      <0 0x16025000 0 0x1000>,	/* VDEC_PP */
+			      <0 0x16026800 0 0x800>,	/* VDEC_HWD */
+			      <0 0x16027000 0 0x800>,	/* VDEC_HWQ */
+			      <0 0x16027800 0 0x800>,	/* VDEC_HWB */
+			      <0 0x16028400 0 0x400>;	/* VDEC_HWG */
+			interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+			mediatek,larb = <&larb1>;
+			iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+			mediatek,vpu = <&vpu>;
+			power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+			clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+				 <&topckgen CLK_TOP_UNIVPLL_D2>,
+				 <&topckgen CLK_TOP_CCI400_SEL>,
+				 <&topckgen CLK_TOP_VDEC_SEL>,
+				 <&topckgen CLK_TOP_VCODECPLL>;
+			clock-names = "vcodecpll",
+				      "univpll_d2",
+				      "clk_cci400_sel",
+				      "vdec_sel",
+				      "vdecpll";
+		};
+
 		larb1: larb@16010000 {
 			compatible = "mediatek,mt8173-smi-larb";
 			reg = <0 0x16010000 0 0x1000>;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 7/7] arm64: dts: mediatek: Add Video Encoder for MT8173
@ 2016-04-13 12:01               ` Tiffany Lin
  0 siblings, 0 replies; 52+ messages in thread
From: Tiffany Lin @ 2016-04-13 12:01 UTC (permalink / raw)
  To: linux-arm-kernel

Add video decoder node for MT8173

Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
---
 arch/arm64/boot/dts/mediatek/mt8173.dtsi |   38 ++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/arm64/boot/dts/mediatek/mt8173.dtsi b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
index 26aeffe..32b555e 100644
--- a/arch/arm64/boot/dts/mediatek/mt8173.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
@@ -1124,6 +1124,44 @@
 			#clock-cells = <1>;
 		};
 
+		vcodec_dec: vcodec at 16000000 {
+			compatible = "mediatek,mt8173-vcodec-dec";
+			reg = <0 0x16000000 0 0x100>,	/* VDEC_SYS */
+			      <0 0x16020000 0 0x1000>,	/* VDEC_MISC */
+			      <0 0x16021000 0 0x800>,	/* VDEC_LD */
+			      <0 0x16021800 0 0x800>,	/* VDEC_TOP */
+			      <0 0x16022000 0 0x1000>,	/* VDEC_CM */
+			      <0 0x16023000 0 0x1000>,	/* VDEC_AD */
+			      <0 0x16024000 0 0x1000>,	/* VDEC_AV */
+			      <0 0x16025000 0 0x1000>,	/* VDEC_PP */
+			      <0 0x16026800 0 0x800>,	/* VDEC_HWD */
+			      <0 0x16027000 0 0x800>,	/* VDEC_HWQ */
+			      <0 0x16027800 0 0x800>,	/* VDEC_HWB */
+			      <0 0x16028400 0 0x400>;	/* VDEC_HWG */
+			interrupts = <GIC_SPI 204 IRQ_TYPE_LEVEL_LOW>;
+			mediatek,larb = <&larb1>;
+			iommus = <&iommu M4U_PORT_HW_VDEC_MC_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PP_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_AVC_MV_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_RD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_PRED_WR_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_UFO_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD_EXT>,
+				 <&iommu M4U_PORT_HW_VDEC_VLD2_EXT>;
+			mediatek,vpu = <&vpu>;
+			power-domains = <&scpsys MT8173_POWER_DOMAIN_VDEC>;
+			clocks = <&apmixedsys CLK_APMIXED_VCODECPLL>,
+				 <&topckgen CLK_TOP_UNIVPLL_D2>,
+				 <&topckgen CLK_TOP_CCI400_SEL>,
+				 <&topckgen CLK_TOP_VDEC_SEL>,
+				 <&topckgen CLK_TOP_VCODECPLL>;
+			clock-names = "vcodecpll",
+				      "univpll_d2",
+				      "clk_cci400_sel",
+				      "vdec_sel",
+				      "vdecpll";
+		};
+
 		larb1: larb at 16010000 {
 			compatible = "mediatek,mt8173-smi-larb";
 			reg = <0 0x16010000 0 0x1000>;
-- 
1.7.9.5

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

* Re: [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
  2016-04-13 12:01   ` Tiffany Lin
@ 2016-04-13 14:23     ` Nicolas Dufresne
  -1 siblings, 0 replies; 52+ messages in thread
From: Nicolas Dufresne @ 2016-04-13 14:23 UTC (permalink / raw)
  To: Tiffany Lin, Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin

Le mercredi 13 avril 2016 à 20:01 +0800, Tiffany Lin a écrit :
> From: Daniel Kurtz <djkurtz@chromium.org>
> 
> Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.
> 
> Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
> V4L2_PIX_FMT_NV12 notation.
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
>  include/uapi/drm/drm_fourcc.h  |    1 +
>  include/uapi/linux/videodev2.h |    2 ++

Might be better to split this patch.

>  2 files changed, 3 insertions(+)
> 
> diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
> index 0b69a77..a193905 100644
> --- a/include/uapi/drm/drm_fourcc.h
> +++ b/include/uapi/drm/drm_fourcc.h
> @@ -116,6 +116,7 @@
>  #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
>  #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
>  
> +#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */

Please document the tiling format, don't just add a define here.

>  /*
>   * 3 plane YCbCr
>   * index 0: Y plane, [7:0] Y
> diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> index d0acd26..e9e3276 100644
> --- a/include/uapi/linux/videodev2.h
> +++ b/include/uapi/linux/videodev2.h
> @@ -527,6 +527,8 @@ struct v4l2_pix_format {
>  #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
>  #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
>  
> +#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
> +

Same. On Linux Media side, there is docbook documentation where you can
explain in detail.

>  /* two planes -- one Y, one Cr + Cb interleaved  */
>  #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
>  #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */

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

* [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-13 14:23     ` Nicolas Dufresne
  0 siblings, 0 replies; 52+ messages in thread
From: Nicolas Dufresne @ 2016-04-13 14:23 UTC (permalink / raw)
  To: linux-arm-kernel

Le mercredi 13 avril 2016 ? 20:01 +0800, Tiffany Lin a ?crit?:
> From: Daniel Kurtz <djkurtz@chromium.org>
> 
> Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.
> 
> Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
> V4L2_PIX_FMT_NV12 notation.
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
> ?include/uapi/drm/drm_fourcc.h??|????1 +
> ?include/uapi/linux/videodev2.h |????2 ++

Might be better to split this patch.

> ?2 files changed, 3 insertions(+)
> 
> diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
> index 0b69a77..a193905 100644
> --- a/include/uapi/drm/drm_fourcc.h
> +++ b/include/uapi/drm/drm_fourcc.h
> @@ -116,6 +116,7 @@
> ?#define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
> ?#define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
> ?
> +#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */

Please document the tiling format, don't just add a define here.

> ?/*
> ? * 3 plane YCbCr
> ? * index 0: Y plane, [7:0] Y
> diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> index d0acd26..e9e3276 100644
> --- a/include/uapi/linux/videodev2.h
> +++ b/include/uapi/linux/videodev2.h
> @@ -527,6 +527,8 @@ struct v4l2_pix_format {
> ?#define V4L2_PIX_FMT_HM12????v4l2_fourcc('H', 'M', '1', '2') /*??8??YUV 4:2:0 16x16 macroblocks */
> ?#define V4L2_PIX_FMT_M420????v4l2_fourcc('M', '4', '2', '0') /* 12??YUV 4:2:0 2 lines y, 1 line uv interleaved */
> ?
> +#define V4L2_PIX_FMT_MT21????v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode??*/
> +

Same. On Linux Media side, there is docbook documentation where you can
explain in detail.

> ?/* two planes -- one Y, one Cr + Cb interleaved??*/
> ?#define V4L2_PIX_FMT_NV12????v4l2_fourcc('N', 'V', '1', '2') /* 12??Y/CbCr 4:2:0??*/
> ?#define V4L2_PIX_FMT_NV21????v4l2_fourcc('N', 'V', '2', '1') /* 12??Y/CrCb 4:2:0??*/

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

* Re: [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-14  6:13       ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-14  6:13 UTC (permalink / raw)
  To: nicolas
  Cc: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Hi 
On Wed, 2016-04-13 at 10:23 -0400, Nicolas Dufresne wrote:
> Le mercredi 13 avril 2016 à 20:01 +0800, Tiffany Lin a écrit :
> > From: Daniel Kurtz <djkurtz@chromium.org>
> > 
> > Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.
> > 
> > Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
> > V4L2_PIX_FMT_NV12 notation.
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> > ---
> >  include/uapi/drm/drm_fourcc.h  |    1 +
> >  include/uapi/linux/videodev2.h |    2 ++
> 
> Might be better to split this patch.
Got it, will split to two patch in next version.

> 
> >  2 files changed, 3 insertions(+)
> > 
> > diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
> > index 0b69a77..a193905 100644
> > --- a/include/uapi/drm/drm_fourcc.h
> > +++ b/include/uapi/drm/drm_fourcc.h
> > @@ -116,6 +116,7 @@
> >  #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
> >  #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
> >  
> > +#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
> 
> Please document the tiling format, don't just add a define here.
Got it, will add documentation in next version.

> 
> >  /*
> >   * 3 plane YCbCr
> >   * index 0: Y plane, [7:0] Y
> > diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> > index d0acd26..e9e3276 100644
> > --- a/include/uapi/linux/videodev2.h
> > +++ b/include/uapi/linux/videodev2.h
> > @@ -527,6 +527,8 @@ struct v4l2_pix_format {
> >  #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
> >  #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
> >  
> > +#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
> > +
> 
> Same. On Linux Media side, there is docbook documentation where you can
> explain in detail.
Got it, will add documentation in next version.

> 
> >  /* two planes -- one Y, one Cr + Cb interleaved  */
> >  #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
> >  #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */

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

* Re: [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-14  6:13       ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-14  6:13 UTC (permalink / raw)
  To: nicolas-dDhyB4GVkw9AFePFGvp55w
  Cc: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w

Hi 
On Wed, 2016-04-13 at 10:23 -0400, Nicolas Dufresne wrote:
> Le mercredi 13 avril 2016 à 20:01 +0800, Tiffany Lin a écrit :
> > From: Daniel Kurtz <djkurtz-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
> > 
> > Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.
> > 
> > Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
> > V4L2_PIX_FMT_NV12 notation.
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> > ---
> >  include/uapi/drm/drm_fourcc.h  |    1 +
> >  include/uapi/linux/videodev2.h |    2 ++
> 
> Might be better to split this patch.
Got it, will split to two patch in next version.

> 
> >  2 files changed, 3 insertions(+)
> > 
> > diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
> > index 0b69a77..a193905 100644
> > --- a/include/uapi/drm/drm_fourcc.h
> > +++ b/include/uapi/drm/drm_fourcc.h
> > @@ -116,6 +116,7 @@
> >  #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
> >  #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
> >  
> > +#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
> 
> Please document the tiling format, don't just add a define here.
Got it, will add documentation in next version.

> 
> >  /*
> >   * 3 plane YCbCr
> >   * index 0: Y plane, [7:0] Y
> > diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> > index d0acd26..e9e3276 100644
> > --- a/include/uapi/linux/videodev2.h
> > +++ b/include/uapi/linux/videodev2.h
> > @@ -527,6 +527,8 @@ struct v4l2_pix_format {
> >  #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
> >  #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
> >  
> > +#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
> > +
> 
> Same. On Linux Media side, there is docbook documentation where you can
> explain in detail.
Got it, will add documentation in next version.

> 
> >  /* two planes -- one Y, one Cr + Cb interleaved  */
> >  #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
> >  #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */


--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format
@ 2016-04-14  6:13       ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-14  6:13 UTC (permalink / raw)
  To: linux-arm-kernel

Hi 
On Wed, 2016-04-13 at 10:23 -0400, Nicolas Dufresne wrote:
> Le mercredi 13 avril 2016 ? 20:01 +0800, Tiffany Lin a ?crit :
> > From: Daniel Kurtz <djkurtz@chromium.org>
> > 
> > Mediatek video format is YVU8_420_2PLANE_PACK8_PROGRESSIVE.
> > 
> > Create V4L2_PIX_FMT_MT21 and DRM_FORMAT_MT21 to be consistent with
> > V4L2_PIX_FMT_NV12 notation.
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> > ---
> >  include/uapi/drm/drm_fourcc.h  |    1 +
> >  include/uapi/linux/videodev2.h |    2 ++
> 
> Might be better to split this patch.
Got it, will split to two patch in next version.

> 
> >  2 files changed, 3 insertions(+)
> > 
> > diff --git a/include/uapi/drm/drm_fourcc.h b/include/uapi/drm/drm_fourcc.h
> > index 0b69a77..a193905 100644
> > --- a/include/uapi/drm/drm_fourcc.h
> > +++ b/include/uapi/drm/drm_fourcc.h
> > @@ -116,6 +116,7 @@
> >  #define DRM_FORMAT_NV24		fourcc_code('N', 'V', '2', '4') /* non-subsampled Cr:Cb plane */
> >  #define DRM_FORMAT_NV42		fourcc_code('N', 'V', '4', '2') /* non-subsampled Cb:Cr plane */
> >  
> > +#define DRM_FORMAT_MT21		fourcc_code('M', 'T', '2', '1') /* Mediatek Block Mode */
> 
> Please document the tiling format, don't just add a define here.
Got it, will add documentation in next version.

> 
> >  /*
> >   * 3 plane YCbCr
> >   * index 0: Y plane, [7:0] Y
> > diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> > index d0acd26..e9e3276 100644
> > --- a/include/uapi/linux/videodev2.h
> > +++ b/include/uapi/linux/videodev2.h
> > @@ -527,6 +527,8 @@ struct v4l2_pix_format {
> >  #define V4L2_PIX_FMT_HM12    v4l2_fourcc('H', 'M', '1', '2') /*  8  YUV 4:2:0 16x16 macroblocks */
> >  #define V4L2_PIX_FMT_M420    v4l2_fourcc('M', '4', '2', '0') /* 12  YUV 4:2:0 2 lines y, 1 line uv interleaved */
> >  
> > +#define V4L2_PIX_FMT_MT21    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek Block Mode  */
> > +
> 
> Same. On Linux Media side, there is docbook documentation where you can
> explain in detail.
Got it, will add documentation in next version.

> 
> >  /* two planes -- one Y, one Cr + Cb interleaved  */
> >  #define V4L2_PIX_FMT_NV12    v4l2_fourcc('N', 'V', '1', '2') /* 12  Y/CbCr 4:2:0  */
> >  #define V4L2_PIX_FMT_NV21    v4l2_fourcc('N', 'V', '2', '1') /* 12  Y/CrCb 4:2:0  */

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

* Re: [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
@ 2016-04-14 16:15       ` Rob Herring
  0 siblings, 0 replies; 52+ messages in thread
From: Rob Herring @ 2016-04-14 16:15 UTC (permalink / raw)
  To: Tiffany Lin
  Cc: Hans Verkuil, daniel.thompson, Mauro Carvalho Chehab,
	Matthias Brugger, Daniel Kurtz, Pawel Osciak, Eddie Huang,
	Yingjoe Chen, devicetree, linux-kernel, linux-arm-kernel,
	linux-media, linux-mediatek, PoChun.Lin

On Wed, Apr 13, 2016 at 08:01:50PM +0800, Tiffany Lin wrote:
> Add a DT binding documentation of Video Decoder for the
> MT8173 SoC from Mediatek.
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
>  .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
>  1 file changed, 46 insertions(+), 4 deletions(-)

Acked-by: Rob Herring <rob@kernel.org>

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

* Re: [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
@ 2016-04-14 16:15       ` Rob Herring
  0 siblings, 0 replies; 52+ messages in thread
From: Rob Herring @ 2016-04-14 16:15 UTC (permalink / raw)
  To: Tiffany Lin
  Cc: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w

On Wed, Apr 13, 2016 at 08:01:50PM +0800, Tiffany Lin wrote:
> Add a DT binding documentation of Video Decoder for the
> MT8173 SoC from Mediatek.
> 
> Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> ---
>  .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
>  1 file changed, 46 insertions(+), 4 deletions(-)

Acked-by: Rob Herring <rob-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder
@ 2016-04-14 16:15       ` Rob Herring
  0 siblings, 0 replies; 52+ messages in thread
From: Rob Herring @ 2016-04-14 16:15 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Apr 13, 2016 at 08:01:50PM +0800, Tiffany Lin wrote:
> Add a DT binding documentation of Video Decoder for the
> MT8173 SoC from Mediatek.
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
>  .../devicetree/bindings/media/mediatek-vcodec.txt  |   50 ++++++++++++++++++--
>  1 file changed, 46 insertions(+), 4 deletions(-)

Acked-by: Rob Herring <rob@kernel.org>

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-15 14:27         ` Hans Verkuil
  0 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-15 14:27 UTC (permalink / raw)
  To: Tiffany Lin, Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree, linux-kernel,
	linux-arm-kernel, linux-media, linux-mediatek, PoChun.Lin

On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> Add v4l2 layer decoder driver for MT8173
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
>  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
>  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
>  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
>  11 files changed, 2596 insertions(+), 20 deletions(-)
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> 
> diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> index dc5cb00..4c8ed2f 100644
> --- a/drivers/media/platform/mtk-vcodec/Makefile
> +++ b/drivers/media/platform/mtk-vcodec/Makefile
> @@ -1,7 +1,13 @@
>  
>  
> -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> -
> +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> +				       mtk-vcodec-enc.o \
> +				       mtk-vcodec-common.o
> +
> +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> +		vdec_drv_if.o \
> +		mtk_vcodec_dec.o \
> +		mtk_vcodec_dec_pm.o \
>  
>  
>  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> new file mode 100644
> index 0000000..0499413
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> @@ -0,0 +1,1429 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vcodec_dec_pm.h"
> +
> +static struct mtk_video_fmt mtk_video_formats[] = {
> +	{
> +		.fourcc = V4L2_PIX_FMT_H264,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP8,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_MT21,
> +		.type = MTK_FMT_FRAME,
> +		.num_planes = 2,
> +	},
> +};
> +#define OUT_FMT_IDX	0
> +#define CAP_FMT_IDX	3
> +
> +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> +
> +#define MTK_VDEC_MIN_W	64U
> +#define MTK_VDEC_MIN_H	64U
> +#define MTK_VDEC_MAX_W	2048U
> +#define MTK_VDEC_MAX_H	1088U
> +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> +
> +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> +	{
> +		.fourcc	= V4L2_PIX_FMT_H264,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc	= V4L2_PIX_FMT_VP8,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +};
> +
> +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> +
> +
> +const struct vb2_ops mtk_vdec_vb2_ops;
> +
> +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +	unsigned int k;
> +
> +	for (k = 0; k < NUM_FORMATS; k++) {
> +		fmt = &mtk_video_formats[k];
> +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> +			return fmt;
> +	}
> +
> +	return NULL;
> +}
> +
> +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> +					      enum v4l2_buf_type type)
> +{
> +	if (V4L2_TYPE_IS_OUTPUT(type))
> +		return &ctx->q_data[MTK_Q_DATA_SRC];
> +
> +	return &ctx->q_data[MTK_Q_DATA_DST];
> +}
> +
> +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vdec_fb *disp_frame_buffer;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_DISP_FRAME_BUFFER,
> +				   &disp_frame_buffer)) {
> +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> +			     ctx->idx);
> +		return NULL;
> +	}
> +
> +	if (disp_frame_buffer == NULL) {
> +		mtk_v4l2_debug(3, "No display frame buffer");
> +		return NULL;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = disp_frame_buffer;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> +							ctx->picinfo.y_bs_sz);
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> +							ctx->picinfo.c_bs_sz);
> +
> +		dstbuf->ready_to_display = true;
> +		dstbuf->nonrealdisplay = false;
> +		mtk_v4l2_debug(2,
> +			       "[%d]status=%x queue idx=%d to done_list %d",
> +			       ctx->idx, frame_buffer->status,
> +			       dstbuf->vb.vb2_buf.index,
> +			       dstbuf->queued_in_vb2);
> +
> +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> +		ctx->decoded_frame_cnt++;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	void *tmp_frame_addr;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_FREE_FRAME_BUFFER,
> +				   &tmp_frame_addr)) {
> +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> +		return NULL;
> +	}
> +	if (tmp_frame_addr == NULL) {
> +		mtk_v4l2_debug(3, " No free frame buffer");
> +		return NULL;
> +	}
> +
> +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> +			 ctx->idx, tmp_frame_addr);
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = tmp_frame_addr;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		if ((dstbuf->queued_in_vb2) &&
> +		    (dstbuf->queued_in_v4l2) &&
> +		    (frame_buffer->status == FB_ST_FREE)) {
> +			mtk_v4l2_debug(2,
> +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> +				 ctx->idx, frame_buffer->status,
> +				 dstbuf->vb.vb2_buf.index,
> +				 dstbuf->queued_in_vb2);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +		} else if ((dstbuf->queued_in_vb2 == false) &&
> +			   (dstbuf->queued_in_v4l2 == true)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]status=%x queue idx=%d to rdy_queue",
> +					 ctx->idx, frame_buffer->status,
> +					 dstbuf->vb.vb2_buf.index);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +			dstbuf->queued_in_vb2 = true;
> +		} else {
> +			mtk_v4l2_debug(2,
> +					"[%d]status=%x err queue idx=%d %d %d",
> +					ctx->idx, frame_buffer->status,
> +					dstbuf->vb.vb2_buf.index,
> +					dstbuf->queued_in_vb2,
> +					dstbuf->queued_in_v4l2);
> +		}
> +		dstbuf->used = false;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_display_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_free_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> +{
> +	static const struct v4l2_event ev_src_ch = {
> +		.type = V4L2_EVENT_SOURCE_CHANGE,
> +		.u.src_change.changes =
> +		V4L2_EVENT_SRC_CH_RESOLUTION,
> +	};
> +
> +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> +
> +	return 0;
> +}
> +
> +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> +{
> +	bool res_chg;
> +	int ret = 0;
> +
> +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> +	if (ret)
> +		mtk_v4l2_err("DecodeFinal failed");
> +
> +	clean_display_buffer(ctx);
> +	clean_free_buffer(ctx);
> +}
> +
> +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> +{
> +	int dpbsize;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_PIC_INFO,
> +				   &ctx->last_decoded_picinfo)) {
> +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +				ctx->idx);
> +		return false;
> +	}
> +
> +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> +		ctx->last_decoded_picinfo.pic_h == 0 ||
> +		ctx->last_decoded_picinfo.buf_w == 0 ||
> +		ctx->last_decoded_picinfo.buf_h == 0)
> +		return false;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> +				 ctx->last_decoded_picinfo.pic_h,
> +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				 ctx->last_decoded_picinfo.buf_w,
> +				 ctx->last_decoded_picinfo.buf_h);
> +
> +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +		if (dpbsize == 0)
> +			dpbsize = 1;
> +		ctx->dpb_count = dpbsize;
> +		return true;
> +	}
> +
> +	return false;
> +}
> +
> +void mtk_vdec_worker(struct work_struct *work)
> +{
> +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> +				    decode_work);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	struct vb2_buffer *src_buf, *dst_buf;
> +	struct mtk_vcodec_mem buf;
> +	struct vdec_fb *pfb;
> +	bool res_chg = false;
> +	int ret;
> +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> +
> +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +	if (src_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> +	if (dst_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	buf.va = vb2_plane_vaddr(src_buf, 0);
> +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> +	if (!buf.va) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> +			       ctx->idx, src_buf->index);
> +		return;
> +	}
> +
> +	pfb = &dst_buf_info->frame_buffer;
> +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> +
> +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> +	pfb->status = 0;
> +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> +	mtk_v4l2_debug(3,
> +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> +			 ctx->idx, buf.va,
> +			 (u64)buf.dma_addr,
> +			 buf.size, src_buf);
> +
> +	mtk_v4l2_debug(3,
> +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> +			dst_buf->index, pfb,
> +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> +
> +	if (src_buf_info->lastframe == true) {
> +		/* update src buf status */
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		src_buf_info->lastframe = false;
> +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> +
> +		/* update dst buf status */
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		dst_buf_info->used = false;
> +
> +		clean_display_buffer(ctx);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> +		clean_free_buffer(ctx);
> +	} else {
> +
> +		dst_buf_info->vb.timestamp
> +					= src_buf_info->vb.timestamp;
> +		dst_buf_info->vb.timecode
> +					= src_buf_info->vb.timecode;
> +		mutex_lock(&dst_buf_info->lock);
> +		dst_buf_info->used = true;
> +		mutex_unlock(&dst_buf_info->lock);
> +		src_buf_info->used = true;
> +
> +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> +
> +		if (ret) {
> +			mtk_v4l2_err(
> +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> +				ctx->idx,
> +				src_buf->index,
> +				src_buf_info->lastframe,
> +				buf.size,
> +				src_buf_info->vb.timestamp.tv_sec,
> +				src_buf_info->vb.timestamp.tv_usec,
> +				dst_buf->index,
> +				ret, res_chg);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +					VB2_BUF_STATE_ERROR);
> +		} else if (res_chg == false) {
> +			/* we only return src buffer with VB2_BUF_STATE_DONE
> +			  * when decode success without resolution change
> +			  */
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +							VB2_BUF_STATE_DONE);
> +		}
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		clean_display_buffer(ctx);
> +		clean_free_buffer(ctx);
> +
> +		if ((ret == 0) && (res_chg == true)) {
> +			mtk_vdec_pic_info_update(ctx);
> +			/*
> +			  * On encountering a resolution change in the stream.
> +			  * The driver must first process and decode all
> +			  * remaining buffers from before the resolution change
> +			  * point, so call flush decode here
> +			  */
> +			mtk_vdec_flush_decoder(ctx);
> +			/*
> +			  * After all buffers containing decoded frames from
> +			  * before the resolution change point ready to be
> +			  * dequeued on the CAPTURE queue, the driver sends a
> +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> +			  */
> +			mtk_vdec_queue_res_chg_event(ctx);
> +		}
> +	}
> +
> +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +
> +}
> +
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_unlock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_lock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	int ret = 0;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	src_vq->drv_priv	= ctx;
> +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	src_vq->ops		= &mtk_vdec_vb2_ops;
> +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	src_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(src_vq);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> +		return ret;
> +	}
> +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	dst_vq->drv_priv	= ctx;
> +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	dst_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(dst_vq);
> +	if (ret) {
> +		vb2_queue_release(src_vq);
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> +	}
> +
> +	return ret;
> +}
> +
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> +{
> +	vdec_if_deinit(ctx);
> +	ctx->state = MTK_STATE_FREE;
> +}
> +
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct mtk_q_data *q_data;
> +
> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> +
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->coded_width = DFT_CFG_WIDTH;
> +	q_data->coded_height = DFT_CFG_HEIGHT;
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +
> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> +
> +	v4l_bound_align_image(&q_data->coded_width,
> +					MTK_VDEC_MIN_W,
> +					MTK_VDEC_MAX_W, 4,
> +					&q_data->coded_height,
> +					MTK_VDEC_MIN_H,
> +					MTK_VDEC_MAX_H, 5, 6);
> +
> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> +	q_data->bytesperline[0] = q_data->coded_width;
> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> +	q_data->bytesperline[1] = q_data->coded_width;
> +
> +}
> +
> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> +				enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +
> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> +				 enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> +			       struct v4l2_requestbuffers *reqbufs)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	int ret;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> +			 reqbufs->type, reqbufs->count);
> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> +
> +	return ret;
> +}

Please use the v4l2_m2m_ioctl_* helper functions were applicable.

> +
> +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> +				struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> +			 buf->type, buf->index);
> +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> +			    struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct vb2_queue *vq;
> +	struct vb2_buffer *vb;
> +	struct mtk_video_buf *mtkbuf;
> +	struct vb2_v4l2_buffer	*vb2_v4l2;
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> +	vb = vq->bufs[buf->index];
> +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    (buf->m.planes[0].bytesused == 0)) {
> +		mtkbuf->lastframe = true;
> +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> +			 ctx->idx, buf->type, buf->index,
> +			 mtkbuf->lastframe, buf->bytesused,
> +			 buf->m.planes[0].bytesused, buf->length,
> +			 vb);
> +	}
> +
> +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> +			     struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> +			      struct v4l2_exportbuffer *eb)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> +}
> +
> +static int vidioc_vdec_querycap(struct file *file, void *priv,
> +				struct v4l2_capability *cap)
> +{
> +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

See my comment about the new device_caps field in struct video_device from my
review of the encoder driver.

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_event_unsubscribe(fh, sub);
> +	default:
> +		return -EINVAL;
> +	}
> +}

No need for this function, just use v4l2_event_unsubscribe.

> +
> +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_src_change_event_subscribe(fh, sub);
> +	default:
> +		return -EINVAL;

Can't you just call v4l2_ctrl_subscribe_event() in the default case?

> +	}
> +}
> +
> +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> +		mtk_v4l2_err("sizeimage of output format must be given");
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> +			 struct v4l2_crop *cr)

Don't use g_crop, use g_selection instead. The selection API is a superset
of the old crop API.

> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state < MTK_STATE_HEADER)
> +		return -EINVAL;
> +
> +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> +
> +		if (vdec_if_get_param(ctx,
> +					   GET_PARAM_CROP_INFO,
> +					   cr)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> +					 ctx->idx);
> +			cr->c.left = 0;
> +			cr->c.top = 0;
> +			cr->c.width = ctx->picinfo.buf_w;
> +			cr->c.height = ctx->picinfo.buf_h;
> +		}
> +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> +				 cr->c.left, cr->c.top, cr->c.width,
> +				 cr->c.height);
> +	} else {
> +		cr->c.left = 0;
> +		cr->c.top = 0;
> +		cr->c.width = ctx->picinfo.pic_w;
> +		cr->c.height = ctx->picinfo.pic_h;
> +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> +				 ctx->picinfo.buf_h);
> +	}
> +	return 0;
> +}
> +
> +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +	int ret;
> +	struct mtk_video_fmt *fmt;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (fmt == NULL) {
> +		mtk_v4l2_err("mtk_venc_find_format fail");
> +		return ret;
> +	}
> +
> +	pix_mp = &f->fmt.pix_mp;
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> +		mtk_v4l2_err("out_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq)
> +		return -EINVAL;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +	if (!q_data)
> +		return -EINVAL;
> +
> +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		q_data->fmt = fmt;
> +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> +		pix_mp->plane_fmt[0].bytesperline = 0;
> +		pix_mp->width = 0;
> +		pix_mp->height = 0;
> +
> +		if (ctx->state == MTK_STATE_FREE) {
> +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> +			if (ret) {
> +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> +					       ctx->idx, ret);
> +				return -EINVAL;
> +			}
> +			ctx->state = MTK_STATE_INIT;
> +		}
> +	} else {
> +		q_data->fmt = fmt;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_enum_framesizes(struct file *file, void *priv,
> +				struct v4l2_frmsizeenum *fsize)
> +{
> +	int i = 0;
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (fsize->index != 0)
> +		return -EINVAL;
> +
> +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> +			continue;
> +
> +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> +		if (!(ctx->dev->dec_capability &
> +				VCODEC_CAPABILITY_4K_DISABLED)) {
> +			mtk_v4l2_debug(1, "4K is enabled");
> +			fsize->stepwise.max_width =
> +					VCODEC_DEC_4K_CODED_WIDTH;
> +			fsize->stepwise.max_height =
> +					VCODEC_DEC_4K_CODED_HEIGHT;
> +		}
> +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> +					ctx->dev->dec_capability,
> +					fsize->stepwise.min_width,
> +					fsize->stepwise.max_width,
> +					fsize->stepwise.step_width,
> +					fsize->stepwise.min_height,
> +					fsize->stepwise.max_height,
> +					fsize->stepwise.step_height);
> +		return 0;
> +	}
> +
> +	return -EINVAL;
> +
> +}
> +
> +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> +{
> +	struct mtk_video_fmt *fmt;
> +	int i, j = 0;
> +
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> +			continue;
> +		if (!output_queue &&
> +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> +			continue;
> +
> +		if (j == f->index)
> +			break;
> +		++j;
> +	}
> +
> +	if (i == NUM_FORMATS)
> +		return -EINVAL;
> +
> +	fmt = &mtk_video_formats[i];
> +	f->pixelformat = fmt->fourcc;
> +	f->flags = 0;

No need.

> +	memset(f->reserved, 0, sizeof(f->reserved));

No need. The core will automatically set flags and reserved.

> +
> +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;

No need for this, this is automatically done for you.

Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
in drivers/media/v4l2-core/v4l2-ioctl.c

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, false);
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, true);
> +}
> +
> +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq) {
> +		mtk_v4l2_err("no vb2 queue for type=%d",
> +					   f->type);
> +		return -EINVAL;
> +	}
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    (ctx->state >= MTK_STATE_HEADER)) {
> +		/* previous decode might have resolution change that make
> +		 * picinfo changed update context'x picinfo here to make sure
> +		 * we know what report to user space
> +		 */
> +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> +			sizeof(struct vdec_pic_info));
> +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> +					ctx->picinfo.y_len_sz;
> +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> +					ctx->picinfo.c_len_sz;
> +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->coded_width = ctx->picinfo.buf_w;
> +		q_data->coded_height = ctx->picinfo.buf_h;
> +
> +		/*
> +		 * Width and height are set to the dimensions
> +		 * of the movie, the buffer is bigger and
> +		 * further processing stages should crop to this
> +		 * rectangle.
> +		 */
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +
> +		/*
> +		 * Set pixelformat to the format in which mt vcodec
> +		 * outputs the decoded frame
> +		 */
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		/*
> +		 * This is run on OUTPUT
> +		 * The buffer contains compressed image
> +		 * so width and height have no meaning.
> +		 * Assign value here to pass v4l2-compliance test
> +		 */
> +		pix_mp->width = q_data->visible_width;
> +		pix_mp->height = q_data->visible_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +	} else {
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> +				ctx->idx, ctx->state, f->type);
> +		return -EAGAIN;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> +				   const void *parg,

Just like the encoder driver this patch needs to be rebased. The latest code no
longer has the parg argument.

> +				   unsigned int *nbuffers,
> +				   unsigned int *nplanes,
> +				   unsigned int sizes[], void *alloc_ctxs[])
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> +	struct mtk_q_data *q_data;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> +
> +	switch (vq->type) {
> +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> +		*nplanes = 1;
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		break;
> +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> +		if (ctx->state < MTK_STATE_HEADER)
> +			return -EINVAL;
> +
> +		/*
> +		 * Output plane count is 2 - one for Y and one for CbCr
> +		 */
> +		*nplanes = 2;
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		sizes[1] = q_data->sizeimage[1];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	mtk_v4l2_debug(1,
> +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> +			ctx->idx, vq->type, *nplanes, *nbuffers,
> +			sizes[0], sizes[1]);
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct mtk_q_data *q_data;
> +	int i;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> +			 ctx->idx, vb->vb2_queue->type, vb->index);
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> +
> +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> +				       i, vb2_plane_size(vb, i),
> +				       q_data->sizeimage[i]);
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> +			ctx->idx, vb->vb2_queue->type,
> +			vb->index, vb);
> +	/*
> +	 * check if this buffer is ready to be used after decode
> +	 */
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		struct vb2_buffer *src_buf;
> +		struct mtk_vcodec_mem buf;
> +		bool res_chg = false;
> +		int ret = 0;
> +		int dpbsize = 1;
> +
> +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> +
> +		if (ctx->state == MTK_STATE_INIT) {
> +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +			if (!src_buf) {
> +				mtk_v4l2_err("No src buffer");
> +				return;
> +			}
> +
> +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> +							src_buf,
> +							0);
> +			buf.size = (size_t)src_buf->planes[0].bytesused;
> +			mtk_v4l2_debug(2,
> +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> +					ctx->idx,
> +					src_buf->index,
> +					buf.va, (u64)buf.dma_addr, buf.size);
> +
> +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> +			if (ret) {
> +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +						VB2_BUF_STATE_DONE);
> +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> +						ctx->idx,
> +						src_buf->index,
> +						buf.size, ret);
> +				return;
> +			}
> +
> +			if (vdec_if_get_param(ctx,
> +						   GET_PARAM_PIC_INFO,
> +						   &ctx->picinfo)) {
> +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +						ctx->idx);
> +				return;
> +			}
> +
> +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> +				sizeof(struct vdec_pic_info));
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +						ctx->picinfo.y_bs_sz +
> +						ctx->picinfo.y_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> +							ctx->picinfo.buf_w;
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> +						ctx->picinfo.c_bs_sz +
> +						ctx->picinfo.c_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> +							ctx->picinfo.buf_w;
> +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> +				ctx->idx,
> +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> +
> +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +			if (dpbsize == 0) {
> +				mtk_v4l2_debug(2,
> +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> +						ctx->idx, ret);
> +				dpbsize = 1;
> +			}
> +			ctx->dpb_count = dpbsize;
> +			ctx->state = MTK_STATE_HEADER;
> +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> +							ctx->dpb_count);
> +		} else {
> +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> +					 ctx->idx, ctx->state);
> +		}
> +
> +	} else {
> +		mutex_lock(&buf->lock);
> +		if (buf->used == false) {
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> +					to_vb2_v4l2_buffer(vb));
> +			buf->queued_in_vb2 = true;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		} else {
> +			buf->queued_in_vb2 = false;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		}
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> +{
> +	if (vb->vb2_queue->type ==
> +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +		mutex_lock(&buf->lock);
> +		buf->queued_in_v4l2 = false;
> +		buf->queued_in_vb2 = false;
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> +{
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		buf->used = false;
> +		buf->ready_to_display = false;
> +		buf->nonrealdisplay = false;
> +		buf->queued_in_v4l2 = false;
> +		mutex_init(&buf->lock);
> +	} else {
> +		buf->lastframe = false;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	if (ctx->state == MTK_STATE_FLUSH)
> +		ctx->state = MTK_STATE_HEADER;
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> +{
> +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> +
> +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		while (src_buf) {
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +					VB2_BUF_STATE_ERROR);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		}
> +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		int i;
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		if (ctx->state >= MTK_STATE_HEADER)
> +			mtk_vdec_flush_decoder(ctx);
> +
> +		ctx->state = MTK_STATE_FLUSH;
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		while (dst_buf) {
> +			vb2_set_plane_payload(dst_buf, 0, 0);
> +			vb2_set_plane_payload(dst_buf, 1, 0);
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> +				VB2_BUF_STATE_ERROR);
> +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		}
> +
> +		for (i = 0; i < q->num_buffers; ++i) {
> +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> +				vb2_v4l2 = container_of(q->bufs[i],
> +					struct vb2_v4l2_buffer, vb2_buf);
> +				buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> +						ctx->idx, i, q->type,
> +						buf->queued_in_vb2,
> +						buf->queued_in_v4l2,
> +						(int)q->bufs[i]->state);
> +				v4l2_m2m_buf_done(vb2_v4l2,
> +						VB2_BUF_STATE_ERROR);
> +			}
> +		}
> +	}
> +}
> +
> +static void m2mops_vdec_device_run(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +
> +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> +}
> +
> +static int m2mops_vdec_job_ready(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (ctx->state == MTK_STATE_ABORT)
> +		return 0;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> +		return 0;
> +
> +	if (ctx->state != MTK_STATE_HEADER)
> +		return 0;
> +
> +	return 1;
> +}
> +
> +static void m2mops_vdec_job_abort(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +
> +	ctx->state = MTK_STATE_ABORT;
> +}
> +
> +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> +		if (ctx->state >= MTK_STATE_HEADER) {
> +			ctrl->val = ctx->dpb_count;
> +		} else {
> +			mtk_v4l2_err("Seqinfo not ready");
> +			ctrl->val = 1;
> +			return -EAGAIN;
> +		}
> +		break;
> +	default:
> +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> +};
> +
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct v4l2_ctrl *ctrl;
> +
> +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> +
> +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> +					  &mtk_vcodec_dec_ctrl_ops,
> +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> +					  1, 32, 1, 1);
> +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> +
> +	if (ctx->ctrl_hdl.error) {
> +		mtk_v4l2_err("Adding control failed %d",
> +					ctx->ctrl_hdl.error);
> +		return ctx->ctrl_hdl.error;
> +	}
> +
> +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> +	return 0;
> +}
> +
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> +{
> +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> +}
> +
> +static void m2mops_vdec_lock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_lock(&ctx->dev->dev_mutex);
> +}
> +
> +static void m2mops_vdec_unlock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_unlock(&ctx->dev->dev_mutex);
> +}
> +
> +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> +	.device_run	= m2mops_vdec_device_run,
> +	.job_ready	= m2mops_vdec_job_ready,
> +	.job_abort	= m2mops_vdec_job_abort,
> +	.lock	= m2mops_vdec_lock,
> +	.unlock	= m2mops_vdec_unlock,
> +};
> +
> +const struct vb2_ops mtk_vdec_vb2_ops = {
> +	.queue_setup	= vb2ops_vdec_queue_setup,
> +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> +	.buf_queue	= vb2ops_vdec_buf_queue,
> +	.wait_prepare	= vb2_ops_wait_prepare,
> +	.wait_finish	= vb2_ops_wait_finish,
> +	.buf_init	= vb2ops_vdec_buf_init,
> +	.buf_finish	= vb2ops_vdec_buf_finish,
> +	.start_streaming	= vb2ops_vdec_start_streaming,
> +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> +};
> +
> +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> +	.vidioc_streamon	= vidioc_vdec_streamon,
> +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> +
> +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> +
> +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> +
> +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> +
> +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> +
> +	.vidioc_querycap	= vidioc_vdec_querycap,
> +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> +};
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> new file mode 100644
> index 0000000..cb5cb42
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> @@ -0,0 +1,81 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#ifndef _MTK_VCODEC_DEC_H_
> +#define _MTK_VCODEC_DEC_H_
> +
> +#include <media/videobuf2-core.h>
> +#include <media/videobuf2-v4l2.h>
> +
> +/**
> + * struct vdec_fb  - decoder frame buffer
> + * @base_y	: Y plane memory info
> + * @base_c	: C plane memory info
> + * @status      : frame buffer status (vdec_fb_status)
> + */
> +struct vdec_fb {
> +	struct mtk_vcodec_mem	base_y;
> +	struct mtk_vcodec_mem	base_c;
> +	unsigned int	status;
> +};
> +
> +/**
> + * struct mtk_video_buf - Private data related to each VB2 buffer.
> + * @b:			VB2 buffer
> + * @list:			link list
> + * @used:		Output buffer contain decoded frame data
> + * @ready_to_display:	Output buffer not display yet
> + * @nonrealdisplay:	Output buffer is not display frame
> + * @queued_in_vb2:	Output buffer is queue in vb2
> + * @queued_in_v4l2:	Output buffer is in v4l2
> + * @lastframe:		Intput buffer is last buffer - EOS
> + * @frame_buffer:		Decode status of output buffer
> + * @lock:			V4L2 and decode thread should get mutex
> + *			before r/w info in mtk_video_buf
> + */
> +struct mtk_video_buf {
> +	struct vb2_v4l2_buffer	vb;
> +	struct list_head	list;
> +
> +	bool	used;
> +	bool	ready_to_display;
> +	bool	nonrealdisplay;
> +	bool	queued_in_vb2;
> +	bool	queued_in_v4l2;
> +	bool	lastframe;
> +	struct vdec_fb	frame_buffer;
> +	struct mutex	lock;
> +};
> +
> +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> +
> +
> +/*
> + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> + * get/release lock before/after access decoder hw.
> + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> + * to ctx instance that get lock
> + */
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq);
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> +
> +#endif /* _MTK_VCODEC_DEC_H_ */
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> new file mode 100644
> index 0000000..34e3217
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> @@ -0,0 +1,469 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of_device.h>
> +#include <linux/of.h>
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_dec_pm.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vpu.h"
> +
> +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> +
> +#define VDEC_HW_ACTIVE	0x10
> +#define VDEC_IRQ_CFG	0x11
> +#define VDEC_IRQ_CLR	0x10
> +#define VDEC_IRQ_CFG_REG	0xa4
> +
> +
> +/* Wake up context wait_queue */
> +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> +{
> +	ctx->int_cond = 1;
> +	ctx->int_type = reason;
> +	wake_up_interruptible(&ctx->queue);
> +}
> +
> +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	unsigned long flags;
> +	struct mtk_vcodec_ctx *ctx;
> +	unsigned int cg_status = 0;
> +	unsigned int dec_done_status = 0;
> +
> +	spin_lock_irqsave(&dev->irqlock, flags);
> +	ctx = dev->curr_ctx;
> +	spin_unlock_irqrestore(&dev->irqlock, flags);
> +
> +	cg_status = readl(dev->reg_base[0] + (0));
> +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> +			       cg_status);
> +		return IRQ_HANDLED;
> +	}
> +
> +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	ctx->irq_status = dec_done_status;
> +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> +		return IRQ_HANDLED;
> +
> +	/* clear interrupt */
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +
> +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> +
> +	mtk_v4l2_debug(3,
> +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> +			ctx->idx, dec_done_status);
> +
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int fops_vcodec_open(struct file *file)
> +{
> +	struct video_device *vfd = video_devdata(file);
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = NULL;
> +	int ret = 0;
> +
> +	if (dev->instance_mask == ~0UL) {
> +		mtk_v4l2_err("Too many open contexts");
> +		ret = -EBUSY;
> +		goto err_alloc;
> +	}
> +
> +	mutex_lock(&dev->dev_mutex);
> +
> +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> +	if (!ctx) {
> +		ret = -ENOMEM;
> +		goto err_alloc;
> +	}
> +
> +	ctx->idx = ffz(dev->instance_mask);
> +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> +	file->private_data = &ctx->fh;
> +	v4l2_fh_add(&ctx->fh);
> +	INIT_LIST_HEAD(&ctx->list);
> +	ctx->dev = dev;
> +	init_waitqueue_head(&ctx->queue);
> +
> +	if (vfd == dev->vfd_dec) {
> +		ctx->type = MTK_INST_DECODER;
> +		ret = mtk_vdec_ctrls_setup(ctx);
> +		if (ret) {
> +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> +			goto err_ctrls_setup;
> +		}
> +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> +						 &m2mctx_vdec_queue_init);
> +		if (IS_ERR(ctx->m2m_ctx)) {
> +			ret = PTR_ERR(ctx->m2m_ctx);
> +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> +				       ret);
> +			goto err_ctx_init;
> +		}
> +		mtk_vcodec_dec_set_default_params(ctx);
> +	} else {
> +		mtk_v4l2_err("Invalid vfd !");
> +		ret = -ENOENT;
> +		goto err_ctx_init;
> +	}
> +
> +	if (v4l2_fh_is_singular(&ctx->fh)) {
> +		mtk_vcodec_dec_pw_on(&dev->pm);
> +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> +		if (ret < 0) {
> +			mtk_v4l2_err("vpu_load_firmware failed!");
> +			goto err_load_fw;
> +		}
> +
> +		dev->dec_capability =
> +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> +		mtk_v4l2_debug(0, "decoder capability %x",
> +			       dev->dec_capability);
> +	}
> +
> +	set_bit(ctx->idx, &dev->instance_mask);
> +	dev->num_instances++;
> +	list_add(&ctx->list, &dev->ctx_list);
> +	mutex_unlock(&dev->dev_mutex);
> +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> +					ctx->idx);
> +
> +	return ret;
> +
> +	/* Deinit when failure occurred */
> +err_load_fw:
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +err_ctx_init:
> +	mtk_vdec_ctrls_free(ctx);
> +err_ctrls_setup:
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +err_alloc:
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_release(struct file *file)
> +{
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> +
> +	mutex_lock(&dev->dev_mutex);
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +	mtk_vcodec_vdec_release(ctx);
> +	mtk_vdec_ctrls_free(ctx);
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	list_del_init(&ctx->list);
> +	dev->num_instances--;
> +	if (dev->num_instances == 0)
> +		mtk_vcodec_dec_pw_off(&dev->pm);
> +	clear_bit(ctx->idx, &dev->instance_mask);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +	mutex_unlock(&dev->dev_mutex);
> +	return 0;
> +}
> +
> +static unsigned int fops_vcodec_poll(struct file *file,
> +				     struct poll_table_struct *wait)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	int ret;
> +
> +	mutex_lock(&dev->dev_mutex);
> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> +}
> +
> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> +	.owner				= THIS_MODULE,
> +	.open				= fops_vcodec_open,
> +	.release			= fops_vcodec_release,
> +	.poll				= fops_vcodec_poll,
> +	.unlocked_ioctl			= video_ioctl2,
> +	.mmap				= fops_vcodec_mmap,

You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.

> +};
> +
> +static void mtk_vcodec_dec_reset_handler(void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	struct mtk_vcodec_ctx *ctx;
> +
> +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> +
> +	mutex_lock(&dev->dev_mutex);
> +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> +		ctx->state = MTK_STATE_ABORT;
> +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> +					   ctx->idx);
> +	}
> +	mutex_unlock(&dev->dev_mutex);
> +}
> +
> +static int mtk_vcodec_probe(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev;
> +	struct video_device *vfd_dec;
> +	struct resource *res;
> +	int i, ret;
> +	int reg_base_num;
> +
> +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> +	if (!dev)
> +		return -ENOMEM;
> +
> +	INIT_LIST_HEAD(&dev->ctx_list);
> +	dev->plat_dev = pdev;
> +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> +	if (dev->vpu_plat_dev == NULL) {
> +		mtk_v4l2_err("[VPU] vpu device in not ready");
> +		return -EPROBE_DEFER;
> +	}
> +
> +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> +				dev, VPU_RST_DEC);
> +
> +	ret = mtk_vcodec_init_dec_pm(dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> +		return ret;
> +	}
> +
> +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> +	for (i = 0; i < reg_base_num; i++) {
> +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> +		if (res == NULL) {
> +			dev_err(&pdev->dev, "get memory resource failed.");
> +			ret = -ENXIO;
> +			goto err_res;
> +		}
> +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> +		if (IS_ERR(dev->reg_base[i])) {
> +			dev_err(&pdev->dev,
> +				"devm_ioremap_resource %d failed.", i);
> +			ret = PTR_ERR(dev->reg_base);
> +			goto err_res;
> +		}
> +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> +	if (res == NULL) {
> +		dev_err(&pdev->dev, "failed to get irq resource");
> +		ret = -ENOENT;
> +		goto err_res;
> +	}
> +
> +	dev->dec_irq = platform_get_irq(pdev, 0);
> +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> +			dev->dec_irq,
> +			ret);
> +		ret = -EINVAL;
> +		goto err_res;
> +	}
> +
> +	disable_irq(dev->dec_irq);
> +	mutex_init(&dev->dev_mutex);
> +	mutex_init(&dev->dec_mutex);
> +	spin_lock_init(&dev->irqlock);
> +
> +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> +		 "[MTK_V4L2]");
> +
> +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> +	if (ret) {
> +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> +		return ret;
> +	}
> +
> +	init_waitqueue_head(&dev->queue);
> +
> +	vfd_dec = video_device_alloc();
> +	if (!vfd_dec) {
> +		mtk_v4l2_err("Failed to allocate video device");
> +		ret = -ENOMEM;
> +		goto err_dec_alloc;
> +	}
> +	vfd_dec->fops		= &mtk_vcodec_fops;
> +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> +	vfd_dec->release	= video_device_release;
> +	vfd_dec->lock		= &dev->dev_mutex;
> +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> +
> +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> +		 MTK_VCODEC_DEC_NAME);
> +	video_set_drvdata(vfd_dec, dev);
> +	dev->vfd_dec = vfd_dec;
> +	platform_set_drvdata(pdev, dev);
> +
> +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> +	if (IS_ERR(dev->alloc_ctx)) {
> +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> +		ret = PTR_ERR(dev->alloc_ctx);
> +		goto err_vb2_ctx_init;
> +	}
> +
> +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> +	if (IS_ERR(dev->m2m_dev_dec)) {
> +		mtk_v4l2_err("Failed to init mem2mem dec device");
> +		ret = PTR_ERR(dev->m2m_dev_dec);
> +		goto err_dec_mem_init;
> +	}
> +
> +	dev->decode_workqueue =
> +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> +	if (!dev->decode_workqueue) {
> +		mtk_v4l2_err("Failed to create decode workqueue");
> +		ret = -EINVAL;
> +		goto err_event_workq;
> +	}
> +
> +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to register video device");
> +		goto err_dec_reg;
> +	}
> +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> +			 vfd_dec->num);
> +
> +	return 0;
> +
> +err_dec_reg:
> +	destroy_workqueue(dev->decode_workqueue);
> +err_event_workq:
> +	v4l2_m2m_release(dev->m2m_dev_dec);
> +err_dec_mem_init:
> +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +err_vb2_ctx_init:
> +	video_unregister_device(vfd_dec);
> +err_dec_alloc:
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +err_res:
> +	mtk_vcodec_release_dec_pm(dev);
> +	return ret;
> +}
> +
> +static const struct of_device_id mtk_vcodec_match[] = {
> +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> +	{},
> +};
> +
> +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> +
> +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug_enter();
> +	flush_workqueue(dev->decode_workqueue);
> +	destroy_workqueue(dev->decode_workqueue);
> +	if (dev->m2m_dev_dec)
> +		v4l2_m2m_release(dev->m2m_dev_dec);
> +	if (dev->alloc_ctx)
> +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +
> +	if (dev->vfd_dec)
> +		video_unregister_device(dev->vfd_dec);
> +
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +	mtk_vcodec_release_dec_pm(dev);
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> +			   mtk_vcodec_vdec_pm_runtime_resume,
> +			   NULL)
> +};
> +
> +static struct platform_driver mtk_vcodec_dec_driver = {
> +	.probe	= mtk_vcodec_probe,
> +	.remove	= mtk_vcodec_dec_remove,
> +	.driver	= {
> +		.name	= MTK_VCODEC_DEC_NAME,
> +		.owner	= THIS_MODULE,
> +		.of_match_table = mtk_vcodec_match,
> +		.pm = &mtk_vcodec_vdec_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(mtk_vcodec_dec_driver);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");

Regards,

	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-15 14:27         ` Hans Verkuil
  0 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-15 14:27 UTC (permalink / raw)
  To: Tiffany Lin, Hans Verkuil,
	daniel.thompson-QSEj5FYQhm4dnm+yROfE0A, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak
  Cc: Eddie Huang, Yingjoe Chen, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w

On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> Add v4l2 layer decoder driver for MT8173
> 
> Signed-off-by: Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> ---
>  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
>  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
>  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
>  11 files changed, 2596 insertions(+), 20 deletions(-)
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> 
> diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> index dc5cb00..4c8ed2f 100644
> --- a/drivers/media/platform/mtk-vcodec/Makefile
> +++ b/drivers/media/platform/mtk-vcodec/Makefile
> @@ -1,7 +1,13 @@
>  
>  
> -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> -
> +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> +				       mtk-vcodec-enc.o \
> +				       mtk-vcodec-common.o
> +
> +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> +		vdec_drv_if.o \
> +		mtk_vcodec_dec.o \
> +		mtk_vcodec_dec_pm.o \
>  
>  
>  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> new file mode 100644
> index 0000000..0499413
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> @@ -0,0 +1,1429 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*         Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vcodec_dec_pm.h"
> +
> +static struct mtk_video_fmt mtk_video_formats[] = {
> +	{
> +		.fourcc = V4L2_PIX_FMT_H264,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP8,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_MT21,
> +		.type = MTK_FMT_FRAME,
> +		.num_planes = 2,
> +	},
> +};
> +#define OUT_FMT_IDX	0
> +#define CAP_FMT_IDX	3
> +
> +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> +
> +#define MTK_VDEC_MIN_W	64U
> +#define MTK_VDEC_MIN_H	64U
> +#define MTK_VDEC_MAX_W	2048U
> +#define MTK_VDEC_MAX_H	1088U
> +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> +
> +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> +	{
> +		.fourcc	= V4L2_PIX_FMT_H264,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc	= V4L2_PIX_FMT_VP8,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +};
> +
> +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> +
> +
> +const struct vb2_ops mtk_vdec_vb2_ops;
> +
> +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +	unsigned int k;
> +
> +	for (k = 0; k < NUM_FORMATS; k++) {
> +		fmt = &mtk_video_formats[k];
> +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> +			return fmt;
> +	}
> +
> +	return NULL;
> +}
> +
> +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> +					      enum v4l2_buf_type type)
> +{
> +	if (V4L2_TYPE_IS_OUTPUT(type))
> +		return &ctx->q_data[MTK_Q_DATA_SRC];
> +
> +	return &ctx->q_data[MTK_Q_DATA_DST];
> +}
> +
> +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vdec_fb *disp_frame_buffer;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_DISP_FRAME_BUFFER,
> +				   &disp_frame_buffer)) {
> +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> +			     ctx->idx);
> +		return NULL;
> +	}
> +
> +	if (disp_frame_buffer == NULL) {
> +		mtk_v4l2_debug(3, "No display frame buffer");
> +		return NULL;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = disp_frame_buffer;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> +							ctx->picinfo.y_bs_sz);
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> +							ctx->picinfo.c_bs_sz);
> +
> +		dstbuf->ready_to_display = true;
> +		dstbuf->nonrealdisplay = false;
> +		mtk_v4l2_debug(2,
> +			       "[%d]status=%x queue idx=%d to done_list %d",
> +			       ctx->idx, frame_buffer->status,
> +			       dstbuf->vb.vb2_buf.index,
> +			       dstbuf->queued_in_vb2);
> +
> +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> +		ctx->decoded_frame_cnt++;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	void *tmp_frame_addr;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_FREE_FRAME_BUFFER,
> +				   &tmp_frame_addr)) {
> +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> +		return NULL;
> +	}
> +	if (tmp_frame_addr == NULL) {
> +		mtk_v4l2_debug(3, " No free frame buffer");
> +		return NULL;
> +	}
> +
> +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> +			 ctx->idx, tmp_frame_addr);
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = tmp_frame_addr;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		if ((dstbuf->queued_in_vb2) &&
> +		    (dstbuf->queued_in_v4l2) &&
> +		    (frame_buffer->status == FB_ST_FREE)) {
> +			mtk_v4l2_debug(2,
> +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> +				 ctx->idx, frame_buffer->status,
> +				 dstbuf->vb.vb2_buf.index,
> +				 dstbuf->queued_in_vb2);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +		} else if ((dstbuf->queued_in_vb2 == false) &&
> +			   (dstbuf->queued_in_v4l2 == true)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]status=%x queue idx=%d to rdy_queue",
> +					 ctx->idx, frame_buffer->status,
> +					 dstbuf->vb.vb2_buf.index);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +			dstbuf->queued_in_vb2 = true;
> +		} else {
> +			mtk_v4l2_debug(2,
> +					"[%d]status=%x err queue idx=%d %d %d",
> +					ctx->idx, frame_buffer->status,
> +					dstbuf->vb.vb2_buf.index,
> +					dstbuf->queued_in_vb2,
> +					dstbuf->queued_in_v4l2);
> +		}
> +		dstbuf->used = false;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_display_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_free_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> +{
> +	static const struct v4l2_event ev_src_ch = {
> +		.type = V4L2_EVENT_SOURCE_CHANGE,
> +		.u.src_change.changes =
> +		V4L2_EVENT_SRC_CH_RESOLUTION,
> +	};
> +
> +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> +
> +	return 0;
> +}
> +
> +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> +{
> +	bool res_chg;
> +	int ret = 0;
> +
> +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> +	if (ret)
> +		mtk_v4l2_err("DecodeFinal failed");
> +
> +	clean_display_buffer(ctx);
> +	clean_free_buffer(ctx);
> +}
> +
> +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> +{
> +	int dpbsize;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_PIC_INFO,
> +				   &ctx->last_decoded_picinfo)) {
> +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +				ctx->idx);
> +		return false;
> +	}
> +
> +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> +		ctx->last_decoded_picinfo.pic_h == 0 ||
> +		ctx->last_decoded_picinfo.buf_w == 0 ||
> +		ctx->last_decoded_picinfo.buf_h == 0)
> +		return false;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> +				 ctx->last_decoded_picinfo.pic_h,
> +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				 ctx->last_decoded_picinfo.buf_w,
> +				 ctx->last_decoded_picinfo.buf_h);
> +
> +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +		if (dpbsize == 0)
> +			dpbsize = 1;
> +		ctx->dpb_count = dpbsize;
> +		return true;
> +	}
> +
> +	return false;
> +}
> +
> +void mtk_vdec_worker(struct work_struct *work)
> +{
> +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> +				    decode_work);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	struct vb2_buffer *src_buf, *dst_buf;
> +	struct mtk_vcodec_mem buf;
> +	struct vdec_fb *pfb;
> +	bool res_chg = false;
> +	int ret;
> +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> +
> +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +	if (src_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> +	if (dst_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	buf.va = vb2_plane_vaddr(src_buf, 0);
> +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> +	if (!buf.va) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> +			       ctx->idx, src_buf->index);
> +		return;
> +	}
> +
> +	pfb = &dst_buf_info->frame_buffer;
> +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> +
> +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> +	pfb->status = 0;
> +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> +	mtk_v4l2_debug(3,
> +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> +			 ctx->idx, buf.va,
> +			 (u64)buf.dma_addr,
> +			 buf.size, src_buf);
> +
> +	mtk_v4l2_debug(3,
> +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> +			dst_buf->index, pfb,
> +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> +
> +	if (src_buf_info->lastframe == true) {
> +		/* update src buf status */
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		src_buf_info->lastframe = false;
> +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> +
> +		/* update dst buf status */
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		dst_buf_info->used = false;
> +
> +		clean_display_buffer(ctx);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> +		clean_free_buffer(ctx);
> +	} else {
> +
> +		dst_buf_info->vb.timestamp
> +					= src_buf_info->vb.timestamp;
> +		dst_buf_info->vb.timecode
> +					= src_buf_info->vb.timecode;
> +		mutex_lock(&dst_buf_info->lock);
> +		dst_buf_info->used = true;
> +		mutex_unlock(&dst_buf_info->lock);
> +		src_buf_info->used = true;
> +
> +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> +
> +		if (ret) {
> +			mtk_v4l2_err(
> +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> +				ctx->idx,
> +				src_buf->index,
> +				src_buf_info->lastframe,
> +				buf.size,
> +				src_buf_info->vb.timestamp.tv_sec,
> +				src_buf_info->vb.timestamp.tv_usec,
> +				dst_buf->index,
> +				ret, res_chg);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +					VB2_BUF_STATE_ERROR);
> +		} else if (res_chg == false) {
> +			/* we only return src buffer with VB2_BUF_STATE_DONE
> +			  * when decode success without resolution change
> +			  */
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +							VB2_BUF_STATE_DONE);
> +		}
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		clean_display_buffer(ctx);
> +		clean_free_buffer(ctx);
> +
> +		if ((ret == 0) && (res_chg == true)) {
> +			mtk_vdec_pic_info_update(ctx);
> +			/*
> +			  * On encountering a resolution change in the stream.
> +			  * The driver must first process and decode all
> +			  * remaining buffers from before the resolution change
> +			  * point, so call flush decode here
> +			  */
> +			mtk_vdec_flush_decoder(ctx);
> +			/*
> +			  * After all buffers containing decoded frames from
> +			  * before the resolution change point ready to be
> +			  * dequeued on the CAPTURE queue, the driver sends a
> +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> +			  */
> +			mtk_vdec_queue_res_chg_event(ctx);
> +		}
> +	}
> +
> +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +
> +}
> +
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_unlock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_lock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	int ret = 0;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	src_vq->drv_priv	= ctx;
> +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	src_vq->ops		= &mtk_vdec_vb2_ops;
> +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	src_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(src_vq);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> +		return ret;
> +	}
> +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	dst_vq->drv_priv	= ctx;
> +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	dst_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(dst_vq);
> +	if (ret) {
> +		vb2_queue_release(src_vq);
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> +	}
> +
> +	return ret;
> +}
> +
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> +{
> +	vdec_if_deinit(ctx);
> +	ctx->state = MTK_STATE_FREE;
> +}
> +
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct mtk_q_data *q_data;
> +
> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> +
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->coded_width = DFT_CFG_WIDTH;
> +	q_data->coded_height = DFT_CFG_HEIGHT;
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +
> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> +
> +	v4l_bound_align_image(&q_data->coded_width,
> +					MTK_VDEC_MIN_W,
> +					MTK_VDEC_MAX_W, 4,
> +					&q_data->coded_height,
> +					MTK_VDEC_MIN_H,
> +					MTK_VDEC_MAX_H, 5, 6);
> +
> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> +	q_data->bytesperline[0] = q_data->coded_width;
> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> +	q_data->bytesperline[1] = q_data->coded_width;
> +
> +}
> +
> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> +				enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +
> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> +				 enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> +			       struct v4l2_requestbuffers *reqbufs)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	int ret;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> +			 reqbufs->type, reqbufs->count);
> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> +
> +	return ret;
> +}

Please use the v4l2_m2m_ioctl_* helper functions were applicable.

> +
> +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> +				struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> +			 buf->type, buf->index);
> +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> +			    struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct vb2_queue *vq;
> +	struct vb2_buffer *vb;
> +	struct mtk_video_buf *mtkbuf;
> +	struct vb2_v4l2_buffer	*vb2_v4l2;
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> +	vb = vq->bufs[buf->index];
> +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    (buf->m.planes[0].bytesused == 0)) {
> +		mtkbuf->lastframe = true;
> +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> +			 ctx->idx, buf->type, buf->index,
> +			 mtkbuf->lastframe, buf->bytesused,
> +			 buf->m.planes[0].bytesused, buf->length,
> +			 vb);
> +	}
> +
> +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> +			     struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> +			      struct v4l2_exportbuffer *eb)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> +}
> +
> +static int vidioc_vdec_querycap(struct file *file, void *priv,
> +				struct v4l2_capability *cap)
> +{
> +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

See my comment about the new device_caps field in struct video_device from my
review of the encoder driver.

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_event_unsubscribe(fh, sub);
> +	default:
> +		return -EINVAL;
> +	}
> +}

No need for this function, just use v4l2_event_unsubscribe.

> +
> +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_src_change_event_subscribe(fh, sub);
> +	default:
> +		return -EINVAL;

Can't you just call v4l2_ctrl_subscribe_event() in the default case?

> +	}
> +}
> +
> +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> +		mtk_v4l2_err("sizeimage of output format must be given");
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> +			 struct v4l2_crop *cr)

Don't use g_crop, use g_selection instead. The selection API is a superset
of the old crop API.

> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state < MTK_STATE_HEADER)
> +		return -EINVAL;
> +
> +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> +
> +		if (vdec_if_get_param(ctx,
> +					   GET_PARAM_CROP_INFO,
> +					   cr)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> +					 ctx->idx);
> +			cr->c.left = 0;
> +			cr->c.top = 0;
> +			cr->c.width = ctx->picinfo.buf_w;
> +			cr->c.height = ctx->picinfo.buf_h;
> +		}
> +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> +				 cr->c.left, cr->c.top, cr->c.width,
> +				 cr->c.height);
> +	} else {
> +		cr->c.left = 0;
> +		cr->c.top = 0;
> +		cr->c.width = ctx->picinfo.pic_w;
> +		cr->c.height = ctx->picinfo.pic_h;
> +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> +				 ctx->picinfo.buf_h);
> +	}
> +	return 0;
> +}
> +
> +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +	int ret;
> +	struct mtk_video_fmt *fmt;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (fmt == NULL) {
> +		mtk_v4l2_err("mtk_venc_find_format fail");
> +		return ret;
> +	}
> +
> +	pix_mp = &f->fmt.pix_mp;
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> +		mtk_v4l2_err("out_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq)
> +		return -EINVAL;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +	if (!q_data)
> +		return -EINVAL;
> +
> +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		q_data->fmt = fmt;
> +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> +		pix_mp->plane_fmt[0].bytesperline = 0;
> +		pix_mp->width = 0;
> +		pix_mp->height = 0;
> +
> +		if (ctx->state == MTK_STATE_FREE) {
> +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> +			if (ret) {
> +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> +					       ctx->idx, ret);
> +				return -EINVAL;
> +			}
> +			ctx->state = MTK_STATE_INIT;
> +		}
> +	} else {
> +		q_data->fmt = fmt;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_enum_framesizes(struct file *file, void *priv,
> +				struct v4l2_frmsizeenum *fsize)
> +{
> +	int i = 0;
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (fsize->index != 0)
> +		return -EINVAL;
> +
> +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> +			continue;
> +
> +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> +		if (!(ctx->dev->dec_capability &
> +				VCODEC_CAPABILITY_4K_DISABLED)) {
> +			mtk_v4l2_debug(1, "4K is enabled");
> +			fsize->stepwise.max_width =
> +					VCODEC_DEC_4K_CODED_WIDTH;
> +			fsize->stepwise.max_height =
> +					VCODEC_DEC_4K_CODED_HEIGHT;
> +		}
> +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> +					ctx->dev->dec_capability,
> +					fsize->stepwise.min_width,
> +					fsize->stepwise.max_width,
> +					fsize->stepwise.step_width,
> +					fsize->stepwise.min_height,
> +					fsize->stepwise.max_height,
> +					fsize->stepwise.step_height);
> +		return 0;
> +	}
> +
> +	return -EINVAL;
> +
> +}
> +
> +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> +{
> +	struct mtk_video_fmt *fmt;
> +	int i, j = 0;
> +
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> +			continue;
> +		if (!output_queue &&
> +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> +			continue;
> +
> +		if (j == f->index)
> +			break;
> +		++j;
> +	}
> +
> +	if (i == NUM_FORMATS)
> +		return -EINVAL;
> +
> +	fmt = &mtk_video_formats[i];
> +	f->pixelformat = fmt->fourcc;
> +	f->flags = 0;

No need.

> +	memset(f->reserved, 0, sizeof(f->reserved));

No need. The core will automatically set flags and reserved.

> +
> +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;

No need for this, this is automatically done for you.

Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
in drivers/media/v4l2-core/v4l2-ioctl.c

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, false);
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, true);
> +}
> +
> +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq) {
> +		mtk_v4l2_err("no vb2 queue for type=%d",
> +					   f->type);
> +		return -EINVAL;
> +	}
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    (ctx->state >= MTK_STATE_HEADER)) {
> +		/* previous decode might have resolution change that make
> +		 * picinfo changed update context'x picinfo here to make sure
> +		 * we know what report to user space
> +		 */
> +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> +			sizeof(struct vdec_pic_info));
> +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> +					ctx->picinfo.y_len_sz;
> +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> +					ctx->picinfo.c_len_sz;
> +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->coded_width = ctx->picinfo.buf_w;
> +		q_data->coded_height = ctx->picinfo.buf_h;
> +
> +		/*
> +		 * Width and height are set to the dimensions
> +		 * of the movie, the buffer is bigger and
> +		 * further processing stages should crop to this
> +		 * rectangle.
> +		 */
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +
> +		/*
> +		 * Set pixelformat to the format in which mt vcodec
> +		 * outputs the decoded frame
> +		 */
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		/*
> +		 * This is run on OUTPUT
> +		 * The buffer contains compressed image
> +		 * so width and height have no meaning.
> +		 * Assign value here to pass v4l2-compliance test
> +		 */
> +		pix_mp->width = q_data->visible_width;
> +		pix_mp->height = q_data->visible_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +	} else {
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> +				ctx->idx, ctx->state, f->type);
> +		return -EAGAIN;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> +				   const void *parg,

Just like the encoder driver this patch needs to be rebased. The latest code no
longer has the parg argument.

> +				   unsigned int *nbuffers,
> +				   unsigned int *nplanes,
> +				   unsigned int sizes[], void *alloc_ctxs[])
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> +	struct mtk_q_data *q_data;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> +
> +	switch (vq->type) {
> +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> +		*nplanes = 1;
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		break;
> +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> +		if (ctx->state < MTK_STATE_HEADER)
> +			return -EINVAL;
> +
> +		/*
> +		 * Output plane count is 2 - one for Y and one for CbCr
> +		 */
> +		*nplanes = 2;
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		sizes[1] = q_data->sizeimage[1];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	mtk_v4l2_debug(1,
> +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> +			ctx->idx, vq->type, *nplanes, *nbuffers,
> +			sizes[0], sizes[1]);
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct mtk_q_data *q_data;
> +	int i;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> +			 ctx->idx, vb->vb2_queue->type, vb->index);
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> +
> +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> +				       i, vb2_plane_size(vb, i),
> +				       q_data->sizeimage[i]);
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> +			ctx->idx, vb->vb2_queue->type,
> +			vb->index, vb);
> +	/*
> +	 * check if this buffer is ready to be used after decode
> +	 */
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		struct vb2_buffer *src_buf;
> +		struct mtk_vcodec_mem buf;
> +		bool res_chg = false;
> +		int ret = 0;
> +		int dpbsize = 1;
> +
> +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> +
> +		if (ctx->state == MTK_STATE_INIT) {
> +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +			if (!src_buf) {
> +				mtk_v4l2_err("No src buffer");
> +				return;
> +			}
> +
> +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> +							src_buf,
> +							0);
> +			buf.size = (size_t)src_buf->planes[0].bytesused;
> +			mtk_v4l2_debug(2,
> +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> +					ctx->idx,
> +					src_buf->index,
> +					buf.va, (u64)buf.dma_addr, buf.size);
> +
> +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> +			if (ret) {
> +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +						VB2_BUF_STATE_DONE);
> +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> +						ctx->idx,
> +						src_buf->index,
> +						buf.size, ret);
> +				return;
> +			}
> +
> +			if (vdec_if_get_param(ctx,
> +						   GET_PARAM_PIC_INFO,
> +						   &ctx->picinfo)) {
> +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +						ctx->idx);
> +				return;
> +			}
> +
> +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> +				sizeof(struct vdec_pic_info));
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +						ctx->picinfo.y_bs_sz +
> +						ctx->picinfo.y_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> +							ctx->picinfo.buf_w;
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> +						ctx->picinfo.c_bs_sz +
> +						ctx->picinfo.c_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> +							ctx->picinfo.buf_w;
> +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> +				ctx->idx,
> +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> +
> +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +			if (dpbsize == 0) {
> +				mtk_v4l2_debug(2,
> +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> +						ctx->idx, ret);
> +				dpbsize = 1;
> +			}
> +			ctx->dpb_count = dpbsize;
> +			ctx->state = MTK_STATE_HEADER;
> +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> +							ctx->dpb_count);
> +		} else {
> +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> +					 ctx->idx, ctx->state);
> +		}
> +
> +	} else {
> +		mutex_lock(&buf->lock);
> +		if (buf->used == false) {
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> +					to_vb2_v4l2_buffer(vb));
> +			buf->queued_in_vb2 = true;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		} else {
> +			buf->queued_in_vb2 = false;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		}
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> +{
> +	if (vb->vb2_queue->type ==
> +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +		mutex_lock(&buf->lock);
> +		buf->queued_in_v4l2 = false;
> +		buf->queued_in_vb2 = false;
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> +{
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		buf->used = false;
> +		buf->ready_to_display = false;
> +		buf->nonrealdisplay = false;
> +		buf->queued_in_v4l2 = false;
> +		mutex_init(&buf->lock);
> +	} else {
> +		buf->lastframe = false;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	if (ctx->state == MTK_STATE_FLUSH)
> +		ctx->state = MTK_STATE_HEADER;
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> +{
> +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> +
> +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		while (src_buf) {
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +					VB2_BUF_STATE_ERROR);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		}
> +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		int i;
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		if (ctx->state >= MTK_STATE_HEADER)
> +			mtk_vdec_flush_decoder(ctx);
> +
> +		ctx->state = MTK_STATE_FLUSH;
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		while (dst_buf) {
> +			vb2_set_plane_payload(dst_buf, 0, 0);
> +			vb2_set_plane_payload(dst_buf, 1, 0);
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> +				VB2_BUF_STATE_ERROR);
> +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		}
> +
> +		for (i = 0; i < q->num_buffers; ++i) {
> +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> +				vb2_v4l2 = container_of(q->bufs[i],
> +					struct vb2_v4l2_buffer, vb2_buf);
> +				buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> +						ctx->idx, i, q->type,
> +						buf->queued_in_vb2,
> +						buf->queued_in_v4l2,
> +						(int)q->bufs[i]->state);
> +				v4l2_m2m_buf_done(vb2_v4l2,
> +						VB2_BUF_STATE_ERROR);
> +			}
> +		}
> +	}
> +}
> +
> +static void m2mops_vdec_device_run(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +
> +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> +}
> +
> +static int m2mops_vdec_job_ready(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (ctx->state == MTK_STATE_ABORT)
> +		return 0;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> +		return 0;
> +
> +	if (ctx->state != MTK_STATE_HEADER)
> +		return 0;
> +
> +	return 1;
> +}
> +
> +static void m2mops_vdec_job_abort(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +
> +	ctx->state = MTK_STATE_ABORT;
> +}
> +
> +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> +		if (ctx->state >= MTK_STATE_HEADER) {
> +			ctrl->val = ctx->dpb_count;
> +		} else {
> +			mtk_v4l2_err("Seqinfo not ready");
> +			ctrl->val = 1;
> +			return -EAGAIN;
> +		}
> +		break;
> +	default:
> +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> +};
> +
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct v4l2_ctrl *ctrl;
> +
> +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> +
> +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> +					  &mtk_vcodec_dec_ctrl_ops,
> +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> +					  1, 32, 1, 1);
> +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> +
> +	if (ctx->ctrl_hdl.error) {
> +		mtk_v4l2_err("Adding control failed %d",
> +					ctx->ctrl_hdl.error);
> +		return ctx->ctrl_hdl.error;
> +	}
> +
> +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> +	return 0;
> +}
> +
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> +{
> +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> +}
> +
> +static void m2mops_vdec_lock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_lock(&ctx->dev->dev_mutex);
> +}
> +
> +static void m2mops_vdec_unlock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_unlock(&ctx->dev->dev_mutex);
> +}
> +
> +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> +	.device_run	= m2mops_vdec_device_run,
> +	.job_ready	= m2mops_vdec_job_ready,
> +	.job_abort	= m2mops_vdec_job_abort,
> +	.lock	= m2mops_vdec_lock,
> +	.unlock	= m2mops_vdec_unlock,
> +};
> +
> +const struct vb2_ops mtk_vdec_vb2_ops = {
> +	.queue_setup	= vb2ops_vdec_queue_setup,
> +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> +	.buf_queue	= vb2ops_vdec_buf_queue,
> +	.wait_prepare	= vb2_ops_wait_prepare,
> +	.wait_finish	= vb2_ops_wait_finish,
> +	.buf_init	= vb2ops_vdec_buf_init,
> +	.buf_finish	= vb2ops_vdec_buf_finish,
> +	.start_streaming	= vb2ops_vdec_start_streaming,
> +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> +};
> +
> +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> +	.vidioc_streamon	= vidioc_vdec_streamon,
> +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> +
> +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> +
> +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> +
> +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> +
> +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> +
> +	.vidioc_querycap	= vidioc_vdec_querycap,
> +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> +};
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> new file mode 100644
> index 0000000..cb5cb42
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> @@ -0,0 +1,81 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*         Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#ifndef _MTK_VCODEC_DEC_H_
> +#define _MTK_VCODEC_DEC_H_
> +
> +#include <media/videobuf2-core.h>
> +#include <media/videobuf2-v4l2.h>
> +
> +/**
> + * struct vdec_fb  - decoder frame buffer
> + * @base_y	: Y plane memory info
> + * @base_c	: C plane memory info
> + * @status      : frame buffer status (vdec_fb_status)
> + */
> +struct vdec_fb {
> +	struct mtk_vcodec_mem	base_y;
> +	struct mtk_vcodec_mem	base_c;
> +	unsigned int	status;
> +};
> +
> +/**
> + * struct mtk_video_buf - Private data related to each VB2 buffer.
> + * @b:			VB2 buffer
> + * @list:			link list
> + * @used:		Output buffer contain decoded frame data
> + * @ready_to_display:	Output buffer not display yet
> + * @nonrealdisplay:	Output buffer is not display frame
> + * @queued_in_vb2:	Output buffer is queue in vb2
> + * @queued_in_v4l2:	Output buffer is in v4l2
> + * @lastframe:		Intput buffer is last buffer - EOS
> + * @frame_buffer:		Decode status of output buffer
> + * @lock:			V4L2 and decode thread should get mutex
> + *			before r/w info in mtk_video_buf
> + */
> +struct mtk_video_buf {
> +	struct vb2_v4l2_buffer	vb;
> +	struct list_head	list;
> +
> +	bool	used;
> +	bool	ready_to_display;
> +	bool	nonrealdisplay;
> +	bool	queued_in_vb2;
> +	bool	queued_in_v4l2;
> +	bool	lastframe;
> +	struct vdec_fb	frame_buffer;
> +	struct mutex	lock;
> +};
> +
> +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> +
> +
> +/*
> + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> + * get/release lock before/after access decoder hw.
> + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> + * to ctx instance that get lock
> + */
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq);
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> +
> +#endif /* _MTK_VCODEC_DEC_H_ */
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> new file mode 100644
> index 0000000..34e3217
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> @@ -0,0 +1,469 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*         Tiffany Lin <tiffany.lin-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of_device.h>
> +#include <linux/of.h>
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_dec_pm.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vpu.h"
> +
> +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> +
> +#define VDEC_HW_ACTIVE	0x10
> +#define VDEC_IRQ_CFG	0x11
> +#define VDEC_IRQ_CLR	0x10
> +#define VDEC_IRQ_CFG_REG	0xa4
> +
> +
> +/* Wake up context wait_queue */
> +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> +{
> +	ctx->int_cond = 1;
> +	ctx->int_type = reason;
> +	wake_up_interruptible(&ctx->queue);
> +}
> +
> +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	unsigned long flags;
> +	struct mtk_vcodec_ctx *ctx;
> +	unsigned int cg_status = 0;
> +	unsigned int dec_done_status = 0;
> +
> +	spin_lock_irqsave(&dev->irqlock, flags);
> +	ctx = dev->curr_ctx;
> +	spin_unlock_irqrestore(&dev->irqlock, flags);
> +
> +	cg_status = readl(dev->reg_base[0] + (0));
> +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> +			       cg_status);
> +		return IRQ_HANDLED;
> +	}
> +
> +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	ctx->irq_status = dec_done_status;
> +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> +		return IRQ_HANDLED;
> +
> +	/* clear interrupt */
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +
> +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> +
> +	mtk_v4l2_debug(3,
> +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> +			ctx->idx, dec_done_status);
> +
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int fops_vcodec_open(struct file *file)
> +{
> +	struct video_device *vfd = video_devdata(file);
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = NULL;
> +	int ret = 0;
> +
> +	if (dev->instance_mask == ~0UL) {
> +		mtk_v4l2_err("Too many open contexts");
> +		ret = -EBUSY;
> +		goto err_alloc;
> +	}
> +
> +	mutex_lock(&dev->dev_mutex);
> +
> +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> +	if (!ctx) {
> +		ret = -ENOMEM;
> +		goto err_alloc;
> +	}
> +
> +	ctx->idx = ffz(dev->instance_mask);
> +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> +	file->private_data = &ctx->fh;
> +	v4l2_fh_add(&ctx->fh);
> +	INIT_LIST_HEAD(&ctx->list);
> +	ctx->dev = dev;
> +	init_waitqueue_head(&ctx->queue);
> +
> +	if (vfd == dev->vfd_dec) {
> +		ctx->type = MTK_INST_DECODER;
> +		ret = mtk_vdec_ctrls_setup(ctx);
> +		if (ret) {
> +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> +			goto err_ctrls_setup;
> +		}
> +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> +						 &m2mctx_vdec_queue_init);
> +		if (IS_ERR(ctx->m2m_ctx)) {
> +			ret = PTR_ERR(ctx->m2m_ctx);
> +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> +				       ret);
> +			goto err_ctx_init;
> +		}
> +		mtk_vcodec_dec_set_default_params(ctx);
> +	} else {
> +		mtk_v4l2_err("Invalid vfd !");
> +		ret = -ENOENT;
> +		goto err_ctx_init;
> +	}
> +
> +	if (v4l2_fh_is_singular(&ctx->fh)) {
> +		mtk_vcodec_dec_pw_on(&dev->pm);
> +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> +		if (ret < 0) {
> +			mtk_v4l2_err("vpu_load_firmware failed!");
> +			goto err_load_fw;
> +		}
> +
> +		dev->dec_capability =
> +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> +		mtk_v4l2_debug(0, "decoder capability %x",
> +			       dev->dec_capability);
> +	}
> +
> +	set_bit(ctx->idx, &dev->instance_mask);
> +	dev->num_instances++;
> +	list_add(&ctx->list, &dev->ctx_list);
> +	mutex_unlock(&dev->dev_mutex);
> +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> +					ctx->idx);
> +
> +	return ret;
> +
> +	/* Deinit when failure occurred */
> +err_load_fw:
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +err_ctx_init:
> +	mtk_vdec_ctrls_free(ctx);
> +err_ctrls_setup:
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +err_alloc:
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_release(struct file *file)
> +{
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> +
> +	mutex_lock(&dev->dev_mutex);
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +	mtk_vcodec_vdec_release(ctx);
> +	mtk_vdec_ctrls_free(ctx);
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	list_del_init(&ctx->list);
> +	dev->num_instances--;
> +	if (dev->num_instances == 0)
> +		mtk_vcodec_dec_pw_off(&dev->pm);
> +	clear_bit(ctx->idx, &dev->instance_mask);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +	mutex_unlock(&dev->dev_mutex);
> +	return 0;
> +}
> +
> +static unsigned int fops_vcodec_poll(struct file *file,
> +				     struct poll_table_struct *wait)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	int ret;
> +
> +	mutex_lock(&dev->dev_mutex);
> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> +}
> +
> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> +	.owner				= THIS_MODULE,
> +	.open				= fops_vcodec_open,
> +	.release			= fops_vcodec_release,
> +	.poll				= fops_vcodec_poll,
> +	.unlocked_ioctl			= video_ioctl2,
> +	.mmap				= fops_vcodec_mmap,

You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.

> +};
> +
> +static void mtk_vcodec_dec_reset_handler(void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	struct mtk_vcodec_ctx *ctx;
> +
> +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> +
> +	mutex_lock(&dev->dev_mutex);
> +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> +		ctx->state = MTK_STATE_ABORT;
> +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> +					   ctx->idx);
> +	}
> +	mutex_unlock(&dev->dev_mutex);
> +}
> +
> +static int mtk_vcodec_probe(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev;
> +	struct video_device *vfd_dec;
> +	struct resource *res;
> +	int i, ret;
> +	int reg_base_num;
> +
> +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> +	if (!dev)
> +		return -ENOMEM;
> +
> +	INIT_LIST_HEAD(&dev->ctx_list);
> +	dev->plat_dev = pdev;
> +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> +	if (dev->vpu_plat_dev == NULL) {
> +		mtk_v4l2_err("[VPU] vpu device in not ready");
> +		return -EPROBE_DEFER;
> +	}
> +
> +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> +				dev, VPU_RST_DEC);
> +
> +	ret = mtk_vcodec_init_dec_pm(dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> +		return ret;
> +	}
> +
> +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> +	for (i = 0; i < reg_base_num; i++) {
> +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> +		if (res == NULL) {
> +			dev_err(&pdev->dev, "get memory resource failed.");
> +			ret = -ENXIO;
> +			goto err_res;
> +		}
> +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> +		if (IS_ERR(dev->reg_base[i])) {
> +			dev_err(&pdev->dev,
> +				"devm_ioremap_resource %d failed.", i);
> +			ret = PTR_ERR(dev->reg_base);
> +			goto err_res;
> +		}
> +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> +	if (res == NULL) {
> +		dev_err(&pdev->dev, "failed to get irq resource");
> +		ret = -ENOENT;
> +		goto err_res;
> +	}
> +
> +	dev->dec_irq = platform_get_irq(pdev, 0);
> +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> +			dev->dec_irq,
> +			ret);
> +		ret = -EINVAL;
> +		goto err_res;
> +	}
> +
> +	disable_irq(dev->dec_irq);
> +	mutex_init(&dev->dev_mutex);
> +	mutex_init(&dev->dec_mutex);
> +	spin_lock_init(&dev->irqlock);
> +
> +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> +		 "[MTK_V4L2]");
> +
> +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> +	if (ret) {
> +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> +		return ret;
> +	}
> +
> +	init_waitqueue_head(&dev->queue);
> +
> +	vfd_dec = video_device_alloc();
> +	if (!vfd_dec) {
> +		mtk_v4l2_err("Failed to allocate video device");
> +		ret = -ENOMEM;
> +		goto err_dec_alloc;
> +	}
> +	vfd_dec->fops		= &mtk_vcodec_fops;
> +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> +	vfd_dec->release	= video_device_release;
> +	vfd_dec->lock		= &dev->dev_mutex;
> +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> +
> +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> +		 MTK_VCODEC_DEC_NAME);
> +	video_set_drvdata(vfd_dec, dev);
> +	dev->vfd_dec = vfd_dec;
> +	platform_set_drvdata(pdev, dev);
> +
> +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> +	if (IS_ERR(dev->alloc_ctx)) {
> +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> +		ret = PTR_ERR(dev->alloc_ctx);
> +		goto err_vb2_ctx_init;
> +	}
> +
> +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> +	if (IS_ERR(dev->m2m_dev_dec)) {
> +		mtk_v4l2_err("Failed to init mem2mem dec device");
> +		ret = PTR_ERR(dev->m2m_dev_dec);
> +		goto err_dec_mem_init;
> +	}
> +
> +	dev->decode_workqueue =
> +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> +	if (!dev->decode_workqueue) {
> +		mtk_v4l2_err("Failed to create decode workqueue");
> +		ret = -EINVAL;
> +		goto err_event_workq;
> +	}
> +
> +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to register video device");
> +		goto err_dec_reg;
> +	}
> +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> +			 vfd_dec->num);
> +
> +	return 0;
> +
> +err_dec_reg:
> +	destroy_workqueue(dev->decode_workqueue);
> +err_event_workq:
> +	v4l2_m2m_release(dev->m2m_dev_dec);
> +err_dec_mem_init:
> +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +err_vb2_ctx_init:
> +	video_unregister_device(vfd_dec);
> +err_dec_alloc:
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +err_res:
> +	mtk_vcodec_release_dec_pm(dev);
> +	return ret;
> +}
> +
> +static const struct of_device_id mtk_vcodec_match[] = {
> +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> +	{},
> +};
> +
> +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> +
> +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug_enter();
> +	flush_workqueue(dev->decode_workqueue);
> +	destroy_workqueue(dev->decode_workqueue);
> +	if (dev->m2m_dev_dec)
> +		v4l2_m2m_release(dev->m2m_dev_dec);
> +	if (dev->alloc_ctx)
> +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +
> +	if (dev->vfd_dec)
> +		video_unregister_device(dev->vfd_dec);
> +
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +	mtk_vcodec_release_dec_pm(dev);
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> +			   mtk_vcodec_vdec_pm_runtime_resume,
> +			   NULL)
> +};
> +
> +static struct platform_driver mtk_vcodec_dec_driver = {
> +	.probe	= mtk_vcodec_probe,
> +	.remove	= mtk_vcodec_dec_remove,
> +	.driver	= {
> +		.name	= MTK_VCODEC_DEC_NAME,
> +		.owner	= THIS_MODULE,
> +		.of_match_table = mtk_vcodec_match,
> +		.pm = &mtk_vcodec_vdec_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(mtk_vcodec_dec_driver);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");

Regards,

	Hans

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-15 14:27         ` Hans Verkuil
  0 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-15 14:27 UTC (permalink / raw)
  To: linux-arm-kernel

On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> Add v4l2 layer decoder driver for MT8173
> 
> Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> ---
>  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
>  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
>  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
>  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
>  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
>  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
>  11 files changed, 2596 insertions(+), 20 deletions(-)
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
>  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> 
> diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> index dc5cb00..4c8ed2f 100644
> --- a/drivers/media/platform/mtk-vcodec/Makefile
> +++ b/drivers/media/platform/mtk-vcodec/Makefile
> @@ -1,7 +1,13 @@
>  
>  
> -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> -
> +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> +				       mtk-vcodec-enc.o \
> +				       mtk-vcodec-common.o
> +
> +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> +		vdec_drv_if.o \
> +		mtk_vcodec_dec.o \
> +		mtk_vcodec_dec_pm.o \
>  
>  
>  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> new file mode 100644
> index 0000000..0499413
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> @@ -0,0 +1,1429 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vcodec_dec_pm.h"
> +
> +static struct mtk_video_fmt mtk_video_formats[] = {
> +	{
> +		.fourcc = V4L2_PIX_FMT_H264,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP8,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.type = MTK_FMT_DEC,
> +		.num_planes	= 1,
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_MT21,
> +		.type = MTK_FMT_FRAME,
> +		.num_planes = 2,
> +	},
> +};
> +#define OUT_FMT_IDX	0
> +#define CAP_FMT_IDX	3
> +
> +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> +
> +#define MTK_VDEC_MIN_W	64U
> +#define MTK_VDEC_MIN_H	64U
> +#define MTK_VDEC_MAX_W	2048U
> +#define MTK_VDEC_MAX_H	1088U
> +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> +
> +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> +	{
> +		.fourcc	= V4L2_PIX_FMT_H264,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc	= V4L2_PIX_FMT_VP8,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +	{
> +		.fourcc = V4L2_PIX_FMT_VP9,
> +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> +	},
> +};
> +
> +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> +
> +
> +const struct vb2_ops mtk_vdec_vb2_ops;
> +
> +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +	unsigned int k;
> +
> +	for (k = 0; k < NUM_FORMATS; k++) {
> +		fmt = &mtk_video_formats[k];
> +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> +			return fmt;
> +	}
> +
> +	return NULL;
> +}
> +
> +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> +					      enum v4l2_buf_type type)
> +{
> +	if (V4L2_TYPE_IS_OUTPUT(type))
> +		return &ctx->q_data[MTK_Q_DATA_SRC];
> +
> +	return &ctx->q_data[MTK_Q_DATA_DST];
> +}
> +
> +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vdec_fb *disp_frame_buffer;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_DISP_FRAME_BUFFER,
> +				   &disp_frame_buffer)) {
> +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> +			     ctx->idx);
> +		return NULL;
> +	}
> +
> +	if (disp_frame_buffer == NULL) {
> +		mtk_v4l2_debug(3, "No display frame buffer");
> +		return NULL;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = disp_frame_buffer;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> +							ctx->picinfo.y_bs_sz);
> +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> +							ctx->picinfo.c_bs_sz);
> +
> +		dstbuf->ready_to_display = true;
> +		dstbuf->nonrealdisplay = false;
> +		mtk_v4l2_debug(2,
> +			       "[%d]status=%x queue idx=%d to done_list %d",
> +			       ctx->idx, frame_buffer->status,
> +			       dstbuf->vb.vb2_buf.index,
> +			       dstbuf->queued_in_vb2);
> +
> +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> +		ctx->decoded_frame_cnt++;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	void *tmp_frame_addr;
> +	struct vb2_queue *vq;
> +	struct mtk_video_buf *dstbuf;
> +	struct vdec_fb *frame_buffer;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_FREE_FRAME_BUFFER,
> +				   &tmp_frame_addr)) {
> +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> +		return NULL;
> +	}
> +	if (tmp_frame_addr == NULL) {
> +		mtk_v4l2_debug(3, " No free frame buffer");
> +		return NULL;
> +	}
> +
> +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> +			 ctx->idx, tmp_frame_addr);
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> +	if (!vq) {
> +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> +		return NULL;
> +	}
> +
> +	frame_buffer = tmp_frame_addr;
> +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> +
> +	mutex_lock(&dstbuf->lock);
> +	if (dstbuf->used) {
> +		if ((dstbuf->queued_in_vb2) &&
> +		    (dstbuf->queued_in_v4l2) &&
> +		    (frame_buffer->status == FB_ST_FREE)) {
> +			mtk_v4l2_debug(2,
> +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> +				 ctx->idx, frame_buffer->status,
> +				 dstbuf->vb.vb2_buf.index,
> +				 dstbuf->queued_in_vb2);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +		} else if ((dstbuf->queued_in_vb2 == false) &&
> +			   (dstbuf->queued_in_v4l2 == true)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]status=%x queue idx=%d to rdy_queue",
> +					 ctx->idx, frame_buffer->status,
> +					 dstbuf->vb.vb2_buf.index);
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> +			dstbuf->queued_in_vb2 = true;
> +		} else {
> +			mtk_v4l2_debug(2,
> +					"[%d]status=%x err queue idx=%d %d %d",
> +					ctx->idx, frame_buffer->status,
> +					dstbuf->vb.vb2_buf.index,
> +					dstbuf->queued_in_vb2,
> +					dstbuf->queued_in_v4l2);
> +		}
> +		dstbuf->used = false;
> +	}
> +	mutex_unlock(&dstbuf->lock);
> +	return &dstbuf->vb.vb2_buf;
> +}
> +
> +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_display_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct vb2_buffer *framptr;
> +
> +	do {
> +		framptr = get_free_buffer(ctx);
> +	} while (framptr);
> +}
> +
> +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> +{
> +	static const struct v4l2_event ev_src_ch = {
> +		.type = V4L2_EVENT_SOURCE_CHANGE,
> +		.u.src_change.changes =
> +		V4L2_EVENT_SRC_CH_RESOLUTION,
> +	};
> +
> +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> +
> +	return 0;
> +}
> +
> +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> +{
> +	bool res_chg;
> +	int ret = 0;
> +
> +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> +	if (ret)
> +		mtk_v4l2_err("DecodeFinal failed");
> +
> +	clean_display_buffer(ctx);
> +	clean_free_buffer(ctx);
> +}
> +
> +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> +{
> +	int dpbsize;
> +
> +	if (vdec_if_get_param(ctx,
> +				   GET_PARAM_PIC_INFO,
> +				   &ctx->last_decoded_picinfo)) {
> +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +				ctx->idx);
> +		return false;
> +	}
> +
> +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> +		ctx->last_decoded_picinfo.pic_h == 0 ||
> +		ctx->last_decoded_picinfo.buf_w == 0 ||
> +		ctx->last_decoded_picinfo.buf_h == 0)
> +		return false;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> +				 ctx->last_decoded_picinfo.pic_h,
> +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				 ctx->last_decoded_picinfo.buf_w,
> +				 ctx->last_decoded_picinfo.buf_h);
> +
> +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +		if (dpbsize == 0)
> +			dpbsize = 1;
> +		ctx->dpb_count = dpbsize;
> +		return true;
> +	}
> +
> +	return false;
> +}
> +
> +void mtk_vdec_worker(struct work_struct *work)
> +{
> +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> +				    decode_work);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	struct vb2_buffer *src_buf, *dst_buf;
> +	struct mtk_vcodec_mem buf;
> +	struct vdec_fb *pfb;
> +	bool res_chg = false;
> +	int ret;
> +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> +
> +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +	if (src_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> +	if (dst_buf == NULL) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> +		return;
> +	}
> +
> +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	buf.va = vb2_plane_vaddr(src_buf, 0);
> +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> +	if (!buf.va) {
> +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> +			       ctx->idx, src_buf->index);
> +		return;
> +	}
> +
> +	pfb = &dst_buf_info->frame_buffer;
> +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> +
> +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> +	pfb->status = 0;
> +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> +	mtk_v4l2_debug(3,
> +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> +			 ctx->idx, buf.va,
> +			 (u64)buf.dma_addr,
> +			 buf.size, src_buf);
> +
> +	mtk_v4l2_debug(3,
> +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> +			dst_buf->index, pfb,
> +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> +
> +	if (src_buf_info->lastframe == true) {
> +		/* update src buf status */
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		src_buf_info->lastframe = false;
> +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> +
> +		/* update dst buf status */
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		dst_buf_info->used = false;
> +
> +		clean_display_buffer(ctx);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> +		clean_free_buffer(ctx);
> +	} else {
> +
> +		dst_buf_info->vb.timestamp
> +					= src_buf_info->vb.timestamp;
> +		dst_buf_info->vb.timecode
> +					= src_buf_info->vb.timecode;
> +		mutex_lock(&dst_buf_info->lock);
> +		dst_buf_info->used = true;
> +		mutex_unlock(&dst_buf_info->lock);
> +		src_buf_info->used = true;
> +
> +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> +
> +		if (ret) {
> +			mtk_v4l2_err(
> +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> +				ctx->idx,
> +				src_buf->index,
> +				src_buf_info->lastframe,
> +				buf.size,
> +				src_buf_info->vb.timestamp.tv_sec,
> +				src_buf_info->vb.timestamp.tv_usec,
> +				dst_buf->index,
> +				ret, res_chg);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +					VB2_BUF_STATE_ERROR);
> +		} else if (res_chg == false) {
> +			/* we only return src buffer with VB2_BUF_STATE_DONE
> +			  * when decode success without resolution change
> +			  */
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +			v4l2_m2m_buf_done(&src_buf_info->vb,
> +							VB2_BUF_STATE_DONE);
> +		}
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		clean_display_buffer(ctx);
> +		clean_free_buffer(ctx);
> +
> +		if ((ret == 0) && (res_chg == true)) {
> +			mtk_vdec_pic_info_update(ctx);
> +			/*
> +			  * On encountering a resolution change in the stream.
> +			  * The driver must first process and decode all
> +			  * remaining buffers from before the resolution change
> +			  * point, so call flush decode here
> +			  */
> +			mtk_vdec_flush_decoder(ctx);
> +			/*
> +			  * After all buffers containing decoded frames from
> +			  * before the resolution change point ready to be
> +			  * dequeued on the CAPTURE queue, the driver sends a
> +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> +			  */
> +			mtk_vdec_queue_res_chg_event(ctx);
> +		}
> +	}
> +
> +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> +
> +}
> +
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_unlock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> +{
> +	mutex_lock(&ctx->dev->dec_mutex);
> +	return 0;
> +}
> +
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	int ret = 0;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	src_vq->drv_priv	= ctx;
> +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	src_vq->ops		= &mtk_vdec_vb2_ops;
> +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	src_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(src_vq);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> +		return ret;
> +	}
> +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> +	dst_vq->drv_priv	= ctx;
> +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> +	dst_vq->lock = &ctx->dev->dev_mutex;
> +
> +	ret = vb2_queue_init(dst_vq);
> +	if (ret) {
> +		vb2_queue_release(src_vq);
> +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> +	}
> +
> +	return ret;
> +}
> +
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> +{
> +	vdec_if_deinit(ctx);
> +	ctx->state = MTK_STATE_FREE;
> +}
> +
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct mtk_q_data *q_data;
> +
> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> +
> +
> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> +	q_data->visible_width = DFT_CFG_WIDTH;
> +	q_data->visible_height = DFT_CFG_HEIGHT;
> +	q_data->coded_width = DFT_CFG_WIDTH;
> +	q_data->coded_height = DFT_CFG_HEIGHT;
> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> +	q_data->field = V4L2_FIELD_NONE;
> +
> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> +
> +	v4l_bound_align_image(&q_data->coded_width,
> +					MTK_VDEC_MIN_W,
> +					MTK_VDEC_MAX_W, 4,
> +					&q_data->coded_height,
> +					MTK_VDEC_MIN_H,
> +					MTK_VDEC_MAX_H, 5, 6);
> +
> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> +	q_data->bytesperline[0] = q_data->coded_width;
> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> +	q_data->bytesperline[1] = q_data->coded_width;
> +
> +}
> +
> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> +				enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +
> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> +				 enum v4l2_buf_type type)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> +}
> +
> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> +			       struct v4l2_requestbuffers *reqbufs)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	int ret;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> +			 reqbufs->type, reqbufs->count);
> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> +
> +	return ret;
> +}

Please use the v4l2_m2m_ioctl_* helper functions were applicable.

> +
> +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> +				struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> +			 buf->type, buf->index);
> +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> +			    struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct vb2_queue *vq;
> +	struct vb2_buffer *vb;
> +	struct mtk_video_buf *mtkbuf;
> +	struct vb2_v4l2_buffer	*vb2_v4l2;
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> +	vb = vq->bufs[buf->index];
> +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +
> +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    (buf->m.planes[0].bytesused == 0)) {
> +		mtkbuf->lastframe = true;
> +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> +			 ctx->idx, buf->type, buf->index,
> +			 mtkbuf->lastframe, buf->bytesused,
> +			 buf->m.planes[0].bytesused, buf->length,
> +			 vb);
> +	}
> +
> +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> +			     struct v4l2_buffer *buf)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state == MTK_STATE_ABORT) {
> +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> +					ctx->idx);
> +		return -EIO;
> +	}
> +
> +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> +}
> +
> +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> +			      struct v4l2_exportbuffer *eb)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> +}
> +
> +static int vidioc_vdec_querycap(struct file *file, void *priv,
> +				struct v4l2_capability *cap)
> +{
> +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;

See my comment about the new device_caps field in struct video_device from my
review of the encoder driver.

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_event_unsubscribe(fh, sub);
> +	default:
> +		return -EINVAL;
> +	}
> +}

No need for this function, just use v4l2_event_unsubscribe.

> +
> +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> +				     const struct v4l2_event_subscription *sub)
> +{
> +	switch (sub->type) {
> +	case V4L2_EVENT_EOS:
> +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> +	case V4L2_EVENT_SOURCE_CHANGE:
> +		return v4l2_src_change_event_subscribe(fh, sub);
> +	default:
> +		return -EINVAL;

Can't you just call v4l2_ctrl_subscribe_event() in the default case?

> +	}
> +}
> +
> +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> +				struct v4l2_format *f)
> +{
> +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> +	struct mtk_video_fmt *fmt;
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (!fmt) {
> +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> +		fmt = mtk_vdec_find_format(f);
> +	}
> +
> +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> +		mtk_v4l2_err("sizeimage of output format must be given");
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> +			 struct v4l2_crop *cr)

Don't use g_crop, use g_selection instead. The selection API is a superset
of the old crop API.

> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (ctx->state < MTK_STATE_HEADER)
> +		return -EINVAL;
> +
> +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> +
> +		if (vdec_if_get_param(ctx,
> +					   GET_PARAM_CROP_INFO,
> +					   cr)) {
> +			mtk_v4l2_debug(2,
> +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> +					 ctx->idx);
> +			cr->c.left = 0;
> +			cr->c.top = 0;
> +			cr->c.width = ctx->picinfo.buf_w;
> +			cr->c.height = ctx->picinfo.buf_h;
> +		}
> +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> +				 cr->c.left, cr->c.top, cr->c.width,
> +				 cr->c.height);
> +	} else {
> +		cr->c.left = 0;
> +		cr->c.top = 0;
> +		cr->c.width = ctx->picinfo.pic_w;
> +		cr->c.height = ctx->picinfo.pic_h;
> +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> +				 ctx->picinfo.buf_h);
> +	}
> +	return 0;
> +}
> +
> +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +	int ret;
> +	struct mtk_video_fmt *fmt;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	fmt = mtk_vdec_find_format(f);
> +	if (fmt == NULL) {
> +		mtk_v4l2_err("mtk_venc_find_format fail");
> +		return ret;
> +	}
> +
> +	pix_mp = &f->fmt.pix_mp;
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> +		mtk_v4l2_err("out_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> +		ret = -EBUSY;
> +	}
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq)
> +		return -EINVAL;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +	if (!q_data)
> +		return -EINVAL;
> +
> +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		q_data->fmt = fmt;
> +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> +		pix_mp->plane_fmt[0].bytesperline = 0;
> +		pix_mp->width = 0;
> +		pix_mp->height = 0;
> +
> +		if (ctx->state == MTK_STATE_FREE) {
> +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> +			if (ret) {
> +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> +					       ctx->idx, ret);
> +				return -EINVAL;
> +			}
> +			ctx->state = MTK_STATE_INIT;
> +		}
> +	} else {
> +		q_data->fmt = fmt;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vidioc_enum_framesizes(struct file *file, void *priv,
> +				struct v4l2_frmsizeenum *fsize)
> +{
> +	int i = 0;
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +
> +	if (fsize->index != 0)
> +		return -EINVAL;
> +
> +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> +			continue;
> +
> +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> +		if (!(ctx->dev->dec_capability &
> +				VCODEC_CAPABILITY_4K_DISABLED)) {
> +			mtk_v4l2_debug(1, "4K is enabled");
> +			fsize->stepwise.max_width =
> +					VCODEC_DEC_4K_CODED_WIDTH;
> +			fsize->stepwise.max_height =
> +					VCODEC_DEC_4K_CODED_HEIGHT;
> +		}
> +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> +					ctx->dev->dec_capability,
> +					fsize->stepwise.min_width,
> +					fsize->stepwise.max_width,
> +					fsize->stepwise.step_width,
> +					fsize->stepwise.min_height,
> +					fsize->stepwise.max_height,
> +					fsize->stepwise.step_height);
> +		return 0;
> +	}
> +
> +	return -EINVAL;
> +
> +}
> +
> +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> +{
> +	struct mtk_video_fmt *fmt;
> +	int i, j = 0;
> +
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> +			continue;
> +		if (!output_queue &&
> +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> +			continue;
> +
> +		if (j == f->index)
> +			break;
> +		++j;
> +	}
> +
> +	if (i == NUM_FORMATS)
> +		return -EINVAL;
> +
> +	fmt = &mtk_video_formats[i];
> +	f->pixelformat = fmt->fourcc;
> +	f->flags = 0;

No need.

> +	memset(f->reserved, 0, sizeof(f->reserved));

No need. The core will automatically set flags and reserved.

> +
> +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;

No need for this, this is automatically done for you.

Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
in drivers/media/v4l2-core/v4l2-ioctl.c

> +
> +	return 0;
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, false);
> +}
> +
> +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> +					       struct v4l2_fmtdesc *f)
> +{
> +	return vidioc_enum_fmt(f, true);
> +}
> +
> +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> +			     struct v4l2_format *f)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> +	struct vb2_queue *vq;
> +	struct mtk_q_data *q_data;
> +
> +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> +	if (!vq) {
> +		mtk_v4l2_err("no vb2 queue for type=%d",
> +					   f->type);
> +		return -EINVAL;
> +	}
> +
> +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> +
> +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> +	    (ctx->state >= MTK_STATE_HEADER)) {
> +		/* previous decode might have resolution change that make
> +		 * picinfo changed update context'x picinfo here to make sure
> +		 * we know what report to user space
> +		 */
> +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> +			sizeof(struct vdec_pic_info));
> +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> +					ctx->picinfo.y_len_sz;
> +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> +					ctx->picinfo.c_len_sz;
> +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> +		q_data->coded_width = ctx->picinfo.buf_w;
> +		q_data->coded_height = ctx->picinfo.buf_h;
> +
> +		/*
> +		 * Width and height are set to the dimensions
> +		 * of the movie, the buffer is bigger and
> +		 * further processing stages should crop to this
> +		 * rectangle.
> +		 */
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +
> +		/*
> +		 * Set pixelformat to the format in which mt vcodec
> +		 * outputs the decoded frame
> +		 */
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		/*
> +		 * This is run on OUTPUT
> +		 * The buffer contains compressed image
> +		 * so width and height have no meaning.
> +		 * Assign value here to pass v4l2-compliance test
> +		 */
> +		pix_mp->width = q_data->visible_width;
> +		pix_mp->height = q_data->visible_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +	} else {
> +		pix_mp->width = q_data->coded_width;
> +		pix_mp->height = q_data->coded_height;
> +		pix_mp->field = V4L2_FIELD_NONE;
> +		pix_mp->num_planes = q_data->fmt->num_planes;
> +		pix_mp->pixelformat = q_data->fmt->fourcc;
> +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> +
> +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> +				ctx->idx, ctx->state, f->type);
> +		return -EAGAIN;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> +				   const void *parg,

Just like the encoder driver this patch needs to be rebased. The latest code no
longer has the parg argument.

> +				   unsigned int *nbuffers,
> +				   unsigned int *nplanes,
> +				   unsigned int sizes[], void *alloc_ctxs[])
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> +	struct mtk_q_data *q_data;
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> +
> +	switch (vq->type) {
> +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> +		*nplanes = 1;
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		break;
> +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> +		if (ctx->state < MTK_STATE_HEADER)
> +			return -EINVAL;
> +
> +		/*
> +		 * Output plane count is 2 - one for Y and one for CbCr
> +		 */
> +		*nplanes = 2;
> +
> +		mtk_v4l2_debug(1,
> +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> +
> +		sizes[0] = q_data->sizeimage[0];
> +		sizes[1] = q_data->sizeimage[1];
> +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	mtk_v4l2_debug(1,
> +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> +			ctx->idx, vq->type, *nplanes, *nbuffers,
> +			sizes[0], sizes[1]);
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct mtk_q_data *q_data;
> +	int i;
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> +			 ctx->idx, vb->vb2_queue->type, vb->index);
> +
> +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> +
> +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> +				       i, vb2_plane_size(vb, i),
> +				       q_data->sizeimage[i]);
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> +			ctx->idx, vb->vb2_queue->type,
> +			vb->index, vb);
> +	/*
> +	 * check if this buffer is ready to be used after decode
> +	 */
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		struct vb2_buffer *src_buf;
> +		struct mtk_vcodec_mem buf;
> +		bool res_chg = false;
> +		int ret = 0;
> +		int dpbsize = 1;
> +
> +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> +
> +		if (ctx->state == MTK_STATE_INIT) {
> +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> +			if (!src_buf) {
> +				mtk_v4l2_err("No src buffer");
> +				return;
> +			}
> +
> +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> +							src_buf,
> +							0);
> +			buf.size = (size_t)src_buf->planes[0].bytesused;
> +			mtk_v4l2_debug(2,
> +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> +					ctx->idx,
> +					src_buf->index,
> +					buf.va, (u64)buf.dma_addr, buf.size);
> +
> +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> +			if (ret) {
> +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +						VB2_BUF_STATE_DONE);
> +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> +						ctx->idx,
> +						src_buf->index,
> +						buf.size, ret);
> +				return;
> +			}
> +
> +			if (vdec_if_get_param(ctx,
> +						   GET_PARAM_PIC_INFO,
> +						   &ctx->picinfo)) {
> +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> +						ctx->idx);
> +				return;
> +			}
> +
> +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> +				sizeof(struct vdec_pic_info));
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> +						ctx->picinfo.y_bs_sz +
> +						ctx->picinfo.y_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> +							ctx->picinfo.buf_w;
> +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> +						ctx->picinfo.c_bs_sz +
> +						ctx->picinfo.c_len_sz;
> +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> +							ctx->picinfo.buf_w;
> +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> +				ctx->idx,
> +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> +
> +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> +			if (dpbsize == 0) {
> +				mtk_v4l2_debug(2,
> +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> +						ctx->idx, ret);
> +				dpbsize = 1;
> +			}
> +			ctx->dpb_count = dpbsize;
> +			ctx->state = MTK_STATE_HEADER;
> +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> +							ctx->dpb_count);
> +		} else {
> +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> +					 ctx->idx, ctx->state);
> +		}
> +
> +	} else {
> +		mutex_lock(&buf->lock);
> +		if (buf->used == false) {
> +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> +					to_vb2_v4l2_buffer(vb));
> +			buf->queued_in_vb2 = true;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		} else {
> +			buf->queued_in_vb2 = false;
> +			buf->queued_in_v4l2 = true;
> +			buf->ready_to_display = false;
> +		}
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> +{
> +	if (vb->vb2_queue->type ==
> +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> +		mutex_lock(&buf->lock);
> +		buf->queued_in_v4l2 = false;
> +		buf->queued_in_vb2 = false;
> +		mutex_unlock(&buf->lock);
> +	}
> +}
> +
> +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> +{
> +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> +					struct vb2_v4l2_buffer, vb2_buf);
> +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		buf->used = false;
> +		buf->ready_to_display = false;
> +		buf->nonrealdisplay = false;
> +		buf->queued_in_v4l2 = false;
> +		mutex_init(&buf->lock);
> +	} else {
> +		buf->lastframe = false;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> +{
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	if (ctx->state == MTK_STATE_FLUSH)
> +		ctx->state = MTK_STATE_HEADER;
> +
> +	return 0;
> +}
> +
> +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> +{
> +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> +
> +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> +
> +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		while (src_buf) {
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> +					VB2_BUF_STATE_ERROR);
> +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> +		}
> +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> +		int i;
> +		struct vb2_v4l2_buffer *vb2_v4l2;
> +		struct mtk_video_buf *buf;
> +
> +		if (ctx->state >= MTK_STATE_HEADER)
> +			mtk_vdec_flush_decoder(ctx);
> +
> +		ctx->state = MTK_STATE_FLUSH;
> +
> +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		while (dst_buf) {
> +			vb2_set_plane_payload(dst_buf, 0, 0);
> +			vb2_set_plane_payload(dst_buf, 1, 0);
> +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> +				VB2_BUF_STATE_ERROR);
> +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> +		}
> +
> +		for (i = 0; i < q->num_buffers; ++i) {
> +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> +				vb2_v4l2 = container_of(q->bufs[i],
> +					struct vb2_v4l2_buffer, vb2_buf);
> +				buf = container_of(vb2_v4l2,
> +					struct mtk_video_buf, vb);
> +
> +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> +						ctx->idx, i, q->type,
> +						buf->queued_in_vb2,
> +						buf->queued_in_v4l2,
> +						(int)q->bufs[i]->state);
> +				v4l2_m2m_buf_done(vb2_v4l2,
> +						VB2_BUF_STATE_ERROR);
> +			}
> +		}
> +	}
> +}
> +
> +static void m2mops_vdec_device_run(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +
> +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> +}
> +
> +static int m2mops_vdec_job_ready(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +
> +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> +		return 0;
> +
> +	if (ctx->state == MTK_STATE_ABORT)
> +		return 0;
> +
> +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> +		return 0;
> +
> +	if (ctx->state != MTK_STATE_HEADER)
> +		return 0;
> +
> +	return 1;
> +}
> +
> +static void m2mops_vdec_job_abort(void *priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = priv;
> +
> +	ctx->state = MTK_STATE_ABORT;
> +}
> +
> +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> +		if (ctx->state >= MTK_STATE_HEADER) {
> +			ctrl->val = ctx->dpb_count;
> +		} else {
> +			mtk_v4l2_err("Seqinfo not ready");
> +			ctrl->val = 1;
> +			return -EAGAIN;
> +		}
> +		break;
> +	default:
> +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> +};
> +
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> +{
> +	struct v4l2_ctrl *ctrl;
> +
> +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> +
> +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> +					  &mtk_vcodec_dec_ctrl_ops,
> +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> +					  1, 32, 1, 1);
> +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> +
> +	if (ctx->ctrl_hdl.error) {
> +		mtk_v4l2_err("Adding control failed %d",
> +					ctx->ctrl_hdl.error);
> +		return ctx->ctrl_hdl.error;
> +	}
> +
> +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> +	return 0;
> +}
> +
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> +{
> +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> +}
> +
> +static void m2mops_vdec_lock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_lock(&ctx->dev->dev_mutex);
> +}
> +
> +static void m2mops_vdec_unlock(void *m2m_priv)
> +{
> +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> +
> +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> +	mutex_unlock(&ctx->dev->dev_mutex);
> +}
> +
> +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> +	.device_run	= m2mops_vdec_device_run,
> +	.job_ready	= m2mops_vdec_job_ready,
> +	.job_abort	= m2mops_vdec_job_abort,
> +	.lock	= m2mops_vdec_lock,
> +	.unlock	= m2mops_vdec_unlock,
> +};
> +
> +const struct vb2_ops mtk_vdec_vb2_ops = {
> +	.queue_setup	= vb2ops_vdec_queue_setup,
> +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> +	.buf_queue	= vb2ops_vdec_buf_queue,
> +	.wait_prepare	= vb2_ops_wait_prepare,
> +	.wait_finish	= vb2_ops_wait_finish,
> +	.buf_init	= vb2ops_vdec_buf_init,
> +	.buf_finish	= vb2ops_vdec_buf_finish,
> +	.start_streaming	= vb2ops_vdec_start_streaming,
> +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> +};
> +
> +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> +	.vidioc_streamon	= vidioc_vdec_streamon,
> +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> +
> +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> +
> +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> +
> +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> +
> +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> +
> +	.vidioc_querycap	= vidioc_vdec_querycap,
> +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> +};
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> new file mode 100644
> index 0000000..cb5cb42
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> @@ -0,0 +1,81 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#ifndef _MTK_VCODEC_DEC_H_
> +#define _MTK_VCODEC_DEC_H_
> +
> +#include <media/videobuf2-core.h>
> +#include <media/videobuf2-v4l2.h>
> +
> +/**
> + * struct vdec_fb  - decoder frame buffer
> + * @base_y	: Y plane memory info
> + * @base_c	: C plane memory info
> + * @status      : frame buffer status (vdec_fb_status)
> + */
> +struct vdec_fb {
> +	struct mtk_vcodec_mem	base_y;
> +	struct mtk_vcodec_mem	base_c;
> +	unsigned int	status;
> +};
> +
> +/**
> + * struct mtk_video_buf - Private data related to each VB2 buffer.
> + * @b:			VB2 buffer
> + * @list:			link list
> + * @used:		Output buffer contain decoded frame data
> + * @ready_to_display:	Output buffer not display yet
> + * @nonrealdisplay:	Output buffer is not display frame
> + * @queued_in_vb2:	Output buffer is queue in vb2
> + * @queued_in_v4l2:	Output buffer is in v4l2
> + * @lastframe:		Intput buffer is last buffer - EOS
> + * @frame_buffer:		Decode status of output buffer
> + * @lock:			V4L2 and decode thread should get mutex
> + *			before r/w info in mtk_video_buf
> + */
> +struct mtk_video_buf {
> +	struct vb2_v4l2_buffer	vb;
> +	struct list_head	list;
> +
> +	bool	used;
> +	bool	ready_to_display;
> +	bool	nonrealdisplay;
> +	bool	queued_in_vb2;
> +	bool	queued_in_v4l2;
> +	bool	lastframe;
> +	struct vdec_fb	frame_buffer;
> +	struct mutex	lock;
> +};
> +
> +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> +
> +
> +/*
> + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> + * get/release lock before/after access decoder hw.
> + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> + * to ctx instance that get lock
> + */
> +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> +			   struct vb2_queue *dst_vq);
> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> +
> +#endif /* _MTK_VCODEC_DEC_H_ */
> diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> new file mode 100644
> index 0000000..34e3217
> --- /dev/null
> +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> @@ -0,0 +1,469 @@
> +/*
> +* Copyright (c) 2016 MediaTek Inc.
> +* Author: PC Chen <pc.chen@mediatek.com>
> +*         Tiffany Lin <tiffany.lin@mediatek.com>
> +*
> +* This program is free software; you can redistribute it and/or modify
> +* it under the terms of the GNU General Public License version 2 as
> +* published by the Free Software Foundation.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of_device.h>
> +#include <linux/of.h>
> +#include <media/v4l2-event.h>
> +#include <media/v4l2-mem2mem.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#include "mtk_vcodec_drv.h"
> +#include "mtk_vcodec_dec.h"
> +#include "mtk_vcodec_dec_pm.h"
> +#include "mtk_vcodec_intr.h"
> +#include "mtk_vcodec_util.h"
> +#include "vdec_drv_if.h"
> +#include "mtk_vpu.h"
> +
> +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> +
> +#define VDEC_HW_ACTIVE	0x10
> +#define VDEC_IRQ_CFG	0x11
> +#define VDEC_IRQ_CLR	0x10
> +#define VDEC_IRQ_CFG_REG	0xa4
> +
> +
> +/* Wake up context wait_queue */
> +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> +{
> +	ctx->int_cond = 1;
> +	ctx->int_type = reason;
> +	wake_up_interruptible(&ctx->queue);
> +}
> +
> +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	unsigned long flags;
> +	struct mtk_vcodec_ctx *ctx;
> +	unsigned int cg_status = 0;
> +	unsigned int dec_done_status = 0;
> +
> +	spin_lock_irqsave(&dev->irqlock, flags);
> +	ctx = dev->curr_ctx;
> +	spin_unlock_irqrestore(&dev->irqlock, flags);
> +
> +	cg_status = readl(dev->reg_base[0] + (0));
> +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> +			       cg_status);
> +		return IRQ_HANDLED;
> +	}
> +
> +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	ctx->irq_status = dec_done_status;
> +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> +		return IRQ_HANDLED;
> +
> +	/* clear interrupt */
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> +
> +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> +
> +	mtk_v4l2_debug(3,
> +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> +			ctx->idx, dec_done_status);
> +
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int fops_vcodec_open(struct file *file)
> +{
> +	struct video_device *vfd = video_devdata(file);
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = NULL;
> +	int ret = 0;
> +
> +	if (dev->instance_mask == ~0UL) {
> +		mtk_v4l2_err("Too many open contexts");
> +		ret = -EBUSY;
> +		goto err_alloc;
> +	}
> +
> +	mutex_lock(&dev->dev_mutex);
> +
> +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> +	if (!ctx) {
> +		ret = -ENOMEM;
> +		goto err_alloc;
> +	}
> +
> +	ctx->idx = ffz(dev->instance_mask);
> +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> +	file->private_data = &ctx->fh;
> +	v4l2_fh_add(&ctx->fh);
> +	INIT_LIST_HEAD(&ctx->list);
> +	ctx->dev = dev;
> +	init_waitqueue_head(&ctx->queue);
> +
> +	if (vfd == dev->vfd_dec) {
> +		ctx->type = MTK_INST_DECODER;
> +		ret = mtk_vdec_ctrls_setup(ctx);
> +		if (ret) {
> +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> +			goto err_ctrls_setup;
> +		}
> +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> +						 &m2mctx_vdec_queue_init);
> +		if (IS_ERR(ctx->m2m_ctx)) {
> +			ret = PTR_ERR(ctx->m2m_ctx);
> +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> +				       ret);
> +			goto err_ctx_init;
> +		}
> +		mtk_vcodec_dec_set_default_params(ctx);
> +	} else {
> +		mtk_v4l2_err("Invalid vfd !");
> +		ret = -ENOENT;
> +		goto err_ctx_init;
> +	}
> +
> +	if (v4l2_fh_is_singular(&ctx->fh)) {
> +		mtk_vcodec_dec_pw_on(&dev->pm);
> +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> +		if (ret < 0) {
> +			mtk_v4l2_err("vpu_load_firmware failed!");
> +			goto err_load_fw;
> +		}
> +
> +		dev->dec_capability =
> +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> +		mtk_v4l2_debug(0, "decoder capability %x",
> +			       dev->dec_capability);
> +	}
> +
> +	set_bit(ctx->idx, &dev->instance_mask);
> +	dev->num_instances++;
> +	list_add(&ctx->list, &dev->ctx_list);
> +	mutex_unlock(&dev->dev_mutex);
> +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> +					ctx->idx);
> +
> +	return ret;
> +
> +	/* Deinit when failure occurred */
> +err_load_fw:
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +err_ctx_init:
> +	mtk_vdec_ctrls_free(ctx);
> +err_ctrls_setup:
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +err_alloc:
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_release(struct file *file)
> +{
> +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> +
> +	mutex_lock(&dev->dev_mutex);
> +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> +	mtk_vcodec_vdec_release(ctx);
> +	mtk_vdec_ctrls_free(ctx);
> +	v4l2_fh_del(&ctx->fh);
> +	v4l2_fh_exit(&ctx->fh);
> +	list_del_init(&ctx->list);
> +	dev->num_instances--;
> +	if (dev->num_instances == 0)
> +		mtk_vcodec_dec_pw_off(&dev->pm);
> +	clear_bit(ctx->idx, &dev->instance_mask);
> +	devm_kfree(&dev->plat_dev->dev, ctx);
> +	mutex_unlock(&dev->dev_mutex);
> +	return 0;
> +}
> +
> +static unsigned int fops_vcodec_poll(struct file *file,
> +				     struct poll_table_struct *wait)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +	struct mtk_vcodec_dev *dev = ctx->dev;
> +	int ret;
> +
> +	mutex_lock(&dev->dev_mutex);
> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> +	mutex_unlock(&dev->dev_mutex);
> +
> +	return ret;
> +}
> +
> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> +{
> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> +
> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> +}
> +
> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> +	.owner				= THIS_MODULE,
> +	.open				= fops_vcodec_open,
> +	.release			= fops_vcodec_release,
> +	.poll				= fops_vcodec_poll,
> +	.unlocked_ioctl			= video_ioctl2,
> +	.mmap				= fops_vcodec_mmap,

You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.

> +};
> +
> +static void mtk_vcodec_dec_reset_handler(void *priv)
> +{
> +	struct mtk_vcodec_dev *dev = priv;
> +	struct mtk_vcodec_ctx *ctx;
> +
> +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> +
> +	mutex_lock(&dev->dev_mutex);
> +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> +		ctx->state = MTK_STATE_ABORT;
> +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> +					   ctx->idx);
> +	}
> +	mutex_unlock(&dev->dev_mutex);
> +}
> +
> +static int mtk_vcodec_probe(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev;
> +	struct video_device *vfd_dec;
> +	struct resource *res;
> +	int i, ret;
> +	int reg_base_num;
> +
> +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> +	if (!dev)
> +		return -ENOMEM;
> +
> +	INIT_LIST_HEAD(&dev->ctx_list);
> +	dev->plat_dev = pdev;
> +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> +	if (dev->vpu_plat_dev == NULL) {
> +		mtk_v4l2_err("[VPU] vpu device in not ready");
> +		return -EPROBE_DEFER;
> +	}
> +
> +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> +				dev, VPU_RST_DEC);
> +
> +	ret = mtk_vcodec_init_dec_pm(dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> +		return ret;
> +	}
> +
> +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> +	for (i = 0; i < reg_base_num; i++) {
> +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> +		if (res == NULL) {
> +			dev_err(&pdev->dev, "get memory resource failed.");
> +			ret = -ENXIO;
> +			goto err_res;
> +		}
> +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> +		if (IS_ERR(dev->reg_base[i])) {
> +			dev_err(&pdev->dev,
> +				"devm_ioremap_resource %d failed.", i);
> +			ret = PTR_ERR(dev->reg_base);
> +			goto err_res;
> +		}
> +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> +	}
> +
> +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> +	if (res == NULL) {
> +		dev_err(&pdev->dev, "failed to get irq resource");
> +		ret = -ENOENT;
> +		goto err_res;
> +	}
> +
> +	dev->dec_irq = platform_get_irq(pdev, 0);
> +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> +			dev->dec_irq,
> +			ret);
> +		ret = -EINVAL;
> +		goto err_res;
> +	}
> +
> +	disable_irq(dev->dec_irq);
> +	mutex_init(&dev->dev_mutex);
> +	mutex_init(&dev->dec_mutex);
> +	spin_lock_init(&dev->irqlock);
> +
> +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> +		 "[MTK_V4L2]");
> +
> +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> +	if (ret) {
> +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> +		return ret;
> +	}
> +
> +	init_waitqueue_head(&dev->queue);
> +
> +	vfd_dec = video_device_alloc();
> +	if (!vfd_dec) {
> +		mtk_v4l2_err("Failed to allocate video device");
> +		ret = -ENOMEM;
> +		goto err_dec_alloc;
> +	}
> +	vfd_dec->fops		= &mtk_vcodec_fops;
> +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> +	vfd_dec->release	= video_device_release;
> +	vfd_dec->lock		= &dev->dev_mutex;
> +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> +
> +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> +		 MTK_VCODEC_DEC_NAME);
> +	video_set_drvdata(vfd_dec, dev);
> +	dev->vfd_dec = vfd_dec;
> +	platform_set_drvdata(pdev, dev);
> +
> +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> +	if (IS_ERR(dev->alloc_ctx)) {
> +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> +		ret = PTR_ERR(dev->alloc_ctx);
> +		goto err_vb2_ctx_init;
> +	}
> +
> +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> +	if (IS_ERR(dev->m2m_dev_dec)) {
> +		mtk_v4l2_err("Failed to init mem2mem dec device");
> +		ret = PTR_ERR(dev->m2m_dev_dec);
> +		goto err_dec_mem_init;
> +	}
> +
> +	dev->decode_workqueue =
> +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> +	if (!dev->decode_workqueue) {
> +		mtk_v4l2_err("Failed to create decode workqueue");
> +		ret = -EINVAL;
> +		goto err_event_workq;
> +	}
> +
> +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> +	if (ret) {
> +		mtk_v4l2_err("Failed to register video device");
> +		goto err_dec_reg;
> +	}
> +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> +			 vfd_dec->num);
> +
> +	return 0;
> +
> +err_dec_reg:
> +	destroy_workqueue(dev->decode_workqueue);
> +err_event_workq:
> +	v4l2_m2m_release(dev->m2m_dev_dec);
> +err_dec_mem_init:
> +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +err_vb2_ctx_init:
> +	video_unregister_device(vfd_dec);
> +err_dec_alloc:
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +err_res:
> +	mtk_vcodec_release_dec_pm(dev);
> +	return ret;
> +}
> +
> +static const struct of_device_id mtk_vcodec_match[] = {
> +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> +	{},
> +};
> +
> +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> +
> +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> +{
> +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug_enter();
> +	flush_workqueue(dev->decode_workqueue);
> +	destroy_workqueue(dev->decode_workqueue);
> +	if (dev->m2m_dev_dec)
> +		v4l2_m2m_release(dev->m2m_dev_dec);
> +	if (dev->alloc_ctx)
> +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> +
> +	if (dev->vfd_dec)
> +		video_unregister_device(dev->vfd_dec);
> +
> +	v4l2_device_unregister(&dev->v4l2_dev);
> +	mtk_vcodec_release_dec_pm(dev);
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> +
> +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> +
> +	if (m_dev->num_instances == 0)
> +		return 0;
> +
> +	return 0;
> +}
> +
> +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> +			   mtk_vcodec_vdec_pm_runtime_resume,
> +			   NULL)
> +};
> +
> +static struct platform_driver mtk_vcodec_dec_driver = {
> +	.probe	= mtk_vcodec_probe,
> +	.remove	= mtk_vcodec_dec_remove,
> +	.driver	= {
> +		.name	= MTK_VCODEC_DEC_NAME,
> +		.owner	= THIS_MODULE,
> +		.of_match_table = mtk_vcodec_match,
> +		.pm = &mtk_vcodec_vdec_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(mtk_vcodec_dec_driver);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");

Regards,

	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  2016-04-15 14:27         ` Hans Verkuil
  (?)
@ 2016-04-18  4:00           ` tiffany lin
  -1 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  4:00 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Hi Hans,

On Fri, 2016-04-15 at 16:27 +0200, Hans Verkuil wrote:
> On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> > Add v4l2 layer decoder driver for MT8173
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> > ---
> >  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
> >  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
> >  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
> >  11 files changed, 2596 insertions(+), 20 deletions(-)
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> > 
> > diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> > index dc5cb00..4c8ed2f 100644
> > --- a/drivers/media/platform/mtk-vcodec/Makefile
> > +++ b/drivers/media/platform/mtk-vcodec/Makefile
> > @@ -1,7 +1,13 @@
> >  
> >  
> > -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> > -
> > +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> > +				       mtk-vcodec-enc.o \
> > +				       mtk-vcodec-common.o
> > +
> > +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> > +		vdec_drv_if.o \
> > +		mtk_vcodec_dec.o \
> > +		mtk_vcodec_dec_pm.o \
> >  
> >  
> >  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > new file mode 100644
> > index 0000000..0499413
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > @@ -0,0 +1,1429 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +
> > +static struct mtk_video_fmt mtk_video_formats[] = {
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_H264,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP8,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_MT21,
> > +		.type = MTK_FMT_FRAME,
> > +		.num_planes = 2,
> > +	},
> > +};
> > +#define OUT_FMT_IDX	0
> > +#define CAP_FMT_IDX	3
> > +
> > +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> > +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> > +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> > +
> > +#define MTK_VDEC_MIN_W	64U
> > +#define MTK_VDEC_MIN_H	64U
> > +#define MTK_VDEC_MAX_W	2048U
> > +#define MTK_VDEC_MAX_H	1088U
> > +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> > +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> > +
> > +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_H264,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_VP8,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +};
> > +
> > +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> > +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> > +
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops;
> > +
> > +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	unsigned int k;
> > +
> > +	for (k = 0; k < NUM_FORMATS; k++) {
> > +		fmt = &mtk_video_formats[k];
> > +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> > +			return fmt;
> > +	}
> > +
> > +	return NULL;
> > +}
> > +
> > +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> > +					      enum v4l2_buf_type type)
> > +{
> > +	if (V4L2_TYPE_IS_OUTPUT(type))
> > +		return &ctx->q_data[MTK_Q_DATA_SRC];
> > +
> > +	return &ctx->q_data[MTK_Q_DATA_DST];
> > +}
> > +
> > +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vdec_fb *disp_frame_buffer;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_DISP_FRAME_BUFFER,
> > +				   &disp_frame_buffer)) {
> > +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> > +			     ctx->idx);
> > +		return NULL;
> > +	}
> > +
> > +	if (disp_frame_buffer == NULL) {
> > +		mtk_v4l2_debug(3, "No display frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = disp_frame_buffer;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> > +							ctx->picinfo.y_bs_sz);
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> > +							ctx->picinfo.c_bs_sz);
> > +
> > +		dstbuf->ready_to_display = true;
> > +		dstbuf->nonrealdisplay = false;
> > +		mtk_v4l2_debug(2,
> > +			       "[%d]status=%x queue idx=%d to done_list %d",
> > +			       ctx->idx, frame_buffer->status,
> > +			       dstbuf->vb.vb2_buf.index,
> > +			       dstbuf->queued_in_vb2);
> > +
> > +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> > +		ctx->decoded_frame_cnt++;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	void *tmp_frame_addr;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_FREE_FRAME_BUFFER,
> > +				   &tmp_frame_addr)) {
> > +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> > +		return NULL;
> > +	}
> > +	if (tmp_frame_addr == NULL) {
> > +		mtk_v4l2_debug(3, " No free frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> > +			 ctx->idx, tmp_frame_addr);
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = tmp_frame_addr;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		if ((dstbuf->queued_in_vb2) &&
> > +		    (dstbuf->queued_in_v4l2) &&
> > +		    (frame_buffer->status == FB_ST_FREE)) {
> > +			mtk_v4l2_debug(2,
> > +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> > +				 ctx->idx, frame_buffer->status,
> > +				 dstbuf->vb.vb2_buf.index,
> > +				 dstbuf->queued_in_vb2);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +		} else if ((dstbuf->queued_in_vb2 == false) &&
> > +			   (dstbuf->queued_in_v4l2 == true)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]status=%x queue idx=%d to rdy_queue",
> > +					 ctx->idx, frame_buffer->status,
> > +					 dstbuf->vb.vb2_buf.index);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +			dstbuf->queued_in_vb2 = true;
> > +		} else {
> > +			mtk_v4l2_debug(2,
> > +					"[%d]status=%x err queue idx=%d %d %d",
> > +					ctx->idx, frame_buffer->status,
> > +					dstbuf->vb.vb2_buf.index,
> > +					dstbuf->queued_in_vb2,
> > +					dstbuf->queued_in_v4l2);
> > +		}
> > +		dstbuf->used = false;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_display_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_free_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	static const struct v4l2_event ev_src_ch = {
> > +		.type = V4L2_EVENT_SOURCE_CHANGE,
> > +		.u.src_change.changes =
> > +		V4L2_EVENT_SRC_CH_RESOLUTION,
> > +	};
> > +
> > +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> > +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> > +
> > +	return 0;
> > +}
> > +
> > +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	bool res_chg;
> > +	int ret = 0;
> > +
> > +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> > +	if (ret)
> > +		mtk_v4l2_err("DecodeFinal failed");
> > +
> > +	clean_display_buffer(ctx);
> > +	clean_free_buffer(ctx);
> > +}
> > +
> > +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	int dpbsize;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_PIC_INFO,
> > +				   &ctx->last_decoded_picinfo)) {
> > +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +				ctx->idx);
> > +		return false;
> > +	}
> > +
> > +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> > +		ctx->last_decoded_picinfo.pic_h == 0 ||
> > +		ctx->last_decoded_picinfo.buf_w == 0 ||
> > +		ctx->last_decoded_picinfo.buf_h == 0)
> > +		return false;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> > +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> > +				 ctx->last_decoded_picinfo.pic_h,
> > +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				 ctx->last_decoded_picinfo.buf_w,
> > +				 ctx->last_decoded_picinfo.buf_h);
> > +
> > +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +		if (dpbsize == 0)
> > +			dpbsize = 1;
> > +		ctx->dpb_count = dpbsize;
> > +		return true;
> > +	}
> > +
> > +	return false;
> > +}
> > +
> > +void mtk_vdec_worker(struct work_struct *work)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> > +				    decode_work);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	struct vb2_buffer *src_buf, *dst_buf;
> > +	struct mtk_vcodec_mem buf;
> > +	struct vdec_fb *pfb;
> > +	bool res_chg = false;
> > +	int ret;
> > +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> > +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> > +
> > +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +	if (src_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> > +	if (dst_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	buf.va = vb2_plane_vaddr(src_buf, 0);
> > +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> > +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> > +	if (!buf.va) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> > +			       ctx->idx, src_buf->index);
> > +		return;
> > +	}
> > +
> > +	pfb = &dst_buf_info->frame_buffer;
> > +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> > +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> > +
> > +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> > +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> > +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> > +	pfb->status = 0;
> > +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> > +	mtk_v4l2_debug(3,
> > +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> > +			 ctx->idx, buf.va,
> > +			 (u64)buf.dma_addr,
> > +			 buf.size, src_buf);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> > +			dst_buf->index, pfb,
> > +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> > +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> > +
> > +	if (src_buf_info->lastframe == true) {
> > +		/* update src buf status */
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		src_buf_info->lastframe = false;
> > +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> > +
> > +		/* update dst buf status */
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		dst_buf_info->used = false;
> > +
> > +		clean_display_buffer(ctx);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> > +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> > +		clean_free_buffer(ctx);
> > +	} else {
> > +
> > +		dst_buf_info->vb.timestamp
> > +					= src_buf_info->vb.timestamp;
> > +		dst_buf_info->vb.timecode
> > +					= src_buf_info->vb.timecode;
> > +		mutex_lock(&dst_buf_info->lock);
> > +		dst_buf_info->used = true;
> > +		mutex_unlock(&dst_buf_info->lock);
> > +		src_buf_info->used = true;
> > +
> > +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> > +
> > +		if (ret) {
> > +			mtk_v4l2_err(
> > +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> > +				ctx->idx,
> > +				src_buf->index,
> > +				src_buf_info->lastframe,
> > +				buf.size,
> > +				src_buf_info->vb.timestamp.tv_sec,
> > +				src_buf_info->vb.timestamp.tv_usec,
> > +				dst_buf->index,
> > +				ret, res_chg);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +					VB2_BUF_STATE_ERROR);
> > +		} else if (res_chg == false) {
> > +			/* we only return src buffer with VB2_BUF_STATE_DONE
> > +			  * when decode success without resolution change
> > +			  */
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +							VB2_BUF_STATE_DONE);
> > +		}
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		clean_display_buffer(ctx);
> > +		clean_free_buffer(ctx);
> > +
> > +		if ((ret == 0) && (res_chg == true)) {
> > +			mtk_vdec_pic_info_update(ctx);
> > +			/*
> > +			  * On encountering a resolution change in the stream.
> > +			  * The driver must first process and decode all
> > +			  * remaining buffers from before the resolution change
> > +			  * point, so call flush decode here
> > +			  */
> > +			mtk_vdec_flush_decoder(ctx);
> > +			/*
> > +			  * After all buffers containing decoded frames from
> > +			  * before the resolution change point ready to be
> > +			  * dequeued on the CAPTURE queue, the driver sends a
> > +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> > +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> > +			  */
> > +			mtk_vdec_queue_res_chg_event(ctx);
> > +		}
> > +	}
> > +
> > +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +
> > +}
> > +
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_unlock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_lock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	int ret = 0;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> > +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	src_vq->drv_priv	= ctx;
> > +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	src_vq->ops		= &mtk_vdec_vb2_ops;
> > +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	src_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(src_vq);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> > +		return ret;
> > +	}
> > +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> > +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	dst_vq->drv_priv	= ctx;
> > +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> > +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	dst_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(dst_vq);
> > +	if (ret) {
> > +		vb2_queue_release(src_vq);
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> > +	}
> > +
> > +	return ret;
> > +}
> > +
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	vdec_if_deinit(ctx);
> > +	ctx->state = MTK_STATE_FREE;
> > +}
> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 
Got it, will fix these to v4l2_m2m_ioctl_*.

> > +
> > +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> > +				struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> > +			 buf->type, buf->index);
> > +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> > +			    struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct vb2_queue *vq;
> > +	struct vb2_buffer *vb;
> > +	struct mtk_video_buf *mtkbuf;
> > +	struct vb2_v4l2_buffer	*vb2_v4l2;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> > +	vb = vq->bufs[buf->index];
> > +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    (buf->m.planes[0].bytesused == 0)) {
> > +		mtkbuf->lastframe = true;
> > +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> > +			 ctx->idx, buf->type, buf->index,
> > +			 mtkbuf->lastframe, buf->bytesused,
> > +			 buf->m.planes[0].bytesused, buf->length,
> > +			 vb);
> > +	}
> > +
> > +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> > +			     struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> > +			      struct v4l2_exportbuffer *eb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> > +}
> > +
> > +static int vidioc_vdec_querycap(struct file *file, void *priv,
> > +				struct v4l2_capability *cap)
> > +{
> > +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> > +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> > +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> > +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> > +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
> 
> See my comment about the new device_caps field in struct video_device from my
> review of the encoder driver.
> 
Got it, will fix this in next version.


> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_event_unsubscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> 
> No need for this function, just use v4l2_event_unsubscribe.
> 
Got it, will fix this.

> > +
> > +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_src_change_event_subscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> 
> Can't you just call v4l2_ctrl_subscribe_event() in the default case?
> 
Got it, we will call v4l2_ctrl_subscribe_event() in the default case.

> > +	}
> > +}
> > +
> > +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> > +		mtk_v4l2_err("sizeimage of output format must be given");
> > +		return -EINVAL;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> > +			 struct v4l2_crop *cr)
> 
> Don't use g_crop, use g_selection instead. The selection API is a superset
> of the old crop API.
> 
Will remove this in next version.


> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state < MTK_STATE_HEADER)
> > +		return -EINVAL;
> > +
> > +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> > +
> > +		if (vdec_if_get_param(ctx,
> > +					   GET_PARAM_CROP_INFO,
> > +					   cr)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> > +					 ctx->idx);
> > +			cr->c.left = 0;
> > +			cr->c.top = 0;
> > +			cr->c.width = ctx->picinfo.buf_w;
> > +			cr->c.height = ctx->picinfo.buf_h;
> > +		}
> > +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> > +				 cr->c.left, cr->c.top, cr->c.width,
> > +				 cr->c.height);
> > +	} else {
> > +		cr->c.left = 0;
> > +		cr->c.top = 0;
> > +		cr->c.width = ctx->picinfo.pic_w;
> > +		cr->c.height = ctx->picinfo.pic_h;
> > +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> > +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> > +				 ctx->picinfo.buf_h);
> > +	}
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +	int ret;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (fmt == NULL) {
> > +		mtk_v4l2_err("mtk_venc_find_format fail");
> > +		return ret;
> > +	}
> > +
> > +	pix_mp = &f->fmt.pix_mp;
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> > +		mtk_v4l2_err("out_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> > +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq)
> > +		return -EINVAL;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +	if (!q_data)
> > +		return -EINVAL;
> > +
> > +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		q_data->fmt = fmt;
> > +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> > +		pix_mp->plane_fmt[0].bytesperline = 0;
> > +		pix_mp->width = 0;
> > +		pix_mp->height = 0;
> > +
> > +		if (ctx->state == MTK_STATE_FREE) {
> > +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> > +			if (ret) {
> > +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> > +					       ctx->idx, ret);
> > +				return -EINVAL;
> > +			}
> > +			ctx->state = MTK_STATE_INIT;
> > +		}
> > +	} else {
> > +		q_data->fmt = fmt;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_enum_framesizes(struct file *file, void *priv,
> > +				struct v4l2_frmsizeenum *fsize)
> > +{
> > +	int i = 0;
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (fsize->index != 0)
> > +		return -EINVAL;
> > +
> > +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> > +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> > +			continue;
> > +
> > +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> > +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> > +		if (!(ctx->dev->dec_capability &
> > +				VCODEC_CAPABILITY_4K_DISABLED)) {
> > +			mtk_v4l2_debug(1, "4K is enabled");
> > +			fsize->stepwise.max_width =
> > +					VCODEC_DEC_4K_CODED_WIDTH;
> > +			fsize->stepwise.max_height =
> > +					VCODEC_DEC_4K_CODED_HEIGHT;
> > +		}
> > +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> > +					ctx->dev->dec_capability,
> > +					fsize->stepwise.min_width,
> > +					fsize->stepwise.max_width,
> > +					fsize->stepwise.step_width,
> > +					fsize->stepwise.min_height,
> > +					fsize->stepwise.max_height,
> > +					fsize->stepwise.step_height);
> > +		return 0;
> > +	}
> > +
> > +	return -EINVAL;
> > +
> > +}
> > +
> > +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	int i, j = 0;
> > +
> > +	for (i = 0; i < NUM_FORMATS; i++) {
> > +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> > +			continue;
> > +		if (!output_queue &&
> > +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> > +			continue;
> > +
> > +		if (j == f->index)
> > +			break;
> > +		++j;
> > +	}
> > +
> > +	if (i == NUM_FORMATS)
> > +		return -EINVAL;
> > +
> > +	fmt = &mtk_video_formats[i];
> > +	f->pixelformat = fmt->fourcc;
> > +	f->flags = 0;
> 
> No need.
> 
Will fix this in next version.

> > +	memset(f->reserved, 0, sizeof(f->reserved));
> 
> No need. The core will automatically set flags and reserved.
> 
Got it, will fix this in next version.
> > +
> > +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> > +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
> 
> No need for this, this is automatically done for you.
> 
Got it, will fix this in next version.

> Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
> in drivers/media/v4l2-core/v4l2-ioctl.c
> 
Got it, google will help upstream these part.

> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, false);
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, true);
> > +}
> > +
> > +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq) {
> > +		mtk_v4l2_err("no vb2 queue for type=%d",
> > +					   f->type);
> > +		return -EINVAL;
> > +	}
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    (ctx->state >= MTK_STATE_HEADER)) {
> > +		/* previous decode might have resolution change that make
> > +		 * picinfo changed update context'x picinfo here to make sure
> > +		 * we know what report to user space
> > +		 */
> > +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> > +			sizeof(struct vdec_pic_info));
> > +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> > +					ctx->picinfo.y_len_sz;
> > +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> > +					ctx->picinfo.c_len_sz;
> > +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->coded_width = ctx->picinfo.buf_w;
> > +		q_data->coded_height = ctx->picinfo.buf_h;
> > +
> > +		/*
> > +		 * Width and height are set to the dimensions
> > +		 * of the movie, the buffer is bigger and
> > +		 * further processing stages should crop to this
> > +		 * rectangle.
> > +		 */
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +
> > +		/*
> > +		 * Set pixelformat to the format in which mt vcodec
> > +		 * outputs the decoded frame
> > +		 */
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		/*
> > +		 * This is run on OUTPUT
> > +		 * The buffer contains compressed image
> > +		 * so width and height have no meaning.
> > +		 * Assign value here to pass v4l2-compliance test
> > +		 */
> > +		pix_mp->width = q_data->visible_width;
> > +		pix_mp->height = q_data->visible_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +	} else {
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> > +				ctx->idx, ctx->state, f->type);
> > +		return -EAGAIN;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> > +				   const void *parg,
> 
> Just like the encoder driver this patch needs to be rebased. The latest code no
> longer has the parg argument.
> 
Got it, will fix this in next version.

> > +				   unsigned int *nbuffers,
> > +				   unsigned int *nplanes,
> > +				   unsigned int sizes[], void *alloc_ctxs[])
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> > +	struct mtk_q_data *q_data;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> > +
> > +	switch (vq->type) {
> > +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> > +		*nplanes = 1;
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		break;
> > +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> > +		if (ctx->state < MTK_STATE_HEADER)
> > +			return -EINVAL;
> > +
> > +		/*
> > +		 * Output plane count is 2 - one for Y and one for CbCr
> > +		 */
> > +		*nplanes = 2;
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> > +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		sizes[1] = q_data->sizeimage[1];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +
> > +	mtk_v4l2_debug(1,
> > +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> > +			ctx->idx, vq->type, *nplanes, *nbuffers,
> > +			sizes[0], sizes[1]);
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct mtk_q_data *q_data;
> > +	int i;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> > +			 ctx->idx, vb->vb2_queue->type, vb->index);
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> > +
> > +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> > +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> > +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> > +				       i, vb2_plane_size(vb, i),
> > +				       q_data->sizeimage[i]);
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> > +			ctx->idx, vb->vb2_queue->type,
> > +			vb->index, vb);
> > +	/*
> > +	 * check if this buffer is ready to be used after decode
> > +	 */
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		struct vb2_buffer *src_buf;
> > +		struct mtk_vcodec_mem buf;
> > +		bool res_chg = false;
> > +		int ret = 0;
> > +		int dpbsize = 1;
> > +
> > +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> > +
> > +		if (ctx->state == MTK_STATE_INIT) {
> > +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +			if (!src_buf) {
> > +				mtk_v4l2_err("No src buffer");
> > +				return;
> > +			}
> > +
> > +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> > +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> > +							src_buf,
> > +							0);
> > +			buf.size = (size_t)src_buf->planes[0].bytesused;
> > +			mtk_v4l2_debug(2,
> > +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> > +					ctx->idx,
> > +					src_buf->index,
> > +					buf.va, (u64)buf.dma_addr, buf.size);
> > +
> > +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> > +			if (ret) {
> > +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +						VB2_BUF_STATE_DONE);
> > +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> > +						ctx->idx,
> > +						src_buf->index,
> > +						buf.size, ret);
> > +				return;
> > +			}
> > +
> > +			if (vdec_if_get_param(ctx,
> > +						   GET_PARAM_PIC_INFO,
> > +						   &ctx->picinfo)) {
> > +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +						ctx->idx);
> > +				return;
> > +			}
> > +
> > +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> > +				sizeof(struct vdec_pic_info));
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +						ctx->picinfo.y_bs_sz +
> > +						ctx->picinfo.y_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> > +							ctx->picinfo.buf_w;
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> > +						ctx->picinfo.c_bs_sz +
> > +						ctx->picinfo.c_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> > +							ctx->picinfo.buf_w;
> > +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> > +				ctx->idx,
> > +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> > +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> > +
> > +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +			if (dpbsize == 0) {
> > +				mtk_v4l2_debug(2,
> > +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> > +						ctx->idx, ret);
> > +				dpbsize = 1;
> > +			}
> > +			ctx->dpb_count = dpbsize;
> > +			ctx->state = MTK_STATE_HEADER;
> > +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> > +							ctx->dpb_count);
> > +		} else {
> > +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> > +					 ctx->idx, ctx->state);
> > +		}
> > +
> > +	} else {
> > +		mutex_lock(&buf->lock);
> > +		if (buf->used == false) {
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> > +					to_vb2_v4l2_buffer(vb));
> > +			buf->queued_in_vb2 = true;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		} else {
> > +			buf->queued_in_vb2 = false;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		}
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> > +{
> > +	if (vb->vb2_queue->type ==
> > +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +		mutex_lock(&buf->lock);
> > +		buf->queued_in_v4l2 = false;
> > +		buf->queued_in_vb2 = false;
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> > +{
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		buf->used = false;
> > +		buf->ready_to_display = false;
> > +		buf->nonrealdisplay = false;
> > +		buf->queued_in_v4l2 = false;
> > +		mutex_init(&buf->lock);
> > +	} else {
> > +		buf->lastframe = false;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	if (ctx->state == MTK_STATE_FLUSH)
> > +		ctx->state = MTK_STATE_HEADER;
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> > +{
> > +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> > +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> > +
> > +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		while (src_buf) {
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +					VB2_BUF_STATE_ERROR);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		}
> > +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		int i;
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		if (ctx->state >= MTK_STATE_HEADER)
> > +			mtk_vdec_flush_decoder(ctx);
> > +
> > +		ctx->state = MTK_STATE_FLUSH;
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		while (dst_buf) {
> > +			vb2_set_plane_payload(dst_buf, 0, 0);
> > +			vb2_set_plane_payload(dst_buf, 1, 0);
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> > +				VB2_BUF_STATE_ERROR);
> > +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		}
> > +
> > +		for (i = 0; i < q->num_buffers; ++i) {
> > +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> > +				vb2_v4l2 = container_of(q->bufs[i],
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +				buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> > +						ctx->idx, i, q->type,
> > +						buf->queued_in_vb2,
> > +						buf->queued_in_v4l2,
> > +						(int)q->bufs[i]->state);
> > +				v4l2_m2m_buf_done(vb2_v4l2,
> > +						VB2_BUF_STATE_ERROR);
> > +			}
> > +		}
> > +	}
> > +}
> > +
> > +static void m2mops_vdec_device_run(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +
> > +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> > +}
> > +
> > +static int m2mops_vdec_job_ready(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT)
> > +		return 0;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> > +		return 0;
> > +
> > +	if (ctx->state != MTK_STATE_HEADER)
> > +		return 0;
> > +
> > +	return 1;
> > +}
> > +
> > +static void m2mops_vdec_job_abort(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +
> > +	ctx->state = MTK_STATE_ABORT;
> > +}
> > +
> > +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> > +
> > +	switch (ctrl->id) {
> > +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> > +		if (ctx->state >= MTK_STATE_HEADER) {
> > +			ctrl->val = ctx->dpb_count;
> > +		} else {
> > +			mtk_v4l2_err("Seqinfo not ready");
> > +			ctrl->val = 1;
> > +			return -EAGAIN;
> > +		}
> > +		break;
> > +	default:
> > +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> > +		return -EINVAL;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> > +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> > +};
> > +
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct v4l2_ctrl *ctrl;
> > +
> > +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> > +
> > +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> > +					  &mtk_vcodec_dec_ctrl_ops,
> > +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> > +					  1, 32, 1, 1);
> > +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> > +
> > +	if (ctx->ctrl_hdl.error) {
> > +		mtk_v4l2_err("Adding control failed %d",
> > +					ctx->ctrl_hdl.error);
> > +		return ctx->ctrl_hdl.error;
> > +	}
> > +
> > +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> > +	return 0;
> > +}
> > +
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> > +}
> > +
> > +static void m2mops_vdec_lock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_lock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +static void m2mops_vdec_unlock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_unlock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> > +	.device_run	= m2mops_vdec_device_run,
> > +	.job_ready	= m2mops_vdec_job_ready,
> > +	.job_abort	= m2mops_vdec_job_abort,
> > +	.lock	= m2mops_vdec_lock,
> > +	.unlock	= m2mops_vdec_unlock,
> > +};
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops = {
> > +	.queue_setup	= vb2ops_vdec_queue_setup,
> > +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> > +	.buf_queue	= vb2ops_vdec_buf_queue,
> > +	.wait_prepare	= vb2_ops_wait_prepare,
> > +	.wait_finish	= vb2_ops_wait_finish,
> > +	.buf_init	= vb2ops_vdec_buf_init,
> > +	.buf_finish	= vb2ops_vdec_buf_finish,
> > +	.start_streaming	= vb2ops_vdec_start_streaming,
> > +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> > +};
> > +
> > +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> > +	.vidioc_streamon	= vidioc_vdec_streamon,
> > +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> > +
> > +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> > +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> > +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> > +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> > +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> > +
> > +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> > +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> > +
> > +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> > +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> > +
> > +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> > +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> > +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> > +
> > +	.vidioc_querycap	= vidioc_vdec_querycap,
> > +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> > +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> > +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> > +};
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > new file mode 100644
> > index 0000000..cb5cb42
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > @@ -0,0 +1,81 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#ifndef _MTK_VCODEC_DEC_H_
> > +#define _MTK_VCODEC_DEC_H_
> > +
> > +#include <media/videobuf2-core.h>
> > +#include <media/videobuf2-v4l2.h>
> > +
> > +/**
> > + * struct vdec_fb  - decoder frame buffer
> > + * @base_y	: Y plane memory info
> > + * @base_c	: C plane memory info
> > + * @status      : frame buffer status (vdec_fb_status)
> > + */
> > +struct vdec_fb {
> > +	struct mtk_vcodec_mem	base_y;
> > +	struct mtk_vcodec_mem	base_c;
> > +	unsigned int	status;
> > +};
> > +
> > +/**
> > + * struct mtk_video_buf - Private data related to each VB2 buffer.
> > + * @b:			VB2 buffer
> > + * @list:			link list
> > + * @used:		Output buffer contain decoded frame data
> > + * @ready_to_display:	Output buffer not display yet
> > + * @nonrealdisplay:	Output buffer is not display frame
> > + * @queued_in_vb2:	Output buffer is queue in vb2
> > + * @queued_in_v4l2:	Output buffer is in v4l2
> > + * @lastframe:		Intput buffer is last buffer - EOS
> > + * @frame_buffer:		Decode status of output buffer
> > + * @lock:			V4L2 and decode thread should get mutex
> > + *			before r/w info in mtk_video_buf
> > + */
> > +struct mtk_video_buf {
> > +	struct vb2_v4l2_buffer	vb;
> > +	struct list_head	list;
> > +
> > +	bool	used;
> > +	bool	ready_to_display;
> > +	bool	nonrealdisplay;
> > +	bool	queued_in_vb2;
> > +	bool	queued_in_v4l2;
> > +	bool	lastframe;
> > +	struct vdec_fb	frame_buffer;
> > +	struct mutex	lock;
> > +};
> > +
> > +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> > +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> > +
> > +
> > +/*
> > + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> > + * get/release lock before/after access decoder hw.
> > + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> > + * to ctx instance that get lock
> > + */
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq);
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> > +
> > +#endif /* _MTK_VCODEC_DEC_H_ */
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > new file mode 100644
> > index 0000000..34e3217
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > @@ -0,0 +1,469 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <linux/interrupt.h>
> > +#include <linux/irq.h>
> > +#include <linux/module.h>
> > +#include <linux/of_device.h>
> > +#include <linux/of.h>
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vpu.h"
> > +
> > +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> > +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> > +
> > +#define VDEC_HW_ACTIVE	0x10
> > +#define VDEC_IRQ_CFG	0x11
> > +#define VDEC_IRQ_CLR	0x10
> > +#define VDEC_IRQ_CFG_REG	0xa4
> > +
> > +
> > +/* Wake up context wait_queue */
> > +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> > +{
> > +	ctx->int_cond = 1;
> > +	ctx->int_type = reason;
> > +	wake_up_interruptible(&ctx->queue);
> > +}
> > +
> > +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	unsigned long flags;
> > +	struct mtk_vcodec_ctx *ctx;
> > +	unsigned int cg_status = 0;
> > +	unsigned int dec_done_status = 0;
> > +
> > +	spin_lock_irqsave(&dev->irqlock, flags);
> > +	ctx = dev->curr_ctx;
> > +	spin_unlock_irqrestore(&dev->irqlock, flags);
> > +
> > +	cg_status = readl(dev->reg_base[0] + (0));
> > +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> > +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> > +			       cg_status);
> > +		return IRQ_HANDLED;
> > +	}
> > +
> > +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	ctx->irq_status = dec_done_status;
> > +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> > +		return IRQ_HANDLED;
> > +
> > +	/* clear interrupt */
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +
> > +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> > +			ctx->idx, dec_done_status);
> > +
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int fops_vcodec_open(struct file *file)
> > +{
> > +	struct video_device *vfd = video_devdata(file);
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = NULL;
> > +	int ret = 0;
> > +
> > +	if (dev->instance_mask == ~0UL) {
> > +		mtk_v4l2_err("Too many open contexts");
> > +		ret = -EBUSY;
> > +		goto err_alloc;
> > +	}
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +
> > +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> > +	if (!ctx) {
> > +		ret = -ENOMEM;
> > +		goto err_alloc;
> > +	}
> > +
> > +	ctx->idx = ffz(dev->instance_mask);
> > +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> > +	file->private_data = &ctx->fh;
> > +	v4l2_fh_add(&ctx->fh);
> > +	INIT_LIST_HEAD(&ctx->list);
> > +	ctx->dev = dev;
> > +	init_waitqueue_head(&ctx->queue);
> > +
> > +	if (vfd == dev->vfd_dec) {
> > +		ctx->type = MTK_INST_DECODER;
> > +		ret = mtk_vdec_ctrls_setup(ctx);
> > +		if (ret) {
> > +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> > +			goto err_ctrls_setup;
> > +		}
> > +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> > +						 &m2mctx_vdec_queue_init);
> > +		if (IS_ERR(ctx->m2m_ctx)) {
> > +			ret = PTR_ERR(ctx->m2m_ctx);
> > +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> > +				       ret);
> > +			goto err_ctx_init;
> > +		}
> > +		mtk_vcodec_dec_set_default_params(ctx);
> > +	} else {
> > +		mtk_v4l2_err("Invalid vfd !");
> > +		ret = -ENOENT;
> > +		goto err_ctx_init;
> > +	}
> > +
> > +	if (v4l2_fh_is_singular(&ctx->fh)) {
> > +		mtk_vcodec_dec_pw_on(&dev->pm);
> > +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> > +		if (ret < 0) {
> > +			mtk_v4l2_err("vpu_load_firmware failed!");
> > +			goto err_load_fw;
> > +		}
> > +
> > +		dev->dec_capability =
> > +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> > +		mtk_v4l2_debug(0, "decoder capability %x",
> > +			       dev->dec_capability);
> > +	}
> > +
> > +	set_bit(ctx->idx, &dev->instance_mask);
> > +	dev->num_instances++;
> > +	list_add(&ctx->list, &dev->ctx_list);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> > +					ctx->idx);
> > +
> > +	return ret;
> > +
> > +	/* Deinit when failure occurred */
> > +err_load_fw:
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +err_ctx_init:
> > +	mtk_vdec_ctrls_free(ctx);
> > +err_ctrls_setup:
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +err_alloc:
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_release(struct file *file)
> > +{
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +	mtk_vcodec_vdec_release(ctx);
> > +	mtk_vdec_ctrls_free(ctx);
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	list_del_init(&ctx->list);
> > +	dev->num_instances--;
> > +	if (dev->num_instances == 0)
> > +		mtk_vcodec_dec_pw_off(&dev->pm);
> > +	clear_bit(ctx->idx, &dev->instance_mask);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	return 0;
> > +}
> > +
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 
Got it, will fix this in next version.


best regards,
Tiffany

> > +};
> > +
> > +static void mtk_vcodec_dec_reset_handler(void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	struct mtk_vcodec_ctx *ctx;
> > +
> > +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> > +		ctx->state = MTK_STATE_ABORT;
> > +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> > +					   ctx->idx);
> > +	}
> > +	mutex_unlock(&dev->dev_mutex);
> > +}
> > +
> > +static int mtk_vcodec_probe(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev;
> > +	struct video_device *vfd_dec;
> > +	struct resource *res;
> > +	int i, ret;
> > +	int reg_base_num;
> > +
> > +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> > +	if (!dev)
> > +		return -ENOMEM;
> > +
> > +	INIT_LIST_HEAD(&dev->ctx_list);
> > +	dev->plat_dev = pdev;
> > +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> > +	if (dev->vpu_plat_dev == NULL) {
> > +		mtk_v4l2_err("[VPU] vpu device in not ready");
> > +		return -EPROBE_DEFER;
> > +	}
> > +
> > +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> > +				dev, VPU_RST_DEC);
> > +
> > +	ret = mtk_vcodec_init_dec_pm(dev);
> > +	if (ret < 0) {
> > +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> > +		return ret;
> > +	}
> > +
> > +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> > +	for (i = 0; i < reg_base_num; i++) {
> > +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> > +		if (res == NULL) {
> > +			dev_err(&pdev->dev, "get memory resource failed.");
> > +			ret = -ENXIO;
> > +			goto err_res;
> > +		}
> > +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> > +		if (IS_ERR(dev->reg_base[i])) {
> > +			dev_err(&pdev->dev,
> > +				"devm_ioremap_resource %d failed.", i);
> > +			ret = PTR_ERR(dev->reg_base);
> > +			goto err_res;
> > +		}
> > +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> > +	}
> > +
> > +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> > +	if (res == NULL) {
> > +		dev_err(&pdev->dev, "failed to get irq resource");
> > +		ret = -ENOENT;
> > +		goto err_res;
> > +	}
> > +
> > +	dev->dec_irq = platform_get_irq(pdev, 0);
> > +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> > +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> > +	if (ret) {
> > +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> > +			dev->dec_irq,
> > +			ret);
> > +		ret = -EINVAL;
> > +		goto err_res;
> > +	}
> > +
> > +	disable_irq(dev->dec_irq);
> > +	mutex_init(&dev->dev_mutex);
> > +	mutex_init(&dev->dec_mutex);
> > +	spin_lock_init(&dev->irqlock);
> > +
> > +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> > +		 "[MTK_V4L2]");
> > +
> > +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> > +	if (ret) {
> > +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> > +		return ret;
> > +	}
> > +
> > +	init_waitqueue_head(&dev->queue);
> > +
> > +	vfd_dec = video_device_alloc();
> > +	if (!vfd_dec) {
> > +		mtk_v4l2_err("Failed to allocate video device");
> > +		ret = -ENOMEM;
> > +		goto err_dec_alloc;
> > +	}
> > +	vfd_dec->fops		= &mtk_vcodec_fops;
> > +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> > +	vfd_dec->release	= video_device_release;
> > +	vfd_dec->lock		= &dev->dev_mutex;
> > +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> > +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> > +
> > +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> > +		 MTK_VCODEC_DEC_NAME);
> > +	video_set_drvdata(vfd_dec, dev);
> > +	dev->vfd_dec = vfd_dec;
> > +	platform_set_drvdata(pdev, dev);
> > +
> > +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> > +	if (IS_ERR(dev->alloc_ctx)) {
> > +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> > +		ret = PTR_ERR(dev->alloc_ctx);
> > +		goto err_vb2_ctx_init;
> > +	}
> > +
> > +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> > +	if (IS_ERR(dev->m2m_dev_dec)) {
> > +		mtk_v4l2_err("Failed to init mem2mem dec device");
> > +		ret = PTR_ERR(dev->m2m_dev_dec);
> > +		goto err_dec_mem_init;
> > +	}
> > +
> > +	dev->decode_workqueue =
> > +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> > +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> > +	if (!dev->decode_workqueue) {
> > +		mtk_v4l2_err("Failed to create decode workqueue");
> > +		ret = -EINVAL;
> > +		goto err_event_workq;
> > +	}
> > +
> > +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to register video device");
> > +		goto err_dec_reg;
> > +	}
> > +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> > +			 vfd_dec->num);
> > +
> > +	return 0;
> > +
> > +err_dec_reg:
> > +	destroy_workqueue(dev->decode_workqueue);
> > +err_event_workq:
> > +	v4l2_m2m_release(dev->m2m_dev_dec);
> > +err_dec_mem_init:
> > +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +err_vb2_ctx_init:
> > +	video_unregister_device(vfd_dec);
> > +err_dec_alloc:
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +err_res:
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return ret;
> > +}
> > +
> > +static const struct of_device_id mtk_vcodec_match[] = {
> > +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> > +	{},
> > +};
> > +
> > +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> > +
> > +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug_enter();
> > +	flush_workqueue(dev->decode_workqueue);
> > +	destroy_workqueue(dev->decode_workqueue);
> > +	if (dev->m2m_dev_dec)
> > +		v4l2_m2m_release(dev->m2m_dev_dec);
> > +	if (dev->alloc_ctx)
> > +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +
> > +	if (dev->vfd_dec)
> > +		video_unregister_device(dev->vfd_dec);
> > +
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> > +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> > +			   mtk_vcodec_vdec_pm_runtime_resume,
> > +			   NULL)
> > +};
> > +
> > +static struct platform_driver mtk_vcodec_dec_driver = {
> > +	.probe	= mtk_vcodec_probe,
> > +	.remove	= mtk_vcodec_dec_remove,
> > +	.driver	= {
> > +		.name	= MTK_VCODEC_DEC_NAME,
> > +		.owner	= THIS_MODULE,
> > +		.of_match_table = mtk_vcodec_match,
> > +		.pm = &mtk_vcodec_vdec_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(mtk_vcodec_dec_driver);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
> 
> Regards,
> 
> 	Hans
> 

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  4:00           ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  4:00 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Hi Hans,

On Fri, 2016-04-15 at 16:27 +0200, Hans Verkuil wrote:
> On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> > Add v4l2 layer decoder driver for MT8173
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> > ---
> >  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
> >  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
> >  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
> >  11 files changed, 2596 insertions(+), 20 deletions(-)
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> > 
> > diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> > index dc5cb00..4c8ed2f 100644
> > --- a/drivers/media/platform/mtk-vcodec/Makefile
> > +++ b/drivers/media/platform/mtk-vcodec/Makefile
> > @@ -1,7 +1,13 @@
> >  
> >  
> > -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> > -
> > +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> > +				       mtk-vcodec-enc.o \
> > +				       mtk-vcodec-common.o
> > +
> > +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> > +		vdec_drv_if.o \
> > +		mtk_vcodec_dec.o \
> > +		mtk_vcodec_dec_pm.o \
> >  
> >  
> >  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > new file mode 100644
> > index 0000000..0499413
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > @@ -0,0 +1,1429 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +
> > +static struct mtk_video_fmt mtk_video_formats[] = {
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_H264,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP8,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_MT21,
> > +		.type = MTK_FMT_FRAME,
> > +		.num_planes = 2,
> > +	},
> > +};
> > +#define OUT_FMT_IDX	0
> > +#define CAP_FMT_IDX	3
> > +
> > +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> > +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> > +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> > +
> > +#define MTK_VDEC_MIN_W	64U
> > +#define MTK_VDEC_MIN_H	64U
> > +#define MTK_VDEC_MAX_W	2048U
> > +#define MTK_VDEC_MAX_H	1088U
> > +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> > +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> > +
> > +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_H264,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_VP8,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +};
> > +
> > +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> > +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> > +
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops;
> > +
> > +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	unsigned int k;
> > +
> > +	for (k = 0; k < NUM_FORMATS; k++) {
> > +		fmt = &mtk_video_formats[k];
> > +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> > +			return fmt;
> > +	}
> > +
> > +	return NULL;
> > +}
> > +
> > +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> > +					      enum v4l2_buf_type type)
> > +{
> > +	if (V4L2_TYPE_IS_OUTPUT(type))
> > +		return &ctx->q_data[MTK_Q_DATA_SRC];
> > +
> > +	return &ctx->q_data[MTK_Q_DATA_DST];
> > +}
> > +
> > +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vdec_fb *disp_frame_buffer;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_DISP_FRAME_BUFFER,
> > +				   &disp_frame_buffer)) {
> > +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> > +			     ctx->idx);
> > +		return NULL;
> > +	}
> > +
> > +	if (disp_frame_buffer == NULL) {
> > +		mtk_v4l2_debug(3, "No display frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = disp_frame_buffer;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> > +							ctx->picinfo.y_bs_sz);
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> > +							ctx->picinfo.c_bs_sz);
> > +
> > +		dstbuf->ready_to_display = true;
> > +		dstbuf->nonrealdisplay = false;
> > +		mtk_v4l2_debug(2,
> > +			       "[%d]status=%x queue idx=%d to done_list %d",
> > +			       ctx->idx, frame_buffer->status,
> > +			       dstbuf->vb.vb2_buf.index,
> > +			       dstbuf->queued_in_vb2);
> > +
> > +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> > +		ctx->decoded_frame_cnt++;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	void *tmp_frame_addr;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_FREE_FRAME_BUFFER,
> > +				   &tmp_frame_addr)) {
> > +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> > +		return NULL;
> > +	}
> > +	if (tmp_frame_addr == NULL) {
> > +		mtk_v4l2_debug(3, " No free frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> > +			 ctx->idx, tmp_frame_addr);
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = tmp_frame_addr;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		if ((dstbuf->queued_in_vb2) &&
> > +		    (dstbuf->queued_in_v4l2) &&
> > +		    (frame_buffer->status == FB_ST_FREE)) {
> > +			mtk_v4l2_debug(2,
> > +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> > +				 ctx->idx, frame_buffer->status,
> > +				 dstbuf->vb.vb2_buf.index,
> > +				 dstbuf->queued_in_vb2);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +		} else if ((dstbuf->queued_in_vb2 == false) &&
> > +			   (dstbuf->queued_in_v4l2 == true)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]status=%x queue idx=%d to rdy_queue",
> > +					 ctx->idx, frame_buffer->status,
> > +					 dstbuf->vb.vb2_buf.index);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +			dstbuf->queued_in_vb2 = true;
> > +		} else {
> > +			mtk_v4l2_debug(2,
> > +					"[%d]status=%x err queue idx=%d %d %d",
> > +					ctx->idx, frame_buffer->status,
> > +					dstbuf->vb.vb2_buf.index,
> > +					dstbuf->queued_in_vb2,
> > +					dstbuf->queued_in_v4l2);
> > +		}
> > +		dstbuf->used = false;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_display_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_free_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	static const struct v4l2_event ev_src_ch = {
> > +		.type = V4L2_EVENT_SOURCE_CHANGE,
> > +		.u.src_change.changes =
> > +		V4L2_EVENT_SRC_CH_RESOLUTION,
> > +	};
> > +
> > +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> > +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> > +
> > +	return 0;
> > +}
> > +
> > +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	bool res_chg;
> > +	int ret = 0;
> > +
> > +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> > +	if (ret)
> > +		mtk_v4l2_err("DecodeFinal failed");
> > +
> > +	clean_display_buffer(ctx);
> > +	clean_free_buffer(ctx);
> > +}
> > +
> > +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	int dpbsize;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_PIC_INFO,
> > +				   &ctx->last_decoded_picinfo)) {
> > +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +				ctx->idx);
> > +		return false;
> > +	}
> > +
> > +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> > +		ctx->last_decoded_picinfo.pic_h == 0 ||
> > +		ctx->last_decoded_picinfo.buf_w == 0 ||
> > +		ctx->last_decoded_picinfo.buf_h == 0)
> > +		return false;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> > +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> > +				 ctx->last_decoded_picinfo.pic_h,
> > +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				 ctx->last_decoded_picinfo.buf_w,
> > +				 ctx->last_decoded_picinfo.buf_h);
> > +
> > +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +		if (dpbsize == 0)
> > +			dpbsize = 1;
> > +		ctx->dpb_count = dpbsize;
> > +		return true;
> > +	}
> > +
> > +	return false;
> > +}
> > +
> > +void mtk_vdec_worker(struct work_struct *work)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> > +				    decode_work);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	struct vb2_buffer *src_buf, *dst_buf;
> > +	struct mtk_vcodec_mem buf;
> > +	struct vdec_fb *pfb;
> > +	bool res_chg = false;
> > +	int ret;
> > +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> > +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> > +
> > +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +	if (src_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> > +	if (dst_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	buf.va = vb2_plane_vaddr(src_buf, 0);
> > +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> > +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> > +	if (!buf.va) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> > +			       ctx->idx, src_buf->index);
> > +		return;
> > +	}
> > +
> > +	pfb = &dst_buf_info->frame_buffer;
> > +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> > +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> > +
> > +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> > +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> > +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> > +	pfb->status = 0;
> > +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> > +	mtk_v4l2_debug(3,
> > +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> > +			 ctx->idx, buf.va,
> > +			 (u64)buf.dma_addr,
> > +			 buf.size, src_buf);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> > +			dst_buf->index, pfb,
> > +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> > +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> > +
> > +	if (src_buf_info->lastframe == true) {
> > +		/* update src buf status */
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		src_buf_info->lastframe = false;
> > +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> > +
> > +		/* update dst buf status */
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		dst_buf_info->used = false;
> > +
> > +		clean_display_buffer(ctx);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> > +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> > +		clean_free_buffer(ctx);
> > +	} else {
> > +
> > +		dst_buf_info->vb.timestamp
> > +					= src_buf_info->vb.timestamp;
> > +		dst_buf_info->vb.timecode
> > +					= src_buf_info->vb.timecode;
> > +		mutex_lock(&dst_buf_info->lock);
> > +		dst_buf_info->used = true;
> > +		mutex_unlock(&dst_buf_info->lock);
> > +		src_buf_info->used = true;
> > +
> > +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> > +
> > +		if (ret) {
> > +			mtk_v4l2_err(
> > +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> > +				ctx->idx,
> > +				src_buf->index,
> > +				src_buf_info->lastframe,
> > +				buf.size,
> > +				src_buf_info->vb.timestamp.tv_sec,
> > +				src_buf_info->vb.timestamp.tv_usec,
> > +				dst_buf->index,
> > +				ret, res_chg);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +					VB2_BUF_STATE_ERROR);
> > +		} else if (res_chg == false) {
> > +			/* we only return src buffer with VB2_BUF_STATE_DONE
> > +			  * when decode success without resolution change
> > +			  */
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +							VB2_BUF_STATE_DONE);
> > +		}
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		clean_display_buffer(ctx);
> > +		clean_free_buffer(ctx);
> > +
> > +		if ((ret == 0) && (res_chg == true)) {
> > +			mtk_vdec_pic_info_update(ctx);
> > +			/*
> > +			  * On encountering a resolution change in the stream.
> > +			  * The driver must first process and decode all
> > +			  * remaining buffers from before the resolution change
> > +			  * point, so call flush decode here
> > +			  */
> > +			mtk_vdec_flush_decoder(ctx);
> > +			/*
> > +			  * After all buffers containing decoded frames from
> > +			  * before the resolution change point ready to be
> > +			  * dequeued on the CAPTURE queue, the driver sends a
> > +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> > +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> > +			  */
> > +			mtk_vdec_queue_res_chg_event(ctx);
> > +		}
> > +	}
> > +
> > +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +
> > +}
> > +
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_unlock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_lock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	int ret = 0;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> > +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	src_vq->drv_priv	= ctx;
> > +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	src_vq->ops		= &mtk_vdec_vb2_ops;
> > +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	src_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(src_vq);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> > +		return ret;
> > +	}
> > +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> > +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	dst_vq->drv_priv	= ctx;
> > +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> > +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	dst_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(dst_vq);
> > +	if (ret) {
> > +		vb2_queue_release(src_vq);
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> > +	}
> > +
> > +	return ret;
> > +}
> > +
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	vdec_if_deinit(ctx);
> > +	ctx->state = MTK_STATE_FREE;
> > +}
> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 
Got it, will fix these to v4l2_m2m_ioctl_*.

> > +
> > +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> > +				struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> > +			 buf->type, buf->index);
> > +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> > +			    struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct vb2_queue *vq;
> > +	struct vb2_buffer *vb;
> > +	struct mtk_video_buf *mtkbuf;
> > +	struct vb2_v4l2_buffer	*vb2_v4l2;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> > +	vb = vq->bufs[buf->index];
> > +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    (buf->m.planes[0].bytesused == 0)) {
> > +		mtkbuf->lastframe = true;
> > +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> > +			 ctx->idx, buf->type, buf->index,
> > +			 mtkbuf->lastframe, buf->bytesused,
> > +			 buf->m.planes[0].bytesused, buf->length,
> > +			 vb);
> > +	}
> > +
> > +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> > +			     struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> > +			      struct v4l2_exportbuffer *eb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> > +}
> > +
> > +static int vidioc_vdec_querycap(struct file *file, void *priv,
> > +				struct v4l2_capability *cap)
> > +{
> > +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> > +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> > +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> > +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> > +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
> 
> See my comment about the new device_caps field in struct video_device from my
> review of the encoder driver.
> 
Got it, will fix this in next version.


> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_event_unsubscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> 
> No need for this function, just use v4l2_event_unsubscribe.
> 
Got it, will fix this.

> > +
> > +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_src_change_event_subscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> 
> Can't you just call v4l2_ctrl_subscribe_event() in the default case?
> 
Got it, we will call v4l2_ctrl_subscribe_event() in the default case.

> > +	}
> > +}
> > +
> > +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> > +		mtk_v4l2_err("sizeimage of output format must be given");
> > +		return -EINVAL;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> > +			 struct v4l2_crop *cr)
> 
> Don't use g_crop, use g_selection instead. The selection API is a superset
> of the old crop API.
> 
Will remove this in next version.


> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state < MTK_STATE_HEADER)
> > +		return -EINVAL;
> > +
> > +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> > +
> > +		if (vdec_if_get_param(ctx,
> > +					   GET_PARAM_CROP_INFO,
> > +					   cr)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> > +					 ctx->idx);
> > +			cr->c.left = 0;
> > +			cr->c.top = 0;
> > +			cr->c.width = ctx->picinfo.buf_w;
> > +			cr->c.height = ctx->picinfo.buf_h;
> > +		}
> > +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> > +				 cr->c.left, cr->c.top, cr->c.width,
> > +				 cr->c.height);
> > +	} else {
> > +		cr->c.left = 0;
> > +		cr->c.top = 0;
> > +		cr->c.width = ctx->picinfo.pic_w;
> > +		cr->c.height = ctx->picinfo.pic_h;
> > +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> > +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> > +				 ctx->picinfo.buf_h);
> > +	}
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +	int ret;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (fmt == NULL) {
> > +		mtk_v4l2_err("mtk_venc_find_format fail");
> > +		return ret;
> > +	}
> > +
> > +	pix_mp = &f->fmt.pix_mp;
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> > +		mtk_v4l2_err("out_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> > +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq)
> > +		return -EINVAL;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +	if (!q_data)
> > +		return -EINVAL;
> > +
> > +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		q_data->fmt = fmt;
> > +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> > +		pix_mp->plane_fmt[0].bytesperline = 0;
> > +		pix_mp->width = 0;
> > +		pix_mp->height = 0;
> > +
> > +		if (ctx->state == MTK_STATE_FREE) {
> > +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> > +			if (ret) {
> > +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> > +					       ctx->idx, ret);
> > +				return -EINVAL;
> > +			}
> > +			ctx->state = MTK_STATE_INIT;
> > +		}
> > +	} else {
> > +		q_data->fmt = fmt;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_enum_framesizes(struct file *file, void *priv,
> > +				struct v4l2_frmsizeenum *fsize)
> > +{
> > +	int i = 0;
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (fsize->index != 0)
> > +		return -EINVAL;
> > +
> > +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> > +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> > +			continue;
> > +
> > +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> > +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> > +		if (!(ctx->dev->dec_capability &
> > +				VCODEC_CAPABILITY_4K_DISABLED)) {
> > +			mtk_v4l2_debug(1, "4K is enabled");
> > +			fsize->stepwise.max_width =
> > +					VCODEC_DEC_4K_CODED_WIDTH;
> > +			fsize->stepwise.max_height =
> > +					VCODEC_DEC_4K_CODED_HEIGHT;
> > +		}
> > +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> > +					ctx->dev->dec_capability,
> > +					fsize->stepwise.min_width,
> > +					fsize->stepwise.max_width,
> > +					fsize->stepwise.step_width,
> > +					fsize->stepwise.min_height,
> > +					fsize->stepwise.max_height,
> > +					fsize->stepwise.step_height);
> > +		return 0;
> > +	}
> > +
> > +	return -EINVAL;
> > +
> > +}
> > +
> > +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	int i, j = 0;
> > +
> > +	for (i = 0; i < NUM_FORMATS; i++) {
> > +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> > +			continue;
> > +		if (!output_queue &&
> > +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> > +			continue;
> > +
> > +		if (j == f->index)
> > +			break;
> > +		++j;
> > +	}
> > +
> > +	if (i == NUM_FORMATS)
> > +		return -EINVAL;
> > +
> > +	fmt = &mtk_video_formats[i];
> > +	f->pixelformat = fmt->fourcc;
> > +	f->flags = 0;
> 
> No need.
> 
Will fix this in next version.

> > +	memset(f->reserved, 0, sizeof(f->reserved));
> 
> No need. The core will automatically set flags and reserved.
> 
Got it, will fix this in next version.
> > +
> > +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> > +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
> 
> No need for this, this is automatically done for you.
> 
Got it, will fix this in next version.

> Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
> in drivers/media/v4l2-core/v4l2-ioctl.c
> 
Got it, google will help upstream these part.

> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, false);
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, true);
> > +}
> > +
> > +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq) {
> > +		mtk_v4l2_err("no vb2 queue for type=%d",
> > +					   f->type);
> > +		return -EINVAL;
> > +	}
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    (ctx->state >= MTK_STATE_HEADER)) {
> > +		/* previous decode might have resolution change that make
> > +		 * picinfo changed update context'x picinfo here to make sure
> > +		 * we know what report to user space
> > +		 */
> > +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> > +			sizeof(struct vdec_pic_info));
> > +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> > +					ctx->picinfo.y_len_sz;
> > +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> > +					ctx->picinfo.c_len_sz;
> > +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->coded_width = ctx->picinfo.buf_w;
> > +		q_data->coded_height = ctx->picinfo.buf_h;
> > +
> > +		/*
> > +		 * Width and height are set to the dimensions
> > +		 * of the movie, the buffer is bigger and
> > +		 * further processing stages should crop to this
> > +		 * rectangle.
> > +		 */
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +
> > +		/*
> > +		 * Set pixelformat to the format in which mt vcodec
> > +		 * outputs the decoded frame
> > +		 */
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		/*
> > +		 * This is run on OUTPUT
> > +		 * The buffer contains compressed image
> > +		 * so width and height have no meaning.
> > +		 * Assign value here to pass v4l2-compliance test
> > +		 */
> > +		pix_mp->width = q_data->visible_width;
> > +		pix_mp->height = q_data->visible_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +	} else {
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> > +				ctx->idx, ctx->state, f->type);
> > +		return -EAGAIN;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> > +				   const void *parg,
> 
> Just like the encoder driver this patch needs to be rebased. The latest code no
> longer has the parg argument.
> 
Got it, will fix this in next version.

> > +				   unsigned int *nbuffers,
> > +				   unsigned int *nplanes,
> > +				   unsigned int sizes[], void *alloc_ctxs[])
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> > +	struct mtk_q_data *q_data;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> > +
> > +	switch (vq->type) {
> > +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> > +		*nplanes = 1;
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		break;
> > +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> > +		if (ctx->state < MTK_STATE_HEADER)
> > +			return -EINVAL;
> > +
> > +		/*
> > +		 * Output plane count is 2 - one for Y and one for CbCr
> > +		 */
> > +		*nplanes = 2;
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> > +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		sizes[1] = q_data->sizeimage[1];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +
> > +	mtk_v4l2_debug(1,
> > +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> > +			ctx->idx, vq->type, *nplanes, *nbuffers,
> > +			sizes[0], sizes[1]);
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct mtk_q_data *q_data;
> > +	int i;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> > +			 ctx->idx, vb->vb2_queue->type, vb->index);
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> > +
> > +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> > +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> > +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> > +				       i, vb2_plane_size(vb, i),
> > +				       q_data->sizeimage[i]);
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> > +			ctx->idx, vb->vb2_queue->type,
> > +			vb->index, vb);
> > +	/*
> > +	 * check if this buffer is ready to be used after decode
> > +	 */
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		struct vb2_buffer *src_buf;
> > +		struct mtk_vcodec_mem buf;
> > +		bool res_chg = false;
> > +		int ret = 0;
> > +		int dpbsize = 1;
> > +
> > +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> > +
> > +		if (ctx->state == MTK_STATE_INIT) {
> > +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +			if (!src_buf) {
> > +				mtk_v4l2_err("No src buffer");
> > +				return;
> > +			}
> > +
> > +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> > +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> > +							src_buf,
> > +							0);
> > +			buf.size = (size_t)src_buf->planes[0].bytesused;
> > +			mtk_v4l2_debug(2,
> > +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> > +					ctx->idx,
> > +					src_buf->index,
> > +					buf.va, (u64)buf.dma_addr, buf.size);
> > +
> > +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> > +			if (ret) {
> > +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +						VB2_BUF_STATE_DONE);
> > +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> > +						ctx->idx,
> > +						src_buf->index,
> > +						buf.size, ret);
> > +				return;
> > +			}
> > +
> > +			if (vdec_if_get_param(ctx,
> > +						   GET_PARAM_PIC_INFO,
> > +						   &ctx->picinfo)) {
> > +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +						ctx->idx);
> > +				return;
> > +			}
> > +
> > +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> > +				sizeof(struct vdec_pic_info));
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +						ctx->picinfo.y_bs_sz +
> > +						ctx->picinfo.y_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> > +							ctx->picinfo.buf_w;
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> > +						ctx->picinfo.c_bs_sz +
> > +						ctx->picinfo.c_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> > +							ctx->picinfo.buf_w;
> > +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> > +				ctx->idx,
> > +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> > +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> > +
> > +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +			if (dpbsize == 0) {
> > +				mtk_v4l2_debug(2,
> > +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> > +						ctx->idx, ret);
> > +				dpbsize = 1;
> > +			}
> > +			ctx->dpb_count = dpbsize;
> > +			ctx->state = MTK_STATE_HEADER;
> > +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> > +							ctx->dpb_count);
> > +		} else {
> > +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> > +					 ctx->idx, ctx->state);
> > +		}
> > +
> > +	} else {
> > +		mutex_lock(&buf->lock);
> > +		if (buf->used == false) {
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> > +					to_vb2_v4l2_buffer(vb));
> > +			buf->queued_in_vb2 = true;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		} else {
> > +			buf->queued_in_vb2 = false;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		}
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> > +{
> > +	if (vb->vb2_queue->type ==
> > +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +		mutex_lock(&buf->lock);
> > +		buf->queued_in_v4l2 = false;
> > +		buf->queued_in_vb2 = false;
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> > +{
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		buf->used = false;
> > +		buf->ready_to_display = false;
> > +		buf->nonrealdisplay = false;
> > +		buf->queued_in_v4l2 = false;
> > +		mutex_init(&buf->lock);
> > +	} else {
> > +		buf->lastframe = false;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	if (ctx->state == MTK_STATE_FLUSH)
> > +		ctx->state = MTK_STATE_HEADER;
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> > +{
> > +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> > +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> > +
> > +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		while (src_buf) {
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +					VB2_BUF_STATE_ERROR);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		}
> > +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		int i;
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		if (ctx->state >= MTK_STATE_HEADER)
> > +			mtk_vdec_flush_decoder(ctx);
> > +
> > +		ctx->state = MTK_STATE_FLUSH;
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		while (dst_buf) {
> > +			vb2_set_plane_payload(dst_buf, 0, 0);
> > +			vb2_set_plane_payload(dst_buf, 1, 0);
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> > +				VB2_BUF_STATE_ERROR);
> > +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		}
> > +
> > +		for (i = 0; i < q->num_buffers; ++i) {
> > +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> > +				vb2_v4l2 = container_of(q->bufs[i],
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +				buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> > +						ctx->idx, i, q->type,
> > +						buf->queued_in_vb2,
> > +						buf->queued_in_v4l2,
> > +						(int)q->bufs[i]->state);
> > +				v4l2_m2m_buf_done(vb2_v4l2,
> > +						VB2_BUF_STATE_ERROR);
> > +			}
> > +		}
> > +	}
> > +}
> > +
> > +static void m2mops_vdec_device_run(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +
> > +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> > +}
> > +
> > +static int m2mops_vdec_job_ready(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT)
> > +		return 0;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> > +		return 0;
> > +
> > +	if (ctx->state != MTK_STATE_HEADER)
> > +		return 0;
> > +
> > +	return 1;
> > +}
> > +
> > +static void m2mops_vdec_job_abort(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +
> > +	ctx->state = MTK_STATE_ABORT;
> > +}
> > +
> > +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> > +
> > +	switch (ctrl->id) {
> > +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> > +		if (ctx->state >= MTK_STATE_HEADER) {
> > +			ctrl->val = ctx->dpb_count;
> > +		} else {
> > +			mtk_v4l2_err("Seqinfo not ready");
> > +			ctrl->val = 1;
> > +			return -EAGAIN;
> > +		}
> > +		break;
> > +	default:
> > +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> > +		return -EINVAL;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> > +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> > +};
> > +
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct v4l2_ctrl *ctrl;
> > +
> > +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> > +
> > +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> > +					  &mtk_vcodec_dec_ctrl_ops,
> > +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> > +					  1, 32, 1, 1);
> > +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> > +
> > +	if (ctx->ctrl_hdl.error) {
> > +		mtk_v4l2_err("Adding control failed %d",
> > +					ctx->ctrl_hdl.error);
> > +		return ctx->ctrl_hdl.error;
> > +	}
> > +
> > +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> > +	return 0;
> > +}
> > +
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> > +}
> > +
> > +static void m2mops_vdec_lock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_lock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +static void m2mops_vdec_unlock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_unlock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> > +	.device_run	= m2mops_vdec_device_run,
> > +	.job_ready	= m2mops_vdec_job_ready,
> > +	.job_abort	= m2mops_vdec_job_abort,
> > +	.lock	= m2mops_vdec_lock,
> > +	.unlock	= m2mops_vdec_unlock,
> > +};
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops = {
> > +	.queue_setup	= vb2ops_vdec_queue_setup,
> > +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> > +	.buf_queue	= vb2ops_vdec_buf_queue,
> > +	.wait_prepare	= vb2_ops_wait_prepare,
> > +	.wait_finish	= vb2_ops_wait_finish,
> > +	.buf_init	= vb2ops_vdec_buf_init,
> > +	.buf_finish	= vb2ops_vdec_buf_finish,
> > +	.start_streaming	= vb2ops_vdec_start_streaming,
> > +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> > +};
> > +
> > +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> > +	.vidioc_streamon	= vidioc_vdec_streamon,
> > +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> > +
> > +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> > +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> > +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> > +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> > +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> > +
> > +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> > +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> > +
> > +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> > +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> > +
> > +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> > +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> > +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> > +
> > +	.vidioc_querycap	= vidioc_vdec_querycap,
> > +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> > +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> > +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> > +};
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > new file mode 100644
> > index 0000000..cb5cb42
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > @@ -0,0 +1,81 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#ifndef _MTK_VCODEC_DEC_H_
> > +#define _MTK_VCODEC_DEC_H_
> > +
> > +#include <media/videobuf2-core.h>
> > +#include <media/videobuf2-v4l2.h>
> > +
> > +/**
> > + * struct vdec_fb  - decoder frame buffer
> > + * @base_y	: Y plane memory info
> > + * @base_c	: C plane memory info
> > + * @status      : frame buffer status (vdec_fb_status)
> > + */
> > +struct vdec_fb {
> > +	struct mtk_vcodec_mem	base_y;
> > +	struct mtk_vcodec_mem	base_c;
> > +	unsigned int	status;
> > +};
> > +
> > +/**
> > + * struct mtk_video_buf - Private data related to each VB2 buffer.
> > + * @b:			VB2 buffer
> > + * @list:			link list
> > + * @used:		Output buffer contain decoded frame data
> > + * @ready_to_display:	Output buffer not display yet
> > + * @nonrealdisplay:	Output buffer is not display frame
> > + * @queued_in_vb2:	Output buffer is queue in vb2
> > + * @queued_in_v4l2:	Output buffer is in v4l2
> > + * @lastframe:		Intput buffer is last buffer - EOS
> > + * @frame_buffer:		Decode status of output buffer
> > + * @lock:			V4L2 and decode thread should get mutex
> > + *			before r/w info in mtk_video_buf
> > + */
> > +struct mtk_video_buf {
> > +	struct vb2_v4l2_buffer	vb;
> > +	struct list_head	list;
> > +
> > +	bool	used;
> > +	bool	ready_to_display;
> > +	bool	nonrealdisplay;
> > +	bool	queued_in_vb2;
> > +	bool	queued_in_v4l2;
> > +	bool	lastframe;
> > +	struct vdec_fb	frame_buffer;
> > +	struct mutex	lock;
> > +};
> > +
> > +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> > +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> > +
> > +
> > +/*
> > + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> > + * get/release lock before/after access decoder hw.
> > + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> > + * to ctx instance that get lock
> > + */
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq);
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> > +
> > +#endif /* _MTK_VCODEC_DEC_H_ */
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > new file mode 100644
> > index 0000000..34e3217
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > @@ -0,0 +1,469 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <linux/interrupt.h>
> > +#include <linux/irq.h>
> > +#include <linux/module.h>
> > +#include <linux/of_device.h>
> > +#include <linux/of.h>
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vpu.h"
> > +
> > +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> > +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> > +
> > +#define VDEC_HW_ACTIVE	0x10
> > +#define VDEC_IRQ_CFG	0x11
> > +#define VDEC_IRQ_CLR	0x10
> > +#define VDEC_IRQ_CFG_REG	0xa4
> > +
> > +
> > +/* Wake up context wait_queue */
> > +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> > +{
> > +	ctx->int_cond = 1;
> > +	ctx->int_type = reason;
> > +	wake_up_interruptible(&ctx->queue);
> > +}
> > +
> > +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	unsigned long flags;
> > +	struct mtk_vcodec_ctx *ctx;
> > +	unsigned int cg_status = 0;
> > +	unsigned int dec_done_status = 0;
> > +
> > +	spin_lock_irqsave(&dev->irqlock, flags);
> > +	ctx = dev->curr_ctx;
> > +	spin_unlock_irqrestore(&dev->irqlock, flags);
> > +
> > +	cg_status = readl(dev->reg_base[0] + (0));
> > +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> > +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> > +			       cg_status);
> > +		return IRQ_HANDLED;
> > +	}
> > +
> > +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	ctx->irq_status = dec_done_status;
> > +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> > +		return IRQ_HANDLED;
> > +
> > +	/* clear interrupt */
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +
> > +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> > +			ctx->idx, dec_done_status);
> > +
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int fops_vcodec_open(struct file *file)
> > +{
> > +	struct video_device *vfd = video_devdata(file);
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = NULL;
> > +	int ret = 0;
> > +
> > +	if (dev->instance_mask == ~0UL) {
> > +		mtk_v4l2_err("Too many open contexts");
> > +		ret = -EBUSY;
> > +		goto err_alloc;
> > +	}
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +
> > +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> > +	if (!ctx) {
> > +		ret = -ENOMEM;
> > +		goto err_alloc;
> > +	}
> > +
> > +	ctx->idx = ffz(dev->instance_mask);
> > +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> > +	file->private_data = &ctx->fh;
> > +	v4l2_fh_add(&ctx->fh);
> > +	INIT_LIST_HEAD(&ctx->list);
> > +	ctx->dev = dev;
> > +	init_waitqueue_head(&ctx->queue);
> > +
> > +	if (vfd == dev->vfd_dec) {
> > +		ctx->type = MTK_INST_DECODER;
> > +		ret = mtk_vdec_ctrls_setup(ctx);
> > +		if (ret) {
> > +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> > +			goto err_ctrls_setup;
> > +		}
> > +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> > +						 &m2mctx_vdec_queue_init);
> > +		if (IS_ERR(ctx->m2m_ctx)) {
> > +			ret = PTR_ERR(ctx->m2m_ctx);
> > +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> > +				       ret);
> > +			goto err_ctx_init;
> > +		}
> > +		mtk_vcodec_dec_set_default_params(ctx);
> > +	} else {
> > +		mtk_v4l2_err("Invalid vfd !");
> > +		ret = -ENOENT;
> > +		goto err_ctx_init;
> > +	}
> > +
> > +	if (v4l2_fh_is_singular(&ctx->fh)) {
> > +		mtk_vcodec_dec_pw_on(&dev->pm);
> > +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> > +		if (ret < 0) {
> > +			mtk_v4l2_err("vpu_load_firmware failed!");
> > +			goto err_load_fw;
> > +		}
> > +
> > +		dev->dec_capability =
> > +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> > +		mtk_v4l2_debug(0, "decoder capability %x",
> > +			       dev->dec_capability);
> > +	}
> > +
> > +	set_bit(ctx->idx, &dev->instance_mask);
> > +	dev->num_instances++;
> > +	list_add(&ctx->list, &dev->ctx_list);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> > +					ctx->idx);
> > +
> > +	return ret;
> > +
> > +	/* Deinit when failure occurred */
> > +err_load_fw:
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +err_ctx_init:
> > +	mtk_vdec_ctrls_free(ctx);
> > +err_ctrls_setup:
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +err_alloc:
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_release(struct file *file)
> > +{
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +	mtk_vcodec_vdec_release(ctx);
> > +	mtk_vdec_ctrls_free(ctx);
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	list_del_init(&ctx->list);
> > +	dev->num_instances--;
> > +	if (dev->num_instances == 0)
> > +		mtk_vcodec_dec_pw_off(&dev->pm);
> > +	clear_bit(ctx->idx, &dev->instance_mask);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	return 0;
> > +}
> > +
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 
Got it, will fix this in next version.


best regards,
Tiffany

> > +};
> > +
> > +static void mtk_vcodec_dec_reset_handler(void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	struct mtk_vcodec_ctx *ctx;
> > +
> > +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> > +		ctx->state = MTK_STATE_ABORT;
> > +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> > +					   ctx->idx);
> > +	}
> > +	mutex_unlock(&dev->dev_mutex);
> > +}
> > +
> > +static int mtk_vcodec_probe(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev;
> > +	struct video_device *vfd_dec;
> > +	struct resource *res;
> > +	int i, ret;
> > +	int reg_base_num;
> > +
> > +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> > +	if (!dev)
> > +		return -ENOMEM;
> > +
> > +	INIT_LIST_HEAD(&dev->ctx_list);
> > +	dev->plat_dev = pdev;
> > +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> > +	if (dev->vpu_plat_dev == NULL) {
> > +		mtk_v4l2_err("[VPU] vpu device in not ready");
> > +		return -EPROBE_DEFER;
> > +	}
> > +
> > +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> > +				dev, VPU_RST_DEC);
> > +
> > +	ret = mtk_vcodec_init_dec_pm(dev);
> > +	if (ret < 0) {
> > +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> > +		return ret;
> > +	}
> > +
> > +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> > +	for (i = 0; i < reg_base_num; i++) {
> > +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> > +		if (res == NULL) {
> > +			dev_err(&pdev->dev, "get memory resource failed.");
> > +			ret = -ENXIO;
> > +			goto err_res;
> > +		}
> > +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> > +		if (IS_ERR(dev->reg_base[i])) {
> > +			dev_err(&pdev->dev,
> > +				"devm_ioremap_resource %d failed.", i);
> > +			ret = PTR_ERR(dev->reg_base);
> > +			goto err_res;
> > +		}
> > +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> > +	}
> > +
> > +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> > +	if (res == NULL) {
> > +		dev_err(&pdev->dev, "failed to get irq resource");
> > +		ret = -ENOENT;
> > +		goto err_res;
> > +	}
> > +
> > +	dev->dec_irq = platform_get_irq(pdev, 0);
> > +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> > +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> > +	if (ret) {
> > +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> > +			dev->dec_irq,
> > +			ret);
> > +		ret = -EINVAL;
> > +		goto err_res;
> > +	}
> > +
> > +	disable_irq(dev->dec_irq);
> > +	mutex_init(&dev->dev_mutex);
> > +	mutex_init(&dev->dec_mutex);
> > +	spin_lock_init(&dev->irqlock);
> > +
> > +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> > +		 "[MTK_V4L2]");
> > +
> > +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> > +	if (ret) {
> > +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> > +		return ret;
> > +	}
> > +
> > +	init_waitqueue_head(&dev->queue);
> > +
> > +	vfd_dec = video_device_alloc();
> > +	if (!vfd_dec) {
> > +		mtk_v4l2_err("Failed to allocate video device");
> > +		ret = -ENOMEM;
> > +		goto err_dec_alloc;
> > +	}
> > +	vfd_dec->fops		= &mtk_vcodec_fops;
> > +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> > +	vfd_dec->release	= video_device_release;
> > +	vfd_dec->lock		= &dev->dev_mutex;
> > +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> > +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> > +
> > +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> > +		 MTK_VCODEC_DEC_NAME);
> > +	video_set_drvdata(vfd_dec, dev);
> > +	dev->vfd_dec = vfd_dec;
> > +	platform_set_drvdata(pdev, dev);
> > +
> > +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> > +	if (IS_ERR(dev->alloc_ctx)) {
> > +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> > +		ret = PTR_ERR(dev->alloc_ctx);
> > +		goto err_vb2_ctx_init;
> > +	}
> > +
> > +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> > +	if (IS_ERR(dev->m2m_dev_dec)) {
> > +		mtk_v4l2_err("Failed to init mem2mem dec device");
> > +		ret = PTR_ERR(dev->m2m_dev_dec);
> > +		goto err_dec_mem_init;
> > +	}
> > +
> > +	dev->decode_workqueue =
> > +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> > +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> > +	if (!dev->decode_workqueue) {
> > +		mtk_v4l2_err("Failed to create decode workqueue");
> > +		ret = -EINVAL;
> > +		goto err_event_workq;
> > +	}
> > +
> > +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to register video device");
> > +		goto err_dec_reg;
> > +	}
> > +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> > +			 vfd_dec->num);
> > +
> > +	return 0;
> > +
> > +err_dec_reg:
> > +	destroy_workqueue(dev->decode_workqueue);
> > +err_event_workq:
> > +	v4l2_m2m_release(dev->m2m_dev_dec);
> > +err_dec_mem_init:
> > +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +err_vb2_ctx_init:
> > +	video_unregister_device(vfd_dec);
> > +err_dec_alloc:
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +err_res:
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return ret;
> > +}
> > +
> > +static const struct of_device_id mtk_vcodec_match[] = {
> > +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> > +	{},
> > +};
> > +
> > +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> > +
> > +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug_enter();
> > +	flush_workqueue(dev->decode_workqueue);
> > +	destroy_workqueue(dev->decode_workqueue);
> > +	if (dev->m2m_dev_dec)
> > +		v4l2_m2m_release(dev->m2m_dev_dec);
> > +	if (dev->alloc_ctx)
> > +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +
> > +	if (dev->vfd_dec)
> > +		video_unregister_device(dev->vfd_dec);
> > +
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> > +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> > +			   mtk_vcodec_vdec_pm_runtime_resume,
> > +			   NULL)
> > +};
> > +
> > +static struct platform_driver mtk_vcodec_dec_driver = {
> > +	.probe	= mtk_vcodec_probe,
> > +	.remove	= mtk_vcodec_dec_remove,
> > +	.driver	= {
> > +		.name	= MTK_VCODEC_DEC_NAME,
> > +		.owner	= THIS_MODULE,
> > +		.of_match_table = mtk_vcodec_match,
> > +		.pm = &mtk_vcodec_vdec_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(mtk_vcodec_dec_driver);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
> 
> Regards,
> 
> 	Hans
> 

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  4:00           ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  4:00 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Hans,

On Fri, 2016-04-15 at 16:27 +0200, Hans Verkuil wrote:
> On 04/13/2016 02:01 PM, Tiffany Lin wrote:
> > Add v4l2 layer decoder driver for MT8173
> > 
> > Signed-off-by: Tiffany Lin <tiffany.lin@mediatek.com>
> > ---
> >  drivers/media/platform/mtk-vcodec/Makefile         |   10 +-
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c | 1429 ++++++++++++++++++++
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h |   81 ++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c |  469 +++++++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c  |  153 +++
> >  .../media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h  |   28 +
> >  drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h |   98 +-
> >  drivers/media/platform/mtk-vcodec/vdec_drv_base.h  |   56 +
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.c    |  113 ++
> >  drivers/media/platform/mtk-vcodec/vdec_drv_if.h    |   93 ++
> >  drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h   |   86 ++
> >  11 files changed, 2596 insertions(+), 20 deletions(-)
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_pm.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_base.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.c
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_drv_if.h
> >  create mode 100644 drivers/media/platform/mtk-vcodec/vdec_ipi_msg.h
> > 
> > diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
> > index dc5cb00..4c8ed2f 100644
> > --- a/drivers/media/platform/mtk-vcodec/Makefile
> > +++ b/drivers/media/platform/mtk-vcodec/Makefile
> > @@ -1,7 +1,13 @@
> >  
> >  
> > -obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
> > -
> > +obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dec.o \
> > +				       mtk-vcodec-enc.o \
> > +				       mtk-vcodec-common.o
> > +
> > +mtk-vcodec-dec-y := mtk_vcodec_dec_drv.o \
> > +		vdec_drv_if.o \
> > +		mtk_vcodec_dec.o \
> > +		mtk_vcodec_dec_pm.o \
> >  
> >  
> >  mtk-vcodec-enc-y := venc/venc_vp8_if.o \
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > new file mode 100644
> > index 0000000..0499413
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
> > @@ -0,0 +1,1429 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +
> > +static struct mtk_video_fmt mtk_video_formats[] = {
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_H264,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP8,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.type = MTK_FMT_DEC,
> > +		.num_planes	= 1,
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_MT21,
> > +		.type = MTK_FMT_FRAME,
> > +		.num_planes = 2,
> > +	},
> > +};
> > +#define OUT_FMT_IDX	0
> > +#define CAP_FMT_IDX	3
> > +
> > +#define VCODEC_CAPABILITY_4K_DISABLED	0x10
> > +#define VCODEC_DEC_4K_CODED_WIDTH	4096U
> > +#define VCODEC_DEC_4K_CODED_HEIGHT	2304U
> > +
> > +#define MTK_VDEC_MIN_W	64U
> > +#define MTK_VDEC_MIN_H	64U
> > +#define MTK_VDEC_MAX_W	2048U
> > +#define MTK_VDEC_MAX_H	1088U
> > +#define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
> > +#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
> > +
> > +static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_H264,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc	= V4L2_PIX_FMT_VP8,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +	{
> > +		.fourcc = V4L2_PIX_FMT_VP9,
> > +		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
> > +					MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
> > +	},
> > +};
> > +
> > +#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
> > +#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
> > +
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops;
> > +
> > +static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	unsigned int k;
> > +
> > +	for (k = 0; k < NUM_FORMATS; k++) {
> > +		fmt = &mtk_video_formats[k];
> > +		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
> > +			return fmt;
> > +	}
> > +
> > +	return NULL;
> > +}
> > +
> > +static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
> > +					      enum v4l2_buf_type type)
> > +{
> > +	if (V4L2_TYPE_IS_OUTPUT(type))
> > +		return &ctx->q_data[MTK_Q_DATA_SRC];
> > +
> > +	return &ctx->q_data[MTK_Q_DATA_DST];
> > +}
> > +
> > +static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vdec_fb *disp_frame_buffer;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_DISP_FRAME_BUFFER,
> > +				   &disp_frame_buffer)) {
> > +		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
> > +			     ctx->idx);
> > +		return NULL;
> > +	}
> > +
> > +	if (disp_frame_buffer == NULL) {
> > +		mtk_v4l2_debug(3, "No display frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = disp_frame_buffer;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
> > +							ctx->picinfo.y_bs_sz);
> > +		vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
> > +							ctx->picinfo.c_bs_sz);
> > +
> > +		dstbuf->ready_to_display = true;
> > +		dstbuf->nonrealdisplay = false;
> > +		mtk_v4l2_debug(2,
> > +			       "[%d]status=%x queue idx=%d to done_list %d",
> > +			       ctx->idx, frame_buffer->status,
> > +			       dstbuf->vb.vb2_buf.index,
> > +			       dstbuf->queued_in_vb2);
> > +
> > +		v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
> > +		ctx->decoded_frame_cnt++;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	void *tmp_frame_addr;
> > +	struct vb2_queue *vq;
> > +	struct mtk_video_buf *dstbuf;
> > +	struct vdec_fb *frame_buffer;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_FREE_FRAME_BUFFER,
> > +				   &tmp_frame_addr)) {
> > +		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->idx);
> > +		return NULL;
> > +	}
> > +	if (tmp_frame_addr == NULL) {
> > +		mtk_v4l2_debug(3, " No free frame buffer");
> > +		return NULL;
> > +	}
> > +
> > +	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
> > +			 ctx->idx, tmp_frame_addr);
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
> > +	if (!vq) {
> > +		mtk_v4l2_err("Cannot find V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE queue!");
> > +		return NULL;
> > +	}
> > +
> > +	frame_buffer = tmp_frame_addr;
> > +	dstbuf = container_of(frame_buffer, struct mtk_video_buf, frame_buffer);
> > +
> > +	mutex_lock(&dstbuf->lock);
> > +	if (dstbuf->used) {
> > +		if ((dstbuf->queued_in_vb2) &&
> > +		    (dstbuf->queued_in_v4l2) &&
> > +		    (frame_buffer->status == FB_ST_FREE)) {
> > +			mtk_v4l2_debug(2,
> > +				"[%d]status=%x queue idx=%d to rdy_queue %d",
> > +				 ctx->idx, frame_buffer->status,
> > +				 dstbuf->vb.vb2_buf.index,
> > +				 dstbuf->queued_in_vb2);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +		} else if ((dstbuf->queued_in_vb2 == false) &&
> > +			   (dstbuf->queued_in_v4l2 == true)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]status=%x queue idx=%d to rdy_queue",
> > +					 ctx->idx, frame_buffer->status,
> > +					 dstbuf->vb.vb2_buf.index);
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
> > +			dstbuf->queued_in_vb2 = true;
> > +		} else {
> > +			mtk_v4l2_debug(2,
> > +					"[%d]status=%x err queue idx=%d %d %d",
> > +					ctx->idx, frame_buffer->status,
> > +					dstbuf->vb.vb2_buf.index,
> > +					dstbuf->queued_in_vb2,
> > +					dstbuf->queued_in_v4l2);
> > +		}
> > +		dstbuf->used = false;
> > +	}
> > +	mutex_unlock(&dstbuf->lock);
> > +	return &dstbuf->vb.vb2_buf;
> > +}
> > +
> > +static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_display_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct vb2_buffer *framptr;
> > +
> > +	do {
> > +		framptr = get_free_buffer(ctx);
> > +	} while (framptr);
> > +}
> > +
> > +static int mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	static const struct v4l2_event ev_src_ch = {
> > +		.type = V4L2_EVENT_SOURCE_CHANGE,
> > +		.u.src_change.changes =
> > +		V4L2_EVENT_SRC_CH_RESOLUTION,
> > +	};
> > +
> > +	mtk_v4l2_debug(1, "[%d]", ctx->idx);
> > +	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
> > +
> > +	return 0;
> > +}
> > +
> > +static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	bool res_chg;
> > +	int ret = 0;
> > +
> > +	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
> > +	if (ret)
> > +		mtk_v4l2_err("DecodeFinal failed");
> > +
> > +	clean_display_buffer(ctx);
> > +	clean_free_buffer(ctx);
> > +}
> > +
> > +static bool mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	int dpbsize;
> > +
> > +	if (vdec_if_get_param(ctx,
> > +				   GET_PARAM_PIC_INFO,
> > +				   &ctx->last_decoded_picinfo)) {
> > +		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +				ctx->idx);
> > +		return false;
> > +	}
> > +
> > +	if (ctx->last_decoded_picinfo.pic_w == 0 ||
> > +		ctx->last_decoded_picinfo.pic_h == 0 ||
> > +		ctx->last_decoded_picinfo.buf_w == 0 ||
> > +		ctx->last_decoded_picinfo.buf_h == 0)
> > +		return false;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) {
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
> > +				 ctx->idx, ctx->last_decoded_picinfo.pic_w,
> > +				 ctx->last_decoded_picinfo.pic_h,
> > +				 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				 ctx->last_decoded_picinfo.buf_w,
> > +				 ctx->last_decoded_picinfo.buf_h);
> > +
> > +		vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +		if (dpbsize == 0)
> > +			dpbsize = 1;
> > +		ctx->dpb_count = dpbsize;
> > +		return true;
> > +	}
> > +
> > +	return false;
> > +}
> > +
> > +void mtk_vdec_worker(struct work_struct *work)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
> > +				    decode_work);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	struct vb2_buffer *src_buf, *dst_buf;
> > +	struct mtk_vcodec_mem buf;
> > +	struct vdec_fb *pfb;
> > +	bool res_chg = false;
> > +	int ret;
> > +	struct mtk_video_buf *dst_buf_info, *src_buf_info;
> > +	struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
> > +
> > +	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +	if (src_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
> > +	if (dst_buf == NULL) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->idx);
> > +		return;
> > +	}
> > +
> > +	src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
> > +	dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	buf.va = vb2_plane_vaddr(src_buf, 0);
> > +	buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
> > +	buf.size = (unsigned int)src_buf->planes[0].bytesused;
> > +	if (!buf.va) {
> > +		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +		mtk_v4l2_err("[%d] idx=%d src_addr is NULL!!",
> > +			       ctx->idx, src_buf->index);
> > +		return;
> > +	}
> > +
> > +	pfb = &dst_buf_info->frame_buffer;
> > +	pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
> > +	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
> > +	pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
> > +
> > +	pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
> > +	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
> > +	pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
> > +	pfb->status = 0;
> > +	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->idx);
> > +	mtk_v4l2_debug(3,
> > +			 "[%d] Bitstream VA=%p DMA=%llx Size=0x%lx vb=%p",
> > +			 ctx->idx, buf.va,
> > +			 (u64)buf.dma_addr,
> > +			 buf.size, src_buf);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"idx=%d Framebuf  pfb=%p VA=%p Y_DMA=%llx C_DMA=%llx Size=0x%lx",
> > +			dst_buf->index, pfb,
> > +			pfb->base_y.va, (u64)pfb->base_y.dma_addr,
> > +			(u64)pfb->base_c.dma_addr, pfb->base_y.size);
> > +
> > +	if (src_buf_info->lastframe == true) {
> > +		/* update src buf status */
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		src_buf_info->lastframe = false;
> > +		v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
> > +
> > +		/* update dst buf status */
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		dst_buf_info->used = false;
> > +
> > +		clean_display_buffer(ctx);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
> > +		vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
> > +		v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
> > +		clean_free_buffer(ctx);
> > +	} else {
> > +
> > +		dst_buf_info->vb.timestamp
> > +					= src_buf_info->vb.timestamp;
> > +		dst_buf_info->vb.timecode
> > +					= src_buf_info->vb.timecode;
> > +		mutex_lock(&dst_buf_info->lock);
> > +		dst_buf_info->used = true;
> > +		mutex_unlock(&dst_buf_info->lock);
> > +		src_buf_info->used = true;
> > +
> > +		ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
> > +
> > +		if (ret) {
> > +			mtk_v4l2_err(
> > +				" <===[%d], src_buf[%d]%d sz=0x%zx pts=(%lu %lu) dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
> > +				ctx->idx,
> > +				src_buf->index,
> > +				src_buf_info->lastframe,
> > +				buf.size,
> > +				src_buf_info->vb.timestamp.tv_sec,
> > +				src_buf_info->vb.timestamp.tv_usec,
> > +				dst_buf->index,
> > +				ret, res_chg);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +					VB2_BUF_STATE_ERROR);
> > +		} else if (res_chg == false) {
> > +			/* we only return src buffer with VB2_BUF_STATE_DONE
> > +			  * when decode success without resolution change
> > +			  */
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +			v4l2_m2m_buf_done(&src_buf_info->vb,
> > +							VB2_BUF_STATE_DONE);
> > +		}
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		clean_display_buffer(ctx);
> > +		clean_free_buffer(ctx);
> > +
> > +		if ((ret == 0) && (res_chg == true)) {
> > +			mtk_vdec_pic_info_update(ctx);
> > +			/*
> > +			  * On encountering a resolution change in the stream.
> > +			  * The driver must first process and decode all
> > +			  * remaining buffers from before the resolution change
> > +			  * point, so call flush decode here
> > +			  */
> > +			mtk_vdec_flush_decoder(ctx);
> > +			/*
> > +			  * After all buffers containing decoded frames from
> > +			  * before the resolution change point ready to be
> > +			  * dequeued on the CAPTURE queue, the driver sends a
> > +			  * V4L2_EVENT_SOURCE_CHANGE event for source change
> > +			  * type V4L2_EVENT_SRC_CH_RESOLUTION
> > +			  */
> > +			mtk_vdec_queue_res_chg_event(ctx);
> > +		}
> > +	}
> > +
> > +	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
> > +
> > +}
> > +
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_unlock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	mutex_lock(&ctx->dev->dec_mutex);
> > +	return 0;
> > +}
> > +
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	int ret = 0;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
> > +	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	src_vq->drv_priv	= ctx;
> > +	src_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	src_vq->ops		= &mtk_vdec_vb2_ops;
> > +	src_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	src_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(src_vq);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
> > +		return ret;
> > +	}
> > +	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
> > +	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
> > +	dst_vq->drv_priv	= ctx;
> > +	dst_vq->buf_struct_size = sizeof(struct mtk_video_buf);
> > +	dst_vq->ops		= &mtk_vdec_vb2_ops;
> > +	dst_vq->mem_ops		= &vb2_dma_contig_memops;
> > +	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
> > +	dst_vq->lock = &ctx->dev->dev_mutex;
> > +
> > +	ret = vb2_queue_init(dst_vq);
> > +	if (ret) {
> > +		vb2_queue_release(src_vq);
> > +		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
> > +	}
> > +
> > +	return ret;
> > +}
> > +
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	vdec_if_deinit(ctx);
> > +	ctx->state = MTK_STATE_FREE;
> > +}
> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 
Got it, will fix these to v4l2_m2m_ioctl_*.

> > +
> > +static int vidioc_vdec_querybuf(struct file *file, void *priv,
> > +				struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d", ctx->idx,
> > +			 buf->type, buf->index);
> > +	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_qbuf(struct file *file, void *priv,
> > +			    struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct vb2_queue *vq;
> > +	struct vb2_buffer *vb;
> > +	struct mtk_video_buf *mtkbuf;
> > +	struct vb2_v4l2_buffer	*vb2_v4l2;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
> > +	vb = vq->bufs[buf->index];
> > +	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +	mtkbuf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +
> > +	if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    (buf->m.planes[0].bytesused == 0)) {
> > +		mtkbuf->lastframe = true;
> > +		mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
> > +			 ctx->idx, buf->type, buf->index,
> > +			 mtkbuf->lastframe, buf->bytesused,
> > +			 buf->m.planes[0].bytesused, buf->length,
> > +			 vb);
> > +	}
> > +
> > +	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_dqbuf(struct file *file, void *priv,
> > +			     struct v4l2_buffer *buf)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state == MTK_STATE_ABORT) {
> > +		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
> > +					ctx->idx);
> > +		return -EIO;
> > +	}
> > +
> > +	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
> > +}
> > +
> > +static int vidioc_vdec_expbuf(struct file *file, void *priv,
> > +			      struct v4l2_exportbuffer *eb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
> > +}
> > +
> > +static int vidioc_vdec_querycap(struct file *file, void *priv,
> > +				struct v4l2_capability *cap)
> > +{
> > +	strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
> > +	strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
> > +	strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
> > +	cap->device_caps  = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
> > +	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
> 
> See my comment about the new device_caps field in struct video_device from my
> review of the encoder driver.
> 
Got it, will fix this in next version.


> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_unsubscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_event_unsubscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> 
> No need for this function, just use v4l2_event_unsubscribe.
> 
Got it, will fix this.

> > +
> > +static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
> > +				     const struct v4l2_event_subscription *sub)
> > +{
> > +	switch (sub->type) {
> > +	case V4L2_EVENT_EOS:
> > +		return v4l2_event_subscribe(fh, sub, 2, NULL);
> > +	case V4L2_EVENT_SOURCE_CHANGE:
> > +		return v4l2_src_change_event_subscribe(fh, sub);
> > +	default:
> > +		return -EINVAL;
> 
> Can't you just call v4l2_ctrl_subscribe_event() in the default case?
> 
Got it, we will call v4l2_ctrl_subscribe_event() in the default case.

> > +	}
> > +}
> > +
> > +static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
> > +				struct v4l2_format *f)
> > +{
> > +	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (!fmt) {
> > +		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
> > +		fmt = mtk_vdec_find_format(f);
> > +	}
> > +
> > +	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
> > +		mtk_v4l2_err("sizeimage of output format must be given");
> > +		return -EINVAL;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_g_crop(struct file *file, void *priv,
> > +			 struct v4l2_crop *cr)
> 
> Don't use g_crop, use g_selection instead. The selection API is a superset
> of the old crop API.
> 
Will remove this in next version.


> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (ctx->state < MTK_STATE_HEADER)
> > +		return -EINVAL;
> > +
> > +	if ((ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_H264) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP8) ||
> > +	    (ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc == V4L2_PIX_FMT_VP9)) {
> > +
> > +		if (vdec_if_get_param(ctx,
> > +					   GET_PARAM_CROP_INFO,
> > +					   cr)) {
> > +			mtk_v4l2_debug(2,
> > +					 "[%d]Error!! Cannot get param : GET_PARAM_CROP_INFO ERR",
> > +					 ctx->idx);
> > +			cr->c.left = 0;
> > +			cr->c.top = 0;
> > +			cr->c.width = ctx->picinfo.buf_w;
> > +			cr->c.height = ctx->picinfo.buf_h;
> > +		}
> > +		mtk_v4l2_debug(2, "Cropping info: l=%d t=%d w=%d h=%d",
> > +				 cr->c.left, cr->c.top, cr->c.width,
> > +				 cr->c.height);
> > +	} else {
> > +		cr->c.left = 0;
> > +		cr->c.top = 0;
> > +		cr->c.width = ctx->picinfo.pic_w;
> > +		cr->c.height = ctx->picinfo.pic_h;
> > +		mtk_v4l2_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d",
> > +				 cr->c.width, cr->c.height, ctx->picinfo.buf_w,
> > +				 ctx->picinfo.buf_h);
> > +	}
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_s_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +	int ret;
> > +	struct mtk_video_fmt *fmt;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	fmt = mtk_vdec_find_format(f);
> > +	if (fmt == NULL) {
> > +		mtk_v4l2_err("mtk_venc_find_format fail");
> > +		return ret;
> > +	}
> > +
> > +	pix_mp = &f->fmt.pix_mp;
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
> > +		mtk_v4l2_err("out_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
> > +		mtk_v4l2_err("cap_q_ctx buffers already requested");
> > +		ret = -EBUSY;
> > +	}
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq)
> > +		return -EINVAL;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +	if (!q_data)
> > +		return -EINVAL;
> > +
> > +	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		q_data->fmt = fmt;
> > +		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
> > +		pix_mp->plane_fmt[0].bytesperline = 0;
> > +		pix_mp->width = 0;
> > +		pix_mp->height = 0;
> > +
> > +		if (ctx->state == MTK_STATE_FREE) {
> > +			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
> > +			if (ret) {
> > +				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
> > +					       ctx->idx, ret);
> > +				return -EINVAL;
> > +			}
> > +			ctx->state = MTK_STATE_INIT;
> > +		}
> > +	} else {
> > +		q_data->fmt = fmt;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_enum_framesizes(struct file *file, void *priv,
> > +				struct v4l2_frmsizeenum *fsize)
> > +{
> > +	int i = 0;
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	if (fsize->index != 0)
> > +		return -EINVAL;
> > +
> > +	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
> > +		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
> > +			continue;
> > +
> > +		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> > +		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
> > +		if (!(ctx->dev->dec_capability &
> > +				VCODEC_CAPABILITY_4K_DISABLED)) {
> > +			mtk_v4l2_debug(1, "4K is enabled");
> > +			fsize->stepwise.max_width =
> > +					VCODEC_DEC_4K_CODED_WIDTH;
> > +			fsize->stepwise.max_height =
> > +					VCODEC_DEC_4K_CODED_HEIGHT;
> > +		}
> > +		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
> > +					ctx->dev->dec_capability,
> > +					fsize->stepwise.min_width,
> > +					fsize->stepwise.max_width,
> > +					fsize->stepwise.step_width,
> > +					fsize->stepwise.min_height,
> > +					fsize->stepwise.max_height,
> > +					fsize->stepwise.step_height);
> > +		return 0;
> > +	}
> > +
> > +	return -EINVAL;
> > +
> > +}
> > +
> > +static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
> > +{
> > +	struct mtk_video_fmt *fmt;
> > +	int i, j = 0;
> > +
> > +	for (i = 0; i < NUM_FORMATS; i++) {
> > +		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
> > +			continue;
> > +		if (!output_queue &&
> > +			(mtk_video_formats[i].type != MTK_FMT_FRAME))
> > +			continue;
> > +
> > +		if (j == f->index)
> > +			break;
> > +		++j;
> > +	}
> > +
> > +	if (i == NUM_FORMATS)
> > +		return -EINVAL;
> > +
> > +	fmt = &mtk_video_formats[i];
> > +	f->pixelformat = fmt->fourcc;
> > +	f->flags = 0;
> 
> No need.
> 
Will fix this in next version.

> > +	memset(f->reserved, 0, sizeof(f->reserved));
> 
> No need. The core will automatically set flags and reserved.
> 
Got it, will fix this in next version.
> > +
> > +	if (mtk_video_formats[i].type != MTK_FMT_DEC)
> > +		f->flags |= V4L2_FMT_FLAG_COMPRESSED;
> 
> No need for this, this is automatically done for you.
> 
Got it, will fix this in next version.

> Note: to support VP9 the DocBook documentation has to be updated and so does v4l_fill_fmtdesc()
> in drivers/media/v4l2-core/v4l2-ioctl.c
> 
Got it, google will help upstream these part.

> > +
> > +	return 0;
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, false);
> > +}
> > +
> > +static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
> > +					       struct v4l2_fmtdesc *f)
> > +{
> > +	return vidioc_enum_fmt(f, true);
> > +}
> > +
> > +static int vidioc_vdec_g_fmt(struct file *file, void *priv,
> > +			     struct v4l2_format *f)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
> > +	struct vb2_queue *vq;
> > +	struct mtk_q_data *q_data;
> > +
> > +	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
> > +	if (!vq) {
> > +		mtk_v4l2_err("no vb2 queue for type=%d",
> > +					   f->type);
> > +		return -EINVAL;
> > +	}
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, f->type);
> > +
> > +	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
> > +	    (ctx->state >= MTK_STATE_HEADER)) {
> > +		/* previous decode might have resolution change that make
> > +		 * picinfo changed update context'x picinfo here to make sure
> > +		 * we know what report to user space
> > +		 */
> > +		memcpy(&ctx->picinfo, &ctx->last_decoded_picinfo,
> > +			sizeof(struct vdec_pic_info));
> > +		q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
> > +					ctx->picinfo.y_len_sz;
> > +		q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
> > +					ctx->picinfo.c_len_sz;
> > +		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
> > +		q_data->coded_width = ctx->picinfo.buf_w;
> > +		q_data->coded_height = ctx->picinfo.buf_h;
> > +
> > +		/*
> > +		 * Width and height are set to the dimensions
> > +		 * of the movie, the buffer is bigger and
> > +		 * further processing stages should crop to this
> > +		 * rectangle.
> > +		 */
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +
> > +		/*
> > +		 * Set pixelformat to the format in which mt vcodec
> > +		 * outputs the decoded frame
> > +		 */
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		/*
> > +		 * This is run on OUTPUT
> > +		 * The buffer contains compressed image
> > +		 * so width and height have no meaning.
> > +		 * Assign value here to pass v4l2-compliance test
> > +		 */
> > +		pix_mp->width = q_data->visible_width;
> > +		pix_mp->height = q_data->visible_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +	} else {
> > +		pix_mp->width = q_data->coded_width;
> > +		pix_mp->height = q_data->coded_height;
> > +		pix_mp->field = V4L2_FIELD_NONE;
> > +		pix_mp->num_planes = q_data->fmt->num_planes;
> > +		pix_mp->pixelformat = q_data->fmt->fourcc;
> > +		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
> > +		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
> > +		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
> > +		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
> > +
> > +		mtk_v4l2_debug(1, "[%d] state=%d Format could not be read %d, not ready yet!",
> > +				ctx->idx, ctx->state, f->type);
> > +		return -EAGAIN;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
> > +				   const void *parg,
> 
> Just like the encoder driver this patch needs to be rebased. The latest code no
> longer has the parg argument.
> 
Got it, will fix this in next version.

> > +				   unsigned int *nbuffers,
> > +				   unsigned int *nplanes,
> > +				   unsigned int sizes[], void *alloc_ctxs[])
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
> > +	struct mtk_q_data *q_data;
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vq->type);
> > +
> > +	switch (vq->type) {
> > +	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
> > +		*nplanes = 1;
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		break;
> > +	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
> > +		if (ctx->state < MTK_STATE_HEADER)
> > +			return -EINVAL;
> > +
> > +		/*
> > +		 * Output plane count is 2 - one for Y and one for CbCr
> > +		 */
> > +		*nplanes = 2;
> > +
> > +		mtk_v4l2_debug(1,
> > +				"[%d]\t get %d plane(s), ctx->dpb_count = %d, %d buffer(s)",
> > +				ctx->idx, *nplanes, ctx->dpb_count, *nbuffers);
> > +
> > +		sizes[0] = q_data->sizeimage[0];
> > +		sizes[1] = q_data->sizeimage[1];
> > +		alloc_ctxs[0] = ctx->dev->alloc_ctx;
> > +		alloc_ctxs[1] = ctx->dev->alloc_ctx;
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +
> > +	mtk_v4l2_debug(1,
> > +			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
> > +			ctx->idx, vq->type, *nplanes, *nbuffers,
> > +			sizes[0], sizes[1]);
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct mtk_q_data *q_data;
> > +	int i;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
> > +			 ctx->idx, vb->vb2_queue->type, vb->index);
> > +
> > +	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
> > +
> > +	for (i = 0; i < q_data->fmt->num_planes; i++) {
> > +		if (vb2_plane_size(vb, i) > q_data->sizeimage[i]) {
> > +			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
> > +				       i, vb2_plane_size(vb, i),
> > +				       q_data->sizeimage[i]);
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
> > +			ctx->idx, vb->vb2_queue->type,
> > +			vb->index, vb);
> > +	/*
> > +	 * check if this buffer is ready to be used after decode
> > +	 */
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		struct vb2_buffer *src_buf;
> > +		struct mtk_vcodec_mem buf;
> > +		bool res_chg = false;
> > +		int ret = 0;
> > +		int dpbsize = 1;
> > +
> > +		v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
> > +
> > +		if (ctx->state == MTK_STATE_INIT) {
> > +			src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
> > +			if (!src_buf) {
> > +				mtk_v4l2_err("No src buffer");
> > +				return;
> > +			}
> > +
> > +			buf.va	= vb2_plane_vaddr(src_buf, 0);
> > +			buf.dma_addr = vb2_dma_contig_plane_dma_addr(
> > +							src_buf,
> > +							0);
> > +			buf.size = (size_t)src_buf->planes[0].bytesused;
> > +			mtk_v4l2_debug(2,
> > +					"[%d] buf idx=%d va=%p dma=%llx size=%zu",
> > +					ctx->idx,
> > +					src_buf->index,
> > +					buf.va, (u64)buf.dma_addr, buf.size);
> > +
> > +			ret = vdec_if_decode(ctx, &buf, NULL, &res_chg);
> > +			if (ret) {
> > +				src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +				v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +						VB2_BUF_STATE_DONE);
> > +				mtk_v4l2_err("[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d",
> > +						ctx->idx,
> > +						src_buf->index,
> > +						buf.size, ret);
> > +				return;
> > +			}
> > +
> > +			if (vdec_if_get_param(ctx,
> > +						   GET_PARAM_PIC_INFO,
> > +						   &ctx->picinfo)) {
> > +				mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
> > +						ctx->idx);
> > +				return;
> > +			}
> > +
> > +			memcpy(&ctx->last_decoded_picinfo, &ctx->picinfo,
> > +				sizeof(struct vdec_pic_info));
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +						ctx->picinfo.y_bs_sz +
> > +						ctx->picinfo.y_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
> > +							ctx->picinfo.buf_w;
> > +			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
> > +						ctx->picinfo.c_bs_sz +
> > +						ctx->picinfo.c_len_sz;
> > +			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
> > +							ctx->picinfo.buf_w;
> > +			mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
> > +				ctx->idx,
> > +				ctx->picinfo.buf_w, ctx->picinfo.buf_h,
> > +				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
> > +				ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
> > +
> > +			vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
> > +			if (dpbsize == 0) {
> > +				mtk_v4l2_debug(2,
> > +						"[%d] vdec_if_get_param()  GET_PARAM_DPB_SIZE fail=%d",
> > +						ctx->idx, ret);
> > +				dpbsize = 1;
> > +			}
> > +			ctx->dpb_count = dpbsize;
> > +			ctx->state = MTK_STATE_HEADER;
> > +			mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->idx,
> > +							ctx->dpb_count);
> > +		} else {
> > +			mtk_v4l2_debug(1, "[%d] already init driver %d",
> > +					 ctx->idx, ctx->state);
> > +		}
> > +
> > +	} else {
> > +		mutex_lock(&buf->lock);
> > +		if (buf->used == false) {
> > +			v4l2_m2m_buf_queue(ctx->m2m_ctx,
> > +					to_vb2_v4l2_buffer(vb));
> > +			buf->queued_in_vb2 = true;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		} else {
> > +			buf->queued_in_vb2 = false;
> > +			buf->queued_in_v4l2 = true;
> > +			buf->ready_to_display = false;
> > +		}
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
> > +{
> > +	if (vb->vb2_queue->type ==
> > +		V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
> > +		buf = container_of(vb2_v4l2, struct mtk_video_buf, vb);
> > +		mutex_lock(&buf->lock);
> > +		buf->queued_in_v4l2 = false;
> > +		buf->queued_in_vb2 = false;
> > +		mutex_unlock(&buf->lock);
> > +	}
> > +}
> > +
> > +static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
> > +{
> > +	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +	struct mtk_video_buf *buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		buf->used = false;
> > +		buf->ready_to_display = false;
> > +		buf->nonrealdisplay = false;
> > +		buf->queued_in_v4l2 = false;
> > +		mutex_init(&buf->lock);
> > +	} else {
> > +		buf->lastframe = false;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	if (ctx->state == MTK_STATE_FLUSH)
> > +		ctx->state = MTK_STATE_HEADER;
> > +
> > +	return 0;
> > +}
> > +
> > +static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
> > +{
> > +	struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
> > +	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
> > +
> > +	mtk_v4l2_debug(1, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
> > +			ctx->idx, q->type, ctx->state, ctx->decoded_frame_cnt);
> > +
> > +	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > +		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		while (src_buf) {
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
> > +					VB2_BUF_STATE_ERROR);
> > +			src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
> > +		}
> > +	} else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > +		int i;
> > +		struct vb2_v4l2_buffer *vb2_v4l2;
> > +		struct mtk_video_buf *buf;
> > +
> > +		if (ctx->state >= MTK_STATE_HEADER)
> > +			mtk_vdec_flush_decoder(ctx);
> > +
> > +		ctx->state = MTK_STATE_FLUSH;
> > +
> > +		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		while (dst_buf) {
> > +			vb2_set_plane_payload(dst_buf, 0, 0);
> > +			vb2_set_plane_payload(dst_buf, 1, 0);
> > +			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
> > +				VB2_BUF_STATE_ERROR);
> > +			dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
> > +		}
> > +
> > +		for (i = 0; i < q->num_buffers; ++i) {
> > +			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
> > +				vb2_v4l2 = container_of(q->bufs[i],
> > +					struct vb2_v4l2_buffer, vb2_buf);
> > +				buf = container_of(vb2_v4l2,
> > +					struct mtk_video_buf, vb);
> > +
> > +				mtk_v4l2_debug(1, "[%d] idx=%d, type=%d, [%d %d] %d -> VB2_BUF_STATE_ERROR",
> > +						ctx->idx, i, q->type,
> > +						buf->queued_in_vb2,
> > +						buf->queued_in_v4l2,
> > +						(int)q->bufs[i]->state);
> > +				v4l2_m2m_buf_done(vb2_v4l2,
> > +						VB2_BUF_STATE_ERROR);
> > +			}
> > +		}
> > +	}
> > +}
> > +
> > +static void m2mops_vdec_device_run(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +
> > +	queue_work(dev->decode_workqueue, &ctx->decode_work);
> > +}
> > +
> > +static int m2mops_vdec_job_ready(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +
> > +	if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
> > +		return 0;
> > +
> > +	if (ctx->state == MTK_STATE_ABORT)
> > +		return 0;
> > +
> > +	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
> > +	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
> > +		return 0;
> > +
> > +	if (ctx->state != MTK_STATE_HEADER)
> > +		return 0;
> > +
> > +	return 1;
> > +}
> > +
> > +static void m2mops_vdec_job_abort(void *priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = priv;
> > +
> > +	ctx->state = MTK_STATE_ABORT;
> > +}
> > +
> > +static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
> > +
> > +	switch (ctrl->id) {
> > +	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
> > +		if (ctx->state >= MTK_STATE_HEADER) {
> > +			ctrl->val = ctx->dpb_count;
> > +		} else {
> > +			mtk_v4l2_err("Seqinfo not ready");
> > +			ctrl->val = 1;
> > +			return -EAGAIN;
> > +		}
> > +		break;
> > +	default:
> > +		mtk_v4l2_err("ctrl-id=%d not support!", ctrl->id);
> > +		return -EINVAL;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
> > +	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
> > +};
> > +
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct v4l2_ctrl *ctrl;
> > +
> > +	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
> > +
> > +	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
> > +					  &mtk_vcodec_dec_ctrl_ops,
> > +					  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
> > +					  1, 32, 1, 1);
> > +	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
> > +
> > +	if (ctx->ctrl_hdl.error) {
> > +		mtk_v4l2_err("Adding control failed %d",
> > +					ctx->ctrl_hdl.error);
> > +		return ctx->ctrl_hdl.error;
> > +	}
> > +
> > +	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
> > +	return 0;
> > +}
> > +
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
> > +}
> > +
> > +static void m2mops_vdec_lock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_lock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +static void m2mops_vdec_unlock(void *m2m_priv)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = m2m_priv;
> > +
> > +	mtk_v4l2_debug(3, "[%d]", ctx->idx);
> > +	mutex_unlock(&ctx->dev->dev_mutex);
> > +}
> > +
> > +const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
> > +	.device_run	= m2mops_vdec_device_run,
> > +	.job_ready	= m2mops_vdec_job_ready,
> > +	.job_abort	= m2mops_vdec_job_abort,
> > +	.lock	= m2mops_vdec_lock,
> > +	.unlock	= m2mops_vdec_unlock,
> > +};
> > +
> > +const struct vb2_ops mtk_vdec_vb2_ops = {
> > +	.queue_setup	= vb2ops_vdec_queue_setup,
> > +	.buf_prepare	= vb2ops_vdec_buf_prepare,
> > +	.buf_queue	= vb2ops_vdec_buf_queue,
> > +	.wait_prepare	= vb2_ops_wait_prepare,
> > +	.wait_finish	= vb2_ops_wait_finish,
> > +	.buf_init	= vb2ops_vdec_buf_init,
> > +	.buf_finish	= vb2ops_vdec_buf_finish,
> > +	.start_streaming	= vb2ops_vdec_start_streaming,
> > +	.stop_streaming	= vb2ops_vdec_stop_streaming,
> > +};
> > +
> > +const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
> > +	.vidioc_streamon	= vidioc_vdec_streamon,
> > +	.vidioc_streamoff	= vidioc_vdec_streamoff,
> > +
> > +	.vidioc_reqbufs		= vidioc_vdec_reqbufs,
> > +	.vidioc_querybuf	= vidioc_vdec_querybuf,
> > +	.vidioc_qbuf	= vidioc_vdec_qbuf,
> > +	.vidioc_expbuf	= vidioc_vdec_expbuf,
> > +	.vidioc_dqbuf	= vidioc_vdec_dqbuf,
> > +
> > +	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
> > +	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
> > +
> > +	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
> > +	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
> > +	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
> > +
> > +	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
> > +	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
> > +	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
> > +
> > +	.vidioc_querycap	= vidioc_vdec_querycap,
> > +	.vidioc_subscribe_event	= vidioc_vdec_subscribe_evt,
> > +	.vidioc_unsubscribe_event	= vidioc_vdec_unsubscribe_evt,
> > +	.vidioc_g_crop	= vidioc_vdec_g_crop,
> > +};
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > new file mode 100644
> > index 0000000..cb5cb42
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.h
> > @@ -0,0 +1,81 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#ifndef _MTK_VCODEC_DEC_H_
> > +#define _MTK_VCODEC_DEC_H_
> > +
> > +#include <media/videobuf2-core.h>
> > +#include <media/videobuf2-v4l2.h>
> > +
> > +/**
> > + * struct vdec_fb  - decoder frame buffer
> > + * @base_y	: Y plane memory info
> > + * @base_c	: C plane memory info
> > + * @status      : frame buffer status (vdec_fb_status)
> > + */
> > +struct vdec_fb {
> > +	struct mtk_vcodec_mem	base_y;
> > +	struct mtk_vcodec_mem	base_c;
> > +	unsigned int	status;
> > +};
> > +
> > +/**
> > + * struct mtk_video_buf - Private data related to each VB2 buffer.
> > + * @b:			VB2 buffer
> > + * @list:			link list
> > + * @used:		Output buffer contain decoded frame data
> > + * @ready_to_display:	Output buffer not display yet
> > + * @nonrealdisplay:	Output buffer is not display frame
> > + * @queued_in_vb2:	Output buffer is queue in vb2
> > + * @queued_in_v4l2:	Output buffer is in v4l2
> > + * @lastframe:		Intput buffer is last buffer - EOS
> > + * @frame_buffer:		Decode status of output buffer
> > + * @lock:			V4L2 and decode thread should get mutex
> > + *			before r/w info in mtk_video_buf
> > + */
> > +struct mtk_video_buf {
> > +	struct vb2_v4l2_buffer	vb;
> > +	struct list_head	list;
> > +
> > +	bool	used;
> > +	bool	ready_to_display;
> > +	bool	nonrealdisplay;
> > +	bool	queued_in_vb2;
> > +	bool	queued_in_v4l2;
> > +	bool	lastframe;
> > +	struct vdec_fb	frame_buffer;
> > +	struct mutex	lock;
> > +};
> > +
> > +extern const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops;
> > +extern const struct v4l2_m2m_ops mtk_vdec_m2m_ops;
> > +
> > +
> > +/*
> > + * mtk_vdec_lock/mtk_vdec_unlock are for ctx instance to
> > + * get/release lock before/after access decoder hw.
> > + * mtk_vdec_lock get decoder hw lock and set curr_ctx
> > + * to ctx instance that get lock
> > + */
> > +int mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_lock(struct mtk_vcodec_ctx *ctx);
> > +int m2mctx_vdec_queue_init(void *priv, struct vb2_queue *src_vq,
> > +			   struct vb2_queue *dst_vq);
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vcodec_vdec_release(struct mtk_vcodec_ctx *ctx);
> > +int mtk_vdec_ctrls_setup(struct mtk_vcodec_ctx *ctx);
> > +void mtk_vdec_ctrls_free(struct mtk_vcodec_ctx *ctx);
> > +
> > +#endif /* _MTK_VCODEC_DEC_H_ */
> > diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > new file mode 100644
> > index 0000000..34e3217
> > --- /dev/null
> > +++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec_drv.c
> > @@ -0,0 +1,469 @@
> > +/*
> > +* Copyright (c) 2016 MediaTek Inc.
> > +* Author: PC Chen <pc.chen@mediatek.com>
> > +*         Tiffany Lin <tiffany.lin@mediatek.com>
> > +*
> > +* This program is free software; you can redistribute it and/or modify
> > +* it under the terms of the GNU General Public License version 2 as
> > +* published by the Free Software Foundation.
> > +*
> > +* This program is distributed in the hope that it will be useful,
> > +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +* GNU General Public License for more details.
> > +*/
> > +
> > +#include <linux/interrupt.h>
> > +#include <linux/irq.h>
> > +#include <linux/module.h>
> > +#include <linux/of_device.h>
> > +#include <linux/of.h>
> > +#include <media/v4l2-event.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "mtk_vcodec_drv.h"
> > +#include "mtk_vcodec_dec.h"
> > +#include "mtk_vcodec_dec_pm.h"
> > +#include "mtk_vcodec_intr.h"
> > +#include "mtk_vcodec_util.h"
> > +#include "vdec_drv_if.h"
> > +#include "mtk_vpu.h"
> > +
> > +module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
> > +module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
> > +
> > +#define VDEC_HW_ACTIVE	0x10
> > +#define VDEC_IRQ_CFG	0x11
> > +#define VDEC_IRQ_CLR	0x10
> > +#define VDEC_IRQ_CFG_REG	0xa4
> > +
> > +
> > +/* Wake up context wait_queue */
> > +static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
> > +{
> > +	ctx->int_cond = 1;
> > +	ctx->int_type = reason;
> > +	wake_up_interruptible(&ctx->queue);
> > +}
> > +
> > +static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	unsigned long flags;
> > +	struct mtk_vcodec_ctx *ctx;
> > +	unsigned int cg_status = 0;
> > +	unsigned int dec_done_status = 0;
> > +
> > +	spin_lock_irqsave(&dev->irqlock, flags);
> > +	ctx = dev->curr_ctx;
> > +	spin_unlock_irqrestore(&dev->irqlock, flags);
> > +
> > +	cg_status = readl(dev->reg_base[0] + (0));
> > +	if ((cg_status & VDEC_HW_ACTIVE) != 0) {
> > +		mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
> > +			       cg_status);
> > +		return IRQ_HANDLED;
> > +	}
> > +
> > +	dec_done_status = readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	ctx->irq_status = dec_done_status;
> > +	if ((dec_done_status & (0x1 << 16)) != 0x10000)
> > +		return IRQ_HANDLED;
> > +
> > +	/* clear interrupt */
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) | VDEC_IRQ_CFG),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +	writel((readl(dev->reg_base[1] + VDEC_IRQ_CFG_REG) & ~VDEC_IRQ_CLR),
> > +	       dev->reg_base[1] + VDEC_IRQ_CFG_REG);
> > +
> > +	wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
> > +
> > +	mtk_v4l2_debug(3,
> > +			"mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
> > +			ctx->idx, dec_done_status);
> > +
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int fops_vcodec_open(struct file *file)
> > +{
> > +	struct video_device *vfd = video_devdata(file);
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = NULL;
> > +	int ret = 0;
> > +
> > +	if (dev->instance_mask == ~0UL) {
> > +		mtk_v4l2_err("Too many open contexts");
> > +		ret = -EBUSY;
> > +		goto err_alloc;
> > +	}
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +
> > +	ctx = devm_kzalloc(&dev->plat_dev->dev, sizeof(*ctx), GFP_KERNEL);
> > +	if (!ctx) {
> > +		ret = -ENOMEM;
> > +		goto err_alloc;
> > +	}
> > +
> > +	ctx->idx = ffz(dev->instance_mask);
> > +	v4l2_fh_init(&ctx->fh, video_devdata(file));
> > +	file->private_data = &ctx->fh;
> > +	v4l2_fh_add(&ctx->fh);
> > +	INIT_LIST_HEAD(&ctx->list);
> > +	ctx->dev = dev;
> > +	init_waitqueue_head(&ctx->queue);
> > +
> > +	if (vfd == dev->vfd_dec) {
> > +		ctx->type = MTK_INST_DECODER;
> > +		ret = mtk_vdec_ctrls_setup(ctx);
> > +		if (ret) {
> > +			mtk_v4l2_err("Failed to setup mt vcodec controls");
> > +			goto err_ctrls_setup;
> > +		}
> > +		ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
> > +						 &m2mctx_vdec_queue_init);
> > +		if (IS_ERR(ctx->m2m_ctx)) {
> > +			ret = PTR_ERR(ctx->m2m_ctx);
> > +			mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
> > +				       ret);
> > +			goto err_ctx_init;
> > +		}
> > +		mtk_vcodec_dec_set_default_params(ctx);
> > +	} else {
> > +		mtk_v4l2_err("Invalid vfd !");
> > +		ret = -ENOENT;
> > +		goto err_ctx_init;
> > +	}
> > +
> > +	if (v4l2_fh_is_singular(&ctx->fh)) {
> > +		mtk_vcodec_dec_pw_on(&dev->pm);
> > +		ret = vpu_load_firmware(dev->vpu_plat_dev);
> > +		if (ret < 0) {
> > +			mtk_v4l2_err("vpu_load_firmware failed!");
> > +			goto err_load_fw;
> > +		}
> > +
> > +		dev->dec_capability =
> > +			vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
> > +		mtk_v4l2_debug(0, "decoder capability %x",
> > +			       dev->dec_capability);
> > +	}
> > +
> > +	set_bit(ctx->idx, &dev->instance_mask);
> > +	dev->num_instances++;
> > +	list_add(&ctx->list, &dev->ctx_list);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
> > +					ctx->idx);
> > +
> > +	return ret;
> > +
> > +	/* Deinit when failure occurred */
> > +err_load_fw:
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +err_ctx_init:
> > +	mtk_vdec_ctrls_free(ctx);
> > +err_ctrls_setup:
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +err_alloc:
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_release(struct file *file)
> > +{
> > +	struct mtk_vcodec_dev *dev = video_drvdata(file);
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	mtk_v4l2_debug(0, "[%d] decoder", ctx->idx);
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	v4l2_m2m_ctx_release(ctx->m2m_ctx);
> > +	mtk_vcodec_vdec_release(ctx);
> > +	mtk_vdec_ctrls_free(ctx);
> > +	v4l2_fh_del(&ctx->fh);
> > +	v4l2_fh_exit(&ctx->fh);
> > +	list_del_init(&ctx->list);
> > +	dev->num_instances--;
> > +	if (dev->num_instances == 0)
> > +		mtk_vcodec_dec_pw_off(&dev->pm);
> > +	clear_bit(ctx->idx, &dev->instance_mask);
> > +	devm_kfree(&dev->plat_dev->dev, ctx);
> > +	mutex_unlock(&dev->dev_mutex);
> > +	return 0;
> > +}
> > +
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 
Got it, will fix this in next version.


best regards,
Tiffany

> > +};
> > +
> > +static void mtk_vcodec_dec_reset_handler(void *priv)
> > +{
> > +	struct mtk_vcodec_dev *dev = priv;
> > +	struct mtk_vcodec_ctx *ctx;
> > +
> > +	mtk_v4l2_debug(0, "Watchdog timeout!!");
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	list_for_each_entry(ctx, &dev->ctx_list, list) {
> > +		ctx->state = MTK_STATE_ABORT;
> > +		mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
> > +					   ctx->idx);
> > +	}
> > +	mutex_unlock(&dev->dev_mutex);
> > +}
> > +
> > +static int mtk_vcodec_probe(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev;
> > +	struct video_device *vfd_dec;
> > +	struct resource *res;
> > +	int i, ret;
> > +	int reg_base_num;
> > +
> > +	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
> > +	if (!dev)
> > +		return -ENOMEM;
> > +
> > +	INIT_LIST_HEAD(&dev->ctx_list);
> > +	dev->plat_dev = pdev;
> > +	dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
> > +	if (dev->vpu_plat_dev == NULL) {
> > +		mtk_v4l2_err("[VPU] vpu device in not ready");
> > +		return -EPROBE_DEFER;
> > +	}
> > +
> > +	vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
> > +				dev, VPU_RST_DEC);
> > +
> > +	ret = mtk_vcodec_init_dec_pm(dev);
> > +	if (ret < 0) {
> > +		dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
> > +		return ret;
> > +	}
> > +
> > +	reg_base_num = NUM_MAX_VDEC_REG_BASE;
> > +	for (i = 0; i < reg_base_num; i++) {
> > +		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
> > +		if (res == NULL) {
> > +			dev_err(&pdev->dev, "get memory resource failed.");
> > +			ret = -ENXIO;
> > +			goto err_res;
> > +		}
> > +		dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
> > +		if (IS_ERR(dev->reg_base[i])) {
> > +			dev_err(&pdev->dev,
> > +				"devm_ioremap_resource %d failed.", i);
> > +			ret = PTR_ERR(dev->reg_base);
> > +			goto err_res;
> > +		}
> > +		mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
> > +	}
> > +
> > +	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> > +	if (res == NULL) {
> > +		dev_err(&pdev->dev, "failed to get irq resource");
> > +		ret = -ENOENT;
> > +		goto err_res;
> > +	}
> > +
> > +	dev->dec_irq = platform_get_irq(pdev, 0);
> > +	ret = devm_request_irq(&pdev->dev, dev->dec_irq,
> > +			       mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
> > +	if (ret) {
> > +		dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
> > +			dev->dec_irq,
> > +			ret);
> > +		ret = -EINVAL;
> > +		goto err_res;
> > +	}
> > +
> > +	disable_irq(dev->dec_irq);
> > +	mutex_init(&dev->dev_mutex);
> > +	mutex_init(&dev->dec_mutex);
> > +	spin_lock_init(&dev->irqlock);
> > +
> > +	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
> > +		 "[MTK_V4L2]");
> > +
> > +	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
> > +	if (ret) {
> > +		mtk_v4l2_err("v4l2_device_register err=%d", ret);
> > +		return ret;
> > +	}
> > +
> > +	init_waitqueue_head(&dev->queue);
> > +
> > +	vfd_dec = video_device_alloc();
> > +	if (!vfd_dec) {
> > +		mtk_v4l2_err("Failed to allocate video device");
> > +		ret = -ENOMEM;
> > +		goto err_dec_alloc;
> > +	}
> > +	vfd_dec->fops		= &mtk_vcodec_fops;
> > +	vfd_dec->ioctl_ops	= &mtk_vdec_ioctl_ops;
> > +	vfd_dec->release	= video_device_release;
> > +	vfd_dec->lock		= &dev->dev_mutex;
> > +	vfd_dec->v4l2_dev	= &dev->v4l2_dev;
> > +	vfd_dec->vfl_dir	= VFL_DIR_M2M;
> > +
> > +	snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
> > +		 MTK_VCODEC_DEC_NAME);
> > +	video_set_drvdata(vfd_dec, dev);
> > +	dev->vfd_dec = vfd_dec;
> > +	platform_set_drvdata(pdev, dev);
> > +
> > +	dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
> > +	if (IS_ERR(dev->alloc_ctx)) {
> > +		mtk_v4l2_err("Failed to alloc vb2 dma context 0");
> > +		ret = PTR_ERR(dev->alloc_ctx);
> > +		goto err_vb2_ctx_init;
> > +	}
> > +
> > +	dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
> > +	if (IS_ERR(dev->m2m_dev_dec)) {
> > +		mtk_v4l2_err("Failed to init mem2mem dec device");
> > +		ret = PTR_ERR(dev->m2m_dev_dec);
> > +		goto err_dec_mem_init;
> > +	}
> > +
> > +	dev->decode_workqueue =
> > +			alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
> > +			WQ_MEM_RECLAIM | WQ_FREEZABLE);
> > +	if (!dev->decode_workqueue) {
> > +		mtk_v4l2_err("Failed to create decode workqueue");
> > +		ret = -EINVAL;
> > +		goto err_event_workq;
> > +	}
> > +
> > +	ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
> > +	if (ret) {
> > +		mtk_v4l2_err("Failed to register video device");
> > +		goto err_dec_reg;
> > +	}
> > +	mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
> > +			 vfd_dec->num);
> > +
> > +	return 0;
> > +
> > +err_dec_reg:
> > +	destroy_workqueue(dev->decode_workqueue);
> > +err_event_workq:
> > +	v4l2_m2m_release(dev->m2m_dev_dec);
> > +err_dec_mem_init:
> > +	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +err_vb2_ctx_init:
> > +	video_unregister_device(vfd_dec);
> > +err_dec_alloc:
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +err_res:
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return ret;
> > +}
> > +
> > +static const struct of_device_id mtk_vcodec_match[] = {
> > +	{.compatible = "mediatek,mt8173-vcodec-dec",},
> > +	{},
> > +};
> > +
> > +MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
> > +
> > +static int mtk_vcodec_dec_remove(struct platform_device *pdev)
> > +{
> > +	struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug_enter();
> > +	flush_workqueue(dev->decode_workqueue);
> > +	destroy_workqueue(dev->decode_workqueue);
> > +	if (dev->m2m_dev_dec)
> > +		v4l2_m2m_release(dev->m2m_dev_dec);
> > +	if (dev->alloc_ctx)
> > +		vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
> > +
> > +	if (dev->vfd_dec)
> > +		video_unregister_device(dev->vfd_dec);
> > +
> > +	v4l2_device_unregister(&dev->v4l2_dev);
> > +	mtk_vcodec_release_dec_pm(dev);
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_suspend(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_vcodec_vdec_pm_runtime_resume(struct device *dev)
> > +{
> > +	struct platform_device *pdev = to_platform_device(dev);
> > +	struct mtk_vcodec_dev *m_dev = platform_get_drvdata(pdev);
> > +
> > +	mtk_v4l2_debug(3, "m_dev->num_instances=%d", m_dev->num_instances);
> > +
> > +	if (m_dev->num_instances == 0)
> > +		return 0;
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct dev_pm_ops mtk_vcodec_vdec_pm_ops = {
> > +	SET_RUNTIME_PM_OPS(mtk_vcodec_vdec_pm_runtime_suspend,
> > +			   mtk_vcodec_vdec_pm_runtime_resume,
> > +			   NULL)
> > +};
> > +
> > +static struct platform_driver mtk_vcodec_dec_driver = {
> > +	.probe	= mtk_vcodec_probe,
> > +	.remove	= mtk_vcodec_dec_remove,
> > +	.driver	= {
> > +		.name	= MTK_VCODEC_DEC_NAME,
> > +		.owner	= THIS_MODULE,
> > +		.of_match_table = mtk_vcodec_match,
> > +		.pm = &mtk_vcodec_vdec_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(mtk_vcodec_dec_driver);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_DESCRIPTION("Mediatek video codec V4L2 driver");
> 
> Regards,
> 
> 	Hans
> 

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  5:40           ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  5:40 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin


snipped.

> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 



snipped.
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 

Hi Hans,

We are plaining to remove m2m framework in th feature, although we think
it is easy to use and could save a lot of code similar to what m2m
framework implemented and reduce code size.
The main reason is that in v4l2_m2m_try_schedule, it required that at
least one output buffer and one capture buffer to run device_run.
We want to start device_run without capture buffer queued.
Is there any suggestion that we could use m2m framework but trigger
device_run with only output buffer.
Or we need to remove m2m and write our own implementation.



snipped.


best regards,
Tiffany

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  5:40           ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  5:40 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w


snipped.

> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 



snipped.
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 

Hi Hans,

We are plaining to remove m2m framework in th feature, although we think
it is easy to use and could save a lot of code similar to what m2m
framework implemented and reduce code size.
The main reason is that in v4l2_m2m_try_schedule, it required that at
least one output buffer and one capture buffer to run device_run.
We want to start device_run without capture buffer queued.
Is there any suggestion that we could use m2m framework but trigger
device_run with only output buffer.
Or we need to remove m2m and write our own implementation.



snipped.


best regards,
Tiffany

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  5:40           ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  5:40 UTC (permalink / raw)
  To: linux-arm-kernel


snipped.

> > +
> > +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> > +{
> > +	struct mtk_q_data *q_data;
> > +
> > +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> > +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> > +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> > +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> > +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> > +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> > +
> > +
> > +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> > +	memset(q_data, 0, sizeof(struct mtk_q_data));
> > +	q_data->visible_width = DFT_CFG_WIDTH;
> > +	q_data->visible_height = DFT_CFG_HEIGHT;
> > +	q_data->coded_width = DFT_CFG_WIDTH;
> > +	q_data->coded_height = DFT_CFG_HEIGHT;
> > +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> > +	q_data->field = V4L2_FIELD_NONE;
> > +
> > +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> > +
> > +	v4l_bound_align_image(&q_data->coded_width,
> > +					MTK_VDEC_MIN_W,
> > +					MTK_VDEC_MAX_W, 4,
> > +					&q_data->coded_height,
> > +					MTK_VDEC_MIN_H,
> > +					MTK_VDEC_MAX_H, 5, 6);
> > +
> > +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> > +	q_data->bytesperline[0] = q_data->coded_width;
> > +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> > +	q_data->bytesperline[1] = q_data->coded_width;
> > +
> > +}
> > +
> > +static int vidioc_vdec_streamon(struct file *file, void *priv,
> > +				enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +
> > +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> > +				 enum v4l2_buf_type type)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> > +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> > +}
> > +
> > +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> > +			       struct v4l2_requestbuffers *reqbufs)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> > +	int ret;
> > +
> > +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> > +			 reqbufs->type, reqbufs->count);
> > +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> > +
> > +	return ret;
> > +}
> 
> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> 



snipped.
> > +static unsigned int fops_vcodec_poll(struct file *file,
> > +				     struct poll_table_struct *wait)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +	struct mtk_vcodec_dev *dev = ctx->dev;
> > +	int ret;
> > +
> > +	mutex_lock(&dev->dev_mutex);
> > +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> > +	mutex_unlock(&dev->dev_mutex);
> > +
> > +	return ret;
> > +}
> > +
> > +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> > +{
> > +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> > +
> > +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> > +}
> > +
> > +static const struct v4l2_file_operations mtk_vcodec_fops = {
> > +	.owner				= THIS_MODULE,
> > +	.open				= fops_vcodec_open,
> > +	.release			= fops_vcodec_release,
> > +	.poll				= fops_vcodec_poll,
> > +	.unlocked_ioctl			= video_ioctl2,
> > +	.mmap				= fops_vcodec_mmap,
> 
> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> 

Hi Hans,

We are plaining to remove m2m framework in th feature, although we think
it is easy to use and could save a lot of code similar to what m2m
framework implemented and reduce code size.
The main reason is that in v4l2_m2m_try_schedule, it required that at
least one output buffer and one capture buffer to run device_run.
We want to start device_run without capture buffer queued.
Is there any suggestion that we could use m2m framework but trigger
device_run with only output buffer.
Or we need to remove m2m and write our own implementation.



snipped.


best regards,
Tiffany

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  2016-04-18  5:40           ` tiffany lin
  (?)
@ 2016-04-18  7:32             ` Hans Verkuil
  -1 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-18  7:32 UTC (permalink / raw)
  To: tiffany lin, Pawel Osciak
  Cc: Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

On 04/18/2016 07:40 AM, tiffany lin wrote:
> 
> snipped.
> 
>>> +
>>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
>>> +{
>>> +	struct mtk_q_data *q_data;
>>> +
>>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
>>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
>>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
>>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
>>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
>>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
>>> +
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->coded_width = DFT_CFG_WIDTH;
>>> +	q_data->coded_height = DFT_CFG_HEIGHT;
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +
>>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
>>> +
>>> +	v4l_bound_align_image(&q_data->coded_width,
>>> +					MTK_VDEC_MIN_W,
>>> +					MTK_VDEC_MAX_W, 4,
>>> +					&q_data->coded_height,
>>> +					MTK_VDEC_MIN_H,
>>> +					MTK_VDEC_MAX_H, 5, 6);
>>> +
>>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
>>> +	q_data->bytesperline[0] = q_data->coded_width;
>>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
>>> +	q_data->bytesperline[1] = q_data->coded_width;
>>> +
>>> +}
>>> +
>>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
>>> +				enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +
>>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
>>> +				 enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
>>> +			       struct v4l2_requestbuffers *reqbufs)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +	int ret;
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
>>> +			 reqbufs->type, reqbufs->count);
>>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
>>> +
>>> +	return ret;
>>> +}
>>
>> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
>>
> 
> 
> 
> snipped.
>>> +static unsigned int fops_vcodec_poll(struct file *file,
>>> +				     struct poll_table_struct *wait)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +	struct mtk_vcodec_dev *dev = ctx->dev;
>>> +	int ret;
>>> +
>>> +	mutex_lock(&dev->dev_mutex);
>>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
>>> +	mutex_unlock(&dev->dev_mutex);
>>> +
>>> +	return ret;
>>> +}
>>> +
>>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +
>>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
>>> +}
>>> +
>>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
>>> +	.owner				= THIS_MODULE,
>>> +	.open				= fops_vcodec_open,
>>> +	.release			= fops_vcodec_release,
>>> +	.poll				= fops_vcodec_poll,
>>> +	.unlocked_ioctl			= video_ioctl2,
>>> +	.mmap				= fops_vcodec_mmap,
>>
>> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
>>
> 
> Hi Hans,
> 
> We are plaining to remove m2m framework in th feature, although we think

Remove it for just the decoder driver or both encoder and decoder?

> it is easy to use and could save a lot of code similar to what m2m
> framework implemented and reduce code size.
> The main reason is that in v4l2_m2m_try_schedule, it required that at
> least one output buffer and one capture buffer to run device_run.
> We want to start device_run without capture buffer queued.

I assume the reason is that you need to get the resolution etc. information
from the encoded stream? Without a capture buffer you can't actually decode
a frame, but that's probably not what this is about.

> Is there any suggestion that we could use m2m framework but trigger
> device_run with only output buffer.
> Or we need to remove m2m and write our own implementation.

I am assuming that not using the m2m framework is for the decoder only and
that its purpose is to obtain data about the encoded stream early.

This is something that was discussed with Pawel in the past. I don't have a
problem if you do it yourself (without the m2m framework), but it might also
be an idea to adapt the framework for this.

Pawel, do you have any thoughts on that?

Regards,

	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  7:32             ` Hans Verkuil
  0 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-18  7:32 UTC (permalink / raw)
  To: tiffany lin, Pawel Osciak
  Cc: Hans Verkuil, daniel.thompson-QSEj5FYQhm4dnm+yROfE0A,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w

On 04/18/2016 07:40 AM, tiffany lin wrote:
> 
> snipped.
> 
>>> +
>>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
>>> +{
>>> +	struct mtk_q_data *q_data;
>>> +
>>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
>>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
>>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
>>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
>>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
>>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
>>> +
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->coded_width = DFT_CFG_WIDTH;
>>> +	q_data->coded_height = DFT_CFG_HEIGHT;
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +
>>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
>>> +
>>> +	v4l_bound_align_image(&q_data->coded_width,
>>> +					MTK_VDEC_MIN_W,
>>> +					MTK_VDEC_MAX_W, 4,
>>> +					&q_data->coded_height,
>>> +					MTK_VDEC_MIN_H,
>>> +					MTK_VDEC_MAX_H, 5, 6);
>>> +
>>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
>>> +	q_data->bytesperline[0] = q_data->coded_width;
>>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
>>> +	q_data->bytesperline[1] = q_data->coded_width;
>>> +
>>> +}
>>> +
>>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
>>> +				enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +
>>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
>>> +				 enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
>>> +			       struct v4l2_requestbuffers *reqbufs)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +	int ret;
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
>>> +			 reqbufs->type, reqbufs->count);
>>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
>>> +
>>> +	return ret;
>>> +}
>>
>> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
>>
> 
> 
> 
> snipped.
>>> +static unsigned int fops_vcodec_poll(struct file *file,
>>> +				     struct poll_table_struct *wait)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +	struct mtk_vcodec_dev *dev = ctx->dev;
>>> +	int ret;
>>> +
>>> +	mutex_lock(&dev->dev_mutex);
>>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
>>> +	mutex_unlock(&dev->dev_mutex);
>>> +
>>> +	return ret;
>>> +}
>>> +
>>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +
>>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
>>> +}
>>> +
>>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
>>> +	.owner				= THIS_MODULE,
>>> +	.open				= fops_vcodec_open,
>>> +	.release			= fops_vcodec_release,
>>> +	.poll				= fops_vcodec_poll,
>>> +	.unlocked_ioctl			= video_ioctl2,
>>> +	.mmap				= fops_vcodec_mmap,
>>
>> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
>>
> 
> Hi Hans,
> 
> We are plaining to remove m2m framework in th feature, although we think

Remove it for just the decoder driver or both encoder and decoder?

> it is easy to use and could save a lot of code similar to what m2m
> framework implemented and reduce code size.
> The main reason is that in v4l2_m2m_try_schedule, it required that at
> least one output buffer and one capture buffer to run device_run.
> We want to start device_run without capture buffer queued.

I assume the reason is that you need to get the resolution etc. information
from the encoded stream? Without a capture buffer you can't actually decode
a frame, but that's probably not what this is about.

> Is there any suggestion that we could use m2m framework but trigger
> device_run with only output buffer.
> Or we need to remove m2m and write our own implementation.

I am assuming that not using the m2m framework is for the decoder only and
that its purpose is to obtain data about the encoded stream early.

This is something that was discussed with Pawel in the past. I don't have a
problem if you do it yourself (without the m2m framework), but it might also
be an idea to adapt the framework for this.

Pawel, do you have any thoughts on that?

Regards,

	Hans
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  7:32             ` Hans Verkuil
  0 siblings, 0 replies; 52+ messages in thread
From: Hans Verkuil @ 2016-04-18  7:32 UTC (permalink / raw)
  To: linux-arm-kernel

On 04/18/2016 07:40 AM, tiffany lin wrote:
> 
> snipped.
> 
>>> +
>>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
>>> +{
>>> +	struct mtk_q_data *q_data;
>>> +
>>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
>>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
>>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
>>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
>>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
>>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
>>> +
>>> +
>>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
>>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
>>> +	q_data->visible_width = DFT_CFG_WIDTH;
>>> +	q_data->visible_height = DFT_CFG_HEIGHT;
>>> +	q_data->coded_width = DFT_CFG_WIDTH;
>>> +	q_data->coded_height = DFT_CFG_HEIGHT;
>>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
>>> +	q_data->field = V4L2_FIELD_NONE;
>>> +
>>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
>>> +
>>> +	v4l_bound_align_image(&q_data->coded_width,
>>> +					MTK_VDEC_MIN_W,
>>> +					MTK_VDEC_MAX_W, 4,
>>> +					&q_data->coded_height,
>>> +					MTK_VDEC_MIN_H,
>>> +					MTK_VDEC_MAX_H, 5, 6);
>>> +
>>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
>>> +	q_data->bytesperline[0] = q_data->coded_width;
>>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
>>> +	q_data->bytesperline[1] = q_data->coded_width;
>>> +
>>> +}
>>> +
>>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
>>> +				enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +
>>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
>>> +				 enum v4l2_buf_type type)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
>>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
>>> +}
>>> +
>>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
>>> +			       struct v4l2_requestbuffers *reqbufs)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
>>> +	int ret;
>>> +
>>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
>>> +			 reqbufs->type, reqbufs->count);
>>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
>>> +
>>> +	return ret;
>>> +}
>>
>> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
>>
> 
> 
> 
> snipped.
>>> +static unsigned int fops_vcodec_poll(struct file *file,
>>> +				     struct poll_table_struct *wait)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +	struct mtk_vcodec_dev *dev = ctx->dev;
>>> +	int ret;
>>> +
>>> +	mutex_lock(&dev->dev_mutex);
>>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
>>> +	mutex_unlock(&dev->dev_mutex);
>>> +
>>> +	return ret;
>>> +}
>>> +
>>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
>>> +{
>>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
>>> +
>>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
>>> +}
>>> +
>>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
>>> +	.owner				= THIS_MODULE,
>>> +	.open				= fops_vcodec_open,
>>> +	.release			= fops_vcodec_release,
>>> +	.poll				= fops_vcodec_poll,
>>> +	.unlocked_ioctl			= video_ioctl2,
>>> +	.mmap				= fops_vcodec_mmap,
>>
>> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
>>
> 
> Hi Hans,
> 
> We are plaining to remove m2m framework in th feature, although we think

Remove it for just the decoder driver or both encoder and decoder?

> it is easy to use and could save a lot of code similar to what m2m
> framework implemented and reduce code size.
> The main reason is that in v4l2_m2m_try_schedule, it required that at
> least one output buffer and one capture buffer to run device_run.
> We want to start device_run without capture buffer queued.

I assume the reason is that you need to get the resolution etc. information
from the encoded stream? Without a capture buffer you can't actually decode
a frame, but that's probably not what this is about.

> Is there any suggestion that we could use m2m framework but trigger
> device_run with only output buffer.
> Or we need to remove m2m and write our own implementation.

I am assuming that not using the m2m framework is for the decoder only and
that its purpose is to obtain data about the encoded stream early.

This is something that was discussed with Pawel in the past. I don't have a
problem if you do it yourself (without the m2m framework), but it might also
be an idea to adapt the framework for this.

Pawel, do you have any thoughts on that?

Regards,

	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  2016-04-18  7:32             ` Hans Verkuil
  (?)
@ 2016-04-18  8:22               ` tiffany lin
  -1 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  8:22 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Pawel Osciak, Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Hi Hans,

On Mon, 2016-04-18 at 09:32 +0200, Hans Verkuil wrote:
> On 04/18/2016 07:40 AM, tiffany lin wrote:
> > 
> > snipped.
> > 
> >>> +
> >>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> >>> +{
> >>> +	struct mtk_q_data *q_data;
> >>> +
> >>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> >>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> >>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> >>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> >>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> >>> +
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->coded_width = DFT_CFG_WIDTH;
> >>> +	q_data->coded_height = DFT_CFG_HEIGHT;
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +
> >>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> >>> +
> >>> +	v4l_bound_align_image(&q_data->coded_width,
> >>> +					MTK_VDEC_MIN_W,
> >>> +					MTK_VDEC_MAX_W, 4,
> >>> +					&q_data->coded_height,
> >>> +					MTK_VDEC_MIN_H,
> >>> +					MTK_VDEC_MAX_H, 5, 6);
> >>> +
> >>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> >>> +	q_data->bytesperline[0] = q_data->coded_width;
> >>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> >>> +	q_data->bytesperline[1] = q_data->coded_width;
> >>> +
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> >>> +				enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +
> >>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> >>> +				 enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> >>> +			       struct v4l2_requestbuffers *reqbufs)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +	int ret;
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> >>> +			 reqbufs->type, reqbufs->count);
> >>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> >>> +
> >>> +	return ret;
> >>> +}
> >>
> >> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> >>
> > 
> > 
> > 
> > snipped.
> >>> +static unsigned int fops_vcodec_poll(struct file *file,
> >>> +				     struct poll_table_struct *wait)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +	struct mtk_vcodec_dev *dev = ctx->dev;
> >>> +	int ret;
> >>> +
> >>> +	mutex_lock(&dev->dev_mutex);
> >>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> >>> +	mutex_unlock(&dev->dev_mutex);
> >>> +
> >>> +	return ret;
> >>> +}
> >>> +
> >>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +
> >>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> >>> +}
> >>> +
> >>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> >>> +	.owner				= THIS_MODULE,
> >>> +	.open				= fops_vcodec_open,
> >>> +	.release			= fops_vcodec_release,
> >>> +	.poll				= fops_vcodec_poll,
> >>> +	.unlocked_ioctl			= video_ioctl2,
> >>> +	.mmap				= fops_vcodec_mmap,
> >>
> >> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> >>
> > 
> > Hi Hans,
> > 
> > We are plaining to remove m2m framework in th feature, although we think
> 
> Remove it for just the decoder driver or both encoder and decoder?
> 
Remove it from decoder driver.

> > it is easy to use and could save a lot of code similar to what m2m
> > framework implemented and reduce code size.
> > The main reason is that in v4l2_m2m_try_schedule, it required that at
> > least one output buffer and one capture buffer to run device_run.
> > We want to start device_run without capture buffer queued.
> 
> I assume the reason is that you need to get the resolution etc. information
> from the encoded stream? Without a capture buffer you can't actually decode
> a frame, but that's probably not what this is about.
Yes, it could start parsing without waiting CAPTURE ready and get
CAPTURE buffer from vb2 when it really need it.


> > Is there any suggestion that we could use m2m framework but trigger
> > device_run with only output buffer.
> > Or we need to remove m2m and write our own implementation.
> 
> I am assuming that not using the m2m framework is for the decoder only and
> that its purpose is to obtain data about the encoded stream early.
> 
yes. it's for decode only.


> This is something that was discussed with Pawel in the past. I don't have a
> problem if you do it yourself (without the m2m framework), but it might also
> be an idea to adapt the framework for this.
> 
> Pawel, do you have any thoughts on that?
> 
> Regards,
> 
> 	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  8:22               ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  8:22 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Pawel Osciak, Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Hi Hans,

On Mon, 2016-04-18 at 09:32 +0200, Hans Verkuil wrote:
> On 04/18/2016 07:40 AM, tiffany lin wrote:
> > 
> > snipped.
> > 
> >>> +
> >>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> >>> +{
> >>> +	struct mtk_q_data *q_data;
> >>> +
> >>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> >>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> >>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> >>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> >>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> >>> +
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->coded_width = DFT_CFG_WIDTH;
> >>> +	q_data->coded_height = DFT_CFG_HEIGHT;
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +
> >>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> >>> +
> >>> +	v4l_bound_align_image(&q_data->coded_width,
> >>> +					MTK_VDEC_MIN_W,
> >>> +					MTK_VDEC_MAX_W, 4,
> >>> +					&q_data->coded_height,
> >>> +					MTK_VDEC_MIN_H,
> >>> +					MTK_VDEC_MAX_H, 5, 6);
> >>> +
> >>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> >>> +	q_data->bytesperline[0] = q_data->coded_width;
> >>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> >>> +	q_data->bytesperline[1] = q_data->coded_width;
> >>> +
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> >>> +				enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +
> >>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> >>> +				 enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> >>> +			       struct v4l2_requestbuffers *reqbufs)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +	int ret;
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> >>> +			 reqbufs->type, reqbufs->count);
> >>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> >>> +
> >>> +	return ret;
> >>> +}
> >>
> >> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> >>
> > 
> > 
> > 
> > snipped.
> >>> +static unsigned int fops_vcodec_poll(struct file *file,
> >>> +				     struct poll_table_struct *wait)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +	struct mtk_vcodec_dev *dev = ctx->dev;
> >>> +	int ret;
> >>> +
> >>> +	mutex_lock(&dev->dev_mutex);
> >>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> >>> +	mutex_unlock(&dev->dev_mutex);
> >>> +
> >>> +	return ret;
> >>> +}
> >>> +
> >>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +
> >>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> >>> +}
> >>> +
> >>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> >>> +	.owner				= THIS_MODULE,
> >>> +	.open				= fops_vcodec_open,
> >>> +	.release			= fops_vcodec_release,
> >>> +	.poll				= fops_vcodec_poll,
> >>> +	.unlocked_ioctl			= video_ioctl2,
> >>> +	.mmap				= fops_vcodec_mmap,
> >>
> >> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> >>
> > 
> > Hi Hans,
> > 
> > We are plaining to remove m2m framework in th feature, although we think
> 
> Remove it for just the decoder driver or both encoder and decoder?
> 
Remove it from decoder driver.

> > it is easy to use and could save a lot of code similar to what m2m
> > framework implemented and reduce code size.
> > The main reason is that in v4l2_m2m_try_schedule, it required that at
> > least one output buffer and one capture buffer to run device_run.
> > We want to start device_run without capture buffer queued.
> 
> I assume the reason is that you need to get the resolution etc. information
> from the encoded stream? Without a capture buffer you can't actually decode
> a frame, but that's probably not what this is about.
Yes, it could start parsing without waiting CAPTURE ready and get
CAPTURE buffer from vb2 when it really need it.


> > Is there any suggestion that we could use m2m framework but trigger
> > device_run with only output buffer.
> > Or we need to remove m2m and write our own implementation.
> 
> I am assuming that not using the m2m framework is for the decoder only and
> that its purpose is to obtain data about the encoded stream early.
> 
yes. it's for decode only.


> This is something that was discussed with Pawel in the past. I don't have a
> problem if you do it yourself (without the m2m framework), but it might also
> be an idea to adapt the framework for this.
> 
> Pawel, do you have any thoughts on that?
> 
> Regards,
> 
> 	Hans

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18  8:22               ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-18  8:22 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Hans,

On Mon, 2016-04-18 at 09:32 +0200, Hans Verkuil wrote:
> On 04/18/2016 07:40 AM, tiffany lin wrote:
> > 
> > snipped.
> > 
> >>> +
> >>> +void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
> >>> +{
> >>> +	struct mtk_q_data *q_data;
> >>> +
> >>> +	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
> >>> +	ctx->fh.m2m_ctx = ctx->m2m_ctx;
> >>> +	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
> >>> +	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
> >>> +		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
> >>> +	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
> >>> +
> >>> +
> >>> +	q_data = &ctx->q_data[MTK_Q_DATA_DST];
> >>> +	memset(q_data, 0, sizeof(struct mtk_q_data));
> >>> +	q_data->visible_width = DFT_CFG_WIDTH;
> >>> +	q_data->visible_height = DFT_CFG_HEIGHT;
> >>> +	q_data->coded_width = DFT_CFG_WIDTH;
> >>> +	q_data->coded_height = DFT_CFG_HEIGHT;
> >>> +	q_data->colorspace = V4L2_COLORSPACE_REC709;
> >>> +	q_data->field = V4L2_FIELD_NONE;
> >>> +
> >>> +	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
> >>> +
> >>> +	v4l_bound_align_image(&q_data->coded_width,
> >>> +					MTK_VDEC_MIN_W,
> >>> +					MTK_VDEC_MAX_W, 4,
> >>> +					&q_data->coded_height,
> >>> +					MTK_VDEC_MIN_H,
> >>> +					MTK_VDEC_MAX_H, 5, 6);
> >>> +
> >>> +	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
> >>> +	q_data->bytesperline[0] = q_data->coded_width;
> >>> +	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
> >>> +	q_data->bytesperline[1] = q_data->coded_width;
> >>> +
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamon(struct file *file, void *priv,
> >>> +				enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +
> >>> +	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_streamoff(struct file *file, void *priv,
> >>> +				 enum v4l2_buf_type type)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d)", ctx->idx, type);
> >>> +	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
> >>> +}
> >>> +
> >>> +static int vidioc_vdec_reqbufs(struct file *file, void *priv,
> >>> +			       struct v4l2_requestbuffers *reqbufs)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
> >>> +	int ret;
> >>> +
> >>> +	mtk_v4l2_debug(3, "[%d] (%d) count=%d", ctx->idx,
> >>> +			 reqbufs->type, reqbufs->count);
> >>> +	ret = v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
> >>> +
> >>> +	return ret;
> >>> +}
> >>
> >> Please use the v4l2_m2m_ioctl_* helper functions were applicable.
> >>
> > 
> > 
> > 
> > snipped.
> >>> +static unsigned int fops_vcodec_poll(struct file *file,
> >>> +				     struct poll_table_struct *wait)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +	struct mtk_vcodec_dev *dev = ctx->dev;
> >>> +	int ret;
> >>> +
> >>> +	mutex_lock(&dev->dev_mutex);
> >>> +	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
> >>> +	mutex_unlock(&dev->dev_mutex);
> >>> +
> >>> +	return ret;
> >>> +}
> >>> +
> >>> +static int fops_vcodec_mmap(struct file *file, struct vm_area_struct *vma)
> >>> +{
> >>> +	struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
> >>> +
> >>> +	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
> >>> +}
> >>> +
> >>> +static const struct v4l2_file_operations mtk_vcodec_fops = {
> >>> +	.owner				= THIS_MODULE,
> >>> +	.open				= fops_vcodec_open,
> >>> +	.release			= fops_vcodec_release,
> >>> +	.poll				= fops_vcodec_poll,
> >>> +	.unlocked_ioctl			= video_ioctl2,
> >>> +	.mmap				= fops_vcodec_mmap,
> >>
> >> You should be able to use the v4l2_m2m_fop helper functions for poll and mmap.
> >>
> > 
> > Hi Hans,
> > 
> > We are plaining to remove m2m framework in th feature, although we think
> 
> Remove it for just the decoder driver or both encoder and decoder?
> 
Remove it from decoder driver.

> > it is easy to use and could save a lot of code similar to what m2m
> > framework implemented and reduce code size.
> > The main reason is that in v4l2_m2m_try_schedule, it required that at
> > least one output buffer and one capture buffer to run device_run.
> > We want to start device_run without capture buffer queued.
> 
> I assume the reason is that you need to get the resolution etc. information
> from the encoded stream? Without a capture buffer you can't actually decode
> a frame, but that's probably not what this is about.
Yes, it could start parsing without waiting CAPTURE ready and get
CAPTURE buffer from vb2 when it really need it.


> > Is there any suggestion that we could use m2m framework but trigger
> > device_run with only output buffer.
> > Or we need to remove m2m and write our own implementation.
> 
> I am assuming that not using the m2m framework is for the decoder only and
> that its purpose is to obtain data about the encoded stream early.
> 
yes. it's for decode only.


> This is something that was discussed with Pawel in the past. I don't have a
> problem if you do it yourself (without the m2m framework), but it might also
> be an idea to adapt the framework for this.
> 
> Pawel, do you have any thoughts on that?
> 
> Regards,
> 
> 	Hans

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
  2016-04-18  8:22               ` tiffany lin
@ 2016-04-18 17:48                 ` Nicolas Dufresne
  -1 siblings, 0 replies; 52+ messages in thread
From: Nicolas Dufresne @ 2016-04-18 17:48 UTC (permalink / raw)
  To: tiffany lin, Hans Verkuil
  Cc: Pawel Osciak, Hans Verkuil, daniel.thompson, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen, devicetree,
	linux-kernel, linux-arm-kernel, linux-media, linux-mediatek,
	PoChun.Lin

Le lundi 18 avril 2016 à 16:22 +0800, tiffany lin a écrit :
> > > We are plaining to remove m2m framework in th feature, although
> we think
> > 
> > Remove it for just the decoder driver or both encoder and decoder?
> > 
> Remove it from decoder driver.

Did you look at how CODA handle it (drivers/media/platform/coda/coda-
common.c) ? I don't know any detail, but they do have the same issue
and use both v4l2_m2m_fop_poll and v4l2_m2m_fop_mmap.

cheers,
Nicolas

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-18 17:48                 ` Nicolas Dufresne
  0 siblings, 0 replies; 52+ messages in thread
From: Nicolas Dufresne @ 2016-04-18 17:48 UTC (permalink / raw)
  To: linux-arm-kernel

Le lundi 18 avril 2016 ? 16:22 +0800, tiffany lin a ?crit?:
> > > We are plaining to remove m2m framework in th feature, although
> we think
> >?
> > Remove it for just the decoder driver or both encoder and decoder?
> >?
> Remove it from decoder driver.

Did you look at how CODA handle it (drivers/media/platform/coda/coda-
common.c) ? I don't know any detail, but they do have the same issue
and use both?v4l2_m2m_fop_poll and?v4l2_m2m_fop_mmap.

cheers,
Nicolas

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-19  6:46                   ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-19  6:46 UTC (permalink / raw)
  To: nicolas
  Cc: Hans Verkuil, Pawel Osciak, Hans Verkuil, daniel.thompson,
	Rob Herring, Mauro Carvalho Chehab, Matthias Brugger,
	Daniel Kurtz, Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree, linux-kernel, linux-arm-kernel, linux-media,
	linux-mediatek, PoChun.Lin

Hi Nicolas,

On Mon, 2016-04-18 at 13:48 -0400, Nicolas Dufresne wrote:
> Le lundi 18 avril 2016 à 16:22 +0800, tiffany lin a écrit :
> > > > We are plaining to remove m2m framework in th feature, although
> > we think
> > > 
> > > Remove it for just the decoder driver or both encoder and decoder?
> > > 
> > Remove it from decoder driver.
> 
> Did you look at how CODA handle it (drivers/media/platform/coda/coda-
> common.c) ? I don't know any detail, but they do have the same issue
> and use both v4l2_m2m_fop_poll and v4l2_m2m_fop_mmap.
> 
I check coda-common.c, it use v4l2_m2m_set_src_buffered to allow
device_run be triggered without OUTPUT buffer.

Double check the patch "[media] mem2mem: add support for hardware
buffered queue".
This patch make m2m framework could support that
1. out-of-order frames, causing a few mem2mem device runs in the
beginning, that don't produce any decompressed buffer at the v4l2
capture side.
2. the last few frames can be decoded from the bitstream with mem2mem
device runs that don't need a new input buffer at the v4l2 output side.

This is similar our requirement that we want start decode without
CAPTURE buffer.
Is there any restriction that when v4l2_m2m_set_src_buffered can be
used?



best regards,
Tiffany

> cheers,
> Nicolas

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

* Re: [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-19  6:46                   ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-19  6:46 UTC (permalink / raw)
  To: nicolas-dDhyB4GVkw9AFePFGvp55w
  Cc: Hans Verkuil, Pawel Osciak, Hans Verkuil,
	daniel.thompson-QSEj5FYQhm4dnm+yROfE0A, Rob Herring,
	Mauro Carvalho Chehab, Matthias Brugger, Daniel Kurtz,
	Pawel Osciak, Eddie Huang, Yingjoe Chen,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-media-u79uwXL29TY76Z2rM5mHXA,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	PoChun.Lin-NuS5LvNUpcJWk0Htik3J/w

Hi Nicolas,

On Mon, 2016-04-18 at 13:48 -0400, Nicolas Dufresne wrote:
> Le lundi 18 avril 2016 à 16:22 +0800, tiffany lin a écrit :
> > > > We are plaining to remove m2m framework in th feature, although
> > we think
> > > 
> > > Remove it for just the decoder driver or both encoder and decoder?
> > > 
> > Remove it from decoder driver.
> 
> Did you look at how CODA handle it (drivers/media/platform/coda/coda-
> common.c) ? I don't know any detail, but they do have the same issue
> and use both v4l2_m2m_fop_poll and v4l2_m2m_fop_mmap.
> 
I check coda-common.c, it use v4l2_m2m_set_src_buffered to allow
device_run be triggered without OUTPUT buffer.

Double check the patch "[media] mem2mem: add support for hardware
buffered queue".
This patch make m2m framework could support that
1. out-of-order frames, causing a few mem2mem device runs in the
beginning, that don't produce any decompressed buffer at the v4l2
capture side.
2. the last few frames can be decoded from the bitstream with mem2mem
device runs that don't need a new input buffer at the v4l2 output side.

This is similar our requirement that we want start decode without
CAPTURE buffer.
Is there any restriction that when v4l2_m2m_set_src_buffered can be
used?



best regards,
Tiffany

> cheers,
> Nicolas


--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver
@ 2016-04-19  6:46                   ` tiffany lin
  0 siblings, 0 replies; 52+ messages in thread
From: tiffany lin @ 2016-04-19  6:46 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Nicolas,

On Mon, 2016-04-18 at 13:48 -0400, Nicolas Dufresne wrote:
> Le lundi 18 avril 2016 ? 16:22 +0800, tiffany lin a ?crit :
> > > > We are plaining to remove m2m framework in th feature, although
> > we think
> > > 
> > > Remove it for just the decoder driver or both encoder and decoder?
> > > 
> > Remove it from decoder driver.
> 
> Did you look at how CODA handle it (drivers/media/platform/coda/coda-
> common.c) ? I don't know any detail, but they do have the same issue
> and use both v4l2_m2m_fop_poll and v4l2_m2m_fop_mmap.
> 
I check coda-common.c, it use v4l2_m2m_set_src_buffered to allow
device_run be triggered without OUTPUT buffer.

Double check the patch "[media] mem2mem: add support for hardware
buffered queue".
This patch make m2m framework could support that
1. out-of-order frames, causing a few mem2mem device runs in the
beginning, that don't produce any decompressed buffer at the v4l2
capture side.
2. the last few frames can be decoded from the bitstream with mem2mem
device runs that don't need a new input buffer at the v4l2 output side.

This is similar our requirement that we want start decode without
CAPTURE buffer.
Is there any restriction that when v4l2_m2m_set_src_buffered can be
used?



best regards,
Tiffany

> cheers,
> Nicolas

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

end of thread, other threads:[~2016-04-19  6:46 UTC | newest]

Thread overview: 52+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-13 12:01 [PATCH 0/7] Add MT8173 Video Decoder Driver Tiffany Lin
2016-04-13 12:01 ` Tiffany Lin
2016-04-13 12:01 ` Tiffany Lin
2016-04-13 12:01 ` [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format Tiffany Lin
2016-04-13 12:01   ` Tiffany Lin
2016-04-13 12:01   ` Tiffany Lin
2016-04-13 12:01   ` [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder Tiffany Lin
2016-04-13 12:01     ` Tiffany Lin
2016-04-13 12:01     ` Tiffany Lin
2016-04-13 12:01     ` [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver Tiffany Lin
2016-04-13 12:01       ` Tiffany Lin
2016-04-13 12:01       ` Tiffany Lin
2016-04-13 12:01       ` [PATCH 4/7] [media] vcodec: mediatek: Add Mediatek H264 " Tiffany Lin
2016-04-13 12:01         ` Tiffany Lin
2016-04-13 12:01         ` Tiffany Lin
2016-04-13 12:01         ` [PATCH 5/7] [media] vcodec: mediatek: Add Mediatek VP8 " Tiffany Lin
2016-04-13 12:01           ` Tiffany Lin
2016-04-13 12:01           ` Tiffany Lin
2016-04-13 12:01           ` [PATCH 6/7] [media] vcodec: mediatek: Add Mediatek VP9 " Tiffany Lin
2016-04-13 12:01             ` Tiffany Lin
2016-04-13 12:01             ` Tiffany Lin
2016-04-13 12:01             ` [PATCH 7/7] arm64: dts: mediatek: Add Video Encoder for MT8173 Tiffany Lin
2016-04-13 12:01               ` Tiffany Lin
2016-04-13 12:01               ` Tiffany Lin
2016-04-15 14:27       ` [PATCH 3/7] [Media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver Hans Verkuil
2016-04-15 14:27         ` Hans Verkuil
2016-04-15 14:27         ` Hans Verkuil
2016-04-18  4:00         ` tiffany lin
2016-04-18  4:00           ` tiffany lin
2016-04-18  4:00           ` tiffany lin
2016-04-18  5:40         ` tiffany lin
2016-04-18  5:40           ` tiffany lin
2016-04-18  5:40           ` tiffany lin
2016-04-18  7:32           ` Hans Verkuil
2016-04-18  7:32             ` Hans Verkuil
2016-04-18  7:32             ` Hans Verkuil
2016-04-18  8:22             ` tiffany lin
2016-04-18  8:22               ` tiffany lin
2016-04-18  8:22               ` tiffany lin
2016-04-18 17:48               ` Nicolas Dufresne
2016-04-18 17:48                 ` Nicolas Dufresne
2016-04-19  6:46                 ` tiffany lin
2016-04-19  6:46                   ` tiffany lin
2016-04-19  6:46                   ` tiffany lin
2016-04-14 16:15     ` [PATCH 2/7] dt-bindings: Add a binding for Mediatek Video Decoder Rob Herring
2016-04-14 16:15       ` Rob Herring
2016-04-14 16:15       ` Rob Herring
2016-04-13 14:23   ` [PATCH 1/7] [media]: v4l: add Mediatek MT21 video block format Nicolas Dufresne
2016-04-13 14:23     ` Nicolas Dufresne
2016-04-14  6:13     ` tiffany lin
2016-04-14  6:13       ` tiffany lin
2016-04-14  6:13       ` tiffany lin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.