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