Home | History | Annotate | Line # | Download | only in pci
gtp.c revision 1.1.2.2
      1 /* $NetBSD: gtp.c,v 1.1.2.2 2002/09/06 08:45:13 jdolecek 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 struct cfattach gtp_ca = {
    105 	sizeof(struct gtp_softc), gtp_match, gtp_attach
    106 };
    107 
    108 void	gtp_set_mute(struct gtp_softc *);
    109 void	gtp_write_bit(bus_space_tag_t, bus_space_handle_t, bus_size_t, int);
    110 void	gtp_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    111 void	gtp_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    112 int	gtp_state(bus_space_tag_t, bus_space_handle_t);
    113 u_int32_t	gtp_hardware_read(bus_space_tag_t, bus_space_handle_t,
    114 		bus_size_t);
    115 
    116 int
    117 gtp_match(struct device *parent, struct cfdata *cf, void *aux)
    118 {
    119 	struct pci_attach_args *pa = aux;
    120 	/* FIXME:
    121 	 * Guillemot produces the card that
    122 	 * was originally developed by Gemtek
    123 	 */
    124 #if 0
    125 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GEMTEK &&
    126 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GEMTEK_PR103)
    127 		return (1);
    128 #else
    129 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_GUILLEMOT &&
    130 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_GUILLEMOT_MAXIRADIO)
    131 		return (1);
    132 #endif
    133 	return (0);
    134 }
    135 
    136 void
    137 gtp_attach(struct device *parent, struct device *self, void *aux)
    138 {
    139 	struct gtp_softc *sc = (struct gtp_softc *) self;
    140 	struct pci_attach_args *pa = aux;
    141 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    142 	pci_chipset_tag_t pc = pa->pa_pc;
    143 	bus_size_t iosize;
    144 	pcireg_t csr;
    145 	char devinfo[256];
    146 
    147 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    148 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    149 
    150 	/* Map I/O registers */
    151 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &sc->tea.iot,
    152 	    &sc->tea.ioh, NULL, &iosize)) {
    153 		printf(": can't map i/o space\n");
    154 		return;
    155 	}
    156 
    157 	/* Enable the card */
    158 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    159 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    160 	    csr | PCI_COMMAND_MASTER_ENABLE);
    161 
    162 	sc->vol = 0;
    163 	sc->mute = 0;
    164 	sc->freq = MIN_FM_FREQ;
    165 	sc->stereo = TEA5757_STEREO;
    166 	sc->lock = TEA5757_S030;
    167 	sc->tea.offset = 0;
    168 	sc->tea.flags = cf->cf_flags;
    169 	sc->tea.init = gtp_init;
    170 	sc->tea.rset = gtp_rset;
    171 	sc->tea.write_bit = gtp_write_bit;
    172 	sc->tea.read = gtp_hardware_read;
    173 
    174 	printf(": Gemtek PR103\n");
    175 
    176 	radio_attach_mi(&gtp_hw_if, sc, &sc->sc_dev);
    177 }
    178 
    179 int
    180 gtp_get_info(void *v, struct radio_info *ri)
    181 {
    182 	struct gtp_softc *sc = v;
    183 
    184 	ri->mute = sc->mute;
    185 	ri->volume = sc->vol ? 255 : 0;
    186 	ri->stereo = sc->stereo == TEA5757_STEREO ? 1 : 0;
    187 	ri->caps = GEMTEK_PCI_CAPS;
    188 	ri->rfreq = 0;
    189 	ri->lock = tea5757_decode_lock(sc->lock);
    190 
    191 	/* Frequency read unsupported */
    192 	ri->freq = sc->freq;
    193 
    194 	ri->info = gtp_state(sc->tea.iot, sc->tea.ioh);
    195 	gtp_set_mute(sc);
    196 
    197 	return (0);
    198 }
    199 
    200 int
    201 gtp_set_info(void *v, struct radio_info *ri)
    202 {
    203 	struct gtp_softc *sc = v;
    204 
    205 	sc->mute = ri->mute ? 1 : 0;
    206 	sc->vol = ri->volume ? 255 : 0;
    207 	sc->stereo = ri->stereo ? TEA5757_STEREO: TEA5757_MONO;
    208 	sc->lock = tea5757_encode_lock(ri->lock);
    209 	ri->freq = sc->freq = tea5757_set_freq(&sc->tea,
    210 	    sc->lock, sc->stereo, ri->freq);
    211 	gtp_set_mute(sc);
    212 
    213 	return (0);
    214 }
    215 
    216 int
    217 gtp_search(void *v, int f)
    218 {
    219 	struct gtp_softc *sc = v;
    220 
    221 	tea5757_search(&sc->tea, sc->lock, sc->stereo, f);
    222 	gtp_set_mute(sc);
    223 
    224 	return (0);
    225 }
    226 
    227 void
    228 gtp_set_mute(struct gtp_softc *sc)
    229 {
    230 	if (sc->mute || !sc->vol)
    231 		bus_space_write_2(sc->tea.iot, sc->tea.ioh, 0, GEMTEK_PCI_MUTE);
    232 	else
    233 		sc->freq = tea5757_set_freq(&sc->tea,
    234 		    sc->lock, sc->stereo, sc->freq);
    235 }
    236 
    237 void
    238 gtp_write_bit(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
    239 		int bit)
    240 {
    241 	u_int8_t data;
    242 
    243 	data = bit ? GTP_DATA_ON : GTP_DATA_OFF;
    244 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
    245 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_ON  | data);
    246 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_CLCK_OFF | data);
    247 }
    248 
    249 void
    250 gtp_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
    251 {
    252 	bus_space_write_1(iot, ioh, off, GTP_WREN_ON | GTP_DATA_ON | GTP_CLCK_OFF);
    253 }
    254 
    255 void
    256 gtp_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, u_int32_t d)
    257 {
    258 	bus_space_write_1(iot, ioh, off, GEMTEK_PCI_RSET);
    259 }
    260 
    261 u_int32_t
    262 gtp_hardware_read(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off)
    263 {
    264 	/* UNSUPPORTED */
    265 	return 0;
    266 }
    267 
    268 int
    269 gtp_state(bus_space_tag_t iot, bus_space_handle_t ioh)
    270 {
    271 	int ret;
    272 
    273 	bus_space_write_2(iot, ioh, 0,
    274 	    GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_OFF);
    275 	ret  = bus_space_read_2(iot, ioh, 0) &
    276 	    GEMTEK_PCI_STEREO?  0 : RADIO_INFO_STEREO;
    277 	bus_space_write_2(iot, ioh, 0,
    278 	    GTP_DATA_ON | GTP_WREN_OFF | GTP_CLCK_ON);
    279 	ret |= bus_space_read_2(iot, ioh, 0) &
    280 	    GEMTEK_PCI_SIGNAL?  0 : RADIO_INFO_SIGNAL;
    281 
    282 	return ret;
    283 }
    284