1 /* $NetBSD: sf16fmr2.c,v 1.14 2009/05/12 08:44:19 cegger 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 $ */
6 * Copyright (c) 2001 Maxim Tsyplakov <tm@oganer.net>,
7 * Vladimir Popov <jumbo@narod.ru>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 /* SoundForte RadioLink SF16-FMR2 FM Radio Card device driver */
34 * Philips TEA5757H AM/FM Self Tuned Radio:
35 * http://www.semiconductors.philips.com/pip/TEA5757H
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sf16fmr2.c,v 1.14 2009/05/12 08:44:19 cegger Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/device.h>
47 #include <sys/radioio.h>
49 #include <dev/isa/isavar.h>
50 #include <dev/radio_if.h>
51 #include <dev/ic/tea5757.h>
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 | \
61 #define SF16FMR2_AMPLIFIER (1 << 7)
62 #define SF16FMR2_SIGNAL (1 << 3)
63 #define SF16FMR2_STEREO (1 << 3)
65 #define SF16FMR2_MUTE 0x00
66 #define SF16FMR2_UNMUTE 0x04
68 #define SF16FMR2_DATA_ON (1 << 0)
69 #define SF16FMR2_DATA_OFF (0 << 0)
71 #define SF16FMR2_CLCK_ON (1 << 1)
72 #define SF16FMR2_CLCK_OFF (0 << 1)
74 #define SF16FMR2_WREN_ON (0 << 2) /* SF16-FMR2 has inverse WREN */
75 #define SF16FMR2_WREN_OFF (1 << 2)
77 #define SF16FMR2_READ_CLOCK_LOW \
78 SF16FMR2_DATA_ON | SF16FMR2_CLCK_OFF | SF16FMR2_WREN_OFF
80 #define SF16FMR2_READ_CLOCK_HIGH \
81 SF16FMR2_DATA_ON | SF16FMR2_CLCK_ON | SF16FMR2_WREN_OFF
83 int sf2r_probe(device_t
, cfdata_t
, void *);
84 void sf2r_attach(device_t
, device_t self
, void *);
86 int sf2r_get_info(void *, struct radio_info
*);
87 int sf2r_set_info(void *, struct radio_info
*);
88 int sf2r_search(void *, int);
90 /* define our interface to the higher level radio driver */
91 const struct radio_hw_if sf2r_hw_if
= {
100 struct device sc_dev
;
108 struct tea5757_t tea
;
111 CFATTACH_DECL(sf2r
, sizeof(struct sf2r_softc
),
112 sf2r_probe
, sf2r_attach
, NULL
, NULL
);
114 void sf2r_set_mute(struct sf2r_softc
*);
115 int sf2r_find(bus_space_tag_t
, bus_space_handle_t
);
117 u_int32_t
sf2r_read_register(bus_space_tag_t
, bus_space_handle_t
, bus_size_t
);
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);
124 sf2r_probe(device_t parent
, cfdata_t cf
, void *aux
)
126 struct isa_attach_args
*ia
= aux
;
127 bus_space_tag_t iot
= ia
->ia_iot
;
128 bus_space_handle_t ioh
;
130 int iosize
= 1, iobase
;
132 if (ISA_DIRECT_CONFIG(ia
))
138 iobase
= ia
->ia_io
[0].ir_addr
;
140 if (!SF16FMR2_BASE_VALID(iobase
)) {
141 printf("sf2r: configured iobase 0x%x invalid\n", iobase
);
145 if (bus_space_map(iot
, iobase
, iosize
, 0, &ioh
))
148 r
= sf2r_find(iot
, ioh
);
150 bus_space_unmap(iot
, ioh
, iosize
);
154 ia
->ia_io
[0].ir_size
= iosize
;
167 sf2r_attach(device_t parent
, device_t self
, void *aux
)
169 struct sf2r_softc
*sc
= (void *) self
;
170 struct isa_attach_args
*ia
= aux
;
172 sc
->tea
.iot
= ia
->ia_iot
;
176 sc
->freq
= MIN_FM_FREQ
;
177 sc
->stereo
= TEA5757_STEREO
;
178 sc
->lock
= TEA5757_S030
;
181 if (bus_space_map(sc
->tea
.iot
, ia
->ia_io
[0].ir_addr
,
182 ia
->ia_io
[0].ir_size
, 0, &sc
->tea
.ioh
))
183 panic("sf2rattach: bus_space_map() failed");
187 sc
->tea
.init
= sf2r_init
;
188 sc
->tea
.rset
= sf2r_rset
;
189 sc
->tea
.write_bit
= sf2r_write_bit
;
190 sc
->tea
.read
= sf2r_read_register
;
192 printf(": SoundForte RadioLink SF16-FMR2\n");
193 tea5757_set_freq(&sc
->tea
, sc
->stereo
, sc
->lock
, sc
->freq
);
196 radio_attach_mi(&sf2r_hw_if
, sc
, &sc
->sc_dev
);
200 * Mute/unmute the card
203 sf2r_set_mute(struct sf2r_softc
*sc
)
207 mute
= (sc
->mute
|| !sc
->vol
) ? SF16FMR2_MUTE
: SF16FMR2_UNMUTE
;
208 bus_space_write_1(sc
->tea
.iot
, sc
->tea
.ioh
, 0, mute
);
210 bus_space_write_1(sc
->tea
.iot
, sc
->tea
.ioh
, 0, mute
);
214 sf2r_init(bus_space_tag_t iot
, bus_space_handle_t ioh
, bus_size_t off
,
217 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_MUTE
);
221 sf2r_rset(bus_space_tag_t iot
, bus_space_handle_t ioh
, bus_size_t off
,
224 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_MUTE
);
225 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_UNMUTE
);
229 sf2r_find(bus_space_tag_t iot
, bus_space_handle_t ioh
)
231 struct sf2r_softc sc
;
237 sc
.tea
.init
= sf2r_init
;
238 sc
.tea
.rset
= sf2r_rset
;
239 sc
.tea
.write_bit
= sf2r_write_bit
;
240 sc
.tea
.read
= sf2r_read_register
;
241 sc
.lock
= TEA5757_S030
;
242 sc
.stereo
= TEA5757_STEREO
;
244 if ((bus_space_read_1(iot
, ioh
, 0) & 0x70) == 0x30) {
246 * Let's try to write and read a frequency.
247 * If the written and read frequencies are
248 * the same then success.
250 sc
.freq
= MIN_FM_FREQ
;
251 tea5757_set_freq(&sc
.tea
, sc
.stereo
, sc
.lock
, sc
.freq
);
253 freq
= sf2r_read_register(iot
, ioh
, sc
.tea
.offset
);
254 if (tea5757_decode_freq(freq
, 0) == sc
.freq
)
262 sf2r_write_bit(bus_space_tag_t iot
, bus_space_handle_t ioh
, bus_size_t off
, int bit
)
266 data
= bit
? SF16FMR2_DATA_ON
: SF16FMR2_DATA_OFF
;
268 bus_space_write_1(iot
, ioh
, off
,
269 SF16FMR2_WREN_ON
| SF16FMR2_CLCK_OFF
| data
);
270 bus_space_write_1(iot
, ioh
, off
,
271 SF16FMR2_WREN_ON
| SF16FMR2_CLCK_ON
| data
);
272 bus_space_write_1(iot
, ioh
, off
,
273 SF16FMR2_WREN_ON
| SF16FMR2_CLCK_OFF
| data
);
277 sf2r_read_register(bus_space_tag_t iot
, bus_space_handle_t ioh
, bus_size_t off
)
280 u_int8_t i
, state
= 0;
282 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_READ_CLOCK_LOW
);
284 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_READ_CLOCK_HIGH
);
286 i
= bus_space_read_1(iot
, ioh
, off
);
288 /* Amplifier: 0 - not present, 1 - present */
289 state
= i
& SF16FMR2_AMPLIFIER
? (1 << 2) : (0 << 2);
290 /* Signal: 0 - not tuned, 1 - tuned */
291 state
|= i
& SF16FMR2_SIGNAL
? (0 << 1) : (1 << 1);
293 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_READ_CLOCK_LOW
);
294 i
= bus_space_read_1(iot
, ioh
, off
);
295 /* Stereo: 0 - mono, 1 - stereo */
296 state
|= i
& SF16FMR2_STEREO
? (0 << 0) : (1 << 0);
297 res
= i
& SF16FMR2_DATA_ON
;
303 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_READ_CLOCK_HIGH
);
305 bus_space_write_1(iot
, ioh
, off
, SF16FMR2_READ_CLOCK_LOW
);
306 res
|= bus_space_read_1(iot
, ioh
, off
) & SF16FMR2_DATA_ON
;
309 return res
| (state
<< 24);
313 sf2r_get_info(void *v
, struct radio_info
*ri
)
315 struct sf2r_softc
*sc
= v
;
319 ri
->volume
= sc
->vol
? 255 : 0;
320 ri
->stereo
= sc
->stereo
== TEA5757_STEREO
? 1 : 0;
321 ri
->caps
= SF16FMR2_CAPABILITIES
;
323 ri
->lock
= tea5757_decode_lock(sc
->lock
);
325 buf
= sf2r_read_register(sc
->tea
.iot
, sc
->tea
.ioh
, sc
->tea
.offset
);
326 ri
->freq
= sc
->freq
= tea5757_decode_freq(buf
, 0);
327 ri
->info
= 3 & (buf
>> 24);
333 sf2r_set_info(void *v
, struct radio_info
*ri
)
335 struct sf2r_softc
*sc
= v
;
337 sc
->mute
= ri
->mute
? 1 : 0;
338 sc
->vol
= ri
->volume
? 255 : 0;
339 sc
->stereo
= ri
->stereo
? TEA5757_STEREO
: TEA5757_MONO
;
340 sc
->lock
= tea5757_encode_lock(ri
->lock
);
341 ri
->freq
= sc
->freq
= tea5757_set_freq(&sc
->tea
,
342 sc
->lock
, sc
->stereo
, ri
->freq
);
349 sf2r_search(void *v
, int f
)
351 struct sf2r_softc
*sc
= v
;
353 tea5757_search(&sc
->tea
, sc
->lock
, sc
->stereo
, f
);