Home | History | Annotate | Line # | Download | only in isa
radiotrack.c revision 1.1
      1 /* $NetBSD: radiotrack.c,v 1.1 2002/01/01 21:51:41 augustss Exp $ */
      2 /* $OpenBSD: radiotrack.c,v 1.1 2001/12/05 10:27:06 mickey Exp $ */
      3 /* $RuOBSD: radiotrack.c,v 1.3 2001/10/18 16:51:36 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 /* AIMS Lab Radiotrack FM Radio Card device driver */
     32 
     33 /*
     34  * Sanyo LM7000 Direct PLL Frequency Synthesizer
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/proc.h>
     40 #include <sys/errno.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/device.h>
     43 #include <sys/radioio.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/isa/isavar.h>
     48 #include <dev/ic/lm700x.h>
     49 #include <dev/radio_if.h>
     50 
     51 #define RF_25K	25
     52 #define RF_50K	50
     53 #define RF_100K	100
     54 
     55 #define MAX_VOL	5	/* XXX Find real value */
     56 #define VOLUME_RATIO(x)	(255 * x / MAX_VOL)
     57 
     58 #define RT_BASE_VALID(x)	\
     59 		((x == 0x30C) || (x == 0x20C) || (x == 0x284) || (x == 0x384))
     60 
     61 #define CARD_RADIOTRACK		0x01
     62 #define CARD_SF16FMI		0x02
     63 #define CARD_UNKNOWN		0xFF
     64 
     65 #define RTRACK_CAPABILITIES	RADIO_CAPS_DETECT_STEREO | 		\
     66 				RADIO_CAPS_DETECT_SIGNAL | 		\
     67 				RADIO_CAPS_SET_MONO | 			\
     68 				RADIO_CAPS_REFERENCE_FREQ
     69 
     70 #define	RT_WREN_ON		(1 << 0)
     71 #define	RT_WREN_OFF		(0 << 0)
     72 
     73 #define RT_CLCK_ON		(1 << 1)
     74 #define RT_CLCK_OFF		(0 << 1)
     75 
     76 #define RT_DATA_ON		(1 << 2)
     77 #define RT_DATA_OFF		(0 << 2)
     78 
     79 #define RT_CARD_ON		(1 << 3)
     80 #define RT_CARD_OFF		(0 << 3)
     81 
     82 #define RT_SIGNAL_METER		(1 << 4)
     83 #define RT_SIGNAL_METER_DELAY	150000
     84 
     85 #define RT_VOLUME_DOWN		(1 << 6)
     86 #define RT_VOLUME_UP		(2 << 6)
     87 #define RT_VOLUME_STEADY	(3 << 6)
     88 #define RT_VOLUME_DELAY		100000
     89 
     90 int	rt_probe(struct device *, struct cfdata *, void *);
     91 void	rt_attach(struct device *, struct device * self, void *);
     92 int	rt_get_info(void *, struct radio_info *);
     93 int	rt_set_info(void *, struct radio_info *);
     94 
     95 struct radio_hw_if rt_hw_if = {
     96 	NULL,   /* open */
     97 	NULL,   /* close */
     98 	rt_get_info,
     99 	rt_set_info,
    100 	NULL
    101 };
    102 
    103 struct rt_softc {
    104 	struct device	sc_dev;
    105 
    106 	int		mute;
    107 	u_int8_t	vol;
    108 	u_int8_t	cardtype;
    109 	u_int32_t	freq;
    110 	u_int32_t	rf;
    111 	u_int32_t	stereo;
    112 
    113 	struct lm700x_t	lm;
    114 };
    115 
    116 struct cfattach rt_ca = {
    117 	sizeof(struct rt_softc), rt_probe, rt_attach
    118 };
    119 
    120 int	rt_find(bus_space_tag_t, bus_space_handle_t);
    121 void	rt_set_mute(struct rt_softc *, int);
    122 void	rt_set_freq(struct rt_softc *, u_int32_t);
    123 u_int8_t	rt_state(bus_space_tag_t, bus_space_handle_t);
    124 
    125 void	rt_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    126 void	rt_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
    127 
    128 u_int8_t	rt_conv_vol(u_int8_t);
    129 u_int8_t	rt_unconv_vol(u_int8_t);
    130 
    131 int
    132 rt_probe(struct device *parent, struct cfdata *cf, void *aux)
    133 {
    134 	struct isa_attach_args *ia = aux;
    135 	bus_space_tag_t iot = ia->ia_iot;
    136 	bus_space_handle_t ioh;
    137 
    138 	int iosize = 1, iobase = ia->ia_iobase;
    139 
    140 	if (!RT_BASE_VALID(iobase)) {
    141 		printf("rt: configured iobase 0x%x invalid", iobase);
    142 		return 0;
    143 	}
    144 
    145 	if (bus_space_map(iot, iobase, iosize, 0, &ioh))
    146 		return 0;
    147 
    148 	bus_space_unmap(iot, ioh, iosize);
    149 
    150 	if (!rt_find(iot, ioh))
    151 		return 0;
    152 
    153 	ia->ia_iosize = iosize;
    154 	return 1;
    155 }
    156 
    157 void
    158 rt_attach(struct device *parent, struct device *self, void *aux)
    159 {
    160 	struct rt_softc *sc = (void *) self;
    161 	struct isa_attach_args *ia = aux;
    162 
    163 	sc->lm.iot = ia->ia_iot;
    164 	sc->rf = LM700X_REF_050;
    165 	sc->stereo = LM700X_STEREO;
    166 	sc->mute = 0;
    167 	sc->freq = MIN_FM_FREQ;
    168 	sc->vol = 0;
    169 
    170 	/* remap I/O */
    171 	if (bus_space_map(sc->lm.iot, ia->ia_iobase, ia->ia_iosize,
    172 			  0, &sc->lm.ioh))
    173 		panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);
    174 
    175 	switch (sc->lm.iot) {
    176 	case 0x20C:
    177 		/* FALLTHROUGH */
    178 	case 0x30C:
    179 		sc->cardtype = CARD_RADIOTRACK;
    180 		printf(": AIMS Lab Radiotrack or compatible");
    181 		break;
    182 	case 0x284:
    183 		/* FALLTHROUGH */
    184 	case 0x384:
    185 		sc->cardtype = CARD_SF16FMI;
    186 		printf(": SoundForte RadioX SF16-FMI");
    187 		break;
    188 	default:
    189 		sc->cardtype = CARD_UNKNOWN;
    190 		printf(": Unknown card");
    191 		break;
    192 	}
    193 
    194 	/* Configure struct lm700x_t lm */
    195 	sc->lm.offset = 0;
    196 	sc->lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF;
    197 	sc->lm.wzch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_OFF;
    198 	sc->lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON;
    199 	sc->lm.woch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_ON;
    200 	sc->lm.initdata = 0;
    201 	sc->lm.rsetdata = RT_DATA_ON | RT_CLCK_ON | RT_WREN_OFF;
    202 	sc->lm.init = rt_lm700x_init;
    203 	sc->lm.rset = rt_lm700x_rset;
    204 
    205 	rt_set_freq(sc, sc->freq);
    206 
    207 	radio_attach_mi(&rt_hw_if, sc, &sc->sc_dev);
    208 }
    209 
    210 /*
    211  * Mute the card
    212  */
    213 void
    214 rt_set_mute(struct rt_softc *sc, int vol)
    215 {
    216 	int val;
    217 
    218 	if (sc->mute) {
    219 		bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    220 				RT_VOLUME_DOWN | RT_CARD_ON);
    221 		DELAY(MAX_VOL * RT_VOLUME_DELAY);
    222 		bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    223 				RT_VOLUME_STEADY | RT_CARD_ON);
    224 		bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF);
    225 		bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF);
    226 	} else {
    227 		val = sc->vol - vol;
    228 		if (val < 0) {
    229 			val *= -1;
    230 			bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    231 					RT_VOLUME_DOWN | RT_CARD_ON);
    232 		} else {
    233 			bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    234 					RT_VOLUME_UP | RT_CARD_ON);
    235 		}
    236 		DELAY(val * RT_VOLUME_DELAY);
    237 		bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
    238 				RT_VOLUME_STEADY | RT_CARD_ON);
    239 	}
    240 }
    241 
    242 void
    243 rt_set_freq(struct rt_softc *sc, u_int32_t nfreq)
    244 {
    245 	u_int32_t reg;
    246 
    247 	if (nfreq > MAX_FM_FREQ)
    248 		nfreq = MAX_FM_FREQ;
    249 	if (nfreq < MIN_FM_FREQ)
    250 		nfreq = MIN_FM_FREQ;
    251 
    252 	sc->freq = nfreq;
    253 
    254 	reg = lm700x_encode_freq(nfreq, sc->rf);
    255 	reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM;
    256 
    257 	lm700x_hardware_write(&sc->lm, reg, RT_VOLUME_STEADY);
    258 
    259 	rt_set_mute(sc, sc->vol);
    260 }
    261 
    262 /*
    263  * Return state of the card - tuned/not tuned, mono/stereo
    264  */
    265 u_int8_t
    266 rt_state(bus_space_tag_t iot, bus_space_handle_t ioh)
    267 {
    268 	u_int8_t ret;
    269 
    270 	bus_space_write_1(iot, ioh, 0,
    271 			RT_VOLUME_STEADY | RT_SIGNAL_METER | RT_CARD_ON);
    272 	DELAY(RT_SIGNAL_METER_DELAY);
    273 	ret = bus_space_read_1(iot, ioh, 0);
    274 
    275 	switch (ret) {
    276 	case 0xFD:
    277 		ret = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO;
    278 		break;
    279 	case 0xFF:
    280 		ret = 0;
    281 		break;
    282 	default:
    283 		ret = RADIO_INFO_SIGNAL;
    284 		break;
    285 	}
    286 
    287 	return ret;
    288 }
    289 
    290 /*
    291  * Convert volume to hardware representation.
    292  */
    293 u_int8_t
    294 rt_conv_vol(u_int8_t vol)
    295 {
    296 	if (vol < VOLUME_RATIO(1))
    297 		return 0;
    298 	else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
    299 		return 1;
    300 	else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
    301 		return 2;
    302 	else if (vol >= VOLUME_RATIO(3) && vol < VOLUME_RATIO(4))
    303 		return 3;
    304 	else
    305 		return 4;
    306 }
    307 
    308 /*
    309  * Convert volume from hardware representation
    310  */
    311 u_int8_t
    312 rt_unconv_vol(u_int8_t vol)
    313 {
    314 	return VOLUME_RATIO(vol);
    315 }
    316 
    317 int
    318 rt_find(bus_space_tag_t iot, bus_space_handle_t ioh)
    319 {
    320 	struct rt_softc sc;
    321 #if 0
    322 	u_int i, scanres = 0;
    323 #endif
    324 
    325 	sc.lm.iot = iot;
    326 	sc.lm.ioh = ioh;
    327 	sc.lm.offset = 0;
    328 	sc.lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF;
    329 	sc.lm.wzch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_OFF;
    330 	sc.lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON;
    331 	sc.lm.woch = RT_WREN_ON | RT_CLCK_ON  | RT_DATA_ON;
    332 	sc.lm.initdata = 0;
    333 	sc.lm.rsetdata = RT_SIGNAL_METER;
    334 	sc.lm.init = rt_lm700x_init;
    335 	sc.lm.rset = rt_lm700x_rset;
    336 	sc.rf = LM700X_REF_050;
    337 	sc.mute = 0;
    338 	sc.stereo = LM700X_STEREO;
    339 	sc.vol = 0;
    340 
    341 	/*
    342 	 * Scan whole FM range. If there is a card it'll
    343 	 * respond on some frequency.
    344 	 */
    345 	return 0;
    346 #if 0
    347 	for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
    348 		rt_set_freq(&sc, i);
    349 		scanres += rt_state(iot, ioh);
    350 	}
    351 
    352 	return scanres;
    353 #endif
    354 }
    355 
    356 void
    357 rt_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
    358 		u_int32_t data)
    359 {
    360 	/* Do nothing */
    361 	return;
    362 }
    363 
    364 void
    365 rt_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
    366 		u_int32_t data)
    367 {
    368 	DELAY(1000);
    369 	bus_space_write_1(iot, ioh, off, RT_CARD_OFF | data);
    370 	DELAY(50000);
    371 	bus_space_write_1(iot, ioh, off, RT_VOLUME_STEADY | RT_CARD_ON | data);
    372 }
    373 
    374 int
    375 rt_set_info(void *v, struct radio_info *ri)
    376 {
    377 	struct rt_softc *sc = v;
    378 
    379 	sc->mute = ri->mute ? 1 : 0;
    380 	sc->vol = rt_conv_vol(ri->volume);
    381 	sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO;
    382 	sc->rf = lm700x_encode_ref(ri->rfreq);
    383 
    384 	rt_set_freq(sc, ri->freq);
    385 	rt_set_mute(sc, sc->vol);
    386 
    387 	return (0);
    388 }
    389 
    390 int
    391 rt_get_info(void *v, struct radio_info *ri)
    392 {
    393 	struct rt_softc *sc = v;
    394 
    395 	ri->mute = sc->mute;
    396 	ri->volume = rt_unconv_vol(sc->vol);
    397 	ri->stereo = sc->stereo == LM700X_STEREO ? 0 : 1;
    398 	ri->caps = RTRACK_CAPABILITIES;
    399 	ri->rfreq = lm700x_decode_ref(sc->rf);
    400 	ri->info = 3 & rt_state(sc->lm.iot, sc->lm.ioh);
    401 	ri->freq = sc->freq;
    402 
    403 	/* UNSUPPORTED */
    404 	ri->lock = 0;
    405 
    406 	return (0);
    407 }
    408