Merge with rsync://fileserver/linux
[pandora-kernel.git] / drivers / media / video / saa7134 / saa6752hs.c
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>
15
16 #include <media/id.h>
17
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)
22
23 /* Addresses to scan */
24 static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
25 I2C_CLIENT_INSMOD;
26
27 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
28 MODULE_AUTHOR("Andrew de Quincey");
29 MODULE_LICENSE("GPL");
30
31 static struct i2c_driver driver;
32 static struct i2c_client client_template;
33
34 enum saa6752hs_videoformat {
35         SAA6752HS_VF_D1 = 0,    /* standard D1 video format: 720x576 */
36         SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
37         SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
38         SAA6752HS_VF_SIF = 3,   /* SIF video format: 352x288 */
39         SAA6752HS_VF_UNKNOWN,
40 };
41
42 static const struct v4l2_format v4l2_format_table[] =
43 {
44         [SAA6752HS_VF_D1] =
45                 { .fmt = { .pix = { .width = 720, .height = 576 }}},
46         [SAA6752HS_VF_2_3_D1] =
47                 { .fmt = { .pix = { .width = 480, .height = 576 }}},
48         [SAA6752HS_VF_1_2_D1] =
49                 { .fmt = { .pix = { .width = 352, .height = 576 }}},
50         [SAA6752HS_VF_SIF] =
51                 { .fmt = { .pix = { .width = 352, .height = 288 }}},
52         [SAA6752HS_VF_UNKNOWN] =
53                 { .fmt = { .pix = { .width = 0, .height = 0}}},
54 };
55
56 struct saa6752hs_state {
57         struct i2c_client             client;
58         struct v4l2_mpeg_compression  params;
59         enum saa6752hs_videoformat    video_format;
60 };
61
62 enum saa6752hs_command {
63         SAA6752HS_COMMAND_RESET = 0,
64         SAA6752HS_COMMAND_STOP = 1,
65         SAA6752HS_COMMAND_START = 2,
66         SAA6752HS_COMMAND_PAUSE = 3,
67         SAA6752HS_COMMAND_RECONFIGURE = 4,
68         SAA6752HS_COMMAND_SLEEP = 5,
69         SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
70
71         SAA6752HS_COMMAND_MAX
72 };
73
74 /* ---------------------------------------------------------------------- */
75
76 static u8 PAT[] = {
77         0xc2, // i2c register
78         0x00, // table number for encoder
79
80         0x47, // sync
81         0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0)
82         0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
83
84         0x00, // PSI pointer to start of table
85
86         0x00, // tid(0)
87         0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
88
89         0x00, 0x01, // transport_stream_id(1)
90
91         0xc1, // version_number(0), current_next_indicator(1)
92
93         0x00, 0x00, // section_number(0), last_section_number(0)
94
95         0x00, 0x01, // program_number(1)
96
97         0xe0, 0x00, // PMT PID
98
99         0x00, 0x00, 0x00, 0x00 // CRC32
100 };
101
102 static u8 PMT[] = {
103         0xc2, // i2c register
104         0x01, // table number for encoder
105
106         0x47, // sync
107         0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid
108         0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
109
110         0x00, // PSI pointer to start of table
111
112         0x02, // tid(2)
113         0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
114
115         0x00, 0x01, // program_number(1)
116
117         0xc1, // version_number(0), current_next_indicator(1)
118
119         0x00, 0x00, // section_number(0), last_section_number(0)
120
121         0xe0, 0x00, // PCR_PID
122
123         0xf0, 0x00, // program_info_length(0)
124
125         0x02, 0xe0, 0x00, 0xf0, 0x00, // video stream type(2), pid
126         0x04, 0xe0, 0x00, 0xf0, 0x00, // audio stream type(4), pid
127
128         0x00, 0x00, 0x00, 0x00 // CRC32
129 };
130
131 static struct v4l2_mpeg_compression param_defaults =
132 {
133         .st_type         = V4L2_MPEG_TS_2,
134         .st_bitrate      = {
135                 .mode    = V4L2_BITRATE_CBR,
136                 .target  = 7000,
137         },
138
139         .ts_pid_pmt      = 16,
140         .ts_pid_video    = 260,
141         .ts_pid_audio    = 256,
142         .ts_pid_pcr      = 259,
143
144         .vi_type         = V4L2_MPEG_VI_2,
145         .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
146         .vi_bitrate      = {
147                 .mode    = V4L2_BITRATE_VBR,
148                 .target  = 4000,
149                 .max     = 6000,
150         },
151
152         .au_type         = V4L2_MPEG_AU_2_II,
153         .au_bitrate      = {
154                 .mode    = V4L2_BITRATE_CBR,
155                 .target  = 256,
156         },
157
158 };
159
160 /* ---------------------------------------------------------------------- */
161
162 static int saa6752hs_chip_command(struct i2c_client* client,
163                                   enum saa6752hs_command command)
164 {
165         unsigned char buf[3];
166         unsigned long timeout;
167         int status = 0;
168
169         // execute the command
170         switch(command) {
171         case SAA6752HS_COMMAND_RESET:
172                 buf[0] = 0x00;
173                 break;
174
175         case SAA6752HS_COMMAND_STOP:
176                 buf[0] = 0x03;
177                 break;
178
179         case SAA6752HS_COMMAND_START:
180                 buf[0] = 0x02;
181                 break;
182
183         case SAA6752HS_COMMAND_PAUSE:
184                 buf[0] = 0x04;
185                 break;
186
187         case SAA6752HS_COMMAND_RECONFIGURE:
188                 buf[0] = 0x05;
189                 break;
190
191         case SAA6752HS_COMMAND_SLEEP:
192                 buf[0] = 0x06;
193                 break;
194
195         case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
196                 buf[0] = 0x07;
197                 break;
198
199         default:
200                 return -EINVAL;
201         }
202
203         // set it and wait for it to be so
204         i2c_master_send(client, buf, 1);
205         timeout = jiffies + HZ * 3;
206         for (;;) {
207                 // get the current status
208                 buf[0] = 0x10;
209                 i2c_master_send(client, buf, 1);
210                 i2c_master_recv(client, buf, 1);
211
212                 if (!(buf[0] & 0x20))
213                         break;
214                 if (time_after(jiffies,timeout)) {
215                         status = -ETIMEDOUT;
216                         break;
217                 }
218
219                 // wait a bit
220                 msleep(10);
221         }
222
223         // delay a bit to let encoder settle
224         msleep(50);
225
226         // done
227         return status;
228 }
229
230
231 static int saa6752hs_set_bitrate(struct i2c_client* client,
232                                  struct v4l2_mpeg_compression* params)
233 {
234         u8 buf[3];
235
236         // set the bitrate mode
237         buf[0] = 0x71;
238         buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1;
239         i2c_master_send(client, buf, 2);
240
241         // set the video bitrate
242         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) {
243                 // set the target bitrate
244                 buf[0] = 0x80;
245                 buf[1] = params->vi_bitrate.target >> 8;
246                 buf[2] = params->vi_bitrate.target & 0xff;
247                 i2c_master_send(client, buf, 3);
248
249                 // set the max bitrate
250                 buf[0] = 0x81;
251                 buf[1] = params->vi_bitrate.max >> 8;
252                 buf[2] = params->vi_bitrate.max & 0xff;
253                 i2c_master_send(client, buf, 3);
254         } else {
255                 // set the target bitrate (no max bitrate for CBR)
256                 buf[0] = 0x81;
257                 buf[1] = params->vi_bitrate.target >> 8;
258                 buf[2] = params->vi_bitrate.target & 0xff;
259                 i2c_master_send(client, buf, 3);
260         }
261
262         // set the audio bitrate
263         buf[0] = 0x94;
264         buf[1] = (256 == params->au_bitrate.target) ? 0 : 1;
265         i2c_master_send(client, buf, 2);
266
267         // set the total bitrate
268         buf[0] = 0xb1;
269         buf[1] = params->st_bitrate.target >> 8;
270         buf[2] = params->st_bitrate.target & 0xff;
271         i2c_master_send(client, buf, 3);
272
273         // return success
274         return 0;
275 }
276
277 static void saa6752hs_set_subsampling(struct i2c_client* client,
278                                       struct v4l2_format* f)
279 {
280         struct saa6752hs_state *h = i2c_get_clientdata(client);
281         int dist_352, dist_480, dist_720;
282
283         /*
284           FIXME: translate and round width/height into EMPRESS
285           subsample type:
286
287           type   |   PAL   |  NTSC
288           ---------------------------
289           SIF    | 352x288 | 352x240
290           1/2 D1 | 352x576 | 352x480
291           2/3 D1 | 480x576 | 480x480
292           D1     | 720x576 | 720x480
293         */
294
295         dist_352 = abs(f->fmt.pix.width - 352);
296         dist_480 = abs(f->fmt.pix.width - 480);
297         dist_720 = abs(f->fmt.pix.width - 720);
298         if (dist_720 < dist_480) {
299                 f->fmt.pix.width = 720;
300                 f->fmt.pix.height = 576;
301                 h->video_format = SAA6752HS_VF_D1;
302         }
303         else if (dist_480 < dist_352) {
304                 f->fmt.pix.width = 480;
305                 f->fmt.pix.height = 576;
306                 h->video_format = SAA6752HS_VF_2_3_D1;
307         }
308         else {
309                 f->fmt.pix.width = 352;
310                 if (abs(f->fmt.pix.height - 576) <
311                     abs(f->fmt.pix.height - 288)) {
312                         f->fmt.pix.height = 576;
313                         h->video_format = SAA6752HS_VF_1_2_D1;
314                 }
315                 else {
316                         f->fmt.pix.height = 288;
317                         h->video_format = SAA6752HS_VF_SIF;
318                 }
319         }
320 }
321
322
323 static void saa6752hs_set_params(struct i2c_client* client,
324                                  struct v4l2_mpeg_compression* params)
325 {
326         struct saa6752hs_state *h = i2c_get_clientdata(client);
327
328         /* check PIDs */
329         if (params->ts_pid_pmt <= MPEG_PID_MAX)
330                 h->params.ts_pid_pmt = params->ts_pid_pmt;
331         if (params->ts_pid_pcr <= MPEG_PID_MAX)
332                 h->params.ts_pid_pcr = params->ts_pid_pcr;
333         if (params->ts_pid_video <= MPEG_PID_MAX)
334                 h->params.ts_pid_video = params->ts_pid_video;
335         if (params->ts_pid_audio <= MPEG_PID_MAX)
336                 h->params.ts_pid_audio = params->ts_pid_audio;
337
338         /* check bitrate parameters */
339         if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
340             (params->vi_bitrate.mode == V4L2_BITRATE_VBR))
341                 h->params.vi_bitrate.mode = params->vi_bitrate.mode;
342         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
343                 h->params.st_bitrate.target = params->st_bitrate.target;
344         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
345                 h->params.vi_bitrate.target = params->vi_bitrate.target;
346         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
347                 h->params.vi_bitrate.max = params->vi_bitrate.max;
348         if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
349                 h->params.au_bitrate.target = params->au_bitrate.target;
350
351         /* aspect ratio */
352         if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
353             params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9)
354                 h->params.vi_aspect_ratio = params->vi_aspect_ratio;
355
356         /* range checks */
357         if (h->params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
358                 h->params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
359         if (h->params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
360                 h->params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
361         if (h->params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
362                 h->params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
363         if (h->params.au_bitrate.target <= 256)
364                 h->params.au_bitrate.target = 256;
365         else
366                 h->params.au_bitrate.target = 384;
367 }
368
369 static int saa6752hs_init(struct i2c_client* client)
370 {
371         unsigned char buf[9], buf2[4];
372         struct saa6752hs_state *h;
373         u32 crc;
374         unsigned char localPAT[256];
375         unsigned char localPMT[256];
376
377         h = i2c_get_clientdata(client);
378
379         // Set video format - must be done first as it resets other settings
380         buf[0] = 0x41;
381         buf[1] = h->video_format;
382         i2c_master_send(client, buf, 2);
383
384         // set bitrate
385         saa6752hs_set_bitrate(client, &h->params);
386
387         // Set GOP structure {3, 13}
388         buf[0] = 0x72;
389         buf[1] = 0x03;
390         buf[2] = 0x0D;
391         i2c_master_send(client,buf,3);
392
393         // Set minimum Q-scale {4}
394         buf[0] = 0x82;
395         buf[1] = 0x04;
396         i2c_master_send(client,buf,2);
397
398         // Set maximum Q-scale {12}
399         buf[0] = 0x83;
400         buf[1] = 0x0C;
401         i2c_master_send(client,buf,2);
402
403         // Set Output Protocol
404         buf[0] = 0xD0;
405         buf[1] = 0x81;
406         i2c_master_send(client,buf,2);
407
408         // Set video output stream format {TS}
409         buf[0] = 0xB0;
410         buf[1] = 0x05;
411         i2c_master_send(client,buf,2);
412
413         /* compute PAT */
414         memcpy(localPAT, PAT, sizeof(PAT));
415         localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
416         localPAT[18] = h->params.ts_pid_pmt & 0xff;
417         crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
418         localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
419         localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
420         localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
421         localPAT[sizeof(PAT) - 1] = crc & 0xFF;
422
423         /* compute PMT */
424         memcpy(localPMT, PMT, sizeof(PMT));
425         localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
426         localPMT[4] = h->params.ts_pid_pmt & 0xff;
427         localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
428         localPMT[16] = h->params.ts_pid_pcr & 0xFF;
429         localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
430         localPMT[21] = h->params.ts_pid_video & 0xFF;
431         localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
432         localPMT[26] = h->params.ts_pid_audio & 0xFF;
433         crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
434         localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
435         localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
436         localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
437         localPMT[sizeof(PMT) - 1] = crc & 0xFF;
438
439         // Set Audio PID
440         buf[0] = 0xC1;
441         buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
442         buf[2] = h->params.ts_pid_audio & 0xFF;
443         i2c_master_send(client,buf,3);
444
445         // Set Video PID
446         buf[0] = 0xC0;
447         buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
448         buf[2] = h->params.ts_pid_video & 0xFF;
449         i2c_master_send(client,buf,3);
450
451         // Set PCR PID
452         buf[0] = 0xC4;
453         buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
454         buf[2] = h->params.ts_pid_pcr & 0xFF;
455         i2c_master_send(client,buf,3);
456
457         // Send SI tables
458         i2c_master_send(client,localPAT,sizeof(PAT));
459         i2c_master_send(client,localPMT,sizeof(PMT));
460
461         // mute then unmute audio. This removes buzzing artefacts
462         buf[0] = 0xa4;
463         buf[1] = 1;
464         i2c_master_send(client, buf, 2);
465         buf[1] = 0;
466         i2c_master_send(client, buf, 2);
467
468         // start it going
469         saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
470
471         // readout current state
472         buf[0] = 0xE1;
473         buf[1] = 0xA7;
474         buf[2] = 0xFE;
475         buf[3] = 0x82;
476         buf[4] = 0xB0;
477         i2c_master_send(client, buf, 5);
478         i2c_master_recv(client, buf2, 4);
479
480         // change aspect ratio
481         buf[0] = 0xE0;
482         buf[1] = 0xA7;
483         buf[2] = 0xFE;
484         buf[3] = 0x82;
485         buf[4] = 0xB0;
486         buf[5] = buf2[0];
487         switch(h->params.vi_aspect_ratio) {
488         case V4L2_MPEG_ASPECT_16_9:
489                 buf[6] = buf2[1] | 0x40;
490                 break;
491         case V4L2_MPEG_ASPECT_4_3:
492         default:
493                 buf[6] = buf2[1] & 0xBF;
494                 break;
495                 break;
496         }
497         buf[7] = buf2[2];
498         buf[8] = buf2[3];
499         i2c_master_send(client, buf, 9);
500
501         // return success
502         return 0;
503 }
504
505 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
506 {
507         struct saa6752hs_state *h;
508
509         printk("saa6752hs: chip found @ 0x%x\n", addr<<1);
510
511         if (NULL == (h = kmalloc(sizeof(*h), GFP_KERNEL)))
512                 return -ENOMEM;
513         memset(h,0,sizeof(*h));
514         h->client = client_template;
515         h->params = param_defaults;
516         h->client.adapter = adap;
517         h->client.addr = addr;
518
519         i2c_set_clientdata(&h->client, h);
520         i2c_attach_client(&h->client);
521         return 0;
522 }
523
524 static int saa6752hs_probe(struct i2c_adapter *adap)
525 {
526         if (adap->class & I2C_CLASS_TV_ANALOG)
527                 return i2c_probe(adap, &addr_data, saa6752hs_attach);
528         return 0;
529 }
530
531 static int saa6752hs_detach(struct i2c_client *client)
532 {
533         struct saa6752hs_state *h;
534
535         h = i2c_get_clientdata(client);
536         i2c_detach_client(client);
537         kfree(h);
538         return 0;
539 }
540
541 static int
542 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
543 {
544         struct saa6752hs_state *h = i2c_get_clientdata(client);
545         struct v4l2_mpeg_compression *params = arg;
546         int err = 0;
547
548         switch (cmd) {
549         case VIDIOC_S_MPEGCOMP:
550                 if (NULL == params) {
551                         /* apply settings and start encoder */
552                         saa6752hs_init(client);
553                         break;
554                 }
555                 saa6752hs_set_params(client, params);
556                 /* fall through */
557         case VIDIOC_G_MPEGCOMP:
558                 *params = h->params;
559                 break;
560         case VIDIOC_G_FMT:
561         {
562            struct v4l2_format *f = arg;
563
564            if (h->video_format == SAA6752HS_VF_UNKNOWN)
565                    h->video_format = SAA6752HS_VF_D1;
566            f->fmt.pix.width =
567                    v4l2_format_table[h->video_format].fmt.pix.width;
568            f->fmt.pix.height =
569                    v4l2_format_table[h->video_format].fmt.pix.height;
570            break ;
571         }
572         case VIDIOC_S_FMT:
573         {
574                 struct v4l2_format *f = arg;
575
576                 saa6752hs_set_subsampling(client, f);
577                 break;
578         }
579         default:
580                 /* nothing */
581                 break;
582         }
583
584         return err;
585 }
586
587 /* ----------------------------------------------------------------------- */
588
589 static struct i2c_driver driver = {
590         .owner          = THIS_MODULE,
591         .name           = "i2c saa6752hs MPEG encoder",
592         .id             = I2C_DRIVERID_SAA6752HS,
593         .flags          = I2C_DF_NOTIFY,
594         .attach_adapter = saa6752hs_probe,
595         .detach_client  = saa6752hs_detach,
596         .command        = saa6752hs_command,
597 };
598
599 static struct i2c_client client_template =
600 {
601         I2C_DEVNAME("saa6752hs"),
602         .flags      = I2C_CLIENT_ALLOW_USE,
603         .driver     = &driver,
604 };
605
606 static int __init saa6752hs_init_module(void)
607 {
608         return i2c_add_driver(&driver);
609 }
610
611 static void __exit saa6752hs_cleanup_module(void)
612 {
613         i2c_del_driver(&driver);
614 }
615
616 module_init(saa6752hs_init_module);
617 module_exit(saa6752hs_cleanup_module);
618
619 /*
620  * Overrides for Emacs so that we follow Linus's tabbing style.
621  * ---------------------------------------------------------------------------
622  * Local variables:
623  * c-basic-offset: 8
624  * End:
625  */