2 * stradis.c - stradis 4:2:2 mpeg decoder driver
4 * Stradis 4:2:2 MPEG-2 Decoder Driver
5 * Copyright (C) 1999 Nathan Laredo <laredo@gnu.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/major.h>
28 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/poll.h>
32 #include <linux/pci.h>
33 #include <linux/signal.h>
35 #include <linux/ioport.h>
36 #include <asm/pgtable.h>
38 #include <linux/sched.h>
39 #include <asm/types.h>
40 #include <linux/types.h>
41 #include <linux/interrupt.h>
42 #include <asm/uaccess.h>
43 #include <linux/vmalloc.h>
44 #include <linux/videodev.h>
47 #include "saa7146reg.h"
52 #define DEBUG(x) /* debug driver */
53 #undef IDEBUG /* debug irq handler */
54 #undef MDEBUG /* debug memory management */
58 static struct saa7146 saa7146s[SAA7146_MAX];
60 static int saa_num = 0; /* number of SAA7146s in use */
62 static int video_nr = -1;
63 MODULE_PARM(video_nr,"i");
64 MODULE_LICENSE("GPL");
67 #define nDebNormal 0x00480000
68 #define nDebNoInc 0x00480000
69 #define nDebVideo 0xd0480000
70 #define nDebAudio 0xd0400000
71 #define nDebDMA 0x02c80000
73 #define oDebNormal 0x13c80000
74 #define oDebNoInc 0x13c80000
75 #define oDebVideo 0xd1080000
76 #define oDebAudio 0xd1080000
77 #define oDebDMA 0x03080000
79 #define NewCard (saa->boardcfg[3])
80 #define ChipControl (saa->boardcfg[1])
81 #define NTSCFirstActive (saa->boardcfg[4])
82 #define PALFirstActive (saa->boardcfg[5])
83 #define NTSCLastActive (saa->boardcfg[54])
84 #define PALLastActive (saa->boardcfg[55])
85 #define Have2MB (saa->boardcfg[18] & 0x40)
86 #define HaveCS8420 (saa->boardcfg[18] & 0x04)
87 #define IBMMPEGCD20 (saa->boardcfg[18] & 0x20)
88 #define HaveCS3310 (saa->boardcfg[18] & 0x01)
89 #define CS3310MaxLvl ((saa->boardcfg[30] << 8) | saa->boardcfg[31])
90 #define HaveCS4341 (saa->boardcfg[40] == 2)
91 #define SDIType (saa->boardcfg[27])
92 #define CurrentMode (saa->boardcfg[2])
94 #define debNormal (NewCard ? nDebNormal : oDebNormal)
95 #define debNoInc (NewCard ? nDebNoInc : oDebNoInc)
96 #define debVideo (NewCard ? nDebVideo : oDebVideo)
97 #define debAudio (NewCard ? nDebAudio : oDebAudio)
98 #define debDMA (NewCard ? nDebDMA : oDebDMA)
101 int stradis_driver(void) /* for the benefit of ksymoops */
107 #ifdef USE_RESCUE_EEPROM_SDM275
108 static unsigned char rescue_eeprom[64] = {
109 0x00,0x01,0x04,0x13,0x26,0x0f,0x10,0x00,0x00,0x00,0x43,0x63,0x22,0x01,0x29,0x15,0x73,0x00,0x1f, 'd', 'e', 'c', 'x', 'l', 'd', 'v', 'a',0x02,0x00,0x01,0x00,0xcc,0xa4,0x63,0x09,0xe2,0x10,0x00,0x0a,0x00,0x02,0x02, 'd', 'e', 'c', 'x', 'l', 'a',0x00,0x00,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
113 /* ----------------------------------------------------------------------- */
114 /* Hardware I2C functions */
115 static void I2CWipe(struct saa7146 *saa)
118 /* set i2c to ~=100kHz, abort transfer, clear busy */
119 saawrite(0x600 | SAA7146_I2C_ABORT, SAA7146_I2C_STATUS);
120 saawrite((SAA7146_MC2_UPLD_I2C << 16) |
121 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
122 /* wait for i2c registers to be programmed */
123 for (i = 0; i < 1000 &&
124 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
126 saawrite(0x600, SAA7146_I2C_STATUS);
127 saawrite((SAA7146_MC2_UPLD_I2C << 16) |
128 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
129 /* wait for i2c registers to be programmed */
130 for (i = 0; i < 1000 &&
131 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
133 saawrite(0x600, SAA7146_I2C_STATUS);
134 saawrite((SAA7146_MC2_UPLD_I2C << 16) |
135 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
136 /* wait for i2c registers to be programmed */
137 for (i = 0; i < 1000 &&
138 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
143 static int I2CRead(struct saa7146 *saa, unsigned char addr,
144 unsigned char subaddr, int dosub)
148 if (saaread(SAA7146_I2C_STATUS) & 0x3c)
150 for (i = 0; i < 1000 &&
151 (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
156 saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 8) |
157 ((subaddr & 0xff) << 16) | 0xed, SAA7146_I2C_TRANSFER);
159 saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 16) |
160 0xf1, SAA7146_I2C_TRANSFER);
161 saawrite((SAA7146_MC2_UPLD_I2C << 16) |
162 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
163 /* wait for i2c registers to be programmed */
164 for (i = 0; i < 1000 &&
165 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
167 /* wait for valid data */
168 for (i = 0; i < 1000 &&
169 (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
171 if (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_ERR)
174 printk("i2c setup read timeout\n");
175 saawrite(0x41, SAA7146_I2C_TRANSFER);
176 saawrite((SAA7146_MC2_UPLD_I2C << 16) |
177 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
178 /* wait for i2c registers to be programmed */
179 for (i = 0; i < 1000 &&
180 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
182 /* wait for valid data */
183 for (i = 0; i < 1000 &&
184 (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_BUSY); i++)
186 if (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_ERR)
189 printk("i2c read timeout\n");
190 return ((saaread(SAA7146_I2C_TRANSFER) >> 24) & 0xff);
193 /* set both to write both bytes, reset it to write only b1 */
195 static int I2CWrite(struct saa7146 *saa, unsigned char addr, unsigned char b1,
196 unsigned char b2, int both)
201 if (saaread(SAA7146_I2C_STATUS) & 0x3c)
203 for (i = 0; i < 1000 &&
204 (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
208 data = ((addr & 0xfe) << 24) | ((b1 & 0xff) << 16);
210 data |= ((b2 & 0xff) << 8) | 0xe5;
213 saawrite(data, SAA7146_I2C_TRANSFER);
214 saawrite((SAA7146_MC2_UPLD_I2C << 16) | SAA7146_MC2_UPLD_I2C,
219 static void attach_inform(struct saa7146 *saa, int id)
223 DEBUG(printk(KERN_DEBUG "stradis%d: i2c: device found=%02x\n", saa->nr, id));
224 if (id == 0xa0) { /* we have rev2 or later board, fill in info */
225 for (i = 0; i < 64; i++)
226 saa->boardcfg[i] = I2CRead(saa, 0xa0, i, 1);
227 #ifdef USE_RESCUE_EEPROM_SDM275
228 if (saa->boardcfg[0] != 0) {
229 printk("stradis%d: WARNING: EEPROM STORED VALUES HAVE BEEN IGNORED\n", saa->nr);
230 for (i = 0; i < 64; i++)
231 saa->boardcfg[i] = rescue_eeprom[i];
234 printk("stradis%d: config =", saa->nr);
235 for (i = 0; i < 51; i++) {
236 printk(" %02x",saa->boardcfg[i]);
242 static void I2CBusScan(struct saa7146 *saa)
245 for (i = 0; i < 0xff; i += 2)
246 if ((I2CRead(saa, i, 0, 0)) >= 0)
247 attach_inform(saa, i);
250 static int debiwait_maxwait = 0;
252 static int wait_for_debi_done(struct saa7146 *saa)
256 /* wait for registers to be programmed */
257 for (i = 0; i < 100000 &&
258 !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_DEBI); i++)
259 saaread(SAA7146_MC2);
260 /* wait for transfer to complete */
261 for (i = 0; i < 500000 &&
262 (saaread(SAA7146_PSR) & SAA7146_PSR_DEBI_S); i++)
263 saaread(SAA7146_MC2);
264 if (i > debiwait_maxwait)
265 printk("wait-for-debi-done maxwait: %d\n",
266 debiwait_maxwait = i);
273 static int debiwrite(struct saa7146 *saa, u32 config, int addr,
277 if (count <= 0 || count > 32764)
279 if (wait_for_debi_done(saa) < 0)
281 saawrite(config, SAA7146_DEBI_CONFIG);
282 if (count <= 4) /* immediate transfer */
283 saawrite(val, SAA7146_DEBI_AD);
284 else /* block transfer */
285 saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
286 saawrite((cmd = (count << 17) | (addr & 0xffff)), SAA7146_DEBI_COMMAND);
287 saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
292 static u32 debiread(struct saa7146 *saa, u32 config, int addr, int count)
296 if (count > 32764 || count <= 0)
298 if (wait_for_debi_done(saa) < 0)
300 saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
301 saawrite((count << 17) | 0x10000 | (addr & 0xffff),
302 SAA7146_DEBI_COMMAND);
303 saawrite(config, SAA7146_DEBI_CONFIG);
304 saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
306 if (count > 4) /* not an immediate transfer */
308 wait_for_debi_done(saa);
309 result = saaread(SAA7146_DEBI_AD);
320 /* MUST be a multiple of 8 bytes and 8-byte aligned and < 32768 bytes */
321 /* data copied into saa->dmadebi buffer, caller must re-enable interrupts */
322 static void ibm_block_dram_read(struct saa7146 *saa, int address, int bytes)
326 buf = (u32 *) saa->dmadebi;
329 saawrite(0, SAA7146_IER); /* disable interrupts */
330 for (i=0; i < 10000 &&
331 (debiread(saa, debNormal, IBM_MP2_DRAM_CMD_STAT, 2)
333 saaread(SAA7146_MC2);
335 printk(KERN_ERR "stradis%d: dram_busy never cleared\n",
337 debiwrite(saa, debNormal, IBM_MP2_SRC_ADDR, (address<<16) |
339 debiwrite(saa, debNormal, IBM_MP2_BLOCK_SIZE, bytes, 2);
340 debiwrite(saa, debNormal, IBM_MP2_CMD_STAT, 0x8a10, 2);
341 for (j = 0; j < bytes/4; j++) {
342 for (i = 0; i < 10000 &&
343 (!(debiread(saa, debNormal, IBM_MP2_DRAM_CMD_STAT, 2)
345 saaread(SAA7146_MC2);
347 printk(KERN_ERR "stradis%d: dram_ready never set\n",
349 buf[j] = debiread(saa, debNormal, IBM_MP2_DRAM_DATA, 4);
354 static void do_irq_send_data(struct saa7146 *saa)
356 int split, audbytes, vidbytes;
358 saawrite(SAA7146_PSR_PIN1, SAA7146_IER);
359 /* if special feature mode in effect, disable audio sending */
360 if (saa->playmode != VID_PLAY_NORMAL)
361 saa->audtail = saa->audhead = 0;
362 if (saa->audhead <= saa->audtail)
363 audbytes = saa->audtail - saa->audhead;
365 audbytes = 65536 - (saa->audhead - saa->audtail);
366 if (saa->vidhead <= saa->vidtail)
367 vidbytes = saa->vidtail - saa->vidhead;
369 vidbytes = 524288 - (saa->vidhead - saa->vidtail);
370 if (audbytes == 0 && vidbytes == 0 && saa->osdtail == saa->osdhead) {
371 saawrite(0, SAA7146_IER);
374 /* if at least 1 block audio waiting and audio fifo isn't full */
375 if (audbytes >= 2048 && (debiread(saa, debNormal,
376 IBM_MP2_AUD_FIFO, 2) & 0xff) < 60) {
377 if (saa->audhead > saa->audtail)
378 split = 65536 - saa->audhead;
382 if (split > 0 && split < 2048) {
383 memcpy(saa->dmadebi, saa->audbuf + saa->audhead,
389 memcpy(saa->dmadebi + split, saa->audbuf + saa->audhead,
391 saa->audhead += audbytes;
392 saa->audhead &= 0xffff;
393 debiwrite(saa, debAudio, (NewCard? IBM_MP2_AUD_FIFO :
394 IBM_MP2_AUD_FIFOW), 0, 2048);
395 wake_up_interruptible(&saa->audq);
396 /* if at least 1 block video waiting and video fifo isn't full */
397 } else if (vidbytes >= 30720 && (debiread(saa, debNormal,
398 IBM_MP2_FIFO, 2)) < 16384) {
399 if (saa->vidhead > saa->vidtail)
400 split = 524288 - saa->vidhead;
404 if (split > 0 && split < 30720) {
405 memcpy(saa->dmadebi, saa->vidbuf + saa->vidhead,
411 memcpy(saa->dmadebi + split, saa->vidbuf + saa->vidhead,
413 saa->vidhead += vidbytes;
414 saa->vidhead &= 0x7ffff;
415 debiwrite(saa, debVideo, (NewCard ? IBM_MP2_FIFO :
416 IBM_MP2_FIFOW), 0, 30720);
417 wake_up_interruptible(&saa->vidq);
419 saawrite(SAA7146_PSR_DEBI_S | SAA7146_PSR_PIN1, SAA7146_IER);
422 static void send_osd_data(struct saa7146 *saa)
424 int size = saa->osdtail - saa->osdhead;
427 /* ensure some multiple of 8 bytes is transferred */
428 size = 8 * ((size + 8)>>3);
430 debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR,
431 (saa->osdhead>>3), 2);
432 memcpy(saa->dmadebi, &saa->osdbuf[saa->osdhead], size);
433 saa->osdhead += size;
434 /* block transfer of next 8 bytes to ~32k bytes */
435 debiwrite(saa, debNormal, IBM_MP2_OSD_DATA, 0, size);
437 if (saa->osdhead >= saa->osdtail) {
438 saa->osdhead = saa->osdtail = 0;
439 debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
443 static irqreturn_t saa7146_irq(int irq, void *dev_id, struct pt_regs *regs)
445 struct saa7146 *saa = (struct saa7146 *) dev_id;
452 /* get/clear interrupt status bits */
453 stat = saaread(SAA7146_ISR);
454 astat = stat & saaread(SAA7146_IER);
458 saawrite(astat, SAA7146_ISR);
459 if (astat & SAA7146_PSR_DEBI_S) {
460 do_irq_send_data(saa);
462 if (astat & SAA7146_PSR_PIN1) {
464 /* the following read will trigger DEBI_S */
465 istat = debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
467 saawrite(0, SAA7146_IER);
469 saawrite(SAA7146_PSR_DEBI_S |
470 SAA7146_PSR_PIN1, SAA7146_IER);
472 if (istat & 0x20) { /* Video Start */
473 saa->vidinfo.frame_count++;
475 if (istat & 0x400) { /* Picture Start */
476 /* update temporal reference */
478 if (istat & 0x200) { /* Picture Resolution Change */
479 /* read new resolution */
481 if (istat & 0x100) { /* New User Data found */
482 /* read new user data */
484 if (istat & 0x1000) { /* new GOP/SMPTE */
487 if (istat & 0x8000) { /* Sequence Start Code */
488 /* reset frame counter, load sizes */
489 saa->vidinfo.frame_count = 0;
490 saa->vidinfo.h_size = 704;
491 saa->vidinfo.v_size = 480;
493 if (saa->endmarkhead != saa->endmarktail) {
495 saa->endmark[saa->endmarkhead];
497 if (saa->endmarkhead >= MAX_MARKS)
498 saa->endmarkhead = 0;
502 if (istat & 0x4000) { /* Sequence Error Code */
503 if (saa->endmarkhead != saa->endmarktail) {
505 saa->endmark[saa->endmarkhead];
507 if (saa->endmarkhead >= MAX_MARKS)
508 saa->endmarkhead = 0;
513 if (astat & SAA7146_PSR_PPEF) {
514 IDEBUG(printk("stradis%d irq: PPEF\n", saa->nr));
516 if (astat & SAA7146_PSR_PABO) {
517 IDEBUG(printk("stradis%d irq: PABO\n", saa->nr));
519 if (astat & SAA7146_PSR_PPED) {
520 IDEBUG(printk("stradis%d irq: PPED\n", saa->nr));
522 if (astat & SAA7146_PSR_RPS_I1) {
523 IDEBUG(printk("stradis%d irq: RPS_I1\n", saa->nr));
525 if (astat & SAA7146_PSR_RPS_I0) {
526 IDEBUG(printk("stradis%d irq: RPS_I0\n", saa->nr));
528 if (astat & SAA7146_PSR_RPS_LATE1) {
529 IDEBUG(printk("stradis%d irq: RPS_LATE1\n", saa->nr));
531 if (astat & SAA7146_PSR_RPS_LATE0) {
532 IDEBUG(printk("stradis%d irq: RPS_LATE0\n", saa->nr));
534 if (astat & SAA7146_PSR_RPS_E1) {
535 IDEBUG(printk("stradis%d irq: RPS_E1\n", saa->nr));
537 if (astat & SAA7146_PSR_RPS_E0) {
538 IDEBUG(printk("stradis%d irq: RPS_E0\n", saa->nr));
540 if (astat & SAA7146_PSR_RPS_TO1) {
541 IDEBUG(printk("stradis%d irq: RPS_TO1\n", saa->nr));
543 if (astat & SAA7146_PSR_RPS_TO0) {
544 IDEBUG(printk("stradis%d irq: RPS_TO0\n", saa->nr));
546 if (astat & SAA7146_PSR_UPLD) {
547 IDEBUG(printk("stradis%d irq: UPLD\n", saa->nr));
549 if (astat & SAA7146_PSR_DEBI_E) {
550 IDEBUG(printk("stradis%d irq: DEBI_E\n", saa->nr));
552 if (astat & SAA7146_PSR_I2C_S) {
553 IDEBUG(printk("stradis%d irq: I2C_S\n", saa->nr));
555 if (astat & SAA7146_PSR_I2C_E) {
556 IDEBUG(printk("stradis%d irq: I2C_E\n", saa->nr));
558 if (astat & SAA7146_PSR_A2_IN) {
559 IDEBUG(printk("stradis%d irq: A2_IN\n", saa->nr));
561 if (astat & SAA7146_PSR_A2_OUT) {
562 IDEBUG(printk("stradis%d irq: A2_OUT\n", saa->nr));
564 if (astat & SAA7146_PSR_A1_IN) {
565 IDEBUG(printk("stradis%d irq: A1_IN\n", saa->nr));
567 if (astat & SAA7146_PSR_A1_OUT) {
568 IDEBUG(printk("stradis%d irq: A1_OUT\n", saa->nr));
570 if (astat & SAA7146_PSR_AFOU) {
571 IDEBUG(printk("stradis%d irq: AFOU\n", saa->nr));
573 if (astat & SAA7146_PSR_V_PE) {
574 IDEBUG(printk("stradis%d irq: V_PE\n", saa->nr));
576 if (astat & SAA7146_PSR_VFOU) {
577 IDEBUG(printk("stradis%d irq: VFOU\n", saa->nr));
579 if (astat & SAA7146_PSR_FIDA) {
580 IDEBUG(printk("stradis%d irq: FIDA\n", saa->nr));
582 if (astat & SAA7146_PSR_FIDB) {
583 IDEBUG(printk("stradis%d irq: FIDB\n", saa->nr));
585 if (astat & SAA7146_PSR_PIN3) {
586 IDEBUG(printk("stradis%d irq: PIN3\n", saa->nr));
588 if (astat & SAA7146_PSR_PIN2) {
589 IDEBUG(printk("stradis%d irq: PIN2\n", saa->nr));
591 if (astat & SAA7146_PSR_PIN0) {
592 IDEBUG(printk("stradis%d irq: PIN0\n", saa->nr));
594 if (astat & SAA7146_PSR_ECS) {
595 IDEBUG(printk("stradis%d irq: ECS\n", saa->nr));
597 if (astat & SAA7146_PSR_EC3S) {
598 IDEBUG(printk("stradis%d irq: EC3S\n", saa->nr));
600 if (astat & SAA7146_PSR_EC0S) {
601 IDEBUG(printk("stradis%d irq: EC0S\n", saa->nr));
606 printk(KERN_WARNING "stradis%d: irq loop %d\n",
609 saawrite(0, SAA7146_IER);
611 "stradis%d: IRQ loop cleared\n", saa->nr);
614 return IRQ_RETVAL(handled);
617 static int ibm_send_command(struct saa7146 *saa,
618 int command, int data, int chain)
623 debiwrite(saa, debNormal, IBM_MP2_COMMAND, (command << 1) | 1, 2);
625 debiwrite(saa, debNormal, IBM_MP2_COMMAND, command << 1, 2);
626 debiwrite(saa, debNormal, IBM_MP2_CMD_DATA, data, 2);
627 debiwrite(saa, debNormal, IBM_MP2_CMD_STAT, 1, 2);
628 for (i = 0; i < 100 &&
629 (debiread(saa, debNormal, IBM_MP2_CMD_STAT, 2) & 1); i++)
636 static void cs4341_setlevel(struct saa7146 *saa, int left, int right)
638 I2CWrite(saa, 0x22, 0x03, left > 94 ? 94 : left, 2);
639 I2CWrite(saa, 0x22, 0x04, right > 94 ? 94 : right, 2);
642 static void initialize_cs4341(struct saa7146 *saa)
645 for (i = 0; i < 200; i++) {
646 /* auto mute off, power on, no de-emphasis */
647 /* I2S data up to 24-bit 64xFs internal SCLK */
648 I2CWrite(saa, 0x22, 0x01, 0x11, 2);
649 /* ATAPI mixer settings */
650 I2CWrite(saa, 0x22, 0x02, 0x49, 2);
651 /* attenuation left 3db */
652 I2CWrite(saa, 0x22, 0x03, 0x00, 2);
653 /* attenuation right 3db */
654 I2CWrite(saa, 0x22, 0x04, 0x00, 2);
655 I2CWrite(saa, 0x22, 0x01, 0x10, 2);
656 if (I2CRead(saa, 0x22, 0x02, 1) == 0x49)
660 printk("stradis%d: CS4341 initialized (%d)\n", saa->nr, i);
664 static void initialize_cs8420(struct saa7146 *saa, int pro)
669 sequence = mode8420pro;
671 sequence = mode8420con;
672 for (i = 0; i < INIT8420LEN; i++)
673 I2CWrite(saa, 0x20, init8420[i * 2],
674 init8420[i * 2 + 1], 2);
675 for (i = 0; i < MODE8420LEN; i++)
676 I2CWrite(saa, 0x20, sequence[i * 2],
677 sequence[i * 2 + 1], 2);
678 printk("stradis%d: CS8420 initialized\n", saa->nr);
681 static void initialize_saa7121(struct saa7146 *saa, int dopal)
686 sequence = init7121pal;
688 sequence = init7121ntsc;
689 mod = saaread(SAA7146_PSR) & 0x08;
690 /* initialize PAL/NTSC video encoder */
691 for (i = 0; i < INIT7121LEN; i++) {
692 if (NewCard) { /* handle new card encoder differences */
693 if (sequence[i*2] == 0x3a)
694 I2CWrite(saa, 0x88, 0x3a, 0x13, 2);
695 else if (sequence[i*2] == 0x6b)
696 I2CWrite(saa, 0x88, 0x6b, 0x20, 2);
697 else if (sequence[i*2] == 0x6c)
698 I2CWrite(saa, 0x88, 0x6c,
699 dopal ? 0x09 : 0xf5, 2);
700 else if (sequence[i*2] == 0x6d)
701 I2CWrite(saa, 0x88, 0x6d,
702 dopal ? 0x20 : 0x00, 2);
703 else if (sequence[i*2] == 0x7a)
704 I2CWrite(saa, 0x88, 0x7a,
705 dopal ? (PALFirstActive - 1) :
706 (NTSCFirstActive - 4), 2);
707 else if (sequence[i*2] == 0x7b)
708 I2CWrite(saa, 0x88, 0x7b,
709 dopal ? PALLastActive :
711 else I2CWrite(saa, 0x88, sequence[i * 2],
712 sequence[i * 2 + 1], 2);
714 if (sequence[i*2] == 0x6b && mod)
715 I2CWrite(saa, 0x88, 0x6b,
716 (sequence[i * 2 + 1] ^ 0x09), 2);
717 else if (sequence[i*2] == 0x7a)
718 I2CWrite(saa, 0x88, 0x7a,
719 dopal ? (PALFirstActive - 1) :
720 (NTSCFirstActive - 4), 2);
721 else if (sequence[i*2] == 0x7b)
722 I2CWrite(saa, 0x88, 0x7b,
723 dopal ? PALLastActive :
726 I2CWrite(saa, 0x88, sequence[i * 2],
727 sequence[i * 2 + 1], 2);
732 static void set_genlock_offset(struct saa7146 *saa, int noffset)
735 int PixelsPerLine = 858;
736 if (CurrentMode == VIDEO_MODE_PAL)
740 else if (noffset < -500)
742 nCode = noffset + 0x100;
745 else if (nCode < 1) nCode = 0x400 + PixelsPerLine + nCode;
746 debiwrite(saa, debNormal, XILINX_GLDELAY, nCode, 2);
749 static void set_out_format(struct saa7146 *saa, int mode)
751 initialize_saa7121(saa, (mode == VIDEO_MODE_NTSC ? 0 : 1));
752 saa->boardcfg[2] = mode;
753 /* do not adjust analog video parameters here, use saa7121 init */
754 /* you will affect the SDI output on the new card */
755 if (mode == VIDEO_MODE_PAL) { /* PAL */
756 debiwrite(saa, debNormal, XILINX_CTL0, 0x0808, 2);
758 saawrite(0x012002c0, SAA7146_NUM_LINE_BYTE1);
760 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
764 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
765 NewCard ? 0xe500: 0x6500, 2);
766 debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
768 (NewCard ? PALFirstActive : PALFirstActive-6), 2);
770 debiwrite(saa, debNormal, XILINX_CTL0, 0x0800, 2);
772 saawrite(0x00f002c0, SAA7146_NUM_LINE_BYTE1);
773 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
774 NewCard ? 0xe100: 0x6100, 2);
775 debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
777 (NewCard ? NTSCFirstActive : NTSCFirstActive-6), 2);
782 /* Intialize bitmangler to map from a byte value to the mangled word that
783 * must be output to program the Xilinx part through the DEBI port.
784 * Xilinx Data Bit->DEBI Bit: 0->15 1->7 2->6 3->12 4->11 5->2 6->1 7->0
785 * transfer FPGA code, init IBM chip, transfer IBM microcode
786 * rev2 card mangles: 0->7 1->6 2->5 3->4 4->3 5->2 6->1 7->0
788 static u16 bitmangler[256];
790 static int initialize_fpga(struct video_code *bitdata)
792 int i, num, startindex, failure = 0, loadtwo, loadfile = 0;
797 /* verify fpga code */
798 for (startindex = 0; startindex < bitdata->datasize; startindex++)
799 if (bitdata->data[startindex] == 255)
801 if (startindex == bitdata->datasize) {
802 printk(KERN_INFO "stradis: bad fpga code\n");
805 /* initialize all detected cards */
806 for (num = 0; num < saa_num; num++) {
807 saa = &saa7146s[num];
808 if (saa->boardcfg[0] > 20)
809 continue; /* card was programmed */
810 loadtwo = (saa->boardcfg[18] & 0x10);
811 if (!NewCard) /* we have an old board */
812 for (i = 0; i < 256; i++)
813 bitmangler[i] = ((i & 0x01) << 15) |
814 ((i & 0x02) << 6) | ((i & 0x04) << 4) |
815 ((i & 0x08) << 9) | ((i & 0x10) << 7) |
816 ((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
818 else /* else we have a new board */
819 for (i = 0; i < 256; i++)
820 bitmangler[i] = ((i & 0x01) << 7) |
821 ((i & 0x02) << 5) | ((i & 0x04) << 3) |
822 ((i & 0x08) << 1) | ((i & 0x10) >> 1) |
823 ((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
826 dmabuf = (u16 *) saa->dmadebi;
827 newdma = (u8 *) saa->dmadebi;
828 if (NewCard) { /* SDM2xxx */
829 if (!strncmp(bitdata->loadwhat, "decoder2", 8))
830 continue; /* fpga not for this card */
831 if (!strncmp(&saa->boardcfg[42],
832 bitdata->loadwhat, 8)) {
834 } else if (loadtwo && !strncmp(&saa->boardcfg[19],
835 bitdata->loadwhat, 8)) {
837 } else if (!saa->boardcfg[42] && /* special */
838 !strncmp("decxl", bitdata->loadwhat, 8)) {
841 continue; /* fpga not for this card */
842 if (loadfile != 1 && loadfile != 2) {
843 continue; /* skip to next card */
845 if (saa->boardcfg[0] && loadfile == 1 )
846 continue; /* skip to next card */
847 if (saa->boardcfg[0] != 1 && loadfile == 2)
848 continue; /* skip to next card */
849 saa->boardcfg[0]++; /* mark fpga handled */
850 printk("stradis%d: loading %s\n", saa->nr,
852 if (loadtwo && loadfile == 2)
853 goto send_fpga_stuff;
854 /* turn on the Audio interface to set PROG low */
855 saawrite(0x00400040, SAA7146_GPIO_CTRL);
856 saaread(SAA7146_PSR); /* ensure posted write */
857 /* wait for everyone to reset */
859 saawrite(0x00400000, SAA7146_GPIO_CTRL);
860 } else { /* original card */
861 if (strncmp(bitdata->loadwhat, "decoder2", 8))
862 continue; /* fpga not for this card */
863 /* Pull the Xilinx PROG signal WS3 low */
864 saawrite(0x02000200, SAA7146_MC1);
865 /* Turn on the Audio interface so can set PROG low */
866 saawrite(0x000000c0, SAA7146_ACON1);
867 /* Pull the Xilinx INIT signal (GPIO2) low */
868 saawrite(0x00400000, SAA7146_GPIO_CTRL);
869 /* Make sure everybody resets */
870 saaread(SAA7146_PSR); /* ensure posted write */
872 /* Release the Xilinx PROG signal */
873 saawrite(0x00000000, SAA7146_ACON1);
874 /* Turn off the Audio interface */
875 saawrite(0x02000000, SAA7146_MC1);
877 /* Release Xilinx INIT signal (WS2) */
878 saawrite(0x00000000, SAA7146_GPIO_CTRL);
879 /* Wait for the INIT to go High */
880 for (i = 0; i < 10000 &&
881 !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2); i++)
884 printk(KERN_INFO "stradis%d: no fpga INIT\n", saa->nr);
889 for (i = startindex; i < bitdata->datasize; i++)
890 newdma[i - startindex] =
891 bitmangler[bitdata->data[i]];
892 debiwrite(saa, 0x01420000, 0, 0,
893 ((bitdata->datasize - startindex) + 5));
897 "awaiting 2nd FPGA bitfile\n",
899 continue; /* skip to next card */
904 for (i = startindex; i < bitdata->datasize; i++)
905 dmabuf[i - startindex] =
906 bitmangler[bitdata->data[i]];
907 debiwrite(saa, 0x014a0000, 0, 0,
908 ((bitdata->datasize - startindex) + 5) * 2);
910 for (i = 0; i < 1000 &&
911 !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2); i++)
914 printk(KERN_INFO "stradis%d: FPGA load failed\n",
920 /* Pull the Xilinx INIT signal (GPIO2) low */
921 saawrite(0x00400000, SAA7146_GPIO_CTRL);
922 saaread(SAA7146_PSR); /* ensure posted write */
924 saawrite(0x00000000, SAA7146_GPIO_CTRL);
927 printk(KERN_INFO "stradis%d: FPGA Loaded\n", saa->nr);
928 saa->boardcfg[0] = 26; /* mark fpga programmed */
929 /* set VXCO to its lowest frequency */
930 debiwrite(saa, debNormal, XILINX_PWM, 0, 2);
934 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT,
936 /* set VXCO to PWM mode, release reset, blank on */
937 debiwrite(saa, debNormal, XILINX_CTL0, 0xffc4, 2);
941 debiwrite(saa, debNormal, XILINX_CTL0,
944 /* set source Black */
945 debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
946 saa->boardcfg[4] = 22; /* set NTSC First Active Line */
947 saa->boardcfg[5] = 23; /* set PAL First Active Line */
948 saa->boardcfg[54] = 2; /* set NTSC Last Active Line - 256 */
949 saa->boardcfg[55] = 54; /* set PAL Last Active Line - 256 */
950 set_out_format(saa, VIDEO_MODE_NTSC);
952 /* begin IBM chip init */
953 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
954 saaread(SAA7146_PSR); /* wait for reset */
956 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
957 debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
958 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0x10, 2);
959 debiwrite(saa, debNormal, IBM_MP2_CMD_ADDR, 0, 2);
960 debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
963 /* set i2s rate converter to 48KHz */
964 debiwrite(saa, debNormal, 0x80c0, 6, 2);
965 /* we must init CS8420 first since rev b pulls i2s */
966 /* master clock low and CS4341 needs i2s master to */
967 /* run the i2c port. */
969 /* 0=consumer, 1=pro */
970 initialize_cs8420(saa, 0);
974 initialize_cs4341(saa);
976 debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
977 debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
978 debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
979 debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
981 set_genlock_offset(saa, 0);
982 debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
985 debiwrite(saa, debNormal, XILINX_CTL0, 0x8000, 2);
987 /* disable genlock */
988 debiwrite(saa, debNormal, XILINX_CTL0, 0x8080, 2);
994 static int do_ibm_reset(struct saa7146 *saa)
996 /* failure if decoder not previously programmed */
997 if (saa->boardcfg[0] < 37)
1001 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, 0, 2);
1002 /* disable interrupts */
1003 saawrite(0, SAA7146_IER);
1004 saa->audhead = saa->audtail = 0;
1005 saa->vidhead = saa->vidtail = 0;
1006 /* tristate debi bus, disable debi transfers */
1007 saawrite(0x00880000, SAA7146_MC1);
1008 /* ensure posted write */
1009 saaread(SAA7146_MC1);
1011 /* re-enable debi transfers */
1012 saawrite(0x00880088, SAA7146_MC1);
1013 /* set source Black */
1014 debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
1015 /* begin IBM chip init */
1016 set_out_format(saa, CurrentMode);
1017 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
1018 saaread(SAA7146_PSR); /* wait for reset */
1020 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
1021 debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
1022 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
1023 debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
1026 /* set i2s rate converter to 48KHz */
1027 debiwrite(saa, debNormal, 0x80c0, 6, 2);
1028 /* we must init CS8420 first since rev b pulls i2s */
1029 /* master clock low and CS4341 needs i2s master to */
1030 /* run the i2c port. */
1032 /* 0=consumer, 1=pro */
1033 initialize_cs8420(saa, 1);
1037 initialize_cs4341(saa);
1039 debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
1040 debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
1041 debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
1042 debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
1044 set_genlock_offset(saa, 0);
1045 debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
1046 debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
1047 debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
1048 if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
1049 (ChipControl == 0x43 ? 0xe800 : 0xe000), 1)) {
1050 printk(KERN_ERR "stradis%d: IBM config failed\n", saa->nr);
1053 int i = CS3310MaxLvl;
1054 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, ((i<<8)|i), 2);
1056 /* start video decoder */
1057 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
1058 /* 256k vid, 3520 bytes aud */
1059 debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD, 0x4037, 2);
1060 debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
1061 ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
1062 /* enable buffer threshold irq */
1063 debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
1064 /* clear pending interrupts */
1065 debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
1066 debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
1070 /* load the decoder microcode */
1071 static int initialize_ibmmpeg2(struct video_code *microcode)
1074 struct saa7146 *saa;
1076 for (num = 0; num < saa_num; num++) {
1077 saa = &saa7146s[num];
1078 /* check that FPGA is loaded */
1079 debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0xa55a, 2);
1080 if ((i = debiread(saa, debNormal, IBM_MP2_OSD_SIZE, 2)) !=
1082 printk(KERN_INFO "stradis%d: %04x != 0xa55a\n",
1088 if (!strncmp(microcode->loadwhat, "decoder.vid", 11)) {
1089 if (saa->boardcfg[0] > 27)
1090 continue; /* skip to next card */
1091 /* load video control store */
1092 saa->boardcfg[1] = 0x13; /* no-sync default */
1093 debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
1094 debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
1095 for (i = 0; i < microcode->datasize / 2; i++)
1096 debiwrite(saa, debNormal, IBM_MP2_PROC_IDATA,
1097 (microcode->data[i * 2] << 8) |
1098 microcode->data[i * 2 + 1], 2);
1099 debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
1100 debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
1101 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
1103 saa->boardcfg[0] = 28;
1105 if (!strncmp(microcode->loadwhat, "decoder.aud", 11)) {
1106 if (saa->boardcfg[0] > 35)
1107 continue; /* skip to next card */
1108 /* load audio control store */
1109 debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
1110 debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
1111 for (i = 0; i < microcode->datasize; i++)
1112 debiwrite(saa, debNormal, IBM_MP2_AUD_IDATA,
1113 microcode->data[i], 1);
1114 debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
1115 debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
1116 debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
1117 debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
1118 if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
1121 "stradis%d: IBM config failed\n",
1125 /* set PWM to center value */
1127 debiwrite(saa, debNormal, XILINX_PWM,
1129 (saa->boardcfg[13]<<8), 2);
1131 debiwrite(saa, debNormal, XILINX_PWM,
1135 debiwrite(saa, debNormal,
1136 XILINX_CS3310_CMPLT, ((i<<8)|i), 2);
1139 "stradis%d: IBM MPEGCD%d Initialized\n",
1140 saa->nr, 18 + (debiread(saa, debNormal,
1141 IBM_MP2_CHIP_CONTROL, 2) >> 12));
1142 /* start video decoder */
1143 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
1145 debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD,
1146 0x4037, 2); /* 256k vid, 3520 bytes aud */
1147 debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
1148 ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
1149 /* enable buffer threshold irq */
1150 debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
1151 debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
1152 /* enable gpio irq */
1153 saawrite(0x00002000, SAA7146_GPIO_CTRL);
1154 /* enable decoder output to HPS */
1155 debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
1156 saa->boardcfg[0] = 37;
1162 static u32 palette2fmt[] =
1163 { /* some of these YUV translations are wrong */
1164 0xffffffff, 0x86000000, 0x87000000, 0x80000000, 0x8100000, 0x82000000,
1165 0x83000000, 0x00000000, 0x03000000, 0x03000000, 0x0a00000, 0x03000000,
1166 0x06000000, 0x00000000, 0x03000000, 0x0a000000, 0x0300000
1168 static int bpp2fmt[4] =
1170 VIDEO_PALETTE_HI240, VIDEO_PALETTE_RGB565, VIDEO_PALETTE_RGB24,
1174 /* I wish I could find a formula to calculate these... */
1175 static u32 h_prescale[64] =
1177 0x10000000, 0x18040202, 0x18080000, 0x380c0606, 0x38100204, 0x38140808,
1178 0x38180000, 0x381c0000, 0x3820161c, 0x38242a3b, 0x38281230, 0x382c4460,
1179 0x38301040, 0x38340080, 0x38380000, 0x383c0000, 0x3840fefe, 0x3844ee9f,
1180 0x3848ee9f, 0x384cee9f, 0x3850ee9f, 0x38542a3b, 0x38581230, 0x385c0000,
1181 0x38600000, 0x38640000, 0x38680000, 0x386c0000, 0x38700000, 0x38740000,
1182 0x38780000, 0x387c0000, 0x30800000, 0x38840000, 0x38880000, 0x388c0000,
1183 0x38900000, 0x38940000, 0x38980000, 0x389c0000, 0x38a00000, 0x38a40000,
1184 0x38a80000, 0x38ac0000, 0x38b00000, 0x38b40000, 0x38b80000, 0x38bc0000,
1185 0x38c00000, 0x38c40000, 0x38c80000, 0x38cc0000, 0x38d00000, 0x38d40000,
1186 0x38d80000, 0x38dc0000, 0x38e00000, 0x38e40000, 0x38e80000, 0x38ec0000,
1187 0x38f00000, 0x38f40000, 0x38f80000, 0x38fc0000,
1189 static u32 v_gain[64] =
1191 0x016000ff, 0x016100ff, 0x016100ff, 0x016200ff, 0x016200ff, 0x016200ff,
1192 0x016200ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff,
1193 0x016300ff, 0x016300ff, 0x016300ff, 0x016400ff, 0x016400ff, 0x016400ff,
1194 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1195 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1196 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1197 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1198 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1199 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1200 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1201 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1205 static void saa7146_set_winsize(struct saa7146 *saa)
1208 int offset, yacl, ysci;
1209 saa->win.color_fmt = format =
1210 (saa->win.depth == 15) ? palette2fmt[VIDEO_PALETTE_RGB555] :
1211 palette2fmt[bpp2fmt[(saa->win.bpp - 1) & 3]];
1212 offset = saa->win.x * saa->win.bpp + saa->win.y * saa->win.bpl;
1213 saawrite(saa->win.vidadr + offset, SAA7146_BASE_EVEN1);
1214 saawrite(saa->win.vidadr + offset + saa->win.bpl, SAA7146_BASE_ODD1);
1215 saawrite(saa->win.bpl * 2, SAA7146_PITCH1);
1216 saawrite(saa->win.vidadr + saa->win.bpl * saa->win.sheight,
1217 SAA7146_PROT_ADDR1);
1218 saawrite(0, SAA7146_PAGE1);
1219 saawrite(format|0x60, SAA7146_CLIP_FORMAT_CTRL);
1220 offset = (704 / (saa->win.width - 1)) & 0x3f;
1221 saawrite(h_prescale[offset], SAA7146_HPS_H_PRESCALE);
1222 offset = (720896 / saa->win.width) / (offset + 1);
1223 saawrite((offset<<12)|0x0c, SAA7146_HPS_H_SCALE);
1224 if (CurrentMode == VIDEO_MODE_NTSC) {
1225 yacl = /*(480 / saa->win.height - 1) & 0x3f*/ 0;
1226 ysci = 1024 - (saa->win.height * 1024 / 480);
1228 yacl = /*(576 / saa->win.height - 1) & 0x3f*/ 0;
1229 ysci = 1024 - (saa->win.height * 1024 / 576);
1231 saawrite((1<<31)|(ysci<<21)|(yacl<<15), SAA7146_HPS_V_SCALE);
1232 saawrite(v_gain[yacl], SAA7146_HPS_V_GAIN);
1233 saawrite(((SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_HPS_V |
1234 SAA7146_MC2_UPLD_HPS_H) << 16) | (SAA7146_MC2_UPLD_DMA1 |
1235 SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_HPS_H),
1239 /* clip_draw_rectangle(cm,x,y,w,h) -- handle clipping an area
1240 * bitmap is fixed width, 128 bytes (1024 pixels represented)
1241 * arranged most-sigificant-bit-left in 32-bit words
1242 * based on saa7146 clipping hardware, it swaps bytes if LE
1243 * much of this makes up for egcs brain damage -- so if you
1244 * are wondering "why did he do this?" it is because the C
1245 * was adjusted to generate the optimal asm output without
1246 * writing non-portable __asm__ directives.
1249 static void clip_draw_rectangle(u32 *clipmap, int x, int y, int w, int h)
1251 register int startword, endword;
1252 register u32 bitsleft, bitsright;
1262 if (w <= 0 || h <= 0 || x > 1023 || y > 639)
1263 return; /* throw away bad clips */
1268 startword = (x >> 5);
1269 endword = ((x + w) >> 5);
1270 bitsleft = (0xffffffff >> (x & 31));
1271 bitsright = (0xffffffff << (~((x + w) - (endword<<5))));
1272 temp = &clipmap[(y<<5) + startword];
1273 w = endword - startword;
1275 bitsleft |= bitsright;
1276 for (y = 0; y < h; y++) {
1281 for (y = 0; y < h; y++) {
1282 *temp++ |= bitsleft;
1283 for (x = 1; x < w; x++)
1284 *temp++ = 0xffffffff;
1291 static void make_clip_tab(struct saa7146 *saa, struct video_clip *cr, int ncr)
1293 int i, width, height;
1296 clipmap = saa->dmavid2;
1297 if((width=saa->win.width)>1023)
1298 width = 1023; /* sanity check */
1299 if((height=saa->win.height)>640)
1300 height = 639; /* sanity check */
1301 if (ncr > 0) { /* rectangles pased */
1302 /* convert rectangular clips to a bitmap */
1303 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1304 for (i = 0; i < ncr; i++)
1305 clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1306 cr[i].width, cr[i].height);
1308 /* clip against viewing window AND screen
1309 so we do not have to rely on the user program
1311 clip_draw_rectangle(clipmap,(saa->win.x+width>saa->win.swidth) ?
1312 (saa->win.swidth-saa->win.x) : width, 0, 1024, 768);
1313 clip_draw_rectangle(clipmap,0,(saa->win.y+height>saa->win.sheight) ?
1314 (saa->win.sheight-saa->win.y) : height,1024,768);
1316 clip_draw_rectangle(clipmap, 0, 0, -(saa->win.x), 768);
1318 clip_draw_rectangle(clipmap, 0, 0, 1024, -(saa->win.y));
1321 static int saa_ioctl(struct inode *inode, struct file *file,
1322 unsigned int cmd, unsigned long argl)
1324 struct saa7146 *saa = file->private_data;
1325 void __user *arg = (void __user *)argl;
1330 struct video_capability b;
1331 strcpy(b.name, saa->video_dev.name);
1332 b.type = VID_TYPE_CAPTURE |
1343 if (copy_to_user(arg, &b, sizeof(b)))
1349 struct video_picture p = saa->picture;
1350 if (saa->win.depth == 8)
1351 p.palette = VIDEO_PALETTE_HI240;
1352 if (saa->win.depth == 15)
1353 p.palette = VIDEO_PALETTE_RGB555;
1354 if (saa->win.depth == 16)
1355 p.palette = VIDEO_PALETTE_RGB565;
1356 if (saa->win.depth == 24)
1357 p.palette = VIDEO_PALETTE_RGB24;
1358 if (saa->win.depth == 32)
1359 p.palette = VIDEO_PALETTE_RGB32;
1360 if (copy_to_user(arg, &p, sizeof(p)))
1366 struct video_picture p;
1368 if (copy_from_user(&p, arg, sizeof(p)))
1370 if (p.palette < sizeof(palette2fmt) / sizeof(u32)) {
1371 format = palette2fmt[p.palette];
1372 saa->win.color_fmt = format;
1373 saawrite(format|0x60, SAA7146_CLIP_FORMAT_CTRL);
1375 saawrite(((p.brightness & 0xff00) << 16) |
1376 ((p.contrast & 0xfe00) << 7) |
1377 ((p.colour & 0xfe00) >> 9), SAA7146_BCS_CTRL);
1379 /* upload changed registers */
1380 saawrite(((SAA7146_MC2_UPLD_HPS_H |
1381 SAA7146_MC2_UPLD_HPS_V) << 16) |
1382 SAA7146_MC2_UPLD_HPS_H | SAA7146_MC2_UPLD_HPS_V,
1388 struct video_window vw;
1389 struct video_clip *vcp = NULL;
1391 if (copy_from_user(&vw, arg, sizeof(vw)))
1394 if (vw.flags || vw.width < 16 || vw.height < 16) { /* stop capture */
1395 saawrite((SAA7146_MC1_TR_E_1 << 16), SAA7146_MC1);
1398 if (saa->win.bpp < 4) { /* 32-bit align start and adjust width */
1400 vw.x = (vw.x + 3) & ~3;
1406 saa->win.width = vw.width;
1407 if (saa->win.width > 768)
1408 saa->win.width = 768;
1409 saa->win.height = vw.height;
1410 if (CurrentMode == VIDEO_MODE_NTSC) {
1411 if (saa->win.height > 480)
1412 saa->win.height = 480;
1414 if (saa->win.height > 576)
1415 saa->win.height = 576;
1419 saawrite((SAA7146_MC1_TR_E_1 << 16), SAA7146_MC1);
1420 saa7146_set_winsize(saa);
1425 if (vw.clipcount < 0) {
1426 if (copy_from_user(saa->dmavid2, vw.clips,
1427 VIDEO_CLIPMAP_SIZE))
1430 else if (vw.clipcount > 16384) {
1432 } else if (vw.clipcount > 0) {
1433 if ((vcp = vmalloc(sizeof(struct video_clip) *
1434 (vw.clipcount))) == NULL)
1436 if (copy_from_user(vcp, vw.clips,
1437 sizeof(struct video_clip) *
1442 } else /* nothing clipped */
1443 memset(saa->dmavid2, 0, VIDEO_CLIPMAP_SIZE);
1444 make_clip_tab(saa, vcp, vw.clipcount);
1445 if (vw.clipcount > 0)
1448 /* start capture & clip dma if we have an address */
1449 if ((saa->cap & 3) && saa->win.vidadr != 0)
1450 saawrite(((SAA7146_MC1_TR_E_1 |
1451 SAA7146_MC1_TR_E_2) << 16) | 0xffff,
1457 struct video_window vw;
1460 vw.width = saa->win.width;
1461 vw.height = saa->win.height;
1464 if (copy_to_user(arg, &vw, sizeof(vw)))
1471 if (copy_from_user(&v, arg, sizeof(v)))
1475 saawrite((SAA7146_MC1_TR_E_1 << 16),
1478 if (saa->win.vidadr == 0 || saa->win.width == 0
1479 || saa->win.height == 0)
1482 saawrite((SAA7146_MC1_TR_E_1 << 16) | 0xffff,
1489 struct video_buffer v;
1490 v.base = (void *) saa->win.vidadr;
1491 v.height = saa->win.sheight;
1492 v.width = saa->win.swidth;
1493 v.depth = saa->win.depth;
1494 v.bytesperline = saa->win.bpl;
1495 if (copy_to_user(arg, &v, sizeof(v)))
1502 struct video_buffer v;
1503 if (!capable(CAP_SYS_ADMIN))
1505 if (copy_from_user(&v, arg, sizeof(v)))
1507 if (v.depth != 8 && v.depth != 15 && v.depth != 16 &&
1508 v.depth != 24 && v.depth != 32 && v.width > 16 &&
1509 v.height > 16 && v.bytesperline > 16)
1512 saa->win.vidadr = (unsigned long) v.base;
1513 saa->win.sheight = v.height;
1514 saa->win.swidth = v.width;
1515 saa->win.bpp = ((v.depth + 7) & 0x38) / 8;
1516 saa->win.depth = v.depth;
1517 saa->win.bpl = v.bytesperline;
1519 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1520 v.base, v.width, v.height, saa->win.bpp, saa->win.bpl));
1521 saa7146_set_winsize(saa);
1526 /* Will be handled higher up .. */
1532 struct video_audio v;
1534 v.flags &= ~(VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE);
1535 v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
1536 strcpy(v.name, "MPEG");
1537 v.mode = VIDEO_SOUND_STEREO;
1538 if (copy_to_user(arg, &v, sizeof(v)))
1544 struct video_audio v;
1546 if (copy_from_user(&v, arg, sizeof(v)))
1548 i = (~(v.volume>>8))&0xff;
1550 if (v.flags & VIDEO_AUDIO_MUTE) {
1551 debiwrite(saa, debNormal,
1555 if (!(v.flags & VIDEO_AUDIO_MUTE))
1556 debiwrite(saa, debNormal,
1559 if (v.flags & VIDEO_AUDIO_VOLUME)
1560 debiwrite(saa, debNormal,
1564 if (v.flags & VIDEO_AUDIO_MUTE)
1565 cs4341_setlevel(saa, 0xff, 0xff);
1566 if (!(v.flags & VIDEO_AUDIO_MUTE))
1567 cs4341_setlevel(saa, 0, 0);
1568 if (v.flags & VIDEO_AUDIO_VOLUME)
1569 cs4341_setlevel(saa, i, i);
1577 struct video_unit vu;
1578 vu.video = saa->video_dev.minor;
1579 vu.vbi = VIDEO_NO_UNIT;
1580 vu.radio = VIDEO_NO_UNIT;
1581 vu.audio = VIDEO_NO_UNIT;
1582 vu.teletext = VIDEO_NO_UNIT;
1583 if (copy_to_user(arg, &vu, sizeof(vu)))
1587 case VIDIOCSPLAYMODE:
1589 struct video_play_mode pmode;
1590 if (copy_from_user((void *) &pmode, arg,
1591 sizeof(struct video_play_mode)))
1593 switch (pmode.mode) {
1594 case VID_PLAY_VID_OUT_MODE:
1595 if (pmode.p1 != VIDEO_MODE_NTSC &&
1596 pmode.p1 != VIDEO_MODE_PAL)
1598 set_out_format(saa, pmode.p1);
1600 case VID_PLAY_GENLOCK:
1601 debiwrite(saa, debNormal,
1603 (pmode.p1 ? 0x8000 : 0x8080),
1606 set_genlock_offset(saa,
1609 case VID_PLAY_NORMAL:
1610 debiwrite(saa, debNormal,
1611 IBM_MP2_CHIP_CONTROL,
1613 ibm_send_command(saa,
1614 IBM_MP2_PLAY, 0, 0);
1615 saa->playmode = pmode.mode;
1617 case VID_PLAY_PAUSE:
1618 /* IBM removed the PAUSE command */
1619 /* they say use SINGLE_FRAME now */
1620 case VID_PLAY_SINGLE_FRAME:
1621 ibm_send_command(saa,
1622 IBM_MP2_SINGLE_FRAME,
1624 if (saa->playmode == pmode.mode) {
1625 debiwrite(saa, debNormal,
1626 IBM_MP2_CHIP_CONTROL,
1629 saa->playmode = pmode.mode;
1631 case VID_PLAY_FAST_FORWARD:
1632 ibm_send_command(saa,
1633 IBM_MP2_FAST_FORWARD, 0, 0);
1634 saa->playmode = pmode.mode;
1636 case VID_PLAY_SLOW_MOTION:
1637 ibm_send_command(saa,
1638 IBM_MP2_SLOW_MOTION,
1640 saa->playmode = pmode.mode;
1642 case VID_PLAY_IMMEDIATE_NORMAL:
1643 /* ensure transfers resume */
1644 debiwrite(saa, debNormal,
1645 IBM_MP2_CHIP_CONTROL,
1647 ibm_send_command(saa,
1648 IBM_MP2_IMED_NORM_PLAY, 0, 0);
1649 saa->playmode = VID_PLAY_NORMAL;
1651 case VID_PLAY_SWITCH_CHANNELS:
1652 saa->audhead = saa->audtail = 0;
1653 saa->vidhead = saa->vidtail = 0;
1654 ibm_send_command(saa,
1655 IBM_MP2_FREEZE_FRAME, 0, 1);
1656 ibm_send_command(saa,
1657 IBM_MP2_RESET_AUD_RATE, 0, 1);
1658 debiwrite(saa, debNormal,
1659 IBM_MP2_CHIP_CONTROL, 0, 2);
1660 ibm_send_command(saa,
1661 IBM_MP2_CHANNEL_SWITCH, 0, 1);
1662 debiwrite(saa, debNormal,
1663 IBM_MP2_CHIP_CONTROL,
1665 ibm_send_command(saa,
1666 IBM_MP2_PLAY, 0, 0);
1667 saa->playmode = VID_PLAY_NORMAL;
1669 case VID_PLAY_FREEZE_FRAME:
1670 ibm_send_command(saa,
1671 IBM_MP2_FREEZE_FRAME, 0, 0);
1672 saa->playmode = pmode.mode;
1674 case VID_PLAY_STILL_MODE:
1675 ibm_send_command(saa,
1676 IBM_MP2_SET_STILL_MODE, 0, 0);
1677 saa->playmode = pmode.mode;
1679 case VID_PLAY_MASTER_MODE:
1680 if (pmode.p1 == VID_PLAY_MASTER_NONE)
1681 saa->boardcfg[1] = 0x13;
1682 else if (pmode.p1 ==
1683 VID_PLAY_MASTER_VIDEO)
1684 saa->boardcfg[1] = 0x23;
1685 else if (pmode.p1 ==
1686 VID_PLAY_MASTER_AUDIO)
1687 saa->boardcfg[1] = 0x43;
1690 debiwrite(saa, debNormal,
1691 IBM_MP2_CHIP_CONTROL,
1694 case VID_PLAY_ACTIVE_SCANLINES:
1695 if (CurrentMode == VIDEO_MODE_PAL) {
1699 saa->boardcfg[5] = pmode.p1;
1700 saa->boardcfg[55] = (pmode.p1 +
1707 saa->boardcfg[4] = pmode.p1;
1708 saa->boardcfg[54] = (pmode.p1 +
1712 set_out_format(saa, CurrentMode);
1713 case VID_PLAY_RESET:
1714 return do_ibm_reset(saa);
1715 case VID_PLAY_END_MARK:
1716 if (saa->endmarktail <
1718 if (saa->endmarkhead -
1719 saa->endmarktail < 2)
1721 } else if (saa->endmarkhead <=
1723 if (saa->endmarktail -
1729 saa->endmark[saa->endmarktail] =
1732 if (saa->endmarktail >= MAX_MARKS)
1733 saa->endmarktail = 0;
1737 case VIDIOCSWRITEMODE:
1740 if (copy_from_user((void *) &mode, arg, sizeof(int)))
1742 if (mode == VID_WRITE_MPEG_AUD ||
1743 mode == VID_WRITE_MPEG_VID ||
1744 mode == VID_WRITE_CC ||
1745 mode == VID_WRITE_TTX ||
1746 mode == VID_WRITE_OSD) {
1747 saa->writemode = mode;
1752 case VIDIOCSMICROCODE:
1754 struct video_code ucode;
1757 if (copy_from_user(&ucode, arg, sizeof(ucode)))
1759 if (ucode.datasize > 65536 || ucode.datasize < 1024 ||
1760 strncmp(ucode.loadwhat, "dec", 3))
1762 if ((udata = vmalloc(ucode.datasize)) == NULL)
1764 if (copy_from_user(udata, ucode.data, ucode.datasize)) {
1769 if (!strncmp(ucode.loadwhat, "decoder.aud", 11)
1770 || !strncmp(ucode.loadwhat, "decoder.vid", 11))
1771 i = initialize_ibmmpeg2(&ucode);
1773 i = initialize_fpga(&ucode);
1780 case VIDIOCGCHAN: /* this makes xawtv happy */
1782 struct video_channel v;
1783 if (copy_from_user(&v, arg, sizeof(v)))
1785 v.flags = VIDEO_VC_AUDIO;
1787 v.type = VID_TYPE_MPEG_DECODER;
1788 v.norm = CurrentMode;
1789 strcpy(v.name, "MPEG2");
1790 if (copy_to_user(arg, &v, sizeof(v)))
1794 case VIDIOCSCHAN: /* this makes xawtv happy */
1796 struct video_channel v;
1797 if (copy_from_user(&v, arg, sizeof(v)))
1803 return -ENOIOCTLCMD;
1808 static int saa_mmap(struct file *file, struct vm_area_struct *vma)
1810 struct saa7146 *saa = file->private_data;
1811 printk(KERN_DEBUG "stradis%d: saa_mmap called\n", saa->nr);
1815 static ssize_t saa_read(struct file *file, char __user *buf,
1816 size_t count, loff_t *ppos)
1821 static ssize_t saa_write(struct file *file, const char __user *buf,
1822 size_t count, loff_t *ppos)
1824 struct saa7146 *saa = file->private_data;
1825 unsigned long todo = count;
1826 int blocksize, split;
1827 unsigned long flags;
1830 if (saa->writemode == VID_WRITE_MPEG_AUD) {
1831 spin_lock_irqsave(&saa->lock, flags);
1832 if (saa->audhead <= saa->audtail)
1833 blocksize = 65536-(saa->audtail - saa->audhead);
1835 blocksize = saa->audhead - saa->audtail;
1836 spin_unlock_irqrestore(&saa->lock, flags);
1837 if (blocksize < 16384) {
1838 saawrite(SAA7146_PSR_DEBI_S |
1839 SAA7146_PSR_PIN1, SAA7146_IER);
1840 saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1841 /* wait for buffer space to open */
1842 interruptible_sleep_on(&saa->audq);
1844 spin_lock_irqsave(&saa->lock, flags);
1845 if (saa->audhead <= saa->audtail) {
1846 blocksize = 65536-(saa->audtail - saa->audhead);
1847 split = 65536 - saa->audtail;
1849 blocksize = saa->audhead - saa->audtail;
1852 spin_unlock_irqrestore(&saa->lock, flags);
1854 if (blocksize > todo)
1856 /* double check that we really have space */
1859 if (split < blocksize) {
1860 if (copy_from_user(saa->audbuf +
1861 saa->audtail, buf, split))
1868 if (copy_from_user(saa->audbuf + saa->audtail, buf,
1871 saa->audtail += blocksize;
1874 saa->audtail &= 0xffff;
1875 } else if (saa->writemode == VID_WRITE_MPEG_VID) {
1876 spin_lock_irqsave(&saa->lock, flags);
1877 if (saa->vidhead <= saa->vidtail)
1878 blocksize=524288-(saa->vidtail - saa->vidhead);
1880 blocksize = saa->vidhead - saa->vidtail;
1881 spin_unlock_irqrestore(&saa->lock, flags);
1882 if (blocksize < 65536) {
1883 saawrite(SAA7146_PSR_DEBI_S |
1884 SAA7146_PSR_PIN1, SAA7146_IER);
1885 saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1886 /* wait for buffer space to open */
1887 interruptible_sleep_on(&saa->vidq);
1889 spin_lock_irqsave(&saa->lock, flags);
1890 if (saa->vidhead <= saa->vidtail) {
1891 blocksize=524288-(saa->vidtail - saa->vidhead);
1892 split = 524288 - saa->vidtail;
1894 blocksize = saa->vidhead - saa->vidtail;
1897 spin_unlock_irqrestore(&saa->lock, flags);
1899 if (blocksize > todo)
1901 /* double check that we really have space */
1904 if (split < blocksize) {
1905 if (copy_from_user(saa->vidbuf +
1906 saa->vidtail, buf, split))
1913 if (copy_from_user(saa->vidbuf + saa->vidtail, buf,
1916 saa->vidtail += blocksize;
1919 saa->vidtail &= 0x7ffff;
1920 } else if (saa->writemode == VID_WRITE_OSD) {
1923 if (copy_from_user(saa->osdbuf, buf, count))
1927 saa->osdtail = count;
1928 debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR, 0, 2);
1929 debiwrite(saa, debNormal, IBM_MP2_OSD_LINK_ADDR, 0, 2);
1930 debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00d, 2);
1931 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
1932 debiread(saa, debNormal,
1933 IBM_MP2_DISP_MODE, 2) | 1, 2);
1934 /* trigger osd data transfer */
1935 saawrite(SAA7146_PSR_DEBI_S |
1936 SAA7146_PSR_PIN1, SAA7146_IER);
1937 saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1943 static int saa_open(struct inode *inode, struct file *file)
1945 struct saa7146 *saa = NULL;
1946 unsigned int minor = iminor(inode);
1949 for (i = 0; i < SAA7146_MAX; i++) {
1950 if (saa7146s[i].video_dev.minor == minor) {
1957 file->private_data = saa;
1959 //saa->video_dev.busy = 0; /* old hack to support multiple open */
1962 return 0; /* device open already, don't reset */
1963 saa->writemode = VID_WRITE_MPEG_VID; /* default to video */
1967 static int saa_release(struct inode *inode, struct file *file)
1969 struct saa7146 *saa = file->private_data;
1971 //saa->video_dev.busy = 0; /* old hack to support multiple open */
1972 if (saa->user > 0) /* still someone using device */
1974 saawrite(0x007f0000, SAA7146_MC1); /* stop all overlay dma */
1978 static struct file_operations saa_fops =
1980 .owner = THIS_MODULE,
1982 .release = saa_release,
1985 .llseek = no_llseek,
1990 /* template for video_device-structure */
1991 static struct video_device saa_template =
1994 .type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY,
1995 .hardware = VID_HARDWARE_SAA7146,
2000 static int configure_saa7146(struct pci_dev *dev, int num)
2003 struct saa7146 *saa;
2005 saa = &saa7146s[num];
2007 saa->endmarkhead = saa->endmarktail = 0;
2008 saa->win.x = saa->win.y = 0;
2009 saa->win.width = saa->win.cropwidth = 720;
2010 saa->win.height = saa->win.cropheight = 480;
2011 saa->win.cropx = saa->win.cropy = 0;
2013 saa->win.depth = 16;
2014 saa->win.color_fmt = palette2fmt[VIDEO_PALETTE_RGB565];
2015 saa->win.bpl = 1024 * saa->win.bpp;
2016 saa->win.swidth = 1024;
2017 saa->win.sheight = 768;
2018 saa->picture.brightness = 32768;
2019 saa->picture.contrast = 38768;
2020 saa->picture.colour = 32768;
2024 saa->playmode = VID_PLAY_NORMAL;
2025 memset(saa->boardcfg, 0, 64); /* clear board config area */
2026 saa->saa7146_mem = NULL;
2027 saa->dmavid1 = saa->dmavid2 = saa->dmavid3 = saa->dmaa1in =
2028 saa->dmaa1out = saa->dmaa2in = saa->dmaa2out =
2029 saa->pagevid1 = saa->pagevid2 = saa->pagevid3 = saa->pagea1in =
2030 saa->pagea1out = saa->pagea2in = saa->pagea2out =
2031 saa->pagedebi = saa->dmaRPS1 = saa->dmaRPS2 = saa->pageRPS1 =
2032 saa->pageRPS2 = NULL;
2033 saa->audbuf = saa->vidbuf = saa->osdbuf = saa->dmadebi = NULL;
2034 saa->audhead = saa->vidtail = 0;
2036 init_waitqueue_head(&saa->i2cq);
2037 init_waitqueue_head(&saa->audq);
2038 init_waitqueue_head(&saa->debiq);
2039 init_waitqueue_head(&saa->vidq);
2040 spin_lock_init(&saa->lock);
2042 if (pci_enable_device(dev))
2045 saa->id = dev->device;
2046 saa->irq = dev->irq;
2047 saa->video_dev.minor = -1;
2048 saa->saa7146_adr = pci_resource_start(dev, 0);
2049 pci_read_config_byte(dev, PCI_CLASS_REVISION, &saa->revision);
2051 saa->saa7146_mem = ioremap(saa->saa7146_adr, 0x200);
2052 if (!saa->saa7146_mem)
2055 memcpy(&saa->video_dev, &saa_template, sizeof(saa_template));
2056 saawrite(0, SAA7146_IER); /* turn off all interrupts */
2057 result = request_irq(saa->irq, saa7146_irq,
2058 SA_SHIRQ | SA_INTERRUPT, "stradis", (void *) saa);
2059 if (result == -EINVAL)
2060 printk(KERN_ERR "stradis%d: Bad irq number or handler\n",
2062 if (result == -EBUSY)
2063 printk(KERN_ERR "stradis%d: IRQ %ld busy, change your PnP"
2064 " config in BIOS\n", num, saa->irq);
2066 iounmap(saa->saa7146_mem);
2069 pci_set_master(dev);
2070 if (video_register_device(&saa->video_dev, VFL_TYPE_GRABBER, video_nr) < 0) {
2071 iounmap(saa->saa7146_mem);
2077 static int init_saa7146(int i)
2079 struct saa7146 *saa = &saa7146s[i];
2082 /* reset the saa7146 */
2083 saawrite(0xffff0000, SAA7146_MC1);
2085 /* enable debi and i2c transfers and pins */
2086 saawrite(((SAA7146_MC1_EDP | SAA7146_MC1_EI2C |
2087 SAA7146_MC1_TR_E_DEBI) << 16) | 0xffff, SAA7146_MC1);
2088 /* ensure proper state of chip */
2089 saawrite(0x00000000, SAA7146_PAGE1);
2090 saawrite(0x00f302c0, SAA7146_NUM_LINE_BYTE1);
2091 saawrite(0x00000000, SAA7146_PAGE2);
2092 saawrite(0x01400080, SAA7146_NUM_LINE_BYTE2);
2093 saawrite(0x00000000, SAA7146_DD1_INIT);
2094 saawrite(0x00000000, SAA7146_DD1_STREAM_B);
2095 saawrite(0x00000000, SAA7146_DD1_STREAM_A);
2096 saawrite(0x00000000, SAA7146_BRS_CTRL);
2097 saawrite(0x80400040, SAA7146_BCS_CTRL);
2098 saawrite(0x0000e000 /*| (1<<29)*/, SAA7146_HPS_CTRL);
2099 saawrite(0x00000060, SAA7146_CLIP_FORMAT_CTRL);
2100 saawrite(0x00000000, SAA7146_ACON1);
2101 saawrite(0x00000000, SAA7146_ACON2);
2102 saawrite(0x00000600, SAA7146_I2C_STATUS);
2103 saawrite(((SAA7146_MC2_UPLD_D1_B | SAA7146_MC2_UPLD_D1_A |
2104 SAA7146_MC2_UPLD_BRS | SAA7146_MC2_UPLD_HPS_H |
2105 SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_DMA2 |
2106 SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_I2C) << 16) | 0xffff,
2108 /* setup arbitration control registers */
2109 saawrite(0x1412121a, SAA7146_PCI_BT_V1);
2111 /* allocate 32k dma buffer + 4k for page table */
2112 if ((saa->dmadebi = kmalloc(32768 + 4096, GFP_KERNEL)) == NULL) {
2113 printk(KERN_ERR "stradis%d: debi kmalloc failed\n", i);
2117 saa->pagedebi = saa->dmadebi + 32768; /* top 4k is for mmu */
2118 saawrite(virt_to_bus(saa->pagedebi) /*|0x800 */ , SAA7146_DEBI_PAGE);
2119 for (i = 0; i < 12; i++) /* setup mmu page table */
2120 saa->pagedebi[i] = virt_to_bus((saa->dmadebi + i * 4096));
2122 saa->audhead = saa->vidhead = saa->osdhead = 0;
2123 saa->audtail = saa->vidtail = saa->osdtail = 0;
2124 if (saa->vidbuf == NULL)
2125 if ((saa->vidbuf = vmalloc(524288)) == NULL) {
2126 printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2129 if (saa->audbuf == NULL)
2130 if ((saa->audbuf = vmalloc(65536)) == NULL) {
2131 printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2136 if (saa->osdbuf == NULL)
2137 if ((saa->osdbuf = vmalloc(131072)) == NULL) {
2138 printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2141 saa->vidbuf = saa->audbuf = NULL;
2144 /* allocate 81920 byte buffer for clipping */
2145 if ((saa->dmavid2 = kmalloc(VIDEO_CLIPMAP_SIZE, GFP_KERNEL)) == NULL) {
2146 printk(KERN_ERR "stradis%d: clip kmalloc failed\n", saa->nr);
2150 saa->vidbuf = saa->audbuf = saa->osdbuf = NULL;
2151 saa->dmavid2 = NULL;
2154 memset(saa->dmavid2, 0x00, VIDEO_CLIPMAP_SIZE); /* clip everything */
2155 /* setup clipping registers */
2156 saawrite(virt_to_bus(saa->dmavid2), SAA7146_BASE_EVEN2);
2157 saawrite(virt_to_bus(saa->dmavid2) + 128, SAA7146_BASE_ODD2);
2158 saawrite(virt_to_bus(saa->dmavid2) + VIDEO_CLIPMAP_SIZE,
2159 SAA7146_PROT_ADDR2);
2160 saawrite(256, SAA7146_PITCH2);
2161 saawrite(4, SAA7146_PAGE2); /* dma direction: read, no byteswap */
2162 saawrite(((SAA7146_MC2_UPLD_DMA2) << 16) | SAA7146_MC2_UPLD_DMA2,
2168 static void release_saa(void)
2172 struct saa7146 *saa;
2174 for (i = 0; i < saa_num; i++) {
2177 /* turn off all capturing, DMA and IRQs */
2178 saawrite(0xffff0000, SAA7146_MC1); /* reset chip */
2179 saawrite(0, SAA7146_MC2);
2180 saawrite(0, SAA7146_IER);
2181 saawrite(0xffffffffUL, SAA7146_ISR);
2183 /* disable PCI bus-mastering */
2184 pci_read_config_byte(saa->dev, PCI_COMMAND, &command);
2185 command &= ~PCI_COMMAND_MASTER;
2186 pci_write_config_byte(saa->dev, PCI_COMMAND, command);
2188 /* unmap and free memory */
2189 saa->audhead = saa->audtail = saa->osdhead = 0;
2190 saa->vidhead = saa->vidtail = saa->osdtail = 0;
2198 kfree((void *) saa->dmavid2);
2199 saa->audbuf = saa->vidbuf = saa->osdbuf = NULL;
2200 saa->dmavid2 = NULL;
2202 kfree((void *) saa->dmadebi);
2204 kfree((void *) saa->dmavid1);
2206 kfree((void *) saa->dmavid2);
2208 kfree((void *) saa->dmavid3);
2210 kfree((void *) saa->dmaa1in);
2212 kfree((void *) saa->dmaa1out);
2214 kfree((void *) saa->dmaa2in);
2216 kfree((void *) saa->dmaa2out);
2218 kfree((void *) saa->dmaRPS1);
2220 kfree((void *) saa->dmaRPS2);
2221 free_irq(saa->irq, saa);
2222 if (saa->saa7146_mem)
2223 iounmap(saa->saa7146_mem);
2224 if (saa->video_dev.minor != -1)
2225 video_unregister_device(&saa->video_dev);
2230 static int __init stradis_init (void)
2232 struct pci_dev *dev = NULL;
2237 while ((dev = pci_find_device(PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, dev))) {
2238 if (!dev->subsystem_vendor)
2239 printk(KERN_INFO "stradis%d: rev1 decoder\n", saa_num);
2241 printk(KERN_INFO "stradis%d: SDM2xx found\n", saa_num);
2242 result = configure_saa7146(dev, saa_num++);
2247 printk(KERN_INFO "stradis: %d card(s) found.\n", saa_num);
2250 for (i = 0; i < saa_num; i++)
2251 if (init_saa7146(i) < 0) {
2259 static void __exit stradis_exit (void)
2262 printk(KERN_INFO "stradis: module cleanup complete\n");
2266 module_init(stradis_init);
2267 module_exit(stradis_exit);