5bde816cd5c4eeccd3a7a2f8441ed7974744e8a8
[pandora-kernel.git] / sound / pci / ymfpci / ymfpci_main.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of YMF724/740/744/754 chips
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include <sound/driver.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/tlv.h>
35 #include <sound/ymfpci.h>
36 #include <sound/asoundef.h>
37 #include <sound/mpu401.h>
38
39 #include <asm/io.h>
40 #include <asm/byteorder.h>
41
42 /*
43  *  common I/O routines
44  */
45
46 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
47
48 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
49 {
50         return readb(chip->reg_area_virt + offset);
51 }
52
53 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
54 {
55         writeb(val, chip->reg_area_virt + offset);
56 }
57
58 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
59 {
60         return readw(chip->reg_area_virt + offset);
61 }
62
63 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
64 {
65         writew(val, chip->reg_area_virt + offset);
66 }
67
68 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
69 {
70         return readl(chip->reg_area_virt + offset);
71 }
72
73 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
74 {
75         writel(val, chip->reg_area_virt + offset);
76 }
77
78 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
79 {
80         unsigned long end_time;
81         u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82         
83         end_time = jiffies + msecs_to_jiffies(750);
84         do {
85                 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
86                         return 0;
87                 set_current_state(TASK_UNINTERRUPTIBLE);
88                 schedule_timeout_uninterruptible(1);
89         } while (time_before(jiffies, end_time));
90         snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
91         return -EBUSY;
92 }
93
94 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
95 {
96         struct snd_ymfpci *chip = ac97->private_data;
97         u32 cmd;
98         
99         snd_ymfpci_codec_ready(chip, 0);
100         cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
101         snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
102 }
103
104 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
105 {
106         struct snd_ymfpci *chip = ac97->private_data;
107
108         if (snd_ymfpci_codec_ready(chip, 0))
109                 return ~0;
110         snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
111         if (snd_ymfpci_codec_ready(chip, 0))
112                 return ~0;
113         if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
114                 int i;
115                 for (i = 0; i < 600; i++)
116                         snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117         }
118         return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
119 }
120
121 /*
122  *  Misc routines
123  */
124
125 static u32 snd_ymfpci_calc_delta(u32 rate)
126 {
127         switch (rate) {
128         case 8000:      return 0x02aaab00;
129         case 11025:     return 0x03accd00;
130         case 16000:     return 0x05555500;
131         case 22050:     return 0x07599a00;
132         case 32000:     return 0x0aaaab00;
133         case 44100:     return 0x0eb33300;
134         default:        return ((rate << 16) / 375) << 5;
135         }
136 }
137
138 static u32 def_rate[8] = {
139         100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
140 };
141
142 static u32 snd_ymfpci_calc_lpfK(u32 rate)
143 {
144         u32 i;
145         static u32 val[8] = {
146                 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
147                 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
148         };
149         
150         if (rate == 44100)
151                 return 0x40000000;      /* FIXME: What's the right value? */
152         for (i = 0; i < 8; i++)
153                 if (rate <= def_rate[i])
154                         return val[i];
155         return val[0];
156 }
157
158 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
159 {
160         u32 i;
161         static u32 val[8] = {
162                 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
163                 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
164         };
165         
166         if (rate == 44100)
167                 return 0x370A0000;
168         for (i = 0; i < 8; i++)
169                 if (rate <= def_rate[i])
170                         return val[i];
171         return val[0];
172 }
173
174 /*
175  *  Hardware start management
176  */
177
178 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
179 {
180         unsigned long flags;
181
182         spin_lock_irqsave(&chip->reg_lock, flags);
183         if (chip->start_count++ > 0)
184                 goto __end;
185         snd_ymfpci_writel(chip, YDSXGR_MODE,
186                           snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
187         chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
188       __end:
189         spin_unlock_irqrestore(&chip->reg_lock, flags);
190 }
191
192 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
193 {
194         unsigned long flags;
195         long timeout = 1000;
196
197         spin_lock_irqsave(&chip->reg_lock, flags);
198         if (--chip->start_count > 0)
199                 goto __end;
200         snd_ymfpci_writel(chip, YDSXGR_MODE,
201                           snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
202         while (timeout-- > 0) {
203                 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
204                         break;
205         }
206         if (atomic_read(&chip->interrupt_sleep_count)) {
207                 atomic_set(&chip->interrupt_sleep_count, 0);
208                 wake_up(&chip->interrupt_sleep);
209         }
210       __end:
211         spin_unlock_irqrestore(&chip->reg_lock, flags);
212 }
213
214 /*
215  *  Playback voice management
216  */
217
218 static int voice_alloc(struct snd_ymfpci *chip,
219                        enum snd_ymfpci_voice_type type, int pair,
220                        struct snd_ymfpci_voice **rvoice)
221 {
222         struct snd_ymfpci_voice *voice, *voice2;
223         int idx;
224         
225         *rvoice = NULL;
226         for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
227                 voice = &chip->voices[idx];
228                 voice2 = pair ? &chip->voices[idx+1] : NULL;
229                 if (voice->use || (voice2 && voice2->use))
230                         continue;
231                 voice->use = 1;
232                 if (voice2)
233                         voice2->use = 1;
234                 switch (type) {
235                 case YMFPCI_PCM:
236                         voice->pcm = 1;
237                         if (voice2)
238                                 voice2->pcm = 1;
239                         break;
240                 case YMFPCI_SYNTH:
241                         voice->synth = 1;
242                         break;
243                 case YMFPCI_MIDI:
244                         voice->midi = 1;
245                         break;
246                 }
247                 snd_ymfpci_hw_start(chip);
248                 if (voice2)
249                         snd_ymfpci_hw_start(chip);
250                 *rvoice = voice;
251                 return 0;
252         }
253         return -ENOMEM;
254 }
255
256 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
257                                   enum snd_ymfpci_voice_type type, int pair,
258                                   struct snd_ymfpci_voice **rvoice)
259 {
260         unsigned long flags;
261         int result;
262         
263         snd_assert(rvoice != NULL, return -EINVAL);
264         snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
265         
266         spin_lock_irqsave(&chip->voice_lock, flags);
267         for (;;) {
268                 result = voice_alloc(chip, type, pair, rvoice);
269                 if (result == 0 || type != YMFPCI_PCM)
270                         break;
271                 /* TODO: synth/midi voice deallocation */
272                 break;
273         }
274         spin_unlock_irqrestore(&chip->voice_lock, flags);       
275         return result;          
276 }
277
278 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
279 {
280         unsigned long flags;
281         
282         snd_assert(pvoice != NULL, return -EINVAL);
283         snd_ymfpci_hw_stop(chip);
284         spin_lock_irqsave(&chip->voice_lock, flags);
285         pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
286         pvoice->ypcm = NULL;
287         pvoice->interrupt = NULL;
288         spin_unlock_irqrestore(&chip->voice_lock, flags);
289         return 0;
290 }
291
292 /*
293  *  PCM part
294  */
295
296 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
297 {
298         struct snd_ymfpci_pcm *ypcm;
299         u32 pos, delta;
300         
301         if ((ypcm = voice->ypcm) == NULL)
302                 return;
303         if (ypcm->substream == NULL)
304                 return;
305         spin_lock(&chip->reg_lock);
306         if (ypcm->running) {
307                 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
308                 if (pos < ypcm->last_pos)
309                         delta = pos + (ypcm->buffer_size - ypcm->last_pos);
310                 else
311                         delta = pos - ypcm->last_pos;
312                 ypcm->period_pos += delta;
313                 ypcm->last_pos = pos;
314                 if (ypcm->period_pos >= ypcm->period_size) {
315                         // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
316                         ypcm->period_pos %= ypcm->period_size;
317                         spin_unlock(&chip->reg_lock);
318                         snd_pcm_period_elapsed(ypcm->substream);
319                         spin_lock(&chip->reg_lock);
320                 }
321
322                 if (unlikely(ypcm->update_pcm_vol)) {
323                         unsigned int subs = ypcm->substream->number;
324                         unsigned int next_bank = 1 - chip->active_bank;
325                         struct snd_ymfpci_playback_bank *bank;
326                         u32 volume;
327                         
328                         bank = &voice->bank[next_bank];
329                         volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
330                         bank->left_gain_end = volume;
331                         if (ypcm->output_rear)
332                                 bank->eff2_gain_end = volume;
333                         if (ypcm->voices[1])
334                                 bank = &ypcm->voices[1]->bank[next_bank];
335                         volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
336                         bank->right_gain_end = volume;
337                         if (ypcm->output_rear)
338                                 bank->eff3_gain_end = volume;
339                         ypcm->update_pcm_vol--;
340                 }
341         }
342         spin_unlock(&chip->reg_lock);
343 }
344
345 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
346 {
347         struct snd_pcm_runtime *runtime = substream->runtime;
348         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
349         struct snd_ymfpci *chip = ypcm->chip;
350         u32 pos, delta;
351         
352         spin_lock(&chip->reg_lock);
353         if (ypcm->running) {
354                 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
355                 if (pos < ypcm->last_pos)
356                         delta = pos + (ypcm->buffer_size - ypcm->last_pos);
357                 else
358                         delta = pos - ypcm->last_pos;
359                 ypcm->period_pos += delta;
360                 ypcm->last_pos = pos;
361                 if (ypcm->period_pos >= ypcm->period_size) {
362                         ypcm->period_pos %= ypcm->period_size;
363                         // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
364                         spin_unlock(&chip->reg_lock);
365                         snd_pcm_period_elapsed(substream);
366                         spin_lock(&chip->reg_lock);
367                 }
368         }
369         spin_unlock(&chip->reg_lock);
370 }
371
372 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
373                                        int cmd)
374 {
375         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
376         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
377         int result = 0;
378
379         spin_lock(&chip->reg_lock);
380         if (ypcm->voices[0] == NULL) {
381                 result = -EINVAL;
382                 goto __unlock;
383         }
384         switch (cmd) {
385         case SNDRV_PCM_TRIGGER_START:
386         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
387         case SNDRV_PCM_TRIGGER_RESUME:
388                 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
389                 if (ypcm->voices[1] != NULL)
390                         chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
391                 ypcm->running = 1;
392                 break;
393         case SNDRV_PCM_TRIGGER_STOP:
394         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
395         case SNDRV_PCM_TRIGGER_SUSPEND:
396                 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
397                 if (ypcm->voices[1] != NULL)
398                         chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
399                 ypcm->running = 0;
400                 break;
401         default:
402                 result = -EINVAL;
403                 break;
404         }
405       __unlock:
406         spin_unlock(&chip->reg_lock);
407         return result;
408 }
409 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
410                                       int cmd)
411 {
412         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
413         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
414         int result = 0;
415         u32 tmp;
416
417         spin_lock(&chip->reg_lock);
418         switch (cmd) {
419         case SNDRV_PCM_TRIGGER_START:
420         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
421         case SNDRV_PCM_TRIGGER_RESUME:
422                 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
423                 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
424                 ypcm->running = 1;
425                 break;
426         case SNDRV_PCM_TRIGGER_STOP:
427         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
428         case SNDRV_PCM_TRIGGER_SUSPEND:
429                 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
430                 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
431                 ypcm->running = 0;
432                 break;
433         default:
434                 result = -EINVAL;
435                 break;
436         }
437         spin_unlock(&chip->reg_lock);
438         return result;
439 }
440
441 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
442 {
443         int err;
444
445         if (ypcm->voices[1] != NULL && voices < 2) {
446                 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
447                 ypcm->voices[1] = NULL;
448         }
449         if (voices == 1 && ypcm->voices[0] != NULL)
450                 return 0;               /* already allocated */
451         if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
452                 return 0;               /* already allocated */
453         if (voices > 1) {
454                 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
455                         snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
456                         ypcm->voices[0] = NULL;
457                 }               
458         }
459         err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
460         if (err < 0)
461                 return err;
462         ypcm->voices[0]->ypcm = ypcm;
463         ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
464         if (voices > 1) {
465                 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
466                 ypcm->voices[1]->ypcm = ypcm;
467         }
468         return 0;
469 }
470
471 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
472                                       struct snd_pcm_runtime *runtime,
473                                       int has_pcm_volume)
474 {
475         struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
476         u32 format;
477         u32 delta = snd_ymfpci_calc_delta(runtime->rate);
478         u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
479         u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
480         struct snd_ymfpci_playback_bank *bank;
481         unsigned int nbank;
482         u32 vol_left, vol_right;
483         u8 use_left, use_right;
484
485         snd_assert(voice != NULL, return);
486         if (runtime->channels == 1) {
487                 use_left = 1;
488                 use_right = 1;
489         } else {
490                 use_left = (voiceidx & 1) == 0;
491                 use_right = !use_left;
492         }
493         if (has_pcm_volume) {
494                 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
495                                        [ypcm->substream->number].left << 15);
496                 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
497                                         [ypcm->substream->number].right << 15);
498         } else {
499                 vol_left = cpu_to_le32(0x40000000);
500                 vol_right = cpu_to_le32(0x40000000);
501         }
502         format = runtime->channels == 2 ? 0x00010000 : 0;
503         if (snd_pcm_format_width(runtime->format) == 8)
504                 format |= 0x80000000;
505         if (runtime->channels == 2 && (voiceidx & 1) != 0)
506                 format |= 1;
507         for (nbank = 0; nbank < 2; nbank++) {
508                 bank = &voice->bank[nbank];
509                 memset(bank, 0, sizeof(*bank));
510                 bank->format = cpu_to_le32(format);
511                 bank->base = cpu_to_le32(runtime->dma_addr);
512                 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
513                 bank->lpfQ = cpu_to_le32(lpfQ);
514                 bank->delta =
515                 bank->delta_end = cpu_to_le32(delta);
516                 bank->lpfK =
517                 bank->lpfK_end = cpu_to_le32(lpfK);
518                 bank->eg_gain =
519                 bank->eg_gain_end = cpu_to_le32(0x40000000);
520
521                 if (ypcm->output_front) {
522                         if (use_left) {
523                                 bank->left_gain =
524                                 bank->left_gain_end = vol_left;
525                         }
526                         if (use_right) {
527                                 bank->right_gain =
528                                 bank->right_gain_end = vol_right;
529                         }
530                 }
531                 if (ypcm->output_rear) {
532                         if (!ypcm->swap_rear) {
533                                 if (use_left) {
534                                         bank->eff2_gain =
535                                         bank->eff2_gain_end = vol_left;
536                                 }
537                                 if (use_right) {
538                                         bank->eff3_gain =
539                                         bank->eff3_gain_end = vol_right;
540                                 }
541                         } else {
542                                 /* The SPDIF out channels seem to be swapped, so we have
543                                  * to swap them here, too.  The rear analog out channels
544                                  * will be wrong, but otherwise AC3 would not work.
545                                  */
546                                 if (use_left) {
547                                         bank->eff3_gain =
548                                         bank->eff3_gain_end = vol_left;
549                                 }
550                                 if (use_right) {
551                                         bank->eff2_gain =
552                                         bank->eff2_gain_end = vol_right;
553                                 }
554                         }
555                 }
556         }
557 }
558
559 static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
560 {
561         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
562                                 4096, &chip->ac3_tmp_base) < 0)
563                 return -ENOMEM;
564
565         chip->bank_effect[3][0]->base =
566         chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
567         chip->bank_effect[3][0]->loop_end =
568         chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
569         chip->bank_effect[4][0]->base =
570         chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
571         chip->bank_effect[4][0]->loop_end =
572         chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
573
574         spin_lock_irq(&chip->reg_lock);
575         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
576                           snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
577         spin_unlock_irq(&chip->reg_lock);
578         return 0;
579 }
580
581 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
582 {
583         spin_lock_irq(&chip->reg_lock);
584         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
585                           snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
586         spin_unlock_irq(&chip->reg_lock);
587         // snd_ymfpci_irq_wait(chip);
588         if (chip->ac3_tmp_base.area) {
589                 snd_dma_free_pages(&chip->ac3_tmp_base);
590                 chip->ac3_tmp_base.area = NULL;
591         }
592         return 0;
593 }
594
595 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
596                                          struct snd_pcm_hw_params *hw_params)
597 {
598         struct snd_pcm_runtime *runtime = substream->runtime;
599         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
600         int err;
601
602         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
603                 return err;
604         if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
605                 return err;
606         return 0;
607 }
608
609 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
610 {
611         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
612         struct snd_pcm_runtime *runtime = substream->runtime;
613         struct snd_ymfpci_pcm *ypcm;
614         
615         if (runtime->private_data == NULL)
616                 return 0;
617         ypcm = runtime->private_data;
618
619         /* wait, until the PCI operations are not finished */
620         snd_ymfpci_irq_wait(chip);
621         snd_pcm_lib_free_pages(substream);
622         if (ypcm->voices[1]) {
623                 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
624                 ypcm->voices[1] = NULL;
625         }
626         if (ypcm->voices[0]) {
627                 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
628                 ypcm->voices[0] = NULL;
629         }
630         return 0;
631 }
632
633 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
634 {
635         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
636         struct snd_pcm_runtime *runtime = substream->runtime;
637         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
638         unsigned int nvoice;
639
640         ypcm->period_size = runtime->period_size;
641         ypcm->buffer_size = runtime->buffer_size;
642         ypcm->period_pos = 0;
643         ypcm->last_pos = 0;
644         for (nvoice = 0; nvoice < runtime->channels; nvoice++)
645                 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
646                                           substream->pcm == chip->pcm);
647         return 0;
648 }
649
650 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
651                                         struct snd_pcm_hw_params *hw_params)
652 {
653         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
654 }
655
656 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
657 {
658         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
659
660         /* wait, until the PCI operations are not finished */
661         snd_ymfpci_irq_wait(chip);
662         return snd_pcm_lib_free_pages(substream);
663 }
664
665 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
666 {
667         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
668         struct snd_pcm_runtime *runtime = substream->runtime;
669         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
670         struct snd_ymfpci_capture_bank * bank;
671         int nbank;
672         u32 rate, format;
673
674         ypcm->period_size = runtime->period_size;
675         ypcm->buffer_size = runtime->buffer_size;
676         ypcm->period_pos = 0;
677         ypcm->last_pos = 0;
678         ypcm->shift = 0;
679         rate = ((48000 * 4096) / runtime->rate) - 1;
680         format = 0;
681         if (runtime->channels == 2) {
682                 format |= 2;
683                 ypcm->shift++;
684         }
685         if (snd_pcm_format_width(runtime->format) == 8)
686                 format |= 1;
687         else
688                 ypcm->shift++;
689         switch (ypcm->capture_bank_number) {
690         case 0:
691                 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
692                 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
693                 break;
694         case 1:
695                 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
696                 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
697                 break;
698         }
699         for (nbank = 0; nbank < 2; nbank++) {
700                 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
701                 bank->base = cpu_to_le32(runtime->dma_addr);
702                 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
703                 bank->start = 0;
704                 bank->num_of_loops = 0;
705         }
706         return 0;
707 }
708
709 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
710 {
711         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
712         struct snd_pcm_runtime *runtime = substream->runtime;
713         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
714         struct snd_ymfpci_voice *voice = ypcm->voices[0];
715
716         if (!(ypcm->running && voice))
717                 return 0;
718         return le32_to_cpu(voice->bank[chip->active_bank].start);
719 }
720
721 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
722 {
723         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
724         struct snd_pcm_runtime *runtime = substream->runtime;
725         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
726
727         if (!ypcm->running)
728                 return 0;
729         return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
730 }
731
732 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
733 {
734         wait_queue_t wait;
735         int loops = 4;
736
737         while (loops-- > 0) {
738                 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
739                         continue;
740                 init_waitqueue_entry(&wait, current);
741                 add_wait_queue(&chip->interrupt_sleep, &wait);
742                 atomic_inc(&chip->interrupt_sleep_count);
743                 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
744                 remove_wait_queue(&chip->interrupt_sleep, &wait);
745         }
746 }
747
748 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
749 {
750         struct snd_ymfpci *chip = dev_id;
751         u32 status, nvoice, mode;
752         struct snd_ymfpci_voice *voice;
753
754         status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
755         if (status & 0x80000000) {
756                 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
757                 spin_lock(&chip->voice_lock);
758                 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
759                         voice = &chip->voices[nvoice];
760                         if (voice->interrupt)
761                                 voice->interrupt(chip, voice);
762                 }
763                 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
764                         if (chip->capture_substream[nvoice])
765                                 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
766                 }
767 #if 0
768                 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
769                         if (chip->effect_substream[nvoice])
770                                 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
771                 }
772 #endif
773                 spin_unlock(&chip->voice_lock);
774                 spin_lock(&chip->reg_lock);
775                 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
776                 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
777                 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
778                 spin_unlock(&chip->reg_lock);
779
780                 if (atomic_read(&chip->interrupt_sleep_count)) {
781                         atomic_set(&chip->interrupt_sleep_count, 0);
782                         wake_up(&chip->interrupt_sleep);
783                 }
784         }
785
786         status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
787         if (status & 1) {
788                 if (chip->timer)
789                         snd_timer_interrupt(chip->timer, chip->timer->sticks);
790         }
791         snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
792
793         if (chip->rawmidi)
794                 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
795         return IRQ_HANDLED;
796 }
797
798 static struct snd_pcm_hardware snd_ymfpci_playback =
799 {
800         .info =                 (SNDRV_PCM_INFO_MMAP |
801                                  SNDRV_PCM_INFO_MMAP_VALID | 
802                                  SNDRV_PCM_INFO_INTERLEAVED |
803                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
804                                  SNDRV_PCM_INFO_PAUSE |
805                                  SNDRV_PCM_INFO_RESUME),
806         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
807         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
808         .rate_min =             8000,
809         .rate_max =             48000,
810         .channels_min =         1,
811         .channels_max =         2,
812         .buffer_bytes_max =     256 * 1024, /* FIXME: enough? */
813         .period_bytes_min =     64,
814         .period_bytes_max =     256 * 1024, /* FIXME: enough? */
815         .periods_min =          3,
816         .periods_max =          1024,
817         .fifo_size =            0,
818 };
819
820 static struct snd_pcm_hardware snd_ymfpci_capture =
821 {
822         .info =                 (SNDRV_PCM_INFO_MMAP |
823                                  SNDRV_PCM_INFO_MMAP_VALID |
824                                  SNDRV_PCM_INFO_INTERLEAVED |
825                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
826                                  SNDRV_PCM_INFO_PAUSE |
827                                  SNDRV_PCM_INFO_RESUME),
828         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
829         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
830         .rate_min =             8000,
831         .rate_max =             48000,
832         .channels_min =         1,
833         .channels_max =         2,
834         .buffer_bytes_max =     256 * 1024, /* FIXME: enough? */
835         .period_bytes_min =     64,
836         .period_bytes_max =     256 * 1024, /* FIXME: enough? */
837         .periods_min =          3,
838         .periods_max =          1024,
839         .fifo_size =            0,
840 };
841
842 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
843 {
844         kfree(runtime->private_data);
845 }
846
847 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
848 {
849         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
850         struct snd_pcm_runtime *runtime = substream->runtime;
851         struct snd_ymfpci_pcm *ypcm;
852
853         ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
854         if (ypcm == NULL)
855                 return -ENOMEM;
856         ypcm->chip = chip;
857         ypcm->type = PLAYBACK_VOICE;
858         ypcm->substream = substream;
859         runtime->hw = snd_ymfpci_playback;
860         runtime->private_data = ypcm;
861         runtime->private_free = snd_ymfpci_pcm_free_substream;
862         /* FIXME? True value is 256/48 = 5.33333 ms */
863         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
864         return 0;
865 }
866
867 /* call with spinlock held */
868 static void ymfpci_open_extension(struct snd_ymfpci *chip)
869 {
870         if (! chip->rear_opened) {
871                 if (! chip->spdif_opened) /* set AC3 */
872                         snd_ymfpci_writel(chip, YDSXGR_MODE,
873                                           snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
874                 /* enable second codec (4CHEN) */
875                 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
876                                   (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
877         }
878 }
879
880 /* call with spinlock held */
881 static void ymfpci_close_extension(struct snd_ymfpci *chip)
882 {
883         if (! chip->rear_opened) {
884                 if (! chip->spdif_opened)
885                         snd_ymfpci_writel(chip, YDSXGR_MODE,
886                                           snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
887                 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
888                                   (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
889         }
890 }
891
892 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
893 {
894         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
895         struct snd_pcm_runtime *runtime = substream->runtime;
896         struct snd_ymfpci_pcm *ypcm;
897         struct snd_kcontrol *kctl;
898         int err;
899         
900         if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
901                 return err;
902         ypcm = runtime->private_data;
903         ypcm->output_front = 1;
904         ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
905         ypcm->swap_rear = 0;
906         spin_lock_irq(&chip->reg_lock);
907         if (ypcm->output_rear) {
908                 ymfpci_open_extension(chip);
909                 chip->rear_opened++;
910         }
911         spin_unlock_irq(&chip->reg_lock);
912
913         kctl = chip->pcm_mixer[substream->number].ctl;
914         kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
915         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
916         return 0;
917 }
918
919 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
920 {
921         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
922         struct snd_pcm_runtime *runtime = substream->runtime;
923         struct snd_ymfpci_pcm *ypcm;
924         int err;
925         
926         if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
927                 return err;
928         ypcm = runtime->private_data;
929         ypcm->output_front = 0;
930         ypcm->output_rear = 1;
931         ypcm->swap_rear = 1;
932         spin_lock_irq(&chip->reg_lock);
933         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
934                           snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
935         ymfpci_open_extension(chip);
936         chip->spdif_pcm_bits = chip->spdif_bits;
937         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
938         chip->spdif_opened++;
939         spin_unlock_irq(&chip->reg_lock);
940
941         chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
942         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
943                        SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
944         return 0;
945 }
946
947 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
948 {
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;
953         
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 = 0;
960         spin_lock_irq(&chip->reg_lock);
961         ymfpci_open_extension(chip);
962         chip->rear_opened++;
963         spin_unlock_irq(&chip->reg_lock);
964         return 0;
965 }
966
967 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
968                                    u32 capture_bank_number)
969 {
970         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
971         struct snd_pcm_runtime *runtime = substream->runtime;
972         struct snd_ymfpci_pcm *ypcm;
973
974         ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
975         if (ypcm == NULL)
976                 return -ENOMEM;
977         ypcm->chip = chip;
978         ypcm->type = capture_bank_number + CAPTURE_REC;
979         ypcm->substream = substream;    
980         ypcm->capture_bank_number = capture_bank_number;
981         chip->capture_substream[capture_bank_number] = substream;
982         runtime->hw = snd_ymfpci_capture;
983         /* FIXME? True value is 256/48 = 5.33333 ms */
984         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
985         runtime->private_data = ypcm;
986         runtime->private_free = snd_ymfpci_pcm_free_substream;
987         snd_ymfpci_hw_start(chip);
988         return 0;
989 }
990
991 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
992 {
993         return snd_ymfpci_capture_open(substream, 0);
994 }
995
996 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
997 {
998         return snd_ymfpci_capture_open(substream, 1);
999 }
1000
1001 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1002 {
1003         return 0;
1004 }
1005
1006 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1007 {
1008         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1009         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1010         struct snd_kcontrol *kctl;
1011
1012         spin_lock_irq(&chip->reg_lock);
1013         if (ypcm->output_rear && chip->rear_opened > 0) {
1014                 chip->rear_opened--;
1015                 ymfpci_close_extension(chip);
1016         }
1017         spin_unlock_irq(&chip->reg_lock);
1018         kctl = chip->pcm_mixer[substream->number].ctl;
1019         kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1020         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
1021         return snd_ymfpci_playback_close_1(substream);
1022 }
1023
1024 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1025 {
1026         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1027
1028         spin_lock_irq(&chip->reg_lock);
1029         chip->spdif_opened = 0;
1030         ymfpci_close_extension(chip);
1031         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1032                           snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1033         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1034         spin_unlock_irq(&chip->reg_lock);
1035         chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1036         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1037                        SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1038         return snd_ymfpci_playback_close_1(substream);
1039 }
1040
1041 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1042 {
1043         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1044
1045         spin_lock_irq(&chip->reg_lock);
1046         if (chip->rear_opened > 0) {
1047                 chip->rear_opened--;
1048                 ymfpci_close_extension(chip);
1049         }
1050         spin_unlock_irq(&chip->reg_lock);
1051         return snd_ymfpci_playback_close_1(substream);
1052 }
1053
1054 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1055 {
1056         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1057         struct snd_pcm_runtime *runtime = substream->runtime;
1058         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1059
1060         if (ypcm != NULL) {
1061                 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1062                 snd_ymfpci_hw_stop(chip);
1063         }
1064         return 0;
1065 }
1066
1067 static struct snd_pcm_ops snd_ymfpci_playback_ops = {
1068         .open =                 snd_ymfpci_playback_open,
1069         .close =                snd_ymfpci_playback_close,
1070         .ioctl =                snd_pcm_lib_ioctl,
1071         .hw_params =            snd_ymfpci_playback_hw_params,
1072         .hw_free =              snd_ymfpci_playback_hw_free,
1073         .prepare =              snd_ymfpci_playback_prepare,
1074         .trigger =              snd_ymfpci_playback_trigger,
1075         .pointer =              snd_ymfpci_playback_pointer,
1076 };
1077
1078 static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1079         .open =                 snd_ymfpci_capture_rec_open,
1080         .close =                snd_ymfpci_capture_close,
1081         .ioctl =                snd_pcm_lib_ioctl,
1082         .hw_params =            snd_ymfpci_capture_hw_params,
1083         .hw_free =              snd_ymfpci_capture_hw_free,
1084         .prepare =              snd_ymfpci_capture_prepare,
1085         .trigger =              snd_ymfpci_capture_trigger,
1086         .pointer =              snd_ymfpci_capture_pointer,
1087 };
1088
1089 int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1090 {
1091         struct snd_pcm *pcm;
1092         int err;
1093
1094         if (rpcm)
1095                 *rpcm = NULL;
1096         if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1097                 return err;
1098         pcm->private_data = chip;
1099
1100         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1101         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1102
1103         /* global setup */
1104         pcm->info_flags = 0;
1105         strcpy(pcm->name, "YMFPCI");
1106         chip->pcm = pcm;
1107
1108         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1109                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1110
1111         if (rpcm)
1112                 *rpcm = pcm;
1113         return 0;
1114 }
1115
1116 static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1117         .open =                 snd_ymfpci_capture_ac97_open,
1118         .close =                snd_ymfpci_capture_close,
1119         .ioctl =                snd_pcm_lib_ioctl,
1120         .hw_params =            snd_ymfpci_capture_hw_params,
1121         .hw_free =              snd_ymfpci_capture_hw_free,
1122         .prepare =              snd_ymfpci_capture_prepare,
1123         .trigger =              snd_ymfpci_capture_trigger,
1124         .pointer =              snd_ymfpci_capture_pointer,
1125 };
1126
1127 int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1128 {
1129         struct snd_pcm *pcm;
1130         int err;
1131
1132         if (rpcm)
1133                 *rpcm = NULL;
1134         if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1135                 return err;
1136         pcm->private_data = chip;
1137
1138         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1139
1140         /* global setup */
1141         pcm->info_flags = 0;
1142         sprintf(pcm->name, "YMFPCI - %s",
1143                 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1144         chip->pcm2 = pcm;
1145
1146         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1147                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1148
1149         if (rpcm)
1150                 *rpcm = pcm;
1151         return 0;
1152 }
1153
1154 static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1155         .open =                 snd_ymfpci_playback_spdif_open,
1156         .close =                snd_ymfpci_playback_spdif_close,
1157         .ioctl =                snd_pcm_lib_ioctl,
1158         .hw_params =            snd_ymfpci_playback_hw_params,
1159         .hw_free =              snd_ymfpci_playback_hw_free,
1160         .prepare =              snd_ymfpci_playback_prepare,
1161         .trigger =              snd_ymfpci_playback_trigger,
1162         .pointer =              snd_ymfpci_playback_pointer,
1163 };
1164
1165 int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1166 {
1167         struct snd_pcm *pcm;
1168         int err;
1169
1170         if (rpcm)
1171                 *rpcm = NULL;
1172         if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1173                 return err;
1174         pcm->private_data = chip;
1175
1176         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1177
1178         /* global setup */
1179         pcm->info_flags = 0;
1180         strcpy(pcm->name, "YMFPCI - IEC958");
1181         chip->pcm_spdif = pcm;
1182
1183         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1184                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1185
1186         if (rpcm)
1187                 *rpcm = pcm;
1188         return 0;
1189 }
1190
1191 static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1192         .open =                 snd_ymfpci_playback_4ch_open,
1193         .close =                snd_ymfpci_playback_4ch_close,
1194         .ioctl =                snd_pcm_lib_ioctl,
1195         .hw_params =            snd_ymfpci_playback_hw_params,
1196         .hw_free =              snd_ymfpci_playback_hw_free,
1197         .prepare =              snd_ymfpci_playback_prepare,
1198         .trigger =              snd_ymfpci_playback_trigger,
1199         .pointer =              snd_ymfpci_playback_pointer,
1200 };
1201
1202 int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1203 {
1204         struct snd_pcm *pcm;
1205         int err;
1206
1207         if (rpcm)
1208                 *rpcm = NULL;
1209         if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1210                 return err;
1211         pcm->private_data = chip;
1212
1213         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1214
1215         /* global setup */
1216         pcm->info_flags = 0;
1217         strcpy(pcm->name, "YMFPCI - Rear PCM");
1218         chip->pcm_4ch = pcm;
1219
1220         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1221                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1222
1223         if (rpcm)
1224                 *rpcm = pcm;
1225         return 0;
1226 }
1227
1228 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1229 {
1230         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1231         uinfo->count = 1;
1232         return 0;
1233 }
1234
1235 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1236                                         struct snd_ctl_elem_value *ucontrol)
1237 {
1238         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1239
1240         spin_lock_irq(&chip->reg_lock);
1241         ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1242         ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1243         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1244         spin_unlock_irq(&chip->reg_lock);
1245         return 0;
1246 }
1247
1248 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1249                                          struct snd_ctl_elem_value *ucontrol)
1250 {
1251         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1252         unsigned int val;
1253         int change;
1254
1255         val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1256               (ucontrol->value.iec958.status[1] << 8);
1257         spin_lock_irq(&chip->reg_lock);
1258         change = chip->spdif_bits != val;
1259         chip->spdif_bits = val;
1260         if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1261                 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1262         spin_unlock_irq(&chip->reg_lock);
1263         return change;
1264 }
1265
1266 static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
1267 {
1268         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1269         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1270         .info =         snd_ymfpci_spdif_default_info,
1271         .get =          snd_ymfpci_spdif_default_get,
1272         .put =          snd_ymfpci_spdif_default_put
1273 };
1274
1275 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1276 {
1277         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1278         uinfo->count = 1;
1279         return 0;
1280 }
1281
1282 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1283                                       struct snd_ctl_elem_value *ucontrol)
1284 {
1285         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1286
1287         spin_lock_irq(&chip->reg_lock);
1288         ucontrol->value.iec958.status[0] = 0x3e;
1289         ucontrol->value.iec958.status[1] = 0xff;
1290         spin_unlock_irq(&chip->reg_lock);
1291         return 0;
1292 }
1293
1294 static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
1295 {
1296         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1297         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1298         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1299         .info =         snd_ymfpci_spdif_mask_info,
1300         .get =          snd_ymfpci_spdif_mask_get,
1301 };
1302
1303 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1304 {
1305         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1306         uinfo->count = 1;
1307         return 0;
1308 }
1309
1310 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1311                                         struct snd_ctl_elem_value *ucontrol)
1312 {
1313         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1314
1315         spin_lock_irq(&chip->reg_lock);
1316         ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1317         ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1318         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1319         spin_unlock_irq(&chip->reg_lock);
1320         return 0;
1321 }
1322
1323 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1324                                         struct snd_ctl_elem_value *ucontrol)
1325 {
1326         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1327         unsigned int val;
1328         int change;
1329
1330         val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1331               (ucontrol->value.iec958.status[1] << 8);
1332         spin_lock_irq(&chip->reg_lock);
1333         change = chip->spdif_pcm_bits != val;
1334         chip->spdif_pcm_bits = val;
1335         if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1336                 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1337         spin_unlock_irq(&chip->reg_lock);
1338         return change;
1339 }
1340
1341 static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
1342 {
1343         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1344         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1345         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1346         .info =         snd_ymfpci_spdif_stream_info,
1347         .get =          snd_ymfpci_spdif_stream_get,
1348         .put =          snd_ymfpci_spdif_stream_put
1349 };
1350
1351 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1352 {
1353         static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1354
1355         info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1356         info->count = 1;
1357         info->value.enumerated.items = 3;
1358         if (info->value.enumerated.item > 2)
1359                 info->value.enumerated.item = 2;
1360         strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1361         return 0;
1362 }
1363
1364 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1365 {
1366         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1367         u16 reg;
1368
1369         spin_lock_irq(&chip->reg_lock);
1370         reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1371         spin_unlock_irq(&chip->reg_lock);
1372         if (!(reg & 0x100))
1373                 value->value.enumerated.item[0] = 0;
1374         else
1375                 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1376         return 0;
1377 }
1378
1379 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1380 {
1381         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1382         u16 reg, old_reg;
1383
1384         spin_lock_irq(&chip->reg_lock);
1385         old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1386         if (value->value.enumerated.item[0] == 0)
1387                 reg = old_reg & ~0x100;
1388         else
1389                 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1390         snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1391         spin_unlock_irq(&chip->reg_lock);
1392         return reg != old_reg;
1393 }
1394
1395 static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
1396         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE,
1397         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1398         .name =         "Direct Recording Source",
1399         .info =         snd_ymfpci_drec_source_info,
1400         .get =          snd_ymfpci_drec_source_get,
1401         .put =          snd_ymfpci_drec_source_put
1402 };
1403
1404 /*
1405  *  Mixer controls
1406  */
1407
1408 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1409 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1410   .info = snd_ymfpci_info_single, \
1411   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1412   .private_value = ((reg) | ((shift) << 16)) }
1413
1414 static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol,
1415                                   struct snd_ctl_elem_info *uinfo)
1416 {
1417         int reg = kcontrol->private_value & 0xffff;
1418
1419         switch (reg) {
1420         case YDSXGR_SPDIFOUTCTRL: break;
1421         case YDSXGR_SPDIFINCTRL: break;
1422         default: return -EINVAL;
1423         }
1424         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1425         uinfo->count = 1;
1426         uinfo->value.integer.min = 0;
1427         uinfo->value.integer.max = 1;
1428         return 0;
1429 }
1430
1431 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1432                                  struct snd_ctl_elem_value *ucontrol)
1433 {
1434         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1435         int reg = kcontrol->private_value & 0xffff;
1436         unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1437         unsigned int mask = 1;
1438         
1439         switch (reg) {
1440         case YDSXGR_SPDIFOUTCTRL: break;
1441         case YDSXGR_SPDIFINCTRL: break;
1442         default: return -EINVAL;
1443         }
1444         ucontrol->value.integer.value[0] =
1445                 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1446         return 0;
1447 }
1448
1449 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1450                                  struct snd_ctl_elem_value *ucontrol)
1451 {
1452         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1453         int reg = kcontrol->private_value & 0xffff;
1454         unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1455         unsigned int mask = 1;
1456         int change;
1457         unsigned int val, oval;
1458         
1459         switch (reg) {
1460         case YDSXGR_SPDIFOUTCTRL: break;
1461         case YDSXGR_SPDIFINCTRL: break;
1462         default: return -EINVAL;
1463         }
1464         val = (ucontrol->value.integer.value[0] & mask);
1465         val <<= shift;
1466         spin_lock_irq(&chip->reg_lock);
1467         oval = snd_ymfpci_readl(chip, reg);
1468         val = (oval & ~(mask << shift)) | val;
1469         change = val != oval;
1470         snd_ymfpci_writel(chip, reg, val);
1471         spin_unlock_irq(&chip->reg_lock);
1472         return change;
1473 }
1474
1475 static DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1476
1477 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1478 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1479   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1480   .info = snd_ymfpci_info_double, \
1481   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1482   .private_value = reg, \
1483   .tlv = { .p = db_scale_native } }
1484
1485 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1486 {
1487         unsigned int reg = kcontrol->private_value;
1488
1489         if (reg < 0x80 || reg >= 0xc0)
1490                 return -EINVAL;
1491         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1492         uinfo->count = 2;
1493         uinfo->value.integer.min = 0;
1494         uinfo->value.integer.max = 16383;
1495         return 0;
1496 }
1497
1498 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1499 {
1500         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1501         unsigned int reg = kcontrol->private_value;
1502         unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1503         unsigned int val;
1504         
1505         if (reg < 0x80 || reg >= 0xc0)
1506                 return -EINVAL;
1507         spin_lock_irq(&chip->reg_lock);
1508         val = snd_ymfpci_readl(chip, reg);
1509         spin_unlock_irq(&chip->reg_lock);
1510         ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1511         ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1512         return 0;
1513 }
1514
1515 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1516 {
1517         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1518         unsigned int reg = kcontrol->private_value;
1519         unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1520         int change;
1521         unsigned int val1, val2, oval;
1522         
1523         if (reg < 0x80 || reg >= 0xc0)
1524                 return -EINVAL;
1525         val1 = ucontrol->value.integer.value[0] & mask;
1526         val2 = ucontrol->value.integer.value[1] & mask;
1527         val1 <<= shift_left;
1528         val2 <<= shift_right;
1529         spin_lock_irq(&chip->reg_lock);
1530         oval = snd_ymfpci_readl(chip, reg);
1531         val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1532         change = val1 != oval;
1533         snd_ymfpci_writel(chip, reg, val1);
1534         spin_unlock_irq(&chip->reg_lock);
1535         return change;
1536 }
1537
1538 /*
1539  * 4ch duplication
1540  */
1541 static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1542 {
1543         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1544         uinfo->count = 1;
1545         uinfo->value.integer.min = 0;
1546         uinfo->value.integer.max = 1;
1547         return 0;
1548 }
1549
1550 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1551 {
1552         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1553         ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1554         return 0;
1555 }
1556
1557 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1558 {
1559         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1560         int change;
1561         change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1562         if (change)
1563                 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1564         return change;
1565 }
1566
1567
1568 static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1569 YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1570 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1571 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1572 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1573 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1574 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1575 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1576 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1577 YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1578 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1579 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1580 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1581 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1582 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1583 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1584 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1585 {
1586         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1587         .name = "4ch Duplication",
1588         .info = snd_ymfpci_info_dup4ch,
1589         .get = snd_ymfpci_get_dup4ch,
1590         .put = snd_ymfpci_put_dup4ch,
1591 },
1592 };
1593
1594
1595 /*
1596  * GPIO
1597  */
1598
1599 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1600 {
1601         u16 reg, mode;
1602         unsigned long flags;
1603
1604         spin_lock_irqsave(&chip->reg_lock, flags);
1605         reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1606         reg &= ~(1 << (pin + 8));
1607         reg |= (1 << pin);
1608         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1609         /* set the level mode for input line */
1610         mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1611         mode &= ~(3 << (pin * 2));
1612         snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1613         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1614         mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1615         spin_unlock_irqrestore(&chip->reg_lock, flags);
1616         return (mode >> pin) & 1;
1617 }
1618
1619 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1620 {
1621         u16 reg;
1622         unsigned long flags;
1623
1624         spin_lock_irqsave(&chip->reg_lock, flags);
1625         reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1626         reg &= ~(1 << pin);
1627         reg &= ~(1 << (pin + 8));
1628         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1629         snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1630         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1631         spin_unlock_irqrestore(&chip->reg_lock, flags);
1632
1633         return 0;
1634 }
1635
1636 static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1637 {
1638         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1639         uinfo->count = 1;
1640         uinfo->value.integer.min = 0;
1641         uinfo->value.integer.max = 1;
1642         return 0;
1643 }
1644
1645 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1646 {
1647         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1648         int pin = (int)kcontrol->private_value;
1649         ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1650         return 0;
1651 }
1652
1653 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1654 {
1655         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1656         int pin = (int)kcontrol->private_value;
1657
1658         if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1659                 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1660                 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1661                 return 1;
1662         }
1663         return 0;
1664 }
1665
1666 static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
1667         .name = "Shared Rear/Line-In Switch",
1668         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669         .info = snd_ymfpci_gpio_sw_info,
1670         .get = snd_ymfpci_gpio_sw_get,
1671         .put = snd_ymfpci_gpio_sw_put,
1672         .private_value = 2,
1673 };
1674
1675 /*
1676  * PCM voice volume
1677  */
1678
1679 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1680                                    struct snd_ctl_elem_info *uinfo)
1681 {
1682         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1683         uinfo->count = 2;
1684         uinfo->value.integer.min = 0;
1685         uinfo->value.integer.max = 0x8000;
1686         return 0;
1687 }
1688
1689 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1690                                   struct snd_ctl_elem_value *ucontrol)
1691 {
1692         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1693         unsigned int subs = kcontrol->id.subdevice;
1694
1695         ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1696         ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1697         return 0;
1698 }
1699
1700 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1701                                   struct snd_ctl_elem_value *ucontrol)
1702 {
1703         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1704         unsigned int subs = kcontrol->id.subdevice;
1705         struct snd_pcm_substream *substream;
1706         unsigned long flags;
1707
1708         if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1709             ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1710                 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1711                 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1712
1713                 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1714                 spin_lock_irqsave(&chip->voice_lock, flags);
1715                 if (substream->runtime && substream->runtime->private_data) {
1716                         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1717                         ypcm->update_pcm_vol = 2;
1718                 }
1719                 spin_unlock_irqrestore(&chip->voice_lock, flags);
1720                 return 1;
1721         }
1722         return 0;
1723 }
1724
1725 static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1726         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1727         .name = "PCM Playback Volume",
1728         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1729                 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1730         .info = snd_ymfpci_pcm_vol_info,
1731         .get = snd_ymfpci_pcm_vol_get,
1732         .put = snd_ymfpci_pcm_vol_put,
1733 };
1734
1735
1736 /*
1737  *  Mixer routines
1738  */
1739
1740 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1741 {
1742         struct snd_ymfpci *chip = bus->private_data;
1743         chip->ac97_bus = NULL;
1744 }
1745
1746 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1747 {
1748         struct snd_ymfpci *chip = ac97->private_data;
1749         chip->ac97 = NULL;
1750 }
1751
1752 int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1753 {
1754         struct snd_ac97_template ac97;
1755         struct snd_kcontrol *kctl;
1756         struct snd_pcm_substream *substream;
1757         unsigned int idx;
1758         int err;
1759         static struct snd_ac97_bus_ops ops = {
1760                 .write = snd_ymfpci_codec_write,
1761                 .read = snd_ymfpci_codec_read,
1762         };
1763
1764         if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1765                 return err;
1766         chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1767         chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1768
1769         memset(&ac97, 0, sizeof(ac97));
1770         ac97.private_data = chip;
1771         ac97.private_free = snd_ymfpci_mixer_free_ac97;
1772         if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1773                 return err;
1774
1775         /* to be sure */
1776         snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1777                              AC97_EA_VRA|AC97_EA_VRM, 0);
1778
1779         for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1780                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1781                         return err;
1782         }
1783
1784         /* add S/PDIF control */
1785         snd_assert(chip->pcm_spdif != NULL, return -EIO);
1786         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1787                 return err;
1788         kctl->id.device = chip->pcm_spdif->device;
1789         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1790                 return err;
1791         kctl->id.device = chip->pcm_spdif->device;
1792         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1793                 return err;
1794         kctl->id.device = chip->pcm_spdif->device;
1795         chip->spdif_pcm_ctl = kctl;
1796
1797         /* direct recording source */
1798         if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1799             (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1800                 return err;
1801
1802         /*
1803          * shared rear/line-in
1804          */
1805         if (rear_switch) {
1806                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1807                         return err;
1808         }
1809
1810         /* per-voice volume */
1811         substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1812         for (idx = 0; idx < 32; ++idx) {
1813                 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1814                 if (!kctl)
1815                         return -ENOMEM;
1816                 kctl->id.device = chip->pcm->device;
1817                 kctl->id.subdevice = idx;
1818                 kctl->private_value = (unsigned long)substream;
1819                 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1820                         return err;
1821                 chip->pcm_mixer[idx].left = 0x8000;
1822                 chip->pcm_mixer[idx].right = 0x8000;
1823                 chip->pcm_mixer[idx].ctl = kctl;
1824                 substream = substream->next;
1825         }
1826
1827         return 0;
1828 }
1829
1830
1831 /*
1832  * timer
1833  */
1834
1835 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1836 {
1837         struct snd_ymfpci *chip;
1838         unsigned long flags;
1839         unsigned int count;
1840
1841         chip = snd_timer_chip(timer);
1842         count = (timer->sticks << 1) - 1;
1843         spin_lock_irqsave(&chip->reg_lock, flags);
1844         snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1845         snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1846         spin_unlock_irqrestore(&chip->reg_lock, flags);
1847         return 0;
1848 }
1849
1850 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1851 {
1852         struct snd_ymfpci *chip;
1853         unsigned long flags;
1854
1855         chip = snd_timer_chip(timer);
1856         spin_lock_irqsave(&chip->reg_lock, flags);
1857         snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1858         spin_unlock_irqrestore(&chip->reg_lock, flags);
1859         return 0;
1860 }
1861
1862 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1863                                                unsigned long *num, unsigned long *den)
1864 {
1865         *num = 1;
1866         *den = 48000;
1867         return 0;
1868 }
1869
1870 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1871         .flags = SNDRV_TIMER_HW_AUTO,
1872         .resolution = 20833, /* 1/fs = 20.8333...us */
1873         .ticks = 0x8000,
1874         .start = snd_ymfpci_timer_start,
1875         .stop = snd_ymfpci_timer_stop,
1876         .precise_resolution = snd_ymfpci_timer_precise_resolution,
1877 };
1878
1879 int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1880 {
1881         struct snd_timer *timer = NULL;
1882         struct snd_timer_id tid;
1883         int err;
1884
1885         tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1886         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1887         tid.card = chip->card->number;
1888         tid.device = device;
1889         tid.subdevice = 0;
1890         if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1891                 strcpy(timer->name, "YMFPCI timer");
1892                 timer->private_data = chip;
1893                 timer->hw = snd_ymfpci_timer_hw;
1894         }
1895         chip->timer = timer;
1896         return err;
1897 }
1898
1899
1900 /*
1901  *  proc interface
1902  */
1903
1904 static void snd_ymfpci_proc_read(struct snd_info_entry *entry, 
1905                                  struct snd_info_buffer *buffer)
1906 {
1907         struct snd_ymfpci *chip = entry->private_data;
1908         int i;
1909         
1910         snd_iprintf(buffer, "YMFPCI\n\n");
1911         for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1912                 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1913 }
1914
1915 static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1916 {
1917         struct snd_info_entry *entry;
1918         
1919         if (! snd_card_proc_new(card, "ymfpci", &entry))
1920                 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
1921         return 0;
1922 }
1923
1924 /*
1925  *  initialization routines
1926  */
1927
1928 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1929 {
1930         u8 cmd;
1931
1932         pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1933 #if 0 // force to reset
1934         if (cmd & 0x03) {
1935 #endif
1936                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1937                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1938                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1939                 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1940                 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1941 #if 0
1942         }
1943 #endif
1944 }
1945
1946 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1947 {
1948         snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1949 }
1950
1951 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1952 {
1953         u32 val;
1954         int timeout = 1000;
1955
1956         val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1957         if (val)
1958                 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1959         while (timeout-- > 0) {
1960                 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1961                 if ((val & 0x00000002) == 0)
1962                         break;
1963         }
1964 }
1965
1966 #define FIRMWARE_IN_THE_KERNEL
1967
1968 #ifdef FIRMWARE_IN_THE_KERNEL
1969
1970 #include "ymfpci_image.h"
1971
1972 static struct firmware snd_ymfpci_dsp_microcode = {
1973         .size = YDSXG_DSPLENGTH,
1974         .data = (u8 *)DspInst,
1975 };
1976 static struct firmware snd_ymfpci_controller_microcode = {
1977         .size = YDSXG_CTRLLENGTH,
1978         .data = (u8 *)CntrlInst,
1979 };
1980 static struct firmware snd_ymfpci_controller_1e_microcode = {
1981         .size = YDSXG_CTRLLENGTH,
1982         .data = (u8 *)CntrlInst1E,
1983 };
1984 #endif
1985
1986 #ifdef __LITTLE_ENDIAN
1987 static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
1988 #else
1989 static void snd_ymfpci_convert_from_le(const struct firmware *fw)
1990 {
1991         int i;
1992         u32 *data = (u32 *)fw->data;
1993
1994         for (i = 0; i < fw->size / 4; ++i)
1995                 le32_to_cpus(&data[i]);
1996 }
1997 #endif
1998
1999 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2000 {
2001         int err, is_1e;
2002         const char *name;
2003
2004         err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2005                                &chip->pci->dev);
2006         if (err >= 0) {
2007                 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2008                         snd_ymfpci_convert_from_le(chip->dsp_microcode);
2009                 else {
2010                         snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2011                         err = -EINVAL;
2012                 }
2013         }
2014         if (err < 0) {
2015 #ifdef FIRMWARE_IN_THE_KERNEL
2016                 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2017 #else
2018                 return err;
2019 #endif
2020         }
2021         is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2022                 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2023                 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2024                 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2025         name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2026         err = request_firmware(&chip->controller_microcode, name,
2027                                &chip->pci->dev);
2028         if (err >= 0) {
2029                 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2030                         snd_ymfpci_convert_from_le(chip->controller_microcode);
2031                 else {
2032                         snd_printk(KERN_ERR "controller microcode"
2033                                    " has wrong size\n");
2034                         err = -EINVAL;
2035                 }
2036         }
2037         if (err < 0) {
2038 #ifdef FIRMWARE_IN_THE_KERNEL
2039                 chip->controller_microcode =
2040                         is_1e ? &snd_ymfpci_controller_1e_microcode
2041                               : &snd_ymfpci_controller_microcode;
2042 #else
2043                 return err;
2044 #endif
2045         }
2046         return 0;
2047 }
2048
2049 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2050 {
2051         int i;
2052         u16 ctrl;
2053         u32 *inst;
2054
2055         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2056         snd_ymfpci_disable_dsp(chip);
2057         snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2058         snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2059         snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2060         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2061         snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2062         snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2063         snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2064         ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2065         snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2066
2067         /* setup DSP instruction code */
2068         inst = (u32 *)chip->dsp_microcode->data;
2069         for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2070                 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
2071
2072         /* setup control instruction code */
2073         inst = (u32 *)chip->controller_microcode->data;
2074         for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2075                 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2076
2077         snd_ymfpci_enable_dsp(chip);
2078 }
2079
2080 static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2081 {
2082         long size, playback_ctrl_size;
2083         int voice, bank, reg;
2084         u8 *ptr;
2085         dma_addr_t ptr_addr;
2086
2087         playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2088         chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2089         chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2090         chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2091         chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2092         
2093         size = ALIGN(playback_ctrl_size, 0x100) +
2094                ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2095                ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2096                ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2097                chip->work_size;
2098         /* work_ptr must be aligned to 256 bytes, but it's already
2099            covered with the kernel page allocation mechanism */
2100         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2101                                 size, &chip->work_ptr) < 0) 
2102                 return -ENOMEM;
2103         ptr = chip->work_ptr.area;
2104         ptr_addr = chip->work_ptr.addr;
2105         memset(ptr, 0, size);   /* for sure */
2106
2107         chip->bank_base_playback = ptr;
2108         chip->bank_base_playback_addr = ptr_addr;
2109         chip->ctrl_playback = (u32 *)ptr;
2110         chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2111         ptr += ALIGN(playback_ctrl_size, 0x100);
2112         ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2113         for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2114                 chip->voices[voice].number = voice;
2115                 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2116                 chip->voices[voice].bank_addr = ptr_addr;
2117                 for (bank = 0; bank < 2; bank++) {
2118                         chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2119                         ptr += chip->bank_size_playback;
2120                         ptr_addr += chip->bank_size_playback;
2121                 }
2122         }
2123         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2124         ptr_addr = ALIGN(ptr_addr, 0x100);
2125         chip->bank_base_capture = ptr;
2126         chip->bank_base_capture_addr = ptr_addr;
2127         for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2128                 for (bank = 0; bank < 2; bank++) {
2129                         chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2130                         ptr += chip->bank_size_capture;
2131                         ptr_addr += chip->bank_size_capture;
2132                 }
2133         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2134         ptr_addr = ALIGN(ptr_addr, 0x100);
2135         chip->bank_base_effect = ptr;
2136         chip->bank_base_effect_addr = ptr_addr;
2137         for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2138                 for (bank = 0; bank < 2; bank++) {
2139                         chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2140                         ptr += chip->bank_size_effect;
2141                         ptr_addr += chip->bank_size_effect;
2142                 }
2143         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2144         ptr_addr = ALIGN(ptr_addr, 0x100);
2145         chip->work_base = ptr;
2146         chip->work_base_addr = ptr_addr;
2147         
2148         snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2149
2150         snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2151         snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2152         snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2153         snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2154         snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2155
2156         /* S/PDIF output initialization */
2157         chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2158         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2159         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2160
2161         /* S/PDIF input initialization */
2162         snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2163
2164         /* digital mixer setup */
2165         for (reg = 0x80; reg < 0xc0; reg += 4)
2166                 snd_ymfpci_writel(chip, reg, 0);
2167         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2168         snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2169         snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2170         snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2171         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2172         snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2173         snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2174         
2175         return 0;
2176 }
2177
2178 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2179 {
2180         u16 ctrl;
2181
2182         snd_assert(chip != NULL, return -EINVAL);
2183
2184         if (chip->res_reg_area) {       /* don't touch busy hardware */
2185                 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2186                 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2187                 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2188                 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2189                 snd_ymfpci_disable_dsp(chip);
2190                 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2191                 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2192                 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2193                 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2194                 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2195                 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2196                 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2197         }
2198
2199         snd_ymfpci_ac3_done(chip);
2200
2201         /* Set PCI device to D3 state */
2202 #if 0
2203         /* FIXME: temporarily disabled, otherwise we cannot fire up
2204          * the chip again unless reboot.  ACPI bug?
2205          */
2206         pci_set_power_state(chip->pci, 3);
2207 #endif
2208
2209 #ifdef CONFIG_PM
2210         vfree(chip->saved_regs);
2211 #endif
2212         release_and_free_resource(chip->mpu_res);
2213         release_and_free_resource(chip->fm_res);
2214         snd_ymfpci_free_gameport(chip);
2215         if (chip->reg_area_virt)
2216                 iounmap(chip->reg_area_virt);
2217         if (chip->work_ptr.area)
2218                 snd_dma_free_pages(&chip->work_ptr);
2219         
2220         if (chip->irq >= 0)
2221                 free_irq(chip->irq, chip);
2222         release_and_free_resource(chip->res_reg_area);
2223
2224         pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2225         
2226         pci_disable_device(chip->pci);
2227 #ifdef FIRMWARE_IN_THE_KERNEL
2228         if (chip->dsp_microcode != &snd_ymfpci_dsp_microcode)
2229 #endif
2230                 release_firmware(chip->dsp_microcode);
2231 #ifdef FIRMWARE_IN_THE_KERNEL
2232         if (chip->controller_microcode != &snd_ymfpci_controller_microcode &&
2233             chip->controller_microcode != &snd_ymfpci_controller_1e_microcode)
2234 #endif
2235                 release_firmware(chip->controller_microcode);
2236         kfree(chip);
2237         return 0;
2238 }
2239
2240 static int snd_ymfpci_dev_free(struct snd_device *device)
2241 {
2242         struct snd_ymfpci *chip = device->device_data;
2243         return snd_ymfpci_free(chip);
2244 }
2245
2246 #ifdef CONFIG_PM
2247 static int saved_regs_index[] = {
2248         /* spdif */
2249         YDSXGR_SPDIFOUTCTRL,
2250         YDSXGR_SPDIFOUTSTATUS,
2251         YDSXGR_SPDIFINCTRL,
2252         /* volumes */
2253         YDSXGR_PRIADCLOOPVOL,
2254         YDSXGR_NATIVEDACINVOL,
2255         YDSXGR_NATIVEDACOUTVOL,
2256         // YDSXGR_BUF441OUTVOL,
2257         YDSXGR_NATIVEADCINVOL,
2258         YDSXGR_SPDIFLOOPVOL,
2259         YDSXGR_SPDIFOUTVOL,
2260         YDSXGR_ZVOUTVOL,
2261         YDSXGR_LEGACYOUTVOL,
2262         /* address bases */
2263         YDSXGR_PLAYCTRLBASE,
2264         YDSXGR_RECCTRLBASE,
2265         YDSXGR_EFFCTRLBASE,
2266         YDSXGR_WORKBASE,
2267         /* capture set up */
2268         YDSXGR_MAPOFREC,
2269         YDSXGR_RECFORMAT,
2270         YDSXGR_RECSLOTSR,
2271         YDSXGR_ADCFORMAT,
2272         YDSXGR_ADCSLOTSR,
2273 };
2274 #define YDSXGR_NUM_SAVED_REGS   ARRAY_SIZE(saved_regs_index)
2275
2276 int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
2277 {
2278         struct snd_card *card = pci_get_drvdata(pci);
2279         struct snd_ymfpci *chip = card->private_data;
2280         unsigned int i;
2281         
2282         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2283         snd_pcm_suspend_all(chip->pcm);
2284         snd_pcm_suspend_all(chip->pcm2);
2285         snd_pcm_suspend_all(chip->pcm_spdif);
2286         snd_pcm_suspend_all(chip->pcm_4ch);
2287         snd_ac97_suspend(chip->ac97);
2288         for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2289                 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2290         chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2291         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2292         snd_ymfpci_disable_dsp(chip);
2293         pci_disable_device(pci);
2294         pci_save_state(pci);
2295         pci_set_power_state(pci, pci_choose_state(pci, state));
2296         return 0;
2297 }
2298
2299 int snd_ymfpci_resume(struct pci_dev *pci)
2300 {
2301         struct snd_card *card = pci_get_drvdata(pci);
2302         struct snd_ymfpci *chip = card->private_data;
2303         unsigned int i;
2304
2305         pci_set_power_state(pci, PCI_D0);
2306         pci_restore_state(pci);
2307         if (pci_enable_device(pci) < 0) {
2308                 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2309                        "disabling device\n");
2310                 snd_card_disconnect(card);
2311                 return -EIO;
2312         }
2313         pci_set_master(pci);
2314         snd_ymfpci_aclink_reset(pci);
2315         snd_ymfpci_codec_ready(chip, 0);
2316         snd_ymfpci_download_image(chip);
2317         udelay(100);
2318
2319         for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2320                 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2321
2322         snd_ac97_resume(chip->ac97);
2323
2324         /* start hw again */
2325         if (chip->start_count > 0) {
2326                 spin_lock_irq(&chip->reg_lock);
2327                 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2328                 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2329                 spin_unlock_irq(&chip->reg_lock);
2330         }
2331         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2332         return 0;
2333 }
2334 #endif /* CONFIG_PM */
2335
2336 int __devinit snd_ymfpci_create(struct snd_card *card,
2337                                 struct pci_dev * pci,
2338                                 unsigned short old_legacy_ctrl,
2339                                 struct snd_ymfpci ** rchip)
2340 {
2341         struct snd_ymfpci *chip;
2342         int err;
2343         static struct snd_device_ops ops = {
2344                 .dev_free =     snd_ymfpci_dev_free,
2345         };
2346         
2347         *rchip = NULL;
2348
2349         /* enable PCI device */
2350         if ((err = pci_enable_device(pci)) < 0)
2351                 return err;
2352
2353         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2354         if (chip == NULL) {
2355                 pci_disable_device(pci);
2356                 return -ENOMEM;
2357         }
2358         chip->old_legacy_ctrl = old_legacy_ctrl;
2359         spin_lock_init(&chip->reg_lock);
2360         spin_lock_init(&chip->voice_lock);
2361         init_waitqueue_head(&chip->interrupt_sleep);
2362         atomic_set(&chip->interrupt_sleep_count, 0);
2363         chip->card = card;
2364         chip->pci = pci;
2365         chip->irq = -1;
2366         chip->device_id = pci->device;
2367         pci_read_config_byte(pci, PCI_REVISION_ID, &chip->rev);
2368         chip->reg_area_phys = pci_resource_start(pci, 0);
2369         chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2370         pci_set_master(pci);
2371
2372         if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2373                 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2374                 snd_ymfpci_free(chip);
2375                 return -EBUSY;
2376         }
2377         if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2378                         "YMFPCI", chip)) {
2379                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2380                 snd_ymfpci_free(chip);
2381                 return -EBUSY;
2382         }
2383         chip->irq = pci->irq;
2384
2385         snd_ymfpci_aclink_reset(pci);
2386         if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2387                 snd_ymfpci_free(chip);
2388                 return -EIO;
2389         }
2390
2391         err = snd_ymfpci_request_firmware(chip);
2392         if (err < 0) {
2393                 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2394                 snd_ymfpci_free(chip);
2395                 return err;
2396         }
2397         snd_ymfpci_download_image(chip);
2398
2399         udelay(100); /* seems we need a delay after downloading image.. */
2400
2401         if (snd_ymfpci_memalloc(chip) < 0) {
2402                 snd_ymfpci_free(chip);
2403                 return -EIO;
2404         }
2405
2406         if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2407                 snd_ymfpci_free(chip);
2408                 return err;
2409         }
2410
2411 #ifdef CONFIG_PM
2412         chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2413         if (chip->saved_regs == NULL) {
2414                 snd_ymfpci_free(chip);
2415                 return -ENOMEM;
2416         }
2417 #endif
2418
2419         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2420                 snd_ymfpci_free(chip);
2421                 return err;
2422         }
2423
2424         snd_ymfpci_proc_init(card, chip);
2425
2426         snd_card_set_dev(card, &pci->dev);
2427
2428         *rchip = chip;
2429         return 0;
2430 }