]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/saa7134/saa6752hs.c
e8c65ddd0181f5663059e73b45b1e0593a5c451f
[linux-2.6-omap-h63xx.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/videodev2.h>
13 #include <media/v4l2-common.h>
14 #include <linux/init.h>
15 #include <linux/crc32.h>
16
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 struct saa6752hs_mpeg_params {
43         /* transport streams */
44         __u16                           ts_pid_pmt;
45         __u16                           ts_pid_audio;
46         __u16                           ts_pid_video;
47         __u16                           ts_pid_pcr;
48
49         /* audio */
50         enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
51
52         /* video */
53         enum v4l2_mpeg_video_aspect     vi_aspect;
54         enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
55         __u32                           vi_bitrate;
56         __u32                           vi_bitrate_peak;
57 };
58
59 static const struct v4l2_format v4l2_format_table[] =
60 {
61         [SAA6752HS_VF_D1] =
62                 { .fmt = { .pix = { .width = 720, .height = 576 }}},
63         [SAA6752HS_VF_2_3_D1] =
64                 { .fmt = { .pix = { .width = 480, .height = 576 }}},
65         [SAA6752HS_VF_1_2_D1] =
66                 { .fmt = { .pix = { .width = 352, .height = 576 }}},
67         [SAA6752HS_VF_SIF] =
68                 { .fmt = { .pix = { .width = 352, .height = 288 }}},
69         [SAA6752HS_VF_UNKNOWN] =
70                 { .fmt = { .pix = { .width = 0, .height = 0}}},
71 };
72
73 struct saa6752hs_state {
74         struct i2c_client             client;
75         struct v4l2_mpeg_compression  old_params;
76         struct saa6752hs_mpeg_params  params;
77         enum saa6752hs_videoformat    video_format;
78         v4l2_std_id                   standard;
79 };
80
81 enum saa6752hs_command {
82         SAA6752HS_COMMAND_RESET = 0,
83         SAA6752HS_COMMAND_STOP = 1,
84         SAA6752HS_COMMAND_START = 2,
85         SAA6752HS_COMMAND_PAUSE = 3,
86         SAA6752HS_COMMAND_RECONFIGURE = 4,
87         SAA6752HS_COMMAND_SLEEP = 5,
88         SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
89
90         SAA6752HS_COMMAND_MAX
91 };
92
93 /* ---------------------------------------------------------------------- */
94
95 static u8 PAT[] = {
96         0xc2, /* i2c register */
97         0x00, /* table number for encoder */
98
99         0x47, /* sync */
100         0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
101         0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
102
103         0x00, /* PSI pointer to start of table */
104
105         0x00, /* tid(0) */
106         0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
107
108         0x00, 0x01, /* transport_stream_id(1) */
109
110         0xc1, /* version_number(0), current_next_indicator(1) */
111
112         0x00, 0x00, /* section_number(0), last_section_number(0) */
113
114         0x00, 0x01, /* program_number(1) */
115
116         0xe0, 0x00, /* PMT PID */
117
118         0x00, 0x00, 0x00, 0x00 /* CRC32 */
119 };
120
121 static u8 PMT[] = {
122         0xc2, /* i2c register */
123         0x01, /* table number for encoder */
124
125         0x47, /* sync */
126         0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
127         0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
128
129         0x00, /* PSI pointer to start of table */
130
131         0x02, /* tid(2) */
132         0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
133
134         0x00, 0x01, /* program_number(1) */
135
136         0xc1, /* version_number(0), current_next_indicator(1) */
137
138         0x00, 0x00, /* section_number(0), last_section_number(0) */
139
140         0xe0, 0x00, /* PCR_PID */
141
142         0xf0, 0x00, /* program_info_length(0) */
143
144         0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
145         0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
146
147         0x00, 0x00, 0x00, 0x00 /* CRC32 */
148 };
149
150 static struct saa6752hs_mpeg_params param_defaults =
151 {
152         .ts_pid_pmt      = 16,
153         .ts_pid_video    = 260,
154         .ts_pid_audio    = 256,
155         .ts_pid_pcr      = 259,
156
157         .vi_aspect       = V4L2_MPEG_VIDEO_ASPECT_4x3,
158         .vi_bitrate      = 4000,
159         .vi_bitrate_peak = 6000,
160         .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
161
162         .au_l2_bitrate   = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
163 };
164
165 static struct v4l2_mpeg_compression old_param_defaults =
166 {
167         .st_type         = V4L2_MPEG_TS_2,
168         .st_bitrate      = {
169                 .mode    = V4L2_BITRATE_CBR,
170                 .target  = 7000,
171         },
172
173         .ts_pid_pmt      = 16,
174         .ts_pid_video    = 260,
175         .ts_pid_audio    = 256,
176         .ts_pid_pcr      = 259,
177
178         .vi_type         = V4L2_MPEG_VI_2,
179         .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
180         .vi_bitrate      = {
181                 .mode    = V4L2_BITRATE_VBR,
182                 .target  = 4000,
183                 .max     = 6000,
184         },
185
186         .au_type         = V4L2_MPEG_AU_2_II,
187         .au_bitrate      = {
188                 .mode    = V4L2_BITRATE_CBR,
189                 .target  = 256,
190         },
191
192 };
193
194 /* ---------------------------------------------------------------------- */
195
196 static int saa6752hs_chip_command(struct i2c_client* client,
197                                   enum saa6752hs_command command)
198 {
199         unsigned char buf[3];
200         unsigned long timeout;
201         int status = 0;
202
203         /* execute the command */
204         switch(command) {
205         case SAA6752HS_COMMAND_RESET:
206                 buf[0] = 0x00;
207                 break;
208
209         case SAA6752HS_COMMAND_STOP:
210                 buf[0] = 0x03;
211                 break;
212
213         case SAA6752HS_COMMAND_START:
214                 buf[0] = 0x02;
215                 break;
216
217         case SAA6752HS_COMMAND_PAUSE:
218                 buf[0] = 0x04;
219                 break;
220
221         case SAA6752HS_COMMAND_RECONFIGURE:
222                 buf[0] = 0x05;
223                 break;
224
225         case SAA6752HS_COMMAND_SLEEP:
226                 buf[0] = 0x06;
227                 break;
228
229         case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
230                 buf[0] = 0x07;
231                 break;
232
233         default:
234                 return -EINVAL;
235         }
236
237         /* set it and wait for it to be so */
238         i2c_master_send(client, buf, 1);
239         timeout = jiffies + HZ * 3;
240         for (;;) {
241                 /* get the current status */
242                 buf[0] = 0x10;
243                 i2c_master_send(client, buf, 1);
244                 i2c_master_recv(client, buf, 1);
245
246                 if (!(buf[0] & 0x20))
247                         break;
248                 if (time_after(jiffies,timeout)) {
249                         status = -ETIMEDOUT;
250                         break;
251                 }
252
253                 msleep(10);
254         }
255
256         /* delay a bit to let encoder settle */
257         msleep(50);
258
259         return status;
260 }
261
262
263 static int saa6752hs_set_bitrate(struct i2c_client* client,
264                                  struct v4l2_mpeg_compression* params)
265 {
266         u8 buf[3];
267
268         /* set the bitrate mode */
269         buf[0] = 0x71;
270         buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1;
271         i2c_master_send(client, buf, 2);
272
273         /* set the video bitrate */
274         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) {
275                 /* set the target bitrate */
276                 buf[0] = 0x80;
277                 buf[1] = params->vi_bitrate.target >> 8;
278                 buf[2] = params->vi_bitrate.target & 0xff;
279                 i2c_master_send(client, buf, 3);
280
281                 /* set the max bitrate */
282                 buf[0] = 0x81;
283                 buf[1] = params->vi_bitrate.max >> 8;
284                 buf[2] = params->vi_bitrate.max & 0xff;
285                 i2c_master_send(client, buf, 3);
286         } else {
287                 /* set the target bitrate (no max bitrate for CBR) */
288                 buf[0] = 0x81;
289                 buf[1] = params->vi_bitrate.target >> 8;
290                 buf[2] = params->vi_bitrate.target & 0xff;
291                 i2c_master_send(client, buf, 3);
292         }
293
294         /* set the audio bitrate */
295         buf[0] = 0x94;
296         buf[1] = (256 == params->au_bitrate.target) ? 0 : 1;
297         i2c_master_send(client, buf, 2);
298
299         /* set the total bitrate */
300         buf[0] = 0xb1;
301         buf[1] = params->st_bitrate.target >> 8;
302         buf[2] = params->st_bitrate.target & 0xff;
303         i2c_master_send(client, buf, 3);
304
305         return 0;
306 }
307
308 static void saa6752hs_set_subsampling(struct i2c_client* client,
309                                       struct v4l2_format* f)
310 {
311         struct saa6752hs_state *h = i2c_get_clientdata(client);
312         int dist_352, dist_480, dist_720;
313
314         /*
315           FIXME: translate and round width/height into EMPRESS
316           subsample type:
317
318           type   |   PAL   |  NTSC
319           ---------------------------
320           SIF    | 352x288 | 352x240
321           1/2 D1 | 352x576 | 352x480
322           2/3 D1 | 480x576 | 480x480
323           D1     | 720x576 | 720x480
324         */
325
326         dist_352 = abs(f->fmt.pix.width - 352);
327         dist_480 = abs(f->fmt.pix.width - 480);
328         dist_720 = abs(f->fmt.pix.width - 720);
329         if (dist_720 < dist_480) {
330                 f->fmt.pix.width = 720;
331                 f->fmt.pix.height = 576;
332                 h->video_format = SAA6752HS_VF_D1;
333         }
334         else if (dist_480 < dist_352) {
335                 f->fmt.pix.width = 480;
336                 f->fmt.pix.height = 576;
337                 h->video_format = SAA6752HS_VF_2_3_D1;
338         }
339         else {
340                 f->fmt.pix.width = 352;
341                 if (abs(f->fmt.pix.height - 576) <
342                     abs(f->fmt.pix.height - 288)) {
343                         f->fmt.pix.height = 576;
344                         h->video_format = SAA6752HS_VF_1_2_D1;
345                 }
346                 else {
347                         f->fmt.pix.height = 288;
348                         h->video_format = SAA6752HS_VF_SIF;
349                 }
350         }
351 }
352
353
354 static void saa6752hs_old_set_params(struct i2c_client* client,
355                                  struct v4l2_mpeg_compression* params)
356 {
357         struct saa6752hs_state *h = i2c_get_clientdata(client);
358
359         /* check PIDs */
360         if (params->ts_pid_pmt <= MPEG_PID_MAX) {
361                 h->old_params.ts_pid_pmt = params->ts_pid_pmt;
362                 h->params.ts_pid_pmt = params->ts_pid_pmt;
363         }
364         if (params->ts_pid_pcr <= MPEG_PID_MAX) {
365                 h->old_params.ts_pid_pcr = params->ts_pid_pcr;
366                 h->params.ts_pid_pcr = params->ts_pid_pcr;
367         }
368         if (params->ts_pid_video <= MPEG_PID_MAX) {
369                 h->old_params.ts_pid_video = params->ts_pid_video;
370                 h->params.ts_pid_video = params->ts_pid_video;
371         }
372         if (params->ts_pid_audio <= MPEG_PID_MAX) {
373                 h->old_params.ts_pid_audio = params->ts_pid_audio;
374                 h->params.ts_pid_audio = params->ts_pid_audio;
375         }
376
377         /* check bitrate parameters */
378         if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
379             (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) {
380                 h->old_params.vi_bitrate.mode = params->vi_bitrate.mode;
381                 h->params.vi_bitrate_mode = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ?
382                        V4L2_MPEG_VIDEO_BITRATE_MODE_VBR : V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
383         }
384         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
385                 h->old_params.st_bitrate.target = params->st_bitrate.target;
386         if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
387                 h->old_params.vi_bitrate.target = params->vi_bitrate.target;
388         if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
389                 h->old_params.vi_bitrate.max = params->vi_bitrate.max;
390         if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
391                 h->old_params.au_bitrate.target = params->au_bitrate.target;
392
393         /* aspect ratio */
394         if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
395             params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) {
396                 h->old_params.vi_aspect_ratio = params->vi_aspect_ratio;
397                 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3)
398                         h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
399                 else
400                         h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_16x9;
401         }
402
403         /* range checks */
404         if (h->old_params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
405                 h->old_params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
406         if (h->old_params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
407                 h->old_params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
408         if (h->old_params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
409                 h->old_params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
410         h->params.vi_bitrate = params->vi_bitrate.target;
411         h->params.vi_bitrate_peak = params->vi_bitrate.max;
412         if (h->old_params.au_bitrate.target <= 256) {
413                 h->old_params.au_bitrate.target = 256;
414                 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
415         }
416         else {
417                 h->old_params.au_bitrate.target = 384;
418                 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
419         }
420 }
421
422 static int handle_ctrl(struct saa6752hs_mpeg_params *params,
423                 struct v4l2_ext_control *ctrl, int cmd)
424 {
425         int old = 0, new;
426         int set = cmd == VIDIOC_S_EXT_CTRLS;
427
428         new = ctrl->value;
429         switch (ctrl->id) {
430                 case V4L2_CID_MPEG_STREAM_TYPE:
431                         old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
432                         if (set && new != old)
433                                 return -ERANGE;
434                         new = old;
435                         break;
436                 case V4L2_CID_MPEG_STREAM_PID_PMT:
437                         old = params->ts_pid_pmt;
438                         if (set && new > MPEG_PID_MAX)
439                                 return -ERANGE;
440                         if (new > MPEG_PID_MAX)
441                                 new = MPEG_PID_MAX;
442                         params->ts_pid_pmt = new;
443                         break;
444                 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
445                         old = params->ts_pid_audio;
446                         if (set && new > MPEG_PID_MAX)
447                                 return -ERANGE;
448                         if (new > MPEG_PID_MAX)
449                                 new = MPEG_PID_MAX;
450                         params->ts_pid_audio = new;
451                         break;
452                 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
453                         old = params->ts_pid_video;
454                         if (set && new > MPEG_PID_MAX)
455                                 return -ERANGE;
456                         if (new > MPEG_PID_MAX)
457                                 new = MPEG_PID_MAX;
458                         params->ts_pid_video = new;
459                         break;
460                 case V4L2_CID_MPEG_STREAM_PID_PCR:
461                         old = params->ts_pid_pcr;
462                         if (set && new > MPEG_PID_MAX)
463                                 return -ERANGE;
464                         if (new > MPEG_PID_MAX)
465                                 new = MPEG_PID_MAX;
466                         params->ts_pid_pcr = new;
467                         break;
468                 case V4L2_CID_MPEG_AUDIO_ENCODING:
469                         old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
470                         if (set && new != old)
471                                 return -ERANGE;
472                         new = old;
473                         break;
474                 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
475                         old = params->au_l2_bitrate;
476                         if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
477                                    new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
478                                 return -ERANGE;
479                         if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
480                                 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
481                         else
482                                 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
483                         params->au_l2_bitrate = new;
484                         break;
485                 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
486                         old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
487                         if (set && new != old)
488                                 return -ERANGE;
489                         new = old;
490                         break;
491                 case V4L2_CID_MPEG_VIDEO_ENCODING:
492                         old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
493                         if (set && new != old)
494                                 return -ERANGE;
495                         new = old;
496                         break;
497                 case V4L2_CID_MPEG_VIDEO_ASPECT:
498                         old = params->vi_aspect;
499                         if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
500                                    new != V4L2_MPEG_VIDEO_ASPECT_4x3)
501                                 return -ERANGE;
502                         if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
503                                 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
504                         params->vi_aspect = new;
505                         break;
506                 case V4L2_CID_MPEG_VIDEO_BITRATE:
507                         old = params->vi_bitrate * 1000;
508                         new = 1000 * (new / 1000);
509                         if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
510                                 return -ERANGE;
511                         if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
512                                 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
513                         params->vi_bitrate = new / 1000;
514                         break;
515                 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
516                         old = params->vi_bitrate_peak * 1000;
517                         new = 1000 * (new / 1000);
518                         if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
519                                 return -ERANGE;
520                         if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
521                                 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
522                         params->vi_bitrate_peak = new / 1000;
523                         break;
524                 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
525                         old = params->vi_bitrate_mode;
526                         params->vi_bitrate_mode = new;
527                         break;
528                 default:
529                         return -EINVAL;
530         }
531         if (cmd == VIDIOC_G_EXT_CTRLS)
532                 ctrl->value = old;
533         else
534                 ctrl->value = new;
535         return 0;
536 }
537
538 static int saa6752hs_init(struct i2c_client* client)
539 {
540         unsigned char buf[9], buf2[4];
541         struct saa6752hs_state *h;
542         u32 crc;
543         unsigned char localPAT[256];
544         unsigned char localPMT[256];
545
546         h = i2c_get_clientdata(client);
547
548         /* Set video format - must be done first as it resets other settings */
549         buf[0] = 0x41;
550         buf[1] = h->video_format;
551         i2c_master_send(client, buf, 2);
552
553         /* Set number of lines in input signal */
554         buf[0] = 0x40;
555         buf[1] = 0x00;
556         if (h->standard & V4L2_STD_525_60)
557                 buf[1] = 0x01;
558         i2c_master_send(client, buf, 2);
559
560         /* set bitrate */
561         saa6752hs_set_bitrate(client, &h->params);
562
563         /* Set GOP structure {3, 13} */
564         buf[0] = 0x72;
565         buf[1] = 0x03;
566         buf[2] = 0x0D;
567         i2c_master_send(client,buf,3);
568
569         /* Set minimum Q-scale {4} */
570         buf[0] = 0x82;
571         buf[1] = 0x04;
572         i2c_master_send(client,buf,2);
573
574         /* Set maximum Q-scale {12} */
575         buf[0] = 0x83;
576         buf[1] = 0x0C;
577         i2c_master_send(client,buf,2);
578
579         /* Set Output Protocol */
580         buf[0] = 0xD0;
581         buf[1] = 0x81;
582         i2c_master_send(client,buf,2);
583
584         /* Set video output stream format {TS} */
585         buf[0] = 0xB0;
586         buf[1] = 0x05;
587         i2c_master_send(client,buf,2);
588
589         /* compute PAT */
590         memcpy(localPAT, PAT, sizeof(PAT));
591         localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
592         localPAT[18] = h->params.ts_pid_pmt & 0xff;
593         crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
594         localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
595         localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
596         localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
597         localPAT[sizeof(PAT) - 1] = crc & 0xFF;
598
599         /* compute PMT */
600         memcpy(localPMT, PMT, sizeof(PMT));
601         localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
602         localPMT[4] = h->params.ts_pid_pmt & 0xff;
603         localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
604         localPMT[16] = h->params.ts_pid_pcr & 0xFF;
605         localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
606         localPMT[21] = h->params.ts_pid_video & 0xFF;
607         localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
608         localPMT[26] = h->params.ts_pid_audio & 0xFF;
609         crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
610         localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
611         localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
612         localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
613         localPMT[sizeof(PMT) - 1] = crc & 0xFF;
614
615         /* Set Audio PID */
616         buf[0] = 0xC1;
617         buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
618         buf[2] = h->params.ts_pid_audio & 0xFF;
619         i2c_master_send(client,buf,3);
620
621         /* Set Video PID */
622         buf[0] = 0xC0;
623         buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
624         buf[2] = h->params.ts_pid_video & 0xFF;
625         i2c_master_send(client,buf,3);
626
627         /* Set PCR PID */
628         buf[0] = 0xC4;
629         buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
630         buf[2] = h->params.ts_pid_pcr & 0xFF;
631         i2c_master_send(client,buf,3);
632
633         /* Send SI tables */
634         i2c_master_send(client,localPAT,sizeof(PAT));
635         i2c_master_send(client,localPMT,sizeof(PMT));
636
637         /* mute then unmute audio. This removes buzzing artefacts */
638         buf[0] = 0xa4;
639         buf[1] = 1;
640         i2c_master_send(client, buf, 2);
641         buf[1] = 0;
642         i2c_master_send(client, buf, 2);
643
644         /* start it going */
645         saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
646
647         /* readout current state */
648         buf[0] = 0xE1;
649         buf[1] = 0xA7;
650         buf[2] = 0xFE;
651         buf[3] = 0x82;
652         buf[4] = 0xB0;
653         i2c_master_send(client, buf, 5);
654         i2c_master_recv(client, buf2, 4);
655
656         /* change aspect ratio */
657         buf[0] = 0xE0;
658         buf[1] = 0xA7;
659         buf[2] = 0xFE;
660         buf[3] = 0x82;
661         buf[4] = 0xB0;
662         buf[5] = buf2[0];
663         switch(h->params.vi_aspect) {
664         case V4L2_MPEG_VIDEO_ASPECT_16x9:
665                 buf[6] = buf2[1] | 0x40;
666                 break;
667         case V4L2_MPEG_VIDEO_ASPECT_4x3:
668         default:
669                 buf[6] = buf2[1] & 0xBF;
670                 break;
671                 break;
672         }
673         buf[7] = buf2[2];
674         buf[8] = buf2[3];
675         i2c_master_send(client, buf, 9);
676
677         return 0;
678 }
679
680 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
681 {
682         struct saa6752hs_state *h;
683
684
685         if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
686                 return -ENOMEM;
687         h->client = client_template;
688         h->params = param_defaults;
689         h->old_params = old_param_defaults;
690         h->client.adapter = adap;
691         h->client.addr = addr;
692
693         /* Assume 625 input lines */
694         h->standard = 0;
695
696         i2c_set_clientdata(&h->client, h);
697         i2c_attach_client(&h->client);
698
699         v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
700
701         return 0;
702 }
703
704 static int saa6752hs_probe(struct i2c_adapter *adap)
705 {
706         if (adap->class & I2C_CLASS_TV_ANALOG)
707                 return i2c_probe(adap, &addr_data, saa6752hs_attach);
708         return 0;
709 }
710
711 static int saa6752hs_detach(struct i2c_client *client)
712 {
713         struct saa6752hs_state *h;
714
715         h = i2c_get_clientdata(client);
716         i2c_detach_client(client);
717         kfree(h);
718         return 0;
719 }
720
721 static int
722 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
723 {
724         struct saa6752hs_state *h = i2c_get_clientdata(client);
725         struct v4l2_ext_controls *ctrls = arg;
726         struct v4l2_mpeg_compression *old_params = arg;
727         struct saa6752hs_mpeg_params params;
728         int err = 0;
729         int i;
730
731         switch (cmd) {
732         case VIDIOC_S_MPEGCOMP:
733                 if (NULL == old_params) {
734                         /* apply settings and start encoder */
735                         saa6752hs_init(client);
736                         break;
737                 }
738                 saa6752hs_old_set_params(client, old_params);
739                 /* fall through */
740         case VIDIOC_G_MPEGCOMP:
741                 *old_params = h->old_params;
742                 break;
743         case VIDIOC_S_EXT_CTRLS:
744                 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
745                         return -EINVAL;
746                 if (ctrls->count == 0) {
747                         /* apply settings and start encoder */
748                         saa6752hs_init(client);
749                         break;
750                 }
751                 /* fall through */
752         case VIDIOC_TRY_EXT_CTRLS:
753         case VIDIOC_G_EXT_CTRLS:
754                 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
755                         return -EINVAL;
756                 params = h->params;
757                 for (i = 0; i < ctrls->count; i++) {
758                         if ((err = handle_ctrl(&params, ctrls->controls + i, cmd))) {
759                                 ctrls->error_idx = i;
760                                 return err;
761                         }
762                 }
763                 h->params = params;
764                 break;
765         case VIDIOC_G_FMT:
766         {
767            struct v4l2_format *f = arg;
768
769            if (h->video_format == SAA6752HS_VF_UNKNOWN)
770                    h->video_format = SAA6752HS_VF_D1;
771            f->fmt.pix.width =
772                    v4l2_format_table[h->video_format].fmt.pix.width;
773            f->fmt.pix.height =
774                    v4l2_format_table[h->video_format].fmt.pix.height;
775            break ;
776         }
777         case VIDIOC_S_FMT:
778         {
779                 struct v4l2_format *f = arg;
780
781                 saa6752hs_set_subsampling(client, f);
782                 break;
783         }
784         case VIDIOC_S_STD:
785                 h->standard = *((v4l2_std_id *) arg);
786                 break;
787         default:
788                 /* nothing */
789                 break;
790         }
791
792         return err;
793 }
794
795 /* ----------------------------------------------------------------------- */
796
797 static struct i2c_driver driver = {
798         .driver = {
799                 .name   = "saa6752hs",
800         },
801         .id             = I2C_DRIVERID_SAA6752HS,
802         .attach_adapter = saa6752hs_probe,
803         .detach_client  = saa6752hs_detach,
804         .command        = saa6752hs_command,
805 };
806
807 static struct i2c_client client_template =
808 {
809         .name       = "saa6752hs",
810         .driver     = &driver,
811 };
812
813 static int __init saa6752hs_init_module(void)
814 {
815         return i2c_add_driver(&driver);
816 }
817
818 static void __exit saa6752hs_cleanup_module(void)
819 {
820         i2c_del_driver(&driver);
821 }
822
823 module_init(saa6752hs_init_module);
824 module_exit(saa6752hs_cleanup_module);
825
826 /*
827  * Overrides for Emacs so that we follow Linus's tabbing style.
828  * ---------------------------------------------------------------------------
829  * Local variables:
830  * c-basic-offset: 8
831  * End:
832  */