18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (c) 2005,2006 Mauro Carvalho Chehab <mchehab@kernel.org>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <dt-bindings/media/tvp5150.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
168c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci#include <media/v4l2-async.h>
198c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
208c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
218c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
228c2ecf20Sopenharmony_ci#include <media/v4l2-fwnode.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-mc.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-rect.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "tvp5150_reg.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define TVP5150_H_MAX		720U
298c2ecf20Sopenharmony_ci#define TVP5150_V_MAX_525_60	480U
308c2ecf20Sopenharmony_ci#define TVP5150_V_MAX_OTHERS	576U
318c2ecf20Sopenharmony_ci#define TVP5150_MAX_CROP_LEFT	511
328c2ecf20Sopenharmony_ci#define TVP5150_MAX_CROP_TOP	127
338c2ecf20Sopenharmony_ci#define TVP5150_CROP_SHIFT	2
348c2ecf20Sopenharmony_ci#define TVP5150_MBUS_FMT	MEDIA_BUS_FMT_UYVY8_2X8
358c2ecf20Sopenharmony_ci#define TVP5150_FIELD		V4L2_FIELD_ALTERNATE
368c2ecf20Sopenharmony_ci#define TVP5150_COLORSPACE	V4L2_COLORSPACE_SMPTE170M
378c2ecf20Sopenharmony_ci#define TVP5150_STD_MASK	(V4L2_STD_NTSC     | \
388c2ecf20Sopenharmony_ci				 V4L2_STD_NTSC_443 | \
398c2ecf20Sopenharmony_ci				 V4L2_STD_PAL      | \
408c2ecf20Sopenharmony_ci				 V4L2_STD_PAL_M    | \
418c2ecf20Sopenharmony_ci				 V4L2_STD_PAL_N    | \
428c2ecf20Sopenharmony_ci				 V4L2_STD_PAL_Nc   | \
438c2ecf20Sopenharmony_ci				 V4L2_STD_SECAM)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define TVP5150_MAX_CONNECTORS	3 /* Check dt-bindings for more information */
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mauro Carvalho Chehab");
498c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int debug;
538c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
548c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-2)");
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cienum tvp5150_pads {
598c2ecf20Sopenharmony_ci	TVP5150_PAD_AIP1A,
608c2ecf20Sopenharmony_ci	TVP5150_PAD_AIP1B,
618c2ecf20Sopenharmony_ci	TVP5150_PAD_VID_OUT,
628c2ecf20Sopenharmony_ci	TVP5150_NUM_PADS
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistruct tvp5150_connector {
668c2ecf20Sopenharmony_ci	struct v4l2_fwnode_connector base;
678c2ecf20Sopenharmony_ci	struct media_entity ent;
688c2ecf20Sopenharmony_ci	struct media_pad pad;
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct tvp5150 {
728c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	struct media_pad pads[TVP5150_NUM_PADS];
758c2ecf20Sopenharmony_ci	struct tvp5150_connector connectors[TVP5150_MAX_CONNECTORS];
768c2ecf20Sopenharmony_ci	struct tvp5150_connector *cur_connector;
778c2ecf20Sopenharmony_ci	unsigned int connectors_num;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
808c2ecf20Sopenharmony_ci	struct v4l2_rect rect;
818c2ecf20Sopenharmony_ci	struct regmap *regmap;
828c2ecf20Sopenharmony_ci	int irq;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	v4l2_std_id norm;	/* Current set standard */
858c2ecf20Sopenharmony_ci	v4l2_std_id detected_norm;
868c2ecf20Sopenharmony_ci	u32 input;
878c2ecf20Sopenharmony_ci	u32 output;
888c2ecf20Sopenharmony_ci	u32 oe;
898c2ecf20Sopenharmony_ci	int enable;
908c2ecf20Sopenharmony_ci	bool lock;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	u16 dev_id;
938c2ecf20Sopenharmony_ci	u16 rom_ver;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	enum v4l2_mbus_type mbus_type;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	return container_of(sd, struct tvp5150, sd);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
1118c2ecf20Sopenharmony_ci	int ret, val;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	ret = regmap_read(decoder->regmap, addr, &val);
1148c2ecf20Sopenharmony_ci	if (ret < 0)
1158c2ecf20Sopenharmony_ci		return ret;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return val;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
1218c2ecf20Sopenharmony_ci				const u8 end, int max_line)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	u8 buf[16];
1248c2ecf20Sopenharmony_ci	int i = 0, j, len;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (max_line > 16) {
1278c2ecf20Sopenharmony_ci		dprintk0(sd->dev, "too much data to dump\n");
1288c2ecf20Sopenharmony_ci		return;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	for (i = init; i < end; i += max_line) {
1328c2ecf20Sopenharmony_ci		len = (end - i > max_line) ? max_line : end - i;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		for (j = 0; j < len; j++)
1358c2ecf20Sopenharmony_ci			buf[j] = tvp5150_read(sd, i + j);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf);
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int tvp5150_log_status(struct v4l2_subdev *sd)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n",
1448c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
1458c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n",
1468c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
1478c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n",
1488c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_OP_MODE_CTL));
1498c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n",
1508c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_MISC_CTL));
1518c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n",
1528c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_AUTOSW_MSK));
1538c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n",
1548c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
1558c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
1568c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
1578c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
1588c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
1598c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n",
1608c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_BRIGHT_CTL));
1618c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n",
1628c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_SATURATION_CTL));
1638c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n",
1648c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_HUE_CTL));
1658c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n",
1668c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CONTRAST_CTL));
1678c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n",
1688c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
1698c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n",
1708c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
1718c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n",
1728c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
1738c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
1748c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Active video cropping stop  = 0x%02x%02x\n",
1758c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
1768c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
1778c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n",
1788c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_GENLOCK));
1798c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n",
1808c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
1818c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n",
1828c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
1838c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n",
1848c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
1858c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
1868c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
1878c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
1888c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n",
1898c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
1908c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n",
1918c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
1928c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n",
1938c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
1948c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n",
1958c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VIDEO_STD));
1968c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
1978c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
1988c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
1998c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n",
2008c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
2018c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n",
2028c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
2038c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
2048c2ecf20Sopenharmony_ci		(tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
2058c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n",
2068c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_MSB_DEV_ID),
2078c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_LSB_DEV_ID));
2088c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n",
2098c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
2108c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
2118c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n",
2128c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
2138c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
2148c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n",
2158c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
2168c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n",
2178c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
2188c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
2198c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_STATUS_REG_1),
2208c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_STATUS_REG_2),
2218c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_STATUS_REG_3),
2228c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_STATUS_REG_4),
2238c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_STATUS_REG_5));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	dump_reg_range(sd, "Teletext filter 1",   TVP5150_TELETEXT_FIL1_INI,
2268c2ecf20Sopenharmony_ci			TVP5150_TELETEXT_FIL1_END, 8);
2278c2ecf20Sopenharmony_ci	dump_reg_range(sd, "Teletext filter 2",   TVP5150_TELETEXT_FIL2_INI,
2288c2ecf20Sopenharmony_ci			TVP5150_TELETEXT_FIL2_END, 8);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n",
2318c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
2328c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n",
2338c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
2348c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n",
2358c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
2368c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n",
2378c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_INT_CONF));
2388c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n",
2398c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
2408c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n",
2418c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
2428c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
2438c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
2448c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n",
2458c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FIFO_RESET));
2468c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n",
2478c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
2488c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
2498c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
2508c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
2518c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n",
2528c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
2538c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n",
2548c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
2558c2ecf20Sopenharmony_ci	dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n",
2568c2ecf20Sopenharmony_ci		tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	dump_reg_range(sd, "CC   data",   TVP5150_CC_DATA_INI,
2598c2ecf20Sopenharmony_ci			TVP5150_CC_DATA_END, 8);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	dump_reg_range(sd, "WSS  data",   TVP5150_WSS_DATA_INI,
2628c2ecf20Sopenharmony_ci			TVP5150_WSS_DATA_END, 8);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	dump_reg_range(sd, "VPS  data",   TVP5150_VPS_DATA_INI,
2658c2ecf20Sopenharmony_ci			TVP5150_VPS_DATA_END, 8);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	dump_reg_range(sd, "VITC data",   TVP5150_VITC_DATA_INI,
2688c2ecf20Sopenharmony_ci			TVP5150_VITC_DATA_END, 10);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	dump_reg_range(sd, "Line mode",   TVP5150_LINE_MODE_INI,
2718c2ecf20Sopenharmony_ci			TVP5150_LINE_MODE_END, 8);
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci/****************************************************************************
2768c2ecf20Sopenharmony_ci			Basic functions
2778c2ecf20Sopenharmony_ci ****************************************************************************/
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic void tvp5150_selmux(struct v4l2_subdev *sd)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	int opmode = 0;
2828c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
2838c2ecf20Sopenharmony_ci	unsigned int mask, val;
2848c2ecf20Sopenharmony_ci	int input = 0;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* Only tvp5150am1 and tvp5151 have signal generator support */
2878c2ecf20Sopenharmony_ci	if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
2888c2ecf20Sopenharmony_ci	    (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
2898c2ecf20Sopenharmony_ci		if (!decoder->enable)
2908c2ecf20Sopenharmony_ci			input = 8;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	switch (decoder->input) {
2948c2ecf20Sopenharmony_ci	case TVP5150_COMPOSITE1:
2958c2ecf20Sopenharmony_ci		input |= 2;
2968c2ecf20Sopenharmony_ci		fallthrough;
2978c2ecf20Sopenharmony_ci	case TVP5150_COMPOSITE0:
2988c2ecf20Sopenharmony_ci		break;
2998c2ecf20Sopenharmony_ci	case TVP5150_SVIDEO:
3008c2ecf20Sopenharmony_ci	default:
3018c2ecf20Sopenharmony_ci		input |= 1;
3028c2ecf20Sopenharmony_ci		break;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug,
3068c2ecf20Sopenharmony_ci		    "Selecting video route: route input=%s, output=%s => tvp5150 input=0x%02x, opmode=0x%02x\n",
3078c2ecf20Sopenharmony_ci		    decoder->input == 0 ? "aip1a" :
3088c2ecf20Sopenharmony_ci		    decoder->input == 2 ? "aip1b" : "svideo",
3098c2ecf20Sopenharmony_ci		    decoder->output == 0 ? "normal" : "black-frame-gen",
3108c2ecf20Sopenharmony_ci		    input, opmode);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_OP_MODE_CTL, opmode);
3138c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_VD_IN_SRC_SEL_1, input);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/*
3168c2ecf20Sopenharmony_ci	 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
3178c2ecf20Sopenharmony_ci	 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
3188c2ecf20Sopenharmony_ci	 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
3198c2ecf20Sopenharmony_ci	 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
3208c2ecf20Sopenharmony_ci	 * INTREQ/GPCL/VBLK to logic 1.
3218c2ecf20Sopenharmony_ci	 */
3228c2ecf20Sopenharmony_ci	mask = TVP5150_MISC_CTL_GPCL | TVP5150_MISC_CTL_HVLK;
3238c2ecf20Sopenharmony_ci	if (decoder->input == TVP5150_SVIDEO)
3248c2ecf20Sopenharmony_ci		val = TVP5150_MISC_CTL_HVLK;
3258c2ecf20Sopenharmony_ci	else
3268c2ecf20Sopenharmony_ci		val = TVP5150_MISC_CTL_GPCL;
3278c2ecf20Sopenharmony_ci	regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
3288c2ecf20Sopenharmony_ci};
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistruct i2c_reg_value {
3318c2ecf20Sopenharmony_ci	unsigned char reg;
3328c2ecf20Sopenharmony_ci	unsigned char value;
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci/* Default values as sugested at TVP5150AM1 datasheet */
3368c2ecf20Sopenharmony_cistatic const struct i2c_reg_value tvp5150_init_default[] = {
3378c2ecf20Sopenharmony_ci	{ /* 0x00 */
3388c2ecf20Sopenharmony_ci		TVP5150_VD_IN_SRC_SEL_1, 0x00
3398c2ecf20Sopenharmony_ci	},
3408c2ecf20Sopenharmony_ci	{ /* 0x01 */
3418c2ecf20Sopenharmony_ci		TVP5150_ANAL_CHL_CTL, 0x15
3428c2ecf20Sopenharmony_ci	},
3438c2ecf20Sopenharmony_ci	{ /* 0x02 */
3448c2ecf20Sopenharmony_ci		TVP5150_OP_MODE_CTL, 0x00
3458c2ecf20Sopenharmony_ci	},
3468c2ecf20Sopenharmony_ci	{ /* 0x03 */
3478c2ecf20Sopenharmony_ci		TVP5150_MISC_CTL, 0x01
3488c2ecf20Sopenharmony_ci	},
3498c2ecf20Sopenharmony_ci	{ /* 0x06 */
3508c2ecf20Sopenharmony_ci		TVP5150_COLOR_KIL_THSH_CTL, 0x10
3518c2ecf20Sopenharmony_ci	},
3528c2ecf20Sopenharmony_ci	{ /* 0x07 */
3538c2ecf20Sopenharmony_ci		TVP5150_LUMA_PROC_CTL_1, 0x60
3548c2ecf20Sopenharmony_ci	},
3558c2ecf20Sopenharmony_ci	{ /* 0x08 */
3568c2ecf20Sopenharmony_ci		TVP5150_LUMA_PROC_CTL_2, 0x00
3578c2ecf20Sopenharmony_ci	},
3588c2ecf20Sopenharmony_ci	{ /* 0x09 */
3598c2ecf20Sopenharmony_ci		TVP5150_BRIGHT_CTL, 0x80
3608c2ecf20Sopenharmony_ci	},
3618c2ecf20Sopenharmony_ci	{ /* 0x0a */
3628c2ecf20Sopenharmony_ci		TVP5150_SATURATION_CTL, 0x80
3638c2ecf20Sopenharmony_ci	},
3648c2ecf20Sopenharmony_ci	{ /* 0x0b */
3658c2ecf20Sopenharmony_ci		TVP5150_HUE_CTL, 0x00
3668c2ecf20Sopenharmony_ci	},
3678c2ecf20Sopenharmony_ci	{ /* 0x0c */
3688c2ecf20Sopenharmony_ci		TVP5150_CONTRAST_CTL, 0x80
3698c2ecf20Sopenharmony_ci	},
3708c2ecf20Sopenharmony_ci	{ /* 0x0d */
3718c2ecf20Sopenharmony_ci		TVP5150_DATA_RATE_SEL, 0x47
3728c2ecf20Sopenharmony_ci	},
3738c2ecf20Sopenharmony_ci	{ /* 0x0e */
3748c2ecf20Sopenharmony_ci		TVP5150_LUMA_PROC_CTL_3, 0x00
3758c2ecf20Sopenharmony_ci	},
3768c2ecf20Sopenharmony_ci	{ /* 0x0f */
3778c2ecf20Sopenharmony_ci		TVP5150_CONF_SHARED_PIN, 0x08
3788c2ecf20Sopenharmony_ci	},
3798c2ecf20Sopenharmony_ci	{ /* 0x11 */
3808c2ecf20Sopenharmony_ci		TVP5150_ACT_VD_CROP_ST_MSB, 0x00
3818c2ecf20Sopenharmony_ci	},
3828c2ecf20Sopenharmony_ci	{ /* 0x12 */
3838c2ecf20Sopenharmony_ci		TVP5150_ACT_VD_CROP_ST_LSB, 0x00
3848c2ecf20Sopenharmony_ci	},
3858c2ecf20Sopenharmony_ci	{ /* 0x13 */
3868c2ecf20Sopenharmony_ci		TVP5150_ACT_VD_CROP_STP_MSB, 0x00
3878c2ecf20Sopenharmony_ci	},
3888c2ecf20Sopenharmony_ci	{ /* 0x14 */
3898c2ecf20Sopenharmony_ci		TVP5150_ACT_VD_CROP_STP_LSB, 0x00
3908c2ecf20Sopenharmony_ci	},
3918c2ecf20Sopenharmony_ci	{ /* 0x15 */
3928c2ecf20Sopenharmony_ci		TVP5150_GENLOCK, 0x01
3938c2ecf20Sopenharmony_ci	},
3948c2ecf20Sopenharmony_ci	{ /* 0x16 */
3958c2ecf20Sopenharmony_ci		TVP5150_HORIZ_SYNC_START, 0x80
3968c2ecf20Sopenharmony_ci	},
3978c2ecf20Sopenharmony_ci	{ /* 0x18 */
3988c2ecf20Sopenharmony_ci		TVP5150_VERT_BLANKING_START, 0x00
3998c2ecf20Sopenharmony_ci	},
4008c2ecf20Sopenharmony_ci	{ /* 0x19 */
4018c2ecf20Sopenharmony_ci		TVP5150_VERT_BLANKING_STOP, 0x00
4028c2ecf20Sopenharmony_ci	},
4038c2ecf20Sopenharmony_ci	{ /* 0x1a */
4048c2ecf20Sopenharmony_ci		TVP5150_CHROMA_PROC_CTL_1, 0x0c
4058c2ecf20Sopenharmony_ci	},
4068c2ecf20Sopenharmony_ci	{ /* 0x1b */
4078c2ecf20Sopenharmony_ci		TVP5150_CHROMA_PROC_CTL_2, 0x14
4088c2ecf20Sopenharmony_ci	},
4098c2ecf20Sopenharmony_ci	{ /* 0x1c */
4108c2ecf20Sopenharmony_ci		TVP5150_INT_RESET_REG_B, 0x00
4118c2ecf20Sopenharmony_ci	},
4128c2ecf20Sopenharmony_ci	{ /* 0x1d */
4138c2ecf20Sopenharmony_ci		TVP5150_INT_ENABLE_REG_B, 0x00
4148c2ecf20Sopenharmony_ci	},
4158c2ecf20Sopenharmony_ci	{ /* 0x1e */
4168c2ecf20Sopenharmony_ci		TVP5150_INTT_CONFIG_REG_B, 0x00
4178c2ecf20Sopenharmony_ci	},
4188c2ecf20Sopenharmony_ci	{ /* 0x28 */
4198c2ecf20Sopenharmony_ci		TVP5150_VIDEO_STD, 0x00
4208c2ecf20Sopenharmony_ci	},
4218c2ecf20Sopenharmony_ci	{ /* 0x2e */
4228c2ecf20Sopenharmony_ci		TVP5150_MACROVISION_ON_CTR, 0x0f
4238c2ecf20Sopenharmony_ci	},
4248c2ecf20Sopenharmony_ci	{ /* 0x2f */
4258c2ecf20Sopenharmony_ci		TVP5150_MACROVISION_OFF_CTR, 0x01
4268c2ecf20Sopenharmony_ci	},
4278c2ecf20Sopenharmony_ci	{ /* 0xbb */
4288c2ecf20Sopenharmony_ci		TVP5150_TELETEXT_FIL_ENA, 0x00
4298c2ecf20Sopenharmony_ci	},
4308c2ecf20Sopenharmony_ci	{ /* 0xc0 */
4318c2ecf20Sopenharmony_ci		TVP5150_INT_STATUS_REG_A, 0x00
4328c2ecf20Sopenharmony_ci	},
4338c2ecf20Sopenharmony_ci	{ /* 0xc1 */
4348c2ecf20Sopenharmony_ci		TVP5150_INT_ENABLE_REG_A, 0x00
4358c2ecf20Sopenharmony_ci	},
4368c2ecf20Sopenharmony_ci	{ /* 0xc2 */
4378c2ecf20Sopenharmony_ci		TVP5150_INT_CONF, 0x04
4388c2ecf20Sopenharmony_ci	},
4398c2ecf20Sopenharmony_ci	{ /* 0xc8 */
4408c2ecf20Sopenharmony_ci		TVP5150_FIFO_INT_THRESHOLD, 0x80
4418c2ecf20Sopenharmony_ci	},
4428c2ecf20Sopenharmony_ci	{ /* 0xc9 */
4438c2ecf20Sopenharmony_ci		TVP5150_FIFO_RESET, 0x00
4448c2ecf20Sopenharmony_ci	},
4458c2ecf20Sopenharmony_ci	{ /* 0xca */
4468c2ecf20Sopenharmony_ci		TVP5150_LINE_NUMBER_INT, 0x00
4478c2ecf20Sopenharmony_ci	},
4488c2ecf20Sopenharmony_ci	{ /* 0xcb */
4498c2ecf20Sopenharmony_ci		TVP5150_PIX_ALIGN_REG_LOW, 0x4e
4508c2ecf20Sopenharmony_ci	},
4518c2ecf20Sopenharmony_ci	{ /* 0xcc */
4528c2ecf20Sopenharmony_ci		TVP5150_PIX_ALIGN_REG_HIGH, 0x00
4538c2ecf20Sopenharmony_ci	},
4548c2ecf20Sopenharmony_ci	{ /* 0xcd */
4558c2ecf20Sopenharmony_ci		TVP5150_FIFO_OUT_CTRL, 0x01
4568c2ecf20Sopenharmony_ci	},
4578c2ecf20Sopenharmony_ci	{ /* 0xcf */
4588c2ecf20Sopenharmony_ci		TVP5150_FULL_FIELD_ENA, 0x00
4598c2ecf20Sopenharmony_ci	},
4608c2ecf20Sopenharmony_ci	{ /* 0xd0 */
4618c2ecf20Sopenharmony_ci		TVP5150_LINE_MODE_INI, 0x00
4628c2ecf20Sopenharmony_ci	},
4638c2ecf20Sopenharmony_ci	{ /* 0xfc */
4648c2ecf20Sopenharmony_ci		TVP5150_FULL_FIELD_MODE_REG, 0x7f
4658c2ecf20Sopenharmony_ci	},
4668c2ecf20Sopenharmony_ci	{ /* end of data */
4678c2ecf20Sopenharmony_ci		0xff, 0xff
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci};
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/* Default values as sugested at TVP5150AM1 datasheet */
4728c2ecf20Sopenharmony_cistatic const struct i2c_reg_value tvp5150_init_enable[] = {
4738c2ecf20Sopenharmony_ci	{	/* Automatic offset and AGC enabled */
4748c2ecf20Sopenharmony_ci		TVP5150_ANAL_CHL_CTL, 0x15
4758c2ecf20Sopenharmony_ci	}, {	/* Activate YCrCb output 0x9 or 0xd ? */
4768c2ecf20Sopenharmony_ci		TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
4778c2ecf20Sopenharmony_ci				  TVP5150_MISC_CTL_INTREQ_OE |
4788c2ecf20Sopenharmony_ci				  TVP5150_MISC_CTL_YCBCR_OE |
4798c2ecf20Sopenharmony_ci				  TVP5150_MISC_CTL_SYNC_OE |
4808c2ecf20Sopenharmony_ci				  TVP5150_MISC_CTL_VBLANK |
4818c2ecf20Sopenharmony_ci				  TVP5150_MISC_CTL_CLOCK_OE,
4828c2ecf20Sopenharmony_ci	}, {	/* Activates video std autodetection for all standards */
4838c2ecf20Sopenharmony_ci		TVP5150_AUTOSW_MSK, 0x0
4848c2ecf20Sopenharmony_ci	}, {	/* Default format: 0x47. For 4:2:2: 0x40 */
4858c2ecf20Sopenharmony_ci		TVP5150_DATA_RATE_SEL, 0x47
4868c2ecf20Sopenharmony_ci	}, {
4878c2ecf20Sopenharmony_ci		TVP5150_CHROMA_PROC_CTL_1, 0x0c
4888c2ecf20Sopenharmony_ci	}, {
4898c2ecf20Sopenharmony_ci		TVP5150_CHROMA_PROC_CTL_2, 0x54
4908c2ecf20Sopenharmony_ci	}, {	/* Non documented, but initialized on WinTV USB2 */
4918c2ecf20Sopenharmony_ci		0x27, 0x20
4928c2ecf20Sopenharmony_ci	}, {
4938c2ecf20Sopenharmony_ci		0xff, 0xff
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci};
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistruct tvp5150_vbi_type {
4988c2ecf20Sopenharmony_ci	unsigned int vbi_type;
4998c2ecf20Sopenharmony_ci	unsigned int ini_line;
5008c2ecf20Sopenharmony_ci	unsigned int end_line;
5018c2ecf20Sopenharmony_ci	unsigned int by_field :1;
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistruct i2c_vbi_ram_value {
5058c2ecf20Sopenharmony_ci	u16 reg;
5068c2ecf20Sopenharmony_ci	struct tvp5150_vbi_type type;
5078c2ecf20Sopenharmony_ci	unsigned char values[16];
5088c2ecf20Sopenharmony_ci};
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/* This struct have the values for each supported VBI Standard
5118c2ecf20Sopenharmony_ci * by
5128c2ecf20Sopenharmony_ci tvp5150_vbi_types should follow the same order as vbi_ram_default
5138c2ecf20Sopenharmony_ci * value 0 means rom position 0x10, value 1 means rom position 0x30
5148c2ecf20Sopenharmony_ci * and so on. There are 16 possible locations from 0 to 15.
5158c2ecf20Sopenharmony_ci */
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic struct i2c_vbi_ram_value vbi_ram_default[] = {
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	/*
5208c2ecf20Sopenharmony_ci	 * FIXME: Current api doesn't handle all VBI types, those not
5218c2ecf20Sopenharmony_ci	 * yet supported are placed under #if 0
5228c2ecf20Sopenharmony_ci	 */
5238c2ecf20Sopenharmony_ci#if 0
5248c2ecf20Sopenharmony_ci	[0] = {0x010, /* Teletext, SECAM, WST System A */
5258c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1},
5268c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
5278c2ecf20Sopenharmony_ci		  0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
5288c2ecf20Sopenharmony_ci	},
5298c2ecf20Sopenharmony_ci#endif
5308c2ecf20Sopenharmony_ci	[1] = {0x030, /* Teletext, PAL, WST System B */
5318c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_B, 6, 22, 1},
5328c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
5338c2ecf20Sopenharmony_ci		  0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
5348c2ecf20Sopenharmony_ci	},
5358c2ecf20Sopenharmony_ci#if 0
5368c2ecf20Sopenharmony_ci	[2] = {0x050, /* Teletext, PAL, WST System C */
5378c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1},
5388c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
5398c2ecf20Sopenharmony_ci		  0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
5408c2ecf20Sopenharmony_ci	},
5418c2ecf20Sopenharmony_ci	[3] = {0x070, /* Teletext, NTSC, WST System B */
5428c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1},
5438c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
5448c2ecf20Sopenharmony_ci		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
5458c2ecf20Sopenharmony_ci	},
5468c2ecf20Sopenharmony_ci	[4] = {0x090, /* Tetetext, NTSC NABTS System C */
5478c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1},
5488c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
5498c2ecf20Sopenharmony_ci		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
5508c2ecf20Sopenharmony_ci	},
5518c2ecf20Sopenharmony_ci	[5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
5528c2ecf20Sopenharmony_ci		{V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1},
5538c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
5548c2ecf20Sopenharmony_ci		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
5558c2ecf20Sopenharmony_ci	},
5568c2ecf20Sopenharmony_ci	[6] = {0x0d0, /* Closed Caption, PAL/SECAM */
5578c2ecf20Sopenharmony_ci		{V4L2_SLICED_CAPTION_625, 22, 22, 1},
5588c2ecf20Sopenharmony_ci		{ 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
5598c2ecf20Sopenharmony_ci		  0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
5608c2ecf20Sopenharmony_ci	},
5618c2ecf20Sopenharmony_ci#endif
5628c2ecf20Sopenharmony_ci	[7] = {0x0f0, /* Closed Caption, NTSC */
5638c2ecf20Sopenharmony_ci		{V4L2_SLICED_CAPTION_525, 21, 21, 1},
5648c2ecf20Sopenharmony_ci		{ 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
5658c2ecf20Sopenharmony_ci		  0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
5668c2ecf20Sopenharmony_ci	},
5678c2ecf20Sopenharmony_ci	[8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
5688c2ecf20Sopenharmony_ci		{V4L2_SLICED_WSS_625, 23, 23, 1},
5698c2ecf20Sopenharmony_ci		{ 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
5708c2ecf20Sopenharmony_ci		  0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
5718c2ecf20Sopenharmony_ci	},
5728c2ecf20Sopenharmony_ci#if 0
5738c2ecf20Sopenharmony_ci	[9] = {0x130, /* Wide Screen Signal, NTSC C */
5748c2ecf20Sopenharmony_ci		{V4L2_SLICED_WSS_525, 20, 20, 1},
5758c2ecf20Sopenharmony_ci		{ 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
5768c2ecf20Sopenharmony_ci		  0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
5778c2ecf20Sopenharmony_ci	},
5788c2ecf20Sopenharmony_ci	[10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
5798c2ecf20Sopenharmony_ci		{V4l2_SLICED_VITC_625, 6, 22, 0},
5808c2ecf20Sopenharmony_ci		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
5818c2ecf20Sopenharmony_ci		  0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
5828c2ecf20Sopenharmony_ci	},
5838c2ecf20Sopenharmony_ci	[11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
5848c2ecf20Sopenharmony_ci		{V4l2_SLICED_VITC_525, 10, 20, 0},
5858c2ecf20Sopenharmony_ci		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
5868c2ecf20Sopenharmony_ci		  0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
5878c2ecf20Sopenharmony_ci	},
5888c2ecf20Sopenharmony_ci#endif
5898c2ecf20Sopenharmony_ci	[12] = {0x190, /* Video Program System (VPS), PAL */
5908c2ecf20Sopenharmony_ci		{V4L2_SLICED_VPS, 16, 16, 0},
5918c2ecf20Sopenharmony_ci		{ 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
5928c2ecf20Sopenharmony_ci		  0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
5938c2ecf20Sopenharmony_ci	},
5948c2ecf20Sopenharmony_ci	/* 0x1d0 User programmable */
5958c2ecf20Sopenharmony_ci};
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic int tvp5150_write_inittab(struct v4l2_subdev *sd,
5988c2ecf20Sopenharmony_ci				const struct i2c_reg_value *regs)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	while (regs->reg != 0xff) {
6038c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, regs->reg, regs->value);
6048c2ecf20Sopenharmony_ci		regs++;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci	return 0;
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic int tvp5150_vdp_init(struct v4l2_subdev *sd)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
6128c2ecf20Sopenharmony_ci	struct regmap *map = decoder->regmap;
6138c2ecf20Sopenharmony_ci	unsigned int i;
6148c2ecf20Sopenharmony_ci	int j;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* Disable Full Field */
6178c2ecf20Sopenharmony_ci	regmap_write(map, TVP5150_FULL_FIELD_ENA, 0);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	/* Before programming, Line mode should be at 0xff */
6208c2ecf20Sopenharmony_ci	for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
6218c2ecf20Sopenharmony_ci		regmap_write(map, i, 0xff);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* Load Ram Table */
6248c2ecf20Sopenharmony_ci	for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) {
6258c2ecf20Sopenharmony_ci		const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j];
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci		if (!regs->type.vbi_type)
6288c2ecf20Sopenharmony_ci			continue;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
6318c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci		for (i = 0; i < 16; i++)
6348c2ecf20Sopenharmony_ci			regmap_write(map, TVP5150_VDP_CONF_RAM_DATA,
6358c2ecf20Sopenharmony_ci				     regs->values[i]);
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci	return 0;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
6418c2ecf20Sopenharmony_cistatic int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
6428c2ecf20Sopenharmony_ci				struct v4l2_sliced_vbi_cap *cap)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	int line, i;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n");
6478c2ecf20Sopenharmony_ci	memset(cap, 0, sizeof(*cap));
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
6508c2ecf20Sopenharmony_ci		const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci		if (!regs->type.vbi_type)
6538c2ecf20Sopenharmony_ci			continue;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci		for (line = regs->type.ini_line;
6568c2ecf20Sopenharmony_ci		     line <= regs->type.end_line;
6578c2ecf20Sopenharmony_ci		     line++) {
6588c2ecf20Sopenharmony_ci			cap->service_lines[0][line] |= regs->type.vbi_type;
6598c2ecf20Sopenharmony_ci		}
6608c2ecf20Sopenharmony_ci		cap->service_set |= regs->type.vbi_type;
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci	return 0;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/* Set vbi processing
6668c2ecf20Sopenharmony_ci * type - one of tvp5150_vbi_types
6678c2ecf20Sopenharmony_ci * line - line to gather data
6688c2ecf20Sopenharmony_ci * fields: bit 0 field1, bit 1, field2
6698c2ecf20Sopenharmony_ci * flags (default=0xf0) is a bitmask, were set means:
6708c2ecf20Sopenharmony_ci *	bit 7: enable filtering null bytes on CC
6718c2ecf20Sopenharmony_ci *	bit 6: send data also to FIFO
6728c2ecf20Sopenharmony_ci *	bit 5: don't allow data with errors on FIFO
6738c2ecf20Sopenharmony_ci *	bit 4: enable ECC when possible
6748c2ecf20Sopenharmony_ci * pix_align = pix alignment:
6758c2ecf20Sopenharmony_ci *	LSB = field1
6768c2ecf20Sopenharmony_ci *	MSB = field2
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_cistatic int tvp5150_set_vbi(struct v4l2_subdev *sd,
6798c2ecf20Sopenharmony_ci			unsigned int type, u8 flags, int line,
6808c2ecf20Sopenharmony_ci			const int fields)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
6838c2ecf20Sopenharmony_ci	v4l2_std_id std = decoder->norm;
6848c2ecf20Sopenharmony_ci	u8 reg;
6858c2ecf20Sopenharmony_ci	int i, pos = 0;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	if (std == V4L2_STD_ALL) {
6888c2ecf20Sopenharmony_ci		dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
6898c2ecf20Sopenharmony_ci		return 0;
6908c2ecf20Sopenharmony_ci	} else if (std & V4L2_STD_625_50) {
6918c2ecf20Sopenharmony_ci		/* Don't follow NTSC Line number convension */
6928c2ecf20Sopenharmony_ci		line += 3;
6938c2ecf20Sopenharmony_ci	}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (line < 6 || line > 27)
6968c2ecf20Sopenharmony_ci		return 0;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
6998c2ecf20Sopenharmony_ci		const struct i2c_vbi_ram_value *regs =  &vbi_ram_default[i];
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		if (!regs->type.vbi_type)
7028c2ecf20Sopenharmony_ci			continue;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		if ((type & regs->type.vbi_type) &&
7058c2ecf20Sopenharmony_ci		    (line >= regs->type.ini_line) &&
7068c2ecf20Sopenharmony_ci		    (line <= regs->type.end_line))
7078c2ecf20Sopenharmony_ci			break;
7088c2ecf20Sopenharmony_ci		pos++;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	type = pos | (flags & 0xf0);
7128c2ecf20Sopenharmony_ci	reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	if (fields & 1)
7158c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, reg, type);
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (fields & 2)
7188c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, reg + 1, type);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return type;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic int tvp5150_get_vbi(struct v4l2_subdev *sd, int line)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
7268c2ecf20Sopenharmony_ci	v4l2_std_id std = decoder->norm;
7278c2ecf20Sopenharmony_ci	u8 reg;
7288c2ecf20Sopenharmony_ci	int pos, type = 0;
7298c2ecf20Sopenharmony_ci	int i, ret = 0;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	if (std == V4L2_STD_ALL) {
7328c2ecf20Sopenharmony_ci		dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
7338c2ecf20Sopenharmony_ci		return 0;
7348c2ecf20Sopenharmony_ci	} else if (std & V4L2_STD_625_50) {
7358c2ecf20Sopenharmony_ci		/* Don't follow NTSC Line number convension */
7368c2ecf20Sopenharmony_ci		line += 3;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	if (line < 6 || line > 27)
7408c2ecf20Sopenharmony_ci		return 0;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	for (i = 0; i <= 1; i++) {
7458c2ecf20Sopenharmony_ci		ret = tvp5150_read(sd, reg + i);
7468c2ecf20Sopenharmony_ci		if (ret < 0) {
7478c2ecf20Sopenharmony_ci			dev_err(sd->dev, "%s: failed with error = %d\n",
7488c2ecf20Sopenharmony_ci				 __func__, ret);
7498c2ecf20Sopenharmony_ci			return 0;
7508c2ecf20Sopenharmony_ci		}
7518c2ecf20Sopenharmony_ci		pos = ret & 0x0f;
7528c2ecf20Sopenharmony_ci		if (pos < ARRAY_SIZE(vbi_ram_default))
7538c2ecf20Sopenharmony_ci			type |= vbi_ram_default[pos].type.vbi_type;
7548c2ecf20Sopenharmony_ci	}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	return type;
7578c2ecf20Sopenharmony_ci}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_cistatic int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
7628c2ecf20Sopenharmony_ci	int fmt = 0;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* First tests should be against specific std */
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (std == V4L2_STD_NTSC_443) {
7678c2ecf20Sopenharmony_ci		fmt = VIDEO_STD_NTSC_4_43_BIT;
7688c2ecf20Sopenharmony_ci	} else if (std == V4L2_STD_PAL_M) {
7698c2ecf20Sopenharmony_ci		fmt = VIDEO_STD_PAL_M_BIT;
7708c2ecf20Sopenharmony_ci	} else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
7718c2ecf20Sopenharmony_ci		fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
7728c2ecf20Sopenharmony_ci	} else {
7738c2ecf20Sopenharmony_ci		/* Then, test against generic ones */
7748c2ecf20Sopenharmony_ci		if (std & V4L2_STD_NTSC)
7758c2ecf20Sopenharmony_ci			fmt = VIDEO_STD_NTSC_MJ_BIT;
7768c2ecf20Sopenharmony_ci		else if (std & V4L2_STD_PAL)
7778c2ecf20Sopenharmony_ci			fmt = VIDEO_STD_PAL_BDGHIN_BIT;
7788c2ecf20Sopenharmony_ci		else if (std & V4L2_STD_SECAM)
7798c2ecf20Sopenharmony_ci			fmt = VIDEO_STD_SECAM_BIT;
7808c2ecf20Sopenharmony_ci	}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt);
7838c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_VIDEO_STD, fmt);
7848c2ecf20Sopenharmony_ci	return 0;
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cistatic int tvp5150_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	*std = decoder->norm;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	return 0;
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cistatic int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
7998c2ecf20Sopenharmony_ci	struct tvp5150_connector *cur_con = decoder->cur_connector;
8008c2ecf20Sopenharmony_ci	v4l2_std_id supported_stds;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	if (decoder->norm == std)
8038c2ecf20Sopenharmony_ci		return 0;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	/* In case of no of-connectors are available no limitations are made */
8068c2ecf20Sopenharmony_ci	if (!decoder->connectors_num)
8078c2ecf20Sopenharmony_ci		supported_stds = V4L2_STD_ALL;
8088c2ecf20Sopenharmony_ci	else
8098c2ecf20Sopenharmony_ci		supported_stds = cur_con->base.connector.analog.sdtv_stds;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	/*
8128c2ecf20Sopenharmony_ci	 * Check if requested std or group of std's is/are supported by the
8138c2ecf20Sopenharmony_ci	 * connector.
8148c2ecf20Sopenharmony_ci	 */
8158c2ecf20Sopenharmony_ci	if ((supported_stds & std) == 0)
8168c2ecf20Sopenharmony_ci		return -EINVAL;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	/* Change cropping height limits */
8198c2ecf20Sopenharmony_ci	if (std & V4L2_STD_525_60)
8208c2ecf20Sopenharmony_ci		decoder->rect.height = TVP5150_V_MAX_525_60;
8218c2ecf20Sopenharmony_ci	else
8228c2ecf20Sopenharmony_ci		decoder->rect.height = TVP5150_V_MAX_OTHERS;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	/* Set only the specific supported std in case of group of std's. */
8258c2ecf20Sopenharmony_ci	decoder->norm = supported_stds & std;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	return tvp5150_set_std(sd, std);
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci	int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	switch (val & 0x0F) {
8358c2ecf20Sopenharmony_ci	case 0x01:
8368c2ecf20Sopenharmony_ci		return V4L2_STD_NTSC;
8378c2ecf20Sopenharmony_ci	case 0x03:
8388c2ecf20Sopenharmony_ci		return V4L2_STD_PAL;
8398c2ecf20Sopenharmony_ci	case 0x05:
8408c2ecf20Sopenharmony_ci		return V4L2_STD_PAL_M;
8418c2ecf20Sopenharmony_ci	case 0x07:
8428c2ecf20Sopenharmony_ci		return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
8438c2ecf20Sopenharmony_ci	case 0x09:
8448c2ecf20Sopenharmony_ci		return V4L2_STD_NTSC_443;
8458c2ecf20Sopenharmony_ci	case 0xb:
8468c2ecf20Sopenharmony_ci		return V4L2_STD_SECAM;
8478c2ecf20Sopenharmony_ci	default:
8488c2ecf20Sopenharmony_ci		return V4L2_STD_UNKNOWN;
8498c2ecf20Sopenharmony_ci	}
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic int query_lock(struct v4l2_subdev *sd)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
8558c2ecf20Sopenharmony_ci	int status;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	if (decoder->irq)
8588c2ecf20Sopenharmony_ci		return decoder->lock;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	regmap_read(decoder->regmap, TVP5150_STATUS_REG_1, &status);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* For standard detection, we need the 3 locks */
8638c2ecf20Sopenharmony_ci	return (status & 0x0e) == 0x0e;
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_cistatic int tvp5150_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	*std_id = query_lock(sd) ? tvp5150_read_std(sd) : V4L2_STD_UNKNOWN;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	return 0;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic const struct v4l2_event tvp5150_ev_fmt = {
8748c2ecf20Sopenharmony_ci	.type = V4L2_EVENT_SOURCE_CHANGE,
8758c2ecf20Sopenharmony_ci	.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
8768c2ecf20Sopenharmony_ci};
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_cistatic irqreturn_t tvp5150_isr(int irq, void *dev_id)
8798c2ecf20Sopenharmony_ci{
8808c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = dev_id;
8818c2ecf20Sopenharmony_ci	struct regmap *map = decoder->regmap;
8828c2ecf20Sopenharmony_ci	unsigned int mask, active = 0, status = 0;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
8858c2ecf20Sopenharmony_ci	       TVP5150_MISC_CTL_CLOCK_OE;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	regmap_read(map, TVP5150_INT_STATUS_REG_A, &status);
8888c2ecf20Sopenharmony_ci	if (status) {
8898c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_INT_STATUS_REG_A, status);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci		if (status & TVP5150_INT_A_LOCK) {
8928c2ecf20Sopenharmony_ci			decoder->lock = !!(status & TVP5150_INT_A_LOCK_STATUS);
8938c2ecf20Sopenharmony_ci			dev_dbg_lvl(decoder->sd.dev, 1, debug,
8948c2ecf20Sopenharmony_ci				    "sync lo%s signal\n",
8958c2ecf20Sopenharmony_ci				    decoder->lock ? "ck" : "ss");
8968c2ecf20Sopenharmony_ci			v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
8978c2ecf20Sopenharmony_ci			regmap_update_bits(map, TVP5150_MISC_CTL, mask,
8988c2ecf20Sopenharmony_ci					   decoder->lock ? decoder->oe : 0);
8998c2ecf20Sopenharmony_ci		}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
9028c2ecf20Sopenharmony_ci	}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	regmap_read(map, TVP5150_INT_ACTIVE_REG_B, &active);
9058c2ecf20Sopenharmony_ci	if (active) {
9068c2ecf20Sopenharmony_ci		status = 0;
9078c2ecf20Sopenharmony_ci		regmap_read(map, TVP5150_INT_STATUS_REG_B, &status);
9088c2ecf20Sopenharmony_ci		if (status)
9098c2ecf20Sopenharmony_ci			regmap_write(map, TVP5150_INT_RESET_REG_B, status);
9108c2ecf20Sopenharmony_ci	}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_cistatic int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
9188c2ecf20Sopenharmony_ci	struct regmap *map = decoder->regmap;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	/* Initializes TVP5150 to its default values */
9218c2ecf20Sopenharmony_ci	tvp5150_write_inittab(sd, tvp5150_init_default);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (decoder->irq) {
9248c2ecf20Sopenharmony_ci		/* Configure pins: FID, VSYNC, INTREQ, SCLK */
9258c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x0);
9268c2ecf20Sopenharmony_ci		/* Set interrupt polarity to active high */
9278c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE | 0x1);
9288c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x1);
9298c2ecf20Sopenharmony_ci	} else {
9308c2ecf20Sopenharmony_ci		/* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */
9318c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x2);
9328c2ecf20Sopenharmony_ci		/* Keep interrupt polarity active low */
9338c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE);
9348c2ecf20Sopenharmony_ci		regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x0);
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	/* Initializes VDP registers */
9388c2ecf20Sopenharmony_ci	tvp5150_vdp_init(sd);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	/* Selects decoder input */
9418c2ecf20Sopenharmony_ci	tvp5150_selmux(sd);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	/* Initialize image preferences */
9448c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(&decoder->hdl);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	return 0;
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cistatic int tvp5150_enable(struct v4l2_subdev *sd)
9508c2ecf20Sopenharmony_ci{
9518c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
9528c2ecf20Sopenharmony_ci	v4l2_std_id std;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	/* Initializes TVP5150 to stream enabled values */
9558c2ecf20Sopenharmony_ci	tvp5150_write_inittab(sd, tvp5150_init_enable);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	if (decoder->norm == V4L2_STD_ALL)
9588c2ecf20Sopenharmony_ci		std = tvp5150_read_std(sd);
9598c2ecf20Sopenharmony_ci	else
9608c2ecf20Sopenharmony_ci		std = decoder->norm;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	/* Disable autoswitch mode */
9638c2ecf20Sopenharmony_ci	tvp5150_set_std(sd, std);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	/*
9668c2ecf20Sopenharmony_ci	 * Enable the YCbCr and clock outputs. In discrete sync mode
9678c2ecf20Sopenharmony_ci	 * (non-BT.656) additionally enable the the sync outputs.
9688c2ecf20Sopenharmony_ci	 */
9698c2ecf20Sopenharmony_ci	switch (decoder->mbus_type) {
9708c2ecf20Sopenharmony_ci	case V4L2_MBUS_PARALLEL:
9718c2ecf20Sopenharmony_ci		/* 8-bit 4:2:2 YUV with discrete sync output */
9728c2ecf20Sopenharmony_ci		regmap_update_bits(decoder->regmap, TVP5150_DATA_RATE_SEL,
9738c2ecf20Sopenharmony_ci				   0x7, 0x0);
9748c2ecf20Sopenharmony_ci		decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
9758c2ecf20Sopenharmony_ci			      TVP5150_MISC_CTL_CLOCK_OE |
9768c2ecf20Sopenharmony_ci			      TVP5150_MISC_CTL_SYNC_OE;
9778c2ecf20Sopenharmony_ci		break;
9788c2ecf20Sopenharmony_ci	case V4L2_MBUS_BT656:
9798c2ecf20Sopenharmony_ci		decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
9808c2ecf20Sopenharmony_ci			      TVP5150_MISC_CTL_CLOCK_OE;
9818c2ecf20Sopenharmony_ci		break;
9828c2ecf20Sopenharmony_ci	default:
9838c2ecf20Sopenharmony_ci		return -EINVAL;
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	return 0;
9878c2ecf20Sopenharmony_ci};
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_cistatic int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
9908c2ecf20Sopenharmony_ci{
9918c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = to_sd(ctrl);
9928c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	switch (ctrl->id) {
9958c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
9968c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_BRIGHT_CTL, ctrl->val);
9978c2ecf20Sopenharmony_ci		return 0;
9988c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
9998c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_CONTRAST_CTL, ctrl->val);
10008c2ecf20Sopenharmony_ci		return 0;
10018c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
10028c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_SATURATION_CTL,
10038c2ecf20Sopenharmony_ci			     ctrl->val);
10048c2ecf20Sopenharmony_ci		return 0;
10058c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
10068c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_HUE_CTL, ctrl->val);
10078c2ecf20Sopenharmony_ci		return 0;
10088c2ecf20Sopenharmony_ci	case V4L2_CID_TEST_PATTERN:
10098c2ecf20Sopenharmony_ci		decoder->enable = ctrl->val ? false : true;
10108c2ecf20Sopenharmony_ci		tvp5150_selmux(sd);
10118c2ecf20Sopenharmony_ci		return 0;
10128c2ecf20Sopenharmony_ci	}
10138c2ecf20Sopenharmony_ci	return -EINVAL;
10148c2ecf20Sopenharmony_ci}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_cistatic void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	/* Default is no cropping */
10198c2ecf20Sopenharmony_ci	crop->top = 0;
10208c2ecf20Sopenharmony_ci	crop->left = 0;
10218c2ecf20Sopenharmony_ci	crop->width = TVP5150_H_MAX;
10228c2ecf20Sopenharmony_ci	if (std & V4L2_STD_525_60)
10238c2ecf20Sopenharmony_ci		crop->height = TVP5150_V_MAX_525_60;
10248c2ecf20Sopenharmony_ci	else
10258c2ecf20Sopenharmony_ci		crop->height = TVP5150_V_MAX_OTHERS;
10268c2ecf20Sopenharmony_ci}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_cistatic struct v4l2_rect *
10298c2ecf20Sopenharmony_citvp5150_get_pad_crop(struct tvp5150 *decoder,
10308c2ecf20Sopenharmony_ci		     struct v4l2_subdev_pad_config *cfg, unsigned int pad,
10318c2ecf20Sopenharmony_ci		     enum v4l2_subdev_format_whence which)
10328c2ecf20Sopenharmony_ci{
10338c2ecf20Sopenharmony_ci	switch (which) {
10348c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
10358c2ecf20Sopenharmony_ci		return &decoder->rect;
10368c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
10378c2ecf20Sopenharmony_ci#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
10388c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_crop(&decoder->sd, cfg, pad);
10398c2ecf20Sopenharmony_ci#else
10408c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
10418c2ecf20Sopenharmony_ci#endif
10428c2ecf20Sopenharmony_ci	default:
10438c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_cistatic int tvp5150_fill_fmt(struct v4l2_subdev *sd,
10488c2ecf20Sopenharmony_ci			    struct v4l2_subdev_pad_config *cfg,
10498c2ecf20Sopenharmony_ci			    struct v4l2_subdev_format *format)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *f;
10528c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	if (!format || (format->pad != TVP5150_PAD_VID_OUT))
10558c2ecf20Sopenharmony_ci		return -EINVAL;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	f = &format->format;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	f->width = decoder->rect.width;
10608c2ecf20Sopenharmony_ci	f->height = decoder->rect.height / 2;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	f->code = TVP5150_MBUS_FMT;
10638c2ecf20Sopenharmony_ci	f->field = TVP5150_FIELD;
10648c2ecf20Sopenharmony_ci	f->colorspace = TVP5150_COLORSPACE;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width,
10678c2ecf20Sopenharmony_ci		    f->height);
10688c2ecf20Sopenharmony_ci	return 0;
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_cistatic unsigned int tvp5150_get_hmax(struct v4l2_subdev *sd)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
10748c2ecf20Sopenharmony_ci	v4l2_std_id std;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	/* Calculate height based on current standard */
10778c2ecf20Sopenharmony_ci	if (decoder->norm == V4L2_STD_ALL)
10788c2ecf20Sopenharmony_ci		std = tvp5150_read_std(sd);
10798c2ecf20Sopenharmony_ci	else
10808c2ecf20Sopenharmony_ci		std = decoder->norm;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	return (std & V4L2_STD_525_60) ?
10838c2ecf20Sopenharmony_ci		TVP5150_V_MAX_525_60 : TVP5150_V_MAX_OTHERS;
10848c2ecf20Sopenharmony_ci}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_cistatic void tvp5150_set_hw_selection(struct v4l2_subdev *sd,
10878c2ecf20Sopenharmony_ci				     struct v4l2_rect *rect)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
10908c2ecf20Sopenharmony_ci	unsigned int hmax = tvp5150_get_hmax(sd);
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top);
10938c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
10948c2ecf20Sopenharmony_ci		     rect->top + rect->height - hmax);
10958c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
10968c2ecf20Sopenharmony_ci		     rect->left >> TVP5150_CROP_SHIFT);
10978c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
10988c2ecf20Sopenharmony_ci		     rect->left | (1 << TVP5150_CROP_SHIFT));
10998c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
11008c2ecf20Sopenharmony_ci		     (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >>
11018c2ecf20Sopenharmony_ci		     TVP5150_CROP_SHIFT);
11028c2ecf20Sopenharmony_ci	regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
11038c2ecf20Sopenharmony_ci		     rect->left + rect->width - TVP5150_MAX_CROP_LEFT);
11048c2ecf20Sopenharmony_ci}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_cistatic int tvp5150_set_selection(struct v4l2_subdev *sd,
11078c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
11088c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
11098c2ecf20Sopenharmony_ci{
11108c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
11118c2ecf20Sopenharmony_ci	struct v4l2_rect *rect = &sel->r;
11128c2ecf20Sopenharmony_ci	struct v4l2_rect *crop;
11138c2ecf20Sopenharmony_ci	unsigned int hmax;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP)
11168c2ecf20Sopenharmony_ci		return -EINVAL;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
11198c2ecf20Sopenharmony_ci		__func__, rect->left, rect->top, rect->width, rect->height);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	/* tvp5150 has some special limits */
11228c2ecf20Sopenharmony_ci	rect->left = clamp(rect->left, 0, TVP5150_MAX_CROP_LEFT);
11238c2ecf20Sopenharmony_ci	rect->top = clamp(rect->top, 0, TVP5150_MAX_CROP_TOP);
11248c2ecf20Sopenharmony_ci	hmax = tvp5150_get_hmax(sd);
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	/*
11278c2ecf20Sopenharmony_ci	 * alignments:
11288c2ecf20Sopenharmony_ci	 *  - width = 2 due to UYVY colorspace
11298c2ecf20Sopenharmony_ci	 *  - height, image = no special alignment
11308c2ecf20Sopenharmony_ci	 */
11318c2ecf20Sopenharmony_ci	v4l_bound_align_image(&rect->width,
11328c2ecf20Sopenharmony_ci			      TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect->left,
11338c2ecf20Sopenharmony_ci			      TVP5150_H_MAX - rect->left, 1, &rect->height,
11348c2ecf20Sopenharmony_ci			      hmax - TVP5150_MAX_CROP_TOP - rect->top,
11358c2ecf20Sopenharmony_ci			      hmax - rect->top, 0, 0);
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API) &&
11388c2ecf20Sopenharmony_ci	    sel->which == V4L2_SUBDEV_FORMAT_TRY)
11398c2ecf20Sopenharmony_ci		return 0;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad, sel->which);
11428c2ecf20Sopenharmony_ci	if (IS_ERR(crop))
11438c2ecf20Sopenharmony_ci		return PTR_ERR(crop);
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	/*
11468c2ecf20Sopenharmony_ci	 * Update output image size if the selection (crop) rectangle size or
11478c2ecf20Sopenharmony_ci	 * position has been modified.
11488c2ecf20Sopenharmony_ci	 */
11498c2ecf20Sopenharmony_ci	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
11508c2ecf20Sopenharmony_ci	    !v4l2_rect_equal(rect, crop))
11518c2ecf20Sopenharmony_ci		tvp5150_set_hw_selection(sd, rect);
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	*crop = *rect;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	return 0;
11568c2ecf20Sopenharmony_ci}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_cistatic int tvp5150_get_selection(struct v4l2_subdev *sd,
11598c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
11608c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
11618c2ecf20Sopenharmony_ci{
11628c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
11638c2ecf20Sopenharmony_ci	struct v4l2_rect *crop;
11648c2ecf20Sopenharmony_ci	v4l2_std_id std;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	switch (sel->target) {
11678c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP_BOUNDS:
11688c2ecf20Sopenharmony_ci		sel->r.left = 0;
11698c2ecf20Sopenharmony_ci		sel->r.top = 0;
11708c2ecf20Sopenharmony_ci		sel->r.width = TVP5150_H_MAX;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci		/* Calculate height based on current standard */
11738c2ecf20Sopenharmony_ci		if (decoder->norm == V4L2_STD_ALL)
11748c2ecf20Sopenharmony_ci			std = tvp5150_read_std(sd);
11758c2ecf20Sopenharmony_ci		else
11768c2ecf20Sopenharmony_ci			std = decoder->norm;
11778c2ecf20Sopenharmony_ci		if (std & V4L2_STD_525_60)
11788c2ecf20Sopenharmony_ci			sel->r.height = TVP5150_V_MAX_525_60;
11798c2ecf20Sopenharmony_ci		else
11808c2ecf20Sopenharmony_ci			sel->r.height = TVP5150_V_MAX_OTHERS;
11818c2ecf20Sopenharmony_ci		return 0;
11828c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP:
11838c2ecf20Sopenharmony_ci		crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad,
11848c2ecf20Sopenharmony_ci					    sel->which);
11858c2ecf20Sopenharmony_ci		if (IS_ERR(crop))
11868c2ecf20Sopenharmony_ci			return PTR_ERR(crop);
11878c2ecf20Sopenharmony_ci		sel->r = *crop;
11888c2ecf20Sopenharmony_ci		return 0;
11898c2ecf20Sopenharmony_ci	default:
11908c2ecf20Sopenharmony_ci		return -EINVAL;
11918c2ecf20Sopenharmony_ci	}
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_cistatic int tvp5150_get_mbus_config(struct v4l2_subdev *sd,
11958c2ecf20Sopenharmony_ci				   unsigned int pad,
11968c2ecf20Sopenharmony_ci				   struct v4l2_mbus_config *cfg)
11978c2ecf20Sopenharmony_ci{
11988c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	cfg->type = decoder->mbus_type;
12018c2ecf20Sopenharmony_ci	cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
12028c2ecf20Sopenharmony_ci		   | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	return 0;
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci/****************************************************************************
12088c2ecf20Sopenharmony_ci			V4L2 subdev pad ops
12098c2ecf20Sopenharmony_ci ****************************************************************************/
12108c2ecf20Sopenharmony_cistatic int tvp5150_init_cfg(struct v4l2_subdev *sd,
12118c2ecf20Sopenharmony_ci			    struct v4l2_subdev_pad_config *cfg)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
12148c2ecf20Sopenharmony_ci	v4l2_std_id std;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	/*
12178c2ecf20Sopenharmony_ci	 * Reset selection to maximum on subdev_open() if autodetection is on
12188c2ecf20Sopenharmony_ci	 * and a standard change is detected.
12198c2ecf20Sopenharmony_ci	 */
12208c2ecf20Sopenharmony_ci	if (decoder->norm == V4L2_STD_ALL) {
12218c2ecf20Sopenharmony_ci		std = tvp5150_read_std(sd);
12228c2ecf20Sopenharmony_ci		if (std != decoder->detected_norm) {
12238c2ecf20Sopenharmony_ci			decoder->detected_norm = std;
12248c2ecf20Sopenharmony_ci			tvp5150_set_default(std, &decoder->rect);
12258c2ecf20Sopenharmony_ci		}
12268c2ecf20Sopenharmony_ci	}
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	return 0;
12298c2ecf20Sopenharmony_ci}
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_cistatic int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
12328c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
12338c2ecf20Sopenharmony_ci		struct v4l2_subdev_mbus_code_enum *code)
12348c2ecf20Sopenharmony_ci{
12358c2ecf20Sopenharmony_ci	if (code->pad || code->index)
12368c2ecf20Sopenharmony_ci		return -EINVAL;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	code->code = TVP5150_MBUS_FMT;
12398c2ecf20Sopenharmony_ci	return 0;
12408c2ecf20Sopenharmony_ci}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_cistatic int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
12438c2ecf20Sopenharmony_ci				   struct v4l2_subdev_pad_config *cfg,
12448c2ecf20Sopenharmony_ci				   struct v4l2_subdev_frame_size_enum *fse)
12458c2ecf20Sopenharmony_ci{
12468c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	if (fse->index >= 8 || fse->code != TVP5150_MBUS_FMT)
12498c2ecf20Sopenharmony_ci		return -EINVAL;
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	fse->code = TVP5150_MBUS_FMT;
12528c2ecf20Sopenharmony_ci	fse->min_width = decoder->rect.width;
12538c2ecf20Sopenharmony_ci	fse->max_width = decoder->rect.width;
12548c2ecf20Sopenharmony_ci	fse->min_height = decoder->rect.height / 2;
12558c2ecf20Sopenharmony_ci	fse->max_height = decoder->rect.height / 2;
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	return 0;
12588c2ecf20Sopenharmony_ci}
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci/****************************************************************************
12618c2ecf20Sopenharmony_ci *			Media entity ops
12628c2ecf20Sopenharmony_ci ****************************************************************************/
12638c2ecf20Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
12648c2ecf20Sopenharmony_cistatic int tvp5150_set_link(struct media_pad *connector_pad,
12658c2ecf20Sopenharmony_ci			    struct media_pad *tvp5150_pad, u32 flags)
12668c2ecf20Sopenharmony_ci{
12678c2ecf20Sopenharmony_ci	struct media_link *link;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	link = media_entity_find_link(connector_pad, tvp5150_pad);
12708c2ecf20Sopenharmony_ci	if (!link)
12718c2ecf20Sopenharmony_ci		return -EINVAL;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	link->flags = flags;
12748c2ecf20Sopenharmony_ci	link->reverse->flags = link->flags;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	return 0;
12778c2ecf20Sopenharmony_ci}
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_cistatic int tvp5150_disable_all_input_links(struct tvp5150 *decoder)
12808c2ecf20Sopenharmony_ci{
12818c2ecf20Sopenharmony_ci	struct media_pad *connector_pad;
12828c2ecf20Sopenharmony_ci	unsigned int i;
12838c2ecf20Sopenharmony_ci	int err;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
12868c2ecf20Sopenharmony_ci		connector_pad = media_entity_remote_pad(&decoder->pads[i]);
12878c2ecf20Sopenharmony_ci		if (!connector_pad)
12888c2ecf20Sopenharmony_ci			continue;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci		err = tvp5150_set_link(connector_pad, &decoder->pads[i], 0);
12918c2ecf20Sopenharmony_ci		if (err)
12928c2ecf20Sopenharmony_ci			return err;
12938c2ecf20Sopenharmony_ci	}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	return 0;
12968c2ecf20Sopenharmony_ci}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_cistatic int tvp5150_s_routing(struct v4l2_subdev *sd, u32 input, u32 output,
12998c2ecf20Sopenharmony_ci			     u32 config);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_cistatic int tvp5150_link_setup(struct media_entity *entity,
13028c2ecf20Sopenharmony_ci			      const struct media_pad *tvp5150_pad,
13038c2ecf20Sopenharmony_ci			      const struct media_pad *remote, u32 flags)
13048c2ecf20Sopenharmony_ci{
13058c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
13068c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
13078c2ecf20Sopenharmony_ci	struct media_pad *other_tvp5150_pad =
13088c2ecf20Sopenharmony_ci		&decoder->pads[tvp5150_pad->index ^ 1];
13098c2ecf20Sopenharmony_ci	struct v4l2_fwnode_connector *v4l2c;
13108c2ecf20Sopenharmony_ci	bool is_svideo = false;
13118c2ecf20Sopenharmony_ci	unsigned int i;
13128c2ecf20Sopenharmony_ci	int err;
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	/*
13158c2ecf20Sopenharmony_ci	 * The TVP5150 state is determined by the enabled sink pad link(s).
13168c2ecf20Sopenharmony_ci	 * Enabling or disabling the source pad link has no effect.
13178c2ecf20Sopenharmony_ci	 */
13188c2ecf20Sopenharmony_ci	if (tvp5150_pad->flags & MEDIA_PAD_FL_SOURCE)
13198c2ecf20Sopenharmony_ci		return 0;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	/* Check if the svideo connector should be enabled */
13228c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
13238c2ecf20Sopenharmony_ci		if (remote->entity == &decoder->connectors[i].ent) {
13248c2ecf20Sopenharmony_ci			v4l2c = &decoder->connectors[i].base;
13258c2ecf20Sopenharmony_ci			is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
13268c2ecf20Sopenharmony_ci			break;
13278c2ecf20Sopenharmony_ci		}
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug, "link setup '%s':%d->'%s':%d[%d]",
13318c2ecf20Sopenharmony_ci		    remote->entity->name, remote->index,
13328c2ecf20Sopenharmony_ci		    tvp5150_pad->entity->name, tvp5150_pad->index,
13338c2ecf20Sopenharmony_ci		    flags & MEDIA_LNK_FL_ENABLED);
13348c2ecf20Sopenharmony_ci	if (is_svideo)
13358c2ecf20Sopenharmony_ci		dev_dbg_lvl(sd->dev, 1, debug,
13368c2ecf20Sopenharmony_ci			    "link setup '%s':%d->'%s':%d[%d]",
13378c2ecf20Sopenharmony_ci			    remote->entity->name, remote->index,
13388c2ecf20Sopenharmony_ci			    other_tvp5150_pad->entity->name,
13398c2ecf20Sopenharmony_ci			    other_tvp5150_pad->index,
13408c2ecf20Sopenharmony_ci			    flags & MEDIA_LNK_FL_ENABLED);
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	/*
13438c2ecf20Sopenharmony_ci	 * The TVP5150 has an internal mux which allows the following setup:
13448c2ecf20Sopenharmony_ci	 *
13458c2ecf20Sopenharmony_ci	 * comp-connector1  --\
13468c2ecf20Sopenharmony_ci	 *		       |---> AIP1A
13478c2ecf20Sopenharmony_ci	 *		      /
13488c2ecf20Sopenharmony_ci	 * svideo-connector -|
13498c2ecf20Sopenharmony_ci	 *		      \
13508c2ecf20Sopenharmony_ci	 *		       |---> AIP1B
13518c2ecf20Sopenharmony_ci	 * comp-connector2  --/
13528c2ecf20Sopenharmony_ci	 *
13538c2ecf20Sopenharmony_ci	 * We can't rely on user space that the current connector gets disabled
13548c2ecf20Sopenharmony_ci	 * first before enabling the new connector. Disable all active
13558c2ecf20Sopenharmony_ci	 * connector links to be on the safe side.
13568c2ecf20Sopenharmony_ci	 */
13578c2ecf20Sopenharmony_ci	err = tvp5150_disable_all_input_links(decoder);
13588c2ecf20Sopenharmony_ci	if (err)
13598c2ecf20Sopenharmony_ci		return err;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	tvp5150_s_routing(sd, is_svideo ? TVP5150_SVIDEO : tvp5150_pad->index,
13628c2ecf20Sopenharmony_ci			  flags & MEDIA_LNK_FL_ENABLED ? TVP5150_NORMAL :
13638c2ecf20Sopenharmony_ci			  TVP5150_BLACK_SCREEN, 0);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	if (flags & MEDIA_LNK_FL_ENABLED) {
13668c2ecf20Sopenharmony_ci		struct v4l2_fwnode_connector_analog *v4l2ca;
13678c2ecf20Sopenharmony_ci		u32 new_norm;
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci		/*
13708c2ecf20Sopenharmony_ci		 * S-Video connector is conneted to both ports AIP1A and AIP1B.
13718c2ecf20Sopenharmony_ci		 * Both links must be enabled in one-shot regardless which link
13728c2ecf20Sopenharmony_ci		 * the user requests.
13738c2ecf20Sopenharmony_ci		 */
13748c2ecf20Sopenharmony_ci		if (is_svideo) {
13758c2ecf20Sopenharmony_ci			err = tvp5150_set_link((struct media_pad *)remote,
13768c2ecf20Sopenharmony_ci					       other_tvp5150_pad, flags);
13778c2ecf20Sopenharmony_ci			if (err)
13788c2ecf20Sopenharmony_ci				return err;
13798c2ecf20Sopenharmony_ci		}
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci		if (!decoder->connectors_num)
13828c2ecf20Sopenharmony_ci			return 0;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci		/* Update the current connector */
13858c2ecf20Sopenharmony_ci		decoder->cur_connector =
13868c2ecf20Sopenharmony_ci			container_of(remote, struct tvp5150_connector, pad);
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci		/*
13898c2ecf20Sopenharmony_ci		 * Do nothing if the new connector supports the same tv-norms as
13908c2ecf20Sopenharmony_ci		 * the old one.
13918c2ecf20Sopenharmony_ci		 */
13928c2ecf20Sopenharmony_ci		v4l2ca = &decoder->cur_connector->base.connector.analog;
13938c2ecf20Sopenharmony_ci		new_norm = decoder->norm & v4l2ca->sdtv_stds;
13948c2ecf20Sopenharmony_ci		if (decoder->norm == new_norm)
13958c2ecf20Sopenharmony_ci			return 0;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci		/*
13988c2ecf20Sopenharmony_ci		 * Fallback to the new connector tv-norms if we can't find any
13998c2ecf20Sopenharmony_ci		 * common between the current tv-norm and the new one.
14008c2ecf20Sopenharmony_ci		 */
14018c2ecf20Sopenharmony_ci		tvp5150_s_std(sd, new_norm ? new_norm : v4l2ca->sdtv_stds);
14028c2ecf20Sopenharmony_ci	}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	return 0;
14058c2ecf20Sopenharmony_ci}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_cistatic const struct media_entity_operations tvp5150_sd_media_ops = {
14088c2ecf20Sopenharmony_ci	.link_setup = tvp5150_link_setup,
14098c2ecf20Sopenharmony_ci};
14108c2ecf20Sopenharmony_ci#endif
14118c2ecf20Sopenharmony_ci/****************************************************************************
14128c2ecf20Sopenharmony_ci			I2C Command
14138c2ecf20Sopenharmony_ci ****************************************************************************/
14148c2ecf20Sopenharmony_cistatic int __maybe_unused tvp5150_runtime_suspend(struct device *dev)
14158c2ecf20Sopenharmony_ci{
14168c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
14178c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
14188c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	if (decoder->irq)
14218c2ecf20Sopenharmony_ci		/* Disable lock interrupt */
14228c2ecf20Sopenharmony_ci		return regmap_update_bits(decoder->regmap,
14238c2ecf20Sopenharmony_ci					  TVP5150_INT_ENABLE_REG_A,
14248c2ecf20Sopenharmony_ci					  TVP5150_INT_A_LOCK, 0);
14258c2ecf20Sopenharmony_ci	return 0;
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_cistatic int __maybe_unused tvp5150_runtime_resume(struct device *dev)
14298c2ecf20Sopenharmony_ci{
14308c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
14318c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
14328c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	if (decoder->irq)
14358c2ecf20Sopenharmony_ci		/* Enable lock interrupt */
14368c2ecf20Sopenharmony_ci		return regmap_update_bits(decoder->regmap,
14378c2ecf20Sopenharmony_ci					  TVP5150_INT_ENABLE_REG_A,
14388c2ecf20Sopenharmony_ci					  TVP5150_INT_A_LOCK,
14398c2ecf20Sopenharmony_ci					  TVP5150_INT_A_LOCK);
14408c2ecf20Sopenharmony_ci	return 0;
14418c2ecf20Sopenharmony_ci}
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_cistatic int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
14448c2ecf20Sopenharmony_ci{
14458c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
14468c2ecf20Sopenharmony_ci	unsigned int mask, val = 0;
14478c2ecf20Sopenharmony_ci	int ret;
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
14508c2ecf20Sopenharmony_ci	       TVP5150_MISC_CTL_CLOCK_OE;
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	if (enable) {
14538c2ecf20Sopenharmony_ci		ret = pm_runtime_get_sync(sd->dev);
14548c2ecf20Sopenharmony_ci		if (ret < 0) {
14558c2ecf20Sopenharmony_ci			pm_runtime_put_noidle(sd->dev);
14568c2ecf20Sopenharmony_ci			return ret;
14578c2ecf20Sopenharmony_ci		}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci		tvp5150_enable(sd);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci		/* Enable outputs if decoder is locked */
14628c2ecf20Sopenharmony_ci		if (decoder->irq)
14638c2ecf20Sopenharmony_ci			val = decoder->lock ? decoder->oe : 0;
14648c2ecf20Sopenharmony_ci		else
14658c2ecf20Sopenharmony_ci			val = decoder->oe;
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci		v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
14688c2ecf20Sopenharmony_ci	} else {
14698c2ecf20Sopenharmony_ci		pm_runtime_put(sd->dev);
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	return 0;
14758c2ecf20Sopenharmony_ci}
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_cistatic int tvp5150_s_routing(struct v4l2_subdev *sd,
14788c2ecf20Sopenharmony_ci			     u32 input, u32 output, u32 config)
14798c2ecf20Sopenharmony_ci{
14808c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	decoder->input = input;
14838c2ecf20Sopenharmony_ci	decoder->output = output;
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	if (output == TVP5150_BLACK_SCREEN)
14868c2ecf20Sopenharmony_ci		decoder->enable = false;
14878c2ecf20Sopenharmony_ci	else
14888c2ecf20Sopenharmony_ci		decoder->enable = true;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci	tvp5150_selmux(sd);
14918c2ecf20Sopenharmony_ci	return 0;
14928c2ecf20Sopenharmony_ci}
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_cistatic int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
14958c2ecf20Sopenharmony_ci{
14968c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	/*
14998c2ecf20Sopenharmony_ci	 * this is for capturing 36 raw vbi lines
15008c2ecf20Sopenharmony_ci	 * if there's a way to cut off the beginning 2 vbi lines
15018c2ecf20Sopenharmony_ci	 * with the tvp5150 then the vbi line count could be lowered
15028c2ecf20Sopenharmony_ci	 * to 17 lines/field again, although I couldn't find a register
15038c2ecf20Sopenharmony_ci	 * which could do that cropping
15048c2ecf20Sopenharmony_ci	 */
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	if (fmt->sample_format == V4L2_PIX_FMT_GREY)
15078c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_LUMA_PROC_CTL_1, 0x70);
15088c2ecf20Sopenharmony_ci	if (fmt->count[0] == 18 && fmt->count[1] == 18) {
15098c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START,
15108c2ecf20Sopenharmony_ci			     0x00);
15118c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 0x01);
15128c2ecf20Sopenharmony_ci	}
15138c2ecf20Sopenharmony_ci	return 0;
15148c2ecf20Sopenharmony_ci}
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_cistatic int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
15178c2ecf20Sopenharmony_ci{
15188c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
15198c2ecf20Sopenharmony_ci	int i;
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	if (svbi->service_set != 0) {
15228c2ecf20Sopenharmony_ci		for (i = 0; i <= 23; i++) {
15238c2ecf20Sopenharmony_ci			svbi->service_lines[1][i] = 0;
15248c2ecf20Sopenharmony_ci			svbi->service_lines[0][i] =
15258c2ecf20Sopenharmony_ci				tvp5150_set_vbi(sd, svbi->service_lines[0][i],
15268c2ecf20Sopenharmony_ci						0xf0, i, 3);
15278c2ecf20Sopenharmony_ci		}
15288c2ecf20Sopenharmony_ci		/* Enables FIFO */
15298c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 1);
15308c2ecf20Sopenharmony_ci	} else {
15318c2ecf20Sopenharmony_ci		/* Disables FIFO*/
15328c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 0);
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci		/* Disable Full Field */
15358c2ecf20Sopenharmony_ci		regmap_write(decoder->regmap, TVP5150_FULL_FIELD_ENA, 0);
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci		/* Disable Line modes */
15388c2ecf20Sopenharmony_ci		for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
15398c2ecf20Sopenharmony_ci			regmap_write(decoder->regmap, i, 0xff);
15408c2ecf20Sopenharmony_ci	}
15418c2ecf20Sopenharmony_ci	return 0;
15428c2ecf20Sopenharmony_ci}
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_cistatic int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
15458c2ecf20Sopenharmony_ci{
15468c2ecf20Sopenharmony_ci	int i, mask = 0;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	for (i = 0; i <= 23; i++) {
15518c2ecf20Sopenharmony_ci		svbi->service_lines[0][i] =
15528c2ecf20Sopenharmony_ci			tvp5150_get_vbi(sd, i);
15538c2ecf20Sopenharmony_ci		mask |= svbi->service_lines[0][i];
15548c2ecf20Sopenharmony_ci	}
15558c2ecf20Sopenharmony_ci	svbi->service_set = mask;
15568c2ecf20Sopenharmony_ci	return 0;
15578c2ecf20Sopenharmony_ci}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
15608c2ecf20Sopenharmony_cistatic int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
15618c2ecf20Sopenharmony_ci{
15628c2ecf20Sopenharmony_ci	int res;
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	res = tvp5150_read(sd, reg->reg & 0xff);
15658c2ecf20Sopenharmony_ci	if (res < 0) {
15668c2ecf20Sopenharmony_ci		dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res);
15678c2ecf20Sopenharmony_ci		return res;
15688c2ecf20Sopenharmony_ci	}
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	reg->val = res;
15718c2ecf20Sopenharmony_ci	reg->size = 1;
15728c2ecf20Sopenharmony_ci	return 0;
15738c2ecf20Sopenharmony_ci}
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_cistatic int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
15768c2ecf20Sopenharmony_ci{
15778c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	return regmap_write(decoder->regmap, reg->reg & 0xff, reg->val & 0xff);
15808c2ecf20Sopenharmony_ci}
15818c2ecf20Sopenharmony_ci#endif
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_cistatic int tvp5150_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
15848c2ecf20Sopenharmony_ci				   struct v4l2_event_subscription *sub)
15858c2ecf20Sopenharmony_ci{
15868c2ecf20Sopenharmony_ci	switch (sub->type) {
15878c2ecf20Sopenharmony_ci	case V4L2_EVENT_SOURCE_CHANGE:
15888c2ecf20Sopenharmony_ci		return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
15898c2ecf20Sopenharmony_ci	case V4L2_EVENT_CTRL:
15908c2ecf20Sopenharmony_ci		return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
15918c2ecf20Sopenharmony_ci	default:
15928c2ecf20Sopenharmony_ci		return -EINVAL;
15938c2ecf20Sopenharmony_ci	}
15948c2ecf20Sopenharmony_ci}
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_cistatic int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
15978c2ecf20Sopenharmony_ci{
15988c2ecf20Sopenharmony_ci	int status = tvp5150_read(sd, 0x88);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
16018c2ecf20Sopenharmony_ci	return 0;
16028c2ecf20Sopenharmony_ci}
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_cistatic int tvp5150_registered(struct v4l2_subdev *sd)
16058c2ecf20Sopenharmony_ci{
16068c2ecf20Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
16078c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
16088c2ecf20Sopenharmony_ci	unsigned int i;
16098c2ecf20Sopenharmony_ci	int ret;
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci	/*
16128c2ecf20Sopenharmony_ci	 * Setup connector pads and links. Enable the link to the first
16138c2ecf20Sopenharmony_ci	 * available connector per default.
16148c2ecf20Sopenharmony_ci	 */
16158c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
16168c2ecf20Sopenharmony_ci		struct media_entity *con = &decoder->connectors[i].ent;
16178c2ecf20Sopenharmony_ci		struct media_pad *pad = &decoder->connectors[i].pad;
16188c2ecf20Sopenharmony_ci		struct v4l2_fwnode_connector *v4l2c =
16198c2ecf20Sopenharmony_ci			&decoder->connectors[i].base;
16208c2ecf20Sopenharmony_ci		struct v4l2_connector_link *link =
16218c2ecf20Sopenharmony_ci			v4l2_connector_first_link(v4l2c);
16228c2ecf20Sopenharmony_ci		unsigned int port = link->fwnode_link.remote_port;
16238c2ecf20Sopenharmony_ci		unsigned int flags = i ? 0 : MEDIA_LNK_FL_ENABLED;
16248c2ecf20Sopenharmony_ci		bool is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci		pad->flags = MEDIA_PAD_FL_SOURCE;
16278c2ecf20Sopenharmony_ci		ret = media_entity_pads_init(con, 1, pad);
16288c2ecf20Sopenharmony_ci		if (ret < 0)
16298c2ecf20Sopenharmony_ci			goto err;
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci		ret = media_device_register_entity(sd->v4l2_dev->mdev, con);
16328c2ecf20Sopenharmony_ci		if (ret < 0)
16338c2ecf20Sopenharmony_ci			goto err;
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci		ret = media_create_pad_link(con, 0, &sd->entity, port, flags);
16368c2ecf20Sopenharmony_ci		if (ret < 0)
16378c2ecf20Sopenharmony_ci			goto err;
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci		if (is_svideo) {
16408c2ecf20Sopenharmony_ci			/*
16418c2ecf20Sopenharmony_ci			 * Check tvp5150_link_setup() comments for more
16428c2ecf20Sopenharmony_ci			 * information.
16438c2ecf20Sopenharmony_ci			 */
16448c2ecf20Sopenharmony_ci			link = v4l2_connector_last_link(v4l2c);
16458c2ecf20Sopenharmony_ci			port = link->fwnode_link.remote_port;
16468c2ecf20Sopenharmony_ci			ret = media_create_pad_link(con, 0, &sd->entity, port,
16478c2ecf20Sopenharmony_ci						    flags);
16488c2ecf20Sopenharmony_ci			if (ret < 0)
16498c2ecf20Sopenharmony_ci				goto err;
16508c2ecf20Sopenharmony_ci		}
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci		/* Enable default input. */
16538c2ecf20Sopenharmony_ci		if (flags == MEDIA_LNK_FL_ENABLED) {
16548c2ecf20Sopenharmony_ci			decoder->input =
16558c2ecf20Sopenharmony_ci				is_svideo ? TVP5150_SVIDEO :
16568c2ecf20Sopenharmony_ci				port == 0 ? TVP5150_COMPOSITE0 :
16578c2ecf20Sopenharmony_ci				TVP5150_COMPOSITE1;
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci			tvp5150_selmux(sd);
16608c2ecf20Sopenharmony_ci			decoder->cur_connector = &decoder->connectors[i];
16618c2ecf20Sopenharmony_ci			tvp5150_s_std(sd, v4l2c->connector.analog.sdtv_stds);
16628c2ecf20Sopenharmony_ci		}
16638c2ecf20Sopenharmony_ci	}
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci	return 0;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_cierr:
16688c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
16698c2ecf20Sopenharmony_ci		media_device_unregister_entity(&decoder->connectors[i].ent);
16708c2ecf20Sopenharmony_ci		media_entity_cleanup(&decoder->connectors[i].ent);
16718c2ecf20Sopenharmony_ci	}
16728c2ecf20Sopenharmony_ci	return ret;
16738c2ecf20Sopenharmony_ci#endif
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	return 0;
16768c2ecf20Sopenharmony_ci}
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_cistatic int tvp5150_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
16798c2ecf20Sopenharmony_ci{
16808c2ecf20Sopenharmony_ci	int ret;
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(sd->dev);
16838c2ecf20Sopenharmony_ci	if (ret < 0) {
16848c2ecf20Sopenharmony_ci		pm_runtime_put_noidle(sd->dev);
16858c2ecf20Sopenharmony_ci		return ret;
16868c2ecf20Sopenharmony_ci	}
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	return 0;
16898c2ecf20Sopenharmony_ci}
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_cistatic int tvp5150_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
16928c2ecf20Sopenharmony_ci{
16938c2ecf20Sopenharmony_ci	pm_runtime_put(sd->dev);
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	return 0;
16968c2ecf20Sopenharmony_ci}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
17018c2ecf20Sopenharmony_ci	.s_ctrl = tvp5150_s_ctrl,
17028c2ecf20Sopenharmony_ci};
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops tvp5150_core_ops = {
17058c2ecf20Sopenharmony_ci	.log_status = tvp5150_log_status,
17068c2ecf20Sopenharmony_ci	.reset = tvp5150_reset,
17078c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
17088c2ecf20Sopenharmony_ci	.g_register = tvp5150_g_register,
17098c2ecf20Sopenharmony_ci	.s_register = tvp5150_s_register,
17108c2ecf20Sopenharmony_ci#endif
17118c2ecf20Sopenharmony_ci	.subscribe_event = tvp5150_subscribe_event,
17128c2ecf20Sopenharmony_ci	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
17138c2ecf20Sopenharmony_ci};
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
17168c2ecf20Sopenharmony_ci	.g_tuner = tvp5150_g_tuner,
17178c2ecf20Sopenharmony_ci};
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops tvp5150_video_ops = {
17208c2ecf20Sopenharmony_ci	.s_std = tvp5150_s_std,
17218c2ecf20Sopenharmony_ci	.g_std = tvp5150_g_std,
17228c2ecf20Sopenharmony_ci	.querystd = tvp5150_querystd,
17238c2ecf20Sopenharmony_ci	.s_stream = tvp5150_s_stream,
17248c2ecf20Sopenharmony_ci	.s_routing = tvp5150_s_routing,
17258c2ecf20Sopenharmony_ci};
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
17288c2ecf20Sopenharmony_ci	.g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
17298c2ecf20Sopenharmony_ci	.g_sliced_fmt = tvp5150_g_sliced_fmt,
17308c2ecf20Sopenharmony_ci	.s_sliced_fmt = tvp5150_s_sliced_fmt,
17318c2ecf20Sopenharmony_ci	.s_raw_fmt = tvp5150_s_raw_fmt,
17328c2ecf20Sopenharmony_ci};
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
17358c2ecf20Sopenharmony_ci	.init_cfg = tvp5150_init_cfg,
17368c2ecf20Sopenharmony_ci	.enum_mbus_code = tvp5150_enum_mbus_code,
17378c2ecf20Sopenharmony_ci	.enum_frame_size = tvp5150_enum_frame_size,
17388c2ecf20Sopenharmony_ci	.set_fmt = tvp5150_fill_fmt,
17398c2ecf20Sopenharmony_ci	.get_fmt = tvp5150_fill_fmt,
17408c2ecf20Sopenharmony_ci	.get_selection = tvp5150_get_selection,
17418c2ecf20Sopenharmony_ci	.set_selection = tvp5150_set_selection,
17428c2ecf20Sopenharmony_ci	.get_mbus_config = tvp5150_get_mbus_config,
17438c2ecf20Sopenharmony_ci};
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops tvp5150_ops = {
17468c2ecf20Sopenharmony_ci	.core = &tvp5150_core_ops,
17478c2ecf20Sopenharmony_ci	.tuner = &tvp5150_tuner_ops,
17488c2ecf20Sopenharmony_ci	.video = &tvp5150_video_ops,
17498c2ecf20Sopenharmony_ci	.vbi = &tvp5150_vbi_ops,
17508c2ecf20Sopenharmony_ci	.pad = &tvp5150_pad_ops,
17518c2ecf20Sopenharmony_ci};
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
17548c2ecf20Sopenharmony_ci	.registered = tvp5150_registered,
17558c2ecf20Sopenharmony_ci	.open = tvp5150_open,
17568c2ecf20Sopenharmony_ci	.close = tvp5150_close,
17578c2ecf20Sopenharmony_ci};
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci/****************************************************************************
17608c2ecf20Sopenharmony_ci			I2C Client & Driver
17618c2ecf20Sopenharmony_ci ****************************************************************************/
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_cistatic const struct regmap_range tvp5150_readable_ranges[] = {
17648c2ecf20Sopenharmony_ci	{
17658c2ecf20Sopenharmony_ci		.range_min = TVP5150_VD_IN_SRC_SEL_1,
17668c2ecf20Sopenharmony_ci		.range_max = TVP5150_AUTOSW_MSK,
17678c2ecf20Sopenharmony_ci	}, {
17688c2ecf20Sopenharmony_ci		.range_min = TVP5150_COLOR_KIL_THSH_CTL,
17698c2ecf20Sopenharmony_ci		.range_max = TVP5150_CONF_SHARED_PIN,
17708c2ecf20Sopenharmony_ci	}, {
17718c2ecf20Sopenharmony_ci		.range_min = TVP5150_ACT_VD_CROP_ST_MSB,
17728c2ecf20Sopenharmony_ci		.range_max = TVP5150_HORIZ_SYNC_START,
17738c2ecf20Sopenharmony_ci	}, {
17748c2ecf20Sopenharmony_ci		.range_min = TVP5150_VERT_BLANKING_START,
17758c2ecf20Sopenharmony_ci		.range_max = TVP5150_INTT_CONFIG_REG_B,
17768c2ecf20Sopenharmony_ci	}, {
17778c2ecf20Sopenharmony_ci		.range_min = TVP5150_VIDEO_STD,
17788c2ecf20Sopenharmony_ci		.range_max = TVP5150_VIDEO_STD,
17798c2ecf20Sopenharmony_ci	}, {
17808c2ecf20Sopenharmony_ci		.range_min = TVP5150_CB_GAIN_FACT,
17818c2ecf20Sopenharmony_ci		.range_max = TVP5150_REV_SELECT,
17828c2ecf20Sopenharmony_ci	}, {
17838c2ecf20Sopenharmony_ci		.range_min = TVP5150_MSB_DEV_ID,
17848c2ecf20Sopenharmony_ci		.range_max = TVP5150_STATUS_REG_5,
17858c2ecf20Sopenharmony_ci	}, {
17868c2ecf20Sopenharmony_ci		.range_min = TVP5150_CC_DATA_INI,
17878c2ecf20Sopenharmony_ci		.range_max = TVP5150_TELETEXT_FIL_ENA,
17888c2ecf20Sopenharmony_ci	}, {
17898c2ecf20Sopenharmony_ci		.range_min = TVP5150_INT_STATUS_REG_A,
17908c2ecf20Sopenharmony_ci		.range_max = TVP5150_FIFO_OUT_CTRL,
17918c2ecf20Sopenharmony_ci	}, {
17928c2ecf20Sopenharmony_ci		.range_min = TVP5150_FULL_FIELD_ENA,
17938c2ecf20Sopenharmony_ci		.range_max = TVP5150_FULL_FIELD_MODE_REG,
17948c2ecf20Sopenharmony_ci	},
17958c2ecf20Sopenharmony_ci};
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_cistatic bool tvp5150_volatile_reg(struct device *dev, unsigned int reg)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	switch (reg) {
18008c2ecf20Sopenharmony_ci	case TVP5150_VERT_LN_COUNT_MSB:
18018c2ecf20Sopenharmony_ci	case TVP5150_VERT_LN_COUNT_LSB:
18028c2ecf20Sopenharmony_ci	case TVP5150_INT_STATUS_REG_A:
18038c2ecf20Sopenharmony_ci	case TVP5150_INT_STATUS_REG_B:
18048c2ecf20Sopenharmony_ci	case TVP5150_INT_ACTIVE_REG_B:
18058c2ecf20Sopenharmony_ci	case TVP5150_STATUS_REG_1:
18068c2ecf20Sopenharmony_ci	case TVP5150_STATUS_REG_2:
18078c2ecf20Sopenharmony_ci	case TVP5150_STATUS_REG_3:
18088c2ecf20Sopenharmony_ci	case TVP5150_STATUS_REG_4:
18098c2ecf20Sopenharmony_ci	case TVP5150_STATUS_REG_5:
18108c2ecf20Sopenharmony_ci	/* CC, WSS, VPS, VITC data? */
18118c2ecf20Sopenharmony_ci	case TVP5150_VBI_FIFO_READ_DATA:
18128c2ecf20Sopenharmony_ci	case TVP5150_VDP_STATUS_REG:
18138c2ecf20Sopenharmony_ci	case TVP5150_FIFO_WORD_COUNT:
18148c2ecf20Sopenharmony_ci		return true;
18158c2ecf20Sopenharmony_ci	default:
18168c2ecf20Sopenharmony_ci		return false;
18178c2ecf20Sopenharmony_ci	}
18188c2ecf20Sopenharmony_ci}
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_cistatic const struct regmap_access_table tvp5150_readable_table = {
18218c2ecf20Sopenharmony_ci	.yes_ranges = tvp5150_readable_ranges,
18228c2ecf20Sopenharmony_ci	.n_yes_ranges = ARRAY_SIZE(tvp5150_readable_ranges),
18238c2ecf20Sopenharmony_ci};
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_cistatic struct regmap_config tvp5150_config = {
18268c2ecf20Sopenharmony_ci	.reg_bits = 8,
18278c2ecf20Sopenharmony_ci	.val_bits = 8,
18288c2ecf20Sopenharmony_ci	.max_register = 0xff,
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ci	.rd_table = &tvp5150_readable_table,
18338c2ecf20Sopenharmony_ci	.volatile_reg = tvp5150_volatile_reg,
18348c2ecf20Sopenharmony_ci};
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_cistatic int tvp5150_detect_version(struct tvp5150 *core)
18378c2ecf20Sopenharmony_ci{
18388c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &core->sd;
18398c2ecf20Sopenharmony_ci	struct i2c_client *c = v4l2_get_subdevdata(sd);
18408c2ecf20Sopenharmony_ci	u8 regs[4];
18418c2ecf20Sopenharmony_ci	int res;
18428c2ecf20Sopenharmony_ci
18438c2ecf20Sopenharmony_ci	/*
18448c2ecf20Sopenharmony_ci	 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
18458c2ecf20Sopenharmony_ci	 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
18468c2ecf20Sopenharmony_ci	 */
18478c2ecf20Sopenharmony_ci	res = regmap_bulk_read(core->regmap, TVP5150_MSB_DEV_ID, regs, 4);
18488c2ecf20Sopenharmony_ci	if (res < 0) {
18498c2ecf20Sopenharmony_ci		dev_err(&c->dev, "reading ID registers failed: %d\n", res);
18508c2ecf20Sopenharmony_ci		return res;
18518c2ecf20Sopenharmony_ci	}
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	core->dev_id = (regs[0] << 8) | regs[1];
18548c2ecf20Sopenharmony_ci	core->rom_ver = (regs[2] << 8) | regs[3];
18558c2ecf20Sopenharmony_ci
18568c2ecf20Sopenharmony_ci	dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
18578c2ecf20Sopenharmony_ci		  core->dev_id, regs[2], regs[3], c->addr << 1,
18588c2ecf20Sopenharmony_ci		  c->adapter->name);
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
18618c2ecf20Sopenharmony_ci		dev_info(sd->dev, "tvp5150a detected.\n");
18628c2ecf20Sopenharmony_ci	} else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
18638c2ecf20Sopenharmony_ci		dev_info(sd->dev, "tvp5150am1 detected.\n");
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci		/* ITU-T BT.656.4 timing */
18668c2ecf20Sopenharmony_ci		regmap_write(core->regmap, TVP5150_REV_SELECT, 0);
18678c2ecf20Sopenharmony_ci	} else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
18688c2ecf20Sopenharmony_ci		dev_info(sd->dev, "tvp5151 detected.\n");
18698c2ecf20Sopenharmony_ci	} else {
18708c2ecf20Sopenharmony_ci		dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n",
18718c2ecf20Sopenharmony_ci			  core->dev_id);
18728c2ecf20Sopenharmony_ci	}
18738c2ecf20Sopenharmony_ci
18748c2ecf20Sopenharmony_ci	return 0;
18758c2ecf20Sopenharmony_ci}
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_cistatic int tvp5150_init(struct i2c_client *c)
18788c2ecf20Sopenharmony_ci{
18798c2ecf20Sopenharmony_ci	struct gpio_desc *pdn_gpio;
18808c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ci	pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
18838c2ecf20Sopenharmony_ci	if (IS_ERR(pdn_gpio))
18848c2ecf20Sopenharmony_ci		return PTR_ERR(pdn_gpio);
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	if (pdn_gpio) {
18878c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(pdn_gpio, 0);
18888c2ecf20Sopenharmony_ci		/* Delay time between power supplies active and reset */
18898c2ecf20Sopenharmony_ci		msleep(20);
18908c2ecf20Sopenharmony_ci	}
18918c2ecf20Sopenharmony_ci
18928c2ecf20Sopenharmony_ci	reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
18938c2ecf20Sopenharmony_ci	if (IS_ERR(reset_gpio))
18948c2ecf20Sopenharmony_ci		return PTR_ERR(reset_gpio);
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	if (reset_gpio) {
18978c2ecf20Sopenharmony_ci		/* RESETB pulse duration */
18988c2ecf20Sopenharmony_ci		ndelay(500);
18998c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(reset_gpio, 0);
19008c2ecf20Sopenharmony_ci		/* Delay time between end of reset to I2C active */
19018c2ecf20Sopenharmony_ci		usleep_range(200, 250);
19028c2ecf20Sopenharmony_ci	}
19038c2ecf20Sopenharmony_ci
19048c2ecf20Sopenharmony_ci	return 0;
19058c2ecf20Sopenharmony_ci}
19068c2ecf20Sopenharmony_ci
19078c2ecf20Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
19088c2ecf20Sopenharmony_cistatic int tvp5150_mc_init(struct tvp5150 *decoder)
19098c2ecf20Sopenharmony_ci{
19108c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &decoder->sd;
19118c2ecf20Sopenharmony_ci	unsigned int i;
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	sd->entity.ops = &tvp5150_sd_media_ops;
19148c2ecf20Sopenharmony_ci	sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci	for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
19178c2ecf20Sopenharmony_ci		decoder->pads[i].flags = MEDIA_PAD_FL_SINK;
19188c2ecf20Sopenharmony_ci		decoder->pads[i].sig_type = PAD_SIGNAL_ANALOG;
19198c2ecf20Sopenharmony_ci	}
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ci	decoder->pads[i].flags = MEDIA_PAD_FL_SOURCE;
19228c2ecf20Sopenharmony_ci	decoder->pads[i].sig_type = PAD_SIGNAL_DV;
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ci	return media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS,
19258c2ecf20Sopenharmony_ci				      decoder->pads);
19268c2ecf20Sopenharmony_ci}
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci#else /* !defined(CONFIG_MEDIA_CONTROLLER) */
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_cistatic inline int tvp5150_mc_init(struct tvp5150 *decoder)
19318c2ecf20Sopenharmony_ci{
19328c2ecf20Sopenharmony_ci	return 0;
19338c2ecf20Sopenharmony_ci}
19348c2ecf20Sopenharmony_ci#endif /* defined(CONFIG_MEDIA_CONTROLLER) */
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_cistatic int tvp5150_validate_connectors(struct tvp5150 *decoder)
19378c2ecf20Sopenharmony_ci{
19388c2ecf20Sopenharmony_ci	struct device *dev = decoder->sd.dev;
19398c2ecf20Sopenharmony_ci	struct tvp5150_connector *tvpc;
19408c2ecf20Sopenharmony_ci	struct v4l2_fwnode_connector *v4l2c;
19418c2ecf20Sopenharmony_ci	unsigned int i;
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_ci	if (!decoder->connectors_num) {
19448c2ecf20Sopenharmony_ci		dev_err(dev, "No valid connector found\n");
19458c2ecf20Sopenharmony_ci		return -ENODEV;
19468c2ecf20Sopenharmony_ci	}
19478c2ecf20Sopenharmony_ci
19488c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
19498c2ecf20Sopenharmony_ci		struct v4l2_connector_link *link0 = NULL;
19508c2ecf20Sopenharmony_ci		struct v4l2_connector_link *link1;
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci		tvpc = &decoder->connectors[i];
19538c2ecf20Sopenharmony_ci		v4l2c = &tvpc->base;
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci		if (v4l2c->type == V4L2_CONN_COMPOSITE) {
19568c2ecf20Sopenharmony_ci			if (v4l2c->nr_of_links != 1) {
19578c2ecf20Sopenharmony_ci				dev_err(dev, "Composite: connector needs 1 link\n");
19588c2ecf20Sopenharmony_ci				return -EINVAL;
19598c2ecf20Sopenharmony_ci			}
19608c2ecf20Sopenharmony_ci			link0 = v4l2_connector_first_link(v4l2c);
19618c2ecf20Sopenharmony_ci			if (!link0) {
19628c2ecf20Sopenharmony_ci				dev_err(dev, "Composite: invalid first link\n");
19638c2ecf20Sopenharmony_ci				return -EINVAL;
19648c2ecf20Sopenharmony_ci			}
19658c2ecf20Sopenharmony_ci			if (link0->fwnode_link.remote_id == 1) {
19668c2ecf20Sopenharmony_ci				dev_err(dev, "Composite: invalid endpoint id\n");
19678c2ecf20Sopenharmony_ci				return -EINVAL;
19688c2ecf20Sopenharmony_ci			}
19698c2ecf20Sopenharmony_ci		}
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci		if (v4l2c->type == V4L2_CONN_SVIDEO) {
19728c2ecf20Sopenharmony_ci			if (v4l2c->nr_of_links != 2) {
19738c2ecf20Sopenharmony_ci				dev_err(dev, "SVideo: connector needs 2 links\n");
19748c2ecf20Sopenharmony_ci				return -EINVAL;
19758c2ecf20Sopenharmony_ci			}
19768c2ecf20Sopenharmony_ci			link0 = v4l2_connector_first_link(v4l2c);
19778c2ecf20Sopenharmony_ci			if (!link0) {
19788c2ecf20Sopenharmony_ci				dev_err(dev, "SVideo: invalid first link\n");
19798c2ecf20Sopenharmony_ci				return -EINVAL;
19808c2ecf20Sopenharmony_ci			}
19818c2ecf20Sopenharmony_ci			link1 = v4l2_connector_last_link(v4l2c);
19828c2ecf20Sopenharmony_ci			if (link0->fwnode_link.remote_port ==
19838c2ecf20Sopenharmony_ci			    link1->fwnode_link.remote_port) {
19848c2ecf20Sopenharmony_ci				dev_err(dev, "SVideo: invalid link setup\n");
19858c2ecf20Sopenharmony_ci				return -EINVAL;
19868c2ecf20Sopenharmony_ci			}
19878c2ecf20Sopenharmony_ci		}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci		if (!(v4l2c->connector.analog.sdtv_stds & TVP5150_STD_MASK)) {
19908c2ecf20Sopenharmony_ci			dev_err(dev, "Unsupported tv-norm on connector %s\n",
19918c2ecf20Sopenharmony_ci				v4l2c->name);
19928c2ecf20Sopenharmony_ci			return -EINVAL;
19938c2ecf20Sopenharmony_ci		}
19948c2ecf20Sopenharmony_ci	}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	return 0;
19978c2ecf20Sopenharmony_ci}
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_cistatic int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
20008c2ecf20Sopenharmony_ci{
20018c2ecf20Sopenharmony_ci	struct device *dev = decoder->sd.dev;
20028c2ecf20Sopenharmony_ci	struct v4l2_fwnode_endpoint bus_cfg = {
20038c2ecf20Sopenharmony_ci		.bus_type = V4L2_MBUS_UNKNOWN
20048c2ecf20Sopenharmony_ci	};
20058c2ecf20Sopenharmony_ci	struct device_node *ep_np;
20068c2ecf20Sopenharmony_ci	struct tvp5150_connector *tvpc;
20078c2ecf20Sopenharmony_ci	struct v4l2_fwnode_connector *v4l2c;
20088c2ecf20Sopenharmony_ci	unsigned int flags, ep_num;
20098c2ecf20Sopenharmony_ci	unsigned int i;
20108c2ecf20Sopenharmony_ci	int ret;
20118c2ecf20Sopenharmony_ci
20128c2ecf20Sopenharmony_ci	/* At least 1 output and 1 input */
20138c2ecf20Sopenharmony_ci	ep_num = of_graph_get_endpoint_count(np);
20148c2ecf20Sopenharmony_ci	if (ep_num < 2 || ep_num > 5) {
20158c2ecf20Sopenharmony_ci		dev_err(dev, "At least 1 input and 1 output must be connected to the device.\n");
20168c2ecf20Sopenharmony_ci		return -EINVAL;
20178c2ecf20Sopenharmony_ci	}
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci	/* Layout if all connectors are used:
20208c2ecf20Sopenharmony_ci	 *
20218c2ecf20Sopenharmony_ci	 * tvp-5150 port@0 (AIP1A)
20228c2ecf20Sopenharmony_ci	 *	endpoint@0 -----------> Comp0-Con  port
20238c2ecf20Sopenharmony_ci	 *	endpoint@1 --------+--> Svideo-Con port
20248c2ecf20Sopenharmony_ci	 * tvp-5150 port@1 (AIP1B) |
20258c2ecf20Sopenharmony_ci	 *	endpoint@1 --------+
20268c2ecf20Sopenharmony_ci	 *	endpoint@0 -----------> Comp1-Con  port
20278c2ecf20Sopenharmony_ci	 * tvp-5150 port@2
20288c2ecf20Sopenharmony_ci	 *	endpoint (video bitstream output at YOUT[0-7] parallel bus)
20298c2ecf20Sopenharmony_ci	 */
20308c2ecf20Sopenharmony_ci	for_each_endpoint_of_node(np, ep_np) {
20318c2ecf20Sopenharmony_ci		struct fwnode_handle *ep_fwnode = of_fwnode_handle(ep_np);
20328c2ecf20Sopenharmony_ci		unsigned int next_connector = decoder->connectors_num;
20338c2ecf20Sopenharmony_ci		struct of_endpoint ep;
20348c2ecf20Sopenharmony_ci
20358c2ecf20Sopenharmony_ci		of_graph_parse_endpoint(ep_np, &ep);
20368c2ecf20Sopenharmony_ci		if (ep.port > 1 || ep.id > 1) {
20378c2ecf20Sopenharmony_ci			dev_dbg(dev, "Ignore connector on port@%u/ep@%u\n",
20388c2ecf20Sopenharmony_ci				ep.port, ep.id);
20398c2ecf20Sopenharmony_ci			continue;
20408c2ecf20Sopenharmony_ci		}
20418c2ecf20Sopenharmony_ci
20428c2ecf20Sopenharmony_ci		tvpc = &decoder->connectors[next_connector];
20438c2ecf20Sopenharmony_ci		v4l2c = &tvpc->base;
20448c2ecf20Sopenharmony_ci
20458c2ecf20Sopenharmony_ci		if (ep.port == 0 || (ep.port == 1 && ep.id == 0)) {
20468c2ecf20Sopenharmony_ci			ret = v4l2_fwnode_connector_parse(ep_fwnode, v4l2c);
20478c2ecf20Sopenharmony_ci			if (ret)
20488c2ecf20Sopenharmony_ci				goto err_put;
20498c2ecf20Sopenharmony_ci			ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
20508c2ecf20Sopenharmony_ci			if (ret)
20518c2ecf20Sopenharmony_ci				goto err_put;
20528c2ecf20Sopenharmony_ci			decoder->connectors_num++;
20538c2ecf20Sopenharmony_ci		} else {
20548c2ecf20Sopenharmony_ci			/* Adding the 2nd svideo link */
20558c2ecf20Sopenharmony_ci			for (i = 0; i < TVP5150_MAX_CONNECTORS; i++) {
20568c2ecf20Sopenharmony_ci				tvpc = &decoder->connectors[i];
20578c2ecf20Sopenharmony_ci				v4l2c = &tvpc->base;
20588c2ecf20Sopenharmony_ci				if (v4l2c->type == V4L2_CONN_SVIDEO)
20598c2ecf20Sopenharmony_ci					break;
20608c2ecf20Sopenharmony_ci			}
20618c2ecf20Sopenharmony_ci
20628c2ecf20Sopenharmony_ci			ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
20638c2ecf20Sopenharmony_ci			if (ret)
20648c2ecf20Sopenharmony_ci				goto err_put;
20658c2ecf20Sopenharmony_ci		}
20668c2ecf20Sopenharmony_ci	}
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci	ret = tvp5150_validate_connectors(decoder);
20698c2ecf20Sopenharmony_ci	if (ret)
20708c2ecf20Sopenharmony_ci		goto err_free;
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
20738c2ecf20Sopenharmony_ci		tvpc = &decoder->connectors[i];
20748c2ecf20Sopenharmony_ci		v4l2c = &tvpc->base;
20758c2ecf20Sopenharmony_ci		tvpc->ent.flags = MEDIA_ENT_FL_CONNECTOR;
20768c2ecf20Sopenharmony_ci		tvpc->ent.function = v4l2c->type == V4L2_CONN_SVIDEO ?
20778c2ecf20Sopenharmony_ci			MEDIA_ENT_F_CONN_SVIDEO : MEDIA_ENT_F_CONN_COMPOSITE;
20788c2ecf20Sopenharmony_ci		tvpc->ent.name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
20798c2ecf20Sopenharmony_ci						v4l2c->name, v4l2c->label ?
20808c2ecf20Sopenharmony_ci						v4l2c->label : "");
20818c2ecf20Sopenharmony_ci		if (!tvpc->ent.name) {
20828c2ecf20Sopenharmony_ci			ret = -ENOMEM;
20838c2ecf20Sopenharmony_ci			goto err_free;
20848c2ecf20Sopenharmony_ci		}
20858c2ecf20Sopenharmony_ci	}
20868c2ecf20Sopenharmony_ci
20878c2ecf20Sopenharmony_ci	ep_np = of_graph_get_endpoint_by_regs(np, TVP5150_PAD_VID_OUT, 0);
20888c2ecf20Sopenharmony_ci	if (!ep_np) {
20898c2ecf20Sopenharmony_ci		ret = -EINVAL;
20908c2ecf20Sopenharmony_ci		dev_err(dev, "Error no output endpoint available\n");
20918c2ecf20Sopenharmony_ci		goto err_free;
20928c2ecf20Sopenharmony_ci	}
20938c2ecf20Sopenharmony_ci	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_np), &bus_cfg);
20948c2ecf20Sopenharmony_ci	of_node_put(ep_np);
20958c2ecf20Sopenharmony_ci	if (ret)
20968c2ecf20Sopenharmony_ci		goto err_free;
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci	flags = bus_cfg.bus.parallel.flags;
20998c2ecf20Sopenharmony_ci	if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
21008c2ecf20Sopenharmony_ci	    !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
21018c2ecf20Sopenharmony_ci	      flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
21028c2ecf20Sopenharmony_ci	      flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
21038c2ecf20Sopenharmony_ci		ret = -EINVAL;
21048c2ecf20Sopenharmony_ci		goto err_free;
21058c2ecf20Sopenharmony_ci	}
21068c2ecf20Sopenharmony_ci
21078c2ecf20Sopenharmony_ci	decoder->mbus_type = bus_cfg.bus_type;
21088c2ecf20Sopenharmony_ci
21098c2ecf20Sopenharmony_ci	return 0;
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_cierr_put:
21128c2ecf20Sopenharmony_ci	of_node_put(ep_np);
21138c2ecf20Sopenharmony_cierr_free:
21148c2ecf20Sopenharmony_ci	for (i = 0; i < TVP5150_MAX_CONNECTORS; i++)
21158c2ecf20Sopenharmony_ci		v4l2_fwnode_connector_free(&decoder->connectors[i].base);
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci	return ret;
21188c2ecf20Sopenharmony_ci}
21198c2ecf20Sopenharmony_ci
21208c2ecf20Sopenharmony_cistatic const char * const tvp5150_test_patterns[2] = {
21218c2ecf20Sopenharmony_ci	"Disabled",
21228c2ecf20Sopenharmony_ci	"Black screen"
21238c2ecf20Sopenharmony_ci};
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_cistatic int tvp5150_probe(struct i2c_client *c)
21268c2ecf20Sopenharmony_ci{
21278c2ecf20Sopenharmony_ci	struct tvp5150 *core;
21288c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
21298c2ecf20Sopenharmony_ci	struct device_node *np = c->dev.of_node;
21308c2ecf20Sopenharmony_ci	struct regmap *map;
21318c2ecf20Sopenharmony_ci	unsigned int i;
21328c2ecf20Sopenharmony_ci	int res;
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci	/* Check if the adapter supports the needed features */
21358c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(c->adapter,
21368c2ecf20Sopenharmony_ci	     I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
21378c2ecf20Sopenharmony_ci		return -EIO;
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	res = tvp5150_init(c);
21408c2ecf20Sopenharmony_ci	if (res)
21418c2ecf20Sopenharmony_ci		return res;
21428c2ecf20Sopenharmony_ci
21438c2ecf20Sopenharmony_ci	core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
21448c2ecf20Sopenharmony_ci	if (!core)
21458c2ecf20Sopenharmony_ci		return -ENOMEM;
21468c2ecf20Sopenharmony_ci
21478c2ecf20Sopenharmony_ci	map = devm_regmap_init_i2c(c, &tvp5150_config);
21488c2ecf20Sopenharmony_ci	if (IS_ERR(map))
21498c2ecf20Sopenharmony_ci		return PTR_ERR(map);
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci	core->regmap = map;
21528c2ecf20Sopenharmony_ci	sd = &core->sd;
21538c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
21548c2ecf20Sopenharmony_ci	sd->internal_ops = &tvp5150_internal_ops;
21558c2ecf20Sopenharmony_ci	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
21568c2ecf20Sopenharmony_ci
21578c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_OF) && np) {
21588c2ecf20Sopenharmony_ci		res = tvp5150_parse_dt(core, np);
21598c2ecf20Sopenharmony_ci		if (res) {
21608c2ecf20Sopenharmony_ci			dev_err(sd->dev, "DT parsing error: %d\n", res);
21618c2ecf20Sopenharmony_ci			return res;
21628c2ecf20Sopenharmony_ci		}
21638c2ecf20Sopenharmony_ci	} else {
21648c2ecf20Sopenharmony_ci		/* Default to BT.656 embedded sync */
21658c2ecf20Sopenharmony_ci		core->mbus_type = V4L2_MBUS_BT656;
21668c2ecf20Sopenharmony_ci	}
21678c2ecf20Sopenharmony_ci
21688c2ecf20Sopenharmony_ci	res = tvp5150_mc_init(core);
21698c2ecf20Sopenharmony_ci	if (res)
21708c2ecf20Sopenharmony_ci		return res;
21718c2ecf20Sopenharmony_ci
21728c2ecf20Sopenharmony_ci	res = tvp5150_detect_version(core);
21738c2ecf20Sopenharmony_ci	if (res < 0)
21748c2ecf20Sopenharmony_ci		return res;
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	/*
21778c2ecf20Sopenharmony_ci	 * Iterate over all available connectors in case they are supported and
21788c2ecf20Sopenharmony_ci	 * successfully parsed. Fallback to default autodetect in case they
21798c2ecf20Sopenharmony_ci	 * aren't supported.
21808c2ecf20Sopenharmony_ci	 */
21818c2ecf20Sopenharmony_ci	for (i = 0; i < core->connectors_num; i++) {
21828c2ecf20Sopenharmony_ci		struct v4l2_fwnode_connector *v4l2c;
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci		v4l2c = &core->connectors[i].base;
21858c2ecf20Sopenharmony_ci		core->norm |= v4l2c->connector.analog.sdtv_stds;
21868c2ecf20Sopenharmony_ci	}
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_ci	if (!core->connectors_num)
21898c2ecf20Sopenharmony_ci		core->norm = V4L2_STD_ALL;
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	core->detected_norm = V4L2_STD_UNKNOWN;
21928c2ecf20Sopenharmony_ci	core->input = TVP5150_COMPOSITE1;
21938c2ecf20Sopenharmony_ci	core->enable = true;
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&core->hdl, 5);
21968c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
21978c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
21988c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
21998c2ecf20Sopenharmony_ci			V4L2_CID_CONTRAST, 0, 255, 1, 128);
22008c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
22018c2ecf20Sopenharmony_ci			V4L2_CID_SATURATION, 0, 255, 1, 128);
22028c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
22038c2ecf20Sopenharmony_ci			V4L2_CID_HUE, -128, 127, 1, 0);
22048c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
22058c2ecf20Sopenharmony_ci			V4L2_CID_PIXEL_RATE, 27000000,
22068c2ecf20Sopenharmony_ci			27000000, 1, 27000000);
22078c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
22088c2ecf20Sopenharmony_ci				     V4L2_CID_TEST_PATTERN,
22098c2ecf20Sopenharmony_ci				     ARRAY_SIZE(tvp5150_test_patterns) - 1,
22108c2ecf20Sopenharmony_ci				     0, 0, tvp5150_test_patterns);
22118c2ecf20Sopenharmony_ci	sd->ctrl_handler = &core->hdl;
22128c2ecf20Sopenharmony_ci	if (core->hdl.error) {
22138c2ecf20Sopenharmony_ci		res = core->hdl.error;
22148c2ecf20Sopenharmony_ci		goto err;
22158c2ecf20Sopenharmony_ci	}
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	tvp5150_set_default(tvp5150_read_std(sd), &core->rect);
22188c2ecf20Sopenharmony_ci
22198c2ecf20Sopenharmony_ci	core->irq = c->irq;
22208c2ecf20Sopenharmony_ci	tvp5150_reset(sd, 0);	/* Calls v4l2_ctrl_handler_setup() */
22218c2ecf20Sopenharmony_ci	if (c->irq) {
22228c2ecf20Sopenharmony_ci		res = devm_request_threaded_irq(&c->dev, c->irq, NULL,
22238c2ecf20Sopenharmony_ci						tvp5150_isr, IRQF_TRIGGER_HIGH |
22248c2ecf20Sopenharmony_ci						IRQF_ONESHOT, "tvp5150", core);
22258c2ecf20Sopenharmony_ci		if (res)
22268c2ecf20Sopenharmony_ci			goto err;
22278c2ecf20Sopenharmony_ci	}
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	res = v4l2_async_register_subdev(sd);
22308c2ecf20Sopenharmony_ci	if (res < 0)
22318c2ecf20Sopenharmony_ci		goto err;
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci	if (debug > 1)
22348c2ecf20Sopenharmony_ci		tvp5150_log_status(sd);
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci	pm_runtime_set_active(&c->dev);
22378c2ecf20Sopenharmony_ci	pm_runtime_enable(&c->dev);
22388c2ecf20Sopenharmony_ci	pm_runtime_idle(&c->dev);
22398c2ecf20Sopenharmony_ci
22408c2ecf20Sopenharmony_ci	return 0;
22418c2ecf20Sopenharmony_ci
22428c2ecf20Sopenharmony_cierr:
22438c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&core->hdl);
22448c2ecf20Sopenharmony_ci	return res;
22458c2ecf20Sopenharmony_ci}
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_cistatic int tvp5150_remove(struct i2c_client *c)
22488c2ecf20Sopenharmony_ci{
22498c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(c);
22508c2ecf20Sopenharmony_ci	struct tvp5150 *decoder = to_tvp5150(sd);
22518c2ecf20Sopenharmony_ci	unsigned int i;
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_ci	dev_dbg_lvl(sd->dev, 1, debug,
22548c2ecf20Sopenharmony_ci		"tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
22558c2ecf20Sopenharmony_ci		c->addr << 1);
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++)
22588c2ecf20Sopenharmony_ci		v4l2_fwnode_connector_free(&decoder->connectors[i].base);
22598c2ecf20Sopenharmony_ci	for (i = 0; i < decoder->connectors_num; i++) {
22608c2ecf20Sopenharmony_ci		media_device_unregister_entity(&decoder->connectors[i].ent);
22618c2ecf20Sopenharmony_ci		media_entity_cleanup(&decoder->connectors[i].ent);
22628c2ecf20Sopenharmony_ci	}
22638c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(sd);
22648c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&decoder->hdl);
22658c2ecf20Sopenharmony_ci	pm_runtime_disable(&c->dev);
22668c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&c->dev);
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	return 0;
22698c2ecf20Sopenharmony_ci}
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_cistatic const struct dev_pm_ops tvp5150_pm_ops = {
22748c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(tvp5150_runtime_suspend,
22758c2ecf20Sopenharmony_ci			   tvp5150_runtime_resume,
22768c2ecf20Sopenharmony_ci			   NULL)
22778c2ecf20Sopenharmony_ci};
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_cistatic const struct i2c_device_id tvp5150_id[] = {
22808c2ecf20Sopenharmony_ci	{ "tvp5150", 0 },
22818c2ecf20Sopenharmony_ci	{ }
22828c2ecf20Sopenharmony_ci};
22838c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tvp5150_id);
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF)
22868c2ecf20Sopenharmony_cistatic const struct of_device_id tvp5150_of_match[] = {
22878c2ecf20Sopenharmony_ci	{ .compatible = "ti,tvp5150", },
22888c2ecf20Sopenharmony_ci	{ /* sentinel */ },
22898c2ecf20Sopenharmony_ci};
22908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tvp5150_of_match);
22918c2ecf20Sopenharmony_ci#endif
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_cistatic struct i2c_driver tvp5150_driver = {
22948c2ecf20Sopenharmony_ci	.driver = {
22958c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(tvp5150_of_match),
22968c2ecf20Sopenharmony_ci		.name	= "tvp5150",
22978c2ecf20Sopenharmony_ci		.pm	= &tvp5150_pm_ops,
22988c2ecf20Sopenharmony_ci	},
22998c2ecf20Sopenharmony_ci	.probe_new	= tvp5150_probe,
23008c2ecf20Sopenharmony_ci	.remove		= tvp5150_remove,
23018c2ecf20Sopenharmony_ci	.id_table	= tvp5150_id,
23028c2ecf20Sopenharmony_ci};
23038c2ecf20Sopenharmony_ci
23048c2ecf20Sopenharmony_cimodule_i2c_driver(tvp5150_driver);
2305