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