2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * Copyright Luigi Rizzo, 1997,1998
4 * Copyright by Hannu Savolainen 1994, 1995
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <dev/sound/pcm/sound.h>
30 #include <dev/sound/isa/ad1816.h>
34 SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/isa/ad1816.c,v 1.7.2.9 2002/12/24 21:17:41 semenu Exp $");
38 struct ad1816_chinfo {
39 struct ad1816_info *parent;
40 struct pcm_channel *channel;
41 struct snd_dbuf *buffer;
46 struct resource *io_base; /* primary I/O address for the board */
50 struct resource *drq1; /* play */
52 struct resource *drq2; /* rec */
55 bus_dma_tag_t parent_dmat;
59 struct ad1816_chinfo pch, rch;
62 static u_int32_t ad1816_fmt[] = {
64 AFMT_STEREO | AFMT_U8,
66 AFMT_STEREO | AFMT_S16_LE,
68 AFMT_STEREO | AFMT_MU_LAW,
70 AFMT_STEREO | AFMT_A_LAW,
74 static struct pcmchan_caps ad1816_caps = {4000, 55200, ad1816_fmt, 0};
76 #define AD1816_MUTE 31 /* value for mute */
79 ad1816_lock(struct ad1816_info *ad1816)
81 snd_mtxlock(ad1816->lock);
85 ad1816_unlock(struct ad1816_info *ad1816)
87 snd_mtxunlock(ad1816->lock);
91 port_rd(struct resource *port, int off)
94 return bus_space_read_1(rman_get_bustag(port),
95 rman_get_bushandle(port),
102 port_wr(struct resource *port, int off, u_int8_t data)
105 bus_space_write_1(rman_get_bustag(port),
106 rman_get_bushandle(port),
111 io_rd(struct ad1816_info *ad1816, int reg)
113 return port_rd(ad1816->io_base, reg);
117 io_wr(struct ad1816_info *ad1816, int reg, u_int8_t data)
119 port_wr(ad1816->io_base, reg, data);
123 ad1816_intr(void *arg)
125 struct ad1816_info *ad1816 = (struct ad1816_info *)arg;
126 unsigned char c, served = 0;
129 /* get interupt status */
130 c = io_rd(ad1816, AD1816_INT);
132 /* check for stray interupts */
133 if (c & ~(AD1816_INTRCI | AD1816_INTRPI)) {
134 printf("pcm: stray int (%x)\n", c);
135 c &= AD1816_INTRCI | AD1816_INTRPI;
137 /* check for capture interupt */
138 if (sndbuf_runsz(ad1816->rch.buffer) && (c & AD1816_INTRCI)) {
139 chn_intr(ad1816->rch.channel);
140 served |= AD1816_INTRCI; /* cp served */
142 /* check for playback interupt */
143 if (sndbuf_runsz(ad1816->pch.buffer) && (c & AD1816_INTRPI)) {
144 chn_intr(ad1816->pch.channel);
145 served |= AD1816_INTRPI; /* pb served */
148 /* this probably means this is not a (working) ad1816 chip, */
149 /* or an error in dma handling */
150 printf("pcm: int without reason (%x)\n", c);
153 io_wr(ad1816, AD1816_INT, c);
154 c = io_rd(ad1816, AD1816_INT);
155 if (c != 0) printf("pcm: int clear failed (%x)\n", c);
156 ad1816_unlock(ad1816);
160 ad1816_wait_init(struct ad1816_info *ad1816, int x)
162 int n = 0; /* to shut up the compiler... */
165 if ((n = (io_rd(ad1816, AD1816_ALE) & AD1816_BUSY)) == 0) DELAY(10);
167 printf("ad1816_wait_init failed 0x%02x.\n", n);
171 static unsigned short
172 ad1816_read(struct ad1816_info *ad1816, unsigned int reg)
176 if (ad1816_wait_init(ad1816, 100) == -1) return 0;
177 io_wr(ad1816, AD1816_ALE, 0);
178 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
179 if (ad1816_wait_init(ad1816, 100) == -1) return 0;
180 x = (io_rd(ad1816, AD1816_HIGH) << 8) | io_rd(ad1816, AD1816_LOW);
185 ad1816_write(struct ad1816_info *ad1816, unsigned int reg, unsigned short data)
187 if (ad1816_wait_init(ad1816, 100) == -1) return;
188 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
189 io_wr(ad1816, AD1816_LOW, (data & 0x000000ff));
190 io_wr(ad1816, AD1816_HIGH, (data & 0x0000ff00) >> 8);
193 /* -------------------------------------------------------------------- */
196 ad1816mix_init(struct snd_mixer *m)
198 mix_setdevs(m, AD1816_MIXER_DEVICES);
199 mix_setrecdevs(m, AD1816_REC_DEVICES);
204 ad1816mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
206 struct ad1816_info *ad1816 = mix_getdevinfo(m);
210 left = AD1816_MUTE - (AD1816_MUTE * left) / 100;
211 right = AD1816_MUTE - (AD1816_MUTE * right) / 100;
213 reg = (left << 8) | right;
215 /* do channel selective muting if volume is zero */
216 if (left == AD1816_MUTE) reg |= 0x8000;
217 if (right == AD1816_MUTE) reg |= 0x0080;
221 case SOUND_MIXER_VOLUME: /* Register 14 master volume */
222 ad1816_write(ad1816, 14, reg);
225 case SOUND_MIXER_CD: /* Register 15 cd */
226 case SOUND_MIXER_LINE1:
227 ad1816_write(ad1816, 15, reg);
230 case SOUND_MIXER_SYNTH: /* Register 16 synth */
231 ad1816_write(ad1816, 16, reg);
234 case SOUND_MIXER_PCM: /* Register 4 pcm */
235 ad1816_write(ad1816, 4, reg);
238 case SOUND_MIXER_LINE:
239 case SOUND_MIXER_LINE3: /* Register 18 line in */
240 ad1816_write(ad1816, 18, reg);
243 case SOUND_MIXER_MIC: /* Register 19 mic volume */
244 ad1816_write(ad1816, 19, reg & ~0xff); /* mic is mono */
247 case SOUND_MIXER_IGAIN:
248 /* and now to something completely different ... */
249 ad1816_write(ad1816, 20, ((ad1816_read(ad1816, 20) & ~0x0f0f)
250 | (((AD1816_MUTE - left) / 2) << 8) /* four bits of adc gain */
251 | ((AD1816_MUTE - right) / 2)));
255 printf("ad1816_mixer_set(): unknown device.\n");
258 ad1816_unlock(ad1816);
260 left = ((AD1816_MUTE - left) * 100) / AD1816_MUTE;
261 right = ((AD1816_MUTE - right) * 100) / AD1816_MUTE;
263 return left | (right << 8);
267 ad1816mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
269 struct ad1816_info *ad1816 = mix_getdevinfo(m);
273 case SOUND_MASK_LINE:
274 case SOUND_MASK_LINE3:
279 case SOUND_MASK_LINE1:
286 src = SOUND_MASK_MIC;
291 ad1816_write(ad1816, 20, (ad1816_read(ad1816, 20) & ~0x7070) | dev);
292 ad1816_unlock(ad1816);
296 static kobj_method_t ad1816mixer_methods[] = {
297 KOBJMETHOD(mixer_init, ad1816mix_init),
298 KOBJMETHOD(mixer_set, ad1816mix_set),
299 KOBJMETHOD(mixer_setrecsrc, ad1816mix_setrecsrc),
302 MIXER_DECLARE(ad1816mixer);
304 /* -------------------------------------------------------------------- */
305 /* channel interface */
307 ad1816chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
309 struct ad1816_info *ad1816 = devinfo;
310 struct ad1816_chinfo *ch = (dir == PCMDIR_PLAY)? &ad1816->pch : &ad1816->rch;
315 if (sndbuf_alloc(ch->buffer, ad1816->parent_dmat, ad1816->bufsize) == -1) return NULL;
320 ad1816chan_setdir(kobj_t obj, void *data, int dir)
322 struct ad1816_chinfo *ch = data;
323 struct ad1816_info *ad1816 = ch->parent;
325 sndbuf_isadmasetup(ch->buffer, (dir == PCMDIR_PLAY)? ad1816->drq1 : ad1816->drq2);
331 ad1816chan_setformat(kobj_t obj, void *data, u_int32_t format)
333 struct ad1816_chinfo *ch = data;
334 struct ad1816_info *ad1816 = ch->parent;
335 int fmt = AD1816_U8, reg;
338 if (ch->dir == PCMDIR_PLAY) {
340 ad1816_write(ad1816, 8, 0x0000); /* reset base and current counter */
341 ad1816_write(ad1816, 9, 0x0000); /* for playback and capture */
344 ad1816_write(ad1816, 10, 0x0000);
345 ad1816_write(ad1816, 11, 0x0000);
347 switch (format & ~AFMT_STEREO) {
368 if (format & AFMT_STEREO) fmt |= AD1816_STEREO;
369 io_wr(ad1816, reg, fmt);
370 ad1816_unlock(ad1816);
376 ad1816chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
378 struct ad1816_chinfo *ch = data;
379 struct ad1816_info *ad1816 = ch->parent;
381 RANGE(speed, 4000, 55200);
383 ad1816_write(ad1816, (ch->dir == PCMDIR_PLAY)? 2 : 3, speed);
384 ad1816_unlock(ad1816);
389 ad1816chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
391 struct ad1816_chinfo *ch = data;
393 ch->blksz = blocksize;
398 ad1816chan_trigger(kobj_t obj, void *data, int go)
400 struct ad1816_chinfo *ch = data;
401 struct ad1816_info *ad1816 = ch->parent;
404 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
407 sndbuf_isadma(ch->buffer, go);
408 wr = (ch->dir == PCMDIR_PLAY);
409 reg = wr? AD1816_PLAY : AD1816_CAPT;
413 /* start only if not already running */
414 if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) {
415 int cnt = ((ch->blksz) >> 2) - 1;
416 ad1816_write(ad1816, wr? 8 : 10, cnt); /* count */
417 ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
418 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) |
419 (wr? 0x8000 : 0x4000)); /* enable int */
420 /* enable playback */
421 io_wr(ad1816, reg, io_rd(ad1816, reg) | AD1816_ENABLE);
422 if (!(io_rd(ad1816, reg) & AD1816_ENABLE))
423 printf("ad1816: failed to start %s DMA!\n",
429 case PCMTRIG_ABORT: /* XXX check this... */
430 /* we don't test here if it is running... */
432 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) &
433 ~(wr? 0x8000 : 0x4000));
435 io_wr(ad1816, reg, io_rd(ad1816, reg) & ~AD1816_ENABLE);
436 /* disable playback */
437 if (io_rd(ad1816, reg) & AD1816_ENABLE)
438 printf("ad1816: failed to stop %s DMA!\n",
440 ad1816_write(ad1816, wr? 8 : 10, 0); /* reset base cnt */
441 ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
445 ad1816_unlock(ad1816);
450 ad1816chan_getptr(kobj_t obj, void *data)
452 struct ad1816_chinfo *ch = data;
453 return sndbuf_isadmaptr(ch->buffer);
456 static struct pcmchan_caps *
457 ad1816chan_getcaps(kobj_t obj, void *data)
462 static kobj_method_t ad1816chan_methods[] = {
463 KOBJMETHOD(channel_init, ad1816chan_init),
464 KOBJMETHOD(channel_setdir, ad1816chan_setdir),
465 KOBJMETHOD(channel_setformat, ad1816chan_setformat),
466 KOBJMETHOD(channel_setspeed, ad1816chan_setspeed),
467 KOBJMETHOD(channel_setblocksize, ad1816chan_setblocksize),
468 KOBJMETHOD(channel_trigger, ad1816chan_trigger),
469 KOBJMETHOD(channel_getptr, ad1816chan_getptr),
470 KOBJMETHOD(channel_getcaps, ad1816chan_getcaps),
473 CHANNEL_DECLARE(ad1816chan);
475 /* -------------------------------------------------------------------- */
478 ad1816_release_resources(struct ad1816_info *ad1816, device_t dev)
482 bus_teardown_intr(dev, ad1816->irq, ad1816->ih);
483 bus_release_resource(dev, SYS_RES_IRQ, ad1816->irq_rid, ad1816->irq);
487 isa_dma_release(rman_get_start(ad1816->drq1));
488 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq1_rid, ad1816->drq1);
492 isa_dma_release(rman_get_start(ad1816->drq2));
493 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq2_rid, ad1816->drq2);
496 if (ad1816->io_base) {
497 bus_release_resource(dev, SYS_RES_IOPORT, ad1816->io_rid, ad1816->io_base);
500 if (ad1816->parent_dmat) {
501 bus_dma_tag_destroy(ad1816->parent_dmat);
502 ad1816->parent_dmat = 0;
505 snd_mtxfree(ad1816->lock);
507 free(ad1816, M_DEVBUF);
511 ad1816_alloc_resources(struct ad1816_info *ad1816, device_t dev)
513 int ok = 1, pdma, rdma;
515 if (!ad1816->io_base)
516 ad1816->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &ad1816->io_rid,
517 0, ~0, 1, RF_ACTIVE);
519 ad1816->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &ad1816->irq_rid,
520 0, ~0, 1, RF_ACTIVE);
522 ad1816->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq1_rid,
523 0, ~0, 1, RF_ACTIVE);
525 ad1816->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq2_rid,
526 0, ~0, 1, RF_ACTIVE);
528 if (!ad1816->io_base || !ad1816->drq1 || !ad1816->irq) ok = 0;
531 pdma = rman_get_start(ad1816->drq1);
532 isa_dma_acquire(pdma);
533 isa_dmainit(pdma, ad1816->bufsize);
535 rdma = rman_get_start(ad1816->drq2);
536 isa_dma_acquire(rdma);
537 isa_dmainit(rdma, ad1816->bufsize);
541 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
548 ad1816_init(struct ad1816_info *ad1816, device_t dev)
550 ad1816_write(ad1816, 1, 0x2); /* disable interrupts */
551 ad1816_write(ad1816, 32, 0x90F0); /* SoundSys Mode, split fmt */
553 ad1816_write(ad1816, 5, 0x8080); /* FM volume mute */
554 ad1816_write(ad1816, 6, 0x8080); /* I2S1 volume mute */
555 ad1816_write(ad1816, 7, 0x8080); /* I2S0 volume mute */
556 ad1816_write(ad1816, 17, 0x8888); /* VID Volume mute */
557 ad1816_write(ad1816, 20, 0x5050); /* recsrc mic, agc off */
558 /* adc gain is set to 0 */
564 ad1816_probe(device_t dev)
567 u_int32_t logical_id = isa_get_logicalid(dev);
569 switch (logical_id) {
570 case 0x80719304: /* ADS7180 */
576 device_set_desc(dev, s);
583 ad1816_attach(device_t dev)
585 struct ad1816_info *ad1816;
586 char status[SND_STATUSLEN], status2[SND_STATUSLEN];
588 ad1816 = (struct ad1816_info *)malloc(sizeof *ad1816, M_DEVBUF, M_NOWAIT | M_ZERO);
589 if (!ad1816) return ENXIO;
591 ad1816->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
594 ad1816->drq1_rid = 0;
595 ad1816->drq2_rid = 1;
596 ad1816->bufsize = pcm_getbuffersize(dev, 4096, DSP_BUFFSIZE, 65536);
598 if (!ad1816_alloc_resources(ad1816, dev)) goto no;
599 ad1816_init(ad1816, dev);
600 if (mixer_init(dev, &ad1816mixer_class, ad1816)) goto no;
602 snd_setup_intr(dev, ad1816->irq, INTR_MPSAFE, ad1816_intr, ad1816, &ad1816->ih);
603 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
604 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
605 /*highaddr*/BUS_SPACE_MAXADDR,
606 /*filter*/NULL, /*filterarg*/NULL,
607 /*maxsize*/ad1816->bufsize, /*nsegments*/1,
609 /*flags*/0, &ad1816->parent_dmat) != 0) {
610 device_printf(dev, "unable to create dma tag\n");
614 snprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(ad1816->drq2));
618 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u",
619 rman_get_start(ad1816->io_base),
620 rman_get_start(ad1816->irq),
621 rman_get_start(ad1816->drq1),
625 if (pcm_register(dev, ad1816, 1, 1)) goto no;
626 pcm_addchan(dev, PCMDIR_REC, &ad1816chan_class, ad1816);
627 pcm_addchan(dev, PCMDIR_PLAY, &ad1816chan_class, ad1816);
628 pcm_setstatus(dev, status);
632 ad1816_release_resources(ad1816, dev);
639 ad1816_detach(device_t dev)
642 struct ad1816_info *ad1816;
644 r = pcm_unregister(dev);
648 ad1816 = pcm_getdevinfo(dev);
649 ad1816_release_resources(ad1816, dev);
653 static device_method_t ad1816_methods[] = {
654 /* Device interface */
655 DEVMETHOD(device_probe, ad1816_probe),
656 DEVMETHOD(device_attach, ad1816_attach),
657 DEVMETHOD(device_detach, ad1816_detach),
662 static driver_t ad1816_driver = {
668 DRIVER_MODULE(snd_ad1816, isa, ad1816_driver, pcm_devclass, 0, 0);
669 MODULE_DEPEND(snd_ad1816, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
670 MODULE_VERSION(snd_ad1816, 1);