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 struct saa6752hs_state
{
36 struct i2c_client client
;
37 struct v4l2_mpeg_compression params
;
40 enum saa6752hs_command
{
41 SAA6752HS_COMMAND_RESET
= 0,
42 SAA6752HS_COMMAND_STOP
= 1,
43 SAA6752HS_COMMAND_START
= 2,
44 SAA6752HS_COMMAND_PAUSE
= 3,
45 SAA6752HS_COMMAND_RECONFIGURE
= 4,
46 SAA6752HS_COMMAND_SLEEP
= 5,
47 SAA6752HS_COMMAND_RECONFIGURE_FORCE
= 6,
52 /* ---------------------------------------------------------------------- */
56 0x00, // table number for encoder
59 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0)
60 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
62 0x00, // PSI pointer to start of table
65 0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
67 0x00, 0x01, // transport_stream_id(1)
69 0xc1, // version_number(0), current_next_indicator(1)
71 0x00, 0x00, // section_number(0), last_section_number(0)
73 0x00, 0x01, // program_number(1)
75 0xe0, 0x00, // PMT PID
77 0x00, 0x00, 0x00, 0x00 // CRC32
82 0x01, // table number for encoder
85 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid
86 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
88 0x00, // PSI pointer to start of table
91 0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
93 0x00, 0x01, // program_number(1)
95 0xc1, // version_number(0), current_next_indicator(1)
97 0x00, 0x00, // section_number(0), last_section_number(0)
99 0xe0, 0x00, // PCR_PID
101 0xf0, 0x00, // program_info_length(0)
103 0x02, 0xe0, 0x00, 0xf0, 0x00, // video stream type(2), pid
104 0x04, 0xe0, 0x00, 0xf0, 0x00, // audio stream type(4), pid
106 0x00, 0x00, 0x00, 0x00 // CRC32
109 static struct v4l2_mpeg_compression param_defaults
=
111 .st_type
= V4L2_MPEG_TS_2
,
113 .mode
= V4L2_BITRATE_CBR
,
122 .vi_type
= V4L2_MPEG_VI_2
,
123 .vi_aspect_ratio
= V4L2_MPEG_ASPECT_4_3
,
125 .mode
= V4L2_BITRATE_VBR
,
130 .au_type
= V4L2_MPEG_AU_2_II
,
132 .mode
= V4L2_BITRATE_CBR
,
137 /* FIXME: size? via S_FMT? */
138 .video_format
= MPEG_VIDEO_FORMAT_D1
,
142 /* ---------------------------------------------------------------------- */
144 static int saa6752hs_chip_command(struct i2c_client
* client
,
145 enum saa6752hs_command command
)
147 unsigned char buf
[3];
148 unsigned long timeout
;
151 // execute the command
153 case SAA6752HS_COMMAND_RESET
:
157 case SAA6752HS_COMMAND_STOP
:
161 case SAA6752HS_COMMAND_START
:
165 case SAA6752HS_COMMAND_PAUSE
:
169 case SAA6752HS_COMMAND_RECONFIGURE
:
173 case SAA6752HS_COMMAND_SLEEP
:
177 case SAA6752HS_COMMAND_RECONFIGURE_FORCE
:
185 // set it and wait for it to be so
186 i2c_master_send(client
, buf
, 1);
187 timeout
= jiffies
+ HZ
* 3;
189 // get the current status
191 i2c_master_send(client
, buf
, 1);
192 i2c_master_recv(client
, buf
, 1);
194 if (!(buf
[0] & 0x20))
196 if (time_after(jiffies
,timeout
)) {
205 // delay a bit to let encoder settle
213 static int saa6752hs_set_bitrate(struct i2c_client
* client
,
214 struct v4l2_mpeg_compression
* params
)
218 // set the bitrate mode
220 buf
[1] = (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
) ? 0 : 1;
221 i2c_master_send(client
, buf
, 2);
223 // set the video bitrate
224 if (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
) {
225 // set the target bitrate
227 buf
[1] = params
->vi_bitrate
.target
>> 8;
228 buf
[2] = params
->vi_bitrate
.target
& 0xff;
229 i2c_master_send(client
, buf
, 3);
231 // set the max bitrate
233 buf
[1] = params
->vi_bitrate
.max
>> 8;
234 buf
[2] = params
->vi_bitrate
.max
& 0xff;
235 i2c_master_send(client
, buf
, 3);
237 // set the target bitrate (no max bitrate for CBR)
239 buf
[1] = params
->vi_bitrate
.target
>> 8;
240 buf
[2] = params
->vi_bitrate
.target
& 0xff;
241 i2c_master_send(client
, buf
, 3);
244 // set the audio bitrate
246 buf
[1] = (256 == params
->au_bitrate
.target
) ? 0 : 1;
247 i2c_master_send(client
, buf
, 2);
249 // set the total bitrate
251 buf
[1] = params
->st_bitrate
.target
>> 8;
252 buf
[2] = params
->st_bitrate
.target
& 0xff;
253 i2c_master_send(client
, buf
, 3);
260 static void saa6752hs_set_params(struct i2c_client
* client
,
261 struct v4l2_mpeg_compression
* params
)
263 struct saa6752hs_state
*h
= i2c_get_clientdata(client
);
266 if (params
->ts_pid_pmt
<= MPEG_PID_MAX
)
267 h
->params
.ts_pid_pmt
= params
->ts_pid_pmt
;
268 if (params
->ts_pid_pcr
<= MPEG_PID_MAX
)
269 h
->params
.ts_pid_pcr
= params
->ts_pid_pcr
;
270 if (params
->ts_pid_video
<= MPEG_PID_MAX
)
271 h
->params
.ts_pid_video
= params
->ts_pid_video
;
272 if (params
->ts_pid_audio
<= MPEG_PID_MAX
)
273 h
->params
.ts_pid_audio
= params
->ts_pid_audio
;
275 /* check bitrate parameters */
276 if ((params
->vi_bitrate
.mode
== V4L2_BITRATE_CBR
) ||
277 (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
))
278 h
->params
.vi_bitrate
.mode
= params
->vi_bitrate
.mode
;
279 if (params
->vi_bitrate
.mode
!= V4L2_BITRATE_NONE
)
280 h
->params
.st_bitrate
.target
= params
->st_bitrate
.target
;
281 if (params
->vi_bitrate
.mode
!= V4L2_BITRATE_NONE
)
282 h
->params
.vi_bitrate
.target
= params
->vi_bitrate
.target
;
283 if (params
->vi_bitrate
.mode
== V4L2_BITRATE_VBR
)
284 h
->params
.vi_bitrate
.max
= params
->vi_bitrate
.max
;
285 if (params
->au_bitrate
.mode
!= V4L2_BITRATE_NONE
)
286 h
->params
.au_bitrate
.target
= params
->au_bitrate
.target
;
289 if (params
->vi_aspect_ratio
== V4L2_MPEG_ASPECT_4_3
||
290 params
->vi_aspect_ratio
== V4L2_MPEG_ASPECT_16_9
)
291 h
->params
.vi_aspect_ratio
= params
->vi_aspect_ratio
;
294 if (h
->params
.st_bitrate
.target
> MPEG_TOTAL_TARGET_BITRATE_MAX
)
295 h
->params
.st_bitrate
.target
= MPEG_TOTAL_TARGET_BITRATE_MAX
;
296 if (h
->params
.vi_bitrate
.target
> MPEG_VIDEO_TARGET_BITRATE_MAX
)
297 h
->params
.vi_bitrate
.target
= MPEG_VIDEO_TARGET_BITRATE_MAX
;
298 if (h
->params
.vi_bitrate
.max
> MPEG_VIDEO_MAX_BITRATE_MAX
)
299 h
->params
.vi_bitrate
.max
= MPEG_VIDEO_MAX_BITRATE_MAX
;
300 if (h
->params
.au_bitrate
.target
<= 256)
301 h
->params
.au_bitrate
.target
= 256;
303 h
->params
.au_bitrate
.target
= 384;
306 static int saa6752hs_init(struct i2c_client
* client
)
308 unsigned char buf
[9], buf2
[4];
309 struct saa6752hs_state
*h
;
311 unsigned char localPAT
[256];
312 unsigned char localPMT
[256];
314 h
= i2c_get_clientdata(client
);
316 // Set video format - must be done first as it resets other settings
318 buf
[1] = 0 /* MPEG_VIDEO_FORMAT_D1 */;
319 i2c_master_send(client
, buf
, 2);
322 saa6752hs_set_bitrate(client
, &h
->params
);
324 // Set GOP structure {3, 13}
328 i2c_master_send(client
,buf
,3);
330 // Set minimum Q-scale {4}
333 i2c_master_send(client
,buf
,2);
335 // Set maximum Q-scale {12}
338 i2c_master_send(client
,buf
,2);
340 // Set Output Protocol
343 i2c_master_send(client
,buf
,2);
345 // Set video output stream format {TS}
348 i2c_master_send(client
,buf
,2);
351 memcpy(localPAT
, PAT
, sizeof(PAT
));
352 localPAT
[17] = 0xe0 | ((h
->params
.ts_pid_pmt
>> 8) & 0x0f);
353 localPAT
[18] = h
->params
.ts_pid_pmt
& 0xff;
354 crc
= crc32_be(~0, &localPAT
[7], sizeof(PAT
) - 7 - 4);
355 localPAT
[sizeof(PAT
) - 4] = (crc
>> 24) & 0xFF;
356 localPAT
[sizeof(PAT
) - 3] = (crc
>> 16) & 0xFF;
357 localPAT
[sizeof(PAT
) - 2] = (crc
>> 8) & 0xFF;
358 localPAT
[sizeof(PAT
) - 1] = crc
& 0xFF;
361 memcpy(localPMT
, PMT
, sizeof(PMT
));
362 localPMT
[3] = 0x40 | ((h
->params
.ts_pid_pmt
>> 8) & 0x0f);
363 localPMT
[4] = h
->params
.ts_pid_pmt
& 0xff;
364 localPMT
[15] = 0xE0 | ((h
->params
.ts_pid_pcr
>> 8) & 0x0F);
365 localPMT
[16] = h
->params
.ts_pid_pcr
& 0xFF;
366 localPMT
[20] = 0xE0 | ((h
->params
.ts_pid_video
>> 8) & 0x0F);
367 localPMT
[21] = h
->params
.ts_pid_video
& 0xFF;
368 localPMT
[25] = 0xE0 | ((h
->params
.ts_pid_audio
>> 8) & 0x0F);
369 localPMT
[26] = h
->params
.ts_pid_audio
& 0xFF;
370 crc
= crc32_be(~0, &localPMT
[7], sizeof(PMT
) - 7 - 4);
371 localPMT
[sizeof(PMT
) - 4] = (crc
>> 24) & 0xFF;
372 localPMT
[sizeof(PMT
) - 3] = (crc
>> 16) & 0xFF;
373 localPMT
[sizeof(PMT
) - 2] = (crc
>> 8) & 0xFF;
374 localPMT
[sizeof(PMT
) - 1] = crc
& 0xFF;
378 buf
[1] = (h
->params
.ts_pid_audio
>> 8) & 0xFF;
379 buf
[2] = h
->params
.ts_pid_audio
& 0xFF;
380 i2c_master_send(client
,buf
,3);
384 buf
[1] = (h
->params
.ts_pid_video
>> 8) & 0xFF;
385 buf
[2] = h
->params
.ts_pid_video
& 0xFF;
386 i2c_master_send(client
,buf
,3);
390 buf
[1] = (h
->params
.ts_pid_pcr
>> 8) & 0xFF;
391 buf
[2] = h
->params
.ts_pid_pcr
& 0xFF;
392 i2c_master_send(client
,buf
,3);
395 i2c_master_send(client
,localPAT
,sizeof(PAT
));
396 i2c_master_send(client
,localPMT
,sizeof(PMT
));
398 // mute then unmute audio. This removes buzzing artefacts
401 i2c_master_send(client
, buf
, 2);
403 i2c_master_send(client
, buf
, 2);
406 saa6752hs_chip_command(client
, SAA6752HS_COMMAND_START
);
408 // readout current state
414 i2c_master_send(client
, buf
, 5);
415 i2c_master_recv(client
, buf2
, 4);
417 // change aspect ratio
424 switch(h
->params
.vi_aspect_ratio
) {
425 case V4L2_MPEG_ASPECT_16_9
:
426 buf
[6] = buf2
[1] | 0x40;
428 case V4L2_MPEG_ASPECT_4_3
:
430 buf
[6] = buf2
[1] & 0xBF;
436 i2c_master_send(client
, buf
, 9);
442 static int saa6752hs_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
444 struct saa6752hs_state
*h
;
446 printk("saa6752hs: chip found @ 0x%x\n", addr
<<1);
448 if (NULL
== (h
= kmalloc(sizeof(*h
), GFP_KERNEL
)))
450 memset(h
,0,sizeof(*h
));
451 h
->client
= client_template
;
452 h
->params
= param_defaults
;
453 h
->client
.adapter
= adap
;
454 h
->client
.addr
= addr
;
456 i2c_set_clientdata(&h
->client
, h
);
457 i2c_attach_client(&h
->client
);
461 static int saa6752hs_probe(struct i2c_adapter
*adap
)
463 if (adap
->class & I2C_CLASS_TV_ANALOG
)
464 return i2c_probe(adap
, &addr_data
, saa6752hs_attach
);
468 static int saa6752hs_detach(struct i2c_client
*client
)
470 struct saa6752hs_state
*h
;
472 h
= i2c_get_clientdata(client
);
473 i2c_detach_client(client
);
479 saa6752hs_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
481 struct saa6752hs_state
*h
= i2c_get_clientdata(client
);
482 struct v4l2_mpeg_compression
*params
= arg
;
486 case VIDIOC_S_MPEGCOMP
:
487 if (NULL
== params
) {
488 /* apply settings and start encoder */
489 saa6752hs_init(client
);
492 saa6752hs_set_params(client
, params
);
494 case VIDIOC_G_MPEGCOMP
:
505 /* ----------------------------------------------------------------------- */
507 static struct i2c_driver driver
= {
508 .owner
= THIS_MODULE
,
509 .name
= "i2c saa6752hs MPEG encoder",
510 .id
= I2C_DRIVERID_SAA6752HS
,
511 .flags
= I2C_DF_NOTIFY
,
512 .attach_adapter
= saa6752hs_probe
,
513 .detach_client
= saa6752hs_detach
,
514 .command
= saa6752hs_command
,
517 static struct i2c_client client_template
=
519 I2C_DEVNAME("saa6752hs"),
520 .flags
= I2C_CLIENT_ALLOW_USE
,
524 static int __init
saa6752hs_init_module(void)
526 return i2c_add_driver(&driver
);
529 static void __exit
saa6752hs_cleanup_module(void)
531 i2c_del_driver(&driver
);
534 module_init(saa6752hs_init_module
);
535 module_exit(saa6752hs_cleanup_module
);
538 * Overrides for Emacs so that we follow Linus's tabbing style.
539 * ---------------------------------------------------------------------------