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.
33 * $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.20 2005/11/13 13:26:37 netchild Exp $
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>
50 #include <sys/event.h>
52 #include <bus/pci/pcivar.h>
54 #include <dev/video/meteor/ioctl_meteor.h>
55 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
56 #include <dev/video/bktr/bktr_reg.h>
57 #include <dev/video/bktr/bktr_tuner.h>
58 #include <dev/video/bktr/bktr_card.h>
59 #include <dev/video/bktr/bktr_core.h>
63 #if defined( TUNER_AFC )
64 #define AFC_DELAY 10000 /* 10 millisend delay */
66 #define AFC_FREQ_MINUS_125 0x00
67 #define AFC_FREQ_MINUS_62 0x01
68 #define AFC_FREQ_CENTERED 0x02
69 #define AFC_FREQ_PLUS_62 0x03
70 #define AFC_FREQ_PLUS_125 0x04
71 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
72 #endif /* TUNER_AFC */
77 #define TTYPE_NTSC_J 2
83 #define TSA552x_CB_MSB (0x80)
84 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
85 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
86 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
87 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
88 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
89 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
90 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
92 #define TSA552x_RADIO (TSA552x_CB_MSB | \
95 /* raise the charge pump voltage for fast tuning */
96 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
102 /* lower the charge pump voltage for better residual oscillator FM */
103 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
108 /* The control value for the ALPS TSCH5 Tuner */
109 #define TSCH5_FCONTROL 0x82
110 #define TSCH5_RADIO 0x86
112 /* The control value for the ALPS TSBH1 Tuner */
113 #define TSBH1_FCONTROL 0xce
116 static void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
119 static const struct TUNER tuners[] = {
120 /* XXX FIXME: fill in the band-switch crosspoints */
122 { "<no>", /* the 'name' */
123 TTYPE_XXX, /* input type */
124 { 0x00, /* control byte for Tuner PLL */
128 { 0x00, 0x00 }, /* band-switch crosspoints */
129 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
132 { "Temic NTSC", /* the 'name' */
133 TTYPE_NTSC, /* input type */
134 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
138 { 0x00, 0x00}, /* band-switch crosspoints */
139 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
142 { "Temic PAL", /* the 'name' */
143 TTYPE_PAL, /* input type */
144 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
148 { 0x00, 0x00 }, /* band-switch crosspoints */
149 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
152 { "Temic SECAM", /* the 'name' */
153 TTYPE_SECAM, /* input type */
154 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
158 { 0x00, 0x00 }, /* band-switch crosspoints */
159 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
162 { "Philips NTSC", /* the 'name' */
163 TTYPE_NTSC, /* input type */
164 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
168 { 0x00, 0x00 }, /* band-switch crosspoints */
169 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
172 { "Philips PAL", /* the 'name' */
173 TTYPE_PAL, /* input type */
174 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
178 { 0x00, 0x00 }, /* band-switch crosspoints */
179 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
182 { "Philips SECAM", /* the 'name' */
183 TTYPE_SECAM, /* input type */
184 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
188 { 0x00, 0x00 }, /* band-switch crosspoints */
189 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
192 { "Temic PAL I", /* the 'name' */
193 TTYPE_PAL, /* input type */
194 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
198 { 0x00, 0x00 }, /* band-switch crosspoints */
199 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
202 { "Philips PAL I", /* the 'name' */
203 TTYPE_PAL, /* input type */
204 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
208 { 0x00, 0x00 }, /* band-switch crosspoints */
209 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
211 /* PHILIPS_FR1236_NTSC */
212 { "Philips FR1236 NTSC FM", /* the 'name' */
213 TTYPE_NTSC, /* input type */
214 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
218 { 0x00, 0x00 }, /* band-switch crosspoints */
219 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
221 /* PHILIPS_FR1216_PAL */
222 { "Philips FR1216 PAL FM" , /* the 'name' */
223 TTYPE_PAL, /* input type */
224 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
228 { 0x00, 0x00 }, /* band-switch crosspoints */
229 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
231 /* PHILIPS_FR1236_SECAM */
232 { "Philips FR1236 SECAM FM", /* the 'name' */
233 TTYPE_SECAM, /* input type */
234 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
238 { 0x00, 0x00 }, /* band-switch crosspoints */
239 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
241 /* ALPS TSCH5 NTSC */
242 { "ALPS TSCH5 NTSC FM", /* the 'name' */
243 TTYPE_NTSC, /* input type */
244 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
248 { 0x00, 0x00 }, /* band-switch crosspoints */
249 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
251 /* ALPS TSBH1 NTSC */
252 { "ALPS TSBH1 NTSC", /* the 'name' */
253 TTYPE_NTSC, /* input type */
254 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
258 { 0x00, 0x00 }, /* band-switch crosspoints */
259 { 0x01, 0x02, 0x08, 0x00 } }, /* the band-switch values */
261 /* MT2032 Microtune */
262 { "MT2032", /* the 'name' */
263 TTYPE_PAL, /* input type */
264 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
268 { 0x00, 0x00 }, /* band-switch crosspoints */
269 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
271 /* LG TPI8PSB12P PAL */
272 { "LG TPI8PSB12P PAL", /* the 'name' */
273 TTYPE_PAL, /* input type */
274 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
278 { 0x00, 0x00 }, /* band-switch crosspoints */
279 { 0xa0, 0x90, 0x30, 0x8e } }, /* the band-switch values */
283 /* scaling factor for frequencies expressed as ints */
284 #define FREQFACTOR 16
288 * entry 0: MAX legal channel
289 * entry 1: IF frequency
290 * expressed as fi{mHz} * 16,
291 * eg 45.75mHz == 45.75 * 16 = 732
292 * entry 2: [place holder/future]
293 * entry 3: base of channel record 0
294 * entry 3 + (x*3): base of channel record 'x'
295 * entry LAST: NULL channel entry marking end of records
298 * int 0: base channel
299 * int 1: frequency of base channel,
300 * expressed as fb{mHz} * 16,
301 * int 2: offset frequency between channels,
302 * expressed as fo{mHz} * 16,
306 * North American Broadcast Channels:
308 * 2: 55.25 mHz - 4: 67.25 mHz
309 * 5: 77.25 mHz - 6: 83.25 mHz
310 * 7: 175.25 mHz - 13: 211.25 mHz
311 * 14: 471.25 mHz - 83: 885.25 mHz
316 static int nabcst[] = {
317 83, (int)( 45.75 * FREQFACTOR), 0,
318 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
319 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
320 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
321 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
327 * North American Cable Channels, IRC:
329 * 2: 55.25 mHz - 4: 67.25 mHz
330 * 5: 77.25 mHz - 6: 83.25 mHz
331 * 7: 175.25 mHz - 13: 211.25 mHz
332 * 14: 121.25 mHz - 22: 169.25 mHz
333 * 23: 217.25 mHz - 94: 643.25 mHz
334 * 95: 91.25 mHz - 99: 115.25 mHz
339 static int irccable[] = {
340 116, (int)( 45.75 * FREQFACTOR), 0,
341 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
342 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
343 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
344 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
345 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
346 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
347 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
353 * North American Cable Channels, HRC:
355 * 2: 54 mHz - 4: 66 mHz
356 * 5: 78 mHz - 6: 84 mHz
357 * 7: 174 mHz - 13: 210 mHz
358 * 14: 120 mHz - 22: 168 mHz
359 * 23: 216 mHz - 94: 642 mHz
360 * 95: 90 mHz - 99: 114 mHz
365 static int hrccable[] = {
366 116, (int)( 45.75 * FREQFACTOR), 0,
367 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
368 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
369 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
370 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
371 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
372 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
373 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
379 * Western European broadcast channels:
381 * (there are others that appear to vary between countries - rmt)
383 * here's the table Philips provides:
384 * caution, some of the offsets don't compute...
493 * Channels S21 - S41 are taken from
494 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
518 * 121 3890 000 IFFREQ
521 static int weurope[] = {
522 121, (int)( 38.90 * FREQFACTOR), 0,
523 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
524 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
525 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
526 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
527 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
528 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
529 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
530 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
531 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
532 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
533 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
538 * Japanese Broadcast Channels:
540 * 1: 91.25MHz - 3: 103.25MHz
541 * 4: 171.25MHz - 7: 189.25MHz
542 * 8: 193.25MHz - 12: 217.25MHz (VHF)
543 * 13: 471.25MHz - 62: 765.25MHz (UHF)
550 #define IF_FREQ 45.75
551 static int jpnbcst[] = {
552 62, (int)(IF_FREQ * FREQFACTOR), 0,
553 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
554 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
555 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
556 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
563 * Japanese Cable Channels:
565 * 1: 91.25MHz - 3: 103.25MHz
566 * 4: 171.25MHz - 7: 189.25MHz
567 * 8: 193.25MHz - 12: 217.25MHz
568 * 13: 109.25MHz - 21: 157.25MHz
570 * 23: 223.25MHz - 63: 463.25MHz
575 #define IF_FREQ 45.75
576 static int jpncable[] = {
577 63, (int)(IF_FREQ * FREQFACTOR), 0,
578 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
579 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
580 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
581 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
582 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
583 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
590 * xUSSR Broadcast Channels:
592 * 1: 49.75MHz - 2: 59.25MHz
593 * 3: 77.25MHz - 5: 93.25MHz
594 * 6: 175.25MHz - 12: 223.25MHz
596 * 21: 471.25MHz - 34: 575.25MHz
597 * 35: 583.25MHz - 69: 855.25MHz
601 * 70: 111.25MHz - 77: 167.25MHz
602 * 78: 231.25MHz -107: 463.25MHz
606 #define IF_FREQ 38.90
607 static int xussr[] = {
608 107, (int)(IF_FREQ * FREQFACTOR), 0,
609 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
610 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
611 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
612 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
613 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
614 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
615 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
621 * Australian broadcast channels
624 #define IF_FREQ 38.90
625 static int australia[] = {
626 83, (int)(IF_FREQ * FREQFACTOR), 0,
627 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
628 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
629 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
630 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
631 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
632 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
639 * France broadcast channels
642 #define IF_FREQ 38.90
643 static int france[] = {
644 69, (int)(IF_FREQ * FREQFACTOR), 0,
645 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
646 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
647 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
648 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
649 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
657 char name[BT848_MAX_CHNLSET_NAME_LEN];
661 {irccable, "cableirc"},
662 {hrccable, "cablehrc"},
663 {weurope, "weurope"},
664 {jpnbcst, "jpnbcst"},
665 {jpncable, "jpncable"},
667 {australia, "australia"},
672 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
673 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
674 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
676 frequency_lookup( bktr_ptr_t bktr, int channel )
680 /* check for "> MAX channel" */
682 if ( channel > TBL_CHNL )
685 /* search the table for data */
686 for ( x = 3; TBL_CHNL; x += 3 ) {
687 if ( channel >= TBL_CHNL ) {
688 return( TBL_BASE_FREQ +
689 ((channel - TBL_CHNL) * TBL_OFFSET) );
693 /* not found, must be below the MIN channel */
701 #define TBL_IF (bktr->format_params == BT848_IFORM_F_NTSCJ || \
702 bktr->format_params == BT848_IFORM_F_NTSCM ? \
703 nabcst[1] : weurope[1])
706 /* Initialise the tuner structures in the bktr_softc */
707 /* This is needed as the tuner details are no longer globally declared */
709 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
710 if (tuner_type < Bt848_MAX_TUNER) {
711 bktr->card.tuner = &tuners[ tuner_type ];
713 bktr->card.tuner = NULL;
719 * Programming the tuner properly is quite complicated.
720 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
721 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
722 * 87.5 Mhz to 108.0 Mhz.
724 * RF and IF. RF = radio frequencies, it is the transmitted signal.
725 * IF is the Intermediate Frequency (the offset from the base
726 * signal where the video, color, audio and NICAM signals are.
728 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
729 * NICAM at 32.348 Mhz.
730 * Strangely enough, there is an IF (intermediate frequency) for
731 * FM Radio which is 10.7 Mhz.
733 * The tuner also works in Bands. Philips bands are
734 * FM radio band 87.50 to 108.00 MHz
735 * Low band 45.75 to 170.00 MHz
736 * Mid band 170.00 to 450.00 MHz
737 * High band 450.00 to 855.25 MHz
740 * Now we need to set the PLL on the tuner to the required freuqncy.
741 * It has a programmable divisor.
743 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
746 * For RADIO we want a different equation.
747 * freq IF is 10.70 MHz (so the data sheet tells me)
748 * N = (freq RF + freq IF) / step size
749 * The step size must be set to 50 khz (so the data sheet tells me)
750 * (note this is 50 kHz, the other things are in MHz)
751 * so we end up with N = 20x(freq RF + 10.7)
758 #define FM_RADIO_BAND 3
761 /* Check if these are correct for other than Philips PAL */
762 #define STATUSBIT_COLD 0x80
763 #define STATUSBIT_LOCK 0x40
764 #define STATUSBIT_TV 0x20
765 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
766 #define STATUSBIT_ADC 0x07
769 * set the frequency of the tuner
770 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
771 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
772 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
776 tv_freq( bktr_ptr_t bktr, int frequency, int type )
778 const struct TUNER* tuner;
784 #if defined( TEST_TUNER_AFC )
785 int oldFrequency, afcDelta;
788 tuner = bktr->card.tuner;
792 if (tuner == &tuners[TUNER_MT2032]) {
793 mt2032_set_tv_freq(bktr, frequency);
796 if (type == TV_FREQUENCY) {
798 * select the band based on frequency
799 * XXX FIXME: get the cross-over points from the tuner struct
801 if ( frequency < (160 * FREQFACTOR ) )
802 band_select = LOW_BAND;
803 else if ( frequency < (454 * FREQFACTOR ) )
804 band_select = MID_BAND;
806 band_select = HIGH_BAND;
808 #if defined( TEST_TUNER_AFC )
809 if ( bktr->tuner.afc )
813 * N = 16 * { fRF(pc) + fIF(pc) }
814 * or N = 16* fRF(pc) + 16*fIF(pc) }
816 * pc is picture carrier, fRF & fIF are in MHz
818 * fortunatly, frequency is passed in as MHz * 16
819 * and the TBL_IF frequency is also stored in MHz * 16
821 N = frequency + TBL_IF;
823 /* set the address of the PLL */
824 addr = bktr->card.tuner_pllAddr;
825 control = tuner->pllControl[ band_select ];
826 band = tuner->bandAddrs[ band_select ];
828 if(!(band && control)) /* Don't try to set un- */
829 return(-1); /* supported modes. */
831 if ( frequency > bktr->tuner.frequency ) {
832 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
833 i2cWrite( bktr, addr, control, band );
836 i2cWrite( bktr, addr, control, band );
837 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
840 #if defined( TUNER_AFC )
841 if ( bktr->tuner.afc == TRUE ) {
842 #if defined( TEST_TUNER_AFC )
843 oldFrequency = frequency;
845 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
846 /* AFC failed, restore requested frequency */
847 N = frequency + TBL_IF;
848 #if defined( TEST_TUNER_AFC )
849 kprintf("%s: do_afc: failed to lock\n",
852 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
855 frequency = N - TBL_IF;
856 #if defined( TEST_TUNER_AFC )
857 kprintf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
858 afcDelta = frequency - oldFrequency;
859 kprintf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
862 #endif /* TUNER_AFC */
864 bktr->tuner.frequency = frequency;
867 if ( type == FM_RADIO_FREQUENCY ) {
868 band_select = FM_RADIO_BAND;
871 * N = { fRF(pc) + fIF(pc) }/step_size
872 * The step size is 50kHz for FM radio.
873 * (eg after 102.35MHz comes 102.40 MHz)
874 * fIF is 10.7 MHz (as detailed in the specs)
876 * frequency is passed in as MHz * 100
878 * So, we have N = (frequency/100 + 10.70) /(50/1000)
880 N = (frequency + 1070)/5;
882 /* set the address of the PLL */
883 addr = bktr->card.tuner_pllAddr;
884 control = tuner->pllControl[ band_select ];
885 band = tuner->bandAddrs[ band_select ];
887 if(!(band && control)) /* Don't try to set un- */
888 return(-1); /* supported modes. */
890 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
891 * the ioctls RADIO_SETMODE
892 * and RADIO_GETMODE */
894 i2cWrite( bktr, addr, control, band );
895 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
897 bktr->tuner.frequency = (N * 5) - 1070;
908 #if defined( TUNER_AFC )
913 do_afc( bktr_ptr_t bktr, int addr, int frequency )
919 origFrequency = frequency;
921 /* wait for first setting to take effect */
922 tsleep( BKTR_SLEEP, 0, "tuning", hz/8 );
924 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
927 #if defined( TEST_TUNER_AFC )
928 kprintf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
930 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
931 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
933 if ( !(status & 0x40) ) {
934 #if defined( TEST_TUNER_AFC )
935 kprintf( "%s: no lock!\n", bktr_name(bktr) );
940 switch( status & AFC_BITS ) {
941 case AFC_FREQ_CENTERED:
942 #if defined( TEST_TUNER_AFC )
943 kprintf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
947 case AFC_FREQ_MINUS_125:
948 case AFC_FREQ_MINUS_62:
949 #if defined( TEST_TUNER_AFC )
950 kprintf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
955 case AFC_FREQ_PLUS_62:
956 case AFC_FREQ_PLUS_125:
957 #if defined( TEST_TUNER_AFC )
958 kprintf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
964 i2cWrite( bktr, addr,
965 (frequency>>8) & 0x7f, frequency & 0xff );
970 i2cWrite( bktr, addr,
971 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
975 #endif /* TUNER_AFC */
980 * Get the Tuner status and signal strength
982 int get_tuner_status( bktr_ptr_t bktr ) {
983 if (bktr->card.tuner == &tuners[TUNER_MT2032])
985 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
989 * set the channel of the tuner
992 tv_channel( bktr_ptr_t bktr, int channel )
996 /* calculate the frequency according to tuner type */
997 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
1000 /* set the new frequency */
1001 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
1004 /* OK to update records */
1005 return( (bktr->tuner.channel = channel) );
1009 * get channelset name
1012 tuner_getchnlset(struct bktr_chnlset *chnlset)
1014 if (( chnlset->index < CHNLSET_MIN ) ||
1015 ( chnlset->index > CHNLSET_MAX ))
1018 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1019 BT848_MAX_CHNLSET_NAME_LEN);
1021 chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1028 #define TDA9887_ADDR 0x86
1031 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1033 u_char addr = TDA9887_ADDR;
1035 i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1036 i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1038 /* PAL BG: 0x09 PAL I: 0x0a NTSC: 0x04 */
1040 i2cWrite(bktr, addr, 2, 0x04);
1042 i2cWrite(bktr, addr, 2, 0x09);
1049 #define MT2032_OPTIMIZE_VCO 1
1051 /* holds the value of XOGC register after init */
1052 static int MT2032_XOGC = 4;
1054 /* card.tuner_pllAddr not set during init */
1055 #define MT2032_ADDR 0xc0
1058 #define MT2032_ADDR (bktr->card.tuner_pllAddr)
1062 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1066 if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1068 kprintf("%s: MT2032 write failed (i2c addr %#x)\n",
1069 bktr_name(bktr), MT2032_ADDR);
1072 if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1074 kprintf("%s: MT2032 get register %d failed\n",
1075 bktr_name(bktr), regNum);
1082 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1084 i2cWrite(bktr, MT2032_ADDR, regNum, data);
1087 #define MT2032_GetRegister(r) _MT2032_GetRegister(bktr,r)
1088 #define MT2032_SetRegister(r,d) _MT2032_SetRegister(bktr,r,d)
1092 mt2032_init(bktr_ptr_t bktr)
1099 TDA9887_init(bktr, 0);
1101 for (i = 0; i < 21; i++) {
1102 if ((x = MT2032_GetRegister(i)) == -1)
1109 kprintf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1111 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1112 if (rdbuf[0x13] != 4) {
1113 kprintf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1117 /* Initialize Registers per spec. */
1118 MT2032_SetRegister(2, 0xff);
1119 MT2032_SetRegister(3, 0x0f);
1120 MT2032_SetRegister(4, 0x1f);
1121 MT2032_SetRegister(6, 0xe4);
1122 MT2032_SetRegister(7, 0x8f);
1123 MT2032_SetRegister(8, 0xc3);
1124 MT2032_SetRegister(9, 0x4e);
1125 MT2032_SetRegister(10, 0xec);
1126 MT2032_SetRegister(13, 0x32);
1128 /* Adjust XOGC (register 7), wait for XOK */
1132 xok = MT2032_GetRegister(0x0e) & 0x01;
1138 xogc = 4; /* min. 4 per spec */
1141 MT2032_SetRegister(7, 0x88 + xogc);
1144 TDA9887_init(bktr, 1);
1152 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1156 f1 = f1 / 1000; /* scale to kHz to avoid 32bit overflows */
1158 spectrum_from /= 1000;
1159 spectrum_to /= 1000;
1167 if ((f > spectrum_from) && (f < spectrum_to)) {
1170 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1189 int fref, lo1, lo1n, lo1a, s, sel;
1190 int lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1194 fref = 5250 * 1000; /* 5.25MHz */
1196 /* per spec 2.3.1 */
1197 desired_lo1 = rfin + if1;
1198 lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1199 lo1freq = lo1 * fref;
1200 desired_lo2 = lo1freq - rfin - if2;
1202 /* per spec 2.3.2 */
1203 for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1204 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1207 if (lo1freq < desired_lo1) {
1213 lo1freq = lo1 * fref;
1214 desired_lo2 = lo1freq - rfin - if2;
1217 /* per spec 2.3.3 */
1218 s = lo1freq / 1000 / 1000;
1220 if (MT2032_OPTIMIZE_VCO) {
1223 } else if (s > 1720) {
1225 } else if (s > 1530) {
1227 } else if (s > 1370) {
1235 } else if (s > 1617) {
1237 } else if (s > 1449) {
1239 } else if (s > 1291) {
1248 /* per spec 2.3.4 */
1250 lo1a = lo1 - (lo1n * 8);
1251 lo2 = desired_lo2 / fref;
1253 lo2a = lo2 - (lo2n * 8);
1254 /* scale to fit in 32bit arith */
1255 lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1257 if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1258 lo2a > 7 || lo2n < 17 || lo2n > 30) {
1259 kprintf("MT2032: parameter out of range\n");
1262 /* set up MT2032 register map for transfer over i2c */
1264 buf[1] = lo1a | (sel << 4);
1265 buf[2] = 0x86; /* LOGC */
1266 buf[3] = 0x0f; /* reserved */
1268 buf[5] = (lo2n - 1) | (lo2a << 5);
1269 if (rfin < 400 * 1000 * 1000) {
1272 buf[6] = 0xf4; /* set PKEN per rev 1.2 */
1276 buf[8] = 0xc3; /* reserved */
1277 buf[9] = 0x4e; /* reserved */
1278 buf[10] = 0xec; /* reserved */
1279 buf[11] = (lo2num & 0xff);
1280 buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1286 MT2032_CheckLOLock(bktr_ptr_t bktr)
1289 for (t = 0; t < 10; t++) {
1290 lock = MT2032_GetRegister(0x0e) & 0x06;
1300 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1304 tad1 = MT2032_GetRegister(0x0f) & 0x07;
1325 lo1a = MT2032_GetRegister(0x01) & 0x07;
1326 MT2032_SetRegister(0x01, lo1a | (sel << 4));
1327 lock = MT2032_CheckLOLock(bktr);
1332 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1335 int lint_try, sel, lock = 0;
1337 if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1340 TDA9887_init(bktr, 0);
1342 /* send only the relevant registers per Rev. 1.2 */
1343 MT2032_SetRegister(0, buf[0x00]);
1344 MT2032_SetRegister(1, buf[0x01]);
1345 MT2032_SetRegister(2, buf[0x02]);
1347 MT2032_SetRegister(5, buf[0x05]);
1348 MT2032_SetRegister(6, buf[0x06]);
1349 MT2032_SetRegister(7, buf[0x07]);
1351 MT2032_SetRegister(11, buf[0x0B]);
1352 MT2032_SetRegister(12, buf[0x0C]);
1354 /* wait for PLLs to lock (per manual), retry LINT if not. */
1355 for (lint_try = 0; lint_try < 2; lint_try++) {
1356 lock = MT2032_CheckLOLock(bktr);
1358 if (MT2032_OPTIMIZE_VCO) {
1359 lock = MT2032_OptimizeVCO(bktr, sel, lock);
1364 /* set LINT to re-init PLLs */
1365 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1367 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1370 kprintf("%s: PLL didn't lock\n", bktr_name(bktr));
1372 MT2032_SetRegister(2, 0x20);
1374 TDA9887_init(bktr, 1);
1379 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1394 if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1395 1090*1000*1000, if2, from, to) == 0) {
1396 bktr->tuner.frequency = freq;
1397 stat = MT2032_GetRegister(0x0e);
1398 tad = MT2032_GetRegister(0x0f);
1400 kprintf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1401 bktr_name(bktr), freq*62500, stat, tad);