18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci /*
38c2ecf20Sopenharmony_ci    saa6752hs - i2c-driver for the saa6752hs by Philips
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci    Copyright (C) 2004 Andrew de Quincey
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci    AC-3 support:
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci    Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci  */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/string.h>
168c2ecf20Sopenharmony_ci#include <linux/timer.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/errno.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/poll.h>
218c2ecf20Sopenharmony_ci#include <linux/i2c.h>
228c2ecf20Sopenharmony_ci#include <linux/types.h>
238c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/crc32.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define MPEG_VIDEO_TARGET_BITRATE_MAX  27000
318c2ecf20Sopenharmony_ci#define MPEG_VIDEO_MAX_BITRATE_MAX     27000
328c2ecf20Sopenharmony_ci#define MPEG_TOTAL_TARGET_BITRATE_MAX  27000
338c2ecf20Sopenharmony_ci#define MPEG_PID_MAX ((1 << 14) - 1)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew de Quincey");
388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cienum saa6752hs_videoformat {
418c2ecf20Sopenharmony_ci	SAA6752HS_VF_D1 = 0,    /* standard D1 video format: 720x576 */
428c2ecf20Sopenharmony_ci	SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
438c2ecf20Sopenharmony_ci	SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
448c2ecf20Sopenharmony_ci	SAA6752HS_VF_SIF = 3,   /* SIF video format: 352x288 */
458c2ecf20Sopenharmony_ci	SAA6752HS_VF_UNKNOWN,
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct saa6752hs_mpeg_params {
498c2ecf20Sopenharmony_ci	/* transport streams */
508c2ecf20Sopenharmony_ci	__u16				ts_pid_pmt;
518c2ecf20Sopenharmony_ci	__u16				ts_pid_audio;
528c2ecf20Sopenharmony_ci	__u16				ts_pid_video;
538c2ecf20Sopenharmony_ci	__u16				ts_pid_pcr;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* audio */
568c2ecf20Sopenharmony_ci	enum v4l2_mpeg_audio_encoding    au_encoding;
578c2ecf20Sopenharmony_ci	enum v4l2_mpeg_audio_l2_bitrate  au_l2_bitrate;
588c2ecf20Sopenharmony_ci	enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* video */
618c2ecf20Sopenharmony_ci	enum v4l2_mpeg_video_aspect	vi_aspect;
628c2ecf20Sopenharmony_ci	enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
638c2ecf20Sopenharmony_ci	__u32				vi_bitrate;
648c2ecf20Sopenharmony_ci	__u32				vi_bitrate_peak;
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic const struct v4l2_format v4l2_format_table[] =
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	[SAA6752HS_VF_D1] =
708c2ecf20Sopenharmony_ci		{ .fmt = { .pix = { .width = 720, .height = 576 }}},
718c2ecf20Sopenharmony_ci	[SAA6752HS_VF_2_3_D1] =
728c2ecf20Sopenharmony_ci		{ .fmt = { .pix = { .width = 480, .height = 576 }}},
738c2ecf20Sopenharmony_ci	[SAA6752HS_VF_1_2_D1] =
748c2ecf20Sopenharmony_ci		{ .fmt = { .pix = { .width = 352, .height = 576 }}},
758c2ecf20Sopenharmony_ci	[SAA6752HS_VF_SIF] =
768c2ecf20Sopenharmony_ci		{ .fmt = { .pix = { .width = 352, .height = 288 }}},
778c2ecf20Sopenharmony_ci	[SAA6752HS_VF_UNKNOWN] =
788c2ecf20Sopenharmony_ci		{ .fmt = { .pix = { .width = 0, .height = 0}}},
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct saa6752hs_state {
828c2ecf20Sopenharmony_ci	struct v4l2_subdev            sd;
838c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler      hdl;
848c2ecf20Sopenharmony_ci	struct { /* video bitrate mode control cluster */
858c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_bitrate_mode;
868c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_bitrate;
878c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_bitrate_peak;
888c2ecf20Sopenharmony_ci	};
898c2ecf20Sopenharmony_ci	u32			      revision;
908c2ecf20Sopenharmony_ci	int			      has_ac3;
918c2ecf20Sopenharmony_ci	struct saa6752hs_mpeg_params  params;
928c2ecf20Sopenharmony_ci	enum saa6752hs_videoformat    video_format;
938c2ecf20Sopenharmony_ci	v4l2_std_id                   standard;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cienum saa6752hs_command {
978c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_RESET = 0,
988c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_STOP = 1,
998c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_START = 2,
1008c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_PAUSE = 3,
1018c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_RECONFIGURE = 4,
1028c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_SLEEP = 5,
1038c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	SAA6752HS_COMMAND_MAX
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return container_of(sd, struct saa6752hs_state, sd);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------------- */
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic const u8 PAT[] = {
1168c2ecf20Sopenharmony_ci	0xc2, /* i2c register */
1178c2ecf20Sopenharmony_ci	0x00, /* table number for encoder */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	0x47, /* sync */
1208c2ecf20Sopenharmony_ci	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
1218c2ecf20Sopenharmony_ci	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	0x00, /* PSI pointer to start of table */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	0x00, /* tid(0) */
1268c2ecf20Sopenharmony_ci	0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	0x00, 0x01, /* transport_stream_id(1) */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	0xc1, /* version_number(0), current_next_indicator(1) */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	0x00, 0x00, /* section_number(0), last_section_number(0) */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	0x00, 0x01, /* program_number(1) */
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	0xe0, 0x00, /* PMT PID */
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00 /* CRC32 */
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic const u8 PMT[] = {
1428c2ecf20Sopenharmony_ci	0xc2, /* i2c register */
1438c2ecf20Sopenharmony_ci	0x01, /* table number for encoder */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	0x47, /* sync */
1468c2ecf20Sopenharmony_ci	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
1478c2ecf20Sopenharmony_ci	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	0x00, /* PSI pointer to start of table */
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	0x02, /* tid(2) */
1528c2ecf20Sopenharmony_ci	0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	0x00, 0x01, /* program_number(1) */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	0xc1, /* version_number(0), current_next_indicator(1) */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	0x00, 0x00, /* section_number(0), last_section_number(0) */
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	0xe0, 0x00, /* PCR_PID */
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	0xf0, 0x00, /* program_info_length(0) */
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
1658c2ecf20Sopenharmony_ci	0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00 /* CRC32 */
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic const u8 PMT_AC3[] = {
1718c2ecf20Sopenharmony_ci	0xc2, /* i2c register */
1728c2ecf20Sopenharmony_ci	0x01, /* table number for encoder(1) */
1738c2ecf20Sopenharmony_ci	0x47, /* sync */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
1768c2ecf20Sopenharmony_ci	0x10, /* PMT PID (0x0010) */
1778c2ecf20Sopenharmony_ci	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	0x00, /* PSI pointer to start of table */
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	0x02, /* TID (2) */
1828c2ecf20Sopenharmony_ci	0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	0x00, 0x01, /* program_number(1) */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	0xc1, /* version_number(0), current_next_indicator(1) */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	0x00, 0x00, /* section_number(0), last_section_number(0) */
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	0xe1, 0x04, /* PCR_PID (0x0104) */
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	0xf0, 0x00, /* program_info_length(0) */
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
1958c2ecf20Sopenharmony_ci	0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
1968c2ecf20Sopenharmony_ci	0x6a, /* AC3 */
1978c2ecf20Sopenharmony_ci	0x01, /* Descriptor_length(1) */
1988c2ecf20Sopenharmony_ci	0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic const struct saa6752hs_mpeg_params param_defaults =
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	.ts_pid_pmt      = 16,
2068c2ecf20Sopenharmony_ci	.ts_pid_video    = 260,
2078c2ecf20Sopenharmony_ci	.ts_pid_audio    = 256,
2088c2ecf20Sopenharmony_ci	.ts_pid_pcr      = 259,
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	.vi_aspect       = V4L2_MPEG_VIDEO_ASPECT_4x3,
2118c2ecf20Sopenharmony_ci	.vi_bitrate      = 4000,
2128c2ecf20Sopenharmony_ci	.vi_bitrate_peak = 6000,
2138c2ecf20Sopenharmony_ci	.vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	.au_encoding     = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
2168c2ecf20Sopenharmony_ci	.au_l2_bitrate   = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
2178c2ecf20Sopenharmony_ci	.au_ac3_bitrate  = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------------- */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic int saa6752hs_chip_command(struct i2c_client *client,
2238c2ecf20Sopenharmony_ci				  enum saa6752hs_command command)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	unsigned char buf[3];
2268c2ecf20Sopenharmony_ci	unsigned long timeout;
2278c2ecf20Sopenharmony_ci	int status = 0;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* execute the command */
2308c2ecf20Sopenharmony_ci	switch(command) {
2318c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_RESET:
2328c2ecf20Sopenharmony_ci		buf[0] = 0x00;
2338c2ecf20Sopenharmony_ci		break;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_STOP:
2368c2ecf20Sopenharmony_ci		buf[0] = 0x03;
2378c2ecf20Sopenharmony_ci		break;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_START:
2408c2ecf20Sopenharmony_ci		buf[0] = 0x02;
2418c2ecf20Sopenharmony_ci		break;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_PAUSE:
2448c2ecf20Sopenharmony_ci		buf[0] = 0x04;
2458c2ecf20Sopenharmony_ci		break;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_RECONFIGURE:
2488c2ecf20Sopenharmony_ci		buf[0] = 0x05;
2498c2ecf20Sopenharmony_ci		break;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_SLEEP:
2528c2ecf20Sopenharmony_ci		buf[0] = 0x06;
2538c2ecf20Sopenharmony_ci		break;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
2568c2ecf20Sopenharmony_ci		buf[0] = 0x07;
2578c2ecf20Sopenharmony_ci		break;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	default:
2608c2ecf20Sopenharmony_ci		return -EINVAL;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* set it and wait for it to be so */
2648c2ecf20Sopenharmony_ci	i2c_master_send(client, buf, 1);
2658c2ecf20Sopenharmony_ci	timeout = jiffies + HZ * 3;
2668c2ecf20Sopenharmony_ci	for (;;) {
2678c2ecf20Sopenharmony_ci		/* get the current status */
2688c2ecf20Sopenharmony_ci		buf[0] = 0x10;
2698c2ecf20Sopenharmony_ci		i2c_master_send(client, buf, 1);
2708c2ecf20Sopenharmony_ci		i2c_master_recv(client, buf, 1);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		if (!(buf[0] & 0x20))
2738c2ecf20Sopenharmony_ci			break;
2748c2ecf20Sopenharmony_ci		if (time_after(jiffies,timeout)) {
2758c2ecf20Sopenharmony_ci			status = -ETIMEDOUT;
2768c2ecf20Sopenharmony_ci			break;
2778c2ecf20Sopenharmony_ci		}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci		msleep(10);
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/* delay a bit to let encoder settle */
2838c2ecf20Sopenharmony_ci	msleep(50);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	return status;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	u8 buf[2];
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	buf[0] = reg;
2948c2ecf20Sopenharmony_ci	buf[1] = val;
2958c2ecf20Sopenharmony_ci	i2c_master_send(client, buf, 2);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	u8 buf[3];
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	buf[0] = reg;
3038c2ecf20Sopenharmony_ci	buf[1] = val >> 8;
3048c2ecf20Sopenharmony_ci	buf[2] = val & 0xff;
3058c2ecf20Sopenharmony_ci	i2c_master_send(client, buf, 3);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic int saa6752hs_set_bitrate(struct i2c_client *client,
3098c2ecf20Sopenharmony_ci				 struct saa6752hs_state *h)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct saa6752hs_mpeg_params *params = &h->params;
3128c2ecf20Sopenharmony_ci	int tot_bitrate;
3138c2ecf20Sopenharmony_ci	int is_384k;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* set the bitrate mode */
3168c2ecf20Sopenharmony_ci	set_reg8(client, 0x71,
3178c2ecf20Sopenharmony_ci		params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* set the video bitrate */
3208c2ecf20Sopenharmony_ci	if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
3218c2ecf20Sopenharmony_ci		/* set the target bitrate */
3228c2ecf20Sopenharmony_ci		set_reg16(client, 0x80, params->vi_bitrate);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		/* set the max bitrate */
3258c2ecf20Sopenharmony_ci		set_reg16(client, 0x81, params->vi_bitrate_peak);
3268c2ecf20Sopenharmony_ci		tot_bitrate = params->vi_bitrate_peak;
3278c2ecf20Sopenharmony_ci	} else {
3288c2ecf20Sopenharmony_ci		/* set the target bitrate (no max bitrate for CBR) */
3298c2ecf20Sopenharmony_ci		set_reg16(client, 0x81, params->vi_bitrate);
3308c2ecf20Sopenharmony_ci		tot_bitrate = params->vi_bitrate;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* set the audio encoding */
3348c2ecf20Sopenharmony_ci	set_reg8(client, 0x93,
3358c2ecf20Sopenharmony_ci			params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* set the audio bitrate */
3388c2ecf20Sopenharmony_ci	if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
3398c2ecf20Sopenharmony_ci		is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
3408c2ecf20Sopenharmony_ci	else
3418c2ecf20Sopenharmony_ci		is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
3428c2ecf20Sopenharmony_ci	set_reg8(client, 0x94, is_384k);
3438c2ecf20Sopenharmony_ci	tot_bitrate += is_384k ? 384 : 256;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Note: the total max bitrate is determined by adding the video and audio
3468c2ecf20Sopenharmony_ci	   bitrates together and also adding an extra 768kbit/s to stay on the
3478c2ecf20Sopenharmony_ci	   safe side. If more control should be required, then an extra MPEG control
3488c2ecf20Sopenharmony_ci	   should be added. */
3498c2ecf20Sopenharmony_ci	tot_bitrate += 768;
3508c2ecf20Sopenharmony_ci	if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
3518c2ecf20Sopenharmony_ci		tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	/* set the total bitrate */
3548c2ecf20Sopenharmony_ci	set_reg16(client, 0xb1, tot_bitrate);
3558c2ecf20Sopenharmony_ci	return 0;
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct saa6752hs_state *h =
3618c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct saa6752hs_state, hdl);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	switch (ctrl->id) {
3648c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
3658c2ecf20Sopenharmony_ci		/* peak bitrate shall be >= normal bitrate */
3668c2ecf20Sopenharmony_ci		if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
3678c2ecf20Sopenharmony_ci		    h->video_bitrate_peak->val < h->video_bitrate->val)
3688c2ecf20Sopenharmony_ci			h->video_bitrate_peak->val = h->video_bitrate->val;
3698c2ecf20Sopenharmony_ci		break;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci	return 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct saa6752hs_state *h =
3778c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct saa6752hs_state, hdl);
3788c2ecf20Sopenharmony_ci	struct saa6752hs_mpeg_params *params = &h->params;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	switch (ctrl->id) {
3818c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_STREAM_TYPE:
3828c2ecf20Sopenharmony_ci		break;
3838c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_STREAM_PID_PMT:
3848c2ecf20Sopenharmony_ci		params->ts_pid_pmt = ctrl->val;
3858c2ecf20Sopenharmony_ci		break;
3868c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_STREAM_PID_AUDIO:
3878c2ecf20Sopenharmony_ci		params->ts_pid_audio = ctrl->val;
3888c2ecf20Sopenharmony_ci		break;
3898c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_STREAM_PID_VIDEO:
3908c2ecf20Sopenharmony_ci		params->ts_pid_video = ctrl->val;
3918c2ecf20Sopenharmony_ci		break;
3928c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_STREAM_PID_PCR:
3938c2ecf20Sopenharmony_ci		params->ts_pid_pcr = ctrl->val;
3948c2ecf20Sopenharmony_ci		break;
3958c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_AUDIO_ENCODING:
3968c2ecf20Sopenharmony_ci		params->au_encoding = ctrl->val;
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
3998c2ecf20Sopenharmony_ci		params->au_l2_bitrate = ctrl->val;
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
4028c2ecf20Sopenharmony_ci		params->au_ac3_bitrate = ctrl->val;
4038c2ecf20Sopenharmony_ci		break;
4048c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
4058c2ecf20Sopenharmony_ci		break;
4068c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_ENCODING:
4078c2ecf20Sopenharmony_ci		break;
4088c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_ASPECT:
4098c2ecf20Sopenharmony_ci		params->vi_aspect = ctrl->val;
4108c2ecf20Sopenharmony_ci		break;
4118c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
4128c2ecf20Sopenharmony_ci		params->vi_bitrate_mode = ctrl->val;
4138c2ecf20Sopenharmony_ci		params->vi_bitrate = h->video_bitrate->val / 1000;
4148c2ecf20Sopenharmony_ci		params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
4158c2ecf20Sopenharmony_ci		v4l2_ctrl_activate(h->video_bitrate_peak,
4168c2ecf20Sopenharmony_ci				ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	default:
4198c2ecf20Sopenharmony_ci		return -EINVAL;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci	return 0;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	unsigned char buf[9], buf2[4];
4278c2ecf20Sopenharmony_ci	struct saa6752hs_state *h = to_state(sd);
4288c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4298c2ecf20Sopenharmony_ci	unsigned size;
4308c2ecf20Sopenharmony_ci	u32 crc;
4318c2ecf20Sopenharmony_ci	unsigned char localPAT[256];
4328c2ecf20Sopenharmony_ci	unsigned char localPMT[256];
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* Set video format - must be done first as it resets other settings */
4358c2ecf20Sopenharmony_ci	set_reg8(client, 0x41, h->video_format);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Set number of lines in input signal */
4388c2ecf20Sopenharmony_ci	set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	/* set bitrate */
4418c2ecf20Sopenharmony_ci	saa6752hs_set_bitrate(client, h);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/* Set GOP structure {3, 13} */
4448c2ecf20Sopenharmony_ci	set_reg16(client, 0x72, 0x030d);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* Set minimum Q-scale {4} */
4478c2ecf20Sopenharmony_ci	set_reg8(client, 0x82, 0x04);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	/* Set maximum Q-scale {12} */
4508c2ecf20Sopenharmony_ci	set_reg8(client, 0x83, 0x0c);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	/* Set Output Protocol */
4538c2ecf20Sopenharmony_ci	set_reg8(client, 0xd0, 0x81);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Set video output stream format {TS} */
4568c2ecf20Sopenharmony_ci	set_reg8(client, 0xb0, 0x05);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* Set leading null byte for TS */
4598c2ecf20Sopenharmony_ci	set_reg16(client, 0xf6, leading_null_bytes);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	/* compute PAT */
4628c2ecf20Sopenharmony_ci	memcpy(localPAT, PAT, sizeof(PAT));
4638c2ecf20Sopenharmony_ci	localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
4648c2ecf20Sopenharmony_ci	localPAT[18] = h->params.ts_pid_pmt & 0xff;
4658c2ecf20Sopenharmony_ci	crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
4668c2ecf20Sopenharmony_ci	localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
4678c2ecf20Sopenharmony_ci	localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
4688c2ecf20Sopenharmony_ci	localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
4698c2ecf20Sopenharmony_ci	localPAT[sizeof(PAT) - 1] = crc & 0xFF;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* compute PMT */
4728c2ecf20Sopenharmony_ci	if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
4738c2ecf20Sopenharmony_ci		size = sizeof(PMT_AC3);
4748c2ecf20Sopenharmony_ci		memcpy(localPMT, PMT_AC3, size);
4758c2ecf20Sopenharmony_ci	} else {
4768c2ecf20Sopenharmony_ci		size = sizeof(PMT);
4778c2ecf20Sopenharmony_ci		memcpy(localPMT, PMT, size);
4788c2ecf20Sopenharmony_ci	}
4798c2ecf20Sopenharmony_ci	localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
4808c2ecf20Sopenharmony_ci	localPMT[4] = h->params.ts_pid_pmt & 0xff;
4818c2ecf20Sopenharmony_ci	localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
4828c2ecf20Sopenharmony_ci	localPMT[16] = h->params.ts_pid_pcr & 0xFF;
4838c2ecf20Sopenharmony_ci	localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
4848c2ecf20Sopenharmony_ci	localPMT[21] = h->params.ts_pid_video & 0xFF;
4858c2ecf20Sopenharmony_ci	localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
4868c2ecf20Sopenharmony_ci	localPMT[26] = h->params.ts_pid_audio & 0xFF;
4878c2ecf20Sopenharmony_ci	crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
4888c2ecf20Sopenharmony_ci	localPMT[size - 4] = (crc >> 24) & 0xFF;
4898c2ecf20Sopenharmony_ci	localPMT[size - 3] = (crc >> 16) & 0xFF;
4908c2ecf20Sopenharmony_ci	localPMT[size - 2] = (crc >> 8) & 0xFF;
4918c2ecf20Sopenharmony_ci	localPMT[size - 1] = crc & 0xFF;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* Set Audio PID */
4948c2ecf20Sopenharmony_ci	set_reg16(client, 0xc1, h->params.ts_pid_audio);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	/* Set Video PID */
4978c2ecf20Sopenharmony_ci	set_reg16(client, 0xc0, h->params.ts_pid_video);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/* Set PCR PID */
5008c2ecf20Sopenharmony_ci	set_reg16(client, 0xc4, h->params.ts_pid_pcr);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/* Send SI tables */
5038c2ecf20Sopenharmony_ci	i2c_master_send(client, localPAT, sizeof(PAT));
5048c2ecf20Sopenharmony_ci	i2c_master_send(client, localPMT, size);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* mute then unmute audio. This removes buzzing artefacts */
5078c2ecf20Sopenharmony_ci	set_reg8(client, 0xa4, 1);
5088c2ecf20Sopenharmony_ci	set_reg8(client, 0xa4, 0);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	/* start it going */
5118c2ecf20Sopenharmony_ci	saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	/* readout current state */
5148c2ecf20Sopenharmony_ci	buf[0] = 0xE1;
5158c2ecf20Sopenharmony_ci	buf[1] = 0xA7;
5168c2ecf20Sopenharmony_ci	buf[2] = 0xFE;
5178c2ecf20Sopenharmony_ci	buf[3] = 0x82;
5188c2ecf20Sopenharmony_ci	buf[4] = 0xB0;
5198c2ecf20Sopenharmony_ci	i2c_master_send(client, buf, 5);
5208c2ecf20Sopenharmony_ci	i2c_master_recv(client, buf2, 4);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	/* change aspect ratio */
5238c2ecf20Sopenharmony_ci	buf[0] = 0xE0;
5248c2ecf20Sopenharmony_ci	buf[1] = 0xA7;
5258c2ecf20Sopenharmony_ci	buf[2] = 0xFE;
5268c2ecf20Sopenharmony_ci	buf[3] = 0x82;
5278c2ecf20Sopenharmony_ci	buf[4] = 0xB0;
5288c2ecf20Sopenharmony_ci	buf[5] = buf2[0];
5298c2ecf20Sopenharmony_ci	switch (h->params.vi_aspect) {
5308c2ecf20Sopenharmony_ci	case V4L2_MPEG_VIDEO_ASPECT_16x9:
5318c2ecf20Sopenharmony_ci		buf[6] = buf2[1] | 0x40;
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	case V4L2_MPEG_VIDEO_ASPECT_4x3:
5348c2ecf20Sopenharmony_ci	default:
5358c2ecf20Sopenharmony_ci		buf[6] = buf2[1] & 0xBF;
5368c2ecf20Sopenharmony_ci		break;
5378c2ecf20Sopenharmony_ci	}
5388c2ecf20Sopenharmony_ci	buf[7] = buf2[2];
5398c2ecf20Sopenharmony_ci	buf[8] = buf2[3];
5408c2ecf20Sopenharmony_ci	i2c_master_send(client, buf, 9);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	return 0;
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistatic int saa6752hs_get_fmt(struct v4l2_subdev *sd,
5468c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
5478c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *f = &format->format;
5508c2ecf20Sopenharmony_ci	struct saa6752hs_state *h = to_state(sd);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	if (format->pad)
5538c2ecf20Sopenharmony_ci		return -EINVAL;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (h->video_format == SAA6752HS_VF_UNKNOWN)
5568c2ecf20Sopenharmony_ci		h->video_format = SAA6752HS_VF_D1;
5578c2ecf20Sopenharmony_ci	f->width = v4l2_format_table[h->video_format].fmt.pix.width;
5588c2ecf20Sopenharmony_ci	f->height = v4l2_format_table[h->video_format].fmt.pix.height;
5598c2ecf20Sopenharmony_ci	f->code = MEDIA_BUS_FMT_FIXED;
5608c2ecf20Sopenharmony_ci	f->field = V4L2_FIELD_INTERLACED;
5618c2ecf20Sopenharmony_ci	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
5628c2ecf20Sopenharmony_ci	return 0;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic int saa6752hs_set_fmt(struct v4l2_subdev *sd,
5668c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
5678c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *f = &format->format;
5708c2ecf20Sopenharmony_ci	struct saa6752hs_state *h = to_state(sd);
5718c2ecf20Sopenharmony_ci	int dist_352, dist_480, dist_720;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	if (format->pad)
5748c2ecf20Sopenharmony_ci		return -EINVAL;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	f->code = MEDIA_BUS_FMT_FIXED;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	dist_352 = abs(f->width - 352);
5798c2ecf20Sopenharmony_ci	dist_480 = abs(f->width - 480);
5808c2ecf20Sopenharmony_ci	dist_720 = abs(f->width - 720);
5818c2ecf20Sopenharmony_ci	if (dist_720 < dist_480) {
5828c2ecf20Sopenharmony_ci		f->width = 720;
5838c2ecf20Sopenharmony_ci		f->height = 576;
5848c2ecf20Sopenharmony_ci	} else if (dist_480 < dist_352) {
5858c2ecf20Sopenharmony_ci		f->width = 480;
5868c2ecf20Sopenharmony_ci		f->height = 576;
5878c2ecf20Sopenharmony_ci	} else {
5888c2ecf20Sopenharmony_ci		f->width = 352;
5898c2ecf20Sopenharmony_ci		if (abs(f->height - 576) < abs(f->height - 288))
5908c2ecf20Sopenharmony_ci			f->height = 576;
5918c2ecf20Sopenharmony_ci		else
5928c2ecf20Sopenharmony_ci			f->height = 288;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci	f->field = V4L2_FIELD_INTERLACED;
5958c2ecf20Sopenharmony_ci	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
5988c2ecf20Sopenharmony_ci		cfg->try_fmt = *f;
5998c2ecf20Sopenharmony_ci		return 0;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/*
6038c2ecf20Sopenharmony_ci	  FIXME: translate and round width/height into EMPRESS
6048c2ecf20Sopenharmony_ci	  subsample type:
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	  type   |   PAL   |  NTSC
6078c2ecf20Sopenharmony_ci	  ---------------------------
6088c2ecf20Sopenharmony_ci	  SIF    | 352x288 | 352x240
6098c2ecf20Sopenharmony_ci	  1/2 D1 | 352x576 | 352x480
6108c2ecf20Sopenharmony_ci	  2/3 D1 | 480x576 | 480x480
6118c2ecf20Sopenharmony_ci	  D1     | 720x576 | 720x480
6128c2ecf20Sopenharmony_ci	*/
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (f->code != MEDIA_BUS_FMT_FIXED)
6158c2ecf20Sopenharmony_ci		return -EINVAL;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (f->width == 720)
6188c2ecf20Sopenharmony_ci		h->video_format = SAA6752HS_VF_D1;
6198c2ecf20Sopenharmony_ci	else if (f->width == 480)
6208c2ecf20Sopenharmony_ci		h->video_format = SAA6752HS_VF_2_3_D1;
6218c2ecf20Sopenharmony_ci	else if (f->height == 576)
6228c2ecf20Sopenharmony_ci		h->video_format = SAA6752HS_VF_1_2_D1;
6238c2ecf20Sopenharmony_ci	else
6248c2ecf20Sopenharmony_ci		h->video_format = SAA6752HS_VF_SIF;
6258c2ecf20Sopenharmony_ci	return 0;
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_cistatic int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	struct saa6752hs_state *h = to_state(sd);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	h->standard = std;
6338c2ecf20Sopenharmony_ci	return 0;
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
6398c2ecf20Sopenharmony_ci	.try_ctrl = saa6752hs_try_ctrl,
6408c2ecf20Sopenharmony_ci	.s_ctrl = saa6752hs_s_ctrl,
6418c2ecf20Sopenharmony_ci};
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
6448c2ecf20Sopenharmony_ci	.init = saa6752hs_init,
6458c2ecf20Sopenharmony_ci};
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
6488c2ecf20Sopenharmony_ci	.s_std = saa6752hs_s_std,
6498c2ecf20Sopenharmony_ci};
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops saa6752hs_pad_ops = {
6528c2ecf20Sopenharmony_ci	.get_fmt = saa6752hs_get_fmt,
6538c2ecf20Sopenharmony_ci	.set_fmt = saa6752hs_set_fmt,
6548c2ecf20Sopenharmony_ci};
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops saa6752hs_ops = {
6578c2ecf20Sopenharmony_ci	.core = &saa6752hs_core_ops,
6588c2ecf20Sopenharmony_ci	.video = &saa6752hs_video_ops,
6598c2ecf20Sopenharmony_ci	.pad = &saa6752hs_pad_ops,
6608c2ecf20Sopenharmony_ci};
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic int saa6752hs_probe(struct i2c_client *client,
6638c2ecf20Sopenharmony_ci		const struct i2c_device_id *id)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	struct saa6752hs_state *h;
6668c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
6678c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl;
6688c2ecf20Sopenharmony_ci	u8 addr = 0x13;
6698c2ecf20Sopenharmony_ci	u8 data[12];
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
6728c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	h = devm_kzalloc(&client->dev, sizeof(*h), GFP_KERNEL);
6758c2ecf20Sopenharmony_ci	if (h == NULL)
6768c2ecf20Sopenharmony_ci		return -ENOMEM;
6778c2ecf20Sopenharmony_ci	sd = &h->sd;
6788c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	i2c_master_send(client, &addr, 1);
6818c2ecf20Sopenharmony_ci	i2c_master_recv(client, data, sizeof(data));
6828c2ecf20Sopenharmony_ci	h->revision = (data[8] << 8) | data[9];
6838c2ecf20Sopenharmony_ci	h->has_ac3 = 0;
6848c2ecf20Sopenharmony_ci	if (h->revision == 0x0206) {
6858c2ecf20Sopenharmony_ci		h->has_ac3 = 1;
6868c2ecf20Sopenharmony_ci		v4l_info(client, "supports AC-3\n");
6878c2ecf20Sopenharmony_ci	}
6888c2ecf20Sopenharmony_ci	h->params = param_defaults;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	hdl = &h->hdl;
6918c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 14);
6928c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
6938c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_AUDIO_ENCODING,
6948c2ecf20Sopenharmony_ci		h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
6958c2ecf20Sopenharmony_ci			V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
6968c2ecf20Sopenharmony_ci		0x0d, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
6998c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_AUDIO_L2_BITRATE,
7008c2ecf20Sopenharmony_ci		V4L2_MPEG_AUDIO_L2_BITRATE_384K,
7018c2ecf20Sopenharmony_ci		~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
7028c2ecf20Sopenharmony_ci		  (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
7038c2ecf20Sopenharmony_ci		V4L2_MPEG_AUDIO_L2_BITRATE_256K);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (h->has_ac3)
7068c2ecf20Sopenharmony_ci		v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7078c2ecf20Sopenharmony_ci			V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
7088c2ecf20Sopenharmony_ci			V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
7098c2ecf20Sopenharmony_ci			~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
7108c2ecf20Sopenharmony_ci			  (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
7118c2ecf20Sopenharmony_ci			V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7148c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
7158c2ecf20Sopenharmony_ci		V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
7168c2ecf20Sopenharmony_ci		~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
7178c2ecf20Sopenharmony_ci		V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7208c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_VIDEO_ENCODING,
7218c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
7228c2ecf20Sopenharmony_ci		~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
7238c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7268c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_VIDEO_ASPECT,
7278c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_ASPECT_16x9, 0x01,
7288c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_ASPECT_4x3);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7318c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
7328c2ecf20Sopenharmony_ci		1000000, 27000000, 1000, 8000000);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7358c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_STREAM_TYPE,
7368c2ecf20Sopenharmony_ci		V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
7378c2ecf20Sopenharmony_ci		~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
7388c2ecf20Sopenharmony_ci		V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
7418c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
7428c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
7438c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
7448c2ecf20Sopenharmony_ci	h->video_bitrate = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7458c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_VIDEO_BITRATE, 1000000, 27000000, 1000, 6000000);
7468c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7478c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_STREAM_PID_PMT, 0, (1 << 14) - 1, 1, 16);
7488c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7498c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_STREAM_PID_AUDIO, 0, (1 << 14) - 1, 1, 260);
7508c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7518c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_STREAM_PID_VIDEO, 0, (1 << 14) - 1, 1, 256);
7528c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
7538c2ecf20Sopenharmony_ci		V4L2_CID_MPEG_STREAM_PID_PCR, 0, (1 << 14) - 1, 1, 259);
7548c2ecf20Sopenharmony_ci	sd->ctrl_handler = hdl;
7558c2ecf20Sopenharmony_ci	if (hdl->error) {
7568c2ecf20Sopenharmony_ci		int err = hdl->error;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
7598c2ecf20Sopenharmony_ci		return err;
7608c2ecf20Sopenharmony_ci	}
7618c2ecf20Sopenharmony_ci	v4l2_ctrl_cluster(3, &h->video_bitrate_mode);
7628c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(hdl);
7638c2ecf20Sopenharmony_ci	h->standard = 0; /* Assume 625 input lines */
7648c2ecf20Sopenharmony_ci	return 0;
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic int saa6752hs_remove(struct i2c_client *client)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
7728c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&to_state(sd)->hdl);
7738c2ecf20Sopenharmony_ci	return 0;
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic const struct i2c_device_id saa6752hs_id[] = {
7778c2ecf20Sopenharmony_ci	{ "saa6752hs", 0 },
7788c2ecf20Sopenharmony_ci	{ }
7798c2ecf20Sopenharmony_ci};
7808c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, saa6752hs_id);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic struct i2c_driver saa6752hs_driver = {
7838c2ecf20Sopenharmony_ci	.driver = {
7848c2ecf20Sopenharmony_ci		.name	= "saa6752hs",
7858c2ecf20Sopenharmony_ci	},
7868c2ecf20Sopenharmony_ci	.probe		= saa6752hs_probe,
7878c2ecf20Sopenharmony_ci	.remove		= saa6752hs_remove,
7888c2ecf20Sopenharmony_ci	.id_table	= saa6752hs_id,
7898c2ecf20Sopenharmony_ci};
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_cimodule_i2c_driver(saa6752hs_driver);
792