18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Xilinx Video Timing Controller
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#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "xilinx-vip.h"
198c2ecf20Sopenharmony_ci#include "xilinx-vtc.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define XVTC_CONTROL_FIELD_ID_POL_SRC		(1 << 26)
228c2ecf20Sopenharmony_ci#define XVTC_CONTROL_ACTIVE_CHROMA_POL_SRC	(1 << 25)
238c2ecf20Sopenharmony_ci#define XVTC_CONTROL_ACTIVE_VIDEO_POL_SRC	(1 << 24)
248c2ecf20Sopenharmony_ci#define XVTC_CONTROL_HSYNC_POL_SRC		(1 << 23)
258c2ecf20Sopenharmony_ci#define XVTC_CONTROL_VSYNC_POL_SRC		(1 << 22)
268c2ecf20Sopenharmony_ci#define XVTC_CONTROL_HBLANK_POL_SRC		(1 << 21)
278c2ecf20Sopenharmony_ci#define XVTC_CONTROL_VBLANK_POL_SRC		(1 << 20)
288c2ecf20Sopenharmony_ci#define XVTC_CONTROL_CHROMA_SRC			(1 << 18)
298c2ecf20Sopenharmony_ci#define XVTC_CONTROL_VBLANK_HOFF_SRC		(1 << 17)
308c2ecf20Sopenharmony_ci#define XVTC_CONTROL_VSYNC_END_SRC		(1 << 16)
318c2ecf20Sopenharmony_ci#define XVTC_CONTROL_VSYNC_START_SRC		(1 << 15)
328c2ecf20Sopenharmony_ci#define XVTC_CONTROL_ACTIVE_VSIZE_SRC		(1 << 14)
338c2ecf20Sopenharmony_ci#define XVTC_CONTROL_FRAME_VSIZE_SRC		(1 << 13)
348c2ecf20Sopenharmony_ci#define XVTC_CONTROL_HSYNC_END_SRC		(1 << 11)
358c2ecf20Sopenharmony_ci#define XVTC_CONTROL_HSYNC_START_SRC		(1 << 10)
368c2ecf20Sopenharmony_ci#define XVTC_CONTROL_ACTIVE_HSIZE_SRC		(1 << 9)
378c2ecf20Sopenharmony_ci#define XVTC_CONTROL_FRAME_HSIZE_SRC		(1 << 8)
388c2ecf20Sopenharmony_ci#define XVTC_CONTROL_SYNC_ENABLE		(1 << 5)
398c2ecf20Sopenharmony_ci#define XVTC_CONTROL_DET_ENABLE			(1 << 3)
408c2ecf20Sopenharmony_ci#define XVTC_CONTROL_GEN_ENABLE			(1 << 2)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define XVTC_STATUS_FSYNC(n)			((n) << 16)
438c2ecf20Sopenharmony_ci#define XVTC_STATUS_GEN_ACTIVE_VIDEO		(1 << 13)
448c2ecf20Sopenharmony_ci#define XVTC_STATUS_GEN_VBLANK			(1 << 12)
458c2ecf20Sopenharmony_ci#define XVTC_STATUS_DET_ACTIVE_VIDEO		(1 << 11)
468c2ecf20Sopenharmony_ci#define XVTC_STATUS_DET_VBLANK			(1 << 10)
478c2ecf20Sopenharmony_ci#define XVTC_STATUS_LOCK_LOSS			(1 << 9)
488c2ecf20Sopenharmony_ci#define XVTC_STATUS_LOCK			(1 << 8)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define XVTC_ERROR_ACTIVE_CHROMA_LOCK		(1 << 21)
518c2ecf20Sopenharmony_ci#define XVTC_ERROR_ACTIVE_VIDEO_LOCK		(1 << 20)
528c2ecf20Sopenharmony_ci#define XVTC_ERROR_HSYNC_LOCK			(1 << 19)
538c2ecf20Sopenharmony_ci#define XVTC_ERROR_VSYNC_LOCK			(1 << 18)
548c2ecf20Sopenharmony_ci#define XVTC_ERROR_HBLANK_LOCK			(1 << 17)
558c2ecf20Sopenharmony_ci#define XVTC_ERROR_VBLANK_LOCK			(1 << 16)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_FSYNC(n)		((n) << 16)
588c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_GEN_ACTIVE_VIDEO	(1 << 13)
598c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_GEN_VBLANK		(1 << 12)
608c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_DET_ACTIVE_VIDEO	(1 << 11)
618c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_DET_VBLANK		(1 << 10)
628c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_LOCK_LOSS		(1 << 9)
638c2ecf20Sopenharmony_ci#define XVTC_IRQ_ENABLE_LOCK			(1 << 8)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/*
668c2ecf20Sopenharmony_ci * The following registers exist in two blocks, one at 0x0020 for the detector
678c2ecf20Sopenharmony_ci * and one at 0x0060 for the generator.
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define XVTC_DETECTOR_OFFSET			0x0020
718c2ecf20Sopenharmony_ci#define XVTC_GENERATOR_OFFSET			0x0060
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define XVTC_ACTIVE_SIZE			0x0000
748c2ecf20Sopenharmony_ci#define XVTC_ACTIVE_VSIZE_SHIFT			16
758c2ecf20Sopenharmony_ci#define XVTC_ACTIVE_VSIZE_MASK			(0x1fff << 16)
768c2ecf20Sopenharmony_ci#define XVTC_ACTIVE_HSIZE_SHIFT			0
778c2ecf20Sopenharmony_ci#define XVTC_ACTIVE_HSIZE_MASK			(0x1fff << 0)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define XVTC_TIMING_STATUS			0x0004
808c2ecf20Sopenharmony_ci#define XVTC_TIMING_STATUS_ACTIVE_VIDEO		(1 << 2)
818c2ecf20Sopenharmony_ci#define XVTC_TIMING_STATUS_VBLANK		(1 << 1)
828c2ecf20Sopenharmony_ci#define XVTC_TIMING_STATUS_LOCKED		(1 << 0)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define XVTC_ENCODING				0x0008
858c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_SHIFT	8
868c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_MASK	(3 << 8)
878c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_EVEN_ALL	(0 << 8)
888c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_ODD_ALL	(1 << 8)
898c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_EVEN_EVEN	(2 << 8)
908c2ecf20Sopenharmony_ci#define XVTC_ENCODING_CHROMA_PARITY_ODD_EVEN	(3 << 8)
918c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_SHIFT	0
928c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_MASK		(0xf << 0)
938c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_YUV422	(0 << 0)
948c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_YUV444	(1 << 0)
958c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_RGB		(2 << 0)
968c2ecf20Sopenharmony_ci#define XVTC_ENCODING_VIDEO_FORMAT_YUV420	(3 << 0)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define XVTC_POLARITY				0x000c
998c2ecf20Sopenharmony_ci#define XVTC_POLARITY_ACTIVE_CHROMA_POL		(1 << 5)
1008c2ecf20Sopenharmony_ci#define XVTC_POLARITY_ACTIVE_VIDEO_POL		(1 << 4)
1018c2ecf20Sopenharmony_ci#define XVTC_POLARITY_HSYNC_POL			(1 << 3)
1028c2ecf20Sopenharmony_ci#define XVTC_POLARITY_VSYNC_POL			(1 << 2)
1038c2ecf20Sopenharmony_ci#define XVTC_POLARITY_HBLANK_POL		(1 << 1)
1048c2ecf20Sopenharmony_ci#define XVTC_POLARITY_VBLANK_POL		(1 << 0)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define XVTC_HSIZE				0x0010
1078c2ecf20Sopenharmony_ci#define XVTC_HSIZE_MASK				(0x1fff << 0)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define XVTC_VSIZE				0x0014
1108c2ecf20Sopenharmony_ci#define XVTC_VSIZE_MASK				(0x1fff << 0)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define XVTC_HSYNC				0x0018
1138c2ecf20Sopenharmony_ci#define XVTC_HSYNC_END_SHIFT			16
1148c2ecf20Sopenharmony_ci#define XVTC_HSYNC_END_MASK			(0x1fff << 16)
1158c2ecf20Sopenharmony_ci#define XVTC_HSYNC_START_SHIFT			0
1168c2ecf20Sopenharmony_ci#define XVTC_HSYNC_START_MASK			(0x1fff << 0)
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define XVTC_F0_VBLANK_H			0x001c
1198c2ecf20Sopenharmony_ci#define XVTC_F0_VBLANK_HEND_SHIFT		16
1208c2ecf20Sopenharmony_ci#define XVTC_F0_VBLANK_HEND_MASK		(0x1fff << 16)
1218c2ecf20Sopenharmony_ci#define XVTC_F0_VBLANK_HSTART_SHIFT		0
1228c2ecf20Sopenharmony_ci#define XVTC_F0_VBLANK_HSTART_MASK		(0x1fff << 0)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_V				0x0020
1258c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_VEND_SHIFT		16
1268c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_VEND_MASK			(0x1fff << 16)
1278c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_VSTART_SHIFT		0
1288c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_VSTART_MASK		(0x1fff << 0)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_H				0x0024
1318c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_HEND_SHIFT		16
1328c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_HEND_MASK			(0x1fff << 16)
1338c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_HSTART_SHIFT		0
1348c2ecf20Sopenharmony_ci#define XVTC_F0_VSYNC_HSTART_MASK		(0x1fff << 0)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#define XVTC_FRAME_SYNC_CONFIG(n)		(0x0100 + 4 * (n))
1378c2ecf20Sopenharmony_ci#define XVTC_FRAME_SYNC_V_START_SHIFT		16
1388c2ecf20Sopenharmony_ci#define XVTC_FRAME_SYNC_V_START_MASK		(0x1fff << 16)
1398c2ecf20Sopenharmony_ci#define XVTC_FRAME_SYNC_H_START_SHIFT		0
1408c2ecf20Sopenharmony_ci#define XVTC_FRAME_SYNC_H_START_MASK		(0x1fff << 0)
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define XVTC_GENERATOR_GLOBAL_DELAY		0x0104
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci * struct xvtc_device - Xilinx Video Timing Controller device structure
1468c2ecf20Sopenharmony_ci * @xvip: Xilinx Video IP device
1478c2ecf20Sopenharmony_ci * @list: entry in the global VTC list
1488c2ecf20Sopenharmony_ci * @has_detector: the VTC has a timing detector
1498c2ecf20Sopenharmony_ci * @has_generator: the VTC has a timing generator
1508c2ecf20Sopenharmony_ci * @config: generator timings configuration
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cistruct xvtc_device {
1538c2ecf20Sopenharmony_ci	struct xvip_device xvip;
1548c2ecf20Sopenharmony_ci	struct list_head list;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	bool has_detector;
1578c2ecf20Sopenharmony_ci	bool has_generator;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	struct xvtc_config config;
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic LIST_HEAD(xvtc_list);
1638c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(xvtc_lock);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline void xvtc_gen_write(struct xvtc_device *xvtc, u32 addr, u32 value)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	xvip_write(&xvtc->xvip, XVTC_GENERATOR_OFFSET + addr, value);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
1718c2ecf20Sopenharmony_ci * Generator Operations
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ciint xvtc_generator_start(struct xvtc_device *xvtc,
1758c2ecf20Sopenharmony_ci			 const struct xvtc_config *config)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	int ret;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (!xvtc->has_generator)
1808c2ecf20Sopenharmony_ci		return -ENXIO;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(xvtc->xvip.clk);
1838c2ecf20Sopenharmony_ci	if (ret < 0)
1848c2ecf20Sopenharmony_ci		return ret;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* We don't care about the chroma active signal, encoding parameters are
1878c2ecf20Sopenharmony_ci	 * not important for now.
1888c2ecf20Sopenharmony_ci	 */
1898c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_POLARITY,
1908c2ecf20Sopenharmony_ci		       XVTC_POLARITY_ACTIVE_CHROMA_POL |
1918c2ecf20Sopenharmony_ci		       XVTC_POLARITY_ACTIVE_VIDEO_POL |
1928c2ecf20Sopenharmony_ci		       XVTC_POLARITY_HSYNC_POL | XVTC_POLARITY_VSYNC_POL |
1938c2ecf20Sopenharmony_ci		       XVTC_POLARITY_HBLANK_POL | XVTC_POLARITY_VBLANK_POL);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/* Hardcode the polarity to active high, as required by the video in to
1968c2ecf20Sopenharmony_ci	 * AXI4-stream core.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_ENCODING, 0);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Configure the timings. The VBLANK and VSYNC signals assertion and
2018c2ecf20Sopenharmony_ci	 * deassertion are hardcoded to the first pixel of the line.
2028c2ecf20Sopenharmony_ci	 */
2038c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_ACTIVE_SIZE,
2048c2ecf20Sopenharmony_ci		       (config->vblank_start << XVTC_ACTIVE_VSIZE_SHIFT) |
2058c2ecf20Sopenharmony_ci		       (config->hblank_start << XVTC_ACTIVE_HSIZE_SHIFT));
2068c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_HSIZE, config->hsize);
2078c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_VSIZE, config->vsize);
2088c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_HSYNC,
2098c2ecf20Sopenharmony_ci		       (config->hsync_end << XVTC_HSYNC_END_SHIFT) |
2108c2ecf20Sopenharmony_ci		       (config->hsync_start << XVTC_HSYNC_START_SHIFT));
2118c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_F0_VBLANK_H, 0);
2128c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_F0_VSYNC_V,
2138c2ecf20Sopenharmony_ci		       (config->vsync_end << XVTC_F0_VSYNC_VEND_SHIFT) |
2148c2ecf20Sopenharmony_ci		       (config->vsync_start << XVTC_F0_VSYNC_VSTART_SHIFT));
2158c2ecf20Sopenharmony_ci	xvtc_gen_write(xvtc, XVTC_F0_VSYNC_H, 0);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Enable the generator. Set the source of all generator parameters to
2188c2ecf20Sopenharmony_ci	 * generator registers.
2198c2ecf20Sopenharmony_ci	 */
2208c2ecf20Sopenharmony_ci	xvip_write(&xvtc->xvip, XVIP_CTRL_CONTROL,
2218c2ecf20Sopenharmony_ci		   XVTC_CONTROL_ACTIVE_CHROMA_POL_SRC |
2228c2ecf20Sopenharmony_ci		   XVTC_CONTROL_ACTIVE_VIDEO_POL_SRC |
2238c2ecf20Sopenharmony_ci		   XVTC_CONTROL_HSYNC_POL_SRC | XVTC_CONTROL_VSYNC_POL_SRC |
2248c2ecf20Sopenharmony_ci		   XVTC_CONTROL_HBLANK_POL_SRC | XVTC_CONTROL_VBLANK_POL_SRC |
2258c2ecf20Sopenharmony_ci		   XVTC_CONTROL_CHROMA_SRC | XVTC_CONTROL_VBLANK_HOFF_SRC |
2268c2ecf20Sopenharmony_ci		   XVTC_CONTROL_VSYNC_END_SRC | XVTC_CONTROL_VSYNC_START_SRC |
2278c2ecf20Sopenharmony_ci		   XVTC_CONTROL_ACTIVE_VSIZE_SRC |
2288c2ecf20Sopenharmony_ci		   XVTC_CONTROL_FRAME_VSIZE_SRC | XVTC_CONTROL_HSYNC_END_SRC |
2298c2ecf20Sopenharmony_ci		   XVTC_CONTROL_HSYNC_START_SRC |
2308c2ecf20Sopenharmony_ci		   XVTC_CONTROL_ACTIVE_HSIZE_SRC |
2318c2ecf20Sopenharmony_ci		   XVTC_CONTROL_FRAME_HSIZE_SRC | XVTC_CONTROL_GEN_ENABLE |
2328c2ecf20Sopenharmony_ci		   XVIP_CTRL_CONTROL_REG_UPDATE);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	return 0;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xvtc_generator_start);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ciint xvtc_generator_stop(struct xvtc_device *xvtc)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	if (!xvtc->has_generator)
2418c2ecf20Sopenharmony_ci		return -ENXIO;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	xvip_write(&xvtc->xvip, XVIP_CTRL_CONTROL, 0);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	clk_disable_unprepare(xvtc->xvip.clk);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	return 0;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xvtc_generator_stop);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistruct xvtc_device *xvtc_of_get(struct device_node *np)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct device_node *xvtc_node;
2548c2ecf20Sopenharmony_ci	struct xvtc_device *found = NULL;
2558c2ecf20Sopenharmony_ci	struct xvtc_device *xvtc;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (!of_find_property(np, "xlnx,vtc", NULL))
2588c2ecf20Sopenharmony_ci		return NULL;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	xvtc_node = of_parse_phandle(np, "xlnx,vtc", 0);
2618c2ecf20Sopenharmony_ci	if (xvtc_node == NULL)
2628c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	mutex_lock(&xvtc_lock);
2658c2ecf20Sopenharmony_ci	list_for_each_entry(xvtc, &xvtc_list, list) {
2668c2ecf20Sopenharmony_ci		if (xvtc->xvip.dev->of_node == xvtc_node) {
2678c2ecf20Sopenharmony_ci			found = xvtc;
2688c2ecf20Sopenharmony_ci			break;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	mutex_unlock(&xvtc_lock);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	of_node_put(xvtc_node);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (!found)
2768c2ecf20Sopenharmony_ci		return ERR_PTR(-EPROBE_DEFER);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return found;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xvtc_of_get);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_civoid xvtc_put(struct xvtc_device *xvtc)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xvtc_put);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
2888c2ecf20Sopenharmony_ci * Registration and Unregistration
2898c2ecf20Sopenharmony_ci */
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic void xvtc_register_device(struct xvtc_device *xvtc)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	mutex_lock(&xvtc_lock);
2948c2ecf20Sopenharmony_ci	list_add_tail(&xvtc->list, &xvtc_list);
2958c2ecf20Sopenharmony_ci	mutex_unlock(&xvtc_lock);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic void xvtc_unregister_device(struct xvtc_device *xvtc)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	mutex_lock(&xvtc_lock);
3018c2ecf20Sopenharmony_ci	list_del(&xvtc->list);
3028c2ecf20Sopenharmony_ci	mutex_unlock(&xvtc_lock);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
3068c2ecf20Sopenharmony_ci * Platform Device Driver
3078c2ecf20Sopenharmony_ci */
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic int xvtc_parse_of(struct xvtc_device *xvtc)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct device_node *node = xvtc->xvip.dev->of_node;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	xvtc->has_detector = of_property_read_bool(node, "xlnx,detector");
3148c2ecf20Sopenharmony_ci	xvtc->has_generator = of_property_read_bool(node, "xlnx,generator");
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	return 0;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int xvtc_probe(struct platform_device *pdev)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct xvtc_device *xvtc;
3228c2ecf20Sopenharmony_ci	int ret;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	xvtc = devm_kzalloc(&pdev->dev, sizeof(*xvtc), GFP_KERNEL);
3258c2ecf20Sopenharmony_ci	if (!xvtc)
3268c2ecf20Sopenharmony_ci		return -ENOMEM;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	xvtc->xvip.dev = &pdev->dev;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	ret = xvtc_parse_of(xvtc);
3318c2ecf20Sopenharmony_ci	if (ret < 0)
3328c2ecf20Sopenharmony_ci		return ret;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	ret = xvip_init_resources(&xvtc->xvip);
3358c2ecf20Sopenharmony_ci	if (ret < 0)
3368c2ecf20Sopenharmony_ci		return ret;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, xvtc);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	xvip_print_version(&xvtc->xvip);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	xvtc_register_device(xvtc);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int xvtc_remove(struct platform_device *pdev)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct xvtc_device *xvtc = platform_get_drvdata(pdev);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	xvtc_unregister_device(xvtc);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	xvip_cleanup_resources(&xvtc->xvip);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	return 0;
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic const struct of_device_id xvtc_of_id_table[] = {
3598c2ecf20Sopenharmony_ci	{ .compatible = "xlnx,v-tc-6.1" },
3608c2ecf20Sopenharmony_ci	{ }
3618c2ecf20Sopenharmony_ci};
3628c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, xvtc_of_id_table);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic struct platform_driver xvtc_driver = {
3658c2ecf20Sopenharmony_ci	.driver = {
3668c2ecf20Sopenharmony_ci		.name = "xilinx-vtc",
3678c2ecf20Sopenharmony_ci		.of_match_table = xvtc_of_id_table,
3688c2ecf20Sopenharmony_ci	},
3698c2ecf20Sopenharmony_ci	.probe = xvtc_probe,
3708c2ecf20Sopenharmony_ci	.remove = xvtc_remove,
3718c2ecf20Sopenharmony_ci};
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cimodule_platform_driver(xvtc_driver);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
3768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Xilinx Video Timing Controller Driver");
3778c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
378