sf16fmr2.c revision 1.9 1 1.9 lukem /* $NetBSD: sf16fmr2.c,v 1.9 2003/07/14 15:47:16 lukem 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.9 lukem
38 1.9 lukem #include <sys/cdefs.h>
39 1.9 lukem __KERNEL_RCSID(0, "$NetBSD: sf16fmr2.c,v 1.9 2003/07/14 15:47:16 lukem Exp $");
40 1.1 augustss
41 1.1 augustss #include <sys/param.h>
42 1.1 augustss #include <sys/systm.h>
43 1.1 augustss #include <sys/proc.h>
44 1.1 augustss #include <sys/errno.h>
45 1.1 augustss #include <sys/ioctl.h>
46 1.1 augustss #include <sys/device.h>
47 1.1 augustss #include <sys/radioio.h>
48 1.1 augustss
49 1.1 augustss #include <dev/isa/isavar.h>
50 1.1 augustss #include <dev/radio_if.h>
51 1.1 augustss #include <dev/ic/tea5757.h>
52 1.1 augustss
53 1.1 augustss #define SF16FMR2_BASE_VALID(x) (x == 0x384)
54 1.1 augustss #define SF16FMR2_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
55 1.1 augustss RADIO_CAPS_DETECT_SIGNAL | \
56 1.1 augustss RADIO_CAPS_SET_MONO | \
57 1.1 augustss RADIO_CAPS_LOCK_SENSITIVITY | \
58 1.1 augustss RADIO_CAPS_HW_AFC | \
59 1.1 augustss RADIO_CAPS_HW_SEARCH
60 1.1 augustss
61 1.1 augustss #define SF16FMR2_AMPLIFIER (1 << 7)
62 1.1 augustss #define SF16FMR2_SIGNAL (1 << 3)
63 1.1 augustss #define SF16FMR2_STEREO (1 << 3)
64 1.1 augustss
65 1.1 augustss #define SF16FMR2_MUTE 0x00
66 1.1 augustss #define SF16FMR2_UNMUTE 0x04
67 1.1 augustss
68 1.1 augustss #define SF16FMR2_DATA_ON (1 << 0)
69 1.1 augustss #define SF16FMR2_DATA_OFF (0 << 0)
70 1.1 augustss
71 1.1 augustss #define SF16FMR2_CLCK_ON (1 << 1)
72 1.1 augustss #define SF16FMR2_CLCK_OFF (0 << 1)
73 1.1 augustss
74 1.1 augustss #define SF16FMR2_WREN_ON (0 << 2) /* SF16-FMR2 has inverse WREN */
75 1.1 augustss #define SF16FMR2_WREN_OFF (1 << 2)
76 1.1 augustss
77 1.1 augustss #define SF16FMR2_READ_CLOCK_LOW \
78 1.1 augustss SF16FMR2_DATA_ON | SF16FMR2_CLCK_OFF | SF16FMR2_WREN_OFF
79 1.1 augustss
80 1.1 augustss #define SF16FMR2_READ_CLOCK_HIGH \
81 1.1 augustss SF16FMR2_DATA_ON | SF16FMR2_CLCK_ON | SF16FMR2_WREN_OFF
82 1.1 augustss
83 1.1 augustss int sf2r_probe(struct device *, struct cfdata *, void *);
84 1.1 augustss void sf2r_attach(struct device *, struct device * self, void *);
85 1.1 augustss
86 1.1 augustss int sf2r_get_info(void *, struct radio_info *);
87 1.1 augustss int sf2r_set_info(void *, struct radio_info *);
88 1.1 augustss int sf2r_search(void *, int);
89 1.1 augustss
90 1.1 augustss /* define our interface to the higher level radio driver */
91 1.1 augustss struct radio_hw_if sf2r_hw_if = {
92 1.1 augustss NULL, /* open */
93 1.1 augustss NULL, /* close */
94 1.1 augustss sf2r_get_info,
95 1.1 augustss sf2r_set_info,
96 1.1 augustss sf2r_search
97 1.1 augustss };
98 1.1 augustss
99 1.1 augustss struct sf2r_softc {
100 1.1 augustss struct device sc_dev;
101 1.1 augustss
102 1.1 augustss u_int32_t freq;
103 1.1 augustss u_int32_t stereo;
104 1.1 augustss u_int32_t lock;
105 1.1 augustss u_int8_t vol;
106 1.1 augustss int mute;
107 1.1 augustss
108 1.1 augustss struct tea5757_t tea;
109 1.1 augustss };
110 1.1 augustss
111 1.7 thorpej CFATTACH_DECL(sf2r, sizeof(struct sf2r_softc),
112 1.8 thorpej sf2r_probe, sf2r_attach, NULL, NULL);
113 1.1 augustss
114 1.1 augustss void sf2r_set_mute(struct sf2r_softc *);
115 1.1 augustss int sf2r_find(bus_space_tag_t, bus_space_handle_t);
116 1.1 augustss
117 1.1 augustss u_int32_t sf2r_read_register(bus_space_tag_t, bus_space_handle_t, bus_size_t);
118 1.1 augustss
119 1.1 augustss void sf2r_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
120 1.1 augustss void sf2r_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
121 1.1 augustss void sf2r_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
122 1.1 augustss
123 1.1 augustss int
124 1.1 augustss sf2r_probe(struct device *parent, struct cfdata *cf, void *aux)
125 1.1 augustss {
126 1.1 augustss struct isa_attach_args *ia = aux;
127 1.1 augustss bus_space_tag_t iot = ia->ia_iot;
128 1.1 augustss bus_space_handle_t ioh;
129 1.2 augustss u_int r;
130 1.4 thorpej int iosize = 1, iobase;
131 1.4 thorpej
132 1.4 thorpej if (ISA_DIRECT_CONFIG(ia))
133 1.4 thorpej return 0;
134 1.4 thorpej
135 1.4 thorpej if (ia->ia_nio < 1)
136 1.4 thorpej return 0;
137 1.4 thorpej
138 1.4 thorpej iobase = ia->ia_io[0].ir_addr;
139 1.1 augustss
140 1.1 augustss if (!SF16FMR2_BASE_VALID(iobase)) {
141 1.1 augustss printf("sf2r: configured iobase 0x%x invalid\n", iobase);
142 1.1 augustss return 0;
143 1.1 augustss }
144 1.1 augustss
145 1.1 augustss if (bus_space_map(iot, iobase, iosize, 0, &ioh))
146 1.1 augustss return 0;
147 1.1 augustss
148 1.2 augustss r = sf2r_find(iot, ioh);
149 1.2 augustss
150 1.1 augustss bus_space_unmap(iot, ioh, iosize);
151 1.1 augustss
152 1.4 thorpej if (r != 0) {
153 1.4 thorpej ia->ia_nio = 1;
154 1.4 thorpej ia->ia_io[0].ir_size = iosize;
155 1.4 thorpej
156 1.4 thorpej ia->ia_niomem = 0;
157 1.4 thorpej ia->ia_nirq = 0;
158 1.4 thorpej ia->ia_ndrq = 0;
159 1.4 thorpej
160 1.4 thorpej return (1);
161 1.4 thorpej }
162 1.1 augustss
163 1.4 thorpej return (0);
164 1.1 augustss }
165 1.1 augustss
166 1.1 augustss void
167 1.1 augustss sf2r_attach(struct device *parent, struct device *self, void *aux)
168 1.1 augustss {
169 1.1 augustss struct sf2r_softc *sc = (void *) self;
170 1.1 augustss struct isa_attach_args *ia = aux;
171 1.1 augustss
172 1.1 augustss sc->tea.iot = ia->ia_iot;
173 1.5 augustss sc->tea.flags = 0;
174 1.1 augustss sc->mute = 0;
175 1.1 augustss sc->vol = 0;
176 1.1 augustss sc->freq = MIN_FM_FREQ;
177 1.1 augustss sc->stereo = TEA5757_STEREO;
178 1.1 augustss sc->lock = TEA5757_S030;
179 1.1 augustss
180 1.1 augustss /* remap I/O */
181 1.4 thorpej if (bus_space_map(sc->tea.iot, ia->ia_io[0].ir_addr,
182 1.4 thorpej ia->ia_io[0].ir_size, 0, &sc->tea.ioh))
183 1.1 augustss panic("sf2rattach: bus_space_map() failed");
184 1.1 augustss
185 1.1 augustss sc->tea.offset = 0;
186 1.1 augustss
187 1.1 augustss sc->tea.init = sf2r_init;
188 1.1 augustss sc->tea.rset = sf2r_rset;
189 1.1 augustss sc->tea.write_bit = sf2r_write_bit;
190 1.1 augustss sc->tea.read = sf2r_read_register;
191 1.1 augustss
192 1.3 augustss printf(": SoundForte RadioLink SF16-FMR2\n");
193 1.1 augustss tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
194 1.1 augustss sf2r_set_mute(sc);
195 1.1 augustss
196 1.1 augustss radio_attach_mi(&sf2r_hw_if, sc, &sc->sc_dev);
197 1.1 augustss }
198 1.1 augustss
199 1.1 augustss /*
200 1.1 augustss * Mute/unmute the card
201 1.1 augustss */
202 1.1 augustss void
203 1.1 augustss sf2r_set_mute(struct sf2r_softc *sc)
204 1.1 augustss {
205 1.1 augustss u_int8_t mute;
206 1.1 augustss
207 1.1 augustss mute = (sc->mute || !sc->vol) ? SF16FMR2_MUTE : SF16FMR2_UNMUTE;
208 1.1 augustss bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
209 1.1 augustss DELAY(64);
210 1.1 augustss bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
211 1.1 augustss }
212 1.1 augustss
213 1.1 augustss void
214 1.1 augustss sf2r_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
215 1.1 augustss {
216 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
217 1.1 augustss }
218 1.1 augustss
219 1.1 augustss void
220 1.1 augustss sf2r_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
221 1.1 augustss {
222 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
223 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_UNMUTE);
224 1.1 augustss }
225 1.1 augustss
226 1.1 augustss int
227 1.1 augustss sf2r_find(bus_space_tag_t iot, bus_space_handle_t ioh)
228 1.1 augustss {
229 1.1 augustss struct sf2r_softc sc;
230 1.1 augustss u_int32_t freq;
231 1.1 augustss
232 1.1 augustss sc.tea.iot = iot;
233 1.1 augustss sc.tea.ioh = ioh;
234 1.1 augustss sc.tea.offset = 0;
235 1.1 augustss sc.tea.init = sf2r_init;
236 1.1 augustss sc.tea.rset = sf2r_rset;
237 1.1 augustss sc.tea.write_bit = sf2r_write_bit;
238 1.1 augustss sc.tea.read = sf2r_read_register;
239 1.1 augustss sc.lock = TEA5757_S030;
240 1.1 augustss sc.stereo = TEA5757_STEREO;
241 1.1 augustss
242 1.1 augustss if ((bus_space_read_1(iot, ioh, 0) & 0x70) == 0x30) {
243 1.1 augustss /*
244 1.1 augustss * Let's try to write and read a frequency.
245 1.1 augustss * If the written and read frequencies are
246 1.1 augustss * the same then success.
247 1.1 augustss */
248 1.1 augustss sc.freq = MIN_FM_FREQ;
249 1.1 augustss tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
250 1.1 augustss sf2r_set_mute(&sc);
251 1.1 augustss freq = sf2r_read_register(iot, ioh, sc.tea.offset);
252 1.8 thorpej if (tea5757_decode_freq(freq, 0) == sc.freq)
253 1.1 augustss return 1;
254 1.1 augustss }
255 1.1 augustss
256 1.1 augustss return 0;
257 1.1 augustss }
258 1.1 augustss
259 1.1 augustss void
260 1.1 augustss sf2r_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
261 1.1 augustss {
262 1.1 augustss u_int8_t data;
263 1.1 augustss
264 1.1 augustss data = bit ? SF16FMR2_DATA_ON : SF16FMR2_DATA_OFF;
265 1.1 augustss
266 1.1 augustss bus_space_write_1(iot, ioh, off,
267 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
268 1.1 augustss bus_space_write_1(iot, ioh, off,
269 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_ON | data);
270 1.1 augustss bus_space_write_1(iot, ioh, off,
271 1.1 augustss SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
272 1.1 augustss }
273 1.1 augustss
274 1.1 augustss u_int32_t
275 1.1 augustss sf2r_read_register(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
276 1.1 augustss {
277 1.1 augustss u_int32_t res = 0;
278 1.1 augustss u_int8_t i, state = 0;
279 1.1 augustss
280 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
281 1.1 augustss DELAY(6);
282 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
283 1.1 augustss
284 1.1 augustss i = bus_space_read_1(iot, ioh, off);
285 1.1 augustss DELAY(6);
286 1.1 augustss /* Amplifier: 0 - not present, 1 - present */
287 1.1 augustss state = i & SF16FMR2_AMPLIFIER ? (1 << 2) : (0 << 2);
288 1.1 augustss /* Signal: 0 - not tuned, 1 - tuned */
289 1.1 augustss state |= i & SF16FMR2_SIGNAL ? (0 << 1) : (1 << 1);
290 1.1 augustss
291 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
292 1.1 augustss i = bus_space_read_1(iot, ioh, off);
293 1.1 augustss /* Stereo: 0 - mono, 1 - stereo */
294 1.1 augustss state |= i & SF16FMR2_STEREO ? (0 << 0) : (1 << 0);
295 1.1 augustss res = i & SF16FMR2_DATA_ON;
296 1.1 augustss
297 1.1 augustss i = 23;
298 1.1 augustss while ( i-- ) {
299 1.1 augustss DELAY(6);
300 1.1 augustss res <<= 1;
301 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
302 1.1 augustss DELAY(6);
303 1.1 augustss bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
304 1.1 augustss res |= bus_space_read_1(iot, ioh, off) & SF16FMR2_DATA_ON;
305 1.1 augustss }
306 1.1 augustss
307 1.1 augustss return res | (state << 24);
308 1.1 augustss }
309 1.1 augustss
310 1.1 augustss int
311 1.1 augustss sf2r_get_info(void *v, struct radio_info *ri)
312 1.1 augustss {
313 1.1 augustss struct sf2r_softc *sc = v;
314 1.1 augustss u_int32_t buf;
315 1.1 augustss
316 1.1 augustss ri->mute = sc->mute;
317 1.1 augustss ri->volume = sc->vol ? 255 : 0;
318 1.1 augustss ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
319 1.1 augustss ri->caps = SF16FMR2_CAPABILITIES;
320 1.1 augustss ri->rfreq = 0;
321 1.1 augustss ri->lock = tea5757_decode_lock(sc->lock);
322 1.1 augustss
323 1.1 augustss buf = sf2r_read_register(sc->tea.iot, sc->tea.ioh, sc->tea.offset);
324 1.8 thorpej ri->freq = sc->freq = tea5757_decode_freq(buf, 0);
325 1.1 augustss ri->info = 3 & (buf >> 24);
326 1.1 augustss
327 1.1 augustss return (0);
328 1.1 augustss }
329 1.1 augustss
330 1.1 augustss int
331 1.1 augustss sf2r_set_info(void *v, struct radio_info *ri)
332 1.1 augustss {
333 1.1 augustss struct sf2r_softc *sc = v;
334 1.1 augustss
335 1.1 augustss sc->mute = ri->mute ? 1 : 0;
336 1.1 augustss sc->vol = ri->volume ? 255 : 0;
337 1.1 augustss sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
338 1.1 augustss sc->lock = tea5757_encode_lock(ri->lock);
339 1.1 augustss ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
340 1.1 augustss sc->lock, sc->stereo, ri->freq);
341 1.1 augustss sf2r_set_mute(sc);
342 1.1 augustss
343 1.1 augustss return (0);
344 1.1 augustss }
345 1.1 augustss
346 1.1 augustss int
347 1.1 augustss sf2r_search(void *v, int f)
348 1.1 augustss {
349 1.1 augustss struct sf2r_softc *sc = v;
350 1.1 augustss
351 1.1 augustss tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
352 1.1 augustss sf2r_set_mute(sc);
353 1.1 augustss
354 1.1 augustss return (0);
355 1.1 augustss }
356