2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Seigo Tanimura
7 * Portions of this source are based on cwcealdr.cpp and dhwiface.cpp in
8 * cwcealdr1.zip, the sample sources by Crystal Semiconductor.
9 * Copyright (c) 1996-1998 Crystal Semiconductor Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include <dev/sound/pcm/sound.h>
38 #include <dev/sound/pcm/ac97.h>
39 #include <dev/sound/chip.h>
40 #include <dev/sound/pci/csareg.h>
41 #include <dev/sound/pci/csavar.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
46 SND_DECLARE_FILE("$FreeBSD$");
48 /* Buffer size on dma transfer. Fixed for CS416x. */
49 #define CS461x_BUFFSIZE (4 * 1024)
51 #define GOF_PER_SEC 200
53 /* device private data */
57 struct csa_info *parent;
58 struct pcm_channel *channel;
59 struct snd_dbuf *buffer;
66 csa_res res; /* resource */
67 void *ih; /* Interrupt cookie */
68 bus_dma_tag_t parent_dmat; /* DMA tag */
69 struct csa_bridgeinfo *binfo; /* The state of the parent. */
70 struct csa_card *card;
73 /* Contents of board's registers */
77 struct csa_chinfo pch, rch;
78 u_int32_t ac97[CS461x_AC97_NUMBER_RESTORE_REGS];
79 u_int32_t ac97_powerdown;
80 u_int32_t ac97_general_purpose;
83 /* -------------------------------------------------------------------- */
86 static int csa_init(struct csa_info *);
87 static void csa_intr(void *);
88 static void csa_setplaysamplerate(csa_res *resp, u_long ulInRate);
89 static void csa_setcapturesamplerate(csa_res *resp, u_long ulOutRate);
90 static void csa_startplaydma(struct csa_info *csa);
91 static void csa_startcapturedma(struct csa_info *csa);
92 static void csa_stopplaydma(struct csa_info *csa);
93 static void csa_stopcapturedma(struct csa_info *csa);
94 static int csa_startdsp(csa_res *resp);
95 static int csa_stopdsp(csa_res *resp);
96 static int csa_allocres(struct csa_info *scp, device_t dev);
97 static void csa_releaseres(struct csa_info *scp, device_t dev);
98 static void csa_ac97_suspend(struct csa_info *csa);
99 static void csa_ac97_resume(struct csa_info *csa);
101 static u_int32_t csa_playfmt[] = {
102 SND_FORMAT(AFMT_U8, 1, 0),
103 SND_FORMAT(AFMT_U8, 2, 0),
104 SND_FORMAT(AFMT_S8, 1, 0),
105 SND_FORMAT(AFMT_S8, 2, 0),
106 SND_FORMAT(AFMT_S16_LE, 1, 0),
107 SND_FORMAT(AFMT_S16_LE, 2, 0),
108 SND_FORMAT(AFMT_S16_BE, 1, 0),
109 SND_FORMAT(AFMT_S16_BE, 2, 0),
112 static struct pcmchan_caps csa_playcaps = {8000, 48000, csa_playfmt, 0};
114 static u_int32_t csa_recfmt[] = {
115 SND_FORMAT(AFMT_S16_LE, 1, 0),
116 SND_FORMAT(AFMT_S16_LE, 2, 0),
119 static struct pcmchan_caps csa_reccaps = {11025, 48000, csa_recfmt, 0};
121 /* -------------------------------------------------------------------- */
124 csa_active(struct csa_info *csa, int run)
131 if ((csa->active > 1) || (csa->active < -1))
133 if (csa->card->active)
134 return (csa->card->active(!(csa->active && old)));
139 /* -------------------------------------------------------------------- */
143 csa_rdcd(kobj_t obj, void *devinfo, int regno)
146 struct csa_info *csa = (struct csa_info *)devinfo;
149 if (csa_readcodec(&csa->res, regno + BA0_AC97_RESET, &data))
157 csa_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
159 struct csa_info *csa = (struct csa_info *)devinfo;
162 csa_writecodec(&csa->res, regno + BA0_AC97_RESET, data);
168 static kobj_method_t csa_ac97_methods[] = {
169 KOBJMETHOD(ac97_read, csa_rdcd),
170 KOBJMETHOD(ac97_write, csa_wrcd),
173 AC97_DECLARE(csa_ac97);
176 csa_setplaysamplerate(csa_res *resp, u_long ulInRate)
178 u_long ulTemp1, ulTemp2;
180 u_long ulCorrectionPerGOF, ulCorrectionPerSec;
186 * Compute the values used to drive the actual sample rate conversion.
187 * The following formulas are being computed, using inline assembly
188 * since we need to use 64 bit arithmetic to compute the values:
190 * ulPhiIncr = floor((Fs,in * 2^26) / Fs,out)
191 * ulCorrectionPerGOF = floor((Fs,in * 2^26 - Fs,out * ulPhiIncr) /
193 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
194 * GOF_PER_SEC * ulCorrectionPerGOF
198 * ulPhiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
199 * ulCorrectionPerGOF:ulCorrectionPerSec =
200 * dividend:remainder(ulOther / GOF_PER_SEC)
202 ulTemp1 = ulInRate << 16;
203 ulPhiIncr = ulTemp1 / ulOutRate;
204 ulTemp1 -= ulPhiIncr * ulOutRate;
207 ulTemp2 = ulTemp1 / ulOutRate;
208 ulPhiIncr += ulTemp2;
209 ulTemp1 -= ulTemp2 * ulOutRate;
210 ulCorrectionPerGOF = ulTemp1 / GOF_PER_SEC;
211 ulTemp1 -= ulCorrectionPerGOF * GOF_PER_SEC;
212 ulCorrectionPerSec = ulTemp1;
215 * Fill in the SampleRateConverter control block.
217 csa_writemem(resp, BA1_PSRC, ((ulCorrectionPerSec << 16) & 0xFFFF0000) | (ulCorrectionPerGOF & 0xFFFF));
218 csa_writemem(resp, BA1_PPI, ulPhiIncr);
222 csa_setcapturesamplerate(csa_res *resp, u_long ulOutRate)
224 u_long ulPhiIncr, ulCoeffIncr, ulTemp1, ulTemp2;
225 u_long ulCorrectionPerGOF, ulCorrectionPerSec, ulInitialDelay;
226 u_long dwFrameGroupLength, dwCnt;
232 * We can only decimate by up to a factor of 1/9th the hardware rate.
233 * Return an error if an attempt is made to stray outside that limit.
235 if((ulOutRate * 9) < ulInRate)
239 * We can not capture at at rate greater than the Input Rate (48000).
240 * Return an error if an attempt is made to stray outside that limit.
242 if(ulOutRate > ulInRate)
246 * Compute the values used to drive the actual sample rate conversion.
247 * The following formulas are being computed, using inline assembly
248 * since we need to use 64 bit arithmetic to compute the values:
250 * ulCoeffIncr = -floor((Fs,out * 2^23) / Fs,in)
251 * ulPhiIncr = floor((Fs,in * 2^26) / Fs,out)
252 * ulCorrectionPerGOF = floor((Fs,in * 2^26 - Fs,out * ulPhiIncr) /
254 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
255 * GOF_PER_SEC * ulCorrectionPerGOF
256 * ulInitialDelay = ceil((24 * Fs,in) / Fs,out)
260 * ulCoeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
261 * ulPhiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
262 * ulCorrectionPerGOF:ulCorrectionPerSec =
263 * dividend:remainder(ulOther / GOF_PER_SEC)
264 * ulInitialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
266 ulTemp1 = ulOutRate << 16;
267 ulCoeffIncr = ulTemp1 / ulInRate;
268 ulTemp1 -= ulCoeffIncr * ulInRate;
271 ulCoeffIncr += ulTemp1 / ulInRate;
272 ulCoeffIncr ^= 0xFFFFFFFF;
274 ulTemp1 = ulInRate << 16;
275 ulPhiIncr = ulTemp1 / ulOutRate;
276 ulTemp1 -= ulPhiIncr * ulOutRate;
279 ulTemp2 = ulTemp1 / ulOutRate;
280 ulPhiIncr += ulTemp2;
281 ulTemp1 -= ulTemp2 * ulOutRate;
282 ulCorrectionPerGOF = ulTemp1 / GOF_PER_SEC;
283 ulTemp1 -= ulCorrectionPerGOF * GOF_PER_SEC;
284 ulCorrectionPerSec = ulTemp1;
285 ulInitialDelay = ((ulInRate * 24) + ulOutRate - 1) / ulOutRate;
288 * Fill in the VariDecimate control block.
290 csa_writemem(resp, BA1_CSRC,
291 ((ulCorrectionPerSec << 16) & 0xFFFF0000) | (ulCorrectionPerGOF & 0xFFFF));
292 csa_writemem(resp, BA1_CCI, ulCoeffIncr);
293 csa_writemem(resp, BA1_CD,
294 (((BA1_VARIDEC_BUF_1 + (ulInitialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
295 csa_writemem(resp, BA1_CPI, ulPhiIncr);
298 * Figure out the frame group length for the write back task. Basically,
299 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
300 * the output sample rate.
302 dwFrameGroupLength = 1;
303 for(dwCnt = 2; dwCnt <= 64; dwCnt *= 2)
305 if(((ulOutRate / dwCnt) * dwCnt) !=
308 dwFrameGroupLength *= 2;
311 if(((ulOutRate / 3) * 3) !=
314 dwFrameGroupLength *= 3;
316 for(dwCnt = 5; dwCnt <= 125; dwCnt *= 5)
318 if(((ulOutRate / dwCnt) * dwCnt) !=
321 dwFrameGroupLength *= 5;
326 * Fill in the WriteBack control block.
328 csa_writemem(resp, BA1_CFG1, dwFrameGroupLength);
329 csa_writemem(resp, BA1_CFG2, (0x00800000 | dwFrameGroupLength));
330 csa_writemem(resp, BA1_CCST, 0x0000FFFF);
331 csa_writemem(resp, BA1_CSPB, ((65536 * ulOutRate) / 24000));
332 csa_writemem(resp, (BA1_CSPB + 4), 0x0000FFFF);
336 csa_startplaydma(struct csa_info *csa)
343 ul = csa_readmem(resp, BA1_PCTL);
345 csa_writemem(resp, BA1_PCTL, ul | csa->pctl);
346 csa_writemem(resp, BA1_PVOL, 0x80008000);
352 csa_startcapturedma(struct csa_info *csa)
359 ul = csa_readmem(resp, BA1_CCTL);
361 csa_writemem(resp, BA1_CCTL, ul | csa->cctl);
362 csa_writemem(resp, BA1_CVOL, 0x80008000);
368 csa_stopplaydma(struct csa_info *csa)
375 ul = csa_readmem(resp, BA1_PCTL);
376 csa->pctl = ul & 0xffff0000;
377 csa_writemem(resp, BA1_PCTL, ul & 0x0000ffff);
378 csa_writemem(resp, BA1_PVOL, 0xffffffff);
382 * The bitwise pointer of the serial FIFO in the DSP
383 * seems to make an error upon starting or stopping the
384 * DSP. Clear the FIFO and correct the pointer if we
388 csa_clearserialfifos(resp);
389 csa_writeio(resp, BA0_SERBSP, 0);
395 csa_stopcapturedma(struct csa_info *csa)
402 ul = csa_readmem(resp, BA1_CCTL);
403 csa->cctl = ul & 0x0000ffff;
404 csa_writemem(resp, BA1_CCTL, ul & 0xffff0000);
405 csa_writemem(resp, BA1_CVOL, 0xffffffff);
409 * The bitwise pointer of the serial FIFO in the DSP
410 * seems to make an error upon starting or stopping the
411 * DSP. Clear the FIFO and correct the pointer if we
415 csa_clearserialfifos(resp);
416 csa_writeio(resp, BA0_SERBSP, 0);
422 csa_startdsp(csa_res *resp)
428 * Set the frame timer to reflect the number of cycles per frame.
430 csa_writemem(resp, BA1_FRMT, 0xadf);
433 * Turn on the run, run at frame, and DMA enable bits in the local copy of
434 * the SP control register.
436 csa_writemem(resp, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
439 * Wait until the run at frame bit resets itself in the SP control
443 for (i = 0 ; i < 25 ; i++) {
445 * Wait a little bit, so we don't issue PCI reads too frequently.
449 * Fetch the current value of the SP status register.
451 ul = csa_readmem(resp, BA1_SPCR);
454 * If the run at frame bit has reset, then stop waiting.
456 if((ul & SPCR_RUNFR) == 0)
460 * If the run at frame bit never reset, then return an error.
462 if((ul & SPCR_RUNFR) != 0)
469 csa_stopdsp(csa_res *resp)
472 * Turn off the run, run at frame, and DMA enable bits in
473 * the local copy of the SP control register.
475 csa_writemem(resp, BA1_SPCR, 0);
481 csa_setupchan(struct csa_chinfo *ch)
483 struct csa_info *csa = ch->parent;
484 csa_res *resp = &csa->res;
487 if (ch->dir == PCMDIR_PLAY) {
489 csa_writemem(resp, BA1_PBA, sndbuf_getbufaddr(ch->buffer));
492 csa->pfie = csa_readmem(resp, BA1_PFIE) & ~0x0000f03f;
493 if (!(ch->fmt & AFMT_SIGNED))
495 if (ch->fmt & AFMT_BIGENDIAN)
497 if (AFMT_CHANNEL(ch->fmt) < 2)
499 if (ch->fmt & AFMT_8BIT)
501 csa_writemem(resp, BA1_PFIE, csa->pfie);
504 if (ch->fmt & AFMT_16BIT)
506 if (AFMT_CHANNEL(ch->fmt) > 1)
510 pdtc = csa_readmem(resp, BA1_PDTC) & ~0x000001ff;
512 csa_writemem(resp, BA1_PDTC, pdtc);
515 csa_setplaysamplerate(resp, ch->spd);
516 } else if (ch->dir == PCMDIR_REC) {
518 csa_writemem(resp, BA1_CBA, sndbuf_getbufaddr(ch->buffer));
521 csa_writemem(resp, BA1_CIE, (csa_readmem(resp, BA1_CIE) & ~0x0000003f) | 0x00000001);
524 csa_setcapturesamplerate(resp, ch->spd);
529 /* -------------------------------------------------------------------- */
530 /* channel interface */
533 csachan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
535 struct csa_info *csa = devinfo;
536 struct csa_chinfo *ch = (dir == PCMDIR_PLAY)? &csa->pch : &csa->rch;
542 if (sndbuf_alloc(ch->buffer, csa->parent_dmat, 0, CS461x_BUFFSIZE) != 0)
548 csachan_setformat(kobj_t obj, void *data, u_int32_t format)
550 struct csa_chinfo *ch = data;
557 csachan_setspeed(kobj_t obj, void *data, u_int32_t speed)
559 struct csa_chinfo *ch = data;
562 return ch->spd; /* XXX calc real speed */
566 csachan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
568 return CS461x_BUFFSIZE / 2;
572 csachan_trigger(kobj_t obj, void *data, int go)
574 struct csa_chinfo *ch = data;
575 struct csa_info *csa = ch->parent;
577 if (!PCMTRIG_COMMON(go))
580 if (go == PCMTRIG_START) {
583 if (ch->dir == PCMDIR_PLAY)
584 csa_startplaydma(csa);
586 csa_startcapturedma(csa);
588 if (ch->dir == PCMDIR_PLAY)
589 csa_stopplaydma(csa);
591 csa_stopcapturedma(csa);
598 csachan_getptr(kobj_t obj, void *data)
600 struct csa_chinfo *ch = data;
601 struct csa_info *csa = ch->parent;
607 if (ch->dir == PCMDIR_PLAY) {
608 ptr = csa_readmem(resp, BA1_PBA) - sndbuf_getbufaddr(ch->buffer);
609 if ((ch->fmt & AFMT_U8) != 0 || (ch->fmt & AFMT_S8) != 0)
612 ptr = csa_readmem(resp, BA1_CBA) - sndbuf_getbufaddr(ch->buffer);
613 if ((ch->fmt & AFMT_U8) != 0 || (ch->fmt & AFMT_S8) != 0)
620 static struct pcmchan_caps *
621 csachan_getcaps(kobj_t obj, void *data)
623 struct csa_chinfo *ch = data;
624 return (ch->dir == PCMDIR_PLAY)? &csa_playcaps : &csa_reccaps;
627 static kobj_method_t csachan_methods[] = {
628 KOBJMETHOD(channel_init, csachan_init),
629 KOBJMETHOD(channel_setformat, csachan_setformat),
630 KOBJMETHOD(channel_setspeed, csachan_setspeed),
631 KOBJMETHOD(channel_setblocksize, csachan_setblocksize),
632 KOBJMETHOD(channel_trigger, csachan_trigger),
633 KOBJMETHOD(channel_getptr, csachan_getptr),
634 KOBJMETHOD(channel_getcaps, csachan_getcaps),
637 CHANNEL_DECLARE(csachan);
639 /* -------------------------------------------------------------------- */
640 /* The interrupt handler */
644 struct csa_info *csa = p;
646 if ((csa->binfo->hisr & HISR_VC0) != 0)
647 chn_intr(csa->pch.channel);
648 if ((csa->binfo->hisr & HISR_VC1) != 0)
649 chn_intr(csa->rch.channel);
652 /* -------------------------------------------------------------------- */
655 * Probe and attach the card
659 csa_init(struct csa_info *csa)
666 csa_stopplaydma(csa);
667 csa_stopcapturedma(csa);
669 if (csa_startdsp(resp))
672 /* Crank up the power on the DAC and ADC. */
673 csa_setplaysamplerate(resp, 8000);
674 csa_setcapturesamplerate(resp, 8000);
676 csa_writeio(resp, BA0_EGPIODR, EGPIODR_GPOE0);
677 csa_writeio(resp, BA0_EGPIOPTR, EGPIOPTR_GPPT0);
678 /* Power up amplifier */
679 csa_writeio(resp, BA0_EGPIODR, csa_readio(resp, BA0_EGPIODR) |
681 csa_writeio(resp, BA0_EGPIOPTR, csa_readio(resp, BA0_EGPIOPTR) |
687 /* Allocates resources. */
689 csa_allocres(struct csa_info *csa, device_t dev)
694 if (resp->io == NULL) {
695 resp->io = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
696 &resp->io_rid, RF_ACTIVE);
697 if (resp->io == NULL)
700 if (resp->mem == NULL) {
701 resp->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
702 &resp->mem_rid, RF_ACTIVE);
703 if (resp->mem == NULL)
706 if (resp->irq == NULL) {
707 resp->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
708 &resp->irq_rid, RF_ACTIVE | RF_SHAREABLE);
709 if (resp->irq == NULL)
712 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev),
713 /*alignment*/CS461x_BUFFSIZE,
714 /*boundary*/CS461x_BUFFSIZE,
715 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
716 /*highaddr*/BUS_SPACE_MAXADDR,
717 /*filter*/NULL, /*filterarg*/NULL,
718 /*maxsize*/CS461x_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff,
719 /*flags*/0, /*lockfunc*/busdma_lock_mutex,
720 /*lockarg*/&Giant, &csa->parent_dmat) != 0)
726 /* Releases resources. */
728 csa_releaseres(struct csa_info *csa, device_t dev)
732 KASSERT(csa != NULL, ("called with bogus resource structure"));
735 if (resp->irq != NULL) {
737 bus_teardown_intr(dev, resp->irq, csa->ih);
738 bus_release_resource(dev, SYS_RES_IRQ, resp->irq_rid, resp->irq);
741 if (resp->io != NULL) {
742 bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
745 if (resp->mem != NULL) {
746 bus_release_resource(dev, SYS_RES_MEMORY, resp->mem_rid, resp->mem);
749 if (csa->parent_dmat != NULL) {
750 bus_dma_tag_destroy(csa->parent_dmat);
751 csa->parent_dmat = NULL;
758 pcmcsa_probe(device_t dev)
761 struct sndcard_func *func;
763 /* The parent device has already been probed. */
765 func = device_get_ivars(dev);
766 if (func == NULL || func->func != SCF_PCM)
769 s = "CS461x PCM Audio";
771 device_set_desc(dev, s);
776 pcmcsa_attach(device_t dev)
778 struct csa_info *csa;
781 char status[SND_STATUSLEN];
782 struct ac97_info *codec;
783 struct sndcard_func *func;
785 csa = malloc(sizeof(*csa), M_DEVBUF, M_WAITOK | M_ZERO);
786 unit = device_get_unit(dev);
787 func = device_get_ivars(dev);
788 csa->binfo = func->varinfo;
790 * Fake the status of DMA so that the initial value of
791 * PCTL and CCTL can be stored into csa->pctl and csa->cctl,
794 csa->pch.dma = csa->rch.dma = 1;
796 csa->card = csa->binfo->card;
798 /* Allocate the resources. */
800 resp->io_rid = PCIR_BAR(0);
801 resp->mem_rid = PCIR_BAR(1);
803 if (csa_allocres(csa, dev)) {
804 csa_releaseres(csa, dev);
810 csa_releaseres(csa, dev);
813 codec = AC97_CREATE(dev, csa, csa_ac97);
815 csa_releaseres(csa, dev);
818 if (csa->card->inv_eapd)
819 ac97_setflags(codec, AC97_F_EAPD_INV);
820 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) {
822 csa_releaseres(csa, dev);
826 snprintf(status, SND_STATUSLEN, "at irq %jd %s",
827 rman_get_start(resp->irq),PCM_KLDSTRING(snd_csa));
829 /* Enable interrupt. */
830 if (snd_setup_intr(dev, resp->irq, 0, csa_intr, csa, &csa->ih)) {
832 csa_releaseres(csa, dev);
835 csa_writemem(resp, BA1_PFIE, csa_readmem(resp, BA1_PFIE) & ~0x0000f03f);
836 csa_writemem(resp, BA1_CIE, (csa_readmem(resp, BA1_CIE) & ~0x0000003f) | 0x00000001);
839 if (pcm_register(dev, csa, 1, 1)) {
841 csa_releaseres(csa, dev);
844 pcm_addchan(dev, PCMDIR_REC, &csachan_class, csa);
845 pcm_addchan(dev, PCMDIR_PLAY, &csachan_class, csa);
846 pcm_setstatus(dev, status);
852 pcmcsa_detach(device_t dev)
855 struct csa_info *csa;
857 r = pcm_unregister(dev);
861 csa = pcm_getdevinfo(dev);
862 csa_releaseres(csa, dev);
868 csa_ac97_suspend(struct csa_info *csa)
873 for (count = 0x2, i=0;
874 (count <= CS461x_AC97_HIGHESTREGTORESTORE) &&
875 (i < CS461x_AC97_NUMBER_RESTORE_REGS);
877 csa_readcodec(&csa->res, BA0_AC97_RESET + count, &csa->ac97[i]);
879 /* mute the outputs */
880 csa_writecodec(&csa->res, BA0_AC97_MASTER_VOLUME, 0x8000);
881 csa_writecodec(&csa->res, BA0_AC97_HEADPHONE_VOLUME, 0x8000);
882 csa_writecodec(&csa->res, BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
883 csa_writecodec(&csa->res, BA0_AC97_PCM_OUT_VOLUME, 0x8000);
884 /* save the registers that cause pops */
885 csa_readcodec(&csa->res, BA0_AC97_POWERDOWN, &csa->ac97_powerdown);
886 csa_readcodec(&csa->res, BA0_AC97_GENERAL_PURPOSE,
887 &csa->ac97_general_purpose);
890 * And power down everything on the AC97 codec. Well, for now,
891 * only power down the DAC/ADC and MIXER VREFON components.
892 * trouble with removing VREF.
896 csa_readcodec(&csa->res, BA0_AC97_POWERDOWN, &tmp);
897 csa_writecodec(&csa->res, BA0_AC97_POWERDOWN,
898 tmp | CS_AC97_POWER_CONTROL_MIXVON);
900 csa_readcodec(&csa->res, BA0_AC97_POWERDOWN, &tmp);
901 csa_writecodec(&csa->res, BA0_AC97_POWERDOWN,
902 tmp | CS_AC97_POWER_CONTROL_ADC);
904 csa_readcodec(&csa->res, BA0_AC97_POWERDOWN, &tmp);
905 csa_writecodec(&csa->res, BA0_AC97_POWERDOWN,
906 tmp | CS_AC97_POWER_CONTROL_DAC);
910 csa_ac97_resume(struct csa_info *csa)
915 * First, we restore the state of the general purpose register. This
916 * contains the mic select (mic1 or mic2) and if we restore this after
917 * we restore the mic volume/boost state and mic2 was selected at
918 * suspend time, we will end up with a brief period of time where mic1
919 * is selected with the volume/boost settings for mic2, causing
920 * acoustic feedback. So we restore the general purpose register
921 * first, thereby getting the correct mic selected before we restore
922 * the mic volume/boost.
924 csa_writecodec(&csa->res, BA0_AC97_GENERAL_PURPOSE,
925 csa->ac97_general_purpose);
927 * Now, while the outputs are still muted, restore the state of power
930 csa_writecodec(&csa->res, BA0_AC97_POWERDOWN, csa->ac97_powerdown);
932 * Restore just the first set of registers, from register number
933 * 0x02 to the register number that ulHighestRegToRestore specifies.
935 for (count = 0x2, i=0;
936 (count <= CS461x_AC97_HIGHESTREGTORESTORE) &&
937 (i < CS461x_AC97_NUMBER_RESTORE_REGS);
939 csa_writecodec(&csa->res, BA0_AC97_RESET + count, csa->ac97[i]);
943 pcmcsa_suspend(device_t dev)
945 struct csa_info *csa;
948 csa = pcm_getdevinfo(dev);
953 /* playback interrupt disable */
954 csa_writemem(resp, BA1_PFIE,
955 (csa_readmem(resp, BA1_PFIE) & ~0x0000f03f) | 0x00000010);
956 /* capture interrupt disable */
957 csa_writemem(resp, BA1_CIE,
958 (csa_readmem(resp, BA1_CIE) & ~0x0000003f) | 0x00000011);
959 csa_stopplaydma(csa);
960 csa_stopcapturedma(csa);
962 csa_ac97_suspend(csa);
968 * Power down the DAC and ADC. For now leave the other areas on.
970 csa_writecodec(&csa->res, BA0_AC97_POWERDOWN, 0x300);
972 * Power down the PLL.
974 csa_writemem(resp, BA0_CLKCR1, 0);
976 * Turn off the Processor by turning off the software clock
977 * enable flag in the clock control register.
979 csa_writemem(resp, BA0_CLKCR1,
980 csa_readmem(resp, BA0_CLKCR1) & ~CLKCR1_SWCE);
988 pcmcsa_resume(device_t dev)
990 struct csa_info *csa;
993 csa = pcm_getdevinfo(dev);
998 /* cs_hardware_init */
999 csa_stopplaydma(csa);
1000 csa_stopcapturedma(csa);
1001 csa_ac97_resume(csa);
1002 if (csa_startdsp(resp))
1004 /* Enable interrupts on the part. */
1005 if ((csa_readio(resp, BA0_HISR) & HISR_INTENA) == 0)
1006 csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
1007 /* playback interrupt enable */
1008 csa_writemem(resp, BA1_PFIE, csa_readmem(resp, BA1_PFIE) & ~0x0000f03f);
1009 /* capture interrupt enable */
1010 csa_writemem(resp, BA1_CIE,
1011 (csa_readmem(resp, BA1_CIE) & ~0x0000003f) | 0x00000001);
1012 /* cs_restart_part */
1013 csa_setupchan(&csa->pch);
1014 csa_startplaydma(csa);
1015 csa_setupchan(&csa->rch);
1016 csa_startcapturedma(csa);
1018 csa_active(csa, -1);
1023 static device_method_t pcmcsa_methods[] = {
1024 /* Device interface */
1025 DEVMETHOD(device_probe , pcmcsa_probe ),
1026 DEVMETHOD(device_attach, pcmcsa_attach),
1027 DEVMETHOD(device_detach, pcmcsa_detach),
1028 DEVMETHOD(device_suspend, pcmcsa_suspend),
1029 DEVMETHOD(device_resume, pcmcsa_resume),
1034 static driver_t pcmcsa_driver = {
1040 DRIVER_MODULE(snd_csapcm, csa, pcmcsa_driver, pcm_devclass, 0, 0);
1041 MODULE_DEPEND(snd_csapcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1042 MODULE_DEPEND(snd_csapcm, snd_csa, 1, 1, 1);
1043 MODULE_VERSION(snd_csapcm, 1);