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