18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Broadcom
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/**
78c2ecf20Sopenharmony_ci * DOC: VC4 SDTV module
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * The VEC encoder generates PAL or NTSC composite video output.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * TV mode selection is done by an atomic property on the encoder,
128c2ecf20Sopenharmony_ci * because a drm_mode_modeinfo is insufficient to distinguish between
138c2ecf20Sopenharmony_ci * PAL and PAL-M or NTSC and NTSC-J.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h>
178c2ecf20Sopenharmony_ci#include <drm/drm_edid.h>
188c2ecf20Sopenharmony_ci#include <drm/drm_panel.h>
198c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h>
208c2ecf20Sopenharmony_ci#include <drm/drm_simple_kms_helper.h>
218c2ecf20Sopenharmony_ci#include <linux/clk.h>
228c2ecf20Sopenharmony_ci#include <linux/component.h>
238c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
248c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
258c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "vc4_drv.h"
288c2ecf20Sopenharmony_ci#include "vc4_regs.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* WSE Registers */
318c2ecf20Sopenharmony_ci#define VEC_WSE_RESET			0xc0
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define VEC_WSE_CONTROL			0xc4
348c2ecf20Sopenharmony_ci#define VEC_WSE_WSS_ENABLE		BIT(7)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define VEC_WSE_WSS_DATA		0xc8
378c2ecf20Sopenharmony_ci#define VEC_WSE_VPS_DATA1		0xcc
388c2ecf20Sopenharmony_ci#define VEC_WSE_VPS_CONTROL		0xd0
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* VEC Registers */
418c2ecf20Sopenharmony_ci#define VEC_REVID			0x100
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define VEC_CONFIG0			0x104
448c2ecf20Sopenharmony_ci#define VEC_CONFIG0_YDEL_MASK		GENMASK(28, 26)
458c2ecf20Sopenharmony_ci#define VEC_CONFIG0_YDEL(x)		((x) << 26)
468c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CDEL_MASK		GENMASK(25, 24)
478c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CDEL(x)		((x) << 24)
488c2ecf20Sopenharmony_ci#define VEC_CONFIG0_PBPR_FIL		BIT(18)
498c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHROMA_GAIN_MASK	GENMASK(17, 16)
508c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHROMA_GAIN_UNITY	(0 << 16)
518c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHROMA_GAIN_1_32	(1 << 16)
528c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHROMA_GAIN_1_16	(2 << 16)
538c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHROMA_GAIN_1_8	(3 << 16)
548c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CBURST_GAIN_MASK	GENMASK(14, 13)
558c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CBURST_GAIN_UNITY	(0 << 13)
568c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CBURST_GAIN_1_128	(1 << 13)
578c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CBURST_GAIN_1_64	(2 << 13)
588c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CBURST_GAIN_1_32	(3 << 13)
598c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHRBW1		BIT(11)
608c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHRBW0		BIT(10)
618c2ecf20Sopenharmony_ci#define VEC_CONFIG0_SYNCDIS		BIT(9)
628c2ecf20Sopenharmony_ci#define VEC_CONFIG0_BURDIS		BIT(8)
638c2ecf20Sopenharmony_ci#define VEC_CONFIG0_CHRDIS		BIT(7)
648c2ecf20Sopenharmony_ci#define VEC_CONFIG0_PDEN		BIT(6)
658c2ecf20Sopenharmony_ci#define VEC_CONFIG0_YCDELAY		BIT(4)
668c2ecf20Sopenharmony_ci#define VEC_CONFIG0_RAMPEN		BIT(2)
678c2ecf20Sopenharmony_ci#define VEC_CONFIG0_YCDIS		BIT(2)
688c2ecf20Sopenharmony_ci#define VEC_CONFIG0_STD_MASK		GENMASK(1, 0)
698c2ecf20Sopenharmony_ci#define VEC_CONFIG0_NTSC_STD		0
708c2ecf20Sopenharmony_ci#define VEC_CONFIG0_PAL_BDGHI_STD	1
718c2ecf20Sopenharmony_ci#define VEC_CONFIG0_PAL_N_STD		3
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define VEC_SCHPH			0x108
748c2ecf20Sopenharmony_ci#define VEC_SOFT_RESET			0x10c
758c2ecf20Sopenharmony_ci#define VEC_CLMP0_START			0x144
768c2ecf20Sopenharmony_ci#define VEC_CLMP0_END			0x148
778c2ecf20Sopenharmony_ci#define VEC_FREQ3_2			0x180
788c2ecf20Sopenharmony_ci#define VEC_FREQ1_0			0x184
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define VEC_CONFIG1			0x188
818c2ecf20Sopenharmony_ci#define VEC_CONFIG_VEC_RESYNC_OFF	BIT(18)
828c2ecf20Sopenharmony_ci#define VEC_CONFIG_RGB219		BIT(17)
838c2ecf20Sopenharmony_ci#define VEC_CONFIG_CBAR_EN		BIT(16)
848c2ecf20Sopenharmony_ci#define VEC_CONFIG_TC_OBB		BIT(15)
858c2ecf20Sopenharmony_ci#define VEC_CONFIG1_OUTPUT_MODE_MASK	GENMASK(12, 10)
868c2ecf20Sopenharmony_ci#define VEC_CONFIG1_C_Y_CVBS		(0 << 10)
878c2ecf20Sopenharmony_ci#define VEC_CONFIG1_CVBS_Y_C		(1 << 10)
888c2ecf20Sopenharmony_ci#define VEC_CONFIG1_PR_Y_PB		(2 << 10)
898c2ecf20Sopenharmony_ci#define VEC_CONFIG1_RGB			(4 << 10)
908c2ecf20Sopenharmony_ci#define VEC_CONFIG1_Y_C_CVBS		(5 << 10)
918c2ecf20Sopenharmony_ci#define VEC_CONFIG1_C_CVBS_Y		(6 << 10)
928c2ecf20Sopenharmony_ci#define VEC_CONFIG1_C_CVBS_CVBS		(7 << 10)
938c2ecf20Sopenharmony_ci#define VEC_CONFIG1_DIS_CHR		BIT(9)
948c2ecf20Sopenharmony_ci#define VEC_CONFIG1_DIS_LUMA		BIT(8)
958c2ecf20Sopenharmony_ci#define VEC_CONFIG1_YCBCR_IN		BIT(6)
968c2ecf20Sopenharmony_ci#define VEC_CONFIG1_DITHER_TYPE_LFSR	0
978c2ecf20Sopenharmony_ci#define VEC_CONFIG1_DITHER_TYPE_COUNTER	BIT(5)
988c2ecf20Sopenharmony_ci#define VEC_CONFIG1_DITHER_EN		BIT(4)
998c2ecf20Sopenharmony_ci#define VEC_CONFIG1_CYDELAY		BIT(3)
1008c2ecf20Sopenharmony_ci#define VEC_CONFIG1_LUMADIS		BIT(2)
1018c2ecf20Sopenharmony_ci#define VEC_CONFIG1_COMPDIS		BIT(1)
1028c2ecf20Sopenharmony_ci#define VEC_CONFIG1_CUSTOM_FREQ		BIT(0)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define VEC_CONFIG2			0x18c
1058c2ecf20Sopenharmony_ci#define VEC_CONFIG2_PROG_SCAN		BIT(15)
1068c2ecf20Sopenharmony_ci#define VEC_CONFIG2_SYNC_ADJ_MASK	GENMASK(14, 12)
1078c2ecf20Sopenharmony_ci#define VEC_CONFIG2_SYNC_ADJ(x)		(((x) / 2) << 12)
1088c2ecf20Sopenharmony_ci#define VEC_CONFIG2_PBPR_EN		BIT(10)
1098c2ecf20Sopenharmony_ci#define VEC_CONFIG2_UV_DIG_DIS		BIT(6)
1108c2ecf20Sopenharmony_ci#define VEC_CONFIG2_RGB_DIG_DIS		BIT(5)
1118c2ecf20Sopenharmony_ci#define VEC_CONFIG2_TMUX_MASK		GENMASK(3, 2)
1128c2ecf20Sopenharmony_ci#define VEC_CONFIG2_TMUX_DRIVE0		(0 << 2)
1138c2ecf20Sopenharmony_ci#define VEC_CONFIG2_TMUX_RG_COMP	(1 << 2)
1148c2ecf20Sopenharmony_ci#define VEC_CONFIG2_TMUX_UV_YC		(2 << 2)
1158c2ecf20Sopenharmony_ci#define VEC_CONFIG2_TMUX_SYNC_YC	(3 << 2)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define VEC_INTERRUPT_CONTROL		0x190
1188c2ecf20Sopenharmony_ci#define VEC_INTERRUPT_STATUS		0x194
1198c2ecf20Sopenharmony_ci#define VEC_FCW_SECAM_B			0x198
1208c2ecf20Sopenharmony_ci#define VEC_SECAM_GAIN_VAL		0x19c
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define VEC_CONFIG3			0x1a0
1238c2ecf20Sopenharmony_ci#define VEC_CONFIG3_HORIZ_LEN_STD	(0 << 0)
1248c2ecf20Sopenharmony_ci#define VEC_CONFIG3_HORIZ_LEN_MPEG1_SIF	(1 << 0)
1258c2ecf20Sopenharmony_ci#define VEC_CONFIG3_SHAPE_NON_LINEAR	BIT(1)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define VEC_STATUS0			0x200
1288c2ecf20Sopenharmony_ci#define VEC_MASK0			0x204
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define VEC_CFG				0x208
1318c2ecf20Sopenharmony_ci#define VEC_CFG_SG_MODE_MASK		GENMASK(6, 5)
1328c2ecf20Sopenharmony_ci#define VEC_CFG_SG_MODE(x)		((x) << 5)
1338c2ecf20Sopenharmony_ci#define VEC_CFG_SG_EN			BIT(4)
1348c2ecf20Sopenharmony_ci#define VEC_CFG_VEC_EN			BIT(3)
1358c2ecf20Sopenharmony_ci#define VEC_CFG_MB_EN			BIT(2)
1368c2ecf20Sopenharmony_ci#define VEC_CFG_ENABLE			BIT(1)
1378c2ecf20Sopenharmony_ci#define VEC_CFG_TB_EN			BIT(0)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define VEC_DAC_TEST			0x20c
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define VEC_DAC_CONFIG			0x210
1428c2ecf20Sopenharmony_ci#define VEC_DAC_CONFIG_LDO_BIAS_CTRL(x)	((x) << 24)
1438c2ecf20Sopenharmony_ci#define VEC_DAC_CONFIG_DRIVER_CTRL(x)	((x) << 16)
1448c2ecf20Sopenharmony_ci#define VEC_DAC_CONFIG_DAC_CTRL(x)	(x)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#define VEC_DAC_MISC			0x214
1478c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_VCD_CTRL_MASK	GENMASK(31, 16)
1488c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_VCD_CTRL(x)	((x) << 16)
1498c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_VID_ACT		BIT(8)
1508c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_VCD_PWRDN		BIT(6)
1518c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_BIAS_PWRDN		BIT(5)
1528c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_DAC_PWRDN		BIT(2)
1538c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_LDO_PWRDN		BIT(1)
1548c2ecf20Sopenharmony_ci#define VEC_DAC_MISC_DAC_RST_N		BIT(0)
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* General VEC hardware state. */
1588c2ecf20Sopenharmony_cistruct vc4_vec {
1598c2ecf20Sopenharmony_ci	struct platform_device *pdev;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	struct drm_encoder *encoder;
1628c2ecf20Sopenharmony_ci	struct drm_connector *connector;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	void __iomem *regs;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	struct clk *clock;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	const struct vc4_vec_tv_mode *tv_mode;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	struct debugfs_regset32 regset;
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define VEC_READ(offset) readl(vec->regs + (offset))
1748c2ecf20Sopenharmony_ci#define VEC_WRITE(offset, val) writel(val, vec->regs + (offset))
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/* VC4 VEC encoder KMS struct */
1778c2ecf20Sopenharmony_cistruct vc4_vec_encoder {
1788c2ecf20Sopenharmony_ci	struct vc4_encoder base;
1798c2ecf20Sopenharmony_ci	struct vc4_vec *vec;
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline struct vc4_vec_encoder *
1838c2ecf20Sopenharmony_cito_vc4_vec_encoder(struct drm_encoder *encoder)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	return container_of(encoder, struct vc4_vec_encoder, base.base);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* VC4 VEC connector KMS struct */
1898c2ecf20Sopenharmony_cistruct vc4_vec_connector {
1908c2ecf20Sopenharmony_ci	struct drm_connector base;
1918c2ecf20Sopenharmony_ci	struct vc4_vec *vec;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Since the connector is attached to just the one encoder,
1948c2ecf20Sopenharmony_ci	 * this is the reference to it so we can do the best_encoder()
1958c2ecf20Sopenharmony_ci	 * hook.
1968c2ecf20Sopenharmony_ci	 */
1978c2ecf20Sopenharmony_ci	struct drm_encoder *encoder;
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic inline struct vc4_vec_connector *
2018c2ecf20Sopenharmony_cito_vc4_vec_connector(struct drm_connector *connector)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	return container_of(connector, struct vc4_vec_connector, base);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cienum vc4_vec_tv_mode_id {
2078c2ecf20Sopenharmony_ci	VC4_VEC_TV_MODE_NTSC,
2088c2ecf20Sopenharmony_ci	VC4_VEC_TV_MODE_NTSC_J,
2098c2ecf20Sopenharmony_ci	VC4_VEC_TV_MODE_PAL,
2108c2ecf20Sopenharmony_ci	VC4_VEC_TV_MODE_PAL_M,
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistruct vc4_vec_tv_mode {
2148c2ecf20Sopenharmony_ci	const struct drm_display_mode *mode;
2158c2ecf20Sopenharmony_ci	void (*mode_set)(struct vc4_vec *vec);
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 vec_regs[] = {
2198c2ecf20Sopenharmony_ci	VC4_REG32(VEC_WSE_CONTROL),
2208c2ecf20Sopenharmony_ci	VC4_REG32(VEC_WSE_WSS_DATA),
2218c2ecf20Sopenharmony_ci	VC4_REG32(VEC_WSE_VPS_DATA1),
2228c2ecf20Sopenharmony_ci	VC4_REG32(VEC_WSE_VPS_CONTROL),
2238c2ecf20Sopenharmony_ci	VC4_REG32(VEC_REVID),
2248c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CONFIG0),
2258c2ecf20Sopenharmony_ci	VC4_REG32(VEC_SCHPH),
2268c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CLMP0_START),
2278c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CLMP0_END),
2288c2ecf20Sopenharmony_ci	VC4_REG32(VEC_FREQ3_2),
2298c2ecf20Sopenharmony_ci	VC4_REG32(VEC_FREQ1_0),
2308c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CONFIG1),
2318c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CONFIG2),
2328c2ecf20Sopenharmony_ci	VC4_REG32(VEC_INTERRUPT_CONTROL),
2338c2ecf20Sopenharmony_ci	VC4_REG32(VEC_INTERRUPT_STATUS),
2348c2ecf20Sopenharmony_ci	VC4_REG32(VEC_FCW_SECAM_B),
2358c2ecf20Sopenharmony_ci	VC4_REG32(VEC_SECAM_GAIN_VAL),
2368c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CONFIG3),
2378c2ecf20Sopenharmony_ci	VC4_REG32(VEC_STATUS0),
2388c2ecf20Sopenharmony_ci	VC4_REG32(VEC_MASK0),
2398c2ecf20Sopenharmony_ci	VC4_REG32(VEC_CFG),
2408c2ecf20Sopenharmony_ci	VC4_REG32(VEC_DAC_TEST),
2418c2ecf20Sopenharmony_ci	VC4_REG32(VEC_DAC_CONFIG),
2428c2ecf20Sopenharmony_ci	VC4_REG32(VEC_DAC_MISC),
2438c2ecf20Sopenharmony_ci};
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void vc4_vec_ntsc_mode_set(struct vc4_vec *vec)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG0, VEC_CONFIG0_NTSC_STD | VEC_CONFIG0_PDEN);
2488c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG1, VEC_CONFIG1_C_CVBS_CVBS);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic void vc4_vec_ntsc_j_mode_set(struct vc4_vec *vec)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG0, VEC_CONFIG0_NTSC_STD);
2548c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG1, VEC_CONFIG1_C_CVBS_CVBS);
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic const struct drm_display_mode ntsc_mode = {
2588c2ecf20Sopenharmony_ci	DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 13500,
2598c2ecf20Sopenharmony_ci		 720, 720 + 14, 720 + 14 + 64, 720 + 14 + 64 + 60, 0,
2608c2ecf20Sopenharmony_ci		 480, 480 + 7, 480 + 7 + 6, 525, 0,
2618c2ecf20Sopenharmony_ci		 DRM_MODE_FLAG_INTERLACE)
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic void vc4_vec_pal_mode_set(struct vc4_vec *vec)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG0, VEC_CONFIG0_PAL_BDGHI_STD);
2678c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG1, VEC_CONFIG1_C_CVBS_CVBS);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic void vc4_vec_pal_m_mode_set(struct vc4_vec *vec)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG0, VEC_CONFIG0_PAL_BDGHI_STD);
2738c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG1,
2748c2ecf20Sopenharmony_ci		  VEC_CONFIG1_C_CVBS_CVBS | VEC_CONFIG1_CUSTOM_FREQ);
2758c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_FREQ3_2, 0x223b);
2768c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_FREQ1_0, 0x61d1);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic const struct drm_display_mode pal_mode = {
2808c2ecf20Sopenharmony_ci	DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 13500,
2818c2ecf20Sopenharmony_ci		 720, 720 + 20, 720 + 20 + 64, 720 + 20 + 64 + 60, 0,
2828c2ecf20Sopenharmony_ci		 576, 576 + 4, 576 + 4 + 6, 625, 0,
2838c2ecf20Sopenharmony_ci		 DRM_MODE_FLAG_INTERLACE)
2848c2ecf20Sopenharmony_ci};
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic const struct vc4_vec_tv_mode vc4_vec_tv_modes[] = {
2878c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_NTSC] = {
2888c2ecf20Sopenharmony_ci		.mode = &ntsc_mode,
2898c2ecf20Sopenharmony_ci		.mode_set = vc4_vec_ntsc_mode_set,
2908c2ecf20Sopenharmony_ci	},
2918c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_NTSC_J] = {
2928c2ecf20Sopenharmony_ci		.mode = &ntsc_mode,
2938c2ecf20Sopenharmony_ci		.mode_set = vc4_vec_ntsc_j_mode_set,
2948c2ecf20Sopenharmony_ci	},
2958c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_PAL] = {
2968c2ecf20Sopenharmony_ci		.mode = &pal_mode,
2978c2ecf20Sopenharmony_ci		.mode_set = vc4_vec_pal_mode_set,
2988c2ecf20Sopenharmony_ci	},
2998c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_PAL_M] = {
3008c2ecf20Sopenharmony_ci		.mode = &pal_mode,
3018c2ecf20Sopenharmony_ci		.mode_set = vc4_vec_pal_m_mode_set,
3028c2ecf20Sopenharmony_ci	},
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic enum drm_connector_status
3068c2ecf20Sopenharmony_civc4_vec_connector_detect(struct drm_connector *connector, bool force)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	return connector_status_unknown;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic void vc4_vec_connector_destroy(struct drm_connector *connector)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	drm_connector_unregister(connector);
3148c2ecf20Sopenharmony_ci	drm_connector_cleanup(connector);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int vc4_vec_connector_get_modes(struct drm_connector *connector)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct drm_connector_state *state = connector->state;
3208c2ecf20Sopenharmony_ci	struct drm_display_mode *mode;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	mode = drm_mode_duplicate(connector->dev,
3238c2ecf20Sopenharmony_ci				  vc4_vec_tv_modes[state->tv.mode].mode);
3248c2ecf20Sopenharmony_ci	if (!mode) {
3258c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to create a new display mode\n");
3268c2ecf20Sopenharmony_ci		return -ENOMEM;
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	drm_mode_probed_add(connector, mode);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	return 1;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic const struct drm_connector_funcs vc4_vec_connector_funcs = {
3358c2ecf20Sopenharmony_ci	.detect = vc4_vec_connector_detect,
3368c2ecf20Sopenharmony_ci	.fill_modes = drm_helper_probe_single_connector_modes,
3378c2ecf20Sopenharmony_ci	.destroy = vc4_vec_connector_destroy,
3388c2ecf20Sopenharmony_ci	.reset = drm_atomic_helper_connector_reset,
3398c2ecf20Sopenharmony_ci	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
3408c2ecf20Sopenharmony_ci	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic const struct drm_connector_helper_funcs vc4_vec_connector_helper_funcs = {
3448c2ecf20Sopenharmony_ci	.get_modes = vc4_vec_connector_get_modes,
3458c2ecf20Sopenharmony_ci};
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic struct drm_connector *vc4_vec_connector_init(struct drm_device *dev,
3488c2ecf20Sopenharmony_ci						    struct vc4_vec *vec)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	struct drm_connector *connector = NULL;
3518c2ecf20Sopenharmony_ci	struct vc4_vec_connector *vec_connector;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	vec_connector = devm_kzalloc(dev->dev, sizeof(*vec_connector),
3548c2ecf20Sopenharmony_ci				     GFP_KERNEL);
3558c2ecf20Sopenharmony_ci	if (!vec_connector)
3568c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	connector = &vec_connector->base;
3598c2ecf20Sopenharmony_ci	connector->interlace_allowed = true;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	vec_connector->encoder = vec->encoder;
3628c2ecf20Sopenharmony_ci	vec_connector->vec = vec;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	drm_connector_init(dev, connector, &vc4_vec_connector_funcs,
3658c2ecf20Sopenharmony_ci			   DRM_MODE_CONNECTOR_Composite);
3668c2ecf20Sopenharmony_ci	drm_connector_helper_add(connector, &vc4_vec_connector_helper_funcs);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	drm_object_attach_property(&connector->base,
3698c2ecf20Sopenharmony_ci				   dev->mode_config.tv_mode_property,
3708c2ecf20Sopenharmony_ci				   VC4_VEC_TV_MODE_NTSC);
3718c2ecf20Sopenharmony_ci	vec->tv_mode = &vc4_vec_tv_modes[VC4_VEC_TV_MODE_NTSC];
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	drm_connector_attach_encoder(connector, vec->encoder);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return connector;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic void vc4_vec_encoder_disable(struct drm_encoder *encoder)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct vc4_vec_encoder *vc4_vec_encoder = to_vc4_vec_encoder(encoder);
3818c2ecf20Sopenharmony_ci	struct vc4_vec *vec = vc4_vec_encoder->vec;
3828c2ecf20Sopenharmony_ci	int ret;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CFG, 0);
3858c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_DAC_MISC,
3868c2ecf20Sopenharmony_ci		  VEC_DAC_MISC_VCD_PWRDN |
3878c2ecf20Sopenharmony_ci		  VEC_DAC_MISC_BIAS_PWRDN |
3888c2ecf20Sopenharmony_ci		  VEC_DAC_MISC_DAC_PWRDN |
3898c2ecf20Sopenharmony_ci		  VEC_DAC_MISC_LDO_PWRDN);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	clk_disable_unprepare(vec->clock);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	ret = pm_runtime_put(&vec->pdev->dev);
3948c2ecf20Sopenharmony_ci	if (ret < 0) {
3958c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to release power domain: %d\n", ret);
3968c2ecf20Sopenharmony_ci		return;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic void vc4_vec_encoder_enable(struct drm_encoder *encoder)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct vc4_vec_encoder *vc4_vec_encoder = to_vc4_vec_encoder(encoder);
4038c2ecf20Sopenharmony_ci	struct vc4_vec *vec = vc4_vec_encoder->vec;
4048c2ecf20Sopenharmony_ci	int ret;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(&vec->pdev->dev);
4078c2ecf20Sopenharmony_ci	if (ret < 0) {
4088c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to retain power domain: %d\n", ret);
4098c2ecf20Sopenharmony_ci		return;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	/*
4138c2ecf20Sopenharmony_ci	 * We need to set the clock rate each time we enable the encoder
4148c2ecf20Sopenharmony_ci	 * because there's a chance we share the same parent with the HDMI
4158c2ecf20Sopenharmony_ci	 * clock, and both drivers are requesting different rates.
4168c2ecf20Sopenharmony_ci	 * The good news is, these 2 encoders cannot be enabled at the same
4178c2ecf20Sopenharmony_ci	 * time, thus preventing incompatible rate requests.
4188c2ecf20Sopenharmony_ci	 */
4198c2ecf20Sopenharmony_ci	ret = clk_set_rate(vec->clock, 108000000);
4208c2ecf20Sopenharmony_ci	if (ret) {
4218c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to set clock rate: %d\n", ret);
4228c2ecf20Sopenharmony_ci		return;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(vec->clock);
4268c2ecf20Sopenharmony_ci	if (ret) {
4278c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to turn on core clock: %d\n", ret);
4288c2ecf20Sopenharmony_ci		return;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* Reset the different blocks */
4328c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_WSE_RESET, 1);
4338c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_SOFT_RESET, 1);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	/* Disable the CGSM-A and WSE blocks */
4368c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_WSE_CONTROL, 0);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Write config common to all modes. */
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	/*
4418c2ecf20Sopenharmony_ci	 * Color subcarrier phase: phase = 360 * SCHPH / 256.
4428c2ecf20Sopenharmony_ci	 * 0x28 <=> 39.375 deg.
4438c2ecf20Sopenharmony_ci	 */
4448c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_SCHPH, 0x28);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/*
4478c2ecf20Sopenharmony_ci	 * Reset to default values.
4488c2ecf20Sopenharmony_ci	 */
4498c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CLMP0_START, 0xac);
4508c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CLMP0_END, 0xec);
4518c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG2,
4528c2ecf20Sopenharmony_ci		  VEC_CONFIG2_UV_DIG_DIS | VEC_CONFIG2_RGB_DIG_DIS);
4538c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CONFIG3, VEC_CONFIG3_HORIZ_LEN_STD);
4548c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_DAC_CONFIG,
4558c2ecf20Sopenharmony_ci		  VEC_DAC_CONFIG_DAC_CTRL(0xc) |
4568c2ecf20Sopenharmony_ci		  VEC_DAC_CONFIG_DRIVER_CTRL(0xc) |
4578c2ecf20Sopenharmony_ci		  VEC_DAC_CONFIG_LDO_BIAS_CTRL(0x46));
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* Mask all interrupts. */
4608c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_MASK0, 0);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	vec->tv_mode->mode_set(vec);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_DAC_MISC,
4658c2ecf20Sopenharmony_ci		  VEC_DAC_MISC_VID_ACT | VEC_DAC_MISC_DAC_RST_N);
4668c2ecf20Sopenharmony_ci	VEC_WRITE(VEC_CFG, VEC_CFG_VEC_EN);
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic bool vc4_vec_encoder_mode_fixup(struct drm_encoder *encoder,
4718c2ecf20Sopenharmony_ci				       const struct drm_display_mode *mode,
4728c2ecf20Sopenharmony_ci				       struct drm_display_mode *adjusted_mode)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	return true;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic void vc4_vec_encoder_atomic_mode_set(struct drm_encoder *encoder,
4788c2ecf20Sopenharmony_ci					struct drm_crtc_state *crtc_state,
4798c2ecf20Sopenharmony_ci					struct drm_connector_state *conn_state)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	struct vc4_vec_encoder *vc4_vec_encoder = to_vc4_vec_encoder(encoder);
4828c2ecf20Sopenharmony_ci	struct vc4_vec *vec = vc4_vec_encoder->vec;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	vec->tv_mode = &vc4_vec_tv_modes[conn_state->tv.mode];
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int vc4_vec_encoder_atomic_check(struct drm_encoder *encoder,
4888c2ecf20Sopenharmony_ci					struct drm_crtc_state *crtc_state,
4898c2ecf20Sopenharmony_ci					struct drm_connector_state *conn_state)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	const struct vc4_vec_tv_mode *vec_mode;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	vec_mode = &vc4_vec_tv_modes[conn_state->tv.mode];
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (conn_state->crtc &&
4968c2ecf20Sopenharmony_ci	    !drm_mode_equal(vec_mode->mode, &crtc_state->adjusted_mode))
4978c2ecf20Sopenharmony_ci		return -EINVAL;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	return 0;
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic const struct drm_encoder_helper_funcs vc4_vec_encoder_helper_funcs = {
5038c2ecf20Sopenharmony_ci	.disable = vc4_vec_encoder_disable,
5048c2ecf20Sopenharmony_ci	.enable = vc4_vec_encoder_enable,
5058c2ecf20Sopenharmony_ci	.mode_fixup = vc4_vec_encoder_mode_fixup,
5068c2ecf20Sopenharmony_ci	.atomic_check = vc4_vec_encoder_atomic_check,
5078c2ecf20Sopenharmony_ci	.atomic_mode_set = vc4_vec_encoder_atomic_mode_set,
5088c2ecf20Sopenharmony_ci};
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic const struct of_device_id vc4_vec_dt_match[] = {
5118c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm2835-vec", .data = NULL },
5128c2ecf20Sopenharmony_ci	{ /* sentinel */ },
5138c2ecf20Sopenharmony_ci};
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic const char * const tv_mode_names[] = {
5168c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_NTSC] = "NTSC",
5178c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_NTSC_J] = "NTSC-J",
5188c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_PAL] = "PAL",
5198c2ecf20Sopenharmony_ci	[VC4_VEC_TV_MODE_PAL_M] = "PAL-M",
5208c2ecf20Sopenharmony_ci};
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic int vc4_vec_bind(struct device *dev, struct device *master, void *data)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
5258c2ecf20Sopenharmony_ci	struct drm_device *drm = dev_get_drvdata(master);
5268c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(drm);
5278c2ecf20Sopenharmony_ci	struct vc4_vec *vec;
5288c2ecf20Sopenharmony_ci	struct vc4_vec_encoder *vc4_vec_encoder;
5298c2ecf20Sopenharmony_ci	int ret;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	ret = drm_mode_create_tv_properties(drm, ARRAY_SIZE(tv_mode_names),
5328c2ecf20Sopenharmony_ci					    tv_mode_names);
5338c2ecf20Sopenharmony_ci	if (ret)
5348c2ecf20Sopenharmony_ci		return ret;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	vec = devm_kzalloc(dev, sizeof(*vec), GFP_KERNEL);
5378c2ecf20Sopenharmony_ci	if (!vec)
5388c2ecf20Sopenharmony_ci		return -ENOMEM;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	vc4_vec_encoder = devm_kzalloc(dev, sizeof(*vc4_vec_encoder),
5418c2ecf20Sopenharmony_ci				       GFP_KERNEL);
5428c2ecf20Sopenharmony_ci	if (!vc4_vec_encoder)
5438c2ecf20Sopenharmony_ci		return -ENOMEM;
5448c2ecf20Sopenharmony_ci	vc4_vec_encoder->base.type = VC4_ENCODER_TYPE_VEC;
5458c2ecf20Sopenharmony_ci	vc4_vec_encoder->vec = vec;
5468c2ecf20Sopenharmony_ci	vec->encoder = &vc4_vec_encoder->base.base;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	vec->pdev = pdev;
5498c2ecf20Sopenharmony_ci	vec->regs = vc4_ioremap_regs(pdev, 0);
5508c2ecf20Sopenharmony_ci	if (IS_ERR(vec->regs))
5518c2ecf20Sopenharmony_ci		return PTR_ERR(vec->regs);
5528c2ecf20Sopenharmony_ci	vec->regset.base = vec->regs;
5538c2ecf20Sopenharmony_ci	vec->regset.regs = vec_regs;
5548c2ecf20Sopenharmony_ci	vec->regset.nregs = ARRAY_SIZE(vec_regs);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	vec->clock = devm_clk_get(dev, NULL);
5578c2ecf20Sopenharmony_ci	if (IS_ERR(vec->clock)) {
5588c2ecf20Sopenharmony_ci		ret = PTR_ERR(vec->clock);
5598c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
5608c2ecf20Sopenharmony_ci			DRM_ERROR("Failed to get clock: %d\n", ret);
5618c2ecf20Sopenharmony_ci		return ret;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	pm_runtime_enable(dev);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	drm_simple_encoder_init(drm, vec->encoder, DRM_MODE_ENCODER_TVDAC);
5678c2ecf20Sopenharmony_ci	drm_encoder_helper_add(vec->encoder, &vc4_vec_encoder_helper_funcs);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	vec->connector = vc4_vec_connector_init(drm, vec);
5708c2ecf20Sopenharmony_ci	if (IS_ERR(vec->connector)) {
5718c2ecf20Sopenharmony_ci		ret = PTR_ERR(vec->connector);
5728c2ecf20Sopenharmony_ci		goto err_destroy_encoder;
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, vec);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	vc4->vec = vec;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	vc4_debugfs_add_regset32(drm, "vec_regs", &vec->regset);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return 0;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cierr_destroy_encoder:
5848c2ecf20Sopenharmony_ci	drm_encoder_cleanup(vec->encoder);
5858c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return ret;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic void vc4_vec_unbind(struct device *dev, struct device *master,
5918c2ecf20Sopenharmony_ci			   void *data)
5928c2ecf20Sopenharmony_ci{
5938c2ecf20Sopenharmony_ci	struct drm_device *drm = dev_get_drvdata(master);
5948c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(drm);
5958c2ecf20Sopenharmony_ci	struct vc4_vec *vec = dev_get_drvdata(dev);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	vc4_vec_connector_destroy(vec->connector);
5988c2ecf20Sopenharmony_ci	drm_encoder_cleanup(vec->encoder);
5998c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	vc4->vec = NULL;
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic const struct component_ops vc4_vec_ops = {
6058c2ecf20Sopenharmony_ci	.bind   = vc4_vec_bind,
6068c2ecf20Sopenharmony_ci	.unbind = vc4_vec_unbind,
6078c2ecf20Sopenharmony_ci};
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic int vc4_vec_dev_probe(struct platform_device *pdev)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	return component_add(&pdev->dev, &vc4_vec_ops);
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic int vc4_vec_dev_remove(struct platform_device *pdev)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	component_del(&pdev->dev, &vc4_vec_ops);
6178c2ecf20Sopenharmony_ci	return 0;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistruct platform_driver vc4_vec_driver = {
6218c2ecf20Sopenharmony_ci	.probe = vc4_vec_dev_probe,
6228c2ecf20Sopenharmony_ci	.remove = vc4_vec_dev_remove,
6238c2ecf20Sopenharmony_ci	.driver = {
6248c2ecf20Sopenharmony_ci		.name = "vc4_vec",
6258c2ecf20Sopenharmony_ci		.of_match_table = vc4_vec_dt_match,
6268c2ecf20Sopenharmony_ci	},
6278c2ecf20Sopenharmony_ci};
628