Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / saa7134 / saa6752hs.c
blobcee13584c9cfa123612e3dfa6ade71a8fa3d09b3
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>
16 #include <media/id.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};
26 I2C_CLIENT_INSMOD;
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,
49 SAA6752HS_COMMAND_MAX
52 /* ---------------------------------------------------------------------- */
54 static u8 PAT[] = {
55 0xc2, // i2c register
56 0x00, // table number for encoder
58 0x47, // sync
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
64 0x00, // tid(0)
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
80 static u8 PMT[] = {
81 0xc2, // i2c register
82 0x01, // table number for encoder
84 0x47, // sync
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
90 0x02, // tid(2)
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,
112 .st_bitrate = {
113 .mode = V4L2_BITRATE_CBR,
114 .target = 7000,
117 .ts_pid_pmt = 16,
118 .ts_pid_video = 260,
119 .ts_pid_audio = 256,
120 .ts_pid_pcr = 259,
122 .vi_type = V4L2_MPEG_VI_2,
123 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
124 .vi_bitrate = {
125 .mode = V4L2_BITRATE_VBR,
126 .target = 4000,
127 .max = 6000,
130 .au_type = V4L2_MPEG_AU_2_II,
131 .au_bitrate = {
132 .mode = V4L2_BITRATE_CBR,
133 .target = 256,
136 #if 0
137 /* FIXME: size? via S_FMT? */
138 .video_format = MPEG_VIDEO_FORMAT_D1,
139 #endif
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;
149 int status = 0;
151 // execute the command
152 switch(command) {
153 case SAA6752HS_COMMAND_RESET:
154 buf[0] = 0x00;
155 break;
157 case SAA6752HS_COMMAND_STOP:
158 buf[0] = 0x03;
159 break;
161 case SAA6752HS_COMMAND_START:
162 buf[0] = 0x02;
163 break;
165 case SAA6752HS_COMMAND_PAUSE:
166 buf[0] = 0x04;
167 break;
169 case SAA6752HS_COMMAND_RECONFIGURE:
170 buf[0] = 0x05;
171 break;
173 case SAA6752HS_COMMAND_SLEEP:
174 buf[0] = 0x06;
175 break;
177 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
178 buf[0] = 0x07;
179 break;
181 default:
182 return -EINVAL;
185 // set it and wait for it to be so
186 i2c_master_send(client, buf, 1);
187 timeout = jiffies + HZ * 3;
188 for (;;) {
189 // get the current status
190 buf[0] = 0x10;
191 i2c_master_send(client, buf, 1);
192 i2c_master_recv(client, buf, 1);
194 if (!(buf[0] & 0x20))
195 break;
196 if (time_after(jiffies,timeout)) {
197 status = -ETIMEDOUT;
198 break;
201 // wait a bit
202 msleep(10);
205 // delay a bit to let encoder settle
206 msleep(50);
208 // done
209 return status;
213 static int saa6752hs_set_bitrate(struct i2c_client* client,
214 struct v4l2_mpeg_compression* params)
216 u8 buf[3];
218 // set the bitrate mode
219 buf[0] = 0x71;
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
226 buf[0] = 0x80;
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
232 buf[0] = 0x81;
233 buf[1] = params->vi_bitrate.max >> 8;
234 buf[2] = params->vi_bitrate.max & 0xff;
235 i2c_master_send(client, buf, 3);
236 } else {
237 // set the target bitrate (no max bitrate for CBR)
238 buf[0] = 0x81;
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
245 buf[0] = 0x94;
246 buf[1] = (256 == params->au_bitrate.target) ? 0 : 1;
247 i2c_master_send(client, buf, 2);
249 // set the total bitrate
250 buf[0] = 0xb1;
251 buf[1] = params->st_bitrate.target >> 8;
252 buf[2] = params->st_bitrate.target & 0xff;
253 i2c_master_send(client, buf, 3);
255 // return success
256 return 0;
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);
265 /* check PIDs */
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;
288 /* aspect ratio */
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;
293 /* range checks */
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;
302 else
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;
310 u32 crc;
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
317 buf[0] = 0x41;
318 buf[1] = 0 /* MPEG_VIDEO_FORMAT_D1 */;
319 i2c_master_send(client, buf, 2);
321 // set bitrate
322 saa6752hs_set_bitrate(client, &h->params);
324 // Set GOP structure {3, 13}
325 buf[0] = 0x72;
326 buf[1] = 0x03;
327 buf[2] = 0x0D;
328 i2c_master_send(client,buf,3);
330 // Set minimum Q-scale {4}
331 buf[0] = 0x82;
332 buf[1] = 0x04;
333 i2c_master_send(client,buf,2);
335 // Set maximum Q-scale {12}
336 buf[0] = 0x83;
337 buf[1] = 0x0C;
338 i2c_master_send(client,buf,2);
340 // Set Output Protocol
341 buf[0] = 0xD0;
342 buf[1] = 0x81;
343 i2c_master_send(client,buf,2);
345 // Set video output stream format {TS}
346 buf[0] = 0xB0;
347 buf[1] = 0x05;
348 i2c_master_send(client,buf,2);
350 /* compute PAT */
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;
360 /* compute PMT */
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;
376 // Set Audio PID
377 buf[0] = 0xC1;
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);
382 // Set Video PID
383 buf[0] = 0xC0;
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);
388 // Set PCR PID
389 buf[0] = 0xC4;
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);
394 // Send SI tables
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
399 buf[0] = 0xa4;
400 buf[1] = 1;
401 i2c_master_send(client, buf, 2);
402 buf[1] = 0;
403 i2c_master_send(client, buf, 2);
405 // start it going
406 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
408 // readout current state
409 buf[0] = 0xE1;
410 buf[1] = 0xA7;
411 buf[2] = 0xFE;
412 buf[3] = 0x82;
413 buf[4] = 0xB0;
414 i2c_master_send(client, buf, 5);
415 i2c_master_recv(client, buf2, 4);
417 // change aspect ratio
418 buf[0] = 0xE0;
419 buf[1] = 0xA7;
420 buf[2] = 0xFE;
421 buf[3] = 0x82;
422 buf[4] = 0xB0;
423 buf[5] = buf2[0];
424 switch(h->params.vi_aspect_ratio) {
425 case V4L2_MPEG_ASPECT_16_9:
426 buf[6] = buf2[1] | 0x40;
427 break;
428 case V4L2_MPEG_ASPECT_4_3:
429 default:
430 buf[6] = buf2[1] & 0xBF;
431 break;
432 break;
434 buf[7] = buf2[2];
435 buf[8] = buf2[3];
436 i2c_master_send(client, buf, 9);
438 // return success
439 return 0;
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)))
449 return -ENOMEM;
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);
458 return 0;
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);
465 return 0;
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);
474 kfree(h);
475 return 0;
478 static int
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;
483 int err = 0;
485 switch (cmd) {
486 case VIDIOC_S_MPEGCOMP:
487 if (NULL == params) {
488 /* apply settings and start encoder */
489 saa6752hs_init(client);
490 break;
492 saa6752hs_set_params(client, params);
493 /* fall through */
494 case VIDIOC_G_MPEGCOMP:
495 *params = h->params;
496 break;
497 default:
498 /* nothing */
499 break;
502 return err;
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,
521 .driver = &driver,
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 * ---------------------------------------------------------------------------
540 * Local variables:
541 * c-basic-offset: 8
542 * End: