2 saa6752hs - i2c-driver for the saa6752hs by Philips
4 Copyright (C) 2004 Andrew de Quincey
8 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License vs published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/timer.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/poll.h>
33 #include <linux/i2c.h>
34 #include <linux/types.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <media/v4l2-i2c-drv-legacy.h>
39 #include <linux/init.h>
40 #include <linux/crc32.h>
42 #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
43 #define MPEG_VIDEO_MAX_BITRATE_MAX 27000
44 #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
45 #define MPEG_PID_MAX ((1 << 14) - 1)
47 /* Addresses to scan */
48 static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
52 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
53 MODULE_AUTHOR("Andrew de Quincey");
54 MODULE_LICENSE("GPL");
56 enum saa6752hs_videoformat {
57 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
58 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
59 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
60 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
64 struct saa6752hs_mpeg_params {
65 /* transport streams */
72 enum v4l2_mpeg_audio_encoding au_encoding;
73 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
74 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
77 enum v4l2_mpeg_video_aspect vi_aspect;
78 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
80 __u32 vi_bitrate_peak;
83 static const struct v4l2_format v4l2_format_table[] =
86 { .fmt = { .pix = { .width = 720, .height = 576 }}},
87 [SAA6752HS_VF_2_3_D1] =
88 { .fmt = { .pix = { .width = 480, .height = 576 }}},
89 [SAA6752HS_VF_1_2_D1] =
90 { .fmt = { .pix = { .width = 352, .height = 576 }}},
92 { .fmt = { .pix = { .width = 352, .height = 288 }}},
93 [SAA6752HS_VF_UNKNOWN] =
94 { .fmt = { .pix = { .width = 0, .height = 0}}},
97 struct saa6752hs_state {
101 struct saa6752hs_mpeg_params params;
102 enum saa6752hs_videoformat video_format;
103 v4l2_std_id standard;
106 enum saa6752hs_command {
107 SAA6752HS_COMMAND_RESET = 0,
108 SAA6752HS_COMMAND_STOP = 1,
109 SAA6752HS_COMMAND_START = 2,
110 SAA6752HS_COMMAND_PAUSE = 3,
111 SAA6752HS_COMMAND_RECONFIGURE = 4,
112 SAA6752HS_COMMAND_SLEEP = 5,
113 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
115 SAA6752HS_COMMAND_MAX
118 /* ---------------------------------------------------------------------- */
121 0xc2, /* i2c register */
122 0x00, /* table number for encoder */
125 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
126 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
128 0x00, /* PSI pointer to start of table */
131 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
133 0x00, 0x01, /* transport_stream_id(1) */
135 0xc1, /* version_number(0), current_next_indicator(1) */
137 0x00, 0x00, /* section_number(0), last_section_number(0) */
139 0x00, 0x01, /* program_number(1) */
141 0xe0, 0x00, /* PMT PID */
143 0x00, 0x00, 0x00, 0x00 /* CRC32 */
147 0xc2, /* i2c register */
148 0x01, /* table number for encoder */
151 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
152 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
154 0x00, /* PSI pointer to start of table */
157 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
159 0x00, 0x01, /* program_number(1) */
161 0xc1, /* version_number(0), current_next_indicator(1) */
163 0x00, 0x00, /* section_number(0), last_section_number(0) */
165 0xe0, 0x00, /* PCR_PID */
167 0xf0, 0x00, /* program_info_length(0) */
169 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
170 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
172 0x00, 0x00, 0x00, 0x00 /* CRC32 */
175 static u8 PMT_AC3[] = {
176 0xc2, /* i2c register */
177 0x01, /* table number for encoder(1) */
180 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
181 0x10, /* PMT PID (0x0010) */
182 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
184 0x00, /* PSI pointer to start of table */
187 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
189 0x00, 0x01, /* program_number(1) */
191 0xc1, /* version_number(0), current_next_indicator(1) */
193 0x00, 0x00, /* section_number(0), last_section_number(0) */
195 0xe1, 0x04, /* PCR_PID (0x0104) */
197 0xf0, 0x00, /* program_info_length(0) */
199 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
200 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
202 0x01, /* Descriptor_length(1) */
203 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
205 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
208 static struct saa6752hs_mpeg_params param_defaults =
215 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
217 .vi_bitrate_peak = 6000,
218 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
220 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
221 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
222 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
225 /* ---------------------------------------------------------------------- */
227 static int saa6752hs_chip_command(struct i2c_client* client,
228 enum saa6752hs_command command)
230 unsigned char buf[3];
231 unsigned long timeout;
234 /* execute the command */
236 case SAA6752HS_COMMAND_RESET:
240 case SAA6752HS_COMMAND_STOP:
244 case SAA6752HS_COMMAND_START:
248 case SAA6752HS_COMMAND_PAUSE:
252 case SAA6752HS_COMMAND_RECONFIGURE:
256 case SAA6752HS_COMMAND_SLEEP:
260 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
268 /* set it and wait for it to be so */
269 i2c_master_send(client, buf, 1);
270 timeout = jiffies + HZ * 3;
272 /* get the current status */
274 i2c_master_send(client, buf, 1);
275 i2c_master_recv(client, buf, 1);
277 if (!(buf[0] & 0x20))
279 if (time_after(jiffies,timeout)) {
287 /* delay a bit to let encoder settle */
294 static int saa6752hs_set_bitrate(struct i2c_client* client,
295 struct saa6752hs_state *h)
297 struct saa6752hs_mpeg_params *params = &h->params;
301 /* set the bitrate mode */
303 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
304 i2c_master_send(client, buf, 2);
306 /* set the video bitrate */
307 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
308 /* set the target bitrate */
310 buf[1] = params->vi_bitrate >> 8;
311 buf[2] = params->vi_bitrate & 0xff;
312 i2c_master_send(client, buf, 3);
314 /* set the max bitrate */
316 buf[1] = params->vi_bitrate_peak >> 8;
317 buf[2] = params->vi_bitrate_peak & 0xff;
318 i2c_master_send(client, buf, 3);
319 tot_bitrate = params->vi_bitrate_peak;
321 /* set the target bitrate (no max bitrate for CBR) */
323 buf[1] = params->vi_bitrate >> 8;
324 buf[2] = params->vi_bitrate & 0xff;
325 i2c_master_send(client, buf, 3);
326 tot_bitrate = params->vi_bitrate;
329 /* set the audio encoding */
331 buf[1] = (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
332 i2c_master_send(client, buf, 2);
334 /* set the audio bitrate */
336 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
337 buf[1] = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
339 buf[1] = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
340 tot_bitrate += buf[1] ? 384 : 256;
341 i2c_master_send(client, buf, 2);
343 /* Note: the total max bitrate is determined by adding the video and audio
344 bitrates together and also adding an extra 768kbit/s to stay on the
345 safe side. If more control should be required, then an extra MPEG control
348 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
349 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
351 /* set the total bitrate */
353 buf[1] = tot_bitrate >> 8;
354 buf[2] = tot_bitrate & 0xff;
355 i2c_master_send(client, buf, 3);
360 static void saa6752hs_set_subsampling(struct i2c_client* client,
361 struct v4l2_format* f)
363 struct saa6752hs_state *h = i2c_get_clientdata(client);
364 int dist_352, dist_480, dist_720;
367 FIXME: translate and round width/height into EMPRESS
371 ---------------------------
372 SIF | 352x288 | 352x240
373 1/2 D1 | 352x576 | 352x480
374 2/3 D1 | 480x576 | 480x480
375 D1 | 720x576 | 720x480
378 dist_352 = abs(f->fmt.pix.width - 352);
379 dist_480 = abs(f->fmt.pix.width - 480);
380 dist_720 = abs(f->fmt.pix.width - 720);
381 if (dist_720 < dist_480) {
382 f->fmt.pix.width = 720;
383 f->fmt.pix.height = 576;
384 h->video_format = SAA6752HS_VF_D1;
386 else if (dist_480 < dist_352) {
387 f->fmt.pix.width = 480;
388 f->fmt.pix.height = 576;
389 h->video_format = SAA6752HS_VF_2_3_D1;
392 f->fmt.pix.width = 352;
393 if (abs(f->fmt.pix.height - 576) <
394 abs(f->fmt.pix.height - 288)) {
395 f->fmt.pix.height = 576;
396 h->video_format = SAA6752HS_VF_1_2_D1;
399 f->fmt.pix.height = 288;
400 h->video_format = SAA6752HS_VF_SIF;
406 static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
407 struct v4l2_ext_control *ctrl, unsigned int cmd)
410 int set = (cmd == VIDIOC_S_EXT_CTRLS);
414 case V4L2_CID_MPEG_STREAM_TYPE:
415 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
416 if (set && new != old)
420 case V4L2_CID_MPEG_STREAM_PID_PMT:
421 old = params->ts_pid_pmt;
422 if (set && new > MPEG_PID_MAX)
424 if (new > MPEG_PID_MAX)
426 params->ts_pid_pmt = new;
428 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
429 old = params->ts_pid_audio;
430 if (set && new > MPEG_PID_MAX)
432 if (new > MPEG_PID_MAX)
434 params->ts_pid_audio = new;
436 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
437 old = params->ts_pid_video;
438 if (set && new > MPEG_PID_MAX)
440 if (new > MPEG_PID_MAX)
442 params->ts_pid_video = new;
444 case V4L2_CID_MPEG_STREAM_PID_PCR:
445 old = params->ts_pid_pcr;
446 if (set && new > MPEG_PID_MAX)
448 if (new > MPEG_PID_MAX)
450 params->ts_pid_pcr = new;
452 case V4L2_CID_MPEG_AUDIO_ENCODING:
453 old = params->au_encoding;
454 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
455 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
459 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
460 old = params->au_l2_bitrate;
461 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
462 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
464 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
465 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
467 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
468 params->au_l2_bitrate = new;
470 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
473 old = params->au_ac3_bitrate;
474 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
475 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
477 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
478 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
480 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
481 params->au_ac3_bitrate = new;
483 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
484 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
485 if (set && new != old)
489 case V4L2_CID_MPEG_VIDEO_ENCODING:
490 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
491 if (set && new != old)
495 case V4L2_CID_MPEG_VIDEO_ASPECT:
496 old = params->vi_aspect;
497 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
498 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
500 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
501 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
502 params->vi_aspect = new;
504 case V4L2_CID_MPEG_VIDEO_BITRATE:
505 old = params->vi_bitrate * 1000;
506 new = 1000 * (new / 1000);
507 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
509 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
510 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
511 params->vi_bitrate = new / 1000;
513 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
514 old = params->vi_bitrate_peak * 1000;
515 new = 1000 * (new / 1000);
516 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
518 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
519 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
520 params->vi_bitrate_peak = new / 1000;
522 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
523 old = params->vi_bitrate_mode;
524 params->vi_bitrate_mode = new;
529 if (cmd == VIDIOC_G_EXT_CTRLS)
536 static int saa6752hs_qctrl(struct saa6752hs_state *h,
537 struct v4l2_queryctrl *qctrl)
539 struct saa6752hs_mpeg_params *params = &h->params;
543 case V4L2_CID_MPEG_AUDIO_ENCODING:
544 return v4l2_ctrl_query_fill(qctrl,
545 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
546 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
547 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
548 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
550 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
551 return v4l2_ctrl_query_fill(qctrl,
552 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
553 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
554 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
556 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
559 return v4l2_ctrl_query_fill(qctrl,
560 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
561 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
562 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
564 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
565 return v4l2_ctrl_query_fill(qctrl,
566 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
567 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
568 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
570 case V4L2_CID_MPEG_VIDEO_ENCODING:
571 return v4l2_ctrl_query_fill(qctrl,
572 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
573 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
574 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
576 case V4L2_CID_MPEG_VIDEO_ASPECT:
577 return v4l2_ctrl_query_fill(qctrl,
578 V4L2_MPEG_VIDEO_ASPECT_4x3,
579 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
580 V4L2_MPEG_VIDEO_ASPECT_4x3);
582 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
583 err = v4l2_ctrl_query_fill_std(qctrl);
585 params->vi_bitrate_mode ==
586 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
587 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
590 case V4L2_CID_MPEG_STREAM_TYPE:
591 return v4l2_ctrl_query_fill(qctrl,
592 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
593 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
594 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
596 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
597 case V4L2_CID_MPEG_VIDEO_BITRATE:
598 case V4L2_CID_MPEG_STREAM_PID_PMT:
599 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
600 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
601 case V4L2_CID_MPEG_STREAM_PID_PCR:
602 return v4l2_ctrl_query_fill_std(qctrl);
610 static int saa6752hs_qmenu(struct saa6752hs_state *h,
611 struct v4l2_querymenu *qmenu)
613 static const u32 mpeg_audio_encoding[] = {
614 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
615 V4L2_CTRL_MENU_IDS_END
617 static const u32 mpeg_audio_ac3_encoding[] = {
618 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
619 V4L2_MPEG_AUDIO_ENCODING_AC3,
620 V4L2_CTRL_MENU_IDS_END
622 static u32 mpeg_audio_l2_bitrate[] = {
623 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
624 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
625 V4L2_CTRL_MENU_IDS_END
627 static u32 mpeg_audio_ac3_bitrate[] = {
628 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
629 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
630 V4L2_CTRL_MENU_IDS_END
632 struct v4l2_queryctrl qctrl;
635 qctrl.id = qmenu->id;
636 err = saa6752hs_qctrl(h, &qctrl);
640 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
641 return v4l2_ctrl_query_menu_valid_items(qmenu,
642 mpeg_audio_l2_bitrate);
643 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
646 return v4l2_ctrl_query_menu_valid_items(qmenu,
647 mpeg_audio_ac3_bitrate);
648 case V4L2_CID_MPEG_AUDIO_ENCODING:
649 return v4l2_ctrl_query_menu_valid_items(qmenu,
650 h->has_ac3 ? mpeg_audio_ac3_encoding :
651 mpeg_audio_encoding);
653 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
656 static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
658 unsigned char buf[9], buf2[4];
659 struct saa6752hs_state *h;
662 unsigned char localPAT[256];
663 unsigned char localPMT[256];
665 h = i2c_get_clientdata(client);
667 /* Set video format - must be done first as it resets other settings */
669 buf[1] = h->video_format;
670 i2c_master_send(client, buf, 2);
672 /* Set number of lines in input signal */
675 if (h->standard & V4L2_STD_525_60)
677 i2c_master_send(client, buf, 2);
680 saa6752hs_set_bitrate(client, h);
682 /* Set GOP structure {3, 13} */
686 i2c_master_send(client,buf,3);
688 /* Set minimum Q-scale {4} */
691 i2c_master_send(client,buf,2);
693 /* Set maximum Q-scale {12} */
696 i2c_master_send(client,buf,2);
698 /* Set Output Protocol */
701 i2c_master_send(client,buf,2);
703 /* Set video output stream format {TS} */
706 i2c_master_send(client,buf,2);
708 /* Set leading null byte for TS */
710 buf[1] = (leading_null_bytes >> 8) & 0xff;
711 buf[2] = leading_null_bytes & 0xff;
712 i2c_master_send(client, buf, 3);
715 memcpy(localPAT, PAT, sizeof(PAT));
716 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
717 localPAT[18] = h->params.ts_pid_pmt & 0xff;
718 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
719 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
720 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
721 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
722 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
725 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
726 size = sizeof(PMT_AC3);
727 memcpy(localPMT, PMT_AC3, size);
730 memcpy(localPMT, PMT, size);
732 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
733 localPMT[4] = h->params.ts_pid_pmt & 0xff;
734 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
735 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
736 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
737 localPMT[21] = h->params.ts_pid_video & 0xFF;
738 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
739 localPMT[26] = h->params.ts_pid_audio & 0xFF;
740 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
741 localPMT[size - 4] = (crc >> 24) & 0xFF;
742 localPMT[size - 3] = (crc >> 16) & 0xFF;
743 localPMT[size - 2] = (crc >> 8) & 0xFF;
744 localPMT[size - 1] = crc & 0xFF;
748 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
749 buf[2] = h->params.ts_pid_audio & 0xFF;
750 i2c_master_send(client,buf,3);
754 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
755 buf[2] = h->params.ts_pid_video & 0xFF;
756 i2c_master_send(client,buf,3);
760 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
761 buf[2] = h->params.ts_pid_pcr & 0xFF;
762 i2c_master_send(client,buf,3);
765 i2c_master_send(client, localPAT, sizeof(PAT));
766 i2c_master_send(client, localPMT, size);
768 /* mute then unmute audio. This removes buzzing artefacts */
771 i2c_master_send(client, buf, 2);
773 i2c_master_send(client, buf, 2);
776 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
778 /* readout current state */
784 i2c_master_send(client, buf, 5);
785 i2c_master_recv(client, buf2, 4);
787 /* change aspect ratio */
794 switch(h->params.vi_aspect) {
795 case V4L2_MPEG_VIDEO_ASPECT_16x9:
796 buf[6] = buf2[1] | 0x40;
798 case V4L2_MPEG_VIDEO_ASPECT_4x3:
800 buf[6] = buf2[1] & 0xBF;
806 i2c_master_send(client, buf, 9);
812 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
814 struct saa6752hs_state *h = i2c_get_clientdata(client);
815 struct v4l2_ext_controls *ctrls = arg;
816 struct saa6752hs_mpeg_params params;
821 case VIDIOC_INT_INIT:
822 /* apply settings and start encoder */
823 saa6752hs_init(client, *(u32 *)arg);
825 case VIDIOC_S_EXT_CTRLS:
826 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
829 case VIDIOC_TRY_EXT_CTRLS:
830 case VIDIOC_G_EXT_CTRLS:
831 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
834 for (i = 0; i < ctrls->count; i++) {
835 err = handle_ctrl(h->has_ac3, ¶ms, ctrls->controls + i, cmd);
837 ctrls->error_idx = i;
843 case VIDIOC_QUERYCTRL:
844 return saa6752hs_qctrl(h, arg);
845 case VIDIOC_QUERYMENU:
846 return saa6752hs_qmenu(h, arg);
849 struct v4l2_format *f = arg;
851 if (h->video_format == SAA6752HS_VF_UNKNOWN)
852 h->video_format = SAA6752HS_VF_D1;
854 v4l2_format_table[h->video_format].fmt.pix.width;
856 v4l2_format_table[h->video_format].fmt.pix.height;
861 struct v4l2_format *f = arg;
863 saa6752hs_set_subsampling(client, f);
867 h->standard = *((v4l2_std_id *) arg);
870 case VIDIOC_G_CHIP_IDENT:
871 return v4l2_chip_ident_i2c_client(client,
872 arg, h->chip, h->revision);
882 static int saa6752hs_probe(struct i2c_client *client,
883 const struct i2c_device_id *id)
885 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
889 v4l_info(client, "chip found @ 0x%x (%s)\n",
890 client->addr << 1, client->adapter->name);
894 i2c_master_send(client, &addr, 1);
895 i2c_master_recv(client, data, sizeof(data));
896 h->chip = V4L2_IDENT_SAA6752HS;
897 h->revision = (data[8] << 8) | data[9];
899 if (h->revision == 0x0206) {
900 h->chip = V4L2_IDENT_SAA6752HS_AC3;
902 v4l_info(client, "support AC-3\n");
904 h->params = param_defaults;
905 h->standard = 0; /* Assume 625 input lines */
907 i2c_set_clientdata(client, h);
911 static int saa6752hs_remove(struct i2c_client *client)
913 kfree(i2c_get_clientdata(client));
917 static const struct i2c_device_id saa6752hs_id[] = {
921 MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
923 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
925 .driverid = I2C_DRIVERID_SAA6752HS,
926 .command = saa6752hs_command,
927 .probe = saa6752hs_probe,
928 .remove = saa6752hs_remove,
929 .id_table = saa6752hs_id,
933 * Overrides for Emacs so that we follow Linus's tabbing style.
934 * ---------------------------------------------------------------------------