saa6752hs.c revision 4d6b5aee9d19d1a982d9db0b5ffea20ee94816c6
1#include <linux/module.h>
2#include <linux/kernel.h>
3#include <linux/sched.h>
4#include <linux/string.h>
5#include <linux/timer.h>
6#include <linux/delay.h>
7#include <linux/errno.h>
8#include <linux/slab.h>
9#include <linux/poll.h>
10#include <linux/i2c.h>
11#include <linux/types.h>
12#include <linux/videodev2.h>
13#include <media/v4l2-common.h>
14#include <linux/init.h>
15#include <linux/crc32.h>
16
17
18#define MPEG_VIDEO_TARGET_BITRATE_MAX  27000
19#define MPEG_VIDEO_MAX_BITRATE_MAX     27000
20#define MPEG_TOTAL_TARGET_BITRATE_MAX  27000
21#define MPEG_PID_MAX ((1 << 14) - 1)
22
23/* Addresses to scan */
24static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
25I2C_CLIENT_INSMOD;
26
27MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
28MODULE_AUTHOR("Andrew de Quincey");
29MODULE_LICENSE("GPL");
30
31static struct i2c_driver driver;
32static struct i2c_client client_template;
33
34enum saa6752hs_videoformat {
35	SAA6752HS_VF_D1 = 0,    /* standard D1 video format: 720x576 */
36	SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
37	SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
38	SAA6752HS_VF_SIF = 3,   /* SIF video format: 352x288 */
39	SAA6752HS_VF_UNKNOWN,
40};
41
42struct saa6752hs_mpeg_params {
43	/* transport streams */
44	__u16				ts_pid_pmt;
45	__u16				ts_pid_audio;
46	__u16				ts_pid_video;
47	__u16				ts_pid_pcr;
48
49	/* audio */
50	enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
51
52	/* video */
53	enum v4l2_mpeg_video_aspect	vi_aspect;
54	enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
55	__u32 				vi_bitrate;
56	__u32 				vi_bitrate_peak;
57};
58
59static const struct v4l2_format v4l2_format_table[] =
60{
61	[SAA6752HS_VF_D1] =
62		{ .fmt = { .pix = { .width = 720, .height = 576 }}},
63	[SAA6752HS_VF_2_3_D1] =
64		{ .fmt = { .pix = { .width = 480, .height = 576 }}},
65	[SAA6752HS_VF_1_2_D1] =
66		{ .fmt = { .pix = { .width = 352, .height = 576 }}},
67	[SAA6752HS_VF_SIF] =
68		{ .fmt = { .pix = { .width = 352, .height = 288 }}},
69	[SAA6752HS_VF_UNKNOWN] =
70		{ .fmt = { .pix = { .width = 0, .height = 0}}},
71};
72
73struct saa6752hs_state {
74	struct i2c_client             client;
75	struct v4l2_mpeg_compression  old_params;
76	struct saa6752hs_mpeg_params  params;
77	enum saa6752hs_videoformat    video_format;
78	v4l2_std_id                   standard;
79};
80
81enum saa6752hs_command {
82	SAA6752HS_COMMAND_RESET = 0,
83	SAA6752HS_COMMAND_STOP = 1,
84	SAA6752HS_COMMAND_START = 2,
85	SAA6752HS_COMMAND_PAUSE = 3,
86	SAA6752HS_COMMAND_RECONFIGURE = 4,
87	SAA6752HS_COMMAND_SLEEP = 5,
88	SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
89
90	SAA6752HS_COMMAND_MAX
91};
92
93/* ---------------------------------------------------------------------- */
94
95static u8 PAT[] = {
96	0xc2, /* i2c register */
97	0x00, /* table number for encoder */
98
99	0x47, /* sync */
100	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
101	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
102
103	0x00, /* PSI pointer to start of table */
104
105	0x00, /* tid(0) */
106	0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
107
108	0x00, 0x01, /* transport_stream_id(1) */
109
110	0xc1, /* version_number(0), current_next_indicator(1) */
111
112	0x00, 0x00, /* section_number(0), last_section_number(0) */
113
114	0x00, 0x01, /* program_number(1) */
115
116	0xe0, 0x00, /* PMT PID */
117
118	0x00, 0x00, 0x00, 0x00 /* CRC32 */
119};
120
121static u8 PMT[] = {
122	0xc2, /* i2c register */
123	0x01, /* table number for encoder */
124
125	0x47, /* sync */
126	0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
127	0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
128
129	0x00, /* PSI pointer to start of table */
130
131	0x02, /* tid(2) */
132	0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
133
134	0x00, 0x01, /* program_number(1) */
135
136	0xc1, /* version_number(0), current_next_indicator(1) */
137
138	0x00, 0x00, /* section_number(0), last_section_number(0) */
139
140	0xe0, 0x00, /* PCR_PID */
141
142	0xf0, 0x00, /* program_info_length(0) */
143
144	0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
145	0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
146
147	0x00, 0x00, 0x00, 0x00 /* CRC32 */
148};
149
150static struct saa6752hs_mpeg_params param_defaults =
151{
152	.ts_pid_pmt      = 16,
153	.ts_pid_video    = 260,
154	.ts_pid_audio    = 256,
155	.ts_pid_pcr      = 259,
156
157	.vi_aspect       = V4L2_MPEG_VIDEO_ASPECT_4x3,
158	.vi_bitrate      = 4000,
159	.vi_bitrate_peak = 6000,
160	.vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
161
162	.au_l2_bitrate   = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
163};
164
165static struct v4l2_mpeg_compression old_param_defaults =
166{
167	.st_type         = V4L2_MPEG_TS_2,
168	.st_bitrate      = {
169		.mode    = V4L2_BITRATE_CBR,
170		.target  = 7000,
171	},
172
173	.ts_pid_pmt      = 16,
174	.ts_pid_video    = 260,
175	.ts_pid_audio    = 256,
176	.ts_pid_pcr      = 259,
177
178	.vi_type         = V4L2_MPEG_VI_2,
179	.vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
180	.vi_bitrate      = {
181		.mode    = V4L2_BITRATE_VBR,
182		.target  = 4000,
183		.max     = 6000,
184	},
185
186	.au_type         = V4L2_MPEG_AU_2_II,
187	.au_bitrate      = {
188		.mode    = V4L2_BITRATE_CBR,
189		.target  = 256,
190	},
191
192};
193
194/* ---------------------------------------------------------------------- */
195
196static int saa6752hs_chip_command(struct i2c_client* client,
197				  enum saa6752hs_command command)
198{
199	unsigned char buf[3];
200	unsigned long timeout;
201	int status = 0;
202
203	/* execute the command */
204	switch(command) {
205	case SAA6752HS_COMMAND_RESET:
206		buf[0] = 0x00;
207		break;
208
209	case SAA6752HS_COMMAND_STOP:
210		buf[0] = 0x03;
211		break;
212
213	case SAA6752HS_COMMAND_START:
214		buf[0] = 0x02;
215		break;
216
217	case SAA6752HS_COMMAND_PAUSE:
218		buf[0] = 0x04;
219		break;
220
221	case SAA6752HS_COMMAND_RECONFIGURE:
222		buf[0] = 0x05;
223		break;
224
225	case SAA6752HS_COMMAND_SLEEP:
226		buf[0] = 0x06;
227		break;
228
229	case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
230		buf[0] = 0x07;
231		break;
232
233	default:
234		return -EINVAL;
235	}
236
237	/* set it and wait for it to be so */
238	i2c_master_send(client, buf, 1);
239	timeout = jiffies + HZ * 3;
240	for (;;) {
241		/* get the current status */
242		buf[0] = 0x10;
243		i2c_master_send(client, buf, 1);
244		i2c_master_recv(client, buf, 1);
245
246		if (!(buf[0] & 0x20))
247			break;
248		if (time_after(jiffies,timeout)) {
249			status = -ETIMEDOUT;
250			break;
251		}
252
253		msleep(10);
254	}
255
256	/* delay a bit to let encoder settle */
257	msleep(50);
258
259	return status;
260}
261
262
263static int saa6752hs_set_bitrate(struct i2c_client* client,
264				 struct saa6752hs_mpeg_params* params)
265{
266	u8 buf[3];
267	int tot_bitrate;
268
269	/* set the bitrate mode */
270	buf[0] = 0x71;
271	buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
272	i2c_master_send(client, buf, 2);
273
274	/* set the video bitrate */
275	if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
276		/* set the target bitrate */
277		buf[0] = 0x80;
278		buf[1] = params->vi_bitrate >> 8;
279		buf[2] = params->vi_bitrate & 0xff;
280		i2c_master_send(client, buf, 3);
281
282		/* set the max bitrate */
283		buf[0] = 0x81;
284		buf[1] = params->vi_bitrate_peak >> 8;
285		buf[2] = params->vi_bitrate_peak & 0xff;
286		i2c_master_send(client, buf, 3);
287		tot_bitrate = params->vi_bitrate_peak;
288	} else {
289		/* set the target bitrate (no max bitrate for CBR) */
290		buf[0] = 0x81;
291		buf[1] = params->vi_bitrate >> 8;
292		buf[2] = params->vi_bitrate & 0xff;
293		i2c_master_send(client, buf, 3);
294		tot_bitrate = params->vi_bitrate;
295	}
296
297	/* set the audio bitrate */
298	buf[0] = 0x94;
299	buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1;
300	i2c_master_send(client, buf, 2);
301	tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384;
302
303	/* Note: the total max bitrate is determined by adding the video and audio
304	   bitrates together and also adding an extra 768kbit/s to stay on the
305	   safe side. If more control should be required, then an extra MPEG control
306	   should be added. */
307	tot_bitrate += 768;
308	if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
309		tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
310
311	/* set the total bitrate */
312	buf[0] = 0xb1;
313	buf[1] = tot_bitrate >> 8;
314	buf[2] = tot_bitrate & 0xff;
315	i2c_master_send(client, buf, 3);
316
317	return 0;
318}
319
320static void saa6752hs_set_subsampling(struct i2c_client* client,
321				      struct v4l2_format* f)
322{
323	struct saa6752hs_state *h = i2c_get_clientdata(client);
324	int dist_352, dist_480, dist_720;
325
326	/*
327	  FIXME: translate and round width/height into EMPRESS
328	  subsample type:
329
330	  type   |   PAL   |  NTSC
331	  ---------------------------
332	  SIF    | 352x288 | 352x240
333	  1/2 D1 | 352x576 | 352x480
334	  2/3 D1 | 480x576 | 480x480
335	  D1     | 720x576 | 720x480
336	*/
337
338	dist_352 = abs(f->fmt.pix.width - 352);
339	dist_480 = abs(f->fmt.pix.width - 480);
340	dist_720 = abs(f->fmt.pix.width - 720);
341	if (dist_720 < dist_480) {
342		f->fmt.pix.width = 720;
343		f->fmt.pix.height = 576;
344		h->video_format = SAA6752HS_VF_D1;
345	}
346	else if (dist_480 < dist_352) {
347		f->fmt.pix.width = 480;
348		f->fmt.pix.height = 576;
349		h->video_format = SAA6752HS_VF_2_3_D1;
350	}
351	else {
352		f->fmt.pix.width = 352;
353		if (abs(f->fmt.pix.height - 576) <
354		    abs(f->fmt.pix.height - 288)) {
355			f->fmt.pix.height = 576;
356			h->video_format = SAA6752HS_VF_1_2_D1;
357		}
358		else {
359			f->fmt.pix.height = 288;
360			h->video_format = SAA6752HS_VF_SIF;
361		}
362	}
363}
364
365
366static void saa6752hs_old_set_params(struct i2c_client* client,
367				 struct v4l2_mpeg_compression* params)
368{
369	struct saa6752hs_state *h = i2c_get_clientdata(client);
370
371	/* check PIDs */
372	if (params->ts_pid_pmt <= MPEG_PID_MAX) {
373		h->old_params.ts_pid_pmt = params->ts_pid_pmt;
374		h->params.ts_pid_pmt = params->ts_pid_pmt;
375	}
376	if (params->ts_pid_pcr <= MPEG_PID_MAX) {
377		h->old_params.ts_pid_pcr = params->ts_pid_pcr;
378		h->params.ts_pid_pcr = params->ts_pid_pcr;
379	}
380	if (params->ts_pid_video <= MPEG_PID_MAX) {
381		h->old_params.ts_pid_video = params->ts_pid_video;
382		h->params.ts_pid_video = params->ts_pid_video;
383	}
384	if (params->ts_pid_audio <= MPEG_PID_MAX) {
385		h->old_params.ts_pid_audio = params->ts_pid_audio;
386		h->params.ts_pid_audio = params->ts_pid_audio;
387	}
388
389	/* check bitrate parameters */
390	if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
391	    (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) {
392		h->old_params.vi_bitrate.mode = params->vi_bitrate.mode;
393		h->params.vi_bitrate_mode = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ?
394		       V4L2_MPEG_VIDEO_BITRATE_MODE_VBR : V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
395	}
396	if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
397		h->old_params.st_bitrate.target = params->st_bitrate.target;
398	if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
399		h->old_params.vi_bitrate.target = params->vi_bitrate.target;
400	if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
401		h->old_params.vi_bitrate.max = params->vi_bitrate.max;
402	if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
403		h->old_params.au_bitrate.target = params->au_bitrate.target;
404
405	/* aspect ratio */
406	if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
407	    params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) {
408		h->old_params.vi_aspect_ratio = params->vi_aspect_ratio;
409		if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3)
410			h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
411		else
412			h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_16x9;
413	}
414
415	/* range checks */
416	if (h->old_params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
417		h->old_params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
418	if (h->old_params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
419		h->old_params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
420	if (h->old_params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
421		h->old_params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
422	h->params.vi_bitrate = params->vi_bitrate.target;
423	h->params.vi_bitrate_peak = params->vi_bitrate.max;
424	if (h->old_params.au_bitrate.target <= 256) {
425		h->old_params.au_bitrate.target = 256;
426		h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
427	}
428	else {
429		h->old_params.au_bitrate.target = 384;
430		h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
431	}
432}
433
434static int handle_ctrl(struct saa6752hs_mpeg_params *params,
435		struct v4l2_ext_control *ctrl, unsigned int cmd)
436{
437	int old = 0, new;
438	int set = (cmd == VIDIOC_S_EXT_CTRLS);
439
440	new = ctrl->value;
441	switch (ctrl->id) {
442		case V4L2_CID_MPEG_STREAM_TYPE:
443			old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
444			if (set && new != old)
445				return -ERANGE;
446			new = old;
447			break;
448		case V4L2_CID_MPEG_STREAM_PID_PMT:
449			old = params->ts_pid_pmt;
450			if (set && new > MPEG_PID_MAX)
451				return -ERANGE;
452			if (new > MPEG_PID_MAX)
453				new = MPEG_PID_MAX;
454			params->ts_pid_pmt = new;
455			break;
456		case V4L2_CID_MPEG_STREAM_PID_AUDIO:
457			old = params->ts_pid_audio;
458			if (set && new > MPEG_PID_MAX)
459				return -ERANGE;
460			if (new > MPEG_PID_MAX)
461				new = MPEG_PID_MAX;
462			params->ts_pid_audio = new;
463			break;
464		case V4L2_CID_MPEG_STREAM_PID_VIDEO:
465			old = params->ts_pid_video;
466			if (set && new > MPEG_PID_MAX)
467				return -ERANGE;
468			if (new > MPEG_PID_MAX)
469				new = MPEG_PID_MAX;
470			params->ts_pid_video = new;
471			break;
472		case V4L2_CID_MPEG_STREAM_PID_PCR:
473			old = params->ts_pid_pcr;
474			if (set && new > MPEG_PID_MAX)
475				return -ERANGE;
476			if (new > MPEG_PID_MAX)
477				new = MPEG_PID_MAX;
478			params->ts_pid_pcr = new;
479			break;
480		case V4L2_CID_MPEG_AUDIO_ENCODING:
481			old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
482			if (set && new != old)
483				return -ERANGE;
484			new = old;
485			break;
486		case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
487			old = params->au_l2_bitrate;
488			if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
489				   new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
490				return -ERANGE;
491			if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
492				new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
493			else
494				new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
495			params->au_l2_bitrate = new;
496			break;
497		case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
498			old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
499			if (set && new != old)
500				return -ERANGE;
501			new = old;
502			break;
503		case V4L2_CID_MPEG_VIDEO_ENCODING:
504			old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
505			if (set && new != old)
506				return -ERANGE;
507			new = old;
508			break;
509		case V4L2_CID_MPEG_VIDEO_ASPECT:
510			old = params->vi_aspect;
511			if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
512				   new != V4L2_MPEG_VIDEO_ASPECT_4x3)
513				return -ERANGE;
514			if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
515				new = V4L2_MPEG_VIDEO_ASPECT_4x3;
516			params->vi_aspect = new;
517			break;
518		case V4L2_CID_MPEG_VIDEO_BITRATE:
519			old = params->vi_bitrate * 1000;
520			new = 1000 * (new / 1000);
521			if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
522				return -ERANGE;
523			if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
524				new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
525			params->vi_bitrate = new / 1000;
526			break;
527		case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
528			old = params->vi_bitrate_peak * 1000;
529			new = 1000 * (new / 1000);
530			if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
531				return -ERANGE;
532			if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
533				new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
534			params->vi_bitrate_peak = new / 1000;
535			break;
536		case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
537			old = params->vi_bitrate_mode;
538			params->vi_bitrate_mode = new;
539			break;
540		default:
541			return -EINVAL;
542	}
543	if (cmd == VIDIOC_G_EXT_CTRLS)
544		ctrl->value = old;
545	else
546		ctrl->value = new;
547	return 0;
548}
549
550static int saa6752hs_init(struct i2c_client* client)
551{
552	unsigned char buf[9], buf2[4];
553	struct saa6752hs_state *h;
554	u32 crc;
555	unsigned char localPAT[256];
556	unsigned char localPMT[256];
557
558	h = i2c_get_clientdata(client);
559
560	/* Set video format - must be done first as it resets other settings */
561	buf[0] = 0x41;
562	buf[1] = h->video_format;
563	i2c_master_send(client, buf, 2);
564
565	/* Set number of lines in input signal */
566	buf[0] = 0x40;
567	buf[1] = 0x00;
568	if (h->standard & V4L2_STD_525_60)
569		buf[1] = 0x01;
570	i2c_master_send(client, buf, 2);
571
572	/* set bitrate */
573	saa6752hs_set_bitrate(client, &h->params);
574
575	/* Set GOP structure {3, 13} */
576	buf[0] = 0x72;
577	buf[1] = 0x03;
578	buf[2] = 0x0D;
579	i2c_master_send(client,buf,3);
580
581	/* Set minimum Q-scale {4} */
582	buf[0] = 0x82;
583	buf[1] = 0x04;
584	i2c_master_send(client,buf,2);
585
586	/* Set maximum Q-scale {12} */
587	buf[0] = 0x83;
588	buf[1] = 0x0C;
589	i2c_master_send(client,buf,2);
590
591	/* Set Output Protocol */
592	buf[0] = 0xD0;
593	buf[1] = 0x81;
594	i2c_master_send(client,buf,2);
595
596	/* Set video output stream format {TS} */
597	buf[0] = 0xB0;
598	buf[1] = 0x05;
599	i2c_master_send(client,buf,2);
600
601	/* compute PAT */
602	memcpy(localPAT, PAT, sizeof(PAT));
603	localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
604	localPAT[18] = h->params.ts_pid_pmt & 0xff;
605	crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
606	localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
607	localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
608	localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
609	localPAT[sizeof(PAT) - 1] = crc & 0xFF;
610
611	/* compute PMT */
612	memcpy(localPMT, PMT, sizeof(PMT));
613	localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
614	localPMT[4] = h->params.ts_pid_pmt & 0xff;
615	localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
616	localPMT[16] = h->params.ts_pid_pcr & 0xFF;
617	localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
618	localPMT[21] = h->params.ts_pid_video & 0xFF;
619	localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
620	localPMT[26] = h->params.ts_pid_audio & 0xFF;
621	crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
622	localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
623	localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
624	localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
625	localPMT[sizeof(PMT) - 1] = crc & 0xFF;
626
627	/* Set Audio PID */
628	buf[0] = 0xC1;
629	buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
630	buf[2] = h->params.ts_pid_audio & 0xFF;
631	i2c_master_send(client,buf,3);
632
633	/* Set Video PID */
634	buf[0] = 0xC0;
635	buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
636	buf[2] = h->params.ts_pid_video & 0xFF;
637	i2c_master_send(client,buf,3);
638
639	/* Set PCR PID */
640	buf[0] = 0xC4;
641	buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
642	buf[2] = h->params.ts_pid_pcr & 0xFF;
643	i2c_master_send(client,buf,3);
644
645	/* Send SI tables */
646	i2c_master_send(client,localPAT,sizeof(PAT));
647	i2c_master_send(client,localPMT,sizeof(PMT));
648
649	/* mute then unmute audio. This removes buzzing artefacts */
650	buf[0] = 0xa4;
651	buf[1] = 1;
652	i2c_master_send(client, buf, 2);
653	buf[1] = 0;
654	i2c_master_send(client, buf, 2);
655
656	/* start it going */
657	saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
658
659	/* readout current state */
660	buf[0] = 0xE1;
661	buf[1] = 0xA7;
662	buf[2] = 0xFE;
663	buf[3] = 0x82;
664	buf[4] = 0xB0;
665	i2c_master_send(client, buf, 5);
666	i2c_master_recv(client, buf2, 4);
667
668	/* change aspect ratio */
669	buf[0] = 0xE0;
670	buf[1] = 0xA7;
671	buf[2] = 0xFE;
672	buf[3] = 0x82;
673	buf[4] = 0xB0;
674	buf[5] = buf2[0];
675	switch(h->params.vi_aspect) {
676	case V4L2_MPEG_VIDEO_ASPECT_16x9:
677		buf[6] = buf2[1] | 0x40;
678		break;
679	case V4L2_MPEG_VIDEO_ASPECT_4x3:
680	default:
681		buf[6] = buf2[1] & 0xBF;
682		break;
683		break;
684	}
685	buf[7] = buf2[2];
686	buf[8] = buf2[3];
687	i2c_master_send(client, buf, 9);
688
689	return 0;
690}
691
692static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
693{
694	struct saa6752hs_state *h;
695
696
697	if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
698		return -ENOMEM;
699	h->client = client_template;
700	h->params = param_defaults;
701	h->old_params = old_param_defaults;
702	h->client.adapter = adap;
703	h->client.addr = addr;
704
705	/* Assume 625 input lines */
706	h->standard = 0;
707
708	i2c_set_clientdata(&h->client, h);
709	i2c_attach_client(&h->client);
710
711	v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
712
713	return 0;
714}
715
716static int saa6752hs_probe(struct i2c_adapter *adap)
717{
718	if (adap->class & I2C_CLASS_TV_ANALOG)
719		return i2c_probe(adap, &addr_data, saa6752hs_attach);
720	return 0;
721}
722
723static int saa6752hs_detach(struct i2c_client *client)
724{
725	struct saa6752hs_state *h;
726
727	h = i2c_get_clientdata(client);
728	i2c_detach_client(client);
729	kfree(h);
730	return 0;
731}
732
733static int
734saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
735{
736	struct saa6752hs_state *h = i2c_get_clientdata(client);
737	struct v4l2_ext_controls *ctrls = arg;
738	struct v4l2_mpeg_compression *old_params = arg;
739	struct saa6752hs_mpeg_params params;
740	int err = 0;
741	int i;
742
743	switch (cmd) {
744	case VIDIOC_S_MPEGCOMP:
745		if (NULL == old_params) {
746			/* apply settings and start encoder */
747			saa6752hs_init(client);
748			break;
749		}
750		saa6752hs_old_set_params(client, old_params);
751		/* fall through */
752	case VIDIOC_G_MPEGCOMP:
753		*old_params = h->old_params;
754		break;
755	case VIDIOC_S_EXT_CTRLS:
756		if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
757			return -EINVAL;
758		if (ctrls->count == 0) {
759			/* apply settings and start encoder */
760			saa6752hs_init(client);
761			break;
762		}
763		/* fall through */
764	case VIDIOC_TRY_EXT_CTRLS:
765	case VIDIOC_G_EXT_CTRLS:
766		if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
767			return -EINVAL;
768		params = h->params;
769		for (i = 0; i < ctrls->count; i++) {
770			if ((err = handle_ctrl(&params, ctrls->controls + i, cmd))) {
771				ctrls->error_idx = i;
772				return err;
773			}
774		}
775		h->params = params;
776		break;
777	case VIDIOC_G_FMT:
778	{
779	   struct v4l2_format *f = arg;
780
781	   if (h->video_format == SAA6752HS_VF_UNKNOWN)
782		   h->video_format = SAA6752HS_VF_D1;
783	   f->fmt.pix.width =
784		   v4l2_format_table[h->video_format].fmt.pix.width;
785	   f->fmt.pix.height =
786		   v4l2_format_table[h->video_format].fmt.pix.height;
787	   break ;
788	}
789	case VIDIOC_S_FMT:
790	{
791		struct v4l2_format *f = arg;
792
793		saa6752hs_set_subsampling(client, f);
794		break;
795	}
796	case VIDIOC_S_STD:
797		h->standard = *((v4l2_std_id *) arg);
798		break;
799	default:
800		/* nothing */
801		break;
802	}
803
804	return err;
805}
806
807/* ----------------------------------------------------------------------- */
808
809static struct i2c_driver driver = {
810	.driver = {
811		.name   = "saa6752hs",
812	},
813	.id             = I2C_DRIVERID_SAA6752HS,
814	.attach_adapter = saa6752hs_probe,
815	.detach_client  = saa6752hs_detach,
816	.command        = saa6752hs_command,
817};
818
819static struct i2c_client client_template =
820{
821	.name       = "saa6752hs",
822	.driver     = &driver,
823};
824
825static int __init saa6752hs_init_module(void)
826{
827	return i2c_add_driver(&driver);
828}
829
830static void __exit saa6752hs_cleanup_module(void)
831{
832	i2c_del_driver(&driver);
833}
834
835module_init(saa6752hs_init_module);
836module_exit(saa6752hs_cleanup_module);
837
838/*
839 * Overrides for Emacs so that we follow Linus's tabbing style.
840 * ---------------------------------------------------------------------------
841 * Local variables:
842 * c-basic-offset: 8
843 * End:
844 */
845