]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/saa7134/saa6752hs.c
V4L/DVB (10244): v4l2: replace a few snprintfs with strlcpy
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / saa7134 / saa6752hs.c
CommitLineData
e281db58
HV
1 /*
2 saa6752hs - i2c-driver for the saa6752hs by Philips
3
4 Copyright (C) 2004 Andrew de Quincey
5
6 AC-3 support:
7
8 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License vs published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
23 */
24
1da177e4
LT
25#include <linux/module.h>
26#include <linux/kernel.h>
1da177e4
LT
27#include <linux/string.h>
28#include <linux/timer.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/poll.h>
33#include <linux/i2c.h>
34#include <linux/types.h>
cab462f7
MCC
35#include <linux/videodev2.h>
36#include <media/v4l2-common.h>
e281db58
HV
37#include <media/v4l2-chip-ident.h>
38#include <media/v4l2-i2c-drv-legacy.h>
1da177e4
LT
39#include <linux/init.h>
40#include <linux/crc32.h>
41
1da177e4
LT
42#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
43#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
44#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
45#define MPEG_PID_MAX ((1 << 14) - 1)
46
47/* Addresses to scan */
48static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
f87086e3 49
1da177e4
LT
50I2C_CLIENT_INSMOD;
51
52MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
53MODULE_AUTHOR("Andrew de Quincey");
54MODULE_LICENSE("GPL");
55
0a4c9c93
FC
56enum saa6752hs_videoformat {
57 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
58 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
59 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
60 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
61 SAA6752HS_VF_UNKNOWN,
62};
63
86b79d66
HV
64struct saa6752hs_mpeg_params {
65 /* transport streams */
66 __u16 ts_pid_pmt;
67 __u16 ts_pid_audio;
68 __u16 ts_pid_video;
69 __u16 ts_pid_pcr;
70
71 /* audio */
e281db58
HV
72 enum v4l2_mpeg_audio_encoding au_encoding;
73 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
74 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
86b79d66
HV
75
76 /* video */
77 enum v4l2_mpeg_video_aspect vi_aspect;
78 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
79 __u32 vi_bitrate;
80 __u32 vi_bitrate_peak;
81};
82
0a4c9c93
FC
83static const struct v4l2_format v4l2_format_table[] =
84{
ac19ecc6
MCC
85 [SAA6752HS_VF_D1] =
86 { .fmt = { .pix = { .width = 720, .height = 576 }}},
87 [SAA6752HS_VF_2_3_D1] =
88 { .fmt = { .pix = { .width = 480, .height = 576 }}},
89 [SAA6752HS_VF_1_2_D1] =
90 { .fmt = { .pix = { .width = 352, .height = 576 }}},
91 [SAA6752HS_VF_SIF] =
92 { .fmt = { .pix = { .width = 352, .height = 288 }}},
93 [SAA6752HS_VF_UNKNOWN] =
94 { .fmt = { .pix = { .width = 0, .height = 0}}},
0a4c9c93
FC
95};
96
1da177e4 97struct saa6752hs_state {
e281db58
HV
98 int chip;
99 u32 revision;
100 int has_ac3;
86b79d66 101 struct saa6752hs_mpeg_params params;
0a4c9c93 102 enum saa6752hs_videoformat video_format;
9b71521b 103 v4l2_std_id standard;
1da177e4
LT
104};
105
106enum saa6752hs_command {
107 SAA6752HS_COMMAND_RESET = 0,
657de3cd
TP
108 SAA6752HS_COMMAND_STOP = 1,
109 SAA6752HS_COMMAND_START = 2,
110 SAA6752HS_COMMAND_PAUSE = 3,
111 SAA6752HS_COMMAND_RECONFIGURE = 4,
112 SAA6752HS_COMMAND_SLEEP = 5,
1da177e4
LT
113 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
114
115 SAA6752HS_COMMAND_MAX
116};
117
118/* ---------------------------------------------------------------------- */
119
120static u8 PAT[] = {
9b71521b
RB
121 0xc2, /* i2c register */
122 0x00, /* table number for encoder */
1da177e4 123
9b71521b
RB
124 0x47, /* sync */
125 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
126 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
1da177e4 127
9b71521b 128 0x00, /* PSI pointer to start of table */
1da177e4 129
9b71521b
RB
130 0x00, /* tid(0) */
131 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
1da177e4 132
9b71521b 133 0x00, 0x01, /* transport_stream_id(1) */
1da177e4 134
9b71521b 135 0xc1, /* version_number(0), current_next_indicator(1) */
1da177e4 136
9b71521b 137 0x00, 0x00, /* section_number(0), last_section_number(0) */
1da177e4 138
9b71521b 139 0x00, 0x01, /* program_number(1) */
1da177e4 140
9b71521b 141 0xe0, 0x00, /* PMT PID */
1da177e4 142
9b71521b 143 0x00, 0x00, 0x00, 0x00 /* CRC32 */
1da177e4
LT
144};
145
146static u8 PMT[] = {
9b71521b
RB
147 0xc2, /* i2c register */
148 0x01, /* table number for encoder */
1da177e4 149
9b71521b
RB
150 0x47, /* sync */
151 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
152 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
1da177e4 153
9b71521b 154 0x00, /* PSI pointer to start of table */
1da177e4 155
9b71521b
RB
156 0x02, /* tid(2) */
157 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
1da177e4 158
9b71521b 159 0x00, 0x01, /* program_number(1) */
1da177e4 160
9b71521b 161 0xc1, /* version_number(0), current_next_indicator(1) */
1da177e4 162
9b71521b 163 0x00, 0x00, /* section_number(0), last_section_number(0) */
1da177e4 164
9b71521b 165 0xe0, 0x00, /* PCR_PID */
1da177e4 166
9b71521b 167 0xf0, 0x00, /* program_info_length(0) */
1da177e4 168
9b71521b
RB
169 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
170 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
1da177e4 171
9b71521b 172 0x00, 0x00, 0x00, 0x00 /* CRC32 */
1da177e4
LT
173};
174
3eea543b
HV
175static u8 PMT_AC3[] = {
176 0xc2, /* i2c register */
177 0x01, /* table number for encoder(1) */
178 0x47, /* sync */
179
180 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
181 0x10, /* PMT PID (0x0010) */
182 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
183
184 0x00, /* PSI pointer to start of table */
185
186 0x02, /* TID (2) */
187 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
188
189 0x00, 0x01, /* program_number(1) */
190
191 0xc1, /* version_number(0), current_next_indicator(1) */
192
193 0x00, 0x00, /* section_number(0), last_section_number(0) */
194
195 0xe1, 0x04, /* PCR_PID (0x0104) */
196
197 0xf0, 0x00, /* program_info_length(0) */
198
199 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
200 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
201 0x6a, /* AC3 */
202 0x01, /* Descriptor_length(1) */
203 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
204
205 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
206};
207
86b79d66
HV
208static struct saa6752hs_mpeg_params param_defaults =
209{
210 .ts_pid_pmt = 16,
211 .ts_pid_video = 260,
212 .ts_pid_audio = 256,
213 .ts_pid_pcr = 259,
214
215 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
216 .vi_bitrate = 4000,
217 .vi_bitrate_peak = 6000,
218 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
219
e281db58 220 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
86b79d66 221 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
3eea543b 222 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
86b79d66
HV
223};
224
1da177e4
LT
225/* ---------------------------------------------------------------------- */
226
ff5f26b4 227static int saa6752hs_chip_command(struct i2c_client *client,
1da177e4
LT
228 enum saa6752hs_command command)
229{
230 unsigned char buf[3];
231 unsigned long timeout;
232 int status = 0;
233
9b71521b 234 /* execute the command */
1da177e4 235 switch(command) {
4ac97914
MCC
236 case SAA6752HS_COMMAND_RESET:
237 buf[0] = 0x00;
1da177e4
LT
238 break;
239
240 case SAA6752HS_COMMAND_STOP:
4ac97914 241 buf[0] = 0x03;
1da177e4
LT
242 break;
243
244 case SAA6752HS_COMMAND_START:
4ac97914 245 buf[0] = 0x02;
1da177e4
LT
246 break;
247
248 case SAA6752HS_COMMAND_PAUSE:
4ac97914 249 buf[0] = 0x04;
1da177e4
LT
250 break;
251
252 case SAA6752HS_COMMAND_RECONFIGURE:
253 buf[0] = 0x05;
254 break;
255
4ac97914
MCC
256 case SAA6752HS_COMMAND_SLEEP:
257 buf[0] = 0x06;
1da177e4
LT
258 break;
259
4ac97914 260 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
1da177e4
LT
261 buf[0] = 0x07;
262 break;
263
264 default:
265 return -EINVAL;
266 }
267
4ac97914 268 /* set it and wait for it to be so */
1da177e4
LT
269 i2c_master_send(client, buf, 1);
270 timeout = jiffies + HZ * 3;
271 for (;;) {
9b71521b 272 /* get the current status */
1da177e4 273 buf[0] = 0x10;
4ac97914 274 i2c_master_send(client, buf, 1);
1da177e4
LT
275 i2c_master_recv(client, buf, 1);
276
277 if (!(buf[0] & 0x20))
278 break;
279 if (time_after(jiffies,timeout)) {
280 status = -ETIMEDOUT;
281 break;
282 }
283
1da177e4
LT
284 msleep(10);
285 }
286
9b71521b 287 /* delay a bit to let encoder settle */
1da177e4
LT
288 msleep(50);
289
4ac97914 290 return status;
1da177e4
LT
291}
292
293
ff5f26b4
HV
294static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
295{
296 u8 buf[2];
297
298 buf[0] = reg;
299 buf[1] = val;
300 i2c_master_send(client, buf, 2);
301}
302
303static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
304{
305 u8 buf[3];
306
307 buf[0] = reg;
308 buf[1] = val >> 8;
309 buf[2] = val & 0xff;
310 i2c_master_send(client, buf, 3);
311}
312
313static int saa6752hs_set_bitrate(struct i2c_client *client,
e281db58 314 struct saa6752hs_state *h)
1da177e4 315{
e281db58 316 struct saa6752hs_mpeg_params *params = &h->params;
b57e5578 317 int tot_bitrate;
ff5f26b4 318 int is_384k;
1da177e4 319
9b71521b 320 /* set the bitrate mode */
ff5f26b4
HV
321 set_reg8(client, 0x71,
322 params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
1da177e4 323
9b71521b 324 /* set the video bitrate */
b57e5578 325 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
9b71521b 326 /* set the target bitrate */
ff5f26b4 327 set_reg16(client, 0x80, params->vi_bitrate);
1da177e4 328
9b71521b 329 /* set the max bitrate */
ff5f26b4 330 set_reg16(client, 0x81, params->vi_bitrate_peak);
b57e5578 331 tot_bitrate = params->vi_bitrate_peak;
1da177e4 332 } else {
9b71521b 333 /* set the target bitrate (no max bitrate for CBR) */
ff5f26b4 334 set_reg16(client, 0x81, params->vi_bitrate);
b57e5578 335 tot_bitrate = params->vi_bitrate;
1da177e4
LT
336 }
337
e281db58 338 /* set the audio encoding */
ff5f26b4
HV
339 set_reg8(client, 0x93,
340 params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
e281db58 341
9b71521b 342 /* set the audio bitrate */
e281db58 343 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
ff5f26b4 344 is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
e281db58 345 else
ff5f26b4
HV
346 is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
347 set_reg8(client, 0x94, is_384k);
348 tot_bitrate += is_384k ? 384 : 256;
b57e5578
MCC
349
350 /* Note: the total max bitrate is determined by adding the video and audio
351 bitrates together and also adding an extra 768kbit/s to stay on the
352 safe side. If more control should be required, then an extra MPEG control
353 should be added. */
354 tot_bitrate += 768;
355 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
356 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
1da177e4 357
9b71521b 358 /* set the total bitrate */
ff5f26b4 359 set_reg16(client, 0xb1, tot_bitrate);
1da177e4
LT
360 return 0;
361}
362
ff5f26b4
HV
363static void saa6752hs_set_subsampling(struct i2c_client *client,
364 struct v4l2_format *f)
0a4c9c93
FC
365{
366 struct saa6752hs_state *h = i2c_get_clientdata(client);
367 int dist_352, dist_480, dist_720;
368
369 /*
370 FIXME: translate and round width/height into EMPRESS
371 subsample type:
372
373 type | PAL | NTSC
374 ---------------------------
375 SIF | 352x288 | 352x240
376 1/2 D1 | 352x576 | 352x480
377 2/3 D1 | 480x576 | 480x480
378 D1 | 720x576 | 720x480
379 */
380
381 dist_352 = abs(f->fmt.pix.width - 352);
382 dist_480 = abs(f->fmt.pix.width - 480);
383 dist_720 = abs(f->fmt.pix.width - 720);
384 if (dist_720 < dist_480) {
385 f->fmt.pix.width = 720;
386 f->fmt.pix.height = 576;
387 h->video_format = SAA6752HS_VF_D1;
388 }
389 else if (dist_480 < dist_352) {
390 f->fmt.pix.width = 480;
391 f->fmt.pix.height = 576;
392 h->video_format = SAA6752HS_VF_2_3_D1;
393 }
394 else {
395 f->fmt.pix.width = 352;
396 if (abs(f->fmt.pix.height - 576) <
397 abs(f->fmt.pix.height - 288)) {
398 f->fmt.pix.height = 576;
399 h->video_format = SAA6752HS_VF_1_2_D1;
400 }
401 else {
402 f->fmt.pix.height = 288;
403 h->video_format = SAA6752HS_VF_SIF;
404 }
405 }
406}
407
1da177e4 408
e281db58 409static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
4d6b5aee 410 struct v4l2_ext_control *ctrl, unsigned int cmd)
86b79d66
HV
411{
412 int old = 0, new;
4d6b5aee 413 int set = (cmd == VIDIOC_S_EXT_CTRLS);
86b79d66
HV
414
415 new = ctrl->value;
416 switch (ctrl->id) {
417 case V4L2_CID_MPEG_STREAM_TYPE:
418 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
419 if (set && new != old)
420 return -ERANGE;
421 new = old;
422 break;
423 case V4L2_CID_MPEG_STREAM_PID_PMT:
424 old = params->ts_pid_pmt;
425 if (set && new > MPEG_PID_MAX)
426 return -ERANGE;
427 if (new > MPEG_PID_MAX)
428 new = MPEG_PID_MAX;
429 params->ts_pid_pmt = new;
430 break;
431 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
432 old = params->ts_pid_audio;
433 if (set && new > MPEG_PID_MAX)
434 return -ERANGE;
435 if (new > MPEG_PID_MAX)
436 new = MPEG_PID_MAX;
437 params->ts_pid_audio = new;
438 break;
439 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
440 old = params->ts_pid_video;
441 if (set && new > MPEG_PID_MAX)
442 return -ERANGE;
443 if (new > MPEG_PID_MAX)
444 new = MPEG_PID_MAX;
445 params->ts_pid_video = new;
446 break;
447 case V4L2_CID_MPEG_STREAM_PID_PCR:
448 old = params->ts_pid_pcr;
449 if (set && new > MPEG_PID_MAX)
450 return -ERANGE;
451 if (new > MPEG_PID_MAX)
452 new = MPEG_PID_MAX;
453 params->ts_pid_pcr = new;
454 break;
455 case V4L2_CID_MPEG_AUDIO_ENCODING:
e281db58
HV
456 old = params->au_encoding;
457 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
458 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
86b79d66
HV
459 return -ERANGE;
460 new = old;
461 break;
462 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
463 old = params->au_l2_bitrate;
464 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
465 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
466 return -ERANGE;
467 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
468 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
469 else
470 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
471 params->au_l2_bitrate = new;
472 break;
e281db58
HV
473 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
474 if (!has_ac3)
475 return -EINVAL;
476 old = params->au_ac3_bitrate;
477 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
478 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
479 return -ERANGE;
480 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
481 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
482 else
483 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
484 params->au_ac3_bitrate = new;
485 break;
86b79d66
HV
486 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
487 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
488 if (set && new != old)
489 return -ERANGE;
490 new = old;
491 break;
492 case V4L2_CID_MPEG_VIDEO_ENCODING:
493 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
494 if (set && new != old)
495 return -ERANGE;
496 new = old;
497 break;
498 case V4L2_CID_MPEG_VIDEO_ASPECT:
499 old = params->vi_aspect;
500 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
501 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
502 return -ERANGE;
503 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
504 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
505 params->vi_aspect = new;
506 break;
507 case V4L2_CID_MPEG_VIDEO_BITRATE:
508 old = params->vi_bitrate * 1000;
509 new = 1000 * (new / 1000);
510 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
511 return -ERANGE;
512 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
513 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
514 params->vi_bitrate = new / 1000;
515 break;
516 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
517 old = params->vi_bitrate_peak * 1000;
518 new = 1000 * (new / 1000);
519 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
520 return -ERANGE;
521 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
522 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
523 params->vi_bitrate_peak = new / 1000;
524 break;
525 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
526 old = params->vi_bitrate_mode;
527 params->vi_bitrate_mode = new;
528 break;
529 default:
530 return -EINVAL;
531 }
532 if (cmd == VIDIOC_G_EXT_CTRLS)
533 ctrl->value = old;
1da177e4 534 else
86b79d66
HV
535 ctrl->value = new;
536 return 0;
1da177e4
LT
537}
538
e281db58 539static int saa6752hs_qctrl(struct saa6752hs_state *h,
8b53b39d
HV
540 struct v4l2_queryctrl *qctrl)
541{
e281db58 542 struct saa6752hs_mpeg_params *params = &h->params;
8b53b39d
HV
543 int err;
544
545 switch (qctrl->id) {
546 case V4L2_CID_MPEG_AUDIO_ENCODING:
547 return v4l2_ctrl_query_fill(qctrl,
548 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
e281db58
HV
549 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
550 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
551 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
8b53b39d
HV
552
553 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
554 return v4l2_ctrl_query_fill(qctrl,
555 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
556 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
557 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
558
e281db58
HV
559 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
560 if (!h->has_ac3)
561 return -EINVAL;
562 return v4l2_ctrl_query_fill(qctrl,
563 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
564 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
565 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
566
8b53b39d
HV
567 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
568 return v4l2_ctrl_query_fill(qctrl,
569 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
570 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
571 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
572
573 case V4L2_CID_MPEG_VIDEO_ENCODING:
574 return v4l2_ctrl_query_fill(qctrl,
575 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
576 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
577 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
578
579 case V4L2_CID_MPEG_VIDEO_ASPECT:
580 return v4l2_ctrl_query_fill(qctrl,
581 V4L2_MPEG_VIDEO_ASPECT_4x3,
582 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
583 V4L2_MPEG_VIDEO_ASPECT_4x3);
584
585 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
586 err = v4l2_ctrl_query_fill_std(qctrl);
587 if (err == 0 &&
588 params->vi_bitrate_mode ==
589 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
590 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
591 return err;
592
593 case V4L2_CID_MPEG_STREAM_TYPE:
594 return v4l2_ctrl_query_fill(qctrl,
595 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
596 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
597 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
598
599 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
600 case V4L2_CID_MPEG_VIDEO_BITRATE:
601 case V4L2_CID_MPEG_STREAM_PID_PMT:
602 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
603 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
604 case V4L2_CID_MPEG_STREAM_PID_PCR:
605 return v4l2_ctrl_query_fill_std(qctrl);
606
607 default:
608 break;
609 }
610 return -EINVAL;
611}
612
e281db58 613static int saa6752hs_qmenu(struct saa6752hs_state *h,
8b53b39d
HV
614 struct v4l2_querymenu *qmenu)
615{
e281db58
HV
616 static const u32 mpeg_audio_encoding[] = {
617 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
618 V4L2_CTRL_MENU_IDS_END
619 };
620 static const u32 mpeg_audio_ac3_encoding[] = {
621 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
622 V4L2_MPEG_AUDIO_ENCODING_AC3,
623 V4L2_CTRL_MENU_IDS_END
624 };
625 static u32 mpeg_audio_l2_bitrate[] = {
626 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
627 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
628 V4L2_CTRL_MENU_IDS_END
629 };
630 static u32 mpeg_audio_ac3_bitrate[] = {
631 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
632 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
633 V4L2_CTRL_MENU_IDS_END
8b53b39d
HV
634 };
635 struct v4l2_queryctrl qctrl;
636 int err;
637
638 qctrl.id = qmenu->id;
e281db58 639 err = saa6752hs_qctrl(h, &qctrl);
8b53b39d
HV
640 if (err)
641 return err;
e281db58
HV
642 switch (qmenu->id) {
643 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
644 return v4l2_ctrl_query_menu_valid_items(qmenu,
8b53b39d 645 mpeg_audio_l2_bitrate);
e281db58
HV
646 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
647 if (!h->has_ac3)
648 return -EINVAL;
649 return v4l2_ctrl_query_menu_valid_items(qmenu,
650 mpeg_audio_ac3_bitrate);
651 case V4L2_CID_MPEG_AUDIO_ENCODING:
652 return v4l2_ctrl_query_menu_valid_items(qmenu,
653 h->has_ac3 ? mpeg_audio_ac3_encoding :
654 mpeg_audio_encoding);
655 }
656 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
8b53b39d
HV
657}
658
f03813e4 659static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
1da177e4
LT
660{
661 unsigned char buf[9], buf2[4];
662 struct saa6752hs_state *h;
3eea543b 663 unsigned size;
1da177e4
LT
664 u32 crc;
665 unsigned char localPAT[256];
666 unsigned char localPMT[256];
667
668 h = i2c_get_clientdata(client);
669
9b71521b 670 /* Set video format - must be done first as it resets other settings */
ff5f26b4 671 set_reg8(client, 0x41, h->video_format);
1da177e4 672
9b71521b 673 /* Set number of lines in input signal */
ff5f26b4 674 set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
9b71521b 675
4ac97914 676 /* set bitrate */
e281db58 677 saa6752hs_set_bitrate(client, h);
1da177e4 678
9b71521b 679 /* Set GOP structure {3, 13} */
ff5f26b4 680 set_reg16(client, 0x72, 0x030d);
1da177e4 681
657de3cd 682 /* Set minimum Q-scale {4} */
ff5f26b4 683 set_reg8(client, 0x82, 0x04);
1da177e4 684
657de3cd 685 /* Set maximum Q-scale {12} */
ff5f26b4 686 set_reg8(client, 0x83, 0x0c);
1da177e4 687
657de3cd 688 /* Set Output Protocol */
ff5f26b4 689 set_reg8(client, 0xd0, 0x81);
1da177e4 690
657de3cd 691 /* Set video output stream format {TS} */
ff5f26b4 692 set_reg8(client, 0xb0, 0x05);
1da177e4 693
f03813e4 694 /* Set leading null byte for TS */
ff5f26b4 695 set_reg16(client, 0xf6, leading_null_bytes);
f03813e4 696
1da177e4
LT
697 /* compute PAT */
698 memcpy(localPAT, PAT, sizeof(PAT));
699 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
700 localPAT[18] = h->params.ts_pid_pmt & 0xff;
701 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
702 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
703 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
704 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
705 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
706
707 /* compute PMT */
3eea543b
HV
708 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
709 size = sizeof(PMT_AC3);
710 memcpy(localPMT, PMT_AC3, size);
711 } else {
712 size = sizeof(PMT);
713 memcpy(localPMT, PMT, size);
714 }
4ac97914
MCC
715 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
716 localPMT[4] = h->params.ts_pid_pmt & 0xff;
1da177e4
LT
717 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
718 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
719 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
720 localPMT[21] = h->params.ts_pid_video & 0xFF;
721 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
722 localPMT[26] = h->params.ts_pid_audio & 0xFF;
3eea543b
HV
723 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
724 localPMT[size - 4] = (crc >> 24) & 0xFF;
725 localPMT[size - 3] = (crc >> 16) & 0xFF;
726 localPMT[size - 2] = (crc >> 8) & 0xFF;
727 localPMT[size - 1] = crc & 0xFF;
1da177e4 728
657de3cd 729 /* Set Audio PID */
ff5f26b4 730 set_reg16(client, 0xc1, h->params.ts_pid_audio);
1da177e4 731
9b71521b 732 /* Set Video PID */
ff5f26b4 733 set_reg16(client, 0xc0, h->params.ts_pid_video);
1da177e4 734
4ac97914 735 /* Set PCR PID */
ff5f26b4 736 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
1da177e4 737
9b71521b 738 /* Send SI tables */
3eea543b
HV
739 i2c_master_send(client, localPAT, sizeof(PAT));
740 i2c_master_send(client, localPMT, size);
1da177e4 741
9b71521b 742 /* mute then unmute audio. This removes buzzing artefacts */
ff5f26b4
HV
743 set_reg8(client, 0xa4, 1);
744 set_reg8(client, 0xa4, 0);
1da177e4 745
9b71521b 746 /* start it going */
1da177e4
LT
747 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
748
9b71521b 749 /* readout current state */
1da177e4
LT
750 buf[0] = 0xE1;
751 buf[1] = 0xA7;
752 buf[2] = 0xFE;
753 buf[3] = 0x82;
754 buf[4] = 0xB0;
755 i2c_master_send(client, buf, 5);
756 i2c_master_recv(client, buf2, 4);
757
9b71521b 758 /* change aspect ratio */
1da177e4
LT
759 buf[0] = 0xE0;
760 buf[1] = 0xA7;
761 buf[2] = 0xFE;
762 buf[3] = 0x82;
763 buf[4] = 0xB0;
764 buf[5] = buf2[0];
86b79d66
HV
765 switch(h->params.vi_aspect) {
766 case V4L2_MPEG_VIDEO_ASPECT_16x9:
1da177e4
LT
767 buf[6] = buf2[1] | 0x40;
768 break;
86b79d66 769 case V4L2_MPEG_VIDEO_ASPECT_4x3:
1da177e4
LT
770 default:
771 buf[6] = buf2[1] & 0xBF;
772 break;
773 break;
774 }
775 buf[7] = buf2[2];
776 buf[8] = buf2[3];
777 i2c_master_send(client, buf, 9);
778
1da177e4
LT
779 return 0;
780}
781
1da177e4
LT
782static int
783saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
784{
785 struct saa6752hs_state *h = i2c_get_clientdata(client);
86b79d66 786 struct v4l2_ext_controls *ctrls = arg;
86b79d66 787 struct saa6752hs_mpeg_params params;
1da177e4 788 int err = 0;
86b79d66 789 int i;
1da177e4 790
4ac97914 791 switch (cmd) {
f03813e4
DB
792 case VIDIOC_INT_INIT:
793 /* apply settings and start encoder */
794 saa6752hs_init(client, *(u32 *)arg);
795 break;
86b79d66
HV
796 case VIDIOC_S_EXT_CTRLS:
797 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
798 return -EINVAL;
86b79d66
HV
799 /* fall through */
800 case VIDIOC_TRY_EXT_CTRLS:
801 case VIDIOC_G_EXT_CTRLS:
802 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
803 return -EINVAL;
804 params = h->params;
805 for (i = 0; i < ctrls->count; i++) {
e281db58
HV
806 err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, cmd);
807 if (err) {
86b79d66
HV
808 ctrls->error_idx = i;
809 return err;
810 }
811 }
812 h->params = params;
1da177e4 813 break;
8b53b39d 814 case VIDIOC_QUERYCTRL:
e281db58 815 return saa6752hs_qctrl(h, arg);
8b53b39d 816 case VIDIOC_QUERYMENU:
e281db58 817 return saa6752hs_qmenu(h, arg);
0a4c9c93
FC
818 case VIDIOC_G_FMT:
819 {
4ac97914 820 struct v4l2_format *f = arg;
0a4c9c93
FC
821
822 if (h->video_format == SAA6752HS_VF_UNKNOWN)
823 h->video_format = SAA6752HS_VF_D1;
824 f->fmt.pix.width =
825 v4l2_format_table[h->video_format].fmt.pix.width;
826 f->fmt.pix.height =
827 v4l2_format_table[h->video_format].fmt.pix.height;
828 break ;
829 }
830 case VIDIOC_S_FMT:
831 {
832 struct v4l2_format *f = arg;
833
834 saa6752hs_set_subsampling(client, f);
835 break;
836 }
9b71521b
RB
837 case VIDIOC_S_STD:
838 h->standard = *((v4l2_std_id *) arg);
839 break;
e281db58 840
aecde8b5 841 case VIDIOC_DBG_G_CHIP_IDENT:
e281db58
HV
842 return v4l2_chip_ident_i2c_client(client,
843 arg, h->chip, h->revision);
844
1da177e4
LT
845 default:
846 /* nothing */
847 break;
848 }
849
850 return err;
851}
852
e281db58
HV
853static int saa6752hs_probe(struct i2c_client *client,
854 const struct i2c_device_id *id)
855{
856 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
857 u8 addr = 0x13;
858 u8 data[12];
1da177e4 859
e281db58
HV
860 v4l_info(client, "chip found @ 0x%x (%s)\n",
861 client->addr << 1, client->adapter->name);
862 if (h == NULL)
863 return -ENOMEM;
1da177e4 864
e281db58
HV
865 i2c_master_send(client, &addr, 1);
866 i2c_master_recv(client, data, sizeof(data));
867 h->chip = V4L2_IDENT_SAA6752HS;
868 h->revision = (data[8] << 8) | data[9];
869 h->has_ac3 = 0;
870 if (h->revision == 0x0206) {
871 h->chip = V4L2_IDENT_SAA6752HS_AC3;
872 h->has_ac3 = 1;
873 v4l_info(client, "support AC-3\n");
874 }
875 h->params = param_defaults;
876 h->standard = 0; /* Assume 625 input lines */
1da177e4 877
e281db58
HV
878 i2c_set_clientdata(client, h);
879 return 0;
1da177e4
LT
880}
881
e281db58 882static int saa6752hs_remove(struct i2c_client *client)
1da177e4 883{
e281db58
HV
884 kfree(i2c_get_clientdata(client));
885 return 0;
1da177e4
LT
886}
887
e281db58
HV
888static const struct i2c_device_id saa6752hs_id[] = {
889 { "saa6752hs", 0 },
890 { }
891};
892MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
893
894static struct v4l2_i2c_driver_data v4l2_i2c_data = {
895 .name = "saa6752hs",
896 .driverid = I2C_DRIVERID_SAA6752HS,
897 .command = saa6752hs_command,
898 .probe = saa6752hs_probe,
899 .remove = saa6752hs_remove,
900 .id_table = saa6752hs_id,
901};
1da177e4
LT
902
903/*
904 * Overrides for Emacs so that we follow Linus's tabbing style.
905 * ---------------------------------------------------------------------------
906 * Local variables:
907 * c-basic-offset: 8
908 * End:
909 */