1 /* $NetBSD: gtp.c,v 1.17 2009/05/12 08:23:00 cegger 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/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: gtp.c,v 1.17 2009/05/12 08:23:00 cegger Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/errno.h> 38 #include <sys/ioctl.h> 39 #include <sys/proc.h> 40 #include <sys/radioio.h> 41 42 #include <sys/bus.h> 43 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pcidevs.h> 47 48 #include <dev/ic/tea5757.h> 49 #include <dev/radio_if.h> 50 51 #define PCI_CBIO 0x10 52 53 static int gtp_match(device_t, cfdata_t, void *); 54 static void gtp_attach(device_t, device_t, void *); 55 56 static int gtp_get_info(void *, struct radio_info *); 57 static int gtp_set_info(void *, struct radio_info *); 58 static int gtp_search(void *, int); 59 60 #define GEMTEK_PCI_CAPS RADIO_CAPS_DETECT_SIGNAL | \ 61 RADIO_CAPS_DETECT_STEREO | \ 62 RADIO_CAPS_SET_MONO | \ 63 RADIO_CAPS_HW_SEARCH | \ 64 RADIO_CAPS_HW_AFC | \ 65 RADIO_CAPS_LOCK_SENSITIVITY 66 67 #define GEMTEK_PCI_MUTE 0x00 68 #define GEMTEK_PCI_RSET 0x10 69 70 #define GEMTEK_PCI_SIGNAL 0x08 71 #define GEMTEK_PCI_STEREO 0x08 72 73 #define GTP_WREN_ON (1 << 2) 74 #define GTP_WREN_OFF (0 << 2) 75 76 #define GTP_DATA_ON (1 << 1) 77 #define GTP_DATA_OFF (0 << 1) 78 79 #define GTP_CLCK_ON (1 << 0) 80 #define GTP_CLCK_OFF (0 << 0) 81 82 #define GTP_READ_CLOCK_LOW (GTP_WREN_OFF | GTP_DATA_ON | GTP_CLCK_OFF) 83 #define GTP_READ_CLOCK_HIGH (GTP_WREN_OFF | GTP_DATA_ON | GTP_CLCK_ON) 84 85 /* define our interface to the high-level radio driver */ 86 87 static const struct radio_hw_if gtp_hw_if = { 88 NULL, /* open */ 89 NULL, /* close */ 90 gtp_get_info, 91 gtp_set_info, 92 gtp_search 93 }; 94 95 struct gtp_softc { 96 struct device sc_dev; 97 98 int mute; 99 u_int8_t vol; 100 u_int32_t freq; 101 u_int32_t stereo; 102 u_int32_t lock; 103 104 struct tea5757_t tea; 105 }; 106 107 CFATTACH_DECL(gtp, sizeof(struct gtp_softc), 108 gtp_match, gtp_attach, NULL, NULL); 109 110 static void gtp_set_mute(struct gtp_softc *); 111 static void gtp_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, 112 int); 113 static void gtp_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, 114 u_int32_t); 115 static void gtp_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, 116 u_int32_t); 117 static int gtp_state(bus_space_tag_t, bus_space_handle_t); 118 static uint32_t gtp_hardware_read(bus_space_tag_t, bus_space_handle_t, 119 bus_size_t); 120 121 static int 122 gtp_match(device_t parent, cfdata_t cf, void *aux) 123 { 124 struct pci_attach_args *pa = aux; 125 /* FIXME: 126 * Guillemot produces the card that 127 * was originally developed by Gemtek 128 */ 129 #if 0 130 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GEMTEK && 131 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GEMTEK_PR103) 132 return (1); 133 #else 134 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GUILLEMOT && 135 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GUILLEMOT_MAXIRADIO) 136 return (1); 137 #endif 138 return (0); 139 } 140 141 static void 142 gtp_attach(device_t parent, device_t self, void *aux) 143 { 144 struct gtp_softc *sc = device_private(self); 145 struct pci_attach_args *pa = aux; 146 cfdata_t cf = device_cfdata(&sc->sc_dev); 147 pci_chipset_tag_t pc = pa->pa_pc; 148 bus_size_t iosize; 149 pcireg_t csr; 150 char devinfo[256]; 151 152 aprint_naive(": Radio controller\n"); 153 154 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 155 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, 156 PCI_REVISION(pa->pa_class)); 157 158 /* Map I/O registers */ 159 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &sc->tea.iot, 160 &sc->tea.ioh, NULL, &iosize)) { 161 aprint_error(": can't map i/o space\n"); 162 return; 163 } 164 165 /* Enable the card */ 166 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 167 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 168 csr | PCI_COMMAND_MASTER_ENABLE); 169 170 sc->vol = 0; 171 sc->mute = 0; 172 sc->freq = MIN_FM_FREQ; 173 sc->stereo = TEA5757_STEREO; 174 sc->lock = TEA5757_S030; 175 sc->tea.offset = 0; 176 sc->tea.flags = cf->cf_flags; 177 sc->tea.init = gtp_init; 178 sc->tea.rset = gtp_rset; 179 sc->tea.write_bit = gtp_write_bit; 180 sc->tea.read = gtp_hardware_read; 181 182 aprint_normal(": Gemtek PR103\n"); 183 184 radio_attach_mi(>p_hw_if, sc, &sc->sc_dev); 185 } 186 187 static int 188 gtp_get_info(void *v, struct radio_info *ri) 189 { 190 struct gtp_softc *sc = v; 191 192 ri->mute = sc->mute; 193 ri->volume = sc->vol ? 255 : 0; 194 ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0; 195 ri->caps = GEMTEK_PCI_CAPS; 196 ri->rfreq = 0; 197 ri->lock = tea5757_decode_lock(sc->lock); 198 199 /* Frequency read unsupported */ 200 ri->freq = sc->freq; 201 202 ri->info = gtp_state(sc->tea.iot, sc->tea.ioh); 203 gtp_set_mute(sc); 204 205 return (0); 206 } 207 208 static int 209 gtp_set_info(void *v, struct radio_info *ri) 210 { 211 struct gtp_softc *sc = v; 212 213 sc->mute = ri->mute ? 1 : 0; 214 sc->vol = ri->volume ? 255 : 0; 215 sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO; 216 sc->lock = tea5757_encode_lock(ri->lock); 217 ri->freq = sc->freq = tea5757_set_freq(&sc->tea, 218 sc->lock, sc->stereo, ri->freq); 219 gtp_set_mute(sc); 220 221 return (0); 222 } 223 224 static int 225 gtp_search(void *v, int f) 226 { 227 struct gtp_softc *sc = v; 228 229 tea5757_search(&sc->tea, sc->lock, sc->stereo, f); 230 gtp_set_mute(sc); 231 232 return (0); 233 } 234 235 static void 236 gtp_set_mute(struct gtp_softc *sc) 237 { 238 if (sc->mute || !sc->vol) 239 bus_space_write_2(sc->tea.iot, sc->tea.ioh, 0, GEMTEK_PCI_MUTE); 240 else 241 sc->freq = tea5757_set_freq(&sc->tea, 242 sc->lock, sc->stereo, sc->freq); 243 } 244 245 static void 246 gtp_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, 247 int bit) 248 { 249 u_int8_t data; 250 251 data = bit ? GTP_DATA_ON : GTP_DATA_OFF; 252 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data); 253 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_ON | data); 254 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data); 255 } 256 257 static void 258 gtp_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, 259 u_int32_t d) 260 { 261 bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_DATA_ON | GTP_CLCK_OFF); 262 } 263 264 static void 265 gtp_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, 266 u_int32_t d) 267 { 268 bus_space_write_1(iot, ioh, off, GEMTEK_PCI_RSET); 269 } 270 271 static uint32_t 272 gtp_hardware_read(bus_space_tag_t iot, bus_space_handle_t ioh, 273 bus_size_t off) 274 { 275 /* UNSUPPORTED */ 276 return 0; 277 } 278 279 static int 280 gtp_state(bus_space_tag_t iot, bus_space_handle_t ioh) 281 { 282 int ret; 283 284 bus_space_write_2(iot, ioh, 0, 285 GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_OFF); 286 ret = bus_space_read_2(iot, ioh, 0) & 287 GEMTEK_PCI_STEREO? 0 : RADIO_INFO_STEREO; 288 bus_space_write_2(iot, ioh, 0, 289 GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_ON); 290 ret |= bus_space_read_2(iot, ioh, 0) & 291 GEMTEK_PCI_SIGNAL? 0 : RADIO_INFO_SIGNAL; 292 293 return ret; 294 } 295