Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / video / saa7134 / saa6752hs.c
blob1eabff6b2456a5744fcaf6b7fe0e42d344fab576
1 /*
2 saa6752hs - i2c-driver for the saa6752hs by Philips
4 Copyright (C) 2004 Andrew de Quincey
6 AC-3 support:
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 */
58 SAA6752HS_VF_UNKNOWN,
61 struct saa6752hs_mpeg_params {
62 /* transport streams */
63 __u16 ts_pid_pmt;
64 __u16 ts_pid_audio;
65 __u16 ts_pid_video;
66 __u16 ts_pid_pcr;
68 /* audio */
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;
73 /* video */
74 enum v4l2_mpeg_video_aspect vi_aspect;
75 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
76 __u32 vi_bitrate;
77 __u32 vi_bitrate_peak;
80 static const struct v4l2_format v4l2_format_table[] =
82 [SAA6752HS_VF_D1] =
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 }}},
88 [SAA6752HS_VF_SIF] =
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;
96 int chip;
97 u32 revision;
98 int has_ac3;
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 /* ---------------------------------------------------------------------- */
123 static u8 PAT[] = {
124 0xc2, /* i2c register */
125 0x00, /* table number for encoder */
127 0x47, /* sync */
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 */
133 0x00, /* tid(0) */
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 */
149 static u8 PMT[] = {
150 0xc2, /* i2c register */
151 0x01, /* table number for encoder */
153 0x47, /* sync */
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 */
159 0x02, /* tid(2) */
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) */
181 0x47, /* sync */
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 */
189 0x02, /* TID (2) */
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 */
204 0x6a, /* AC3 */
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 =
213 .ts_pid_pmt = 16,
214 .ts_pid_video = 260,
215 .ts_pid_audio = 256,
216 .ts_pid_pcr = 259,
218 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
219 .vi_bitrate = 4000,
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;
235 int status = 0;
237 /* execute the command */
238 switch(command) {
239 case SAA6752HS_COMMAND_RESET:
240 buf[0] = 0x00;
241 break;
243 case SAA6752HS_COMMAND_STOP:
244 buf[0] = 0x03;
245 break;
247 case SAA6752HS_COMMAND_START:
248 buf[0] = 0x02;
249 break;
251 case SAA6752HS_COMMAND_PAUSE:
252 buf[0] = 0x04;
253 break;
255 case SAA6752HS_COMMAND_RECONFIGURE:
256 buf[0] = 0x05;
257 break;
259 case SAA6752HS_COMMAND_SLEEP:
260 buf[0] = 0x06;
261 break;
263 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
264 buf[0] = 0x07;
265 break;
267 default:
268 return -EINVAL;
271 /* set it and wait for it to be so */
272 i2c_master_send(client, buf, 1);
273 timeout = jiffies + HZ * 3;
274 for (;;) {
275 /* get the current status */
276 buf[0] = 0x10;
277 i2c_master_send(client, buf, 1);
278 i2c_master_recv(client, buf, 1);
280 if (!(buf[0] & 0x20))
281 break;
282 if (time_after(jiffies,timeout)) {
283 status = -ETIMEDOUT;
284 break;
287 msleep(10);
290 /* delay a bit to let encoder settle */
291 msleep(50);
293 return status;
297 static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
299 u8 buf[2];
301 buf[0] = reg;
302 buf[1] = val;
303 i2c_master_send(client, buf, 2);
306 static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
308 u8 buf[3];
310 buf[0] = reg;
311 buf[1] = val >> 8;
312 buf[2] = val & 0xff;
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;
320 int tot_bitrate;
321 int is_384k;
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;
335 } else {
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;
348 else
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
356 should be added. */
357 tot_bitrate += 768;
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);
363 return 0;
367 static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
368 struct v4l2_ext_control *ctrl)
370 switch (ctrl->id) {
371 case V4L2_CID_MPEG_STREAM_TYPE:
372 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
373 break;
374 case V4L2_CID_MPEG_STREAM_PID_PMT:
375 ctrl->value = params->ts_pid_pmt;
376 break;
377 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
378 ctrl->value = params->ts_pid_audio;
379 break;
380 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
381 ctrl->value = params->ts_pid_video;
382 break;
383 case V4L2_CID_MPEG_STREAM_PID_PCR:
384 ctrl->value = params->ts_pid_pcr;
385 break;
386 case V4L2_CID_MPEG_AUDIO_ENCODING:
387 ctrl->value = params->au_encoding;
388 break;
389 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
390 ctrl->value = params->au_l2_bitrate;
391 break;
392 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
393 if (!has_ac3)
394 return -EINVAL;
395 ctrl->value = params->au_ac3_bitrate;
396 break;
397 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
398 ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
399 break;
400 case V4L2_CID_MPEG_VIDEO_ENCODING:
401 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
402 break;
403 case V4L2_CID_MPEG_VIDEO_ASPECT:
404 ctrl->value = params->vi_aspect;
405 break;
406 case V4L2_CID_MPEG_VIDEO_BITRATE:
407 ctrl->value = params->vi_bitrate * 1000;
408 break;
409 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
410 ctrl->value = params->vi_bitrate_peak * 1000;
411 break;
412 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
413 ctrl->value = params->vi_bitrate_mode;
414 break;
415 default:
416 return -EINVAL;
418 return 0;
421 static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
422 struct v4l2_ext_control *ctrl, int set)
424 int old = 0, new;
426 new = ctrl->value;
427 switch (ctrl->id) {
428 case V4L2_CID_MPEG_STREAM_TYPE:
429 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
430 if (set && new != old)
431 return -ERANGE;
432 new = old;
433 break;
434 case V4L2_CID_MPEG_STREAM_PID_PMT:
435 old = params->ts_pid_pmt;
436 if (set && new > MPEG_PID_MAX)
437 return -ERANGE;
438 if (new > MPEG_PID_MAX)
439 new = MPEG_PID_MAX;
440 params->ts_pid_pmt = new;
441 break;
442 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
443 old = params->ts_pid_audio;
444 if (set && new > MPEG_PID_MAX)
445 return -ERANGE;
446 if (new > MPEG_PID_MAX)
447 new = MPEG_PID_MAX;
448 params->ts_pid_audio = new;
449 break;
450 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
451 old = params->ts_pid_video;
452 if (set && new > MPEG_PID_MAX)
453 return -ERANGE;
454 if (new > MPEG_PID_MAX)
455 new = MPEG_PID_MAX;
456 params->ts_pid_video = new;
457 break;
458 case V4L2_CID_MPEG_STREAM_PID_PCR:
459 old = params->ts_pid_pcr;
460 if (set && new > MPEG_PID_MAX)
461 return -ERANGE;
462 if (new > MPEG_PID_MAX)
463 new = MPEG_PID_MAX;
464 params->ts_pid_pcr = new;
465 break;
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))
470 return -ERANGE;
471 params->au_encoding = new;
472 break;
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)
477 return -ERANGE;
478 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
479 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
480 else
481 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
482 params->au_l2_bitrate = new;
483 break;
484 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
485 if (!has_ac3)
486 return -EINVAL;
487 old = params->au_ac3_bitrate;
488 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
489 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
490 return -ERANGE;
491 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
492 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
493 else
494 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
495 params->au_ac3_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;
543 ctrl->value = new;
544 return 0;
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;
552 int err;
554 switch (qctrl->id) {
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:
569 if (!h->has_ac3)
570 return -EINVAL;
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);
596 if (err == 0 &&
597 params->vi_bitrate_mode ==
598 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
599 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
600 return err;
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);
624 default:
625 break;
627 return -EINVAL;
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;
653 int err;
655 qctrl.id = qmenu->id;
656 err = saa6752hs_queryctrl(sd, &qctrl);
657 if (err)
658 return err;
659 switch (qmenu->id) {
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:
664 if (!h->has_ac3)
665 return -EINVAL;
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);
681 unsigned size;
682 u32 crc;
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);
692 /* set bitrate */
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);
713 /* compute PAT */
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;
723 /* compute PMT */
724 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
725 size = sizeof(PMT_AC3);
726 memcpy(localPMT, PMT_AC3, size);
727 } else {
728 size = sizeof(PMT);
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;
745 /* Set Audio PID */
746 set_reg16(client, 0xc1, h->params.ts_pid_audio);
748 /* Set Video PID */
749 set_reg16(client, 0xc0, h->params.ts_pid_video);
751 /* Set PCR PID */
752 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
754 /* Send SI tables */
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);
762 /* start it going */
763 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
765 /* readout current state */
766 buf[0] = 0xE1;
767 buf[1] = 0xA7;
768 buf[2] = 0xFE;
769 buf[3] = 0x82;
770 buf[4] = 0xB0;
771 i2c_master_send(client, buf, 5);
772 i2c_master_recv(client, buf2, 4);
774 /* change aspect ratio */
775 buf[0] = 0xE0;
776 buf[1] = 0xA7;
777 buf[2] = 0xFE;
778 buf[3] = 0x82;
779 buf[4] = 0xB0;
780 buf[5] = buf2[0];
781 switch (h->params.vi_aspect) {
782 case V4L2_MPEG_VIDEO_ASPECT_16x9:
783 buf[6] = buf2[1] | 0x40;
784 break;
785 case V4L2_MPEG_VIDEO_ASPECT_4x3:
786 default:
787 buf[6] = buf2[1] & 0xBF;
788 break;
790 buf[7] = buf2[2];
791 buf[8] = buf2[3];
792 i2c_master_send(client, buf, 9);
794 return 0;
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;
801 int i;
803 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
804 return -EINVAL;
806 params = h->params;
807 for (i = 0; i < ctrls->count; i++) {
808 int err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, set);
810 if (err) {
811 ctrls->error_idx = i;
812 return err;
815 if (set)
816 h->params = params;
817 return 0;
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);
833 int i;
835 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
836 return -EINVAL;
838 for (i = 0; i < ctrls->count; i++) {
839 int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i);
841 if (err) {
842 ctrls->error_idx = i;
843 return err;
846 return 0;
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;
855 f->fmt.pix.width =
856 v4l2_format_table[h->video_format].fmt.pix.width;
857 f->fmt.pix.height =
858 v4l2_format_table[h->video_format].fmt.pix.height;
859 return 0;
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
869 subsample type:
871 type | PAL | NTSC
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;
890 } else {
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;
896 } else {
897 f->fmt.pix.height = 288;
898 h->video_format = SAA6752HS_VF_SIF;
901 return 0;
904 static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
906 struct saa6752hs_state *h = to_state(sd);
908 h->standard = std;
909 return 0;
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,
931 .s_std = saa6752hs_s_std,
934 static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
935 .s_fmt = saa6752hs_s_fmt,
936 .g_fmt = saa6752hs_g_fmt,
939 static const struct v4l2_subdev_ops saa6752hs_ops = {
940 .core = &saa6752hs_core_ops,
941 .video = &saa6752hs_video_ops,
944 static int saa6752hs_probe(struct i2c_client *client,
945 const struct i2c_device_id *id)
947 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
948 struct v4l2_subdev *sd;
949 u8 addr = 0x13;
950 u8 data[12];
952 v4l_info(client, "chip found @ 0x%x (%s)\n",
953 client->addr << 1, client->adapter->name);
954 if (h == NULL)
955 return -ENOMEM;
956 sd = &h->sd;
957 v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
959 i2c_master_send(client, &addr, 1);
960 i2c_master_recv(client, data, sizeof(data));
961 h->chip = V4L2_IDENT_SAA6752HS;
962 h->revision = (data[8] << 8) | data[9];
963 h->has_ac3 = 0;
964 if (h->revision == 0x0206) {
965 h->chip = V4L2_IDENT_SAA6752HS_AC3;
966 h->has_ac3 = 1;
967 v4l_info(client, "support AC-3\n");
969 h->params = param_defaults;
970 h->standard = 0; /* Assume 625 input lines */
971 return 0;
974 static int saa6752hs_remove(struct i2c_client *client)
976 struct v4l2_subdev *sd = i2c_get_clientdata(client);
978 v4l2_device_unregister_subdev(sd);
979 kfree(to_state(sd));
980 return 0;
983 static const struct i2c_device_id saa6752hs_id[] = {
984 { "saa6752hs", 0 },
987 MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
989 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
990 .name = "saa6752hs",
991 .probe = saa6752hs_probe,
992 .remove = saa6752hs_remove,
993 .id_table = saa6752hs_id,
997 * Overrides for Emacs so that we follow Linus's tabbing style.
998 * ---------------------------------------------------------------------------
999 * Local variables:
1000 * c-basic-offset: 8
1001 * End: