Lines Matching +full:invert +full:- +full:ext

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for C-Media CMI8338 and 8738 PCI soundcards.
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).");
86 #define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/IN -> OUT loopback */
105 #define CM_SPD24SEL39 0x00200000 /* 24-bit spdif: model 039 */
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 */
220 #define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
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 */
365 #define CM_INVLRCK 0x00010000 /* invert ZVPORT's LRCK */
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);
667 mutex_lock(&cm->open_mutex);
668 if (cm->opened[CM_CH_PLAY]) {
669 mutex_unlock(&cm->open_mutex);
670 return -EBUSY;
673 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
674 mutex_unlock(&cm->open_mutex);
681 int reset = CM_RST_CH0 << (cm->channel[ch].ch);
682 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
683 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
711 if (!cm->can_multi_ch || !rec->ch)
712 return -EINVAL;
713 if (rec->fmt != 0x03) /* stereo 16bit only */
714 return -EINVAL;
717 if (cm->can_multi_ch) {
718 spin_lock_irq(&cm->reg_lock);
741 spin_unlock_irq(&cm->reg_lock);
749 * channel to be used must have been set in rec->ch.
756 struct snd_pcm_runtime *runtime = substream->runtime;
758 rec->fmt = 0;
759 rec->shift = 0;
760 if (snd_pcm_format_width(runtime->format) >= 16) {
761 rec->fmt |= 0x02;
762 if (snd_pcm_format_width(runtime->format) > 16)
763 rec->shift++; /* 24/32bit */
765 if (runtime->channels > 1)
766 rec->fmt |= 0x01;
767 if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
768 dev_dbg(cm->card->dev, "cannot set dac channels\n");
769 return -EINVAL;
772 rec->offset = runtime->dma_addr;
774 rec->dma_size = runtime->buffer_size << rec->shift;
775 period_size = runtime->period_size << rec->shift;
776 if (runtime->channels > 2) {
777 /* multi-channels */
778 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
779 period_size = (period_size * runtime->channels) / 2;
782 spin_lock_irq(&cm->reg_lock);
785 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
786 snd_cmipci_write(cm, reg, rec->offset);
788 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
789 snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
790 snd_cmipci_write_w(cm, reg + 2, period_size - 1);
793 val = rec->ch ? CM_CHADC1 : CM_CHADC0;
794 if (rec->is_dac)
795 cm->ctrl &= ~val;
797 cm->ctrl |= val;
798 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
799 /* dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl); */
804 if (runtime->rate > 48000)
805 switch (runtime->rate) {
812 freq = snd_cmipci_rate_freq(runtime->rate);
814 if (rec->ch) {
822 dev_dbg(cm->card->dev, "functrl1 = %08x\n", val);
826 if (rec->ch) {
828 val |= rec->fmt << CM_CH1FMT_SHIFT;
831 val |= rec->fmt << CM_CH0FMT_SHIFT;
833 if (cm->can_96k) {
834 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
835 val |= freq_ext << (rec->ch * 2);
838 dev_dbg(cm->card->dev, "chformat = %08x\n", val);
840 if (!rec->is_dac && cm->chip_version) {
841 if (runtime->rate > 44100)
847 rec->running = 0;
848 spin_unlock_irq(&cm->reg_lock);
862 inthld = CM_CH0_INT_EN << rec->ch;
863 chen = CM_CHEN0 << rec->ch;
864 reset = CM_RST_CH0 << rec->ch;
865 pause = CM_PAUSE0 << rec->ch;
867 spin_lock(&cm->reg_lock);
870 rec->running = 1;
873 cm->ctrl |= chen;
875 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
876 dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl);
879 rec->running = 0;
883 cm->ctrl &= ~chen;
884 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
885 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
886 rec->needs_silencing = rec->is_dac;
890 cm->ctrl |= pause;
891 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
895 cm->ctrl &= ~pause;
896 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
899 result = -EINVAL;
902 spin_unlock(&cm->reg_lock);
915 if (!rec->running)
918 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
921 if (rem < rec->dma_size)
924 dev_err(cm->card->dev, "invalid PCM pointer: %#x\n", rem);
927 ptr = (rec->dma_size - (rem + 1)) >> rec->shift;
929 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
930 ptr = snd_cmipci_read(cm, reg) - rec->offset;
931 ptr = bytes_to_frames(substream->runtime, ptr);
933 if (substream->runtime->channels > 2)
934 ptr = (ptr * 2) / substream->runtime->channels;
946 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
952 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
965 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
971 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
982 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
983 uinfo->count = 1;
993 spin_lock_irq(&chip->reg_lock);
995 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
996 spin_unlock_irq(&chip->reg_lock);
1008 spin_lock_irq(&chip->reg_lock);
1010 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1011 change = val != chip->dig_status;
1012 chip->dig_status = val;
1013 spin_unlock_irq(&chip->reg_lock);
1029 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1030 uinfo->count = 1;
1037 ucontrol->value.iec958.status[0] = 0xff;
1038 ucontrol->value.iec958.status[1] = 0xff;
1039 ucontrol->value.iec958.status[2] = 0xff;
1040 ucontrol->value.iec958.status[3] = 0xff;
1056 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1057 uinfo->count = 1;
1067 spin_lock_irq(&chip->reg_lock);
1069 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1070 spin_unlock_irq(&chip->reg_lock);
1082 spin_lock_irq(&chip->reg_lock);
1084 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1085 change = val != chip->dig_pcm_status;
1086 chip->dig_pcm_status = val;
1087 spin_unlock_irq(&chip->reg_lock);
1107 if (! cm->mixer_insensitive) {
1113 return -ENOMEM;
1115 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1119 ctl->get(ctl, val);
1120 cm->mixer_res_status[i] = val->value.integer.value[0];
1121 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1123 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1124 ctl->put(ctl, val); /* toggle */
1127 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1128 snd_ctl_notify(cm->card, event, &ctl->id);
1132 cm->mixer_insensitive = 1;
1141 if (cm->mixer_insensitive) {
1148 cm->mixer_insensitive = 0; /* at first clear this;
1151 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1156 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1157 ctl->get(ctl, val);
1159 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1160 val->value.integer.value[0] = cm->mixer_res_status[i];
1161 ctl->put(ctl, val);
1164 snd_ctl_notify(cm->card, event, &ctl->id);
1180 if (cm->can_ac3_hw) {
1189 if (cm->chip_version == 33) {
1202 if (cm->can_ac3_hw) {
1204 if (snd_pcm_format_width(subs->runtime->format) > 16) {
1223 rate = subs->runtime->rate;
1231 spin_lock_irq(&cm->reg_lock);
1232 cm->spdif_playback_avail = up;
1237 if (cm->spdif_playback_enabled)
1257 spin_unlock_irq(&cm->reg_lock);
1266 /* playback - enable spdif only on the certain condition */
1270 int rate = substream->runtime->rate;
1274 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1275 substream->runtime->channels == 2);
1276 if (do_spdif && cm->can_ac3_hw)
1277 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1281 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1284 /* playback (via device #2) - enable spdif always */
1290 if (cm->can_ac3_hw)
1291 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1297 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1310 struct snd_pcm_runtime *runtime = rec->substream->runtime;
1313 if (rec->needs_silencing && runtime && runtime->dma_area) {
1315 memset(runtime->dma_area, 0, PAGE_SIZE);
1316 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1317 val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
1321 if (runtime->channels > 2)
1323 spin_lock_irq(&cm->reg_lock);
1325 val &= ~(CM_ASFC_MASK << (rec->ch * 3));
1326 val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
1329 val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
1330 val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
1331 if (cm->can_96k)
1332 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
1336 cm->ctrl |= CM_CHEN0 << rec->ch;
1337 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
1338 spin_unlock_irq(&cm->reg_lock);
1343 spin_lock_irq(&cm->reg_lock);
1344 cm->ctrl &= ~(CM_CHEN0 << rec->ch);
1345 val = CM_RST_CH0 << rec->ch;
1346 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
1347 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
1348 spin_unlock_irq(&cm->reg_lock);
1350 rec->needs_silencing = 0;
1359 snd_cmipci_silence_hack(cm, &cm->channel[0]);
1366 snd_cmipci_silence_hack(cm, &cm->channel[1]);
1374 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1382 spin_lock_irq(&cm->reg_lock);
1384 if (cm->can_96k) {
1385 if (substream->runtime->rate > 48000)
1390 if (snd_pcm_format_width(substream->runtime->format) > 16)
1395 spin_unlock_irq(&cm->reg_lock);
1397 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1404 spin_lock_irq(&cm->reg_lock);
1407 spin_unlock_irq(&cm->reg_lock);
1427 spin_lock(&cm->reg_lock);
1434 spin_unlock(&cm->reg_lock);
1436 if (cm->rmidi && (status & CM_UARTINT))
1437 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
1439 if (cm->pcm) {
1440 if ((status & CM_CHINT0) && cm->channel[0].running)
1441 snd_pcm_period_elapsed(cm->channel[0].substream);
1442 if ((status & CM_CHINT1) && cm->channel[1].running)
1443 snd_pcm_period_elapsed(cm->channel[1].substream);
1492 /* playback on channel B - stereo 16bit only? */
1585 mutex_lock(&cm->open_mutex);
1586 if (cm->opened[ch]) {
1587 mutex_unlock(&cm->open_mutex);
1588 return -EBUSY;
1590 cm->opened[ch] = mode;
1591 cm->channel[ch].substream = subs;
1594 cm->channel[ch].is_dac = 0;
1595 spin_lock_irq(&cm->reg_lock);
1597 spin_unlock_irq(&cm->reg_lock);
1599 mutex_unlock(&cm->open_mutex);
1607 mutex_lock(&cm->open_mutex);
1608 if (cm->opened[ch] == mode) {
1609 if (cm->channel[ch].substream) {
1611 cm->channel[ch].running = 0;
1612 cm->channel[ch].substream = NULL;
1614 cm->opened[ch] = 0;
1615 if (! cm->channel[ch].is_dac) {
1617 cm->channel[ch].is_dac = 1;
1618 spin_lock_irq(&cm->reg_lock);
1620 spin_unlock_irq(&cm->reg_lock);
1623 mutex_unlock(&cm->open_mutex);
1632 struct snd_pcm_runtime *runtime = substream->runtime;
1638 runtime->hw = snd_cmipci_playback;
1639 if (cm->chip_version == 68) {
1640 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1642 runtime->hw.rate_max = 96000;
1643 } else if (cm->chip_version == 55) {
1644 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1647 runtime->hw.rate_max = 128000;
1650 cm->dig_pcm_status = cm->dig_status;
1657 struct snd_pcm_runtime *runtime = substream->runtime;
1663 runtime->hw = snd_cmipci_capture;
1664 if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
1665 runtime->hw.rate_min = 41000;
1666 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1667 } else if (cm->chip_version == 55) {
1668 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1671 runtime->hw.rate_max = 128000;
1680 struct snd_pcm_runtime *runtime = substream->runtime;
1687 runtime->hw = snd_cmipci_playback2;
1688 mutex_lock(&cm->open_mutex);
1689 if (! cm->opened[CM_CH_PLAY]) {
1690 if (cm->can_multi_ch) {
1691 runtime->hw.channels_max = cm->max_channels;
1692 if (cm->max_channels == 4)
1694 else if (cm->max_channels == 6)
1696 else if (cm->max_channels == 8)
1700 mutex_unlock(&cm->open_mutex);
1701 if (cm->chip_version == 68) {
1702 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1704 runtime->hw.rate_max = 96000;
1705 } else if (cm->chip_version == 55) {
1706 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1709 runtime->hw.rate_max = 128000;
1718 struct snd_pcm_runtime *runtime = substream->runtime;
1725 if (cm->can_ac3_hw) {
1726 runtime->hw = snd_cmipci_playback_spdif;
1727 if (cm->chip_version >= 37) {
1728 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1731 if (cm->can_96k) {
1732 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1734 runtime->hw.rate_max = 96000;
1737 runtime->hw = snd_cmipci_playback_iec958_subframe;
1740 cm->dig_pcm_status = cm->dig_status;
1747 struct snd_pcm_runtime *runtime = substream->runtime;
1754 runtime->hw = snd_cmipci_capture_spdif;
1755 if (cm->can_96k && !(cm->chip_version == 68)) {
1756 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1758 runtime->hw.rate_max = 96000;
1862 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1869 pcm->private_data = cm;
1870 pcm->info_flags = 0;
1871 strcpy(pcm->name, "C-Media PCI DAC/ADC");
1872 cm->pcm = pcm;
1875 &cm->pci->dev, 64*1024, 128*1024);
1885 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1891 pcm->private_data = cm;
1892 pcm->info_flags = 0;
1893 strcpy(pcm->name, "C-Media PCI 2nd DAC");
1894 cm->pcm2 = pcm;
1897 &cm->pci->dev, 64*1024, 128*1024);
1907 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1914 pcm->private_data = cm;
1915 pcm->info_flags = 0;
1916 strcpy(pcm->name, "C-Media PCI IEC958");
1917 cm->pcm_spdif = pcm;
1920 &cm->pci->dev, 64*1024, 128*1024);
1923 snd_pcm_alt_chmaps, cm->max_channels, 0,
1933 * - CM8338/8738 has a compatible mixer interface with SB16, but
1935 * - Access to native registers:
1936 * - A 3D switch
1937 * - Output mute switches
1942 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1943 outb(data, s->iobase + CM_REG_SB16_DATA);
1950 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1951 v = inb(s->iobase + CM_REG_SB16_DATA);
1962 unsigned int invert: 1;
1966 #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1967 ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1969 #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1973 .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1983 r->left_reg = val & 0xff;
1984 r->right_reg = (val >> 8) & 0xff;
1985 r->left_shift = (val >> 16) & 0x07;
1986 r->right_shift = (val >> 19) & 0x07;
1987 r->invert = (val >> 22) & 1;
1988 r->stereo = (val >> 23) & 1;
1989 r->mask = (val >> 24) & 0xff;
1997 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1998 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1999 uinfo->count = reg.stereo + 1;
2000 uinfo->value.integer.min = 0;
2001 uinfo->value.integer.max = reg.mask;
2012 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2013 spin_lock_irq(&cm->reg_lock);
2015 if (reg.invert)
2016 val = reg.mask - val;
2017 ucontrol->value.integer.value[0] = val;
2020 if (reg.invert)
2021 val = reg.mask - val;
2022 ucontrol->value.integer.value[1] = val;
2024 spin_unlock_irq(&cm->reg_lock);
2036 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2037 left = ucontrol->value.integer.value[0] & reg.mask;
2038 if (reg.invert)
2039 left = reg.mask - left;
2042 right = ucontrol->value.integer.value[1] & reg.mask;
2043 if (reg.invert)
2044 right = reg.mask - right;
2048 spin_lock_irq(&cm->reg_lock);
2063 spin_unlock_irq(&cm->reg_lock);
2068 * input route (left,right) -> (left,right)
2080 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2081 uinfo->count = 4;
2082 uinfo->value.integer.min = 0;
2083 uinfo->value.integer.max = 1;
2094 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2095 spin_lock_irq(&cm->reg_lock);
2098 spin_unlock_irq(&cm->reg_lock);
2099 ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2100 ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2101 ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2102 ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2114 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2115 spin_lock_irq(&cm->reg_lock);
2120 val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2121 val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2122 val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2123 val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2127 spin_unlock_irq(&cm->reg_lock);
2135 #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
2139 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
2142 #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
2146 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
2168 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2169 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2170 uinfo->count = reg.stereo + 1;
2171 uinfo->value.integer.min = 0;
2172 uinfo->value.integer.max = reg.mask;
2184 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2185 spin_lock_irq(&cm->reg_lock);
2186 oreg = inb(cm->iobase + reg.left_reg);
2188 if (reg.invert)
2189 val = reg.mask - val;
2190 ucontrol->value.integer.value[0] = val;
2193 if (reg.invert)
2194 val = reg.mask - val;
2195 ucontrol->value.integer.value[1] = val;
2197 spin_unlock_irq(&cm->reg_lock);
2208 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2209 spin_lock_irq(&cm->reg_lock);
2210 oreg = inb(cm->iobase + reg.left_reg);
2211 val = ucontrol->value.integer.value[0] & reg.mask;
2212 if (reg.invert)
2213 val = reg.mask - val;
2217 val = ucontrol->value.integer.value[1] & reg.mask;
2218 if (reg.invert)
2219 val = reg.mask - val;
2223 outb(nreg, cm->iobase + reg.left_reg);
2224 spin_unlock_irq(&cm->reg_lock);
2229 * special case - check mixer sensitivity
2242 if (cm->mixer_insensitive) {
2252 CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2298 * non-audio operation?
2311 spin_lock_irq(&cm->reg_lock);
2312 if (args->ac3_sensitive && cm->mixer_insensitive) {
2313 ucontrol->value.integer.value[0] = 0;
2314 spin_unlock_irq(&cm->reg_lock);
2317 if (args->is_byte)
2318 val = inb(cm->iobase + args->reg);
2320 val = snd_cmipci_read(cm, args->reg);
2321 ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2322 spin_unlock_irq(&cm->reg_lock);
2330 args = (struct cmipci_switch_args *)kcontrol->private_value;
2332 return -EINVAL;
2344 spin_lock_irq(&cm->reg_lock);
2345 if (args->ac3_sensitive && cm->mixer_insensitive) {
2347 spin_unlock_irq(&cm->reg_lock);
2350 if (args->is_byte)
2351 val = inb(cm->iobase + args->reg);
2353 val = snd_cmipci_read(cm, args->reg);
2354 change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
2355 args->mask_on : (args->mask & ~args->mask_on));
2357 val &= ~args->mask;
2358 if (ucontrol->value.integer.value[0])
2359 val |= args->mask_on;
2361 val |= (args->mask & ~args->mask_on);
2362 if (args->is_byte)
2363 outb((unsigned char)val, cm->iobase + args->reg);
2365 snd_cmipci_write(cm, args->reg, val);
2367 spin_unlock_irq(&cm->reg_lock);
2375 args = (struct cmipci_switch_args *)kcontrol->private_value;
2377 return -EINVAL;
2456 if (ucontrol->value.integer.value[0]) {
2457 if (chip->spdif_playback_avail)
2460 if (chip->spdif_playback_avail)
2464 chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2474 "Line-In", "Rear Output", "Bass Output"
2478 cm->chip_version >= 39 ? 3 : 2, texts);
2484 if (cm->chip_version >= 39) {
2500 spin_lock_irq(&cm->reg_lock);
2501 ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2502 spin_unlock_irq(&cm->reg_lock);
2512 spin_lock_irq(&cm->reg_lock);
2513 if (ucontrol->value.enumerated.item[0] == 2)
2517 if (ucontrol->value.enumerated.item[0] == 1)
2521 spin_unlock_irq(&cm->reg_lock);
2528 static const char *const texts[2] = { "Mic-In", "Center/LFE Output" };
2538 spin_lock_irq(&cm->reg_lock);
2539 ucontrol->value.enumerated.item[0] =
2541 spin_unlock_irq(&cm->reg_lock);
2551 spin_lock_irq(&cm->reg_lock);
2552 if (ucontrol->value.enumerated.item[0])
2556 spin_unlock_irq(&cm->reg_lock);
2564 .name = "Line-In Mode",
2572 /* for non-multichannel chips */
2610 .name = "Mic-In Mode",
2631 if (snd_BUG_ON(!cm || !cm->card))
2632 return -EINVAL;
2634 card = cm->card;
2636 strcpy(card->mixername, "CMedia PCI");
2638 spin_lock_irq(&cm->reg_lock);
2640 spin_unlock_irq(&cm->reg_lock);
2643 if (cm->chip_version == 68) { // 8768 has no PCM volume
2656 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2660 if (! cm->can_multi_ch) {
2661 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2665 if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2666 cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2669 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2673 if (cm->can_ac3_hw) {
2675 kctl->id.device = pcm_spdif_device;
2680 kctl->id.device = pcm_spdif_device;
2685 kctl->id.device = pcm_spdif_device;
2690 if (cm->chip_version <= 37) {
2693 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2699 if (cm->chip_version >= 39) {
2702 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2713 if (cm->chip_version < 39) {
2714 err = snd_ctl_add(cm->card,
2722 ctl = snd_ctl_find_id_mixer(cm->card, cm_saved_mixer[idx].name);
2724 cm->mixer_res_ctl[idx] = ctl;
2738 struct cmipci *cm = entry->private_data;
2741 snd_iprintf(buffer, "%s\n", cm->card->longname);
2745 v = inb(cm->iobase + i);
2755 snd_card_ro_proc_new(cm->card, "cmipci", cm, snd_cmipci_proc_read);
2776 /* check reg 0Ch, bit 24-31 */
2779 /* check reg 08h, bit 24-28 */
2783 cm->chip_version = 33;
2784 if (cm->do_soft_ac3)
2785 cm->can_ac3_sw = 1;
2787 cm->can_ac3_hw = 1;
2790 cm->chip_version = 37;
2791 cm->can_ac3_hw = 1;
2794 cm->chip_version = 39;
2795 cm->can_ac3_hw = 1;
2798 cm->max_channels = 2;
2801 cm->chip_version = 39;
2803 cm->max_channels = 6;
2805 cm->max_channels = 4;
2807 cm->chip_version = 68;
2808 cm->max_channels = 8;
2809 cm->can_96k = 1;
2811 cm->chip_version = 55;
2812 cm->max_channels = 6;
2813 cm->can_96k = 1;
2815 cm->can_ac3_hw = 1;
2816 cm->can_multi_ch = 1;
2829 return -ENODEV;
2831 if (joystick_port[dev] == 1) { /* auto-detect */
2834 r = devm_request_region(&cm->pci->dev, io_port, 1,
2841 r = devm_request_region(&cm->pci->dev, io_port, 1,
2846 dev_warn(cm->card->dev, "cannot reserve joystick ports\n");
2847 return -EBUSY;
2850 cm->gameport = gp = gameport_allocate_port();
2852 dev_err(cm->card->dev, "cannot allocate memory for gameport\n");
2853 return -ENOMEM;
2855 gameport_set_name(gp, "C-Media Gameport");
2856 gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2857 gameport_set_dev_parent(gp, &cm->pci->dev);
2858 gp->io = io_port;
2862 gameport_register_port(cm->gameport);
2869 if (cm->gameport) {
2870 gameport_unregister_port(cm->gameport);
2871 cm->gameport = NULL;
2877 static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2883 struct cmipci *cm = card->private_data;
2909 if (cm->chip_version >= 39) {
2911 iosynth = cm->iobase + CM_REG_FM_PCI;
2912 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2915 err = -EIO;
2933 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2935 dev_err(cm->card->dev,
2943 dev_err(cm->card->dev, "cannot create OPL3 hwdep\n");
2957 struct cmipci *cm = card->private_data;
2973 spin_lock_init(&cm->reg_lock);
2974 mutex_init(&cm->open_mutex);
2975 cm->device = pci->device;
2976 cm->card = card;
2977 cm->pci = pci;
2978 cm->irq = -1;
2979 cm->channel[0].ch = 0;
2980 cm->channel[1].ch = 1;
2981 cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
2983 err = pci_request_regions(pci, card->driver);
2986 cm->iobase = pci_resource_start(pci, 0);
2988 if (devm_request_irq(&pci->dev, pci->irq, snd_cmipci_interrupt,
2990 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2991 return -EBUSY;
2993 cm->irq = pci->irq;
2994 card->sync_irq = cm->irq;
2995 card->private_free = snd_cmipci_free;
2997 pci_set_master(cm->pci);
3003 cm->chip_version = 0;
3004 cm->max_channels = 2;
3005 cm->do_soft_ac3 = soft_ac3[dev];
3007 if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
3008 pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
3010 /* added -MCx suffix for chip supporting multi-channels */
3011 if (cm->can_multi_ch)
3012 sprintf(cm->card->driver + strlen(cm->card->driver),
3013 "-MC%d", cm->max_channels);
3014 else if (cm->can_ac3_sw)
3015 strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
3017 cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3018 cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3021 cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
3023 cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
3042 if (cm->chip_version) {
3050 switch (pci->device) {
3060 if (cm->chip_version < 68) {
3061 val = pci->device < 0x110 ? 8338 : 8738;
3071 switch ((pci->subsystem_vendor << 16) |
3072 pci->subsystem_device) {
3087 sprintf(card->shortname, "C-Media CMI%u", val);
3088 if (cm->chip_version < 68)
3090 " (model %d)", cm->chip_version);
3093 scnprintf(card->longname, sizeof(card->longname),
3095 card->shortname, modelstr, cm->iobase, cm->irq);
3097 if (cm->chip_version >= 39) {
3101 iomidi = cm->iobase + CM_REG_MPU_PCI;
3121 dev_err(cm->card->dev,
3122 "cannot enable MPU-401 port at %#lx\n",
3131 if (cm->chip_version < 68) {
3152 if (cm->can_ac3_hw || cm->can_ac3_sw) {
3170 -1, &cm->rmidi);
3172 dev_err(cm->card->dev,
3181 * (Re-)Enable external switch spdo_48k
3205 return -ENODEV;
3208 return -ENOENT;
3211 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3216 switch (pci->device) {
3219 strcpy(card->driver, "CMI8738");
3223 strcpy(card->driver, "CMI8338");
3226 strcpy(card->driver, "CMIPCI");
3272 struct cmipci *cm = card->private_data;
3279 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3281 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3291 struct cmipci *cm = card->private_data;
3302 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3304 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);