xref: /netbsd-src/sys/dev/isa/sf16fmr2.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $NetBSD: sf16fmr2.c,v 1.13 2006/11/16 01:33:00 christos Exp $ */
2 /* $OpenBSD: sf16fmr2.c,v 1.3 2001/12/18 18:48:08 mickey Exp $ */
3 /* $RuOBSD: sf16fmr2.c,v 1.12 2001/10/18 16:51:36 pva Exp $ */
4 
5 /*
6  * Copyright (c) 2001 Maxim Tsyplakov <tm@oganer.net>,
7  *                    Vladimir Popov <jumbo@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 /* SoundForte RadioLink SF16-FMR2 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/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sf16fmr2.c,v 1.13 2006/11/16 01:33:00 christos Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/device.h>
47 #include <sys/radioio.h>
48 
49 #include <dev/isa/isavar.h>
50 #include <dev/radio_if.h>
51 #include <dev/ic/tea5757.h>
52 
53 #define SF16FMR2_BASE_VALID(x)	(x == 0x384)
54 #define SF16FMR2_CAPABILITIES	RADIO_CAPS_DETECT_STEREO |		\
55 				RADIO_CAPS_DETECT_SIGNAL |		\
56 				RADIO_CAPS_SET_MONO |			\
57 				RADIO_CAPS_LOCK_SENSITIVITY |		\
58 				RADIO_CAPS_HW_AFC |			\
59 				RADIO_CAPS_HW_SEARCH
60 
61 #define SF16FMR2_AMPLIFIER	(1 << 7)
62 #define SF16FMR2_SIGNAL		(1 << 3)
63 #define SF16FMR2_STEREO		(1 << 3)
64 
65 #define SF16FMR2_MUTE		0x00
66 #define SF16FMR2_UNMUTE		0x04
67 
68 #define SF16FMR2_DATA_ON	(1 << 0)
69 #define SF16FMR2_DATA_OFF	(0 << 0)
70 
71 #define SF16FMR2_CLCK_ON	(1 << 1)
72 #define SF16FMR2_CLCK_OFF	(0 << 1)
73 
74 #define SF16FMR2_WREN_ON	(0 << 2)  /* SF16-FMR2 has inverse WREN */
75 #define SF16FMR2_WREN_OFF	(1 << 2)
76 
77 #define SF16FMR2_READ_CLOCK_LOW		\
78 		SF16FMR2_DATA_ON | SF16FMR2_CLCK_OFF | SF16FMR2_WREN_OFF
79 
80 #define SF16FMR2_READ_CLOCK_HIGH	\
81 		SF16FMR2_DATA_ON | SF16FMR2_CLCK_ON | SF16FMR2_WREN_OFF
82 
83 int	sf2r_probe(struct device *, struct cfdata *, void *);
84 void	sf2r_attach(struct device *, struct device * self, void *);
85 
86 int	sf2r_get_info(void *, struct radio_info *);
87 int	sf2r_set_info(void *, struct radio_info *);
88 int	sf2r_search(void *, int);
89 
90 /* define our interface to the higher level radio driver */
91 const struct radio_hw_if sf2r_hw_if = {
92 	NULL, /* open */
93 	NULL, /* close */
94 	sf2r_get_info,
95 	sf2r_set_info,
96 	sf2r_search
97 };
98 
99 struct sf2r_softc {
100 	struct device	sc_dev;
101 
102 	u_int32_t	freq;
103 	u_int32_t	stereo;
104 	u_int32_t	lock;
105 	u_int8_t	vol;
106 	int	mute;
107 
108 	struct tea5757_t	tea;
109 };
110 
111 CFATTACH_DECL(sf2r, sizeof(struct sf2r_softc),
112     sf2r_probe, sf2r_attach, NULL, NULL);
113 
114 void	sf2r_set_mute(struct sf2r_softc *);
115 int	sf2r_find(bus_space_tag_t, bus_space_handle_t);
116 
117 u_int32_t	sf2r_read_register(bus_space_tag_t, bus_space_handle_t, bus_size_t);
118 
119 void	sf2r_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
120 void	sf2r_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
121 void	sf2r_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
122 
123 int
124 sf2r_probe(struct device *parent, struct cfdata *cf,
125     void *aux)
126 {
127 	struct isa_attach_args *ia = aux;
128 	bus_space_tag_t iot = ia->ia_iot;
129 	bus_space_handle_t ioh;
130 	u_int r;
131 	int iosize = 1, iobase;
132 
133 	if (ISA_DIRECT_CONFIG(ia))
134 		return 0;
135 
136 	if (ia->ia_nio < 1)
137 		return 0;
138 
139 	iobase = ia->ia_io[0].ir_addr;
140 
141 	if (!SF16FMR2_BASE_VALID(iobase)) {
142 		printf("sf2r: configured iobase 0x%x invalid\n", iobase);
143 		return 0;
144 	}
145 
146 	if (bus_space_map(iot, iobase, iosize, 0, &ioh))
147 		return 0;
148 
149 	r = sf2r_find(iot, ioh);
150 
151 	bus_space_unmap(iot, ioh, iosize);
152 
153 	if (r != 0) {
154 		ia->ia_nio = 1;
155 		ia->ia_io[0].ir_size = iosize;
156 
157 		ia->ia_niomem = 0;
158 		ia->ia_nirq = 0;
159 		ia->ia_ndrq = 0;
160 
161 		return (1);
162 	}
163 
164 	return (0);
165 }
166 
167 void
168 sf2r_attach(struct device *parent, struct device *self, void *aux)
169 {
170 	struct sf2r_softc *sc = (void *) self;
171 	struct isa_attach_args *ia = aux;
172 
173 	sc->tea.iot = ia->ia_iot;
174 	sc->tea.flags = 0;
175 	sc->mute = 0;
176 	sc->vol = 0;
177 	sc->freq = MIN_FM_FREQ;
178 	sc->stereo = TEA5757_STEREO;
179 	sc->lock = TEA5757_S030;
180 
181 	/* remap I/O */
182 	if (bus_space_map(sc->tea.iot, ia->ia_io[0].ir_addr,
183 	    ia->ia_io[0].ir_size, 0, &sc->tea.ioh))
184 		panic("sf2rattach: bus_space_map() failed");
185 
186 	sc->tea.offset = 0;
187 
188 	sc->tea.init = sf2r_init;
189 	sc->tea.rset = sf2r_rset;
190 	sc->tea.write_bit = sf2r_write_bit;
191 	sc->tea.read = sf2r_read_register;
192 
193 	printf(": SoundForte RadioLink SF16-FMR2\n");
194 	tea5757_set_freq(&sc->tea, sc->stereo, sc->lock, sc->freq);
195 	sf2r_set_mute(sc);
196 
197 	radio_attach_mi(&sf2r_hw_if, sc, &sc->sc_dev);
198 }
199 
200 /*
201  * Mute/unmute the card
202  */
203 void
204 sf2r_set_mute(struct sf2r_softc *sc)
205 {
206 	u_int8_t mute;
207 
208 	mute = (sc->mute || !sc->vol) ? SF16FMR2_MUTE : SF16FMR2_UNMUTE;
209 	bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
210 	DELAY(64);
211 	bus_space_write_1(sc->tea.iot, sc->tea.ioh, 0, mute);
212 }
213 
214 void
215 sf2r_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
216     u_int32_t d)
217 {
218 	bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
219 }
220 
221 void
222 sf2r_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
223     u_int32_t d)
224 {
225 	bus_space_write_1(iot, ioh, off, SF16FMR2_MUTE);
226 	bus_space_write_1(iot, ioh, off, SF16FMR2_UNMUTE);
227 }
228 
229 int
230 sf2r_find(bus_space_tag_t iot, bus_space_handle_t ioh)
231 {
232 	struct sf2r_softc sc;
233 	u_int32_t freq;
234 
235 	sc.tea.iot = iot;
236 	sc.tea.ioh = ioh;
237 	sc.tea.offset = 0;
238 	sc.tea.init = sf2r_init;
239 	sc.tea.rset = sf2r_rset;
240 	sc.tea.write_bit = sf2r_write_bit;
241 	sc.tea.read = sf2r_read_register;
242 	sc.lock = TEA5757_S030;
243 	sc.stereo = TEA5757_STEREO;
244 
245 	if ((bus_space_read_1(iot, ioh, 0) & 0x70) == 0x30) {
246 		/*
247 		 * Let's try to write and read a frequency.
248 		 * If the written and read frequencies are
249 		 * the same then success.
250 		 */
251 		sc.freq = MIN_FM_FREQ;
252 		tea5757_set_freq(&sc.tea, sc.stereo, sc.lock, sc.freq);
253 		sf2r_set_mute(&sc);
254 		freq = sf2r_read_register(iot, ioh, sc.tea.offset);
255 		if (tea5757_decode_freq(freq, 0) == sc.freq)
256 			return 1;
257 	}
258 
259 	return 0;
260 }
261 
262 void
263 sf2r_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, int bit)
264 {
265 	u_int8_t data;
266 
267 	data = bit ? SF16FMR2_DATA_ON : SF16FMR2_DATA_OFF;
268 
269 	bus_space_write_1(iot, ioh, off,
270 			SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
271 	bus_space_write_1(iot, ioh, off,
272 			SF16FMR2_WREN_ON | SF16FMR2_CLCK_ON  | data);
273 	bus_space_write_1(iot, ioh, off,
274 			SF16FMR2_WREN_ON | SF16FMR2_CLCK_OFF | data);
275 }
276 
277 u_int32_t
278 sf2r_read_register(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
279 {
280 	u_int32_t res = 0;
281 	u_int8_t i, state = 0;
282 
283 	bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
284 	DELAY(6);
285 	bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
286 
287 	i = bus_space_read_1(iot, ioh, off);
288 	DELAY(6);
289 	/* Amplifier: 0 - not present, 1 - present */
290 	state = i & SF16FMR2_AMPLIFIER ? (1 << 2) : (0 << 2);
291 	/* Signal: 0 - not tuned, 1 - tuned */
292 	state |= i & SF16FMR2_SIGNAL   ? (0 << 1) : (1 << 1);
293 
294 	bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
295 	i = bus_space_read_1(iot, ioh, off);
296 	/* Stereo: 0 - mono, 1 - stereo */
297 	state |= i & SF16FMR2_STEREO   ? (0 << 0) : (1 << 0);
298 	res = i & SF16FMR2_DATA_ON;
299 
300 	i = 23;
301 	while ( i-- ) {
302 		DELAY(6);
303 		res <<= 1;
304 		bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_HIGH);
305 		DELAY(6);
306 		bus_space_write_1(iot, ioh, off, SF16FMR2_READ_CLOCK_LOW);
307 		res |= bus_space_read_1(iot, ioh, off) & SF16FMR2_DATA_ON;
308 	}
309 
310 	return res | (state << 24);
311 }
312 
313 int
314 sf2r_get_info(void *v, struct radio_info *ri)
315 {
316 	struct sf2r_softc *sc = v;
317 	u_int32_t buf;
318 
319 	ri->mute = sc->mute;
320 	ri->volume = sc->vol ? 255 : 0;
321 	ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
322 	ri->caps = SF16FMR2_CAPABILITIES;
323 	ri->rfreq = 0;
324 	ri->lock = tea5757_decode_lock(sc->lock);
325 
326 	buf = sf2r_read_register(sc->tea.iot, sc->tea.ioh, sc->tea.offset);
327 	ri->freq  = sc->freq = tea5757_decode_freq(buf, 0);
328 	ri->info = 3 & (buf >> 24);
329 
330 	return (0);
331 }
332 
333 int
334 sf2r_set_info(void *v, struct radio_info *ri)
335 {
336 	struct sf2r_softc *sc = v;
337 
338 	sc->mute = ri->mute ? 1 : 0;
339 	sc->vol = ri->volume ? 255 : 0;
340 	sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
341 	sc->lock = tea5757_encode_lock(ri->lock);
342 	ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
343 			sc->lock, sc->stereo, ri->freq);
344 	sf2r_set_mute(sc);
345 
346 	return (0);
347 }
348 
349 int
350 sf2r_search(void *v, int f)
351 {
352 	struct sf2r_softc *sc = v;
353 
354 	tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
355 	sf2r_set_mute(sc);
356 
357 	return (0);
358 }
359