[PATCH] v4l: fix I2C detect after normal_i2c_range()
[linux-2.6.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 #if 0
159         /* FIXME: size? via S_FMT? */
160         .video_format = MPEG_VIDEO_FORMAT_D1,
161 #endif
162 };
163
164 /* ---------------------------------------------------------------------- */
165
166 static int saa6752hs_chip_command(struct i2c_client* client,
167                                   enum saa6752hs_command command)
168 {
169         unsigned char buf[3];
170         unsigned long timeout;
171         int status = 0;
172
173         // execute the command
174         switch(command) {
175         case SAA6752HS_COMMAND_RESET:
176                 buf[0] = 0x00;
177                 break;
178
179         case SAA6752HS_COMMAND_STOP:
180                 buf[0] = 0x03;
181                 break;
182
183         case SAA6752HS_COMMAND_START:
184                 buf[0] = 0x02;
185                 break;
186
187         case SAA6752HS_COMMAND_PAUSE:
188                 buf[0] = 0x04;
189                 break;
190
191         case SAA6752HS_COMMAND_RECONFIGURE:
192                 buf[0] = 0x05;
193                 break;
194
195         case SAA6752HS_COMMAND_SLEEP:
196                 buf[0] = 0x06;
197                 break;
198
199         case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
200                 buf[0] = 0x07;
201                 break;
202
203         default:
204                 return -EINVAL;
205         }
206
207         // set it and wait for it to be so
208         i2c_master_send(client, buf, 1);
209         timeout = jiffies + HZ * 3;
210         for (;;) {
211                 // get the current status
212                 buf[0] = 0x10;
213                 i2c_master_send(client, buf, 1);
214                 i2c_master_recv(client, buf, 1);
215
216                 if (!(buf[0] & 0x20))
217                         break;
218                 if (time_after(jiffies,timeout)) {
219                         status = -ETIMEDOUT;
220                         break;
221                 }
222
223                 // wait a bit
224                 msleep(10);
225         }
226
227         // delay a bit to let encoder settle
228         msleep(50);
229
230         // done
231         return status;
232 }
233
234
235 static int saa6752hs_set_bitrate(struct i2c_client* client,
236                                  struct v4l2_mpeg_compression* params)
237 {
238         u8 buf[3];
239
240         // set the bitrate mode
241         buf[0] = 0x71;
242         buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1;
243         i2c_master_send(client, buf, 2);
244
245         // set the video bitrate
246         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) {
247                 // set the target bitrate
248                 buf[0] = 0x80;
249                 buf[1] = params->vi_bitrate.target >> 8;
250                 buf[2] = params->vi_bitrate.target & 0xff;
251                 i2c_master_send(client, buf, 3);
252
253                 // set the max bitrate
254                 buf[0] = 0x81;
255                 buf[1] = params->vi_bitrate.max >> 8;
256                 buf[2] = params->vi_bitrate.max & 0xff;
257                 i2c_master_send(client, buf, 3);
258         } else {
259                 // set the target bitrate (no max bitrate for CBR)
260                 buf[0] = 0x81;
261                 buf[1] = params->vi_bitrate.target >> 8;
262                 buf[2] = params->vi_bitrate.target & 0xff;
263                 i2c_master_send(client, buf, 3);
264         }
265
266         // set the audio bitrate
267         buf[0] = 0x94;
268         buf[1] = (256 == params->au_bitrate.target) ? 0 : 1;
269         i2c_master_send(client, buf, 2);
270
271         // set the total bitrate
272         buf[0] = 0xb1;
273         buf[1] = params->st_bitrate.target >> 8;
274         buf[2] = params->st_bitrate.target & 0xff;
275         i2c_master_send(client, buf, 3);
276
277         // return success
278         return 0;
279 }
280
281 static void saa6752hs_set_subsampling(struct i2c_client* client,
282                                       struct v4l2_format* f)
283 {
284         struct saa6752hs_state *h = i2c_get_clientdata(client);
285         int dist_352, dist_480, dist_720;
286
287         /*
288           FIXME: translate and round width/height into EMPRESS
289           subsample type:
290
291           type   |   PAL   |  NTSC
292           ---------------------------
293           SIF    | 352x288 | 352x240
294           1/2 D1 | 352x576 | 352x480
295           2/3 D1 | 480x576 | 480x480
296           D1     | 720x576 | 720x480
297         */
298
299         dist_352 = abs(f->fmt.pix.width - 352);
300         dist_480 = abs(f->fmt.pix.width - 480);
301         dist_720 = abs(f->fmt.pix.width - 720);
302         if (dist_720 < dist_480) {
303                 f->fmt.pix.width = 720;
304                 f->fmt.pix.height = 576;
305                 h->video_format = SAA6752HS_VF_D1;
306         }
307         else if (dist_480 < dist_352) {
308                 f->fmt.pix.width = 480;
309                 f->fmt.pix.height = 576;
310                 h->video_format = SAA6752HS_VF_2_3_D1;
311         }
312         else {
313                 f->fmt.pix.width = 352;
314                 if (abs(f->fmt.pix.height - 576) <
315                     abs(f->fmt.pix.height - 288)) {
316                         f->fmt.pix.height = 576;
317                         h->video_format = SAA6752HS_VF_1_2_D1;
318                 }
319                 else {
320                         f->fmt.pix.height = 288;
321                         h->video_format = SAA6752HS_VF_SIF;
322                 }
323         }
324 }
325
326
327 static void saa6752hs_set_params(struct i2c_client* client,
328                                  struct v4l2_mpeg_compression* params)
329 {
330         struct saa6752hs_state *h = i2c_get_clientdata(client);
331
332         /* check PIDs */
333         if (params->ts_pid_pmt <= MPEG_PID_MAX)
334                 h->params.ts_pid_pmt = params->ts_pid_pmt;
335         if (params->ts_pid_pcr <= MPEG_PID_MAX)
336                 h->params.ts_pid_pcr = params->ts_pid_pcr;
337         if (params->ts_pid_video <= MPEG_PID_MAX)
338                 h->params.ts_pid_video = params->ts_pid_video;
339         if (params->ts_pid_audio <= MPEG_PID_MAX)
340                 h->params.ts_pid_audio = params->ts_pid_audio;
341
342         /* check bitrate parameters */
343         if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
344             (params->vi_bitrate.mode == V4L2_BITRATE_VBR))
345                 h->params.vi_bitrate.mode = params->vi_bitrate.mode;
346         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
347                 h->params.st_bitrate.target = params->st_bitrate.target;
348         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
349                 h->params.vi_bitrate.target = params->vi_bitrate.target;
350         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
351                 h->params.vi_bitrate.max = params->vi_bitrate.max;
352         if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
353                 h->params.au_bitrate.target = params->au_bitrate.target;
354
355         /* aspect ratio */
356         if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
357             params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9)
358                 h->params.vi_aspect_ratio = params->vi_aspect_ratio;
359
360         /* range checks */
361         if (h->params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
362                 h->params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
363         if (h->params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
364                 h->params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
365         if (h->params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
366                 h->params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
367         if (h->params.au_bitrate.target <= 256)
368                 h->params.au_bitrate.target = 256;
369         else
370                 h->params.au_bitrate.target = 384;
371 }
372
373 static int saa6752hs_init(struct i2c_client* client)
374 {
375         unsigned char buf[9], buf2[4];
376         struct saa6752hs_state *h;
377         u32 crc;
378         unsigned char localPAT[256];
379         unsigned char localPMT[256];
380
381         h = i2c_get_clientdata(client);
382
383         // Set video format - must be done first as it resets other settings
384         buf[0] = 0x41;
385         buf[1] = h->video_format;
386         i2c_master_send(client, buf, 2);
387
388         // set bitrate
389         saa6752hs_set_bitrate(client, &h->params);
390
391         // Set GOP structure {3, 13}
392         buf[0] = 0x72;
393         buf[1] = 0x03;
394         buf[2] = 0x0D;
395         i2c_master_send(client,buf,3);
396
397         // Set minimum Q-scale {4}
398         buf[0] = 0x82;
399         buf[1] = 0x04;
400         i2c_master_send(client,buf,2);
401
402         // Set maximum Q-scale {12}
403         buf[0] = 0x83;
404         buf[1] = 0x0C;
405         i2c_master_send(client,buf,2);
406
407         // Set Output Protocol
408         buf[0] = 0xD0;
409         buf[1] = 0x81;
410         i2c_master_send(client,buf,2);
411
412         // Set video output stream format {TS}
413         buf[0] = 0xB0;
414         buf[1] = 0x05;
415         i2c_master_send(client,buf,2);
416
417         /* compute PAT */
418         memcpy(localPAT, PAT, sizeof(PAT));
419         localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
420         localPAT[18] = h->params.ts_pid_pmt & 0xff;
421         crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
422         localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
423         localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
424         localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
425         localPAT[sizeof(PAT) - 1] = crc & 0xFF;
426
427         /* compute PMT */
428         memcpy(localPMT, PMT, sizeof(PMT));
429         localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
430         localPMT[4] = h->params.ts_pid_pmt & 0xff;
431         localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
432         localPMT[16] = h->params.ts_pid_pcr & 0xFF;
433         localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
434         localPMT[21] = h->params.ts_pid_video & 0xFF;
435         localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
436         localPMT[26] = h->params.ts_pid_audio & 0xFF;
437         crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
438         localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
439         localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
440         localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
441         localPMT[sizeof(PMT) - 1] = crc & 0xFF;
442
443         // Set Audio PID
444         buf[0] = 0xC1;
445         buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
446         buf[2] = h->params.ts_pid_audio & 0xFF;
447         i2c_master_send(client,buf,3);
448
449         // Set Video PID
450         buf[0] = 0xC0;
451         buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
452         buf[2] = h->params.ts_pid_video & 0xFF;
453         i2c_master_send(client,buf,3);
454
455         // Set PCR PID
456         buf[0] = 0xC4;
457         buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
458         buf[2] = h->params.ts_pid_pcr & 0xFF;
459         i2c_master_send(client,buf,3);
460
461         // Send SI tables
462         i2c_master_send(client,localPAT,sizeof(PAT));
463         i2c_master_send(client,localPMT,sizeof(PMT));
464
465         // mute then unmute audio. This removes buzzing artefacts
466         buf[0] = 0xa4;
467         buf[1] = 1;
468         i2c_master_send(client, buf, 2);
469         buf[1] = 0;
470         i2c_master_send(client, buf, 2);
471
472         // start it going
473         saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
474
475         // readout current state
476         buf[0] = 0xE1;
477         buf[1] = 0xA7;
478         buf[2] = 0xFE;
479         buf[3] = 0x82;
480         buf[4] = 0xB0;
481         i2c_master_send(client, buf, 5);
482         i2c_master_recv(client, buf2, 4);
483
484         // change aspect ratio
485         buf[0] = 0xE0;
486         buf[1] = 0xA7;
487         buf[2] = 0xFE;
488         buf[3] = 0x82;
489         buf[4] = 0xB0;
490         buf[5] = buf2[0];
491         switch(h->params.vi_aspect_ratio) {
492         case V4L2_MPEG_ASPECT_16_9:
493                 buf[6] = buf2[1] | 0x40;
494                 break;
495         case V4L2_MPEG_ASPECT_4_3:
496         default:
497                 buf[6] = buf2[1] & 0xBF;
498                 break;
499                 break;
500         }
501         buf[7] = buf2[2];
502         buf[8] = buf2[3];
503         i2c_master_send(client, buf, 9);
504
505         // return success
506         return 0;
507 }
508
509 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
510 {
511         struct saa6752hs_state *h;
512
513         printk("saa6752hs: chip found @ 0x%x\n", addr<<1);
514
515         if (NULL == (h = kmalloc(sizeof(*h), GFP_KERNEL)))
516                 return -ENOMEM;
517         memset(h,0,sizeof(*h));
518         h->client = client_template;
519         h->params = param_defaults;
520         h->client.adapter = adap;
521         h->client.addr = addr;
522
523         i2c_set_clientdata(&h->client, h);
524         i2c_attach_client(&h->client);
525         return 0;
526 }
527
528 static int saa6752hs_probe(struct i2c_adapter *adap)
529 {
530         if (adap->class & I2C_CLASS_TV_ANALOG)
531                 return i2c_probe(adap, &addr_data, saa6752hs_attach);
532         return 0;
533 }
534
535 static int saa6752hs_detach(struct i2c_client *client)
536 {
537         struct saa6752hs_state *h;
538
539         h = i2c_get_clientdata(client);
540         i2c_detach_client(client);
541         kfree(h);
542         return 0;
543 }
544
545 static int
546 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
547 {
548         struct saa6752hs_state *h = i2c_get_clientdata(client);
549         struct v4l2_mpeg_compression *params = arg;
550         int err = 0;
551
552         switch (cmd) {
553         case VIDIOC_S_MPEGCOMP:
554                 if (NULL == params) {
555                         /* apply settings and start encoder */
556                         saa6752hs_init(client);
557                         break;
558                 }
559                 saa6752hs_set_params(client, params);
560                 /* fall through */
561         case VIDIOC_G_MPEGCOMP:
562                 *params = h->params;
563                 break;
564         case VIDIOC_G_FMT:
565         {
566            struct v4l2_format *f = arg;
567
568            if (h->video_format == SAA6752HS_VF_UNKNOWN)
569                    h->video_format = SAA6752HS_VF_D1;
570            f->fmt.pix.width =
571                    v4l2_format_table[h->video_format].fmt.pix.width;
572            f->fmt.pix.height =
573                    v4l2_format_table[h->video_format].fmt.pix.height;
574            break ;
575         }
576         case VIDIOC_S_FMT:
577         {
578                 struct v4l2_format *f = arg;
579
580                 saa6752hs_set_subsampling(client, f);
581                 break;
582         }
583         default:
584                 /* nothing */
585                 break;
586         }
587
588         return err;
589 }
590
591 /* ----------------------------------------------------------------------- */
592
593 static struct i2c_driver driver = {
594         .owner          = THIS_MODULE,
595         .name           = "i2c saa6752hs MPEG encoder",
596         .id             = I2C_DRIVERID_SAA6752HS,
597         .flags          = I2C_DF_NOTIFY,
598         .attach_adapter = saa6752hs_probe,
599         .detach_client  = saa6752hs_detach,
600         .command        = saa6752hs_command,
601 };
602
603 static struct i2c_client client_template =
604 {
605         I2C_DEVNAME("saa6752hs"),
606         .flags      = I2C_CLIENT_ALLOW_USE,
607         .driver     = &driver,
608 };
609
610 static int __init saa6752hs_init_module(void)
611 {
612         return i2c_add_driver(&driver);
613 }
614
615 static void __exit saa6752hs_cleanup_module(void)
616 {
617         i2c_del_driver(&driver);
618 }
619
620 module_init(saa6752hs_init_module);
621 module_exit(saa6752hs_cleanup_module);
622
623 /*
624  * Overrides for Emacs so that we follow Linus's tabbing style.
625  * ---------------------------------------------------------------------------
626  * Local variables:
627  * c-basic-offset: 8
628  * End:
629  */