radiotrack2.c revision 1.2 1 /* $NetBSD: radiotrack2.c,v 1.2 2002/01/02 12:42:23 augustss Exp $ */
2 /* $OpenBSD: radiotrack2.c,v 1.1 2001/12/05 10:27:06 mickey Exp $ */
3 /* $RuOBSD: radiotrack2.c,v 1.2 2001/10/18 16:51:36 pva Exp $ */
4
5 /*
6 * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.net>,
7 * Vladimir Popov <jumbo (at) narod.ru>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* AIMS Lab Radiotrack II FM Radio Card device driver */
32
33 /*
34 * Philips TEA5757H AM/FM Self Tuned Radio:
35 * http://www.semiconductors.philips.com/pip/TEA5757H
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/errno.h>
42 #include <sys/ioctl.h>
43 #include <sys/device.h>
44 #include <sys/radioio.h>
45
46 #include <dev/isa/isavar.h>
47 #include <dev/radio_if.h>
48 #include <dev/ic/tea5757.h>
49
50 #define RTII_BASE_VALID(x) ((x == 0x20C) || (x == 0x30C))
51 #define RTII_CAPABILITIES RADIO_CAPS_DETECT_STEREO | \
52 RADIO_CAPS_DETECT_SIGNAL | \
53 RADIO_CAPS_SET_MONO | \
54 RADIO_CAPS_LOCK_SENSITIVITY | \
55 RADIO_CAPS_HW_AFC | \
56 RADIO_CAPS_HW_SEARCH
57
58 #if 0
59 #define RTII_SIGNAL (1 << 3)
60 #define RTII_STEREO (1 << 3)
61 #endif /* 0 */
62
63 #define RTII_MUTE 0x01
64 #define RTII_UNMUTE 0x00
65
66 #define RTII_RESET 0xC8
67
68 #define RTII_DATA_ON (1 << 2)
69 #define RTII_DATA_OFF (0 << 2)
70
71 #define RTII_CLCK_ON (1 << 1)
72 #define RTII_CLCK_OFF (0 << 1)
73
74 #define RTII_WREN_ON (0 << 0)
75 #define RTII_WREN_OFF (1 << 0)
76
77 #define RTII_READ_CLOCK_LOW (RTII_DATA_ON | RTII_CLCK_OFF | RTII_WREN_OFF)
78 #define RTII_READ_CLOCK_HIGH (RTII_DATA_ON | RTII_CLCK_ON | RTII_WREN_OFF)
79
80 int rtii_probe(struct device *, struct cfdata *, void *);
81 void rtii_attach(struct device *, struct device * self, void *);
82
83 int rtii_get_info(void *, struct radio_info *);
84 int rtii_set_info(void *, struct radio_info *);
85 int rtii_search(void *, int);
86
87 /* define our interface to the higher level radio driver */
88 struct radio_hw_if rtii_hw_if = {
89 NULL, /* open */
90 NULL, /* close */
91 rtii_get_info,
92 rtii_set_info,
93 rtii_search
94 };
95
96 struct rtii_softc {
97 struct device dev;
98
99 u_int32_t freq;
100 u_int32_t stereo;
101 u_int32_t lock;
102 u_int8_t vol;
103 int mute;
104
105 struct tea5757_t tea;
106 };
107
108 struct cfattach rtii_ca = {
109 sizeof(struct rtii_softc), rtii_probe, rtii_attach
110 };
111
112 void rtii_set_mute(struct rtii_softc *);
113 int rtii_find(bus_space_tag_t, bus_space_handle_t);
114
115 u_int32_t rtii_hw_read(bus_space_tag_t, bus_space_handle_t, bus_size_t);
116
117 void rtii_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
118 void rtii_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
119 void rtii_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
120
121 int
122 rtii_probe(struct device *parent, struct cfdata *cf, void *aux)
123 {
124 struct isa_attach_args *ia = aux;
125 bus_space_tag_t iot = ia->ia_iot;
126 bus_space_handle_t ioh;
127 u_int r;
128 int iosize = 1, iobase = ia->ia_iobase;
129
130 if (!RTII_BASE_VALID(iobase)) {
131 printf("rtii: configured iobase 0x%x invalid\n", iobase);
132 return 0;
133 }
134
135 if (bus_space_map(iot, iobase, iosize, 0, &ioh))
136 return 0;
137
138 r = rtii_find(iot, ioh);
139
140 bus_space_unmap(iot, ioh, iosize);
141
142 ia->ia_iosize = iosize;
143
144 return (r != 0);
145 }
146
147 void
148 rtii_attach(struct device *parent, struct device *self, void *aux)
149 {
150 struct rtii_softc *sc = (void *) self;
151 struct isa_attach_args *ia = aux;
152
153 sc->tea.iot = ia->ia_iot;
154 sc->mute = 0;
155 sc->vol = 0;
156 sc->freq = MIN_FM_FREQ;
157 sc->stereo = TEA5757_STEREO;
158 sc->lock = TEA5757_S030;
159
160 /* remap I/O */
161 if (bus_space_map(sc->tea.iot, ia->ia_iobase, ia->ia_iosize,
162 0, &sc->tea.ioh))
163 panic("rtiiattach: bus_space_map() failed");
164
165 sc->tea.offset = 0;
166
167 sc->tea.init = rtii_init;
168 sc->tea.rset = rtii_rset;
169 sc->tea.write_bit = rtii_write_bit;
170 sc->tea.read = rtii_hw_read;
171
172 printf(": AIMS Lab Radiotrack II");
173 tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
174 rtii_set_mute(sc);
175
176 radio_attach_mi(&rtii_hw_if, sc, &sc->dev);
177 }
178
179 /*
180 * Mute/unmute the card
181 */
182 void
183 rtii_set_mute(struct rtii_softc *sc)
184 {
185 u_int8_t mute;
186
187 mute = (sc->mute || !sc->vol) ? RTII_MUTE : RTII_UNMUTE;
188 bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
189 DELAY(6);
190 bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
191 }
192
193 void
194 rtii_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
195 {
196 bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
197 bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
198 bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_ON);
199 }
200
201 void
202 rtii_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
203 {
204 bus_space_write_1(iot, ioh, off, RTII_RESET | RTII_WREN_OFF);
205 }
206
207 int
208 rtii_find(bus_space_tag_t iot, bus_space_handle_t ioh)
209 {
210 struct rtii_softc sc;
211 u_int32_t freq;
212
213 sc.tea.iot = iot;
214 sc.tea.ioh = ioh;
215 sc.tea.offset = 0;
216 sc.tea.init = rtii_init;
217 sc.tea.rset = rtii_rset;
218 sc.tea.write_bit = rtii_write_bit;
219 sc.tea.read = rtii_hw_read;
220 sc.lock = TEA5757_S030;
221 sc.stereo = TEA5757_STEREO;
222
223 /*
224 * Let's try to write and read a frequency.
225 * If the written and read frequencies are
226 * the same then success.
227 */
228 sc.freq = MIN_FM_FREQ;
229 tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
230 rtii_set_mute(&sc);
231 freq = rtii_hw_read(iot, ioh, sc.tea.offset);
232 if (tea5757_decode_freq(freq) == sc.freq)
233 return 1;
234
235 return 0;
236 }
237
238 void
239 rtii_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
240 {
241 u_int8_t data;
242
243 data = bit ? RTII_DATA_ON : RTII_DATA_OFF;
244
245 bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
246 bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_ON | data);
247 bus_space_write_1(iot, ioh, off, RTII_WREN_ON | RTII_CLCK_OFF | data);
248 }
249
250 u_int32_t
251 rtii_hw_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
252 {
253 u_int8_t i;
254 u_int32_t res = 0;
255
256 bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
257 DELAY(6);
258
259 i = 24;
260 while ( i-- ) {
261 bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_HIGH);
262 DELAY(6);
263 bus_space_write_1(iot, ioh, off, RTII_READ_CLOCK_LOW);
264 res |= bus_space_read_1(iot, ioh, off) & RTII_DATA_ON ? 1 : 0;
265 DELAY(6);
266 res <<= 1;
267 }
268
269 return (res & (TEA5757_DATA | TEA5757_FREQ)) >> 1;
270 }
271
272 int
273 rtii_get_info(void *v, struct radio_info *ri)
274 {
275 struct rtii_softc *sc = v;
276
277 ri->mute = sc->mute;
278 ri->volume = sc->vol ? 255 : 0;
279 ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
280 ri->caps = RTII_CAPABILITIES;
281 ri->rfreq = 0;
282 ri->lock = tea5757_decode_lock(sc->lock);
283
284 ri->freq = sc->freq = tea5757_decode_freq(rtii_hw_read(sc->tea.iot,
285 sc->tea.ioh, sc->tea.offset));
286
287 switch (bus_space_read_1(sc->tea.iot, sc->tea.ioh, 0)) {
288 case 0xFD:
289 ri->info = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO;
290 break;
291 case 0xFF:
292 ri->info = 0;
293 break;
294 default:
295 ri->info = RADIO_INFO_SIGNAL;
296 }
297
298 return (0);
299 }
300
301 int
302 rtii_set_info(void *v, struct radio_info *ri)
303 {
304 struct rtii_softc *sc = v;
305
306 sc->mute = ri->mute ? 1 : 0;
307 sc->vol = ri->volume ? 255 : 0;
308 sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
309 sc->lock = tea5757_encode_lock(ri->lock);
310 ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
311 sc->lock, sc->stereo, ri->freq);
312 rtii_set_mute(sc);
313
314 return (0);
315 }
316
317 int
318 rtii_search(void *v, int f)
319 {
320 struct rtii_softc *sc = v;
321
322 tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
323 rtii_set_mute(sc);
324
325 return (0);
326 }
327