18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci// saa711x - Philips SAA711x video decoder driver
38c2ecf20Sopenharmony_ci// This driver can work with saa7111, saa7111a, saa7113, saa7114,
48c2ecf20Sopenharmony_ci//			     saa7115 and saa7118.
58c2ecf20Sopenharmony_ci//
68c2ecf20Sopenharmony_ci// Based on saa7114 driver by Maxim Yevtyushkin, which is based on
78c2ecf20Sopenharmony_ci// the saa7111 driver by Dave Perks.
88c2ecf20Sopenharmony_ci//
98c2ecf20Sopenharmony_ci// Copyright (C) 1998 Dave Perks <dperks@ibm.net>
108c2ecf20Sopenharmony_ci// Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
118c2ecf20Sopenharmony_ci//
128c2ecf20Sopenharmony_ci// Slight changes for video timing and attachment output by
138c2ecf20Sopenharmony_ci// Wolfgang Scherr <scherr@net4you.net>
148c2ecf20Sopenharmony_ci//
158c2ecf20Sopenharmony_ci// Moved over to the linux >= 2.4.x i2c protocol (1/1/2003)
168c2ecf20Sopenharmony_ci// by Ronald Bultje <rbultje@ronald.bitfreak.net>
178c2ecf20Sopenharmony_ci//
188c2ecf20Sopenharmony_ci// Added saa7115 support by Kevin Thayer <nufan_wfk at yahoo.com>
198c2ecf20Sopenharmony_ci// (2/17/2003)
208c2ecf20Sopenharmony_ci//
218c2ecf20Sopenharmony_ci// VBI support (2004) and cleanups (2005) by Hans Verkuil <hverkuil@xs4all.nl>
228c2ecf20Sopenharmony_ci//
238c2ecf20Sopenharmony_ci// Copyright (c) 2005-2006 Mauro Carvalho Chehab <mchehab@kernel.org>
248c2ecf20Sopenharmony_ci//	SAA7111, SAA7113 and SAA7118 support
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "saa711x_regs.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/kernel.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/slab.h>
318c2ecf20Sopenharmony_ci#include <linux/i2c.h>
328c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
338c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
348c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
358c2ecf20Sopenharmony_ci#include <media/v4l2-mc.h>
368c2ecf20Sopenharmony_ci#include <media/i2c/saa7115.h>
378c2ecf20Sopenharmony_ci#include <asm/div64.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define VRES_60HZ	(480+16)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Philips SAA7111/SAA7113/SAA7114/SAA7115/SAA7118 video decoder driver");
428c2ecf20Sopenharmony_ciMODULE_AUTHOR(  "Maxim Yevtyushkin, Kevin Thayer, Chris Kennedy, "
438c2ecf20Sopenharmony_ci		"Hans Verkuil, Mauro Carvalho Chehab");
448c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic bool debug;
478c2ecf20Sopenharmony_cimodule_param(debug, bool, 0644);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-1)");
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cienum saa711x_model {
538c2ecf20Sopenharmony_ci	SAA7111A,
548c2ecf20Sopenharmony_ci	SAA7111,
558c2ecf20Sopenharmony_ci	SAA7113,
568c2ecf20Sopenharmony_ci	GM7113C,
578c2ecf20Sopenharmony_ci	SAA7114,
588c2ecf20Sopenharmony_ci	SAA7115,
598c2ecf20Sopenharmony_ci	SAA7118,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cienum saa711x_pads {
638c2ecf20Sopenharmony_ci	SAA711X_PAD_IF_INPUT,
648c2ecf20Sopenharmony_ci	SAA711X_PAD_VID_OUT,
658c2ecf20Sopenharmony_ci	SAA711X_NUM_PADS
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct saa711x_state {
698c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
708c2ecf20Sopenharmony_ci#ifdef CONFIG_MEDIA_CONTROLLER
718c2ecf20Sopenharmony_ci	struct media_pad pads[SAA711X_NUM_PADS];
728c2ecf20Sopenharmony_ci#endif
738c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	struct {
768c2ecf20Sopenharmony_ci		/* chroma gain control cluster */
778c2ecf20Sopenharmony_ci		struct v4l2_ctrl *agc;
788c2ecf20Sopenharmony_ci		struct v4l2_ctrl *gain;
798c2ecf20Sopenharmony_ci	};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	v4l2_std_id std;
828c2ecf20Sopenharmony_ci	int input;
838c2ecf20Sopenharmony_ci	int output;
848c2ecf20Sopenharmony_ci	int enable;
858c2ecf20Sopenharmony_ci	int radio;
868c2ecf20Sopenharmony_ci	int width;
878c2ecf20Sopenharmony_ci	int height;
888c2ecf20Sopenharmony_ci	enum saa711x_model ident;
898c2ecf20Sopenharmony_ci	u32 audclk_freq;
908c2ecf20Sopenharmony_ci	u32 crystal_freq;
918c2ecf20Sopenharmony_ci	bool ucgc;
928c2ecf20Sopenharmony_ci	u8 cgcdiv;
938c2ecf20Sopenharmony_ci	bool apll;
948c2ecf20Sopenharmony_ci	bool double_asclk;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic inline struct saa711x_state *to_state(struct v4l2_subdev *sd)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	return container_of(sd, struct saa711x_state, sd);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	return &container_of(ctrl->handler, struct saa711x_state, hdl)->sd;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline int saa711x_write(struct v4l2_subdev *sd, u8 reg, u8 value)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, reg, value);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* Sanity routine to check if a register is present */
1178c2ecf20Sopenharmony_cistatic int saa711x_has_reg(const int id, const u8 reg)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	if (id == SAA7111)
1208c2ecf20Sopenharmony_ci		return reg < 0x20 && reg != 0x01 && reg != 0x0f &&
1218c2ecf20Sopenharmony_ci		       (reg < 0x13 || reg > 0x19) && reg != 0x1d && reg != 0x1e;
1228c2ecf20Sopenharmony_ci	if (id == SAA7111A)
1238c2ecf20Sopenharmony_ci		return reg < 0x20 && reg != 0x01 && reg != 0x0f &&
1248c2ecf20Sopenharmony_ci		       reg != 0x14 && reg != 0x18 && reg != 0x19 &&
1258c2ecf20Sopenharmony_ci		       reg != 0x1d && reg != 0x1e;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* common for saa7113/4/5/8 */
1288c2ecf20Sopenharmony_ci	if (unlikely((reg >= 0x3b && reg <= 0x3f) || reg == 0x5c || reg == 0x5f ||
1298c2ecf20Sopenharmony_ci	    reg == 0xa3 || reg == 0xa7 || reg == 0xab || reg == 0xaf || (reg >= 0xb5 && reg <= 0xb7) ||
1308c2ecf20Sopenharmony_ci	    reg == 0xd3 || reg == 0xd7 || reg == 0xdb || reg == 0xdf || (reg >= 0xe5 && reg <= 0xe7) ||
1318c2ecf20Sopenharmony_ci	    reg == 0x82 || (reg >= 0x89 && reg <= 0x8e)))
1328c2ecf20Sopenharmony_ci		return 0;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	switch (id) {
1358c2ecf20Sopenharmony_ci	case GM7113C:
1368c2ecf20Sopenharmony_ci		return reg != 0x14 && (reg < 0x18 || reg > 0x1e) && reg < 0x20;
1378c2ecf20Sopenharmony_ci	case SAA7113:
1388c2ecf20Sopenharmony_ci		return reg != 0x14 && (reg < 0x18 || reg > 0x1e) && (reg < 0x20 || reg > 0x3f) &&
1398c2ecf20Sopenharmony_ci		       reg != 0x5d && reg < 0x63;
1408c2ecf20Sopenharmony_ci	case SAA7114:
1418c2ecf20Sopenharmony_ci		return (reg < 0x1a || reg > 0x1e) && (reg < 0x20 || reg > 0x2f) &&
1428c2ecf20Sopenharmony_ci		       (reg < 0x63 || reg > 0x7f) && reg != 0x33 && reg != 0x37 &&
1438c2ecf20Sopenharmony_ci		       reg != 0x81 && reg < 0xf0;
1448c2ecf20Sopenharmony_ci	case SAA7115:
1458c2ecf20Sopenharmony_ci		return (reg < 0x20 || reg > 0x2f) && reg != 0x65 && (reg < 0xfc || reg > 0xfe);
1468c2ecf20Sopenharmony_ci	case SAA7118:
1478c2ecf20Sopenharmony_ci		return (reg < 0x1a || reg > 0x1d) && (reg < 0x20 || reg > 0x22) &&
1488c2ecf20Sopenharmony_ci		       (reg < 0x26 || reg > 0x28) && reg != 0x33 && reg != 0x37 &&
1498c2ecf20Sopenharmony_ci		       (reg < 0x63 || reg > 0x7f) && reg != 0x81 && reg < 0xf0;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	return 1;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int saa711x_writeregs(struct v4l2_subdev *sd, const unsigned char *regs)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
1578c2ecf20Sopenharmony_ci	unsigned char reg, data;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	while (*regs != 0x00) {
1608c2ecf20Sopenharmony_ci		reg = *(regs++);
1618c2ecf20Sopenharmony_ci		data = *(regs++);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		/* According with datasheets, reserved regs should be
1648c2ecf20Sopenharmony_ci		   filled with 0 - seems better not to touch on they */
1658c2ecf20Sopenharmony_ci		if (saa711x_has_reg(state->ident, reg)) {
1668c2ecf20Sopenharmony_ci			if (saa711x_write(sd, reg, data) < 0)
1678c2ecf20Sopenharmony_ci				return -1;
1688c2ecf20Sopenharmony_ci		} else {
1698c2ecf20Sopenharmony_ci			v4l2_dbg(1, debug, sd, "tried to access reserved reg 0x%02x\n", reg);
1708c2ecf20Sopenharmony_ci		}
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic inline int saa711x_read(struct v4l2_subdev *sd, u8 reg)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, reg);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* SAA7111 initialization table */
1858c2ecf20Sopenharmony_cistatic const unsigned char saa7111_init[] = {
1868c2ecf20Sopenharmony_ci	R_01_INC_DELAY, 0x00,		/* reserved */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/*front end */
1898c2ecf20Sopenharmony_ci	R_02_INPUT_CNTL_1, 0xd0,	/* FUSE=3, GUDL=2, MODE=0 */
1908c2ecf20Sopenharmony_ci	R_03_INPUT_CNTL_2, 0x23,	/* HLNRS=0, VBSL=1, WPOFF=0, HOLDG=0,
1918c2ecf20Sopenharmony_ci					 * GAFIX=0, GAI1=256, GAI2=256 */
1928c2ecf20Sopenharmony_ci	R_04_INPUT_CNTL_3, 0x00,	/* GAI1=256 */
1938c2ecf20Sopenharmony_ci	R_05_INPUT_CNTL_4, 0x00,	/* GAI2=256 */
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/* decoder */
1968c2ecf20Sopenharmony_ci	R_06_H_SYNC_START, 0xf3,	/* HSB at  13(50Hz) /  17(60Hz)
1978c2ecf20Sopenharmony_ci					 * pixels after end of last line */
1988c2ecf20Sopenharmony_ci	R_07_H_SYNC_STOP, 0xe8,		/* HSS seems to be needed to
1998c2ecf20Sopenharmony_ci					 * work with NTSC, too */
2008c2ecf20Sopenharmony_ci	R_08_SYNC_CNTL, 0xc8,		/* AUFD=1, FSEL=1, EXFIL=0,
2018c2ecf20Sopenharmony_ci					 * VTRC=1, HPLL=0, VNOI=0 */
2028c2ecf20Sopenharmony_ci	R_09_LUMA_CNTL, 0x01,		/* BYPS=0, PREF=0, BPSS=0,
2038c2ecf20Sopenharmony_ci					 * VBLB=0, UPTCV=0, APER=1 */
2048c2ecf20Sopenharmony_ci	R_0A_LUMA_BRIGHT_CNTL, 0x80,
2058c2ecf20Sopenharmony_ci	R_0B_LUMA_CONTRAST_CNTL, 0x47,	/* 0b - CONT=1.109 */
2068c2ecf20Sopenharmony_ci	R_0C_CHROMA_SAT_CNTL, 0x40,
2078c2ecf20Sopenharmony_ci	R_0D_CHROMA_HUE_CNTL, 0x00,
2088c2ecf20Sopenharmony_ci	R_0E_CHROMA_CNTL_1, 0x01,	/* 0e - CDTO=0, CSTD=0, DCCF=0,
2098c2ecf20Sopenharmony_ci					 * FCTC=0, CHBW=1 */
2108c2ecf20Sopenharmony_ci	R_0F_CHROMA_GAIN_CNTL, 0x00,	/* reserved */
2118c2ecf20Sopenharmony_ci	R_10_CHROMA_CNTL_2, 0x48,	/* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
2128c2ecf20Sopenharmony_ci	R_11_MODE_DELAY_CNTL, 0x1c,	/* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
2138c2ecf20Sopenharmony_ci					 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
2148c2ecf20Sopenharmony_ci	R_12_RT_SIGNAL_CNTL, 0x00,	/* 12 - output control 2 */
2158c2ecf20Sopenharmony_ci	R_13_RT_X_PORT_OUT_CNTL, 0x00,	/* 13 - output control 3 */
2168c2ecf20Sopenharmony_ci	R_14_ANAL_ADC_COMPAT_CNTL, 0x00,
2178c2ecf20Sopenharmony_ci	R_15_VGATE_START_FID_CHG, 0x00,
2188c2ecf20Sopenharmony_ci	R_16_VGATE_STOP, 0x00,
2198c2ecf20Sopenharmony_ci	R_17_MISC_VGATE_CONF_AND_MSB, 0x00,
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	0x00, 0x00
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * This table has one illegal value, and some values that are not
2268c2ecf20Sopenharmony_ci * correct according to the datasheet initialization table.
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci *  If you need a table with legal/default values tell the driver in
2298c2ecf20Sopenharmony_ci *  i2c_board_info.platform_data, and you will get the gm7113c_init
2308c2ecf20Sopenharmony_ci *  table instead.
2318c2ecf20Sopenharmony_ci */
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/* SAA7113 Init codes */
2348c2ecf20Sopenharmony_cistatic const unsigned char saa7113_init[] = {
2358c2ecf20Sopenharmony_ci	R_01_INC_DELAY, 0x08,
2368c2ecf20Sopenharmony_ci	R_02_INPUT_CNTL_1, 0xc2,
2378c2ecf20Sopenharmony_ci	R_03_INPUT_CNTL_2, 0x30,
2388c2ecf20Sopenharmony_ci	R_04_INPUT_CNTL_3, 0x00,
2398c2ecf20Sopenharmony_ci	R_05_INPUT_CNTL_4, 0x00,
2408c2ecf20Sopenharmony_ci	R_06_H_SYNC_START, 0x89,	/* Illegal value -119,
2418c2ecf20Sopenharmony_ci					 * min. value = -108 (0x94) */
2428c2ecf20Sopenharmony_ci	R_07_H_SYNC_STOP, 0x0d,
2438c2ecf20Sopenharmony_ci	R_08_SYNC_CNTL, 0x88,		/* Not datasheet default.
2448c2ecf20Sopenharmony_ci					 * HTC = VTR mode, should be 0x98 */
2458c2ecf20Sopenharmony_ci	R_09_LUMA_CNTL, 0x01,
2468c2ecf20Sopenharmony_ci	R_0A_LUMA_BRIGHT_CNTL, 0x80,
2478c2ecf20Sopenharmony_ci	R_0B_LUMA_CONTRAST_CNTL, 0x47,
2488c2ecf20Sopenharmony_ci	R_0C_CHROMA_SAT_CNTL, 0x40,
2498c2ecf20Sopenharmony_ci	R_0D_CHROMA_HUE_CNTL, 0x00,
2508c2ecf20Sopenharmony_ci	R_0E_CHROMA_CNTL_1, 0x01,
2518c2ecf20Sopenharmony_ci	R_0F_CHROMA_GAIN_CNTL, 0x2a,
2528c2ecf20Sopenharmony_ci	R_10_CHROMA_CNTL_2, 0x08,	/* Not datsheet default.
2538c2ecf20Sopenharmony_ci					 * VRLN enabled, should be 0x00 */
2548c2ecf20Sopenharmony_ci	R_11_MODE_DELAY_CNTL, 0x0c,
2558c2ecf20Sopenharmony_ci	R_12_RT_SIGNAL_CNTL, 0x07,	/* Not datasheet default,
2568c2ecf20Sopenharmony_ci					 * should be 0x01 */
2578c2ecf20Sopenharmony_ci	R_13_RT_X_PORT_OUT_CNTL, 0x00,
2588c2ecf20Sopenharmony_ci	R_14_ANAL_ADC_COMPAT_CNTL, 0x00,
2598c2ecf20Sopenharmony_ci	R_15_VGATE_START_FID_CHG, 0x00,
2608c2ecf20Sopenharmony_ci	R_16_VGATE_STOP, 0x00,
2618c2ecf20Sopenharmony_ci	R_17_MISC_VGATE_CONF_AND_MSB, 0x00,
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	0x00, 0x00
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/*
2678c2ecf20Sopenharmony_ci * GM7113C is a clone of the SAA7113 chip
2688c2ecf20Sopenharmony_ci *  This init table is copied out of the saa7113 datasheet.
2698c2ecf20Sopenharmony_ci *  In R_08 we enable "Automatic Field Detection" [AUFD],
2708c2ecf20Sopenharmony_ci *  this is disabled when saa711x_set_v4lstd is called.
2718c2ecf20Sopenharmony_ci */
2728c2ecf20Sopenharmony_cistatic const unsigned char gm7113c_init[] = {
2738c2ecf20Sopenharmony_ci	R_01_INC_DELAY, 0x08,
2748c2ecf20Sopenharmony_ci	R_02_INPUT_CNTL_1, 0xc0,
2758c2ecf20Sopenharmony_ci	R_03_INPUT_CNTL_2, 0x33,
2768c2ecf20Sopenharmony_ci	R_04_INPUT_CNTL_3, 0x00,
2778c2ecf20Sopenharmony_ci	R_05_INPUT_CNTL_4, 0x00,
2788c2ecf20Sopenharmony_ci	R_06_H_SYNC_START, 0xe9,
2798c2ecf20Sopenharmony_ci	R_07_H_SYNC_STOP, 0x0d,
2808c2ecf20Sopenharmony_ci	R_08_SYNC_CNTL, 0x98,
2818c2ecf20Sopenharmony_ci	R_09_LUMA_CNTL, 0x01,
2828c2ecf20Sopenharmony_ci	R_0A_LUMA_BRIGHT_CNTL, 0x80,
2838c2ecf20Sopenharmony_ci	R_0B_LUMA_CONTRAST_CNTL, 0x47,
2848c2ecf20Sopenharmony_ci	R_0C_CHROMA_SAT_CNTL, 0x40,
2858c2ecf20Sopenharmony_ci	R_0D_CHROMA_HUE_CNTL, 0x00,
2868c2ecf20Sopenharmony_ci	R_0E_CHROMA_CNTL_1, 0x01,
2878c2ecf20Sopenharmony_ci	R_0F_CHROMA_GAIN_CNTL, 0x2a,
2888c2ecf20Sopenharmony_ci	R_10_CHROMA_CNTL_2, 0x00,
2898c2ecf20Sopenharmony_ci	R_11_MODE_DELAY_CNTL, 0x0c,
2908c2ecf20Sopenharmony_ci	R_12_RT_SIGNAL_CNTL, 0x01,
2918c2ecf20Sopenharmony_ci	R_13_RT_X_PORT_OUT_CNTL, 0x00,
2928c2ecf20Sopenharmony_ci	R_14_ANAL_ADC_COMPAT_CNTL, 0x00,
2938c2ecf20Sopenharmony_ci	R_15_VGATE_START_FID_CHG, 0x00,
2948c2ecf20Sopenharmony_ci	R_16_VGATE_STOP, 0x00,
2958c2ecf20Sopenharmony_ci	R_17_MISC_VGATE_CONF_AND_MSB, 0x00,
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	0x00, 0x00
2988c2ecf20Sopenharmony_ci};
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/* If a value differs from the Hauppauge driver values, then the comment starts with
3018c2ecf20Sopenharmony_ci   'was 0xXX' to denote the Hauppauge value. Otherwise the value is identical to what the
3028c2ecf20Sopenharmony_ci   Hauppauge driver sets. */
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/* SAA7114 and SAA7115 initialization table */
3058c2ecf20Sopenharmony_cistatic const unsigned char saa7115_init_auto_input[] = {
3068c2ecf20Sopenharmony_ci		/* Front-End Part */
3078c2ecf20Sopenharmony_ci	R_01_INC_DELAY, 0x48,			/* white peak control disabled */
3088c2ecf20Sopenharmony_ci	R_03_INPUT_CNTL_2, 0x20,		/* was 0x30. 0x20: long vertical blanking */
3098c2ecf20Sopenharmony_ci	R_04_INPUT_CNTL_3, 0x90,		/* analog gain set to 0 */
3108c2ecf20Sopenharmony_ci	R_05_INPUT_CNTL_4, 0x90,		/* analog gain set to 0 */
3118c2ecf20Sopenharmony_ci		/* Decoder Part */
3128c2ecf20Sopenharmony_ci	R_06_H_SYNC_START, 0xeb,		/* horiz sync begin = -21 */
3138c2ecf20Sopenharmony_ci	R_07_H_SYNC_STOP, 0xe0,			/* horiz sync stop = -17 */
3148c2ecf20Sopenharmony_ci	R_09_LUMA_CNTL, 0x53,			/* 0x53, was 0x56 for 60hz. luminance control */
3158c2ecf20Sopenharmony_ci	R_0A_LUMA_BRIGHT_CNTL, 0x80,		/* was 0x88. decoder brightness, 0x80 is itu standard */
3168c2ecf20Sopenharmony_ci	R_0B_LUMA_CONTRAST_CNTL, 0x44,		/* was 0x48. decoder contrast, 0x44 is itu standard */
3178c2ecf20Sopenharmony_ci	R_0C_CHROMA_SAT_CNTL, 0x40,		/* was 0x47. decoder saturation, 0x40 is itu standard */
3188c2ecf20Sopenharmony_ci	R_0D_CHROMA_HUE_CNTL, 0x00,
3198c2ecf20Sopenharmony_ci	R_0F_CHROMA_GAIN_CNTL, 0x00,		/* use automatic gain  */
3208c2ecf20Sopenharmony_ci	R_10_CHROMA_CNTL_2, 0x06,		/* chroma: active adaptive combfilter */
3218c2ecf20Sopenharmony_ci	R_11_MODE_DELAY_CNTL, 0x00,
3228c2ecf20Sopenharmony_ci	R_12_RT_SIGNAL_CNTL, 0x9d,		/* RTS0 output control: VGATE */
3238c2ecf20Sopenharmony_ci	R_13_RT_X_PORT_OUT_CNTL, 0x80,		/* ITU656 standard mode, RTCO output enable RTCE */
3248c2ecf20Sopenharmony_ci	R_14_ANAL_ADC_COMPAT_CNTL, 0x00,
3258c2ecf20Sopenharmony_ci	R_18_RAW_DATA_GAIN_CNTL, 0x40,		/* gain 0x00 = nominal */
3268c2ecf20Sopenharmony_ci	R_19_RAW_DATA_OFF_CNTL, 0x80,
3278c2ecf20Sopenharmony_ci	R_1A_COLOR_KILL_LVL_CNTL, 0x77,		/* recommended value */
3288c2ecf20Sopenharmony_ci	R_1B_MISC_TVVCRDET, 0x42,		/* recommended value */
3298c2ecf20Sopenharmony_ci	R_1C_ENHAN_COMB_CTRL1, 0xa9,		/* recommended value */
3308c2ecf20Sopenharmony_ci	R_1D_ENHAN_COMB_CTRL2, 0x01,		/* recommended value */
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x0,		/* No tasks enabled at init */
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		/* Power Device Control */
3368c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,	/* reset device */
3378c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xf0,	/* set device programmed, all in operational mode */
3388c2ecf20Sopenharmony_ci	0x00, 0x00
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/* Used to reset saa7113, saa7114 and saa7115 */
3428c2ecf20Sopenharmony_cistatic const unsigned char saa7115_cfg_reset_scaler[] = {
3438c2ecf20Sopenharmony_ci	R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, 0x00,	/* disable I-port output */
3448c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,		/* reset scaler */
3458c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xf0,		/* activate scaler */
3468c2ecf20Sopenharmony_ci	R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, 0x01,	/* enable I-port output */
3478c2ecf20Sopenharmony_ci	0x00, 0x00
3488c2ecf20Sopenharmony_ci};
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci/* ============== SAA7715 VIDEO templates =============  */
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic const unsigned char saa7115_cfg_60hz_video[] = {
3538c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x00,			/* reset tasks */
3548c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,		/* reset scaler */
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	R_15_VGATE_START_FID_CHG, 0x03,
3578c2ecf20Sopenharmony_ci	R_16_VGATE_STOP, 0x11,
3588c2ecf20Sopenharmony_ci	R_17_MISC_VGATE_CONF_AND_MSB, 0x9c,
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	R_08_SYNC_CNTL, 0x68,			/* 0xBO: auto detection, 0x68 = NTSC */
3618c2ecf20Sopenharmony_ci	R_0E_CHROMA_CNTL_1, 0x07,		/* video autodetection is on */
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	R_5A_V_OFF_FOR_SLICER, 0x06,		/* standard 60hz value for ITU656 line counting */
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	/* Task A */
3668c2ecf20Sopenharmony_ci	R_90_A_TASK_HANDLING_CNTL, 0x80,
3678c2ecf20Sopenharmony_ci	R_91_A_X_PORT_FORMATS_AND_CONF, 0x48,
3688c2ecf20Sopenharmony_ci	R_92_A_X_PORT_INPUT_REFERENCE_SIGNAL, 0x40,
3698c2ecf20Sopenharmony_ci	R_93_A_I_PORT_OUTPUT_FORMATS_AND_CONF, 0x84,
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* hoffset low (input), 0x0002 is minimum */
3728c2ecf20Sopenharmony_ci	R_94_A_HORIZ_INPUT_WINDOW_START, 0x01,
3738c2ecf20Sopenharmony_ci	R_95_A_HORIZ_INPUT_WINDOW_START_MSB, 0x00,
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	/* hsize low (input), 0x02d0 = 720 */
3768c2ecf20Sopenharmony_ci	R_96_A_HORIZ_INPUT_WINDOW_LENGTH, 0xd0,
3778c2ecf20Sopenharmony_ci	R_97_A_HORIZ_INPUT_WINDOW_LENGTH_MSB, 0x02,
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	R_98_A_VERT_INPUT_WINDOW_START, 0x05,
3808c2ecf20Sopenharmony_ci	R_99_A_VERT_INPUT_WINDOW_START_MSB, 0x00,
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	R_9A_A_VERT_INPUT_WINDOW_LENGTH, 0x0c,
3838c2ecf20Sopenharmony_ci	R_9B_A_VERT_INPUT_WINDOW_LENGTH_MSB, 0x00,
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	R_9C_A_HORIZ_OUTPUT_WINDOW_LENGTH, 0xa0,
3868c2ecf20Sopenharmony_ci	R_9D_A_HORIZ_OUTPUT_WINDOW_LENGTH_MSB, 0x05,
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	R_9E_A_VERT_OUTPUT_WINDOW_LENGTH, 0x0c,
3898c2ecf20Sopenharmony_ci	R_9F_A_VERT_OUTPUT_WINDOW_LENGTH_MSB, 0x00,
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/* Task B */
3928c2ecf20Sopenharmony_ci	R_C0_B_TASK_HANDLING_CNTL, 0x00,
3938c2ecf20Sopenharmony_ci	R_C1_B_X_PORT_FORMATS_AND_CONF, 0x08,
3948c2ecf20Sopenharmony_ci	R_C2_B_INPUT_REFERENCE_SIGNAL_DEFINITION, 0x00,
3958c2ecf20Sopenharmony_ci	R_C3_B_I_PORT_FORMATS_AND_CONF, 0x80,
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	/* 0x0002 is minimum */
3988c2ecf20Sopenharmony_ci	R_C4_B_HORIZ_INPUT_WINDOW_START, 0x02,
3998c2ecf20Sopenharmony_ci	R_C5_B_HORIZ_INPUT_WINDOW_START_MSB, 0x00,
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/* 0x02d0 = 720 */
4028c2ecf20Sopenharmony_ci	R_C6_B_HORIZ_INPUT_WINDOW_LENGTH, 0xd0,
4038c2ecf20Sopenharmony_ci	R_C7_B_HORIZ_INPUT_WINDOW_LENGTH_MSB, 0x02,
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* vwindow start 0x12 = 18 */
4068c2ecf20Sopenharmony_ci	R_C8_B_VERT_INPUT_WINDOW_START, 0x12,
4078c2ecf20Sopenharmony_ci	R_C9_B_VERT_INPUT_WINDOW_START_MSB, 0x00,
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* vwindow length 0xf8 = 248 */
4108c2ecf20Sopenharmony_ci	R_CA_B_VERT_INPUT_WINDOW_LENGTH, VRES_60HZ>>1,
4118c2ecf20Sopenharmony_ci	R_CB_B_VERT_INPUT_WINDOW_LENGTH_MSB, VRES_60HZ>>9,
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/* hwindow 0x02d0 = 720 */
4148c2ecf20Sopenharmony_ci	R_CC_B_HORIZ_OUTPUT_WINDOW_LENGTH, 0xd0,
4158c2ecf20Sopenharmony_ci	R_CD_B_HORIZ_OUTPUT_WINDOW_LENGTH_MSB, 0x02,
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	R_F0_LFCO_PER_LINE, 0xad,		/* Set PLL Register. 60hz 525 lines per frame, 27 MHz */
4188c2ecf20Sopenharmony_ci	R_F1_P_I_PARAM_SELECT, 0x05,		/* low bit with 0xF0 */
4198c2ecf20Sopenharmony_ci	R_F5_PULSGEN_LINE_LENGTH, 0xad,
4208c2ecf20Sopenharmony_ci	R_F6_PULSE_A_POS_LSB_AND_PULSEGEN_CONFIG, 0x01,
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	0x00, 0x00
4238c2ecf20Sopenharmony_ci};
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic const unsigned char saa7115_cfg_50hz_video[] = {
4268c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x00,
4278c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,	/* reset scaler */
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	R_15_VGATE_START_FID_CHG, 0x37,		/* VGATE start */
4308c2ecf20Sopenharmony_ci	R_16_VGATE_STOP, 0x16,
4318c2ecf20Sopenharmony_ci	R_17_MISC_VGATE_CONF_AND_MSB, 0x99,
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	R_08_SYNC_CNTL, 0x28,			/* 0x28 = PAL */
4348c2ecf20Sopenharmony_ci	R_0E_CHROMA_CNTL_1, 0x07,
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	R_5A_V_OFF_FOR_SLICER, 0x03,		/* standard 50hz value */
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Task A */
4398c2ecf20Sopenharmony_ci	R_90_A_TASK_HANDLING_CNTL, 0x81,
4408c2ecf20Sopenharmony_ci	R_91_A_X_PORT_FORMATS_AND_CONF, 0x48,
4418c2ecf20Sopenharmony_ci	R_92_A_X_PORT_INPUT_REFERENCE_SIGNAL, 0x40,
4428c2ecf20Sopenharmony_ci	R_93_A_I_PORT_OUTPUT_FORMATS_AND_CONF, 0x84,
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* This is weird: the datasheet says that you should use 2 as the minimum value, */
4458c2ecf20Sopenharmony_ci	/* but Hauppauge uses 0, and changing that to 2 causes indeed problems (for 50hz) */
4468c2ecf20Sopenharmony_ci	/* hoffset low (input), 0x0002 is minimum */
4478c2ecf20Sopenharmony_ci	R_94_A_HORIZ_INPUT_WINDOW_START, 0x00,
4488c2ecf20Sopenharmony_ci	R_95_A_HORIZ_INPUT_WINDOW_START_MSB, 0x00,
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* hsize low (input), 0x02d0 = 720 */
4518c2ecf20Sopenharmony_ci	R_96_A_HORIZ_INPUT_WINDOW_LENGTH, 0xd0,
4528c2ecf20Sopenharmony_ci	R_97_A_HORIZ_INPUT_WINDOW_LENGTH_MSB, 0x02,
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	R_98_A_VERT_INPUT_WINDOW_START, 0x03,
4558c2ecf20Sopenharmony_ci	R_99_A_VERT_INPUT_WINDOW_START_MSB, 0x00,
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* vsize 0x12 = 18 */
4588c2ecf20Sopenharmony_ci	R_9A_A_VERT_INPUT_WINDOW_LENGTH, 0x12,
4598c2ecf20Sopenharmony_ci	R_9B_A_VERT_INPUT_WINDOW_LENGTH_MSB, 0x00,
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	/* hsize 0x05a0 = 1440 */
4628c2ecf20Sopenharmony_ci	R_9C_A_HORIZ_OUTPUT_WINDOW_LENGTH, 0xa0,
4638c2ecf20Sopenharmony_ci	R_9D_A_HORIZ_OUTPUT_WINDOW_LENGTH_MSB, 0x05,	/* hsize hi (output) */
4648c2ecf20Sopenharmony_ci	R_9E_A_VERT_OUTPUT_WINDOW_LENGTH, 0x12,		/* vsize low (output), 0x12 = 18 */
4658c2ecf20Sopenharmony_ci	R_9F_A_VERT_OUTPUT_WINDOW_LENGTH_MSB, 0x00,	/* vsize hi (output) */
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/* Task B */
4688c2ecf20Sopenharmony_ci	R_C0_B_TASK_HANDLING_CNTL, 0x00,
4698c2ecf20Sopenharmony_ci	R_C1_B_X_PORT_FORMATS_AND_CONF, 0x08,
4708c2ecf20Sopenharmony_ci	R_C2_B_INPUT_REFERENCE_SIGNAL_DEFINITION, 0x00,
4718c2ecf20Sopenharmony_ci	R_C3_B_I_PORT_FORMATS_AND_CONF, 0x80,
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* This is weird: the datasheet says that you should use 2 as the minimum value, */
4748c2ecf20Sopenharmony_ci	/* but Hauppauge uses 0, and changing that to 2 causes indeed problems (for 50hz) */
4758c2ecf20Sopenharmony_ci	/* hoffset low (input), 0x0002 is minimum. See comment above. */
4768c2ecf20Sopenharmony_ci	R_C4_B_HORIZ_INPUT_WINDOW_START, 0x00,
4778c2ecf20Sopenharmony_ci	R_C5_B_HORIZ_INPUT_WINDOW_START_MSB, 0x00,
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/* hsize 0x02d0 = 720 */
4808c2ecf20Sopenharmony_ci	R_C6_B_HORIZ_INPUT_WINDOW_LENGTH, 0xd0,
4818c2ecf20Sopenharmony_ci	R_C7_B_HORIZ_INPUT_WINDOW_LENGTH_MSB, 0x02,
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* voffset 0x16 = 22 */
4848c2ecf20Sopenharmony_ci	R_C8_B_VERT_INPUT_WINDOW_START, 0x16,
4858c2ecf20Sopenharmony_ci	R_C9_B_VERT_INPUT_WINDOW_START_MSB, 0x00,
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/* vsize 0x0120 = 288 */
4888c2ecf20Sopenharmony_ci	R_CA_B_VERT_INPUT_WINDOW_LENGTH, 0x20,
4898c2ecf20Sopenharmony_ci	R_CB_B_VERT_INPUT_WINDOW_LENGTH_MSB, 0x01,
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* hsize 0x02d0 = 720 */
4928c2ecf20Sopenharmony_ci	R_CC_B_HORIZ_OUTPUT_WINDOW_LENGTH, 0xd0,
4938c2ecf20Sopenharmony_ci	R_CD_B_HORIZ_OUTPUT_WINDOW_LENGTH_MSB, 0x02,
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	R_F0_LFCO_PER_LINE, 0xb0,		/* Set PLL Register. 50hz 625 lines per frame, 27 MHz */
4968c2ecf20Sopenharmony_ci	R_F1_P_I_PARAM_SELECT, 0x05,		/* low bit with 0xF0, (was 0x05) */
4978c2ecf20Sopenharmony_ci	R_F5_PULSGEN_LINE_LENGTH, 0xb0,
4988c2ecf20Sopenharmony_ci	R_F6_PULSE_A_POS_LSB_AND_PULSEGEN_CONFIG, 0x01,
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	0x00, 0x00
5018c2ecf20Sopenharmony_ci};
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/* ============== SAA7715 VIDEO templates (end) =======  */
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic const unsigned char saa7115_cfg_vbi_on[] = {
5068c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x00,			/* reset tasks */
5078c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,		/* reset scaler */
5088c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x30,			/* Activate both tasks */
5098c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xf0,		/* activate scaler */
5108c2ecf20Sopenharmony_ci	R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, 0x01,	/* Enable I-port output */
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	0x00, 0x00
5138c2ecf20Sopenharmony_ci};
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic const unsigned char saa7115_cfg_vbi_off[] = {
5168c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x00,			/* reset tasks */
5178c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,		/* reset scaler */
5188c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x20,			/* Activate only task "B" */
5198c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xf0,		/* activate scaler */
5208c2ecf20Sopenharmony_ci	R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, 0x01,	/* Enable I-port output */
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	0x00, 0x00
5238c2ecf20Sopenharmony_ci};
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic const unsigned char saa7115_init_misc[] = {
5278c2ecf20Sopenharmony_ci	R_81_V_SYNC_FLD_ID_SRC_SEL_AND_RETIMED_V_F, 0x01,
5288c2ecf20Sopenharmony_ci	R_83_X_PORT_I_O_ENA_AND_OUT_CLK, 0x01,
5298c2ecf20Sopenharmony_ci	R_84_I_PORT_SIGNAL_DEF, 0x20,
5308c2ecf20Sopenharmony_ci	R_85_I_PORT_SIGNAL_POLAR, 0x21,
5318c2ecf20Sopenharmony_ci	R_86_I_PORT_FIFO_FLAG_CNTL_AND_ARBIT, 0xc5,
5328c2ecf20Sopenharmony_ci	R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, 0x01,
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	/* Task A */
5358c2ecf20Sopenharmony_ci	R_A0_A_HORIZ_PRESCALING, 0x01,
5368c2ecf20Sopenharmony_ci	R_A1_A_ACCUMULATION_LENGTH, 0x00,
5378c2ecf20Sopenharmony_ci	R_A2_A_PRESCALER_DC_GAIN_AND_FIR_PREFILTER, 0x00,
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Configure controls at nominal value*/
5408c2ecf20Sopenharmony_ci	R_A4_A_LUMA_BRIGHTNESS_CNTL, 0x80,
5418c2ecf20Sopenharmony_ci	R_A5_A_LUMA_CONTRAST_CNTL, 0x40,
5428c2ecf20Sopenharmony_ci	R_A6_A_CHROMA_SATURATION_CNTL, 0x40,
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/* note: 2 x zoom ensures that VBI lines have same length as video lines. */
5458c2ecf20Sopenharmony_ci	R_A8_A_HORIZ_LUMA_SCALING_INC, 0x00,
5468c2ecf20Sopenharmony_ci	R_A9_A_HORIZ_LUMA_SCALING_INC_MSB, 0x02,
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	R_AA_A_HORIZ_LUMA_PHASE_OFF, 0x00,
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* must be horiz lum scaling / 2 */
5518c2ecf20Sopenharmony_ci	R_AC_A_HORIZ_CHROMA_SCALING_INC, 0x00,
5528c2ecf20Sopenharmony_ci	R_AD_A_HORIZ_CHROMA_SCALING_INC_MSB, 0x01,
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	/* must be offset luma / 2 */
5558c2ecf20Sopenharmony_ci	R_AE_A_HORIZ_CHROMA_PHASE_OFF, 0x00,
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	R_B0_A_VERT_LUMA_SCALING_INC, 0x00,
5588c2ecf20Sopenharmony_ci	R_B1_A_VERT_LUMA_SCALING_INC_MSB, 0x04,
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	R_B2_A_VERT_CHROMA_SCALING_INC, 0x00,
5618c2ecf20Sopenharmony_ci	R_B3_A_VERT_CHROMA_SCALING_INC_MSB, 0x04,
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	R_B4_A_VERT_SCALING_MODE_CNTL, 0x01,
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	R_B8_A_VERT_CHROMA_PHASE_OFF_00, 0x00,
5668c2ecf20Sopenharmony_ci	R_B9_A_VERT_CHROMA_PHASE_OFF_01, 0x00,
5678c2ecf20Sopenharmony_ci	R_BA_A_VERT_CHROMA_PHASE_OFF_10, 0x00,
5688c2ecf20Sopenharmony_ci	R_BB_A_VERT_CHROMA_PHASE_OFF_11, 0x00,
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	R_BC_A_VERT_LUMA_PHASE_OFF_00, 0x00,
5718c2ecf20Sopenharmony_ci	R_BD_A_VERT_LUMA_PHASE_OFF_01, 0x00,
5728c2ecf20Sopenharmony_ci	R_BE_A_VERT_LUMA_PHASE_OFF_10, 0x00,
5738c2ecf20Sopenharmony_ci	R_BF_A_VERT_LUMA_PHASE_OFF_11, 0x00,
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/* Task B */
5768c2ecf20Sopenharmony_ci	R_D0_B_HORIZ_PRESCALING, 0x01,
5778c2ecf20Sopenharmony_ci	R_D1_B_ACCUMULATION_LENGTH, 0x00,
5788c2ecf20Sopenharmony_ci	R_D2_B_PRESCALER_DC_GAIN_AND_FIR_PREFILTER, 0x00,
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* Configure controls at nominal value*/
5818c2ecf20Sopenharmony_ci	R_D4_B_LUMA_BRIGHTNESS_CNTL, 0x80,
5828c2ecf20Sopenharmony_ci	R_D5_B_LUMA_CONTRAST_CNTL, 0x40,
5838c2ecf20Sopenharmony_ci	R_D6_B_CHROMA_SATURATION_CNTL, 0x40,
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* hor lum scaling 0x0400 = 1 */
5868c2ecf20Sopenharmony_ci	R_D8_B_HORIZ_LUMA_SCALING_INC, 0x00,
5878c2ecf20Sopenharmony_ci	R_D9_B_HORIZ_LUMA_SCALING_INC_MSB, 0x04,
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	R_DA_B_HORIZ_LUMA_PHASE_OFF, 0x00,
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* must be hor lum scaling / 2 */
5928c2ecf20Sopenharmony_ci	R_DC_B_HORIZ_CHROMA_SCALING, 0x00,
5938c2ecf20Sopenharmony_ci	R_DD_B_HORIZ_CHROMA_SCALING_MSB, 0x02,
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/* must be offset luma / 2 */
5968c2ecf20Sopenharmony_ci	R_DE_B_HORIZ_PHASE_OFFSET_CRHOMA, 0x00,
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	R_E0_B_VERT_LUMA_SCALING_INC, 0x00,
5998c2ecf20Sopenharmony_ci	R_E1_B_VERT_LUMA_SCALING_INC_MSB, 0x04,
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	R_E2_B_VERT_CHROMA_SCALING_INC, 0x00,
6028c2ecf20Sopenharmony_ci	R_E3_B_VERT_CHROMA_SCALING_INC_MSB, 0x04,
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	R_E4_B_VERT_SCALING_MODE_CNTL, 0x01,
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	R_E8_B_VERT_CHROMA_PHASE_OFF_00, 0x00,
6078c2ecf20Sopenharmony_ci	R_E9_B_VERT_CHROMA_PHASE_OFF_01, 0x00,
6088c2ecf20Sopenharmony_ci	R_EA_B_VERT_CHROMA_PHASE_OFF_10, 0x00,
6098c2ecf20Sopenharmony_ci	R_EB_B_VERT_CHROMA_PHASE_OFF_11, 0x00,
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	R_EC_B_VERT_LUMA_PHASE_OFF_00, 0x00,
6128c2ecf20Sopenharmony_ci	R_ED_B_VERT_LUMA_PHASE_OFF_01, 0x00,
6138c2ecf20Sopenharmony_ci	R_EE_B_VERT_LUMA_PHASE_OFF_10, 0x00,
6148c2ecf20Sopenharmony_ci	R_EF_B_VERT_LUMA_PHASE_OFF_11, 0x00,
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	R_F2_NOMINAL_PLL2_DTO, 0x50,		/* crystal clock = 24.576 MHz, target = 27MHz */
6178c2ecf20Sopenharmony_ci	R_F3_PLL_INCREMENT, 0x46,
6188c2ecf20Sopenharmony_ci	R_F4_PLL2_STATUS, 0x00,
6198c2ecf20Sopenharmony_ci	R_F7_PULSE_A_POS_MSB, 0x4b,		/* not the recommended settings! */
6208c2ecf20Sopenharmony_ci	R_F8_PULSE_B_POS, 0x00,
6218c2ecf20Sopenharmony_ci	R_F9_PULSE_B_POS_MSB, 0x4b,
6228c2ecf20Sopenharmony_ci	R_FA_PULSE_C_POS, 0x00,
6238c2ecf20Sopenharmony_ci	R_FB_PULSE_C_POS_MSB, 0x4b,
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* PLL2 lock detection settings: 71 lines 50% phase error */
6268c2ecf20Sopenharmony_ci	R_FF_S_PLL_MAX_PHASE_ERR_THRESH_NUM_LINES, 0x88,
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	/* Turn off VBI */
6298c2ecf20Sopenharmony_ci	R_40_SLICER_CNTL_1, 0x20,             /* No framing code errors allowed. */
6308c2ecf20Sopenharmony_ci	R_41_LCR_BASE, 0xff,
6318c2ecf20Sopenharmony_ci	R_41_LCR_BASE+1, 0xff,
6328c2ecf20Sopenharmony_ci	R_41_LCR_BASE+2, 0xff,
6338c2ecf20Sopenharmony_ci	R_41_LCR_BASE+3, 0xff,
6348c2ecf20Sopenharmony_ci	R_41_LCR_BASE+4, 0xff,
6358c2ecf20Sopenharmony_ci	R_41_LCR_BASE+5, 0xff,
6368c2ecf20Sopenharmony_ci	R_41_LCR_BASE+6, 0xff,
6378c2ecf20Sopenharmony_ci	R_41_LCR_BASE+7, 0xff,
6388c2ecf20Sopenharmony_ci	R_41_LCR_BASE+8, 0xff,
6398c2ecf20Sopenharmony_ci	R_41_LCR_BASE+9, 0xff,
6408c2ecf20Sopenharmony_ci	R_41_LCR_BASE+10, 0xff,
6418c2ecf20Sopenharmony_ci	R_41_LCR_BASE+11, 0xff,
6428c2ecf20Sopenharmony_ci	R_41_LCR_BASE+12, 0xff,
6438c2ecf20Sopenharmony_ci	R_41_LCR_BASE+13, 0xff,
6448c2ecf20Sopenharmony_ci	R_41_LCR_BASE+14, 0xff,
6458c2ecf20Sopenharmony_ci	R_41_LCR_BASE+15, 0xff,
6468c2ecf20Sopenharmony_ci	R_41_LCR_BASE+16, 0xff,
6478c2ecf20Sopenharmony_ci	R_41_LCR_BASE+17, 0xff,
6488c2ecf20Sopenharmony_ci	R_41_LCR_BASE+18, 0xff,
6498c2ecf20Sopenharmony_ci	R_41_LCR_BASE+19, 0xff,
6508c2ecf20Sopenharmony_ci	R_41_LCR_BASE+20, 0xff,
6518c2ecf20Sopenharmony_ci	R_41_LCR_BASE+21, 0xff,
6528c2ecf20Sopenharmony_ci	R_41_LCR_BASE+22, 0xff,
6538c2ecf20Sopenharmony_ci	R_58_PROGRAM_FRAMING_CODE, 0x40,
6548c2ecf20Sopenharmony_ci	R_59_H_OFF_FOR_SLICER, 0x47,
6558c2ecf20Sopenharmony_ci	R_5B_FLD_OFF_AND_MSB_FOR_H_AND_V_OFF, 0x83,
6568c2ecf20Sopenharmony_ci	R_5D_DID, 0xbd,
6578c2ecf20Sopenharmony_ci	R_5E_SDID, 0x35,
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	R_02_INPUT_CNTL_1, 0xc4, /* input tuner -> input 4, amplifier active */
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	R_80_GLOBAL_CNTL_1, 0x20,		/* enable task B */
6628c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xd0,
6638c2ecf20Sopenharmony_ci	R_88_POWER_SAVE_ADC_PORT_CNTL, 0xf0,
6648c2ecf20Sopenharmony_ci	0x00, 0x00
6658c2ecf20Sopenharmony_ci};
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic int saa711x_odd_parity(u8 c)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	c ^= (c >> 4);
6708c2ecf20Sopenharmony_ci	c ^= (c >> 2);
6718c2ecf20Sopenharmony_ci	c ^= (c >> 1);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	return c & 1;
6748c2ecf20Sopenharmony_ci}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_cistatic int saa711x_decode_vps(u8 *dst, u8 *p)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	static const u8 biphase_tbl[] = {
6798c2ecf20Sopenharmony_ci		0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
6808c2ecf20Sopenharmony_ci		0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
6818c2ecf20Sopenharmony_ci		0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
6828c2ecf20Sopenharmony_ci		0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
6838c2ecf20Sopenharmony_ci		0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
6848c2ecf20Sopenharmony_ci		0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
6858c2ecf20Sopenharmony_ci		0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
6868c2ecf20Sopenharmony_ci		0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
6878c2ecf20Sopenharmony_ci		0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
6888c2ecf20Sopenharmony_ci		0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
6898c2ecf20Sopenharmony_ci		0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
6908c2ecf20Sopenharmony_ci		0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
6918c2ecf20Sopenharmony_ci		0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
6928c2ecf20Sopenharmony_ci		0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
6938c2ecf20Sopenharmony_ci		0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
6948c2ecf20Sopenharmony_ci		0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
6958c2ecf20Sopenharmony_ci		0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
6968c2ecf20Sopenharmony_ci		0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
6978c2ecf20Sopenharmony_ci		0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
6988c2ecf20Sopenharmony_ci		0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
6998c2ecf20Sopenharmony_ci		0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
7008c2ecf20Sopenharmony_ci		0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
7018c2ecf20Sopenharmony_ci		0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
7028c2ecf20Sopenharmony_ci		0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
7038c2ecf20Sopenharmony_ci		0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
7048c2ecf20Sopenharmony_ci		0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
7058c2ecf20Sopenharmony_ci		0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
7068c2ecf20Sopenharmony_ci		0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
7078c2ecf20Sopenharmony_ci		0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
7088c2ecf20Sopenharmony_ci		0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
7098c2ecf20Sopenharmony_ci		0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
7108c2ecf20Sopenharmony_ci		0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
7118c2ecf20Sopenharmony_ci	};
7128c2ecf20Sopenharmony_ci	int i;
7138c2ecf20Sopenharmony_ci	u8 c, err = 0;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	for (i = 0; i < 2 * 13; i += 2) {
7168c2ecf20Sopenharmony_ci		err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
7178c2ecf20Sopenharmony_ci		c = (biphase_tbl[p[i + 1]] & 0xf) | ((biphase_tbl[p[i]] & 0xf) << 4);
7188c2ecf20Sopenharmony_ci		dst[i / 2] = c;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci	return err & 0xf0;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic int saa711x_decode_wss(u8 *p)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	static const int wss_bits[8] = {
7268c2ecf20Sopenharmony_ci		0, 0, 0, 1, 0, 1, 1, 1
7278c2ecf20Sopenharmony_ci	};
7288c2ecf20Sopenharmony_ci	unsigned char parity;
7298c2ecf20Sopenharmony_ci	int wss = 0;
7308c2ecf20Sopenharmony_ci	int i;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++) {
7338c2ecf20Sopenharmony_ci		int b1 = wss_bits[p[i] & 7];
7348c2ecf20Sopenharmony_ci		int b2 = wss_bits[(p[i] >> 3) & 7];
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		if (b1 == b2)
7378c2ecf20Sopenharmony_ci			return -1;
7388c2ecf20Sopenharmony_ci		wss |= b2 << i;
7398c2ecf20Sopenharmony_ci	}
7408c2ecf20Sopenharmony_ci	parity = wss & 15;
7418c2ecf20Sopenharmony_ci	parity ^= parity >> 2;
7428c2ecf20Sopenharmony_ci	parity ^= parity >> 1;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	if (!(parity & 1))
7458c2ecf20Sopenharmony_ci		return -1;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	return wss;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic int saa711x_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
7538c2ecf20Sopenharmony_ci	u32 acpf;
7548c2ecf20Sopenharmony_ci	u32 acni;
7558c2ecf20Sopenharmony_ci	u32 hz;
7568c2ecf20Sopenharmony_ci	u64 f;
7578c2ecf20Sopenharmony_ci	u8 acc = 0;	/* reg 0x3a, audio clock control */
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	/* Checks for chips that don't have audio clock (saa7111, saa7113) */
7608c2ecf20Sopenharmony_ci	if (!saa711x_has_reg(state->ident, R_30_AUD_MAST_CLK_CYCLES_PER_FIELD))
7618c2ecf20Sopenharmony_ci		return 0;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "set audio clock freq: %d\n", freq);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	/* sanity check */
7668c2ecf20Sopenharmony_ci	if (freq < 32000 || freq > 48000)
7678c2ecf20Sopenharmony_ci		return -EINVAL;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	/* hz is the refresh rate times 100 */
7708c2ecf20Sopenharmony_ci	hz = (state->std & V4L2_STD_525_60) ? 5994 : 5000;
7718c2ecf20Sopenharmony_ci	/* acpf = (256 * freq) / field_frequency == (256 * 100 * freq) / hz */
7728c2ecf20Sopenharmony_ci	acpf = (25600 * freq) / hz;
7738c2ecf20Sopenharmony_ci	/* acni = (256 * freq * 2^23) / crystal_frequency =
7748c2ecf20Sopenharmony_ci		  (freq * 2^(8+23)) / crystal_frequency =
7758c2ecf20Sopenharmony_ci		  (freq << 31) / crystal_frequency */
7768c2ecf20Sopenharmony_ci	f = freq;
7778c2ecf20Sopenharmony_ci	f = f << 31;
7788c2ecf20Sopenharmony_ci	do_div(f, state->crystal_freq);
7798c2ecf20Sopenharmony_ci	acni = f;
7808c2ecf20Sopenharmony_ci	if (state->ucgc) {
7818c2ecf20Sopenharmony_ci		acpf = acpf * state->cgcdiv / 16;
7828c2ecf20Sopenharmony_ci		acni = acni * state->cgcdiv / 16;
7838c2ecf20Sopenharmony_ci		acc = 0x80;
7848c2ecf20Sopenharmony_ci		if (state->cgcdiv == 3)
7858c2ecf20Sopenharmony_ci			acc |= 0x40;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci	if (state->apll)
7888c2ecf20Sopenharmony_ci		acc |= 0x08;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	if (state->double_asclk) {
7918c2ecf20Sopenharmony_ci		acpf <<= 1;
7928c2ecf20Sopenharmony_ci		acni <<= 1;
7938c2ecf20Sopenharmony_ci	}
7948c2ecf20Sopenharmony_ci	saa711x_write(sd, R_38_CLK_RATIO_AMXCLK_TO_ASCLK, 0x03);
7958c2ecf20Sopenharmony_ci	saa711x_write(sd, R_39_CLK_RATIO_ASCLK_TO_ALRCLK, 0x10 << state->double_asclk);
7968c2ecf20Sopenharmony_ci	saa711x_write(sd, R_3A_AUD_CLK_GEN_BASIC_SETUP, acc);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	saa711x_write(sd, R_30_AUD_MAST_CLK_CYCLES_PER_FIELD, acpf & 0xff);
7998c2ecf20Sopenharmony_ci	saa711x_write(sd, R_30_AUD_MAST_CLK_CYCLES_PER_FIELD+1,
8008c2ecf20Sopenharmony_ci							(acpf >> 8) & 0xff);
8018c2ecf20Sopenharmony_ci	saa711x_write(sd, R_30_AUD_MAST_CLK_CYCLES_PER_FIELD+2,
8028c2ecf20Sopenharmony_ci							(acpf >> 16) & 0x03);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	saa711x_write(sd, R_34_AUD_MAST_CLK_NOMINAL_INC, acni & 0xff);
8058c2ecf20Sopenharmony_ci	saa711x_write(sd, R_34_AUD_MAST_CLK_NOMINAL_INC+1, (acni >> 8) & 0xff);
8068c2ecf20Sopenharmony_ci	saa711x_write(sd, R_34_AUD_MAST_CLK_NOMINAL_INC+2, (acni >> 16) & 0x3f);
8078c2ecf20Sopenharmony_ci	state->audclk_freq = freq;
8088c2ecf20Sopenharmony_ci	return 0;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic int saa711x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = to_sd(ctrl);
8148c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	switch (ctrl->id) {
8178c2ecf20Sopenharmony_ci	case V4L2_CID_CHROMA_AGC:
8188c2ecf20Sopenharmony_ci		/* chroma gain cluster */
8198c2ecf20Sopenharmony_ci		if (state->agc->val)
8208c2ecf20Sopenharmony_ci			state->gain->val =
8218c2ecf20Sopenharmony_ci				saa711x_read(sd, R_0F_CHROMA_GAIN_CNTL) & 0x7f;
8228c2ecf20Sopenharmony_ci		break;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci	return 0;
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic int saa711x_s_ctrl(struct v4l2_ctrl *ctrl)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = to_sd(ctrl);
8308c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	switch (ctrl->id) {
8338c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
8348c2ecf20Sopenharmony_ci		saa711x_write(sd, R_0A_LUMA_BRIGHT_CNTL, ctrl->val);
8358c2ecf20Sopenharmony_ci		break;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
8388c2ecf20Sopenharmony_ci		saa711x_write(sd, R_0B_LUMA_CONTRAST_CNTL, ctrl->val);
8398c2ecf20Sopenharmony_ci		break;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
8428c2ecf20Sopenharmony_ci		saa711x_write(sd, R_0C_CHROMA_SAT_CNTL, ctrl->val);
8438c2ecf20Sopenharmony_ci		break;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
8468c2ecf20Sopenharmony_ci		saa711x_write(sd, R_0D_CHROMA_HUE_CNTL, ctrl->val);
8478c2ecf20Sopenharmony_ci		break;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	case V4L2_CID_CHROMA_AGC:
8508c2ecf20Sopenharmony_ci		/* chroma gain cluster */
8518c2ecf20Sopenharmony_ci		if (state->agc->val)
8528c2ecf20Sopenharmony_ci			saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, state->gain->val);
8538c2ecf20Sopenharmony_ci		else
8548c2ecf20Sopenharmony_ci			saa711x_write(sd, R_0F_CHROMA_GAIN_CNTL, state->gain->val | 0x80);
8558c2ecf20Sopenharmony_ci		break;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	default:
8588c2ecf20Sopenharmony_ci		return -EINVAL;
8598c2ecf20Sopenharmony_ci	}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	return 0;
8628c2ecf20Sopenharmony_ci}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic int saa711x_set_size(struct v4l2_subdev *sd, int width, int height)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
8678c2ecf20Sopenharmony_ci	int HPSC, HFSC;
8688c2ecf20Sopenharmony_ci	int VSCY;
8698c2ecf20Sopenharmony_ci	int res;
8708c2ecf20Sopenharmony_ci	int is_50hz = state->std & V4L2_STD_625_50;
8718c2ecf20Sopenharmony_ci	int Vsrc = is_50hz ? 576 : 480;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "decoder set size to %ix%i\n", width, height);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	/* FIXME need better bounds checking here */
8768c2ecf20Sopenharmony_ci	if ((width < 1) || (width > 1440))
8778c2ecf20Sopenharmony_ci		return -EINVAL;
8788c2ecf20Sopenharmony_ci	if ((height < 1) || (height > Vsrc))
8798c2ecf20Sopenharmony_ci		return -EINVAL;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (!saa711x_has_reg(state->ident, R_D0_B_HORIZ_PRESCALING)) {
8828c2ecf20Sopenharmony_ci		/* Decoder only supports 720 columns and 480 or 576 lines */
8838c2ecf20Sopenharmony_ci		if (width != 720)
8848c2ecf20Sopenharmony_ci			return -EINVAL;
8858c2ecf20Sopenharmony_ci		if (height != Vsrc)
8868c2ecf20Sopenharmony_ci			return -EINVAL;
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	state->width = width;
8908c2ecf20Sopenharmony_ci	state->height = height;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	if (!saa711x_has_reg(state->ident, R_CC_B_HORIZ_OUTPUT_WINDOW_LENGTH))
8938c2ecf20Sopenharmony_ci		return 0;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* probably have a valid size, let's set it */
8968c2ecf20Sopenharmony_ci	/* Set output width/height */
8978c2ecf20Sopenharmony_ci	/* width */
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	saa711x_write(sd, R_CC_B_HORIZ_OUTPUT_WINDOW_LENGTH,
9008c2ecf20Sopenharmony_ci					(u8) (width & 0xff));
9018c2ecf20Sopenharmony_ci	saa711x_write(sd, R_CD_B_HORIZ_OUTPUT_WINDOW_LENGTH_MSB,
9028c2ecf20Sopenharmony_ci					(u8) ((width >> 8) & 0xff));
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	/* Vertical Scaling uses height/2 */
9058c2ecf20Sopenharmony_ci	res = height / 2;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	/* On 60Hz, it is using a higher Vertical Output Size */
9088c2ecf20Sopenharmony_ci	if (!is_50hz)
9098c2ecf20Sopenharmony_ci		res += (VRES_60HZ - 480) >> 1;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci		/* height */
9128c2ecf20Sopenharmony_ci	saa711x_write(sd, R_CE_B_VERT_OUTPUT_WINDOW_LENGTH,
9138c2ecf20Sopenharmony_ci					(u8) (res & 0xff));
9148c2ecf20Sopenharmony_ci	saa711x_write(sd, R_CF_B_VERT_OUTPUT_WINDOW_LENGTH_MSB,
9158c2ecf20Sopenharmony_ci					(u8) ((res >> 8) & 0xff));
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	/* Scaling settings */
9188c2ecf20Sopenharmony_ci	/* Hprescaler is floor(inres/outres) */
9198c2ecf20Sopenharmony_ci	HPSC = (int)(720 / width);
9208c2ecf20Sopenharmony_ci	/* 0 is not allowed (div. by zero) */
9218c2ecf20Sopenharmony_ci	HPSC = HPSC ? HPSC : 1;
9228c2ecf20Sopenharmony_ci	HFSC = (int)((1024 * 720) / (HPSC * width));
9238c2ecf20Sopenharmony_ci	/* FIXME hardcodes to "Task B"
9248c2ecf20Sopenharmony_ci	 * write H prescaler integer */
9258c2ecf20Sopenharmony_ci	saa711x_write(sd, R_D0_B_HORIZ_PRESCALING,
9268c2ecf20Sopenharmony_ci				(u8) (HPSC & 0x3f));
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "Hpsc: 0x%05x, Hfsc: 0x%05x\n", HPSC, HFSC);
9298c2ecf20Sopenharmony_ci	/* write H fine-scaling (luminance) */
9308c2ecf20Sopenharmony_ci	saa711x_write(sd, R_D8_B_HORIZ_LUMA_SCALING_INC,
9318c2ecf20Sopenharmony_ci				(u8) (HFSC & 0xff));
9328c2ecf20Sopenharmony_ci	saa711x_write(sd, R_D9_B_HORIZ_LUMA_SCALING_INC_MSB,
9338c2ecf20Sopenharmony_ci				(u8) ((HFSC >> 8) & 0xff));
9348c2ecf20Sopenharmony_ci	/* write H fine-scaling (chrominance)
9358c2ecf20Sopenharmony_ci	 * must be lum/2, so i'll just bitshift :) */
9368c2ecf20Sopenharmony_ci	saa711x_write(sd, R_DC_B_HORIZ_CHROMA_SCALING,
9378c2ecf20Sopenharmony_ci				(u8) ((HFSC >> 1) & 0xff));
9388c2ecf20Sopenharmony_ci	saa711x_write(sd, R_DD_B_HORIZ_CHROMA_SCALING_MSB,
9398c2ecf20Sopenharmony_ci				(u8) ((HFSC >> 9) & 0xff));
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	VSCY = (int)((1024 * Vsrc) / height);
9428c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "Vsrc: %d, Vscy: 0x%05x\n", Vsrc, VSCY);
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	/* Correct Contrast and Luminance */
9458c2ecf20Sopenharmony_ci	saa711x_write(sd, R_D5_B_LUMA_CONTRAST_CNTL,
9468c2ecf20Sopenharmony_ci					(u8) (64 * 1024 / VSCY));
9478c2ecf20Sopenharmony_ci	saa711x_write(sd, R_D6_B_CHROMA_SATURATION_CNTL,
9488c2ecf20Sopenharmony_ci					(u8) (64 * 1024 / VSCY));
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci		/* write V fine-scaling (luminance) */
9518c2ecf20Sopenharmony_ci	saa711x_write(sd, R_E0_B_VERT_LUMA_SCALING_INC,
9528c2ecf20Sopenharmony_ci					(u8) (VSCY & 0xff));
9538c2ecf20Sopenharmony_ci	saa711x_write(sd, R_E1_B_VERT_LUMA_SCALING_INC_MSB,
9548c2ecf20Sopenharmony_ci					(u8) ((VSCY >> 8) & 0xff));
9558c2ecf20Sopenharmony_ci		/* write V fine-scaling (chrominance) */
9568c2ecf20Sopenharmony_ci	saa711x_write(sd, R_E2_B_VERT_CHROMA_SCALING_INC,
9578c2ecf20Sopenharmony_ci					(u8) (VSCY & 0xff));
9588c2ecf20Sopenharmony_ci	saa711x_write(sd, R_E3_B_VERT_CHROMA_SCALING_INC_MSB,
9598c2ecf20Sopenharmony_ci					(u8) ((VSCY >> 8) & 0xff));
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	saa711x_writeregs(sd, saa7115_cfg_reset_scaler);
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	/* Activates task "B" */
9648c2ecf20Sopenharmony_ci	saa711x_write(sd, R_80_GLOBAL_CNTL_1,
9658c2ecf20Sopenharmony_ci				saa711x_read(sd, R_80_GLOBAL_CNTL_1) | 0x20);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	return 0;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic void saa711x_set_v4lstd(struct v4l2_subdev *sd, v4l2_std_id std)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	/* Prevent unnecessary standard changes. During a standard
9758c2ecf20Sopenharmony_ci	   change the I-Port is temporarily disabled. Any devices
9768c2ecf20Sopenharmony_ci	   reading from that port can get confused.
9778c2ecf20Sopenharmony_ci	   Note that s_std is also used to switch from
9788c2ecf20Sopenharmony_ci	   radio to TV mode, so if a s_std is broadcast to
9798c2ecf20Sopenharmony_ci	   all I2C devices then you do not want to have an unwanted
9808c2ecf20Sopenharmony_ci	   side-effect here. */
9818c2ecf20Sopenharmony_ci	if (std == state->std)
9828c2ecf20Sopenharmony_ci		return;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	state->std = std;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	// This works for NTSC-M, SECAM-L and the 50Hz PAL variants.
9878c2ecf20Sopenharmony_ci	if (std & V4L2_STD_525_60) {
9888c2ecf20Sopenharmony_ci		v4l2_dbg(1, debug, sd, "decoder set standard 60 Hz\n");
9898c2ecf20Sopenharmony_ci		if (state->ident == GM7113C) {
9908c2ecf20Sopenharmony_ci			u8 reg = saa711x_read(sd, R_08_SYNC_CNTL);
9918c2ecf20Sopenharmony_ci			reg &= ~(SAA7113_R_08_FSEL | SAA7113_R_08_AUFD);
9928c2ecf20Sopenharmony_ci			reg |= SAA7113_R_08_FSEL;
9938c2ecf20Sopenharmony_ci			saa711x_write(sd, R_08_SYNC_CNTL, reg);
9948c2ecf20Sopenharmony_ci		} else {
9958c2ecf20Sopenharmony_ci			saa711x_writeregs(sd, saa7115_cfg_60hz_video);
9968c2ecf20Sopenharmony_ci		}
9978c2ecf20Sopenharmony_ci		saa711x_set_size(sd, 720, 480);
9988c2ecf20Sopenharmony_ci	} else {
9998c2ecf20Sopenharmony_ci		v4l2_dbg(1, debug, sd, "decoder set standard 50 Hz\n");
10008c2ecf20Sopenharmony_ci		if (state->ident == GM7113C) {
10018c2ecf20Sopenharmony_ci			u8 reg = saa711x_read(sd, R_08_SYNC_CNTL);
10028c2ecf20Sopenharmony_ci			reg &= ~(SAA7113_R_08_FSEL | SAA7113_R_08_AUFD);
10038c2ecf20Sopenharmony_ci			saa711x_write(sd, R_08_SYNC_CNTL, reg);
10048c2ecf20Sopenharmony_ci		} else {
10058c2ecf20Sopenharmony_ci			saa711x_writeregs(sd, saa7115_cfg_50hz_video);
10068c2ecf20Sopenharmony_ci		}
10078c2ecf20Sopenharmony_ci		saa711x_set_size(sd, 720, 576);
10088c2ecf20Sopenharmony_ci	}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	/* Register 0E - Bits D6-D4 on NO-AUTO mode
10118c2ecf20Sopenharmony_ci		(SAA7111 and SAA7113 doesn't have auto mode)
10128c2ecf20Sopenharmony_ci	    50 Hz / 625 lines           60 Hz / 525 lines
10138c2ecf20Sopenharmony_ci	000 PAL BGDHI (4.43Mhz)         NTSC M (3.58MHz)
10148c2ecf20Sopenharmony_ci	001 NTSC 4.43 (50 Hz)           PAL 4.43 (60 Hz)
10158c2ecf20Sopenharmony_ci	010 Combination-PAL N (3.58MHz) NTSC 4.43 (60 Hz)
10168c2ecf20Sopenharmony_ci	011 NTSC N (3.58MHz)            PAL M (3.58MHz)
10178c2ecf20Sopenharmony_ci	100 reserved                    NTSC-Japan (3.58MHz)
10188c2ecf20Sopenharmony_ci	*/
10198c2ecf20Sopenharmony_ci	if (state->ident <= SAA7113 ||
10208c2ecf20Sopenharmony_ci	    state->ident == GM7113C) {
10218c2ecf20Sopenharmony_ci		u8 reg = saa711x_read(sd, R_0E_CHROMA_CNTL_1) & 0x8f;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci		if (std == V4L2_STD_PAL_M) {
10248c2ecf20Sopenharmony_ci			reg |= 0x30;
10258c2ecf20Sopenharmony_ci		} else if (std == V4L2_STD_PAL_Nc) {
10268c2ecf20Sopenharmony_ci			reg |= 0x20;
10278c2ecf20Sopenharmony_ci		} else if (std == V4L2_STD_PAL_60) {
10288c2ecf20Sopenharmony_ci			reg |= 0x10;
10298c2ecf20Sopenharmony_ci		} else if (std == V4L2_STD_NTSC_M_JP) {
10308c2ecf20Sopenharmony_ci			reg |= 0x40;
10318c2ecf20Sopenharmony_ci		} else if (std & V4L2_STD_SECAM) {
10328c2ecf20Sopenharmony_ci			reg |= 0x50;
10338c2ecf20Sopenharmony_ci		}
10348c2ecf20Sopenharmony_ci		saa711x_write(sd, R_0E_CHROMA_CNTL_1, reg);
10358c2ecf20Sopenharmony_ci	} else {
10368c2ecf20Sopenharmony_ci		/* restart task B if needed */
10378c2ecf20Sopenharmony_ci		int taskb = saa711x_read(sd, R_80_GLOBAL_CNTL_1) & 0x10;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci		if (taskb && state->ident == SAA7114)
10408c2ecf20Sopenharmony_ci			saa711x_writeregs(sd, saa7115_cfg_vbi_on);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci		/* switch audio mode too! */
10438c2ecf20Sopenharmony_ci		saa711x_s_clock_freq(sd, state->audclk_freq);
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci/* setup the sliced VBI lcr registers according to the sliced VBI format */
10488c2ecf20Sopenharmony_cistatic void saa711x_set_lcr(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt)
10498c2ecf20Sopenharmony_ci{
10508c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
10518c2ecf20Sopenharmony_ci	int is_50hz = (state->std & V4L2_STD_625_50);
10528c2ecf20Sopenharmony_ci	u8 lcr[24];
10538c2ecf20Sopenharmony_ci	int i, x;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci#if 1
10568c2ecf20Sopenharmony_ci	/* saa7113/7114/7118 VBI support are experimental */
10578c2ecf20Sopenharmony_ci	if (!saa711x_has_reg(state->ident, R_41_LCR_BASE))
10588c2ecf20Sopenharmony_ci		return;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci#else
10618c2ecf20Sopenharmony_ci	/* SAA7113 and SAA7118 also should support VBI - Need testing */
10628c2ecf20Sopenharmony_ci	if (state->ident != SAA7115)
10638c2ecf20Sopenharmony_ci		return;
10648c2ecf20Sopenharmony_ci#endif
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	for (i = 0; i <= 23; i++)
10678c2ecf20Sopenharmony_ci		lcr[i] = 0xff;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	if (fmt == NULL) {
10708c2ecf20Sopenharmony_ci		/* raw VBI */
10718c2ecf20Sopenharmony_ci		if (is_50hz)
10728c2ecf20Sopenharmony_ci			for (i = 6; i <= 23; i++)
10738c2ecf20Sopenharmony_ci				lcr[i] = 0xdd;
10748c2ecf20Sopenharmony_ci		else
10758c2ecf20Sopenharmony_ci			for (i = 10; i <= 21; i++)
10768c2ecf20Sopenharmony_ci				lcr[i] = 0xdd;
10778c2ecf20Sopenharmony_ci	} else {
10788c2ecf20Sopenharmony_ci		/* sliced VBI */
10798c2ecf20Sopenharmony_ci		/* first clear lines that cannot be captured */
10808c2ecf20Sopenharmony_ci		if (is_50hz) {
10818c2ecf20Sopenharmony_ci			for (i = 0; i <= 5; i++)
10828c2ecf20Sopenharmony_ci				fmt->service_lines[0][i] =
10838c2ecf20Sopenharmony_ci					fmt->service_lines[1][i] = 0;
10848c2ecf20Sopenharmony_ci		}
10858c2ecf20Sopenharmony_ci		else {
10868c2ecf20Sopenharmony_ci			for (i = 0; i <= 9; i++)
10878c2ecf20Sopenharmony_ci				fmt->service_lines[0][i] =
10888c2ecf20Sopenharmony_ci					fmt->service_lines[1][i] = 0;
10898c2ecf20Sopenharmony_ci			for (i = 22; i <= 23; i++)
10908c2ecf20Sopenharmony_ci				fmt->service_lines[0][i] =
10918c2ecf20Sopenharmony_ci					fmt->service_lines[1][i] = 0;
10928c2ecf20Sopenharmony_ci		}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci		/* Now set the lcr values according to the specified service */
10958c2ecf20Sopenharmony_ci		for (i = 6; i <= 23; i++) {
10968c2ecf20Sopenharmony_ci			lcr[i] = 0;
10978c2ecf20Sopenharmony_ci			for (x = 0; x <= 1; x++) {
10988c2ecf20Sopenharmony_ci				switch (fmt->service_lines[1-x][i]) {
10998c2ecf20Sopenharmony_ci					case 0:
11008c2ecf20Sopenharmony_ci						lcr[i] |= 0xf << (4 * x);
11018c2ecf20Sopenharmony_ci						break;
11028c2ecf20Sopenharmony_ci					case V4L2_SLICED_TELETEXT_B:
11038c2ecf20Sopenharmony_ci						lcr[i] |= 1 << (4 * x);
11048c2ecf20Sopenharmony_ci						break;
11058c2ecf20Sopenharmony_ci					case V4L2_SLICED_CAPTION_525:
11068c2ecf20Sopenharmony_ci						lcr[i] |= 4 << (4 * x);
11078c2ecf20Sopenharmony_ci						break;
11088c2ecf20Sopenharmony_ci					case V4L2_SLICED_WSS_625:
11098c2ecf20Sopenharmony_ci						lcr[i] |= 5 << (4 * x);
11108c2ecf20Sopenharmony_ci						break;
11118c2ecf20Sopenharmony_ci					case V4L2_SLICED_VPS:
11128c2ecf20Sopenharmony_ci						lcr[i] |= 7 << (4 * x);
11138c2ecf20Sopenharmony_ci						break;
11148c2ecf20Sopenharmony_ci				}
11158c2ecf20Sopenharmony_ci			}
11168c2ecf20Sopenharmony_ci		}
11178c2ecf20Sopenharmony_ci	}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	/* write the lcr registers */
11208c2ecf20Sopenharmony_ci	for (i = 2; i <= 23; i++) {
11218c2ecf20Sopenharmony_ci		saa711x_write(sd, i - 2 + R_41_LCR_BASE, lcr[i]);
11228c2ecf20Sopenharmony_ci	}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	/* enable/disable raw VBI capturing */
11258c2ecf20Sopenharmony_ci	saa711x_writeregs(sd, fmt == NULL ?
11268c2ecf20Sopenharmony_ci				saa7115_cfg_vbi_on :
11278c2ecf20Sopenharmony_ci				saa7115_cfg_vbi_off);
11288c2ecf20Sopenharmony_ci}
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_cistatic int saa711x_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *sliced)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	static u16 lcr2vbi[] = {
11338c2ecf20Sopenharmony_ci		0, V4L2_SLICED_TELETEXT_B, 0,	/* 1 */
11348c2ecf20Sopenharmony_ci		0, V4L2_SLICED_CAPTION_525,	/* 4 */
11358c2ecf20Sopenharmony_ci		V4L2_SLICED_WSS_625, 0,		/* 5 */
11368c2ecf20Sopenharmony_ci		V4L2_SLICED_VPS, 0, 0, 0, 0,	/* 7 */
11378c2ecf20Sopenharmony_ci		0, 0, 0, 0
11388c2ecf20Sopenharmony_ci	};
11398c2ecf20Sopenharmony_ci	int i;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	memset(sliced->service_lines, 0, sizeof(sliced->service_lines));
11428c2ecf20Sopenharmony_ci	sliced->service_set = 0;
11438c2ecf20Sopenharmony_ci	/* done if using raw VBI */
11448c2ecf20Sopenharmony_ci	if (saa711x_read(sd, R_80_GLOBAL_CNTL_1) & 0x10)
11458c2ecf20Sopenharmony_ci		return 0;
11468c2ecf20Sopenharmony_ci	for (i = 2; i <= 23; i++) {
11478c2ecf20Sopenharmony_ci		u8 v = saa711x_read(sd, i - 2 + R_41_LCR_BASE);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci		sliced->service_lines[0][i] = lcr2vbi[v >> 4];
11508c2ecf20Sopenharmony_ci		sliced->service_lines[1][i] = lcr2vbi[v & 0xf];
11518c2ecf20Sopenharmony_ci		sliced->service_set |=
11528c2ecf20Sopenharmony_ci			sliced->service_lines[0][i] | sliced->service_lines[1][i];
11538c2ecf20Sopenharmony_ci	}
11548c2ecf20Sopenharmony_ci	return 0;
11558c2ecf20Sopenharmony_ci}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_cistatic int saa711x_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
11588c2ecf20Sopenharmony_ci{
11598c2ecf20Sopenharmony_ci	saa711x_set_lcr(sd, NULL);
11608c2ecf20Sopenharmony_ci	return 0;
11618c2ecf20Sopenharmony_ci}
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_cistatic int saa711x_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt)
11648c2ecf20Sopenharmony_ci{
11658c2ecf20Sopenharmony_ci	saa711x_set_lcr(sd, fmt);
11668c2ecf20Sopenharmony_ci	return 0;
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic int saa711x_set_fmt(struct v4l2_subdev *sd,
11708c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
11718c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
11728c2ecf20Sopenharmony_ci{
11738c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *fmt = &format->format;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED)
11768c2ecf20Sopenharmony_ci		return -EINVAL;
11778c2ecf20Sopenharmony_ci	fmt->field = V4L2_FIELD_INTERLACED;
11788c2ecf20Sopenharmony_ci	fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
11798c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
11808c2ecf20Sopenharmony_ci		return 0;
11818c2ecf20Sopenharmony_ci	return saa711x_set_size(sd, fmt->width, fmt->height);
11828c2ecf20Sopenharmony_ci}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci/* Decode the sliced VBI data stream as created by the saa7115.
11858c2ecf20Sopenharmony_ci   The format is described in the saa7115 datasheet in Tables 25 and 26
11868c2ecf20Sopenharmony_ci   and in Figure 33.
11878c2ecf20Sopenharmony_ci   The current implementation uses SAV/EAV codes and not the ancillary data
11888c2ecf20Sopenharmony_ci   headers. The vbi->p pointer points to the R_5E_SDID byte right after the SAV
11898c2ecf20Sopenharmony_ci   code. */
11908c2ecf20Sopenharmony_cistatic int saa711x_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
11938c2ecf20Sopenharmony_ci	static const char vbi_no_data_pattern[] = {
11948c2ecf20Sopenharmony_ci		0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0
11958c2ecf20Sopenharmony_ci	};
11968c2ecf20Sopenharmony_ci	u8 *p = vbi->p;
11978c2ecf20Sopenharmony_ci	u32 wss;
11988c2ecf20Sopenharmony_ci	int id1, id2;   /* the ID1 and ID2 bytes from the internal header */
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	vbi->type = 0;  /* mark result as a failure */
12018c2ecf20Sopenharmony_ci	id1 = p[2];
12028c2ecf20Sopenharmony_ci	id2 = p[3];
12038c2ecf20Sopenharmony_ci	/* Note: the field bit is inverted for 60 Hz video */
12048c2ecf20Sopenharmony_ci	if (state->std & V4L2_STD_525_60)
12058c2ecf20Sopenharmony_ci		id1 ^= 0x40;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	/* Skip internal header, p now points to the start of the payload */
12088c2ecf20Sopenharmony_ci	p += 4;
12098c2ecf20Sopenharmony_ci	vbi->p = p;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	/* calculate field and line number of the VBI packet (1-23) */
12128c2ecf20Sopenharmony_ci	vbi->is_second_field = ((id1 & 0x40) != 0);
12138c2ecf20Sopenharmony_ci	vbi->line = (id1 & 0x3f) << 3;
12148c2ecf20Sopenharmony_ci	vbi->line |= (id2 & 0x70) >> 4;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	/* Obtain data type */
12178c2ecf20Sopenharmony_ci	id2 &= 0xf;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	/* If the VBI slicer does not detect any signal it will fill up
12208c2ecf20Sopenharmony_ci	   the payload buffer with 0xa0 bytes. */
12218c2ecf20Sopenharmony_ci	if (!memcmp(p, vbi_no_data_pattern, sizeof(vbi_no_data_pattern)))
12228c2ecf20Sopenharmony_ci		return 0;
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	/* decode payloads */
12258c2ecf20Sopenharmony_ci	switch (id2) {
12268c2ecf20Sopenharmony_ci	case 1:
12278c2ecf20Sopenharmony_ci		vbi->type = V4L2_SLICED_TELETEXT_B;
12288c2ecf20Sopenharmony_ci		break;
12298c2ecf20Sopenharmony_ci	case 4:
12308c2ecf20Sopenharmony_ci		if (!saa711x_odd_parity(p[0]) || !saa711x_odd_parity(p[1]))
12318c2ecf20Sopenharmony_ci			return 0;
12328c2ecf20Sopenharmony_ci		vbi->type = V4L2_SLICED_CAPTION_525;
12338c2ecf20Sopenharmony_ci		break;
12348c2ecf20Sopenharmony_ci	case 5:
12358c2ecf20Sopenharmony_ci		wss = saa711x_decode_wss(p);
12368c2ecf20Sopenharmony_ci		if (wss == -1)
12378c2ecf20Sopenharmony_ci			return 0;
12388c2ecf20Sopenharmony_ci		p[0] = wss & 0xff;
12398c2ecf20Sopenharmony_ci		p[1] = wss >> 8;
12408c2ecf20Sopenharmony_ci		vbi->type = V4L2_SLICED_WSS_625;
12418c2ecf20Sopenharmony_ci		break;
12428c2ecf20Sopenharmony_ci	case 7:
12438c2ecf20Sopenharmony_ci		if (saa711x_decode_vps(p, p) != 0)
12448c2ecf20Sopenharmony_ci			return 0;
12458c2ecf20Sopenharmony_ci		vbi->type = V4L2_SLICED_VPS;
12468c2ecf20Sopenharmony_ci		break;
12478c2ecf20Sopenharmony_ci	default:
12488c2ecf20Sopenharmony_ci		break;
12498c2ecf20Sopenharmony_ci	}
12508c2ecf20Sopenharmony_ci	return 0;
12518c2ecf20Sopenharmony_ci}
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci/* ============ SAA7115 AUDIO settings (end) ============= */
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_cistatic int saa711x_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
12588c2ecf20Sopenharmony_ci	int status;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	if (state->radio)
12618c2ecf20Sopenharmony_ci		return 0;
12628c2ecf20Sopenharmony_ci	status = saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC);
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "status: 0x%02x\n", status);
12658c2ecf20Sopenharmony_ci	vt->signal = ((status & (1 << 6)) == 0) ? 0xffff : 0x0;
12668c2ecf20Sopenharmony_ci	return 0;
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_cistatic int saa711x_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
12708c2ecf20Sopenharmony_ci{
12718c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	state->radio = 0;
12748c2ecf20Sopenharmony_ci	saa711x_set_v4lstd(sd, std);
12758c2ecf20Sopenharmony_ci	return 0;
12768c2ecf20Sopenharmony_ci}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cistatic int saa711x_s_radio(struct v4l2_subdev *sd)
12798c2ecf20Sopenharmony_ci{
12808c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	state->radio = 1;
12838c2ecf20Sopenharmony_ci	return 0;
12848c2ecf20Sopenharmony_ci}
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_cistatic int saa711x_s_routing(struct v4l2_subdev *sd,
12878c2ecf20Sopenharmony_ci			     u32 input, u32 output, u32 config)
12888c2ecf20Sopenharmony_ci{
12898c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
12908c2ecf20Sopenharmony_ci	u8 mask = (state->ident <= SAA7111A) ? 0xf8 : 0xf0;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "decoder set input %d output %d\n",
12938c2ecf20Sopenharmony_ci		input, output);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* saa7111/3 does not have these inputs */
12968c2ecf20Sopenharmony_ci	if ((state->ident <= SAA7113 ||
12978c2ecf20Sopenharmony_ci	     state->ident == GM7113C) &&
12988c2ecf20Sopenharmony_ci	    (input == SAA7115_COMPOSITE4 ||
12998c2ecf20Sopenharmony_ci	     input == SAA7115_COMPOSITE5)) {
13008c2ecf20Sopenharmony_ci		return -EINVAL;
13018c2ecf20Sopenharmony_ci	}
13028c2ecf20Sopenharmony_ci	if (input > SAA7115_SVIDEO3)
13038c2ecf20Sopenharmony_ci		return -EINVAL;
13048c2ecf20Sopenharmony_ci	if (state->input == input && state->output == output)
13058c2ecf20Sopenharmony_ci		return 0;
13068c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "now setting %s input %s output\n",
13078c2ecf20Sopenharmony_ci		(input >= SAA7115_SVIDEO0) ? "S-Video" : "Composite",
13088c2ecf20Sopenharmony_ci		(output == SAA7115_IPORT_ON) ? "iport on" : "iport off");
13098c2ecf20Sopenharmony_ci	state->input = input;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	/* saa7111 has slightly different input numbering */
13128c2ecf20Sopenharmony_ci	if (state->ident <= SAA7111A) {
13138c2ecf20Sopenharmony_ci		if (input >= SAA7115_COMPOSITE4)
13148c2ecf20Sopenharmony_ci			input -= 2;
13158c2ecf20Sopenharmony_ci		/* saa7111 specific */
13168c2ecf20Sopenharmony_ci		saa711x_write(sd, R_10_CHROMA_CNTL_2,
13178c2ecf20Sopenharmony_ci				(saa711x_read(sd, R_10_CHROMA_CNTL_2) & 0x3f) |
13188c2ecf20Sopenharmony_ci				((output & 0xc0) ^ 0x40));
13198c2ecf20Sopenharmony_ci		saa711x_write(sd, R_13_RT_X_PORT_OUT_CNTL,
13208c2ecf20Sopenharmony_ci				(saa711x_read(sd, R_13_RT_X_PORT_OUT_CNTL) & 0xf0) |
13218c2ecf20Sopenharmony_ci				((output & 2) ? 0x0a : 0));
13228c2ecf20Sopenharmony_ci	}
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	/* select mode */
13258c2ecf20Sopenharmony_ci	saa711x_write(sd, R_02_INPUT_CNTL_1,
13268c2ecf20Sopenharmony_ci		      (saa711x_read(sd, R_02_INPUT_CNTL_1) & mask) |
13278c2ecf20Sopenharmony_ci		       input);
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	/* bypass chrominance trap for S-Video modes */
13308c2ecf20Sopenharmony_ci	saa711x_write(sd, R_09_LUMA_CNTL,
13318c2ecf20Sopenharmony_ci			(saa711x_read(sd, R_09_LUMA_CNTL) & 0x7f) |
13328c2ecf20Sopenharmony_ci			(state->input >= SAA7115_SVIDEO0 ? 0x80 : 0x0));
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	state->output = output;
13358c2ecf20Sopenharmony_ci	if (state->ident == SAA7114 ||
13368c2ecf20Sopenharmony_ci			state->ident == SAA7115) {
13378c2ecf20Sopenharmony_ci		saa711x_write(sd, R_83_X_PORT_I_O_ENA_AND_OUT_CLK,
13388c2ecf20Sopenharmony_ci				(saa711x_read(sd, R_83_X_PORT_I_O_ENA_AND_OUT_CLK) & 0xfe) |
13398c2ecf20Sopenharmony_ci				(state->output & 0x01));
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci	if (state->ident > SAA7111A) {
13428c2ecf20Sopenharmony_ci		if (config & SAA7115_IDQ_IS_DEFAULT)
13438c2ecf20Sopenharmony_ci			saa711x_write(sd, R_85_I_PORT_SIGNAL_POLAR, 0x20);
13448c2ecf20Sopenharmony_ci		else
13458c2ecf20Sopenharmony_ci			saa711x_write(sd, R_85_I_PORT_SIGNAL_POLAR, 0x21);
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci	return 0;
13488c2ecf20Sopenharmony_ci}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_cistatic int saa711x_s_gpio(struct v4l2_subdev *sd, u32 val)
13518c2ecf20Sopenharmony_ci{
13528c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	if (state->ident > SAA7111A)
13558c2ecf20Sopenharmony_ci		return -EINVAL;
13568c2ecf20Sopenharmony_ci	saa711x_write(sd, 0x11, (saa711x_read(sd, 0x11) & 0x7f) |
13578c2ecf20Sopenharmony_ci		(val ? 0x80 : 0));
13588c2ecf20Sopenharmony_ci	return 0;
13598c2ecf20Sopenharmony_ci}
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_cistatic int saa711x_s_stream(struct v4l2_subdev *sd, int enable)
13628c2ecf20Sopenharmony_ci{
13638c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "%s output\n",
13668c2ecf20Sopenharmony_ci			enable ? "enable" : "disable");
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	if (state->enable == enable)
13698c2ecf20Sopenharmony_ci		return 0;
13708c2ecf20Sopenharmony_ci	state->enable = enable;
13718c2ecf20Sopenharmony_ci	if (!saa711x_has_reg(state->ident, R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED))
13728c2ecf20Sopenharmony_ci		return 0;
13738c2ecf20Sopenharmony_ci	saa711x_write(sd, R_87_I_PORT_I_O_ENA_OUT_CLK_AND_GATED, state->enable);
13748c2ecf20Sopenharmony_ci	return 0;
13758c2ecf20Sopenharmony_ci}
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_cistatic int saa711x_s_crystal_freq(struct v4l2_subdev *sd, u32 freq, u32 flags)
13788c2ecf20Sopenharmony_ci{
13798c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	if (freq != SAA7115_FREQ_32_11_MHZ && freq != SAA7115_FREQ_24_576_MHZ)
13828c2ecf20Sopenharmony_ci		return -EINVAL;
13838c2ecf20Sopenharmony_ci	state->crystal_freq = freq;
13848c2ecf20Sopenharmony_ci	state->double_asclk = flags & SAA7115_FREQ_FL_DOUBLE_ASCLK;
13858c2ecf20Sopenharmony_ci	state->cgcdiv = (flags & SAA7115_FREQ_FL_CGCDIV) ? 3 : 4;
13868c2ecf20Sopenharmony_ci	state->ucgc = flags & SAA7115_FREQ_FL_UCGC;
13878c2ecf20Sopenharmony_ci	state->apll = flags & SAA7115_FREQ_FL_APLL;
13888c2ecf20Sopenharmony_ci	saa711x_s_clock_freq(sd, state->audclk_freq);
13898c2ecf20Sopenharmony_ci	return 0;
13908c2ecf20Sopenharmony_ci}
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_cistatic int saa711x_reset(struct v4l2_subdev *sd, u32 val)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "decoder RESET\n");
13958c2ecf20Sopenharmony_ci	saa711x_writeregs(sd, saa7115_cfg_reset_scaler);
13968c2ecf20Sopenharmony_ci	return 0;
13978c2ecf20Sopenharmony_ci}
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_cistatic int saa711x_g_vbi_data(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *data)
14008c2ecf20Sopenharmony_ci{
14018c2ecf20Sopenharmony_ci	/* Note: the internal field ID is inverted for NTSC,
14028c2ecf20Sopenharmony_ci	   so data->field 0 maps to the saa7115 even field,
14038c2ecf20Sopenharmony_ci	   whereas for PAL it maps to the saa7115 odd field. */
14048c2ecf20Sopenharmony_ci	switch (data->id) {
14058c2ecf20Sopenharmony_ci	case V4L2_SLICED_WSS_625:
14068c2ecf20Sopenharmony_ci		if (saa711x_read(sd, 0x6b) & 0xc0)
14078c2ecf20Sopenharmony_ci			return -EIO;
14088c2ecf20Sopenharmony_ci		data->data[0] = saa711x_read(sd, 0x6c);
14098c2ecf20Sopenharmony_ci		data->data[1] = saa711x_read(sd, 0x6d);
14108c2ecf20Sopenharmony_ci		return 0;
14118c2ecf20Sopenharmony_ci	case V4L2_SLICED_CAPTION_525:
14128c2ecf20Sopenharmony_ci		if (data->field == 0) {
14138c2ecf20Sopenharmony_ci			/* CC */
14148c2ecf20Sopenharmony_ci			if (saa711x_read(sd, 0x66) & 0x30)
14158c2ecf20Sopenharmony_ci				return -EIO;
14168c2ecf20Sopenharmony_ci			data->data[0] = saa711x_read(sd, 0x69);
14178c2ecf20Sopenharmony_ci			data->data[1] = saa711x_read(sd, 0x6a);
14188c2ecf20Sopenharmony_ci			return 0;
14198c2ecf20Sopenharmony_ci		}
14208c2ecf20Sopenharmony_ci		/* XDS */
14218c2ecf20Sopenharmony_ci		if (saa711x_read(sd, 0x66) & 0xc0)
14228c2ecf20Sopenharmony_ci			return -EIO;
14238c2ecf20Sopenharmony_ci		data->data[0] = saa711x_read(sd, 0x67);
14248c2ecf20Sopenharmony_ci		data->data[1] = saa711x_read(sd, 0x68);
14258c2ecf20Sopenharmony_ci		return 0;
14268c2ecf20Sopenharmony_ci	default:
14278c2ecf20Sopenharmony_ci		return -EINVAL;
14288c2ecf20Sopenharmony_ci	}
14298c2ecf20Sopenharmony_ci}
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_cistatic int saa711x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
14328c2ecf20Sopenharmony_ci{
14338c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
14348c2ecf20Sopenharmony_ci	int reg1f, reg1e;
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	/*
14378c2ecf20Sopenharmony_ci	 * The V4L2 core already initializes std with all supported
14388c2ecf20Sopenharmony_ci	 * Standards. All driver needs to do is to mask it, to remove
14398c2ecf20Sopenharmony_ci	 * standards that don't apply from the mask
14408c2ecf20Sopenharmony_ci	 */
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	reg1f = saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC);
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	if (state->ident == SAA7115) {
14458c2ecf20Sopenharmony_ci		reg1e = saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC);
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci		v4l2_dbg(1, debug, sd, "Status byte 1 (0x1e)=0x%02x\n", reg1e);
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci		switch (reg1e & 0x03) {
14508c2ecf20Sopenharmony_ci		case 1:
14518c2ecf20Sopenharmony_ci			*std &= V4L2_STD_NTSC;
14528c2ecf20Sopenharmony_ci			break;
14538c2ecf20Sopenharmony_ci		case 2:
14548c2ecf20Sopenharmony_ci			/*
14558c2ecf20Sopenharmony_ci			 * V4L2_STD_PAL just cover the european PAL standards.
14568c2ecf20Sopenharmony_ci			 * This is wrong, as the device could also be using an
14578c2ecf20Sopenharmony_ci			 * other PAL standard.
14588c2ecf20Sopenharmony_ci			 */
14598c2ecf20Sopenharmony_ci			*std &= V4L2_STD_PAL   | V4L2_STD_PAL_N  | V4L2_STD_PAL_Nc |
14608c2ecf20Sopenharmony_ci				V4L2_STD_PAL_M | V4L2_STD_PAL_60;
14618c2ecf20Sopenharmony_ci			break;
14628c2ecf20Sopenharmony_ci		case 3:
14638c2ecf20Sopenharmony_ci			*std &= V4L2_STD_SECAM;
14648c2ecf20Sopenharmony_ci			break;
14658c2ecf20Sopenharmony_ci		default:
14668c2ecf20Sopenharmony_ci			*std = V4L2_STD_UNKNOWN;
14678c2ecf20Sopenharmony_ci			/* Can't detect anything */
14688c2ecf20Sopenharmony_ci			break;
14698c2ecf20Sopenharmony_ci		}
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "Status byte 2 (0x1f)=0x%02x\n", reg1f);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	/* horizontal/vertical not locked */
14758c2ecf20Sopenharmony_ci	if (reg1f & 0x40) {
14768c2ecf20Sopenharmony_ci		*std = V4L2_STD_UNKNOWN;
14778c2ecf20Sopenharmony_ci		goto ret;
14788c2ecf20Sopenharmony_ci	}
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	if (reg1f & 0x20)
14818c2ecf20Sopenharmony_ci		*std &= V4L2_STD_525_60;
14828c2ecf20Sopenharmony_ci	else
14838c2ecf20Sopenharmony_ci		*std &= V4L2_STD_625_50;
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ciret:
14868c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "detected std mask = %08Lx\n", *std);
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	return 0;
14898c2ecf20Sopenharmony_ci}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_cistatic int saa711x_g_input_status(struct v4l2_subdev *sd, u32 *status)
14928c2ecf20Sopenharmony_ci{
14938c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
14948c2ecf20Sopenharmony_ci	int reg1e = 0x80;
14958c2ecf20Sopenharmony_ci	int reg1f;
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	*status = V4L2_IN_ST_NO_SIGNAL;
14988c2ecf20Sopenharmony_ci	if (state->ident == SAA7115)
14998c2ecf20Sopenharmony_ci		reg1e = saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC);
15008c2ecf20Sopenharmony_ci	reg1f = saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC);
15018c2ecf20Sopenharmony_ci	if ((reg1f & 0xc1) == 0x81 && (reg1e & 0xc0) == 0x80)
15028c2ecf20Sopenharmony_ci		*status = 0;
15038c2ecf20Sopenharmony_ci	return 0;
15048c2ecf20Sopenharmony_ci}
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
15078c2ecf20Sopenharmony_cistatic int saa711x_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
15088c2ecf20Sopenharmony_ci{
15098c2ecf20Sopenharmony_ci	reg->val = saa711x_read(sd, reg->reg & 0xff);
15108c2ecf20Sopenharmony_ci	reg->size = 1;
15118c2ecf20Sopenharmony_ci	return 0;
15128c2ecf20Sopenharmony_ci}
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_cistatic int saa711x_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
15158c2ecf20Sopenharmony_ci{
15168c2ecf20Sopenharmony_ci	saa711x_write(sd, reg->reg & 0xff, reg->val & 0xff);
15178c2ecf20Sopenharmony_ci	return 0;
15188c2ecf20Sopenharmony_ci}
15198c2ecf20Sopenharmony_ci#endif
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_cistatic int saa711x_log_status(struct v4l2_subdev *sd)
15228c2ecf20Sopenharmony_ci{
15238c2ecf20Sopenharmony_ci	struct saa711x_state *state = to_state(sd);
15248c2ecf20Sopenharmony_ci	int reg1e, reg1f;
15258c2ecf20Sopenharmony_ci	int signalOk;
15268c2ecf20Sopenharmony_ci	int vcr;
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	v4l2_info(sd, "Audio frequency: %d Hz\n", state->audclk_freq);
15298c2ecf20Sopenharmony_ci	if (state->ident != SAA7115) {
15308c2ecf20Sopenharmony_ci		/* status for the saa7114 */
15318c2ecf20Sopenharmony_ci		reg1f = saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC);
15328c2ecf20Sopenharmony_ci		signalOk = (reg1f & 0xc1) == 0x81;
15338c2ecf20Sopenharmony_ci		v4l2_info(sd, "Video signal:    %s\n", signalOk ? "ok" : "bad");
15348c2ecf20Sopenharmony_ci		v4l2_info(sd, "Frequency:       %s\n", (reg1f & 0x20) ? "60 Hz" : "50 Hz");
15358c2ecf20Sopenharmony_ci		return 0;
15368c2ecf20Sopenharmony_ci	}
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	/* status for the saa7115 */
15398c2ecf20Sopenharmony_ci	reg1e = saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC);
15408c2ecf20Sopenharmony_ci	reg1f = saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC);
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	signalOk = (reg1f & 0xc1) == 0x81 && (reg1e & 0xc0) == 0x80;
15438c2ecf20Sopenharmony_ci	vcr = !(reg1f & 0x10);
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	if (state->input >= 6)
15468c2ecf20Sopenharmony_ci		v4l2_info(sd, "Input:           S-Video %d\n", state->input - 6);
15478c2ecf20Sopenharmony_ci	else
15488c2ecf20Sopenharmony_ci		v4l2_info(sd, "Input:           Composite %d\n", state->input);
15498c2ecf20Sopenharmony_ci	v4l2_info(sd, "Video signal:    %s\n", signalOk ? (vcr ? "VCR" : "broadcast/DVD") : "bad");
15508c2ecf20Sopenharmony_ci	v4l2_info(sd, "Frequency:       %s\n", (reg1f & 0x20) ? "60 Hz" : "50 Hz");
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	switch (reg1e & 0x03) {
15538c2ecf20Sopenharmony_ci	case 1:
15548c2ecf20Sopenharmony_ci		v4l2_info(sd, "Detected format: NTSC\n");
15558c2ecf20Sopenharmony_ci		break;
15568c2ecf20Sopenharmony_ci	case 2:
15578c2ecf20Sopenharmony_ci		v4l2_info(sd, "Detected format: PAL\n");
15588c2ecf20Sopenharmony_ci		break;
15598c2ecf20Sopenharmony_ci	case 3:
15608c2ecf20Sopenharmony_ci		v4l2_info(sd, "Detected format: SECAM\n");
15618c2ecf20Sopenharmony_ci		break;
15628c2ecf20Sopenharmony_ci	default:
15638c2ecf20Sopenharmony_ci		v4l2_info(sd, "Detected format: BW/No color\n");
15648c2ecf20Sopenharmony_ci		break;
15658c2ecf20Sopenharmony_ci	}
15668c2ecf20Sopenharmony_ci	v4l2_info(sd, "Width, Height:   %d, %d\n", state->width, state->height);
15678c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
15688c2ecf20Sopenharmony_ci	return 0;
15698c2ecf20Sopenharmony_ci}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops saa711x_ctrl_ops = {
15748c2ecf20Sopenharmony_ci	.s_ctrl = saa711x_s_ctrl,
15758c2ecf20Sopenharmony_ci	.g_volatile_ctrl = saa711x_g_volatile_ctrl,
15768c2ecf20Sopenharmony_ci};
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops saa711x_core_ops = {
15798c2ecf20Sopenharmony_ci	.log_status = saa711x_log_status,
15808c2ecf20Sopenharmony_ci	.reset = saa711x_reset,
15818c2ecf20Sopenharmony_ci	.s_gpio = saa711x_s_gpio,
15828c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
15838c2ecf20Sopenharmony_ci	.g_register = saa711x_g_register,
15848c2ecf20Sopenharmony_ci	.s_register = saa711x_s_register,
15858c2ecf20Sopenharmony_ci#endif
15868c2ecf20Sopenharmony_ci};
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops saa711x_tuner_ops = {
15898c2ecf20Sopenharmony_ci	.s_radio = saa711x_s_radio,
15908c2ecf20Sopenharmony_ci	.g_tuner = saa711x_g_tuner,
15918c2ecf20Sopenharmony_ci};
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_audio_ops saa711x_audio_ops = {
15948c2ecf20Sopenharmony_ci	.s_clock_freq = saa711x_s_clock_freq,
15958c2ecf20Sopenharmony_ci};
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops saa711x_video_ops = {
15988c2ecf20Sopenharmony_ci	.s_std = saa711x_s_std,
15998c2ecf20Sopenharmony_ci	.s_routing = saa711x_s_routing,
16008c2ecf20Sopenharmony_ci	.s_crystal_freq = saa711x_s_crystal_freq,
16018c2ecf20Sopenharmony_ci	.s_stream = saa711x_s_stream,
16028c2ecf20Sopenharmony_ci	.querystd = saa711x_querystd,
16038c2ecf20Sopenharmony_ci	.g_input_status = saa711x_g_input_status,
16048c2ecf20Sopenharmony_ci};
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_vbi_ops saa711x_vbi_ops = {
16078c2ecf20Sopenharmony_ci	.g_vbi_data = saa711x_g_vbi_data,
16088c2ecf20Sopenharmony_ci	.decode_vbi_line = saa711x_decode_vbi_line,
16098c2ecf20Sopenharmony_ci	.g_sliced_fmt = saa711x_g_sliced_fmt,
16108c2ecf20Sopenharmony_ci	.s_sliced_fmt = saa711x_s_sliced_fmt,
16118c2ecf20Sopenharmony_ci	.s_raw_fmt = saa711x_s_raw_fmt,
16128c2ecf20Sopenharmony_ci};
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops saa711x_pad_ops = {
16158c2ecf20Sopenharmony_ci	.set_fmt = saa711x_set_fmt,
16168c2ecf20Sopenharmony_ci};
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops saa711x_ops = {
16198c2ecf20Sopenharmony_ci	.core = &saa711x_core_ops,
16208c2ecf20Sopenharmony_ci	.tuner = &saa711x_tuner_ops,
16218c2ecf20Sopenharmony_ci	.audio = &saa711x_audio_ops,
16228c2ecf20Sopenharmony_ci	.video = &saa711x_video_ops,
16238c2ecf20Sopenharmony_ci	.vbi = &saa711x_vbi_ops,
16248c2ecf20Sopenharmony_ci	.pad = &saa711x_pad_ops,
16258c2ecf20Sopenharmony_ci};
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci#define CHIP_VER_SIZE	16
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_cistatic void saa711x_write_platform_data(struct saa711x_state *state,
16328c2ecf20Sopenharmony_ci					struct saa7115_platform_data *data)
16338c2ecf20Sopenharmony_ci{
16348c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &state->sd;
16358c2ecf20Sopenharmony_ci	u8 work;
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	if (state->ident != GM7113C &&
16388c2ecf20Sopenharmony_ci	    state->ident != SAA7113)
16398c2ecf20Sopenharmony_ci		return;
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	if (data->saa7113_r08_htc) {
16428c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_08_SYNC_CNTL);
16438c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_08_HTC_MASK;
16448c2ecf20Sopenharmony_ci		work |= ((*data->saa7113_r08_htc) << SAA7113_R_08_HTC_OFFSET);
16458c2ecf20Sopenharmony_ci		saa711x_write(sd, R_08_SYNC_CNTL, work);
16468c2ecf20Sopenharmony_ci	}
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	if (data->saa7113_r10_vrln) {
16498c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_10_CHROMA_CNTL_2);
16508c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_10_VRLN_MASK;
16518c2ecf20Sopenharmony_ci		if (*data->saa7113_r10_vrln)
16528c2ecf20Sopenharmony_ci			work |= (1 << SAA7113_R_10_VRLN_OFFSET);
16538c2ecf20Sopenharmony_ci		saa711x_write(sd, R_10_CHROMA_CNTL_2, work);
16548c2ecf20Sopenharmony_ci	}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci	if (data->saa7113_r10_ofts) {
16578c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_10_CHROMA_CNTL_2);
16588c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_10_OFTS_MASK;
16598c2ecf20Sopenharmony_ci		work |= (*data->saa7113_r10_ofts << SAA7113_R_10_OFTS_OFFSET);
16608c2ecf20Sopenharmony_ci		saa711x_write(sd, R_10_CHROMA_CNTL_2, work);
16618c2ecf20Sopenharmony_ci	}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	if (data->saa7113_r12_rts0) {
16648c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_12_RT_SIGNAL_CNTL);
16658c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_12_RTS0_MASK;
16668c2ecf20Sopenharmony_ci		work |= (*data->saa7113_r12_rts0 << SAA7113_R_12_RTS0_OFFSET);
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci		/* According to the datasheet,
16698c2ecf20Sopenharmony_ci		 * SAA7113_RTS_DOT_IN should only be used on RTS1 */
16708c2ecf20Sopenharmony_ci		WARN_ON(*data->saa7113_r12_rts0 == SAA7113_RTS_DOT_IN);
16718c2ecf20Sopenharmony_ci		saa711x_write(sd, R_12_RT_SIGNAL_CNTL, work);
16728c2ecf20Sopenharmony_ci	}
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	if (data->saa7113_r12_rts1) {
16758c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_12_RT_SIGNAL_CNTL);
16768c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_12_RTS1_MASK;
16778c2ecf20Sopenharmony_ci		work |= (*data->saa7113_r12_rts1 << SAA7113_R_12_RTS1_OFFSET);
16788c2ecf20Sopenharmony_ci		saa711x_write(sd, R_12_RT_SIGNAL_CNTL, work);
16798c2ecf20Sopenharmony_ci	}
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	if (data->saa7113_r13_adlsb) {
16828c2ecf20Sopenharmony_ci		work = saa711x_read(sd, R_13_RT_X_PORT_OUT_CNTL);
16838c2ecf20Sopenharmony_ci		work &= ~SAA7113_R_13_ADLSB_MASK;
16848c2ecf20Sopenharmony_ci		if (*data->saa7113_r13_adlsb)
16858c2ecf20Sopenharmony_ci			work |= (1 << SAA7113_R_13_ADLSB_OFFSET);
16868c2ecf20Sopenharmony_ci		saa711x_write(sd, R_13_RT_X_PORT_OUT_CNTL, work);
16878c2ecf20Sopenharmony_ci	}
16888c2ecf20Sopenharmony_ci}
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci/**
16918c2ecf20Sopenharmony_ci * saa711x_detect_chip - Detects the saa711x (or clone) variant
16928c2ecf20Sopenharmony_ci * @client:		I2C client structure.
16938c2ecf20Sopenharmony_ci * @id:			I2C device ID structure.
16948c2ecf20Sopenharmony_ci * @name:		Name of the device to be filled.
16958c2ecf20Sopenharmony_ci *
16968c2ecf20Sopenharmony_ci * Detects the Philips/NXP saa711x chip, or some clone of it.
16978c2ecf20Sopenharmony_ci * if 'id' is NULL or id->driver_data is equal to 1, it auto-probes
16988c2ecf20Sopenharmony_ci * the analog demod.
16998c2ecf20Sopenharmony_ci * If the tuner is not found, it returns -ENODEV.
17008c2ecf20Sopenharmony_ci * If auto-detection is disabled and the tuner doesn't match what it was
17018c2ecf20Sopenharmony_ci *	required, it returns -EINVAL and fills 'name'.
17028c2ecf20Sopenharmony_ci * If the chip is found, it returns the chip ID and fills 'name'.
17038c2ecf20Sopenharmony_ci */
17048c2ecf20Sopenharmony_cistatic int saa711x_detect_chip(struct i2c_client *client,
17058c2ecf20Sopenharmony_ci			       const struct i2c_device_id *id,
17068c2ecf20Sopenharmony_ci			       char *name)
17078c2ecf20Sopenharmony_ci{
17088c2ecf20Sopenharmony_ci	char chip_ver[CHIP_VER_SIZE];
17098c2ecf20Sopenharmony_ci	char chip_id;
17108c2ecf20Sopenharmony_ci	int i;
17118c2ecf20Sopenharmony_ci	int autodetect;
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	autodetect = !id || id->driver_data == 1;
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	/* Read the chip version register */
17168c2ecf20Sopenharmony_ci	for (i = 0; i < CHIP_VER_SIZE; i++) {
17178c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, 0, i);
17188c2ecf20Sopenharmony_ci		chip_ver[i] = i2c_smbus_read_byte_data(client, 0);
17198c2ecf20Sopenharmony_ci		name[i] = (chip_ver[i] & 0x0f) + '0';
17208c2ecf20Sopenharmony_ci		if (name[i] > '9')
17218c2ecf20Sopenharmony_ci			name[i] += 'a' - '9' - 1;
17228c2ecf20Sopenharmony_ci	}
17238c2ecf20Sopenharmony_ci	name[i] = '\0';
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci	/* Check if it is a Philips/NXP chip */
17268c2ecf20Sopenharmony_ci	if (!memcmp(name + 1, "f711", 4)) {
17278c2ecf20Sopenharmony_ci		chip_id = name[5];
17288c2ecf20Sopenharmony_ci		snprintf(name, CHIP_VER_SIZE, "saa711%c", chip_id);
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci		if (!autodetect && strcmp(name, id->name))
17318c2ecf20Sopenharmony_ci			return -EINVAL;
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci		switch (chip_id) {
17348c2ecf20Sopenharmony_ci		case '1':
17358c2ecf20Sopenharmony_ci			if (chip_ver[0] & 0xf0) {
17368c2ecf20Sopenharmony_ci				snprintf(name, CHIP_VER_SIZE, "saa711%ca", chip_id);
17378c2ecf20Sopenharmony_ci				v4l_info(client, "saa7111a variant found\n");
17388c2ecf20Sopenharmony_ci				return SAA7111A;
17398c2ecf20Sopenharmony_ci			}
17408c2ecf20Sopenharmony_ci			return SAA7111;
17418c2ecf20Sopenharmony_ci		case '3':
17428c2ecf20Sopenharmony_ci			return SAA7113;
17438c2ecf20Sopenharmony_ci		case '4':
17448c2ecf20Sopenharmony_ci			return SAA7114;
17458c2ecf20Sopenharmony_ci		case '5':
17468c2ecf20Sopenharmony_ci			return SAA7115;
17478c2ecf20Sopenharmony_ci		case '8':
17488c2ecf20Sopenharmony_ci			return SAA7118;
17498c2ecf20Sopenharmony_ci		default:
17508c2ecf20Sopenharmony_ci			v4l2_info(client,
17518c2ecf20Sopenharmony_ci				  "WARNING: Philips/NXP chip unknown - Falling back to saa7111\n");
17528c2ecf20Sopenharmony_ci			return SAA7111;
17538c2ecf20Sopenharmony_ci		}
17548c2ecf20Sopenharmony_ci	}
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	/* Check if it is a gm7113c */
17578c2ecf20Sopenharmony_ci	if (!memcmp(name, "0000", 4)) {
17588c2ecf20Sopenharmony_ci		chip_id = 0;
17598c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++) {
17608c2ecf20Sopenharmony_ci			chip_id = chip_id << 1;
17618c2ecf20Sopenharmony_ci			chip_id |= (chip_ver[i] & 0x80) ? 1 : 0;
17628c2ecf20Sopenharmony_ci		}
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci		/*
17658c2ecf20Sopenharmony_ci		 * Note: From the datasheet, only versions 1 and 2
17668c2ecf20Sopenharmony_ci		 * exists. However, tests on a device labeled as:
17678c2ecf20Sopenharmony_ci		 * "GM7113C 1145" returned "10" on all 16 chip
17688c2ecf20Sopenharmony_ci		 * version (reg 0x00) reads. So, we need to also
17698c2ecf20Sopenharmony_ci		 * accept at least version 0. For now, let's just
17708c2ecf20Sopenharmony_ci		 * assume that a device that returns "0000" for
17718c2ecf20Sopenharmony_ci		 * the lower nibble is a gm7113c.
17728c2ecf20Sopenharmony_ci		 */
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci		strscpy(name, "gm7113c", CHIP_VER_SIZE);
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci		if (!autodetect && strcmp(name, id->name))
17778c2ecf20Sopenharmony_ci			return -EINVAL;
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci		v4l_dbg(1, debug, client,
17808c2ecf20Sopenharmony_ci			"It seems to be a %s chip (%*ph) @ 0x%x.\n",
17818c2ecf20Sopenharmony_ci			name, 16, chip_ver, client->addr << 1);
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci		return GM7113C;
17848c2ecf20Sopenharmony_ci	}
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	/* Check if it is a CJC7113 */
17878c2ecf20Sopenharmony_ci	if (!memcmp(name, "1111111111111111", CHIP_VER_SIZE)) {
17888c2ecf20Sopenharmony_ci		strscpy(name, "cjc7113", CHIP_VER_SIZE);
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci		if (!autodetect && strcmp(name, id->name))
17918c2ecf20Sopenharmony_ci			return -EINVAL;
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci		v4l_dbg(1, debug, client,
17948c2ecf20Sopenharmony_ci			"It seems to be a %s chip (%*ph) @ 0x%x.\n",
17958c2ecf20Sopenharmony_ci			name, 16, chip_ver, client->addr << 1);
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci		/* CJC7113 seems to be SAA7113-compatible */
17988c2ecf20Sopenharmony_ci		return SAA7113;
17998c2ecf20Sopenharmony_ci	}
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_ci	/* Chip was not discovered. Return its ID and don't bind */
18028c2ecf20Sopenharmony_ci	v4l_dbg(1, debug, client, "chip %*ph @ 0x%x is unknown.\n",
18038c2ecf20Sopenharmony_ci		16, chip_ver, client->addr << 1);
18048c2ecf20Sopenharmony_ci	return -ENODEV;
18058c2ecf20Sopenharmony_ci}
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_cistatic int saa711x_probe(struct i2c_client *client,
18088c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
18098c2ecf20Sopenharmony_ci{
18108c2ecf20Sopenharmony_ci	struct saa711x_state *state;
18118c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
18128c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl;
18138c2ecf20Sopenharmony_ci	struct saa7115_platform_data *pdata;
18148c2ecf20Sopenharmony_ci	int ident;
18158c2ecf20Sopenharmony_ci	char name[CHIP_VER_SIZE + 1];
18168c2ecf20Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
18178c2ecf20Sopenharmony_ci	int ret;
18188c2ecf20Sopenharmony_ci#endif
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ci	/* Check if the adapter supports the needed features */
18218c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
18228c2ecf20Sopenharmony_ci		return -EIO;
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_ci	ident = saa711x_detect_chip(client, id, name);
18258c2ecf20Sopenharmony_ci	if (ident == -EINVAL) {
18268c2ecf20Sopenharmony_ci		/* Chip exists, but doesn't match */
18278c2ecf20Sopenharmony_ci		v4l_warn(client, "found %s while %s was expected\n",
18288c2ecf20Sopenharmony_ci			 name, id->name);
18298c2ecf20Sopenharmony_ci		return -ENODEV;
18308c2ecf20Sopenharmony_ci	}
18318c2ecf20Sopenharmony_ci	if (ident < 0)
18328c2ecf20Sopenharmony_ci		return ident;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	strscpy(client->name, name, sizeof(client->name));
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
18378c2ecf20Sopenharmony_ci	if (state == NULL)
18388c2ecf20Sopenharmony_ci		return -ENOMEM;
18398c2ecf20Sopenharmony_ci	sd = &state->sd;
18408c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &saa711x_ops);
18418c2ecf20Sopenharmony_ci
18428c2ecf20Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
18438c2ecf20Sopenharmony_ci	state->pads[SAA711X_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
18448c2ecf20Sopenharmony_ci	state->pads[SAA711X_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
18458c2ecf20Sopenharmony_ci	state->pads[SAA711X_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
18468c2ecf20Sopenharmony_ci	state->pads[SAA711X_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&sd->entity, SAA711X_NUM_PADS,
18518c2ecf20Sopenharmony_ci				     state->pads);
18528c2ecf20Sopenharmony_ci	if (ret < 0)
18538c2ecf20Sopenharmony_ci		return ret;
18548c2ecf20Sopenharmony_ci#endif
18558c2ecf20Sopenharmony_ci
18568c2ecf20Sopenharmony_ci	v4l_info(client, "%s found @ 0x%x (%s)\n", name,
18578c2ecf20Sopenharmony_ci		 client->addr << 1, client->adapter->name);
18588c2ecf20Sopenharmony_ci	hdl = &state->hdl;
18598c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 6);
18608c2ecf20Sopenharmony_ci	/* add in ascending ID order */
18618c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18628c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
18638c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18648c2ecf20Sopenharmony_ci			V4L2_CID_CONTRAST, 0, 127, 1, 64);
18658c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18668c2ecf20Sopenharmony_ci			V4L2_CID_SATURATION, 0, 127, 1, 64);
18678c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18688c2ecf20Sopenharmony_ci			V4L2_CID_HUE, -128, 127, 1, 0);
18698c2ecf20Sopenharmony_ci	state->agc = v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18708c2ecf20Sopenharmony_ci			V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
18718c2ecf20Sopenharmony_ci	state->gain = v4l2_ctrl_new_std(hdl, &saa711x_ctrl_ops,
18728c2ecf20Sopenharmony_ci			V4L2_CID_CHROMA_GAIN, 0, 127, 1, 40);
18738c2ecf20Sopenharmony_ci	sd->ctrl_handler = hdl;
18748c2ecf20Sopenharmony_ci	if (hdl->error) {
18758c2ecf20Sopenharmony_ci		int err = hdl->error;
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
18788c2ecf20Sopenharmony_ci		return err;
18798c2ecf20Sopenharmony_ci	}
18808c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(2, &state->agc, 0, true);
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ci	state->input = -1;
18838c2ecf20Sopenharmony_ci	state->output = SAA7115_IPORT_ON;
18848c2ecf20Sopenharmony_ci	state->enable = 1;
18858c2ecf20Sopenharmony_ci	state->radio = 0;
18868c2ecf20Sopenharmony_ci	state->ident = ident;
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	state->audclk_freq = 48000;
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "writing init values\n");
18918c2ecf20Sopenharmony_ci
18928c2ecf20Sopenharmony_ci	/* init to 60hz/48khz */
18938c2ecf20Sopenharmony_ci	state->crystal_freq = SAA7115_FREQ_24_576_MHZ;
18948c2ecf20Sopenharmony_ci	pdata = client->dev.platform_data;
18958c2ecf20Sopenharmony_ci	switch (state->ident) {
18968c2ecf20Sopenharmony_ci	case SAA7111:
18978c2ecf20Sopenharmony_ci	case SAA7111A:
18988c2ecf20Sopenharmony_ci		saa711x_writeregs(sd, saa7111_init);
18998c2ecf20Sopenharmony_ci		break;
19008c2ecf20Sopenharmony_ci	case GM7113C:
19018c2ecf20Sopenharmony_ci		saa711x_writeregs(sd, gm7113c_init);
19028c2ecf20Sopenharmony_ci		break;
19038c2ecf20Sopenharmony_ci	case SAA7113:
19048c2ecf20Sopenharmony_ci		if (pdata && pdata->saa7113_force_gm7113c_init)
19058c2ecf20Sopenharmony_ci			saa711x_writeregs(sd, gm7113c_init);
19068c2ecf20Sopenharmony_ci		else
19078c2ecf20Sopenharmony_ci			saa711x_writeregs(sd, saa7113_init);
19088c2ecf20Sopenharmony_ci		break;
19098c2ecf20Sopenharmony_ci	default:
19108c2ecf20Sopenharmony_ci		state->crystal_freq = SAA7115_FREQ_32_11_MHZ;
19118c2ecf20Sopenharmony_ci		saa711x_writeregs(sd, saa7115_init_auto_input);
19128c2ecf20Sopenharmony_ci	}
19138c2ecf20Sopenharmony_ci	if (state->ident > SAA7111A && state->ident != GM7113C)
19148c2ecf20Sopenharmony_ci		saa711x_writeregs(sd, saa7115_init_misc);
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci	if (pdata)
19178c2ecf20Sopenharmony_ci		saa711x_write_platform_data(state, pdata);
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	saa711x_set_v4lstd(sd, V4L2_STD_NTSC);
19208c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(hdl);
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "status: (1E) 0x%02x, (1F) 0x%02x\n",
19238c2ecf20Sopenharmony_ci		saa711x_read(sd, R_1E_STATUS_BYTE_1_VD_DEC),
19248c2ecf20Sopenharmony_ci		saa711x_read(sd, R_1F_STATUS_BYTE_2_VD_DEC));
19258c2ecf20Sopenharmony_ci	return 0;
19268c2ecf20Sopenharmony_ci}
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_cistatic int saa711x_remove(struct i2c_client *client)
19318c2ecf20Sopenharmony_ci{
19328c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
19338c2ecf20Sopenharmony_ci
19348c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
19358c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(sd->ctrl_handler);
19368c2ecf20Sopenharmony_ci	return 0;
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_cistatic const struct i2c_device_id saa711x_id[] = {
19408c2ecf20Sopenharmony_ci	{ "saa7115_auto", 1 }, /* autodetect */
19418c2ecf20Sopenharmony_ci	{ "saa7111", 0 },
19428c2ecf20Sopenharmony_ci	{ "saa7113", 0 },
19438c2ecf20Sopenharmony_ci	{ "saa7114", 0 },
19448c2ecf20Sopenharmony_ci	{ "saa7115", 0 },
19458c2ecf20Sopenharmony_ci	{ "saa7118", 0 },
19468c2ecf20Sopenharmony_ci	{ "gm7113c", 0 },
19478c2ecf20Sopenharmony_ci	{ }
19488c2ecf20Sopenharmony_ci};
19498c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, saa711x_id);
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_cistatic struct i2c_driver saa711x_driver = {
19528c2ecf20Sopenharmony_ci	.driver = {
19538c2ecf20Sopenharmony_ci		.name	= "saa7115",
19548c2ecf20Sopenharmony_ci	},
19558c2ecf20Sopenharmony_ci	.probe		= saa711x_probe,
19568c2ecf20Sopenharmony_ci	.remove		= saa711x_remove,
19578c2ecf20Sopenharmony_ci	.id_table	= saa711x_id,
19588c2ecf20Sopenharmony_ci};
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_cimodule_i2c_driver(saa711x_driver);
1961