From: Takashi Iwai Subject: Re: debugging nm256: some results, many questions Date: Mon, 13 Mar 2006 16:50:31 +0100 Message-ID: References: <20060223185253.GA7954@tigerbay> <20060311174421.GB7775@tigerbay> <20060313064627.GA3864@tigerbay> <20060313145534.GA7995@tigerbay> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20060313145534.GA7995@tigerbay> Sender: alsa-devel-admin@lists.sourceforge.net Errors-To: alsa-devel-admin@lists.sourceforge.net List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , List-Archive: To: Florian Schlichting Cc: alsa-devel@lists.sourceforge.net List-Id: alsa-devel@alsa-project.org At Mon, 13 Mar 2006 22:55:34 +0800, Florian Schlichting wrote: > > On Mon, Mar 13, 2006 at 12:58:46PM +0100, Takashi Iwai wrote: > > > > The writes in snd_nm256_ac97_reset() break the PM resume. You should > > check reg_accessed[] and use the cached value if already written. > > Yes, I just noticed the broken PM resume... The following is just to > check that I don't misunderstand things: > > I originally wanted to write the initial values in snd_nm256_mixer(), > but there ac97 is still an ac97_template_t and doesn't have a register > cache. Still, reg_accessed[] is set there, meaning "register may be > touched". Consequently, in snd_nm256_ac97_reset() all usable > reg_accessed[] are already set, which the first time means nothing (and > all registers should be written), but during a resume means the value > shouldn't be touched... So there needs to be another way to distinguish > between these two states. > > (I couldn't find any documentation about reg_accessed[], which to me > seems to indicate both "register can be accessed" and later "register > value is cached" - which really are two different things and I wonder > whether they should be kept apart?) Well, practically this check is used only by NM256, so it's OK to change that interface. IMO, we can implement the filtering more cleanly in the accessor side, i.e. filtering the register in snd_nm256_ac97_read/write. In that way, we can clean up ac97_code.c, too. How about the patch below? (It's only for CVS version) > Assuming that when first initialized, all cache registers are set to > 0x0000, which is also a valid volume setting, I could probably test for > a non-zero value in AC97_VENDOR_ID1, which would mean I've already done > the initial writing to the cache and it's a resume; otherwise all > registers are to be written. > > > Additionally, we may use a static resolution table to skip the volume > > resolution detection with the latest ac97_codec. > > I couldn't find any indication of that in 1.0.11rc3. Can you give me a > hint? It's newly added in the CVS version. Look at ac97_patch.c:patch_lm4550() Takashi Index: alsa-kernel/include/ac97_codec.h =================================================================== RCS file: /home/iwai/cvs/alsa/alsa-kernel/include/ac97_codec.h,v retrieving revision 1.75 diff -u -r1.75 ac97_codec.h --- alsa-kernel/include/ac97_codec.h 9 Feb 2006 11:43:01 -0000 1.75 +++ alsa-kernel/include/ac97_codec.h 13 Mar 2006 15:49:22 -0000 @@ -433,6 +433,12 @@ struct snd_info_entry *proc; }; +/* static resolution table */ +struct snd_ac97_res_table { + unsigned short reg; /* register */ + unsigned short bits; /* resolution bitmask */ +}; + struct snd_ac97_template { void *private_data; void (*private_free) (struct snd_ac97 *ac97); @@ -440,14 +446,7 @@ unsigned short num; /* number of codec: 0 = primary, 1 = secondary */ unsigned short addr; /* physical address of codec [0-3] */ unsigned int scaps; /* driver capabilities */ - unsigned int limited_regs; /* allow limited registers only */ - DECLARE_BITMAP(reg_accessed, 0x80); /* bit flags */ -}; - -/* static resolution table */ -struct snd_ac97_res_table { - unsigned short reg; /* register */ - unsigned short bits; /* resolution bitmask */ + const struct snd_ac97_res_table *res_table; /* static resolution */ }; struct snd_ac97 { @@ -476,7 +475,6 @@ unsigned int rates[6]; /* see AC97_RATES_* defines */ unsigned int spdif_status; unsigned short regs[0x80]; /* register cache */ - unsigned int limited_regs; /* allow limited registers only */ DECLARE_BITMAP(reg_accessed, 0x80); /* bit flags */ union { /* vendor specific code */ struct { Index: alsa-kernel/pci/ac97/ac97_codec.c =================================================================== RCS file: /home/iwai/cvs/alsa/alsa-kernel/pci/ac97/ac97_codec.c,v retrieving revision 1.202 diff -u -r1.202 ac97_codec.c --- alsa-kernel/pci/ac97/ac97_codec.c 9 Feb 2006 11:46:47 -0000 1.202 +++ alsa-kernel/pci/ac97/ac97_codec.c 13 Mar 2006 15:49:22 -0000 @@ -192,9 +192,6 @@ static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg) { - if (ac97->limited_regs && ! test_bit(reg, ac97->reg_accessed)) - return 0; - /* filter some registers for buggy codecs */ switch (ac97->id) { case AC97_ID_AK4540: @@ -1008,9 +1005,6 @@ break; } - if (ac97->limited_regs && test_bit(reg, ac97->reg_accessed)) - return 1; /* allow without check */ - val = snd_ac97_read(ac97, reg); if (!(val & mask)) { /* nothing seems to be here - mute flag is not set */ @@ -1866,8 +1860,7 @@ ac97->num = template->num; ac97->addr = template->addr; ac97->scaps = template->scaps; - ac97->limited_regs = template->limited_regs; - memcpy(ac97->reg_accessed, template->reg_accessed, sizeof(ac97->reg_accessed)); + ac97->res_table = template->res_table; bus->codec[ac97->num] = ac97; mutex_init(&ac97->reg_mutex); mutex_init(&ac97->page_mutex); Index: alsa-kernel/pci/nm256/nm256.c =================================================================== RCS file: /home/iwai/cvs/alsa/alsa-kernel/pci/nm256/nm256.c,v retrieving revision 1.81 diff -u -r1.81 nm256.c --- alsa-kernel/pci/nm256/nm256.c 16 Jan 2006 16:40:06 -0000 1.81 +++ alsa-kernel/pci/nm256/nm256.c 13 Mar 2006 15:49:22 -0000 @@ -228,6 +228,7 @@ unsigned int use_cache: 1; /* use one big coef. table */ unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */ unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */ + unsigned int in_resume: 1; int mixer_base; /* register offset of ac97 mixer */ int mixer_status_offset; /* offset of mixer status reg. */ @@ -242,6 +243,7 @@ struct nm256_stream streams[2]; struct snd_ac97 *ac97; + DECLARE_BITMAP(allowed_regs, 128); struct snd_pcm *pcm; @@ -1154,22 +1156,17 @@ } /* + * some nm256 easily crash when reading from mixer registers + * thus we're caching both read and written values for reading */ static unsigned short snd_nm256_ac97_read(struct snd_ac97 *ac97, unsigned short reg) { struct nm256 *chip = ac97->private_data; - int res; - if (reg >= 128) + if (! test_bit(reg, chip->allowed_regs)) return 0; - - if (! snd_nm256_ac97_ready(chip)) - return 0; - res = snd_nm256_readw(chip, chip->mixer_base + reg); - /* Magic delay. Bleah yucky. */ - msleep(1); - return res; + return ac97->regs[reg]; } /* @@ -1182,6 +1179,9 @@ int tries = 2; u32 base; + if (! test_bit(reg, chip->allowed_regs)) + return; + base = chip->mixer_base; snd_nm256_ac97_ready(chip); @@ -1190,12 +1190,71 @@ while (tries-- > 0) { snd_nm256_writew(chip, base + reg, val); msleep(1); /* a little delay here seems better.. */ - if (snd_nm256_ac97_ready(chip)) + if (snd_nm256_ac97_ready(chip)) { + /* successful write: set cache */ + ac97->regs[reg] = val; return; + } } snd_printd("nm256: ac97 codec not ready..\n"); } +/* + * below copied from OSS driver + * this list could perhaps be fused with mixer_regs[] ?? + * + * Initial register values to be written to the AC97 mixer. + * While most of these are identical to the reset values, we do this + * so that we have most of the register contents cached--this avoids + * reading from the mixer directly (which seems to be problematic, + * probably due to ignorance). + */ + +struct initialValues +{ + unsigned short reg; + unsigned short value; +}; + +static struct initialValues nm256_ac97_init_val[] = +{ + { AC97_MASTER, 0x8000 }, + { AC97_HEADPHONE, 0x8000 }, + { AC97_MASTER_MONO, 0x8000 }, + { AC97_PC_BEEP, 0x8000 }, + { AC97_PHONE, 0x8008 }, + { AC97_MIC, 0x8000 }, + { AC97_LINE, 0x8808 }, + { AC97_CD, 0x8808 }, + { AC97_VIDEO, 0x8808 }, + { AC97_AUX, 0x8808 }, + { AC97_PCM, 0x8808 }, + { AC97_REC_SEL, 0x0000 }, + { AC97_REC_GAIN, 0x0B0B }, + { AC97_GENERAL_PURPOSE, 0x0000 }, + { AC97_3D_CONTROL, 0x8000 }, + { AC97_VENDOR_ID1, 0x8384 }, + { AC97_VENDOR_ID2, 0x7609 }, + { 0xffff, 0xffff } +}; + +/* static resolution table */ +static struct snd_ac97_res_table nm256_res_table[] = { + { AC97_MASTER, 0x1f1f }, + { AC97_HEADPHONE, 0x1f1f }, + { AC97_MASTER_MONO, 0x001f }, + { AC97_PC_BEEP, 0x001f }, + { AC97_PHONE, 0x001f }, + { AC97_MIC, 0x001f }, + { AC97_LINE, 0x1f1f }, + { AC97_CD, 0x1f1f }, + { AC97_VIDEO, 0x1f1f }, + { AC97_AUX, 0x1f1f }, + { AC97_PCM, 0x1f1f }, + { AC97_REC_GAIN, 0x0f0f }, + { } /* terminator */ +}; + /* initialize the ac97 into a known state */ static void snd_nm256_ac97_reset(struct snd_ac97 *ac97) @@ -1213,6 +1272,16 @@ snd_nm256_writeb(chip, 0x6cc, 0x80); snd_nm256_writeb(chip, 0x6cc, 0x0); } + if (! chip->in_resume) { + struct initialValues *x; + for (x = nm256_ac97_init_val; x->reg != 0xffff; x++) { + /* preload the cache, so as to avoid even a single + * read of the mixer regs + */ + set_bit(x->reg, chip->allowed_regs); + snd_nm256_ac97_write(ac97, x->reg, x->value); + } + } } /* create an ac97 mixer interface */ @@ -1221,32 +1290,20 @@ { struct snd_ac97_bus *pbus; struct snd_ac97_template ac97; - int i, err; + int err; static struct snd_ac97_bus_ops ops = { .reset = snd_nm256_ac97_reset, .write = snd_nm256_ac97_write, .read = snd_nm256_ac97_read, }; - /* looks like nm256 hangs up when unexpected registers are touched... */ - static int mixer_regs[] = { - AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO, - AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD, - AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL, - AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL, - /*AC97_EXTENDED_ID,*/ - AC97_VENDOR_ID1, AC97_VENDOR_ID2, - -1 - }; if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0) return err; memset(&ac97, 0, sizeof(ac97)); ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */ - ac97.limited_regs = 1; - for (i = 0; mixer_regs[i] >= 0; i++) - set_bit(mixer_regs[i], ac97.reg_accessed); ac97.private_data = chip; + ac97.res_table = nm256_res_table; pbus->no_vra = 1; err = snd_ac97_mixer(pbus, &ac97, &chip->ac97); if (err < 0) @@ -1331,6 +1388,7 @@ int i; /* Perform a full reset on the hardware */ + chip->in_resume = 1; pci_restore_state(pci); pci_enable_device(pci); snd_nm256_init_chip(chip); @@ -1348,6 +1406,7 @@ } snd_power_change_state(card, SNDRV_CTL_POWER_D0); + chip->in_resume = 0; return 0; } #endif /* CONFIG_PM */ ------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642