Lines Matching +full:dac +full:- +full:mode +full:- +full:mask

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for C-Media CMI8338 and 8738 PCI soundcards.
7 /* Does not work. Warning may block system in capture mode */
31 MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
38 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
41 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
42 static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
43 static bool soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
49 MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
51 MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
53 MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
55 MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
59 MODULE_PARM_DESC(soft_ac3, "Software-conversion of raw SPDIF packets (model 033 only).");
80 #define CM_DSFC_MASK 0x0000E000 /* channel 1 (DAC?) sampling frequency */
86 #define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/IN -> OUT loopback */
87 #define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
105 #define CM_SPD24SEL39 0x00200000 /* 24-bit spdif: model 039 */
169 #define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
176 #define CM_NXCHG 0x80000000 /* don't map base reg dword->sample */
195 #define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
203 #define CM_CENTR2LIN 0x00004000 /* line-in as center out */
204 #define CM_BASE2LIN 0x00002000 /* line-in as bass out */
210 #define CM_SFIL_MASK 0x30000000 /* filter control at front end DAC, model 037? */
217 #define CM_ENDBDAC 0x00800000 /* enable double dac */
220 #define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
226 #define CM_MASK_EN 0x00004000 /* activate channel mask on legacy DMA */
229 #define CM_SFILENB 0x00001000 /* filter stepping at front end DAC, model 037? */
230 #define CM_MMODE_MASK 0x00000E00 /* model DAA interface mode */
234 #define CM_MUTECH1 0x00000040 /* mute PCI ch1 to DAC */
236 #define CM_MIDSMP 0x00000010 /* 1/2 interpolation at front end DAC */
274 #define CM_REAR2LIN 0x20 /* lin-in -> rear line out */
284 #define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
296 #define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
316 * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
322 * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
327 * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
332 * access from SB-mixer port
335 #define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
339 #define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
340 #define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
344 * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
345 * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
355 #define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
356 #define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
363 #define CM_SHAREADC 0x00040000 /* DAC in ADC as Center/LFE */
416 u8 running; /* dac/adc running? */
429 int toggle_on; /* value to change when ac3 mode */
435 // {"IEC958 Out To DAC", 1}, // no longer used
450 struct snd_pcm *pcm; /* DAC/ADC PCM */
451 struct snd_pcm *pcm2; /* 2nd DAC */
471 int opened[2]; /* open mode */
478 struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
497 outl(data, cm->iobase + cmd);
502 return inl(cm->iobase + cmd);
508 outw(data, cm->iobase + cmd);
513 return inw(cm->iobase + cmd);
519 outb(data, cm->iobase + cmd);
524 return inb(cm->iobase + cmd);
531 val = oval = inl(cm->iobase + cmd);
535 outl(val, cm->iobase + cmd);
542 val = oval = inl(cm->iobase + cmd);
546 outl(val, cm->iobase + cmd);
554 val = oval = inb(cm->iobase + cmd);
558 outb(val, cm->iobase + cmd);
565 val = oval = inb(cm->iobase + cmd);
569 outb(val, cm->iobase + cmd);
599 * does it this way .. maybe not. Never get any information from C-Media about
609 *n = -1;
619 delta = rate - xr;
621 delta = xr - rate;
629 *m = xm - 2;
630 *n = xn - 2;
635 return (*n > -1);
640 * are mapped onto the 8 ADC/DAC sampling frequency which can be chosen
667 guard(mutex)(&cm->open_mutex);
668 if (cm->opened[CM_CH_PLAY])
669 return -EBUSY;
671 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
678 int reset = CM_RST_CH0 << (cm->channel[ch].ch);
679 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
680 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
692 .mask = 0,
697 .mask = 0,
702 .mask = 0,
708 if (!cm->can_multi_ch || !rec->ch)
709 return -EINVAL;
710 if (rec->fmt != 0x03) /* stereo 16bit only */
711 return -EINVAL;
714 if (cm->can_multi_ch) {
715 guard(spinlock_irq)(&cm->reg_lock);
745 * channel to be used must have been set in rec->ch.
752 struct snd_pcm_runtime *runtime = substream->runtime;
754 rec->fmt = 0;
755 rec->shift = 0;
756 if (snd_pcm_format_width(runtime->format) >= 16) {
757 rec->fmt |= 0x02;
758 if (snd_pcm_format_width(runtime->format) > 16)
759 rec->shift++; /* 24/32bit */
761 if (runtime->channels > 1)
762 rec->fmt |= 0x01;
763 if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
764 dev_dbg(cm->card->dev, "cannot set dac channels\n");
765 return -EINVAL;
768 rec->offset = runtime->dma_addr;
770 rec->dma_size = runtime->buffer_size << rec->shift;
771 period_size = runtime->period_size << rec->shift;
772 if (runtime->channels > 2) {
773 /* multi-channels */
774 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
775 period_size = (period_size * runtime->channels) / 2;
778 guard(spinlock_irq)(&cm->reg_lock);
781 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
782 snd_cmipci_write(cm, reg, rec->offset);
784 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
785 snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
786 snd_cmipci_write_w(cm, reg + 2, period_size - 1);
788 /* set adc/dac flag */
789 val = rec->ch ? CM_CHADC1 : CM_CHADC0;
790 if (rec->is_dac)
791 cm->ctrl &= ~val;
793 cm->ctrl |= val;
794 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
795 /* dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl); */
800 if (runtime->rate > 48000)
801 switch (runtime->rate) {
808 freq = snd_cmipci_rate_freq(runtime->rate);
810 if (rec->ch) {
818 dev_dbg(cm->card->dev, "functrl1 = %08x\n", val);
822 if (rec->ch) {
824 val |= rec->fmt << CM_CH1FMT_SHIFT;
827 val |= rec->fmt << CM_CH0FMT_SHIFT;
829 if (cm->can_96k) {
830 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
831 val |= freq_ext << (rec->ch * 2);
834 dev_dbg(cm->card->dev, "chformat = %08x\n", val);
836 if (!rec->is_dac && cm->chip_version) {
837 if (runtime->rate > 44100)
843 rec->running = 0;
856 inthld = CM_CH0_INT_EN << rec->ch;
857 chen = CM_CHEN0 << rec->ch;
858 reset = CM_RST_CH0 << rec->ch;
859 pause = CM_PAUSE0 << rec->ch;
861 guard(spinlock)(&cm->reg_lock);
864 rec->running = 1;
867 cm->ctrl |= chen;
869 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
870 dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl);
873 rec->running = 0;
877 cm->ctrl &= ~chen;
878 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
879 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
880 rec->needs_silencing = rec->is_dac;
884 cm->ctrl |= pause;
885 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
889 cm->ctrl &= ~pause;
890 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
893 return -EINVAL;
907 if (!rec->running)
910 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
913 if (rem < rec->dma_size)
916 dev_err(cm->card->dev, "invalid PCM pointer: %#x\n", rem);
919 ptr = (rec->dma_size - (rem + 1)) >> rec->shift;
921 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
922 ptr = snd_cmipci_read(cm, reg) - rec->offset;
923 ptr = bytes_to_frames(substream->runtime, ptr);
925 if (substream->runtime->channels > 2)
926 ptr = (ptr * 2) / substream->runtime->channels;
938 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
944 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
957 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
963 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
974 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
975 uinfo->count = 1;
985 guard(spinlock_irq)(&chip->reg_lock);
987 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
999 guard(spinlock_irq)(&chip->reg_lock);
1001 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1002 change = val != chip->dig_status;
1003 chip->dig_status = val;
1019 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1020 uinfo->count = 1;
1027 ucontrol->value.iec958.status[0] = 0xff;
1028 ucontrol->value.iec958.status[1] = 0xff;
1029 ucontrol->value.iec958.status[2] = 0xff;
1030 ucontrol->value.iec958.status[3] = 0xff;
1046 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1047 uinfo->count = 1;
1057 guard(spinlock_irq)(&chip->reg_lock);
1059 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1071 guard(spinlock_irq)(&chip->reg_lock);
1073 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1074 change = val != chip->dig_pcm_status;
1075 chip->dig_pcm_status = val;
1095 if (! cm->mixer_insensitive) {
1101 return -ENOMEM;
1103 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1107 ctl->get(ctl, val);
1108 cm->mixer_res_status[i] = val->value.integer.value[0];
1109 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1111 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1112 ctl->put(ctl, val); /* toggle */
1115 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1116 snd_ctl_notify(cm->card, event, &ctl->id);
1120 cm->mixer_insensitive = 1;
1129 if (cm->mixer_insensitive) {
1136 cm->mixer_insensitive = 0; /* at first clear this;
1139 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1144 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1145 ctl->get(ctl, val);
1147 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1148 val->value.integer.value[0] = cm->mixer_res_status[i];
1149 ctl->put(ctl, val);
1152 snd_ctl_notify(cm->card, event, &ctl->id);
1168 if (cm->can_ac3_hw) {
1177 if (cm->chip_version == 33) {
1190 if (cm->can_ac3_hw) {
1192 if (snd_pcm_format_width(subs->runtime->format) > 16) {
1211 rate = subs->runtime->rate;
1219 guard(spinlock_irq)(&cm->reg_lock);
1220 cm->spdif_playback_avail = up;
1225 if (cm->spdif_playback_enabled)
1253 /* playback - enable spdif only on the certain condition */
1257 int rate = substream->runtime->rate;
1261 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1262 substream->runtime->channels == 2);
1263 if (do_spdif && cm->can_ac3_hw)
1264 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1268 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1271 /* playback (via device #2) - enable spdif always */
1277 if (cm->can_ac3_hw)
1278 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1284 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1297 struct snd_pcm_runtime *runtime = rec->substream->runtime;
1300 if (rec->needs_silencing && runtime && runtime->dma_area) {
1302 memset(runtime->dma_area, 0, PAGE_SIZE);
1303 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1304 val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
1308 if (runtime->channels > 2)
1310 scoped_guard(spinlock_irq, &cm->reg_lock) {
1312 val &= ~(CM_ASFC_MASK << (rec->ch * 3));
1313 val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
1316 val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
1317 val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
1318 if (cm->can_96k)
1319 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
1323 cm->ctrl |= CM_CHEN0 << rec->ch;
1324 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
1330 scoped_guard(spinlock_irq, &cm->reg_lock) {
1331 cm->ctrl &= ~(CM_CHEN0 << rec->ch);
1332 val = CM_RST_CH0 << rec->ch;
1333 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
1334 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
1337 rec->needs_silencing = 0;
1346 snd_cmipci_silence_hack(cm, &cm->channel[0]);
1353 snd_cmipci_silence_hack(cm, &cm->channel[1]);
1361 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1369 scoped_guard(spinlock_irq, &cm->reg_lock) {
1371 if (cm->can_96k) {
1372 if (substream->runtime->rate > 48000)
1377 if (snd_pcm_format_width(substream->runtime->format) > 16)
1383 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1390 guard(spinlock_irq)(&cm->reg_lock);
1404 unsigned int status, mask = 0;
1412 scoped_guard(spinlock, &cm->reg_lock) {
1414 mask |= CM_CH0_INT_EN;
1416 mask |= CM_CH1_INT_EN;
1417 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
1418 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
1421 if (cm->rmidi && (status & CM_UARTINT))
1422 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
1424 if (cm->pcm) {
1425 if ((status & CM_CHINT0) && cm->channel[0].running)
1426 snd_pcm_period_elapsed(cm->channel[0].substream);
1427 if ((status & CM_CHINT1) && cm->channel[1].running)
1428 snd_pcm_period_elapsed(cm->channel[1].substream);
1477 /* playback on channel B - stereo 16bit only? */
1561 static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
1563 int ch = mode & CM_OPEN_CH_MASK;
1566 * when it's opened on blocking mode. however, since the current
1568 * we can't know whether blocking mode or not in open callback..
1570 guard(mutex)(&cm->open_mutex);
1571 if (cm->opened[ch])
1572 return -EBUSY;
1573 cm->opened[ch] = mode;
1574 cm->channel[ch].substream = subs;
1575 if (! (mode & CM_OPEN_DAC)) {
1576 /* disable dual DAC mode */
1577 cm->channel[ch].is_dac = 0;
1578 guard(spinlock_irq)(&cm->reg_lock);
1584 static void close_device_check(struct cmipci *cm, int mode)
1586 int ch = mode & CM_OPEN_CH_MASK;
1588 guard(mutex)(&cm->open_mutex);
1589 if (cm->opened[ch] == mode) {
1590 if (cm->channel[ch].substream) {
1592 cm->channel[ch].running = 0;
1593 cm->channel[ch].substream = NULL;
1595 cm->opened[ch] = 0;
1596 if (! cm->channel[ch].is_dac) {
1597 /* enable dual DAC mode again */
1598 cm->channel[ch].is_dac = 1;
1599 guard(spinlock_irq)(&cm->reg_lock);
1611 struct snd_pcm_runtime *runtime = substream->runtime;
1617 runtime->hw = snd_cmipci_playback;
1618 if (cm->chip_version == 68) {
1619 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1621 runtime->hw.rate_max = 96000;
1622 } else if (cm->chip_version == 55) {
1623 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1626 runtime->hw.rate_max = 128000;
1629 cm->dig_pcm_status = cm->dig_status;
1636 struct snd_pcm_runtime *runtime = substream->runtime;
1642 runtime->hw = snd_cmipci_capture;
1643 if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
1644 runtime->hw.rate_min = 41000;
1645 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1646 } else if (cm->chip_version == 55) {
1647 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1650 runtime->hw.rate_max = 128000;
1659 struct snd_pcm_runtime *runtime = substream->runtime;
1666 runtime->hw = snd_cmipci_playback2;
1667 guard(mutex)(&cm->open_mutex);
1668 if (! cm->opened[CM_CH_PLAY]) {
1669 if (cm->can_multi_ch) {
1670 runtime->hw.channels_max = cm->max_channels;
1671 if (cm->max_channels == 4)
1673 else if (cm->max_channels == 6)
1675 else if (cm->max_channels == 8)
1679 if (cm->chip_version == 68) {
1680 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1682 runtime->hw.rate_max = 96000;
1683 } else if (cm->chip_version == 55) {
1684 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1687 runtime->hw.rate_max = 128000;
1696 struct snd_pcm_runtime *runtime = substream->runtime;
1703 if (cm->can_ac3_hw) {
1704 runtime->hw = snd_cmipci_playback_spdif;
1705 if (cm->chip_version >= 37) {
1706 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1709 if (cm->can_96k) {
1710 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1712 runtime->hw.rate_max = 96000;
1715 runtime->hw = snd_cmipci_playback_iec958_subframe;
1718 cm->dig_pcm_status = cm->dig_status;
1725 struct snd_pcm_runtime *runtime = substream->runtime;
1732 runtime->hw = snd_cmipci_capture_spdif;
1733 if (cm->can_96k && !(cm->chip_version == 68)) {
1734 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1736 runtime->hw.rate_max = 96000;
1840 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1847 pcm->private_data = cm;
1848 pcm->info_flags = 0;
1849 strscpy(pcm->name, "C-Media PCI DAC/ADC");
1850 cm->pcm = pcm;
1853 &cm->pci->dev, 64*1024, 128*1024);
1863 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1869 pcm->private_data = cm;
1870 pcm->info_flags = 0;
1871 strscpy(pcm->name, "C-Media PCI 2nd DAC");
1872 cm->pcm2 = pcm;
1875 &cm->pci->dev, 64*1024, 128*1024);
1885 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1892 pcm->private_data = cm;
1893 pcm->info_flags = 0;
1894 strscpy(pcm->name, "C-Media PCI IEC958");
1895 cm->pcm_spdif = pcm;
1898 &cm->pci->dev, 64*1024, 128*1024);
1901 snd_pcm_alt_chmaps, cm->max_channels, 0,
1911 * - CM8338/8738 has a compatible mixer interface with SB16, but
1913 * - Access to native registers:
1914 * - A 3D switch
1915 * - Output mute switches
1920 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1921 outb(data, s->iobase + CM_REG_SB16_DATA);
1928 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1929 v = inb(s->iobase + CM_REG_SB16_DATA);
1939 unsigned int mask;
1944 #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1945 ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1947 #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1951 .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1954 #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
1955 #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
1961 r->left_reg = val & 0xff;
1962 r->right_reg = (val >> 8) & 0xff;
1963 r->left_shift = (val >> 16) & 0x07;
1964 r->right_shift = (val >> 19) & 0x07;
1965 r->invert = (val >> 22) & 1;
1966 r->stereo = (val >> 23) & 1;
1967 r->mask = (val >> 24) & 0xff;
1975 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1976 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1977 uinfo->count = reg.stereo + 1;
1978 uinfo->value.integer.min = 0;
1979 uinfo->value.integer.max = reg.mask;
1990 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1991 guard(spinlock_irq)(&cm->reg_lock);
1992 val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
1994 val = reg.mask - val;
1995 ucontrol->value.integer.value[0] = val;
1997 val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
1999 val = reg.mask - val;
2000 ucontrol->value.integer.value[1] = val;
2013 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2014 left = ucontrol->value.integer.value[0] & reg.mask;
2016 left = reg.mask - left;
2019 right = ucontrol->value.integer.value[1] & reg.mask;
2021 right = reg.mask - right;
2025 guard(spinlock_irq)(&cm->reg_lock);
2027 left |= oleft & ~(reg.mask << reg.left_shift);
2035 right |= oright & ~(reg.mask << reg.right_shift);
2044 * input route (left,right) -> (left,right)
2056 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2057 uinfo->count = 4;
2058 uinfo->value.integer.min = 0;
2059 uinfo->value.integer.max = 1;
2070 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2071 guard(spinlock_irq)(&cm->reg_lock);
2074 ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2075 ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2076 ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2077 ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2089 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2090 guard(spinlock_irq)(&cm->reg_lock);
2095 val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2096 val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2097 val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2098 val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2123 #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
2127 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
2130 #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
2134 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
2142 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2143 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2144 uinfo->count = reg.stereo + 1;
2145 uinfo->value.integer.min = 0;
2146 uinfo->value.integer.max = reg.mask;
2158 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2159 guard(spinlock_irq)(&cm->reg_lock);
2160 oreg = inb(cm->iobase + reg.left_reg);
2161 val = (oreg >> reg.left_shift) & reg.mask;
2163 val = reg.mask - val;
2164 ucontrol->value.integer.value[0] = val;
2166 val = (oreg >> reg.right_shift) & reg.mask;
2168 val = reg.mask - val;
2169 ucontrol->value.integer.value[1] = val;
2181 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2182 guard(spinlock_irq)(&cm->reg_lock);
2183 oreg = inb(cm->iobase + reg.left_reg);
2184 val = ucontrol->value.integer.value[0] & reg.mask;
2186 val = reg.mask - val;
2187 nreg = oreg & ~(reg.mask << reg.left_shift);
2190 val = ucontrol->value.integer.value[1] & reg.mask;
2192 val = reg.mask - val;
2193 nreg &= ~(reg.mask << reg.right_shift);
2196 outb(nreg, cm->iobase + reg.left_reg);
2201 * special case - check mixer sensitivity
2214 if (cm->mixer_insensitive) {
2224 CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2266 unsigned int mask; /* mask bits */
2267 unsigned int mask_on; /* mask bits to turn on */
2270 * non-audio operation?
2283 guard(spinlock_irq)(&cm->reg_lock);
2284 if (args->ac3_sensitive && cm->mixer_insensitive) {
2285 ucontrol->value.integer.value[0] = 0;
2288 if (args->is_byte)
2289 val = inb(cm->iobase + args->reg);
2291 val = snd_cmipci_read(cm, args->reg);
2292 ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2300 args = (struct cmipci_switch_args *)kcontrol->private_value;
2302 return -EINVAL;
2314 guard(spinlock_irq)(&cm->reg_lock);
2315 if (args->ac3_sensitive && cm->mixer_insensitive) {
2319 if (args->is_byte)
2320 val = inb(cm->iobase + args->reg);
2322 val = snd_cmipci_read(cm, args->reg);
2323 change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
2324 args->mask_on : (args->mask & ~args->mask_on));
2326 val &= ~args->mask;
2327 if (ucontrol->value.integer.value[0])
2328 val |= args->mask_on;
2330 val |= (args->mask & ~args->mask_on);
2331 if (args->is_byte)
2332 outb((unsigned char)val, cm->iobase + args->reg);
2334 snd_cmipci_write(cm, args->reg, val);
2343 args = (struct cmipci_switch_args *)kcontrol->private_value;
2345 return -EINVAL;
2352 .mask = xmask, \
2424 if (ucontrol->value.integer.value[0]) {
2425 if (chip->spdif_playback_avail)
2428 if (chip->spdif_playback_avail)
2432 chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2442 "Line-In", "Rear Output", "Bass Output"
2446 cm->chip_version >= 39 ? 3 : 2, texts);
2452 if (cm->chip_version >= 39) {
2468 guard(spinlock_irq)(&cm->reg_lock);
2469 ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2479 guard(spinlock_irq)(&cm->reg_lock);
2480 if (ucontrol->value.enumerated.item[0] == 2)
2484 if (ucontrol->value.enumerated.item[0] == 1)
2494 static const char *const texts[2] = { "Mic-In", "Center/LFE Output" };
2505 guard(spinlock_irq)(&cm->reg_lock);
2506 ucontrol->value.enumerated.item[0] =
2517 guard(spinlock_irq)(&cm->reg_lock);
2518 if (ucontrol->value.enumerated.item[0])
2527 DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
2529 .name = "Line-In Mode",
2537 /* for non-multichannel chips */
2539 DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
2546 DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
2575 .name = "Mic-In Mode",
2596 if (snd_BUG_ON(!cm || !cm->card))
2597 return -EINVAL;
2599 card = cm->card;
2601 strscpy(card->mixername, "CMedia PCI");
2603 scoped_guard(spinlock_irq, &cm->reg_lock) {
2608 if (cm->chip_version == 68) { // 8768 has no PCM volume
2621 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2625 if (! cm->can_multi_ch) {
2626 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2630 if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2631 cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2634 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2638 if (cm->can_ac3_hw) {
2640 kctl->id.device = pcm_spdif_device;
2645 kctl->id.device = pcm_spdif_device;
2650 kctl->id.device = pcm_spdif_device;
2655 if (cm->chip_version <= 37) {
2658 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2664 if (cm->chip_version >= 39) {
2667 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2678 if (cm->chip_version < 39) {
2679 err = snd_ctl_add(cm->card,
2687 ctl = snd_ctl_find_id_mixer(cm->card, cm_saved_mixer[idx].name);
2689 cm->mixer_res_ctl[idx] = ctl;
2703 struct cmipci *cm = entry->private_data;
2706 snd_iprintf(buffer, "%s\n", cm->card->longname);
2710 v = inb(cm->iobase + i);
2720 snd_card_ro_proc_new(cm->card, "cmipci", cm, snd_cmipci_proc_read);
2741 /* check reg 0Ch, bit 24-31 */
2744 /* check reg 08h, bit 24-28 */
2748 cm->chip_version = 33;
2749 if (cm->do_soft_ac3)
2750 cm->can_ac3_sw = 1;
2752 cm->can_ac3_hw = 1;
2755 cm->chip_version = 37;
2756 cm->can_ac3_hw = 1;
2759 cm->chip_version = 39;
2760 cm->can_ac3_hw = 1;
2763 cm->max_channels = 2;
2766 cm->chip_version = 39;
2768 cm->max_channels = 6;
2770 cm->max_channels = 4;
2772 cm->chip_version = 68;
2773 cm->max_channels = 8;
2774 cm->can_96k = 1;
2776 cm->chip_version = 55;
2777 cm->max_channels = 6;
2778 cm->can_96k = 1;
2780 cm->can_ac3_hw = 1;
2781 cm->can_multi_ch = 1;
2794 return -ENODEV;
2796 if (joystick_port[dev] == 1) { /* auto-detect */
2799 r = devm_request_region(&cm->pci->dev, io_port, 1,
2806 r = devm_request_region(&cm->pci->dev, io_port, 1,
2811 dev_warn(cm->card->dev, "cannot reserve joystick ports\n");
2812 return -EBUSY;
2815 cm->gameport = gp = gameport_allocate_port();
2817 dev_err(cm->card->dev, "cannot allocate memory for gameport\n");
2818 return -ENOMEM;
2820 gameport_set_name(gp, "C-Media Gameport");
2821 gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2822 gameport_set_dev_parent(gp, &cm->pci->dev);
2823 gp->io = io_port;
2827 gameport_register_port(cm->gameport);
2834 if (cm->gameport) {
2835 gameport_unregister_port(cm->gameport);
2836 cm->gameport = NULL;
2842 static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2848 struct cmipci *cm = card->private_data;
2874 if (cm->chip_version >= 39) {
2876 iosynth = cm->iobase + CM_REG_FM_PCI;
2877 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2880 err = -EIO;
2898 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2900 dev_err(cm->card->dev,
2908 dev_err(cm->card->dev, "cannot create OPL3 hwdep\n");
2922 struct cmipci *cm = card->private_data;
2938 spin_lock_init(&cm->reg_lock);
2939 mutex_init(&cm->open_mutex);
2940 cm->device = pci->device;
2941 cm->card = card;
2942 cm->pci = pci;
2943 cm->irq = -1;
2944 cm->channel[0].ch = 0;
2945 cm->channel[1].ch = 1;
2946 cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
2948 err = pcim_request_all_regions(pci, card->driver);
2951 cm->iobase = pci_resource_start(pci, 0);
2953 if (devm_request_irq(&pci->dev, pci->irq, snd_cmipci_interrupt,
2955 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2956 return -EBUSY;
2958 cm->irq = pci->irq;
2959 card->sync_irq = cm->irq;
2960 card->private_free = snd_cmipci_free;
2962 pci_set_master(cm->pci);
2968 cm->chip_version = 0;
2969 cm->max_channels = 2;
2970 cm->do_soft_ac3 = soft_ac3[dev];
2972 if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
2973 pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
2975 /* added -MCx suffix for chip supporting multi-channels */
2976 if (cm->can_multi_ch) {
2977 int l = strlen(cm->card->driver);
2978 scnprintf(cm->card->driver + l, sizeof(cm->card->driver) - l,
2979 "-MC%d", cm->max_channels);
2980 } else if (cm->can_ac3_sw)
2981 strlcat(cm->card->driver, "-SWIEC", sizeof(cm->card->driver));
2983 cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2984 cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2987 cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
2989 cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
3008 if (cm->chip_version) {
3016 switch (pci->device) {
3026 if (cm->chip_version < 68) {
3027 val = pci->device < 0x110 ? 8338 : 8738;
3037 switch ((pci->subsystem_vendor << 16) |
3038 pci->subsystem_device) {
3053 sprintf(card->shortname, "C-Media CMI%u", val);
3054 if (cm->chip_version < 68)
3056 " (model %d)", cm->chip_version);
3059 scnprintf(card->longname, sizeof(card->longname),
3061 card->shortname, modelstr, cm->iobase, cm->irq);
3063 if (cm->chip_version >= 39) {
3067 iomidi = cm->iobase + CM_REG_MPU_PCI;
3087 dev_err(cm->card->dev,
3088 "cannot enable MPU-401 port at %#lx\n",
3097 if (cm->chip_version < 68) {
3118 if (cm->can_ac3_hw || cm->can_ac3_sw) {
3136 -1, &cm->rmidi);
3138 dev_err(cm->card->dev,
3147 * (Re-)Enable external switch spdo_48k
3171 return -ENODEV;
3174 return -ENOENT;
3177 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3182 switch (pci->device) {
3185 strscpy(card->driver, "CMI8738");
3189 strscpy(card->driver, "CMI8338");
3192 strscpy(card->driver, "CMIPCI");
3238 struct cmipci *cm = card->private_data;
3245 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3247 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3257 struct cmipci *cm = card->private_data;
3268 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3270 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);