2 * SPDX-License-Identifier: BSD-4-Clause
4 * 1. Redistributions of source code must retain the
5 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Amancio Hasty and
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * This is part of the Driver for Video Capture Cards (Frame grabbers)
41 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
43 * Copyright Roger Hardiman and Amancio Hasty.
45 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
56 #if (__FreeBSD_version < 500000)
57 #include <machine/clock.h> /* for DELAY */
58 #include <pci/pcivar.h>
61 #include <sys/mutex.h>
62 #include <sys/selinfo.h>
63 #include <dev/pci/pcivar.h>
66 #include <machine/bus.h>
71 #include <dev/ic/bt8xx.h> /* NetBSD .h file location */
72 #include <dev/pci/bktr/bktr_reg.h>
73 #include <dev/pci/bktr/bktr_tuner.h>
74 #include <dev/pci/bktr/bktr_card.h>
75 #include <dev/pci/bktr/bktr_core.h>
77 #include <dev/bktr/ioctl_meteor.h>
78 #include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
79 #include <dev/bktr/bktr_reg.h>
80 #include <dev/bktr/bktr_tuner.h>
81 #include <dev/bktr/bktr_card.h>
82 #include <dev/bktr/bktr_core.h>
87 #if defined( TUNER_AFC )
88 #define AFC_DELAY 10000 /* 10 millisend delay */
90 #define AFC_FREQ_MINUS_125 0x00
91 #define AFC_FREQ_MINUS_62 0x01
92 #define AFC_FREQ_CENTERED 0x02
93 #define AFC_FREQ_PLUS_62 0x03
94 #define AFC_FREQ_PLUS_125 0x04
95 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
96 #endif /* TUNER_AFC */
101 #define TTYPE_NTSC_J 2
103 #define TTYPE_PAL_M 4
104 #define TTYPE_PAL_N 5
105 #define TTYPE_SECAM 6
107 #define TSA552x_CB_MSB (0x80)
108 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
109 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
110 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
111 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
112 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
113 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
114 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
116 #define TSA552x_RADIO (TSA552x_CB_MSB | \
119 /* raise the charge pump voltage for fast tuning */
120 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
126 /* lower the charge pump voltage for better residual oscillator FM */
127 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
132 /* The control value for the ALPS TSCH5 Tuner */
133 #define TSCH5_FCONTROL 0x82
134 #define TSCH5_RADIO 0x86
136 /* The control value for the ALPS TSBH1 Tuner */
137 #define TSBH1_FCONTROL 0xce
140 static void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
143 static const struct TUNER tuners[] = {
144 /* XXX FIXME: fill in the band-switch crosspoints */
146 { "<no>", /* the 'name' */
147 TTYPE_XXX, /* input type */
148 { 0x00, /* control byte for Tuner PLL */
152 { 0x00, 0x00 }, /* band-switch crosspoints */
153 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
156 { "Temic NTSC", /* the 'name' */
157 TTYPE_NTSC, /* input type */
158 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
162 { 0x00, 0x00}, /* band-switch crosspoints */
163 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
166 { "Temic PAL", /* the 'name' */
167 TTYPE_PAL, /* input type */
168 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
172 { 0x00, 0x00 }, /* band-switch crosspoints */
173 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
176 { "Temic SECAM", /* the 'name' */
177 TTYPE_SECAM, /* input type */
178 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
182 { 0x00, 0x00 }, /* band-switch crosspoints */
183 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
186 { "Philips NTSC", /* the 'name' */
187 TTYPE_NTSC, /* input type */
188 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
192 { 0x00, 0x00 }, /* band-switch crosspoints */
193 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
196 { "Philips PAL", /* the 'name' */
197 TTYPE_PAL, /* input type */
198 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
202 { 0x00, 0x00 }, /* band-switch crosspoints */
203 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
206 { "Philips SECAM", /* the 'name' */
207 TTYPE_SECAM, /* input type */
208 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
212 { 0x00, 0x00 }, /* band-switch crosspoints */
213 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
216 { "Temic PAL I", /* the 'name' */
217 TTYPE_PAL, /* input type */
218 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
222 { 0x00, 0x00 }, /* band-switch crosspoints */
223 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
226 { "Philips PAL I", /* the 'name' */
227 TTYPE_PAL, /* input type */
228 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
232 { 0x00, 0x00 }, /* band-switch crosspoints */
233 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
235 /* PHILIPS_FR1236_NTSC */
236 { "Philips FR1236 NTSC FM", /* the 'name' */
237 TTYPE_NTSC, /* input type */
238 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
242 { 0x00, 0x00 }, /* band-switch crosspoints */
243 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
245 /* PHILIPS_FR1216_PAL */
246 { "Philips FR1216 PAL FM" , /* the 'name' */
247 TTYPE_PAL, /* input type */
248 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
252 { 0x00, 0x00 }, /* band-switch crosspoints */
253 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
255 /* PHILIPS_FR1236_SECAM */
256 { "Philips FR1236 SECAM FM", /* the 'name' */
257 TTYPE_SECAM, /* input type */
258 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
262 { 0x00, 0x00 }, /* band-switch crosspoints */
263 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
265 /* ALPS TSCH5 NTSC */
266 { "ALPS TSCH5 NTSC FM", /* the 'name' */
267 TTYPE_NTSC, /* input type */
268 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
272 { 0x00, 0x00 }, /* band-switch crosspoints */
273 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
275 /* ALPS TSBH1 NTSC */
276 { "ALPS TSBH1 NTSC", /* the 'name' */
277 TTYPE_NTSC, /* input type */
278 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
282 { 0x00, 0x00 }, /* band-switch crosspoints */
283 { 0x01, 0x02, 0x08, 0x00 } }, /* the band-switch values */
285 /* MT2032 Microtune */
286 { "MT2032", /* the 'name' */
287 TTYPE_PAL, /* input type */
288 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
292 { 0x00, 0x00 }, /* band-switch crosspoints */
293 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
295 /* LG TPI8PSB12P PAL */
296 { "LG TPI8PSB12P PAL", /* the 'name' */
297 TTYPE_PAL, /* input type */
298 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
302 { 0x00, 0x00 }, /* band-switch crosspoints */
303 { 0xa0, 0x90, 0x30, 0x8e } }, /* the band-switch values */
306 { "PHILIPS_FI1216", /* the 'name' */
307 TTYPE_PAL, /* input type */
308 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
312 { 0x00, 0x00 }, /* band-switch crosspoints */
313 { 0x01, 0x02, 0x04, 0x00 } }, /* the band-switch values */
317 /* scaling factor for frequencies expressed as ints */
318 #define FREQFACTOR 16
322 * entry 0: MAX legal channel
323 * entry 1: IF frequency
324 * expressed as fi{mHz} * 16,
325 * eg 45.75mHz == 45.75 * 16 = 732
326 * entry 2: [place holder/future]
327 * entry 3: base of channel record 0
328 * entry 3 + (x*3): base of channel record 'x'
329 * entry LAST: NULL channel entry marking end of records
332 * int 0: base channel
333 * int 1: frequency of base channel,
334 * expressed as fb{mHz} * 16,
335 * int 2: offset frequency between channels,
336 * expressed as fo{mHz} * 16,
340 * North American Broadcast Channels:
342 * 2: 55.25 mHz - 4: 67.25 mHz
343 * 5: 77.25 mHz - 6: 83.25 mHz
344 * 7: 175.25 mHz - 13: 211.25 mHz
345 * 14: 471.25 mHz - 83: 885.25 mHz
350 static int nabcst[] = {
351 83, (int)( 45.75 * FREQFACTOR), 0,
352 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
353 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
354 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
355 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
361 * North American Cable Channels, IRC:
363 * 2: 55.25 mHz - 4: 67.25 mHz
364 * 5: 77.25 mHz - 6: 83.25 mHz
365 * 7: 175.25 mHz - 13: 211.25 mHz
366 * 14: 121.25 mHz - 22: 169.25 mHz
367 * 23: 217.25 mHz - 94: 643.25 mHz
368 * 95: 91.25 mHz - 99: 115.25 mHz
373 static int irccable[] = {
374 116, (int)( 45.75 * FREQFACTOR), 0,
375 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
376 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
377 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
378 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
379 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
380 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
381 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
387 * North American Cable Channels, HRC:
389 * 2: 54 mHz - 4: 66 mHz
390 * 5: 78 mHz - 6: 84 mHz
391 * 7: 174 mHz - 13: 210 mHz
392 * 14: 120 mHz - 22: 168 mHz
393 * 23: 216 mHz - 94: 642 mHz
394 * 95: 90 mHz - 99: 114 mHz
399 static int hrccable[] = {
400 116, (int)( 45.75 * FREQFACTOR), 0,
401 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
402 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
403 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
404 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
405 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
406 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
407 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
413 * Western European broadcast channels:
415 * (there are others that appear to vary between countries - rmt)
417 * here's the table Philips provides:
418 * caution, some of the offsets don't compute...
527 * Channels S21 - S41 are taken from
528 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
552 * 121 3890 000 IFFREQ
555 static int weurope[] = {
556 121, (int)( 38.90 * FREQFACTOR), 0,
557 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
558 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
559 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
560 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
561 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
562 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
563 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
564 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
565 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
566 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
567 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
572 * Japanese Broadcast Channels:
574 * 1: 91.25MHz - 3: 103.25MHz
575 * 4: 171.25MHz - 7: 189.25MHz
576 * 8: 193.25MHz - 12: 217.25MHz (VHF)
577 * 13: 471.25MHz - 62: 765.25MHz (UHF)
582 #define IF_FREQ 58.75
583 static int jpnbcst[] = {
584 62, (int)(IF_FREQ * FREQFACTOR), 0,
585 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
586 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
587 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
588 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
595 * Japanese Cable Channels:
597 * 1: 91.25MHz - 3: 103.25MHz
598 * 4: 171.25MHz - 7: 189.25MHz
599 * 8: 193.25MHz - 12: 217.25MHz
600 * 13: 109.25MHz - 21: 157.25MHz
602 * 23: 223.25MHz - 63: 463.25MHz
607 #define IF_FREQ 58.75
608 static int jpncable[] = {
609 63, (int)(IF_FREQ * FREQFACTOR), 0,
610 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
611 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
612 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
613 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
614 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
615 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
622 * xUSSR Broadcast Channels:
624 * 1: 49.75MHz - 2: 59.25MHz
625 * 3: 77.25MHz - 5: 93.25MHz
626 * 6: 175.25MHz - 12: 223.25MHz
628 * 21: 471.25MHz - 34: 575.25MHz
629 * 35: 583.25MHz - 69: 855.25MHz
633 * 70: 111.25MHz - 77: 167.25MHz
634 * 78: 231.25MHz -107: 463.25MHz
638 #define IF_FREQ 38.90
639 static int xussr[] = {
640 107, (int)(IF_FREQ * FREQFACTOR), 0,
641 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
642 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
643 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
644 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
645 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
646 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
647 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
653 * Australian broadcast channels
656 #define IF_FREQ 38.90
657 static int australia[] = {
658 83, (int)(IF_FREQ * FREQFACTOR), 0,
659 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
660 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
661 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
662 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
663 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
664 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
671 * France broadcast channels
674 #define IF_FREQ 38.90
675 static int france[] = {
676 69, (int)(IF_FREQ * FREQFACTOR), 0,
677 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
678 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
679 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
680 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
681 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
689 char name[BT848_MAX_CHNLSET_NAME_LEN];
693 {irccable, "cableirc"},
694 {hrccable, "cablehrc"},
695 {weurope, "weurope"},
696 {jpnbcst, "jpnbcst"},
697 {jpncable, "jpncable"},
699 {australia, "australia"},
704 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
705 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
706 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
708 frequency_lookup( bktr_ptr_t bktr, int channel )
712 /* check for "> MAX channel" */
714 if ( channel > TBL_CHNL )
717 /* search the table for data */
718 for ( x = 3; TBL_CHNL; x += 3 ) {
719 if ( channel >= TBL_CHNL ) {
720 return( TBL_BASE_FREQ +
721 ((channel - TBL_CHNL) * TBL_OFFSET) );
725 /* not found, must be below the MIN channel */
733 #define TBL_IF (bktr->format_params == BT848_IFORM_F_NTSCJ || \
734 bktr->format_params == BT848_IFORM_F_NTSCM ? \
735 nabcst[1] : weurope[1])
738 /* Initialise the tuner structures in the bktr_softc */
739 /* This is needed as the tuner details are no longer globally declared */
741 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
742 if (tuner_type < Bt848_MAX_TUNER) {
743 bktr->card.tuner = &tuners[ tuner_type ];
745 bktr->card.tuner = NULL;
751 * Programming the tuner properly is quite complicated.
752 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
753 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
754 * 87.5 Mhz to 108.0 Mhz.
756 * RF and IF. RF = radio frequencies, it is the transmitted signal.
757 * IF is the Intermediate Frequency (the offset from the base
758 * signal where the video, color, audio and NICAM signals are.
760 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
761 * NICAM at 32.348 Mhz.
762 * Strangely enough, there is an IF (intermediate frequency) for
763 * FM Radio which is 10.7 Mhz.
765 * The tuner also works in Bands. Philips bands are
766 * FM radio band 87.50 to 108.00 MHz
767 * Low band 45.75 to 170.00 MHz
768 * Mid band 170.00 to 450.00 MHz
769 * High band 450.00 to 855.25 MHz
772 * Now we need to set the PLL on the tuner to the required freuqncy.
773 * It has a programmable divisor.
775 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
778 * For RADIO we want a different equation.
779 * freq IF is 10.70 MHz (so the data sheet tells me)
780 * N = (freq RF + freq IF) / step size
781 * The step size must be set to 50 khz (so the data sheet tells me)
782 * (note this is 50 kHz, the other things are in MHz)
783 * so we end up with N = 20x(freq RF + 10.7)
790 #define FM_RADIO_BAND 3
793 /* Check if these are correct for other than Philips PAL */
794 #define STATUSBIT_COLD 0x80
795 #define STATUSBIT_LOCK 0x40
796 #define STATUSBIT_TV 0x20
797 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
798 #define STATUSBIT_ADC 0x07
801 * set the frequency of the tuner
802 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
803 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
804 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
808 tv_freq( bktr_ptr_t bktr, int frequency, int type )
810 const struct TUNER* tuner;
816 #if defined( TEST_TUNER_AFC )
817 int oldFrequency, afcDelta;
820 tuner = bktr->card.tuner;
824 if (tuner == &tuners[TUNER_MT2032]) {
825 mt2032_set_tv_freq(bktr, frequency);
828 if (type == TV_FREQUENCY) {
830 * select the band based on frequency
831 * XXX FIXME: get the cross-over points from the tuner struct
833 if ( frequency < (160 * FREQFACTOR ) )
834 band_select = LOW_BAND;
835 else if ( frequency < (454 * FREQFACTOR ) )
836 band_select = MID_BAND;
838 band_select = HIGH_BAND;
840 #if defined( TEST_TUNER_AFC )
841 if ( bktr->tuner.afc )
845 * N = 16 * { fRF(pc) + fIF(pc) }
846 * or N = 16* fRF(pc) + 16*fIF(pc) }
848 * pc is picture carrier, fRF & fIF are in MHz
850 * fortunatly, frequency is passed in as MHz * 16
851 * and the TBL_IF frequency is also stored in MHz * 16
853 N = frequency + TBL_IF;
855 /* set the address of the PLL */
856 addr = bktr->card.tuner_pllAddr;
857 control = tuner->pllControl[ band_select ];
858 band = tuner->bandAddrs[ band_select ];
860 if(!(band && control)) /* Don't try to set un- */
861 return(-1); /* supported modes. */
863 if ( frequency > bktr->tuner.frequency ) {
864 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
865 i2cWrite( bktr, addr, control, band );
868 i2cWrite( bktr, addr, control, band );
869 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
872 #if defined( TUNER_AFC )
873 if ( bktr->tuner.afc == TRUE ) {
874 #if defined( TEST_TUNER_AFC )
875 oldFrequency = frequency;
877 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
878 /* AFC failed, restore requested frequency */
879 N = frequency + TBL_IF;
880 #if defined( TEST_TUNER_AFC )
881 printf("%s: do_afc: failed to lock\n",
884 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
887 frequency = N - TBL_IF;
888 #if defined( TEST_TUNER_AFC )
889 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
890 afcDelta = frequency - oldFrequency;
891 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
894 #endif /* TUNER_AFC */
896 bktr->tuner.frequency = frequency;
899 if ( type == FM_RADIO_FREQUENCY ) {
900 band_select = FM_RADIO_BAND;
903 * N = { fRF(pc) + fIF(pc) }/step_size
904 * The step size is 50kHz for FM radio.
905 * (eg after 102.35MHz comes 102.40 MHz)
906 * fIF is 10.7 MHz (as detailed in the specs)
908 * frequency is passed in as MHz * 100
910 * So, we have N = (frequency/100 + 10.70) /(50/1000)
912 N = (frequency + 1070)/5;
914 /* set the address of the PLL */
915 addr = bktr->card.tuner_pllAddr;
916 control = tuner->pllControl[ band_select ];
917 band = tuner->bandAddrs[ band_select ];
919 if(!(band && control)) /* Don't try to set un- */
920 return(-1); /* supported modes. */
922 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
923 * the ioctls RADIO_SETMODE
924 * and RADIO_GETMODE */
926 i2cWrite( bktr, addr, control, band );
927 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
929 bktr->tuner.frequency = (N * 5) - 1070;
940 #if defined( TUNER_AFC )
945 do_afc( bktr_ptr_t bktr, int addr, int frequency )
951 origFrequency = frequency;
953 /* wait for first setting to take effect */
954 tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
956 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
959 #if defined( TEST_TUNER_AFC )
960 printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
962 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
963 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
965 if ( !(status & 0x40) ) {
966 #if defined( TEST_TUNER_AFC )
967 printf( "%s: no lock!\n", bktr_name(bktr) );
972 switch( status & AFC_BITS ) {
973 case AFC_FREQ_CENTERED:
974 #if defined( TEST_TUNER_AFC )
975 printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
979 case AFC_FREQ_MINUS_125:
980 case AFC_FREQ_MINUS_62:
981 #if defined( TEST_TUNER_AFC )
982 printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
987 case AFC_FREQ_PLUS_62:
988 case AFC_FREQ_PLUS_125:
989 #if defined( TEST_TUNER_AFC )
990 printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
996 i2cWrite( bktr, addr,
997 (frequency>>8) & 0x7f, frequency & 0xff );
1002 i2cWrite( bktr, addr,
1003 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
1007 #endif /* TUNER_AFC */
1012 * Get the Tuner status and signal strength
1014 int get_tuner_status( bktr_ptr_t bktr ) {
1015 if (bktr->card.tuner == &tuners[TUNER_MT2032])
1017 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
1021 * set the channel of the tuner
1024 tv_channel( bktr_ptr_t bktr, int channel )
1028 /* calculate the frequency according to tuner type */
1029 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
1032 /* set the new frequency */
1033 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
1036 /* OK to update records */
1037 return( (bktr->tuner.channel = channel) );
1041 * get channelset name
1044 tuner_getchnlset(struct bktr_chnlset *chnlset)
1046 if (( chnlset->index < CHNLSET_MIN ) ||
1047 ( chnlset->index > CHNLSET_MAX ))
1050 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1051 BT848_MAX_CHNLSET_NAME_LEN);
1053 chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1060 #define TDA9887_ADDR 0x86
1063 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1065 u_char addr = TDA9887_ADDR;
1067 i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1068 i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1070 /* PAL BG: 0x09 PAL I: 0x0a NTSC: 0x04 */
1072 i2cWrite(bktr, addr, 2, 0x04);
1074 i2cWrite(bktr, addr, 2, 0x09);
1081 #define MT2032_OPTIMIZE_VCO 1
1083 /* holds the value of XOGC register after init */
1084 static int MT2032_XOGC = 4;
1086 /* card.tuner_pllAddr not set during init */
1087 #define MT2032_ADDR 0xc0
1090 #define MT2032_ADDR (bktr->card.tuner_pllAddr)
1094 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1098 if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1100 printf("%s: MT2032 write failed (i2c addr %#x)\n",
1101 bktr_name(bktr), MT2032_ADDR);
1104 if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1106 printf("%s: MT2032 get register %d failed\n",
1107 bktr_name(bktr), regNum);
1114 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1116 i2cWrite(bktr, MT2032_ADDR, regNum, data);
1119 #define MT2032_GetRegister(r) _MT2032_GetRegister(bktr,r)
1120 #define MT2032_SetRegister(r,d) _MT2032_SetRegister(bktr,r,d)
1124 mt2032_init(bktr_ptr_t bktr)
1131 TDA9887_init(bktr, 0);
1133 for (i = 0; i < 21; i++) {
1134 if ((x = MT2032_GetRegister(i)) == -1)
1141 printf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1143 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1144 if (rdbuf[0x13] != 4) {
1145 printf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1149 /* Initialize Registers per spec. */
1150 MT2032_SetRegister(2, 0xff);
1151 MT2032_SetRegister(3, 0x0f);
1152 MT2032_SetRegister(4, 0x1f);
1153 MT2032_SetRegister(6, 0xe4);
1154 MT2032_SetRegister(7, 0x8f);
1155 MT2032_SetRegister(8, 0xc3);
1156 MT2032_SetRegister(9, 0x4e);
1157 MT2032_SetRegister(10, 0xec);
1158 MT2032_SetRegister(13, 0x32);
1160 /* Adjust XOGC (register 7), wait for XOK */
1164 xok = MT2032_GetRegister(0x0e) & 0x01;
1170 xogc = 4; /* min. 4 per spec */
1173 MT2032_SetRegister(7, 0x88 + xogc);
1176 TDA9887_init(bktr, 1);
1184 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1188 f1 = f1 / 1000; /* scale to kHz to avoid 32bit overflows */
1190 spectrum_from /= 1000;
1191 spectrum_to /= 1000;
1199 if ((f > spectrum_from) && (f < spectrum_to)) {
1202 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1221 int fref, lo1, lo1n, lo1a, s, sel;
1222 int lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1226 fref = 5250 * 1000; /* 5.25MHz */
1228 /* per spec 2.3.1 */
1229 desired_lo1 = rfin + if1;
1230 lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1231 lo1freq = lo1 * fref;
1232 desired_lo2 = lo1freq - rfin - if2;
1234 /* per spec 2.3.2 */
1235 for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1236 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1239 if (lo1freq < desired_lo1) {
1245 lo1freq = lo1 * fref;
1246 desired_lo2 = lo1freq - rfin - if2;
1249 /* per spec 2.3.3 */
1250 s = lo1freq / 1000 / 1000;
1252 if (MT2032_OPTIMIZE_VCO) {
1255 } else if (s > 1720) {
1257 } else if (s > 1530) {
1259 } else if (s > 1370) {
1267 } else if (s > 1617) {
1269 } else if (s > 1449) {
1271 } else if (s > 1291) {
1280 /* per spec 2.3.4 */
1282 lo1a = lo1 - (lo1n * 8);
1283 lo2 = desired_lo2 / fref;
1285 lo2a = lo2 - (lo2n * 8);
1286 /* scale to fit in 32bit arith */
1287 lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1288 lo2freq = (lo2a + 8 * lo2n) * fref + lo2num * (fref / 1000) / 3780 * 1000;
1290 if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1291 lo2a > 7 || lo2n < 17 || lo2n > 30) {
1292 printf("MT2032: parameter out of range\n");
1295 /* set up MT2032 register map for transfer over i2c */
1297 buf[1] = lo1a | (sel << 4);
1298 buf[2] = 0x86; /* LOGC */
1299 buf[3] = 0x0f; /* reserved */
1301 buf[5] = (lo2n - 1) | (lo2a << 5);
1302 if (rfin < 400 * 1000 * 1000) {
1305 buf[6] = 0xf4; /* set PKEN per rev 1.2 */
1309 buf[8] = 0xc3; /* reserved */
1310 buf[9] = 0x4e; /* reserved */
1311 buf[10] = 0xec; /* reserved */
1312 buf[11] = (lo2num & 0xff);
1313 buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1319 MT2032_CheckLOLock(bktr_ptr_t bktr)
1322 for (t = 0; t < 10; t++) {
1323 lock = MT2032_GetRegister(0x0e) & 0x06;
1333 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1337 tad1 = MT2032_GetRegister(0x0f) & 0x07;
1358 lo1a = MT2032_GetRegister(0x01) & 0x07;
1359 MT2032_SetRegister(0x01, lo1a | (sel << 4));
1360 lock = MT2032_CheckLOLock(bktr);
1365 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1368 int lint_try, sel, lock = 0;
1370 if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1373 TDA9887_init(bktr, 0);
1375 /* send only the relevant registers per Rev. 1.2 */
1376 MT2032_SetRegister(0, buf[0x00]);
1377 MT2032_SetRegister(1, buf[0x01]);
1378 MT2032_SetRegister(2, buf[0x02]);
1380 MT2032_SetRegister(5, buf[0x05]);
1381 MT2032_SetRegister(6, buf[0x06]);
1382 MT2032_SetRegister(7, buf[0x07]);
1384 MT2032_SetRegister(11, buf[0x0B]);
1385 MT2032_SetRegister(12, buf[0x0C]);
1387 /* wait for PLLs to lock (per manual), retry LINT if not. */
1388 for (lint_try = 0; lint_try < 2; lint_try++) {
1389 lock = MT2032_CheckLOLock(bktr);
1391 if (MT2032_OPTIMIZE_VCO) {
1392 lock = MT2032_OptimizeVCO(bktr, sel, lock);
1397 /* set LINT to re-init PLLs */
1398 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1400 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1403 printf("%s: PLL didn't lock\n", bktr_name(bktr));
1405 MT2032_SetRegister(2, 0x20);
1407 TDA9887_init(bktr, 1);
1412 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1427 if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1428 1090*1000*1000, if2, from, to) == 0) {
1429 bktr->tuner.frequency = freq;
1430 stat = MT2032_GetRegister(0x0e);
1431 tad = MT2032_GetRegister(0x0f);
1433 printf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1434 bktr_name(bktr), freq*62500, stat, tad);