blob: 7fa57c9ecc5f5109bc9a7568f5556d687ddb1734 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <sound/driver.h>
22#include <linux/delay.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020023#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/vmalloc.h>
30
31#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/ymfpci.h>
36#include <sound/asoundef.h>
37#include <sound/mpu401.h>
38
39#include <asm/io.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020040#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * common I/O routines
44 */
45
Takashi Iwai208a1b42005-11-17 14:53:41 +010046static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai208a1b42005-11-17 14:53:41 +010048static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return readb(chip->reg_area_virt + offset);
51}
52
Takashi Iwai208a1b42005-11-17 14:53:41 +010053static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 writeb(val, chip->reg_area_virt + offset);
56}
57
Takashi Iwai208a1b42005-11-17 14:53:41 +010058static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return readw(chip->reg_area_virt + offset);
61}
62
Takashi Iwai208a1b42005-11-17 14:53:41 +010063static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 writew(val, chip->reg_area_virt + offset);
66}
67
Takashi Iwai208a1b42005-11-17 14:53:41 +010068static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return readl(chip->reg_area_virt + offset);
71}
72
Takashi Iwai208a1b42005-11-17 14:53:41 +010073static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 writel(val, chip->reg_area_virt + offset);
76}
77
Takashi Iwai208a1b42005-11-17 14:53:41 +010078static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020080 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020083 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 do {
85 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
86 return 0;
87 set_current_state(TASK_UNINTERRUPTIBLE);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020088 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020089 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020090 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 -070091 return -EBUSY;
92}
93
Takashi Iwai208a1b42005-11-17 14:53:41 +010094static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Takashi Iwai208a1b42005-11-17 14:53:41 +010096 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 u32 cmd;
98
99 snd_ymfpci_codec_ready(chip, 0);
100 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
101 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
102}
103
Takashi Iwai208a1b42005-11-17 14:53:41 +0100104static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100106 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (snd_ymfpci_codec_ready(chip, 0))
109 return ~0;
110 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
111 if (snd_ymfpci_codec_ready(chip, 0))
112 return ~0;
113 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
114 int i;
115 for (i = 0; i < 600; i++)
116 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117 }
118 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
119}
120
121/*
122 * Misc routines
123 */
124
125static u32 snd_ymfpci_calc_delta(u32 rate)
126{
127 switch (rate) {
128 case 8000: return 0x02aaab00;
129 case 11025: return 0x03accd00;
130 case 16000: return 0x05555500;
131 case 22050: return 0x07599a00;
132 case 32000: return 0x0aaaab00;
133 case 44100: return 0x0eb33300;
134 default: return ((rate << 16) / 375) << 5;
135 }
136}
137
138static u32 def_rate[8] = {
139 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
140};
141
142static u32 snd_ymfpci_calc_lpfK(u32 rate)
143{
144 u32 i;
145 static u32 val[8] = {
146 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
147 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
148 };
149
150 if (rate == 44100)
151 return 0x40000000; /* FIXME: What's the right value? */
152 for (i = 0; i < 8; i++)
153 if (rate <= def_rate[i])
154 return val[i];
155 return val[0];
156}
157
158static u32 snd_ymfpci_calc_lpfQ(u32 rate)
159{
160 u32 i;
161 static u32 val[8] = {
162 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
163 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
164 };
165
166 if (rate == 44100)
167 return 0x370A0000;
168 for (i = 0; i < 8; i++)
169 if (rate <= def_rate[i])
170 return val[i];
171 return val[0];
172}
173
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100174static void snd_ymfpci_pcm_441_volume_set(struct snd_ymfpci_pcm *ypcm)
175{
176 unsigned int value;
177 struct snd_ymfpci_pcm_mixer *mixer;
178
179 mixer = &ypcm->chip->pcm_mixer[ypcm->substream->number];
180 value = min_t(unsigned int, mixer->left, 0x7fff) >> 1;
181 value |= (min_t(unsigned int, mixer->right, 0x7fff) >> 1) << 16;
182 snd_ymfpci_writel(ypcm->chip, YDSXGR_BUF441OUTVOL, value);
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * Hardware start management
187 */
188
Takashi Iwai208a1b42005-11-17 14:53:41 +0100189static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 unsigned long flags;
192
193 spin_lock_irqsave(&chip->reg_lock, flags);
194 if (chip->start_count++ > 0)
195 goto __end;
196 snd_ymfpci_writel(chip, YDSXGR_MODE,
197 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
198 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
199 __end:
200 spin_unlock_irqrestore(&chip->reg_lock, flags);
201}
202
Takashi Iwai208a1b42005-11-17 14:53:41 +0100203static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned long flags;
206 long timeout = 1000;
207
208 spin_lock_irqsave(&chip->reg_lock, flags);
209 if (--chip->start_count > 0)
210 goto __end;
211 snd_ymfpci_writel(chip, YDSXGR_MODE,
212 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
213 while (timeout-- > 0) {
214 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
215 break;
216 }
217 if (atomic_read(&chip->interrupt_sleep_count)) {
218 atomic_set(&chip->interrupt_sleep_count, 0);
219 wake_up(&chip->interrupt_sleep);
220 }
221 __end:
222 spin_unlock_irqrestore(&chip->reg_lock, flags);
223}
224
225/*
226 * Playback voice management
227 */
228
Takashi Iwai208a1b42005-11-17 14:53:41 +0100229static int voice_alloc(struct snd_ymfpci *chip,
230 enum snd_ymfpci_voice_type type, int pair,
231 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100233 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int idx;
235
236 *rvoice = NULL;
237 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
238 voice = &chip->voices[idx];
239 voice2 = pair ? &chip->voices[idx+1] : NULL;
240 if (voice->use || (voice2 && voice2->use))
241 continue;
242 voice->use = 1;
243 if (voice2)
244 voice2->use = 1;
245 switch (type) {
246 case YMFPCI_PCM:
247 voice->pcm = 1;
248 if (voice2)
249 voice2->pcm = 1;
250 break;
251 case YMFPCI_SYNTH:
252 voice->synth = 1;
253 break;
254 case YMFPCI_MIDI:
255 voice->midi = 1;
256 break;
257 }
258 snd_ymfpci_hw_start(chip);
259 if (voice2)
260 snd_ymfpci_hw_start(chip);
261 *rvoice = voice;
262 return 0;
263 }
264 return -ENOMEM;
265}
266
Takashi Iwai208a1b42005-11-17 14:53:41 +0100267static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
268 enum snd_ymfpci_voice_type type, int pair,
269 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned long flags;
272 int result;
273
274 snd_assert(rvoice != NULL, return -EINVAL);
275 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
276
277 spin_lock_irqsave(&chip->voice_lock, flags);
278 for (;;) {
279 result = voice_alloc(chip, type, pair, rvoice);
280 if (result == 0 || type != YMFPCI_PCM)
281 break;
282 /* TODO: synth/midi voice deallocation */
283 break;
284 }
285 spin_unlock_irqrestore(&chip->voice_lock, flags);
286 return result;
287}
288
Takashi Iwai208a1b42005-11-17 14:53:41 +0100289static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long flags;
292
293 snd_assert(pvoice != NULL, return -EINVAL);
294 snd_ymfpci_hw_stop(chip);
295 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100296 if (pvoice->number == chip->src441_used) {
297 chip->src441_used = -1;
298 pvoice->ypcm->use_441_slot = 0;
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
301 pvoice->ypcm = NULL;
302 pvoice->interrupt = NULL;
303 spin_unlock_irqrestore(&chip->voice_lock, flags);
304 return 0;
305}
306
307/*
308 * PCM part
309 */
310
Takashi Iwai208a1b42005-11-17 14:53:41 +0100311static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100313 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u32 pos, delta;
315
316 if ((ypcm = voice->ypcm) == NULL)
317 return;
318 if (ypcm->substream == NULL)
319 return;
320 spin_lock(&chip->reg_lock);
321 if (ypcm->running) {
322 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
323 if (pos < ypcm->last_pos)
324 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
325 else
326 delta = pos - ypcm->last_pos;
327 ypcm->period_pos += delta;
328 ypcm->last_pos = pos;
329 if (ypcm->period_pos >= ypcm->period_size) {
330 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
331 ypcm->period_pos %= ypcm->period_size;
332 spin_unlock(&chip->reg_lock);
333 snd_pcm_period_elapsed(ypcm->substream);
334 spin_lock(&chip->reg_lock);
335 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200336
337 if (unlikely(ypcm->update_pcm_vol)) {
338 unsigned int subs = ypcm->substream->number;
339 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100340 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200341 u32 volume;
342
343 bank = &voice->bank[next_bank];
344 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
345 bank->left_gain_end = volume;
346 if (ypcm->output_rear)
347 bank->eff2_gain_end = volume;
348 if (ypcm->voices[1])
349 bank = &ypcm->voices[1]->bank[next_bank];
350 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
351 bank->right_gain_end = volume;
352 if (ypcm->output_rear)
353 bank->eff3_gain_end = volume;
354 ypcm->update_pcm_vol--;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 spin_unlock(&chip->reg_lock);
358}
359
Takashi Iwai208a1b42005-11-17 14:53:41 +0100360static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100362 struct snd_pcm_runtime *runtime = substream->runtime;
363 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
364 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 u32 pos, delta;
366
367 spin_lock(&chip->reg_lock);
368 if (ypcm->running) {
369 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
370 if (pos < ypcm->last_pos)
371 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
372 else
373 delta = pos - ypcm->last_pos;
374 ypcm->period_pos += delta;
375 ypcm->last_pos = pos;
376 if (ypcm->period_pos >= ypcm->period_size) {
377 ypcm->period_pos %= ypcm->period_size;
378 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
379 spin_unlock(&chip->reg_lock);
380 snd_pcm_period_elapsed(substream);
381 spin_lock(&chip->reg_lock);
382 }
383 }
384 spin_unlock(&chip->reg_lock);
385}
386
Takashi Iwai208a1b42005-11-17 14:53:41 +0100387static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int cmd)
389{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100390 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
391 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int result = 0;
393
394 spin_lock(&chip->reg_lock);
395 if (ypcm->voices[0] == NULL) {
396 result = -EINVAL;
397 goto __unlock;
398 }
399 switch (cmd) {
400 case SNDRV_PCM_TRIGGER_START:
401 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
402 case SNDRV_PCM_TRIGGER_RESUME:
403 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100404 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
406 ypcm->running = 1;
407 break;
408 case SNDRV_PCM_TRIGGER_STOP:
409 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
410 case SNDRV_PCM_TRIGGER_SUSPEND:
411 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100412 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
414 ypcm->running = 0;
415 break;
416 default:
417 result = -EINVAL;
418 break;
419 }
420 __unlock:
421 spin_unlock(&chip->reg_lock);
422 return result;
423}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100424static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int cmd)
426{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100427 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
428 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int result = 0;
430 u32 tmp;
431
432 spin_lock(&chip->reg_lock);
433 switch (cmd) {
434 case SNDRV_PCM_TRIGGER_START:
435 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
436 case SNDRV_PCM_TRIGGER_RESUME:
437 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
438 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
439 ypcm->running = 1;
440 break;
441 case SNDRV_PCM_TRIGGER_STOP:
442 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
443 case SNDRV_PCM_TRIGGER_SUSPEND:
444 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
445 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
446 ypcm->running = 0;
447 break;
448 default:
449 result = -EINVAL;
450 break;
451 }
452 spin_unlock(&chip->reg_lock);
453 return result;
454}
455
Takashi Iwai208a1b42005-11-17 14:53:41 +0100456static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int err;
459
460 if (ypcm->voices[1] != NULL && voices < 2) {
461 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
462 ypcm->voices[1] = NULL;
463 }
464 if (voices == 1 && ypcm->voices[0] != NULL)
465 return 0; /* already allocated */
466 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
467 return 0; /* already allocated */
468 if (voices > 1) {
469 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
470 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
471 ypcm->voices[0] = NULL;
472 }
473 }
474 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
475 if (err < 0)
476 return err;
477 ypcm->voices[0]->ypcm = ypcm;
478 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
479 if (voices > 1) {
480 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
481 ypcm->voices[1]->ypcm = ypcm;
482 }
483 return 0;
484}
485
Takashi Iwai208a1b42005-11-17 14:53:41 +0100486static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
487 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200488 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100490 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200492 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
493 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
494 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100495 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200497 u32 vol_left, vol_right;
498 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200502 if (runtime->channels == 1) {
503 use_left = 1;
504 use_right = 1;
505 } else {
506 use_left = (voiceidx & 1) == 0;
507 use_right = !use_left;
508 }
509 if (has_pcm_volume) {
510 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
511 [ypcm->substream->number].left << 15);
512 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
513 [ypcm->substream->number].right << 15);
514 } else {
515 vol_left = cpu_to_le32(0x40000000);
516 vol_right = cpu_to_le32(0x40000000);
517 }
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100518 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200519 format = runtime->channels == 2 ? 0x00010000 : 0;
520 if (snd_pcm_format_width(runtime->format) == 8)
521 format |= 0x80000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100522 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
523 runtime->rate == 44100 && runtime->channels == 2 &&
524 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
525 ypcm->chip->src441_used == voice->number)) {
526 ypcm->chip->src441_used = voice->number;
527 ypcm->use_441_slot = 1;
528 format |= 0x10000000;
529 snd_ymfpci_pcm_441_volume_set(ypcm);
530 }
531 if (ypcm->chip->src441_used == voice->number &&
532 (format & 0x10000000) == 0) {
533 ypcm->chip->src441_used = -1;
534 ypcm->use_441_slot = 0;
535 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200536 if (runtime->channels == 2 && (voiceidx & 1) != 0)
537 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100538 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 for (nbank = 0; nbank < 2; nbank++) {
540 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200541 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200543 bank->base = cpu_to_le32(runtime->dma_addr);
544 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 bank->delta =
547 bank->delta_end = cpu_to_le32(delta);
548 bank->lpfK =
549 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200550 bank->eg_gain =
551 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200553 if (ypcm->output_front) {
554 if (use_left) {
555 bank->left_gain =
556 bank->left_gain_end = vol_left;
557 }
558 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200560 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200562 }
563 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100564 if (!ypcm->swap_rear) {
565 if (use_left) {
566 bank->eff2_gain =
567 bank->eff2_gain_end = vol_left;
568 }
569 if (use_right) {
570 bank->eff3_gain =
571 bank->eff3_gain_end = vol_right;
572 }
573 } else {
574 /* The SPDIF out channels seem to be swapped, so we have
575 * to swap them here, too. The rear analog out channels
576 * will be wrong, but otherwise AC3 would not work.
577 */
578 if (use_left) {
579 bank->eff3_gain =
580 bank->eff3_gain_end = vol_left;
581 }
582 if (use_right) {
583 bank->eff2_gain =
584 bank->eff2_gain_end = vol_right;
585 }
586 }
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589}
590
Takashi Iwai208a1b42005-11-17 14:53:41 +0100591static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
594 4096, &chip->ac3_tmp_base) < 0)
595 return -ENOMEM;
596
597 chip->bank_effect[3][0]->base =
598 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
599 chip->bank_effect[3][0]->loop_end =
600 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
601 chip->bank_effect[4][0]->base =
602 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
603 chip->bank_effect[4][0]->loop_end =
604 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
605
606 spin_lock_irq(&chip->reg_lock);
607 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
608 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
609 spin_unlock_irq(&chip->reg_lock);
610 return 0;
611}
612
Takashi Iwai208a1b42005-11-17 14:53:41 +0100613static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 spin_lock_irq(&chip->reg_lock);
616 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
617 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
618 spin_unlock_irq(&chip->reg_lock);
619 // snd_ymfpci_irq_wait(chip);
620 if (chip->ac3_tmp_base.area) {
621 snd_dma_free_pages(&chip->ac3_tmp_base);
622 chip->ac3_tmp_base.area = NULL;
623 }
624 return 0;
625}
626
Takashi Iwai208a1b42005-11-17 14:53:41 +0100627static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
628 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100630 struct snd_pcm_runtime *runtime = substream->runtime;
631 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int err;
633
634 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
635 return err;
636 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
637 return err;
638 return 0;
639}
640
Takashi Iwai208a1b42005-11-17 14:53:41 +0100641static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100643 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
644 struct snd_pcm_runtime *runtime = substream->runtime;
645 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (runtime->private_data == NULL)
648 return 0;
649 ypcm = runtime->private_data;
650
651 /* wait, until the PCI operations are not finished */
652 snd_ymfpci_irq_wait(chip);
653 snd_pcm_lib_free_pages(substream);
654 if (ypcm->voices[1]) {
655 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
656 ypcm->voices[1] = NULL;
657 }
658 if (ypcm->voices[0]) {
659 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
660 ypcm->voices[0] = NULL;
661 }
662 return 0;
663}
664
Takashi Iwai208a1b42005-11-17 14:53:41 +0100665static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100667 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
668 struct snd_pcm_runtime *runtime = substream->runtime;
669 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned int nvoice;
671
672 ypcm->period_size = runtime->period_size;
673 ypcm->buffer_size = runtime->buffer_size;
674 ypcm->period_pos = 0;
675 ypcm->last_pos = 0;
676 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200677 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
678 substream->pcm == chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680}
681
Takashi Iwai208a1b42005-11-17 14:53:41 +0100682static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
683 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
686}
687
Takashi Iwai208a1b42005-11-17 14:53:41 +0100688static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100690 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* wait, until the PCI operations are not finished */
693 snd_ymfpci_irq_wait(chip);
694 return snd_pcm_lib_free_pages(substream);
695}
696
Takashi Iwai208a1b42005-11-17 14:53:41 +0100697static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100699 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
700 struct snd_pcm_runtime *runtime = substream->runtime;
701 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
702 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int nbank;
704 u32 rate, format;
705
706 ypcm->period_size = runtime->period_size;
707 ypcm->buffer_size = runtime->buffer_size;
708 ypcm->period_pos = 0;
709 ypcm->last_pos = 0;
710 ypcm->shift = 0;
711 rate = ((48000 * 4096) / runtime->rate) - 1;
712 format = 0;
713 if (runtime->channels == 2) {
714 format |= 2;
715 ypcm->shift++;
716 }
717 if (snd_pcm_format_width(runtime->format) == 8)
718 format |= 1;
719 else
720 ypcm->shift++;
721 switch (ypcm->capture_bank_number) {
722 case 0:
723 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
724 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
725 break;
726 case 1:
727 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
728 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
729 break;
730 }
731 for (nbank = 0; nbank < 2; nbank++) {
732 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
733 bank->base = cpu_to_le32(runtime->dma_addr);
734 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
735 bank->start = 0;
736 bank->num_of_loops = 0;
737 }
738 return 0;
739}
740
Takashi Iwai208a1b42005-11-17 14:53:41 +0100741static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100743 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
744 struct snd_pcm_runtime *runtime = substream->runtime;
745 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
746 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (!(ypcm->running && voice))
749 return 0;
750 return le32_to_cpu(voice->bank[chip->active_bank].start);
751}
752
Takashi Iwai208a1b42005-11-17 14:53:41 +0100753static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100755 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
756 struct snd_pcm_runtime *runtime = substream->runtime;
757 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (!ypcm->running)
760 return 0;
761 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
762}
763
Takashi Iwai208a1b42005-11-17 14:53:41 +0100764static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 wait_queue_t wait;
767 int loops = 4;
768
769 while (loops-- > 0) {
770 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
771 continue;
772 init_waitqueue_entry(&wait, current);
773 add_wait_queue(&chip->interrupt_sleep, &wait);
774 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200775 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 remove_wait_queue(&chip->interrupt_sleep, &wait);
777 }
778}
779
David Howells7d12e782006-10-05 14:55:46 +0100780static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100782 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100784 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
787 if (status & 0x80000000) {
788 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
789 spin_lock(&chip->voice_lock);
790 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
791 voice = &chip->voices[nvoice];
792 if (voice->interrupt)
793 voice->interrupt(chip, voice);
794 }
795 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
796 if (chip->capture_substream[nvoice])
797 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
798 }
799#if 0
800 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
801 if (chip->effect_substream[nvoice])
802 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
803 }
804#endif
805 spin_unlock(&chip->voice_lock);
806 spin_lock(&chip->reg_lock);
807 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
808 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
809 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
810 spin_unlock(&chip->reg_lock);
811
812 if (atomic_read(&chip->interrupt_sleep_count)) {
813 atomic_set(&chip->interrupt_sleep_count, 0);
814 wake_up(&chip->interrupt_sleep);
815 }
816 }
817
818 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
819 if (status & 1) {
820 if (chip->timer)
821 snd_timer_interrupt(chip->timer, chip->timer->sticks);
822 }
823 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
824
825 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100826 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return IRQ_HANDLED;
828}
829
Takashi Iwai208a1b42005-11-17 14:53:41 +0100830static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 .info = (SNDRV_PCM_INFO_MMAP |
833 SNDRV_PCM_INFO_MMAP_VALID |
834 SNDRV_PCM_INFO_INTERLEAVED |
835 SNDRV_PCM_INFO_BLOCK_TRANSFER |
836 SNDRV_PCM_INFO_PAUSE |
837 SNDRV_PCM_INFO_RESUME),
838 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
839 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
840 .rate_min = 8000,
841 .rate_max = 48000,
842 .channels_min = 1,
843 .channels_max = 2,
844 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
845 .period_bytes_min = 64,
846 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
847 .periods_min = 3,
848 .periods_max = 1024,
849 .fifo_size = 0,
850};
851
Takashi Iwai208a1b42005-11-17 14:53:41 +0100852static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 .info = (SNDRV_PCM_INFO_MMAP |
855 SNDRV_PCM_INFO_MMAP_VALID |
856 SNDRV_PCM_INFO_INTERLEAVED |
857 SNDRV_PCM_INFO_BLOCK_TRANSFER |
858 SNDRV_PCM_INFO_PAUSE |
859 SNDRV_PCM_INFO_RESUME),
860 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
861 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
862 .rate_min = 8000,
863 .rate_max = 48000,
864 .channels_min = 1,
865 .channels_max = 2,
866 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
867 .period_bytes_min = 64,
868 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
869 .periods_min = 3,
870 .periods_max = 1024,
871 .fifo_size = 0,
872};
873
Takashi Iwai208a1b42005-11-17 14:53:41 +0100874static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jesper Juhl4d572772005-05-30 17:30:32 +0200876 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Takashi Iwai208a1b42005-11-17 14:53:41 +0100879static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100881 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
882 struct snd_pcm_runtime *runtime = substream->runtime;
883 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200885 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (ypcm == NULL)
887 return -ENOMEM;
888 ypcm->chip = chip;
889 ypcm->type = PLAYBACK_VOICE;
890 ypcm->substream = substream;
891 runtime->hw = snd_ymfpci_playback;
892 runtime->private_data = ypcm;
893 runtime->private_free = snd_ymfpci_pcm_free_substream;
894 /* FIXME? True value is 256/48 = 5.33333 ms */
895 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
896 return 0;
897}
898
899/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100900static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 if (! chip->rear_opened) {
903 if (! chip->spdif_opened) /* set AC3 */
904 snd_ymfpci_writel(chip, YDSXGR_MODE,
905 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
906 /* enable second codec (4CHEN) */
907 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
908 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
909 }
910}
911
912/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100913static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 if (! chip->rear_opened) {
916 if (! chip->spdif_opened)
917 snd_ymfpci_writel(chip, YDSXGR_MODE,
918 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
919 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
920 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
921 }
922}
923
Takashi Iwai208a1b42005-11-17 14:53:41 +0100924static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100926 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
927 struct snd_pcm_runtime *runtime = substream->runtime;
928 struct snd_ymfpci_pcm *ypcm;
929 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int err;
931
932 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
933 return err;
934 ypcm = runtime->private_data;
935 ypcm->output_front = 1;
936 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200937 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 spin_lock_irq(&chip->reg_lock);
939 if (ypcm->output_rear) {
940 ymfpci_open_extension(chip);
941 chip->rear_opened++;
942 }
943 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200944
945 kctl = chip->pcm_mixer[substream->number].ctl;
946 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
947 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949}
950
Takashi Iwai208a1b42005-11-17 14:53:41 +0100951static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100953 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
954 struct snd_pcm_runtime *runtime = substream->runtime;
955 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int err;
957
958 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
959 return err;
960 ypcm = runtime->private_data;
961 ypcm->output_front = 0;
962 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200963 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 spin_lock_irq(&chip->reg_lock);
965 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
966 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
967 ymfpci_open_extension(chip);
968 chip->spdif_pcm_bits = chip->spdif_bits;
969 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
970 chip->spdif_opened++;
971 spin_unlock_irq(&chip->reg_lock);
972
973 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
974 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
975 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
976 return 0;
977}
978
Takashi Iwai208a1b42005-11-17 14:53:41 +0100979static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100981 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
982 struct snd_pcm_runtime *runtime = substream->runtime;
983 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int err;
985
986 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
987 return err;
988 ypcm = runtime->private_data;
989 ypcm->output_front = 0;
990 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200991 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_lock_irq(&chip->reg_lock);
993 ymfpci_open_extension(chip);
994 chip->rear_opened++;
995 spin_unlock_irq(&chip->reg_lock);
996 return 0;
997}
998
Takashi Iwai208a1b42005-11-17 14:53:41 +0100999static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 u32 capture_bank_number)
1001{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001002 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1003 struct snd_pcm_runtime *runtime = substream->runtime;
1004 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001006 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (ypcm == NULL)
1008 return -ENOMEM;
1009 ypcm->chip = chip;
1010 ypcm->type = capture_bank_number + CAPTURE_REC;
1011 ypcm->substream = substream;
1012 ypcm->capture_bank_number = capture_bank_number;
1013 chip->capture_substream[capture_bank_number] = substream;
1014 runtime->hw = snd_ymfpci_capture;
1015 /* FIXME? True value is 256/48 = 5.33333 ms */
1016 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1017 runtime->private_data = ypcm;
1018 runtime->private_free = snd_ymfpci_pcm_free_substream;
1019 snd_ymfpci_hw_start(chip);
1020 return 0;
1021}
1022
Takashi Iwai208a1b42005-11-17 14:53:41 +01001023static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 return snd_ymfpci_capture_open(substream, 0);
1026}
1027
Takashi Iwai208a1b42005-11-17 14:53:41 +01001028static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 return snd_ymfpci_capture_open(substream, 1);
1031}
1032
Takashi Iwai208a1b42005-11-17 14:53:41 +01001033static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 return 0;
1036}
1037
Takashi Iwai208a1b42005-11-17 14:53:41 +01001038static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001040 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1041 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1042 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 spin_lock_irq(&chip->reg_lock);
1045 if (ypcm->output_rear && chip->rear_opened > 0) {
1046 chip->rear_opened--;
1047 ymfpci_close_extension(chip);
1048 }
1049 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001050 kctl = chip->pcm_mixer[substream->number].ctl;
1051 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1052 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return snd_ymfpci_playback_close_1(substream);
1054}
1055
Takashi Iwai208a1b42005-11-17 14:53:41 +01001056static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001058 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 spin_lock_irq(&chip->reg_lock);
1061 chip->spdif_opened = 0;
1062 ymfpci_close_extension(chip);
1063 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1064 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1065 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1066 spin_unlock_irq(&chip->reg_lock);
1067 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1068 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1069 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1070 return snd_ymfpci_playback_close_1(substream);
1071}
1072
Takashi Iwai208a1b42005-11-17 14:53:41 +01001073static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001075 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 spin_lock_irq(&chip->reg_lock);
1078 if (chip->rear_opened > 0) {
1079 chip->rear_opened--;
1080 ymfpci_close_extension(chip);
1081 }
1082 spin_unlock_irq(&chip->reg_lock);
1083 return snd_ymfpci_playback_close_1(substream);
1084}
1085
Takashi Iwai208a1b42005-11-17 14:53:41 +01001086static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001088 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1089 struct snd_pcm_runtime *runtime = substream->runtime;
1090 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (ypcm != NULL) {
1093 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1094 snd_ymfpci_hw_stop(chip);
1095 }
1096 return 0;
1097}
1098
Takashi Iwai208a1b42005-11-17 14:53:41 +01001099static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 .open = snd_ymfpci_playback_open,
1101 .close = snd_ymfpci_playback_close,
1102 .ioctl = snd_pcm_lib_ioctl,
1103 .hw_params = snd_ymfpci_playback_hw_params,
1104 .hw_free = snd_ymfpci_playback_hw_free,
1105 .prepare = snd_ymfpci_playback_prepare,
1106 .trigger = snd_ymfpci_playback_trigger,
1107 .pointer = snd_ymfpci_playback_pointer,
1108};
1109
Takashi Iwai208a1b42005-11-17 14:53:41 +01001110static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 .open = snd_ymfpci_capture_rec_open,
1112 .close = snd_ymfpci_capture_close,
1113 .ioctl = snd_pcm_lib_ioctl,
1114 .hw_params = snd_ymfpci_capture_hw_params,
1115 .hw_free = snd_ymfpci_capture_hw_free,
1116 .prepare = snd_ymfpci_capture_prepare,
1117 .trigger = snd_ymfpci_capture_trigger,
1118 .pointer = snd_ymfpci_capture_pointer,
1119};
1120
Takashi Iwai208a1b42005-11-17 14:53:41 +01001121int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001123 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int err;
1125
1126 if (rpcm)
1127 *rpcm = NULL;
1128 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1129 return err;
1130 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1133 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1134
1135 /* global setup */
1136 pcm->info_flags = 0;
1137 strcpy(pcm->name, "YMFPCI");
1138 chip->pcm = pcm;
1139
1140 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1141 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1142
1143 if (rpcm)
1144 *rpcm = pcm;
1145 return 0;
1146}
1147
Takashi Iwai208a1b42005-11-17 14:53:41 +01001148static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 .open = snd_ymfpci_capture_ac97_open,
1150 .close = snd_ymfpci_capture_close,
1151 .ioctl = snd_pcm_lib_ioctl,
1152 .hw_params = snd_ymfpci_capture_hw_params,
1153 .hw_free = snd_ymfpci_capture_hw_free,
1154 .prepare = snd_ymfpci_capture_prepare,
1155 .trigger = snd_ymfpci_capture_trigger,
1156 .pointer = snd_ymfpci_capture_pointer,
1157};
1158
Takashi Iwai208a1b42005-11-17 14:53:41 +01001159int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001161 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int err;
1163
1164 if (rpcm)
1165 *rpcm = NULL;
1166 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1167 return err;
1168 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1171
1172 /* global setup */
1173 pcm->info_flags = 0;
1174 sprintf(pcm->name, "YMFPCI - %s",
1175 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1176 chip->pcm2 = pcm;
1177
1178 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1179 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1180
1181 if (rpcm)
1182 *rpcm = pcm;
1183 return 0;
1184}
1185
Takashi Iwai208a1b42005-11-17 14:53:41 +01001186static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 .open = snd_ymfpci_playback_spdif_open,
1188 .close = snd_ymfpci_playback_spdif_close,
1189 .ioctl = snd_pcm_lib_ioctl,
1190 .hw_params = snd_ymfpci_playback_hw_params,
1191 .hw_free = snd_ymfpci_playback_hw_free,
1192 .prepare = snd_ymfpci_playback_prepare,
1193 .trigger = snd_ymfpci_playback_trigger,
1194 .pointer = snd_ymfpci_playback_pointer,
1195};
1196
Takashi Iwai208a1b42005-11-17 14:53:41 +01001197int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001199 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int err;
1201
1202 if (rpcm)
1203 *rpcm = NULL;
1204 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1205 return err;
1206 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1209
1210 /* global setup */
1211 pcm->info_flags = 0;
1212 strcpy(pcm->name, "YMFPCI - IEC958");
1213 chip->pcm_spdif = pcm;
1214
1215 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1216 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1217
1218 if (rpcm)
1219 *rpcm = pcm;
1220 return 0;
1221}
1222
Takashi Iwai208a1b42005-11-17 14:53:41 +01001223static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 .open = snd_ymfpci_playback_4ch_open,
1225 .close = snd_ymfpci_playback_4ch_close,
1226 .ioctl = snd_pcm_lib_ioctl,
1227 .hw_params = snd_ymfpci_playback_hw_params,
1228 .hw_free = snd_ymfpci_playback_hw_free,
1229 .prepare = snd_ymfpci_playback_prepare,
1230 .trigger = snd_ymfpci_playback_trigger,
1231 .pointer = snd_ymfpci_playback_pointer,
1232};
1233
Takashi Iwai208a1b42005-11-17 14:53:41 +01001234int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001236 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int err;
1238
1239 if (rpcm)
1240 *rpcm = NULL;
1241 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1242 return err;
1243 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1246
1247 /* global setup */
1248 pcm->info_flags = 0;
1249 strcpy(pcm->name, "YMFPCI - Rear PCM");
1250 chip->pcm_4ch = pcm;
1251
1252 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1253 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1254
1255 if (rpcm)
1256 *rpcm = pcm;
1257 return 0;
1258}
1259
Takashi Iwai208a1b42005-11-17 14:53:41 +01001260static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1263 uinfo->count = 1;
1264 return 0;
1265}
1266
Takashi Iwai208a1b42005-11-17 14:53:41 +01001267static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1268 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001270 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 spin_lock_irq(&chip->reg_lock);
1273 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1274 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001275 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 spin_unlock_irq(&chip->reg_lock);
1277 return 0;
1278}
1279
Takashi Iwai208a1b42005-11-17 14:53:41 +01001280static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1281 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001283 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unsigned int val;
1285 int change;
1286
1287 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1288 (ucontrol->value.iec958.status[1] << 8);
1289 spin_lock_irq(&chip->reg_lock);
1290 change = chip->spdif_bits != val;
1291 chip->spdif_bits = val;
1292 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1293 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1294 spin_unlock_irq(&chip->reg_lock);
1295 return change;
1296}
1297
Takashi Iwai208a1b42005-11-17 14:53:41 +01001298static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1301 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1302 .info = snd_ymfpci_spdif_default_info,
1303 .get = snd_ymfpci_spdif_default_get,
1304 .put = snd_ymfpci_spdif_default_put
1305};
1306
Takashi Iwai208a1b42005-11-17 14:53:41 +01001307static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1310 uinfo->count = 1;
1311 return 0;
1312}
1313
Takashi Iwai208a1b42005-11-17 14:53:41 +01001314static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1315 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001317 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 spin_lock_irq(&chip->reg_lock);
1320 ucontrol->value.iec958.status[0] = 0x3e;
1321 ucontrol->value.iec958.status[1] = 0xff;
1322 spin_unlock_irq(&chip->reg_lock);
1323 return 0;
1324}
1325
Takashi Iwai208a1b42005-11-17 14:53:41 +01001326static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1329 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1330 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1331 .info = snd_ymfpci_spdif_mask_info,
1332 .get = snd_ymfpci_spdif_mask_get,
1333};
1334
Takashi Iwai208a1b42005-11-17 14:53:41 +01001335static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1338 uinfo->count = 1;
1339 return 0;
1340}
1341
Takashi Iwai208a1b42005-11-17 14:53:41 +01001342static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001345 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 spin_lock_irq(&chip->reg_lock);
1348 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1349 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001350 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_unlock_irq(&chip->reg_lock);
1352 return 0;
1353}
1354
Takashi Iwai208a1b42005-11-17 14:53:41 +01001355static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001358 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned int val;
1360 int change;
1361
1362 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1363 (ucontrol->value.iec958.status[1] << 8);
1364 spin_lock_irq(&chip->reg_lock);
1365 change = chip->spdif_pcm_bits != val;
1366 chip->spdif_pcm_bits = val;
1367 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1368 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1369 spin_unlock_irq(&chip->reg_lock);
1370 return change;
1371}
1372
Takashi Iwai208a1b42005-11-17 14:53:41 +01001373static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1376 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1377 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1378 .info = snd_ymfpci_spdif_stream_info,
1379 .get = snd_ymfpci_spdif_stream_get,
1380 .put = snd_ymfpci_spdif_stream_put
1381};
1382
Takashi Iwai208a1b42005-11-17 14:53:41 +01001383static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1386
1387 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1388 info->count = 1;
1389 info->value.enumerated.items = 3;
1390 if (info->value.enumerated.item > 2)
1391 info->value.enumerated.item = 2;
1392 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1393 return 0;
1394}
1395
Takashi Iwai208a1b42005-11-17 14:53:41 +01001396static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001398 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 u16 reg;
1400
1401 spin_lock_irq(&chip->reg_lock);
1402 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1403 spin_unlock_irq(&chip->reg_lock);
1404 if (!(reg & 0x100))
1405 value->value.enumerated.item[0] = 0;
1406 else
1407 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1408 return 0;
1409}
1410
Takashi Iwai208a1b42005-11-17 14:53:41 +01001411static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001413 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 u16 reg, old_reg;
1415
1416 spin_lock_irq(&chip->reg_lock);
1417 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1418 if (value->value.enumerated.item[0] == 0)
1419 reg = old_reg & ~0x100;
1420 else
1421 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1422 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1423 spin_unlock_irq(&chip->reg_lock);
1424 return reg != old_reg;
1425}
1426
Takashi Iwai208a1b42005-11-17 14:53:41 +01001427static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1429 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1430 .name = "Direct Recording Source",
1431 .info = snd_ymfpci_drec_source_info,
1432 .get = snd_ymfpci_drec_source_get,
1433 .put = snd_ymfpci_drec_source_put
1434};
1435
1436/*
1437 * Mixer controls
1438 */
1439
Glen Masgaid602c882005-09-28 08:19:05 +02001440#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1442 .info = snd_ymfpci_info_single, \
1443 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001444 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001446#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwai208a1b42005-11-17 14:53:41 +01001448static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1449 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001451 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001452 int reg = kcontrol->private_value & 0xffff;
1453 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1454 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Glen Masgaid602c882005-09-28 08:19:05 +02001456 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 case YDSXGR_SPDIFOUTCTRL: break;
1458 case YDSXGR_SPDIFINCTRL: break;
1459 default: return -EINVAL;
1460 }
Glen Masgaid602c882005-09-28 08:19:05 +02001461 ucontrol->value.integer.value[0] =
1462 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
Takashi Iwai208a1b42005-11-17 14:53:41 +01001466static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001469 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001470 int reg = kcontrol->private_value & 0xffff;
1471 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1472 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int change;
1474 unsigned int val, oval;
1475
Glen Masgaid602c882005-09-28 08:19:05 +02001476 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 case YDSXGR_SPDIFOUTCTRL: break;
1478 case YDSXGR_SPDIFINCTRL: break;
1479 default: return -EINVAL;
1480 }
1481 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 val <<= shift;
1483 spin_lock_irq(&chip->reg_lock);
1484 oval = snd_ymfpci_readl(chip, reg);
1485 val = (oval & ~(mask << shift)) | val;
1486 change = val != oval;
1487 snd_ymfpci_writel(chip, reg, val);
1488 spin_unlock_irq(&chip->reg_lock);
1489 return change;
1490}
1491
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001492static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494#define YMFPCI_DOUBLE(xname, xindex, reg) \
1495{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001496 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .info = snd_ymfpci_info_double, \
1498 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001499 .private_value = reg, \
1500 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Takashi Iwai208a1b42005-11-17 14:53:41 +01001502static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 if (reg < 0x80 || reg >= 0xc0)
1507 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001508 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 uinfo->count = 2;
1510 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001511 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513}
1514
Takashi Iwai208a1b42005-11-17 14:53:41 +01001515static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001517 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001519 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unsigned int val;
1521
1522 if (reg < 0x80 || reg >= 0xc0)
1523 return -EINVAL;
1524 spin_lock_irq(&chip->reg_lock);
1525 val = snd_ymfpci_readl(chip, reg);
1526 spin_unlock_irq(&chip->reg_lock);
1527 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1528 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530}
1531
Takashi Iwai208a1b42005-11-17 14:53:41 +01001532static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001534 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001536 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int change;
1538 unsigned int val1, val2, oval;
1539
1540 if (reg < 0x80 || reg >= 0xc0)
1541 return -EINVAL;
1542 val1 = ucontrol->value.integer.value[0] & mask;
1543 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 val1 <<= shift_left;
1545 val2 <<= shift_right;
1546 spin_lock_irq(&chip->reg_lock);
1547 oval = snd_ymfpci_readl(chip, reg);
1548 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1549 change = val1 != oval;
1550 snd_ymfpci_writel(chip, reg, val1);
1551 spin_unlock_irq(&chip->reg_lock);
1552 return change;
1553}
1554
1555/*
1556 * 4ch duplication
1557 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001558#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Takashi Iwai208a1b42005-11-17 14:53:41 +01001560static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001562 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1564 return 0;
1565}
1566
Takashi Iwai208a1b42005-11-17 14:53:41 +01001567static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001569 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int change;
1571 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1572 if (change)
1573 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1574 return change;
1575}
1576
1577
Takashi Iwai208a1b42005-11-17 14:53:41 +01001578static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1580YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1581YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1582YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1583YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1584YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1585YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1586YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1587YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1588YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1589YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1590YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1591YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001592YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1593YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1594YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1597 .name = "4ch Duplication",
1598 .info = snd_ymfpci_info_dup4ch,
1599 .get = snd_ymfpci_get_dup4ch,
1600 .put = snd_ymfpci_put_dup4ch,
1601},
1602};
1603
1604
1605/*
1606 * GPIO
1607 */
1608
Takashi Iwai208a1b42005-11-17 14:53:41 +01001609static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 u16 reg, mode;
1612 unsigned long flags;
1613
1614 spin_lock_irqsave(&chip->reg_lock, flags);
1615 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1616 reg &= ~(1 << (pin + 8));
1617 reg |= (1 << pin);
1618 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1619 /* set the level mode for input line */
1620 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1621 mode &= ~(3 << (pin * 2));
1622 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1623 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1624 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1625 spin_unlock_irqrestore(&chip->reg_lock, flags);
1626 return (mode >> pin) & 1;
1627}
1628
Takashi Iwai208a1b42005-11-17 14:53:41 +01001629static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 u16 reg;
1632 unsigned long flags;
1633
1634 spin_lock_irqsave(&chip->reg_lock, flags);
1635 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1636 reg &= ~(1 << pin);
1637 reg &= ~(1 << (pin + 8));
1638 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1639 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1640 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1641 spin_unlock_irqrestore(&chip->reg_lock, flags);
1642
1643 return 0;
1644}
1645
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001646#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Takashi Iwai208a1b42005-11-17 14:53:41 +01001648static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001650 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int pin = (int)kcontrol->private_value;
1652 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1653 return 0;
1654}
1655
Takashi Iwai208a1b42005-11-17 14:53:41 +01001656static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001658 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 int pin = (int)kcontrol->private_value;
1660
1661 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1662 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1663 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1664 return 1;
1665 }
1666 return 0;
1667}
1668
Takashi Iwai208a1b42005-11-17 14:53:41 +01001669static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 .name = "Shared Rear/Line-In Switch",
1671 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1672 .info = snd_ymfpci_gpio_sw_info,
1673 .get = snd_ymfpci_gpio_sw_get,
1674 .put = snd_ymfpci_gpio_sw_put,
1675 .private_value = 2,
1676};
1677
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001678/*
1679 * PCM voice volume
1680 */
1681
Takashi Iwai208a1b42005-11-17 14:53:41 +01001682static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001684{
1685 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1686 uinfo->count = 2;
1687 uinfo->value.integer.min = 0;
1688 uinfo->value.integer.max = 0x8000;
1689 return 0;
1690}
1691
Takashi Iwai208a1b42005-11-17 14:53:41 +01001692static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001694{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001695 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001696 unsigned int subs = kcontrol->id.subdevice;
1697
1698 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1699 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1700 return 0;
1701}
1702
Takashi Iwai208a1b42005-11-17 14:53:41 +01001703static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001705{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001706 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001707 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001708 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001709 unsigned long flags;
1710
1711 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1712 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1713 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1714 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1715
Takashi Iwai208a1b42005-11-17 14:53:41 +01001716 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001717 spin_lock_irqsave(&chip->voice_lock, flags);
1718 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001719 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001720 if (!ypcm->use_441_slot)
1721 ypcm->update_pcm_vol = 2;
1722 else
1723 snd_ymfpci_pcm_441_volume_set(ypcm);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001724 }
1725 spin_unlock_irqrestore(&chip->voice_lock, flags);
1726 return 1;
1727 }
1728 return 0;
1729}
1730
Takashi Iwai208a1b42005-11-17 14:53:41 +01001731static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001732 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1733 .name = "PCM Playback Volume",
1734 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1735 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1736 .info = snd_ymfpci_pcm_vol_info,
1737 .get = snd_ymfpci_pcm_vol_get,
1738 .put = snd_ymfpci_pcm_vol_put,
1739};
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742/*
1743 * Mixer routines
1744 */
1745
Takashi Iwai208a1b42005-11-17 14:53:41 +01001746static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001748 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 chip->ac97_bus = NULL;
1750}
1751
Takashi Iwai208a1b42005-11-17 14:53:41 +01001752static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001754 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 chip->ac97 = NULL;
1756}
1757
Glen Masgaid9301262006-10-10 09:27:19 +02001758int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001760 struct snd_ac97_template ac97;
1761 struct snd_kcontrol *kctl;
1762 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 unsigned int idx;
1764 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001765 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .write = snd_ymfpci_codec_write,
1767 .read = snd_ymfpci_codec_read,
1768 };
1769
1770 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1771 return err;
1772 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1773 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1774
1775 memset(&ac97, 0, sizeof(ac97));
1776 ac97.private_data = chip;
1777 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1778 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1779 return err;
1780
1781 /* to be sure */
1782 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1783 AC97_EA_VRA|AC97_EA_VRM, 0);
1784
1785 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1786 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1787 return err;
1788 }
1789
1790 /* add S/PDIF control */
1791 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1792 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1793 return err;
1794 kctl->id.device = chip->pcm_spdif->device;
1795 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1796 return err;
1797 kctl->id.device = chip->pcm_spdif->device;
1798 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1799 return err;
1800 kctl->id.device = chip->pcm_spdif->device;
1801 chip->spdif_pcm_ctl = kctl;
1802
1803 /* direct recording source */
1804 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1805 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1806 return err;
1807
1808 /*
1809 * shared rear/line-in
1810 */
1811 if (rear_switch) {
1812 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1813 return err;
1814 }
1815
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001816 /* per-voice volume */
1817 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1818 for (idx = 0; idx < 32; ++idx) {
1819 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1820 if (!kctl)
1821 return -ENOMEM;
1822 kctl->id.device = chip->pcm->device;
1823 kctl->id.subdevice = idx;
1824 kctl->private_value = (unsigned long)substream;
1825 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1826 return err;
1827 chip->pcm_mixer[idx].left = 0x8000;
1828 chip->pcm_mixer[idx].right = 0x8000;
1829 chip->pcm_mixer[idx].ctl = kctl;
1830 substream = substream->next;
1831 }
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834}
1835
1836
1837/*
1838 * timer
1839 */
1840
Takashi Iwai208a1b42005-11-17 14:53:41 +01001841static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001843 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 unsigned long flags;
1845 unsigned int count;
1846
1847 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001848 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 spin_lock_irqsave(&chip->reg_lock, flags);
1850 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1851 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1852 spin_unlock_irqrestore(&chip->reg_lock, flags);
1853 return 0;
1854}
1855
Takashi Iwai208a1b42005-11-17 14:53:41 +01001856static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001858 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned long flags;
1860
1861 chip = snd_timer_chip(timer);
1862 spin_lock_irqsave(&chip->reg_lock, flags);
1863 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1864 spin_unlock_irqrestore(&chip->reg_lock, flags);
1865 return 0;
1866}
1867
Takashi Iwai208a1b42005-11-17 14:53:41 +01001868static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 unsigned long *num, unsigned long *den)
1870{
1871 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001872 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return 0;
1874}
1875
Takashi Iwai208a1b42005-11-17 14:53:41 +01001876static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001878 .resolution = 20833, /* 1/fs = 20.8333...us */
1879 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 .start = snd_ymfpci_timer_start,
1881 .stop = snd_ymfpci_timer_stop,
1882 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1883};
1884
Takashi Iwai208a1b42005-11-17 14:53:41 +01001885int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001887 struct snd_timer *timer = NULL;
1888 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int err;
1890
1891 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1892 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1893 tid.card = chip->card->number;
1894 tid.device = device;
1895 tid.subdevice = 0;
1896 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1897 strcpy(timer->name, "YMFPCI timer");
1898 timer->private_data = chip;
1899 timer->hw = snd_ymfpci_timer_hw;
1900 }
1901 chip->timer = timer;
1902 return err;
1903}
1904
1905
1906/*
1907 * proc interface
1908 */
1909
Takashi Iwai208a1b42005-11-17 14:53:41 +01001910static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1911 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001913 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 int i;
1915
1916 snd_iprintf(buffer, "YMFPCI\n\n");
1917 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1918 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1919}
1920
Takashi Iwai208a1b42005-11-17 14:53:41 +01001921static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001923 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001926 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
1928}
1929
1930/*
1931 * initialization routines
1932 */
1933
1934static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1935{
1936 u8 cmd;
1937
1938 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1939#if 0 // force to reset
1940 if (cmd & 0x03) {
1941#endif
1942 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1943 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1944 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1945 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1946 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1947#if 0
1948 }
1949#endif
1950}
1951
Takashi Iwai208a1b42005-11-17 14:53:41 +01001952static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1955}
1956
Takashi Iwai208a1b42005-11-17 14:53:41 +01001957static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 u32 val;
1960 int timeout = 1000;
1961
1962 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1963 if (val)
1964 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1965 while (timeout-- > 0) {
1966 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1967 if ((val & 0x00000002) == 0)
1968 break;
1969 }
1970}
1971
Takashi Iwai8ad2da12007-02-26 15:55:43 +01001972#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
Clemens Ladisch102fa902006-10-11 12:05:59 +02001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974#include "ymfpci_image.h"
1975
Clemens Ladisch102fa902006-10-11 12:05:59 +02001976static struct firmware snd_ymfpci_dsp_microcode = {
1977 .size = YDSXG_DSPLENGTH,
1978 .data = (u8 *)DspInst,
1979};
1980static struct firmware snd_ymfpci_controller_microcode = {
1981 .size = YDSXG_CTRLLENGTH,
1982 .data = (u8 *)CntrlInst,
1983};
1984static struct firmware snd_ymfpci_controller_1e_microcode = {
1985 .size = YDSXG_CTRLLENGTH,
1986 .data = (u8 *)CntrlInst1E,
1987};
1988#endif
1989
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02001990#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
1991static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
1992{
1993 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
1994 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
1995 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
1996 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
1997 chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
1998 chip->controller_microcode =
1999 &snd_ymfpci_controller_1e_microcode;
2000 else
2001 chip->controller_microcode =
2002 &snd_ymfpci_controller_microcode;
2003 return 0;
2004}
2005
2006#else /* use fw_loader */
2007
Clemens Ladisch102fa902006-10-11 12:05:59 +02002008#ifdef __LITTLE_ENDIAN
2009static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
2010#else
2011static void snd_ymfpci_convert_from_le(const struct firmware *fw)
2012{
2013 int i;
2014 u32 *data = (u32 *)fw->data;
2015
2016 for (i = 0; i < fw->size / 4; ++i)
2017 le32_to_cpus(&data[i]);
2018}
2019#endif
2020
2021static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2022{
2023 int err, is_1e;
2024 const char *name;
2025
2026 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2027 &chip->pci->dev);
2028 if (err >= 0) {
2029 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2030 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2031 else {
2032 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2033 err = -EINVAL;
2034 }
2035 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002036 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002037 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002038 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2039 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2040 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2041 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2042 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2043 err = request_firmware(&chip->controller_microcode, name,
2044 &chip->pci->dev);
2045 if (err >= 0) {
2046 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2047 snd_ymfpci_convert_from_le(chip->controller_microcode);
2048 else {
2049 snd_printk(KERN_ERR "controller microcode"
2050 " has wrong size\n");
2051 err = -EINVAL;
2052 }
2053 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002054 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002055 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002056 return 0;
2057}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002058
2059MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2060MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2061MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2062
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002063#endif
Clemens Ladisch102fa902006-10-11 12:05:59 +02002064
Takashi Iwai208a1b42005-11-17 14:53:41 +01002065static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 int i;
2068 u16 ctrl;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002069 u32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2072 snd_ymfpci_disable_dsp(chip);
2073 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2074 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2075 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2076 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2077 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2078 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2079 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2080 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2081 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2082
2083 /* setup DSP instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002084 inst = (u32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002086 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /* setup control instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002089 inst = (u32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2091 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2092
2093 snd_ymfpci_enable_dsp(chip);
2094}
2095
Takashi Iwai208a1b42005-11-17 14:53:41 +01002096static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 long size, playback_ctrl_size;
2099 int voice, bank, reg;
2100 u8 *ptr;
2101 dma_addr_t ptr_addr;
2102
2103 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2104 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2105 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2106 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2107 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2108
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002109 size = ALIGN(playback_ctrl_size, 0x100) +
2110 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2111 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2112 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 chip->work_size;
2114 /* work_ptr must be aligned to 256 bytes, but it's already
2115 covered with the kernel page allocation mechanism */
2116 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2117 size, &chip->work_ptr) < 0)
2118 return -ENOMEM;
2119 ptr = chip->work_ptr.area;
2120 ptr_addr = chip->work_ptr.addr;
2121 memset(ptr, 0, size); /* for sure */
2122
2123 chip->bank_base_playback = ptr;
2124 chip->bank_base_playback_addr = ptr_addr;
2125 chip->ctrl_playback = (u32 *)ptr;
2126 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002127 ptr += ALIGN(playback_ctrl_size, 0x100);
2128 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2130 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002131 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 chip->voices[voice].bank_addr = ptr_addr;
2133 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002134 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 ptr += chip->bank_size_playback;
2136 ptr_addr += chip->bank_size_playback;
2137 }
2138 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002139 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2140 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 chip->bank_base_capture = ptr;
2142 chip->bank_base_capture_addr = ptr_addr;
2143 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2144 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002145 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 ptr += chip->bank_size_capture;
2147 ptr_addr += chip->bank_size_capture;
2148 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002149 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2150 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 chip->bank_base_effect = ptr;
2152 chip->bank_base_effect_addr = ptr_addr;
2153 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2154 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002155 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 ptr += chip->bank_size_effect;
2157 ptr_addr += chip->bank_size_effect;
2158 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002159 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2160 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 chip->work_base = ptr;
2162 chip->work_base_addr = ptr_addr;
2163
2164 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2165
2166 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2167 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2168 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2169 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2170 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2171
2172 /* S/PDIF output initialization */
2173 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2174 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2175 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2176
2177 /* S/PDIF input initialization */
2178 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2179
2180 /* digital mixer setup */
2181 for (reg = 0x80; reg < 0xc0; reg += 4)
2182 snd_ymfpci_writel(chip, reg, 0);
2183 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2184 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2185 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2186 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2187 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2188 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2189 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2190
2191 return 0;
2192}
2193
Takashi Iwai208a1b42005-11-17 14:53:41 +01002194static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 u16 ctrl;
2197
2198 snd_assert(chip != NULL, return -EINVAL);
2199
2200 if (chip->res_reg_area) { /* don't touch busy hardware */
2201 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2202 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2203 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2204 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2205 snd_ymfpci_disable_dsp(chip);
2206 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2207 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2208 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2209 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2210 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2211 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2212 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2213 }
2214
2215 snd_ymfpci_ac3_done(chip);
2216
2217 /* Set PCI device to D3 state */
2218#if 0
2219 /* FIXME: temporarily disabled, otherwise we cannot fire up
2220 * the chip again unless reboot. ACPI bug?
2221 */
2222 pci_set_power_state(chip->pci, 3);
2223#endif
2224
2225#ifdef CONFIG_PM
2226 vfree(chip->saved_regs);
2227#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +02002228 release_and_free_resource(chip->mpu_res);
2229 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 snd_ymfpci_free_gameport(chip);
2231 if (chip->reg_area_virt)
2232 iounmap(chip->reg_area_virt);
2233 if (chip->work_ptr.area)
2234 snd_dma_free_pages(&chip->work_ptr);
2235
2236 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01002237 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002238 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2241
2242 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002243#ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2244 release_firmware(chip->dsp_microcode);
2245 release_firmware(chip->controller_microcode);
Clemens Ladisch102fa902006-10-11 12:05:59 +02002246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 kfree(chip);
2248 return 0;
2249}
2250
Takashi Iwai208a1b42005-11-17 14:53:41 +01002251static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002253 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return snd_ymfpci_free(chip);
2255}
2256
2257#ifdef CONFIG_PM
2258static int saved_regs_index[] = {
2259 /* spdif */
2260 YDSXGR_SPDIFOUTCTRL,
2261 YDSXGR_SPDIFOUTSTATUS,
2262 YDSXGR_SPDIFINCTRL,
2263 /* volumes */
2264 YDSXGR_PRIADCLOOPVOL,
2265 YDSXGR_NATIVEDACINVOL,
2266 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002267 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 YDSXGR_NATIVEADCINVOL,
2269 YDSXGR_SPDIFLOOPVOL,
2270 YDSXGR_SPDIFOUTVOL,
2271 YDSXGR_ZVOUTVOL,
2272 YDSXGR_LEGACYOUTVOL,
2273 /* address bases */
2274 YDSXGR_PLAYCTRLBASE,
2275 YDSXGR_RECCTRLBASE,
2276 YDSXGR_EFFCTRLBASE,
2277 YDSXGR_WORKBASE,
2278 /* capture set up */
2279 YDSXGR_MAPOFREC,
2280 YDSXGR_RECFORMAT,
2281 YDSXGR_RECSLOTSR,
2282 YDSXGR_ADCFORMAT,
2283 YDSXGR_ADCSLOTSR,
2284};
2285#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2286
Takashi Iwaided46232005-11-17 16:09:43 +01002287int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Takashi Iwaided46232005-11-17 16:09:43 +01002289 struct snd_card *card = pci_get_drvdata(pci);
2290 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 unsigned int i;
2292
Takashi Iwaided46232005-11-17 16:09:43 +01002293 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 snd_pcm_suspend_all(chip->pcm);
2295 snd_pcm_suspend_all(chip->pcm2);
2296 snd_pcm_suspend_all(chip->pcm_spdif);
2297 snd_pcm_suspend_all(chip->pcm_4ch);
2298 snd_ac97_suspend(chip->ac97);
2299 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2300 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2301 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2302 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2303 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002304 pci_disable_device(pci);
2305 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002306 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
2309
Takashi Iwaided46232005-11-17 16:09:43 +01002310int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Takashi Iwaided46232005-11-17 16:09:43 +01002312 struct snd_card *card = pci_get_drvdata(pci);
2313 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 unsigned int i;
2315
Takashi Iwai30b35392006-10-11 18:52:53 +02002316 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002317 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002318 if (pci_enable_device(pci) < 0) {
2319 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2320 "disabling device\n");
2321 snd_card_disconnect(card);
2322 return -EIO;
2323 }
Takashi Iwaided46232005-11-17 16:09:43 +01002324 pci_set_master(pci);
2325 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 snd_ymfpci_codec_ready(chip, 0);
2327 snd_ymfpci_download_image(chip);
2328 udelay(100);
2329
2330 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2331 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2332
2333 snd_ac97_resume(chip->ac97);
2334
2335 /* start hw again */
2336 if (chip->start_count > 0) {
2337 spin_lock_irq(&chip->reg_lock);
2338 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2339 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2340 spin_unlock_irq(&chip->reg_lock);
2341 }
Takashi Iwaided46232005-11-17 16:09:43 +01002342 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344}
2345#endif /* CONFIG_PM */
2346
Takashi Iwai208a1b42005-11-17 14:53:41 +01002347int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 struct pci_dev * pci,
2349 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002350 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002352 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002354 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 .dev_free = snd_ymfpci_dev_free,
2356 };
2357
2358 *rchip = NULL;
2359
2360 /* enable PCI device */
2361 if ((err = pci_enable_device(pci)) < 0)
2362 return err;
2363
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002364 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (chip == NULL) {
2366 pci_disable_device(pci);
2367 return -ENOMEM;
2368 }
2369 chip->old_legacy_ctrl = old_legacy_ctrl;
2370 spin_lock_init(&chip->reg_lock);
2371 spin_lock_init(&chip->voice_lock);
2372 init_waitqueue_head(&chip->interrupt_sleep);
2373 atomic_set(&chip->interrupt_sleep_count, 0);
2374 chip->card = card;
2375 chip->pci = pci;
2376 chip->irq = -1;
2377 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002378 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 chip->reg_area_phys = pci_resource_start(pci, 0);
2380 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2381 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002382 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002385 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 -07002386 snd_ymfpci_free(chip);
2387 return -EBUSY;
2388 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002389 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2390 "YMFPCI", chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002391 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 snd_ymfpci_free(chip);
2393 return -EBUSY;
2394 }
2395 chip->irq = pci->irq;
2396
2397 snd_ymfpci_aclink_reset(pci);
2398 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2399 snd_ymfpci_free(chip);
2400 return -EIO;
2401 }
2402
Clemens Ladisch102fa902006-10-11 12:05:59 +02002403 err = snd_ymfpci_request_firmware(chip);
2404 if (err < 0) {
2405 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2406 snd_ymfpci_free(chip);
2407 return err;
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 snd_ymfpci_download_image(chip);
2410
2411 udelay(100); /* seems we need a delay after downloading image.. */
2412
2413 if (snd_ymfpci_memalloc(chip) < 0) {
2414 snd_ymfpci_free(chip);
2415 return -EIO;
2416 }
2417
2418 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2419 snd_ymfpci_free(chip);
2420 return err;
2421 }
2422
2423#ifdef CONFIG_PM
2424 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2425 if (chip->saved_regs == NULL) {
2426 snd_ymfpci_free(chip);
2427 return -ENOMEM;
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429#endif
2430
2431 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2432 snd_ymfpci_free(chip);
2433 return err;
2434 }
2435
2436 snd_ymfpci_proc_init(card, chip);
2437
2438 snd_card_set_dev(card, &pci->dev);
2439
2440 *rchip = chip;
2441 return 0;
2442}