Merge branch 'imx/fixes' of git://git.pengutronix.de/git/imx/linux-2.6 into fixes
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / pms.c
1 /*
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@lxorguk.ukuu.org.uk
14 * unless the userspace driver also doesn't work for you...
15 *
16 * Changes:
17 * 25-11-2009 Hans Verkuil <hverkuil@xs4all.nl>
18 * - converted to version 2 of the V4L API.
19 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
20 * - pms_capture: report back -EFAULT
21 */
22
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/mutex.h>
32 #include <linux/uaccess.h>
33 #include <linux/isa.h>
34 #include <asm/io.h>
35
36 #include <linux/videodev2.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-ctrls.h>
40 #include <media/v4l2-fh.h>
41 #include <media/v4l2-event.h>
42 #include <media/v4l2-device.h>
43
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION("0.0.5");
46
47 #define MOTOROLA 1
48 #define PHILIPS2 2 /* SAA7191 */
49 #define PHILIPS1 3
50 #define MVVMEMORYWIDTH 0x40 /* 512 bytes */
51
52 struct i2c_info {
53 u8 slave;
54 u8 sub;
55 u8 data;
56 u8 hits;
57 };
58
59 struct pms {
60 struct v4l2_device v4l2_dev;
61 struct video_device vdev;
62 struct v4l2_ctrl_handler hdl;
63 int height;
64 int width;
65 int depth;
66 int input;
67 struct mutex lock;
68 int i2c_count;
69 struct i2c_info i2cinfo[64];
70
71 int decoder;
72 int standard; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
73 v4l2_std_id std;
74 int io;
75 int data;
76 void __iomem *mem;
77 };
78
79 /*
80 * I/O ports and Shared Memory
81 */
82
83 static int io_port = 0x250;
84 module_param(io_port, int, 0);
85
86 static int mem_base = 0xc8000;
87 module_param(mem_base, int, 0);
88
89 static int video_nr = -1;
90 module_param(video_nr, int, 0);
91
92
93 static inline void mvv_write(struct pms *dev, u8 index, u8 value)
94 {
95 outw(index | (value << 8), dev->io);
96 }
97
98 static inline u8 mvv_read(struct pms *dev, u8 index)
99 {
100 outb(index, dev->io);
101 return inb(dev->data);
102 }
103
104 static int pms_i2c_stat(struct pms *dev, u8 slave)
105 {
106 int counter = 0;
107 int i;
108
109 outb(0x28, dev->io);
110
111 while ((inb(dev->data) & 0x01) == 0)
112 if (counter++ == 256)
113 break;
114
115 while ((inb(dev->data) & 0x01) != 0)
116 if (counter++ == 256)
117 break;
118
119 outb(slave, dev->io);
120
121 counter = 0;
122 while ((inb(dev->data) & 0x01) == 0)
123 if (counter++ == 256)
124 break;
125
126 while ((inb(dev->data) & 0x01) != 0)
127 if (counter++ == 256)
128 break;
129
130 for (i = 0; i < 12; i++) {
131 char st = inb(dev->data);
132
133 if ((st & 2) != 0)
134 return -1;
135 if ((st & 1) == 0)
136 break;
137 }
138 outb(0x29, dev->io);
139 return inb(dev->data);
140 }
141
142 static int pms_i2c_write(struct pms *dev, u16 slave, u16 sub, u16 data)
143 {
144 int skip = 0;
145 int count;
146 int i;
147
148 for (i = 0; i < dev->i2c_count; i++) {
149 if ((dev->i2cinfo[i].slave == slave) &&
150 (dev->i2cinfo[i].sub == sub)) {
151 if (dev->i2cinfo[i].data == data)
152 skip = 1;
153 dev->i2cinfo[i].data = data;
154 i = dev->i2c_count + 1;
155 }
156 }
157
158 if (i == dev->i2c_count && dev->i2c_count < 64) {
159 dev->i2cinfo[dev->i2c_count].slave = slave;
160 dev->i2cinfo[dev->i2c_count].sub = sub;
161 dev->i2cinfo[dev->i2c_count].data = data;
162 dev->i2c_count++;
163 }
164
165 if (skip)
166 return 0;
167
168 mvv_write(dev, 0x29, sub);
169 mvv_write(dev, 0x2A, data);
170 mvv_write(dev, 0x28, slave);
171
172 outb(0x28, dev->io);
173
174 count = 0;
175 while ((inb(dev->data) & 1) == 0)
176 if (count > 255)
177 break;
178 while ((inb(dev->data) & 1) != 0)
179 if (count > 255)
180 break;
181
182 count = inb(dev->data);
183
184 if (count & 2)
185 return -1;
186 return count;
187 }
188
189 static int pms_i2c_read(struct pms *dev, int slave, int sub)
190 {
191 int i;
192
193 for (i = 0; i < dev->i2c_count; i++) {
194 if (dev->i2cinfo[i].slave == slave && dev->i2cinfo[i].sub == sub)
195 return dev->i2cinfo[i].data;
196 }
197 return 0;
198 }
199
200
201 static void pms_i2c_andor(struct pms *dev, int slave, int sub, int and, int or)
202 {
203 u8 tmp;
204
205 tmp = pms_i2c_read(dev, slave, sub);
206 tmp = (tmp & and) | or;
207 pms_i2c_write(dev, slave, sub, tmp);
208 }
209
210 /*
211 * Control functions
212 */
213
214
215 static void pms_videosource(struct pms *dev, short source)
216 {
217 switch (dev->decoder) {
218 case MOTOROLA:
219 break;
220 case PHILIPS2:
221 pms_i2c_andor(dev, 0x8a, 0x06, 0x7f, source ? 0x80 : 0);
222 break;
223 case PHILIPS1:
224 break;
225 }
226 mvv_write(dev, 0x2E, 0x31);
227 /* Was: mvv_write(dev, 0x2E, source ? 0x31 : 0x30);
228 But could not make this work correctly. Only Composite input
229 worked for me. */
230 }
231
232 static void pms_hue(struct pms *dev, short hue)
233 {
234 switch (dev->decoder) {
235 case MOTOROLA:
236 pms_i2c_write(dev, 0x8a, 0x00, hue);
237 break;
238 case PHILIPS2:
239 pms_i2c_write(dev, 0x8a, 0x07, hue);
240 break;
241 case PHILIPS1:
242 pms_i2c_write(dev, 0x42, 0x07, hue);
243 break;
244 }
245 }
246
247 static void pms_saturation(struct pms *dev, short sat)
248 {
249 switch (dev->decoder) {
250 case MOTOROLA:
251 pms_i2c_write(dev, 0x8a, 0x00, sat);
252 break;
253 case PHILIPS1:
254 pms_i2c_write(dev, 0x42, 0x12, sat);
255 break;
256 }
257 }
258
259
260 static void pms_contrast(struct pms *dev, short contrast)
261 {
262 switch (dev->decoder) {
263 case MOTOROLA:
264 pms_i2c_write(dev, 0x8a, 0x00, contrast);
265 break;
266 case PHILIPS1:
267 pms_i2c_write(dev, 0x42, 0x13, contrast);
268 break;
269 }
270 }
271
272 static void pms_brightness(struct pms *dev, short brightness)
273 {
274 switch (dev->decoder) {
275 case MOTOROLA:
276 pms_i2c_write(dev, 0x8a, 0x00, brightness);
277 pms_i2c_write(dev, 0x8a, 0x00, brightness);
278 pms_i2c_write(dev, 0x8a, 0x00, brightness);
279 break;
280 case PHILIPS1:
281 pms_i2c_write(dev, 0x42, 0x19, brightness);
282 break;
283 }
284 }
285
286
287 static void pms_format(struct pms *dev, short format)
288 {
289 int target;
290
291 dev->standard = format;
292
293 if (dev->decoder == PHILIPS1)
294 target = 0x42;
295 else if (dev->decoder == PHILIPS2)
296 target = 0x8a;
297 else
298 return;
299
300 switch (format) {
301 case 0: /* Auto */
302 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
303 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x80);
304 break;
305 case 1: /* NTSC */
306 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
307 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x40);
308 break;
309 case 2: /* PAL */
310 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
311 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
312 break;
313 case 3: /* SECAM */
314 pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x01);
315 pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
316 break;
317 }
318 }
319
320 #ifdef FOR_FUTURE_EXPANSION
321
322 /*
323 * These features of the PMS card are not currently exposes. They
324 * could become a private v4l ioctl for PMSCONFIG or somesuch if
325 * people need it. We also don't yet use the PMS interrupt.
326 */
327
328 static void pms_hstart(struct pms *dev, short start)
329 {
330 switch (dev->decoder) {
331 case PHILIPS1:
332 pms_i2c_write(dev, 0x8a, 0x05, start);
333 pms_i2c_write(dev, 0x8a, 0x18, start);
334 break;
335 case PHILIPS2:
336 pms_i2c_write(dev, 0x42, 0x05, start);
337 pms_i2c_write(dev, 0x42, 0x18, start);
338 break;
339 }
340 }
341
342 /*
343 * Bandpass filters
344 */
345
346 static void pms_bandpass(struct pms *dev, short pass)
347 {
348 if (dev->decoder == PHILIPS2)
349 pms_i2c_andor(dev, 0x8a, 0x06, 0xcf, (pass & 0x03) << 4);
350 else if (dev->decoder == PHILIPS1)
351 pms_i2c_andor(dev, 0x42, 0x06, 0xcf, (pass & 0x03) << 4);
352 }
353
354 static void pms_antisnow(struct pms *dev, short snow)
355 {
356 if (dev->decoder == PHILIPS2)
357 pms_i2c_andor(dev, 0x8a, 0x06, 0xf3, (snow & 0x03) << 2);
358 else if (dev->decoder == PHILIPS1)
359 pms_i2c_andor(dev, 0x42, 0x06, 0xf3, (snow & 0x03) << 2);
360 }
361
362 static void pms_sharpness(struct pms *dev, short sharp)
363 {
364 if (dev->decoder == PHILIPS2)
365 pms_i2c_andor(dev, 0x8a, 0x06, 0xfc, sharp & 0x03);
366 else if (dev->decoder == PHILIPS1)
367 pms_i2c_andor(dev, 0x42, 0x06, 0xfc, sharp & 0x03);
368 }
369
370 static void pms_chromaagc(struct pms *dev, short agc)
371 {
372 if (dev->decoder == PHILIPS2)
373 pms_i2c_andor(dev, 0x8a, 0x0c, 0x9f, (agc & 0x03) << 5);
374 else if (dev->decoder == PHILIPS1)
375 pms_i2c_andor(dev, 0x42, 0x0c, 0x9f, (agc & 0x03) << 5);
376 }
377
378 static void pms_vertnoise(struct pms *dev, short noise)
379 {
380 if (dev->decoder == PHILIPS2)
381 pms_i2c_andor(dev, 0x8a, 0x10, 0xfc, noise & 3);
382 else if (dev->decoder == PHILIPS1)
383 pms_i2c_andor(dev, 0x42, 0x10, 0xfc, noise & 3);
384 }
385
386 static void pms_forcecolour(struct pms *dev, short colour)
387 {
388 if (dev->decoder == PHILIPS2)
389 pms_i2c_andor(dev, 0x8a, 0x0c, 0x7f, (colour & 1) << 7);
390 else if (dev->decoder == PHILIPS1)
391 pms_i2c_andor(dev, 0x42, 0x0c, 0x7, (colour & 1) << 7);
392 }
393
394 static void pms_antigamma(struct pms *dev, short gamma)
395 {
396 if (dev->decoder == PHILIPS2)
397 pms_i2c_andor(dev, 0xb8, 0x00, 0x7f, (gamma & 1) << 7);
398 else if (dev->decoder == PHILIPS1)
399 pms_i2c_andor(dev, 0x42, 0x20, 0x7, (gamma & 1) << 7);
400 }
401
402 static void pms_prefilter(struct pms *dev, short filter)
403 {
404 if (dev->decoder == PHILIPS2)
405 pms_i2c_andor(dev, 0x8a, 0x06, 0xbf, (filter & 1) << 6);
406 else if (dev->decoder == PHILIPS1)
407 pms_i2c_andor(dev, 0x42, 0x06, 0xbf, (filter & 1) << 6);
408 }
409
410 static void pms_hfilter(struct pms *dev, short filter)
411 {
412 if (dev->decoder == PHILIPS2)
413 pms_i2c_andor(dev, 0xb8, 0x04, 0x1f, (filter & 7) << 5);
414 else if (dev->decoder == PHILIPS1)
415 pms_i2c_andor(dev, 0x42, 0x24, 0x1f, (filter & 7) << 5);
416 }
417
418 static void pms_vfilter(struct pms *dev, short filter)
419 {
420 if (dev->decoder == PHILIPS2)
421 pms_i2c_andor(dev, 0xb8, 0x08, 0x9f, (filter & 3) << 5);
422 else if (dev->decoder == PHILIPS1)
423 pms_i2c_andor(dev, 0x42, 0x28, 0x9f, (filter & 3) << 5);
424 }
425
426 static void pms_killcolour(struct pms *dev, short colour)
427 {
428 if (dev->decoder == PHILIPS2) {
429 pms_i2c_andor(dev, 0x8a, 0x08, 0x07, (colour & 0x1f) << 3);
430 pms_i2c_andor(dev, 0x8a, 0x09, 0x07, (colour & 0x1f) << 3);
431 } else if (dev->decoder == PHILIPS1) {
432 pms_i2c_andor(dev, 0x42, 0x08, 0x07, (colour & 0x1f) << 3);
433 pms_i2c_andor(dev, 0x42, 0x09, 0x07, (colour & 0x1f) << 3);
434 }
435 }
436
437 static void pms_chromagain(struct pms *dev, short chroma)
438 {
439 if (dev->decoder == PHILIPS2)
440 pms_i2c_write(dev, 0x8a, 0x11, chroma);
441 else if (dev->decoder == PHILIPS1)
442 pms_i2c_write(dev, 0x42, 0x11, chroma);
443 }
444
445
446 static void pms_spacialcompl(struct pms *dev, short data)
447 {
448 mvv_write(dev, 0x3b, data);
449 }
450
451 static void pms_spacialcomph(struct pms *dev, short data)
452 {
453 mvv_write(dev, 0x3a, data);
454 }
455
456 static void pms_vstart(struct pms *dev, short start)
457 {
458 mvv_write(dev, 0x16, start);
459 mvv_write(dev, 0x17, (start >> 8) & 0x01);
460 }
461
462 #endif
463
464 static void pms_secamcross(struct pms *dev, short cross)
465 {
466 if (dev->decoder == PHILIPS2)
467 pms_i2c_andor(dev, 0x8a, 0x0f, 0xdf, (cross & 1) << 5);
468 else if (dev->decoder == PHILIPS1)
469 pms_i2c_andor(dev, 0x42, 0x0f, 0xdf, (cross & 1) << 5);
470 }
471
472
473 static void pms_swsense(struct pms *dev, short sense)
474 {
475 if (dev->decoder == PHILIPS2) {
476 pms_i2c_write(dev, 0x8a, 0x0a, sense);
477 pms_i2c_write(dev, 0x8a, 0x0b, sense);
478 } else if (dev->decoder == PHILIPS1) {
479 pms_i2c_write(dev, 0x42, 0x0a, sense);
480 pms_i2c_write(dev, 0x42, 0x0b, sense);
481 }
482 }
483
484
485 static void pms_framerate(struct pms *dev, short frr)
486 {
487 int fps = (dev->std & V4L2_STD_525_60) ? 30 : 25;
488
489 if (frr == 0)
490 return;
491 fps = fps/frr;
492 mvv_write(dev, 0x14, 0x80 | fps);
493 mvv_write(dev, 0x15, 1);
494 }
495
496 static void pms_vert(struct pms *dev, u8 deciden, u8 decinum)
497 {
498 mvv_write(dev, 0x1c, deciden); /* Denominator */
499 mvv_write(dev, 0x1d, decinum); /* Numerator */
500 }
501
502 /*
503 * Turn 16bit ratios into best small ratio the chipset can grok
504 */
505
506 static void pms_vertdeci(struct pms *dev, unsigned short decinum, unsigned short deciden)
507 {
508 /* Knock it down by / 5 once */
509 if (decinum % 5 == 0) {
510 deciden /= 5;
511 decinum /= 5;
512 }
513 /*
514 * 3's
515 */
516 while (decinum % 3 == 0 && deciden % 3 == 0) {
517 deciden /= 3;
518 decinum /= 3;
519 }
520 /*
521 * 2's
522 */
523 while (decinum % 2 == 0 && deciden % 2 == 0) {
524 decinum /= 2;
525 deciden /= 2;
526 }
527 /*
528 * Fudgyify
529 */
530 while (deciden > 32) {
531 deciden /= 2;
532 decinum = (decinum + 1) / 2;
533 }
534 if (deciden == 32)
535 deciden--;
536 pms_vert(dev, deciden, decinum);
537 }
538
539 static void pms_horzdeci(struct pms *dev, short decinum, short deciden)
540 {
541 if (decinum <= 512) {
542 if (decinum % 5 == 0) {
543 decinum /= 5;
544 deciden /= 5;
545 }
546 } else {
547 decinum = 512;
548 deciden = 640; /* 768 would be ideal */
549 }
550
551 while (((decinum | deciden) & 1) == 0) {
552 decinum >>= 1;
553 deciden >>= 1;
554 }
555 while (deciden > 32) {
556 deciden >>= 1;
557 decinum = (decinum + 1) >> 1;
558 }
559 if (deciden == 32)
560 deciden--;
561
562 mvv_write(dev, 0x24, 0x80 | deciden);
563 mvv_write(dev, 0x25, decinum);
564 }
565
566 static void pms_resolution(struct pms *dev, short width, short height)
567 {
568 int fg_height;
569
570 fg_height = height;
571 if (fg_height > 280)
572 fg_height = 280;
573
574 mvv_write(dev, 0x18, fg_height);
575 mvv_write(dev, 0x19, fg_height >> 8);
576
577 if (dev->std & V4L2_STD_525_60) {
578 mvv_write(dev, 0x1a, 0xfc);
579 mvv_write(dev, 0x1b, 0x00);
580 if (height > fg_height)
581 pms_vertdeci(dev, 240, 240);
582 else
583 pms_vertdeci(dev, fg_height, 240);
584 } else {
585 mvv_write(dev, 0x1a, 0x1a);
586 mvv_write(dev, 0x1b, 0x01);
587 if (fg_height > 256)
588 pms_vertdeci(dev, 270, 270);
589 else
590 pms_vertdeci(dev, fg_height, 270);
591 }
592 mvv_write(dev, 0x12, 0);
593 mvv_write(dev, 0x13, MVVMEMORYWIDTH);
594 mvv_write(dev, 0x42, 0x00);
595 mvv_write(dev, 0x43, 0x00);
596 mvv_write(dev, 0x44, MVVMEMORYWIDTH);
597
598 mvv_write(dev, 0x22, width + 8);
599 mvv_write(dev, 0x23, (width + 8) >> 8);
600
601 if (dev->std & V4L2_STD_525_60)
602 pms_horzdeci(dev, width, 640);
603 else
604 pms_horzdeci(dev, width + 8, 768);
605
606 mvv_write(dev, 0x30, mvv_read(dev, 0x30) & 0xfe);
607 mvv_write(dev, 0x08, mvv_read(dev, 0x08) | 0x01);
608 mvv_write(dev, 0x01, mvv_read(dev, 0x01) & 0xfd);
609 mvv_write(dev, 0x32, 0x00);
610 mvv_write(dev, 0x33, MVVMEMORYWIDTH);
611 }
612
613
614 /*
615 * Set Input
616 */
617
618 static void pms_vcrinput(struct pms *dev, short input)
619 {
620 if (dev->decoder == PHILIPS2)
621 pms_i2c_andor(dev, 0x8a, 0x0d, 0x7f, (input & 1) << 7);
622 else if (dev->decoder == PHILIPS1)
623 pms_i2c_andor(dev, 0x42, 0x0d, 0x7f, (input & 1) << 7);
624 }
625
626
627 static int pms_capture(struct pms *dev, char __user *buf, int rgb555, int count)
628 {
629 int y;
630 int dw = 2 * dev->width;
631 char tmp[dw + 32]; /* using a temp buffer is faster than direct */
632 int cnt = 0;
633 int len = 0;
634 unsigned char r8 = 0x5; /* value for reg8 */
635
636 if (rgb555)
637 r8 |= 0x20; /* else use untranslated rgb = 565 */
638 mvv_write(dev, 0x08, r8); /* capture rgb555/565, init DRAM, PC enable */
639
640 /* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
641
642 for (y = 0; y < dev->height; y++) {
643 writeb(0, dev->mem); /* synchronisiert neue Zeile */
644
645 /*
646 * This is in truth a fifo, be very careful as if you
647 * forgot this odd things will occur 8)
648 */
649
650 memcpy_fromio(tmp, dev->mem, dw + 32); /* discard 16 word */
651 cnt -= dev->height;
652 while (cnt <= 0) {
653 /*
654 * Don't copy too far
655 */
656 int dt = dw;
657 if (dt + len > count)
658 dt = count - len;
659 cnt += dev->height;
660 if (copy_to_user(buf, tmp + 32, dt))
661 return len ? len : -EFAULT;
662 buf += dt;
663 len += dt;
664 }
665 }
666 return len;
667 }
668
669
670 /*
671 * Video4linux interfacing
672 */
673
674 static int pms_querycap(struct file *file, void *priv,
675 struct v4l2_capability *vcap)
676 {
677 struct pms *dev = video_drvdata(file);
678
679 strlcpy(vcap->driver, dev->v4l2_dev.name, sizeof(vcap->driver));
680 strlcpy(vcap->card, "Mediavision PMS", sizeof(vcap->card));
681 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
682 "ISA:%s", dev->v4l2_dev.name);
683 vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
684 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
685 return 0;
686 }
687
688 static int pms_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
689 {
690 static const char *inputs[4] = {
691 "Composite",
692 "S-Video",
693 "Composite (VCR)",
694 "S-Video (VCR)"
695 };
696
697 if (vin->index > 3)
698 return -EINVAL;
699 strlcpy(vin->name, inputs[vin->index], sizeof(vin->name));
700 vin->type = V4L2_INPUT_TYPE_CAMERA;
701 vin->audioset = 0;
702 vin->tuner = 0;
703 vin->std = V4L2_STD_ALL;
704 vin->status = 0;
705 return 0;
706 }
707
708 static int pms_g_input(struct file *file, void *fh, unsigned int *inp)
709 {
710 struct pms *dev = video_drvdata(file);
711
712 *inp = dev->input;
713 return 0;
714 }
715
716 static int pms_s_input(struct file *file, void *fh, unsigned int inp)
717 {
718 struct pms *dev = video_drvdata(file);
719
720 if (inp > 3)
721 return -EINVAL;
722
723 dev->input = inp;
724 pms_videosource(dev, inp & 1);
725 pms_vcrinput(dev, inp >> 1);
726 return 0;
727 }
728
729 static int pms_g_std(struct file *file, void *fh, v4l2_std_id *std)
730 {
731 struct pms *dev = video_drvdata(file);
732
733 *std = dev->std;
734 return 0;
735 }
736
737 static int pms_s_std(struct file *file, void *fh, v4l2_std_id *std)
738 {
739 struct pms *dev = video_drvdata(file);
740 int ret = 0;
741
742 dev->std = *std;
743 if (dev->std & V4L2_STD_NTSC) {
744 pms_framerate(dev, 30);
745 pms_secamcross(dev, 0);
746 pms_format(dev, 1);
747 } else if (dev->std & V4L2_STD_PAL) {
748 pms_framerate(dev, 25);
749 pms_secamcross(dev, 0);
750 pms_format(dev, 2);
751 } else if (dev->std & V4L2_STD_SECAM) {
752 pms_framerate(dev, 25);
753 pms_secamcross(dev, 1);
754 pms_format(dev, 2);
755 } else {
756 ret = -EINVAL;
757 }
758 /*
759 switch (v->mode) {
760 case VIDEO_MODE_AUTO:
761 pms_framerate(dev, 25);
762 pms_secamcross(dev, 0);
763 pms_format(dev, 0);
764 break;
765 }*/
766 return ret;
767 }
768
769 static int pms_s_ctrl(struct v4l2_ctrl *ctrl)
770 {
771 struct pms *dev = container_of(ctrl->handler, struct pms, hdl);
772 int ret = 0;
773
774 switch (ctrl->id) {
775 case V4L2_CID_BRIGHTNESS:
776 pms_brightness(dev, ctrl->val);
777 break;
778 case V4L2_CID_CONTRAST:
779 pms_contrast(dev, ctrl->val);
780 break;
781 case V4L2_CID_SATURATION:
782 pms_saturation(dev, ctrl->val);
783 break;
784 case V4L2_CID_HUE:
785 pms_hue(dev, ctrl->val);
786 break;
787 default:
788 ret = -EINVAL;
789 break;
790 }
791 return ret;
792 }
793
794 static int pms_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
795 {
796 struct pms *dev = video_drvdata(file);
797 struct v4l2_pix_format *pix = &fmt->fmt.pix;
798
799 pix->width = dev->width;
800 pix->height = dev->height;
801 pix->pixelformat = dev->width == 15 ?
802 V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB565;
803 pix->field = V4L2_FIELD_NONE;
804 pix->bytesperline = 2 * dev->width;
805 pix->sizeimage = 2 * dev->width * dev->height;
806 /* Just a guess */
807 pix->colorspace = V4L2_COLORSPACE_SRGB;
808 return 0;
809 }
810
811 static int pms_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
812 {
813 struct v4l2_pix_format *pix = &fmt->fmt.pix;
814
815 if (pix->height < 16 || pix->height > 480)
816 return -EINVAL;
817 if (pix->width < 16 || pix->width > 640)
818 return -EINVAL;
819 if (pix->pixelformat != V4L2_PIX_FMT_RGB555 &&
820 pix->pixelformat != V4L2_PIX_FMT_RGB565)
821 return -EINVAL;
822 pix->field = V4L2_FIELD_NONE;
823 pix->bytesperline = 2 * pix->width;
824 pix->sizeimage = 2 * pix->width * pix->height;
825 /* Just a guess */
826 pix->colorspace = V4L2_COLORSPACE_SRGB;
827 return 0;
828 }
829
830 static int pms_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
831 {
832 struct pms *dev = video_drvdata(file);
833 struct v4l2_pix_format *pix = &fmt->fmt.pix;
834 int ret = pms_try_fmt_vid_cap(file, fh, fmt);
835
836 if (ret)
837 return ret;
838 dev->width = pix->width;
839 dev->height = pix->height;
840 dev->depth = (pix->pixelformat == V4L2_PIX_FMT_RGB555) ? 15 : 16;
841 pms_resolution(dev, dev->width, dev->height);
842 /* Ok we figured out what to use from our wide choice */
843 return 0;
844 }
845
846 static int pms_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
847 {
848 static struct v4l2_fmtdesc formats[] = {
849 { 0, 0, 0,
850 "RGB 5:5:5", V4L2_PIX_FMT_RGB555,
851 { 0, 0, 0, 0 }
852 },
853 { 1, 0, 0,
854 "RGB 5:6:5", V4L2_PIX_FMT_RGB565,
855 { 0, 0, 0, 0 }
856 },
857 };
858 enum v4l2_buf_type type = fmt->type;
859
860 if (fmt->index > 1)
861 return -EINVAL;
862
863 *fmt = formats[fmt->index];
864 fmt->type = type;
865 return 0;
866 }
867
868 static ssize_t pms_read(struct file *file, char __user *buf,
869 size_t count, loff_t *ppos)
870 {
871 struct pms *dev = video_drvdata(file);
872 int len;
873
874 len = pms_capture(dev, buf, (dev->depth == 15), count);
875 return len;
876 }
877
878 static unsigned int pms_poll(struct file *file, struct poll_table_struct *wait)
879 {
880 struct v4l2_fh *fh = file->private_data;
881 unsigned int res = POLLIN | POLLRDNORM;
882
883 if (v4l2_event_pending(fh))
884 res |= POLLPRI;
885 poll_wait(file, &fh->wait, wait);
886 return res;
887 }
888
889 static const struct v4l2_file_operations pms_fops = {
890 .owner = THIS_MODULE,
891 .open = v4l2_fh_open,
892 .release = v4l2_fh_release,
893 .poll = pms_poll,
894 .unlocked_ioctl = video_ioctl2,
895 .read = pms_read,
896 };
897
898 static const struct v4l2_ioctl_ops pms_ioctl_ops = {
899 .vidioc_querycap = pms_querycap,
900 .vidioc_g_input = pms_g_input,
901 .vidioc_s_input = pms_s_input,
902 .vidioc_enum_input = pms_enum_input,
903 .vidioc_g_std = pms_g_std,
904 .vidioc_s_std = pms_s_std,
905 .vidioc_enum_fmt_vid_cap = pms_enum_fmt_vid_cap,
906 .vidioc_g_fmt_vid_cap = pms_g_fmt_vid_cap,
907 .vidioc_s_fmt_vid_cap = pms_s_fmt_vid_cap,
908 .vidioc_try_fmt_vid_cap = pms_try_fmt_vid_cap,
909 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
910 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
911 };
912
913 /*
914 * Probe for and initialise the Mediavision PMS
915 */
916
917 static int init_mediavision(struct pms *dev)
918 {
919 int idec, decst;
920 int i;
921 static const unsigned char i2c_defs[] = {
922 0x4c, 0x30, 0x00, 0xe8,
923 0xb6, 0xe2, 0x00, 0x00,
924 0xff, 0xff, 0x00, 0x00,
925 0x00, 0x00, 0x78, 0x98,
926 0x00, 0x00, 0x00, 0x00,
927 0x34, 0x0a, 0xf4, 0xce,
928 0xe4
929 };
930
931 dev->mem = ioremap(mem_base, 0x800);
932 if (!dev->mem)
933 return -ENOMEM;
934
935 if (!request_region(0x9a01, 1, "Mediavision PMS config")) {
936 printk(KERN_WARNING "mediavision: unable to detect: 0x9a01 in use.\n");
937 iounmap(dev->mem);
938 return -EBUSY;
939 }
940 if (!request_region(dev->io, 3, "Mediavision PMS")) {
941 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", dev->io);
942 release_region(0x9a01, 1);
943 iounmap(dev->mem);
944 return -EBUSY;
945 }
946 outb(0xb8, 0x9a01); /* Unlock */
947 outb(dev->io >> 4, 0x9a01); /* Set IO port */
948
949
950 decst = pms_i2c_stat(dev, 0x43);
951
952 if (decst != -1)
953 idec = 2;
954 else if (pms_i2c_stat(dev, 0xb9) != -1)
955 idec = 3;
956 else if (pms_i2c_stat(dev, 0x8b) != -1)
957 idec = 1;
958 else
959 idec = 0;
960
961 printk(KERN_INFO "PMS type is %d\n", idec);
962 if (idec == 0) {
963 release_region(dev->io, 3);
964 release_region(0x9a01, 1);
965 iounmap(dev->mem);
966 return -ENODEV;
967 }
968
969 /*
970 * Ok we have a PMS of some sort
971 */
972
973 mvv_write(dev, 0x04, mem_base >> 12); /* Set the memory area */
974
975 /* Ok now load the defaults */
976
977 for (i = 0; i < 0x19; i++) {
978 if (i2c_defs[i] == 0xff)
979 pms_i2c_andor(dev, 0x8a, i, 0x07, 0x00);
980 else
981 pms_i2c_write(dev, 0x8a, i, i2c_defs[i]);
982 }
983
984 pms_i2c_write(dev, 0xb8, 0x00, 0x12);
985 pms_i2c_write(dev, 0xb8, 0x04, 0x00);
986 pms_i2c_write(dev, 0xb8, 0x07, 0x00);
987 pms_i2c_write(dev, 0xb8, 0x08, 0x00);
988 pms_i2c_write(dev, 0xb8, 0x09, 0xff);
989 pms_i2c_write(dev, 0xb8, 0x0a, 0x00);
990 pms_i2c_write(dev, 0xb8, 0x0b, 0x10);
991 pms_i2c_write(dev, 0xb8, 0x10, 0x03);
992
993 mvv_write(dev, 0x01, 0x00);
994 mvv_write(dev, 0x05, 0xa0);
995 mvv_write(dev, 0x08, 0x25);
996 mvv_write(dev, 0x09, 0x00);
997 mvv_write(dev, 0x0a, 0x20 | MVVMEMORYWIDTH);
998
999 mvv_write(dev, 0x10, 0x02);
1000 mvv_write(dev, 0x1e, 0x0c);
1001 mvv_write(dev, 0x1f, 0x03);
1002 mvv_write(dev, 0x26, 0x06);
1003
1004 mvv_write(dev, 0x2b, 0x00);
1005 mvv_write(dev, 0x2c, 0x20);
1006 mvv_write(dev, 0x2d, 0x00);
1007 mvv_write(dev, 0x2f, 0x70);
1008 mvv_write(dev, 0x32, 0x00);
1009 mvv_write(dev, 0x33, MVVMEMORYWIDTH);
1010 mvv_write(dev, 0x34, 0x00);
1011 mvv_write(dev, 0x35, 0x00);
1012 mvv_write(dev, 0x3a, 0x80);
1013 mvv_write(dev, 0x3b, 0x10);
1014 mvv_write(dev, 0x20, 0x00);
1015 mvv_write(dev, 0x21, 0x00);
1016 mvv_write(dev, 0x30, 0x22);
1017 return 0;
1018 }
1019
1020 /*
1021 * Initialization and module stuff
1022 */
1023
1024 #ifndef MODULE
1025 static int enable;
1026 module_param(enable, int, 0);
1027 #endif
1028
1029 static const struct v4l2_ctrl_ops pms_ctrl_ops = {
1030 .s_ctrl = pms_s_ctrl,
1031 };
1032
1033 static int pms_probe(struct device *pdev, unsigned int card)
1034 {
1035 struct pms *dev;
1036 struct v4l2_device *v4l2_dev;
1037 struct v4l2_ctrl_handler *hdl;
1038 int res;
1039
1040 #ifndef MODULE
1041 if (!enable) {
1042 pr_err("PMS: not enabled, use pms.enable=1 to probe\n");
1043 return -ENODEV;
1044 }
1045 #endif
1046
1047 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1048 if (dev == NULL)
1049 return -ENOMEM;
1050
1051 dev->decoder = PHILIPS2;
1052 dev->io = io_port;
1053 dev->data = io_port + 1;
1054 v4l2_dev = &dev->v4l2_dev;
1055 hdl = &dev->hdl;
1056
1057 res = v4l2_device_register(pdev, v4l2_dev);
1058 if (res < 0) {
1059 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
1060 goto free_dev;
1061 }
1062 v4l2_info(v4l2_dev, "Mediavision Pro Movie Studio driver 0.05\n");
1063
1064 res = init_mediavision(dev);
1065 if (res) {
1066 v4l2_err(v4l2_dev, "Board not found.\n");
1067 goto free_io;
1068 }
1069
1070 v4l2_ctrl_handler_init(hdl, 4);
1071 v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
1072 V4L2_CID_BRIGHTNESS, 0, 255, 1, 139);
1073 v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
1074 V4L2_CID_CONTRAST, 0, 255, 1, 70);
1075 v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
1076 V4L2_CID_SATURATION, 0, 255, 1, 64);
1077 v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
1078 V4L2_CID_HUE, 0, 255, 1, 0);
1079 if (hdl->error) {
1080 res = hdl->error;
1081 goto free_hdl;
1082 }
1083
1084 mutex_init(&dev->lock);
1085 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
1086 dev->vdev.v4l2_dev = v4l2_dev;
1087 dev->vdev.ctrl_handler = hdl;
1088 dev->vdev.fops = &pms_fops;
1089 dev->vdev.ioctl_ops = &pms_ioctl_ops;
1090 dev->vdev.release = video_device_release_empty;
1091 dev->vdev.lock = &dev->lock;
1092 dev->vdev.tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1093 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
1094 video_set_drvdata(&dev->vdev, dev);
1095 dev->std = V4L2_STD_NTSC_M;
1096 dev->height = 240;
1097 dev->width = 320;
1098 dev->depth = 16;
1099 pms_swsense(dev, 75);
1100 pms_resolution(dev, 320, 240);
1101 pms_videosource(dev, 0);
1102 pms_vcrinput(dev, 0);
1103 v4l2_ctrl_handler_setup(hdl);
1104 res = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr);
1105 if (res >= 0)
1106 return 0;
1107
1108 free_hdl:
1109 v4l2_ctrl_handler_free(hdl);
1110 v4l2_device_unregister(&dev->v4l2_dev);
1111 free_io:
1112 release_region(dev->io, 3);
1113 release_region(0x9a01, 1);
1114 iounmap(dev->mem);
1115 free_dev:
1116 kfree(dev);
1117 return res;
1118 }
1119
1120 static int pms_remove(struct device *pdev, unsigned int card)
1121 {
1122 struct pms *dev = dev_get_drvdata(pdev);
1123
1124 video_unregister_device(&dev->vdev);
1125 v4l2_ctrl_handler_free(&dev->hdl);
1126 release_region(dev->io, 3);
1127 release_region(0x9a01, 1);
1128 iounmap(dev->mem);
1129 return 0;
1130 }
1131
1132 static struct isa_driver pms_driver = {
1133 .probe = pms_probe,
1134 .remove = pms_remove,
1135 .driver = {
1136 .name = "pms",
1137 },
1138 };
1139
1140 static int __init pms_init(void)
1141 {
1142 return isa_register_driver(&pms_driver, 1);
1143 }
1144
1145 static void __exit pms_exit(void)
1146 {
1147 isa_unregister_driver(&pms_driver);
1148 }
1149
1150 module_init(pms_init);
1151 module_exit(pms_exit);