blob: 994807818aa26eacd2ffc09920445d2cc69f58d9 [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;
Hans Verkuil7d43cd52008-08-23 05:31:47 -030050 unsigned long in_use;
Ingo Molnar3593cab2006-02-07 06:49:14 -020051 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54struct i2c_info
55{
56 u8 slave;
57 u8 sub;
58 u8 data;
59 u8 hits;
60};
61
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030062static int i2c_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct i2c_info i2cinfo[64];
64
65static int decoder = PHILIPS2;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030066static int standard; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * I/O ports and Shared Memory
70 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int io_port = 0x250;
73static int data_port = 0x251;
74static int mem_base = 0xC8000;
75static void __iomem *mem;
76static int video_nr = -1;
77
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static inline void mvv_write(u8 index, u8 value)
81{
82 outw(index|(value<<8), io_port);
83}
84
85static inline u8 mvv_read(u8 index)
86{
87 outb(index, io_port);
88 return inb(data_port);
89}
90
91static int pms_i2c_stat(u8 slave)
92{
93 int counter;
94 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 outb(0x28, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 counter=0;
99 while((inb(data_port)&0x01)==0)
100 if(counter++==256)
101 break;
102
103 while((inb(data_port)&0x01)!=0)
104 if(counter++==256)
105 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 outb(slave, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 counter=0;
110 while((inb(data_port)&0x01)==0)
111 if(counter++==256)
112 break;
113
114 while((inb(data_port)&0x01)!=0)
115 if(counter++==256)
116 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 for(i=0;i<12;i++)
119 {
120 char st=inb(data_port);
121 if((st&2)!=0)
122 return -1;
123 if((st&1)==0)
124 break;
125 }
126 outb(0x29, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300127 return inb(data_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static int pms_i2c_write(u16 slave, u16 sub, u16 data)
131{
132 int skip=0;
133 int count;
134 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 for(i=0;i<i2c_count;i++)
137 {
138 if((i2cinfo[i].slave==slave) &&
139 (i2cinfo[i].sub == sub))
140 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300141 if(i2cinfo[i].data==data)
142 skip=1;
143 i2cinfo[i].data=data;
144 i=i2c_count+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if(i==i2c_count && i2c_count<64)
149 {
150 i2cinfo[i2c_count].slave=slave;
151 i2cinfo[i2c_count].sub=sub;
152 i2cinfo[i2c_count].data=data;
153 i2c_count++;
154 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if(skip)
157 return 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 mvv_write(0x29, sub);
160 mvv_write(0x2A, data);
161 mvv_write(0x28, slave);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 outb(0x28, io_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 count=0;
166 while((inb(data_port)&1)==0)
167 if(count>255)
168 break;
169 while((inb(data_port)&1)!=0)
170 if(count>255)
171 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 count=inb(data_port);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if(count&2)
176 return -1;
177 return count;
178}
179
180static int pms_i2c_read(int slave, int sub)
181{
182 int i=0;
183 for(i=0;i<i2c_count;i++)
184 {
185 if(i2cinfo[i].slave==slave && i2cinfo[i].sub==sub)
186 return i2cinfo[i].data;
187 }
188 return 0;
189}
190
191
192static void pms_i2c_andor(int slave, int sub, int and, int or)
193{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300194 u8 tmp;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 tmp=pms_i2c_read(slave, sub);
197 tmp = (tmp&and)|or;
198 pms_i2c_write(slave, sub, tmp);
199}
200
201/*
202 * Control functions
203 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206static void pms_videosource(short source)
207{
208 mvv_write(0x2E, source?0x31:0x30);
209}
210
211static void pms_hue(short hue)
212{
213 switch(decoder)
214 {
215 case MOTOROLA:
216 pms_i2c_write(0x8A, 0x00, hue);
217 break;
218 case PHILIPS2:
219 pms_i2c_write(0x8A, 0x07, hue);
220 break;
221 case PHILIPS1:
222 pms_i2c_write(0x42, 0x07, hue);
223 break;
224 }
225}
226
227static void pms_colour(short colour)
228{
229 switch(decoder)
230 {
231 case MOTOROLA:
232 pms_i2c_write(0x8A, 0x00, colour);
233 break;
234 case PHILIPS1:
235 pms_i2c_write(0x42, 0x12, colour);
236 break;
237 }
238}
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300239
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static void pms_contrast(short contrast)
242{
243 switch(decoder)
244 {
245 case MOTOROLA:
246 pms_i2c_write(0x8A, 0x00, contrast);
247 break;
248 case PHILIPS1:
249 pms_i2c_write(0x42, 0x13, contrast);
250 break;
251 }
252}
253
254static void pms_brightness(short brightness)
255{
256 switch(decoder)
257 {
258 case MOTOROLA:
259 pms_i2c_write(0x8A, 0x00, brightness);
260 pms_i2c_write(0x8A, 0x00, brightness);
261 pms_i2c_write(0x8A, 0x00, brightness);
262 break;
263 case PHILIPS1:
264 pms_i2c_write(0x42, 0x19, brightness);
265 break;
266 }
267}
268
269
270static void pms_format(short format)
271{
272 int target;
273 standard = format;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if(decoder==PHILIPS1)
276 target=0x42;
277 else if(decoder==PHILIPS2)
278 target=0x8A;
279 else
280 return;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 switch(format)
283 {
284 case 0: /* Auto */
285 pms_i2c_andor(target, 0x0D, 0xFE,0x00);
286 pms_i2c_andor(target, 0x0F, 0x3F,0x80);
287 break;
288 case 1: /* NTSC */
289 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
290 pms_i2c_andor(target, 0x0F, 0x3F, 0x40);
291 break;
292 case 2: /* PAL */
293 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
294 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
295 break;
296 case 3: /* SECAM */
297 pms_i2c_andor(target, 0x0D, 0xFE, 0x01);
298 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
299 break;
300 }
301}
302
303#ifdef FOR_FUTURE_EXPANSION
304
305/*
306 * These features of the PMS card are not currently exposes. They
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300307 * could become a private v4l ioctl for PMSCONFIG or somesuch if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * people need it. We also don't yet use the PMS interrupt.
309 */
310
311static void pms_hstart(short start)
312{
313 switch(decoder)
314 {
315 case PHILIPS1:
316 pms_i2c_write(0x8A, 0x05, start);
317 pms_i2c_write(0x8A, 0x18, start);
318 break;
319 case PHILIPS2:
320 pms_i2c_write(0x42, 0x05, start);
321 pms_i2c_write(0x42, 0x18, start);
322 break;
323 }
324}
325
326/*
327 * Bandpass filters
328 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void pms_bandpass(short pass)
331{
332 if(decoder==PHILIPS2)
333 pms_i2c_andor(0x8A, 0x06, 0xCF, (pass&0x03)<<4);
334 else if(decoder==PHILIPS1)
335 pms_i2c_andor(0x42, 0x06, 0xCF, (pass&0x03)<<4);
336}
337
338static void pms_antisnow(short snow)
339{
340 if(decoder==PHILIPS2)
341 pms_i2c_andor(0x8A, 0x06, 0xF3, (snow&0x03)<<2);
342 else if(decoder==PHILIPS1)
343 pms_i2c_andor(0x42, 0x06, 0xF3, (snow&0x03)<<2);
344}
345
346static void pms_sharpness(short sharp)
347{
348 if(decoder==PHILIPS2)
349 pms_i2c_andor(0x8A, 0x06, 0xFC, sharp&0x03);
350 else if(decoder==PHILIPS1)
351 pms_i2c_andor(0x42, 0x06, 0xFC, sharp&0x03);
352}
353
354static void pms_chromaagc(short agc)
355{
356 if(decoder==PHILIPS2)
357 pms_i2c_andor(0x8A, 0x0C, 0x9F, (agc&0x03)<<5);
358 else if(decoder==PHILIPS1)
359 pms_i2c_andor(0x42, 0x0C, 0x9F, (agc&0x03)<<5);
360}
361
362static void pms_vertnoise(short noise)
363{
364 if(decoder==PHILIPS2)
365 pms_i2c_andor(0x8A, 0x10, 0xFC, noise&3);
366 else if(decoder==PHILIPS1)
367 pms_i2c_andor(0x42, 0x10, 0xFC, noise&3);
368}
369
370static void pms_forcecolour(short colour)
371{
372 if(decoder==PHILIPS2)
373 pms_i2c_andor(0x8A, 0x0C, 0x7F, (colour&1)<<7);
374 else if(decoder==PHILIPS1)
375 pms_i2c_andor(0x42, 0x0C, 0x7, (colour&1)<<7);
376}
377
378static void pms_antigamma(short gamma)
379{
380 if(decoder==PHILIPS2)
381 pms_i2c_andor(0xB8, 0x00, 0x7F, (gamma&1)<<7);
382 else if(decoder==PHILIPS1)
383 pms_i2c_andor(0x42, 0x20, 0x7, (gamma&1)<<7);
384}
385
386static void pms_prefilter(short filter)
387{
388 if(decoder==PHILIPS2)
389 pms_i2c_andor(0x8A, 0x06, 0xBF, (filter&1)<<6);
390 else if(decoder==PHILIPS1)
391 pms_i2c_andor(0x42, 0x06, 0xBF, (filter&1)<<6);
392}
393
394static void pms_hfilter(short filter)
395{
396 if(decoder==PHILIPS2)
397 pms_i2c_andor(0xB8, 0x04, 0x1F, (filter&7)<<5);
398 else if(decoder==PHILIPS1)
399 pms_i2c_andor(0x42, 0x24, 0x1F, (filter&7)<<5);
400}
401
402static void pms_vfilter(short filter)
403{
404 if(decoder==PHILIPS2)
405 pms_i2c_andor(0xB8, 0x08, 0x9F, (filter&3)<<5);
406 else if(decoder==PHILIPS1)
407 pms_i2c_andor(0x42, 0x28, 0x9F, (filter&3)<<5);
408}
409
410static void pms_killcolour(short colour)
411{
412 if(decoder==PHILIPS2)
413 {
414 pms_i2c_andor(0x8A, 0x08, 0x07, (colour&0x1F)<<3);
415 pms_i2c_andor(0x8A, 0x09, 0x07, (colour&0x1F)<<3);
416 }
417 else if(decoder==PHILIPS1)
418 {
419 pms_i2c_andor(0x42, 0x08, 0x07, (colour&0x1F)<<3);
420 pms_i2c_andor(0x42, 0x09, 0x07, (colour&0x1F)<<3);
421 }
422}
423
424static void pms_chromagain(short chroma)
425{
426 if(decoder==PHILIPS2)
427 {
428 pms_i2c_write(0x8A, 0x11, chroma);
429 }
430 else if(decoder==PHILIPS1)
431 {
432 pms_i2c_write(0x42, 0x11, chroma);
433 }
434}
435
436
437static void pms_spacialcompl(short data)
438{
439 mvv_write(0x3B, data);
440}
441
442static void pms_spacialcomph(short data)
443{
444 mvv_write(0x3A, data);
445}
446
447static void pms_vstart(short start)
448{
449 mvv_write(0x16, start);
450 mvv_write(0x17, (start>>8)&0x01);
451}
452
453#endif
454
455static void pms_secamcross(short cross)
456{
457 if(decoder==PHILIPS2)
458 pms_i2c_andor(0x8A, 0x0F, 0xDF, (cross&1)<<5);
459 else if(decoder==PHILIPS1)
460 pms_i2c_andor(0x42, 0x0F, 0xDF, (cross&1)<<5);
461}
462
463
464static void pms_swsense(short sense)
465{
466 if(decoder==PHILIPS2)
467 {
468 pms_i2c_write(0x8A, 0x0A, sense);
469 pms_i2c_write(0x8A, 0x0B, sense);
470 }
471 else if(decoder==PHILIPS1)
472 {
473 pms_i2c_write(0x42, 0x0A, sense);
474 pms_i2c_write(0x42, 0x0B, sense);
475 }
476}
477
478
479static void pms_framerate(short frr)
480{
481 int fps=(standard==1)?30:25;
482 if(frr==0)
483 return;
484 fps=fps/frr;
485 mvv_write(0x14,0x80|fps);
486 mvv_write(0x15,1);
487}
488
489static void pms_vert(u8 deciden, u8 decinum)
490{
491 mvv_write(0x1C, deciden); /* Denominator */
492 mvv_write(0x1D, decinum); /* Numerator */
493}
494
495/*
496 * Turn 16bit ratios into best small ratio the chipset can grok
497 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void pms_vertdeci(unsigned short decinum, unsigned short deciden)
500{
501 /* Knock it down by /5 once */
502 if(decinum%5==0)
503 {
504 deciden/=5;
505 decinum/=5;
506 }
507 /*
508 * 3's
509 */
510 while(decinum%3==0 && deciden%3==0)
511 {
512 deciden/=3;
513 decinum/=3;
514 }
515 /*
516 * 2's
517 */
518 while(decinum%2==0 && deciden%2==0)
519 {
520 decinum/=2;
521 deciden/=2;
522 }
523 /*
524 * Fudgyify
525 */
526 while(deciden>32)
527 {
528 deciden/=2;
529 decinum=(decinum+1)/2;
530 }
531 if(deciden==32)
532 deciden--;
533 pms_vert(deciden,decinum);
534}
535
536static void pms_horzdeci(short decinum, short deciden)
537{
538 if(decinum<=512)
539 {
540 if(decinum%5==0)
541 {
542 decinum/=5;
543 deciden/=5;
544 }
545 }
546 else
547 {
548 decinum=512;
549 deciden=640; /* 768 would be ideal */
550 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 while(((decinum|deciden)&1)==0)
553 {
554 decinum>>=1;
555 deciden>>=1;
556 }
557 while(deciden>32)
558 {
559 deciden>>=1;
560 decinum=(decinum+1)>>1;
561 }
562 if(deciden==32)
563 deciden--;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 mvv_write(0x24, 0x80|deciden);
566 mvv_write(0x25, decinum);
567}
568
569static void pms_resolution(short width, short height)
570{
571 int fg_height;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 fg_height=height;
574 if(fg_height>280)
575 fg_height=280;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 mvv_write(0x18, fg_height);
578 mvv_write(0x19, fg_height>>8);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if(standard==1)
581 {
582 mvv_write(0x1A, 0xFC);
583 mvv_write(0x1B, 0x00);
584 if(height>fg_height)
585 pms_vertdeci(240,240);
586 else
587 pms_vertdeci(fg_height,240);
588 }
589 else
590 {
591 mvv_write(0x1A, 0x1A);
592 mvv_write(0x1B, 0x01);
593 if(fg_height>256)
594 pms_vertdeci(270,270);
595 else
596 pms_vertdeci(fg_height, 270);
597 }
598 mvv_write(0x12,0);
599 mvv_write(0x13, MVVMEMORYWIDTH);
600 mvv_write(0x42, 0x00);
601 mvv_write(0x43, 0x00);
602 mvv_write(0x44, MVVMEMORYWIDTH);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 mvv_write(0x22, width+8);
605 mvv_write(0x23, (width+8)>> 8);
606
607 if(standard==1)
608 pms_horzdeci(width,640);
609 else
610 pms_horzdeci(width+8, 768);
611
612 mvv_write(0x30, mvv_read(0x30)&0xFE);
613 mvv_write(0x08, mvv_read(0x08)|0x01);
614 mvv_write(0x01, mvv_read(0x01)&0xFD);
615 mvv_write(0x32, 0x00);
616 mvv_write(0x33, MVVMEMORYWIDTH);
617}
618
619
620/*
621 * Set Input
622 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static void pms_vcrinput(short input)
625{
626 if(decoder==PHILIPS2)
627 pms_i2c_andor(0x8A,0x0D,0x7F,(input&1)<<7);
628 else if(decoder==PHILIPS1)
629 pms_i2c_andor(0x42,0x0D,0x7F,(input&1)<<7);
630}
631
632
633static int pms_capture(struct pms_device *dev, char __user *buf, int rgb555, int count)
634{
635 int y;
636 int dw = 2*dev->width;
637
638 char tmp[dw+32]; /* using a temp buffer is faster than direct */
639 int cnt = 0;
640 int len=0;
641 unsigned char r8 = 0x5; /* value for reg8 */
642
643 if (rgb555)
644 r8 |= 0x20; /* else use untranslated rgb = 565 */
645 mvv_write(0x08,r8); /* capture rgb555/565, init DRAM, PC enable */
646
647/* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300648
649 for (y = 0; y < dev->height; y++ )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 {
651 writeb(0, mem); /* synchronisiert neue Zeile */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * This is in truth a fifo, be very careful as if you
655 * forgot this odd things will occur 8)
656 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 memcpy_fromio(tmp, mem, dw+32); /* discard 16 word */
659 cnt -= dev->height;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300660 while (cnt <= 0)
661 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Don't copy too far
664 */
665 int dt=dw;
666 if(dt+len>count)
667 dt=count-len;
668 cnt += dev->height;
669 if (copy_to_user(buf, tmp+32, dt))
670 return len ? len : -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300671 buf += dt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 len += dt;
673 }
674 }
675 return len;
676}
677
678
679/*
680 * Video4linux interfacing
681 */
682
683static int pms_do_ioctl(struct inode *inode, struct file *file,
684 unsigned int cmd, void *arg)
685{
686 struct video_device *dev = video_devdata(file);
687 struct pms_device *pd=(struct pms_device *)dev;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 switch(cmd)
690 {
691 case VIDIOCGCAP:
692 {
693 struct video_capability *b = arg;
694 strcpy(b->name, "Mediavision PMS");
695 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
696 b->channels = 4;
697 b->audios = 0;
698 b->maxwidth = 640;
699 b->maxheight = 480;
700 b->minwidth = 16;
701 b->minheight = 16;
702 return 0;
703 }
704 case VIDIOCGCHAN:
705 {
706 struct video_channel *v = arg;
707 if(v->channel<0 || v->channel>3)
708 return -EINVAL;
709 v->flags=0;
710 v->tuners=1;
711 /* Good question.. its composite or SVHS so.. */
712 v->type = VIDEO_TYPE_CAMERA;
713 switch(v->channel)
714 {
715 case 0:
716 strcpy(v->name, "Composite");break;
717 case 1:
718 strcpy(v->name, "SVideo");break;
719 case 2:
720 strcpy(v->name, "Composite(VCR)");break;
721 case 3:
722 strcpy(v->name, "SVideo(VCR)");break;
723 }
724 return 0;
725 }
726 case VIDIOCSCHAN:
727 {
728 struct video_channel *v = arg;
729 if(v->channel<0 || v->channel>3)
730 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200731 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 pms_videosource(v->channel&1);
733 pms_vcrinput(v->channel>>1);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200734 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736 }
737 case VIDIOCGTUNER:
738 {
739 struct video_tuner *v = arg;
740 if(v->tuner)
741 return -EINVAL;
742 strcpy(v->name, "Format");
743 v->rangelow=0;
744 v->rangehigh=0;
745 v->flags= VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
746 switch(standard)
747 {
748 case 0:
749 v->mode = VIDEO_MODE_AUTO;
750 break;
751 case 1:
752 v->mode = VIDEO_MODE_NTSC;
753 break;
754 case 2:
755 v->mode = VIDEO_MODE_PAL;
756 break;
757 case 3:
758 v->mode = VIDEO_MODE_SECAM;
759 break;
760 }
761 return 0;
762 }
763 case VIDIOCSTUNER:
764 {
765 struct video_tuner *v = arg;
766 if(v->tuner)
767 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200768 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 switch(v->mode)
770 {
771 case VIDEO_MODE_AUTO:
772 pms_framerate(25);
773 pms_secamcross(0);
774 pms_format(0);
775 break;
776 case VIDEO_MODE_NTSC:
777 pms_framerate(30);
778 pms_secamcross(0);
779 pms_format(1);
780 break;
781 case VIDEO_MODE_PAL:
782 pms_framerate(25);
783 pms_secamcross(0);
784 pms_format(2);
785 break;
786 case VIDEO_MODE_SECAM:
787 pms_framerate(25);
788 pms_secamcross(1);
789 pms_format(2);
790 break;
791 default:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200792 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -EINVAL;
794 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200795 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797 }
798 case VIDIOCGPICT:
799 {
800 struct video_picture *p = arg;
801 *p = pd->picture;
802 return 0;
803 }
804 case VIDIOCSPICT:
805 {
806 struct video_picture *p = arg;
807 if(!((p->palette==VIDEO_PALETTE_RGB565 && p->depth==16)
808 ||(p->palette==VIDEO_PALETTE_RGB555 && p->depth==15)))
Trent Piepho657de3c2006-06-20 00:30:57 -0300809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 pd->picture= *p;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /*
813 * Now load the card.
814 */
815
Ingo Molnar3593cab2006-02-07 06:49:14 -0200816 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 pms_brightness(p->brightness>>8);
818 pms_hue(p->hue>>8);
819 pms_colour(p->colour>>8);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300820 pms_contrast(p->contrast>>8);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200821 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823 }
824 case VIDIOCSWIN:
825 {
826 struct video_window *vw = arg;
827 if(vw->flags)
828 return -EINVAL;
829 if(vw->clipcount)
830 return -EINVAL;
831 if(vw->height<16||vw->height>480)
832 return -EINVAL;
833 if(vw->width<16||vw->width>640)
834 return -EINVAL;
835 pd->width=vw->width;
836 pd->height=vw->height;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200837 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 pms_resolution(pd->width, pd->height);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200839 mutex_unlock(&pd->lock); /* Ok we figured out what to use from our wide choice */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841 }
842 case VIDIOCGWIN:
843 {
844 struct video_window *vw = arg;
845 memset(vw,0,sizeof(*vw));
846 vw->width=pd->width;
847 vw->height=pd->height;
848 return 0;
849 }
850 case VIDIOCKEY:
851 return 0;
852 case VIDIOCCAPTURE:
853 case VIDIOCGFBUF:
854 case VIDIOCSFBUF:
855 case VIDIOCGFREQ:
856 case VIDIOCSFREQ:
857 case VIDIOCGAUDIO:
858 case VIDIOCSAUDIO:
859 return -EINVAL;
860 default:
861 return -ENOIOCTLCMD;
862 }
863 return 0;
864}
865
866static int pms_ioctl(struct inode *inode, struct file *file,
867 unsigned int cmd, unsigned long arg)
868{
869 return video_usercopy(inode, file, cmd, arg, pms_do_ioctl);
870}
871
872static ssize_t pms_read(struct file *file, char __user *buf,
873 size_t count, loff_t *ppos)
874{
875 struct video_device *v = video_devdata(file);
876 struct pms_device *pd=(struct pms_device *)v;
877 int len;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300878
Ingo Molnar3593cab2006-02-07 06:49:14 -0200879 mutex_lock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 len=pms_capture(pd, buf, (pd->picture.depth==16)?0:1,count);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200881 mutex_unlock(&pd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return len;
883}
884
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300885static int pms_exclusive_open(struct inode *inode, struct file *file)
886{
887 struct video_device *v = video_devdata(file);
888 struct pms_device *pd = (struct pms_device *)v;
889
890 return test_and_set_bit(0, &pd->in_use) ? -EBUSY : 0;
891}
892
893static int pms_exclusive_release(struct inode *inode, struct file *file)
894{
895 struct video_device *v = video_devdata(file);
896 struct pms_device *pd = (struct pms_device *)v;
897
898 clear_bit(0, &pd->in_use);
899 return 0;
900}
901
Arjan van de Venfa027c22007-02-12 00:55:33 -0800902static const struct file_operations pms_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .owner = THIS_MODULE,
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300904 .open = pms_exclusive_open,
905 .release = pms_exclusive_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .ioctl = pms_ioctl,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300907#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200908 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300909#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 .read = pms_read,
911 .llseek = no_llseek,
912};
913
914static struct video_device pms_template=
915{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .name = "Mediavision PMS",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 .fops = &pms_fops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300918 .release = video_device_release_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919};
920
921static struct pms_device pms_device;
922
923
924/*
925 * Probe for and initialise the Mediavision PMS
926 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928static int init_mediavision(void)
929{
930 int id;
931 int idec, decst;
932 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unsigned char i2c_defs[]={
935 0x4C,0x30,0x00,0xE8,
936 0xB6,0xE2,0x00,0x00,
937 0xFF,0xFF,0x00,0x00,
938 0x00,0x00,0x78,0x98,
939 0x00,0x00,0x00,0x00,
940 0x34,0x0A,0xF4,0xCE,
941 0xE4
942 };
943
944 mem = ioremap(mem_base, 0x800);
945 if (!mem)
946 return -ENOMEM;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!request_region(0x9A01, 1, "Mediavision PMS config"))
949 {
950 printk(KERN_WARNING "mediavision: unable to detect: 0x9A01 in use.\n");
951 iounmap(mem);
952 return -EBUSY;
953 }
954 if (!request_region(io_port, 3, "Mediavision PMS"))
955 {
956 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", io_port);
957 release_region(0x9A01, 1);
958 iounmap(mem);
959 return -EBUSY;
960 }
961 outb(0xB8, 0x9A01); /* Unlock */
962 outb(io_port>>4, 0x9A01); /* Set IO port */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300963
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 id=mvv_read(3);
966 decst=pms_i2c_stat(0x43);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if(decst!=-1)
969 idec=2;
970 else if(pms_i2c_stat(0xb9)!=-1)
971 idec=3;
972 else if(pms_i2c_stat(0x8b)!=-1)
973 idec=1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300974 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 idec=0;
976
977 printk(KERN_INFO "PMS type is %d\n", idec);
978 if(idec == 0) {
979 release_region(io_port, 3);
980 release_region(0x9A01, 1);
981 iounmap(mem);
982 return -ENODEV;
983 }
984
985 /*
986 * Ok we have a PMS of some sort
987 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 mvv_write(0x04, mem_base>>12); /* Set the memory area */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Ok now load the defaults */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 for(i=0;i<0x19;i++)
994 {
995 if(i2c_defs[i]==0xFF)
996 pms_i2c_andor(0x8A, i, 0x07,0x00);
997 else
998 pms_i2c_write(0x8A, i, i2c_defs[i]);
999 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 pms_i2c_write(0xB8,0x00,0x12);
1002 pms_i2c_write(0xB8,0x04,0x00);
1003 pms_i2c_write(0xB8,0x07,0x00);
1004 pms_i2c_write(0xB8,0x08,0x00);
1005 pms_i2c_write(0xB8,0x09,0xFF);
1006 pms_i2c_write(0xB8,0x0A,0x00);
1007 pms_i2c_write(0xB8,0x0B,0x10);
1008 pms_i2c_write(0xB8,0x10,0x03);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mvv_write(0x01, 0x00);
1011 mvv_write(0x05, 0xA0);
1012 mvv_write(0x08, 0x25);
1013 mvv_write(0x09, 0x00);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001014 mvv_write(0x0A, 0x20|MVVMEMORYWIDTH);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 mvv_write(0x10, 0x02);
1017 mvv_write(0x1E, 0x0C);
1018 mvv_write(0x1F, 0x03);
1019 mvv_write(0x26, 0x06);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mvv_write(0x2B, 0x00);
1022 mvv_write(0x2C, 0x20);
1023 mvv_write(0x2D, 0x00);
1024 mvv_write(0x2F, 0x70);
1025 mvv_write(0x32, 0x00);
1026 mvv_write(0x33, MVVMEMORYWIDTH);
1027 mvv_write(0x34, 0x00);
1028 mvv_write(0x35, 0x00);
1029 mvv_write(0x3A, 0x80);
1030 mvv_write(0x3B, 0x10);
1031 mvv_write(0x20, 0x00);
1032 mvv_write(0x21, 0x00);
1033 mvv_write(0x30, 0x22);
1034 return 0;
1035}
1036
1037/*
1038 * Initialization and module stuff
1039 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001040
Rene Hermanb54ff932008-09-03 16:48:21 -03001041#ifndef MODULE
1042static int enable;
1043module_param(enable, int, 0);
1044#endif
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static int __init init_pms_cards(void)
1047{
1048 printk(KERN_INFO "Mediavision Pro Movie Studio driver 0.02\n");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001049
Rene Hermanb54ff932008-09-03 16:48:21 -03001050#ifndef MODULE
1051 if (!enable) {
Rene Herman4ce0b652008-09-03 16:48:23 -03001052 printk(KERN_INFO "PMS: not enabled, use pms.enable=1 to "
1053 "probe\n");
Rene Hermanb54ff932008-09-03 16:48:21 -03001054 return -ENODEV;
1055 }
1056#endif
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 data_port = io_port +1;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if(init_mediavision())
1061 {
1062 printk(KERN_INFO "Board not found.\n");
1063 return -ENODEV;
1064 }
1065 memcpy(&pms_device, &pms_template, sizeof(pms_template));
Ingo Molnar3593cab2006-02-07 06:49:14 -02001066 mutex_init(&pms_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 pms_device.height=240;
1068 pms_device.width=320;
1069 pms_swsense(75);
1070 pms_resolution(320,240);
1071 return video_register_device((struct video_device *)&pms_device, VFL_TYPE_GRABBER, video_nr);
1072}
1073
1074module_param(io_port, int, 0);
1075module_param(mem_base, int, 0);
1076module_param(video_nr, int, 0);
1077MODULE_LICENSE("GPL");
1078
1079
1080static void __exit shutdown_mediavision(void)
1081{
1082 release_region(io_port,3);
1083 release_region(0x9A01, 1);
1084}
1085
1086static void __exit cleanup_pms_module(void)
1087{
1088 shutdown_mediavision();
1089 video_unregister_device((struct video_device *)&pms_device);
1090 iounmap(mem);
1091}
1092
1093module_init(init_pms_cards);
1094module_exit(cleanup_pms_module);
1095