xref: /linux/sound/pci/ac97/ac97_pcm.c (revision ee42381e71c56328db9e9d64d19a4de7a2f09a93)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
31da177e4SLinus Torvalds  *  Universal interface for Audio Codec '97
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  For more details look to AC '97 component specification revision 2.2
61da177e4SLinus Torvalds  *  by Intel Corporation (http://developer.intel.com) and to datasheets
71da177e4SLinus Torvalds  *  for specific codecs.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  *   (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds  *   GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <sound/driver.h>
271da177e4SLinus Torvalds #include <linux/delay.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <sound/core.h>
311da177e4SLinus Torvalds #include <sound/pcm.h>
321da177e4SLinus Torvalds #include <sound/control.h>
331da177e4SLinus Torvalds #include <sound/ac97_codec.h>
341da177e4SLinus Torvalds #include <sound/asoundef.h>
351da177e4SLinus Torvalds #include "ac97_patch.h"
361da177e4SLinus Torvalds #include "ac97_id.h"
371da177e4SLinus Torvalds #include "ac97_local.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  *  PCM support
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static unsigned char rate_reg_tables[2][4][9] = {
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds   /* standard rates */
461da177e4SLinus Torvalds   {
471da177e4SLinus Torvalds   	/* 3&4 front, 7&8 rear, 6&9 center/lfe */
481da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 3 */
491da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 4 */
501da177e4SLinus Torvalds 	0xff,				/* slot 5 */
511da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 6 */
521da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 7 */
531da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 8 */
541da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 9 */
551da177e4SLinus Torvalds 	0xff,				/* slot 10 */
561da177e4SLinus Torvalds 	0xff,				/* slot 11 */
571da177e4SLinus Torvalds   },
581da177e4SLinus Torvalds   {
591da177e4SLinus Torvalds   	/* 7&8 front, 6&9 rear, 10&11 center/lfe */
601da177e4SLinus Torvalds 	0xff,				/* slot 3 */
611da177e4SLinus Torvalds 	0xff,				/* slot 4 */
621da177e4SLinus Torvalds 	0xff,				/* slot 5 */
631da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 6 */
641da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 7 */
651da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 8 */
661da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 9 */
671da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 10 */
681da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 11 */
691da177e4SLinus Torvalds   },
701da177e4SLinus Torvalds   {
711da177e4SLinus Torvalds   	/* 6&9 front, 10&11 rear, 3&4 center/lfe */
721da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 3 */
731da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 4 */
741da177e4SLinus Torvalds 	0xff,				/* slot 5 */
751da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 6 */
761da177e4SLinus Torvalds 	0xff,				/* slot 7 */
771da177e4SLinus Torvalds 	0xff,				/* slot 8 */
781da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 9 */
791da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 10 */
801da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 11 */
811da177e4SLinus Torvalds   },
821da177e4SLinus Torvalds   {
831da177e4SLinus Torvalds   	/* 10&11 front, 3&4 rear, 7&8 center/lfe */
841da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 3 */
851da177e4SLinus Torvalds 	AC97_PCM_SURR_DAC_RATE,		/* slot 4 */
861da177e4SLinus Torvalds 	0xff,				/* slot 5 */
871da177e4SLinus Torvalds 	0xff,				/* slot 6 */
881da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 7 */
891da177e4SLinus Torvalds 	AC97_PCM_LFE_DAC_RATE,		/* slot 8 */
901da177e4SLinus Torvalds 	0xff,				/* slot 9 */
911da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 10 */
921da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 11 */
931da177e4SLinus Torvalds   },
941da177e4SLinus Torvalds },
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds   /* double rates */
971da177e4SLinus Torvalds   {
981da177e4SLinus Torvalds   	/* 3&4 front, 7&8 front (t+1) */
991da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 3 */
1001da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 4 */
1011da177e4SLinus Torvalds 	0xff,				/* slot 5 */
1021da177e4SLinus Torvalds 	0xff,				/* slot 6 */
1031da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 7 */
1041da177e4SLinus Torvalds 	AC97_PCM_FRONT_DAC_RATE,	/* slot 8 */
1051da177e4SLinus Torvalds 	0xff,				/* slot 9 */
1061da177e4SLinus Torvalds 	0xff,				/* slot 10 */
1071da177e4SLinus Torvalds 	0xff,				/* slot 11 */
1081da177e4SLinus Torvalds   },
1091da177e4SLinus Torvalds   {
1101da177e4SLinus Torvalds 	/* not specified in the specification */
1111da177e4SLinus Torvalds 	0xff,				/* slot 3 */
1121da177e4SLinus Torvalds 	0xff,				/* slot 4 */
1131da177e4SLinus Torvalds 	0xff,				/* slot 5 */
1141da177e4SLinus Torvalds 	0xff,				/* slot 6 */
1151da177e4SLinus Torvalds 	0xff,				/* slot 7 */
1161da177e4SLinus Torvalds 	0xff,				/* slot 8 */
1171da177e4SLinus Torvalds 	0xff,				/* slot 9 */
1181da177e4SLinus Torvalds 	0xff,				/* slot 10 */
1191da177e4SLinus Torvalds 	0xff,				/* slot 11 */
1201da177e4SLinus Torvalds   },
1211da177e4SLinus Torvalds   {
1221da177e4SLinus Torvalds 	0xff,				/* slot 3 */
1231da177e4SLinus Torvalds 	0xff,				/* slot 4 */
1241da177e4SLinus Torvalds 	0xff,				/* slot 5 */
1251da177e4SLinus Torvalds 	0xff,				/* slot 6 */
1261da177e4SLinus Torvalds 	0xff,				/* slot 7 */
1271da177e4SLinus Torvalds 	0xff,				/* slot 8 */
1281da177e4SLinus Torvalds 	0xff,				/* slot 9 */
1291da177e4SLinus Torvalds 	0xff,				/* slot 10 */
1301da177e4SLinus Torvalds 	0xff,				/* slot 11 */
1311da177e4SLinus Torvalds   },
1321da177e4SLinus Torvalds   {
1331da177e4SLinus Torvalds 	0xff,				/* slot 3 */
1341da177e4SLinus Torvalds 	0xff,				/* slot 4 */
1351da177e4SLinus Torvalds 	0xff,				/* slot 5 */
1361da177e4SLinus Torvalds 	0xff,				/* slot 6 */
1371da177e4SLinus Torvalds 	0xff,				/* slot 7 */
1381da177e4SLinus Torvalds 	0xff,				/* slot 8 */
1391da177e4SLinus Torvalds 	0xff,				/* slot 9 */
1401da177e4SLinus Torvalds 	0xff,				/* slot 10 */
1411da177e4SLinus Torvalds 	0xff,				/* slot 11 */
1421da177e4SLinus Torvalds   }
1431da177e4SLinus Torvalds }};
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* FIXME: more various mappings for ADC? */
1461da177e4SLinus Torvalds static unsigned char rate_cregs[9] = {
1471da177e4SLinus Torvalds 	AC97_PCM_LR_ADC_RATE,	/* 3 */
1481da177e4SLinus Torvalds 	AC97_PCM_LR_ADC_RATE,	/* 4 */
1491da177e4SLinus Torvalds 	0xff,			/* 5 */
1501da177e4SLinus Torvalds 	AC97_PCM_MIC_ADC_RATE,	/* 6 */
1511da177e4SLinus Torvalds 	0xff,			/* 7 */
1521da177e4SLinus Torvalds 	0xff,			/* 8 */
1531da177e4SLinus Torvalds 	0xff,			/* 9 */
1541da177e4SLinus Torvalds 	0xff,			/* 10 */
1551da177e4SLinus Torvalds 	0xff,			/* 11 */
1561da177e4SLinus Torvalds };
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static unsigned char get_slot_reg(struct ac97_pcm *pcm, unsigned short cidx,
1591da177e4SLinus Torvalds 				  unsigned short slot, int dbl)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	if (slot < 3)
1621da177e4SLinus Torvalds 		return 0xff;
1631da177e4SLinus Torvalds 	if (slot > 11)
1641da177e4SLinus Torvalds 		return 0xff;
1651da177e4SLinus Torvalds 	if (pcm->spdif)
1661da177e4SLinus Torvalds 		return AC97_SPDIF; /* pseudo register */
1671da177e4SLinus Torvalds 	if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK)
1681da177e4SLinus Torvalds 		return rate_reg_tables[dbl][pcm->r[dbl].rate_table[cidx]][slot - 3];
1691da177e4SLinus Torvalds 	else
1701da177e4SLinus Torvalds 		return rate_cregs[slot - 3];
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
173*ee42381eSTakashi Iwai static int set_spdif_rate(struct snd_ac97 *ac97, unsigned short rate)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	unsigned short old, bits, reg, mask;
1761da177e4SLinus Torvalds 	unsigned int sbits;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (! (ac97->ext_id & AC97_EI_SPDIF))
1791da177e4SLinus Torvalds 		return -ENODEV;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/* TODO: double rate support */
1821da177e4SLinus Torvalds 	if (ac97->flags & AC97_CS_SPDIF) {
1831da177e4SLinus Torvalds 		switch (rate) {
1841da177e4SLinus Torvalds 		case 48000: bits = 0; break;
1851da177e4SLinus Torvalds 		case 44100: bits = 1 << AC97_SC_SPSR_SHIFT; break;
1861da177e4SLinus Torvalds 		default: /* invalid - disable output */
1871da177e4SLinus Torvalds 			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
1881da177e4SLinus Torvalds 			return -EINVAL;
1891da177e4SLinus Torvalds 		}
1901da177e4SLinus Torvalds 		reg = AC97_CSR_SPDIF;
1911da177e4SLinus Torvalds 		mask = 1 << AC97_SC_SPSR_SHIFT;
1921da177e4SLinus Torvalds 	} else {
1931da177e4SLinus Torvalds 		if (ac97->id == AC97_ID_CM9739 && rate != 48000) {
1941da177e4SLinus Torvalds 			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
1951da177e4SLinus Torvalds 			return -EINVAL;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 		switch (rate) {
1981da177e4SLinus Torvalds 		case 44100: bits = AC97_SC_SPSR_44K; break;
1991da177e4SLinus Torvalds 		case 48000: bits = AC97_SC_SPSR_48K; break;
2001da177e4SLinus Torvalds 		case 32000: bits = AC97_SC_SPSR_32K; break;
2011da177e4SLinus Torvalds 		default: /* invalid - disable output */
2021da177e4SLinus Torvalds 			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2031da177e4SLinus Torvalds 			return -EINVAL;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 		reg = AC97_SPDIF;
2061da177e4SLinus Torvalds 		mask = AC97_SC_SPSR_MASK;
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	down(&ac97->reg_mutex);
2101da177e4SLinus Torvalds 	old = snd_ac97_read(ac97, reg) & mask;
2111da177e4SLinus Torvalds 	if (old != bits) {
2121da177e4SLinus Torvalds 		snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2131da177e4SLinus Torvalds 		snd_ac97_update_bits_nolock(ac97, reg, mask, bits);
2141da177e4SLinus Torvalds 		/* update the internal spdif bits */
2151da177e4SLinus Torvalds 		sbits = ac97->spdif_status;
2161da177e4SLinus Torvalds 		if (sbits & IEC958_AES0_PROFESSIONAL) {
2171da177e4SLinus Torvalds 			sbits &= ~IEC958_AES0_PRO_FS;
2181da177e4SLinus Torvalds 			switch (rate) {
2191da177e4SLinus Torvalds 			case 44100: sbits |= IEC958_AES0_PRO_FS_44100; break;
2201da177e4SLinus Torvalds 			case 48000: sbits |= IEC958_AES0_PRO_FS_48000; break;
2211da177e4SLinus Torvalds 			case 32000: sbits |= IEC958_AES0_PRO_FS_32000; break;
2221da177e4SLinus Torvalds 			}
2231da177e4SLinus Torvalds 		} else {
2241da177e4SLinus Torvalds 			sbits &= ~(IEC958_AES3_CON_FS << 24);
2251da177e4SLinus Torvalds 			switch (rate) {
2261da177e4SLinus Torvalds 			case 44100: sbits |= IEC958_AES3_CON_FS_44100<<24; break;
2271da177e4SLinus Torvalds 			case 48000: sbits |= IEC958_AES3_CON_FS_48000<<24; break;
2281da177e4SLinus Torvalds 			case 32000: sbits |= IEC958_AES3_CON_FS_32000<<24; break;
2291da177e4SLinus Torvalds 			}
2301da177e4SLinus Torvalds 		}
2311da177e4SLinus Torvalds 		ac97->spdif_status = sbits;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF);
2341da177e4SLinus Torvalds 	up(&ac97->reg_mutex);
2351da177e4SLinus Torvalds 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /**
2391da177e4SLinus Torvalds  * snd_ac97_set_rate - change the rate of the given input/output.
2401da177e4SLinus Torvalds  * @ac97: the ac97 instance
2411da177e4SLinus Torvalds  * @reg: the register to change
2421da177e4SLinus Torvalds  * @rate: the sample rate to set
2431da177e4SLinus Torvalds  *
2441da177e4SLinus Torvalds  * Changes the rate of the given input/output on the codec.
2451da177e4SLinus Torvalds  * If the codec doesn't support VAR, the rate must be 48000 (except
2461da177e4SLinus Torvalds  * for SPDIF).
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * The valid registers are AC97_PMC_MIC_ADC_RATE,
2491da177e4SLinus Torvalds  * AC97_PCM_FRONT_DAC_RATE, AC97_PCM_LR_ADC_RATE.
2501da177e4SLinus Torvalds  * AC97_PCM_SURR_DAC_RATE and AC97_PCM_LFE_DAC_RATE are accepted
2511da177e4SLinus Torvalds  * if the codec supports them.
2521da177e4SLinus Torvalds  * AC97_SPDIF is accepted as a pseudo register to modify the SPDIF
2531da177e4SLinus Torvalds  * status bits.
2541da177e4SLinus Torvalds  *
2551da177e4SLinus Torvalds  * Returns zero if successful, or a negative error code on failure.
2561da177e4SLinus Torvalds  */
257*ee42381eSTakashi Iwai int snd_ac97_set_rate(struct snd_ac97 *ac97, int reg, unsigned int rate)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	int dbl;
2601da177e4SLinus Torvalds 	unsigned int tmp;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	dbl = rate > 48000;
2631da177e4SLinus Torvalds 	if (dbl) {
2641da177e4SLinus Torvalds 		if (!(ac97->flags & AC97_DOUBLE_RATE))
2651da177e4SLinus Torvalds 			return -EINVAL;
2661da177e4SLinus Torvalds 		if (reg != AC97_PCM_FRONT_DAC_RATE)
2671da177e4SLinus Torvalds 			return -EINVAL;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	switch (reg) {
2711da177e4SLinus Torvalds 	case AC97_PCM_MIC_ADC_RATE:
2721da177e4SLinus Torvalds 		if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRM) == 0)	/* MIC VRA */
2731da177e4SLinus Torvalds 			if (rate != 48000)
2741da177e4SLinus Torvalds 				return -EINVAL;
2751da177e4SLinus Torvalds 		break;
2761da177e4SLinus Torvalds 	case AC97_PCM_FRONT_DAC_RATE:
2771da177e4SLinus Torvalds 	case AC97_PCM_LR_ADC_RATE:
2781da177e4SLinus Torvalds 		if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRA) == 0)	/* VRA */
2791da177e4SLinus Torvalds 			if (rate != 48000 && rate != 96000)
2801da177e4SLinus Torvalds 				return -EINVAL;
2811da177e4SLinus Torvalds 		break;
2821da177e4SLinus Torvalds 	case AC97_PCM_SURR_DAC_RATE:
2831da177e4SLinus Torvalds 		if (! (ac97->scaps & AC97_SCAP_SURROUND_DAC))
2841da177e4SLinus Torvalds 			return -EINVAL;
2851da177e4SLinus Torvalds 		break;
2861da177e4SLinus Torvalds 	case AC97_PCM_LFE_DAC_RATE:
2871da177e4SLinus Torvalds 		if (! (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
2881da177e4SLinus Torvalds 			return -EINVAL;
2891da177e4SLinus Torvalds 		break;
2901da177e4SLinus Torvalds 	case AC97_SPDIF:
2911da177e4SLinus Torvalds 		/* special case */
2921da177e4SLinus Torvalds 		return set_spdif_rate(ac97, rate);
2931da177e4SLinus Torvalds 	default:
2941da177e4SLinus Torvalds 		return -EINVAL;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	if (dbl)
2971da177e4SLinus Torvalds 		rate /= 2;
2981da177e4SLinus Torvalds 	tmp = (rate * ac97->bus->clock) / 48000;
2991da177e4SLinus Torvalds 	if (tmp > 65535)
3001da177e4SLinus Torvalds 		return -EINVAL;
3011da177e4SLinus Torvalds 	if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
3021da177e4SLinus Torvalds 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
3031da177e4SLinus Torvalds 				     AC97_EA_DRA, dbl ? AC97_EA_DRA : 0);
3041da177e4SLinus Torvalds 	snd_ac97_update(ac97, reg, tmp & 0xffff);
3051da177e4SLinus Torvalds 	snd_ac97_read(ac97, reg);
306091e95eeSTakashi Iwai 	if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE) {
307091e95eeSTakashi Iwai 		/* Intel controllers require double rate data to be put in
308091e95eeSTakashi Iwai 		 * slots 7+8
309091e95eeSTakashi Iwai 		 */
310091e95eeSTakashi Iwai 		snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE,
311091e95eeSTakashi Iwai 				     AC97_GP_DRSS_MASK,
312091e95eeSTakashi Iwai 				     dbl ? AC97_GP_DRSS_78 : 0);
313091e95eeSTakashi Iwai 		snd_ac97_read(ac97, AC97_GENERAL_PURPOSE);
314091e95eeSTakashi Iwai 	}
3151da177e4SLinus Torvalds 	return 0;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318*ee42381eSTakashi Iwai static unsigned short get_pslots(struct snd_ac97 *ac97, unsigned char *rate_table, unsigned short *spdif_slots)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	if (!ac97_is_audio(ac97))
3211da177e4SLinus Torvalds 		return 0;
3221da177e4SLinus Torvalds 	if (ac97_is_rev22(ac97) || ac97_can_amap(ac97)) {
3231da177e4SLinus Torvalds 		unsigned short slots = 0;
3241da177e4SLinus Torvalds 		if (ac97_is_rev22(ac97)) {
3251da177e4SLinus Torvalds 			/* Note: it's simply emulation of AMAP behaviour */
3261da177e4SLinus Torvalds 			u16 es;
3271da177e4SLinus Torvalds 			es = ac97->regs[AC97_EXTENDED_ID] &= ~AC97_EI_DACS_SLOT_MASK;
3281da177e4SLinus Torvalds 			switch (ac97->addr) {
3291da177e4SLinus Torvalds 			case 1:
3301da177e4SLinus Torvalds 			case 2: es |= (1<<AC97_EI_DACS_SLOT_SHIFT); break;
3311da177e4SLinus Torvalds 			case 3: es |= (2<<AC97_EI_DACS_SLOT_SHIFT); break;
3321da177e4SLinus Torvalds 			}
3331da177e4SLinus Torvalds 			snd_ac97_write_cache(ac97, AC97_EXTENDED_ID, es);
3341da177e4SLinus Torvalds 		}
3351da177e4SLinus Torvalds 		switch (ac97->addr) {
3361da177e4SLinus Torvalds 		case 0:
3371da177e4SLinus Torvalds 			slots |= (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
3381da177e4SLinus Torvalds 			if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
3391da177e4SLinus Torvalds 				slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
3401da177e4SLinus Torvalds 			if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
3411da177e4SLinus Torvalds 				slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
3421da177e4SLinus Torvalds 			if (ac97->ext_id & AC97_EI_SPDIF) {
3431da177e4SLinus Torvalds 				if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
3441da177e4SLinus Torvalds 					*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
3451da177e4SLinus Torvalds 				else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
3461da177e4SLinus Torvalds 					*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
3471da177e4SLinus Torvalds 				else
3481da177e4SLinus Torvalds 					*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
3491da177e4SLinus Torvalds 			}
3501da177e4SLinus Torvalds 			*rate_table = 0;
3511da177e4SLinus Torvalds 			break;
3521da177e4SLinus Torvalds 		case 1:
3531da177e4SLinus Torvalds 		case 2:
3541da177e4SLinus Torvalds 			slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
3551da177e4SLinus Torvalds 			if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
3561da177e4SLinus Torvalds 				slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
3571da177e4SLinus Torvalds 			if (ac97->ext_id & AC97_EI_SPDIF) {
3581da177e4SLinus Torvalds 				if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
3591da177e4SLinus Torvalds 					*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
3601da177e4SLinus Torvalds 				else
3611da177e4SLinus Torvalds 					*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
3621da177e4SLinus Torvalds 			}
3631da177e4SLinus Torvalds 			*rate_table = 1;
3641da177e4SLinus Torvalds 			break;
3651da177e4SLinus Torvalds 		case 3:
3661da177e4SLinus Torvalds 			slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
3671da177e4SLinus Torvalds 			if (ac97->ext_id & AC97_EI_SPDIF)
3681da177e4SLinus Torvalds 				*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
3691da177e4SLinus Torvalds 			*rate_table = 2;
3701da177e4SLinus Torvalds 			break;
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 		return slots;
3731da177e4SLinus Torvalds 	} else {
3741da177e4SLinus Torvalds 		unsigned short slots;
3751da177e4SLinus Torvalds 		slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
3761da177e4SLinus Torvalds 		if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
3771da177e4SLinus Torvalds 			slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
3781da177e4SLinus Torvalds 		if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
3791da177e4SLinus Torvalds 			slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
3801da177e4SLinus Torvalds 		if (ac97->ext_id & AC97_EI_SPDIF) {
3811da177e4SLinus Torvalds 			if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
3821da177e4SLinus Torvalds 				*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
3831da177e4SLinus Torvalds 			else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
3841da177e4SLinus Torvalds 				*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
3851da177e4SLinus Torvalds 			else
3861da177e4SLinus Torvalds 				*spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 		*rate_table = 0;
3891da177e4SLinus Torvalds 		return slots;
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393*ee42381eSTakashi Iwai static unsigned short get_cslots(struct snd_ac97 *ac97)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	unsigned short slots;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if (!ac97_is_audio(ac97))
3981da177e4SLinus Torvalds 		return 0;
3991da177e4SLinus Torvalds 	slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
4001da177e4SLinus Torvalds 	slots |= (1<<AC97_SLOT_MIC);
4011da177e4SLinus Torvalds 	return slots;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds static unsigned int get_rates(struct ac97_pcm *pcm, unsigned int cidx, unsigned short slots, int dbl)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	int i, idx;
4071da177e4SLinus Torvalds 	unsigned int rates = ~0;
4081da177e4SLinus Torvalds 	unsigned char reg;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	for (i = 3; i < 12; i++) {
4111da177e4SLinus Torvalds 		if (!(slots & (1 << i)))
4121da177e4SLinus Torvalds 			continue;
4131da177e4SLinus Torvalds 		reg = get_slot_reg(pcm, cidx, i, dbl);
4141da177e4SLinus Torvalds 		switch (reg) {
4151da177e4SLinus Torvalds 		case AC97_PCM_FRONT_DAC_RATE:	idx = AC97_RATES_FRONT_DAC; break;
4161da177e4SLinus Torvalds 		case AC97_PCM_SURR_DAC_RATE:	idx = AC97_RATES_SURR_DAC; break;
4171da177e4SLinus Torvalds 		case AC97_PCM_LFE_DAC_RATE:	idx = AC97_RATES_LFE_DAC; break;
4181da177e4SLinus Torvalds 		case AC97_PCM_LR_ADC_RATE:	idx = AC97_RATES_ADC; break;
4191da177e4SLinus Torvalds 		case AC97_PCM_MIC_ADC_RATE:	idx = AC97_RATES_MIC_ADC; break;
4201da177e4SLinus Torvalds 		default:			idx = AC97_RATES_SPDIF; break;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 		rates &= pcm->r[dbl].codec[cidx]->rates[idx];
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 	if (!dbl)
4251da177e4SLinus Torvalds 		rates &= ~(SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 |
4261da177e4SLinus Torvalds 			   SNDRV_PCM_RATE_96000);
4271da177e4SLinus Torvalds 	return rates;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds /**
4311da177e4SLinus Torvalds  * snd_ac97_pcm_assign - assign AC97 slots to given PCM streams
4321da177e4SLinus Torvalds  * @bus: the ac97 bus instance
4331da177e4SLinus Torvalds  * @pcms_count: count of PCMs to be assigned
4341da177e4SLinus Torvalds  * @pcms: PCMs to be assigned
4351da177e4SLinus Torvalds  *
4361da177e4SLinus Torvalds  * It assigns available AC97 slots for given PCMs. If none or only
4371da177e4SLinus Torvalds  * some slots are available, pcm->xxx.slots and pcm->xxx.rslots[] members
4381da177e4SLinus Torvalds  * are reduced and might be zero.
4391da177e4SLinus Torvalds  */
440*ee42381eSTakashi Iwai int snd_ac97_pcm_assign(struct snd_ac97_bus *bus,
4411da177e4SLinus Torvalds 			unsigned short pcms_count,
4421da177e4SLinus Torvalds 			const struct ac97_pcm *pcms)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	int i, j, k;
4451da177e4SLinus Torvalds 	const struct ac97_pcm *pcm;
4461da177e4SLinus Torvalds 	struct ac97_pcm *rpcms, *rpcm;
4471da177e4SLinus Torvalds 	unsigned short avail_slots[2][4];
4481da177e4SLinus Torvalds 	unsigned char rate_table[2][4];
4491da177e4SLinus Torvalds 	unsigned short tmp, slots;
4501da177e4SLinus Torvalds 	unsigned short spdif_slots[4];
4511da177e4SLinus Torvalds 	unsigned int rates;
452*ee42381eSTakashi Iwai 	struct snd_ac97 *codec;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	rpcms = kcalloc(pcms_count, sizeof(struct ac97_pcm), GFP_KERNEL);
4551da177e4SLinus Torvalds 	if (rpcms == NULL)
4561da177e4SLinus Torvalds 		return -ENOMEM;
4571da177e4SLinus Torvalds 	memset(avail_slots, 0, sizeof(avail_slots));
4581da177e4SLinus Torvalds 	memset(rate_table, 0, sizeof(rate_table));
4591da177e4SLinus Torvalds 	memset(spdif_slots, 0, sizeof(spdif_slots));
4601da177e4SLinus Torvalds 	for (i = 0; i < 4; i++) {
4611da177e4SLinus Torvalds 		codec = bus->codec[i];
4621da177e4SLinus Torvalds 		if (!codec)
4631da177e4SLinus Torvalds 			continue;
4641da177e4SLinus Torvalds 		avail_slots[0][i] = get_pslots(codec, &rate_table[0][i], &spdif_slots[i]);
4651da177e4SLinus Torvalds 		avail_slots[1][i] = get_cslots(codec);
4661da177e4SLinus Torvalds 		if (!(codec->scaps & AC97_SCAP_INDEP_SDIN)) {
4671da177e4SLinus Torvalds 			for (j = 0; j < i; j++) {
4681da177e4SLinus Torvalds 				if (bus->codec[j])
4691da177e4SLinus Torvalds 					avail_slots[1][i] &= ~avail_slots[1][j];
4701da177e4SLinus Torvalds 			}
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	/* first step - exclusive devices */
4741da177e4SLinus Torvalds 	for (i = 0; i < pcms_count; i++) {
4751da177e4SLinus Torvalds 		pcm = &pcms[i];
4761da177e4SLinus Torvalds 		rpcm = &rpcms[i];
4771da177e4SLinus Torvalds 		/* low-level driver thinks that it's more clever */
4781da177e4SLinus Torvalds 		if (pcm->copy_flag) {
4791da177e4SLinus Torvalds 			*rpcm = *pcm;
4801da177e4SLinus Torvalds 			continue;
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 		rpcm->stream = pcm->stream;
4831da177e4SLinus Torvalds 		rpcm->exclusive = pcm->exclusive;
4841da177e4SLinus Torvalds 		rpcm->spdif = pcm->spdif;
4851da177e4SLinus Torvalds 		rpcm->private_value = pcm->private_value;
4861da177e4SLinus Torvalds 		rpcm->bus = bus;
4871da177e4SLinus Torvalds 		rpcm->rates = ~0;
4881da177e4SLinus Torvalds 		slots = pcm->r[0].slots;
4891da177e4SLinus Torvalds 		for (j = 0; j < 4 && slots; j++) {
4901da177e4SLinus Torvalds 			if (!bus->codec[j])
4911da177e4SLinus Torvalds 				continue;
4921da177e4SLinus Torvalds 			rates = ~0;
4931da177e4SLinus Torvalds 			if (pcm->spdif && pcm->stream == 0)
4941da177e4SLinus Torvalds 				tmp = spdif_slots[j];
4951da177e4SLinus Torvalds 			else
4961da177e4SLinus Torvalds 				tmp = avail_slots[pcm->stream][j];
4971da177e4SLinus Torvalds 			if (pcm->exclusive) {
4981da177e4SLinus Torvalds 				/* exclusive access */
4991da177e4SLinus Torvalds 				tmp &= slots;
5001da177e4SLinus Torvalds 				for (k = 0; k < i; k++) {
5011da177e4SLinus Torvalds 					if (rpcm->stream == rpcms[k].stream)
5021da177e4SLinus Torvalds 						tmp &= ~rpcms[k].r[0].rslots[j];
5031da177e4SLinus Torvalds 				}
5041da177e4SLinus Torvalds 			} else {
5051da177e4SLinus Torvalds 				/* non-exclusive access */
5061da177e4SLinus Torvalds 				tmp &= pcm->r[0].slots;
5071da177e4SLinus Torvalds 			}
5081da177e4SLinus Torvalds 			if (tmp) {
5091da177e4SLinus Torvalds 				rpcm->r[0].rslots[j] = tmp;
5101da177e4SLinus Torvalds 				rpcm->r[0].codec[j] = bus->codec[j];
5111da177e4SLinus Torvalds 				rpcm->r[0].rate_table[j] = rate_table[pcm->stream][j];
5121da177e4SLinus Torvalds 				if (bus->no_vra)
5131da177e4SLinus Torvalds 					rates = SNDRV_PCM_RATE_48000;
5141da177e4SLinus Torvalds 				else
5151da177e4SLinus Torvalds 					rates = get_rates(rpcm, j, tmp, 0);
5161da177e4SLinus Torvalds 				if (pcm->exclusive)
5171da177e4SLinus Torvalds 					avail_slots[pcm->stream][j] &= ~tmp;
5181da177e4SLinus Torvalds 			}
5191da177e4SLinus Torvalds 			slots &= ~tmp;
5201da177e4SLinus Torvalds 			rpcm->r[0].slots |= tmp;
5211da177e4SLinus Torvalds 			rpcm->rates &= rates;
5221da177e4SLinus Torvalds 		}
5231da177e4SLinus Torvalds 		/* for double rate, we check the first codec only */
5241da177e4SLinus Torvalds 		if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK &&
5251da177e4SLinus Torvalds 		    bus->codec[0] && (bus->codec[0]->flags & AC97_DOUBLE_RATE) &&
5261da177e4SLinus Torvalds 		    rate_table[pcm->stream][0] == 0) {
5271da177e4SLinus Torvalds 			tmp = (1<<AC97_SLOT_PCM_LEFT) | (1<<AC97_SLOT_PCM_RIGHT) |
5281da177e4SLinus Torvalds 			      (1<<AC97_SLOT_PCM_LEFT_0) | (1<<AC97_SLOT_PCM_RIGHT_0);
5291da177e4SLinus Torvalds 			if ((tmp & pcm->r[1].slots) == tmp) {
5301da177e4SLinus Torvalds 				rpcm->r[1].slots = tmp;
5311da177e4SLinus Torvalds 				rpcm->r[1].rslots[0] = tmp;
5321da177e4SLinus Torvalds 				rpcm->r[1].rate_table[0] = 0;
5331da177e4SLinus Torvalds 				rpcm->r[1].codec[0] = bus->codec[0];
5341da177e4SLinus Torvalds 				if (pcm->exclusive)
5351da177e4SLinus Torvalds 					avail_slots[pcm->stream][0] &= ~tmp;
5361da177e4SLinus Torvalds 				if (bus->no_vra)
5371da177e4SLinus Torvalds 					rates = SNDRV_PCM_RATE_96000;
5381da177e4SLinus Torvalds 				else
5391da177e4SLinus Torvalds 					rates = get_rates(rpcm, 0, tmp, 1);
5401da177e4SLinus Torvalds 				rpcm->rates |= rates;
5411da177e4SLinus Torvalds 			}
5421da177e4SLinus Torvalds 		}
5431da177e4SLinus Torvalds 		if (rpcm->rates == ~0)
5441da177e4SLinus Torvalds 			rpcm->rates = 0; /* not used */
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 	bus->pcms_count = pcms_count;
5471da177e4SLinus Torvalds 	bus->pcms = rpcms;
5481da177e4SLinus Torvalds 	return 0;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /**
5521da177e4SLinus Torvalds  * snd_ac97_pcm_open - opens the given AC97 pcm
5531da177e4SLinus Torvalds  * @pcm: the ac97 pcm instance
5541da177e4SLinus Torvalds  * @rate: rate in Hz, if codec does not support VRA, this value must be 48000Hz
5551da177e4SLinus Torvalds  * @cfg: output stream characteristics
5561da177e4SLinus Torvalds  * @slots: a subset of allocated slots (snd_ac97_pcm_assign) for this pcm
5571da177e4SLinus Torvalds  *
5581da177e4SLinus Torvalds  * It locks the specified slots and sets the given rate to AC97 registers.
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds int snd_ac97_pcm_open(struct ac97_pcm *pcm, unsigned int rate,
5611da177e4SLinus Torvalds 		      enum ac97_pcm_cfg cfg, unsigned short slots)
5621da177e4SLinus Torvalds {
563*ee42381eSTakashi Iwai 	struct snd_ac97_bus *bus;
5641da177e4SLinus Torvalds 	int i, cidx, r, ok_flag;
5651da177e4SLinus Torvalds 	unsigned int reg_ok[4] = {0,0,0,0};
5661da177e4SLinus Torvalds 	unsigned char reg;
5671da177e4SLinus Torvalds 	int err = 0;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	r = rate > 48000;
5701da177e4SLinus Torvalds 	bus = pcm->bus;
5711da177e4SLinus Torvalds 	if (cfg == AC97_PCM_CFG_SPDIF) {
5721da177e4SLinus Torvalds 		int err;
5731da177e4SLinus Torvalds 		for (cidx = 0; cidx < 4; cidx++)
5741da177e4SLinus Torvalds 			if (bus->codec[cidx] && (bus->codec[cidx]->ext_id & AC97_EI_SPDIF)) {
5751da177e4SLinus Torvalds 				err = set_spdif_rate(bus->codec[cidx], rate);
5761da177e4SLinus Torvalds 				if (err < 0)
5771da177e4SLinus Torvalds 					return err;
5781da177e4SLinus Torvalds 			}
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	spin_lock_irq(&pcm->bus->bus_lock);
5811da177e4SLinus Torvalds 	for (i = 3; i < 12; i++) {
5821da177e4SLinus Torvalds 		if (!(slots & (1 << i)))
5831da177e4SLinus Torvalds 			continue;
5841da177e4SLinus Torvalds 		ok_flag = 0;
5851da177e4SLinus Torvalds 		for (cidx = 0; cidx < 4; cidx++) {
5861da177e4SLinus Torvalds 			if (bus->used_slots[pcm->stream][cidx] & (1 << i)) {
5871da177e4SLinus Torvalds 				spin_unlock_irq(&pcm->bus->bus_lock);
5881da177e4SLinus Torvalds 				err = -EBUSY;
5891da177e4SLinus Torvalds 				goto error;
5901da177e4SLinus Torvalds 			}
5911da177e4SLinus Torvalds 			if (pcm->r[r].rslots[cidx] & (1 << i)) {
5921da177e4SLinus Torvalds 				bus->used_slots[pcm->stream][cidx] |= (1 << i);
5931da177e4SLinus Torvalds 				ok_flag++;
5941da177e4SLinus Torvalds 			}
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 		if (!ok_flag) {
5971da177e4SLinus Torvalds 			spin_unlock_irq(&pcm->bus->bus_lock);
5981da177e4SLinus Torvalds 			snd_printk(KERN_ERR "cannot find configuration for AC97 slot %i\n", i);
5991da177e4SLinus Torvalds 			err = -EAGAIN;
6001da177e4SLinus Torvalds 			goto error;
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 	spin_unlock_irq(&pcm->bus->bus_lock);
6041da177e4SLinus Torvalds 	for (i = 3; i < 12; i++) {
6051da177e4SLinus Torvalds 		if (!(slots & (1 << i)))
6061da177e4SLinus Torvalds 			continue;
6071da177e4SLinus Torvalds 		for (cidx = 0; cidx < 4; cidx++) {
6081da177e4SLinus Torvalds 			if (pcm->r[r].rslots[cidx] & (1 << i)) {
6091da177e4SLinus Torvalds 				reg = get_slot_reg(pcm, cidx, i, r);
6101da177e4SLinus Torvalds 				if (reg == 0xff) {
6111da177e4SLinus Torvalds 					snd_printk(KERN_ERR "invalid AC97 slot %i?\n", i);
6121da177e4SLinus Torvalds 					continue;
6131da177e4SLinus Torvalds 				}
6141da177e4SLinus Torvalds 				if (reg_ok[cidx] & (1 << (reg - AC97_PCM_FRONT_DAC_RATE)))
6151da177e4SLinus Torvalds 					continue;
6161da177e4SLinus Torvalds 				//printk(KERN_DEBUG "setting ac97 reg 0x%x to rate %d\n", reg, rate);
6171da177e4SLinus Torvalds 				err = snd_ac97_set_rate(pcm->r[r].codec[cidx], reg, rate);
6181da177e4SLinus Torvalds 				if (err < 0)
6191da177e4SLinus Torvalds 					snd_printk(KERN_ERR "error in snd_ac97_set_rate: cidx=%d, reg=0x%x, rate=%d, err=%d\n", cidx, reg, rate, err);
6201da177e4SLinus Torvalds 				else
6211da177e4SLinus Torvalds 					reg_ok[cidx] |= (1 << (reg - AC97_PCM_FRONT_DAC_RATE));
6221da177e4SLinus Torvalds 			}
6231da177e4SLinus Torvalds 		}
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 	pcm->aslots = slots;
6261da177e4SLinus Torvalds 	return 0;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds  error:
6291da177e4SLinus Torvalds 	pcm->aslots = slots;
6301da177e4SLinus Torvalds 	snd_ac97_pcm_close(pcm);
6311da177e4SLinus Torvalds 	return err;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds /**
6351da177e4SLinus Torvalds  * snd_ac97_pcm_close - closes the given AC97 pcm
6361da177e4SLinus Torvalds  * @pcm: the ac97 pcm instance
6371da177e4SLinus Torvalds  *
6381da177e4SLinus Torvalds  * It frees the locked AC97 slots.
6391da177e4SLinus Torvalds  */
6401da177e4SLinus Torvalds int snd_ac97_pcm_close(struct ac97_pcm *pcm)
6411da177e4SLinus Torvalds {
642*ee42381eSTakashi Iwai 	struct snd_ac97_bus *bus;
6431da177e4SLinus Torvalds 	unsigned short slots = pcm->aslots;
6441da177e4SLinus Torvalds 	int i, cidx;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	bus = pcm->bus;
6471da177e4SLinus Torvalds 	spin_lock_irq(&pcm->bus->bus_lock);
6481da177e4SLinus Torvalds 	for (i = 3; i < 12; i++) {
6491da177e4SLinus Torvalds 		if (!(slots & (1 << i)))
6501da177e4SLinus Torvalds 			continue;
6511da177e4SLinus Torvalds 		for (cidx = 0; cidx < 4; cidx++)
6521da177e4SLinus Torvalds 			bus->used_slots[pcm->stream][cidx] &= ~(1 << i);
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	pcm->aslots = 0;
6551da177e4SLinus Torvalds 	spin_unlock_irq(&pcm->bus->bus_lock);
6561da177e4SLinus Torvalds 	return 0;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
659*ee42381eSTakashi Iwai static int double_rate_hw_constraint_rate(struct snd_pcm_hw_params *params,
660*ee42381eSTakashi Iwai 					  struct snd_pcm_hw_rule *rule)
6611da177e4SLinus Torvalds {
662*ee42381eSTakashi Iwai 	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
6631da177e4SLinus Torvalds 	if (channels->min > 2) {
664*ee42381eSTakashi Iwai 		static const struct snd_interval single_rates = {
6651da177e4SLinus Torvalds 			.min = 1,
6661da177e4SLinus Torvalds 			.max = 48000,
6671da177e4SLinus Torvalds 		};
668*ee42381eSTakashi Iwai 		struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
6691da177e4SLinus Torvalds 		return snd_interval_refine(rate, &single_rates);
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	return 0;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674*ee42381eSTakashi Iwai static int double_rate_hw_constraint_channels(struct snd_pcm_hw_params *params,
675*ee42381eSTakashi Iwai 					      struct snd_pcm_hw_rule *rule)
6761da177e4SLinus Torvalds {
677*ee42381eSTakashi Iwai 	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
6781da177e4SLinus Torvalds 	if (rate->min > 48000) {
679*ee42381eSTakashi Iwai 		static const struct snd_interval double_rate_channels = {
6801da177e4SLinus Torvalds 			.min = 2,
6811da177e4SLinus Torvalds 			.max = 2,
6821da177e4SLinus Torvalds 		};
683*ee42381eSTakashi Iwai 		struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
6841da177e4SLinus Torvalds 		return snd_interval_refine(channels, &double_rate_channels);
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 	return 0;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /**
6901da177e4SLinus Torvalds  * snd_ac97_pcm_double_rate_rules - set double rate constraints
6911da177e4SLinus Torvalds  * @runtime: the runtime of the ac97 front playback pcm
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  * Installs the hardware constraint rules to prevent using double rates and
6941da177e4SLinus Torvalds  * more than two channels at the same time.
6951da177e4SLinus Torvalds  */
696*ee42381eSTakashi Iwai int snd_ac97_pcm_double_rate_rules(struct snd_pcm_runtime *runtime)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	int err;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
7011da177e4SLinus Torvalds 				  double_rate_hw_constraint_rate, NULL,
7021da177e4SLinus Torvalds 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
7031da177e4SLinus Torvalds 	if (err < 0)
7041da177e4SLinus Torvalds 		return err;
7051da177e4SLinus Torvalds 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
7061da177e4SLinus Torvalds 				  double_rate_hw_constraint_channels, NULL,
7071da177e4SLinus Torvalds 				  SNDRV_PCM_HW_PARAM_RATE, -1);
7081da177e4SLinus Torvalds 	return err;
7091da177e4SLinus Torvalds }
710