2 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/mutex.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 schedule_timeout_uninterruptible(1);
88 } while (time_before(jiffies, end_time));
89 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
93 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
95 struct snd_ymfpci *chip = ac97->private_data;
98 snd_ymfpci_codec_ready(chip, 0);
99 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
100 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
103 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
105 struct snd_ymfpci *chip = ac97->private_data;
107 if (snd_ymfpci_codec_ready(chip, 0))
109 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
110 if (snd_ymfpci_codec_ready(chip, 0))
112 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
114 for (i = 0; i < 600; i++)
115 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
124 static u32 snd_ymfpci_calc_delta(u32 rate)
127 case 8000: return 0x02aaab00;
128 case 11025: return 0x03accd00;
129 case 16000: return 0x05555500;
130 case 22050: return 0x07599a00;
131 case 32000: return 0x0aaaab00;
132 case 44100: return 0x0eb33300;
133 default: return ((rate << 16) / 375) << 5;
137 static u32 def_rate[8] = {
138 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
141 static u32 snd_ymfpci_calc_lpfK(u32 rate)
144 static u32 val[8] = {
145 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
146 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
150 return 0x40000000; /* FIXME: What's the right value? */
151 for (i = 0; i < 8; i++)
152 if (rate <= def_rate[i])
157 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
160 static u32 val[8] = {
161 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
162 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
167 for (i = 0; i < 8; i++)
168 if (rate <= def_rate[i])
174 * Hardware start management
177 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
181 spin_lock_irqsave(&chip->reg_lock, flags);
182 if (chip->start_count++ > 0)
184 snd_ymfpci_writel(chip, YDSXGR_MODE,
185 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
186 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
188 spin_unlock_irqrestore(&chip->reg_lock, flags);
191 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
196 spin_lock_irqsave(&chip->reg_lock, flags);
197 if (--chip->start_count > 0)
199 snd_ymfpci_writel(chip, YDSXGR_MODE,
200 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
201 while (timeout-- > 0) {
202 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
205 if (atomic_read(&chip->interrupt_sleep_count)) {
206 atomic_set(&chip->interrupt_sleep_count, 0);
207 wake_up(&chip->interrupt_sleep);
210 spin_unlock_irqrestore(&chip->reg_lock, flags);
214 * Playback voice management
217 static int voice_alloc(struct snd_ymfpci *chip,
218 enum snd_ymfpci_voice_type type, int pair,
219 struct snd_ymfpci_voice **rvoice)
221 struct snd_ymfpci_voice *voice, *voice2;
225 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
226 voice = &chip->voices[idx];
227 voice2 = pair ? &chip->voices[idx+1] : NULL;
228 if (voice->use || (voice2 && voice2->use))
246 snd_ymfpci_hw_start(chip);
248 snd_ymfpci_hw_start(chip);
255 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
256 enum snd_ymfpci_voice_type type, int pair,
257 struct snd_ymfpci_voice **rvoice)
262 if (snd_BUG_ON(!rvoice))
264 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
267 spin_lock_irqsave(&chip->voice_lock, flags);
269 result = voice_alloc(chip, type, pair, rvoice);
270 if (result == 0 || type != YMFPCI_PCM)
272 /* TODO: synth/midi voice deallocation */
275 spin_unlock_irqrestore(&chip->voice_lock, flags);
279 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
283 if (snd_BUG_ON(!pvoice))
285 snd_ymfpci_hw_stop(chip);
286 spin_lock_irqsave(&chip->voice_lock, flags);
287 if (pvoice->number == chip->src441_used) {
288 chip->src441_used = -1;
289 pvoice->ypcm->use_441_slot = 0;
291 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
293 pvoice->interrupt = NULL;
294 spin_unlock_irqrestore(&chip->voice_lock, flags);
302 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
304 struct snd_ymfpci_pcm *ypcm;
307 if ((ypcm = voice->ypcm) == NULL)
309 if (ypcm->substream == NULL)
311 spin_lock(&chip->reg_lock);
313 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
314 if (pos < ypcm->last_pos)
315 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
317 delta = pos - ypcm->last_pos;
318 ypcm->period_pos += delta;
319 ypcm->last_pos = pos;
320 if (ypcm->period_pos >= ypcm->period_size) {
321 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
322 ypcm->period_pos %= ypcm->period_size;
323 spin_unlock(&chip->reg_lock);
324 snd_pcm_period_elapsed(ypcm->substream);
325 spin_lock(&chip->reg_lock);
328 if (unlikely(ypcm->update_pcm_vol)) {
329 unsigned int subs = ypcm->substream->number;
330 unsigned int next_bank = 1 - chip->active_bank;
331 struct snd_ymfpci_playback_bank *bank;
334 bank = &voice->bank[next_bank];
335 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
336 bank->left_gain_end = volume;
337 if (ypcm->output_rear)
338 bank->eff2_gain_end = volume;
340 bank = &ypcm->voices[1]->bank[next_bank];
341 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
342 bank->right_gain_end = volume;
343 if (ypcm->output_rear)
344 bank->eff3_gain_end = volume;
345 ypcm->update_pcm_vol--;
348 spin_unlock(&chip->reg_lock);
351 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
355 struct snd_ymfpci *chip = ypcm->chip;
358 spin_lock(&chip->reg_lock);
360 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
361 if (pos < ypcm->last_pos)
362 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
364 delta = pos - ypcm->last_pos;
365 ypcm->period_pos += delta;
366 ypcm->last_pos = pos;
367 if (ypcm->period_pos >= ypcm->period_size) {
368 ypcm->period_pos %= ypcm->period_size;
369 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
370 spin_unlock(&chip->reg_lock);
371 snd_pcm_period_elapsed(substream);
372 spin_lock(&chip->reg_lock);
375 spin_unlock(&chip->reg_lock);
378 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
381 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
382 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
383 struct snd_kcontrol *kctl = NULL;
386 spin_lock(&chip->reg_lock);
387 if (ypcm->voices[0] == NULL) {
392 case SNDRV_PCM_TRIGGER_START:
393 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
394 case SNDRV_PCM_TRIGGER_RESUME:
395 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
396 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
397 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
400 case SNDRV_PCM_TRIGGER_STOP:
401 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
402 kctl = chip->pcm_mixer[substream->number].ctl;
403 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
406 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
407 case SNDRV_PCM_TRIGGER_SUSPEND:
408 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
409 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
410 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
418 spin_unlock(&chip->reg_lock);
420 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
423 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
426 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
427 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
431 spin_lock(&chip->reg_lock);
433 case SNDRV_PCM_TRIGGER_START:
434 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
435 case SNDRV_PCM_TRIGGER_RESUME:
436 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
437 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
440 case SNDRV_PCM_TRIGGER_STOP:
441 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
442 case SNDRV_PCM_TRIGGER_SUSPEND:
443 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
444 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
451 spin_unlock(&chip->reg_lock);
455 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
459 if (ypcm->voices[1] != NULL && voices < 2) {
460 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
461 ypcm->voices[1] = NULL;
463 if (voices == 1 && ypcm->voices[0] != NULL)
464 return 0; /* already allocated */
465 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
466 return 0; /* already allocated */
468 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
469 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
470 ypcm->voices[0] = NULL;
473 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
476 ypcm->voices[0]->ypcm = ypcm;
477 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
479 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
480 ypcm->voices[1]->ypcm = ypcm;
485 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
486 struct snd_pcm_runtime *runtime,
489 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
491 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
492 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
493 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
494 struct snd_ymfpci_playback_bank *bank;
496 u32 vol_left, vol_right;
497 u8 use_left, use_right;
500 if (snd_BUG_ON(!voice))
502 if (runtime->channels == 1) {
506 use_left = (voiceidx & 1) == 0;
507 use_right = !use_left;
509 if (has_pcm_volume) {
510 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
511 [ypcm->substream->number].left << 15);
512 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
513 [ypcm->substream->number].right << 15);
515 vol_left = cpu_to_le32(0x40000000);
516 vol_right = cpu_to_le32(0x40000000);
518 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
519 format = runtime->channels == 2 ? 0x00010000 : 0;
520 if (snd_pcm_format_width(runtime->format) == 8)
521 format |= 0x80000000;
522 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
523 runtime->rate == 44100 && runtime->channels == 2 &&
524 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
525 ypcm->chip->src441_used == voice->number)) {
526 ypcm->chip->src441_used = voice->number;
527 ypcm->use_441_slot = 1;
528 format |= 0x10000000;
530 if (ypcm->chip->src441_used == voice->number &&
531 (format & 0x10000000) == 0) {
532 ypcm->chip->src441_used = -1;
533 ypcm->use_441_slot = 0;
535 if (runtime->channels == 2 && (voiceidx & 1) != 0)
537 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
538 for (nbank = 0; nbank < 2; nbank++) {
539 bank = &voice->bank[nbank];
540 memset(bank, 0, sizeof(*bank));
541 bank->format = cpu_to_le32(format);
542 bank->base = cpu_to_le32(runtime->dma_addr);
543 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
544 bank->lpfQ = cpu_to_le32(lpfQ);
546 bank->delta_end = cpu_to_le32(delta);
548 bank->lpfK_end = cpu_to_le32(lpfK);
550 bank->eg_gain_end = cpu_to_le32(0x40000000);
552 if (ypcm->output_front) {
555 bank->left_gain_end = vol_left;
559 bank->right_gain_end = vol_right;
562 if (ypcm->output_rear) {
563 if (!ypcm->swap_rear) {
566 bank->eff2_gain_end = vol_left;
570 bank->eff3_gain_end = vol_right;
573 /* The SPDIF out channels seem to be swapped, so we have
574 * to swap them here, too. The rear analog out channels
575 * will be wrong, but otherwise AC3 would not work.
579 bank->eff3_gain_end = vol_left;
583 bank->eff2_gain_end = vol_right;
590 static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
592 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
593 4096, &chip->ac3_tmp_base) < 0)
596 chip->bank_effect[3][0]->base =
597 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
598 chip->bank_effect[3][0]->loop_end =
599 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
600 chip->bank_effect[4][0]->base =
601 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
602 chip->bank_effect[4][0]->loop_end =
603 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
605 spin_lock_irq(&chip->reg_lock);
606 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
607 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
608 spin_unlock_irq(&chip->reg_lock);
612 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
614 spin_lock_irq(&chip->reg_lock);
615 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
616 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
617 spin_unlock_irq(&chip->reg_lock);
618 // snd_ymfpci_irq_wait(chip);
619 if (chip->ac3_tmp_base.area) {
620 snd_dma_free_pages(&chip->ac3_tmp_base);
621 chip->ac3_tmp_base.area = NULL;
626 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
627 struct snd_pcm_hw_params *hw_params)
629 struct snd_pcm_runtime *runtime = substream->runtime;
630 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
633 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
635 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
640 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
642 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
643 struct snd_pcm_runtime *runtime = substream->runtime;
644 struct snd_ymfpci_pcm *ypcm;
646 if (runtime->private_data == NULL)
648 ypcm = runtime->private_data;
650 /* wait, until the PCI operations are not finished */
651 snd_ymfpci_irq_wait(chip);
652 snd_pcm_lib_free_pages(substream);
653 if (ypcm->voices[1]) {
654 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
655 ypcm->voices[1] = NULL;
657 if (ypcm->voices[0]) {
658 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
659 ypcm->voices[0] = NULL;
664 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
666 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
667 struct snd_pcm_runtime *runtime = substream->runtime;
668 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
669 struct snd_kcontrol *kctl;
672 ypcm->period_size = runtime->period_size;
673 ypcm->buffer_size = runtime->buffer_size;
674 ypcm->period_pos = 0;
676 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
677 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
678 substream->pcm == chip->pcm);
680 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
681 kctl = chip->pcm_mixer[substream->number].ctl;
682 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
683 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
688 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
689 struct snd_pcm_hw_params *hw_params)
691 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
694 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
696 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
698 /* wait, until the PCI operations are not finished */
699 snd_ymfpci_irq_wait(chip);
700 return snd_pcm_lib_free_pages(substream);
703 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
705 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
706 struct snd_pcm_runtime *runtime = substream->runtime;
707 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
708 struct snd_ymfpci_capture_bank * bank;
712 ypcm->period_size = runtime->period_size;
713 ypcm->buffer_size = runtime->buffer_size;
714 ypcm->period_pos = 0;
717 rate = ((48000 * 4096) / runtime->rate) - 1;
719 if (runtime->channels == 2) {
723 if (snd_pcm_format_width(runtime->format) == 8)
727 switch (ypcm->capture_bank_number) {
729 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
730 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
733 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
734 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
737 for (nbank = 0; nbank < 2; nbank++) {
738 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
739 bank->base = cpu_to_le32(runtime->dma_addr);
740 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
742 bank->num_of_loops = 0;
747 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
749 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
752 struct snd_ymfpci_voice *voice = ypcm->voices[0];
754 if (!(ypcm->running && voice))
756 return le32_to_cpu(voice->bank[chip->active_bank].start);
759 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
761 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
762 struct snd_pcm_runtime *runtime = substream->runtime;
763 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
767 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
770 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
775 while (loops-- > 0) {
776 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
778 init_waitqueue_entry(&wait, current);
779 add_wait_queue(&chip->interrupt_sleep, &wait);
780 atomic_inc(&chip->interrupt_sleep_count);
781 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
782 remove_wait_queue(&chip->interrupt_sleep, &wait);
786 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
788 struct snd_ymfpci *chip = dev_id;
789 u32 status, nvoice, mode;
790 struct snd_ymfpci_voice *voice;
792 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
793 if (status & 0x80000000) {
794 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
795 spin_lock(&chip->voice_lock);
796 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
797 voice = &chip->voices[nvoice];
798 if (voice->interrupt)
799 voice->interrupt(chip, voice);
801 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
802 if (chip->capture_substream[nvoice])
803 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
806 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
807 if (chip->effect_substream[nvoice])
808 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
811 spin_unlock(&chip->voice_lock);
812 spin_lock(&chip->reg_lock);
813 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
814 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
815 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
816 spin_unlock(&chip->reg_lock);
818 if (atomic_read(&chip->interrupt_sleep_count)) {
819 atomic_set(&chip->interrupt_sleep_count, 0);
820 wake_up(&chip->interrupt_sleep);
824 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
827 snd_timer_interrupt(chip->timer, chip->timer->sticks);
829 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
832 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
836 static struct snd_pcm_hardware snd_ymfpci_playback =
838 .info = (SNDRV_PCM_INFO_MMAP |
839 SNDRV_PCM_INFO_MMAP_VALID |
840 SNDRV_PCM_INFO_INTERLEAVED |
841 SNDRV_PCM_INFO_BLOCK_TRANSFER |
842 SNDRV_PCM_INFO_PAUSE |
843 SNDRV_PCM_INFO_RESUME),
844 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
845 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
850 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
851 .period_bytes_min = 64,
852 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
858 static struct snd_pcm_hardware snd_ymfpci_capture =
860 .info = (SNDRV_PCM_INFO_MMAP |
861 SNDRV_PCM_INFO_MMAP_VALID |
862 SNDRV_PCM_INFO_INTERLEAVED |
863 SNDRV_PCM_INFO_BLOCK_TRANSFER |
864 SNDRV_PCM_INFO_PAUSE |
865 SNDRV_PCM_INFO_RESUME),
866 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
867 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
872 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
873 .period_bytes_min = 64,
874 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
880 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
882 kfree(runtime->private_data);
885 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
887 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 struct snd_ymfpci_pcm *ypcm;
891 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
895 ypcm->type = PLAYBACK_VOICE;
896 ypcm->substream = substream;
897 runtime->hw = snd_ymfpci_playback;
898 runtime->private_data = ypcm;
899 runtime->private_free = snd_ymfpci_pcm_free_substream;
900 /* FIXME? True value is 256/48 = 5.33333 ms */
901 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
905 /* call with spinlock held */
906 static void ymfpci_open_extension(struct snd_ymfpci *chip)
908 if (! chip->rear_opened) {
909 if (! chip->spdif_opened) /* set AC3 */
910 snd_ymfpci_writel(chip, YDSXGR_MODE,
911 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
912 /* enable second codec (4CHEN) */
913 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
914 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
918 /* call with spinlock held */
919 static void ymfpci_close_extension(struct snd_ymfpci *chip)
921 if (! chip->rear_opened) {
922 if (! chip->spdif_opened)
923 snd_ymfpci_writel(chip, YDSXGR_MODE,
924 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
925 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
926 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
930 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
932 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
933 struct snd_pcm_runtime *runtime = substream->runtime;
934 struct snd_ymfpci_pcm *ypcm;
937 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
939 ypcm = runtime->private_data;
940 ypcm->output_front = 1;
941 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
943 spin_lock_irq(&chip->reg_lock);
944 if (ypcm->output_rear) {
945 ymfpci_open_extension(chip);
948 spin_unlock_irq(&chip->reg_lock);
952 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
954 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
955 struct snd_pcm_runtime *runtime = substream->runtime;
956 struct snd_ymfpci_pcm *ypcm;
959 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
961 ypcm = runtime->private_data;
962 ypcm->output_front = 0;
963 ypcm->output_rear = 1;
965 spin_lock_irq(&chip->reg_lock);
966 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
967 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
968 ymfpci_open_extension(chip);
969 chip->spdif_pcm_bits = chip->spdif_bits;
970 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
971 chip->spdif_opened++;
972 spin_unlock_irq(&chip->reg_lock);
974 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
975 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
976 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
980 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
982 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
983 struct snd_pcm_runtime *runtime = substream->runtime;
984 struct snd_ymfpci_pcm *ypcm;
987 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
989 ypcm = runtime->private_data;
990 ypcm->output_front = 0;
991 ypcm->output_rear = 1;
993 spin_lock_irq(&chip->reg_lock);
994 ymfpci_open_extension(chip);
996 spin_unlock_irq(&chip->reg_lock);
1000 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
1001 u32 capture_bank_number)
1003 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 struct snd_ymfpci_pcm *ypcm;
1007 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1011 ypcm->type = capture_bank_number + CAPTURE_REC;
1012 ypcm->substream = substream;
1013 ypcm->capture_bank_number = capture_bank_number;
1014 chip->capture_substream[capture_bank_number] = substream;
1015 runtime->hw = snd_ymfpci_capture;
1016 /* FIXME? True value is 256/48 = 5.33333 ms */
1017 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1018 runtime->private_data = ypcm;
1019 runtime->private_free = snd_ymfpci_pcm_free_substream;
1020 snd_ymfpci_hw_start(chip);
1024 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1026 return snd_ymfpci_capture_open(substream, 0);
1029 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1031 return snd_ymfpci_capture_open(substream, 1);
1034 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1039 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1041 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1042 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1044 spin_lock_irq(&chip->reg_lock);
1045 if (ypcm->output_rear && chip->rear_opened > 0) {
1046 chip->rear_opened--;
1047 ymfpci_close_extension(chip);
1049 spin_unlock_irq(&chip->reg_lock);
1050 return snd_ymfpci_playback_close_1(substream);
1053 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1055 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1057 spin_lock_irq(&chip->reg_lock);
1058 chip->spdif_opened = 0;
1059 ymfpci_close_extension(chip);
1060 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1061 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1062 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1063 spin_unlock_irq(&chip->reg_lock);
1064 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1065 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1066 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1067 return snd_ymfpci_playback_close_1(substream);
1070 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1072 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1074 spin_lock_irq(&chip->reg_lock);
1075 if (chip->rear_opened > 0) {
1076 chip->rear_opened--;
1077 ymfpci_close_extension(chip);
1079 spin_unlock_irq(&chip->reg_lock);
1080 return snd_ymfpci_playback_close_1(substream);
1083 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1085 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1086 struct snd_pcm_runtime *runtime = substream->runtime;
1087 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1090 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1091 snd_ymfpci_hw_stop(chip);
1096 static struct snd_pcm_ops snd_ymfpci_playback_ops = {
1097 .open = snd_ymfpci_playback_open,
1098 .close = snd_ymfpci_playback_close,
1099 .ioctl = snd_pcm_lib_ioctl,
1100 .hw_params = snd_ymfpci_playback_hw_params,
1101 .hw_free = snd_ymfpci_playback_hw_free,
1102 .prepare = snd_ymfpci_playback_prepare,
1103 .trigger = snd_ymfpci_playback_trigger,
1104 .pointer = snd_ymfpci_playback_pointer,
1107 static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1108 .open = snd_ymfpci_capture_rec_open,
1109 .close = snd_ymfpci_capture_close,
1110 .ioctl = snd_pcm_lib_ioctl,
1111 .hw_params = snd_ymfpci_capture_hw_params,
1112 .hw_free = snd_ymfpci_capture_hw_free,
1113 .prepare = snd_ymfpci_capture_prepare,
1114 .trigger = snd_ymfpci_capture_trigger,
1115 .pointer = snd_ymfpci_capture_pointer,
1118 int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1120 struct snd_pcm *pcm;
1125 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1127 pcm->private_data = chip;
1129 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1130 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1133 pcm->info_flags = 0;
1134 strcpy(pcm->name, "YMFPCI");
1137 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1138 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1145 static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1146 .open = snd_ymfpci_capture_ac97_open,
1147 .close = snd_ymfpci_capture_close,
1148 .ioctl = snd_pcm_lib_ioctl,
1149 .hw_params = snd_ymfpci_capture_hw_params,
1150 .hw_free = snd_ymfpci_capture_hw_free,
1151 .prepare = snd_ymfpci_capture_prepare,
1152 .trigger = snd_ymfpci_capture_trigger,
1153 .pointer = snd_ymfpci_capture_pointer,
1156 int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1158 struct snd_pcm *pcm;
1163 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1165 pcm->private_data = chip;
1167 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1170 pcm->info_flags = 0;
1171 sprintf(pcm->name, "YMFPCI - %s",
1172 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1175 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1176 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1183 static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1184 .open = snd_ymfpci_playback_spdif_open,
1185 .close = snd_ymfpci_playback_spdif_close,
1186 .ioctl = snd_pcm_lib_ioctl,
1187 .hw_params = snd_ymfpci_playback_hw_params,
1188 .hw_free = snd_ymfpci_playback_hw_free,
1189 .prepare = snd_ymfpci_playback_prepare,
1190 .trigger = snd_ymfpci_playback_trigger,
1191 .pointer = snd_ymfpci_playback_pointer,
1194 int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1196 struct snd_pcm *pcm;
1201 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1203 pcm->private_data = chip;
1205 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1208 pcm->info_flags = 0;
1209 strcpy(pcm->name, "YMFPCI - IEC958");
1210 chip->pcm_spdif = pcm;
1212 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1213 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1220 static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1221 .open = snd_ymfpci_playback_4ch_open,
1222 .close = snd_ymfpci_playback_4ch_close,
1223 .ioctl = snd_pcm_lib_ioctl,
1224 .hw_params = snd_ymfpci_playback_hw_params,
1225 .hw_free = snd_ymfpci_playback_hw_free,
1226 .prepare = snd_ymfpci_playback_prepare,
1227 .trigger = snd_ymfpci_playback_trigger,
1228 .pointer = snd_ymfpci_playback_pointer,
1231 int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1233 struct snd_pcm *pcm;
1238 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1240 pcm->private_data = chip;
1242 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1245 pcm->info_flags = 0;
1246 strcpy(pcm->name, "YMFPCI - Rear PCM");
1247 chip->pcm_4ch = pcm;
1249 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1250 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1257 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1259 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1264 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1265 struct snd_ctl_elem_value *ucontrol)
1267 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1269 spin_lock_irq(&chip->reg_lock);
1270 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1271 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1272 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1273 spin_unlock_irq(&chip->reg_lock);
1277 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1278 struct snd_ctl_elem_value *ucontrol)
1280 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1284 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1285 (ucontrol->value.iec958.status[1] << 8);
1286 spin_lock_irq(&chip->reg_lock);
1287 change = chip->spdif_bits != val;
1288 chip->spdif_bits = val;
1289 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1290 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1291 spin_unlock_irq(&chip->reg_lock);
1295 static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
1297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1298 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1299 .info = snd_ymfpci_spdif_default_info,
1300 .get = snd_ymfpci_spdif_default_get,
1301 .put = snd_ymfpci_spdif_default_put
1304 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1306 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1311 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1312 struct snd_ctl_elem_value *ucontrol)
1314 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1316 spin_lock_irq(&chip->reg_lock);
1317 ucontrol->value.iec958.status[0] = 0x3e;
1318 ucontrol->value.iec958.status[1] = 0xff;
1319 spin_unlock_irq(&chip->reg_lock);
1323 static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
1325 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1326 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1327 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1328 .info = snd_ymfpci_spdif_mask_info,
1329 .get = snd_ymfpci_spdif_mask_get,
1332 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1334 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1339 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
1342 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1344 spin_lock_irq(&chip->reg_lock);
1345 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1346 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1347 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1348 spin_unlock_irq(&chip->reg_lock);
1352 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1353 struct snd_ctl_elem_value *ucontrol)
1355 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1359 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1360 (ucontrol->value.iec958.status[1] << 8);
1361 spin_lock_irq(&chip->reg_lock);
1362 change = chip->spdif_pcm_bits != val;
1363 chip->spdif_pcm_bits = val;
1364 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1365 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1366 spin_unlock_irq(&chip->reg_lock);
1370 static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
1372 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1373 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1374 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1375 .info = snd_ymfpci_spdif_stream_info,
1376 .get = snd_ymfpci_spdif_stream_get,
1377 .put = snd_ymfpci_spdif_stream_put
1380 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1382 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1384 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1386 info->value.enumerated.items = 3;
1387 if (info->value.enumerated.item > 2)
1388 info->value.enumerated.item = 2;
1389 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1393 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1395 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1398 spin_lock_irq(&chip->reg_lock);
1399 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1400 spin_unlock_irq(&chip->reg_lock);
1402 value->value.enumerated.item[0] = 0;
1404 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1408 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1410 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1413 spin_lock_irq(&chip->reg_lock);
1414 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1415 if (value->value.enumerated.item[0] == 0)
1416 reg = old_reg & ~0x100;
1418 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1419 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1420 spin_unlock_irq(&chip->reg_lock);
1421 return reg != old_reg;
1424 static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
1425 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1427 .name = "Direct Recording Source",
1428 .info = snd_ymfpci_drec_source_info,
1429 .get = snd_ymfpci_drec_source_get,
1430 .put = snd_ymfpci_drec_source_put
1437 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1438 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1439 .info = snd_ymfpci_info_single, \
1440 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1441 .private_value = ((reg) | ((shift) << 16)) }
1443 #define snd_ymfpci_info_single snd_ctl_boolean_mono_info
1445 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
1448 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1449 int reg = kcontrol->private_value & 0xffff;
1450 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1451 unsigned int mask = 1;
1454 case YDSXGR_SPDIFOUTCTRL: break;
1455 case YDSXGR_SPDIFINCTRL: break;
1456 default: return -EINVAL;
1458 ucontrol->value.integer.value[0] =
1459 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1463 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_value *ucontrol)
1466 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1467 int reg = kcontrol->private_value & 0xffff;
1468 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1469 unsigned int mask = 1;
1471 unsigned int val, oval;
1474 case YDSXGR_SPDIFOUTCTRL: break;
1475 case YDSXGR_SPDIFINCTRL: break;
1476 default: return -EINVAL;
1478 val = (ucontrol->value.integer.value[0] & mask);
1480 spin_lock_irq(&chip->reg_lock);
1481 oval = snd_ymfpci_readl(chip, reg);
1482 val = (oval & ~(mask << shift)) | val;
1483 change = val != oval;
1484 snd_ymfpci_writel(chip, reg, val);
1485 spin_unlock_irq(&chip->reg_lock);
1489 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1491 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1492 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1493 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1494 .info = snd_ymfpci_info_double, \
1495 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1496 .private_value = reg, \
1497 .tlv = { .p = db_scale_native } }
1499 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1501 unsigned int reg = kcontrol->private_value;
1503 if (reg < 0x80 || reg >= 0xc0)
1505 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1507 uinfo->value.integer.min = 0;
1508 uinfo->value.integer.max = 16383;
1512 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1514 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1515 unsigned int reg = kcontrol->private_value;
1516 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1519 if (reg < 0x80 || reg >= 0xc0)
1521 spin_lock_irq(&chip->reg_lock);
1522 val = snd_ymfpci_readl(chip, reg);
1523 spin_unlock_irq(&chip->reg_lock);
1524 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1525 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1529 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1531 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1532 unsigned int reg = kcontrol->private_value;
1533 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1535 unsigned int val1, val2, oval;
1537 if (reg < 0x80 || reg >= 0xc0)
1539 val1 = ucontrol->value.integer.value[0] & mask;
1540 val2 = ucontrol->value.integer.value[1] & mask;
1541 val1 <<= shift_left;
1542 val2 <<= shift_right;
1543 spin_lock_irq(&chip->reg_lock);
1544 oval = snd_ymfpci_readl(chip, reg);
1545 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1546 change = val1 != oval;
1547 snd_ymfpci_writel(chip, reg, val1);
1548 spin_unlock_irq(&chip->reg_lock);
1552 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_value *ucontrol)
1555 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1556 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1557 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1559 unsigned int value, oval;
1561 value = ucontrol->value.integer.value[0] & 0x3fff;
1562 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1563 spin_lock_irq(&chip->reg_lock);
1564 oval = snd_ymfpci_readl(chip, reg);
1565 change = value != oval;
1566 snd_ymfpci_writel(chip, reg, value);
1567 snd_ymfpci_writel(chip, reg2, value);
1568 spin_unlock_irq(&chip->reg_lock);
1575 #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
1577 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1579 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1580 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1584 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1586 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1588 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1590 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1595 static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1598 .name = "Wave Playback Volume",
1599 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1600 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1601 .info = snd_ymfpci_info_double,
1602 .get = snd_ymfpci_get_double,
1603 .put = snd_ymfpci_put_nativedacvol,
1604 .private_value = YDSXGR_NATIVEDACOUTVOL,
1605 .tlv = { .p = db_scale_native },
1607 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1608 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1609 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1610 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1611 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1612 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1613 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1614 YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1615 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1616 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1617 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1618 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1619 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1620 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1621 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1623 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1624 .name = "4ch Duplication",
1625 .info = snd_ymfpci_info_dup4ch,
1626 .get = snd_ymfpci_get_dup4ch,
1627 .put = snd_ymfpci_put_dup4ch,
1636 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1639 unsigned long flags;
1641 spin_lock_irqsave(&chip->reg_lock, flags);
1642 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1643 reg &= ~(1 << (pin + 8));
1645 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1646 /* set the level mode for input line */
1647 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1648 mode &= ~(3 << (pin * 2));
1649 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1650 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1651 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1652 spin_unlock_irqrestore(&chip->reg_lock, flags);
1653 return (mode >> pin) & 1;
1656 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1659 unsigned long flags;
1661 spin_lock_irqsave(&chip->reg_lock, flags);
1662 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1664 reg &= ~(1 << (pin + 8));
1665 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1666 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1667 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1668 spin_unlock_irqrestore(&chip->reg_lock, flags);
1673 #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
1675 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1677 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1678 int pin = (int)kcontrol->private_value;
1679 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1683 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1685 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1686 int pin = (int)kcontrol->private_value;
1688 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1689 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1690 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1696 static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
1697 .name = "Shared Rear/Line-In Switch",
1698 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1699 .info = snd_ymfpci_gpio_sw_info,
1700 .get = snd_ymfpci_gpio_sw_get,
1701 .put = snd_ymfpci_gpio_sw_put,
1709 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_info *uinfo)
1712 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1714 uinfo->value.integer.min = 0;
1715 uinfo->value.integer.max = 0x8000;
1719 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
1722 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1723 unsigned int subs = kcontrol->id.subdevice;
1725 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1726 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1730 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1733 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1734 unsigned int subs = kcontrol->id.subdevice;
1735 struct snd_pcm_substream *substream;
1736 unsigned long flags;
1738 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1739 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1740 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1741 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1742 if (chip->pcm_mixer[subs].left > 0x8000)
1743 chip->pcm_mixer[subs].left = 0x8000;
1744 if (chip->pcm_mixer[subs].right > 0x8000)
1745 chip->pcm_mixer[subs].right = 0x8000;
1747 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1748 spin_lock_irqsave(&chip->voice_lock, flags);
1749 if (substream->runtime && substream->runtime->private_data) {
1750 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1751 if (!ypcm->use_441_slot)
1752 ypcm->update_pcm_vol = 2;
1754 spin_unlock_irqrestore(&chip->voice_lock, flags);
1760 static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1761 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1762 .name = "PCM Playback Volume",
1763 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1764 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1765 .info = snd_ymfpci_pcm_vol_info,
1766 .get = snd_ymfpci_pcm_vol_get,
1767 .put = snd_ymfpci_pcm_vol_put,
1775 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1777 struct snd_ymfpci *chip = bus->private_data;
1778 chip->ac97_bus = NULL;
1781 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1783 struct snd_ymfpci *chip = ac97->private_data;
1787 int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1789 struct snd_ac97_template ac97;
1790 struct snd_kcontrol *kctl;
1791 struct snd_pcm_substream *substream;
1794 static struct snd_ac97_bus_ops ops = {
1795 .write = snd_ymfpci_codec_write,
1796 .read = snd_ymfpci_codec_read,
1799 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1801 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1802 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1804 memset(&ac97, 0, sizeof(ac97));
1805 ac97.private_data = chip;
1806 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1807 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1811 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1812 AC97_EA_VRA|AC97_EA_VRM, 0);
1814 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1815 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1819 /* add S/PDIF control */
1820 if (snd_BUG_ON(!chip->pcm_spdif))
1822 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1824 kctl->id.device = chip->pcm_spdif->device;
1825 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1827 kctl->id.device = chip->pcm_spdif->device;
1828 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1830 kctl->id.device = chip->pcm_spdif->device;
1831 chip->spdif_pcm_ctl = kctl;
1833 /* direct recording source */
1834 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1835 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1839 * shared rear/line-in
1842 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1846 /* per-voice volume */
1847 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1848 for (idx = 0; idx < 32; ++idx) {
1849 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1852 kctl->id.device = chip->pcm->device;
1853 kctl->id.subdevice = idx;
1854 kctl->private_value = (unsigned long)substream;
1855 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1857 chip->pcm_mixer[idx].left = 0x8000;
1858 chip->pcm_mixer[idx].right = 0x8000;
1859 chip->pcm_mixer[idx].ctl = kctl;
1860 substream = substream->next;
1871 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1873 struct snd_ymfpci *chip;
1874 unsigned long flags;
1877 chip = snd_timer_chip(timer);
1878 count = (timer->sticks << 1) - 1;
1879 spin_lock_irqsave(&chip->reg_lock, flags);
1880 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1881 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1882 spin_unlock_irqrestore(&chip->reg_lock, flags);
1886 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1888 struct snd_ymfpci *chip;
1889 unsigned long flags;
1891 chip = snd_timer_chip(timer);
1892 spin_lock_irqsave(&chip->reg_lock, flags);
1893 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1894 spin_unlock_irqrestore(&chip->reg_lock, flags);
1898 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1899 unsigned long *num, unsigned long *den)
1906 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1907 .flags = SNDRV_TIMER_HW_AUTO,
1908 .resolution = 20833, /* 1/fs = 20.8333...us */
1910 .start = snd_ymfpci_timer_start,
1911 .stop = snd_ymfpci_timer_stop,
1912 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1915 int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1917 struct snd_timer *timer = NULL;
1918 struct snd_timer_id tid;
1921 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1922 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1923 tid.card = chip->card->number;
1924 tid.device = device;
1926 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1927 strcpy(timer->name, "YMFPCI timer");
1928 timer->private_data = chip;
1929 timer->hw = snd_ymfpci_timer_hw;
1931 chip->timer = timer;
1940 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1941 struct snd_info_buffer *buffer)
1943 struct snd_ymfpci *chip = entry->private_data;
1946 snd_iprintf(buffer, "YMFPCI\n\n");
1947 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1948 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1951 static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1953 struct snd_info_entry *entry;
1955 if (! snd_card_proc_new(card, "ymfpci", &entry))
1956 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
1961 * initialization routines
1964 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1968 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1969 #if 0 // force to reset
1972 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1973 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1974 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1975 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1976 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1982 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1984 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1987 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1992 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1994 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1995 while (timeout-- > 0) {
1996 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1997 if ((val & 0x00000002) == 0)
2002 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2007 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2010 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2011 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2017 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2018 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2019 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2020 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2021 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2022 err = request_firmware(&chip->controller_microcode, name,
2025 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2026 snd_printk(KERN_ERR "controller microcode"
2027 " has wrong size\n");
2036 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2037 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2038 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2040 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2046 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2047 snd_ymfpci_disable_dsp(chip);
2048 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2049 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2050 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2051 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2052 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2053 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2054 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2055 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2056 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2058 /* setup DSP instruction code */
2059 inst = (const __le32 *)chip->dsp_microcode->data;
2060 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2061 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2062 le32_to_cpu(inst[i]));
2064 /* setup control instruction code */
2065 inst = (const __le32 *)chip->controller_microcode->data;
2066 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2067 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2068 le32_to_cpu(inst[i]));
2070 snd_ymfpci_enable_dsp(chip);
2073 static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2075 long size, playback_ctrl_size;
2076 int voice, bank, reg;
2078 dma_addr_t ptr_addr;
2080 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2081 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2082 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2083 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2084 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2086 size = ALIGN(playback_ctrl_size, 0x100) +
2087 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2088 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2089 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2091 /* work_ptr must be aligned to 256 bytes, but it's already
2092 covered with the kernel page allocation mechanism */
2093 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2094 size, &chip->work_ptr) < 0)
2096 ptr = chip->work_ptr.area;
2097 ptr_addr = chip->work_ptr.addr;
2098 memset(ptr, 0, size); /* for sure */
2100 chip->bank_base_playback = ptr;
2101 chip->bank_base_playback_addr = ptr_addr;
2102 chip->ctrl_playback = (u32 *)ptr;
2103 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2104 ptr += ALIGN(playback_ctrl_size, 0x100);
2105 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2106 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2107 chip->voices[voice].number = voice;
2108 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2109 chip->voices[voice].bank_addr = ptr_addr;
2110 for (bank = 0; bank < 2; bank++) {
2111 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2112 ptr += chip->bank_size_playback;
2113 ptr_addr += chip->bank_size_playback;
2116 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2117 ptr_addr = ALIGN(ptr_addr, 0x100);
2118 chip->bank_base_capture = ptr;
2119 chip->bank_base_capture_addr = ptr_addr;
2120 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2121 for (bank = 0; bank < 2; bank++) {
2122 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2123 ptr += chip->bank_size_capture;
2124 ptr_addr += chip->bank_size_capture;
2126 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2127 ptr_addr = ALIGN(ptr_addr, 0x100);
2128 chip->bank_base_effect = ptr;
2129 chip->bank_base_effect_addr = ptr_addr;
2130 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2131 for (bank = 0; bank < 2; bank++) {
2132 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2133 ptr += chip->bank_size_effect;
2134 ptr_addr += chip->bank_size_effect;
2136 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2137 ptr_addr = ALIGN(ptr_addr, 0x100);
2138 chip->work_base = ptr;
2139 chip->work_base_addr = ptr_addr;
2141 snd_BUG_ON(ptr + chip->work_size !=
2142 chip->work_ptr.area + chip->work_ptr.bytes);
2144 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2145 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2146 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2147 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2148 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2150 /* S/PDIF output initialization */
2151 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2152 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2153 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2155 /* S/PDIF input initialization */
2156 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2158 /* digital mixer setup */
2159 for (reg = 0x80; reg < 0xc0; reg += 4)
2160 snd_ymfpci_writel(chip, reg, 0);
2161 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2162 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2163 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2164 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2165 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2166 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2167 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2168 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2173 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2177 if (snd_BUG_ON(!chip))
2180 if (chip->res_reg_area) { /* don't touch busy hardware */
2181 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2182 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2183 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2184 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2185 snd_ymfpci_disable_dsp(chip);
2186 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2187 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2188 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2189 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2190 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2191 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2192 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2195 snd_ymfpci_ac3_done(chip);
2197 /* Set PCI device to D3 state */
2199 /* FIXME: temporarily disabled, otherwise we cannot fire up
2200 * the chip again unless reboot. ACPI bug?
2202 pci_set_power_state(chip->pci, 3);
2206 vfree(chip->saved_regs);
2209 free_irq(chip->irq, chip);
2210 release_and_free_resource(chip->mpu_res);
2211 release_and_free_resource(chip->fm_res);
2212 snd_ymfpci_free_gameport(chip);
2213 if (chip->reg_area_virt)
2214 iounmap(chip->reg_area_virt);
2215 if (chip->work_ptr.area)
2216 snd_dma_free_pages(&chip->work_ptr);
2218 release_and_free_resource(chip->res_reg_area);
2220 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2222 pci_disable_device(chip->pci);
2223 release_firmware(chip->dsp_microcode);
2224 release_firmware(chip->controller_microcode);
2229 static int snd_ymfpci_dev_free(struct snd_device *device)
2231 struct snd_ymfpci *chip = device->device_data;
2232 return snd_ymfpci_free(chip);
2236 static int saved_regs_index[] = {
2238 YDSXGR_SPDIFOUTCTRL,
2239 YDSXGR_SPDIFOUTSTATUS,
2242 YDSXGR_PRIADCLOOPVOL,
2243 YDSXGR_NATIVEDACINVOL,
2244 YDSXGR_NATIVEDACOUTVOL,
2245 YDSXGR_BUF441OUTVOL,
2246 YDSXGR_NATIVEADCINVOL,
2247 YDSXGR_SPDIFLOOPVOL,
2250 YDSXGR_LEGACYOUTVOL,
2252 YDSXGR_PLAYCTRLBASE,
2256 /* capture set up */
2263 #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2265 int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
2267 struct snd_card *card = pci_get_drvdata(pci);
2268 struct snd_ymfpci *chip = card->private_data;
2271 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2272 snd_pcm_suspend_all(chip->pcm);
2273 snd_pcm_suspend_all(chip->pcm2);
2274 snd_pcm_suspend_all(chip->pcm_spdif);
2275 snd_pcm_suspend_all(chip->pcm_4ch);
2276 snd_ac97_suspend(chip->ac97);
2277 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2278 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2279 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2280 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2281 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2282 snd_ymfpci_disable_dsp(chip);
2283 pci_disable_device(pci);
2284 pci_save_state(pci);
2285 pci_set_power_state(pci, pci_choose_state(pci, state));
2289 int snd_ymfpci_resume(struct pci_dev *pci)
2291 struct snd_card *card = pci_get_drvdata(pci);
2292 struct snd_ymfpci *chip = card->private_data;
2295 pci_set_power_state(pci, PCI_D0);
2296 pci_restore_state(pci);
2297 if (pci_enable_device(pci) < 0) {
2298 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2299 "disabling device\n");
2300 snd_card_disconnect(card);
2303 pci_set_master(pci);
2304 snd_ymfpci_aclink_reset(pci);
2305 snd_ymfpci_codec_ready(chip, 0);
2306 snd_ymfpci_download_image(chip);
2309 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2310 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2312 snd_ac97_resume(chip->ac97);
2314 /* start hw again */
2315 if (chip->start_count > 0) {
2316 spin_lock_irq(&chip->reg_lock);
2317 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2318 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2319 spin_unlock_irq(&chip->reg_lock);
2321 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2324 #endif /* CONFIG_PM */
2326 int __devinit snd_ymfpci_create(struct snd_card *card,
2327 struct pci_dev * pci,
2328 unsigned short old_legacy_ctrl,
2329 struct snd_ymfpci ** rchip)
2331 struct snd_ymfpci *chip;
2333 static struct snd_device_ops ops = {
2334 .dev_free = snd_ymfpci_dev_free,
2339 /* enable PCI device */
2340 if ((err = pci_enable_device(pci)) < 0)
2343 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2345 pci_disable_device(pci);
2348 chip->old_legacy_ctrl = old_legacy_ctrl;
2349 spin_lock_init(&chip->reg_lock);
2350 spin_lock_init(&chip->voice_lock);
2351 init_waitqueue_head(&chip->interrupt_sleep);
2352 atomic_set(&chip->interrupt_sleep_count, 0);
2356 chip->device_id = pci->device;
2357 chip->rev = pci->revision;
2358 chip->reg_area_phys = pci_resource_start(pci, 0);
2359 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2360 pci_set_master(pci);
2361 chip->src441_used = -1;
2363 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2364 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2365 snd_ymfpci_free(chip);
2368 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2370 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2371 snd_ymfpci_free(chip);
2374 chip->irq = pci->irq;
2376 snd_ymfpci_aclink_reset(pci);
2377 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2378 snd_ymfpci_free(chip);
2382 err = snd_ymfpci_request_firmware(chip);
2384 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2385 snd_ymfpci_free(chip);
2388 snd_ymfpci_download_image(chip);
2390 udelay(100); /* seems we need a delay after downloading image.. */
2392 if (snd_ymfpci_memalloc(chip) < 0) {
2393 snd_ymfpci_free(chip);
2397 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2398 snd_ymfpci_free(chip);
2403 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2404 if (chip->saved_regs == NULL) {
2405 snd_ymfpci_free(chip);
2410 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2411 snd_ymfpci_free(chip);
2415 snd_ymfpci_proc_init(card, chip);
2417 snd_card_set_dev(card, &pci->dev);