2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
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
28 * $FreeBSD: src/sys/dev/sound/isa/ad1816.c,v 1.37.2.2 2006/04/04 17:23:24 ariff Exp $
31 #include <dev/sound/pcm/sound.h>
32 #include <dev/sound/isa/ad1816.h>
34 #include <bus/isa/isavar.h>
38 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/ad1816.c,v 1.8 2007/06/16 20:07:18 dillon Exp $");
42 struct ad1816_chinfo {
43 struct ad1816_info *parent;
44 struct pcm_channel *channel;
45 struct snd_dbuf *buffer;
50 struct resource *io_base; /* primary I/O address for the board */
54 struct resource *drq1; /* play */
56 struct resource *drq2; /* rec */
59 bus_dma_tag_t parent_dmat;
63 struct ad1816_chinfo pch, rch;
66 static u_int32_t ad1816_fmt[] = {
68 AFMT_STEREO | AFMT_U8,
70 AFMT_STEREO | AFMT_S16_LE,
72 AFMT_STEREO | AFMT_MU_LAW,
74 AFMT_STEREO | AFMT_A_LAW,
78 static struct pcmchan_caps ad1816_caps = {4000, 55200, ad1816_fmt, 0};
80 #define AD1816_MUTE 31 /* value for mute */
83 ad1816_lock(struct ad1816_info *ad1816)
85 snd_mtxlock(ad1816->lock);
89 ad1816_unlock(struct ad1816_info *ad1816)
91 snd_mtxunlock(ad1816->lock);
95 port_rd(struct resource *port, int off)
98 return bus_space_read_1(rman_get_bustag(port),
99 rman_get_bushandle(port),
106 port_wr(struct resource *port, int off, u_int8_t data)
109 bus_space_write_1(rman_get_bustag(port),
110 rman_get_bushandle(port),
115 io_rd(struct ad1816_info *ad1816, int reg)
117 return port_rd(ad1816->io_base, reg);
121 io_wr(struct ad1816_info *ad1816, int reg, u_int8_t data)
123 port_wr(ad1816->io_base, reg, data);
127 ad1816_intr(void *arg)
129 struct ad1816_info *ad1816 = (struct ad1816_info *)arg;
130 unsigned char c, served = 0;
133 /* get interupt status */
134 c = io_rd(ad1816, AD1816_INT);
136 /* check for stray interupts */
137 if (c & ~(AD1816_INTRCI | AD1816_INTRPI)) {
138 kprintf("pcm: stray int (%x)\n", c);
139 c &= AD1816_INTRCI | AD1816_INTRPI;
141 /* check for capture interupt */
142 if (sndbuf_runsz(ad1816->rch.buffer) && (c & AD1816_INTRCI)) {
143 ad1816_unlock(ad1816);
144 chn_intr(ad1816->rch.channel);
146 served |= AD1816_INTRCI; /* cp served */
148 /* check for playback interupt */
149 if (sndbuf_runsz(ad1816->pch.buffer) && (c & AD1816_INTRPI)) {
150 ad1816_unlock(ad1816);
151 chn_intr(ad1816->pch.channel);
153 served |= AD1816_INTRPI; /* pb served */
156 /* this probably means this is not a (working) ad1816 chip, */
157 /* or an error in dma handling */
158 kprintf("pcm: int without reason (%x)\n", c);
161 io_wr(ad1816, AD1816_INT, c);
162 c = io_rd(ad1816, AD1816_INT);
163 if (c != 0) kprintf("pcm: int clear failed (%x)\n", c);
164 ad1816_unlock(ad1816);
168 ad1816_wait_init(struct ad1816_info *ad1816, int x)
170 int n = 0; /* to shut up the compiler... */
173 if ((n = (io_rd(ad1816, AD1816_ALE) & AD1816_BUSY)) == 0) DELAY(10);
175 kprintf("ad1816_wait_init failed 0x%02x.\n", n);
179 static unsigned short
180 ad1816_read(struct ad1816_info *ad1816, unsigned int reg)
184 if (ad1816_wait_init(ad1816, 100) == -1) return 0;
185 io_wr(ad1816, AD1816_ALE, 0);
186 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
187 if (ad1816_wait_init(ad1816, 100) == -1) return 0;
188 x = (io_rd(ad1816, AD1816_HIGH) << 8) | io_rd(ad1816, AD1816_LOW);
193 ad1816_write(struct ad1816_info *ad1816, unsigned int reg, unsigned short data)
195 if (ad1816_wait_init(ad1816, 100) == -1) return;
196 io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
197 io_wr(ad1816, AD1816_LOW, (data & 0x000000ff));
198 io_wr(ad1816, AD1816_HIGH, (data & 0x0000ff00) >> 8);
201 /* -------------------------------------------------------------------- */
204 ad1816mix_init(struct snd_mixer *m)
206 mix_setdevs(m, AD1816_MIXER_DEVICES);
207 mix_setrecdevs(m, AD1816_REC_DEVICES);
212 ad1816mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
214 struct ad1816_info *ad1816 = mix_getdevinfo(m);
218 left = AD1816_MUTE - (AD1816_MUTE * left) / 100;
219 right = AD1816_MUTE - (AD1816_MUTE * right) / 100;
221 reg = (left << 8) | right;
223 /* do channel selective muting if volume is zero */
224 if (left == AD1816_MUTE) reg |= 0x8000;
225 if (right == AD1816_MUTE) reg |= 0x0080;
229 case SOUND_MIXER_VOLUME: /* Register 14 master volume */
230 ad1816_write(ad1816, 14, reg);
233 case SOUND_MIXER_CD: /* Register 15 cd */
234 case SOUND_MIXER_LINE1:
235 ad1816_write(ad1816, 15, reg);
238 case SOUND_MIXER_SYNTH: /* Register 16 synth */
239 ad1816_write(ad1816, 16, reg);
242 case SOUND_MIXER_PCM: /* Register 4 pcm */
243 ad1816_write(ad1816, 4, reg);
246 case SOUND_MIXER_LINE:
247 case SOUND_MIXER_LINE3: /* Register 18 line in */
248 ad1816_write(ad1816, 18, reg);
251 case SOUND_MIXER_MIC: /* Register 19 mic volume */
252 ad1816_write(ad1816, 19, reg & ~0xff); /* mic is mono */
255 case SOUND_MIXER_IGAIN:
256 /* and now to something completely different ... */
257 ad1816_write(ad1816, 20, ((ad1816_read(ad1816, 20) & ~0x0f0f)
258 | (((AD1816_MUTE - left) / 2) << 8) /* four bits of adc gain */
259 | ((AD1816_MUTE - right) / 2)));
263 kprintf("ad1816_mixer_set(): unknown device.\n");
266 ad1816_unlock(ad1816);
268 left = ((AD1816_MUTE - left) * 100) / AD1816_MUTE;
269 right = ((AD1816_MUTE - right) * 100) / AD1816_MUTE;
271 return left | (right << 8);
275 ad1816mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
277 struct ad1816_info *ad1816 = mix_getdevinfo(m);
281 case SOUND_MASK_LINE:
282 case SOUND_MASK_LINE3:
287 case SOUND_MASK_LINE1:
294 src = SOUND_MASK_MIC;
299 ad1816_write(ad1816, 20, (ad1816_read(ad1816, 20) & ~0x7070) | dev);
300 ad1816_unlock(ad1816);
304 static kobj_method_t ad1816mixer_methods[] = {
305 KOBJMETHOD(mixer_init, ad1816mix_init),
306 KOBJMETHOD(mixer_set, ad1816mix_set),
307 KOBJMETHOD(mixer_setrecsrc, ad1816mix_setrecsrc),
310 MIXER_DECLARE(ad1816mixer);
312 /* -------------------------------------------------------------------- */
313 /* channel interface */
315 ad1816chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
317 struct ad1816_info *ad1816 = devinfo;
318 struct ad1816_chinfo *ch = (dir == PCMDIR_PLAY)? &ad1816->pch : &ad1816->rch;
323 if (sndbuf_alloc(ch->buffer, ad1816->parent_dmat, ad1816->bufsize) != 0)
329 ad1816chan_setdir(kobj_t obj, void *data, int dir)
331 struct ad1816_chinfo *ch = data;
332 struct ad1816_info *ad1816 = ch->parent;
334 sndbuf_dmasetup(ch->buffer, (dir == PCMDIR_PLAY)? ad1816->drq1 : ad1816->drq2);
340 ad1816chan_setformat(kobj_t obj, void *data, u_int32_t format)
342 struct ad1816_chinfo *ch = data;
343 struct ad1816_info *ad1816 = ch->parent;
344 int fmt = AD1816_U8, reg;
347 if (ch->dir == PCMDIR_PLAY) {
349 ad1816_write(ad1816, 8, 0x0000); /* reset base and current counter */
350 ad1816_write(ad1816, 9, 0x0000); /* for playback and capture */
353 ad1816_write(ad1816, 10, 0x0000);
354 ad1816_write(ad1816, 11, 0x0000);
356 switch (format & ~AFMT_STEREO) {
377 if (format & AFMT_STEREO) fmt |= AD1816_STEREO;
378 io_wr(ad1816, reg, fmt);
379 ad1816_unlock(ad1816);
388 ad1816chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
390 struct ad1816_chinfo *ch = data;
391 struct ad1816_info *ad1816 = ch->parent;
393 RANGE(speed, 4000, 55200);
395 ad1816_write(ad1816, (ch->dir == PCMDIR_PLAY)? 2 : 3, speed);
396 ad1816_unlock(ad1816);
401 ad1816chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
403 struct ad1816_chinfo *ch = data;
405 ch->blksz = blocksize;
410 ad1816chan_trigger(kobj_t obj, void *data, int go)
412 struct ad1816_chinfo *ch = data;
413 struct ad1816_info *ad1816 = ch->parent;
416 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
419 sndbuf_dma(ch->buffer, go);
420 wr = (ch->dir == PCMDIR_PLAY);
421 reg = wr? AD1816_PLAY : AD1816_CAPT;
425 /* start only if not already running */
426 if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) {
427 int cnt = ((ch->blksz) >> 2) - 1;
428 ad1816_write(ad1816, wr? 8 : 10, cnt); /* count */
429 ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
430 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) |
431 (wr? 0x8000 : 0x4000)); /* enable int */
432 /* enable playback */
433 io_wr(ad1816, reg, io_rd(ad1816, reg) | AD1816_ENABLE);
434 if (!(io_rd(ad1816, reg) & AD1816_ENABLE))
435 kprintf("ad1816: failed to start %s DMA!\n",
441 case PCMTRIG_ABORT: /* XXX check this... */
442 /* we don't test here if it is running... */
444 ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) &
445 ~(wr? 0x8000 : 0x4000));
447 io_wr(ad1816, reg, io_rd(ad1816, reg) & ~AD1816_ENABLE);
448 /* disable playback */
449 if (io_rd(ad1816, reg) & AD1816_ENABLE)
450 kprintf("ad1816: failed to stop %s DMA!\n",
452 ad1816_write(ad1816, wr? 8 : 10, 0); /* reset base cnt */
453 ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
457 ad1816_unlock(ad1816);
462 ad1816chan_getptr(kobj_t obj, void *data)
464 struct ad1816_chinfo *ch = data;
465 return sndbuf_dmaptr(ch->buffer);
468 static struct pcmchan_caps *
469 ad1816chan_getcaps(kobj_t obj, void *data)
474 static kobj_method_t ad1816chan_methods[] = {
475 KOBJMETHOD(channel_init, ad1816chan_init),
476 KOBJMETHOD(channel_setdir, ad1816chan_setdir),
477 KOBJMETHOD(channel_setformat, ad1816chan_setformat),
478 KOBJMETHOD(channel_setspeed, ad1816chan_setspeed),
479 KOBJMETHOD(channel_setblocksize, ad1816chan_setblocksize),
480 KOBJMETHOD(channel_trigger, ad1816chan_trigger),
481 KOBJMETHOD(channel_getptr, ad1816chan_getptr),
482 KOBJMETHOD(channel_getcaps, ad1816chan_getcaps),
485 CHANNEL_DECLARE(ad1816chan);
487 /* -------------------------------------------------------------------- */
490 ad1816_release_resources(struct ad1816_info *ad1816, device_t dev)
494 bus_teardown_intr(dev, ad1816->irq, ad1816->ih);
495 bus_release_resource(dev, SYS_RES_IRQ, ad1816->irq_rid, ad1816->irq);
499 isa_dma_release(rman_get_start(ad1816->drq1));
500 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq1_rid, ad1816->drq1);
504 isa_dma_release(rman_get_start(ad1816->drq2));
505 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq2_rid, ad1816->drq2);
508 if (ad1816->io_base) {
509 bus_release_resource(dev, SYS_RES_IOPORT, ad1816->io_rid, ad1816->io_base);
510 ad1816->io_base = NULL;
512 if (ad1816->parent_dmat) {
513 bus_dma_tag_destroy(ad1816->parent_dmat);
514 ad1816->parent_dmat = 0;
517 snd_mtxfree(ad1816->lock);
519 kfree(ad1816, M_DEVBUF);
523 ad1816_alloc_resources(struct ad1816_info *ad1816, device_t dev)
525 int ok = 1, pdma, rdma;
527 if (!ad1816->io_base)
528 ad1816->io_base = bus_alloc_resource_any(dev,
529 SYS_RES_IOPORT, &ad1816->io_rid, RF_ACTIVE);
531 ad1816->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
532 &ad1816->irq_rid, RF_ACTIVE);
534 ad1816->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
535 &ad1816->drq1_rid, RF_ACTIVE);
537 ad1816->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
538 &ad1816->drq2_rid, RF_ACTIVE);
540 if (!ad1816->io_base || !ad1816->drq1 || !ad1816->irq) ok = 0;
543 pdma = rman_get_start(ad1816->drq1);
544 isa_dma_acquire(pdma);
545 isa_dmainit(pdma, ad1816->bufsize);
547 rdma = rman_get_start(ad1816->drq2);
548 isa_dma_acquire(rdma);
549 isa_dmainit(rdma, ad1816->bufsize);
553 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
560 ad1816_init(struct ad1816_info *ad1816, device_t dev)
562 ad1816_write(ad1816, 1, 0x2); /* disable interrupts */
563 ad1816_write(ad1816, 32, 0x90F0); /* SoundSys Mode, split fmt */
565 ad1816_write(ad1816, 5, 0x8080); /* FM volume mute */
566 ad1816_write(ad1816, 6, 0x8080); /* I2S1 volume mute */
567 ad1816_write(ad1816, 7, 0x8080); /* I2S0 volume mute */
568 ad1816_write(ad1816, 17, 0x8888); /* VID Volume mute */
569 ad1816_write(ad1816, 20, 0x5050); /* recsrc mic, agc off */
570 /* adc gain is set to 0 */
576 ad1816_probe(device_t dev)
579 u_int32_t logical_id = isa_get_logicalid(dev);
581 switch (logical_id) {
582 case 0x80719304: /* ADS7180 */
585 case 0x50719304: /* ADS7150 */
591 device_set_desc(dev, s);
592 return BUS_PROBE_DEFAULT;
598 ad1816_attach(device_t dev)
600 struct ad1816_info *ad1816;
601 char status[SND_STATUSLEN], status2[SND_STATUSLEN];
603 ad1816 = kmalloc(sizeof *ad1816, M_DEVBUF, M_WAITOK | M_ZERO);
604 ad1816->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
607 ad1816->drq1_rid = 0;
608 ad1816->drq2_rid = 1;
609 ad1816->bufsize = pcm_getbuffersize(dev, 4096, DSP_BUFFSIZE, 65536);
611 if (!ad1816_alloc_resources(ad1816, dev)) goto no;
612 ad1816_init(ad1816, dev);
613 if (mixer_init(dev, &ad1816mixer_class, ad1816)) goto no;
615 snd_setup_intr(dev, ad1816->irq, 0, ad1816_intr, ad1816, &ad1816->ih);
616 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
617 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
618 /*highaddr*/BUS_SPACE_MAXADDR,
619 /*filter*/NULL, /*filterarg*/NULL,
620 /*maxsize*/ad1816->bufsize, /*nsegments*/1,
623 &ad1816->parent_dmat) != 0) {
624 device_printf(dev, "unable to create dma tag\n");
628 ksnprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(ad1816->drq2));
632 ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s",
633 rman_get_start(ad1816->io_base),
634 rman_get_start(ad1816->irq),
635 rman_get_start(ad1816->drq1),
638 PCM_KLDSTRING(snd_ad1816));
640 if (pcm_register(dev, ad1816, 1, 1)) goto no;
641 pcm_addchan(dev, PCMDIR_REC, &ad1816chan_class, ad1816);
642 pcm_addchan(dev, PCMDIR_PLAY, &ad1816chan_class, ad1816);
643 pcm_setstatus(dev, status);
647 ad1816_release_resources(ad1816, dev);
654 ad1816_detach(device_t dev)
657 struct ad1816_info *ad1816;
659 r = pcm_unregister(dev);
663 ad1816 = pcm_getdevinfo(dev);
664 ad1816_release_resources(ad1816, dev);
668 static device_method_t ad1816_methods[] = {
669 /* Device interface */
670 DEVMETHOD(device_probe, ad1816_probe),
671 DEVMETHOD(device_attach, ad1816_attach),
672 DEVMETHOD(device_detach, ad1816_detach),
677 static driver_t ad1816_driver = {
683 DRIVER_MODULE(snd_ad1816, isa, ad1816_driver, pcm_devclass, NULL, NULL);
684 DRIVER_MODULE(snd_ad1816, acpi, ad1816_driver, pcm_devclass, NULL, NULL);
685 MODULE_DEPEND(snd_ad1816, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
686 MODULE_VERSION(snd_ad1816, 1);