aztech.c revision 1.2 1 /* $NetBSD: aztech.c,v 1.2 2002/01/02 12:42:23 augustss 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 (at) oganer.net>,
7 * Vladimir Popov <jumbo (at) 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 = ia->ia_iobase;
126
127 if (!AZ_BASE_VALID(iobase)) {
128 printf("az: configured iobase 0x%x invalid", iobase);
129 return 0;
130 }
131
132 if (bus_space_map(iot, iobase, iosize, 0, &ioh))
133 return 0;
134
135 r = az_find(iot, ioh);
136
137 bus_space_unmap(iot, ioh, iosize);
138
139 ia->ia_iosize = iosize;
140
141 return (r != 0);
142 }
143
144 void
145 az_attach(struct device *parent, struct device *self, void *aux)
146 {
147 struct az_softc *sc = (void *)self;
148 struct isa_attach_args *ia = aux;
149
150 sc->lm.iot = ia->ia_iot;
151 sc->rf = LM700X_REF_050;
152 sc->stereo = LM700X_STEREO;
153 sc->mute = 0;
154 sc->freq = MIN_FM_FREQ;
155 sc->vol = 0;
156
157 /* remap I/O */
158 if (bus_space_map(sc->lm.iot, ia->ia_iobase, ia->ia_iosize,
159 0, &sc->lm.ioh))
160 panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
161
162 printf(": Aztech/PackardBell");
163
164 /* Configure struct lm700x_t lm */
165 sc->lm.offset = 0;
166 sc->lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
167 sc->lm.wzch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_OFF;
168 sc->lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
169 sc->lm.woch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_ON;
170 sc->lm.initdata = 0;
171 sc->lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
172 sc->lm.init = az_lm700x_init;
173 sc->lm.rset = az_lm700x_rset;
174
175 az_set_freq(sc, sc->freq);
176
177 radio_attach_mi(&az_hw_if, sc, &sc->sc_dev);
178 }
179
180 /*
181 * Mute the card
182 */
183 void
184 az_set_mute(struct az_softc *sc)
185 {
186 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
187 sc->mute ? 0 : sc->vol);
188 delay(6);
189 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
190 sc->mute ? 0 : sc->vol);
191 }
192
193 void
194 az_set_freq(struct az_softc *sc, u_int32_t nfreq)
195 {
196 u_int8_t vol;
197 u_int32_t reg;
198
199 vol = sc->mute ? 0 : sc->vol;
200
201 if (nfreq > MAX_FM_FREQ)
202 nfreq = MAX_FM_FREQ;
203 if (nfreq < MIN_FM_FREQ)
204 nfreq = MIN_FM_FREQ;
205
206 sc->freq = nfreq;
207
208 reg = lm700x_encode_freq(nfreq, sc->rf);
209 reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
210
211 lm700x_hardware_write(&sc->lm, reg, vol);
212
213 az_set_mute(sc);
214 }
215
216 /*
217 * Return state of the card - tuned/not tuned, mono/stereo
218 */
219 u_int8_t
220 az_state(bus_space_tag_t iot, bus_space_handle_t ioh)
221 {
222 return (3 ^ bus_space_read_1(iot, ioh, 0)) & 3;
223 }
224
225 /*
226 * Convert volume to hardware representation.
227 * The card uses bits 00000x0x to set volume.
228 */
229 u_int8_t
230 az_conv_vol(u_int8_t vol)
231 {
232 if (vol < VOLUME_RATIO(1))
233 return 0;
234 else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
235 return 1;
236 else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
237 return 4;
238 else
239 return 5;
240 }
241
242 /*
243 * Convert volume from hardware representation
244 */
245 u_int8_t
246 az_unconv_vol(u_int8_t vol)
247 {
248 switch (vol) {
249 case 0:
250 return VOLUME_RATIO(0);
251 case 1:
252 return VOLUME_RATIO(1);
253 case 4:
254 return VOLUME_RATIO(2);
255 }
256 return VOLUME_RATIO(3);
257 }
258
259 u_int
260 az_find(bus_space_tag_t iot, bus_space_handle_t ioh)
261 {
262 struct az_softc sc;
263 u_int i, scanres = 0;
264
265 sc.lm.iot = iot;
266 sc.lm.ioh = ioh;
267 sc.lm.offset = 0;
268 sc.lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
269 sc.lm.wzch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_OFF;
270 sc.lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
271 sc.lm.woch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_ON;
272 sc.lm.initdata = 0;
273 sc.lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
274 sc.lm.init = az_lm700x_init;
275 sc.lm.rset = az_lm700x_rset;
276 sc.rf = LM700X_REF_050;
277 sc.mute = 0;
278 sc.stereo = LM700X_STEREO;
279 sc.vol = 0;
280
281 /*
282 * Scan whole FM range. If there is a card it'll
283 * respond on some frequency.
284 */
285 for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
286 az_set_freq(&sc, i);
287 scanres += 3 - az_state(iot, ioh);
288 }
289
290 return scanres;
291 }
292
293 void
294 az_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
295 u_int32_t data)
296 {
297 /* Do nothing */
298 return;
299 }
300
301 void
302 az_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
303 u_int32_t data)
304 {
305 bus_space_write_1(iot, ioh, off, data);
306 }
307
308 int
309 az_get_info(void *v, struct radio_info *ri)
310 {
311 struct az_softc *sc = v;
312
313 ri->mute = sc->mute;
314 ri->volume = az_unconv_vol(sc->vol);
315 ri->stereo = sc->stereo == LM700X_STEREO ? 1 : 0;
316 ri->caps = AZTECH_CAPABILITIES;
317 ri->rfreq = lm700x_decode_ref(sc->rf);
318 ri->info = az_state(sc->lm.iot, sc->lm.ioh);
319 ri->freq = sc->freq;
320
321 /* UNSUPPORTED */
322 ri->lock = 0;
323
324 return (0);
325 }
326
327 int
328 az_set_info(void *v, struct radio_info *ri)
329 {
330 struct az_softc *sc = v;
331
332 sc->mute = ri->mute ? 1 : 0;
333 sc->vol = az_conv_vol(ri->volume);
334 sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
335 sc->rf = lm700x_encode_ref(ri->rfreq);
336
337 az_set_freq(sc, ri->freq);
338 az_set_mute(sc);
339
340 return (0);
341 }
342