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