1 /* $NetBSD: radiotrack.c,v 1.8 2002/10/02 03:10:50 thorpej 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@oganer.net>, 7 * Vladimir Popov <jumbo@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 CFATTACH_DECL(rt, sizeof(struct rt_softc), 117 rt_probe, rt_attach, NULL, NULL); 118 119 int rt_find(bus_space_tag_t, bus_space_handle_t); 120 void rt_set_mute(struct rt_softc *, int); 121 void rt_set_freq(struct rt_softc *, u_int32_t); 122 u_int8_t rt_state(bus_space_tag_t, bus_space_handle_t); 123 124 void rt_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t); 125 void rt_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t); 126 127 u_int8_t rt_conv_vol(u_int8_t); 128 u_int8_t rt_unconv_vol(u_int8_t); 129 130 int 131 rt_probe(struct device *parent, struct cfdata *cf, void *aux) 132 { 133 struct isa_attach_args *ia = aux; 134 bus_space_tag_t iot = ia->ia_iot; 135 bus_space_handle_t ioh; 136 u_int r; 137 int iosize = 1, iobase; 138 139 if (ISA_DIRECT_CONFIG(ia)) 140 return 0; 141 142 if (ia->ia_nio < 1) 143 return (0); 144 145 iobase = ia->ia_io[0].ir_addr; 146 147 if (!RT_BASE_VALID(iobase)) { 148 printf("rt: configured iobase 0x%x invalid\n", iobase); 149 return 0; 150 } 151 152 if (bus_space_map(iot, iobase, iosize, 0, &ioh)) 153 return 0; 154 155 r = rt_find(iot, ioh); 156 157 bus_space_unmap(iot, ioh, iosize); 158 159 if (r != 0) { 160 ia->ia_nio = 1; 161 ia->ia_io[0].ir_size = iosize; 162 163 ia->ia_niomem = 0; 164 ia->ia_nirq = 0; 165 ia->ia_ndrq = 0; 166 167 return (1); 168 } 169 170 return (0); 171 } 172 173 void 174 rt_attach(struct device *parent, struct device *self, void *aux) 175 { 176 struct rt_softc *sc = (void *) self; 177 struct isa_attach_args *ia = aux; 178 179 sc->lm.iot = ia->ia_iot; 180 sc->rf = LM700X_REF_050; 181 sc->stereo = LM700X_STEREO; 182 sc->mute = 0; 183 sc->freq = MIN_FM_FREQ; 184 sc->vol = 0; 185 186 /* remap I/O */ 187 if (bus_space_map(sc->lm.iot, ia->ia_io[0].ir_addr, 188 ia->ia_io[0].ir_size, 0, &sc->lm.ioh)) 189 panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname); 190 191 switch (ia->ia_io[0].ir_addr) { 192 case 0x20C: 193 /* FALLTHROUGH */ 194 case 0x30C: 195 sc->cardtype = CARD_RADIOTRACK; 196 printf(": AIMS Lab Radiotrack or compatible\n"); 197 break; 198 case 0x284: 199 /* FALLTHROUGH */ 200 case 0x384: 201 sc->cardtype = CARD_SF16FMI; 202 printf(": SoundForte RadioX SF16-FMI\n"); 203 break; 204 default: 205 sc->cardtype = CARD_UNKNOWN; 206 printf(": Unknown card\n"); 207 break; 208 } 209 210 /* Configure struct lm700x_t lm */ 211 sc->lm.offset = 0; 212 sc->lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF; 213 sc->lm.wzch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_OFF; 214 sc->lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON; 215 sc->lm.woch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_ON; 216 sc->lm.initdata = 0; 217 sc->lm.rsetdata = RT_DATA_ON | RT_CLCK_ON | RT_WREN_OFF; 218 sc->lm.init = rt_lm700x_init; 219 sc->lm.rset = rt_lm700x_rset; 220 221 rt_set_freq(sc, sc->freq); 222 223 radio_attach_mi(&rt_hw_if, sc, &sc->sc_dev); 224 } 225 226 /* 227 * Mute the card 228 */ 229 void 230 rt_set_mute(struct rt_softc *sc, int vol) 231 { 232 int val; 233 234 if (sc->mute) { 235 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, 236 RT_VOLUME_DOWN | RT_CARD_ON); 237 DELAY(MAX_VOL * RT_VOLUME_DELAY); 238 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, 239 RT_VOLUME_STEADY | RT_CARD_ON); 240 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF); 241 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, RT_CARD_OFF); 242 } else { 243 val = sc->vol - vol; 244 if (val < 0) { 245 val *= -1; 246 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, 247 RT_VOLUME_DOWN | RT_CARD_ON); 248 } else { 249 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, 250 RT_VOLUME_UP | RT_CARD_ON); 251 } 252 DELAY(val * RT_VOLUME_DELAY); 253 bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0, 254 RT_VOLUME_STEADY | RT_CARD_ON); 255 } 256 } 257 258 void 259 rt_set_freq(struct rt_softc *sc, u_int32_t nfreq) 260 { 261 u_int32_t reg; 262 263 if (nfreq > MAX_FM_FREQ) 264 nfreq = MAX_FM_FREQ; 265 if (nfreq < MIN_FM_FREQ) 266 nfreq = MIN_FM_FREQ; 267 268 sc->freq = nfreq; 269 270 reg = lm700x_encode_freq(nfreq, sc->rf); 271 reg |= sc->stereo | sc->rf | LM700X_DIVIDER_FM; 272 273 lm700x_hardware_write(&sc->lm, reg, RT_VOLUME_STEADY); 274 275 rt_set_mute(sc, sc->vol); 276 } 277 278 /* 279 * Return state of the card - tuned/not tuned, mono/stereo 280 */ 281 u_int8_t 282 rt_state(bus_space_tag_t iot, bus_space_handle_t ioh) 283 { 284 u_int8_t ret; 285 286 bus_space_write_1(iot, ioh, 0, 287 RT_VOLUME_STEADY | RT_SIGNAL_METER | RT_CARD_ON); 288 DELAY(RT_SIGNAL_METER_DELAY); 289 ret = bus_space_read_1(iot, ioh, 0); 290 291 switch (ret) { 292 case 0xFD: 293 ret = RADIO_INFO_SIGNAL | RADIO_INFO_STEREO; 294 break; 295 case 0xFF: 296 ret = 0; 297 break; 298 default: 299 ret = RADIO_INFO_SIGNAL; 300 break; 301 } 302 303 return ret; 304 } 305 306 /* 307 * Convert volume to hardware representation. 308 */ 309 u_int8_t 310 rt_conv_vol(u_int8_t vol) 311 { 312 if (vol < VOLUME_RATIO(1)) 313 return 0; 314 else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2)) 315 return 1; 316 else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3)) 317 return 2; 318 else if (vol >= VOLUME_RATIO(3) && vol < VOLUME_RATIO(4)) 319 return 3; 320 else 321 return 4; 322 } 323 324 /* 325 * Convert volume from hardware representation 326 */ 327 u_int8_t 328 rt_unconv_vol(u_int8_t vol) 329 { 330 return VOLUME_RATIO(vol); 331 } 332 333 int 334 rt_find(bus_space_tag_t iot, bus_space_handle_t ioh) 335 { 336 struct rt_softc sc; 337 #if 0 338 u_int i, scanres = 0; 339 #endif 340 341 sc.lm.iot = iot; 342 sc.lm.ioh = ioh; 343 sc.lm.offset = 0; 344 sc.lm.wzcl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_OFF; 345 sc.lm.wzch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_OFF; 346 sc.lm.wocl = RT_WREN_ON | RT_CLCK_OFF | RT_DATA_ON; 347 sc.lm.woch = RT_WREN_ON | RT_CLCK_ON | RT_DATA_ON; 348 sc.lm.initdata = 0; 349 sc.lm.rsetdata = RT_SIGNAL_METER; 350 sc.lm.init = rt_lm700x_init; 351 sc.lm.rset = rt_lm700x_rset; 352 sc.rf = LM700X_REF_050; 353 sc.mute = 0; 354 sc.stereo = LM700X_STEREO; 355 sc.vol = 0; 356 357 /* 358 * Scan whole FM range. If there is a card it'll 359 * respond on some frequency. 360 */ 361 return 0; 362 #if 0 363 for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) { 364 rt_set_freq(&sc, i); 365 scanres += rt_state(iot, ioh); 366 } 367 368 return scanres; 369 #endif 370 } 371 372 void 373 rt_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, 374 u_int32_t data) 375 { 376 /* Do nothing */ 377 return; 378 } 379 380 void 381 rt_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off, 382 u_int32_t data) 383 { 384 DELAY(1000); 385 bus_space_write_1(iot, ioh, off, RT_CARD_OFF | data); 386 DELAY(50000); 387 bus_space_write_1(iot, ioh, off, RT_VOLUME_STEADY | RT_CARD_ON | data); 388 } 389 390 int 391 rt_set_info(void *v, struct radio_info *ri) 392 { 393 struct rt_softc *sc = v; 394 395 sc->mute = ri->mute ? 1 : 0; 396 sc->vol = rt_conv_vol(ri->volume); 397 sc->stereo = ri->stereo ? LM700X_STEREO : LM700X_MONO; 398 sc->rf = lm700x_encode_ref(ri->rfreq); 399 400 rt_set_freq(sc, ri->freq); 401 rt_set_mute(sc, sc->vol); 402 403 return (0); 404 } 405 406 int 407 rt_get_info(void *v, struct radio_info *ri) 408 { 409 struct rt_softc *sc = v; 410 411 ri->mute = sc->mute; 412 ri->volume = rt_unconv_vol(sc->vol); 413 ri->stereo = sc->stereo == LM700X_STEREO ? 0 : 1; 414 ri->caps = RTRACK_CAPABILITIES; 415 ri->rfreq = lm700x_decode_ref(sc->rf); 416 ri->info = 3 & rt_state(sc->lm.iot, sc->lm.ioh); 417 ri->freq = sc->freq; 418 419 /* UNSUPPORTED */ 420 ri->lock = 0; 421 422 return (0); 423 } 424