xref: /netbsd/sys/dev/isa/aztech.c (revision bf9ec67e)
1 /* $NetBSD: aztech.c,v 1.4 2002/01/07 21:47:15 thorpej Exp $ */
2 /* $OpenBSD: aztech.c,v 1.2 2001/12/05 10:27:06 mickey Exp $ */
3 /* $RuOBSD: aztech.c,v 1.11 2001/10/20 13:23:47 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 /* Aztech/PackardBell FM Radio Card device driver */
32 
33 /*
34  * Sanyo LM7001J Direct PLL Frequency Synthesizer:
35  *     ??? See http://www.redsword.com/tjacobs/geeb/fmcard.htm
36  *
37  * Philips TEA5712T AM/FM Stereo DTS Radio:
38  *     http://www.semiconductors.philips.com/pip/TEA5712
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/device.h>
47 #include <sys/radioio.h>
48 
49 #include <machine/bus.h>
50 
51 #include <dev/isa/isavar.h>
52 #include <dev/ic/lm700x.h>
53 #include <dev/radio_if.h>
54 
55 #define RF_25K	25
56 #define RF_50K	50
57 #define RF_100K	100
58 
59 #define MAX_VOL	3
60 #define VOLUME_RATIO(x)	(255 * x / MAX_VOL)
61 
62 #define AZ_BASE_VALID(x)	((x == 0x350) || (x == 0x358))
63 #define AZTECH_CAPABILITIES	RADIO_CAPS_DETECT_STEREO | 		\
64 				RADIO_CAPS_DETECT_SIGNAL | 		\
65 				RADIO_CAPS_SET_MONO | 			\
66 				RADIO_CAPS_REFERENCE_FREQ
67 
68 #define	AZ_WREN_ON	(1 << 1)
69 #define	AZ_WREN_OFF	(0 << 1)
70 
71 #define AZ_CLCK_ON	(1 << 6)
72 #define AZ_CLCK_OFF	(0 << 6)
73 
74 #define AZ_DATA_ON	(1 << 7)
75 #define AZ_DATA_OFF	(0 << 7)
76 
77 int	az_probe(struct device *, struct cfdata *, void *);
78 void	az_attach(struct device *, struct device * self, void *);
79 
80 int	az_get_info(void *, struct radio_info *);
81 int	az_set_info(void *, struct radio_info *);
82 
83 struct radio_hw_if az_hw_if = {
84 	NULL,	/* open */
85 	NULL,	/* close */
86 	az_get_info,
87 	az_set_info,
88 	NULL
89 };
90 
91 struct az_softc {
92 	struct device	sc_dev;
93 
94 	int		mute;
95 	u_int8_t	vol;
96 	u_int32_t	freq;
97 	u_int32_t	rf;
98 	u_int32_t	stereo;
99 
100 	struct lm700x_t	lm;
101 };
102 
103 struct cfattach az_ca = {
104 	sizeof(struct az_softc), az_probe, az_attach
105 };
106 
107 u_int	az_find(bus_space_tag_t, bus_space_handle_t);
108 void	az_set_mute(struct az_softc *);
109 void	az_set_freq(struct az_softc *, u_int32_t);
110 u_int8_t	az_state(bus_space_tag_t, bus_space_handle_t);
111 
112 void	az_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
113 void	az_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
114 
115 u_int8_t	az_conv_vol(u_int8_t);
116 u_int8_t	az_unconv_vol(u_int8_t);
117 
118 int
119 az_probe(struct device *parent, struct cfdata *cf, void *aux)
120 {
121 	struct isa_attach_args *ia = aux;
122 	bus_space_tag_t iot = ia->ia_iot;
123 	bus_space_handle_t ioh;
124 	u_int r;
125 	int iosize = 1, iobase;
126 
127 	if (ISA_DIRECT_CONFIG(ia))
128 		return 0;
129 
130 	if (ia->ia_nio < 1)
131 		return 0;
132 
133 	iobase = ia->ia_io[0].ir_addr;
134 
135 	if (!AZ_BASE_VALID(iobase)) {
136 		printf("az: configured iobase 0x%x invalid", iobase);
137 		return 0;
138 	}
139 
140 	if (bus_space_map(iot, iobase, iosize, 0, &ioh))
141 		return 0;
142 
143 	r = az_find(iot, ioh);
144 
145 	bus_space_unmap(iot, ioh, iosize);
146 
147 	if (r != 0) {
148 		ia->ia_nio = 1;
149 		ia->ia_io[0].ir_size = iosize;
150 
151 		ia->ia_niomem = 0;
152 		ia->ia_nirq = 0;
153 		ia->ia_ndrq = 0;
154 
155 		return (1);
156 	}
157 
158 	return (0);
159 }
160 
161 void
162 az_attach(struct device *parent, struct device *self, void *aux)
163 {
164 	struct az_softc *sc = (void *)self;
165 	struct isa_attach_args *ia = aux;
166 
167 	sc->lm.iot = ia->ia_iot;
168 	sc->rf = LM700X_REF_050;
169 	sc->stereo = LM700X_STEREO;
170 	sc->mute = 0;
171 	sc->freq = MIN_FM_FREQ;
172 	sc->vol = 0;
173 
174 	/* remap I/O */
175 	if (bus_space_map(sc->lm.iot, ia->ia_io[0].ir_addr,
176 	    ia->ia_io[0].ir_size, 0, &sc->lm.ioh))
177 		panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
178 
179 	printf(": Aztech/PackardBell\n");
180 
181 	/* Configure struct lm700x_t lm */
182 	sc->lm.offset = 0;
183 	sc->lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
184 	sc->lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
185 	sc->lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
186 	sc->lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
187 	sc->lm.initdata = 0;
188 	sc->lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
189 	sc->lm.init = az_lm700x_init;
190 	sc->lm.rset = az_lm700x_rset;
191 
192 	az_set_freq(sc, sc->freq);
193 
194 	radio_attach_mi(&az_hw_if, sc, &sc->sc_dev);
195 }
196 
197 /*
198  * Mute the card
199  */
200 void
201 az_set_mute(struct az_softc *sc)
202 {
203 	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
204 	    sc->mute ? 0 : sc->vol);
205 	delay(6);
206 	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
207 	    sc->mute ? 0 : sc->vol);
208 }
209 
210 void
211 az_set_freq(struct az_softc *sc, u_int32_t nfreq)
212 {
213 	u_int8_t vol;
214 	u_int32_t reg;
215 
216 	vol = sc->mute ? 0 : sc->vol;
217 
218 	if (nfreq > MAX_FM_FREQ)
219 		nfreq = MAX_FM_FREQ;
220 	if (nfreq < MIN_FM_FREQ)
221 		nfreq = MIN_FM_FREQ;
222 
223 	sc->freq = nfreq;
224 
225 	reg = lm700x_encode_freq(nfreq, sc->rf);
226 	reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
227 
228 	lm700x_hardware_write(&sc->lm, reg, vol);
229 
230 	az_set_mute(sc);
231 }
232 
233 /*
234  * Return state of the card - tuned/not tuned, mono/stereo
235  */
236 u_int8_t
237 az_state(bus_space_tag_t iot, bus_space_handle_t ioh)
238 {
239 	return (3 ^ bus_space_read_1(iot, ioh, 0)) & 3;
240 }
241 
242 /*
243  * Convert volume to hardware representation.
244  * The card uses bits 00000x0x to set volume.
245  */
246 u_int8_t
247 az_conv_vol(u_int8_t vol)
248 {
249 	if (vol < VOLUME_RATIO(1))
250 		return 0;
251 	else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
252 		return 1;
253 	else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
254 		return 4;
255 	else
256 		return 5;
257 }
258 
259 /*
260  * Convert volume from hardware representation
261  */
262 u_int8_t
263 az_unconv_vol(u_int8_t vol)
264 {
265 	switch (vol) {
266 	case 0:
267 		return VOLUME_RATIO(0);
268 	case 1:
269 		return VOLUME_RATIO(1);
270 	case 4:
271 		return VOLUME_RATIO(2);
272 	}
273 	return VOLUME_RATIO(3);
274 }
275 
276 u_int
277 az_find(bus_space_tag_t iot, bus_space_handle_t ioh)
278 {
279 	struct az_softc sc;
280 	u_int i, scanres = 0;
281 
282 	sc.lm.iot = iot;
283 	sc.lm.ioh = ioh;
284 	sc.lm.offset = 0;
285 	sc.lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
286 	sc.lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
287 	sc.lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
288 	sc.lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
289 	sc.lm.initdata = 0;
290 	sc.lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
291 	sc.lm.init = az_lm700x_init;
292 	sc.lm.rset = az_lm700x_rset;
293 	sc.rf = LM700X_REF_050;
294 	sc.mute = 0;
295 	sc.stereo = LM700X_STEREO;
296 	sc.vol = 0;
297 
298 	/*
299 	 * Scan whole FM range. If there is a card it'll
300 	 * respond on some frequency.
301 	 */
302 	for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
303 		az_set_freq(&sc, i);
304 		scanres += 3 - az_state(iot, ioh);
305 	}
306 
307 	return scanres;
308 }
309 
310 void
311 az_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
312 		u_int32_t data)
313 {
314 	/* Do nothing */
315 	return;
316 }
317 
318 void
319 az_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
320 		u_int32_t data)
321 {
322 	bus_space_write_1(iot, ioh, off, data);
323 }
324 
325 int
326 az_get_info(void *v, struct radio_info *ri)
327 {
328 	struct az_softc *sc = v;
329 
330 	ri->mute = sc->mute;
331 	ri->volume = az_unconv_vol(sc->vol);
332 	ri->stereo = sc->stereo == LM700X_STEREO ? 1 : 0;
333 	ri->caps = AZTECH_CAPABILITIES;
334 	ri->rfreq = lm700x_decode_ref(sc->rf);
335 	ri->info = az_state(sc->lm.iot, sc->lm.ioh);
336 	ri->freq = sc->freq;
337 
338 	/* UNSUPPORTED */
339 	ri->lock = 0;
340 
341 	return (0);
342 }
343 
344 int
345 az_set_info(void *v, struct radio_info *ri)
346 {
347 	struct az_softc *sc = v;
348 
349 	sc->mute = ri->mute ? 1 : 0;
350 	sc->vol = az_conv_vol(ri->volume);
351 	sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
352 	sc->rf = lm700x_encode_ref(ri->rfreq);
353 
354 	az_set_freq(sc, ri->freq);
355 	az_set_mute(sc);
356 
357 	return (0);
358 }
359