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-device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/v4l2-i2c-drv.h>
40 #include <linux/init.h>
41 #include <linux/crc32.h>
43 #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
44 #define MPEG_VIDEO_MAX_BITRATE_MAX 27000
45 #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
46 #define MPEG_PID_MAX ((1 << 14) - 1)
49 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
50 MODULE_AUTHOR("Andrew de Quincey");
51 MODULE_LICENSE("GPL");
53 enum saa6752hs_videoformat {
54 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
55 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
56 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
57 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
61 struct saa6752hs_mpeg_params {
62 /* transport streams */
69 enum v4l2_mpeg_audio_encoding au_encoding;
70 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
71 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
74 enum v4l2_mpeg_video_aspect vi_aspect;
75 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
77 __u32 vi_bitrate_peak;
80 static const struct v4l2_format v4l2_format_table[] =
83 { .fmt = { .pix = { .width = 720, .height = 576 }}},
84 [SAA6752HS_VF_2_3_D1] =
85 { .fmt = { .pix = { .width = 480, .height = 576 }}},
86 [SAA6752HS_VF_1_2_D1] =
87 { .fmt = { .pix = { .width = 352, .height = 576 }}},
89 { .fmt = { .pix = { .width = 352, .height = 288 }}},
90 [SAA6752HS_VF_UNKNOWN] =
91 { .fmt = { .pix = { .width = 0, .height = 0}}},
94 struct saa6752hs_state {
95 struct v4l2_subdev sd;
99 struct saa6752hs_mpeg_params params;
100 enum saa6752hs_videoformat video_format;
101 v4l2_std_id standard;
104 enum saa6752hs_command {
105 SAA6752HS_COMMAND_RESET = 0,
106 SAA6752HS_COMMAND_STOP = 1,
107 SAA6752HS_COMMAND_START = 2,
108 SAA6752HS_COMMAND_PAUSE = 3,
109 SAA6752HS_COMMAND_RECONFIGURE = 4,
110 SAA6752HS_COMMAND_SLEEP = 5,
111 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
113 SAA6752HS_COMMAND_MAX
116 static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
118 return container_of(sd, struct saa6752hs_state, sd);
121 /* ---------------------------------------------------------------------- */
124 0xc2, /* i2c register */
125 0x00, /* table number for encoder */
128 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
129 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
131 0x00, /* PSI pointer to start of table */
134 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
136 0x00, 0x01, /* transport_stream_id(1) */
138 0xc1, /* version_number(0), current_next_indicator(1) */
140 0x00, 0x00, /* section_number(0), last_section_number(0) */
142 0x00, 0x01, /* program_number(1) */
144 0xe0, 0x00, /* PMT PID */
146 0x00, 0x00, 0x00, 0x00 /* CRC32 */
150 0xc2, /* i2c register */
151 0x01, /* table number for encoder */
154 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
155 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
157 0x00, /* PSI pointer to start of table */
160 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
162 0x00, 0x01, /* program_number(1) */
164 0xc1, /* version_number(0), current_next_indicator(1) */
166 0x00, 0x00, /* section_number(0), last_section_number(0) */
168 0xe0, 0x00, /* PCR_PID */
170 0xf0, 0x00, /* program_info_length(0) */
172 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
173 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
175 0x00, 0x00, 0x00, 0x00 /* CRC32 */
178 static u8 PMT_AC3[] = {
179 0xc2, /* i2c register */
180 0x01, /* table number for encoder(1) */
183 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
184 0x10, /* PMT PID (0x0010) */
185 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
187 0x00, /* PSI pointer to start of table */
190 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
192 0x00, 0x01, /* program_number(1) */
194 0xc1, /* version_number(0), current_next_indicator(1) */
196 0x00, 0x00, /* section_number(0), last_section_number(0) */
198 0xe1, 0x04, /* PCR_PID (0x0104) */
200 0xf0, 0x00, /* program_info_length(0) */
202 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
203 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
205 0x01, /* Descriptor_length(1) */
206 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
208 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
211 static struct saa6752hs_mpeg_params param_defaults =
218 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
220 .vi_bitrate_peak = 6000,
221 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
223 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
224 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
225 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
228 /* ---------------------------------------------------------------------- */
230 static int saa6752hs_chip_command(struct i2c_client *client,
231 enum saa6752hs_command command)
233 unsigned char buf[3];
234 unsigned long timeout;
237 /* execute the command */
239 case SAA6752HS_COMMAND_RESET:
243 case SAA6752HS_COMMAND_STOP:
247 case SAA6752HS_COMMAND_START:
251 case SAA6752HS_COMMAND_PAUSE:
255 case SAA6752HS_COMMAND_RECONFIGURE:
259 case SAA6752HS_COMMAND_SLEEP:
263 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
271 /* set it and wait for it to be so */
272 i2c_master_send(client, buf, 1);
273 timeout = jiffies + HZ * 3;
275 /* get the current status */
277 i2c_master_send(client, buf, 1);
278 i2c_master_recv(client, buf, 1);
280 if (!(buf[0] & 0x20))
282 if (time_after(jiffies,timeout)) {
290 /* delay a bit to let encoder settle */
297 static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
303 i2c_master_send(client, buf, 2);
306 static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
313 i2c_master_send(client, buf, 3);
316 static int saa6752hs_set_bitrate(struct i2c_client *client,
317 struct saa6752hs_state *h)
319 struct saa6752hs_mpeg_params *params = &h->params;
323 /* set the bitrate mode */
324 set_reg8(client, 0x71,
325 params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
327 /* set the video bitrate */
328 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
329 /* set the target bitrate */
330 set_reg16(client, 0x80, params->vi_bitrate);
332 /* set the max bitrate */
333 set_reg16(client, 0x81, params->vi_bitrate_peak);
334 tot_bitrate = params->vi_bitrate_peak;
336 /* set the target bitrate (no max bitrate for CBR) */
337 set_reg16(client, 0x81, params->vi_bitrate);
338 tot_bitrate = params->vi_bitrate;
341 /* set the audio encoding */
342 set_reg8(client, 0x93,
343 params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
345 /* set the audio bitrate */
346 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
347 is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
349 is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
350 set_reg8(client, 0x94, is_384k);
351 tot_bitrate += is_384k ? 384 : 256;
353 /* Note: the total max bitrate is determined by adding the video and audio
354 bitrates together and also adding an extra 768kbit/s to stay on the
355 safe side. If more control should be required, then an extra MPEG control
358 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
359 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
361 /* set the total bitrate */
362 set_reg16(client, 0xb1, tot_bitrate);
367 static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
368 struct v4l2_ext_control *ctrl)
371 case V4L2_CID_MPEG_STREAM_TYPE:
372 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
374 case V4L2_CID_MPEG_STREAM_PID_PMT:
375 ctrl->value = params->ts_pid_pmt;
377 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
378 ctrl->value = params->ts_pid_audio;
380 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
381 ctrl->value = params->ts_pid_video;
383 case V4L2_CID_MPEG_STREAM_PID_PCR:
384 ctrl->value = params->ts_pid_pcr;
386 case V4L2_CID_MPEG_AUDIO_ENCODING:
387 ctrl->value = params->au_encoding;
389 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
390 ctrl->value = params->au_l2_bitrate;
392 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
395 ctrl->value = params->au_ac3_bitrate;
397 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
398 ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
400 case V4L2_CID_MPEG_VIDEO_ENCODING:
401 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
403 case V4L2_CID_MPEG_VIDEO_ASPECT:
404 ctrl->value = params->vi_aspect;
406 case V4L2_CID_MPEG_VIDEO_BITRATE:
407 ctrl->value = params->vi_bitrate * 1000;
409 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
410 ctrl->value = params->vi_bitrate_peak * 1000;
412 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
413 ctrl->value = params->vi_bitrate_mode;
421 static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
422 struct v4l2_ext_control *ctrl, int set)
428 case V4L2_CID_MPEG_STREAM_TYPE:
429 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
430 if (set && new != old)
434 case V4L2_CID_MPEG_STREAM_PID_PMT:
435 old = params->ts_pid_pmt;
436 if (set && new > MPEG_PID_MAX)
438 if (new > MPEG_PID_MAX)
440 params->ts_pid_pmt = new;
442 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
443 old = params->ts_pid_audio;
444 if (set && new > MPEG_PID_MAX)
446 if (new > MPEG_PID_MAX)
448 params->ts_pid_audio = new;
450 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
451 old = params->ts_pid_video;
452 if (set && new > MPEG_PID_MAX)
454 if (new > MPEG_PID_MAX)
456 params->ts_pid_video = new;
458 case V4L2_CID_MPEG_STREAM_PID_PCR:
459 old = params->ts_pid_pcr;
460 if (set && new > MPEG_PID_MAX)
462 if (new > MPEG_PID_MAX)
464 params->ts_pid_pcr = new;
466 case V4L2_CID_MPEG_AUDIO_ENCODING:
467 old = params->au_encoding;
468 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
469 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
473 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
474 old = params->au_l2_bitrate;
475 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
476 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
478 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
479 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
481 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
482 params->au_l2_bitrate = new;
484 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
487 old = params->au_ac3_bitrate;
488 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
489 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
491 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
492 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
494 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
495 params->au_ac3_bitrate = new;
497 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
498 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
499 if (set && new != old)
503 case V4L2_CID_MPEG_VIDEO_ENCODING:
504 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
505 if (set && new != old)
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)
514 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
515 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
516 params->vi_aspect = new;
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)
523 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
524 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
525 params->vi_bitrate = new / 1000;
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)
532 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
533 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
534 params->vi_bitrate_peak = new / 1000;
536 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
537 old = params->vi_bitrate_mode;
538 params->vi_bitrate_mode = new;
548 static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
550 struct saa6752hs_state *h = to_state(sd);
551 struct saa6752hs_mpeg_params *params = &h->params;
555 case V4L2_CID_MPEG_AUDIO_ENCODING:
556 return v4l2_ctrl_query_fill(qctrl,
557 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
558 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
559 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
560 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
562 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
563 return v4l2_ctrl_query_fill(qctrl,
564 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
565 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
566 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
568 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
571 return v4l2_ctrl_query_fill(qctrl,
572 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
573 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
574 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
576 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
577 return v4l2_ctrl_query_fill(qctrl,
578 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
579 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
580 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
582 case V4L2_CID_MPEG_VIDEO_ENCODING:
583 return v4l2_ctrl_query_fill(qctrl,
584 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
585 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
586 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
588 case V4L2_CID_MPEG_VIDEO_ASPECT:
589 return v4l2_ctrl_query_fill(qctrl,
590 V4L2_MPEG_VIDEO_ASPECT_4x3,
591 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
592 V4L2_MPEG_VIDEO_ASPECT_4x3);
594 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
595 err = v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
597 params->vi_bitrate_mode ==
598 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
599 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
602 case V4L2_CID_MPEG_STREAM_TYPE:
603 return v4l2_ctrl_query_fill(qctrl,
604 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
605 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
606 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
608 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
609 return v4l2_ctrl_query_fill(qctrl,
610 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
611 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
612 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
613 case V4L2_CID_MPEG_VIDEO_BITRATE:
614 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
615 case V4L2_CID_MPEG_STREAM_PID_PMT:
616 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
617 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
618 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
619 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
620 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
621 case V4L2_CID_MPEG_STREAM_PID_PCR:
622 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
630 static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu)
632 static const u32 mpeg_audio_encoding[] = {
633 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
634 V4L2_CTRL_MENU_IDS_END
636 static const u32 mpeg_audio_ac3_encoding[] = {
637 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
638 V4L2_MPEG_AUDIO_ENCODING_AC3,
639 V4L2_CTRL_MENU_IDS_END
641 static u32 mpeg_audio_l2_bitrate[] = {
642 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
643 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
644 V4L2_CTRL_MENU_IDS_END
646 static u32 mpeg_audio_ac3_bitrate[] = {
647 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
648 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
649 V4L2_CTRL_MENU_IDS_END
651 struct saa6752hs_state *h = to_state(sd);
652 struct v4l2_queryctrl qctrl;
655 qctrl.id = qmenu->id;
656 err = saa6752hs_queryctrl(sd, &qctrl);
660 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
661 return v4l2_ctrl_query_menu_valid_items(qmenu,
662 mpeg_audio_l2_bitrate);
663 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
666 return v4l2_ctrl_query_menu_valid_items(qmenu,
667 mpeg_audio_ac3_bitrate);
668 case V4L2_CID_MPEG_AUDIO_ENCODING:
669 return v4l2_ctrl_query_menu_valid_items(qmenu,
670 h->has_ac3 ? mpeg_audio_ac3_encoding :
671 mpeg_audio_encoding);
673 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
676 static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
678 unsigned char buf[9], buf2[4];
679 struct saa6752hs_state *h = to_state(sd);
680 struct i2c_client *client = v4l2_get_subdevdata(sd);
683 unsigned char localPAT[256];
684 unsigned char localPMT[256];
686 /* Set video format - must be done first as it resets other settings */
687 set_reg8(client, 0x41, h->video_format);
689 /* Set number of lines in input signal */
690 set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
693 saa6752hs_set_bitrate(client, h);
695 /* Set GOP structure {3, 13} */
696 set_reg16(client, 0x72, 0x030d);
698 /* Set minimum Q-scale {4} */
699 set_reg8(client, 0x82, 0x04);
701 /* Set maximum Q-scale {12} */
702 set_reg8(client, 0x83, 0x0c);
704 /* Set Output Protocol */
705 set_reg8(client, 0xd0, 0x81);
707 /* Set video output stream format {TS} */
708 set_reg8(client, 0xb0, 0x05);
710 /* Set leading null byte for TS */
711 set_reg16(client, 0xf6, leading_null_bytes);
714 memcpy(localPAT, PAT, sizeof(PAT));
715 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
716 localPAT[18] = h->params.ts_pid_pmt & 0xff;
717 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
718 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
719 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
720 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
721 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
724 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
725 size = sizeof(PMT_AC3);
726 memcpy(localPMT, PMT_AC3, size);
729 memcpy(localPMT, PMT, size);
731 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
732 localPMT[4] = h->params.ts_pid_pmt & 0xff;
733 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
734 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
735 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
736 localPMT[21] = h->params.ts_pid_video & 0xFF;
737 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
738 localPMT[26] = h->params.ts_pid_audio & 0xFF;
739 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
740 localPMT[size - 4] = (crc >> 24) & 0xFF;
741 localPMT[size - 3] = (crc >> 16) & 0xFF;
742 localPMT[size - 2] = (crc >> 8) & 0xFF;
743 localPMT[size - 1] = crc & 0xFF;
746 set_reg16(client, 0xc1, h->params.ts_pid_audio);
749 set_reg16(client, 0xc0, h->params.ts_pid_video);
752 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
755 i2c_master_send(client, localPAT, sizeof(PAT));
756 i2c_master_send(client, localPMT, size);
758 /* mute then unmute audio. This removes buzzing artefacts */
759 set_reg8(client, 0xa4, 1);
760 set_reg8(client, 0xa4, 0);
763 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
765 /* readout current state */
771 i2c_master_send(client, buf, 5);
772 i2c_master_recv(client, buf2, 4);
774 /* change aspect ratio */
781 switch (h->params.vi_aspect) {
782 case V4L2_MPEG_VIDEO_ASPECT_16x9:
783 buf[6] = buf2[1] | 0x40;
785 case V4L2_MPEG_VIDEO_ASPECT_4x3:
787 buf[6] = buf2[1] & 0xBF;
792 i2c_master_send(client, buf, 9);
797 static int saa6752hs_do_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls, int set)
799 struct saa6752hs_state *h = to_state(sd);
800 struct saa6752hs_mpeg_params params;
803 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
807 for (i = 0; i < ctrls->count; i++) {
808 int err = handle_ctrl(h->has_ac3, ¶ms, ctrls->controls + i, set);
811 ctrls->error_idx = i;
820 static int saa6752hs_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
822 return saa6752hs_do_ext_ctrls(sd, ctrls, 1);
825 static int saa6752hs_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
827 return saa6752hs_do_ext_ctrls(sd, ctrls, 0);
830 static int saa6752hs_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
832 struct saa6752hs_state *h = to_state(sd);
835 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
838 for (i = 0; i < ctrls->count; i++) {
839 int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i);
842 ctrls->error_idx = i;
849 static int saa6752hs_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
851 struct saa6752hs_state *h = to_state(sd);
853 if (h->video_format == SAA6752HS_VF_UNKNOWN)
854 h->video_format = SAA6752HS_VF_D1;
856 v4l2_format_table[h->video_format].fmt.pix.width;
858 v4l2_format_table[h->video_format].fmt.pix.height;
862 static int saa6752hs_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
864 struct saa6752hs_state *h = to_state(sd);
865 int dist_352, dist_480, dist_720;
868 FIXME: translate and round width/height into EMPRESS
872 ---------------------------
873 SIF | 352x288 | 352x240
874 1/2 D1 | 352x576 | 352x480
875 2/3 D1 | 480x576 | 480x480
876 D1 | 720x576 | 720x480
879 dist_352 = abs(f->fmt.pix.width - 352);
880 dist_480 = abs(f->fmt.pix.width - 480);
881 dist_720 = abs(f->fmt.pix.width - 720);
882 if (dist_720 < dist_480) {
883 f->fmt.pix.width = 720;
884 f->fmt.pix.height = 576;
885 h->video_format = SAA6752HS_VF_D1;
886 } else if (dist_480 < dist_352) {
887 f->fmt.pix.width = 480;
888 f->fmt.pix.height = 576;
889 h->video_format = SAA6752HS_VF_2_3_D1;
891 f->fmt.pix.width = 352;
892 if (abs(f->fmt.pix.height - 576) <
893 abs(f->fmt.pix.height - 288)) {
894 f->fmt.pix.height = 576;
895 h->video_format = SAA6752HS_VF_1_2_D1;
897 f->fmt.pix.height = 288;
898 h->video_format = SAA6752HS_VF_SIF;
904 static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
906 struct saa6752hs_state *h = to_state(sd);
912 static int saa6752hs_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
914 struct i2c_client *client = v4l2_get_subdevdata(sd);
915 struct saa6752hs_state *h = to_state(sd);
917 return v4l2_chip_ident_i2c_client(client,
918 chip, h->chip, h->revision);
921 /* ----------------------------------------------------------------------- */
923 static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
924 .g_chip_ident = saa6752hs_g_chip_ident,
925 .init = saa6752hs_init,
926 .queryctrl = saa6752hs_queryctrl,
927 .querymenu = saa6752hs_querymenu,
928 .g_ext_ctrls = saa6752hs_g_ext_ctrls,
929 .s_ext_ctrls = saa6752hs_s_ext_ctrls,
930 .try_ext_ctrls = saa6752hs_try_ext_ctrls,
933 static const struct v4l2_subdev_tuner_ops saa6752hs_tuner_ops = {
934 .s_std = saa6752hs_s_std,
937 static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
938 .s_fmt = saa6752hs_s_fmt,
939 .g_fmt = saa6752hs_g_fmt,
942 static const struct v4l2_subdev_ops saa6752hs_ops = {
943 .core = &saa6752hs_core_ops,
944 .tuner = &saa6752hs_tuner_ops,
945 .video = &saa6752hs_video_ops,
948 static int saa6752hs_probe(struct i2c_client *client,
949 const struct i2c_device_id *id)
951 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
952 struct v4l2_subdev *sd;
956 v4l_info(client, "chip found @ 0x%x (%s)\n",
957 client->addr << 1, client->adapter->name);
961 v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
963 i2c_master_send(client, &addr, 1);
964 i2c_master_recv(client, data, sizeof(data));
965 h->chip = V4L2_IDENT_SAA6752HS;
966 h->revision = (data[8] << 8) | data[9];
968 if (h->revision == 0x0206) {
969 h->chip = V4L2_IDENT_SAA6752HS_AC3;
971 v4l_info(client, "support AC-3\n");
973 h->params = param_defaults;
974 h->standard = 0; /* Assume 625 input lines */
978 static int saa6752hs_remove(struct i2c_client *client)
980 struct v4l2_subdev *sd = i2c_get_clientdata(client);
982 v4l2_device_unregister_subdev(sd);
987 static const struct i2c_device_id saa6752hs_id[] = {
991 MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
993 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
995 .probe = saa6752hs_probe,
996 .remove = saa6752hs_remove,
997 .id_table = saa6752hs_id,
1001 * Overrides for Emacs so that we follow Linus's tabbing style.
1002 * ---------------------------------------------------------------------------