sf16fmr2.c revision 1.7 1 /* $NetBSD: sf16fmr2.c,v 1.7 2002/10/02 02:09:20 thorpej 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 CFATTACH_DECL(sf2r, sizeof(struct sf2r_softc),
109 sf2r_probe, sf2r_attach, NULL, NULL);
110
111 void sf2r_set_mute(struct sf2r_softc *);
112 int sf2r_find(bus_space_tag_t, bus_space_handle_t);
113
114 u_int32_t sf2r_read_register(bus_space_tag_t, bus_space_handle_t, bus_size_t);
115
116 void sf2r_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
117 void sf2r_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
118 void sf2r_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
119
120 int
121 sf2r_probe(struct device *parent, struct cfdata *cf, void *aux)
122 {
123 struct isa_attach_args *ia = aux;
124 bus_space_tag_t iot = ia->ia_iot;
125 bus_space_handle_t ioh;
126 u_int r;
127 int iosize = 1, iobase;
128
129 if (ISA_DIRECT_CONFIG(ia))
130 return 0;
131
132 if (ia->ia_nio < 1)
133 return 0;
134
135 iobase = ia->ia_io[0].ir_addr;
136
137 if (!SF16FMR2_BASE_VALID(iobase)) {
138 printf("sf2r: configured iobase 0x%x invalid\n", iobase);
139 return 0;
140 }
141
142 if (bus_space_map(iot, iobase, iosize, 0, &ioh))
143 return 0;
144
145 r = sf2r_find(iot, ioh);
146
147 bus_space_unmap(iot, ioh, iosize);
148
149 if (r != 0) {
150 ia->ia_nio = 1;
151 ia->ia_io[0].ir_size = iosize;
152
153 ia->ia_niomem = 0;
154 ia->ia_nirq = 0;
155 ia->ia_ndrq = 0;
156
157 return (1);
158 }
159
160 return (0);
161 }
162
163 void
164 sf2r_attach(struct device *parent, struct device *self, void *aux)
165 {
166 struct sf2r_softc *sc = (void *) self;
167 struct isa_attach_args *ia = aux;
168
169 sc->tea.iot = ia->ia_iot;
170 sc->tea.flags = 0;
171 sc->mute = 0;
172 sc->vol = 0;
173 sc->freq = MIN_FM_FREQ;
174 sc->stereo = TEA5757_STEREO;
175 sc->lock = TEA5757_S030;
176
177 /* remap I/O */
178 if (bus_space_map(sc->tea.iot, ia->ia_io[0].ir_addr,
179 ia->ia_io[0].ir_size, 0, &sc->tea.ioh))
180 panic("sf2rattach: bus_space_map() failed");
181
182 sc->tea.offset = 0;
183
184 sc->tea.init = sf2r_init;
185 sc->tea.rset = sf2r_rset;
186 sc->tea.write_bit = sf2r_write_bit;
187 sc->tea.read = sf2r_read_register;
188
189 printf(": SoundForte RadioLink SF16-FMR2\n");
190 tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
191 sf2r_set_mute(sc);
192
193 radio_attach_mi(&sf2r_hw_if, sc, &sc->sc_dev);
194 }
195
196 /*
197 * Mute/unmute the card
198 */
199 void
200 sf2r_set_mute(struct sf2r_softc *sc)
201 {
202 u_int8_t mute;
203
204 mute = (sc->mute || !sc->vol) ? SF16FMR2_MUTE : SF16FMR2_UNMUTE;
205 bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
206 DELAY(64);
207 bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
208 }
209
210 void
211 sf2r_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
212 {
213 bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
214 }
215
216 void
217 sf2r_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
218 {
219 bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
220 bus_space_write_1(iot, ioh, off, SF16FMR2_UNMUTE);
221 }
222
223 int
224 sf2r_find(bus_space_tag_t iot, bus_space_handle_t ioh)
225 {
226 struct sf2r_softc sc;
227 u_int32_t freq;
228
229 sc.tea.iot = iot;
230 sc.tea.ioh = ioh;
231 sc.tea.offset = 0;
232 sc.tea.init = sf2r_init;
233 sc.tea.rset = sf2r_rset;
234 sc.tea.write_bit = sf2r_write_bit;
235 sc.tea.read = sf2r_read_register;
236 sc.lock = TEA5757_S030;
237 sc.stereo = TEA5757_STEREO;
238
239 if ((bus_space_read_1(iot, ioh, 0) & 0x70) == 0x30) {
240 /*
241 * Let's try to write and read a frequency.
242 * If the written and read frequencies are
243 * the same then success.
244 */
245 sc.freq = MIN_FM_FREQ;
246 tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
247 sf2r_set_mute(&sc);
248 freq = sf2r_read_register(iot, ioh, sc.tea.offset);
249 if (tea5757_decode_freq(freq) == sc.freq)
250 return 1;
251 }
252
253 return 0;
254 }
255
256 void
257 sf2r_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
258 {
259 u_int8_t data;
260
261 data = bit ? SF16FMR2_DATA_ON : SF16FMR2_DATA_OFF;
262
263 bus_space_write_1(iot, ioh, off,
264 SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
265 bus_space_write_1(iot, ioh, off,
266 SF16FMR2_WREN_ON | SF16FMR2_CLCK_ON | data);
267 bus_space_write_1(iot, ioh, off,
268 SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
269 }
270
271 u_int32_t
272 sf2r_read_register(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
273 {
274 u_int32_t res = 0;
275 u_int8_t i, state = 0;
276
277 bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
278 DELAY(6);
279 bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
280
281 i = bus_space_read_1(iot, ioh, off);
282 DELAY(6);
283 /* Amplifier: 0 - not present, 1 - present */
284 state = i & SF16FMR2_AMPLIFIER ? (1 << 2) : (0 << 2);
285 /* Signal: 0 - not tuned, 1 - tuned */
286 state |= i & SF16FMR2_SIGNAL ? (0 << 1) : (1 << 1);
287
288 bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
289 i = bus_space_read_1(iot, ioh, off);
290 /* Stereo: 0 - mono, 1 - stereo */
291 state |= i & SF16FMR2_STEREO ? (0 << 0) : (1 << 0);
292 res = i & SF16FMR2_DATA_ON;
293
294 i = 23;
295 while ( i-- ) {
296 DELAY(6);
297 res <<= 1;
298 bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
299 DELAY(6);
300 bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
301 res |= bus_space_read_1(iot, ioh, off) & SF16FMR2_DATA_ON;
302 }
303
304 return res | (state << 24);
305 }
306
307 int
308 sf2r_get_info(void *v, struct radio_info *ri)
309 {
310 struct sf2r_softc *sc = v;
311 u_int32_t buf;
312
313 ri->mute = sc->mute;
314 ri->volume = sc->vol ? 255 : 0;
315 ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
316 ri->caps = SF16FMR2_CAPABILITIES;
317 ri->rfreq = 0;
318 ri->lock = tea5757_decode_lock(sc->lock);
319
320 buf = sf2r_read_register(sc->tea.iot, sc->tea.ioh, sc->tea.offset);
321 ri->freq = sc->freq = tea5757_decode_freq(buf);
322 ri->info = 3 & (buf >> 24);
323
324 return (0);
325 }
326
327 int
328 sf2r_set_info(void *v, struct radio_info *ri)
329 {
330 struct sf2r_softc *sc = v;
331
332 sc->mute = ri->mute ? 1 : 0;
333 sc->vol = ri->volume ? 255 : 0;
334 sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
335 sc->lock = tea5757_encode_lock(ri->lock);
336 ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
337 sc->lock, sc->stereo, ri->freq);
338 sf2r_set_mute(sc);
339
340 return (0);
341 }
342
343 int
344 sf2r_search(void *v, int f)
345 {
346 struct sf2r_softc *sc = v;
347
348 tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
349 sf2r_set_mute(sc);
350
351 return (0);
352 }
353