18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 Mentor Graphics Inc.
48c2ecf20Sopenharmony_ci * Copyright 2005-2012 Freescale Semiconductor, Inc. All Rights Reserved.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include <linux/bitrev.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/sizes.h>
108c2ecf20Sopenharmony_ci#include <drm/drm_fourcc.h>
118c2ecf20Sopenharmony_ci#include "ipu-prv.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct ipu_cpmem_word {
148c2ecf20Sopenharmony_ci	u32 data[5];
158c2ecf20Sopenharmony_ci	u32 res[3];
168c2ecf20Sopenharmony_ci};
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct ipu_ch_param {
198c2ecf20Sopenharmony_ci	struct ipu_cpmem_word word[2];
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct ipu_cpmem {
238c2ecf20Sopenharmony_ci	struct ipu_ch_param __iomem *base;
248c2ecf20Sopenharmony_ci	u32 module;
258c2ecf20Sopenharmony_ci	spinlock_t lock;
268c2ecf20Sopenharmony_ci	int use_count;
278c2ecf20Sopenharmony_ci	struct ipu_soc *ipu;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define IPU_CPMEM_WORD(word, ofs, size) ((((word) * 160 + (ofs)) << 8) | (size))
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define IPU_FIELD_UBO		IPU_CPMEM_WORD(0, 46, 22)
338c2ecf20Sopenharmony_ci#define IPU_FIELD_VBO		IPU_CPMEM_WORD(0, 68, 22)
348c2ecf20Sopenharmony_ci#define IPU_FIELD_IOX		IPU_CPMEM_WORD(0, 90, 4)
358c2ecf20Sopenharmony_ci#define IPU_FIELD_RDRW		IPU_CPMEM_WORD(0, 94, 1)
368c2ecf20Sopenharmony_ci#define IPU_FIELD_SO		IPU_CPMEM_WORD(0, 113, 1)
378c2ecf20Sopenharmony_ci#define IPU_FIELD_SLY		IPU_CPMEM_WORD(1, 102, 14)
388c2ecf20Sopenharmony_ci#define IPU_FIELD_SLUV		IPU_CPMEM_WORD(1, 128, 14)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define IPU_FIELD_XV		IPU_CPMEM_WORD(0, 0, 10)
418c2ecf20Sopenharmony_ci#define IPU_FIELD_YV		IPU_CPMEM_WORD(0, 10, 9)
428c2ecf20Sopenharmony_ci#define IPU_FIELD_XB		IPU_CPMEM_WORD(0, 19, 13)
438c2ecf20Sopenharmony_ci#define IPU_FIELD_YB		IPU_CPMEM_WORD(0, 32, 12)
448c2ecf20Sopenharmony_ci#define IPU_FIELD_NSB_B		IPU_CPMEM_WORD(0, 44, 1)
458c2ecf20Sopenharmony_ci#define IPU_FIELD_CF		IPU_CPMEM_WORD(0, 45, 1)
468c2ecf20Sopenharmony_ci#define IPU_FIELD_SX		IPU_CPMEM_WORD(0, 46, 12)
478c2ecf20Sopenharmony_ci#define IPU_FIELD_SY		IPU_CPMEM_WORD(0, 58, 11)
488c2ecf20Sopenharmony_ci#define IPU_FIELD_NS		IPU_CPMEM_WORD(0, 69, 10)
498c2ecf20Sopenharmony_ci#define IPU_FIELD_SDX		IPU_CPMEM_WORD(0, 79, 7)
508c2ecf20Sopenharmony_ci#define IPU_FIELD_SM		IPU_CPMEM_WORD(0, 86, 10)
518c2ecf20Sopenharmony_ci#define IPU_FIELD_SCC		IPU_CPMEM_WORD(0, 96, 1)
528c2ecf20Sopenharmony_ci#define IPU_FIELD_SCE		IPU_CPMEM_WORD(0, 97, 1)
538c2ecf20Sopenharmony_ci#define IPU_FIELD_SDY		IPU_CPMEM_WORD(0, 98, 7)
548c2ecf20Sopenharmony_ci#define IPU_FIELD_SDRX		IPU_CPMEM_WORD(0, 105, 1)
558c2ecf20Sopenharmony_ci#define IPU_FIELD_SDRY		IPU_CPMEM_WORD(0, 106, 1)
568c2ecf20Sopenharmony_ci#define IPU_FIELD_BPP		IPU_CPMEM_WORD(0, 107, 3)
578c2ecf20Sopenharmony_ci#define IPU_FIELD_DEC_SEL	IPU_CPMEM_WORD(0, 110, 2)
588c2ecf20Sopenharmony_ci#define IPU_FIELD_DIM		IPU_CPMEM_WORD(0, 112, 1)
598c2ecf20Sopenharmony_ci#define IPU_FIELD_BNDM		IPU_CPMEM_WORD(0, 114, 3)
608c2ecf20Sopenharmony_ci#define IPU_FIELD_BM		IPU_CPMEM_WORD(0, 117, 2)
618c2ecf20Sopenharmony_ci#define IPU_FIELD_ROT		IPU_CPMEM_WORD(0, 119, 1)
628c2ecf20Sopenharmony_ci#define IPU_FIELD_ROT_HF_VF	IPU_CPMEM_WORD(0, 119, 3)
638c2ecf20Sopenharmony_ci#define IPU_FIELD_HF		IPU_CPMEM_WORD(0, 120, 1)
648c2ecf20Sopenharmony_ci#define IPU_FIELD_VF		IPU_CPMEM_WORD(0, 121, 1)
658c2ecf20Sopenharmony_ci#define IPU_FIELD_THE		IPU_CPMEM_WORD(0, 122, 1)
668c2ecf20Sopenharmony_ci#define IPU_FIELD_CAP		IPU_CPMEM_WORD(0, 123, 1)
678c2ecf20Sopenharmony_ci#define IPU_FIELD_CAE		IPU_CPMEM_WORD(0, 124, 1)
688c2ecf20Sopenharmony_ci#define IPU_FIELD_FW		IPU_CPMEM_WORD(0, 125, 13)
698c2ecf20Sopenharmony_ci#define IPU_FIELD_FH		IPU_CPMEM_WORD(0, 138, 12)
708c2ecf20Sopenharmony_ci#define IPU_FIELD_EBA0		IPU_CPMEM_WORD(1, 0, 29)
718c2ecf20Sopenharmony_ci#define IPU_FIELD_EBA1		IPU_CPMEM_WORD(1, 29, 29)
728c2ecf20Sopenharmony_ci#define IPU_FIELD_ILO		IPU_CPMEM_WORD(1, 58, 20)
738c2ecf20Sopenharmony_ci#define IPU_FIELD_NPB		IPU_CPMEM_WORD(1, 78, 7)
748c2ecf20Sopenharmony_ci#define IPU_FIELD_PFS		IPU_CPMEM_WORD(1, 85, 4)
758c2ecf20Sopenharmony_ci#define IPU_FIELD_ALU		IPU_CPMEM_WORD(1, 89, 1)
768c2ecf20Sopenharmony_ci#define IPU_FIELD_ALBM		IPU_CPMEM_WORD(1, 90, 3)
778c2ecf20Sopenharmony_ci#define IPU_FIELD_ID		IPU_CPMEM_WORD(1, 93, 2)
788c2ecf20Sopenharmony_ci#define IPU_FIELD_TH		IPU_CPMEM_WORD(1, 95, 7)
798c2ecf20Sopenharmony_ci#define IPU_FIELD_SL		IPU_CPMEM_WORD(1, 102, 14)
808c2ecf20Sopenharmony_ci#define IPU_FIELD_WID0		IPU_CPMEM_WORD(1, 116, 3)
818c2ecf20Sopenharmony_ci#define IPU_FIELD_WID1		IPU_CPMEM_WORD(1, 119, 3)
828c2ecf20Sopenharmony_ci#define IPU_FIELD_WID2		IPU_CPMEM_WORD(1, 122, 3)
838c2ecf20Sopenharmony_ci#define IPU_FIELD_WID3		IPU_CPMEM_WORD(1, 125, 3)
848c2ecf20Sopenharmony_ci#define IPU_FIELD_OFS0		IPU_CPMEM_WORD(1, 128, 5)
858c2ecf20Sopenharmony_ci#define IPU_FIELD_OFS1		IPU_CPMEM_WORD(1, 133, 5)
868c2ecf20Sopenharmony_ci#define IPU_FIELD_OFS2		IPU_CPMEM_WORD(1, 138, 5)
878c2ecf20Sopenharmony_ci#define IPU_FIELD_OFS3		IPU_CPMEM_WORD(1, 143, 5)
888c2ecf20Sopenharmony_ci#define IPU_FIELD_SXYS		IPU_CPMEM_WORD(1, 148, 1)
898c2ecf20Sopenharmony_ci#define IPU_FIELD_CRE		IPU_CPMEM_WORD(1, 149, 1)
908c2ecf20Sopenharmony_ci#define IPU_FIELD_DEC_SEL2	IPU_CPMEM_WORD(1, 150, 1)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic inline struct ipu_ch_param __iomem *
938c2ecf20Sopenharmony_ciipu_get_cpmem(struct ipuv3_channel *ch)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct ipu_cpmem *cpmem = ch->ipu->cpmem_priv;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return cpmem->base + ch->num;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void ipu_ch_param_write_field(struct ipuv3_channel *ch, u32 wbs, u32 v)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct ipu_ch_param __iomem *base = ipu_get_cpmem(ch);
1038c2ecf20Sopenharmony_ci	u32 bit = (wbs >> 8) % 160;
1048c2ecf20Sopenharmony_ci	u32 size = wbs & 0xff;
1058c2ecf20Sopenharmony_ci	u32 word = (wbs >> 8) / 160;
1068c2ecf20Sopenharmony_ci	u32 i = bit / 32;
1078c2ecf20Sopenharmony_ci	u32 ofs = bit % 32;
1088c2ecf20Sopenharmony_ci	u32 mask = (1 << size) - 1;
1098c2ecf20Sopenharmony_ci	u32 val;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	pr_debug("%s %d %d %d\n", __func__, word, bit , size);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	val = readl(&base->word[word].data[i]);
1148c2ecf20Sopenharmony_ci	val &= ~(mask << ofs);
1158c2ecf20Sopenharmony_ci	val |= v << ofs;
1168c2ecf20Sopenharmony_ci	writel(val, &base->word[word].data[i]);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if ((bit + size - 1) / 32 > i) {
1198c2ecf20Sopenharmony_ci		val = readl(&base->word[word].data[i + 1]);
1208c2ecf20Sopenharmony_ci		val &= ~(mask >> (ofs ? (32 - ofs) : 0));
1218c2ecf20Sopenharmony_ci		val |= v >> (ofs ? (32 - ofs) : 0);
1228c2ecf20Sopenharmony_ci		writel(val, &base->word[word].data[i + 1]);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic u32 ipu_ch_param_read_field(struct ipuv3_channel *ch, u32 wbs)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct ipu_ch_param __iomem *base = ipu_get_cpmem(ch);
1298c2ecf20Sopenharmony_ci	u32 bit = (wbs >> 8) % 160;
1308c2ecf20Sopenharmony_ci	u32 size = wbs & 0xff;
1318c2ecf20Sopenharmony_ci	u32 word = (wbs >> 8) / 160;
1328c2ecf20Sopenharmony_ci	u32 i = bit / 32;
1338c2ecf20Sopenharmony_ci	u32 ofs = bit % 32;
1348c2ecf20Sopenharmony_ci	u32 mask = (1 << size) - 1;
1358c2ecf20Sopenharmony_ci	u32 val = 0;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	pr_debug("%s %d %d %d\n", __func__, word, bit , size);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	val = (readl(&base->word[word].data[i]) >> ofs) & mask;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if ((bit + size - 1) / 32 > i) {
1428c2ecf20Sopenharmony_ci		u32 tmp;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		tmp = readl(&base->word[word].data[i + 1]);
1458c2ecf20Sopenharmony_ci		tmp &= mask >> (ofs ? (32 - ofs) : 0);
1468c2ecf20Sopenharmony_ci		val |= tmp << (ofs ? (32 - ofs) : 0);
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return val;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * The V4L2 spec defines packed RGB formats in memory byte order, which from
1548c2ecf20Sopenharmony_ci * point of view of the IPU corresponds to little-endian words with the first
1558c2ecf20Sopenharmony_ci * component in the least significant bits.
1568c2ecf20Sopenharmony_ci * The DRM pixel formats and IPU internal representation are ordered the other
1578c2ecf20Sopenharmony_ci * way around, with the first named component ordered at the most significant
1588c2ecf20Sopenharmony_ci * bits. Further, V4L2 formats are not well defined:
1598c2ecf20Sopenharmony_ci *     https://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html
1608c2ecf20Sopenharmony_ci * We choose the interpretation which matches GStreamer behavior.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_cistatic int v4l2_pix_fmt_to_drm_fourcc(u32 pixelformat)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	switch (pixelformat) {
1658c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565:
1668c2ecf20Sopenharmony_ci		/*
1678c2ecf20Sopenharmony_ci		 * Here we choose the 'corrected' interpretation of RGBP, a
1688c2ecf20Sopenharmony_ci		 * little-endian 16-bit word with the red component at the most
1698c2ecf20Sopenharmony_ci		 * significant bits:
1708c2ecf20Sopenharmony_ci		 * g[2:0]b[4:0] r[4:0]g[5:3] <=> [16:0] R:G:B
1718c2ecf20Sopenharmony_ci		 */
1728c2ecf20Sopenharmony_ci		return DRM_FORMAT_RGB565;
1738c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGR24:
1748c2ecf20Sopenharmony_ci		/* B G R <=> [24:0] R:G:B */
1758c2ecf20Sopenharmony_ci		return DRM_FORMAT_RGB888;
1768c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB24:
1778c2ecf20Sopenharmony_ci		/* R G B <=> [24:0] B:G:R */
1788c2ecf20Sopenharmony_ci		return DRM_FORMAT_BGR888;
1798c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGR32:
1808c2ecf20Sopenharmony_ci		/* B G R A <=> [32:0] A:B:G:R */
1818c2ecf20Sopenharmony_ci		return DRM_FORMAT_XRGB8888;
1828c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB32:
1838c2ecf20Sopenharmony_ci		/* R G B A <=> [32:0] A:B:G:R */
1848c2ecf20Sopenharmony_ci		return DRM_FORMAT_XBGR8888;
1858c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_ABGR32:
1868c2ecf20Sopenharmony_ci		/* B G R A <=> [32:0] A:R:G:B */
1878c2ecf20Sopenharmony_ci		return DRM_FORMAT_ARGB8888;
1888c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XBGR32:
1898c2ecf20Sopenharmony_ci		/* B G R X <=> [32:0] X:R:G:B */
1908c2ecf20Sopenharmony_ci		return DRM_FORMAT_XRGB8888;
1918c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGRA32:
1928c2ecf20Sopenharmony_ci		/* A B G R <=> [32:0] R:G:B:A */
1938c2ecf20Sopenharmony_ci		return DRM_FORMAT_RGBA8888;
1948c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGRX32:
1958c2ecf20Sopenharmony_ci		/* X B G R <=> [32:0] R:G:B:X */
1968c2ecf20Sopenharmony_ci		return DRM_FORMAT_RGBX8888;
1978c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGBA32:
1988c2ecf20Sopenharmony_ci		/* R G B A <=> [32:0] A:B:G:R */
1998c2ecf20Sopenharmony_ci		return DRM_FORMAT_ABGR8888;
2008c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGBX32:
2018c2ecf20Sopenharmony_ci		/* R G B X <=> [32:0] X:B:G:R */
2028c2ecf20Sopenharmony_ci		return DRM_FORMAT_XBGR8888;
2038c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_ARGB32:
2048c2ecf20Sopenharmony_ci		/* A R G B <=> [32:0] B:G:R:A */
2058c2ecf20Sopenharmony_ci		return DRM_FORMAT_BGRA8888;
2068c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XRGB32:
2078c2ecf20Sopenharmony_ci		/* X R G B <=> [32:0] B:G:R:X */
2088c2ecf20Sopenharmony_ci		return DRM_FORMAT_BGRX8888;
2098c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
2108c2ecf20Sopenharmony_ci		return DRM_FORMAT_UYVY;
2118c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
2128c2ecf20Sopenharmony_ci		return DRM_FORMAT_YUYV;
2138c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
2148c2ecf20Sopenharmony_ci		return DRM_FORMAT_YUV420;
2158c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
2168c2ecf20Sopenharmony_ci		return DRM_FORMAT_YUV422;
2178c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVU420:
2188c2ecf20Sopenharmony_ci		return DRM_FORMAT_YVU420;
2198c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
2208c2ecf20Sopenharmony_ci		return DRM_FORMAT_NV12;
2218c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
2228c2ecf20Sopenharmony_ci		return DRM_FORMAT_NV16;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return -EINVAL;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_civoid ipu_cpmem_zero(struct ipuv3_channel *ch)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct ipu_ch_param __iomem *p = ipu_get_cpmem(ch);
2318c2ecf20Sopenharmony_ci	void __iomem *base = p;
2328c2ecf20Sopenharmony_ci	int i;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(*p) / sizeof(u32); i++)
2358c2ecf20Sopenharmony_ci		writel(0, base + i * sizeof(u32));
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_zero);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_civoid ipu_cpmem_set_resolution(struct ipuv3_channel *ch, int xres, int yres)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_FW, xres - 1);
2428c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_FH, yres - 1);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_resolution);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_civoid ipu_cpmem_skip_odd_chroma_rows(struct ipuv3_channel *ch)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_RDRW, 1);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_skip_odd_chroma_rows);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_civoid ipu_cpmem_set_stride(struct ipuv3_channel *ch, int stride)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_SLY, stride - 1);
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_stride);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_civoid ipu_cpmem_set_high_priority(struct ipuv3_channel *ch)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct ipu_soc *ipu = ch->ipu;
2618c2ecf20Sopenharmony_ci	u32 val;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (ipu->ipu_type == IPUV3EX)
2648c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_ID, 1);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	val = ipu_idmac_read(ipu, IDMAC_CHA_PRI(ch->num));
2678c2ecf20Sopenharmony_ci	val |= 1 << (ch->num % 32);
2688c2ecf20Sopenharmony_ci	ipu_idmac_write(ipu, val, IDMAC_CHA_PRI(ch->num));
2698c2ecf20Sopenharmony_ci};
2708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_high_priority);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_civoid ipu_cpmem_set_buffer(struct ipuv3_channel *ch, int bufnum, dma_addr_t buf)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	WARN_ON_ONCE(buf & 0x7);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (bufnum)
2778c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_EBA1, buf >> 3);
2788c2ecf20Sopenharmony_ci	else
2798c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_EBA0, buf >> 3);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_buffer);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_civoid ipu_cpmem_set_uv_offset(struct ipuv3_channel *ch, u32 u_off, u32 v_off)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	WARN_ON_ONCE((u_off & 0x7) || (v_off & 0x7));
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_UBO, u_off / 8);
2888c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_VBO, v_off / 8);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_uv_offset);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_civoid ipu_cpmem_interlaced_scan(struct ipuv3_channel *ch, int stride,
2938c2ecf20Sopenharmony_ci			       u32 pixelformat)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	u32 ilo, sly, sluv;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (stride < 0) {
2988c2ecf20Sopenharmony_ci		stride = -stride;
2998c2ecf20Sopenharmony_ci		ilo = 0x100000 - (stride / 8);
3008c2ecf20Sopenharmony_ci	} else {
3018c2ecf20Sopenharmony_ci		ilo = stride / 8;
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	sly = (stride * 2) - 1;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	switch (pixelformat) {
3078c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
3088c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVU420:
3098c2ecf20Sopenharmony_ci		sluv = stride / 2 - 1;
3108c2ecf20Sopenharmony_ci		break;
3118c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
3128c2ecf20Sopenharmony_ci		sluv = stride - 1;
3138c2ecf20Sopenharmony_ci		break;
3148c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
3158c2ecf20Sopenharmony_ci		sluv = stride - 1;
3168c2ecf20Sopenharmony_ci		break;
3178c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
3188c2ecf20Sopenharmony_ci		sluv = stride * 2 - 1;
3198c2ecf20Sopenharmony_ci		break;
3208c2ecf20Sopenharmony_ci	default:
3218c2ecf20Sopenharmony_ci		sluv = 0;
3228c2ecf20Sopenharmony_ci		break;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_SO, 1);
3268c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_ILO, ilo);
3278c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_SLY, sly);
3288c2ecf20Sopenharmony_ci	if (sluv)
3298c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_SLUV, sluv);
3308c2ecf20Sopenharmony_ci};
3318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_interlaced_scan);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_civoid ipu_cpmem_set_axi_id(struct ipuv3_channel *ch, u32 id)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	id &= 0x3;
3368c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_ID, id);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_axi_id);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ciint ipu_cpmem_get_burstsize(struct ipuv3_channel *ch)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	return ipu_ch_param_read_field(ch, IPU_FIELD_NPB) + 1;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_get_burstsize);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_civoid ipu_cpmem_set_burstsize(struct ipuv3_channel *ch, int burstsize)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_NPB, burstsize - 1);
3498c2ecf20Sopenharmony_ci};
3508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_burstsize);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_civoid ipu_cpmem_set_block_mode(struct ipuv3_channel *ch)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_BM, 1);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_block_mode);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_civoid ipu_cpmem_set_rotation(struct ipuv3_channel *ch,
3598c2ecf20Sopenharmony_ci			    enum ipu_rotate_mode rot)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	u32 temp_rot = bitrev8(rot) >> 5;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_ROT_HF_VF, temp_rot);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_rotation);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ciint ipu_cpmem_set_format_rgb(struct ipuv3_channel *ch,
3688c2ecf20Sopenharmony_ci			     const struct ipu_rgb *rgb)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	int bpp = 0, npb = 0, ro, go, bo, to;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	ro = rgb->bits_per_pixel - rgb->red.length - rgb->red.offset;
3738c2ecf20Sopenharmony_ci	go = rgb->bits_per_pixel - rgb->green.length - rgb->green.offset;
3748c2ecf20Sopenharmony_ci	bo = rgb->bits_per_pixel - rgb->blue.length - rgb->blue.offset;
3758c2ecf20Sopenharmony_ci	to = rgb->bits_per_pixel - rgb->transp.length - rgb->transp.offset;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_WID0, rgb->red.length - 1);
3788c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_OFS0, ro);
3798c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_WID1, rgb->green.length - 1);
3808c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_OFS1, go);
3818c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_WID2, rgb->blue.length - 1);
3828c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_OFS2, bo);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (rgb->transp.length) {
3858c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_WID3,
3868c2ecf20Sopenharmony_ci				rgb->transp.length - 1);
3878c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_OFS3, to);
3888c2ecf20Sopenharmony_ci	} else {
3898c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_WID3, 7);
3908c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_OFS3,
3918c2ecf20Sopenharmony_ci				rgb->bits_per_pixel);
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	switch (rgb->bits_per_pixel) {
3958c2ecf20Sopenharmony_ci	case 32:
3968c2ecf20Sopenharmony_ci		bpp = 0;
3978c2ecf20Sopenharmony_ci		npb = 15;
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	case 24:
4008c2ecf20Sopenharmony_ci		bpp = 1;
4018c2ecf20Sopenharmony_ci		npb = 19;
4028c2ecf20Sopenharmony_ci		break;
4038c2ecf20Sopenharmony_ci	case 16:
4048c2ecf20Sopenharmony_ci		bpp = 3;
4058c2ecf20Sopenharmony_ci		npb = 31;
4068c2ecf20Sopenharmony_ci		break;
4078c2ecf20Sopenharmony_ci	case 8:
4088c2ecf20Sopenharmony_ci		bpp = 5;
4098c2ecf20Sopenharmony_ci		npb = 63;
4108c2ecf20Sopenharmony_ci		break;
4118c2ecf20Sopenharmony_ci	default:
4128c2ecf20Sopenharmony_ci		return -EINVAL;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_BPP, bpp);
4158c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_NPB, npb);
4168c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 7); /* rgb mode */
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	return 0;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_format_rgb);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ciint ipu_cpmem_set_format_passthrough(struct ipuv3_channel *ch, int width)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	int bpp = 0, npb = 0;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	switch (width) {
4278c2ecf20Sopenharmony_ci	case 32:
4288c2ecf20Sopenharmony_ci		bpp = 0;
4298c2ecf20Sopenharmony_ci		npb = 15;
4308c2ecf20Sopenharmony_ci		break;
4318c2ecf20Sopenharmony_ci	case 24:
4328c2ecf20Sopenharmony_ci		bpp = 1;
4338c2ecf20Sopenharmony_ci		npb = 19;
4348c2ecf20Sopenharmony_ci		break;
4358c2ecf20Sopenharmony_ci	case 16:
4368c2ecf20Sopenharmony_ci		bpp = 3;
4378c2ecf20Sopenharmony_ci		npb = 31;
4388c2ecf20Sopenharmony_ci		break;
4398c2ecf20Sopenharmony_ci	case 8:
4408c2ecf20Sopenharmony_ci		bpp = 5;
4418c2ecf20Sopenharmony_ci		npb = 63;
4428c2ecf20Sopenharmony_ci		break;
4438c2ecf20Sopenharmony_ci	default:
4448c2ecf20Sopenharmony_ci		return -EINVAL;
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_BPP, bpp);
4488c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_NPB, npb);
4498c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 6); /* raw mode */
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return 0;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_format_passthrough);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_civoid ipu_cpmem_set_yuv_interleaved(struct ipuv3_channel *ch, u32 pixel_format)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	switch (pixel_format) {
4588c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
4598c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3); /* bits/pixel */
4608c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0xA);/* pix fmt */
4618c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);/* burst size */
4628c2ecf20Sopenharmony_ci		break;
4638c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
4648c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3); /* bits/pixel */
4658c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0x8);/* pix fmt */
4668c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);/* burst size */
4678c2ecf20Sopenharmony_ci		break;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_interleaved);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_civoid ipu_cpmem_set_yuv_planar_full(struct ipuv3_channel *ch,
4738c2ecf20Sopenharmony_ci				   unsigned int uv_stride,
4748c2ecf20Sopenharmony_ci				   unsigned int u_offset, unsigned int v_offset)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	WARN_ON_ONCE((u_offset & 0x7) || (v_offset & 0x7));
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_SLUV, uv_stride - 1);
4798c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_UBO, u_offset / 8);
4808c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_VBO, v_offset / 8);
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar_full);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_xrgb_32 = {
4858c2ecf20Sopenharmony_ci	.red	= { .offset = 16, .length = 8, },
4868c2ecf20Sopenharmony_ci	.green	= { .offset =  8, .length = 8, },
4878c2ecf20Sopenharmony_ci	.blue	= { .offset =  0, .length = 8, },
4888c2ecf20Sopenharmony_ci	.transp = { .offset = 24, .length = 8, },
4898c2ecf20Sopenharmony_ci	.bits_per_pixel = 32,
4908c2ecf20Sopenharmony_ci};
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_xbgr_32 = {
4938c2ecf20Sopenharmony_ci	.red	= { .offset =  0, .length = 8, },
4948c2ecf20Sopenharmony_ci	.green	= { .offset =  8, .length = 8, },
4958c2ecf20Sopenharmony_ci	.blue	= { .offset = 16, .length = 8, },
4968c2ecf20Sopenharmony_ci	.transp = { .offset = 24, .length = 8, },
4978c2ecf20Sopenharmony_ci	.bits_per_pixel = 32,
4988c2ecf20Sopenharmony_ci};
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_rgbx_32 = {
5018c2ecf20Sopenharmony_ci	.red	= { .offset = 24, .length = 8, },
5028c2ecf20Sopenharmony_ci	.green	= { .offset = 16, .length = 8, },
5038c2ecf20Sopenharmony_ci	.blue	= { .offset =  8, .length = 8, },
5048c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 8, },
5058c2ecf20Sopenharmony_ci	.bits_per_pixel = 32,
5068c2ecf20Sopenharmony_ci};
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_bgrx_32 = {
5098c2ecf20Sopenharmony_ci	.red	= { .offset =  8, .length = 8, },
5108c2ecf20Sopenharmony_ci	.green	= { .offset = 16, .length = 8, },
5118c2ecf20Sopenharmony_ci	.blue	= { .offset = 24, .length = 8, },
5128c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 8, },
5138c2ecf20Sopenharmony_ci	.bits_per_pixel = 32,
5148c2ecf20Sopenharmony_ci};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_rgb_24 = {
5178c2ecf20Sopenharmony_ci	.red	= { .offset = 16, .length = 8, },
5188c2ecf20Sopenharmony_ci	.green	= { .offset =  8, .length = 8, },
5198c2ecf20Sopenharmony_ci	.blue	= { .offset =  0, .length = 8, },
5208c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 0, },
5218c2ecf20Sopenharmony_ci	.bits_per_pixel = 24,
5228c2ecf20Sopenharmony_ci};
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_bgr_24 = {
5258c2ecf20Sopenharmony_ci	.red	= { .offset =  0, .length = 8, },
5268c2ecf20Sopenharmony_ci	.green	= { .offset =  8, .length = 8, },
5278c2ecf20Sopenharmony_ci	.blue	= { .offset = 16, .length = 8, },
5288c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 0, },
5298c2ecf20Sopenharmony_ci	.bits_per_pixel = 24,
5308c2ecf20Sopenharmony_ci};
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_rgb_16 = {
5338c2ecf20Sopenharmony_ci	.red	= { .offset = 11, .length = 5, },
5348c2ecf20Sopenharmony_ci	.green	= { .offset =  5, .length = 6, },
5358c2ecf20Sopenharmony_ci	.blue	= { .offset =  0, .length = 5, },
5368c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 0, },
5378c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5388c2ecf20Sopenharmony_ci};
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_bgr_16 = {
5418c2ecf20Sopenharmony_ci	.red	= { .offset =  0, .length = 5, },
5428c2ecf20Sopenharmony_ci	.green	= { .offset =  5, .length = 6, },
5438c2ecf20Sopenharmony_ci	.blue	= { .offset = 11, .length = 5, },
5448c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 0, },
5458c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5468c2ecf20Sopenharmony_ci};
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_argb_16 = {
5498c2ecf20Sopenharmony_ci	.red	= { .offset = 10, .length = 5, },
5508c2ecf20Sopenharmony_ci	.green	= { .offset =  5, .length = 5, },
5518c2ecf20Sopenharmony_ci	.blue	= { .offset =  0, .length = 5, },
5528c2ecf20Sopenharmony_ci	.transp = { .offset = 15, .length = 1, },
5538c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5548c2ecf20Sopenharmony_ci};
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_argb_16_4444 = {
5578c2ecf20Sopenharmony_ci	.red	= { .offset =  8, .length = 4, },
5588c2ecf20Sopenharmony_ci	.green	= { .offset =  4, .length = 4, },
5598c2ecf20Sopenharmony_ci	.blue	= { .offset =  0, .length = 4, },
5608c2ecf20Sopenharmony_ci	.transp = { .offset = 12, .length = 4, },
5618c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5628c2ecf20Sopenharmony_ci};
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_abgr_16 = {
5658c2ecf20Sopenharmony_ci	.red	= { .offset =  0, .length = 5, },
5668c2ecf20Sopenharmony_ci	.green	= { .offset =  5, .length = 5, },
5678c2ecf20Sopenharmony_ci	.blue	= { .offset = 10, .length = 5, },
5688c2ecf20Sopenharmony_ci	.transp = { .offset = 15, .length = 1, },
5698c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5708c2ecf20Sopenharmony_ci};
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_rgba_16 = {
5738c2ecf20Sopenharmony_ci	.red	= { .offset = 11, .length = 5, },
5748c2ecf20Sopenharmony_ci	.green	= { .offset =  6, .length = 5, },
5758c2ecf20Sopenharmony_ci	.blue	= { .offset =  1, .length = 5, },
5768c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 1, },
5778c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5788c2ecf20Sopenharmony_ci};
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic const struct ipu_rgb def_bgra_16 = {
5818c2ecf20Sopenharmony_ci	.red	= { .offset =  1, .length = 5, },
5828c2ecf20Sopenharmony_ci	.green	= { .offset =  6, .length = 5, },
5838c2ecf20Sopenharmony_ci	.blue	= { .offset = 11, .length = 5, },
5848c2ecf20Sopenharmony_ci	.transp = { .offset =  0, .length = 1, },
5858c2ecf20Sopenharmony_ci	.bits_per_pixel = 16,
5868c2ecf20Sopenharmony_ci};
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci#define Y_OFFSET(pix, x, y)	((x) + pix->bytesperline * (y))
5898c2ecf20Sopenharmony_ci#define U_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
5908c2ecf20Sopenharmony_ci				 (pix->bytesperline * ((y) / 2) / 2) + (x) / 2)
5918c2ecf20Sopenharmony_ci#define V_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
5928c2ecf20Sopenharmony_ci				 (pix->bytesperline * pix->height / 4) + \
5938c2ecf20Sopenharmony_ci				 (pix->bytesperline * ((y) / 2) / 2) + (x) / 2)
5948c2ecf20Sopenharmony_ci#define U2_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
5958c2ecf20Sopenharmony_ci				 (pix->bytesperline * (y) / 2) + (x) / 2)
5968c2ecf20Sopenharmony_ci#define V2_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
5978c2ecf20Sopenharmony_ci				 (pix->bytesperline * pix->height / 2) + \
5988c2ecf20Sopenharmony_ci				 (pix->bytesperline * (y) / 2) + (x) / 2)
5998c2ecf20Sopenharmony_ci#define UV_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
6008c2ecf20Sopenharmony_ci				 (pix->bytesperline * ((y) / 2)) + (x))
6018c2ecf20Sopenharmony_ci#define UV2_OFFSET(pix, x, y)	((pix->bytesperline * pix->height) +	 \
6028c2ecf20Sopenharmony_ci				 (pix->bytesperline * y) + (x))
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci#define NUM_ALPHA_CHANNELS	7
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci/* See Table 37-12. Alpha channels mapping. */
6078c2ecf20Sopenharmony_cistatic int ipu_channel_albm(int ch_num)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	switch (ch_num) {
6108c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_G_MEM_IC_PRP_VF:	return 0;
6118c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_G_MEM_IC_PP:		return 1;
6128c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_MEM_FG_SYNC:		return 2;
6138c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_MEM_FG_ASYNC:	return 3;
6148c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_MEM_BG_SYNC:		return 4;
6158c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_MEM_BG_ASYNC:	return 5;
6168c2ecf20Sopenharmony_ci	case IPUV3_CHANNEL_MEM_VDI_PLANE1_COMB: return 6;
6178c2ecf20Sopenharmony_ci	default:
6188c2ecf20Sopenharmony_ci		return -EINVAL;
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cistatic void ipu_cpmem_set_separate_alpha(struct ipuv3_channel *ch)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	struct ipu_soc *ipu = ch->ipu;
6258c2ecf20Sopenharmony_ci	int albm;
6268c2ecf20Sopenharmony_ci	u32 val;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	albm = ipu_channel_albm(ch->num);
6298c2ecf20Sopenharmony_ci	if (albm < 0)
6308c2ecf20Sopenharmony_ci		return;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_ALU, 1);
6338c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_ALBM, albm);
6348c2ecf20Sopenharmony_ci	ipu_ch_param_write_field(ch, IPU_FIELD_CRE, 1);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	val = ipu_idmac_read(ipu, IDMAC_SEP_ALPHA);
6378c2ecf20Sopenharmony_ci	val |= BIT(ch->num);
6388c2ecf20Sopenharmony_ci	ipu_idmac_write(ipu, val, IDMAC_SEP_ALPHA);
6398c2ecf20Sopenharmony_ci}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ciint ipu_cpmem_set_fmt(struct ipuv3_channel *ch, u32 drm_fourcc)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	switch (drm_fourcc) {
6448c2ecf20Sopenharmony_ci	case DRM_FORMAT_YUV420:
6458c2ecf20Sopenharmony_ci	case DRM_FORMAT_YVU420:
6468c2ecf20Sopenharmony_ci		/* pix format */
6478c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 2);
6488c2ecf20Sopenharmony_ci		/* burst size */
6498c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6508c2ecf20Sopenharmony_ci		break;
6518c2ecf20Sopenharmony_ci	case DRM_FORMAT_YUV422:
6528c2ecf20Sopenharmony_ci	case DRM_FORMAT_YVU422:
6538c2ecf20Sopenharmony_ci		/* pix format */
6548c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 1);
6558c2ecf20Sopenharmony_ci		/* burst size */
6568c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	case DRM_FORMAT_YUV444:
6598c2ecf20Sopenharmony_ci	case DRM_FORMAT_YVU444:
6608c2ecf20Sopenharmony_ci		/* pix format */
6618c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0);
6628c2ecf20Sopenharmony_ci		/* burst size */
6638c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6648c2ecf20Sopenharmony_ci		break;
6658c2ecf20Sopenharmony_ci	case DRM_FORMAT_NV12:
6668c2ecf20Sopenharmony_ci		/* pix format */
6678c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 4);
6688c2ecf20Sopenharmony_ci		/* burst size */
6698c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6708c2ecf20Sopenharmony_ci		break;
6718c2ecf20Sopenharmony_ci	case DRM_FORMAT_NV16:
6728c2ecf20Sopenharmony_ci		/* pix format */
6738c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 3);
6748c2ecf20Sopenharmony_ci		/* burst size */
6758c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6768c2ecf20Sopenharmony_ci		break;
6778c2ecf20Sopenharmony_ci	case DRM_FORMAT_UYVY:
6788c2ecf20Sopenharmony_ci		/* bits/pixel */
6798c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3);
6808c2ecf20Sopenharmony_ci		/* pix format */
6818c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0xA);
6828c2ecf20Sopenharmony_ci		/* burst size */
6838c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6848c2ecf20Sopenharmony_ci		break;
6858c2ecf20Sopenharmony_ci	case DRM_FORMAT_YUYV:
6868c2ecf20Sopenharmony_ci		/* bits/pixel */
6878c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3);
6888c2ecf20Sopenharmony_ci		/* pix format */
6898c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0x8);
6908c2ecf20Sopenharmony_ci		/* burst size */
6918c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
6928c2ecf20Sopenharmony_ci		break;
6938c2ecf20Sopenharmony_ci	case DRM_FORMAT_ABGR8888:
6948c2ecf20Sopenharmony_ci	case DRM_FORMAT_XBGR8888:
6958c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_xbgr_32);
6968c2ecf20Sopenharmony_ci		break;
6978c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB8888:
6988c2ecf20Sopenharmony_ci	case DRM_FORMAT_XRGB8888:
6998c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_xrgb_32);
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGBA8888:
7028c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGBX8888:
7038c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGBX8888_A8:
7048c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_rgbx_32);
7058c2ecf20Sopenharmony_ci		break;
7068c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGRA8888:
7078c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGRX8888:
7088c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGRX8888_A8:
7098c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_bgrx_32);
7108c2ecf20Sopenharmony_ci		break;
7118c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR888:
7128c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR888_A8:
7138c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_bgr_24);
7148c2ecf20Sopenharmony_ci		break;
7158c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB888:
7168c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB888_A8:
7178c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_rgb_24);
7188c2ecf20Sopenharmony_ci		break;
7198c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB565:
7208c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB565_A8:
7218c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_rgb_16);
7228c2ecf20Sopenharmony_ci		break;
7238c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR565:
7248c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR565_A8:
7258c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_bgr_16);
7268c2ecf20Sopenharmony_ci		break;
7278c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB1555:
7288c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_argb_16);
7298c2ecf20Sopenharmony_ci		break;
7308c2ecf20Sopenharmony_ci	case DRM_FORMAT_ABGR1555:
7318c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_abgr_16);
7328c2ecf20Sopenharmony_ci		break;
7338c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGBA5551:
7348c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_rgba_16);
7358c2ecf20Sopenharmony_ci		break;
7368c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGRA5551:
7378c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_bgra_16);
7388c2ecf20Sopenharmony_ci		break;
7398c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB4444:
7408c2ecf20Sopenharmony_ci		ipu_cpmem_set_format_rgb(ch, &def_argb_16_4444);
7418c2ecf20Sopenharmony_ci		break;
7428c2ecf20Sopenharmony_ci	default:
7438c2ecf20Sopenharmony_ci		return -EINVAL;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	switch (drm_fourcc) {
7478c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB565_A8:
7488c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR565_A8:
7498c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB888_A8:
7508c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGR888_A8:
7518c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGBX8888_A8:
7528c2ecf20Sopenharmony_ci	case DRM_FORMAT_BGRX8888_A8:
7538c2ecf20Sopenharmony_ci		ipu_ch_param_write_field(ch, IPU_FIELD_WID3, 7);
7548c2ecf20Sopenharmony_ci		ipu_cpmem_set_separate_alpha(ch);
7558c2ecf20Sopenharmony_ci		break;
7568c2ecf20Sopenharmony_ci	default:
7578c2ecf20Sopenharmony_ci		break;
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	return 0;
7618c2ecf20Sopenharmony_ci}
7628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_fmt);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ciint ipu_cpmem_set_image(struct ipuv3_channel *ch, struct ipu_image *image)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	struct v4l2_pix_format *pix = &image->pix;
7678c2ecf20Sopenharmony_ci	int offset, u_offset, v_offset;
7688c2ecf20Sopenharmony_ci	int ret = 0;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	pr_debug("%s: resolution: %dx%d stride: %d\n",
7718c2ecf20Sopenharmony_ci		 __func__, pix->width, pix->height,
7728c2ecf20Sopenharmony_ci		 pix->bytesperline);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	ipu_cpmem_set_resolution(ch, image->rect.width, image->rect.height);
7758c2ecf20Sopenharmony_ci	ipu_cpmem_set_stride(ch, pix->bytesperline);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	ipu_cpmem_set_fmt(ch, v4l2_pix_fmt_to_drm_fourcc(pix->pixelformat));
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	switch (pix->pixelformat) {
7808c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV420:
7818c2ecf20Sopenharmony_ci		offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
7828c2ecf20Sopenharmony_ci		u_offset = image->u_offset ?
7838c2ecf20Sopenharmony_ci			image->u_offset : U_OFFSET(pix, image->rect.left,
7848c2ecf20Sopenharmony_ci						   image->rect.top) - offset;
7858c2ecf20Sopenharmony_ci		v_offset = image->v_offset ?
7868c2ecf20Sopenharmony_ci			image->v_offset : V_OFFSET(pix, image->rect.left,
7878c2ecf20Sopenharmony_ci						   image->rect.top) - offset;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci		ipu_cpmem_set_yuv_planar_full(ch, pix->bytesperline / 2,
7908c2ecf20Sopenharmony_ci					      u_offset, v_offset);
7918c2ecf20Sopenharmony_ci		break;
7928c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVU420:
7938c2ecf20Sopenharmony_ci		offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
7948c2ecf20Sopenharmony_ci		u_offset = image->u_offset ?
7958c2ecf20Sopenharmony_ci			image->u_offset : V_OFFSET(pix, image->rect.left,
7968c2ecf20Sopenharmony_ci						   image->rect.top) - offset;
7978c2ecf20Sopenharmony_ci		v_offset = image->v_offset ?
7988c2ecf20Sopenharmony_ci			image->v_offset : U_OFFSET(pix, image->rect.left,
7998c2ecf20Sopenharmony_ci						   image->rect.top) - offset;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci		ipu_cpmem_set_yuv_planar_full(ch, pix->bytesperline / 2,
8028c2ecf20Sopenharmony_ci					      u_offset, v_offset);
8038c2ecf20Sopenharmony_ci		break;
8048c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUV422P:
8058c2ecf20Sopenharmony_ci		offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
8068c2ecf20Sopenharmony_ci		u_offset = image->u_offset ?
8078c2ecf20Sopenharmony_ci			image->u_offset : U2_OFFSET(pix, image->rect.left,
8088c2ecf20Sopenharmony_ci						    image->rect.top) - offset;
8098c2ecf20Sopenharmony_ci		v_offset = image->v_offset ?
8108c2ecf20Sopenharmony_ci			image->v_offset : V2_OFFSET(pix, image->rect.left,
8118c2ecf20Sopenharmony_ci						    image->rect.top) - offset;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci		ipu_cpmem_set_yuv_planar_full(ch, pix->bytesperline / 2,
8148c2ecf20Sopenharmony_ci					      u_offset, v_offset);
8158c2ecf20Sopenharmony_ci		break;
8168c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
8178c2ecf20Sopenharmony_ci		offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
8188c2ecf20Sopenharmony_ci		u_offset = image->u_offset ?
8198c2ecf20Sopenharmony_ci			image->u_offset : UV_OFFSET(pix, image->rect.left,
8208c2ecf20Sopenharmony_ci						    image->rect.top) - offset;
8218c2ecf20Sopenharmony_ci		v_offset = image->v_offset ? image->v_offset : 0;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci		ipu_cpmem_set_yuv_planar_full(ch, pix->bytesperline,
8248c2ecf20Sopenharmony_ci					      u_offset, v_offset);
8258c2ecf20Sopenharmony_ci		break;
8268c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
8278c2ecf20Sopenharmony_ci		offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
8288c2ecf20Sopenharmony_ci		u_offset = image->u_offset ?
8298c2ecf20Sopenharmony_ci			image->u_offset : UV2_OFFSET(pix, image->rect.left,
8308c2ecf20Sopenharmony_ci						     image->rect.top) - offset;
8318c2ecf20Sopenharmony_ci		v_offset = image->v_offset ? image->v_offset : 0;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci		ipu_cpmem_set_yuv_planar_full(ch, pix->bytesperline,
8348c2ecf20Sopenharmony_ci					      u_offset, v_offset);
8358c2ecf20Sopenharmony_ci		break;
8368c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
8378c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
8388c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565:
8398c2ecf20Sopenharmony_ci		offset = image->rect.left * 2 +
8408c2ecf20Sopenharmony_ci			image->rect.top * pix->bytesperline;
8418c2ecf20Sopenharmony_ci		break;
8428c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB32:
8438c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGR32:
8448c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_ABGR32:
8458c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XBGR32:
8468c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGRA32:
8478c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGRX32:
8488c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGBA32:
8498c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGBX32:
8508c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_ARGB32:
8518c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XRGB32:
8528c2ecf20Sopenharmony_ci		offset = image->rect.left * 4 +
8538c2ecf20Sopenharmony_ci			image->rect.top * pix->bytesperline;
8548c2ecf20Sopenharmony_ci		break;
8558c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB24:
8568c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_BGR24:
8578c2ecf20Sopenharmony_ci		offset = image->rect.left * 3 +
8588c2ecf20Sopenharmony_ci			image->rect.top * pix->bytesperline;
8598c2ecf20Sopenharmony_ci		break;
8608c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SBGGR8:
8618c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SGBRG8:
8628c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SGRBG8:
8638c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SRGGB8:
8648c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_GREY:
8658c2ecf20Sopenharmony_ci		offset = image->rect.left + image->rect.top * pix->bytesperline;
8668c2ecf20Sopenharmony_ci		break;
8678c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SBGGR16:
8688c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SGBRG16:
8698c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SGRBG16:
8708c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SRGGB16:
8718c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_Y16:
8728c2ecf20Sopenharmony_ci		offset = image->rect.left * 2 +
8738c2ecf20Sopenharmony_ci			 image->rect.top * pix->bytesperline;
8748c2ecf20Sopenharmony_ci		break;
8758c2ecf20Sopenharmony_ci	default:
8768c2ecf20Sopenharmony_ci		/* This should not happen */
8778c2ecf20Sopenharmony_ci		WARN_ON(1);
8788c2ecf20Sopenharmony_ci		offset = 0;
8798c2ecf20Sopenharmony_ci		ret = -EINVAL;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	ipu_cpmem_set_buffer(ch, 0, image->phys0 + offset);
8838c2ecf20Sopenharmony_ci	ipu_cpmem_set_buffer(ch, 1, image->phys1 + offset);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	return ret;
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_set_image);
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_civoid ipu_cpmem_dump(struct ipuv3_channel *ch)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct ipu_ch_param __iomem *p = ipu_get_cpmem(ch);
8928c2ecf20Sopenharmony_ci	struct ipu_soc *ipu = ch->ipu;
8938c2ecf20Sopenharmony_ci	int chno = ch->num;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "ch %d word 0 - %08X %08X %08X %08X %08X\n", chno,
8968c2ecf20Sopenharmony_ci		readl(&p->word[0].data[0]),
8978c2ecf20Sopenharmony_ci		readl(&p->word[0].data[1]),
8988c2ecf20Sopenharmony_ci		readl(&p->word[0].data[2]),
8998c2ecf20Sopenharmony_ci		readl(&p->word[0].data[3]),
9008c2ecf20Sopenharmony_ci		readl(&p->word[0].data[4]));
9018c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "ch %d word 1 - %08X %08X %08X %08X %08X\n", chno,
9028c2ecf20Sopenharmony_ci		readl(&p->word[1].data[0]),
9038c2ecf20Sopenharmony_ci		readl(&p->word[1].data[1]),
9048c2ecf20Sopenharmony_ci		readl(&p->word[1].data[2]),
9058c2ecf20Sopenharmony_ci		readl(&p->word[1].data[3]),
9068c2ecf20Sopenharmony_ci		readl(&p->word[1].data[4]));
9078c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "PFS 0x%x, ",
9088c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_PFS));
9098c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "BPP 0x%x, ",
9108c2ecf20Sopenharmony_ci		ipu_ch_param_read_field(ch, IPU_FIELD_BPP));
9118c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "NPB 0x%x\n",
9128c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_NPB));
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "FW %d, ",
9158c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_FW));
9168c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "FH %d, ",
9178c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_FH));
9188c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "EBA0 0x%x\n",
9198c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_EBA0) << 3);
9208c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "EBA1 0x%x\n",
9218c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_EBA1) << 3);
9228c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Stride %d\n",
9238c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_SL));
9248c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "scan_order %d\n",
9258c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_SO));
9268c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "uv_stride %d\n",
9278c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_SLUV));
9288c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "u_offset 0x%x\n",
9298c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_UBO) << 3);
9308c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "v_offset 0x%x\n",
9318c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_VBO) << 3);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Width0 %d+1, ",
9348c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_WID0));
9358c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Width1 %d+1, ",
9368c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_WID1));
9378c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Width2 %d+1, ",
9388c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_WID2));
9398c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Width3 %d+1, ",
9408c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_WID3));
9418c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Offset0 %d, ",
9428c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_OFS0));
9438c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Offset1 %d, ",
9448c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_OFS1));
9458c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Offset2 %d, ",
9468c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_OFS2));
9478c2ecf20Sopenharmony_ci	dev_dbg(ipu->dev, "Offset3 %d\n",
9488c2ecf20Sopenharmony_ci		 ipu_ch_param_read_field(ch, IPU_FIELD_OFS3));
9498c2ecf20Sopenharmony_ci}
9508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_cpmem_dump);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ciint ipu_cpmem_init(struct ipu_soc *ipu, struct device *dev, unsigned long base)
9538c2ecf20Sopenharmony_ci{
9548c2ecf20Sopenharmony_ci	struct ipu_cpmem *cpmem;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	cpmem = devm_kzalloc(dev, sizeof(*cpmem), GFP_KERNEL);
9578c2ecf20Sopenharmony_ci	if (!cpmem)
9588c2ecf20Sopenharmony_ci		return -ENOMEM;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	ipu->cpmem_priv = cpmem;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	spin_lock_init(&cpmem->lock);
9638c2ecf20Sopenharmony_ci	cpmem->base = devm_ioremap(dev, base, SZ_128K);
9648c2ecf20Sopenharmony_ci	if (!cpmem->base)
9658c2ecf20Sopenharmony_ci		return -ENOMEM;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	dev_dbg(dev, "CPMEM base: 0x%08lx remapped to %p\n",
9688c2ecf20Sopenharmony_ci		base, cpmem->base);
9698c2ecf20Sopenharmony_ci	cpmem->ipu = ipu;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	return 0;
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_civoid ipu_cpmem_exit(struct ipu_soc *ipu)
9758c2ecf20Sopenharmony_ci{
9768c2ecf20Sopenharmony_ci}
977