xref: /openbmc/linux/drivers/media/i2c/saa6752hs.c (revision fa840ba4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2  /*
3     saa6752hs - i2c-driver for the saa6752hs by Philips
4 
5     Copyright (C) 2004 Andrew de Quincey
6 
7     AC-3 support:
8 
9     Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
10 
11   */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/timer.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/poll.h>
21 #include <linux/i2c.h>
22 #include <linux/types.h>
23 #include <linux/videodev2.h>
24 #include <linux/init.h>
25 #include <linux/crc32.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-common.h>
29 
30 #define MPEG_VIDEO_TARGET_BITRATE_MAX  27000
31 #define MPEG_VIDEO_MAX_BITRATE_MAX     27000
32 #define MPEG_TOTAL_TARGET_BITRATE_MAX  27000
33 #define MPEG_PID_MAX ((1 << 14) - 1)
34 
35 
36 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
37 MODULE_AUTHOR("Andrew de Quincey");
38 MODULE_LICENSE("GPL");
39 
40 enum saa6752hs_videoformat {
41 	SAA6752HS_VF_D1 = 0,    /* standard D1 video format: 720x576 */
42 	SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
43 	SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
44 	SAA6752HS_VF_SIF = 3,   /* SIF video format: 352x288 */
45 	SAA6752HS_VF_UNKNOWN,
46 };
47 
48 struct saa6752hs_mpeg_params {
49 	/* transport streams */
50 	__u16				ts_pid_pmt;
51 	__u16				ts_pid_audio;
52 	__u16				ts_pid_video;
53 	__u16				ts_pid_pcr;
54 
55 	/* audio */
56 	enum v4l2_mpeg_audio_encoding    au_encoding;
57 	enum v4l2_mpeg_audio_l2_bitrate  au_l2_bitrate;
58 	enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
59 
60 	/* video */
61 	enum v4l2_mpeg_video_aspect	vi_aspect;
62 	enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
63 	__u32				vi_bitrate;
64 	__u32				vi_bitrate_peak;
65 };
66 
67 static const struct v4l2_format v4l2_format_table[] =
68 {
69 	[SAA6752HS_VF_D1] =
70 		{ .fmt = { .pix = { .width = 720, .height = 576 }}},
71 	[SAA6752HS_VF_2_3_D1] =
72 		{ .fmt = { .pix = { .width = 480, .height = 576 }}},
73 	[SAA6752HS_VF_1_2_D1] =
74 		{ .fmt = { .pix = { .width = 352, .height = 576 }}},
75 	[SAA6752HS_VF_SIF] =
76 		{ .fmt = { .pix = { .width = 352, .height = 288 }}},
77 	[SAA6752HS_VF_UNKNOWN] =
78 		{ .fmt = { .pix = { .width = 0, .height = 0}}},
79 };
80 
81 struct saa6752hs_state {
82 	struct v4l2_subdev            sd;
83 	struct v4l2_ctrl_handler      hdl;
84 	struct { /* video bitrate mode control cluster */
85 		struct v4l2_ctrl *video_bitrate_mode;
86 		struct v4l2_ctrl *video_bitrate;
87 		struct v4l2_ctrl *video_bitrate_peak;
88 	};
89 	u32			      revision;
90 	int			      has_ac3;
91 	struct saa6752hs_mpeg_params  params;
92 	enum saa6752hs_videoformat    video_format;
93 	v4l2_std_id                   standard;
94 };
95 
96 enum saa6752hs_command {
97 	SAA6752HS_COMMAND_RESET = 0,
98 	SAA6752HS_COMMAND_STOP = 1,
99 	SAA6752HS_COMMAND_START = 2,
100 	SAA6752HS_COMMAND_PAUSE = 3,
101 	SAA6752HS_COMMAND_RECONFIGURE = 4,
102 	SAA6752HS_COMMAND_SLEEP = 5,
103 	SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
104 
105 	SAA6752HS_COMMAND_MAX
106 };
107 
108 static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
109 {
110 	return container_of(sd, struct saa6752hs_state, sd);
111 }
112 
113 /* ---------------------------------------------------------------------- */
114 
115 static const u8 PAT[] = {
116 	0xc2, /* i2c register */
117 	0x00, /* table number for encoder */
118 
119 	0x47, /* sync */
120 	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
121 	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
122 
123 	0x00, /* PSI pointer to start of table */
124 
125 	0x00, /* tid(0) */
126 	0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
127 
128 	0x00, 0x01, /* transport_stream_id(1) */
129 
130 	0xc1, /* version_number(0), current_next_indicator(1) */
131 
132 	0x00, 0x00, /* section_number(0), last_section_number(0) */
133 
134 	0x00, 0x01, /* program_number(1) */
135 
136 	0xe0, 0x00, /* PMT PID */
137 
138 	0x00, 0x00, 0x00, 0x00 /* CRC32 */
139 };
140 
141 static const u8 PMT[] = {
142 	0xc2, /* i2c register */
143 	0x01, /* table number for encoder */
144 
145 	0x47, /* sync */
146 	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
147 	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
148 
149 	0x00, /* PSI pointer to start of table */
150 
151 	0x02, /* tid(2) */
152 	0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
153 
154 	0x00, 0x01, /* program_number(1) */
155 
156 	0xc1, /* version_number(0), current_next_indicator(1) */
157 
158 	0x00, 0x00, /* section_number(0), last_section_number(0) */
159 
160 	0xe0, 0x00, /* PCR_PID */
161 
162 	0xf0, 0x00, /* program_info_length(0) */
163 
164 	0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
165 	0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
166 
167 	0x00, 0x00, 0x00, 0x00 /* CRC32 */
168 };
169 
170 static const u8 PMT_AC3[] = {
171 	0xc2, /* i2c register */
172 	0x01, /* table number for encoder(1) */
173 	0x47, /* sync */
174 
175 	0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
176 	0x10, /* PMT PID (0x0010) */
177 	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
178 
179 	0x00, /* PSI pointer to start of table */
180 
181 	0x02, /* TID (2) */
182 	0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
183 
184 	0x00, 0x01, /* program_number(1) */
185 
186 	0xc1, /* version_number(0), current_next_indicator(1) */
187 
188 	0x00, 0x00, /* section_number(0), last_section_number(0) */
189 
190 	0xe1, 0x04, /* PCR_PID (0x0104) */
191 
192 	0xf0, 0x00, /* program_info_length(0) */
193 
194 	0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
195 	0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
196 	0x6a, /* AC3 */
197 	0x01, /* Descriptor_length(1) */
198 	0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
199 
200 	0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
201 };
202 
203 static const struct saa6752hs_mpeg_params param_defaults =
204 {
205 	.ts_pid_pmt      = 16,
206 	.ts_pid_video    = 260,
207 	.ts_pid_audio    = 256,
208 	.ts_pid_pcr      = 259,
209 
210 	.vi_aspect       = V4L2_MPEG_VIDEO_ASPECT_4x3,
211 	.vi_bitrate      = 4000,
212 	.vi_bitrate_peak = 6000,
213 	.vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
214 
215 	.au_encoding     = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
216 	.au_l2_bitrate   = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
217 	.au_ac3_bitrate  = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
218 };
219 
220 /* ---------------------------------------------------------------------- */
221 
222 static int saa6752hs_chip_command(struct i2c_client *client,
223 				  enum saa6752hs_command command)
224 {
225 	unsigned char buf[3];
226 	unsigned long timeout;
227 	int status = 0;
228 
229 	/* execute the command */
230 	switch(command) {
231 	case SAA6752HS_COMMAND_RESET:
232 		buf[0] = 0x00;
233 		break;
234 
235 	case SAA6752HS_COMMAND_STOP:
236 		buf[0] = 0x03;
237 		break;
238 
239 	case SAA6752HS_COMMAND_START:
240 		buf[0] = 0x02;
241 		break;
242 
243 	case SAA6752HS_COMMAND_PAUSE:
244 		buf[0] = 0x04;
245 		break;
246 
247 	case SAA6752HS_COMMAND_RECONFIGURE:
248 		buf[0] = 0x05;
249 		break;
250 
251 	case SAA6752HS_COMMAND_SLEEP:
252 		buf[0] = 0x06;
253 		break;
254 
255 	case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
256 		buf[0] = 0x07;
257 		break;
258 
259 	default:
260 		return -EINVAL;
261 	}
262 
263 	/* set it and wait for it to be so */
264 	i2c_master_send(client, buf, 1);
265 	timeout = jiffies + HZ * 3;
266 	for (;;) {
267 		/* get the current status */
268 		buf[0] = 0x10;
269 		i2c_master_send(client, buf, 1);
270 		i2c_master_recv(client, buf, 1);
271 
272 		if (!(buf[0] & 0x20))
273 			break;
274 		if (time_after(jiffies,timeout)) {
275 			status = -ETIMEDOUT;
276 			break;
277 		}
278 
279 		msleep(10);
280 	}
281 
282 	/* delay a bit to let encoder settle */
283 	msleep(50);
284 
285 	return status;
286 }
287 
288 
289 static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
290 {
291 	u8 buf[2];
292 
293 	buf[0] = reg;
294 	buf[1] = val;
295 	i2c_master_send(client, buf, 2);
296 }
297 
298 static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
299 {
300 	u8 buf[3];
301 
302 	buf[0] = reg;
303 	buf[1] = val >> 8;
304 	buf[2] = val & 0xff;
305 	i2c_master_send(client, buf, 3);
306 }
307 
308 static int saa6752hs_set_bitrate(struct i2c_client *client,
309 				 struct saa6752hs_state *h)
310 {
311 	struct saa6752hs_mpeg_params *params = &h->params;
312 	int tot_bitrate;
313 	int is_384k;
314 
315 	/* set the bitrate mode */
316 	set_reg8(client, 0x71,
317 		params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
318 
319 	/* set the video bitrate */
320 	if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
321 		/* set the target bitrate */
322 		set_reg16(client, 0x80, params->vi_bitrate);
323 
324 		/* set the max bitrate */
325 		set_reg16(client, 0x81, params->vi_bitrate_peak);
326 		tot_bitrate = params->vi_bitrate_peak;
327 	} else {
328 		/* set the target bitrate (no max bitrate for CBR) */
329 		set_reg16(client, 0x81, params->vi_bitrate);
330 		tot_bitrate = params->vi_bitrate;
331 	}
332 
333 	/* set the audio encoding */
334 	set_reg8(client, 0x93,
335 			params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
336 
337 	/* set the audio bitrate */
338 	if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
339 		is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
340 	else
341 		is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
342 	set_reg8(client, 0x94, is_384k);
343 	tot_bitrate += is_384k ? 384 : 256;
344 
345 	/* Note: the total max bitrate is determined by adding the video and audio
346 	   bitrates together and also adding an extra 768kbit/s to stay on the
347 	   safe side. If more control should be required, then an extra MPEG control
348 	   should be added. */
349 	tot_bitrate += 768;
350 	if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
351 		tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
352 
353 	/* set the total bitrate */
354 	set_reg16(client, 0xb1, tot_bitrate);
355 	return 0;
356 }
357 
358 static int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
359 {
360 	struct saa6752hs_state *h =
361 		container_of(ctrl->handler, struct saa6752hs_state, hdl);
362 
363 	switch (ctrl->id) {
364 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
365 		/* peak bitrate shall be >= normal bitrate */
366 		if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
367 		    h->video_bitrate_peak->val < h->video_bitrate->val)
368 			h->video_bitrate_peak->val = h->video_bitrate->val;
369 		break;
370 	}
371 	return 0;
372 }
373 
374 static int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
375 {
376 	struct saa6752hs_state *h =
377 		container_of(ctrl->handler, struct saa6752hs_state, hdl);
378 	struct saa6752hs_mpeg_params *params = &h->params;
379 
380 	switch (ctrl->id) {
381 	case V4L2_CID_MPEG_STREAM_TYPE:
382 		break;
383 	case V4L2_CID_MPEG_STREAM_PID_PMT:
384 		params->ts_pid_pmt = ctrl->val;
385 		break;
386 	case V4L2_CID_MPEG_STREAM_PID_AUDIO:
387 		params->ts_pid_audio = ctrl->val;
388 		break;
389 	case V4L2_CID_MPEG_STREAM_PID_VIDEO:
390 		params->ts_pid_video = ctrl->val;
391 		break;
392 	case V4L2_CID_MPEG_STREAM_PID_PCR:
393 		params->ts_pid_pcr = ctrl->val;
394 		break;
395 	case V4L2_CID_MPEG_AUDIO_ENCODING:
396 		params->au_encoding = ctrl->val;
397 		break;
398 	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
399 		params->au_l2_bitrate = ctrl->val;
400 		break;
401 	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
402 		params->au_ac3_bitrate = ctrl->val;
403 		break;
404 	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
405 		break;
406 	case V4L2_CID_MPEG_VIDEO_ENCODING:
407 		break;
408 	case V4L2_CID_MPEG_VIDEO_ASPECT:
409 		params->vi_aspect = ctrl->val;
410 		break;
411 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
412 		params->vi_bitrate_mode = ctrl->val;
413 		params->vi_bitrate = h->video_bitrate->val / 1000;
414 		params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
415 		v4l2_ctrl_activate(h->video_bitrate_peak,
416 				ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
417 		break;
418 	default:
419 		return -EINVAL;
420 	}
421 	return 0;
422 }
423 
424 static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
425 {
426 	unsigned char buf[9], buf2[4];
427 	struct saa6752hs_state *h = to_state(sd);
428 	struct i2c_client *client = v4l2_get_subdevdata(sd);
429 	unsigned size;
430 	u32 crc;
431 	unsigned char localPAT[256];
432 	unsigned char localPMT[256];
433 
434 	/* Set video format - must be done first as it resets other settings */
435 	set_reg8(client, 0x41, h->video_format);
436 
437 	/* Set number of lines in input signal */
438 	set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
439 
440 	/* set bitrate */
441 	saa6752hs_set_bitrate(client, h);
442 
443 	/* Set GOP structure {3, 13} */
444 	set_reg16(client, 0x72, 0x030d);
445 
446 	/* Set minimum Q-scale {4} */
447 	set_reg8(client, 0x82, 0x04);
448 
449 	/* Set maximum Q-scale {12} */
450 	set_reg8(client, 0x83, 0x0c);
451 
452 	/* Set Output Protocol */
453 	set_reg8(client, 0xd0, 0x81);
454 
455 	/* Set video output stream format {TS} */
456 	set_reg8(client, 0xb0, 0x05);
457 
458 	/* Set leading null byte for TS */
459 	set_reg16(client, 0xf6, leading_null_bytes);
460 
461 	/* compute PAT */
462 	memcpy(localPAT, PAT, sizeof(PAT));
463 	localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
464 	localPAT[18] = h->params.ts_pid_pmt & 0xff;
465 	crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
466 	localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
467 	localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
468 	localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
469 	localPAT[sizeof(PAT) - 1] = crc & 0xFF;
470 
471 	/* compute PMT */
472 	if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
473 		size = sizeof(PMT_AC3);
474 		memcpy(localPMT, PMT_AC3, size);
475 	} else {
476 		size = sizeof(PMT);
477 		memcpy(localPMT, PMT, size);
478 	}
479 	localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
480 	localPMT[4] = h->params.ts_pid_pmt & 0xff;
481 	localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
482 	localPMT[16] = h->params.ts_pid_pcr & 0xFF;
483 	localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
484 	localPMT[21] = h->params.ts_pid_video & 0xFF;
485 	localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
486 	localPMT[26] = h->params.ts_pid_audio & 0xFF;
487 	crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
488 	localPMT[size - 4] = (crc >> 24) & 0xFF;
489 	localPMT[size - 3] = (crc >> 16) & 0xFF;
490 	localPMT[size - 2] = (crc >> 8) & 0xFF;
491 	localPMT[size - 1] = crc & 0xFF;
492 
493 	/* Set Audio PID */
494 	set_reg16(client, 0xc1, h->params.ts_pid_audio);
495 
496 	/* Set Video PID */
497 	set_reg16(client, 0xc0, h->params.ts_pid_video);
498 
499 	/* Set PCR PID */
500 	set_reg16(client, 0xc4, h->params.ts_pid_pcr);
501 
502 	/* Send SI tables */
503 	i2c_master_send(client, localPAT, sizeof(PAT));
504 	i2c_master_send(client, localPMT, size);
505 
506 	/* mute then unmute audio. This removes buzzing artefacts */
507 	set_reg8(client, 0xa4, 1);
508 	set_reg8(client, 0xa4, 0);
509 
510 	/* start it going */
511 	saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
512 
513 	/* readout current state */
514 	buf[0] = 0xE1;
515 	buf[1] = 0xA7;
516 	buf[2] = 0xFE;
517 	buf[3] = 0x82;
518 	buf[4] = 0xB0;
519 	i2c_master_send(client, buf, 5);
520 	i2c_master_recv(client, buf2, 4);
521 
522 	/* change aspect ratio */
523 	buf[0] = 0xE0;
524 	buf[1] = 0xA7;
525 	buf[2] = 0xFE;
526 	buf[3] = 0x82;
527 	buf[4] = 0xB0;
528 	buf[5] = buf2[0];
529 	switch (h->params.vi_aspect) {
530 	case V4L2_MPEG_VIDEO_ASPECT_16x9:
531 		buf[6] = buf2[1] | 0x40;
532 		break;
533 	case V4L2_MPEG_VIDEO_ASPECT_4x3:
534 	default:
535 		buf[6] = buf2[1] & 0xBF;
536 		break;
537 	}
538 	buf[7] = buf2[2];
539 	buf[8] = buf2[3];
540 	i2c_master_send(client, buf, 9);
541 
542 	return 0;
543 }
544 
545 static int saa6752hs_get_fmt(struct v4l2_subdev *sd,
546 		struct v4l2_subdev_state *sd_state,
547 		struct v4l2_subdev_format *format)
548 {
549 	struct v4l2_mbus_framefmt *f = &format->format;
550 	struct saa6752hs_state *h = to_state(sd);
551 
552 	if (format->pad)
553 		return -EINVAL;
554 
555 	if (h->video_format == SAA6752HS_VF_UNKNOWN)
556 		h->video_format = SAA6752HS_VF_D1;
557 	f->width = v4l2_format_table[h->video_format].fmt.pix.width;
558 	f->height = v4l2_format_table[h->video_format].fmt.pix.height;
559 	f->code = MEDIA_BUS_FMT_FIXED;
560 	f->field = V4L2_FIELD_INTERLACED;
561 	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
562 	return 0;
563 }
564 
565 static int saa6752hs_set_fmt(struct v4l2_subdev *sd,
566 		struct v4l2_subdev_state *sd_state,
567 		struct v4l2_subdev_format *format)
568 {
569 	struct v4l2_mbus_framefmt *f = &format->format;
570 	struct saa6752hs_state *h = to_state(sd);
571 	int dist_352, dist_480, dist_720;
572 
573 	if (format->pad)
574 		return -EINVAL;
575 
576 	f->code = MEDIA_BUS_FMT_FIXED;
577 
578 	dist_352 = abs(f->width - 352);
579 	dist_480 = abs(f->width - 480);
580 	dist_720 = abs(f->width - 720);
581 	if (dist_720 < dist_480) {
582 		f->width = 720;
583 		f->height = 576;
584 	} else if (dist_480 < dist_352) {
585 		f->width = 480;
586 		f->height = 576;
587 	} else {
588 		f->width = 352;
589 		if (abs(f->height - 576) < abs(f->height - 288))
590 			f->height = 576;
591 		else
592 			f->height = 288;
593 	}
594 	f->field = V4L2_FIELD_INTERLACED;
595 	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
596 
597 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
598 		sd_state->pads->try_fmt = *f;
599 		return 0;
600 	}
601 
602 	/*
603 	  FIXME: translate and round width/height into EMPRESS
604 	  subsample type:
605 
606 	  type   |   PAL   |  NTSC
607 	  ---------------------------
608 	  SIF    | 352x288 | 352x240
609 	  1/2 D1 | 352x576 | 352x480
610 	  2/3 D1 | 480x576 | 480x480
611 	  D1     | 720x576 | 720x480
612 	*/
613 
614 	if (f->code != MEDIA_BUS_FMT_FIXED)
615 		return -EINVAL;
616 
617 	if (f->width == 720)
618 		h->video_format = SAA6752HS_VF_D1;
619 	else if (f->width == 480)
620 		h->video_format = SAA6752HS_VF_2_3_D1;
621 	else if (f->height == 576)
622 		h->video_format = SAA6752HS_VF_1_2_D1;
623 	else
624 		h->video_format = SAA6752HS_VF_SIF;
625 	return 0;
626 }
627 
628 static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
629 {
630 	struct saa6752hs_state *h = to_state(sd);
631 
632 	h->standard = std;
633 	return 0;
634 }
635 
636 /* ----------------------------------------------------------------------- */
637 
638 static const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
639 	.try_ctrl = saa6752hs_try_ctrl,
640 	.s_ctrl = saa6752hs_s_ctrl,
641 };
642 
643 static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
644 	.init = saa6752hs_init,
645 };
646 
647 static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
648 	.s_std = saa6752hs_s_std,
649 };
650 
651 static const struct v4l2_subdev_pad_ops saa6752hs_pad_ops = {
652 	.get_fmt = saa6752hs_get_fmt,
653 	.set_fmt = saa6752hs_set_fmt,
654 };
655 
656 static const struct v4l2_subdev_ops saa6752hs_ops = {
657 	.core = &saa6752hs_core_ops,
658 	.video = &saa6752hs_video_ops,
659 	.pad = &saa6752hs_pad_ops,
660 };
661 
662 static int saa6752hs_probe(struct i2c_client *client)
663 {
664 	struct saa6752hs_state *h;
665 	struct v4l2_subdev *sd;
666 	struct v4l2_ctrl_handler *hdl;
667 	u8 addr = 0x13;
668 	u8 data[12];
669 
670 	v4l_info(client, "chip found @ 0x%x (%s)\n",
671 			client->addr << 1, client->adapter->name);
672 
673 	h = devm_kzalloc(&client->dev, sizeof(*h), GFP_KERNEL);
674 	if (h == NULL)
675 		return -ENOMEM;
676 	sd = &h->sd;
677 	v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
678 
679 	i2c_master_send(client, &addr, 1);
680 	i2c_master_recv(client, data, sizeof(data));
681 	h->revision = (data[8] << 8) | data[9];
682 	h->has_ac3 = 0;
683 	if (h->revision == 0x0206) {
684 		h->has_ac3 = 1;
685 		v4l_info(client, "supports AC-3\n");
686 	}
687 	h->params = param_defaults;
688 
689 	hdl = &h->hdl;
690 	v4l2_ctrl_handler_init(hdl, 14);
691 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
692 		V4L2_CID_MPEG_AUDIO_ENCODING,
693 		h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
694 			V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
695 		0x0d, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
696 
697 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
698 		V4L2_CID_MPEG_AUDIO_L2_BITRATE,
699 		V4L2_MPEG_AUDIO_L2_BITRATE_384K,
700 		~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
701 		  (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
702 		V4L2_MPEG_AUDIO_L2_BITRATE_256K);
703 
704 	if (h->has_ac3)
705 		v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
706 			V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
707 			V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
708 			~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
709 			  (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
710 			V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
711 
712 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
713 		V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
714 		V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
715 		~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
716 		V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
717 
718 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
719 		V4L2_CID_MPEG_VIDEO_ENCODING,
720 		V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
721 		~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
722 		V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
723 
724 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
725 		V4L2_CID_MPEG_VIDEO_ASPECT,
726 		V4L2_MPEG_VIDEO_ASPECT_16x9, 0x01,
727 		V4L2_MPEG_VIDEO_ASPECT_4x3);
728 
729 	h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
730 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
731 		1000000, 27000000, 1000, 8000000);
732 
733 	v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
734 		V4L2_CID_MPEG_STREAM_TYPE,
735 		V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
736 		~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
737 		V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
738 
739 	h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
740 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
741 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
742 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
743 	h->video_bitrate = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
744 		V4L2_CID_MPEG_VIDEO_BITRATE, 1000000, 27000000, 1000, 6000000);
745 	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
746 		V4L2_CID_MPEG_STREAM_PID_PMT, 0, (1 << 14) - 1, 1, 16);
747 	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
748 		V4L2_CID_MPEG_STREAM_PID_AUDIO, 0, (1 << 14) - 1, 1, 260);
749 	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
750 		V4L2_CID_MPEG_STREAM_PID_VIDEO, 0, (1 << 14) - 1, 1, 256);
751 	v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
752 		V4L2_CID_MPEG_STREAM_PID_PCR, 0, (1 << 14) - 1, 1, 259);
753 	sd->ctrl_handler = hdl;
754 	if (hdl->error) {
755 		int err = hdl->error;
756 
757 		v4l2_ctrl_handler_free(hdl);
758 		return err;
759 	}
760 	v4l2_ctrl_cluster(3, &h->video_bitrate_mode);
761 	v4l2_ctrl_handler_setup(hdl);
762 	h->standard = 0; /* Assume 625 input lines */
763 	return 0;
764 }
765 
766 static void saa6752hs_remove(struct i2c_client *client)
767 {
768 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
769 
770 	v4l2_device_unregister_subdev(sd);
771 	v4l2_ctrl_handler_free(&to_state(sd)->hdl);
772 }
773 
774 static const struct i2c_device_id saa6752hs_id[] = {
775 	{ "saa6752hs", 0 },
776 	{ }
777 };
778 MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
779 
780 static struct i2c_driver saa6752hs_driver = {
781 	.driver = {
782 		.name	= "saa6752hs",
783 	},
784 	.probe		= saa6752hs_probe,
785 	.remove		= saa6752hs_remove,
786 	.id_table	= saa6752hs_id,
787 };
788 
789 module_i2c_driver(saa6752hs_driver);
790