xref: /netbsd/sys/dev/pci/gtp.c (revision c4a72b64)
1 /* $NetBSD: gtp.c,v 1.4 2002/10/02 16:51:16 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@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 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
147 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
148 
149 	/* Map I/O registers */
150 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &sc->tea.iot,
151 	    &sc->tea.ioh, NULL, &iosize)) {
152 		printf(": can't map i/o space\n");
153 		return;
154 	}
155 
156 	/* Enable the card */
157 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
158 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
159 	    csr | PCI_COMMAND_MASTER_ENABLE);
160 
161 	sc->vol = 0;
162 	sc->mute = 0;
163 	sc->freq = MIN_FM_FREQ;
164 	sc->stereo = TEA5757_STEREO;
165 	sc->lock = TEA5757_S030;
166 	sc->tea.offset = 0;
167 	sc->tea.flags = cf->cf_flags;
168 	sc->tea.init = gtp_init;
169 	sc->tea.rset = gtp_rset;
170 	sc->tea.write_bit = gtp_write_bit;
171 	sc->tea.read = gtp_hardware_read;
172 
173 	printf(": Gemtek PR103\n");
174 
175 	radio_attach_mi(&gtp_hw_if, sc, &sc->sc_dev);
176 }
177 
178 int
179 gtp_get_info(void *v, struct radio_info *ri)
180 {
181 	struct gtp_softc *sc = v;
182 
183 	ri->mute = sc->mute;
184 	ri->volume = sc->vol ? 255 : 0;
185 	ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
186 	ri->caps = GEMTEK_PCI_CAPS;
187 	ri->rfreq = 0;
188 	ri->lock = tea5757_decode_lock(sc->lock);
189 
190 	/* Frequency read unsupported */
191 	ri->freq = sc->freq;
192 
193 	ri->info = gtp_state(sc->tea.iot, sc->tea.ioh);
194 	gtp_set_mute(sc);
195 
196 	return (0);
197 }
198 
199 int
200 gtp_set_info(void *v, struct radio_info *ri)
201 {
202 	struct gtp_softc *sc = v;
203 
204 	sc->mute = ri->mute ? 1 : 0;
205 	sc->vol = ri->volume ? 255 : 0;
206 	sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
207 	sc->lock = tea5757_encode_lock(ri->lock);
208 	ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
209 	    sc->lock, sc->stereo, ri->freq);
210 	gtp_set_mute(sc);
211 
212 	return (0);
213 }
214 
215 int
216 gtp_search(void *v, int f)
217 {
218 	struct gtp_softc *sc = v;
219 
220 	tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
221 	gtp_set_mute(sc);
222 
223 	return (0);
224 }
225 
226 void
227 gtp_set_mute(struct gtp_softc *sc)
228 {
229 	if (sc->mute || !sc->vol)
230 		bus_space_write_2(sc->tea.iot, sc->tea.ioh, 0, GEMTEK_PCI_MUTE);
231 	else
232 		sc->freq = tea5757_set_freq(&sc->tea,
233 		    sc->lock, sc->stereo, sc->freq);
234 }
235 
236 void
237 gtp_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
238 		int bit)
239 {
240 	u_int8_t data;
241 
242 	data = bit ? GTP_DATA_ON : GTP_DATA_OFF;
243 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
244 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_ON  | data);
245 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
246 }
247 
248 void
249 gtp_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
250 {
251 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_DATA_ON | GTP_CLCK_OFF);
252 }
253 
254 void
255 gtp_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
256 {
257 	bus_space_write_1(iot, ioh, off, GEMTEK_PCI_RSET);
258 }
259 
260 u_int32_t
261 gtp_hardware_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
262 {
263 	/* UNSUPPORTED */
264 	return 0;
265 }
266 
267 int
268 gtp_state(bus_space_tag_t iot, bus_space_handle_t ioh)
269 {
270 	int ret;
271 
272 	bus_space_write_2(iot, ioh, 0,
273 	    GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_OFF);
274 	ret  = bus_space_read_2(iot, ioh, 0) &
275 	    GEMTEK_PCI_STEREO?  0 : RADIO_INFO_STEREO;
276 	bus_space_write_2(iot, ioh, 0,
277 	    GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_ON);
278 	ret |= bus_space_read_2(iot, ioh, 0) &
279 	    GEMTEK_PCI_SIGNAL?  0 : RADIO_INFO_SIGNAL;
280 
281 	return ret;
282 }
283