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