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