Re: [PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-25 Thread andrew-ct chen
On Mon, 2016-04-25 at 09:40 +0200, Hans Verkuil wrote:
> On 04/22/2016 06:25 AM, Tiffany Lin wrote:
> > From: Andrew-CT Chen 
> > 
> > The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
> > It is able to handle video decoding/encoding of in a range of formats.
> > The driver provides with VPU firmware download, memory management and
> > the communication interface between CPU and VPU.
> > For VPU initialization, it will create virtual memory for CPU access and
> > IOMMU address for vcodec hw device access. When a decode/encode instance
> > opens a device node, vpu driver will download vpu firmware to the device.
> > A decode/encode instant will decode/encode a frame using VPU
> > interface to interrupt vpu to handle decoding/encoding jobs.
> > 
> > Signed-off-by: Andrew-CT Chen 
> > Signed-off-by: Tiffany Lin 
> > 
> > ---
> >  drivers/media/platform/Kconfig   |   13 +
> >  drivers/media/platform/Makefile  |2 +
> >  drivers/media/platform/mtk-vpu/Makefile  |3 +
> >  drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 
> > ++
> >  drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
> >  5 files changed, 1130 insertions(+)
> >  create mode 100644 drivers/media/platform/mtk-vpu/Makefile
> >  create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
> >  create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h
> > 
> 
> 
> > +int vpu_load_firmware(struct platform_device *pdev)
> > +{
> > +   struct mtk_vpu *vpu = platform_get_drvdata(pdev);
> > +   struct device *dev = >dev;
> > +   struct vpu_run *run = >run;
> > +   const struct firmware *vpu_fw;
> > +   int ret;
> > +
> > +   if (!pdev) {
> > +   dev_err(dev, "VPU platform device is invalid\n");
> > +   return -EINVAL;
> > +   }
> > +
> > +   ret = vpu_clock_enable(vpu);
> > +   if (ret) {
> > +   dev_err(dev, "enable clock failed %d\n", ret);
> > +   return ret;
> > +   }
> > +
> > +   mutex_lock(>vpu_mutex);
> > +
> > +   if (vpu_running(vpu)) {
> > +   mutex_unlock(>vpu_mutex);
> > +   vpu_clock_disable(vpu);
> > +   dev_warn(dev, "vpu is running already\n");
> 
> This warning should be dropped. Currently vpu_load_firmware is called
> every time the video device is opened and no one else has the video device
> open. So calling this multiple times is perfectly normal and the log shouldn't
> be spammed with warnings.
> 
> I would recommend adding a fw_loaded bool to struct mtk_vpu and just
> check that at the beginning of this function and just return 0 if it is true.
> 
> Then you don't need to enable the vpu clock either.
> 
> I hope I understand the hw correctly, though.
> 
> Assuming you can do this, then this code from the v4l driver needs an
> additional comment:

We will change this in next version.

> 
> >>> + if (v4l2_fh_is_singular(>fh)) {
> 
> Add a comment here that says that vpu_load_firmware checks if it was
> loaded already and does nothing in that case.

We will change this in next version. Thanks

> 
> >>> + ret = vpu_load_firmware(dev->vpu_plat_dev);
> >>> + if (ret < 0) {
> >>> + /*
> >>> +   * Return 0 if downloading firmware successfully,
> >>> +   * otherwise it is failed
> >>> +   */
> >>> + mtk_v4l2_err("vpu_load_firmware failed!");
> >>> + goto err_load_fw;
> >>> + }
> 
> That makes it clear to the reader (i.e. me :-) ) that you can safely call
> vpu_load_firmware multiple times.
> 
> Regards,
> 
>   Hans
> 
> > +   return 0;
> > +   }
> > +
> > +   run->signaled = false;
> > +   dev_dbg(vpu->dev, "firmware request\n");
> > +   /* Downloading program firmware to device*/
> > +   ret = load_requested_vpu(vpu, vpu_fw, P_FW);
> > +   if (ret < 0) {
> > +   dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   /* Downloading data firmware to device */
> > +   ret = load_requested_vpu(vpu, vpu_fw, D_FW);
> > +   if (ret < 0) {
> > +   dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   /* boot up vpu */
> > +   vpu_cfg_writel(vpu, 0x1, VPU_RESET);
> > +
> > +   ret = wait_event_interruptible_timeout(run->wq,
> > +  run->signaled,
> > +  msecs_to_jiffies(INIT_TIMEOUT_MS)
> > +  );
> > +   if (ret == 0) {
> > +   ret = -ETIME;
> > +   dev_err(dev, "wait vpu initialization timout!\n");
> > +   goto OUT_LOAD_FW;
> > +   } else if (-ERESTARTSYS == ret) {
> > +   dev_err(dev, "wait vpu interrupted by a signal!\n");
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   ret = 0;
> > +   dev_info(dev, "vpu is 

Re: [PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-25 Thread andrew-ct chen
On Mon, 2016-04-25 at 09:40 +0200, Hans Verkuil wrote:
> On 04/22/2016 06:25 AM, Tiffany Lin wrote:
> > From: Andrew-CT Chen 
> > 
> > The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
> > It is able to handle video decoding/encoding of in a range of formats.
> > The driver provides with VPU firmware download, memory management and
> > the communication interface between CPU and VPU.
> > For VPU initialization, it will create virtual memory for CPU access and
> > IOMMU address for vcodec hw device access. When a decode/encode instance
> > opens a device node, vpu driver will download vpu firmware to the device.
> > A decode/encode instant will decode/encode a frame using VPU
> > interface to interrupt vpu to handle decoding/encoding jobs.
> > 
> > Signed-off-by: Andrew-CT Chen 
> > Signed-off-by: Tiffany Lin 
> > 
> > ---
> >  drivers/media/platform/Kconfig   |   13 +
> >  drivers/media/platform/Makefile  |2 +
> >  drivers/media/platform/mtk-vpu/Makefile  |3 +
> >  drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 
> > ++
> >  drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
> >  5 files changed, 1130 insertions(+)
> >  create mode 100644 drivers/media/platform/mtk-vpu/Makefile
> >  create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
> >  create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h
> > 
> 
> 
> > +int vpu_load_firmware(struct platform_device *pdev)
> > +{
> > +   struct mtk_vpu *vpu = platform_get_drvdata(pdev);
> > +   struct device *dev = >dev;
> > +   struct vpu_run *run = >run;
> > +   const struct firmware *vpu_fw;
> > +   int ret;
> > +
> > +   if (!pdev) {
> > +   dev_err(dev, "VPU platform device is invalid\n");
> > +   return -EINVAL;
> > +   }
> > +
> > +   ret = vpu_clock_enable(vpu);
> > +   if (ret) {
> > +   dev_err(dev, "enable clock failed %d\n", ret);
> > +   return ret;
> > +   }
> > +
> > +   mutex_lock(>vpu_mutex);
> > +
> > +   if (vpu_running(vpu)) {
> > +   mutex_unlock(>vpu_mutex);
> > +   vpu_clock_disable(vpu);
> > +   dev_warn(dev, "vpu is running already\n");
> 
> This warning should be dropped. Currently vpu_load_firmware is called
> every time the video device is opened and no one else has the video device
> open. So calling this multiple times is perfectly normal and the log shouldn't
> be spammed with warnings.
> 
> I would recommend adding a fw_loaded bool to struct mtk_vpu and just
> check that at the beginning of this function and just return 0 if it is true.
> 
> Then you don't need to enable the vpu clock either.
> 
> I hope I understand the hw correctly, though.
> 
> Assuming you can do this, then this code from the v4l driver needs an
> additional comment:

We will change this in next version.

> 
> >>> + if (v4l2_fh_is_singular(>fh)) {
> 
> Add a comment here that says that vpu_load_firmware checks if it was
> loaded already and does nothing in that case.

We will change this in next version. Thanks

> 
> >>> + ret = vpu_load_firmware(dev->vpu_plat_dev);
> >>> + if (ret < 0) {
> >>> + /*
> >>> +   * Return 0 if downloading firmware successfully,
> >>> +   * otherwise it is failed
> >>> +   */
> >>> + mtk_v4l2_err("vpu_load_firmware failed!");
> >>> + goto err_load_fw;
> >>> + }
> 
> That makes it clear to the reader (i.e. me :-) ) that you can safely call
> vpu_load_firmware multiple times.
> 
> Regards,
> 
>   Hans
> 
> > +   return 0;
> > +   }
> > +
> > +   run->signaled = false;
> > +   dev_dbg(vpu->dev, "firmware request\n");
> > +   /* Downloading program firmware to device*/
> > +   ret = load_requested_vpu(vpu, vpu_fw, P_FW);
> > +   if (ret < 0) {
> > +   dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   /* Downloading data firmware to device */
> > +   ret = load_requested_vpu(vpu, vpu_fw, D_FW);
> > +   if (ret < 0) {
> > +   dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   /* boot up vpu */
> > +   vpu_cfg_writel(vpu, 0x1, VPU_RESET);
> > +
> > +   ret = wait_event_interruptible_timeout(run->wq,
> > +  run->signaled,
> > +  msecs_to_jiffies(INIT_TIMEOUT_MS)
> > +  );
> > +   if (ret == 0) {
> > +   ret = -ETIME;
> > +   dev_err(dev, "wait vpu initialization timout!\n");
> > +   goto OUT_LOAD_FW;
> > +   } else if (-ERESTARTSYS == ret) {
> > +   dev_err(dev, "wait vpu interrupted by a signal!\n");
> > +   goto OUT_LOAD_FW;
> > +   }
> > +
> > +   ret = 0;
> > +   dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
> > +
> > +OUT_LOAD_FW:
> > +   

Re: [PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-25 Thread Hans Verkuil
On 04/22/2016 06:25 AM, Tiffany Lin wrote:
> From: Andrew-CT Chen 
> 
> The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
> It is able to handle video decoding/encoding of in a range of formats.
> The driver provides with VPU firmware download, memory management and
> the communication interface between CPU and VPU.
> For VPU initialization, it will create virtual memory for CPU access and
> IOMMU address for vcodec hw device access. When a decode/encode instance
> opens a device node, vpu driver will download vpu firmware to the device.
> A decode/encode instant will decode/encode a frame using VPU
> interface to interrupt vpu to handle decoding/encoding jobs.
> 
> Signed-off-by: Andrew-CT Chen 
> Signed-off-by: Tiffany Lin 
> 
> ---
>  drivers/media/platform/Kconfig   |   13 +
>  drivers/media/platform/Makefile  |2 +
>  drivers/media/platform/mtk-vpu/Makefile  |3 +
>  drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 
> ++
>  drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
>  5 files changed, 1130 insertions(+)
>  create mode 100644 drivers/media/platform/mtk-vpu/Makefile
>  create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
>  create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h
> 


> +int vpu_load_firmware(struct platform_device *pdev)
> +{
> + struct mtk_vpu *vpu = platform_get_drvdata(pdev);
> + struct device *dev = >dev;
> + struct vpu_run *run = >run;
> + const struct firmware *vpu_fw;
> + int ret;
> +
> + if (!pdev) {
> + dev_err(dev, "VPU platform device is invalid\n");
> + return -EINVAL;
> + }
> +
> + ret = vpu_clock_enable(vpu);
> + if (ret) {
> + dev_err(dev, "enable clock failed %d\n", ret);
> + return ret;
> + }
> +
> + mutex_lock(>vpu_mutex);
> +
> + if (vpu_running(vpu)) {
> + mutex_unlock(>vpu_mutex);
> + vpu_clock_disable(vpu);
> + dev_warn(dev, "vpu is running already\n");

This warning should be dropped. Currently vpu_load_firmware is called
every time the video device is opened and no one else has the video device
open. So calling this multiple times is perfectly normal and the log shouldn't
be spammed with warnings.

I would recommend adding a fw_loaded bool to struct mtk_vpu and just
check that at the beginning of this function and just return 0 if it is true.

Then you don't need to enable the vpu clock either.

I hope I understand the hw correctly, though.

Assuming you can do this, then this code from the v4l driver needs an
additional comment:

>>> +   if (v4l2_fh_is_singular(>fh)) {

Add a comment here that says that vpu_load_firmware checks if it was
loaded already and does nothing in that case.

>>> +   ret = vpu_load_firmware(dev->vpu_plat_dev);
>>> +   if (ret < 0) {
>>> +   /*
>>> + * Return 0 if downloading firmware successfully,
>>> + * otherwise it is failed
>>> + */
>>> +   mtk_v4l2_err("vpu_load_firmware failed!");
>>> +   goto err_load_fw;
>>> +   }

That makes it clear to the reader (i.e. me :-) ) that you can safely call
vpu_load_firmware multiple times.

Regards,

Hans

> + return 0;
> + }
> +
> + run->signaled = false;
> + dev_dbg(vpu->dev, "firmware request\n");
> + /* Downloading program firmware to device*/
> + ret = load_requested_vpu(vpu, vpu_fw, P_FW);
> + if (ret < 0) {
> + dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
> + goto OUT_LOAD_FW;
> + }
> +
> + /* Downloading data firmware to device */
> + ret = load_requested_vpu(vpu, vpu_fw, D_FW);
> + if (ret < 0) {
> + dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
> + goto OUT_LOAD_FW;
> + }
> +
> + /* boot up vpu */
> + vpu_cfg_writel(vpu, 0x1, VPU_RESET);
> +
> + ret = wait_event_interruptible_timeout(run->wq,
> +run->signaled,
> +msecs_to_jiffies(INIT_TIMEOUT_MS)
> +);
> + if (ret == 0) {
> + ret = -ETIME;
> + dev_err(dev, "wait vpu initialization timout!\n");
> + goto OUT_LOAD_FW;
> + } else if (-ERESTARTSYS == ret) {
> + dev_err(dev, "wait vpu interrupted by a signal!\n");
> + goto OUT_LOAD_FW;
> + }
> +
> + ret = 0;
> + dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
> +
> +OUT_LOAD_FW:
> + mutex_unlock(>vpu_mutex);
> + vpu_clock_disable(vpu);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(vpu_load_firmware);



Re: [PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-25 Thread Hans Verkuil
On 04/22/2016 06:25 AM, Tiffany Lin wrote:
> From: Andrew-CT Chen 
> 
> The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
> It is able to handle video decoding/encoding of in a range of formats.
> The driver provides with VPU firmware download, memory management and
> the communication interface between CPU and VPU.
> For VPU initialization, it will create virtual memory for CPU access and
> IOMMU address for vcodec hw device access. When a decode/encode instance
> opens a device node, vpu driver will download vpu firmware to the device.
> A decode/encode instant will decode/encode a frame using VPU
> interface to interrupt vpu to handle decoding/encoding jobs.
> 
> Signed-off-by: Andrew-CT Chen 
> Signed-off-by: Tiffany Lin 
> 
> ---
>  drivers/media/platform/Kconfig   |   13 +
>  drivers/media/platform/Makefile  |2 +
>  drivers/media/platform/mtk-vpu/Makefile  |3 +
>  drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 
> ++
>  drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
>  5 files changed, 1130 insertions(+)
>  create mode 100644 drivers/media/platform/mtk-vpu/Makefile
>  create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
>  create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h
> 


> +int vpu_load_firmware(struct platform_device *pdev)
> +{
> + struct mtk_vpu *vpu = platform_get_drvdata(pdev);
> + struct device *dev = >dev;
> + struct vpu_run *run = >run;
> + const struct firmware *vpu_fw;
> + int ret;
> +
> + if (!pdev) {
> + dev_err(dev, "VPU platform device is invalid\n");
> + return -EINVAL;
> + }
> +
> + ret = vpu_clock_enable(vpu);
> + if (ret) {
> + dev_err(dev, "enable clock failed %d\n", ret);
> + return ret;
> + }
> +
> + mutex_lock(>vpu_mutex);
> +
> + if (vpu_running(vpu)) {
> + mutex_unlock(>vpu_mutex);
> + vpu_clock_disable(vpu);
> + dev_warn(dev, "vpu is running already\n");

This warning should be dropped. Currently vpu_load_firmware is called
every time the video device is opened and no one else has the video device
open. So calling this multiple times is perfectly normal and the log shouldn't
be spammed with warnings.

I would recommend adding a fw_loaded bool to struct mtk_vpu and just
check that at the beginning of this function and just return 0 if it is true.

Then you don't need to enable the vpu clock either.

I hope I understand the hw correctly, though.

Assuming you can do this, then this code from the v4l driver needs an
additional comment:

>>> +   if (v4l2_fh_is_singular(>fh)) {

Add a comment here that says that vpu_load_firmware checks if it was
loaded already and does nothing in that case.

>>> +   ret = vpu_load_firmware(dev->vpu_plat_dev);
>>> +   if (ret < 0) {
>>> +   /*
>>> + * Return 0 if downloading firmware successfully,
>>> + * otherwise it is failed
>>> + */
>>> +   mtk_v4l2_err("vpu_load_firmware failed!");
>>> +   goto err_load_fw;
>>> +   }

That makes it clear to the reader (i.e. me :-) ) that you can safely call
vpu_load_firmware multiple times.

Regards,

Hans

> + return 0;
> + }
> +
> + run->signaled = false;
> + dev_dbg(vpu->dev, "firmware request\n");
> + /* Downloading program firmware to device*/
> + ret = load_requested_vpu(vpu, vpu_fw, P_FW);
> + if (ret < 0) {
> + dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
> + goto OUT_LOAD_FW;
> + }
> +
> + /* Downloading data firmware to device */
> + ret = load_requested_vpu(vpu, vpu_fw, D_FW);
> + if (ret < 0) {
> + dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
> + goto OUT_LOAD_FW;
> + }
> +
> + /* boot up vpu */
> + vpu_cfg_writel(vpu, 0x1, VPU_RESET);
> +
> + ret = wait_event_interruptible_timeout(run->wq,
> +run->signaled,
> +msecs_to_jiffies(INIT_TIMEOUT_MS)
> +);
> + if (ret == 0) {
> + ret = -ETIME;
> + dev_err(dev, "wait vpu initialization timout!\n");
> + goto OUT_LOAD_FW;
> + } else if (-ERESTARTSYS == ret) {
> + dev_err(dev, "wait vpu interrupted by a signal!\n");
> + goto OUT_LOAD_FW;
> + }
> +
> + ret = 0;
> + dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
> +
> +OUT_LOAD_FW:
> + mutex_unlock(>vpu_mutex);
> + vpu_clock_disable(vpu);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(vpu_load_firmware);



[PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-21 Thread Tiffany Lin
From: Andrew-CT Chen 

The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
It is able to handle video decoding/encoding of in a range of formats.
The driver provides with VPU firmware download, memory management and
the communication interface between CPU and VPU.
For VPU initialization, it will create virtual memory for CPU access and
IOMMU address for vcodec hw device access. When a decode/encode instance
opens a device node, vpu driver will download vpu firmware to the device.
A decode/encode instant will decode/encode a frame using VPU
interface to interrupt vpu to handle decoding/encoding jobs.

Signed-off-by: Andrew-CT Chen 
Signed-off-by: Tiffany Lin 

---
 drivers/media/platform/Kconfig   |   13 +
 drivers/media/platform/Makefile  |2 +
 drivers/media/platform/mtk-vpu/Makefile  |3 +
 drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 ++
 drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
 5 files changed, 1130 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vpu/Makefile
 create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
 create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h

diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig
index 84e041c..74c3575 100644
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -152,6 +152,19 @@ config VIDEO_CODA
   Coda is a range of video codec IPs that supports
   H.264, MPEG-4, and other video formats.
 
+config VIDEO_MEDIATEK_VPU
+   tristate "Mediatek Video Processor Unit"
+   depends on VIDEO_DEV && VIDEO_V4L2
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   ---help---
+   This driver provides downloading VPU firmware and
+   communicating with VPU. This driver for hw video
+   codec embedded in Mediatek's MT8173 SOCs. It is able
+   to handle video decoding/encoding in a range of formats.
+
+   To compile this driver as a module, choose M here: the
+   module will be called mtk-vpu.
+
 config VIDEO_MEM2MEM_DEINTERLACE
tristate "Deinterlace support"
depends on VIDEO_DEV && VIDEO_V4L2 && DMA_ENGINE
diff --git a/drivers/media/platform/Makefile b/drivers/media/platform/Makefile
index bbb7bd1..2efb7b1 100644
--- a/drivers/media/platform/Makefile
+++ b/drivers/media/platform/Makefile
@@ -56,3 +56,5 @@ obj-$(CONFIG_VIDEO_AM437X_VPFE)   += am437x/
 obj-$(CONFIG_VIDEO_XILINX) += xilinx/
 
 ccflags-y += -I$(srctree)/drivers/media/i2c
+
+obj-$(CONFIG_VIDEO_MEDIATEK_VPU)   += mtk-vpu/
diff --git a/drivers/media/platform/mtk-vpu/Makefile 
b/drivers/media/platform/mtk-vpu/Makefile
new file mode 100644
index 000..58cc1b4
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/Makefile
@@ -0,0 +1,3 @@
+mtk-vpu-y += mtk_vpu.o
+
+obj-$(CONFIG_VIDEO_MEDIATEK_VPU) += mtk-vpu.o
diff --git a/drivers/media/platform/mtk-vpu/mtk_vpu.c 
b/drivers/media/platform/mtk-vpu/mtk_vpu.c
new file mode 100755
index 000..55c081e
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/mtk_vpu.c
@@ -0,0 +1,950 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Andrew-CT Chen 
+*
+* 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mtk_vpu.h"
+
+/**
+ * VPU (video processor unit) is a tiny processor controlling video hardware
+ * related to video codec, scaling and color format converting.
+ * VPU interfaces with other blocks by share memory and interrupt.
+ **/
+
+#define INIT_TIMEOUT_MS2000U
+#define IPI_TIMEOUT_MS 2000U
+#define VPU_FW_VER_LEN 16
+
+/* maximum program/data TCM (Tightly-Coupled Memory) size */
+#define VPU_PTCM_SIZE  (96 * SZ_1K)
+#define VPU_DTCM_SIZE  (32 * SZ_1K)
+/* the offset to get data tcm address */
+#define VPU_DTCM_OFFSET0x18000UL
+/* daynamic allocated maximum extended memory size */
+#define VPU_EXT_P_SIZE SZ_1M
+#define VPU_EXT_D_SIZE SZ_4M
+/* maximum binary firmware size */
+#define VPU_P_FW_SIZE  (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
+#define VPU_D_FW_SIZE  (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
+/* the size of share buffer between Host and  VPU */
+#define SHARE_BUF_SIZE 48
+
+/* binary firmware name */
+#define VPU_P_FW   "vpu_p.bin"
+#define 

[PATCH v7 2/8] [media] VPU: mediatek: support Mediatek VPU

2016-04-21 Thread Tiffany Lin
From: Andrew-CT Chen 

The VPU driver for hw video codec embedded in Mediatek's MT8173 SOCs.
It is able to handle video decoding/encoding of in a range of formats.
The driver provides with VPU firmware download, memory management and
the communication interface between CPU and VPU.
For VPU initialization, it will create virtual memory for CPU access and
IOMMU address for vcodec hw device access. When a decode/encode instance
opens a device node, vpu driver will download vpu firmware to the device.
A decode/encode instant will decode/encode a frame using VPU
interface to interrupt vpu to handle decoding/encoding jobs.

Signed-off-by: Andrew-CT Chen 
Signed-off-by: Tiffany Lin 

---
 drivers/media/platform/Kconfig   |   13 +
 drivers/media/platform/Makefile  |2 +
 drivers/media/platform/mtk-vpu/Makefile  |3 +
 drivers/media/platform/mtk-vpu/mtk_vpu.c |  950 ++
 drivers/media/platform/mtk-vpu/mtk_vpu.h |  162 +
 5 files changed, 1130 insertions(+)
 create mode 100644 drivers/media/platform/mtk-vpu/Makefile
 create mode 100755 drivers/media/platform/mtk-vpu/mtk_vpu.c
 create mode 100644 drivers/media/platform/mtk-vpu/mtk_vpu.h

diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig
index 84e041c..74c3575 100644
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -152,6 +152,19 @@ config VIDEO_CODA
   Coda is a range of video codec IPs that supports
   H.264, MPEG-4, and other video formats.
 
+config VIDEO_MEDIATEK_VPU
+   tristate "Mediatek Video Processor Unit"
+   depends on VIDEO_DEV && VIDEO_V4L2
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   ---help---
+   This driver provides downloading VPU firmware and
+   communicating with VPU. This driver for hw video
+   codec embedded in Mediatek's MT8173 SOCs. It is able
+   to handle video decoding/encoding in a range of formats.
+
+   To compile this driver as a module, choose M here: the
+   module will be called mtk-vpu.
+
 config VIDEO_MEM2MEM_DEINTERLACE
tristate "Deinterlace support"
depends on VIDEO_DEV && VIDEO_V4L2 && DMA_ENGINE
diff --git a/drivers/media/platform/Makefile b/drivers/media/platform/Makefile
index bbb7bd1..2efb7b1 100644
--- a/drivers/media/platform/Makefile
+++ b/drivers/media/platform/Makefile
@@ -56,3 +56,5 @@ obj-$(CONFIG_VIDEO_AM437X_VPFE)   += am437x/
 obj-$(CONFIG_VIDEO_XILINX) += xilinx/
 
 ccflags-y += -I$(srctree)/drivers/media/i2c
+
+obj-$(CONFIG_VIDEO_MEDIATEK_VPU)   += mtk-vpu/
diff --git a/drivers/media/platform/mtk-vpu/Makefile 
b/drivers/media/platform/mtk-vpu/Makefile
new file mode 100644
index 000..58cc1b4
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/Makefile
@@ -0,0 +1,3 @@
+mtk-vpu-y += mtk_vpu.o
+
+obj-$(CONFIG_VIDEO_MEDIATEK_VPU) += mtk-vpu.o
diff --git a/drivers/media/platform/mtk-vpu/mtk_vpu.c 
b/drivers/media/platform/mtk-vpu/mtk_vpu.c
new file mode 100755
index 000..55c081e
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/mtk_vpu.c
@@ -0,0 +1,950 @@
+/*
+* Copyright (c) 2016 MediaTek Inc.
+* Author: Andrew-CT Chen 
+*
+* 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mtk_vpu.h"
+
+/**
+ * VPU (video processor unit) is a tiny processor controlling video hardware
+ * related to video codec, scaling and color format converting.
+ * VPU interfaces with other blocks by share memory and interrupt.
+ **/
+
+#define INIT_TIMEOUT_MS2000U
+#define IPI_TIMEOUT_MS 2000U
+#define VPU_FW_VER_LEN 16
+
+/* maximum program/data TCM (Tightly-Coupled Memory) size */
+#define VPU_PTCM_SIZE  (96 * SZ_1K)
+#define VPU_DTCM_SIZE  (32 * SZ_1K)
+/* the offset to get data tcm address */
+#define VPU_DTCM_OFFSET0x18000UL
+/* daynamic allocated maximum extended memory size */
+#define VPU_EXT_P_SIZE SZ_1M
+#define VPU_EXT_D_SIZE SZ_4M
+/* maximum binary firmware size */
+#define VPU_P_FW_SIZE  (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
+#define VPU_D_FW_SIZE  (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
+/* the size of share buffer between Host and  VPU */
+#define SHARE_BUF_SIZE 48
+
+/* binary firmware name */
+#define VPU_P_FW   "vpu_p.bin"
+#define VPU_D_FW   "vpu_d.bin"
+
+#define VPU_RESET  0x0
+#define VPU_TCM_CFG0x0008
+#define