xref: /netbsd/sys/dev/isa/radiotrack.c (revision a5a060aa)
1 /* $NetBSD: radiotrack.c,v 1.20 2014/03/23 02:59:19 christos 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/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: radiotrack.c,v 1.20 2014/03/23 02:59:19 christos Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/errno.h>
44 #include <sys/ioctl.h>
45 #include <sys/device.h>
46 #include <sys/radioio.h>
47 
48 #include <sys/bus.h>
49 
50 #include <dev/isa/isavar.h>
51 #include <dev/ic/lm700x.h>
52 #include <dev/radio_if.h>
53 
54 #define RF_25K	25
55 #define RF_50K	50
56 #define RF_100K	100
57 
58 #define MAX_VOL	5	/* XXX Find real value */
59 #define VOLUME_RATIO(x)	(255 * x / MAX_VOL)
60 
61 #define RT_BASE_VALID(x)	\
62 		((x == 0x30C) || (x == 0x20C) || (x == 0x284) || (x == 0x384))
63 
64 #define CARD_RADIOTRACK		0x01
65 #define CARD_SF16FMI		0x02
66 #define CARD_UNKNOWN		0xFF
67 
68 #define RTRACK_CAPABILITIES	RADIO_CAPS_DETECT_STEREO | 		\
69 				RADIO_CAPS_DETECT_SIGNAL | 		\
70 				RADIO_CAPS_SET_MONO | 			\
71 				RADIO_CAPS_REFERENCE_FREQ
72 
73 #define	RT_WREN_ON		(1 << 0)
74 #define	RT_WREN_OFF		(0 << 0)
75 
76 #define RT_CLCK_ON		(1 << 1)
77 #define RT_CLCK_OFF		(0 << 1)
78 
79 #define RT_DATA_ON		(1 << 2)
80 #define RT_DATA_OFF		(0 << 2)
81 
82 #define RT_CARD_ON		(1 << 3)
83 #define RT_CARD_OFF		(0 << 3)
84 
85 #define RT_SIGNAL_METER		(1 << 4)
86 #define RT_SIGNAL_METER_DELAY	150000
87 
88 #define RT_VOLUME_DOWN		(1 << 6)
89 #define RT_VOLUME_UP		(2 << 6)
90 #define RT_VOLUME_STEADY	(3 << 6)
91 #define RT_VOLUME_DELAY		100000
92 
93 int	rt_probe(device_t, cfdata_t, void *);
94 void	rt_attach(device_t, device_t  self, void *);
95 int	rt_get_info(void *, struct radio_info *);
96 int	rt_set_info(void *, struct radio_info *);
97 
98 const struct radio_hw_if rt_hw_if = {
99 	NULL,   /* open */
100 	NULL,   /* close */
101 	rt_get_info,
102 	rt_set_info,
103 	NULL
104 };
105 
106 struct rt_softc {
107 	int		mute;
108 	u_int8_t	vol;
109 	u_int8_t	cardtype;
110 	u_int32_t	freq;
111 	u_int32_t	rf;
112 	u_int32_t	stereo;
113 
114 	struct lm700x_t	lm;
115 };
116 
117 CFATTACH_DECL_NEW(rt, sizeof(struct rt_softc),
118     rt_probe, rt_attach, NULL, NULL);
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
rt_probe(device_t parent,cfdata_t cf,void * aux)132 rt_probe(device_t parent, cfdata_t 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
rt_attach(device_t parent,device_t self,void * aux)175 rt_attach(device_t parent, device_t self, void *aux)
176 {
177 	struct rt_softc *sc = device_private(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", device_xname(self));
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, self);
225 }
226 
227 /*
228  * Mute the card
229  */
230 void
rt_set_mute(struct rt_softc * sc,int vol)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
rt_set_freq(struct rt_softc * sc,u_int32_t nfreq)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
rt_state(bus_space_tag_t iot,bus_space_handle_t ioh)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
rt_conv_vol(u_int8_t vol)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
rt_unconv_vol(u_int8_t vol)329 rt_unconv_vol(u_int8_t vol)
330 {
331 	return VOLUME_RATIO(vol);
332 }
333 
334 int
rt_find(bus_space_tag_t iot,bus_space_handle_t ioh)335 rt_find(bus_space_tag_t iot, bus_space_handle_t ioh)
336 {
337 #ifdef notdef
338 	struct rt_softc sc;
339 	u_int i, scanres = 0;
340 
341 	sc.lm.iot = iot;
342 	sc.lm.ioh = ioh;
343 	sc.lm.offset = 0;
344 	sc.lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF;
345 	sc.lm.wzch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_OFF;
346 	sc.lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON;
347 	sc.lm.woch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_ON;
348 	sc.lm.initdata = 0;
349 	sc.lm.rsetdata = RT_SIGNAL_METER;
350 	sc.lm.init = rt_lm700x_init;
351 	sc.lm.rset = rt_lm700x_rset;
352 	sc.rf = LM700X_REF_050;
353 	sc.mute = 0;
354 	sc.stereo = LM700X_STEREO;
355 	sc.vol = 0;
356 
357 	/*
358 	 * Scan whole FM range. If there is a card it'll
359 	 * respond on some frequency.
360 	 */
361 	for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
362 		rt_set_freq(&sc, i);
363 		scanres += rt_state(iot, ioh);
364 	}
365 
366 	return scanres;
367 #else
368 	return 0;
369 #endif
370 }
371 
372 void
rt_lm700x_init(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t off,u_int32_t data)373 rt_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh,
374     bus_size_t off, u_int32_t data)
375 {
376 	/* Do nothing */
377 	return;
378 }
379 
380 void
rt_lm700x_rset(bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t off,u_int32_t data)381 rt_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
382 		u_int32_t data)
383 {
384 	DELAY(1000);
385 	bus_space_write_1(iot, ioh, off, RT_CARD_OFF | data);
386 	DELAY(50000);
387 	bus_space_write_1(iot, ioh, off, RT_VOLUME_STEADY | RT_CARD_ON | data);
388 }
389 
390 int
rt_set_info(void * v,struct radio_info * ri)391 rt_set_info(void *v, struct radio_info *ri)
392 {
393 	struct rt_softc *sc = v;
394 
395 	sc->mute = ri->mute ? 1 : 0;
396 	sc->vol = rt_conv_vol(ri->volume);
397 	sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
398 	sc->rf = lm700x_encode_ref(ri->rfreq);
399 
400 	rt_set_freq(sc, ri->freq);
401 	rt_set_mute(sc, sc->vol);
402 
403 	return (0);
404 }
405 
406 int
rt_get_info(void * v,struct radio_info * ri)407 rt_get_info(void *v, struct radio_info *ri)
408 {
409 	struct rt_softc *sc = v;
410 
411 	ri->mute = sc->mute;
412 	ri->volume = rt_unconv_vol(sc->vol);
413 	ri->stereo = sc->stereo == LM700X_STEREO ? 0 : 1;
414 	ri->caps = RTRACK_CAPABILITIES;
415 	ri->rfreq = lm700x_decode_ref(sc->rf);
416 	ri->info = 3 & rt_state(sc->lm.iot, sc->lm.ioh);
417 	ri->freq = sc->freq;
418 
419 	/* UNSUPPORTED */
420 	ri->lock = 0;
421 
422 	return (0);
423 }
424