radiotrack.c revision 1.9 1 1.9 lukem /* $NetBSD: radiotrack.c,v 1.9 2003/07/14 15:47:16 lukem Exp $ */
2 1.1 augustss /* $OpenBSD: radiotrack.c,v 1.1 2001/12/05 10:27:06 mickey Exp $ */
3 1.1 augustss /* $RuOBSD: radiotrack.c,v 1.3 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 /* AIMS Lab Radiotrack FM Radio Card device driver */
32 1.1 augustss
33 1.1 augustss /*
34 1.1 augustss * Sanyo LM7000 Direct PLL Frequency Synthesizer
35 1.1 augustss */
36 1.9 lukem
37 1.9 lukem #include <sys/cdefs.h>
38 1.9 lukem __KERNEL_RCSID(0, "$NetBSD: radiotrack.c,v 1.9 2003/07/14 15:47:16 lukem Exp $");
39 1.1 augustss
40 1.1 augustss #include <sys/param.h>
41 1.1 augustss #include <sys/systm.h>
42 1.1 augustss #include <sys/proc.h>
43 1.1 augustss #include <sys/errno.h>
44 1.1 augustss #include <sys/ioctl.h>
45 1.1 augustss #include <sys/device.h>
46 1.1 augustss #include <sys/radioio.h>
47 1.1 augustss
48 1.1 augustss #include <machine/bus.h>
49 1.1 augustss
50 1.1 augustss #include <dev/isa/isavar.h>
51 1.1 augustss #include <dev/ic/lm700x.h>
52 1.1 augustss #include <dev/radio_if.h>
53 1.1 augustss
54 1.1 augustss #define RF_25K 25
55 1.1 augustss #define RF_50K 50
56 1.1 augustss #define RF_100K 100
57 1.1 augustss
58 1.1 augustss #define MAX_VOL 5 /* XXX Find real value */
59 1.1 augustss #define VOLUME_RATIO(x) (255 * x / MAX_VOL)
60 1.1 augustss
61 1.1 augustss #define RT_BASE_VALID(x) \
62 1.1 augustss ((x == 0x30C) || (x == 0x20C) || (x == 0x284) || (x == 0x384))
63 1.1 augustss
64 1.1 augustss #define CARD_RADIOTRACK 0x01
65 1.1 augustss #define CARD_SF16FMI 0x02
66 1.1 augustss #define CARD_UNKNOWN 0xFF
67 1.1 augustss
68 1.1 augustss #define RTRACK_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
69 1.1 augustss RADIO_CAPS_DETECT_SIGNAL | \
70 1.1 augustss RADIO_CAPS_SET_MONO | \
71 1.1 augustss RADIO_CAPS_REFERENCE_FREQ
72 1.1 augustss
73 1.1 augustss #define RT_WREN_ON (1 << 0)
74 1.1 augustss #define RT_WREN_OFF (0 << 0)
75 1.1 augustss
76 1.1 augustss #define RT_CLCK_ON (1 << 1)
77 1.1 augustss #define RT_CLCK_OFF (0 << 1)
78 1.1 augustss
79 1.1 augustss #define RT_DATA_ON (1 << 2)
80 1.1 augustss #define RT_DATA_OFF (0 << 2)
81 1.1 augustss
82 1.1 augustss #define RT_CARD_ON (1 << 3)
83 1.1 augustss #define RT_CARD_OFF (0 << 3)
84 1.1 augustss
85 1.1 augustss #define RT_SIGNAL_METER (1 << 4)
86 1.1 augustss #define RT_SIGNAL_METER_DELAY 150000
87 1.1 augustss
88 1.1 augustss #define RT_VOLUME_DOWN (1 << 6)
89 1.1 augustss #define RT_VOLUME_UP (2 << 6)
90 1.1 augustss #define RT_VOLUME_STEADY (3 << 6)
91 1.1 augustss #define RT_VOLUME_DELAY 100000
92 1.1 augustss
93 1.1 augustss int rt_probe(struct device *, struct cfdata *, void *);
94 1.1 augustss void rt_attach(struct device *, struct device * self, void *);
95 1.1 augustss int rt_get_info(void *, struct radio_info *);
96 1.1 augustss int rt_set_info(void *, struct radio_info *);
97 1.1 augustss
98 1.1 augustss struct radio_hw_if rt_hw_if = {
99 1.1 augustss NULL, /* open */
100 1.1 augustss NULL, /* close */
101 1.1 augustss rt_get_info,
102 1.1 augustss rt_set_info,
103 1.1 augustss NULL
104 1.1 augustss };
105 1.1 augustss
106 1.1 augustss struct rt_softc {
107 1.1 augustss struct device sc_dev;
108 1.1 augustss
109 1.1 augustss int mute;
110 1.1 augustss u_int8_t vol;
111 1.1 augustss u_int8_t cardtype;
112 1.1 augustss u_int32_t freq;
113 1.1 augustss u_int32_t rf;
114 1.1 augustss u_int32_t stereo;
115 1.1 augustss
116 1.1 augustss struct lm700x_t lm;
117 1.1 augustss };
118 1.1 augustss
119 1.7 thorpej CFATTACH_DECL(rt, sizeof(struct rt_softc),
120 1.8 thorpej rt_probe, rt_attach, NULL, NULL);
121 1.1 augustss
122 1.1 augustss int rt_find(bus_space_tag_t, bus_space_handle_t);
123 1.1 augustss void rt_set_mute(struct rt_softc *, int);
124 1.1 augustss void rt_set_freq(struct rt_softc *, u_int32_t);
125 1.1 augustss u_int8_t rt_state(bus_space_tag_t, bus_space_handle_t);
126 1.1 augustss
127 1.1 augustss void rt_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
128 1.1 augustss void rt_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
129 1.1 augustss
130 1.1 augustss u_int8_t rt_conv_vol(u_int8_t);
131 1.1 augustss u_int8_t rt_unconv_vol(u_int8_t);
132 1.1 augustss
133 1.1 augustss int
134 1.1 augustss rt_probe(struct device *parent, struct cfdata *cf, void *aux)
135 1.1 augustss {
136 1.1 augustss struct isa_attach_args *ia = aux;
137 1.1 augustss bus_space_tag_t iot = ia->ia_iot;
138 1.1 augustss bus_space_handle_t ioh;
139 1.3 augustss u_int r;
140 1.5 thorpej int iosize = 1, iobase;
141 1.5 thorpej
142 1.5 thorpej if (ISA_DIRECT_CONFIG(ia))
143 1.5 thorpej return 0;
144 1.5 thorpej
145 1.5 thorpej if (ia->ia_nio < 1)
146 1.5 thorpej return (0);
147 1.5 thorpej
148 1.5 thorpej iobase = ia->ia_io[0].ir_addr;
149 1.1 augustss
150 1.1 augustss if (!RT_BASE_VALID(iobase)) {
151 1.2 augustss printf("rt: configured iobase 0x%x invalid\n", iobase);
152 1.1 augustss return 0;
153 1.1 augustss }
154 1.1 augustss
155 1.1 augustss if (bus_space_map(iot, iobase, iosize, 0, &ioh))
156 1.1 augustss return 0;
157 1.1 augustss
158 1.3 augustss r = rt_find(iot, ioh);
159 1.3 augustss
160 1.1 augustss bus_space_unmap(iot, ioh, iosize);
161 1.1 augustss
162 1.5 thorpej if (r != 0) {
163 1.5 thorpej ia->ia_nio = 1;
164 1.5 thorpej ia->ia_io[0].ir_size = iosize;
165 1.5 thorpej
166 1.5 thorpej ia->ia_niomem = 0;
167 1.5 thorpej ia->ia_nirq = 0;
168 1.5 thorpej ia->ia_ndrq = 0;
169 1.5 thorpej
170 1.5 thorpej return (1);
171 1.5 thorpej }
172 1.1 augustss
173 1.5 thorpej return (0);
174 1.1 augustss }
175 1.1 augustss
176 1.1 augustss void
177 1.1 augustss rt_attach(struct device *parent, struct device *self, void *aux)
178 1.1 augustss {
179 1.1 augustss struct rt_softc *sc = (void *) self;
180 1.1 augustss struct isa_attach_args *ia = aux;
181 1.1 augustss
182 1.1 augustss sc->lm.iot = ia->ia_iot;
183 1.1 augustss sc->rf = LM700X_REF_050;
184 1.1 augustss sc->stereo = LM700X_STEREO;
185 1.1 augustss sc->mute = 0;
186 1.1 augustss sc->freq = MIN_FM_FREQ;
187 1.1 augustss sc->vol = 0;
188 1.1 augustss
189 1.1 augustss /* remap I/O */
190 1.5 thorpej if (bus_space_map(sc->lm.iot, ia->ia_io[0].ir_addr,
191 1.5 thorpej ia->ia_io[0].ir_size, 0, &sc->lm.ioh))
192 1.1 augustss panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
193 1.1 augustss
194 1.5 thorpej switch (ia->ia_io[0].ir_addr) {
195 1.1 augustss case 0x20C:
196 1.1 augustss /* FALLTHROUGH */
197 1.1 augustss case 0x30C:
198 1.1 augustss sc->cardtype = CARD_RADIOTRACK;
199 1.4 augustss printf(": AIMS Lab Radiotrack or compatible\n");
200 1.1 augustss break;
201 1.1 augustss case 0x284:
202 1.1 augustss /* FALLTHROUGH */
203 1.1 augustss case 0x384:
204 1.1 augustss sc->cardtype = CARD_SF16FMI;
205 1.4 augustss printf(": SoundForte RadioX SF16-FMI\n");
206 1.1 augustss break;
207 1.1 augustss default:
208 1.1 augustss sc->cardtype = CARD_UNKNOWN;
209 1.4 augustss printf(": Unknown card\n");
210 1.1 augustss break;
211 1.1 augustss }
212 1.1 augustss
213 1.1 augustss /* Configure struct lm700x_t lm */
214 1.1 augustss sc->lm.offset = 0;
215 1.1 augustss sc->lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF;
216 1.1 augustss sc->lm.wzch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_OFF;
217 1.1 augustss sc->lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON;
218 1.1 augustss sc->lm.woch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_ON;
219 1.1 augustss sc->lm.initdata = 0;
220 1.1 augustss sc->lm.rsetdata = RT_DATA_ON | RT_CLCK_ON | RT_WREN_OFF;
221 1.1 augustss sc->lm.init = rt_lm700x_init;
222 1.1 augustss sc->lm.rset = rt_lm700x_rset;
223 1.1 augustss
224 1.1 augustss rt_set_freq(sc, sc->freq);
225 1.1 augustss
226 1.1 augustss radio_attach_mi(&rt_hw_if, sc, &sc->sc_dev);
227 1.1 augustss }
228 1.1 augustss
229 1.1 augustss /*
230 1.1 augustss * Mute the card
231 1.1 augustss */
232 1.1 augustss void
233 1.1 augustss rt_set_mute(struct rt_softc *sc, int vol)
234 1.1 augustss {
235 1.1 augustss int val;
236 1.1 augustss
237 1.1 augustss if (sc->mute) {
238 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
239 1.1 augustss RT_VOLUME_DOWN | RT_CARD_ON);
240 1.1 augustss DELAY(MAX_VOL * RT_VOLUME_DELAY);
241 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
242 1.1 augustss RT_VOLUME_STEADY | RT_CARD_ON);
243 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF);
244 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF);
245 1.1 augustss } else {
246 1.1 augustss val = sc->vol - vol;
247 1.1 augustss if (val < 0) {
248 1.1 augustss val *= -1;
249 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
250 1.1 augustss RT_VOLUME_DOWN | RT_CARD_ON);
251 1.1 augustss } else {
252 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
253 1.1 augustss RT_VOLUME_UP | RT_CARD_ON);
254 1.1 augustss }
255 1.1 augustss DELAY(val * RT_VOLUME_DELAY);
256 1.1 augustss bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
257 1.1 augustss RT_VOLUME_STEADY | RT_CARD_ON);
258 1.1 augustss }
259 1.1 augustss }
260 1.1 augustss
261 1.1 augustss void
262 1.1 augustss rt_set_freq(struct rt_softc *sc, u_int32_t nfreq)
263 1.1 augustss {
264 1.1 augustss u_int32_t reg;
265 1.1 augustss
266 1.1 augustss if (nfreq > MAX_FM_FREQ)
267 1.1 augustss nfreq = MAX_FM_FREQ;
268 1.1 augustss if (nfreq < MIN_FM_FREQ)
269 1.1 augustss nfreq = MIN_FM_FREQ;
270 1.1 augustss
271 1.1 augustss sc->freq = nfreq;
272 1.1 augustss
273 1.1 augustss reg = lm700x_encode_freq(nfreq, sc->rf);
274 1.1 augustss reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
275 1.1 augustss
276 1.1 augustss lm700x_hardware_write(&sc->lm, reg, RT_VOLUME_STEADY);
277 1.1 augustss
278 1.1 augustss rt_set_mute(sc, sc->vol);
279 1.1 augustss }
280 1.1 augustss
281 1.1 augustss /*
282 1.1 augustss * Return state of the card - tuned/not tuned, mono/stereo
283 1.1 augustss */
284 1.1 augustss u_int8_t
285 1.1 augustss rt_state(bus_space_tag_t iot, bus_space_handle_t ioh)
286 1.1 augustss {
287 1.1 augustss u_int8_t ret;
288 1.1 augustss
289 1.1 augustss bus_space_write_1(iot, ioh, 0,
290 1.1 augustss RT_VOLUME_STEADY | RT_SIGNAL_METER | RT_CARD_ON);
291 1.1 augustss DELAY(RT_SIGNAL_METER_DELAY);
292 1.1 augustss ret = bus_space_read_1(iot, ioh, 0);
293 1.1 augustss
294 1.1 augustss switch (ret) {
295 1.1 augustss case 0xFD:
296 1.1 augustss ret = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO;
297 1.1 augustss break;
298 1.1 augustss case 0xFF:
299 1.1 augustss ret = 0;
300 1.1 augustss break;
301 1.1 augustss default:
302 1.1 augustss ret = RADIO_INFO_SIGNAL;
303 1.1 augustss break;
304 1.1 augustss }
305 1.1 augustss
306 1.1 augustss return ret;
307 1.1 augustss }
308 1.1 augustss
309 1.1 augustss /*
310 1.1 augustss * Convert volume to hardware representation.
311 1.1 augustss */
312 1.1 augustss u_int8_t
313 1.1 augustss rt_conv_vol(u_int8_t vol)
314 1.1 augustss {
315 1.1 augustss if (vol < VOLUME_RATIO(1))
316 1.1 augustss return 0;
317 1.1 augustss else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
318 1.1 augustss return 1;
319 1.1 augustss else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
320 1.1 augustss return 2;
321 1.1 augustss else if (vol >= VOLUME_RATIO(3) && vol < VOLUME_RATIO(4))
322 1.1 augustss return 3;
323 1.1 augustss else
324 1.1 augustss return 4;
325 1.1 augustss }
326 1.1 augustss
327 1.1 augustss /*
328 1.1 augustss * Convert volume from hardware representation
329 1.1 augustss */
330 1.1 augustss u_int8_t
331 1.1 augustss rt_unconv_vol(u_int8_t vol)
332 1.1 augustss {
333 1.1 augustss return VOLUME_RATIO(vol);
334 1.1 augustss }
335 1.1 augustss
336 1.1 augustss int
337 1.1 augustss rt_find(bus_space_tag_t iot, bus_space_handle_t ioh)
338 1.1 augustss {
339 1.1 augustss struct rt_softc sc;
340 1.1 augustss #if 0
341 1.1 augustss u_int i, scanres = 0;
342 1.1 augustss #endif
343 1.1 augustss
344 1.1 augustss sc.lm.iot = iot;
345 1.1 augustss sc.lm.ioh = ioh;
346 1.1 augustss sc.lm.offset = 0;
347 1.1 augustss sc.lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF;
348 1.1 augustss sc.lm.wzch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_OFF;
349 1.1 augustss sc.lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON;
350 1.1 augustss sc.lm.woch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_ON;
351 1.1 augustss sc.lm.initdata = 0;
352 1.1 augustss sc.lm.rsetdata = RT_SIGNAL_METER;
353 1.1 augustss sc.lm.init = rt_lm700x_init;
354 1.1 augustss sc.lm.rset = rt_lm700x_rset;
355 1.1 augustss sc.rf = LM700X_REF_050;
356 1.1 augustss sc.mute = 0;
357 1.1 augustss sc.stereo = LM700X_STEREO;
358 1.1 augustss sc.vol = 0;
359 1.1 augustss
360 1.1 augustss /*
361 1.1 augustss * Scan whole FM range. If there is a card it'll
362 1.1 augustss * respond on some frequency.
363 1.1 augustss */
364 1.1 augustss return 0;
365 1.1 augustss #if 0
366 1.1 augustss for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
367 1.1 augustss rt_set_freq(&sc, i);
368 1.1 augustss scanres += rt_state(iot, ioh);
369 1.1 augustss }
370 1.1 augustss
371 1.1 augustss return scanres;
372 1.1 augustss #endif
373 1.1 augustss }
374 1.1 augustss
375 1.1 augustss void
376 1.1 augustss rt_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
377 1.1 augustss u_int32_t data)
378 1.1 augustss {
379 1.1 augustss /* Do nothing */
380 1.1 augustss return;
381 1.1 augustss }
382 1.1 augustss
383 1.1 augustss void
384 1.1 augustss rt_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
385 1.1 augustss u_int32_t data)
386 1.1 augustss {
387 1.1 augustss DELAY(1000);
388 1.1 augustss bus_space_write_1(iot, ioh, off, RT_CARD_OFF | data);
389 1.1 augustss DELAY(50000);
390 1.1 augustss bus_space_write_1(iot, ioh, off, RT_VOLUME_STEADY | RT_CARD_ON | data);
391 1.1 augustss }
392 1.1 augustss
393 1.1 augustss int
394 1.1 augustss rt_set_info(void *v, struct radio_info *ri)
395 1.1 augustss {
396 1.1 augustss struct rt_softc *sc = v;
397 1.1 augustss
398 1.1 augustss sc->mute = ri->mute ? 1 : 0;
399 1.1 augustss sc->vol = rt_conv_vol(ri->volume);
400 1.1 augustss sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
401 1.1 augustss sc->rf = lm700x_encode_ref(ri->rfreq);
402 1.1 augustss
403 1.1 augustss rt_set_freq(sc, ri->freq);
404 1.1 augustss rt_set_mute(sc, sc->vol);
405 1.1 augustss
406 1.1 augustss return (0);
407 1.1 augustss }
408 1.1 augustss
409 1.1 augustss int
410 1.1 augustss rt_get_info(void *v, struct radio_info *ri)
411 1.1 augustss {
412 1.1 augustss struct rt_softc *sc = v;
413 1.1 augustss
414 1.1 augustss ri->mute = sc->mute;
415 1.1 augustss ri->volume = rt_unconv_vol(sc->vol);
416 1.1 augustss ri->stereo = sc->stereo == LM700X_STEREO ? 0 : 1;
417 1.1 augustss ri->caps = RTRACK_CAPABILITIES;
418 1.1 augustss ri->rfreq = lm700x_decode_ref(sc->rf);
419 1.1 augustss ri->info = 3 & rt_state(sc->lm.iot, sc->lm.ioh);
420 1.1 augustss ri->freq = sc->freq;
421 1.1 augustss
422 1.1 augustss /* UNSUPPORTED */
423 1.1 augustss ri->lock = 0;
424 1.1 augustss
425 1.1 augustss return (0);
426 1.1 augustss }
427