2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of YMF724/740/744/754 chips
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sound/driver.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/tlv.h>
35 #include <sound/ymfpci.h>
36 #include <sound/asoundef.h>
37 #include <sound/mpu401.h>
40 #include <asm/byteorder.h>
46 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
48 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
50 return readb(chip->reg_area_virt + offset);
53 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
55 writeb(val, chip->reg_area_virt + offset);
58 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
60 return readw(chip->reg_area_virt + offset);
63 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
65 writew(val, chip->reg_area_virt + offset);
68 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
70 return readl(chip->reg_area_virt + offset);
73 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
75 writel(val, chip->reg_area_virt + offset);
78 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
80 unsigned long end_time;
81 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
83 end_time = jiffies + msecs_to_jiffies(750);
85 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
87 set_current_state(TASK_UNINTERRUPTIBLE);
88 schedule_timeout_uninterruptible(1);
89 } while (time_before(jiffies, end_time));
90 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
94 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
96 struct snd_ymfpci *chip = ac97->private_data;
99 snd_ymfpci_codec_ready(chip, 0);
100 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
101 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
104 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
106 struct snd_ymfpci *chip = ac97->private_data;
108 if (snd_ymfpci_codec_ready(chip, 0))
110 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
111 if (snd_ymfpci_codec_ready(chip, 0))
113 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
115 for (i = 0; i < 600; i++)
116 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
118 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
125 static u32 snd_ymfpci_calc_delta(u32 rate)
128 case 8000: return 0x02aaab00;
129 case 11025: return 0x03accd00;
130 case 16000: return 0x05555500;
131 case 22050: return 0x07599a00;
132 case 32000: return 0x0aaaab00;
133 case 44100: return 0x0eb33300;
134 default: return ((rate << 16) / 375) << 5;
138 static u32 def_rate[8] = {
139 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
142 static u32 snd_ymfpci_calc_lpfK(u32 rate)
145 static u32 val[8] = {
146 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
147 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
151 return 0x40000000; /* FIXME: What's the right value? */
152 for (i = 0; i < 8; i++)
153 if (rate <= def_rate[i])
158 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
161 static u32 val[8] = {
162 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
163 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
168 for (i = 0; i < 8; i++)
169 if (rate <= def_rate[i])
175 * Hardware start management
178 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
182 spin_lock_irqsave(&chip->reg_lock, flags);
183 if (chip->start_count++ > 0)
185 snd_ymfpci_writel(chip, YDSXGR_MODE,
186 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
187 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
189 spin_unlock_irqrestore(&chip->reg_lock, flags);
192 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
197 spin_lock_irqsave(&chip->reg_lock, flags);
198 if (--chip->start_count > 0)
200 snd_ymfpci_writel(chip, YDSXGR_MODE,
201 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
202 while (timeout-- > 0) {
203 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
206 if (atomic_read(&chip->interrupt_sleep_count)) {
207 atomic_set(&chip->interrupt_sleep_count, 0);
208 wake_up(&chip->interrupt_sleep);
211 spin_unlock_irqrestore(&chip->reg_lock, flags);
215 * Playback voice management
218 static int voice_alloc(struct snd_ymfpci *chip,
219 enum snd_ymfpci_voice_type type, int pair,
220 struct snd_ymfpci_voice **rvoice)
222 struct snd_ymfpci_voice *voice, *voice2;
226 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
227 voice = &chip->voices[idx];
228 voice2 = pair ? &chip->voices[idx+1] : NULL;
229 if (voice->use || (voice2 && voice2->use))
247 snd_ymfpci_hw_start(chip);
249 snd_ymfpci_hw_start(chip);
256 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
257 enum snd_ymfpci_voice_type type, int pair,
258 struct snd_ymfpci_voice **rvoice)
263 snd_assert(rvoice != NULL, return -EINVAL);
264 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
266 spin_lock_irqsave(&chip->voice_lock, flags);
268 result = voice_alloc(chip, type, pair, rvoice);
269 if (result == 0 || type != YMFPCI_PCM)
271 /* TODO: synth/midi voice deallocation */
274 spin_unlock_irqrestore(&chip->voice_lock, flags);
278 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
282 snd_assert(pvoice != NULL, return -EINVAL);
283 snd_ymfpci_hw_stop(chip);
284 spin_lock_irqsave(&chip->voice_lock, flags);
285 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
287 pvoice->interrupt = NULL;
288 spin_unlock_irqrestore(&chip->voice_lock, flags);
296 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
298 struct snd_ymfpci_pcm *ypcm;
301 if ((ypcm = voice->ypcm) == NULL)
303 if (ypcm->substream == NULL)
305 spin_lock(&chip->reg_lock);
307 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
308 if (pos < ypcm->last_pos)
309 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
311 delta = pos - ypcm->last_pos;
312 ypcm->period_pos += delta;
313 ypcm->last_pos = pos;
314 if (ypcm->period_pos >= ypcm->period_size) {
315 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
316 ypcm->period_pos %= ypcm->period_size;
317 spin_unlock(&chip->reg_lock);
318 snd_pcm_period_elapsed(ypcm->substream);
319 spin_lock(&chip->reg_lock);
322 if (unlikely(ypcm->update_pcm_vol)) {
323 unsigned int subs = ypcm->substream->number;
324 unsigned int next_bank = 1 - chip->active_bank;
325 struct snd_ymfpci_playback_bank *bank;
328 bank = &voice->bank[next_bank];
329 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
330 bank->left_gain_end = volume;
331 if (ypcm->output_rear)
332 bank->eff2_gain_end = volume;
334 bank = &ypcm->voices[1]->bank[next_bank];
335 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
336 bank->right_gain_end = volume;
337 if (ypcm->output_rear)
338 bank->eff3_gain_end = volume;
339 ypcm->update_pcm_vol--;
342 spin_unlock(&chip->reg_lock);
345 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
347 struct snd_pcm_runtime *runtime = substream->runtime;
348 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
349 struct snd_ymfpci *chip = ypcm->chip;
352 spin_lock(&chip->reg_lock);
354 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
355 if (pos < ypcm->last_pos)
356 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
358 delta = pos - ypcm->last_pos;
359 ypcm->period_pos += delta;
360 ypcm->last_pos = pos;
361 if (ypcm->period_pos >= ypcm->period_size) {
362 ypcm->period_pos %= ypcm->period_size;
363 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
364 spin_unlock(&chip->reg_lock);
365 snd_pcm_period_elapsed(substream);
366 spin_lock(&chip->reg_lock);
369 spin_unlock(&chip->reg_lock);
372 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
375 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
376 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
379 spin_lock(&chip->reg_lock);
380 if (ypcm->voices[0] == NULL) {
385 case SNDRV_PCM_TRIGGER_START:
386 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
387 case SNDRV_PCM_TRIGGER_RESUME:
388 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
389 if (ypcm->voices[1] != NULL)
390 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
393 case SNDRV_PCM_TRIGGER_STOP:
394 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
395 case SNDRV_PCM_TRIGGER_SUSPEND:
396 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
397 if (ypcm->voices[1] != NULL)
398 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
406 spin_unlock(&chip->reg_lock);
409 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
412 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
413 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
417 spin_lock(&chip->reg_lock);
419 case SNDRV_PCM_TRIGGER_START:
420 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
421 case SNDRV_PCM_TRIGGER_RESUME:
422 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
423 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
426 case SNDRV_PCM_TRIGGER_STOP:
427 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
428 case SNDRV_PCM_TRIGGER_SUSPEND:
429 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
430 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
437 spin_unlock(&chip->reg_lock);
441 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
445 if (ypcm->voices[1] != NULL && voices < 2) {
446 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
447 ypcm->voices[1] = NULL;
449 if (voices == 1 && ypcm->voices[0] != NULL)
450 return 0; /* already allocated */
451 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
452 return 0; /* already allocated */
454 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
455 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
456 ypcm->voices[0] = NULL;
459 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
462 ypcm->voices[0]->ypcm = ypcm;
463 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
465 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
466 ypcm->voices[1]->ypcm = ypcm;
471 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
472 struct snd_pcm_runtime *runtime,
475 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
477 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
478 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
479 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
480 struct snd_ymfpci_playback_bank *bank;
482 u32 vol_left, vol_right;
483 u8 use_left, use_right;
485 snd_assert(voice != NULL, return);
486 if (runtime->channels == 1) {
490 use_left = (voiceidx & 1) == 0;
491 use_right = !use_left;
493 if (has_pcm_volume) {
494 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
495 [ypcm->substream->number].left << 15);
496 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
497 [ypcm->substream->number].right << 15);
499 vol_left = cpu_to_le32(0x40000000);
500 vol_right = cpu_to_le32(0x40000000);
502 format = runtime->channels == 2 ? 0x00010000 : 0;
503 if (snd_pcm_format_width(runtime->format) == 8)
504 format |= 0x80000000;
505 if (runtime->channels == 2 && (voiceidx & 1) != 0)
507 for (nbank = 0; nbank < 2; nbank++) {
508 bank = &voice->bank[nbank];
509 memset(bank, 0, sizeof(*bank));
510 bank->format = cpu_to_le32(format);
511 bank->base = cpu_to_le32(runtime->dma_addr);
512 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
513 bank->lpfQ = cpu_to_le32(lpfQ);
515 bank->delta_end = cpu_to_le32(delta);
517 bank->lpfK_end = cpu_to_le32(lpfK);
519 bank->eg_gain_end = cpu_to_le32(0x40000000);
521 if (ypcm->output_front) {
524 bank->left_gain_end = vol_left;
528 bank->right_gain_end = vol_right;
531 if (ypcm->output_rear) {
532 if (!ypcm->swap_rear) {
535 bank->eff2_gain_end = vol_left;
539 bank->eff3_gain_end = vol_right;
542 /* The SPDIF out channels seem to be swapped, so we have
543 * to swap them here, too. The rear analog out channels
544 * will be wrong, but otherwise AC3 would not work.
548 bank->eff3_gain_end = vol_left;
552 bank->eff2_gain_end = vol_right;
559 static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
561 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
562 4096, &chip->ac3_tmp_base) < 0)
565 chip->bank_effect[3][0]->base =
566 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
567 chip->bank_effect[3][0]->loop_end =
568 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
569 chip->bank_effect[4][0]->base =
570 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
571 chip->bank_effect[4][0]->loop_end =
572 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
574 spin_lock_irq(&chip->reg_lock);
575 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
576 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
577 spin_unlock_irq(&chip->reg_lock);
581 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
583 spin_lock_irq(&chip->reg_lock);
584 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
585 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
586 spin_unlock_irq(&chip->reg_lock);
587 // snd_ymfpci_irq_wait(chip);
588 if (chip->ac3_tmp_base.area) {
589 snd_dma_free_pages(&chip->ac3_tmp_base);
590 chip->ac3_tmp_base.area = NULL;
595 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
596 struct snd_pcm_hw_params *hw_params)
598 struct snd_pcm_runtime *runtime = substream->runtime;
599 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
602 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
604 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
609 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
611 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
612 struct snd_pcm_runtime *runtime = substream->runtime;
613 struct snd_ymfpci_pcm *ypcm;
615 if (runtime->private_data == NULL)
617 ypcm = runtime->private_data;
619 /* wait, until the PCI operations are not finished */
620 snd_ymfpci_irq_wait(chip);
621 snd_pcm_lib_free_pages(substream);
622 if (ypcm->voices[1]) {
623 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
624 ypcm->voices[1] = NULL;
626 if (ypcm->voices[0]) {
627 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
628 ypcm->voices[0] = NULL;
633 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
635 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
636 struct snd_pcm_runtime *runtime = substream->runtime;
637 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
640 ypcm->period_size = runtime->period_size;
641 ypcm->buffer_size = runtime->buffer_size;
642 ypcm->period_pos = 0;
644 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
645 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
646 substream->pcm == chip->pcm);
650 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
651 struct snd_pcm_hw_params *hw_params)
653 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
656 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
658 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
660 /* wait, until the PCI operations are not finished */
661 snd_ymfpci_irq_wait(chip);
662 return snd_pcm_lib_free_pages(substream);
665 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
667 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
668 struct snd_pcm_runtime *runtime = substream->runtime;
669 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
670 struct snd_ymfpci_capture_bank * bank;
674 ypcm->period_size = runtime->period_size;
675 ypcm->buffer_size = runtime->buffer_size;
676 ypcm->period_pos = 0;
679 rate = ((48000 * 4096) / runtime->rate) - 1;
681 if (runtime->channels == 2) {
685 if (snd_pcm_format_width(runtime->format) == 8)
689 switch (ypcm->capture_bank_number) {
691 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
692 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
695 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
696 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
699 for (nbank = 0; nbank < 2; nbank++) {
700 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
701 bank->base = cpu_to_le32(runtime->dma_addr);
702 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
704 bank->num_of_loops = 0;
709 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
711 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
712 struct snd_pcm_runtime *runtime = substream->runtime;
713 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
714 struct snd_ymfpci_voice *voice = ypcm->voices[0];
716 if (!(ypcm->running && voice))
718 return le32_to_cpu(voice->bank[chip->active_bank].start);
721 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
723 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
724 struct snd_pcm_runtime *runtime = substream->runtime;
725 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
729 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
732 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
737 while (loops-- > 0) {
738 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
740 init_waitqueue_entry(&wait, current);
741 add_wait_queue(&chip->interrupt_sleep, &wait);
742 atomic_inc(&chip->interrupt_sleep_count);
743 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
744 remove_wait_queue(&chip->interrupt_sleep, &wait);
748 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
750 struct snd_ymfpci *chip = dev_id;
751 u32 status, nvoice, mode;
752 struct snd_ymfpci_voice *voice;
754 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
755 if (status & 0x80000000) {
756 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
757 spin_lock(&chip->voice_lock);
758 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
759 voice = &chip->voices[nvoice];
760 if (voice->interrupt)
761 voice->interrupt(chip, voice);
763 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
764 if (chip->capture_substream[nvoice])
765 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
768 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
769 if (chip->effect_substream[nvoice])
770 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
773 spin_unlock(&chip->voice_lock);
774 spin_lock(&chip->reg_lock);
775 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
776 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
777 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
778 spin_unlock(&chip->reg_lock);
780 if (atomic_read(&chip->interrupt_sleep_count)) {
781 atomic_set(&chip->interrupt_sleep_count, 0);
782 wake_up(&chip->interrupt_sleep);
786 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
789 snd_timer_interrupt(chip->timer, chip->timer->sticks);
791 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
794 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
798 static struct snd_pcm_hardware snd_ymfpci_playback =
800 .info = (SNDRV_PCM_INFO_MMAP |
801 SNDRV_PCM_INFO_MMAP_VALID |
802 SNDRV_PCM_INFO_INTERLEAVED |
803 SNDRV_PCM_INFO_BLOCK_TRANSFER |
804 SNDRV_PCM_INFO_PAUSE |
805 SNDRV_PCM_INFO_RESUME),
806 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
807 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
812 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
813 .period_bytes_min = 64,
814 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
820 static struct snd_pcm_hardware snd_ymfpci_capture =
822 .info = (SNDRV_PCM_INFO_MMAP |
823 SNDRV_PCM_INFO_MMAP_VALID |
824 SNDRV_PCM_INFO_INTERLEAVED |
825 SNDRV_PCM_INFO_BLOCK_TRANSFER |
826 SNDRV_PCM_INFO_PAUSE |
827 SNDRV_PCM_INFO_RESUME),
828 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
829 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
834 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
835 .period_bytes_min = 64,
836 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
842 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
844 kfree(runtime->private_data);
847 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
849 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
850 struct snd_pcm_runtime *runtime = substream->runtime;
851 struct snd_ymfpci_pcm *ypcm;
853 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
857 ypcm->type = PLAYBACK_VOICE;
858 ypcm->substream = substream;
859 runtime->hw = snd_ymfpci_playback;
860 runtime->private_data = ypcm;
861 runtime->private_free = snd_ymfpci_pcm_free_substream;
862 /* FIXME? True value is 256/48 = 5.33333 ms */
863 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
867 /* call with spinlock held */
868 static void ymfpci_open_extension(struct snd_ymfpci *chip)
870 if (! chip->rear_opened) {
871 if (! chip->spdif_opened) /* set AC3 */
872 snd_ymfpci_writel(chip, YDSXGR_MODE,
873 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
874 /* enable second codec (4CHEN) */
875 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
876 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
880 /* call with spinlock held */
881 static void ymfpci_close_extension(struct snd_ymfpci *chip)
883 if (! chip->rear_opened) {
884 if (! chip->spdif_opened)
885 snd_ymfpci_writel(chip, YDSXGR_MODE,
886 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
887 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
888 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
892 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
894 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
895 struct snd_pcm_runtime *runtime = substream->runtime;
896 struct snd_ymfpci_pcm *ypcm;
897 struct snd_kcontrol *kctl;
900 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
902 ypcm = runtime->private_data;
903 ypcm->output_front = 1;
904 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
906 spin_lock_irq(&chip->reg_lock);
907 if (ypcm->output_rear) {
908 ymfpci_open_extension(chip);
911 spin_unlock_irq(&chip->reg_lock);
913 kctl = chip->pcm_mixer[substream->number].ctl;
914 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
915 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
919 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
921 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
922 struct snd_pcm_runtime *runtime = substream->runtime;
923 struct snd_ymfpci_pcm *ypcm;
926 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
928 ypcm = runtime->private_data;
929 ypcm->output_front = 0;
930 ypcm->output_rear = 1;
932 spin_lock_irq(&chip->reg_lock);
933 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
934 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
935 ymfpci_open_extension(chip);
936 chip->spdif_pcm_bits = chip->spdif_bits;
937 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
938 chip->spdif_opened++;
939 spin_unlock_irq(&chip->reg_lock);
941 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
942 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
943 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
947 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
949 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
950 struct snd_pcm_runtime *runtime = substream->runtime;
951 struct snd_ymfpci_pcm *ypcm;
954 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
956 ypcm = runtime->private_data;
957 ypcm->output_front = 0;
958 ypcm->output_rear = 1;
960 spin_lock_irq(&chip->reg_lock);
961 ymfpci_open_extension(chip);
963 spin_unlock_irq(&chip->reg_lock);
967 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
968 u32 capture_bank_number)
970 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
971 struct snd_pcm_runtime *runtime = substream->runtime;
972 struct snd_ymfpci_pcm *ypcm;
974 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
978 ypcm->type = capture_bank_number + CAPTURE_REC;
979 ypcm->substream = substream;
980 ypcm->capture_bank_number = capture_bank_number;
981 chip->capture_substream[capture_bank_number] = substream;
982 runtime->hw = snd_ymfpci_capture;
983 /* FIXME? True value is 256/48 = 5.33333 ms */
984 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
985 runtime->private_data = ypcm;
986 runtime->private_free = snd_ymfpci_pcm_free_substream;
987 snd_ymfpci_hw_start(chip);
991 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
993 return snd_ymfpci_capture_open(substream, 0);
996 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
998 return snd_ymfpci_capture_open(substream, 1);
1001 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1006 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1008 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1009 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1010 struct snd_kcontrol *kctl;
1012 spin_lock_irq(&chip->reg_lock);
1013 if (ypcm->output_rear && chip->rear_opened > 0) {
1014 chip->rear_opened--;
1015 ymfpci_close_extension(chip);
1017 spin_unlock_irq(&chip->reg_lock);
1018 kctl = chip->pcm_mixer[substream->number].ctl;
1019 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1020 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
1021 return snd_ymfpci_playback_close_1(substream);
1024 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1026 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1028 spin_lock_irq(&chip->reg_lock);
1029 chip->spdif_opened = 0;
1030 ymfpci_close_extension(chip);
1031 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1032 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1033 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1034 spin_unlock_irq(&chip->reg_lock);
1035 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1036 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1037 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1038 return snd_ymfpci_playback_close_1(substream);
1041 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1043 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1045 spin_lock_irq(&chip->reg_lock);
1046 if (chip->rear_opened > 0) {
1047 chip->rear_opened--;
1048 ymfpci_close_extension(chip);
1050 spin_unlock_irq(&chip->reg_lock);
1051 return snd_ymfpci_playback_close_1(substream);
1054 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1056 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1057 struct snd_pcm_runtime *runtime = substream->runtime;
1058 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1061 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1062 snd_ymfpci_hw_stop(chip);
1067 static struct snd_pcm_ops snd_ymfpci_playback_ops = {
1068 .open = snd_ymfpci_playback_open,
1069 .close = snd_ymfpci_playback_close,
1070 .ioctl = snd_pcm_lib_ioctl,
1071 .hw_params = snd_ymfpci_playback_hw_params,
1072 .hw_free = snd_ymfpci_playback_hw_free,
1073 .prepare = snd_ymfpci_playback_prepare,
1074 .trigger = snd_ymfpci_playback_trigger,
1075 .pointer = snd_ymfpci_playback_pointer,
1078 static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1079 .open = snd_ymfpci_capture_rec_open,
1080 .close = snd_ymfpci_capture_close,
1081 .ioctl = snd_pcm_lib_ioctl,
1082 .hw_params = snd_ymfpci_capture_hw_params,
1083 .hw_free = snd_ymfpci_capture_hw_free,
1084 .prepare = snd_ymfpci_capture_prepare,
1085 .trigger = snd_ymfpci_capture_trigger,
1086 .pointer = snd_ymfpci_capture_pointer,
1089 int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1091 struct snd_pcm *pcm;
1096 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1098 pcm->private_data = chip;
1100 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1101 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1104 pcm->info_flags = 0;
1105 strcpy(pcm->name, "YMFPCI");
1108 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1109 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1116 static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1117 .open = snd_ymfpci_capture_ac97_open,
1118 .close = snd_ymfpci_capture_close,
1119 .ioctl = snd_pcm_lib_ioctl,
1120 .hw_params = snd_ymfpci_capture_hw_params,
1121 .hw_free = snd_ymfpci_capture_hw_free,
1122 .prepare = snd_ymfpci_capture_prepare,
1123 .trigger = snd_ymfpci_capture_trigger,
1124 .pointer = snd_ymfpci_capture_pointer,
1127 int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1129 struct snd_pcm *pcm;
1134 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1136 pcm->private_data = chip;
1138 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1141 pcm->info_flags = 0;
1142 sprintf(pcm->name, "YMFPCI - %s",
1143 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1146 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1147 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1154 static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1155 .open = snd_ymfpci_playback_spdif_open,
1156 .close = snd_ymfpci_playback_spdif_close,
1157 .ioctl = snd_pcm_lib_ioctl,
1158 .hw_params = snd_ymfpci_playback_hw_params,
1159 .hw_free = snd_ymfpci_playback_hw_free,
1160 .prepare = snd_ymfpci_playback_prepare,
1161 .trigger = snd_ymfpci_playback_trigger,
1162 .pointer = snd_ymfpci_playback_pointer,
1165 int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1167 struct snd_pcm *pcm;
1172 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1174 pcm->private_data = chip;
1176 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1179 pcm->info_flags = 0;
1180 strcpy(pcm->name, "YMFPCI - IEC958");
1181 chip->pcm_spdif = pcm;
1183 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1184 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1191 static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1192 .open = snd_ymfpci_playback_4ch_open,
1193 .close = snd_ymfpci_playback_4ch_close,
1194 .ioctl = snd_pcm_lib_ioctl,
1195 .hw_params = snd_ymfpci_playback_hw_params,
1196 .hw_free = snd_ymfpci_playback_hw_free,
1197 .prepare = snd_ymfpci_playback_prepare,
1198 .trigger = snd_ymfpci_playback_trigger,
1199 .pointer = snd_ymfpci_playback_pointer,
1202 int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1204 struct snd_pcm *pcm;
1209 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1211 pcm->private_data = chip;
1213 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1216 pcm->info_flags = 0;
1217 strcpy(pcm->name, "YMFPCI - Rear PCM");
1218 chip->pcm_4ch = pcm;
1220 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1221 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1228 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1230 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1235 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1236 struct snd_ctl_elem_value *ucontrol)
1238 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1240 spin_lock_irq(&chip->reg_lock);
1241 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1242 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1243 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1244 spin_unlock_irq(&chip->reg_lock);
1248 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1249 struct snd_ctl_elem_value *ucontrol)
1251 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1255 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1256 (ucontrol->value.iec958.status[1] << 8);
1257 spin_lock_irq(&chip->reg_lock);
1258 change = chip->spdif_bits != val;
1259 chip->spdif_bits = val;
1260 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1261 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1262 spin_unlock_irq(&chip->reg_lock);
1266 static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
1268 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1269 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1270 .info = snd_ymfpci_spdif_default_info,
1271 .get = snd_ymfpci_spdif_default_get,
1272 .put = snd_ymfpci_spdif_default_put
1275 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1277 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1282 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1283 struct snd_ctl_elem_value *ucontrol)
1285 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1287 spin_lock_irq(&chip->reg_lock);
1288 ucontrol->value.iec958.status[0] = 0x3e;
1289 ucontrol->value.iec958.status[1] = 0xff;
1290 spin_unlock_irq(&chip->reg_lock);
1294 static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
1296 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1298 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1299 .info = snd_ymfpci_spdif_mask_info,
1300 .get = snd_ymfpci_spdif_mask_get,
1303 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1310 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
1313 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1315 spin_lock_irq(&chip->reg_lock);
1316 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1317 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1318 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1319 spin_unlock_irq(&chip->reg_lock);
1323 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1324 struct snd_ctl_elem_value *ucontrol)
1326 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1330 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1331 (ucontrol->value.iec958.status[1] << 8);
1332 spin_lock_irq(&chip->reg_lock);
1333 change = chip->spdif_pcm_bits != val;
1334 chip->spdif_pcm_bits = val;
1335 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1336 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1337 spin_unlock_irq(&chip->reg_lock);
1341 static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
1343 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1344 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1345 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1346 .info = snd_ymfpci_spdif_stream_info,
1347 .get = snd_ymfpci_spdif_stream_get,
1348 .put = snd_ymfpci_spdif_stream_put
1351 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1353 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1355 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1357 info->value.enumerated.items = 3;
1358 if (info->value.enumerated.item > 2)
1359 info->value.enumerated.item = 2;
1360 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1364 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1366 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1369 spin_lock_irq(&chip->reg_lock);
1370 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1371 spin_unlock_irq(&chip->reg_lock);
1373 value->value.enumerated.item[0] = 0;
1375 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1379 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1381 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1384 spin_lock_irq(&chip->reg_lock);
1385 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1386 if (value->value.enumerated.item[0] == 0)
1387 reg = old_reg & ~0x100;
1389 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1390 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1391 spin_unlock_irq(&chip->reg_lock);
1392 return reg != old_reg;
1395 static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
1396 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1398 .name = "Direct Recording Source",
1399 .info = snd_ymfpci_drec_source_info,
1400 .get = snd_ymfpci_drec_source_get,
1401 .put = snd_ymfpci_drec_source_put
1408 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1409 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1410 .info = snd_ymfpci_info_single, \
1411 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1412 .private_value = ((reg) | ((shift) << 16)) }
1414 static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol,
1415 struct snd_ctl_elem_info *uinfo)
1417 int reg = kcontrol->private_value & 0xffff;
1420 case YDSXGR_SPDIFOUTCTRL: break;
1421 case YDSXGR_SPDIFINCTRL: break;
1422 default: return -EINVAL;
1424 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1426 uinfo->value.integer.min = 0;
1427 uinfo->value.integer.max = 1;
1431 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1434 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1435 int reg = kcontrol->private_value & 0xffff;
1436 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1437 unsigned int mask = 1;
1440 case YDSXGR_SPDIFOUTCTRL: break;
1441 case YDSXGR_SPDIFINCTRL: break;
1442 default: return -EINVAL;
1444 ucontrol->value.integer.value[0] =
1445 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1449 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1450 struct snd_ctl_elem_value *ucontrol)
1452 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1453 int reg = kcontrol->private_value & 0xffff;
1454 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1455 unsigned int mask = 1;
1457 unsigned int val, oval;
1460 case YDSXGR_SPDIFOUTCTRL: break;
1461 case YDSXGR_SPDIFINCTRL: break;
1462 default: return -EINVAL;
1464 val = (ucontrol->value.integer.value[0] & mask);
1466 spin_lock_irq(&chip->reg_lock);
1467 oval = snd_ymfpci_readl(chip, reg);
1468 val = (oval & ~(mask << shift)) | val;
1469 change = val != oval;
1470 snd_ymfpci_writel(chip, reg, val);
1471 spin_unlock_irq(&chip->reg_lock);
1475 static DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1477 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1478 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1479 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1480 .info = snd_ymfpci_info_double, \
1481 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1482 .private_value = reg, \
1483 .tlv = { .p = db_scale_native } }
1485 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1487 unsigned int reg = kcontrol->private_value;
1489 if (reg < 0x80 || reg >= 0xc0)
1491 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1493 uinfo->value.integer.min = 0;
1494 uinfo->value.integer.max = 16383;
1498 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1500 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1501 unsigned int reg = kcontrol->private_value;
1502 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1505 if (reg < 0x80 || reg >= 0xc0)
1507 spin_lock_irq(&chip->reg_lock);
1508 val = snd_ymfpci_readl(chip, reg);
1509 spin_unlock_irq(&chip->reg_lock);
1510 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1511 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1515 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1517 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1518 unsigned int reg = kcontrol->private_value;
1519 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1521 unsigned int val1, val2, oval;
1523 if (reg < 0x80 || reg >= 0xc0)
1525 val1 = ucontrol->value.integer.value[0] & mask;
1526 val2 = ucontrol->value.integer.value[1] & mask;
1527 val1 <<= shift_left;
1528 val2 <<= shift_right;
1529 spin_lock_irq(&chip->reg_lock);
1530 oval = snd_ymfpci_readl(chip, reg);
1531 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1532 change = val1 != oval;
1533 snd_ymfpci_writel(chip, reg, val1);
1534 spin_unlock_irq(&chip->reg_lock);
1541 static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1543 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1545 uinfo->value.integer.min = 0;
1546 uinfo->value.integer.max = 1;
1550 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1552 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1553 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1557 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1559 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1561 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1563 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1568 static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1569 YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1570 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1571 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1572 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1573 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1574 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1575 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1576 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1577 YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1578 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1579 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1580 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1581 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1582 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1583 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1584 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1587 .name = "4ch Duplication",
1588 .info = snd_ymfpci_info_dup4ch,
1589 .get = snd_ymfpci_get_dup4ch,
1590 .put = snd_ymfpci_put_dup4ch,
1599 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1602 unsigned long flags;
1604 spin_lock_irqsave(&chip->reg_lock, flags);
1605 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1606 reg &= ~(1 << (pin + 8));
1608 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1609 /* set the level mode for input line */
1610 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1611 mode &= ~(3 << (pin * 2));
1612 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1613 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1614 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1615 spin_unlock_irqrestore(&chip->reg_lock, flags);
1616 return (mode >> pin) & 1;
1619 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1622 unsigned long flags;
1624 spin_lock_irqsave(&chip->reg_lock, flags);
1625 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1627 reg &= ~(1 << (pin + 8));
1628 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1629 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1630 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1631 spin_unlock_irqrestore(&chip->reg_lock, flags);
1636 static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1638 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1640 uinfo->value.integer.min = 0;
1641 uinfo->value.integer.max = 1;
1645 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1647 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1648 int pin = (int)kcontrol->private_value;
1649 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1653 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1655 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1656 int pin = (int)kcontrol->private_value;
1658 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1659 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1660 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1666 static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
1667 .name = "Shared Rear/Line-In Switch",
1668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669 .info = snd_ymfpci_gpio_sw_info,
1670 .get = snd_ymfpci_gpio_sw_get,
1671 .put = snd_ymfpci_gpio_sw_put,
1679 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1680 struct snd_ctl_elem_info *uinfo)
1682 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1684 uinfo->value.integer.min = 0;
1685 uinfo->value.integer.max = 0x8000;
1689 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1692 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1693 unsigned int subs = kcontrol->id.subdevice;
1695 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1696 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1700 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol)
1703 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1704 unsigned int subs = kcontrol->id.subdevice;
1705 struct snd_pcm_substream *substream;
1706 unsigned long flags;
1708 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1709 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1710 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1711 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1713 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1714 spin_lock_irqsave(&chip->voice_lock, flags);
1715 if (substream->runtime && substream->runtime->private_data) {
1716 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1717 ypcm->update_pcm_vol = 2;
1719 spin_unlock_irqrestore(&chip->voice_lock, flags);
1725 static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1726 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1727 .name = "PCM Playback Volume",
1728 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1729 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1730 .info = snd_ymfpci_pcm_vol_info,
1731 .get = snd_ymfpci_pcm_vol_get,
1732 .put = snd_ymfpci_pcm_vol_put,
1740 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1742 struct snd_ymfpci *chip = bus->private_data;
1743 chip->ac97_bus = NULL;
1746 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1748 struct snd_ymfpci *chip = ac97->private_data;
1752 int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1754 struct snd_ac97_template ac97;
1755 struct snd_kcontrol *kctl;
1756 struct snd_pcm_substream *substream;
1759 static struct snd_ac97_bus_ops ops = {
1760 .write = snd_ymfpci_codec_write,
1761 .read = snd_ymfpci_codec_read,
1764 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1766 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1767 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1769 memset(&ac97, 0, sizeof(ac97));
1770 ac97.private_data = chip;
1771 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1772 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1776 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1777 AC97_EA_VRA|AC97_EA_VRM, 0);
1779 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1780 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1784 /* add S/PDIF control */
1785 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1786 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1788 kctl->id.device = chip->pcm_spdif->device;
1789 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1791 kctl->id.device = chip->pcm_spdif->device;
1792 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1794 kctl->id.device = chip->pcm_spdif->device;
1795 chip->spdif_pcm_ctl = kctl;
1797 /* direct recording source */
1798 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1799 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1803 * shared rear/line-in
1806 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1810 /* per-voice volume */
1811 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1812 for (idx = 0; idx < 32; ++idx) {
1813 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1816 kctl->id.device = chip->pcm->device;
1817 kctl->id.subdevice = idx;
1818 kctl->private_value = (unsigned long)substream;
1819 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1821 chip->pcm_mixer[idx].left = 0x8000;
1822 chip->pcm_mixer[idx].right = 0x8000;
1823 chip->pcm_mixer[idx].ctl = kctl;
1824 substream = substream->next;
1835 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1837 struct snd_ymfpci *chip;
1838 unsigned long flags;
1841 chip = snd_timer_chip(timer);
1842 count = (timer->sticks << 1) - 1;
1843 spin_lock_irqsave(&chip->reg_lock, flags);
1844 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1845 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1846 spin_unlock_irqrestore(&chip->reg_lock, flags);
1850 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1852 struct snd_ymfpci *chip;
1853 unsigned long flags;
1855 chip = snd_timer_chip(timer);
1856 spin_lock_irqsave(&chip->reg_lock, flags);
1857 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1858 spin_unlock_irqrestore(&chip->reg_lock, flags);
1862 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1863 unsigned long *num, unsigned long *den)
1870 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1871 .flags = SNDRV_TIMER_HW_AUTO,
1872 .resolution = 20833, /* 1/fs = 20.8333...us */
1874 .start = snd_ymfpci_timer_start,
1875 .stop = snd_ymfpci_timer_stop,
1876 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1879 int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1881 struct snd_timer *timer = NULL;
1882 struct snd_timer_id tid;
1885 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1886 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1887 tid.card = chip->card->number;
1888 tid.device = device;
1890 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1891 strcpy(timer->name, "YMFPCI timer");
1892 timer->private_data = chip;
1893 timer->hw = snd_ymfpci_timer_hw;
1895 chip->timer = timer;
1904 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1905 struct snd_info_buffer *buffer)
1907 struct snd_ymfpci *chip = entry->private_data;
1910 snd_iprintf(buffer, "YMFPCI\n\n");
1911 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1912 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1915 static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1917 struct snd_info_entry *entry;
1919 if (! snd_card_proc_new(card, "ymfpci", &entry))
1920 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
1925 * initialization routines
1928 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1932 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1933 #if 0 // force to reset
1936 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1937 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1938 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1939 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1940 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1946 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1948 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1951 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1956 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1958 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1959 while (timeout-- > 0) {
1960 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1961 if ((val & 0x00000002) == 0)
1966 #define FIRMWARE_IN_THE_KERNEL
1968 #ifdef FIRMWARE_IN_THE_KERNEL
1970 #include "ymfpci_image.h"
1972 static struct firmware snd_ymfpci_dsp_microcode = {
1973 .size = YDSXG_DSPLENGTH,
1974 .data = (u8 *)DspInst,
1976 static struct firmware snd_ymfpci_controller_microcode = {
1977 .size = YDSXG_CTRLLENGTH,
1978 .data = (u8 *)CntrlInst,
1980 static struct firmware snd_ymfpci_controller_1e_microcode = {
1981 .size = YDSXG_CTRLLENGTH,
1982 .data = (u8 *)CntrlInst1E,
1986 #ifdef __LITTLE_ENDIAN
1987 static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
1989 static void snd_ymfpci_convert_from_le(const struct firmware *fw)
1992 u32 *data = (u32 *)fw->data;
1994 for (i = 0; i < fw->size / 4; ++i)
1995 le32_to_cpus(&data[i]);
1999 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2004 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2007 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2008 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2010 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2015 #ifdef FIRMWARE_IN_THE_KERNEL
2016 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2021 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2022 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2023 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2024 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2025 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2026 err = request_firmware(&chip->controller_microcode, name,
2029 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2030 snd_ymfpci_convert_from_le(chip->controller_microcode);
2032 snd_printk(KERN_ERR "controller microcode"
2033 " has wrong size\n");
2038 #ifdef FIRMWARE_IN_THE_KERNEL
2039 chip->controller_microcode =
2040 is_1e ? &snd_ymfpci_controller_1e_microcode
2041 : &snd_ymfpci_controller_microcode;
2049 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2055 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2056 snd_ymfpci_disable_dsp(chip);
2057 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2058 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2059 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2060 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2061 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2062 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2063 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2064 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2065 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2067 /* setup DSP instruction code */
2068 inst = (u32 *)chip->dsp_microcode->data;
2069 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2070 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
2072 /* setup control instruction code */
2073 inst = (u32 *)chip->controller_microcode->data;
2074 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2075 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2077 snd_ymfpci_enable_dsp(chip);
2080 static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2082 long size, playback_ctrl_size;
2083 int voice, bank, reg;
2085 dma_addr_t ptr_addr;
2087 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2088 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2089 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2090 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2091 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2093 size = ALIGN(playback_ctrl_size, 0x100) +
2094 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2095 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2096 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2098 /* work_ptr must be aligned to 256 bytes, but it's already
2099 covered with the kernel page allocation mechanism */
2100 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2101 size, &chip->work_ptr) < 0)
2103 ptr = chip->work_ptr.area;
2104 ptr_addr = chip->work_ptr.addr;
2105 memset(ptr, 0, size); /* for sure */
2107 chip->bank_base_playback = ptr;
2108 chip->bank_base_playback_addr = ptr_addr;
2109 chip->ctrl_playback = (u32 *)ptr;
2110 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2111 ptr += ALIGN(playback_ctrl_size, 0x100);
2112 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2113 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2114 chip->voices[voice].number = voice;
2115 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2116 chip->voices[voice].bank_addr = ptr_addr;
2117 for (bank = 0; bank < 2; bank++) {
2118 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2119 ptr += chip->bank_size_playback;
2120 ptr_addr += chip->bank_size_playback;
2123 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2124 ptr_addr = ALIGN(ptr_addr, 0x100);
2125 chip->bank_base_capture = ptr;
2126 chip->bank_base_capture_addr = ptr_addr;
2127 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2128 for (bank = 0; bank < 2; bank++) {
2129 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2130 ptr += chip->bank_size_capture;
2131 ptr_addr += chip->bank_size_capture;
2133 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2134 ptr_addr = ALIGN(ptr_addr, 0x100);
2135 chip->bank_base_effect = ptr;
2136 chip->bank_base_effect_addr = ptr_addr;
2137 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2138 for (bank = 0; bank < 2; bank++) {
2139 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2140 ptr += chip->bank_size_effect;
2141 ptr_addr += chip->bank_size_effect;
2143 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2144 ptr_addr = ALIGN(ptr_addr, 0x100);
2145 chip->work_base = ptr;
2146 chip->work_base_addr = ptr_addr;
2148 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2150 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2151 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2152 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2153 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2154 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2156 /* S/PDIF output initialization */
2157 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2158 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2159 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2161 /* S/PDIF input initialization */
2162 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2164 /* digital mixer setup */
2165 for (reg = 0x80; reg < 0xc0; reg += 4)
2166 snd_ymfpci_writel(chip, reg, 0);
2167 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2168 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2169 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2170 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2171 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2172 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2173 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2178 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2182 snd_assert(chip != NULL, return -EINVAL);
2184 if (chip->res_reg_area) { /* don't touch busy hardware */
2185 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2186 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2187 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2188 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2189 snd_ymfpci_disable_dsp(chip);
2190 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2191 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2192 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2193 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2194 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2195 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2196 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2199 snd_ymfpci_ac3_done(chip);
2201 /* Set PCI device to D3 state */
2203 /* FIXME: temporarily disabled, otherwise we cannot fire up
2204 * the chip again unless reboot. ACPI bug?
2206 pci_set_power_state(chip->pci, 3);
2210 vfree(chip->saved_regs);
2212 release_and_free_resource(chip->mpu_res);
2213 release_and_free_resource(chip->fm_res);
2214 snd_ymfpci_free_gameport(chip);
2215 if (chip->reg_area_virt)
2216 iounmap(chip->reg_area_virt);
2217 if (chip->work_ptr.area)
2218 snd_dma_free_pages(&chip->work_ptr);
2221 free_irq(chip->irq, chip);
2222 release_and_free_resource(chip->res_reg_area);
2224 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2226 pci_disable_device(chip->pci);
2227 #ifdef FIRMWARE_IN_THE_KERNEL
2228 if (chip->dsp_microcode != &snd_ymfpci_dsp_microcode)
2230 release_firmware(chip->dsp_microcode);
2231 #ifdef FIRMWARE_IN_THE_KERNEL
2232 if (chip->controller_microcode != &snd_ymfpci_controller_microcode &&
2233 chip->controller_microcode != &snd_ymfpci_controller_1e_microcode)
2235 release_firmware(chip->controller_microcode);
2240 static int snd_ymfpci_dev_free(struct snd_device *device)
2242 struct snd_ymfpci *chip = device->device_data;
2243 return snd_ymfpci_free(chip);
2247 static int saved_regs_index[] = {
2249 YDSXGR_SPDIFOUTCTRL,
2250 YDSXGR_SPDIFOUTSTATUS,
2253 YDSXGR_PRIADCLOOPVOL,
2254 YDSXGR_NATIVEDACINVOL,
2255 YDSXGR_NATIVEDACOUTVOL,
2256 // YDSXGR_BUF441OUTVOL,
2257 YDSXGR_NATIVEADCINVOL,
2258 YDSXGR_SPDIFLOOPVOL,
2261 YDSXGR_LEGACYOUTVOL,
2263 YDSXGR_PLAYCTRLBASE,
2267 /* capture set up */
2274 #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2276 int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
2278 struct snd_card *card = pci_get_drvdata(pci);
2279 struct snd_ymfpci *chip = card->private_data;
2282 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2283 snd_pcm_suspend_all(chip->pcm);
2284 snd_pcm_suspend_all(chip->pcm2);
2285 snd_pcm_suspend_all(chip->pcm_spdif);
2286 snd_pcm_suspend_all(chip->pcm_4ch);
2287 snd_ac97_suspend(chip->ac97);
2288 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2289 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2290 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2291 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2292 snd_ymfpci_disable_dsp(chip);
2293 pci_disable_device(pci);
2294 pci_save_state(pci);
2295 pci_set_power_state(pci, pci_choose_state(pci, state));
2299 int snd_ymfpci_resume(struct pci_dev *pci)
2301 struct snd_card *card = pci_get_drvdata(pci);
2302 struct snd_ymfpci *chip = card->private_data;
2305 pci_set_power_state(pci, PCI_D0);
2306 pci_restore_state(pci);
2307 if (pci_enable_device(pci) < 0) {
2308 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2309 "disabling device\n");
2310 snd_card_disconnect(card);
2313 pci_set_master(pci);
2314 snd_ymfpci_aclink_reset(pci);
2315 snd_ymfpci_codec_ready(chip, 0);
2316 snd_ymfpci_download_image(chip);
2319 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2320 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2322 snd_ac97_resume(chip->ac97);
2324 /* start hw again */
2325 if (chip->start_count > 0) {
2326 spin_lock_irq(&chip->reg_lock);
2327 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2328 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2329 spin_unlock_irq(&chip->reg_lock);
2331 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2334 #endif /* CONFIG_PM */
2336 int __devinit snd_ymfpci_create(struct snd_card *card,
2337 struct pci_dev * pci,
2338 unsigned short old_legacy_ctrl,
2339 struct snd_ymfpci ** rchip)
2341 struct snd_ymfpci *chip;
2343 static struct snd_device_ops ops = {
2344 .dev_free = snd_ymfpci_dev_free,
2349 /* enable PCI device */
2350 if ((err = pci_enable_device(pci)) < 0)
2353 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2355 pci_disable_device(pci);
2358 chip->old_legacy_ctrl = old_legacy_ctrl;
2359 spin_lock_init(&chip->reg_lock);
2360 spin_lock_init(&chip->voice_lock);
2361 init_waitqueue_head(&chip->interrupt_sleep);
2362 atomic_set(&chip->interrupt_sleep_count, 0);
2366 chip->device_id = pci->device;
2367 pci_read_config_byte(pci, PCI_REVISION_ID, &chip->rev);
2368 chip->reg_area_phys = pci_resource_start(pci, 0);
2369 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2370 pci_set_master(pci);
2372 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2373 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2374 snd_ymfpci_free(chip);
2377 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2379 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2380 snd_ymfpci_free(chip);
2383 chip->irq = pci->irq;
2385 snd_ymfpci_aclink_reset(pci);
2386 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2387 snd_ymfpci_free(chip);
2391 err = snd_ymfpci_request_firmware(chip);
2393 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2394 snd_ymfpci_free(chip);
2397 snd_ymfpci_download_image(chip);
2399 udelay(100); /* seems we need a delay after downloading image.. */
2401 if (snd_ymfpci_memalloc(chip) < 0) {
2402 snd_ymfpci_free(chip);
2406 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2407 snd_ymfpci_free(chip);
2412 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2413 if (chip->saved_regs == NULL) {
2414 snd_ymfpci_free(chip);
2419 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2420 snd_ymfpci_free(chip);
2424 snd_ymfpci_proc_init(card, chip);
2426 snd_card_set_dev(card, &pci->dev);