/*
 * Copyright 2024 Sony Corporation
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/of.h>
#include <linux/types.h>
#include <linux/platform_device.h>
#include <uapi/linux/mtk-v4l2-pq.h>
#include <linux/videodev2.h>
#include <linux/uaccess.h>

#include <media/v4l2-mem2mem.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-vmalloc.h>
#include <media/v4l2-ctrls.h>

#include "mtk_pq.h"
#include "mtk_pq_pqd.h"
#include "mtk_pq_pqd_sony.h"
#include "pqu_msg.h"
#include "m_pqu_pq.h"
#include "mtk_pq_common.h"
#include "ext_command_video_if.h"
//-----------------------------------------------------------------------------
//  Local Defines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//  Local Variables
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// LOCAL FUNCTION
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//  FUNCTION
//-----------------------------------------------------------------------------

int mtk_pqd_SetPQCustomParam(struct meta_buffer *pqu_buf, struct meta_buffer *pqhal_buf, __u32 win_id)
{
	struct meta_custom_pq_pqparam *pstCustomPqparamPqhal = NULL;
	int ret = 0;

	if (!pqu_buf) {
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR, "Invalid meta_buf input\n");
		return -EFAULT;
	}
	if (!pqhal_buf) {
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR, "Invalid meta_buf input\n");
		return -EFAULT;
	}

	/* read metadata written by pqhal */
	ret = mtk_pq_common_read_metadata_addr_ptr(pqhal_buf,
		EN_PQ_METATAG_PQ_CUSTOMER1_TAG, (void **)&pstCustomPqparamPqhal);
	if (ret) {
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR, "va:%#llx, size:%x\n",
			(__u64)pqhal_buf->paddr, pqhal_buf->size);
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR,
			" read EN_PQ_METATAG_PQ_CUSTOMER1_TAG Failed, ret = %d\n", ret);
		return -EFAULT;
	}
	STI_PQ_LOG(STI_PQ_LOG_LEVEL_ENHANCE, "[PQDD][PQ_CUSTOM][win_id:%d][ver:%d][%s]\n",
		win_id, pstCustomPqparamPqhal->version, pstCustomPqparamPqhal->data);

	/* write metadata for pqu read */
	ret = mtk_pq_common_write_metadata_addr(pqu_buf,
		EN_PQ_METATAG_PQ_CUSTOMER1_PQU_TAG, (void *)pstCustomPqparamPqhal);
	if (ret) {
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR, "va:%#llx, size:%x\n",
			(__u64)pqu_buf->paddr, pqu_buf->size);
		STI_PQ_LOG(STI_PQ_LOG_LEVEL_ERROR,
			" write EN_PQ_METATAG_PQ_CUSTOMER1_PQU_TAG Failed, ret = %d\n", ret);
		return -EFAULT;
	}

	return 0;
}
