Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / pci / ymfpci / ymfpci_main.c
blob42c1eb7d35f5d2b2e1811d32a3a893d705b1d2a8
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Routines for control of YMF724/740/744/754 chips
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/tlv.h>
34 #include <sound/ymfpci.h>
35 #include <sound/asoundef.h>
36 #include <sound/mpu401.h>
38 #include <asm/io.h>
39 #include <asm/byteorder.h>
42 * common I/O routines
45 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
47 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
49 return readb(chip->reg_area_virt + offset);
52 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
54 writeb(val, chip->reg_area_virt + offset);
57 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
59 return readw(chip->reg_area_virt + offset);
62 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
64 writew(val, chip->reg_area_virt + offset);
67 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
69 return readl(chip->reg_area_virt + offset);
72 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
74 writel(val, chip->reg_area_virt + offset);
77 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
79 unsigned long end_time;
80 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82 end_time = jiffies + msecs_to_jiffies(750);
83 do {
84 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
85 return 0;
86 schedule_timeout_uninterruptible(1);
87 } while (time_before(jiffies, end_time));
88 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
89 return -EBUSY;
92 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
94 struct snd_ymfpci *chip = ac97->private_data;
95 u32 cmd;
97 snd_ymfpci_codec_ready(chip, 0);
98 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
99 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
102 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
104 struct snd_ymfpci *chip = ac97->private_data;
106 if (snd_ymfpci_codec_ready(chip, 0))
107 return ~0;
108 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
109 if (snd_ymfpci_codec_ready(chip, 0))
110 return ~0;
111 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
112 int i;
113 for (i = 0; i < 600; i++)
114 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
116 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
120 * Misc routines
123 static u32 snd_ymfpci_calc_delta(u32 rate)
125 switch (rate) {
126 case 8000: return 0x02aaab00;
127 case 11025: return 0x03accd00;
128 case 16000: return 0x05555500;
129 case 22050: return 0x07599a00;
130 case 32000: return 0x0aaaab00;
131 case 44100: return 0x0eb33300;
132 default: return ((rate << 16) / 375) << 5;
136 static u32 def_rate[8] = {
137 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
140 static u32 snd_ymfpci_calc_lpfK(u32 rate)
142 u32 i;
143 static u32 val[8] = {
144 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
145 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
148 if (rate == 44100)
149 return 0x40000000; /* FIXME: What's the right value? */
150 for (i = 0; i < 8; i++)
151 if (rate <= def_rate[i])
152 return val[i];
153 return val[0];
156 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
158 u32 i;
159 static u32 val[8] = {
160 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
161 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
164 if (rate == 44100)
165 return 0x370A0000;
166 for (i = 0; i < 8; i++)
167 if (rate <= def_rate[i])
168 return val[i];
169 return val[0];
173 * Hardware start management
176 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
178 unsigned long flags;
180 spin_lock_irqsave(&chip->reg_lock, flags);
181 if (chip->start_count++ > 0)
182 goto __end;
183 snd_ymfpci_writel(chip, YDSXGR_MODE,
184 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
185 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
186 __end:
187 spin_unlock_irqrestore(&chip->reg_lock, flags);
190 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
192 unsigned long flags;
193 long timeout = 1000;
195 spin_lock_irqsave(&chip->reg_lock, flags);
196 if (--chip->start_count > 0)
197 goto __end;
198 snd_ymfpci_writel(chip, YDSXGR_MODE,
199 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
200 while (timeout-- > 0) {
201 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
202 break;
204 if (atomic_read(&chip->interrupt_sleep_count)) {
205 atomic_set(&chip->interrupt_sleep_count, 0);
206 wake_up(&chip->interrupt_sleep);
208 __end:
209 spin_unlock_irqrestore(&chip->reg_lock, flags);
213 * Playback voice management
216 static int voice_alloc(struct snd_ymfpci *chip,
217 enum snd_ymfpci_voice_type type, int pair,
218 struct snd_ymfpci_voice **rvoice)
220 struct snd_ymfpci_voice *voice, *voice2;
221 int idx;
223 *rvoice = NULL;
224 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
225 voice = &chip->voices[idx];
226 voice2 = pair ? &chip->voices[idx+1] : NULL;
227 if (voice->use || (voice2 && voice2->use))
228 continue;
229 voice->use = 1;
230 if (voice2)
231 voice2->use = 1;
232 switch (type) {
233 case YMFPCI_PCM:
234 voice->pcm = 1;
235 if (voice2)
236 voice2->pcm = 1;
237 break;
238 case YMFPCI_SYNTH:
239 voice->synth = 1;
240 break;
241 case YMFPCI_MIDI:
242 voice->midi = 1;
243 break;
245 snd_ymfpci_hw_start(chip);
246 if (voice2)
247 snd_ymfpci_hw_start(chip);
248 *rvoice = voice;
249 return 0;
251 return -ENOMEM;
254 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
255 enum snd_ymfpci_voice_type type, int pair,
256 struct snd_ymfpci_voice **rvoice)
258 unsigned long flags;
259 int result;
261 snd_assert(rvoice != NULL, return -EINVAL);
262 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
264 spin_lock_irqsave(&chip->voice_lock, flags);
265 for (;;) {
266 result = voice_alloc(chip, type, pair, rvoice);
267 if (result == 0 || type != YMFPCI_PCM)
268 break;
269 /* TODO: synth/midi voice deallocation */
270 break;
272 spin_unlock_irqrestore(&chip->voice_lock, flags);
273 return result;
276 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
278 unsigned long flags;
280 snd_assert(pvoice != NULL, return -EINVAL);
281 snd_ymfpci_hw_stop(chip);
282 spin_lock_irqsave(&chip->voice_lock, flags);
283 if (pvoice->number == chip->src441_used) {
284 chip->src441_used = -1;
285 pvoice->ypcm->use_441_slot = 0;
287 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
288 pvoice->ypcm = NULL;
289 pvoice->interrupt = NULL;
290 spin_unlock_irqrestore(&chip->voice_lock, flags);
291 return 0;
295 * PCM part
298 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
300 struct snd_ymfpci_pcm *ypcm;
301 u32 pos, delta;
303 if ((ypcm = voice->ypcm) == NULL)
304 return;
305 if (ypcm->substream == NULL)
306 return;
307 spin_lock(&chip->reg_lock);
308 if (ypcm->running) {
309 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
310 if (pos < ypcm->last_pos)
311 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
312 else
313 delta = pos - ypcm->last_pos;
314 ypcm->period_pos += delta;
315 ypcm->last_pos = pos;
316 if (ypcm->period_pos >= ypcm->period_size) {
317 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
318 ypcm->period_pos %= ypcm->period_size;
319 spin_unlock(&chip->reg_lock);
320 snd_pcm_period_elapsed(ypcm->substream);
321 spin_lock(&chip->reg_lock);
324 if (unlikely(ypcm->update_pcm_vol)) {
325 unsigned int subs = ypcm->substream->number;
326 unsigned int next_bank = 1 - chip->active_bank;
327 struct snd_ymfpci_playback_bank *bank;
328 u32 volume;
330 bank = &voice->bank[next_bank];
331 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
332 bank->left_gain_end = volume;
333 if (ypcm->output_rear)
334 bank->eff2_gain_end = volume;
335 if (ypcm->voices[1])
336 bank = &ypcm->voices[1]->bank[next_bank];
337 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
338 bank->right_gain_end = volume;
339 if (ypcm->output_rear)
340 bank->eff3_gain_end = volume;
341 ypcm->update_pcm_vol--;
344 spin_unlock(&chip->reg_lock);
347 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
351 struct snd_ymfpci *chip = ypcm->chip;
352 u32 pos, delta;
354 spin_lock(&chip->reg_lock);
355 if (ypcm->running) {
356 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
357 if (pos < ypcm->last_pos)
358 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
359 else
360 delta = pos - ypcm->last_pos;
361 ypcm->period_pos += delta;
362 ypcm->last_pos = pos;
363 if (ypcm->period_pos >= ypcm->period_size) {
364 ypcm->period_pos %= ypcm->period_size;
365 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
366 spin_unlock(&chip->reg_lock);
367 snd_pcm_period_elapsed(substream);
368 spin_lock(&chip->reg_lock);
371 spin_unlock(&chip->reg_lock);
374 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
375 int cmd)
377 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
378 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
379 struct snd_kcontrol *kctl = NULL;
380 int result = 0;
382 spin_lock(&chip->reg_lock);
383 if (ypcm->voices[0] == NULL) {
384 result = -EINVAL;
385 goto __unlock;
387 switch (cmd) {
388 case SNDRV_PCM_TRIGGER_START:
389 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
390 case SNDRV_PCM_TRIGGER_RESUME:
391 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
392 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
393 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
394 ypcm->running = 1;
395 break;
396 case SNDRV_PCM_TRIGGER_STOP:
397 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
398 kctl = chip->pcm_mixer[substream->number].ctl;
399 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
401 /* fall through */
402 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
403 case SNDRV_PCM_TRIGGER_SUSPEND:
404 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
405 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
406 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
407 ypcm->running = 0;
408 break;
409 default:
410 result = -EINVAL;
411 break;
413 __unlock:
414 spin_unlock(&chip->reg_lock);
415 if (kctl)
416 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
417 return result;
419 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
420 int cmd)
422 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
423 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
424 int result = 0;
425 u32 tmp;
427 spin_lock(&chip->reg_lock);
428 switch (cmd) {
429 case SNDRV_PCM_TRIGGER_START:
430 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
431 case SNDRV_PCM_TRIGGER_RESUME:
432 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
433 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
434 ypcm->running = 1;
435 break;
436 case SNDRV_PCM_TRIGGER_STOP:
437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
438 case SNDRV_PCM_TRIGGER_SUSPEND:
439 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
440 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
441 ypcm->running = 0;
442 break;
443 default:
444 result = -EINVAL;
445 break;
447 spin_unlock(&chip->reg_lock);
448 return result;
451 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
453 int err;
455 if (ypcm->voices[1] != NULL && voices < 2) {
456 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
457 ypcm->voices[1] = NULL;
459 if (voices == 1 && ypcm->voices[0] != NULL)
460 return 0; /* already allocated */
461 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
462 return 0; /* already allocated */
463 if (voices > 1) {
464 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
465 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
466 ypcm->voices[0] = NULL;
469 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
470 if (err < 0)
471 return err;
472 ypcm->voices[0]->ypcm = ypcm;
473 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
474 if (voices > 1) {
475 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
476 ypcm->voices[1]->ypcm = ypcm;
478 return 0;
481 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
482 struct snd_pcm_runtime *runtime,
483 int has_pcm_volume)
485 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
486 u32 format;
487 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
488 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
489 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
490 struct snd_ymfpci_playback_bank *bank;
491 unsigned int nbank;
492 u32 vol_left, vol_right;
493 u8 use_left, use_right;
494 unsigned long flags;
496 snd_assert(voice != NULL, return);
497 if (runtime->channels == 1) {
498 use_left = 1;
499 use_right = 1;
500 } else {
501 use_left = (voiceidx & 1) == 0;
502 use_right = !use_left;
504 if (has_pcm_volume) {
505 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
506 [ypcm->substream->number].left << 15);
507 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
508 [ypcm->substream->number].right << 15);
509 } else {
510 vol_left = cpu_to_le32(0x40000000);
511 vol_right = cpu_to_le32(0x40000000);
513 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
514 format = runtime->channels == 2 ? 0x00010000 : 0;
515 if (snd_pcm_format_width(runtime->format) == 8)
516 format |= 0x80000000;
517 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
518 runtime->rate == 44100 && runtime->channels == 2 &&
519 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
520 ypcm->chip->src441_used == voice->number)) {
521 ypcm->chip->src441_used = voice->number;
522 ypcm->use_441_slot = 1;
523 format |= 0x10000000;
525 if (ypcm->chip->src441_used == voice->number &&
526 (format & 0x10000000) == 0) {
527 ypcm->chip->src441_used = -1;
528 ypcm->use_441_slot = 0;
530 if (runtime->channels == 2 && (voiceidx & 1) != 0)
531 format |= 1;
532 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
533 for (nbank = 0; nbank < 2; nbank++) {
534 bank = &voice->bank[nbank];
535 memset(bank, 0, sizeof(*bank));
536 bank->format = cpu_to_le32(format);
537 bank->base = cpu_to_le32(runtime->dma_addr);
538 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
539 bank->lpfQ = cpu_to_le32(lpfQ);
540 bank->delta =
541 bank->delta_end = cpu_to_le32(delta);
542 bank->lpfK =
543 bank->lpfK_end = cpu_to_le32(lpfK);
544 bank->eg_gain =
545 bank->eg_gain_end = cpu_to_le32(0x40000000);
547 if (ypcm->output_front) {
548 if (use_left) {
549 bank->left_gain =
550 bank->left_gain_end = vol_left;
552 if (use_right) {
553 bank->right_gain =
554 bank->right_gain_end = vol_right;
557 if (ypcm->output_rear) {
558 if (!ypcm->swap_rear) {
559 if (use_left) {
560 bank->eff2_gain =
561 bank->eff2_gain_end = vol_left;
563 if (use_right) {
564 bank->eff3_gain =
565 bank->eff3_gain_end = vol_right;
567 } else {
568 /* The SPDIF out channels seem to be swapped, so we have
569 * to swap them here, too. The rear analog out channels
570 * will be wrong, but otherwise AC3 would not work.
572 if (use_left) {
573 bank->eff3_gain =
574 bank->eff3_gain_end = vol_left;
576 if (use_right) {
577 bank->eff2_gain =
578 bank->eff2_gain_end = vol_right;
585 static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
587 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
588 4096, &chip->ac3_tmp_base) < 0)
589 return -ENOMEM;
591 chip->bank_effect[3][0]->base =
592 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
593 chip->bank_effect[3][0]->loop_end =
594 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
595 chip->bank_effect[4][0]->base =
596 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
597 chip->bank_effect[4][0]->loop_end =
598 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
600 spin_lock_irq(&chip->reg_lock);
601 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
602 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
603 spin_unlock_irq(&chip->reg_lock);
604 return 0;
607 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
609 spin_lock_irq(&chip->reg_lock);
610 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
611 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
612 spin_unlock_irq(&chip->reg_lock);
613 // snd_ymfpci_irq_wait(chip);
614 if (chip->ac3_tmp_base.area) {
615 snd_dma_free_pages(&chip->ac3_tmp_base);
616 chip->ac3_tmp_base.area = NULL;
618 return 0;
621 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
622 struct snd_pcm_hw_params *hw_params)
624 struct snd_pcm_runtime *runtime = substream->runtime;
625 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
626 int err;
628 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
629 return err;
630 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
631 return err;
632 return 0;
635 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
637 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
638 struct snd_pcm_runtime *runtime = substream->runtime;
639 struct snd_ymfpci_pcm *ypcm;
641 if (runtime->private_data == NULL)
642 return 0;
643 ypcm = runtime->private_data;
645 /* wait, until the PCI operations are not finished */
646 snd_ymfpci_irq_wait(chip);
647 snd_pcm_lib_free_pages(substream);
648 if (ypcm->voices[1]) {
649 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
650 ypcm->voices[1] = NULL;
652 if (ypcm->voices[0]) {
653 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
654 ypcm->voices[0] = NULL;
656 return 0;
659 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
661 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
664 struct snd_kcontrol *kctl;
665 unsigned int nvoice;
667 ypcm->period_size = runtime->period_size;
668 ypcm->buffer_size = runtime->buffer_size;
669 ypcm->period_pos = 0;
670 ypcm->last_pos = 0;
671 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
672 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
673 substream->pcm == chip->pcm);
675 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
676 kctl = chip->pcm_mixer[substream->number].ctl;
677 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
680 return 0;
683 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
684 struct snd_pcm_hw_params *hw_params)
686 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
689 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
691 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
693 /* wait, until the PCI operations are not finished */
694 snd_ymfpci_irq_wait(chip);
695 return snd_pcm_lib_free_pages(substream);
698 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
700 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
701 struct snd_pcm_runtime *runtime = substream->runtime;
702 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
703 struct snd_ymfpci_capture_bank * bank;
704 int nbank;
705 u32 rate, format;
707 ypcm->period_size = runtime->period_size;
708 ypcm->buffer_size = runtime->buffer_size;
709 ypcm->period_pos = 0;
710 ypcm->last_pos = 0;
711 ypcm->shift = 0;
712 rate = ((48000 * 4096) / runtime->rate) - 1;
713 format = 0;
714 if (runtime->channels == 2) {
715 format |= 2;
716 ypcm->shift++;
718 if (snd_pcm_format_width(runtime->format) == 8)
719 format |= 1;
720 else
721 ypcm->shift++;
722 switch (ypcm->capture_bank_number) {
723 case 0:
724 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
725 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
726 break;
727 case 1:
728 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
729 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
730 break;
732 for (nbank = 0; nbank < 2; nbank++) {
733 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
734 bank->base = cpu_to_le32(runtime->dma_addr);
735 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
736 bank->start = 0;
737 bank->num_of_loops = 0;
739 return 0;
742 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
744 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
745 struct snd_pcm_runtime *runtime = substream->runtime;
746 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
747 struct snd_ymfpci_voice *voice = ypcm->voices[0];
749 if (!(ypcm->running && voice))
750 return 0;
751 return le32_to_cpu(voice->bank[chip->active_bank].start);
754 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
756 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
757 struct snd_pcm_runtime *runtime = substream->runtime;
758 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
760 if (!ypcm->running)
761 return 0;
762 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
765 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
767 wait_queue_t wait;
768 int loops = 4;
770 while (loops-- > 0) {
771 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
772 continue;
773 init_waitqueue_entry(&wait, current);
774 add_wait_queue(&chip->interrupt_sleep, &wait);
775 atomic_inc(&chip->interrupt_sleep_count);
776 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
777 remove_wait_queue(&chip->interrupt_sleep, &wait);
781 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
783 struct snd_ymfpci *chip = dev_id;
784 u32 status, nvoice, mode;
785 struct snd_ymfpci_voice *voice;
787 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
788 if (status & 0x80000000) {
789 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
790 spin_lock(&chip->voice_lock);
791 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
792 voice = &chip->voices[nvoice];
793 if (voice->interrupt)
794 voice->interrupt(chip, voice);
796 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
797 if (chip->capture_substream[nvoice])
798 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
800 #if 0
801 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
802 if (chip->effect_substream[nvoice])
803 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
805 #endif
806 spin_unlock(&chip->voice_lock);
807 spin_lock(&chip->reg_lock);
808 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
809 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
810 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
811 spin_unlock(&chip->reg_lock);
813 if (atomic_read(&chip->interrupt_sleep_count)) {
814 atomic_set(&chip->interrupt_sleep_count, 0);
815 wake_up(&chip->interrupt_sleep);
819 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
820 if (status & 1) {
821 if (chip->timer)
822 snd_timer_interrupt(chip->timer, chip->timer->sticks);
824 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
826 if (chip->rawmidi)
827 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
828 return IRQ_HANDLED;
831 static struct snd_pcm_hardware snd_ymfpci_playback =
833 .info = (SNDRV_PCM_INFO_MMAP |
834 SNDRV_PCM_INFO_MMAP_VALID |
835 SNDRV_PCM_INFO_INTERLEAVED |
836 SNDRV_PCM_INFO_BLOCK_TRANSFER |
837 SNDRV_PCM_INFO_PAUSE |
838 SNDRV_PCM_INFO_RESUME),
839 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
840 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
841 .rate_min = 8000,
842 .rate_max = 48000,
843 .channels_min = 1,
844 .channels_max = 2,
845 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
846 .period_bytes_min = 64,
847 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
848 .periods_min = 3,
849 .periods_max = 1024,
850 .fifo_size = 0,
853 static struct snd_pcm_hardware snd_ymfpci_capture =
855 .info = (SNDRV_PCM_INFO_MMAP |
856 SNDRV_PCM_INFO_MMAP_VALID |
857 SNDRV_PCM_INFO_INTERLEAVED |
858 SNDRV_PCM_INFO_BLOCK_TRANSFER |
859 SNDRV_PCM_INFO_PAUSE |
860 SNDRV_PCM_INFO_RESUME),
861 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
862 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
863 .rate_min = 8000,
864 .rate_max = 48000,
865 .channels_min = 1,
866 .channels_max = 2,
867 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
868 .period_bytes_min = 64,
869 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
870 .periods_min = 3,
871 .periods_max = 1024,
872 .fifo_size = 0,
875 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
877 kfree(runtime->private_data);
880 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
882 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_ymfpci_pcm *ypcm;
886 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
887 if (ypcm == NULL)
888 return -ENOMEM;
889 ypcm->chip = chip;
890 ypcm->type = PLAYBACK_VOICE;
891 ypcm->substream = substream;
892 runtime->hw = snd_ymfpci_playback;
893 runtime->private_data = ypcm;
894 runtime->private_free = snd_ymfpci_pcm_free_substream;
895 /* FIXME? True value is 256/48 = 5.33333 ms */
896 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
897 return 0;
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;
930 int err;
932 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
933 return err;
934 ypcm = runtime->private_data;
935 ypcm->output_front = 1;
936 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
937 ypcm->swap_rear = 0;
938 spin_lock_irq(&chip->reg_lock);
939 if (ypcm->output_rear) {
940 ymfpci_open_extension(chip);
941 chip->rear_opened++;
943 spin_unlock_irq(&chip->reg_lock);
944 return 0;
947 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
949 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
950 struct snd_pcm_runtime *runtime = substream->runtime;
951 struct snd_ymfpci_pcm *ypcm;
952 int err;
954 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
955 return err;
956 ypcm = runtime->private_data;
957 ypcm->output_front = 0;
958 ypcm->output_rear = 1;
959 ypcm->swap_rear = 1;
960 spin_lock_irq(&chip->reg_lock);
961 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
962 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
963 ymfpci_open_extension(chip);
964 chip->spdif_pcm_bits = chip->spdif_bits;
965 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
966 chip->spdif_opened++;
967 spin_unlock_irq(&chip->reg_lock);
969 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
970 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
971 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
972 return 0;
975 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
977 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
978 struct snd_pcm_runtime *runtime = substream->runtime;
979 struct snd_ymfpci_pcm *ypcm;
980 int err;
982 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
983 return err;
984 ypcm = runtime->private_data;
985 ypcm->output_front = 0;
986 ypcm->output_rear = 1;
987 ypcm->swap_rear = 0;
988 spin_lock_irq(&chip->reg_lock);
989 ymfpci_open_extension(chip);
990 chip->rear_opened++;
991 spin_unlock_irq(&chip->reg_lock);
992 return 0;
995 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
996 u32 capture_bank_number)
998 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
999 struct snd_pcm_runtime *runtime = substream->runtime;
1000 struct snd_ymfpci_pcm *ypcm;
1002 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1003 if (ypcm == NULL)
1004 return -ENOMEM;
1005 ypcm->chip = chip;
1006 ypcm->type = capture_bank_number + CAPTURE_REC;
1007 ypcm->substream = substream;
1008 ypcm->capture_bank_number = capture_bank_number;
1009 chip->capture_substream[capture_bank_number] = substream;
1010 runtime->hw = snd_ymfpci_capture;
1011 /* FIXME? True value is 256/48 = 5.33333 ms */
1012 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1013 runtime->private_data = ypcm;
1014 runtime->private_free = snd_ymfpci_pcm_free_substream;
1015 snd_ymfpci_hw_start(chip);
1016 return 0;
1019 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1021 return snd_ymfpci_capture_open(substream, 0);
1024 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1026 return snd_ymfpci_capture_open(substream, 1);
1029 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1031 return 0;
1034 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1036 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1037 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1039 spin_lock_irq(&chip->reg_lock);
1040 if (ypcm->output_rear && chip->rear_opened > 0) {
1041 chip->rear_opened--;
1042 ymfpci_close_extension(chip);
1044 spin_unlock_irq(&chip->reg_lock);
1045 return snd_ymfpci_playback_close_1(substream);
1048 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1050 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1052 spin_lock_irq(&chip->reg_lock);
1053 chip->spdif_opened = 0;
1054 ymfpci_close_extension(chip);
1055 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1056 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1057 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1058 spin_unlock_irq(&chip->reg_lock);
1059 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1060 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1061 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1062 return snd_ymfpci_playback_close_1(substream);
1065 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1067 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1069 spin_lock_irq(&chip->reg_lock);
1070 if (chip->rear_opened > 0) {
1071 chip->rear_opened--;
1072 ymfpci_close_extension(chip);
1074 spin_unlock_irq(&chip->reg_lock);
1075 return snd_ymfpci_playback_close_1(substream);
1078 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1080 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1081 struct snd_pcm_runtime *runtime = substream->runtime;
1082 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1084 if (ypcm != NULL) {
1085 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1086 snd_ymfpci_hw_stop(chip);
1088 return 0;
1091 static struct snd_pcm_ops snd_ymfpci_playback_ops = {
1092 .open = snd_ymfpci_playback_open,
1093 .close = snd_ymfpci_playback_close,
1094 .ioctl = snd_pcm_lib_ioctl,
1095 .hw_params = snd_ymfpci_playback_hw_params,
1096 .hw_free = snd_ymfpci_playback_hw_free,
1097 .prepare = snd_ymfpci_playback_prepare,
1098 .trigger = snd_ymfpci_playback_trigger,
1099 .pointer = snd_ymfpci_playback_pointer,
1102 static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1103 .open = snd_ymfpci_capture_rec_open,
1104 .close = snd_ymfpci_capture_close,
1105 .ioctl = snd_pcm_lib_ioctl,
1106 .hw_params = snd_ymfpci_capture_hw_params,
1107 .hw_free = snd_ymfpci_capture_hw_free,
1108 .prepare = snd_ymfpci_capture_prepare,
1109 .trigger = snd_ymfpci_capture_trigger,
1110 .pointer = snd_ymfpci_capture_pointer,
1113 int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1115 struct snd_pcm *pcm;
1116 int err;
1118 if (rpcm)
1119 *rpcm = NULL;
1120 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1121 return err;
1122 pcm->private_data = chip;
1124 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1125 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1127 /* global setup */
1128 pcm->info_flags = 0;
1129 strcpy(pcm->name, "YMFPCI");
1130 chip->pcm = pcm;
1132 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1133 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1135 if (rpcm)
1136 *rpcm = pcm;
1137 return 0;
1140 static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1141 .open = snd_ymfpci_capture_ac97_open,
1142 .close = snd_ymfpci_capture_close,
1143 .ioctl = snd_pcm_lib_ioctl,
1144 .hw_params = snd_ymfpci_capture_hw_params,
1145 .hw_free = snd_ymfpci_capture_hw_free,
1146 .prepare = snd_ymfpci_capture_prepare,
1147 .trigger = snd_ymfpci_capture_trigger,
1148 .pointer = snd_ymfpci_capture_pointer,
1151 int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1153 struct snd_pcm *pcm;
1154 int err;
1156 if (rpcm)
1157 *rpcm = NULL;
1158 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1159 return err;
1160 pcm->private_data = chip;
1162 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1164 /* global setup */
1165 pcm->info_flags = 0;
1166 sprintf(pcm->name, "YMFPCI - %s",
1167 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1168 chip->pcm2 = pcm;
1170 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1171 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1173 if (rpcm)
1174 *rpcm = pcm;
1175 return 0;
1178 static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1179 .open = snd_ymfpci_playback_spdif_open,
1180 .close = snd_ymfpci_playback_spdif_close,
1181 .ioctl = snd_pcm_lib_ioctl,
1182 .hw_params = snd_ymfpci_playback_hw_params,
1183 .hw_free = snd_ymfpci_playback_hw_free,
1184 .prepare = snd_ymfpci_playback_prepare,
1185 .trigger = snd_ymfpci_playback_trigger,
1186 .pointer = snd_ymfpci_playback_pointer,
1189 int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1191 struct snd_pcm *pcm;
1192 int err;
1194 if (rpcm)
1195 *rpcm = NULL;
1196 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1197 return err;
1198 pcm->private_data = chip;
1200 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1202 /* global setup */
1203 pcm->info_flags = 0;
1204 strcpy(pcm->name, "YMFPCI - IEC958");
1205 chip->pcm_spdif = pcm;
1207 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1208 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1210 if (rpcm)
1211 *rpcm = pcm;
1212 return 0;
1215 static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1216 .open = snd_ymfpci_playback_4ch_open,
1217 .close = snd_ymfpci_playback_4ch_close,
1218 .ioctl = snd_pcm_lib_ioctl,
1219 .hw_params = snd_ymfpci_playback_hw_params,
1220 .hw_free = snd_ymfpci_playback_hw_free,
1221 .prepare = snd_ymfpci_playback_prepare,
1222 .trigger = snd_ymfpci_playback_trigger,
1223 .pointer = snd_ymfpci_playback_pointer,
1226 int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1228 struct snd_pcm *pcm;
1229 int err;
1231 if (rpcm)
1232 *rpcm = NULL;
1233 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1234 return err;
1235 pcm->private_data = chip;
1237 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1239 /* global setup */
1240 pcm->info_flags = 0;
1241 strcpy(pcm->name, "YMFPCI - Rear PCM");
1242 chip->pcm_4ch = pcm;
1244 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1245 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1247 if (rpcm)
1248 *rpcm = pcm;
1249 return 0;
1252 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1254 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1255 uinfo->count = 1;
1256 return 0;
1259 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1260 struct snd_ctl_elem_value *ucontrol)
1262 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1264 spin_lock_irq(&chip->reg_lock);
1265 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1266 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1267 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1268 spin_unlock_irq(&chip->reg_lock);
1269 return 0;
1272 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1273 struct snd_ctl_elem_value *ucontrol)
1275 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1276 unsigned int val;
1277 int change;
1279 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1280 (ucontrol->value.iec958.status[1] << 8);
1281 spin_lock_irq(&chip->reg_lock);
1282 change = chip->spdif_bits != val;
1283 chip->spdif_bits = val;
1284 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1285 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1286 spin_unlock_irq(&chip->reg_lock);
1287 return change;
1290 static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
1292 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1293 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1294 .info = snd_ymfpci_spdif_default_info,
1295 .get = snd_ymfpci_spdif_default_get,
1296 .put = snd_ymfpci_spdif_default_put
1299 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1301 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1302 uinfo->count = 1;
1303 return 0;
1306 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1307 struct snd_ctl_elem_value *ucontrol)
1309 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1311 spin_lock_irq(&chip->reg_lock);
1312 ucontrol->value.iec958.status[0] = 0x3e;
1313 ucontrol->value.iec958.status[1] = 0xff;
1314 spin_unlock_irq(&chip->reg_lock);
1315 return 0;
1318 static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
1320 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1321 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1322 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1323 .info = snd_ymfpci_spdif_mask_info,
1324 .get = snd_ymfpci_spdif_mask_get,
1327 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1329 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1330 uinfo->count = 1;
1331 return 0;
1334 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1335 struct snd_ctl_elem_value *ucontrol)
1337 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1339 spin_lock_irq(&chip->reg_lock);
1340 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1341 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1342 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1343 spin_unlock_irq(&chip->reg_lock);
1344 return 0;
1347 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_value *ucontrol)
1350 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1351 unsigned int val;
1352 int change;
1354 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1355 (ucontrol->value.iec958.status[1] << 8);
1356 spin_lock_irq(&chip->reg_lock);
1357 change = chip->spdif_pcm_bits != val;
1358 chip->spdif_pcm_bits = val;
1359 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1360 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1361 spin_unlock_irq(&chip->reg_lock);
1362 return change;
1365 static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
1367 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1368 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1369 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1370 .info = snd_ymfpci_spdif_stream_info,
1371 .get = snd_ymfpci_spdif_stream_get,
1372 .put = snd_ymfpci_spdif_stream_put
1375 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1377 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1379 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1380 info->count = 1;
1381 info->value.enumerated.items = 3;
1382 if (info->value.enumerated.item > 2)
1383 info->value.enumerated.item = 2;
1384 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1385 return 0;
1388 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1390 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1391 u16 reg;
1393 spin_lock_irq(&chip->reg_lock);
1394 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1395 spin_unlock_irq(&chip->reg_lock);
1396 if (!(reg & 0x100))
1397 value->value.enumerated.item[0] = 0;
1398 else
1399 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1400 return 0;
1403 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1405 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1406 u16 reg, old_reg;
1408 spin_lock_irq(&chip->reg_lock);
1409 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1410 if (value->value.enumerated.item[0] == 0)
1411 reg = old_reg & ~0x100;
1412 else
1413 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1414 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1415 spin_unlock_irq(&chip->reg_lock);
1416 return reg != old_reg;
1419 static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
1420 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1422 .name = "Direct Recording Source",
1423 .info = snd_ymfpci_drec_source_info,
1424 .get = snd_ymfpci_drec_source_get,
1425 .put = snd_ymfpci_drec_source_put
1429 * Mixer controls
1432 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1433 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1434 .info = snd_ymfpci_info_single, \
1435 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1436 .private_value = ((reg) | ((shift) << 16)) }
1438 #define snd_ymfpci_info_single snd_ctl_boolean_mono_info
1440 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1443 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1444 int reg = kcontrol->private_value & 0xffff;
1445 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1446 unsigned int mask = 1;
1448 switch (reg) {
1449 case YDSXGR_SPDIFOUTCTRL: break;
1450 case YDSXGR_SPDIFINCTRL: break;
1451 default: return -EINVAL;
1453 ucontrol->value.integer.value[0] =
1454 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1455 return 0;
1458 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
1461 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1462 int reg = kcontrol->private_value & 0xffff;
1463 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1464 unsigned int mask = 1;
1465 int change;
1466 unsigned int val, oval;
1468 switch (reg) {
1469 case YDSXGR_SPDIFOUTCTRL: break;
1470 case YDSXGR_SPDIFINCTRL: break;
1471 default: return -EINVAL;
1473 val = (ucontrol->value.integer.value[0] & mask);
1474 val <<= shift;
1475 spin_lock_irq(&chip->reg_lock);
1476 oval = snd_ymfpci_readl(chip, reg);
1477 val = (oval & ~(mask << shift)) | val;
1478 change = val != oval;
1479 snd_ymfpci_writel(chip, reg, val);
1480 spin_unlock_irq(&chip->reg_lock);
1481 return change;
1484 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1486 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1487 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1488 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1489 .info = snd_ymfpci_info_double, \
1490 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1491 .private_value = reg, \
1492 .tlv = { .p = db_scale_native } }
1494 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1496 unsigned int reg = kcontrol->private_value;
1498 if (reg < 0x80 || reg >= 0xc0)
1499 return -EINVAL;
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1501 uinfo->count = 2;
1502 uinfo->value.integer.min = 0;
1503 uinfo->value.integer.max = 16383;
1504 return 0;
1507 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1509 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1510 unsigned int reg = kcontrol->private_value;
1511 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1512 unsigned int val;
1514 if (reg < 0x80 || reg >= 0xc0)
1515 return -EINVAL;
1516 spin_lock_irq(&chip->reg_lock);
1517 val = snd_ymfpci_readl(chip, reg);
1518 spin_unlock_irq(&chip->reg_lock);
1519 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1520 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1521 return 0;
1524 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1526 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1527 unsigned int reg = kcontrol->private_value;
1528 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1529 int change;
1530 unsigned int val1, val2, oval;
1532 if (reg < 0x80 || reg >= 0xc0)
1533 return -EINVAL;
1534 val1 = ucontrol->value.integer.value[0] & mask;
1535 val2 = ucontrol->value.integer.value[1] & mask;
1536 val1 <<= shift_left;
1537 val2 <<= shift_right;
1538 spin_lock_irq(&chip->reg_lock);
1539 oval = snd_ymfpci_readl(chip, reg);
1540 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1541 change = val1 != oval;
1542 snd_ymfpci_writel(chip, reg, val1);
1543 spin_unlock_irq(&chip->reg_lock);
1544 return change;
1547 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_value *ucontrol)
1550 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1551 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1552 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1553 int change;
1554 unsigned int value, oval;
1556 value = ucontrol->value.integer.value[0] & 0x3fff;
1557 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1558 spin_lock_irq(&chip->reg_lock);
1559 oval = snd_ymfpci_readl(chip, reg);
1560 change = value != oval;
1561 snd_ymfpci_writel(chip, reg, value);
1562 snd_ymfpci_writel(chip, reg2, value);
1563 spin_unlock_irq(&chip->reg_lock);
1564 return change;
1568 * 4ch duplication
1570 #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
1572 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1574 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1575 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1576 return 0;
1579 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1581 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1582 int change;
1583 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1584 if (change)
1585 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1586 return change;
1590 static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593 .name = "Wave Playback Volume",
1594 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1595 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1596 .info = snd_ymfpci_info_double,
1597 .get = snd_ymfpci_get_double,
1598 .put = snd_ymfpci_put_nativedacvol,
1599 .private_value = YDSXGR_NATIVEDACOUTVOL,
1600 .tlv = { .p = db_scale_native },
1602 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1603 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1604 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1605 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1606 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1607 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1608 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1609 YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1610 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1611 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1612 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1613 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1614 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1615 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1616 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1619 .name = "4ch Duplication",
1620 .info = snd_ymfpci_info_dup4ch,
1621 .get = snd_ymfpci_get_dup4ch,
1622 .put = snd_ymfpci_put_dup4ch,
1628 * GPIO
1631 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1633 u16 reg, mode;
1634 unsigned long flags;
1636 spin_lock_irqsave(&chip->reg_lock, flags);
1637 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1638 reg &= ~(1 << (pin + 8));
1639 reg |= (1 << pin);
1640 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1641 /* set the level mode for input line */
1642 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1643 mode &= ~(3 << (pin * 2));
1644 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1645 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1646 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1647 spin_unlock_irqrestore(&chip->reg_lock, flags);
1648 return (mode >> pin) & 1;
1651 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1653 u16 reg;
1654 unsigned long flags;
1656 spin_lock_irqsave(&chip->reg_lock, flags);
1657 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1658 reg &= ~(1 << pin);
1659 reg &= ~(1 << (pin + 8));
1660 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1661 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1662 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1663 spin_unlock_irqrestore(&chip->reg_lock, flags);
1665 return 0;
1668 #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
1670 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1672 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1673 int pin = (int)kcontrol->private_value;
1674 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1675 return 0;
1678 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1680 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1681 int pin = (int)kcontrol->private_value;
1683 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1684 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1685 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1686 return 1;
1688 return 0;
1691 static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
1692 .name = "Shared Rear/Line-In Switch",
1693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1694 .info = snd_ymfpci_gpio_sw_info,
1695 .get = snd_ymfpci_gpio_sw_get,
1696 .put = snd_ymfpci_gpio_sw_put,
1697 .private_value = 2,
1701 * PCM voice volume
1704 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_info *uinfo)
1707 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1708 uinfo->count = 2;
1709 uinfo->value.integer.min = 0;
1710 uinfo->value.integer.max = 0x8000;
1711 return 0;
1714 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
1717 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1718 unsigned int subs = kcontrol->id.subdevice;
1720 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1721 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1722 return 0;
1725 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol)
1728 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1729 unsigned int subs = kcontrol->id.subdevice;
1730 struct snd_pcm_substream *substream;
1731 unsigned long flags;
1733 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1734 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1735 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1736 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1737 if (chip->pcm_mixer[subs].left > 0x8000)
1738 chip->pcm_mixer[subs].left = 0x8000;
1739 if (chip->pcm_mixer[subs].right > 0x8000)
1740 chip->pcm_mixer[subs].right = 0x8000;
1742 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1743 spin_lock_irqsave(&chip->voice_lock, flags);
1744 if (substream->runtime && substream->runtime->private_data) {
1745 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1746 if (!ypcm->use_441_slot)
1747 ypcm->update_pcm_vol = 2;
1749 spin_unlock_irqrestore(&chip->voice_lock, flags);
1750 return 1;
1752 return 0;
1755 static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1756 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1757 .name = "PCM Playback Volume",
1758 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1759 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1760 .info = snd_ymfpci_pcm_vol_info,
1761 .get = snd_ymfpci_pcm_vol_get,
1762 .put = snd_ymfpci_pcm_vol_put,
1767 * Mixer routines
1770 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1772 struct snd_ymfpci *chip = bus->private_data;
1773 chip->ac97_bus = NULL;
1776 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1778 struct snd_ymfpci *chip = ac97->private_data;
1779 chip->ac97 = NULL;
1782 int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1784 struct snd_ac97_template ac97;
1785 struct snd_kcontrol *kctl;
1786 struct snd_pcm_substream *substream;
1787 unsigned int idx;
1788 int err;
1789 static struct snd_ac97_bus_ops ops = {
1790 .write = snd_ymfpci_codec_write,
1791 .read = snd_ymfpci_codec_read,
1794 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1795 return err;
1796 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1797 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1799 memset(&ac97, 0, sizeof(ac97));
1800 ac97.private_data = chip;
1801 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1802 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1803 return err;
1805 /* to be sure */
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 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1811 return err;
1814 /* add S/PDIF control */
1815 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1816 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1817 return err;
1818 kctl->id.device = chip->pcm_spdif->device;
1819 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1820 return err;
1821 kctl->id.device = chip->pcm_spdif->device;
1822 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1823 return err;
1824 kctl->id.device = chip->pcm_spdif->device;
1825 chip->spdif_pcm_ctl = kctl;
1827 /* direct recording source */
1828 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1829 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1830 return err;
1833 * shared rear/line-in
1835 if (rear_switch) {
1836 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1837 return err;
1840 /* per-voice volume */
1841 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1842 for (idx = 0; idx < 32; ++idx) {
1843 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1844 if (!kctl)
1845 return -ENOMEM;
1846 kctl->id.device = chip->pcm->device;
1847 kctl->id.subdevice = idx;
1848 kctl->private_value = (unsigned long)substream;
1849 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1850 return err;
1851 chip->pcm_mixer[idx].left = 0x8000;
1852 chip->pcm_mixer[idx].right = 0x8000;
1853 chip->pcm_mixer[idx].ctl = kctl;
1854 substream = substream->next;
1857 return 0;
1862 * timer
1865 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1867 struct snd_ymfpci *chip;
1868 unsigned long flags;
1869 unsigned int count;
1871 chip = snd_timer_chip(timer);
1872 count = (timer->sticks << 1) - 1;
1873 spin_lock_irqsave(&chip->reg_lock, flags);
1874 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1875 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1876 spin_unlock_irqrestore(&chip->reg_lock, flags);
1877 return 0;
1880 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1882 struct snd_ymfpci *chip;
1883 unsigned long flags;
1885 chip = snd_timer_chip(timer);
1886 spin_lock_irqsave(&chip->reg_lock, flags);
1887 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1888 spin_unlock_irqrestore(&chip->reg_lock, flags);
1889 return 0;
1892 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1893 unsigned long *num, unsigned long *den)
1895 *num = 1;
1896 *den = 48000;
1897 return 0;
1900 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1901 .flags = SNDRV_TIMER_HW_AUTO,
1902 .resolution = 20833, /* 1/fs = 20.8333...us */
1903 .ticks = 0x8000,
1904 .start = snd_ymfpci_timer_start,
1905 .stop = snd_ymfpci_timer_stop,
1906 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1909 int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1911 struct snd_timer *timer = NULL;
1912 struct snd_timer_id tid;
1913 int err;
1915 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1916 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1917 tid.card = chip->card->number;
1918 tid.device = device;
1919 tid.subdevice = 0;
1920 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1921 strcpy(timer->name, "YMFPCI timer");
1922 timer->private_data = chip;
1923 timer->hw = snd_ymfpci_timer_hw;
1925 chip->timer = timer;
1926 return err;
1931 * proc interface
1934 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1935 struct snd_info_buffer *buffer)
1937 struct snd_ymfpci *chip = entry->private_data;
1938 int i;
1940 snd_iprintf(buffer, "YMFPCI\n\n");
1941 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1942 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1945 static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1947 struct snd_info_entry *entry;
1949 if (! snd_card_proc_new(card, "ymfpci", &entry))
1950 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
1951 return 0;
1955 * initialization routines
1958 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1960 u8 cmd;
1962 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1963 #if 0 // force to reset
1964 if (cmd & 0x03) {
1965 #endif
1966 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1967 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1968 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1969 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1970 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1971 #if 0
1973 #endif
1976 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1978 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1981 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1983 u32 val;
1984 int timeout = 1000;
1986 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1987 if (val)
1988 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1989 while (timeout-- > 0) {
1990 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1991 if ((val & 0x00000002) == 0)
1992 break;
1996 #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
1998 #include "ymfpci_image.h"
2000 static struct firmware snd_ymfpci_dsp_microcode = {
2001 .size = YDSXG_DSPLENGTH,
2002 .data = (u8 *)DspInst,
2004 static struct firmware snd_ymfpci_controller_microcode = {
2005 .size = YDSXG_CTRLLENGTH,
2006 .data = (u8 *)CntrlInst,
2008 static struct firmware snd_ymfpci_controller_1e_microcode = {
2009 .size = YDSXG_CTRLLENGTH,
2010 .data = (u8 *)CntrlInst1E,
2012 #endif
2014 #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2015 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2017 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2018 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2019 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2020 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2021 chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
2022 chip->controller_microcode =
2023 &snd_ymfpci_controller_1e_microcode;
2024 else
2025 chip->controller_microcode =
2026 &snd_ymfpci_controller_microcode;
2027 return 0;
2030 #else /* use fw_loader */
2032 #ifdef __LITTLE_ENDIAN
2033 static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
2034 #else
2035 static void snd_ymfpci_convert_from_le(const struct firmware *fw)
2037 int i;
2038 u32 *data = (u32 *)fw->data;
2040 for (i = 0; i < fw->size / 4; ++i)
2041 le32_to_cpus(&data[i]);
2043 #endif
2045 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2047 int err, is_1e;
2048 const char *name;
2050 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2051 &chip->pci->dev);
2052 if (err >= 0) {
2053 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2054 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2055 else {
2056 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2057 err = -EINVAL;
2060 if (err < 0)
2061 return err;
2062 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2063 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2064 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2065 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2066 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2067 err = request_firmware(&chip->controller_microcode, name,
2068 &chip->pci->dev);
2069 if (err >= 0) {
2070 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2071 snd_ymfpci_convert_from_le(chip->controller_microcode);
2072 else {
2073 snd_printk(KERN_ERR "controller microcode"
2074 " has wrong size\n");
2075 err = -EINVAL;
2078 if (err < 0)
2079 return err;
2080 return 0;
2083 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2084 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2085 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2087 #endif
2089 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2091 int i;
2092 u16 ctrl;
2093 u32 *inst;
2095 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2096 snd_ymfpci_disable_dsp(chip);
2097 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2098 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2099 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2100 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2101 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2102 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2103 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2104 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2105 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2107 /* setup DSP instruction code */
2108 inst = (u32 *)chip->dsp_microcode->data;
2109 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2110 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
2112 /* setup control instruction code */
2113 inst = (u32 *)chip->controller_microcode->data;
2114 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2115 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2117 snd_ymfpci_enable_dsp(chip);
2120 static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2122 long size, playback_ctrl_size;
2123 int voice, bank, reg;
2124 u8 *ptr;
2125 dma_addr_t ptr_addr;
2127 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2128 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2129 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2130 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2131 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2133 size = ALIGN(playback_ctrl_size, 0x100) +
2134 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2135 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2136 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2137 chip->work_size;
2138 /* work_ptr must be aligned to 256 bytes, but it's already
2139 covered with the kernel page allocation mechanism */
2140 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2141 size, &chip->work_ptr) < 0)
2142 return -ENOMEM;
2143 ptr = chip->work_ptr.area;
2144 ptr_addr = chip->work_ptr.addr;
2145 memset(ptr, 0, size); /* for sure */
2147 chip->bank_base_playback = ptr;
2148 chip->bank_base_playback_addr = ptr_addr;
2149 chip->ctrl_playback = (u32 *)ptr;
2150 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2151 ptr += ALIGN(playback_ctrl_size, 0x100);
2152 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2153 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2154 chip->voices[voice].number = voice;
2155 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2156 chip->voices[voice].bank_addr = ptr_addr;
2157 for (bank = 0; bank < 2; bank++) {
2158 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2159 ptr += chip->bank_size_playback;
2160 ptr_addr += chip->bank_size_playback;
2163 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2164 ptr_addr = ALIGN(ptr_addr, 0x100);
2165 chip->bank_base_capture = ptr;
2166 chip->bank_base_capture_addr = ptr_addr;
2167 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2168 for (bank = 0; bank < 2; bank++) {
2169 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2170 ptr += chip->bank_size_capture;
2171 ptr_addr += chip->bank_size_capture;
2173 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2174 ptr_addr = ALIGN(ptr_addr, 0x100);
2175 chip->bank_base_effect = ptr;
2176 chip->bank_base_effect_addr = ptr_addr;
2177 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2178 for (bank = 0; bank < 2; bank++) {
2179 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2180 ptr += chip->bank_size_effect;
2181 ptr_addr += chip->bank_size_effect;
2183 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2184 ptr_addr = ALIGN(ptr_addr, 0x100);
2185 chip->work_base = ptr;
2186 chip->work_base_addr = ptr_addr;
2188 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2190 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2191 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2192 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2193 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2194 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2196 /* S/PDIF output initialization */
2197 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2198 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2199 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2201 /* S/PDIF input initialization */
2202 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2204 /* digital mixer setup */
2205 for (reg = 0x80; reg < 0xc0; reg += 4)
2206 snd_ymfpci_writel(chip, reg, 0);
2207 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2208 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2209 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2210 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2211 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2212 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2213 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2215 return 0;
2218 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2220 u16 ctrl;
2222 snd_assert(chip != NULL, return -EINVAL);
2224 if (chip->res_reg_area) { /* don't touch busy hardware */
2225 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2226 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2227 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2228 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2229 snd_ymfpci_disable_dsp(chip);
2230 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2231 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2232 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2233 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2234 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2235 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2236 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2239 snd_ymfpci_ac3_done(chip);
2241 /* Set PCI device to D3 state */
2242 #if 0
2243 /* FIXME: temporarily disabled, otherwise we cannot fire up
2244 * the chip again unless reboot. ACPI bug?
2246 pci_set_power_state(chip->pci, 3);
2247 #endif
2249 #ifdef CONFIG_PM
2250 vfree(chip->saved_regs);
2251 #endif
2252 release_and_free_resource(chip->mpu_res);
2253 release_and_free_resource(chip->fm_res);
2254 snd_ymfpci_free_gameport(chip);
2255 if (chip->reg_area_virt)
2256 iounmap(chip->reg_area_virt);
2257 if (chip->work_ptr.area)
2258 snd_dma_free_pages(&chip->work_ptr);
2260 if (chip->irq >= 0)
2261 free_irq(chip->irq, chip);
2262 release_and_free_resource(chip->res_reg_area);
2264 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2266 pci_disable_device(chip->pci);
2267 #ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2268 release_firmware(chip->dsp_microcode);
2269 release_firmware(chip->controller_microcode);
2270 #endif
2271 kfree(chip);
2272 return 0;
2275 static int snd_ymfpci_dev_free(struct snd_device *device)
2277 struct snd_ymfpci *chip = device->device_data;
2278 return snd_ymfpci_free(chip);
2281 #ifdef CONFIG_PM
2282 static int saved_regs_index[] = {
2283 /* spdif */
2284 YDSXGR_SPDIFOUTCTRL,
2285 YDSXGR_SPDIFOUTSTATUS,
2286 YDSXGR_SPDIFINCTRL,
2287 /* volumes */
2288 YDSXGR_PRIADCLOOPVOL,
2289 YDSXGR_NATIVEDACINVOL,
2290 YDSXGR_NATIVEDACOUTVOL,
2291 YDSXGR_BUF441OUTVOL,
2292 YDSXGR_NATIVEADCINVOL,
2293 YDSXGR_SPDIFLOOPVOL,
2294 YDSXGR_SPDIFOUTVOL,
2295 YDSXGR_ZVOUTVOL,
2296 YDSXGR_LEGACYOUTVOL,
2297 /* address bases */
2298 YDSXGR_PLAYCTRLBASE,
2299 YDSXGR_RECCTRLBASE,
2300 YDSXGR_EFFCTRLBASE,
2301 YDSXGR_WORKBASE,
2302 /* capture set up */
2303 YDSXGR_MAPOFREC,
2304 YDSXGR_RECFORMAT,
2305 YDSXGR_RECSLOTSR,
2306 YDSXGR_ADCFORMAT,
2307 YDSXGR_ADCSLOTSR,
2309 #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2311 int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
2313 struct snd_card *card = pci_get_drvdata(pci);
2314 struct snd_ymfpci *chip = card->private_data;
2315 unsigned int i;
2317 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2318 snd_pcm_suspend_all(chip->pcm);
2319 snd_pcm_suspend_all(chip->pcm2);
2320 snd_pcm_suspend_all(chip->pcm_spdif);
2321 snd_pcm_suspend_all(chip->pcm_4ch);
2322 snd_ac97_suspend(chip->ac97);
2323 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2324 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2325 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2326 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2327 snd_ymfpci_disable_dsp(chip);
2328 pci_disable_device(pci);
2329 pci_save_state(pci);
2330 pci_set_power_state(pci, pci_choose_state(pci, state));
2331 return 0;
2334 int snd_ymfpci_resume(struct pci_dev *pci)
2336 struct snd_card *card = pci_get_drvdata(pci);
2337 struct snd_ymfpci *chip = card->private_data;
2338 unsigned int i;
2340 pci_set_power_state(pci, PCI_D0);
2341 pci_restore_state(pci);
2342 if (pci_enable_device(pci) < 0) {
2343 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2344 "disabling device\n");
2345 snd_card_disconnect(card);
2346 return -EIO;
2348 pci_set_master(pci);
2349 snd_ymfpci_aclink_reset(pci);
2350 snd_ymfpci_codec_ready(chip, 0);
2351 snd_ymfpci_download_image(chip);
2352 udelay(100);
2354 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2355 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2357 snd_ac97_resume(chip->ac97);
2359 /* start hw again */
2360 if (chip->start_count > 0) {
2361 spin_lock_irq(&chip->reg_lock);
2362 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2363 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2364 spin_unlock_irq(&chip->reg_lock);
2366 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2367 return 0;
2369 #endif /* CONFIG_PM */
2371 int __devinit snd_ymfpci_create(struct snd_card *card,
2372 struct pci_dev * pci,
2373 unsigned short old_legacy_ctrl,
2374 struct snd_ymfpci ** rchip)
2376 struct snd_ymfpci *chip;
2377 int err;
2378 static struct snd_device_ops ops = {
2379 .dev_free = snd_ymfpci_dev_free,
2382 *rchip = NULL;
2384 /* enable PCI device */
2385 if ((err = pci_enable_device(pci)) < 0)
2386 return err;
2388 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2389 if (chip == NULL) {
2390 pci_disable_device(pci);
2391 return -ENOMEM;
2393 chip->old_legacy_ctrl = old_legacy_ctrl;
2394 spin_lock_init(&chip->reg_lock);
2395 spin_lock_init(&chip->voice_lock);
2396 init_waitqueue_head(&chip->interrupt_sleep);
2397 atomic_set(&chip->interrupt_sleep_count, 0);
2398 chip->card = card;
2399 chip->pci = pci;
2400 chip->irq = -1;
2401 chip->device_id = pci->device;
2402 chip->rev = pci->revision;
2403 chip->reg_area_phys = pci_resource_start(pci, 0);
2404 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2405 pci_set_master(pci);
2406 chip->src441_used = -1;
2408 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2409 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2410 snd_ymfpci_free(chip);
2411 return -EBUSY;
2413 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2414 "YMFPCI", chip)) {
2415 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2416 snd_ymfpci_free(chip);
2417 return -EBUSY;
2419 chip->irq = pci->irq;
2421 snd_ymfpci_aclink_reset(pci);
2422 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2423 snd_ymfpci_free(chip);
2424 return -EIO;
2427 err = snd_ymfpci_request_firmware(chip);
2428 if (err < 0) {
2429 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2430 snd_ymfpci_free(chip);
2431 return err;
2433 snd_ymfpci_download_image(chip);
2435 udelay(100); /* seems we need a delay after downloading image.. */
2437 if (snd_ymfpci_memalloc(chip) < 0) {
2438 snd_ymfpci_free(chip);
2439 return -EIO;
2442 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2443 snd_ymfpci_free(chip);
2444 return err;
2447 #ifdef CONFIG_PM
2448 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2449 if (chip->saved_regs == NULL) {
2450 snd_ymfpci_free(chip);
2451 return -ENOMEM;
2453 #endif
2455 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2456 snd_ymfpci_free(chip);
2457 return err;
2460 snd_ymfpci_proc_init(card, chip);
2462 snd_card_set_dev(card, &pci->dev);
2464 *rchip = chip;
2465 return 0;