11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) by Jaroslav Kysela <perex@suse.cz> 31da177e4SLinus Torvalds * Routines for control of YMF724/740/744/754 chips 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 161da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 171da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <sound/driver.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 23102fa906SClemens Ladisch #include <linux/firmware.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/interrupt.h> 261da177e4SLinus Torvalds #include <linux/pci.h> 271da177e4SLinus Torvalds #include <linux/sched.h> 281da177e4SLinus Torvalds #include <linux/slab.h> 291da177e4SLinus Torvalds #include <linux/vmalloc.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <sound/core.h> 321da177e4SLinus Torvalds #include <sound/control.h> 331da177e4SLinus Torvalds #include <sound/info.h> 3433925186STakashi Iwai #include <sound/tlv.h> 351da177e4SLinus Torvalds #include <sound/ymfpci.h> 361da177e4SLinus Torvalds #include <sound/asoundef.h> 371da177e4SLinus Torvalds #include <sound/mpu401.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <asm/io.h> 40102fa906SClemens Ladisch #include <asm/byteorder.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* 431da177e4SLinus Torvalds * common I/O routines 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds 46208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip); 471da177e4SLinus Torvalds 48208a1b4cSTakashi Iwai static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds return readb(chip->reg_area_virt + offset); 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds 53208a1b4cSTakashi Iwai static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val) 541da177e4SLinus Torvalds { 551da177e4SLinus Torvalds writeb(val, chip->reg_area_virt + offset); 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 58208a1b4cSTakashi Iwai static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds return readw(chip->reg_area_virt + offset); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 63208a1b4cSTakashi Iwai static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds writew(val, chip->reg_area_virt + offset); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 68208a1b4cSTakashi Iwai static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds return readl(chip->reg_area_virt + offset); 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 73208a1b4cSTakashi Iwai static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds writel(val, chip->reg_area_virt + offset); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 78208a1b4cSTakashi Iwai static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary) 791da177e4SLinus Torvalds { 80ef21ca24SNishanth Aravamudan unsigned long end_time; 811da177e4SLinus Torvalds u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR; 821da177e4SLinus Torvalds 83ef21ca24SNishanth Aravamudan end_time = jiffies + msecs_to_jiffies(750); 841da177e4SLinus Torvalds do { 851da177e4SLinus Torvalds if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0) 861da177e4SLinus Torvalds return 0; 871da177e4SLinus Torvalds set_current_state(TASK_UNINTERRUPTIBLE); 888433a509SNishanth Aravamudan schedule_timeout_uninterruptible(1); 89ef21ca24SNishanth Aravamudan } while (time_before(jiffies, end_time)); 9099b359baSTakashi Iwai snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg)); 911da177e4SLinus Torvalds return -EBUSY; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds 94208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val) 951da177e4SLinus Torvalds { 96208a1b4cSTakashi Iwai struct snd_ymfpci *chip = ac97->private_data; 971da177e4SLinus Torvalds u32 cmd; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds snd_ymfpci_codec_ready(chip, 0); 1001da177e4SLinus Torvalds cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val; 1011da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 104208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg) 1051da177e4SLinus Torvalds { 106208a1b4cSTakashi Iwai struct snd_ymfpci *chip = ac97->private_data; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds if (snd_ymfpci_codec_ready(chip, 0)) 1091da177e4SLinus Torvalds return ~0; 1101da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg); 1111da177e4SLinus Torvalds if (snd_ymfpci_codec_ready(chip, 0)) 1121da177e4SLinus Torvalds return ~0; 1131da177e4SLinus Torvalds if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) { 1141da177e4SLinus Torvalds int i; 1151da177e4SLinus Torvalds for (i = 0; i < 600; i++) 1161da177e4SLinus Torvalds snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA); 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA); 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Misc routines 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds switch (rate) { 1281da177e4SLinus Torvalds case 8000: return 0x02aaab00; 1291da177e4SLinus Torvalds case 11025: return 0x03accd00; 1301da177e4SLinus Torvalds case 16000: return 0x05555500; 1311da177e4SLinus Torvalds case 22050: return 0x07599a00; 1321da177e4SLinus Torvalds case 32000: return 0x0aaaab00; 1331da177e4SLinus Torvalds case 44100: return 0x0eb33300; 1341da177e4SLinus Torvalds default: return ((rate << 16) / 375) << 5; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds static u32 def_rate[8] = { 1391da177e4SLinus Torvalds 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000 1401da177e4SLinus Torvalds }; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds u32 i; 1451da177e4SLinus Torvalds static u32 val[8] = { 1461da177e4SLinus Torvalds 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000, 1471da177e4SLinus Torvalds 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000 1481da177e4SLinus Torvalds }; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds if (rate == 44100) 1511da177e4SLinus Torvalds return 0x40000000; /* FIXME: What's the right value? */ 1521da177e4SLinus Torvalds for (i = 0; i < 8; i++) 1531da177e4SLinus Torvalds if (rate <= def_rate[i]) 1541da177e4SLinus Torvalds return val[i]; 1551da177e4SLinus Torvalds return val[0]; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds u32 i; 1611da177e4SLinus Torvalds static u32 val[8] = { 1621da177e4SLinus Torvalds 0x35280000, 0x34A70000, 0x32020000, 0x31770000, 1631da177e4SLinus Torvalds 0x31390000, 0x31C90000, 0x33D00000, 0x40000000 1641da177e4SLinus Torvalds }; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds if (rate == 44100) 1671da177e4SLinus Torvalds return 0x370A0000; 1681da177e4SLinus Torvalds for (i = 0; i < 8; i++) 1691da177e4SLinus Torvalds if (rate <= def_rate[i]) 1701da177e4SLinus Torvalds return val[i]; 1711da177e4SLinus Torvalds return val[0]; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1749ed1261eSTeru KAMOGASHIRA static void snd_ymfpci_pcm_441_volume_set(struct snd_ymfpci_pcm *ypcm) 1759ed1261eSTeru KAMOGASHIRA { 1769ed1261eSTeru KAMOGASHIRA unsigned int value; 1779ed1261eSTeru KAMOGASHIRA struct snd_ymfpci_pcm_mixer *mixer; 1789ed1261eSTeru KAMOGASHIRA 1799ed1261eSTeru KAMOGASHIRA mixer = &ypcm->chip->pcm_mixer[ypcm->substream->number]; 1809ed1261eSTeru KAMOGASHIRA value = min_t(unsigned int, mixer->left, 0x7fff) >> 1; 1819ed1261eSTeru KAMOGASHIRA value |= (min_t(unsigned int, mixer->right, 0x7fff) >> 1) << 16; 1829ed1261eSTeru KAMOGASHIRA snd_ymfpci_writel(ypcm->chip, YDSXGR_BUF441OUTVOL, value); 1839ed1261eSTeru KAMOGASHIRA } 1849ed1261eSTeru KAMOGASHIRA 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * Hardware start management 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds 189208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds unsigned long flags; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 1941da177e4SLinus Torvalds if (chip->start_count++ > 0) 1951da177e4SLinus Torvalds goto __end; 1961da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 1971da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MODE) | 3); 1981da177e4SLinus Torvalds chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1; 1991da177e4SLinus Torvalds __end: 2001da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds unsigned long flags; 2061da177e4SLinus Torvalds long timeout = 1000; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 2091da177e4SLinus Torvalds if (--chip->start_count > 0) 2101da177e4SLinus Torvalds goto __end; 2111da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 2121da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3); 2131da177e4SLinus Torvalds while (timeout-- > 0) { 2141da177e4SLinus Torvalds if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0) 2151da177e4SLinus Torvalds break; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds if (atomic_read(&chip->interrupt_sleep_count)) { 2181da177e4SLinus Torvalds atomic_set(&chip->interrupt_sleep_count, 0); 2191da177e4SLinus Torvalds wake_up(&chip->interrupt_sleep); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds __end: 2221da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* 2261da177e4SLinus Torvalds * Playback voice management 2271da177e4SLinus Torvalds */ 2281da177e4SLinus Torvalds 229208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip, 230208a1b4cSTakashi Iwai enum snd_ymfpci_voice_type type, int pair, 231208a1b4cSTakashi Iwai struct snd_ymfpci_voice **rvoice) 2321da177e4SLinus Torvalds { 233208a1b4cSTakashi Iwai struct snd_ymfpci_voice *voice, *voice2; 2341da177e4SLinus Torvalds int idx; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds *rvoice = NULL; 2371da177e4SLinus Torvalds for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) { 2381da177e4SLinus Torvalds voice = &chip->voices[idx]; 2391da177e4SLinus Torvalds voice2 = pair ? &chip->voices[idx+1] : NULL; 2401da177e4SLinus Torvalds if (voice->use || (voice2 && voice2->use)) 2411da177e4SLinus Torvalds continue; 2421da177e4SLinus Torvalds voice->use = 1; 2431da177e4SLinus Torvalds if (voice2) 2441da177e4SLinus Torvalds voice2->use = 1; 2451da177e4SLinus Torvalds switch (type) { 2461da177e4SLinus Torvalds case YMFPCI_PCM: 2471da177e4SLinus Torvalds voice->pcm = 1; 2481da177e4SLinus Torvalds if (voice2) 2491da177e4SLinus Torvalds voice2->pcm = 1; 2501da177e4SLinus Torvalds break; 2511da177e4SLinus Torvalds case YMFPCI_SYNTH: 2521da177e4SLinus Torvalds voice->synth = 1; 2531da177e4SLinus Torvalds break; 2541da177e4SLinus Torvalds case YMFPCI_MIDI: 2551da177e4SLinus Torvalds voice->midi = 1; 2561da177e4SLinus Torvalds break; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds snd_ymfpci_hw_start(chip); 2591da177e4SLinus Torvalds if (voice2) 2601da177e4SLinus Torvalds snd_ymfpci_hw_start(chip); 2611da177e4SLinus Torvalds *rvoice = voice; 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds return -ENOMEM; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip, 268208a1b4cSTakashi Iwai enum snd_ymfpci_voice_type type, int pair, 269208a1b4cSTakashi Iwai struct snd_ymfpci_voice **rvoice) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds unsigned long flags; 2721da177e4SLinus Torvalds int result; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds snd_assert(rvoice != NULL, return -EINVAL); 2751da177e4SLinus Torvalds snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds spin_lock_irqsave(&chip->voice_lock, flags); 2781da177e4SLinus Torvalds for (;;) { 2791da177e4SLinus Torvalds result = voice_alloc(chip, type, pair, rvoice); 2801da177e4SLinus Torvalds if (result == 0 || type != YMFPCI_PCM) 2811da177e4SLinus Torvalds break; 2821da177e4SLinus Torvalds /* TODO: synth/midi voice deallocation */ 2831da177e4SLinus Torvalds break; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->voice_lock, flags); 2861da177e4SLinus Torvalds return result; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds unsigned long flags; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds snd_assert(pvoice != NULL, return -EINVAL); 2941da177e4SLinus Torvalds snd_ymfpci_hw_stop(chip); 2951da177e4SLinus Torvalds spin_lock_irqsave(&chip->voice_lock, flags); 2969ed1261eSTeru KAMOGASHIRA if (pvoice->number == chip->src441_used) { 2979ed1261eSTeru KAMOGASHIRA chip->src441_used = -1; 2989ed1261eSTeru KAMOGASHIRA pvoice->ypcm->use_441_slot = 0; 2999ed1261eSTeru KAMOGASHIRA } 3001da177e4SLinus Torvalds pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0; 3011da177e4SLinus Torvalds pvoice->ypcm = NULL; 3021da177e4SLinus Torvalds pvoice->interrupt = NULL; 3031da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->voice_lock, flags); 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * PCM part 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds 311208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice) 3121da177e4SLinus Torvalds { 313208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 3141da177e4SLinus Torvalds u32 pos, delta; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds if ((ypcm = voice->ypcm) == NULL) 3171da177e4SLinus Torvalds return; 3181da177e4SLinus Torvalds if (ypcm->substream == NULL) 3191da177e4SLinus Torvalds return; 3201da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3211da177e4SLinus Torvalds if (ypcm->running) { 3221da177e4SLinus Torvalds pos = le32_to_cpu(voice->bank[chip->active_bank].start); 3231da177e4SLinus Torvalds if (pos < ypcm->last_pos) 3241da177e4SLinus Torvalds delta = pos + (ypcm->buffer_size - ypcm->last_pos); 3251da177e4SLinus Torvalds else 3261da177e4SLinus Torvalds delta = pos - ypcm->last_pos; 3271da177e4SLinus Torvalds ypcm->period_pos += delta; 3281da177e4SLinus Torvalds ypcm->last_pos = pos; 3291da177e4SLinus Torvalds if (ypcm->period_pos >= ypcm->period_size) { 3301da177e4SLinus Torvalds // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start); 3311da177e4SLinus Torvalds ypcm->period_pos %= ypcm->period_size; 3321da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3331da177e4SLinus Torvalds snd_pcm_period_elapsed(ypcm->substream); 3341da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3351da177e4SLinus Torvalds } 3369bcf6551SClemens Ladisch 3379bcf6551SClemens Ladisch if (unlikely(ypcm->update_pcm_vol)) { 3389bcf6551SClemens Ladisch unsigned int subs = ypcm->substream->number; 3399bcf6551SClemens Ladisch unsigned int next_bank = 1 - chip->active_bank; 340208a1b4cSTakashi Iwai struct snd_ymfpci_playback_bank *bank; 3419bcf6551SClemens Ladisch u32 volume; 3429bcf6551SClemens Ladisch 3439bcf6551SClemens Ladisch bank = &voice->bank[next_bank]; 3449bcf6551SClemens Ladisch volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15); 3459bcf6551SClemens Ladisch bank->left_gain_end = volume; 3469bcf6551SClemens Ladisch if (ypcm->output_rear) 3479bcf6551SClemens Ladisch bank->eff2_gain_end = volume; 3489bcf6551SClemens Ladisch if (ypcm->voices[1]) 3499bcf6551SClemens Ladisch bank = &ypcm->voices[1]->bank[next_bank]; 3509bcf6551SClemens Ladisch volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15); 3519bcf6551SClemens Ladisch bank->right_gain_end = volume; 3529bcf6551SClemens Ladisch if (ypcm->output_rear) 3539bcf6551SClemens Ladisch bank->eff3_gain_end = volume; 3549bcf6551SClemens Ladisch ypcm->update_pcm_vol--; 3559bcf6551SClemens Ladisch } 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream) 3611da177e4SLinus Torvalds { 362208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 363208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 364208a1b4cSTakashi Iwai struct snd_ymfpci *chip = ypcm->chip; 3651da177e4SLinus Torvalds u32 pos, delta; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3681da177e4SLinus Torvalds if (ypcm->running) { 3691da177e4SLinus Torvalds pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift; 3701da177e4SLinus Torvalds if (pos < ypcm->last_pos) 3711da177e4SLinus Torvalds delta = pos + (ypcm->buffer_size - ypcm->last_pos); 3721da177e4SLinus Torvalds else 3731da177e4SLinus Torvalds delta = pos - ypcm->last_pos; 3741da177e4SLinus Torvalds ypcm->period_pos += delta; 3751da177e4SLinus Torvalds ypcm->last_pos = pos; 3761da177e4SLinus Torvalds if (ypcm->period_pos >= ypcm->period_size) { 3771da177e4SLinus Torvalds ypcm->period_pos %= ypcm->period_size; 3781da177e4SLinus Torvalds // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start); 3791da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3801da177e4SLinus Torvalds snd_pcm_period_elapsed(substream); 3811da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 387208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream, 3881da177e4SLinus Torvalds int cmd) 3891da177e4SLinus Torvalds { 390208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 391208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; 3921da177e4SLinus Torvalds int result = 0; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3951da177e4SLinus Torvalds if (ypcm->voices[0] == NULL) { 3961da177e4SLinus Torvalds result = -EINVAL; 3971da177e4SLinus Torvalds goto __unlock; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds switch (cmd) { 4001da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_START: 4011da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 4021da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_RESUME: 4031da177e4SLinus Torvalds chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr); 4049ed1261eSTeru KAMOGASHIRA if (ypcm->voices[1] != NULL && !ypcm->use_441_slot) 4051da177e4SLinus Torvalds chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr); 4061da177e4SLinus Torvalds ypcm->running = 1; 4071da177e4SLinus Torvalds break; 4081da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_STOP: 4091da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 4101da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_SUSPEND: 4111da177e4SLinus Torvalds chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0; 4129ed1261eSTeru KAMOGASHIRA if (ypcm->voices[1] != NULL && !ypcm->use_441_slot) 4131da177e4SLinus Torvalds chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0; 4141da177e4SLinus Torvalds ypcm->running = 0; 4151da177e4SLinus Torvalds break; 4161da177e4SLinus Torvalds default: 4171da177e4SLinus Torvalds result = -EINVAL; 4181da177e4SLinus Torvalds break; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds __unlock: 4211da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 4221da177e4SLinus Torvalds return result; 4231da177e4SLinus Torvalds } 424208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream, 4251da177e4SLinus Torvalds int cmd) 4261da177e4SLinus Torvalds { 427208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 428208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; 4291da177e4SLinus Torvalds int result = 0; 4301da177e4SLinus Torvalds u32 tmp; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 4331da177e4SLinus Torvalds switch (cmd) { 4341da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_START: 4351da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 4361da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_RESUME: 4371da177e4SLinus Torvalds tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number); 4381da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp); 4391da177e4SLinus Torvalds ypcm->running = 1; 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_STOP: 4421da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 4431da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_SUSPEND: 4441da177e4SLinus Torvalds tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number); 4451da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp); 4461da177e4SLinus Torvalds ypcm->running = 0; 4471da177e4SLinus Torvalds break; 4481da177e4SLinus Torvalds default: 4491da177e4SLinus Torvalds result = -EINVAL; 4501da177e4SLinus Torvalds break; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 4531da177e4SLinus Torvalds return result; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 456208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices) 4571da177e4SLinus Torvalds { 4581da177e4SLinus Torvalds int err; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds if (ypcm->voices[1] != NULL && voices < 2) { 4611da177e4SLinus Torvalds snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]); 4621da177e4SLinus Torvalds ypcm->voices[1] = NULL; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds if (voices == 1 && ypcm->voices[0] != NULL) 4651da177e4SLinus Torvalds return 0; /* already allocated */ 4661da177e4SLinus Torvalds if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL) 4671da177e4SLinus Torvalds return 0; /* already allocated */ 4681da177e4SLinus Torvalds if (voices > 1) { 4691da177e4SLinus Torvalds if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) { 4701da177e4SLinus Torvalds snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]); 4711da177e4SLinus Torvalds ypcm->voices[0] = NULL; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]); 4751da177e4SLinus Torvalds if (err < 0) 4761da177e4SLinus Torvalds return err; 4771da177e4SLinus Torvalds ypcm->voices[0]->ypcm = ypcm; 4781da177e4SLinus Torvalds ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt; 4791da177e4SLinus Torvalds if (voices > 1) { 4801da177e4SLinus Torvalds ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1]; 4811da177e4SLinus Torvalds ypcm->voices[1]->ypcm = ypcm; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds return 0; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 486208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx, 487208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime, 4889bcf6551SClemens Ladisch int has_pcm_volume) 4891da177e4SLinus Torvalds { 490208a1b4cSTakashi Iwai struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx]; 4911da177e4SLinus Torvalds u32 format; 4929bcf6551SClemens Ladisch u32 delta = snd_ymfpci_calc_delta(runtime->rate); 4939bcf6551SClemens Ladisch u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate); 4949bcf6551SClemens Ladisch u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate); 495208a1b4cSTakashi Iwai struct snd_ymfpci_playback_bank *bank; 4961da177e4SLinus Torvalds unsigned int nbank; 4979bcf6551SClemens Ladisch u32 vol_left, vol_right; 4989bcf6551SClemens Ladisch u8 use_left, use_right; 4999ed1261eSTeru KAMOGASHIRA unsigned long flags; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds snd_assert(voice != NULL, return); 5029bcf6551SClemens Ladisch if (runtime->channels == 1) { 5039bcf6551SClemens Ladisch use_left = 1; 5049bcf6551SClemens Ladisch use_right = 1; 5059bcf6551SClemens Ladisch } else { 5069bcf6551SClemens Ladisch use_left = (voiceidx & 1) == 0; 5079bcf6551SClemens Ladisch use_right = !use_left; 5089bcf6551SClemens Ladisch } 5099bcf6551SClemens Ladisch if (has_pcm_volume) { 5109bcf6551SClemens Ladisch vol_left = cpu_to_le32(ypcm->chip->pcm_mixer 5119bcf6551SClemens Ladisch [ypcm->substream->number].left << 15); 5129bcf6551SClemens Ladisch vol_right = cpu_to_le32(ypcm->chip->pcm_mixer 5139bcf6551SClemens Ladisch [ypcm->substream->number].right << 15); 5149bcf6551SClemens Ladisch } else { 5159bcf6551SClemens Ladisch vol_left = cpu_to_le32(0x40000000); 5169bcf6551SClemens Ladisch vol_right = cpu_to_le32(0x40000000); 5179bcf6551SClemens Ladisch } 5189ed1261eSTeru KAMOGASHIRA spin_lock_irqsave(&ypcm->chip->voice_lock, flags); 5199bcf6551SClemens Ladisch format = runtime->channels == 2 ? 0x00010000 : 0; 5209bcf6551SClemens Ladisch if (snd_pcm_format_width(runtime->format) == 8) 5219bcf6551SClemens Ladisch format |= 0x80000000; 5229ed1261eSTeru KAMOGASHIRA else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 && 5239ed1261eSTeru KAMOGASHIRA runtime->rate == 44100 && runtime->channels == 2 && 5249ed1261eSTeru KAMOGASHIRA voiceidx == 0 && (ypcm->chip->src441_used == -1 || 5259ed1261eSTeru KAMOGASHIRA ypcm->chip->src441_used == voice->number)) { 5269ed1261eSTeru KAMOGASHIRA ypcm->chip->src441_used = voice->number; 5279ed1261eSTeru KAMOGASHIRA ypcm->use_441_slot = 1; 5289ed1261eSTeru KAMOGASHIRA format |= 0x10000000; 5299ed1261eSTeru KAMOGASHIRA snd_ymfpci_pcm_441_volume_set(ypcm); 5309ed1261eSTeru KAMOGASHIRA } 5319ed1261eSTeru KAMOGASHIRA if (ypcm->chip->src441_used == voice->number && 5329ed1261eSTeru KAMOGASHIRA (format & 0x10000000) == 0) { 5339ed1261eSTeru KAMOGASHIRA ypcm->chip->src441_used = -1; 5349ed1261eSTeru KAMOGASHIRA ypcm->use_441_slot = 0; 5359ed1261eSTeru KAMOGASHIRA } 5369bcf6551SClemens Ladisch if (runtime->channels == 2 && (voiceidx & 1) != 0) 5379bcf6551SClemens Ladisch format |= 1; 5389ed1261eSTeru KAMOGASHIRA spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags); 5391da177e4SLinus Torvalds for (nbank = 0; nbank < 2; nbank++) { 5401da177e4SLinus Torvalds bank = &voice->bank[nbank]; 5419bcf6551SClemens Ladisch memset(bank, 0, sizeof(*bank)); 5421da177e4SLinus Torvalds bank->format = cpu_to_le32(format); 5439bcf6551SClemens Ladisch bank->base = cpu_to_le32(runtime->dma_addr); 5449bcf6551SClemens Ladisch bank->loop_end = cpu_to_le32(ypcm->buffer_size); 5451da177e4SLinus Torvalds bank->lpfQ = cpu_to_le32(lpfQ); 5461da177e4SLinus Torvalds bank->delta = 5471da177e4SLinus Torvalds bank->delta_end = cpu_to_le32(delta); 5481da177e4SLinus Torvalds bank->lpfK = 5491da177e4SLinus Torvalds bank->lpfK_end = cpu_to_le32(lpfK); 5509bcf6551SClemens Ladisch bank->eg_gain = 5519bcf6551SClemens Ladisch bank->eg_gain_end = cpu_to_le32(0x40000000); 5521da177e4SLinus Torvalds 5539bcf6551SClemens Ladisch if (ypcm->output_front) { 5549bcf6551SClemens Ladisch if (use_left) { 5551da177e4SLinus Torvalds bank->left_gain = 5569bcf6551SClemens Ladisch bank->left_gain_end = vol_left; 5579bcf6551SClemens Ladisch } 5589bcf6551SClemens Ladisch if (use_right) { 5591da177e4SLinus Torvalds bank->right_gain = 5609bcf6551SClemens Ladisch bank->right_gain_end = vol_right; 5619bcf6551SClemens Ladisch } 5629bcf6551SClemens Ladisch } 5639bcf6551SClemens Ladisch if (ypcm->output_rear) { 5645a25c5cfSJaroslav Kysela if (!ypcm->swap_rear) { 5655a25c5cfSJaroslav Kysela if (use_left) { 5665a25c5cfSJaroslav Kysela bank->eff2_gain = 5675a25c5cfSJaroslav Kysela bank->eff2_gain_end = vol_left; 5685a25c5cfSJaroslav Kysela } 5695a25c5cfSJaroslav Kysela if (use_right) { 5705a25c5cfSJaroslav Kysela bank->eff3_gain = 5715a25c5cfSJaroslav Kysela bank->eff3_gain_end = vol_right; 5725a25c5cfSJaroslav Kysela } 5735a25c5cfSJaroslav Kysela } else { 574153abaa4SClemens Ladisch /* The SPDIF out channels seem to be swapped, so we have 575153abaa4SClemens Ladisch * to swap them here, too. The rear analog out channels 576153abaa4SClemens Ladisch * will be wrong, but otherwise AC3 would not work. 577153abaa4SClemens Ladisch */ 5789bcf6551SClemens Ladisch if (use_left) { 579153abaa4SClemens Ladisch bank->eff3_gain = 580153abaa4SClemens Ladisch bank->eff3_gain_end = vol_left; 5819bcf6551SClemens Ladisch } 5829bcf6551SClemens Ladisch if (use_right) { 583153abaa4SClemens Ladisch bank->eff2_gain = 584153abaa4SClemens Ladisch bank->eff2_gain_end = vol_right; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds } 5895a25c5cfSJaroslav Kysela } 5901da177e4SLinus Torvalds 591208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci), 5941da177e4SLinus Torvalds 4096, &chip->ac3_tmp_base) < 0) 5951da177e4SLinus Torvalds return -ENOMEM; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds chip->bank_effect[3][0]->base = 5981da177e4SLinus Torvalds chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr); 5991da177e4SLinus Torvalds chip->bank_effect[3][0]->loop_end = 6001da177e4SLinus Torvalds chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024); 6011da177e4SLinus Torvalds chip->bank_effect[4][0]->base = 6021da177e4SLinus Torvalds chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048); 6031da177e4SLinus Torvalds chip->bank_effect[4][0]->loop_end = 6041da177e4SLinus Torvalds chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 6071da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 6081da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3); 6091da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 6101da177e4SLinus Torvalds return 0; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 613208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 6161da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 6171da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3)); 6181da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 6191da177e4SLinus Torvalds // snd_ymfpci_irq_wait(chip); 6201da177e4SLinus Torvalds if (chip->ac3_tmp_base.area) { 6211da177e4SLinus Torvalds snd_dma_free_pages(&chip->ac3_tmp_base); 6221da177e4SLinus Torvalds chip->ac3_tmp_base.area = NULL; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds return 0; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 627208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream, 628208a1b4cSTakashi Iwai struct snd_pcm_hw_params *hw_params) 6291da177e4SLinus Torvalds { 630208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 631208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 6321da177e4SLinus Torvalds int err; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) 6351da177e4SLinus Torvalds return err; 6361da177e4SLinus Torvalds if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0) 6371da177e4SLinus Torvalds return err; 6381da177e4SLinus Torvalds return 0; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 641208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream) 6421da177e4SLinus Torvalds { 643208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 644208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 645208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds if (runtime->private_data == NULL) 6481da177e4SLinus Torvalds return 0; 6491da177e4SLinus Torvalds ypcm = runtime->private_data; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* wait, until the PCI operations are not finished */ 6521da177e4SLinus Torvalds snd_ymfpci_irq_wait(chip); 6531da177e4SLinus Torvalds snd_pcm_lib_free_pages(substream); 6541da177e4SLinus Torvalds if (ypcm->voices[1]) { 6551da177e4SLinus Torvalds snd_ymfpci_voice_free(chip, ypcm->voices[1]); 6561da177e4SLinus Torvalds ypcm->voices[1] = NULL; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds if (ypcm->voices[0]) { 6591da177e4SLinus Torvalds snd_ymfpci_voice_free(chip, ypcm->voices[0]); 6601da177e4SLinus Torvalds ypcm->voices[0] = NULL; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds return 0; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 665208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream) 6661da177e4SLinus Torvalds { 667208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 668208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 669208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 6701da177e4SLinus Torvalds unsigned int nvoice; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds ypcm->period_size = runtime->period_size; 6731da177e4SLinus Torvalds ypcm->buffer_size = runtime->buffer_size; 6741da177e4SLinus Torvalds ypcm->period_pos = 0; 6751da177e4SLinus Torvalds ypcm->last_pos = 0; 6761da177e4SLinus Torvalds for (nvoice = 0; nvoice < runtime->channels; nvoice++) 6779bcf6551SClemens Ladisch snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime, 6789bcf6551SClemens Ladisch substream->pcm == chip->pcm); 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 682208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream, 683208a1b4cSTakashi Iwai struct snd_pcm_hw_params *hw_params) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 688208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream) 6891da177e4SLinus Torvalds { 690208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* wait, until the PCI operations are not finished */ 6931da177e4SLinus Torvalds snd_ymfpci_irq_wait(chip); 6941da177e4SLinus Torvalds return snd_pcm_lib_free_pages(substream); 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 697208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream) 6981da177e4SLinus Torvalds { 699208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 700208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 701208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 702208a1b4cSTakashi Iwai struct snd_ymfpci_capture_bank * bank; 7031da177e4SLinus Torvalds int nbank; 7041da177e4SLinus Torvalds u32 rate, format; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds ypcm->period_size = runtime->period_size; 7071da177e4SLinus Torvalds ypcm->buffer_size = runtime->buffer_size; 7081da177e4SLinus Torvalds ypcm->period_pos = 0; 7091da177e4SLinus Torvalds ypcm->last_pos = 0; 7101da177e4SLinus Torvalds ypcm->shift = 0; 7111da177e4SLinus Torvalds rate = ((48000 * 4096) / runtime->rate) - 1; 7121da177e4SLinus Torvalds format = 0; 7131da177e4SLinus Torvalds if (runtime->channels == 2) { 7141da177e4SLinus Torvalds format |= 2; 7151da177e4SLinus Torvalds ypcm->shift++; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds if (snd_pcm_format_width(runtime->format) == 8) 7181da177e4SLinus Torvalds format |= 1; 7191da177e4SLinus Torvalds else 7201da177e4SLinus Torvalds ypcm->shift++; 7211da177e4SLinus Torvalds switch (ypcm->capture_bank_number) { 7221da177e4SLinus Torvalds case 0: 7231da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format); 7241da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate); 7251da177e4SLinus Torvalds break; 7261da177e4SLinus Torvalds case 1: 7271da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format); 7281da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate); 7291da177e4SLinus Torvalds break; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds for (nbank = 0; nbank < 2; nbank++) { 7321da177e4SLinus Torvalds bank = chip->bank_capture[ypcm->capture_bank_number][nbank]; 7331da177e4SLinus Torvalds bank->base = cpu_to_le32(runtime->dma_addr); 7341da177e4SLinus Torvalds bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift); 7351da177e4SLinus Torvalds bank->start = 0; 7361da177e4SLinus Torvalds bank->num_of_loops = 0; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds return 0; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream) 7421da177e4SLinus Torvalds { 743208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 744208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 745208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 746208a1b4cSTakashi Iwai struct snd_ymfpci_voice *voice = ypcm->voices[0]; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds if (!(ypcm->running && voice)) 7491da177e4SLinus Torvalds return 0; 7501da177e4SLinus Torvalds return le32_to_cpu(voice->bank[chip->active_bank].start); 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream) 7541da177e4SLinus Torvalds { 755208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 756208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 757208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds if (!ypcm->running) 7601da177e4SLinus Torvalds return 0; 7611da177e4SLinus Torvalds return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 764208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip) 7651da177e4SLinus Torvalds { 7661da177e4SLinus Torvalds wait_queue_t wait; 7671da177e4SLinus Torvalds int loops = 4; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds while (loops-- > 0) { 7701da177e4SLinus Torvalds if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0) 7711da177e4SLinus Torvalds continue; 7721da177e4SLinus Torvalds init_waitqueue_entry(&wait, current); 7731da177e4SLinus Torvalds add_wait_queue(&chip->interrupt_sleep, &wait); 7741da177e4SLinus Torvalds atomic_inc(&chip->interrupt_sleep_count); 7758433a509SNishanth Aravamudan schedule_timeout_uninterruptible(msecs_to_jiffies(50)); 7761da177e4SLinus Torvalds remove_wait_queue(&chip->interrupt_sleep, &wait); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7807d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id) 7811da177e4SLinus Torvalds { 782208a1b4cSTakashi Iwai struct snd_ymfpci *chip = dev_id; 7831da177e4SLinus Torvalds u32 status, nvoice, mode; 784208a1b4cSTakashi Iwai struct snd_ymfpci_voice *voice; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds status = snd_ymfpci_readl(chip, YDSXGR_STATUS); 7871da177e4SLinus Torvalds if (status & 0x80000000) { 7881da177e4SLinus Torvalds chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1; 7891da177e4SLinus Torvalds spin_lock(&chip->voice_lock); 7901da177e4SLinus Torvalds for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) { 7911da177e4SLinus Torvalds voice = &chip->voices[nvoice]; 7921da177e4SLinus Torvalds if (voice->interrupt) 7931da177e4SLinus Torvalds voice->interrupt(chip, voice); 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) { 7961da177e4SLinus Torvalds if (chip->capture_substream[nvoice]) 7971da177e4SLinus Torvalds snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]); 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds #if 0 8001da177e4SLinus Torvalds for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) { 8011da177e4SLinus Torvalds if (chip->effect_substream[nvoice]) 8021da177e4SLinus Torvalds snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds #endif 8051da177e4SLinus Torvalds spin_unlock(&chip->voice_lock); 8061da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 8071da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000); 8081da177e4SLinus Torvalds mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2; 8091da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, mode); 8101da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds if (atomic_read(&chip->interrupt_sleep_count)) { 8131da177e4SLinus Torvalds atomic_set(&chip->interrupt_sleep_count, 0); 8141da177e4SLinus Torvalds wake_up(&chip->interrupt_sleep); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG); 8191da177e4SLinus Torvalds if (status & 1) { 8201da177e4SLinus Torvalds if (chip->timer) 8211da177e4SLinus Torvalds snd_timer_interrupt(chip->timer, chip->timer->sticks); 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status); 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds if (chip->rawmidi) 8267d12e780SDavid Howells snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data); 8271da177e4SLinus Torvalds return IRQ_HANDLED; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 830208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_playback = 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds .info = (SNDRV_PCM_INFO_MMAP | 8331da177e4SLinus Torvalds SNDRV_PCM_INFO_MMAP_VALID | 8341da177e4SLinus Torvalds SNDRV_PCM_INFO_INTERLEAVED | 8351da177e4SLinus Torvalds SNDRV_PCM_INFO_BLOCK_TRANSFER | 8361da177e4SLinus Torvalds SNDRV_PCM_INFO_PAUSE | 8371da177e4SLinus Torvalds SNDRV_PCM_INFO_RESUME), 8381da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, 8391da177e4SLinus Torvalds .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, 8401da177e4SLinus Torvalds .rate_min = 8000, 8411da177e4SLinus Torvalds .rate_max = 48000, 8421da177e4SLinus Torvalds .channels_min = 1, 8431da177e4SLinus Torvalds .channels_max = 2, 8441da177e4SLinus Torvalds .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */ 8451da177e4SLinus Torvalds .period_bytes_min = 64, 8461da177e4SLinus Torvalds .period_bytes_max = 256 * 1024, /* FIXME: enough? */ 8471da177e4SLinus Torvalds .periods_min = 3, 8481da177e4SLinus Torvalds .periods_max = 1024, 8491da177e4SLinus Torvalds .fifo_size = 0, 8501da177e4SLinus Torvalds }; 8511da177e4SLinus Torvalds 852208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_capture = 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds .info = (SNDRV_PCM_INFO_MMAP | 8551da177e4SLinus Torvalds SNDRV_PCM_INFO_MMAP_VALID | 8561da177e4SLinus Torvalds SNDRV_PCM_INFO_INTERLEAVED | 8571da177e4SLinus Torvalds SNDRV_PCM_INFO_BLOCK_TRANSFER | 8581da177e4SLinus Torvalds SNDRV_PCM_INFO_PAUSE | 8591da177e4SLinus Torvalds SNDRV_PCM_INFO_RESUME), 8601da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, 8611da177e4SLinus Torvalds .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, 8621da177e4SLinus Torvalds .rate_min = 8000, 8631da177e4SLinus Torvalds .rate_max = 48000, 8641da177e4SLinus Torvalds .channels_min = 1, 8651da177e4SLinus Torvalds .channels_max = 2, 8661da177e4SLinus Torvalds .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */ 8671da177e4SLinus Torvalds .period_bytes_min = 64, 8681da177e4SLinus Torvalds .period_bytes_max = 256 * 1024, /* FIXME: enough? */ 8691da177e4SLinus Torvalds .periods_min = 3, 8701da177e4SLinus Torvalds .periods_max = 1024, 8711da177e4SLinus Torvalds .fifo_size = 0, 8721da177e4SLinus Torvalds }; 8731da177e4SLinus Torvalds 874208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime) 8751da177e4SLinus Torvalds { 8764d572776SJesper Juhl kfree(runtime->private_data); 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream) 8801da177e4SLinus Torvalds { 881208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 882208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 883208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 8841da177e4SLinus Torvalds 885e560d8d8STakashi Iwai ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL); 8861da177e4SLinus Torvalds if (ypcm == NULL) 8871da177e4SLinus Torvalds return -ENOMEM; 8881da177e4SLinus Torvalds ypcm->chip = chip; 8891da177e4SLinus Torvalds ypcm->type = PLAYBACK_VOICE; 8901da177e4SLinus Torvalds ypcm->substream = substream; 8911da177e4SLinus Torvalds runtime->hw = snd_ymfpci_playback; 8921da177e4SLinus Torvalds runtime->private_data = ypcm; 8931da177e4SLinus Torvalds runtime->private_free = snd_ymfpci_pcm_free_substream; 8941da177e4SLinus Torvalds /* FIXME? True value is 256/48 = 5.33333 ms */ 8951da177e4SLinus Torvalds snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX); 8961da177e4SLinus Torvalds return 0; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* call with spinlock held */ 900208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip) 9011da177e4SLinus Torvalds { 9021da177e4SLinus Torvalds if (! chip->rear_opened) { 9031da177e4SLinus Torvalds if (! chip->spdif_opened) /* set AC3 */ 9041da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 9051da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30)); 9061da177e4SLinus Torvalds /* enable second codec (4CHEN) */ 9071da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SECCONFIG, 9081da177e4SLinus Torvalds (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010); 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* call with spinlock held */ 913208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip) 9141da177e4SLinus Torvalds { 9151da177e4SLinus Torvalds if (! chip->rear_opened) { 9161da177e4SLinus Torvalds if (! chip->spdif_opened) 9171da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 9181da177e4SLinus Torvalds snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30)); 9191da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SECCONFIG, 9201da177e4SLinus Torvalds (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010); 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 924208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream) 9251da177e4SLinus Torvalds { 926208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 927208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 928208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 929208a1b4cSTakashi Iwai struct snd_kcontrol *kctl; 9301da177e4SLinus Torvalds int err; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds if ((err = snd_ymfpci_playback_open_1(substream)) < 0) 9331da177e4SLinus Torvalds return err; 9341da177e4SLinus Torvalds ypcm = runtime->private_data; 9351da177e4SLinus Torvalds ypcm->output_front = 1; 9361da177e4SLinus Torvalds ypcm->output_rear = chip->mode_dup4ch ? 1 : 0; 937d9301263SGlen Masgai ypcm->swap_rear = 0; 9381da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 9391da177e4SLinus Torvalds if (ypcm->output_rear) { 9401da177e4SLinus Torvalds ymfpci_open_extension(chip); 9411da177e4SLinus Torvalds chip->rear_opened++; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 9449bcf6551SClemens Ladisch 9459bcf6551SClemens Ladisch kctl = chip->pcm_mixer[substream->number].ctl; 9469bcf6551SClemens Ladisch kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 9479bcf6551SClemens Ladisch snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id); 9481da177e4SLinus Torvalds return 0; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream) 9521da177e4SLinus Torvalds { 953208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 954208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 955208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 9561da177e4SLinus Torvalds int err; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds if ((err = snd_ymfpci_playback_open_1(substream)) < 0) 9591da177e4SLinus Torvalds return err; 9601da177e4SLinus Torvalds ypcm = runtime->private_data; 9611da177e4SLinus Torvalds ypcm->output_front = 0; 9621da177e4SLinus Torvalds ypcm->output_rear = 1; 963d9301263SGlen Masgai ypcm->swap_rear = 1; 9641da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 9651da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 9661da177e4SLinus Torvalds snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2); 9671da177e4SLinus Torvalds ymfpci_open_extension(chip); 9681da177e4SLinus Torvalds chip->spdif_pcm_bits = chip->spdif_bits; 9691da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits); 9701da177e4SLinus Torvalds chip->spdif_opened++; 9711da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 9741da177e4SLinus Torvalds snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE | 9751da177e4SLinus Torvalds SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id); 9761da177e4SLinus Torvalds return 0; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 979208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream) 9801da177e4SLinus Torvalds { 981208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 982208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 983208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 9841da177e4SLinus Torvalds int err; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds if ((err = snd_ymfpci_playback_open_1(substream)) < 0) 9871da177e4SLinus Torvalds return err; 9881da177e4SLinus Torvalds ypcm = runtime->private_data; 9891da177e4SLinus Torvalds ypcm->output_front = 0; 9901da177e4SLinus Torvalds ypcm->output_rear = 1; 991d9301263SGlen Masgai ypcm->swap_rear = 0; 9921da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 9931da177e4SLinus Torvalds ymfpci_open_extension(chip); 9941da177e4SLinus Torvalds chip->rear_opened++; 9951da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 999208a1b4cSTakashi Iwai static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream, 10001da177e4SLinus Torvalds u32 capture_bank_number) 10011da177e4SLinus Torvalds { 1002208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 1003208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 1004208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm; 10051da177e4SLinus Torvalds 1006e560d8d8STakashi Iwai ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL); 10071da177e4SLinus Torvalds if (ypcm == NULL) 10081da177e4SLinus Torvalds return -ENOMEM; 10091da177e4SLinus Torvalds ypcm->chip = chip; 10101da177e4SLinus Torvalds ypcm->type = capture_bank_number + CAPTURE_REC; 10111da177e4SLinus Torvalds ypcm->substream = substream; 10121da177e4SLinus Torvalds ypcm->capture_bank_number = capture_bank_number; 10131da177e4SLinus Torvalds chip->capture_substream[capture_bank_number] = substream; 10141da177e4SLinus Torvalds runtime->hw = snd_ymfpci_capture; 10151da177e4SLinus Torvalds /* FIXME? True value is 256/48 = 5.33333 ms */ 10161da177e4SLinus Torvalds snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX); 10171da177e4SLinus Torvalds runtime->private_data = ypcm; 10181da177e4SLinus Torvalds runtime->private_free = snd_ymfpci_pcm_free_substream; 10191da177e4SLinus Torvalds snd_ymfpci_hw_start(chip); 10201da177e4SLinus Torvalds return 0; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 1023208a1b4cSTakashi Iwai static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream) 10241da177e4SLinus Torvalds { 10251da177e4SLinus Torvalds return snd_ymfpci_capture_open(substream, 0); 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 1028208a1b4cSTakashi Iwai static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds return snd_ymfpci_capture_open(substream, 1); 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 1033208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream) 10341da177e4SLinus Torvalds { 10351da177e4SLinus Torvalds return 0; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream) 10391da177e4SLinus Torvalds { 1040208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 1041208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; 1042208a1b4cSTakashi Iwai struct snd_kcontrol *kctl; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 10451da177e4SLinus Torvalds if (ypcm->output_rear && chip->rear_opened > 0) { 10461da177e4SLinus Torvalds chip->rear_opened--; 10471da177e4SLinus Torvalds ymfpci_close_extension(chip); 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 10509bcf6551SClemens Ladisch kctl = chip->pcm_mixer[substream->number].ctl; 10519bcf6551SClemens Ladisch kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; 10529bcf6551SClemens Ladisch snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id); 10531da177e4SLinus Torvalds return snd_ymfpci_playback_close_1(substream); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 1056208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream) 10571da177e4SLinus Torvalds { 1058208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 10611da177e4SLinus Torvalds chip->spdif_opened = 0; 10621da177e4SLinus Torvalds ymfpci_close_extension(chip); 10631da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 10641da177e4SLinus Torvalds snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2); 10651da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); 10661da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 10671da177e4SLinus Torvalds chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; 10681da177e4SLinus Torvalds snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE | 10691da177e4SLinus Torvalds SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id); 10701da177e4SLinus Torvalds return snd_ymfpci_playback_close_1(substream); 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 1073208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream) 10741da177e4SLinus Torvalds { 1075208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 10781da177e4SLinus Torvalds if (chip->rear_opened > 0) { 10791da177e4SLinus Torvalds chip->rear_opened--; 10801da177e4SLinus Torvalds ymfpci_close_extension(chip); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 10831da177e4SLinus Torvalds return snd_ymfpci_playback_close_1(substream); 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 1086208a1b4cSTakashi Iwai static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream) 10871da177e4SLinus Torvalds { 1088208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); 1089208a1b4cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 1090208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = runtime->private_data; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds if (ypcm != NULL) { 10931da177e4SLinus Torvalds chip->capture_substream[ypcm->capture_bank_number] = NULL; 10941da177e4SLinus Torvalds snd_ymfpci_hw_stop(chip); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds return 0; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 1099208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_ops = { 11001da177e4SLinus Torvalds .open = snd_ymfpci_playback_open, 11011da177e4SLinus Torvalds .close = snd_ymfpci_playback_close, 11021da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 11031da177e4SLinus Torvalds .hw_params = snd_ymfpci_playback_hw_params, 11041da177e4SLinus Torvalds .hw_free = snd_ymfpci_playback_hw_free, 11051da177e4SLinus Torvalds .prepare = snd_ymfpci_playback_prepare, 11061da177e4SLinus Torvalds .trigger = snd_ymfpci_playback_trigger, 11071da177e4SLinus Torvalds .pointer = snd_ymfpci_playback_pointer, 11081da177e4SLinus Torvalds }; 11091da177e4SLinus Torvalds 1110208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = { 11111da177e4SLinus Torvalds .open = snd_ymfpci_capture_rec_open, 11121da177e4SLinus Torvalds .close = snd_ymfpci_capture_close, 11131da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 11141da177e4SLinus Torvalds .hw_params = snd_ymfpci_capture_hw_params, 11151da177e4SLinus Torvalds .hw_free = snd_ymfpci_capture_hw_free, 11161da177e4SLinus Torvalds .prepare = snd_ymfpci_capture_prepare, 11171da177e4SLinus Torvalds .trigger = snd_ymfpci_capture_trigger, 11181da177e4SLinus Torvalds .pointer = snd_ymfpci_capture_pointer, 11191da177e4SLinus Torvalds }; 11201da177e4SLinus Torvalds 1121208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) 11221da177e4SLinus Torvalds { 1123208a1b4cSTakashi Iwai struct snd_pcm *pcm; 11241da177e4SLinus Torvalds int err; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds if (rpcm) 11271da177e4SLinus Torvalds *rpcm = NULL; 11281da177e4SLinus Torvalds if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0) 11291da177e4SLinus Torvalds return err; 11301da177e4SLinus Torvalds pcm->private_data = chip; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops); 11331da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds /* global setup */ 11361da177e4SLinus Torvalds pcm->info_flags = 0; 11371da177e4SLinus Torvalds strcpy(pcm->name, "YMFPCI"); 11381da177e4SLinus Torvalds chip->pcm = pcm; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 11411da177e4SLinus Torvalds snd_dma_pci_data(chip->pci), 64*1024, 256*1024); 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds if (rpcm) 11441da177e4SLinus Torvalds *rpcm = pcm; 11451da177e4SLinus Torvalds return 0; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 1148208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = { 11491da177e4SLinus Torvalds .open = snd_ymfpci_capture_ac97_open, 11501da177e4SLinus Torvalds .close = snd_ymfpci_capture_close, 11511da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 11521da177e4SLinus Torvalds .hw_params = snd_ymfpci_capture_hw_params, 11531da177e4SLinus Torvalds .hw_free = snd_ymfpci_capture_hw_free, 11541da177e4SLinus Torvalds .prepare = snd_ymfpci_capture_prepare, 11551da177e4SLinus Torvalds .trigger = snd_ymfpci_capture_trigger, 11561da177e4SLinus Torvalds .pointer = snd_ymfpci_capture_pointer, 11571da177e4SLinus Torvalds }; 11581da177e4SLinus Torvalds 1159208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) 11601da177e4SLinus Torvalds { 1161208a1b4cSTakashi Iwai struct snd_pcm *pcm; 11621da177e4SLinus Torvalds int err; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds if (rpcm) 11651da177e4SLinus Torvalds *rpcm = NULL; 11661da177e4SLinus Torvalds if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0) 11671da177e4SLinus Torvalds return err; 11681da177e4SLinus Torvalds pcm->private_data = chip; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops); 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds /* global setup */ 11731da177e4SLinus Torvalds pcm->info_flags = 0; 11741da177e4SLinus Torvalds sprintf(pcm->name, "YMFPCI - %s", 11751da177e4SLinus Torvalds chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97"); 11761da177e4SLinus Torvalds chip->pcm2 = pcm; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 11791da177e4SLinus Torvalds snd_dma_pci_data(chip->pci), 64*1024, 256*1024); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if (rpcm) 11821da177e4SLinus Torvalds *rpcm = pcm; 11831da177e4SLinus Torvalds return 0; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 1186208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = { 11871da177e4SLinus Torvalds .open = snd_ymfpci_playback_spdif_open, 11881da177e4SLinus Torvalds .close = snd_ymfpci_playback_spdif_close, 11891da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 11901da177e4SLinus Torvalds .hw_params = snd_ymfpci_playback_hw_params, 11911da177e4SLinus Torvalds .hw_free = snd_ymfpci_playback_hw_free, 11921da177e4SLinus Torvalds .prepare = snd_ymfpci_playback_prepare, 11931da177e4SLinus Torvalds .trigger = snd_ymfpci_playback_trigger, 11941da177e4SLinus Torvalds .pointer = snd_ymfpci_playback_pointer, 11951da177e4SLinus Torvalds }; 11961da177e4SLinus Torvalds 1197208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) 11981da177e4SLinus Torvalds { 1199208a1b4cSTakashi Iwai struct snd_pcm *pcm; 12001da177e4SLinus Torvalds int err; 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds if (rpcm) 12031da177e4SLinus Torvalds *rpcm = NULL; 12041da177e4SLinus Torvalds if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0) 12051da177e4SLinus Torvalds return err; 12061da177e4SLinus Torvalds pcm->private_data = chip; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops); 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* global setup */ 12111da177e4SLinus Torvalds pcm->info_flags = 0; 12121da177e4SLinus Torvalds strcpy(pcm->name, "YMFPCI - IEC958"); 12131da177e4SLinus Torvalds chip->pcm_spdif = pcm; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 12161da177e4SLinus Torvalds snd_dma_pci_data(chip->pci), 64*1024, 256*1024); 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (rpcm) 12191da177e4SLinus Torvalds *rpcm = pcm; 12201da177e4SLinus Torvalds return 0; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 1223208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = { 12241da177e4SLinus Torvalds .open = snd_ymfpci_playback_4ch_open, 12251da177e4SLinus Torvalds .close = snd_ymfpci_playback_4ch_close, 12261da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 12271da177e4SLinus Torvalds .hw_params = snd_ymfpci_playback_hw_params, 12281da177e4SLinus Torvalds .hw_free = snd_ymfpci_playback_hw_free, 12291da177e4SLinus Torvalds .prepare = snd_ymfpci_playback_prepare, 12301da177e4SLinus Torvalds .trigger = snd_ymfpci_playback_trigger, 12311da177e4SLinus Torvalds .pointer = snd_ymfpci_playback_pointer, 12321da177e4SLinus Torvalds }; 12331da177e4SLinus Torvalds 1234208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) 12351da177e4SLinus Torvalds { 1236208a1b4cSTakashi Iwai struct snd_pcm *pcm; 12371da177e4SLinus Torvalds int err; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds if (rpcm) 12401da177e4SLinus Torvalds *rpcm = NULL; 12411da177e4SLinus Torvalds if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0) 12421da177e4SLinus Torvalds return err; 12431da177e4SLinus Torvalds pcm->private_data = chip; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds /* global setup */ 12481da177e4SLinus Torvalds pcm->info_flags = 0; 12491da177e4SLinus Torvalds strcpy(pcm->name, "YMFPCI - Rear PCM"); 12501da177e4SLinus Torvalds chip->pcm_4ch = pcm; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 12531da177e4SLinus Torvalds snd_dma_pci_data(chip->pci), 64*1024, 256*1024); 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds if (rpcm) 12561da177e4SLinus Torvalds *rpcm = pcm; 12571da177e4SLinus Torvalds return 0; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 1260208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 12611da177e4SLinus Torvalds { 12621da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 12631da177e4SLinus Torvalds uinfo->count = 1; 12641da177e4SLinus Torvalds return 0; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 1267208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol, 1268208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 12691da177e4SLinus Torvalds { 1270208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 12731da177e4SLinus Torvalds ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff; 12741da177e4SLinus Torvalds ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff; 1275fc80a202SClemens Ladisch ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; 12761da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 12771da177e4SLinus Torvalds return 0; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 1280208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol, 1281208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 12821da177e4SLinus Torvalds { 1283208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 12841da177e4SLinus Torvalds unsigned int val; 12851da177e4SLinus Torvalds int change; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) | 12881da177e4SLinus Torvalds (ucontrol->value.iec958.status[1] << 8); 12891da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 12901da177e4SLinus Torvalds change = chip->spdif_bits != val; 12911da177e4SLinus Torvalds chip->spdif_bits = val; 12921da177e4SLinus Torvalds if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL) 12931da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); 12941da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 12951da177e4SLinus Torvalds return change; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 1298208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata = 12991da177e4SLinus Torvalds { 13001da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_PCM, 13011da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), 13021da177e4SLinus Torvalds .info = snd_ymfpci_spdif_default_info, 13031da177e4SLinus Torvalds .get = snd_ymfpci_spdif_default_get, 13041da177e4SLinus Torvalds .put = snd_ymfpci_spdif_default_put 13051da177e4SLinus Torvalds }; 13061da177e4SLinus Torvalds 1307208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 13101da177e4SLinus Torvalds uinfo->count = 1; 13111da177e4SLinus Torvalds return 0; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 1314208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol, 1315208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 13161da177e4SLinus Torvalds { 1317208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 13201da177e4SLinus Torvalds ucontrol->value.iec958.status[0] = 0x3e; 13211da177e4SLinus Torvalds ucontrol->value.iec958.status[1] = 0xff; 13221da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 1326208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata = 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, 13291da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_PCM, 13301da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), 13311da177e4SLinus Torvalds .info = snd_ymfpci_spdif_mask_info, 13321da177e4SLinus Torvalds .get = snd_ymfpci_spdif_mask_get, 13331da177e4SLinus Torvalds }; 13341da177e4SLinus Torvalds 1335208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 13361da177e4SLinus Torvalds { 13371da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 13381da177e4SLinus Torvalds uinfo->count = 1; 13391da177e4SLinus Torvalds return 0; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 1342208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol, 1343208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 13441da177e4SLinus Torvalds { 1345208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 13481da177e4SLinus Torvalds ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff; 13491da177e4SLinus Torvalds ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff; 1350fc80a202SClemens Ladisch ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; 13511da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 13521da177e4SLinus Torvalds return 0; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol, 1356208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 13571da177e4SLinus Torvalds { 1358208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 13591da177e4SLinus Torvalds unsigned int val; 13601da177e4SLinus Torvalds int change; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) | 13631da177e4SLinus Torvalds (ucontrol->value.iec958.status[1] << 8); 13641da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 13651da177e4SLinus Torvalds change = chip->spdif_pcm_bits != val; 13661da177e4SLinus Torvalds chip->spdif_pcm_bits = val; 13671da177e4SLinus Torvalds if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2)) 13681da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits); 13691da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 13701da177e4SLinus Torvalds return change; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 1373208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata = 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE, 13761da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_PCM, 13771da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM), 13781da177e4SLinus Torvalds .info = snd_ymfpci_spdif_stream_info, 13791da177e4SLinus Torvalds .get = snd_ymfpci_spdif_stream_get, 13801da177e4SLinus Torvalds .put = snd_ymfpci_spdif_stream_put 13811da177e4SLinus Torvalds }; 13821da177e4SLinus Torvalds 1383208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds static char *texts[3] = {"AC'97", "IEC958", "ZV Port"}; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 13881da177e4SLinus Torvalds info->count = 1; 13891da177e4SLinus Torvalds info->value.enumerated.items = 3; 13901da177e4SLinus Torvalds if (info->value.enumerated.item > 2) 13911da177e4SLinus Torvalds info->value.enumerated.item = 2; 13921da177e4SLinus Torvalds strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]); 13931da177e4SLinus Torvalds return 0; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 1396208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value) 13971da177e4SLinus Torvalds { 1398208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 13991da177e4SLinus Torvalds u16 reg; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 14021da177e4SLinus Torvalds reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); 14031da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 14041da177e4SLinus Torvalds if (!(reg & 0x100)) 14051da177e4SLinus Torvalds value->value.enumerated.item[0] = 0; 14061da177e4SLinus Torvalds else 14071da177e4SLinus Torvalds value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0); 14081da177e4SLinus Torvalds return 0; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 1411208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value) 14121da177e4SLinus Torvalds { 1413208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 14141da177e4SLinus Torvalds u16 reg, old_reg; 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 14171da177e4SLinus Torvalds old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); 14181da177e4SLinus Torvalds if (value->value.enumerated.item[0] == 0) 14191da177e4SLinus Torvalds reg = old_reg & ~0x100; 14201da177e4SLinus Torvalds else 14211da177e4SLinus Torvalds reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9); 14221da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg); 14231da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 14241da177e4SLinus Torvalds return reg != old_reg; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 1427208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = { 14281da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 14291da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 14301da177e4SLinus Torvalds .name = "Direct Recording Source", 14311da177e4SLinus Torvalds .info = snd_ymfpci_drec_source_info, 14321da177e4SLinus Torvalds .get = snd_ymfpci_drec_source_get, 14331da177e4SLinus Torvalds .put = snd_ymfpci_drec_source_put 14341da177e4SLinus Torvalds }; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds /* 14371da177e4SLinus Torvalds * Mixer controls 14381da177e4SLinus Torvalds */ 14391da177e4SLinus Torvalds 1440d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \ 14411da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 14421da177e4SLinus Torvalds .info = snd_ymfpci_info_single, \ 14431da177e4SLinus Torvalds .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \ 1444d602c885SGlen Masgai .private_value = ((reg) | ((shift) << 16)) } 14451da177e4SLinus Torvalds 1446208a1b4cSTakashi Iwai static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol, 1447208a1b4cSTakashi Iwai struct snd_ctl_elem_info *uinfo) 14481da177e4SLinus Torvalds { 1449d602c885SGlen Masgai int reg = kcontrol->private_value & 0xffff; 1450d602c885SGlen Masgai 1451d602c885SGlen Masgai switch (reg) { 14521da177e4SLinus Torvalds case YDSXGR_SPDIFOUTCTRL: break; 14531da177e4SLinus Torvalds case YDSXGR_SPDIFINCTRL: break; 14541da177e4SLinus Torvalds default: return -EINVAL; 14551da177e4SLinus Torvalds } 1456467a8c2fSAdrian Bunk uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 14571da177e4SLinus Torvalds uinfo->count = 1; 14581da177e4SLinus Torvalds uinfo->value.integer.min = 0; 1459467a8c2fSAdrian Bunk uinfo->value.integer.max = 1; 14601da177e4SLinus Torvalds return 0; 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 1463208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol, 1464208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 14651da177e4SLinus Torvalds { 1466208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 1467d602c885SGlen Masgai int reg = kcontrol->private_value & 0xffff; 1468d602c885SGlen Masgai unsigned int shift = (kcontrol->private_value >> 16) & 0xff; 1469d602c885SGlen Masgai unsigned int mask = 1; 14701da177e4SLinus Torvalds 1471d602c885SGlen Masgai switch (reg) { 14721da177e4SLinus Torvalds case YDSXGR_SPDIFOUTCTRL: break; 14731da177e4SLinus Torvalds case YDSXGR_SPDIFINCTRL: break; 14741da177e4SLinus Torvalds default: return -EINVAL; 14751da177e4SLinus Torvalds } 1476d602c885SGlen Masgai ucontrol->value.integer.value[0] = 1477d602c885SGlen Masgai (snd_ymfpci_readl(chip, reg) >> shift) & mask; 14781da177e4SLinus Torvalds return 0; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 1481208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol, 1482208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 14831da177e4SLinus Torvalds { 1484208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 1485d602c885SGlen Masgai int reg = kcontrol->private_value & 0xffff; 1486d602c885SGlen Masgai unsigned int shift = (kcontrol->private_value >> 16) & 0xff; 1487d602c885SGlen Masgai unsigned int mask = 1; 14881da177e4SLinus Torvalds int change; 14891da177e4SLinus Torvalds unsigned int val, oval; 14901da177e4SLinus Torvalds 1491d602c885SGlen Masgai switch (reg) { 14921da177e4SLinus Torvalds case YDSXGR_SPDIFOUTCTRL: break; 14931da177e4SLinus Torvalds case YDSXGR_SPDIFINCTRL: break; 14941da177e4SLinus Torvalds default: return -EINVAL; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds val = (ucontrol->value.integer.value[0] & mask); 14971da177e4SLinus Torvalds val <<= shift; 14981da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 14991da177e4SLinus Torvalds oval = snd_ymfpci_readl(chip, reg); 15001da177e4SLinus Torvalds val = (oval & ~(mask << shift)) | val; 15011da177e4SLinus Torvalds change = val != oval; 15021da177e4SLinus Torvalds snd_ymfpci_writel(chip, reg, val); 15031da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 15041da177e4SLinus Torvalds return change; 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15070cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0); 150833925186STakashi Iwai 15091da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \ 15101da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 151133925186STakashi Iwai .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ 15121da177e4SLinus Torvalds .info = snd_ymfpci_info_double, \ 15131da177e4SLinus Torvalds .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \ 151433925186STakashi Iwai .private_value = reg, \ 151533925186STakashi Iwai .tlv = { .p = db_scale_native } } 15161da177e4SLinus Torvalds 1517208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 15181da177e4SLinus Torvalds { 15191da177e4SLinus Torvalds unsigned int reg = kcontrol->private_value; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds if (reg < 0x80 || reg >= 0xc0) 15221da177e4SLinus Torvalds return -EINVAL; 1523467a8c2fSAdrian Bunk uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 15241da177e4SLinus Torvalds uinfo->count = 2; 15251da177e4SLinus Torvalds uinfo->value.integer.min = 0; 1526467a8c2fSAdrian Bunk uinfo->value.integer.max = 16383; 15271da177e4SLinus Torvalds return 0; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 15311da177e4SLinus Torvalds { 1532208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 15331da177e4SLinus Torvalds unsigned int reg = kcontrol->private_value; 1534467a8c2fSAdrian Bunk unsigned int shift_left = 0, shift_right = 16, mask = 16383; 15351da177e4SLinus Torvalds unsigned int val; 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds if (reg < 0x80 || reg >= 0xc0) 15381da177e4SLinus Torvalds return -EINVAL; 15391da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 15401da177e4SLinus Torvalds val = snd_ymfpci_readl(chip, reg); 15411da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 15421da177e4SLinus Torvalds ucontrol->value.integer.value[0] = (val >> shift_left) & mask; 15431da177e4SLinus Torvalds ucontrol->value.integer.value[1] = (val >> shift_right) & mask; 15441da177e4SLinus Torvalds return 0; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 1547208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 15481da177e4SLinus Torvalds { 1549208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 15501da177e4SLinus Torvalds unsigned int reg = kcontrol->private_value; 1551467a8c2fSAdrian Bunk unsigned int shift_left = 0, shift_right = 16, mask = 16383; 15521da177e4SLinus Torvalds int change; 15531da177e4SLinus Torvalds unsigned int val1, val2, oval; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds if (reg < 0x80 || reg >= 0xc0) 15561da177e4SLinus Torvalds return -EINVAL; 15571da177e4SLinus Torvalds val1 = ucontrol->value.integer.value[0] & mask; 15581da177e4SLinus Torvalds val2 = ucontrol->value.integer.value[1] & mask; 15591da177e4SLinus Torvalds val1 <<= shift_left; 15601da177e4SLinus Torvalds val2 <<= shift_right; 15611da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 15621da177e4SLinus Torvalds oval = snd_ymfpci_readl(chip, reg); 15631da177e4SLinus Torvalds val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; 15641da177e4SLinus Torvalds change = val1 != oval; 15651da177e4SLinus Torvalds snd_ymfpci_writel(chip, reg, val1); 15661da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 15671da177e4SLinus Torvalds return change; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds /* 15711da177e4SLinus Torvalds * 4ch duplication 15721da177e4SLinus Torvalds */ 1573208a1b4cSTakashi Iwai static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 15741da177e4SLinus Torvalds { 15751da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 15761da177e4SLinus Torvalds uinfo->count = 1; 15771da177e4SLinus Torvalds uinfo->value.integer.min = 0; 15781da177e4SLinus Torvalds uinfo->value.integer.max = 1; 15791da177e4SLinus Torvalds return 0; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 1582208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 15831da177e4SLinus Torvalds { 1584208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 15851da177e4SLinus Torvalds ucontrol->value.integer.value[0] = chip->mode_dup4ch; 15861da177e4SLinus Torvalds return 0; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 1589208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 15901da177e4SLinus Torvalds { 1591208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 15921da177e4SLinus Torvalds int change; 15931da177e4SLinus Torvalds change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch); 15941da177e4SLinus Torvalds if (change) 15951da177e4SLinus Torvalds chip->mode_dup4ch = !!ucontrol->value.integer.value[0]; 15961da177e4SLinus Torvalds return change; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds 1600208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = { 16011da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL), 16021da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL), 16031da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL), 16041da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL), 16051da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL), 16061da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL), 16071da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL), 16081da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL), 16091da177e4SLinus Torvalds YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL), 16101da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL), 16111da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL), 16121da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL), 16131da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL), 1614d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0), 1615d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0), 1616d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4), 16171da177e4SLinus Torvalds { 16181da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 16191da177e4SLinus Torvalds .name = "4ch Duplication", 16201da177e4SLinus Torvalds .info = snd_ymfpci_info_dup4ch, 16211da177e4SLinus Torvalds .get = snd_ymfpci_get_dup4ch, 16221da177e4SLinus Torvalds .put = snd_ymfpci_put_dup4ch, 16231da177e4SLinus Torvalds }, 16241da177e4SLinus Torvalds }; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * GPIO 16291da177e4SLinus Torvalds */ 16301da177e4SLinus Torvalds 1631208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds u16 reg, mode; 16341da177e4SLinus Torvalds unsigned long flags; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 16371da177e4SLinus Torvalds reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE); 16381da177e4SLinus Torvalds reg &= ~(1 << (pin + 8)); 16391da177e4SLinus Torvalds reg |= (1 << pin); 16401da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg); 16411da177e4SLinus Torvalds /* set the level mode for input line */ 16421da177e4SLinus Torvalds mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG); 16431da177e4SLinus Torvalds mode &= ~(3 << (pin * 2)); 16441da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode); 16451da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8))); 16461da177e4SLinus Torvalds mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS); 16471da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 16481da177e4SLinus Torvalds return (mode >> pin) & 1; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 1651208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable) 16521da177e4SLinus Torvalds { 16531da177e4SLinus Torvalds u16 reg; 16541da177e4SLinus Torvalds unsigned long flags; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 16571da177e4SLinus Torvalds reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE); 16581da177e4SLinus Torvalds reg &= ~(1 << pin); 16591da177e4SLinus Torvalds reg &= ~(1 << (pin + 8)); 16601da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg); 16611da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin); 16621da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8))); 16631da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds return 0; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 16711da177e4SLinus Torvalds uinfo->count = 1; 16721da177e4SLinus Torvalds uinfo->value.integer.min = 0; 16731da177e4SLinus Torvalds uinfo->value.integer.max = 1; 16741da177e4SLinus Torvalds return 0; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 1677208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 16781da177e4SLinus Torvalds { 1679208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 16801da177e4SLinus Torvalds int pin = (int)kcontrol->private_value; 16811da177e4SLinus Torvalds ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin); 16821da177e4SLinus Torvalds return 0; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 1685208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 16861da177e4SLinus Torvalds { 1687208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 16881da177e4SLinus Torvalds int pin = (int)kcontrol->private_value; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) { 16911da177e4SLinus Torvalds snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]); 16921da177e4SLinus Torvalds ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin); 16931da177e4SLinus Torvalds return 1; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds return 0; 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 1698208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = { 16991da177e4SLinus Torvalds .name = "Shared Rear/Line-In Switch", 17001da177e4SLinus Torvalds .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 17011da177e4SLinus Torvalds .info = snd_ymfpci_gpio_sw_info, 17021da177e4SLinus Torvalds .get = snd_ymfpci_gpio_sw_get, 17031da177e4SLinus Torvalds .put = snd_ymfpci_gpio_sw_put, 17041da177e4SLinus Torvalds .private_value = 2, 17051da177e4SLinus Torvalds }; 17061da177e4SLinus Torvalds 17079bcf6551SClemens Ladisch /* 17089bcf6551SClemens Ladisch * PCM voice volume 17099bcf6551SClemens Ladisch */ 17109bcf6551SClemens Ladisch 1711208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol, 1712208a1b4cSTakashi Iwai struct snd_ctl_elem_info *uinfo) 17139bcf6551SClemens Ladisch { 17149bcf6551SClemens Ladisch uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 17159bcf6551SClemens Ladisch uinfo->count = 2; 17169bcf6551SClemens Ladisch uinfo->value.integer.min = 0; 17179bcf6551SClemens Ladisch uinfo->value.integer.max = 0x8000; 17189bcf6551SClemens Ladisch return 0; 17199bcf6551SClemens Ladisch } 17209bcf6551SClemens Ladisch 1721208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol, 1722208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 17239bcf6551SClemens Ladisch { 1724208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 17259bcf6551SClemens Ladisch unsigned int subs = kcontrol->id.subdevice; 17269bcf6551SClemens Ladisch 17279bcf6551SClemens Ladisch ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left; 17289bcf6551SClemens Ladisch ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right; 17299bcf6551SClemens Ladisch return 0; 17309bcf6551SClemens Ladisch } 17319bcf6551SClemens Ladisch 1732208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol, 1733208a1b4cSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 17349bcf6551SClemens Ladisch { 1735208a1b4cSTakashi Iwai struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); 17369bcf6551SClemens Ladisch unsigned int subs = kcontrol->id.subdevice; 1737208a1b4cSTakashi Iwai struct snd_pcm_substream *substream; 17389bcf6551SClemens Ladisch unsigned long flags; 17399bcf6551SClemens Ladisch 17409bcf6551SClemens Ladisch if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left || 17419bcf6551SClemens Ladisch ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) { 17429bcf6551SClemens Ladisch chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0]; 17439bcf6551SClemens Ladisch chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1]; 17449bcf6551SClemens Ladisch 1745208a1b4cSTakashi Iwai substream = (struct snd_pcm_substream *)kcontrol->private_value; 17469bcf6551SClemens Ladisch spin_lock_irqsave(&chip->voice_lock, flags); 17479bcf6551SClemens Ladisch if (substream->runtime && substream->runtime->private_data) { 1748208a1b4cSTakashi Iwai struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; 17499ed1261eSTeru KAMOGASHIRA if (!ypcm->use_441_slot) 17509bcf6551SClemens Ladisch ypcm->update_pcm_vol = 2; 17519ed1261eSTeru KAMOGASHIRA else 17529ed1261eSTeru KAMOGASHIRA snd_ymfpci_pcm_441_volume_set(ypcm); 17539bcf6551SClemens Ladisch } 17549bcf6551SClemens Ladisch spin_unlock_irqrestore(&chip->voice_lock, flags); 17559bcf6551SClemens Ladisch return 1; 17569bcf6551SClemens Ladisch } 17579bcf6551SClemens Ladisch return 0; 17589bcf6551SClemens Ladisch } 17599bcf6551SClemens Ladisch 1760208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = { 17619bcf6551SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_PCM, 17629bcf6551SClemens Ladisch .name = "PCM Playback Volume", 17639bcf6551SClemens Ladisch .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 17649bcf6551SClemens Ladisch SNDRV_CTL_ELEM_ACCESS_INACTIVE, 17659bcf6551SClemens Ladisch .info = snd_ymfpci_pcm_vol_info, 17669bcf6551SClemens Ladisch .get = snd_ymfpci_pcm_vol_get, 17679bcf6551SClemens Ladisch .put = snd_ymfpci_pcm_vol_put, 17689bcf6551SClemens Ladisch }; 17699bcf6551SClemens Ladisch 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * Mixer routines 17731da177e4SLinus Torvalds */ 17741da177e4SLinus Torvalds 1775208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus) 17761da177e4SLinus Torvalds { 1777208a1b4cSTakashi Iwai struct snd_ymfpci *chip = bus->private_data; 17781da177e4SLinus Torvalds chip->ac97_bus = NULL; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 1781208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97) 17821da177e4SLinus Torvalds { 1783208a1b4cSTakashi Iwai struct snd_ymfpci *chip = ac97->private_data; 17841da177e4SLinus Torvalds chip->ac97 = NULL; 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 1787d9301263SGlen Masgai int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch) 17881da177e4SLinus Torvalds { 1789208a1b4cSTakashi Iwai struct snd_ac97_template ac97; 1790208a1b4cSTakashi Iwai struct snd_kcontrol *kctl; 1791208a1b4cSTakashi Iwai struct snd_pcm_substream *substream; 17921da177e4SLinus Torvalds unsigned int idx; 17931da177e4SLinus Torvalds int err; 1794208a1b4cSTakashi Iwai static struct snd_ac97_bus_ops ops = { 17951da177e4SLinus Torvalds .write = snd_ymfpci_codec_write, 17961da177e4SLinus Torvalds .read = snd_ymfpci_codec_read, 17971da177e4SLinus Torvalds }; 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0) 18001da177e4SLinus Torvalds return err; 18011da177e4SLinus Torvalds chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus; 18021da177e4SLinus Torvalds chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */ 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds memset(&ac97, 0, sizeof(ac97)); 18051da177e4SLinus Torvalds ac97.private_data = chip; 18061da177e4SLinus Torvalds ac97.private_free = snd_ymfpci_mixer_free_ac97; 18071da177e4SLinus Torvalds if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0) 18081da177e4SLinus Torvalds return err; 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds /* to be sure */ 18111da177e4SLinus Torvalds snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 18121da177e4SLinus Torvalds AC97_EA_VRA|AC97_EA_VRM, 0); 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) { 18151da177e4SLinus Torvalds if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0) 18161da177e4SLinus Torvalds return err; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds /* add S/PDIF control */ 18201da177e4SLinus Torvalds snd_assert(chip->pcm_spdif != NULL, return -EIO); 18211da177e4SLinus Torvalds if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0) 18221da177e4SLinus Torvalds return err; 18231da177e4SLinus Torvalds kctl->id.device = chip->pcm_spdif->device; 18241da177e4SLinus Torvalds if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0) 18251da177e4SLinus Torvalds return err; 18261da177e4SLinus Torvalds kctl->id.device = chip->pcm_spdif->device; 18271da177e4SLinus Torvalds if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0) 18281da177e4SLinus Torvalds return err; 18291da177e4SLinus Torvalds kctl->id.device = chip->pcm_spdif->device; 18301da177e4SLinus Torvalds chip->spdif_pcm_ctl = kctl; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* direct recording source */ 18331da177e4SLinus Torvalds if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 && 18341da177e4SLinus Torvalds (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0) 18351da177e4SLinus Torvalds return err; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds /* 18381da177e4SLinus Torvalds * shared rear/line-in 18391da177e4SLinus Torvalds */ 18401da177e4SLinus Torvalds if (rear_switch) { 18411da177e4SLinus Torvalds if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0) 18421da177e4SLinus Torvalds return err; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 18459bcf6551SClemens Ladisch /* per-voice volume */ 18469bcf6551SClemens Ladisch substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 18479bcf6551SClemens Ladisch for (idx = 0; idx < 32; ++idx) { 18489bcf6551SClemens Ladisch kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip); 18499bcf6551SClemens Ladisch if (!kctl) 18509bcf6551SClemens Ladisch return -ENOMEM; 18519bcf6551SClemens Ladisch kctl->id.device = chip->pcm->device; 18529bcf6551SClemens Ladisch kctl->id.subdevice = idx; 18539bcf6551SClemens Ladisch kctl->private_value = (unsigned long)substream; 18549bcf6551SClemens Ladisch if ((err = snd_ctl_add(chip->card, kctl)) < 0) 18559bcf6551SClemens Ladisch return err; 18569bcf6551SClemens Ladisch chip->pcm_mixer[idx].left = 0x8000; 18579bcf6551SClemens Ladisch chip->pcm_mixer[idx].right = 0x8000; 18589bcf6551SClemens Ladisch chip->pcm_mixer[idx].ctl = kctl; 18599bcf6551SClemens Ladisch substream = substream->next; 18609bcf6551SClemens Ladisch } 18619bcf6551SClemens Ladisch 18621da177e4SLinus Torvalds return 0; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds /* 18671da177e4SLinus Torvalds * timer 18681da177e4SLinus Torvalds */ 18691da177e4SLinus Torvalds 1870208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer) 18711da177e4SLinus Torvalds { 1872208a1b4cSTakashi Iwai struct snd_ymfpci *chip; 18731da177e4SLinus Torvalds unsigned long flags; 18741da177e4SLinus Torvalds unsigned int count; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds chip = snd_timer_chip(timer); 1877d44c39acSClemens Ladisch count = (timer->sticks << 1) - 1; 18781da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 18791da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count); 18801da177e4SLinus Torvalds snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03); 18811da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 18821da177e4SLinus Torvalds return 0; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 1885208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer) 18861da177e4SLinus Torvalds { 1887208a1b4cSTakashi Iwai struct snd_ymfpci *chip; 18881da177e4SLinus Torvalds unsigned long flags; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds chip = snd_timer_chip(timer); 18911da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 18921da177e4SLinus Torvalds snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00); 18931da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 18941da177e4SLinus Torvalds return 0; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 1897208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer, 18981da177e4SLinus Torvalds unsigned long *num, unsigned long *den) 18991da177e4SLinus Torvalds { 19001da177e4SLinus Torvalds *num = 1; 1901d44c39acSClemens Ladisch *den = 48000; 19021da177e4SLinus Torvalds return 0; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 1905208a1b4cSTakashi Iwai static struct snd_timer_hardware snd_ymfpci_timer_hw = { 19061da177e4SLinus Torvalds .flags = SNDRV_TIMER_HW_AUTO, 1907d44c39acSClemens Ladisch .resolution = 20833, /* 1/fs = 20.8333...us */ 1908d44c39acSClemens Ladisch .ticks = 0x8000, 19091da177e4SLinus Torvalds .start = snd_ymfpci_timer_start, 19101da177e4SLinus Torvalds .stop = snd_ymfpci_timer_stop, 19111da177e4SLinus Torvalds .precise_resolution = snd_ymfpci_timer_precise_resolution, 19121da177e4SLinus Torvalds }; 19131da177e4SLinus Torvalds 1914208a1b4cSTakashi Iwai int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device) 19151da177e4SLinus Torvalds { 1916208a1b4cSTakashi Iwai struct snd_timer *timer = NULL; 1917208a1b4cSTakashi Iwai struct snd_timer_id tid; 19181da177e4SLinus Torvalds int err; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds tid.dev_class = SNDRV_TIMER_CLASS_CARD; 19211da177e4SLinus Torvalds tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; 19221da177e4SLinus Torvalds tid.card = chip->card->number; 19231da177e4SLinus Torvalds tid.device = device; 19241da177e4SLinus Torvalds tid.subdevice = 0; 19251da177e4SLinus Torvalds if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) { 19261da177e4SLinus Torvalds strcpy(timer->name, "YMFPCI timer"); 19271da177e4SLinus Torvalds timer->private_data = chip; 19281da177e4SLinus Torvalds timer->hw = snd_ymfpci_timer_hw; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds chip->timer = timer; 19311da177e4SLinus Torvalds return err; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* 19361da177e4SLinus Torvalds * proc interface 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds 1939208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry, 1940208a1b4cSTakashi Iwai struct snd_info_buffer *buffer) 19411da177e4SLinus Torvalds { 1942208a1b4cSTakashi Iwai struct snd_ymfpci *chip = entry->private_data; 19431da177e4SLinus Torvalds int i; 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds snd_iprintf(buffer, "YMFPCI\n\n"); 19461da177e4SLinus Torvalds for (i = 0; i <= YDSXGR_WORKBASE; i += 4) 19471da177e4SLinus Torvalds snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i)); 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 1950208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip) 19511da177e4SLinus Torvalds { 1952208a1b4cSTakashi Iwai struct snd_info_entry *entry; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds if (! snd_card_proc_new(card, "ymfpci", &entry)) 1955bf850204STakashi Iwai snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read); 19561da177e4SLinus Torvalds return 0; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds /* 19601da177e4SLinus Torvalds * initialization routines 19611da177e4SLinus Torvalds */ 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci) 19641da177e4SLinus Torvalds { 19651da177e4SLinus Torvalds u8 cmd; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd); 19681da177e4SLinus Torvalds #if 0 // force to reset 19691da177e4SLinus Torvalds if (cmd & 0x03) { 19701da177e4SLinus Torvalds #endif 19711da177e4SLinus Torvalds pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc); 19721da177e4SLinus Torvalds pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03); 19731da177e4SLinus Torvalds pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc); 19741da177e4SLinus Torvalds pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0); 19751da177e4SLinus Torvalds pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0); 19761da177e4SLinus Torvalds #if 0 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds #endif 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds 1981208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip) 19821da177e4SLinus Torvalds { 19831da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 1986208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip) 19871da177e4SLinus Torvalds { 19881da177e4SLinus Torvalds u32 val; 19891da177e4SLinus Torvalds int timeout = 1000; 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds val = snd_ymfpci_readl(chip, YDSXGR_CONFIG); 19921da177e4SLinus Torvalds if (val) 19931da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000); 19941da177e4SLinus Torvalds while (timeout-- > 0) { 19951da177e4SLinus Torvalds val = snd_ymfpci_readl(chip, YDSXGR_STATUS); 19961da177e4SLinus Torvalds if ((val & 0x00000002) == 0) 19971da177e4SLinus Torvalds break; 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 20018ad2da19STakashi Iwai #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL 2002102fa906SClemens Ladisch 20031da177e4SLinus Torvalds #include "ymfpci_image.h" 20041da177e4SLinus Torvalds 2005102fa906SClemens Ladisch static struct firmware snd_ymfpci_dsp_microcode = { 2006102fa906SClemens Ladisch .size = YDSXG_DSPLENGTH, 2007102fa906SClemens Ladisch .data = (u8 *)DspInst, 2008102fa906SClemens Ladisch }; 2009102fa906SClemens Ladisch static struct firmware snd_ymfpci_controller_microcode = { 2010102fa906SClemens Ladisch .size = YDSXG_CTRLLENGTH, 2011102fa906SClemens Ladisch .data = (u8 *)CntrlInst, 2012102fa906SClemens Ladisch }; 2013102fa906SClemens Ladisch static struct firmware snd_ymfpci_controller_1e_microcode = { 2014102fa906SClemens Ladisch .size = YDSXG_CTRLLENGTH, 2015102fa906SClemens Ladisch .data = (u8 *)CntrlInst1E, 2016102fa906SClemens Ladisch }; 2017102fa906SClemens Ladisch #endif 2018102fa906SClemens Ladisch 2019b7dd2b34STakashi Iwai #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL 2020b7dd2b34STakashi Iwai static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip) 2021b7dd2b34STakashi Iwai { 2022b7dd2b34STakashi Iwai chip->dsp_microcode = &snd_ymfpci_dsp_microcode; 2023b7dd2b34STakashi Iwai if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F || 2024b7dd2b34STakashi Iwai chip->device_id == PCI_DEVICE_ID_YAMAHA_740C || 2025b7dd2b34STakashi Iwai chip->device_id == PCI_DEVICE_ID_YAMAHA_744 || 2026b7dd2b34STakashi Iwai chip->device_id == PCI_DEVICE_ID_YAMAHA_754) 2027b7dd2b34STakashi Iwai chip->controller_microcode = 2028b7dd2b34STakashi Iwai &snd_ymfpci_controller_1e_microcode; 2029b7dd2b34STakashi Iwai else 2030b7dd2b34STakashi Iwai chip->controller_microcode = 2031b7dd2b34STakashi Iwai &snd_ymfpci_controller_microcode; 2032b7dd2b34STakashi Iwai return 0; 2033b7dd2b34STakashi Iwai } 2034b7dd2b34STakashi Iwai 2035b7dd2b34STakashi Iwai #else /* use fw_loader */ 2036b7dd2b34STakashi Iwai 2037102fa906SClemens Ladisch #ifdef __LITTLE_ENDIAN 2038102fa906SClemens Ladisch static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { } 2039102fa906SClemens Ladisch #else 2040102fa906SClemens Ladisch static void snd_ymfpci_convert_from_le(const struct firmware *fw) 2041102fa906SClemens Ladisch { 2042102fa906SClemens Ladisch int i; 2043102fa906SClemens Ladisch u32 *data = (u32 *)fw->data; 2044102fa906SClemens Ladisch 2045102fa906SClemens Ladisch for (i = 0; i < fw->size / 4; ++i) 2046102fa906SClemens Ladisch le32_to_cpus(&data[i]); 2047102fa906SClemens Ladisch } 2048102fa906SClemens Ladisch #endif 2049102fa906SClemens Ladisch 2050102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip) 2051102fa906SClemens Ladisch { 2052102fa906SClemens Ladisch int err, is_1e; 2053102fa906SClemens Ladisch const char *name; 2054102fa906SClemens Ladisch 2055102fa906SClemens Ladisch err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw", 2056102fa906SClemens Ladisch &chip->pci->dev); 2057102fa906SClemens Ladisch if (err >= 0) { 2058102fa906SClemens Ladisch if (chip->dsp_microcode->size == YDSXG_DSPLENGTH) 2059102fa906SClemens Ladisch snd_ymfpci_convert_from_le(chip->dsp_microcode); 2060102fa906SClemens Ladisch else { 2061102fa906SClemens Ladisch snd_printk(KERN_ERR "DSP microcode has wrong size\n"); 2062102fa906SClemens Ladisch err = -EINVAL; 2063102fa906SClemens Ladisch } 2064102fa906SClemens Ladisch } 2065b7dd2b34STakashi Iwai if (err < 0) 2066102fa906SClemens Ladisch return err; 2067102fa906SClemens Ladisch is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F || 2068102fa906SClemens Ladisch chip->device_id == PCI_DEVICE_ID_YAMAHA_740C || 2069102fa906SClemens Ladisch chip->device_id == PCI_DEVICE_ID_YAMAHA_744 || 2070102fa906SClemens Ladisch chip->device_id == PCI_DEVICE_ID_YAMAHA_754; 2071102fa906SClemens Ladisch name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw"; 2072102fa906SClemens Ladisch err = request_firmware(&chip->controller_microcode, name, 2073102fa906SClemens Ladisch &chip->pci->dev); 2074102fa906SClemens Ladisch if (err >= 0) { 2075102fa906SClemens Ladisch if (chip->controller_microcode->size == YDSXG_CTRLLENGTH) 2076102fa906SClemens Ladisch snd_ymfpci_convert_from_le(chip->controller_microcode); 2077102fa906SClemens Ladisch else { 2078102fa906SClemens Ladisch snd_printk(KERN_ERR "controller microcode" 2079102fa906SClemens Ladisch " has wrong size\n"); 2080102fa906SClemens Ladisch err = -EINVAL; 2081102fa906SClemens Ladisch } 2082102fa906SClemens Ladisch } 2083b7dd2b34STakashi Iwai if (err < 0) 2084102fa906SClemens Ladisch return err; 2085102fa906SClemens Ladisch return 0; 2086102fa906SClemens Ladisch } 2087*7e0af29dSClemens Ladisch 2088*7e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw"); 2089*7e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw"); 2090*7e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw"); 2091*7e0af29dSClemens Ladisch 2092b7dd2b34STakashi Iwai #endif 2093102fa906SClemens Ladisch 2094208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip) 20951da177e4SLinus Torvalds { 20961da177e4SLinus Torvalds int i; 20971da177e4SLinus Torvalds u16 ctrl; 2098102fa906SClemens Ladisch u32 *inst; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000); 21011da177e4SLinus Torvalds snd_ymfpci_disable_dsp(chip); 21021da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000); 21031da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000); 21041da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000); 21051da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000); 21061da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000); 21071da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000); 21081da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000); 21091da177e4SLinus Torvalds ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); 21101da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007); 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds /* setup DSP instruction code */ 2113102fa906SClemens Ladisch inst = (u32 *)chip->dsp_microcode->data; 21141da177e4SLinus Torvalds for (i = 0; i < YDSXG_DSPLENGTH / 4; i++) 2115102fa906SClemens Ladisch snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds /* setup control instruction code */ 2118102fa906SClemens Ladisch inst = (u32 *)chip->controller_microcode->data; 21191da177e4SLinus Torvalds for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++) 21201da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds snd_ymfpci_enable_dsp(chip); 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 2125208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip) 21261da177e4SLinus Torvalds { 21271da177e4SLinus Torvalds long size, playback_ctrl_size; 21281da177e4SLinus Torvalds int voice, bank, reg; 21291da177e4SLinus Torvalds u8 *ptr; 21301da177e4SLinus Torvalds dma_addr_t ptr_addr; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES; 21331da177e4SLinus Torvalds chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2; 21341da177e4SLinus Torvalds chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2; 21351da177e4SLinus Torvalds chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2; 21361da177e4SLinus Torvalds chip->work_size = YDSXG_DEFAULT_WORK_SIZE; 21371da177e4SLinus Torvalds 21387ab39926SClemens Ladisch size = ALIGN(playback_ctrl_size, 0x100) + 21397ab39926SClemens Ladisch ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) + 21407ab39926SClemens Ladisch ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) + 21417ab39926SClemens Ladisch ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) + 21421da177e4SLinus Torvalds chip->work_size; 21431da177e4SLinus Torvalds /* work_ptr must be aligned to 256 bytes, but it's already 21441da177e4SLinus Torvalds covered with the kernel page allocation mechanism */ 21451da177e4SLinus Torvalds if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci), 21461da177e4SLinus Torvalds size, &chip->work_ptr) < 0) 21471da177e4SLinus Torvalds return -ENOMEM; 21481da177e4SLinus Torvalds ptr = chip->work_ptr.area; 21491da177e4SLinus Torvalds ptr_addr = chip->work_ptr.addr; 21501da177e4SLinus Torvalds memset(ptr, 0, size); /* for sure */ 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds chip->bank_base_playback = ptr; 21531da177e4SLinus Torvalds chip->bank_base_playback_addr = ptr_addr; 21541da177e4SLinus Torvalds chip->ctrl_playback = (u32 *)ptr; 21551da177e4SLinus Torvalds chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES); 21567ab39926SClemens Ladisch ptr += ALIGN(playback_ctrl_size, 0x100); 21577ab39926SClemens Ladisch ptr_addr += ALIGN(playback_ctrl_size, 0x100); 21581da177e4SLinus Torvalds for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) { 21591da177e4SLinus Torvalds chip->voices[voice].number = voice; 2160208a1b4cSTakashi Iwai chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr; 21611da177e4SLinus Torvalds chip->voices[voice].bank_addr = ptr_addr; 21621da177e4SLinus Torvalds for (bank = 0; bank < 2; bank++) { 2163208a1b4cSTakashi Iwai chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr; 21641da177e4SLinus Torvalds ptr += chip->bank_size_playback; 21651da177e4SLinus Torvalds ptr_addr += chip->bank_size_playback; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds } 21687ab39926SClemens Ladisch ptr = (char *)ALIGN((unsigned long)ptr, 0x100); 21697ab39926SClemens Ladisch ptr_addr = ALIGN(ptr_addr, 0x100); 21701da177e4SLinus Torvalds chip->bank_base_capture = ptr; 21711da177e4SLinus Torvalds chip->bank_base_capture_addr = ptr_addr; 21721da177e4SLinus Torvalds for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++) 21731da177e4SLinus Torvalds for (bank = 0; bank < 2; bank++) { 2174208a1b4cSTakashi Iwai chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr; 21751da177e4SLinus Torvalds ptr += chip->bank_size_capture; 21761da177e4SLinus Torvalds ptr_addr += chip->bank_size_capture; 21771da177e4SLinus Torvalds } 21787ab39926SClemens Ladisch ptr = (char *)ALIGN((unsigned long)ptr, 0x100); 21797ab39926SClemens Ladisch ptr_addr = ALIGN(ptr_addr, 0x100); 21801da177e4SLinus Torvalds chip->bank_base_effect = ptr; 21811da177e4SLinus Torvalds chip->bank_base_effect_addr = ptr_addr; 21821da177e4SLinus Torvalds for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++) 21831da177e4SLinus Torvalds for (bank = 0; bank < 2; bank++) { 2184208a1b4cSTakashi Iwai chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr; 21851da177e4SLinus Torvalds ptr += chip->bank_size_effect; 21861da177e4SLinus Torvalds ptr_addr += chip->bank_size_effect; 21871da177e4SLinus Torvalds } 21887ab39926SClemens Ladisch ptr = (char *)ALIGN((unsigned long)ptr, 0x100); 21897ab39926SClemens Ladisch ptr_addr = ALIGN(ptr_addr, 0x100); 21901da177e4SLinus Torvalds chip->work_base = ptr; 21911da177e4SLinus Torvalds chip->work_base_addr = ptr_addr; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, ); 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr); 21961da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr); 21971da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr); 21981da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr); 21991da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2); 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds /* S/PDIF output initialization */ 22021da177e4SLinus Torvalds chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff; 22031da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0); 22041da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds /* S/PDIF input initialization */ 22071da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0); 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds /* digital mixer setup */ 22101da177e4SLinus Torvalds for (reg = 0x80; reg < 0xc0; reg += 4) 22111da177e4SLinus Torvalds snd_ymfpci_writel(chip, reg, 0); 22121da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff); 22131da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff); 22141da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff); 22151da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff); 22161da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff); 22171da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff); 22181da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff); 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds return 0; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 2223208a1b4cSTakashi Iwai static int snd_ymfpci_free(struct snd_ymfpci *chip) 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds u16 ctrl; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds snd_assert(chip != NULL, return -EINVAL); 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds if (chip->res_reg_area) { /* don't touch busy hardware */ 22301da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0); 22311da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0); 22321da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0); 22331da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0); 22341da177e4SLinus Torvalds snd_ymfpci_disable_dsp(chip); 22351da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0); 22361da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0); 22371da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0); 22381da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0); 22391da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0); 22401da177e4SLinus Torvalds ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); 22411da177e4SLinus Torvalds snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007); 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds snd_ymfpci_ac3_done(chip); 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds /* Set PCI device to D3 state */ 22471da177e4SLinus Torvalds #if 0 22481da177e4SLinus Torvalds /* FIXME: temporarily disabled, otherwise we cannot fire up 22491da177e4SLinus Torvalds * the chip again unless reboot. ACPI bug? 22501da177e4SLinus Torvalds */ 22511da177e4SLinus Torvalds pci_set_power_state(chip->pci, 3); 22521da177e4SLinus Torvalds #endif 22531da177e4SLinus Torvalds 22541da177e4SLinus Torvalds #ifdef CONFIG_PM 22551da177e4SLinus Torvalds vfree(chip->saved_regs); 22561da177e4SLinus Torvalds #endif 2257b1d5776dSTakashi Iwai release_and_free_resource(chip->mpu_res); 2258b1d5776dSTakashi Iwai release_and_free_resource(chip->fm_res); 22591da177e4SLinus Torvalds snd_ymfpci_free_gameport(chip); 22601da177e4SLinus Torvalds if (chip->reg_area_virt) 22611da177e4SLinus Torvalds iounmap(chip->reg_area_virt); 22621da177e4SLinus Torvalds if (chip->work_ptr.area) 22631da177e4SLinus Torvalds snd_dma_free_pages(&chip->work_ptr); 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds if (chip->irq >= 0) 2266437a5a46STakashi Iwai free_irq(chip->irq, chip); 2267b1d5776dSTakashi Iwai release_and_free_resource(chip->res_reg_area); 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl); 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds pci_disable_device(chip->pci); 2272b7dd2b34STakashi Iwai #ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL 2273102fa906SClemens Ladisch release_firmware(chip->dsp_microcode); 2274102fa906SClemens Ladisch release_firmware(chip->controller_microcode); 2275b7dd2b34STakashi Iwai #endif 22761da177e4SLinus Torvalds kfree(chip); 22771da177e4SLinus Torvalds return 0; 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds 2280208a1b4cSTakashi Iwai static int snd_ymfpci_dev_free(struct snd_device *device) 22811da177e4SLinus Torvalds { 2282208a1b4cSTakashi Iwai struct snd_ymfpci *chip = device->device_data; 22831da177e4SLinus Torvalds return snd_ymfpci_free(chip); 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds #ifdef CONFIG_PM 22871da177e4SLinus Torvalds static int saved_regs_index[] = { 22881da177e4SLinus Torvalds /* spdif */ 22891da177e4SLinus Torvalds YDSXGR_SPDIFOUTCTRL, 22901da177e4SLinus Torvalds YDSXGR_SPDIFOUTSTATUS, 22911da177e4SLinus Torvalds YDSXGR_SPDIFINCTRL, 22921da177e4SLinus Torvalds /* volumes */ 22931da177e4SLinus Torvalds YDSXGR_PRIADCLOOPVOL, 22941da177e4SLinus Torvalds YDSXGR_NATIVEDACINVOL, 22951da177e4SLinus Torvalds YDSXGR_NATIVEDACOUTVOL, 22969ed1261eSTeru KAMOGASHIRA YDSXGR_BUF441OUTVOL, 22971da177e4SLinus Torvalds YDSXGR_NATIVEADCINVOL, 22981da177e4SLinus Torvalds YDSXGR_SPDIFLOOPVOL, 22991da177e4SLinus Torvalds YDSXGR_SPDIFOUTVOL, 23001da177e4SLinus Torvalds YDSXGR_ZVOUTVOL, 23011da177e4SLinus Torvalds YDSXGR_LEGACYOUTVOL, 23021da177e4SLinus Torvalds /* address bases */ 23031da177e4SLinus Torvalds YDSXGR_PLAYCTRLBASE, 23041da177e4SLinus Torvalds YDSXGR_RECCTRLBASE, 23051da177e4SLinus Torvalds YDSXGR_EFFCTRLBASE, 23061da177e4SLinus Torvalds YDSXGR_WORKBASE, 23071da177e4SLinus Torvalds /* capture set up */ 23081da177e4SLinus Torvalds YDSXGR_MAPOFREC, 23091da177e4SLinus Torvalds YDSXGR_RECFORMAT, 23101da177e4SLinus Torvalds YDSXGR_RECSLOTSR, 23111da177e4SLinus Torvalds YDSXGR_ADCFORMAT, 23121da177e4SLinus Torvalds YDSXGR_ADCSLOTSR, 23131da177e4SLinus Torvalds }; 23141da177e4SLinus Torvalds #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index) 23151da177e4SLinus Torvalds 2316ded46235STakashi Iwai int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state) 23171da177e4SLinus Torvalds { 2318ded46235STakashi Iwai struct snd_card *card = pci_get_drvdata(pci); 2319ded46235STakashi Iwai struct snd_ymfpci *chip = card->private_data; 23201da177e4SLinus Torvalds unsigned int i; 23211da177e4SLinus Torvalds 2322ded46235STakashi Iwai snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 23231da177e4SLinus Torvalds snd_pcm_suspend_all(chip->pcm); 23241da177e4SLinus Torvalds snd_pcm_suspend_all(chip->pcm2); 23251da177e4SLinus Torvalds snd_pcm_suspend_all(chip->pcm_spdif); 23261da177e4SLinus Torvalds snd_pcm_suspend_all(chip->pcm_4ch); 23271da177e4SLinus Torvalds snd_ac97_suspend(chip->ac97); 23281da177e4SLinus Torvalds for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++) 23291da177e4SLinus Torvalds chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]); 23301da177e4SLinus Torvalds chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE); 23311da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0); 23321da177e4SLinus Torvalds snd_ymfpci_disable_dsp(chip); 2333ded46235STakashi Iwai pci_disable_device(pci); 2334ded46235STakashi Iwai pci_save_state(pci); 233530b35399STakashi Iwai pci_set_power_state(pci, pci_choose_state(pci, state)); 23361da177e4SLinus Torvalds return 0; 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 2339ded46235STakashi Iwai int snd_ymfpci_resume(struct pci_dev *pci) 23401da177e4SLinus Torvalds { 2341ded46235STakashi Iwai struct snd_card *card = pci_get_drvdata(pci); 2342ded46235STakashi Iwai struct snd_ymfpci *chip = card->private_data; 23431da177e4SLinus Torvalds unsigned int i; 23441da177e4SLinus Torvalds 234530b35399STakashi Iwai pci_set_power_state(pci, PCI_D0); 2346ded46235STakashi Iwai pci_restore_state(pci); 234730b35399STakashi Iwai if (pci_enable_device(pci) < 0) { 234830b35399STakashi Iwai printk(KERN_ERR "ymfpci: pci_enable_device failed, " 234930b35399STakashi Iwai "disabling device\n"); 235030b35399STakashi Iwai snd_card_disconnect(card); 235130b35399STakashi Iwai return -EIO; 235230b35399STakashi Iwai } 2353ded46235STakashi Iwai pci_set_master(pci); 2354ded46235STakashi Iwai snd_ymfpci_aclink_reset(pci); 23551da177e4SLinus Torvalds snd_ymfpci_codec_ready(chip, 0); 23561da177e4SLinus Torvalds snd_ymfpci_download_image(chip); 23571da177e4SLinus Torvalds udelay(100); 23581da177e4SLinus Torvalds 23591da177e4SLinus Torvalds for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++) 23601da177e4SLinus Torvalds snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]); 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds snd_ac97_resume(chip->ac97); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* start hw again */ 23651da177e4SLinus Torvalds if (chip->start_count > 0) { 23661da177e4SLinus Torvalds spin_lock_irq(&chip->reg_lock); 23671da177e4SLinus Torvalds snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode); 23681da177e4SLinus Torvalds chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT); 23691da177e4SLinus Torvalds spin_unlock_irq(&chip->reg_lock); 23701da177e4SLinus Torvalds } 2371ded46235STakashi Iwai snd_power_change_state(card, SNDRV_CTL_POWER_D0); 23721da177e4SLinus Torvalds return 0; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds #endif /* CONFIG_PM */ 23751da177e4SLinus Torvalds 2376208a1b4cSTakashi Iwai int __devinit snd_ymfpci_create(struct snd_card *card, 23771da177e4SLinus Torvalds struct pci_dev * pci, 23781da177e4SLinus Torvalds unsigned short old_legacy_ctrl, 2379208a1b4cSTakashi Iwai struct snd_ymfpci ** rchip) 23801da177e4SLinus Torvalds { 2381208a1b4cSTakashi Iwai struct snd_ymfpci *chip; 23821da177e4SLinus Torvalds int err; 2383208a1b4cSTakashi Iwai static struct snd_device_ops ops = { 23841da177e4SLinus Torvalds .dev_free = snd_ymfpci_dev_free, 23851da177e4SLinus Torvalds }; 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds *rchip = NULL; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds /* enable PCI device */ 23901da177e4SLinus Torvalds if ((err = pci_enable_device(pci)) < 0) 23911da177e4SLinus Torvalds return err; 23921da177e4SLinus Torvalds 2393e560d8d8STakashi Iwai chip = kzalloc(sizeof(*chip), GFP_KERNEL); 23941da177e4SLinus Torvalds if (chip == NULL) { 23951da177e4SLinus Torvalds pci_disable_device(pci); 23961da177e4SLinus Torvalds return -ENOMEM; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds chip->old_legacy_ctrl = old_legacy_ctrl; 23991da177e4SLinus Torvalds spin_lock_init(&chip->reg_lock); 24001da177e4SLinus Torvalds spin_lock_init(&chip->voice_lock); 24011da177e4SLinus Torvalds init_waitqueue_head(&chip->interrupt_sleep); 24021da177e4SLinus Torvalds atomic_set(&chip->interrupt_sleep_count, 0); 24031da177e4SLinus Torvalds chip->card = card; 24041da177e4SLinus Torvalds chip->pci = pci; 24051da177e4SLinus Torvalds chip->irq = -1; 24061da177e4SLinus Torvalds chip->device_id = pci->device; 240701f681daSTakashi Iwai pci_read_config_byte(pci, PCI_REVISION_ID, &chip->rev); 24081da177e4SLinus Torvalds chip->reg_area_phys = pci_resource_start(pci, 0); 24091da177e4SLinus Torvalds chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000); 24101da177e4SLinus Torvalds pci_set_master(pci); 24119ed1261eSTeru KAMOGASHIRA chip->src441_used = -1; 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) { 241499b359baSTakashi Iwai snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1); 24151da177e4SLinus Torvalds snd_ymfpci_free(chip); 24161da177e4SLinus Torvalds return -EBUSY; 24171da177e4SLinus Torvalds } 2418437a5a46STakashi Iwai if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED, 2419437a5a46STakashi Iwai "YMFPCI", chip)) { 242099b359baSTakashi Iwai snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq); 24211da177e4SLinus Torvalds snd_ymfpci_free(chip); 24221da177e4SLinus Torvalds return -EBUSY; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds chip->irq = pci->irq; 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds snd_ymfpci_aclink_reset(pci); 24271da177e4SLinus Torvalds if (snd_ymfpci_codec_ready(chip, 0) < 0) { 24281da177e4SLinus Torvalds snd_ymfpci_free(chip); 24291da177e4SLinus Torvalds return -EIO; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 2432102fa906SClemens Ladisch err = snd_ymfpci_request_firmware(chip); 2433102fa906SClemens Ladisch if (err < 0) { 2434102fa906SClemens Ladisch snd_printk(KERN_ERR "firmware request failed: %d\n", err); 2435102fa906SClemens Ladisch snd_ymfpci_free(chip); 2436102fa906SClemens Ladisch return err; 2437102fa906SClemens Ladisch } 24381da177e4SLinus Torvalds snd_ymfpci_download_image(chip); 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds udelay(100); /* seems we need a delay after downloading image.. */ 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds if (snd_ymfpci_memalloc(chip) < 0) { 24431da177e4SLinus Torvalds snd_ymfpci_free(chip); 24441da177e4SLinus Torvalds return -EIO; 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds if ((err = snd_ymfpci_ac3_init(chip)) < 0) { 24481da177e4SLinus Torvalds snd_ymfpci_free(chip); 24491da177e4SLinus Torvalds return err; 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds #ifdef CONFIG_PM 24531da177e4SLinus Torvalds chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32)); 24541da177e4SLinus Torvalds if (chip->saved_regs == NULL) { 24551da177e4SLinus Torvalds snd_ymfpci_free(chip); 24561da177e4SLinus Torvalds return -ENOMEM; 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds #endif 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 24611da177e4SLinus Torvalds snd_ymfpci_free(chip); 24621da177e4SLinus Torvalds return err; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds snd_ymfpci_proc_init(card, chip); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds snd_card_set_dev(card, &pci->dev); 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds *rchip = chip; 24701da177e4SLinus Torvalds return 0; 24711da177e4SLinus Torvalds } 2472