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 void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
36 writeb(val, chip->reg_area_virt + offset);
39 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
41 return readw(chip->reg_area_virt + offset);
44 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
46 writew(val, chip->reg_area_virt + offset);
49 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
51 return readl(chip->reg_area_virt + offset);
54 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
56 writel(val, chip->reg_area_virt + offset);
59 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
61 unsigned long end_time;
62 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
64 end_time = jiffies + msecs_to_jiffies(750);
66 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
68 schedule_timeout_uninterruptible(1);
69 } while (time_before(jiffies, end_time));
70 dev_err(chip->card->dev,
71 "codec_ready: codec %i is not ready [0x%x]\n",
72 secondary, snd_ymfpci_readw(chip, reg));
76 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
78 struct snd_ymfpci *chip = ac97->private_data;
81 snd_ymfpci_codec_ready(chip, 0);
82 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
83 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
86 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
88 struct snd_ymfpci *chip = ac97->private_data;
90 if (snd_ymfpci_codec_ready(chip, 0))
92 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
93 if (snd_ymfpci_codec_ready(chip, 0))
95 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
97 for (i = 0; i < 600; i++)
98 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
100 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
107 static u32 snd_ymfpci_calc_delta(u32 rate)
110 case 8000: return 0x02aaab00;
111 case 11025: return 0x03accd00;
112 case 16000: return 0x05555500;
113 case 22050: return 0x07599a00;
114 case 32000: return 0x0aaaab00;
115 case 44100: return 0x0eb33300;
116 default: return ((rate << 16) / 375) << 5;
120 static const u32 def_rate[8] = {
121 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
124 static u32 snd_ymfpci_calc_lpfK(u32 rate)
127 static const u32 val[8] = {
128 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
129 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
133 return 0x40000000; /* FIXME: What's the right value? */
134 for (i = 0; i < 8; i++)
135 if (rate <= def_rate[i])
140 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
143 static const u32 val[8] = {
144 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
145 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
150 for (i = 0; i < 8; i++)
151 if (rate <= def_rate[i])
157 * Hardware start management
160 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
164 spin_lock_irqsave(&chip->reg_lock, flags);
165 if (chip->start_count++ > 0)
167 snd_ymfpci_writel(chip, YDSXGR_MODE,
168 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
169 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
171 spin_unlock_irqrestore(&chip->reg_lock, flags);
174 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
179 spin_lock_irqsave(&chip->reg_lock, flags);
180 if (--chip->start_count > 0)
182 snd_ymfpci_writel(chip, YDSXGR_MODE,
183 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
184 while (timeout-- > 0) {
185 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
188 if (atomic_read(&chip->interrupt_sleep_count)) {
189 atomic_set(&chip->interrupt_sleep_count, 0);
190 wake_up(&chip->interrupt_sleep);
193 spin_unlock_irqrestore(&chip->reg_lock, flags);
197 * Playback voice management
200 static int voice_alloc(struct snd_ymfpci *chip,
201 enum snd_ymfpci_voice_type type, int pair,
202 struct snd_ymfpci_voice **rvoice)
204 struct snd_ymfpci_voice *voice, *voice2;
208 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
209 voice = &chip->voices[idx];
210 voice2 = pair ? &chip->voices[idx+1] : NULL;
211 if (voice->use || (voice2 && voice2->use))
229 snd_ymfpci_hw_start(chip);
231 snd_ymfpci_hw_start(chip);
238 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
239 enum snd_ymfpci_voice_type type, int pair,
240 struct snd_ymfpci_voice **rvoice)
245 if (snd_BUG_ON(!rvoice))
247 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
250 spin_lock_irqsave(&chip->voice_lock, flags);
252 result = voice_alloc(chip, type, pair, rvoice);
253 if (result == 0 || type != YMFPCI_PCM)
255 /* TODO: synth/midi voice deallocation */
258 spin_unlock_irqrestore(&chip->voice_lock, flags);
262 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
266 if (snd_BUG_ON(!pvoice))
268 snd_ymfpci_hw_stop(chip);
269 spin_lock_irqsave(&chip->voice_lock, flags);
270 if (pvoice->number == chip->src441_used) {
271 chip->src441_used = -1;
272 pvoice->ypcm->use_441_slot = 0;
274 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
276 pvoice->interrupt = NULL;
277 spin_unlock_irqrestore(&chip->voice_lock, flags);
285 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
287 struct snd_ymfpci_pcm *ypcm;
293 if (ypcm->substream == NULL)
295 spin_lock(&chip->reg_lock);
297 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
298 if (pos < ypcm->last_pos)
299 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
301 delta = pos - ypcm->last_pos;
302 ypcm->period_pos += delta;
303 ypcm->last_pos = pos;
304 if (ypcm->period_pos >= ypcm->period_size) {
306 dev_dbg(chip->card->dev,
307 "done - active_bank = 0x%x, start = 0x%x\n",
309 voice->bank[chip->active_bank].start);
311 ypcm->period_pos %= ypcm->period_size;
312 spin_unlock(&chip->reg_lock);
313 snd_pcm_period_elapsed(ypcm->substream);
314 spin_lock(&chip->reg_lock);
317 if (unlikely(ypcm->update_pcm_vol)) {
318 unsigned int subs = ypcm->substream->number;
319 unsigned int next_bank = 1 - chip->active_bank;
320 struct snd_ymfpci_playback_bank *bank;
323 bank = &voice->bank[next_bank];
324 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
325 bank->left_gain_end = volume;
326 if (ypcm->output_rear)
327 bank->eff2_gain_end = volume;
329 bank = &ypcm->voices[1]->bank[next_bank];
330 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
331 bank->right_gain_end = volume;
332 if (ypcm->output_rear)
333 bank->eff3_gain_end = volume;
334 ypcm->update_pcm_vol--;
337 spin_unlock(&chip->reg_lock);
340 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
342 struct snd_pcm_runtime *runtime = substream->runtime;
343 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
344 struct snd_ymfpci *chip = ypcm->chip;
347 spin_lock(&chip->reg_lock);
349 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
350 if (pos < ypcm->last_pos)
351 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
353 delta = pos - ypcm->last_pos;
354 ypcm->period_pos += delta;
355 ypcm->last_pos = pos;
356 if (ypcm->period_pos >= ypcm->period_size) {
357 ypcm->period_pos %= ypcm->period_size;
359 dev_dbg(chip->card->dev,
360 "done - active_bank = 0x%x, start = 0x%x\n",
362 voice->bank[chip->active_bank].start);
364 spin_unlock(&chip->reg_lock);
365 snd_pcm_period_elapsed(substream);
366 spin_lock(&chip->reg_lock);
369 spin_unlock(&chip->reg_lock);
372 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
375 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
376 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
377 struct snd_kcontrol *kctl = NULL;
380 spin_lock(&chip->reg_lock);
381 if (ypcm->voices[0] == NULL) {
386 case SNDRV_PCM_TRIGGER_START:
387 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
388 case SNDRV_PCM_TRIGGER_RESUME:
389 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
390 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
391 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
394 case SNDRV_PCM_TRIGGER_STOP:
395 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
396 kctl = chip->pcm_mixer[substream->number].ctl;
397 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
400 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
401 case SNDRV_PCM_TRIGGER_SUSPEND:
402 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
403 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
404 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
412 spin_unlock(&chip->reg_lock);
414 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
417 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
420 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
421 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
425 spin_lock(&chip->reg_lock);
427 case SNDRV_PCM_TRIGGER_START:
428 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
429 case SNDRV_PCM_TRIGGER_RESUME:
430 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
431 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
434 case SNDRV_PCM_TRIGGER_STOP:
435 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
436 case SNDRV_PCM_TRIGGER_SUSPEND:
437 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
438 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
445 spin_unlock(&chip->reg_lock);
449 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
453 if (ypcm->voices[1] != NULL && voices < 2) {
454 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
455 ypcm->voices[1] = NULL;
457 if (voices == 1 && ypcm->voices[0] != NULL)
458 return 0; /* already allocated */
459 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
460 return 0; /* already allocated */
462 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
463 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
464 ypcm->voices[0] = NULL;
467 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
470 ypcm->voices[0]->ypcm = ypcm;
471 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
473 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
474 ypcm->voices[1]->ypcm = ypcm;
479 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
480 struct snd_pcm_runtime *runtime,
483 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
485 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
486 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
487 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
488 struct snd_ymfpci_playback_bank *bank;
490 __le32 vol_left, vol_right;
491 u8 use_left, use_right;
494 if (snd_BUG_ON(!voice))
496 if (runtime->channels == 1) {
500 use_left = (voiceidx & 1) == 0;
501 use_right = !use_left;
503 if (has_pcm_volume) {
504 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
505 [ypcm->substream->number].left << 15);
506 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
507 [ypcm->substream->number].right << 15);
509 vol_left = cpu_to_le32(0x40000000);
510 vol_right = cpu_to_le32(0x40000000);
512 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
513 format = runtime->channels == 2 ? 0x00010000 : 0;
514 if (snd_pcm_format_width(runtime->format) == 8)
515 format |= 0x80000000;
516 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
517 runtime->rate == 44100 && runtime->channels == 2 &&
518 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
519 ypcm->chip->src441_used == voice->number)) {
520 ypcm->chip->src441_used = voice->number;
521 ypcm->use_441_slot = 1;
522 format |= 0x10000000;
524 if (ypcm->chip->src441_used == voice->number &&
525 (format & 0x10000000) == 0) {
526 ypcm->chip->src441_used = -1;
527 ypcm->use_441_slot = 0;
529 if (runtime->channels == 2 && (voiceidx & 1) != 0)
531 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
532 for (nbank = 0; nbank < 2; nbank++) {
533 bank = &voice->bank[nbank];
534 memset(bank, 0, sizeof(*bank));
535 bank->format = cpu_to_le32(format);
536 bank->base = cpu_to_le32(runtime->dma_addr);
537 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
538 bank->lpfQ = cpu_to_le32(lpfQ);
540 bank->delta_end = cpu_to_le32(delta);
542 bank->lpfK_end = cpu_to_le32(lpfK);
544 bank->eg_gain_end = cpu_to_le32(0x40000000);
546 if (ypcm->output_front) {
549 bank->left_gain_end = vol_left;
553 bank->right_gain_end = vol_right;
556 if (ypcm->output_rear) {
557 if (!ypcm->swap_rear) {
560 bank->eff2_gain_end = vol_left;
564 bank->eff3_gain_end = vol_right;
567 /* The SPDIF out channels seem to be swapped, so we have
568 * to swap them here, too. The rear analog out channels
569 * will be wrong, but otherwise AC3 would not work.
573 bank->eff3_gain_end = vol_left;
577 bank->eff2_gain_end = vol_right;
584 static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
586 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
587 4096, &chip->ac3_tmp_base) < 0)
590 chip->bank_effect[3][0]->base =
591 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
592 chip->bank_effect[3][0]->loop_end =
593 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
594 chip->bank_effect[4][0]->base =
595 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
596 chip->bank_effect[4][0]->loop_end =
597 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
599 spin_lock_irq(&chip->reg_lock);
600 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
601 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
602 spin_unlock_irq(&chip->reg_lock);
606 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
608 spin_lock_irq(&chip->reg_lock);
609 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
610 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
611 spin_unlock_irq(&chip->reg_lock);
612 // snd_ymfpci_irq_wait(chip);
613 if (chip->ac3_tmp_base.area) {
614 snd_dma_free_pages(&chip->ac3_tmp_base);
615 chip->ac3_tmp_base.area = NULL;
620 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
621 struct snd_pcm_hw_params *hw_params)
623 struct snd_pcm_runtime *runtime = substream->runtime;
624 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
627 err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params));
633 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
635 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
636 struct snd_pcm_runtime *runtime = substream->runtime;
637 struct snd_ymfpci_pcm *ypcm;
639 if (runtime->private_data == NULL)
641 ypcm = runtime->private_data;
643 /* wait, until the PCI operations are not finished */
644 snd_ymfpci_irq_wait(chip);
645 if (ypcm->voices[1]) {
646 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
647 ypcm->voices[1] = NULL;
649 if (ypcm->voices[0]) {
650 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
651 ypcm->voices[0] = NULL;
656 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
658 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
659 struct snd_pcm_runtime *runtime = substream->runtime;
660 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
661 struct snd_kcontrol *kctl;
664 ypcm->period_size = runtime->period_size;
665 ypcm->buffer_size = runtime->buffer_size;
666 ypcm->period_pos = 0;
668 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
669 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
670 substream->pcm == chip->pcm);
672 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
673 kctl = chip->pcm_mixer[substream->number].ctl;
674 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
675 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
680 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
682 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
684 /* wait, until the PCI operations are not finished */
685 snd_ymfpci_irq_wait(chip);
689 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
691 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
692 struct snd_pcm_runtime *runtime = substream->runtime;
693 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
694 struct snd_ymfpci_capture_bank * bank;
698 ypcm->period_size = runtime->period_size;
699 ypcm->buffer_size = runtime->buffer_size;
700 ypcm->period_pos = 0;
703 rate = ((48000 * 4096) / runtime->rate) - 1;
705 if (runtime->channels == 2) {
709 if (snd_pcm_format_width(runtime->format) == 8)
713 switch (ypcm->capture_bank_number) {
715 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
716 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
719 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
720 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
723 for (nbank = 0; nbank < 2; nbank++) {
724 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
725 bank->base = cpu_to_le32(runtime->dma_addr);
726 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
728 bank->num_of_loops = 0;
733 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
735 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
736 struct snd_pcm_runtime *runtime = substream->runtime;
737 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
738 struct snd_ymfpci_voice *voice = ypcm->voices[0];
740 if (!(ypcm->running && voice))
742 return le32_to_cpu(voice->bank[chip->active_bank].start);
745 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
747 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
748 struct snd_pcm_runtime *runtime = substream->runtime;
749 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
753 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
756 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
758 wait_queue_entry_t wait;
761 while (loops-- > 0) {
762 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
764 init_waitqueue_entry(&wait, current);
765 add_wait_queue(&chip->interrupt_sleep, &wait);
766 atomic_inc(&chip->interrupt_sleep_count);
767 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
768 remove_wait_queue(&chip->interrupt_sleep, &wait);
772 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
774 struct snd_ymfpci *chip = dev_id;
775 u32 status, nvoice, mode;
776 struct snd_ymfpci_voice *voice;
778 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
779 if (status & 0x80000000) {
780 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
781 spin_lock(&chip->voice_lock);
782 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
783 voice = &chip->voices[nvoice];
784 if (voice->interrupt)
785 voice->interrupt(chip, voice);
787 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
788 if (chip->capture_substream[nvoice])
789 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
792 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
793 if (chip->effect_substream[nvoice])
794 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
797 spin_unlock(&chip->voice_lock);
798 spin_lock(&chip->reg_lock);
799 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
800 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
801 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
802 spin_unlock(&chip->reg_lock);
804 if (atomic_read(&chip->interrupt_sleep_count)) {
805 atomic_set(&chip->interrupt_sleep_count, 0);
806 wake_up(&chip->interrupt_sleep);
810 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
813 snd_timer_interrupt(chip->timer, chip->timer_ticks);
815 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
818 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
822 static const struct snd_pcm_hardware snd_ymfpci_playback =
824 .info = (SNDRV_PCM_INFO_MMAP |
825 SNDRV_PCM_INFO_MMAP_VALID |
826 SNDRV_PCM_INFO_INTERLEAVED |
827 SNDRV_PCM_INFO_BLOCK_TRANSFER |
828 SNDRV_PCM_INFO_PAUSE |
829 SNDRV_PCM_INFO_RESUME),
830 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
831 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
836 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
837 .period_bytes_min = 64,
838 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
844 static const struct snd_pcm_hardware snd_ymfpci_capture =
846 .info = (SNDRV_PCM_INFO_MMAP |
847 SNDRV_PCM_INFO_MMAP_VALID |
848 SNDRV_PCM_INFO_INTERLEAVED |
849 SNDRV_PCM_INFO_BLOCK_TRANSFER |
850 SNDRV_PCM_INFO_PAUSE |
851 SNDRV_PCM_INFO_RESUME),
852 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
853 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
858 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
859 .period_bytes_min = 64,
860 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
866 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
868 kfree(runtime->private_data);
871 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
873 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
874 struct snd_pcm_runtime *runtime = substream->runtime;
875 struct snd_ymfpci_pcm *ypcm;
878 runtime->hw = snd_ymfpci_playback;
879 /* FIXME? True value is 256/48 = 5.33333 ms */
880 err = snd_pcm_hw_constraint_minmax(runtime,
881 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
885 err = snd_pcm_hw_rule_noresample(runtime, 48000);
889 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
893 ypcm->type = PLAYBACK_VOICE;
894 ypcm->substream = substream;
895 runtime->private_data = ypcm;
896 runtime->private_free = snd_ymfpci_pcm_free_substream;
900 /* call with spinlock held */
901 static void ymfpci_open_extension(struct snd_ymfpci *chip)
903 if (! chip->rear_opened) {
904 if (! chip->spdif_opened) /* set AC3 */
905 snd_ymfpci_writel(chip, YDSXGR_MODE,
906 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
907 /* enable second codec (4CHEN) */
908 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
909 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
913 /* call with spinlock held */
914 static void ymfpci_close_extension(struct snd_ymfpci *chip)
916 if (! chip->rear_opened) {
917 if (! chip->spdif_opened)
918 snd_ymfpci_writel(chip, YDSXGR_MODE,
919 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
920 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
921 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
925 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
927 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
928 struct snd_pcm_runtime *runtime = substream->runtime;
929 struct snd_ymfpci_pcm *ypcm;
932 err = snd_ymfpci_playback_open_1(substream);
935 ypcm = runtime->private_data;
936 ypcm->output_front = 1;
937 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
939 spin_lock_irq(&chip->reg_lock);
940 if (ypcm->output_rear) {
941 ymfpci_open_extension(chip);
944 spin_unlock_irq(&chip->reg_lock);
948 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
950 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
951 struct snd_pcm_runtime *runtime = substream->runtime;
952 struct snd_ymfpci_pcm *ypcm;
955 err = snd_ymfpci_playback_open_1(substream);
958 ypcm = runtime->private_data;
959 ypcm->output_front = 0;
960 ypcm->output_rear = 1;
962 spin_lock_irq(&chip->reg_lock);
963 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
964 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
965 ymfpci_open_extension(chip);
966 chip->spdif_pcm_bits = chip->spdif_bits;
967 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
968 chip->spdif_opened++;
969 spin_unlock_irq(&chip->reg_lock);
971 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
972 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
973 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
977 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
979 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
980 struct snd_pcm_runtime *runtime = substream->runtime;
981 struct snd_ymfpci_pcm *ypcm;
984 err = snd_ymfpci_playback_open_1(substream);
987 ypcm = runtime->private_data;
988 ypcm->output_front = 0;
989 ypcm->output_rear = 1;
991 spin_lock_irq(&chip->reg_lock);
992 ymfpci_open_extension(chip);
994 spin_unlock_irq(&chip->reg_lock);
998 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
999 u32 capture_bank_number)
1001 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1002 struct snd_pcm_runtime *runtime = substream->runtime;
1003 struct snd_ymfpci_pcm *ypcm;
1006 runtime->hw = snd_ymfpci_capture;
1007 /* FIXME? True value is 256/48 = 5.33333 ms */
1008 err = snd_pcm_hw_constraint_minmax(runtime,
1009 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1013 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1017 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1021 ypcm->type = capture_bank_number + CAPTURE_REC;
1022 ypcm->substream = substream;
1023 ypcm->capture_bank_number = capture_bank_number;
1024 chip->capture_substream[capture_bank_number] = substream;
1025 runtime->private_data = ypcm;
1026 runtime->private_free = snd_ymfpci_pcm_free_substream;
1027 snd_ymfpci_hw_start(chip);
1031 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1033 return snd_ymfpci_capture_open(substream, 0);
1036 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1038 return snd_ymfpci_capture_open(substream, 1);
1041 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1046 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1048 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1049 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1051 spin_lock_irq(&chip->reg_lock);
1052 if (ypcm->output_rear && chip->rear_opened > 0) {
1053 chip->rear_opened--;
1054 ymfpci_close_extension(chip);
1056 spin_unlock_irq(&chip->reg_lock);
1057 return snd_ymfpci_playback_close_1(substream);
1060 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1062 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1064 spin_lock_irq(&chip->reg_lock);
1065 chip->spdif_opened = 0;
1066 ymfpci_close_extension(chip);
1067 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1068 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1069 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1070 spin_unlock_irq(&chip->reg_lock);
1071 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1072 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1073 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1074 return snd_ymfpci_playback_close_1(substream);
1077 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1079 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1081 spin_lock_irq(&chip->reg_lock);
1082 if (chip->rear_opened > 0) {
1083 chip->rear_opened--;
1084 ymfpci_close_extension(chip);
1086 spin_unlock_irq(&chip->reg_lock);
1087 return snd_ymfpci_playback_close_1(substream);
1090 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1092 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1097 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1098 snd_ymfpci_hw_stop(chip);
1103 static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
1104 .open = snd_ymfpci_playback_open,
1105 .close = snd_ymfpci_playback_close,
1106 .hw_params = snd_ymfpci_playback_hw_params,
1107 .hw_free = snd_ymfpci_playback_hw_free,
1108 .prepare = snd_ymfpci_playback_prepare,
1109 .trigger = snd_ymfpci_playback_trigger,
1110 .pointer = snd_ymfpci_playback_pointer,
1113 static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1114 .open = snd_ymfpci_capture_rec_open,
1115 .close = snd_ymfpci_capture_close,
1116 .hw_free = snd_ymfpci_capture_hw_free,
1117 .prepare = snd_ymfpci_capture_prepare,
1118 .trigger = snd_ymfpci_capture_trigger,
1119 .pointer = snd_ymfpci_capture_pointer,
1122 int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
1124 struct snd_pcm *pcm;
1127 err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm);
1130 pcm->private_data = chip;
1132 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1133 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1136 pcm->info_flags = 0;
1137 strcpy(pcm->name, "YMFPCI");
1140 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1141 &chip->pci->dev, 64*1024, 256*1024);
1143 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1144 snd_pcm_std_chmaps, 2, 0, NULL);
1147 static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1148 .open = snd_ymfpci_capture_ac97_open,
1149 .close = snd_ymfpci_capture_close,
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 snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
1158 struct snd_pcm *pcm;
1161 err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm);
1164 pcm->private_data = chip;
1166 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1169 pcm->info_flags = 0;
1170 sprintf(pcm->name, "YMFPCI - %s",
1171 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1174 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1175 &chip->pci->dev, 64*1024, 256*1024);
1180 static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1181 .open = snd_ymfpci_playback_spdif_open,
1182 .close = snd_ymfpci_playback_spdif_close,
1183 .hw_params = snd_ymfpci_playback_hw_params,
1184 .hw_free = snd_ymfpci_playback_hw_free,
1185 .prepare = snd_ymfpci_playback_prepare,
1186 .trigger = snd_ymfpci_playback_trigger,
1187 .pointer = snd_ymfpci_playback_pointer,
1190 int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
1192 struct snd_pcm *pcm;
1195 err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm);
1198 pcm->private_data = chip;
1200 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1203 pcm->info_flags = 0;
1204 strcpy(pcm->name, "YMFPCI - IEC958");
1205 chip->pcm_spdif = pcm;
1207 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1208 &chip->pci->dev, 64*1024, 256*1024);
1213 static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1214 .open = snd_ymfpci_playback_4ch_open,
1215 .close = snd_ymfpci_playback_4ch_close,
1216 .hw_params = snd_ymfpci_playback_hw_params,
1217 .hw_free = snd_ymfpci_playback_hw_free,
1218 .prepare = snd_ymfpci_playback_prepare,
1219 .trigger = snd_ymfpci_playback_trigger,
1220 .pointer = snd_ymfpci_playback_pointer,
1223 static const struct snd_pcm_chmap_elem surround_map[] = {
1225 .map = { SNDRV_CHMAP_MONO } },
1227 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1231 int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
1233 struct snd_pcm *pcm;
1236 err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm);
1239 pcm->private_data = chip;
1241 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1244 pcm->info_flags = 0;
1245 strcpy(pcm->name, "YMFPCI - Rear PCM");
1246 chip->pcm_4ch = pcm;
1248 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1249 &chip->pci->dev, 64*1024, 256*1024);
1251 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1252 surround_map, 2, 0, NULL);
1255 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1257 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1262 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1263 struct snd_ctl_elem_value *ucontrol)
1265 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1267 spin_lock_irq(&chip->reg_lock);
1268 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1269 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1270 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1271 spin_unlock_irq(&chip->reg_lock);
1275 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1276 struct snd_ctl_elem_value *ucontrol)
1278 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1282 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1283 (ucontrol->value.iec958.status[1] << 8);
1284 spin_lock_irq(&chip->reg_lock);
1285 change = chip->spdif_bits != val;
1286 chip->spdif_bits = val;
1287 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1288 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1289 spin_unlock_irq(&chip->reg_lock);
1293 static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
1295 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1296 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1297 .info = snd_ymfpci_spdif_default_info,
1298 .get = snd_ymfpci_spdif_default_get,
1299 .put = snd_ymfpci_spdif_default_put
1302 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1304 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1309 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1310 struct snd_ctl_elem_value *ucontrol)
1312 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1314 spin_lock_irq(&chip->reg_lock);
1315 ucontrol->value.iec958.status[0] = 0x3e;
1316 ucontrol->value.iec958.status[1] = 0xff;
1317 spin_unlock_irq(&chip->reg_lock);
1321 static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
1323 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1324 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1325 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1326 .info = snd_ymfpci_spdif_mask_info,
1327 .get = snd_ymfpci_spdif_mask_get,
1330 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1332 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1337 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1338 struct snd_ctl_elem_value *ucontrol)
1340 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1342 spin_lock_irq(&chip->reg_lock);
1343 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1344 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1345 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1346 spin_unlock_irq(&chip->reg_lock);
1350 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1351 struct snd_ctl_elem_value *ucontrol)
1353 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1357 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1358 (ucontrol->value.iec958.status[1] << 8);
1359 spin_lock_irq(&chip->reg_lock);
1360 change = chip->spdif_pcm_bits != val;
1361 chip->spdif_pcm_bits = val;
1362 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1363 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1364 spin_unlock_irq(&chip->reg_lock);
1368 static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
1370 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1371 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1372 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1373 .info = snd_ymfpci_spdif_stream_info,
1374 .get = snd_ymfpci_spdif_stream_get,
1375 .put = snd_ymfpci_spdif_stream_put
1378 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1380 static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
1382 return snd_ctl_enum_info(info, 1, 3, texts);
1385 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1387 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1390 spin_lock_irq(&chip->reg_lock);
1391 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1392 spin_unlock_irq(&chip->reg_lock);
1394 value->value.enumerated.item[0] = 0;
1396 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1400 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1402 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1405 spin_lock_irq(&chip->reg_lock);
1406 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1407 if (value->value.enumerated.item[0] == 0)
1408 reg = old_reg & ~0x100;
1410 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1411 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1412 spin_unlock_irq(&chip->reg_lock);
1413 return reg != old_reg;
1416 static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
1417 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1418 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1419 .name = "Direct Recording Source",
1420 .info = snd_ymfpci_drec_source_info,
1421 .get = snd_ymfpci_drec_source_get,
1422 .put = snd_ymfpci_drec_source_put
1429 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1430 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1431 .info = snd_ymfpci_info_single, \
1432 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1433 .private_value = ((reg) | ((shift) << 16)) }
1435 #define snd_ymfpci_info_single snd_ctl_boolean_mono_info
1437 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1438 struct snd_ctl_elem_value *ucontrol)
1440 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1441 int reg = kcontrol->private_value & 0xffff;
1442 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1443 unsigned int mask = 1;
1446 case YDSXGR_SPDIFOUTCTRL: break;
1447 case YDSXGR_SPDIFINCTRL: break;
1448 default: return -EINVAL;
1450 ucontrol->value.integer.value[0] =
1451 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1455 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1456 struct snd_ctl_elem_value *ucontrol)
1458 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1459 int reg = kcontrol->private_value & 0xffff;
1460 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1461 unsigned int mask = 1;
1463 unsigned int val, oval;
1466 case YDSXGR_SPDIFOUTCTRL: break;
1467 case YDSXGR_SPDIFINCTRL: break;
1468 default: return -EINVAL;
1470 val = (ucontrol->value.integer.value[0] & mask);
1472 spin_lock_irq(&chip->reg_lock);
1473 oval = snd_ymfpci_readl(chip, reg);
1474 val = (oval & ~(mask << shift)) | val;
1475 change = val != oval;
1476 snd_ymfpci_writel(chip, reg, val);
1477 spin_unlock_irq(&chip->reg_lock);
1481 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1483 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1484 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1485 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1486 .info = snd_ymfpci_info_double, \
1487 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1488 .private_value = reg, \
1489 .tlv = { .p = db_scale_native } }
1491 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1493 unsigned int reg = kcontrol->private_value;
1495 if (reg < 0x80 || reg >= 0xc0)
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1499 uinfo->value.integer.min = 0;
1500 uinfo->value.integer.max = 16383;
1504 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1506 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1507 unsigned int reg = kcontrol->private_value;
1508 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1511 if (reg < 0x80 || reg >= 0xc0)
1513 spin_lock_irq(&chip->reg_lock);
1514 val = snd_ymfpci_readl(chip, reg);
1515 spin_unlock_irq(&chip->reg_lock);
1516 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1517 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1521 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1523 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1524 unsigned int reg = kcontrol->private_value;
1525 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1527 unsigned int val1, val2, oval;
1529 if (reg < 0x80 || reg >= 0xc0)
1531 val1 = ucontrol->value.integer.value[0] & mask;
1532 val2 = ucontrol->value.integer.value[1] & mask;
1533 val1 <<= shift_left;
1534 val2 <<= shift_right;
1535 spin_lock_irq(&chip->reg_lock);
1536 oval = snd_ymfpci_readl(chip, reg);
1537 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1538 change = val1 != oval;
1539 snd_ymfpci_writel(chip, reg, val1);
1540 spin_unlock_irq(&chip->reg_lock);
1544 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_value *ucontrol)
1547 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1548 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1549 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1551 unsigned int value, oval;
1553 value = ucontrol->value.integer.value[0] & 0x3fff;
1554 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1555 spin_lock_irq(&chip->reg_lock);
1556 oval = snd_ymfpci_readl(chip, reg);
1557 change = value != oval;
1558 snd_ymfpci_writel(chip, reg, value);
1559 snd_ymfpci_writel(chip, reg2, value);
1560 spin_unlock_irq(&chip->reg_lock);
1567 #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
1569 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1571 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1572 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1576 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1578 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1580 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1582 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1586 static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
1587 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1588 .name = "4ch Duplication",
1589 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1590 .info = snd_ymfpci_info_dup4ch,
1591 .get = snd_ymfpci_get_dup4ch,
1592 .put = snd_ymfpci_put_dup4ch,
1595 static const struct snd_kcontrol_new snd_ymfpci_controls[] = {
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 Playback 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),
1629 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1632 unsigned long flags;
1634 spin_lock_irqsave(&chip->reg_lock, flags);
1635 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1636 reg &= ~(1 << (pin + 8));
1638 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1639 /* set the level mode for input line */
1640 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1641 mode &= ~(3 << (pin * 2));
1642 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1643 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1644 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1645 spin_unlock_irqrestore(&chip->reg_lock, flags);
1646 return (mode >> pin) & 1;
1649 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1652 unsigned long flags;
1654 spin_lock_irqsave(&chip->reg_lock, flags);
1655 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1657 reg &= ~(1 << (pin + 8));
1658 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1659 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1660 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1661 spin_unlock_irqrestore(&chip->reg_lock, flags);
1666 #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
1668 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1670 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1671 int pin = (int)kcontrol->private_value;
1672 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1676 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1678 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1679 int pin = (int)kcontrol->private_value;
1681 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1682 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1683 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1689 static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
1690 .name = "Shared Rear/Line-In Switch",
1691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1692 .info = snd_ymfpci_gpio_sw_info,
1693 .get = snd_ymfpci_gpio_sw_get,
1694 .put = snd_ymfpci_gpio_sw_put,
1702 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_info *uinfo)
1705 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1707 uinfo->value.integer.min = 0;
1708 uinfo->value.integer.max = 0x8000;
1712 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_value *ucontrol)
1715 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1716 unsigned int subs = kcontrol->id.subdevice;
1718 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1719 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1723 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_value *ucontrol)
1726 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1727 unsigned int subs = kcontrol->id.subdevice;
1728 struct snd_pcm_substream *substream;
1729 unsigned long flags;
1731 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1732 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1733 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1734 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1735 if (chip->pcm_mixer[subs].left > 0x8000)
1736 chip->pcm_mixer[subs].left = 0x8000;
1737 if (chip->pcm_mixer[subs].right > 0x8000)
1738 chip->pcm_mixer[subs].right = 0x8000;
1740 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1741 spin_lock_irqsave(&chip->voice_lock, flags);
1742 if (substream->runtime && substream->runtime->private_data) {
1743 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1744 if (!ypcm->use_441_slot)
1745 ypcm->update_pcm_vol = 2;
1747 spin_unlock_irqrestore(&chip->voice_lock, flags);
1753 static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
1754 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1755 .name = "PCM Playback Volume",
1756 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1757 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1758 .info = snd_ymfpci_pcm_vol_info,
1759 .get = snd_ymfpci_pcm_vol_get,
1760 .put = snd_ymfpci_pcm_vol_put,
1768 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1770 struct snd_ymfpci *chip = bus->private_data;
1771 chip->ac97_bus = NULL;
1774 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1776 struct snd_ymfpci *chip = ac97->private_data;
1780 int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1782 struct snd_ac97_template ac97;
1783 struct snd_kcontrol *kctl;
1784 struct snd_pcm_substream *substream;
1787 static const struct snd_ac97_bus_ops ops = {
1788 .write = snd_ymfpci_codec_write,
1789 .read = snd_ymfpci_codec_read,
1792 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1795 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1796 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1798 memset(&ac97, 0, sizeof(ac97));
1799 ac97.private_data = chip;
1800 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1801 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1806 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1807 AC97_EA_VRA|AC97_EA_VRM, 0);
1809 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1810 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip));
1814 if (chip->ac97->ext_id & AC97_EI_SDAC) {
1815 kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1816 err = snd_ctl_add(chip->card, kctl);
1821 /* add S/PDIF control */
1822 if (snd_BUG_ON(!chip->pcm_spdif))
1824 kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip);
1825 kctl->id.device = chip->pcm_spdif->device;
1826 err = snd_ctl_add(chip->card, kctl);
1829 kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip);
1830 kctl->id.device = chip->pcm_spdif->device;
1831 err = snd_ctl_add(chip->card, kctl);
1834 kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip);
1835 kctl->id.device = chip->pcm_spdif->device;
1836 err = snd_ctl_add(chip->card, kctl);
1839 chip->spdif_pcm_ctl = kctl;
1841 /* direct recording source */
1842 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754) {
1843 kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip);
1844 err = snd_ctl_add(chip->card, kctl);
1850 * shared rear/line-in
1853 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip));
1858 /* per-voice volume */
1859 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1860 for (idx = 0; idx < 32; ++idx) {
1861 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1864 kctl->id.device = chip->pcm->device;
1865 kctl->id.subdevice = idx;
1866 kctl->private_value = (unsigned long)substream;
1867 err = snd_ctl_add(chip->card, kctl);
1870 chip->pcm_mixer[idx].left = 0x8000;
1871 chip->pcm_mixer[idx].right = 0x8000;
1872 chip->pcm_mixer[idx].ctl = kctl;
1873 substream = substream->next;
1884 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1886 struct snd_ymfpci *chip;
1887 unsigned long flags;
1890 chip = snd_timer_chip(timer);
1891 spin_lock_irqsave(&chip->reg_lock, flags);
1892 if (timer->sticks > 1) {
1893 chip->timer_ticks = timer->sticks;
1894 count = timer->sticks - 1;
1897 * Divisor 1 is not allowed; fake it by using divisor 2 and
1898 * counting two ticks for each interrupt.
1900 chip->timer_ticks = 2;
1903 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1904 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1905 spin_unlock_irqrestore(&chip->reg_lock, flags);
1909 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1911 struct snd_ymfpci *chip;
1912 unsigned long flags;
1914 chip = snd_timer_chip(timer);
1915 spin_lock_irqsave(&chip->reg_lock, flags);
1916 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1917 spin_unlock_irqrestore(&chip->reg_lock, flags);
1921 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1922 unsigned long *num, unsigned long *den)
1929 static const struct snd_timer_hardware snd_ymfpci_timer_hw = {
1930 .flags = SNDRV_TIMER_HW_AUTO,
1931 .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1933 .start = snd_ymfpci_timer_start,
1934 .stop = snd_ymfpci_timer_stop,
1935 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1938 int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1940 struct snd_timer *timer = NULL;
1941 struct snd_timer_id tid;
1944 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1945 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1946 tid.card = chip->card->number;
1947 tid.device = device;
1949 err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer);
1951 strcpy(timer->name, "YMFPCI timer");
1952 timer->private_data = chip;
1953 timer->hw = snd_ymfpci_timer_hw;
1955 chip->timer = timer;
1964 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1965 struct snd_info_buffer *buffer)
1967 struct snd_ymfpci *chip = entry->private_data;
1970 snd_iprintf(buffer, "YMFPCI\n\n");
1971 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1972 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1975 static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1977 return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
1981 * initialization routines
1984 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1988 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1989 #if 0 // force to reset
1992 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1993 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1994 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1995 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1996 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
2002 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
2004 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
2007 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
2012 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
2014 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2015 while (timeout-- > 0) {
2016 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2017 if ((val & 0x00000002) == 0)
2022 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2027 err = reject_firmware(&chip->dsp_microcode, "/*(DEBLOBBED)*/",
2030 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2031 dev_err(chip->card->dev,
2032 "DSP microcode has wrong size\n");
2038 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2039 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2040 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2041 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2042 name = is_1e ? "/*(DEBLOBBED)*/" : "/*(DEBLOBBED)*/";
2043 err = reject_firmware(&chip->controller_microcode, name,
2046 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2047 dev_err(chip->card->dev,
2048 "controller microcode has wrong size\n");
2059 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2065 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2066 snd_ymfpci_disable_dsp(chip);
2067 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2068 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2069 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2070 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2071 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2072 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2073 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2074 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2075 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2077 /* setup DSP instruction code */
2078 inst = (const __le32 *)chip->dsp_microcode->data;
2079 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2080 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2081 le32_to_cpu(inst[i]));
2083 /* setup control instruction code */
2084 inst = (const __le32 *)chip->controller_microcode->data;
2085 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2086 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2087 le32_to_cpu(inst[i]));
2089 snd_ymfpci_enable_dsp(chip);
2092 static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2094 long size, playback_ctrl_size;
2095 int voice, bank, reg;
2097 dma_addr_t ptr_addr;
2099 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2100 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2101 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2102 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2103 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2105 size = ALIGN(playback_ctrl_size, 0x100) +
2106 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2107 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2108 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2110 /* work_ptr must be aligned to 256 bytes, but it's already
2111 covered with the kernel page allocation mechanism */
2112 chip->work_ptr = snd_devm_alloc_pages(&chip->pci->dev,
2113 SNDRV_DMA_TYPE_DEV, size);
2114 if (!chip->work_ptr)
2116 ptr = chip->work_ptr->area;
2117 ptr_addr = chip->work_ptr->addr;
2118 memset(ptr, 0, size); /* for sure */
2120 chip->bank_base_playback = ptr;
2121 chip->bank_base_playback_addr = ptr_addr;
2122 chip->ctrl_playback = (__le32 *)ptr;
2123 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2124 ptr += ALIGN(playback_ctrl_size, 0x100);
2125 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2126 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2127 chip->voices[voice].number = voice;
2128 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2129 chip->voices[voice].bank_addr = ptr_addr;
2130 for (bank = 0; bank < 2; bank++) {
2131 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2132 ptr += chip->bank_size_playback;
2133 ptr_addr += chip->bank_size_playback;
2136 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2137 ptr_addr = ALIGN(ptr_addr, 0x100);
2138 chip->bank_base_capture = ptr;
2139 chip->bank_base_capture_addr = ptr_addr;
2140 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2141 for (bank = 0; bank < 2; bank++) {
2142 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2143 ptr += chip->bank_size_capture;
2144 ptr_addr += chip->bank_size_capture;
2146 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2147 ptr_addr = ALIGN(ptr_addr, 0x100);
2148 chip->bank_base_effect = ptr;
2149 chip->bank_base_effect_addr = ptr_addr;
2150 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2151 for (bank = 0; bank < 2; bank++) {
2152 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2153 ptr += chip->bank_size_effect;
2154 ptr_addr += chip->bank_size_effect;
2156 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2157 ptr_addr = ALIGN(ptr_addr, 0x100);
2158 chip->work_base = ptr;
2159 chip->work_base_addr = ptr_addr;
2161 snd_BUG_ON(ptr + PAGE_ALIGN(chip->work_size) !=
2162 chip->work_ptr->area + chip->work_ptr->bytes);
2164 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2165 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2166 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2167 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2168 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2170 /* S/PDIF output initialization */
2171 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2172 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2173 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2175 /* S/PDIF input initialization */
2176 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2178 /* digital mixer setup */
2179 for (reg = 0x80; reg < 0xc0; reg += 4)
2180 snd_ymfpci_writel(chip, reg, 0);
2181 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2182 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2183 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2184 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2185 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2186 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2187 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2188 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2193 static void snd_ymfpci_free(struct snd_card *card)
2195 struct snd_ymfpci *chip = card->private_data;
2198 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2199 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2200 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2201 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2202 snd_ymfpci_disable_dsp(chip);
2203 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2204 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2205 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2206 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2207 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2208 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2209 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2211 snd_ymfpci_ac3_done(chip);
2213 snd_ymfpci_free_gameport(chip);
2215 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, chip->old_legacy_ctrl);
2217 release_firmware(chip->dsp_microcode);
2218 release_firmware(chip->controller_microcode);
2221 static int snd_ymfpci_suspend(struct device *dev)
2223 struct snd_card *card = dev_get_drvdata(dev);
2224 struct snd_ymfpci *chip = card->private_data;
2225 unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
2227 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
2228 legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
2230 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2231 snd_ac97_suspend(chip->ac97);
2233 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2234 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2236 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2238 for (i = 0; i < legacy_reg_count; i++)
2239 pci_read_config_word(chip->pci, pci_saved_regs_index[i],
2240 chip->saved_dsxg_pci_regs + i);
2242 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2243 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2244 snd_ymfpci_disable_dsp(chip);
2248 static int snd_ymfpci_resume(struct device *dev)
2250 struct pci_dev *pci = to_pci_dev(dev);
2251 struct snd_card *card = dev_get_drvdata(dev);
2252 struct snd_ymfpci *chip = card->private_data;
2253 unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
2255 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
2256 legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
2258 snd_ymfpci_aclink_reset(pci);
2259 snd_ymfpci_codec_ready(chip, 0);
2260 snd_ymfpci_download_image(chip);
2263 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2264 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2266 snd_ac97_resume(chip->ac97);
2268 for (i = 0; i < legacy_reg_count; i++)
2269 pci_write_config_word(chip->pci, pci_saved_regs_index[i],
2270 chip->saved_dsxg_pci_regs[i]);
2272 /* start hw again */
2273 if (chip->start_count > 0) {
2274 spin_lock_irq(&chip->reg_lock);
2275 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2276 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2277 spin_unlock_irq(&chip->reg_lock);
2279 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2283 DEFINE_SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2285 int snd_ymfpci_create(struct snd_card *card,
2286 struct pci_dev *pci,
2287 u16 old_legacy_ctrl)
2289 struct snd_ymfpci *chip = card->private_data;
2292 /* enable PCI device */
2293 err = pcim_enable_device(pci);
2297 chip->old_legacy_ctrl = old_legacy_ctrl;
2298 spin_lock_init(&chip->reg_lock);
2299 spin_lock_init(&chip->voice_lock);
2300 init_waitqueue_head(&chip->interrupt_sleep);
2301 atomic_set(&chip->interrupt_sleep_count, 0);
2305 chip->device_id = pci->device;
2306 chip->rev = pci->revision;
2308 err = pci_request_regions(pci, "YMFPCI");
2312 chip->reg_area_phys = pci_resource_start(pci, 0);
2313 chip->reg_area_virt = devm_ioremap(&pci->dev, chip->reg_area_phys, 0x8000);
2314 if (!chip->reg_area_virt) {
2315 dev_err(chip->card->dev,
2316 "unable to grab memory region 0x%lx-0x%lx\n",
2317 chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2320 pci_set_master(pci);
2321 chip->src441_used = -1;
2323 if (devm_request_irq(&pci->dev, pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2324 KBUILD_MODNAME, chip)) {
2325 dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2328 chip->irq = pci->irq;
2329 card->sync_irq = chip->irq;
2330 card->private_free = snd_ymfpci_free;
2332 snd_ymfpci_aclink_reset(pci);
2333 if (snd_ymfpci_codec_ready(chip, 0) < 0)
2336 err = snd_ymfpci_request_firmware(chip);
2338 dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2341 snd_ymfpci_download_image(chip);
2343 udelay(100); /* seems we need a delay after downloading image.. */
2345 if (snd_ymfpci_memalloc(chip) < 0)
2348 err = snd_ymfpci_ac3_init(chip);
2352 snd_ymfpci_proc_init(card, chip);