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>
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};
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 */
41 struct saa6752hs_mpeg_params {
42 /* transport streams */
49 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
52 enum v4l2_mpeg_video_aspect vi_aspect;
53 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
55 __u32 vi_bitrate_peak;
58 static const struct v4l2_format v4l2_format_table[] =
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 }}},
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 saa6752hs_mpeg_params params;
75 enum saa6752hs_videoformat video_format;
79 enum saa6752hs_command {
80 SAA6752HS_COMMAND_RESET = 0,
81 SAA6752HS_COMMAND_STOP = 1,
82 SAA6752HS_COMMAND_START = 2,
83 SAA6752HS_COMMAND_PAUSE = 3,
84 SAA6752HS_COMMAND_RECONFIGURE = 4,
85 SAA6752HS_COMMAND_SLEEP = 5,
86 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
91 /* ---------------------------------------------------------------------- */
94 0xc2, /* i2c register */
95 0x00, /* table number for encoder */
98 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
99 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
101 0x00, /* PSI pointer to start of table */
104 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
106 0x00, 0x01, /* transport_stream_id(1) */
108 0xc1, /* version_number(0), current_next_indicator(1) */
110 0x00, 0x00, /* section_number(0), last_section_number(0) */
112 0x00, 0x01, /* program_number(1) */
114 0xe0, 0x00, /* PMT PID */
116 0x00, 0x00, 0x00, 0x00 /* CRC32 */
120 0xc2, /* i2c register */
121 0x01, /* table number for encoder */
124 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
125 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
127 0x00, /* PSI pointer to start of table */
130 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
132 0x00, 0x01, /* program_number(1) */
134 0xc1, /* version_number(0), current_next_indicator(1) */
136 0x00, 0x00, /* section_number(0), last_section_number(0) */
138 0xe0, 0x00, /* PCR_PID */
140 0xf0, 0x00, /* program_info_length(0) */
142 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
143 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
145 0x00, 0x00, 0x00, 0x00 /* CRC32 */
148 static struct saa6752hs_mpeg_params param_defaults =
155 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
157 .vi_bitrate_peak = 6000,
158 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
160 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
163 /* ---------------------------------------------------------------------- */
165 static int saa6752hs_chip_command(struct i2c_client* client,
166 enum saa6752hs_command command)
168 unsigned char buf[3];
169 unsigned long timeout;
172 /* execute the command */
174 case SAA6752HS_COMMAND_RESET:
178 case SAA6752HS_COMMAND_STOP:
182 case SAA6752HS_COMMAND_START:
186 case SAA6752HS_COMMAND_PAUSE:
190 case SAA6752HS_COMMAND_RECONFIGURE:
194 case SAA6752HS_COMMAND_SLEEP:
198 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
206 /* set it and wait for it to be so */
207 i2c_master_send(client, buf, 1);
208 timeout = jiffies + HZ * 3;
210 /* get the current status */
212 i2c_master_send(client, buf, 1);
213 i2c_master_recv(client, buf, 1);
215 if (!(buf[0] & 0x20))
217 if (time_after(jiffies,timeout)) {
225 /* delay a bit to let encoder settle */
232 static int saa6752hs_set_bitrate(struct i2c_client* client,
233 struct saa6752hs_mpeg_params* params)
238 /* set the bitrate mode */
240 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
241 i2c_master_send(client, buf, 2);
243 /* set the video bitrate */
244 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
245 /* set the target bitrate */
247 buf[1] = params->vi_bitrate >> 8;
248 buf[2] = params->vi_bitrate & 0xff;
249 i2c_master_send(client, buf, 3);
251 /* set the max bitrate */
253 buf[1] = params->vi_bitrate_peak >> 8;
254 buf[2] = params->vi_bitrate_peak & 0xff;
255 i2c_master_send(client, buf, 3);
256 tot_bitrate = params->vi_bitrate_peak;
258 /* set the target bitrate (no max bitrate for CBR) */
260 buf[1] = params->vi_bitrate >> 8;
261 buf[2] = params->vi_bitrate & 0xff;
262 i2c_master_send(client, buf, 3);
263 tot_bitrate = params->vi_bitrate;
266 /* set the audio bitrate */
268 buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1;
269 i2c_master_send(client, buf, 2);
270 tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384;
272 /* Note: the total max bitrate is determined by adding the video and audio
273 bitrates together and also adding an extra 768kbit/s to stay on the
274 safe side. If more control should be required, then an extra MPEG control
277 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
278 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
280 /* set the total bitrate */
282 buf[1] = tot_bitrate >> 8;
283 buf[2] = tot_bitrate & 0xff;
284 i2c_master_send(client, buf, 3);
289 static void saa6752hs_set_subsampling(struct i2c_client* client,
290 struct v4l2_format* f)
292 struct saa6752hs_state *h = i2c_get_clientdata(client);
293 int dist_352, dist_480, dist_720;
296 FIXME: translate and round width/height into EMPRESS
300 ---------------------------
301 SIF | 352x288 | 352x240
302 1/2 D1 | 352x576 | 352x480
303 2/3 D1 | 480x576 | 480x480
304 D1 | 720x576 | 720x480
307 dist_352 = abs(f->fmt.pix.width - 352);
308 dist_480 = abs(f->fmt.pix.width - 480);
309 dist_720 = abs(f->fmt.pix.width - 720);
310 if (dist_720 < dist_480) {
311 f->fmt.pix.width = 720;
312 f->fmt.pix.height = 576;
313 h->video_format = SAA6752HS_VF_D1;
315 else if (dist_480 < dist_352) {
316 f->fmt.pix.width = 480;
317 f->fmt.pix.height = 576;
318 h->video_format = SAA6752HS_VF_2_3_D1;
321 f->fmt.pix.width = 352;
322 if (abs(f->fmt.pix.height - 576) <
323 abs(f->fmt.pix.height - 288)) {
324 f->fmt.pix.height = 576;
325 h->video_format = SAA6752HS_VF_1_2_D1;
328 f->fmt.pix.height = 288;
329 h->video_format = SAA6752HS_VF_SIF;
335 static int handle_ctrl(struct saa6752hs_mpeg_params *params,
336 struct v4l2_ext_control *ctrl, unsigned int cmd)
339 int set = (cmd == VIDIOC_S_EXT_CTRLS);
343 case V4L2_CID_MPEG_STREAM_TYPE:
344 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
345 if (set && new != old)
349 case V4L2_CID_MPEG_STREAM_PID_PMT:
350 old = params->ts_pid_pmt;
351 if (set && new > MPEG_PID_MAX)
353 if (new > MPEG_PID_MAX)
355 params->ts_pid_pmt = new;
357 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
358 old = params->ts_pid_audio;
359 if (set && new > MPEG_PID_MAX)
361 if (new > MPEG_PID_MAX)
363 params->ts_pid_audio = new;
365 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
366 old = params->ts_pid_video;
367 if (set && new > MPEG_PID_MAX)
369 if (new > MPEG_PID_MAX)
371 params->ts_pid_video = new;
373 case V4L2_CID_MPEG_STREAM_PID_PCR:
374 old = params->ts_pid_pcr;
375 if (set && new > MPEG_PID_MAX)
377 if (new > MPEG_PID_MAX)
379 params->ts_pid_pcr = new;
381 case V4L2_CID_MPEG_AUDIO_ENCODING:
382 old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
383 if (set && new != old)
387 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
388 old = params->au_l2_bitrate;
389 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
390 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
392 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
393 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
395 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
396 params->au_l2_bitrate = new;
398 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
399 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
400 if (set && new != old)
404 case V4L2_CID_MPEG_VIDEO_ENCODING:
405 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
406 if (set && new != old)
410 case V4L2_CID_MPEG_VIDEO_ASPECT:
411 old = params->vi_aspect;
412 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
413 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
415 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
416 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
417 params->vi_aspect = new;
419 case V4L2_CID_MPEG_VIDEO_BITRATE:
420 old = params->vi_bitrate * 1000;
421 new = 1000 * (new / 1000);
422 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
424 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
425 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
426 params->vi_bitrate = new / 1000;
428 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
429 old = params->vi_bitrate_peak * 1000;
430 new = 1000 * (new / 1000);
431 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
433 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
434 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
435 params->vi_bitrate_peak = new / 1000;
437 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
438 old = params->vi_bitrate_mode;
439 params->vi_bitrate_mode = new;
444 if (cmd == VIDIOC_G_EXT_CTRLS)
451 static int saa6752hs_init(struct i2c_client* client)
453 unsigned char buf[9], buf2[4];
454 struct saa6752hs_state *h;
456 unsigned char localPAT[256];
457 unsigned char localPMT[256];
459 h = i2c_get_clientdata(client);
461 /* Set video format - must be done first as it resets other settings */
463 buf[1] = h->video_format;
464 i2c_master_send(client, buf, 2);
466 /* Set number of lines in input signal */
469 if (h->standard & V4L2_STD_525_60)
471 i2c_master_send(client, buf, 2);
474 saa6752hs_set_bitrate(client, &h->params);
476 /* Set GOP structure {3, 13} */
480 i2c_master_send(client,buf,3);
482 /* Set minimum Q-scale {4} */
485 i2c_master_send(client,buf,2);
487 /* Set maximum Q-scale {12} */
490 i2c_master_send(client,buf,2);
492 /* Set Output Protocol */
495 i2c_master_send(client,buf,2);
497 /* Set video output stream format {TS} */
500 i2c_master_send(client,buf,2);
503 memcpy(localPAT, PAT, sizeof(PAT));
504 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
505 localPAT[18] = h->params.ts_pid_pmt & 0xff;
506 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
507 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
508 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
509 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
510 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
513 memcpy(localPMT, PMT, sizeof(PMT));
514 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
515 localPMT[4] = h->params.ts_pid_pmt & 0xff;
516 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
517 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
518 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
519 localPMT[21] = h->params.ts_pid_video & 0xFF;
520 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
521 localPMT[26] = h->params.ts_pid_audio & 0xFF;
522 crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
523 localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
524 localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
525 localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
526 localPMT[sizeof(PMT) - 1] = crc & 0xFF;
530 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
531 buf[2] = h->params.ts_pid_audio & 0xFF;
532 i2c_master_send(client,buf,3);
536 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
537 buf[2] = h->params.ts_pid_video & 0xFF;
538 i2c_master_send(client,buf,3);
542 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
543 buf[2] = h->params.ts_pid_pcr & 0xFF;
544 i2c_master_send(client,buf,3);
547 i2c_master_send(client,localPAT,sizeof(PAT));
548 i2c_master_send(client,localPMT,sizeof(PMT));
550 /* mute then unmute audio. This removes buzzing artefacts */
553 i2c_master_send(client, buf, 2);
555 i2c_master_send(client, buf, 2);
558 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
560 /* readout current state */
566 i2c_master_send(client, buf, 5);
567 i2c_master_recv(client, buf2, 4);
569 /* change aspect ratio */
576 switch(h->params.vi_aspect) {
577 case V4L2_MPEG_VIDEO_ASPECT_16x9:
578 buf[6] = buf2[1] | 0x40;
580 case V4L2_MPEG_VIDEO_ASPECT_4x3:
582 buf[6] = buf2[1] & 0xBF;
588 i2c_master_send(client, buf, 9);
593 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
595 struct saa6752hs_state *h;
598 if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
600 h->client = client_template;
601 h->params = param_defaults;
602 h->client.adapter = adap;
603 h->client.addr = addr;
605 /* Assume 625 input lines */
608 i2c_set_clientdata(&h->client, h);
609 i2c_attach_client(&h->client);
611 v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
616 static int saa6752hs_probe(struct i2c_adapter *adap)
618 if (adap->class & I2C_CLASS_TV_ANALOG)
619 return i2c_probe(adap, &addr_data, saa6752hs_attach);
623 static int saa6752hs_detach(struct i2c_client *client)
625 struct saa6752hs_state *h;
627 h = i2c_get_clientdata(client);
628 i2c_detach_client(client);
634 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
636 struct saa6752hs_state *h = i2c_get_clientdata(client);
637 struct v4l2_ext_controls *ctrls = arg;
638 struct saa6752hs_mpeg_params params;
643 case VIDIOC_S_EXT_CTRLS:
644 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
646 if (ctrls->count == 0) {
647 /* apply settings and start encoder */
648 saa6752hs_init(client);
652 case VIDIOC_TRY_EXT_CTRLS:
653 case VIDIOC_G_EXT_CTRLS:
654 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
657 for (i = 0; i < ctrls->count; i++) {
658 if ((err = handle_ctrl(¶ms, ctrls->controls + i, cmd))) {
659 ctrls->error_idx = i;
667 struct v4l2_format *f = arg;
669 if (h->video_format == SAA6752HS_VF_UNKNOWN)
670 h->video_format = SAA6752HS_VF_D1;
672 v4l2_format_table[h->video_format].fmt.pix.width;
674 v4l2_format_table[h->video_format].fmt.pix.height;
679 struct v4l2_format *f = arg;
681 saa6752hs_set_subsampling(client, f);
685 h->standard = *((v4l2_std_id *) arg);
695 /* ----------------------------------------------------------------------- */
697 static struct i2c_driver driver = {
701 .id = I2C_DRIVERID_SAA6752HS,
702 .attach_adapter = saa6752hs_probe,
703 .detach_client = saa6752hs_detach,
704 .command = saa6752hs_command,
707 static struct i2c_client client_template =
713 static int __init saa6752hs_init_module(void)
715 return i2c_add_driver(&driver);
718 static void __exit saa6752hs_cleanup_module(void)
720 i2c_del_driver(&driver);
723 module_init(saa6752hs_init_module);
724 module_exit(saa6752hs_cleanup_module);
727 * Overrides for Emacs so that we follow Linus's tabbing style.
728 * ---------------------------------------------------------------------------