aztech.c revision 1.4.2.4 1 1.4.2.4 nathanw /* $NetBSD: aztech.c,v 1.4.2.4 2002/02/28 04:13:38 nathanw Exp $ */
2 1.4.2.2 nathanw /* $OpenBSD: aztech.c,v 1.2 2001/12/05 10:27:06 mickey Exp $ */
3 1.4.2.2 nathanw /* $RuOBSD: aztech.c,v 1.11 2001/10/20 13:23:47 pva Exp $ */
4 1.4.2.2 nathanw
5 1.4.2.2 nathanw /*
6 1.4.2.2 nathanw * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.net>,
7 1.4.2.2 nathanw * Vladimir Popov <jumbo (at) narod.ru>
8 1.4.2.2 nathanw * All rights reserved.
9 1.4.2.2 nathanw *
10 1.4.2.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.4.2.2 nathanw * modification, are permitted provided that the following conditions
12 1.4.2.2 nathanw * are met:
13 1.4.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.4.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.4.2.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.4.2.2 nathanw *
19 1.4.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 1.4.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.4.2.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.4.2.2 nathanw * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.4.2.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.4.2.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.4.2.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.4.2.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.4.2.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.4.2.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.4.2.2 nathanw */
30 1.4.2.2 nathanw
31 1.4.2.2 nathanw /* Aztech/PackardBell FM Radio Card device driver */
32 1.4.2.2 nathanw
33 1.4.2.2 nathanw /*
34 1.4.2.2 nathanw * Sanyo LM7001J Direct PLL Frequency Synthesizer:
35 1.4.2.2 nathanw * ??? See http://www.redsword.com/tjacobs/geeb/fmcard.htm
36 1.4.2.2 nathanw *
37 1.4.2.2 nathanw * Philips TEA5712T AM/FM Stereo DTS Radio:
38 1.4.2.2 nathanw * http://www.semiconductors.philips.com/pip/TEA5712
39 1.4.2.2 nathanw */
40 1.4.2.2 nathanw
41 1.4.2.2 nathanw #include <sys/param.h>
42 1.4.2.2 nathanw #include <sys/systm.h>
43 1.4.2.2 nathanw #include <sys/proc.h>
44 1.4.2.2 nathanw #include <sys/errno.h>
45 1.4.2.2 nathanw #include <sys/ioctl.h>
46 1.4.2.2 nathanw #include <sys/device.h>
47 1.4.2.2 nathanw #include <sys/radioio.h>
48 1.4.2.2 nathanw
49 1.4.2.2 nathanw #include <machine/bus.h>
50 1.4.2.2 nathanw
51 1.4.2.2 nathanw #include <dev/isa/isavar.h>
52 1.4.2.2 nathanw #include <dev/ic/lm700x.h>
53 1.4.2.2 nathanw #include <dev/radio_if.h>
54 1.4.2.2 nathanw
55 1.4.2.2 nathanw #define RF_25K 25
56 1.4.2.2 nathanw #define RF_50K 50
57 1.4.2.2 nathanw #define RF_100K 100
58 1.4.2.2 nathanw
59 1.4.2.2 nathanw #define MAX_VOL 3
60 1.4.2.2 nathanw #define VOLUME_RATIO(x) (255 * x / MAX_VOL)
61 1.4.2.2 nathanw
62 1.4.2.2 nathanw #define AZ_BASE_VALID(x) ((x == 0x350) || (x == 0x358))
63 1.4.2.2 nathanw #define AZTECH_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
64 1.4.2.2 nathanw RADIO_CAPS_DETECT_SIGNAL | \
65 1.4.2.2 nathanw RADIO_CAPS_SET_MONO | \
66 1.4.2.2 nathanw RADIO_CAPS_REFERENCE_FREQ
67 1.4.2.2 nathanw
68 1.4.2.2 nathanw #define AZ_WREN_ON (1 << 1)
69 1.4.2.2 nathanw #define AZ_WREN_OFF (0 << 1)
70 1.4.2.2 nathanw
71 1.4.2.2 nathanw #define AZ_CLCK_ON (1 << 6)
72 1.4.2.2 nathanw #define AZ_CLCK_OFF (0 << 6)
73 1.4.2.2 nathanw
74 1.4.2.2 nathanw #define AZ_DATA_ON (1 << 7)
75 1.4.2.2 nathanw #define AZ_DATA_OFF (0 << 7)
76 1.4.2.2 nathanw
77 1.4.2.2 nathanw int az_probe(struct device *, struct cfdata *, void *);
78 1.4.2.2 nathanw void az_attach(struct device *, struct device * self, void *);
79 1.4.2.2 nathanw
80 1.4.2.2 nathanw int az_get_info(void *, struct radio_info *);
81 1.4.2.2 nathanw int az_set_info(void *, struct radio_info *);
82 1.4.2.2 nathanw
83 1.4.2.2 nathanw struct radio_hw_if az_hw_if = {
84 1.4.2.2 nathanw NULL, /* open */
85 1.4.2.2 nathanw NULL, /* close */
86 1.4.2.2 nathanw az_get_info,
87 1.4.2.2 nathanw az_set_info,
88 1.4.2.2 nathanw NULL
89 1.4.2.2 nathanw };
90 1.4.2.2 nathanw
91 1.4.2.2 nathanw struct az_softc {
92 1.4.2.2 nathanw struct device sc_dev;
93 1.4.2.2 nathanw
94 1.4.2.2 nathanw int mute;
95 1.4.2.2 nathanw u_int8_t vol;
96 1.4.2.2 nathanw u_int32_t freq;
97 1.4.2.2 nathanw u_int32_t rf;
98 1.4.2.2 nathanw u_int32_t stereo;
99 1.4.2.2 nathanw
100 1.4.2.2 nathanw struct lm700x_t lm;
101 1.4.2.2 nathanw };
102 1.4.2.2 nathanw
103 1.4.2.2 nathanw struct cfattach az_ca = {
104 1.4.2.2 nathanw sizeof(struct az_softc), az_probe, az_attach
105 1.4.2.2 nathanw };
106 1.4.2.2 nathanw
107 1.4.2.2 nathanw u_int az_find(bus_space_tag_t, bus_space_handle_t);
108 1.4.2.2 nathanw void az_set_mute(struct az_softc *);
109 1.4.2.2 nathanw void az_set_freq(struct az_softc *, u_int32_t);
110 1.4.2.2 nathanw u_int8_t az_state(bus_space_tag_t, bus_space_handle_t);
111 1.4.2.2 nathanw
112 1.4.2.2 nathanw void az_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
113 1.4.2.2 nathanw void az_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
114 1.4.2.2 nathanw
115 1.4.2.2 nathanw u_int8_t az_conv_vol(u_int8_t);
116 1.4.2.2 nathanw u_int8_t az_unconv_vol(u_int8_t);
117 1.4.2.2 nathanw
118 1.4.2.2 nathanw int
119 1.4.2.2 nathanw az_probe(struct device *parent, struct cfdata *cf, void *aux)
120 1.4.2.2 nathanw {
121 1.4.2.2 nathanw struct isa_attach_args *ia = aux;
122 1.4.2.2 nathanw bus_space_tag_t iot = ia->ia_iot;
123 1.4.2.2 nathanw bus_space_handle_t ioh;
124 1.4.2.3 nathanw u_int r;
125 1.4.2.3 nathanw int iosize = 1, iobase;
126 1.4.2.2 nathanw
127 1.4.2.3 nathanw if (ISA_DIRECT_CONFIG(ia))
128 1.4.2.3 nathanw return 0;
129 1.4.2.3 nathanw
130 1.4.2.3 nathanw if (ia->ia_nio < 1)
131 1.4.2.3 nathanw return 0;
132 1.4.2.3 nathanw
133 1.4.2.3 nathanw iobase = ia->ia_io[0].ir_addr;
134 1.4.2.2 nathanw
135 1.4.2.2 nathanw if (!AZ_BASE_VALID(iobase)) {
136 1.4.2.2 nathanw printf("az: configured iobase 0x%x invalid", iobase);
137 1.4.2.2 nathanw return 0;
138 1.4.2.2 nathanw }
139 1.4.2.2 nathanw
140 1.4.2.2 nathanw if (bus_space_map(iot, iobase, iosize, 0, &ioh))
141 1.4.2.2 nathanw return 0;
142 1.4.2.2 nathanw
143 1.4.2.3 nathanw r = az_find(iot, ioh);
144 1.4.2.3 nathanw
145 1.4.2.2 nathanw bus_space_unmap(iot, ioh, iosize);
146 1.4.2.2 nathanw
147 1.4.2.3 nathanw if (r != 0) {
148 1.4.2.3 nathanw ia->ia_nio = 1;
149 1.4.2.3 nathanw ia->ia_io[0].ir_size = iosize;
150 1.4.2.3 nathanw
151 1.4.2.3 nathanw ia->ia_niomem = 0;
152 1.4.2.3 nathanw ia->ia_nirq = 0;
153 1.4.2.3 nathanw ia->ia_ndrq = 0;
154 1.4.2.3 nathanw
155 1.4.2.3 nathanw return (1);
156 1.4.2.3 nathanw }
157 1.4.2.2 nathanw
158 1.4.2.3 nathanw return (0);
159 1.4.2.2 nathanw }
160 1.4.2.2 nathanw
161 1.4.2.2 nathanw void
162 1.4.2.2 nathanw az_attach(struct device *parent, struct device *self, void *aux)
163 1.4.2.2 nathanw {
164 1.4.2.2 nathanw struct az_softc *sc = (void *)self;
165 1.4.2.2 nathanw struct isa_attach_args *ia = aux;
166 1.4.2.2 nathanw
167 1.4.2.2 nathanw sc->lm.iot = ia->ia_iot;
168 1.4.2.2 nathanw sc->rf = LM700X_REF_050;
169 1.4.2.2 nathanw sc->stereo = LM700X_STEREO;
170 1.4.2.2 nathanw sc->mute = 0;
171 1.4.2.2 nathanw sc->freq = MIN_FM_FREQ;
172 1.4.2.2 nathanw sc->vol = 0;
173 1.4.2.2 nathanw
174 1.4.2.2 nathanw /* remap I/O */
175 1.4.2.3 nathanw if (bus_space_map(sc->lm.iot, ia->ia_io[0].ir_addr,
176 1.4.2.3 nathanw ia->ia_io[0].ir_size, 0, &sc->lm.ioh))
177 1.4.2.2 nathanw panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
178 1.4.2.2 nathanw
179 1.4.2.3 nathanw printf(": Aztech/PackardBell\n");
180 1.4.2.2 nathanw
181 1.4.2.2 nathanw /* Configure struct lm700x_t lm */
182 1.4.2.2 nathanw sc->lm.offset = 0;
183 1.4.2.2 nathanw sc->lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
184 1.4.2.2 nathanw sc->lm.wzch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_OFF;
185 1.4.2.2 nathanw sc->lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
186 1.4.2.2 nathanw sc->lm.woch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_ON;
187 1.4.2.2 nathanw sc->lm.initdata = 0;
188 1.4.2.2 nathanw sc->lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
189 1.4.2.2 nathanw sc->lm.init = az_lm700x_init;
190 1.4.2.2 nathanw sc->lm.rset = az_lm700x_rset;
191 1.4.2.2 nathanw
192 1.4.2.2 nathanw az_set_freq(sc, sc->freq);
193 1.4.2.2 nathanw
194 1.4.2.2 nathanw radio_attach_mi(&az_hw_if, sc, &sc->sc_dev);
195 1.4.2.2 nathanw }
196 1.4.2.2 nathanw
197 1.4.2.2 nathanw /*
198 1.4.2.2 nathanw * Mute the card
199 1.4.2.2 nathanw */
200 1.4.2.2 nathanw void
201 1.4.2.2 nathanw az_set_mute(struct az_softc *sc)
202 1.4.2.2 nathanw {
203 1.4.2.2 nathanw bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
204 1.4.2.2 nathanw sc->mute ? 0 : sc->vol);
205 1.4.2.2 nathanw delay(6);
206 1.4.2.2 nathanw bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
207 1.4.2.2 nathanw sc->mute ? 0 : sc->vol);
208 1.4.2.2 nathanw }
209 1.4.2.2 nathanw
210 1.4.2.2 nathanw void
211 1.4.2.2 nathanw az_set_freq(struct az_softc *sc, u_int32_t nfreq)
212 1.4.2.2 nathanw {
213 1.4.2.2 nathanw u_int8_t vol;
214 1.4.2.2 nathanw u_int32_t reg;
215 1.4.2.2 nathanw
216 1.4.2.2 nathanw vol = sc->mute ? 0 : sc->vol;
217 1.4.2.2 nathanw
218 1.4.2.2 nathanw if (nfreq > MAX_FM_FREQ)
219 1.4.2.2 nathanw nfreq = MAX_FM_FREQ;
220 1.4.2.2 nathanw if (nfreq < MIN_FM_FREQ)
221 1.4.2.2 nathanw nfreq = MIN_FM_FREQ;
222 1.4.2.2 nathanw
223 1.4.2.2 nathanw sc->freq = nfreq;
224 1.4.2.2 nathanw
225 1.4.2.2 nathanw reg = lm700x_encode_freq(nfreq, sc->rf);
226 1.4.2.2 nathanw reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
227 1.4.2.2 nathanw
228 1.4.2.2 nathanw lm700x_hardware_write(&sc->lm, reg, vol);
229 1.4.2.2 nathanw
230 1.4.2.2 nathanw az_set_mute(sc);
231 1.4.2.2 nathanw }
232 1.4.2.2 nathanw
233 1.4.2.2 nathanw /*
234 1.4.2.2 nathanw * Return state of the card - tuned/not tuned, mono/stereo
235 1.4.2.2 nathanw */
236 1.4.2.2 nathanw u_int8_t
237 1.4.2.2 nathanw az_state(bus_space_tag_t iot, bus_space_handle_t ioh)
238 1.4.2.2 nathanw {
239 1.4.2.2 nathanw return (3 ^ bus_space_read_1(iot, ioh, 0)) & 3;
240 1.4.2.2 nathanw }
241 1.4.2.2 nathanw
242 1.4.2.2 nathanw /*
243 1.4.2.2 nathanw * Convert volume to hardware representation.
244 1.4.2.2 nathanw * The card uses bits 00000x0x to set volume.
245 1.4.2.2 nathanw */
246 1.4.2.2 nathanw u_int8_t
247 1.4.2.2 nathanw az_conv_vol(u_int8_t vol)
248 1.4.2.2 nathanw {
249 1.4.2.2 nathanw if (vol < VOLUME_RATIO(1))
250 1.4.2.2 nathanw return 0;
251 1.4.2.2 nathanw else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
252 1.4.2.2 nathanw return 1;
253 1.4.2.2 nathanw else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
254 1.4.2.2 nathanw return 4;
255 1.4.2.2 nathanw else
256 1.4.2.2 nathanw return 5;
257 1.4.2.2 nathanw }
258 1.4.2.2 nathanw
259 1.4.2.2 nathanw /*
260 1.4.2.2 nathanw * Convert volume from hardware representation
261 1.4.2.2 nathanw */
262 1.4.2.2 nathanw u_int8_t
263 1.4.2.2 nathanw az_unconv_vol(u_int8_t vol)
264 1.4.2.2 nathanw {
265 1.4.2.2 nathanw switch (vol) {
266 1.4.2.2 nathanw case 0:
267 1.4.2.2 nathanw return VOLUME_RATIO(0);
268 1.4.2.2 nathanw case 1:
269 1.4.2.2 nathanw return VOLUME_RATIO(1);
270 1.4.2.2 nathanw case 4:
271 1.4.2.2 nathanw return VOLUME_RATIO(2);
272 1.4.2.2 nathanw }
273 1.4.2.2 nathanw return VOLUME_RATIO(3);
274 1.4.2.2 nathanw }
275 1.4.2.2 nathanw
276 1.4.2.2 nathanw u_int
277 1.4.2.2 nathanw az_find(bus_space_tag_t iot, bus_space_handle_t ioh)
278 1.4.2.2 nathanw {
279 1.4.2.2 nathanw struct az_softc sc;
280 1.4.2.2 nathanw u_int i, scanres = 0;
281 1.4.2.2 nathanw
282 1.4.2.2 nathanw sc.lm.iot = iot;
283 1.4.2.2 nathanw sc.lm.ioh = ioh;
284 1.4.2.2 nathanw sc.lm.offset = 0;
285 1.4.2.2 nathanw sc.lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
286 1.4.2.2 nathanw sc.lm.wzch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_OFF;
287 1.4.2.2 nathanw sc.lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
288 1.4.2.2 nathanw sc.lm.woch = AZ_WREN_ON | AZ_CLCK_ON | AZ_DATA_ON;
289 1.4.2.2 nathanw sc.lm.initdata = 0;
290 1.4.2.2 nathanw sc.lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
291 1.4.2.2 nathanw sc.lm.init = az_lm700x_init;
292 1.4.2.2 nathanw sc.lm.rset = az_lm700x_rset;
293 1.4.2.2 nathanw sc.rf = LM700X_REF_050;
294 1.4.2.2 nathanw sc.mute = 0;
295 1.4.2.2 nathanw sc.stereo = LM700X_STEREO;
296 1.4.2.2 nathanw sc.vol = 0;
297 1.4.2.2 nathanw
298 1.4.2.2 nathanw /*
299 1.4.2.2 nathanw * Scan whole FM range. If there is a card it'll
300 1.4.2.2 nathanw * respond on some frequency.
301 1.4.2.2 nathanw */
302 1.4.2.2 nathanw for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
303 1.4.2.2 nathanw az_set_freq(&sc, i);
304 1.4.2.2 nathanw scanres += 3 - az_state(iot, ioh);
305 1.4.2.2 nathanw }
306 1.4.2.2 nathanw
307 1.4.2.2 nathanw return scanres;
308 1.4.2.2 nathanw }
309 1.4.2.2 nathanw
310 1.4.2.2 nathanw void
311 1.4.2.2 nathanw az_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
312 1.4.2.2 nathanw u_int32_t data)
313 1.4.2.2 nathanw {
314 1.4.2.2 nathanw /* Do nothing */
315 1.4.2.2 nathanw return;
316 1.4.2.2 nathanw }
317 1.4.2.2 nathanw
318 1.4.2.2 nathanw void
319 1.4.2.2 nathanw az_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
320 1.4.2.2 nathanw u_int32_t data)
321 1.4.2.2 nathanw {
322 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, data);
323 1.4.2.2 nathanw }
324 1.4.2.2 nathanw
325 1.4.2.2 nathanw int
326 1.4.2.2 nathanw az_get_info(void *v, struct radio_info *ri)
327 1.4.2.2 nathanw {
328 1.4.2.2 nathanw struct az_softc *sc = v;
329 1.4.2.2 nathanw
330 1.4.2.2 nathanw ri->mute = sc->mute;
331 1.4.2.2 nathanw ri->volume = az_unconv_vol(sc->vol);
332 1.4.2.2 nathanw ri->stereo = sc->stereo == LM700X_STEREO ? 1 : 0;
333 1.4.2.2 nathanw ri->caps = AZTECH_CAPABILITIES;
334 1.4.2.2 nathanw ri->rfreq = lm700x_decode_ref(sc->rf);
335 1.4.2.2 nathanw ri->info = az_state(sc->lm.iot, sc->lm.ioh);
336 1.4.2.2 nathanw ri->freq = sc->freq;
337 1.4.2.2 nathanw
338 1.4.2.2 nathanw /* UNSUPPORTED */
339 1.4.2.2 nathanw ri->lock = 0;
340 1.4.2.2 nathanw
341 1.4.2.2 nathanw return (0);
342 1.4.2.2 nathanw }
343 1.4.2.2 nathanw
344 1.4.2.2 nathanw int
345 1.4.2.2 nathanw az_set_info(void *v, struct radio_info *ri)
346 1.4.2.2 nathanw {
347 1.4.2.2 nathanw struct az_softc *sc = v;
348 1.4.2.2 nathanw
349 1.4.2.2 nathanw sc->mute = ri->mute ? 1 : 0;
350 1.4.2.2 nathanw sc->vol = az_conv_vol(ri->volume);
351 1.4.2.2 nathanw sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
352 1.4.2.2 nathanw sc->rf = lm700x_encode_ref(ri->rfreq);
353 1.4.2.2 nathanw
354 1.4.2.2 nathanw az_set_freq(sc, ri->freq);
355 1.4.2.2 nathanw az_set_mute(sc);
356 1.4.2.2 nathanw
357 1.4.2.2 nathanw return (0);
358 1.4.2.2 nathanw }
359