blob: ede522a33d7d06a032ab39e6fb4b9f8c00792155 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Media Vision Pro Movie Studio
3 * or
4 * "all you need is an I2C bus some RAM and a prayer"
5 *
6 * This draws heavily on code
7 *
8 * (c) Wolfgang Koehler, wolf@first.gmd.de, Dec. 1994
9 * Kiefernring 15
10 * 14478 Potsdam, Germany
11 *
12 * Most of this code is directly derived from his userspace driver.
13 * His driver works so send any reports to alan@redhat.com unless the
14 * userspace driver also doesn't work for you...
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Changes:
17 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030018 * - pms_capture: report back -EFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
21#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/ioport.h>
29#include <linux/init.h>
30#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030032#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030033#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020034#include <linux/mutex.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
38
39#define MOTOROLA 1
40#define PHILIPS2 2
41#define PHILIPS1 3
42#define MVVMEMORYWIDTH 0x40 /* 512 bytes */
43
44struct pms_device
45{
46 struct video_device v;
47 struct video_picture picture;
48 int height;
49 int width;
Ingo Molnar3593cab2006-02-07 06:49:14 -020050 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53struct i2c_info
54{
55 u8 slave;
56 u8 sub;
57 u8 data;
58 u8 hits;
59};
60
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030061static int i2c_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct i2c_info i2cinfo[64];
63
64static int decoder = PHILIPS2;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030065static int standard; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * I/O ports and Shared Memory
69 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int io_port = 0x250;
72static int data_port = 0x251;
73static int mem_base = 0xC8000;
74static void __iomem *mem;
75static int video_nr = -1;
76
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static inline void mvv_write(u8 index, u8 value)
80{
81 outw(index|(value<<8), io_port);
82}
83
84static inline u8 mvv_read(u8 index)
85{
86 outb(index, io_port);
87 return inb(data_port);
88}
89
90static int pms_i2c_stat(u8 slave)
91{
92 int counter;
93 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 outb(0x28, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 counter=0;
98 while((inb(data_port)&0x01)==0)
99 if(counter++==256)
100 break;
101
102 while((inb(data_port)&0x01)!=0)
103 if(counter++==256)
104 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 outb(slave, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 counter=0;
109 while((inb(data_port)&0x01)==0)
110 if(counter++==256)
111 break;
112
113 while((inb(data_port)&0x01)!=0)
114 if(counter++==256)
115 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 for(i=0;i<12;i++)
118 {
119 char st=inb(data_port);
120 if((st&2)!=0)
121 return -1;
122 if((st&1)==0)
123 break;
124 }
125 outb(0x29, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300126 return inb(data_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static int pms_i2c_write(u16 slave, u16 sub, u16 data)
130{
131 int skip=0;
132 int count;
133 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 for(i=0;i<i2c_count;i++)
136 {
137 if((i2cinfo[i].slave==slave) &&
138 (i2cinfo[i].sub == sub))
139 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300140 if(i2cinfo[i].data==data)
141 skip=1;
142 i2cinfo[i].data=data;
143 i=i2c_count+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if(i==i2c_count && i2c_count<64)
148 {
149 i2cinfo[i2c_count].slave=slave;
150 i2cinfo[i2c_count].sub=sub;
151 i2cinfo[i2c_count].data=data;
152 i2c_count++;
153 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if(skip)
156 return 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 mvv_write(0x29, sub);
159 mvv_write(0x2A, data);
160 mvv_write(0x28, slave);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 outb(0x28, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 count=0;
165 while((inb(data_port)&1)==0)
166 if(count>255)
167 break;
168 while((inb(data_port)&1)!=0)
169 if(count>255)
170 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 count=inb(data_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if(count&2)
175 return -1;
176 return count;
177}
178
179static int pms_i2c_read(int slave, int sub)
180{
181 int i=0;
182 for(i=0;i<i2c_count;i++)
183 {
184 if(i2cinfo[i].slave==slave && i2cinfo[i].sub==sub)
185 return i2cinfo[i].data;
186 }
187 return 0;
188}
189
190
191static void pms_i2c_andor(int slave, int sub, int and, int or)
192{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300193 u8 tmp;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 tmp=pms_i2c_read(slave, sub);
196 tmp = (tmp&and)|or;
197 pms_i2c_write(slave, sub, tmp);
198}
199
200/*
201 * Control functions
202 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static void pms_videosource(short source)
206{
207 mvv_write(0x2E, source?0x31:0x30);
208}
209
210static void pms_hue(short hue)
211{
212 switch(decoder)
213 {
214 case MOTOROLA:
215 pms_i2c_write(0x8A, 0x00, hue);
216 break;
217 case PHILIPS2:
218 pms_i2c_write(0x8A, 0x07, hue);
219 break;
220 case PHILIPS1:
221 pms_i2c_write(0x42, 0x07, hue);
222 break;
223 }
224}
225
226static void pms_colour(short colour)
227{
228 switch(decoder)
229 {
230 case MOTOROLA:
231 pms_i2c_write(0x8A, 0x00, colour);
232 break;
233 case PHILIPS1:
234 pms_i2c_write(0x42, 0x12, colour);
235 break;
236 }
237}
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300238
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void pms_contrast(short contrast)
241{
242 switch(decoder)
243 {
244 case MOTOROLA:
245 pms_i2c_write(0x8A, 0x00, contrast);
246 break;
247 case PHILIPS1:
248 pms_i2c_write(0x42, 0x13, contrast);
249 break;
250 }
251}
252
253static void pms_brightness(short brightness)
254{
255 switch(decoder)
256 {
257 case MOTOROLA:
258 pms_i2c_write(0x8A, 0x00, brightness);
259 pms_i2c_write(0x8A, 0x00, brightness);
260 pms_i2c_write(0x8A, 0x00, brightness);
261 break;
262 case PHILIPS1:
263 pms_i2c_write(0x42, 0x19, brightness);
264 break;
265 }
266}
267
268
269static void pms_format(short format)
270{
271 int target;
272 standard = format;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if(decoder==PHILIPS1)
275 target=0x42;
276 else if(decoder==PHILIPS2)
277 target=0x8A;
278 else
279 return;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 switch(format)
282 {
283 case 0: /* Auto */
284 pms_i2c_andor(target, 0x0D, 0xFE,0x00);
285 pms_i2c_andor(target, 0x0F, 0x3F,0x80);
286 break;
287 case 1: /* NTSC */
288 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
289 pms_i2c_andor(target, 0x0F, 0x3F, 0x40);
290 break;
291 case 2: /* PAL */
292 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
293 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
294 break;
295 case 3: /* SECAM */
296 pms_i2c_andor(target, 0x0D, 0xFE, 0x01);
297 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
298 break;
299 }
300}
301
302#ifdef FOR_FUTURE_EXPANSION
303
304/*
305 * These features of the PMS card are not currently exposes. They
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300306 * could become a private v4l ioctl for PMSCONFIG or somesuch if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * people need it. We also don't yet use the PMS interrupt.
308 */
309
310static void pms_hstart(short start)
311{
312 switch(decoder)
313 {
314 case PHILIPS1:
315 pms_i2c_write(0x8A, 0x05, start);
316 pms_i2c_write(0x8A, 0x18, start);
317 break;
318 case PHILIPS2:
319 pms_i2c_write(0x42, 0x05, start);
320 pms_i2c_write(0x42, 0x18, start);
321 break;
322 }
323}
324
325/*
326 * Bandpass filters
327 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static void pms_bandpass(short pass)
330{
331 if(decoder==PHILIPS2)
332 pms_i2c_andor(0x8A, 0x06, 0xCF, (pass&0x03)<<4);
333 else if(decoder==PHILIPS1)
334 pms_i2c_andor(0x42, 0x06, 0xCF, (pass&0x03)<<4);
335}
336
337static void pms_antisnow(short snow)
338{
339 if(decoder==PHILIPS2)
340 pms_i2c_andor(0x8A, 0x06, 0xF3, (snow&0x03)<<2);
341 else if(decoder==PHILIPS1)
342 pms_i2c_andor(0x42, 0x06, 0xF3, (snow&0x03)<<2);
343}
344
345static void pms_sharpness(short sharp)
346{
347 if(decoder==PHILIPS2)
348 pms_i2c_andor(0x8A, 0x06, 0xFC, sharp&0x03);
349 else if(decoder==PHILIPS1)
350 pms_i2c_andor(0x42, 0x06, 0xFC, sharp&0x03);
351}
352
353static void pms_chromaagc(short agc)
354{
355 if(decoder==PHILIPS2)
356 pms_i2c_andor(0x8A, 0x0C, 0x9F, (agc&0x03)<<5);
357 else if(decoder==PHILIPS1)
358 pms_i2c_andor(0x42, 0x0C, 0x9F, (agc&0x03)<<5);
359}
360
361static void pms_vertnoise(short noise)
362{
363 if(decoder==PHILIPS2)
364 pms_i2c_andor(0x8A, 0x10, 0xFC, noise&3);
365 else if(decoder==PHILIPS1)
366 pms_i2c_andor(0x42, 0x10, 0xFC, noise&3);
367}
368
369static void pms_forcecolour(short colour)
370{
371 if(decoder==PHILIPS2)
372 pms_i2c_andor(0x8A, 0x0C, 0x7F, (colour&1)<<7);
373 else if(decoder==PHILIPS1)
374 pms_i2c_andor(0x42, 0x0C, 0x7, (colour&1)<<7);
375}
376
377static void pms_antigamma(short gamma)
378{
379 if(decoder==PHILIPS2)
380 pms_i2c_andor(0xB8, 0x00, 0x7F, (gamma&1)<<7);
381 else if(decoder==PHILIPS1)
382 pms_i2c_andor(0x42, 0x20, 0x7, (gamma&1)<<7);
383}
384
385static void pms_prefilter(short filter)
386{
387 if(decoder==PHILIPS2)
388 pms_i2c_andor(0x8A, 0x06, 0xBF, (filter&1)<<6);
389 else if(decoder==PHILIPS1)
390 pms_i2c_andor(0x42, 0x06, 0xBF, (filter&1)<<6);
391}
392
393static void pms_hfilter(short filter)
394{
395 if(decoder==PHILIPS2)
396 pms_i2c_andor(0xB8, 0x04, 0x1F, (filter&7)<<5);
397 else if(decoder==PHILIPS1)
398 pms_i2c_andor(0x42, 0x24, 0x1F, (filter&7)<<5);
399}
400
401static void pms_vfilter(short filter)
402{
403 if(decoder==PHILIPS2)
404 pms_i2c_andor(0xB8, 0x08, 0x9F, (filter&3)<<5);
405 else if(decoder==PHILIPS1)
406 pms_i2c_andor(0x42, 0x28, 0x9F, (filter&3)<<5);
407}
408
409static void pms_killcolour(short colour)
410{
411 if(decoder==PHILIPS2)
412 {
413 pms_i2c_andor(0x8A, 0x08, 0x07, (colour&0x1F)<<3);
414 pms_i2c_andor(0x8A, 0x09, 0x07, (colour&0x1F)<<3);
415 }
416 else if(decoder==PHILIPS1)
417 {
418 pms_i2c_andor(0x42, 0x08, 0x07, (colour&0x1F)<<3);
419 pms_i2c_andor(0x42, 0x09, 0x07, (colour&0x1F)<<3);
420 }
421}
422
423static void pms_chromagain(short chroma)
424{
425 if(decoder==PHILIPS2)
426 {
427 pms_i2c_write(0x8A, 0x11, chroma);
428 }
429 else if(decoder==PHILIPS1)
430 {
431 pms_i2c_write(0x42, 0x11, chroma);
432 }
433}
434
435
436static void pms_spacialcompl(short data)
437{
438 mvv_write(0x3B, data);
439}
440
441static void pms_spacialcomph(short data)
442{
443 mvv_write(0x3A, data);
444}
445
446static void pms_vstart(short start)
447{
448 mvv_write(0x16, start);
449 mvv_write(0x17, (start>>8)&0x01);
450}
451
452#endif
453
454static void pms_secamcross(short cross)
455{
456 if(decoder==PHILIPS2)
457 pms_i2c_andor(0x8A, 0x0F, 0xDF, (cross&1)<<5);
458 else if(decoder==PHILIPS1)
459 pms_i2c_andor(0x42, 0x0F, 0xDF, (cross&1)<<5);
460}
461
462
463static void pms_swsense(short sense)
464{
465 if(decoder==PHILIPS2)
466 {
467 pms_i2c_write(0x8A, 0x0A, sense);
468 pms_i2c_write(0x8A, 0x0B, sense);
469 }
470 else if(decoder==PHILIPS1)
471 {
472 pms_i2c_write(0x42, 0x0A, sense);
473 pms_i2c_write(0x42, 0x0B, sense);
474 }
475}
476
477
478static void pms_framerate(short frr)
479{
480 int fps=(standard==1)?30:25;
481 if(frr==0)
482 return;
483 fps=fps/frr;
484 mvv_write(0x14,0x80|fps);
485 mvv_write(0x15,1);
486}
487
488static void pms_vert(u8 deciden, u8 decinum)
489{
490 mvv_write(0x1C, deciden); /* Denominator */
491 mvv_write(0x1D, decinum); /* Numerator */
492}
493
494/*
495 * Turn 16bit ratios into best small ratio the chipset can grok
496 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void pms_vertdeci(unsigned short decinum, unsigned short deciden)
499{
500 /* Knock it down by /5 once */
501 if(decinum%5==0)
502 {
503 deciden/=5;
504 decinum/=5;
505 }
506 /*
507 * 3's
508 */
509 while(decinum%3==0 && deciden%3==0)
510 {
511 deciden/=3;
512 decinum/=3;
513 }
514 /*
515 * 2's
516 */
517 while(decinum%2==0 && deciden%2==0)
518 {
519 decinum/=2;
520 deciden/=2;
521 }
522 /*
523 * Fudgyify
524 */
525 while(deciden>32)
526 {
527 deciden/=2;
528 decinum=(decinum+1)/2;
529 }
530 if(deciden==32)
531 deciden--;
532 pms_vert(deciden,decinum);
533}
534
535static void pms_horzdeci(short decinum, short deciden)
536{
537 if(decinum<=512)
538 {
539 if(decinum%5==0)
540 {
541 decinum/=5;
542 deciden/=5;
543 }
544 }
545 else
546 {
547 decinum=512;
548 deciden=640; /* 768 would be ideal */
549 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 while(((decinum|deciden)&1)==0)
552 {
553 decinum>>=1;
554 deciden>>=1;
555 }
556 while(deciden>32)
557 {
558 deciden>>=1;
559 decinum=(decinum+1)>>1;
560 }
561 if(deciden==32)
562 deciden--;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mvv_write(0x24, 0x80|deciden);
565 mvv_write(0x25, decinum);
566}
567
568static void pms_resolution(short width, short height)
569{
570 int fg_height;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 fg_height=height;
573 if(fg_height>280)
574 fg_height=280;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 mvv_write(0x18, fg_height);
577 mvv_write(0x19, fg_height>>8);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if(standard==1)
580 {
581 mvv_write(0x1A, 0xFC);
582 mvv_write(0x1B, 0x00);
583 if(height>fg_height)
584 pms_vertdeci(240,240);
585 else
586 pms_vertdeci(fg_height,240);
587 }
588 else
589 {
590 mvv_write(0x1A, 0x1A);
591 mvv_write(0x1B, 0x01);
592 if(fg_height>256)
593 pms_vertdeci(270,270);
594 else
595 pms_vertdeci(fg_height, 270);
596 }
597 mvv_write(0x12,0);
598 mvv_write(0x13, MVVMEMORYWIDTH);
599 mvv_write(0x42, 0x00);
600 mvv_write(0x43, 0x00);
601 mvv_write(0x44, MVVMEMORYWIDTH);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 mvv_write(0x22, width+8);
604 mvv_write(0x23, (width+8)>> 8);
605
606 if(standard==1)
607 pms_horzdeci(width,640);
608 else
609 pms_horzdeci(width+8, 768);
610
611 mvv_write(0x30, mvv_read(0x30)&0xFE);
612 mvv_write(0x08, mvv_read(0x08)|0x01);
613 mvv_write(0x01, mvv_read(0x01)&0xFD);
614 mvv_write(0x32, 0x00);
615 mvv_write(0x33, MVVMEMORYWIDTH);
616}
617
618
619/*
620 * Set Input
621 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static void pms_vcrinput(short input)
624{
625 if(decoder==PHILIPS2)
626 pms_i2c_andor(0x8A,0x0D,0x7F,(input&1)<<7);
627 else if(decoder==PHILIPS1)
628 pms_i2c_andor(0x42,0x0D,0x7F,(input&1)<<7);
629}
630
631
632static int pms_capture(struct pms_device *dev, char __user *buf, int rgb555, int count)
633{
634 int y;
635 int dw = 2*dev->width;
636
637 char tmp[dw+32]; /* using a temp buffer is faster than direct */
638 int cnt = 0;
639 int len=0;
640 unsigned char r8 = 0x5; /* value for reg8 */
641
642 if (rgb555)
643 r8 |= 0x20; /* else use untranslated rgb = 565 */
644 mvv_write(0x08,r8); /* capture rgb555/565, init DRAM, PC enable */
645
646/* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300647
648 for (y = 0; y < dev->height; y++ )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 {
650 writeb(0, mem); /* synchronisiert neue Zeile */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * This is in truth a fifo, be very careful as if you
654 * forgot this odd things will occur 8)
655 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 memcpy_fromio(tmp, mem, dw+32); /* discard 16 word */
658 cnt -= dev->height;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300659 while (cnt <= 0)
660 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Don't copy too far
663 */
664 int dt=dw;
665 if(dt+len>count)
666 dt=count-len;
667 cnt += dev->height;
668 if (copy_to_user(buf, tmp+32, dt))
669 return len ? len : -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300670 buf += dt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 len += dt;
672 }
673 }
674 return len;
675}
676
677
678/*
679 * Video4linux interfacing
680 */
681
682static int pms_do_ioctl(struct inode *inode, struct file *file,
683 unsigned int cmd, void *arg)
684{
685 struct video_device *dev = video_devdata(file);
686 struct pms_device *pd=(struct pms_device *)dev;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 switch(cmd)
689 {
690 case VIDIOCGCAP:
691 {
692 struct video_capability *b = arg;
693 strcpy(b->name, "Mediavision PMS");
694 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
695 b->channels = 4;
696 b->audios = 0;
697 b->maxwidth = 640;
698 b->maxheight = 480;
699 b->minwidth = 16;
700 b->minheight = 16;
701 return 0;
702 }
703 case VIDIOCGCHAN:
704 {
705 struct video_channel *v = arg;
706 if(v->channel<0 || v->channel>3)
707 return -EINVAL;
708 v->flags=0;
709 v->tuners=1;
710 /* Good question.. its composite or SVHS so.. */
711 v->type = VIDEO_TYPE_CAMERA;
712 switch(v->channel)
713 {
714 case 0:
715 strcpy(v->name, "Composite");break;
716 case 1:
717 strcpy(v->name, "SVideo");break;
718 case 2:
719 strcpy(v->name, "Composite(VCR)");break;
720 case 3:
721 strcpy(v->name, "SVideo(VCR)");break;
722 }
723 return 0;
724 }
725 case VIDIOCSCHAN:
726 {
727 struct video_channel *v = arg;
728 if(v->channel<0 || v->channel>3)
729 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200730 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pms_videosource(v->channel&1);
732 pms_vcrinput(v->channel>>1);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200733 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735 }
736 case VIDIOCGTUNER:
737 {
738 struct video_tuner *v = arg;
739 if(v->tuner)
740 return -EINVAL;
741 strcpy(v->name, "Format");
742 v->rangelow=0;
743 v->rangehigh=0;
744 v->flags= VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
745 switch(standard)
746 {
747 case 0:
748 v->mode = VIDEO_MODE_AUTO;
749 break;
750 case 1:
751 v->mode = VIDEO_MODE_NTSC;
752 break;
753 case 2:
754 v->mode = VIDEO_MODE_PAL;
755 break;
756 case 3:
757 v->mode = VIDEO_MODE_SECAM;
758 break;
759 }
760 return 0;
761 }
762 case VIDIOCSTUNER:
763 {
764 struct video_tuner *v = arg;
765 if(v->tuner)
766 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200767 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 switch(v->mode)
769 {
770 case VIDEO_MODE_AUTO:
771 pms_framerate(25);
772 pms_secamcross(0);
773 pms_format(0);
774 break;
775 case VIDEO_MODE_NTSC:
776 pms_framerate(30);
777 pms_secamcross(0);
778 pms_format(1);
779 break;
780 case VIDEO_MODE_PAL:
781 pms_framerate(25);
782 pms_secamcross(0);
783 pms_format(2);
784 break;
785 case VIDEO_MODE_SECAM:
786 pms_framerate(25);
787 pms_secamcross(1);
788 pms_format(2);
789 break;
790 default:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200791 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EINVAL;
793 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200794 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796 }
797 case VIDIOCGPICT:
798 {
799 struct video_picture *p = arg;
800 *p = pd->picture;
801 return 0;
802 }
803 case VIDIOCSPICT:
804 {
805 struct video_picture *p = arg;
806 if(!((p->palette==VIDEO_PALETTE_RGB565 && p->depth==16)
807 ||(p->palette==VIDEO_PALETTE_RGB555 && p->depth==15)))
Trent Piepho657de3c2006-06-20 00:30:57 -0300808 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 pd->picture= *p;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Now load the card.
813 */
814
Ingo Molnar3593cab2006-02-07 06:49:14 -0200815 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 pms_brightness(p->brightness>>8);
817 pms_hue(p->hue>>8);
818 pms_colour(p->colour>>8);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300819 pms_contrast(p->contrast>>8);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200820 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822 }
823 case VIDIOCSWIN:
824 {
825 struct video_window *vw = arg;
826 if(vw->flags)
827 return -EINVAL;
828 if(vw->clipcount)
829 return -EINVAL;
830 if(vw->height<16||vw->height>480)
831 return -EINVAL;
832 if(vw->width<16||vw->width>640)
833 return -EINVAL;
834 pd->width=vw->width;
835 pd->height=vw->height;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200836 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 pms_resolution(pd->width, pd->height);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200838 mutex_unlock(&pd->lock); /* Ok we figured out what to use from our wide choice */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840 }
841 case VIDIOCGWIN:
842 {
843 struct video_window *vw = arg;
844 memset(vw,0,sizeof(*vw));
845 vw->width=pd->width;
846 vw->height=pd->height;
847 return 0;
848 }
849 case VIDIOCKEY:
850 return 0;
851 case VIDIOCCAPTURE:
852 case VIDIOCGFBUF:
853 case VIDIOCSFBUF:
854 case VIDIOCGFREQ:
855 case VIDIOCSFREQ:
856 case VIDIOCGAUDIO:
857 case VIDIOCSAUDIO:
858 return -EINVAL;
859 default:
860 return -ENOIOCTLCMD;
861 }
862 return 0;
863}
864
865static int pms_ioctl(struct inode *inode, struct file *file,
866 unsigned int cmd, unsigned long arg)
867{
868 return video_usercopy(inode, file, cmd, arg, pms_do_ioctl);
869}
870
871static ssize_t pms_read(struct file *file, char __user *buf,
872 size_t count, loff_t *ppos)
873{
874 struct video_device *v = video_devdata(file);
875 struct pms_device *pd=(struct pms_device *)v;
876 int len;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300877
Ingo Molnar3593cab2006-02-07 06:49:14 -0200878 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 len=pms_capture(pd, buf, (pd->picture.depth==16)?0:1,count);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200880 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return len;
882}
883
Arjan van de Venfa027c22007-02-12 00:55:33 -0800884static const struct file_operations pms_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 .owner = THIS_MODULE,
886 .open = video_exclusive_open,
887 .release = video_exclusive_release,
888 .ioctl = pms_ioctl,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300889#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200890 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300891#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .read = pms_read,
893 .llseek = no_llseek,
894};
895
896static struct video_device pms_template=
897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .name = "Mediavision PMS",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 .fops = &pms_fops,
900};
901
902static struct pms_device pms_device;
903
904
905/*
906 * Probe for and initialise the Mediavision PMS
907 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int init_mediavision(void)
910{
911 int id;
912 int idec, decst;
913 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned char i2c_defs[]={
916 0x4C,0x30,0x00,0xE8,
917 0xB6,0xE2,0x00,0x00,
918 0xFF,0xFF,0x00,0x00,
919 0x00,0x00,0x78,0x98,
920 0x00,0x00,0x00,0x00,
921 0x34,0x0A,0xF4,0xCE,
922 0xE4
923 };
924
925 mem = ioremap(mem_base, 0x800);
926 if (!mem)
927 return -ENOMEM;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!request_region(0x9A01, 1, "Mediavision PMS config"))
930 {
931 printk(KERN_WARNING "mediavision: unable to detect: 0x9A01 in use.\n");
932 iounmap(mem);
933 return -EBUSY;
934 }
935 if (!request_region(io_port, 3, "Mediavision PMS"))
936 {
937 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", io_port);
938 release_region(0x9A01, 1);
939 iounmap(mem);
940 return -EBUSY;
941 }
942 outb(0xB8, 0x9A01); /* Unlock */
943 outb(io_port>>4, 0x9A01); /* Set IO port */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300944
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 id=mvv_read(3);
947 decst=pms_i2c_stat(0x43);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if(decst!=-1)
950 idec=2;
951 else if(pms_i2c_stat(0xb9)!=-1)
952 idec=3;
953 else if(pms_i2c_stat(0x8b)!=-1)
954 idec=1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300955 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 idec=0;
957
958 printk(KERN_INFO "PMS type is %d\n", idec);
959 if(idec == 0) {
960 release_region(io_port, 3);
961 release_region(0x9A01, 1);
962 iounmap(mem);
963 return -ENODEV;
964 }
965
966 /*
967 * Ok we have a PMS of some sort
968 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 mvv_write(0x04, mem_base>>12); /* Set the memory area */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Ok now load the defaults */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 for(i=0;i<0x19;i++)
975 {
976 if(i2c_defs[i]==0xFF)
977 pms_i2c_andor(0x8A, i, 0x07,0x00);
978 else
979 pms_i2c_write(0x8A, i, i2c_defs[i]);
980 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 pms_i2c_write(0xB8,0x00,0x12);
983 pms_i2c_write(0xB8,0x04,0x00);
984 pms_i2c_write(0xB8,0x07,0x00);
985 pms_i2c_write(0xB8,0x08,0x00);
986 pms_i2c_write(0xB8,0x09,0xFF);
987 pms_i2c_write(0xB8,0x0A,0x00);
988 pms_i2c_write(0xB8,0x0B,0x10);
989 pms_i2c_write(0xB8,0x10,0x03);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 mvv_write(0x01, 0x00);
992 mvv_write(0x05, 0xA0);
993 mvv_write(0x08, 0x25);
994 mvv_write(0x09, 0x00);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300995 mvv_write(0x0A, 0x20|MVVMEMORYWIDTH);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 mvv_write(0x10, 0x02);
998 mvv_write(0x1E, 0x0C);
999 mvv_write(0x1F, 0x03);
1000 mvv_write(0x26, 0x06);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 mvv_write(0x2B, 0x00);
1003 mvv_write(0x2C, 0x20);
1004 mvv_write(0x2D, 0x00);
1005 mvv_write(0x2F, 0x70);
1006 mvv_write(0x32, 0x00);
1007 mvv_write(0x33, MVVMEMORYWIDTH);
1008 mvv_write(0x34, 0x00);
1009 mvv_write(0x35, 0x00);
1010 mvv_write(0x3A, 0x80);
1011 mvv_write(0x3B, 0x10);
1012 mvv_write(0x20, 0x00);
1013 mvv_write(0x21, 0x00);
1014 mvv_write(0x30, 0x22);
1015 return 0;
1016}
1017
1018/*
1019 * Initialization and module stuff
1020 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001021
Rene Hermanb54ff932008-09-03 16:48:21 -03001022#ifndef MODULE
1023static int enable;
1024module_param(enable, int, 0);
1025#endif
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int __init init_pms_cards(void)
1028{
1029 printk(KERN_INFO "Mediavision Pro Movie Studio driver 0.02\n");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001030
Rene Hermanb54ff932008-09-03 16:48:21 -03001031#ifndef MODULE
1032 if (!enable) {
1033 printk(KERN_INFO "Not enabled\n");
1034 return -ENODEV;
1035 }
1036#endif
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 data_port = io_port +1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if(init_mediavision())
1041 {
1042 printk(KERN_INFO "Board not found.\n");
1043 return -ENODEV;
1044 }
1045 memcpy(&pms_device, &pms_template, sizeof(pms_template));
Ingo Molnar3593cab2006-02-07 06:49:14 -02001046 mutex_init(&pms_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 pms_device.height=240;
1048 pms_device.width=320;
1049 pms_swsense(75);
1050 pms_resolution(320,240);
1051 return video_register_device((struct video_device *)&pms_device, VFL_TYPE_GRABBER, video_nr);
1052}
1053
1054module_param(io_port, int, 0);
1055module_param(mem_base, int, 0);
1056module_param(video_nr, int, 0);
1057MODULE_LICENSE("GPL");
1058
1059
1060static void __exit shutdown_mediavision(void)
1061{
1062 release_region(io_port,3);
1063 release_region(0x9A01, 1);
1064}
1065
1066static void __exit cleanup_pms_module(void)
1067{
1068 shutdown_mediavision();
1069 video_unregister_device((struct video_device *)&pms_device);
1070 iounmap(mem);
1071}
1072
1073module_init(init_pms_cards);
1074module_exit(cleanup_pms_module);
1075