2 * Support the ENSONIQ AudioPCI board and Creative Labs SoundBlaster PCI
3 * boards based on the ES1370, ES1371 and ES1373 chips.
5 * Copyright (c) 1999 Russell Cattelan <cattelan@thebarn.com>
6 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
7 * Copyright (c) 1998 by Joachim Kuebart. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * 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
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 * This product includes software developed by Joachim Kuebart.
25 * 4. The name of the author may not be used to endorse or promote
26 * products derived from this software without specific prior
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 * Part of this code was heavily inspired by the linux driver from
44 * Thomas Sailer (sailer@ife.ee.ethz.ch)
45 * Just about everything has been touched and reworked in some way but
46 * the all the underlying sequences/timing/register values are from
51 #ifdef HAVE_KERNEL_OPTION_HEADERS
55 #include <dev/sound/pcm/sound.h>
56 #include <dev/sound/pcm/ac97.h>
57 #include <dev/sound/pci/es137x.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
62 #include <sys/sysctl.h>
66 SND_DECLARE_FILE("$FreeBSD$");
68 #define MEM_MAP_REG 0x14
70 /* PCI IDs of supported chips */
71 #define ES1370_PCI_ID 0x50001274
72 #define ES1371_PCI_ID 0x13711274
73 #define ES1371_PCI_ID2 0x13713274
74 #define CT5880_PCI_ID 0x58801274
75 #define CT4730_PCI_ID 0x89381102
77 #define ES1371REV_ES1371_A 0x02
78 #define ES1371REV_ES1371_B 0x09
80 #define ES1371REV_ES1373_8 0x08
81 #define ES1371REV_ES1373_A 0x04
82 #define ES1371REV_ES1373_B 0x06
84 #define ES1371REV_CT5880_A 0x07
86 #define CT5880REV_CT5880_C 0x02
87 #define CT5880REV_CT5880_D 0x03
88 #define CT5880REV_CT5880_E 0x04
90 #define CT4730REV_CT4730_A 0x00
92 #define ES_DEFAULT_BUFSZ 4096
94 /* 2 DAC for playback, 1 ADC for record */
100 #define ES_DMA_SEGS_MIN 2
101 #define ES_DMA_SEGS_MAX 256
102 #define ES_BLK_MIN 64
103 #define ES_BLK_ALIGN (~(ES_BLK_MIN - 1))
105 #define ES1370_DAC1_MINSPEED 5512
106 #define ES1370_DAC1_MAXSPEED 44100
108 /* device private data */
112 struct es_info *parent;
113 struct pcm_channel *channel;
114 struct snd_dbuf *buffer;
115 struct pcmchan_caps caps;
117 uint32_t fmt, blksz, blkcnt, bufsz;
118 uint32_t ptr, prevptr;
123 * 32bit Ensoniq Configuration (es->escfg).
124 * ----------------------------------------
126 * +-------+--------+------+------+---------+--------+---------+---------+
127 * len | 16 | 1 | 1 | 1 | 2 | 2 | 1 | 8 |
128 * +-------+--------+------+------+---------+--------+---------+---------+
129 * | fixed | single | | | | | is | general |
130 * | rate | pcm | DACx | DACy | numplay | numrec | es1370? | purpose |
131 * | | mixer | | | | | | |
132 * +-------+--------+------+------+---------+--------+---------+---------+
134 #define ES_FIXED_RATE(cfgv) \
135 (((cfgv) & 0xffff0000) >> 16)
136 #define ES_SET_FIXED_RATE(cfgv, nv) \
137 (((cfgv) & ~0xffff0000) | (((nv) & 0xffff) << 16))
138 #define ES_SINGLE_PCM_MIX(cfgv) \
139 (((cfgv) & 0x8000) >> 15)
140 #define ES_SET_SINGLE_PCM_MIX(cfgv, nv) \
141 (((cfgv) & ~0x8000) | (((nv) ? 1 : 0) << 15))
142 #define ES_DAC_FIRST(cfgv) \
143 (((cfgv) & 0x4000) >> 14)
144 #define ES_SET_DAC_FIRST(cfgv, nv) \
145 (((cfgv) & ~0x4000) | (((nv) & 0x1) << 14))
146 #define ES_DAC_SECOND(cfgv) \
147 (((cfgv) & 0x2000) >> 13)
148 #define ES_SET_DAC_SECOND(cfgv, nv) \
149 (((cfgv) & ~0x2000) | (((nv) & 0x1) << 13))
150 #define ES_NUMPLAY(cfgv) \
151 (((cfgv) & 0x1800) >> 11)
152 #define ES_SET_NUMPLAY(cfgv, nv) \
153 (((cfgv) & ~0x1800) | (((nv) & 0x3) << 11))
154 #define ES_NUMREC(cfgv) \
155 (((cfgv) & 0x600) >> 9)
156 #define ES_SET_NUMREC(cfgv, nv) \
157 (((cfgv) & ~0x600) | (((nv) & 0x3) << 9))
158 #define ES_IS_ES1370(cfgv) \
159 (((cfgv) & 0x100) >> 8)
160 #define ES_SET_IS_ES1370(cfgv, nv) \
161 (((cfgv) & ~0x100) | (((nv) ? 1 : 0) << 8))
162 #define ES_GP(cfgv) \
164 #define ES_SET_GP(cfgv, nv) \
165 (((cfgv) & ~0xff) | ((nv) & 0xff))
167 #define ES_DAC1_ENABLED(cfgv) \
168 (ES_NUMPLAY(cfgv) > 1 || \
169 (ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC1))
170 #define ES_DAC2_ENABLED(cfgv) \
171 (ES_NUMPLAY(cfgv) > 1 || \
172 (ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC2))
175 * DAC 1/2 configuration through kernel hint - hint.pcm.<unit>.dac="val"
177 * 0 = Enable both DACs - Default
178 * 1 = Enable single DAC (DAC1)
179 * 2 = Enable single DAC (DAC2)
180 * 3 = Enable both DACs, swap position (DAC2 comes first instead of DAC1)
182 #define ES_DEFAULT_DAC_CFG 0
186 bus_space_handle_t sh;
187 bus_dma_tag_t parent_dmat;
189 struct resource *reg, *irq;
190 int regtype, regid, irqid;
195 unsigned int bufsz, blkcnt;
197 /* Contents of board's registers */
201 struct es_chinfo ch[ES_NCHANS];
203 struct callout poll_timer;
204 int poll_ticks, polling;
207 #define ES_LOCK(sc) snd_mtxlock((sc)->lock)
208 #define ES_UNLOCK(sc) snd_mtxunlock((sc)->lock)
209 #define ES_LOCK_ASSERT(sc) snd_mtxassert((sc)->lock)
212 static void es_intr(void *);
213 static uint32_t es1371_wait_src_ready(struct es_info *);
214 static void es1371_src_write(struct es_info *,
215 unsigned short, unsigned short);
216 static unsigned int es1371_adc_rate(struct es_info *, unsigned int, int);
217 static unsigned int es1371_dac_rate(struct es_info *, unsigned int, int);
218 static int es1371_init(struct es_info *);
219 static int es1370_init(struct es_info *);
220 static int es1370_wrcodec(struct es_info *, unsigned char, unsigned char);
222 static uint32_t es_fmt[] = {
223 SND_FORMAT(AFMT_U8, 1, 0),
224 SND_FORMAT(AFMT_U8, 2, 0),
225 SND_FORMAT(AFMT_S16_LE, 1, 0),
226 SND_FORMAT(AFMT_S16_LE, 2, 0),
229 static struct pcmchan_caps es_caps = {4000, 48000, es_fmt, 0};
231 static const struct {
238 } mixtable[SOUND_MIXER_NRDEVICES] = {
239 [SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x1f7f, 1 },
240 [SOUND_MIXER_PCM] = { 1, 0x2, 0x3, 1, 0x0400, 1 },
241 [SOUND_MIXER_SYNTH] = { 2, 0x4, 0x5, 1, 0x0060, 1 },
242 [SOUND_MIXER_CD] = { 3, 0x6, 0x7, 1, 0x0006, 1 },
243 [SOUND_MIXER_LINE] = { 4, 0x8, 0x9, 1, 0x0018, 1 },
244 [SOUND_MIXER_LINE1] = { 5, 0xa, 0xb, 1, 0x1800, 1 },
245 [SOUND_MIXER_LINE2] = { 6, 0xc, 0x0, 0, 0x0100, 1 },
246 [SOUND_MIXER_LINE3] = { 7, 0xd, 0x0, 0, 0x0200, 1 },
247 [SOUND_MIXER_MIC] = { 8, 0xe, 0x0, 0, 0x0001, 1 },
248 [SOUND_MIXER_OGAIN] = { 9, 0xf, 0x0, 0, 0x0000, 1 }
251 static __inline uint32_t
252 es_rd(struct es_info *es, int regno, int size)
256 return (bus_space_read_1(es->st, es->sh, regno));
258 return (bus_space_read_2(es->st, es->sh, regno));
260 return (bus_space_read_4(es->st, es->sh, regno));
267 es_wr(struct es_info *es, int regno, uint32_t data, int size)
272 bus_space_write_1(es->st, es->sh, regno, data);
275 bus_space_write_2(es->st, es->sh, regno, data);
278 bus_space_write_4(es->st, es->sh, regno, data);
283 /* -------------------------------------------------------------------- */
284 /* The es1370 mixer interface */
287 es1370_mixinit(struct snd_mixer *m)
293 es = mix_getdevinfo(m);
295 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
296 if (mixtable[i].avail)
300 * Each DAC1/2 for ES1370 can be controlled independently
301 * DAC1 = controlled by synth
302 * DAC2 = controlled by pcm
303 * This is indeed can confuse user if DAC1 become primary playback
304 * channel. Try to be smart and combine both if necessary.
306 if (ES_SINGLE_PCM_MIX(es->escfg))
307 v &= ~(1 << SOUND_MIXER_SYNTH);
310 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
311 if (mixtable[i].recmask)
314 if (ES_SINGLE_PCM_MIX(es->escfg)) /* ditto */
315 v &= ~(1 << SOUND_MIXER_SYNTH);
316 mix_setrecdevs(m, v);
321 es1370_mixset(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
324 int l, r, rl, rr, set_dac1;
326 if (!mixtable[dev].avail)
329 r = (mixtable[dev].stereo) ? right : l;
330 if (mixtable[dev].left == 0xf)
331 rl = (l < 2) ? 0x80 : 7 - (l - 2) / 14;
333 rl = (l < 10) ? 0x80 : 15 - (l - 10) / 6;
334 es = mix_getdevinfo(m);
336 if (dev == SOUND_MIXER_PCM && (ES_SINGLE_PCM_MIX(es->escfg)) &&
337 ES_DAC1_ENABLED(es->escfg))
341 if (mixtable[dev].stereo) {
342 rr = (r < 10) ? 0x80 : 15 - (r - 10) / 6;
343 es1370_wrcodec(es, mixtable[dev].right, rr);
344 if (set_dac1 && mixtable[SOUND_MIXER_SYNTH].stereo)
346 mixtable[SOUND_MIXER_SYNTH].right, rr);
348 es1370_wrcodec(es, mixtable[dev].left, rl);
350 es1370_wrcodec(es, mixtable[SOUND_MIXER_SYNTH].left, rl);
353 return (l | (r << 8));
357 es1370_mixsetrecsrc(struct snd_mixer *m, uint32_t src)
362 es = mix_getdevinfo(m);
363 if (src == 0) src = 1 << SOUND_MIXER_MIC;
364 src &= mix_getrecdevs(m);
365 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
366 if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
369 if ((src & (1 << SOUND_MIXER_PCM)) && ES_SINGLE_PCM_MIX(es->escfg) &&
370 ES_DAC1_ENABLED(es->escfg))
371 j |= mixtable[SOUND_MIXER_SYNTH].recmask;
372 es1370_wrcodec(es, CODEC_LIMIX1, j & 0x55);
373 es1370_wrcodec(es, CODEC_RIMIX1, j & 0xaa);
374 es1370_wrcodec(es, CODEC_LIMIX2, (j >> 8) & 0x17);
375 es1370_wrcodec(es, CODEC_RIMIX2, (j >> 8) & 0x0f);
376 es1370_wrcodec(es, CODEC_OMIX1, 0x7f);
377 es1370_wrcodec(es, CODEC_OMIX2, 0x3f);
383 static kobj_method_t es1370_mixer_methods[] = {
384 KOBJMETHOD(mixer_init, es1370_mixinit),
385 KOBJMETHOD(mixer_set, es1370_mixset),
386 KOBJMETHOD(mixer_setrecsrc, es1370_mixsetrecsrc),
389 MIXER_DECLARE(es1370_mixer);
391 /* -------------------------------------------------------------------- */
394 es1370_wrcodec(struct es_info *es, unsigned char i, unsigned char data)
400 for (t = 0; t < 0x1000; t++) {
401 if ((es_rd(es, ES1370_REG_STATUS, 4) &
403 es_wr(es, ES1370_REG_CODEC,
404 ((unsigned short)i << CODEC_INDEX_SHIFT) | data, 2);
409 device_printf(es->dev, "%s: timed out\n", __func__);
413 /* -------------------------------------------------------------------- */
415 /* channel interface */
417 eschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
418 struct pcm_channel *c, int dir)
420 struct es_info *es = devinfo;
421 struct es_chinfo *ch;
426 if (dir == PCMDIR_PLAY) {
427 index = ES_GP(es->escfg);
428 es->escfg = ES_SET_GP(es->escfg, index + 1);
430 index = ES_DAC_FIRST(es->escfg);
432 index = ES_DAC_SECOND(es->escfg);
434 device_printf(es->dev,
435 "Invalid ES_GP index: %d\n", index);
439 if (!(index == ES_DAC1 || index == ES_DAC2)) {
440 device_printf(es->dev, "Unknown DAC: %d\n", index + 1);
444 if (es->ch[index].channel != NULL) {
445 device_printf(es->dev, "DAC%d already initialized!\n",
457 if (ES_IS_ES1370(es->escfg)) {
458 if (ch->index == ES_DAC1) {
459 ch->caps.maxspeed = ES1370_DAC1_MAXSPEED;
460 ch->caps.minspeed = ES1370_DAC1_MINSPEED;
462 uint32_t fixed_rate = ES_FIXED_RATE(es->escfg);
463 if (!(fixed_rate < es_caps.minspeed ||
464 fixed_rate > es_caps.maxspeed)) {
465 ch->caps.maxspeed = fixed_rate;
466 ch->caps.minspeed = fixed_rate;
473 ch->bufsz = es->bufsz;
474 ch->blkcnt = es->blkcnt;
475 ch->blksz = ch->bufsz / ch->blkcnt;
478 if (sndbuf_alloc(ch->buffer, es->parent_dmat, 0, ch->bufsz) != 0)
481 if (dir == PCMDIR_PLAY) {
482 if (ch->index == ES_DAC1) {
483 es_wr(es, ES1370_REG_MEMPAGE,
484 ES1370_REG_DAC1_FRAMEADR >> 8, 1);
485 es_wr(es, ES1370_REG_DAC1_FRAMEADR & 0xff,
486 sndbuf_getbufaddr(ch->buffer), 4);
487 es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
488 (ch->bufsz >> 2) - 1, 4);
490 es_wr(es, ES1370_REG_MEMPAGE,
491 ES1370_REG_DAC2_FRAMEADR >> 8, 1);
492 es_wr(es, ES1370_REG_DAC2_FRAMEADR & 0xff,
493 sndbuf_getbufaddr(ch->buffer), 4);
494 es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
495 (ch->bufsz >> 2) - 1, 4);
498 es_wr(es, ES1370_REG_MEMPAGE, ES1370_REG_ADC_FRAMEADR >> 8, 1);
499 es_wr(es, ES1370_REG_ADC_FRAMEADR & 0xff,
500 sndbuf_getbufaddr(ch->buffer), 4);
501 es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
502 (ch->bufsz >> 2) - 1, 4);
509 eschan_setformat(kobj_t obj, void *data, uint32_t format)
511 struct es_chinfo *ch = data;
512 struct es_info *es = ch->parent;
515 if (ch->dir == PCMDIR_PLAY) {
516 if (ch->index == ES_DAC1) {
517 es->sctrl &= ~SCTRL_P1FMT;
518 if (format & AFMT_S16_LE)
519 es->sctrl |= SCTRL_P1SEB;
520 if (AFMT_CHANNEL(format) > 1)
521 es->sctrl |= SCTRL_P1SMB;
523 es->sctrl &= ~SCTRL_P2FMT;
524 if (format & AFMT_S16_LE)
525 es->sctrl |= SCTRL_P2SEB;
526 if (AFMT_CHANNEL(format) > 1)
527 es->sctrl |= SCTRL_P2SMB;
530 es->sctrl &= ~SCTRL_R1FMT;
531 if (format & AFMT_S16_LE)
532 es->sctrl |= SCTRL_R1SEB;
533 if (AFMT_CHANNEL(format) > 1)
534 es->sctrl |= SCTRL_R1SMB;
536 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
543 eschan1370_setspeed(kobj_t obj, void *data, uint32_t speed)
545 struct es_chinfo *ch = data;
546 struct es_info *es = ch->parent;
549 /* Fixed rate , do nothing. */
550 if (ch->caps.minspeed == ch->caps.maxspeed) {
552 return (ch->caps.maxspeed);
554 if (speed < ch->caps.minspeed)
555 speed = ch->caps.minspeed;
556 if (speed > ch->caps.maxspeed)
557 speed = ch->caps.maxspeed;
558 if (ch->index == ES_DAC1) {
560 * DAC1 does not support continuous rate settings.
561 * Pick the nearest and use it since FEEDER_RATE will
562 * do the the proper conversion for us.
564 es->ctrl &= ~CTRL_WTSRSEL;
567 es->ctrl |= 0 << CTRL_SH_WTSRSEL;
568 } else if (speed < 16537) {
570 es->ctrl |= 1 << CTRL_SH_WTSRSEL;
571 } else if (speed < 33075) {
573 es->ctrl |= 2 << CTRL_SH_WTSRSEL;
576 es->ctrl |= 3 << CTRL_SH_WTSRSEL;
579 es->ctrl &= ~CTRL_PCLKDIV;
580 es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
582 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
588 eschan1371_setspeed(kobj_t obj, void *data, uint32_t speed)
590 struct es_chinfo *ch = data;
591 struct es_info *es = ch->parent;
596 if (ch->dir == PCMDIR_PLAY)
597 i = es1371_dac_rate(es, speed, ch->index); /* play */
599 i = es1371_adc_rate(es, speed, ch->index); /* record */
601 delta = (speed > i) ? (speed - i) : (i - speed);
608 eschan_setfragments(kobj_t obj, void *data, uint32_t blksz, uint32_t blkcnt)
610 struct es_chinfo *ch = data;
611 struct es_info *es = ch->parent;
613 blksz &= ES_BLK_ALIGN;
615 if (blksz > (sndbuf_getmaxsize(ch->buffer) / ES_DMA_SEGS_MIN))
616 blksz = sndbuf_getmaxsize(ch->buffer) / ES_DMA_SEGS_MIN;
617 if (blksz < ES_BLK_MIN)
619 if (blkcnt > ES_DMA_SEGS_MAX)
620 blkcnt = ES_DMA_SEGS_MAX;
621 if (blkcnt < ES_DMA_SEGS_MIN)
622 blkcnt = ES_DMA_SEGS_MIN;
624 while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->buffer)) {
625 if ((blkcnt >> 1) >= ES_DMA_SEGS_MIN)
627 else if ((blksz >> 1) >= ES_BLK_MIN)
633 if ((sndbuf_getblksz(ch->buffer) != blksz ||
634 sndbuf_getblkcnt(ch->buffer) != blkcnt) &&
635 sndbuf_resize(ch->buffer, blkcnt, blksz) != 0)
636 device_printf(es->dev, "%s: failed blksz=%u blkcnt=%u\n",
637 __func__, blksz, blkcnt);
639 ch->bufsz = sndbuf_getsize(ch->buffer);
640 ch->blksz = sndbuf_getblksz(ch->buffer);
641 ch->blkcnt = sndbuf_getblkcnt(ch->buffer);
647 eschan_setblocksize(kobj_t obj, void *data, uint32_t blksz)
649 struct es_chinfo *ch = data;
650 struct es_info *es = ch->parent;
652 eschan_setfragments(obj, data, blksz, es->blkcnt);
657 #define es_chan_active(es) ((es)->ch[ES_DAC1].active + \
658 (es)->ch[ES_DAC2].active + \
659 (es)->ch[ES_ADC].active)
662 es_poll_channel(struct es_chinfo *ch)
668 if (ch == NULL || ch->channel == NULL || ch->active == 0)
672 if (ch->dir == PCMDIR_PLAY) {
673 if (ch->index == ES_DAC1)
674 reg = ES1370_REG_DAC1_FRAMECNT;
676 reg = ES1370_REG_DAC2_FRAMECNT;
678 reg = ES1370_REG_ADC_FRAMECNT;
679 sz = ch->blksz * ch->blkcnt;
680 es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
681 ptr = es_rd(es, reg & 0x000000ff, 4) >> 16;
685 ptr &= ~(ch->blksz - 1);
686 delta = (sz + ptr - ch->prevptr) % sz;
688 if (delta < ch->blksz)
697 es_poll_callback(void *arg)
699 struct es_info *es = arg;
700 uint32_t trigger = 0;
707 if (es->polling == 0 || es_chan_active(es) == 0) {
712 for (i = 0; i < ES_NCHANS; i++) {
713 if (es_poll_channel(&es->ch[i]) != 0)
718 callout_reset(&es->poll_timer, 1/*es->poll_ticks*/,
719 es_poll_callback, es);
723 for (i = 0; i < ES_NCHANS; i++) {
724 if (trigger & (1 << i))
725 chn_intr(es->ch[i].channel);
730 eschan_trigger(kobj_t obj, void *data, int go)
732 struct es_chinfo *ch = data;
733 struct es_info *es = ch->parent;
736 if (!PCMTRIG_COMMON(go))
740 cnt = (ch->blksz / sndbuf_getalign(ch->buffer)) - 1;
741 if (ch->fmt & AFMT_16BIT)
743 if (AFMT_CHANNEL(ch->fmt) > 1)
745 if (ch->dir == PCMDIR_PLAY) {
746 if (go == PCMTRIG_START) {
747 if (ch->index == ES_DAC1) {
748 es->ctrl |= CTRL_DAC1_EN;
749 es->sctrl &= ~(SCTRL_P1LOOPSEL |
750 SCTRL_P1PAUSE | SCTRL_P1SCTRLD);
751 if (es->polling == 0)
752 es->sctrl |= SCTRL_P1INTEN;
754 es->sctrl &= ~SCTRL_P1INTEN;
756 es_wr(es, ES1370_REG_DAC1_SCOUNT, cnt, 4);
757 /* start at beginning of buffer */
758 es_wr(es, ES1370_REG_MEMPAGE,
759 ES1370_REG_DAC1_FRAMECNT >> 8, 4);
760 es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
761 (ch->bufsz >> 2) - 1, 4);
763 es->ctrl |= CTRL_DAC2_EN;
764 es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC |
765 SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
767 if (es->polling == 0)
768 es->sctrl |= SCTRL_P2INTEN;
770 es->sctrl &= ~SCTRL_P2INTEN;
771 es->sctrl |= (b << 2) |
772 ((((b >> 1) & 1) + 1) << SCTRL_SH_P2ENDINC);
773 es_wr(es, ES1370_REG_DAC2_SCOUNT, cnt, 4);
774 /* start at beginning of buffer */
775 es_wr(es, ES1370_REG_MEMPAGE,
776 ES1370_REG_DAC2_FRAMECNT >> 8, 4);
777 es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
778 (ch->bufsz >> 2) - 1, 4);
781 es->ctrl &= ~((ch->index == ES_DAC1) ?
782 CTRL_DAC1_EN : CTRL_DAC2_EN);
784 if (go == PCMTRIG_START) {
785 es->ctrl |= CTRL_ADC_EN;
786 es->sctrl &= ~SCTRL_R1LOOPSEL;
787 if (es->polling == 0)
788 es->sctrl |= SCTRL_R1INTEN;
790 es->sctrl &= ~SCTRL_R1INTEN;
792 es_wr(es, ES1370_REG_ADC_SCOUNT, cnt, 4);
793 /* start at beginning of buffer */
794 es_wr(es, ES1370_REG_MEMPAGE,
795 ES1370_REG_ADC_FRAMECNT >> 8, 4);
796 es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
797 (ch->bufsz >> 2) - 1, 4);
799 es->ctrl &= ~CTRL_ADC_EN;
801 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
802 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
803 if (go == PCMTRIG_START) {
804 if (es->polling != 0) {
807 if (es_chan_active(es) == 0) {
809 callout_reset(&es->poll_timer, 1,
810 es_poll_callback, es);
816 if (es->polling != 0) {
817 if (es_chan_active(es) == 0) {
818 callout_stop(&es->poll_timer);
828 eschan_getptr(kobj_t obj, void *data)
830 struct es_chinfo *ch = data;
831 struct es_info *es = ch->parent;
835 if (es->polling != 0)
838 if (ch->dir == PCMDIR_PLAY) {
839 if (ch->index == ES_DAC1)
840 reg = ES1370_REG_DAC1_FRAMECNT;
842 reg = ES1370_REG_DAC2_FRAMECNT;
844 reg = ES1370_REG_ADC_FRAMECNT;
845 es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
846 cnt = es_rd(es, reg & 0x000000ff, 4) >> 16;
847 /* cnt is longwords */
857 static struct pcmchan_caps *
858 eschan_getcaps(kobj_t obj, void *data)
860 struct es_chinfo *ch = data;
865 static kobj_method_t eschan1370_methods[] = {
866 KOBJMETHOD(channel_init, eschan_init),
867 KOBJMETHOD(channel_setformat, eschan_setformat),
868 KOBJMETHOD(channel_setspeed, eschan1370_setspeed),
869 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
870 KOBJMETHOD(channel_setfragments, eschan_setfragments),
871 KOBJMETHOD(channel_trigger, eschan_trigger),
872 KOBJMETHOD(channel_getptr, eschan_getptr),
873 KOBJMETHOD(channel_getcaps, eschan_getcaps),
876 CHANNEL_DECLARE(eschan1370);
878 static kobj_method_t eschan1371_methods[] = {
879 KOBJMETHOD(channel_init, eschan_init),
880 KOBJMETHOD(channel_setformat, eschan_setformat),
881 KOBJMETHOD(channel_setspeed, eschan1371_setspeed),
882 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
883 KOBJMETHOD(channel_setfragments, eschan_setfragments),
884 KOBJMETHOD(channel_trigger, eschan_trigger),
885 KOBJMETHOD(channel_getptr, eschan_getptr),
886 KOBJMETHOD(channel_getcaps, eschan_getcaps),
889 CHANNEL_DECLARE(eschan1371);
891 /* -------------------------------------------------------------------- */
892 /* The interrupt handler */
896 struct es_info *es = p;
897 uint32_t intsrc, sctrl;
900 if (es->polling != 0) {
904 intsrc = es_rd(es, ES1370_REG_STATUS, 4);
905 if ((intsrc & STAT_INTR) == 0) {
911 if (intsrc & STAT_ADC)
912 sctrl &= ~SCTRL_R1INTEN;
913 if (intsrc & STAT_DAC1)
914 sctrl &= ~SCTRL_P1INTEN;
915 if (intsrc & STAT_DAC2)
916 sctrl &= ~SCTRL_P2INTEN;
918 es_wr(es, ES1370_REG_SERIAL_CONTROL, sctrl, 4);
919 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
922 if (intsrc & STAT_ADC)
923 chn_intr(es->ch[ES_ADC].channel);
924 if (intsrc & STAT_DAC1)
925 chn_intr(es->ch[ES_DAC1].channel);
926 if (intsrc & STAT_DAC2)
927 chn_intr(es->ch[ES_DAC2].channel);
930 /* ES1370 specific */
932 es1370_init(struct es_info *es)
937 /* ES1370 default to fixed rate operation */
938 if (resource_int_value(device_get_name(es->dev),
939 device_get_unit(es->dev), "fixed_rate", &r) == 0) {
942 if (fixed_rate < es_caps.minspeed)
943 fixed_rate = es_caps.minspeed;
944 if (fixed_rate > es_caps.maxspeed)
945 fixed_rate = es_caps.maxspeed;
948 fixed_rate = es_caps.maxspeed;
950 if (resource_int_value(device_get_name(es->dev),
951 device_get_unit(es->dev), "single_pcm_mixer", &r) == 0)
952 single_pcm = (r != 0) ? 1 : 0;
957 if (ES_NUMPLAY(es->escfg) == 1)
960 es->escfg = ES_SET_IS_ES1370(es->escfg, 1);
962 es->escfg = ES_SET_FIXED_RATE(es->escfg, fixed_rate);
964 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
965 fixed_rate = DSP_DEFAULT_SPEED;
968 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
970 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
971 es->ctrl = CTRL_CDC_EN | CTRL_JYSTK_EN | CTRL_SERR_DIS |
972 (DAC2_SRTODIV(fixed_rate) << CTRL_SH_PCLKDIV);
973 es->ctrl |= 3 << CTRL_SH_WTSRSEL;
974 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
977 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
980 es1370_wrcodec(es, CODEC_RES_PD, 3);
982 * CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL;
983 * program DAC_SYNC=0!
985 es1370_wrcodec(es, CODEC_CSEL, 0);
986 /* Recording source is mixer */
987 es1370_wrcodec(es, CODEC_ADSEL, 0);
989 es1370_wrcodec(es, CODEC_MGAIN, 0);
995 /* ES1371 specific */
997 es1371_init(struct es_info *es)
999 uint32_t cssr, devid, revid, subdev;
1003 /* This is NOT ES1370 */
1004 es->escfg = ES_SET_IS_ES1370(es->escfg, 0);
1008 devid = pci_get_devid(es->dev);
1009 revid = pci_get_revid(es->dev);
1010 subdev = (pci_get_subdevice(es->dev) << 16) |
1011 pci_get_subvendor(es->dev);
1013 * Joyport blacklist. Either we're facing with broken hardware
1014 * or because this hardware need special (unknown) initialization
1018 case 0x20001274: /* old Ensoniq */
1022 es->ctrl = CTRL_JYSTK_EN;
1025 if (devid == CT4730_PCI_ID) {
1026 /* XXX amplifier hack? */
1027 es->ctrl |= (1 << 16);
1029 /* initialize the chips */
1030 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1031 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
1032 es_wr(es, ES1371_REG_LEGACY, 0, 4);
1033 if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
1034 (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
1035 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
1036 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
1037 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1039 es_wr(es, ES1370_REG_STATUS, cssr, 4);
1042 /* AC'97 warm reset to start the bitclk */
1043 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1044 es_wr(es, ES1371_REG_LEGACY, ES1371_SYNC_RES, 4);
1046 es_wr(es, ES1370_REG_CONTROL, es->sctrl, 4);
1047 es1371_wait_src_ready(es);
1048 /* Init the sample rate converter */
1049 es_wr(es, ES1371_REG_SMPRATE, ES1371_DIS_SRC, 4);
1050 for (idx = 0; idx < 0x80; idx++)
1051 es1371_src_write(es, idx, 0);
1052 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
1053 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
1054 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
1055 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
1056 es1371_src_write(es, ES_SMPREG_VOL_ADC, 1 << 12);
1057 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, 1 << 12);
1058 es1371_src_write(es, ES_SMPREG_VOL_DAC1, 1 << 12);
1059 es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
1060 es1371_src_write(es, ES_SMPREG_VOL_DAC2, 1 << 12);
1061 es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
1062 es1371_adc_rate(es, 22050, ES_ADC);
1063 es1371_dac_rate(es, 22050, ES_DAC1);
1064 es1371_dac_rate(es, 22050, ES_DAC2);
1067 * enabling the sample rate converter without properly programming
1068 * its parameters causes the chip to lock up (the SRC busy bit will
1069 * be stuck high, and I've found no way to rectify this other than
1072 es1371_wait_src_ready(es);
1073 es_wr(es, ES1371_REG_SMPRATE, 0, 4);
1074 /* try to reset codec directly */
1075 es_wr(es, ES1371_REG_CODEC, 0, 4);
1076 es_wr(es, ES1370_REG_STATUS, cssr, 4);
1082 /* -------------------------------------------------------------------- */
1085 es1371_wrcd(kobj_t obj, void *s, int addr, uint32_t data)
1087 uint32_t t, x, orig;
1088 struct es_info *es = (struct es_info*)s;
1090 for (t = 0; t < 0x1000; t++) {
1091 if (!es_rd(es, ES1371_REG_CODEC & CODEC_WIP, 4))
1094 /* save the current state for later */
1095 x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1096 /* enable SRC state data in SRC mux */
1097 es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1098 ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1100 for (t = 0; t < 0x1000; t++) {
1101 if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1105 /* wait for a SAFE time to write addr/data and then do it, dammit */
1106 for (t = 0; t < 0x1000; t++) {
1107 if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1112 es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1113 CODEC_POADD_MASK) | ((data << CODEC_PODAT_SHIFT) &
1114 CODEC_PODAT_MASK), 4);
1115 /* restore SRC reg */
1116 es1371_wait_src_ready(s);
1117 es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1123 es1371_rdcd(kobj_t obj, void *s, int addr)
1125 uint32_t t, x, orig;
1126 struct es_info *es = (struct es_info *)s;
1128 for (t = 0; t < 0x1000; t++) {
1129 if (!(x = es_rd(es, ES1371_REG_CODEC, 4) & CODEC_WIP))
1133 /* save the current state for later */
1134 x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1135 /* enable SRC state data in SRC mux */
1136 es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1137 ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1139 for (t = 0; t < 0x1000; t++) {
1140 if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1144 /* wait for a SAFE time to write addr/data and then do it, dammit */
1145 for (t = 0; t < 0x1000; t++) {
1146 if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1151 es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1152 CODEC_POADD_MASK) | CODEC_PORD, 4);
1154 /* restore SRC reg */
1155 es1371_wait_src_ready(s);
1156 es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1158 /* now wait for the stinkin' data (RDY) */
1159 for (t = 0; t < 0x1000; t++) {
1160 if ((x = es_rd(es, ES1371_REG_CODEC, 4)) & CODEC_RDY)
1164 return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT);
1167 static kobj_method_t es1371_ac97_methods[] = {
1168 KOBJMETHOD(ac97_read, es1371_rdcd),
1169 KOBJMETHOD(ac97_write, es1371_wrcd),
1172 AC97_DECLARE(es1371_ac97);
1174 /* -------------------------------------------------------------------- */
1177 es1371_src_read(struct es_info *es, unsigned short reg)
1181 r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1182 ES1371_DIS_P2 | ES1371_DIS_R1);
1183 r |= ES1371_SRC_RAM_ADDRO(reg);
1184 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1185 return (ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es)));
1189 es1371_src_write(struct es_info *es, unsigned short reg, unsigned short data)
1193 r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1194 ES1371_DIS_P2 | ES1371_DIS_R1);
1195 r |= ES1371_SRC_RAM_ADDRO(reg) | ES1371_SRC_RAM_DATAO(data);
1196 es_wr(es, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE, 4);
1200 es1371_adc_rate(struct es_info *es, unsigned int rate, int set)
1202 unsigned int n, truncm, freq, result;
1211 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
1213 truncm = (21 * n - 1) | 1;
1214 freq = ((48000UL << 15) / rate) * n;
1215 result = (48000UL << 15) / (freq / n);
1217 if (rate >= 24000) {
1220 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1221 (((239 - truncm) >> 1) << 9) | (n << 4));
1225 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1226 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
1228 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
1229 (es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) &
1230 0x00ff) | ((freq >> 5) & 0xfc00));
1231 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC,
1233 es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8);
1234 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8);
1240 es1371_dac_rate(struct es_info *es, unsigned int rate, int set)
1242 unsigned int freq, r, result, dac, dis;
1250 freq = ((rate << 15) + 1500) / 3000;
1251 result = (freq * 3000) >> 15;
1253 dac = (set == ES_DAC1) ? ES_SMPREG_DAC1 : ES_SMPREG_DAC2;
1254 dis = (set == ES_DAC1) ? ES1371_DIS_P2 : ES1371_DIS_P1;
1255 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1256 ES1371_DIS_P2 | ES1371_DIS_R1));
1257 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1258 es1371_src_write(es, dac + ES_SMPREG_INT_REGS,
1259 (es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) |
1260 ((freq >> 5) & 0xfc00));
1261 es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
1262 r = (es1371_wait_src_ready(es) &
1263 (ES1371_DIS_SRC | dis | ES1371_DIS_R1));
1264 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1269 es1371_wait_src_ready(struct es_info *es)
1273 for (t = 0; t < 0x1000; t++) {
1274 if (!((r = es_rd(es, ES1371_REG_SMPRATE, 4)) &
1275 ES1371_SRC_RAM_BUSY))
1279 device_printf(es->dev, "%s: timed out 0x%x [0x%x]\n", __func__,
1280 ES1371_REG_SMPRATE, r);
1284 /* -------------------------------------------------------------------- */
1287 * Probe and attach the card
1291 es_pci_probe(device_t dev)
1293 switch(pci_get_devid(dev)) {
1295 device_set_desc(dev, "AudioPCI ES1370");
1296 return (BUS_PROBE_DEFAULT);
1298 switch(pci_get_revid(dev)) {
1299 case ES1371REV_ES1371_A:
1300 device_set_desc(dev, "AudioPCI ES1371-A");
1301 return (BUS_PROBE_DEFAULT);
1302 case ES1371REV_ES1371_B:
1303 device_set_desc(dev, "AudioPCI ES1371-B");
1304 return (BUS_PROBE_DEFAULT);
1305 case ES1371REV_ES1373_A:
1306 device_set_desc(dev, "AudioPCI ES1373-A");
1307 return (BUS_PROBE_DEFAULT);
1308 case ES1371REV_ES1373_B:
1309 device_set_desc(dev, "AudioPCI ES1373-B");
1310 return (BUS_PROBE_DEFAULT);
1311 case ES1371REV_ES1373_8:
1312 device_set_desc(dev, "AudioPCI ES1373-8");
1313 return (BUS_PROBE_DEFAULT);
1314 case ES1371REV_CT5880_A:
1315 device_set_desc(dev, "Creative CT5880-A");
1316 return (BUS_PROBE_DEFAULT);
1318 device_set_desc(dev, "AudioPCI ES1371-?");
1320 "unknown revision %d -- please report to "
1321 "freebsd-multimedia@freebsd.org\n",
1322 pci_get_revid(dev));
1323 return (BUS_PROBE_DEFAULT);
1325 case ES1371_PCI_ID2:
1326 device_set_desc(dev, "Strange AudioPCI ES1371-? (vid=3274)");
1328 "unknown revision %d -- please report to "
1329 "freebsd-multimedia@freebsd.org\n", pci_get_revid(dev));
1330 return (BUS_PROBE_DEFAULT);
1332 switch(pci_get_revid(dev)) {
1333 case CT4730REV_CT4730_A:
1334 device_set_desc(dev,
1335 "Creative SB AudioPCI CT4730/EV1938");
1336 return (BUS_PROBE_DEFAULT);
1338 device_set_desc(dev, "Creative SB AudioPCI CT4730-?");
1340 "unknown revision %d -- please report to "
1341 "freebsd-multimedia@freebsd.org\n",
1342 pci_get_revid(dev));
1343 return (BUS_PROBE_DEFAULT);
1346 switch(pci_get_revid(dev)) {
1347 case CT5880REV_CT5880_C:
1348 device_set_desc(dev, "Creative CT5880-C");
1349 return (BUS_PROBE_DEFAULT);
1350 case CT5880REV_CT5880_D:
1351 device_set_desc(dev, "Creative CT5880-D");
1352 return (BUS_PROBE_DEFAULT);
1353 case CT5880REV_CT5880_E:
1354 device_set_desc(dev, "Creative CT5880-E");
1355 return (BUS_PROBE_DEFAULT);
1357 device_set_desc(dev, "Creative CT5880-?");
1359 "unknown revision %d -- please report to "
1360 "freebsd-multimedia@freebsd.org\n",
1361 pci_get_revid(dev));
1362 return (BUS_PROBE_DEFAULT);
1370 sysctl_es137x_spdif_enable(SYSCTL_HANDLER_ARGS)
1377 dev = oidp->oid_arg1;
1378 es = pcm_getdevinfo(dev);
1380 r = es_rd(es, ES1370_REG_STATUS, 4);
1382 new_en = (r & ENABLE_SPDIF) ? 1 : 0;
1383 err = sysctl_handle_int(oidp, &new_en, 0, req);
1385 if (err || req->newptr == NULL)
1387 if (new_en < 0 || new_en > 1)
1393 es->ctrl |= SPDIFEN_B;
1394 es->ctrl |= RECEN_B;
1397 es->ctrl &= ~SPDIFEN_B;
1398 es->ctrl &= ~RECEN_B;
1400 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1401 es_wr(es, ES1370_REG_STATUS, r, 4);
1408 sysctl_es137x_latency_timer(SYSCTL_HANDLER_ARGS)
1415 dev = oidp->oid_arg1;
1416 es = pcm_getdevinfo(dev);
1418 val = pci_read_config(dev, PCIR_LATTIMER, 1);
1420 err = sysctl_handle_int(oidp, &val, 0, req);
1422 if (err || req->newptr == NULL)
1428 pci_write_config(dev, PCIR_LATTIMER, val, 1);
1435 sysctl_es137x_fixed_rate(SYSCTL_HANDLER_ARGS)
1442 dev = oidp->oid_arg1;
1443 es = pcm_getdevinfo(dev);
1445 val = ES_FIXED_RATE(es->escfg);
1446 if (val < es_caps.minspeed)
1449 err = sysctl_handle_int(oidp, &val, 0, req);
1451 if (err || req->newptr == NULL)
1453 if (val != 0 && (val < es_caps.minspeed || val > es_caps.maxspeed))
1457 if (es->ctrl & (CTRL_DAC2_EN|CTRL_ADC_EN)) {
1462 if (val != ES_FIXED_RATE(es->escfg)) {
1463 es->escfg = ES_SET_FIXED_RATE(es->escfg, val);
1464 es->ch[ES_DAC2].caps.maxspeed = val;
1465 es->ch[ES_DAC2].caps.minspeed = val;
1466 es->ch[ES_ADC].caps.maxspeed = val;
1467 es->ch[ES_ADC].caps.minspeed = val;
1468 es->ctrl &= ~CTRL_PCLKDIV;
1469 es->ctrl |= DAC2_SRTODIV(val) << CTRL_SH_PCLKDIV;
1470 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1473 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1474 es->ch[ES_DAC2].caps = es_caps;
1475 es->ch[ES_ADC].caps = es_caps;
1483 sysctl_es137x_single_pcm_mixer(SYSCTL_HANDLER_ARGS)
1486 struct snddev_info *d;
1487 struct snd_mixer *m;
1490 int recsrc, level, err;
1492 dev = oidp->oid_arg1;
1493 d = device_get_softc(dev);
1494 if (!PCM_REGISTERED(d) || d->mixer_dev == NULL ||
1495 d->mixer_dev->si_drv1 == NULL)
1501 set = ES_SINGLE_PCM_MIX(es->escfg);
1504 err = sysctl_handle_int(oidp, &val, 0, req);
1506 if (err || req->newptr == NULL)
1508 if (!(val == 0 || val == 1))
1512 PCM_ACQUIRE_QUICK(d);
1513 m = (d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 : NULL;
1515 PCM_RELEASE_QUICK(d);
1518 if (mixer_busy(m) != 0) {
1519 PCM_RELEASE_QUICK(d);
1522 level = mix_get(m, SOUND_MIXER_PCM);
1523 recsrc = mix_getrecsrc(m);
1524 if (level < 0 || recsrc < 0) {
1525 PCM_RELEASE_QUICK(d);
1530 if (es->ctrl & (CTRL_ADC_EN | CTRL_DAC1_EN | CTRL_DAC2_EN)) {
1532 PCM_RELEASE_QUICK(d);
1536 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
1538 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
1541 mix_setdevs(m, mix_getdevs(m) | (1 << SOUND_MIXER_SYNTH));
1542 mix_setrecdevs(m, mix_getrecdevs(m) | (1 << SOUND_MIXER_SYNTH));
1543 err = mix_set(m, SOUND_MIXER_SYNTH, level & 0x7f,
1544 (level >> 8) & 0x7f);
1546 err = mix_set(m, SOUND_MIXER_SYNTH, level & 0x7f,
1547 (level >> 8) & 0x7f);
1548 mix_setdevs(m, mix_getdevs(m) & ~(1 << SOUND_MIXER_SYNTH));
1549 mix_setrecdevs(m, mix_getrecdevs(m) &
1550 ~(1 << SOUND_MIXER_SYNTH));
1554 if (recsrc & (1 << SOUND_MIXER_PCM))
1555 recsrc |= 1 << SOUND_MIXER_SYNTH;
1556 else if (recsrc & (1 << SOUND_MIXER_SYNTH))
1557 recsrc |= 1 << SOUND_MIXER_PCM;
1558 if (level != recsrc)
1559 err = mix_setrecsrc(m, recsrc);
1562 PCM_RELEASE_QUICK(d);
1568 sysctl_es_polling(SYSCTL_HANDLER_ARGS)
1574 dev = oidp->oid_arg1;
1575 es = pcm_getdevinfo(dev);
1581 err = sysctl_handle_int(oidp, &val, 0, req);
1583 if (err || req->newptr == NULL)
1585 if (val < 0 || val > 1)
1589 if (val != es->polling) {
1590 if (es_chan_active(es) != 0)
1603 es_init_sysctls(device_t dev)
1606 int r, devid, revid;
1608 devid = pci_get_devid(dev);
1609 revid = pci_get_revid(dev);
1610 es = pcm_getdevinfo(dev);
1611 if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
1612 (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
1613 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
1614 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
1615 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1616 /* XXX: an user should be able to set this with a control tool,
1617 if not done before 7.0-RELEASE, this needs to be converted
1618 to a device specific sysctl "dev.pcm.X.yyy" via
1619 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1620 <861wujij2q.fsf@xps.des.no> */
1621 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1622 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1623 "spdif_enabled", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1624 sysctl_es137x_spdif_enable, "I",
1625 "Enable S/PDIF output on primary playback channel");
1626 } else if (devid == ES1370_PCI_ID) {
1628 * Enable fixed rate sysctl if both DAC2 / ADC enabled.
1630 if (es->ch[ES_DAC2].channel != NULL &&
1631 es->ch[ES_ADC].channel != NULL) {
1632 /* XXX: an user should be able to set this with a control tool,
1633 if not done before 7.0-RELEASE, this needs to be converted
1634 to a device specific sysctl "dev.pcm.X.yyy" via
1635 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1636 <861wujij2q.fsf@xps.des.no> */
1637 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1638 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1639 OID_AUTO, "fixed_rate", CTLTYPE_INT | CTLFLAG_RW,
1640 dev, sizeof(dev), sysctl_es137x_fixed_rate, "I",
1641 "Enable fixed rate playback/recording");
1644 * Enable single pcm mixer sysctl if both DAC1/2 enabled.
1646 if (es->ch[ES_DAC1].channel != NULL &&
1647 es->ch[ES_DAC2].channel != NULL) {
1648 /* XXX: an user should be able to set this with a control tool,
1649 if not done before 7.0-RELEASE, this needs to be converted
1650 to a device specific sysctl "dev.pcm.X.yyy" via
1651 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1652 <861wujij2q.fsf@xps.des.no> */
1653 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1654 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1655 OID_AUTO, "single_pcm_mixer",
1656 CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1657 sysctl_es137x_single_pcm_mixer, "I",
1658 "Single PCM mixer controller for both DAC1/DAC2");
1661 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1662 "latency_timer", &r) == 0 && !(r < 0 || r > 255))
1663 pci_write_config(dev, PCIR_LATTIMER, r, 1);
1664 /* XXX: this needs to be converted to a device specific sysctl
1665 "dev.pcm.X.yyy" via device_get_sysctl_*() as discussed on
1666 multimedia@ in msg-id <861wujij2q.fsf@xps.des.no> */
1667 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1668 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1669 "latency_timer", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1670 sysctl_es137x_latency_timer, "I",
1671 "PCI Latency Timer configuration");
1672 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1673 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1674 "polling", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1675 sysctl_es_polling, "I",
1676 "Enable polling mode");
1680 es_pci_attach(device_t dev)
1683 struct es_info *es = NULL;
1684 int mapped, i, numplay, dac_cfg;
1685 char status[SND_STATUSLEN];
1686 struct ac97_info *codec = NULL;
1687 kobj_class_t ct = NULL;
1690 es = malloc(sizeof *es, M_DEVBUF, M_WAITOK | M_ZERO);
1691 es->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_es137x softc");
1696 pci_enable_busmaster(dev);
1697 data = pci_read_config(dev, PCIR_COMMAND, 2);
1698 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
1699 pci_write_config(dev, PCIR_COMMAND, data, 2);
1700 data = pci_read_config(dev, PCIR_COMMAND, 2);
1701 if (mapped == 0 && (data & PCIM_CMD_MEMEN)) {
1702 es->regid = MEM_MAP_REG;
1703 es->regtype = SYS_RES_MEMORY;
1704 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1709 if (mapped == 0 && (data & PCIM_CMD_PORTEN)) {
1710 es->regid = PCIR_BAR(0);
1711 es->regtype = SYS_RES_IOPORT;
1712 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1718 device_printf(dev, "unable to map register space\n");
1722 es->st = rman_get_bustag(es->reg);
1723 es->sh = rman_get_bushandle(es->reg);
1724 callout_init(&es->poll_timer, CALLOUT_MPSAFE);
1727 if (resource_int_value(device_get_name(dev),
1728 device_get_unit(dev), "polling", &i) == 0 && i != 0)
1733 es->bufsz = pcm_getbuffersize(dev, 4096, ES_DEFAULT_BUFSZ, 65536);
1734 if (resource_int_value(device_get_name(dev),
1735 device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
1739 es->blkcnt = es->bufsz / i;
1741 while (es->blkcnt >> i)
1743 es->blkcnt = 1 << (i - 1);
1744 if (es->blkcnt < ES_DMA_SEGS_MIN)
1745 es->blkcnt = ES_DMA_SEGS_MIN;
1746 else if (es->blkcnt > ES_DMA_SEGS_MAX)
1747 es->blkcnt = ES_DMA_SEGS_MAX;
1752 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1753 "dac", &dac_cfg) == 0) {
1754 if (dac_cfg < 0 || dac_cfg > 3)
1755 dac_cfg = ES_DEFAULT_DAC_CFG;
1757 dac_cfg = ES_DEFAULT_DAC_CFG;
1760 case 0: /* Enable all DAC: DAC1, DAC2 */
1762 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1763 es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC2);
1765 case 1: /* Only DAC1 */
1767 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1769 case 3: /* Enable all DAC / swap position: DAC2, DAC1 */
1771 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1772 es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC1);
1774 case 2: /* Only DAC2 */
1777 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1780 es->escfg = ES_SET_NUMPLAY(es->escfg, numplay);
1781 es->escfg = ES_SET_NUMREC(es->escfg, 1);
1783 devid = pci_get_devid(dev);
1786 case ES1371_PCI_ID2:
1790 codec = AC97_CREATE(dev, es, es1371_ac97);
1793 /* our init routine does everything for us */
1794 /* set to NULL; flag mixer_init not to run the ac97_init */
1795 /* ac97_mixer.init = NULL; */
1796 if (mixer_init(dev, ac97_getmixerclass(), codec))
1798 ct = &eschan1371_class;
1803 * Disable fixed rate operation if DAC2 disabled.
1804 * This is a special case for es1370 only, where the
1805 * speed of both ADC and DAC2 locked together.
1807 if (!ES_DAC2_ENABLED(es->escfg))
1808 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1809 if (mixer_init(dev, &es1370_mixer_class, es))
1811 ct = &eschan1370_class;
1819 es->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &es->irqid,
1820 RF_ACTIVE | RF_SHAREABLE);
1821 if (!es->irq || snd_setup_intr(dev, es->irq, INTR_MPSAFE, es_intr,
1823 device_printf(dev, "unable to map interrupt\n");
1827 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev),
1828 /*alignment*/2, /*boundary*/0,
1829 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1830 /*highaddr*/BUS_SPACE_MAXADDR,
1831 /*filter*/NULL, /*filterarg*/NULL,
1832 /*maxsize*/es->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
1833 /*flags*/0, /*lockfunc*/NULL,
1834 /*lockarg*/NULL, &es->parent_dmat) != 0) {
1835 device_printf(dev, "unable to create dma tag\n");
1839 snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld %s",
1840 (es->regtype == SYS_RES_IOPORT)? "io" : "memory",
1841 rman_get_start(es->reg), rman_get_start(es->irq),
1842 PCM_KLDSTRING(snd_es137x));
1844 if (pcm_register(dev, es, numplay, 1))
1846 for (i = 0; i < numplay; i++)
1847 pcm_addchan(dev, PCMDIR_PLAY, ct, es);
1848 pcm_addchan(dev, PCMDIR_REC, ct, es);
1849 es_init_sysctls(dev);
1850 pcm_setstatus(dev, status);
1851 es->escfg = ES_SET_GP(es->escfg, 0);
1853 device_printf(dev, "<Playback: DAC%d / Record: ADC>\n",
1854 ES_DAC_FIRST(es->escfg) + 1);
1855 else if (numplay == 2)
1856 device_printf(dev, "<Playback: DAC%d,DAC%d / Record: ADC>\n",
1857 ES_DAC_FIRST(es->escfg) + 1, ES_DAC_SECOND(es->escfg) + 1);
1861 if (es->parent_dmat)
1862 bus_dma_tag_destroy(es->parent_dmat);
1864 bus_teardown_intr(dev, es->irq, es->ih);
1866 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1868 ac97_destroy(codec);
1870 bus_release_resource(dev, es->regtype, es->regid, es->reg);
1872 snd_mtxfree(es->lock);
1879 es_pci_detach(device_t dev)
1884 r = pcm_unregister(dev);
1888 es = pcm_getdevinfo(dev);
1890 if (es != NULL && es->num != 0) {
1893 callout_stop(&es->poll_timer);
1895 callout_drain(&es->poll_timer);
1898 bus_teardown_intr(dev, es->irq, es->ih);
1899 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1900 bus_release_resource(dev, es->regtype, es->regid, es->reg);
1901 bus_dma_tag_destroy(es->parent_dmat);
1902 snd_mtxfree(es->lock);
1908 static device_method_t es_methods[] = {
1909 /* Device interface */
1910 DEVMETHOD(device_probe, es_pci_probe),
1911 DEVMETHOD(device_attach, es_pci_attach),
1912 DEVMETHOD(device_detach, es_pci_detach),
1917 static driver_t es_driver = {
1923 DRIVER_MODULE(snd_es137x, pci, es_driver, pcm_devclass, 0, 0);
1924 MODULE_DEPEND(snd_es137x, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1925 MODULE_VERSION(snd_es137x, 1);