radiotrack2.c revision 1.4.2.3 1 1.4.2.3 nathanw /* $NetBSD: radiotrack2.c,v 1.4.2.3 2002/01/11 23:39:11 nathanw Exp $ */
2 1.4.2.2 nathanw /* $OpenBSD: radiotrack2.c,v 1.1 2001/12/05 10:27:06 mickey Exp $ */
3 1.4.2.2 nathanw /* $RuOBSD: radiotrack2.c,v 1.2 2001/10/18 16:51:36 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 /* AIMS Lab Radiotrack II FM Radio Card device driver */
32 1.4.2.2 nathanw
33 1.4.2.2 nathanw /*
34 1.4.2.2 nathanw * Philips TEA5757H AM/FM Self Tuned Radio:
35 1.4.2.2 nathanw * http://www.semiconductors.philips.com/pip/TEA5757H
36 1.4.2.2 nathanw */
37 1.4.2.2 nathanw
38 1.4.2.2 nathanw #include <sys/param.h>
39 1.4.2.2 nathanw #include <sys/systm.h>
40 1.4.2.2 nathanw #include <sys/proc.h>
41 1.4.2.2 nathanw #include <sys/errno.h>
42 1.4.2.2 nathanw #include <sys/ioctl.h>
43 1.4.2.2 nathanw #include <sys/device.h>
44 1.4.2.2 nathanw #include <sys/radioio.h>
45 1.4.2.2 nathanw
46 1.4.2.2 nathanw #include <dev/isa/isavar.h>
47 1.4.2.2 nathanw #include <dev/radio_if.h>
48 1.4.2.2 nathanw #include <dev/ic/tea5757.h>
49 1.4.2.2 nathanw
50 1.4.2.2 nathanw #define RTII_BASE_VALID(x) ((x == 0x20C) || (x == 0x30C))
51 1.4.2.2 nathanw #define RTII_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
52 1.4.2.2 nathanw RADIO_CAPS_DETECT_SIGNAL | \
53 1.4.2.2 nathanw RADIO_CAPS_SET_MONO | \
54 1.4.2.2 nathanw RADIO_CAPS_LOCK_SENSITIVITY | \
55 1.4.2.2 nathanw RADIO_CAPS_HW_AFC | \
56 1.4.2.2 nathanw RADIO_CAPS_HW_SEARCH
57 1.4.2.2 nathanw
58 1.4.2.2 nathanw #if 0
59 1.4.2.2 nathanw #define RTII_SIGNAL (1 << 3)
60 1.4.2.2 nathanw #define RTII_STEREO (1 << 3)
61 1.4.2.2 nathanw #endif /* 0 */
62 1.4.2.2 nathanw
63 1.4.2.2 nathanw #define RTII_MUTE 0x01
64 1.4.2.2 nathanw #define RTII_UNMUTE 0x00
65 1.4.2.2 nathanw
66 1.4.2.2 nathanw #define RTII_RESET 0xC8
67 1.4.2.2 nathanw
68 1.4.2.2 nathanw #define RTII_DATA_ON (1 << 2)
69 1.4.2.2 nathanw #define RTII_DATA_OFF (0 << 2)
70 1.4.2.2 nathanw
71 1.4.2.2 nathanw #define RTII_CLCK_ON (1 << 1)
72 1.4.2.2 nathanw #define RTII_CLCK_OFF (0 << 1)
73 1.4.2.2 nathanw
74 1.4.2.2 nathanw #define RTII_WREN_ON (0 << 0)
75 1.4.2.2 nathanw #define RTII_WREN_OFF (1 << 0)
76 1.4.2.2 nathanw
77 1.4.2.2 nathanw #define RTII_READ_CLOCK_LOW (RTII_DATA_ON | RTII_CLCK_OFF | RTII_WREN_OFF)
78 1.4.2.2 nathanw #define RTII_READ_CLOCK_HIGH (RTII_DATA_ON | RTII_CLCK_ON | RTII_WREN_OFF)
79 1.4.2.2 nathanw
80 1.4.2.2 nathanw int rtii_probe(struct device *, struct cfdata *, void *);
81 1.4.2.2 nathanw void rtii_attach(struct device *, struct device * self, void *);
82 1.4.2.2 nathanw
83 1.4.2.2 nathanw int rtii_get_info(void *, struct radio_info *);
84 1.4.2.2 nathanw int rtii_set_info(void *, struct radio_info *);
85 1.4.2.2 nathanw int rtii_search(void *, int);
86 1.4.2.2 nathanw
87 1.4.2.2 nathanw /* define our interface to the higher level radio driver */
88 1.4.2.2 nathanw struct radio_hw_if rtii_hw_if = {
89 1.4.2.2 nathanw NULL, /* open */
90 1.4.2.2 nathanw NULL, /* close */
91 1.4.2.2 nathanw rtii_get_info,
92 1.4.2.2 nathanw rtii_set_info,
93 1.4.2.2 nathanw rtii_search
94 1.4.2.2 nathanw };
95 1.4.2.2 nathanw
96 1.4.2.2 nathanw struct rtii_softc {
97 1.4.2.2 nathanw struct device dev;
98 1.4.2.2 nathanw
99 1.4.2.2 nathanw u_int32_t freq;
100 1.4.2.2 nathanw u_int32_t stereo;
101 1.4.2.2 nathanw u_int32_t lock;
102 1.4.2.2 nathanw u_int8_t vol;
103 1.4.2.2 nathanw int mute;
104 1.4.2.2 nathanw
105 1.4.2.2 nathanw struct tea5757_t tea;
106 1.4.2.2 nathanw };
107 1.4.2.2 nathanw
108 1.4.2.2 nathanw struct cfattach rtii_ca = {
109 1.4.2.2 nathanw sizeof(struct rtii_softc), rtii_probe, rtii_attach
110 1.4.2.2 nathanw };
111 1.4.2.2 nathanw
112 1.4.2.2 nathanw void rtii_set_mute(struct rtii_softc *);
113 1.4.2.2 nathanw int rtii_find(bus_space_tag_t, bus_space_handle_t);
114 1.4.2.2 nathanw
115 1.4.2.2 nathanw u_int32_t rtii_hw_read(bus_space_tag_t, bus_space_handle_t, bus_size_t);
116 1.4.2.2 nathanw
117 1.4.2.2 nathanw void rtii_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
118 1.4.2.2 nathanw void rtii_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
119 1.4.2.2 nathanw void rtii_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
120 1.4.2.2 nathanw
121 1.4.2.2 nathanw int
122 1.4.2.2 nathanw rtii_probe(struct device *parent, struct cfdata *cf, void *aux)
123 1.4.2.2 nathanw {
124 1.4.2.2 nathanw struct isa_attach_args *ia = aux;
125 1.4.2.2 nathanw bus_space_tag_t iot = ia->ia_iot;
126 1.4.2.2 nathanw bus_space_handle_t ioh;
127 1.4.2.3 nathanw u_int r;
128 1.4.2.3 nathanw int iosize = 1, iobase;
129 1.4.2.2 nathanw
130 1.4.2.3 nathanw if (ISA_DIRECT_CONFIG(ia))
131 1.4.2.3 nathanw return 0;
132 1.4.2.3 nathanw
133 1.4.2.3 nathanw if (ia->ia_nio < 1)
134 1.4.2.3 nathanw return 0;
135 1.4.2.3 nathanw
136 1.4.2.3 nathanw iobase = ia->ia_io[0].ir_addr;
137 1.4.2.2 nathanw
138 1.4.2.2 nathanw if (!RTII_BASE_VALID(iobase)) {
139 1.4.2.2 nathanw printf("rtii: configured iobase 0x%x invalid\n", iobase);
140 1.4.2.2 nathanw return 0;
141 1.4.2.2 nathanw }
142 1.4.2.2 nathanw
143 1.4.2.2 nathanw if (bus_space_map(iot, iobase, iosize, 0, &ioh))
144 1.4.2.2 nathanw return 0;
145 1.4.2.2 nathanw
146 1.4.2.3 nathanw r = rtii_find(iot, ioh);
147 1.4.2.3 nathanw
148 1.4.2.2 nathanw bus_space_unmap(iot, ioh, iosize);
149 1.4.2.2 nathanw
150 1.4.2.3 nathanw if (r != 0) {
151 1.4.2.3 nathanw ia->ia_nio = 1;
152 1.4.2.3 nathanw ia->ia_io[0].ir_size = iosize;
153 1.4.2.3 nathanw
154 1.4.2.3 nathanw ia->ia_niomem = 0;
155 1.4.2.3 nathanw ia->ia_nirq = 0;
156 1.4.2.3 nathanw ia->ia_ndrq = 0;
157 1.4.2.3 nathanw
158 1.4.2.3 nathanw return (1);
159 1.4.2.3 nathanw }
160 1.4.2.2 nathanw
161 1.4.2.3 nathanw return (0);
162 1.4.2.2 nathanw }
163 1.4.2.2 nathanw
164 1.4.2.2 nathanw void
165 1.4.2.2 nathanw rtii_attach(struct device *parent, struct device *self, void *aux)
166 1.4.2.2 nathanw {
167 1.4.2.2 nathanw struct rtii_softc *sc = (void *) self;
168 1.4.2.2 nathanw struct isa_attach_args *ia = aux;
169 1.4.2.2 nathanw
170 1.4.2.2 nathanw sc->tea.iot = ia->ia_iot;
171 1.4.2.2 nathanw sc->mute = 0;
172 1.4.2.2 nathanw sc->vol = 0;
173 1.4.2.2 nathanw sc->freq = MIN_FM_FREQ;
174 1.4.2.2 nathanw sc->stereo = TEA5757_STEREO;
175 1.4.2.2 nathanw sc->lock = TEA5757_S030;
176 1.4.2.2 nathanw
177 1.4.2.2 nathanw /* remap I/O */
178 1.4.2.3 nathanw if (bus_space_map(sc->tea.iot, ia->ia_io[0].ir_addr,
179 1.4.2.3 nathanw ia->ia_io[0].ir_size, 0, &sc->tea.ioh))
180 1.4.2.2 nathanw panic("rtiiattach: bus_space_map() failed");
181 1.4.2.2 nathanw
182 1.4.2.2 nathanw sc->tea.offset = 0;
183 1.4.2.2 nathanw
184 1.4.2.2 nathanw sc->tea.init = rtii_init;
185 1.4.2.2 nathanw sc->tea.rset = rtii_rset;
186 1.4.2.2 nathanw sc->tea.write_bit = rtii_write_bit;
187 1.4.2.2 nathanw sc->tea.read = rtii_hw_read;
188 1.4.2.2 nathanw
189 1.4.2.3 nathanw printf(": AIMS Lab Radiotrack II\n");
190 1.4.2.2 nathanw tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
191 1.4.2.2 nathanw rtii_set_mute(sc);
192 1.4.2.2 nathanw
193 1.4.2.2 nathanw radio_attach_mi(&rtii_hw_if, sc, &sc->dev);
194 1.4.2.2 nathanw }
195 1.4.2.2 nathanw
196 1.4.2.2 nathanw /*
197 1.4.2.2 nathanw * Mute/unmute the card
198 1.4.2.2 nathanw */
199 1.4.2.2 nathanw void
200 1.4.2.2 nathanw rtii_set_mute(struct rtii_softc *sc)
201 1.4.2.2 nathanw {
202 1.4.2.2 nathanw u_int8_t mute;
203 1.4.2.2 nathanw
204 1.4.2.2 nathanw mute = (sc->mute || !sc->vol) ? RTII_MUTE : RTII_UNMUTE;
205 1.4.2.2 nathanw bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
206 1.4.2.2 nathanw DELAY(6);
207 1.4.2.2 nathanw bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
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 rtii_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
212 1.4.2.2 nathanw {
213 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
214 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
215 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
216 1.4.2.2 nathanw }
217 1.4.2.2 nathanw
218 1.4.2.2 nathanw void
219 1.4.2.2 nathanw rtii_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
220 1.4.2.2 nathanw {
221 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
222 1.4.2.2 nathanw }
223 1.4.2.2 nathanw
224 1.4.2.2 nathanw int
225 1.4.2.2 nathanw rtii_find(bus_space_tag_t iot, bus_space_handle_t ioh)
226 1.4.2.2 nathanw {
227 1.4.2.2 nathanw struct rtii_softc sc;
228 1.4.2.2 nathanw u_int32_t freq;
229 1.4.2.2 nathanw
230 1.4.2.2 nathanw sc.tea.iot = iot;
231 1.4.2.2 nathanw sc.tea.ioh = ioh;
232 1.4.2.2 nathanw sc.tea.offset = 0;
233 1.4.2.2 nathanw sc.tea.init = rtii_init;
234 1.4.2.2 nathanw sc.tea.rset = rtii_rset;
235 1.4.2.2 nathanw sc.tea.write_bit = rtii_write_bit;
236 1.4.2.2 nathanw sc.tea.read = rtii_hw_read;
237 1.4.2.2 nathanw sc.lock = TEA5757_S030;
238 1.4.2.2 nathanw sc.stereo = TEA5757_STEREO;
239 1.4.2.2 nathanw
240 1.4.2.2 nathanw /*
241 1.4.2.2 nathanw * Let's try to write and read a frequency.
242 1.4.2.2 nathanw * If the written and read frequencies are
243 1.4.2.2 nathanw * the same then success.
244 1.4.2.2 nathanw */
245 1.4.2.2 nathanw sc.freq = MIN_FM_FREQ;
246 1.4.2.2 nathanw tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
247 1.4.2.2 nathanw rtii_set_mute(&sc);
248 1.4.2.2 nathanw freq = rtii_hw_read(iot, ioh, sc.tea.offset);
249 1.4.2.2 nathanw if (tea5757_decode_freq(freq) == sc.freq)
250 1.4.2.2 nathanw return 1;
251 1.4.2.2 nathanw
252 1.4.2.2 nathanw return 0;
253 1.4.2.2 nathanw }
254 1.4.2.2 nathanw
255 1.4.2.2 nathanw void
256 1.4.2.2 nathanw rtii_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
257 1.4.2.2 nathanw {
258 1.4.2.2 nathanw u_int8_t data;
259 1.4.2.2 nathanw
260 1.4.2.2 nathanw data = bit ? RTII_DATA_ON : RTII_DATA_OFF;
261 1.4.2.2 nathanw
262 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
263 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_ON | data);
264 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
265 1.4.2.2 nathanw }
266 1.4.2.2 nathanw
267 1.4.2.2 nathanw u_int32_t
268 1.4.2.2 nathanw rtii_hw_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
269 1.4.2.2 nathanw {
270 1.4.2.2 nathanw u_int8_t i;
271 1.4.2.2 nathanw u_int32_t res = 0;
272 1.4.2.2 nathanw
273 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
274 1.4.2.2 nathanw DELAY(6);
275 1.4.2.2 nathanw
276 1.4.2.2 nathanw i = 24;
277 1.4.2.2 nathanw while ( i-- ) {
278 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_HIGH);
279 1.4.2.2 nathanw DELAY(6);
280 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
281 1.4.2.2 nathanw res |= bus_space_read_1(iot, ioh, off) & RTII_DATA_ON ? 1 : 0;
282 1.4.2.2 nathanw DELAY(6);
283 1.4.2.2 nathanw res <<= 1;
284 1.4.2.2 nathanw }
285 1.4.2.2 nathanw
286 1.4.2.2 nathanw return (res & (TEA5757_DATA | TEA5757_FREQ)) >> 1;
287 1.4.2.2 nathanw }
288 1.4.2.2 nathanw
289 1.4.2.2 nathanw int
290 1.4.2.2 nathanw rtii_get_info(void *v, struct radio_info *ri)
291 1.4.2.2 nathanw {
292 1.4.2.2 nathanw struct rtii_softc *sc = v;
293 1.4.2.2 nathanw
294 1.4.2.2 nathanw ri->mute = sc->mute;
295 1.4.2.2 nathanw ri->volume = sc->vol ? 255 : 0;
296 1.4.2.2 nathanw ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
297 1.4.2.2 nathanw ri->caps = RTII_CAPABILITIES;
298 1.4.2.2 nathanw ri->rfreq = 0;
299 1.4.2.2 nathanw ri->lock = tea5757_decode_lock(sc->lock);
300 1.4.2.2 nathanw
301 1.4.2.2 nathanw ri->freq = sc->freq = tea5757_decode_freq(rtii_hw_read(sc->tea.iot,
302 1.4.2.2 nathanw sc->tea.ioh, sc->tea.offset));
303 1.4.2.2 nathanw
304 1.4.2.2 nathanw switch (bus_space_read_1(sc->tea.iot, sc->tea.ioh, 0)) {
305 1.4.2.2 nathanw case 0xFD:
306 1.4.2.2 nathanw ri->info = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO;
307 1.4.2.2 nathanw break;
308 1.4.2.2 nathanw case 0xFF:
309 1.4.2.2 nathanw ri->info = 0;
310 1.4.2.2 nathanw break;
311 1.4.2.2 nathanw default:
312 1.4.2.2 nathanw ri->info = RADIO_INFO_SIGNAL;
313 1.4.2.2 nathanw }
314 1.4.2.2 nathanw
315 1.4.2.2 nathanw return (0);
316 1.4.2.2 nathanw }
317 1.4.2.2 nathanw
318 1.4.2.2 nathanw int
319 1.4.2.2 nathanw rtii_set_info(void *v, struct radio_info *ri)
320 1.4.2.2 nathanw {
321 1.4.2.2 nathanw struct rtii_softc *sc = v;
322 1.4.2.2 nathanw
323 1.4.2.2 nathanw sc->mute = ri->mute ? 1 : 0;
324 1.4.2.2 nathanw sc->vol = ri->volume ? 255 : 0;
325 1.4.2.2 nathanw sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
326 1.4.2.2 nathanw sc->lock = tea5757_encode_lock(ri->lock);
327 1.4.2.2 nathanw ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
328 1.4.2.2 nathanw sc->lock, sc->stereo, ri->freq);
329 1.4.2.2 nathanw rtii_set_mute(sc);
330 1.4.2.2 nathanw
331 1.4.2.2 nathanw return (0);
332 1.4.2.2 nathanw }
333 1.4.2.2 nathanw
334 1.4.2.2 nathanw int
335 1.4.2.2 nathanw rtii_search(void *v, int f)
336 1.4.2.2 nathanw {
337 1.4.2.2 nathanw struct rtii_softc *sc = v;
338 1.4.2.2 nathanw
339 1.4.2.2 nathanw tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
340 1.4.2.2 nathanw rtii_set_mute(sc);
341 1.4.2.2 nathanw
342 1.4.2.2 nathanw return (0);
343 1.4.2.2 nathanw }
344