1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/sched.h>
4 #include <linux/string.h>
5 #include <linux/timer.h>
6 #include <linux/delay.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/poll.h>
10 #include <linux/i2c.h>
11 #include <linux/types.h>
12 #include <linux/videodev.h>
13 #include <linux/init.h>
14 #include <linux/crc32.h>
18 #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
19 #define MPEG_VIDEO_MAX_BITRATE_MAX 27000
20 #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
21 #define MPEG_PID_MAX ((1 << 14) - 1)
23 /* Addresses to scan */
24 static unsigned short normal_i2c
[] = {0x20, I2C_CLIENT_END
};
25 static unsigned short normal_i2c_range
[] = {I2C_CLIENT_END
};
28 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
29 MODULE_AUTHOR("Andrew de Quincey");
30 MODULE_LICENSE("GPL");
32 static struct i2c_driver driver
;
33 static struct i2c_client client_template
;
35 enum saa6752hs_videoformat
{
36 SAA6752HS_VF_D1
= 0, /* standard D1 video format: 720x576 */
37 SAA6752HS_VF_2_3_D1
= 1,/* 2/3D1 video format: 480x576 */
38 SAA6752HS_VF_1_2_D1
= 2,/* 1/2D1 video format: 352x576 */
39 SAA6752HS_VF_SIF
= 3, /* SIF video format: 352x288 */
43 static const struct v4l2_format v4l2_format_table
[] =
46 .fmt
.pix
.width
= 720, .fmt
.pix
.height
= 576 },
47 [SAA6752HS_VF_2_3_D1
] = {
48 .fmt
.pix
.width
= 480, .fmt
.pix
.height
= 576 },
49 [SAA6752HS_VF_1_2_D1
] = {
50 .fmt
.pix
.width
= 352, .fmt
.pix
.height
= 576 },
51 [SAA6752HS_VF_SIF
] = {
52 .fmt
.pix
.width
= 352, .fmt
.pix
.height
= 288 },
53 [SAA6752HS_VF_UNKNOWN
] = {
54 .fmt
.pix
.width
= 0, .fmt
.pix
.height
= 0},
57 struct saa6752hs_state
{
58 struct i2c_client client
;
59 struct v4l2_mpeg_compression params
;
60 enum saa6752hs_videoformat video_format
;
63 enum saa6752hs_command
{
64 SAA6752HS_COMMAND_RESET
= 0,
65 SAA6752HS_COMMAND_STOP
= 1,
66 SAA6752HS_COMMAND_START
= 2,
67 SAA6752HS_COMMAND_PAUSE
= 3,
68 SAA6752HS_COMMAND_RECONFIGURE
= 4,
69 SAA6752HS_COMMAND_SLEEP
= 5,
70 SAA6752HS_COMMAND_RECONFIGURE_FORCE
= 6,
75 /* ---------------------------------------------------------------------- */
79 0x00, // table number for encoder
82 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0)
83 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
85 0x00, // PSI pointer to start of table
88 0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
90 0x00, 0x01, // transport_stream_id(1)
92 0xc1, // version_number(0), current_next_indicator(1)
94 0x00, 0x00, // section_number(0), last_section_number(0)
96 0x00, 0x01, // program_number(1)
98 0xe0, 0x00, // PMT PID
100 0x00, 0x00, 0x00, 0x00 // CRC32
104 0xc2, // i2c register
105 0x01, // table number for encoder
108 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid
109 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
111 0x00, // PSI pointer to start of table
114 0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
116 0x00, 0x01, // program_number(1)
118 0xc1, // version_number(0), current_next_indicator(1)
120 0x00, 0x00, // section_number(0), last_section_number(0)
122 0xe0, 0x00, // PCR_PID
124 0xf0, 0x00, // program_info_length(0)
126 0x02, 0xe0, 0x00, 0xf0, 0x00, // video stream type(2), pid
127 0x04, 0xe0, 0x00, 0xf0, 0x00, // audio stream type(4), pid
129 0x00, 0x00, 0x00, 0x00 // CRC32
132 static struct v4l2_mpeg_compression param_defaults
=
134 .st_type
= V4L2_MPEG_TS_2
,
136 .mode
= V4L2_BITRATE_CBR
,
145 .vi_type
= V4L2_MPEG_VI_2
,
146 .vi_aspect_ratio
= V4L2_MPEG_ASPECT_4_3
,
148 .mode
= V4L2_BITRATE_VBR
,
153 .au_type
= V4L2_MPEG_AU_2_II
,
155 .mode
= V4L2_BITRATE_CBR
,
160 /* FIXME: size? via S_FMT? */
161 .video_format
= MPEG_VIDEO_FORMAT_D1
,
165 /* ---------------------------------------------------------------------- */
167 static int saa6752hs_chip_command(struct i2c_client
* client
,
168 enum saa6752hs_command command
)
170 unsigned char buf
[3];
171 unsigned long timeout
;
174 // execute the command
176 case SAA6752HS_COMMAND_RESET
:
180 case SAA6752HS_COMMAND_STOP
:
184 case SAA6752HS_COMMAND_START
:
188 case SAA6752HS_COMMAND_PAUSE
:
192 case SAA6752HS_COMMAND_RECONFIGURE
:
196 case SAA6752HS_COMMAND_SLEEP
:
200 case SAA6752HS_COMMAND_RECONFIGURE_FORCE
:
208 // set it and wait for it to be so
209 i2c_master_send(client
, buf
, 1);
210 timeout
= jiffies
+ HZ
* 3;
212 // get the current status
214 i2c_master_send(client
, buf
, 1);
215 i2c_master_recv(client
, buf
, 1);
217 if (!(buf
[0] & 0x20))
219 if (time_after(jiffies
,timeout
)) {
228 // delay a bit to let encoder settle
236 static int saa6752hs_set_bitrate(struct i2c_client
* client
,
237 struct v4l2_mpeg_compression
* params
)
241 // set the bitrate mode
243 buf
[1] = (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
) ? 0 : 1;
244 i2c_master_send(client
, buf
, 2);
246 // set the video bitrate
247 if (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
) {
248 // set the target bitrate
250 buf
[1] = params
->vi_bitrate
.target
>> 8;
251 buf
[2] = params
->vi_bitrate
.target
& 0xff;
252 i2c_master_send(client
, buf
, 3);
254 // set the max bitrate
256 buf
[1] = params
->vi_bitrate
.max
>> 8;
257 buf
[2] = params
->vi_bitrate
.max
& 0xff;
258 i2c_master_send(client
, buf
, 3);
260 // set the target bitrate (no max bitrate for CBR)
262 buf
[1] = params
->vi_bitrate
.target
>> 8;
263 buf
[2] = params
->vi_bitrate
.target
& 0xff;
264 i2c_master_send(client
, buf
, 3);
267 // set the audio bitrate
269 buf
[1] = (256 == params
->au_bitrate
.target
) ? 0 : 1;
270 i2c_master_send(client
, buf
, 2);
272 // set the total bitrate
274 buf
[1] = params
->st_bitrate
.target
>> 8;
275 buf
[2] = params
->st_bitrate
.target
& 0xff;
276 i2c_master_send(client
, buf
, 3);
282 static void saa6752hs_set_subsampling(struct i2c_client
* client
,
283 struct v4l2_format
* f
)
285 struct saa6752hs_state
*h
= i2c_get_clientdata(client
);
286 int dist_352
, dist_480
, dist_720
;
289 FIXME: translate and round width/height into EMPRESS
293 ---------------------------
294 SIF | 352x288 | 352x240
295 1/2 D1 | 352x576 | 352x480
296 2/3 D1 | 480x576 | 480x480
297 D1 | 720x576 | 720x480
300 dist_352
= abs(f
->fmt
.pix
.width
- 352);
301 dist_480
= abs(f
->fmt
.pix
.width
- 480);
302 dist_720
= abs(f
->fmt
.pix
.width
- 720);
303 if (dist_720
< dist_480
) {
304 f
->fmt
.pix
.width
= 720;
305 f
->fmt
.pix
.height
= 576;
306 h
->video_format
= SAA6752HS_VF_D1
;
308 else if (dist_480
< dist_352
) {
309 f
->fmt
.pix
.width
= 480;
310 f
->fmt
.pix
.height
= 576;
311 h
->video_format
= SAA6752HS_VF_2_3_D1
;
314 f
->fmt
.pix
.width
= 352;
315 if (abs(f
->fmt
.pix
.height
- 576) <
316 abs(f
->fmt
.pix
.height
- 288)) {
317 f
->fmt
.pix
.height
= 576;
318 h
->video_format
= SAA6752HS_VF_1_2_D1
;
321 f
->fmt
.pix
.height
= 288;
322 h
->video_format
= SAA6752HS_VF_SIF
;
328 static void saa6752hs_set_params(struct i2c_client
* client
,
329 struct v4l2_mpeg_compression
* params
)
331 struct saa6752hs_state
*h
= i2c_get_clientdata(client
);
334 if (params
->ts_pid_pmt
<= MPEG_PID_MAX
)
335 h
->params
.ts_pid_pmt
= params
->ts_pid_pmt
;
336 if (params
->ts_pid_pcr
<= MPEG_PID_MAX
)
337 h
->params
.ts_pid_pcr
= params
->ts_pid_pcr
;
338 if (params
->ts_pid_video
<= MPEG_PID_MAX
)
339 h
->params
.ts_pid_video
= params
->ts_pid_video
;
340 if (params
->ts_pid_audio
<= MPEG_PID_MAX
)
341 h
->params
.ts_pid_audio
= params
->ts_pid_audio
;
343 /* check bitrate parameters */
344 if ((params
->vi_bitrate
.mode
== V4L2_BITRATE_CBR
) ||
345 (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
))
346 h
->params
.vi_bitrate
.mode
= params
->vi_bitrate
.mode
;
347 if (params
->vi_bitrate
.mode
!= V4L2_BITRATE_NONE
)
348 h
->params
.st_bitrate
.target
= params
->st_bitrate
.target
;
349 if (params
->vi_bitrate
.mode
!= V4L2_BITRATE_NONE
)
350 h
->params
.vi_bitrate
.target
= params
->vi_bitrate
.target
;
351 if (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
)
352 h
->params
.vi_bitrate
.max
= params
->vi_bitrate
.max
;
353 if (params
->au_bitrate
.mode
!= V4L2_BITRATE_NONE
)
354 h
->params
.au_bitrate
.target
= params
->au_bitrate
.target
;
357 if (params
->vi_aspect_ratio
== V4L2_MPEG_ASPECT_4_3
||
358 params
->vi_aspect_ratio
== V4L2_MPEG_ASPECT_16_9
)
359 h
->params
.vi_aspect_ratio
= params
->vi_aspect_ratio
;
362 if (h
->params
.st_bitrate
.target
> MPEG_TOTAL_TARGET_BITRATE_MAX
)
363 h
->params
.st_bitrate
.target
= MPEG_TOTAL_TARGET_BITRATE_MAX
;
364 if (h
->params
.vi_bitrate
.target
> MPEG_VIDEO_TARGET_BITRATE_MAX
)
365 h
->params
.vi_bitrate
.target
= MPEG_VIDEO_TARGET_BITRATE_MAX
;
366 if (h
->params
.vi_bitrate
.max
> MPEG_VIDEO_MAX_BITRATE_MAX
)
367 h
->params
.vi_bitrate
.max
= MPEG_VIDEO_MAX_BITRATE_MAX
;
368 if (h
->params
.au_bitrate
.target
<= 256)
369 h
->params
.au_bitrate
.target
= 256;
371 h
->params
.au_bitrate
.target
= 384;
374 static int saa6752hs_init(struct i2c_client
* client
)
376 unsigned char buf
[9], buf2
[4];
377 struct saa6752hs_state
*h
;
379 unsigned char localPAT
[256];
380 unsigned char localPMT
[256];
382 h
= i2c_get_clientdata(client
);
384 // Set video format - must be done first as it resets other settings
386 buf
[1] = h
->video_format
;
387 i2c_master_send(client
, buf
, 2);
390 saa6752hs_set_bitrate(client
, &h
->params
);
392 // Set GOP structure {3, 13}
396 i2c_master_send(client
,buf
,3);
398 // Set minimum Q-scale {4}
401 i2c_master_send(client
,buf
,2);
403 // Set maximum Q-scale {12}
406 i2c_master_send(client
,buf
,2);
408 // Set Output Protocol
411 i2c_master_send(client
,buf
,2);
413 // Set video output stream format {TS}
416 i2c_master_send(client
,buf
,2);
419 memcpy(localPAT
, PAT
, sizeof(PAT
));
420 localPAT
[17] = 0xe0 | ((h
->params
.ts_pid_pmt
>> 8) & 0x0f);
421 localPAT
[18] = h
->params
.ts_pid_pmt
& 0xff;
422 crc
= crc32_be(~0, &localPAT
[7], sizeof(PAT
) - 7 - 4);
423 localPAT
[sizeof(PAT
) - 4] = (crc
>> 24) & 0xFF;
424 localPAT
[sizeof(PAT
) - 3] = (crc
>> 16) & 0xFF;
425 localPAT
[sizeof(PAT
) - 2] = (crc
>> 8) & 0xFF;
426 localPAT
[sizeof(PAT
) - 1] = crc
& 0xFF;
429 memcpy(localPMT
, PMT
, sizeof(PMT
));
430 localPMT
[3] = 0x40 | ((h
->params
.ts_pid_pmt
>> 8) & 0x0f);
431 localPMT
[4] = h
->params
.ts_pid_pmt
& 0xff;
432 localPMT
[15] = 0xE0 | ((h
->params
.ts_pid_pcr
>> 8) & 0x0F);
433 localPMT
[16] = h
->params
.ts_pid_pcr
& 0xFF;
434 localPMT
[20] = 0xE0 | ((h
->params
.ts_pid_video
>> 8) & 0x0F);
435 localPMT
[21] = h
->params
.ts_pid_video
& 0xFF;
436 localPMT
[25] = 0xE0 | ((h
->params
.ts_pid_audio
>> 8) & 0x0F);
437 localPMT
[26] = h
->params
.ts_pid_audio
& 0xFF;
438 crc
= crc32_be(~0, &localPMT
[7], sizeof(PMT
) - 7 - 4);
439 localPMT
[sizeof(PMT
) - 4] = (crc
>> 24) & 0xFF;
440 localPMT
[sizeof(PMT
) - 3] = (crc
>> 16) & 0xFF;
441 localPMT
[sizeof(PMT
) - 2] = (crc
>> 8) & 0xFF;
442 localPMT
[sizeof(PMT
) - 1] = crc
& 0xFF;
446 buf
[1] = (h
->params
.ts_pid_audio
>> 8) & 0xFF;
447 buf
[2] = h
->params
.ts_pid_audio
& 0xFF;
448 i2c_master_send(client
,buf
,3);
452 buf
[1] = (h
->params
.ts_pid_video
>> 8) & 0xFF;
453 buf
[2] = h
->params
.ts_pid_video
& 0xFF;
454 i2c_master_send(client
,buf
,3);
458 buf
[1] = (h
->params
.ts_pid_pcr
>> 8) & 0xFF;
459 buf
[2] = h
->params
.ts_pid_pcr
& 0xFF;
460 i2c_master_send(client
,buf
,3);
463 i2c_master_send(client
,localPAT
,sizeof(PAT
));
464 i2c_master_send(client
,localPMT
,sizeof(PMT
));
466 // mute then unmute audio. This removes buzzing artefacts
469 i2c_master_send(client
, buf
, 2);
471 i2c_master_send(client
, buf
, 2);
474 saa6752hs_chip_command(client
, SAA6752HS_COMMAND_START
);
476 // readout current state
482 i2c_master_send(client
, buf
, 5);
483 i2c_master_recv(client
, buf2
, 4);
485 // change aspect ratio
492 switch(h
->params
.vi_aspect_ratio
) {
493 case V4L2_MPEG_ASPECT_16_9
:
494 buf
[6] = buf2
[1] | 0x40;
496 case V4L2_MPEG_ASPECT_4_3
:
498 buf
[6] = buf2
[1] & 0xBF;
504 i2c_master_send(client
, buf
, 9);
510 static int saa6752hs_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
512 struct saa6752hs_state
*h
;
514 printk("saa6752hs: chip found @ 0x%x\n", addr
<<1);
516 if (NULL
== (h
= kmalloc(sizeof(*h
), GFP_KERNEL
)))
518 memset(h
,0,sizeof(*h
));
519 h
->client
= client_template
;
520 h
->params
= param_defaults
;
521 h
->client
.adapter
= adap
;
522 h
->client
.addr
= addr
;
524 i2c_set_clientdata(&h
->client
, h
);
525 i2c_attach_client(&h
->client
);
529 static int saa6752hs_probe(struct i2c_adapter
*adap
)
531 if (adap
->class & I2C_CLASS_TV_ANALOG
)
532 return i2c_probe(adap
, &addr_data
, saa6752hs_attach
);
536 static int saa6752hs_detach(struct i2c_client
*client
)
538 struct saa6752hs_state
*h
;
540 h
= i2c_get_clientdata(client
);
541 i2c_detach_client(client
);
547 saa6752hs_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
549 struct saa6752hs_state
*h
= i2c_get_clientdata(client
);
550 struct v4l2_mpeg_compression
*params
= arg
;
554 case VIDIOC_S_MPEGCOMP
:
555 if (NULL
== params
) {
556 /* apply settings and start encoder */
557 saa6752hs_init(client
);
560 saa6752hs_set_params(client
, params
);
562 case VIDIOC_G_MPEGCOMP
:
567 struct v4l2_format
*f
= arg
;
569 if (h
->video_format
== SAA6752HS_VF_UNKNOWN
)
570 h
->video_format
= SAA6752HS_VF_D1
;
572 v4l2_format_table
[h
->video_format
].fmt
.pix
.width
;
574 v4l2_format_table
[h
->video_format
].fmt
.pix
.height
;
579 struct v4l2_format
*f
= arg
;
581 saa6752hs_set_subsampling(client
, f
);
592 /* ----------------------------------------------------------------------- */
594 static struct i2c_driver driver
= {
595 .owner
= THIS_MODULE
,
596 .name
= "i2c saa6752hs MPEG encoder",
597 .id
= I2C_DRIVERID_SAA6752HS
,
598 .flags
= I2C_DF_NOTIFY
,
599 .attach_adapter
= saa6752hs_probe
,
600 .detach_client
= saa6752hs_detach
,
601 .command
= saa6752hs_command
,
604 static struct i2c_client client_template
=
606 I2C_DEVNAME("saa6752hs"),
607 .flags
= I2C_CLIENT_ALLOW_USE
,
611 static int __init
saa6752hs_init_module(void)
613 return i2c_add_driver(&driver
);
616 static void __exit
saa6752hs_cleanup_module(void)
618 i2c_del_driver(&driver
);
621 module_init(saa6752hs_init_module
);
622 module_exit(saa6752hs_cleanup_module
);
625 * Overrides for Emacs so that we follow Linus's tabbing style.
626 * ---------------------------------------------------------------------------