radiotrack2.c revision 1.4.2.2 1 1.4.2.2 nathanw /* $NetBSD: radiotrack2.c,v 1.4.2.2 2002/01/08 00:30:30 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.2 nathanw
128 1.4.2.2 nathanw int iosize = 1, iobase = ia->ia_iobase;
129 1.4.2.2 nathanw
130 1.4.2.2 nathanw if (!RTII_BASE_VALID(iobase)) {
131 1.4.2.2 nathanw printf("rtii: configured iobase 0x%x invalid\n", iobase);
132 1.4.2.2 nathanw return 0;
133 1.4.2.2 nathanw }
134 1.4.2.2 nathanw
135 1.4.2.2 nathanw if (bus_space_map(iot, iobase, iosize, 0, &ioh))
136 1.4.2.2 nathanw return 0;
137 1.4.2.2 nathanw
138 1.4.2.2 nathanw bus_space_unmap(iot, ioh, iosize);
139 1.4.2.2 nathanw
140 1.4.2.2 nathanw if (!rtii_find(iot, ioh))
141 1.4.2.2 nathanw return 0;
142 1.4.2.2 nathanw
143 1.4.2.2 nathanw ia->ia_iosize = iosize;
144 1.4.2.2 nathanw return 1;
145 1.4.2.2 nathanw }
146 1.4.2.2 nathanw
147 1.4.2.2 nathanw void
148 1.4.2.2 nathanw rtii_attach(struct device *parent, struct device *self, void *aux)
149 1.4.2.2 nathanw {
150 1.4.2.2 nathanw struct rtii_softc *sc = (void *) self;
151 1.4.2.2 nathanw struct isa_attach_args *ia = aux;
152 1.4.2.2 nathanw
153 1.4.2.2 nathanw sc->tea.iot = ia->ia_iot;
154 1.4.2.2 nathanw sc->mute = 0;
155 1.4.2.2 nathanw sc->vol = 0;
156 1.4.2.2 nathanw sc->freq = MIN_FM_FREQ;
157 1.4.2.2 nathanw sc->stereo = TEA5757_STEREO;
158 1.4.2.2 nathanw sc->lock = TEA5757_S030;
159 1.4.2.2 nathanw
160 1.4.2.2 nathanw /* remap I/O */
161 1.4.2.2 nathanw if (bus_space_map(sc->tea.iot, ia->ia_iobase, ia->ia_iosize,
162 1.4.2.2 nathanw 0, &sc->tea.ioh))
163 1.4.2.2 nathanw panic("rtiiattach: bus_space_map() failed");
164 1.4.2.2 nathanw
165 1.4.2.2 nathanw sc->tea.offset = 0;
166 1.4.2.2 nathanw
167 1.4.2.2 nathanw sc->tea.init = rtii_init;
168 1.4.2.2 nathanw sc->tea.rset = rtii_rset;
169 1.4.2.2 nathanw sc->tea.write_bit = rtii_write_bit;
170 1.4.2.2 nathanw sc->tea.read = rtii_hw_read;
171 1.4.2.2 nathanw
172 1.4.2.2 nathanw printf(": AIMS Lab Radiotrack II");
173 1.4.2.2 nathanw tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
174 1.4.2.2 nathanw rtii_set_mute(sc);
175 1.4.2.2 nathanw
176 1.4.2.2 nathanw radio_attach_mi(&rtii_hw_if, sc, &sc->dev);
177 1.4.2.2 nathanw }
178 1.4.2.2 nathanw
179 1.4.2.2 nathanw /*
180 1.4.2.2 nathanw * Mute/unmute the card
181 1.4.2.2 nathanw */
182 1.4.2.2 nathanw void
183 1.4.2.2 nathanw rtii_set_mute(struct rtii_softc *sc)
184 1.4.2.2 nathanw {
185 1.4.2.2 nathanw u_int8_t mute;
186 1.4.2.2 nathanw
187 1.4.2.2 nathanw mute = (sc->mute || !sc->vol) ? RTII_MUTE : RTII_UNMUTE;
188 1.4.2.2 nathanw bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
189 1.4.2.2 nathanw DELAY(6);
190 1.4.2.2 nathanw bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
191 1.4.2.2 nathanw }
192 1.4.2.2 nathanw
193 1.4.2.2 nathanw void
194 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)
195 1.4.2.2 nathanw {
196 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
197 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
198 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
199 1.4.2.2 nathanw }
200 1.4.2.2 nathanw
201 1.4.2.2 nathanw void
202 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)
203 1.4.2.2 nathanw {
204 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
205 1.4.2.2 nathanw }
206 1.4.2.2 nathanw
207 1.4.2.2 nathanw int
208 1.4.2.2 nathanw rtii_find(bus_space_tag_t iot, bus_space_handle_t ioh)
209 1.4.2.2 nathanw {
210 1.4.2.2 nathanw struct rtii_softc sc;
211 1.4.2.2 nathanw u_int32_t freq;
212 1.4.2.2 nathanw
213 1.4.2.2 nathanw sc.tea.iot = iot;
214 1.4.2.2 nathanw sc.tea.ioh = ioh;
215 1.4.2.2 nathanw sc.tea.offset = 0;
216 1.4.2.2 nathanw sc.tea.init = rtii_init;
217 1.4.2.2 nathanw sc.tea.rset = rtii_rset;
218 1.4.2.2 nathanw sc.tea.write_bit = rtii_write_bit;
219 1.4.2.2 nathanw sc.tea.read = rtii_hw_read;
220 1.4.2.2 nathanw sc.lock = TEA5757_S030;
221 1.4.2.2 nathanw sc.stereo = TEA5757_STEREO;
222 1.4.2.2 nathanw
223 1.4.2.2 nathanw /*
224 1.4.2.2 nathanw * Let's try to write and read a frequency.
225 1.4.2.2 nathanw * If the written and read frequencies are
226 1.4.2.2 nathanw * the same then success.
227 1.4.2.2 nathanw */
228 1.4.2.2 nathanw sc.freq = MIN_FM_FREQ;
229 1.4.2.2 nathanw tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
230 1.4.2.2 nathanw rtii_set_mute(&sc);
231 1.4.2.2 nathanw freq = rtii_hw_read(iot, ioh, sc.tea.offset);
232 1.4.2.2 nathanw if (tea5757_decode_freq(freq) == sc.freq)
233 1.4.2.2 nathanw return 1;
234 1.4.2.2 nathanw
235 1.4.2.2 nathanw return 0;
236 1.4.2.2 nathanw }
237 1.4.2.2 nathanw
238 1.4.2.2 nathanw void
239 1.4.2.2 nathanw rtii_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
240 1.4.2.2 nathanw {
241 1.4.2.2 nathanw u_int8_t data;
242 1.4.2.2 nathanw
243 1.4.2.2 nathanw data = bit ? RTII_DATA_ON : RTII_DATA_OFF;
244 1.4.2.2 nathanw
245 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
246 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_ON | data);
247 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
248 1.4.2.2 nathanw }
249 1.4.2.2 nathanw
250 1.4.2.2 nathanw u_int32_t
251 1.4.2.2 nathanw rtii_hw_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
252 1.4.2.2 nathanw {
253 1.4.2.2 nathanw u_int8_t i;
254 1.4.2.2 nathanw u_int32_t res = 0;
255 1.4.2.2 nathanw
256 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
257 1.4.2.2 nathanw DELAY(6);
258 1.4.2.2 nathanw
259 1.4.2.2 nathanw i = 24;
260 1.4.2.2 nathanw while ( i-- ) {
261 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_HIGH);
262 1.4.2.2 nathanw DELAY(6);
263 1.4.2.2 nathanw bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
264 1.4.2.2 nathanw res |= bus_space_read_1(iot, ioh, off) & RTII_DATA_ON ? 1 : 0;
265 1.4.2.2 nathanw DELAY(6);
266 1.4.2.2 nathanw res <<= 1;
267 1.4.2.2 nathanw }
268 1.4.2.2 nathanw
269 1.4.2.2 nathanw return (res & (TEA5757_DATA | TEA5757_FREQ)) >> 1;
270 1.4.2.2 nathanw }
271 1.4.2.2 nathanw
272 1.4.2.2 nathanw int
273 1.4.2.2 nathanw rtii_get_info(void *v, struct radio_info *ri)
274 1.4.2.2 nathanw {
275 1.4.2.2 nathanw struct rtii_softc *sc = v;
276 1.4.2.2 nathanw
277 1.4.2.2 nathanw ri->mute = sc->mute;
278 1.4.2.2 nathanw ri->volume = sc->vol ? 255 : 0;
279 1.4.2.2 nathanw ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
280 1.4.2.2 nathanw ri->caps = RTII_CAPABILITIES;
281 1.4.2.2 nathanw ri->rfreq = 0;
282 1.4.2.2 nathanw ri->lock = tea5757_decode_lock(sc->lock);
283 1.4.2.2 nathanw
284 1.4.2.2 nathanw ri->freq = sc->freq = tea5757_decode_freq(rtii_hw_read(sc->tea.iot,
285 1.4.2.2 nathanw sc->tea.ioh, sc->tea.offset));
286 1.4.2.2 nathanw
287 1.4.2.2 nathanw switch (bus_space_read_1(sc->tea.iot, sc->tea.ioh, 0)) {
288 1.4.2.2 nathanw case 0xFD:
289 1.4.2.2 nathanw ri->info = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO;
290 1.4.2.2 nathanw break;
291 1.4.2.2 nathanw case 0xFF:
292 1.4.2.2 nathanw ri->info = 0;
293 1.4.2.2 nathanw break;
294 1.4.2.2 nathanw default:
295 1.4.2.2 nathanw ri->info = RADIO_INFO_SIGNAL;
296 1.4.2.2 nathanw }
297 1.4.2.2 nathanw
298 1.4.2.2 nathanw return (0);
299 1.4.2.2 nathanw }
300 1.4.2.2 nathanw
301 1.4.2.2 nathanw int
302 1.4.2.2 nathanw rtii_set_info(void *v, struct radio_info *ri)
303 1.4.2.2 nathanw {
304 1.4.2.2 nathanw struct rtii_softc *sc = v;
305 1.4.2.2 nathanw
306 1.4.2.2 nathanw sc->mute = ri->mute ? 1 : 0;
307 1.4.2.2 nathanw sc->vol = ri->volume ? 255 : 0;
308 1.4.2.2 nathanw sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
309 1.4.2.2 nathanw sc->lock = tea5757_encode_lock(ri->lock);
310 1.4.2.2 nathanw ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
311 1.4.2.2 nathanw sc->lock, sc->stereo, ri->freq);
312 1.4.2.2 nathanw rtii_set_mute(sc);
313 1.4.2.2 nathanw
314 1.4.2.2 nathanw return (0);
315 1.4.2.2 nathanw }
316 1.4.2.2 nathanw
317 1.4.2.2 nathanw int
318 1.4.2.2 nathanw rtii_search(void *v, int f)
319 1.4.2.2 nathanw {
320 1.4.2.2 nathanw struct rtii_softc *sc = v;
321 1.4.2.2 nathanw
322 1.4.2.2 nathanw tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
323 1.4.2.2 nathanw rtii_set_mute(sc);
324 1.4.2.2 nathanw
325 1.4.2.2 nathanw return (0);
326 1.4.2.2 nathanw }
327