gtp.c revision 1.5 1 /* $NetBSD: gtp.c,v 1.5 2003/01/31 00:07:42 thorpej Exp $ */
2 /* $OpenBSD: gtp.c,v 1.1 2002/06/03 16:13:21 mickey Exp $ */
3
4 /*
5 * Copyright (c) 2002 Vladimir Popov <jumbo (at) narod.ru>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* Gemtek PCI Radio Card Device Driver */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/errno.h>
35 #include <sys/ioctl.h>
36 #include <sys/proc.h>
37 #include <sys/radioio.h>
38
39 #include <machine/bus.h>
40
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcidevs.h>
44
45 #include <dev/ic/tea5757.h>
46 #include <dev/radio_if.h>
47
48 #define PCI_CBIO 0x10
49
50 int gtp_match(struct device *, struct cfdata *, void *);
51 void gtp_attach(struct device *, struct device *, void *);
52
53 int gtp_get_info(void *, struct radio_info *);
54 int gtp_set_info(void *, struct radio_info *);
55 int gtp_search(void *, int);
56
57 #define GEMTEK_PCI_CAPS RADIO_CAPS_DETECT_SIGNAL | \
58 RADIO_CAPS_DETECT_STEREO | \
59 RADIO_CAPS_SET_MONO | \
60 RADIO_CAPS_HW_SEARCH | \
61 RADIO_CAPS_HW_AFC | \
62 RADIO_CAPS_LOCK_SENSITIVITY
63
64 #define GEMTEK_PCI_MUTE 0x00
65 #define GEMTEK_PCI_RSET 0x10
66
67 #define GEMTEK_PCI_SIGNAL 0x08
68 #define GEMTEK_PCI_STEREO 0x08
69
70 #define GTP_WREN_ON (1 << 2)
71 #define GTP_WREN_OFF (0 << 2)
72
73 #define GTP_DATA_ON (1 << 1)
74 #define GTP_DATA_OFF (0 << 1)
75
76 #define GTP_CLCK_ON (1 << 0)
77 #define GTP_CLCK_OFF (0 << 0)
78
79 #define GTP_READ_CLOCK_LOW (GTP_WREN_OFF | GTP_DATA_ON | GTP_CLCK_OFF)
80 #define GTP_READ_CLOCK_HIGH (GTP_WREN_OFF | GTP_DATA_ON | GTP_CLCK_ON)
81
82 /* define our interface to the high-level radio driver */
83
84 struct radio_hw_if gtp_hw_if = {
85 NULL, /* open */
86 NULL, /* close */
87 gtp_get_info,
88 gtp_set_info,
89 gtp_search
90 };
91
92 struct gtp_softc {
93 struct device sc_dev;
94
95 int mute;
96 u_int8_t vol;
97 u_int32_t freq;
98 u_int32_t stereo;
99 u_int32_t lock;
100
101 struct tea5757_t tea;
102 };
103
104 CFATTACH_DECL(gtp, sizeof(struct gtp_softc),
105 gtp_match, gtp_attach, NULL, NULL);
106
107 void gtp_set_mute(struct gtp_softc *);
108 void gtp_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
109 void gtp_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
110 void gtp_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
111 int gtp_state(bus_space_tag_t, bus_space_handle_t);
112 u_int32_t gtp_hardware_read(bus_space_tag_t, bus_space_handle_t,
113 bus_size_t);
114
115 int
116 gtp_match(struct device *parent, struct cfdata *cf, void *aux)
117 {
118 struct pci_attach_args *pa = aux;
119 /* FIXME:
120 * Guillemot produces the card that
121 * was originally developed by Gemtek
122 */
123 #if 0
124 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GEMTEK &&
125 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GEMTEK_PR103)
126 return (1);
127 #else
128 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GUILLEMOT &&
129 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GUILLEMOT_MAXIRADIO)
130 return (1);
131 #endif
132 return (0);
133 }
134
135 void
136 gtp_attach(struct device *parent, struct device *self, void *aux)
137 {
138 struct gtp_softc *sc = (struct gtp_softc *) self;
139 struct pci_attach_args *pa = aux;
140 struct cfdata *cf = sc->sc_dev.dv_cfdata;
141 pci_chipset_tag_t pc = pa->pa_pc;
142 bus_size_t iosize;
143 pcireg_t csr;
144 char devinfo[256];
145
146 aprint_naive(": Radio controller\n");
147
148 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
149 aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
150 PCI_REVISION(pa->pa_class));
151
152 /* Map I/O registers */
153 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &sc->tea.iot,
154 &sc->tea.ioh, NULL, &iosize)) {
155 aprint_error(": can't map i/o space\n");
156 return;
157 }
158
159 /* Enable the card */
160 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
161 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
162 csr | PCI_COMMAND_MASTER_ENABLE);
163
164 sc->vol = 0;
165 sc->mute = 0;
166 sc->freq = MIN_FM_FREQ;
167 sc->stereo = TEA5757_STEREO;
168 sc->lock = TEA5757_S030;
169 sc->tea.offset = 0;
170 sc->tea.flags = cf->cf_flags;
171 sc->tea.init = gtp_init;
172 sc->tea.rset = gtp_rset;
173 sc->tea.write_bit = gtp_write_bit;
174 sc->tea.read = gtp_hardware_read;
175
176 aprint_normal(": Gemtek PR103\n");
177
178 radio_attach_mi(>p_hw_if, sc, &sc->sc_dev);
179 }
180
181 int
182 gtp_get_info(void *v, struct radio_info *ri)
183 {
184 struct gtp_softc *sc = v;
185
186 ri->mute = sc->mute;
187 ri->volume = sc->vol ? 255 : 0;
188 ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
189 ri->caps = GEMTEK_PCI_CAPS;
190 ri->rfreq = 0;
191 ri->lock = tea5757_decode_lock(sc->lock);
192
193 /* Frequency read unsupported */
194 ri->freq = sc->freq;
195
196 ri->info = gtp_state(sc->tea.iot, sc->tea.ioh);
197 gtp_set_mute(sc);
198
199 return (0);
200 }
201
202 int
203 gtp_set_info(void *v, struct radio_info *ri)
204 {
205 struct gtp_softc *sc = v;
206
207 sc->mute = ri->mute ? 1 : 0;
208 sc->vol = ri->volume ? 255 : 0;
209 sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
210 sc->lock = tea5757_encode_lock(ri->lock);
211 ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
212 sc->lock, sc->stereo, ri->freq);
213 gtp_set_mute(sc);
214
215 return (0);
216 }
217
218 int
219 gtp_search(void *v, int f)
220 {
221 struct gtp_softc *sc = v;
222
223 tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
224 gtp_set_mute(sc);
225
226 return (0);
227 }
228
229 void
230 gtp_set_mute(struct gtp_softc *sc)
231 {
232 if (sc->mute || !sc->vol)
233 bus_space_write_2(sc->tea.iot, sc->tea.ioh, 0, GEMTEK_PCI_MUTE);
234 else
235 sc->freq = tea5757_set_freq(&sc->tea,
236 sc->lock, sc->stereo, sc->freq);
237 }
238
239 void
240 gtp_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
241 int bit)
242 {
243 u_int8_t data;
244
245 data = bit ? GTP_DATA_ON : GTP_DATA_OFF;
246 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
247 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_ON | data);
248 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
249 }
250
251 void
252 gtp_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
253 {
254 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_DATA_ON | GTP_CLCK_OFF);
255 }
256
257 void
258 gtp_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
259 {
260 bus_space_write_1(iot, ioh, off, GEMTEK_PCI_RSET);
261 }
262
263 u_int32_t
264 gtp_hardware_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
265 {
266 /* UNSUPPORTED */
267 return 0;
268 }
269
270 int
271 gtp_state(bus_space_tag_t iot, bus_space_handle_t ioh)
272 {
273 int ret;
274
275 bus_space_write_2(iot, ioh, 0,
276 GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_OFF);
277 ret = bus_space_read_2(iot, ioh, 0) &
278 GEMTEK_PCI_STEREO? 0 : RADIO_INFO_STEREO;
279 bus_space_write_2(iot, ioh, 0,
280 GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_ON);
281 ret |= bus_space_read_2(iot, ioh, 0) &
282 GEMTEK_PCI_SIGNAL? 0 : RADIO_INFO_SIGNAL;
283
284 return ret;
285 }
286