tea5757.c revision 1.1.2.2 1 /* $NetBSD: tea5757.c,v 1.1.2.2 2002/01/08 00:30:06 nathanw Exp $ */
2 /* $OpenBSD: tea5757.c,v 1.2 2001/12/06 16:28:18 mickey Exp $ */
3
4 /*
5 * Copyright (c) 2001 Vladimir Popov <jumbo (at) narod.ru>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* Implementation of most common TEA5757 routines */
30
31 /*
32 * Philips TEA5757H Self Tuned Radio
33 * http://www.semiconductors.philips.com/pip/TEA5757H
34 *
35 * The TEA5757; TEA5759 is a 44-pin integrated AM/FM stereo radio circuit.
36 * The radio part is based on the TEA5712.
37 *
38 * The TEA5757 is used in FM-standards in which the local oscillator
39 * frequency is above the radio frequency (e.g. European and American
40 * standards). The TEA5759 is the version in which the oscillator frequency
41 * is below the radio frequency (e.g. Japanese standard).
42 *
43 * The TEA5757; TEA5759 radio has a bus which consists of three wires:
44 * BUS-CLOCK: software driven clock input
45 * DATA: data input/output
46 * WRITE-ENABLE: write/read input
47 *
48 * The TEA5757; TEA5759 has a 25-bit shift register.
49 *
50 * The chips are used in Radiotrack II, Guillemot Maxi Radio FM 2000,
51 * Gemtek PCI cards and most Mediaforte FM tuners and sound cards with
52 * integrated FM tuners.
53 */
54
55 #include <sys/param.h>
56 #include <sys/radioio.h>
57
58 #include <dev/ic/tea5757.h>
59
60 /*
61 * Convert frequency to hardware representation
62 */
63 u_int32_t
64 tea5757_encode_freq(u_int32_t freq)
65 {
66 #ifdef RADIO_TEA5759
67 freq -= IF_FREQ;
68 #else
69 freq += IF_FREQ;
70 #endif /* RADIO_TEA5759 */
71 /*
72 * NO FLOATING POINT!
73 */
74 freq *= 10;
75 freq /= 125;
76 return freq & TEA5757_FREQ;
77 }
78
79 /*
80 * Convert frequency from hardware representation
81 */
82 u_int32_t
83 tea5757_decode_freq(u_int32_t freq)
84 {
85 freq &= TEA5757_FREQ;
86 freq *= 125; /* 12.5 kHz */
87 freq /= 10;
88 #ifdef RADIO_TEA5759
89 freq += IF_FREQ;
90 #else
91 freq -= IF_FREQ;
92 #endif /* RADIO_TEA5759 */
93 return freq;
94 }
95
96 /*
97 * Hardware search
98 */
99 void
100 tea5757_search(struct tea5757_t *tea, u_int32_t stereo, u_int32_t lock, int dir)
101 {
102 u_int32_t reg;
103 u_int co = 0;
104
105 reg = stereo | lock | TEA5757_SEARCH_START;
106 reg |= dir ? TEA5757_SEARCH_UP : TEA5757_SEARCH_DOWN;
107 tea5757_hardware_write(tea, reg);
108
109 DELAY(TEA5757_ACQUISITION_DELAY);
110
111 do {
112 DELAY(TEA5757_WAIT_DELAY);
113 reg = tea->read(tea->iot, tea->ioh, tea->offset);
114 } while ((reg & TEA5757_FREQ) == 0 && ++co < 200);
115 }
116
117 void
118 tea5757_hardware_write(struct tea5757_t *tea, u_int32_t data)
119 {
120 int i = TEA5757_REGISTER_LENGTH;
121
122 tea->init(tea->iot, tea->ioh, tea->offset, 0);
123
124 while (i--)
125 if (data & (1 << i))
126 tea->write_bit(tea->iot, tea->ioh, tea->offset, 1);
127 else
128 tea->write_bit(tea->iot, tea->ioh, tea->offset, 0);
129
130 tea->rset(tea->iot, tea->ioh, tea->offset, 0);
131 }
132
133 u_int32_t
134 tea5757_set_freq(struct tea5757_t *tea, u_int32_t stereo, u_int32_t lock, u_int32_t freq)
135 {
136 u_int32_t data = 0ul;
137
138 if (freq < MIN_FM_FREQ)
139 freq = MIN_FM_FREQ;
140 if (freq > MAX_FM_FREQ)
141 freq = MAX_FM_FREQ;
142
143 data = tea5757_encode_freq(freq) | stereo | lock | TEA5757_SEARCH_END;
144 tea5757_hardware_write(tea, data);
145
146 return freq;
147 }
148
149 u_int32_t
150 tea5757_encode_lock(u_int8_t lock)
151 {
152 u_int32_t ret;
153
154 if (lock < 8)
155 ret = TEA5757_S005;
156 else if (lock > 7 && lock < 15)
157 ret = TEA5757_S010;
158 else if (lock > 14 && lock < 51)
159 ret = TEA5757_S030;
160 else if (lock > 50)
161 ret = TEA5757_S150;
162
163 return ret;
164 }
165
166 u_int8_t
167 tea5757_decode_lock(u_int32_t lock)
168 {
169 u_int8_t ret = 150;
170
171 switch (lock) {
172 case TEA5757_S005:
173 ret = 5;
174 break;
175 case TEA5757_S010:
176 ret = 10;
177 break;
178 case TEA5757_S030:
179 ret = 30;
180 break;
181 case TEA5757_S150:
182 ret = 150;
183 break;
184 }
185
186 return ret;
187 }
188