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