18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Xilinx Video IP Core
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013-2015 Ideas on Board
68c2ecf20Sopenharmony_ci * Copyright (C) 2013-2015 Xilinx, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
98c2ecf20Sopenharmony_ci *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef __XILINX_VIP_H__
138c2ecf20Sopenharmony_ci#define __XILINX_VIP_H__
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/bitops.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct clk;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Minimum and maximum width and height common to most video IP cores. IP
238c2ecf20Sopenharmony_ci * cores with different requirements must define their own values.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci#define XVIP_MIN_WIDTH			32
268c2ecf20Sopenharmony_ci#define XVIP_MAX_WIDTH			7680
278c2ecf20Sopenharmony_ci#define XVIP_MIN_HEIGHT			32
288c2ecf20Sopenharmony_ci#define XVIP_MAX_HEIGHT			7680
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Pad IDs. IP cores with with multiple inputs or outputs should define
328c2ecf20Sopenharmony_ci * their own values.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define XVIP_PAD_SINK			0
358c2ecf20Sopenharmony_ci#define XVIP_PAD_SOURCE			1
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* Xilinx Video IP Control Registers */
388c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL			0x0000
398c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_SW_ENABLE		BIT(0)
408c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_REG_UPDATE		BIT(1)
418c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_BYPASS		BIT(4)
428c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_TEST_PATTERN		BIT(5)
438c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_FRAME_SYNC_RESET	BIT(30)
448c2ecf20Sopenharmony_ci#define XVIP_CTRL_CONTROL_SW_RESET		BIT(31)
458c2ecf20Sopenharmony_ci#define XVIP_CTRL_STATUS			0x0004
468c2ecf20Sopenharmony_ci#define XVIP_CTRL_STATUS_PROC_STARTED		BIT(0)
478c2ecf20Sopenharmony_ci#define XVIP_CTRL_STATUS_EOF			BIT(1)
488c2ecf20Sopenharmony_ci#define XVIP_CTRL_ERROR				0x0008
498c2ecf20Sopenharmony_ci#define XVIP_CTRL_ERROR_SLAVE_EOL_EARLY		BIT(0)
508c2ecf20Sopenharmony_ci#define XVIP_CTRL_ERROR_SLAVE_EOL_LATE		BIT(1)
518c2ecf20Sopenharmony_ci#define XVIP_CTRL_ERROR_SLAVE_SOF_EARLY		BIT(2)
528c2ecf20Sopenharmony_ci#define XVIP_CTRL_ERROR_SLAVE_SOF_LATE		BIT(3)
538c2ecf20Sopenharmony_ci#define XVIP_CTRL_IRQ_ENABLE			0x000c
548c2ecf20Sopenharmony_ci#define XVIP_CTRL_IRQ_ENABLE_PROC_STARTED	BIT(0)
558c2ecf20Sopenharmony_ci#define XVIP_CTRL_IRQ_EOF			BIT(1)
568c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION			0x0010
578c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_MAJOR_MASK		(0xff << 24)
588c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_MAJOR_SHIFT		24
598c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_MINOR_MASK		(0xff << 16)
608c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_MINOR_SHIFT		16
618c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_REVISION_MASK		(0xf << 12)
628c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_REVISION_SHIFT	12
638c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_PATCH_MASK		(0xf << 8)
648c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_PATCH_SHIFT		8
658c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_INTERNAL_MASK		(0xff << 0)
668c2ecf20Sopenharmony_ci#define XVIP_CTRL_VERSION_INTERNAL_SHIFT	0
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Xilinx Video IP Timing Registers */
698c2ecf20Sopenharmony_ci#define XVIP_ACTIVE_SIZE			0x0020
708c2ecf20Sopenharmony_ci#define XVIP_ACTIVE_VSIZE_MASK			(0x7ff << 16)
718c2ecf20Sopenharmony_ci#define XVIP_ACTIVE_VSIZE_SHIFT			16
728c2ecf20Sopenharmony_ci#define XVIP_ACTIVE_HSIZE_MASK			(0x7ff << 0)
738c2ecf20Sopenharmony_ci#define XVIP_ACTIVE_HSIZE_SHIFT			0
748c2ecf20Sopenharmony_ci#define XVIP_ENCODING				0x0028
758c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_8			(0 << 4)
768c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_10			(1 << 4)
778c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_12			(2 << 4)
788c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_16			(3 << 4)
798c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_MASK		(3 << 4)
808c2ecf20Sopenharmony_ci#define XVIP_ENCODING_NBITS_SHIFT		4
818c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_YUV422	(0 << 0)
828c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_YUV444	(1 << 0)
838c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_RGB		(2 << 0)
848c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_YUV420	(3 << 0)
858c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_MASK		(3 << 0)
868c2ecf20Sopenharmony_ci#define XVIP_ENCODING_VIDEO_FORMAT_SHIFT	0
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/**
898c2ecf20Sopenharmony_ci * struct xvip_device - Xilinx Video IP device structure
908c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
918c2ecf20Sopenharmony_ci * @dev: (OF) device
928c2ecf20Sopenharmony_ci * @iomem: device I/O register space remapped to kernel virtual memory
938c2ecf20Sopenharmony_ci * @clk: video core clock
948c2ecf20Sopenharmony_ci * @saved_ctrl: saved control register for resume / suspend
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistruct xvip_device {
978c2ecf20Sopenharmony_ci	struct v4l2_subdev subdev;
988c2ecf20Sopenharmony_ci	struct device *dev;
998c2ecf20Sopenharmony_ci	void __iomem *iomem;
1008c2ecf20Sopenharmony_ci	struct clk *clk;
1018c2ecf20Sopenharmony_ci	u32 saved_ctrl;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/**
1058c2ecf20Sopenharmony_ci * struct xvip_video_format - Xilinx Video IP video format description
1068c2ecf20Sopenharmony_ci * @vf_code: AXI4 video format code
1078c2ecf20Sopenharmony_ci * @width: AXI4 format width in bits per component
1088c2ecf20Sopenharmony_ci * @pattern: CFA pattern for Mono/Sensor formats
1098c2ecf20Sopenharmony_ci * @code: media bus format code
1108c2ecf20Sopenharmony_ci * @bpp: bytes per pixel (when stored in memory)
1118c2ecf20Sopenharmony_ci * @fourcc: V4L2 pixel format FCC identifier
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistruct xvip_video_format {
1148c2ecf20Sopenharmony_ci	unsigned int vf_code;
1158c2ecf20Sopenharmony_ci	unsigned int width;
1168c2ecf20Sopenharmony_ci	const char *pattern;
1178c2ecf20Sopenharmony_ci	unsigned int code;
1188c2ecf20Sopenharmony_ci	unsigned int bpp;
1198c2ecf20Sopenharmony_ci	u32 fourcc;
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciconst struct xvip_video_format *xvip_get_format_by_code(unsigned int code);
1238c2ecf20Sopenharmony_ciconst struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc);
1248c2ecf20Sopenharmony_ciconst struct xvip_video_format *xvip_of_get_format(struct device_node *node);
1258c2ecf20Sopenharmony_civoid xvip_set_format_size(struct v4l2_mbus_framefmt *format,
1268c2ecf20Sopenharmony_ci			  const struct v4l2_subdev_format *fmt);
1278c2ecf20Sopenharmony_ciint xvip_enum_mbus_code(struct v4l2_subdev *subdev,
1288c2ecf20Sopenharmony_ci			struct v4l2_subdev_pad_config *cfg,
1298c2ecf20Sopenharmony_ci			struct v4l2_subdev_mbus_code_enum *code);
1308c2ecf20Sopenharmony_ciint xvip_enum_frame_size(struct v4l2_subdev *subdev,
1318c2ecf20Sopenharmony_ci			 struct v4l2_subdev_pad_config *cfg,
1328c2ecf20Sopenharmony_ci			 struct v4l2_subdev_frame_size_enum *fse);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic inline u32 xvip_read(struct xvip_device *xvip, u32 addr)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return ioread32(xvip->iomem + addr);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline void xvip_write(struct xvip_device *xvip, u32 addr, u32 value)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	iowrite32(value, xvip->iomem + addr);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic inline void xvip_clr(struct xvip_device *xvip, u32 addr, u32 clr)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	xvip_write(xvip, addr, xvip_read(xvip, addr) & ~clr);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic inline void xvip_set(struct xvip_device *xvip, u32 addr, u32 set)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	xvip_write(xvip, addr, xvip_read(xvip, addr) | set);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_civoid xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set);
1558c2ecf20Sopenharmony_civoid xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciint xvip_init_resources(struct xvip_device *xvip);
1588c2ecf20Sopenharmony_civoid xvip_cleanup_resources(struct xvip_device *xvip);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline void xvip_reset(struct xvip_device *xvip)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	xvip_write(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_SW_RESET);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline void xvip_start(struct xvip_device *xvip)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	xvip_set(xvip, XVIP_CTRL_CONTROL,
1688c2ecf20Sopenharmony_ci		 XVIP_CTRL_CONTROL_SW_ENABLE | XVIP_CTRL_CONTROL_REG_UPDATE);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic inline void xvip_stop(struct xvip_device *xvip)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	xvip_clr(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_SW_ENABLE);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic inline void xvip_resume(struct xvip_device *xvip)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	xvip_write(xvip, XVIP_CTRL_CONTROL,
1798c2ecf20Sopenharmony_ci		   xvip->saved_ctrl | XVIP_CTRL_CONTROL_SW_ENABLE);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline void xvip_suspend(struct xvip_device *xvip)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	xvip->saved_ctrl = xvip_read(xvip, XVIP_CTRL_CONTROL);
1858c2ecf20Sopenharmony_ci	xvip_write(xvip, XVIP_CTRL_CONTROL,
1868c2ecf20Sopenharmony_ci		   xvip->saved_ctrl & ~XVIP_CTRL_CONTROL_SW_ENABLE);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic inline void xvip_set_frame_size(struct xvip_device *xvip,
1908c2ecf20Sopenharmony_ci				       const struct v4l2_mbus_framefmt *format)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	xvip_write(xvip, XVIP_ACTIVE_SIZE,
1938c2ecf20Sopenharmony_ci		   (format->height << XVIP_ACTIVE_VSIZE_SHIFT) |
1948c2ecf20Sopenharmony_ci		   (format->width << XVIP_ACTIVE_HSIZE_SHIFT));
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic inline void xvip_get_frame_size(struct xvip_device *xvip,
1988c2ecf20Sopenharmony_ci				       struct v4l2_mbus_framefmt *format)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	u32 reg;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	reg = xvip_read(xvip, XVIP_ACTIVE_SIZE);
2038c2ecf20Sopenharmony_ci	format->width = (reg & XVIP_ACTIVE_HSIZE_MASK) >>
2048c2ecf20Sopenharmony_ci			XVIP_ACTIVE_HSIZE_SHIFT;
2058c2ecf20Sopenharmony_ci	format->height = (reg & XVIP_ACTIVE_VSIZE_MASK) >>
2068c2ecf20Sopenharmony_ci			 XVIP_ACTIVE_VSIZE_SHIFT;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic inline void xvip_enable_reg_update(struct xvip_device *xvip)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	xvip_set(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_REG_UPDATE);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline void xvip_disable_reg_update(struct xvip_device *xvip)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	xvip_clr(xvip, XVIP_CTRL_CONTROL, XVIP_CTRL_CONTROL_REG_UPDATE);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic inline void xvip_print_version(struct xvip_device *xvip)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	u32 version;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	version = xvip_read(xvip, XVIP_CTRL_VERSION);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	dev_info(xvip->dev, "device found, version %u.%02x%x\n",
2268c2ecf20Sopenharmony_ci		 ((version & XVIP_CTRL_VERSION_MAJOR_MASK) >>
2278c2ecf20Sopenharmony_ci		  XVIP_CTRL_VERSION_MAJOR_SHIFT),
2288c2ecf20Sopenharmony_ci		 ((version & XVIP_CTRL_VERSION_MINOR_MASK) >>
2298c2ecf20Sopenharmony_ci		  XVIP_CTRL_VERSION_MINOR_SHIFT),
2308c2ecf20Sopenharmony_ci		 ((version & XVIP_CTRL_VERSION_REVISION_MASK) >>
2318c2ecf20Sopenharmony_ci		  XVIP_CTRL_VERSION_REVISION_SHIFT));
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci#endif /* __XILINX_VIP_H__ */
235