ad1816.c revision 53597
1/* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * Copyright Luigi Rizzo, 1997,1998 4 * Copyright by Hannu Savolainen 1994, 1995 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: head/sys/dev/sound/isa/ad1816.c 53597 1999-11-22 16:20:48Z cg $ 29 */ 30 31#include <dev/sound/pcm/sound.h> 32 33#if NPCM > 0 && NPNP > 0 34 35#include <dev/sound/isa/ad1816.h> 36 37struct ad1816_info; 38 39struct ad1816_chinfo { 40 struct ad1816_info *parent; 41 pcm_channel *channel; 42 snd_dbuf *buffer; 43 int dir; 44}; 45 46struct ad1816_info { 47 struct resource *io_base; /* primary I/O address for the board */ 48 int io_rid; 49 struct resource *irq; 50 int irq_rid; 51 struct resource *drq1; /* play */ 52 int drq1_rid; 53 struct resource *drq2; /* rec */ 54 int drq2_rid; 55 bus_dma_tag_t parent_dmat; 56 57 struct ad1816_chinfo pch, rch; 58}; 59 60static driver_intr_t ad1816_intr; 61static int ad1816_probe(device_t dev); 62static int ad1816_attach(device_t dev); 63 64/* IO primitives */ 65static int ad1816_wait_init(struct ad1816_info *ad1816, int x); 66static u_short ad1816_read(struct ad1816_info *ad1816, u_int reg); 67static void ad1816_write(struct ad1816_info *ad1816, u_int reg, u_short data); 68 69static int ad1816mix_init(snd_mixer *m); 70static int ad1816mix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right); 71static int ad1816mix_setrecsrc(snd_mixer *m, u_int32_t src); 72static snd_mixer ad1816_mixer = { 73 "ad1816 mixer", 74 ad1816mix_init, 75 ad1816mix_set, 76 ad1816mix_setrecsrc, 77}; 78 79static devclass_t pcm_devclass; 80 81/* channel interface */ 82static void *ad1816chan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir); 83static int ad1816chan_setdir(void *data, int dir); 84static int ad1816chan_setformat(void *data, u_int32_t format); 85static int ad1816chan_setspeed(void *data, u_int32_t speed); 86static int ad1816chan_setblocksize(void *data, u_int32_t blocksize); 87static int ad1816chan_trigger(void *data, int go); 88static int ad1816chan_getptr(void *data); 89static pcmchan_caps *ad1816chan_getcaps(void *data); 90 91static pcmchan_caps ad1816_caps = { 92 4000, 55200, 93 AFMT_STEREO | AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW, 94 AFMT_STEREO | AFMT_S16_LE 95}; 96 97static pcm_channel ad1816_chantemplate = { 98 ad1816chan_init, 99 ad1816chan_setdir, 100 ad1816chan_setformat, 101 ad1816chan_setspeed, 102 ad1816chan_setblocksize, 103 ad1816chan_trigger, 104 ad1816chan_getptr, 105 ad1816chan_getcaps, 106}; 107 108#define FULL_DUPLEX(x) (pcm_getflags(x) & SD_F_SIMPLEX) 109#define AD1816_MUTE 31 /* value for mute */ 110 111static int 112port_rd(struct resource *port, int off) 113{ 114 if (port) 115 return bus_space_read_1(rman_get_bustag(port), 116 rman_get_bushandle(port), 117 off); 118 else 119 return -1; 120} 121 122static void 123port_wr(struct resource *port, int off, u_int8_t data) 124{ 125 if (port) 126 return bus_space_write_1(rman_get_bustag(port), 127 rman_get_bushandle(port), 128 off, data); 129} 130 131static int 132io_rd(struct ad1816_info *ad1816, int reg) 133{ 134 return port_rd(ad1816->io_base, reg); 135} 136 137static void 138io_wr(struct ad1816_info *ad1816, int reg, u_int8_t data) 139{ 140 return port_wr(ad1816->io_base, reg, data); 141} 142 143static void 144ad1816_intr(void *arg) 145{ 146 struct ad1816_info *ad1816 = (struct ad1816_info *)arg; 147 unsigned char c, served = 0; 148 149 /* get interupt status */ 150 c = io_rd(ad1816, AD1816_INT); 151 152 /* check for stray interupts */ 153 if (c & ~(AD1816_INTRCI | AD1816_INTRPI)) { 154 printf("pcm: stray int (%x)\n", c); 155 c &= AD1816_INTRCI | AD1816_INTRPI; 156 } 157 /* check for capture interupt */ 158 if (ad1816->rch.buffer->dl && (c & AD1816_INTRCI)) { 159 chn_intr(ad1816->rch.channel); 160 served |= AD1816_INTRCI; /* cp served */ 161 } 162 /* check for playback interupt */ 163 if (ad1816->pch.buffer->dl && (c & AD1816_INTRPI)) { 164 chn_intr(ad1816->pch.channel); 165 served |= AD1816_INTRPI; /* pb served */ 166 } 167 if (served == 0) { 168 /* this probably means this is not a (working) ad1816 chip, */ 169 /* or an error in dma handling */ 170 printf("pcm: int without reason (%x)\n", c); 171 c = 0; 172 } else c &= ~served; 173 io_wr(ad1816, AD1816_INT, c); 174 c = io_rd(ad1816, AD1816_INT); 175 if (c != 0) printf("pcm: int clear failed (%x)\n", c); 176} 177 178static int 179ad1816_wait_init(struct ad1816_info *ad1816, int x) 180{ 181 int n = 0; /* to shut up the compiler... */ 182 183 for (; x--;) 184 if ((n = (io_rd(ad1816, AD1816_ALE) & AD1816_BUSY)) == 0) DELAY(10); 185 else return n; 186 printf("ad1816_wait_init failed 0x%02x.\n", n); 187 return -1; 188} 189 190static unsigned short 191ad1816_read(struct ad1816_info *ad1816, unsigned int reg) 192{ 193 int flags; 194 u_short x = 0; 195 196 /* we don't want to be blocked here */ 197 flags = spltty(); 198 if (ad1816_wait_init(ad1816, 100) == -1) return 0; 199 io_wr(ad1816, AD1816_ALE, 0); 200 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK)); 201 if (ad1816_wait_init(ad1816, 100) == -1) return 0; 202 x = (io_rd(ad1816, AD1816_HIGH) << 8) | io_rd(ad1816, AD1816_LOW); 203 splx(flags); 204 return x; 205} 206 207static void 208ad1816_write(struct ad1816_info *ad1816, unsigned int reg, unsigned short data) 209{ 210 int flags; 211 212 flags = spltty(); 213 if (ad1816_wait_init(ad1816, 100) == -1) return; 214 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK)); 215 io_wr(ad1816, AD1816_LOW, (data & 0x000000ff)); 216 io_wr(ad1816, AD1816_HIGH, (data & 0x0000ff00) >> 8); 217 splx(flags); 218} 219 220static int 221ad1816mix_init(snd_mixer *m) 222{ 223 mix_setdevs(m, AD1816_MIXER_DEVICES); 224 mix_setrecdevs(m, AD1816_REC_DEVICES); 225 return 0; 226} 227 228static int 229ad1816mix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right) 230{ 231 struct ad1816_info *ad1816 = mix_getdevinfo(m); 232 u_short reg = 0; 233 234 /* Scale volumes */ 235 left = AD1816_MUTE - (AD1816_MUTE * left) / 100; 236 right = AD1816_MUTE - (AD1816_MUTE * right) / 100; 237 238 reg = (left << 8) | right; 239 240 /* do channel selective muting if volume is zero */ 241 if (left == AD1816_MUTE) reg |= 0x8000; 242 if (right == AD1816_MUTE) reg |= 0x0080; 243 244 switch (dev) { 245 case SOUND_MIXER_VOLUME: /* Register 14 master volume */ 246 ad1816_write(ad1816, 14, reg); 247 break; 248 249 case SOUND_MIXER_CD: /* Register 15 cd */ 250 case SOUND_MIXER_LINE1: 251 ad1816_write(ad1816, 15, reg); 252 break; 253 254 case SOUND_MIXER_SYNTH: /* Register 16 synth */ 255 ad1816_write(ad1816, 16, reg); 256 break; 257 258 case SOUND_MIXER_PCM: /* Register 4 pcm */ 259 ad1816_write(ad1816, 4, reg); 260 break; 261 262 case SOUND_MIXER_LINE: 263 case SOUND_MIXER_LINE3: /* Register 18 line in */ 264 ad1816_write(ad1816, 18, reg); 265 break; 266 267 case SOUND_MIXER_MIC: /* Register 19 mic volume */ 268 ad1816_write(ad1816, 19, reg & ~0xff); /* mic is mono */ 269 break; 270 271 case SOUND_MIXER_IGAIN: 272 /* and now to something completely different ... */ 273 ad1816_write(ad1816, 20, ((ad1816_read(ad1816, 20) & ~0x0f0f) 274 | (((AD1816_MUTE - left) / 2) << 8) /* four bits of adc gain */ 275 | ((AD1816_MUTE - right) / 2))); 276 break; 277 278 default: 279 printf("ad1816_mixer_set(): unknown device.\n"); 280 break; 281 } 282 283 left = ((AD1816_MUTE - left) * 100) / AD1816_MUTE; 284 right = ((AD1816_MUTE - right) * 100) / AD1816_MUTE; 285 286 return left | (right << 8); 287} 288 289static int 290ad1816mix_setrecsrc(snd_mixer *m, u_int32_t src) 291{ 292 struct ad1816_info *ad1816 = mix_getdevinfo(m); 293 int dev; 294 295 switch (src) { 296 case SOUND_MASK_LINE: 297 case SOUND_MASK_LINE3: 298 dev = 0x00; 299 break; 300 301 case SOUND_MASK_CD: 302 case SOUND_MASK_LINE1: 303 dev = 0x20; 304 break; 305 306 case SOUND_MASK_MIC: 307 default: 308 dev = 0x50; 309 src = SOUND_MASK_MIC; 310 } 311 312 dev |= dev << 8; 313 ad1816_write(ad1816, 20, (ad1816_read(ad1816, 20) & ~0x7070) | dev); 314 return src; 315} 316 317/* channel interface */ 318static void * 319ad1816chan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir) 320{ 321 struct ad1816_info *ad1816 = devinfo; 322 struct ad1816_chinfo *ch = (dir == PCMDIR_PLAY)? &ad1816->pch : &ad1816->rch; 323 324 ch->parent = ad1816; 325 ch->channel = c; 326 ch->buffer = b; 327 ch->buffer->bufsize = DSP_BUFFSIZE; 328 if (chn_allocbuf(ch->buffer, ad1816->parent_dmat) == -1) return NULL; 329 return ch; 330} 331 332static int 333ad1816chan_setdir(void *data, int dir) 334{ 335 struct ad1816_chinfo *ch = data; 336 struct ad1816_info *ad1816 = ch->parent; 337 338 ch->buffer->chan = rman_get_start((dir == PCMDIR_PLAY)? 339 ad1816->drq1 : ad1816->drq2); 340 ch->dir = dir; 341 return 0; 342} 343 344static int 345ad1816chan_setformat(void *data, u_int32_t format) 346{ 347 struct ad1816_chinfo *ch = data; 348 struct ad1816_info *ad1816 = ch->parent; 349 350 int fmt = AD1816_U8, reg; 351 if (ch->dir == PCMDIR_PLAY) { 352 reg = AD1816_PLAY; 353 ad1816_write(ad1816, 8, 0x0000); /* reset base and current counter */ 354 ad1816_write(ad1816, 9, 0x0000); /* for playback and capture */ 355 } else { 356 reg = AD1816_CAPT; 357 ad1816_write(ad1816, 10, 0x0000); 358 ad1816_write(ad1816, 11, 0x0000); 359 } 360 switch (format & ~AFMT_STEREO) { 361 case AFMT_A_LAW: 362 fmt = AD1816_ALAW; 363 break; 364 365 case AFMT_MU_LAW: 366 fmt = AD1816_MULAW; 367 break; 368 369 case AFMT_S16_LE: 370 fmt = AD1816_S16LE; 371 break; 372 373 case AFMT_S16_BE: 374 fmt = AD1816_S16BE; 375 break; 376 377 case AFMT_U8: 378 fmt = AD1816_U8; 379 break; 380 } 381 if (format & AFMT_STEREO) fmt |= AD1816_STEREO; 382 io_wr(ad1816, reg, fmt); 383 return format; 384} 385 386static int 387ad1816chan_setspeed(void *data, u_int32_t speed) 388{ 389 struct ad1816_chinfo *ch = data; 390 struct ad1816_info *ad1816 = ch->parent; 391 392 RANGE(speed, 4000, 55200); 393 ad1816_write(ad1816, (ch->dir == PCMDIR_PLAY)? 2 : 3, speed); 394 return speed; 395} 396 397static int 398ad1816chan_setblocksize(void *data, u_int32_t blocksize) 399{ 400 return blocksize; 401} 402 403static int 404ad1816chan_trigger(void *data, int go) 405{ 406 struct ad1816_chinfo *ch = data; 407 struct ad1816_info *ad1816 = ch->parent; 408 int wr, reg; 409 410 buf_isadma(ch->buffer, go); 411 wr = (ch->dir == PCMDIR_PLAY); 412 reg = wr? AD1816_PLAY : AD1816_CAPT; 413 switch (go) { 414 case PCMTRIG_START: 415 /* start only if not already running */ 416 if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) { 417 int cnt = ((ch->buffer->dl) >> 2) - 1; 418 ad1816_write(ad1816, wr? 8 : 10, cnt); /* count */ 419 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) | 420 (wr? 0x8000 : 0x4000)); /* enable int */ 421 /* enable playback */ 422 io_wr(ad1816, reg, io_rd(ad1816, reg) | AD1816_ENABLE); 423 if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) 424 printf("ad1816: failed to start %s DMA!\n", 425 wr? "play" : "rec"); 426 } 427 break; 428 429 case PCMTRIG_STOP: 430 case PCMTRIG_ABORT: /* XXX check this... */ 431 /* we don't test here if it is running... */ 432 if (wr) { 433 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) & 434 ~(wr? 0x8000 : 0x4000)); 435 /* disable int */ 436 io_wr(ad1816, reg, io_rd(ad1816, reg) & ~AD1816_ENABLE); 437 /* disable playback */ 438 if (io_rd(ad1816, reg) & AD1816_ENABLE) 439 printf("ad1816: failed to stop %s DMA!\n", 440 wr? "play" : "rec"); 441 ad1816_write(ad1816, wr? 8 : 10, 0); /* reset base cnt */ 442 ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */ 443 } 444 break; 445 } 446 return 0; 447} 448 449static int 450ad1816chan_getptr(void *data) 451{ 452 struct ad1816_chinfo *ch = data; 453 return buf_isadmaptr(ch->buffer); 454} 455 456static pcmchan_caps * 457ad1816chan_getcaps(void *data) 458{ 459 return &ad1816_caps; 460} 461 462static void 463ad1816_release_resources(struct ad1816_info *ad1816, device_t dev) 464{ 465 if (ad1816->irq) { 466 bus_release_resource(dev, SYS_RES_IRQ, ad1816->irq_rid, 467 ad1816->irq); 468 ad1816->irq = 0; 469 } 470 if (ad1816->drq1) { 471 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq1_rid, 472 ad1816->drq1); 473 ad1816->drq1 = 0; 474 } 475 if (ad1816->drq2) { 476 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq2_rid, 477 ad1816->drq2); 478 ad1816->drq2 = 0; 479 } 480 if (ad1816->io_base) { 481 bus_release_resource(dev, SYS_RES_IOPORT, ad1816->io_rid, 482 ad1816->io_base); 483 ad1816->io_base = 0; 484 } 485 free(ad1816, M_DEVBUF); 486} 487 488static int 489ad1816_alloc_resources(struct ad1816_info *ad1816, device_t dev) 490{ 491 int ok = 1, pdma, rdma; 492 if (!ad1816->io_base) 493 ad1816->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &ad1816->io_rid, 494 0, ~0, 1, RF_ACTIVE); 495 if (!ad1816->irq) 496 ad1816->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &ad1816->irq_rid, 497 0, ~0, 1, RF_ACTIVE); 498 if (!ad1816->drq1) 499 ad1816->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq1_rid, 500 0, ~0, 1, RF_ACTIVE); 501 if (ad1816->drq2_rid >= 0 && !ad1816->drq2) 502 ad1816->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq2_rid, 503 0, ~0, 1, RF_ACTIVE); 504 505 if (!ad1816->io_base || !ad1816->drq1 || !ad1816->irq) ok = 0; 506 if (ad1816->drq2_rid >= 0 && !ad1816->drq2) ok = 0; 507 508 if (ok) { 509 pdma = rman_get_start(ad1816->drq1); 510 isa_dma_acquire(pdma); 511 isa_dmainit(pdma, DSP_BUFFSIZE); 512 if (ad1816->drq2) { 513 rdma = rman_get_start(ad1816->drq2); 514 isa_dma_acquire(rdma); 515 isa_dmainit(rdma, DSP_BUFFSIZE); 516 } else rdma = pdma; 517 if (pdma == rdma) 518 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 519 } 520 return ok; 521} 522 523static int 524ad1816_init(struct ad1816_info *ad1816, device_t dev) 525{ 526 ad1816_write(ad1816, 1, 0x2); /* disable interrupts */ 527 ad1816_write(ad1816, 32, 0x90F0); /* SoundSys Mode, split fmt */ 528 529 ad1816_write(ad1816, 5, 0x8080); /* FM volume mute */ 530 ad1816_write(ad1816, 6, 0x8080); /* I2S1 volume mute */ 531 ad1816_write(ad1816, 7, 0x8080); /* I2S0 volume mute */ 532 ad1816_write(ad1816, 17, 0x8888); /* VID Volume mute */ 533 ad1816_write(ad1816, 20, 0x5050); /* recsrc mic, agc off */ 534 /* adc gain is set to 0 */ 535 536 return 0; 537} 538 539static int 540ad1816_probe(device_t dev) 541{ 542 char *s = NULL; 543 u_int32_t logical_id = isa_get_logicalid(dev); 544 545 switch (logical_id) { 546 case 0x80719304: /* ADS7180 */ 547 s = "Terratec Soundsystem BASE 1"; 548 break; 549 } 550 551 if (s) { 552 device_set_desc(dev, s); 553 return 0; 554 } 555 return ENXIO; 556} 557 558static int 559ad1816_attach(device_t dev) 560{ 561 struct ad1816_info *ad1816; 562 snddev_info *d = device_get_softc(dev); 563 void *ih; 564 char status[SND_STATUSLEN]; 565 566 ad1816 = (struct ad1816_info *)malloc(sizeof *ad1816, M_DEVBUF, M_NOWAIT); 567 if (!ad1816) return ENXIO; 568 bzero(ad1816, sizeof *ad1816); 569 570 ad1816->io_rid = 2; 571 ad1816->irq_rid = 0; 572 ad1816->drq1_rid = 0; 573 ad1816->drq2_rid = 1; 574 575 if (!ad1816_alloc_resources(ad1816, dev)) goto no; 576 ad1816_init(ad1816, dev); 577 mixer_init(d, &ad1816_mixer, ad1816); 578 bus_setup_intr(dev, ad1816->irq, INTR_TYPE_TTY, ad1816_intr, ad1816, &ih); 579 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 580 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 581 /*highaddr*/BUS_SPACE_MAXADDR, 582 /*filter*/NULL, /*filterarg*/NULL, 583 /*maxsize*/DSP_BUFFSIZE, /*nsegments*/1, 584 /*maxsegz*/0x3ffff, 585 /*flags*/0, &ad1816->parent_dmat) != 0) { 586 device_printf(dev, "unable to create dma tag\n"); 587 goto no; 588 } 589 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld", 590 rman_get_start(ad1816->io_base), 591 rman_get_start(ad1816->irq), 592 rman_get_start(ad1816->drq1)); 593 if (FULL_DUPLEX(dev)) snprintf(status + strlen(status), 594 SND_STATUSLEN - strlen(status), ":%ld", 595 rman_get_start(ad1816->drq1)); 596 597 if (pcm_register(dev, ad1816, 1, 1)) goto no; 598 pcm_addchan(dev, PCMDIR_REC, &ad1816_chantemplate, ad1816); 599 pcm_addchan(dev, PCMDIR_PLAY, &ad1816_chantemplate, ad1816); 600 pcm_setstatus(dev, status); 601 602 return 0; 603no: 604 ad1816_release_resources(ad1816, dev); 605 return ENXIO; 606 607} 608 609static device_method_t ad1816_methods[] = { 610 /* Device interface */ 611 DEVMETHOD(device_probe, ad1816_probe), 612 DEVMETHOD(device_attach, ad1816_attach), 613 614 { 0, 0 } 615}; 616 617static driver_t ad1816_driver = { 618 "pcm", 619 ad1816_methods, 620 sizeof(snddev_info), 621}; 622 623DRIVER_MODULE(ad1816, isa, ad1816_driver, pcm_devclass, 0, 0); 624 625#endif 626