blob: de7b9e6e932a9b70aec1e53e2ff5d3ff985369c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020012#include <linux/videodev2.h>
13#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/crc32.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
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 */
24static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
Linus Torvalds1da177e2005-04-16 15:20:36 -070025I2C_CLIENT_INSMOD;
26
27MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
28MODULE_AUTHOR("Andrew de Quincey");
29MODULE_LICENSE("GPL");
30
31static struct i2c_driver driver;
32static struct i2c_client client_template;
33
Frederic CAND0a4c9c92005-05-05 16:15:52 -070034enum 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
Hans Verkuil86b79d62006-06-18 16:40:10 -030042struct 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
Frederic CAND0a4c9c92005-05-05 16:15:52 -070059static const struct v4l2_format v4l2_format_table[] =
60{
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -070061 [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}}},
Frederic CAND0a4c9c92005-05-05 16:15:52 -070071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct saa6752hs_state {
74 struct i2c_client client;
Hans Verkuil86b79d62006-06-18 16:40:10 -030075 struct v4l2_mpeg_compression old_params;
76 struct saa6752hs_mpeg_params params;
Frederic CAND0a4c9c92005-05-05 16:15:52 -070077 enum saa6752hs_videoformat video_format;
Robert W. Boone9b715212005-11-08 21:36:45 -080078 v4l2_std_id standard;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81enum saa6752hs_command {
82 SAA6752HS_COMMAND_RESET = 0,
Trent Piepho657de3c2006-06-20 00:30:57 -030083 SAA6752HS_COMMAND_STOP = 1,
84 SAA6752HS_COMMAND_START = 2,
85 SAA6752HS_COMMAND_PAUSE = 3,
86 SAA6752HS_COMMAND_RECONFIGURE = 4,
87 SAA6752HS_COMMAND_SLEEP = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
89
90 SAA6752HS_COMMAND_MAX
91};
92
93/* ---------------------------------------------------------------------- */
94
95static u8 PAT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -080096 0xc2, /* i2c register */
97 0x00, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Robert W. Boone9b715212005-11-08 21:36:45 -080099 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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Robert W. Boone9b715212005-11-08 21:36:45 -0800103 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Robert W. Boone9b715212005-11-08 21:36:45 -0800105 0x00, /* tid(0) */
106 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Robert W. Boone9b715212005-11-08 21:36:45 -0800108 0x00, 0x01, /* transport_stream_id(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Robert W. Boone9b715212005-11-08 21:36:45 -0800110 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Robert W. Boone9b715212005-11-08 21:36:45 -0800112 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Robert W. Boone9b715212005-11-08 21:36:45 -0800114 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Robert W. Boone9b715212005-11-08 21:36:45 -0800116 0xe0, 0x00, /* PMT PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Robert W. Boone9b715212005-11-08 21:36:45 -0800118 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
121static u8 PMT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800122 0xc2, /* i2c register */
123 0x01, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Robert W. Boone9b715212005-11-08 21:36:45 -0800125 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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Robert W. Boone9b715212005-11-08 21:36:45 -0800129 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Robert W. Boone9b715212005-11-08 21:36:45 -0800131 0x02, /* tid(2) */
132 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Robert W. Boone9b715212005-11-08 21:36:45 -0800134 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Robert W. Boone9b715212005-11-08 21:36:45 -0800136 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Robert W. Boone9b715212005-11-08 21:36:45 -0800138 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Robert W. Boone9b715212005-11-08 21:36:45 -0800140 0xe0, 0x00, /* PCR_PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Robert W. Boone9b715212005-11-08 21:36:45 -0800142 0xf0, 0x00, /* program_info_length(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Robert W. Boone9b715212005-11-08 21:36:45 -0800144 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
145 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Robert W. Boone9b715212005-11-08 21:36:45 -0800147 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Hans Verkuil86b79d62006-06-18 16:40:10 -0300150static 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
165static struct v4l2_mpeg_compression old_param_defaults =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194/* ---------------------------------------------------------------------- */
195
196static 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
Robert W. Boone9b715212005-11-08 21:36:45 -0800203 /* execute the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 switch(command) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800205 case SAA6752HS_COMMAND_RESET:
206 buf[0] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208
209 case SAA6752HS_COMMAND_STOP:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800210 buf[0] = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212
213 case SAA6752HS_COMMAND_START:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800214 buf[0] = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
217 case SAA6752HS_COMMAND_PAUSE:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800218 buf[0] = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
220
221 case SAA6752HS_COMMAND_RECONFIGURE:
222 buf[0] = 0x05;
223 break;
224
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800225 case SAA6752HS_COMMAND_SLEEP:
226 buf[0] = 0x06;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800229 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 buf[0] = 0x07;
231 break;
232
233 default:
234 return -EINVAL;
235 }
236
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800237 /* set it and wait for it to be so */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 i2c_master_send(client, buf, 1);
239 timeout = jiffies + HZ * 3;
240 for (;;) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800241 /* get the current status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 buf[0] = 0x10;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800243 i2c_master_send(client, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 msleep(10);
254 }
255
Robert W. Boone9b715212005-11-08 21:36:45 -0800256 /* delay a bit to let encoder settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 msleep(50);
258
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262
263static int saa6752hs_set_bitrate(struct i2c_client* client,
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300264 struct saa6752hs_mpeg_params* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800266 u8 buf[3];
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300267 int tot_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Robert W. Boone9b715212005-11-08 21:36:45 -0800269 /* set the bitrate mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 buf[0] = 0x71;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300271 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 i2c_master_send(client, buf, 2);
273
Robert W. Boone9b715212005-11-08 21:36:45 -0800274 /* set the video bitrate */
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300275 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800276 /* set the target bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 buf[0] = 0x80;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300278 buf[1] = params->vi_bitrate >> 8;
279 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 i2c_master_send(client, buf, 3);
281
Robert W. Boone9b715212005-11-08 21:36:45 -0800282 /* set the max bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300284 buf[1] = params->vi_bitrate_peak >> 8;
285 buf[2] = params->vi_bitrate_peak & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300287 tot_bitrate = params->vi_bitrate_peak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else {
Robert W. Boone9b715212005-11-08 21:36:45 -0800289 /* set the target bitrate (no max bitrate for CBR) */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800290 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300291 buf[1] = params->vi_bitrate >> 8;
292 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300294 tot_bitrate = params->vi_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Robert W. Boone9b715212005-11-08 21:36:45 -0800297 /* set the audio bitrate */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800298 buf[0] = 0x94;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300299 buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300301 tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384;
302
303 /* Note: the total max bitrate is determined by adding the video and audio
304 bitrates together and also adding an extra 768kbit/s to stay on the
305 safe side. If more control should be required, then an extra MPEG control
306 should be added. */
307 tot_bitrate += 768;
308 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
309 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Robert W. Boone9b715212005-11-08 21:36:45 -0800311 /* set the total bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 buf[0] = 0xb1;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300313 buf[1] = tot_bitrate >> 8;
314 buf[2] = tot_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 i2c_master_send(client, buf, 3);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700320static void saa6752hs_set_subsampling(struct i2c_client* client,
321 struct v4l2_format* f)
322{
323 struct saa6752hs_state *h = i2c_get_clientdata(client);
324 int dist_352, dist_480, dist_720;
325
326 /*
327 FIXME: translate and round width/height into EMPRESS
328 subsample type:
329
330 type | PAL | NTSC
331 ---------------------------
332 SIF | 352x288 | 352x240
333 1/2 D1 | 352x576 | 352x480
334 2/3 D1 | 480x576 | 480x480
335 D1 | 720x576 | 720x480
336 */
337
338 dist_352 = abs(f->fmt.pix.width - 352);
339 dist_480 = abs(f->fmt.pix.width - 480);
340 dist_720 = abs(f->fmt.pix.width - 720);
341 if (dist_720 < dist_480) {
342 f->fmt.pix.width = 720;
343 f->fmt.pix.height = 576;
344 h->video_format = SAA6752HS_VF_D1;
345 }
346 else if (dist_480 < dist_352) {
347 f->fmt.pix.width = 480;
348 f->fmt.pix.height = 576;
349 h->video_format = SAA6752HS_VF_2_3_D1;
350 }
351 else {
352 f->fmt.pix.width = 352;
353 if (abs(f->fmt.pix.height - 576) <
354 abs(f->fmt.pix.height - 288)) {
355 f->fmt.pix.height = 576;
356 h->video_format = SAA6752HS_VF_1_2_D1;
357 }
358 else {
359 f->fmt.pix.height = 288;
360 h->video_format = SAA6752HS_VF_SIF;
361 }
362 }
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Hans Verkuil86b79d62006-06-18 16:40:10 -0300366static void saa6752hs_old_set_params(struct i2c_client* client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct v4l2_mpeg_compression* params)
368{
369 struct saa6752hs_state *h = i2c_get_clientdata(client);
370
371 /* check PIDs */
Hans Verkuil86b79d62006-06-18 16:40:10 -0300372 if (params->ts_pid_pmt <= MPEG_PID_MAX) {
373 h->old_params.ts_pid_pmt = params->ts_pid_pmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 h->params.ts_pid_pmt = params->ts_pid_pmt;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300375 }
376 if (params->ts_pid_pcr <= MPEG_PID_MAX) {
377 h->old_params.ts_pid_pcr = params->ts_pid_pcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 h->params.ts_pid_pcr = params->ts_pid_pcr;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300379 }
380 if (params->ts_pid_video <= MPEG_PID_MAX) {
381 h->old_params.ts_pid_video = params->ts_pid_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 h->params.ts_pid_video = params->ts_pid_video;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300383 }
384 if (params->ts_pid_audio <= MPEG_PID_MAX) {
385 h->old_params.ts_pid_audio = params->ts_pid_audio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 h->params.ts_pid_audio = params->ts_pid_audio;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* check bitrate parameters */
390 if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
Hans Verkuil86b79d62006-06-18 16:40:10 -0300391 (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) {
392 h->old_params.vi_bitrate.mode = params->vi_bitrate.mode;
393 h->params.vi_bitrate_mode = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ?
394 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR : V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300397 h->old_params.st_bitrate.target = params->st_bitrate.target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300399 h->old_params.vi_bitrate.target = params->vi_bitrate.target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300401 h->old_params.vi_bitrate.max = params->vi_bitrate.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300403 h->old_params.au_bitrate.target = params->au_bitrate.target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* aspect ratio */
406 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
Hans Verkuil86b79d62006-06-18 16:40:10 -0300407 params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) {
408 h->old_params.vi_aspect_ratio = params->vi_aspect_ratio;
409 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3)
410 h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
411 else
412 h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_16x9;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* range checks */
Hans Verkuil86b79d62006-06-18 16:40:10 -0300416 if (h->old_params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
417 h->old_params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
418 if (h->old_params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
419 h->old_params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
420 if (h->old_params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
421 h->old_params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
422 h->params.vi_bitrate = params->vi_bitrate.target;
423 h->params.vi_bitrate_peak = params->vi_bitrate.max;
424 if (h->old_params.au_bitrate.target <= 256) {
425 h->old_params.au_bitrate.target = 256;
426 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
427 }
428 else {
429 h->old_params.au_bitrate.target = 384;
430 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
431 }
432}
433
434static int handle_ctrl(struct saa6752hs_mpeg_params *params,
435 struct v4l2_ext_control *ctrl, int cmd)
436{
437 int old = 0, new;
438 int set = cmd == VIDIOC_S_EXT_CTRLS;
439
440 new = ctrl->value;
441 switch (ctrl->id) {
442 case V4L2_CID_MPEG_STREAM_TYPE:
443 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
444 if (set && new != old)
445 return -ERANGE;
446 new = old;
447 break;
448 case V4L2_CID_MPEG_STREAM_PID_PMT:
449 old = params->ts_pid_pmt;
450 if (set && new > MPEG_PID_MAX)
451 return -ERANGE;
452 if (new > MPEG_PID_MAX)
453 new = MPEG_PID_MAX;
454 params->ts_pid_pmt = new;
455 break;
456 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
457 old = params->ts_pid_audio;
458 if (set && new > MPEG_PID_MAX)
459 return -ERANGE;
460 if (new > MPEG_PID_MAX)
461 new = MPEG_PID_MAX;
462 params->ts_pid_audio = new;
463 break;
464 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
465 old = params->ts_pid_video;
466 if (set && new > MPEG_PID_MAX)
467 return -ERANGE;
468 if (new > MPEG_PID_MAX)
469 new = MPEG_PID_MAX;
470 params->ts_pid_video = new;
471 break;
472 case V4L2_CID_MPEG_STREAM_PID_PCR:
473 old = params->ts_pid_pcr;
474 if (set && new > MPEG_PID_MAX)
475 return -ERANGE;
476 if (new > MPEG_PID_MAX)
477 new = MPEG_PID_MAX;
478 params->ts_pid_pcr = new;
479 break;
480 case V4L2_CID_MPEG_AUDIO_ENCODING:
481 old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
482 if (set && new != old)
483 return -ERANGE;
484 new = old;
485 break;
486 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
487 old = params->au_l2_bitrate;
488 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
489 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
490 return -ERANGE;
491 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
492 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
493 else
494 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
495 params->au_l2_bitrate = new;
496 break;
497 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
498 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
499 if (set && new != old)
500 return -ERANGE;
501 new = old;
502 break;
503 case V4L2_CID_MPEG_VIDEO_ENCODING:
504 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
505 if (set && new != old)
506 return -ERANGE;
507 new = old;
508 break;
509 case V4L2_CID_MPEG_VIDEO_ASPECT:
510 old = params->vi_aspect;
511 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
512 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
513 return -ERANGE;
514 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
515 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
516 params->vi_aspect = new;
517 break;
518 case V4L2_CID_MPEG_VIDEO_BITRATE:
519 old = params->vi_bitrate * 1000;
520 new = 1000 * (new / 1000);
521 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
522 return -ERANGE;
523 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
524 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
525 params->vi_bitrate = new / 1000;
526 break;
527 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
528 old = params->vi_bitrate_peak * 1000;
529 new = 1000 * (new / 1000);
530 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
531 return -ERANGE;
532 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
533 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
534 params->vi_bitrate_peak = new / 1000;
535 break;
536 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
537 old = params->vi_bitrate_mode;
538 params->vi_bitrate_mode = new;
539 break;
540 default:
541 return -EINVAL;
542 }
543 if (cmd == VIDIOC_G_EXT_CTRLS)
544 ctrl->value = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 else
Hans Verkuil86b79d62006-06-18 16:40:10 -0300546 ctrl->value = new;
547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static int saa6752hs_init(struct i2c_client* client)
551{
552 unsigned char buf[9], buf2[4];
553 struct saa6752hs_state *h;
554 u32 crc;
555 unsigned char localPAT[256];
556 unsigned char localPMT[256];
557
558 h = i2c_get_clientdata(client);
559
Robert W. Boone9b715212005-11-08 21:36:45 -0800560 /* Set video format - must be done first as it resets other settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 buf[0] = 0x41;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700562 buf[1] = h->video_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 i2c_master_send(client, buf, 2);
564
Robert W. Boone9b715212005-11-08 21:36:45 -0800565 /* Set number of lines in input signal */
566 buf[0] = 0x40;
567 buf[1] = 0x00;
568 if (h->standard & V4L2_STD_525_60)
569 buf[1] = 0x01;
570 i2c_master_send(client, buf, 2);
571
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800572 /* set bitrate */
573 saa6752hs_set_bitrate(client, &h->params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Robert W. Boone9b715212005-11-08 21:36:45 -0800575 /* Set GOP structure {3, 13} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 buf[0] = 0x72;
577 buf[1] = 0x03;
578 buf[2] = 0x0D;
579 i2c_master_send(client,buf,3);
580
Trent Piepho657de3c2006-06-20 00:30:57 -0300581 /* Set minimum Q-scale {4} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 buf[0] = 0x82;
583 buf[1] = 0x04;
584 i2c_master_send(client,buf,2);
585
Trent Piepho657de3c2006-06-20 00:30:57 -0300586 /* Set maximum Q-scale {12} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 buf[0] = 0x83;
588 buf[1] = 0x0C;
589 i2c_master_send(client,buf,2);
590
Trent Piepho657de3c2006-06-20 00:30:57 -0300591 /* Set Output Protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 buf[0] = 0xD0;
593 buf[1] = 0x81;
594 i2c_master_send(client,buf,2);
595
Trent Piepho657de3c2006-06-20 00:30:57 -0300596 /* Set video output stream format {TS} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 buf[0] = 0xB0;
598 buf[1] = 0x05;
599 i2c_master_send(client,buf,2);
600
601 /* compute PAT */
602 memcpy(localPAT, PAT, sizeof(PAT));
603 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
604 localPAT[18] = h->params.ts_pid_pmt & 0xff;
605 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
606 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
607 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
608 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
609 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
610
611 /* compute PMT */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800612 memcpy(localPMT, PMT, sizeof(PMT));
613 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
614 localPMT[4] = h->params.ts_pid_pmt & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
616 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
617 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
618 localPMT[21] = h->params.ts_pid_video & 0xFF;
619 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
620 localPMT[26] = h->params.ts_pid_audio & 0xFF;
621 crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
622 localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
623 localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
624 localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
625 localPMT[sizeof(PMT) - 1] = crc & 0xFF;
626
Trent Piepho657de3c2006-06-20 00:30:57 -0300627 /* Set Audio PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 buf[0] = 0xC1;
629 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
630 buf[2] = h->params.ts_pid_audio & 0xFF;
631 i2c_master_send(client,buf,3);
632
Robert W. Boone9b715212005-11-08 21:36:45 -0800633 /* Set Video PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 buf[0] = 0xC0;
635 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
636 buf[2] = h->params.ts_pid_video & 0xFF;
637 i2c_master_send(client,buf,3);
638
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800639 /* Set PCR PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 buf[0] = 0xC4;
641 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
642 buf[2] = h->params.ts_pid_pcr & 0xFF;
643 i2c_master_send(client,buf,3);
644
Robert W. Boone9b715212005-11-08 21:36:45 -0800645 /* Send SI tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 i2c_master_send(client,localPAT,sizeof(PAT));
647 i2c_master_send(client,localPMT,sizeof(PMT));
648
Robert W. Boone9b715212005-11-08 21:36:45 -0800649 /* mute then unmute audio. This removes buzzing artefacts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 buf[0] = 0xa4;
651 buf[1] = 1;
652 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800653 buf[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 i2c_master_send(client, buf, 2);
655
Robert W. Boone9b715212005-11-08 21:36:45 -0800656 /* start it going */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
658
Robert W. Boone9b715212005-11-08 21:36:45 -0800659 /* readout current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 buf[0] = 0xE1;
661 buf[1] = 0xA7;
662 buf[2] = 0xFE;
663 buf[3] = 0x82;
664 buf[4] = 0xB0;
665 i2c_master_send(client, buf, 5);
666 i2c_master_recv(client, buf2, 4);
667
Robert W. Boone9b715212005-11-08 21:36:45 -0800668 /* change aspect ratio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 buf[0] = 0xE0;
670 buf[1] = 0xA7;
671 buf[2] = 0xFE;
672 buf[3] = 0x82;
673 buf[4] = 0xB0;
674 buf[5] = buf2[0];
Hans Verkuil86b79d62006-06-18 16:40:10 -0300675 switch(h->params.vi_aspect) {
676 case V4L2_MPEG_VIDEO_ASPECT_16x9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 buf[6] = buf2[1] | 0x40;
678 break;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300679 case V4L2_MPEG_VIDEO_ASPECT_4x3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 default:
681 buf[6] = buf2[1] & 0xBF;
682 break;
683 break;
684 }
685 buf[7] = buf2[2];
686 buf[8] = buf2[3];
687 i2c_master_send(client, buf, 9);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
692static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
693{
694 struct saa6752hs_state *h;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Panagiotis Issaris74081872006-01-11 19:40:56 -0200697 if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800698 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 h->client = client_template;
700 h->params = param_defaults;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300701 h->old_params = old_param_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 h->client.adapter = adap;
703 h->client.addr = addr;
704
Robert W. Boone9b715212005-11-08 21:36:45 -0800705 /* Assume 625 input lines */
706 h->standard = 0;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 i2c_set_clientdata(&h->client, h);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800709 i2c_attach_client(&h->client);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800710
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200711 v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
716static int saa6752hs_probe(struct i2c_adapter *adap)
717{
718 if (adap->class & I2C_CLASS_TV_ANALOG)
719 return i2c_probe(adap, &addr_data, saa6752hs_attach);
720 return 0;
721}
722
723static int saa6752hs_detach(struct i2c_client *client)
724{
725 struct saa6752hs_state *h;
726
727 h = i2c_get_clientdata(client);
728 i2c_detach_client(client);
729 kfree(h);
730 return 0;
731}
732
733static int
734saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
735{
736 struct saa6752hs_state *h = i2c_get_clientdata(client);
Hans Verkuil86b79d62006-06-18 16:40:10 -0300737 struct v4l2_ext_controls *ctrls = arg;
738 struct v4l2_mpeg_compression *old_params = arg;
739 struct saa6752hs_mpeg_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int err = 0;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300741 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800743 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case VIDIOC_S_MPEGCOMP:
Hans Verkuil86b79d62006-06-18 16:40:10 -0300745 if (NULL == old_params) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* apply settings and start encoder */
747 saa6752hs_init(client);
748 break;
749 }
Hans Verkuil86b79d62006-06-18 16:40:10 -0300750 saa6752hs_old_set_params(client, old_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* fall through */
752 case VIDIOC_G_MPEGCOMP:
Hans Verkuil86b79d62006-06-18 16:40:10 -0300753 *old_params = h->old_params;
754 break;
755 case VIDIOC_S_EXT_CTRLS:
756 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
757 return -EINVAL;
758 if (ctrls->count == 0) {
759 /* apply settings and start encoder */
760 saa6752hs_init(client);
761 break;
762 }
763 /* fall through */
764 case VIDIOC_TRY_EXT_CTRLS:
765 case VIDIOC_G_EXT_CTRLS:
766 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
767 return -EINVAL;
768 params = h->params;
769 for (i = 0; i < ctrls->count; i++) {
770 if ((err = handle_ctrl(&params, ctrls->controls + i, cmd))) {
771 ctrls->error_idx = i;
772 return err;
773 }
774 }
775 h->params = params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700777 case VIDIOC_G_FMT:
778 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800779 struct v4l2_format *f = arg;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700780
781 if (h->video_format == SAA6752HS_VF_UNKNOWN)
782 h->video_format = SAA6752HS_VF_D1;
783 f->fmt.pix.width =
784 v4l2_format_table[h->video_format].fmt.pix.width;
785 f->fmt.pix.height =
786 v4l2_format_table[h->video_format].fmt.pix.height;
787 break ;
788 }
789 case VIDIOC_S_FMT:
790 {
791 struct v4l2_format *f = arg;
792
793 saa6752hs_set_subsampling(client, f);
794 break;
795 }
Robert W. Boone9b715212005-11-08 21:36:45 -0800796 case VIDIOC_S_STD:
797 h->standard = *((v4l2_std_id *) arg);
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 default:
800 /* nothing */
801 break;
802 }
803
804 return err;
805}
806
807/* ----------------------------------------------------------------------- */
808
809static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100810 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200811 .name = "saa6752hs",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100812 },
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800813 .id = I2C_DRIVERID_SAA6752HS,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800814 .attach_adapter = saa6752hs_probe,
815 .detach_client = saa6752hs_detach,
816 .command = saa6752hs_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817};
818
819static struct i2c_client client_template =
820{
Jean Delvarefae91e72005-08-15 19:57:04 +0200821 .name = "saa6752hs",
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800822 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823};
824
825static int __init saa6752hs_init_module(void)
826{
827 return i2c_add_driver(&driver);
828}
829
830static void __exit saa6752hs_cleanup_module(void)
831{
832 i2c_del_driver(&driver);
833}
834
835module_init(saa6752hs_init_module);
836module_exit(saa6752hs_cleanup_module);
837
838/*
839 * Overrides for Emacs so that we follow Linus's tabbing style.
840 * ---------------------------------------------------------------------------
841 * Local variables:
842 * c-basic-offset: 8
843 * End:
844 */