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