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