2 * 1. Redistributions of source code must retain the
3 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Amancio Hasty and
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * This is part of the Driver for Video Capture Cards (Frame grabbers)
39 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
41 * Copyright Roger Hardiman and Amancio Hasty.
43 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
54 #if (__FreeBSD_version < 500000)
55 #include <machine/clock.h> /* for DELAY */
56 #include <pci/pcivar.h>
59 #include <sys/mutex.h>
60 #include <sys/selinfo.h>
61 #include <dev/pci/pcivar.h>
64 #include <machine/bus.h>
69 #include <dev/ic/bt8xx.h> /* NetBSD .h file location */
70 #include <dev/pci/bktr/bktr_reg.h>
71 #include <dev/pci/bktr/bktr_tuner.h>
72 #include <dev/pci/bktr/bktr_card.h>
73 #include <dev/pci/bktr/bktr_core.h>
75 #include <dev/bktr/ioctl_meteor.h>
76 #include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
77 #include <dev/bktr/bktr_reg.h>
78 #include <dev/bktr/bktr_tuner.h>
79 #include <dev/bktr/bktr_card.h>
80 #include <dev/bktr/bktr_core.h>
85 #if defined( TUNER_AFC )
86 #define AFC_DELAY 10000 /* 10 millisend delay */
88 #define AFC_FREQ_MINUS_125 0x00
89 #define AFC_FREQ_MINUS_62 0x01
90 #define AFC_FREQ_CENTERED 0x02
91 #define AFC_FREQ_PLUS_62 0x03
92 #define AFC_FREQ_PLUS_125 0x04
93 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
94 #endif /* TUNER_AFC */
99 #define TTYPE_NTSC_J 2
101 #define TTYPE_PAL_M 4
102 #define TTYPE_PAL_N 5
103 #define TTYPE_SECAM 6
105 #define TSA552x_CB_MSB (0x80)
106 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
107 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
108 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
109 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
110 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
111 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
112 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
114 #define TSA552x_RADIO (TSA552x_CB_MSB | \
117 /* raise the charge pump voltage for fast tuning */
118 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
124 /* lower the charge pump voltage for better residual oscillator FM */
125 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
130 /* The control value for the ALPS TSCH5 Tuner */
131 #define TSCH5_FCONTROL 0x82
132 #define TSCH5_RADIO 0x86
134 /* The control value for the ALPS TSBH1 Tuner */
135 #define TSBH1_FCONTROL 0xce
138 static void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
141 static const struct TUNER tuners[] = {
142 /* XXX FIXME: fill in the band-switch crosspoints */
144 { "<no>", /* the 'name' */
145 TTYPE_XXX, /* input type */
146 { 0x00, /* control byte for Tuner PLL */
150 { 0x00, 0x00 }, /* band-switch crosspoints */
151 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
154 { "Temic NTSC", /* the 'name' */
155 TTYPE_NTSC, /* input type */
156 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
160 { 0x00, 0x00}, /* band-switch crosspoints */
161 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
164 { "Temic PAL", /* the 'name' */
165 TTYPE_PAL, /* input type */
166 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
170 { 0x00, 0x00 }, /* band-switch crosspoints */
171 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
174 { "Temic SECAM", /* the 'name' */
175 TTYPE_SECAM, /* input type */
176 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
180 { 0x00, 0x00 }, /* band-switch crosspoints */
181 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
184 { "Philips NTSC", /* the 'name' */
185 TTYPE_NTSC, /* input type */
186 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
190 { 0x00, 0x00 }, /* band-switch crosspoints */
191 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
194 { "Philips PAL", /* the 'name' */
195 TTYPE_PAL, /* input type */
196 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
200 { 0x00, 0x00 }, /* band-switch crosspoints */
201 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
204 { "Philips SECAM", /* the 'name' */
205 TTYPE_SECAM, /* input type */
206 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
210 { 0x00, 0x00 }, /* band-switch crosspoints */
211 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
214 { "Temic PAL I", /* the 'name' */
215 TTYPE_PAL, /* input type */
216 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
220 { 0x00, 0x00 }, /* band-switch crosspoints */
221 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
224 { "Philips PAL I", /* the 'name' */
225 TTYPE_PAL, /* input type */
226 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
230 { 0x00, 0x00 }, /* band-switch crosspoints */
231 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
233 /* PHILIPS_FR1236_NTSC */
234 { "Philips FR1236 NTSC FM", /* the 'name' */
235 TTYPE_NTSC, /* input type */
236 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
240 { 0x00, 0x00 }, /* band-switch crosspoints */
241 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
243 /* PHILIPS_FR1216_PAL */
244 { "Philips FR1216 PAL FM" , /* the 'name' */
245 TTYPE_PAL, /* input type */
246 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
250 { 0x00, 0x00 }, /* band-switch crosspoints */
251 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
253 /* PHILIPS_FR1236_SECAM */
254 { "Philips FR1236 SECAM FM", /* the 'name' */
255 TTYPE_SECAM, /* input type */
256 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
260 { 0x00, 0x00 }, /* band-switch crosspoints */
261 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
263 /* ALPS TSCH5 NTSC */
264 { "ALPS TSCH5 NTSC FM", /* the 'name' */
265 TTYPE_NTSC, /* input type */
266 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
270 { 0x00, 0x00 }, /* band-switch crosspoints */
271 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
273 /* ALPS TSBH1 NTSC */
274 { "ALPS TSBH1 NTSC", /* the 'name' */
275 TTYPE_NTSC, /* input type */
276 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
280 { 0x00, 0x00 }, /* band-switch crosspoints */
281 { 0x01, 0x02, 0x08, 0x00 } }, /* the band-switch values */
283 /* MT2032 Microtune */
284 { "MT2032", /* the 'name' */
285 TTYPE_PAL, /* input type */
286 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
290 { 0x00, 0x00 }, /* band-switch crosspoints */
291 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
293 /* LG TPI8PSB12P PAL */
294 { "LG TPI8PSB12P PAL", /* the 'name' */
295 TTYPE_PAL, /* input type */
296 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
300 { 0x00, 0x00 }, /* band-switch crosspoints */
301 { 0xa0, 0x90, 0x30, 0x8e } }, /* the band-switch values */
305 /* scaling factor for frequencies expressed as ints */
306 #define FREQFACTOR 16
310 * entry 0: MAX legal channel
311 * entry 1: IF frequency
312 * expressed as fi{mHz} * 16,
313 * eg 45.75mHz == 45.75 * 16 = 732
314 * entry 2: [place holder/future]
315 * entry 3: base of channel record 0
316 * entry 3 + (x*3): base of channel record 'x'
317 * entry LAST: NULL channel entry marking end of records
320 * int 0: base channel
321 * int 1: frequency of base channel,
322 * expressed as fb{mHz} * 16,
323 * int 2: offset frequency between channels,
324 * expressed as fo{mHz} * 16,
328 * North American Broadcast Channels:
330 * 2: 55.25 mHz - 4: 67.25 mHz
331 * 5: 77.25 mHz - 6: 83.25 mHz
332 * 7: 175.25 mHz - 13: 211.25 mHz
333 * 14: 471.25 mHz - 83: 885.25 mHz
338 static int nabcst[] = {
339 83, (int)( 45.75 * FREQFACTOR), 0,
340 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
341 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
342 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
343 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
349 * North American Cable Channels, IRC:
351 * 2: 55.25 mHz - 4: 67.25 mHz
352 * 5: 77.25 mHz - 6: 83.25 mHz
353 * 7: 175.25 mHz - 13: 211.25 mHz
354 * 14: 121.25 mHz - 22: 169.25 mHz
355 * 23: 217.25 mHz - 94: 643.25 mHz
356 * 95: 91.25 mHz - 99: 115.25 mHz
361 static int irccable[] = {
362 116, (int)( 45.75 * FREQFACTOR), 0,
363 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
364 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
365 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
366 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
367 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
368 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
369 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
375 * North American Cable Channels, HRC:
377 * 2: 54 mHz - 4: 66 mHz
378 * 5: 78 mHz - 6: 84 mHz
379 * 7: 174 mHz - 13: 210 mHz
380 * 14: 120 mHz - 22: 168 mHz
381 * 23: 216 mHz - 94: 642 mHz
382 * 95: 90 mHz - 99: 114 mHz
387 static int hrccable[] = {
388 116, (int)( 45.75 * FREQFACTOR), 0,
389 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
390 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
391 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
392 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
393 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
394 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
395 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
401 * Western European broadcast channels:
403 * (there are others that appear to vary between countries - rmt)
405 * here's the table Philips provides:
406 * caution, some of the offsets don't compute...
515 * Channels S21 - S41 are taken from
516 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
540 * 121 3890 000 IFFREQ
543 static int weurope[] = {
544 121, (int)( 38.90 * FREQFACTOR), 0,
545 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
546 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
547 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
548 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
549 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
550 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
551 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
552 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
553 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
554 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
555 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
560 * Japanese Broadcast Channels:
562 * 1: 91.25MHz - 3: 103.25MHz
563 * 4: 171.25MHz - 7: 189.25MHz
564 * 8: 193.25MHz - 12: 217.25MHz (VHF)
565 * 13: 471.25MHz - 62: 765.25MHz (UHF)
572 #define IF_FREQ 45.75
573 static int jpnbcst[] = {
574 62, (int)(IF_FREQ * FREQFACTOR), 0,
575 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
576 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
577 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
578 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
585 * Japanese Cable Channels:
587 * 1: 91.25MHz - 3: 103.25MHz
588 * 4: 171.25MHz - 7: 189.25MHz
589 * 8: 193.25MHz - 12: 217.25MHz
590 * 13: 109.25MHz - 21: 157.25MHz
592 * 23: 223.25MHz - 63: 463.25MHz
597 #define IF_FREQ 45.75
598 static int jpncable[] = {
599 63, (int)(IF_FREQ * FREQFACTOR), 0,
600 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
601 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
602 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
603 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
604 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
605 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
612 * xUSSR Broadcast Channels:
614 * 1: 49.75MHz - 2: 59.25MHz
615 * 3: 77.25MHz - 5: 93.25MHz
616 * 6: 175.25MHz - 12: 223.25MHz
618 * 21: 471.25MHz - 34: 575.25MHz
619 * 35: 583.25MHz - 69: 855.25MHz
623 * 70: 111.25MHz - 77: 167.25MHz
624 * 78: 231.25MHz -107: 463.25MHz
628 #define IF_FREQ 38.90
629 static int xussr[] = {
630 107, (int)(IF_FREQ * FREQFACTOR), 0,
631 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
632 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
633 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
634 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
635 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
636 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
637 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
643 * Australian broadcast channels
646 #define IF_FREQ 38.90
647 static int australia[] = {
648 83, (int)(IF_FREQ * FREQFACTOR), 0,
649 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
650 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
651 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
652 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
653 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
654 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
661 * France broadcast channels
664 #define IF_FREQ 38.90
665 static int france[] = {
666 69, (int)(IF_FREQ * FREQFACTOR), 0,
667 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
668 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
669 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
670 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
671 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
679 char name[BT848_MAX_CHNLSET_NAME_LEN];
683 {irccable, "cableirc"},
684 {hrccable, "cablehrc"},
685 {weurope, "weurope"},
686 {jpnbcst, "jpnbcst"},
687 {jpncable, "jpncable"},
689 {australia, "australia"},
694 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
695 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
696 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
698 frequency_lookup( bktr_ptr_t bktr, int channel )
702 /* check for "> MAX channel" */
704 if ( channel > TBL_CHNL )
707 /* search the table for data */
708 for ( x = 3; TBL_CHNL; x += 3 ) {
709 if ( channel >= TBL_CHNL ) {
710 return( TBL_BASE_FREQ +
711 ((channel - TBL_CHNL) * TBL_OFFSET) );
715 /* not found, must be below the MIN channel */
723 #define TBL_IF (bktr->format_params == BT848_IFORM_F_NTSCJ || \
724 bktr->format_params == BT848_IFORM_F_NTSCM ? \
725 nabcst[1] : weurope[1])
728 /* Initialise the tuner structures in the bktr_softc */
729 /* This is needed as the tuner details are no longer globally declared */
731 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
732 if (tuner_type < Bt848_MAX_TUNER) {
733 bktr->card.tuner = &tuners[ tuner_type ];
735 bktr->card.tuner = NULL;
741 * Programming the tuner properly is quite complicated.
742 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
743 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
744 * 87.5 Mhz to 108.0 Mhz.
746 * RF and IF. RF = radio frequencies, it is the transmitted signal.
747 * IF is the Intermediate Frequency (the offset from the base
748 * signal where the video, color, audio and NICAM signals are.
750 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
751 * NICAM at 32.348 Mhz.
752 * Strangely enough, there is an IF (intermediate frequency) for
753 * FM Radio which is 10.7 Mhz.
755 * The tuner also works in Bands. Philips bands are
756 * FM radio band 87.50 to 108.00 MHz
757 * Low band 45.75 to 170.00 MHz
758 * Mid band 170.00 to 450.00 MHz
759 * High band 450.00 to 855.25 MHz
762 * Now we need to set the PLL on the tuner to the required freuqncy.
763 * It has a programmable divisor.
765 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
768 * For RADIO we want a different equation.
769 * freq IF is 10.70 MHz (so the data sheet tells me)
770 * N = (freq RF + freq IF) / step size
771 * The step size must be set to 50 khz (so the data sheet tells me)
772 * (note this is 50 kHz, the other things are in MHz)
773 * so we end up with N = 20x(freq RF + 10.7)
780 #define FM_RADIO_BAND 3
783 /* Check if these are correct for other than Philips PAL */
784 #define STATUSBIT_COLD 0x80
785 #define STATUSBIT_LOCK 0x40
786 #define STATUSBIT_TV 0x20
787 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
788 #define STATUSBIT_ADC 0x07
791 * set the frequency of the tuner
792 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
793 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
794 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
798 tv_freq( bktr_ptr_t bktr, int frequency, int type )
800 const struct TUNER* tuner;
806 #if defined( TEST_TUNER_AFC )
807 int oldFrequency, afcDelta;
810 tuner = bktr->card.tuner;
814 if (tuner == &tuners[TUNER_MT2032]) {
815 mt2032_set_tv_freq(bktr, frequency);
818 if (type == TV_FREQUENCY) {
820 * select the band based on frequency
821 * XXX FIXME: get the cross-over points from the tuner struct
823 if ( frequency < (160 * FREQFACTOR ) )
824 band_select = LOW_BAND;
825 else if ( frequency < (454 * FREQFACTOR ) )
826 band_select = MID_BAND;
828 band_select = HIGH_BAND;
830 #if defined( TEST_TUNER_AFC )
831 if ( bktr->tuner.afc )
835 * N = 16 * { fRF(pc) + fIF(pc) }
836 * or N = 16* fRF(pc) + 16*fIF(pc) }
838 * pc is picture carrier, fRF & fIF are in MHz
840 * fortunatly, frequency is passed in as MHz * 16
841 * and the TBL_IF frequency is also stored in MHz * 16
843 N = frequency + TBL_IF;
845 /* set the address of the PLL */
846 addr = bktr->card.tuner_pllAddr;
847 control = tuner->pllControl[ band_select ];
848 band = tuner->bandAddrs[ band_select ];
850 if(!(band && control)) /* Don't try to set un- */
851 return(-1); /* supported modes. */
853 if ( frequency > bktr->tuner.frequency ) {
854 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
855 i2cWrite( bktr, addr, control, band );
858 i2cWrite( bktr, addr, control, band );
859 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
862 #if defined( TUNER_AFC )
863 if ( bktr->tuner.afc == TRUE ) {
864 #if defined( TEST_TUNER_AFC )
865 oldFrequency = frequency;
867 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
868 /* AFC failed, restore requested frequency */
869 N = frequency + TBL_IF;
870 #if defined( TEST_TUNER_AFC )
871 printf("%s: do_afc: failed to lock\n",
874 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
877 frequency = N - TBL_IF;
878 #if defined( TEST_TUNER_AFC )
879 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
880 afcDelta = frequency - oldFrequency;
881 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
884 #endif /* TUNER_AFC */
886 bktr->tuner.frequency = frequency;
889 if ( type == FM_RADIO_FREQUENCY ) {
890 band_select = FM_RADIO_BAND;
893 * N = { fRF(pc) + fIF(pc) }/step_size
894 * The step size is 50kHz for FM radio.
895 * (eg after 102.35MHz comes 102.40 MHz)
896 * fIF is 10.7 MHz (as detailed in the specs)
898 * frequency is passed in as MHz * 100
900 * So, we have N = (frequency/100 + 10.70) /(50/1000)
902 N = (frequency + 1070)/5;
904 /* set the address of the PLL */
905 addr = bktr->card.tuner_pllAddr;
906 control = tuner->pllControl[ band_select ];
907 band = tuner->bandAddrs[ band_select ];
909 if(!(band && control)) /* Don't try to set un- */
910 return(-1); /* supported modes. */
912 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
913 * the ioctls RADIO_SETMODE
914 * and RADIO_GETMODE */
916 i2cWrite( bktr, addr, control, band );
917 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
919 bktr->tuner.frequency = (N * 5) - 1070;
930 #if defined( TUNER_AFC )
935 do_afc( bktr_ptr_t bktr, int addr, int frequency )
941 origFrequency = frequency;
943 /* wait for first setting to take effect */
944 tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
946 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
949 #if defined( TEST_TUNER_AFC )
950 printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
952 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
953 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
955 if ( !(status & 0x40) ) {
956 #if defined( TEST_TUNER_AFC )
957 printf( "%s: no lock!\n", bktr_name(bktr) );
962 switch( status & AFC_BITS ) {
963 case AFC_FREQ_CENTERED:
964 #if defined( TEST_TUNER_AFC )
965 printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
969 case AFC_FREQ_MINUS_125:
970 case AFC_FREQ_MINUS_62:
971 #if defined( TEST_TUNER_AFC )
972 printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
977 case AFC_FREQ_PLUS_62:
978 case AFC_FREQ_PLUS_125:
979 #if defined( TEST_TUNER_AFC )
980 printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
986 i2cWrite( bktr, addr,
987 (frequency>>8) & 0x7f, frequency & 0xff );
992 i2cWrite( bktr, addr,
993 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
997 #endif /* TUNER_AFC */
1002 * Get the Tuner status and signal strength
1004 int get_tuner_status( bktr_ptr_t bktr ) {
1005 if (bktr->card.tuner == &tuners[TUNER_MT2032])
1007 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
1011 * set the channel of the tuner
1014 tv_channel( bktr_ptr_t bktr, int channel )
1018 /* calculate the frequency according to tuner type */
1019 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
1022 /* set the new frequency */
1023 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
1026 /* OK to update records */
1027 return( (bktr->tuner.channel = channel) );
1031 * get channelset name
1034 tuner_getchnlset(struct bktr_chnlset *chnlset)
1036 if (( chnlset->index < CHNLSET_MIN ) ||
1037 ( chnlset->index > CHNLSET_MAX ))
1040 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1041 BT848_MAX_CHNLSET_NAME_LEN);
1043 chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1050 #define TDA9887_ADDR 0x86
1053 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1055 u_char addr = TDA9887_ADDR;
1057 i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1058 i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1060 /* PAL BG: 0x09 PAL I: 0x0a NTSC: 0x04 */
1062 i2cWrite(bktr, addr, 2, 0x04);
1064 i2cWrite(bktr, addr, 2, 0x09);
1071 #define MT2032_OPTIMIZE_VCO 1
1073 /* holds the value of XOGC register after init */
1074 static int MT2032_XOGC = 4;
1076 /* card.tuner_pllAddr not set during init */
1077 #define MT2032_ADDR 0xc0
1080 #define MT2032_ADDR (bktr->card.tuner_pllAddr)
1084 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1088 if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1090 printf("%s: MT2032 write failed (i2c addr %#x)\n",
1091 bktr_name(bktr), MT2032_ADDR);
1094 if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1096 printf("%s: MT2032 get register %d failed\n",
1097 bktr_name(bktr), regNum);
1104 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1106 i2cWrite(bktr, MT2032_ADDR, regNum, data);
1109 #define MT2032_GetRegister(r) _MT2032_GetRegister(bktr,r)
1110 #define MT2032_SetRegister(r,d) _MT2032_SetRegister(bktr,r,d)
1114 mt2032_init(bktr_ptr_t bktr)
1121 TDA9887_init(bktr, 0);
1123 for (i = 0; i < 21; i++) {
1124 if ((x = MT2032_GetRegister(i)) == -1)
1131 printf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1133 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1134 if (rdbuf[0x13] != 4) {
1135 printf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1139 /* Initialize Registers per spec. */
1140 MT2032_SetRegister(2, 0xff);
1141 MT2032_SetRegister(3, 0x0f);
1142 MT2032_SetRegister(4, 0x1f);
1143 MT2032_SetRegister(6, 0xe4);
1144 MT2032_SetRegister(7, 0x8f);
1145 MT2032_SetRegister(8, 0xc3);
1146 MT2032_SetRegister(9, 0x4e);
1147 MT2032_SetRegister(10, 0xec);
1148 MT2032_SetRegister(13, 0x32);
1150 /* Adjust XOGC (register 7), wait for XOK */
1154 xok = MT2032_GetRegister(0x0e) & 0x01;
1160 xogc = 4; /* min. 4 per spec */
1163 MT2032_SetRegister(7, 0x88 + xogc);
1166 TDA9887_init(bktr, 1);
1174 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1178 f1 = f1 / 1000; /* scale to kHz to avoid 32bit overflows */
1180 spectrum_from /= 1000;
1181 spectrum_to /= 1000;
1189 if ((f > spectrum_from) && (f < spectrum_to)) {
1192 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1211 int fref, lo1, lo1n, lo1a, s, sel;
1212 int lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1216 fref = 5250 * 1000; /* 5.25MHz */
1218 /* per spec 2.3.1 */
1219 desired_lo1 = rfin + if1;
1220 lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1221 lo1freq = lo1 * fref;
1222 desired_lo2 = lo1freq - rfin - if2;
1224 /* per spec 2.3.2 */
1225 for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1226 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1229 if (lo1freq < desired_lo1) {
1235 lo1freq = lo1 * fref;
1236 desired_lo2 = lo1freq - rfin - if2;
1239 /* per spec 2.3.3 */
1240 s = lo1freq / 1000 / 1000;
1242 if (MT2032_OPTIMIZE_VCO) {
1245 } else if (s > 1720) {
1247 } else if (s > 1530) {
1249 } else if (s > 1370) {
1257 } else if (s > 1617) {
1259 } else if (s > 1449) {
1261 } else if (s > 1291) {
1270 /* per spec 2.3.4 */
1272 lo1a = lo1 - (lo1n * 8);
1273 lo2 = desired_lo2 / fref;
1275 lo2a = lo2 - (lo2n * 8);
1276 /* scale to fit in 32bit arith */
1277 lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1278 lo2freq = (lo2a + 8 * lo2n) * fref + lo2num * (fref / 1000) / 3780 * 1000;
1280 if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1281 lo2a > 7 || lo2n < 17 || lo2n > 30) {
1282 printf("MT2032: parameter out of range\n");
1285 /* set up MT2032 register map for transfer over i2c */
1287 buf[1] = lo1a | (sel << 4);
1288 buf[2] = 0x86; /* LOGC */
1289 buf[3] = 0x0f; /* reserved */
1291 buf[5] = (lo2n - 1) | (lo2a << 5);
1292 if (rfin < 400 * 1000 * 1000) {
1295 buf[6] = 0xf4; /* set PKEN per rev 1.2 */
1299 buf[8] = 0xc3; /* reserved */
1300 buf[9] = 0x4e; /* reserved */
1301 buf[10] = 0xec; /* reserved */
1302 buf[11] = (lo2num & 0xff);
1303 buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1309 MT2032_CheckLOLock(bktr_ptr_t bktr)
1312 for (t = 0; t < 10; t++) {
1313 lock = MT2032_GetRegister(0x0e) & 0x06;
1323 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1327 tad1 = MT2032_GetRegister(0x0f) & 0x07;
1348 lo1a = MT2032_GetRegister(0x01) & 0x07;
1349 MT2032_SetRegister(0x01, lo1a | (sel << 4));
1350 lock = MT2032_CheckLOLock(bktr);
1355 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1358 int lint_try, sel, lock = 0;
1360 if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1363 TDA9887_init(bktr, 0);
1365 /* send only the relevant registers per Rev. 1.2 */
1366 MT2032_SetRegister(0, buf[0x00]);
1367 MT2032_SetRegister(1, buf[0x01]);
1368 MT2032_SetRegister(2, buf[0x02]);
1370 MT2032_SetRegister(5, buf[0x05]);
1371 MT2032_SetRegister(6, buf[0x06]);
1372 MT2032_SetRegister(7, buf[0x07]);
1374 MT2032_SetRegister(11, buf[0x0B]);
1375 MT2032_SetRegister(12, buf[0x0C]);
1377 /* wait for PLLs to lock (per manual), retry LINT if not. */
1378 for (lint_try = 0; lint_try < 2; lint_try++) {
1379 lock = MT2032_CheckLOLock(bktr);
1381 if (MT2032_OPTIMIZE_VCO) {
1382 lock = MT2032_OptimizeVCO(bktr, sel, lock);
1387 /* set LINT to re-init PLLs */
1388 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1390 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1393 printf("%s: PLL didn't lock\n", bktr_name(bktr));
1395 MT2032_SetRegister(2, 0x20);
1397 TDA9887_init(bktr, 1);
1402 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1417 if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1418 1090*1000*1000, if2, from, to) == 0) {
1419 bktr->tuner.frequency = freq;
1420 stat = MT2032_GetRegister(0x0e);
1421 tad = MT2032_GetRegister(0x0f);
1423 printf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1424 bktr_name(bktr), freq*62500, stat, tad);