Say hello to a sound system update from FreeBSD. This includes the long
[dragonfly.git] / sys / dev / sound / pci / vibes.c
1 /*-
2  * Copyright (c) 2001 Orion Hodson <O.Hodson@cs.ucl.ac.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * This card has the annoying habit of "clicking" when attached and
27  * detached, haven't been able to remedy this with any combination of
28  * muting.
29  * 
30  * $FreeBSD: src/sys/dev/sound/pci/vibes.c,v 1.19.2.1 2006/01/24 18:54:22 joel Exp $
31  * $DragonFly: src/sys/dev/sound/pci/vibes.c,v 1.10 2007/01/04 21:47:02 corecode Exp $
32  */
33
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pci/vibes.h>
36
37 #include <bus/pci/pcireg.h>
38 #include <bus/pci/pcivar.h>
39
40 #include "mixer_if.h"
41
42 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/vibes.c,v 1.10 2007/01/04 21:47:02 corecode Exp $");
43
44 /* ------------------------------------------------------------------------- */
45 /* Constants */
46
47 #define SV_PCI_ID               0xca005333
48 #define SV_DEFAULT_BUFSZ        16384
49 #define SV_MIN_BLKSZ            128
50 #define SV_INTR_PER_BUFFER      2
51
52 #ifndef DEB
53 #define DEB(x) /* (x) */
54 #endif
55
56 /* ------------------------------------------------------------------------- */
57 /* Structures */
58
59 struct sc_info;
60
61 struct sc_chinfo {
62         struct sc_info  *parent;
63         struct pcm_channel      *channel;
64         struct snd_dbuf *buffer;
65         u_int32_t       fmt, spd;
66         int             dir;
67         int             dma_active, dma_was_active;
68 };
69
70 struct sc_info {
71         device_t                dev;
72
73         /* DMA buffer allocator */
74         bus_dma_tag_t           parent_dmat;
75
76         /* Enhanced register resources */
77         struct resource         *enh_reg;
78         bus_space_tag_t         enh_st;
79         bus_space_handle_t      enh_sh;
80         int                     enh_type;
81         int                     enh_rid;
82
83         /* DMA configuration */
84         struct resource         *dmaa_reg, *dmac_reg;
85         bus_space_tag_t         dmaa_st, dmac_st;
86         bus_space_handle_t      dmaa_sh, dmac_sh;
87         int                     dmaa_type, dmac_type;
88         int                     dmaa_rid, dmac_rid;
89
90         /* Interrupt resources */
91         struct resource         *irq;
92         int                     irqid;
93         void                    *ih;
94
95         /* User configurable buffer size */
96         unsigned int            bufsz;
97
98         struct sc_chinfo        rch, pch;
99         u_int8_t                rev;
100 };
101
102 static u_int32_t sc_fmt[] = {
103         AFMT_U8,
104         AFMT_U8 | AFMT_STEREO,
105         AFMT_S16_LE,
106         AFMT_S16_LE | AFMT_STEREO,
107         0
108 };
109
110 static struct pcmchan_caps sc_caps = {8000, 48000, sc_fmt, 0};
111
112 /* ------------------------------------------------------------------------- */
113 /* Register Manipulations */
114
115 #define sv_direct_set(x, y, z) _sv_direct_set(x, y, z, __LINE__)
116
117 static u_int8_t
118 sv_direct_get(struct sc_info *sc, u_int8_t reg)
119 {
120         return bus_space_read_1(sc->enh_st, sc->enh_sh, reg);
121 }
122
123 static void
124 _sv_direct_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line)
125 {
126         u_int8_t n;
127         bus_space_write_1(sc->enh_st, sc->enh_sh, reg, val);
128
129         n = sv_direct_get(sc, reg);
130         if (n != val) {
131                 device_printf(sc->dev, "sv_direct_set register 0x%02x %d != %d from line %d\n", reg, n, val, line);
132         }
133 }
134
135 static u_int8_t
136 sv_indirect_get(struct sc_info *sc, u_int8_t reg)
137 {
138         if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR)
139                 reg |= SV_CM_INDEX_MCE;
140
141         bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg);
142         return bus_space_read_1(sc->enh_st, sc->enh_sh, SV_CM_DATA);
143 }
144
145 #define sv_indirect_set(x, y, z) _sv_indirect_set(x, y, z, __LINE__)
146
147 static void
148 _sv_indirect_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line)
149 {
150         if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR)
151                 reg |= SV_CM_INDEX_MCE;
152
153         bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg);
154         bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_DATA, val);
155
156         reg &= ~SV_CM_INDEX_MCE;
157         if (reg != SV_REG_ADC_PLLM) {
158                 u_int8_t n;
159                 n = sv_indirect_get(sc, reg);
160                 if (n != val) {
161                         device_printf(sc->dev, "sv_indirect_set register 0x%02x %d != %d line %d\n", reg, n, val, line);
162                 }
163         }
164 }
165
166 static void
167 sv_dma_set_config(bus_space_tag_t st, bus_space_handle_t sh,
168                   u_int32_t base, u_int32_t count, u_int8_t mode)
169 {
170         bus_space_write_4(st, sh, SV_DMA_ADDR, base);
171         bus_space_write_4(st, sh, SV_DMA_COUNT, count & 0xffffff);
172         bus_space_write_1(st, sh, SV_DMA_MODE, mode);
173
174         DEB(kprintf("base 0x%08x count %5d mode 0x%02x\n",
175                    base, count, mode));
176 }
177
178 static u_int32_t
179 sv_dma_get_count(bus_space_tag_t st, bus_space_handle_t sh)
180 {
181         return bus_space_read_4(st, sh, SV_DMA_COUNT) & 0xffffff;
182 }
183
184 /* ------------------------------------------------------------------------- */
185 /* Play / Record Common Interface */
186
187 static void *
188 svchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
189 {
190         struct sc_info          *sc = devinfo;
191         struct sc_chinfo        *ch;
192         ch = (dir == PCMDIR_PLAY) ? &sc->pch : &sc->rch;
193
194         ch->parent = sc;
195         ch->channel = c;
196         ch->dir = dir;
197
198         if (sndbuf_alloc(b, sc->parent_dmat, sc->bufsz) != 0) {
199                 DEB(kprintf("svchan_init failed\n"));
200                 return NULL;
201         }
202         ch->buffer = b;
203         ch->fmt = AFMT_U8;
204         ch->spd = DSP_DEFAULT_SPEED;
205         ch->dma_active = ch->dma_was_active = 0;
206
207         return ch;
208 }
209
210 static struct pcmchan_caps *
211 svchan_getcaps(kobj_t obj, void *data)
212 {
213         return &sc_caps;
214 }
215
216 static int
217 svchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
218 {
219         struct sc_chinfo *ch = data;
220         struct sc_info *sc = ch->parent;
221
222         /* user has requested interrupts every blocksize bytes */
223         RANGE(blocksize, SV_MIN_BLKSZ, sc->bufsz / SV_INTR_PER_BUFFER);
224         sndbuf_resize(ch->buffer, SV_INTR_PER_BUFFER, blocksize);
225         DEB(kprintf("svchan_setblocksize: %d\n", blocksize));
226         return blocksize;
227 }
228
229 static int
230 svchan_setformat(kobj_t obj, void *data, u_int32_t format)
231 {
232         struct sc_chinfo *ch = data;
233         /* NB Just note format here as setting format register
234          * generates noise if dma channel is inactive. */
235         ch->fmt  = (format & AFMT_STEREO) ? SV_AFMT_STEREO : SV_AFMT_MONO;
236         ch->fmt |= (format & AFMT_16BIT) ? SV_AFMT_S16 : SV_AFMT_U8;
237         return 0;
238 }
239
240 static int
241 svchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
242 {
243         struct sc_chinfo *ch = data;
244         RANGE(speed, 8000, 48000);
245         ch->spd = speed;
246         return speed;
247 }
248
249 /* ------------------------------------------------------------------------- */
250 /* Recording interface */
251
252 static int
253 sv_set_recspeed(struct sc_info *sc, u_int32_t speed)
254 {
255         u_int32_t       f_out, f_actual;
256         u_int32_t       rs, re, r, best_r = 0, r2, t, n, best_n = 0;
257         int32_t         m, best_m = 0, ms, me, err, min_err;
258
259         /* This algorithm is a variant described in sonicvibes.pdf
260          * appendix A.  This search is marginally more extensive and
261          * results in (nominally) better sample rate matching. */
262
263         f_out = SV_F_SCALE * speed;
264         min_err = 0x7fffffff;
265
266         /* Find bounds of r to examine, rs <= r <= re */
267         t = 80000000 / f_out;
268         for (rs = 1; (1 << rs) < t; rs++);
269
270         t = 150000000 / f_out;
271         for (re = 1; (2 << re) < t; re++);
272         if (re > 7) re = 7;
273
274         /* Search over r, n, m */
275         for (r = rs; r <= re; r++) {
276                 r2 = (1 << r);
277                 for (n = 3; n < 34; n++) {
278                         m = f_out * n / (SV_F_REF / r2);
279                         ms = (m > 3) ? (m - 1) : 3;
280                         me = (m < 129) ? (m + 1) : 129;
281                         for (m = ms; m <= me; m++) {
282                                 f_actual = m * SV_F_REF / (n * r2);
283                                 if (f_actual > f_out) {
284                                         err = f_actual - f_out;
285                                 } else {
286                                         err = f_out - f_actual;
287                                 }
288                                 if (err < min_err) {
289                                         best_r = r;
290                                         best_m = m - 2;
291                                         best_n = n - 2;
292                                         min_err = err;
293                                         if (err == 0) break;
294                                 }
295                         }
296                 }
297         }
298
299         sv_indirect_set(sc, SV_REG_ADC_PLLM, best_m);
300         sv_indirect_set(sc, SV_REG_ADC_PLLN,
301                         SV_ADC_PLLN(best_n) | SV_ADC_PLLR(best_r));
302         DEB(kprintf("svrchan_setspeed: %d -> PLLM 0x%02x PLLNR 0x%08x\n",
303                    speed,
304                    sv_indirect_get(sc, SV_REG_ADC_PLLM),
305                    sv_indirect_get(sc, SV_REG_ADC_PLLN)));
306         return 0;
307 }
308
309 static int
310 svrchan_trigger(kobj_t obj, void *data, int go)
311 {
312         struct sc_chinfo        *ch = data;
313         struct sc_info          *sc = ch->parent;
314         u_int32_t               count, enable;
315         u_int8_t                v;
316
317         switch(go) {
318         case PCMTRIG_START:
319                 /* Set speed */
320                 sv_set_recspeed(sc, ch->spd);
321
322                 /* Set format */
323                 v  = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAC_MSK;
324                 v |= SV_AFMT_DMAC(ch->fmt);
325                 sv_indirect_set(sc, SV_REG_FORMAT, v);
326
327                 /* Program DMA */
328                 count = sndbuf_getsize(ch->buffer) / 2; /* DMAC uses words */
329                 sv_dma_set_config(sc->dmac_st, sc->dmac_sh,
330                                   sndbuf_getbufaddr(ch->buffer),
331                                   count - 1,
332                                   SV_DMA_MODE_AUTO | SV_DMA_MODE_RD);
333                 count = count / SV_INTR_PER_BUFFER - 1;
334                 sv_indirect_set(sc, SV_REG_DMAC_COUNT_HI, count >> 8);
335                 sv_indirect_set(sc, SV_REG_DMAC_COUNT_LO, count & 0xff);
336
337                 /* Enable DMA */
338                 enable = sv_indirect_get(sc, SV_REG_ENABLE) | SV_RECORD_ENABLE;
339                 sv_indirect_set(sc, SV_REG_ENABLE, enable);
340                 ch->dma_active = 1;
341                 break;
342         case PCMTRIG_ABORT:
343                 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_RECORD_ENABLE;
344                 sv_indirect_set(sc, SV_REG_ENABLE, enable);
345                 ch->dma_active = 0;
346                 break;
347         }
348
349         return 0;
350 }
351
352 static int
353 svrchan_getptr(kobj_t obj, void *data)
354 {
355         struct sc_chinfo        *ch = data;
356         struct sc_info          *sc = ch->parent;
357         u_int32_t sz, remain;
358
359         sz = sndbuf_getsize(ch->buffer);
360         /* DMAC uses words */
361         remain = (sv_dma_get_count(sc->dmac_st, sc->dmac_sh) + 1) * 2;
362         return sz - remain;
363 }
364
365 static kobj_method_t svrchan_methods[] = {
366         KOBJMETHOD(channel_init,                svchan_init),
367         KOBJMETHOD(channel_setformat,           svchan_setformat),
368         KOBJMETHOD(channel_setspeed,            svchan_setspeed),
369         KOBJMETHOD(channel_setblocksize,        svchan_setblocksize),
370         KOBJMETHOD(channel_trigger,             svrchan_trigger),
371         KOBJMETHOD(channel_getptr,              svrchan_getptr),
372         KOBJMETHOD(channel_getcaps,             svchan_getcaps),
373         { 0, 0 }
374 };
375 CHANNEL_DECLARE(svrchan);
376
377 /* ------------------------------------------------------------------------- */
378 /* Playback interface */
379
380 static int
381 svpchan_trigger(kobj_t obj, void *data, int go)
382 {
383         struct sc_chinfo        *ch = data;
384         struct sc_info          *sc = ch->parent;
385         u_int32_t               count, enable, speed;
386         u_int8_t                v;
387
388         switch(go) {
389         case PCMTRIG_START:
390                 /* Set speed */
391                 speed = (ch->spd * 65536) / 48000;
392                 if (speed > 65535)
393                         speed = 65535;
394                 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_HI, speed >> 8);
395                 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_LO, speed & 0xff);
396
397                 /* Set format */
398                 v  = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAA_MSK;
399                 v |= SV_AFMT_DMAA(ch->fmt);
400                 sv_indirect_set(sc, SV_REG_FORMAT, v);
401
402                 /* Program DMA */
403                 count = sndbuf_getsize(ch->buffer);
404                 sv_dma_set_config(sc->dmaa_st, sc->dmaa_sh,
405                                   sndbuf_getbufaddr(ch->buffer),
406                                   count - 1,
407                                   SV_DMA_MODE_AUTO | SV_DMA_MODE_WR);
408                 count = count / SV_INTR_PER_BUFFER - 1;
409                 sv_indirect_set(sc, SV_REG_DMAA_COUNT_HI, count >> 8);
410                 sv_indirect_set(sc, SV_REG_DMAA_COUNT_LO, count & 0xff);
411
412                 /* Enable DMA */
413                 enable = sv_indirect_get(sc, SV_REG_ENABLE);
414                 enable = (enable | SV_PLAY_ENABLE) & ~SV_PLAYBACK_PAUSE;
415                 sv_indirect_set(sc, SV_REG_ENABLE, enable);
416                 ch->dma_active = 1;
417                 break;
418         case PCMTRIG_ABORT:
419                 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_PLAY_ENABLE;
420                 sv_indirect_set(sc, SV_REG_ENABLE, enable);
421                 ch->dma_active = 0;
422                 break;
423         }
424
425         return 0;
426 }
427
428 static int
429 svpchan_getptr(kobj_t obj, void *data)
430 {
431         struct sc_chinfo        *ch = data;
432         struct sc_info          *sc = ch->parent;
433         u_int32_t sz, remain;
434
435         sz = sndbuf_getsize(ch->buffer);
436         /* DMAA uses bytes */
437         remain = sv_dma_get_count(sc->dmaa_st, sc->dmaa_sh) + 1;
438         return (sz - remain);
439 }
440
441 static kobj_method_t svpchan_methods[] = {
442         KOBJMETHOD(channel_init,                svchan_init),
443         KOBJMETHOD(channel_setformat,           svchan_setformat),
444         KOBJMETHOD(channel_setspeed,            svchan_setspeed),
445         KOBJMETHOD(channel_setblocksize,        svchan_setblocksize),
446         KOBJMETHOD(channel_trigger,             svpchan_trigger),
447         KOBJMETHOD(channel_getptr,              svpchan_getptr),
448         KOBJMETHOD(channel_getcaps,             svchan_getcaps),
449         { 0, 0 }
450 };
451 CHANNEL_DECLARE(svpchan);
452
453 /* ------------------------------------------------------------------------- */
454 /* Mixer support */
455
456 struct sv_mix_props {
457         u_int8_t        reg;            /* Register */
458         u_int8_t        stereo:1;       /* Supports 2 channels */
459         u_int8_t        mute:1;         /* Supports muting */
460         u_int8_t        neg:1;          /* Negative gain */
461         u_int8_t        max;            /* Max gain */
462         u_int8_t        iselect;        /* Input selector */
463 } static const mt [SOUND_MIXER_NRDEVICES] = {
464         [SOUND_MIXER_LINE1]  = {SV_REG_AUX1,      1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX1},
465         [SOUND_MIXER_CD]     = {SV_REG_CD,        1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_CD},
466         [SOUND_MIXER_LINE]   = {SV_REG_LINE,      1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_LINE},
467         [SOUND_MIXER_MIC]    = {SV_REG_MIC,       0, 1, 1, SV_MIC_MAX,     SV_INPUT_MIC},
468         [SOUND_MIXER_SYNTH]  = {SV_REG_SYNTH,     0, 1, 1, SV_DEFAULT_MAX, 0},
469         [SOUND_MIXER_LINE2]  = {SV_REG_AUX2,      1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX2},
470         [SOUND_MIXER_VOLUME] = {SV_REG_MIX,       1, 1, 1, SV_DEFAULT_MAX, 0},
471         [SOUND_MIXER_PCM]    = {SV_REG_PCM,       1, 1, 1, SV_PCM_MAX,     0},
472         [SOUND_MIXER_RECLEV] = {SV_REG_ADC_INPUT, 1, 0, 0, SV_ADC_MAX, 0},
473 };
474
475 static void
476 sv_channel_gain(struct sc_info *sc, u_int32_t dev, u_int32_t gain, u_int32_t channel)
477 {
478         u_int8_t        v;
479         int32_t         g;
480
481         g = mt[dev].max * gain / 100;
482         if (mt[dev].neg)
483                 g = mt[dev].max - g;
484         v  = sv_indirect_get(sc, mt[dev].reg + channel) & ~mt[dev].max;
485         v |= g;
486
487         if (mt[dev].mute) {
488                 if (gain == 0) {
489                         v |= SV_MUTE;
490                 } else {
491                         v &= ~SV_MUTE;
492                 }
493         }
494         sv_indirect_set(sc, mt[dev].reg + channel, v);
495 }
496
497 static int
498 sv_gain(struct sc_info *sc, u_int32_t dev, u_int32_t left, u_int32_t right)
499 {
500         sv_channel_gain(sc, dev, left, 0);
501         if (mt[dev].stereo)
502                 sv_channel_gain(sc, dev, right, 1);
503         return 0;
504 }
505
506 static void
507 sv_mix_mute_all(struct sc_info *sc)
508 {
509         int32_t i;
510         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
511                 if (mt[i].reg) sv_gain(sc, i, 0, 0);
512         }
513 }
514
515 static int
516 sv_mix_init(struct snd_mixer *m)
517 {
518         u_int32_t       i, v;
519
520         for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
521                 if (mt[i].max) v |= (1 << i);
522         }
523         mix_setdevs(m, v);
524
525         for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
526                 if (mt[i].iselect) v |= (1 << i);
527         }
528         mix_setrecdevs(m, v);
529
530         return 0;
531 }
532
533 static int
534 sv_mix_set(struct snd_mixer *m, u_int32_t dev, u_int32_t left, u_int32_t right)
535 {
536         struct sc_info  *sc = mix_getdevinfo(m);
537         return sv_gain(sc, dev, left, right);
538 }
539
540 static int
541 sv_mix_setrecsrc(struct snd_mixer *m, u_int32_t mask)
542 {
543         struct sc_info  *sc = mix_getdevinfo(m);
544         u_int32_t       i, v;
545
546         v = sv_indirect_get(sc, SV_REG_ADC_INPUT) & SV_INPUT_GAIN_MASK;
547         for(i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
548                 if ((1 << i) & mask) {
549                         v |= mt[i].iselect;
550                 }
551         }
552         DEB(kprintf("sv_mix_setrecsrc: mask 0x%08x adc_input 0x%02x\n", mask, v));
553         sv_indirect_set(sc, SV_REG_ADC_INPUT, v);
554         return mask;
555 }
556
557 static kobj_method_t sv_mixer_methods[] = {
558         KOBJMETHOD(mixer_init,          sv_mix_init),
559         KOBJMETHOD(mixer_set,           sv_mix_set),
560         KOBJMETHOD(mixer_setrecsrc,     sv_mix_setrecsrc),
561         { 0, 0 }
562 };
563 MIXER_DECLARE(sv_mixer);
564
565 /* ------------------------------------------------------------------------- */
566 /* Power management and reset */
567
568 static void
569 sv_power(struct sc_info *sc, int state)
570 {
571         u_int8_t v;
572
573         switch (state) {
574         case 0:
575                 /* power on */
576                 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) &~ SV_ANALOG_OFF;
577                 v |= SV_ANALOG_OFF_SRS | SV_ANALOG_OFF_SPLL;
578                 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v);
579                 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) &~ SV_DIGITAL_OFF;
580                 v |= SV_DIGITAL_OFF_SYN | SV_DIGITAL_OFF_MU | SV_DIGITAL_OFF_GP;
581                 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, v);
582                 break;
583         default:
584                 /* power off */
585                 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) | SV_ANALOG_OFF;
586                 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v);
587                 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) | SV_DIGITAL_OFF;
588                 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, SV_DIGITAL_OFF);
589                 break;
590         }
591         DEB(kprintf("Power state %d\n", state));
592 }
593
594 static int
595 sv_init(struct sc_info *sc)
596 {
597         u_int8_t        v;
598
599         /* Effect reset */
600         v  = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_ENHANCED;
601         v |= SV_CM_CONTROL_RESET;
602         sv_direct_set(sc, SV_CM_CONTROL, v);
603         DELAY(50);
604
605         v = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_RESET;
606         sv_direct_set(sc, SV_CM_CONTROL, v);
607         DELAY(50);
608
609         /* Set in enhanced mode */
610         v = sv_direct_get(sc, SV_CM_CONTROL);
611         v |= SV_CM_CONTROL_ENHANCED;
612         sv_direct_set(sc, SV_CM_CONTROL, v);
613
614         /* Enable interrupts (UDM and MIDM are superfluous) */
615         v = sv_direct_get(sc, SV_CM_IMR);
616         v &= ~(SV_CM_IMR_AMSK | SV_CM_IMR_CMSK | SV_CM_IMR_SMSK);
617         sv_direct_set(sc, SV_CM_IMR, v);
618
619         /* Select ADC PLL for ADC clock */
620         v = sv_indirect_get(sc, SV_REG_CLOCK_SOURCE) & ~SV_CLOCK_ALTERNATE;
621         sv_indirect_set(sc, SV_REG_CLOCK_SOURCE, v);
622
623         /* Disable loopback - binds ADC and DAC rates */
624         v = sv_indirect_get(sc, SV_REG_LOOPBACK) & ~SV_LOOPBACK_ENABLE;
625         sv_indirect_set(sc, SV_REG_LOOPBACK, v);
626
627         /* Disable SRS */
628         v = sv_indirect_get(sc, SV_REG_SRS_SPACE) | SV_SRS_DISABLED;
629         sv_indirect_set(sc, SV_REG_SRS_SPACE, v);
630
631         /* Get revision */
632         sc->rev = sv_indirect_get(sc, SV_REG_REVISION);
633
634         return 0;
635 }
636
637 static int
638 sv_suspend(device_t dev)
639 {
640         struct sc_info  *sc = pcm_getdevinfo(dev);
641
642         sc->rch.dma_was_active = sc->rch.dma_active;
643         svrchan_trigger(NULL, &sc->rch, PCMTRIG_ABORT);
644
645         sc->pch.dma_was_active = sc->pch.dma_active;
646         svrchan_trigger(NULL, &sc->pch, PCMTRIG_ABORT);
647
648         sv_mix_mute_all(sc);
649         sv_power(sc, 3);
650
651         return 0;
652 }
653
654 static int
655 sv_resume(device_t dev)
656 {
657         struct sc_info  *sc = pcm_getdevinfo(dev);
658
659         sv_mix_mute_all(sc);
660         sv_power(sc, 0);
661         if (sv_init(sc) == -1) {
662                 device_printf(dev, "unable to reinitialize the card\n");
663                 return ENXIO;
664         }
665
666         if (mixer_reinit(dev) == -1) {
667                 device_printf(dev, "unable to reinitialize the mixer\n");
668                 return ENXIO;
669         }
670
671         if (sc->rch.dma_was_active) {
672                 svrchan_trigger(0, &sc->rch, PCMTRIG_START);
673         }
674
675         if (sc->pch.dma_was_active) {
676                 svpchan_trigger(0, &sc->pch, PCMTRIG_START);
677         }
678
679         return 0;
680 }
681
682 /* ------------------------------------------------------------------------- */
683 /* Resource related */
684
685 static void
686 sv_intr(void *data)
687 {
688         struct sc_info  *sc = data;
689         u_int8_t        status;
690
691         status = sv_direct_get(sc, SV_CM_STATUS);
692         if (status & SV_CM_STATUS_AINT)
693                 chn_intr(sc->pch.channel);
694
695         if (status & SV_CM_STATUS_CINT)
696                 chn_intr(sc->rch.channel);
697
698         status &= ~(SV_CM_STATUS_AINT|SV_CM_STATUS_CINT);
699         DEB(if (status) kprintf("intr 0x%02x ?\n", status));
700
701         return;
702 }
703
704 static int
705 sv_probe(device_t dev)
706 {
707         switch(pci_get_devid(dev)) {
708         case SV_PCI_ID:
709                 device_set_desc(dev, "S3 Sonicvibes");
710                 return BUS_PROBE_DEFAULT;
711         default:
712                 return ENXIO;
713         }
714 }
715
716 static int
717 sv_attach(device_t dev) {
718         struct sc_info  *sc;
719         u_int32_t       data;
720         char            status[SND_STATUSLEN];
721         u_long          midi_start, games_start, count, sdmaa, sdmac, ml, mu;
722
723         sc = kmalloc(sizeof(struct sc_info), M_DEVBUF, M_NOWAIT | M_ZERO);
724         if (sc == NULL) {
725                 device_printf(dev, "cannot allocate softc");
726                 return ENXIO;
727         }
728         sc->dev = dev;
729
730         data = pci_read_config(dev, PCIR_COMMAND, 2);
731         data |= (PCIM_CMD_PORTEN|PCIM_CMD_BUSMASTEREN);
732         pci_write_config(dev, PCIR_COMMAND, data, 2);
733         data = pci_read_config(dev, PCIR_COMMAND, 2);
734
735 #if __FreeBSD_version > 500000
736         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
737                 device_printf(dev, "chip is in D%d power mode "
738                               "-- setting to D0\n", pci_get_powerstate(dev));
739                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
740         }
741 #endif
742         sc->enh_rid  = SV_PCI_ENHANCED;
743         sc->enh_type = SYS_RES_IOPORT;
744         sc->enh_reg  = bus_alloc_resource(dev, sc->enh_type,
745                                           &sc->enh_rid, 0, ~0,
746                                           SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
747         if (sc->enh_reg == NULL) {
748                 device_printf(dev, "sv_attach: cannot allocate enh\n");
749                 return ENXIO;
750         }
751         sc->enh_st = rman_get_bustag(sc->enh_reg);
752         sc->enh_sh = rman_get_bushandle(sc->enh_reg);
753
754         data = pci_read_config(dev, SV_PCI_DMAA, 4);
755         DEB(kprintf("sv_attach: initial dmaa 0x%08x\n", data));
756         data = pci_read_config(dev, SV_PCI_DMAC, 4);
757         DEB(kprintf("sv_attach: initial dmac 0x%08x\n", data));
758
759         /* Initialize DMA_A and DMA_C */
760         pci_write_config(dev, SV_PCI_DMAA, SV_PCI_DMA_EXTENDED, 4);
761         pci_write_config(dev, SV_PCI_DMAC, 0, 4);
762
763         /* Register IRQ handler */
764         sc->irqid = 0;
765         sc->irq   = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid,
766                                        0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
767         if (!sc->irq ||
768             bus_setup_intr(dev, sc->irq, INTR_TYPE_AV, sv_intr, sc, &sc->ih, NULL)) {
769                 device_printf(dev, "sv_attach: Unable to map interrupt\n");
770                 goto fail;
771         }
772
773         sc->bufsz = pcm_getbuffersize(dev, 4096, SV_DEFAULT_BUFSZ, 65536);
774         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
775                                /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
776                                /*highaddr*/BUS_SPACE_MAXADDR,
777                                /*filter*/NULL, /*filterarg*/NULL,
778                                /*maxsize*/sc->bufsz, /*nsegments*/1,
779                                /*maxsegz*/0x3ffff, /*flags*/0,
780                                &sc->parent_dmat) != 0) {
781                 device_printf(dev, "sv_attach: Unable to create dma tag\n");
782                 goto fail;
783         }
784
785         /* Power up and initialize */
786         sv_mix_mute_all(sc);
787         sv_power(sc, 0);
788         sv_init(sc);
789
790         if (mixer_init(dev, &sv_mixer_class, sc) != 0) {
791                 device_printf(dev, "sv_attach: Mixer failed to initialize\n");
792                 goto fail;
793         }
794
795         /* XXX This is a hack, and it's ugly.  Okay, the deal is this
796          * card has two more io regions that available for automatic
797          * configuration by the pci code.  These need to be allocated
798          * to used as control registers for the DMA engines.
799          * Unfortunately FBSD has no bus_space_foo() functions so we
800          * have to grab port space in region of existing resources.  Go
801          * for space between midi and game ports.
802          */
803         bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_MIDI, &midi_start, &count);
804         bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_GAMES, &games_start, &count);
805
806         if (games_start < midi_start) {
807                 ml = games_start;
808                 mu = midi_start;
809         } else {
810                 ml = midi_start;
811                 mu = games_start;
812         }
813         /* Check assumptions about space availability and
814            alignment. How driver loaded can determine whether
815            games_start > midi_start or vice versa */
816         if ((mu - ml >= 0x800)  ||
817             ((mu - ml) % 0x200)) {
818                 device_printf(dev, "sv_attach: resource assumptions not met "
819                               "(midi 0x%08lx, games 0x%08lx)\n",
820                               midi_start, games_start);
821                 goto fail;
822         }
823
824         sdmaa = ml + 0x40;
825         sdmac = sdmaa + 0x40;
826
827         /* Add resources to list of pci resources for this device - from here on
828          * they look like normal pci resources. */
829         bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAA, sdmaa, SV_PCI_DMAA_SIZE);
830         bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAC, sdmac, SV_PCI_DMAC_SIZE);
831
832         /* Cache resource short-cuts for dma_a */
833         sc->dmaa_rid = SV_PCI_DMAA;
834         sc->dmaa_type = SYS_RES_IOPORT;
835         sc->dmaa_reg  = bus_alloc_resource(dev, sc->dmaa_type,
836                                            &sc->dmaa_rid, 0, ~0,
837                                            SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
838         if (sc->dmaa_reg == NULL) {
839                 device_printf(dev, "sv_attach: cannot allocate dmaa\n");
840                 goto fail;
841         }
842         sc->dmaa_st = rman_get_bustag(sc->dmaa_reg);
843         sc->dmaa_sh = rman_get_bushandle(sc->dmaa_reg);
844
845         /* Poke port into dma_a configuration, nb bit flags to enable dma */
846         data = pci_read_config(dev, SV_PCI_DMAA, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED;
847         data = ((u_int32_t)sdmaa & 0xfffffff0) | (data & 0x0f);
848         pci_write_config(dev, SV_PCI_DMAA, data, 4);
849         DEB(kprintf("dmaa: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAA, 4)));
850
851         /* Cache resource short-cuts for dma_c */
852         sc->dmac_rid = SV_PCI_DMAC;
853         sc->dmac_type = SYS_RES_IOPORT;
854         sc->dmac_reg  = bus_alloc_resource(dev, sc->dmac_type,
855                                            &sc->dmac_rid, 0, ~0,
856                                            SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
857         if (sc->dmac_reg == NULL) {
858                 device_printf(dev, "sv_attach: cannot allocate dmac\n");
859                 goto fail;
860         }
861         sc->dmac_st = rman_get_bustag(sc->dmac_reg);
862         sc->dmac_sh = rman_get_bushandle(sc->dmac_reg);
863
864         /* Poke port into dma_c configuration, nb bit flags to enable dma */
865         data = pci_read_config(dev, SV_PCI_DMAC, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED;
866         data = ((u_int32_t)sdmac & 0xfffffff0) | (data & 0x0f);
867         pci_write_config(dev, SV_PCI_DMAC, data, 4);
868         DEB(kprintf("dmac: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAC, 4)));
869
870         if (bootverbose)
871                 kprintf("Sonicvibes: revision %d.\n", sc->rev);
872
873         if (pcm_register(dev, sc, 1, 1)) {
874                 device_printf(dev, "sv_attach: pcm_register fail\n");
875                 goto fail;
876         }
877
878         pcm_addchan(dev, PCMDIR_PLAY, &svpchan_class, sc);
879         pcm_addchan(dev, PCMDIR_REC,  &svrchan_class, sc);
880
881         ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s",
882                  rman_get_start(sc->enh_reg),  rman_get_start(sc->irq),PCM_KLDSTRING(snd_vibes));
883         pcm_setstatus(dev, status);
884
885         DEB(kprintf("sv_attach: succeeded\n"));
886
887         return 0;
888
889  fail:
890         if (sc->parent_dmat)
891                 bus_dma_tag_destroy(sc->parent_dmat);
892         if (sc->ih)
893                 bus_teardown_intr(dev, sc->irq, sc->ih);
894         if (sc->irq)
895                 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
896         if (sc->enh_reg)
897                 bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg);
898         if (sc->dmaa_reg)
899                 bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg);
900         if (sc->dmac_reg)
901                 bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg);
902         return ENXIO;
903 }
904
905 static int
906 sv_detach(device_t dev) {
907         struct sc_info  *sc;
908         int             r;
909
910         r = pcm_unregister(dev);
911         if (r) return r;
912
913         sc = pcm_getdevinfo(dev);
914         sv_mix_mute_all(sc);
915         sv_power(sc, 3);
916
917         bus_dma_tag_destroy(sc->parent_dmat);
918         bus_teardown_intr(dev, sc->irq, sc->ih);
919         bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
920         bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg);
921         bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg);
922         bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg);
923
924         kfree(sc, M_DEVBUF);
925
926         return 0;
927 }
928
929 static device_method_t sc_methods[] = {
930         DEVMETHOD(device_probe,         sv_probe),
931         DEVMETHOD(device_attach,        sv_attach),
932         DEVMETHOD(device_detach,        sv_detach),
933         DEVMETHOD(device_resume,        sv_resume),
934         DEVMETHOD(device_suspend,       sv_suspend),
935         { 0, 0 }
936 };
937
938 static driver_t sonicvibes_driver = {
939         "pcm",
940         sc_methods,
941         PCM_SOFTC_SIZE
942 };
943
944 DRIVER_MODULE(snd_vibes, pci, sonicvibes_driver, pcm_devclass, 0, 0);
945 MODULE_DEPEND(snd_vibes, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
946 MODULE_VERSION(snd_vibes, 1);