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