blob: a1aa74b79b3d9c2086aa6acc21df188cbbcf12b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of YMF724/740/744/754 chips
4 *
5 * BUGS:
6 * --
7 *
8 * TODO:
9 * --
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <sound/driver.h>
28#include <linux/delay.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/vmalloc.h>
35
36#include <sound/core.h>
37#include <sound/control.h>
38#include <sound/info.h>
39#include <sound/ymfpci.h>
40#include <sound/asoundef.h>
41#include <sound/mpu401.h>
42
43#include <asm/io.h>
44
45/*
46 * constants
47 */
48
49/*
50 * common I/O routines
51 */
52
Takashi Iwai208a1b42005-11-17 14:53:41 +010053static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Takashi Iwai208a1b42005-11-17 14:53:41 +010055static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 return readb(chip->reg_area_virt + offset);
58}
59
Takashi Iwai208a1b42005-11-17 14:53:41 +010060static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 writeb(val, chip->reg_area_virt + offset);
63}
64
Takashi Iwai208a1b42005-11-17 14:53:41 +010065static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 return readw(chip->reg_area_virt + offset);
68}
69
Takashi Iwai208a1b42005-11-17 14:53:41 +010070static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 writew(val, chip->reg_area_virt + offset);
73}
74
Takashi Iwai208a1b42005-11-17 14:53:41 +010075static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 return readl(chip->reg_area_virt + offset);
78}
79
Takashi Iwai208a1b42005-11-17 14:53:41 +010080static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 writel(val, chip->reg_area_virt + offset);
83}
84
Takashi Iwai208a1b42005-11-17 14:53:41 +010085static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020087 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
89
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020090 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 do {
92 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
93 return 0;
94 set_current_state(TASK_UNINTERRUPTIBLE);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020095 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020096 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020097 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EBUSY;
99}
100
Takashi Iwai208a1b42005-11-17 14:53:41 +0100101static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100103 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 u32 cmd;
105
106 snd_ymfpci_codec_ready(chip, 0);
107 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
108 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
109}
110
Takashi Iwai208a1b42005-11-17 14:53:41 +0100111static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100113 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (snd_ymfpci_codec_ready(chip, 0))
116 return ~0;
117 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
118 if (snd_ymfpci_codec_ready(chip, 0))
119 return ~0;
120 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
121 int i;
122 for (i = 0; i < 600; i++)
123 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
124 }
125 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
126}
127
128/*
129 * Misc routines
130 */
131
132static u32 snd_ymfpci_calc_delta(u32 rate)
133{
134 switch (rate) {
135 case 8000: return 0x02aaab00;
136 case 11025: return 0x03accd00;
137 case 16000: return 0x05555500;
138 case 22050: return 0x07599a00;
139 case 32000: return 0x0aaaab00;
140 case 44100: return 0x0eb33300;
141 default: return ((rate << 16) / 375) << 5;
142 }
143}
144
145static u32 def_rate[8] = {
146 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
147};
148
149static u32 snd_ymfpci_calc_lpfK(u32 rate)
150{
151 u32 i;
152 static u32 val[8] = {
153 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
154 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
155 };
156
157 if (rate == 44100)
158 return 0x40000000; /* FIXME: What's the right value? */
159 for (i = 0; i < 8; i++)
160 if (rate <= def_rate[i])
161 return val[i];
162 return val[0];
163}
164
165static u32 snd_ymfpci_calc_lpfQ(u32 rate)
166{
167 u32 i;
168 static u32 val[8] = {
169 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
170 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
171 };
172
173 if (rate == 44100)
174 return 0x370A0000;
175 for (i = 0; i < 8; i++)
176 if (rate <= def_rate[i])
177 return val[i];
178 return val[0];
179}
180
181/*
182 * Hardware start management
183 */
184
Takashi Iwai208a1b42005-11-17 14:53:41 +0100185static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 unsigned long flags;
188
189 spin_lock_irqsave(&chip->reg_lock, flags);
190 if (chip->start_count++ > 0)
191 goto __end;
192 snd_ymfpci_writel(chip, YDSXGR_MODE,
193 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
194 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
195 __end:
196 spin_unlock_irqrestore(&chip->reg_lock, flags);
197}
198
Takashi Iwai208a1b42005-11-17 14:53:41 +0100199static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 unsigned long flags;
202 long timeout = 1000;
203
204 spin_lock_irqsave(&chip->reg_lock, flags);
205 if (--chip->start_count > 0)
206 goto __end;
207 snd_ymfpci_writel(chip, YDSXGR_MODE,
208 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
209 while (timeout-- > 0) {
210 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
211 break;
212 }
213 if (atomic_read(&chip->interrupt_sleep_count)) {
214 atomic_set(&chip->interrupt_sleep_count, 0);
215 wake_up(&chip->interrupt_sleep);
216 }
217 __end:
218 spin_unlock_irqrestore(&chip->reg_lock, flags);
219}
220
221/*
222 * Playback voice management
223 */
224
Takashi Iwai208a1b42005-11-17 14:53:41 +0100225static int voice_alloc(struct snd_ymfpci *chip,
226 enum snd_ymfpci_voice_type type, int pair,
227 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100229 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int idx;
231
232 *rvoice = NULL;
233 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
234 voice = &chip->voices[idx];
235 voice2 = pair ? &chip->voices[idx+1] : NULL;
236 if (voice->use || (voice2 && voice2->use))
237 continue;
238 voice->use = 1;
239 if (voice2)
240 voice2->use = 1;
241 switch (type) {
242 case YMFPCI_PCM:
243 voice->pcm = 1;
244 if (voice2)
245 voice2->pcm = 1;
246 break;
247 case YMFPCI_SYNTH:
248 voice->synth = 1;
249 break;
250 case YMFPCI_MIDI:
251 voice->midi = 1;
252 break;
253 }
254 snd_ymfpci_hw_start(chip);
255 if (voice2)
256 snd_ymfpci_hw_start(chip);
257 *rvoice = voice;
258 return 0;
259 }
260 return -ENOMEM;
261}
262
Takashi Iwai208a1b42005-11-17 14:53:41 +0100263static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
264 enum snd_ymfpci_voice_type type, int pair,
265 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 unsigned long flags;
268 int result;
269
270 snd_assert(rvoice != NULL, return -EINVAL);
271 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
272
273 spin_lock_irqsave(&chip->voice_lock, flags);
274 for (;;) {
275 result = voice_alloc(chip, type, pair, rvoice);
276 if (result == 0 || type != YMFPCI_PCM)
277 break;
278 /* TODO: synth/midi voice deallocation */
279 break;
280 }
281 spin_unlock_irqrestore(&chip->voice_lock, flags);
282 return result;
283}
284
Takashi Iwai208a1b42005-11-17 14:53:41 +0100285static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned long flags;
288
289 snd_assert(pvoice != NULL, return -EINVAL);
290 snd_ymfpci_hw_stop(chip);
291 spin_lock_irqsave(&chip->voice_lock, flags);
292 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
293 pvoice->ypcm = NULL;
294 pvoice->interrupt = NULL;
295 spin_unlock_irqrestore(&chip->voice_lock, flags);
296 return 0;
297}
298
299/*
300 * PCM part
301 */
302
Takashi Iwai208a1b42005-11-17 14:53:41 +0100303static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100305 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 u32 pos, delta;
307
308 if ((ypcm = voice->ypcm) == NULL)
309 return;
310 if (ypcm->substream == NULL)
311 return;
312 spin_lock(&chip->reg_lock);
313 if (ypcm->running) {
314 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
315 if (pos < ypcm->last_pos)
316 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
317 else
318 delta = pos - ypcm->last_pos;
319 ypcm->period_pos += delta;
320 ypcm->last_pos = pos;
321 if (ypcm->period_pos >= ypcm->period_size) {
322 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
323 ypcm->period_pos %= ypcm->period_size;
324 spin_unlock(&chip->reg_lock);
325 snd_pcm_period_elapsed(ypcm->substream);
326 spin_lock(&chip->reg_lock);
327 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200328
329 if (unlikely(ypcm->update_pcm_vol)) {
330 unsigned int subs = ypcm->substream->number;
331 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100332 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200333 u32 volume;
334
335 bank = &voice->bank[next_bank];
336 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
337 bank->left_gain_end = volume;
338 if (ypcm->output_rear)
339 bank->eff2_gain_end = volume;
340 if (ypcm->voices[1])
341 bank = &ypcm->voices[1]->bank[next_bank];
342 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
343 bank->right_gain_end = volume;
344 if (ypcm->output_rear)
345 bank->eff3_gain_end = volume;
346 ypcm->update_pcm_vol--;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 spin_unlock(&chip->reg_lock);
350}
351
Takashi Iwai208a1b42005-11-17 14:53:41 +0100352static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100354 struct snd_pcm_runtime *runtime = substream->runtime;
355 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
356 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 u32 pos, delta;
358
359 spin_lock(&chip->reg_lock);
360 if (ypcm->running) {
361 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
362 if (pos < ypcm->last_pos)
363 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
364 else
365 delta = pos - ypcm->last_pos;
366 ypcm->period_pos += delta;
367 ypcm->last_pos = pos;
368 if (ypcm->period_pos >= ypcm->period_size) {
369 ypcm->period_pos %= ypcm->period_size;
370 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
371 spin_unlock(&chip->reg_lock);
372 snd_pcm_period_elapsed(substream);
373 spin_lock(&chip->reg_lock);
374 }
375 }
376 spin_unlock(&chip->reg_lock);
377}
378
Takashi Iwai208a1b42005-11-17 14:53:41 +0100379static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int cmd)
381{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100382 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
383 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int result = 0;
385
386 spin_lock(&chip->reg_lock);
387 if (ypcm->voices[0] == NULL) {
388 result = -EINVAL;
389 goto __unlock;
390 }
391 switch (cmd) {
392 case SNDRV_PCM_TRIGGER_START:
393 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
394 case SNDRV_PCM_TRIGGER_RESUME:
395 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
396 if (ypcm->voices[1] != NULL)
397 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
398 ypcm->running = 1;
399 break;
400 case SNDRV_PCM_TRIGGER_STOP:
401 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
402 case SNDRV_PCM_TRIGGER_SUSPEND:
403 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
404 if (ypcm->voices[1] != NULL)
405 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
406 ypcm->running = 0;
407 break;
408 default:
409 result = -EINVAL;
410 break;
411 }
412 __unlock:
413 spin_unlock(&chip->reg_lock);
414 return result;
415}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100416static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int cmd)
418{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100419 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
420 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int result = 0;
422 u32 tmp;
423
424 spin_lock(&chip->reg_lock);
425 switch (cmd) {
426 case SNDRV_PCM_TRIGGER_START:
427 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
428 case SNDRV_PCM_TRIGGER_RESUME:
429 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
430 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
431 ypcm->running = 1;
432 break;
433 case SNDRV_PCM_TRIGGER_STOP:
434 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
435 case SNDRV_PCM_TRIGGER_SUSPEND:
436 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
437 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
438 ypcm->running = 0;
439 break;
440 default:
441 result = -EINVAL;
442 break;
443 }
444 spin_unlock(&chip->reg_lock);
445 return result;
446}
447
Takashi Iwai208a1b42005-11-17 14:53:41 +0100448static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int err;
451
452 if (ypcm->voices[1] != NULL && voices < 2) {
453 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
454 ypcm->voices[1] = NULL;
455 }
456 if (voices == 1 && ypcm->voices[0] != NULL)
457 return 0; /* already allocated */
458 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
459 return 0; /* already allocated */
460 if (voices > 1) {
461 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
462 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
463 ypcm->voices[0] = NULL;
464 }
465 }
466 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
467 if (err < 0)
468 return err;
469 ypcm->voices[0]->ypcm = ypcm;
470 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
471 if (voices > 1) {
472 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
473 ypcm->voices[1]->ypcm = ypcm;
474 }
475 return 0;
476}
477
Takashi Iwai208a1b42005-11-17 14:53:41 +0100478static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
479 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200480 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100482 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200484 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
485 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
486 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100487 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200489 u32 vol_left, vol_right;
490 u8 use_left, use_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200493 if (runtime->channels == 1) {
494 use_left = 1;
495 use_right = 1;
496 } else {
497 use_left = (voiceidx & 1) == 0;
498 use_right = !use_left;
499 }
500 if (has_pcm_volume) {
501 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
502 [ypcm->substream->number].left << 15);
503 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
504 [ypcm->substream->number].right << 15);
505 } else {
506 vol_left = cpu_to_le32(0x40000000);
507 vol_right = cpu_to_le32(0x40000000);
508 }
509 format = runtime->channels == 2 ? 0x00010000 : 0;
510 if (snd_pcm_format_width(runtime->format) == 8)
511 format |= 0x80000000;
512 if (runtime->channels == 2 && (voiceidx & 1) != 0)
513 format |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 for (nbank = 0; nbank < 2; nbank++) {
515 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200516 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200518 bank->base = cpu_to_le32(runtime->dma_addr);
519 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 bank->delta =
522 bank->delta_end = cpu_to_le32(delta);
523 bank->lpfK =
524 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200525 bank->eg_gain =
526 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200528 if (ypcm->output_front) {
529 if (use_left) {
530 bank->left_gain =
531 bank->left_gain_end = vol_left;
532 }
533 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200535 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200537 }
538 if (ypcm->output_rear) {
539 if (use_left) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 bank->eff2_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200541 bank->eff2_gain_end = vol_left;
542 }
543 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 bank->eff3_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200545 bank->eff3_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 }
548 }
549}
550
Takashi Iwai208a1b42005-11-17 14:53:41 +0100551static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
554 4096, &chip->ac3_tmp_base) < 0)
555 return -ENOMEM;
556
557 chip->bank_effect[3][0]->base =
558 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
559 chip->bank_effect[3][0]->loop_end =
560 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
561 chip->bank_effect[4][0]->base =
562 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
563 chip->bank_effect[4][0]->loop_end =
564 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
565
566 spin_lock_irq(&chip->reg_lock);
567 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
568 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
569 spin_unlock_irq(&chip->reg_lock);
570 return 0;
571}
572
Takashi Iwai208a1b42005-11-17 14:53:41 +0100573static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 spin_lock_irq(&chip->reg_lock);
576 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
577 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
578 spin_unlock_irq(&chip->reg_lock);
579 // snd_ymfpci_irq_wait(chip);
580 if (chip->ac3_tmp_base.area) {
581 snd_dma_free_pages(&chip->ac3_tmp_base);
582 chip->ac3_tmp_base.area = NULL;
583 }
584 return 0;
585}
586
Takashi Iwai208a1b42005-11-17 14:53:41 +0100587static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
588 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100590 struct snd_pcm_runtime *runtime = substream->runtime;
591 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int err;
593
594 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
595 return err;
596 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
597 return err;
598 return 0;
599}
600
Takashi Iwai208a1b42005-11-17 14:53:41 +0100601static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100603 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
604 struct snd_pcm_runtime *runtime = substream->runtime;
605 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (runtime->private_data == NULL)
608 return 0;
609 ypcm = runtime->private_data;
610
611 /* wait, until the PCI operations are not finished */
612 snd_ymfpci_irq_wait(chip);
613 snd_pcm_lib_free_pages(substream);
614 if (ypcm->voices[1]) {
615 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
616 ypcm->voices[1] = NULL;
617 }
618 if (ypcm->voices[0]) {
619 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
620 ypcm->voices[0] = NULL;
621 }
622 return 0;
623}
624
Takashi Iwai208a1b42005-11-17 14:53:41 +0100625static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100627 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
628 struct snd_pcm_runtime *runtime = substream->runtime;
629 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned int nvoice;
631
632 ypcm->period_size = runtime->period_size;
633 ypcm->buffer_size = runtime->buffer_size;
634 ypcm->period_pos = 0;
635 ypcm->last_pos = 0;
636 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200637 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
638 substream->pcm == chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
641
Takashi Iwai208a1b42005-11-17 14:53:41 +0100642static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
643 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
646}
647
Takashi Iwai208a1b42005-11-17 14:53:41 +0100648static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100650 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* wait, until the PCI operations are not finished */
653 snd_ymfpci_irq_wait(chip);
654 return snd_pcm_lib_free_pages(substream);
655}
656
Takashi Iwai208a1b42005-11-17 14:53:41 +0100657static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100659 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
660 struct snd_pcm_runtime *runtime = substream->runtime;
661 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
662 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int nbank;
664 u32 rate, format;
665
666 ypcm->period_size = runtime->period_size;
667 ypcm->buffer_size = runtime->buffer_size;
668 ypcm->period_pos = 0;
669 ypcm->last_pos = 0;
670 ypcm->shift = 0;
671 rate = ((48000 * 4096) / runtime->rate) - 1;
672 format = 0;
673 if (runtime->channels == 2) {
674 format |= 2;
675 ypcm->shift++;
676 }
677 if (snd_pcm_format_width(runtime->format) == 8)
678 format |= 1;
679 else
680 ypcm->shift++;
681 switch (ypcm->capture_bank_number) {
682 case 0:
683 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
684 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
685 break;
686 case 1:
687 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
688 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
689 break;
690 }
691 for (nbank = 0; nbank < 2; nbank++) {
692 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
693 bank->base = cpu_to_le32(runtime->dma_addr);
694 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
695 bank->start = 0;
696 bank->num_of_loops = 0;
697 }
698 return 0;
699}
700
Takashi Iwai208a1b42005-11-17 14:53:41 +0100701static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100703 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
704 struct snd_pcm_runtime *runtime = substream->runtime;
705 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
706 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (!(ypcm->running && voice))
709 return 0;
710 return le32_to_cpu(voice->bank[chip->active_bank].start);
711}
712
Takashi Iwai208a1b42005-11-17 14:53:41 +0100713static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100715 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
716 struct snd_pcm_runtime *runtime = substream->runtime;
717 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (!ypcm->running)
720 return 0;
721 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
722}
723
Takashi Iwai208a1b42005-11-17 14:53:41 +0100724static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 wait_queue_t wait;
727 int loops = 4;
728
729 while (loops-- > 0) {
730 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
731 continue;
732 init_waitqueue_entry(&wait, current);
733 add_wait_queue(&chip->interrupt_sleep, &wait);
734 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200735 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 remove_wait_queue(&chip->interrupt_sleep, &wait);
737 }
738}
739
740static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
741{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100742 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100744 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
747 if (status & 0x80000000) {
748 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
749 spin_lock(&chip->voice_lock);
750 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
751 voice = &chip->voices[nvoice];
752 if (voice->interrupt)
753 voice->interrupt(chip, voice);
754 }
755 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
756 if (chip->capture_substream[nvoice])
757 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
758 }
759#if 0
760 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
761 if (chip->effect_substream[nvoice])
762 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
763 }
764#endif
765 spin_unlock(&chip->voice_lock);
766 spin_lock(&chip->reg_lock);
767 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
768 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
769 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
770 spin_unlock(&chip->reg_lock);
771
772 if (atomic_read(&chip->interrupt_sleep_count)) {
773 atomic_set(&chip->interrupt_sleep_count, 0);
774 wake_up(&chip->interrupt_sleep);
775 }
776 }
777
778 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
779 if (status & 1) {
780 if (chip->timer)
781 snd_timer_interrupt(chip->timer, chip->timer->sticks);
782 }
783 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
784
785 if (chip->rawmidi)
786 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data, regs);
787 return IRQ_HANDLED;
788}
789
Takashi Iwai208a1b42005-11-17 14:53:41 +0100790static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 .info = (SNDRV_PCM_INFO_MMAP |
793 SNDRV_PCM_INFO_MMAP_VALID |
794 SNDRV_PCM_INFO_INTERLEAVED |
795 SNDRV_PCM_INFO_BLOCK_TRANSFER |
796 SNDRV_PCM_INFO_PAUSE |
797 SNDRV_PCM_INFO_RESUME),
798 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
799 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
800 .rate_min = 8000,
801 .rate_max = 48000,
802 .channels_min = 1,
803 .channels_max = 2,
804 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
805 .period_bytes_min = 64,
806 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
807 .periods_min = 3,
808 .periods_max = 1024,
809 .fifo_size = 0,
810};
811
Takashi Iwai208a1b42005-11-17 14:53:41 +0100812static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 .info = (SNDRV_PCM_INFO_MMAP |
815 SNDRV_PCM_INFO_MMAP_VALID |
816 SNDRV_PCM_INFO_INTERLEAVED |
817 SNDRV_PCM_INFO_BLOCK_TRANSFER |
818 SNDRV_PCM_INFO_PAUSE |
819 SNDRV_PCM_INFO_RESUME),
820 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
821 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
822 .rate_min = 8000,
823 .rate_max = 48000,
824 .channels_min = 1,
825 .channels_max = 2,
826 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
827 .period_bytes_min = 64,
828 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
829 .periods_min = 3,
830 .periods_max = 1024,
831 .fifo_size = 0,
832};
833
Takashi Iwai208a1b42005-11-17 14:53:41 +0100834static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jesper Juhl4d572772005-05-30 17:30:32 +0200836 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Takashi Iwai208a1b42005-11-17 14:53:41 +0100839static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100841 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
842 struct snd_pcm_runtime *runtime = substream->runtime;
843 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200845 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (ypcm == NULL)
847 return -ENOMEM;
848 ypcm->chip = chip;
849 ypcm->type = PLAYBACK_VOICE;
850 ypcm->substream = substream;
851 runtime->hw = snd_ymfpci_playback;
852 runtime->private_data = ypcm;
853 runtime->private_free = snd_ymfpci_pcm_free_substream;
854 /* FIXME? True value is 256/48 = 5.33333 ms */
855 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
856 return 0;
857}
858
859/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100860static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (! chip->rear_opened) {
863 if (! chip->spdif_opened) /* set AC3 */
864 snd_ymfpci_writel(chip, YDSXGR_MODE,
865 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
866 /* enable second codec (4CHEN) */
867 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
868 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
869 }
870}
871
872/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100873static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 if (! chip->rear_opened) {
876 if (! chip->spdif_opened)
877 snd_ymfpci_writel(chip, YDSXGR_MODE,
878 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
879 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
880 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
881 }
882}
883
Takashi Iwai208a1b42005-11-17 14:53:41 +0100884static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100886 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
887 struct snd_pcm_runtime *runtime = substream->runtime;
888 struct snd_ymfpci_pcm *ypcm;
889 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int err;
891
892 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
893 return err;
894 ypcm = runtime->private_data;
895 ypcm->output_front = 1;
896 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
897 spin_lock_irq(&chip->reg_lock);
898 if (ypcm->output_rear) {
899 ymfpci_open_extension(chip);
900 chip->rear_opened++;
901 }
902 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200903
904 kctl = chip->pcm_mixer[substream->number].ctl;
905 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
906 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
909
Takashi Iwai208a1b42005-11-17 14:53:41 +0100910static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100912 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
913 struct snd_pcm_runtime *runtime = substream->runtime;
914 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int err;
916
917 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
918 return err;
919 ypcm = runtime->private_data;
920 ypcm->output_front = 0;
921 ypcm->output_rear = 1;
922 spin_lock_irq(&chip->reg_lock);
923 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
924 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
925 ymfpci_open_extension(chip);
926 chip->spdif_pcm_bits = chip->spdif_bits;
927 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
928 chip->spdif_opened++;
929 spin_unlock_irq(&chip->reg_lock);
930
931 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
932 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
933 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
934 return 0;
935}
936
Takashi Iwai208a1b42005-11-17 14:53:41 +0100937static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100939 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
940 struct snd_pcm_runtime *runtime = substream->runtime;
941 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int err;
943
944 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
945 return err;
946 ypcm = runtime->private_data;
947 ypcm->output_front = 0;
948 ypcm->output_rear = 1;
949 spin_lock_irq(&chip->reg_lock);
950 ymfpci_open_extension(chip);
951 chip->rear_opened++;
952 spin_unlock_irq(&chip->reg_lock);
953 return 0;
954}
955
Takashi Iwai208a1b42005-11-17 14:53:41 +0100956static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 u32 capture_bank_number)
958{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100959 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
960 struct snd_pcm_runtime *runtime = substream->runtime;
961 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200963 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (ypcm == NULL)
965 return -ENOMEM;
966 ypcm->chip = chip;
967 ypcm->type = capture_bank_number + CAPTURE_REC;
968 ypcm->substream = substream;
969 ypcm->capture_bank_number = capture_bank_number;
970 chip->capture_substream[capture_bank_number] = substream;
971 runtime->hw = snd_ymfpci_capture;
972 /* FIXME? True value is 256/48 = 5.33333 ms */
973 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
974 runtime->private_data = ypcm;
975 runtime->private_free = snd_ymfpci_pcm_free_substream;
976 snd_ymfpci_hw_start(chip);
977 return 0;
978}
979
Takashi Iwai208a1b42005-11-17 14:53:41 +0100980static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 return snd_ymfpci_capture_open(substream, 0);
983}
984
Takashi Iwai208a1b42005-11-17 14:53:41 +0100985static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 return snd_ymfpci_capture_open(substream, 1);
988}
989
Takashi Iwai208a1b42005-11-17 14:53:41 +0100990static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 return 0;
993}
994
Takashi Iwai208a1b42005-11-17 14:53:41 +0100995static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100997 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
998 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
999 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 spin_lock_irq(&chip->reg_lock);
1002 if (ypcm->output_rear && chip->rear_opened > 0) {
1003 chip->rear_opened--;
1004 ymfpci_close_extension(chip);
1005 }
1006 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001007 kctl = chip->pcm_mixer[substream->number].ctl;
1008 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1009 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return snd_ymfpci_playback_close_1(substream);
1011}
1012
Takashi Iwai208a1b42005-11-17 14:53:41 +01001013static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001015 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 spin_lock_irq(&chip->reg_lock);
1018 chip->spdif_opened = 0;
1019 ymfpci_close_extension(chip);
1020 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1021 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1022 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1023 spin_unlock_irq(&chip->reg_lock);
1024 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1025 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1026 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1027 return snd_ymfpci_playback_close_1(substream);
1028}
1029
Takashi Iwai208a1b42005-11-17 14:53:41 +01001030static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001032 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 spin_lock_irq(&chip->reg_lock);
1035 if (chip->rear_opened > 0) {
1036 chip->rear_opened--;
1037 ymfpci_close_extension(chip);
1038 }
1039 spin_unlock_irq(&chip->reg_lock);
1040 return snd_ymfpci_playback_close_1(substream);
1041}
1042
Takashi Iwai208a1b42005-11-17 14:53:41 +01001043static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001045 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1046 struct snd_pcm_runtime *runtime = substream->runtime;
1047 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (ypcm != NULL) {
1050 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1051 snd_ymfpci_hw_stop(chip);
1052 }
1053 return 0;
1054}
1055
Takashi Iwai208a1b42005-11-17 14:53:41 +01001056static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 .open = snd_ymfpci_playback_open,
1058 .close = snd_ymfpci_playback_close,
1059 .ioctl = snd_pcm_lib_ioctl,
1060 .hw_params = snd_ymfpci_playback_hw_params,
1061 .hw_free = snd_ymfpci_playback_hw_free,
1062 .prepare = snd_ymfpci_playback_prepare,
1063 .trigger = snd_ymfpci_playback_trigger,
1064 .pointer = snd_ymfpci_playback_pointer,
1065};
1066
Takashi Iwai208a1b42005-11-17 14:53:41 +01001067static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 .open = snd_ymfpci_capture_rec_open,
1069 .close = snd_ymfpci_capture_close,
1070 .ioctl = snd_pcm_lib_ioctl,
1071 .hw_params = snd_ymfpci_capture_hw_params,
1072 .hw_free = snd_ymfpci_capture_hw_free,
1073 .prepare = snd_ymfpci_capture_prepare,
1074 .trigger = snd_ymfpci_capture_trigger,
1075 .pointer = snd_ymfpci_capture_pointer,
1076};
1077
Takashi Iwai208a1b42005-11-17 14:53:41 +01001078int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001080 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int err;
1082
1083 if (rpcm)
1084 *rpcm = NULL;
1085 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1086 return err;
1087 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1090 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1091
1092 /* global setup */
1093 pcm->info_flags = 0;
1094 strcpy(pcm->name, "YMFPCI");
1095 chip->pcm = pcm;
1096
1097 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1098 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1099
1100 if (rpcm)
1101 *rpcm = pcm;
1102 return 0;
1103}
1104
Takashi Iwai208a1b42005-11-17 14:53:41 +01001105static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 .open = snd_ymfpci_capture_ac97_open,
1107 .close = snd_ymfpci_capture_close,
1108 .ioctl = snd_pcm_lib_ioctl,
1109 .hw_params = snd_ymfpci_capture_hw_params,
1110 .hw_free = snd_ymfpci_capture_hw_free,
1111 .prepare = snd_ymfpci_capture_prepare,
1112 .trigger = snd_ymfpci_capture_trigger,
1113 .pointer = snd_ymfpci_capture_pointer,
1114};
1115
Takashi Iwai208a1b42005-11-17 14:53:41 +01001116int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001118 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int err;
1120
1121 if (rpcm)
1122 *rpcm = NULL;
1123 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1124 return err;
1125 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1128
1129 /* global setup */
1130 pcm->info_flags = 0;
1131 sprintf(pcm->name, "YMFPCI - %s",
1132 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1133 chip->pcm2 = pcm;
1134
1135 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1136 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1137
1138 if (rpcm)
1139 *rpcm = pcm;
1140 return 0;
1141}
1142
Takashi Iwai208a1b42005-11-17 14:53:41 +01001143static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 .open = snd_ymfpci_playback_spdif_open,
1145 .close = snd_ymfpci_playback_spdif_close,
1146 .ioctl = snd_pcm_lib_ioctl,
1147 .hw_params = snd_ymfpci_playback_hw_params,
1148 .hw_free = snd_ymfpci_playback_hw_free,
1149 .prepare = snd_ymfpci_playback_prepare,
1150 .trigger = snd_ymfpci_playback_trigger,
1151 .pointer = snd_ymfpci_playback_pointer,
1152};
1153
Takashi Iwai208a1b42005-11-17 14:53:41 +01001154int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001156 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int err;
1158
1159 if (rpcm)
1160 *rpcm = NULL;
1161 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1162 return err;
1163 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1166
1167 /* global setup */
1168 pcm->info_flags = 0;
1169 strcpy(pcm->name, "YMFPCI - IEC958");
1170 chip->pcm_spdif = pcm;
1171
1172 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1173 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1174
1175 if (rpcm)
1176 *rpcm = pcm;
1177 return 0;
1178}
1179
Takashi Iwai208a1b42005-11-17 14:53:41 +01001180static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .open = snd_ymfpci_playback_4ch_open,
1182 .close = snd_ymfpci_playback_4ch_close,
1183 .ioctl = snd_pcm_lib_ioctl,
1184 .hw_params = snd_ymfpci_playback_hw_params,
1185 .hw_free = snd_ymfpci_playback_hw_free,
1186 .prepare = snd_ymfpci_playback_prepare,
1187 .trigger = snd_ymfpci_playback_trigger,
1188 .pointer = snd_ymfpci_playback_pointer,
1189};
1190
Takashi Iwai208a1b42005-11-17 14:53:41 +01001191int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001193 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int err;
1195
1196 if (rpcm)
1197 *rpcm = NULL;
1198 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1199 return err;
1200 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1203
1204 /* global setup */
1205 pcm->info_flags = 0;
1206 strcpy(pcm->name, "YMFPCI - Rear PCM");
1207 chip->pcm_4ch = pcm;
1208
1209 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1210 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1211
1212 if (rpcm)
1213 *rpcm = pcm;
1214 return 0;
1215}
1216
Takashi Iwai208a1b42005-11-17 14:53:41 +01001217static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1220 uinfo->count = 1;
1221 return 0;
1222}
1223
Takashi Iwai208a1b42005-11-17 14:53:41 +01001224static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1225 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001227 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 spin_lock_irq(&chip->reg_lock);
1230 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1231 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001232 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 spin_unlock_irq(&chip->reg_lock);
1234 return 0;
1235}
1236
Takashi Iwai208a1b42005-11-17 14:53:41 +01001237static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1238 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001240 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned int val;
1242 int change;
1243
1244 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1245 (ucontrol->value.iec958.status[1] << 8);
1246 spin_lock_irq(&chip->reg_lock);
1247 change = chip->spdif_bits != val;
1248 chip->spdif_bits = val;
1249 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1250 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1251 spin_unlock_irq(&chip->reg_lock);
1252 return change;
1253}
1254
Takashi Iwai208a1b42005-11-17 14:53:41 +01001255static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1258 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1259 .info = snd_ymfpci_spdif_default_info,
1260 .get = snd_ymfpci_spdif_default_get,
1261 .put = snd_ymfpci_spdif_default_put
1262};
1263
Takashi Iwai208a1b42005-11-17 14:53:41 +01001264static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1267 uinfo->count = 1;
1268 return 0;
1269}
1270
Takashi Iwai208a1b42005-11-17 14:53:41 +01001271static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1272 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001274 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 spin_lock_irq(&chip->reg_lock);
1277 ucontrol->value.iec958.status[0] = 0x3e;
1278 ucontrol->value.iec958.status[1] = 0xff;
1279 spin_unlock_irq(&chip->reg_lock);
1280 return 0;
1281}
1282
Takashi Iwai208a1b42005-11-17 14:53:41 +01001283static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1286 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1287 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1288 .info = snd_ymfpci_spdif_mask_info,
1289 .get = snd_ymfpci_spdif_mask_get,
1290};
1291
Takashi Iwai208a1b42005-11-17 14:53:41 +01001292static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1295 uinfo->count = 1;
1296 return 0;
1297}
1298
Takashi Iwai208a1b42005-11-17 14:53:41 +01001299static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1300 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001302 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 spin_lock_irq(&chip->reg_lock);
1305 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1306 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001307 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 spin_unlock_irq(&chip->reg_lock);
1309 return 0;
1310}
1311
Takashi Iwai208a1b42005-11-17 14:53:41 +01001312static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1313 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001315 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 unsigned int val;
1317 int change;
1318
1319 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1320 (ucontrol->value.iec958.status[1] << 8);
1321 spin_lock_irq(&chip->reg_lock);
1322 change = chip->spdif_pcm_bits != val;
1323 chip->spdif_pcm_bits = val;
1324 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1325 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1326 spin_unlock_irq(&chip->reg_lock);
1327 return change;
1328}
1329
Takashi Iwai208a1b42005-11-17 14:53:41 +01001330static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1333 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1334 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1335 .info = snd_ymfpci_spdif_stream_info,
1336 .get = snd_ymfpci_spdif_stream_get,
1337 .put = snd_ymfpci_spdif_stream_put
1338};
1339
Takashi Iwai208a1b42005-11-17 14:53:41 +01001340static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1343
1344 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1345 info->count = 1;
1346 info->value.enumerated.items = 3;
1347 if (info->value.enumerated.item > 2)
1348 info->value.enumerated.item = 2;
1349 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1350 return 0;
1351}
1352
Takashi Iwai208a1b42005-11-17 14:53:41 +01001353static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001355 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 u16 reg;
1357
1358 spin_lock_irq(&chip->reg_lock);
1359 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1360 spin_unlock_irq(&chip->reg_lock);
1361 if (!(reg & 0x100))
1362 value->value.enumerated.item[0] = 0;
1363 else
1364 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1365 return 0;
1366}
1367
Takashi Iwai208a1b42005-11-17 14:53:41 +01001368static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001370 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 u16 reg, old_reg;
1372
1373 spin_lock_irq(&chip->reg_lock);
1374 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1375 if (value->value.enumerated.item[0] == 0)
1376 reg = old_reg & ~0x100;
1377 else
1378 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1379 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1380 spin_unlock_irq(&chip->reg_lock);
1381 return reg != old_reg;
1382}
1383
Takashi Iwai208a1b42005-11-17 14:53:41 +01001384static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1387 .name = "Direct Recording Source",
1388 .info = snd_ymfpci_drec_source_info,
1389 .get = snd_ymfpci_drec_source_get,
1390 .put = snd_ymfpci_drec_source_put
1391};
1392
1393/*
1394 * Mixer controls
1395 */
1396
Glen Masgaid602c882005-09-28 08:19:05 +02001397#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1399 .info = snd_ymfpci_info_single, \
1400 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001401 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Takashi Iwai208a1b42005-11-17 14:53:41 +01001403static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol,
1404 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Glen Masgaid602c882005-09-28 08:19:05 +02001406 int reg = kcontrol->private_value & 0xffff;
1407
1408 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 case YDSXGR_SPDIFOUTCTRL: break;
1410 case YDSXGR_SPDIFINCTRL: break;
1411 default: return -EINVAL;
1412 }
Adrian Bunk467a8c22005-04-11 14:11:00 +02001413 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 uinfo->count = 1;
1415 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001416 uinfo->value.integer.max = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418}
1419
Takashi Iwai208a1b42005-11-17 14:53:41 +01001420static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001423 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001424 int reg = kcontrol->private_value & 0xffff;
1425 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1426 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Glen Masgaid602c882005-09-28 08:19:05 +02001428 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 case YDSXGR_SPDIFOUTCTRL: break;
1430 case YDSXGR_SPDIFINCTRL: break;
1431 default: return -EINVAL;
1432 }
Glen Masgaid602c882005-09-28 08:19:05 +02001433 ucontrol->value.integer.value[0] =
1434 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436}
1437
Takashi Iwai208a1b42005-11-17 14:53:41 +01001438static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001441 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001442 int reg = kcontrol->private_value & 0xffff;
1443 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1444 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int change;
1446 unsigned int val, oval;
1447
Glen Masgaid602c882005-09-28 08:19:05 +02001448 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 case YDSXGR_SPDIFOUTCTRL: break;
1450 case YDSXGR_SPDIFINCTRL: break;
1451 default: return -EINVAL;
1452 }
1453 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 val <<= shift;
1455 spin_lock_irq(&chip->reg_lock);
1456 oval = snd_ymfpci_readl(chip, reg);
1457 val = (oval & ~(mask << shift)) | val;
1458 change = val != oval;
1459 snd_ymfpci_writel(chip, reg, val);
1460 spin_unlock_irq(&chip->reg_lock);
1461 return change;
1462}
1463
1464#define YMFPCI_DOUBLE(xname, xindex, reg) \
1465{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1466 .info = snd_ymfpci_info_double, \
1467 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1468 .private_value = reg }
1469
Takashi Iwai208a1b42005-11-17 14:53:41 +01001470static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (reg < 0x80 || reg >= 0xc0)
1475 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001476 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 uinfo->count = 2;
1478 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001479 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
Takashi Iwai208a1b42005-11-17 14:53:41 +01001483static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001485 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001487 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned int val;
1489
1490 if (reg < 0x80 || reg >= 0xc0)
1491 return -EINVAL;
1492 spin_lock_irq(&chip->reg_lock);
1493 val = snd_ymfpci_readl(chip, reg);
1494 spin_unlock_irq(&chip->reg_lock);
1495 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1496 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
Takashi Iwai208a1b42005-11-17 14:53:41 +01001500static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001502 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001504 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int change;
1506 unsigned int val1, val2, oval;
1507
1508 if (reg < 0x80 || reg >= 0xc0)
1509 return -EINVAL;
1510 val1 = ucontrol->value.integer.value[0] & mask;
1511 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 val1 <<= shift_left;
1513 val2 <<= shift_right;
1514 spin_lock_irq(&chip->reg_lock);
1515 oval = snd_ymfpci_readl(chip, reg);
1516 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1517 change = val1 != oval;
1518 snd_ymfpci_writel(chip, reg, val1);
1519 spin_unlock_irq(&chip->reg_lock);
1520 return change;
1521}
1522
1523/*
1524 * 4ch duplication
1525 */
Takashi Iwai208a1b42005-11-17 14:53:41 +01001526static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1529 uinfo->count = 1;
1530 uinfo->value.integer.min = 0;
1531 uinfo->value.integer.max = 1;
1532 return 0;
1533}
1534
Takashi Iwai208a1b42005-11-17 14:53:41 +01001535static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001537 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1539 return 0;
1540}
1541
Takashi Iwai208a1b42005-11-17 14:53:41 +01001542static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001544 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int change;
1546 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1547 if (change)
1548 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1549 return change;
1550}
1551
1552
Takashi Iwai208a1b42005-11-17 14:53:41 +01001553static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1555YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1556YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1557YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1558YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1559YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1560YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1561YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1562YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1563YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1564YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1565YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1566YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001567YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1568YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1569YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1572 .name = "4ch Duplication",
1573 .info = snd_ymfpci_info_dup4ch,
1574 .get = snd_ymfpci_get_dup4ch,
1575 .put = snd_ymfpci_put_dup4ch,
1576},
1577};
1578
1579
1580/*
1581 * GPIO
1582 */
1583
Takashi Iwai208a1b42005-11-17 14:53:41 +01001584static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 u16 reg, mode;
1587 unsigned long flags;
1588
1589 spin_lock_irqsave(&chip->reg_lock, flags);
1590 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1591 reg &= ~(1 << (pin + 8));
1592 reg |= (1 << pin);
1593 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1594 /* set the level mode for input line */
1595 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1596 mode &= ~(3 << (pin * 2));
1597 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1598 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1599 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1600 spin_unlock_irqrestore(&chip->reg_lock, flags);
1601 return (mode >> pin) & 1;
1602}
1603
Takashi Iwai208a1b42005-11-17 14:53:41 +01001604static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 u16 reg;
1607 unsigned long flags;
1608
1609 spin_lock_irqsave(&chip->reg_lock, flags);
1610 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1611 reg &= ~(1 << pin);
1612 reg &= ~(1 << (pin + 8));
1613 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1614 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1615 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1616 spin_unlock_irqrestore(&chip->reg_lock, flags);
1617
1618 return 0;
1619}
1620
Takashi Iwai208a1b42005-11-17 14:53:41 +01001621static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1624 uinfo->count = 1;
1625 uinfo->value.integer.min = 0;
1626 uinfo->value.integer.max = 1;
1627 return 0;
1628}
1629
Takashi Iwai208a1b42005-11-17 14:53:41 +01001630static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001632 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int pin = (int)kcontrol->private_value;
1634 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1635 return 0;
1636}
1637
Takashi Iwai208a1b42005-11-17 14:53:41 +01001638static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001640 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int pin = (int)kcontrol->private_value;
1642
1643 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1644 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1645 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1646 return 1;
1647 }
1648 return 0;
1649}
1650
Takashi Iwai208a1b42005-11-17 14:53:41 +01001651static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .name = "Shared Rear/Line-In Switch",
1653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1654 .info = snd_ymfpci_gpio_sw_info,
1655 .get = snd_ymfpci_gpio_sw_get,
1656 .put = snd_ymfpci_gpio_sw_put,
1657 .private_value = 2,
1658};
1659
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001660/*
1661 * PCM voice volume
1662 */
1663
Takashi Iwai208a1b42005-11-17 14:53:41 +01001664static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001666{
1667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1668 uinfo->count = 2;
1669 uinfo->value.integer.min = 0;
1670 uinfo->value.integer.max = 0x8000;
1671 return 0;
1672}
1673
Takashi Iwai208a1b42005-11-17 14:53:41 +01001674static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001676{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001677 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001678 unsigned int subs = kcontrol->id.subdevice;
1679
1680 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1681 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1682 return 0;
1683}
1684
Takashi Iwai208a1b42005-11-17 14:53:41 +01001685static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1686 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001687{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001688 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001689 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001690 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001691 unsigned long flags;
1692
1693 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1694 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1695 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1696 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1697
Takashi Iwai208a1b42005-11-17 14:53:41 +01001698 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001699 spin_lock_irqsave(&chip->voice_lock, flags);
1700 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001701 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001702 ypcm->update_pcm_vol = 2;
1703 }
1704 spin_unlock_irqrestore(&chip->voice_lock, flags);
1705 return 1;
1706 }
1707 return 0;
1708}
1709
Takashi Iwai208a1b42005-11-17 14:53:41 +01001710static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001711 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1712 .name = "PCM Playback Volume",
1713 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1714 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1715 .info = snd_ymfpci_pcm_vol_info,
1716 .get = snd_ymfpci_pcm_vol_get,
1717 .put = snd_ymfpci_pcm_vol_put,
1718};
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/*
1722 * Mixer routines
1723 */
1724
Takashi Iwai208a1b42005-11-17 14:53:41 +01001725static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001727 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 chip->ac97_bus = NULL;
1729}
1730
Takashi Iwai208a1b42005-11-17 14:53:41 +01001731static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001733 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 chip->ac97 = NULL;
1735}
1736
Takashi Iwai208a1b42005-11-17 14:53:41 +01001737int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001739 struct snd_ac97_template ac97;
1740 struct snd_kcontrol *kctl;
1741 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int idx;
1743 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001744 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .write = snd_ymfpci_codec_write,
1746 .read = snd_ymfpci_codec_read,
1747 };
1748
1749 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1750 return err;
1751 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1752 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1753
1754 memset(&ac97, 0, sizeof(ac97));
1755 ac97.private_data = chip;
1756 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1757 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1758 return err;
1759
1760 /* to be sure */
1761 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1762 AC97_EA_VRA|AC97_EA_VRM, 0);
1763
1764 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1765 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1766 return err;
1767 }
1768
1769 /* add S/PDIF control */
1770 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1771 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1772 return err;
1773 kctl->id.device = chip->pcm_spdif->device;
1774 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1775 return err;
1776 kctl->id.device = chip->pcm_spdif->device;
1777 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1778 return err;
1779 kctl->id.device = chip->pcm_spdif->device;
1780 chip->spdif_pcm_ctl = kctl;
1781
1782 /* direct recording source */
1783 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1784 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1785 return err;
1786
1787 /*
1788 * shared rear/line-in
1789 */
1790 if (rear_switch) {
1791 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1792 return err;
1793 }
1794
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001795 /* per-voice volume */
1796 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1797 for (idx = 0; idx < 32; ++idx) {
1798 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1799 if (!kctl)
1800 return -ENOMEM;
1801 kctl->id.device = chip->pcm->device;
1802 kctl->id.subdevice = idx;
1803 kctl->private_value = (unsigned long)substream;
1804 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1805 return err;
1806 chip->pcm_mixer[idx].left = 0x8000;
1807 chip->pcm_mixer[idx].right = 0x8000;
1808 chip->pcm_mixer[idx].ctl = kctl;
1809 substream = substream->next;
1810 }
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814
1815
1816/*
1817 * timer
1818 */
1819
Takashi Iwai208a1b42005-11-17 14:53:41 +01001820static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001822 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned long flags;
1824 unsigned int count;
1825
1826 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001827 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 spin_lock_irqsave(&chip->reg_lock, flags);
1829 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1830 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1831 spin_unlock_irqrestore(&chip->reg_lock, flags);
1832 return 0;
1833}
1834
Takashi Iwai208a1b42005-11-17 14:53:41 +01001835static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001837 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 unsigned long flags;
1839
1840 chip = snd_timer_chip(timer);
1841 spin_lock_irqsave(&chip->reg_lock, flags);
1842 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1843 spin_unlock_irqrestore(&chip->reg_lock, flags);
1844 return 0;
1845}
1846
Takashi Iwai208a1b42005-11-17 14:53:41 +01001847static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 unsigned long *num, unsigned long *den)
1849{
1850 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001851 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853}
1854
Takashi Iwai208a1b42005-11-17 14:53:41 +01001855static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001857 .resolution = 20833, /* 1/fs = 20.8333...us */
1858 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .start = snd_ymfpci_timer_start,
1860 .stop = snd_ymfpci_timer_stop,
1861 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1862};
1863
Takashi Iwai208a1b42005-11-17 14:53:41 +01001864int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001866 struct snd_timer *timer = NULL;
1867 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int err;
1869
1870 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1871 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1872 tid.card = chip->card->number;
1873 tid.device = device;
1874 tid.subdevice = 0;
1875 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1876 strcpy(timer->name, "YMFPCI timer");
1877 timer->private_data = chip;
1878 timer->hw = snd_ymfpci_timer_hw;
1879 }
1880 chip->timer = timer;
1881 return err;
1882}
1883
1884
1885/*
1886 * proc interface
1887 */
1888
Takashi Iwai208a1b42005-11-17 14:53:41 +01001889static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1890 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001892 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int i;
1894
1895 snd_iprintf(buffer, "YMFPCI\n\n");
1896 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1897 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1898}
1899
Takashi Iwai208a1b42005-11-17 14:53:41 +01001900static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001902 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (! snd_card_proc_new(card, "ymfpci", &entry))
1905 snd_info_set_text_ops(entry, chip, 1024, snd_ymfpci_proc_read);
1906 return 0;
1907}
1908
1909/*
1910 * initialization routines
1911 */
1912
1913static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1914{
1915 u8 cmd;
1916
1917 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1918#if 0 // force to reset
1919 if (cmd & 0x03) {
1920#endif
1921 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1922 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1923 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1924 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1925 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1926#if 0
1927 }
1928#endif
1929}
1930
Takashi Iwai208a1b42005-11-17 14:53:41 +01001931static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1934}
1935
Takashi Iwai208a1b42005-11-17 14:53:41 +01001936static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 u32 val;
1939 int timeout = 1000;
1940
1941 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1942 if (val)
1943 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1944 while (timeout-- > 0) {
1945 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1946 if ((val & 0x00000002) == 0)
1947 break;
1948 }
1949}
1950
1951#include "ymfpci_image.h"
1952
Takashi Iwai208a1b42005-11-17 14:53:41 +01001953static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 int i;
1956 u16 ctrl;
1957 unsigned long *inst;
1958
1959 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
1960 snd_ymfpci_disable_dsp(chip);
1961 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
1962 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
1963 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
1964 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
1965 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
1966 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
1967 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
1968 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1969 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
1970
1971 /* setup DSP instruction code */
1972 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
1973 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
1974
1975 /* setup control instruction code */
1976 switch (chip->device_id) {
1977 case PCI_DEVICE_ID_YAMAHA_724F:
1978 case PCI_DEVICE_ID_YAMAHA_740C:
1979 case PCI_DEVICE_ID_YAMAHA_744:
1980 case PCI_DEVICE_ID_YAMAHA_754:
1981 inst = CntrlInst1E;
1982 break;
1983 default:
1984 inst = CntrlInst;
1985 break;
1986 }
1987 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
1988 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
1989
1990 snd_ymfpci_enable_dsp(chip);
1991}
1992
Takashi Iwai208a1b42005-11-17 14:53:41 +01001993static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 long size, playback_ctrl_size;
1996 int voice, bank, reg;
1997 u8 *ptr;
1998 dma_addr_t ptr_addr;
1999
2000 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2001 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2002 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2003 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2004 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2005
2006 size = ((playback_ctrl_size + 0x00ff) & ~0x00ff) +
2007 ((chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES + 0x00ff) & ~0x00ff) +
2008 ((chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES + 0x00ff) & ~0x00ff) +
2009 ((chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES + 0x00ff) & ~0x00ff) +
2010 chip->work_size;
2011 /* work_ptr must be aligned to 256 bytes, but it's already
2012 covered with the kernel page allocation mechanism */
2013 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2014 size, &chip->work_ptr) < 0)
2015 return -ENOMEM;
2016 ptr = chip->work_ptr.area;
2017 ptr_addr = chip->work_ptr.addr;
2018 memset(ptr, 0, size); /* for sure */
2019
2020 chip->bank_base_playback = ptr;
2021 chip->bank_base_playback_addr = ptr_addr;
2022 chip->ctrl_playback = (u32 *)ptr;
2023 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2024 ptr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2025 ptr_addr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2026 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2027 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002028 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 chip->voices[voice].bank_addr = ptr_addr;
2030 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002031 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 ptr += chip->bank_size_playback;
2033 ptr_addr += chip->bank_size_playback;
2034 }
2035 }
2036 ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
2037 ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
2038 chip->bank_base_capture = ptr;
2039 chip->bank_base_capture_addr = ptr_addr;
2040 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2041 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002042 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 ptr += chip->bank_size_capture;
2044 ptr_addr += chip->bank_size_capture;
2045 }
2046 ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
2047 ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
2048 chip->bank_base_effect = ptr;
2049 chip->bank_base_effect_addr = ptr_addr;
2050 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2051 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002052 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 ptr += chip->bank_size_effect;
2054 ptr_addr += chip->bank_size_effect;
2055 }
2056 ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
2057 ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
2058 chip->work_base = ptr;
2059 chip->work_base_addr = ptr_addr;
2060
2061 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2062
2063 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2064 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2065 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2066 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2067 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2068
2069 /* S/PDIF output initialization */
2070 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2071 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2072 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2073
2074 /* S/PDIF input initialization */
2075 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2076
2077 /* digital mixer setup */
2078 for (reg = 0x80; reg < 0xc0; reg += 4)
2079 snd_ymfpci_writel(chip, reg, 0);
2080 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2081 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2082 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2083 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2084 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2085 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2086 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2087
2088 return 0;
2089}
2090
Takashi Iwai208a1b42005-11-17 14:53:41 +01002091static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 u16 ctrl;
2094
2095 snd_assert(chip != NULL, return -EINVAL);
2096
2097 if (chip->res_reg_area) { /* don't touch busy hardware */
2098 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2099 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2100 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2101 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2102 snd_ymfpci_disable_dsp(chip);
2103 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2104 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2105 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2106 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2107 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2108 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2109 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2110 }
2111
2112 snd_ymfpci_ac3_done(chip);
2113
2114 /* Set PCI device to D3 state */
2115#if 0
2116 /* FIXME: temporarily disabled, otherwise we cannot fire up
2117 * the chip again unless reboot. ACPI bug?
2118 */
2119 pci_set_power_state(chip->pci, 3);
2120#endif
2121
2122#ifdef CONFIG_PM
2123 vfree(chip->saved_regs);
2124#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +02002125 release_and_free_resource(chip->mpu_res);
2126 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 snd_ymfpci_free_gameport(chip);
2128 if (chip->reg_area_virt)
2129 iounmap(chip->reg_area_virt);
2130 if (chip->work_ptr.area)
2131 snd_dma_free_pages(&chip->work_ptr);
2132
2133 if (chip->irq >= 0)
2134 free_irq(chip->irq, (void *)chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002135 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2138
2139 pci_disable_device(chip->pci);
2140 kfree(chip);
2141 return 0;
2142}
2143
Takashi Iwai208a1b42005-11-17 14:53:41 +01002144static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002146 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return snd_ymfpci_free(chip);
2148}
2149
2150#ifdef CONFIG_PM
2151static int saved_regs_index[] = {
2152 /* spdif */
2153 YDSXGR_SPDIFOUTCTRL,
2154 YDSXGR_SPDIFOUTSTATUS,
2155 YDSXGR_SPDIFINCTRL,
2156 /* volumes */
2157 YDSXGR_PRIADCLOOPVOL,
2158 YDSXGR_NATIVEDACINVOL,
2159 YDSXGR_NATIVEDACOUTVOL,
2160 // YDSXGR_BUF441OUTVOL,
2161 YDSXGR_NATIVEADCINVOL,
2162 YDSXGR_SPDIFLOOPVOL,
2163 YDSXGR_SPDIFOUTVOL,
2164 YDSXGR_ZVOUTVOL,
2165 YDSXGR_LEGACYOUTVOL,
2166 /* address bases */
2167 YDSXGR_PLAYCTRLBASE,
2168 YDSXGR_RECCTRLBASE,
2169 YDSXGR_EFFCTRLBASE,
2170 YDSXGR_WORKBASE,
2171 /* capture set up */
2172 YDSXGR_MAPOFREC,
2173 YDSXGR_RECFORMAT,
2174 YDSXGR_RECSLOTSR,
2175 YDSXGR_ADCFORMAT,
2176 YDSXGR_ADCSLOTSR,
2177};
2178#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2179
Takashi Iwaided46232005-11-17 16:09:43 +01002180int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Takashi Iwaided46232005-11-17 16:09:43 +01002182 struct snd_card *card = pci_get_drvdata(pci);
2183 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 unsigned int i;
2185
Takashi Iwaided46232005-11-17 16:09:43 +01002186 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 snd_pcm_suspend_all(chip->pcm);
2188 snd_pcm_suspend_all(chip->pcm2);
2189 snd_pcm_suspend_all(chip->pcm_spdif);
2190 snd_pcm_suspend_all(chip->pcm_4ch);
2191 snd_ac97_suspend(chip->ac97);
2192 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2193 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2194 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2195 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2196 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002197 pci_disable_device(pci);
2198 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200}
2201
Takashi Iwaided46232005-11-17 16:09:43 +01002202int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Takashi Iwaided46232005-11-17 16:09:43 +01002204 struct snd_card *card = pci_get_drvdata(pci);
2205 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 unsigned int i;
2207
Takashi Iwaided46232005-11-17 16:09:43 +01002208 pci_restore_state(pci);
2209 pci_enable_device(pci);
2210 pci_set_master(pci);
2211 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_ymfpci_codec_ready(chip, 0);
2213 snd_ymfpci_download_image(chip);
2214 udelay(100);
2215
2216 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2217 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2218
2219 snd_ac97_resume(chip->ac97);
2220
2221 /* start hw again */
2222 if (chip->start_count > 0) {
2223 spin_lock_irq(&chip->reg_lock);
2224 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2225 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2226 spin_unlock_irq(&chip->reg_lock);
2227 }
Takashi Iwaided46232005-11-17 16:09:43 +01002228 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
2230}
2231#endif /* CONFIG_PM */
2232
Takashi Iwai208a1b42005-11-17 14:53:41 +01002233int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct pci_dev * pci,
2235 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002236 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002238 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002240 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 .dev_free = snd_ymfpci_dev_free,
2242 };
2243
2244 *rchip = NULL;
2245
2246 /* enable PCI device */
2247 if ((err = pci_enable_device(pci)) < 0)
2248 return err;
2249
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002250 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (chip == NULL) {
2252 pci_disable_device(pci);
2253 return -ENOMEM;
2254 }
2255 chip->old_legacy_ctrl = old_legacy_ctrl;
2256 spin_lock_init(&chip->reg_lock);
2257 spin_lock_init(&chip->voice_lock);
2258 init_waitqueue_head(&chip->interrupt_sleep);
2259 atomic_set(&chip->interrupt_sleep_count, 0);
2260 chip->card = card;
2261 chip->pci = pci;
2262 chip->irq = -1;
2263 chip->device_id = pci->device;
2264 pci_read_config_byte(pci, PCI_REVISION_ID, (u8 *)&chip->rev);
2265 chip->reg_area_phys = pci_resource_start(pci, 0);
2266 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2267 pci_set_master(pci);
2268
2269 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002270 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 snd_ymfpci_free(chip);
2272 return -EBUSY;
2273 }
2274 if (request_irq(pci->irq, snd_ymfpci_interrupt, SA_INTERRUPT|SA_SHIRQ, "YMFPCI", (void *) chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002275 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 snd_ymfpci_free(chip);
2277 return -EBUSY;
2278 }
2279 chip->irq = pci->irq;
2280
2281 snd_ymfpci_aclink_reset(pci);
2282 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2283 snd_ymfpci_free(chip);
2284 return -EIO;
2285 }
2286
2287 snd_ymfpci_download_image(chip);
2288
2289 udelay(100); /* seems we need a delay after downloading image.. */
2290
2291 if (snd_ymfpci_memalloc(chip) < 0) {
2292 snd_ymfpci_free(chip);
2293 return -EIO;
2294 }
2295
2296 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2297 snd_ymfpci_free(chip);
2298 return err;
2299 }
2300
2301#ifdef CONFIG_PM
2302 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2303 if (chip->saved_regs == NULL) {
2304 snd_ymfpci_free(chip);
2305 return -ENOMEM;
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307#endif
2308
2309 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2310 snd_ymfpci_free(chip);
2311 return err;
2312 }
2313
2314 snd_ymfpci_proc_init(card, chip);
2315
2316 snd_card_set_dev(card, &pci->dev);
2317
2318 *rchip = chip;
2319 return 0;
2320}