Home | History | Annotate | Line # | Download | only in isa
aztech.c revision 1.6
      1 /* $NetBSD: aztech.c,v 1.6 2002/10/02 02:09:16 thorpej Exp $ */
      2 /* $OpenBSD: aztech.c,v 1.2 2001/12/05 10:27:06 mickey Exp $ */
      3 /* $RuOBSD: aztech.c,v 1.11 2001/10/20 13:23:47 pva Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2001 Maxim Tsyplakov <tm (at) oganer.net>,
      7  *                    Vladimir Popov <jumbo (at) narod.ru>
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /* Aztech/PackardBell FM Radio Card device driver */
     32 
     33 /*
     34  * Sanyo LM7001J Direct PLL Frequency Synthesizer:
     35  *     ??? See http://www.redsword.com/tjacobs/geeb/fmcard.htm
     36  *
     37  * Philips TEA5712T AM/FM Stereo DTS Radio:
     38  *     http://www.semiconductors.philips.com/pip/TEA5712
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/proc.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/device.h>
     47 #include <sys/radioio.h>
     48 
     49 #include <machine/bus.h>
     50 
     51 #include <dev/isa/isavar.h>
     52 #include <dev/ic/lm700x.h>
     53 #include <dev/radio_if.h>
     54 
     55 #define RF_25K	25
     56 #define RF_50K	50
     57 #define RF_100K	100
     58 
     59 #define MAX_VOL	3
     60 #define VOLUME_RATIO(x)	(255 * x / MAX_VOL)
     61 
     62 #define AZ_BASE_VALID(x)	((x == 0x350) || (x == 0x358))
     63 #define AZTECH_CAPABILITIES	RADIO_CAPS_DETECT_STEREO | 		\
     64 				RADIO_CAPS_DETECT_SIGNAL | 		\
     65 				RADIO_CAPS_SET_MONO | 			\
     66 				RADIO_CAPS_REFERENCE_FREQ
     67 
     68 #define	AZ_WREN_ON	(1 << 1)
     69 #define	AZ_WREN_OFF	(0 << 1)
     70 
     71 #define AZ_CLCK_ON	(1 << 6)
     72 #define AZ_CLCK_OFF	(0 << 6)
     73 
     74 #define AZ_DATA_ON	(1 << 7)
     75 #define AZ_DATA_OFF	(0 << 7)
     76 
     77 int	az_probe(struct device *, struct cfdata *, void *);
     78 void	az_attach(struct device *, struct device * self, void *);
     79 
     80 int	az_get_info(void *, struct radio_info *);
     81 int	az_set_info(void *, struct radio_info *);
     82 
     83 struct radio_hw_if az_hw_if = {
     84 	NULL,	/* open */
     85 	NULL,	/* close */
     86 	az_get_info,
     87 	az_set_info,
     88 	NULL
     89 };
     90 
     91 struct az_softc {
     92 	struct device	sc_dev;
     93 
     94 	int		mute;
     95 	u_int8_t	vol;
     96 	u_int32_t	freq;
     97 	u_int32_t	rf;
     98 	u_int32_t	stereo;
     99 
    100 	struct lm700x_t	lm;
    101 };
    102 
    103 CFATTACH_DECL(az, sizeof(struct az_softc),
    104 	az_probe, az_attach, NULL, NULL);
    105 
    106 u_int	az_find(bus_space_tag_t, bus_space_handle_t);
    107 void	az_set_mute(struct az_softc *);
    108 void	az_set_freq(struct az_softc *, u_int32_t);
    109 u_int8_t	az_state(bus_space_tag_t, bus_space_handle_t);
    110 
    111 void	az_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    112 void	az_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    113 
    114 u_int8_t	az_conv_vol(u_int8_t);
    115 u_int8_t	az_unconv_vol(u_int8_t);
    116 
    117 int
    118 az_probe(struct device *parent, struct cfdata *cf, void *aux)
    119 {
    120 	struct isa_attach_args *ia = aux;
    121 	bus_space_tag_t iot = ia->ia_iot;
    122 	bus_space_handle_t ioh;
    123 	u_int r;
    124 	int iosize = 1, iobase;
    125 
    126 	if (ISA_DIRECT_CONFIG(ia))
    127 		return 0;
    128 
    129 	if (ia->ia_nio < 1)
    130 		return 0;
    131 
    132 	iobase = ia->ia_io[0].ir_addr;
    133 
    134 	if (!AZ_BASE_VALID(iobase)) {
    135 		printf("az: configured iobase 0x%x invalid", iobase);
    136 		return 0;
    137 	}
    138 
    139 	if (bus_space_map(iot, iobase, iosize, 0, &ioh))
    140 		return 0;
    141 
    142 	r = az_find(iot, ioh);
    143 
    144 	bus_space_unmap(iot, ioh, iosize);
    145 
    146 	if (r != 0) {
    147 		ia->ia_nio = 1;
    148 		ia->ia_io[0].ir_size = iosize;
    149 
    150 		ia->ia_niomem = 0;
    151 		ia->ia_nirq = 0;
    152 		ia->ia_ndrq = 0;
    153 
    154 		return (1);
    155 	}
    156 
    157 	return (0);
    158 }
    159 
    160 void
    161 az_attach(struct device *parent, struct device *self, void *aux)
    162 {
    163 	struct az_softc *sc = (void *)self;
    164 	struct isa_attach_args *ia = aux;
    165 
    166 	sc->lm.iot = ia->ia_iot;
    167 	sc->rf = LM700X_REF_050;
    168 	sc->stereo = LM700X_STEREO;
    169 	sc->mute = 0;
    170 	sc->freq = MIN_FM_FREQ;
    171 	sc->vol = 0;
    172 
    173 	/* remap I/O */
    174 	if (bus_space_map(sc->lm.iot, ia->ia_io[0].ir_addr,
    175 	    ia->ia_io[0].ir_size, 0, &sc->lm.ioh))
    176 		panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
    177 
    178 	printf(": Aztech/PackardBell\n");
    179 
    180 	/* Configure struct lm700x_t lm */
    181 	sc->lm.offset = 0;
    182 	sc->lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
    183 	sc->lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
    184 	sc->lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
    185 	sc->lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
    186 	sc->lm.initdata = 0;
    187 	sc->lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
    188 	sc->lm.init = az_lm700x_init;
    189 	sc->lm.rset = az_lm700x_rset;
    190 
    191 	az_set_freq(sc, sc->freq);
    192 
    193 	radio_attach_mi(&az_hw_if, sc, &sc->sc_dev);
    194 }
    195 
    196 /*
    197  * Mute the card
    198  */
    199 void
    200 az_set_mute(struct az_softc *sc)
    201 {
    202 	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    203 	    sc->mute ? 0 : sc->vol);
    204 	delay(6);
    205 	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    206 	    sc->mute ? 0 : sc->vol);
    207 }
    208 
    209 void
    210 az_set_freq(struct az_softc *sc, u_int32_t nfreq)
    211 {
    212 	u_int8_t vol;
    213 	u_int32_t reg;
    214 
    215 	vol = sc->mute ? 0 : sc->vol;
    216 
    217 	if (nfreq > MAX_FM_FREQ)
    218 		nfreq = MAX_FM_FREQ;
    219 	if (nfreq < MIN_FM_FREQ)
    220 		nfreq = MIN_FM_FREQ;
    221 
    222 	sc->freq = nfreq;
    223 
    224 	reg = lm700x_encode_freq(nfreq, sc->rf);
    225 	reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
    226 
    227 	lm700x_hardware_write(&sc->lm, reg, vol);
    228 
    229 	az_set_mute(sc);
    230 }
    231 
    232 /*
    233  * Return state of the card - tuned/not tuned, mono/stereo
    234  */
    235 u_int8_t
    236 az_state(bus_space_tag_t iot, bus_space_handle_t ioh)
    237 {
    238 	return (3 ^ bus_space_read_1(iot, ioh, 0)) & 3;
    239 }
    240 
    241 /*
    242  * Convert volume to hardware representation.
    243  * The card uses bits 00000x0x to set volume.
    244  */
    245 u_int8_t
    246 az_conv_vol(u_int8_t vol)
    247 {
    248 	if (vol < VOLUME_RATIO(1))
    249 		return 0;
    250 	else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
    251 		return 1;
    252 	else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
    253 		return 4;
    254 	else
    255 		return 5;
    256 }
    257 
    258 /*
    259  * Convert volume from hardware representation
    260  */
    261 u_int8_t
    262 az_unconv_vol(u_int8_t vol)
    263 {
    264 	switch (vol) {
    265 	case 0:
    266 		return VOLUME_RATIO(0);
    267 	case 1:
    268 		return VOLUME_RATIO(1);
    269 	case 4:
    270 		return VOLUME_RATIO(2);
    271 	}
    272 	return VOLUME_RATIO(3);
    273 }
    274 
    275 u_int
    276 az_find(bus_space_tag_t iot, bus_space_handle_t ioh)
    277 {
    278 	struct az_softc sc;
    279 	u_int i, scanres = 0;
    280 
    281 	sc.lm.iot = iot;
    282 	sc.lm.ioh = ioh;
    283 	sc.lm.offset = 0;
    284 	sc.lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
    285 	sc.lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
    286 	sc.lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
    287 	sc.lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
    288 	sc.lm.initdata = 0;
    289 	sc.lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
    290 	sc.lm.init = az_lm700x_init;
    291 	sc.lm.rset = az_lm700x_rset;
    292 	sc.rf = LM700X_REF_050;
    293 	sc.mute = 0;
    294 	sc.stereo = LM700X_STEREO;
    295 	sc.vol = 0;
    296 
    297 	/*
    298 	 * Scan whole FM range. If there is a card it'll
    299 	 * respond on some frequency.
    300 	 */
    301 	for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
    302 		az_set_freq(&sc, i);
    303 		scanres += 3 - az_state(iot, ioh);
    304 	}
    305 
    306 	return scanres;
    307 }
    308 
    309 void
    310 az_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
    311 		u_int32_t data)
    312 {
    313 	/* Do nothing */
    314 	return;
    315 }
    316 
    317 void
    318 az_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
    319 		u_int32_t data)
    320 {
    321 	bus_space_write_1(iot, ioh, off, data);
    322 }
    323 
    324 int
    325 az_get_info(void *v, struct radio_info *ri)
    326 {
    327 	struct az_softc *sc = v;
    328 
    329 	ri->mute = sc->mute;
    330 	ri->volume = az_unconv_vol(sc->vol);
    331 	ri->stereo = sc->stereo == LM700X_STEREO ? 1 : 0;
    332 	ri->caps = AZTECH_CAPABILITIES;
    333 	ri->rfreq = lm700x_decode_ref(sc->rf);
    334 	ri->info = az_state(sc->lm.iot, sc->lm.ioh);
    335 	ri->freq = sc->freq;
    336 
    337 	/* UNSUPPORTED */
    338 	ri->lock = 0;
    339 
    340 	return (0);
    341 }
    342 
    343 int
    344 az_set_info(void *v, struct radio_info *ri)
    345 {
    346 	struct az_softc *sc = v;
    347 
    348 	sc->mute = ri->mute ? 1 : 0;
    349 	sc->vol = az_conv_vol(ri->volume);
    350 	sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
    351 	sc->rf = lm700x_encode_ref(ri->rfreq);
    352 
    353 	az_set_freq(sc, ri->freq);
    354 	az_set_mute(sc);
    355 
    356 	return (0);
    357 }
    358