aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/saa7134/saa6752hs.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/saa7134/saa6752hs.c')
-rw-r--r--drivers/media/video/saa7134/saa6752hs.c543
1 files changed, 543 insertions, 0 deletions
diff --git a/drivers/media/video/saa7134/saa6752hs.c b/drivers/media/video/saa7134/saa6752hs.c
new file mode 100644
index 000000000000..cee13584c9cf
--- /dev/null
+++ b/drivers/media/video/saa7134/saa6752hs.c
@@ -0,0 +1,543 @@
1#include <linux/module.h>
2#include <linux/kernel.h>
3#include <linux/sched.h>
4#include <linux/string.h>
5#include <linux/timer.h>
6#include <linux/delay.h>
7#include <linux/errno.h>
8#include <linux/slab.h>
9#include <linux/poll.h>
10#include <linux/i2c.h>
11#include <linux/types.h>
12#include <linux/videodev.h>
13#include <linux/init.h>
14#include <linux/crc32.h>
15
16#include <media/id.h>
17
18#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
19#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
20#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
21#define MPEG_PID_MAX ((1 << 14) - 1)
22
23/* Addresses to scan */
24static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
25static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
26I2C_CLIENT_INSMOD;
27
28MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
29MODULE_AUTHOR("Andrew de Quincey");
30MODULE_LICENSE("GPL");
31
32static struct i2c_driver driver;
33static struct i2c_client client_template;
34
35struct saa6752hs_state {
36 struct i2c_client client;
37 struct v4l2_mpeg_compression params;
38};
39
40enum saa6752hs_command {
41 SAA6752HS_COMMAND_RESET = 0,
42 SAA6752HS_COMMAND_STOP = 1,
43 SAA6752HS_COMMAND_START = 2,
44 SAA6752HS_COMMAND_PAUSE = 3,
45 SAA6752HS_COMMAND_RECONFIGURE = 4,
46 SAA6752HS_COMMAND_SLEEP = 5,
47 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
48
49 SAA6752HS_COMMAND_MAX
50};
51
52/* ---------------------------------------------------------------------- */
53
54static u8 PAT[] = {
55 0xc2, // i2c register
56 0x00, // table number for encoder
57
58 0x47, // sync
59 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0)
60 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
61
62 0x00, // PSI pointer to start of table
63
64 0x00, // tid(0)
65 0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
66
67 0x00, 0x01, // transport_stream_id(1)
68
69 0xc1, // version_number(0), current_next_indicator(1)
70
71 0x00, 0x00, // section_number(0), last_section_number(0)
72
73 0x00, 0x01, // program_number(1)
74
75 0xe0, 0x00, // PMT PID
76
77 0x00, 0x00, 0x00, 0x00 // CRC32
78};
79
80static u8 PMT[] = {
81 0xc2, // i2c register
82 0x01, // table number for encoder
83
84 0x47, // sync
85 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid
86 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
87
88 0x00, // PSI pointer to start of table
89
90 0x02, // tid(2)
91 0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
92
93 0x00, 0x01, // program_number(1)
94
95 0xc1, // version_number(0), current_next_indicator(1)
96
97 0x00, 0x00, // section_number(0), last_section_number(0)
98
99 0xe0, 0x00, // PCR_PID
100
101 0xf0, 0x00, // program_info_length(0)
102
103 0x02, 0xe0, 0x00, 0xf0, 0x00, // video stream type(2), pid
104 0x04, 0xe0, 0x00, 0xf0, 0x00, // audio stream type(4), pid
105
106 0x00, 0x00, 0x00, 0x00 // CRC32
107};
108
109static struct v4l2_mpeg_compression param_defaults =
110{
111 .st_type = V4L2_MPEG_TS_2,
112 .st_bitrate = {
113 .mode = V4L2_BITRATE_CBR,
114 .target = 7000,
115 },
116
117 .ts_pid_pmt = 16,
118 .ts_pid_video = 260,
119 .ts_pid_audio = 256,
120 .ts_pid_pcr = 259,
121
122 .vi_type = V4L2_MPEG_VI_2,
123 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
124 .vi_bitrate = {
125 .mode = V4L2_BITRATE_VBR,
126 .target = 4000,
127 .max = 6000,
128 },
129
130 .au_type = V4L2_MPEG_AU_2_II,
131 .au_bitrate = {
132 .mode = V4L2_BITRATE_CBR,
133 .target = 256,
134 },
135
136#if 0
137 /* FIXME: size? via S_FMT? */
138 .video_format = MPEG_VIDEO_FORMAT_D1,
139#endif
140};
141
142/* ---------------------------------------------------------------------- */
143
144static int saa6752hs_chip_command(struct i2c_client* client,
145 enum saa6752hs_command command)
146{
147 unsigned char buf[3];
148 unsigned long timeout;
149 int status = 0;
150
151 // execute the command
152 switch(command) {
153 case SAA6752HS_COMMAND_RESET:
154 buf[0] = 0x00;
155 break;
156
157 case SAA6752HS_COMMAND_STOP:
158 buf[0] = 0x03;
159 break;
160
161 case SAA6752HS_COMMAND_START:
162 buf[0] = 0x02;
163 break;
164
165 case SAA6752HS_COMMAND_PAUSE:
166 buf[0] = 0x04;
167 break;
168
169 case SAA6752HS_COMMAND_RECONFIGURE:
170 buf[0] = 0x05;
171 break;
172
173 case SAA6752HS_COMMAND_SLEEP:
174 buf[0] = 0x06;
175 break;
176
177 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
178 buf[0] = 0x07;
179 break;
180
181 default:
182 return -EINVAL;
183 }
184
185 // set it and wait for it to be so
186 i2c_master_send(client, buf, 1);
187 timeout = jiffies + HZ * 3;
188 for (;;) {
189 // get the current status
190 buf[0] = 0x10;
191 i2c_master_send(client, buf, 1);
192 i2c_master_recv(client, buf, 1);
193
194 if (!(buf[0] & 0x20))
195 break;
196 if (time_after(jiffies,timeout)) {
197 status = -ETIMEDOUT;
198 break;
199 }
200
201 // wait a bit
202 msleep(10);
203 }
204
205 // delay a bit to let encoder settle
206 msleep(50);
207
208 // done
209 return status;
210}
211
212
213static int saa6752hs_set_bitrate(struct i2c_client* client,
214 struct v4l2_mpeg_compression* params)
215{
216 u8 buf[3];
217
218 // set the bitrate mode
219 buf[0] = 0x71;
220 buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1;
221 i2c_master_send(client, buf, 2);
222
223 // set the video bitrate
224 if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) {
225 // set the target bitrate
226 buf[0] = 0x80;
227 buf[1] = params->vi_bitrate.target >> 8;
228 buf[2] = params->vi_bitrate.target & 0xff;
229 i2c_master_send(client, buf, 3);
230
231 // set the max bitrate
232 buf[0] = 0x81;
233 buf[1] = params->vi_bitrate.max >> 8;
234 buf[2] = params->vi_bitrate.max & 0xff;
235 i2c_master_send(client, buf, 3);
236 } else {
237 // set the target bitrate (no max bitrate for CBR)
238 buf[0] = 0x81;
239 buf[1] = params->vi_bitrate.target >> 8;
240 buf[2] = params->vi_bitrate.target & 0xff;
241 i2c_master_send(client, buf, 3);
242 }
243
244 // set the audio bitrate
245 buf[0] = 0x94;
246 buf[1] = (256 == params->au_bitrate.target) ? 0 : 1;
247 i2c_master_send(client, buf, 2);
248
249 // set the total bitrate
250 buf[0] = 0xb1;
251 buf[1] = params->st_bitrate.target >> 8;
252 buf[2] = params->st_bitrate.target & 0xff;
253 i2c_master_send(client, buf, 3);
254
255 // return success
256 return 0;
257}
258
259
260static void saa6752hs_set_params(struct i2c_client* client,
261 struct v4l2_mpeg_compression* params)
262{
263 struct saa6752hs_state *h = i2c_get_clientdata(client);
264
265 /* check PIDs */
266 if (params->ts_pid_pmt <= MPEG_PID_MAX)
267 h->params.ts_pid_pmt = params->ts_pid_pmt;
268 if (params->ts_pid_pcr <= MPEG_PID_MAX)
269 h->params.ts_pid_pcr = params->ts_pid_pcr;
270 if (params->ts_pid_video <= MPEG_PID_MAX)
271 h->params.ts_pid_video = params->ts_pid_video;
272 if (params->ts_pid_audio <= MPEG_PID_MAX)
273 h->params.ts_pid_audio = params->ts_pid_audio;
274
275 /* check bitrate parameters */
276 if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) ||
277 (params->vi_bitrate.mode == V4L2_BITRATE_VBR))
278 h->params.vi_bitrate.mode = params->vi_bitrate.mode;
279 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
280 h->params.st_bitrate.target = params->st_bitrate.target;
281 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE)
282 h->params.vi_bitrate.target = params->vi_bitrate.target;
283 if (params->vi_bitrate.mode == V4L2_BITRATE_VBR)
284 h->params.vi_bitrate.max = params->vi_bitrate.max;
285 if (params->au_bitrate.mode != V4L2_BITRATE_NONE)
286 h->params.au_bitrate.target = params->au_bitrate.target;
287
288 /* aspect ratio */
289 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 ||
290 params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9)
291 h->params.vi_aspect_ratio = params->vi_aspect_ratio;
292
293 /* range checks */
294 if (h->params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX)
295 h->params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX;
296 if (h->params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX)
297 h->params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX;
298 if (h->params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX)
299 h->params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX;
300 if (h->params.au_bitrate.target <= 256)
301 h->params.au_bitrate.target = 256;
302 else
303 h->params.au_bitrate.target = 384;
304}
305
306static int saa6752hs_init(struct i2c_client* client)
307{
308 unsigned char buf[9], buf2[4];
309 struct saa6752hs_state *h;
310 u32 crc;
311 unsigned char localPAT[256];
312 unsigned char localPMT[256];
313
314 h = i2c_get_clientdata(client);
315
316 // Set video format - must be done first as it resets other settings
317 buf[0] = 0x41;
318 buf[1] = 0 /* MPEG_VIDEO_FORMAT_D1 */;
319 i2c_master_send(client, buf, 2);
320
321 // set bitrate
322 saa6752hs_set_bitrate(client, &h->params);
323
324 // Set GOP structure {3, 13}
325 buf[0] = 0x72;
326 buf[1] = 0x03;
327 buf[2] = 0x0D;
328 i2c_master_send(client,buf,3);
329
330 // Set minimum Q-scale {4}
331 buf[0] = 0x82;
332 buf[1] = 0x04;
333 i2c_master_send(client,buf,2);
334
335 // Set maximum Q-scale {12}
336 buf[0] = 0x83;
337 buf[1] = 0x0C;
338 i2c_master_send(client,buf,2);
339
340 // Set Output Protocol
341 buf[0] = 0xD0;
342 buf[1] = 0x81;
343 i2c_master_send(client,buf,2);
344
345 // Set video output stream format {TS}
346 buf[0] = 0xB0;
347 buf[1] = 0x05;
348 i2c_master_send(client,buf,2);
349
350 /* compute PAT */
351 memcpy(localPAT, PAT, sizeof(PAT));
352 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
353 localPAT[18] = h->params.ts_pid_pmt & 0xff;
354 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
355 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
356 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
357 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
358 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
359
360 /* compute PMT */
361 memcpy(localPMT, PMT, sizeof(PMT));
362 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
363 localPMT[4] = h->params.ts_pid_pmt & 0xff;
364 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
365 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
366 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
367 localPMT[21] = h->params.ts_pid_video & 0xFF;
368 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
369 localPMT[26] = h->params.ts_pid_audio & 0xFF;
370 crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
371 localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
372 localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
373 localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
374 localPMT[sizeof(PMT) - 1] = crc & 0xFF;
375
376 // Set Audio PID
377 buf[0] = 0xC1;
378 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
379 buf[2] = h->params.ts_pid_audio & 0xFF;
380 i2c_master_send(client,buf,3);
381
382 // Set Video PID
383 buf[0] = 0xC0;
384 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
385 buf[2] = h->params.ts_pid_video & 0xFF;
386 i2c_master_send(client,buf,3);
387
388 // Set PCR PID
389 buf[0] = 0xC4;
390 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
391 buf[2] = h->params.ts_pid_pcr & 0xFF;
392 i2c_master_send(client,buf,3);
393
394 // Send SI tables
395 i2c_master_send(client,localPAT,sizeof(PAT));
396 i2c_master_send(client,localPMT,sizeof(PMT));
397
398 // mute then unmute audio. This removes buzzing artefacts
399 buf[0] = 0xa4;
400 buf[1] = 1;
401 i2c_master_send(client, buf, 2);
402 buf[1] = 0;
403 i2c_master_send(client, buf, 2);
404
405 // start it going
406 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
407
408 // readout current state
409 buf[0] = 0xE1;
410 buf[1] = 0xA7;
411 buf[2] = 0xFE;
412 buf[3] = 0x82;
413 buf[4] = 0xB0;
414 i2c_master_send(client, buf, 5);
415 i2c_master_recv(client, buf2, 4);
416
417 // change aspect ratio
418 buf[0] = 0xE0;
419 buf[1] = 0xA7;
420 buf[2] = 0xFE;
421 buf[3] = 0x82;
422 buf[4] = 0xB0;
423 buf[5] = buf2[0];
424 switch(h->params.vi_aspect_ratio) {
425 case V4L2_MPEG_ASPECT_16_9:
426 buf[6] = buf2[1] | 0x40;
427 break;
428 case V4L2_MPEG_ASPECT_4_3:
429 default:
430 buf[6] = buf2[1] & 0xBF;
431 break;
432 break;
433 }
434 buf[7] = buf2[2];
435 buf[8] = buf2[3];
436 i2c_master_send(client, buf, 9);
437
438 // return success
439 return 0;
440}
441
442static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
443{
444 struct saa6752hs_state *h;
445
446 printk("saa6752hs: chip found @ 0x%x\n", addr<<1);
447
448 if (NULL == (h = kmalloc(sizeof(*h), GFP_KERNEL)))
449 return -ENOMEM;
450 memset(h,0,sizeof(*h));
451 h->client = client_template;
452 h->params = param_defaults;
453 h->client.adapter = adap;
454 h->client.addr = addr;
455
456 i2c_set_clientdata(&h->client, h);
457 i2c_attach_client(&h->client);
458 return 0;
459}
460
461static int saa6752hs_probe(struct i2c_adapter *adap)
462{
463 if (adap->class & I2C_CLASS_TV_ANALOG)
464 return i2c_probe(adap, &addr_data, saa6752hs_attach);
465 return 0;
466}
467
468static int saa6752hs_detach(struct i2c_client *client)
469{
470 struct saa6752hs_state *h;
471
472 h = i2c_get_clientdata(client);
473 i2c_detach_client(client);
474 kfree(h);
475 return 0;
476}
477
478static int
479saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
480{
481 struct saa6752hs_state *h = i2c_get_clientdata(client);
482 struct v4l2_mpeg_compression *params = arg;
483 int err = 0;
484
485 switch (cmd) {
486 case VIDIOC_S_MPEGCOMP:
487 if (NULL == params) {
488 /* apply settings and start encoder */
489 saa6752hs_init(client);
490 break;
491 }
492 saa6752hs_set_params(client, params);
493 /* fall through */
494 case VIDIOC_G_MPEGCOMP:
495 *params = h->params;
496 break;
497 default:
498 /* nothing */
499 break;
500 }
501
502 return err;
503}
504
505/* ----------------------------------------------------------------------- */
506
507static struct i2c_driver driver = {
508 .owner = THIS_MODULE,
509 .name = "i2c saa6752hs MPEG encoder",
510 .id = I2C_DRIVERID_SAA6752HS,
511 .flags = I2C_DF_NOTIFY,
512 .attach_adapter = saa6752hs_probe,
513 .detach_client = saa6752hs_detach,
514 .command = saa6752hs_command,
515};
516
517static struct i2c_client client_template =
518{
519 I2C_DEVNAME("saa6752hs"),
520 .flags = I2C_CLIENT_ALLOW_USE,
521 .driver = &driver,
522};
523
524static int __init saa6752hs_init_module(void)
525{
526 return i2c_add_driver(&driver);
527}
528
529static void __exit saa6752hs_cleanup_module(void)
530{
531 i2c_del_driver(&driver);
532}
533
534module_init(saa6752hs_init_module);
535module_exit(saa6752hs_cleanup_module);
536
537/*
538 * Overrides for Emacs so that we follow Linus's tabbing style.
539 * ---------------------------------------------------------------------------
540 * Local variables:
541 * c-basic-offset: 8
542 * End:
543 */