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