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