18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * i.MX Pixel Pipeline (PXP) mem-to-mem scaler/CSC/rotator driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2018 Pengutronix, Philipp Zabel
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * based on vim2m
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
108c2ecf20Sopenharmony_ci * Pawel Osciak, <pawel@osciak.com>
118c2ecf20Sopenharmony_ci * Marek Szyprowski, <m.szyprowski@samsung.com>
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/sched.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
258c2ecf20Sopenharmony_ci#include <media/v4l2-mem2mem.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
298c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
308c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "imx-pxp.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic unsigned int debug;
358c2ecf20Sopenharmony_cimodule_param(debug, uint, 0644);
368c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "activates debug info");
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define MIN_W 8
398c2ecf20Sopenharmony_ci#define MIN_H 8
408c2ecf20Sopenharmony_ci#define MAX_W 4096
418c2ecf20Sopenharmony_ci#define MAX_H 4096
428c2ecf20Sopenharmony_ci#define ALIGN_W 3 /* 8x8 pixel blocks */
438c2ecf20Sopenharmony_ci#define ALIGN_H 3
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Flags that indicate a format can be used for capture/output */
468c2ecf20Sopenharmony_ci#define MEM2MEM_CAPTURE	(1 << 0)
478c2ecf20Sopenharmony_ci#define MEM2MEM_OUTPUT	(1 << 1)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define MEM2MEM_NAME		"pxp"
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* Flags that indicate processing mode */
528c2ecf20Sopenharmony_ci#define MEM2MEM_HFLIP	(1 << 0)
538c2ecf20Sopenharmony_ci#define MEM2MEM_VFLIP	(1 << 1)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define dprintk(dev, fmt, arg...) \
568c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct pxp_fmt {
598c2ecf20Sopenharmony_ci	u32	fourcc;
608c2ecf20Sopenharmony_ci	int	depth;
618c2ecf20Sopenharmony_ci	/* Types the format can be used for */
628c2ecf20Sopenharmony_ci	u32	types;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic struct pxp_fmt formats[] = {
668c2ecf20Sopenharmony_ci	{
678c2ecf20Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_XBGR32,
688c2ecf20Sopenharmony_ci		.depth	= 32,
698c2ecf20Sopenharmony_ci		/* Both capture and output format */
708c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
718c2ecf20Sopenharmony_ci	}, {
728c2ecf20Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_ABGR32,
738c2ecf20Sopenharmony_ci		.depth	= 32,
748c2ecf20Sopenharmony_ci		/* Capture-only format */
758c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE,
768c2ecf20Sopenharmony_ci	}, {
778c2ecf20Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_BGR24,
788c2ecf20Sopenharmony_ci		.depth	= 24,
798c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE,
808c2ecf20Sopenharmony_ci	}, {
818c2ecf20Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGB565,
828c2ecf20Sopenharmony_ci		.depth	= 16,
838c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
848c2ecf20Sopenharmony_ci	}, {
858c2ecf20Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGB555,
868c2ecf20Sopenharmony_ci		.depth	= 16,
878c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
888c2ecf20Sopenharmony_ci	}, {
898c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_RGB444,
908c2ecf20Sopenharmony_ci		.depth	= 16,
918c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
928c2ecf20Sopenharmony_ci	}, {
938c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_VUYA32,
948c2ecf20Sopenharmony_ci		.depth	= 32,
958c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE,
968c2ecf20Sopenharmony_ci	}, {
978c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_VUYX32,
988c2ecf20Sopenharmony_ci		.depth	= 32,
998c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1008c2ecf20Sopenharmony_ci	}, {
1018c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_UYVY,
1028c2ecf20Sopenharmony_ci		.depth	= 16,
1038c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1048c2ecf20Sopenharmony_ci	}, {
1058c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_YUYV,
1068c2ecf20Sopenharmony_ci		.depth	= 16,
1078c2ecf20Sopenharmony_ci		/* Output-only format */
1088c2ecf20Sopenharmony_ci		.types	= MEM2MEM_OUTPUT,
1098c2ecf20Sopenharmony_ci	}, {
1108c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_VYUY,
1118c2ecf20Sopenharmony_ci		.depth	= 16,
1128c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1138c2ecf20Sopenharmony_ci	}, {
1148c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_YVYU,
1158c2ecf20Sopenharmony_ci		.depth	= 16,
1168c2ecf20Sopenharmony_ci		.types	= MEM2MEM_OUTPUT,
1178c2ecf20Sopenharmony_ci	}, {
1188c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_GREY,
1198c2ecf20Sopenharmony_ci		.depth	= 8,
1208c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1218c2ecf20Sopenharmony_ci	}, {
1228c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_Y4,
1238c2ecf20Sopenharmony_ci		.depth	= 4,
1248c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1258c2ecf20Sopenharmony_ci	}, {
1268c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV16,
1278c2ecf20Sopenharmony_ci		.depth	= 16,
1288c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1298c2ecf20Sopenharmony_ci	}, {
1308c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV12,
1318c2ecf20Sopenharmony_ci		.depth	= 12,
1328c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1338c2ecf20Sopenharmony_ci	}, {
1348c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV21,
1358c2ecf20Sopenharmony_ci		.depth	= 12,
1368c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1378c2ecf20Sopenharmony_ci	}, {
1388c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV61,
1398c2ecf20Sopenharmony_ci		.depth	= 16,
1408c2ecf20Sopenharmony_ci		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
1418c2ecf20Sopenharmony_ci	}, {
1428c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_YUV422P,
1438c2ecf20Sopenharmony_ci		.depth	= 16,
1448c2ecf20Sopenharmony_ci		.types	= MEM2MEM_OUTPUT,
1458c2ecf20Sopenharmony_ci	}, {
1468c2ecf20Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_YUV420,
1478c2ecf20Sopenharmony_ci		.depth	= 12,
1488c2ecf20Sopenharmony_ci		.types	= MEM2MEM_OUTPUT,
1498c2ecf20Sopenharmony_ci	},
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define NUM_FORMATS ARRAY_SIZE(formats)
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* Per-queue, driver-specific private data */
1558c2ecf20Sopenharmony_cistruct pxp_q_data {
1568c2ecf20Sopenharmony_ci	unsigned int		width;
1578c2ecf20Sopenharmony_ci	unsigned int		height;
1588c2ecf20Sopenharmony_ci	unsigned int		bytesperline;
1598c2ecf20Sopenharmony_ci	unsigned int		sizeimage;
1608c2ecf20Sopenharmony_ci	unsigned int		sequence;
1618c2ecf20Sopenharmony_ci	struct pxp_fmt		*fmt;
1628c2ecf20Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_enc;
1638c2ecf20Sopenharmony_ci	enum v4l2_quantization	quant;
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cienum {
1678c2ecf20Sopenharmony_ci	V4L2_M2M_SRC = 0,
1688c2ecf20Sopenharmony_ci	V4L2_M2M_DST = 1,
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic struct pxp_fmt *find_format(struct v4l2_format *f)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct pxp_fmt *fmt;
1748c2ecf20Sopenharmony_ci	unsigned int k;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	for (k = 0; k < NUM_FORMATS; k++) {
1778c2ecf20Sopenharmony_ci		fmt = &formats[k];
1788c2ecf20Sopenharmony_ci		if (fmt->fourcc == f->fmt.pix.pixelformat)
1798c2ecf20Sopenharmony_ci			break;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (k == NUM_FORMATS)
1838c2ecf20Sopenharmony_ci		return NULL;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	return &formats[k];
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistruct pxp_dev {
1898c2ecf20Sopenharmony_ci	struct v4l2_device	v4l2_dev;
1908c2ecf20Sopenharmony_ci	struct video_device	vfd;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	struct clk		*clk;
1938c2ecf20Sopenharmony_ci	void __iomem		*mmio;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	atomic_t		num_inst;
1968c2ecf20Sopenharmony_ci	struct mutex		dev_mutex;
1978c2ecf20Sopenharmony_ci	spinlock_t		irqlock;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	struct v4l2_m2m_dev	*m2m_dev;
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistruct pxp_ctx {
2038c2ecf20Sopenharmony_ci	struct v4l2_fh		fh;
2048c2ecf20Sopenharmony_ci	struct pxp_dev	*dev;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* Abort requested by m2m */
2098c2ecf20Sopenharmony_ci	int			aborting;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* Processing mode */
2128c2ecf20Sopenharmony_ci	int			mode;
2138c2ecf20Sopenharmony_ci	u8			alpha_component;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	enum v4l2_colorspace	colorspace;
2168c2ecf20Sopenharmony_ci	enum v4l2_xfer_func	xfer_func;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* Source and destination queue data */
2198c2ecf20Sopenharmony_ci	struct pxp_q_data   q_data[2];
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic inline struct pxp_ctx *file2ctx(struct file *file)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	return container_of(file->private_data, struct pxp_ctx, fh);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic struct pxp_q_data *get_q_data(struct pxp_ctx *ctx,
2288c2ecf20Sopenharmony_ci					 enum v4l2_buf_type type)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2318c2ecf20Sopenharmony_ci		return &ctx->q_data[V4L2_M2M_SRC];
2328c2ecf20Sopenharmony_ci	else
2338c2ecf20Sopenharmony_ci		return &ctx->q_data[V4L2_M2M_DST];
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic u32 pxp_v4l2_pix_fmt_to_ps_format(u32 v4l2_pix_fmt)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	switch (v4l2_pix_fmt) {
2398c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XBGR32:  return BV_PXP_PS_CTRL_FORMAT__RGB888;
2408c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB555:  return BV_PXP_PS_CTRL_FORMAT__RGB555;
2418c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB444:  return BV_PXP_PS_CTRL_FORMAT__RGB444;
2428c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565:  return BV_PXP_PS_CTRL_FORMAT__RGB565;
2438c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VUYX32:  return BV_PXP_PS_CTRL_FORMAT__YUV1P444;
2448c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:    return BV_PXP_PS_CTRL_FORMAT__UYVY1P422;
2458c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:    return BM_PXP_PS_CTRL_WB_SWAP |
2468c2ecf20Sopenharmony_ci					  BV_PXP_PS_CTRL_FORMAT__UYVY1P422;
2478c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VYUY:    return BV_PXP_PS_CTRL_FORMAT__VYUY1P422;
2488c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVYU:    return BM_PXP_PS_CTRL_WB_SWAP |
2498c2ecf20Sopenharmony_ci					  BV_PXP_PS_CTRL_FORMAT__VYUY1P422;
2508c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_GREY:    return BV_PXP_PS_CTRL_FORMAT__Y8;
2518c2ecf20Sopenharmony_ci	default:
2528c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_Y4:      return BV_PXP_PS_CTRL_FORMAT__Y4;
2538c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:    return BV_PXP_PS_CTRL_FORMAT__YUV2P422;
2548c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:    return BV_PXP_PS_CTRL_FORMAT__YUV2P420;
2558c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:    return BV_PXP_PS_CTRL_FORMAT__YVU2P420;
2568c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:    return BV_PXP_PS_CTRL_FORMAT__YVU2P422;
2578c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P: return BV_PXP_PS_CTRL_FORMAT__YUV422;
2588c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:  return BV_PXP_PS_CTRL_FORMAT__YUV420;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic u32 pxp_v4l2_pix_fmt_to_out_format(u32 v4l2_pix_fmt)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	switch (v4l2_pix_fmt) {
2658c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XBGR32:   return BV_PXP_OUT_CTRL_FORMAT__RGB888;
2668c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_ABGR32:   return BV_PXP_OUT_CTRL_FORMAT__ARGB8888;
2678c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGR24:    return BV_PXP_OUT_CTRL_FORMAT__RGB888P;
2688c2ecf20Sopenharmony_ci	/* Missing V4L2 pixel formats for ARGB1555 and ARGB4444 */
2698c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB555:   return BV_PXP_OUT_CTRL_FORMAT__RGB555;
2708c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB444:   return BV_PXP_OUT_CTRL_FORMAT__RGB444;
2718c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565:   return BV_PXP_OUT_CTRL_FORMAT__RGB565;
2728c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VUYA32:
2738c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VUYX32:   return BV_PXP_OUT_CTRL_FORMAT__YUV1P444;
2748c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:     return BV_PXP_OUT_CTRL_FORMAT__UYVY1P422;
2758c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VYUY:     return BV_PXP_OUT_CTRL_FORMAT__VYUY1P422;
2768c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_GREY:     return BV_PXP_OUT_CTRL_FORMAT__Y8;
2778c2ecf20Sopenharmony_ci	default:
2788c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_Y4:       return BV_PXP_OUT_CTRL_FORMAT__Y4;
2798c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:     return BV_PXP_OUT_CTRL_FORMAT__YUV2P422;
2808c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:     return BV_PXP_OUT_CTRL_FORMAT__YUV2P420;
2818c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:     return BV_PXP_OUT_CTRL_FORMAT__YVU2P422;
2828c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:     return BV_PXP_OUT_CTRL_FORMAT__YVU2P420;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic bool pxp_v4l2_pix_fmt_is_yuv(u32 v4l2_pix_fmt)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	switch (v4l2_pix_fmt) {
2898c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VUYA32:
2908c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VUYX32:
2918c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
2928c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
2938c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VYUY:
2948c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVYU:
2958c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
2968c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
2978c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
2988c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
2998c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
3008c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
3018c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_GREY:
3028c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_Y4:
3038c2ecf20Sopenharmony_ci		return true;
3048c2ecf20Sopenharmony_ci	default:
3058c2ecf20Sopenharmony_ci		return false;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic void pxp_setup_csc(struct pxp_ctx *ctx)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct pxp_dev *dev = ctx->dev;
3128c2ecf20Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_enc;
3138c2ecf20Sopenharmony_ci	enum v4l2_quantization quantization;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	if (pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) &&
3168c2ecf20Sopenharmony_ci	    !pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_DST].fmt->fourcc)) {
3178c2ecf20Sopenharmony_ci		/*
3188c2ecf20Sopenharmony_ci		 * CSC1 YUV/YCbCr to RGB conversion is implemented as follows:
3198c2ecf20Sopenharmony_ci		 *
3208c2ecf20Sopenharmony_ci		 * |R|   |C0 0  C1|   |Y  + Yoffset |
3218c2ecf20Sopenharmony_ci		 * |G| = |C0 C3 C2| * |Cb + UVoffset|
3228c2ecf20Sopenharmony_ci		 * |B|   |C0 C4 0 |   |Cr + UVoffset|
3238c2ecf20Sopenharmony_ci		 *
3248c2ecf20Sopenharmony_ci		 * Results are clamped to 0..255.
3258c2ecf20Sopenharmony_ci		 *
3268c2ecf20Sopenharmony_ci		 * BT.601 limited range:
3278c2ecf20Sopenharmony_ci		 *
3288c2ecf20Sopenharmony_ci		 * |R|   |1.1644  0.0000  1.5960|   |Y  - 16 |
3298c2ecf20Sopenharmony_ci		 * |G| = |1.1644 -0.3917 -0.8129| * |Cb - 128|
3308c2ecf20Sopenharmony_ci		 * |B|   |1.1644  2.0172  0.0000|   |Cr - 128|
3318c2ecf20Sopenharmony_ci		 */
3328c2ecf20Sopenharmony_ci		static const u32 csc1_coef_bt601_lim[3] = {
3338c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
3348c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x12a) |	/*  1.1641 (-0.03 %) */
3358c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
3368c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
3378c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x198) |	/*  1.5938 (-0.23 %) */
3388c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x204),	/*  2.0156 (-0.16 %) */
3398c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x730) |	/* -0.8125 (+0.04 %) */
3408c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x79c),	/* -0.3906 (+0.11 %) */
3418c2ecf20Sopenharmony_ci		};
3428c2ecf20Sopenharmony_ci		/*
3438c2ecf20Sopenharmony_ci		 * BT.601 full range:
3448c2ecf20Sopenharmony_ci		 *
3458c2ecf20Sopenharmony_ci		 * |R|   |1.0000  0.0000  1.4020|   |Y  + 0  |
3468c2ecf20Sopenharmony_ci		 * |G| = |1.0000 -0.3441 -0.7141| * |Cb - 128|
3478c2ecf20Sopenharmony_ci		 * |B|   |1.0000  1.7720  0.0000|   |Cr - 128|
3488c2ecf20Sopenharmony_ci		 */
3498c2ecf20Sopenharmony_ci		static const u32 csc1_coef_bt601_full[3] = {
3508c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
3518c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x100) |	/*  1.0000 (+0.00 %) */
3528c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
3538c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(0),
3548c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x166) |	/*  1.3984 (-0.36 %) */
3558c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x1c5),	/*  1.7695 (-0.25 %) */
3568c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x74a) |	/* -0.7109 (+0.32 %) */
3578c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7a8),	/* -0.3438 (+0.04 %) */
3588c2ecf20Sopenharmony_ci		};
3598c2ecf20Sopenharmony_ci		/*
3608c2ecf20Sopenharmony_ci		 * Rec.709 limited range:
3618c2ecf20Sopenharmony_ci		 *
3628c2ecf20Sopenharmony_ci		 * |R|   |1.1644  0.0000  1.7927|   |Y  - 16 |
3638c2ecf20Sopenharmony_ci		 * |G| = |1.1644 -0.2132 -0.5329| * |Cb - 128|
3648c2ecf20Sopenharmony_ci		 * |B|   |1.1644  2.1124  0.0000|   |Cr - 128|
3658c2ecf20Sopenharmony_ci		 */
3668c2ecf20Sopenharmony_ci		static const u32 csc1_coef_rec709_lim[3] = {
3678c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
3688c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x12a) |	/*  1.1641 (-0.03 %) */
3698c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
3708c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
3718c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x1ca) |	/*  1.7891 (-0.37 %) */
3728c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x21c),	/*  2.1094 (-0.30 %) */
3738c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x778) |	/* -0.5312 (+0.16 %) */
3748c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7ca),	/* -0.2109 (+0.23 %) */
3758c2ecf20Sopenharmony_ci		};
3768c2ecf20Sopenharmony_ci		/*
3778c2ecf20Sopenharmony_ci		 * Rec.709 full range:
3788c2ecf20Sopenharmony_ci		 *
3798c2ecf20Sopenharmony_ci		 * |R|   |1.0000  0.0000  1.5748|   |Y  + 0  |
3808c2ecf20Sopenharmony_ci		 * |G| = |1.0000 -0.1873 -0.4681| * |Cb - 128|
3818c2ecf20Sopenharmony_ci		 * |B|   |1.0000  1.8556  0.0000|   |Cr - 128|
3828c2ecf20Sopenharmony_ci		 */
3838c2ecf20Sopenharmony_ci		static const u32 csc1_coef_rec709_full[3] = {
3848c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
3858c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x100) |	/*  1.0000 (+0.00 %) */
3868c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
3878c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(0),
3888c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x193) |	/*  1.5742 (-0.06 %) */
3898c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x1db),	/*  1.8555 (-0.01 %) */
3908c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x789) |	/* -0.4648 (+0.33 %) */
3918c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7d1),	/* -0.1836 (+0.37 %) */
3928c2ecf20Sopenharmony_ci		};
3938c2ecf20Sopenharmony_ci		/*
3948c2ecf20Sopenharmony_ci		 * BT.2020 limited range:
3958c2ecf20Sopenharmony_ci		 *
3968c2ecf20Sopenharmony_ci		 * |R|   |1.1644  0.0000  1.6787|   |Y  - 16 |
3978c2ecf20Sopenharmony_ci		 * |G| = |1.1644 -0.1874 -0.6505| * |Cb - 128|
3988c2ecf20Sopenharmony_ci		 * |B|   |1.1644  2.1418  0.0000|   |Cr - 128|
3998c2ecf20Sopenharmony_ci		 */
4008c2ecf20Sopenharmony_ci		static const u32 csc1_coef_bt2020_lim[3] = {
4018c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
4028c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x12a) |	/*  1.1641 (-0.03 %) */
4038c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
4048c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
4058c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x1ad) |	/*  1.6758 (-0.29 %) */
4068c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x224),	/*  2.1406 (-0.11 %) */
4078c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x75a) |	/* -0.6484 (+0.20 %) */
4088c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7d1),	/* -0.1836 (+0.38 %) */
4098c2ecf20Sopenharmony_ci		};
4108c2ecf20Sopenharmony_ci		/*
4118c2ecf20Sopenharmony_ci		 * BT.2020 full range:
4128c2ecf20Sopenharmony_ci		 *
4138c2ecf20Sopenharmony_ci		 * |R|   |1.0000  0.0000  1.4746|   |Y  + 0  |
4148c2ecf20Sopenharmony_ci		 * |G| = |1.0000 -0.1646 -0.5714| * |Cb - 128|
4158c2ecf20Sopenharmony_ci		 * |B|   |1.0000  1.8814  0.0000|   |Cr - 128|
4168c2ecf20Sopenharmony_ci		 */
4178c2ecf20Sopenharmony_ci		static const u32 csc1_coef_bt2020_full[3] = {
4188c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
4198c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x100) |	/*  1.0000 (+0.00 %) */
4208c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
4218c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(0),
4228c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x179) |	/*  1.4727 (-0.19 %) */
4238c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x1e1),	/*  1.8789 (-0.25 %) */
4248c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x76e) |	/* -0.5703 (+0.11 %) */
4258c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7d6),	/* -0.1641 (+0.05 %) */
4268c2ecf20Sopenharmony_ci		};
4278c2ecf20Sopenharmony_ci		/*
4288c2ecf20Sopenharmony_ci		 * SMPTE 240m limited range:
4298c2ecf20Sopenharmony_ci		 *
4308c2ecf20Sopenharmony_ci		 * |R|   |1.1644  0.0000  1.7937|   |Y  - 16 |
4318c2ecf20Sopenharmony_ci		 * |G| = |1.1644 -0.2565 -0.5427| * |Cb - 128|
4328c2ecf20Sopenharmony_ci		 * |B|   |1.1644  2.0798  0.0000|   |Cr - 128|
4338c2ecf20Sopenharmony_ci		 */
4348c2ecf20Sopenharmony_ci		static const u32 csc1_coef_smpte240m_lim[3] = {
4358c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
4368c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x12a) |	/*  1.1641 (-0.03 %) */
4378c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
4388c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
4398c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x1cb) |	/*  1.7930 (-0.07 %) */
4408c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x214),	/*  2.0781 (-0.17 %) */
4418c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x776) |	/* -0.5391 (+0.36 %) */
4428c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7bf),	/* -0.2539 (+0.26 %) */
4438c2ecf20Sopenharmony_ci		};
4448c2ecf20Sopenharmony_ci		/*
4458c2ecf20Sopenharmony_ci		 * SMPTE 240m full range:
4468c2ecf20Sopenharmony_ci		 *
4478c2ecf20Sopenharmony_ci		 * |R|   |1.0000  0.0000  1.5756|   |Y  + 0  |
4488c2ecf20Sopenharmony_ci		 * |G| = |1.0000 -0.2253 -0.4767| * |Cb - 128|
4498c2ecf20Sopenharmony_ci		 * |B|   |1.0000  1.8270  0.0000|   |Cr - 128|
4508c2ecf20Sopenharmony_ci		 */
4518c2ecf20Sopenharmony_ci		static const u32 csc1_coef_smpte240m_full[3] = {
4528c2ecf20Sopenharmony_ci			BM_PXP_CSC1_COEF0_YCBCR_MODE |
4538c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_C0(0x100) |	/*  1.0000 (+0.00 %) */
4548c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
4558c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF0_Y_OFFSET(0),
4568c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C1(0x193) |	/*  1.5742 (-0.14 %) */
4578c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF1_C4(0x1d3),	/*  1.8242 (-0.28 %) */
4588c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C2(0x786) |	/* -0.4766 (+0.01 %) */
4598c2ecf20Sopenharmony_ci			BF_PXP_CSC1_COEF2_C3(0x7c7),	/* -0.2227 (+0.26 %) */
4608c2ecf20Sopenharmony_ci		};
4618c2ecf20Sopenharmony_ci		const u32 *csc1_coef;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		ycbcr_enc = ctx->q_data[V4L2_M2M_SRC].ycbcr_enc;
4648c2ecf20Sopenharmony_ci		quantization = ctx->q_data[V4L2_M2M_SRC].quant;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci		if (ycbcr_enc == V4L2_YCBCR_ENC_601) {
4678c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
4688c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_bt601_full;
4698c2ecf20Sopenharmony_ci			else
4708c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_bt601_lim;
4718c2ecf20Sopenharmony_ci		} else if (ycbcr_enc == V4L2_YCBCR_ENC_709) {
4728c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
4738c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_rec709_full;
4748c2ecf20Sopenharmony_ci			else
4758c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_rec709_lim;
4768c2ecf20Sopenharmony_ci		} else if (ycbcr_enc == V4L2_YCBCR_ENC_BT2020) {
4778c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
4788c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_bt2020_full;
4798c2ecf20Sopenharmony_ci			else
4808c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_bt2020_lim;
4818c2ecf20Sopenharmony_ci		} else {
4828c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
4838c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_smpte240m_full;
4848c2ecf20Sopenharmony_ci			else
4858c2ecf20Sopenharmony_ci				csc1_coef = csc1_coef_smpte240m_lim;
4868c2ecf20Sopenharmony_ci		}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		writel(csc1_coef[0], dev->mmio + HW_PXP_CSC1_COEF0);
4898c2ecf20Sopenharmony_ci		writel(csc1_coef[1], dev->mmio + HW_PXP_CSC1_COEF1);
4908c2ecf20Sopenharmony_ci		writel(csc1_coef[2], dev->mmio + HW_PXP_CSC1_COEF2);
4918c2ecf20Sopenharmony_ci	} else {
4928c2ecf20Sopenharmony_ci		writel(BM_PXP_CSC1_COEF0_BYPASS, dev->mmio + HW_PXP_CSC1_COEF0);
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (!pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) &&
4968c2ecf20Sopenharmony_ci	    pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_DST].fmt->fourcc)) {
4978c2ecf20Sopenharmony_ci		/*
4988c2ecf20Sopenharmony_ci		 * CSC2 RGB to YUV/YCbCr conversion is implemented as follows:
4998c2ecf20Sopenharmony_ci		 *
5008c2ecf20Sopenharmony_ci		 * |Y |   |A1 A2 A3|   |R|   |D1|
5018c2ecf20Sopenharmony_ci		 * |Cb| = |B1 B2 B3| * |G| + |D2|
5028c2ecf20Sopenharmony_ci		 * |Cr|   |C1 C2 C3|   |B|   |D3|
5038c2ecf20Sopenharmony_ci		 *
5048c2ecf20Sopenharmony_ci		 * Results are clamped to 0..255.
5058c2ecf20Sopenharmony_ci		 *
5068c2ecf20Sopenharmony_ci		 * BT.601 limited range:
5078c2ecf20Sopenharmony_ci		 *
5088c2ecf20Sopenharmony_ci		 * |Y |   | 0.2568  0.5041  0.0979|   |R|   |16 |
5098c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1482 -0.2910  0.4392| * |G| + |128|
5108c2ecf20Sopenharmony_ci		 * |Cr|   | 0.4392  0.4392 -0.3678|   |B|   |128|
5118c2ecf20Sopenharmony_ci		 */
5128c2ecf20Sopenharmony_ci		static const u32 csc2_coef_bt601_lim[6] = {
5138c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x081) |	/*  0.5039 (-0.02 %) */
5148c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x041),	/*  0.2539 (-0.29 %) */
5158c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7db) |	/* -0.1445 (+0.37 %) */
5168c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x019),	/*  0.0977 (-0.02 %) */
5178c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x070) |	/*  0.4375 (-0.17 %) */
5188c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7b6),	/* -0.2891 (+0.20 %) */
5198c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x7a2) |	/* -0.3672 (+0.06 %) */
5208c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x070),	/*  0.4375 (-0.17 %) */
5218c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(16) |
5228c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7ee),	/* -0.0703 (+0.11 %) */
5238c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
5248c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
5258c2ecf20Sopenharmony_ci		};
5268c2ecf20Sopenharmony_ci		/*
5278c2ecf20Sopenharmony_ci		 * BT.601 full range:
5288c2ecf20Sopenharmony_ci		 *
5298c2ecf20Sopenharmony_ci		 * |Y |   | 0.2990  0.5870  0.1140|   |R|   |0  |
5308c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1687 -0.3313  0.5000| * |G| + |128|
5318c2ecf20Sopenharmony_ci		 * |Cr|   | 0.5000  0.5000 -0.4187|   |B|   |128|
5328c2ecf20Sopenharmony_ci		 */
5338c2ecf20Sopenharmony_ci		static const u32 csc2_coef_bt601_full[6] = {
5348c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x096) |	/*  0.5859 (-0.11 %) */
5358c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x04c),	/*  0.2969 (-0.21 %) */
5368c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7d5) |	/* -0.1680 (+0.07 %) */
5378c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x01d),	/*  0.1133 (-0.07 %) */
5388c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x080) |	/*  0.5000 (+0.00 %) */
5398c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7ac),	/* -0.3281 (+0.32 %) */
5408c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x795) |	/* -0.4180 (+0.07 %) */
5418c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x080),	/*  0.5000 (+0.00 %) */
5428c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(0) |
5438c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7ec),	/* -0.0781 (+0.32 %) */
5448c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
5458c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
5468c2ecf20Sopenharmony_ci		};
5478c2ecf20Sopenharmony_ci		/*
5488c2ecf20Sopenharmony_ci		 * Rec.709 limited range:
5498c2ecf20Sopenharmony_ci		 *
5508c2ecf20Sopenharmony_ci		 * |Y |   | 0.1826  0.6142  0.0620|   |R|   |16 |
5518c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1007 -0.3385  0.4392| * |G| + |128|
5528c2ecf20Sopenharmony_ci		 * |Cr|   | 0.4392  0.4392 -0.3990|   |B|   |128|
5538c2ecf20Sopenharmony_ci		 */
5548c2ecf20Sopenharmony_ci		static const u32 csc2_coef_rec709_lim[6] = {
5558c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x09d) |	/*  0.6133 (-0.09 %) */
5568c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x02e),	/*  0.1797 (-0.29 %) */
5578c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7e7) |	/* -0.0977 (+0.30 %) */
5588c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x00f),	/*  0.0586 (-0.34 %) */
5598c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x070) |	/*  0.4375 (-0.17 %) */
5608c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7aa),	/* -0.3359 (+0.26 %) */
5618c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x79a) |	/* -0.3984 (+0.05 %) */
5628c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x070),	/*  0.4375 (-0.17 %) */
5638c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(16) |
5648c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f6),	/* -0.0391 (+0.12 %) */
5658c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
5668c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
5678c2ecf20Sopenharmony_ci		};
5688c2ecf20Sopenharmony_ci		/*
5698c2ecf20Sopenharmony_ci		 * Rec.709 full range:
5708c2ecf20Sopenharmony_ci		 *
5718c2ecf20Sopenharmony_ci		 * |Y |   | 0.2126  0.7152  0.0722|   |R|   |0  |
5728c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1146 -0.3854  0.5000| * |G| + |128|
5738c2ecf20Sopenharmony_ci		 * |Cr|   | 0.5000  0.5000 -0.4542|   |B|   |128|
5748c2ecf20Sopenharmony_ci		 */
5758c2ecf20Sopenharmony_ci		static const u32 csc2_coef_rec709_full[6] = {
5768c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x0b7) |	/*  0.7148 (-0.04 %) */
5778c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x036),	/*  0.2109 (-0.17 %) */
5788c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7e3) |	/* -0.1133 (+0.13 %) */
5798c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x012),	/*  0.0703 (-0.19 %) */
5808c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x080) |	/*  0.5000 (+0.00 %) */
5818c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x79e),	/* -0.3828 (+0.26 %) */
5828c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x78c) |	/* -0.4531 (+0.11 %) */
5838c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x080),	/*  0.5000 (+0.00 %) */
5848c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(0) |
5858c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f5),	/* -0.0430 (+0.28 %) */
5868c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
5878c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
5888c2ecf20Sopenharmony_ci		};
5898c2ecf20Sopenharmony_ci		/*
5908c2ecf20Sopenharmony_ci		 * BT.2020 limited range:
5918c2ecf20Sopenharmony_ci		 *
5928c2ecf20Sopenharmony_ci		 * |Y |   | 0.2256  0.5823  0.0509|   |R|   |16 |
5938c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1226 -0.3166  0.4392| * |G| + |128|
5948c2ecf20Sopenharmony_ci		 * |Cr|   | 0.4392  0.4392 -0.4039|   |B|   |128|
5958c2ecf20Sopenharmony_ci		 */
5968c2ecf20Sopenharmony_ci		static const u32 csc2_coef_bt2020_lim[6] = {
5978c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x095) |	/*  0.5820 (-0.03 %) */
5988c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x039),	/*  0.2227 (-0.30 %) */
5998c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7e1) |	/* -0.1211 (+0.15 %) */
6008c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x00d),	/*  0.0508 (-0.01 %) */
6018c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x070) |	/*  0.4375 (-0.17 %) */
6028c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7af),	/* -0.3164 (+0.02 %) */
6038c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x799) |	/* -0.4023 (+0.16 %) */
6048c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x070),	/*  0.4375 (-0.17 %) */
6058c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(16) |
6068c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f7),	/* -0.0352 (+0.02 %) */
6078c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
6088c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
6098c2ecf20Sopenharmony_ci		};
6108c2ecf20Sopenharmony_ci		/*
6118c2ecf20Sopenharmony_ci		 * BT.2020 full range:
6128c2ecf20Sopenharmony_ci		 *
6138c2ecf20Sopenharmony_ci		 * |Y |   | 0.2627  0.6780  0.0593|   |R|   |0  |
6148c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1396 -0.3604  0.5000| * |G| + |128|
6158c2ecf20Sopenharmony_ci		 * |Cr|   | 0.5000  0.5000 -0.4598|   |B|   |128|
6168c2ecf20Sopenharmony_ci		 */
6178c2ecf20Sopenharmony_ci		static const u32 csc2_coef_bt2020_full[6] = {
6188c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x0ad) |	/*  0.6758 (-0.22 %) */
6198c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x043),	/*  0.2617 (-0.10 %) */
6208c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7dd) |	/* -0.1367 (+0.29 %) */
6218c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x00f),	/*  0.0586 (-0.07 %) */
6228c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x080) |	/*  0.5000 (+0.00 %) */
6238c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7a4),	/* -0.3594 (+0.10 %) */
6248c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x78b) |	/* -0.4570 (+0.28 %) */
6258c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x080),	/*  0.5000 (+0.00 %) */
6268c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(0) |
6278c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f6),	/* -0.0391 (+0.11 %) */
6288c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
6298c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
6308c2ecf20Sopenharmony_ci		};
6318c2ecf20Sopenharmony_ci		/*
6328c2ecf20Sopenharmony_ci		 * SMPTE 240m limited range:
6338c2ecf20Sopenharmony_ci		 *
6348c2ecf20Sopenharmony_ci		 * |Y |   | 0.1821  0.6020  0.0747|   |R|   |16 |
6358c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1019 -0.3373  0.4392| * |G| + |128|
6368c2ecf20Sopenharmony_ci		 * |Cr|   | 0.4392  0.4392 -0.3909|   |B|   |128|
6378c2ecf20Sopenharmony_ci		 */
6388c2ecf20Sopenharmony_ci		static const u32 csc2_coef_smpte240m_lim[6] = {
6398c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x09a) |	/*  0.6016 (-0.05 %) */
6408c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x02e),	/*  0.1797 (-0.24 %) */
6418c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7e6) |	/* -0.1016 (+0.03 %) */
6428c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x013),	/*  0.0742 (-0.05 %) */
6438c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x070) |	/*  0.4375 (-0.17 %) */
6448c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x7aa),	/* -0.3359 (+0.14 %) */
6458c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x79c) |	/* -0.3906 (+0.03 %) */
6468c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x070),	/*  0.4375 (-0.17 %) */
6478c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(16) |
6488c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f4),	/* -0.0469 (+0.14 %) */
6498c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
6508c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
6518c2ecf20Sopenharmony_ci		};
6528c2ecf20Sopenharmony_ci		/*
6538c2ecf20Sopenharmony_ci		 * SMPTE 240m full range:
6548c2ecf20Sopenharmony_ci		 *
6558c2ecf20Sopenharmony_ci		 * |Y |   | 0.2120  0.7010  0.0870|   |R|   |0  |
6568c2ecf20Sopenharmony_ci		 * |Cb| = |-0.1160 -0.3840  0.5000| * |G| + |128|
6578c2ecf20Sopenharmony_ci		 * |Cr|   | 0.5000  0.5000 -0.4450|   |B|   |128|
6588c2ecf20Sopenharmony_ci		 */
6598c2ecf20Sopenharmony_ci		static const u32 csc2_coef_smpte240m_full[6] = {
6608c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A2(0x0b3) |	/*  0.6992 (-0.18 %) */
6618c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF0_A1(0x036),	/*  0.2109 (-0.11 %) */
6628c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_B1(0x7e3) |	/* -0.1133 (+0.27 %) */
6638c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF1_A3(0x016),	/*  0.0859 (-0.11 %) */
6648c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B3(0x080) |	/*  0.5000 (+0.00 %) */
6658c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF2_B2(0x79e),	/* -0.3828 (+0.12 %) */
6668c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C2(0x78f) |	/* -0.4414 (+0.36 %) */
6678c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF3_C1(0x080),	/*  0.5000 (+0.00 %) */
6688c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_D1(0) |
6698c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF4_C3(0x7f2),	/* -0.0547 (+0.03 %) */
6708c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D3(128) |
6718c2ecf20Sopenharmony_ci			BF_PXP_CSC2_COEF5_D2(128),
6728c2ecf20Sopenharmony_ci		};
6738c2ecf20Sopenharmony_ci		const u32 *csc2_coef;
6748c2ecf20Sopenharmony_ci		u32 csc2_ctrl;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		ycbcr_enc = ctx->q_data[V4L2_M2M_DST].ycbcr_enc;
6778c2ecf20Sopenharmony_ci		quantization = ctx->q_data[V4L2_M2M_DST].quant;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		if (ycbcr_enc == V4L2_YCBCR_ENC_601) {
6808c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
6818c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_bt601_full;
6828c2ecf20Sopenharmony_ci			else
6838c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_bt601_lim;
6848c2ecf20Sopenharmony_ci		} else if (ycbcr_enc == V4L2_YCBCR_ENC_709) {
6858c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
6868c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_rec709_full;
6878c2ecf20Sopenharmony_ci			else
6888c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_rec709_lim;
6898c2ecf20Sopenharmony_ci		} else if (ycbcr_enc == V4L2_YCBCR_ENC_BT2020) {
6908c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
6918c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_bt2020_full;
6928c2ecf20Sopenharmony_ci			else
6938c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_bt2020_lim;
6948c2ecf20Sopenharmony_ci		} else {
6958c2ecf20Sopenharmony_ci			if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
6968c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_smpte240m_full;
6978c2ecf20Sopenharmony_ci			else
6988c2ecf20Sopenharmony_ci				csc2_coef = csc2_coef_smpte240m_lim;
6998c2ecf20Sopenharmony_ci		}
7008c2ecf20Sopenharmony_ci		if (quantization == V4L2_QUANTIZATION_FULL_RANGE) {
7018c2ecf20Sopenharmony_ci			csc2_ctrl = BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YUV <<
7028c2ecf20Sopenharmony_ci				    BP_PXP_CSC2_CTRL_CSC_MODE;
7038c2ecf20Sopenharmony_ci		} else {
7048c2ecf20Sopenharmony_ci			csc2_ctrl = BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YCbCr <<
7058c2ecf20Sopenharmony_ci				    BP_PXP_CSC2_CTRL_CSC_MODE;
7068c2ecf20Sopenharmony_ci		}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		writel(csc2_ctrl, dev->mmio + HW_PXP_CSC2_CTRL);
7098c2ecf20Sopenharmony_ci		writel(csc2_coef[0], dev->mmio + HW_PXP_CSC2_COEF0);
7108c2ecf20Sopenharmony_ci		writel(csc2_coef[1], dev->mmio + HW_PXP_CSC2_COEF1);
7118c2ecf20Sopenharmony_ci		writel(csc2_coef[2], dev->mmio + HW_PXP_CSC2_COEF2);
7128c2ecf20Sopenharmony_ci		writel(csc2_coef[3], dev->mmio + HW_PXP_CSC2_COEF3);
7138c2ecf20Sopenharmony_ci		writel(csc2_coef[4], dev->mmio + HW_PXP_CSC2_COEF4);
7148c2ecf20Sopenharmony_ci		writel(csc2_coef[5], dev->mmio + HW_PXP_CSC2_COEF5);
7158c2ecf20Sopenharmony_ci	} else {
7168c2ecf20Sopenharmony_ci		writel(BM_PXP_CSC2_CTRL_BYPASS, dev->mmio + HW_PXP_CSC2_CTRL);
7178c2ecf20Sopenharmony_ci	}
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic int pxp_start(struct pxp_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
7218c2ecf20Sopenharmony_ci		     struct vb2_v4l2_buffer *out_vb)
7228c2ecf20Sopenharmony_ci{
7238c2ecf20Sopenharmony_ci	struct pxp_dev *dev = ctx->dev;
7248c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data;
7258c2ecf20Sopenharmony_ci	u32 src_width, src_height, src_stride, src_fourcc;
7268c2ecf20Sopenharmony_ci	u32 dst_width, dst_height, dst_stride, dst_fourcc;
7278c2ecf20Sopenharmony_ci	dma_addr_t p_in, p_out;
7288c2ecf20Sopenharmony_ci	u32 ctrl, out_ctrl, out_buf, out_buf2, out_pitch, out_lrc, out_ps_ulc;
7298c2ecf20Sopenharmony_ci	u32 out_ps_lrc;
7308c2ecf20Sopenharmony_ci	u32 ps_ctrl, ps_buf, ps_ubuf, ps_vbuf, ps_pitch, ps_scale, ps_offset;
7318c2ecf20Sopenharmony_ci	u32 as_ulc, as_lrc;
7328c2ecf20Sopenharmony_ci	u32 y_size;
7338c2ecf20Sopenharmony_ci	u32 decx, decy, xscale, yscale;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	src_width = ctx->q_data[V4L2_M2M_SRC].width;
7388c2ecf20Sopenharmony_ci	dst_width = ctx->q_data[V4L2_M2M_DST].width;
7398c2ecf20Sopenharmony_ci	src_height = ctx->q_data[V4L2_M2M_SRC].height;
7408c2ecf20Sopenharmony_ci	dst_height = ctx->q_data[V4L2_M2M_DST].height;
7418c2ecf20Sopenharmony_ci	src_stride = ctx->q_data[V4L2_M2M_SRC].bytesperline;
7428c2ecf20Sopenharmony_ci	dst_stride = ctx->q_data[V4L2_M2M_DST].bytesperline;
7438c2ecf20Sopenharmony_ci	src_fourcc = ctx->q_data[V4L2_M2M_SRC].fmt->fourcc;
7448c2ecf20Sopenharmony_ci	dst_fourcc = ctx->q_data[V4L2_M2M_DST].fmt->fourcc;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	p_in = vb2_dma_contig_plane_dma_addr(&in_vb->vb2_buf, 0);
7478c2ecf20Sopenharmony_ci	p_out = vb2_dma_contig_plane_dma_addr(&out_vb->vb2_buf, 0);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	if (!p_in || !p_out) {
7508c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev,
7518c2ecf20Sopenharmony_ci			 "Acquiring DMA addresses of buffers failed\n");
7528c2ecf20Sopenharmony_ci		return -EFAULT;
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	out_vb->sequence =
7568c2ecf20Sopenharmony_ci		get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
7578c2ecf20Sopenharmony_ci	in_vb->sequence = q_data->sequence++;
7588c2ecf20Sopenharmony_ci	out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
7618c2ecf20Sopenharmony_ci		out_vb->timecode = in_vb->timecode;
7628c2ecf20Sopenharmony_ci	out_vb->field = in_vb->field;
7638c2ecf20Sopenharmony_ci	out_vb->flags = in_vb->flags &
7648c2ecf20Sopenharmony_ci		(V4L2_BUF_FLAG_TIMECODE |
7658c2ecf20Sopenharmony_ci		 V4L2_BUF_FLAG_KEYFRAME |
7668c2ecf20Sopenharmony_ci		 V4L2_BUF_FLAG_PFRAME |
7678c2ecf20Sopenharmony_ci		 V4L2_BUF_FLAG_BFRAME |
7688c2ecf20Sopenharmony_ci		 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* Rotation disabled, 8x8 block size */
7718c2ecf20Sopenharmony_ci	ctrl = BF_PXP_CTRL_VFLIP0(!!(ctx->mode & MEM2MEM_VFLIP)) |
7728c2ecf20Sopenharmony_ci	       BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP));
7738c2ecf20Sopenharmony_ci	/* Always write alpha value as V4L2_CID_ALPHA_COMPONENT */
7748c2ecf20Sopenharmony_ci	out_ctrl = BF_PXP_OUT_CTRL_ALPHA(ctx->alpha_component) |
7758c2ecf20Sopenharmony_ci		   BF_PXP_OUT_CTRL_ALPHA_OUTPUT(1) |
7768c2ecf20Sopenharmony_ci		   pxp_v4l2_pix_fmt_to_out_format(dst_fourcc);
7778c2ecf20Sopenharmony_ci	out_buf = p_out;
7788c2ecf20Sopenharmony_ci	switch (dst_fourcc) {
7798c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
7808c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
7818c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
7828c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
7838c2ecf20Sopenharmony_ci		out_buf2 = out_buf + dst_stride * dst_height;
7848c2ecf20Sopenharmony_ci		break;
7858c2ecf20Sopenharmony_ci	default:
7868c2ecf20Sopenharmony_ci		out_buf2 = 0;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	out_pitch = BF_PXP_OUT_PITCH_PITCH(dst_stride);
7908c2ecf20Sopenharmony_ci	out_lrc = BF_PXP_OUT_LRC_X(dst_width - 1) |
7918c2ecf20Sopenharmony_ci		  BF_PXP_OUT_LRC_Y(dst_height - 1);
7928c2ecf20Sopenharmony_ci	/* PS covers whole output */
7938c2ecf20Sopenharmony_ci	out_ps_ulc = BF_PXP_OUT_PS_ULC_X(0) | BF_PXP_OUT_PS_ULC_Y(0);
7948c2ecf20Sopenharmony_ci	out_ps_lrc = BF_PXP_OUT_PS_LRC_X(dst_width - 1) |
7958c2ecf20Sopenharmony_ci		     BF_PXP_OUT_PS_LRC_Y(dst_height - 1);
7968c2ecf20Sopenharmony_ci	/* no AS */
7978c2ecf20Sopenharmony_ci	as_ulc = BF_PXP_OUT_AS_ULC_X(1) | BF_PXP_OUT_AS_ULC_Y(1);
7988c2ecf20Sopenharmony_ci	as_lrc = BF_PXP_OUT_AS_LRC_X(0) | BF_PXP_OUT_AS_LRC_Y(0);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	decx = (src_width <= dst_width) ? 0 : ilog2(src_width / dst_width);
8018c2ecf20Sopenharmony_ci	decy = (src_height <= dst_height) ? 0 : ilog2(src_height / dst_height);
8028c2ecf20Sopenharmony_ci	ps_ctrl = BF_PXP_PS_CTRL_DECX(decx) | BF_PXP_PS_CTRL_DECY(decy) |
8038c2ecf20Sopenharmony_ci		  pxp_v4l2_pix_fmt_to_ps_format(src_fourcc);
8048c2ecf20Sopenharmony_ci	ps_buf = p_in;
8058c2ecf20Sopenharmony_ci	y_size = src_stride * src_height;
8068c2ecf20Sopenharmony_ci	switch (src_fourcc) {
8078c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
8088c2ecf20Sopenharmony_ci		ps_ubuf = ps_buf + y_size;
8098c2ecf20Sopenharmony_ci		ps_vbuf = ps_ubuf + y_size / 4;
8108c2ecf20Sopenharmony_ci		break;
8118c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
8128c2ecf20Sopenharmony_ci		ps_ubuf = ps_buf + y_size;
8138c2ecf20Sopenharmony_ci		ps_vbuf = ps_ubuf + y_size / 2;
8148c2ecf20Sopenharmony_ci		break;
8158c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
8168c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
8178c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
8188c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
8198c2ecf20Sopenharmony_ci		ps_ubuf = ps_buf + y_size;
8208c2ecf20Sopenharmony_ci		ps_vbuf = 0;
8218c2ecf20Sopenharmony_ci		break;
8228c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_GREY:
8238c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_Y4:
8248c2ecf20Sopenharmony_ci		ps_ubuf = 0;
8258c2ecf20Sopenharmony_ci		/* In grayscale mode, ps_vbuf contents are reused as CbCr */
8268c2ecf20Sopenharmony_ci		ps_vbuf = 0x8080;
8278c2ecf20Sopenharmony_ci		break;
8288c2ecf20Sopenharmony_ci	default:
8298c2ecf20Sopenharmony_ci		ps_ubuf = 0;
8308c2ecf20Sopenharmony_ci		ps_vbuf = 0;
8318c2ecf20Sopenharmony_ci		break;
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci	ps_pitch = BF_PXP_PS_PITCH_PITCH(src_stride);
8348c2ecf20Sopenharmony_ci	if (decx) {
8358c2ecf20Sopenharmony_ci		xscale = (src_width >> decx) * 0x1000 / dst_width;
8368c2ecf20Sopenharmony_ci	} else {
8378c2ecf20Sopenharmony_ci		switch (src_fourcc) {
8388c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_UYVY:
8398c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_YUYV:
8408c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_VYUY:
8418c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_YVYU:
8428c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_NV16:
8438c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_NV12:
8448c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_NV21:
8458c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_NV61:
8468c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_YUV422P:
8478c2ecf20Sopenharmony_ci		case V4L2_PIX_FMT_YUV420:
8488c2ecf20Sopenharmony_ci			/*
8498c2ecf20Sopenharmony_ci			 * This avoids sampling past the right edge for
8508c2ecf20Sopenharmony_ci			 * horizontally chroma subsampled formats.
8518c2ecf20Sopenharmony_ci			 */
8528c2ecf20Sopenharmony_ci			xscale = (src_width - 2) * 0x1000 / (dst_width - 1);
8538c2ecf20Sopenharmony_ci			break;
8548c2ecf20Sopenharmony_ci		default:
8558c2ecf20Sopenharmony_ci			xscale = (src_width - 1) * 0x1000 / (dst_width - 1);
8568c2ecf20Sopenharmony_ci			break;
8578c2ecf20Sopenharmony_ci		}
8588c2ecf20Sopenharmony_ci	}
8598c2ecf20Sopenharmony_ci	if (decy)
8608c2ecf20Sopenharmony_ci		yscale = (src_height >> decy) * 0x1000 / dst_height;
8618c2ecf20Sopenharmony_ci	else
8628c2ecf20Sopenharmony_ci		yscale = (src_height - 1) * 0x1000 / (dst_height - 1);
8638c2ecf20Sopenharmony_ci	ps_scale = BF_PXP_PS_SCALE_YSCALE(yscale) |
8648c2ecf20Sopenharmony_ci		   BF_PXP_PS_SCALE_XSCALE(xscale);
8658c2ecf20Sopenharmony_ci	ps_offset = BF_PXP_PS_OFFSET_YOFFSET(0) | BF_PXP_PS_OFFSET_XOFFSET(0);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	writel(ctrl, dev->mmio + HW_PXP_CTRL);
8688c2ecf20Sopenharmony_ci	/* skip STAT */
8698c2ecf20Sopenharmony_ci	writel(out_ctrl, dev->mmio + HW_PXP_OUT_CTRL);
8708c2ecf20Sopenharmony_ci	writel(out_buf, dev->mmio + HW_PXP_OUT_BUF);
8718c2ecf20Sopenharmony_ci	writel(out_buf2, dev->mmio + HW_PXP_OUT_BUF2);
8728c2ecf20Sopenharmony_ci	writel(out_pitch, dev->mmio + HW_PXP_OUT_PITCH);
8738c2ecf20Sopenharmony_ci	writel(out_lrc, dev->mmio + HW_PXP_OUT_LRC);
8748c2ecf20Sopenharmony_ci	writel(out_ps_ulc, dev->mmio + HW_PXP_OUT_PS_ULC);
8758c2ecf20Sopenharmony_ci	writel(out_ps_lrc, dev->mmio + HW_PXP_OUT_PS_LRC);
8768c2ecf20Sopenharmony_ci	writel(as_ulc, dev->mmio + HW_PXP_OUT_AS_ULC);
8778c2ecf20Sopenharmony_ci	writel(as_lrc, dev->mmio + HW_PXP_OUT_AS_LRC);
8788c2ecf20Sopenharmony_ci	writel(ps_ctrl, dev->mmio + HW_PXP_PS_CTRL);
8798c2ecf20Sopenharmony_ci	writel(ps_buf, dev->mmio + HW_PXP_PS_BUF);
8808c2ecf20Sopenharmony_ci	writel(ps_ubuf, dev->mmio + HW_PXP_PS_UBUF);
8818c2ecf20Sopenharmony_ci	writel(ps_vbuf, dev->mmio + HW_PXP_PS_VBUF);
8828c2ecf20Sopenharmony_ci	writel(ps_pitch, dev->mmio + HW_PXP_PS_PITCH);
8838c2ecf20Sopenharmony_ci	writel(0x00ffffff, dev->mmio + HW_PXP_PS_BACKGROUND_0);
8848c2ecf20Sopenharmony_ci	writel(ps_scale, dev->mmio + HW_PXP_PS_SCALE);
8858c2ecf20Sopenharmony_ci	writel(ps_offset, dev->mmio + HW_PXP_PS_OFFSET);
8868c2ecf20Sopenharmony_ci	/* disable processed surface color keying */
8878c2ecf20Sopenharmony_ci	writel(0x00ffffff, dev->mmio + HW_PXP_PS_CLRKEYLOW_0);
8888c2ecf20Sopenharmony_ci	writel(0x00000000, dev->mmio + HW_PXP_PS_CLRKEYHIGH_0);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	/* disable alpha surface color keying */
8918c2ecf20Sopenharmony_ci	writel(0x00ffffff, dev->mmio + HW_PXP_AS_CLRKEYLOW_0);
8928c2ecf20Sopenharmony_ci	writel(0x00000000, dev->mmio + HW_PXP_AS_CLRKEYHIGH_0);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	/* setup CSC */
8958c2ecf20Sopenharmony_ci	pxp_setup_csc(ctx);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/* bypass LUT */
8988c2ecf20Sopenharmony_ci	writel(BM_PXP_LUT_CTRL_BYPASS, dev->mmio + HW_PXP_LUT_CTRL);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	writel(BF_PXP_DATA_PATH_CTRL0_MUX15_SEL(0)|
9018c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX14_SEL(1)|
9028c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX13_SEL(0)|
9038c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX12_SEL(0)|
9048c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX11_SEL(0)|
9058c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX10_SEL(0)|
9068c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX9_SEL(1)|
9078c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX8_SEL(0)|
9088c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX7_SEL(0)|
9098c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX6_SEL(0)|
9108c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX5_SEL(0)|
9118c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX4_SEL(0)|
9128c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX3_SEL(0)|
9138c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX2_SEL(0)|
9148c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX1_SEL(0)|
9158c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL0_MUX0_SEL(0),
9168c2ecf20Sopenharmony_ci	       dev->mmio + HW_PXP_DATA_PATH_CTRL0);
9178c2ecf20Sopenharmony_ci	writel(BF_PXP_DATA_PATH_CTRL1_MUX17_SEL(1) |
9188c2ecf20Sopenharmony_ci	       BF_PXP_DATA_PATH_CTRL1_MUX16_SEL(1),
9198c2ecf20Sopenharmony_ci	       dev->mmio + HW_PXP_DATA_PATH_CTRL1);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	writel(0xffff, dev->mmio + HW_PXP_IRQ_MASK);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	/* ungate, enable PS/AS/OUT and PXP operation */
9248c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_IRQ_ENABLE, dev->mmio + HW_PXP_CTRL_SET);
9258c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_ENABLE | BM_PXP_CTRL_ENABLE_CSC2 |
9268c2ecf20Sopenharmony_ci	       BM_PXP_CTRL_ENABLE_LUT | BM_PXP_CTRL_ENABLE_ROTATE0 |
9278c2ecf20Sopenharmony_ci	       BM_PXP_CTRL_ENABLE_PS_AS_OUT, dev->mmio + HW_PXP_CTRL_SET);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	return 0;
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic void pxp_job_finish(struct pxp_dev *dev)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	struct pxp_ctx *curr_ctx;
9358c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *src_vb, *dst_vb;
9368c2ecf20Sopenharmony_ci	unsigned long flags;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	curr_ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	if (curr_ctx == NULL) {
9418c2ecf20Sopenharmony_ci		pr_err("Instance released before the end of transaction\n");
9428c2ecf20Sopenharmony_ci		return;
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
9468c2ecf20Sopenharmony_ci	dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->irqlock, flags);
9498c2ecf20Sopenharmony_ci	v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
9508c2ecf20Sopenharmony_ci	v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
9518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->irqlock, flags);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	dprintk(curr_ctx->dev, "Finishing transaction\n");
9548c2ecf20Sopenharmony_ci	v4l2_m2m_job_finish(dev->m2m_dev, curr_ctx->fh.m2m_ctx);
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci/*
9588c2ecf20Sopenharmony_ci * mem2mem callbacks
9598c2ecf20Sopenharmony_ci */
9608c2ecf20Sopenharmony_cistatic void pxp_device_run(void *priv)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = priv;
9638c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *src_buf, *dst_buf;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
9668c2ecf20Sopenharmony_ci	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	pxp_start(ctx, src_buf, dst_buf);
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_cistatic int pxp_job_ready(void *priv)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = priv;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < 1 ||
9768c2ecf20Sopenharmony_ci	    v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < 1) {
9778c2ecf20Sopenharmony_ci		dprintk(ctx->dev, "Not enough buffers available\n");
9788c2ecf20Sopenharmony_ci		return 0;
9798c2ecf20Sopenharmony_ci	}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	return 1;
9828c2ecf20Sopenharmony_ci}
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_cistatic void pxp_job_abort(void *priv)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = priv;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	/* Will cancel the transaction in the next interrupt handler */
9898c2ecf20Sopenharmony_ci	ctx->aborting = 1;
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci/*
9938c2ecf20Sopenharmony_ci * interrupt handler
9948c2ecf20Sopenharmony_ci */
9958c2ecf20Sopenharmony_cistatic irqreturn_t pxp_irq_handler(int irq, void *dev_id)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	struct pxp_dev *dev = dev_id;
9988c2ecf20Sopenharmony_ci	u32 stat;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	stat = readl(dev->mmio + HW_PXP_STAT);
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	if (stat & BM_PXP_STAT_IRQ0) {
10038c2ecf20Sopenharmony_ci		/* we expect x = 0, y = height, irq0 = 1 */
10048c2ecf20Sopenharmony_ci		if (stat & ~(BM_PXP_STAT_BLOCKX | BM_PXP_STAT_BLOCKY |
10058c2ecf20Sopenharmony_ci			     BM_PXP_STAT_IRQ0))
10068c2ecf20Sopenharmony_ci			dprintk(dev, "%s: stat = 0x%08x\n", __func__, stat);
10078c2ecf20Sopenharmony_ci		writel(BM_PXP_STAT_IRQ0, dev->mmio + HW_PXP_STAT_CLR);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci		pxp_job_finish(dev);
10108c2ecf20Sopenharmony_ci	} else {
10118c2ecf20Sopenharmony_ci		u32 irq = readl(dev->mmio + HW_PXP_IRQ);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci		dprintk(dev, "%s: stat = 0x%08x\n", __func__, stat);
10148c2ecf20Sopenharmony_ci		dprintk(dev, "%s: irq = 0x%08x\n", __func__, irq);
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci		writel(irq, dev->mmio + HW_PXP_IRQ_CLR);
10178c2ecf20Sopenharmony_ci	}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci/*
10238c2ecf20Sopenharmony_ci * video ioctls
10248c2ecf20Sopenharmony_ci */
10258c2ecf20Sopenharmony_cistatic int pxp_querycap(struct file *file, void *priv,
10268c2ecf20Sopenharmony_ci			   struct v4l2_capability *cap)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
10298c2ecf20Sopenharmony_ci	strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
10308c2ecf20Sopenharmony_ci	snprintf(cap->bus_info, sizeof(cap->bus_info),
10318c2ecf20Sopenharmony_ci			"platform:%s", MEM2MEM_NAME);
10328c2ecf20Sopenharmony_ci	return 0;
10338c2ecf20Sopenharmony_ci}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_cistatic int pxp_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	int i, num;
10388c2ecf20Sopenharmony_ci	struct pxp_fmt *fmt;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	num = 0;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_FORMATS; ++i) {
10438c2ecf20Sopenharmony_ci		if (formats[i].types & type) {
10448c2ecf20Sopenharmony_ci			/* index-th format of type type found ? */
10458c2ecf20Sopenharmony_ci			if (num == f->index)
10468c2ecf20Sopenharmony_ci				break;
10478c2ecf20Sopenharmony_ci			/*
10488c2ecf20Sopenharmony_ci			 * Correct type but haven't reached our index yet,
10498c2ecf20Sopenharmony_ci			 * just increment per-type index
10508c2ecf20Sopenharmony_ci			 */
10518c2ecf20Sopenharmony_ci			++num;
10528c2ecf20Sopenharmony_ci		}
10538c2ecf20Sopenharmony_ci	}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	if (i < NUM_FORMATS) {
10568c2ecf20Sopenharmony_ci		/* Format found */
10578c2ecf20Sopenharmony_ci		fmt = &formats[i];
10588c2ecf20Sopenharmony_ci		f->pixelformat = fmt->fourcc;
10598c2ecf20Sopenharmony_ci		return 0;
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	/* Format not found */
10638c2ecf20Sopenharmony_ci	return -EINVAL;
10648c2ecf20Sopenharmony_ci}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_cistatic int pxp_enum_fmt_vid_cap(struct file *file, void *priv,
10678c2ecf20Sopenharmony_ci				struct v4l2_fmtdesc *f)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	return pxp_enum_fmt(f, MEM2MEM_CAPTURE);
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic int pxp_enum_fmt_vid_out(struct file *file, void *priv,
10738c2ecf20Sopenharmony_ci				struct v4l2_fmtdesc *f)
10748c2ecf20Sopenharmony_ci{
10758c2ecf20Sopenharmony_ci	return pxp_enum_fmt(f, MEM2MEM_OUTPUT);
10768c2ecf20Sopenharmony_ci}
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_cistatic int pxp_g_fmt(struct pxp_ctx *ctx, struct v4l2_format *f)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct vb2_queue *vq;
10818c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data;
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
10848c2ecf20Sopenharmony_ci	if (!vq)
10858c2ecf20Sopenharmony_ci		return -EINVAL;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	q_data = get_q_data(ctx, f->type);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	f->fmt.pix.width	= q_data->width;
10908c2ecf20Sopenharmony_ci	f->fmt.pix.height	= q_data->height;
10918c2ecf20Sopenharmony_ci	f->fmt.pix.field	= V4L2_FIELD_NONE;
10928c2ecf20Sopenharmony_ci	f->fmt.pix.pixelformat	= q_data->fmt->fourcc;
10938c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline	= q_data->bytesperline;
10948c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage	= q_data->sizeimage;
10958c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace	= ctx->colorspace;
10968c2ecf20Sopenharmony_ci	f->fmt.pix.xfer_func	= ctx->xfer_func;
10978c2ecf20Sopenharmony_ci	f->fmt.pix.ycbcr_enc	= q_data->ycbcr_enc;
10988c2ecf20Sopenharmony_ci	f->fmt.pix.quantization	= q_data->quant;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	return 0;
11018c2ecf20Sopenharmony_ci}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_cistatic int pxp_g_fmt_vid_out(struct file *file, void *priv,
11048c2ecf20Sopenharmony_ci				struct v4l2_format *f)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	return pxp_g_fmt(file2ctx(file), f);
11078c2ecf20Sopenharmony_ci}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_cistatic int pxp_g_fmt_vid_cap(struct file *file, void *priv,
11108c2ecf20Sopenharmony_ci				struct v4l2_format *f)
11118c2ecf20Sopenharmony_ci{
11128c2ecf20Sopenharmony_ci	return pxp_g_fmt(file2ctx(file), f);
11138c2ecf20Sopenharmony_ci}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_cistatic inline u32 pxp_bytesperline(struct pxp_fmt *fmt, u32 width)
11168c2ecf20Sopenharmony_ci{
11178c2ecf20Sopenharmony_ci	switch (fmt->fourcc) {
11188c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
11198c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
11208c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
11218c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
11228c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
11238c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
11248c2ecf20Sopenharmony_ci		return width;
11258c2ecf20Sopenharmony_ci	default:
11268c2ecf20Sopenharmony_ci		return (width * fmt->depth) >> 3;
11278c2ecf20Sopenharmony_ci	}
11288c2ecf20Sopenharmony_ci}
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_cistatic inline u32 pxp_sizeimage(struct pxp_fmt *fmt, u32 width, u32 height)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	return (fmt->depth * width * height) >> 3;
11338c2ecf20Sopenharmony_ci}
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_cistatic int pxp_try_fmt(struct v4l2_format *f, struct pxp_fmt *fmt)
11368c2ecf20Sopenharmony_ci{
11378c2ecf20Sopenharmony_ci	v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, ALIGN_W,
11388c2ecf20Sopenharmony_ci			      &f->fmt.pix.height, MIN_H, MAX_H, ALIGN_H, 0);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = pxp_bytesperline(fmt, f->fmt.pix.width);
11418c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = pxp_sizeimage(fmt, f->fmt.pix.width,
11428c2ecf20Sopenharmony_ci					     f->fmt.pix.height);
11438c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	return 0;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic void
11498c2ecf20Sopenharmony_cipxp_fixup_colorimetry_cap(struct pxp_ctx *ctx, u32 dst_fourcc,
11508c2ecf20Sopenharmony_ci			  enum v4l2_ycbcr_encoding *ycbcr_enc,
11518c2ecf20Sopenharmony_ci			  enum v4l2_quantization *quantization)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	bool dst_is_yuv = pxp_v4l2_pix_fmt_is_yuv(dst_fourcc);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	if (pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) ==
11568c2ecf20Sopenharmony_ci	    dst_is_yuv) {
11578c2ecf20Sopenharmony_ci		/*
11588c2ecf20Sopenharmony_ci		 * There is no support for conversion between different YCbCr
11598c2ecf20Sopenharmony_ci		 * encodings or between RGB limited and full range.
11608c2ecf20Sopenharmony_ci		 */
11618c2ecf20Sopenharmony_ci		*ycbcr_enc = ctx->q_data[V4L2_M2M_SRC].ycbcr_enc;
11628c2ecf20Sopenharmony_ci		*quantization = ctx->q_data[V4L2_M2M_SRC].quant;
11638c2ecf20Sopenharmony_ci	} else {
11648c2ecf20Sopenharmony_ci		*ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(ctx->colorspace);
11658c2ecf20Sopenharmony_ci		*quantization = V4L2_MAP_QUANTIZATION_DEFAULT(!dst_is_yuv,
11668c2ecf20Sopenharmony_ci							      ctx->colorspace,
11678c2ecf20Sopenharmony_ci							      *ycbcr_enc);
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci}
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_cistatic int pxp_try_fmt_vid_cap(struct file *file, void *priv,
11728c2ecf20Sopenharmony_ci			       struct v4l2_format *f)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	struct pxp_fmt *fmt;
11758c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = file2ctx(file);
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	fmt = find_format(f);
11788c2ecf20Sopenharmony_ci	if (!fmt) {
11798c2ecf20Sopenharmony_ci		f->fmt.pix.pixelformat = formats[0].fourcc;
11808c2ecf20Sopenharmony_ci		fmt = find_format(f);
11818c2ecf20Sopenharmony_ci	}
11828c2ecf20Sopenharmony_ci	if (!(fmt->types & MEM2MEM_CAPTURE)) {
11838c2ecf20Sopenharmony_ci		v4l2_err(&ctx->dev->v4l2_dev,
11848c2ecf20Sopenharmony_ci			 "Fourcc format (0x%08x) invalid.\n",
11858c2ecf20Sopenharmony_ci			 f->fmt.pix.pixelformat);
11868c2ecf20Sopenharmony_ci		return -EINVAL;
11878c2ecf20Sopenharmony_ci	}
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace = ctx->colorspace;
11908c2ecf20Sopenharmony_ci	f->fmt.pix.xfer_func = ctx->xfer_func;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	pxp_fixup_colorimetry_cap(ctx, fmt->fourcc,
11938c2ecf20Sopenharmony_ci				  &f->fmt.pix.ycbcr_enc,
11948c2ecf20Sopenharmony_ci				  &f->fmt.pix.quantization);
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	return pxp_try_fmt(f, fmt);
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_cistatic int pxp_try_fmt_vid_out(struct file *file, void *priv,
12008c2ecf20Sopenharmony_ci			       struct v4l2_format *f)
12018c2ecf20Sopenharmony_ci{
12028c2ecf20Sopenharmony_ci	struct pxp_fmt *fmt;
12038c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = file2ctx(file);
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	fmt = find_format(f);
12068c2ecf20Sopenharmony_ci	if (!fmt) {
12078c2ecf20Sopenharmony_ci		f->fmt.pix.pixelformat = formats[0].fourcc;
12088c2ecf20Sopenharmony_ci		fmt = find_format(f);
12098c2ecf20Sopenharmony_ci	}
12108c2ecf20Sopenharmony_ci	if (!(fmt->types & MEM2MEM_OUTPUT)) {
12118c2ecf20Sopenharmony_ci		v4l2_err(&ctx->dev->v4l2_dev,
12128c2ecf20Sopenharmony_ci			 "Fourcc format (0x%08x) invalid.\n",
12138c2ecf20Sopenharmony_ci			 f->fmt.pix.pixelformat);
12148c2ecf20Sopenharmony_ci		return -EINVAL;
12158c2ecf20Sopenharmony_ci	}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	if (!f->fmt.pix.colorspace)
12188c2ecf20Sopenharmony_ci		f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	return pxp_try_fmt(f, fmt);
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_cistatic int pxp_s_fmt(struct pxp_ctx *ctx, struct v4l2_format *f)
12248c2ecf20Sopenharmony_ci{
12258c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data;
12268c2ecf20Sopenharmony_ci	struct vb2_queue *vq;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
12298c2ecf20Sopenharmony_ci	if (!vq)
12308c2ecf20Sopenharmony_ci		return -EINVAL;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	q_data = get_q_data(ctx, f->type);
12338c2ecf20Sopenharmony_ci	if (!q_data)
12348c2ecf20Sopenharmony_ci		return -EINVAL;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	if (vb2_is_busy(vq)) {
12378c2ecf20Sopenharmony_ci		v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
12388c2ecf20Sopenharmony_ci		return -EBUSY;
12398c2ecf20Sopenharmony_ci	}
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	q_data->fmt		= find_format(f);
12428c2ecf20Sopenharmony_ci	q_data->width		= f->fmt.pix.width;
12438c2ecf20Sopenharmony_ci	q_data->height		= f->fmt.pix.height;
12448c2ecf20Sopenharmony_ci	q_data->bytesperline	= f->fmt.pix.bytesperline;
12458c2ecf20Sopenharmony_ci	q_data->sizeimage	= f->fmt.pix.sizeimage;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	dprintk(ctx->dev,
12488c2ecf20Sopenharmony_ci		"Setting format for type %d, wxh: %dx%d, fmt: %d\n",
12498c2ecf20Sopenharmony_ci		f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	return 0;
12528c2ecf20Sopenharmony_ci}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_cistatic int pxp_s_fmt_vid_cap(struct file *file, void *priv,
12558c2ecf20Sopenharmony_ci			     struct v4l2_format *f)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = file2ctx(file);
12588c2ecf20Sopenharmony_ci	int ret;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	ret = pxp_try_fmt_vid_cap(file, priv, f);
12618c2ecf20Sopenharmony_ci	if (ret)
12628c2ecf20Sopenharmony_ci		return ret;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	ret = pxp_s_fmt(file2ctx(file), f);
12658c2ecf20Sopenharmony_ci	if (ret)
12668c2ecf20Sopenharmony_ci		return ret;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_DST].ycbcr_enc = f->fmt.pix.ycbcr_enc;
12698c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_DST].quant = f->fmt.pix.quantization;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	return 0;
12728c2ecf20Sopenharmony_ci}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_cistatic int pxp_s_fmt_vid_out(struct file *file, void *priv,
12758c2ecf20Sopenharmony_ci			     struct v4l2_format *f)
12768c2ecf20Sopenharmony_ci{
12778c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = file2ctx(file);
12788c2ecf20Sopenharmony_ci	int ret;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	ret = pxp_try_fmt_vid_out(file, priv, f);
12818c2ecf20Sopenharmony_ci	if (ret)
12828c2ecf20Sopenharmony_ci		return ret;
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	ret = pxp_s_fmt(file2ctx(file), f);
12858c2ecf20Sopenharmony_ci	if (ret)
12868c2ecf20Sopenharmony_ci		return ret;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	ctx->colorspace = f->fmt.pix.colorspace;
12898c2ecf20Sopenharmony_ci	ctx->xfer_func = f->fmt.pix.xfer_func;
12908c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].ycbcr_enc = f->fmt.pix.ycbcr_enc;
12918c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].quant = f->fmt.pix.quantization;
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	pxp_fixup_colorimetry_cap(ctx, ctx->q_data[V4L2_M2M_DST].fmt->fourcc,
12948c2ecf20Sopenharmony_ci				  &ctx->q_data[V4L2_M2M_DST].ycbcr_enc,
12958c2ecf20Sopenharmony_ci				  &ctx->q_data[V4L2_M2M_DST].quant);
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	return 0;
12988c2ecf20Sopenharmony_ci}
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_cistatic int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
13018c2ecf20Sopenharmony_ci{
13028c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx =
13038c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct pxp_ctx, hdl);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	switch (ctrl->id) {
13068c2ecf20Sopenharmony_ci	case V4L2_CID_HFLIP:
13078c2ecf20Sopenharmony_ci		if (ctrl->val)
13088c2ecf20Sopenharmony_ci			ctx->mode |= MEM2MEM_HFLIP;
13098c2ecf20Sopenharmony_ci		else
13108c2ecf20Sopenharmony_ci			ctx->mode &= ~MEM2MEM_HFLIP;
13118c2ecf20Sopenharmony_ci		break;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	case V4L2_CID_VFLIP:
13148c2ecf20Sopenharmony_ci		if (ctrl->val)
13158c2ecf20Sopenharmony_ci			ctx->mode |= MEM2MEM_VFLIP;
13168c2ecf20Sopenharmony_ci		else
13178c2ecf20Sopenharmony_ci			ctx->mode &= ~MEM2MEM_VFLIP;
13188c2ecf20Sopenharmony_ci		break;
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	case V4L2_CID_ALPHA_COMPONENT:
13218c2ecf20Sopenharmony_ci		ctx->alpha_component = ctrl->val;
13228c2ecf20Sopenharmony_ci		break;
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	default:
13258c2ecf20Sopenharmony_ci		v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
13268c2ecf20Sopenharmony_ci		return -EINVAL;
13278c2ecf20Sopenharmony_ci	}
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	return 0;
13308c2ecf20Sopenharmony_ci}
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops pxp_ctrl_ops = {
13338c2ecf20Sopenharmony_ci	.s_ctrl = pxp_s_ctrl,
13348c2ecf20Sopenharmony_ci};
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops pxp_ioctl_ops = {
13378c2ecf20Sopenharmony_ci	.vidioc_querycap	= pxp_querycap,
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_cap = pxp_enum_fmt_vid_cap,
13408c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_cap	= pxp_g_fmt_vid_cap,
13418c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_cap	= pxp_try_fmt_vid_cap,
13428c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_cap	= pxp_s_fmt_vid_cap,
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_out = pxp_enum_fmt_vid_out,
13458c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_out	= pxp_g_fmt_vid_out,
13468c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_out	= pxp_try_fmt_vid_out,
13478c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_out	= pxp_s_fmt_vid_out,
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
13508c2ecf20Sopenharmony_ci	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
13518c2ecf20Sopenharmony_ci	.vidioc_qbuf		= v4l2_m2m_ioctl_qbuf,
13528c2ecf20Sopenharmony_ci	.vidioc_dqbuf		= v4l2_m2m_ioctl_dqbuf,
13538c2ecf20Sopenharmony_ci	.vidioc_prepare_buf	= v4l2_m2m_ioctl_prepare_buf,
13548c2ecf20Sopenharmony_ci	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
13558c2ecf20Sopenharmony_ci	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
13588c2ecf20Sopenharmony_ci	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
13618c2ecf20Sopenharmony_ci	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
13628c2ecf20Sopenharmony_ci};
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci/*
13658c2ecf20Sopenharmony_ci * Queue operations
13668c2ecf20Sopenharmony_ci */
13678c2ecf20Sopenharmony_cistatic int pxp_queue_setup(struct vb2_queue *vq,
13688c2ecf20Sopenharmony_ci			   unsigned int *nbuffers, unsigned int *nplanes,
13698c2ecf20Sopenharmony_ci			   unsigned int sizes[], struct device *alloc_devs[])
13708c2ecf20Sopenharmony_ci{
13718c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = vb2_get_drv_priv(vq);
13728c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data;
13738c2ecf20Sopenharmony_ci	unsigned int size, count = *nbuffers;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	q_data = get_q_data(ctx, vq->type);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	size = q_data->sizeimage;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	*nbuffers = count;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	if (*nplanes)
13828c2ecf20Sopenharmony_ci		return sizes[0] < size ? -EINVAL : 0;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	*nplanes = 1;
13858c2ecf20Sopenharmony_ci	sizes[0] = size;
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	return 0;
13908c2ecf20Sopenharmony_ci}
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_cistatic int pxp_buf_prepare(struct vb2_buffer *vb)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
13958c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
13968c2ecf20Sopenharmony_ci	struct pxp_dev *dev = ctx->dev;
13978c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data;
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci	dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	q_data = get_q_data(ctx, vb->vb2_queue->type);
14028c2ecf20Sopenharmony_ci	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
14038c2ecf20Sopenharmony_ci		if (vbuf->field == V4L2_FIELD_ANY)
14048c2ecf20Sopenharmony_ci			vbuf->field = V4L2_FIELD_NONE;
14058c2ecf20Sopenharmony_ci		if (vbuf->field != V4L2_FIELD_NONE) {
14068c2ecf20Sopenharmony_ci			dprintk(dev, "%s field isn't supported\n", __func__);
14078c2ecf20Sopenharmony_ci			return -EINVAL;
14088c2ecf20Sopenharmony_ci		}
14098c2ecf20Sopenharmony_ci	}
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
14128c2ecf20Sopenharmony_ci		dprintk(dev, "%s data will not fit into plane (%lu < %lu)\n",
14138c2ecf20Sopenharmony_ci			__func__, vb2_plane_size(vb, 0),
14148c2ecf20Sopenharmony_ci			(long)q_data->sizeimage);
14158c2ecf20Sopenharmony_ci		return -EINVAL;
14168c2ecf20Sopenharmony_ci	}
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	vb2_set_plane_payload(vb, 0, q_data->sizeimage);
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	return 0;
14218c2ecf20Sopenharmony_ci}
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_cistatic void pxp_buf_queue(struct vb2_buffer *vb)
14248c2ecf20Sopenharmony_ci{
14258c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
14268c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
14298c2ecf20Sopenharmony_ci}
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_cistatic int pxp_start_streaming(struct vb2_queue *q, unsigned int count)
14328c2ecf20Sopenharmony_ci{
14338c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = vb2_get_drv_priv(q);
14348c2ecf20Sopenharmony_ci	struct pxp_q_data *q_data = get_q_data(ctx, q->type);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	q_data->sequence = 0;
14378c2ecf20Sopenharmony_ci	return 0;
14388c2ecf20Sopenharmony_ci}
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_cistatic void pxp_stop_streaming(struct vb2_queue *q)
14418c2ecf20Sopenharmony_ci{
14428c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = vb2_get_drv_priv(q);
14438c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf;
14448c2ecf20Sopenharmony_ci	unsigned long flags;
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	for (;;) {
14478c2ecf20Sopenharmony_ci		if (V4L2_TYPE_IS_OUTPUT(q->type))
14488c2ecf20Sopenharmony_ci			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
14498c2ecf20Sopenharmony_ci		else
14508c2ecf20Sopenharmony_ci			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
14518c2ecf20Sopenharmony_ci		if (vbuf == NULL)
14528c2ecf20Sopenharmony_ci			return;
14538c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ctx->dev->irqlock, flags);
14548c2ecf20Sopenharmony_ci		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
14558c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
14568c2ecf20Sopenharmony_ci	}
14578c2ecf20Sopenharmony_ci}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_cistatic const struct vb2_ops pxp_qops = {
14608c2ecf20Sopenharmony_ci	.queue_setup	 = pxp_queue_setup,
14618c2ecf20Sopenharmony_ci	.buf_prepare	 = pxp_buf_prepare,
14628c2ecf20Sopenharmony_ci	.buf_queue	 = pxp_buf_queue,
14638c2ecf20Sopenharmony_ci	.start_streaming = pxp_start_streaming,
14648c2ecf20Sopenharmony_ci	.stop_streaming  = pxp_stop_streaming,
14658c2ecf20Sopenharmony_ci	.wait_prepare	 = vb2_ops_wait_prepare,
14668c2ecf20Sopenharmony_ci	.wait_finish	 = vb2_ops_wait_finish,
14678c2ecf20Sopenharmony_ci};
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_cistatic int queue_init(void *priv, struct vb2_queue *src_vq,
14708c2ecf20Sopenharmony_ci		      struct vb2_queue *dst_vq)
14718c2ecf20Sopenharmony_ci{
14728c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = priv;
14738c2ecf20Sopenharmony_ci	int ret;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
14768c2ecf20Sopenharmony_ci	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
14778c2ecf20Sopenharmony_ci	src_vq->drv_priv = ctx;
14788c2ecf20Sopenharmony_ci	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
14798c2ecf20Sopenharmony_ci	src_vq->ops = &pxp_qops;
14808c2ecf20Sopenharmony_ci	src_vq->mem_ops = &vb2_dma_contig_memops;
14818c2ecf20Sopenharmony_ci	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
14828c2ecf20Sopenharmony_ci	src_vq->lock = &ctx->dev->dev_mutex;
14838c2ecf20Sopenharmony_ci	src_vq->dev = ctx->dev->v4l2_dev.dev;
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	ret = vb2_queue_init(src_vq);
14868c2ecf20Sopenharmony_ci	if (ret)
14878c2ecf20Sopenharmony_ci		return ret;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
14908c2ecf20Sopenharmony_ci	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
14918c2ecf20Sopenharmony_ci	dst_vq->drv_priv = ctx;
14928c2ecf20Sopenharmony_ci	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
14938c2ecf20Sopenharmony_ci	dst_vq->ops = &pxp_qops;
14948c2ecf20Sopenharmony_ci	dst_vq->mem_ops = &vb2_dma_contig_memops;
14958c2ecf20Sopenharmony_ci	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
14968c2ecf20Sopenharmony_ci	dst_vq->lock = &ctx->dev->dev_mutex;
14978c2ecf20Sopenharmony_ci	dst_vq->dev = ctx->dev->v4l2_dev.dev;
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	return vb2_queue_init(dst_vq);
15008c2ecf20Sopenharmony_ci}
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci/*
15038c2ecf20Sopenharmony_ci * File operations
15048c2ecf20Sopenharmony_ci */
15058c2ecf20Sopenharmony_cistatic int pxp_open(struct file *file)
15068c2ecf20Sopenharmony_ci{
15078c2ecf20Sopenharmony_ci	struct pxp_dev *dev = video_drvdata(file);
15088c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = NULL;
15098c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl;
15108c2ecf20Sopenharmony_ci	int rc = 0;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&dev->dev_mutex))
15138c2ecf20Sopenharmony_ci		return -ERESTARTSYS;
15148c2ecf20Sopenharmony_ci	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
15158c2ecf20Sopenharmony_ci	if (!ctx) {
15168c2ecf20Sopenharmony_ci		rc = -ENOMEM;
15178c2ecf20Sopenharmony_ci		goto open_unlock;
15188c2ecf20Sopenharmony_ci	}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	v4l2_fh_init(&ctx->fh, video_devdata(file));
15218c2ecf20Sopenharmony_ci	file->private_data = &ctx->fh;
15228c2ecf20Sopenharmony_ci	ctx->dev = dev;
15238c2ecf20Sopenharmony_ci	hdl = &ctx->hdl;
15248c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 4);
15258c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
15268c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
15278c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
15288c2ecf20Sopenharmony_ci			  0, 255, 1, 255);
15298c2ecf20Sopenharmony_ci	if (hdl->error) {
15308c2ecf20Sopenharmony_ci		rc = hdl->error;
15318c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
15328c2ecf20Sopenharmony_ci		kfree(ctx);
15338c2ecf20Sopenharmony_ci		goto open_unlock;
15348c2ecf20Sopenharmony_ci	}
15358c2ecf20Sopenharmony_ci	ctx->fh.ctrl_handler = hdl;
15368c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(hdl);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
15398c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].width = 640;
15408c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].height = 480;
15418c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].bytesperline =
15428c2ecf20Sopenharmony_ci		pxp_bytesperline(&formats[0], 640);
15438c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_SRC].sizeimage =
15448c2ecf20Sopenharmony_ci		pxp_sizeimage(&formats[0], 640, 480);
15458c2ecf20Sopenharmony_ci	ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
15468c2ecf20Sopenharmony_ci	ctx->colorspace = V4L2_COLORSPACE_REC709;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->fh.m2m_ctx)) {
15518c2ecf20Sopenharmony_ci		rc = PTR_ERR(ctx->fh.m2m_ctx);
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
15548c2ecf20Sopenharmony_ci		v4l2_fh_exit(&ctx->fh);
15558c2ecf20Sopenharmony_ci		kfree(ctx);
15568c2ecf20Sopenharmony_ci		goto open_unlock;
15578c2ecf20Sopenharmony_ci	}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	v4l2_fh_add(&ctx->fh);
15608c2ecf20Sopenharmony_ci	atomic_inc(&dev->num_inst);
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
15638c2ecf20Sopenharmony_ci		ctx, ctx->fh.m2m_ctx);
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ciopen_unlock:
15668c2ecf20Sopenharmony_ci	mutex_unlock(&dev->dev_mutex);
15678c2ecf20Sopenharmony_ci	return rc;
15688c2ecf20Sopenharmony_ci}
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_cistatic int pxp_release(struct file *file)
15718c2ecf20Sopenharmony_ci{
15728c2ecf20Sopenharmony_ci	struct pxp_dev *dev = video_drvdata(file);
15738c2ecf20Sopenharmony_ci	struct pxp_ctx *ctx = file2ctx(file);
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	dprintk(dev, "Releasing instance %p\n", ctx);
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	v4l2_fh_del(&ctx->fh);
15788c2ecf20Sopenharmony_ci	v4l2_fh_exit(&ctx->fh);
15798c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&ctx->hdl);
15808c2ecf20Sopenharmony_ci	mutex_lock(&dev->dev_mutex);
15818c2ecf20Sopenharmony_ci	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
15828c2ecf20Sopenharmony_ci	mutex_unlock(&dev->dev_mutex);
15838c2ecf20Sopenharmony_ci	kfree(ctx);
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	atomic_dec(&dev->num_inst);
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	return 0;
15888c2ecf20Sopenharmony_ci}
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations pxp_fops = {
15918c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
15928c2ecf20Sopenharmony_ci	.open		= pxp_open,
15938c2ecf20Sopenharmony_ci	.release	= pxp_release,
15948c2ecf20Sopenharmony_ci	.poll		= v4l2_m2m_fop_poll,
15958c2ecf20Sopenharmony_ci	.unlocked_ioctl	= video_ioctl2,
15968c2ecf20Sopenharmony_ci	.mmap		= v4l2_m2m_fop_mmap,
15978c2ecf20Sopenharmony_ci};
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_cistatic const struct video_device pxp_videodev = {
16008c2ecf20Sopenharmony_ci	.name		= MEM2MEM_NAME,
16018c2ecf20Sopenharmony_ci	.vfl_dir	= VFL_DIR_M2M,
16028c2ecf20Sopenharmony_ci	.fops		= &pxp_fops,
16038c2ecf20Sopenharmony_ci	.device_caps	= V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
16048c2ecf20Sopenharmony_ci	.ioctl_ops	= &pxp_ioctl_ops,
16058c2ecf20Sopenharmony_ci	.minor		= -1,
16068c2ecf20Sopenharmony_ci	.release	= video_device_release_empty,
16078c2ecf20Sopenharmony_ci};
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_cistatic const struct v4l2_m2m_ops m2m_ops = {
16108c2ecf20Sopenharmony_ci	.device_run	= pxp_device_run,
16118c2ecf20Sopenharmony_ci	.job_ready	= pxp_job_ready,
16128c2ecf20Sopenharmony_ci	.job_abort	= pxp_job_abort,
16138c2ecf20Sopenharmony_ci};
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_cistatic int pxp_soft_reset(struct pxp_dev *dev)
16168c2ecf20Sopenharmony_ci{
16178c2ecf20Sopenharmony_ci	int ret;
16188c2ecf20Sopenharmony_ci	u32 val;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_CLR);
16218c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_CLR);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_SET);
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	ret = readl_poll_timeout(dev->mmio + HW_PXP_CTRL, val,
16268c2ecf20Sopenharmony_ci				 val & BM_PXP_CTRL_CLKGATE, 0, 100);
16278c2ecf20Sopenharmony_ci	if (ret < 0)
16288c2ecf20Sopenharmony_ci		return ret;
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_CLR);
16318c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_CLR);
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	return 0;
16348c2ecf20Sopenharmony_ci}
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_cistatic int pxp_probe(struct platform_device *pdev)
16378c2ecf20Sopenharmony_ci{
16388c2ecf20Sopenharmony_ci	struct pxp_dev *dev;
16398c2ecf20Sopenharmony_ci	struct resource *res;
16408c2ecf20Sopenharmony_ci	struct video_device *vfd;
16418c2ecf20Sopenharmony_ci	int irq;
16428c2ecf20Sopenharmony_ci	int ret;
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
16458c2ecf20Sopenharmony_ci	if (!dev)
16468c2ecf20Sopenharmony_ci		return -ENOMEM;
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	dev->clk = devm_clk_get(&pdev->dev, "axi");
16498c2ecf20Sopenharmony_ci	if (IS_ERR(dev->clk)) {
16508c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->clk);
16518c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get clk: %d\n", ret);
16528c2ecf20Sopenharmony_ci		return ret;
16538c2ecf20Sopenharmony_ci	}
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16568c2ecf20Sopenharmony_ci	dev->mmio = devm_ioremap_resource(&pdev->dev, res);
16578c2ecf20Sopenharmony_ci	if (IS_ERR(dev->mmio)) {
16588c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->mmio);
16598c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to map register space: %d\n", ret);
16608c2ecf20Sopenharmony_ci		return ret;
16618c2ecf20Sopenharmony_ci	}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
16648c2ecf20Sopenharmony_ci	if (irq < 0)
16658c2ecf20Sopenharmony_ci		return irq;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	spin_lock_init(&dev->irqlock);
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, pxp_irq_handler,
16708c2ecf20Sopenharmony_ci			IRQF_ONESHOT, dev_name(&pdev->dev), dev);
16718c2ecf20Sopenharmony_ci	if (ret < 0) {
16728c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
16738c2ecf20Sopenharmony_ci		return ret;
16748c2ecf20Sopenharmony_ci	}
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(dev->clk);
16778c2ecf20Sopenharmony_ci	if (ret < 0)
16788c2ecf20Sopenharmony_ci		return ret;
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	ret = pxp_soft_reset(dev);
16818c2ecf20Sopenharmony_ci	if (ret < 0) {
16828c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "PXP reset timeout: %d\n", ret);
16838c2ecf20Sopenharmony_ci		goto err_clk;
16848c2ecf20Sopenharmony_ci	}
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
16878c2ecf20Sopenharmony_ci	if (ret)
16888c2ecf20Sopenharmony_ci		goto err_clk;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	atomic_set(&dev->num_inst, 0);
16918c2ecf20Sopenharmony_ci	mutex_init(&dev->dev_mutex);
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	dev->vfd = pxp_videodev;
16948c2ecf20Sopenharmony_ci	vfd = &dev->vfd;
16958c2ecf20Sopenharmony_ci	vfd->lock = &dev->dev_mutex;
16968c2ecf20Sopenharmony_ci	vfd->v4l2_dev = &dev->v4l2_dev;
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	video_set_drvdata(vfd, dev);
16998c2ecf20Sopenharmony_ci	snprintf(vfd->name, sizeof(vfd->name), "%s", pxp_videodev.name);
17008c2ecf20Sopenharmony_ci	v4l2_info(&dev->v4l2_dev,
17018c2ecf20Sopenharmony_ci			"Device registered as /dev/video%d\n", vfd->num);
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, dev);
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
17068c2ecf20Sopenharmony_ci	if (IS_ERR(dev->m2m_dev)) {
17078c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
17088c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->m2m_dev);
17098c2ecf20Sopenharmony_ci		goto err_v4l2;
17108c2ecf20Sopenharmony_ci	}
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
17138c2ecf20Sopenharmony_ci	if (ret) {
17148c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
17158c2ecf20Sopenharmony_ci		goto err_m2m;
17168c2ecf20Sopenharmony_ci	}
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci	return 0;
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_cierr_m2m:
17218c2ecf20Sopenharmony_ci	v4l2_m2m_release(dev->m2m_dev);
17228c2ecf20Sopenharmony_cierr_v4l2:
17238c2ecf20Sopenharmony_ci	v4l2_device_unregister(&dev->v4l2_dev);
17248c2ecf20Sopenharmony_cierr_clk:
17258c2ecf20Sopenharmony_ci	clk_disable_unprepare(dev->clk);
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	return ret;
17288c2ecf20Sopenharmony_ci}
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_cistatic int pxp_remove(struct platform_device *pdev)
17318c2ecf20Sopenharmony_ci{
17328c2ecf20Sopenharmony_ci	struct pxp_dev *dev = platform_get_drvdata(pdev);
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_SET);
17358c2ecf20Sopenharmony_ci	writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_SET);
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci	clk_disable_unprepare(dev->clk);
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
17408c2ecf20Sopenharmony_ci	video_unregister_device(&dev->vfd);
17418c2ecf20Sopenharmony_ci	v4l2_m2m_release(dev->m2m_dev);
17428c2ecf20Sopenharmony_ci	v4l2_device_unregister(&dev->v4l2_dev);
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	return 0;
17458c2ecf20Sopenharmony_ci}
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_cistatic const struct of_device_id pxp_dt_ids[] = {
17488c2ecf20Sopenharmony_ci	{ .compatible = "fsl,imx6ull-pxp", .data = NULL },
17498c2ecf20Sopenharmony_ci	{ },
17508c2ecf20Sopenharmony_ci};
17518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, pxp_dt_ids);
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_cistatic struct platform_driver pxp_driver = {
17548c2ecf20Sopenharmony_ci	.probe		= pxp_probe,
17558c2ecf20Sopenharmony_ci	.remove		= pxp_remove,
17568c2ecf20Sopenharmony_ci	.driver		= {
17578c2ecf20Sopenharmony_ci		.name	= MEM2MEM_NAME,
17588c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(pxp_dt_ids),
17598c2ecf20Sopenharmony_ci	},
17608c2ecf20Sopenharmony_ci};
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_cimodule_platform_driver(pxp_driver);
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("i.MX PXP mem2mem scaler/CSC/rotator");
17658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Philipp Zabel <kernel@pengutronix.de>");
17668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1767