1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Routines for control of YMF724/740/744/754 chips
7 #include <linux/delay.h>
8 #include <linux/firmware.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/pci.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/tlv.h>
23 #include <sound/asoundef.h>
24 #include <sound/mpu401.h>
26 #include <asm/byteorder.h>
32 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
34 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
36 return readb(chip->reg_area_virt + offset);
39 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
41 writeb(val, chip->reg_area_virt + offset);
44 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
46 return readw(chip->reg_area_virt + offset);
49 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
51 writew(val, chip->reg_area_virt + offset);
54 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
56 return readl(chip->reg_area_virt + offset);
59 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
61 writel(val, chip->reg_area_virt + offset);
64 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
66 unsigned long end_time;
67 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
69 end_time = jiffies + msecs_to_jiffies(750);
71 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
73 schedule_timeout_uninterruptible(1);
74 } while (time_before(jiffies, end_time));
75 dev_err(chip->card->dev,
76 "codec_ready: codec %i is not ready [0x%x]\n",
77 secondary, snd_ymfpci_readw(chip, reg));
81 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
83 struct snd_ymfpci *chip = ac97->private_data;
86 snd_ymfpci_codec_ready(chip, 0);
87 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
88 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
91 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
93 struct snd_ymfpci *chip = ac97->private_data;
95 if (snd_ymfpci_codec_ready(chip, 0))
97 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
98 if (snd_ymfpci_codec_ready(chip, 0))
100 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
102 for (i = 0; i < 600; i++)
103 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
105 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
112 static u32 snd_ymfpci_calc_delta(u32 rate)
115 case 8000: return 0x02aaab00;
116 case 11025: return 0x03accd00;
117 case 16000: return 0x05555500;
118 case 22050: return 0x07599a00;
119 case 32000: return 0x0aaaab00;
120 case 44100: return 0x0eb33300;
121 default: return ((rate << 16) / 375) << 5;
125 static const u32 def_rate[8] = {
126 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
129 static u32 snd_ymfpci_calc_lpfK(u32 rate)
132 static const u32 val[8] = {
133 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
134 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
138 return 0x40000000; /* FIXME: What's the right value? */
139 for (i = 0; i < 8; i++)
140 if (rate <= def_rate[i])
145 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
148 static const u32 val[8] = {
149 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
150 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
155 for (i = 0; i < 8; i++)
156 if (rate <= def_rate[i])
162 * Hardware start management
165 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
169 spin_lock_irqsave(&chip->reg_lock, flags);
170 if (chip->start_count++ > 0)
172 snd_ymfpci_writel(chip, YDSXGR_MODE,
173 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
174 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
176 spin_unlock_irqrestore(&chip->reg_lock, flags);
179 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
184 spin_lock_irqsave(&chip->reg_lock, flags);
185 if (--chip->start_count > 0)
187 snd_ymfpci_writel(chip, YDSXGR_MODE,
188 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
189 while (timeout-- > 0) {
190 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
193 if (atomic_read(&chip->interrupt_sleep_count)) {
194 atomic_set(&chip->interrupt_sleep_count, 0);
195 wake_up(&chip->interrupt_sleep);
198 spin_unlock_irqrestore(&chip->reg_lock, flags);
202 * Playback voice management
205 static int voice_alloc(struct snd_ymfpci *chip,
206 enum snd_ymfpci_voice_type type, int pair,
207 struct snd_ymfpci_voice **rvoice)
209 struct snd_ymfpci_voice *voice, *voice2;
213 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
214 voice = &chip->voices[idx];
215 voice2 = pair ? &chip->voices[idx+1] : NULL;
216 if (voice->use || (voice2 && voice2->use))
234 snd_ymfpci_hw_start(chip);
236 snd_ymfpci_hw_start(chip);
243 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
244 enum snd_ymfpci_voice_type type, int pair,
245 struct snd_ymfpci_voice **rvoice)
250 if (snd_BUG_ON(!rvoice))
252 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
255 spin_lock_irqsave(&chip->voice_lock, flags);
257 result = voice_alloc(chip, type, pair, rvoice);
258 if (result == 0 || type != YMFPCI_PCM)
260 /* TODO: synth/midi voice deallocation */
263 spin_unlock_irqrestore(&chip->voice_lock, flags);
267 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
271 if (snd_BUG_ON(!pvoice))
273 snd_ymfpci_hw_stop(chip);
274 spin_lock_irqsave(&chip->voice_lock, flags);
275 if (pvoice->number == chip->src441_used) {
276 chip->src441_used = -1;
277 pvoice->ypcm->use_441_slot = 0;
279 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
281 pvoice->interrupt = NULL;
282 spin_unlock_irqrestore(&chip->voice_lock, flags);
290 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
292 struct snd_ymfpci_pcm *ypcm;
298 if (ypcm->substream == NULL)
300 spin_lock(&chip->reg_lock);
302 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
303 if (pos < ypcm->last_pos)
304 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
306 delta = pos - ypcm->last_pos;
307 ypcm->period_pos += delta;
308 ypcm->last_pos = pos;
309 if (ypcm->period_pos >= ypcm->period_size) {
311 dev_dbg(chip->card->dev,
312 "done - active_bank = 0x%x, start = 0x%x\n",
314 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;
364 dev_dbg(chip->card->dev,
365 "done - active_bank = 0x%x, start = 0x%x\n",
367 voice->bank[chip->active_bank].start);
369 spin_unlock(&chip->reg_lock);
370 snd_pcm_period_elapsed(substream);
371 spin_lock(&chip->reg_lock);
374 spin_unlock(&chip->reg_lock);
377 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
380 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
381 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
382 struct snd_kcontrol *kctl = NULL;
385 spin_lock(&chip->reg_lock);
386 if (ypcm->voices[0] == NULL) {
391 case SNDRV_PCM_TRIGGER_START:
392 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
393 case SNDRV_PCM_TRIGGER_RESUME:
394 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
395 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
396 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
399 case SNDRV_PCM_TRIGGER_STOP:
400 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
401 kctl = chip->pcm_mixer[substream->number].ctl;
402 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
405 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
406 case SNDRV_PCM_TRIGGER_SUSPEND:
407 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
408 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
409 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
417 spin_unlock(&chip->reg_lock);
419 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
422 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
425 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
426 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
430 spin_lock(&chip->reg_lock);
432 case SNDRV_PCM_TRIGGER_START:
433 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
434 case SNDRV_PCM_TRIGGER_RESUME:
435 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
436 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
439 case SNDRV_PCM_TRIGGER_STOP:
440 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
441 case SNDRV_PCM_TRIGGER_SUSPEND:
442 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
443 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
450 spin_unlock(&chip->reg_lock);
454 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
458 if (ypcm->voices[1] != NULL && voices < 2) {
459 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
460 ypcm->voices[1] = NULL;
462 if (voices == 1 && ypcm->voices[0] != NULL)
463 return 0; /* already allocated */
464 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
465 return 0; /* already allocated */
467 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
468 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
469 ypcm->voices[0] = NULL;
472 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
475 ypcm->voices[0]->ypcm = ypcm;
476 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
478 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
479 ypcm->voices[1]->ypcm = ypcm;
484 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
485 struct snd_pcm_runtime *runtime,
488 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
490 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
491 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
492 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
493 struct snd_ymfpci_playback_bank *bank;
495 __le32 vol_left, vol_right;
496 u8 use_left, use_right;
499 if (snd_BUG_ON(!voice))
501 if (runtime->channels == 1) {
505 use_left = (voiceidx & 1) == 0;
506 use_right = !use_left;
508 if (has_pcm_volume) {
509 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
510 [ypcm->substream->number].left << 15);
511 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
512 [ypcm->substream->number].right << 15);
514 vol_left = cpu_to_le32(0x40000000);
515 vol_right = cpu_to_le32(0x40000000);
517 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
518 format = runtime->channels == 2 ? 0x00010000 : 0;
519 if (snd_pcm_format_width(runtime->format) == 8)
520 format |= 0x80000000;
521 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
522 runtime->rate == 44100 && runtime->channels == 2 &&
523 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
524 ypcm->chip->src441_used == voice->number)) {
525 ypcm->chip->src441_used = voice->number;
526 ypcm->use_441_slot = 1;
527 format |= 0x10000000;
529 if (ypcm->chip->src441_used == voice->number &&
530 (format & 0x10000000) == 0) {
531 ypcm->chip->src441_used = -1;
532 ypcm->use_441_slot = 0;
534 if (runtime->channels == 2 && (voiceidx & 1) != 0)
536 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
537 for (nbank = 0; nbank < 2; nbank++) {
538 bank = &voice->bank[nbank];
539 memset(bank, 0, sizeof(*bank));
540 bank->format = cpu_to_le32(format);
541 bank->base = cpu_to_le32(runtime->dma_addr);
542 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
543 bank->lpfQ = cpu_to_le32(lpfQ);
545 bank->delta_end = cpu_to_le32(delta);
547 bank->lpfK_end = cpu_to_le32(lpfK);
549 bank->eg_gain_end = cpu_to_le32(0x40000000);
551 if (ypcm->output_front) {
554 bank->left_gain_end = vol_left;
558 bank->right_gain_end = vol_right;
561 if (ypcm->output_rear) {
562 if (!ypcm->swap_rear) {
565 bank->eff2_gain_end = vol_left;
569 bank->eff3_gain_end = vol_right;
572 /* The SPDIF out channels seem to be swapped, so we have
573 * to swap them here, too. The rear analog out channels
574 * will be wrong, but otherwise AC3 would not work.
578 bank->eff3_gain_end = vol_left;
582 bank->eff2_gain_end = vol_right;
589 static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
591 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
592 4096, &chip->ac3_tmp_base) < 0)
595 chip->bank_effect[3][0]->base =
596 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
597 chip->bank_effect[3][0]->loop_end =
598 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
599 chip->bank_effect[4][0]->base =
600 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
601 chip->bank_effect[4][0]->loop_end =
602 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
604 spin_lock_irq(&chip->reg_lock);
605 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
606 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
607 spin_unlock_irq(&chip->reg_lock);
611 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
613 spin_lock_irq(&chip->reg_lock);
614 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
615 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
616 spin_unlock_irq(&chip->reg_lock);
617 // snd_ymfpci_irq_wait(chip);
618 if (chip->ac3_tmp_base.area) {
619 snd_dma_free_pages(&chip->ac3_tmp_base);
620 chip->ac3_tmp_base.area = NULL;
625 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
626 struct snd_pcm_hw_params *hw_params)
628 struct snd_pcm_runtime *runtime = substream->runtime;
629 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
632 err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params));
638 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
640 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
641 struct snd_pcm_runtime *runtime = substream->runtime;
642 struct snd_ymfpci_pcm *ypcm;
644 if (runtime->private_data == NULL)
646 ypcm = runtime->private_data;
648 /* wait, until the PCI operations are not finished */
649 snd_ymfpci_irq_wait(chip);
650 if (ypcm->voices[1]) {
651 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
652 ypcm->voices[1] = NULL;
654 if (ypcm->voices[0]) {
655 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
656 ypcm->voices[0] = NULL;
661 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
663 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
666 struct snd_kcontrol *kctl;
669 ypcm->period_size = runtime->period_size;
670 ypcm->buffer_size = runtime->buffer_size;
671 ypcm->period_pos = 0;
673 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
674 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
675 substream->pcm == chip->pcm);
677 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
678 kctl = chip->pcm_mixer[substream->number].ctl;
679 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
680 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
685 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
687 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
689 /* wait, until the PCI operations are not finished */
690 snd_ymfpci_irq_wait(chip);
694 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
696 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
697 struct snd_pcm_runtime *runtime = substream->runtime;
698 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
699 struct snd_ymfpci_capture_bank * bank;
703 ypcm->period_size = runtime->period_size;
704 ypcm->buffer_size = runtime->buffer_size;
705 ypcm->period_pos = 0;
708 rate = ((48000 * 4096) / runtime->rate) - 1;
710 if (runtime->channels == 2) {
714 if (snd_pcm_format_width(runtime->format) == 8)
718 switch (ypcm->capture_bank_number) {
720 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
721 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
724 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
725 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
728 for (nbank = 0; nbank < 2; nbank++) {
729 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
730 bank->base = cpu_to_le32(runtime->dma_addr);
731 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
733 bank->num_of_loops = 0;
738 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
740 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
741 struct snd_pcm_runtime *runtime = substream->runtime;
742 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
743 struct snd_ymfpci_voice *voice = ypcm->voices[0];
745 if (!(ypcm->running && voice))
747 return le32_to_cpu(voice->bank[chip->active_bank].start);
750 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
752 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
753 struct snd_pcm_runtime *runtime = substream->runtime;
754 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
758 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
761 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
763 wait_queue_entry_t wait;
766 while (loops-- > 0) {
767 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
769 init_waitqueue_entry(&wait, current);
770 add_wait_queue(&chip->interrupt_sleep, &wait);
771 atomic_inc(&chip->interrupt_sleep_count);
772 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
773 remove_wait_queue(&chip->interrupt_sleep, &wait);
777 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
779 struct snd_ymfpci *chip = dev_id;
780 u32 status, nvoice, mode;
781 struct snd_ymfpci_voice *voice;
783 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
784 if (status & 0x80000000) {
785 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
786 spin_lock(&chip->voice_lock);
787 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
788 voice = &chip->voices[nvoice];
789 if (voice->interrupt)
790 voice->interrupt(chip, voice);
792 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
793 if (chip->capture_substream[nvoice])
794 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
797 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
798 if (chip->effect_substream[nvoice])
799 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
802 spin_unlock(&chip->voice_lock);
803 spin_lock(&chip->reg_lock);
804 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
805 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
806 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
807 spin_unlock(&chip->reg_lock);
809 if (atomic_read(&chip->interrupt_sleep_count)) {
810 atomic_set(&chip->interrupt_sleep_count, 0);
811 wake_up(&chip->interrupt_sleep);
815 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
818 snd_timer_interrupt(chip->timer, chip->timer_ticks);
820 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
823 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
827 static const struct snd_pcm_hardware snd_ymfpci_playback =
829 .info = (SNDRV_PCM_INFO_MMAP |
830 SNDRV_PCM_INFO_MMAP_VALID |
831 SNDRV_PCM_INFO_INTERLEAVED |
832 SNDRV_PCM_INFO_BLOCK_TRANSFER |
833 SNDRV_PCM_INFO_PAUSE |
834 SNDRV_PCM_INFO_RESUME),
835 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
836 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
841 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
842 .period_bytes_min = 64,
843 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
849 static const struct snd_pcm_hardware snd_ymfpci_capture =
851 .info = (SNDRV_PCM_INFO_MMAP |
852 SNDRV_PCM_INFO_MMAP_VALID |
853 SNDRV_PCM_INFO_INTERLEAVED |
854 SNDRV_PCM_INFO_BLOCK_TRANSFER |
855 SNDRV_PCM_INFO_PAUSE |
856 SNDRV_PCM_INFO_RESUME),
857 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
858 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
863 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
864 .period_bytes_min = 64,
865 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
871 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
873 kfree(runtime->private_data);
876 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
878 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
879 struct snd_pcm_runtime *runtime = substream->runtime;
880 struct snd_ymfpci_pcm *ypcm;
883 runtime->hw = snd_ymfpci_playback;
884 /* FIXME? True value is 256/48 = 5.33333 ms */
885 err = snd_pcm_hw_constraint_minmax(runtime,
886 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
890 err = snd_pcm_hw_rule_noresample(runtime, 48000);
894 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
898 ypcm->type = PLAYBACK_VOICE;
899 ypcm->substream = substream;
900 runtime->private_data = ypcm;
901 runtime->private_free = snd_ymfpci_pcm_free_substream;
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 err = snd_ymfpci_playback_open_1(substream);
940 ypcm = runtime->private_data;
941 ypcm->output_front = 1;
942 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
944 spin_lock_irq(&chip->reg_lock);
945 if (ypcm->output_rear) {
946 ymfpci_open_extension(chip);
949 spin_unlock_irq(&chip->reg_lock);
953 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
955 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
956 struct snd_pcm_runtime *runtime = substream->runtime;
957 struct snd_ymfpci_pcm *ypcm;
960 err = snd_ymfpci_playback_open_1(substream);
963 ypcm = runtime->private_data;
964 ypcm->output_front = 0;
965 ypcm->output_rear = 1;
967 spin_lock_irq(&chip->reg_lock);
968 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
969 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
970 ymfpci_open_extension(chip);
971 chip->spdif_pcm_bits = chip->spdif_bits;
972 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
973 chip->spdif_opened++;
974 spin_unlock_irq(&chip->reg_lock);
976 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
977 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
978 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
982 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
984 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
985 struct snd_pcm_runtime *runtime = substream->runtime;
986 struct snd_ymfpci_pcm *ypcm;
989 err = snd_ymfpci_playback_open_1(substream);
992 ypcm = runtime->private_data;
993 ypcm->output_front = 0;
994 ypcm->output_rear = 1;
996 spin_lock_irq(&chip->reg_lock);
997 ymfpci_open_extension(chip);
999 spin_unlock_irq(&chip->reg_lock);
1003 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
1004 u32 capture_bank_number)
1006 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1007 struct snd_pcm_runtime *runtime = substream->runtime;
1008 struct snd_ymfpci_pcm *ypcm;
1011 runtime->hw = snd_ymfpci_capture;
1012 /* FIXME? True value is 256/48 = 5.33333 ms */
1013 err = snd_pcm_hw_constraint_minmax(runtime,
1014 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1018 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1022 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1026 ypcm->type = capture_bank_number + CAPTURE_REC;
1027 ypcm->substream = substream;
1028 ypcm->capture_bank_number = capture_bank_number;
1029 chip->capture_substream[capture_bank_number] = substream;
1030 runtime->private_data = ypcm;
1031 runtime->private_free = snd_ymfpci_pcm_free_substream;
1032 snd_ymfpci_hw_start(chip);
1036 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1038 return snd_ymfpci_capture_open(substream, 0);
1041 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1043 return snd_ymfpci_capture_open(substream, 1);
1046 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1051 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1053 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1054 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1056 spin_lock_irq(&chip->reg_lock);
1057 if (ypcm->output_rear && chip->rear_opened > 0) {
1058 chip->rear_opened--;
1059 ymfpci_close_extension(chip);
1061 spin_unlock_irq(&chip->reg_lock);
1062 return snd_ymfpci_playback_close_1(substream);
1065 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1067 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1069 spin_lock_irq(&chip->reg_lock);
1070 chip->spdif_opened = 0;
1071 ymfpci_close_extension(chip);
1072 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1073 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1074 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1075 spin_unlock_irq(&chip->reg_lock);
1076 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1077 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1078 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1079 return snd_ymfpci_playback_close_1(substream);
1082 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1084 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1086 spin_lock_irq(&chip->reg_lock);
1087 if (chip->rear_opened > 0) {
1088 chip->rear_opened--;
1089 ymfpci_close_extension(chip);
1091 spin_unlock_irq(&chip->reg_lock);
1092 return snd_ymfpci_playback_close_1(substream);
1095 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1097 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1098 struct snd_pcm_runtime *runtime = substream->runtime;
1099 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1102 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1103 snd_ymfpci_hw_stop(chip);
1108 static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
1109 .open = snd_ymfpci_playback_open,
1110 .close = snd_ymfpci_playback_close,
1111 .hw_params = snd_ymfpci_playback_hw_params,
1112 .hw_free = snd_ymfpci_playback_hw_free,
1113 .prepare = snd_ymfpci_playback_prepare,
1114 .trigger = snd_ymfpci_playback_trigger,
1115 .pointer = snd_ymfpci_playback_pointer,
1118 static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1119 .open = snd_ymfpci_capture_rec_open,
1120 .close = snd_ymfpci_capture_close,
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 snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
1129 struct snd_pcm *pcm;
1132 err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm);
1135 pcm->private_data = chip;
1137 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1138 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1141 pcm->info_flags = 0;
1142 strcpy(pcm->name, "YMFPCI");
1145 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1146 &chip->pci->dev, 64*1024, 256*1024);
1148 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1149 snd_pcm_std_chmaps, 2, 0, NULL);
1152 static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1153 .open = snd_ymfpci_capture_ac97_open,
1154 .close = snd_ymfpci_capture_close,
1155 .hw_free = snd_ymfpci_capture_hw_free,
1156 .prepare = snd_ymfpci_capture_prepare,
1157 .trigger = snd_ymfpci_capture_trigger,
1158 .pointer = snd_ymfpci_capture_pointer,
1161 int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
1163 struct snd_pcm *pcm;
1166 err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm);
1169 pcm->private_data = chip;
1171 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1174 pcm->info_flags = 0;
1175 sprintf(pcm->name, "YMFPCI - %s",
1176 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1179 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1180 &chip->pci->dev, 64*1024, 256*1024);
1185 static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1186 .open = snd_ymfpci_playback_spdif_open,
1187 .close = snd_ymfpci_playback_spdif_close,
1188 .hw_params = snd_ymfpci_playback_hw_params,
1189 .hw_free = snd_ymfpci_playback_hw_free,
1190 .prepare = snd_ymfpci_playback_prepare,
1191 .trigger = snd_ymfpci_playback_trigger,
1192 .pointer = snd_ymfpci_playback_pointer,
1195 int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
1197 struct snd_pcm *pcm;
1200 err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm);
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_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1213 &chip->pci->dev, 64*1024, 256*1024);
1218 static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1219 .open = snd_ymfpci_playback_4ch_open,
1220 .close = snd_ymfpci_playback_4ch_close,
1221 .hw_params = snd_ymfpci_playback_hw_params,
1222 .hw_free = snd_ymfpci_playback_hw_free,
1223 .prepare = snd_ymfpci_playback_prepare,
1224 .trigger = snd_ymfpci_playback_trigger,
1225 .pointer = snd_ymfpci_playback_pointer,
1228 static const struct snd_pcm_chmap_elem surround_map[] = {
1230 .map = { SNDRV_CHMAP_MONO } },
1232 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1236 int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
1238 struct snd_pcm *pcm;
1241 err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm);
1244 pcm->private_data = chip;
1246 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1249 pcm->info_flags = 0;
1250 strcpy(pcm->name, "YMFPCI - Rear PCM");
1251 chip->pcm_4ch = pcm;
1253 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1254 &chip->pci->dev, 64*1024, 256*1024);
1256 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1257 surround_map, 2, 0, NULL);
1260 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1262 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1267 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1268 struct snd_ctl_elem_value *ucontrol)
1270 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1272 spin_lock_irq(&chip->reg_lock);
1273 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1274 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1275 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1276 spin_unlock_irq(&chip->reg_lock);
1280 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1281 struct snd_ctl_elem_value *ucontrol)
1283 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1287 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1288 (ucontrol->value.iec958.status[1] << 8);
1289 spin_lock_irq(&chip->reg_lock);
1290 change = chip->spdif_bits != val;
1291 chip->spdif_bits = val;
1292 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1293 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1294 spin_unlock_irq(&chip->reg_lock);
1298 static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
1300 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1301 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1302 .info = snd_ymfpci_spdif_default_info,
1303 .get = snd_ymfpci_spdif_default_get,
1304 .put = snd_ymfpci_spdif_default_put
1307 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1309 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1314 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1315 struct snd_ctl_elem_value *ucontrol)
1317 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1319 spin_lock_irq(&chip->reg_lock);
1320 ucontrol->value.iec958.status[0] = 0x3e;
1321 ucontrol->value.iec958.status[1] = 0xff;
1322 spin_unlock_irq(&chip->reg_lock);
1326 static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
1328 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1329 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1330 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1331 .info = snd_ymfpci_spdif_mask_info,
1332 .get = snd_ymfpci_spdif_mask_get,
1335 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1337 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1342 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_value *ucontrol)
1345 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1347 spin_lock_irq(&chip->reg_lock);
1348 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1349 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1350 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1351 spin_unlock_irq(&chip->reg_lock);
1355 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
1358 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1362 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1363 (ucontrol->value.iec958.status[1] << 8);
1364 spin_lock_irq(&chip->reg_lock);
1365 change = chip->spdif_pcm_bits != val;
1366 chip->spdif_pcm_bits = val;
1367 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1368 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1369 spin_unlock_irq(&chip->reg_lock);
1373 static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
1375 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1376 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1377 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1378 .info = snd_ymfpci_spdif_stream_info,
1379 .get = snd_ymfpci_spdif_stream_get,
1380 .put = snd_ymfpci_spdif_stream_put
1383 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1385 static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
1387 return snd_ctl_enum_info(info, 1, 3, texts);
1390 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1392 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1395 spin_lock_irq(&chip->reg_lock);
1396 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1397 spin_unlock_irq(&chip->reg_lock);
1399 value->value.enumerated.item[0] = 0;
1401 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1405 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1407 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1410 spin_lock_irq(&chip->reg_lock);
1411 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1412 if (value->value.enumerated.item[0] == 0)
1413 reg = old_reg & ~0x100;
1415 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1416 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1417 spin_unlock_irq(&chip->reg_lock);
1418 return reg != old_reg;
1421 static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
1422 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1424 .name = "Direct Recording Source",
1425 .info = snd_ymfpci_drec_source_info,
1426 .get = snd_ymfpci_drec_source_get,
1427 .put = snd_ymfpci_drec_source_put
1434 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1435 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1436 .info = snd_ymfpci_info_single, \
1437 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1438 .private_value = ((reg) | ((shift) << 16)) }
1440 #define snd_ymfpci_info_single snd_ctl_boolean_mono_info
1442 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1443 struct snd_ctl_elem_value *ucontrol)
1445 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1446 int reg = kcontrol->private_value & 0xffff;
1447 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1448 unsigned int mask = 1;
1451 case YDSXGR_SPDIFOUTCTRL: break;
1452 case YDSXGR_SPDIFINCTRL: break;
1453 default: return -EINVAL;
1455 ucontrol->value.integer.value[0] =
1456 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1460 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_value *ucontrol)
1463 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1464 int reg = kcontrol->private_value & 0xffff;
1465 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1466 unsigned int mask = 1;
1468 unsigned int val, oval;
1471 case YDSXGR_SPDIFOUTCTRL: break;
1472 case YDSXGR_SPDIFINCTRL: break;
1473 default: return -EINVAL;
1475 val = (ucontrol->value.integer.value[0] & mask);
1477 spin_lock_irq(&chip->reg_lock);
1478 oval = snd_ymfpci_readl(chip, reg);
1479 val = (oval & ~(mask << shift)) | val;
1480 change = val != oval;
1481 snd_ymfpci_writel(chip, reg, val);
1482 spin_unlock_irq(&chip->reg_lock);
1486 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1488 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1489 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1490 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1491 .info = snd_ymfpci_info_double, \
1492 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1493 .private_value = reg, \
1494 .tlv = { .p = db_scale_native } }
1496 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1498 unsigned int reg = kcontrol->private_value;
1500 if (reg < 0x80 || reg >= 0xc0)
1502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1504 uinfo->value.integer.min = 0;
1505 uinfo->value.integer.max = 16383;
1509 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1511 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1512 unsigned int reg = kcontrol->private_value;
1513 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1516 if (reg < 0x80 || reg >= 0xc0)
1518 spin_lock_irq(&chip->reg_lock);
1519 val = snd_ymfpci_readl(chip, reg);
1520 spin_unlock_irq(&chip->reg_lock);
1521 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1522 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1526 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1528 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1529 unsigned int reg = kcontrol->private_value;
1530 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1532 unsigned int val1, val2, oval;
1534 if (reg < 0x80 || reg >= 0xc0)
1536 val1 = ucontrol->value.integer.value[0] & mask;
1537 val2 = ucontrol->value.integer.value[1] & mask;
1538 val1 <<= shift_left;
1539 val2 <<= shift_right;
1540 spin_lock_irq(&chip->reg_lock);
1541 oval = snd_ymfpci_readl(chip, reg);
1542 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1543 change = val1 != oval;
1544 snd_ymfpci_writel(chip, reg, val1);
1545 spin_unlock_irq(&chip->reg_lock);
1549 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_value *ucontrol)
1552 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1553 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1554 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1556 unsigned int value, oval;
1558 value = ucontrol->value.integer.value[0] & 0x3fff;
1559 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1560 spin_lock_irq(&chip->reg_lock);
1561 oval = snd_ymfpci_readl(chip, reg);
1562 change = value != oval;
1563 snd_ymfpci_writel(chip, reg, value);
1564 snd_ymfpci_writel(chip, reg2, value);
1565 spin_unlock_irq(&chip->reg_lock);
1572 #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
1574 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1576 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1577 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1581 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1583 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1585 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1587 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1591 static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
1592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593 .name = "4ch Duplication",
1594 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1595 .info = snd_ymfpci_info_dup4ch,
1596 .get = snd_ymfpci_get_dup4ch,
1597 .put = snd_ymfpci_put_dup4ch,
1600 static const struct snd_kcontrol_new snd_ymfpci_controls[] = {
1602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1603 .name = "Wave Playback Volume",
1604 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1605 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1606 .info = snd_ymfpci_info_double,
1607 .get = snd_ymfpci_get_double,
1608 .put = snd_ymfpci_put_nativedacvol,
1609 .private_value = YDSXGR_NATIVEDACOUTVOL,
1610 .tlv = { .p = db_scale_native },
1612 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1613 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1614 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1615 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1616 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1617 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1618 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1619 YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
1620 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1621 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1622 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1623 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1624 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1625 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1626 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1634 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1637 unsigned long flags;
1639 spin_lock_irqsave(&chip->reg_lock, flags);
1640 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1641 reg &= ~(1 << (pin + 8));
1643 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1644 /* set the level mode for input line */
1645 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1646 mode &= ~(3 << (pin * 2));
1647 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1648 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1649 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1650 spin_unlock_irqrestore(&chip->reg_lock, flags);
1651 return (mode >> pin) & 1;
1654 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1657 unsigned long flags;
1659 spin_lock_irqsave(&chip->reg_lock, flags);
1660 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1662 reg &= ~(1 << (pin + 8));
1663 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1664 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1665 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1666 spin_unlock_irqrestore(&chip->reg_lock, flags);
1671 #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
1673 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1675 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1676 int pin = (int)kcontrol->private_value;
1677 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1681 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1683 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1684 int pin = (int)kcontrol->private_value;
1686 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1687 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1688 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1694 static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
1695 .name = "Shared Rear/Line-In Switch",
1696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1697 .info = snd_ymfpci_gpio_sw_info,
1698 .get = snd_ymfpci_gpio_sw_get,
1699 .put = snd_ymfpci_gpio_sw_put,
1707 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1708 struct snd_ctl_elem_info *uinfo)
1710 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1712 uinfo->value.integer.min = 0;
1713 uinfo->value.integer.max = 0x8000;
1717 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_value *ucontrol)
1720 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1721 unsigned int subs = kcontrol->id.subdevice;
1723 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1724 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1728 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_value *ucontrol)
1731 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1732 unsigned int subs = kcontrol->id.subdevice;
1733 struct snd_pcm_substream *substream;
1734 unsigned long flags;
1736 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1737 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1738 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1739 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1740 if (chip->pcm_mixer[subs].left > 0x8000)
1741 chip->pcm_mixer[subs].left = 0x8000;
1742 if (chip->pcm_mixer[subs].right > 0x8000)
1743 chip->pcm_mixer[subs].right = 0x8000;
1745 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1746 spin_lock_irqsave(&chip->voice_lock, flags);
1747 if (substream->runtime && substream->runtime->private_data) {
1748 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1749 if (!ypcm->use_441_slot)
1750 ypcm->update_pcm_vol = 2;
1752 spin_unlock_irqrestore(&chip->voice_lock, flags);
1758 static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
1759 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1760 .name = "PCM Playback Volume",
1761 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1762 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1763 .info = snd_ymfpci_pcm_vol_info,
1764 .get = snd_ymfpci_pcm_vol_get,
1765 .put = snd_ymfpci_pcm_vol_put,
1773 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1775 struct snd_ymfpci *chip = bus->private_data;
1776 chip->ac97_bus = NULL;
1779 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1781 struct snd_ymfpci *chip = ac97->private_data;
1785 int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1787 struct snd_ac97_template ac97;
1788 struct snd_kcontrol *kctl;
1789 struct snd_pcm_substream *substream;
1792 static const struct snd_ac97_bus_ops ops = {
1793 .write = snd_ymfpci_codec_write,
1794 .read = snd_ymfpci_codec_read,
1797 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1800 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1801 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1803 memset(&ac97, 0, sizeof(ac97));
1804 ac97.private_data = chip;
1805 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1806 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
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 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip));
1819 if (chip->ac97->ext_id & AC97_EI_SDAC) {
1820 kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1821 err = snd_ctl_add(chip->card, kctl);
1826 /* add S/PDIF control */
1827 if (snd_BUG_ON(!chip->pcm_spdif))
1829 kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip);
1830 err = snd_ctl_add(chip->card, kctl);
1833 kctl->id.device = chip->pcm_spdif->device;
1834 kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip);
1835 err = snd_ctl_add(chip->card, kctl);
1838 kctl->id.device = chip->pcm_spdif->device;
1839 kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip);
1840 err = snd_ctl_add(chip->card, kctl);
1843 kctl->id.device = chip->pcm_spdif->device;
1844 chip->spdif_pcm_ctl = kctl;
1846 /* direct recording source */
1847 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754) {
1848 kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip);
1849 err = snd_ctl_add(chip->card, kctl);
1855 * shared rear/line-in
1858 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip));
1863 /* per-voice volume */
1864 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1865 for (idx = 0; idx < 32; ++idx) {
1866 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1869 kctl->id.device = chip->pcm->device;
1870 kctl->id.subdevice = idx;
1871 kctl->private_value = (unsigned long)substream;
1872 err = snd_ctl_add(chip->card, kctl);
1875 chip->pcm_mixer[idx].left = 0x8000;
1876 chip->pcm_mixer[idx].right = 0x8000;
1877 chip->pcm_mixer[idx].ctl = kctl;
1878 substream = substream->next;
1889 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1891 struct snd_ymfpci *chip;
1892 unsigned long flags;
1895 chip = snd_timer_chip(timer);
1896 spin_lock_irqsave(&chip->reg_lock, flags);
1897 if (timer->sticks > 1) {
1898 chip->timer_ticks = timer->sticks;
1899 count = timer->sticks - 1;
1902 * Divisor 1 is not allowed; fake it by using divisor 2 and
1903 * counting two ticks for each interrupt.
1905 chip->timer_ticks = 2;
1908 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1909 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1910 spin_unlock_irqrestore(&chip->reg_lock, flags);
1914 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1916 struct snd_ymfpci *chip;
1917 unsigned long flags;
1919 chip = snd_timer_chip(timer);
1920 spin_lock_irqsave(&chip->reg_lock, flags);
1921 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1922 spin_unlock_irqrestore(&chip->reg_lock, flags);
1926 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1927 unsigned long *num, unsigned long *den)
1934 static const struct snd_timer_hardware snd_ymfpci_timer_hw = {
1935 .flags = SNDRV_TIMER_HW_AUTO,
1936 .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1938 .start = snd_ymfpci_timer_start,
1939 .stop = snd_ymfpci_timer_stop,
1940 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1943 int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1945 struct snd_timer *timer = NULL;
1946 struct snd_timer_id tid;
1949 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1950 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1951 tid.card = chip->card->number;
1952 tid.device = device;
1954 err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer);
1956 strcpy(timer->name, "YMFPCI timer");
1957 timer->private_data = chip;
1958 timer->hw = snd_ymfpci_timer_hw;
1960 chip->timer = timer;
1969 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1970 struct snd_info_buffer *buffer)
1972 struct snd_ymfpci *chip = entry->private_data;
1975 snd_iprintf(buffer, "YMFPCI\n\n");
1976 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1977 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1980 static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1982 return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
1986 * initialization routines
1989 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1993 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1994 #if 0 // force to reset
1997 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1998 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1999 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
2000 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
2001 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
2007 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
2009 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
2012 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
2017 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
2019 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2020 while (timeout-- > 0) {
2021 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2022 if ((val & 0x00000002) == 0)
2027 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2032 err = reject_firmware(&chip->dsp_microcode, "/*(DEBLOBBED)*/",
2035 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2036 dev_err(chip->card->dev,
2037 "DSP microcode has wrong size\n");
2043 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2044 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2045 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2046 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2047 name = is_1e ? "/*(DEBLOBBED)*/" : "/*(DEBLOBBED)*/";
2048 err = reject_firmware(&chip->controller_microcode, name,
2051 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2052 dev_err(chip->card->dev,
2053 "controller microcode has wrong size\n");
2064 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2070 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2071 snd_ymfpci_disable_dsp(chip);
2072 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2073 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2074 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2075 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2076 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2077 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2078 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2079 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2080 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2082 /* setup DSP instruction code */
2083 inst = (const __le32 *)chip->dsp_microcode->data;
2084 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2085 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2086 le32_to_cpu(inst[i]));
2088 /* setup control instruction code */
2089 inst = (const __le32 *)chip->controller_microcode->data;
2090 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2091 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2092 le32_to_cpu(inst[i]));
2094 snd_ymfpci_enable_dsp(chip);
2097 static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2099 long size, playback_ctrl_size;
2100 int voice, bank, reg;
2102 dma_addr_t ptr_addr;
2104 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2105 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2106 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2107 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2108 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2110 size = ALIGN(playback_ctrl_size, 0x100) +
2111 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2112 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2113 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2115 /* work_ptr must be aligned to 256 bytes, but it's already
2116 covered with the kernel page allocation mechanism */
2117 chip->work_ptr = snd_devm_alloc_pages(&chip->pci->dev,
2118 SNDRV_DMA_TYPE_DEV, size);
2119 if (!chip->work_ptr)
2121 ptr = chip->work_ptr->area;
2122 ptr_addr = chip->work_ptr->addr;
2123 memset(ptr, 0, size); /* for sure */
2125 chip->bank_base_playback = ptr;
2126 chip->bank_base_playback_addr = ptr_addr;
2127 chip->ctrl_playback = (__le32 *)ptr;
2128 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2129 ptr += ALIGN(playback_ctrl_size, 0x100);
2130 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2131 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2132 chip->voices[voice].number = voice;
2133 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2134 chip->voices[voice].bank_addr = ptr_addr;
2135 for (bank = 0; bank < 2; bank++) {
2136 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2137 ptr += chip->bank_size_playback;
2138 ptr_addr += chip->bank_size_playback;
2141 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2142 ptr_addr = ALIGN(ptr_addr, 0x100);
2143 chip->bank_base_capture = ptr;
2144 chip->bank_base_capture_addr = ptr_addr;
2145 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2146 for (bank = 0; bank < 2; bank++) {
2147 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2148 ptr += chip->bank_size_capture;
2149 ptr_addr += chip->bank_size_capture;
2151 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2152 ptr_addr = ALIGN(ptr_addr, 0x100);
2153 chip->bank_base_effect = ptr;
2154 chip->bank_base_effect_addr = ptr_addr;
2155 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2156 for (bank = 0; bank < 2; bank++) {
2157 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2158 ptr += chip->bank_size_effect;
2159 ptr_addr += chip->bank_size_effect;
2161 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2162 ptr_addr = ALIGN(ptr_addr, 0x100);
2163 chip->work_base = ptr;
2164 chip->work_base_addr = ptr_addr;
2166 snd_BUG_ON(ptr + PAGE_ALIGN(chip->work_size) !=
2167 chip->work_ptr->area + chip->work_ptr->bytes);
2169 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2170 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2171 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2172 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2173 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2175 /* S/PDIF output initialization */
2176 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2177 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2178 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2180 /* S/PDIF input initialization */
2181 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2183 /* digital mixer setup */
2184 for (reg = 0x80; reg < 0xc0; reg += 4)
2185 snd_ymfpci_writel(chip, reg, 0);
2186 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2187 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2188 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2189 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2190 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2191 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2192 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2193 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2198 static void snd_ymfpci_free(struct snd_card *card)
2200 struct snd_ymfpci *chip = card->private_data;
2203 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2204 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2205 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2206 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2207 snd_ymfpci_disable_dsp(chip);
2208 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2209 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2210 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2211 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2212 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2213 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2214 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2216 snd_ymfpci_ac3_done(chip);
2218 snd_ymfpci_free_gameport(chip);
2220 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2222 release_firmware(chip->dsp_microcode);
2223 release_firmware(chip->controller_microcode);
2226 #ifdef CONFIG_PM_SLEEP
2227 static const int saved_regs_index[] = {
2229 YDSXGR_SPDIFOUTCTRL,
2230 YDSXGR_SPDIFOUTSTATUS,
2233 YDSXGR_PRIADCLOOPVOL,
2234 YDSXGR_NATIVEDACINVOL,
2235 YDSXGR_NATIVEDACOUTVOL,
2236 YDSXGR_BUF441OUTVOL,
2237 YDSXGR_NATIVEADCINVOL,
2238 YDSXGR_SPDIFLOOPVOL,
2241 YDSXGR_LEGACYOUTVOL,
2243 YDSXGR_PLAYCTRLBASE,
2247 /* capture set up */
2254 #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2256 static int snd_ymfpci_suspend(struct device *dev)
2258 struct snd_card *card = dev_get_drvdata(dev);
2259 struct snd_ymfpci *chip = card->private_data;
2262 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2263 snd_ac97_suspend(chip->ac97);
2264 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2265 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2266 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2267 pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2268 &chip->saved_dsxg_legacy);
2269 pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2270 &chip->saved_dsxg_elegacy);
2271 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2272 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2273 snd_ymfpci_disable_dsp(chip);
2277 static int snd_ymfpci_resume(struct device *dev)
2279 struct pci_dev *pci = to_pci_dev(dev);
2280 struct snd_card *card = dev_get_drvdata(dev);
2281 struct snd_ymfpci *chip = card->private_data;
2284 snd_ymfpci_aclink_reset(pci);
2285 snd_ymfpci_codec_ready(chip, 0);
2286 snd_ymfpci_download_image(chip);
2289 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2290 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2292 snd_ac97_resume(chip->ac97);
2294 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2295 chip->saved_dsxg_legacy);
2296 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2297 chip->saved_dsxg_elegacy);
2299 /* start hw again */
2300 if (chip->start_count > 0) {
2301 spin_lock_irq(&chip->reg_lock);
2302 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2303 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2304 spin_unlock_irq(&chip->reg_lock);
2306 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2310 SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2311 #endif /* CONFIG_PM_SLEEP */
2313 int snd_ymfpci_create(struct snd_card *card,
2314 struct pci_dev *pci,
2315 unsigned short old_legacy_ctrl)
2317 struct snd_ymfpci *chip = card->private_data;
2320 /* enable PCI device */
2321 err = pcim_enable_device(pci);
2325 chip->old_legacy_ctrl = old_legacy_ctrl;
2326 spin_lock_init(&chip->reg_lock);
2327 spin_lock_init(&chip->voice_lock);
2328 init_waitqueue_head(&chip->interrupt_sleep);
2329 atomic_set(&chip->interrupt_sleep_count, 0);
2333 chip->device_id = pci->device;
2334 chip->rev = pci->revision;
2336 err = pci_request_regions(pci, "YMFPCI");
2340 chip->reg_area_phys = pci_resource_start(pci, 0);
2341 chip->reg_area_virt = devm_ioremap(&pci->dev, chip->reg_area_phys, 0x8000);
2342 if (!chip->reg_area_virt) {
2343 dev_err(chip->card->dev,
2344 "unable to grab memory region 0x%lx-0x%lx\n",
2345 chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2348 pci_set_master(pci);
2349 chip->src441_used = -1;
2351 if (devm_request_irq(&pci->dev, pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2352 KBUILD_MODNAME, chip)) {
2353 dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2356 chip->irq = pci->irq;
2357 card->sync_irq = chip->irq;
2358 card->private_free = snd_ymfpci_free;
2360 snd_ymfpci_aclink_reset(pci);
2361 if (snd_ymfpci_codec_ready(chip, 0) < 0)
2364 err = snd_ymfpci_request_firmware(chip);
2366 dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2369 snd_ymfpci_download_image(chip);
2371 udelay(100); /* seems we need a delay after downloading image.. */
2373 if (snd_ymfpci_memalloc(chip) < 0)
2376 err = snd_ymfpci_ac3_init(chip);
2380 #ifdef CONFIG_PM_SLEEP
2381 chip->saved_regs = devm_kmalloc_array(&pci->dev, YDSXGR_NUM_SAVED_REGS,
2382 sizeof(u32), GFP_KERNEL);
2383 if (!chip->saved_regs)
2387 snd_ymfpci_proc_init(card, chip);