blob: 002e70a33a4f6bf2fe847c6223a1a6518f25955a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/module.h>
2#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/string.h>
4#include <linux/timer.h>
5#include <linux/delay.h>
6#include <linux/errno.h>
7#include <linux/slab.h>
8#include <linux/poll.h>
9#include <linux/i2c.h>
10#include <linux/types.h>
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020011#include <linux/videodev2.h>
12#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/crc32.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
18#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
19#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
20#define MPEG_PID_MAX ((1 << 14) - 1)
21
22/* Addresses to scan */
23static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
Linus Torvalds1da177e2005-04-16 15:20:36 -070024I2C_CLIENT_INSMOD;
25
26MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
27MODULE_AUTHOR("Andrew de Quincey");
28MODULE_LICENSE("GPL");
29
30static struct i2c_driver driver;
31static struct i2c_client client_template;
32
Frederic CAND0a4c9c92005-05-05 16:15:52 -070033enum saa6752hs_videoformat {
34 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
35 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
36 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
37 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
38 SAA6752HS_VF_UNKNOWN,
39};
40
Hans Verkuil86b79d662006-06-18 16:40:10 -030041struct saa6752hs_mpeg_params {
42 /* transport streams */
43 __u16 ts_pid_pmt;
44 __u16 ts_pid_audio;
45 __u16 ts_pid_video;
46 __u16 ts_pid_pcr;
47
48 /* audio */
49 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
50
51 /* video */
52 enum v4l2_mpeg_video_aspect vi_aspect;
53 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
54 __u32 vi_bitrate;
55 __u32 vi_bitrate_peak;
56};
57
Frederic CAND0a4c9c92005-05-05 16:15:52 -070058static const struct v4l2_format v4l2_format_table[] =
59{
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -070060 [SAA6752HS_VF_D1] =
61 { .fmt = { .pix = { .width = 720, .height = 576 }}},
62 [SAA6752HS_VF_2_3_D1] =
63 { .fmt = { .pix = { .width = 480, .height = 576 }}},
64 [SAA6752HS_VF_1_2_D1] =
65 { .fmt = { .pix = { .width = 352, .height = 576 }}},
66 [SAA6752HS_VF_SIF] =
67 { .fmt = { .pix = { .width = 352, .height = 288 }}},
68 [SAA6752HS_VF_UNKNOWN] =
69 { .fmt = { .pix = { .width = 0, .height = 0}}},
Frederic CAND0a4c9c92005-05-05 16:15:52 -070070};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct saa6752hs_state {
73 struct i2c_client client;
Hans Verkuil86b79d662006-06-18 16:40:10 -030074 struct saa6752hs_mpeg_params params;
Frederic CAND0a4c9c92005-05-05 16:15:52 -070075 enum saa6752hs_videoformat video_format;
Robert W. Boone9b715212005-11-08 21:36:45 -080076 v4l2_std_id standard;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79enum saa6752hs_command {
80 SAA6752HS_COMMAND_RESET = 0,
Trent Piepho657de3c2006-06-20 00:30:57 -030081 SAA6752HS_COMMAND_STOP = 1,
82 SAA6752HS_COMMAND_START = 2,
83 SAA6752HS_COMMAND_PAUSE = 3,
84 SAA6752HS_COMMAND_RECONFIGURE = 4,
85 SAA6752HS_COMMAND_SLEEP = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
87
88 SAA6752HS_COMMAND_MAX
89};
90
91/* ---------------------------------------------------------------------- */
92
93static u8 PAT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -080094 0xc2, /* i2c register */
95 0x00, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Robert W. Boone9b715212005-11-08 21:36:45 -080097 0x47, /* sync */
98 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
99 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Robert W. Boone9b715212005-11-08 21:36:45 -0800101 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Robert W. Boone9b715212005-11-08 21:36:45 -0800103 0x00, /* tid(0) */
104 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Robert W. Boone9b715212005-11-08 21:36:45 -0800106 0x00, 0x01, /* transport_stream_id(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Robert W. Boone9b715212005-11-08 21:36:45 -0800108 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Robert W. Boone9b715212005-11-08 21:36:45 -0800110 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Robert W. Boone9b715212005-11-08 21:36:45 -0800112 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Robert W. Boone9b715212005-11-08 21:36:45 -0800114 0xe0, 0x00, /* PMT PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Robert W. Boone9b715212005-11-08 21:36:45 -0800116 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119static u8 PMT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800120 0xc2, /* i2c register */
121 0x01, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Robert W. Boone9b715212005-11-08 21:36:45 -0800123 0x47, /* sync */
124 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
125 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Robert W. Boone9b715212005-11-08 21:36:45 -0800127 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Robert W. Boone9b715212005-11-08 21:36:45 -0800129 0x02, /* tid(2) */
130 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Robert W. Boone9b715212005-11-08 21:36:45 -0800132 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Robert W. Boone9b715212005-11-08 21:36:45 -0800134 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Robert W. Boone9b715212005-11-08 21:36:45 -0800136 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Robert W. Boone9b715212005-11-08 21:36:45 -0800138 0xe0, 0x00, /* PCR_PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Robert W. Boone9b715212005-11-08 21:36:45 -0800140 0xf0, 0x00, /* program_info_length(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Robert W. Boone9b715212005-11-08 21:36:45 -0800142 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
143 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Robert W. Boone9b715212005-11-08 21:36:45 -0800145 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Hans Verkuil86b79d662006-06-18 16:40:10 -0300148static struct saa6752hs_mpeg_params param_defaults =
149{
150 .ts_pid_pmt = 16,
151 .ts_pid_video = 260,
152 .ts_pid_audio = 256,
153 .ts_pid_pcr = 259,
154
155 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
156 .vi_bitrate = 4000,
157 .vi_bitrate_peak = 6000,
158 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
159
160 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
161};
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* ---------------------------------------------------------------------- */
164
165static int saa6752hs_chip_command(struct i2c_client* client,
166 enum saa6752hs_command command)
167{
168 unsigned char buf[3];
169 unsigned long timeout;
170 int status = 0;
171
Robert W. Boone9b715212005-11-08 21:36:45 -0800172 /* execute the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 switch(command) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800174 case SAA6752HS_COMMAND_RESET:
175 buf[0] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177
178 case SAA6752HS_COMMAND_STOP:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800179 buf[0] = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181
182 case SAA6752HS_COMMAND_START:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800183 buf[0] = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185
186 case SAA6752HS_COMMAND_PAUSE:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800187 buf[0] = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189
190 case SAA6752HS_COMMAND_RECONFIGURE:
191 buf[0] = 0x05;
192 break;
193
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800194 case SAA6752HS_COMMAND_SLEEP:
195 buf[0] = 0x06;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800198 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 buf[0] = 0x07;
200 break;
201
202 default:
203 return -EINVAL;
204 }
205
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800206 /* set it and wait for it to be so */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 i2c_master_send(client, buf, 1);
208 timeout = jiffies + HZ * 3;
209 for (;;) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800210 /* get the current status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 buf[0] = 0x10;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800212 i2c_master_send(client, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 i2c_master_recv(client, buf, 1);
214
215 if (!(buf[0] & 0x20))
216 break;
217 if (time_after(jiffies,timeout)) {
218 status = -ETIMEDOUT;
219 break;
220 }
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 msleep(10);
223 }
224
Robert W. Boone9b715212005-11-08 21:36:45 -0800225 /* delay a bit to let encoder settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 msleep(50);
227
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800228 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231
232static int saa6752hs_set_bitrate(struct i2c_client* client,
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300233 struct saa6752hs_mpeg_params* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800235 u8 buf[3];
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300236 int tot_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Robert W. Boone9b715212005-11-08 21:36:45 -0800238 /* set the bitrate mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 buf[0] = 0x71;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300240 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 i2c_master_send(client, buf, 2);
242
Robert W. Boone9b715212005-11-08 21:36:45 -0800243 /* set the video bitrate */
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300244 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800245 /* set the target bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 buf[0] = 0x80;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300247 buf[1] = params->vi_bitrate >> 8;
248 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 i2c_master_send(client, buf, 3);
250
Robert W. Boone9b715212005-11-08 21:36:45 -0800251 /* set the max bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300253 buf[1] = params->vi_bitrate_peak >> 8;
254 buf[2] = params->vi_bitrate_peak & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300256 tot_bitrate = params->vi_bitrate_peak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
Robert W. Boone9b715212005-11-08 21:36:45 -0800258 /* set the target bitrate (no max bitrate for CBR) */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800259 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300260 buf[1] = params->vi_bitrate >> 8;
261 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300263 tot_bitrate = params->vi_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Robert W. Boone9b715212005-11-08 21:36:45 -0800266 /* set the audio bitrate */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800267 buf[0] = 0x94;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300268 buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300270 tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384;
271
272 /* Note: the total max bitrate is determined by adding the video and audio
273 bitrates together and also adding an extra 768kbit/s to stay on the
274 safe side. If more control should be required, then an extra MPEG control
275 should be added. */
276 tot_bitrate += 768;
277 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
278 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Robert W. Boone9b715212005-11-08 21:36:45 -0800280 /* set the total bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 buf[0] = 0xb1;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300282 buf[1] = tot_bitrate >> 8;
283 buf[2] = tot_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 i2c_master_send(client, buf, 3);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
288
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700289static void saa6752hs_set_subsampling(struct i2c_client* client,
290 struct v4l2_format* f)
291{
292 struct saa6752hs_state *h = i2c_get_clientdata(client);
293 int dist_352, dist_480, dist_720;
294
295 /*
296 FIXME: translate and round width/height into EMPRESS
297 subsample type:
298
299 type | PAL | NTSC
300 ---------------------------
301 SIF | 352x288 | 352x240
302 1/2 D1 | 352x576 | 352x480
303 2/3 D1 | 480x576 | 480x480
304 D1 | 720x576 | 720x480
305 */
306
307 dist_352 = abs(f->fmt.pix.width - 352);
308 dist_480 = abs(f->fmt.pix.width - 480);
309 dist_720 = abs(f->fmt.pix.width - 720);
310 if (dist_720 < dist_480) {
311 f->fmt.pix.width = 720;
312 f->fmt.pix.height = 576;
313 h->video_format = SAA6752HS_VF_D1;
314 }
315 else if (dist_480 < dist_352) {
316 f->fmt.pix.width = 480;
317 f->fmt.pix.height = 576;
318 h->video_format = SAA6752HS_VF_2_3_D1;
319 }
320 else {
321 f->fmt.pix.width = 352;
322 if (abs(f->fmt.pix.height - 576) <
323 abs(f->fmt.pix.height - 288)) {
324 f->fmt.pix.height = 576;
325 h->video_format = SAA6752HS_VF_1_2_D1;
326 }
327 else {
328 f->fmt.pix.height = 288;
329 h->video_format = SAA6752HS_VF_SIF;
330 }
331 }
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Hans Verkuil86b79d662006-06-18 16:40:10 -0300335static int handle_ctrl(struct saa6752hs_mpeg_params *params,
Hans Verkuil4d6b5ae2006-06-26 09:31:18 -0300336 struct v4l2_ext_control *ctrl, unsigned int cmd)
Hans Verkuil86b79d662006-06-18 16:40:10 -0300337{
338 int old = 0, new;
Hans Verkuil4d6b5ae2006-06-26 09:31:18 -0300339 int set = (cmd == VIDIOC_S_EXT_CTRLS);
Hans Verkuil86b79d662006-06-18 16:40:10 -0300340
341 new = ctrl->value;
342 switch (ctrl->id) {
343 case V4L2_CID_MPEG_STREAM_TYPE:
344 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
345 if (set && new != old)
346 return -ERANGE;
347 new = old;
348 break;
349 case V4L2_CID_MPEG_STREAM_PID_PMT:
350 old = params->ts_pid_pmt;
351 if (set && new > MPEG_PID_MAX)
352 return -ERANGE;
353 if (new > MPEG_PID_MAX)
354 new = MPEG_PID_MAX;
355 params->ts_pid_pmt = new;
356 break;
357 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
358 old = params->ts_pid_audio;
359 if (set && new > MPEG_PID_MAX)
360 return -ERANGE;
361 if (new > MPEG_PID_MAX)
362 new = MPEG_PID_MAX;
363 params->ts_pid_audio = new;
364 break;
365 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
366 old = params->ts_pid_video;
367 if (set && new > MPEG_PID_MAX)
368 return -ERANGE;
369 if (new > MPEG_PID_MAX)
370 new = MPEG_PID_MAX;
371 params->ts_pid_video = new;
372 break;
373 case V4L2_CID_MPEG_STREAM_PID_PCR:
374 old = params->ts_pid_pcr;
375 if (set && new > MPEG_PID_MAX)
376 return -ERANGE;
377 if (new > MPEG_PID_MAX)
378 new = MPEG_PID_MAX;
379 params->ts_pid_pcr = new;
380 break;
381 case V4L2_CID_MPEG_AUDIO_ENCODING:
382 old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
383 if (set && new != old)
384 return -ERANGE;
385 new = old;
386 break;
387 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
388 old = params->au_l2_bitrate;
389 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
390 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
391 return -ERANGE;
392 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
393 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
394 else
395 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
396 params->au_l2_bitrate = new;
397 break;
398 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
399 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
400 if (set && new != old)
401 return -ERANGE;
402 new = old;
403 break;
404 case V4L2_CID_MPEG_VIDEO_ENCODING:
405 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
406 if (set && new != old)
407 return -ERANGE;
408 new = old;
409 break;
410 case V4L2_CID_MPEG_VIDEO_ASPECT:
411 old = params->vi_aspect;
412 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
413 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
414 return -ERANGE;
415 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
416 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
417 params->vi_aspect = new;
418 break;
419 case V4L2_CID_MPEG_VIDEO_BITRATE:
420 old = params->vi_bitrate * 1000;
421 new = 1000 * (new / 1000);
422 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
423 return -ERANGE;
424 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
425 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
426 params->vi_bitrate = new / 1000;
427 break;
428 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
429 old = params->vi_bitrate_peak * 1000;
430 new = 1000 * (new / 1000);
431 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
432 return -ERANGE;
433 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
434 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
435 params->vi_bitrate_peak = new / 1000;
436 break;
437 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
438 old = params->vi_bitrate_mode;
439 params->vi_bitrate_mode = new;
440 break;
441 default:
442 return -EINVAL;
443 }
444 if (cmd == VIDIOC_G_EXT_CTRLS)
445 ctrl->value = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 else
Hans Verkuil86b79d662006-06-18 16:40:10 -0300447 ctrl->value = new;
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static int saa6752hs_init(struct i2c_client* client)
452{
453 unsigned char buf[9], buf2[4];
454 struct saa6752hs_state *h;
455 u32 crc;
456 unsigned char localPAT[256];
457 unsigned char localPMT[256];
458
459 h = i2c_get_clientdata(client);
460
Robert W. Boone9b715212005-11-08 21:36:45 -0800461 /* Set video format - must be done first as it resets other settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 buf[0] = 0x41;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700463 buf[1] = h->video_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 i2c_master_send(client, buf, 2);
465
Robert W. Boone9b715212005-11-08 21:36:45 -0800466 /* Set number of lines in input signal */
467 buf[0] = 0x40;
468 buf[1] = 0x00;
469 if (h->standard & V4L2_STD_525_60)
470 buf[1] = 0x01;
471 i2c_master_send(client, buf, 2);
472
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800473 /* set bitrate */
474 saa6752hs_set_bitrate(client, &h->params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Robert W. Boone9b715212005-11-08 21:36:45 -0800476 /* Set GOP structure {3, 13} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 buf[0] = 0x72;
478 buf[1] = 0x03;
479 buf[2] = 0x0D;
480 i2c_master_send(client,buf,3);
481
Trent Piepho657de3c2006-06-20 00:30:57 -0300482 /* Set minimum Q-scale {4} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 buf[0] = 0x82;
484 buf[1] = 0x04;
485 i2c_master_send(client,buf,2);
486
Trent Piepho657de3c2006-06-20 00:30:57 -0300487 /* Set maximum Q-scale {12} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 buf[0] = 0x83;
489 buf[1] = 0x0C;
490 i2c_master_send(client,buf,2);
491
Trent Piepho657de3c2006-06-20 00:30:57 -0300492 /* Set Output Protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 buf[0] = 0xD0;
494 buf[1] = 0x81;
495 i2c_master_send(client,buf,2);
496
Trent Piepho657de3c2006-06-20 00:30:57 -0300497 /* Set video output stream format {TS} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 buf[0] = 0xB0;
499 buf[1] = 0x05;
500 i2c_master_send(client,buf,2);
501
502 /* compute PAT */
503 memcpy(localPAT, PAT, sizeof(PAT));
504 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
505 localPAT[18] = h->params.ts_pid_pmt & 0xff;
506 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
507 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
508 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
509 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
510 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
511
512 /* compute PMT */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800513 memcpy(localPMT, PMT, sizeof(PMT));
514 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
515 localPMT[4] = h->params.ts_pid_pmt & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
517 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
518 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
519 localPMT[21] = h->params.ts_pid_video & 0xFF;
520 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
521 localPMT[26] = h->params.ts_pid_audio & 0xFF;
522 crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
523 localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
524 localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
525 localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
526 localPMT[sizeof(PMT) - 1] = crc & 0xFF;
527
Trent Piepho657de3c2006-06-20 00:30:57 -0300528 /* Set Audio PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 buf[0] = 0xC1;
530 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
531 buf[2] = h->params.ts_pid_audio & 0xFF;
532 i2c_master_send(client,buf,3);
533
Robert W. Boone9b715212005-11-08 21:36:45 -0800534 /* Set Video PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 buf[0] = 0xC0;
536 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
537 buf[2] = h->params.ts_pid_video & 0xFF;
538 i2c_master_send(client,buf,3);
539
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800540 /* Set PCR PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 buf[0] = 0xC4;
542 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
543 buf[2] = h->params.ts_pid_pcr & 0xFF;
544 i2c_master_send(client,buf,3);
545
Robert W. Boone9b715212005-11-08 21:36:45 -0800546 /* Send SI tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 i2c_master_send(client,localPAT,sizeof(PAT));
548 i2c_master_send(client,localPMT,sizeof(PMT));
549
Robert W. Boone9b715212005-11-08 21:36:45 -0800550 /* mute then unmute audio. This removes buzzing artefacts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 buf[0] = 0xa4;
552 buf[1] = 1;
553 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800554 buf[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 i2c_master_send(client, buf, 2);
556
Robert W. Boone9b715212005-11-08 21:36:45 -0800557 /* start it going */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
559
Robert W. Boone9b715212005-11-08 21:36:45 -0800560 /* readout current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 buf[0] = 0xE1;
562 buf[1] = 0xA7;
563 buf[2] = 0xFE;
564 buf[3] = 0x82;
565 buf[4] = 0xB0;
566 i2c_master_send(client, buf, 5);
567 i2c_master_recv(client, buf2, 4);
568
Robert W. Boone9b715212005-11-08 21:36:45 -0800569 /* change aspect ratio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 buf[0] = 0xE0;
571 buf[1] = 0xA7;
572 buf[2] = 0xFE;
573 buf[3] = 0x82;
574 buf[4] = 0xB0;
575 buf[5] = buf2[0];
Hans Verkuil86b79d662006-06-18 16:40:10 -0300576 switch(h->params.vi_aspect) {
577 case V4L2_MPEG_VIDEO_ASPECT_16x9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 buf[6] = buf2[1] | 0x40;
579 break;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300580 case V4L2_MPEG_VIDEO_ASPECT_4x3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 default:
582 buf[6] = buf2[1] & 0xBF;
583 break;
584 break;
585 }
586 buf[7] = buf2[2];
587 buf[8] = buf2[3];
588 i2c_master_send(client, buf, 9);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
594{
595 struct saa6752hs_state *h;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Panagiotis Issaris74081872006-01-11 19:40:56 -0200598 if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800599 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 h->client = client_template;
601 h->params = param_defaults;
602 h->client.adapter = adap;
603 h->client.addr = addr;
604
Robert W. Boone9b715212005-11-08 21:36:45 -0800605 /* Assume 625 input lines */
606 h->standard = 0;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 i2c_set_clientdata(&h->client, h);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800609 i2c_attach_client(&h->client);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800610
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200611 v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614}
615
616static int saa6752hs_probe(struct i2c_adapter *adap)
617{
618 if (adap->class & I2C_CLASS_TV_ANALOG)
619 return i2c_probe(adap, &addr_data, saa6752hs_attach);
620 return 0;
621}
622
623static int saa6752hs_detach(struct i2c_client *client)
624{
625 struct saa6752hs_state *h;
626
627 h = i2c_get_clientdata(client);
628 i2c_detach_client(client);
629 kfree(h);
630 return 0;
631}
632
633static int
634saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
635{
636 struct saa6752hs_state *h = i2c_get_clientdata(client);
Hans Verkuil86b79d662006-06-18 16:40:10 -0300637 struct v4l2_ext_controls *ctrls = arg;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300638 struct saa6752hs_mpeg_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int err = 0;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300640 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800642 switch (cmd) {
Hans Verkuil86b79d662006-06-18 16:40:10 -0300643 case VIDIOC_S_EXT_CTRLS:
644 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
645 return -EINVAL;
646 if (ctrls->count == 0) {
647 /* apply settings and start encoder */
648 saa6752hs_init(client);
649 break;
650 }
651 /* fall through */
652 case VIDIOC_TRY_EXT_CTRLS:
653 case VIDIOC_G_EXT_CTRLS:
654 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
655 return -EINVAL;
656 params = h->params;
657 for (i = 0; i < ctrls->count; i++) {
658 if ((err = handle_ctrl(&params, ctrls->controls + i, cmd))) {
659 ctrls->error_idx = i;
660 return err;
661 }
662 }
663 h->params = params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700665 case VIDIOC_G_FMT:
666 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800667 struct v4l2_format *f = arg;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700668
669 if (h->video_format == SAA6752HS_VF_UNKNOWN)
670 h->video_format = SAA6752HS_VF_D1;
671 f->fmt.pix.width =
672 v4l2_format_table[h->video_format].fmt.pix.width;
673 f->fmt.pix.height =
674 v4l2_format_table[h->video_format].fmt.pix.height;
675 break ;
676 }
677 case VIDIOC_S_FMT:
678 {
679 struct v4l2_format *f = arg;
680
681 saa6752hs_set_subsampling(client, f);
682 break;
683 }
Robert W. Boone9b715212005-11-08 21:36:45 -0800684 case VIDIOC_S_STD:
685 h->standard = *((v4l2_std_id *) arg);
686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 default:
688 /* nothing */
689 break;
690 }
691
692 return err;
693}
694
695/* ----------------------------------------------------------------------- */
696
697static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100698 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200699 .name = "saa6752hs",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100700 },
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800701 .id = I2C_DRIVERID_SAA6752HS,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800702 .attach_adapter = saa6752hs_probe,
703 .detach_client = saa6752hs_detach,
704 .command = saa6752hs_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
707static struct i2c_client client_template =
708{
Jean Delvarefae91e72005-08-15 19:57:04 +0200709 .name = "saa6752hs",
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800710 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711};
712
713static int __init saa6752hs_init_module(void)
714{
715 return i2c_add_driver(&driver);
716}
717
718static void __exit saa6752hs_cleanup_module(void)
719{
720 i2c_del_driver(&driver);
721}
722
723module_init(saa6752hs_init_module);
724module_exit(saa6752hs_cleanup_module);
725
726/*
727 * Overrides for Emacs so that we follow Linus's tabbing style.
728 * ---------------------------------------------------------------------------
729 * Local variables:
730 * c-basic-offset: 8
731 * End:
732 */