sf16fmr2.c revision 1.3 1 1.3 augustss /* $NetBSD: sf16fmr2.c,v 1.3 2002/01/03 18:13:20 augustss Exp $ */
2 1.1 augustss /* $OpenBSD: sf16fmr2.c,v 1.3 2001/12/18 18:48:08 mickey Exp $ */
3 1.1 augustss /* $RuOBSD: sf16fmr2.c,v 1.12 2001/10/18 16:51:36 pva Exp $ */
4 1.1 augustss
5 1.1 augustss /*
6 1.1 augustss * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.net>,
7 1.1 augustss * Vladimir Popov <jumbo (at) narod.ru>
8 1.1 augustss * All rights reserved.
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 1.1 augustss * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 augustss * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 augustss * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 augustss * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 augustss * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 augustss * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 augustss * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 augustss * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 augustss * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 augustss */
30 1.1 augustss
31 1.1 augustss /* SoundForte RadioLink SF16-FMR2 FM Radio Card device driver */
32 1.1 augustss
33 1.1 augustss /*
34 1.1 augustss * Philips TEA5757H AM/FM Self Tuned Radio:
35 1.1 augustss * http://www.semiconductors.philips.com/pip/TEA5757H
36 1.1 augustss */
37 1.1 augustss
38 1.1 augustss #include <sys/param.h>
39 1.1 augustss #include <sys/systm.h>
40 1.1 augustss #include <sys/proc.h>
41 1.1 augustss #include <sys/errno.h>
42 1.1 augustss #include <sys/ioctl.h>
43 1.1 augustss #include <sys/device.h>
44 1.1 augustss #include <sys/radioio.h>
45 1.1 augustss
46 1.1 augustss #include <dev/isa/isavar.h>
47 1.1 augustss #include <dev/radio_if.h>
48 1.1 augustss #include <dev/ic/tea5757.h>
49 1.1 augustss
50 1.1 augustss #define SF16FMR2_BASE_VALID(x) (x == 0x384)
51 1.1 augustss #define SF16FMR2_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
52 1.1 augustss RADIO_CAPS_DETECT_SIGNAL | \
53 1.1 augustss RADIO_CAPS_SET_MONO | \
54 1.1 augustss RADIO_CAPS_LOCK_SENSITIVITY | \
55 1.1 augustss RADIO_CAPS_HW_AFC | \
56 1.1 augustss RADIO_CAPS_HW_SEARCH
57 1.1 augustss
58 1.1 augustss #define SF16FMR2_AMPLIFIER (1 << 7)
59 1.1 augustss #define SF16FMR2_SIGNAL (1 << 3)
60 1.1 augustss #define SF16FMR2_STEREO (1 << 3)
61 1.1 augustss
62 1.1 augustss #define SF16FMR2_MUTE 0x00
63 1.1 augustss #define SF16FMR2_UNMUTE 0x04
64 1.1 augustss
65 1.1 augustss #define SF16FMR2_DATA_ON (1 << 0)
66 1.1 augustss #define SF16FMR2_DATA_OFF (0 << 0)
67 1.1 augustss
68 1.1 augustss #define SF16FMR2_CLCK_ON (1 << 1)
69 1.1 augustss #define SF16FMR2_CLCK_OFF (0 << 1)
70 1.1 augustss
71 1.1 augustss #define SF16FMR2_WREN_ON (0 << 2) /* SF16-FMR2 has inverse WREN */
72 1.1 augustss #define SF16FMR2_WREN_OFF (1 << 2)
73 1.1 augustss
74 1.1 augustss #define SF16FMR2_READ_CLOCK_LOW \
75 1.1 augustss SF16FMR2_DATA_ON | SF16FMR2_CLCK_OFF | SF16FMR2_WREN_OFF
76 1.1 augustss
77 1.1 augustss #define SF16FMR2_READ_CLOCK_HIGH \
78 1.1 augustss SF16FMR2_DATA_ON | SF16FMR2_CLCK_ON | SF16FMR2_WREN_OFF
79 1.1 augustss
80 1.1 augustss int sf2r_probe(struct device *, struct cfdata *, void *);
81 1.1 augustss void sf2r_attach(struct device *, struct device * self, void *);
82 1.1 augustss
83 1.1 augustss int sf2r_get_info(void *, struct radio_info *);
84 1.1 augustss int sf2r_set_info(void *, struct radio_info *);
85 1.1 augustss int sf2r_search(void *, int);
86 1.1 augustss
87 1.1 augustss /* define our interface to the higher level radio driver */
88 1.1 augustss struct radio_hw_if sf2r_hw_if = {
89 1.1 augustss NULL, /* open */
90 1.1 augustss NULL, /* close */
91 1.1 augustss sf2r_get_info,
92 1.1 augustss sf2r_set_info,
93 1.1 augustss sf2r_search
94 1.1 augustss };
95 1.1 augustss
96 1.1 augustss struct sf2r_softc {
97 1.1 augustss struct device sc_dev;
98 1.1 augustss
99 1.1 augustss u_int32_t freq;
100 1.1 augustss u_int32_t stereo;
101 1.1 augustss u_int32_t lock;
102 1.1 augustss u_int8_t vol;
103 1.1 augustss int mute;
104 1.1 augustss
105 1.1 augustss struct tea5757_t tea;
106 1.1 augustss };
107 1.1 augustss
108 1.1 augustss struct cfattach sf2r_ca = {
109 1.1 augustss sizeof(struct sf2r_softc), sf2r_probe, sf2r_attach
110 1.1 augustss };
111 1.1 augustss
112 1.1 augustss void sf2r_set_mute(struct sf2r_softc *);
113 1.1 augustss int sf2r_find(bus_space_tag_t, bus_space_handle_t);
114 1.1 augustss
115 1.1 augustss u_int32_t sf2r_read_register(bus_space_tag_t, bus_space_handle_t, bus_size_t);
116 1.1 augustss
117 1.1 augustss void sf2r_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
118 1.1 augustss void sf2r_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
119 1.1 augustss void sf2r_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
120 1.1 augustss
121 1.1 augustss int
122 1.1 augustss sf2r_probe(struct device *parent, struct cfdata *cf, void *aux)
123 1.1 augustss {
124 1.1 augustss struct isa_attach_args *ia = aux;
125 1.1 augustss bus_space_tag_t iot = ia->ia_iot;
126 1.1 augustss bus_space_handle_t ioh;
127 1.2 augustss u_int r;
128 1.1 augustss int iosize = 1, iobase = ia->ia_iobase;
129 1.1 augustss
130 1.1 augustss if (!SF16FMR2_BASE_VALID(iobase)) {
131 1.1 augustss printf("sf2r: configured iobase 0x%x invalid\n", iobase);
132 1.1 augustss return 0;
133 1.1 augustss }
134 1.1 augustss
135 1.1 augustss if (bus_space_map(iot, iobase, iosize, 0, &ioh))
136 1.1 augustss return 0;
137 1.1 augustss
138 1.2 augustss r = sf2r_find(iot, ioh);
139 1.2 augustss
140 1.1 augustss bus_space_unmap(iot, ioh, iosize);
141 1.1 augustss
142 1.2 augustss ia->ia_iosize = iosize;
143 1.1 augustss
144 1.2 augustss return (r != 0);
145 1.1 augustss }
146 1.1 augustss
147 1.1 augustss void
148 1.1 augustss sf2r_attach(struct device *parent, struct device *self, void *aux)
149 1.1 augustss {
150 1.1 augustss struct sf2r_softc *sc = (void *) self;
151 1.1 augustss struct isa_attach_args *ia = aux;
152 1.1 augustss
153 1.1 augustss sc->tea.iot = ia->ia_iot;
154 1.1 augustss sc->mute = 0;
155 1.1 augustss sc->vol = 0;
156 1.1 augustss sc->freq = MIN_FM_FREQ;
157 1.1 augustss sc->stereo = TEA5757_STEREO;
158 1.1 augustss sc->lock = TEA5757_S030;
159 1.1 augustss
160 1.1 augustss /* remap I/O */
161 1.1 augustss if (bus_space_map(sc->tea.iot, ia->ia_iobase, ia->ia_iosize,
162 1.1 augustss 0, &sc->tea.ioh))
163 1.1 augustss panic("sf2rattach: bus_space_map() failed");
164 1.1 augustss
165 1.1 augustss sc->tea.offset = 0;
166 1.1 augustss
167 1.1 augustss sc->tea.init = sf2r_init;
168 1.1 augustss sc->tea.rset = sf2r_rset;
169 1.1 augustss sc->tea.write_bit = sf2r_write_bit;
170 1.1 augustss sc->tea.read = sf2r_read_register;
171 1.1 augustss
172 1.3 augustss printf(": SoundForte RadioLink SF16-FMR2\n");
173 1.1 augustss tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
174 1.1 augustss sf2r_set_mute(sc);
175 1.1 augustss
176 1.1 augustss radio_attach_mi(&sf2r_hw_if, sc, &sc->sc_dev);
177 1.1 augustss }
178 1.1 augustss
179 1.1 augustss /*
180 1.1 augustss * Mute/unmute the card
181 1.1 augustss */
182 1.1 augustss void
183 1.1 augustss sf2r_set_mute(struct sf2r_softc *sc)
184 1.1 augustss {
185 1.1 augustss u_int8_t mute;
186 1.1 augustss
187 1.1 augustss mute = (sc->mute || !sc->vol) ? SF16FMR2_MUTE : SF16FMR2_UNMUTE;
188 1.1 augustss bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
189 1.1 augustss DELAY(64);
190 1.1 augustss bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
191 1.1 augustss }
192 1.1 augustss
193 1.1 augustss void
194 1.1 augustss sf2r_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
195 1.1 augustss {
196 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
197 1.1 augustss }
198 1.1 augustss
199 1.1 augustss void
200 1.1 augustss sf2r_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
201 1.1 augustss {
202 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
203 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_UNMUTE);
204 1.1 augustss }
205 1.1 augustss
206 1.1 augustss int
207 1.1 augustss sf2r_find(bus_space_tag_t iot, bus_space_handle_t ioh)
208 1.1 augustss {
209 1.1 augustss struct sf2r_softc sc;
210 1.1 augustss u_int32_t freq;
211 1.1 augustss
212 1.1 augustss sc.tea.iot = iot;
213 1.1 augustss sc.tea.ioh = ioh;
214 1.1 augustss sc.tea.offset = 0;
215 1.1 augustss sc.tea.init = sf2r_init;
216 1.1 augustss sc.tea.rset = sf2r_rset;
217 1.1 augustss sc.tea.write_bit = sf2r_write_bit;
218 1.1 augustss sc.tea.read = sf2r_read_register;
219 1.1 augustss sc.lock = TEA5757_S030;
220 1.1 augustss sc.stereo = TEA5757_STEREO;
221 1.1 augustss
222 1.1 augustss if ((bus_space_read_1(iot, ioh, 0) & 0x70) == 0x30) {
223 1.1 augustss /*
224 1.1 augustss * Let's try to write and read a frequency.
225 1.1 augustss * If the written and read frequencies are
226 1.1 augustss * the same then success.
227 1.1 augustss */
228 1.1 augustss sc.freq = MIN_FM_FREQ;
229 1.1 augustss tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
230 1.1 augustss sf2r_set_mute(&sc);
231 1.1 augustss freq = sf2r_read_register(iot, ioh, sc.tea.offset);
232 1.1 augustss if (tea5757_decode_freq(freq) == sc.freq)
233 1.1 augustss return 1;
234 1.1 augustss }
235 1.1 augustss
236 1.1 augustss return 0;
237 1.1 augustss }
238 1.1 augustss
239 1.1 augustss void
240 1.1 augustss sf2r_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
241 1.1 augustss {
242 1.1 augustss u_int8_t data;
243 1.1 augustss
244 1.1 augustss data = bit ? SF16FMR2_DATA_ON : SF16FMR2_DATA_OFF;
245 1.1 augustss
246 1.1 augustss bus_space_write_1(iot, ioh, off,
247 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
248 1.1 augustss bus_space_write_1(iot, ioh, off,
249 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_ON | data);
250 1.1 augustss bus_space_write_1(iot, ioh, off,
251 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
252 1.1 augustss }
253 1.1 augustss
254 1.1 augustss u_int32_t
255 1.1 augustss sf2r_read_register(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
256 1.1 augustss {
257 1.1 augustss u_int32_t res = 0;
258 1.1 augustss u_int8_t i, state = 0;
259 1.1 augustss
260 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
261 1.1 augustss DELAY(6);
262 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
263 1.1 augustss
264 1.1 augustss i = bus_space_read_1(iot, ioh, off);
265 1.1 augustss DELAY(6);
266 1.1 augustss /* Amplifier: 0 - not present, 1 - present */
267 1.1 augustss state = i & SF16FMR2_AMPLIFIER ? (1 << 2) : (0 << 2);
268 1.1 augustss /* Signal: 0 - not tuned, 1 - tuned */
269 1.1 augustss state |= i & SF16FMR2_SIGNAL ? (0 << 1) : (1 << 1);
270 1.1 augustss
271 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
272 1.1 augustss i = bus_space_read_1(iot, ioh, off);
273 1.1 augustss /* Stereo: 0 - mono, 1 - stereo */
274 1.1 augustss state |= i & SF16FMR2_STEREO ? (0 << 0) : (1 << 0);
275 1.1 augustss res = i & SF16FMR2_DATA_ON;
276 1.1 augustss
277 1.1 augustss i = 23;
278 1.1 augustss while ( i-- ) {
279 1.1 augustss DELAY(6);
280 1.1 augustss res <<= 1;
281 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
282 1.1 augustss DELAY(6);
283 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
284 1.1 augustss res |= bus_space_read_1(iot, ioh, off) & SF16FMR2_DATA_ON;
285 1.1 augustss }
286 1.1 augustss
287 1.1 augustss return res | (state << 24);
288 1.1 augustss }
289 1.1 augustss
290 1.1 augustss int
291 1.1 augustss sf2r_get_info(void *v, struct radio_info *ri)
292 1.1 augustss {
293 1.1 augustss struct sf2r_softc *sc = v;
294 1.1 augustss u_int32_t buf;
295 1.1 augustss
296 1.1 augustss ri->mute = sc->mute;
297 1.1 augustss ri->volume = sc->vol ? 255 : 0;
298 1.1 augustss ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
299 1.1 augustss ri->caps = SF16FMR2_CAPABILITIES;
300 1.1 augustss ri->rfreq = 0;
301 1.1 augustss ri->lock = tea5757_decode_lock(sc->lock);
302 1.1 augustss
303 1.1 augustss buf = sf2r_read_register(sc->tea.iot, sc->tea.ioh, sc->tea.offset);
304 1.1 augustss ri->freq = sc->freq = tea5757_decode_freq(buf);
305 1.1 augustss ri->info = 3 & (buf >> 24);
306 1.1 augustss
307 1.1 augustss return (0);
308 1.1 augustss }
309 1.1 augustss
310 1.1 augustss int
311 1.1 augustss sf2r_set_info(void *v, struct radio_info *ri)
312 1.1 augustss {
313 1.1 augustss struct sf2r_softc *sc = v;
314 1.1 augustss
315 1.1 augustss sc->mute = ri->mute ? 1 : 0;
316 1.1 augustss sc->vol = ri->volume ? 255 : 0;
317 1.1 augustss sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
318 1.1 augustss sc->lock = tea5757_encode_lock(ri->lock);
319 1.1 augustss ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
320 1.1 augustss sc->lock, sc->stereo, ri->freq);
321 1.1 augustss sf2r_set_mute(sc);
322 1.1 augustss
323 1.1 augustss return (0);
324 1.1 augustss }
325 1.1 augustss
326 1.1 augustss int
327 1.1 augustss sf2r_search(void *v, int f)
328 1.1 augustss {
329 1.1 augustss struct sf2r_softc *sc = v;
330 1.1 augustss
331 1.1 augustss tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
332 1.1 augustss sf2r_set_mute(sc);
333 1.1 augustss
334 1.1 augustss return (0);
335 1.1 augustss }
336